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

439 lines (438 loc) • 21.6 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/. /** * `akm search` — entry point. * * Search consults the local FTS5 index. There is one query path * because there is one data store; there is no provider fan-out. * * The orchestration here is thin: build the FTS query, optionally interleave * a registry search behind `--from registry|all`, and log a usage event. * Provider `search()` methods do not exist. */ import { getSources, loadConfig } from "../../core/config/config.js"; import { rethrowIfTestIsolationError, UsageError } from "../../core/errors.js"; import { appendEvent } from "../../core/events.js"; import { includedTypes, includesProposed, normalizeInclude } from "../../core/include.js"; import { redactCredentialPatterns } from "../../core/redaction.js"; import { withStateDbTelemetry } from "../../core/state-db.js"; import { indexLibraryForSource } from "../../indexer/index-persist.js"; import { resolveReadSources } from "../../indexer/read-preflight.js"; import { searchLocal } from "../../indexer/search/db-search.js"; import { getSearchHitAttribution, usageEventAttributionMetadata, } from "../../indexer/search/search-attribution.js"; import { insertUsageEvent } from "../../indexer/usage/usage-events.js"; import { TELEMETRY_BUSY_TIMEOUT_MS } from "../../storage/repositories/index-db.js"; import { searchRegistry } from "./registry-search.js"; const DEFAULT_LIMIT = 20; function duplicateConceptWarnings(hits, defaultBundle) { const ownersByConcept = new Map(); for (const hit of hits) { const displayRef = hit.ref.split("#", 1)[0] ?? hit.ref; const boundary = displayRef.indexOf("//"); const conceptId = boundary >= 0 ? displayRef.slice(boundary + 2) : displayRef; const owner = hit.origin ?? defaultBundle ?? "working-bundle"; const owners = ownersByConcept.get(conceptId) ?? []; if (!owners.includes(owner)) owners.push(owner); ownersByConcept.set(conceptId, owners); } return [...ownersByConcept] .filter(([, owners]) => owners.length > 1) .sort(([left], [right]) => left.localeCompare(right)) .map(([conceptId, owners]) => `Multiple bundles provide "${conceptId}": ${owners.join(", ")}. ` + "Unqualified refs resolve by configured bundle priority; use a bundle-qualified ref to select explicitly."); } export async function akmSearch(input) { const t0 = Date.now(); const byUsage = input.byUsage === true; const query = byUsage ? "" : input.query.trim(); const searchType = input.type ?? "any"; const limit = normalizeLimit(input.limit); const parsedSource = parseSearchSource(input.source ?? "local"); const config = loadConfig(); // Named-source filter: when --from is not a standard enum value, treat it // as a named source (a `bundles` key). Validated early (before // resolveSourceEntries, which can throw STASH_DIR_NOT_FOUND) so that a bad // --from name always produces INVALID_SOURCE_VALUE regardless of stash state. let namedSourceNames; let source; if (Array.isArray(parsedSource) || (parsedSource !== "local" && parsedSource !== "registry" && parsedSource !== "all")) { namedSourceNames = Array.isArray(parsedSource) ? parsedSource : [parsedSource]; for (const name of namedSourceNames) assertNamedSourceExists(config, name); source = "local"; } else { source = parsedSource; } if (byUsage) source = "local"; // A pure `--from registry` search needs no local stash at all (this is the // one path folded in from the retired `akm registry search`, which never // touched local source/stash resolution either) — only resolve local // sources when local hits are actually needed, or a named source narrows // to a local bundle. Without this, `resolveReadSources` (which can throw // STASH_DIR_NOT_FOUND via `resolveStashDir()`) would make registry-only // search fail on a machine with no stash ever configured. const needsLocalSources = source !== "registry" || namedSourceNames !== undefined; let allSources = needsLocalSources ? resolveReadSources(undefined, config).sources : []; // When a named source was requested, narrow the sources list to just that entry. if (namedSourceNames !== undefined) { allSources = narrowSourcesToName(allSources, namedSourceNames); // allSources may still be empty if the configured source dir doesn't exist on // disk (resolveSourceEntries skips non-existent dirs). Fall through to the // zero-sources guard below which emits a friendly warning. } if (needsLocalSources && allSources.length === 0) { const response = { schemaVersion: 1, bundleDir: "", source, hits: [], warnings: ["No bundles configured. Run `akm bundle create` to create your working bundle."], timing: { totalMs: Date.now() - t0 }, }; maybeLogSearchEvent(input, query, response); return response; } const primary = allSources[0]; const bundleDir = primary && !primary.unresolved ? primary.path : ""; const filters = normalizeScopeFilters(input.filters); const include = normalizeInclude(input.include); const includeProposed = input.includeProposed === true || includesProposed(include); const belief = parseBeliefFilterMode(input.belief ?? "current"); const localResult = primary === undefined ? undefined : await searchLocal({ query, searchType, limit, stashDir: indexLibraryForSource(primary), sources: allSources, config, filters, prefer: normalizeScopeFilters(input.prefer), since: input.since, until: input.until, includeProposed, beliefFilter: belief, // When `--from <name>` narrowed the source list above, propagate // that intent down to the database layer so FTS/vector hits from // sources outside the narrowed set cannot consume candidate budgets. // Without this, the index (which spans every configured source) // would leak hits from sources the caller did not request. restrictToSources: namedSourceNames !== undefined, includeExcludedTypes: input.includeSessions === true, includeTypes: includedTypes(include), readOnly: input.readOnly, project: input.project, byUsage, }); const registryResult = source === "local" ? undefined : await searchRegistry(query, { limit, registries: config.registries }); if (source === "local") { const localHits = localResult?.hits ?? []; const warnings = [ ...(localResult?.warnings ?? []), ...duplicateConceptWarnings(localHits, config.defaultBundle), ...(byUsage && input.query.trim() ? ["--by-usage lists by use and ignores the query."] : []), ...(byUsage && (parsedSource === "registry" || parsedSource === "all") ? [`--by-usage lists local bundles and ignores --from ${parsedSource}.`] : []), ]; const hasResults = localHits.length > 0; const response = { schemaVersion: 1, ...(bundleDir === undefined ? {} : { bundleDir }), source, ...bundlesField(namedSourceNames), hits: localHits, tip: hasResults ? undefined : localResult?.tip, warnings: warnings.length ? warnings : undefined, searchMode: localResult?.mode ?? "keyword", timing: { totalMs: Date.now() - t0, rankMs: localResult?.rankMs, embedMs: localResult?.embedMs }, }; maybeLogSearchEvent(input, query, response, usageSearchMode(localResult?.mode)); return response; } const registryHits = (registryResult?.hits ?? []).map((hit) => { const installRef = hit.installRef; // Hit-level `warnings` are forwarded when the provider surfaced any. return { type: "registry", name: hit.title, id: hit.id, description: hit.description, action: `akm bundle add ${installRef} -> then search again`, installRef, score: hit.score, registryName: hit.registryName, ...(hit.warnings && hit.warnings.length > 0 ? { warnings: hit.warnings } : {}), }; }); if (source === "registry") { const slicedRegistryHits = registryHits.slice(0, limit); const hasResults = slicedRegistryHits.length > 0; const response = { schemaVersion: 1, ...(bundleDir === undefined ? {} : { bundleDir }), source, hits: [], registryHits: slicedRegistryHits, tip: hasResults ? undefined : "No matching registry entries were found.", warnings: registryResult?.warnings.length ? registryResult.warnings : undefined, timing: { totalMs: Date.now() - t0 }, }; maybeLogSearchEvent(input, query, response); return response; } // source === "all" const allStashHits = (localResult?.hits ?? []).slice(0, limit); const warnings = [ ...(localResult?.warnings ?? []), ...duplicateConceptWarnings(allStashHits, config.defaultBundle), ...(registryResult?.warnings ?? []), ]; const hasResults = allStashHits.length > 0 || registryHits.length > 0; const response = { schemaVersion: 1, ...(bundleDir === undefined ? {} : { bundleDir }), source, ...bundlesField(namedSourceNames), hits: allStashHits, registryHits, tip: hasResults ? undefined : "No matching stash assets or registry entries were found.", warnings: warnings.length ? warnings : undefined, searchMode: localResult?.mode ?? "keyword", timing: { totalMs: Date.now() - t0 }, }; maybeLogSearchEvent(input, query, response, usageSearchMode(localResult?.mode)); return response; } /** Several `--from` bundles are reported back; a single one reports nothing extra, as before. */ function bundlesField(names) { return names !== undefined && names.length > 1 ? { bundles: names } : {}; } /** Usage telemetry retains its historical semantic|keyword vocabulary. */ function usageSearchMode(mode) { return mode === "semantic" ? "semantic" : "keyword"; } function maybeLogSearchEvent(input, query, response, mode) { if (input.skipLogging || input.readOnly || input.byUsage) return; logSearchEvent(query, response, mode, input.eventSource, input.attributionProjection); } /** The sources the named `--from <name>` values select (any of them): matched on bundle id or path. */ export function narrowSourcesToName(sources, names) { const wanted = typeof names === "string" ? [names] : names; return sources.filter((s) => wanted.some((name) => s.bundleId === name || s.path === name)); } /** * Fire-and-forget: log a search event to the usage_events table. * Never blocks the caller; errors are silently ignored. * * Result count semantics: * - `stashHitCount`: number of local stash hits (response.hits, source-only * entries). Always 0 for registry-only searches. * - `registryHitCount`: number of registry hits (response.registryHits). * Only non-zero when source is "registry" or "all". * - `resultCount`: total across both pools so telemetry reflects the actual * number of results the user saw, regardless of source mode. * * Per-entry events are recorded only for stash hits because registry hits * have no local entry_id to reference. */ function logSearchEvent(rawQuery, response, mode = "keyword", eventSource = "user", attributionProjection = "full") { // Credentials pasted into a query (e.g. by the Claude Code hook that // curates every user prompt) must never reach state.db verbatim — see // `redactCredentialPatterns`. Redacted once here so every persistence call // below (events.metadata_json via appendEvent, usage_events.query via // insertUsageEvent) gets the same scrubbed text. const query = redactCredentialPatterns(rawQuery); // Emit a structured event to events.jsonl so workflow-trace consumers // detect akm search invocations without relying on stdout scraping. const stashHits = response.hits.filter((h) => h.type !== "registry"); // Include registry hit refs so a show following a registry-only search generates a select event const registryHitRefs = (response.registryHits ?? []).map((h) => `registry:${h.id}`); // Lets a later `select` name the search it came from, so metrics can dedupe // repeated shows of one result and attribute them to this search's source. const searchId = crypto.randomUUID(); const allResultRefs = [...stashHits.map((h) => h.ref), ...registryHitRefs]; appendEvent({ eventType: "search", metadata: { query, hitCount: stashHits.length, resultRefs: allResultRefs, mode, source: eventSource, searchId }, }); try { // Each hit already carries its durable `bundle//conceptId` ref, so no index // connection is opened here. Short busy timeout: telemetry must never stall // the search result behind a background write on state.db. const resolved = stashHits.slice(0, 50).flatMap((hit) => (hit.itemRef ? [{ ref: hit.itemRef, hit }] : [])); const stashHitCount = response.hits.length; const registryHitCount = Array.isArray(response.registryHits) ? response.registryHits.length : 0; withStateDbTelemetry((stateDb) => { for (const { ref, hit } of resolved) { insertUsageEvent(stateDb, { event_type: "search", query, entry_ref: ref, metadata: usageEventAttributionMetadata(getSearchHitAttribution(hit), ref, attributionProjection), source: eventSource, }); } // Count registry hits separately so registry-only searches record a // non-zero resultCount. response.hits is always [] when source="registry". insertUsageEvent(stateDb, { event_type: "search", query, metadata: JSON.stringify({ resultCount: stashHitCount + registryHitCount, stashHitCount, registryHitCount, resolvedCount: resolved.length, mode, // Whole-search latency plus the rank/embed phases when the local // search reported them, so `akm metrics` can show timings later. totalMs: response.timing?.totalMs, rankMs: response.timing?.rankMs, embedMs: response.timing?.embedMs, }), source: eventSource, }); }, TELEMETRY_BUSY_TIMEOUT_MS); // No live utility_scores write here (#862): a search result is an // impression, not a signal that the asset was useful. Retrieval // counts are recorded above via insertUsageEvent (search_count) and // rolled into utility_scores by the offline `recomputeUtilityScores` // pass (`akm index`). } catch (err) { rethrowIfTestIsolationError(err); /* fire-and-forget */ } } // ── Helpers ────────────────────────────────────────────────────────────────── /** * Validate a named `--from` against the bundle-derived source list (a named * source is a `bundles` key, matched via its derived * source entry's `name`, or an exact path). Throws INVALID_SOURCE_VALUE with * the known names before any stash access can fail differently. */ export function assertNamedSourceExists(config, namedSourceName) { const configSources = getSources(config); const foundInConfig = configSources.find((source) => source.name === namedSourceName || source.path === namedSourceName); if (foundInConfig?.enabled === false) { throw new UsageError(`Source "${namedSourceName}" is disabled.`, "INVALID_SOURCE_VALUE"); } if (!foundInConfig) { const validNames = configSources .filter((source) => source.enabled !== false) .map((source) => source.name) .filter((name) => Boolean(name)); const hint = validNames.length > 0 ? `Known source names: ${validNames.join(", ")}` : "No named sources are configured. Run `akm bundle list` to see installed bundles."; throw new UsageError(`Unknown source name: "${namedSourceName}". ${hint}`, "INVALID_SOURCE_VALUE"); } } function normalizeLimit(limit) { if (typeof limit !== "number" || Number.isNaN(limit) || limit <= 0) { return DEFAULT_LIMIT; } return Math.min(Math.floor(limit), 200); } /** * Parse the `--from` flag value. * * Accepts: * - `local` (default) — search the local stash index only * - `registry` — search remote registries only * - `all` — search local and registries * - Any named configured bundle — filters local results to that bundle only. * Repeated (an array), several bundles are searched as one set (OR); a * keyword cannot be mixed with bundle names. * The named-source path is detected and resolved * inside `akmSearch`; this function returns the raw name so the caller can * pass it through to `akmSearch` which accepts `SearchSource | string`. * * Unknown values that are not a known enum AND not a named source will still * produce an error inside `akmSearch` when the config lookup finds nothing. * This allows the CLI to accept named sources without requiring config access * at parse time. */ export function parseSearchSource(source) { if (Array.isArray(source)) { const names = [...new Set(source)]; if (names.length <= 1) return parseSearchSource(names[0]); const keyword = names.find((name) => name === "local" || name === "registry" || name === "all"); if (keyword !== undefined) { throw new UsageError(`--from "${keyword}" cannot be combined with other --from values. Repeat --from only for bundle names.`, "INVALID_SOURCE_VALUE", `Use "${keyword}" alone, or repeat --from with configured bundle names only.`); } return names; } if (source === "local" || source === "registry" || source === "all") return source; if (typeof source === "undefined") return "local"; // Pass through unknown strings — they may be valid named sources. // `akmSearch` will validate against config.sources and throw a UsageError // with a helpful message if the name isn't found. return source; } export function parseBeliefFilterMode(value) { if (value === undefined || value === "all") return "all"; if (value === "current" || value === "historical") return value; throw new UsageError(`Invalid value for --belief: ${String(value)}. Expected one of: all|current|historical`, "INVALID_FLAG_VALUE"); } /** * Normalise scope filters to per-key value lists: strings are trimmed, and * non-string values and duplicates are dropped; an empty string stays (it * selects assets without the key). Returns `undefined` * when nothing meaningful remains, so callers don't pay for an empty-filter * post-walk in `searchLocal`. */ export function normalizeScopeFilters(raw) { if (!raw) return undefined; const out = Object.create(null); for (const [key, given] of Object.entries(raw)) { const values = new Set(); for (const value of Array.isArray(given) ? given : [given]) { if (typeof value === "string") values.add(value.trim()); } if (values.size > 0) out[key] = [...values]; } return Object.keys(out).length > 0 ? out : undefined; } /** * Parse repeated `--filter k=v` argv tokens into a * `ScopeFilters`: a repeated key collects all its values (matched as OR; see * `scopeMatchesFilters`). An empty value (`key=`) selects assets without the * key. Throws a {@link UsageError} for malformed tokens (missing `=` or an * empty key) so callers don't see ambiguous misses. * * Used by `akm search --filter` and `akm curate --filter`. */ export function parseScopeFilterFlags(values, flagName = "--filter") { if (values.length === 0) return undefined; const out = Object.create(null); for (const raw of values) { const eq = raw.indexOf("="); if (eq <= 0) { throw new UsageError(`Invalid ${flagName} value "${raw}". Expected key=value (e.g. team=platform).`); } const key = raw.slice(0, eq).trim(); const value = raw.slice(eq + 1).trim(); if (!key) { throw new UsageError(`Invalid ${flagName} value "${raw}". Expected key=value (e.g. team=platform).`); } out[key] = [...(out[key] ?? []), value]; } return Object.keys(out).length > 0 ? out : undefined; }