From 917bf3e40441159de7e8c98f442a309128168c8d Mon Sep 17 00:00:00 2001 From: kimi Date: Fri, 20 Mar 2026 23:33:14 -0400 Subject: [PATCH] refactor: extract _read_message_input and _resolve_session_id from chat() Break up the 72-line chat() function into a thin orchestrator by extracting stdin reading logic into _read_message_input() and session resolution into _resolve_session_id(). Fixes #627 Co-Authored-By: Claude Opus 4.6 --- src/timmy/cli.py | 58 ++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/timmy/cli.py b/src/timmy/cli.py index 51051128..1dee908b 100644 --- a/src/timmy/cli.py +++ b/src/timmy/cli.py @@ -143,6 +143,35 @@ def think( timmy.print_response(f"Think carefully about: {topic}", stream=True, session_id=_CLI_SESSION_ID) +def _read_message_input(message: list[str]) -> str: + """Join CLI arguments and read from stdin when appropriate.""" + message_str = " ".join(message) + + if message_str == "-" or not _is_interactive(): + try: + stdin_content = sys.stdin.read().strip() + except (KeyboardInterrupt, EOFError): + stdin_content = "" + if stdin_content: + message_str = stdin_content + elif message_str == "-": + typer.echo("No input provided via stdin.", err=True) + raise typer.Exit(1) + + return message_str + + +def _resolve_session_id(session_id: str | None, new_session: bool) -> str: + """Return the effective session ID based on CLI flags.""" + import uuid + + if session_id is not None: + return session_id + if new_session: + return str(uuid.uuid4()) + return _CLI_SESSION_ID + + @app.command() def chat( message: list[str] = typer.Argument( @@ -179,38 +208,13 @@ def chat( Read from stdin by passing "-" as the message or piping input. """ - import uuid - - # Join multiple arguments into a single message string - message_str = " ".join(message) - - # Handle stdin input if "-" is passed or stdin is not a tty - if message_str == "-" or not _is_interactive(): - try: - stdin_content = sys.stdin.read().strip() - except (KeyboardInterrupt, EOFError): - stdin_content = "" - if stdin_content: - message_str = stdin_content - elif message_str == "-": - typer.echo("No input provided via stdin.", err=True) - raise typer.Exit(1) - - if session_id is not None: - pass # use the provided value - elif new_session: - session_id = str(uuid.uuid4()) - else: - session_id = _CLI_SESSION_ID + message_str = _read_message_input(message) + session_id = _resolve_session_id(session_id, new_session) timmy = create_timmy(backend=backend, session_id=session_id) - # Use agent.run() so we can intercept paused runs for tool confirmation. run_output = timmy.run(message_str, stream=False, session_id=session_id) - - # Handle paused runs — dangerous tools need user approval run_output = _handle_tool_confirmation(timmy, run_output, session_id, autonomous=autonomous) - # Print the final response content = run_output.content if hasattr(run_output, "content") else str(run_output) if content: from timmy.session import _clean_response -- 2.43.0