docs: 对齐 Ollama 官方 Tool Calling 文档,补充关键注释

对照 docs.ollama.com/capabilities/tool-calling JavaScript 示例:
- 确认 assistant 消息格式:content + thinking + tool_calls 三者共存
- 确认工具结果格式:{role: 'tool', tool_name, content}
- 确认 Agent Loop 结构与官方一致
- 补充流式 tool_calls 累积逻辑的注释
This commit is contained in:
Metona Dev
2026-04-06 20:43:33 +08:00
parent a9c56582d0
commit e71a307f33
+4
View File
@@ -163,6 +163,7 @@ export async function runAgentLoop(
if (chunk.message?.tool_calls?.length) { if (chunk.message?.tool_calls?.length) {
for (const tc of chunk.message.tool_calls) { for (const tc of chunk.message.tool_calls) {
if (tc.function?.name) { if (tc.function?.name) {
// 新的工具调用
toolCalls.push({ toolCalls.push({
type: 'function', type: 'function',
function: { function: {
@@ -171,6 +172,7 @@ export async function runAgentLoop(
} }
}); });
} else if (toolCalls.length > 0) { } else if (toolCalls.length > 0) {
// 增量 arguments 追加到当前最后一个 tool_call
const last = toolCalls[toolCalls.length - 1]; const last = toolCalls[toolCalls.length - 1];
if (tc.function?.arguments && typeof tc.function.arguments === 'object') { if (tc.function?.arguments && typeof tc.function.arguments === 'object') {
Object.assign(last.function.arguments, tc.function.arguments); Object.assign(last.function.arguments, tc.function.arguments);
@@ -223,6 +225,8 @@ export async function runAgentLoop(
}; };
if (toolCalls.length > 0) { if (toolCalls.length > 0) {
assistantMsg.tool_calls = toolCalls; assistantMsg.tool_calls = toolCalls;
// 官方示例要求:tool_calls 时同时保留 content 和 thinking
// 确保 content 不为空(模型可能在调用工具前说了话)
} }
messages.push(assistantMsg); messages.push(assistantMsg);