fix(loop): 原生工具调用从 Response.ToolCalls 解码 + 逐次回填工具结果 — 修复工具消息无回复
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

- 根因: 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绿+交叉编译
This commit is contained in:
AskaEth committed 2026-09-08 22:37:05 +08:00
1 parent 57d5d22d87
commit f347681034
7 files changed
+332 -33

No files matched your search

+2 -2
View File
@@ -366,8 +366,8 @@ func TestRunStep_ContextCanceled(t *testing.T) {
func TestExtractToolCalls_NonJSONContent(t *testing.T) {
t.Parallel()
if _, ok := extractToolCalls(model.Response{Content: "plain text"}); ok {
t.Fatalf("extractToolCalls(plain) ok = true, want false")
if _, ok := ExtractToolCalls(model.Response{Content: "plain text"}); ok {
t.Fatalf("ExtractToolCalls(plain) ok = true, want false")
}
}
+30 -5
View File
@@ -25,6 +25,11 @@ const (
// ToolCall is a single function call the assistant requested, decoded from a
// step. Args holds the tool arguments as a JSON object.
type ToolCall struct {
// ID is the provider tool_use/tool_call id, so the loop can feed the result
// back as a native tool_result correlated to the exact call (Anthropic
// tool_use_id / OpenAI tool_call_id). Empty for the legacy JSON-envelope
// convention, where the provider emitted no id.
ID string `json:"id,omitempty"`
Name string `json:"name"`
Args map[string]any `json:"args"`
}
@@ -95,15 +100,17 @@ func (a *Agent) RunStep(ctx context.Context, sessionID string, messages []model.
}
}
if calls, ok := extractToolCalls(resp); ok {
if calls, ok := ExtractToolCalls(resp); ok {
return &StepResult{Final: false, ToolCalls: calls, StopReason: StopReasonToolCalls}, nil
}
return &StepResult{Final: true, Text: resp.Content, StopReason: StopReasonStop}, nil
}
// extractToolCalls decodes tool calls from a model.Response. The current model
// layer does not parse provider tool_calls into a structured field, so the agent
// uses two conventions in priority order:
// ExtractToolCalls decodes tool calls from a model.Response. It prefers the
// provider-native ToolCalls the adapters populate (Anthropic tool_use / OpenAI
// tool_calls), which carry the provider id needed to correlate the result and
// are the only shape a real native-tool provider emits. It then falls back to
// two legacy conventions used by prompt-engineered providers:
//
// 1. The normalized assistant-message placeholder: when resp.Message carries a
// ToolCallName it is a single call with no argument payload.
@@ -112,7 +119,10 @@ func (a *Agent) RunStep(ctx context.Context, sessionID string, messages []model.
//
// It reports ok=true (with a non-empty slice) when a call is decoded, and
// ok=false otherwise.
func extractToolCalls(resp model.Response) ([]ToolCall, bool) {
func ExtractToolCalls(resp model.Response) ([]ToolCall, bool) {
if len(resp.ToolCalls) > 0 {
return nativeToolCalls(resp.ToolCalls), true
}
if resp.Message.Role == model.RoleAssistant && resp.Message.ToolCallName != "" {
return []ToolCall{{Name: resp.Message.ToolCallName, Args: map[string]any{}}}, true
}
@@ -131,3 +141,18 @@ func extractToolCalls(resp model.Response) ([]ToolCall, bool) {
}
return payload.ToolCalls, true
}
// nativeToolCalls converts the adapter-decoded provider tool calls into the
// agent's ToolCall, preserving the provider id and normalizing a nil Args to an
// empty object so downstream fingerprinting and invocation never see nil.
func nativeToolCalls(calls []model.ToolCall) []ToolCall {
out := make([]ToolCall, 0, len(calls))
for _, c := range calls {
args := c.Args
if args == nil {
args = map[string]any{}
}
out = append(out, ToolCall{ID: c.ID, Name: c.Name, Args: args})
}
return out
}