缺陷 / 高 / Agent 引擎
electron/harness/utils/token-estimator.ts
estimateMessagesTokens 处理 assistant 消息时:
if (msg.toolCalls) { msg.toolCalls.forEach(tc => { total += estimateTextTokens(JSON.stringify(tc.args)); // 忽略 tc.name 和 tc.id }); }
未计算 tc.name(工具名)和 tc.id(调用 ID)的 token。 实际上 OpenAI tokenizer 会将 tool_call 的完整结构(id、name、args)都计入 token。
tc.name
tc.id
if (msg.toolCalls) { msg.toolCalls.forEach(tc => { // id 通常 24 字符 (call_xxx) total += estimateTextTokens(tc.id ?? ''); // name 通常 5-20 字符 total += estimateTextTokens(tc.name ?? ''); // args JSON total += estimateTextTokens(JSON.stringify(tc.args ?? {})); // 结构开销 total += 8; // {"id":"","name":"","arguments":""} 等结构字符 }); }
同时处理 tool 消息的 tool_call_id 字段。
文件: electron/harness/utils/token-estimator.ts
修复: estimateMessagesTokens 的 toolCalls 估算补充 tc.id 和 tc.name,并加 8 token 结构开销(TOOL_CALL_OVERHEAD_TOKENS);新增 toolCallId 字段处理 tool 消息的 tool_call_id。
estimateMessagesTokens
TOOL_CALL_OVERHEAD_TOKENS
toolCallId
tool_call_id
验证: tsc --noEmit 类型检查通过。
tsc --noEmit
No dependencies set.
The note is not visible to the blocked user.
问题类型
缺陷 / 高 / Agent 引擎
文件位置
electron/harness/utils/token-estimator.ts问题描述
estimateMessagesTokens 处理 assistant 消息时:
未计算
tc.name(工具名)和tc.id(调用 ID)的 token。实际上 OpenAI tokenizer 会将 tool_call 的完整结构(id、name、args)都计入 token。
影响
建议修复
同时处理 tool 消息的 tool_call_id 字段。
修复说明
文件:
electron/harness/utils/token-estimator.ts修复:
estimateMessagesTokens的 toolCalls 估算补充tc.id和tc.name,并加 8 token 结构开销(TOOL_CALL_OVERHEAD_TOKENS);新增toolCallId字段处理 tool 消息的tool_call_id。验证:
tsc --noEmit类型检查通过。