Fix TUI output with patch_stdout

- Add patch_stdout to capture external prints in TUI mode
- Any stdout from handlers/pump appears in output area, not prompt
- Simplify response handler logic

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
dullfig 2026-01-11 14:34:03 -08:00
parent 01598bd708
commit cc5257d342
2 changed files with 14 additions and 15 deletions

View file

@ -313,7 +313,11 @@ class TUIConsole:
self.print_raw(f"Type /help for commands, @listener message to chat", "output.dim") self.print_raw(f"Type /help for commands, @listener message to chat", "output.dim")
self.print_raw("", "output") self.print_raw("", "output")
# Patch stdout so any external prints go to our output area
from prompt_toolkit.patch_stdout import patch_stdout
try: try:
with patch_stdout(raw=True):
await self.app.run_async() await self.app.run_async()
except Exception as e: except Exception as e:
print(f"Console error: {e}") print(f"Console error: {e}")

View file

@ -125,23 +125,18 @@ async def handle_response_print(payload: ShoutedResponse, metadata: HandlerMetad
Print the final response to the console. Print the final response to the console.
Routes output to the TUI console if available, otherwise prints to stdout. Routes output to the TUI console if available, otherwise prints to stdout.
With TUI, stdout is patched so prints appear in the output area.
""" """
console = None console = None
try: try:
from run_organism import get_console from run_organism import get_console
console = get_console() console = get_console()
except ImportError as e: except ImportError:
console = None pass
if console is not None: if console is not None and hasattr(console, 'on_response'):
if hasattr(console, 'on_response'): # Use TUI's output buffer directly (preferred)
try:
console.on_response("shouter", payload) console.on_response("shouter", payload)
return # Success - exit without fallback print else:
except Exception as e: # Print to stdout (TUI patches this, simple mode shows directly)
pass # Fall through to fallback print(f"\033[36m[response] {payload.message}\033[0m")
# Console exists but no on_response - shouldn't happen
# Fallback only runs if console is None
# This should NOT print if TUI is running
return None