Files
Cyrene_v2/internal/runtime/loop/tool_history.go
T
AskaEth f347681034
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
fix(loop): 原生工具调用从 Response.ToolCalls 解码 + 逐次回填工具结果 — 修复工具消息无回复
- 根因: loop/agent.decodeToolCalls 只读 resp.Message.ToolCallName/resp.Content JSON信封, 从不看 api 原生解析出的 resp.ToolCalls(Anthropic tool_use / OpenAI tool_calls)
  → 模型返回 thinking+tool_use(无text) 被判定'无工具调用' → 空 Content 当最终 text → res.Text="" → no reply produced; 纯聊天消息不调工具直接出 text, 故表现为'部分消息无回复'
- agent/step.go: ToolCall 增 ID; ExtractToolCalls 导出并优先 resp.ToolCalls(保留原生id+归一化nil Args), 回落两旧约定
- loop/request.go: decodeToolCalls 委托 agent.ExtractToolCalls(流式/非流式共用)
- loop/tool_history.go(新): 原生批次回填 assistant tool_use + 带tool_use_id的tool_result; 旧JSON信封批次保持 RoleTool 纯文本(向后兼容)
- openai.go: tool_call parts 编成 tool_calls + tool_call_id
- 新增 native_toolcall_test + native_toolcall_http_test(httptest假Anthropic SSE: round1 tool_use(无text)→round2含tool_use_id/tool_result→res.Text非空)
- build/vet/test绿+交叉编译
2026-09-08 22:37:05 +08:00

50 lines
1.7 KiB
Go

package loop
import (
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/agent"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
)
// appendToolCallHistory renders the assistant tool-call turn followed by every
// tool result, in call order. When every call carries a provider id (native
// Anthropic tool_use / OpenAI tool_calls), the turn is emitted as native
// tool_use + tool_result blocks so the provider can correlate each result to its
// call; a batch without ids (the legacy JSON-envelope convention) keeps the
// plain RoleTool text feed-back shape.
func appendToolCallHistory(messages []model.Message, calls []agent.ToolCall, outcomes []ToolOutcome) []model.Message {
native := nativeModelToolCalls(calls)
if len(native) == 0 {
for i := range calls {
messages = append(messages, toolResultMessage(calls[i], outcomes[i]))
}
return messages
}
messages = append(messages, model.AssistantToolCallMessage(native))
for i := range calls {
isError := outcomes[i].Err != nil || outcomes[i].Skipped
messages = append(messages, model.ToolResultMessage(calls[i].ID, toolResultText(calls[i], outcomes[i]), isError))
}
return messages
}
// nativeModelToolCalls converts a batch into model.ToolCall only when every call
// carries a provider id; a batch without ids (the legacy JSON-envelope
// convention) returns nil so the caller keeps the plain-text feed-back shape.
func nativeModelToolCalls(calls []agent.ToolCall) []model.ToolCall {
if len(calls) == 0 {
return nil
}
out := make([]model.ToolCall, 0, len(calls))
for _, c := range calls {
if c.ID == "" {
return nil
}
args := c.Args
if args == nil {
args = map[string]any{}
}
out = append(out, model.ToolCall{ID: c.ID, Name: c.Name, Args: args})
}
return out
}