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

View file

@ -125,23 +125,18 @@ async def handle_response_print(payload: ShoutedResponse, metadata: HandlerMetad
Print the final response to the console.
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
try:
from run_organism import get_console
console = get_console()
except ImportError as e:
console = None
except ImportError:
pass
if console is not None:
if hasattr(console, 'on_response'):
try:
console.on_response("shouter", payload)
return # Success - exit without fallback print
except Exception as e:
pass # Fall through to fallback
# 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
if console is not None and hasattr(console, 'on_response'):
# Use TUI's output buffer directly (preferred)
console.on_response("shouter", payload)
else:
# Print to stdout (TUI patches this, simple mode shows directly)
print(f"\033[36m[response] {payload.message}\033[0m")