opencode-quota-gui 1.2.0 → 1.3.1
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/README.md +104 -34
- package/dist/contracts/analytics-entity.d.ts +6 -1
- package/dist/contracts/analytics-entity.js +27 -10
- package/dist/contracts/build-identity.js +1 -1
- package/dist/contracts/card-visibility.d.ts +21 -0
- package/dist/contracts/card-visibility.js +19 -0
- package/dist/contracts/dashboard.d.ts +35 -1
- package/dist/contracts/dashboard.js +2 -0
- package/dist/contracts/dto-guards.d.ts +24 -0
- package/dist/contracts/dto-guards.js +594 -0
- package/dist/contracts/openai-quota.d.ts +3 -0
- package/dist/contracts/openai-quota.js +3 -0
- package/dist/contracts/provider-failure.d.ts +18 -0
- package/dist/contracts/provider-failure.js +25 -0
- package/dist/data/modelsdev-temporal.min.json +57 -0
- package/dist/gui/config.d.ts +20 -3
- package/dist/gui/config.js +72 -4
- package/dist/gui/dashboard-errors.d.ts +32 -0
- package/dist/gui/dashboard-errors.js +97 -0
- package/dist/gui/lifecycle.d.ts +47 -12
- package/dist/gui/lifecycle.js +215 -37
- package/dist/gui/primary-quota-windows.d.ts +28 -0
- package/dist/gui/primary-quota-windows.js +149 -0
- package/dist/gui/provider-cards.d.ts +11 -3
- package/dist/gui/provider-cards.js +85 -33
- package/dist/gui/quota-notification-detector.d.ts +151 -0
- package/dist/gui/quota-notification-detector.js +318 -0
- package/dist/gui/quota-notification-observer.d.ts +54 -0
- package/dist/gui/quota-notification-observer.js +412 -0
- package/dist/gui/rpc.d.ts +61 -0
- package/dist/gui/rpc.js +310 -0
- package/dist/gui/server.d.ts +15 -27
- package/dist/gui/server.js +34 -123
- package/dist/gui/snapshot-service.d.ts +34 -5
- package/dist/gui/snapshot-service.js +301 -70
- package/dist/gui/sources.d.ts +1 -1
- package/dist/gui/sources.js +98 -56
- package/dist/gui-experimental.cjs +1761 -0
- package/dist/lib/accounting-format.js +8 -0
- package/dist/lib/alibaba-coding-plan-cli.d.ts +79 -0
- package/dist/lib/alibaba-coding-plan-cli.js +539 -0
- package/dist/lib/anthropic-auth.d.ts +2 -3
- package/dist/lib/anthropic-auth.js +2 -3
- package/dist/lib/anthropic.d.ts +53 -12
- package/dist/lib/anthropic.js +622 -270
- package/dist/lib/api-key-retry-after.d.ts +7 -0
- package/dist/lib/api-key-retry-after.js +62 -0
- package/dist/lib/atomic-json.d.ts +2 -0
- package/dist/lib/atomic-json.js +1 -0
- package/dist/lib/cache-observer-ingestion.d.ts +90 -0
- package/dist/lib/cache-observer-ingestion.js +981 -0
- package/dist/lib/cache-observer.d.ts +138 -0
- package/dist/lib/cache-observer.js +317 -0
- package/dist/lib/canonical-config-lock.d.ts +9 -0
- package/dist/lib/canonical-config-lock.js +36 -0
- package/dist/lib/chutes.js +63 -35
- package/dist/lib/claude-agent-transcript-ttl.d.ts +48 -0
- package/dist/lib/claude-agent-transcript-ttl.js +154 -0
- package/dist/lib/claude-agent-ttl-tracker.d.ts +21 -0
- package/dist/lib/claude-agent-ttl-tracker.js +152 -0
- package/dist/lib/cli-show.js +5 -3
- package/dist/lib/config-write-target.d.ts +1 -1
- package/dist/lib/config-write-target.js +5 -2
- package/dist/lib/config.d.ts +6 -1
- package/dist/lib/config.js +241 -2
- package/dist/lib/copilot.js +198 -85
- package/dist/lib/dashboard-web-policy.d.ts +35 -0
- package/dist/lib/dashboard-web-policy.js +24 -0
- package/dist/lib/deepseek.js +34 -11
- package/dist/lib/display-sanitize.js +3 -0
- package/dist/lib/entries.d.ts +26 -0
- package/dist/lib/entries.js +2 -0
- package/dist/lib/format-utils.d.ts +1 -1
- package/dist/lib/format-utils.js +2 -2
- package/dist/lib/glm-coding-plan.js +102 -68
- package/dist/lib/google-agy.js +147 -24
- package/dist/lib/google-gemini-cli.js +140 -23
- package/dist/lib/http.d.ts +4 -0
- package/dist/lib/http.js +16 -2
- package/dist/lib/init-installer.js +30 -7
- package/dist/lib/kilo.js +63 -55
- package/dist/lib/kimi.js +43 -14
- package/dist/lib/mimo.d.ts +4 -0
- package/dist/lib/mimo.js +45 -1
- package/dist/lib/nanogpt.d.ts +3 -0
- package/dist/lib/nanogpt.js +50 -6
- package/dist/lib/ollama-cloud.d.ts +1 -1
- package/dist/lib/ollama-cloud.js +51 -29
- package/dist/lib/openai.js +90 -40
- package/dist/lib/opencode-auth-sqlite.d.ts +2 -2
- package/dist/lib/opencode-auth-sqlite.js +2 -2
- package/dist/lib/opencode-auth.d.ts +3 -2
- package/dist/lib/opencode-auth.js +8 -1
- package/dist/lib/opencode-config-editor.d.ts +1 -0
- package/dist/lib/opencode-config-editor.js +6 -1
- package/dist/lib/opencode-go.js +53 -5
- package/dist/lib/opencode-version-core.d.ts +23 -0
- package/dist/lib/opencode-version-core.js +49 -0
- package/dist/lib/opencode-version.d.ts +4 -3
- package/dist/lib/opencode-version.js +15 -11
- package/dist/lib/opencode-zen.d.ts +12 -1
- package/dist/lib/opencode-zen.js +98 -12
- package/dist/lib/provider-add.js +5 -2
- package/dist/lib/provider-registration.d.ts +19 -9
- package/dist/lib/provider-registration.js +17 -6
- package/dist/lib/quota-command-format.js +1 -1
- package/dist/lib/quota-exhaustion-projection.d.ts +11 -0
- package/dist/lib/quota-exhaustion-projection.js +25 -0
- package/dist/lib/quota-providers-remote.d.ts +4 -1
- package/dist/lib/quota-providers-remote.js +137 -22
- package/dist/lib/quota-providers.d.ts +1 -1
- package/dist/lib/quota-providers.js +12 -35
- package/dist/lib/quota-render-data.d.ts +6 -0
- package/dist/lib/quota-render-data.js +43 -17
- package/dist/lib/quota-reset-notifications.d.ts +17 -0
- package/dist/lib/quota-reset-notifications.js +11 -2
- package/dist/lib/quota-state-codec.js +19 -1
- package/dist/lib/quota-state.d.ts +7 -1
- package/dist/lib/quota-state.js +208 -34
- package/dist/lib/quota-stats.js +21 -0
- package/dist/lib/quota-status.js +3 -51
- package/dist/lib/scoped-update.d.ts +1 -1
- package/dist/lib/synthetic.js +67 -48
- package/dist/lib/types.d.ts +49 -1
- package/dist/lib/types.js +16 -0
- package/dist/lib/xai.js +64 -24
- package/dist/plugin.d.ts +4 -3
- package/dist/plugin.js +293 -16
- package/dist/provider-logos/NOTICE.md +1 -1
- package/dist/provider-logos/OPENCODE-NOTICE.md +4 -3
- package/dist/providers/alibaba-coding-plan.js +107 -102
- package/dist/providers/alibaba-token-plan.js +17 -0
- package/dist/providers/anthropic.d.ts +12 -3
- package/dist/providers/anthropic.js +71 -219
- package/dist/providers/cache-policies.d.ts +11 -4
- package/dist/providers/cache-policies.js +19 -13
- package/dist/providers/claude-agent.d.ts +19 -0
- package/dist/providers/claude-agent.js +196 -0
- package/dist/providers/copilot.js +12 -2
- package/dist/providers/glm-coding-plan-provider.d.ts +3 -0
- package/dist/providers/glm-coding-plan-provider.js +5 -1
- package/dist/providers/google-account-format.js +2 -0
- package/dist/providers/google-agy.d.ts +6 -5
- package/dist/providers/google-agy.js +55 -17
- package/dist/providers/google-gemini-cli.js +13 -2
- package/dist/providers/kilo.js +10 -1
- package/dist/providers/kimi-code.js +13 -6
- package/dist/providers/mimo.js +9 -2
- package/dist/providers/minimax-coding-plan.js +28 -5
- package/dist/providers/nanogpt.js +4 -0
- package/dist/providers/ollama-cloud.js +3 -0
- package/dist/providers/openai.js +1 -0
- package/dist/providers/opencode-go.js +11 -1
- package/dist/providers/opencode-zen.js +32 -3
- package/dist/providers/openrouter.js +4 -0
- package/dist/providers/quota-providers.js +46 -9
- package/dist/providers/registry.js +2 -0
- package/dist/providers/result-helpers.d.ts +6 -0
- package/dist/providers/result-helpers.js +18 -2
- package/dist/rpc.d.ts +440 -4
- package/dist/rpc.js +507 -2
- package/dist/ui/assets/{index-Cs-nrywz.css → index-BcueIm3Z.css} +1 -1
- package/dist/ui/assets/index-C6qRSoh8.js +10 -0
- package/dist/ui/assets/{popover-DKRrwr_K.js → popover-4bFSvRZ7.js} +1 -1
- package/dist/ui/assets/providers/claude-agent.svg +3 -0
- package/dist/ui/index.html +2 -2
- package/package.json +15 -7
- package/dist/bin/opencode-quota-gui.d.ts.map +0 -1
- package/dist/bin/opencode-quota-gui.js.map +0 -1
- package/dist/contracts/analytics-entity.d.ts.map +0 -1
- package/dist/contracts/analytics-entity.js.map +0 -1
- package/dist/contracts/analytics-temporal.d.ts.map +0 -1
- package/dist/contracts/analytics-temporal.js.map +0 -1
- package/dist/contracts/build-identity.d.ts.map +0 -1
- package/dist/contracts/card-order.d.ts.map +0 -1
- package/dist/contracts/card-order.js.map +0 -1
- package/dist/contracts/collision-safe-key.d.ts.map +0 -1
- package/dist/contracts/collision-safe-key.js.map +0 -1
- package/dist/contracts/dashboard.d.ts.map +0 -1
- package/dist/contracts/dashboard.js.map +0 -1
- package/dist/contracts/guards.d.ts.map +0 -1
- package/dist/contracts/guards.js.map +0 -1
- package/dist/contracts/identity.d.ts.map +0 -1
- package/dist/contracts/identity.js.map +0 -1
- package/dist/contracts/openai-quota.d.ts.map +0 -1
- package/dist/contracts/openai-quota.js.map +0 -1
- package/dist/contracts/ranges.d.ts.map +0 -1
- package/dist/contracts/ranges.js.map +0 -1
- package/dist/contracts/settings.d.ts.map +0 -1
- package/dist/contracts/settings.js.map +0 -1
- package/dist/contracts/temporal.d.ts.map +0 -1
- package/dist/contracts/temporal.js.map +0 -1
- package/dist/contracts/time-zone.d.ts.map +0 -1
- package/dist/contracts/time-zone.js.map +0 -1
- package/dist/gui/aggregation.d.ts.map +0 -1
- package/dist/gui/aggregation.js.map +0 -1
- package/dist/gui/analytics-bridge.d.ts.map +0 -1
- package/dist/gui/analytics-bridge.js.map +0 -1
- package/dist/gui/analytics-engine.d.ts.map +0 -1
- package/dist/gui/analytics-engine.js.map +0 -1
- package/dist/gui/analytics-indexes.d.ts.map +0 -1
- package/dist/gui/analytics-indexes.js.map +0 -1
- package/dist/gui/analytics-model.d.ts.map +0 -1
- package/dist/gui/analytics-model.js.map +0 -1
- package/dist/gui/analytics-pricing-revision.d.ts.map +0 -1
- package/dist/gui/analytics-pricing-revision.js.map +0 -1
- package/dist/gui/analytics-protocol.d.ts.map +0 -1
- package/dist/gui/analytics-protocol.js.map +0 -1
- package/dist/gui/analytics-reader.d.ts.map +0 -1
- package/dist/gui/analytics-reader.js.map +0 -1
- package/dist/gui/analytics-service.d.ts.map +0 -1
- package/dist/gui/analytics-service.js.map +0 -1
- package/dist/gui/analytics-state.d.ts.map +0 -1
- package/dist/gui/analytics-state.js.map +0 -1
- package/dist/gui/analytics-worker-runtime.d.ts.map +0 -1
- package/dist/gui/analytics-worker-runtime.js.map +0 -1
- package/dist/gui/analytics-worker.d.ts.map +0 -1
- package/dist/gui/analytics-worker.js.map +0 -1
- package/dist/gui/config.d.ts.map +0 -1
- package/dist/gui/config.js.map +0 -1
- package/dist/gui/identity.d.ts.map +0 -1
- package/dist/gui/identity.js.map +0 -1
- package/dist/gui/lifecycle.d.ts.map +0 -1
- package/dist/gui/lifecycle.js.map +0 -1
- package/dist/gui/log.d.ts.map +0 -1
- package/dist/gui/log.js.map +0 -1
- package/dist/gui/provider-cards.d.ts.map +0 -1
- package/dist/gui/provider-cards.js.map +0 -1
- package/dist/gui/server.d.ts.map +0 -1
- package/dist/gui/server.js.map +0 -1
- package/dist/gui/settings-file-opener.d.ts.map +0 -1
- package/dist/gui/settings-file-opener.js.map +0 -1
- package/dist/gui/snapshot-service.d.ts.map +0 -1
- package/dist/gui/snapshot-service.js.map +0 -1
- package/dist/gui/sources.d.ts.map +0 -1
- package/dist/gui/sources.js.map +0 -1
- package/dist/gui/sse.d.ts.map +0 -1
- package/dist/gui/sse.js.map +0 -1
- package/dist/gui/temporal-quota-policy-data.d.ts.map +0 -1
- package/dist/gui/temporal-quota-policy-data.js.map +0 -1
- package/dist/gui/temporal-quota-policy.d.ts.map +0 -1
- package/dist/gui/temporal-quota-policy.js.map +0 -1
- package/dist/gui/temporal-status.d.ts.map +0 -1
- package/dist/gui/temporal-status.js.map +0 -1
- package/dist/gui/time-zone.d.ts.map +0 -1
- package/dist/gui/time-zone.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/accounting-format.d.ts.map +0 -1
- package/dist/lib/accounting-format.js.map +0 -1
- package/dist/lib/alibaba-auth.d.ts.map +0 -1
- package/dist/lib/alibaba-auth.js.map +0 -1
- package/dist/lib/alibaba-coding-plan-local-quota.d.ts +0 -47
- package/dist/lib/alibaba-coding-plan-local-quota.d.ts.map +0 -1
- package/dist/lib/alibaba-coding-plan-local-quota.js +0 -146
- package/dist/lib/alibaba-coding-plan-local-quota.js.map +0 -1
- package/dist/lib/alibaba-token-plan.d.ts.map +0 -1
- package/dist/lib/alibaba-token-plan.js.map +0 -1
- package/dist/lib/anthropic-auth.d.ts.map +0 -1
- package/dist/lib/anthropic-auth.js.map +0 -1
- package/dist/lib/anthropic.d.ts.map +0 -1
- package/dist/lib/anthropic.js.map +0 -1
- package/dist/lib/api-key-resolver.d.ts.map +0 -1
- package/dist/lib/api-key-resolver.js.map +0 -1
- package/dist/lib/atomic-json.d.ts.map +0 -1
- package/dist/lib/atomic-json.js.map +0 -1
- package/dist/lib/chutes-config.d.ts.map +0 -1
- package/dist/lib/chutes-config.js.map +0 -1
- package/dist/lib/chutes.d.ts.map +0 -1
- package/dist/lib/chutes.js.map +0 -1
- package/dist/lib/cli-reports.d.ts.map +0 -1
- package/dist/lib/cli-reports.js.map +0 -1
- package/dist/lib/cli-show.d.ts.map +0 -1
- package/dist/lib/cli-show.js.map +0 -1
- package/dist/lib/cli-status.d.ts.map +0 -1
- package/dist/lib/cli-status.js.map +0 -1
- package/dist/lib/command-parsing.d.ts.map +0 -1
- package/dist/lib/command-parsing.js.map +0 -1
- package/dist/lib/config-file-utils.d.ts.map +0 -1
- package/dist/lib/config-file-utils.js.map +0 -1
- package/dist/lib/config-write-target.d.ts.map +0 -1
- package/dist/lib/config-write-target.js.map +0 -1
- package/dist/lib/config.d.ts.map +0 -1
- package/dist/lib/config.js.map +0 -1
- package/dist/lib/copilot.d.ts.map +0 -1
- package/dist/lib/copilot.js.map +0 -1
- package/dist/lib/cursor-detection.d.ts.map +0 -1
- package/dist/lib/cursor-detection.js.map +0 -1
- package/dist/lib/cursor-pricing.d.ts.map +0 -1
- package/dist/lib/cursor-pricing.js.map +0 -1
- package/dist/lib/cursor-usage.d.ts.map +0 -1
- package/dist/lib/cursor-usage.js.map +0 -1
- package/dist/lib/dashboard-visibility.d.ts.map +0 -1
- package/dist/lib/dashboard-visibility.js.map +0 -1
- package/dist/lib/deepseek-auth.d.ts.map +0 -1
- package/dist/lib/deepseek-auth.js.map +0 -1
- package/dist/lib/deepseek.d.ts.map +0 -1
- package/dist/lib/deepseek.js.map +0 -1
- package/dist/lib/display-sanitize.d.ts.map +0 -1
- package/dist/lib/display-sanitize.js.map +0 -1
- package/dist/lib/entries.d.ts.map +0 -1
- package/dist/lib/entries.js.map +0 -1
- package/dist/lib/env-template.d.ts.map +0 -1
- package/dist/lib/env-template.js.map +0 -1
- package/dist/lib/format-utils.d.ts.map +0 -1
- package/dist/lib/format-utils.js.map +0 -1
- package/dist/lib/format.d.ts.map +0 -1
- package/dist/lib/format.js.map +0 -1
- package/dist/lib/glm-coding-plan.d.ts.map +0 -1
- package/dist/lib/glm-coding-plan.js.map +0 -1
- package/dist/lib/google-agy-companion.d.ts.map +0 -1
- package/dist/lib/google-agy-companion.js.map +0 -1
- package/dist/lib/google-agy.d.ts.map +0 -1
- package/dist/lib/google-agy.js.map +0 -1
- package/dist/lib/google-companion-credentials.d.ts.map +0 -1
- package/dist/lib/google-companion-credentials.js.map +0 -1
- package/dist/lib/google-gemini-cli-companion.d.ts.map +0 -1
- package/dist/lib/google-gemini-cli-companion.js.map +0 -1
- package/dist/lib/google-gemini-cli.d.ts.map +0 -1
- package/dist/lib/google-gemini-cli.js.map +0 -1
- package/dist/lib/google-token-cache.d.ts.map +0 -1
- package/dist/lib/google-token-cache.js.map +0 -1
- package/dist/lib/grouped-entry-normalization.d.ts.map +0 -1
- package/dist/lib/grouped-entry-normalization.js.map +0 -1
- package/dist/lib/grouped-header-format.d.ts.map +0 -1
- package/dist/lib/grouped-header-format.js.map +0 -1
- package/dist/lib/gui-config-target.d.ts.map +0 -1
- package/dist/lib/gui-config-target.js.map +0 -1
- package/dist/lib/home-dir-display.d.ts.map +0 -1
- package/dist/lib/home-dir-display.js.map +0 -1
- package/dist/lib/http.d.ts.map +0 -1
- package/dist/lib/http.js.map +0 -1
- package/dist/lib/init-installer.d.ts.map +0 -1
- package/dist/lib/init-installer.js.map +0 -1
- package/dist/lib/jsonc.d.ts.map +0 -1
- package/dist/lib/jsonc.js.map +0 -1
- package/dist/lib/kilo-config.d.ts.map +0 -1
- package/dist/lib/kilo-config.js.map +0 -1
- package/dist/lib/kilo.d.ts.map +0 -1
- package/dist/lib/kilo.js.map +0 -1
- package/dist/lib/kimi-auth.d.ts.map +0 -1
- package/dist/lib/kimi-auth.js.map +0 -1
- package/dist/lib/kimi-endpoints.d.ts.map +0 -1
- package/dist/lib/kimi-endpoints.js.map +0 -1
- package/dist/lib/kimi.d.ts.map +0 -1
- package/dist/lib/kimi.js.map +0 -1
- package/dist/lib/maintainer-announcements.d.ts.map +0 -1
- package/dist/lib/maintainer-announcements.js.map +0 -1
- package/dist/lib/map-with-concurrency.d.ts.map +0 -1
- package/dist/lib/map-with-concurrency.js.map +0 -1
- package/dist/lib/markdown-table.d.ts.map +0 -1
- package/dist/lib/markdown-table.js.map +0 -1
- package/dist/lib/mimo-config.d.ts.map +0 -1
- package/dist/lib/mimo-config.js.map +0 -1
- package/dist/lib/mimo.d.ts.map +0 -1
- package/dist/lib/mimo.js.map +0 -1
- package/dist/lib/minimax-auth.d.ts.map +0 -1
- package/dist/lib/minimax-auth.js.map +0 -1
- package/dist/lib/minimax-endpoints.d.ts.map +0 -1
- package/dist/lib/minimax-endpoints.js.map +0 -1
- package/dist/lib/modelsdev-pricing.d.ts.map +0 -1
- package/dist/lib/modelsdev-pricing.js.map +0 -1
- package/dist/lib/modelsdev-temporal.d.ts.map +0 -1
- package/dist/lib/modelsdev-temporal.js.map +0 -1
- package/dist/lib/nanogpt-config.d.ts.map +0 -1
- package/dist/lib/nanogpt-config.js.map +0 -1
- package/dist/lib/nanogpt.d.ts.map +0 -1
- package/dist/lib/nanogpt.js.map +0 -1
- package/dist/lib/ollama-cloud-config.d.ts.map +0 -1
- package/dist/lib/ollama-cloud-config.js.map +0 -1
- package/dist/lib/ollama-cloud.d.ts.map +0 -1
- package/dist/lib/ollama-cloud.js.map +0 -1
- package/dist/lib/openai.d.ts.map +0 -1
- package/dist/lib/openai.js.map +0 -1
- package/dist/lib/opencode-auth-sqlite.d.ts.map +0 -1
- package/dist/lib/opencode-auth-sqlite.js.map +0 -1
- package/dist/lib/opencode-auth.d.ts.map +0 -1
- package/dist/lib/opencode-auth.js.map +0 -1
- package/dist/lib/opencode-config-editor.d.ts.map +0 -1
- package/dist/lib/opencode-config-editor.js.map +0 -1
- package/dist/lib/opencode-config-providers.d.ts.map +0 -1
- package/dist/lib/opencode-config-providers.js.map +0 -1
- package/dist/lib/opencode-config-read.d.ts.map +0 -1
- package/dist/lib/opencode-config-read.js.map +0 -1
- package/dist/lib/opencode-console-auth.d.ts.map +0 -1
- package/dist/lib/opencode-console-auth.js.map +0 -1
- package/dist/lib/opencode-db-path.d.ts.map +0 -1
- package/dist/lib/opencode-db-path.js.map +0 -1
- package/dist/lib/opencode-go-auth.d.ts.map +0 -1
- package/dist/lib/opencode-go-auth.js.map +0 -1
- package/dist/lib/opencode-go.d.ts.map +0 -1
- package/dist/lib/opencode-go.js.map +0 -1
- package/dist/lib/opencode-runtime-paths.d.ts.map +0 -1
- package/dist/lib/opencode-runtime-paths.js.map +0 -1
- package/dist/lib/opencode-sqlite.d.ts.map +0 -1
- package/dist/lib/opencode-sqlite.js.map +0 -1
- package/dist/lib/opencode-storage.d.ts.map +0 -1
- package/dist/lib/opencode-storage.js.map +0 -1
- package/dist/lib/opencode-version.d.ts.map +0 -1
- package/dist/lib/opencode-version.js.map +0 -1
- package/dist/lib/opencode-zen.d.ts.map +0 -1
- package/dist/lib/opencode-zen.js.map +0 -1
- package/dist/lib/openrouter.d.ts.map +0 -1
- package/dist/lib/openrouter.js.map +0 -1
- package/dist/lib/private-state-paths.d.ts.map +0 -1
- package/dist/lib/private-state-paths.js.map +0 -1
- package/dist/lib/provider-add-command.d.ts.map +0 -1
- package/dist/lib/provider-add-command.js.map +0 -1
- package/dist/lib/provider-add-json-v1-questionnaire.d.ts.map +0 -1
- package/dist/lib/provider-add-json-v1-questionnaire.js.map +0 -1
- package/dist/lib/provider-add.d.ts.map +0 -1
- package/dist/lib/provider-add.js.map +0 -1
- package/dist/lib/provider-availability.d.ts.map +0 -1
- package/dist/lib/provider-availability.js.map +0 -1
- package/dist/lib/provider-metadata.d.ts.map +0 -1
- package/dist/lib/provider-metadata.js.map +0 -1
- package/dist/lib/provider-model-matching.d.ts.map +0 -1
- package/dist/lib/provider-model-matching.js.map +0 -1
- package/dist/lib/provider-registration.d.ts.map +0 -1
- package/dist/lib/provider-registration.js.map +0 -1
- package/dist/lib/quota-accounting-projection.d.ts.map +0 -1
- package/dist/lib/quota-accounting-projection.js.map +0 -1
- package/dist/lib/quota-columns.d.ts.map +0 -1
- package/dist/lib/quota-columns.js.map +0 -1
- package/dist/lib/quota-command-format.d.ts.map +0 -1
- package/dist/lib/quota-command-format.js.map +0 -1
- package/dist/lib/quota-dialog-command-specs.d.ts.map +0 -1
- package/dist/lib/quota-dialog-command-specs.js.map +0 -1
- package/dist/lib/quota-dialog-commands.d.ts.map +0 -1
- package/dist/lib/quota-dialog-commands.js.map +0 -1
- package/dist/lib/quota-entry-display.d.ts.map +0 -1
- package/dist/lib/quota-entry-display.js.map +0 -1
- package/dist/lib/quota-exhaustion-projection.d.ts.map +0 -1
- package/dist/lib/quota-exhaustion-projection.js.map +0 -1
- package/dist/lib/quota-export-types.d.ts.map +0 -1
- package/dist/lib/quota-export-types.js.map +0 -1
- package/dist/lib/quota-export.d.ts.map +0 -1
- package/dist/lib/quota-export.js.map +0 -1
- package/dist/lib/quota-format-style.d.ts.map +0 -1
- package/dist/lib/quota-format-style.js.map +0 -1
- package/dist/lib/quota-providers-local.d.ts.map +0 -1
- package/dist/lib/quota-providers-local.js.map +0 -1
- package/dist/lib/quota-providers-remote.d.ts.map +0 -1
- package/dist/lib/quota-providers-remote.js.map +0 -1
- package/dist/lib/quota-providers.d.ts.map +0 -1
- package/dist/lib/quota-providers.js.map +0 -1
- package/dist/lib/quota-render-data.d.ts.map +0 -1
- package/dist/lib/quota-render-data.js.map +0 -1
- package/dist/lib/quota-report-message.d.ts.map +0 -1
- package/dist/lib/quota-report-message.js.map +0 -1
- package/dist/lib/quota-reset-notifications.d.ts.map +0 -1
- package/dist/lib/quota-reset-notifications.js.map +0 -1
- package/dist/lib/quota-retry-wait.d.ts.map +0 -1
- package/dist/lib/quota-retry-wait.js.map +0 -1
- package/dist/lib/quota-runtime-context.d.ts.map +0 -1
- package/dist/lib/quota-runtime-context.js.map +0 -1
- package/dist/lib/quota-state-codec.d.ts.map +0 -1
- package/dist/lib/quota-state-codec.js.map +0 -1
- package/dist/lib/quota-state.d.ts.map +0 -1
- package/dist/lib/quota-state.js.map +0 -1
- package/dist/lib/quota-stats-format.d.ts.map +0 -1
- package/dist/lib/quota-stats-format.js.map +0 -1
- package/dist/lib/quota-stats.d.ts.map +0 -1
- package/dist/lib/quota-stats.js.map +0 -1
- package/dist/lib/quota-status.d.ts.map +0 -1
- package/dist/lib/quota-status.js.map +0 -1
- package/dist/lib/quota-surface-data.d.ts.map +0 -1
- package/dist/lib/quota-surface-data.js.map +0 -1
- package/dist/lib/quota-telemetry.d.ts.map +0 -1
- package/dist/lib/quota-telemetry.js.map +0 -1
- package/dist/lib/report-document.d.ts.map +0 -1
- package/dist/lib/report-document.js.map +0 -1
- package/dist/lib/resolved-auth-identity.d.ts.map +0 -1
- package/dist/lib/resolved-auth-identity.js.map +0 -1
- package/dist/lib/runtime-provider-ids.d.ts.map +0 -1
- package/dist/lib/runtime-provider-ids.js.map +0 -1
- package/dist/lib/scoped-query-chunks.d.ts.map +0 -1
- package/dist/lib/scoped-query-chunks.js.map +0 -1
- package/dist/lib/scoped-update-migration.d.ts.map +0 -1
- package/dist/lib/scoped-update-migration.js.map +0 -1
- package/dist/lib/scoped-update.d.ts.map +0 -1
- package/dist/lib/scoped-update.js.map +0 -1
- package/dist/lib/session-tokens-format.d.ts.map +0 -1
- package/dist/lib/session-tokens-format.js.map +0 -1
- package/dist/lib/session-tokens.d.ts.map +0 -1
- package/dist/lib/session-tokens.js.map +0 -1
- package/dist/lib/synthetic-config.d.ts.map +0 -1
- package/dist/lib/synthetic-config.js.map +0 -1
- package/dist/lib/synthetic.d.ts.map +0 -1
- package/dist/lib/synthetic.js.map +0 -1
- package/dist/lib/toast-format-grouped.d.ts.map +0 -1
- package/dist/lib/toast-format-grouped.js.map +0 -1
- package/dist/lib/token-buckets.d.ts.map +0 -1
- package/dist/lib/token-buckets.js.map +0 -1
- package/dist/lib/token-cost.d.ts.map +0 -1
- package/dist/lib/token-cost.js.map +0 -1
- package/dist/lib/tui-compact-format.d.ts.map +0 -1
- package/dist/lib/tui-compact-format.js.map +0 -1
- package/dist/lib/tui-panel-state.d.ts.map +0 -1
- package/dist/lib/tui-panel-state.js.map +0 -1
- package/dist/lib/tui-prompt-bar-format.d.ts.map +0 -1
- package/dist/lib/tui-prompt-bar-format.js.map +0 -1
- package/dist/lib/tui-sidebar-format.d.ts.map +0 -1
- package/dist/lib/tui-sidebar-format.js.map +0 -1
- package/dist/lib/types.d.ts.map +0 -1
- package/dist/lib/types.js.map +0 -1
- package/dist/lib/usage-ownership.d.ts.map +0 -1
- package/dist/lib/usage-ownership.js.map +0 -1
- package/dist/lib/version.d.ts.map +0 -1
- package/dist/lib/version.js.map +0 -1
- package/dist/lib/xai.d.ts.map +0 -1
- package/dist/lib/xai.js.map +0 -1
- package/dist/lib/zai-auth.d.ts.map +0 -1
- package/dist/lib/zai-auth.js.map +0 -1
- package/dist/lib/zai.d.ts.map +0 -1
- package/dist/lib/zai.js.map +0 -1
- package/dist/lib/zhipu-auth.d.ts.map +0 -1
- package/dist/lib/zhipu-auth.js.map +0 -1
- package/dist/lib/zhipu.d.ts.map +0 -1
- package/dist/lib/zhipu.js.map +0 -1
- package/dist/plugin.d.ts.map +0 -1
- package/dist/plugin.js.map +0 -1
- package/dist/providers/accounting-decimal.d.ts.map +0 -1
- package/dist/providers/accounting-decimal.js.map +0 -1
- package/dist/providers/alibaba-coding-plan.d.ts.map +0 -1
- package/dist/providers/alibaba-coding-plan.js.map +0 -1
- package/dist/providers/alibaba-token-plan.d.ts.map +0 -1
- package/dist/providers/alibaba-token-plan.js.map +0 -1
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/cache-policies.d.ts.map +0 -1
- package/dist/providers/cache-policies.js.map +0 -1
- package/dist/providers/chutes.d.ts.map +0 -1
- package/dist/providers/chutes.js.map +0 -1
- package/dist/providers/copilot.d.ts.map +0 -1
- package/dist/providers/copilot.js.map +0 -1
- package/dist/providers/cursor.d.ts.map +0 -1
- package/dist/providers/cursor.js.map +0 -1
- package/dist/providers/deepseek.d.ts.map +0 -1
- package/dist/providers/deepseek.js.map +0 -1
- package/dist/providers/glm-coding-plan-provider.d.ts.map +0 -1
- package/dist/providers/glm-coding-plan-provider.js.map +0 -1
- package/dist/providers/google-account-format.d.ts.map +0 -1
- package/dist/providers/google-account-format.js.map +0 -1
- package/dist/providers/google-agy.d.ts.map +0 -1
- package/dist/providers/google-agy.js.map +0 -1
- package/dist/providers/google-gemini-cli.d.ts.map +0 -1
- package/dist/providers/google-gemini-cli.js.map +0 -1
- package/dist/providers/kilo.d.ts.map +0 -1
- package/dist/providers/kilo.js.map +0 -1
- package/dist/providers/kimi-code.d.ts.map +0 -1
- package/dist/providers/kimi-code.js.map +0 -1
- package/dist/providers/mimo.d.ts.map +0 -1
- package/dist/providers/mimo.js.map +0 -1
- package/dist/providers/minimax-coding-plan.d.ts.map +0 -1
- package/dist/providers/minimax-coding-plan.js.map +0 -1
- package/dist/providers/nanogpt.d.ts.map +0 -1
- package/dist/providers/nanogpt.js.map +0 -1
- package/dist/providers/ollama-cloud.d.ts.map +0 -1
- package/dist/providers/ollama-cloud.js.map +0 -1
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js.map +0 -1
- package/dist/providers/opencode-go.d.ts.map +0 -1
- package/dist/providers/opencode-go.js.map +0 -1
- package/dist/providers/opencode-zen.d.ts.map +0 -1
- package/dist/providers/opencode-zen.js.map +0 -1
- package/dist/providers/openrouter.d.ts.map +0 -1
- package/dist/providers/openrouter.js.map +0 -1
- package/dist/providers/quota-providers.d.ts.map +0 -1
- package/dist/providers/quota-providers.js.map +0 -1
- package/dist/providers/registry.d.ts.map +0 -1
- package/dist/providers/registry.js.map +0 -1
- package/dist/providers/result-helpers.d.ts.map +0 -1
- package/dist/providers/result-helpers.js.map +0 -1
- package/dist/providers/synthetic.d.ts.map +0 -1
- package/dist/providers/synthetic.js.map +0 -1
- package/dist/providers/xai.d.ts.map +0 -1
- package/dist/providers/xai.js.map +0 -1
- package/dist/providers/zai.d.ts.map +0 -1
- package/dist/providers/zai.js.map +0 -1
- package/dist/providers/zhipu.d.ts.map +0 -1
- package/dist/providers/zhipu.js.map +0 -1
- package/dist/rpc.d.ts.map +0 -1
- package/dist/rpc.js.map +0 -1
- package/dist/ui/assets/index-D9lFKhSN.js +0 -9
package/dist/lib/anthropic.js
CHANGED
|
@@ -4,36 +4,62 @@
|
|
|
4
4
|
* Uses the local Claude CLI/runtime to detect install/auth state first. When
|
|
5
5
|
* Claude auth is confirmed but local quota windows are missing, it falls back
|
|
6
6
|
* to Anthropic's OAuth usage endpoint using the first usable OAuth access
|
|
7
|
-
* token
|
|
8
|
-
* Keychain first, then the local credentials file).
|
|
7
|
+
* token from Claude OAuth credentials (macOS Keychain first, then the local credentials file).
|
|
9
8
|
*/
|
|
10
9
|
import { execFile } from "child_process";
|
|
11
10
|
import { createHash } from "crypto";
|
|
12
11
|
import { existsSync } from "fs";
|
|
13
12
|
import { readFile } from "fs/promises";
|
|
14
13
|
import { homedir } from "os";
|
|
15
|
-
import { join } from "path";
|
|
16
|
-
import { resolveAnthropicOAuthCached } from "./anthropic-auth.js";
|
|
14
|
+
import { isAbsolute, join, resolve } from "path";
|
|
17
15
|
import { sanitizeDisplaySnippet, sanitizeDisplayText } from "./display-sanitize.js";
|
|
18
16
|
import { fetchWithTimeout } from "./http.js";
|
|
17
|
+
import { buildFixedWindowEvidence } from "./quota-exhaustion-projection.js";
|
|
19
18
|
import { composeResolvedAuthIdentities, deriveResolvedAuthIdentity, } from "./resolved-auth-identity.js";
|
|
19
|
+
import { REQUEST_TIMEOUT_MS } from "./types.js";
|
|
20
20
|
const DEFAULT_CLAUDE_BINARY = "claude";
|
|
21
21
|
/** How `binaryPath` reads when the default `claude` ran from PATH. */
|
|
22
22
|
const CLAUDE_ON_PATH_DISPLAY = "claude (PATH)";
|
|
23
23
|
const CLAUDE_COMMAND_TIMEOUT_MS = 3_000;
|
|
24
|
-
const ANTHROPIC_DIAGNOSTICS_TTL_MS = 5_000;
|
|
25
24
|
const ANTHROPIC_OAUTH_BACKOFF_BASE_MS = 30_000;
|
|
26
25
|
const ANTHROPIC_OAUTH_COOLDOWN_MAX_MS = 15 * 60_000;
|
|
27
26
|
const ANTHROPIC_OAUTH_COOLDOWN_MAX_ENTRIES = 16;
|
|
27
|
+
const ANTHROPIC_OAUTH_REUSE_MS = 60_000;
|
|
28
28
|
const ANTHROPIC_USAGE_URL = "https://api.anthropic.com/api/oauth/usage";
|
|
29
|
+
const ANTHROPIC_USAGE_ENHANCED_URL = `${ANTHROPIC_USAGE_URL}?cedar_ember=1`;
|
|
29
30
|
const ANTHROPIC_BETA_HEADER = "oauth-2025-04-20";
|
|
30
31
|
const CLAUDE_CODE_CREDENTIALS_SERVICE = "Claude Code-credentials";
|
|
31
32
|
const CLAUDE_NO_LOCAL_QUOTA_MESSAGE = "Claude CLI auth detected, but local quota windows were not exposed.";
|
|
32
33
|
const ANTHROPIC_NO_QUOTA_MESSAGE = "Claude CLI auth detected, but quota was unavailable from the local CLI and OAuth credential sources.";
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
const
|
|
34
|
+
const CLAUDE_NON_SUBSCRIPTION_LOGIN_MESSAGE = "Claude CLI is not signed in with a claude.ai subscription. Run `claude auth login` and try again.";
|
|
35
|
+
const CLAUDE_CLI_TOKEN_UNVERIFIED_DETAIL = "Claude CLI did not confirm a first-party claude.ai login, so its stored token is not used.";
|
|
36
|
+
/** Environment variable the Claude CLI reads for its config directory. */
|
|
37
|
+
const CLAUDE_CONFIG_DIR_ENV = "CLAUDE_CONFIG_DIR";
|
|
38
|
+
const CLAUDE_CONFIG_DIRECTORY_UNRESOLVED_DETAIL = "The Claude CLI config directory could not be determined, so its login is not used.";
|
|
39
|
+
/** Fixed labels for where a Claude login is kept. They name neither the home folder nor a custom path. */
|
|
40
|
+
const CLAUDE_DEFAULT_CREDENTIALS_LOCATION = "~/.claude/.credentials.json";
|
|
41
|
+
const CLAUDE_CUSTOM_CONFIG_DIRECTORY_LOCATION = "the Claude CLI config directory";
|
|
42
|
+
const ANTHROPIC_OAUTH_SHARED_STATE_KEY = Symbol.for("opencode-quota-gui.anthropic-oauth-shared-state.v1");
|
|
43
|
+
const anthropicOAuthGlobal = globalThis;
|
|
44
|
+
let anthropicOAuthSharedState = anthropicOAuthGlobal[ANTHROPIC_OAUTH_SHARED_STATE_KEY];
|
|
45
|
+
if (anthropicOAuthSharedState === undefined) {
|
|
46
|
+
anthropicOAuthSharedState = {
|
|
47
|
+
cooldowns: new Map(),
|
|
48
|
+
inFlight: new Map(),
|
|
49
|
+
recent: new Map(),
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(anthropicOAuthGlobal, ANTHROPIC_OAUTH_SHARED_STATE_KEY, {
|
|
52
|
+
value: anthropicOAuthSharedState,
|
|
53
|
+
configurable: true,
|
|
54
|
+
writable: true,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const anthropicOAuthCooldowns = anthropicOAuthSharedState.cooldowns;
|
|
58
|
+
const anthropicOAuthInFlight = anthropicOAuthSharedState.inFlight;
|
|
59
|
+
anthropicOAuthSharedState.forcedInFlight ??= new Map();
|
|
60
|
+
const anthropicOAuthForcedInFlight = anthropicOAuthSharedState.forcedInFlight;
|
|
61
|
+
const anthropicOAuthRecent = anthropicOAuthSharedState.recent;
|
|
62
|
+
const anthropicForcedResultsKey = Symbol("anthropic forced results");
|
|
37
63
|
export function resolveAnthropicBinaryPath(binaryPath) {
|
|
38
64
|
const trimmed = binaryPath?.trim();
|
|
39
65
|
return trimmed ? trimmed : DEFAULT_CLAUDE_BINARY;
|
|
@@ -60,9 +86,16 @@ function formatCommandDisplayArg(value) {
|
|
|
60
86
|
function formatCommandDisplay(parts) {
|
|
61
87
|
return parts.map(formatCommandDisplayArg).join(" ");
|
|
62
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* cmd.exe cannot escape `"` inside a quoted argument, and `%` and `!` expand even inside
|
|
91
|
+
* quotes, so a path containing any of these is refused rather than handed to the shell.
|
|
92
|
+
*/
|
|
93
|
+
const WINDOWS_CMD_UNSAFE_ARG = /[%!^&|<>"\r\n]/u;
|
|
63
94
|
function quoteWindowsCmdArg(value) {
|
|
64
|
-
|
|
65
|
-
|
|
95
|
+
if (WINDOWS_CMD_UNSAFE_ARG.test(value)) {
|
|
96
|
+
throw new Error("Claude CLI path contains characters that cannot be passed safely through cmd.exe.");
|
|
97
|
+
}
|
|
98
|
+
return `"${value}"`;
|
|
66
99
|
}
|
|
67
100
|
function shouldBridgeClaudeCommandThroughWindowsShell(binaryPath) {
|
|
68
101
|
const normalized = binaryPath.trim().toLowerCase();
|
|
@@ -76,10 +109,13 @@ export function buildClaudeCommandInvocation(binaryPath, args, runtime = {}) {
|
|
|
76
109
|
const display = formatCommandDisplay([resolvedBinaryPath, ...args]);
|
|
77
110
|
if ((runtime.platform ?? process.platform) === "win32" &&
|
|
78
111
|
shouldBridgeClaudeCommandThroughWindowsShell(resolvedBinaryPath)) {
|
|
112
|
+
// `/s` strips only the outermost quote pair, so the quoted command line is wrapped once more.
|
|
113
|
+
const commandLine = [resolvedBinaryPath, ...args].map(quoteWindowsCmdArg).join(" ");
|
|
79
114
|
return {
|
|
80
115
|
file: runtime.comspec?.trim() || process.env["ComSpec"]?.trim() || "cmd.exe",
|
|
81
|
-
args: ["/d", "/s", "/c",
|
|
116
|
+
args: ["/d", "/s", "/c", `"${commandLine}"`],
|
|
82
117
|
display,
|
|
118
|
+
windowsVerbatimArguments: true,
|
|
83
119
|
};
|
|
84
120
|
}
|
|
85
121
|
return {
|
|
@@ -161,8 +197,19 @@ function parseExtraUsageQuota(extraUsage) {
|
|
|
161
197
|
if (!record || record["is_enabled"] !== true) {
|
|
162
198
|
return undefined;
|
|
163
199
|
}
|
|
164
|
-
const
|
|
165
|
-
|
|
200
|
+
const utilization = record["utilization"];
|
|
201
|
+
const usedCredits = record["used_credits"];
|
|
202
|
+
const monthlyLimit = record["monthly_limit"];
|
|
203
|
+
const used = typeof utilization === "number" && Number.isFinite(utilization)
|
|
204
|
+
? utilization
|
|
205
|
+
: typeof usedCredits === "number" &&
|
|
206
|
+
Number.isFinite(usedCredits) &&
|
|
207
|
+
typeof monthlyLimit === "number" &&
|
|
208
|
+
Number.isFinite(monthlyLimit) &&
|
|
209
|
+
monthlyLimit > 0
|
|
210
|
+
? (usedCredits / monthlyLimit) * 100
|
|
211
|
+
: undefined;
|
|
212
|
+
if (used === undefined || used < 0 || used > 100) {
|
|
166
213
|
return undefined;
|
|
167
214
|
}
|
|
168
215
|
return { percentRemaining: Math.round(100 - used) };
|
|
@@ -217,46 +264,202 @@ function parseFableWeeklyWindow(limits) {
|
|
|
217
264
|
}
|
|
218
265
|
return undefined;
|
|
219
266
|
}
|
|
220
|
-
function
|
|
267
|
+
function readUsageWindow(root, snakeKey, camelKey) {
|
|
268
|
+
const hasSnakeKey = Object.hasOwn(root, snakeKey);
|
|
269
|
+
const hasCamelKey = Object.hasOwn(root, camelKey);
|
|
270
|
+
if (!hasSnakeKey && !hasCamelKey) {
|
|
271
|
+
return { state: "absent" };
|
|
272
|
+
}
|
|
273
|
+
if (hasSnakeKey && hasCamelKey && root[snakeKey] !== root[camelKey]) {
|
|
274
|
+
return { state: "malformed" };
|
|
275
|
+
}
|
|
276
|
+
const value = hasSnakeKey ? root[snakeKey] : root[camelKey];
|
|
277
|
+
if (value === null) {
|
|
278
|
+
return { state: "absent" };
|
|
279
|
+
}
|
|
280
|
+
const window = parseQuotaWindow(value);
|
|
281
|
+
return window ? { state: "present", window } : { state: "malformed" };
|
|
282
|
+
}
|
|
283
|
+
function findUsageWindows(data, options = {}) {
|
|
221
284
|
for (const root of getUsageRoots(data)) {
|
|
222
|
-
const fiveHour =
|
|
223
|
-
const sevenDay =
|
|
224
|
-
if (
|
|
285
|
+
const fiveHour = readUsageWindow(root, "five_hour", "fiveHour");
|
|
286
|
+
const sevenDay = readUsageWindow(root, "seven_day", "sevenDay");
|
|
287
|
+
if (fiveHour.state === "malformed" || sevenDay.state === "malformed") {
|
|
225
288
|
continue;
|
|
226
289
|
}
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
290
|
+
const fiveHourWindow = fiveHour.state === "present" ? fiveHour.window : undefined;
|
|
291
|
+
const sevenDayWindow = sevenDay.state === "present" ? sevenDay.window : undefined;
|
|
292
|
+
if (!fiveHourWindow && !sevenDayWindow) {
|
|
293
|
+
if (!options.allowSupplementalOnly) {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (!parseExtraUsageQuota(root["extra_usage"]) && !parseFableWeeklyWindow(root["limits"])) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
230
300
|
return {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
...(extraUsage ? { extra_usage: extraUsage } : {}),
|
|
301
|
+
root,
|
|
302
|
+
...(fiveHourWindow ? { fiveHour: fiveHourWindow } : {}),
|
|
303
|
+
...(sevenDayWindow ? { sevenDay: sevenDayWindow } : {}),
|
|
235
304
|
};
|
|
236
305
|
}
|
|
237
306
|
return null;
|
|
238
307
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
308
|
+
/** Anthropic's 5-hour session window and its weekly windows (including the Fable weekly scope). */
|
|
309
|
+
const FIVE_HOUR_WINDOW_MS = 5 * 60 * 60_000;
|
|
310
|
+
const SEVEN_DAY_WINDOW_MS = 7 * 24 * 60 * 60_000;
|
|
311
|
+
/**
|
|
312
|
+
* Attaches fixed-window evidence derived from the window length and its reset. The 5h and weekly
|
|
313
|
+
* windows are assumed to reset on a fixed cycle; that is not verified against the provider. A
|
|
314
|
+
* window without a valid reset, or observed outside its inferred span, gets no evidence.
|
|
315
|
+
*/
|
|
316
|
+
function windowWithFixedEvidence(window, durationMs, observedAtMs) {
|
|
317
|
+
const fixedWindow = buildFixedWindowEvidence({
|
|
318
|
+
durationMs,
|
|
319
|
+
resetTimeIso: window.resetTimeIso,
|
|
320
|
+
observedAtMs,
|
|
321
|
+
});
|
|
322
|
+
return fixedWindow ? { ...window, fixedWindow } : window;
|
|
323
|
+
}
|
|
324
|
+
function parseUsageResponse(data, options = {}) {
|
|
325
|
+
const windows = findUsageWindows(data);
|
|
326
|
+
if (!windows?.fiveHour || !windows.sevenDay)
|
|
327
|
+
return null;
|
|
328
|
+
const extraUsage = options.includeExtraUsage
|
|
329
|
+
? parseExtraUsageQuota(windows.root["extra_usage"])
|
|
330
|
+
: undefined;
|
|
331
|
+
const observedAtMs = options.observedAtMs ?? Date.now();
|
|
332
|
+
return {
|
|
333
|
+
success: true,
|
|
334
|
+
five_hour: windowWithFixedEvidence(windows.fiveHour, FIVE_HOUR_WINDOW_MS, observedAtMs),
|
|
335
|
+
seven_day: windowWithFixedEvidence(windows.sevenDay, SEVEN_DAY_WINDOW_MS, observedAtMs),
|
|
336
|
+
...(extraUsage ? { extra_usage: extraUsage } : {}),
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
/** Display cap for saved reset segments and the displayed count; larger valid inventories are truncated, not hidden. */
|
|
340
|
+
const MAX_SAVED_RESET_SEGMENTS = 100;
|
|
341
|
+
/** An optional ISO date: absent/null is open (`undefined`), a bad string is invalid (`NaN`). */
|
|
342
|
+
function savedResetDateMs(value) {
|
|
343
|
+
if (value === undefined || value === null)
|
|
344
|
+
return undefined;
|
|
345
|
+
return typeof value === "string" ? Date.parse(value) : Number.NaN;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Independent of the quota windows: any malformed part only drops that grant (or the inventory).
|
|
349
|
+
* Identifiers, labels and every other field are discarded; unknown expiry stays absent.
|
|
350
|
+
*/
|
|
351
|
+
function parseSavedResets(raw, nowMs) {
|
|
352
|
+
const root = asRecord(raw);
|
|
353
|
+
if (root?.["eligible"] !== true || !Array.isArray(root["grants"]))
|
|
354
|
+
return undefined;
|
|
355
|
+
const grants = [];
|
|
356
|
+
for (const entry of root["grants"]) {
|
|
357
|
+
const grant = asRecord(entry);
|
|
358
|
+
if (!grant || grant["paused"] !== false)
|
|
359
|
+
continue;
|
|
360
|
+
const left = grant["resets_left"];
|
|
361
|
+
if (typeof left !== "number" || !Number.isSafeInteger(left) || left <= 0)
|
|
362
|
+
continue;
|
|
363
|
+
const startMs = savedResetDateMs(grant["starts_at"]);
|
|
364
|
+
const endMs = savedResetDateMs(grant["ends_at"]);
|
|
365
|
+
if (Number.isNaN(startMs) || Number.isNaN(endMs))
|
|
366
|
+
continue;
|
|
367
|
+
if (startMs !== undefined && startMs > nowMs)
|
|
368
|
+
continue;
|
|
369
|
+
if (endMs !== undefined && endMs <= nowMs)
|
|
370
|
+
continue;
|
|
371
|
+
const total = grant["resets_total"];
|
|
372
|
+
if (total !== undefined &&
|
|
373
|
+
(typeof total !== "number" || !Number.isSafeInteger(total) || total < left)) {
|
|
244
374
|
continue;
|
|
245
375
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
376
|
+
// Counts are clamped per grant so huge safe integers never grow the sum or the allocation.
|
|
377
|
+
grants.push({ count: Math.min(left, MAX_SAVED_RESET_SEGMENTS), endMs });
|
|
378
|
+
}
|
|
379
|
+
// Earliest known expiry first, unknown last; stable for equal expiries.
|
|
380
|
+
grants.sort((a, b) => (a.endMs ?? Infinity) - (b.endMs ?? Infinity) || 0);
|
|
381
|
+
const credits = [];
|
|
382
|
+
for (const grant of grants) {
|
|
383
|
+
const expiry = grant.endMs === undefined ? {} : { expiresAtIso: new Date(grant.endMs).toISOString() };
|
|
384
|
+
for (let n = grant.count; n > 0 && credits.length < MAX_SAVED_RESET_SEGMENTS; n -= 1) {
|
|
385
|
+
credits.push({ ...expiry });
|
|
386
|
+
}
|
|
255
387
|
}
|
|
256
|
-
return
|
|
388
|
+
return credits.length > 0 ? { availableCount: credits.length, credits } : undefined;
|
|
389
|
+
}
|
|
390
|
+
function parseOAuthUsageResponse(data, observedAtMs = Date.now()) {
|
|
391
|
+
const windows = findUsageWindows(data, { allowSupplementalOnly: true });
|
|
392
|
+
if (!windows)
|
|
393
|
+
return null;
|
|
394
|
+
const extraUsage = parseExtraUsageQuota(windows.root["extra_usage"]);
|
|
395
|
+
const fableWeekly = parseFableWeeklyWindow(windows.root["limits"]);
|
|
396
|
+
const savedResets = parseSavedResets(windows.root["cedar_ember"], observedAtMs);
|
|
397
|
+
return {
|
|
398
|
+
...(savedResets ? { saved_resets: savedResets } : {}),
|
|
399
|
+
success: true,
|
|
400
|
+
...(windows.fiveHour
|
|
401
|
+
? {
|
|
402
|
+
five_hour: windowWithFixedEvidence(windows.fiveHour, FIVE_HOUR_WINDOW_MS, observedAtMs),
|
|
403
|
+
}
|
|
404
|
+
: {}),
|
|
405
|
+
...(windows.sevenDay
|
|
406
|
+
? {
|
|
407
|
+
seven_day: windowWithFixedEvidence(windows.sevenDay, SEVEN_DAY_WINDOW_MS, observedAtMs),
|
|
408
|
+
}
|
|
409
|
+
: {}),
|
|
410
|
+
...(extraUsage ? { extra_usage: extraUsage } : {}),
|
|
411
|
+
...(fableWeekly ? { fable_weekly: fableWeekly } : {}),
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Parses one candidate config directory. `undefined` means the value is absent, which is not an
|
|
416
|
+
* error. `null` means it is present but cannot name a directory (not a string, empty, padded with
|
|
417
|
+
* whitespace, or relative), so the caller must fail closed rather than read another login.
|
|
418
|
+
*/
|
|
419
|
+
export function parseClaudeConfigDirectoryCandidate(value) {
|
|
420
|
+
if (value === undefined)
|
|
421
|
+
return undefined;
|
|
422
|
+
if (typeof value !== "string")
|
|
423
|
+
return null;
|
|
424
|
+
if (!value || value.trim() !== value || !isAbsolute(value))
|
|
425
|
+
return null;
|
|
426
|
+
return resolve(value);
|
|
257
427
|
}
|
|
258
|
-
function
|
|
259
|
-
return
|
|
428
|
+
function isSameConfigDirectory(left, right) {
|
|
429
|
+
return process.platform === "win32" ? left.toLowerCase() === right.toLowerCase() : left === right;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* The directory the Claude CLI reads its login from. A directory the CLI reports is authoritative;
|
|
433
|
+
* otherwise CLAUDE_CONFIG_DIR names it, and with neither set it is the default `~/.claude`. An
|
|
434
|
+
* invalid value, or a reported directory that disagrees with CLAUDE_CONFIG_DIR, is unresolved, so
|
|
435
|
+
* no login is read in its place.
|
|
436
|
+
*/
|
|
437
|
+
function resolveClaudeConfigDirectory(reported, environment) {
|
|
438
|
+
const fromReport = parseClaudeConfigDirectoryCandidate(reported);
|
|
439
|
+
const fromEnvironment = parseClaudeConfigDirectoryCandidate(environment);
|
|
440
|
+
if (fromReport === null || fromEnvironment === null)
|
|
441
|
+
return { state: "unresolved" };
|
|
442
|
+
if (fromReport && fromEnvironment && !isSameConfigDirectory(fromReport, fromEnvironment)) {
|
|
443
|
+
return { state: "unresolved" };
|
|
444
|
+
}
|
|
445
|
+
const defaultDirectory = resolve(join(homedir(), ".claude"));
|
|
446
|
+
const path = fromReport ?? fromEnvironment ?? defaultDirectory;
|
|
447
|
+
return {
|
|
448
|
+
state: "resolved",
|
|
449
|
+
path,
|
|
450
|
+
// An explicit config directory may use a different macOS Keychain service,
|
|
451
|
+
// even when it names the default path; never guess that service's identity.
|
|
452
|
+
isDefault: fromEnvironment === undefined && isSameConfigDirectory(path, defaultDirectory),
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
/** The identity qualifier for a config directory: its resolved path, or a fixed word when none is bound. */
|
|
456
|
+
function claudeConfigDirectoryQualifier(configDirectory) {
|
|
457
|
+
return configDirectory?.state === "resolved" ? configDirectory.path : "unresolved";
|
|
458
|
+
}
|
|
459
|
+
function claudeCredentialsLocation(directory) {
|
|
460
|
+
return directory.isDefault
|
|
461
|
+
? CLAUDE_DEFAULT_CREDENTIALS_LOCATION
|
|
462
|
+
: CLAUDE_CUSTOM_CONFIG_DIRECTORY_LOCATION;
|
|
260
463
|
}
|
|
261
464
|
function getClaudeKeychainLocation() {
|
|
262
465
|
return `macOS Keychain service ${sanitizeDisplayText(CLAUDE_CODE_CREDENTIALS_SERVICE)}`;
|
|
@@ -308,10 +511,9 @@ function parseClaudeCredentialsAccessToken(content, options) {
|
|
|
308
511
|
const accessToken = extractClaudeCredentialsAccessToken(parsed);
|
|
309
512
|
return accessToken ? { accessToken } : { error: "missing" };
|
|
310
513
|
}
|
|
311
|
-
catch
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
};
|
|
514
|
+
catch {
|
|
515
|
+
// The parser's own message can quote a fragment of the token, so only a fixed reason is kept.
|
|
516
|
+
return { error: "invalid JSON" };
|
|
315
517
|
}
|
|
316
518
|
}
|
|
317
519
|
async function runCredentialCommand(file, args) {
|
|
@@ -321,6 +523,7 @@ async function runCredentialCommand(file, args) {
|
|
|
321
523
|
encoding: "utf8",
|
|
322
524
|
timeout: CLAUDE_COMMAND_TIMEOUT_MS,
|
|
323
525
|
maxBuffer: 1024 * 1024,
|
|
526
|
+
windowsHide: true,
|
|
324
527
|
}, (error, stdout, stderr) => {
|
|
325
528
|
const stdoutText = typeof stdout === "string" ? stdout : stdout.toString("utf8");
|
|
326
529
|
const stderrText = typeof stderr === "string" ? stderr : stderr.toString("utf8");
|
|
@@ -380,18 +583,18 @@ async function readClaudeCredentialsAccessTokenFromMacOSKeychain() {
|
|
|
380
583
|
: `Claude OAuth access token missing in ${location}.`,
|
|
381
584
|
};
|
|
382
585
|
}
|
|
383
|
-
async function readClaudeCredentialsAccessTokenFromFile() {
|
|
384
|
-
const
|
|
586
|
+
async function readClaudeCredentialsAccessTokenFromFile(directory) {
|
|
587
|
+
const location = claudeCredentialsLocation(directory);
|
|
385
588
|
try {
|
|
386
|
-
const content = await readFile(
|
|
589
|
+
const content = await readFile(join(directory.path, ".credentials.json"), "utf8");
|
|
387
590
|
const parsed = parseClaudeCredentialsAccessToken(content, { allowPlainText: false });
|
|
388
591
|
const accessToken = parsed.accessToken?.trim() ?? "";
|
|
389
592
|
if (!accessToken) {
|
|
390
593
|
return {
|
|
391
594
|
state: "unavailable",
|
|
392
595
|
detail: parsed.error && parsed.error !== "missing"
|
|
393
|
-
? `Could not parse Claude credentials
|
|
394
|
-
: `Claude OAuth access token missing in ${
|
|
596
|
+
? `Could not parse Claude credentials in ${location}: ${parsed.error}.`
|
|
597
|
+
: `Claude OAuth access token missing in ${location}.`,
|
|
395
598
|
};
|
|
396
599
|
}
|
|
397
600
|
return {
|
|
@@ -406,19 +609,27 @@ async function readClaudeCredentialsAccessTokenFromFile() {
|
|
|
406
609
|
if (code === "ENOENT") {
|
|
407
610
|
return {
|
|
408
611
|
state: "not-found",
|
|
409
|
-
location
|
|
612
|
+
location,
|
|
410
613
|
};
|
|
411
614
|
}
|
|
615
|
+
// Only a system error code is shown: a file system message can name the path it failed on.
|
|
616
|
+
const shownCode = typeof code === "string" && /^E[A-Z0-9]+$/u.test(code) ? code : "unknown";
|
|
412
617
|
return {
|
|
413
618
|
state: "unavailable",
|
|
414
|
-
detail: `Could not read Claude credentials
|
|
619
|
+
detail: `Could not read Claude credentials in ${location}: ${shownCode} error.`,
|
|
415
620
|
};
|
|
416
621
|
}
|
|
417
622
|
}
|
|
418
|
-
async function readClaudeOAuthAccessToken() {
|
|
623
|
+
async function readClaudeOAuthAccessToken(configDirectory) {
|
|
624
|
+
if (configDirectory.state !== "resolved") {
|
|
625
|
+
return { state: "unavailable", detail: CLAUDE_CONFIG_DIRECTORY_UNRESOLVED_DETAIL };
|
|
626
|
+
}
|
|
419
627
|
const locationsChecked = [];
|
|
420
628
|
const unavailableDetails = [];
|
|
421
|
-
|
|
629
|
+
// Only the default directory has a macOS Keychain entry, so a custom directory never reads it.
|
|
630
|
+
const keychainCredentials = configDirectory.isDefault
|
|
631
|
+
? await readClaudeCredentialsAccessTokenFromMacOSKeychain()
|
|
632
|
+
: null;
|
|
422
633
|
if (keychainCredentials?.state === "configured") {
|
|
423
634
|
return { ...keychainCredentials, source: "claude-credentials" };
|
|
424
635
|
}
|
|
@@ -428,7 +639,7 @@ async function readClaudeOAuthAccessToken() {
|
|
|
428
639
|
if (keychainCredentials?.state === "not-found") {
|
|
429
640
|
locationsChecked.push(keychainCredentials.location);
|
|
430
641
|
}
|
|
431
|
-
const fileCredentials = await readClaudeCredentialsAccessTokenFromFile();
|
|
642
|
+
const fileCredentials = await readClaudeCredentialsAccessTokenFromFile(configDirectory);
|
|
432
643
|
if (fileCredentials.state === "configured") {
|
|
433
644
|
return { ...fileCredentials, source: "claude-credentials" };
|
|
434
645
|
}
|
|
@@ -443,20 +654,17 @@ async function readClaudeOAuthAccessToken() {
|
|
|
443
654
|
detail: unavailableDetails[0] ?? getClaudeCredentialsNotFoundDetail(locationsChecked),
|
|
444
655
|
};
|
|
445
656
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
};
|
|
657
|
+
/**
|
|
658
|
+
* The Claude CLI's stored subscription token. It is read only when the CLI positively reports a
|
|
659
|
+
* first-party claude.ai login, because an absent or non-JSON status cannot prove whose token it is.
|
|
660
|
+
*/
|
|
661
|
+
async function readClaudeCliSubscriptionToken(localDiagnostics) {
|
|
662
|
+
if (localDiagnostics.claudeAiLoginVerified !== true) {
|
|
663
|
+
return { state: "unavailable", detail: CLAUDE_CLI_TOKEN_UNVERIFIED_DETAIL };
|
|
454
664
|
}
|
|
455
|
-
return
|
|
456
|
-
? await readClaudeOAuthAccessToken()
|
|
457
|
-
: { state: "unavailable" };
|
|
665
|
+
return await readClaudeOAuthAccessToken(localDiagnostics.configDirectory ?? { state: "unresolved" });
|
|
458
666
|
}
|
|
459
|
-
function fingerprintAccessToken(accessToken) {
|
|
667
|
+
export function fingerprintAccessToken(accessToken) {
|
|
460
668
|
return createHash("sha256").update(accessToken).digest("hex");
|
|
461
669
|
}
|
|
462
670
|
function parseRetryAfterMs(value, nowMs) {
|
|
@@ -493,6 +701,15 @@ function retainAnthropicOAuthCooldownForToken(tokenFingerprint) {
|
|
|
493
701
|
anthropicOAuthCooldowns.delete(fingerprint);
|
|
494
702
|
}
|
|
495
703
|
}
|
|
704
|
+
function rememberAnthropicOAuthQuota(tokenFingerprint, quota) {
|
|
705
|
+
const nowMs = Date.now();
|
|
706
|
+
for (const [fingerprint, entry] of anthropicOAuthRecent) {
|
|
707
|
+
if (nowMs - entry.checkedAtMs >= ANTHROPIC_OAUTH_REUSE_MS) {
|
|
708
|
+
anthropicOAuthRecent.delete(fingerprint);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
anthropicOAuthRecent.set(tokenFingerprint, { quota, checkedAtMs: nowMs });
|
|
712
|
+
}
|
|
496
713
|
function redactAccessTokenFromSanitizedDetail(detail, accessToken) {
|
|
497
714
|
const sanitizedDetail = sanitizeDisplayText(detail);
|
|
498
715
|
const sanitizedAccessToken = sanitizeDisplayText(accessToken);
|
|
@@ -507,24 +724,54 @@ function sanitizeAnthropicApiDetail(detail, accessToken) {
|
|
|
507
724
|
function sanitizeAnthropicRequestError(detail, accessToken) {
|
|
508
725
|
return redactAccessTokenFromSanitizedDetail(detail, accessToken);
|
|
509
726
|
}
|
|
510
|
-
|
|
727
|
+
/** The usage endpoint rejects unrecognized clients, so identify as the Claude CLI that owns the login. */
|
|
728
|
+
function claudeCodeUserAgent(cliVersion, enhanced) {
|
|
729
|
+
const version = (cliVersion ?? "").replace(/[^0-9A-Za-z.+-]/gu, "").slice(0, 48);
|
|
730
|
+
if (enhanced)
|
|
731
|
+
return version ? `claude-cli/${version} (external, cli)` : "claude-cli (external, cli)";
|
|
732
|
+
// The plain fallback keeps the identity the usage probe has always sent.
|
|
733
|
+
return version ? `claude-code/${version}` : "claude-code";
|
|
734
|
+
}
|
|
735
|
+
const RETRY_WITHOUT_ENHANCED = Symbol("retry usage request without cedar_ember");
|
|
736
|
+
/** Enhanced request, then one plain retry only when the enhanced form itself was rejected. */
|
|
737
|
+
async function performAnthropicOAuthUsageRequest(accessToken, tokenFingerprint, requestTimeoutMs, cliVersion) {
|
|
738
|
+
const startedAtMs = Date.now();
|
|
739
|
+
const timeoutMs = requestTimeoutMs ?? REQUEST_TIMEOUT_MS;
|
|
740
|
+
const enhanced = await requestAnthropicOAuthUsage(accessToken, tokenFingerprint, requestTimeoutMs, cliVersion, true);
|
|
741
|
+
if (enhanced !== RETRY_WITHOUT_ENHANCED)
|
|
742
|
+
return enhanced;
|
|
743
|
+
// The fallback shares the caller's one timeout budget rather than doubling it.
|
|
744
|
+
const remainingMs = timeoutMs - (Date.now() - startedAtMs);
|
|
745
|
+
if (remainingMs <= 0) {
|
|
746
|
+
return {
|
|
747
|
+
state: "unavailable",
|
|
748
|
+
detail: "Anthropic OAuth usage request timed out",
|
|
749
|
+
failureCategory: "provider_api",
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
// The plain request never yields the retry sentinel.
|
|
753
|
+
return (await requestAnthropicOAuthUsage(accessToken, tokenFingerprint, remainingMs, cliVersion, false));
|
|
754
|
+
}
|
|
755
|
+
async function requestAnthropicOAuthUsage(accessToken, tokenFingerprint, requestTimeoutMs, cliVersion, enhanced) {
|
|
511
756
|
try {
|
|
512
|
-
return await fetchWithTimeout(ANTHROPIC_USAGE_URL, {
|
|
757
|
+
return await fetchWithTimeout(enhanced ? ANTHROPIC_USAGE_ENHANCED_URL : ANTHROPIC_USAGE_URL, {
|
|
513
758
|
request: {
|
|
514
759
|
headers: {
|
|
515
760
|
Authorization: `Bearer ${accessToken}`,
|
|
516
761
|
"anthropic-beta": ANTHROPIC_BETA_HEADER,
|
|
762
|
+
"User-Agent": claudeCodeUserAgent(cliVersion, enhanced),
|
|
517
763
|
},
|
|
518
764
|
},
|
|
519
765
|
timeoutMs: requestTimeoutMs,
|
|
520
766
|
consume: async (response, timeoutSignal) => {
|
|
521
767
|
if (response.status === 429) {
|
|
768
|
+
anthropicOAuthRecent.delete(tokenFingerprint);
|
|
522
769
|
const nowMs = Date.now();
|
|
523
770
|
const previousFailureCount = anthropicOAuthCooldowns.get(tokenFingerprint)?.failureCount ?? 0;
|
|
524
771
|
const durationMs = getAnthropicOAuthCooldownDurationMs(previousFailureCount, response.headers?.get?.("Retry-After") ?? null, nowMs);
|
|
525
772
|
anthropicOAuthCooldowns.set(tokenFingerprint, {
|
|
526
773
|
failureCount: previousFailureCount + 1,
|
|
527
|
-
blockedUntilMs: nowMs + durationMs,
|
|
774
|
+
blockedUntilMs: Math.ceil(nowMs + durationMs),
|
|
528
775
|
});
|
|
529
776
|
let detail = "";
|
|
530
777
|
try {
|
|
@@ -541,6 +788,8 @@ async function performAnthropicOAuthUsageRequest(accessToken, tokenFingerprint,
|
|
|
541
788
|
return {
|
|
542
789
|
state: "unavailable",
|
|
543
790
|
detail: `${errorDetail} ${getAnthropicOAuthCooldownMessage(durationMs)}`,
|
|
791
|
+
failureCategory: "provider_api",
|
|
792
|
+
retryAtMs: Math.ceil(nowMs + durationMs),
|
|
544
793
|
};
|
|
545
794
|
}
|
|
546
795
|
anthropicOAuthCooldowns.delete(tokenFingerprint);
|
|
@@ -548,7 +797,14 @@ async function performAnthropicOAuthUsageRequest(accessToken, tokenFingerprint,
|
|
|
548
797
|
const authenticationFailure = response.status === 401 || response.status === 403;
|
|
549
798
|
let detail = "";
|
|
550
799
|
try {
|
|
551
|
-
|
|
800
|
+
const body = await response.text();
|
|
801
|
+
// A 403 naming a missing scope or user:profile is a real credential problem, never an enhanced-form rejection.
|
|
802
|
+
if (enhanced &&
|
|
803
|
+
(response.status === 400 ||
|
|
804
|
+
(response.status === 403 && !/scope|user:profile/iu.test(body)))) {
|
|
805
|
+
return RETRY_WITHOUT_ENHANCED;
|
|
806
|
+
}
|
|
807
|
+
detail = sanitizeAnthropicApiDetail(body, accessToken);
|
|
552
808
|
}
|
|
553
809
|
catch (error) {
|
|
554
810
|
if (timeoutSignal.aborted && !authenticationFailure)
|
|
@@ -561,6 +817,7 @@ async function performAnthropicOAuthUsageRequest(accessToken, tokenFingerprint,
|
|
|
561
817
|
? `Anthropic API error ${response.status}: ${detail}`
|
|
562
818
|
: `Anthropic API returned ${response.status}`,
|
|
563
819
|
...(authenticationFailure ? { failureKind: "authentication" } : {}),
|
|
820
|
+
failureCategory: authenticationFailure ? "credentials" : "provider_api",
|
|
564
821
|
};
|
|
565
822
|
}
|
|
566
823
|
let data;
|
|
@@ -573,6 +830,7 @@ async function performAnthropicOAuthUsageRequest(accessToken, tokenFingerprint,
|
|
|
573
830
|
return {
|
|
574
831
|
state: "unavailable",
|
|
575
832
|
detail: "Failed to parse Anthropic quota response",
|
|
833
|
+
failureCategory: "provider_api",
|
|
576
834
|
};
|
|
577
835
|
}
|
|
578
836
|
const quota = parseOAuthUsageResponse(data);
|
|
@@ -580,6 +838,7 @@ async function performAnthropicOAuthUsageRequest(accessToken, tokenFingerprint,
|
|
|
580
838
|
return {
|
|
581
839
|
state: "unavailable",
|
|
582
840
|
detail: "Unexpected Anthropic quota response shape",
|
|
841
|
+
failureCategory: "provider_api",
|
|
583
842
|
};
|
|
584
843
|
}
|
|
585
844
|
return {
|
|
@@ -593,10 +852,11 @@ async function performAnthropicOAuthUsageRequest(accessToken, tokenFingerprint,
|
|
|
593
852
|
return {
|
|
594
853
|
state: "unavailable",
|
|
595
854
|
detail: sanitizeAnthropicRequestError(error instanceof Error ? error.message : String(error), accessToken),
|
|
855
|
+
failureCategory: "provider_api",
|
|
596
856
|
};
|
|
597
857
|
}
|
|
598
858
|
}
|
|
599
|
-
async function queryAnthropicQuotaFromOAuthAccessToken(accessToken, requestTimeoutMs) {
|
|
859
|
+
async function queryAnthropicQuotaFromOAuthAccessToken(accessToken, requestTimeoutMs, forcedOperationCache, cliVersion) {
|
|
600
860
|
const tokenFingerprint = fingerprintAccessToken(accessToken);
|
|
601
861
|
retainAnthropicOAuthCooldownForToken(tokenFingerprint);
|
|
602
862
|
const nowMs = Date.now();
|
|
@@ -605,28 +865,91 @@ async function queryAnthropicQuotaFromOAuthAccessToken(accessToken, requestTimeo
|
|
|
605
865
|
return {
|
|
606
866
|
state: "unavailable",
|
|
607
867
|
detail: getAnthropicOAuthCooldownMessage(cooldown.blockedUntilMs - nowMs),
|
|
868
|
+
failureCategory: "provider_api",
|
|
869
|
+
retryAtMs: cooldown.blockedUntilMs,
|
|
608
870
|
};
|
|
609
871
|
}
|
|
610
|
-
const
|
|
611
|
-
if (
|
|
612
|
-
|
|
872
|
+
const recent = forcedOperationCache ? undefined : anthropicOAuthRecent.get(tokenFingerprint);
|
|
873
|
+
if (recent &&
|
|
874
|
+
nowMs >= recent.checkedAtMs &&
|
|
875
|
+
nowMs - recent.checkedAtMs < ANTHROPIC_OAUTH_REUSE_MS) {
|
|
876
|
+
return { state: "success", quota: recent.quota };
|
|
877
|
+
}
|
|
878
|
+
let forcedResults;
|
|
879
|
+
if (forcedOperationCache) {
|
|
880
|
+
forcedResults = forcedOperationCache.get(anthropicForcedResultsKey);
|
|
881
|
+
if (!forcedResults) {
|
|
882
|
+
forcedResults = new Map();
|
|
883
|
+
forcedOperationCache.set(anthropicForcedResultsKey, forcedResults);
|
|
884
|
+
}
|
|
885
|
+
const existing = forcedResults.get(tokenFingerprint);
|
|
886
|
+
if (existing)
|
|
887
|
+
return await existing;
|
|
888
|
+
}
|
|
889
|
+
if (forcedResults) {
|
|
890
|
+
const existingForced = anthropicOAuthForcedInFlight.get(tokenFingerprint);
|
|
891
|
+
if (existingForced) {
|
|
892
|
+
forcedResults.set(tokenFingerprint, existingForced);
|
|
893
|
+
return await existingForced;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
else {
|
|
897
|
+
const existing = anthropicOAuthInFlight.get(tokenFingerprint);
|
|
898
|
+
if (existing)
|
|
899
|
+
return await existing;
|
|
613
900
|
}
|
|
614
|
-
const
|
|
901
|
+
const prior = forcedResults ? anthropicOAuthInFlight.get(tokenFingerprint) : undefined;
|
|
902
|
+
const inFlight = (async () => {
|
|
903
|
+
if (prior)
|
|
904
|
+
await prior;
|
|
905
|
+
const activeCooldown = anthropicOAuthCooldowns.get(tokenFingerprint);
|
|
906
|
+
const remaining = (activeCooldown?.blockedUntilMs ?? 0) - Date.now();
|
|
907
|
+
if (remaining > 0)
|
|
908
|
+
return {
|
|
909
|
+
state: "unavailable",
|
|
910
|
+
detail: getAnthropicOAuthCooldownMessage(remaining),
|
|
911
|
+
failureCategory: "provider_api",
|
|
912
|
+
retryAtMs: activeCooldown?.blockedUntilMs,
|
|
913
|
+
};
|
|
914
|
+
const result = await performAnthropicOAuthUsageRequest(accessToken, tokenFingerprint, requestTimeoutMs, cliVersion);
|
|
915
|
+
if (result.state === "success") {
|
|
916
|
+
rememberAnthropicOAuthQuota(tokenFingerprint, result.quota);
|
|
917
|
+
}
|
|
918
|
+
else {
|
|
919
|
+
const cached = anthropicOAuthRecent.get(tokenFingerprint);
|
|
920
|
+
if (cached?.quota.saved_resets) {
|
|
921
|
+
const { saved_resets: _savedResets, ...quota } = cached.quota;
|
|
922
|
+
anthropicOAuthRecent.set(tokenFingerprint, { ...cached, quota });
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
return result;
|
|
926
|
+
})();
|
|
615
927
|
anthropicOAuthInFlight.set(tokenFingerprint, inFlight);
|
|
928
|
+
if (forcedResults) {
|
|
929
|
+
forcedResults.set(tokenFingerprint, inFlight);
|
|
930
|
+
anthropicOAuthForcedInFlight.set(tokenFingerprint, inFlight);
|
|
931
|
+
}
|
|
616
932
|
try {
|
|
617
933
|
return await inFlight;
|
|
618
934
|
}
|
|
619
935
|
finally {
|
|
620
|
-
if (anthropicOAuthInFlight.get(tokenFingerprint) === inFlight)
|
|
936
|
+
if (anthropicOAuthInFlight.get(tokenFingerprint) === inFlight)
|
|
621
937
|
anthropicOAuthInFlight.delete(tokenFingerprint);
|
|
622
|
-
|
|
938
|
+
if (anthropicOAuthForcedInFlight.get(tokenFingerprint) === inFlight)
|
|
939
|
+
anthropicOAuthForcedInFlight.delete(tokenFingerprint);
|
|
623
940
|
}
|
|
624
941
|
}
|
|
625
|
-
export async function queryAnthropicQuotaWithOAuth(accessToken, requestTimeoutMs) {
|
|
626
|
-
const result = await queryAnthropicQuotaFromOAuthAccessToken(accessToken, requestTimeoutMs);
|
|
942
|
+
export async function queryAnthropicQuotaWithOAuth(accessToken, requestTimeoutMs, forcedOperationCache) {
|
|
943
|
+
const result = await queryAnthropicQuotaFromOAuthAccessToken(accessToken, requestTimeoutMs, forcedOperationCache);
|
|
627
944
|
return result.state === "success"
|
|
628
945
|
? result.quota
|
|
629
|
-
: {
|
|
946
|
+
: {
|
|
947
|
+
success: false,
|
|
948
|
+
error: result.detail ?? "Anthropic OAuth quota unavailable",
|
|
949
|
+
...(result.failureCategory !== undefined
|
|
950
|
+
? { failureCategory: result.failureCategory }
|
|
951
|
+
: {}),
|
|
952
|
+
};
|
|
630
953
|
}
|
|
631
954
|
function extractAuthBoolean(data) {
|
|
632
955
|
const record = asRecord(data);
|
|
@@ -662,6 +985,62 @@ function extractAuthBoolean(data) {
|
|
|
662
985
|
}
|
|
663
986
|
return undefined;
|
|
664
987
|
}
|
|
988
|
+
/**
|
|
989
|
+
* `auth status --json` reports how the login was made. Only a claude.ai subscription login on
|
|
990
|
+
* Anthropic's first-party API is the Claude Code account; any other reported method or provider is
|
|
991
|
+
* stale or unrelated. A field that is absent is not evidence either way.
|
|
992
|
+
*/
|
|
993
|
+
/**
|
|
994
|
+
* Positive evidence that the Claude CLI login is a first-party claude.ai subscription. Both fields
|
|
995
|
+
* must be reported; an absent field proves nothing, so a login without them cannot prove its identity.
|
|
996
|
+
*/
|
|
997
|
+
function hasFirstPartyClaudeAiLogin(payload) {
|
|
998
|
+
const record = asRecord(payload);
|
|
999
|
+
if (!record)
|
|
1000
|
+
return false;
|
|
1001
|
+
const method = record["authMethod"];
|
|
1002
|
+
const provider = record["apiProvider"];
|
|
1003
|
+
return (typeof method === "string" &&
|
|
1004
|
+
method.trim().toLowerCase() === "claude.ai" &&
|
|
1005
|
+
typeof provider === "string" &&
|
|
1006
|
+
provider.trim().toLowerCase() === "firstparty");
|
|
1007
|
+
}
|
|
1008
|
+
/** The only plan names shown. The CLI's `subscriptionType` is undocumented, so anything else is omitted. */
|
|
1009
|
+
const CLAUDE_PLAN_LABELS = new Map([
|
|
1010
|
+
["free", "Free"],
|
|
1011
|
+
["pro", "Pro"],
|
|
1012
|
+
["max", "Max"],
|
|
1013
|
+
["team", "Team"],
|
|
1014
|
+
["enterprise", "Enterprise"],
|
|
1015
|
+
["ultra", "Ultra"],
|
|
1016
|
+
]);
|
|
1017
|
+
/**
|
|
1018
|
+
* Display-only plan name from a verified login's `subscriptionType`; absent, null, or unknown gives none.
|
|
1019
|
+
* Bare name (e.g. "Max"): the card already names the provider, so the header reads `Anthropic · Max`,
|
|
1020
|
+
* the same shape as OpenAI's account header (`OpenAI · Pro`).
|
|
1021
|
+
*/
|
|
1022
|
+
function deriveClaudePlanLabel(payload) {
|
|
1023
|
+
const subscriptionType = asRecord(payload)?.["subscriptionType"];
|
|
1024
|
+
if (typeof subscriptionType !== "string")
|
|
1025
|
+
return undefined;
|
|
1026
|
+
return CLAUDE_PLAN_LABELS.get(subscriptionType.trim().toLowerCase());
|
|
1027
|
+
}
|
|
1028
|
+
function hasNonSubscriptionLogin(payload) {
|
|
1029
|
+
const record = asRecord(payload);
|
|
1030
|
+
if (!record)
|
|
1031
|
+
return false;
|
|
1032
|
+
const method = record["authMethod"];
|
|
1033
|
+
if (method !== undefined && method !== null) {
|
|
1034
|
+
if (typeof method !== "string" || method.trim().toLowerCase() !== "claude.ai")
|
|
1035
|
+
return true;
|
|
1036
|
+
}
|
|
1037
|
+
const provider = record["apiProvider"];
|
|
1038
|
+
if (provider !== undefined && provider !== null) {
|
|
1039
|
+
if (typeof provider !== "string" || provider.trim().toLowerCase() !== "firstparty")
|
|
1040
|
+
return true;
|
|
1041
|
+
}
|
|
1042
|
+
return false;
|
|
1043
|
+
}
|
|
665
1044
|
function hasUnsupportedCommandText(output) {
|
|
666
1045
|
const normalized = output.toLowerCase();
|
|
667
1046
|
return (normalized.includes("unknown command") ||
|
|
@@ -707,6 +1086,8 @@ async function runClaudeCommand(invocation) {
|
|
|
707
1086
|
encoding: "utf8",
|
|
708
1087
|
timeout: CLAUDE_COMMAND_TIMEOUT_MS,
|
|
709
1088
|
maxBuffer: 1024 * 1024,
|
|
1089
|
+
windowsHide: true,
|
|
1090
|
+
...(invocation.windowsVerbatimArguments ? { windowsVerbatimArguments: true } : {}),
|
|
710
1091
|
}, (error, stdout, stderr) => {
|
|
711
1092
|
const stdoutText = typeof stdout === "string" ? stdout : stdout.toString("utf8");
|
|
712
1093
|
const stderrText = typeof stderr === "string" ? stderr : stderr.toString("utf8");
|
|
@@ -756,9 +1137,16 @@ function parseClaudeAuthStatusResult(result) {
|
|
|
756
1137
|
const payload = JSON.parse(trimmed);
|
|
757
1138
|
const auth = extractAuthBoolean(payload);
|
|
758
1139
|
if (auth === true) {
|
|
1140
|
+
if (hasNonSubscriptionLogin(payload)) {
|
|
1141
|
+
return {
|
|
1142
|
+
authStatus: "unknown",
|
|
1143
|
+
message: CLAUDE_NON_SUBSCRIPTION_LOGIN_MESSAGE,
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
759
1146
|
return {
|
|
760
1147
|
authStatus: "authenticated",
|
|
761
1148
|
jsonPayload: payload,
|
|
1149
|
+
claudeAiLoginVerified: hasFirstPartyClaudeAiLogin(payload),
|
|
762
1150
|
};
|
|
763
1151
|
}
|
|
764
1152
|
if (auth === false) {
|
|
@@ -806,6 +1194,9 @@ function mapLocalDiagnosticsToAnthropicDiagnostics(localDiagnostics) {
|
|
|
806
1194
|
binaryPath: localDiagnostics.binaryPath,
|
|
807
1195
|
checkedCommands: localDiagnostics.checkedCommands,
|
|
808
1196
|
quota: localDiagnostics.localQuota,
|
|
1197
|
+
...(localDiagnostics.planLabel === undefined
|
|
1198
|
+
? {}
|
|
1199
|
+
: { planLabel: localDiagnostics.planLabel }),
|
|
809
1200
|
};
|
|
810
1201
|
}
|
|
811
1202
|
const diagnostics = {
|
|
@@ -882,7 +1273,14 @@ async function probeAnthropicLocalDiagnostics(options = {}) {
|
|
|
882
1273
|
message: parsedAuth.message,
|
|
883
1274
|
};
|
|
884
1275
|
}
|
|
1276
|
+
// The CLI's report wins; otherwise CLAUDE_CONFIG_DIR, then the default. The CLI inherits this
|
|
1277
|
+
// environment, so this is the directory it reads its login from.
|
|
1278
|
+
const configDirectory = resolveClaudeConfigDirectory(asRecord(parsedAuth.jsonPayload)?.["configDirectory"], process.env[CLAUDE_CONFIG_DIR_ENV]);
|
|
885
1279
|
const quota = parsedAuth.jsonPayload ? parseUsageResponse(parsedAuth.jsonPayload) : null;
|
|
1280
|
+
// Only a verified first-party login may name its plan; the same login answers the quota above.
|
|
1281
|
+
const planLabel = parsedAuth.claudeAiLoginVerified === true
|
|
1282
|
+
? deriveClaudePlanLabel(parsedAuth.jsonPayload)
|
|
1283
|
+
: undefined;
|
|
886
1284
|
if (quota) {
|
|
887
1285
|
return {
|
|
888
1286
|
installed: true,
|
|
@@ -891,6 +1289,10 @@ async function probeAnthropicLocalDiagnostics(options = {}) {
|
|
|
891
1289
|
binaryPath: binaryPathDisplay,
|
|
892
1290
|
checkedCommands,
|
|
893
1291
|
localQuota: quota,
|
|
1292
|
+
effectiveBinary: binaryPath,
|
|
1293
|
+
configDirectory,
|
|
1294
|
+
claudeAiLoginVerified: parsedAuth.claudeAiLoginVerified === true,
|
|
1295
|
+
...(planLabel === undefined ? {} : { planLabel }),
|
|
894
1296
|
};
|
|
895
1297
|
}
|
|
896
1298
|
return {
|
|
@@ -900,235 +1302,185 @@ async function probeAnthropicLocalDiagnostics(options = {}) {
|
|
|
900
1302
|
binaryPath: binaryPathDisplay,
|
|
901
1303
|
checkedCommands,
|
|
902
1304
|
message: CLAUDE_NO_LOCAL_QUOTA_MESSAGE,
|
|
1305
|
+
effectiveBinary: binaryPath,
|
|
1306
|
+
configDirectory,
|
|
1307
|
+
claudeAiLoginVerified: parsedAuth.claudeAiLoginVerified === true,
|
|
1308
|
+
...(planLabel === undefined ? {} : { planLabel }),
|
|
903
1309
|
};
|
|
904
1310
|
}
|
|
905
1311
|
export function clearAnthropicDiagnosticsCacheForTests() {
|
|
906
|
-
diagnosticsCache.clear();
|
|
907
|
-
localDiagnosticsCache.clear();
|
|
908
1312
|
anthropicOAuthCooldowns.clear();
|
|
909
1313
|
anthropicOAuthInFlight.clear();
|
|
1314
|
+
anthropicOAuthForcedInFlight.clear();
|
|
1315
|
+
anthropicOAuthRecent.clear();
|
|
910
1316
|
}
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
});
|
|
933
|
-
localDiagnosticsCache.set(binaryPath, {
|
|
934
|
-
timestamp: cached.timestamp,
|
|
935
|
-
value: cached.value,
|
|
936
|
-
inFlight,
|
|
937
|
-
});
|
|
1317
|
+
const localDiagnosticsCacheKey = Symbol("anthropic local diagnostics");
|
|
1318
|
+
async function getAnthropicLocalDiagnostics(options = {}) {
|
|
1319
|
+
if (!options.operationCache)
|
|
1320
|
+
return await probeAnthropicLocalDiagnostics(options);
|
|
1321
|
+
let probes = options.operationCache.get(localDiagnosticsCacheKey);
|
|
1322
|
+
if (!probes) {
|
|
1323
|
+
probes = new Map();
|
|
1324
|
+
options.operationCache.set(localDiagnosticsCacheKey, probes);
|
|
1325
|
+
}
|
|
1326
|
+
const key = JSON.stringify([
|
|
1327
|
+
resolveAnthropicBinaryPath(options.binaryPath),
|
|
1328
|
+
process.env[CLAUDE_CONFIG_DIR_ENV],
|
|
1329
|
+
]);
|
|
1330
|
+
let probe = probes.get(key);
|
|
1331
|
+
if (!probe) {
|
|
1332
|
+
probe = probeAnthropicLocalDiagnostics(options);
|
|
1333
|
+
probes.set(key, probe);
|
|
1334
|
+
}
|
|
1335
|
+
return await probe;
|
|
1336
|
+
}
|
|
1337
|
+
async function readSavedResetsForLocalQuota(localDiagnostics, options) {
|
|
938
1338
|
try {
|
|
939
|
-
|
|
1339
|
+
const credentials = await readClaudeCliSubscriptionToken(localDiagnostics);
|
|
1340
|
+
if (credentials.state !== "configured")
|
|
1341
|
+
return undefined;
|
|
1342
|
+
const result = await queryAnthropicQuotaFromOAuthAccessToken(credentials.accessToken, options.requestTimeoutMs, options.forceRefresh ? options.operationCache : undefined, localDiagnostics.version);
|
|
1343
|
+
return result.state === "success" ? result.quota.saved_resets : undefined;
|
|
940
1344
|
}
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
if (latest?.inFlight === inFlight) {
|
|
944
|
-
localDiagnosticsCache.set(binaryPath, {
|
|
945
|
-
timestamp: latest.timestamp,
|
|
946
|
-
value: latest.value,
|
|
947
|
-
});
|
|
948
|
-
}
|
|
1345
|
+
catch {
|
|
1346
|
+
return undefined;
|
|
949
1347
|
}
|
|
950
1348
|
}
|
|
951
1349
|
export async function getAnthropicDiagnostics(options = {}) {
|
|
952
|
-
const
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
1350
|
+
const localDiagnostics = await getAnthropicLocalDiagnostics(options);
|
|
1351
|
+
if (localDiagnostics.localQuota) {
|
|
1352
|
+
// Local quota stays authoritative and needs no token. Saved resets are an optional extra read
|
|
1353
|
+
// with the same verified-login token gate; the result still has no account identity, so it is
|
|
1354
|
+
// never cached across accounts.
|
|
1355
|
+
const mapped = mapLocalDiagnosticsToAnthropicDiagnostics(localDiagnostics);
|
|
1356
|
+
const savedResets = await readSavedResetsForLocalQuota(localDiagnostics, options);
|
|
1357
|
+
return savedResets && mapped.quota
|
|
1358
|
+
? { ...mapped, quota: { ...mapped.quota, saved_resets: savedResets } }
|
|
1359
|
+
: mapped;
|
|
962
1360
|
}
|
|
963
|
-
if (
|
|
964
|
-
return
|
|
1361
|
+
if (localDiagnostics.authStatus !== "authenticated") {
|
|
1362
|
+
return mapLocalDiagnosticsToAnthropicDiagnostics(localDiagnostics);
|
|
965
1363
|
}
|
|
966
|
-
const
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
return mapLocalDiagnosticsToAnthropicDiagnostics(localDiagnostics);
|
|
970
|
-
}
|
|
971
|
-
let credentials = await readAnthropicOAuthAccessToken({
|
|
972
|
-
includeClaudeCredentials: localDiagnostics.authStatus === "authenticated",
|
|
973
|
-
});
|
|
974
|
-
if (credentials.state !== "configured") {
|
|
975
|
-
if (localDiagnostics.authStatus !== "authenticated") {
|
|
976
|
-
return mapLocalDiagnosticsToAnthropicDiagnostics(localDiagnostics);
|
|
977
|
-
}
|
|
978
|
-
const diagnostics = {
|
|
979
|
-
installed: localDiagnostics.installed,
|
|
980
|
-
version: localDiagnostics.version,
|
|
981
|
-
authStatus: localDiagnostics.authStatus,
|
|
982
|
-
quotaSupported: false,
|
|
983
|
-
quotaSource: "none",
|
|
984
|
-
binaryPath: localDiagnostics.binaryPath,
|
|
985
|
-
checkedCommands: localDiagnostics.checkedCommands,
|
|
986
|
-
message: buildAnthropicNoQuotaDiagnosticsMessage(credentials.detail),
|
|
987
|
-
};
|
|
988
|
-
return diagnostics;
|
|
989
|
-
}
|
|
990
|
-
let fallbackQuota = await queryAnthropicQuotaFromOAuthAccessToken(credentials.accessToken, options.requestTimeoutMs);
|
|
991
|
-
if (fallbackQuota.state === "unavailable" &&
|
|
992
|
-
fallbackQuota.failureKind === "authentication" &&
|
|
993
|
-
credentials.source === "opencode-auth" &&
|
|
994
|
-
localDiagnostics.authStatus === "authenticated") {
|
|
995
|
-
const claudeCredentials = await readClaudeOAuthAccessToken();
|
|
996
|
-
if (claudeCredentials.state === "configured") {
|
|
997
|
-
credentials = claudeCredentials;
|
|
998
|
-
fallbackQuota = await queryAnthropicQuotaFromOAuthAccessToken(claudeCredentials.accessToken, options.requestTimeoutMs);
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
if (fallbackQuota.state !== "success") {
|
|
1002
|
-
const diagnostics = {
|
|
1003
|
-
installed: localDiagnostics.installed,
|
|
1004
|
-
version: localDiagnostics.version,
|
|
1005
|
-
authStatus: localDiagnostics.authStatus,
|
|
1006
|
-
quotaSupported: false,
|
|
1007
|
-
quotaSource: "none",
|
|
1008
|
-
oauthCredentialSource: credentials.source,
|
|
1009
|
-
binaryPath: localDiagnostics.binaryPath,
|
|
1010
|
-
checkedCommands: localDiagnostics.checkedCommands,
|
|
1011
|
-
message: buildAnthropicNoQuotaDiagnosticsMessage(fallbackQuota.detail),
|
|
1012
|
-
};
|
|
1013
|
-
return diagnostics;
|
|
1014
|
-
}
|
|
1015
|
-
const diagnostics = {
|
|
1364
|
+
const credentials = await readClaudeCliSubscriptionToken(localDiagnostics);
|
|
1365
|
+
if (credentials.state !== "configured") {
|
|
1366
|
+
return {
|
|
1016
1367
|
installed: localDiagnostics.installed,
|
|
1017
1368
|
version: localDiagnostics.version,
|
|
1018
1369
|
authStatus: localDiagnostics.authStatus,
|
|
1019
|
-
quotaSupported:
|
|
1020
|
-
quotaSource:
|
|
1021
|
-
? "opencode-auth-oauth-api"
|
|
1022
|
-
: "claude-credentials-oauth-api",
|
|
1023
|
-
oauthCredentialSource: credentials.source,
|
|
1370
|
+
quotaSupported: false,
|
|
1371
|
+
quotaSource: "none",
|
|
1024
1372
|
binaryPath: localDiagnostics.binaryPath,
|
|
1025
1373
|
checkedCommands: localDiagnostics.checkedCommands,
|
|
1026
|
-
|
|
1374
|
+
message: buildAnthropicNoQuotaDiagnosticsMessage(credentials.detail),
|
|
1027
1375
|
};
|
|
1028
|
-
return diagnostics;
|
|
1029
|
-
})().then((value) => {
|
|
1030
|
-
diagnosticsCache.set(binaryPath, {
|
|
1031
|
-
timestamp: Date.now(),
|
|
1032
|
-
value,
|
|
1033
|
-
});
|
|
1034
|
-
return value;
|
|
1035
|
-
});
|
|
1036
|
-
diagnosticsCache.set(binaryPath, {
|
|
1037
|
-
timestamp: cached.timestamp,
|
|
1038
|
-
value: cached.value,
|
|
1039
|
-
inFlight,
|
|
1040
|
-
});
|
|
1041
|
-
try {
|
|
1042
|
-
return await inFlight;
|
|
1043
1376
|
}
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1377
|
+
const credentialFingerprint = fingerprintAccessToken(credentials.accessToken);
|
|
1378
|
+
const fallbackQuota = await queryAnthropicQuotaFromOAuthAccessToken(credentials.accessToken, options.requestTimeoutMs, options.forceRefresh ? options.operationCache : undefined, localDiagnostics.version);
|
|
1379
|
+
if (fallbackQuota.state !== "success") {
|
|
1380
|
+
return {
|
|
1381
|
+
installed: localDiagnostics.installed,
|
|
1382
|
+
version: localDiagnostics.version,
|
|
1383
|
+
authStatus: localDiagnostics.authStatus,
|
|
1384
|
+
quotaSupported: false,
|
|
1385
|
+
quotaSource: "none",
|
|
1386
|
+
oauthCredentialSource: credentials.source,
|
|
1387
|
+
binaryPath: localDiagnostics.binaryPath,
|
|
1388
|
+
checkedCommands: localDiagnostics.checkedCommands,
|
|
1389
|
+
message: buildAnthropicNoQuotaDiagnosticsMessage(fallbackQuota.detail),
|
|
1390
|
+
credentialFingerprint,
|
|
1391
|
+
...(fallbackQuota.failureCategory !== undefined
|
|
1392
|
+
? { failureCategory: fallbackQuota.failureCategory }
|
|
1393
|
+
: {}),
|
|
1394
|
+
...(fallbackQuota.retryAtMs !== undefined ? { retryAtMs: fallbackQuota.retryAtMs } : {}),
|
|
1395
|
+
};
|
|
1052
1396
|
}
|
|
1397
|
+
return {
|
|
1398
|
+
installed: localDiagnostics.installed,
|
|
1399
|
+
version: localDiagnostics.version,
|
|
1400
|
+
authStatus: localDiagnostics.authStatus,
|
|
1401
|
+
quotaSupported: true,
|
|
1402
|
+
quotaSource: "claude-credentials-oauth-api",
|
|
1403
|
+
oauthCredentialSource: credentials.source,
|
|
1404
|
+
binaryPath: localDiagnostics.binaryPath,
|
|
1405
|
+
checkedCommands: localDiagnostics.checkedCommands,
|
|
1406
|
+
quota: fallbackQuota.quota,
|
|
1407
|
+
credentialFingerprint,
|
|
1408
|
+
// The plan belongs to the CLI login only when this quota came from that login's own token.
|
|
1409
|
+
...(credentials.source === "claude-credentials" && localDiagnostics.planLabel !== undefined
|
|
1410
|
+
? { planLabel: localDiagnostics.planLabel }
|
|
1411
|
+
: {}),
|
|
1412
|
+
};
|
|
1053
1413
|
}
|
|
1054
1414
|
export async function hasAnthropicCredentialsConfigured(options = {}) {
|
|
1055
1415
|
try {
|
|
1056
|
-
const
|
|
1057
|
-
// A login OpenCode could not return still counts, so it shows as an error.
|
|
1058
|
-
if (opencodeCredentials.state === "configured" || opencodeCredentials.state === "failed") {
|
|
1059
|
-
return true;
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
catch {
|
|
1063
|
-
// Fall back to the existing local Claude CLI probe.
|
|
1064
|
-
}
|
|
1065
|
-
try {
|
|
1066
|
-
const diagnostics = await getCachedAnthropicLocalDiagnostics(options);
|
|
1416
|
+
const diagnostics = await getAnthropicLocalDiagnostics(options);
|
|
1067
1417
|
return diagnostics.installed && diagnostics.authStatus === "authenticated";
|
|
1068
1418
|
}
|
|
1069
1419
|
catch {
|
|
1070
1420
|
return false;
|
|
1071
1421
|
}
|
|
1072
1422
|
}
|
|
1073
|
-
|
|
1423
|
+
/**
|
|
1424
|
+
* Identity of the Claude Agent provider's single Claude CLI login. Null when the CLI reports quota
|
|
1425
|
+
* itself (no account binding) or cannot be probed, so the caller must not cache it.
|
|
1426
|
+
*/
|
|
1427
|
+
export async function resolveClaudeAgentAccountIdentity(params) {
|
|
1074
1428
|
let localDiagnostics;
|
|
1075
1429
|
try {
|
|
1076
|
-
localDiagnostics = await
|
|
1430
|
+
localDiagnostics = await getAnthropicLocalDiagnostics({
|
|
1431
|
+
binaryPath: params.binaryPath,
|
|
1432
|
+
operationCache: params.operationCache,
|
|
1433
|
+
});
|
|
1077
1434
|
}
|
|
1078
1435
|
catch {
|
|
1079
1436
|
return null;
|
|
1080
1437
|
}
|
|
1081
|
-
// Claude CLI quota does not expose an account identity.
|
|
1082
|
-
// path process-local rather than risking reuse after a CLI account switch.
|
|
1438
|
+
// Claude CLI quota does not expose an account identity.
|
|
1083
1439
|
if (localDiagnostics.localQuota)
|
|
1084
1440
|
return null;
|
|
1085
|
-
const
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
:
|
|
1092
|
-
|
|
1093
|
-
return opencodeIdentity;
|
|
1094
|
-
}
|
|
1095
|
-
const claudeCredentials = await readClaudeOAuthAccessToken();
|
|
1096
|
-
const claudeIdentity = claudeCredentials.state === "configured"
|
|
1097
|
-
? await deriveResolvedAuthIdentity({
|
|
1098
|
-
providerId: "anthropic:claude-credentials",
|
|
1099
|
-
principal: { kind: "credential", value: claudeCredentials.accessToken },
|
|
1100
|
-
})
|
|
1101
|
-
: null;
|
|
1102
|
-
if (opencodeIdentity && claudeIdentity) {
|
|
1103
|
-
return composeResolvedAuthIdentities({
|
|
1104
|
-
providerId: "anthropic",
|
|
1105
|
-
identities: [opencodeIdentity, claudeIdentity],
|
|
1106
|
-
});
|
|
1107
|
-
}
|
|
1108
|
-
return opencodeIdentity ?? claudeIdentity;
|
|
1441
|
+
const identity = await claudeCliAccountIdentity(localDiagnostics, params.runtimeDetected);
|
|
1442
|
+
if (!identity)
|
|
1443
|
+
return null;
|
|
1444
|
+
return await composeResolvedAuthIdentities({
|
|
1445
|
+
providerId: "claude-agent",
|
|
1446
|
+
identities: [identity],
|
|
1447
|
+
qualifiers: [params.onlyCurrentModel ? "only-current-model" : "all-models"],
|
|
1448
|
+
});
|
|
1109
1449
|
}
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
return {
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1450
|
+
/**
|
|
1451
|
+
* The Claude CLI row's identity. A readable first-party CLI token keys it, because its quota and its
|
|
1452
|
+
* quota depends on that token. Otherwise the row shows only the CLI's install and sign-in
|
|
1453
|
+
* state, so that state keys it.
|
|
1454
|
+
*/
|
|
1455
|
+
async function claudeCliAccountIdentity(localDiagnostics, claudeCodeRuntimeDetected) {
|
|
1456
|
+
if (localDiagnostics.authStatus === "authenticated") {
|
|
1457
|
+
const credentials = await readClaudeCliSubscriptionToken(localDiagnostics);
|
|
1458
|
+
if (credentials.state === "configured") {
|
|
1459
|
+
return await deriveResolvedAuthIdentity({
|
|
1460
|
+
providerId: "anthropic:claude-cli-account",
|
|
1461
|
+
principal: { kind: "credential", value: credentials.accessToken },
|
|
1462
|
+
qualifiers: [
|
|
1463
|
+
localDiagnostics.effectiveBinary ?? "",
|
|
1464
|
+
claudeConfigDirectoryQualifier(localDiagnostics.configDirectory),
|
|
1465
|
+
// Preserve the existing cache identity for the previously non-excluded CLI login.
|
|
1466
|
+
"distinct",
|
|
1467
|
+
],
|
|
1468
|
+
});
|
|
1123
1469
|
}
|
|
1124
|
-
return null;
|
|
1125
|
-
}
|
|
1126
|
-
catch (err) {
|
|
1127
|
-
return {
|
|
1128
|
-
success: false,
|
|
1129
|
-
error: `Claude CLI probe failed: ${sanitizeDisplayText(err instanceof Error ? err.message : String(err))}`,
|
|
1130
|
-
};
|
|
1131
1470
|
}
|
|
1471
|
+
return await deriveResolvedAuthIdentity({
|
|
1472
|
+
providerId: "anthropic:claude-cli-state",
|
|
1473
|
+
principal: { kind: "stable-id", value: "claude-cli-state" },
|
|
1474
|
+
qualifiers: [
|
|
1475
|
+
localDiagnostics.installed ? "installed" : "missing",
|
|
1476
|
+
localDiagnostics.version ?? "",
|
|
1477
|
+
localDiagnostics.authStatus,
|
|
1478
|
+
localDiagnostics.effectiveBinary ?? "",
|
|
1479
|
+
claudeConfigDirectoryQualifier(localDiagnostics.configDirectory),
|
|
1480
|
+
localDiagnostics.claudeAiLoginVerified === true ? "verified" : "unverified",
|
|
1481
|
+
claudeCodeRuntimeDetected ? "detected" : "undetected",
|
|
1482
|
+
],
|
|
1483
|
+
});
|
|
1132
1484
|
}
|
|
1133
1485
|
export { parseOAuthUsageResponse, parseUsageResponse };
|
|
1134
1486
|
//# sourceMappingURL=anthropic.js.map
|