mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-15 12:55:51 +00:00
fix: 防止 <available-deferred-tools> 在每轮 API 调用中重复注入
使用模块级 Set 缓存已注入的 deferred tool 列表,diff 后仅在有 新增工具时重新注入。根因:注入消息追加到 queryModel 的局部变量 messagesForAPI,不写入消息历史,所以每次调用都是首次。
This commit is contained in:
@@ -1036,6 +1036,16 @@ export function stripExcessMediaItems(
|
|||||||
}) as (UserMessage | AssistantMessage)[]
|
}) as (UserMessage | AssistantMessage)[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module-level cache of deferred-tool lines that have already been announced
|
||||||
|
* via <available-deferred-tools>. Because the injection is ephemeral (appended
|
||||||
|
* to a local `messagesForAPI` that is never persisted back into the caller's
|
||||||
|
* message history), we cannot scan history to detect prior injections — the
|
||||||
|
* injected message is gone after each API call. Instead we keep this Set so we
|
||||||
|
* only re-inject when new deferred tools appear (e.g. MCP server connects).
|
||||||
|
*/
|
||||||
|
const lastAnnouncedDeferredTools = new Set<string>()
|
||||||
|
|
||||||
async function* queryModel(
|
async function* queryModel(
|
||||||
messages: Message[],
|
messages: Message[],
|
||||||
systemPrompt: SystemPrompt,
|
systemPrompt: SystemPrompt,
|
||||||
@@ -1385,21 +1395,33 @@ async function* queryModel(
|
|||||||
// via persisted deferred_tools_delta attachments instead of this
|
// via persisted deferred_tools_delta attachments instead of this
|
||||||
// ephemeral prepend (which busts cache whenever the pool changes).
|
// ephemeral prepend (which busts cache whenever the pool changes).
|
||||||
if (useSearchExtraTools && !isDeferredToolsDeltaEnabled()) {
|
if (useSearchExtraTools && !isDeferredToolsDeltaEnabled()) {
|
||||||
|
// Diff current deferred tools against what's already been announced in
|
||||||
|
// prior <available-deferred-tools> injections. Only re-inject when new
|
||||||
|
// tools appear (e.g. MCP server connects mid-session).
|
||||||
const deferredToolList = tools
|
const deferredToolList = tools
|
||||||
.filter(t => deferredToolNames.has(t.name))
|
.filter(t => deferredToolNames.has(t.name))
|
||||||
.map(formatDeferredToolLine)
|
.map(formatDeferredToolLine)
|
||||||
.sort()
|
.sort()
|
||||||
.join('\n')
|
.join('\n')
|
||||||
|
|
||||||
if (deferredToolList) {
|
if (deferredToolList) {
|
||||||
// Append to the end of the messages array (not prepend) so it
|
const currentTools = new Set(deferredToolList.split('\n'))
|
||||||
// never抢占 <project-instructions> (CLAUDE.md) at the front.
|
const hasNewTools = [...currentTools].some(
|
||||||
messagesForAPI = [
|
t => !lastAnnouncedDeferredTools.has(t),
|
||||||
...messagesForAPI,
|
)
|
||||||
createUserMessage({
|
|
||||||
content: `<system-reminder>\n<available-deferred-tools>\n${deferredToolList}\n</available-deferred-tools>\nIMPORTANT: The tools listed above are deferred-loading — they are NOT in your tool list. To use them, you MUST first discover a tool via SearchExtraTools, then invoke it with ExecuteExtraTool.\n\nSearchExtraTools and ExecuteExtraTool are core tools already in your tool list right now — call them directly, do NOT use Bash/Glob to find them.\n\nSteps:\n1. SearchExtraTools({"query": "select:<tool_name>"}) — discover the tool and its schema\n2. ExecuteExtraTool({"tool_name": "<name>", "params": {...}}) — invoke it with correct parameters\n</system-reminder>`,
|
if (hasNewTools) {
|
||||||
isMeta: true,
|
lastAnnouncedDeferredTools.clear()
|
||||||
}),
|
for (const t of currentTools) lastAnnouncedDeferredTools.add(t)
|
||||||
]
|
|
||||||
|
messagesForAPI = [
|
||||||
|
...messagesForAPI,
|
||||||
|
createUserMessage({
|
||||||
|
content: `<system-reminder>\n<available-deferred-tools>\n${deferredToolList}\n</available-deferred-tools>\nIMPORTANT: The tools listed above are deferred-loading — they are NOT in your tool list. To use them, you MUST first discover a tool via SearchExtraTools, then invoke it with ExecuteExtraTool.\n\nSearchExtraTools and ExecuteExtraTool are core tools already in your tool list right now — call them directly, do NOT use Bash/Glob to find them.\n\nSteps:\n1. SearchExtraTools({"query": "select:<tool_name>"}) — discover the tool and its schema\n2. ExecuteExtraTool({"tool_name": "<name>", "params": {...}}) — invoke it with correct parameters\n</system-reminder>`,
|
||||||
|
isMeta: true,
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user