From 519d6b850e07a0387511a8f024dc394250b1a241 Mon Sep 17 00:00:00 2001 From: Clawd Date: Sat, 28 Feb 2026 15:26:02 -0800 Subject: Clear artifacts when creating new session Each new session now starts with empty research.md and plan.md files, preventing stale content from previous sessions appearing. --- dist/main/claude/index.js | 109 +++++++++++++++++++++++++++++++++ dist/main/claude/phases.js | 147 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 256 insertions(+) create mode 100644 dist/main/claude/index.js create mode 100644 dist/main/claude/phases.js (limited to 'dist/main/claude') diff --git a/dist/main/claude/index.js b/dist/main/claude/index.js new file mode 100644 index 0000000..42a9652 --- /dev/null +++ b/dist/main/claude/index.js @@ -0,0 +1,109 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.sendMessage = sendMessage; +exports.interruptSession = interruptSession; +exports.triggerReview = triggerReview; +exports.advancePhase = advancePhase; +exports.readArtifact = readArtifact; +exports.writeArtifact = writeArtifact; +exports.getPhaseInitialMessage = getPhaseInitialMessage; +const claude_agent_sdk_1 = require("@anthropic-ai/claude-agent-sdk"); +const phases_1 = require("./phases"); +const projects_1 = require("../db/projects"); +const sessions_1 = require("../db/sessions"); +const node_fs_1 = __importDefault(require("node:fs")); +const node_path_1 = __importDefault(require("node:path")); +// Track active queries by session ID +const activeQueries = new Map(); +function ensureArtifactDir(projectPath) { + const dir = node_path_1.default.join(projectPath, ".claude-flow"); + if (!node_fs_1.default.existsSync(dir)) { + node_fs_1.default.mkdirSync(dir, { recursive: true }); + } +} +async function sendMessage({ session, message, onMessage, }) { + const project = (0, projects_1.getProject)(session.project_id); + if (!project) + throw new Error("Project not found"); + ensureArtifactDir(project.path); + const phaseConfig = (0, phases_1.getPhaseConfig)(session.phase, session.permission_mode); + const q = (0, claude_agent_sdk_1.query)({ + prompt: message, + options: { + cwd: project.path, + resume: session.claude_session_id ?? undefined, + tools: phaseConfig.tools, + permissionMode: phaseConfig.permissionMode, + systemPrompt: phaseConfig.systemPrompt, + }, + }); + activeQueries.set(session.id, q); + try { + for await (const msg of q) { + // Capture session ID from init message + if (msg.type === "system" && msg.subtype === "init") { + if (!session.claude_session_id) { + (0, sessions_1.updateSession)(session.id, { claude_session_id: msg.session_id }); + } + } + onMessage(msg); + } + } + finally { + activeQueries.delete(session.id); + } +} +function interruptSession(sessionId) { + const q = activeQueries.get(sessionId); + if (q) { + q.close(); + activeQueries.delete(sessionId); + } +} +/** + * Trigger a review: Claude reads the document and addresses user annotations + */ +async function triggerReview(session, onMessage) { + const docName = (0, phases_1.getArtifactFilename)(session.phase); + const message = `I've updated .claude-flow/${docName} with annotations. Read the file, find all my inline notes (marked with // REVIEW:, // NOTE:, TODO:, or similar), address each one, and update the document accordingly. Do not implement anything yet.`; + await sendMessage({ session, message, onMessage }); +} +/** + * Advance to the next phase + */ +function advancePhase(session) { + const nextPhase = (0, phases_1.getNextPhase)(session.phase); + if (nextPhase) { + (0, sessions_1.updateSession)(session.id, { phase: nextPhase }); + } + return nextPhase; +} +/** + * Read an artifact file from the project's .claude-flow directory + */ +function readArtifact(projectPath, filename) { + const filePath = node_path_1.default.join(projectPath, ".claude-flow", filename); + if (node_fs_1.default.existsSync(filePath)) { + return node_fs_1.default.readFileSync(filePath, "utf-8"); + } + return null; +} +/** + * Write an artifact file to the project's .claude-flow directory + */ +function writeArtifact(projectPath, filename, content) { + const dir = node_path_1.default.join(projectPath, ".claude-flow"); + if (!node_fs_1.default.existsSync(dir)) { + node_fs_1.default.mkdirSync(dir, { recursive: true }); + } + node_fs_1.default.writeFileSync(node_path_1.default.join(dir, filename), content, "utf-8"); +} +/** + * Get the initial message for a phase + */ +function getPhaseInitialMessage(phase) { + return (0, phases_1.getPhaseConfig)(phase).initialMessage; +} diff --git a/dist/main/claude/phases.js b/dist/main/claude/phases.js new file mode 100644 index 0000000..65ea46d --- /dev/null +++ b/dist/main/claude/phases.js @@ -0,0 +1,147 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.phaseConfigs = void 0; +exports.getPhaseConfig = getPhaseConfig; +exports.getNextPhase = getNextPhase; +exports.getArtifactFilename = getArtifactFilename; +exports.phaseConfigs = { + research: { + permissionMode: "acceptEdits", + tools: ["Read", "Glob", "Grep", "Bash", "Write"], + initialMessage: "What areas of the codebase should I research? What are you trying to build?", + systemPrompt: `You are in RESEARCH mode. Your ONLY job is to understand the codebase. + +CRITICAL RULES: +1. You MUST write ALL findings to .claude-flow/research.md — this is your PRIMARY output +2. DO NOT just respond in chat. The document viewer shows research.md, so write there. +3. DO NOT suggest moving to planning or implementation +4. DO NOT ask "are you ready to implement?" or similar +5. DO NOT modify any source code files +6. The user controls phase transitions via UI buttons — never prompt them about it + +WORKFLOW: +1. Ask what to research (if unclear) +2. Read files thoroughly using Read, Glob, Grep +3. Write structured findings to .claude-flow/research.md +4. Keep updating research.md as you learn more + +FORMAT for research.md: +\`\`\`markdown +# Research Findings + +## Overview +[High-level summary of what you found] + +## Architecture +[Key components, patterns, structure] + +## Relevant Files +[List of important files with descriptions] + +## Key Insights +[Important discoveries, patterns, potential issues] + +## Questions/Unknowns +[Things that need clarification] +\`\`\` + +When the user adds annotations (// REVIEW:, // NOTE:, TODO:) to research.md and clicks Review, address each annotation and update the document. + +Remember: Your output goes in research.md, not chat. Chat is for clarifying questions only.`, + }, + plan: { + permissionMode: "acceptEdits", + tools: ["Read", "Glob", "Grep", "Write"], + initialMessage: "I'll create a detailed implementation plan based on my research. Writing to plan.md...", + systemPrompt: `You are in PLANNING mode. Your ONLY job is to create an implementation plan. + +CRITICAL RULES: +1. You MUST write the plan to .claude-flow/plan.md — this is your PRIMARY output +2. DO NOT just respond in chat. The document viewer shows plan.md, so write there. +3. DO NOT implement anything — no code changes to source files +4. DO NOT ask "should I start implementing?" or similar +5. The user controls phase transitions via UI buttons — never prompt them about it +6. Base your plan on .claude-flow/research.md + +WORKFLOW: +1. Read .claude-flow/research.md to understand the codebase +2. Write a detailed plan to .claude-flow/plan.md +3. Include specific code snippets showing proposed changes +4. Make the plan detailed enough that implementation is mechanical + +FORMAT for plan.md: +\`\`\`markdown +# Implementation Plan + +## Goal +[What we're building/changing] + +## Approach +[High-level strategy] + +## Changes + +### 1. [First change] +**File:** path/to/file.ts +**What:** [Description] +**Code:** +\\\`\\\`\\\`typescript +// Proposed code +\\\`\\\`\\\` + +### 2. [Second change] +... + +## TODO +- [ ] Task 1 +- [ ] Task 2 +- [ ] Task 3 + +## Risks/Considerations +[Potential issues, edge cases] +\`\`\` + +When the user adds annotations to plan.md and clicks Review, address each annotation and update the document. + +Remember: Your output goes in plan.md, not chat. Chat is for clarifying questions only.`, + }, + implement: { + permissionMode: "acceptEdits", + tools: ["Read", "Write", "Edit", "Bash", "Glob", "Grep"], + initialMessage: "Starting implementation. I'll follow the plan exactly and mark tasks complete as I go.", + systemPrompt: `You are in IMPLEMENTATION mode. Execute the approved plan. + +CRITICAL RULES: +1. Read .claude-flow/plan.md and follow it exactly +2. Mark tasks complete in plan.md as you finish them: - [ ] → - [x] +3. DO NOT deviate from the plan without asking +4. Run tests/typecheck if available +5. Stop and ask if you encounter issues not covered by the plan + +WORKFLOW: +1. Read .claude-flow/plan.md +2. Execute each task in order +3. Update plan.md to mark tasks complete +4. Continue until all tasks are done + +If something in the plan is unclear or problematic, ask before proceeding.`, + }, +}; +function getPhaseConfig(phase, userPermissionMode) { + const config = { ...exports.phaseConfigs[phase] }; + if (phase === "implement" && userPermissionMode) { + config.permissionMode = userPermissionMode; + } + return config; +} +function getNextPhase(phase) { + const transitions = { + research: "plan", + plan: "implement", + implement: null, + }; + return transitions[phase]; +} +function getArtifactFilename(phase) { + return phase === "research" ? "research.md" : "plan.md"; +} -- cgit v1.2.3