swayrouter 1.0.0
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 +43 -0
- package/CHANGELOG.md +132 -0
- package/LICENSE +30 -0
- package/README.id.md +633 -0
- package/README.md +628 -0
- package/cli/cli.js +388 -0
- package/cli/package.json +4 -0
- package/cli/src/cli/commands/xaiVideo.js +280 -0
- package/cli/src/cli/tray/SwayAvatar.svg +20 -0
- package/cli/src/cli/tray/autostart.js +351 -0
- package/cli/src/cli/tray/icon.ico +0 -0
- package/cli/src/cli/tray/icon.png +0 -0
- package/cli/src/cli/tray/tray.js +258 -0
- package/cli/src/cli/tray/tray.ps1 +129 -0
- package/cli/src/cli/tray/trayHost.js +157 -0
- package/cli/src/cli/tray/trayWin.js +83 -0
- package/cli/src/cli/utils/display.js +123 -0
- package/cli/src/cli/utils/input.js +138 -0
- package/cli/src/runtime/discover.js +153 -0
- package/cli/src/runtime/lifecycle.js +333 -0
- package/cli/src/runtime/runMenu.js +250 -0
- package/cli/src/runtime/uiAdapters.js +12 -0
- package/cli/src/runtime/update.js +130 -0
- package/dashboard/dist/assets/CliTools-Ch3gKuNw.js +24 -0
- package/dashboard/dist/assets/Combo-BBCldjWi.js +1 -0
- package/dashboard/dist/assets/ConsoleLog-wywAYcPT.js +6 -0
- package/dashboard/dist/assets/Dashboard-DxzyF41b.js +1 -0
- package/dashboard/dist/assets/Header-C_9Su9BF.js +1 -0
- package/dashboard/dist/assets/Keys-D0ufgcsc.js +1 -0
- package/dashboard/dist/assets/Login-BeKpipS_.js +1 -0
- package/dashboard/dist/assets/ManageApiKey-CVXsmW2B.js +1 -0
- package/dashboard/dist/assets/OAuthCallback-D_UZYcF6.js +1 -0
- package/dashboard/dist/assets/Provider-BU7JfTqm.js +4 -0
- package/dashboard/dist/assets/ProviderModelAccordion-BAifkfVR.js +1 -0
- package/dashboard/dist/assets/Proxy-CU15VJ--.js +1 -0
- package/dashboard/dist/assets/QuotaMonitor-HzGK7bcc.js +1 -0
- package/dashboard/dist/assets/Settings-Dt4y2dC0.js +1 -0
- package/dashboard/dist/assets/Setup-DSDUwU7N.js +1 -0
- package/dashboard/dist/assets/StatusBadge-1nAOyHU2.js +1 -0
- package/dashboard/dist/assets/SwayChat-Bup7up6D.js +10 -0
- package/dashboard/dist/assets/Usage-eWnrPQ_v.js +4 -0
- package/dashboard/dist/assets/accordion-DOOPDDgt.js +1 -0
- package/dashboard/dist/assets/admin-extras-api-CCdu5TFM.js +1 -0
- package/dashboard/dist/assets/api-keys-api-ifAC2Ok3.js +1 -0
- package/dashboard/dist/assets/card-CC0Omjcv.js +1 -0
- package/dashboard/dist/assets/chatStudio-C0zPz_Z_.js +2 -0
- package/dashboard/dist/assets/checkbox-7SZBrmbN.js +1 -0
- package/dashboard/dist/assets/connections-api-_N54UD3S.js +1 -0
- package/dashboard/dist/assets/copy-button-qBvM6H9z.js +1 -0
- package/dashboard/dist/assets/currency-api-SV9CBf5e.js +1 -0
- package/dashboard/dist/assets/flip-button-BGgkjtl0.js +1 -0
- package/dashboard/dist/assets/frame-cUiti6Lq.js +1 -0
- package/dashboard/dist/assets/icons-vendor-CUx7SB5V.js +541 -0
- package/dashboard/dist/assets/index-B6j-9zHy.js +10 -0
- package/dashboard/dist/assets/index-C-_l0yYz.css +1 -0
- package/dashboard/dist/assets/live-mode-CHShLY4E.js +1 -0
- package/dashboard/dist/assets/markdown-vendor-BY8e1gQ8.js +29 -0
- package/dashboard/dist/assets/motion-vendor-g5fpGzmJ.js +9 -0
- package/dashboard/dist/assets/oauth-callback-CqYQzyd7.js +1 -0
- package/dashboard/dist/assets/query-vendor-DVevaSVS.js +1 -0
- package/dashboard/dist/assets/react-vendor-xnYrwzsL.js +61 -0
- package/dashboard/dist/assets/ripple-button-l8aMiVVG.js +1 -0
- package/dashboard/dist/assets/segmented-CqmEnQjZ.js +1 -0
- package/dashboard/dist/assets/select-D8pUtMXp.js +1 -0
- package/dashboard/dist/assets/skeleton-7uJDiIXe.js +1 -0
- package/dashboard/dist/assets/table-D0M60Tu1.js +1 -0
- package/dashboard/dist/assets/tabs-C6YkuulG.js +1 -0
- package/dashboard/dist/assets/tunnel-api-DDPsBaDV.js +1 -0
- package/dashboard/dist/assets/ui-vendor-BuM9f2NY.js +17 -0
- package/dashboard/dist/image2/SwayAvatar.svg +20 -0
- package/dashboard/dist/index.html +32 -0
- package/dashboard/dist/logo/avatar.svg +1 -0
- package/dashboard/dist/logo/sway.svg +20 -0
- package/dashboard/dist/logo/swaychat.svg +20 -0
- package/dashboard/dist/providers/SwayCustom.svg +19 -0
- package/dashboard/dist/providers/alicode.svg +12 -0
- package/dashboard/dist/providers/anthropic.svg +11 -0
- package/dashboard/dist/providers/antigravity.svg +3 -0
- package/dashboard/dist/providers/azure.svg +5 -0
- package/dashboard/dist/providers/baidu.svg +3 -0
- package/dashboard/dist/providers/blackbox.svg +5 -0
- package/dashboard/dist/providers/byteplus.svg +3 -0
- package/dashboard/dist/providers/cerebras.svg +4 -0
- package/dashboard/dist/providers/chutes.svg +4 -0
- package/dashboard/dist/providers/claude.svg +3 -0
- package/dashboard/dist/providers/cline.svg +11 -0
- package/dashboard/dist/providers/cloudflare-ai.svg +4 -0
- package/dashboard/dist/providers/codebuddy.svg +11 -0
- package/dashboard/dist/providers/codex.svg +10 -0
- package/dashboard/dist/providers/commandcode.svg +3 -0
- package/dashboard/dist/providers/cursor.svg +3 -0
- package/dashboard/dist/providers/deepseek.svg +10 -0
- package/dashboard/dist/providers/featherless.svg +3 -0
- package/dashboard/dist/providers/fireworks.svg +3 -0
- package/dashboard/dist/providers/gemini.svg +3 -0
- package/dashboard/dist/providers/geminicli.svg +11 -0
- package/dashboard/dist/providers/github.svg +3 -0
- package/dashboard/dist/providers/grok.svg +3 -0
- package/dashboard/dist/providers/groq.svg +3 -0
- package/dashboard/dist/providers/hermesagent.svg +1 -0
- package/dashboard/dist/providers/kilocode.svg +15 -0
- package/dashboard/dist/providers/kimchi.svg +3 -0
- package/dashboard/dist/providers/kimi.svg +4 -0
- package/dashboard/dist/providers/kiro.svg +10 -0
- package/dashboard/dist/providers/meta.svg +3 -0
- package/dashboard/dist/providers/mimo.svg +10 -0
- package/dashboard/dist/providers/minimax.svg +3 -0
- package/dashboard/dist/providers/mistral.svg +3 -0
- package/dashboard/dist/providers/morph.svg +3 -0
- package/dashboard/dist/providers/nebius.svg +4 -0
- package/dashboard/dist/providers/nvidia.svg +3 -0
- package/dashboard/dist/providers/ollama.svg +3 -0
- package/dashboard/dist/providers/omp.svg +5 -0
- package/dashboard/dist/providers/openai.svg +3 -0
- package/dashboard/dist/providers/opencode.svg +3 -0
- package/dashboard/dist/providers/openrouter.svg +3 -0
- package/dashboard/dist/providers/perplexity.svg +3 -0
- package/dashboard/dist/providers/poolside.svg +36 -0
- package/dashboard/dist/providers/qoder.svg +11 -0
- package/dashboard/dist/providers/sumopod.svg +3 -0
- package/dashboard/dist/providers/tencent.svg +5 -0
- package/dashboard/dist/providers/together.svg +12 -0
- package/dashboard/dist/providers/trae.svg +3 -0
- package/dashboard/dist/providers/venice.svg +4 -0
- package/dashboard/dist/providers/vercel.svg +3 -0
- package/dashboard/dist/providers/vertex.svg +17 -0
- package/dashboard/dist/providers/windsurf.svg +10 -0
- package/dashboard/dist/providers/zai.svg +3 -0
- package/dashboard/tsconfig.json +13 -0
- package/package.json +87 -0
- package/src/config/appConstants.js +153 -0
- package/src/config/defaultThinkingSignature.js +7 -0
- package/src/config/errorConfig.js +117 -0
- package/src/config/grokCli.js +9 -0
- package/src/config/kiroConstants.js +235 -0
- package/src/config/mediaConfig.js +20 -0
- package/src/config/ollamaModels.js +18 -0
- package/src/config/providerModels.js +109 -0
- package/src/config/providers.js +17 -0
- package/src/config/runtimeConfig.js +84 -0
- package/src/dashboardGuard.js +254 -0
- package/src/executors/antigravity.js +389 -0
- package/src/executors/azure.js +49 -0
- package/src/executors/base.js +225 -0
- package/src/executors/codebuddy-cn.js +23 -0
- package/src/executors/codebuddy-intl.js +60 -0
- package/src/executors/codex.js +436 -0
- package/src/executors/commandcode.js +83 -0
- package/src/executors/cursor.js +1075 -0
- package/src/executors/default.js +294 -0
- package/src/executors/gemini-cli.js +88 -0
- package/src/executors/github.js +393 -0
- package/src/executors/grok-cli.js +510 -0
- package/src/executors/grok-web.js +342 -0
- package/src/executors/index.js +86 -0
- package/src/executors/kimchi.js +116 -0
- package/src/executors/kiro.js +1155 -0
- package/src/executors/ollama-local.js +14 -0
- package/src/executors/opencode-go.js +47 -0
- package/src/executors/opencode.js +31 -0
- package/src/executors/qoder.js +433 -0
- package/src/executors/trae.js +318 -0
- package/src/executors/vertex.js +151 -0
- package/src/executors/windsurf.js +555 -0
- package/src/executors/xiaomi-tokenplan.js +16 -0
- package/src/handlers/chatCore/nonStreamingHandler.js +370 -0
- package/src/handlers/chatCore/requestDetail.js +130 -0
- package/src/handlers/chatCore/sseToJsonHandler.js +318 -0
- package/src/handlers/chatCore/streamingHandler.js +250 -0
- package/src/handlers/chatCore.js +652 -0
- package/src/lib/auth/dashboardSession.js +83 -0
- package/src/lib/auth/loginLimiter.js +99 -0
- package/src/lib/cache/ttlCache.js +153 -0
- package/src/lib/consoleLogBuffer.js +127 -0
- package/src/lib/dataDir.js +45 -0
- package/src/lib/db/adapters/betterSqliteAdapter.js +36 -0
- package/src/lib/db/adapters/bunSqliteAdapter.js +47 -0
- package/src/lib/db/adapters/nodeSqliteAdapter.js +65 -0
- package/src/lib/db/adapters/runtimeHelpers.js +23 -0
- package/src/lib/db/adapters/sqljsAdapter.js +103 -0
- package/src/lib/db/backup.js +75 -0
- package/src/lib/db/driver.js +110 -0
- package/src/lib/db/helpers/jsonCol.js +18 -0
- package/src/lib/db/helpers/kvStore.js +48 -0
- package/src/lib/db/helpers/metaStore.js +32 -0
- package/src/lib/db/index.js +473 -0
- package/src/lib/db/migrate.js +267 -0
- package/src/lib/db/migrations/001-initial.js +12 -0
- package/src/lib/db/migrations/002-prune-headroom-pxpipe-settings.js +32 -0
- package/src/lib/db/migrations/004-request-details-trace-id.js +12 -0
- package/src/lib/db/migrations/005-request-details-payload-capture.js +11 -0
- package/src/lib/db/migrations/007-headroom-readd-settings.js +35 -0
- package/src/lib/db/migrations/008-account-model-locks.js +22 -0
- package/src/lib/db/migrations/009-warp-pool-lifecycle.js +5 -0
- package/src/lib/db/migrations/010-api-key-file-permissions.js +7 -0
- package/src/lib/db/migrations/011-remove-upstream-user-agent-profile.js +41 -0
- package/src/lib/db/migrations/012-remove-headroom-settings.js +33 -0
- package/src/lib/db/migrations/013-default-fill-first-routing.js +20 -0
- package/src/lib/db/migrations/014-normalize-fill-first-routing.js +37 -0
- package/src/lib/db/migrations/015-remove-ip-ban-tables.js +8 -0
- package/src/lib/db/migrations/016-prune-legacy-codebuddy-intl-models.js +25 -0
- package/src/lib/db/migrations/index.js +20 -0
- package/src/lib/db/nineRouterMigration.js +785 -0
- package/src/lib/db/paths.js +23 -0
- package/src/lib/db/repos/accountModelLocksRepo.js +74 -0
- package/src/lib/db/repos/aliasRepo.js +108 -0
- package/src/lib/db/repos/apiKeysRepo.js +204 -0
- package/src/lib/db/repos/combosRepo.js +85 -0
- package/src/lib/db/repos/connectionsRepo.js +522 -0
- package/src/lib/db/repos/disabledModelsRepo.js +63 -0
- package/src/lib/db/repos/nodesRepo.js +127 -0
- package/src/lib/db/repos/pricingRepo.js +108 -0
- package/src/lib/db/repos/proxyPoolsRepo.js +104 -0
- package/src/lib/db/repos/requestDetailsRepo.js +345 -0
- package/src/lib/db/repos/settingsRepo.js +135 -0
- package/src/lib/db/repos/usageRepo.js +1335 -0
- package/src/lib/db/schema.js +179 -0
- package/src/lib/db/version.js +32 -0
- package/src/lib/db/writeBehind.js +174 -0
- package/src/lib/disabledModelsDb.js +3 -0
- package/src/lib/env.ts +175 -0
- package/src/lib/errors/publicError.js +18 -0
- package/src/lib/grokBuildConfig.js +243 -0
- package/src/lib/guardRequest.js +51 -0
- package/src/lib/localDb.js +21 -0
- package/src/lib/mcp/stdioSseBridge.js +174 -0
- package/src/lib/network/connectionProxy.js +200 -0
- package/src/lib/network/proxyTest.js +158 -0
- package/src/lib/network/proxyUrl.js +76 -0
- package/src/lib/network/relay.js +71 -0
- package/src/lib/oauth/constants/oauth.js +152 -0
- package/src/lib/oauth/constants/xai.js +43 -0
- package/src/lib/oauth/kiroExternalIdp.js +167 -0
- package/src/lib/oauth/providerHelpers.js +90 -0
- package/src/lib/oauth/providers/_shared.js +12 -0
- package/src/lib/oauth/providers/antigravity.js +126 -0
- package/src/lib/oauth/providers/claude.js +60 -0
- package/src/lib/oauth/providers/cline.js +62 -0
- package/src/lib/oauth/providers/clinepass.js +62 -0
- package/src/lib/oauth/providers/codebuddy-cn.js +75 -0
- package/src/lib/oauth/providers/codebuddy-intl.js +73 -0
- package/src/lib/oauth/providers/codex.js +72 -0
- package/src/lib/oauth/providers/cursor.js +18 -0
- package/src/lib/oauth/providers/gemini-cli.js +84 -0
- package/src/lib/oauth/providers/github.js +96 -0
- package/src/lib/oauth/providers/grok-cli.js +123 -0
- package/src/lib/oauth/providers/index.js +233 -0
- package/src/lib/oauth/providers/kilocode.js +60 -0
- package/src/lib/oauth/providers/kimchi.js +79 -0
- package/src/lib/oauth/providers/kimi.js +79 -0
- package/src/lib/oauth/providers/kiro.js +147 -0
- package/src/lib/oauth/providers/qoder.js +90 -0
- package/src/lib/oauth/providers/trae.js +244 -0
- package/src/lib/oauth/providers/windsurf.js +123 -0
- package/src/lib/oauth/providers/xai.js +95 -0
- package/src/lib/oauth/providers.js +16 -0
- package/src/lib/oauth/services/cursor.js +132 -0
- package/src/lib/oauth/services/kiro.js +351 -0
- package/src/lib/oauth/services/oauth.js +135 -0
- package/src/lib/oauth/services/qoder.js +151 -0
- package/src/lib/oauth/services/xai.js +204 -0
- package/src/lib/oauth/utils/ideDetect.js +55 -0
- package/src/lib/oauth/utils/pkce.js +25 -0
- package/src/lib/oauth/utils/server.js +732 -0
- package/src/lib/oauth/utils/ui.js +43 -0
- package/src/lib/providerNormalization.js +40 -0
- package/src/lib/qoder/constants.js +1 -0
- package/src/lib/requestDetailsDb.js +3 -0
- package/src/lib/resourceProfile.ts +124 -0
- package/src/lib/runtimeSecrets.ts +130 -0
- package/src/lib/security/payloadCapture.js +87 -0
- package/src/lib/tunnel/cloudflare/cloudflared.js +436 -0
- package/src/lib/tunnel/cloudflare/config.js +8 -0
- package/src/lib/tunnel/cloudflare/healthCheck.js +37 -0
- package/src/lib/tunnel/cloudflare/manager.js +149 -0
- package/src/lib/tunnel/cloudflare/pid.js +31 -0
- package/src/lib/tunnel/cloudflare/publicUrl.js +41 -0
- package/src/lib/tunnel/cloudflare/requirements.js +12 -0
- package/src/lib/tunnel/index.js +26 -0
- package/src/lib/tunnel/shared/dnsResolver.js +16 -0
- package/src/lib/tunnel/shared/internetCheck.js +30 -0
- package/src/lib/tunnel/shared/state.js +45 -0
- package/src/lib/tunnel/shared/watchdogConfig.js +6 -0
- package/src/lib/update/installer.js +52 -0
- package/src/lib/update/version.js +116 -0
- package/src/lib/usageDb.js +6 -0
- package/src/lib/usageRefresh.js +88 -0
- package/src/lifecycle/appLifecycle.ts +99 -0
- package/src/models.js +36 -0
- package/src/next/headers.js +12 -0
- package/src/next/server.js +74 -0
- package/src/observability/metrics.js +439 -0
- package/src/observability/tracing.js +107 -0
- package/src/open-sse-index.js +70 -0
- package/src/providers/capabilities.js +299 -0
- package/src/providers/display.ts +142 -0
- package/src/providers/index.js +27 -0
- package/src/providers/models/helpers.js +19 -0
- package/src/providers/models/namePatterns.js +27 -0
- package/src/providers/models/schema.js +32 -0
- package/src/providers/pricing.js +248 -0
- package/src/providers/registry/agentrouter.js +52 -0
- package/src/providers/registry/alicode-intl.js +32 -0
- package/src/providers/registry/alicode.js +33 -0
- package/src/providers/registry/alims-intl.js +30 -0
- package/src/providers/registry/anthropic.js +29 -0
- package/src/providers/registry/antigravity.js +139 -0
- package/src/providers/registry/azure.js +23 -0
- package/src/providers/registry/baidu.js +35 -0
- package/src/providers/registry/blackbox.js +41 -0
- package/src/providers/registry/byteplus.js +36 -0
- package/src/providers/registry/cerebras.js +33 -0
- package/src/providers/registry/chutes.js +26 -0
- package/src/providers/registry/claude.js +80 -0
- package/src/providers/registry/cline.js +53 -0
- package/src/providers/registry/clinepass.js +62 -0
- package/src/providers/registry/cloudflare-ai.js +84 -0
- package/src/providers/registry/codebuddy-cn.js +95 -0
- package/src/providers/registry/codebuddy-intl.js +91 -0
- package/src/providers/registry/codex.js +145 -0
- package/src/providers/registry/commandcode.js +45 -0
- package/src/providers/registry/cursor.js +61 -0
- package/src/providers/registry/deepseek.js +55 -0
- package/src/providers/registry/featherless.js +36 -0
- package/src/providers/registry/fireworks.js +34 -0
- package/src/providers/registry/gemini-cli.js +59 -0
- package/src/providers/registry/gemini.js +76 -0
- package/src/providers/registry/github.js +131 -0
- package/src/providers/registry/glm-cn.js +35 -0
- package/src/providers/registry/glm.js +58 -0
- package/src/providers/registry/grok-cli.js +87 -0
- package/src/providers/registry/grok-web.js +41 -0
- package/src/providers/registry/groq.js +38 -0
- package/src/providers/registry/index.js +77 -0
- package/src/providers/registry/kilo-gateway.js +34 -0
- package/src/providers/registry/kilocode.js +48 -0
- package/src/providers/registry/kimchi.js +49 -0
- package/src/providers/registry/kimi.js +82 -0
- package/src/providers/registry/kiro.js +132 -0
- package/src/providers/registry/meta.js +34 -0
- package/src/providers/registry/minimax-cn.js +90 -0
- package/src/providers/registry/minimax.js +90 -0
- package/src/providers/registry/mistral.js +36 -0
- package/src/providers/registry/morph.js +31 -0
- package/src/providers/registry/nebius.js +26 -0
- package/src/providers/registry/nvidia.js +55 -0
- package/src/providers/registry/ollama-local.js +18 -0
- package/src/providers/registry/ollama.js +38 -0
- package/src/providers/registry/openai.js +114 -0
- package/src/providers/registry/opencode-go.js +41 -0
- package/src/providers/registry/opencode.js +26 -0
- package/src/providers/registry/openrouter.js +36 -0
- package/src/providers/registry/perplexity.js +56 -0
- package/src/providers/registry/poolside.js +30 -0
- package/src/providers/registry/qoder.js +58 -0
- package/src/providers/registry/sumopod.js +84 -0
- package/src/providers/registry/tencent.js +29 -0
- package/src/providers/registry/together.js +38 -0
- package/src/providers/registry/trae.js +71 -0
- package/src/providers/registry/venice.js +69 -0
- package/src/providers/registry/vercel-ai-gateway.js +37 -0
- package/src/providers/registry/vertex-partner.js +31 -0
- package/src/providers/registry/vertex.js +33 -0
- package/src/providers/registry/windsurf.js +129 -0
- package/src/providers/registry/xai.js +46 -0
- package/src/providers/registry/xiaomi-mimo.js +66 -0
- package/src/providers/registry/xiaomi-tokenplan.js +58 -0
- package/src/providers/registryEntry.js +3 -0
- package/src/providers/schema.js +33 -0
- package/src/providers/shared.js +114 -0
- package/src/providers/thinkingLevels.js +49 -0
- package/src/routes/api/auth/login/route.js +82 -0
- package/src/routes/api/auth/logout/route.js +9 -0
- package/src/routes/api/auth/reset-password/route.js +12 -0
- package/src/routes/api/auth/status/route.js +33 -0
- package/src/routes/api/auth/verify-password/route.js +39 -0
- package/src/routes/api/chat/tools/curl/route.js +100 -0
- package/src/routes/api/chat/tools/router/route.js +215 -0
- package/src/routes/api/chat/tools/search/route.js +362 -0
- package/src/routes/api/chat/tools/shell/route.js +228 -0
- package/src/routes/api/cli-tools/all-statuses/route.js +37 -0
- package/src/routes/api/cli-tools/claude-settings/route.js +261 -0
- package/src/routes/api/cli-tools/codex-settings/route.js +220 -0
- package/src/routes/api/cli-tools/cowork-mcp-registry/route.js +80 -0
- package/src/routes/api/cli-tools/cowork-mcp-tools/route.js +96 -0
- package/src/routes/api/cli-tools/cowork-settings/route.js +380 -0
- package/src/routes/api/cli-tools/grok-build-settings/route.js +152 -0
- package/src/routes/api/cli-tools/hermes-settings/route.js +169 -0
- package/src/routes/api/cli-tools/kilo-settings/route.js +131 -0
- package/src/routes/api/cli-tools/model-mappings/route.js +43 -0
- package/src/routes/api/cli-tools/omp-settings/route.js +266 -0
- package/src/routes/api/cli-tools/opencode-settings/route.js +243 -0
- package/src/routes/api/combos/[id]/route.js +73 -0
- package/src/routes/api/combos/route.js +43 -0
- package/src/routes/api/health/live/route.js +7 -0
- package/src/routes/api/health/ready/route.js +19 -0
- package/src/routes/api/health/route.js +17 -0
- package/src/routes/api/inflight/stream/route.js +24 -0
- package/src/routes/api/init/route.js +3 -0
- package/src/routes/api/keys/[id]/route.js +71 -0
- package/src/routes/api/keys/route.js +41 -0
- package/src/routes/api/logs/route.js +19 -0
- package/src/routes/api/mcp/[plugin]/message/route.js +23 -0
- package/src/routes/api/mcp/[plugin]/sse/route.js +35 -0
- package/src/routes/api/models/availability/route.js +104 -0
- package/src/routes/api/models/custom/route.js +58 -0
- package/src/routes/api/models/disabled/route.js +47 -0
- package/src/routes/api/models/route.js +64 -0
- package/src/routes/api/models/test/ping.js +236 -0
- package/src/routes/api/models/test/route.js +14 -0
- package/src/routes/api/oauth/[provider]/[action]/route.js +491 -0
- package/src/routes/api/oauth/codex/bulk-import/route.js +105 -0
- package/src/routes/api/oauth/codex/import-token/route.js +82 -0
- package/src/routes/api/oauth/cursor/auto-import/route.js +250 -0
- package/src/routes/api/oauth/cursor/import/route.js +86 -0
- package/src/routes/api/oauth/kiro/api-key/route.js +60 -0
- package/src/routes/api/oauth/kiro/auto-import/route.js +120 -0
- package/src/routes/api/oauth/kiro/import/route.js +72 -0
- package/src/routes/api/oauth/kiro/import-cli-proxy/route.js +42 -0
- package/src/routes/api/oauth/kiro/social-authorize/route.js +40 -0
- package/src/routes/api/oauth/kiro/social-exchange/route.js +62 -0
- package/src/routes/api/pricing/route.js +96 -0
- package/src/routes/api/provider-nodes/[id]/route.js +106 -0
- package/src/routes/api/provider-nodes/route.js +110 -0
- package/src/routes/api/provider-nodes/validate/route.js +197 -0
- package/src/routes/api/providers/[id]/models/route.js +716 -0
- package/src/routes/api/providers/[id]/route.js +195 -0
- package/src/routes/api/providers/[id]/test/route.js +27 -0
- package/src/routes/api/providers/[id]/test/testUtils.js +912 -0
- package/src/routes/api/providers/[id]/test-models/route.js +211 -0
- package/src/routes/api/providers/catalog/route.js +58 -0
- package/src/routes/api/providers/client/route.js +133 -0
- package/src/routes/api/providers/counts/route.js +43 -0
- package/src/routes/api/providers/kilo/free-models/route.js +58 -0
- package/src/routes/api/providers/route.js +361 -0
- package/src/routes/api/providers/suggested-models/filters.js +24 -0
- package/src/routes/api/providers/suggested-models/route.js +34 -0
- package/src/routes/api/providers/test-batch/route.js +145 -0
- package/src/routes/api/providers/validate/route.js +633 -0
- package/src/routes/api/proxy-pools/[id]/route.js +129 -0
- package/src/routes/api/proxy-pools/[id]/test/route.js +64 -0
- package/src/routes/api/proxy-pools/cloudflare-deploy/route.js +183 -0
- package/src/routes/api/proxy-pools/route.js +85 -0
- package/src/routes/api/proxy-pools/vercel-deploy/route.js +181 -0
- package/src/routes/api/settings/currency/route.js +29 -0
- package/src/routes/api/settings/database/path/route.js +8 -0
- package/src/routes/api/settings/database/route.js +89 -0
- package/src/routes/api/settings/migrate-9router/route.js +81 -0
- package/src/routes/api/settings/require-login/route.js +14 -0
- package/src/routes/api/settings/route.js +186 -0
- package/src/routes/api/shutdown/route.js +25 -0
- package/src/routes/api/tags/route.js +17 -0
- package/src/routes/api/translator/console-logs/route.js +28 -0
- package/src/routes/api/translator/console-logs/stream/route.js +95 -0
- package/src/routes/api/tunnel/disable/route.js +18 -0
- package/src/routes/api/tunnel/enable/route.js +32 -0
- package/src/routes/api/tunnel/status/route.js +23 -0
- package/src/routes/api/update/route.js +42 -0
- package/src/routes/api/usage/[connectionId]/codex-reset-credits/route.js +169 -0
- package/src/routes/api/usage/[connectionId]/route.js +222 -0
- package/src/routes/api/usage/chart/route.js +27 -0
- package/src/routes/api/usage/history/route.js +94 -0
- package/src/routes/api/usage/logs/route.js +14 -0
- package/src/routes/api/usage/providers/route.js +36 -0
- package/src/routes/api/usage/request-details/route.js +61 -0
- package/src/routes/api/usage/request-logs/route.js +15 -0
- package/src/routes/api/usage/stats/route.js +23 -0
- package/src/routes/api/usage/stream/route.js +78 -0
- package/src/routes/api/v1/api/chat/route.js +36 -0
- package/src/routes/api/v1/chat/completions/route.js +28 -0
- package/src/routes/api/v1/images/edits/route.js +70 -0
- package/src/routes/api/v1/images/generations/route.js +50 -0
- package/src/routes/api/v1/images/imageValidation.js +45 -0
- package/src/routes/api/v1/messages/count_tokens/route.js +87 -0
- package/src/routes/api/v1/messages/route.js +26 -0
- package/src/routes/api/v1/models/route.js +440 -0
- package/src/routes/api/v1/responses/compact/route.js +34 -0
- package/src/routes/api/v1/responses/route.js +26 -0
- package/src/routes/api/v1/route.js +1 -0
- package/src/routes/api/version/route.js +11 -0
- package/src/routes/table.js +1141 -0
- package/src/rtk/applyFilter.js +13 -0
- package/src/rtk/autodetect.js +104 -0
- package/src/rtk/caveman.js +6 -0
- package/src/rtk/cavemanPrompts.js +107 -0
- package/src/rtk/constants.js +51 -0
- package/src/rtk/filters/buildOutput.js +120 -0
- package/src/rtk/filters/dedupLog.js +43 -0
- package/src/rtk/filters/find.js +47 -0
- package/src/rtk/filters/gitDiff.js +90 -0
- package/src/rtk/filters/gitLog.js +88 -0
- package/src/rtk/filters/gitStatus.js +101 -0
- package/src/rtk/filters/grep.js +45 -0
- package/src/rtk/filters/ls.js +73 -0
- package/src/rtk/filters/readNumbered.js +22 -0
- package/src/rtk/filters/searchList.js +49 -0
- package/src/rtk/filters/smartTruncate.js +13 -0
- package/src/rtk/filters/tree.js +28 -0
- package/src/rtk/index.js +143 -0
- package/src/rtk/ponytail.js +6 -0
- package/src/rtk/ponytailPrompt.js +49 -0
- package/src/rtk/systemInject.js +89 -0
- package/src/server.ts +474 -0
- package/src/services/accountFallback.js +241 -0
- package/src/services/accountPool.js +389 -0
- package/src/services/capacityAdapter.js +140 -0
- package/src/services/cliModelMapping.js +84 -0
- package/src/services/clinepassModels.js +60 -0
- package/src/services/codebuddyModels.js +250 -0
- package/src/services/combo.js +482 -0
- package/src/services/copilotModels.js +126 -0
- package/src/services/currencyRate.js +56 -0
- package/src/services/cursorModels.js +197 -0
- package/src/services/grokCliModels.js +130 -0
- package/src/services/kimchiModels.js +175 -0
- package/src/services/kiroModels.js +246 -0
- package/src/services/model.js +83 -0
- package/src/services/oauthCredentialManager.js +153 -0
- package/src/services/projectId.js +240 -0
- package/src/services/provider.js +145 -0
- package/src/services/qoderModels.js +274 -0
- package/src/services/tokenRefresh/dedup.js +70 -0
- package/src/services/tokenRefresh/providers.js +626 -0
- package/src/services/tokenRefresh.js +274 -0
- package/src/services/usage/claude.js +134 -0
- package/src/services/usage/codebuddy-cn.js +212 -0
- package/src/services/usage/codex.js +193 -0
- package/src/services/usage/deepseek.js +103 -0
- package/src/services/usage/github.js +94 -0
- package/src/services/usage/google.js +218 -0
- package/src/services/usage/grok-cli.js +368 -0
- package/src/services/usage/grokCliQuotaFrame.js +170 -0
- package/src/services/usage/kimi.js +185 -0
- package/src/services/usage/kiro.js +172 -0
- package/src/services/usage/minimax.js +215 -0
- package/src/services/usage/misc.js +264 -0
- package/src/services/usage/shared.js +59 -0
- package/src/services/usage.js +56 -0
- package/src/shared/clineAuth.js +37 -0
- package/src/shared/constants/config.js +80 -0
- package/src/shared/constants/coworkPlugins.js +69 -0
- package/src/shared/constants/models.js +41 -0
- package/src/shared/constants/providers.js +128 -0
- package/src/shared/constants/providersDisplay.js +1 -0
- package/src/shared/qoder/constants.js +50 -0
- package/src/shared/qoder/cosy.js +134 -0
- package/src/shared/qoder/encoding.js +36 -0
- package/src/shared/services/initializeApp.js +226 -0
- package/src/shared/services/quotaAutoPing.js +310 -0
- package/src/shared/utils/apiKey.js +65 -0
- package/src/shared/utils/clineAuth.js +1 -0
- package/src/shared/utils/index.js +3 -0
- package/src/shared/utils/machineId.js +60 -0
- package/src/shared/utils/ssrfGuard.js +280 -0
- package/src/sse/handlers/chat.js +426 -0
- package/src/sse/oauthLease.js +32 -0
- package/src/sse/recovery.js +235 -0
- package/src/sse/services/auth.js +489 -0
- package/src/sse/services/backgroundTokenRefresh.js +173 -0
- package/src/sse/services/model.js +129 -0
- package/src/sse/services/requestCancellation.js +4 -0
- package/src/sse/services/tokenRefresh.js +264 -0
- package/src/sse/utils/logger.js +142 -0
- package/src/staticServe.ts +95 -0
- package/src/transformer/streamToJsonConverter.js +87 -0
- package/src/translator/canonical.js +81 -0
- package/src/translator/concerns/chunk.js +9 -0
- package/src/translator/concerns/compatFallback.js +147 -0
- package/src/translator/concerns/finishReason.js +59 -0
- package/src/translator/concerns/image.js +105 -0
- package/src/translator/concerns/json.js +4 -0
- package/src/translator/concerns/kiroConversation.js +428 -0
- package/src/translator/concerns/message.js +5 -0
- package/src/translator/concerns/modality.js +134 -0
- package/src/translator/concerns/paramSupport.js +97 -0
- package/src/translator/concerns/prefetch.js +86 -0
- package/src/translator/concerns/reasoning.js +18 -0
- package/src/translator/concerns/thinking.js +40 -0
- package/src/translator/concerns/thinkingUnified.js +319 -0
- package/src/translator/concerns/toolCall.js +134 -0
- package/src/translator/concerns/usage.js +61 -0
- package/src/translator/detection.js +42 -0
- package/src/translator/formats/claude.js +323 -0
- package/src/translator/formats/gemini.js +366 -0
- package/src/translator/formats/maxTokens.js +19 -0
- package/src/translator/formats/openai.js +116 -0
- package/src/translator/formats/responsesApi.js +117 -0
- package/src/translator/formats.js +28 -0
- package/src/translator/index.js +237 -0
- package/src/translator/request/antigravity-to-openai.js +217 -0
- package/src/translator/request/claude-to-kiro.js +288 -0
- package/src/translator/request/claude-to-openai.js +199 -0
- package/src/translator/request/gemini-to-openai.js +142 -0
- package/src/translator/request/openai-responses.js +373 -0
- package/src/translator/request/openai-to-claude.js +260 -0
- package/src/translator/request/openai-to-commandcode.js +161 -0
- package/src/translator/request/openai-to-cursor.js +175 -0
- package/src/translator/request/openai-to-gemini.js +422 -0
- package/src/translator/request/openai-to-kiro.js +360 -0
- package/src/translator/request/openai-to-ollama.js +152 -0
- package/src/translator/request/openai-to-vertex.js +36 -0
- package/src/translator/response/claude-to-openai.js +185 -0
- package/src/translator/response/commandcode-to-openai.js +164 -0
- package/src/translator/response/cursor-to-openai.js +18 -0
- package/src/translator/response/gemini-to-openai.js +127 -0
- package/src/translator/response/kiro-to-claude.js +230 -0
- package/src/translator/response/kiro-to-openai.js +136 -0
- package/src/translator/response/ollama-to-openai.js +106 -0
- package/src/translator/response/openai-responses.js +534 -0
- package/src/translator/response/openai-to-antigravity.js +107 -0
- package/src/translator/response/openai-to-claude.js +231 -0
- package/src/translator/schema/blocks.js +40 -0
- package/src/translator/schema/defaults.js +3 -0
- package/src/translator/schema/finishReasons.js +22 -0
- package/src/translator/schema/index.js +7 -0
- package/src/translator/schema/roles.js +12 -0
- package/src/transport/requestBoundary.ts +223 -0
- package/src/transport/routerAdmission.ts +380 -0
- package/src/types/provider.ts +59 -0
- package/src/utils/bypassHandler.js +272 -0
- package/src/utils/claudeCloaking.js +10 -0
- package/src/utils/claudeSignature.js +35 -0
- package/src/utils/clientDetector.js +183 -0
- package/src/utils/cursorChecksum.js +104 -0
- package/src/utils/cursorProtobuf.js +801 -0
- package/src/utils/debugLog.js +12 -0
- package/src/utils/error.js +120 -0
- package/src/utils/inflight.js +127 -0
- package/src/utils/kiroSessionReplay.js +139 -0
- package/src/utils/ollamaTransform.js +83 -0
- package/src/utils/proxyFetch.js +308 -0
- package/src/utils/reasoningContentInjector.js +71 -0
- package/src/utils/requestLogger.js +272 -0
- package/src/utils/responsesStreamHelpers.js +46 -0
- package/src/utils/sessionManager.js +205 -0
- package/src/utils/sse.js +13 -0
- package/src/utils/sseConstants.js +21 -0
- package/src/utils/stream.js +511 -0
- package/src/utils/streamHandler.js +256 -0
- package/src/utils/streamHelpers.js +113 -0
- package/src/utils/toolDeduper.js +44 -0
- package/src/utils/usageTracking.js +329 -0
- package/tsconfig.json +45 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { FORMATS } from "../formats.js";
|
|
2
|
+
import { fetchImageAsBase64, parseDataUri } from "./image.js";
|
|
3
|
+
|
|
4
|
+
const TARGETS_NEED_BASE64 = new Set([
|
|
5
|
+
FORMATS.GEMINI, FORMATS.GEMINI_CLI, FORMATS.VERTEX,
|
|
6
|
+
FORMATS.ANTIGRAVITY, FORMATS.OLLAMA, FORMATS.KIRO,
|
|
7
|
+
]);
|
|
8
|
+
|
|
9
|
+
function isRemoteUrl(url) {
|
|
10
|
+
return typeof url === "string" && (url.startsWith("http://") || url.startsWith("https://"));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function collectImageRefs(body, sourceFormat) {
|
|
14
|
+
const refs = [];
|
|
15
|
+
const pushOpenAI = (messages) => {
|
|
16
|
+
for (const msg of messages || []) {
|
|
17
|
+
if (!Array.isArray(msg.content)) continue;
|
|
18
|
+
for (const block of msg.content) {
|
|
19
|
+
if (block?.type === "image_url") {
|
|
20
|
+
const url = typeof block.image_url === "string" ? block.image_url : block.image_url?.url;
|
|
21
|
+
if (isRemoteUrl(url)) refs.push({ get: () => url, set: (v) => {
|
|
22
|
+
if (typeof block.image_url === "string") block.image_url = v; else block.image_url.url = v;
|
|
23
|
+
} });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const pushGemini = (contents) => {
|
|
29
|
+
for (const c of contents || []) {
|
|
30
|
+
for (const p of c.parts || []) {
|
|
31
|
+
const uri = p?.fileData?.fileUri;
|
|
32
|
+
if (isRemoteUrl(uri)) refs.push({ get: () => uri, part: p });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
switch (sourceFormat) {
|
|
38
|
+
case FORMATS.OPENAI:
|
|
39
|
+
case FORMATS.OLLAMA:
|
|
40
|
+
case FORMATS.KIRO:
|
|
41
|
+
case FORMATS.CURSOR:
|
|
42
|
+
case FORMATS.COMMANDCODE:
|
|
43
|
+
pushOpenAI(body.messages);
|
|
44
|
+
break;
|
|
45
|
+
case FORMATS.CLAUDE:
|
|
46
|
+
for (const msg of body.messages || []) {
|
|
47
|
+
if (!Array.isArray(msg.content)) continue;
|
|
48
|
+
for (const block of msg.content) {
|
|
49
|
+
if (block?.type === "image" && block.source?.type === "url" && isRemoteUrl(block.source.url)) {
|
|
50
|
+
refs.push({ get: () => block.source.url, claudeBlock: block });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
break;
|
|
55
|
+
case FORMATS.GEMINI:
|
|
56
|
+
case FORMATS.GEMINI_CLI:
|
|
57
|
+
case FORMATS.VERTEX:
|
|
58
|
+
pushGemini(body.contents);
|
|
59
|
+
break;
|
|
60
|
+
case FORMATS.ANTIGRAVITY:
|
|
61
|
+
pushGemini(body?.request?.contents);
|
|
62
|
+
break;
|
|
63
|
+
default:
|
|
64
|
+
pushOpenAI(body.messages);
|
|
65
|
+
}
|
|
66
|
+
return refs;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function prefetchRemoteImages(body, sourceFormat, targetFormat, options = {}) {
|
|
70
|
+
if (!body || !TARGETS_NEED_BASE64.has(targetFormat)) return 0;
|
|
71
|
+
const refs = collectImageRefs(body, sourceFormat);
|
|
72
|
+
if (!refs.length) return 0;
|
|
73
|
+
|
|
74
|
+
let converted = 0;
|
|
75
|
+
for (const ref of refs) {
|
|
76
|
+
const url = ref.get();
|
|
77
|
+
if (parseDataUri(url)) continue;
|
|
78
|
+
const fetched = await fetchImageAsBase64(url, options);
|
|
79
|
+
if (!fetched) continue;
|
|
80
|
+
if (ref.set) ref.set(fetched.url);
|
|
81
|
+
else if (ref.part) { delete ref.part.fileData; ref.part.inlineData = { mimeType: fetched.mimeType, data: fetched.url.split(",")[1] }; }
|
|
82
|
+
else if (ref.claudeBlock) ref.claudeBlock.source = { type: "base64", media_type: fetched.mimeType, data: fetched.url.split(",")[1] };
|
|
83
|
+
converted++;
|
|
84
|
+
}
|
|
85
|
+
return converted;
|
|
86
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ROLE } from "../schema/index.js";
|
|
2
|
+
|
|
3
|
+
export function reasoningDelta(text, withRole = false) {
|
|
4
|
+
return withRole
|
|
5
|
+
? { role: ROLE.ASSISTANT, reasoning_content: text }
|
|
6
|
+
: { reasoning_content: text };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function extractReasoningText(delta) {
|
|
10
|
+
if (!delta || typeof delta !== "object") return "";
|
|
11
|
+
if (typeof delta.reasoning_content === "string" && delta.reasoning_content) return delta.reasoning_content;
|
|
12
|
+
if (typeof delta.reasoning === "string" && delta.reasoning) return delta.reasoning;
|
|
13
|
+
const details = delta.reasoning_details;
|
|
14
|
+
if (Array.isArray(details)) {
|
|
15
|
+
return details.map((d) => (typeof d === "string" ? d : d?.text || d?.content || "")).join("");
|
|
16
|
+
}
|
|
17
|
+
return "";
|
|
18
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export const EFFORT_LEVELS = ["minimal", "low", "medium", "high", "xhigh", "max"];
|
|
2
|
+
|
|
3
|
+
export const LEVEL_TO_BUDGET = {
|
|
4
|
+
none: 0,
|
|
5
|
+
minimal: 512,
|
|
6
|
+
low: 1024,
|
|
7
|
+
medium: 8192,
|
|
8
|
+
high: 24576,
|
|
9
|
+
xhigh: 32768,
|
|
10
|
+
max: 128000,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function effortToBudget(effort) {
|
|
14
|
+
if (!effort) return undefined;
|
|
15
|
+
return LEVEL_TO_BUDGET[String(effort).toLowerCase()];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function effortToThinkingLevel(effort) {
|
|
19
|
+
const e = String(effort).toLowerCase().trim();
|
|
20
|
+
if (e === "none" || e === "off") return "minimal";
|
|
21
|
+
if (e === "xhigh" || e === "max") return "high";
|
|
22
|
+
return e;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function budgetToLevel(budget) {
|
|
26
|
+
const b = Number(budget);
|
|
27
|
+
if (!b || b <= 0) return null;
|
|
28
|
+
if (b <= 768) return "minimal";
|
|
29
|
+
if (b <= 4096) return "low";
|
|
30
|
+
if (b <= 16384) return "medium";
|
|
31
|
+
if (b <= 28672) return "high";
|
|
32
|
+
return "xhigh";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function budgetToEffort(budget) {
|
|
36
|
+
if (!budget || budget <= 0) return null;
|
|
37
|
+
if (budget <= 2048) return "low";
|
|
38
|
+
if (budget <= 16384) return "medium";
|
|
39
|
+
return "high";
|
|
40
|
+
}
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { getCapabilitiesForModel } from "../../providers/capabilities.js";
|
|
2
|
+
import { getThinkingLevels } from "../../providers/thinkingLevels.js";
|
|
3
|
+
import { PROVIDERS } from "../../providers/index.js";
|
|
4
|
+
import { LEVEL_TO_BUDGET, budgetToLevel, effortToBudget, effortToThinkingLevel } from "./thinking.js";
|
|
5
|
+
|
|
6
|
+
const FORMAT_TO_NATIVE = {
|
|
7
|
+
openai: "openai",
|
|
8
|
+
"openai-responses": "openai",
|
|
9
|
+
"openai-response": "openai",
|
|
10
|
+
codex: "openai",
|
|
11
|
+
claude: "claude-budget",
|
|
12
|
+
gemini: "gemini-budget",
|
|
13
|
+
"gemini-cli": "gemini-budget",
|
|
14
|
+
vertex: "gemini-budget",
|
|
15
|
+
antigravity: "gemini-budget",
|
|
16
|
+
kiro: "kiro",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function stripThinkingSuffix(model) {
|
|
20
|
+
if (typeof model !== "string") return model;
|
|
21
|
+
const m = model.match(/^(.*)\([^()]+\)\s*$/);
|
|
22
|
+
return m ? m[1].trim() : model;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function parseSuffix(model) {
|
|
26
|
+
if (typeof model !== "string") return { cleanModel: model, override: null };
|
|
27
|
+
const m = model.match(/^(.*)\(([^()]+)\)\s*$/);
|
|
28
|
+
if (!m) return { cleanModel: model, override: null };
|
|
29
|
+
const cleanModel = m[1].trim();
|
|
30
|
+
const raw = m[2].trim().toLowerCase();
|
|
31
|
+
if (raw === "none" || raw === "off") return { cleanModel, override: { mode: "none" } };
|
|
32
|
+
if (raw === "auto") return { cleanModel, override: { mode: "auto" } };
|
|
33
|
+
if (raw === "ultra") return { cleanModel, override: { mode: "level", level: raw } };
|
|
34
|
+
if (/^\d+$/.test(raw)) return { cleanModel, override: { mode: "budget", budget: Number(raw) } };
|
|
35
|
+
if (LEVEL_TO_BUDGET[raw] !== undefined) return { cleanModel, override: { mode: "level", level: raw } };
|
|
36
|
+
return { cleanModel, override: null };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function extractThinking(body) {
|
|
40
|
+
if (!body || typeof body !== "object") return null;
|
|
41
|
+
|
|
42
|
+
const oc = body.output_config?.effort;
|
|
43
|
+
if (typeof oc === "string" && oc) {
|
|
44
|
+
const e = oc.toLowerCase();
|
|
45
|
+
if (e === "none" || e === "off") return { mode: "none" };
|
|
46
|
+
if (e === "auto") return { mode: "auto" };
|
|
47
|
+
return { mode: "level", level: e };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const t = body.thinking;
|
|
51
|
+
if (t && typeof t === "object") {
|
|
52
|
+
if (t.type === "disabled") return { mode: "none" };
|
|
53
|
+
if (t.type === "adaptive" || t.type === "enabled") {
|
|
54
|
+
const budget = Number(t.budget_tokens);
|
|
55
|
+
if (Number.isFinite(budget) && budget > 0) return { mode: "budget", budget };
|
|
56
|
+
return { mode: "auto" };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const effort = body.reasoning_effort ?? (typeof body.reasoning === "object" ? body.reasoning?.effort : null);
|
|
61
|
+
if (typeof effort === "string" && effort) {
|
|
62
|
+
const e = effort.toLowerCase();
|
|
63
|
+
if (e === "none" || e === "off") return { mode: "none" };
|
|
64
|
+
if (e === "auto") return { mode: "auto" };
|
|
65
|
+
return { mode: "level", level: e };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const tc = body.thinkingConfig || body.generationConfig?.thinkingConfig || body.request?.generationConfig?.thinkingConfig;
|
|
69
|
+
if (tc && typeof tc === "object") {
|
|
70
|
+
if (typeof tc.thinkingLevel === "string") return { mode: "level", level: tc.thinkingLevel.toLowerCase() };
|
|
71
|
+
const tb = Number(tc.thinkingBudget);
|
|
72
|
+
if (Number.isFinite(tb)) {
|
|
73
|
+
if (tb === 0) return { mode: "none" };
|
|
74
|
+
if (tb < 0) return { mode: "auto" };
|
|
75
|
+
return { mode: "budget", budget: tb };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (body.enable_thinking === false) return { mode: "none" };
|
|
80
|
+
if (body.enable_thinking === true) {
|
|
81
|
+
const tb = Number(body.thinking_budget);
|
|
82
|
+
if (Number.isFinite(tb) && tb > 0) return { mode: "budget", budget: tb };
|
|
83
|
+
return { mode: "auto" };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const captureThinking = extractThinking;
|
|
90
|
+
|
|
91
|
+
function resolveFormat(targetFormat, model, provider, capabilityOverride = null) {
|
|
92
|
+
if (typeof capabilityOverride?.thinkingFormat === "string" && capabilityOverride.thinkingFormat) {
|
|
93
|
+
return capabilityOverride.thinkingFormat;
|
|
94
|
+
}
|
|
95
|
+
const providerFmt = provider ? PROVIDERS[provider]?.thinkingFormat : null;
|
|
96
|
+
if (providerFmt) return providerFmt;
|
|
97
|
+
const caps = getCapabilitiesForModel(provider, model, capabilityOverride);
|
|
98
|
+
if (caps.thinkingFormat) return caps.thinkingFormat;
|
|
99
|
+
return FORMAT_TO_NATIVE[targetFormat] || "openai";
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function toBudget(cfg, range) {
|
|
103
|
+
let budget;
|
|
104
|
+
if (cfg.mode === "budget") budget = cfg.budget;
|
|
105
|
+
else if (cfg.mode === "level") budget = effortToBudget(cfg.level);
|
|
106
|
+
else if (cfg.mode === "auto") return -1;
|
|
107
|
+
if (!Number.isFinite(budget)) return undefined;
|
|
108
|
+
if (range) {
|
|
109
|
+
if (range.min != null && budget < range.min) budget = range.min;
|
|
110
|
+
if (range.max != null && budget > range.max) budget = range.max;
|
|
111
|
+
}
|
|
112
|
+
return budget;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function toLevel(cfg) {
|
|
116
|
+
if (cfg.mode === "level") return cfg.level;
|
|
117
|
+
if (cfg.mode === "budget") return budgetToLevel(cfg.budget) || "medium";
|
|
118
|
+
if (cfg.mode === "auto") return "auto";
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function normalizeOpenAILevel(level, supportedLevels) {
|
|
123
|
+
if (level !== "max" && level !== "ultra") return level;
|
|
124
|
+
if (supportedLevels?.includes(level)) return level;
|
|
125
|
+
if (level === "ultra" && supportedLevels?.includes("max")) return "max";
|
|
126
|
+
return "xhigh";
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function toGeminiThinkingLevel(cfg) {
|
|
130
|
+
const raw = cfg.mode === "auto" ? "high" : (toLevel(cfg) || "high");
|
|
131
|
+
return effortToThinkingLevel(raw);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function toKimiReasoningEffort(cfg) {
|
|
135
|
+
const level = toLevel(cfg);
|
|
136
|
+
if (level === "auto") return "high";
|
|
137
|
+
if (level === "minimal") return "low";
|
|
138
|
+
if (level === "xhigh") return "max";
|
|
139
|
+
if (["low", "medium", "high", "max"].includes(level)) return level;
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const GEMINI_LEVEL_OUTPUT_FLOOR = {
|
|
144
|
+
minimal: 4096,
|
|
145
|
+
low: 8192,
|
|
146
|
+
medium: 16384,
|
|
147
|
+
high: 65535,
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
function geminiBudgetOutputFloor(budget) {
|
|
151
|
+
if (budget === -1) return 32768;
|
|
152
|
+
if (!Number.isFinite(budget)) return 32768;
|
|
153
|
+
if (budget <= 1024) return 8192;
|
|
154
|
+
if (budget <= 8192) return 16384;
|
|
155
|
+
if (budget <= 24576) return 32768;
|
|
156
|
+
return 65535;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function geminiLevelOutputFloor(level) {
|
|
160
|
+
return GEMINI_LEVEL_OUTPUT_FLOOR[level] || GEMINI_LEVEL_OUTPUT_FLOOR.high;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function getGeminiGenerationConfig(body) {
|
|
164
|
+
if (body.request && typeof body.request === "object") {
|
|
165
|
+
if (!body.request.generationConfig || typeof body.request.generationConfig !== "object") {
|
|
166
|
+
body.request.generationConfig = {};
|
|
167
|
+
}
|
|
168
|
+
return body.request.generationConfig;
|
|
169
|
+
}
|
|
170
|
+
if (!body.generationConfig || typeof body.generationConfig !== "object") {
|
|
171
|
+
body.generationConfig = {};
|
|
172
|
+
}
|
|
173
|
+
return body.generationConfig;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function setGeminiThinking(body, tc) {
|
|
177
|
+
const gc = getGeminiGenerationConfig(body);
|
|
178
|
+
gc.thinkingConfig = tc;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function ensureGeminiOutputFloor(body, floor, caps) {
|
|
182
|
+
const cap = Number.isFinite(caps?.maxOutput) ? caps.maxOutput : floor;
|
|
183
|
+
const target = Math.min(floor, cap);
|
|
184
|
+
const gc = getGeminiGenerationConfig(body);
|
|
185
|
+
const current = Number(gc.maxOutputTokens);
|
|
186
|
+
if (!Number.isFinite(current) || current < target) {
|
|
187
|
+
gc.maxOutputTokens = target;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function stripAll(body) {
|
|
192
|
+
delete body.thinking;
|
|
193
|
+
delete body.reasoning_effort;
|
|
194
|
+
delete body.reasoning;
|
|
195
|
+
delete body.thinkingConfig;
|
|
196
|
+
delete body.enable_thinking;
|
|
197
|
+
delete body.thinking_budget;
|
|
198
|
+
delete body.output_config;
|
|
199
|
+
if (body.generationConfig) delete body.generationConfig.thinkingConfig;
|
|
200
|
+
if (body.request?.generationConfig) delete body.request.generationConfig.thinkingConfig;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function applyFormat(fmt, body, cfg, caps, supportedLevels) {
|
|
204
|
+
const none = cfg.mode === "none";
|
|
205
|
+
const canDisable = caps.thinkingCanDisable !== false;
|
|
206
|
+
|
|
207
|
+
const eff = none && !canDisable ? { mode: "level", level: "minimal" } : cfg;
|
|
208
|
+
|
|
209
|
+
switch (fmt) {
|
|
210
|
+
case "openai": {
|
|
211
|
+
if (none && canDisable) { body.reasoning_effort = "none"; break; }
|
|
212
|
+
if (cfg.mode === "auto") break;
|
|
213
|
+
const level = toLevel(eff);
|
|
214
|
+
if (level) body.reasoning_effort = normalizeOpenAILevel(level, supportedLevels);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case "claude-adaptive": {
|
|
218
|
+
if (none && canDisable) { body.thinking = { type: "disabled" }; break; }
|
|
219
|
+
|
|
220
|
+
body.thinking = { type: "adaptive" };
|
|
221
|
+
const level = toLevel(eff);
|
|
222
|
+
if (level && level !== "auto") {
|
|
223
|
+
body.output_config = { effort: level === "xhigh" ? "high" : level };
|
|
224
|
+
}
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
case "claude-budget": {
|
|
228
|
+
if (none && canDisable) { body.thinking = { type: "disabled" }; break; }
|
|
229
|
+
if (cfg.mode === "auto") break;
|
|
230
|
+
const budget = toBudget(eff, caps.thinkingRange);
|
|
231
|
+
body.thinking = budget === -1 ? { type: "enabled" } : { type: "enabled", budget_tokens: budget || 8192 };
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
case "gemini-level": {
|
|
235
|
+
const level = none ? "minimal" : toGeminiThinkingLevel(eff);
|
|
236
|
+
setGeminiThinking(body, { thinkingLevel: level, includeThoughts: level !== "minimal" });
|
|
237
|
+
ensureGeminiOutputFloor(body, geminiLevelOutputFloor(level), caps);
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
case "gemini-budget": {
|
|
241
|
+
if (none && canDisable) { setGeminiThinking(body, { thinkingBudget: 0, includeThoughts: false }); break; }
|
|
242
|
+
const budget = toBudget(eff, caps.thinkingRange);
|
|
243
|
+
setGeminiThinking(body, { thinkingBudget: budget ?? -1, includeThoughts: true });
|
|
244
|
+
ensureGeminiOutputFloor(body, geminiBudgetOutputFloor(budget ?? -1), caps);
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
case "zai": {
|
|
248
|
+
|
|
249
|
+
if (none && canDisable) { body.enable_thinking = false; delete body.thinking; break; }
|
|
250
|
+
body.thinking = { type: "enabled" };
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
case "qwen": {
|
|
254
|
+
if (none && canDisable) { body.enable_thinking = false; break; }
|
|
255
|
+
body.enable_thinking = true;
|
|
256
|
+
const budget = toBudget(eff, caps.thinkingRange);
|
|
257
|
+
if (Number.isFinite(budget) && budget > 0) body.thinking_budget = budget;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
case "deepseek": {
|
|
261
|
+
if (none && canDisable) { body.thinking = { type: "disabled" }; break; }
|
|
262
|
+
body.thinking = { type: "enabled" };
|
|
263
|
+
|
|
264
|
+
const level = toLevel(eff);
|
|
265
|
+
if (level === "max" || level === "xhigh") body.reasoning_effort = "max";
|
|
266
|
+
else if (level === "low") body.reasoning_effort = "low";
|
|
267
|
+
else body.reasoning_effort = "high";
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
case "kimi": {
|
|
271
|
+
if (none && canDisable) { body.thinking = { type: "disabled" }; break; }
|
|
272
|
+
const effort = toKimiReasoningEffort(eff);
|
|
273
|
+
if (effort) body.reasoning_effort = effort;
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case "minimax": {
|
|
277
|
+
|
|
278
|
+
body.thinking = { type: none && canDisable ? "disabled" : "adaptive" };
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
case "hunyuan": {
|
|
282
|
+
if (none && canDisable) { body.thinking = { type: "disabled" }; break; }
|
|
283
|
+
const budget = toBudget(eff, caps.thinkingRange);
|
|
284
|
+
body.thinking = budget === -1 ? { type: "enabled" } : { type: "enabled", budget_tokens: budget || 8192 };
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
case "step": {
|
|
288
|
+
if (none && canDisable) break;
|
|
289
|
+
const level = toLevel(eff);
|
|
290
|
+
if (level) body.reasoning_effort = level === "xhigh" || level === "max" ? "high" : level;
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
case "kiro":
|
|
294
|
+
|
|
295
|
+
break;
|
|
296
|
+
default:
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function applyThinking(targetFormat, model, body, provider = null, intent = undefined, capabilityOverride = null) {
|
|
302
|
+
if (!body || typeof body !== "object") return body;
|
|
303
|
+
|
|
304
|
+
const { cleanModel, override } = parseSuffix(model);
|
|
305
|
+
const cfg = override || intent || extractThinking(body);
|
|
306
|
+
const caps = getCapabilitiesForModel(provider, cleanModel, capabilityOverride);
|
|
307
|
+
|
|
308
|
+
if (!caps.reasoning) {
|
|
309
|
+
stripAll(body);
|
|
310
|
+
return body;
|
|
311
|
+
}
|
|
312
|
+
if (!cfg) return body;
|
|
313
|
+
|
|
314
|
+
const fmt = resolveFormat(targetFormat, cleanModel, provider, capabilityOverride);
|
|
315
|
+
const supportedLevels = getThinkingLevels(provider, cleanModel, capabilityOverride);
|
|
316
|
+
stripAll(body);
|
|
317
|
+
applyFormat(fmt, body, cfg, caps, supportedLevels);
|
|
318
|
+
return body;
|
|
319
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
const TOOL_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
|
|
2
|
+
|
|
3
|
+
export function fallbackToolCallId(index) {
|
|
4
|
+
return index === undefined ? `call_${Date.now()}` : `call_${index}_${Date.now()}`;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function generateToolCallId(msgIndex = 0, tcIndex = 0, toolName = "") {
|
|
8
|
+
const name = toolName ? `_${toolName.replace(/[^a-zA-Z0-9_-]/g, "")}` : "";
|
|
9
|
+
return `call_msg${msgIndex}_tc${tcIndex}${name}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function sanitizeToolId(id) {
|
|
13
|
+
if (!id || typeof id !== "string") return null;
|
|
14
|
+
const sanitized = id.replace(/[^a-zA-Z0-9_-]/g, "");
|
|
15
|
+
return sanitized.length > 0 ? sanitized : null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function ensureToolCallIds(body) {
|
|
19
|
+
if (!body.messages || !Array.isArray(body.messages)) return body;
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < body.messages.length; i++) {
|
|
22
|
+
const msg = body.messages[i];
|
|
23
|
+
if (msg.role === "assistant" && msg.tool_calls && Array.isArray(msg.tool_calls)) {
|
|
24
|
+
for (let j = 0; j < msg.tool_calls.length; j++) {
|
|
25
|
+
const tc = msg.tool_calls[j];
|
|
26
|
+
|
|
27
|
+
if (!tc.id || !TOOL_ID_PATTERN.test(tc.id)) {
|
|
28
|
+
const sanitized = sanitizeToolId(tc.id);
|
|
29
|
+
tc.id = sanitized || generateToolCallId(i, j, tc.function?.name);
|
|
30
|
+
}
|
|
31
|
+
if (!tc.type) {
|
|
32
|
+
tc.type = "function";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (tc.function?.arguments && typeof tc.function.arguments !== "string") {
|
|
36
|
+
tc.function.arguments = JSON.stringify(tc.function.arguments);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (msg.role === "tool" && msg.tool_call_id && !TOOL_ID_PATTERN.test(msg.tool_call_id)) {
|
|
42
|
+
const sanitized = sanitizeToolId(msg.tool_call_id);
|
|
43
|
+
msg.tool_call_id = sanitized || generateToolCallId(i, 0);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (Array.isArray(msg.content)) {
|
|
47
|
+
for (let k = 0; k < msg.content.length; k++) {
|
|
48
|
+
const block = msg.content[k];
|
|
49
|
+
if (block.type === "tool_use" && block.id && !TOOL_ID_PATTERN.test(block.id)) {
|
|
50
|
+
const sanitized = sanitizeToolId(block.id);
|
|
51
|
+
block.id = sanitized || generateToolCallId(i, k, block.name);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (block.type === "tool_result" && block.tool_use_id && !TOOL_ID_PATTERN.test(block.tool_use_id)) {
|
|
55
|
+
const sanitized = sanitizeToolId(block.tool_use_id);
|
|
56
|
+
block.tool_use_id = sanitized || generateToolCallId(i, k);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return body;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function getToolCallIds(msg) {
|
|
66
|
+
if (msg.role !== "assistant") return [];
|
|
67
|
+
|
|
68
|
+
const ids = [];
|
|
69
|
+
|
|
70
|
+
if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
|
|
71
|
+
for (const tc of msg.tool_calls) {
|
|
72
|
+
if (tc.id) ids.push(tc.id);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (Array.isArray(msg.content)) {
|
|
77
|
+
for (const block of msg.content) {
|
|
78
|
+
if (block.type === "tool_use" && block.id) {
|
|
79
|
+
ids.push(block.id);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return ids;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function hasToolResults(msg, toolCallIds) {
|
|
88
|
+
if (!msg || !toolCallIds.length) return false;
|
|
89
|
+
|
|
90
|
+
if (msg.role === "tool" && msg.tool_call_id) {
|
|
91
|
+
return toolCallIds.includes(msg.tool_call_id);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (msg.role === "user" && Array.isArray(msg.content)) {
|
|
95
|
+
for (const block of msg.content) {
|
|
96
|
+
if (block.type === "tool_result" && toolCallIds.includes(block.tool_use_id)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function fixMissingToolResponses(body) {
|
|
106
|
+
if (!body.messages || !Array.isArray(body.messages)) return body;
|
|
107
|
+
|
|
108
|
+
const newMessages = [];
|
|
109
|
+
|
|
110
|
+
for (let i = 0; i < body.messages.length; i++) {
|
|
111
|
+
const msg = body.messages[i];
|
|
112
|
+
const nextMsg = body.messages[i + 1];
|
|
113
|
+
|
|
114
|
+
newMessages.push(msg);
|
|
115
|
+
|
|
116
|
+
const toolCallIds = getToolCallIds(msg);
|
|
117
|
+
if (toolCallIds.length === 0) continue;
|
|
118
|
+
|
|
119
|
+
if (nextMsg && !hasToolResults(nextMsg, toolCallIds)) {
|
|
120
|
+
|
|
121
|
+
for (const id of toolCallIds) {
|
|
122
|
+
|
|
123
|
+
newMessages.push({
|
|
124
|
+
role: "tool",
|
|
125
|
+
tool_call_id: id,
|
|
126
|
+
content: ""
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
body.messages = newMessages;
|
|
133
|
+
return body;
|
|
134
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export function buildUsage({ promptTokens, completionTokens, totalTokens, cachedTokens = 0, cacheCreationTokens = 0, reasoningTokens = 0 }) {
|
|
2
|
+
const usage = { prompt_tokens: promptTokens, completion_tokens: completionTokens, total_tokens: totalTokens };
|
|
3
|
+
if (cachedTokens > 0 || cacheCreationTokens > 0) {
|
|
4
|
+
usage.prompt_tokens_details = {};
|
|
5
|
+
if (cachedTokens > 0) usage.prompt_tokens_details.cached_tokens = cachedTokens;
|
|
6
|
+
if (cacheCreationTokens > 0) usage.prompt_tokens_details.cache_creation_tokens = cacheCreationTokens;
|
|
7
|
+
}
|
|
8
|
+
if (reasoningTokens > 0) {
|
|
9
|
+
usage.completion_tokens_details = { reasoning_tokens: reasoningTokens };
|
|
10
|
+
}
|
|
11
|
+
return usage;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const n = (v) => (typeof v === "number" ? v : 0);
|
|
15
|
+
|
|
16
|
+
const USAGE_EXTRACTORS = {
|
|
17
|
+
claude(raw) {
|
|
18
|
+
const input = n(raw.input_tokens), output = n(raw.output_tokens);
|
|
19
|
+
const cacheRead = n(raw.cache_read_input_tokens), cacheCreate = n(raw.cache_creation_input_tokens);
|
|
20
|
+
const prompt = input + cacheRead + cacheCreate;
|
|
21
|
+
return { promptTokens: prompt, completionTokens: output, totalTokens: prompt + output, cachedTokens: cacheRead, cacheCreationTokens: cacheCreate };
|
|
22
|
+
},
|
|
23
|
+
gemini(raw) {
|
|
24
|
+
const cached = n(raw.cachedContentTokenCount);
|
|
25
|
+
const prompt = n(raw.promptTokenCount);
|
|
26
|
+
const thoughts = n(raw.thoughtsTokenCount);
|
|
27
|
+
const total = n(raw.totalTokenCount);
|
|
28
|
+
let candidates = n(raw.candidatesTokenCount);
|
|
29
|
+
|
|
30
|
+
if (candidates === 0 && total > 0) {
|
|
31
|
+
candidates = total - prompt - thoughts;
|
|
32
|
+
if (candidates < 0) candidates = 0;
|
|
33
|
+
}
|
|
34
|
+
return { promptTokens: prompt, completionTokens: candidates + thoughts, totalTokens: total, cachedTokens: cached, reasoningTokens: thoughts };
|
|
35
|
+
},
|
|
36
|
+
kiro(raw) {
|
|
37
|
+
const input = n(raw.inputTokens), output = n(raw.outputTokens);
|
|
38
|
+
|
|
39
|
+
const cached = n(raw.cache_read_input_tokens) || n(raw.cachedTokens) || n(raw.cached_tokens);
|
|
40
|
+
const cacheCreation = n(raw.cache_creation_input_tokens);
|
|
41
|
+
const out = { promptTokens: input, completionTokens: output, totalTokens: input + output };
|
|
42
|
+
if (cached > 0) out.cachedTokens = cached;
|
|
43
|
+
if (cacheCreation > 0) out.cacheCreationTokens = cacheCreation;
|
|
44
|
+
return out;
|
|
45
|
+
},
|
|
46
|
+
ollama(raw) {
|
|
47
|
+
const input = n(raw.prompt_eval_count), output = n(raw.eval_count);
|
|
48
|
+
return { promptTokens: input, completionTokens: output, totalTokens: input + output };
|
|
49
|
+
},
|
|
50
|
+
commandcode(raw) {
|
|
51
|
+
const input = n(raw.inputTokens), output = n(raw.outputTokens);
|
|
52
|
+
const total = typeof raw.totalTokens === "number" ? raw.totalTokens : input + output;
|
|
53
|
+
return { promptTokens: input, completionTokens: output, totalTokens: total };
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export function toOpenAIUsage(raw, kind) {
|
|
58
|
+
const extract = USAGE_EXTRACTORS[kind];
|
|
59
|
+
if (!extract || !raw || typeof raw !== "object") return null;
|
|
60
|
+
return buildUsage(extract(raw));
|
|
61
|
+
}
|