BYOK provider embeddings (wave 6 chunk B): kill local transformers

Replace the @huggingface/transformers local embedder (~316MB ONNX cache)
with an EmbeddingPort seam: graph.embeddingModel (nullable "provider:id"
slug) selects an OpenRouter/OpenAI/Gemini adapter, resolved fresh per
call from settings + the auth store so key rotations land immediately.

- src/server/embeddings.ts rewritten: slug grammar, injected-fetch
  adapters (OpenRouter input_type, Gemini taskType batching, OpenAI
  index-ordered rows), honest resolution (null = skip-with-info, set
  model with missing key = honest error), embedNewNodes with model-change
  re-embed wipe + ghost pruning + usage steps, federated catalog
  (OpenRouter /api/v1/embeddings/models — VERIFIED live, the inverse
  path 404s; OpenAI text-embedding- filter; Gemini embedContent filter).
- GET /api/models/embeddings: federated catalog, empty when no keys.
- Extraction tail records stages.embedding usage into the run's money
  channel; EMBEDDING_PRICES overlay in core/prices.ts (outside the
  sync-generated block), callCost falls back to it; cost-summary folds
  the fourth stage; graph-context injection resolves its embedder per
  run, fails honestly when unconfigured, records query tokens.
- Settings: GraphSettings.embeddingModel (null factory), section parser
  validates slug shape (id may contain colons — OpenRouter :free
  suffixes, live catalog fact).
- Removal: @huggingface/transformers uninstalled, PATHS.modelCache and
  onnx tracing entries deleted, @huggingface tracing-excludes guard
  added, stale sharp/libvips comments updated to historical.

Live-verified on port 3004: catalog federates 36 rows (33 OpenRouter +
3 OpenAI), embedNewNodes embedded 10 nodes via the free liquid/lfm-2.5
model (dim 1024, 435 prompt tokens, 1 call, $0), settings PUT rejects
bad slugs with 400.

Gates: typecheck, lint, 895/895 tests, build.
This commit is contained in:
amosblomqvist
2026-09-23 03:53:57 +03:00
parent a6338511d3
commit 0a4a9a11de
29 changed files with 1233 additions and 841 deletions
+26 -1
View File
@@ -2752,6 +2752,30 @@ export const CONTEXT_LIMITS: Record<string, number> = {
// ─── Pure helpers (folds + cost inference) ───────────────────────────────────
// ─── Embedding prices (wave 6, 2026-09-23) ─────────────────────────────────
/** Hand-maintained BYOK embedding prices, USD per 1M INPUT tokens — the
* sync script never touches this block (pi-ai's catalogs carry no
* embedding models, so the generated PRICES table has none of these).
* VERIFIED against provider pricing pages 2026-09-23: OpenAI's model
* docs ($0.02 3-small / $0.13 3-large / $0.10 ada-002), Google's pricing
* page ($0.15 gemini-embedding-001 / $0.20 Gemini Embedding 2 text),
* OpenRouter passes provider pricing through. output: 0 — embeddings
* emit no output tokens. Unlisted models stay unpriced ("—", never
* zero); tokens still get captured. */
export const EMBEDDING_PRICES: Record<string, ModelPrice> = {
"openai:text-embedding-3-small": { input: 0.02, output: 0 },
"openai:text-embedding-3-large": { input: 0.13, output: 0 },
"openai:text-embedding-ada-002": { input: 0.1, output: 0 },
"google:gemini-embedding-001": { input: 0.15, output: 0 },
"google:gemini-embedding-2": { input: 0.2, output: 0 },
"openrouter:openai/text-embedding-3-small": { input: 0.02, output: 0 },
"openrouter:openai/text-embedding-3-large": { input: 0.13, output: 0 },
"openrouter:openai/text-embedding-ada-002": { input: 0.1, output: 0 },
"openrouter:google/gemini-embedding-001": { input: 0.15, output: 0 },
};
/** Sum a list of per-step usages into one CallUsage. A field stays
* undefined when EVERY step left it unreported — absent is not zero. */
export function sumSteps(steps: readonly CallUsage[]): CallUsage {
@@ -2773,7 +2797,8 @@ export function callCost(
usage: { inputTokens?: number; outputTokens?: number; cacheReadTokens?: number; cacheWriteTokens?: number },
model: string,
): number | null {
const price = PRICES[model];
// Wave 6: the hand-maintained embedding overlay backs the lookup too.
const price = PRICES[model] ?? EMBEDDING_PRICES[model];
if (!price) return null;
if (usage.inputTokens === undefined || usage.outputTokens === undefined) return null;
const cacheRead = usage.cacheReadTokens ?? 0;