diff options
Diffstat (limited to 'src/main/db/sessions.ts')
| -rw-r--r-- | src/main/db/sessions.ts | 106 |
1 files changed, 106 insertions, 0 deletions
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 @@ | |||
| 1 | import { getDb } from "./index"; | ||
| 2 | import { v4 as uuid } from "uuid"; | ||
| 3 | |||
| 4 | export type Phase = "research" | "plan" | "implement"; | ||
| 5 | export type PermissionMode = "acceptEdits" | "bypassPermissions"; | ||
| 6 | |||
| 7 | export interface Session { | ||
| 8 | id: string; | ||
| 9 | project_id: string; | ||
| 10 | name: string; | ||
| 11 | phase: Phase; | ||
| 12 | claude_session_id: string | null; | ||
| 13 | permission_mode: PermissionMode; | ||
| 14 | created_at: number; | ||
| 15 | updated_at: number; | ||
| 16 | } | ||
| 17 | |||
| 18 | export interface Message { | ||
| 19 | id: string; | ||
| 20 | session_id: string; | ||
| 21 | role: "user" | "assistant"; | ||
| 22 | content: string; | ||
| 23 | created_at: number; | ||
| 24 | } | ||
| 25 | |||
| 26 | export function listSessions(projectId: string): Session[] { | ||
| 27 | return getDb() | ||
| 28 | .prepare("SELECT * FROM sessions WHERE project_id = ? ORDER BY updated_at DESC") | ||
| 29 | .all(projectId) as Session[]; | ||
| 30 | } | ||
| 31 | |||
| 32 | export function getSession(id: string): Session | undefined { | ||
| 33 | return getDb() | ||
| 34 | .prepare("SELECT * FROM sessions WHERE id = ?") | ||
| 35 | .get(id) as Session | undefined; | ||
| 36 | } | ||
| 37 | |||
| 38 | export function createSession(projectId: string, name: string): Session { | ||
| 39 | const db = getDb(); | ||
| 40 | const id = uuid(); | ||
| 41 | const now = Math.floor(Date.now() / 1000); | ||
| 42 | |||
| 43 | db.prepare( | ||
| 44 | `INSERT INTO sessions (id, project_id, name, phase, permission_mode, created_at, updated_at) | ||
| 45 | VALUES (?, ?, ?, 'research', 'acceptEdits', ?, ?)` | ||
| 46 | ).run(id, projectId, name, now, now); | ||
| 47 | |||
| 48 | return { | ||
| 49 | id, | ||
| 50 | project_id: projectId, | ||
| 51 | name, | ||
| 52 | phase: "research", | ||
| 53 | claude_session_id: null, | ||
| 54 | permission_mode: "acceptEdits", | ||
| 55 | created_at: now, | ||
| 56 | updated_at: now, | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | |||
| 60 | export function updateSession( | ||
| 61 | id: string, | ||
| 62 | updates: Partial<Pick<Session, "name" | "phase" | "claude_session_id" | "permission_mode">> | ||
| 63 | ): void { | ||
| 64 | const db = getDb(); | ||
| 65 | const sets: string[] = []; | ||
| 66 | const values: any[] = []; | ||
| 67 | |||
| 68 | for (const [key, value] of Object.entries(updates)) { | ||
| 69 | if (value !== undefined) { | ||
| 70 | sets.push(`${key} = ?`); | ||
| 71 | values.push(value); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | if (sets.length > 0) { | ||
| 76 | sets.push("updated_at = ?"); | ||
| 77 | values.push(Math.floor(Date.now() / 1000)); | ||
| 78 | values.push(id); | ||
| 79 | db.prepare(`UPDATE sessions SET ${sets.join(", ")} WHERE id = ?`).run(...values); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | export function deleteSession(id: string): void { | ||
| 84 | getDb().prepare("DELETE FROM sessions WHERE id = ?").run(id); | ||
| 85 | } | ||
| 86 | |||
| 87 | // Messages | ||
| 88 | export function listMessages(sessionId: string): Message[] { | ||
| 89 | return getDb() | ||
| 90 | .prepare("SELECT * FROM messages WHERE session_id = ? ORDER BY created_at ASC") | ||
| 91 | .all(sessionId) as Message[]; | ||
| 92 | } | ||
| 93 | |||
| 94 | export function addMessage(sessionId: string, role: Message["role"], content: string): Message { | ||
| 95 | const db = getDb(); | ||
| 96 | const id = uuid(); | ||
| 97 | const now = Math.floor(Date.now() / 1000); | ||
| 98 | |||
| 99 | db.prepare( | ||
| 100 | "INSERT INTO messages (id, session_id, role, content, created_at) VALUES (?, ?, ?, ?, ?)" | ||
| 101 | ).run(id, sessionId, role, content, now); | ||
| 102 | |||
| 103 | db.prepare("UPDATE sessions SET updated_at = ? WHERE id = ?").run(now, sessionId); | ||
| 104 | |||
| 105 | return { id, session_id: sessionId, role, content, created_at: now }; | ||
| 106 | } | ||
