From 0a4a9a11defa557e8fc7de1696cee97265f27661 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Wed, 23 Sep 2026 03:53:57 +0300 Subject: [PATCH] BYOK provider embeddings (wave 6 chunk B): kill local transformers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/api/models/embeddings/route.ts | 17 + components/settings/appearance-section.tsx | 4 +- config.ts | 5 - next-env.d.ts | 4 +- next.config.ts | 15 +- package-lock.json | 694 ++------------------- package.json | 1 - scripts/embed-graph.ts | 33 +- src/agent/subagents/extraction.ts | 49 +- src/agent/subagents/graph-context.ts | 62 +- src/agent/subagents/headless.ts | 12 +- src/core/contracts/chat.ts | 4 + src/core/cost-summary.ts | 32 +- src/core/ports.ts | 46 ++ src/core/prices.ts | 27 +- src/core/settings-resolution.ts | 3 + src/core/settings.ts | 17 +- src/server/chat-handler.ts | 32 +- src/server/embeddings.ts | 478 +++++++++++--- src/server/index.ts | 1 + src/server/settings-body.ts | 25 +- tests/cost-summary.test.ts | 33 + tests/data-dir-seam.test.ts | 8 +- tests/embeddings.test.ts | 401 ++++++++++++ tests/graph-context-job.test.ts | 20 +- tests/headless-png.test.ts | 9 +- tests/settings-body.test.ts | 24 + tests/settings-resolution.test.ts | 16 +- tests/settings.test.ts | 2 +- 29 files changed, 1233 insertions(+), 841 deletions(-) create mode 100644 app/api/models/embeddings/route.ts create mode 100644 tests/embeddings.test.ts diff --git a/app/api/models/embeddings/route.ts b/app/api/models/embeddings/route.ts new file mode 100644 index 0000000..fe1cf23 --- /dev/null +++ b/app/api/models/embeddings/route.ts @@ -0,0 +1,17 @@ +/** + * GET /api/models/embeddings — the BYOK embedding-model picker's data + * (wave 6, owner ruling 2026-09-23): every KEYED provider's embedding + * catalog, federated LIVE (unlike /api/models — pi-ai's static catalog + * carries no embedding models). Rows are provider-qualified slugs, the + * exact shape graph.embeddingModel stores. Empty array when no provider + * is keyed (or every catalog fetch failed — per-provider failures are + * logged and skipped, never 500s). + */ +import { embeddingModelCatalog } from "@/src/server"; + +export const dynamic = "force-dynamic"; + +export async function GET() { + const models = await embeddingModelCatalog(); + return Response.json(models); +} \ No newline at end of file diff --git a/components/settings/appearance-section.tsx b/components/settings/appearance-section.tsx index 6b12247..18bd4fd 100644 --- a/components/settings/appearance-section.tsx +++ b/components/settings/appearance-section.tsx @@ -139,9 +139,9 @@ export function AppearanceSection({ } const LADDER: readonly { - key: keyof GraphSettings; + key: "enabled" | "embedding" | "clustering" | "labeling"; label: string; - needs: keyof GraphSettings | null; + needs: "enabled" | "embedding" | "clustering" | "labeling" | null; }[] = [ { key: "enabled", label: "Graph", needs: null }, { key: "embedding", label: "Embedding", needs: "enabled" }, diff --git a/config.ts b/config.ts index 49057b1..1dfe46b 100644 --- a/config.ts +++ b/config.ts @@ -26,11 +26,6 @@ export const PATHS = { /** Chat sessions (AI SDK UIMessage transcripts, one JSON file per chat). */ chats: path.join(DATA_ROOT, "chats"), graph: path.join(DATA_ROOT, "graph"), - /** transformers.js model cache (server-side embeddings, ~300MB). - * null in dev = keep the package default (the model is already - * downloaded there); under LH_DATA_DIR the packaged app must never - * try to cache inside its own bundle. */ - modelCache: process.env.LH_DATA_DIR ? path.join(DATA_ROOT, "models") : null, } as const; // ─── Models ──────────────────────────────────────────────────────────────── diff --git a/next-env.d.ts b/next-env.d.ts index a419cbe..ce4e94a 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -1,7 +1,7 @@ /// /// -import "./.next/dev/types/routes.d.ts"; -import "./.next/dev/types/root-params.d.ts"; +import "./.next/types/routes.d.ts"; +import "./.next/types/root-params.d.ts"; // NOTE: This file should not be edited // see https://nextjs.org/docs/app/api-reference/config/typescript for more information. diff --git a/next.config.ts b/next.config.ts index 373738f..d7b3b5a 100644 --- a/next.config.ts +++ b/next.config.ts @@ -12,7 +12,13 @@ const nextConfig: NextConfig = { // INTO the standalone bundle. A shipped desktop app would carry the // developer's session data inside it. outputFileTracingExcludes: { - "*": ["data/**/*"], + "*": [ + "data/**/*", + // Wave 6 (2026-09-23): the local transformers embeddings died — any + // @huggingface copy (or its ~300MB model cache, if one ever + // reappears) must never sweep into the standalone bundle again. + "node_modules/@huggingface/**/*", + ], }, // Empirically verified gaps (2026-09-17 standalone smoke): the tracer // misses JSON loaded via `import ... with { type: "json" }` attributes — @@ -31,21 +37,14 @@ const nextConfig: NextConfig = { "node_modules/pdfjs-dist/standard_fonts/**/*", "node_modules/@earendil-works/pi-ai/dist/providers/data/**/*", "node_modules/@earendil-works/pi-agent-core/dist/**/*.json", - // onnxruntime-node: the .node binding was traced, but its - // sibling dylib (libonnxruntime.*.dylib, dlopen'd via @rpath) - // was not — dlopen fails at module load. - "node_modules/onnxruntime-node/bin/**/*", ], }, // Framework-free core lives in src/ (plain TS, imported by routes and CLI alike). // The pi engine must stay external: its providers are runtime-loaded Node // code (undici streams, catalog data) that breaks when bundled into routes. - // transformers.js (embeddings) is runtime-loaded ONNX — same reason as - // the pi engine: bundling onnxruntime-node breaks route builds. serverExternalPackages: [ "@earendil-works/pi-ai", "@earendil-works/pi-agent-core", - "@huggingface/transformers", "pdfjs-dist", // sources v0: bundling breaks its node fake-worker path ], }; diff --git a/package-lock.json b/package-lock.json index 785ac63..3d56219 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,6 @@ "@ai-sdk/react": "^4.0.79", "@earendil-works/pi-agent-core": "^0.85.1", "@earendil-works/pi-ai": "^0.85.1", - "@huggingface/transformers": "^4.2.0", "@mozilla/readability": "^0.6.0", "@radix-ui/react-use-controllable-state": "^1.2.6", "@rive-app/react-webgl2": "^4.32.1", @@ -2231,582 +2230,6 @@ } } }, - "node_modules/@huggingface/jinja": { - "version": "0.5.10", - "resolved": "https://registry.npmjs.org/@huggingface/jinja/-/jinja-0.5.10.tgz", - "integrity": "sha512-SgS1D1bglQ94ceD4ZCL6eayUDy9uV1xuyk61OjgSxU5GJh7upqZCKII0JoTYMc6wWsg3JUgaPRX0ElQzXPk3Cw==", - "license": "MIT", - "engines": { - "node": ">=18" - } - }, - "node_modules/@huggingface/tokenizers": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/@huggingface/tokenizers/-/tokenizers-0.1.3.tgz", - "integrity": "sha512-8rF/RRT10u+kn7YuUbUg0OF30K8rjTc78aHpxT+qJ1uWSqxT1MHi8+9ltwYfkFYJzT/oS+qw3JVfHtNMGAdqyA==", - "license": "Apache-2.0" - }, - "node_modules/@huggingface/transformers": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/@huggingface/transformers/-/transformers-4.2.0.tgz", - "integrity": "sha512-8BRCoBMH0XsWaEIamuR0LrJGAfftgHAfb2Vrffy0VKlSAE/MnUJ5/h/zTfEP3fDIft+nk7TqB8xXEyABGitBjQ==", - "license": "Apache-2.0", - "dependencies": { - "@huggingface/jinja": "^0.5.6", - "@huggingface/tokenizers": "^0.1.3", - "onnxruntime-node": "1.24.3", - "onnxruntime-web": "1.26.0-dev.20260416-b7804b056c", - "sharp": "^0.34.5" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-darwin-arm64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-darwin-arm64/-/sharp-darwin-arm64-0.34.5.tgz", - "integrity": "sha512-imtQ3WMJXbMY4fxb/Ndp6HBTNVtWCUI0WdobyheGf5+ad6xX8VIDO8u2xE4qc/fr08CKG/7dDseFtn6M6g/r3w==", - "cpu": [ - "arm64" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-darwin-arm64": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-darwin-x64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-darwin-x64/-/sharp-darwin-x64-0.34.5.tgz", - "integrity": "sha512-YNEFAF/4KQ/PeW0N+r+aVVsoIY0/qxxikF2SWdp+NRkmMB7y9LBZAVqQ4yhGCm/H3H270OSykqmQMKLBhBJDEw==", - "cpu": [ - "x64" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-darwin-x64": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-darwin-arm64": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-arm64/-/sharp-libvips-darwin-arm64-1.2.4.tgz", - "integrity": "sha512-zqjjo7RatFfFoP0MkQ51jfuFZBnVE2pRiaydKJ1G/rHZvnsrHAOcQALIi9sA5co5xenQdTugCvtb1cuf78Vf4g==", - "cpu": [ - "arm64" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "darwin" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-darwin-x64": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-x64/-/sharp-libvips-darwin-x64-1.2.4.tgz", - "integrity": "sha512-1IOd5xfVhlGwX+zXv2N93k0yMONvUlANylbJw1eTah8K/Jtpi15KC+WSiaX/nBmbm2HxRM1gZ0nSdjSsrZbGKg==", - "cpu": [ - "x64" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "darwin" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-linux-arm": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm/-/sharp-libvips-linux-arm-1.2.4.tgz", - "integrity": "sha512-bFI7xcKFELdiNCVov8e44Ia4u2byA+l3XtsAj+Q8tfCwO6BQ8iDojYdvoPMqsKDkuoOo+X6HZA0s0q11ANMQ8A==", - "cpu": [ - "arm" - ], - "libc": [ - "glibc" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "linux" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-linux-arm64": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm64/-/sharp-libvips-linux-arm64-1.2.4.tgz", - "integrity": "sha512-excjX8DfsIcJ10x1Kzr4RcWe1edC9PquDRRPx3YVCvQv+U5p7Yin2s32ftzikXojb1PIFc/9Mt28/y+iRklkrw==", - "cpu": [ - "arm64" - ], - "libc": [ - "glibc" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "linux" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-linux-ppc64": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-ppc64/-/sharp-libvips-linux-ppc64-1.2.4.tgz", - "integrity": "sha512-FMuvGijLDYG6lW+b/UvyilUWu5Ayu+3r2d1S8notiGCIyYU/76eig1UfMmkZ7vwgOrzKzlQbFSuQfgm7GYUPpA==", - "cpu": [ - "ppc64" - ], - "libc": [ - "glibc" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "linux" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-linux-riscv64": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-riscv64/-/sharp-libvips-linux-riscv64-1.2.4.tgz", - "integrity": "sha512-oVDbcR4zUC0ce82teubSm+x6ETixtKZBh/qbREIOcI3cULzDyb18Sr/Wcyx7NRQeQzOiHTNbZFF1UwPS2scyGA==", - "cpu": [ - "riscv64" - ], - "libc": [ - "glibc" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "linux" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-linux-s390x": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-s390x/-/sharp-libvips-linux-s390x-1.2.4.tgz", - "integrity": "sha512-qmp9VrzgPgMoGZyPvrQHqk02uyjA0/QrTO26Tqk6l4ZV0MPWIW6LTkqOIov+J1yEu7MbFQaDpwdwJKhbJvuRxQ==", - "cpu": [ - "s390x" - ], - "libc": [ - "glibc" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "linux" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-linux-x64": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-x64/-/sharp-libvips-linux-x64-1.2.4.tgz", - "integrity": "sha512-tJxiiLsmHc9Ax1bz3oaOYBURTXGIRDODBqhveVHonrHJ9/+k89qbLl0bcJns+e4t4rvaNBxaEZsFtSfAdquPrw==", - "cpu": [ - "x64" - ], - "libc": [ - "glibc" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "linux" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-linuxmusl-arm64": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-arm64/-/sharp-libvips-linuxmusl-arm64-1.2.4.tgz", - "integrity": "sha512-FVQHuwx1IIuNow9QAbYUzJ+En8KcVm9Lk5+uGUQJHaZmMECZmOlix9HnH7n1TRkXMS0pGxIJokIVB9SuqZGGXw==", - "cpu": [ - "arm64" - ], - "libc": [ - "musl" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "linux" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-libvips-linuxmusl-x64": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-x64/-/sharp-libvips-linuxmusl-x64-1.2.4.tgz", - "integrity": "sha512-+LpyBk7L44ZIXwz/VYfglaX/okxezESc6UxDSoyo2Ks6Jxc4Y7sGjpgU9s4PMgqgjj1gZCylTieNamqA1MF7Dg==", - "cpu": [ - "x64" - ], - "libc": [ - "musl" - ], - "license": "LGPL-3.0-or-later", - "optional": true, - "os": [ - "linux" - ], - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-linux-arm": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm/-/sharp-linux-arm-0.34.5.tgz", - "integrity": "sha512-9dLqsvwtg1uuXBGZKsxem9595+ujv0sJ6Vi8wcTANSFpwV/GONat5eCkzQo/1O6zRIkh0m/8+5BjrRr7jDUSZw==", - "cpu": [ - "arm" - ], - "libc": [ - "glibc" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-linux-arm": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-linux-arm64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm64/-/sharp-linux-arm64-0.34.5.tgz", - "integrity": "sha512-bKQzaJRY/bkPOXyKx5EVup7qkaojECG6NLYswgktOZjaXecSAeCWiZwwiFf3/Y+O1HrauiE3FVsGxFg8c24rZg==", - "cpu": [ - "arm64" - ], - "libc": [ - "glibc" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-linux-arm64": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-linux-ppc64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-linux-ppc64/-/sharp-linux-ppc64-0.34.5.tgz", - "integrity": "sha512-7zznwNaqW6YtsfrGGDA6BRkISKAAE1Jo0QdpNYXNMHu2+0dTrPflTLNkpc8l7MUP5M16ZJcUvysVWWrMefZquA==", - "cpu": [ - "ppc64" - ], - "libc": [ - "glibc" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-linux-ppc64": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-linux-riscv64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-linux-riscv64/-/sharp-linux-riscv64-0.34.5.tgz", - "integrity": "sha512-51gJuLPTKa7piYPaVs8GmByo7/U7/7TZOq+cnXJIHZKavIRHAP77e3N2HEl3dgiqdD/w0yUfiJnII77PuDDFdw==", - "cpu": [ - "riscv64" - ], - "libc": [ - "glibc" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-linux-riscv64": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-linux-s390x": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-linux-s390x/-/sharp-linux-s390x-0.34.5.tgz", - "integrity": "sha512-nQtCk0PdKfho3eC5MrbQoigJ2gd1CgddUMkabUj+rBevs8tZ2cULOx46E7oyX+04WGfABgIwmMC0VqieTiR4jg==", - "cpu": [ - "s390x" - ], - "libc": [ - "glibc" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-linux-s390x": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-linux-x64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-linux-x64/-/sharp-linux-x64-0.34.5.tgz", - "integrity": "sha512-MEzd8HPKxVxVenwAa+JRPwEC7QFjoPWuS5NZnBt6B3pu7EG2Ge0id1oLHZpPJdn3OQK+BQDiw9zStiHBTJQQQQ==", - "cpu": [ - "x64" - ], - "libc": [ - "glibc" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-linux-x64": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-linuxmusl-arm64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-arm64/-/sharp-linuxmusl-arm64-0.34.5.tgz", - "integrity": "sha512-fprJR6GtRsMt6Kyfq44IsChVZeGN97gTD331weR1ex1c1rypDEABN6Tm2xa1wE6lYb5DdEnk03NZPqA7Id21yg==", - "cpu": [ - "arm64" - ], - "libc": [ - "musl" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-linuxmusl-arm64": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-linuxmusl-x64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-x64/-/sharp-linuxmusl-x64-0.34.5.tgz", - "integrity": "sha512-Jg8wNT1MUzIvhBFxViqrEhWDGzqymo3sV7z7ZsaWbZNDLXRJZoRGrjulp60YYtV4wfY8VIKcWidjojlLcWrd8Q==", - "cpu": [ - "x64" - ], - "libc": [ - "musl" - ], - "license": "Apache-2.0", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-libvips-linuxmusl-x64": "1.2.4" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-wasm32": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-wasm32/-/sharp-wasm32-0.34.5.tgz", - "integrity": "sha512-OdWTEiVkY2PHwqkbBI8frFxQQFekHaSSkUIJkwzclWZe64O1X4UlUjqqqLaPbUpMOQk6FBu/HtlGXNblIs0huw==", - "cpu": [ - "wasm32" - ], - "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", - "optional": true, - "dependencies": { - "@emnapi/runtime": "^1.7.0" - }, - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-win32-arm64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-win32-arm64/-/sharp-win32-arm64-0.34.5.tgz", - "integrity": "sha512-WQ3AgWCWYSb2yt+IG8mnC6Jdk9Whs7O0gxphblsLvdhSpSTtmu69ZG1Gkb6NuvxsNACwiPV6cNSZNzt0KPsw7g==", - "cpu": [ - "arm64" - ], - "license": "Apache-2.0 AND LGPL-3.0-or-later", - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-win32-ia32": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-win32-ia32/-/sharp-win32-ia32-0.34.5.tgz", - "integrity": "sha512-FV9m/7NmeCmSHDD5j4+4pNI8Cp3aW+JvLoXcTUo0IqyjSfAZJ8dIUmijx1qaJsIiU+Hosw6xM5KijAWRJCSgNg==", - "cpu": [ - "ia32" - ], - "license": "Apache-2.0 AND LGPL-3.0-or-later", - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/@img/sharp-win32-x64": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/@img/sharp-win32-x64/-/sharp-win32-x64-0.34.5.tgz", - "integrity": "sha512-+29YMsqY2/9eFEiW93eqWnuLcWcufowXewwSNIT6UwZdUUCrM3oFjMWH/Z6/TMmb4hlFenmfAVbpWeup2jryCw==", - "cpu": [ - "x64" - ], - "license": "Apache-2.0 AND LGPL-3.0-or-later", - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - } - }, - "node_modules/@huggingface/transformers/node_modules/sharp": { - "version": "0.34.5", - "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.34.5.tgz", - "integrity": "sha512-Ou9I5Ft9WNcCbXrU9cMgPBcCK8LiwLqcbywW3t4oDV37n1pzpuNLsYiAV8eODnjbtQlSDwZ2cUEeQz4E54Hltg==", - "hasInstallScript": true, - "license": "Apache-2.0", - "dependencies": { - "@img/colour": "^1.0.0", - "detect-libc": "^2.1.2", - "semver": "^7.7.3" - }, - "engines": { - "node": "^18.17.0 || ^20.3.0 || >=21.0.0" - }, - "funding": { - "url": "https://opencollective.com/libvips" - }, - "optionalDependencies": { - "@img/sharp-darwin-arm64": "0.34.5", - "@img/sharp-darwin-x64": "0.34.5", - "@img/sharp-libvips-darwin-arm64": "1.2.4", - "@img/sharp-libvips-darwin-x64": "1.2.4", - "@img/sharp-libvips-linux-arm": "1.2.4", - "@img/sharp-libvips-linux-arm64": "1.2.4", - "@img/sharp-libvips-linux-ppc64": "1.2.4", - "@img/sharp-libvips-linux-riscv64": "1.2.4", - "@img/sharp-libvips-linux-s390x": "1.2.4", - "@img/sharp-libvips-linux-x64": "1.2.4", - "@img/sharp-libvips-linuxmusl-arm64": "1.2.4", - "@img/sharp-libvips-linuxmusl-x64": "1.2.4", - "@img/sharp-linux-arm": "0.34.5", - "@img/sharp-linux-arm64": "0.34.5", - "@img/sharp-linux-ppc64": "0.34.5", - "@img/sharp-linux-riscv64": "0.34.5", - "@img/sharp-linux-s390x": "0.34.5", - "@img/sharp-linux-x64": "0.34.5", - "@img/sharp-linuxmusl-arm64": "0.34.5", - "@img/sharp-linuxmusl-x64": "0.34.5", - "@img/sharp-wasm32": "0.34.5", - "@img/sharp-win32-arm64": "0.34.5", - "@img/sharp-win32-ia32": "0.34.5", - "@img/sharp-win32-x64": "0.34.5" - } - }, "node_modules/@humanfs/core": { "version": "0.19.2", "resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.2.tgz", @@ -2895,6 +2318,7 @@ "resolved": "https://registry.npmjs.org/@img/colour/-/colour-1.1.0.tgz", "integrity": "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ==", "license": "MIT", + "optional": true, "engines": { "node": ">=18" } @@ -7355,15 +6779,6 @@ "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0" } }, - "node_modules/adm-zip": { - "version": "0.5.18", - "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.18.tgz", - "integrity": "sha512-ufJnssQGbxzLNS1Ho9bCtX4rQKCCvoVuDLHoJyc3F9dOGDB4BkWs2Ci0kv53lqocAEQ/Cbi+I2XCsNYGqVYqng==", - "license": "MIT", - "engines": { - "node": ">=12.0" - } - }, "node_modules/agent-base": { "version": "7.1.4", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.4.tgz", @@ -7987,7 +7402,9 @@ "resolved": "https://registry.npmjs.org/boolean/-/boolean-3.2.0.tgz", "integrity": "sha512-d0II/GO9uf9lfUHH2BQsjxzRJZBdsjgsBiW4BvhWk/3qoKwQFjIDVN19PfX8F2D/r9PCMTtLWjYVCFrpeYUzsw==", "deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.", - "license": "MIT" + "dev": true, + "license": "MIT", + "optional": true }, "node_modules/bowser": { "version": "2.14.1", @@ -9335,6 +8752,7 @@ "version": "1.1.4", "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz", "integrity": "sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==", + "dev": true, "license": "MIT", "dependencies": { "es-define-property": "^1.0.0", @@ -9352,6 +8770,7 @@ "version": "1.2.1", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.2.1.tgz", "integrity": "sha512-8QmQKqEASLd5nx0U1B1okLElbUuuttJ/AnYmRXbbbGDWh6uS208EjD4Xqq/I9wK7u0v6O08XhTWnt5XtEbR6Dg==", + "dev": true, "license": "MIT", "dependencies": { "define-data-property": "^1.0.1", @@ -9397,6 +8816,7 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "devOptional": true, "license": "Apache-2.0", "engines": { "node": ">=8" @@ -9406,7 +8826,9 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.1.0.tgz", "integrity": "sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==", - "license": "MIT" + "dev": true, + "license": "MIT", + "optional": true }, "node_modules/detect-node-es": { "version": "1.1.0", @@ -10118,6 +9540,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "dev": true, "license": "MIT", "engines": { "node": ">= 0.4" @@ -10127,6 +9550,7 @@ "version": "1.3.0", "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "dev": true, "license": "MIT", "engines": { "node": ">= 0.4" @@ -10211,7 +9635,9 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/es6-error/-/es6-error-4.1.1.tgz", "integrity": "sha512-Um/+FxMr9CISWh0bi5Zv0iOD+4cFh5qLeks1qhAopKVAJw3drgKbKySikp7wGhDL0HPeaja0P5ULZrxLkniUVg==", - "license": "MIT" + "dev": true, + "license": "MIT", + "optional": true }, "node_modules/esbuild": { "version": "0.28.2", @@ -10947,12 +10373,6 @@ "node": ">=16" } }, - "node_modules/flatbuffers": { - "version": "25.9.23", - "resolved": "https://registry.npmjs.org/flatbuffers/-/flatbuffers-25.9.23.tgz", - "integrity": "sha512-MI1qs7Lo4Syw0EOzUl0xjs2lsoeqFku44KpngfIduHBYvzm8h2+7K8YMQh1JtVVVrUvhLpNwqVi4DERegUJhPQ==", - "license": "Apache-2.0" - }, "node_modules/flatted": { "version": "3.4.4", "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.4.4.tgz", @@ -11333,7 +10753,9 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/global-agent/-/global-agent-3.0.0.tgz", "integrity": "sha512-PT6XReJ+D07JvGoxQMkT6qji/jVNfX/h364XHZOWeRzy64sSFr+xJ5OX7LI3b4MPQzdL4H8Y8M0xzPpsVMwA8Q==", + "dev": true, "license": "BSD-3-Clause", + "optional": true, "dependencies": { "boolean": "^3.0.1", "es6-error": "^4.1.1", @@ -11363,6 +10785,7 @@ "version": "1.0.4", "resolved": "https://registry.npmjs.org/globalthis/-/globalthis-1.0.4.tgz", "integrity": "sha512-DpLKbNU4WylpxJykQujfCcwYWiV/Jhm50Goo0wrVILAv5jOr9d+H+UR3PhSCD2rCCEIg0uc+G+muBTwD54JhDQ==", + "dev": true, "license": "MIT", "dependencies": { "define-properties": "^1.2.1", @@ -11405,6 +10828,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "dev": true, "license": "MIT", "engines": { "node": ">= 0.4" @@ -11446,12 +10870,6 @@ "dev": true, "license": "ISC" }, - "node_modules/guid-typescript": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/guid-typescript/-/guid-typescript-1.0.9.tgz", - "integrity": "sha512-Y8T4vYhEfwJOTbouREvG+3XDsjr8E3kIr7uf+JZ0BYloFsttiHU0WfvANVsR7TxNUJa/WpCnw/Ino/p+DeBhBQ==", - "license": "ISC" - }, "node_modules/hachure-fill": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/hachure-fill/-/hachure-fill-0.5.2.tgz", @@ -11485,6 +10903,7 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.2.tgz", "integrity": "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==", + "dev": true, "license": "MIT", "dependencies": { "es-define-property": "^1.0.0" @@ -12708,7 +12127,9 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", "integrity": "sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==", - "license": "ISC" + "dev": true, + "license": "ISC", + "optional": true }, "node_modules/json5": { "version": "1.0.2", @@ -13229,7 +12650,9 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/matcher/-/matcher-3.0.0.tgz", "integrity": "sha512-OkeDaAZ/bQCxeFAozM55PKcKU0yJMPGifLwV4Qgjitu+5MoAfSQN4lsLJeXZ1b8w0x+/Emda6MZgXS1jvsapng==", + "dev": true, "license": "MIT", + "optional": true, "dependencies": { "escape-string-regexp": "^4.0.0" }, @@ -13241,7 +12664,9 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", + "dev": true, "license": "MIT", + "optional": true, "engines": { "node": ">=10" }, @@ -14870,6 +14295,7 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", + "dev": true, "license": "MIT", "engines": { "node": ">= 0.4" @@ -14992,49 +14418,6 @@ "regex-recursion": "^6.0.2" } }, - "node_modules/onnxruntime-common": { - "version": "1.24.3", - "resolved": "https://registry.npmjs.org/onnxruntime-common/-/onnxruntime-common-1.24.3.tgz", - "integrity": "sha512-GeuPZO6U/LBJXvwdaqHbuUmoXiEdeCjWi/EG7Y1HNnDwJYuk6WUbNXpF6luSUY8yASul3cmUlLGrCCL1ZgVXqA==", - "license": "MIT" - }, - "node_modules/onnxruntime-node": { - "version": "1.24.3", - "resolved": "https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-1.24.3.tgz", - "integrity": "sha512-JH7+czbc8ALA819vlTgcV+Q214/+VjGeBHDjX81+ZCD0PCVCIFGFNtT0V4sXG/1JXypKPgScQcB3ij/hk3YnTg==", - "hasInstallScript": true, - "license": "MIT", - "os": [ - "win32", - "darwin", - "linux" - ], - "dependencies": { - "adm-zip": "^0.5.16", - "global-agent": "^3.0.0", - "onnxruntime-common": "1.24.3" - } - }, - "node_modules/onnxruntime-web": { - "version": "1.26.0-dev.20260416-b7804b056c", - "resolved": "https://registry.npmjs.org/onnxruntime-web/-/onnxruntime-web-1.26.0-dev.20260416-b7804b056c.tgz", - "integrity": "sha512-MD6Ss4GSpQBo6zqoJzyT9LRbKYs7x/JVN23FT24EcEvlqF4VuzPOeH6X38orZPKHQDbprn7K+SBpu0/mj2CQiw==", - "license": "MIT", - "dependencies": { - "flatbuffers": "^25.1.24", - "guid-typescript": "^1.0.9", - "long": "^5.2.3", - "onnxruntime-common": "1.24.0-dev.20251116-b39e144322", - "platform": "^1.3.6", - "protobufjs": "^7.2.4" - } - }, - "node_modules/onnxruntime-web/node_modules/onnxruntime-common": { - "version": "1.24.0-dev.20251116-b39e144322", - "resolved": "https://registry.npmjs.org/onnxruntime-common/-/onnxruntime-common-1.24.0-dev.20251116-b39e144322.tgz", - "integrity": "sha512-BOoomdHYmNRL5r4iQ4bMvsl2t0/hzVQ3OM3PHD0gxeXu1PmggqBv3puZicEUVOA3AtHHYmqZtjMj9FOfGrATTw==", - "license": "MIT" - }, "node_modules/openai": { "version": "6.40.0", "resolved": "https://registry.npmjs.org/openai/-/openai-6.40.0.tgz", @@ -15336,12 +14719,6 @@ "url": "https://paulmillr.com/funding/" } }, - "node_modules/platform": { - "version": "1.3.6", - "resolved": "https://registry.npmjs.org/platform/-/platform-1.3.6.tgz", - "integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==", - "license": "MIT" - }, "node_modules/plist": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/plist/-/plist-3.1.0.tgz", @@ -16334,7 +15711,9 @@ "version": "2.15.4", "resolved": "https://registry.npmjs.org/roarr/-/roarr-2.15.4.tgz", "integrity": "sha512-CHhPh+UNHD2GTXNYhPWLnU8ONHdI+5DI+4EYIAOaiD63rHeYlZvyh8P+in5999TTSFgUYuKUAjzRI4mdh/p+2A==", + "dev": true, "license": "BSD-3-Clause", + "optional": true, "dependencies": { "boolean": "^3.0.1", "detect-node": "^2.0.4", @@ -16516,6 +15895,7 @@ "version": "7.8.5", "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz", "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==", + "devOptional": true, "license": "ISC", "bin": { "semver": "bin/semver.js" @@ -16528,13 +15908,17 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/semver-compare/-/semver-compare-1.0.0.tgz", "integrity": "sha512-YM3/ITh2MJ5MtzaM429anh+x2jiLVjqILF4m4oyQB18W7Ggea7BfqdH/wGMK7dDiMghv/6WG7znWMwUDzJiXow==", - "license": "MIT" + "dev": true, + "license": "MIT", + "optional": true }, "node_modules/serialize-error": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/serialize-error/-/serialize-error-7.0.1.tgz", "integrity": "sha512-8I8TjW5KMOKsZQTvoxjuSIa7foAwPWGOts+6o7sgjz41/qMD9VQHEDxi6PBvK2l0MXUmqZyNpUK+T2tQaaElvw==", + "dev": true, "license": "MIT", + "optional": true, "dependencies": { "type-fest": "^0.13.1" }, @@ -16820,7 +16204,9 @@ "version": "1.1.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.3.tgz", "integrity": "sha512-Oo+0REFV59/rz3gfJNKQiBlwfHaSESl1pcGyABQsnnIfWOFt6JNj5gCog2U6MLZ//IGYD+nA8nI+mTShREReaA==", - "license": "BSD-3-Clause" + "dev": true, + "license": "BSD-3-Clause", + "optional": true }, "node_modules/stable-hash-x": { "version": "0.2.0", @@ -17510,7 +16896,9 @@ "version": "0.13.1", "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.13.1.tgz", "integrity": "sha512-34R7HTnG0XIJcBSn5XhDd7nNFPRcXYRZrBB2O2jdKqYODldSzBAqzsWoZYYvduky73toYS/ESqxPvkDf/F0XMg==", + "dev": true, "license": "(MIT OR CC0-1.0)", + "optional": true, "engines": { "node": ">=10" }, diff --git a/package.json b/package.json index 20406e6..3dad78d 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ "@ai-sdk/react": "^4.0.79", "@earendil-works/pi-agent-core": "^0.85.1", "@earendil-works/pi-ai": "^0.85.1", - "@huggingface/transformers": "^4.2.0", "@mozilla/readability": "^0.6.0", "@radix-ui/react-use-controllable-state": "^1.2.6", "@rive-app/react-webgl2": "^4.32.1", diff --git a/scripts/embed-graph.ts b/scripts/embed-graph.ts index 04514a0..60f7a9e 100644 --- a/scripts/embed-graph.ts +++ b/scripts/embed-graph.ts @@ -1,20 +1,35 @@ /** - * Embed the whole graph (backfill / cold start): one-time pass that - * fills the embedding ledger for every node — the same embedNewNodes the - * post-merge hook runs, just with an empty (or stale-model) ledger so - * every node is "new". First run downloads the model weights (~300MB) - * to the local HF cache; subsequent runs embed only what's missing. + * Embed the whole graph (backfill / cold start / model change): one-time + * pass that fills the embedding ledger for every node — the same + * embedNewNodes the post-merge hook runs, just with an empty (or + * stale-model) ledger so every node is "new". + * + * BYOK (wave 6, 2026-09-23): vectors come from the configured provider + * (graph.embeddingModel, keys from the auth store) — this script resolves + * the live state up front so an unconfigured machine says so before + * anything bills. A model CHANGE re-embeds everything (ledger staleness). * * npx tsx scripts/embed-graph.ts */ +import { embedNewNodes, embeddingPort } from "../src/server/embeddings.ts"; import { graphStore } from "../src/store/graph.ts"; -import { embedNewNodes, EMBEDDING_MODEL, EMBEDDING_DIM } from "../src/server/embeddings.ts"; async function main() { + const resolved = embeddingPort(); + if (!resolved.ok) { + console.error(`[embed] cannot run — ${resolved.reason}`); + process.exit(1); + } const t0 = Date.now(); - console.log(`[embed] model: ${EMBEDDING_MODEL} (dim ${EMBEDDING_DIM}, q8)`); - const { embedded, total } = await embedNewNodes(graphStore); - console.log(`[embed] embedded ${embedded} nodes → ledger holds ${total} vectors (${((Date.now() - t0) / 1000).toFixed(1)}s)`); + console.log(`[embed] model: ${resolved.port.model} (provider embeddings, batch 32)`); + const { embedded, total, usage } = await embedNewNodes(graphStore); + console.log( + `[embed] embedded ${embedded} nodes → ledger holds ${total} vectors (${((Date.now() - t0) / 1000).toFixed(1)}s)`, + ); + if (usage) { + const tokens = usage.steps.reduce((sum, s) => sum + (s.inputTokens ?? 0), 0); + console.log(`[embed] embedding spend: ${tokens} prompt tokens over ${usage.steps.length} call(s)`); + } } main().catch((err) => { diff --git a/src/agent/subagents/extraction.ts b/src/agent/subagents/extraction.ts index 9beea12..84bef25 100644 --- a/src/agent/subagents/extraction.ts +++ b/src/agent/subagents/extraction.ts @@ -505,8 +505,14 @@ export interface RunningExtraction { phase: ExtractionPhase; startedAt: number; /** Live per-stage usage, accumulated as steps bill (the dock's cost / - * context-window blobs read this through /api/graph/status). */ - usage: { extractor?: ExtractionStageUsage; auditor?: ExtractionStageUsage }; + * context-window blobs read this through /api/graph/status). Wave 6: + * the provider-embedding tail became a billable stage — recorded from + * the embedder's reported usage when present. */ + usage: { + extractor?: ExtractionStageUsage; + auditor?: ExtractionStageUsage; + embedding?: ExtractionStageUsage; + }; /** True-cancellation handle (the dock's abort button): one controller per * run, shared across phases; abort() cancels the in-flight model call. */ controller: AbortController; @@ -574,9 +580,12 @@ export interface ExtractionModels { export interface ExtractionPipelineDeps extends GraphDeps, ExtractionModels { /** The post-merge embedding batch (semantic clusters run on it). * INJECTED by the composition root (the agent layer never imports - * the server layer — architecture law): production passes the real - * local-model embedder (server/embeddings.ts embedNewNodes), tests - * fake it. Failure inside it is non-fatal by contract. */ + * the server layer — architecture law): production passes the + * provider-embedder batch (server/embeddings.ts embedNewNodes — BYOK + * wave 6; null model = it skips itself with an info log), tests fake + * it. Failure inside it is non-fatal by contract. When the result + * carries a `usage` stage (the EmbedBatchResult contract), it is + * recorded into the run's money-channel capture. */ embedder: (graphStore: GraphStore) => Promise; } @@ -664,6 +673,27 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti run.usage[stage]!.steps.push(usage); } + /** The embedding tail's failure log (non-fatal by contract). */ + function recordEmbedFailure(chatId: string): (err: unknown) => undefined { + return (err) => { + console.error(`[extraction ${chatId}] embedding failed (non-fatal):`, err); + return undefined; + }; + } + + /** Wave 6: fold the provider-embedding tail's reported usage into the + * run's capture — one ExtractionStageUsage per run (the embedder + * already aggregated its API calls into steps). Absent/skipped/failed + * embeds contribute nothing (a run that embedded nothing stays + * two-stage, like the whole local-embedder era). */ + function captureEmbedUsage(chatId: string, result: unknown): void { + const stage = (result as { usage?: ExtractionStageUsage } | undefined)?.usage; + if (!stage) return; + const run = runningExtractions.get(chatId); + if (!run) return; + run.usage.embedding = stage; + } + async function extract(chatId: string, view: string): Promise { const signal = runningExtractions.get(chatId)?.controller.signal; @@ -739,6 +769,7 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti stages: { extractor: run.usage.extractor ?? { model: "unknown", steps: [] }, auditor: run.usage.auditor ?? { model: "unknown", steps: [] }, + ...(run.usage.embedding ? { embedding: run.usage.embedding } : {}), }, }; if (run.kind === "notes") graphStore.appendSourceUsage(chatId, entry); @@ -830,9 +861,7 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti // MEANING, so embed the new propositions BEFORE the settle and // the namer look at clusters. Failure is non-fatal — clustering // runs without the new nodes until the next merge retries. - await embed(graphStore).catch((err) => { - console.error(`[extraction ${chatId}] embedding failed (non-fatal):`, err); - }); + await captureEmbedUsage(chatId, await embed(graphStore).catch(recordEmbedFailure(chatId))); // Option A: the layout ledger evolves with the merge (sync — the // next snapshot fetch must already reflect the new nodes). settleGraphLayout(graphStore); @@ -963,9 +992,7 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti // (topics from meaning), then the settle, then the namer — // under their own phase, exactly like the chat path. setPhase(id, "embedding", "notes", input.filePath); - await embed(graphStore).catch((err) => { - console.error(`[extraction ${id}] embedding failed (non-fatal):`, err); - }); + await captureEmbedUsage(id, await embed(graphStore).catch(recordEmbedFailure(id))); // Option A: same post-merge settle as the chat path. settleGraphLayout(graphStore); persistRunUsage(id, Math.round(performance.now() - t0), stats); diff --git a/src/agent/subagents/graph-context.ts b/src/agent/subagents/graph-context.ts index 034d41c..d3cef03 100644 --- a/src/agent/subagents/graph-context.ts +++ b/src/agent/subagents/graph-context.ts @@ -12,9 +12,10 @@ * quiz/ask calls, so delivery lands mid-probing, before plan_init — where * prior knowledge matters most. * - * No model call, no usage: the job's only cost is one embedding pass over - * the warm singleton (tens–~200ms) plus a brute-force cosine scan. It still - * carries a JobRecord (kind "graph-context") so the dock shows it, and it + * No teacher-model call: the job's only model spend is ONE embedding + * query (the local embedder was free, provider ones are not — wave 6 + * records its prompt tokens on the job record). It still carries a + * JobRecord (kind "graph-context") so the dock shows it, and it * registers its AbortController so the dock's END kills it — but an abort * steers NOTHING: nothing was ever promised in the lesson (the injection is * server-initiated scaffolding, unlike a teacher-dispatched sub-agent). @@ -27,6 +28,7 @@ */ import { + accumulateJobUsage, buildGraphContextSteer, steerDelivered, type JobRecord, @@ -41,20 +43,25 @@ import type { UIMessage } from "ai"; import { registerJobAbort, unregisterJobAbort } from "./aborts.ts"; import type { ChatStore, GraphStore, JobStore } from "../../core/ports.ts"; -/** The embedder as the runner needs it: the model/dim the ledger must - * match (parity: one vector space) + the query seam. Injected so tests - * run without the transformer model. */ +/** The embedder as the runner needs it (BYOK wave 6): the model slug the + * ledger must match (parity: one vector space) + the query seam. + * Resolved FRESH per run by the composition root (deps.embedder) so a + * settings PUT or a key save between dispatch and spawn lands; null + * model = no embedding configured — the run skips honestly. The query + * carries its prompt-token count so the job records its spend (the + * local embedder was free; provider ones are not). */ export interface GraphContextEmbedder { - model: string; - dim: number; - embedQuery: (text: string) => Promise; + model: string | null; + embedQuery: (text: string) => Promise<{ vector: number[]; promptTokens?: number }>; } export interface GraphContextDeps { chatStore: Pick; jobStore: Pick; graphStore: Pick; - embedder: GraphContextEmbedder; + /** The embedder RESOLVER (wave 6): called once at run start — fresh + * setting + key reads each run. null = no embedding model configured. */ + embedder: () => GraphContextEmbedder | null; /** The wake seam — jobs.ts deliverJobResult (resolves on delivery). */ deliver: (steer: SteerMessage) => Promise; } @@ -126,7 +133,7 @@ export function startGraphContextInjection(deps: GraphContextDeps, chatId: strin * crash-recovery respawn seam: a job record orphaned "running" by a * process crash re-runs from here (the record is the only input needed). */ export async function runGraphContextJob(deps: GraphContextDeps, jobId: string): Promise { - const { chatStore, jobStore, graphStore, embedder, deliver } = deps; + const { chatStore, jobStore, graphStore, deliver } = deps; const job = jobStore.getJob(jobId); if (!job || job.status !== "running") return; @@ -135,6 +142,13 @@ export async function runGraphContextJob(deps: GraphContextDeps, jobId: string): }; const chat = chatStore.getChat(job.chatId); if (!chat) return fail(`chat not found: ${job.chatId}`); + // BYOK (wave 6): fresh resolution per run; null = honestly off (the + // dispatch gate already skips unconfigured sessions — this only fires + // on a config change racing a respawn, and the error says exactly why). + const embedder = deps.embedder(); + if (!embedder || embedder.model === null) { + return fail("no embedding model configured — injection skipped (graph.embeddingModel is null)"); + } const topic = topicOf(chat.messages); if (topic === null) return fail("no injectable topic (session past its start)"); const budget = effectiveContextBudget(chat.settings); @@ -146,19 +160,39 @@ export async function runGraphContextJob(deps: GraphContextDeps, jobId: string): try { jobStore.updateJob(jobId, { activity: "embedding topic" }); const ledger = graphStore.readEmbeddings(); - if (ledger.model !== embedder.model || ledger.dim !== embedder.dim) { - return fail("embeddings ledger is stale (model/dim mismatch) — injection skipped"); + // Staleness is the MODEL identity (wave 6): a given model serves one + // dim by default, so model parity is space parity; the dim is checked + // defensively against the served vector below. + if (ledger.model !== embedder.model) { + return fail("embeddings ledger is stale (model mismatch) — injection skipped"); } if (Object.keys(ledger.vectors).length === 0) { return fail("the knowledge graph has no embeddings yet — nothing to inject"); } const query = await embedder.embedQuery(topic); if (controller.signal.aborted) return; + // The defensive dim half of the old parity check: a provider-side dim + // change (rare) must fail the query, never silently mismatch. + if (query.vector.length !== ledger.dim) { + return fail( + `embedding dim mismatch: query is ${query.vector.length}, ledger holds ${ledger.dim}`, + ); + } + // Money channel (wave 6): the one embedding call's prompt tokens fold + // into the job record — priced via the EMBEDDING_PRICES overlay. + if (query.promptTokens !== undefined) { + jobStore.updateJob(jobId, { + usage: accumulateJobUsage(job.usage, embedder.model, { + inputTokens: query.promptTokens, + outputTokens: 0, + }), + }); + } jobStore.updateJob(jobId, { activity: "selecting propositions" }); const data = selectGraphContext({ chatId: job.chatId, - query, + query: query.vector, vectors: ledger.vectors, nodes: graphStore.readNodes(), events: graphStore.readEvents(), diff --git a/src/agent/subagents/headless.ts b/src/agent/subagents/headless.ts index 4652f6b..b94ba55 100644 --- a/src/agent/subagents/headless.ts +++ b/src/agent/subagents/headless.ts @@ -205,11 +205,13 @@ function svgViewport(svg: string, opts: RenderSvgOptions): { width: number; heig * are an arbitrary SPLIT of one zlib stream, so merging them is the more * standard shape, byte-identical stream, zero re-encoding. * - * Pure Node on purpose (no sharp): the process already loads transformers' - * nested sharp + libvips, and importing the root sharp (Next's optional - * dependency) put a SECOND libvips into the process — macOS's ObjC runtime - * warns (duplicate GNotificationCenterDelegate, "mysterious crashes"). - * One native image lib per process; app code carries none. */ + * Pure Node on purpose (no sharp): historically the process already carried + * a native image lib via @huggingface/transformers' nested sharp + libvips, + * and importing the root sharp (Next's optional dependency) put a SECOND + * libvips into the process — macOS's ObjC runtime warns (duplicate + * GNotificationCenterDelegate, "mysterious crashes"). The transformers + * embedder died with wave 6 (2026-09-23) but the rule lives on: one native + * image lib per process; app code carries none. */ export function providerSafePng(png: Buffer): Buffer { const SIG_LEN = 8; if (png.length < SIG_LEN + 12 || png.subarray(0, SIG_LEN).toString("hex") !== "89504e470d0a1a0a") return png; diff --git a/src/core/contracts/chat.ts b/src/core/contracts/chat.ts index d3f170b..6537a05 100644 --- a/src/core/contracts/chat.ts +++ b/src/core/contracts/chat.ts @@ -108,6 +108,10 @@ export interface ExtractionRunUsage { stages: { extractor: ExtractionStageUsage; auditor: ExtractionStageUsage; + /** Wave 6 (2026-09-23): the provider-embedding tail — present only + * when an embedding model is configured and the run embedded + * (local embeddings were free, so this post-dates them). */ + embedding?: ExtractionStageUsage; }; } diff --git a/src/core/cost-summary.ts b/src/core/cost-summary.ts index 2c29ff5..6270a7a 100644 --- a/src/core/cost-summary.ts +++ b/src/core/cost-summary.ts @@ -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 = [extDollars, audDollars, embDollars]; + const runDollars = allStages.every((d) => d === null) + ? null + : allStages.reduce((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, }; } diff --git a/src/core/ports.ts b/src/core/ports.ts index 683a9e1..93df26f 100644 --- a/src/core/ports.ts +++ b/src/core/ports.ts @@ -243,3 +243,49 @@ export interface AuthStore { /** Remove one VAR if present (absent = no-op, still a silent success). */ deleteAuthVar(name: string): void; } + +// ─── EmbeddingPort — BYOK provider embeddings (wave 6, 2026-09-23) ─────────── + +/** + * The provider-embedding seam (the local transformers embedder died with + * this wave): vectors come from the user's keyed provider — OpenRouter, + * OpenAI, or Google — named by graph.embeddingModel ("provider:id", null = + * honestly off: embedding/clustering/injection skip with an info log). + * + * Owner decisions (2026-09-23) baked in: + * - The adapters are resolved FRESH per call from the setting + the auth + * store (key rotations land immediately — the search port's treatment). + * - Vectors are stored as the provider returns them (default dims, no + * truncation, no normalization); the ledger is self-describing on + * model + dim, and any model change voids it (full re-embed). + * - Usage (prompt tokens) flows back to the money channel: adapters + * report it, the caller records it as a CallUsage step. + * + * Pure types only (browser-safe): the adapters are server-side + * (src/server/embeddings.ts) and arrive as injected dependencies. + */ + +/** Which side of retrieval the text is — providers bill identically but + * quality-optimize differently (OpenRouter's input_type, Gemini's + * taskType); every input in one call shares the task. */ +export type EmbeddingTask = "document" | "query"; + +/** One embedding batch's result: vectors in input order, the model's + * identity + dim (the ledger stamps), and the provider-reported prompt + * token count (undefined = provider didn't report; cost stays honest + * "—" via the existing unpriced path). */ +export interface EmbeddingResult { + vectors: number[][]; + /** The full "provider:id" slug — ledger/cost keys use it verbatim. */ + model: string; + dim: number; + promptTokens?: number; +} + +/** A live provider embedding adapter. `embed` throws on failure (adapters + * map HTTP errors to clear Error messages); callers treat failure as + * non-fatal per the extraction contract. */ +export interface EmbeddingPort { + model: string; + embed(texts: readonly string[], task: EmbeddingTask): Promise; +} diff --git a/src/core/prices.ts b/src/core/prices.ts index 8123082..3babfa4 100644 --- a/src/core/prices.ts +++ b/src/core/prices.ts @@ -2752,6 +2752,30 @@ export const CONTEXT_LIMITS: Record = { // ─── 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 = { + "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; diff --git a/src/core/settings-resolution.ts b/src/core/settings-resolution.ts index c399767..95596b0 100644 --- a/src/core/settings-resolution.ts +++ b/src/core/settings-resolution.ts @@ -201,6 +201,9 @@ export function resolveSettings(record: UserSettings | null): EffectiveSettings embedding: graph.embedding ?? FACTORY_SETTINGS.graph.embedding, clustering: graph.clustering ?? FACTORY_SETTINGS.graph.clustering, labeling: graph.labeling ?? FACTORY_SETTINGS.graph.labeling, + // BYOK (wave 6): null is the factory floor and a VALID resolved + // value — ?? only fills the absent case, null passes through. + embeddingModel: graph.embeddingModel ?? FACTORY_SETTINGS.graph.embeddingModel, }, }; } diff --git a/src/core/settings.ts b/src/core/settings.ts index 6741232..6481f25 100644 --- a/src/core/settings.ts +++ b/src/core/settings.ts @@ -251,6 +251,13 @@ export interface GraphSettings { clustering?: boolean; /** The LLM cluster-namer (needs clustering). */ labeling?: boolean; + /** BYOK embeddings (wave 6, 2026-09-23): the provider embedding model + * as a "provider:id" slug (openrouter:…, openai:…, google:…). null = + * no model — embedding/clustering/injection skip honestly (info log, + * graph stays textual); no factory floor exists. Keys live in the auth + * store, resolved fresh per call. A model CHANGE re-embeds everything + * (the ledger's staleness semantics). */ + embeddingModel?: string | null; } /** THE record. Every section optional (delta); unknown top-level keys are @@ -303,7 +310,7 @@ export const FACTORY_SETTINGS: FactorySettings = { sessionNameSerif: true, mdRendering: { headingWeight: 900, boldColor: null, italicColor: null }, }, - graph: { enabled: true, embedding: true, clustering: true, labeling: true }, + graph: { enabled: true, embedding: true, clustering: true, labeling: true, embeddingModel: null }, }; // ─── Sanitizer ───────────────────────────────────────────────────────────── @@ -453,6 +460,14 @@ function sanitizeGraph(raw: Record): GraphSettings { if (clustering !== undefined) out.clustering = clustering; const labeling = boolOrDrop(raw.labeling); if (labeling !== undefined) out.labeling = labeling; + // BYOK embeddings (wave 6): null or a provider-qualified slug; anything + // else (bad type, bad shape) DROPS to absent — an invalid write never + // becomes a live model (the PUT parser 400s it first; this is the + // read-side belt-and-suspenders for hand-edited files). + if (raw.embeddingModel === null) out.embeddingModel = null; + else if (typeof raw.embeddingModel === "string" && /^\S+:\S+$/.test(raw.embeddingModel)) { + out.embeddingModel = raw.embeddingModel; + } return out; } diff --git a/src/server/chat-handler.ts b/src/server/chat-handler.ts index b2508c8..7d5f8ff 100644 --- a/src/server/chat-handler.ts +++ b/src/server/chat-handler.ts @@ -5,7 +5,7 @@ import { piEngineForSpec, type PiEngine } from "../agent/models.ts"; import { createJobRunner } from "../agent/subagents/runner.ts"; import { createExtractionPipeline } from "../agent/subagents/extraction.ts"; import { runGraphContextJob, type GraphContextDeps } from "../agent/subagents/graph-context.ts"; -import { EMBEDDING_DIM, EMBEDDING_MODEL, embedNewNodes, embedQuery } from "./embeddings.ts"; +import { embedNewNodes, embeddingPort } from "./embeddings.ts"; import type { ModelRole } from "../../config.ts"; import { claimSteers, deliverJobResult, setContextRespawn, setJobRespawn, setTurnRunner } from "./jobs.ts"; import { recordToPiMessages } from "./pi/record-adapter.ts"; @@ -63,8 +63,8 @@ const jobRunner = createJobRunner({ * (/api/graph/import) hit the same single-flight registry. */ export const extraction = createExtractionPipeline({ ...stores, - // The REAL embedder (local transformer model): post-merge embedding - // batch — semantic clusters are computed and persisted here. + // The provider-embedder batch (BYOK wave 6): post-merge embedding — + // resolves the model fresh per call; null model = info-skip inside. embedder: embedNewNodes, // BYOK (owner, 2026-09-22): the extraction stages resolve the chain — // role override → slot → NOTHING — read FRESH per stage call; an empty @@ -75,14 +75,27 @@ export const extraction = createExtractionPipeline({ }); /** Graph context injection (vision-context-injection.md): the runner's - * deps — same storage ports, the REAL embedder as the query seam (same - * warm singleton as extraction, so vector-space parity holds by - * construction), and the wake pipeline as the delivery seam. */ + * deps — same storage ports, the provider-embedder RESOLVER as the query + * seam (BYOK wave 6: fresh setting + key reads per run, so vector-space + * parity with the ledger is checked against the CURRENT model; a key or + * settings PUT between dispatch and spawn lands), and the wake pipeline + * as the delivery seam. */ export const graphContext: GraphContextDeps = { chatStore: stores.chatStore, jobStore: stores.jobStore, graphStore: stores.graphStore, - embedder: { model: EMBEDDING_MODEL, dim: EMBEDDING_DIM, embedQuery }, + embedder: () => { + const resolved = embeddingPort(); + if (!resolved.ok) return null; + const port = resolved.port; + return { + model: port.model, + embedQuery: async (text: string) => { + const result = await port.embed([text], "query"); + return { vector: result.vectors[0]!, promptTokens: result.promptTokens }; + }, + }; + }, deliver: deliverJobResult, }; @@ -221,7 +234,10 @@ export async function handleChatRequest( // GRAPH GATES (settings-storage wave, 2026-09-19): the read channel // needs BOTH the master switch and the embedding rung (injection is a // vector query); with graph off there is no context job at all. - if (graphContextEnabled(effective.graph)) { + // BYOK (wave 6): an unconfigured embedding model means no vectors to + // query — the dispatch skips honestly (the runner's own resolution is + // the belt-and-suspenders for respawn races). + if (graphContextEnabled(effective.graph) && effective.graph.embeddingModel !== null) { triggerGraphContext(graphContext, chatId); } diff --git a/src/server/embeddings.ts b/src/server/embeddings.ts index 4f83884..d09fd71 100644 --- a/src/server/embeddings.ts +++ b/src/server/embeddings.ts @@ -1,94 +1,293 @@ /** - * THE EMBEDDER (DECIDED 2026-09-13): semantic topic clustering needs - * vectors, and vectors need a model running ON the user's machine — no - * external services, no API bills, no network dependency for re-embedding. + * BYOK PROVIDER EMBEDDINGS (wave 6, 2026-09-23): the local transformers + * embedder is dead — vectors now come from the user's keyed provider + * (OpenRouter / OpenAI / Google), named by graph.embeddingModel + * ("provider:id", null = honestly off; skip with an info log, the graph + * stays textual — owner ruling 2026-09-23). * - * Runtime: in-process transformers.js (@huggingface/transformers v4 over - * onnxruntime-node — native Apple Silicon acceleration). The model - * weights download ONCE to the local HF cache on first run (~300MB). + * Owner decisions baked in: + * - NO factory model (BYOK, like the model slots): null is the floor. + * - Adapters resolve FRESH per call from the setting + the auth store + * (the search port's treatment — key rotations land immediately); a + * key missing for a SET model is an honest error, never a fallback. + * - Vectors stored as the provider returns them (default dims, no + * truncation, no normalization). The ledger is self-describing on + * model + dim; a model change voids it (full re-embed, minutes at + * current scale — the local embedder's same staleness rule). + * - Usage (prompt tokens) flows to the money channel: every API call + * reports one CallUsage step (outputTokens: 0 — embeddings emit no + * output; priced via the hand-maintained EMBEDDING_PRICES overlay). * - * Model: EmbeddingGemma-300M (q8) — best quality-per-megabyte small - * embedding model as of late 2025 (MTEB-en 68.36), built for on-device - * use, with MRL truncation so we store 512 of its 768 dims. Every input - * uses the model card's document format ("title: none | text: …"), - * consistently — mixing prefixed and unprefixed inputs degrades quality. - * - * Model swaps re-embed EVERYTHING: vectors from different models live in - * incompatible spaces. The ledger records model + dim; a mismatch marks - * the whole ledger stale and this module re-embeds from scratch (minutes - * at current scale). + * The adapters are injected-fetch dependencies so tests never touch the + * network; wire protocols VERIFIED against provider docs 2026-09-23: + * - OpenRouter: POST /api/v1/embeddings (Bearer), input_type + * search_document|search_query; catalog GET /api/v1/embeddings/models + * (VERIFIED live 2026-09-23 — the inverse path /api/v1/models/ + * embeddings 404s). + * - OpenAI: POST /v1/embeddings (Bearer); catalog GET /v1/models. + * - Google: POST /v1beta/models/{m}:batchEmbedContents (x-goog-api-key), + * taskType RETRIEVAL_DOCUMENT|RETRIEVAL_QUERY; catalog GET /v1beta/models + * filtered on supportedGenerationMethods ∋ embedContent. */ -import * as fs from "node:fs"; -import { env, pipeline, type FeatureExtractionPipeline } from "@huggingface/transformers"; -import { PATHS } from "../../config.ts"; -import type { EmbeddingsLedger, GraphNode } from "../core/contracts.ts"; + +import type { CallUsage, EmbeddingsLedger, GraphNode } from "../core/contracts.ts"; +import type { ExtractionStageUsage } from "../core/contracts/chat.ts"; +import type { EmbeddingPort, EmbeddingResult, EmbeddingTask } from "../core/ports.ts"; import { semanticClusters, SEMANTIC_SIM_THRESHOLD } from "../core/semantic-clusters.ts"; +import { keyValue } from "./keys.ts"; +import { effectiveSettings } from "./stores.ts"; -export const EMBEDDING_MODEL = "onnx-community/embeddinggemma-300m-ONNX"; -/** MRL truncation of the native 768 — negligible quality loss, half the - * ledger size (2026-09-13 researcher sweep). */ -export const EMBEDDING_DIM = 512; -/** Batch size bounds peak memory; propositions are tiny so 32 per call - * keeps the pipeline warm without thrashing. */ +/** The wire-callable shape the adapters close over (tests inject a stub). */ +type FetchLike = (input: string, init: RequestInit) => Promise; + +/** One provider embedding API call — kept small enough to never trip + * per-request input limits (propositions are tiny; 32 was the local + * embedder's batch and it stays). */ const EMBED_BATCH = 32; +/** Per-request timeout: a hung provider call must not hang an extraction + * run (the wave-1 hung-job lesson). */ +const REQUEST_TIMEOUT_MS = 60_000; -// Model cache routing (Electron seam, 2026-09-17): the default cacheDir -// lives inside node_modules — read-only inside a packaged .app bundle. -// When LH_DATA_DIR is set the cache follows the data seam (userData); -// in dev it stays at the package default (the model is already there). -if (PATHS.modelCache) { - fs.mkdirSync(PATHS.modelCache, { recursive: true }); - env.cacheDir = PATHS.modelCache; +/** The providers this wave ships + their auth vars (the curated key + * catalog, same ids the auth store and /api/keys use). */ +const PROVIDER_KEYS = { + openrouter: "OPENROUTER_API_KEY", + openai: "OPENAI_API_KEY", + google: "GEMINI_API_KEY", +} as const; +export type EmbeddingProviderId = keyof typeof PROVIDER_KEYS; + +/** "provider:id" → {provider, id}; null when malformed (no guessing). */ +export function parseEmbeddingModelSlug(slug: string): { provider: EmbeddingProviderId; id: string } | null { + const colon = slug.indexOf(":"); + if (colon <= 0 || colon === slug.length - 1) return null; + const provider = slug.slice(0, colon) as EmbeddingProviderId; + if (!(provider in PROVIDER_KEYS)) return null; + return { provider, id: slug.slice(colon + 1) }; } -let pipelinePromise: Promise | null = null; +// ─── Adapters (one per provider; pure injected-fetch factories) ────────────── -function getPipeline(): Promise { - pipelinePromise ??= pipeline("feature-extraction", EMBEDDING_MODEL, { dtype: "q8" }); - return pipelinePromise; +/** Shared: HTTP-error → clear Error (the search adapters' mapping). */ +async function demandOk(resp: Response, label: string): Promise { + if (resp.ok) return; + const body = await resp.text(); + throw new Error(`${label} API ${resp.status}: ${body.slice(0, 200)}`); } -/** EmbeddingGemma's required document format (model card). */ -function embedInput(statement: string): string { - return `title: none | text: ${statement}`; +/** Shared: normalize a provider's {embedding, index} rows to input order. */ +function rowsInOrder( + data: Array<{ embedding?: unknown; index?: unknown }>, + count: number, + label: string, +): number[][] { + const byIndex = new Map(); + for (const row of data) { + if ( + Array.isArray(row.embedding) && + row.embedding.every((x) => typeof x === "number") && + typeof row.index === "number" + ) { + byIndex.set(row.index, row.embedding as number[]); + } + } + const out: number[][] = []; + for (let i = 0; i < count; i++) { + const vec = byIndex.get(i); + if (!vec) throw new Error(`${label} API returned ${byIndex.size}/${count} vectors`); + out.push(vec); + } + return out; } -/** EmbeddingGemma's retrieval-QUERY format (model card) — the topic side of - * graph context injection (vision-context-injection.md). Role-asymmetric - * BY DESIGN: queries and documents use different trained templates; the - * requirement is same model + same truncation (one vector space), each - * side in the format the model was trained for. */ -function queryInput(text: string): string { - return `task: search result | query: ${text}`; +/** One API call's money-channel step: embeddings emit no output, cache + * nothing, reason never — the fields exist (CallUsage requires them) as + * absent-is-not-zero undefineds. */ +function embeddingStep(promptTokens: number): CallUsage { + return { + inputTokens: promptTokens, + outputTokens: 0, + cacheReadTokens: undefined, + cacheWriteTokens: undefined, + reasoningTokens: undefined, + }; } -/** Embed one retrieval query (the session's topic) — same warm singleton, - * same 512-dim MRL truncation as the ledger. The one embedding call the - * injection job makes; tens–~200ms warm. */ -export async function embedQuery(text: string): Promise { - const extractor = await getPipeline(); - const output = await extractor([queryInput(text)], { pooling: "mean", normalize: true }); - return truncateNormalize((output.tolist() as number[][])[0]!); +/** OpenRouter: POST https://openrouter.ai/api/v1/embeddings — Bearer auth, + * input_type hints the retrieval side (verified against the docs 2026-09-23). */ +export function openrouterEmbeddingProvider(model: string, apiKey: string, fetchFn: FetchLike = fetch): EmbeddingPort { + const parsed = parseEmbeddingModelSlug(model); + if (!parsed) throw new Error(`invalid embedding model slug: ${model}`); + return { + model, + async embed(texts: readonly string[], task: EmbeddingTask): Promise { + const resp = await fetchFn("https://openrouter.ai/api/v1/embeddings", { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` }, + body: JSON.stringify({ + model: parsed.id, + input: texts, + input_type: task === "query" ? "search_query" : "search_document", + }), + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), + }); + await demandOk(resp, "OpenRouter embeddings"); + const data = (await resp.json()) as { + data?: Array<{ embedding?: unknown; index?: unknown }>; + usage?: { prompt_tokens?: number }; + }; + return { + vectors: rowsInOrder(data.data ?? [], texts.length, "OpenRouter embeddings"), + model, + dim: 0, // filled by the caller from the first vector (below) + promptTokens: data.usage?.prompt_tokens, + }; + }, + }; } -/** Truncate an L2-normalized vector to EMBEDDING_DIM (MRL: leading dims - * carry most of the signal) and re-normalize so cosine = dot product. */ -function truncateNormalize(vec: readonly number[]): number[] { - const out = vec.slice(0, EMBEDDING_DIM); - let mag = 0; - for (const x of out) mag += x * x; - mag = Math.sqrt(mag); - return mag > 0 ? out.map((x) => x / mag) : out; +/** OpenAI: POST https://api.openai.com/v1/embeddings — Bearer auth; the + * response's usage carries prompt_tokens (verified 2026-09-23). */ +export function openaiEmbeddingProvider(model: string, apiKey: string, fetchFn: FetchLike = fetch): EmbeddingPort { + const parsed = parseEmbeddingModelSlug(model); + if (!parsed) throw new Error(`invalid embedding model slug: ${model}`); + return { + model, + async embed(texts: readonly string[], task: EmbeddingTask): Promise { + // OpenAI's embeddings API takes no task hint (one format per + // model); the argument exists for seam parity and is unused here. + void task; + const resp = await fetchFn("https://api.openai.com/v1/embeddings", { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` }, + body: JSON.stringify({ model: parsed.id, input: texts }), + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), + }); + await demandOk(resp, "OpenAI embeddings"); + const data = (await resp.json()) as { + data?: Array<{ embedding?: unknown; index?: unknown }>; + usage?: { prompt_tokens?: number }; + }; + return { + vectors: rowsInOrder(data.data ?? [], texts.length, "OpenAI embeddings"), + model, + dim: 0, + promptTokens: data.usage?.prompt_tokens, + }; + }, + }; +} + +/** Google: POST …/models/{m}:batchEmbedContents — x-goog-api-key, one + * request per text with taskType RETRIEVAL_QUERY|RETRIEVAL_DOCUMENT + * (verified against the Gemini API docs 2026-09-23). */ +export function googleEmbeddingProvider(model: string, apiKey: string, fetchFn: FetchLike = fetch): EmbeddingPort { + const parsed = parseEmbeddingModelSlug(model); + if (!parsed) throw new Error(`invalid embedding model slug: ${model}`); + return { + model, + async embed(texts: readonly string[], task: EmbeddingTask): Promise { + const resp = await fetchFn( + `https://generativelanguage.googleapis.com/v1beta/models/${encodeURIComponent(parsed.id)}:batchEmbedContents`, + { + method: "POST", + headers: { "content-type": "application/json", "x-goog-api-key": apiKey }, + body: JSON.stringify({ + requests: texts.map((text) => ({ + model: `models/${parsed.id}`, + content: { parts: [{ text }] }, + taskType: task === "query" ? "RETRIEVAL_QUERY" : "RETRIEVAL_DOCUMENT", + })), + }), + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), + }, + ); + await demandOk(resp, "Google embeddings"); + const data = (await resp.json()) as { + embeddings?: Array<{ values?: unknown }>; + /** embedContent responses carry usageMetadata; the batch + * shape may too — parsed opportunistically, never guessed. */ + usageMetadata?: { promptTokenCount?: number }; + }; + const vectors = (data.embeddings ?? []).map((row, i) => { + if (!Array.isArray(row.values) || !row.values.every((x) => typeof x === "number")) { + throw new Error(`Google embeddings API returned a bad vector at index ${i}`); + } + return row.values as number[]; + }); + if (vectors.length !== texts.length) { + throw new Error(`Google embeddings API returned ${vectors.length}/${texts.length} vectors`); + } + return { vectors, model, dim: 0, promptTokens: data.usageMetadata?.promptTokenCount }; + }, + }; +} + +// ─── Resolution (fresh per call — the search port's treatment) ─────────────── + +/** The resolved state: a live port, or the honest reason it isn't. */ +export type EmbeddingResolution = + | { ok: true; port: EmbeddingPort } + | { ok: false; reason: string }; + +/** Pure resolution from a setting slug + a key lookup (testable): null + * slug = off; a set slug with no key = honest unavailability. */ +export function resolveEmbeddingPortFor( + slug: string | null | undefined, + keyOf: (envVar: string) => string | undefined, + fetchFn?: FetchLike, +): EmbeddingResolution { + if (slug === null || slug === undefined || slug === "") { + return { ok: false, reason: "no embedding model configured (graph.embeddingModel is null)" }; + } + const parsed = parseEmbeddingModelSlug(slug); + if (!parsed) { + return { ok: false, reason: `invalid embedding model slug: ${slug}` }; + } + const apiKey = keyOf(PROVIDER_KEYS[parsed.provider]); + if (!apiKey) { + return { + ok: false, + reason: `embedding model is set to "${slug}" but its ${PROVIDER_KEYS[parsed.provider]} key is missing`, + }; + } + const factory = + parsed.provider === "openrouter" + ? openrouterEmbeddingProvider + : parsed.provider === "openai" + ? openaiEmbeddingProvider + : googleEmbeddingProvider; + return { ok: true, port: factory(slug, apiKey, fetchFn) }; +} + +/** The LIVE resolution — reads the settings record + auth store fresh on + * every call, so a settings PUT or a key save lands on the next call. */ +export function embeddingPort(): EmbeddingResolution { + return resolveEmbeddingPortFor(effectiveSettings().graph.embeddingModel, keyValue); +} + +// ─── The post-merge batch (the extraction pipeline's embedding tail) ──────── + +/** What the embedding tail reports back: counts for logs, the skip reason + * when nothing ran (owner ruling: null model = info skip, not error), and + * the money-channel stage when it billed (one CallUsage step per API call, + * outputTokens: 0 — priced via EMBEDDING_PRICES). */ +export interface EmbedBatchResult { + embedded: number; + total: number; + skipped?: string; + usage?: ExtractionStageUsage; } /** * Embed every node the ledger is missing — the post-merge hook (new * propositions only, typically a handful) AND the cold-start backfill - * (empty ledger = every node) in one function. Model/dim mismatch voids - * the ledger first (full re-embed). Also prunes vectors for nodes that - * no longer exist. Writes the whole ledger and returns counts for logs. - * Non-fatal by contract: the caller treats failure as "clustering runs - * without the new nodes until the next merge retries". + * (empty ledger = every node) in one function. A model change voids the + * ledger first (full re-embed — vectors from different models live in + * incompatible spaces). Also prunes vectors for nodes that no longer + * exist, then re-clusters ONCE per batch (consumers read the persisted + * partition). Non-fatal by contract: the caller treats failure as + * "clustering runs without the new nodes until the next merge retries". */ export async function embedNewNodes( graphStore: { @@ -96,10 +295,21 @@ export async function embedNewNodes( readEmbeddings(): EmbeddingsLedger; writeEmbeddings(ledger: EmbeddingsLedger): void; }, -): Promise<{ embedded: number; total: number }> { + /** Test seam: the live resolution by default; tests inject a scripted + * port (they never read the machine's settings/auth files). */ + resolvePort: () => EmbeddingResolution = embeddingPort, +): Promise { + const resolved = resolvePort(); const ledger = graphStore.readEmbeddings(); - const stale = ledger.model !== EMBEDDING_MODEL || ledger.dim !== EMBEDDING_DIM; + if (!resolved.ok) { + console.info(`[embeddings] skipped — ${resolved.reason}`); + return { embedded: 0, total: Object.keys(ledger.vectors).length, skipped: resolved.reason }; + } + const port = resolved.port; + const stale = ledger.model !== port.model; const vectors: Record = stale ? {} : { ...ledger.vectors }; + const steps: NonNullable["steps"] = []; + let dim = ledger.dim; // Node LINES may repeat an id — dedupe, first defines (getGraph rule). const seen = new Set(); @@ -110,18 +320,20 @@ export async function embedNewNodes( if (!vectors[node.id]) todo.push(node); } - if (todo.length > 0) { - const extractor = await getPipeline(); - for (let i = 0; i < todo.length; i += EMBED_BATCH) { - const batch = todo.slice(i, i + EMBED_BATCH); - const output = await extractor(batch.map((n) => embedInput(n.statement)), { - pooling: "mean", - normalize: true, - }); - const rows = output.tolist() as number[][]; - for (let j = 0; j < batch.length; j++) { - vectors[batch[j]!.id] = truncateNormalize(rows[j]!); - } + for (let i = 0; i < todo.length; i += EMBED_BATCH) { + const batch = todo.slice(i, i + EMBED_BATCH); + const result = await port.embed( + batch.map((n) => n.statement), + "document", + ); + for (let j = 0; j < batch.length; j++) { + vectors[batch[j]!.id] = result.vectors[j]!; + } + if (result.vectors[0]) dim = result.vectors[0].length; + if (result.promptTokens !== undefined) { + // outputTokens: 0 — embeddings emit no output; callCost needs it + // defined to price the input at all. + steps.push(embeddingStep(result.promptTokens)); } } @@ -135,20 +347,112 @@ export async function embedNewNodes( } // RECLUSTER AT THE END OF THE BATCH (user-decided 2026-09-13): the - // agglomerative pass is ~1.5s at current scale, so it runs HERE — - // once per embedding batch — and consumers (snapshot, settle, namer) - // read the persisted partition instead of recomputing per read. + // agglomerative pass runs HERE — once per embedding batch — and + // consumers (snapshot, settle, namer) read the persisted partition. const clusters = semanticClusters(vectors, { similarityThreshold: SEMANTIC_SIM_THRESHOLD }); const wrote = todo.length > 0 || pruned > 0 || stale || ledger.clusters.length !== clusters.length; if (wrote) { graphStore.writeEmbeddings({ - model: EMBEDDING_MODEL, - dim: EMBEDDING_DIM, + model: port.model, + dim, vectors, clusters, updatedAt: new Date().toISOString(), }); } - return { embedded: todo.length, total: Object.keys(vectors).length }; + return { + embedded: todo.length, + total: Object.keys(vectors).length, + usage: steps.length > 0 ? { model: port.model, steps } : undefined, + }; +} +// ─── The federated embedding catalog (wave 6, owner ruling 2026-09-23) ────── + +/** One catalog row: provider-qualified id (the setting's exact slug + * format) + display name. */ +export interface EmbeddingCatalogModel { + /** The full "provider:id" slug — what graph.embeddingModel stores. */ + id: string; + /** Human label for the picker (the provider's name, when given). */ + name: string; +} + +/** The embedding models of every KEYED provider, fetched live (the + * chat-models picker is a static pi-ai catalog, but pi-ai carries no + * embedding models — so this endpoint federates the providers' own + * lists). A provider without a key contributes nothing; a provider whose + * catalog call fails is skipped with an error log (never poisons the + * rest, never fails the endpoint). Empty array = nothing keyed. */ +export async function embeddingModelCatalog( + fetchFn: FetchLike = fetch, + /** Test seam: the live key lookup by default; tests inject scripted + * keys (they never read the machine's auth file). */ + keyOf: (id: string) => string | undefined = keyValue, +): Promise { + const out: EmbeddingCatalogModel[] = []; + const fetchJson = async (url: string, headers: Record): Promise => { + const resp = await fetchFn(url, { headers, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) }); + await demandOk(resp, "Embeddings catalog"); + return resp.json(); + }; + + // OpenRouter: GET /api/v1/embeddings/models — the dedicated embedding + // list (VERIFIED live 2026-09-23: the inverse path /api/v1/models/ + // embeddings 404s; this one returns {data: [{id, name, …}]}). Paginated + // — limit=500 covers the whole list at current size (33). + if (keyOf(PROVIDER_KEYS.openrouter)) { + try { + const data = (await fetchJson("https://openrouter.ai/api/v1/embeddings/models?limit=500", { + authorization: `Bearer ${keyOf(PROVIDER_KEYS.openrouter)}`, + })) as { data?: Array<{ id?: unknown; name?: unknown }> }; + for (const m of data.data ?? []) { + if (typeof m.id !== "string" || !m.id) continue; + out.push({ id: `openrouter:${m.id}`, name: typeof m.name === "string" && m.name ? m.name : m.id }); + } + } catch (err) { + console.error("[embeddings] OpenRouter catalog fetch failed:", err); + } + } + + // OpenAI: GET /v1/models — no embedding-only list, so filter the + // generic one on the embedding naming convention. + if (keyOf(PROVIDER_KEYS.openai)) { + try { + const data = (await fetchJson("https://api.openai.com/v1/models", { + authorization: `Bearer ${keyOf(PROVIDER_KEYS.openai)}`, + })) as { data?: Array<{ id?: unknown }> }; + for (const m of data.data ?? []) { + if (typeof m.id !== "string" || !/^text-embedding-/.test(m.id)) continue; + out.push({ id: `openai:${m.id}`, name: m.id }); + } + } catch (err) { + console.error("[embeddings] OpenAI catalog fetch failed:", err); + } + } + + // Google: GET /v1beta/models — filter on the methods the model + // actually serves (embedContent present). + if (keyOf(PROVIDER_KEYS.google)) { + try { + const data = (await fetchJson("https://generativelanguage.googleapis.com/v1beta/models?pageSize=1000", { + "x-goog-api-key": keyOf(PROVIDER_KEYS.google)!, + })) as { + models?: Array<{ name?: unknown; displayName?: unknown; supportedGenerationMethods?: unknown }>; + }; + for (const m of data.models ?? []) { + if (typeof m.name !== "string" || !m.name.startsWith("models/")) continue; + if (!Array.isArray(m.supportedGenerationMethods) || !m.supportedGenerationMethods.includes("embedContent")) continue; + const id = m.name.slice("models/".length); + out.push({ + id: `google:${id}`, + name: typeof m.displayName === "string" && m.displayName ? m.displayName : id, + }); + } + } catch (err) { + console.error("[embeddings] Google catalog fetch failed:", err); + } + } + + return out; } diff --git a/src/server/index.ts b/src/server/index.ts index fd23306..ebf64d0 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -6,6 +6,7 @@ export * from "./chat-handler.ts"; export * from "./chats.ts"; +export * from "./embeddings.ts"; export * from "./graph.ts"; export * from "./commit-positions.ts"; export * from "./jobs.ts"; diff --git a/src/server/settings-body.ts b/src/server/settings-body.ts index 9c5e91f..2f823d3 100644 --- a/src/server/settings-body.ts +++ b/src/server/settings-body.ts @@ -277,7 +277,11 @@ export function parseAppearanceSettingsBody(body: unknown): SettingsBodyResult { const raw = asRecord(body); if (!raw) return { ok: false, error: "Invalid JSON body" }; @@ -290,6 +294,25 @@ export function parseGraphSettingsBody(body: unknown): SettingsBodyResult { assert.equal(s.unpricedRuns, 1); // run-level: only the both-stages run }); + test("the embedding stage (wave 6): folds when present, absent when the run embedded nothing", () => { + const EMBED_MODEL = "openai:text-embedding-3-small"; + const embStep = callCost(step(5000, 0), EMBED_MODEL)!; + assert.ok(embStep > 0, "the overlay prices the known embedding models"); + const withEmbed = extractionRun(100, PRICED, PRICED); + withEmbed.stages.embedding = { model: EMBED_MODEL, steps: [step(5000, 0)] }; + const s = graphCostSummary({ + chats: [{ title: "a", extractions: [withEmbed] }, { title: "b", extractions: [extractionRun(200, PRICED, PRICED)] }], + sources: [], + naming: [], + }); + assert.equal(s.stages.embedding.runs, 1, "only the run that embedded"); + assert.equal(s.stages.embedding.dollars, embStep); + assert.equal(s.stages.embedding.tokens.inputTokens, 5000); + assert.equal(s.stages.extraction.runs, 2, "the two-stage era still folds"); + assert.equal(s.stages.extraction.runs, 2); + // Run-level: the embedding dollars ride the run's total. + const extStep = callCost(step(1000, 200), PRICED)!; + const audStep = callCost(step(2000, 100), PRICED)!; + assert.equal(s.runs.find((r) => r.startedAt === 100)!.dollars, extStep + audStep + embStep); + assert.equal(s.runs.find((r) => r.startedAt === 200)!.dollars, extStep + audStep); + assert.equal(s.totalDollars, (extStep + audStep) * 2 + embStep); + }); + + test("an UNPRICED embedding model stays honest: counted, never a silent zero; a priced sibling saves the run total", () => { + const unpricedEmbed = extractionRun(100, PRICED, "unknown-model"); + unpricedEmbed.stages.embedding = { model: "some/new-embed", steps: [step(900, 0)] }; + const s = graphCostSummary({ chats: [{ title: "x", extractions: [unpricedEmbed] }], sources: [], naming: [] }); + assert.equal(s.stages.embedding.unpricedRuns, 1); + assert.equal(s.stages.embedding.dollars, 0); + assert.notEqual(s.runs[0]!.dollars, null, "the priced extractor saves the run total"); + }); + test("recent runs cap: newest survive", () => { const extractions = [1, 2, 3, 4].map((n) => extractionRun(n * 100, PRICED, PRICED)); const s = graphCostSummary({ diff --git a/tests/data-dir-seam.test.ts b/tests/data-dir-seam.test.ts index def0c06..f91b226 100644 --- a/tests/data-dir-seam.test.ts +++ b/tests/data-dir-seam.test.ts @@ -19,7 +19,7 @@ const REPO_ROOT = path.resolve(import.meta.dirname, ".."); const TSX_LOADER = path.join(REPO_ROOT, "node_modules", "tsx", "dist", "loader.mjs"); describe("LH_DATA_DIR seam", () => { - it("relocates data/chats/graph + modelCache under LH_DATA_DIR, keeps prompts ROOT-anchored", async () => { + it("relocates data/chats/graph under LH_DATA_DIR, keeps prompts ROOT-anchored", async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "lh-seam-")); process.env.LH_DATA_DIR = dir; try { @@ -27,7 +27,6 @@ describe("LH_DATA_DIR seam", () => { assert.equal(PATHS.data, dir, "PATHS.data resolves the env value (resolved absolute)"); assert.equal(PATHS.chats, path.join(dir, "chats")); assert.equal(PATHS.graph, path.join(dir, "graph")); - assert.equal(PATHS.modelCache, path.join(dir, "models"), "the HF model cache follows the seam"); assert.equal( PATHS.teacherPrompt, path.join(ROOT, "src", "agent", "system-prompt.md"), @@ -41,12 +40,11 @@ describe("LH_DATA_DIR seam", () => { it("unset = the repo data/ exactly as before (fresh child process, no env)", () => { const out = execFileSync( process.execPath, - ["--import", TSX_LOADER, "--eval", `const m = await import(${JSON.stringify(path.join(REPO_ROOT, "config.ts"))}); console.log(JSON.stringify({ data: m.PATHS.data, chats: m.PATHS.chats, modelCache: m.PATHS.modelCache }));`], + ["--import", TSX_LOADER, "--eval", `const m = await import(${JSON.stringify(path.join(REPO_ROOT, "config.ts"))}); console.log(JSON.stringify({ data: m.PATHS.data, chats: m.PATHS.chats }));`], { cwd: REPO_ROOT, encoding: "utf-8" }, ); - const parsed = JSON.parse(out) as { data: string; chats: string | null; modelCache: string | null }; + const parsed = JSON.parse(out) as { data: string; chats: string | null }; assert.equal(parsed.data, path.join(REPO_ROOT, "data")); assert.equal(parsed.chats, path.join(REPO_ROOT, "data", "chats")); - assert.equal(parsed.modelCache, null, "dev keeps the transformers package default cache"); }); }); \ No newline at end of file diff --git a/tests/embeddings.test.ts b/tests/embeddings.test.ts new file mode 100644 index 0000000..1990d7e --- /dev/null +++ b/tests/embeddings.test.ts @@ -0,0 +1,401 @@ +/** + * BYOK provider embeddings (wave 6, 2026-09-23): the adapters' wire + * protocols (injected fetch — never the network), the slug/keys + * resolution rules (null = honestly off; a set model with a missing key + * is an honest error), the post-merge batch's staleness/re-embed/skip + * semantics (scripted port — never the machine's settings/auth files), + * and the federated catalog's federation rules. + */ + +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import { callCost, EMBEDDING_PRICES } from "../src/core/prices.ts"; +import { + embedNewNodes, + embeddingModelCatalog, + googleEmbeddingProvider, + openaiEmbeddingProvider, + openrouterEmbeddingProvider, + parseEmbeddingModelSlug, + resolveEmbeddingPortFor, + type EmbeddingResolution, +} from "../src/server/embeddings.ts"; +import type { EmbeddingPort, EmbeddingTask } from "../src/core/ports.ts"; +import type { EmbeddingsLedger, GraphNode } from "../src/core/contracts/graph.ts"; + +// ─── scripted fetch + in-memory graph (no fs, no network) ──────────────────── + +type RecordedCall = { url: string; init: RequestInit }; + +function jsonResponse(body: unknown, status = 200): Response { + return new Response(JSON.stringify(body), { + status, + headers: { "content-type": "application/json" }, + }); +} + +function recordingFetch( + route: (url: string, body: unknown) => Response | Promise, +): { calls: RecordedCall[]; fetch: (input: string, init: RequestInit) => Promise } { + const calls: RecordedCall[] = []; + const fetchFn = async (input: string, init: RequestInit): Promise => { + calls.push({ url: input, init }); + const body = init.body ? (JSON.parse(String(init.body)) as unknown) : null; + return route(input, body); + }; + return { calls, fetch: fetchFn }; +} + +function node(id: string, statement = `statement of ${id}`): GraphNode { + return { id, name: id, statement, aliases: [], provenance: [], createdAt: "2026-09-23T00:00:00.000Z" }; +} + +function fakeGraphStore(start?: Partial): { + ledger: EmbeddingsLedger; + writes: EmbeddingsLedger[]; + store: { + readNodes(): GraphNode[]; + readEmbeddings(): EmbeddingsLedger; + writeEmbeddings(ledger: EmbeddingsLedger): void; + }; + nodes: GraphNode[]; +} { + const nodes = [node("n1", "alpha fact"), node("n2", "beta fact")]; + const ledger: EmbeddingsLedger = { + model: start?.model ?? "test-embed", + dim: start?.dim ?? 2, + vectors: { ...start?.vectors }, + clusters: [...start?.clusters ?? []], + updatedAt: start?.updatedAt ?? "2026-09-23T00:00:00.000Z", + }; + const writes: EmbeddingsLedger[] = []; + return { + ledger, + writes, + nodes, + store: { + readNodes: () => nodes, + readEmbeddings: () => ledger, + writeEmbeddings: (next) => { + writes.push(next); + Object.assign(ledger, next); + }, + }, + }; +} + +/** A scripted port: unit vectors by text hash, records (texts, task) calls. */ +function scriptedPort(model = "openai:test-embed", dim = 2, tokensPerCall = 120): { + calls: Array<{ texts: string[]; task: EmbeddingTask }>; + port: EmbeddingPort; +} { + const calls: Array<{ texts: string[]; task: EmbeddingTask }> = []; + return { + calls, + port: { + model, + async embed(texts, task) { + calls.push({ texts: [...texts], task }); + return { + vectors: texts.map((_, i) => Array.from({ length: dim }, (_, j) => (j === i ? 1 : 0))), + model, + dim, + promptTokens: tokensPerCall, + }; + }, + }, + }; +} + +const okPort = (port: EmbeddingPort): EmbeddingResolution => ({ ok: true, port }); + +// ─── the money channel's embedding overlay ───────────────────────────────── + +describe("the embedding price overlay (hand-maintained, outside the sync block)", () => { + it("prices the known embedding models at their verified input rates", () => { + // USD per 1M input tokens, verified against provider pages 2026-09-23. + assert.equal(callCost({ inputTokens: 1_000_000, outputTokens: 0 }, "openai:text-embedding-3-small"), 0.02); + assert.equal(callCost({ inputTokens: 1_000_000, outputTokens: 0 }, "openai:text-embedding-3-large"), 0.13); + assert.equal(callCost({ inputTokens: 1_000_000, outputTokens: 0 }, "google:gemini-embedding-001"), 0.15); + assert.equal(callCost({ inputTokens: 1_000_000, outputTokens: 0 }, "google:gemini-embedding-2"), 0.2); + assert.equal(callCost({ inputTokens: 1_000_000, outputTokens: 0 }, "openrouter:openai/text-embedding-3-small"), 0.02); + }); + + it("unknown embedding models stay unpriced (—, never zero), tokens still captured", () => { + assert.equal(callCost({ inputTokens: 100, outputTokens: 0 }, "openrouter:mystery/embed"), null); + assert.equal(!("openrouter:mystery/embed" in EMBEDDING_PRICES), true); + }); +}); + +// ─── the slug grammar ───────────────────────────────────────────────────────── + +describe("parseEmbeddingModelSlug", () => { + it("parses the three shipped providers", () => { + assert.deepEqual(parseEmbeddingModelSlug("openai:text-embedding-3-small"), { provider: "openai", id: "text-embedding-3-small" }); + assert.deepEqual(parseEmbeddingModelSlug("openrouter:openai/text-embedding-3-large"), { provider: "openrouter", id: "openai/text-embedding-3-large" }); + assert.deepEqual(parseEmbeddingModelSlug("google:gemini-embedding-001"), { provider: "google", id: "gemini-embedding-001" }); + // OpenRouter ids may carry a :free suffix (live catalog fact) — the + // provider is the part before the FIRST colon. + assert.deepEqual(parseEmbeddingModelSlug("openrouter:liquid/lfm-2.5-embedding-350m:free"), { provider: "openrouter", id: "liquid/lfm-2.5-embedding-350m:free" }); + }); + + it("rejects malformed slugs (no guessing)", () => { + assert.equal(parseEmbeddingModelSlug("text-embedding-3-small"), null, "no provider prefix"); + assert.equal(parseEmbeddingModelSlug("anthropic:embed"), null, "unshipped provider"); + assert.equal(parseEmbeddingModelSlug("openai:"), null, "empty id"); + assert.equal(parseEmbeddingModelSlug(":id"), null, "empty provider"); + }); +}); + +// ─── resolution: BYOK honesty rules ─────────────────────────────────────────── + +describe("resolveEmbeddingPortFor", () => { + it("null/absent setting = honestly off", () => { + for (const slug of [null, undefined, ""]) { + const res = resolveEmbeddingPortFor(slug, () => "key"); + assert.equal(res.ok, false); + assert.match((res as { reason: string }).reason, /no embedding model configured/); + } + }); + + it("a set model with no key is an honest error naming the key", () => { + const res = resolveEmbeddingPortFor("openai:text-embedding-3-small", () => undefined); + assert.equal(res.ok, false); + assert.match((res as { reason: string }).reason, /OPENAI_API_KEY/); + }); + + it("an invalid slug never resolves", () => { + const res = resolveEmbeddingPortFor("bedrock:whatever", () => "key"); + assert.equal(res.ok, false); + assert.match((res as { reason: string }).reason, /invalid embedding model slug/); + }); + + it("a keyed model resolves to its provider adapter", () => { + const res = resolveEmbeddingPortFor("openai:text-embedding-3-small", () => "sk-test"); + assert.equal(res.ok, true); + assert.equal((res as { port: EmbeddingPort }).port.model, "openai:text-embedding-3-small"); + }); +}); + +// ─── the adapters' wire protocols (injected fetch) ──────────────────────────── + +describe("openrouterEmbeddingProvider", () => { + it("POSTs /api/v1/embeddings with Bearer auth and the task input_type", async () => { + const { calls, fetch } = recordingFetch(() => + jsonResponse({ data: [{ embedding: [1, 0], index: 0 }], usage: { prompt_tokens: 42 } }), + ); + const port = openrouterEmbeddingProvider("openrouter:openai/text-embedding-3-small", "k", fetch); + const result = await port.embed(["hello"], "document"); + assert.equal(calls.length, 1); + assert.equal(calls[0]!.url, "https://openrouter.ai/api/v1/embeddings"); + assert.equal((calls[0]!.init.headers as Record).authorization, "Bearer k"); + assert.deepEqual(JSON.parse(String(calls[0]!.init.body)), { + model: "openai/text-embedding-3-small", + input: ["hello"], + input_type: "search_document", + }); + assert.deepEqual(result.vectors, [[1, 0]]); + assert.equal(result.promptTokens, 42); + + await port.embed(["q"], "query"); + assert.equal((JSON.parse(String(calls[1]!.init.body)) as { input_type: string }).input_type, "search_query"); + }); +}); + +describe("openaiEmbeddingProvider", () => { + it("POSTs /v1/embeddings, Bearer auth, no task hint; index ordering honored", async () => { + const { calls, fetch } = recordingFetch(() => + jsonResponse({ + data: [{ embedding: [0, 1], index: 1 }, { embedding: [1, 0], index: 0 }], + usage: { prompt_tokens: 7, total_tokens: 7 }, + }), + ); + const port = openaiEmbeddingProvider("openai:text-embedding-3-small", "sk", fetch); + const result = await port.embed(["a", "b"], "document"); + assert.equal(calls[0]!.url, "https://api.openai.com/v1/embeddings"); + assert.equal((calls[0]!.init.headers as Record).authorization, "Bearer sk"); + const body = JSON.parse(String(calls[0]!.init.body)) as Record; + assert.deepEqual(body.input, ["a", "b"]); + assert.equal("input_type" in body, false, "OpenAI takes no task hint"); + // Rows arrive out of order — the adapter must restore input order. + assert.deepEqual(result.vectors, [[1, 0], [0, 1]]); + assert.equal(result.promptTokens, 7); + }); +}); + +describe("googleEmbeddingProvider", () => { + it("POSTs :batchEmbedContents with x-goog-api-key and per-text taskType", async () => { + const { calls, fetch } = recordingFetch((_url, body) => { + const n = ((body as { requests?: unknown[] } | null)?.requests ?? []).length; + return jsonResponse({ embeddings: Array.from({ length: n }, (_, i) => ({ values: i === 0 ? [1, 0] : [0, 1] })) }); + }); + const port = googleEmbeddingProvider("google:gemini-embedding-001", "gk", fetch); + const result = await port.embed(["a", "b"], "document"); + assert.equal(calls[0]!.url, "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:batchEmbedContents"); + assert.equal((calls[0]!.init.headers as Record)["x-goog-api-key"], "gk"); + assert.deepEqual(JSON.parse(String(calls[0]!.init.body)), { + requests: [ + { model: "models/gemini-embedding-001", content: { parts: [{ text: "a" }] }, taskType: "RETRIEVAL_DOCUMENT" }, + { model: "models/gemini-embedding-001", content: { parts: [{ text: "b" }] }, taskType: "RETRIEVAL_DOCUMENT" }, + ], + }); + assert.deepEqual(result.vectors, [[1, 0], [0, 1]]); + assert.equal(result.promptTokens, undefined, "batch responses carry no token count we can trust"); + + await port.embed(["q"], "query"); + const second = JSON.parse(String(calls[1]!.init.body)) as { requests: Array<{ taskType: string }> }; + assert.equal(second.requests[0]!.taskType, "RETRIEVAL_QUERY"); + }); + + it("a short/missing vector is an honest error, never a silent misalignment", async () => { + const { fetch } = recordingFetch(() => jsonResponse({ embeddings: [{ values: [1, 0] }] })); + const port = googleEmbeddingProvider("google:gemini-embedding-001", "gk", fetch); + await assert.rejects(port.embed(["a", "b"], "document"), /1\/2 vectors/); + }); +}); + +describe("adapter error mapping (all providers)", () => { + it("HTTP failures become clear Errors carrying the status", async () => { + for (const factory of [openrouterEmbeddingProvider, openaiEmbeddingProvider, googleEmbeddingProvider]) { + const { fetch } = recordingFetch(() => jsonResponse({ error: { message: "bad key" } }, 401)); + const port = factory("openai:text-embedding-3-small", "k", fetch); + await assert.rejects(port.embed(["x"], "document"), /API 401/); + } + }); +}); + +// ─── the post-merge batch (scripted port — the machine's files stay unread) ─── + +describe("embedNewNodes (the extraction tail)", () => { + it("embeds missing nodes, stamps model/dim, records one usage step per API call", async () => { + const g = fakeGraphStore(); + const scripted = scriptedPort("openai:text-embedding-3-small", 2, 120); + const result = await embedNewNodes(g.store, () => okPort(scripted.port)); + assert.deepEqual(result, { + embedded: 2, + total: 2, + usage: { + model: "openai:text-embedding-3-small", + steps: [ + { inputTokens: 120, outputTokens: 0, cacheReadTokens: undefined, cacheWriteTokens: undefined, reasoningTokens: undefined }, + ], + }, + }); + assert.equal(g.writes.length, 1, "the ledger is written once per batch"); + assert.equal(g.ledger.model, "openai:text-embedding-3-small"); + assert.equal(g.ledger.dim, 2); + assert.ok(g.ledger.vectors.n1 && g.ledger.vectors.n2); + assert.deepEqual(scripted.calls[0]!.texts, ["alpha fact", "beta fact"]); + assert.equal(scripted.calls[0]!.task, "document"); + assert.equal(result.usage?.steps.length, 1); + }); + + it("a MODEL CHANGE voids the ledger and re-embeds everything", async () => { + // The ledger holds a stale-model vector for a node that still exists. + const g = fakeGraphStore({ model: "openai:old-embed", vectors: { n1: [9, 9] } }); + const scripted = scriptedPort("openai:new-embed", 2); + const result = await embedNewNodes(g.store, () => okPort(scripted.port)); + assert.equal(result.embedded, 2, "the stale vector dies — BOTH nodes re-embed"); + assert.deepEqual(scripted.calls[0]!.texts, ["alpha fact", "beta fact"]); + assert.equal(g.ledger.model, "openai:new-embed"); + assert.ok(!g.ledger.vectors.n1!.every((x, i) => x === [9, 9][i]), "the old vector was replaced"); + }); + + it("null model = info skip: nothing written, nothing embedded, the reason rides the result", async () => { + const g = fakeGraphStore({ vectors: { n1: [1, 0] } }); + const result = await embedNewNodes(g.store, () => ({ + ok: false, + reason: "no embedding model configured (graph.embeddingModel is null)", + })); + assert.deepEqual(result, { embedded: 0, total: 1, skipped: "no embedding model configured (graph.embeddingModel is null)" }); + assert.equal(g.writes.length, 0, "a skipped batch never touches the ledger"); + }); + + it("a set model with a missing key is the same skip path (its reason says so)", async () => { + const g = fakeGraphStore(); + const result = await embedNewNodes(g.store, () => ({ + ok: false, + reason: 'embedding model is set to "openai:x" but its OPENAI_API_KEY key is missing', + })); + assert.match(result.skipped ?? "", /OPENAI_API_KEY/); + }); + + it("prunes ghost vectors for nodes that no longer exist", async () => { + const g = fakeGraphStore({ vectors: { n1: [1, 0], ghost: [0, 1] } }); + const scripted = scriptedPort(g.ledger.model, 2); + const result = await embedNewNodes(g.store, () => okPort(scripted.port)); + assert.equal(result.embedded, 1, "n1 already has a vector; only n2 embeds"); + assert.ok(!("ghost" in g.ledger.vectors), "the ghost vector is pruned"); + }); + + it("batches at 32 (a 40-node backfill = two API calls)", async () => { + const many: GraphNode[] = Array.from({ length: 40 }, (_, i) => node(`n${i}`, `statement ${i}`)); + const ledger: EmbeddingsLedger = { model: "m", dim: 2, vectors: {}, clusters: [], updatedAt: "" }; + const writes: EmbeddingsLedger[] = []; + const scripted = scriptedPort("m", 2); + const result = await embedNewNodes( + { + readNodes: () => many, + readEmbeddings: () => ledger, + writeEmbeddings: (next) => writes.push(next), + }, + () => okPort(scripted.port), + ); + assert.equal(result.embedded, 40); + assert.equal(scripted.calls.length, 2); + assert.equal(scripted.calls[0]!.texts.length, 32); + assert.equal(scripted.calls[1]!.texts.length, 8); + assert.equal(writes.length, 1); + }); +}); + +// ─── the federated catalog (scripted keys + fetch) ──────────────────────────── + +describe("embeddingModelCatalog", () => { + it("no keys = empty catalog", async () => { + const models = await embeddingModelCatalog( + recordingFetch(() => jsonResponse({})).fetch, + () => undefined, + ); + assert.deepEqual(models, []); + }); + + it("federates every keyed provider with its filter rules", async () => { + const { fetch, calls } = recordingFetch((url) => { + if (url.includes("openrouter.ai/api/v1/embeddings/models")) { + return jsonResponse({ data: [{ id: "openai/text-embedding-3-small", name: "Text Embedding 3 Small" }, { id: "other/embed" }] }); + } + if (url.includes("api.openai.com")) { + return jsonResponse({ data: [{ id: "text-embedding-3-large" }, { id: "text-embedding-3-small" }, { id: "gpt-6-sol" }] }); + } + if (url.includes("generativelanguage")) { + return jsonResponse({ + models: [ + { name: "models/gemini-embedding-001", displayName: "Gemini Embedding", supportedGenerationMethods: ["embedContent", "generateContent"] }, + { name: "models/gemini-6-pro", supportedGenerationMethods: ["generateContent"] }, + ], + }); + } + return jsonResponse({}); + }); + const models = await embeddingModelCatalog(fetch, (id) => (id === "OPENROUTER_API_KEY" || id === "OPENAI_API_KEY" || id === "GEMINI_API_KEY" ? "k" : undefined)); + assert.equal(calls[0]!.url, "https://openrouter.ai/api/v1/embeddings/models?limit=500", "the real catalog path (the inverse 404s — verified live)"); + assert.deepEqual(models, [ + { id: "openrouter:openai/text-embedding-3-small", name: "Text Embedding 3 Small" }, + { id: "openrouter:other/embed", name: "other/embed" }, + { id: "openai:text-embedding-3-large", name: "text-embedding-3-large" }, + { id: "openai:text-embedding-3-small", name: "text-embedding-3-small" }, + { id: "google:gemini-embedding-001", name: "Gemini Embedding" }, + ]); + }); + + it("a failing provider never poisons the rest", async () => { + const { fetch } = recordingFetch((url) => { + if (url.includes("openrouter.ai")) return jsonResponse({}, 500); // their side broke + if (url.includes("api.openai.com")) return jsonResponse({ data: [{ id: "text-embedding-3-small" }] }); + return jsonResponse({}); + }); + const models = await embeddingModelCatalog(fetch, (id) => (id === "OPENROUTER_API_KEY" || id === "OPENAI_API_KEY" ? "k" : undefined)); + assert.deepEqual(models, [{ id: "openai:text-embedding-3-small", name: "text-embedding-3-small" }]); + }); +}); \ No newline at end of file diff --git a/tests/graph-context-job.test.ts b/tests/graph-context-job.test.ts index 8cad3ec..ec99a4e 100644 --- a/tests/graph-context-job.test.ts +++ b/tests/graph-context-job.test.ts @@ -15,7 +15,7 @@ import { after, describe, it } from "node:test"; import { deliverJobResult, setTurnRunner } from "../src/server/jobs.ts"; import { abortJob } from "../src/server/jobs.ts"; import { persistTurn } from "../src/server/turn-service.ts"; -import { startGraphContextInjection, runGraphContextJob, type GraphContextDeps } from "../src/agent/subagents/graph-context.ts"; +import { startGraphContextInjection, runGraphContextJob, type GraphContextDeps, type GraphContextEmbedder } from "../src/agent/subagents/graph-context.ts"; import { chatStore } from "../src/store/chats.ts"; import { jobStore } from "../src/store/jobs.ts"; import { PATHS } from "../config.ts"; @@ -104,17 +104,18 @@ function newTopicChat(label: string, settings?: Record): { id: return { id: chat.id }; } -/** The scripted embedder: deterministic query vectors, recorded topics. */ +/** The scripted embedder: deterministic query vectors, recorded topics. + * Wave 6 shape: the query carries its (absent) prompt-token count; the + * resolver seam wraps the static fake (fresh resolution per run). */ function fakeEmbedder(vec: number[] = unit(0)) { const topics: string[] = []; return { topics, embedder: { model: "test-embed", - dim: 8, embedQuery: async (text: string) => { topics.push(text); - return vec; + return { vector: vec, promptTokens: undefined }; }, }, }; @@ -122,10 +123,10 @@ function fakeEmbedder(vec: number[] = unit(0)) { function deps( graph: Pick, - embedder: GraphContextDeps["embedder"], + embedder: GraphContextEmbedder, deliver: (steer: SteerMessage) => Promise, ): GraphContextDeps { - return { chatStore, jobStore, graphStore: graph, embedder, deliver }; + return { chatStore, jobStore, graphStore: graph, embedder: () => embedder, deliver }; } describe("startGraphContextInjection — the one-shot gates", () => { @@ -205,7 +206,9 @@ describe("runGraphContextJob — failure modes are honest errors, never phantom trackChat(chat.id); const job = jobStore.createJob({ kind: "graph-context", chatId, toolCallId: `ctx_${chatId}` }); trackJob(job.id); - // Ledger stamped with a DIFFERENT model than the embedder. + // Ledger stamped with a DIFFERENT model than the embedder (wave 6: + // staleness is the model identity — dim is checked against the + // served vector below). const d = deps(fakeGraphStore({ vectors: { n1: unit(0) }, model: "other-model" }), fakeEmbedder().embedder, async () => {}); await runGraphContextJob(d, job.id); const got = jobStore.getJob(job.id)!; @@ -280,8 +283,7 @@ describe("the injection + the wake pipeline (end to end, fake turn runner)", () }); const embedder = { model: "test-embed", - dim: 8, - embedQuery: () => hanging.then(() => unit(0)), + embedQuery: () => hanging.then(() => ({ vector: unit(0), promptTokens: undefined })), }; const fired: number[] = []; setTurnRunner(async () => { diff --git a/tests/headless-png.test.ts b/tests/headless-png.test.ts index 1fa5b5d..307d988 100644 --- a/tests/headless-png.test.ts +++ b/tests/headless-png.test.ts @@ -11,9 +11,12 @@ import { providerSafePng } from "../src/agent/subagents/headless.ts"; * passed. providerSafePng merges the split back into the canonical ONE-IDAT * form (PNG spec: the IDAT chunks are an arbitrary split of ONE zlib stream — * the merged form carries the byte-identical stream). Pure Node on purpose: - * the process already loads transformers' nested sharp/libvips; a second copy - * (root sharp) triggered macOS's duplicate-libvips ObjC warning. These tests - * pin the property with no native deps at all. + * historically the process already carried a native image lib via + * @huggingface/transformers' nested sharp/libvips, and a second copy (root + * sharp) triggered macOS's duplicate-libvips ObjC warning — the transformers + * embedder died with wave 6 (2026-09-23) but the no-native-deps rule lives + * on: app code carries no image library. These tests pin the property with + * no native deps at all. */ // ── a tiny PNG toolchain (spec-level, no image library) ───────────────────── diff --git a/tests/settings-body.test.ts b/tests/settings-body.test.ts index 5660cea..fa69652 100644 --- a/tests/settings-body.test.ts +++ b/tests/settings-body.test.ts @@ -156,6 +156,30 @@ describe("parseGraphSettingsBody", () => { const r = parseGraphSettingsBody({ enabled: false, clustering: true, labeling: true }); assert.ok(r.ok); }); + + it("embeddingModel (wave 6): null or a shipped-provider slug; anything else is a 400", () => { + const nulled = parseGraphSettingsBody({ embeddingModel: null }); + assert.ok(nulled.ok); + assert.deepEqual(nulled.settings, { embeddingModel: null }); + for (const slug of [ + "openai:text-embedding-3-small", + "openrouter:openai/text-embedding-3-large", + "google:gemini-embedding-001", + // OpenRouter ids may carry a :free-style suffix — the provider is + // the part before the FIRST colon (live catalog fact, 2026-09-23). + "openrouter:liquid/lfm-2.5-embedding-350m:free", + ]) { + const r = parseGraphSettingsBody({ embeddingModel: slug }); + assert.ok(r.ok, slug); + assert.deepEqual(r.settings, { embeddingModel: slug }); + } + // Bad shapes never become a live model. + assert.equal(parseGraphSettingsBody({ embeddingModel: "text-embedding-3-small" }).ok, false, "no provider prefix"); + assert.equal(parseGraphSettingsBody({ embeddingModel: "bedrock:whatever" }).ok, false, "unshipped provider"); + assert.equal(parseGraphSettingsBody({ embeddingModel: "openai:" }).ok, false, "empty id"); + assert.equal(parseGraphSettingsBody({ embeddingModel: 42 }).ok, false, "non-string"); + assert.equal(parseGraphSettingsBody({ embeddingModel: "openai:has spaces" }).ok, false, "whitespace id"); + }); }); describe("SETTINGS_SECTIONS (the route's section map)", () => { diff --git a/tests/settings-resolution.test.ts b/tests/settings-resolution.test.ts index 6efe0d8..f9eaa68 100644 --- a/tests/settings-resolution.test.ts +++ b/tests/settings-resolution.test.ts @@ -42,7 +42,7 @@ describe("resolveSettings", () => { assert.equal(eff.appearance.proseFontSize, 16); assert.equal(eff.appearance.sessionNameSerif, true); assert.equal(eff.appearance.mdRendering.headingWeight, 900); - assert.deepEqual(eff.graph, { enabled: true, embedding: true, clustering: true, labeling: true }); + assert.deepEqual(eff.graph, { enabled: true, embedding: true, clustering: true, labeling: true, embeddingModel: null }); // BYOK wave 6: no factory embedding model }); it("explicit record values win over every factory default", () => { @@ -77,7 +77,7 @@ describe("resolveSettings", () => { assert.equal(eff.appearance.mdRendering.headingWeight, 600); // graph: enabled=false explicit; absent rungs stay factory-on (stored // but inert — the ladder grays them out in the UI). - assert.deepEqual(eff.graph, { enabled: false, embedding: true, clustering: true, labeling: true }); + assert.deepEqual(eff.graph, { enabled: false, embedding: true, clustering: true, labeling: true, embeddingModel: null }); // wave 6: unset embeddingModel resolves to the null floor }); it("partial role override: thinkingLevel alone borrows the SLOT's model (or nothing, without one)", () => { @@ -224,15 +224,15 @@ describe("resolveConversationSpec (the turn gate's chain)", () => { describe("the graph channel gates (the wave's seam honoring)", () => { it("WRITE channel (extraction): dies with the master switch only", () => { - assert.equal(extractionEnabled({ enabled: true, embedding: true, clustering: true, labeling: true }), true); - assert.equal(extractionEnabled({ enabled: true, embedding: false, clustering: false, labeling: false }), true); - assert.equal(extractionEnabled({ enabled: false, embedding: true, clustering: true, labeling: true }), false); + assert.equal(extractionEnabled({ enabled: true, embedding: true, clustering: true, labeling: true, embeddingModel: null }), true); + assert.equal(extractionEnabled({ enabled: true, embedding: false, clustering: false, labeling: false, embeddingModel: null }), true); + assert.equal(extractionEnabled({ enabled: false, embedding: true, clustering: true, labeling: true, embeddingModel: null }), false); }); it("READ channel (graph-context): needs master AND the embedding rung", () => { - assert.equal(graphContextEnabled({ enabled: true, embedding: true, clustering: false, labeling: false }), true); - assert.equal(graphContextEnabled({ enabled: true, embedding: false, clustering: true, labeling: true }), false); - assert.equal(graphContextEnabled({ enabled: false, embedding: true, clustering: true, labeling: true }), false); + assert.equal(graphContextEnabled({ enabled: true, embedding: true, clustering: false, labeling: false, embeddingModel: null }), true); + assert.equal(graphContextEnabled({ enabled: true, embedding: false, clustering: true, labeling: true, embeddingModel: null }), false); + assert.equal(graphContextEnabled({ enabled: false, embedding: true, clustering: true, labeling: true, embeddingModel: null }), false); }); it("both gates ride resolveSettings' factory defaults (all on → both open)", () => { diff --git a/tests/settings.test.ts b/tests/settings.test.ts index 7f8fffa..71c29c7 100644 --- a/tests/settings.test.ts +++ b/tests/settings.test.ts @@ -21,7 +21,7 @@ describe("FACTORY_SETTINGS (the compiled factory layer)", () => { it("pins the owner-decided defaults", () => { assert.equal(CURRENT_SETTINGS_VERSION, 1); assert.deepEqual(FACTORY_SETTINGS.general, { autoUpdate: true }); - assert.deepEqual(FACTORY_SETTINGS.graph, { enabled: true, embedding: true, clustering: true, labeling: true }); + assert.deepEqual(FACTORY_SETTINGS.graph, { enabled: true, embedding: true, clustering: true, labeling: true, embeddingModel: null }); // BYOK wave 6: no factory embedding model const a = FACTORY_SETTINGS.appearance; assert.equal(a.mode, "dark"); assert.equal(a.background, null); // the mode's own default, not a factory hex