fix: persistent event loop in CLI interview — no more Event loop is closed #42
@@ -179,28 +179,44 @@ def interview(
|
|||||||
|
|
||||||
typer.echo("Initializing Timmy for interview...\n")
|
typer.echo("Initializing Timmy for interview...\n")
|
||||||
|
|
||||||
# Force agent creation by calling chat once with a warm-up prompt
|
# Use a single persistent event loop for the entire interview.
|
||||||
|
# Calling asyncio.run() per question kills the loop between calls,
|
||||||
|
# orphaning MCP stdio transports and causing "Event loop is closed."
|
||||||
|
loop = asyncio.new_event_loop()
|
||||||
|
asyncio.set_event_loop(loop)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
asyncio.run(
|
# Force agent creation by calling chat once with a warm-up prompt
|
||||||
chat("Hello, Timmy. We're about to start your interview.", session_id="interview")
|
try:
|
||||||
|
loop.run_until_complete(
|
||||||
|
chat("Hello, Timmy. We're about to start your interview.", session_id="interview")
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
typer.echo(f"Warning: Initialization issue — {exc}", err=True)
|
||||||
|
|
||||||
|
def _on_answer(entry: InterviewEntry) -> None:
|
||||||
|
typer.echo(f"[{entry.category}]")
|
||||||
|
typer.echo(f" Q: {entry.question}")
|
||||||
|
typer.echo(f" A: {entry.answer}")
|
||||||
|
typer.echo()
|
||||||
|
|
||||||
|
typer.echo("Starting interview...\n")
|
||||||
|
transcript = run_interview(
|
||||||
|
chat_fn=lambda msg: loop.run_until_complete(chat(msg, session_id="interview")),
|
||||||
|
on_answer=_on_answer,
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
|
||||||
typer.echo(f"Warning: Initialization issue — {exc}", err=True)
|
|
||||||
|
|
||||||
def _on_answer(entry: InterviewEntry) -> None:
|
# Print full transcript at the end
|
||||||
typer.echo(f"[{entry.category}]")
|
typer.echo("\n" + format_transcript(transcript))
|
||||||
typer.echo(f" Q: {entry.question}")
|
finally:
|
||||||
typer.echo(f" A: {entry.answer}")
|
# Clean shutdown: close MCP sessions, then the loop
|
||||||
typer.echo()
|
try:
|
||||||
|
from timmy.mcp_tools import close_mcp_sessions
|
||||||
|
|
||||||
typer.echo("Starting interview...\n")
|
loop.run_until_complete(close_mcp_sessions())
|
||||||
transcript = run_interview(
|
except Exception:
|
||||||
chat_fn=lambda msg: asyncio.run(chat(msg, session_id="interview")),
|
pass
|
||||||
on_answer=_on_answer,
|
loop.close()
|
||||||
)
|
|
||||||
|
|
||||||
# Print full transcript at the end
|
|
||||||
typer.echo("\n" + format_transcript(transcript))
|
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
|
|||||||
Reference in New Issue
Block a user