From 9a636af9090b122db2e55737fca3e78550aab9df Mon Sep 17 00:00:00 2001 From: bndw Date: Sat, 28 Feb 2026 19:14:01 -0800 Subject: fix: scope artifacts to sessions --- src/main/claude/index.ts | 51 ++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'src/main/claude/index.ts') diff --git a/src/main/claude/index.ts b/src/main/claude/index.ts index b8c9c07..4dd49f2 100644 --- a/src/main/claude/index.ts +++ b/src/main/claude/index.ts @@ -1,31 +1,24 @@ import { query, type SDKMessage, type Query } from "@anthropic-ai/claude-agent-sdk"; import type { Session } from "../db/sessions"; -import { getPhaseConfig, getNextPhase, getArtifactFilename, getSessionArtifactDir } from "./phases"; +import { getPhaseConfig, getNextPhase, getArtifactFilename } from "./phases"; import type { Phase, UserPermissionMode } from "./phases"; import { getProject } from "../db/projects"; import { updateSession } from "../db/sessions"; import fs from "node:fs"; import path from "node:path"; -import os from "node:os"; // Track active queries by session ID const activeQueries = new Map(); -// Global storage in home directory -const GLOBAL_CLAUDE_FLOW_DIR = path.join(os.homedir(), ".claude-flow"); - function ensureDir(dirPath: string): void { if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }); } } -function getProjectDir(projectId: string): string { - return path.join(GLOBAL_CLAUDE_FLOW_DIR, "projects", projectId); -} - -function getSessionDir(projectId: string, sessionId: string): string { - return path.join(getProjectDir(projectId), "sessions", sessionId); +// Artifacts live inside the project directory so the SDK's Write tool can reach them +function getSessionDir(projectPath: string, sessionId: string): string { + return path.join(projectPath, ".claude-flow", "sessions", sessionId); } export interface SendMessageOptions { @@ -42,12 +35,13 @@ export async function sendMessage({ const project = getProject(session.project_id); if (!project) throw new Error("Project not found"); - // Ensure session artifact directory exists in global storage - const sessionDir = getSessionDir(session.project_id, session.id); + // Ensure session artifact directory exists inside the project + const sessionDir = getSessionDir(project.path, session.id); ensureDir(sessionDir); const phaseConfig = getPhaseConfig( session.phase as Phase, + sessionDir, session.permission_mode as UserPermissionMode ); @@ -112,22 +106,26 @@ export function advancePhase(session: Session): Phase | null { } /** - * Get the artifact path for a session and phase (in global storage) + * Get the artifact path for a session and phase (inside the project directory) */ export function getArtifactPath(session: Session): string { + const project = getProject(session.project_id); + if (!project) throw new Error("Project not found"); const filename = getArtifactFilename(session.phase as Phase); - return path.join(getSessionDir(session.project_id, session.id), filename); + return path.join(getSessionDir(project.path, session.id), filename); } /** - * Read an artifact file for a session (from global storage) + * Read an artifact file for a session */ export function readSessionArtifact( projectId: string, sessionId: string, filename: string ): string | null { - const filePath = path.join(getSessionDir(projectId, sessionId), filename); + const project = getProject(projectId); + if (!project) return null; + const filePath = path.join(getSessionDir(project.path, sessionId), filename); if (fs.existsSync(filePath)) { return fs.readFileSync(filePath, "utf-8"); } @@ -135,7 +133,7 @@ export function readSessionArtifact( } /** - * Write an artifact file for a session (to global storage) + * Write an artifact file for a session */ export function writeSessionArtifact( projectId: string, @@ -143,7 +141,9 @@ export function writeSessionArtifact( filename: string, content: string ): void { - const dir = getSessionDir(projectId, sessionId); + const project = getProject(projectId); + if (!project) throw new Error("Project not found"); + const dir = getSessionDir(project.path, sessionId); ensureDir(dir); fs.writeFileSync(path.join(dir, filename), content, "utf-8"); } @@ -168,22 +168,17 @@ export function writeClaudeMd(projectPath: string, content: string): void { } /** - * Clear session artifacts from global storage + * Clear session artifacts */ export function clearSessionArtifacts(projectId: string, sessionId: string): void { - const dir = getSessionDir(projectId, sessionId); + const project = getProject(projectId); + if (!project) return; + const dir = getSessionDir(project.path, sessionId); if (fs.existsSync(dir)) { fs.rmSync(dir, { recursive: true, force: true }); } } -/** - * Get the initial message for a phase - */ -export function getPhaseInitialMessage(phase: Phase): string { - return getPhaseConfig(phase).initialMessage; -} - // Re-export types export type { SDKMessage } from "@anthropic-ai/claude-agent-sdk"; export type { Phase, UserPermissionMode } from "./phases"; -- cgit v1.2.3