cartethyia 2.5.3 → 2.5.6
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/.env.example +13 -4
- package/README.md +2 -2
- package/dist/dashboard/assets/{BannedPage-hfCzi-Ux.js → BannedPage-CADDq4IU.js} +1 -1
- package/dist/dashboard/assets/BannedPage-CADDq4IU.js.gz +0 -0
- package/dist/dashboard/assets/CliToolDetailPage-_z1hID7E.js +1 -0
- package/dist/dashboard/assets/CliToolDetailPage-_z1hID7E.js.gz +0 -0
- package/dist/dashboard/assets/{CliToolsPage-DEDyKBlC.js → CliToolsPage-DuO1Z7dq.js} +1 -1
- package/dist/dashboard/assets/CliToolsPage-DuO1Z7dq.js.gz +0 -0
- package/dist/dashboard/assets/CombosPage-DNEPi3xU.js +1 -0
- package/dist/dashboard/assets/CombosPage-DNEPi3xU.js.gz +0 -0
- package/dist/dashboard/assets/{ConsoleLogPage-bsLuZmYH.js → ConsoleLogPage-BVdpfiz1.js} +1 -1
- package/dist/dashboard/assets/ConsoleLogPage-BVdpfiz1.js.gz +0 -0
- package/dist/dashboard/assets/{CustomizationPage-_UWhwziJ.js → CustomizationPage-BR5HHvbi.js} +1 -1
- package/dist/dashboard/assets/CustomizationPage-BR5HHvbi.js.gz +0 -0
- package/dist/dashboard/assets/HealthEventsModal-mUfcknqm.js +1 -0
- package/dist/dashboard/assets/HealthEventsModal-mUfcknqm.js.gz +0 -0
- package/dist/dashboard/assets/{LoginPage-DDVIfZGW.js → LoginPage-V5faV9V2.js} +1 -1
- package/dist/dashboard/assets/LoginPage-V5faV9V2.js.gz +0 -0
- package/dist/dashboard/assets/ModelPicker-C7pDb41R.js +1 -0
- package/dist/dashboard/assets/ModelPicker-C7pDb41R.js.gz +0 -0
- package/dist/dashboard/assets/OverviewPage-BDu20Jdx.js +1 -0
- package/dist/dashboard/assets/OverviewPage-BDu20Jdx.js.gz +0 -0
- package/dist/dashboard/assets/ProviderDetailPage-Dllr_aFO.js +3 -0
- package/dist/dashboard/assets/ProviderDetailPage-Dllr_aFO.js.gz +0 -0
- package/dist/dashboard/assets/ProvidersPage-uPiGVTTK.js +1 -0
- package/dist/dashboard/assets/ProvidersPage-uPiGVTTK.js.gz +0 -0
- package/dist/dashboard/assets/ProxyPage-BaUkO-4O.js +7 -0
- package/dist/dashboard/assets/ProxyPage-BaUkO-4O.js.gz +0 -0
- package/dist/dashboard/assets/QuotaPage-o0IFAsFb.js +1 -0
- package/dist/dashboard/assets/QuotaPage-o0IFAsFb.js.gz +0 -0
- package/dist/dashboard/assets/SettingsPage-CYWqbXjp.js +1 -0
- package/dist/dashboard/assets/SettingsPage-CYWqbXjp.js.gz +0 -0
- package/dist/dashboard/assets/{SetupPage-D6pmQ86L.js → SetupPage-UB690SE2.js} +1 -1
- package/dist/dashboard/assets/SetupPage-UB690SE2.js.gz +0 -0
- package/dist/dashboard/assets/{SortableList-DWvKiuSl.js → SortableList-BLujdX7N.js} +1 -1
- package/dist/dashboard/assets/SortableList-BLujdX7N.js.gz +0 -0
- package/dist/dashboard/assets/{StudioPage-CmkUrgdj.js → StudioPage-D5C2raAc.js} +3 -3
- package/dist/dashboard/assets/StudioPage-D5C2raAc.js.gz +0 -0
- package/dist/dashboard/assets/UsagePage-8uIcxa1W.js +56 -0
- package/dist/dashboard/assets/UsagePage-8uIcxa1W.js.gz +0 -0
- package/dist/dashboard/assets/api-keys-DQYLQxpW.js +1 -0
- package/dist/dashboard/assets/api-keys-DQYLQxpW.js.gz +0 -0
- package/dist/dashboard/assets/app-CX_Z8cMs.css +2 -0
- package/dist/dashboard/assets/app-CX_Z8cMs.css.gz +0 -0
- package/dist/dashboard/assets/{assert-NHwYq95t.js → assert-C9FD_JwJ.js} +1 -1
- package/dist/dashboard/assets/assert-C9FD_JwJ.js.gz +0 -0
- package/dist/dashboard/assets/{badge-6uzDDvqs.js → badge-CR72dasT.js} +1 -1
- package/dist/dashboard/assets/badge-CR72dasT.js.gz +0 -0
- package/dist/dashboard/assets/{banner-4_WNg2WZ.js → banner-CI9_eSfO.js} +1 -1
- package/dist/dashboard/assets/banner-CI9_eSfO.js.gz +0 -0
- package/dist/dashboard/assets/{card--AtFedAq.js → card-DS7CQEsO.js} +1 -1
- package/dist/dashboard/assets/card-DS7CQEsO.js.gz +0 -0
- package/dist/dashboard/assets/{cli-tools-B3e7dx3N.js → cli-tools-CNqzrfdb.js} +1 -1
- package/dist/dashboard/assets/cli-tools-CNqzrfdb.js.gz +0 -0
- package/dist/dashboard/assets/{clipboard-button-NreUx6c8.js → clipboard-button-611pSsaX.js} +1 -1
- package/dist/dashboard/assets/clipboard-button-611pSsaX.js.gz +0 -0
- package/dist/dashboard/assets/{fingerprint-pattern-BWuHtbW4.js → concurrency-BDF0ok7y.js} +1 -1
- package/dist/dashboard/assets/concurrency-BDF0ok7y.js.gz +0 -0
- package/dist/dashboard/assets/database-Dz9N3zGd.js +1 -0
- package/dist/dashboard/assets/database-Dz9N3zGd.js.gz +0 -0
- package/dist/dashboard/assets/dialog-k37exjNt.js +1 -0
- package/dist/dashboard/assets/dialog-k37exjNt.js.gz +0 -0
- package/dist/dashboard/assets/drawer-CX7N8ucR.js +1 -0
- package/dist/dashboard/assets/drawer-CX7N8ucR.js.gz +0 -0
- package/dist/dashboard/assets/entry-B9Xvtnfj.js +1 -0
- package/dist/dashboard/assets/entry-B9Xvtnfj.js.gz +0 -0
- package/dist/dashboard/assets/entry-CTP8AyiS.js +1 -0
- package/dist/dashboard/assets/entry-CTP8AyiS.js.gz +0 -0
- package/dist/dashboard/assets/entry-DxBHnauy.js +4 -0
- package/dist/dashboard/assets/entry-DxBHnauy.js.gz +0 -0
- package/dist/dashboard/assets/{github-badge-D_eM3IgG.js → github-badge-sltk1qUi.js} +1 -1
- package/dist/dashboard/assets/github-badge-sltk1qUi.js.gz +0 -0
- package/dist/dashboard/assets/{index-BydyKaDz.js → index-CmxZr14F.js} +2 -2
- package/dist/dashboard/assets/index-CmxZr14F.js.gz +0 -0
- package/dist/dashboard/assets/{inline-BcNGF8Gr.js → inline-BhY2sSnO.js} +1 -1
- package/dist/dashboard/assets/inline-BhY2sSnO.js.gz +0 -0
- package/dist/dashboard/assets/{input-BG41et9G.js → input-V1umFiBk.js} +1 -1
- package/dist/dashboard/assets/input-V1umFiBk.js.gz +0 -0
- package/dist/dashboard/assets/layout-BBOYycyS.js +1 -0
- package/dist/dashboard/assets/layout-BBOYycyS.js.gz +0 -0
- package/dist/dashboard/assets/{list-row-Gh_W4OR_.js → list-row-1zzZYM0B.js} +1 -1
- package/dist/dashboard/assets/list-row-1zzZYM0B.js.gz +0 -0
- package/dist/dashboard/assets/{live-BJGYRBnR.js → live-BivGfuUs.js} +1 -1
- package/dist/dashboard/assets/live-BivGfuUs.js.gz +0 -0
- package/dist/dashboard/assets/model-bans-CmffjLuG.js +1 -0
- package/dist/dashboard/assets/model-bans-CmffjLuG.js.gz +0 -0
- package/dist/dashboard/assets/{mutation-BkoLM5Ba.js → mutation-46P0oWMe.js} +1 -1
- package/dist/dashboard/assets/mutation-46P0oWMe.js.gz +0 -0
- package/dist/dashboard/assets/numbers-DkfMR5Qs.js +2 -0
- package/dist/dashboard/assets/numbers-DkfMR5Qs.js.gz +0 -0
- package/dist/dashboard/assets/{query-keys-D7wX2vM5.js → query-keys-BFhGpwR1.js} +1 -1
- package/dist/dashboard/assets/query-keys-BFhGpwR1.js.gz +0 -0
- package/dist/dashboard/assets/{routing-CcusqXA7.js → routing-CAb4F37R.js} +1 -1
- package/dist/dashboard/assets/routing-CAb4F37R.js.gz +0 -0
- package/dist/dashboard/assets/{select-BO6h8yNv.js → select-BYR_5K2c.js} +2 -2
- package/dist/dashboard/assets/select-BYR_5K2c.js.gz +0 -0
- package/dist/dashboard/assets/{settings-mpmXTgUa.js → settings-DNX9sydi.js} +1 -1
- package/dist/dashboard/assets/settings-DNX9sydi.js.gz +0 -0
- package/dist/dashboard/assets/{stack-DwLmjUFK.js → stack-BHTfXdz_.js} +1 -1
- package/dist/dashboard/assets/stack-BHTfXdz_.js.gz +0 -0
- package/dist/dashboard/assets/{state-DYdQmdAl.js → state-mPDRlQkl.js} +1 -1
- package/dist/dashboard/assets/state-mPDRlQkl.js.gz +0 -0
- package/dist/dashboard/assets/switch-DOmJxg7H.js +1 -0
- package/dist/dashboard/assets/switch-DOmJxg7H.js.gz +0 -0
- package/dist/dashboard/assets/terminal-0_IjPWTk.js +1 -0
- package/dist/dashboard/assets/terminal-0_IjPWTk.js.gz +0 -0
- package/dist/dashboard/assets/tokens-COzario9.js +1 -0
- package/dist/dashboard/assets/tokens-COzario9.js.gz +0 -0
- package/dist/dashboard/assets/use-modal-focus-By7oJoGd.js +1 -0
- package/dist/dashboard/assets/use-modal-focus-By7oJoGd.js.gz +0 -0
- package/dist/dashboard/assets/{useMutation-w4u4CVue.js → useMutation-Dem0lXQj.js} +1 -1
- package/dist/dashboard/assets/useMutation-Dem0lXQj.js.gz +0 -0
- package/dist/dashboard/assets/zap-C4EeVW4q.js +1 -0
- package/dist/dashboard/assets/zap-C4EeVW4q.js.gz +0 -0
- package/dist/dashboard/donation/qris-risun-marke.png +0 -0
- package/dist/dashboard/index.html +1 -1
- package/dist/dashboard/index.html.gz +0 -0
- package/dist/dashboard/precompressed-assets.json +57 -57
- package/migrations/README.md +52 -52
- package/migrations/lite-sqlite3/0000_baseline.sql +2 -2
- package/package.json +96 -95
- package/scripts/cli/admin.ts +125 -0
- package/scripts/cli/autostart.ts +237 -0
- package/scripts/cli/{ui/background.ts → background.ts} +1 -1
- package/scripts/cli/{command/console-menu.ts → console-menu.ts} +275 -248
- package/scripts/cli/doctor.ts +37 -0
- package/scripts/cli/{support/env.ts → env.ts} +99 -99
- package/scripts/cli/{gateway/spawn.ts → gateway.ts} +112 -24
- package/scripts/cli/instance.ts +224 -0
- package/scripts/cli/main.ts +88 -18
- package/scripts/cli/{support/platform.ts → platform.ts} +64 -64
- package/scripts/cli/port.ts +100 -0
- package/scripts/cli/{support/services.ts → services.ts} +88 -88
- package/scripts/cli/{command/setup.ts → setup.ts} +35 -34
- package/scripts/cli/{command/start.ts → start.ts} +10 -5
- package/scripts/cli/{support/startup-signal.ts → startup-signal.ts} +23 -23
- package/scripts/cli/tray/index.ts +62 -12
- package/scripts/cli/{command/tray.ts → tray.ts} +208 -208
- package/scripts/cli/update.ts +40 -0
- package/scripts/cli/version.ts +67 -0
- package/scripts/cli/{ui/wordmark.ts → wordmark.ts} +58 -58
- package/src/boot/app.ts +11 -7
- package/src/boot/dependencies.ts +50 -19
- package/src/boot/timeout.ts +2 -13
- package/src/config/cooldowns.ts +186 -138
- package/src/config/network.ts +8 -14
- package/src/config/providers.ts +7 -10
- package/src/config/runtime.ts +76 -76
- package/src/config/security.ts +15 -0
- package/src/config/spec.ts +52 -7
- package/src/config/telemetry.ts +26 -0
- package/src/config/timeouts.ts +0 -6
- package/src/console/admin/model-abuse/contracts.ts +28 -6
- package/src/console/admin/model-abuse/routes.ts +56 -3
- package/src/console/api-keys/bucket-spend.ts +15 -4
- package/src/console/api-keys/contracts.ts +410 -414
- package/src/console/api-keys/routes.ts +881 -880
- package/src/console/auth/credentials.ts +3 -3
- package/src/console/auth/setup-service.ts +2 -2
- package/src/console/backup/contracts.ts +15 -2
- package/src/console/backup/export-endpoint.ts +6 -2
- package/src/console/backup/nine-router.ts +2 -5
- package/src/console/backup/service.ts +4 -0
- package/src/console/backup/store.ts +192 -29
- package/src/console/backup/validate.ts +3 -45
- package/src/console/cli-tools/fs-ops.ts +487 -487
- package/src/console/console-router-guards.ts +1 -1
- package/src/console/console-router.ts +8 -0
- package/src/console/data-management/contracts.ts +100 -0
- package/src/console/data-management/operations.ts +234 -0
- package/src/console/data-management/routes.ts +103 -0
- package/src/console/domain-registration.ts +45 -1
- package/src/console/observability/contracts.ts +524 -485
- package/src/console/observability/live.ts +22 -0
- package/src/console/observability/store.ts +1138 -1018
- package/src/console/observability/usage-dimensions.ts +4 -4
- package/src/console/providers/catalog/catalog-projections.ts +6 -7
- package/src/console/providers/catalog/contracts.ts +6 -0
- package/src/console/providers/catalog/model-operations.ts +5 -2
- package/src/console/providers/catalog/provider-operations.ts +24 -4
- package/src/console/providers/catalog/public-model-store.ts +8 -11
- package/src/console/providers/catalog/routes.ts +7 -0
- package/src/console/providers/catalog/store.ts +1553 -1532
- package/src/console/providers/oauth/routes.ts +8 -12
- package/src/console/quota/account-quota-shared.ts +52 -39
- package/src/console/quota/account-quota-tenant.ts +6 -6
- package/src/console/quota/quota-view.ts +1 -1
- package/src/console/quota/refresh.ts +441 -429
- package/src/console/remote-support/contracts.ts +61 -0
- package/src/console/remote-support/routes.ts +65 -0
- package/src/console/remote-support/service.ts +207 -0
- package/src/console/response-compression.ts +0 -3
- package/src/console/routing/pools/import-parse.ts +206 -0
- package/src/console/routing/pools/operations.ts +543 -501
- package/src/console/routing/pools/probe-result.ts +3 -2
- package/src/console/routing/pools/probe-verdict.ts +132 -132
- package/src/console/routing/pools/routes.ts +181 -164
- package/src/console/routing/pools/store.ts +8 -8
- package/src/console/settings/contracts.ts +95 -2
- package/src/console/settings/store.ts +81 -3
- package/src/console/shared/cursor.ts +8 -8
- package/src/console/shared/errors.ts +1 -3
- package/src/console/updates/routes.ts +16 -0
- package/src/main.ts +10 -0
- package/src/network/http2-bidi.ts +2 -4
- package/src/network/pool/agent.ts +478 -478
- package/src/network/pool/selector.ts +5 -4
- package/src/network/retry.ts +1 -2
- package/src/network/ssrf.ts +5 -4
- package/src/observability/event-compaction.ts +317 -0
- package/src/observability/event-day-index.ts +440 -0
- package/src/observability/event-log.ts +155 -6
- package/src/observability/logger.ts +6 -6
- package/src/observability/payload-capture.ts +158 -33
- package/src/observability/payload-store.ts +82 -61
- package/src/observability/redaction.ts +5 -6
- package/src/observability/request-notify.ts +43 -0
- package/src/observability/telemetry-status.ts +8 -9
- package/src/observability/token-speed.ts +5 -4
- package/src/persistence/db-mode.ts +7 -7
- package/src/persistence/health-event-flush.ts +36 -0
- package/src/persistence/readiness.ts +1 -1
- package/src/persistence/schema.ts +75 -29
- package/src/persistence/shared-cache.ts +1 -2
- package/src/persistence/stores/api-key-store.ts +18 -6
- package/src/persistence/stores/share-store.ts +4 -3
- package/src/persistence/stores/telemetry-store.ts +33 -11
- package/src/protocol/canonical-model.ts +11 -3
- package/src/protocol/codecs.ts +139 -6
- package/src/protocol/gateway-error.ts +6 -4
- package/src/protocol/image-clamp.ts +19 -1
- package/src/protocol/registry.ts +8 -0
- package/src/protocol/request/chat.ts +21 -1
- package/src/protocol/request/codex.ts +10 -1
- package/src/protocol/request/gemini.ts +9 -2
- package/src/protocol/request/messages.ts +18 -7
- package/src/protocol/request/responses.ts +9 -1
- package/src/protocol/response/chat.ts +34 -4
- package/src/protocol/response/codex.ts +7 -4
- package/src/protocol/response/dsml.ts +140 -28
- package/src/protocol/response/responses.ts +11 -5
- package/src/protocol/stream-error-frames.ts +4 -5
- package/src/providers/adapters/compatible-adapter.ts +3 -0
- package/src/providers/authentication/jwt-validator.ts +9 -10
- package/src/providers/authentication/oauth-refresh-service.ts +9 -11
- package/src/providers/bundled-discovery-providers.ts +122 -119
- package/src/providers/configured-openai-providers.ts +4 -4
- package/src/providers/default-registry.ts +5 -4
- package/src/providers/discovery/catalog/catalog-blob.ts +18 -18
- package/src/providers/discovery/catalog/catalog-format.ts +3 -6
- package/src/providers/discovery/models-dev-catalog.ts +354 -354
- package/src/providers/discovery/openai-model-discovery.ts +5 -0
- package/src/providers/discovery/openrouter-free-roster.ts +15 -17
- package/src/providers/discovery/probe-phases.ts +4 -4
- package/src/providers/discovery/probe-wire.ts +6 -13
- package/src/providers/discovery/probing-service.ts +1117 -1045
- package/src/providers/integrations/alibabacp/alibabacp.ts +1 -1
- package/src/providers/integrations/antigravity/antigravity-quota.ts +5 -7
- package/src/providers/integrations/buddy/buddy-chat-shared.ts +23 -2
- package/src/providers/integrations/buddy/buddy-checkin.ts +1 -4
- package/src/providers/integrations/buddy/buddy-cli-headers.ts +23 -6
- package/src/providers/integrations/buddy/buddy-conversation-headers.ts +0 -1
- package/src/providers/integrations/buddy/buddy-identity.ts +52 -0
- package/src/providers/integrations/buddy/buddy-media.ts +2 -2
- package/src/providers/integrations/buddy/buddy-usage-store.ts +3 -4
- package/src/providers/integrations/buddy/codebuddy/quota.ts +2 -13
- package/src/providers/integrations/buddy/codebuddy/shared.ts +118 -115
- package/src/providers/integrations/buddy/workbuddy/quota.ts +0 -11
- package/src/providers/integrations/buddy/workbuddy/shared.ts +5 -2
- package/src/providers/integrations/claude/claude.ts +608 -608
- package/src/providers/integrations/cline/cline-quota.ts +27 -20
- package/src/providers/integrations/cline/cline.ts +537 -539
- package/src/providers/integrations/codex/codex-headers.ts +25 -39
- package/src/providers/integrations/codex/codex-oauth.ts +1 -1
- package/src/providers/integrations/codex/codex.ts +2 -29
- package/src/providers/integrations/commandcode/commandcode.ts +674 -675
- package/src/providers/integrations/commandcode/discovery.ts +117 -116
- package/src/providers/integrations/cursor/cursor-auth.ts +418 -418
- package/src/providers/integrations/cursor/cursor-quota.ts +309 -0
- package/src/providers/integrations/cursor/cursor-request.ts +616 -471
- package/src/providers/integrations/cursor/cursor-session-manager.ts +319 -281
- package/src/providers/integrations/cursor/cursor.ts +1333 -960
- package/src/providers/integrations/cursor/protocol/messages.ts +25 -0
- package/src/providers/integrations/factory/factory-models.ts +3 -5
- package/src/providers/integrations/factory/factory.ts +8 -1
- package/src/providers/integrations/gemini.ts +6 -1
- package/src/providers/integrations/grok/grok-install-id.ts +2 -3
- package/src/providers/integrations/grok/grok-quota.ts +279 -177
- package/src/providers/integrations/grok/grok-ratelimit-store.ts +73 -0
- package/src/providers/integrations/grok/grok-ratelimit.ts +218 -0
- package/src/providers/integrations/grok/grok.ts +6 -7
- package/src/providers/integrations/hermes.ts +14 -18
- package/src/providers/integrations/kiro/kiro-discovery.ts +1 -1
- package/src/providers/integrations/kiro/kiro.ts +1 -1
- package/src/providers/integrations/mistral.ts +6 -6
- package/src/providers/integrations/opencode/fingerprint.ts +3 -3
- package/src/providers/integrations/opencode/provider.ts +19 -20
- package/src/providers/integrations/qoder/qoder-catalog.ts +450 -450
- package/src/providers/integrations/qoder/qoder-oauth.ts +388 -310
- package/src/providers/integrations/qoder/qoder-quota.ts +192 -0
- package/src/providers/integrations/qoder/qoder.ts +884 -865
- package/src/providers/integrations/shared/connect.ts +6 -6
- package/src/providers/integrations/trae/trae-catalog.ts +1 -2
- package/src/providers/integrations/trae/trae-quota.ts +384 -0
- package/src/providers/integrations/trae/trae.ts +2 -2
- package/src/providers/integrations/zcode/oauth.ts +6 -1
- package/src/providers/operations/account-health-service.ts +75 -27
- package/src/providers/operations/account-reset-service.ts +1 -1
- package/src/providers/operations/client-versions.ts +6 -6
- package/src/providers/operations/custom-cli-headers.ts +3 -4
- package/src/providers/operations/provider-catalog-seeder.ts +95 -95
- package/src/providers/operations/provider-catalog-service.ts +3 -4
- package/src/providers/provider-contracts.ts +7 -0
- package/src/providers/provider-metadata.ts +10 -8
- package/src/providers/provider-registry.ts +6 -7
- package/src/providers/provider-usage.ts +1 -1
- package/src/providers/quota/quota-support.ts +8 -0
- package/src/security/abuse.ts +15 -10
- package/src/security/access-control.ts +144 -146
- package/src/security/admission/contracts.ts +11 -0
- package/src/security/admission/in-memory-store.ts +4 -3
- package/src/security/admission/lease-sweep.ts +6 -6
- package/src/security/admission/reasons.ts +5 -1
- package/src/security/admission/redis-store.ts +211 -210
- package/src/security/admission/service.ts +4 -0
- package/src/security/api-key-auth.ts +27 -17
- package/src/security/ip-boundary.ts +155 -15
- package/src/security/model-abuse.ts +366 -94
- package/src/security/outbound-headers.ts +3 -4
- package/src/shared/sleep.ts +4 -0
- package/src/transport/dispatch/attempt-finalize.ts +47 -5
- package/src/transport/dispatch/attempt-loop.ts +531 -530
- package/src/transport/dispatch/generation.ts +6 -0
- package/src/transport/dispatch/proxy-request.ts +24 -1
- package/src/transport/dispatch/ratelimit-capture.ts +37 -0
- package/src/transport/dispatch/stream-bridge.ts +1 -3
- package/src/transport/dispatch/streaming/finalize.ts +1 -1
- package/src/transport/dispatch/streaming/stream.ts +19 -6
- package/src/transport/dispatch/upstream.ts +9 -0
- package/src/transport/failure-policy.ts +162 -8
- package/src/transport/guards/auth-guard.ts +7 -4
- package/src/transport/guards/concurrency-guard.ts +0 -5
- package/src/transport/guards/error-lifecycle.ts +13 -6
- package/src/transport/guards/pipeline.ts +15 -3
- package/src/transport/guards/request-context.ts +8 -2
- package/src/transport/request/inflight.ts +5 -6
- package/src/transport/request/preparer.ts +9 -8
- package/src/transport/request/state.ts +21 -8
- package/src/transport/request/tenant-preferences.ts +60 -0
- package/src/transport/routing/admission.ts +4 -0
- package/src/transport/routing/route-catalog.ts +65 -10
- package/src/transport/routing/route-model.ts +46 -14
- package/src/transport/routing/router.ts +21 -14
- package/src/transport/surface/completion.ts +2 -3
- package/src/transport/surface/dialects.ts +5 -6
- package/src/transport/surface/responses/encode.ts +38 -0
- package/src/transport/surface/responses/parse.ts +4 -0
- package/src/transport/translation/capabilities.ts +2 -3
- package/src/transport/translation/quirks.ts +3 -3
- package/src/workers/buddy-growth-tasks.ts +5 -10
- package/src/workers/daily-checkin.ts +17 -23
- package/src/workers/quota-refresh-sweep.ts +26 -26
- package/dist/dashboard/assets/BannedPage-hfCzi-Ux.js.gz +0 -0
- package/dist/dashboard/assets/CliToolDetailPage-DLGqYEwI.js +0 -1
- package/dist/dashboard/assets/CliToolDetailPage-DLGqYEwI.js.gz +0 -0
- package/dist/dashboard/assets/CliToolsPage-DEDyKBlC.js.gz +0 -0
- package/dist/dashboard/assets/CombosPage-CfuYiQ8Z.js +0 -1
- package/dist/dashboard/assets/CombosPage-CfuYiQ8Z.js.gz +0 -0
- package/dist/dashboard/assets/ConsoleLogPage-bsLuZmYH.js.gz +0 -0
- package/dist/dashboard/assets/CustomizationPage-_UWhwziJ.js.gz +0 -0
- package/dist/dashboard/assets/HealthEventsModal-Cjfc5WFS.js +0 -1
- package/dist/dashboard/assets/HealthEventsModal-Cjfc5WFS.js.gz +0 -0
- package/dist/dashboard/assets/LoginPage-DDVIfZGW.js.gz +0 -0
- package/dist/dashboard/assets/ModelPicker-CWkTUtpX.js +0 -1
- package/dist/dashboard/assets/ModelPicker-CWkTUtpX.js.gz +0 -0
- package/dist/dashboard/assets/OverviewPage-B53YYMng.js +0 -1
- package/dist/dashboard/assets/OverviewPage-B53YYMng.js.gz +0 -0
- package/dist/dashboard/assets/ProviderDetailPage-B0KaAQf0.js +0 -3
- package/dist/dashboard/assets/ProviderDetailPage-B0KaAQf0.js.gz +0 -0
- package/dist/dashboard/assets/ProvidersPage-Dx31DgIO.js +0 -1
- package/dist/dashboard/assets/ProvidersPage-Dx31DgIO.js.gz +0 -0
- package/dist/dashboard/assets/ProxyPage-BpnR4kXT.js +0 -7
- package/dist/dashboard/assets/ProxyPage-BpnR4kXT.js.gz +0 -0
- package/dist/dashboard/assets/QuotaPage-BpOQCnun.js +0 -1
- package/dist/dashboard/assets/QuotaPage-BpOQCnun.js.gz +0 -0
- package/dist/dashboard/assets/SettingsPage-DV1eo2J_.js +0 -1
- package/dist/dashboard/assets/SettingsPage-DV1eo2J_.js.gz +0 -0
- package/dist/dashboard/assets/SetupPage-D6pmQ86L.js.gz +0 -0
- package/dist/dashboard/assets/SortableList-DWvKiuSl.js.gz +0 -0
- package/dist/dashboard/assets/StudioPage-CmkUrgdj.js.gz +0 -0
- package/dist/dashboard/assets/UsagePage-B47uOydr.js +0 -56
- package/dist/dashboard/assets/UsagePage-B47uOydr.js.gz +0 -0
- package/dist/dashboard/assets/api-keys-uNywYlfv.js +0 -1
- package/dist/dashboard/assets/api-keys-uNywYlfv.js.gz +0 -0
- package/dist/dashboard/assets/app-DxivpfYG.css +0 -2
- package/dist/dashboard/assets/app-DxivpfYG.css.gz +0 -0
- package/dist/dashboard/assets/assert-NHwYq95t.js.gz +0 -0
- package/dist/dashboard/assets/badge-6uzDDvqs.js.gz +0 -0
- package/dist/dashboard/assets/banner-4_WNg2WZ.js.gz +0 -0
- package/dist/dashboard/assets/card--AtFedAq.js.gz +0 -0
- package/dist/dashboard/assets/cli-tools-B3e7dx3N.js.gz +0 -0
- package/dist/dashboard/assets/clipboard-button-NreUx6c8.js.gz +0 -0
- package/dist/dashboard/assets/dialog-BtpsJU98.js +0 -1
- package/dist/dashboard/assets/dialog-BtpsJU98.js.gz +0 -0
- package/dist/dashboard/assets/download-A0SW9OCW.js +0 -1
- package/dist/dashboard/assets/download-A0SW9OCW.js.gz +0 -0
- package/dist/dashboard/assets/drawer-WwwtzihF.js +0 -1
- package/dist/dashboard/assets/drawer-WwwtzihF.js.gz +0 -0
- package/dist/dashboard/assets/entry-CkryrIea.js +0 -4
- package/dist/dashboard/assets/entry-CkryrIea.js.gz +0 -0
- package/dist/dashboard/assets/entry-TJIye_jT.js +0 -1
- package/dist/dashboard/assets/entry-TJIye_jT.js.gz +0 -0
- package/dist/dashboard/assets/entry-jy9_4-Ke.js +0 -1
- package/dist/dashboard/assets/entry-jy9_4-Ke.js.gz +0 -0
- package/dist/dashboard/assets/fingerprint-pattern-BWuHtbW4.js.gz +0 -0
- package/dist/dashboard/assets/github-badge-D_eM3IgG.js.gz +0 -0
- package/dist/dashboard/assets/index-BydyKaDz.js.gz +0 -0
- package/dist/dashboard/assets/inline-BcNGF8Gr.js.gz +0 -0
- package/dist/dashboard/assets/input-BG41et9G.js.gz +0 -0
- package/dist/dashboard/assets/layout-C8mer_Ak.js +0 -1
- package/dist/dashboard/assets/layout-C8mer_Ak.js.gz +0 -0
- package/dist/dashboard/assets/list-row-Gh_W4OR_.js.gz +0 -0
- package/dist/dashboard/assets/live-BJGYRBnR.js.gz +0 -0
- package/dist/dashboard/assets/model-bans-DeVLDA7I.js +0 -1
- package/dist/dashboard/assets/model-bans-DeVLDA7I.js.gz +0 -0
- package/dist/dashboard/assets/mutation-BkoLM5Ba.js.gz +0 -0
- package/dist/dashboard/assets/numbers-Bs4LOOuf.js +0 -2
- package/dist/dashboard/assets/numbers-Bs4LOOuf.js.gz +0 -0
- package/dist/dashboard/assets/query-keys-D7wX2vM5.js.gz +0 -0
- package/dist/dashboard/assets/routing-CcusqXA7.js.gz +0 -0
- package/dist/dashboard/assets/select-BO6h8yNv.js.gz +0 -0
- package/dist/dashboard/assets/settings-mpmXTgUa.js.gz +0 -0
- package/dist/dashboard/assets/sparkles-BRaRUxAU.js +0 -1
- package/dist/dashboard/assets/sparkles-BRaRUxAU.js.gz +0 -0
- package/dist/dashboard/assets/stack-DwLmjUFK.js.gz +0 -0
- package/dist/dashboard/assets/state-DYdQmdAl.js.gz +0 -0
- package/dist/dashboard/assets/switch-CidSfbG7.js +0 -1
- package/dist/dashboard/assets/switch-CidSfbG7.js.gz +0 -0
- package/dist/dashboard/assets/terminal-Bd4ee8TA.js +0 -1
- package/dist/dashboard/assets/terminal-Bd4ee8TA.js.gz +0 -0
- package/dist/dashboard/assets/tokens-DDxMbSRL.js +0 -1
- package/dist/dashboard/assets/tokens-DDxMbSRL.js.gz +0 -0
- package/dist/dashboard/assets/use-presence-BSZEBx7U.js +0 -1
- package/dist/dashboard/assets/use-presence-BSZEBx7U.js.gz +0 -0
- package/dist/dashboard/assets/useMutation-w4u4CVue.js.gz +0 -0
- package/scripts/cli/command/admin.ts +0 -65
- package/scripts/cli/command/doctor.ts +0 -257
- package/scripts/cli/command/update.ts +0 -23
- package/scripts/cli/gateway/drain-token.ts +0 -78
- package/scripts/cli/gateway/instance-lock.ts +0 -105
- package/scripts/cli/gateway/stop.ts +0 -51
- package/scripts/cli/support/bun.ts +0 -27
- package/scripts/cli/support/version.ts +0 -37
- /package/dist/dashboard/assets/{app-CafWtVXA.js → app-BIt446yE.js} +0 -0
- /package/dist/dashboard/assets/{app-CafWtVXA.js.gz → app-BIt446yE.js.gz} +0 -0
- /package/scripts/cli/{ui/prompt.ts → prompt.ts} +0 -0
|
@@ -1,865 +1,884 @@
|
|
|
1
|
-
// Factory-blocked (Phase C4): PAT-to-jobToken session handshake, COSY
|
|
2
|
-
// AES/RSA request signing, alphabet-scrambled binary body, enveloped SSE
|
|
3
|
-
// frames — session/crypto protocol outside factory reach; stays bespoke.
|
|
4
|
-
import { constants, createCipheriv, createHash, publicEncrypt, randomUUID } from "node:crypto";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
readonly
|
|
30
|
-
readonly
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
*
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
"
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
"
|
|
211
|
-
"
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
"
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
"cosy-
|
|
263
|
-
"cosy-
|
|
264
|
-
"cosy-
|
|
265
|
-
"cosy-
|
|
266
|
-
"cosy-
|
|
267
|
-
"
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
"
|
|
284
|
-
"
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
}
|
|
307
|
-
if (
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
throw new GatewayError("platform_unavailable", 502, "Qoder PAT exchange returned
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
*
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
if (
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
const
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
const
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
//
|
|
792
|
-
//
|
|
793
|
-
//
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
const
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
*
|
|
843
|
-
*
|
|
844
|
-
*
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
const
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
1
|
+
// Factory-blocked (Phase C4): PAT-to-jobToken session handshake, COSY
|
|
2
|
+
// AES/RSA request signing, alphabet-scrambled binary body, enveloped SSE
|
|
3
|
+
// frames — session/crypto protocol outside factory reach; stays bespoke.
|
|
4
|
+
import { constants, createCipheriv, createHash, publicEncrypt, randomUUID } from "node:crypto";
|
|
5
|
+
import {
|
|
6
|
+
classifyUpstreamError,
|
|
7
|
+
extractUpstreamMessage,
|
|
8
|
+
isUpstreamQueued,
|
|
9
|
+
mapUpstreamHttpError,
|
|
10
|
+
queueRetryAfterMs,
|
|
11
|
+
upstreamErrorIdentifier,
|
|
12
|
+
} from "../../../transport/failure-policy";
|
|
13
|
+
import { GatewayError } from "../../../protocol/gateway-error";
|
|
14
|
+
import type { CanonicalEvent, CanonicalRequest, CanonicalStopReason, ContentPart } from "../../../protocol/canonical-model";
|
|
15
|
+
import { toolResultParts } from "../../../protocol/canonical-model";
|
|
16
|
+
import { decodeSseEvents } from "../../../protocol/sse-decoder";
|
|
17
|
+
import { usageFromProvider, readReasoningText } from "../../provider-usage";
|
|
18
|
+
import { readCredentialSecret, type ProviderDispatchTarget, type ProviderAdapter, type ProviderDispatchContext, type ProviderId } from "../../provider-contracts";
|
|
19
|
+
import type { ModelDefinition } from "../../provider-model-definition";
|
|
20
|
+
import { parseQoderOAuthState } from "./qoder-oauth";
|
|
21
|
+
import { getQoderVersion } from "../../operations/client-versions";
|
|
22
|
+
import { abortGatewayError, createUpstreamDeadlineLifecycle } from "../../operations/upstream-deadline";
|
|
23
|
+
import { qoderMachineOs } from "../../operations/cli-platform";
|
|
24
|
+
import { qoderGatewayCandidates, QODER_CHAT_PATH, QODER_REALMS, type QoderRealm } from "./qoder-realm";
|
|
25
|
+
import { QODER_CN_MODEL_CONFIGS, QODER_INTL_MODEL_CONFIGS, QODER_INTL_MODELS } from "./qoder-catalog";
|
|
26
|
+
import { isQoderVirtualModel, qoderDispatchModelId } from "./qoder-virtual";
|
|
27
|
+
|
|
28
|
+
interface QoderModeProfile {
|
|
29
|
+
readonly chatUrl: string;
|
|
30
|
+
readonly businessProduct: string;
|
|
31
|
+
readonly businessType: string;
|
|
32
|
+
readonly businessVersion: string;
|
|
33
|
+
readonly cosyScene: string;
|
|
34
|
+
readonly mirrorTopLevelSystem: boolean;
|
|
35
|
+
readonly sendBusinessHeaders: boolean;
|
|
36
|
+
readonly sendModelSourceHeaders: boolean;
|
|
37
|
+
readonly emptyAliyunUserType: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const MODERN_PROFILE: QoderModeProfile = {
|
|
41
|
+
chatUrl: "https://api2.qoder.sh/algo/api/v2/service/pro/sse/agent_chat_generation?FetchKeys=llm_model_result&AgentId=agent_common&Encode=1",
|
|
42
|
+
businessProduct: "cli",
|
|
43
|
+
businessType: "agent",
|
|
44
|
+
businessVersion: "1.0.22",
|
|
45
|
+
cosyScene: "assistant",
|
|
46
|
+
mirrorTopLevelSystem: true,
|
|
47
|
+
sendBusinessHeaders: true,
|
|
48
|
+
sendModelSourceHeaders: true,
|
|
49
|
+
emptyAliyunUserType: true,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const QODER_PROVIDER_ID = "qoder" as const;
|
|
53
|
+
|
|
54
|
+
export const QODER_MODEL_CONFIGS: Record<
|
|
55
|
+
string,
|
|
56
|
+
{ id: string; display_name: string; max_input_tokens: number; max_output_tokens?: number; is_vl: boolean; is_reasoning: boolean; source?: string }
|
|
57
|
+
> = {
|
|
58
|
+
auto: { id: "auto", display_name: "Auto", max_input_tokens: 180000, is_vl: true, is_reasoning: false },
|
|
59
|
+
ultimate: { id: "ultimate", display_name: "Ultimate", max_input_tokens: 180000, is_vl: true, is_reasoning: true },
|
|
60
|
+
performance: { id: "performance", display_name: "Performance", max_input_tokens: 272000, is_vl: true, is_reasoning: false },
|
|
61
|
+
efficient: { id: "efficient", display_name: "Efficient", max_input_tokens: 180000, is_vl: true, is_reasoning: false },
|
|
62
|
+
lite: { id: "lite", display_name: "Lite", max_input_tokens: 180000, is_vl: false, is_reasoning: true },
|
|
63
|
+
qmodel: { id: "qmodel", display_name: "Qwen 3.6 Plus", max_input_tokens: 180000, is_vl: true, is_reasoning: false },
|
|
64
|
+
qmodel_latest: { id: "qmodel_latest", display_name: "Qwen 3.7 Max", max_input_tokens: 1000000, is_vl: true, is_reasoning: false },
|
|
65
|
+
qmodel_preview: { id: "qmodel_preview", display_name: "Qwen 3.8 Max", max_input_tokens: 1000000, is_vl: true, is_reasoning: false },
|
|
66
|
+
dmodel: { id: "dmodel", display_name: "DeepSeek V4 Pro", max_input_tokens: 180000, is_vl: true, is_reasoning: true },
|
|
67
|
+
dfmodel: { id: "dfmodel", display_name: "DeepSeek V4 Flash", max_input_tokens: 180000, is_vl: true, is_reasoning: true },
|
|
68
|
+
gm51model: { id: "gm51model", display_name: "GLM 5.1", max_input_tokens: 180000, is_vl: true, is_reasoning: true },
|
|
69
|
+
kmodel: { id: "kmodel", display_name: "Kimi K2.6", max_input_tokens: 256000, is_vl: true, is_reasoning: false },
|
|
70
|
+
mmodel: { id: "mmodel", display_name: "MiniMax M2.7", max_input_tokens: 180000, is_vl: true, is_reasoning: false },
|
|
71
|
+
kmodel_latest: { id: "kmodel_latest", display_name: "Kimi K2.7 Latest", max_input_tokens: 256000, is_vl: true, is_reasoning: false },
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const QODER_APPCODE = "cosy";
|
|
75
|
+
const QODER_SIG_SECRET = "d2FyLCB3YXIgbmV2ZXIgY2hhbmdlcw==";
|
|
76
|
+
/**
|
|
77
|
+
* Where a PAT is exchanged for a short-lived job token, per realm.
|
|
78
|
+
*
|
|
79
|
+
* The job token is realm-bound: one minted on the global host answers "Login
|
|
80
|
+
* expired" on the China gateway. Exchanging against the wrong host therefore
|
|
81
|
+
* looks like a dead credential rather than a routing bug, which is why the
|
|
82
|
+
* URL is derived from the realm table instead of being one constant.
|
|
83
|
+
*
|
|
84
|
+
* `intl` keeps its historical `center.qoder.sh` v3 endpoint. `cn` uses the
|
|
85
|
+
* openapi host the donor's `PATH_JOB_EXCHANGE` describes; if that path is
|
|
86
|
+
* ever wrong for CN, the failure surfaces as a 401/403 on exchange, not as a
|
|
87
|
+
* silently misrouted request.
|
|
88
|
+
*/
|
|
89
|
+
const QODER_JOB_TOKEN_URL = "https://center.qoder.sh/algo/api/v3/user/jobToken?Encode=1";
|
|
90
|
+
function qoderJobTokenUrl(realm: QoderRealm): string {
|
|
91
|
+
return realm === "cn"
|
|
92
|
+
? `${QODER_REALMS.cn.openapi}/api/v1/jobToken/exchange`
|
|
93
|
+
: QODER_JOB_TOKEN_URL;
|
|
94
|
+
}
|
|
95
|
+
const QODER_STANDARD_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
96
|
+
const QODER_ALPHABET = "_doRTgHZBKcGVjlvpC,@aFSx#DPuNJme&i*MzLOEn)sUrthbf%Y^w.(kIQyXqWA!";
|
|
97
|
+
const QODER_RSA_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
|
98
|
+
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDA8iMH5c02LilrsERw9t6Pv5Nc
|
|
99
|
+
4k6Pz1EaDicBMpdpxKduSZu5OANqUq8er4GM95omAGIOPOh+Nx0spthYA2BqGz+l
|
|
100
|
+
6HRkPJ7S236FZz73In/KVuLnwI8JJ2CbuJap8kvheCCZpmAWpb/cPx/3Vr/J6I17
|
|
101
|
+
XcW+ML9FoCI6AOvOzwIDAQAB
|
|
102
|
+
-----END PUBLIC KEY-----`;
|
|
103
|
+
|
|
104
|
+
export interface QoderAuth {
|
|
105
|
+
userId: string;
|
|
106
|
+
userName: string;
|
|
107
|
+
userType: string;
|
|
108
|
+
securityOauthToken: string;
|
|
109
|
+
refreshToken: string;
|
|
110
|
+
machineId: string;
|
|
111
|
+
}
|
|
112
|
+
interface QoderModelConfig {
|
|
113
|
+
id: string;
|
|
114
|
+
display_name: string;
|
|
115
|
+
max_input_tokens: number;
|
|
116
|
+
max_output_tokens?: number;
|
|
117
|
+
is_vl: boolean;
|
|
118
|
+
is_reasoning: boolean;
|
|
119
|
+
source?: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
function qoderMd5(value: string | Uint8Array): string {
|
|
124
|
+
return createHash("md5").update(value).digest("hex");
|
|
125
|
+
}
|
|
126
|
+
function qoderPathSignature(url: string): string {
|
|
127
|
+
const path = new URL(url).pathname;
|
|
128
|
+
return path.startsWith("/algo") ? path.slice(5) : path;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const QODER_MACHINE_ID_TTL_MS = 60 * 60 * 1000;
|
|
132
|
+
const QODER_MAX_MACHINE_IDS = 1_024;
|
|
133
|
+
const qoderMachineIds = new Map<string, { id: string; expiresAt: number }>();
|
|
134
|
+
/**
|
|
135
|
+
* A PAT (`pt-...`) must be exchanged for a short-lived job token before it
|
|
136
|
+
* can sign COSY requests. Device tokens (`dt-...`) from OAuth login and job
|
|
137
|
+
* tokens (`jt-...`) are used directly.
|
|
138
|
+
*/
|
|
139
|
+
export function isQoderPat(secret: string): boolean {
|
|
140
|
+
return secret.startsWith("pt-");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Stable per-account machine id for OAuth dispatches. Derived from the
|
|
145
|
+
* account id (not minted per request) so pooled accounts keep one device
|
|
146
|
+
* fingerprint across restarts instead of looking like a new device every
|
|
147
|
+
* time, while staying distinct per account.
|
|
148
|
+
*/
|
|
149
|
+
export function qoderMachineIdForAccount(accountId: string): string {
|
|
150
|
+
const hex = createHash("sha256").update(`qoder-machine-v1:${accountId}`).digest("hex");
|
|
151
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Builds dispatch auth from an OAuth device token. The token is used
|
|
156
|
+
* directly as the COSY identity's oauth token; the user id and machine id
|
|
157
|
+
* come from the `auth_state` the login persisted. Fails closed with a
|
|
158
|
+
* reconnect prompt when the identity is absent (e.g. an account created
|
|
159
|
+
* before `auth_state` existed) instead of signing with a guessed id.
|
|
160
|
+
*/
|
|
161
|
+
export function qoderOAuthAuth(
|
|
162
|
+
secret: string,
|
|
163
|
+
authState: unknown,
|
|
164
|
+
accountId: string | undefined,
|
|
165
|
+
): QoderAuth {
|
|
166
|
+
const state = parseQoderOAuthState(authState);
|
|
167
|
+
if (state === undefined || !state.userId) {
|
|
168
|
+
throw new GatewayError(
|
|
169
|
+
"authentication_failed",
|
|
170
|
+
401,
|
|
171
|
+
"Qoder OAuth account is missing its user identity; sign in again",
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
userId: state.userId,
|
|
176
|
+
userName: "",
|
|
177
|
+
userType: "personal_standard",
|
|
178
|
+
securityOauthToken: secret,
|
|
179
|
+
refreshToken: "",
|
|
180
|
+
machineId: state.machineId || (accountId ? qoderMachineIdForAccount(accountId) : randomUUID()),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function qoderMachineIdFromPat(pat: string): string {
|
|
185
|
+
const key = createHash("sha256").update(pat).digest("hex");
|
|
186
|
+
const now = Date.now();
|
|
187
|
+
const existing = qoderMachineIds.get(key);
|
|
188
|
+
if (existing && existing.expiresAt > now) return existing.id;
|
|
189
|
+
const id = randomUUID();
|
|
190
|
+
qoderMachineIds.delete(key);
|
|
191
|
+
qoderMachineIds.set(key, { id, expiresAt: now + QODER_MACHINE_ID_TTL_MS });
|
|
192
|
+
for (const [candidate, value] of qoderMachineIds) {
|
|
193
|
+
if (value.expiresAt <= now) qoderMachineIds.delete(candidate);
|
|
194
|
+
}
|
|
195
|
+
while (qoderMachineIds.size > QODER_MAX_MACHINE_IDS) {
|
|
196
|
+
const oldest = qoderMachineIds.keys().next();
|
|
197
|
+
if (oldest.done) break;
|
|
198
|
+
qoderMachineIds.delete(oldest.value);
|
|
199
|
+
}
|
|
200
|
+
return id;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function qoderStaticHeaders(machineId: string, version = getQoderVersion()): Record<string, string> {
|
|
204
|
+
const date = new Date().toUTCString();
|
|
205
|
+
return {
|
|
206
|
+
"cosy-machinetoken": machineId,
|
|
207
|
+
"cosy-machinetype": "5",
|
|
208
|
+
"cosy-machineid": machineId,
|
|
209
|
+
"cosy-clienttype": "5",
|
|
210
|
+
"cosy-version": version,
|
|
211
|
+
"login-version": "v2",
|
|
212
|
+
appcode: QODER_APPCODE,
|
|
213
|
+
accept: "application/json",
|
|
214
|
+
"accept-encoding": "identity",
|
|
215
|
+
date,
|
|
216
|
+
signature: qoderMd5(`${QODER_APPCODE}&${QODER_SIG_SECRET}&${date}`),
|
|
217
|
+
"content-type": "application/json",
|
|
218
|
+
"user-agent": "Go-http-client/2.0",
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function transformQoderBody(plaintext: string): Uint8Array {
|
|
223
|
+
const base64 = Buffer.from(plaintext).toString("base64");
|
|
224
|
+
const third = Math.floor(base64.length / 3);
|
|
225
|
+
const reordered = base64.slice(base64.length - third) + base64.slice(third, base64.length - third) + base64.slice(0, third);
|
|
226
|
+
let encoded = "";
|
|
227
|
+
for (const char of reordered) {
|
|
228
|
+
if (char === "=") encoded += "$";
|
|
229
|
+
else encoded += QODER_ALPHABET[QODER_STANDARD_ALPHABET.indexOf(char)] ?? char;
|
|
230
|
+
}
|
|
231
|
+
return Buffer.from(encoded, "latin1");
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function qoderBuildCosyHeaders(
|
|
235
|
+
body: Uint8Array,
|
|
236
|
+
url: string,
|
|
237
|
+
auth: QoderAuth,
|
|
238
|
+
version = getQoderVersion(),
|
|
239
|
+
): Record<string, string> {
|
|
240
|
+
const key = randomUUID().replaceAll("-", "").slice(0, 16);
|
|
241
|
+
const identity = JSON.stringify({
|
|
242
|
+
name: auth.userName,
|
|
243
|
+
aid: auth.userId,
|
|
244
|
+
uid: auth.userId,
|
|
245
|
+
yx_uid: "",
|
|
246
|
+
organization_id: "",
|
|
247
|
+
organization_name: "",
|
|
248
|
+
user_type: auth.userType,
|
|
249
|
+
security_oauth_token: auth.securityOauthToken,
|
|
250
|
+
refresh_token: auth.refreshToken,
|
|
251
|
+
});
|
|
252
|
+
const cipher = createCipheriv("aes-128-cbc", Buffer.from(key), Buffer.from(key));
|
|
253
|
+
const info = Buffer.concat([cipher.update(identity), cipher.final()]).toString("base64");
|
|
254
|
+
const cosyKey = publicEncrypt({ key: QODER_RSA_PUBLIC_KEY, padding: constants.RSA_PKCS1_PADDING }, Buffer.from(key)).toString("base64");
|
|
255
|
+
const payload = Buffer.from(
|
|
256
|
+
JSON.stringify({ version: "v1", requestId: randomUUID(), info, cosyVersion: version, ideVersion: "" }),
|
|
257
|
+
).toString("base64");
|
|
258
|
+
const date = String(Math.floor(Date.now() / 1000));
|
|
259
|
+
const signature = qoderMd5(`${payload}\n${cosyKey}\n${date}\n${Buffer.from(body).toString("latin1")}\n${qoderPathSignature(url)}`);
|
|
260
|
+
return {
|
|
261
|
+
authorization: `Bearer COSY.${payload}.${signature}`,
|
|
262
|
+
"cosy-data-policy": "agree",
|
|
263
|
+
"cosy-machinetype": "5",
|
|
264
|
+
"cosy-machineos": qoderMachineOs(),
|
|
265
|
+
"cosy-clienttype": "5",
|
|
266
|
+
"cosy-date": date,
|
|
267
|
+
"cosy-user": auth.userId,
|
|
268
|
+
"cosy-key": cosyKey,
|
|
269
|
+
"cosy-bodyhash": qoderMd5(body),
|
|
270
|
+
"cosy-bodylength": String(body.byteLength),
|
|
271
|
+
"cosy-sigpath": qoderPathSignature(url),
|
|
272
|
+
"cosy-organization-id": "",
|
|
273
|
+
"cosy-organization-tags": "",
|
|
274
|
+
"x-request-id": randomUUID(),
|
|
275
|
+
...(MODERN_PROFILE.sendBusinessHeaders
|
|
276
|
+
? {
|
|
277
|
+
"cosy-business-product": MODERN_PROFILE.businessProduct,
|
|
278
|
+
"cosy-business-type": MODERN_PROFILE.businessType,
|
|
279
|
+
"cosy-business-version": version,
|
|
280
|
+
"cosy-scene": MODERN_PROFILE.cosyScene,
|
|
281
|
+
}
|
|
282
|
+
: {}),
|
|
283
|
+
"cosy-version": version,
|
|
284
|
+
"login-version": "v2",
|
|
285
|
+
appcode: QODER_APPCODE,
|
|
286
|
+
accept: "application/json",
|
|
287
|
+
"accept-encoding": "identity",
|
|
288
|
+
date: new Date().toUTCString(),
|
|
289
|
+
signature: qoderMd5(`${QODER_APPCODE}&${QODER_SIG_SECRET}&${new Date().toUTCString()}`),
|
|
290
|
+
"content-type": "application/json",
|
|
291
|
+
"user-agent": "Go-http-client/2.0",
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
type QoderFetcher = (url: string, init: RequestInit) => Promise<Response>;
|
|
296
|
+
|
|
297
|
+
async function exchangeQoderPat(
|
|
298
|
+
pat: string,
|
|
299
|
+
signal: AbortSignal,
|
|
300
|
+
fetcher: QoderFetcher,
|
|
301
|
+
version = getQoderVersion(),
|
|
302
|
+
realm: QoderRealm = "intl",
|
|
303
|
+
): Promise<QoderAuth> {
|
|
304
|
+
const machineId = qoderMachineIdFromPat(pat);
|
|
305
|
+
const encodedBody = transformQoderBody(JSON.stringify({ payload: JSON.stringify({ personalToken: pat, securityOauthToken: "", refreshToken: "", needRefresh: false, authInfo: {} }), encodeVersion: "1" }));
|
|
306
|
+
const response = await fetcher(qoderJobTokenUrl(realm), { method: "POST", headers: qoderStaticHeaders(machineId, version), body: encodedBody as unknown as BodyInit, signal });
|
|
307
|
+
if (!response.ok) throw await mapUpstreamHttpError(response, "qoder");
|
|
308
|
+
let responseBody: unknown;
|
|
309
|
+
try {
|
|
310
|
+
responseBody = await response.json();
|
|
311
|
+
} catch {
|
|
312
|
+
throw new GatewayError("platform_unavailable", 502, "Qoder PAT exchange returned invalid JSON", {}, "upstream");
|
|
313
|
+
}
|
|
314
|
+
if (typeof responseBody !== "object" || responseBody === null || Array.isArray(responseBody)) {
|
|
315
|
+
throw new GatewayError("platform_unavailable", 502, "Qoder PAT exchange returned unexpected response", {}, "upstream");
|
|
316
|
+
}
|
|
317
|
+
const rec = responseBody as Record<string, unknown>;
|
|
318
|
+
if (typeof rec["id"] !== "string" || typeof rec["securityOauthToken"] !== "string") {
|
|
319
|
+
throw new GatewayError("platform_unavailable", 502, "Qoder PAT exchange returned incomplete credentials", {}, "upstream");
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
userId: rec["id"] as string,
|
|
323
|
+
userName: typeof rec["name"] === "string" ? (rec["name"] as string) : "",
|
|
324
|
+
userType: typeof rec["userType"] === "string" ? (rec["userType"] as string) : "personal_standard",
|
|
325
|
+
securityOauthToken: rec["securityOauthToken"] as string,
|
|
326
|
+
refreshToken: typeof rec["refreshToken"] === "string" ? (rec["refreshToken"] as string) : "",
|
|
327
|
+
machineId,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async function callQoder(
|
|
332
|
+
url: string,
|
|
333
|
+
body: Record<string, unknown>,
|
|
334
|
+
modelId: string,
|
|
335
|
+
auth: QoderAuth,
|
|
336
|
+
signal: AbortSignal,
|
|
337
|
+
fetcher: QoderFetcher,
|
|
338
|
+
version = getQoderVersion(),
|
|
339
|
+
): Promise<Response> {
|
|
340
|
+
const encoded = transformQoderBody(JSON.stringify(body));
|
|
341
|
+
const modelConfig = (body["model_config"] as Record<string, unknown> | null) ?? null;
|
|
342
|
+
return fetcher(url, {
|
|
343
|
+
method: "POST",
|
|
344
|
+
headers: {
|
|
345
|
+
...qoderBuildCosyHeaders(encoded, url, auth, version),
|
|
346
|
+
...(MODERN_PROFILE.sendModelSourceHeaders
|
|
347
|
+
? { "x-model-key": modelId, "x-model-source": typeof modelConfig?.["source"] === "string" ? (modelConfig["source"] as string) : "system" }
|
|
348
|
+
: {}),
|
|
349
|
+
Accept: "text/event-stream",
|
|
350
|
+
"Cache-Control": "no-cache",
|
|
351
|
+
"Accept-Encoding": "identity",
|
|
352
|
+
},
|
|
353
|
+
body: encoded as unknown as BodyInit,
|
|
354
|
+
signal,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* The human-readable text out of a Qoder error envelope.
|
|
360
|
+
*
|
|
361
|
+
* The envelope is Qoder's own wrapper around the stream, and it does not use
|
|
362
|
+
* one field for the message: the text has been seen as a nested `error`
|
|
363
|
+
* object, a bare string `body`, and a top-level `message`/`msg`. The canonical
|
|
364
|
+
* extractor covers the structured shapes; a string body and the top-level
|
|
365
|
+
* fields cover the rest. Whatever survives is bounded the same way an upstream
|
|
366
|
+
* message is, so a verbose envelope cannot blow up the public error line.
|
|
367
|
+
*/
|
|
368
|
+
function qoderEnvelopeMessage(rec: Record<string, unknown>): string {
|
|
369
|
+
const structured = extractUpstreamMessage(rec["body"]);
|
|
370
|
+
if (structured.length > 0) return structured;
|
|
371
|
+
if (typeof rec["body"] === "string" && rec["body"].trim().length > 0 && rec["body"] !== "[DONE]")
|
|
372
|
+
return rec["body"].replace(/[\r\n]+/g, " ").trim().slice(0, 500);
|
|
373
|
+
return extractUpstreamMessage(rec);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function qoderEnvelopeToFrames(data: string): Array<{ event: string | null; data: string }> {
|
|
377
|
+
if (data === "[DONE]") return [{ event: null, data: "[DONE]" }];
|
|
378
|
+
let envelope: unknown;
|
|
379
|
+
try {
|
|
380
|
+
envelope = JSON.parse(data);
|
|
381
|
+
} catch {
|
|
382
|
+
}
|
|
383
|
+
if (typeof envelope !== "object" || envelope === null || Array.isArray(envelope)) return [];
|
|
384
|
+
const rec = envelope as Record<string, unknown>;
|
|
385
|
+
if (rec["statusCodeValue"] !== undefined && rec["statusCodeValue"] !== 200) {
|
|
386
|
+
const status = typeof rec["statusCodeValue"] === "number" ? (rec["statusCodeValue"] as number) : 502;
|
|
387
|
+
// Qoder reports failures inside the stream envelope instead of as HTTP
|
|
388
|
+
// responses, so classify its structured provider code and status through
|
|
389
|
+
// the same gateway error policy as the other dispatch paths.
|
|
390
|
+
const detail = qoderEnvelopeMessage(rec);
|
|
391
|
+
const identifier = upstreamErrorIdentifier(rec["body"]) ?? upstreamErrorIdentifier(rec);
|
|
392
|
+
// Qoder signals a queued request the same way inside the stream as it does
|
|
393
|
+
// on HTTP: `{"code":"10605", "isQueued":true, "retryAfterSeconds":N}` on a
|
|
394
|
+
// non-200 envelope. The status table would read the 403 as a refusal, and
|
|
395
|
+
// the caller would end its turn on a request that was only waiting.
|
|
396
|
+
const queuedWaitMs = isUpstreamQueued(rec["body"]) ?? false;
|
|
397
|
+
const waitMs = queuedWaitMs
|
|
398
|
+
? (queueRetryAfterMs(rec["body"]) ?? undefined)
|
|
399
|
+
: undefined;
|
|
400
|
+
const classification = queuedWaitMs
|
|
401
|
+
? { code: "upstream_queued" as const, status: 503, origin: "upstream" as const }
|
|
402
|
+
: classifyUpstreamError(status, identifier);
|
|
403
|
+
throw new GatewayError(
|
|
404
|
+
classification.code,
|
|
405
|
+
classification.status,
|
|
406
|
+
detail.length > 0 ? detail : `Qoder stream envelope returned HTTP ${status}`,
|
|
407
|
+
{
|
|
408
|
+
providerId: QODER_PROVIDER_ID,
|
|
409
|
+
providerStatus: status,
|
|
410
|
+
...(identifier ? { providerCode: identifier } : {}),
|
|
411
|
+
...(classification.credentialEvidence ? { credentialEvidence: true } : {}),
|
|
412
|
+
...(classification.rateLimitScope
|
|
413
|
+
? { rateLimitScope: classification.rateLimitScope }
|
|
414
|
+
: {}),
|
|
415
|
+
...(waitMs === undefined ? {} : { retryAfterMs: waitMs, cooldownMs: waitMs }),
|
|
416
|
+
...(queuedWaitMs ? { reason: "upstream-queued" } : {}),
|
|
417
|
+
},
|
|
418
|
+
classification.origin,
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
const body = rec["body"];
|
|
422
|
+
if (body === "[DONE]") return [{ event: null, data: "[DONE]" }];
|
|
423
|
+
if (typeof body !== "string") return [];
|
|
424
|
+
return [{ event: null, data: body.replace(/\r?\n/g, "") }];
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function qoderStableHash(prefix: string, ...parts: string[]): string {
|
|
428
|
+
const h = createHash("sha256");
|
|
429
|
+
h.update(prefix);
|
|
430
|
+
for (const p of parts) {
|
|
431
|
+
h.update("\0");
|
|
432
|
+
h.update(p);
|
|
433
|
+
}
|
|
434
|
+
return h.digest("hex").slice(0, 16);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function flattenCanonicalContent(content: readonly ContentPart[]): string {
|
|
438
|
+
return content
|
|
439
|
+
.filter((b) => b.kind === "text" || b.kind === "toolResult")
|
|
440
|
+
.map((b) => {
|
|
441
|
+
if (b.kind === "text") return b.text;
|
|
442
|
+
if (b.kind === "toolResult") return typeof b.content === "string" ? b.content : JSON.stringify(b.content);
|
|
443
|
+
return "";
|
|
444
|
+
})
|
|
445
|
+
.join("\n");
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function qoderStableRecordId(modelId: string, messages: CanonicalRequest["messages"], maxTokens: number): string {
|
|
449
|
+
const h = createHash("sha256");
|
|
450
|
+
h.update("qoder-record\0");
|
|
451
|
+
h.update(modelId);
|
|
452
|
+
for (const m of messages) {
|
|
453
|
+
h.update("\0");
|
|
454
|
+
h.update(m.role);
|
|
455
|
+
const text = flattenCanonicalContent(m.content);
|
|
456
|
+
if (text) {
|
|
457
|
+
h.update("\0");
|
|
458
|
+
h.update(text);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
h.update(`\0mt=${maxTokens}`);
|
|
462
|
+
return h.digest("hex").slice(0, 16);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function qoderRequestedMaxTokens(request: CanonicalRequest, modelConfig: QoderModelConfig): number {
|
|
466
|
+
/**
|
|
467
|
+
* The ceiling is the roster's stated one. A fixed 32k fallback would clamp
|
|
468
|
+
* every model alike, so a request asking for more would be silently lowered
|
|
469
|
+
* no matter which model it named.
|
|
470
|
+
*/
|
|
471
|
+
const configured = typeof modelConfig.max_output_tokens === "number" ? modelConfig.max_output_tokens : 65_536;
|
|
472
|
+
const controls = request.generation_controls as Record<string, unknown>;
|
|
473
|
+
const requested =
|
|
474
|
+
typeof controls["max_tokens"] === "number"
|
|
475
|
+
? (controls["max_tokens"] as number)
|
|
476
|
+
: typeof controls["max_output_tokens"] === "number"
|
|
477
|
+
? (controls["max_output_tokens"] as number)
|
|
478
|
+
: null;
|
|
479
|
+
return typeof requested === "number" && requested > 0 ? Math.min(requested, configured) : configured;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
export function buildQoderRequest(
|
|
483
|
+
modelId: string,
|
|
484
|
+
request: CanonicalRequest,
|
|
485
|
+
modelConfig: QoderModelConfig,
|
|
486
|
+
auth: QoderAuth,
|
|
487
|
+
): Record<string, unknown> {
|
|
488
|
+
const allMessages = request.messages;
|
|
489
|
+
const systemParts: string[] = [];
|
|
490
|
+
if (request.system) {
|
|
491
|
+
for (const p of request.system) if (p.kind === "text") systemParts.push(p.text);
|
|
492
|
+
}
|
|
493
|
+
if (request.instructions) {
|
|
494
|
+
for (const p of request.instructions) if (p.kind === "text") systemParts.push(p.text);
|
|
495
|
+
}
|
|
496
|
+
for (const m of allMessages) {
|
|
497
|
+
if (m.role === "system" || m.role === "developer") {
|
|
498
|
+
const t = flattenCanonicalContent(m.content);
|
|
499
|
+
if (t) systemParts.push(t);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
const systemPrompt = systemParts.join("\n\n").trim();
|
|
503
|
+
const qoderMessages: Array<Record<string, unknown>> = [];
|
|
504
|
+
if (MODERN_PROFILE.mirrorTopLevelSystem && systemPrompt.length > 0) {
|
|
505
|
+
qoderMessages.push({
|
|
506
|
+
role: "system",
|
|
507
|
+
content: systemPrompt,
|
|
508
|
+
contents: [{ type: "text", text: systemPrompt }],
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
for (const m of allMessages) {
|
|
512
|
+
if (m.role === "system" || m.role === "developer") continue;
|
|
513
|
+
// Answers may live in a `user` turn (the Messages ledger re-homes them),
|
|
514
|
+
// so the result scan is role-agnostic. Each result becomes its own `tool`
|
|
515
|
+
// turn carrying its `tool_call_id`; any non-result content on the same turn
|
|
516
|
+
// is emitted after, so nothing is duplicated or lost.
|
|
517
|
+
const results = toolResultParts(m);
|
|
518
|
+
if (results.length > 0) {
|
|
519
|
+
for (const block of results) {
|
|
520
|
+
const text = flattenCanonicalContent(
|
|
521
|
+
typeof block.content === "string"
|
|
522
|
+
? [{ kind: "text", text: block.content }]
|
|
523
|
+
: block.content,
|
|
524
|
+
);
|
|
525
|
+
qoderMessages.push({
|
|
526
|
+
role: "tool",
|
|
527
|
+
content: text,
|
|
528
|
+
contents: [{ type: "text", text }],
|
|
529
|
+
tool_call_id: block.call_id,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
const remaining = m.content.filter((part) => part.kind !== "toolResult");
|
|
533
|
+
if (remaining.length === 0) continue;
|
|
534
|
+
const flat = flattenCanonicalContent(remaining);
|
|
535
|
+
qoderMessages.push({
|
|
536
|
+
role: "user",
|
|
537
|
+
content: flat,
|
|
538
|
+
contents: [{ type: "text", text: flat }],
|
|
539
|
+
});
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
const flat = flattenCanonicalContent(m.content);
|
|
543
|
+
qoderMessages.push({
|
|
544
|
+
role: m.role === "assistant" ? "assistant" : "user",
|
|
545
|
+
content: flat,
|
|
546
|
+
contents: [{ type: "text", text: flat }],
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
const latestUserMessage = [...qoderMessages].reverse().find((m) => m["role"] === "user");
|
|
550
|
+
const latestUserText = typeof latestUserMessage?.["content"] === "string" ? (latestUserMessage["content"] as string) : "";
|
|
551
|
+
const maxTokens = qoderRequestedMaxTokens(request, modelConfig);
|
|
552
|
+
const reasoning = modelConfig.is_reasoning === true;
|
|
553
|
+
const sessionId = qoderStableHash("qoder-session", auth.userId, modelId);
|
|
554
|
+
const recordId = qoderStableRecordId(modelId, allMessages, maxTokens);
|
|
555
|
+
return {
|
|
556
|
+
request_id: randomUUID(),
|
|
557
|
+
request_set_id: recordId,
|
|
558
|
+
chat_record_id: recordId,
|
|
559
|
+
session_id: sessionId,
|
|
560
|
+
stream: true,
|
|
561
|
+
...(MODERN_PROFILE.emptyAliyunUserType ? { aliyun_user_type: "" } : {}),
|
|
562
|
+
chat_task: "FREE_INPUT",
|
|
563
|
+
is_reply: true,
|
|
564
|
+
is_retry: false,
|
|
565
|
+
source: 1,
|
|
566
|
+
version: "3",
|
|
567
|
+
session_type: "qodercli",
|
|
568
|
+
agent_id: "agent_common",
|
|
569
|
+
task_id: "common",
|
|
570
|
+
code_language: "",
|
|
571
|
+
chat_prompt: MODERN_PROFILE.mirrorTopLevelSystem ? systemPrompt : "",
|
|
572
|
+
image_urls: null,
|
|
573
|
+
chat_context: {
|
|
574
|
+
chatPrompt: MODERN_PROFILE.mirrorTopLevelSystem ? systemPrompt : "",
|
|
575
|
+
imageUrls: null,
|
|
576
|
+
extra: { context: [], modelConfig: { key: modelId, is_reasoning: reasoning }, originalContent: latestUserText },
|
|
577
|
+
features: [],
|
|
578
|
+
text: latestUserText,
|
|
579
|
+
},
|
|
580
|
+
model_config: {
|
|
581
|
+
key: modelId,
|
|
582
|
+
display_name: modelConfig.display_name,
|
|
583
|
+
is_vl: modelConfig.is_vl,
|
|
584
|
+
is_reasoning: modelConfig.is_reasoning,
|
|
585
|
+
max_input_tokens: modelConfig.max_input_tokens,
|
|
586
|
+
// Mirrors the roster's stated ceiling. A fixed 32k fallback would be sent
|
|
587
|
+
// for every model alike, so the request would declare a cap the catalog
|
|
588
|
+
// does not publish.
|
|
589
|
+
max_output_tokens: modelConfig.max_output_tokens ?? 65_536,
|
|
590
|
+
source: modelConfig.source ?? "system",
|
|
591
|
+
},
|
|
592
|
+
messages: qoderMessages,
|
|
593
|
+
max_tokens: maxTokens,
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// Stream transformation — decode SSE, unwrap COSY envelope, map OpenAI chat chunks to CanonicalEvent
|
|
598
|
+
|
|
599
|
+
function mapQoderFinishReason(raw: unknown): CanonicalStopReason | undefined {
|
|
600
|
+
if (raw === "stop") return "stop";
|
|
601
|
+
if (raw === "length") return "length";
|
|
602
|
+
if (raw === "tool_calls") return "tool_use";
|
|
603
|
+
if (raw === "content_filter") return "content_filter";
|
|
604
|
+
return undefined;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
async function* qoderBodyToCanonicalEvents(
|
|
608
|
+
body: ReadableStream<Uint8Array>,
|
|
609
|
+
signal: AbortSignal,
|
|
610
|
+
model: string,
|
|
611
|
+
): AsyncIterable<CanonicalEvent> {
|
|
612
|
+
let seq = 1;
|
|
613
|
+
yield { type: "response_start", sequence_number: seq++, model } as CanonicalEvent;
|
|
614
|
+
let finishReason: unknown = undefined;
|
|
615
|
+
let rawUsage: Record<string, unknown> | undefined = undefined;
|
|
616
|
+
let hasTerminal = false;
|
|
617
|
+
|
|
618
|
+
for await (const sse of decodeSseEvents(body, { signal })) {
|
|
619
|
+
const sseData = sse.data;
|
|
620
|
+
let frames: Array<{ event: string | null; data: string }>;
|
|
621
|
+
try {
|
|
622
|
+
frames = qoderEnvelopeToFrames(sseData);
|
|
623
|
+
} catch (e) {
|
|
624
|
+
throw e;
|
|
625
|
+
}
|
|
626
|
+
for (const frame of frames) {
|
|
627
|
+
const data = frame.data.trim();
|
|
628
|
+
if (!data) continue;
|
|
629
|
+
if (data === "[DONE]") {
|
|
630
|
+
hasTerminal = true;
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
633
|
+
let json: Record<string, unknown>;
|
|
634
|
+
try {
|
|
635
|
+
json = JSON.parse(data) as Record<string, unknown>;
|
|
636
|
+
} catch {
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
if (json["usage"] && typeof json["usage"] === "object") rawUsage = json["usage"] as Record<string, unknown>;
|
|
640
|
+
const choices = (json["choices"] as Array<Record<string, unknown>>) ?? [];
|
|
641
|
+
for (const choice of choices) {
|
|
642
|
+
const delta = choice["delta"] as Record<string, unknown> | undefined;
|
|
643
|
+
if (delta) {
|
|
644
|
+
if (typeof delta["content"] === "string" && (delta["content"] as string).length > 0) {
|
|
645
|
+
yield {
|
|
646
|
+
type: "content_delta",
|
|
647
|
+
sequence_number: seq++,
|
|
648
|
+
content: { kind: "text", text: delta["content"] as string },
|
|
649
|
+
} as CanonicalEvent;
|
|
650
|
+
}
|
|
651
|
+
const reasoningText = readReasoningText(delta);
|
|
652
|
+
if (typeof reasoningText === "string" && reasoningText.length > 0) {
|
|
653
|
+
yield {
|
|
654
|
+
type: "content_delta",
|
|
655
|
+
sequence_number: seq++,
|
|
656
|
+
content: { kind: "reasoning", payload: null, summary: reasoningText },
|
|
657
|
+
} as CanonicalEvent;
|
|
658
|
+
}
|
|
659
|
+
const toolCalls = delta["tool_calls"] as Array<Record<string, unknown>> | undefined;
|
|
660
|
+
if (toolCalls) {
|
|
661
|
+
for (const tc of toolCalls) {
|
|
662
|
+
const fn = tc["function"] as Record<string, unknown> | undefined;
|
|
663
|
+
yield {
|
|
664
|
+
type: "tool_call_delta",
|
|
665
|
+
sequence_number: seq++,
|
|
666
|
+
call_id: (tc["id"] as string) ?? `call_${String(tc["index"] ?? 0)}`,
|
|
667
|
+
name: fn?.["name"] as string | undefined,
|
|
668
|
+
arguments_delta: (fn?.["arguments"] as string) ?? "",
|
|
669
|
+
} as CanonicalEvent;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
const fr = choice["finish_reason"];
|
|
674
|
+
if (fr !== null && fr !== undefined) finishReason = fr;
|
|
675
|
+
// Some providers send usage in choices[0].usage or top-level
|
|
676
|
+
if (choice["usage"] && typeof choice["usage"] === "object") rawUsage = choice["usage"] as Record<string, unknown>;
|
|
677
|
+
}
|
|
678
|
+
// Some Qoder chunks carry finish_reason at top level
|
|
679
|
+
if (json["finish_reason"] !== undefined) finishReason = json["finish_reason"];
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
// If no terminal arrived, synthesize one (prevents truncation error)
|
|
683
|
+
const stopReason = mapQoderFinishReason(finishReason);
|
|
684
|
+
const _providerStop = typeof finishReason === "string" ? finishReason : undefined;
|
|
685
|
+
void hasTerminal;
|
|
686
|
+
yield {
|
|
687
|
+
type: "terminal",
|
|
688
|
+
sequence_number: seq++,
|
|
689
|
+
state: "complete",
|
|
690
|
+
...(stopReason === undefined ? {} : { stop_reason: stopReason }),
|
|
691
|
+
...(_providerStop === undefined ? {} : { provider_stop_reason: _providerStop }),
|
|
692
|
+
usage: usageFromProvider(rawUsage),
|
|
693
|
+
} as CanonicalEvent;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// Model catalog — maps QODER_MODEL_CONFIGS (provider) to Cartethyia ModelDefinition
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* The intl catalog this provider publishes.
|
|
700
|
+
*
|
|
701
|
+
* `QODER_MODEL_CONFIGS` is not the source here: it freezes most rows at 180k
|
|
702
|
+
* while the upstream reports 1M for the same keys. That number is not
|
|
703
|
+
* cosmetic: it is sent upstream as `model_config.max_input_tokens`, so a stale
|
|
704
|
+
* low value tells Qoder the conversation window is small and makes it behave
|
|
705
|
+
* that way. Both the published catalog and the dispatched request come from the
|
|
706
|
+
* realm's roster, which carries the upstream's own numbers.
|
|
707
|
+
*/
|
|
708
|
+
export const QODER_MODELS: readonly ModelDefinition[] = QODER_INTL_MODELS;
|
|
709
|
+
|
|
710
|
+
// ProviderAdapter — manual dispatch preserving COSY encryption + envelope logic
|
|
711
|
+
|
|
712
|
+
interface QoderAdapterOptions {
|
|
713
|
+
readonly fetch?: typeof fetch;
|
|
714
|
+
/** Which deployment this adapter dispatches to. Defaults to intl. */
|
|
715
|
+
readonly realm?: QoderRealm;
|
|
716
|
+
/** Bundled provider id, so the adapter's own id matches the route's. */
|
|
717
|
+
readonly providerId?: string;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
class QoderAdapter implements ProviderAdapter {
|
|
721
|
+
readonly provider_id: ProviderId;
|
|
722
|
+
private readonly realm: QoderRealm;
|
|
723
|
+
private readonly fetchFn: typeof fetch;
|
|
724
|
+
|
|
725
|
+
constructor(options: QoderAdapterOptions = {}) {
|
|
726
|
+
this.provider_id = (options.providerId ?? QODER_PROVIDER_ID) as ProviderId;
|
|
727
|
+
this.realm = options.realm ?? "intl";
|
|
728
|
+
this.fetchFn = options.fetch ?? globalThis.fetch;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* The static roster for this adapter's realm.
|
|
733
|
+
*
|
|
734
|
+
* A single merged table advertised CN-only keys to global accounts and vice
|
|
735
|
+
* versa, so a request was routed to a deployment that refuses it.
|
|
736
|
+
*/
|
|
737
|
+
#modelConfigs(): Record<string, QoderModelConfig> {
|
|
738
|
+
return (this.realm === "cn" ? QODER_CN_MODEL_CONFIGS : QODER_INTL_MODEL_CONFIGS) as Record<
|
|
739
|
+
string,
|
|
740
|
+
QoderModelConfig
|
|
741
|
+
>;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
async *dispatch(
|
|
745
|
+
request: CanonicalRequest,
|
|
746
|
+
candidate: ProviderDispatchTarget,
|
|
747
|
+
context: ProviderDispatchContext,
|
|
748
|
+
): AsyncIterable<CanonicalEvent> {
|
|
749
|
+
if (candidate.wire_family !== "chat") {
|
|
750
|
+
throw new GatewayError("capability_unsupported", 400, `qoder supports chat only, got ${candidate.wire_family}`);
|
|
751
|
+
}
|
|
752
|
+
const secret = readCredentialSecret(context.credential, "Qoder credential is required");
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* A caller-facing virtual name is translated to its roster key here.
|
|
756
|
+
*
|
|
757
|
+
* The translation has to happen before the config lookup and before the
|
|
758
|
+
* request body: the body carries the roster key, and looking up the
|
|
759
|
+
* virtual id in the config table would fall through to the synthetic
|
|
760
|
+
* default and silently dispatch under-declared capabilities.
|
|
761
|
+
*/
|
|
762
|
+
const requestedModelId = candidate.model_id || request.model;
|
|
763
|
+
if (isQoderVirtualModel(requestedModelId) && !this.#modelConfigs()[qoderDispatchModelId(requestedModelId)]) {
|
|
764
|
+
throw new GatewayError(
|
|
765
|
+
"model_not_found",
|
|
766
|
+
404,
|
|
767
|
+
`Qoder virtual model ${requestedModelId} resolves to a roster key this realm does not serve`,
|
|
768
|
+
{ virtual_model: requestedModelId, target: qoderDispatchModelId(requestedModelId) },
|
|
769
|
+
"upstream",
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
const modelId = qoderDispatchModelId(requestedModelId);
|
|
773
|
+
const modelConfig = this.#modelConfigs()[modelId] ?? {
|
|
774
|
+
id: modelId,
|
|
775
|
+
display_name: modelId,
|
|
776
|
+
max_input_tokens: 180_000,
|
|
777
|
+
is_vl: true,
|
|
778
|
+
is_reasoning: false,
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
const lifecycle = createUpstreamDeadlineLifecycle(context);
|
|
782
|
+
|
|
783
|
+
// Outbound fetch respects SSRF binding when provided
|
|
784
|
+
const outboundFetch: typeof fetch = (context.outbound_fetch as unknown as typeof fetch) ?? this.fetchFn;
|
|
785
|
+
|
|
786
|
+
const fetcher: QoderFetcher = (url, init) =>
|
|
787
|
+
outboundFetch(url, { ...init, signal: lifecycle.signal } as RequestInit) as Promise<Response>;
|
|
788
|
+
|
|
789
|
+
try {
|
|
790
|
+
const version = getQoderVersion();
|
|
791
|
+
// Dual mode like Cline: a pasted PAT is exchanged for a job token per
|
|
792
|
+
// request, while an OAuth device token is used directly with the
|
|
793
|
+
// identity the login persisted.
|
|
794
|
+
const auth = isQoderPat(secret)
|
|
795
|
+
? await exchangeQoderPat(secret, lifecycle.signal, fetcher, version, this.realm)
|
|
796
|
+
: qoderOAuthAuth(secret, context.credential.auth_state, context.credential.account_id);
|
|
797
|
+
const qoderBody = buildQoderRequest(modelId, request, modelConfig, auth);
|
|
798
|
+
// A full URL from the route wins, but the fallback is this realm's
|
|
799
|
+
// gateway — not the intl host hardcoded into `MODERN_PROFILE`. Dispatching
|
|
800
|
+
// a CN credential to `*.qoder.sh` is refused outright.
|
|
801
|
+
const url =
|
|
802
|
+
candidate.endpoint_path && candidate.endpoint_path.startsWith("http")
|
|
803
|
+
? candidate.endpoint_path
|
|
804
|
+
: `${qoderGatewayCandidates(this.realm)[0] ?? ""}${QODER_CHAT_PATH}`;
|
|
805
|
+
|
|
806
|
+
const response = await callQoder(url, qoderBody, modelId, auth, lifecycle.signal, fetcher, version);
|
|
807
|
+
if (!response.ok) throw await mapUpstreamHttpError(response, "qoder");
|
|
808
|
+
if (!response.body) throw new GatewayError("platform_unavailable", 502, "Qoder returned empty body", {}, "upstream");
|
|
809
|
+
// Headers arrived: the pre-stream deadline has served its purpose.
|
|
810
|
+
// From here the gateway stall/first-chunk watchdog (propagated via
|
|
811
|
+
// context.abort_signal) governs the body. Keeping this timer armed
|
|
812
|
+
// would kill healthy long streams at the stale pre-stream deadline.
|
|
813
|
+
lifecycle.release();
|
|
814
|
+
yield* qoderBodyToCanonicalEvents(response.body as ReadableStream<Uint8Array>, lifecycle.signal, request.model);
|
|
815
|
+
} catch (err: unknown) {
|
|
816
|
+
const abortError = abortGatewayError(lifecycle, err, context.abort_signal);
|
|
817
|
+
if (abortError) throw abortError;
|
|
818
|
+
throw err;
|
|
819
|
+
} finally {
|
|
820
|
+
lifecycle.release();
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
export function createQoderAdapter(options: QoderAdapterOptions = {}): ProviderAdapter {
|
|
826
|
+
return new QoderAdapter(options);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/**
|
|
830
|
+
* Adapter for the China deployment.
|
|
831
|
+
*
|
|
832
|
+
* Same wire, different gateway host and roster — see `qoder-realm.ts`.
|
|
833
|
+
*/
|
|
834
|
+
export function createQoderCnAdapter(options: QoderAdapterOptions = {}): ProviderAdapter {
|
|
835
|
+
return new QoderAdapter({ ...options, realm: "cn", providerId: "qodercn" });
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* A COSY signer for one already-resolved account identity.
|
|
840
|
+
*
|
|
841
|
+
* Discovery needs signed headers but lives in `qoder-catalog.ts`, which cannot
|
|
842
|
+
* import this module (the adapter imports its rosters — that would be a cycle).
|
|
843
|
+
* Exporting the signer keeps one signing implementation and lets discovery
|
|
844
|
+
* stay pure and injectable.
|
|
845
|
+
*/
|
|
846
|
+
export function qoderSignerFor(
|
|
847
|
+
auth: QoderAuth,
|
|
848
|
+
version = getQoderVersion(),
|
|
849
|
+
): (url: string, body: string) => Record<string, string> {
|
|
850
|
+
return (url: string, body: string): Record<string, string> =>
|
|
851
|
+
qoderBuildCosyHeaders(Buffer.from(body, "latin1"), url, auth, version);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/** Qoder's own base64 alphabet transform, for a request body. */
|
|
855
|
+
export function qoderEncodeBody(plaintext: Uint8Array | string): string {
|
|
856
|
+
const text = typeof plaintext === "string" ? plaintext : new TextDecoder().decode(plaintext);
|
|
857
|
+
return Buffer.from(transformQoderBody(text)).toString("latin1");
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* Resolves the dispatch identity for one Qoder credential.
|
|
862
|
+
*
|
|
863
|
+
* Shared by the adapter and by discovery: both need the same `QoderAuth` the
|
|
864
|
+
* COSY signature is built from, and duplicating the PAT-vs-OAuth branch would
|
|
865
|
+
* let the two drift.
|
|
866
|
+
*/
|
|
867
|
+
export async function resolveQoderAuth(input: {
|
|
868
|
+
readonly secret: string;
|
|
869
|
+
readonly authState: unknown;
|
|
870
|
+
readonly signal: AbortSignal;
|
|
871
|
+
readonly fetcher: (url: string, init: RequestInit) => Promise<Response>;
|
|
872
|
+
/** Which deployment mints the job token; a PAT is realm-bound. */
|
|
873
|
+
readonly realm?: QoderRealm;
|
|
874
|
+
}): Promise<QoderAuth> {
|
|
875
|
+
const realm = input.realm ?? "intl";
|
|
876
|
+
if (isQoderPat(input.secret)) {
|
|
877
|
+
return await exchangeQoderPat(input.secret, input.signal, input.fetcher, getQoderVersion(), realm);
|
|
878
|
+
}
|
|
879
|
+
// Discovery is handed only the secret and the persisted state, so the
|
|
880
|
+
// account id is read from that state — the same place `qoderOAuthAuth` reads
|
|
881
|
+
// it — rather than being a separate field callers can pass wrongly.
|
|
882
|
+
const state = parseQoderOAuthState(input.authState);
|
|
883
|
+
return qoderOAuthAuth(input.secret, input.authState, state?.userId);
|
|
884
|
+
}
|