fix: non-streaming tool calls now feed results back to AI for final answer

Previously tool calls in non-streaming path returned raw tool output as the
response. Now executes tool, sends results back to model for a synthesized
answer. Fixes the 'silent after streaming fallback' bug.
This commit is contained in:
admin
2026-05-05 17:09:32 +00:00
Unverified
parent 092fefbc52
commit 78349bb22b

View File

@@ -390,20 +390,28 @@ export async function initBot(config, api, tools, skills, agents) {
const msg = choice.message;
if (msg.tool_calls?.length) {
const parts = [];
// Execute all tool calls, feed results back to AI for final answer
const toolMessages = [{ role: 'assistant', tool_calls: msg.tool_calls }];
for (const tc of msg.tool_calls) {
const fn = tc.function;
try {
const handler = toolHandlers[fn.name];
if (!handler) { parts.push(`❌ Unknown tool: ${fn.name}`); continue; }
if (!handler) {
toolMessages.push({ role: 'tool', tool_call_id: tc.id, content: `❌ Unknown tool: ${fn.name}` });
continue;
}
const args = JSON.parse(fn.arguments);
logger.info(`🔧 ${fn.name}(${fn.arguments?.slice(0, 80)})`);
const result = await handler(args);
parts.push(`${result}`);
toolMessages.push({ role: 'tool', tool_call_id: tc.id, content: String(result).slice(0, 4000) });
} catch (e) {
parts.push(`❌ Tool ${fn.name} error: ${e.message}`);
toolMessages.push({ role: 'tool', tool_call_id: tc.id, content: `❌ Error: ${e.message}` });
}
}
return parts.join('\n\n');
// Ask AI to produce final text answer using tool results
const followUp = [...body.messages, ...toolMessages, { role: 'user', content: 'Based on the tool results above, provide your final answer.' }];
const followRes = await api.client.post('/chat/completions', { ...body, messages: followUp, tools: [] });
return followRes.data.choices?.[0]?.message?.content || '✅ Done.';
}
return msg.content || '✅ Done.';
} catch (error) {