aboutsummaryrefslogtreecommitdiffstats
path: root/dist/main/db/sessions.js
diff options
context:
space:
mode:
Diffstat (limited to 'dist/main/db/sessions.js')
-rw-r--r--dist/main/db/sessions.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/dist/main/db/sessions.js b/dist/main/db/sessions.js
new file mode 100644
index 0000000..b554898
--- /dev/null
+++ b/dist/main/db/sessions.js
@@ -0,0 +1,72 @@
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.listSessions = listSessions;
4exports.getSession = getSession;
5exports.createSession = createSession;
6exports.updateSession = updateSession;
7exports.deleteSession = deleteSession;
8exports.listMessages = listMessages;
9exports.addMessage = addMessage;
10const index_1 = require("./index");
11const uuid_1 = require("uuid");
12function listSessions(projectId) {
13 return (0, index_1.getDb)()
14 .prepare("SELECT * FROM sessions WHERE project_id = ? ORDER BY updated_at DESC")
15 .all(projectId);
16}
17function getSession(id) {
18 return (0, index_1.getDb)()
19 .prepare("SELECT * FROM sessions WHERE id = ?")
20 .get(id);
21}
22function createSession(projectId, name) {
23 const db = (0, index_1.getDb)();
24 const id = (0, uuid_1.v4)();
25 const now = Math.floor(Date.now() / 1000);
26 db.prepare(`INSERT INTO sessions (id, project_id, name, phase, permission_mode, created_at, updated_at)
27 VALUES (?, ?, ?, 'research', 'acceptEdits', ?, ?)`).run(id, projectId, name, now, now);
28 return {
29 id,
30 project_id: projectId,
31 name,
32 phase: "research",
33 claude_session_id: null,
34 permission_mode: "acceptEdits",
35 created_at: now,
36 updated_at: now,
37 };
38}
39function updateSession(id, updates) {
40 const db = (0, index_1.getDb)();
41 const sets = [];
42 const values = [];
43 for (const [key, value] of Object.entries(updates)) {
44 if (value !== undefined) {
45 sets.push(`${key} = ?`);
46 values.push(value);
47 }
48 }
49 if (sets.length > 0) {
50 sets.push("updated_at = ?");
51 values.push(Math.floor(Date.now() / 1000));
52 values.push(id);
53 db.prepare(`UPDATE sessions SET ${sets.join(", ")} WHERE id = ?`).run(...values);
54 }
55}
56function deleteSession(id) {
57 (0, index_1.getDb)().prepare("DELETE FROM sessions WHERE id = ?").run(id);
58}
59// Messages
60function listMessages(sessionId) {
61 return (0, index_1.getDb)()
62 .prepare("SELECT * FROM messages WHERE session_id = ? ORDER BY created_at ASC")
63 .all(sessionId);
64}
65function addMessage(sessionId, role, content) {
66 const db = (0, index_1.getDb)();
67 const id = (0, uuid_1.v4)();
68 const now = Math.floor(Date.now() / 1000);
69 db.prepare("INSERT INTO messages (id, session_id, role, content, created_at) VALUES (?, ?, ?, ?, ?)").run(id, sessionId, role, content, now);
70 db.prepare("UPDATE sessions SET updated_at = ? WHERE id = ?").run(now, sessionId);
71 return { id, session_id: sessionId, role, content, created_at: now };
72}