fix: extract tool results from messages array in AI SDK v6
- In AI SDK v6, tool results are in fullResponse.messages with role 'tool' - Updated result extraction to iterate through messages array - Added detailed logging to debug response structure - Handle both text output and tool-only responses 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
56e0c0da79
commit
ac8f6d239c
1 changed files with 46 additions and 1 deletions
|
|
@ -227,15 +227,60 @@ export async function executeToolWithAgent(
|
|||
|
||||
console.log('[executeToolWithAgent] Starting text stream consumption');
|
||||
|
||||
// In AI SDK v6, we need to handle both text and tool calls
|
||||
// The response might be ONLY a tool call with no text
|
||||
let toolCallResult: unknown = null;
|
||||
|
||||
// Stream and collect text
|
||||
for await (const chunk of result.textStream) {
|
||||
console.log('[executeToolWithAgent] Received chunk:', chunk);
|
||||
console.log('[executeToolWithAgent] Received text chunk:', chunk);
|
||||
fullOutput += chunk;
|
||||
onChunk?.(chunk);
|
||||
}
|
||||
|
||||
console.log('[executeToolWithAgent] Text stream complete, fullOutput length:', fullOutput.length);
|
||||
|
||||
// Get the full response including tool calls
|
||||
const fullResponse = await result.response;
|
||||
console.log('[executeToolWithAgent] Full response:', JSON.stringify(fullResponse, null, 2));
|
||||
console.log('[executeToolWithAgent] Response keys:', Object.keys(fullResponse));
|
||||
|
||||
// In AI SDK v6, check the messages array for tool calls
|
||||
if (fullResponse.messages && fullResponse.messages.length > 0) {
|
||||
console.log('[executeToolWithAgent] Messages detected:', fullResponse.messages.length);
|
||||
|
||||
for (const message of fullResponse.messages) {
|
||||
console.log('[executeToolWithAgent] Message role:', message.role);
|
||||
console.log('[executeToolWithAgent] Message keys:', Object.keys(message));
|
||||
|
||||
// Tool results are in messages with role 'tool'
|
||||
if (message.role === 'tool' && 'content' in message) {
|
||||
toolCallResult = message.content;
|
||||
console.log('[executeToolWithAgent] Tool message result:', toolCallResult);
|
||||
|
||||
// If there's no text output, use the tool result as the output
|
||||
if (fullOutput.length === 0 && toolCallResult) {
|
||||
fullOutput =
|
||||
typeof toolCallResult === 'string'
|
||||
? toolCallResult
|
||||
: JSON.stringify(toolCallResult, null, 2);
|
||||
|
||||
// Stream the tool result
|
||||
onChunk?.(fullOutput);
|
||||
console.log(
|
||||
'[executeToolWithAgent] Using tool result as output, length:',
|
||||
fullOutput.length
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Also check assistant messages for tool calls
|
||||
if (message.role === 'assistant' && 'toolCalls' in message) {
|
||||
console.log('[executeToolWithAgent] Assistant message has toolCalls:', message.toolCalls);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate final token breakdown
|
||||
const parameters = Array.isArray(tool.parameters)
|
||||
? (tool.parameters as unknown as TPMJSParameter[])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue