|
| 1 | +"""Smart stdout filter for selectively suppressing output. |
| 2 | +
|
| 3 | +Allows interactive prompts to pass through while suppressing |
| 4 | +accumulated agent output during streaming. |
| 5 | +""" |
| 6 | + |
| 7 | +import io |
| 8 | +import re |
| 9 | + |
| 10 | + |
| 11 | +class SmartStdoutFilter(io.StringIO): |
| 12 | + """Stdout wrapper that detects and passes through interactive prompts. |
| 13 | +
|
| 14 | + Suppresses accumulated agent output during streaming but allows |
| 15 | + tool confirmation prompts to display normally. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, real_stdout): |
| 19 | + """Initialize the filter. |
| 20 | +
|
| 21 | + Args: |
| 22 | + real_stdout: The real sys.stdout to pass prompts through to |
| 23 | + """ |
| 24 | + super().__init__() |
| 25 | + self.real_stdout = real_stdout |
| 26 | + self.buffer = "" |
| 27 | + |
| 28 | + # Patterns that indicate an interactive prompt |
| 29 | + self.prompt_patterns = [ |
| 30 | + r'\? *$', # Ends with ? (e.g., "Delete file?") |
| 31 | + r'[Yy]/[Nn] *:? *$', # Contains Y/n (e.g., "Continue? Y/n:") |
| 32 | + r': *$', # Ends with : (e.g., "Enter name:") |
| 33 | + r'\[.*\] *:? *$', # Ends with [option]: (e.g., "Choose [Y/n]:") |
| 34 | + r'> *$', # Ends with > (shell-like prompt) |
| 35 | + ] |
| 36 | + |
| 37 | + def write(self, text: str) -> int: |
| 38 | + """Write text, passing through if it looks like a prompt. |
| 39 | +
|
| 40 | + Args: |
| 41 | + text: Text to write |
| 42 | +
|
| 43 | + Returns: |
| 44 | + Number of characters written |
| 45 | + """ |
| 46 | + if not text: |
| 47 | + return 0 |
| 48 | + |
| 49 | + # Accumulate text for pattern detection |
| 50 | + self.buffer += text |
| 51 | + |
| 52 | + # Check if this looks like an interactive prompt |
| 53 | + if self._looks_like_prompt(): |
| 54 | + # Pass through to real stdout |
| 55 | + self.real_stdout.write(text) |
| 56 | + self.real_stdout.flush() |
| 57 | + return len(text) |
| 58 | + |
| 59 | + # Otherwise suppress it (accumulate in buffer) |
| 60 | + return super().write(text) |
| 61 | + |
| 62 | + def _looks_like_prompt(self) -> bool: |
| 63 | + """Check if buffered text looks like an interactive prompt. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + True if text appears to be a prompt |
| 67 | + """ |
| 68 | + # Get the last line (prompts are typically on their own line) |
| 69 | + lines = self.buffer.split('\n') |
| 70 | + last_line = lines[-1] if lines else "" |
| 71 | + |
| 72 | + # Also check the previous line in case of multi-line prompts |
| 73 | + prev_line = lines[-2] if len(lines) > 1 else "" |
| 74 | + |
| 75 | + # Check both lines against patterns |
| 76 | + for pattern in self.prompt_patterns: |
| 77 | + if re.search(pattern, last_line): |
| 78 | + return True |
| 79 | + if re.search(pattern, prev_line): |
| 80 | + return True |
| 81 | + |
| 82 | + return False |
| 83 | + |
| 84 | + def flush(self): |
| 85 | + """Flush both buffers.""" |
| 86 | + super().flush() |
| 87 | + self.real_stdout.flush() |
| 88 | + |
| 89 | + def get_suppressed_output(self) -> str: |
| 90 | + """Get the output that was suppressed. |
| 91 | +
|
| 92 | + Useful for debugging what was filtered out. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + The suppressed output text |
| 96 | + """ |
| 97 | + return self.getvalue() |
0 commit comments