fix(copilot): serialize tool output to standard JSON format for frontend parsing

Changed tool output serialization in AgentService._convert_event_to_chunk()
from str() to json.dumps() to ensure structured data (dict/list) is properly
formatted as standard JSON instead of Python string representation.

Changes:
- Added json import to agent_service.py
- Modified on_tool_end event handling to use json.dumps(output, ensure_ascii=False, indent=2)
- Updated ai-chat-api-design.md to document tool_output format

Benefits:
- Frontend can parse tool results with standard JSON.parse()
- Chinese and non-ASCII characters are preserved (not escaped)
- Formatted output (indent=2) improves readability

Co-Authored-By: YueGuobin <yueguobin@outlook.com>
This commit is contained in:
YueGuobin committed 2026-03-09 15:15:50 +08:00
1 parent 13a032ea2c
commit 0f2d0e2859
2 files changed
+12 -3

No files matched your search

@@ -32,6 +32,7 @@ in the project directory.
"""
import asyncio
import json
import logging
import os
from datetime import datetime
@@ -460,9 +461,10 @@ class AgentService:
elif event_type == "on_tool_end":
# Tool execution completed
output = data.get("output", "")
# Convert output to string if it's not already
# Convert output to JSON string if it's not already a string
# This ensures dict/list outputs are properly serialized for frontend parsing
if not isinstance(output, str):
output = str(output)
output = json.dumps(output, ensure_ascii=False, indent=2)
return {
"type": "tool_end",
"tool_name": event.get("name", ""),