更新插件:使用 node:sqlite 替代 sqlite3 命令行,提高 Windows 兼容性

This commit is contained in:
ypshidifu
2026-09-21 17:21:54 +08:00
parent 0d19143065
commit 45e68bfecf
2 changed files with 30 additions and 30 deletions
+2
View File
@@ -0,0 +1,2 @@
node_modules/
*.js
+20 -22
View File
@@ -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<Session[]> {
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 (!existsSync(dbPath)) {
console.error("[oc-session-all] Database not found at:", dbPath);
return [];
}
if (!stdout.trim()) 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 lines = stdout.trim().split("\n");
const rows = db.prepare(query).all() as any[];
for (const line of lines) {
const parts = line.split("|");
if (parts.length >= 7) {
for (const row of rows) {
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,
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,