From eb0b3556343b3ff8611d6bbe07de77192aa57cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=B5=B7?= <7836246@qq.com> Date: Wed, 4 Mar 2026 15:53:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8A=A8=E6=80=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=9C=A8=E7=BC=BA?= =?UTF-8?q?=E4=B9=8F=E6=B2=9F=E9=80=9A=E5=B7=A5=E5=85=B7=EF=BC=88=E5=A6=82?= =?UTF-8?q?Claude=20Code=EF=BC=89=E6=97=B6=E5=BE=AA=E7=8E=AF=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E6=97=A0=E6=84=8F=E4=B9=89=E5=8A=A8=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/converter.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/converter.ts b/src/converter.ts index d5bda2b..a69e3c5 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -48,7 +48,7 @@ function filterCoreTools(tools: AnthropicTool[]): AnthropicTool[] { * 策略:使用纯 JSON 格式代替 XML,避免触发 Claude 的安全过滤器 * (XML 标签如 容易被判定为提示词注入试图覆盖原生工具) */ -function buildToolInstructions(tools: AnthropicTool[]): string { +function buildToolInstructions(tools: AnthropicTool[], hasCommunicationTool: boolean): string { if (!tools || tools.length === 0) return ''; const toolList = tools.map((tool) => { @@ -60,6 +60,14 @@ function buildToolInstructions(tools: AnthropicTool[]): string { return `- ${tool.name}(${params})`; }).join('\n'); + const rules = hasCommunicationTool + ? `CRITICAL RULES: +1. You MUST provide at least one \`\`\`json action ... \`\`\` block in every response. +2. If you are waiting for me to give you a task, or you want to ask me a question, DO NOT run meaningless terminal commands. Instead, use the appropriate communication tool (e.g. attempt_completion, ask_followup_question).` + : `CRITICAL RULES: +1. If you need to perform an action, provide exactly one \`\`\`json action ... \`\`\` block. +2. If you have finished your task, are waiting for me to give you a task, or just want to reply/ask a question, DO NOT output any JSON action block! Just reply with normal text. DO NOT run meaningless terminal commands (like "echo ready").`; + return `Hi! I need your help. For every step we take, please provide the action I should run formatted as a simple JSON block. Please output your desired action in EXACTLY this markdown format: @@ -76,9 +84,7 @@ Please output your desired action in EXACTLY this markdown format: Here are the valid ACTION_NAMEs and parameters you can choose from: ${toolList} -CRITICAL RULES: -1. You MUST provide at least one \`\`\`json action ... \`\`\` block in every response. -2. If you are waiting for me to give you a task, or you want to ask me a question, DO NOT run meaningless terminal commands (like "echo ready"). Instead, use the appropriate communication tool (such as AskFollowupQuestion, ask_followup_question, or attempt_completion).`; +${rules}`; } // ==================== 请求转换 ==================== @@ -98,7 +104,8 @@ export function convertToCursorRequest(req: AnthropicRequest): CursorChatRequest const coreTools = filterCoreTools(req.tools!); console.log(`[Converter] 工具: ${req.tools!.length} → ${coreTools.length} (过滤到核心)`); - const toolInstructions = buildToolInstructions(coreTools); + const hasCommunicationTool = coreTools.some(t => ['attempt_completion', 'ask_followup_question', 'AskFollowupQuestion'].includes(t.name)); + const toolInstructions = buildToolInstructions(coreTools, hasCommunicationTool); // 动态选取第一个工具做 few-shot 示例 const exampleTool = coreTools[0]; @@ -140,7 +147,12 @@ export function convertToCursorRequest(req: AnthropicRequest): CursorChatRequest // 在每条用户消息末尾追加格式提醒 if (hasTools && msg.role === 'user') { - text += '\n\n[Reminder: Please output a ```json action block for the action you want to take. If you have no task or want to respond, use AskFollowupQuestion or ask_followup_question or attempt_completion, DO NOT use Bash/execute_command]'; + const hasCommunicationTool = req.tools!.some(t => ['attempt_completion', 'ask_followup_question', 'AskFollowupQuestion'].includes(t.name)); + if (hasCommunicationTool) { + text += '\n\n[Reminder: Please output a ```json action block for the action you want to take. If you have no task or want to respond, use AskFollowupQuestion or ask_followup_question or attempt_completion, DO NOT use Bash/execute_command]'; + } else { + text += '\n\n[Reminder: If you need to perform an action, output a ```json action block. If you just want to talk to me or are waiting for a task, DO NOT output any action block, just reply with plain text.]'; + } } messages.push({