fix: avoid Agent SDK race on greeting + clear session on abort

1. Change on_enter greeting from generate_reply() to session.say() with
   a static message — avoids spawning an Agent SDK task just for a greeting,
   which caused a race condition when the user speaks before it completes.

2. Clear agent session ID when receiving abort/exit errors so the next
   task starts a fresh session instead of trying to resume a dead process.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-02 02:22:52 -08:00
parent a78e2cd923
commit 7c9fabd891
2 changed files with 13 additions and 4 deletions

View File

@ -93,10 +93,14 @@ class IT0VoiceAgent(Agent):
)
async def on_enter(self):
"""Called when the agent becomes active — greet the user."""
self.session.generate_reply(
instructions="用一句简短的话打招呼告诉用户你是IT0运维助手可以帮助什么。"
)
"""Called when the agent becomes active — greet the user.
Uses session.say() with a static message instead of generate_reply()
to avoid triggering the Agent SDK / LLM pipeline for a simple greeting.
This prevents a race condition when the user speaks before the
greeting LLM task completes.
"""
self.session.say("你好我是IT0运维助手有什么可以帮你的")
# ---------------------------------------------------------------------------

View File

@ -331,6 +331,11 @@ class AgentServiceLLMStream(llm.LLMStream):
elif evt_type == "error":
err_msg = evt_data.get("message", "Unknown error")
logger.error("Agent error: %s", err_msg)
# Clear session so next task starts fresh
# (don't try to resume a dead/aborted session)
if "aborted" in err_msg.lower() or "exited" in err_msg.lower():
logger.info("Clearing agent session after abort/exit")
self._llm_instance._agent_session_id = None
self._event_ch.send_nowait(
llm.ChatChunk(
id=request_id,