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

93 lines (92 loc) • 4.17 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 { extractInlineRefMentions } from "../../session-logs/inline-refs.js"; import { MODEL_WORK_AGENT } from "./agent-builder.js"; /** The metadata of a row, with a column that is absent or empty left out. */ export function toMeta(r, sessionId) { return { sessionId, startedAt: typeof r.time_created === "number" ? r.time_created : undefined, endedAt: typeof r.time_updated === "number" ? r.time_updated : undefined, projectHint: typeof r.directory === "string" && r.directory.length > 0 ? r.directory : undefined, title: typeof r.title === "string" && r.title.length > 0 ? r.title : undefined, }; } /** Listing needs only `session`; reading a session needs `message` and `part`, and fails loudly without them. */ export const V1_TABLES = ["session"]; /** * Sessions that are not akm's model work. OpenCode 1 records the agent on the `session` row from the release that added * its `agent` column (May 2026); an older file has no column, and its sessions are all listed. */ function userSessionsFilter(db) { const hasAgent = db .prepare("PRAGMA table_info(session)") .all() .some((c) => c.name === "agent"); return hasAgent ? `(agent IS NULL OR agent != '${MODEL_WORK_AGENT}')` : "1"; } /** Every V1 session that is not akm's model work, updated at or after `sinceMs`, newest first. Throws on an incompatible schema. */ export function listV1Sessions(db, sinceMs) { const rows = db .prepare(`SELECT id, title, directory, time_created, time_updated FROM session WHERE ${userSessionsFilter(db)} AND time_updated >= ? ORDER BY time_updated DESC`) .all(sinceMs); return rows.map((r) => toMeta(r, r.id)); } /** * One event per message, text-parts concatenated in time order. Throws on an * incompatible schema or a session that is not in the file; individual unparsable * rows are skipped. */ export function readV1Session(db, harness, sessionId, filePath) { const meta = db .prepare("SELECT title, directory, time_created, time_updated FROM session WHERE id = ?") .get(sessionId); if (!meta) throw new Error(`session ${sessionId} is not in the file`); const messages = db .prepare("SELECT id, data, time_created FROM message WHERE session_id = ? ORDER BY time_created ASC, id ASC") .all(sessionId); const parts = db .prepare("SELECT message_id, data FROM part WHERE session_id = ? ORDER BY time_created ASC, id ASC") .all(sessionId); const textByMessage = new Map(); for (const part of parts) { let parsed; try { parsed = JSON.parse(part.data); } catch { continue; } if (parsed?.type !== "text") continue; const text = parsed.text; if (typeof text !== "string" || text.length < 1) continue; const bucket = textByMessage.get(part.message_id) ?? []; bucket.push(text); textByMessage.set(part.message_id, bucket); } const events = []; const inlineRefs = []; for (const message of messages) { let mdata = {}; try { mdata = JSON.parse(message.data); } catch { // role/timing unavailable — fall through with defaults } const role = typeof mdata.role === "string" ? mdata.role : "unknown"; const mtime = mdata.time?.created; const ts = typeof mtime === "number" ? mtime : typeof message.time_created === "number" ? message.time_created : undefined; const text = (textByMessage.get(message.id) ?? []).join("\n").trim(); if (text.length < 1) continue; events.push({ harness, text, ts, sessionId, role, filePath }); inlineRefs.push(...extractInlineRefMentions(text, ts)); } events.sort((a, b) => (a.ts ?? 0) - (b.ts ?? 0)); return { meta: toMeta(meta, sessionId), events, inlineRefs }; }