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
+25 -7
View File
@@ -55,10 +55,12 @@ export interface GraphCostSummary {
totalDollars: number;
/** Runs where EVERY stage was unpriced (totalDollars excludes them). */
unpricedRuns: number;
/** The three billable pipeline stages (merger/embedder are free). */
/** The billable pipeline stages (wave 6: the embedder became one —
* provider embeddings cost money; merger is the only free stage). */
stages: {
extraction: StageCost;
audit: StageCost;
embedding: StageCost;
naming: StageCost;
};
/** Newest first, capped at `recentCap` (default 10). */
@@ -113,6 +115,7 @@ function mergeTokens(into: CallUsage, steps: readonly CallUsage[]): void {
export function graphCostSummary(input: CostSummaryInput): GraphCostSummary {
const extraction = emptyStage();
const audit = emptyStage();
const embedding = emptyStage();
const naming = emptyStage();
const runs: RecentRun[] = [];
@@ -131,10 +134,25 @@ export function graphCostSummary(input: CostSummaryInput): GraphCostSummary {
if (audDollars === null) audit.unpricedRuns++;
else audit.dollars += audDollars;
// Run-level: null only when BOTH stages are unpriced.
const priced = extDollars ?? 0;
const runDollars = extDollars === null && audDollars === null ? null : priced + (audDollars ?? 0);
const allSteps = [...stages.extractor.steps, ...stages.auditor.steps];
// Wave 6: the embedding tail — present only when an embedding model
// ran (absent stage = the run embedded nothing, never "free").
let embDollars: number | null = null;
let embSteps: CallUsage[] = [];
if (stages.embedding) {
embedding.runs++;
embSteps = stages.embedding.steps;
mergeTokens(embedding.tokens, embSteps);
embDollars = stageDollars(stages.embedding.model, embSteps);
if (embDollars === null) embedding.unpricedRuns++;
else embedding.dollars += embDollars;
}
// Run-level: null only when EVERY stage is unpriced.
const allStages: Array<number | null> = [extDollars, audDollars, embDollars];
const runDollars = allStages.every((d) => d === null)
? null
: allStages.reduce<number>((a, d) => a + (d ?? 0), 0);
const allSteps = [...stages.extractor.steps, ...stages.auditor.steps, ...embSteps];
runs.push({
startedAt: run.startedAt,
at: run.at,
@@ -177,9 +195,9 @@ export function graphCostSummary(input: CostSummaryInput): GraphCostSummary {
// A run is "unpriced" when it billed nothing countable at all.
const unpricedRuns = runs.filter((r) => r.dollars === null).length;
return {
totalDollars: extraction.dollars + audit.dollars + naming.dollars,
totalDollars: extraction.dollars + audit.dollars + embedding.dollars + naming.dollars,
unpricedRuns,
stages: { extraction, audit, naming },
stages: { extraction, audit, embedding, naming },
runs: capped,
};
}