aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/claude/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/claude/index.ts')
-rw-r--r--src/main/claude/index.ts91
1 files changed, 78 insertions, 13 deletions
diff --git a/src/main/claude/index.ts b/src/main/claude/index.ts
index 8bdcccd..4d8909b 100644
--- a/src/main/claude/index.ts
+++ b/src/main/claude/index.ts
@@ -1,6 +1,6 @@
1import { query, type SDKMessage, type Query } from "@anthropic-ai/claude-agent-sdk"; 1import { query, type SDKMessage, type Query } from "@anthropic-ai/claude-agent-sdk";
2import type { Session } from "../db/sessions"; 2import type { Session } from "../db/sessions";
3import { getPhaseConfig, getNextPhase, getArtifactFilename } from "./phases"; 3import { getPhaseConfig, getNextPhase, getArtifactFilename, getSessionArtifactDir } from "./phases";
4import type { Phase, UserPermissionMode } from "./phases"; 4import type { Phase, UserPermissionMode } from "./phases";
5import { getProject } from "../db/projects"; 5import { getProject } from "../db/projects";
6import { updateSession } from "../db/sessions"; 6import { updateSession } from "../db/sessions";
@@ -10,10 +10,9 @@ import path from "node:path";
10// Track active queries by session ID 10// Track active queries by session ID
11const activeQueries = new Map<string, Query>(); 11const activeQueries = new Map<string, Query>();
12 12
13function ensureArtifactDir(projectPath: string): void { 13function ensureDir(dirPath: string): void {
14 const dir = path.join(projectPath, ".claude-flow"); 14 if (!fs.existsSync(dirPath)) {
15 if (!fs.existsSync(dir)) { 15 fs.mkdirSync(dirPath, { recursive: true });
16 fs.mkdirSync(dir, { recursive: true });
17 } 16 }
18} 17}
19 18
@@ -31,7 +30,9 @@ export async function sendMessage({
31 const project = getProject(session.project_id); 30 const project = getProject(session.project_id);
32 if (!project) throw new Error("Project not found"); 31 if (!project) throw new Error("Project not found");
33 32
34 ensureArtifactDir(project.path); 33 // Ensure session artifact directory exists
34 const sessionDir = path.join(project.path, getSessionArtifactDir(session.id));
35 ensureDir(sessionDir);
35 36
36 const phaseConfig = getPhaseConfig( 37 const phaseConfig = getPhaseConfig(
37 session.phase as Phase, 38 session.phase as Phase,
@@ -81,8 +82,8 @@ export async function triggerReview(
81 session: Session, 82 session: Session,
82 onMessage: (msg: SDKMessage) => void 83 onMessage: (msg: SDKMessage) => void
83): Promise<void> { 84): Promise<void> {
84 const docName = getArtifactFilename(session.phase as Phase); 85 const artifactPath = getArtifactPath(session);
85 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.`; 86 const message = `I've updated ${artifactPath} 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.`;
86 87
87 await sendMessage({ session, message, onMessage }); 88 await sendMessage({ session, message, onMessage });
88} 89}
@@ -99,7 +100,63 @@ export function advancePhase(session: Session): Phase | null {
99} 100}
100 101
101/** 102/**
102 * Read an artifact file from the project's .claude-flow directory 103 * Get the artifact path for a session and phase
104 */
105export function getArtifactPath(session: Session): string {
106 const filename = getArtifactFilename(session.phase as Phase);
107 return path.join(getSessionArtifactDir(session.id), filename);
108}
109
110/**
111 * Read an artifact file for a session
112 */
113export function readSessionArtifact(
114 projectPath: string,
115 sessionId: string,
116 filename: string
117): string | null {
118 const filePath = path.join(projectPath, getSessionArtifactDir(sessionId), filename);
119 if (fs.existsSync(filePath)) {
120 return fs.readFileSync(filePath, "utf-8");
121 }
122 return null;
123}
124
125/**
126 * Write an artifact file for a session
127 */
128export function writeSessionArtifact(
129 projectPath: string,
130 sessionId: string,
131 filename: string,
132 content: string
133): void {
134 const dir = path.join(projectPath, getSessionArtifactDir(sessionId));
135 ensureDir(dir);
136 fs.writeFileSync(path.join(dir, filename), content, "utf-8");
137}
138
139/**
140 * Read CLAUDE.md from project root
141 */
142export function readClaudeMd(projectPath: string): string | null {
143 const filePath = path.join(projectPath, "CLAUDE.md");
144 if (fs.existsSync(filePath)) {
145 return fs.readFileSync(filePath, "utf-8");
146 }
147 return null;
148}
149
150/**
151 * Write CLAUDE.md to project root
152 */
153export function writeClaudeMd(projectPath: string, content: string): void {
154 const filePath = path.join(projectPath, "CLAUDE.md");
155 fs.writeFileSync(filePath, content, "utf-8");
156}
157
158/**
159 * Read an artifact file from the project's .claude-flow directory (legacy path)
103 */ 160 */
104export function readArtifact( 161export function readArtifact(
105 projectPath: string, 162 projectPath: string,
@@ -113,7 +170,7 @@ export function readArtifact(
113} 170}
114 171
115/** 172/**
116 * Write an artifact file to the project's .claude-flow directory 173 * Write an artifact file to the project's .claude-flow directory (legacy path)
117 */ 174 */
118export function writeArtifact( 175export function writeArtifact(
119 projectPath: string, 176 projectPath: string,
@@ -121,13 +178,21 @@ export function writeArtifact(
121 content: string 178 content: string
122): void { 179): void {
123 const dir = path.join(projectPath, ".claude-flow"); 180 const dir = path.join(projectPath, ".claude-flow");
124 if (!fs.existsSync(dir)) { 181 ensureDir(dir);
125 fs.mkdirSync(dir, { recursive: true });
126 }
127 fs.writeFileSync(path.join(dir, filename), content, "utf-8"); 182 fs.writeFileSync(path.join(dir, filename), content, "utf-8");
128} 183}
129 184
130/** 185/**
186 * Clear session artifacts
187 */
188export function clearSessionArtifacts(projectPath: string, sessionId: string): void {
189 const dir = path.join(projectPath, getSessionArtifactDir(sessionId));
190 if (fs.existsSync(dir)) {
191 fs.rmSync(dir, { recursive: true, force: true });
192 }
193}
194
195/**
131 * Get the initial message for a phase 196 * Get the initial message for a phase
132 */ 197 */
133export function getPhaseInitialMessage(phase: Phase): string { 198export function getPhaseInitialMessage(phase: Phase): string {