feat: Phase 2 — embeddings + hybrid memory.search
Adds a small embedder sidecar (Xenova/bge-small-en-v1.5, ONNX, CPU-only) that the web app calls inline on memory.write and memory.update, and on demand from the new memory.search tool. memory.search performs three candidate fetches in parallel — pgvector cosine similarity, Postgres full-text via plainto_tsquery + ts_rank_cd, and tag-set overlap — then fuses them with Reciprocal Rank Fusion (k=60). Each result carries its per-source rank so the model can see *why* a memory surfaced. The migrator boot step gained an idempotent embedding backfill: any row with embedding IS NULL is batched (32 at a time) through the embedder after SQL migrations apply. Safe to run on every boot. New tool memory.update fixes the missing edit path; centralises the re-embed-on-content-change rule alongside write. Stack additions: - apps/embedder/ — Fastify server, persistent /data/models volume so the ~30 MB model only downloads once - apps/web/lib/embedder.ts — typed HTTP client with batched embed + health probe - packages/schemas — MemoryUpdateInput, MemorySearchInput - docker-compose — embedder service, healthcheck, app + migrator both depend_on it healthy; EMBEDDER_URL promoted to a required env var Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { env } from "@/lib/env";
|
||||
|
||||
/**
|
||||
* Thin HTTP client for the embedder sidecar. Used by memory.write /
|
||||
* memory.update / memory.search and by the migrator's backfill step.
|
||||
*
|
||||
* Calls are blocking on purpose — write-path latency is a worthwhile
|
||||
* trade for "the memory I just wrote is searchable now."
|
||||
*/
|
||||
|
||||
export class EmbedderError extends Error {
|
||||
constructor(message: string, public readonly status?: number) {
|
||||
super(message);
|
||||
this.name = "EmbedderError";
|
||||
}
|
||||
}
|
||||
|
||||
function url(): string {
|
||||
const u = env().EMBEDDER_URL;
|
||||
if (!u) throw new EmbedderError("EMBEDDER_URL is not configured");
|
||||
return u.replace(/\/$/, "");
|
||||
}
|
||||
|
||||
/** Embed a batch of texts. Returns one vector per input. */
|
||||
export async function embedTexts(texts: string[]): Promise<number[][]> {
|
||||
if (texts.length === 0) return [];
|
||||
|
||||
const res = await fetch(`${url()}/embed`, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ texts }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const detail = await res.text().catch(() => "");
|
||||
throw new EmbedderError(
|
||||
`embedder returned ${res.status}: ${detail.slice(0, 200)}`,
|
||||
res.status,
|
||||
);
|
||||
}
|
||||
const body = (await res.json()) as { vectors: number[][] };
|
||||
if (!Array.isArray(body.vectors) || body.vectors.length !== texts.length) {
|
||||
throw new EmbedderError("embedder response shape mismatch");
|
||||
}
|
||||
return body.vectors;
|
||||
}
|
||||
|
||||
/** Embed a single text — convenience for one-off calls. */
|
||||
export async function embedText(text: string): Promise<number[]> {
|
||||
const [vec] = await embedTexts([text]);
|
||||
if (!vec) throw new EmbedderError("embedder returned no vector");
|
||||
return vec;
|
||||
}
|
||||
|
||||
/** Quick check used by the migrator before backfilling. */
|
||||
export async function embedderReady(): Promise<boolean> {
|
||||
try {
|
||||
const res = await fetch(`${url()}/health`);
|
||||
if (!res.ok) return false;
|
||||
const body = (await res.json()) as { ready?: boolean };
|
||||
return body.ready === true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user