diff options
Diffstat (limited to 'src/main/claude/phases.ts')
| -rw-r--r-- | src/main/claude/phases.ts | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/src/main/claude/phases.ts b/src/main/claude/phases.ts index f1df719..89e7c22 100644 --- a/src/main/claude/phases.ts +++ b/src/main/claude/phases.ts | |||
| @@ -3,6 +3,7 @@ import type { PermissionMode } from "@anthropic-ai/claude-agent-sdk"; | |||
| 3 | export type Phase = "research" | "plan" | "implement"; | 3 | export type Phase = "research" | "plan" | "implement"; |
| 4 | export type UserPermissionMode = "acceptEdits" | "bypassPermissions"; | 4 | export type UserPermissionMode = "acceptEdits" | "bypassPermissions"; |
| 5 | 5 | ||
| 6 | // External interface — consumers see a resolved string | ||
| 6 | export interface PhaseConfig { | 7 | export interface PhaseConfig { |
| 7 | systemPrompt: string; | 8 | systemPrompt: string; |
| 8 | tools: string[]; | 9 | tools: string[]; |
| @@ -10,27 +11,24 @@ export interface PhaseConfig { | |||
| 10 | initialMessage: string; | 11 | initialMessage: string; |
| 11 | } | 12 | } |
| 12 | 13 | ||
| 13 | // Get session-specific artifact path (relative to ~/.claude-flow/) | 14 | // Internal template — systemPrompt is a function that receives the artifact dir |
| 14 | export function getSessionArtifactDir(sessionId: string): string { | 15 | interface PhaseConfigTemplate { |
| 15 | return `sessions/${sessionId}`; | 16 | systemPrompt: (artifactDir: string) => string; |
| 16 | } | 17 | tools: string[]; |
| 17 | 18 | permissionMode: PermissionMode; | |
| 18 | export function getArtifactPath(phase: Phase, sessionId: string): string { | 19 | initialMessage: string; |
| 19 | const dir = getSessionArtifactDir(sessionId); | ||
| 20 | const filename = phase === "research" ? "research.md" : "plan.md"; | ||
| 21 | return `${dir}/${filename}`; | ||
| 22 | } | 20 | } |
| 23 | 21 | ||
| 24 | export const phaseConfigs: Record<Phase, PhaseConfig> = { | 22 | const phaseConfigTemplates: Record<Phase, PhaseConfigTemplate> = { |
| 25 | research: { | 23 | research: { |
| 26 | permissionMode: "acceptEdits", | 24 | permissionMode: "acceptEdits", |
| 27 | tools: ["Read", "Glob", "Grep", "Bash", "Write"], | 25 | tools: ["Read", "Glob", "Grep", "Bash", "Write"], |
| 28 | initialMessage: | 26 | initialMessage: |
| 29 | "What areas of the codebase should I research? What are you trying to build?", | 27 | "What areas of the codebase should I research? What are you trying to build?", |
| 30 | systemPrompt: `You are in RESEARCH mode. Your ONLY job is to understand the codebase. | 28 | systemPrompt: (artifactDir) => `You are in RESEARCH mode. Your ONLY job is to understand the codebase. |
| 31 | 29 | ||
| 32 | CRITICAL RULES: | 30 | CRITICAL RULES: |
| 33 | 1. You MUST write ALL findings to the session research.md — this is your PRIMARY output | 31 | 1. You MUST write ALL findings to ${artifactDir}/research.md — this is your PRIMARY output |
| 34 | 2. DO NOT just respond in chat. The document viewer shows research.md, so write there. | 32 | 2. DO NOT just respond in chat. The document viewer shows research.md, so write there. |
| 35 | 3. DO NOT suggest moving to planning or implementation | 33 | 3. DO NOT suggest moving to planning or implementation |
| 36 | 4. DO NOT ask "are you ready to implement?" or similar | 34 | 4. DO NOT ask "are you ready to implement?" or similar |
| @@ -46,7 +44,7 @@ WORKFLOW: | |||
| 46 | 1. Read CLAUDE.md (create at project root if missing) | 44 | 1. Read CLAUDE.md (create at project root if missing) |
| 47 | 2. Ask what to research (if unclear) | 45 | 2. Ask what to research (if unclear) |
| 48 | 3. Read files thoroughly using Read, Glob, Grep | 46 | 3. Read files thoroughly using Read, Glob, Grep |
| 49 | 4. Write findings to session research.md | 47 | 4. Write findings to ${artifactDir}/research.md |
| 50 | 5. Update CLAUDE.md with any new general insights worth sharing | 48 | 5. Update CLAUDE.md with any new general insights worth sharing |
| 51 | 49 | ||
| 52 | FORMAT for research.md: | 50 | FORMAT for research.md: |
| @@ -69,7 +67,7 @@ FORMAT for research.md: | |||
| 69 | [Things that need clarification] | 67 | [Things that need clarification] |
| 70 | \`\`\` | 68 | \`\`\` |
| 71 | 69 | ||
| 72 | Remember: Your output goes in research.md, not chat. Chat is for clarifying questions only.`, | 70 | Remember: Your output goes in ${artifactDir}/research.md, not chat. Chat is for clarifying questions only.`, |
| 73 | }, | 71 | }, |
| 74 | 72 | ||
| 75 | plan: { | 73 | plan: { |
| @@ -77,10 +75,10 @@ Remember: Your output goes in research.md, not chat. Chat is for clarifying ques | |||
| 77 | tools: ["Read", "Glob", "Grep", "Write"], | 75 | tools: ["Read", "Glob", "Grep", "Write"], |
| 78 | initialMessage: | 76 | initialMessage: |
| 79 | "I'll create a detailed implementation plan based on my research. Writing to plan.md...", | 77 | "I'll create a detailed implementation plan based on my research. Writing to plan.md...", |
| 80 | systemPrompt: `You are in PLANNING mode. Your ONLY job is to create an implementation plan. | 78 | systemPrompt: (artifactDir) => `You are in PLANNING mode. Your ONLY job is to create an implementation plan. |
| 81 | 79 | ||
| 82 | CRITICAL RULES: | 80 | CRITICAL RULES: |
| 83 | 1. You MUST write the plan to session plan.md — this is your PRIMARY output | 81 | 1. You MUST write the plan to ${artifactDir}/plan.md — this is your PRIMARY output |
| 84 | 2. DO NOT just respond in chat. The document viewer shows plan.md, so write there. | 82 | 2. DO NOT just respond in chat. The document viewer shows plan.md, so write there. |
| 85 | 3. DO NOT implement anything — no code changes to source files | 83 | 3. DO NOT implement anything — no code changes to source files |
| 86 | 4. DO NOT ask "should I start implementing?" or similar | 84 | 4. DO NOT ask "should I start implementing?" or similar |
| @@ -89,12 +87,12 @@ CRITICAL RULES: | |||
| 89 | 87 | ||
| 90 | CONTEXT: | 88 | CONTEXT: |
| 91 | - Read CLAUDE.md at project root for codebase overview | 89 | - Read CLAUDE.md at project root for codebase overview |
| 92 | - Read the session research.md to understand the specific task | 90 | - Read ${artifactDir}/research.md to understand the specific task |
| 93 | 91 | ||
| 94 | WORKFLOW: | 92 | WORKFLOW: |
| 95 | 1. Read CLAUDE.md for codebase overview | 93 | 1. Read CLAUDE.md for codebase overview |
| 96 | 2. Read the session research.md to understand the specific task | 94 | 2. Read ${artifactDir}/research.md to understand the specific task |
| 97 | 3. Write a detailed plan to session plan.md | 95 | 3. Write a detailed plan to ${artifactDir}/plan.md |
| 98 | 4. Include specific code snippets showing proposed changes | 96 | 4. Include specific code snippets showing proposed changes |
| 99 | 5. Make the plan detailed enough that implementation is mechanical | 97 | 5. Make the plan detailed enough that implementation is mechanical |
| 100 | 98 | ||
| @@ -132,7 +130,7 @@ FORMAT for plan.md: | |||
| 132 | 130 | ||
| 133 | When the user adds annotations to plan.md and clicks Review, address each annotation and update the document. | 131 | When the user adds annotations to plan.md and clicks Review, address each annotation and update the document. |
| 134 | 132 | ||
| 135 | Remember: Your output goes in plan.md, not chat. Chat is for clarifying questions only.`, | 133 | Remember: Your output goes in ${artifactDir}/plan.md, not chat. Chat is for clarifying questions only.`, |
| 136 | }, | 134 | }, |
| 137 | 135 | ||
| 138 | implement: { | 136 | implement: { |
| @@ -140,19 +138,19 @@ Remember: Your output goes in plan.md, not chat. Chat is for clarifying question | |||
| 140 | tools: ["Read", "Write", "Edit", "Bash", "Glob", "Grep"], | 138 | tools: ["Read", "Write", "Edit", "Bash", "Glob", "Grep"], |
| 141 | initialMessage: | 139 | initialMessage: |
| 142 | "Starting implementation. I'll follow the plan exactly and mark tasks complete as I go.", | 140 | "Starting implementation. I'll follow the plan exactly and mark tasks complete as I go.", |
| 143 | systemPrompt: `You are in IMPLEMENTATION mode. Execute the approved plan. | 141 | systemPrompt: (artifactDir) => `You are in IMPLEMENTATION mode. Execute the approved plan. |
| 144 | 142 | ||
| 145 | CRITICAL RULES: | 143 | CRITICAL RULES: |
| 146 | 1. Read session plan.md and follow it exactly | 144 | 1. Read ${artifactDir}/plan.md and follow it exactly |
| 147 | 2. Mark tasks complete in plan.md as you finish them: - [ ] → - [x] | 145 | 2. Mark tasks complete in ${artifactDir}/plan.md as you finish them: - [ ] → - [x] |
| 148 | 3. DO NOT deviate from the plan without asking | 146 | 3. DO NOT deviate from the plan without asking |
| 149 | 4. Run tests/typecheck if available | 147 | 4. Run tests/typecheck if available |
| 150 | 5. Stop and ask if you encounter issues not covered by the plan | 148 | 5. Stop and ask if you encounter issues not covered by the plan |
| 151 | 149 | ||
| 152 | WORKFLOW: | 150 | WORKFLOW: |
| 153 | 1. Read session plan.md | 151 | 1. Read ${artifactDir}/plan.md |
| 154 | 2. Execute each task in order | 152 | 2. Execute each task in order |
| 155 | 3. Update plan.md to mark tasks complete | 153 | 3. Update ${artifactDir}/plan.md to mark tasks complete |
| 156 | 4. Continue until all tasks are done | 154 | 4. Continue until all tasks are done |
| 157 | 155 | ||
| 158 | When complete, summarize what was done and any follow-up tasks.`, | 156 | When complete, summarize what was done and any follow-up tasks.`, |
| @@ -161,15 +159,26 @@ When complete, summarize what was done and any follow-up tasks.`, | |||
| 161 | 159 | ||
| 162 | export function getPhaseConfig( | 160 | export function getPhaseConfig( |
| 163 | phase: Phase, | 161 | phase: Phase, |
| 162 | artifactDir: string, | ||
| 164 | userPermissionMode?: UserPermissionMode | 163 | userPermissionMode?: UserPermissionMode |
| 165 | ): PhaseConfig { | 164 | ): PhaseConfig { |
| 166 | const config = { ...phaseConfigs[phase] }; | 165 | const template = phaseConfigTemplates[phase]; |
| 166 | const config: PhaseConfig = { | ||
| 167 | systemPrompt: template.systemPrompt(artifactDir), | ||
| 168 | tools: template.tools, | ||
| 169 | permissionMode: template.permissionMode, | ||
| 170 | initialMessage: template.initialMessage, | ||
| 171 | }; | ||
| 167 | if (phase === "implement" && userPermissionMode) { | 172 | if (phase === "implement" && userPermissionMode) { |
| 168 | config.permissionMode = userPermissionMode; | 173 | config.permissionMode = userPermissionMode; |
| 169 | } | 174 | } |
| 170 | return config; | 175 | return config; |
| 171 | } | 176 | } |
| 172 | 177 | ||
| 178 | export function getPhaseInitialMessage(phase: Phase): string { | ||
| 179 | return phaseConfigTemplates[phase].initialMessage; | ||
| 180 | } | ||
| 181 | |||
| 173 | export function getNextPhase(phase: Phase): Phase | null { | 182 | export function getNextPhase(phase: Phase): Phase | null { |
| 174 | const transitions: Record<Phase, Phase | null> = { | 183 | const transitions: Record<Phase, Phase | null> = { |
| 175 | research: "plan", | 184 | research: "plan", |
