fix(backend/chat): Address additional PR review comments

- Add terminal StreamFinish in adapt_sdk_stream if SDK ends without one
- Sanitize error message in adapt_sdk_stream exception handler
- Pass full JSON schema (type, properties, required) to tool decorator
This commit is contained in:
Zamil Majdy
2026-02-08 07:14:45 +04:00
parent 5efb80d47b
commit b9c759ce4f
3 changed files with 15 additions and 3 deletions

View File

@@ -282,6 +282,7 @@ async def adapt_sdk_stream(
# Emit start immediately
yield StreamStart(messageId=adapter.message_id, taskId=task_id)
finished = False
try:
async for sdk_message in sdk_stream:
responses = adapter.convert_message(sdk_message)
@@ -289,12 +290,19 @@ async def adapt_sdk_stream(
# Skip duplicate start messages
if isinstance(response, StreamStart):
continue
if isinstance(response, StreamFinish):
finished = True
yield response
except Exception as e:
logger.error(f"Error in SDK stream: {e}", exc_info=True)
yield StreamError(
errorText=f"Stream error: {str(e)}",
errorText="An error occurred. Please try again.",
code="stream_error",
)
yield StreamFinish()
return
# Ensure terminal StreamFinish if SDK stream ended without one
if not finished:
yield StreamFinish()

View File

@@ -350,7 +350,6 @@ async def stream_chat_completion_sdk(
# Receive messages from the SDK
async for sdk_msg in client.receive_messages():
for response in adapter.convert_message(sdk_msg):
if isinstance(response, StreamStart):
continue

View File

@@ -179,10 +179,15 @@ def create_copilot_mcp_server():
# Create the decorated tool
# The @tool decorator expects (name, description, schema)
# Pass full JSON schema with type, properties, and required
decorated = tool(
tool_name,
base_tool.description,
base_tool.parameters.get("properties", {}),
{
"type": "object",
"properties": base_tool.parameters.get("properties", {}),
"required": base_tool.parameters.get("required", []),
},
)(handler)
sdk_tools.append(decorated)