@linxiraos/pi-ai 1.0.0 → 1.0.2
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/CHANGELOG.md +123 -30
- package/package.json +135 -136
- package/src/auth/sqlite-credential-store.ts +0 -56
- package/src/auth-retry.ts +5 -4
- package/src/auth-storage.ts +122 -112
- package/src/dialect/owned-stream.ts +3 -0
- package/src/error/auth-classify.ts +7 -6
- package/src/error/aws.ts +5 -1
- package/src/error/flags.ts +25 -1
- package/src/error/provider.ts +6 -5
- package/src/error/rate-limit.ts +71 -1
- package/src/providers/amazon-bedrock.ts +38 -0
- package/src/providers/anthropic-client.ts +6 -10
- package/src/providers/anthropic.ts +107 -259
- package/src/providers/aws-credentials.ts +222 -29
- package/src/providers/azure-openai-responses.ts +1 -0
- package/src/providers/cursor/exec-modern.ts +1 -0
- package/src/providers/cursor-pi-args.ts +22 -0
- package/src/providers/cursor.ts +81 -1
- package/src/providers/devin.ts +3 -3
- package/src/providers/google-antigravity-forced-tool.md +1 -0
- package/src/providers/google-gemini-cli.ts +83 -40
- package/src/providers/google-shared.ts +22 -8
- package/src/providers/ollama.ts +15 -3
- package/src/providers/openai-codex/request-transformer.ts +47 -21
- package/src/providers/openai-codex-responses.ts +3 -3
- package/src/providers/openai-reasoning-fallback.ts +21 -8
- package/src/providers/openai-responses.ts +77 -12
- package/src/providers/openai-shared.ts +114 -15
- package/src/providers/pi-native-server.ts +1 -0
- package/src/providers/register-builtins.ts +18 -14
- package/src/providers/vision-guard.ts +15 -3
- package/src/registry/amazon-bedrock.ts +9 -14
- package/src/registry/api-key-login.ts +26 -12
- package/src/registry/aws.ts +16 -8
- package/src/registry/cloudflare-ai-gateway.ts +10 -29
- package/src/registry/kagi.ts +11 -29
- package/src/registry/litellm.ts +11 -29
- package/src/registry/llama-cpp.ts +11 -21
- package/src/registry/lm-studio.ts +9 -20
- package/src/registry/oauth/callback-server.ts +93 -5
- package/src/registry/oauth/google-antigravity.ts +99 -44
- package/src/registry/oauth/perplexity.ts +32 -4
- package/src/registry/parallel.ts +10 -28
- package/src/registry/tavily.ts +9 -27
- package/src/registry/together.ts +7 -3
- package/src/registry/vercel-ai-gateway.ts +10 -28
- package/src/registry/vllm.ts +11 -21
- package/src/stream.ts +459 -53
- package/src/types.ts +23 -0
- package/src/usage/alibaba-token-plan.ts +8 -15
- package/src/usage/claude.ts +12 -19
- package/src/usage/cursor.ts +179 -59
- package/src/usage/gemini.ts +4 -3
- package/src/usage/github-copilot.ts +3 -2
- package/src/usage/google-antigravity.ts +10 -17
- package/src/usage/kimi.ts +35 -17
- package/src/usage/minimax-code.ts +7 -27
- package/src/usage/openai-codex-reset.ts +3 -2
- package/src/usage/openai-codex.ts +5 -5
- package/src/usage/opencode-go.ts +170 -60
- package/src/usage/shared.ts +33 -10
- package/src/usage/synthetic.ts +4 -11
- package/src/usage/umans.ts +2 -2
- package/src/usage/xai-oauth.ts +62 -34
- package/src/usage/zai.ts +4 -6
- package/src/usage.ts +0 -20
- package/src/utils/aws-profile.ts +39 -1
- package/src/utils/block-symbols.ts +18 -0
- package/src/utils/empty-completion-retry.ts +4 -1
- package/src/utils/leaked-thinking-stream.ts +3 -0
- package/src/utils/openrouter-headers.ts +4 -4
- package/src/utils/thinking-loop.ts +17 -38
- package/dist/types/api-registry.d.ts +0 -30
- package/dist/types/auth/sqlite-credential-store.d.ts +0 -117
- package/dist/types/auth-broker/client.d.ts +0 -93
- package/dist/types/auth-broker/discover.d.ts +0 -39
- package/dist/types/auth-broker/index.d.ts +0 -7
- package/dist/types/auth-broker/refresher.d.ts +0 -25
- package/dist/types/auth-broker/remote-store.d.ts +0 -136
- package/dist/types/auth-broker/server.d.ts +0 -48
- package/dist/types/auth-broker/snapshot-cache.d.ts +0 -17
- package/dist/types/auth-broker/types.d.ts +0 -152
- package/dist/types/auth-broker/wire-schema-resource.d.ts +0 -53
- package/dist/types/auth-broker/wire-schemas.d.ts +0 -5
- package/dist/types/auth-gateway/http.d.ts +0 -56
- package/dist/types/auth-gateway/index.d.ts +0 -3
- package/dist/types/auth-gateway/server.d.ts +0 -36
- package/dist/types/auth-gateway/types.d.ts +0 -127
- package/dist/types/auth-retry.d.ts +0 -150
- package/dist/types/auth-storage.d.ts +0 -1258
- package/dist/types/dialect/anthropic.d.ts +0 -15
- package/dist/types/dialect/catalog.d.ts +0 -3
- package/dist/types/dialect/coercion.d.ts +0 -23
- package/dist/types/dialect/deepseek.d.ts +0 -14
- package/dist/types/dialect/demotion.d.ts +0 -30
- package/dist/types/dialect/examples.d.ts +0 -18
- package/dist/types/dialect/factory.d.ts +0 -3
- package/dist/types/dialect/fenced-thinking.d.ts +0 -53
- package/dist/types/dialect/gemini.d.ts +0 -17
- package/dist/types/dialect/gemma.d.ts +0 -15
- package/dist/types/dialect/glm.d.ts +0 -9
- package/dist/types/dialect/harmony.d.ts +0 -8
- package/dist/types/dialect/hermes.d.ts +0 -9
- package/dist/types/dialect/history.d.ts +0 -3
- package/dist/types/dialect/index.d.ts +0 -11
- package/dist/types/dialect/inventory.d.ts +0 -9
- package/dist/types/dialect/kimi.d.ts +0 -14
- package/dist/types/dialect/minimax.d.ts +0 -3
- package/dist/types/dialect/owned-stream.d.ts +0 -4
- package/dist/types/dialect/qwen3.d.ts +0 -9
- package/dist/types/dialect/rendering.d.ts +0 -54
- package/dist/types/dialect/thinking.d.ts +0 -6
- package/dist/types/dialect/types.d.ts +0 -68
- package/dist/types/dialect/xml.d.ts +0 -9
- package/dist/types/error/abort.d.ts +0 -14
- package/dist/types/error/auth-classify.d.ts +0 -20
- package/dist/types/error/auth.d.ts +0 -27
- package/dist/types/error/aws.d.ts +0 -27
- package/dist/types/error/classes.d.ts +0 -106
- package/dist/types/error/finalize.d.ts +0 -39
- package/dist/types/error/flags.d.ts +0 -94
- package/dist/types/error/format.d.ts +0 -20
- package/dist/types/error/gateway.d.ts +0 -20
- package/dist/types/error/index.d.ts +0 -14
- package/dist/types/error/oauth.d.ts +0 -43
- package/dist/types/error/provider.d.ts +0 -42
- package/dist/types/error/rate-limit.d.ts +0 -80
- package/dist/types/error/retryable.d.ts +0 -27
- package/dist/types/error/validation.d.ts +0 -32
- package/dist/types/index.d.ts +0 -51
- package/dist/types/provider-details.d.ts +0 -24
- package/dist/types/providers/amazon-bedrock.d.ts +0 -39
- package/dist/types/providers/anthropic-client.d.ts +0 -106
- package/dist/types/providers/anthropic-messages-server-schema.d.ts +0 -937
- package/dist/types/providers/anthropic-messages-server.d.ts +0 -17
- package/dist/types/providers/anthropic-wire.d.ts +0 -345
- package/dist/types/providers/anthropic.d.ts +0 -261
- package/dist/types/providers/aws-credentials.d.ts +0 -48
- package/dist/types/providers/aws-eventstream.d.ts +0 -39
- package/dist/types/providers/aws-sigv4.d.ts +0 -55
- package/dist/types/providers/azure-openai-responses.d.ts +0 -16
- package/dist/types/providers/bedrock-mantle.d.ts +0 -13
- package/dist/types/providers/claude-code-fingerprint.d.ts +0 -19
- package/dist/types/providers/cowork-fetch.d.ts +0 -3
- package/dist/types/providers/cursor/exec-modern.d.ts +0 -98
- package/dist/types/providers/cursor-pi-args.d.ts +0 -105
- package/dist/types/providers/cursor.d.ts +0 -221
- package/dist/types/providers/devin.d.ts +0 -12
- package/dist/types/providers/error-message.d.ts +0 -25
- package/dist/types/providers/github-copilot-headers.d.ts +0 -40
- package/dist/types/providers/gitlab-duo-workflow.d.ts +0 -254
- package/dist/types/providers/gitlab-duo.d.ts +0 -27
- package/dist/types/providers/google-auth.d.ts +0 -24
- package/dist/types/providers/google-gemini-cli.d.ts +0 -120
- package/dist/types/providers/google-shared.d.ts +0 -203
- package/dist/types/providers/google-types.d.ts +0 -155
- package/dist/types/providers/google-vertex.d.ts +0 -7
- package/dist/types/providers/google.d.ts +0 -4
- package/dist/types/providers/grammar.d.ts +0 -1
- package/dist/types/providers/kimi.d.ts +0 -27
- package/dist/types/providers/mock.d.ts +0 -179
- package/dist/types/providers/ollama.d.ts +0 -8
- package/dist/types/providers/openai-anthropic-shim.d.ts +0 -35
- package/dist/types/providers/openai-chat-server-schema.d.ts +0 -1311
- package/dist/types/providers/openai-chat-server.d.ts +0 -16
- package/dist/types/providers/openai-chat-wire.d.ts +0 -669
- package/dist/types/providers/openai-codex/request-transformer.d.ts +0 -109
- package/dist/types/providers/openai-codex/response-handler.d.ts +0 -26
- package/dist/types/providers/openai-codex-responses.d.ts +0 -238
- package/dist/types/providers/openai-completions.d.ts +0 -48
- package/dist/types/providers/openai-reasoning-fallback.d.ts +0 -25
- package/dist/types/providers/openai-responses-server-schema.d.ts +0 -1314
- package/dist/types/providers/openai-responses-server.d.ts +0 -17
- package/dist/types/providers/openai-responses-wire.d.ts +0 -6099
- package/dist/types/providers/openai-responses.d.ts +0 -131
- package/dist/types/providers/openai-shared.d.ts +0 -624
- package/dist/types/providers/pi-native-client.d.ts +0 -13
- package/dist/types/providers/pi-native-server.d.ts +0 -69
- package/dist/types/providers/register-builtins.d.ts +0 -37
- package/dist/types/providers/synthetic.d.ts +0 -26
- package/dist/types/providers/transform-messages.d.ts +0 -32
- package/dist/types/providers/vision-guard.d.ts +0 -20
- package/dist/types/registry/aiand.d.ts +0 -7
- package/dist/types/registry/aimlapi.d.ts +0 -4
- package/dist/types/registry/alibaba-coding-plan.d.ts +0 -8
- package/dist/types/registry/alibaba-token-plan.d.ts +0 -18
- package/dist/types/registry/amazon-bedrock.d.ts +0 -5
- package/dist/types/registry/anthropic.d.ts +0 -10
- package/dist/types/registry/api-key-login.d.ts +0 -42
- package/dist/types/registry/api-key-validation.d.ts +0 -43
- package/dist/types/registry/aws.d.ts +0 -13
- package/dist/types/registry/azure.d.ts +0 -4
- package/dist/types/registry/baseten.d.ts +0 -7
- package/dist/types/registry/bedrock-mantle.d.ts +0 -22
- package/dist/types/registry/cerebras.d.ts +0 -7
- package/dist/types/registry/cloudflare-ai-gateway.d.ts +0 -13
- package/dist/types/registry/coreweave.d.ts +0 -7
- package/dist/types/registry/cursor.d.ts +0 -7
- package/dist/types/registry/deepseek.d.ts +0 -8
- package/dist/types/registry/derived.d.ts +0 -5
- package/dist/types/registry/devin.d.ts +0 -8
- package/dist/types/registry/exa.d.ts +0 -8
- package/dist/types/registry/firepass.d.ts +0 -16
- package/dist/types/registry/fireworks.d.ts +0 -7
- package/dist/types/registry/github-copilot.d.ts +0 -7
- package/dist/types/registry/gitlab-duo-workflow.d.ts +0 -10
- package/dist/types/registry/gitlab-duo.d.ts +0 -9
- package/dist/types/registry/gmi-cloud.d.ts +0 -7
- package/dist/types/registry/google-antigravity.d.ts +0 -9
- package/dist/types/registry/google-gemini-cli.d.ts +0 -9
- package/dist/types/registry/google-vertex.d.ts +0 -5
- package/dist/types/registry/google.d.ts +0 -4
- package/dist/types/registry/groq.d.ts +0 -4
- package/dist/types/registry/huggingface.d.ts +0 -7
- package/dist/types/registry/index.d.ts +0 -4
- package/dist/types/registry/kagi.d.ts +0 -14
- package/dist/types/registry/kilo.d.ts +0 -7
- package/dist/types/registry/kimi-code.d.ts +0 -7
- package/dist/types/registry/litellm.d.ts +0 -13
- package/dist/types/registry/llama-cpp.d.ts +0 -8
- package/dist/types/registry/lm-studio.d.ts +0 -8
- package/dist/types/registry/meta.d.ts +0 -7
- package/dist/types/registry/minimax-code-cn.d.ts +0 -6
- package/dist/types/registry/minimax-code.d.ts +0 -6
- package/dist/types/registry/minimax.d.ts +0 -4
- package/dist/types/registry/mistral.d.ts +0 -4
- package/dist/types/registry/moonshot.d.ts +0 -7
- package/dist/types/registry/nanogpt.d.ts +0 -7
- package/dist/types/registry/novita.d.ts +0 -6
- package/dist/types/registry/nvidia.d.ts +0 -7
- package/dist/types/registry/oauth/anthropic-constants.d.ts +0 -12
- package/dist/types/registry/oauth/anthropic.d.ts +0 -24
- package/dist/types/registry/oauth/callback-server.d.ts +0 -74
- package/dist/types/registry/oauth/cursor.d.ts +0 -16
- package/dist/types/registry/oauth/device-code.d.ts +0 -25
- package/dist/types/registry/oauth/devin.d.ts +0 -5
- package/dist/types/registry/oauth/github-copilot.d.ts +0 -30
- package/dist/types/registry/oauth/gitlab-duo-workflow.d.ts +0 -6
- package/dist/types/registry/oauth/gitlab-duo.d.ts +0 -3
- package/dist/types/registry/oauth/google-antigravity.d.ts +0 -11
- package/dist/types/registry/oauth/google-gemini-cli.d.ts +0 -22
- package/dist/types/registry/oauth/google-oauth-shared.d.ts +0 -56
- package/dist/types/registry/oauth/index.d.ts +0 -45
- package/dist/types/registry/oauth/kimi.d.ts +0 -21
- package/dist/types/registry/oauth/minimax-code.d.ts +0 -27
- package/dist/types/registry/oauth/openai-codex.d.ts +0 -33
- package/dist/types/registry/oauth/opencode.d.ts +0 -18
- package/dist/types/registry/oauth/perplexity.d.ts +0 -9
- package/dist/types/registry/oauth/pkce.d.ts +0 -8
- package/dist/types/registry/oauth/types.d.ts +0 -87
- package/dist/types/registry/oauth/wafer.d.ts +0 -1
- package/dist/types/registry/oauth/xai-oauth.d.ts +0 -46
- package/dist/types/registry/oauth/xiaomi.d.ts +0 -25
- package/dist/types/registry/oauth/zai.d.ts +0 -25
- package/dist/types/registry/ollama-cloud.d.ts +0 -7
- package/dist/types/registry/ollama.d.ts +0 -12
- package/dist/types/registry/openai-codex-device.d.ts +0 -8
- package/dist/types/registry/openai-codex.d.ts +0 -9
- package/dist/types/registry/openai.d.ts +0 -4
- package/dist/types/registry/opencode-go.d.ts +0 -6
- package/dist/types/registry/opencode-zen.d.ts +0 -6
- package/dist/types/registry/openrouter.d.ts +0 -13
- package/dist/types/registry/parallel.d.ts +0 -14
- package/dist/types/registry/perplexity.d.ts +0 -7
- package/dist/types/registry/qianfan.d.ts +0 -7
- package/dist/types/registry/qwen-portal.d.ts +0 -7
- package/dist/types/registry/registry.d.ts +0 -367
- package/dist/types/registry/sakana.d.ts +0 -7
- package/dist/types/registry/siliconflow-cn.d.ts +0 -7
- package/dist/types/registry/siliconflow.d.ts +0 -7
- package/dist/types/registry/synthetic.d.ts +0 -6
- package/dist/types/registry/tavily.d.ts +0 -14
- package/dist/types/registry/together.d.ts +0 -6
- package/dist/types/registry/types.d.ts +0 -75
- package/dist/types/registry/umans.d.ts +0 -7
- package/dist/types/registry/venice.d.ts +0 -13
- package/dist/types/registry/vercel-ai-gateway.d.ts +0 -7
- package/dist/types/registry/vllm.d.ts +0 -7
- package/dist/types/registry/wafer-serverless.d.ts +0 -6
- package/dist/types/registry/xai-oauth.d.ts +0 -7
- package/dist/types/registry/xai.d.ts +0 -7
- package/dist/types/registry/xiaomi-token-plan-ams.d.ts +0 -6
- package/dist/types/registry/xiaomi-token-plan-cn.d.ts +0 -6
- package/dist/types/registry/xiaomi-token-plan-sgp.d.ts +0 -6
- package/dist/types/registry/xiaomi.d.ts +0 -6
- package/dist/types/registry/zai.d.ts +0 -15
- package/dist/types/registry/zenmux.d.ts +0 -7
- package/dist/types/registry/zhipu-coding-plan.d.ts +0 -7
- package/dist/types/stream.d.ts +0 -46
- package/dist/types/types.d.ts +0 -1064
- package/dist/types/usage/alibaba-token-plan.d.ts +0 -3
- package/dist/types/usage/claude.d.ts +0 -4
- package/dist/types/usage/cursor.d.ts +0 -4
- package/dist/types/usage/gemini.d.ts +0 -2
- package/dist/types/usage/github-copilot.d.ts +0 -7
- package/dist/types/usage/google-antigravity.d.ts +0 -15
- package/dist/types/usage/kimi.d.ts +0 -2
- package/dist/types/usage/minimax-code.d.ts +0 -3
- package/dist/types/usage/ollama.d.ts +0 -5
- package/dist/types/usage/openai-codex-base-url.d.ts +0 -18
- package/dist/types/usage/openai-codex-reset.d.ts +0 -88
- package/dist/types/usage/openai-codex.d.ts +0 -10
- package/dist/types/usage/opencode-go.d.ts +0 -2
- package/dist/types/usage/shared.d.ts +0 -1
- package/dist/types/usage/synthetic.d.ts +0 -2
- package/dist/types/usage/umans.d.ts +0 -2
- package/dist/types/usage/xai-oauth.d.ts +0 -12
- package/dist/types/usage/zai.d.ts +0 -3
- package/dist/types/usage.d.ts +0 -527
- package/dist/types/utils/abort.d.ts +0 -25
- package/dist/types/utils/anthropic-auth.d.ts +0 -35
- package/dist/types/utils/aws-profile.d.ts +0 -17
- package/dist/types/utils/block-symbols.d.ts +0 -62
- package/dist/types/utils/deterministic-id.d.ts +0 -16
- package/dist/types/utils/empty-completion-retry.d.ts +0 -21
- package/dist/types/utils/event-stream.d.ts +0 -39
- package/dist/types/utils/foundry.d.ts +0 -1
- package/dist/types/utils/google-validation.d.ts +0 -2
- package/dist/types/utils/harmony-leak.d.ts +0 -135
- package/dist/types/utils/http-inspector.d.ts +0 -49
- package/dist/types/utils/idle-iterator.d.ts +0 -149
- package/dist/types/utils/leaked-thinking-stream.d.ts +0 -33
- package/dist/types/utils/openai-http.d.ts +0 -48
- package/dist/types/utils/openrouter-headers.d.ts +0 -1
- package/dist/types/utils/parse-bind.d.ts +0 -23
- package/dist/types/utils/provider-response.d.ts +0 -3
- package/dist/types/utils/proxy.d.ts +0 -39
- package/dist/types/utils/request-debug.d.ts +0 -29
- package/dist/types/utils/retry-after.d.ts +0 -4
- package/dist/types/utils/retry.d.ts +0 -14
- package/dist/types/utils/schema/adapt.d.ts +0 -24
- package/dist/types/utils/schema/compatibility.d.ts +0 -30
- package/dist/types/utils/schema/dereference.d.ts +0 -11
- package/dist/types/utils/schema/draft.d.ts +0 -10
- package/dist/types/utils/schema/equality.d.ts +0 -4
- package/dist/types/utils/schema/fields.d.ts +0 -54
- package/dist/types/utils/schema/index.d.ts +0 -14
- package/dist/types/utils/schema/json-schema-validator.d.ts +0 -20
- package/dist/types/utils/schema/meta-validator.d.ts +0 -2
- package/dist/types/utils/schema/normalize.d.ts +0 -153
- package/dist/types/utils/schema/spill.d.ts +0 -8
- package/dist/types/utils/schema/stamps.d.ts +0 -17
- package/dist/types/utils/schema/strict-tool-validation.d.ts +0 -16
- package/dist/types/utils/schema/types.d.ts +0 -4
- package/dist/types/utils/schema/typescript.d.ts +0 -24
- package/dist/types/utils/schema/wire.d.ts +0 -52
- package/dist/types/utils/sdk-stream-timeout.d.ts +0 -33
- package/dist/types/utils/sse-debug.d.ts +0 -5
- package/dist/types/utils/stream-markup-healing.d.ts +0 -87
- package/dist/types/utils/thinking-loop.d.ts +0 -102
- package/dist/types/utils/tool-call-loop-guard.d.ts +0 -26
- package/dist/types/utils/tool-choice.d.ts +0 -52
- package/dist/types/utils/validation.d.ts +0 -28
- package/dist/types/utils.d.ts +0 -57
package/src/error/rate-limit.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { extractRetryHint } from "@linxiraos/pi-utils";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Rate limit reason classification and backoff calculation utilities.
|
|
3
5
|
* Ported from opencode-antigravity-auth plugin for consistency.
|
|
@@ -5,6 +7,7 @@
|
|
|
5
7
|
|
|
6
8
|
export type RateLimitReason =
|
|
7
9
|
| "QUOTA_EXHAUSTED"
|
|
10
|
+
| "INSUFFICIENT_G1_CREDITS_BALANCE"
|
|
8
11
|
| "RATE_LIMIT_EXCEEDED"
|
|
9
12
|
| "CONCURRENT_LIMIT"
|
|
10
13
|
| "MODEL_CAPACITY_EXHAUSTED"
|
|
@@ -67,6 +70,66 @@ const CN_TRANSIENT_CAP_PATTERN =
|
|
|
67
70
|
// of rotating through the opaque-429 fallback.
|
|
68
71
|
const CN_THROTTLE_PATTERN = /速率(?:限制|过快)|频率(?:过高|过快)|过于频繁|稍后[重再]试/;
|
|
69
72
|
|
|
73
|
+
const GOOGLE_RPC_ERROR_INFO_TYPE = "type.googleapis.com/google.rpc.ErrorInfo";
|
|
74
|
+
const LONG_RATE_LIMIT_DELAY_MS = 5 * 60 * 1000;
|
|
75
|
+
|
|
76
|
+
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
77
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
78
|
+
? (value as Record<string, unknown>)
|
|
79
|
+
: undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function parseJsonBody(errorMessage: string): Record<string, unknown> | undefined {
|
|
83
|
+
const start = errorMessage.indexOf("{");
|
|
84
|
+
const end = errorMessage.lastIndexOf("}");
|
|
85
|
+
if (start < 0 || end < start) return undefined;
|
|
86
|
+
try {
|
|
87
|
+
const parsed: unknown = JSON.parse(errorMessage.slice(start, end + 1));
|
|
88
|
+
return asRecord(parsed);
|
|
89
|
+
} catch {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Classify structured Google RESOURCE_EXHAUSTED bodies before consulting text.
|
|
96
|
+
* Cloud Code Assist prefixes the JSON with its HTTP error label, so accept an
|
|
97
|
+
* embedded top-level object as well as a raw JSON body.
|
|
98
|
+
*/
|
|
99
|
+
function parseGoogleRpcRateLimitReason(errorMessage: string): RateLimitReason | undefined {
|
|
100
|
+
const body = parseJsonBody(errorMessage);
|
|
101
|
+
const error = asRecord(body?.error);
|
|
102
|
+
if (typeof error?.status !== "string" || error.status.trim().toUpperCase() !== "RESOURCE_EXHAUSTED") {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
if (!Array.isArray(error.details)) return undefined;
|
|
106
|
+
|
|
107
|
+
for (const value of error.details) {
|
|
108
|
+
const detail = asRecord(value);
|
|
109
|
+
if (detail?.["@type"] !== GOOGLE_RPC_ERROR_INFO_TYPE || typeof detail.reason !== "string") continue;
|
|
110
|
+
const reason = detail.reason.trim().toUpperCase();
|
|
111
|
+
switch (reason) {
|
|
112
|
+
case "QUOTA_EXHAUSTED":
|
|
113
|
+
return "QUOTA_EXHAUSTED";
|
|
114
|
+
case "INSUFFICIENT_G1_CREDITS_BALANCE":
|
|
115
|
+
// Keep Google's specific credit-balance reason available to logs
|
|
116
|
+
// and callers while treating it as credential-rotatable below.
|
|
117
|
+
return "INSUFFICIENT_G1_CREDITS_BALANCE";
|
|
118
|
+
case "RATE_LIMIT_EXCEEDED": {
|
|
119
|
+
const retryDelayMs = extractRetryHint(undefined, errorMessage);
|
|
120
|
+
return retryDelayMs !== undefined && retryDelayMs >= LONG_RATE_LIMIT_DELAY_MS
|
|
121
|
+
? "QUOTA_EXHAUSTED"
|
|
122
|
+
: "RATE_LIMIT_EXCEEDED";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function isQuotaExhaustedReason(reason: RateLimitReason): boolean {
|
|
130
|
+
return reason === "QUOTA_EXHAUSTED" || reason === "INSUFFICIENT_G1_CREDITS_BALANCE";
|
|
131
|
+
}
|
|
132
|
+
|
|
70
133
|
/**
|
|
71
134
|
* Classify a rate-limit error message into a reason category.
|
|
72
135
|
* Priority order: explicit details in a resource-exhausted error > QUOTA
|
|
@@ -77,6 +140,8 @@ const CN_THROTTLE_PATTERN = /速率(?:限制|过快)|频率(?:过高|过快)|过
|
|
|
77
140
|
* Explicit details such as "quota exceeded" retain their normal classification.
|
|
78
141
|
*/
|
|
79
142
|
export function parseRateLimitReason(errorMessage: string): RateLimitReason {
|
|
143
|
+
const structuredReason = parseGoogleRpcRateLimitReason(errorMessage);
|
|
144
|
+
if (structuredReason !== undefined) return structuredReason;
|
|
80
145
|
const lowerWithStatus = errorMessage.toLowerCase();
|
|
81
146
|
const lower = lowerWithStatus.replace(RESOURCE_EXHAUSTED_PATTERN, "");
|
|
82
147
|
const hasResourceExhaustedStatus = lower !== lowerWithStatus;
|
|
@@ -162,6 +227,7 @@ export function parseRateLimitReason(errorMessage: string): RateLimitReason {
|
|
|
162
227
|
*/
|
|
163
228
|
export function calculateRateLimitBackoffMs(reason: RateLimitReason): number {
|
|
164
229
|
switch (reason) {
|
|
230
|
+
case "INSUFFICIENT_G1_CREDITS_BALANCE":
|
|
165
231
|
case "QUOTA_EXHAUSTED":
|
|
166
232
|
return QUOTA_EXHAUSTED_BACKOFF_MS;
|
|
167
233
|
case "RATE_LIMIT_EXCEEDED":
|
|
@@ -215,6 +281,8 @@ export function isUsageLimitStatus(status: number | undefined): boolean {
|
|
|
215
281
|
* credentials.
|
|
216
282
|
*/
|
|
217
283
|
export function isUsageLimitOutcome(status: number | undefined, message: string | undefined): boolean {
|
|
284
|
+
const structuredReason = message ? parseGoogleRpcRateLimitReason(message) : undefined;
|
|
285
|
+
if (structuredReason !== undefined) return isQuotaExhaustedReason(structuredReason);
|
|
218
286
|
// Concurrency caps are shed-and-backoff, not credential-rotatable — but only
|
|
219
287
|
// for quota-worded 429 / other statuses. HTTP 402 is categorically an
|
|
220
288
|
// account-billing cap, so a 402 whose body happens to mention concurrency is
|
|
@@ -235,7 +303,7 @@ export function isUsageLimitOutcome(status: number | undefined, message: string
|
|
|
235
303
|
const reason = parseRateLimitReason(message);
|
|
236
304
|
// For the categorical 402 billing cap a concurrency-worded body is still an
|
|
237
305
|
// exhausted cap (rotate); for 429 / other only QUOTA_EXHAUSTED rotates.
|
|
238
|
-
return reason
|
|
306
|
+
return isQuotaExhaustedReason(reason) || (isBillingCapStatus && reason === "CONCURRENT_LIMIT");
|
|
239
307
|
}
|
|
240
308
|
|
|
241
309
|
/**
|
|
@@ -272,6 +340,8 @@ export function isOpaqueStatusBody(message: string): boolean {
|
|
|
272
340
|
* {@link isUsageLimitOutcome} uses it for the account-rotation decision.
|
|
273
341
|
*/
|
|
274
342
|
export function matchesUsageLimitText(errorMessage: string): boolean {
|
|
343
|
+
const structuredReason = parseGoogleRpcRateLimitReason(errorMessage);
|
|
344
|
+
if (structuredReason !== undefined) return isQuotaExhaustedReason(structuredReason);
|
|
275
345
|
return (
|
|
276
346
|
USAGE_LIMIT_PATTERN.test(errorMessage) ||
|
|
277
347
|
(CN_QUOTA_EXHAUSTED_PATTERN.test(errorMessage) && !CN_TRANSIENT_CAP_PATTERN.test(errorMessage)) ||
|
|
@@ -45,6 +45,19 @@ import { decodeEventStream } from "./aws-eventstream";
|
|
|
45
45
|
import { signRequest } from "./aws-sigv4";
|
|
46
46
|
import { transformMessages } from "./transform-messages";
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Headers SigV4 generates for itself. A caller cannot be allowed to supply these:
|
|
50
|
+
* `signRequest` would sign the caller's value but return its own, so the signature
|
|
51
|
+
* would not match what goes on the wire.
|
|
52
|
+
*/
|
|
53
|
+
const SIGNER_OWNED_HEADERS = new Set(["host", "x-amz-date", "x-amz-content-sha256", "x-amz-security-token"]);
|
|
54
|
+
|
|
55
|
+
/** Headers the Bedrock request sets itself; a caller copy in any casing duplicates them. */
|
|
56
|
+
// `content-length` included: the fetch layer recomputes it from the serialized
|
|
57
|
+
// body, so a caller value would be signed but not sent, and AWS rejects the
|
|
58
|
+
// mismatch.
|
|
59
|
+
const BEDROCK_RESERVED_HEADERS = new Set(["content-type", "accept", "authorization", "content-length"]);
|
|
60
|
+
|
|
48
61
|
export type BedrockThinkingDisplay = "summarized" | "omitted";
|
|
49
62
|
|
|
50
63
|
export interface BedrockOptions extends StreamOptions {
|
|
@@ -356,7 +369,32 @@ export const streamBedrock: StreamFunction<"bedrock-converse-stream"> = (
|
|
|
356
369
|
|
|
357
370
|
const bodyText = JSON.stringify(commandInput);
|
|
358
371
|
const body = new TextEncoder().encode(bodyText);
|
|
372
|
+
// Caller headers are merged BEFORE signing, so SigV4 covers them and they
|
|
373
|
+
// reach the wire. Bedrock built its header map from scratch and ignored
|
|
374
|
+
// `options.headers` entirely, so tracing/attribution headers set by a
|
|
375
|
+
// caller (or by a `before_provider_headers` extension) were silently
|
|
376
|
+
// dropped here while working on every other provider. Content-type and
|
|
377
|
+
// accept stay last: the eventstream framing is not the caller's to change.
|
|
378
|
+
//
|
|
379
|
+
// The signer's OWN headers are dropped first, and that is load-bearing:
|
|
380
|
+
// `signRequest` lets a caller value overwrite `host`/`x-amz-*` in the map
|
|
381
|
+
// it signs, but always RETURNS the generated ones, which `requestHeaders`
|
|
382
|
+
// below then puts on the wire. A caller supplying any of them would sign
|
|
383
|
+
// one set of values and send another, and Bedrock would reject every
|
|
384
|
+
// request with a signature mismatch.
|
|
385
|
+
// Lower-cased, and names the request sets itself are dropped. Keeping a
|
|
386
|
+
// caller `Content-Type` beside the fixed `content-type` leaves TWO object
|
|
387
|
+
// keys: SigV4 signs one value while fetch canonicalizes both into a single
|
|
388
|
+
// comma-joined wire header, so AWS validates different bytes than were
|
|
389
|
+
// signed and rejects the request.
|
|
390
|
+
const callerHeaders: Record<string, string> = {};
|
|
391
|
+
for (const [name, value] of Object.entries(options?.headers ?? {})) {
|
|
392
|
+
const field = name.toLowerCase();
|
|
393
|
+
if (SIGNER_OWNED_HEADERS.has(field) || BEDROCK_RESERVED_HEADERS.has(field)) continue;
|
|
394
|
+
callerHeaders[field] = value;
|
|
395
|
+
}
|
|
359
396
|
const baseHeaders: Record<string, string> = {
|
|
397
|
+
...callerHeaders,
|
|
360
398
|
"content-type": "application/json",
|
|
361
399
|
accept: "application/vnd.amazon.eventstream",
|
|
362
400
|
};
|
|
@@ -27,7 +27,7 @@ import { AnthropicApiError, AnthropicConnectionError, AnthropicConnectionTimeout
|
|
|
27
27
|
export { AnthropicApiError, AnthropicConnectionError, AnthropicConnectionTimeoutError };
|
|
28
28
|
|
|
29
29
|
import type { FetchImpl } from "../types";
|
|
30
|
-
import type {
|
|
30
|
+
import type { MessageCreateParams } from "./anthropic-wire";
|
|
31
31
|
|
|
32
32
|
/** Default pre-response timeout, matching the SDK's 10-minute default. */
|
|
33
33
|
const DEFAULT_TIMEOUT_MS = 600_000;
|
|
@@ -173,7 +173,7 @@ export class AnthropicMessages {
|
|
|
173
173
|
this.#path = path;
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
-
create(params:
|
|
176
|
+
create(params: MessageCreateParams, options?: AnthropicRequestOptions): AnthropicApiRequest {
|
|
177
177
|
return this.#client.request(this.#path, params, options);
|
|
178
178
|
}
|
|
179
179
|
}
|
|
@@ -184,8 +184,8 @@ export class AnthropicMessages {
|
|
|
184
184
|
* alternative Messages-API client via `AnthropicOptions.client`.
|
|
185
185
|
*/
|
|
186
186
|
export interface AnthropicMessagesClientLike {
|
|
187
|
-
messages: { create(params:
|
|
188
|
-
beta?: { messages: { create(params:
|
|
187
|
+
messages: { create(params: MessageCreateParams, options?: AnthropicRequestOptions): unknown };
|
|
188
|
+
beta?: { messages: { create(params: MessageCreateParams, options?: AnthropicRequestOptions): unknown } };
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
export class AnthropicMessagesClient implements AnthropicMessagesClientLike {
|
|
@@ -199,7 +199,7 @@ export class AnthropicMessagesClient implements AnthropicMessagesClientLike {
|
|
|
199
199
|
this.beta = { messages: new AnthropicMessages(this, "/v1/messages?beta=true") };
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
request(path: string, params:
|
|
202
|
+
request(path: string, params: MessageCreateParams, options?: AnthropicRequestOptions): AnthropicApiRequest {
|
|
203
203
|
return new AnthropicApiRequest(() => this.#send(path, params, options));
|
|
204
204
|
}
|
|
205
205
|
|
|
@@ -218,11 +218,7 @@ export class AnthropicMessagesClient implements AnthropicMessagesClientLike {
|
|
|
218
218
|
return headers;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
async #send(
|
|
222
|
-
path: string,
|
|
223
|
-
params: MessageCreateParamsStreaming,
|
|
224
|
-
options?: AnthropicRequestOptions,
|
|
225
|
-
): Promise<Response> {
|
|
221
|
+
async #send(path: string, params: MessageCreateParams, options?: AnthropicRequestOptions): Promise<Response> {
|
|
226
222
|
const opts = this.#options;
|
|
227
223
|
const fetchFn: FetchImpl = opts.fetch ?? fetch;
|
|
228
224
|
const callerSignal = options?.signal;
|