Skip to content

Commit 2f5b7bd

Browse files
authored
fix: format toolcall result (opentiny#1637)
* fix: format toolcall result
1 parent 439aea7 commit 2f5b7bd

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

packages/plugins/robot/src/mcp/utils.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ export const serializeError = (err: unknown): string => {
5252
}
5353
}
5454

55+
const formatToolResult = (
56+
toolResult: string | { type: 'text'; text: string } | Array<{ type: 'text'; text: string }>
57+
) => {
58+
let result: any = toolResult
59+
if (Array.isArray(result) && result.length === 1) {
60+
result = result[0]
61+
}
62+
63+
if (typeof result === 'object' && result.type === 'text' && result.text) {
64+
result = result.text
65+
}
66+
67+
if (typeof result === 'string') {
68+
return result
69+
}
70+
71+
return JSON.stringify(result)
72+
}
73+
5574
const handleToolCall = async (
5675
res: LLMResponse,
5776
tools: RequestTool[],
@@ -99,8 +118,7 @@ const handleToolCall = async (
99118
toolCallResult = serializeError(error)
100119
}
101120
toolMessages.push({
102-
type: 'text',
103-
content: toolCallResult,
121+
content: formatToolResult(toolCallResult),
104122
role: 'tool',
105123
tool_call_id: tool.id
106124
})

0 commit comments

Comments
 (0)