UNPKG

akm-cli

Version:

akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur

91 lines (90 loc) • 4.38 kB
// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. import fs from "node:fs"; import { toErrorMessage } from "../../../core/common.js"; import { warnOnce } from "../../../core/warn.js"; import { openDatabase } from "../../../storage/database.js"; import { openSqliteReadSnapshot } from "../../../storage/sqlite-read-snapshot.js"; import { AbstractSessionLogProvider } from "../../session-logs/provider-base.js"; import { akmOpencodeDatabasePath, databaseIn, opencodeDatabasePath } from "./roots.js"; import { listV1Sessions, readV1Session, V1_TABLES } from "./session-log-v1.js"; import { hasV2Tables, listV2Sessions, readV2Session } from "./session-log-v2.js"; function layoutOf(db) { const rows = db.prepare("SELECT name FROM sqlite_master WHERE type = 'table'").all(); const tables = new Set(rows.map((r) => r.name)); if (tables.has("session_v2") && hasV2Tables(db)) return "v2"; return V1_TABLES.every((t) => tables.has(t)) && !tables.has("session_v2") ? "v1" : "unsupported"; } export class OpenCodeProvider extends AbstractSessionLogProvider { name = "opencode"; #userDatabase = opencodeDatabasePath(); availabilityRoot() { return this.#userDatabase; } /** The user's OpenCode history, or akm's own. */ isAvailable() { return super.isAvailable() || fs.existsSync(akmOpencodeDatabasePath()); } /** The stores to read: the one under `location`, else the user's and akm's. */ #stores(location) { if (location) return [databaseIn(location)]; return [this.#userDatabase, akmOpencodeDatabasePath()]; } async listSessions(input = {}) { const sinceMs = input.sinceMs ?? 0; const listed = []; for (const dbPath of this.#stores(input.location)) { if (!fs.existsSync(dbPath)) continue; try { listed.push(...this.#listStore(dbPath, sinceMs, input.isolatedSnapshot === true)); } catch (error) { this.#warnUnavailable(dbPath, `cannot list it (${toErrorMessage(error)})`); } } return listed.sort((a, b) => (b.endedAt ?? 0) - (a.endedAt ?? 0)); } /** One session's events. A file or session that cannot be read throws, so the caller records the failure. */ async readSession(ref) { const layout = this.#withDb(ref.filePath, false, layoutOf); if (layout === "unsupported") { throw new Error(`${ref.filePath} has no supported OpenCode 1 or OpenCode 2 session tables`); } const read = this.#withDb(ref.filePath, false, (db) => layout === "v2" ? readV2Session(db, this.name, ref.sessionId, ref.filePath) : readV1Session(db, this.name, ref.sessionId, ref.filePath)); return { ref: this.sessionRef({ ...read.meta, cwd: read.meta.projectHint, filePath: ref.filePath }), events: read.events, inlineRefs: read.inlineRefs, }; } #listStore(dbPath, sinceMs, isolated) { const layout = this.#withDb(dbPath, isolated, layoutOf); if (layout === "unsupported") { this.#warnUnavailable(dbPath, `it has neither the OpenCode 1 (${V1_TABLES.join("/")}) nor the OpenCode 2 tables`); return []; } const metas = this.#withDb(dbPath, isolated, (db) => layout === "v2" ? listV2Sessions(db, sinceMs) : listV1Sessions(db, sinceMs)); return metas.map((meta) => this.sessionRef({ ...meta, cwd: meta.projectHint, filePath: dbPath })); } /** Run `use` on the SQLite file at `dbPath`, read-only (a private copy when `isolated`). */ #withDb(dbPath, isolated, use) { const db = isolated ? openSqliteReadSnapshot(dbPath) : openDatabase(dbPath, { readonly: true, create: false }); if (!db) throw new Error("the file disappeared"); try { return use(db); } finally { db.close(); } } #warnUnavailable(dbPath, why) { warnOnce(`opencode-history:${dbPath}:${why}`, `OpenCode session history at ${dbPath} was not read: ${why}. This is an unsupported or damaged store, not an empty history.`); } }