aboutsummaryrefslogtreecommitdiffstats
path: root/dist/main/claude/index.js
diff options
context:
space:
mode:
authorClawd <ai@clawd.bot>2026-02-28 15:26:02 -0800
committerClawd <ai@clawd.bot>2026-02-28 15:26:02 -0800
commit519d6b850e07a0387511a8f024dc394250b1a241 (patch)
tree5ee0a1bbbd7410f93a770e8129c88e29b5a5e5fb /dist/main/claude/index.js
parentbebbffa890c38f12f04b6fafad4c5c5e46e051a8 (diff)
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.
Diffstat (limited to 'dist/main/claude/index.js')
-rw-r--r--dist/main/claude/index.js109
1 files changed, 109 insertions, 0 deletions
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 @@
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.sendMessage = sendMessage;
7exports.interruptSession = interruptSession;
8exports.triggerReview = triggerReview;
9exports.advancePhase = advancePhase;
10exports.readArtifact = readArtifact;
11exports.writeArtifact = writeArtifact;
12exports.getPhaseInitialMessage = getPhaseInitialMessage;
13const claude_agent_sdk_1 = require("@anthropic-ai/claude-agent-sdk");
14const phases_1 = require("./phases");
15const projects_1 = require("../db/projects");
16const sessions_1 = require("../db/sessions");
17const node_fs_1 = __importDefault(require("node:fs"));
18const node_path_1 = __importDefault(require("node:path"));
19// Track active queries by session ID
20const activeQueries = new Map();
21function ensureArtifactDir(projectPath) {
22 const dir = node_path_1.default.join(projectPath, ".claude-flow");
23 if (!node_fs_1.default.existsSync(dir)) {
24 node_fs_1.default.mkdirSync(dir, { recursive: true });
25 }
26}
27async function sendMessage({ session, message, onMessage, }) {
28 const project = (0, projects_1.getProject)(session.project_id);
29 if (!project)
30 throw new Error("Project not found");
31 ensureArtifactDir(project.path);
32 const phaseConfig = (0, phases_1.getPhaseConfig)(session.phase, session.permission_mode);
33 const q = (0, claude_agent_sdk_1.query)({
34 prompt: message,
35 options: {
36 cwd: project.path,
37 resume: session.claude_session_id ?? undefined,
38 tools: phaseConfig.tools,
39 permissionMode: phaseConfig.permissionMode,
40 systemPrompt: phaseConfig.systemPrompt,
41 },
42 });
43 activeQueries.set(session.id, q);
44 try {
45 for await (const msg of q) {
46 // Capture session ID from init message
47 if (msg.type === "system" && msg.subtype === "init") {
48 if (!session.claude_session_id) {
49 (0, sessions_1.updateSession)(session.id, { claude_session_id: msg.session_id });
50 }
51 }
52 onMessage(msg);
53 }
54 }
55 finally {
56 activeQueries.delete(session.id);
57 }
58}
59function interruptSession(sessionId) {
60 const q = activeQueries.get(sessionId);
61 if (q) {
62 q.close();
63 activeQueries.delete(sessionId);
64 }
65}
66/**
67 * Trigger a review: Claude reads the document and addresses user annotations
68 */
69async function triggerReview(session, onMessage) {
70 const docName = (0, phases_1.getArtifactFilename)(session.phase);
71 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.`;
72 await sendMessage({ session, message, onMessage });
73}
74/**
75 * Advance to the next phase
76 */
77function advancePhase(session) {
78 const nextPhase = (0, phases_1.getNextPhase)(session.phase);
79 if (nextPhase) {
80 (0, sessions_1.updateSession)(session.id, { phase: nextPhase });
81 }
82 return nextPhase;
83}
84/**
85 * Read an artifact file from the project's .claude-flow directory
86 */
87function readArtifact(projectPath, filename) {
88 const filePath = node_path_1.default.join(projectPath, ".claude-flow", filename);
89 if (node_fs_1.default.existsSync(filePath)) {
90 return node_fs_1.default.readFileSync(filePath, "utf-8");
91 }
92 return null;
93}
94/**
95 * Write an artifact file to the project's .claude-flow directory
96 */
97function writeArtifact(projectPath, filename, content) {
98 const dir = node_path_1.default.join(projectPath, ".claude-flow");
99 if (!node_fs_1.default.existsSync(dir)) {
100 node_fs_1.default.mkdirSync(dir, { recursive: true });
101 }
102 node_fs_1.default.writeFileSync(node_path_1.default.join(dir, filename), content, "utf-8");
103}
104/**
105 * Get the initial message for a phase
106 */
107function getPhaseInitialMessage(phase) {
108 return (0, phases_1.getPhaseConfig)(phase).initialMessage;
109}