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

36 lines (35 loc) • 1.34 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/. /** * Cosine similarity between two embedding vectors. * * Lives next to {@link EmbeddingVector} so importers (notably `db.ts`) * can pull just the math without dragging in the embedder facade and its * transitive Transformers.js runtime import chain. * * Returns 0 when the vectors have different dimensions — silently * computing on a truncated view would produce meaningless scores. */ export function cosineSimilarity(a, b) { if (a.length !== b.length) { warn("cosineSimilarity: vector dimension mismatch (%d vs %d) — re-index recommended", a.length, b.length); return 0; } const len = a.length; if (len === 0) return 0; let dot = 0; let magA = 0; let magB = 0; for (let i = 0; i < len; i++) { dot += a[i] * b[i]; magA += a[i] * a[i]; magB += b[i] * b[i]; } const denom = Math.sqrt(magA) * Math.sqrt(magB); return denom === 0 ? 0 : dot / denom; } // Imported lazily to keep this types module dependency-free where possible; // `warn` is a thin printf wrapper so the cost is negligible. import { warn } from "../../core/warn.js";