From 332e5cec2992fefb302251962a3ceca38437a110 Mon Sep 17 00:00:00 2001 From: Clawd Date: Sat, 28 Feb 2026 07:26:43 -0800 Subject: Phase 2: Claude integration layer - Add @anthropic-ai/claude-agent-sdk dependency - Implement src/main/claude/phases.ts with phase configs (research/plan/implement) - Implement src/main/claude/index.ts with SDK wrapper - query() integration with session management - Session resume support - Artifact read/write utilities - Phase advancement logic --- src/main/db/sessions.ts | 106 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/main/db/sessions.ts (limited to 'src/main/db/sessions.ts') diff --git a/src/main/db/sessions.ts b/src/main/db/sessions.ts new file mode 100644 index 0000000..684bb9e --- /dev/null +++ b/src/main/db/sessions.ts @@ -0,0 +1,106 @@ +import { getDb } from "./index"; +import { v4 as uuid } from "uuid"; + +export type Phase = "research" | "plan" | "implement"; +export type PermissionMode = "acceptEdits" | "bypassPermissions"; + +export interface Session { + id: string; + project_id: string; + name: string; + phase: Phase; + claude_session_id: string | null; + permission_mode: PermissionMode; + created_at: number; + updated_at: number; +} + +export interface Message { + id: string; + session_id: string; + role: "user" | "assistant"; + content: string; + created_at: number; +} + +export function listSessions(projectId: string): Session[] { + return getDb() + .prepare("SELECT * FROM sessions WHERE project_id = ? ORDER BY updated_at DESC") + .all(projectId) as Session[]; +} + +export function getSession(id: string): Session | undefined { + return getDb() + .prepare("SELECT * FROM sessions WHERE id = ?") + .get(id) as Session | undefined; +} + +export function createSession(projectId: string, name: string): Session { + const db = getDb(); + const id = uuid(); + const now = Math.floor(Date.now() / 1000); + + db.prepare( + `INSERT INTO sessions (id, project_id, name, phase, permission_mode, created_at, updated_at) + VALUES (?, ?, ?, 'research', 'acceptEdits', ?, ?)` + ).run(id, projectId, name, now, now); + + return { + id, + project_id: projectId, + name, + phase: "research", + claude_session_id: null, + permission_mode: "acceptEdits", + created_at: now, + updated_at: now, + }; +} + +export function updateSession( + id: string, + updates: Partial> +): void { + const db = getDb(); + const sets: string[] = []; + const values: any[] = []; + + for (const [key, value] of Object.entries(updates)) { + if (value !== undefined) { + sets.push(`${key} = ?`); + values.push(value); + } + } + + if (sets.length > 0) { + sets.push("updated_at = ?"); + values.push(Math.floor(Date.now() / 1000)); + values.push(id); + db.prepare(`UPDATE sessions SET ${sets.join(", ")} WHERE id = ?`).run(...values); + } +} + +export function deleteSession(id: string): void { + getDb().prepare("DELETE FROM sessions WHERE id = ?").run(id); +} + +// Messages +export function listMessages(sessionId: string): Message[] { + return getDb() + .prepare("SELECT * FROM messages WHERE session_id = ? ORDER BY created_at ASC") + .all(sessionId) as Message[]; +} + +export function addMessage(sessionId: string, role: Message["role"], content: string): Message { + const db = getDb(); + const id = uuid(); + const now = Math.floor(Date.now() / 1000); + + db.prepare( + "INSERT INTO messages (id, session_id, role, content, created_at) VALUES (?, ?, ?, ?, ?)" + ).run(id, sessionId, role, content, now); + + db.prepare("UPDATE sessions SET updated_at = ? WHERE id = ?").run(now, sessionId); + + return { id, session_id: sessionId, role, content, created_at: now }; +} -- cgit v1.2.3