fix: 修复 Agent 流式 tool_call arguments 拼接错误及并行工具调用串扰问题#1355
Open
cyfung1031 wants to merge 9 commits intorelease/v1.4-agentfrom
Open
fix: 修复 Agent 流式 tool_call arguments 拼接错误及并行工具调用串扰问题#1355cyfung1031 wants to merge 9 commits intorelease/v1.4-agentfrom
cyfung1031 wants to merge 9 commits intorelease/v1.4-agentfrom
Conversation
* 删除 uint8Array 重复封装 * 添加 ResourceService.loadByUrl 单元测试 --------- Co-authored-by: 王一之 <yz@ggnb.top>
…避免 supply chain 攻击) (#1341) * pnpm cooldown: 不抓取一星期内最新版 * pnpm: 提升至 10.33.0 以使用 minimumReleaseAge 避免 supply chain 攻击 * minimumReleaseAge 延长至 30 天(43200 分钟) --------- Co-authored-by: 王一之 <yz@ggnb.top>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
在 Agent Chat 界面中,工具调用的参数(arguments)显示异常,开头出现多余的
{}前缀,例如:根本原因有两处:
问题一:
parseOpenAIStream的if/else if互斥逻辑(providers/openai.ts)部分 API 网关(OpenRouter、Azure 某些部署、本地 vllm)在首个 chunk 中会同时携带
name和arguments: "{}"(占位符)。tool_call_start携带"{}"后,后续真实 JSON 通过tool_call_delta追加,最终"{}" + '{"description":...}' = '{}{...}',与截图完全吻合。问题二:
tool_call_delta按length-1匹配(多处 consumer)background_session_manager.ts、ChatArea.tsx、sub_agent_service.ts中处理tool_call_delta时,均使用toolCalls[toolCalls.length - 1]来找目标工具,而非按id配对。OpenAI 并行返回多个 tool_call 时(用index区分),交错到达的 delta 会被写入错误的工具。另外,Anthropic parser 的
tool_call_delta固定 emitid: "",导致按 id 配对完全失效。变更内容
src/app/service/agent/core/types.tsLLMStreamEvent中tool_call_delta新增可选字段index?: number,用于并行工具调用的精确匹配src/app/service/agent/core/providers/openai.tsif/else if改为两个独立的if,tool_call_start的arguments永远为空字符串arguments同样作为tool_call_delta发出tool_call_delta透传tc.index,供 consumer 精确匹配src/app/service/agent/core/providers/anthropic.tstoolUseByIndex: Map<number, { id: string }>追踪每个content_block的 index 与 id 对应关系input_json_delta发出的tool_call_delta现在携带正确的id和index(原来id固定为"")content_block_stop时清理对应 index 条目src/app/service/agent/service_worker/background_session_manager.tstool_call_delta匹配逻辑改为:按id精确匹配 → 按index匹配 → fallback 最近status=running的工具src/pages/options/routes/AgentChat/ChatArea.tsxtool_call_delta分支采用相同的三级匹配逻辑,替换原length-1写法src/app/service/agent/service_worker/sub_agent_service.tsrunSubAgentCore中tool_call_delta分支同上,采用三级匹配逻辑测试更新
openai.test.ts:新增并行 tool_call 按 index 分派测试;更新两个因 parser 行为变更而需调整断言的旧测试(event 数量 +1,tool_call_start.arguments改为断言空字符串)anthropic.test.ts:新增input_json_delta携带正确 id 和 index 的测试background.test.ts:新增并行 tool_call 交错 delta 按 index 正确分派的测试不受影响的文件
src/types/scriptcat.d.ts/scriptcat.zh-CN.d.ts:公开给 userscript 的StreamChunk接口不含tool_call_delta,无需修改src/app/service/content/gm_api/cat_agent.ts:processStream不处理tool_call_delta事件,无需修改ToolCall类型)不变