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绿+交叉编译
This commit is contained in:
1 parent
57d5d22d87
commit
f347681034
7 files changed
+332
-33
No files matched your search
@@ -6,7 +6,6 @@ import (
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/agent"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/capacity"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/tool"
|
||||
)
|
||||
|
||||
// buildRequest assembles a model.Request from the agent's public surface,
|
||||
@@ -164,26 +163,11 @@ func (l *Loop) capacityRecall(ctx context.Context, messages []model.Message) []c
|
||||
return items
|
||||
}
|
||||
|
||||
// decodeToolCalls decodes tool calls from a model.Response using the same two
|
||||
// conventions as the agent package: the normalized RoleTool placeholder, then a
|
||||
// JSON envelope of the form {"tool_calls":[...]} in Content. It reports ok=true
|
||||
// (with a non-empty slice) when a call is decoded.
|
||||
// decodeToolCalls decodes tool calls from a model.Response via the agent's shared
|
||||
// decoder, so the streaming and non-streaming paths agree on the provider-native
|
||||
// tool calls the adapters emit (Anthropic tool_use / OpenAI tool_calls) as well
|
||||
// as the legacy JSON-envelope convention. It reports ok=true (with a non-empty
|
||||
// slice) when a call is decoded.
|
||||
func decodeToolCalls(resp model.Response) ([]agent.ToolCall, bool) {
|
||||
if resp.Message.Role == model.RoleAssistant && resp.Message.ToolCallName != "" {
|
||||
return []agent.ToolCall{{Name: resp.Message.ToolCallName, Args: map[string]any{}}}, true
|
||||
}
|
||||
var payload struct {
|
||||
ToolCalls []agent.ToolCall `json:"tool_calls"`
|
||||
}
|
||||
// Lenient decode repairs common model-emitted malformations (code fences,
|
||||
// trailing commas) so a slightly broken tool-call envelope does not turn into
|
||||
// a text answer; an unrecoverable payload still yields ok=false and the round
|
||||
// proceeds as text.
|
||||
if err := tool.LenientJSONUnmarshal([]byte(resp.Content), &payload); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
if len(payload.ToolCalls) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
return payload.ToolCalls, true
|
||||
return agent.ExtractToolCalls(resp)
|
||||
}
|
||||
Reference in new issue
Block a user