# input_region.py — Rule-based input region extraction and normalization (T2 & T3) import re from lib_py.agents.registry import get_adapter def extract_input_region(pane_text: str, agent_name: str) -> str: """ Extracts the input area text between horizontal rule separators (─{10,}) for a given agent. If rules are absent, falls back to full pane_text to support mocks and unruled panes. Strips leading prompt tokens ('>', '❯') and placeholders ('Ask anything...'). Raises ValueError for unsupported agents (like hermes with no input area facts). """ adapter = get_adapter(agent_name) if not adapter or not adapter.input_prompt: raise ValueError(f"Agent {agent_name!r} has no input area facts") rule_pattern = adapter.input_rule_pattern or r'─{10,}' rule_re = re.compile(rule_pattern) lines = pane_text.splitlines() rule_indices = [i for i, line in enumerate(lines) if rule_re.search(line)] if len(rule_indices) >= 2: # Exclude the rule lines themselves: lines between rule_indices[-2] + 1 and rule_indices[-1] region_lines = lines[rule_indices[-2] + 1:rule_indices[-1]] elif len(rule_indices) == 1: region_lines = lines[rule_indices[0] + 1:] else: region_lines = lines region_text = "\n".join(region_lines) prompt = adapter.input_prompt if prompt: region_text = re.sub(r'^\s*' + re.escape(prompt) + r'\s*', '', region_text, flags=re.MULTILINE) placeholder = adapter.input_placeholder if placeholder: region_text = region_text.replace(placeholder, '') return region_text.strip()