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
46 lines (45 loc) • 1.67 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/.
const EMBED_CACHE_MAX = 100;
const embedCache = new Map();
/**
* Build a cache key from query text and optional config.
* Different endpoints/models should not share cached embeddings.
* apiKey deliberately excluded: same endpoint+model produce identical embeddings regardless of auth.
*/
export function embedCacheKey(text, config) {
if (!config)
return `local::${text}`;
const endpoint = config.endpoint || "";
const model = config.model || config.localModel || "";
return `${endpoint}:${model}:${text}`;
}
export function getCachedEmbedding(key) {
const cached = embedCache.get(key);
if (cached === undefined)
return undefined;
// Move to end (most recently used) for LRU ordering
embedCache.delete(key);
embedCache.set(key, cached);
return cached;
}
export function setCachedEmbedding(key, value) {
// Delete first so an overwrite refreshes LRU recency AND is not counted as a
// new insert: only a genuinely new key at capacity should evict the oldest.
embedCache.delete(key);
if (embedCache.size >= EMBED_CACHE_MAX) {
const oldest = embedCache.keys().next().value;
if (oldest !== undefined) {
embedCache.delete(oldest);
}
}
embedCache.set(key, value);
}
/**
* Clear the embedding cache. Call when the embedding model changes
* or when you want to force fresh embeddings.
*/
export function clearEmbeddingCache() {
embedCache.clear();
}