@nyte-ai/ai 0.0.0-stage → 0.0.18
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/LICENSE +21 -0
- package/README.md +170 -2
- package/THIRD-PARTY-NOTICES.md +800 -0
- package/dist/chunks/account-limits-cache-031vfpt3.js +37 -0
- package/dist/chunks/account-limits-cache-09vs98g6.js +41 -0
- package/dist/chunks/account-limits-cache-0af5gqeg.js +15 -0
- package/dist/chunks/account-limits-cache-0yd6bde8.js +103 -0
- package/dist/chunks/account-limits-cache-1jv43fhh.js +22 -0
- package/dist/chunks/account-limits-cache-3ykqmnj5.js +88 -0
- package/dist/chunks/account-limits-cache-4b8ckwqw.js +293 -0
- package/dist/chunks/account-limits-cache-4fn5w63p.js +295 -0
- package/dist/chunks/account-limits-cache-4hptgd2h.js +49 -0
- package/dist/chunks/account-limits-cache-53e64mhx.js +417 -0
- package/dist/chunks/account-limits-cache-5jdhn0e1.js +562 -0
- package/dist/chunks/account-limits-cache-5rx2pfm5.js +8 -0
- package/dist/chunks/account-limits-cache-5ygxyyss.js +19 -0
- package/dist/chunks/account-limits-cache-6250ykn6.js +21 -0
- package/dist/chunks/account-limits-cache-6x05n06z.js +49 -0
- package/dist/chunks/account-limits-cache-736tdyme.js +6 -0
- package/dist/chunks/account-limits-cache-75smws02.js +213 -0
- package/dist/chunks/account-limits-cache-7z9s8j16.js +139 -0
- package/dist/chunks/account-limits-cache-8137j3zp.js +37 -0
- package/dist/chunks/account-limits-cache-8qzk56vj.js +160 -0
- package/dist/chunks/account-limits-cache-8s8bz4dr.js +36 -0
- package/dist/chunks/account-limits-cache-8vzx8k6r.js +166 -0
- package/dist/chunks/account-limits-cache-8wfqg73s.js +135 -0
- package/dist/chunks/account-limits-cache-90c9c2eb.js +50 -0
- package/dist/chunks/account-limits-cache-9wqk28n5.js +25 -0
- package/dist/chunks/account-limits-cache-a1jyaq00.js +56 -0
- package/dist/chunks/account-limits-cache-a558rjbg.js +138 -0
- package/dist/chunks/account-limits-cache-a55e14fr.js +70 -0
- package/dist/chunks/account-limits-cache-a6cgeagz.js +77 -0
- package/dist/chunks/account-limits-cache-ag5b8dja.js +38 -0
- package/dist/chunks/account-limits-cache-b0x2sfya.js +45 -0
- package/dist/chunks/account-limits-cache-b23sbrke.js +26 -0
- package/dist/chunks/account-limits-cache-b4edaybk.js +8 -0
- package/dist/chunks/account-limits-cache-bsknrh6t.js +74 -0
- package/dist/chunks/account-limits-cache-c7v93krb.js +235 -0
- package/dist/chunks/account-limits-cache-csbxvam7.js +12 -0
- package/dist/chunks/account-limits-cache-cw7q7vfy.js +293 -0
- package/dist/chunks/account-limits-cache-d1ypzqzp.js +27 -0
- package/dist/chunks/account-limits-cache-d2a51dka.js +17 -0
- package/dist/chunks/account-limits-cache-dejeygdt.js +114 -0
- package/dist/chunks/account-limits-cache-eejm0vth.js +11 -0
- package/dist/chunks/account-limits-cache-ehy0ze2b.js +33 -0
- package/dist/chunks/account-limits-cache-emg5b4hj.js +53 -0
- package/dist/chunks/account-limits-cache-eqwweapz.js +6 -0
- package/dist/chunks/account-limits-cache-eyx1jcxc.js +8 -0
- package/dist/chunks/account-limits-cache-f9z00x8c.js +36 -0
- package/dist/chunks/account-limits-cache-fbkaq46e.js +113 -0
- package/dist/chunks/account-limits-cache-fdwad56n.js +399 -0
- package/dist/chunks/account-limits-cache-fht68jp2.js +22 -0
- package/dist/chunks/account-limits-cache-fp03ztsw.js +17 -0
- package/dist/chunks/account-limits-cache-fsktnetm.js +69 -0
- package/dist/chunks/account-limits-cache-fvadc36m.js +110 -0
- package/dist/chunks/account-limits-cache-ghe9ne4j.js +69 -0
- package/dist/chunks/account-limits-cache-gknnm1kv.js +13 -0
- package/dist/chunks/account-limits-cache-gqsrbky9.js +40 -0
- package/dist/chunks/account-limits-cache-gr671dr2.js +0 -0
- package/dist/chunks/account-limits-cache-gs8a3fhw.js +32 -0
- package/dist/chunks/account-limits-cache-gsa5tzck.js +37 -0
- package/dist/chunks/account-limits-cache-h92bzz1j.js +235 -0
- package/dist/chunks/account-limits-cache-hemxtapd.js +142 -0
- package/dist/chunks/account-limits-cache-hj1h48sc.js +92 -0
- package/dist/chunks/account-limits-cache-hj3ek3ty.js +128 -0
- package/dist/chunks/account-limits-cache-hwehc67r.js +509 -0
- package/dist/chunks/account-limits-cache-hyzn1jmk.js +46 -0
- package/dist/chunks/account-limits-cache-j2bt392s.js +63 -0
- package/dist/chunks/account-limits-cache-k0pkzhbx.js +74 -0
- package/dist/chunks/account-limits-cache-kjx405q0.js +277 -0
- package/dist/chunks/account-limits-cache-ks1sjym9.js +37 -0
- package/dist/chunks/account-limits-cache-me1x57bs.js +89 -0
- package/dist/chunks/account-limits-cache-n3yk6g5c.js +314 -0
- package/dist/chunks/account-limits-cache-nbhht7xz.js +72 -0
- package/dist/chunks/account-limits-cache-nbkyhs6v.js +42 -0
- package/dist/chunks/account-limits-cache-nn9jebsj.js +26 -0
- package/dist/chunks/account-limits-cache-nxkprk3g.js +13 -0
- package/dist/chunks/account-limits-cache-nyy770yd.js +38 -0
- package/dist/chunks/account-limits-cache-pfa86y8e.js +45 -0
- package/dist/chunks/account-limits-cache-pfr14mcn.js +59 -0
- package/dist/chunks/account-limits-cache-q1ebtvet.js +8 -0
- package/dist/chunks/account-limits-cache-q63mc41j.js +51 -0
- package/dist/chunks/account-limits-cache-re8hpzwn.js +104 -0
- package/dist/chunks/account-limits-cache-rz8p1g5y.js +9 -0
- package/dist/chunks/account-limits-cache-skbfmzqe.js +7 -0
- package/dist/chunks/account-limits-cache-tc38g0ca.js +20 -0
- package/dist/chunks/account-limits-cache-v4n30az2.js +24 -0
- package/dist/chunks/account-limits-cache-vwgfz727.js +281 -0
- package/dist/chunks/account-limits-cache-x9fvb3s5.js +8 -0
- package/dist/chunks/account-limits-cache-xds88kxd.js +209 -0
- package/dist/chunks/account-limits-cache-xz65wyd1.js +428 -0
- package/dist/chunks/account-limits-cache-ydq8p8qp.js +39 -0
- package/dist/chunks/account-limits-cache-ykbqsvv6.js +134 -0
- package/dist/chunks/account-limits-cache-za42x8a3.js +25 -0
- package/dist/chunks/account-limits-cache-zcbkmrvy.js +79 -0
- package/dist/chunks/account-limits-cache-zgzptd0z.js +62 -0
- package/dist/chunks/account-limits-cache-zn01kscn.js +252 -0
- package/dist/chunks/account-limits-cache-zpjmeyt0.js +113 -0
- package/dist/chunks/account-limits-cache-zqdb4yh4.js +8 -0
- package/dist/src/account-limits-cache.d.ts +16 -0
- package/dist/src/account-limits-cache.js +7 -0
- package/dist/src/api/anthropic-client.d.ts +25 -0
- package/dist/src/api/anthropic-client.js +30 -0
- package/dist/src/api/anthropic-convert.d.ts +17 -0
- package/dist/src/api/anthropic-convert.js +14 -0
- package/dist/src/api/anthropic-events.d.ts +101 -0
- package/dist/src/api/anthropic-events.js +11 -0
- package/dist/src/api/anthropic-messages.d.ts +88 -0
- package/dist/src/api/anthropic-messages.js +578 -0
- package/dist/src/api/anthropic-messages.lazy.d.ts +2 -0
- package/dist/src/api/anthropic-messages.lazy.js +11 -0
- package/dist/src/api/assistant-stream.d.ts +23 -0
- package/dist/src/api/assistant-stream.js +15 -0
- package/dist/src/api/claude-code.d.ts +12 -0
- package/dist/src/api/claude-code.js +12 -0
- package/dist/src/api/cloudflare.d.ts +8 -0
- package/dist/src/api/cloudflare.js +11 -0
- package/dist/src/api/constrained-sampling.d.ts +24 -0
- package/dist/src/api/constrained-sampling.js +16 -0
- package/dist/src/api/deferred.d.ts +7 -0
- package/dist/src/api/deferred.js +10 -0
- package/dist/src/api/github-copilot-headers.d.ts +15 -0
- package/dist/src/api/github-copilot-headers.js +12 -0
- package/dist/src/api/google-generative-ai.d.ts +22 -0
- package/dist/src/api/google-generative-ai.js +583 -0
- package/dist/src/api/google-generative-ai.lazy.d.ts +2 -0
- package/dist/src/api/google-generative-ai.lazy.js +11 -0
- package/dist/src/api/lazy.d.ts +18 -0
- package/dist/src/api/lazy.js +12 -0
- package/dist/src/api/openai-codex-compact.d.ts +14 -0
- package/dist/src/api/openai-codex-compact.js +389 -0
- package/dist/src/api/openai-codex-compaction-error.d.ts +7 -0
- package/dist/src/api/openai-codex-compaction-error.js +6 -0
- package/dist/src/api/openai-codex-protocol.d.ts +125 -0
- package/dist/src/api/openai-codex-protocol.js +48 -0
- package/dist/src/api/openai-codex-responses.d.ts +4 -0
- package/dist/src/api/openai-codex-responses.js +175 -0
- package/dist/src/api/openai-codex-responses.lazy.d.ts +2 -0
- package/dist/src/api/openai-codex-responses.lazy.js +11 -0
- package/dist/src/api/openai-codex-websocket.d.ts +13 -0
- package/dist/src/api/openai-codex-websocket.js +37 -0
- package/dist/src/api/openai-codex-wire.d.ts +44 -0
- package/dist/src/api/openai-codex-wire.js +34 -0
- package/dist/src/api/openai-compact.d.ts +9 -0
- package/dist/src/api/openai-compact.js +12 -0
- package/dist/src/api/openai-completions-convert.d.ts +58 -0
- package/dist/src/api/openai-completions-convert.js +21 -0
- package/dist/src/api/openai-completions-params.d.ts +29 -0
- package/dist/src/api/openai-completions-params.js +20 -0
- package/dist/src/api/openai-completions-reasoning.d.ts +54 -0
- package/dist/src/api/openai-completions-reasoning.js +22 -0
- package/dist/src/api/openai-completions.d.ts +22 -0
- package/dist/src/api/openai-completions.js +484 -0
- package/dist/src/api/openai-completions.lazy.d.ts +5 -0
- package/dist/src/api/openai-completions.lazy.js +11 -0
- package/dist/src/api/openai-prompt-cache.d.ts +1 -0
- package/dist/src/api/openai-prompt-cache.js +6 -0
- package/dist/src/api/openai-responses-convert.d.ts +58 -0
- package/dist/src/api/openai-responses-convert.js +16 -0
- package/dist/src/api/openai-responses-events.d.ts +379 -0
- package/dist/src/api/openai-responses-events.js +12 -0
- package/dist/src/api/openai-responses-stream.d.ts +36 -0
- package/dist/src/api/openai-responses-stream.js +25 -0
- package/dist/src/api/openai-responses.d.ts +18 -0
- package/dist/src/api/openai-responses.js +276 -0
- package/dist/src/api/openai-responses.lazy.d.ts +2 -0
- package/dist/src/api/openai-responses.lazy.js +11 -0
- package/dist/src/api/simple-options.d.ts +17 -0
- package/dist/src/api/simple-options.js +18 -0
- package/dist/src/api/token-count.d.ts +5 -0
- package/dist/src/api/token-count.js +8 -0
- package/dist/src/api/transform-messages.d.ts +9 -0
- package/dist/src/api/transform-messages.js +6 -0
- package/dist/src/api-options.d.ts +27 -0
- package/dist/src/api-options.js +0 -0
- package/dist/src/auth/context.d.ts +11 -0
- package/dist/src/auth/context.js +6 -0
- package/dist/src/auth/credential-store.d.ts +17 -0
- package/dist/src/auth/credential-store.js +7 -0
- package/dist/src/auth/helpers.d.ts +24 -0
- package/dist/src/auth/helpers.js +8 -0
- package/dist/src/auth/oauth/anthropic.d.ts +7 -0
- package/dist/src/auth/oauth/anthropic.js +12 -0
- package/dist/src/auth/oauth/authorization-input.d.ts +14 -0
- package/dist/src/auth/oauth/authorization-input.js +6 -0
- package/dist/src/auth/oauth/device-code.d.ts +29 -0
- package/dist/src/auth/oauth/device-code.js +9 -0
- package/dist/src/auth/oauth/github-copilot.d.ts +23 -0
- package/dist/src/auth/oauth/github-copilot.js +12 -0
- package/dist/src/auth/oauth/load.d.ts +14 -0
- package/dist/src/auth/oauth/load.js +10 -0
- package/dist/src/auth/oauth/oauth-page.d.ts +13 -0
- package/dist/src/auth/oauth/oauth-page.js +8 -0
- package/dist/src/auth/oauth/openai-codex.d.ts +10 -0
- package/dist/src/auth/oauth/openai-codex.js +18 -0
- package/dist/src/auth/oauth/pkce.d.ts +12 -0
- package/dist/src/auth/oauth/pkce.js +6 -0
- package/dist/src/auth/resolve.d.ts +38 -0
- package/dist/src/auth/resolve.js +12 -0
- package/dist/src/auth/store.d.ts +29 -0
- package/dist/src/auth/store.js +10 -0
- package/dist/src/auth/types.d.ts +291 -0
- package/dist/src/auth/types.js +8 -0
- package/dist/src/auth/verify.d.ts +13 -0
- package/dist/src/auth/verify.js +7 -0
- package/dist/src/bun-oauth.d.ts +2 -0
- package/dist/src/bun-oauth.js +25 -0
- package/dist/src/env-api-keys.d.ts +32 -0
- package/dist/src/env-api-keys.js +27 -0
- package/dist/src/file-models-store.d.ts +18 -0
- package/dist/src/file-models-store.js +9 -0
- package/dist/src/index.d.ts +62 -0
- package/dist/src/index.js +372 -0
- package/dist/src/model-capabilities.d.ts +21 -0
- package/dist/src/model-capabilities.js +14 -0
- package/dist/src/model-pricing.d.ts +6 -0
- package/dist/src/model-pricing.js +10 -0
- package/dist/src/model-variants.d.ts +21 -0
- package/dist/src/model-variants.js +19 -0
- package/dist/src/models-refresh.d.ts +27 -0
- package/dist/src/models-refresh.js +9 -0
- package/dist/src/models-store.d.ts +31 -0
- package/dist/src/models-store.js +6 -0
- package/dist/src/models-types.d.ts +206 -0
- package/dist/src/models-types.js +0 -0
- package/dist/src/models.d.ts +7 -0
- package/dist/src/models.js +44 -0
- package/dist/src/prompt-cache.d.ts +5 -0
- package/dist/src/prompt-cache.js +9 -0
- package/dist/src/provider.d.ts +23 -0
- package/dist/src/provider.js +14 -0
- package/dist/src/providers/account-limits.d.ts +13 -0
- package/dist/src/providers/account-limits.js +8 -0
- package/dist/src/providers/anthropic-limits.d.ts +9 -0
- package/dist/src/providers/anthropic-limits.js +10 -0
- package/dist/src/providers/anthropic.d.ts +2 -0
- package/dist/src/providers/anthropic.js +25 -0
- package/dist/src/providers/github-copilot-catalog.d.ts +5 -0
- package/dist/src/providers/github-copilot-catalog.js +7 -0
- package/dist/src/providers/github-copilot.d.ts +2 -0
- package/dist/src/providers/github-copilot.js +23 -0
- package/dist/src/providers/nyte-catalog.d.ts +15 -0
- package/dist/src/providers/nyte-catalog.js +63 -0
- package/dist/src/providers/openai-codex-limits.d.ts +8 -0
- package/dist/src/providers/openai-codex-limits.js +14 -0
- package/dist/src/providers/openai-codex.d.ts +2 -0
- package/dist/src/providers/openai-codex.js +23 -0
- package/dist/src/providers/openai.d.ts +2 -0
- package/dist/src/providers/openai.js +19 -0
- package/dist/src/providers/opencode-go.d.ts +5 -0
- package/dist/src/providers/opencode-go.js +22 -0
- package/dist/src/providers/opencode-headers.d.ts +5 -0
- package/dist/src/providers/opencode-headers.js +6 -0
- package/dist/src/providers/opencode.d.ts +14 -0
- package/dist/src/providers/opencode.js +23 -0
- package/dist/src/providers/openrouter.d.ts +2 -0
- package/dist/src/providers/openrouter.js +19 -0
- package/dist/src/providers/vercel-ai-gateway.d.ts +3 -0
- package/dist/src/providers/vercel-ai-gateway.js +19 -0
- package/dist/src/session-resources.d.ts +9 -0
- package/dist/src/session-resources.js +10 -0
- package/dist/src/types.d.ts +210 -0
- package/dist/src/types.js +1 -0
- package/dist/src/utils/abort.d.ts +12 -0
- package/dist/src/utils/abort.js +10 -0
- package/dist/src/utils/deferred-tools.d.ts +11 -0
- package/dist/src/utils/deferred-tools.js +6 -0
- package/dist/src/utils/diagnostics.d.ts +11 -0
- package/dist/src/utils/diagnostics.js +12 -0
- package/dist/src/utils/error-body.d.ts +26 -0
- package/dist/src/utils/error-body.js +10 -0
- package/dist/src/utils/estimate.d.ts +20 -0
- package/dist/src/utils/estimate.js +14 -0
- package/dist/src/utils/event-stream.d.ts +22 -0
- package/dist/src/utils/event-stream.js +11 -0
- package/dist/src/utils/failure.d.ts +49 -0
- package/dist/src/utils/failure.js +10 -0
- package/dist/src/utils/fifo-queue.d.ts +11 -0
- package/dist/src/utils/fifo-queue.js +6 -0
- package/dist/src/utils/hash.d.ts +5 -0
- package/dist/src/utils/hash.js +6 -0
- package/dist/src/utils/headers.d.ts +12 -0
- package/dist/src/utils/headers.js +14 -0
- package/dist/src/utils/json-parse.d.ts +33 -0
- package/dist/src/utils/json-parse.js +12 -0
- package/dist/src/utils/node-http-proxy.d.ts +6 -0
- package/dist/src/utils/node-http-proxy.js +9 -0
- package/dist/src/utils/nyte-home.d.ts +2 -0
- package/dist/src/utils/nyte-home.js +6 -0
- package/dist/src/utils/nyte-user-agent.d.ts +1 -0
- package/dist/src/utils/nyte-user-agent.js +6 -0
- package/dist/src/utils/overflow.d.ts +18 -0
- package/dist/src/utils/overflow.js +9 -0
- package/dist/src/utils/provider-env.d.ts +9 -0
- package/dist/src/utils/provider-env.js +6 -0
- package/dist/src/utils/provider-retry.d.ts +22 -0
- package/dist/src/utils/provider-retry.js +9 -0
- package/dist/src/utils/retry.d.ts +66 -0
- package/dist/src/utils/retry.js +18 -0
- package/dist/src/utils/sanitize-unicode.d.ts +24 -0
- package/dist/src/utils/sanitize-unicode.js +6 -0
- package/dist/src/utils/sse.d.ts +21 -0
- package/dist/src/utils/sse.js +6 -0
- package/dist/src/utils/text.d.ts +5 -0
- package/dist/src/utils/text.js +10 -0
- package/dist/src/utils/transcript.d.ts +5 -0
- package/dist/src/utils/transcript.js +36 -0
- package/dist/src/utils/typebox-helpers.d.ts +19 -0
- package/dist/src/utils/typebox-helpers.js +6 -0
- package/dist/src/utils/uuid.d.ts +2 -0
- package/dist/src/utils/uuid.js +6 -0
- package/dist/src/utils/validation.d.ts +13 -0
- package/dist/src/utils/validation.js +8 -0
- package/package.json +152 -3
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
claudeCodeVersion2,
|
|
3
|
+
usesClaudeSubscription2
|
|
4
|
+
} from "./account-limits-cache-ks1sjym9.js";
|
|
5
|
+
import {
|
|
6
|
+
limitWindow2,
|
|
7
|
+
fetchAccountUsage2
|
|
8
|
+
} from "./account-limits-cache-ehy0ze2b.js";
|
|
9
|
+
|
|
10
|
+
// src/providers/anthropic-limits.ts
|
|
11
|
+
import { Type } from "typebox";
|
|
12
|
+
import { Value } from "typebox/value";
|
|
13
|
+
var AccountResetSchema = Type.Optional(Type.Union([Type.Number(), Type.String(), Type.Null()]));
|
|
14
|
+
var ClaudeUsageResponseSchema = Type.Record(Type.String(), Type.Unknown());
|
|
15
|
+
var ClaudeUsageWindowSchema = Type.Object({
|
|
16
|
+
utilization: Type.Number(),
|
|
17
|
+
resets_at: AccountResetSchema
|
|
18
|
+
});
|
|
19
|
+
var ClaudeScopedUsageWindowSchema = Type.Object({
|
|
20
|
+
kind: Type.Literal("weekly_scoped"),
|
|
21
|
+
scope: Type.Object({ model: Type.Object({ display_name: Type.String() }) }),
|
|
22
|
+
percent: Type.Number(),
|
|
23
|
+
resets_at: AccountResetSchema
|
|
24
|
+
});
|
|
25
|
+
function resetAtMs(value) {
|
|
26
|
+
if (value === null || value === undefined)
|
|
27
|
+
return;
|
|
28
|
+
if (typeof value === "number") {
|
|
29
|
+
return value < 1000000000000 ? value * 1000 : value;
|
|
30
|
+
}
|
|
31
|
+
const timestamp = Date.parse(value);
|
|
32
|
+
return Number.isFinite(timestamp) ? timestamp : undefined;
|
|
33
|
+
}
|
|
34
|
+
function accountLimitWindow(id, label, percent, reset, windowMinutes) {
|
|
35
|
+
return limitWindow2(id, label, percent, resetAtMs(reset), windowMinutes);
|
|
36
|
+
}
|
|
37
|
+
var CLAUDE_USAGE_WINDOWS = [
|
|
38
|
+
{ id: "five_hour", label: "5-hour", windowMinutes: 5 * 60 },
|
|
39
|
+
{ id: "seven_day", label: "Weekly", windowMinutes: 7 * 24 * 60 },
|
|
40
|
+
{ id: "seven_day_sonnet", label: "Sonnet weekly", windowMinutes: 7 * 24 * 60 },
|
|
41
|
+
{ id: "seven_day_opus", label: "Opus weekly", windowMinutes: 7 * 24 * 60 }
|
|
42
|
+
];
|
|
43
|
+
function claudeHeaderLimits2(headers, providerId) {
|
|
44
|
+
const windows = [];
|
|
45
|
+
for (const [id, label, suffix, windowMinutes] of [
|
|
46
|
+
["five_hour", "5-hour", "5h", 5 * 60],
|
|
47
|
+
["seven_day", "Weekly", "7d", 7 * 24 * 60]
|
|
48
|
+
]) {
|
|
49
|
+
const utilization = Number(headers[`anthropic-ratelimit-unified-${suffix}-utilization`]);
|
|
50
|
+
if (!Number.isFinite(utilization))
|
|
51
|
+
continue;
|
|
52
|
+
const reset = Number(headers[`anthropic-ratelimit-unified-${suffix}-reset`]);
|
|
53
|
+
windows.push(accountLimitWindow(id, label, utilization * 100, Number.isFinite(reset) ? reset : undefined, windowMinutes));
|
|
54
|
+
}
|
|
55
|
+
return windows.length === 0 ? undefined : { providerId, windows, observedAt: Date.now() };
|
|
56
|
+
}
|
|
57
|
+
function claudeUsageWindows(usage) {
|
|
58
|
+
const windows = [];
|
|
59
|
+
for (const definition of CLAUDE_USAGE_WINDOWS) {
|
|
60
|
+
const window = usage[definition.id];
|
|
61
|
+
if (!Value.Check(ClaudeUsageWindowSchema, window))
|
|
62
|
+
continue;
|
|
63
|
+
windows.push(accountLimitWindow(definition.id, definition.label, window.utilization, window.resets_at, definition.windowMinutes));
|
|
64
|
+
}
|
|
65
|
+
const scoped = usage["limits"];
|
|
66
|
+
for (const window of Array.isArray(scoped) ? scoped : []) {
|
|
67
|
+
if (!Value.Check(ClaudeScopedUsageWindowSchema, window))
|
|
68
|
+
continue;
|
|
69
|
+
const name = window.scope.model.display_name;
|
|
70
|
+
if (name.trim() === "")
|
|
71
|
+
continue;
|
|
72
|
+
windows.push(accountLimitWindow(`seven_day_${name}`, `${name} weekly`, window.percent, window.resets_at, 7 * 24 * 60));
|
|
73
|
+
}
|
|
74
|
+
return windows;
|
|
75
|
+
}
|
|
76
|
+
async function fetchAnthropicAccountLimits2(model, options) {
|
|
77
|
+
const apiKey = options?.apiKey;
|
|
78
|
+
if (!apiKey || !usesClaudeSubscription2(apiKey)) {
|
|
79
|
+
throw new Error("Claude usage requires Anthropic OAuth");
|
|
80
|
+
}
|
|
81
|
+
const headers = new Headers({
|
|
82
|
+
Authorization: `Bearer ${apiKey}`,
|
|
83
|
+
"Content-Type": "application/json",
|
|
84
|
+
"User-Agent": `claude-cli/${claudeCodeVersion2}`,
|
|
85
|
+
"anthropic-beta": "oauth-2025-04-20"
|
|
86
|
+
});
|
|
87
|
+
for (const source of [model.headers, options?.headers]) {
|
|
88
|
+
for (const [name, value] of Object.entries(source ?? {})) {
|
|
89
|
+
if (value === null)
|
|
90
|
+
headers.delete(name);
|
|
91
|
+
else
|
|
92
|
+
headers.set(name, value);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const baseUrl = (model.baseUrl?.trim() || "https://api.anthropic.com").replace(/\/+$/, "").replace(/\/v1$/, "");
|
|
96
|
+
const decoded = await fetchAccountUsage2(`${baseUrl}/api/oauth/usage`, headers, options, ClaudeUsageResponseSchema, "Claude");
|
|
97
|
+
return {
|
|
98
|
+
providerId: model.provider,
|
|
99
|
+
windows: claudeUsageWindows(decoded),
|
|
100
|
+
observedAt: Date.now()
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { claudeHeaderLimits2, fetchAnthropicAccountLimits2 };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// src/utils/nyte-home.ts
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import process from "node:process";
|
|
5
|
+
function defaultNyteHome2() {
|
|
6
|
+
return process.env["NYTE_HOME"] ?? join(homedir(), ".nyte");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export { defaultNyteHome2 };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/auth/oauth/pkce.ts
|
|
2
|
+
function base64urlEncode(bytes) {
|
|
3
|
+
let binary = "";
|
|
4
|
+
for (const byte of bytes) {
|
|
5
|
+
binary += String.fromCharCode(byte);
|
|
6
|
+
}
|
|
7
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
8
|
+
}
|
|
9
|
+
async function generatePKCE2() {
|
|
10
|
+
const verifierBytes = new Uint8Array(32);
|
|
11
|
+
crypto.getRandomValues(verifierBytes);
|
|
12
|
+
const verifier = base64urlEncode(verifierBytes);
|
|
13
|
+
const encoder = new TextEncoder;
|
|
14
|
+
const data = encoder.encode(verifier);
|
|
15
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
16
|
+
const challenge = base64urlEncode(new Uint8Array(hashBuffer));
|
|
17
|
+
return { verifier, challenge };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { generatePKCE2 };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/auth/oauth/load.ts
|
|
2
|
+
var registeredFlows = new Map;
|
|
3
|
+
function registerOAuthFlow2(id, flow) {
|
|
4
|
+
registeredFlows.set(id, flow);
|
|
5
|
+
}
|
|
6
|
+
async function importOAuthModule(specifier) {
|
|
7
|
+
try {
|
|
8
|
+
await import(specifier);
|
|
9
|
+
} catch (error) {
|
|
10
|
+
throw new Error(`OAuth flow module ${specifier} is not loadable at runtime. Bundled hosts must register flows up front with registerBunOAuthFlows.`, { cause: error });
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
async function loadOAuthFlow(id, specifier) {
|
|
14
|
+
if (!registeredFlows.has(id))
|
|
15
|
+
await importOAuthModule(specifier);
|
|
16
|
+
const flow = registeredFlows.get(id);
|
|
17
|
+
if (flow === undefined)
|
|
18
|
+
throw new Error(`OAuth flow module ${specifier} did not register its flow`);
|
|
19
|
+
return flow;
|
|
20
|
+
}
|
|
21
|
+
var loadAnthropicOAuth2 = () => loadOAuthFlow("anthropic", "@nyte-ai/ai/auth/oauth/anthropic");
|
|
22
|
+
var loadOpenAICodexOAuth2 = () => loadOAuthFlow("openaiCodex", "@nyte-ai/ai/auth/oauth/openai-codex");
|
|
23
|
+
|
|
24
|
+
export { registerOAuthFlow2, loadAnthropicOAuth2, loadOpenAICodexOAuth2 };
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyServiceTierPricing2
|
|
3
|
+
} from "./account-limits-cache-f9z00x8c.js";
|
|
4
|
+
import {
|
|
5
|
+
CodexFrameJson2,
|
|
6
|
+
readText2,
|
|
7
|
+
readBoolean2,
|
|
8
|
+
readNumber2,
|
|
9
|
+
readRecord2,
|
|
10
|
+
TERMINAL_RESPONSE_TYPES2
|
|
11
|
+
} from "./account-limits-cache-fvadc36m.js";
|
|
12
|
+
import {
|
|
13
|
+
codexFrameLimits2
|
|
14
|
+
} from "./account-limits-cache-hj3ek3ty.js";
|
|
15
|
+
import {
|
|
16
|
+
splitDeferredTools2
|
|
17
|
+
} from "./account-limits-cache-8s8bz4dr.js";
|
|
18
|
+
import {
|
|
19
|
+
formatThrownValue2
|
|
20
|
+
} from "./account-limits-cache-9wqk28n5.js";
|
|
21
|
+
import {
|
|
22
|
+
iterateServerSentEvents2
|
|
23
|
+
} from "./account-limits-cache-me1x57bs.js";
|
|
24
|
+
import {
|
|
25
|
+
getSystemMessageText2
|
|
26
|
+
} from "./account-limits-cache-b4edaybk.js";
|
|
27
|
+
import {
|
|
28
|
+
getDeclaredTools2,
|
|
29
|
+
getInitialSystemMessage2,
|
|
30
|
+
resolveTranscriptTools2
|
|
31
|
+
} from "./account-limits-cache-6250ykn6.js";
|
|
32
|
+
import {
|
|
33
|
+
createGrammarToolInputProperties2
|
|
34
|
+
} from "./account-limits-cache-h92bzz1j.js";
|
|
35
|
+
import {
|
|
36
|
+
convertResponsesMessages2,
|
|
37
|
+
convertResponsesTools2
|
|
38
|
+
} from "./account-limits-cache-n3yk6g5c.js";
|
|
39
|
+
|
|
40
|
+
// src/api/openai-codex-protocol.ts
|
|
41
|
+
import { Type } from "typebox";
|
|
42
|
+
import { Value } from "typebox/value";
|
|
43
|
+
var CodexResponseStatusSchema = Type.Union([
|
|
44
|
+
Type.Literal("completed"),
|
|
45
|
+
Type.Literal("incomplete"),
|
|
46
|
+
Type.Literal("failed"),
|
|
47
|
+
Type.Literal("cancelled"),
|
|
48
|
+
Type.Literal("queued"),
|
|
49
|
+
Type.Literal("in_progress")
|
|
50
|
+
]);
|
|
51
|
+
var DEFAULT_MAX_RETRIES2 = 0;
|
|
52
|
+
var REQUEST_COMPRESSION_ZSTD_LEVEL = 3;
|
|
53
|
+
var CODEX_TOOL_CALL_PROVIDERS2 = new Set([
|
|
54
|
+
"openai",
|
|
55
|
+
"openai-codex",
|
|
56
|
+
"opencode"
|
|
57
|
+
]);
|
|
58
|
+
function compressRequestBodyZstd2(bodyJson) {
|
|
59
|
+
if (typeof process === "undefined" || !(process.versions?.node || process.versions?.bun)) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const zlib = process.getBuiltinModule("node:zlib");
|
|
64
|
+
const compressed = zlib.zstdCompressSync(bodyJson, {
|
|
65
|
+
params: { [zlib.constants.ZSTD_c_compressionLevel]: REQUEST_COMPRESSION_ZSTD_LEVEL }
|
|
66
|
+
});
|
|
67
|
+
return new Uint8Array(compressed);
|
|
68
|
+
} catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function buildRequestBody2(model, context, options, cacheSessionId, grammarToolInputProperties = createGrammarToolInputProperties2(getDeclaredTools2(context.messages), model.compat?.supportsOpenAIGrammarTools ?? false)) {
|
|
73
|
+
const supportsStrictMode = model.compat?.supportsStrictMode ?? true;
|
|
74
|
+
const supportsOpenAIGrammarTools = model.compat?.supportsOpenAIGrammarTools ?? false;
|
|
75
|
+
const deferredToolsMode = model.compat?.supportsAdditionalTools ? "additional-tools" : model.compat?.supportsToolSearch ? "tool-search" : undefined;
|
|
76
|
+
const transcriptTools = resolveTranscriptTools2(context.messages, deferredToolsMode !== undefined);
|
|
77
|
+
const toolPlacement = splitDeferredTools2({ messages: context.messages, tools: transcriptTools.requestTools }, deferredToolsMode !== undefined);
|
|
78
|
+
const messages = convertResponsesMessages2(model, context, CODEX_TOOL_CALL_PROVIDERS2, {
|
|
79
|
+
includeSystemPrompt: false,
|
|
80
|
+
grammarToolInputProperties,
|
|
81
|
+
supportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,
|
|
82
|
+
deferredTools: toolPlacement.deferred,
|
|
83
|
+
deferredToolsMode,
|
|
84
|
+
toolOptions: {
|
|
85
|
+
strict: null,
|
|
86
|
+
supportsStrictMode,
|
|
87
|
+
supportsOpenAIGrammarTools
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
const initialSystemMessage = getInitialSystemMessage2(context.messages);
|
|
91
|
+
const instructions = initialSystemMessage ? getSystemMessageText2(initialSystemMessage) : "";
|
|
92
|
+
const body = {
|
|
93
|
+
model: model.id,
|
|
94
|
+
store: false,
|
|
95
|
+
stream: true,
|
|
96
|
+
instructions: instructions || "You are a helpful assistant.",
|
|
97
|
+
input: messages,
|
|
98
|
+
text: { verbosity: options?.textVerbosity || "low" },
|
|
99
|
+
include: ["reasoning.encrypted_content"],
|
|
100
|
+
prompt_cache_key: cacheSessionId,
|
|
101
|
+
tool_choice: options?.toolChoice ?? "auto",
|
|
102
|
+
parallel_tool_calls: true
|
|
103
|
+
};
|
|
104
|
+
if (options?.temperature !== undefined) {
|
|
105
|
+
body.temperature = options.temperature;
|
|
106
|
+
}
|
|
107
|
+
if (options?.serviceTier !== undefined) {
|
|
108
|
+
body.service_tier = options.serviceTier;
|
|
109
|
+
}
|
|
110
|
+
if (toolPlacement.immediate.length > 0) {
|
|
111
|
+
body.tools = convertResponsesTools2(toolPlacement.immediate, {
|
|
112
|
+
strict: null,
|
|
113
|
+
supportsStrictMode,
|
|
114
|
+
supportsOpenAIGrammarTools
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
if (options?.reasoningEffort !== undefined) {
|
|
118
|
+
const effort = options.reasoningEffort === "none" ? model.thinkingLevelMap?.off === undefined ? "none" : model.thinkingLevelMap.off : model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
119
|
+
if (effort !== null) {
|
|
120
|
+
body.reasoning = {
|
|
121
|
+
effort,
|
|
122
|
+
summary: options.reasoningSummary ?? "auto"
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
} else if (model.reasoning && model.thinkingLevelMap?.off !== null) {
|
|
126
|
+
body.reasoning = { effort: model.thinkingLevelMap?.off ?? "none" };
|
|
127
|
+
}
|
|
128
|
+
return body;
|
|
129
|
+
}
|
|
130
|
+
function resolveCodexServiceTier2(responseServiceTier, requestServiceTier) {
|
|
131
|
+
if (responseServiceTier === "default" && (requestServiceTier === "flex" || requestServiceTier === "priority")) {
|
|
132
|
+
return requestServiceTier;
|
|
133
|
+
}
|
|
134
|
+
return responseServiceTier ?? requestServiceTier;
|
|
135
|
+
}
|
|
136
|
+
function codexStreamOptions2(request) {
|
|
137
|
+
return {
|
|
138
|
+
serviceTier: request.options?.serviceTier,
|
|
139
|
+
grammarToolInputProperties: request.grammarToolInputProperties,
|
|
140
|
+
resolveServiceTier: resolveCodexServiceTier2,
|
|
141
|
+
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing2(usage, serviceTier, request.model)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
class CodexRequestError2 extends Error {
|
|
146
|
+
status;
|
|
147
|
+
headers;
|
|
148
|
+
retryable;
|
|
149
|
+
constructor(message, status, headers, retryable, options) {
|
|
150
|
+
super(message, options);
|
|
151
|
+
this.name = "CodexRequestError";
|
|
152
|
+
this.status = status;
|
|
153
|
+
this.headers = headers;
|
|
154
|
+
this.retryable = retryable;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function retryableCodexRequest2(error) {
|
|
158
|
+
return error instanceof CodexRequestError2 && error.retryable;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
class CodexApiError2 extends Error {
|
|
162
|
+
code;
|
|
163
|
+
payload;
|
|
164
|
+
constructor(message, options) {
|
|
165
|
+
super(message, { cause: options?.cause });
|
|
166
|
+
this.name = "CodexApiError";
|
|
167
|
+
this.code = options?.code;
|
|
168
|
+
this.payload = options?.payload;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
class CodexProtocolError2 extends Error {
|
|
173
|
+
payload;
|
|
174
|
+
constructor(message, options) {
|
|
175
|
+
super(message, { cause: options?.cause });
|
|
176
|
+
this.name = "CodexProtocolError";
|
|
177
|
+
this.payload = options?.payload;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function extractCodexEventError2(event) {
|
|
181
|
+
const nested = readRecord2(event.error);
|
|
182
|
+
return {
|
|
183
|
+
code: readText2(event.code) ?? readText2(nested?.code),
|
|
184
|
+
message: readText2(event.message) ?? readText2(nested?.message)
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
async function reportFrameLimits(frame, providerId, onAccountLimits) {
|
|
188
|
+
const limits = codexFrameLimits2(frame, providerId);
|
|
189
|
+
if (limits !== undefined)
|
|
190
|
+
await onAccountLimits(limits);
|
|
191
|
+
}
|
|
192
|
+
async function* mapCodexEvents2(events, output, onAccountLimits) {
|
|
193
|
+
for await (const event of events) {
|
|
194
|
+
const { type } = event;
|
|
195
|
+
if (type === "codex.rate_limits") {
|
|
196
|
+
if (onAccountLimits !== undefined) {
|
|
197
|
+
await reportFrameLimits(event, output.provider, onAccountLimits).catch(() => {
|
|
198
|
+
return;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (type === "error") {
|
|
204
|
+
const { code, message } = extractCodexEventError2(event);
|
|
205
|
+
throw new CodexApiError2(`Codex error: ${message || code || JSON.stringify(event)}`, {
|
|
206
|
+
code,
|
|
207
|
+
payload: event
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
if (TERMINAL_RESPONSE_TYPES2.has(type)) {
|
|
211
|
+
const response = readRecord2(event.response);
|
|
212
|
+
const endTurn = readBoolean2(response?.end_turn);
|
|
213
|
+
const status = response?.status;
|
|
214
|
+
if (endTurn !== undefined) {
|
|
215
|
+
output.endTurn = endTurn;
|
|
216
|
+
}
|
|
217
|
+
yield {
|
|
218
|
+
...event,
|
|
219
|
+
type: "response.completed",
|
|
220
|
+
response: {
|
|
221
|
+
...response,
|
|
222
|
+
status: type === "response.failed" ? "failed" : Value.Check(CodexResponseStatusSchema, status) ? status : undefined
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
if (type === "response.failed") {
|
|
226
|
+
const failure = readRecord2(response?.error);
|
|
227
|
+
throw new CodexApiError2(readText2(failure?.message) || "Codex response failed", {
|
|
228
|
+
code: readText2(failure?.code),
|
|
229
|
+
payload: event
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
yield event;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
async function* parseSSE2(response, signal, idleTimeoutMs) {
|
|
238
|
+
if (!response.body)
|
|
239
|
+
return;
|
|
240
|
+
for await (const { data } of iterateServerSentEvents2(response.body, signal, idleTimeoutMs)) {
|
|
241
|
+
if (data.trim() === "" || data === "[DONE]")
|
|
242
|
+
continue;
|
|
243
|
+
let frame;
|
|
244
|
+
try {
|
|
245
|
+
frame = JSON.parse(data);
|
|
246
|
+
} catch (cause) {
|
|
247
|
+
throw new CodexProtocolError2(`Invalid Codex SSE JSON: ${formatThrownValue2(cause)}`, {
|
|
248
|
+
cause,
|
|
249
|
+
payload: data
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
if (Value.Check(CodexFrameJson2, frame))
|
|
253
|
+
yield frame;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function parseJsonObject(raw) {
|
|
257
|
+
try {
|
|
258
|
+
return readRecord2(JSON.parse(raw));
|
|
259
|
+
} catch {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function parseErrorResponse2(raw, response) {
|
|
264
|
+
const fallback = raw || response.statusText || "Request failed";
|
|
265
|
+
const err = readRecord2(parseJsonObject(raw)?.error);
|
|
266
|
+
if (!err)
|
|
267
|
+
return { message: fallback };
|
|
268
|
+
const code = readText2(err.code) ?? readText2(err.type) ?? "";
|
|
269
|
+
let friendlyMessage;
|
|
270
|
+
if (/usage_limit_reached|usage_not_included|rate_limit_exceeded/i.test(code) || response.status === 429) {
|
|
271
|
+
const planType = readText2(err.plan_type);
|
|
272
|
+
const plan = planType ? ` (${planType.toLowerCase()} plan)` : "";
|
|
273
|
+
const resetsAt = readNumber2(err.resets_at);
|
|
274
|
+
const mins = resetsAt ? Math.max(0, Math.round((resetsAt * 1000 - Date.now()) / 60000)) : undefined;
|
|
275
|
+
const when = mins !== undefined ? ` Try again in ~${mins} min.` : "";
|
|
276
|
+
friendlyMessage = `You have hit your ChatGPT usage limit${plan}.${when}`.trim();
|
|
277
|
+
}
|
|
278
|
+
return { message: readText2(err.message) || friendlyMessage || fallback, friendlyMessage };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export { DEFAULT_MAX_RETRIES2, CODEX_TOOL_CALL_PROVIDERS2, compressRequestBodyZstd2, buildRequestBody2, resolveCodexServiceTier2, codexStreamOptions2, CodexRequestError2, retryableCodexRequest2, CodexApiError2, CodexProtocolError2, extractCodexEventError2, mapCodexEvents2, parseSSE2, parseErrorResponse2 };
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// src/utils/failure.ts
|
|
2
|
+
var CONTEXT_WINDOW_PATTERNS = [
|
|
3
|
+
/prompt (?:is )?too long/i,
|
|
4
|
+
/prompt exceeds max length/i,
|
|
5
|
+
/request_too_large/i,
|
|
6
|
+
/input is too long for requested model/i,
|
|
7
|
+
/exceeds the context window/i,
|
|
8
|
+
/exceeds (?:the )?(?:model'?s )?maximum context length(?: of [\d,]+ tokens?|\s*\([\d,]+\))/i,
|
|
9
|
+
/input token count.*exceeds the maximum/i,
|
|
10
|
+
/maximum prompt length is \d+/i,
|
|
11
|
+
/reduce the length of the messages/i,
|
|
12
|
+
/maximum context length is \d+ tokens/i,
|
|
13
|
+
/exceeds (?:the )?maximum allowed input length of [\d,]+ tokens?/i,
|
|
14
|
+
/input \(\d+ tokens\) is longer than the model'?s context length \(\d+ tokens\)/i,
|
|
15
|
+
/exceeds the limit of \d+/i,
|
|
16
|
+
/exceeds the available context size/i,
|
|
17
|
+
/greater than the context length/i,
|
|
18
|
+
/context window exceeds limit/i,
|
|
19
|
+
/exceeded model token limit/i,
|
|
20
|
+
/too large for model with \d+ maximum context length/i,
|
|
21
|
+
/prompt has [\d,]+ tokens?, but the configured context size is [\d,]+ tokens?/i,
|
|
22
|
+
/model_context_window_exceeded/i,
|
|
23
|
+
/prompt too long; exceeded (?:max )?context length/i,
|
|
24
|
+
/range of input length should be/i,
|
|
25
|
+
/context[_ ]length[_ ]exceeded/i,
|
|
26
|
+
/too many tokens/i,
|
|
27
|
+
/token limit exceeded/i
|
|
28
|
+
];
|
|
29
|
+
var CEREBRAS_BODYLESS_CONTEXT_WINDOW_PATTERN = /^4(?:00|13)\s*(?:status code)?\s*\(no body\)/i;
|
|
30
|
+
var NOT_CONTEXT_WINDOW_PATTERN = /^(Throttling error|Service unavailable):|rate limit|too many requests/i;
|
|
31
|
+
function pattern(parts) {
|
|
32
|
+
return new RegExp(parts.join("|"), "i");
|
|
33
|
+
}
|
|
34
|
+
var QUOTA_PATTERN = pattern([
|
|
35
|
+
"GoUsageLimitError",
|
|
36
|
+
"FreeUsageLimitError",
|
|
37
|
+
"Monthly usage limit reached",
|
|
38
|
+
"available balance",
|
|
39
|
+
"insufficient[_ -]?quota",
|
|
40
|
+
"out[_ ]of[_ ]credits",
|
|
41
|
+
"out of budget",
|
|
42
|
+
"quota exceeded",
|
|
43
|
+
"billing"
|
|
44
|
+
]);
|
|
45
|
+
function reportsQuotaExhaustion2(text) {
|
|
46
|
+
return QUOTA_PATTERN.test(text);
|
|
47
|
+
}
|
|
48
|
+
var AUTH_PATTERN = pattern([
|
|
49
|
+
"\\b401\\b",
|
|
50
|
+
"unauthori[sz]ed",
|
|
51
|
+
"invalid (?:x-)?api[ _-]?key",
|
|
52
|
+
"authentication"
|
|
53
|
+
]);
|
|
54
|
+
var RATE_LIMIT_PATTERN = pattern([
|
|
55
|
+
"rate[_ .-]?limit",
|
|
56
|
+
"too many requests",
|
|
57
|
+
"throttl",
|
|
58
|
+
"ResourceExhausted",
|
|
59
|
+
"retry delay"
|
|
60
|
+
]);
|
|
61
|
+
var OVERLOADED_PATTERN = pattern([
|
|
62
|
+
"\\b529\\b",
|
|
63
|
+
"\\b5(?:00|02|03|04|20|24)\\b",
|
|
64
|
+
"overload",
|
|
65
|
+
"currently experiencing high demand",
|
|
66
|
+
"model is at capacity",
|
|
67
|
+
"service.?unavailable",
|
|
68
|
+
"temporarily unavailable",
|
|
69
|
+
"server.?error",
|
|
70
|
+
"internal.?error",
|
|
71
|
+
"provider.?returned.?error",
|
|
72
|
+
"exceeded request buffer limit while retrying upstream",
|
|
73
|
+
"you can retry your request",
|
|
74
|
+
"try your request again",
|
|
75
|
+
"please retry your request"
|
|
76
|
+
]);
|
|
77
|
+
var NETWORK_PATTERN = pattern([
|
|
78
|
+
"network",
|
|
79
|
+
"fetch failed",
|
|
80
|
+
"ECONN",
|
|
81
|
+
"connection.?(?:lost|refused|reset|error)",
|
|
82
|
+
"other side closed",
|
|
83
|
+
"getaddrinfo",
|
|
84
|
+
"ENOTFOUND",
|
|
85
|
+
"EAI_AGAIN",
|
|
86
|
+
"upstream.?connect",
|
|
87
|
+
"reset before headers",
|
|
88
|
+
"socket hang up",
|
|
89
|
+
"socket connection was closed",
|
|
90
|
+
"timed? out",
|
|
91
|
+
"timeout",
|
|
92
|
+
"terminated",
|
|
93
|
+
"websocket.?(?:closed|error)",
|
|
94
|
+
"ended without",
|
|
95
|
+
"stream ended before message_stop",
|
|
96
|
+
"stream ended before a terminal response event",
|
|
97
|
+
"http2 request did not get a response"
|
|
98
|
+
]);
|
|
99
|
+
var RETRY_DELAY_PATTERN = /server requested ([0-9]+(?:\.[0-9]+)?)s retry delay(?: \(max: ([0-9]+(?:\.[0-9]+)?)s\))?/i;
|
|
100
|
+
function classForStatus(status) {
|
|
101
|
+
if (status === 429)
|
|
102
|
+
return "rate_limit";
|
|
103
|
+
if (status === 401 || status === 403)
|
|
104
|
+
return "auth";
|
|
105
|
+
if (status === 402)
|
|
106
|
+
return "quota";
|
|
107
|
+
if (status === 413)
|
|
108
|
+
return "context_window";
|
|
109
|
+
if (status >= 500 && status <= 599)
|
|
110
|
+
return "overloaded";
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
function structuredClass(message) {
|
|
114
|
+
for (const diagnostic of message.diagnostics ?? []) {
|
|
115
|
+
for (const value of [
|
|
116
|
+
diagnostic.details?.status,
|
|
117
|
+
diagnostic.details?.statusCode,
|
|
118
|
+
diagnostic.error?.code
|
|
119
|
+
]) {
|
|
120
|
+
const status = typeof value === "number" ? value : typeof value === "string" && /^\d{3}$/u.test(value) ? Number(value) : Number.NaN;
|
|
121
|
+
if (!Number.isInteger(status))
|
|
122
|
+
continue;
|
|
123
|
+
const failureClass = classForStatus(status);
|
|
124
|
+
if (failureClass !== undefined)
|
|
125
|
+
return failureClass;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
function classifyText(text, provider) {
|
|
131
|
+
if (!NOT_CONTEXT_WINDOW_PATTERN.test(text) && (CONTEXT_WINDOW_PATTERNS.some((p) => p.test(text)) || provider === "cerebras" && CEREBRAS_BODYLESS_CONTEXT_WINDOW_PATTERN.test(text))) {
|
|
132
|
+
return "context_window";
|
|
133
|
+
}
|
|
134
|
+
if (reportsQuotaExhaustion2(text))
|
|
135
|
+
return "quota";
|
|
136
|
+
if (RATE_LIMIT_PATTERN.test(text))
|
|
137
|
+
return "rate_limit";
|
|
138
|
+
if (OVERLOADED_PATTERN.test(text))
|
|
139
|
+
return "overloaded";
|
|
140
|
+
if (NETWORK_PATTERN.test(text))
|
|
141
|
+
return "network";
|
|
142
|
+
if (AUTH_PATTERN.test(text))
|
|
143
|
+
return "auth";
|
|
144
|
+
if (/\b429\b/u.test(text))
|
|
145
|
+
return "rate_limit";
|
|
146
|
+
return "provider";
|
|
147
|
+
}
|
|
148
|
+
function retryAfterMs(message) {
|
|
149
|
+
for (const diagnostic of message.diagnostics ?? []) {
|
|
150
|
+
for (const key of ["retryAfterMs", "retryDelayMs"]) {
|
|
151
|
+
const value = diagnostic.details?.[key];
|
|
152
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0)
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const match = message.errorMessage?.match(RETRY_DELAY_PATTERN);
|
|
157
|
+
const seconds = match === null || match === undefined ? Number.NaN : Number(match[1]);
|
|
158
|
+
const maximum = match === null || match === undefined ? Number.NaN : Number(match[2]);
|
|
159
|
+
if (!Number.isFinite(seconds) || seconds < 0)
|
|
160
|
+
return;
|
|
161
|
+
return Number.isFinite(maximum) && maximum > 0 && seconds > maximum ? undefined : seconds * 1000;
|
|
162
|
+
}
|
|
163
|
+
function silentOverflow(message, contextWindow) {
|
|
164
|
+
if (contextWindow === undefined || contextWindow === 0)
|
|
165
|
+
return false;
|
|
166
|
+
const inputTokens = message.usage.input + message.usage.cacheRead;
|
|
167
|
+
if (message.stopReason === "stop")
|
|
168
|
+
return inputTokens > contextWindow;
|
|
169
|
+
return message.stopReason === "length" && message.usage.output === 0 && inputTokens >= contextWindow * 0.99;
|
|
170
|
+
}
|
|
171
|
+
function classifyAssistantFailure2(message, model) {
|
|
172
|
+
if (message.stopReason === "aborted") {
|
|
173
|
+
return { class: "aborted", message: message.errorMessage ?? "Aborted" };
|
|
174
|
+
}
|
|
175
|
+
if (message.stopReason !== "error") {
|
|
176
|
+
return {
|
|
177
|
+
class: silentOverflow(message, model?.contextWindow) ? "context_window" : "provider",
|
|
178
|
+
message: message.errorMessage ?? ""
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
const text = message.errorMessage ?? "Unknown error";
|
|
182
|
+
const failure = {
|
|
183
|
+
class: reportsQuotaExhaustion2(text) ? "quota" : structuredClass(message) ?? classifyText(text, message.provider),
|
|
184
|
+
message: text
|
|
185
|
+
};
|
|
186
|
+
const delay = retryAfterMs(message);
|
|
187
|
+
return delay === undefined ? failure : { ...failure, retryAfterMs: delay };
|
|
188
|
+
}
|
|
189
|
+
function retryCanHelp2(failureClass) {
|
|
190
|
+
switch (failureClass) {
|
|
191
|
+
case "rate_limit":
|
|
192
|
+
case "overloaded":
|
|
193
|
+
case "network":
|
|
194
|
+
return true;
|
|
195
|
+
case "auth":
|
|
196
|
+
case "quota":
|
|
197
|
+
case "context_window":
|
|
198
|
+
case "aborted":
|
|
199
|
+
case "provider":
|
|
200
|
+
case "runner":
|
|
201
|
+
return false;
|
|
202
|
+
default: {
|
|
203
|
+
const _exhaustive = failureClass;
|
|
204
|
+
return _exhaustive;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export { reportsQuotaExhaustion2, classifyAssistantFailure2, retryCanHelp2 };
|