feat(agent): update SSE event schema and handle tool calls from LLM

- Update AI chat API documentation with revised SSE event schema
- Add support for multiple tool calls in `tool_call` events
- Include `session_id` in all event types for better session tracking
- Implement `on_chat_model_end` handler to process LLM tool call decisions
- Update example JSON payloads to reflect new schema structure
This commit is contained in:
YueGuobin committed 2026-03-05 22:03:27 +08:00
1 parent 54775faaf4
commit 1de7d00db3
2 files changed
+46 -11

No files matched your search

@@ -410,6 +410,28 @@ class AgentService:
if content:
return {"type": "content", "content": content}
elif event_type == "on_chat_model_end":
# LLM call completed, check if it decided to call tools
output = data.get("output", {})
if hasattr(output, "tool_calls") and output.tool_calls:
# Extract tool calls information
tool_calls_data = []
for tc in output.tool_calls:
# Convert to dict if it's an object
tc_dict = tc if isinstance(tc, dict) else tc.model_dump()
tool_calls_data.append(
{
"id": tc_dict.get("id", ""),
"name": tc_dict.get("name", ""),
"args": tc_dict.get("args", {}),
}
)
return {
"type": "tool_call",
"tool_calls": tool_calls_data,
"session_id": session_id,
}
elif event_type == "on_tool_start":
# Tool execution started
return {"type": "tool_start", "tool_name": event.get("name", ""), "session_id": session_id}