UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

29 lines (28 loc) • 2.45 kB
/** * design/81 Slice 5 — the shared distance primitives for the consolidation `searchScored` retrieval, extracted * BYTE-IDENTICAL from the original PgMemoryStore implementation so the File (portable/TOC) and Pg (native/TOB) * backends rank on ONE source of truth and can't drift. The `{@link import("./runner/memory-consolidation.js")}` * band `{0.05, 0.3}` is defined on this cosine-distance scale; the lexical `1 - Jaccard ∈ [0,1] ⊂ [0,2]` is the * same-zero-point stand-in used when no embedding vector is present (fail-open per row). * * The capability LADDER a store reports via {@link import("./memory.js").MemoryStore.vectorMode}: * - `lexical` — no embedder: `1 - Jaccard(terms)` everywhere. * - `portable` — embedder + IN-PROCESS cosine over stored vectors (File jsonl / Pg jsonb / MySQL JSON — ANY * backend that can store a float array; O(n) over the scope, no index). * - `native` — embedder + a backend index/operator (pgvector `<=>` / TiDB `VEC_COSINE_DISTANCE`). * MySQL is NOT "can't support vectors" — it is `portable`-capable (JSON column + in-process cosine), just not * `native`. A deployment injects an embedder (config-driven); the store then reports the achieved rung. */ /** Lower-cased lexical term set: alphanumeric runs PLUS CJK character bigrams (a single-character * run contributes its unigram). CJK scripts have no `a-z0-9` runs at all, so an alphanumeric-only * tokenizer made every pure-CJK entry an EMPTY set — never a lexical-rung candidate for any query * — and a pure-CJK query returned nothing; character bigrams are the standard analyzer unit there * (word boundaries are not written). Bigrams stay within one run: adjacency across an intervening * non-CJK character is not real adjacency. */ export declare function termSet(s: string): Set<string>; /** Lexical stand-in distance: `1 - Jaccard(terms)` ∈ [0,1] ⊂ [0,2]; `null` = no overlap (not a candidate). */ export declare function jaccardDistance(query: Set<string>, text: string): number | null; /** Cosine distance ∈ [0,2] (0 = identical), the {@link import("./memory.js").ScoredMemory} contract. Uses * `Math.min(a.length,b.length)` — a store MUST only call this on dimension-matched vectors (the row-level * dimension guard lives at the caller; a mismatch falls open to {@link jaccardDistance}). */ export declare function cosineDistance(a: number[], b: number[]): number;