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
48 lines (47 loc) • 2.3 kB
JavaScript
// 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/.
/** What people searched for and akm found nothing, grouped by query. */
import { metricsWindow, readUsageRows } from "../commands/metrics/metrics-cli.js";
import { stateDbUnavailable, withReadonlyStateDb } from "./readers.js";
/** An event the person made themselves, not an agent's, improve's or a task's. */
export const isUserEvent = (row) => row.source === "user";
/** A search the user ran themselves, not a lookup of one asset and not an agent's search. */
export const isUserSearch = (row) => row.eventType === "search" && row.ref === undefined && isUserEvent(row);
const normalize = (query) => query.trim().replace(/\s+/g, " ").toLowerCase();
/** The user's searches that found nothing, grouped by normalised query, repeats first then most recent. */
export function missesFromRows(rows) {
const byQuery = new Map();
for (const row of rows) {
if (!isUserSearch(row) || row.resultCount !== 0)
continue;
const query = row.query?.trim();
if (!query)
continue;
const key = normalize(query);
const entry = byQuery.get(key) ?? { query, count: 0, days: new Set(), lastAt: row.at };
entry.count += 1;
entry.days.add(row.at.slice(0, 10));
if (row.at > entry.lastAt) {
entry.lastAt = row.at;
entry.query = query;
}
byQuery.set(key, entry);
}
return [...byQuery.values()]
.map(({ days, ...rest }) => ({ ...rest, days: days.size, repeated: days.size >= 2 }))
.sort((a, b) => Number(b.repeated) - Number(a.repeated) || b.lastAt.localeCompare(a.lastAt) || a.query.localeCompare(b.query));
}
export function searchMisses(input = {}) {
const window = metricsWindow(input.since || "30d");
const rows = withReadonlyStateDb((db) => readUsageRows(db, window), null);
if (!rows)
return stateDbUnavailable();
const misses = missesFromRows(rows);
return {
available: true,
searches: rows.filter(isUserSearch).length,
foundNothing: misses.reduce((sum, m) => sum + m.count, 0),
misses,
};
}