aboutsummaryrefslogtreecommitdiffstats
path: root/dist/main/db/sessions.js
blob: b55489899959a792cdf1fdb9cb1144ce33c99dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listSessions = listSessions;
exports.getSession = getSession;
exports.createSession = createSession;
exports.updateSession = updateSession;
exports.deleteSession = deleteSession;
exports.listMessages = listMessages;
exports.addMessage = addMessage;
const index_1 = require("./index");
const uuid_1 = require("uuid");
function listSessions(projectId) {
    return (0, index_1.getDb)()
        .prepare("SELECT * FROM sessions WHERE project_id = ? ORDER BY updated_at DESC")
        .all(projectId);
}
function getSession(id) {
    return (0, index_1.getDb)()
        .prepare("SELECT * FROM sessions WHERE id = ?")
        .get(id);
}
function createSession(projectId, name) {
    const db = (0, index_1.getDb)();
    const id = (0, uuid_1.v4)();
    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,
    };
}
function updateSession(id, updates) {
    const db = (0, index_1.getDb)();
    const sets = [];
    const values = [];
    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);
    }
}
function deleteSession(id) {
    (0, index_1.getDb)().prepare("DELETE FROM sessions WHERE id = ?").run(id);
}
// Messages
function listMessages(sessionId) {
    return (0, index_1.getDb)()
        .prepare("SELECT * FROM messages WHERE session_id = ? ORDER BY created_at ASC")
        .all(sessionId);
}
function addMessage(sessionId, role, content) {
    const db = (0, index_1.getDb)();
    const id = (0, uuid_1.v4)();
    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 };
}