@powerhousedao/switchboard 6.2.3-dev.1 → 6.2.3-dev.100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.tsbuild/test/ai-gateway.test.d.ts +2 -0
- package/.tsbuild/test/ai-gateway.test.d.ts.map +1 -0
- package/.tsbuild/test/attachments/byte-grants.test.d.ts +2 -0
- package/.tsbuild/test/attachments/byte-grants.test.d.ts.map +1 -0
- package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts +2 -0
- package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts.map +1 -0
- package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts +2 -0
- package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts.map +1 -0
- package/.tsbuild/test/attachments/url-signer.test.d.ts +2 -0
- package/.tsbuild/test/attachments/url-signer.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-auth-config.test.d.ts +2 -0
- package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-create-signature-policy.test.d.ts +2 -0
- package/.tsbuild/test/boot-create-signature-policy.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-desktop-auth.test.d.ts +2 -0
- package/.tsbuild/test/boot-desktop-auth.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-env-isolation.test.d.ts +2 -0
- package/.tsbuild/test/boot-env-isolation.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-helpers.d.ts +5 -0
- package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
- package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
- package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
- package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-mcp-enabled.test.d.ts +2 -0
- package/.tsbuild/test/boot-mcp-enabled.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
- package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-preconditions.test.d.ts +2 -0
- package/.tsbuild/test/boot-preconditions.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
- package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-ready-drives.test.d.ts +2 -0
- package/.tsbuild/test/boot-ready-drives.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-undo-v3-flag.test.d.ts +2 -0
- package/.tsbuild/test/boot-undo-v3-flag.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts +2 -0
- package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-without-renown.test.d.ts +2 -0
- package/.tsbuild/test/boot-without-renown.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-without-require-signatures.test.d.ts +2 -0
- package/.tsbuild/test/boot-without-require-signatures.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-worker-trust-policy.test.d.ts +2 -0
- package/.tsbuild/test/boot-worker-trust-policy.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
- package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
- package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
- package/.tsbuild/test/builder-defaults.test.d.ts +2 -0
- package/.tsbuild/test/builder-defaults.test.d.ts.map +1 -0
- package/.tsbuild/test/config-from-env.test.d.ts +2 -0
- package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
- package/.tsbuild/test/config-merge.test.d.ts +2 -0
- package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
- package/.tsbuild/test/config-schema.test.d.ts +2 -0
- package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
- package/.tsbuild/test/create-signature-policy.test.d.ts +2 -0
- package/.tsbuild/test/create-signature-policy.test.d.ts.map +1 -0
- package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
- package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
- package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
- package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
- package/.tsbuild/test/delegated-publish.test.d.ts +2 -0
- package/.tsbuild/test/delegated-publish.test.d.ts.map +1 -0
- package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
- package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
- package/.tsbuild/test/fatal-shutdown.test.d.ts +2 -0
- package/.tsbuild/test/fatal-shutdown.test.d.ts.map +1 -0
- package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
- package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
- package/.tsbuild/test/keyframe-reclaim-store.test.d.ts +2 -0
- package/.tsbuild/test/keyframe-reclaim-store.test.d.ts.map +1 -0
- package/.tsbuild/test/observability-signals.test.d.ts +2 -0
- package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
- package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
- package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
- package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
- package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
- package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
- package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
- package/.tsbuild/test/pglite-store.test.d.ts +2 -0
- package/.tsbuild/test/pglite-store.test.d.ts.map +1 -0
- package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
- package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
- package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
- package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
- package/.tsbuild/test/port-probe.test.d.ts +2 -0
- package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
- package/.tsbuild/test/processor-timing-log.test.d.ts +2 -0
- package/.tsbuild/test/processor-timing-log.test.d.ts.map +1 -0
- package/.tsbuild/test/redact-webhook-path.test.d.ts +2 -0
- package/.tsbuild/test/redact-webhook-path.test.d.ts.map +1 -0
- package/.tsbuild/test/renown-storage-path.test.d.ts +2 -0
- package/.tsbuild/test/renown-storage-path.test.d.ts.map +1 -0
- package/.tsbuild/test/renown-trust-policy.test.d.ts +2 -0
- package/.tsbuild/test/renown-trust-policy.test.d.ts.map +1 -0
- package/.tsbuild/test/served-schemas.test.d.ts +2 -0
- package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
- package/.tsbuild/test/shutdown.test.d.ts +2 -0
- package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
- package/.tsbuild/test/snapshot-writer.d.ts +18 -0
- package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
- package/.tsbuild/test/store-lock.test.d.ts +2 -0
- package/.tsbuild/test/store-lock.test.d.ts.map +1 -0
- package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
- package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
- package/.tsbuild/test/unsupported-stored-documents.test.d.ts +2 -0
- package/.tsbuild/test/unsupported-stored-documents.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/harness.d.ts +56 -0
- package/.tsbuild/test/workflow/harness.d.ts.map +1 -0
- package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts +2 -0
- package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/oauth.test.d.ts +2 -0
- package/.tsbuild/test/workflow/oauth.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/resolvers.test.d.ts +2 -0
- package/.tsbuild/test/workflow/resolvers.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
- package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts +2 -0
- package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts +2 -0
- package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts +2 -0
- package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/webhook-signed.test.d.ts +2 -0
- package/.tsbuild/test/workflow/webhook-signed.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts +2 -0
- package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts +2 -0
- package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
- package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
- package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-read-gate.test.d.ts +2 -0
- package/.tsbuild/test/workflow-read-gate.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-runtime.test.d.ts +2 -0
- package/.tsbuild/test/workflow-runtime.test.d.ts.map +1 -0
- package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
- package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
- package/.tsbuild/tsconfig.tsbuildinfo +1 -1
- package/.tsbuild/tsdown.config.d.ts.map +1 -1
- package/.tsbuild/vitest.config.d.ts.map +1 -1
- package/Auth.md +144 -2
- package/CHANGELOG.md +1356 -0
- package/README.md +102 -22
- package/dist/boot.d.mts +46 -0
- package/dist/boot.d.mts.map +1 -0
- package/dist/boot.mjs +67 -0
- package/dist/boot.mjs.map +1 -0
- package/dist/config/from-env.d.mts +15 -0
- package/dist/config/from-env.d.mts.map +1 -0
- package/dist/config/from-env.mjs +4 -0
- package/dist/config/schema.d.mts +3 -0
- package/dist/config/schema.mjs +4 -0
- package/dist/from-env-FH4fPeIE.mjs +481 -0
- package/dist/from-env-FH4fPeIE.mjs.map +1 -0
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +98 -80
- package/dist/index.mjs.map +1 -1
- package/dist/install-packages.d.mts +2 -1
- package/dist/install-packages.mjs +2 -2
- package/dist/install-packages.mjs.map +1 -1
- package/dist/mcp-SsBK14Jm.mjs +32 -0
- package/dist/mcp-SsBK14Jm.mjs.map +1 -0
- package/dist/migrate.d.mts +2 -1
- package/dist/migrate.mjs +7 -5
- package/dist/migrate.mjs.map +1 -1
- package/dist/plugin-host-CK9_tVak.d.mts +46 -0
- package/dist/plugin-host-CK9_tVak.d.mts.map +1 -0
- package/dist/plugins/ai.d.mts +37 -0
- package/dist/plugins/ai.d.mts.map +1 -0
- package/dist/plugins/ai.mjs +400 -0
- package/dist/plugins/ai.mjs.map +1 -0
- package/dist/plugins/attachments.d.mts +33 -0
- package/dist/plugins/attachments.d.mts.map +1 -0
- package/dist/plugins/attachments.mjs +4 -0
- package/dist/plugins/mcp.d.mts +31 -0
- package/dist/plugins/mcp.d.mts.map +1 -0
- package/dist/plugins/mcp.mjs +4 -0
- package/dist/plugins/privacy.d.mts +22 -0
- package/dist/plugins/privacy.d.mts.map +1 -0
- package/dist/plugins/privacy.mjs +4 -0
- package/dist/plugins/workflows.d.mts +79 -0
- package/dist/plugins/workflows.d.mts.map +1 -0
- package/dist/plugins/workflows.mjs +4 -0
- package/dist/postgres-url-DTDhBjXX.mjs +11 -0
- package/dist/postgres-url-DTDhBjXX.mjs.map +1 -0
- package/dist/privacy-BO8Q25xI.mjs +44 -0
- package/dist/privacy-BO8Q25xI.mjs.map +1 -0
- package/dist/privacy-DxIJGH4P.mjs +91 -0
- package/dist/privacy-DxIJGH4P.mjs.map +1 -0
- package/dist/renown-layers-DmbWLt46.mjs +42 -0
- package/dist/renown-layers-DmbWLt46.mjs.map +1 -0
- package/dist/schema-CsY8bjsu.mjs +329 -0
- package/dist/schema-CsY8bjsu.mjs.map +1 -0
- package/dist/schema-MAUTnmj1.d.mts +251 -0
- package/dist/schema-MAUTnmj1.d.mts.map +1 -0
- package/dist/server-CirQ9hyu.mjs +2732 -0
- package/dist/server-CirQ9hyu.mjs.map +1 -0
- package/dist/server.d.mts +129 -189
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +5 -5
- package/dist/types-CidZdkhU.d.mts +314 -0
- package/dist/types-CidZdkhU.d.mts.map +1 -0
- package/dist/utils-Cm0TE6dP.mjs +148 -0
- package/dist/utils-Cm0TE6dP.mjs.map +1 -0
- package/dist/utils.d.mts +30 -5
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +5 -4
- package/dist/worker-pool-gyxJnCxA.mjs +304 -0
- package/dist/worker-pool-gyxJnCxA.mjs.map +1 -0
- package/dist/workflow-runtime-CF5pc8mt.d.mts +33 -0
- package/dist/workflow-runtime-CF5pc8mt.d.mts.map +1 -0
- package/dist/workflows-C9BQlyT0.mjs +1943 -0
- package/dist/workflows-C9BQlyT0.mjs.map +1 -0
- package/package.json +85 -24
- package/scripts/install-packages.sh +0 -0
- package/test/ai-gateway.test.ts +612 -0
- package/test/attachment-reference-read-model.test.ts +28 -20
- package/test/attachments/auth.test.ts +85 -0
- package/test/attachments/byte-grants.test.ts +290 -0
- package/test/attachments/byte-routes-read-gate.integration.test.ts +314 -0
- package/test/attachments/download-target.test.ts +211 -32
- package/test/attachments/index.test.ts +47 -4
- package/test/attachments/reserve-metadata-parity.test.ts +96 -0
- package/test/attachments/routes-integration.test.ts +60 -4
- package/test/attachments/routes.test.ts +297 -35
- package/test/attachments/service-config.test.ts +41 -19
- package/test/attachments/url-signer.test.ts +105 -0
- package/test/boot-auth-config.test.ts +109 -0
- package/test/boot-create-signature-policy.test.ts +110 -0
- package/test/boot-desktop-auth.test.ts +278 -0
- package/test/boot-env-isolation.test.ts +26 -0
- package/test/boot-helpers.ts +19 -0
- package/test/boot-inspection-enabled.test.ts +140 -0
- package/test/boot-mcp-auth.test.ts +449 -0
- package/test/boot-mcp-enabled.test.ts +119 -0
- package/test/boot-port-zero.test.ts +101 -0
- package/test/boot-preconditions.test.ts +213 -0
- package/test/boot-privacy.test.ts +232 -0
- package/test/boot-ready-drives.test.ts +141 -0
- package/test/boot-undo-v3-flag.test.ts +120 -0
- package/test/boot-unsupported-stored-documents.test.ts +214 -0
- package/test/boot-without-renown.test.ts +83 -0
- package/test/boot-without-require-signatures.test.ts +76 -0
- package/test/boot-worker-trust-policy.test.ts +91 -0
- package/test/boot-workflow-singleton-conflict.test.ts +109 -0
- package/test/boot-workflow-singleton-retry.test.ts +122 -0
- package/test/builder-defaults.test.ts +80 -0
- package/test/config-from-env.test.ts +963 -0
- package/test/config-merge.test.ts +248 -0
- package/test/config-schema.test.ts +167 -0
- package/test/create-signature-policy.test.ts +69 -0
- package/test/default-drive-protocol-versions.test.ts +79 -0
- package/test/default-drive-slug.test.ts +265 -0
- package/test/delegated-publish.test.ts +435 -0
- package/test/durable-node-fs-pg16.test.ts +80 -0
- package/test/fatal-shutdown.test.ts +205 -0
- package/test/goldens/packages.graphql +797 -0
- package/test/goldens/privacy.graphql +853 -0
- package/test/goldens/reactor-drive-nodes.graphql +827 -0
- package/test/goldens/resolvers.json +327 -0
- package/test/goldens/supergraph.graphql +5918 -0
- package/test/goldens/workflow-runtime.graphql +1548 -0
- package/test/inspection-throttle.test.ts +23 -0
- package/test/keyframe-reclaim-store.test.ts +156 -0
- package/test/observability-signals.test.ts +131 -0
- package/test/permissions-backfill.test.ts +486 -0
- package/test/pglite-dialect.test.ts +237 -2
- package/test/pglite-preflight.test.ts +313 -0
- package/test/pglite-storage-dirs.test.ts +113 -0
- package/test/pglite-store.test.ts +441 -0
- package/test/pglite-version.test.ts +9 -1
- package/test/plugin-config-schemas.test.ts +83 -0
- package/test/plugin-presence.test.ts +262 -0
- package/test/port-probe.test.ts +45 -0
- package/test/processor-timing-log.test.ts +109 -0
- package/test/reactor-feature-flags.test.ts +20 -0
- package/test/redact-webhook-path.test.ts +62 -0
- package/test/renown-storage-path.test.ts +40 -0
- package/test/renown-trust-policy.test.ts +313 -0
- package/test/served-schemas.test.ts +134 -0
- package/test/shutdown.test.ts +278 -0
- package/test/snapshot-writer.ts +94 -0
- package/test/store-lock.test.ts +65 -0
- package/test/two-switchboards.test.ts +146 -0
- package/test/unsupported-stored-documents.test.ts +72 -0
- package/test/worker-pool.test.ts +27 -1
- package/test/workflow/harness.ts +289 -0
- package/test/workflow/oauth-apps-config.test.ts +66 -0
- package/test/workflow/oauth.test.ts +286 -0
- package/test/workflow/resolvers.test.ts +488 -0
- package/test/workflow/runtime-host.test.ts +19 -0
- package/test/workflow/waitpoint-routes.test.ts +306 -0
- package/test/workflow/waitpoint-runtime.test.ts +204 -0
- package/test/workflow/webhook-dedupe.test.ts +295 -0
- package/test/workflow/webhook-signed.test.ts +509 -0
- package/test/workflow/webhook-trigger-test.test.ts +82 -0
- package/test/workflow/webhook-unsigned.test.ts +345 -0
- package/test/workflow-reactor-access.test.ts +384 -0
- package/test/workflow-reactor-enforcement.test.ts +808 -0
- package/test/workflow-read-gate.test.ts +213 -0
- package/test/workflow-runtime.test.ts +1254 -0
- package/test/workflows-plugin-config.test.ts +77 -0
- package/tsconfig.json +11 -2
- package/tsdown.config.ts +13 -1
- package/vitest.config.ts +65 -0
- package/dist/server-B2r6AQ4Q.mjs +0 -1553
- package/dist/server-B2r6AQ4Q.mjs.map +0 -1
- package/dist/utils-Baw7rThP.mjs +0 -80
- package/dist/utils-Baw7rThP.mjs.map +0 -1
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
// The AI gateway on a booted Switchboard: real did:key bearers, the real
|
|
2
|
+
// encrypted secret store, and a stub provider that records what it was sent
|
|
3
|
+
// and echoes the credentials back wherever a careless proxy would repeat them.
|
|
4
|
+
import { randomBytes } from "node:crypto";
|
|
5
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
6
|
+
import {
|
|
7
|
+
createServer,
|
|
8
|
+
type IncomingMessage,
|
|
9
|
+
type Server,
|
|
10
|
+
type ServerResponse,
|
|
11
|
+
} from "node:http";
|
|
12
|
+
import type { AddressInfo } from "node:net";
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import type { HostPlugin, ResolvedPlugin } from "@powerhousedao/host-core";
|
|
16
|
+
import { MemoryKeyStorage, RenownCryptoBuilder } from "@renown/sdk/node";
|
|
17
|
+
import type { ILogger } from "document-model";
|
|
18
|
+
import {
|
|
19
|
+
afterAll,
|
|
20
|
+
afterEach,
|
|
21
|
+
beforeAll,
|
|
22
|
+
describe,
|
|
23
|
+
expect,
|
|
24
|
+
it,
|
|
25
|
+
vi,
|
|
26
|
+
} from "vitest";
|
|
27
|
+
import { maskKey } from "../src/ai/key-store.mjs";
|
|
28
|
+
import { redactSecrets } from "../src/ai/routes.mjs";
|
|
29
|
+
import { createAiPlugin } from "../src/plugins/ai.mjs";
|
|
30
|
+
import { workflowsPlugin } from "../src/plugins/workflows.mjs";
|
|
31
|
+
import type { SwitchboardReactor } from "../src/types.js";
|
|
32
|
+
import { bootSwitchboard } from "./boot-helpers.js";
|
|
33
|
+
|
|
34
|
+
const ADMIN = "0x1111111111111111111111111111111111111111";
|
|
35
|
+
const USER = "0x2222222222222222222222222222222222222222";
|
|
36
|
+
const KEY = `sk-or-v1-${randomBytes(24).toString("hex")}`;
|
|
37
|
+
const OTHER_KEY = `sk-or-v1-${randomBytes(24).toString("hex")}`;
|
|
38
|
+
const BAD_KEY = `sk-or-v1-${randomBytes(24).toString("hex")}`;
|
|
39
|
+
const DEMO_KEY = `sk-or-v1-${randomBytes(24).toString("hex")}`;
|
|
40
|
+
const DEFAULT_MODEL = "qwen/test-vision";
|
|
41
|
+
const MODELS = { data: [{ id: DEFAULT_MODEL }, { id: "qwen/other" }] };
|
|
42
|
+
const GATEWAY = "/api/@powerhousedao/switchboard/ai";
|
|
43
|
+
|
|
44
|
+
const ENV_KEYS = [
|
|
45
|
+
"PH_REACTOR_DATABASE_URL",
|
|
46
|
+
"PH_WORKFLOWS_SECRETS_MASTER_KEY",
|
|
47
|
+
"MCP_ENABLED",
|
|
48
|
+
"FEATURE_PLUGIN_HOST_ENABLED",
|
|
49
|
+
"AUTH_ENABLED",
|
|
50
|
+
"ADMINS",
|
|
51
|
+
"RESOLVE_CALLER_IDENTITY",
|
|
52
|
+
"SKIP_CREDENTIAL_VERIFICATION",
|
|
53
|
+
] as const;
|
|
54
|
+
|
|
55
|
+
const logger = {
|
|
56
|
+
verbose: vi.fn(),
|
|
57
|
+
debug: vi.fn(),
|
|
58
|
+
info: vi.fn(),
|
|
59
|
+
warn: vi.fn(),
|
|
60
|
+
error: vi.fn(),
|
|
61
|
+
child: () => logger,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
type Seen = { path: string; authorization?: string; body: string };
|
|
65
|
+
type Stub = (req: IncomingMessage, res: ServerResponse, seen: Seen) => void;
|
|
66
|
+
|
|
67
|
+
const seen: Seen[] = [];
|
|
68
|
+
let answerChat: Stub | undefined;
|
|
69
|
+
let chatClosed: Promise<void> | undefined;
|
|
70
|
+
|
|
71
|
+
/** Echoes the credential in the body, as a careless upstream error might. */
|
|
72
|
+
function echoRefusal(
|
|
73
|
+
res: ServerResponse,
|
|
74
|
+
status: number,
|
|
75
|
+
authorization: string | undefined,
|
|
76
|
+
) {
|
|
77
|
+
res.statusCode = status;
|
|
78
|
+
res.setHeader("content-type", "application/json");
|
|
79
|
+
res.end(
|
|
80
|
+
JSON.stringify({
|
|
81
|
+
error: { message: `Refused ${authorization ?? ""}`, code: "refused" },
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function stubProvider(req: IncomingMessage, res: ServerResponse): void {
|
|
87
|
+
const chunks: Buffer[] = [];
|
|
88
|
+
req.on("data", (chunk: Buffer) => chunks.push(chunk));
|
|
89
|
+
req.on("end", () => {
|
|
90
|
+
const entry: Seen = {
|
|
91
|
+
path: req.url ?? "",
|
|
92
|
+
authorization: req.headers.authorization,
|
|
93
|
+
body: Buffer.concat(chunks).toString("utf8"),
|
|
94
|
+
};
|
|
95
|
+
seen.push(entry);
|
|
96
|
+
if (req.url === "/key") {
|
|
97
|
+
if (req.headers.authorization === `Bearer ${BAD_KEY}`) {
|
|
98
|
+
echoRefusal(res, 401, req.headers.authorization);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
res.setHeader("content-type", "application/json");
|
|
102
|
+
res.end(JSON.stringify({ data: { label: "stub" } }));
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (req.url === "/models") {
|
|
106
|
+
res.setHeader("content-type", "application/json");
|
|
107
|
+
res.end(JSON.stringify(MODELS));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (answerChat) {
|
|
111
|
+
answerChat(req, res, entry);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
res.setHeader("content-type", "text/event-stream");
|
|
115
|
+
res.end('data: {"choices":[]}\n\ndata: [DONE]\n\n');
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let tempRoot = "";
|
|
120
|
+
let previous: [string, string | undefined][] = [];
|
|
121
|
+
let switchboard: SwitchboardReactor | undefined;
|
|
122
|
+
let provider: Server;
|
|
123
|
+
const tokens: Record<string, string> = {};
|
|
124
|
+
const consoleLines: string[] = [];
|
|
125
|
+
|
|
126
|
+
const resolved = (plugin: HostPlugin, module: string): ResolvedPlugin => ({
|
|
127
|
+
plugin,
|
|
128
|
+
provenance: "built-in",
|
|
129
|
+
entry: { enabled: true, verbs: "*", config: {}, module },
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
async function bearerFor(address: string): Promise<string> {
|
|
133
|
+
const crypto = await new RenownCryptoBuilder()
|
|
134
|
+
.withKeyPairStorage(new MemoryKeyStorage())
|
|
135
|
+
.build();
|
|
136
|
+
return crypto.getBearerToken(address);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
type Caller = typeof ADMIN | typeof USER | "anonymous";
|
|
140
|
+
|
|
141
|
+
function call(
|
|
142
|
+
caller: Caller,
|
|
143
|
+
path: string,
|
|
144
|
+
init: { method?: string; body?: string; signal?: AbortSignal } = {},
|
|
145
|
+
): Promise<Response> {
|
|
146
|
+
return fetch(`http://127.0.0.1:${switchboard!.port}${GATEWAY}${path}`, {
|
|
147
|
+
...init,
|
|
148
|
+
headers: {
|
|
149
|
+
"content-type": "application/json",
|
|
150
|
+
...(caller === "anonymous"
|
|
151
|
+
? {}
|
|
152
|
+
: { authorization: `Bearer ${tokens[caller]}` }),
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const setKey = (value: string) =>
|
|
158
|
+
call(ADMIN, "/key", { method: "PUT", body: JSON.stringify({ value }) });
|
|
159
|
+
|
|
160
|
+
const chat = (body: unknown, signal?: AbortSignal) =>
|
|
161
|
+
call(ADMIN, "/v1/chat/completions", {
|
|
162
|
+
method: "POST",
|
|
163
|
+
body: JSON.stringify(body),
|
|
164
|
+
signal,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const lastChat = () => seen.findLast((s) => s.path === "/chat/completions")!;
|
|
168
|
+
|
|
169
|
+
beforeAll(async () => {
|
|
170
|
+
provider = createServer(stubProvider);
|
|
171
|
+
await new Promise<void>((resolve) =>
|
|
172
|
+
provider.listen(0, "127.0.0.1", resolve),
|
|
173
|
+
);
|
|
174
|
+
const upstreamUrl = `http://127.0.0.1:${(provider.address() as AddressInfo).port}`;
|
|
175
|
+
|
|
176
|
+
tempRoot = await mkdtemp(join(tmpdir(), "switchboard-ai-gateway-"));
|
|
177
|
+
previous = ENV_KEYS.map((name) => [name, process.env[name]]);
|
|
178
|
+
for (const name of ENV_KEYS) delete process.env[name];
|
|
179
|
+
process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
|
|
180
|
+
process.env.PH_WORKFLOWS_SECRETS_MASTER_KEY = randomBytes(32).toString("hex");
|
|
181
|
+
process.env.FEATURE_PLUGIN_HOST_ENABLED = "1";
|
|
182
|
+
const configFile = join(tempRoot, "powerhouse.config.json");
|
|
183
|
+
await writeFile(
|
|
184
|
+
configFile,
|
|
185
|
+
JSON.stringify({ ai: { demoKeyFile: "./demo-key.txt" } }),
|
|
186
|
+
);
|
|
187
|
+
await writeFile(join(tempRoot, "demo-key.txt"), `${DEMO_KEY}\n`);
|
|
188
|
+
|
|
189
|
+
for (const method of ["log", "info", "warn", "error"] as const) {
|
|
190
|
+
vi.spyOn(console, method).mockImplementation((...args: unknown[]) => {
|
|
191
|
+
consoleLines.push(args.map(String).join(" "));
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
switchboard = await bootSwitchboard(
|
|
196
|
+
{
|
|
197
|
+
configFile,
|
|
198
|
+
server: { port: 0 },
|
|
199
|
+
storage: { readModel: { url: join(tempRoot, "read-model") } },
|
|
200
|
+
features: { workflows: true, mcp: false },
|
|
201
|
+
packages: { disableLocal: true },
|
|
202
|
+
identity: { keypairPath: join(tempRoot, "identity.json") },
|
|
203
|
+
auth: {
|
|
204
|
+
enabled: true,
|
|
205
|
+
admins: [ADMIN],
|
|
206
|
+
skipCredentialVerification: true,
|
|
207
|
+
renown: { switchboardUrl: "http://127.0.0.1:9/graphql" },
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
logger: logger as unknown as ILogger,
|
|
212
|
+
pluginComposition: [
|
|
213
|
+
resolved(
|
|
214
|
+
workflowsPlugin as HostPlugin,
|
|
215
|
+
"@powerhousedao/switchboard/plugins/workflows",
|
|
216
|
+
),
|
|
217
|
+
resolved(
|
|
218
|
+
createAiPlugin({
|
|
219
|
+
defaultModel: DEFAULT_MODEL,
|
|
220
|
+
maxOutputTokens: 1000,
|
|
221
|
+
upstreamUrl,
|
|
222
|
+
}) as HostPlugin,
|
|
223
|
+
"@powerhousedao/switchboard/plugins/ai",
|
|
224
|
+
),
|
|
225
|
+
],
|
|
226
|
+
},
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
tokens[ADMIN] = await bearerFor(ADMIN);
|
|
230
|
+
tokens[USER] = await bearerFor(USER);
|
|
231
|
+
}, 120_000);
|
|
232
|
+
|
|
233
|
+
afterAll(async () => {
|
|
234
|
+
await switchboard?.shutdown();
|
|
235
|
+
await new Promise((resolve) => provider.close(resolve));
|
|
236
|
+
vi.restoreAllMocks();
|
|
237
|
+
for (const [name, value] of previous) {
|
|
238
|
+
if (value === undefined) delete process.env[name];
|
|
239
|
+
else process.env[name] = value;
|
|
240
|
+
}
|
|
241
|
+
await rm(tempRoot, { recursive: true, force: true });
|
|
242
|
+
}, 60_000);
|
|
243
|
+
|
|
244
|
+
afterEach(() => {
|
|
245
|
+
answerChat = undefined;
|
|
246
|
+
chatClosed = undefined;
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe("the gateway on a signed-in host", () => {
|
|
250
|
+
it("answers only admins, on every route", async () => {
|
|
251
|
+
const routes: [string, string][] = [
|
|
252
|
+
["GET", "/status"],
|
|
253
|
+
["PUT", "/key"],
|
|
254
|
+
["PUT", "/key/demo"],
|
|
255
|
+
["DELETE", "/key"],
|
|
256
|
+
["PUT", "/model"],
|
|
257
|
+
["GET", "/v1/models"],
|
|
258
|
+
["POST", "/v1/chat/completions"],
|
|
259
|
+
];
|
|
260
|
+
for (const [method, path] of routes) {
|
|
261
|
+
const body = method === "GET" ? undefined : "{}";
|
|
262
|
+
const anonymous = await call("anonymous", path, { method, body });
|
|
263
|
+
expect([method, path, anonymous.status]).toEqual([method, path, 401]);
|
|
264
|
+
const user = await call(USER, path, { method, body });
|
|
265
|
+
expect([method, path, user.status]).toEqual([method, path, 403]);
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("reports not configured, and the chat refuses until a key is set", async () => {
|
|
270
|
+
const status = await call(ADMIN, "/status");
|
|
271
|
+
expect(status.status).toBe(200);
|
|
272
|
+
expect(await status.json()).toEqual({
|
|
273
|
+
configured: false,
|
|
274
|
+
keyHint: null,
|
|
275
|
+
keySetBy: null,
|
|
276
|
+
keySetAt: null,
|
|
277
|
+
model: DEFAULT_MODEL,
|
|
278
|
+
demoKeyAvailable: true,
|
|
279
|
+
});
|
|
280
|
+
const refused = await chat({ messages: [] });
|
|
281
|
+
expect(refused.status).toBe(503);
|
|
282
|
+
expect(await refused.text()).toContain("not_configured");
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("does not store a key the provider refuses, nor repeat it", async () => {
|
|
286
|
+
const answer = await setKey(BAD_KEY);
|
|
287
|
+
expect(answer.status).toBe(400);
|
|
288
|
+
expect(await answer.text()).not.toContain(BAD_KEY);
|
|
289
|
+
const status = (await (await call(ADMIN, "/status")).json()) as {
|
|
290
|
+
configured: boolean;
|
|
291
|
+
};
|
|
292
|
+
expect(status.configured).toBe(false);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("does not repeat a malformed body that holds a key", async () => {
|
|
296
|
+
const answer = await call(ADMIN, "/key", {
|
|
297
|
+
method: "PUT",
|
|
298
|
+
body: `${BAD_KEY} is not JSON`,
|
|
299
|
+
});
|
|
300
|
+
expect(answer.status).toBe(400);
|
|
301
|
+
expect(await answer.text()).not.toContain(BAD_KEY);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("stores a key the provider accepts and shows only a hint", async () => {
|
|
305
|
+
const answer = await setKey(KEY);
|
|
306
|
+
expect(answer.status).toBe(200);
|
|
307
|
+
const text = await answer.text();
|
|
308
|
+
expect(text).not.toContain(KEY);
|
|
309
|
+
const status = JSON.parse(text) as Record<string, unknown>;
|
|
310
|
+
expect(status).toMatchObject({
|
|
311
|
+
configured: true,
|
|
312
|
+
keyHint: `sk-or-v1-…${KEY.slice(-4)}`,
|
|
313
|
+
keySetBy: ADMIN,
|
|
314
|
+
model: DEFAULT_MODEL,
|
|
315
|
+
});
|
|
316
|
+
expect(seen.at(-1)).toMatchObject({
|
|
317
|
+
path: "/key",
|
|
318
|
+
authorization: `Bearer ${KEY}`,
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("keeps the working key when a replacement is refused", async () => {
|
|
323
|
+
expect((await setKey(BAD_KEY)).status).toBe(400);
|
|
324
|
+
const status = (await (await call(ADMIN, "/status")).json()) as {
|
|
325
|
+
keyHint: string;
|
|
326
|
+
};
|
|
327
|
+
expect(status.keyHint).toContain(KEY.slice(-4));
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("forwards a chat with the stored key, the host's model and capped tokens", async () => {
|
|
331
|
+
const answer = await chat({
|
|
332
|
+
model: "someone/expensive",
|
|
333
|
+
provider: { order: ["x"] },
|
|
334
|
+
max_tokens: 999_999,
|
|
335
|
+
stream: true,
|
|
336
|
+
messages: [
|
|
337
|
+
{
|
|
338
|
+
role: "user",
|
|
339
|
+
content: [
|
|
340
|
+
{ type: "text", text: "What is this?" },
|
|
341
|
+
{
|
|
342
|
+
type: "image_url",
|
|
343
|
+
image_url: { url: "data:image/png;base64,AAAA" },
|
|
344
|
+
},
|
|
345
|
+
],
|
|
346
|
+
},
|
|
347
|
+
],
|
|
348
|
+
});
|
|
349
|
+
expect(answer.status).toBe(200);
|
|
350
|
+
expect(answer.headers.get("content-type")).toContain("text/event-stream");
|
|
351
|
+
await answer.text();
|
|
352
|
+
|
|
353
|
+
const forwarded = lastChat();
|
|
354
|
+
expect(forwarded.authorization).toBe(`Bearer ${KEY}`);
|
|
355
|
+
const body = JSON.parse(forwarded.body) as Record<string, unknown>;
|
|
356
|
+
expect(body.model).toBe(DEFAULT_MODEL);
|
|
357
|
+
expect(body.max_tokens).toBe(1000);
|
|
358
|
+
expect(body.provider).toBeUndefined();
|
|
359
|
+
expect(JSON.stringify(body.messages)).toContain(
|
|
360
|
+
"data:image/png;base64,AAAA",
|
|
361
|
+
);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("streams the answer through as it is produced", async () => {
|
|
365
|
+
let finish = () => {};
|
|
366
|
+
const finishing = new Promise<void>((resolve) => {
|
|
367
|
+
finish = resolve;
|
|
368
|
+
});
|
|
369
|
+
answerChat = (_req, res) => {
|
|
370
|
+
res.setHeader("content-type", "text/event-stream");
|
|
371
|
+
res.write("data: first\n\n");
|
|
372
|
+
void finishing.then(() => res.end("data: second\n\n"));
|
|
373
|
+
};
|
|
374
|
+
const answer = await chat({ messages: [], stream: true });
|
|
375
|
+
const reader = (answer.body as ReadableStream<Uint8Array>).getReader();
|
|
376
|
+
const first = await reader.read();
|
|
377
|
+
// The first event arrived while the provider is still holding the rest.
|
|
378
|
+
expect(new TextDecoder().decode(first.value)).toContain("first");
|
|
379
|
+
finish();
|
|
380
|
+
let rest = "";
|
|
381
|
+
for (;;) {
|
|
382
|
+
const next = await reader.read();
|
|
383
|
+
if (next.done) break;
|
|
384
|
+
rest += new TextDecoder().decode(next.value);
|
|
385
|
+
}
|
|
386
|
+
expect(rest).toContain("second");
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it("stops the provider's call when the client goes away", async () => {
|
|
390
|
+
chatClosed = new Promise<void>((resolve) => {
|
|
391
|
+
answerChat = (_req, res) => {
|
|
392
|
+
res.setHeader("content-type", "text/event-stream");
|
|
393
|
+
res.write("data: first\n\n");
|
|
394
|
+
res.on("close", resolve);
|
|
395
|
+
};
|
|
396
|
+
});
|
|
397
|
+
const controller = new AbortController();
|
|
398
|
+
const answer = await chat(
|
|
399
|
+
{ messages: [], stream: true },
|
|
400
|
+
controller.signal,
|
|
401
|
+
);
|
|
402
|
+
await answer.body!.getReader().read();
|
|
403
|
+
controller.abort();
|
|
404
|
+
await chatClosed;
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("keeps the provider's status but never echoes the key from an error", async () => {
|
|
408
|
+
answerChat = (req, res) => echoRefusal(res, 429, req.headers.authorization);
|
|
409
|
+
const answer = await chat({ messages: [] });
|
|
410
|
+
expect(answer.status).toBe(429);
|
|
411
|
+
const text = await answer.text();
|
|
412
|
+
expect(text).toContain("Refused");
|
|
413
|
+
expect(text).not.toContain(KEY);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it("does not pass on an error body that is not JSON", async () => {
|
|
417
|
+
answerChat = (req, res) => {
|
|
418
|
+
res.statusCode = 500;
|
|
419
|
+
res.end(`upstream trace, headers: ${req.headers.authorization}`);
|
|
420
|
+
};
|
|
421
|
+
const answer = await chat({ messages: [] });
|
|
422
|
+
expect(answer.status).toBe(500);
|
|
423
|
+
expect(await answer.text()).not.toContain(KEY);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it("lists the provider's models", async () => {
|
|
427
|
+
const answer = await call(ADMIN, "/v1/models");
|
|
428
|
+
expect(answer.status).toBe(200);
|
|
429
|
+
expect(await answer.json()).toEqual(MODELS);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it("takes only a model the provider has, and chats with it", async () => {
|
|
433
|
+
const unknown = await call(ADMIN, "/model", {
|
|
434
|
+
method: "PUT",
|
|
435
|
+
body: JSON.stringify({ model: "nobody/nothing" }),
|
|
436
|
+
});
|
|
437
|
+
expect(unknown.status).toBe(400);
|
|
438
|
+
|
|
439
|
+
const known = await call(ADMIN, "/model", {
|
|
440
|
+
method: "PUT",
|
|
441
|
+
body: JSON.stringify({ model: "qwen/other" }),
|
|
442
|
+
});
|
|
443
|
+
expect(known.status).toBe(200);
|
|
444
|
+
expect(await known.json()).toMatchObject({ model: "qwen/other" });
|
|
445
|
+
|
|
446
|
+
await (await chat({ messages: [] })).text();
|
|
447
|
+
expect(JSON.parse(lastChat().body)).toMatchObject({ model: "qwen/other" });
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it("rotates on a second set, so the old key stops being used", async () => {
|
|
451
|
+
expect((await setKey(OTHER_KEY)).status).toBe(200);
|
|
452
|
+
await (await chat({ messages: [] })).text();
|
|
453
|
+
expect(lastChat().authorization).toBe(`Bearer ${OTHER_KEY}`);
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it("forgets the key on reset", async () => {
|
|
457
|
+
const answer = await call(ADMIN, "/key", { method: "DELETE" });
|
|
458
|
+
expect(answer.status).toBe(200);
|
|
459
|
+
expect(await answer.json()).toMatchObject({
|
|
460
|
+
configured: false,
|
|
461
|
+
keyHint: null,
|
|
462
|
+
});
|
|
463
|
+
expect((await chat({ messages: [] })).status).toBe(503);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it("logged no key at any point", () => {
|
|
467
|
+
const logged = [
|
|
468
|
+
...consoleLines,
|
|
469
|
+
...[logger.debug, logger.info, logger.warn, logger.error].flatMap((fn) =>
|
|
470
|
+
fn.mock.calls.map((args) => JSON.stringify(args)),
|
|
471
|
+
),
|
|
472
|
+
].join("\n");
|
|
473
|
+
expect(logged.length).toBeGreaterThan(0);
|
|
474
|
+
for (const key of [KEY, OTHER_KEY, BAD_KEY]) {
|
|
475
|
+
expect(logged).not.toContain(key);
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
describe("the demo key", () => {
|
|
481
|
+
const demoFile = () => join(tempRoot, "demo-key.txt");
|
|
482
|
+
const useDemoKey = () => call(ADMIN, "/key/demo", { method: "PUT" });
|
|
483
|
+
const status = async () =>
|
|
484
|
+
(await (await call(ADMIN, "/status")).json()) as {
|
|
485
|
+
demoKeyAvailable: boolean;
|
|
486
|
+
keyHint: string;
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
afterEach(async () => {
|
|
490
|
+
await writeFile(demoFile(), DEMO_KEY);
|
|
491
|
+
await setKey(KEY);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("is tested and stored like any key, and never reaches the browser", async () => {
|
|
495
|
+
const answer = await useDemoKey();
|
|
496
|
+
const text = await answer.text();
|
|
497
|
+
|
|
498
|
+
expect(answer.status).toBe(200);
|
|
499
|
+
expect(text).not.toContain(DEMO_KEY);
|
|
500
|
+
expect(seen.at(-1)).toMatchObject({
|
|
501
|
+
path: "/key",
|
|
502
|
+
authorization: `Bearer ${DEMO_KEY}`,
|
|
503
|
+
});
|
|
504
|
+
expect((await status()).keyHint).toContain(DEMO_KEY.slice(-4));
|
|
505
|
+
await chat({ messages: [] });
|
|
506
|
+
expect(lastChat().authorization).toBe(`Bearer ${DEMO_KEY}`);
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it("keeps the working key when the provider refuses it", async () => {
|
|
510
|
+
await writeFile(demoFile(), BAD_KEY);
|
|
511
|
+
|
|
512
|
+
const answer = await useDemoKey();
|
|
513
|
+
|
|
514
|
+
expect(answer.status).toBe(400);
|
|
515
|
+
expect(await answer.text()).not.toContain(BAD_KEY);
|
|
516
|
+
expect((await status()).keyHint).toContain(KEY.slice(-4));
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it("isn't offered when the file has gone", async () => {
|
|
520
|
+
await rm(demoFile());
|
|
521
|
+
|
|
522
|
+
expect((await status()).demoKeyAvailable).toBe(false);
|
|
523
|
+
expect((await useDemoKey()).status).toBe(404);
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
describe("the plugin's mount", () => {
|
|
528
|
+
const fakeContext = (policy: string) => {
|
|
529
|
+
const route = vi.fn();
|
|
530
|
+
const warn = vi.fn();
|
|
531
|
+
return {
|
|
532
|
+
route,
|
|
533
|
+
warn,
|
|
534
|
+
ctx: {
|
|
535
|
+
logger: { warn },
|
|
536
|
+
options: {},
|
|
537
|
+
renown: null,
|
|
538
|
+
contributions: {},
|
|
539
|
+
api: {
|
|
540
|
+
authorizationService: {
|
|
541
|
+
config: { policy },
|
|
542
|
+
isSupremeAdmin: () => true,
|
|
543
|
+
},
|
|
544
|
+
httpRoutes: { scopeFor: () => ({ route }) },
|
|
545
|
+
},
|
|
546
|
+
} as never,
|
|
547
|
+
};
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
it("stays off without sign-in, where everyone would be an admin", () => {
|
|
551
|
+
const { ctx, route, warn } = fakeContext("OPEN");
|
|
552
|
+
createAiPlugin({ defaultModel: "m" }).hooks?.onHttpMounting?.(ctx);
|
|
553
|
+
expect(route).not.toHaveBeenCalled();
|
|
554
|
+
expect(warn).toHaveBeenCalledOnce();
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
it("mounts without sign-in only when told to", () => {
|
|
558
|
+
const { ctx, route } = fakeContext("OPEN");
|
|
559
|
+
createAiPlugin({
|
|
560
|
+
defaultModel: "m",
|
|
561
|
+
allowWithoutAuth: true,
|
|
562
|
+
}).hooks?.onHttpMounting?.(ctx);
|
|
563
|
+
expect(route).toHaveBeenCalled();
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
it("answers 503 while the workflow runtime is absent", async () => {
|
|
567
|
+
const { ctx, route } = fakeContext("ADMIN_ONLY");
|
|
568
|
+
createAiPlugin({ defaultModel: "m" }).hooks?.onHttpMounting?.(ctx);
|
|
569
|
+
const status = route.mock.calls
|
|
570
|
+
.map(
|
|
571
|
+
([spec]) =>
|
|
572
|
+
spec as {
|
|
573
|
+
method: string;
|
|
574
|
+
path: string;
|
|
575
|
+
handler: (r: Request, c: unknown) => Promise<Response>;
|
|
576
|
+
},
|
|
577
|
+
)
|
|
578
|
+
.find((spec) => spec.path === "ai/status")!;
|
|
579
|
+
const answer = await status.handler(
|
|
580
|
+
new Request("http://localhost/status"),
|
|
581
|
+
{},
|
|
582
|
+
);
|
|
583
|
+
expect(answer.status).toBe(503);
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
it("requires a default model and rejects a typo'd key", () => {
|
|
587
|
+
expect(() => createAiPlugin({})).toThrow();
|
|
588
|
+
expect(() => createAiPlugin({ defaultModel: "m", modle: "x" })).toThrow();
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
describe("masking", () => {
|
|
593
|
+
it("keeps the provider prefix and the last four", () => {
|
|
594
|
+
expect(maskKey("sk-or-v1-0123456789abcdef0123456789abcd")).toBe(
|
|
595
|
+
"sk-or-v1-…abcd",
|
|
596
|
+
);
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
it("shows no tail of a short value", () => {
|
|
600
|
+
expect(maskKey("sk-or-v1-abcd")).toBe("sk-or-v1-…");
|
|
601
|
+
expect(maskKey("short")).toBe("…");
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
it("removes the key and anything shaped like one", () => {
|
|
605
|
+
expect(
|
|
606
|
+
redactSecrets(
|
|
607
|
+
"a Bearer xyz b sk-or-v1-aaaabbbbcccc c opaque d",
|
|
608
|
+
"opaque",
|
|
609
|
+
),
|
|
610
|
+
).toBe("a Bearer [redacted] b [redacted] c [redacted] d");
|
|
611
|
+
});
|
|
612
|
+
});
|
|
@@ -35,7 +35,8 @@ import {
|
|
|
35
35
|
registerAttachmentReferenceReadModelOnModule,
|
|
36
36
|
} from "../src/attachment-reference-read-model.mjs";
|
|
37
37
|
import { applySwitchboardReactorDefaults } from "../src/builder-defaults.mjs";
|
|
38
|
-
import { startSwitchboard } from "../src/server.mjs";
|
|
38
|
+
import type { startSwitchboard } from "../src/server.mjs";
|
|
39
|
+
import { bootSwitchboard } from "./boot-helpers.js";
|
|
39
40
|
|
|
40
41
|
const REF_A = `attachment://v1:${"a".repeat(64)}` as const;
|
|
41
42
|
const REF_B = `attachment://v1:${"b".repeat(64)}` as const;
|
|
@@ -155,15 +156,16 @@ describe("Switchboard attachment-reference read model registration", () => {
|
|
|
155
156
|
let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
|
|
156
157
|
|
|
157
158
|
try {
|
|
158
|
-
switchboard = await
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
159
|
+
switchboard = await bootSwitchboard(
|
|
160
|
+
{
|
|
161
|
+
server: { port: 0 },
|
|
162
|
+
storage: { readModel: { url: readModelPath } },
|
|
163
|
+
features: { mcp: false },
|
|
164
|
+
packages: { disableLocal: true },
|
|
165
|
+
identity: { keypairPath: join(tempRoot, "identity.json") },
|
|
166
|
+
},
|
|
167
|
+
{ reactor: callerModule, logger },
|
|
168
|
+
);
|
|
167
169
|
|
|
168
170
|
expect(switchboard.reactor).toBe(callerModule.client);
|
|
169
171
|
expect(switchboard.attachmentReferenceProjection).toEqual({
|
|
@@ -232,6 +234,8 @@ describe("Switchboard attachment-reference read model registration", () => {
|
|
|
232
234
|
writtenOrdinals.push(...references.map(({ ordinal }) => ordinal));
|
|
233
235
|
await attachmentReferenceIndex.store.addReferences(references);
|
|
234
236
|
},
|
|
237
|
+
removeDocuments: (documentIds) =>
|
|
238
|
+
attachmentReferenceIndex.store.removeDocuments(documentIds),
|
|
235
239
|
};
|
|
236
240
|
|
|
237
241
|
const coordinator =
|
|
@@ -283,7 +287,10 @@ describe("Switchboard attachment-reference read model registration", () => {
|
|
|
283
287
|
.withReadModelCoordinator(customCoordinator)
|
|
284
288
|
.buildModule();
|
|
285
289
|
const addReferences = vi.fn();
|
|
286
|
-
const writer = {
|
|
290
|
+
const writer: IAttachmentReferenceWriter = {
|
|
291
|
+
addReferences,
|
|
292
|
+
removeDocuments: vi.fn(),
|
|
293
|
+
};
|
|
287
294
|
|
|
288
295
|
await expect(
|
|
289
296
|
registerAttachmentReferenceReadModelOnModule(
|
|
@@ -327,15 +334,16 @@ describe("Switchboard attachment-reference read model registration", () => {
|
|
|
327
334
|
let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
|
|
328
335
|
|
|
329
336
|
try {
|
|
330
|
-
switchboard = await
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
337
|
+
switchboard = await bootSwitchboard(
|
|
338
|
+
{
|
|
339
|
+
server: { port: 0 },
|
|
340
|
+
storage: { readModel: { url: join(tempRoot, "read-model") } },
|
|
341
|
+
features: { mcp: false },
|
|
342
|
+
packages: { disableLocal: true },
|
|
343
|
+
identity: { keypairPath: join(tempRoot, "identity.json") },
|
|
344
|
+
},
|
|
345
|
+
{ reactor: callerModule, logger: stubLogger() },
|
|
346
|
+
);
|
|
339
347
|
|
|
340
348
|
expect(switchboard.reactor).toBe(callerModule.client);
|
|
341
349
|
expect(switchboard.attachmentReferenceProjection).toEqual({
|