diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5f2ba6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +*.js diff --git a/src/index.ts b/src/index.ts index faa2175..c0880e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,12 +8,9 @@ */ import { define } from "@opencode-ai/plugin/v2/promise"; -import { exec } from "child_process"; -import { promisify } from "util"; import { join } from "path"; import { existsSync } from "fs"; - -const execAsync = promisify(exec); +import { DatabaseSync } from "node:sqlite"; interface Session { id: string; @@ -43,34 +40,35 @@ function getDbPath(): string { } /** - * Query all sessions from the database + * Query all sessions from the database using node:sqlite */ -async function getAllSessionsFromDb(): Promise { +function getAllSessionsFromDb(): Session[] { try { const dbPath = getDbPath(); - const query = `SELECT id, project_id, directory, title, time_created, time_updated, cost FROM session_v2 ORDER BY time_updated DESC;`; - const { stdout } = await execAsync(`sqlite3 "${dbPath}" "${query}"`); - - if (!stdout.trim()) return []; - - const sessions: Session[] = []; - const lines = stdout.trim().split("\n"); - - for (const line of lines) { - const parts = line.split("|"); - if (parts.length >= 7) { - sessions.push({ - id: parts[0], - project_id: parts[1], - directory: parts[2], - title: parts[3] || null, - time_created: parseInt(parts[4]) || 0, - time_updated: parseInt(parts[5]) || 0, - cost: parseFloat(parts[6]) || 0, - }); - } + if (!existsSync(dbPath)) { + console.error("[oc-session-all] Database not found at:", dbPath); + return []; } - + + const db = new DatabaseSync(dbPath, { open: true, readOnly: true }); + const query = `SELECT id, project_id, directory, title, time_created, time_updated, cost FROM session_v2 ORDER BY time_updated DESC;`; + + const sessions: Session[] = []; + const rows = db.prepare(query).all() as any[]; + + for (const row of rows) { + sessions.push({ + id: row.id, + project_id: row.project_id, + directory: row.directory, + title: row.title || null, + time_created: row.time_created || 0, + time_updated: row.time_updated || 0, + cost: row.cost || 0, + }); + } + + db.close(); return sessions; } catch (error) { console.error("[oc-session-all] Failed to query database:", error); @@ -135,7 +133,7 @@ export default define({ input: {}, // No input parameters required async execute(input) { console.log("[oc-session-all] Tool executed"); - const sessions = await getAllSessionsFromDb(); + const sessions = getAllSessionsFromDb(); const output = formatSessions(sessions); return { content: output, @@ -146,4 +144,4 @@ export default define({ console.log("[oc-session-all] Tool registered successfully"); }, -}); +}); \ No newline at end of file