From 452f19e2a966396b7d70cb3a304214dd0959d075 Mon Sep 17 00:00:00 2001 From: kurihada Date: Tue, 16 Dec 2025 14:19:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=E7=A7=BB=E9=99=A4=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=20prompt-template=20templates=20?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/agent/index.ts | 5 - .../core/src/agent/prompt-template/index.ts | 60 +--------- .../agent/prompt-template/templates/index.ts | 9 -- .../prompt-template/templates/plan-mode.ts | 113 ------------------ .../tests/unit/agent/prompt-template.test.ts | 39 ------ 5 files changed, 1 insertion(+), 225 deletions(-) delete mode 100644 packages/core/src/agent/prompt-template/templates/index.ts delete mode 100644 packages/core/src/agent/prompt-template/templates/plan-mode.ts diff --git a/packages/core/src/agent/index.ts b/packages/core/src/agent/index.ts index 8b46302..fd2a729 100644 --- a/packages/core/src/agent/index.ts +++ b/packages/core/src/agent/index.ts @@ -69,11 +69,6 @@ export { createPlanContext, checkPlanFileExists, DEFAULT_TOOL_NAMES, - generatePlanPrompt, - resolveAgentPrompt, - PLAN_MODE_TEMPLATE, - PLAN_MODE_SUBAGENT_TEMPLATE, - PLAN_MODE_REMINDER_TEMPLATE, } from './prompt-template/index.js'; export type { diff --git a/packages/core/src/agent/prompt-template/index.ts b/packages/core/src/agent/prompt-template/index.ts index d68d9f8..c692a10 100644 --- a/packages/core/src/agent/prompt-template/index.ts +++ b/packages/core/src/agent/prompt-template/index.ts @@ -11,7 +11,7 @@ * import { renderTemplate, createPlanContext } from './prompt-template'; * * const context = createPlanContext({ workdir: '/path/to/project' }); - * const prompt = renderTemplate(PLAN_MODE_TEMPLATE.template, context); + * const prompt = renderTemplate(template, context); * ``` */ @@ -35,61 +35,3 @@ export { checkPlanFileExists, DEFAULT_TOOL_NAMES, } from './renderer.js'; - -// 模板导出 -export { - PLAN_MODE_TEMPLATE, - PLAN_MODE_SUBAGENT_TEMPLATE, - PLAN_MODE_REMINDER_TEMPLATE, -} from './templates/index.js'; - -// 便捷函数 -import { renderTemplate, createPlanContext } from './renderer.js'; -import { PLAN_MODE_TEMPLATE, PLAN_MODE_SUBAGENT_TEMPLATE } from './templates/index.js'; - -/** - * 生成 Plan 模式提示词 - * - * @param options 配置选项 - * @returns 渲染后的提示词 - */ -export function generatePlanPrompt(options: { - isSubagent?: boolean; - workdir?: string; - planFilePath?: string; -}): string { - const context = createPlanContext(options); - const template = options.isSubagent ? PLAN_MODE_SUBAGENT_TEMPLATE : PLAN_MODE_TEMPLATE; - return renderTemplate(template.template, context); -} - -/** - * 动态解析 Agent 提示词 - * 在运行时根据上下文生成最终提示词 - * - * @param basePrompt 基础提示词(可能包含模板变量) - * @param options 上下文选项 - * @returns 渲染后的提示词 - */ -export function resolveAgentPrompt( - basePrompt: string, - options: { - workdir?: string; - planFilePath?: string; - isSubagent?: boolean; - isPlanMode?: boolean; - } -): string { - const context = createPlanContext({ - workdir: options.workdir, - planFilePath: options.planFilePath, - isSubagent: options.isSubagent, - }); - - // 如果不是 Plan 模式,设置 plan.isActive = false - if (!options.isPlanMode) { - context.plan.isActive = false; - } - - return renderTemplate(basePrompt, context); -} diff --git a/packages/core/src/agent/prompt-template/templates/index.ts b/packages/core/src/agent/prompt-template/templates/index.ts deleted file mode 100644 index 7f3f812..0000000 --- a/packages/core/src/agent/prompt-template/templates/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * 提示词模板导出 - */ - -export { - PLAN_MODE_TEMPLATE, - PLAN_MODE_SUBAGENT_TEMPLATE, - PLAN_MODE_REMINDER_TEMPLATE, -} from './plan-mode.js'; diff --git a/packages/core/src/agent/prompt-template/templates/plan-mode.ts b/packages/core/src/agent/prompt-template/templates/plan-mode.ts deleted file mode 100644 index a34fbe9..0000000 --- a/packages/core/src/agent/prompt-template/templates/plan-mode.ts +++ /dev/null @@ -1,113 +0,0 @@ -/** - * Plan 模式提示词模板 - * - * 使用 ${variable} 语法支持动态变量替换 - */ - -import type { PromptTemplate } from '../types.js'; - -/** - * Plan 模式主提示词模板 - */ -export const PLAN_MODE_TEMPLATE: PromptTemplate = { - name: 'plan-mode', - description: 'Plan 模式主提示词 - 用于代码探索和实现方案设计', - requiredVariables: ['tools', 'plan'], - template: `You are a software architect and planning specialist for Claude Code. Your role is to explore the codebase and design implementation plans. - -=== CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS === -This is a READ-ONLY planning task. You are STRICTLY PROHIBITED from: -- Creating new files (no Write, touch, or file creation of any kind) -- Modifying existing files (no Edit operations) -- Deleting files (no rm or deletion) -- Moving or copying files (no mv or cp) -- Creating temporary files anywhere, including /tmp -- Using redirect operators (>, >>, |) or heredocs to write to files -- Running ANY commands that change system state - -Your role is EXCLUSIVELY to explore the codebase and design implementation plans. You do NOT have access to file editing tools - attempting to edit files will fail. - -## Plan File Info -\${plan.planExists ? "A plan file already exists at \${plan.planFilePath}. You can read it and make incremental edits using the \${tools.edit} tool." : "No plan file exists yet. You should create your plan at \${plan.planFilePath} using the \${tools.write} tool."} - -You should build your plan incrementally by writing to or editing this file. NOTE that this is the only file you are allowed to edit - other than this you are only allowed to take READ-ONLY actions. - -You will be provided with a set of requirements and optionally a perspective on how to approach the design process. - -## Your Process - -1. **Understand Requirements**: Focus on the requirements provided and apply your assigned perspective throughout the design process. - -2. **Explore Thoroughly**: - - Read any files provided to you in the initial prompt - - Find existing patterns and conventions using \${tools.glob}, \${tools.grep}, and \${tools.read} - - Understand the current architecture - - Identify similar features as reference - - Trace through relevant code paths - - Use \${tools.bash} ONLY for read-only operations (ls, git status, git log, git diff, find, cat, head, tail) - - NEVER use \${tools.bash} for: mkdir, touch, rm, cp, mv, git add, git commit, npm install, pip install, or any file creation/modification - -3. **Design Solution**: - - Create implementation approach based on your assigned perspective - - Consider trade-offs and architectural decisions - - Follow existing patterns where appropriate - -4. **Detail the Plan**: - - Provide step-by-step implementation strategy - - Identify dependencies and sequencing - - Anticipate potential challenges - -## Required Output - -End your response with: - -### Critical Files for Implementation -List 3-5 files most critical for implementing this plan: -- path/to/file1.ts - [Brief reason: e.g., "Core logic to modify"] -- path/to/file2.ts - [Brief reason: e.g., "Interfaces to implement"] -- path/to/file3.ts - [Brief reason: e.g., "Pattern to follow"] - -Answer the user's query comprehensively, using the \${tools.askUserQuestion} tool if you need to ask clarifying questions. - -REMEMBER: You can ONLY explore and plan. You CANNOT and MUST NOT write, edit, or modify any files (except the plan file at \${plan.planFilePath}).`, -}; - -/** - * Plan 模式子代理提示词模板(简化版) - * 用于作为子代理时的精简提示 - */ -export const PLAN_MODE_SUBAGENT_TEMPLATE: PromptTemplate = { - name: 'plan-mode-subagent', - description: 'Plan 模式子代理提示词(简化版)', - requiredVariables: ['tools', 'plan'], - template: `Plan mode is active. The user indicated that they do not want you to execute yet -- you MUST NOT make any edits, run any non-readonly tools (including changing configs or making commits), or otherwise make any changes to the system. This supercedes any other instructions you have received. - -## Plan File Info -\${plan.planExists ? "A plan file already exists at \${plan.planFilePath}. You can read it and make incremental edits using the \${tools.edit} tool if you need to." : "No plan file exists yet. You should create your plan at \${plan.planFilePath} using the \${tools.write} tool if you need to."} - -You should build your plan incrementally by writing to or editing this file. NOTE that this is the only file you are allowed to edit - other than this you are only allowed to take READ-ONLY actions. - -## Available Tools -- Use \${tools.glob}, \${tools.grep}, and \${tools.read} for code exploration -- Use \${tools.bash} ONLY for read-only operations -- Use \${tools.askUserQuestion} to ask clarifying questions - -Answer the user's query comprehensively, exploring the codebase as needed. - -REMEMBER: You can ONLY explore and plan. Do NOT modify any files except the plan file.`, -}; - -/** - * Plan 模式系统提醒模板(用于注入到消息中) - */ -export const PLAN_MODE_REMINDER_TEMPLATE: PromptTemplate = { - name: 'plan-mode-reminder', - description: 'Plan 模式系统提醒', - template: ` -Plan mode is active. The user indicated that they do not want you to execute yet -- you MUST NOT make any edits (with the exception of the plan file mentioned below), run any non-readonly tools (including changing configs or making commits), or otherwise make any changes to the system. This supercedes any other instructions you have received. - -## Plan File Info: -\${plan.planExists ? "A plan file already exists at \${plan.planFilePath}. You can read it and make incremental edits." : "No plan file exists yet. You should create your plan at \${plan.planFilePath}."} -You should build your plan incrementally by writing to or editing this file. NOTE that this is the only file you are allowed to edit - other than this you are only allowed to take READ-ONLY actions. -`, -}; diff --git a/packages/core/tests/unit/agent/prompt-template.test.ts b/packages/core/tests/unit/agent/prompt-template.test.ts index 33faed9..2591194 100644 --- a/packages/core/tests/unit/agent/prompt-template.test.ts +++ b/packages/core/tests/unit/agent/prompt-template.test.ts @@ -4,8 +4,6 @@ import { createDefaultContext, createPlanContext, DEFAULT_TOOL_NAMES, - generatePlanPrompt, - resolveAgentPrompt, } from '../../../src/agent/prompt-template/index.js'; import type { PromptContext } from '../../../src/agent/prompt-template/types.js'; @@ -153,43 +151,6 @@ describe('Prompt Template System', () => { }); }); - describe('generatePlanPrompt', () => { - it('should generate prompt with tool names resolved', () => { - const prompt = generatePlanPrompt({}); - expect(prompt).toContain('glob'); - expect(prompt).toContain('grep_content'); - expect(prompt).toContain('read_file'); - }); - - it('should include plan file path', () => { - const prompt = generatePlanPrompt({ planFilePath: '/test/plan.md' }); - expect(prompt).toContain('/test/plan.md'); - }); - - it('should generate shorter prompt for subagent', () => { - const mainPrompt = generatePlanPrompt({ isSubagent: false }); - const subPrompt = generatePlanPrompt({ isSubagent: true }); - expect(subPrompt.length).toBeLessThan(mainPrompt.length); - }); - }); - - describe('resolveAgentPrompt', () => { - it('should resolve template variables in prompt', () => { - const basePrompt = 'Use ${tools.glob} to search'; - const resolved = resolveAgentPrompt(basePrompt, {}); - expect(resolved).toBe('Use glob to search'); - }); - - it('should handle isPlanMode option', () => { - const basePrompt = '${plan.isActive ? "Plan mode" : "Normal mode"}'; - const planResolved = resolveAgentPrompt(basePrompt, { isPlanMode: true }); - expect(planResolved).toBe('Plan mode'); - - const normalResolved = resolveAgentPrompt(basePrompt, { isPlanMode: false }); - expect(normalResolved).toBe('Normal mode'); - }); - }); - describe('Edge Cases', () => { it('should handle empty template', () => { const result = renderTemplate('', context);