@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,509 @@
|
|
|
1
|
+
import {
|
|
2
|
+
registerSessionResourceCleanup2
|
|
3
|
+
} from "./account-limits-cache-gqsrbky9.js";
|
|
4
|
+
import {
|
|
5
|
+
formatThrownValue2,
|
|
6
|
+
createAssistantMessageDiagnostic2,
|
|
7
|
+
appendAssistantMessageDiagnostic2
|
|
8
|
+
} from "./account-limits-cache-9wqk28n5.js";
|
|
9
|
+
import {
|
|
10
|
+
FifoQueue2
|
|
11
|
+
} from "./account-limits-cache-nn9jebsj.js";
|
|
12
|
+
import {
|
|
13
|
+
headersToRecord2
|
|
14
|
+
} from "./account-limits-cache-ag5b8dja.js";
|
|
15
|
+
import {
|
|
16
|
+
resolveHttpProxyUrlForTarget2
|
|
17
|
+
} from "./account-limits-cache-0yd6bde8.js";
|
|
18
|
+
import {
|
|
19
|
+
normalizeContext2
|
|
20
|
+
} from "./account-limits-cache-6250ykn6.js";
|
|
21
|
+
import {
|
|
22
|
+
CodexFrameJson2,
|
|
23
|
+
readText2,
|
|
24
|
+
readBoolean2,
|
|
25
|
+
readNumber2,
|
|
26
|
+
readRecord2,
|
|
27
|
+
TERMINAL_RESPONSE_TYPES2,
|
|
28
|
+
resolveCodexWebSocketUrl2
|
|
29
|
+
} from "./account-limits-cache-fvadc36m.js";
|
|
30
|
+
import {
|
|
31
|
+
convertResponsesMessages2
|
|
32
|
+
} from "./account-limits-cache-n3yk6g5c.js";
|
|
33
|
+
import {
|
|
34
|
+
CODEX_TOOL_CALL_PROVIDERS2,
|
|
35
|
+
codexStreamOptions2,
|
|
36
|
+
CodexApiError2,
|
|
37
|
+
CodexProtocolError2,
|
|
38
|
+
mapCodexEvents2
|
|
39
|
+
} from "./account-limits-cache-vwgfz727.js";
|
|
40
|
+
import {
|
|
41
|
+
ResponsesUsageError2,
|
|
42
|
+
processResponsesStream2
|
|
43
|
+
} from "./account-limits-cache-53e64mhx.js";
|
|
44
|
+
|
|
45
|
+
// src/api/openai-codex-websocket.ts
|
|
46
|
+
import { Value } from "typebox/value";
|
|
47
|
+
var DEFAULT_WEBSOCKET_CONNECT_TIMEOUT_MS = 15000;
|
|
48
|
+
var WEBSOCKET_MESSAGE_TOO_BIG_CLOSE_CODE = 1009;
|
|
49
|
+
var WEBSOCKET_CONNECTION_LIMIT_REACHED_CODE = "websocket_connection_limit_reached";
|
|
50
|
+
var PREVIOUS_RESPONSE_NOT_FOUND_CODE = "previous_response_not_found";
|
|
51
|
+
var SESSION_WEBSOCKET_CACHE_TTL_MS = 5 * 60 * 1000;
|
|
52
|
+
var SESSION_WEBSOCKET_MAX_AGE_MS = 55 * 60 * 1000;
|
|
53
|
+
var WEBSOCKET_OPEN = 1;
|
|
54
|
+
var websocketSessionCache = new Map;
|
|
55
|
+
var websocketSseFallbackSessions = new Set;
|
|
56
|
+
function closeOpenAICodexWebSocketSessions2(sessionId) {
|
|
57
|
+
const closeEntry = (entry) => {
|
|
58
|
+
if (entry.idleTimer)
|
|
59
|
+
clearTimeout(entry.idleTimer);
|
|
60
|
+
closeWebSocketSilently(entry.socket, 1000, "debug_close");
|
|
61
|
+
};
|
|
62
|
+
if (sessionId) {
|
|
63
|
+
for (const entry of websocketSessionCache.get(sessionId)?.values() ?? [])
|
|
64
|
+
closeEntry(entry);
|
|
65
|
+
websocketSessionCache.delete(sessionId);
|
|
66
|
+
websocketSseFallbackSessions.delete(sessionId);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
for (const accountEntries of websocketSessionCache.values()) {
|
|
70
|
+
for (const entry of accountEntries.values())
|
|
71
|
+
closeEntry(entry);
|
|
72
|
+
}
|
|
73
|
+
websocketSessionCache.clear();
|
|
74
|
+
websocketSseFallbackSessions.clear();
|
|
75
|
+
}
|
|
76
|
+
registerSessionResourceCleanup2(closeOpenAICodexWebSocketSessions2);
|
|
77
|
+
|
|
78
|
+
class WebSocketCloseError extends Error {
|
|
79
|
+
code;
|
|
80
|
+
reason;
|
|
81
|
+
wasClean;
|
|
82
|
+
constructor(message, options) {
|
|
83
|
+
super(message);
|
|
84
|
+
this.name = "WebSocketCloseError";
|
|
85
|
+
this.code = options?.code;
|
|
86
|
+
this.reason = options?.reason;
|
|
87
|
+
this.wasClean = options?.wasClean;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function codexErrorCode(cause) {
|
|
91
|
+
return cause instanceof CodexApiError2 ? cause.code : undefined;
|
|
92
|
+
}
|
|
93
|
+
function socketReusable(socket) {
|
|
94
|
+
return socket.readyState === undefined || socket.readyState === WEBSOCKET_OPEN;
|
|
95
|
+
}
|
|
96
|
+
function closeWebSocketSilently(socket, code = 1000, reason = "done") {
|
|
97
|
+
try {
|
|
98
|
+
socket.close(code, reason);
|
|
99
|
+
} catch {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function forgetEntry(sessionId, accountId, entry) {
|
|
104
|
+
const accountEntries = websocketSessionCache.get(sessionId);
|
|
105
|
+
if (accountEntries?.get(accountId) === entry)
|
|
106
|
+
accountEntries.delete(accountId);
|
|
107
|
+
if (accountEntries?.size === 0)
|
|
108
|
+
websocketSessionCache.delete(sessionId);
|
|
109
|
+
}
|
|
110
|
+
function scheduleSessionWebSocketExpiry(sessionId, accountId, entry) {
|
|
111
|
+
if (entry.idleTimer) {
|
|
112
|
+
clearTimeout(entry.idleTimer);
|
|
113
|
+
}
|
|
114
|
+
entry.idleTimer = setTimeout(() => {
|
|
115
|
+
if (entry.busy)
|
|
116
|
+
return;
|
|
117
|
+
closeWebSocketSilently(entry.socket, 1000, "idle_timeout");
|
|
118
|
+
forgetEntry(sessionId, accountId, entry);
|
|
119
|
+
}, SESSION_WEBSOCKET_CACHE_TTL_MS);
|
|
120
|
+
entry.idleTimer.unref?.();
|
|
121
|
+
}
|
|
122
|
+
function openWebSocket(url, headers, env) {
|
|
123
|
+
const WebSocketClass = globalThis.WebSocket;
|
|
124
|
+
if (typeof WebSocketClass !== "function") {
|
|
125
|
+
throw new Error("WebSocket transport is not available in this runtime");
|
|
126
|
+
}
|
|
127
|
+
const init = { headers };
|
|
128
|
+
if (typeof process !== "undefined" && process.versions?.bun) {
|
|
129
|
+
const proxyUrl = resolveHttpProxyUrlForTarget2(url.replace(/^wss:/, "https:").replace(/^ws:/, "http:"), env);
|
|
130
|
+
if (proxyUrl)
|
|
131
|
+
init.proxy = proxyUrl.href;
|
|
132
|
+
}
|
|
133
|
+
const socket = Reflect.construct(WebSocketClass, [url, init]);
|
|
134
|
+
return socket;
|
|
135
|
+
}
|
|
136
|
+
async function connectWebSocket(url, headers, signal, connectTimeoutMs = DEFAULT_WEBSOCKET_CONNECT_TIMEOUT_MS, env) {
|
|
137
|
+
const wsHeaders = headersToRecord2(headers);
|
|
138
|
+
delete wsHeaders["OpenAI-Beta"];
|
|
139
|
+
const socket = openWebSocket(url, wsHeaders, env);
|
|
140
|
+
return new Promise((resolve, reject) => {
|
|
141
|
+
let settled = false;
|
|
142
|
+
let timeout;
|
|
143
|
+
const cleanup = () => {
|
|
144
|
+
if (timeout) {
|
|
145
|
+
clearTimeout(timeout);
|
|
146
|
+
timeout = undefined;
|
|
147
|
+
}
|
|
148
|
+
socket.removeEventListener("open", onOpen);
|
|
149
|
+
socket.removeEventListener("error", onError);
|
|
150
|
+
socket.removeEventListener("close", onClose);
|
|
151
|
+
signal?.removeEventListener("abort", onAbort);
|
|
152
|
+
};
|
|
153
|
+
const fail = (error, closeReason) => {
|
|
154
|
+
if (settled)
|
|
155
|
+
return;
|
|
156
|
+
settled = true;
|
|
157
|
+
cleanup();
|
|
158
|
+
if (closeReason) {
|
|
159
|
+
closeWebSocketSilently(socket, 1000, closeReason);
|
|
160
|
+
}
|
|
161
|
+
reject(error);
|
|
162
|
+
};
|
|
163
|
+
const onOpen = () => {
|
|
164
|
+
if (settled)
|
|
165
|
+
return;
|
|
166
|
+
settled = true;
|
|
167
|
+
cleanup();
|
|
168
|
+
resolve(socket);
|
|
169
|
+
};
|
|
170
|
+
const onError = (event) => {
|
|
171
|
+
fail(extractWebSocketError(event));
|
|
172
|
+
};
|
|
173
|
+
const onClose = (event) => {
|
|
174
|
+
fail(extractWebSocketCloseError(event));
|
|
175
|
+
};
|
|
176
|
+
const onAbort = () => {
|
|
177
|
+
fail(new Error("Request was aborted"), "aborted");
|
|
178
|
+
};
|
|
179
|
+
socket.addEventListener("open", onOpen);
|
|
180
|
+
socket.addEventListener("error", onError);
|
|
181
|
+
socket.addEventListener("close", onClose);
|
|
182
|
+
signal?.addEventListener("abort", onAbort);
|
|
183
|
+
if (connectTimeoutMs > 0) {
|
|
184
|
+
timeout = setTimeout(() => {
|
|
185
|
+
fail(new Error(`WebSocket connect timeout after ${connectTimeoutMs}ms`), "connect_timeout");
|
|
186
|
+
}, connectTimeoutMs);
|
|
187
|
+
}
|
|
188
|
+
if (signal?.aborted) {
|
|
189
|
+
onAbort();
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
function cachedEntryRelease(sessionId, accountId, entry) {
|
|
194
|
+
return (keep) => {
|
|
195
|
+
if (!keep || !socketReusable(entry.socket)) {
|
|
196
|
+
closeWebSocketSilently(entry.socket);
|
|
197
|
+
if (entry.idleTimer)
|
|
198
|
+
clearTimeout(entry.idleTimer);
|
|
199
|
+
forgetEntry(sessionId, accountId, entry);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
entry.busy = false;
|
|
203
|
+
scheduleSessionWebSocketExpiry(sessionId, accountId, entry);
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
async function acquireWebSocket(url, headers, sessionId, accountId, signal, connectTimeoutMs, env) {
|
|
207
|
+
const connectSingleUse = async () => {
|
|
208
|
+
const socket = await connectWebSocket(url, headers, signal, connectTimeoutMs, env);
|
|
209
|
+
return { socket, release: () => closeWebSocketSilently(socket) };
|
|
210
|
+
};
|
|
211
|
+
if (!sessionId)
|
|
212
|
+
return connectSingleUse();
|
|
213
|
+
const cached = websocketSessionCache.get(sessionId)?.get(accountId);
|
|
214
|
+
if (cached) {
|
|
215
|
+
if (cached.idleTimer) {
|
|
216
|
+
clearTimeout(cached.idleTimer);
|
|
217
|
+
cached.idleTimer = undefined;
|
|
218
|
+
}
|
|
219
|
+
if (cached.busy)
|
|
220
|
+
return connectSingleUse();
|
|
221
|
+
if (Date.now() - cached.createdAt >= SESSION_WEBSOCKET_MAX_AGE_MS) {
|
|
222
|
+
closeWebSocketSilently(cached.socket, 1000, "connection_age_limit");
|
|
223
|
+
forgetEntry(sessionId, accountId, cached);
|
|
224
|
+
} else if (socketReusable(cached.socket)) {
|
|
225
|
+
cached.busy = true;
|
|
226
|
+
return {
|
|
227
|
+
socket: cached.socket,
|
|
228
|
+
entry: cached,
|
|
229
|
+
release: cachedEntryRelease(sessionId, accountId, cached)
|
|
230
|
+
};
|
|
231
|
+
} else {
|
|
232
|
+
closeWebSocketSilently(cached.socket);
|
|
233
|
+
forgetEntry(sessionId, accountId, cached);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const socket = await connectWebSocket(url, headers, signal, connectTimeoutMs, env);
|
|
237
|
+
const entry = { socket, busy: true, createdAt: Date.now() };
|
|
238
|
+
let accountEntries = websocketSessionCache.get(sessionId);
|
|
239
|
+
if (!accountEntries) {
|
|
240
|
+
accountEntries = new Map;
|
|
241
|
+
websocketSessionCache.set(sessionId, accountEntries);
|
|
242
|
+
}
|
|
243
|
+
accountEntries.set(accountId, entry);
|
|
244
|
+
return { socket, entry, release: cachedEntryRelease(sessionId, accountId, entry) };
|
|
245
|
+
}
|
|
246
|
+
function extractWebSocketError(event) {
|
|
247
|
+
const message = readText2(event.message);
|
|
248
|
+
if (message)
|
|
249
|
+
return new Error(message);
|
|
250
|
+
const nested = event.error;
|
|
251
|
+
if (nested instanceof Error && nested.message.length > 0)
|
|
252
|
+
return nested;
|
|
253
|
+
const nestedMessage = readText2(readRecord2(nested)?.message);
|
|
254
|
+
if (nestedMessage)
|
|
255
|
+
return new Error(nestedMessage);
|
|
256
|
+
return new Error("WebSocket error");
|
|
257
|
+
}
|
|
258
|
+
function extractWebSocketCloseError(event) {
|
|
259
|
+
const code = readNumber2(event.code);
|
|
260
|
+
const reason = readText2(event.reason);
|
|
261
|
+
const codeText = code !== undefined ? ` ${code}` : "";
|
|
262
|
+
const reasonText = reason && reason.length > 0 ? ` ${reason}` : code === WEBSOCKET_MESSAGE_TOO_BIG_CLOSE_CODE ? " message too big" : "";
|
|
263
|
+
return new WebSocketCloseError(`WebSocket closed${codeText}${reasonText}`.trim(), {
|
|
264
|
+
code,
|
|
265
|
+
reason,
|
|
266
|
+
wasClean: readBoolean2(event.wasClean)
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
async function decodeWebSocketData(data) {
|
|
270
|
+
const text = readText2(data);
|
|
271
|
+
if (text !== undefined)
|
|
272
|
+
return text;
|
|
273
|
+
if (data instanceof ArrayBuffer) {
|
|
274
|
+
return new TextDecoder().decode(new Uint8Array(data));
|
|
275
|
+
}
|
|
276
|
+
if (ArrayBuffer.isView(data)) {
|
|
277
|
+
return new TextDecoder().decode(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
|
|
278
|
+
}
|
|
279
|
+
const blobLike = readRecord2(data);
|
|
280
|
+
if (blobLike && blobLike.arrayBuffer instanceof Function) {
|
|
281
|
+
const arrayBuffer = await blobLike.arrayBuffer();
|
|
282
|
+
return new TextDecoder().decode(new Uint8Array(arrayBuffer));
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
async function* parseWebSocket(socket, signal, idleTimeoutMs) {
|
|
287
|
+
const queue = new FifoQueue2;
|
|
288
|
+
let pending = null;
|
|
289
|
+
const wake = () => {
|
|
290
|
+
if (!pending)
|
|
291
|
+
return;
|
|
292
|
+
const resolve = pending;
|
|
293
|
+
pending = null;
|
|
294
|
+
resolve();
|
|
295
|
+
};
|
|
296
|
+
const enqueue = (event) => {
|
|
297
|
+
queue.enqueue(event);
|
|
298
|
+
wake();
|
|
299
|
+
};
|
|
300
|
+
const onMessage = (event) => {
|
|
301
|
+
enqueue({ kind: "message", data: event.data });
|
|
302
|
+
};
|
|
303
|
+
const onError = (event) => {
|
|
304
|
+
enqueue({ kind: "failure", error: extractWebSocketError(event) });
|
|
305
|
+
};
|
|
306
|
+
const onClose = (event) => {
|
|
307
|
+
enqueue({ kind: "failure", error: extractWebSocketCloseError(event) });
|
|
308
|
+
};
|
|
309
|
+
const onAbort = () => {
|
|
310
|
+
enqueue({ kind: "failure", error: new Error("Request was aborted") });
|
|
311
|
+
};
|
|
312
|
+
socket.addEventListener("message", onMessage);
|
|
313
|
+
socket.addEventListener("error", onError);
|
|
314
|
+
socket.addEventListener("close", onClose);
|
|
315
|
+
signal?.addEventListener("abort", onAbort);
|
|
316
|
+
try {
|
|
317
|
+
while (true) {
|
|
318
|
+
if (signal?.aborted) {
|
|
319
|
+
throw new Error("Request was aborted");
|
|
320
|
+
}
|
|
321
|
+
if (queue.length === 0) {
|
|
322
|
+
let timeout;
|
|
323
|
+
await new Promise((resolve, reject) => {
|
|
324
|
+
pending = resolve;
|
|
325
|
+
if (idleTimeoutMs !== undefined && idleTimeoutMs > 0) {
|
|
326
|
+
timeout = setTimeout(() => {
|
|
327
|
+
const error = new Error(`WebSocket idle timeout after ${idleTimeoutMs}ms`);
|
|
328
|
+
pending = null;
|
|
329
|
+
closeWebSocketSilently(socket, 1000, "idle_timeout");
|
|
330
|
+
reject(error);
|
|
331
|
+
}, idleTimeoutMs);
|
|
332
|
+
}
|
|
333
|
+
}).finally(() => {
|
|
334
|
+
if (timeout) {
|
|
335
|
+
clearTimeout(timeout);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
const event = queue.dequeue();
|
|
340
|
+
if (!event)
|
|
341
|
+
continue;
|
|
342
|
+
if (event.kind === "failure") {
|
|
343
|
+
throw event.error;
|
|
344
|
+
}
|
|
345
|
+
let text = null;
|
|
346
|
+
let frame;
|
|
347
|
+
try {
|
|
348
|
+
text = await decodeWebSocketData(event.data);
|
|
349
|
+
const decoded = text ? JSON.parse(text) : undefined;
|
|
350
|
+
if (Value.Check(CodexFrameJson2, decoded))
|
|
351
|
+
frame = decoded;
|
|
352
|
+
} catch (cause) {
|
|
353
|
+
throw new CodexProtocolError2(`Invalid Codex WebSocket JSON: ${formatThrownValue2(cause)}`, {
|
|
354
|
+
cause,
|
|
355
|
+
payload: text
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (!frame)
|
|
359
|
+
continue;
|
|
360
|
+
const terminal = TERMINAL_RESPONSE_TYPES2.has(frame.type);
|
|
361
|
+
yield frame;
|
|
362
|
+
if (terminal)
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
} finally {
|
|
366
|
+
socket.removeEventListener("message", onMessage);
|
|
367
|
+
socket.removeEventListener("error", onError);
|
|
368
|
+
socket.removeEventListener("close", onClose);
|
|
369
|
+
signal?.removeEventListener("abort", onAbort);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
function requestBodyWithoutInput(body) {
|
|
373
|
+
const { input: _input, previous_response_id: _previousResponseId, ...rest } = body;
|
|
374
|
+
return rest;
|
|
375
|
+
}
|
|
376
|
+
function getCachedWebSocketInputDelta(body, continuation) {
|
|
377
|
+
if (JSON.stringify(requestBodyWithoutInput(body)) !== JSON.stringify(requestBodyWithoutInput(continuation.lastRequestBody))) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const currentInput = body.input ?? [];
|
|
381
|
+
const baseline = [
|
|
382
|
+
...continuation.lastRequestBody.input ?? [],
|
|
383
|
+
...continuation.lastResponseItems
|
|
384
|
+
];
|
|
385
|
+
if (currentInput.length < baseline.length) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (JSON.stringify(currentInput.slice(0, baseline.length)) !== JSON.stringify(baseline)) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
return currentInput.slice(baseline.length);
|
|
392
|
+
}
|
|
393
|
+
function buildCachedWebSocketRequestBody(entry, body) {
|
|
394
|
+
const continuation = entry.continuation;
|
|
395
|
+
if (!continuation) {
|
|
396
|
+
return body;
|
|
397
|
+
}
|
|
398
|
+
const delta = getCachedWebSocketInputDelta(body, continuation);
|
|
399
|
+
if (!delta || !continuation.lastResponseId) {
|
|
400
|
+
entry.continuation = undefined;
|
|
401
|
+
return body;
|
|
402
|
+
}
|
|
403
|
+
return {
|
|
404
|
+
...body,
|
|
405
|
+
previous_response_id: continuation.lastResponseId,
|
|
406
|
+
input: delta
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
var NON_STARTING_FRAMES = new Set([
|
|
410
|
+
"codex.rate_limits",
|
|
411
|
+
"error",
|
|
412
|
+
"response.failed"
|
|
413
|
+
]);
|
|
414
|
+
async function* startWebSocketOutputOnFirstEvent(frames, onStart) {
|
|
415
|
+
let started = false;
|
|
416
|
+
for await (const frame of frames) {
|
|
417
|
+
if (!started && !NON_STARTING_FRAMES.has(frame.type)) {
|
|
418
|
+
started = true;
|
|
419
|
+
onStart();
|
|
420
|
+
}
|
|
421
|
+
yield frame;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
async function processWebSocketStream(request, headers, output, stream, onStart) {
|
|
425
|
+
const { model, options } = request;
|
|
426
|
+
const { socket, entry, release } = await acquireWebSocket(resolveCodexWebSocketUrl2(model.baseUrl), headers, request.cacheSessionId, request.accountId, options?.signal, request.connectTimeoutMs, options?.env);
|
|
427
|
+
let keepConnection = true;
|
|
428
|
+
const useCachedContext = options?.transport === "websocket-cached" || options?.transport === "auto";
|
|
429
|
+
const requestBody = useCachedContext && entry ? buildCachedWebSocketRequestBody(entry, request.body) : request.body;
|
|
430
|
+
try {
|
|
431
|
+
socket.send(JSON.stringify({ type: "response.create", ...requestBody }));
|
|
432
|
+
await processResponsesStream2(mapCodexEvents2(startWebSocketOutputOnFirstEvent(parseWebSocket(socket, options?.signal, request.timeoutMs), onStart), output, options?.onAccountLimits === undefined ? undefined : (limits) => options.onAccountLimits?.(limits, model)), output, stream, model, codexStreamOptions2(request));
|
|
433
|
+
if (options?.signal?.aborted) {
|
|
434
|
+
keepConnection = false;
|
|
435
|
+
} else if (useCachedContext && entry && output.responseId) {
|
|
436
|
+
const responseItems = convertResponsesMessages2(model, normalizeContext2({ messages: [output] }), CODEX_TOOL_CALL_PROVIDERS2, {
|
|
437
|
+
includeSystemPrompt: false,
|
|
438
|
+
grammarToolInputProperties: request.grammarToolInputProperties
|
|
439
|
+
}).filter((item) => item.type !== "function_call_output" && item.type !== "custom_tool_call_output");
|
|
440
|
+
entry.continuation = {
|
|
441
|
+
lastRequestBody: request.body,
|
|
442
|
+
lastResponseId: output.responseId,
|
|
443
|
+
lastResponseItems: responseItems
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
} catch (error) {
|
|
447
|
+
if (entry) {
|
|
448
|
+
entry.continuation = undefined;
|
|
449
|
+
}
|
|
450
|
+
keepConnection = false;
|
|
451
|
+
throw error;
|
|
452
|
+
} finally {
|
|
453
|
+
release(keepConnection);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
async function streamOverWebSocket2(request, headers, output, stream) {
|
|
457
|
+
const { options, cacheSessionId } = request;
|
|
458
|
+
const transport = options?.transport || "auto";
|
|
459
|
+
if (transport === "sse" || cacheSessionId !== undefined && websocketSseFallbackSessions.has(cacheSessionId)) {
|
|
460
|
+
return false;
|
|
461
|
+
}
|
|
462
|
+
let startEmitted = false;
|
|
463
|
+
let retriedWebSocketConnectionLimit = false;
|
|
464
|
+
let retriedMissingWebSocketContinuation = false;
|
|
465
|
+
for (;; ) {
|
|
466
|
+
let websocketStarted = false;
|
|
467
|
+
try {
|
|
468
|
+
await processWebSocketStream(request, headers, output, stream, () => {
|
|
469
|
+
websocketStarted = true;
|
|
470
|
+
if (!startEmitted) {
|
|
471
|
+
startEmitted = true;
|
|
472
|
+
stream.push({ type: "start", partial: output });
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
return true;
|
|
476
|
+
} catch (error) {
|
|
477
|
+
const aborted = options?.signal?.aborted;
|
|
478
|
+
const code = codexErrorCode(error);
|
|
479
|
+
const connectionLimitBeforeStart = !websocketStarted && code === WEBSOCKET_CONNECTION_LIMIT_REACHED_CODE;
|
|
480
|
+
if (!aborted && code === PREVIOUS_RESPONSE_NOT_FOUND_CODE && !retriedMissingWebSocketContinuation) {
|
|
481
|
+
retriedMissingWebSocketContinuation = true;
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
if (!aborted && connectionLimitBeforeStart && !retriedWebSocketConnectionLimit) {
|
|
485
|
+
retriedWebSocketConnectionLimit = true;
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
const failedInProtocol = error instanceof CodexApiError2 || error instanceof CodexProtocolError2 || error instanceof ResponsesUsageError2;
|
|
489
|
+
if (aborted || failedInProtocol && !connectionLimitBeforeStart) {
|
|
490
|
+
throw error;
|
|
491
|
+
}
|
|
492
|
+
appendAssistantMessageDiagnostic2(output, createAssistantMessageDiagnostic2("provider_transport_failure", error, {
|
|
493
|
+
configuredTransport: transport,
|
|
494
|
+
fallbackTransport: websocketStarted ? undefined : "sse",
|
|
495
|
+
eventsEmitted: websocketStarted,
|
|
496
|
+
phase: websocketStarted ? "after_message_stream_start" : "before_message_stream_start",
|
|
497
|
+
requestBytes: new TextEncoder().encode(JSON.stringify(request.body)).byteLength
|
|
498
|
+
}));
|
|
499
|
+
if (cacheSessionId)
|
|
500
|
+
websocketSseFallbackSessions.add(cacheSessionId);
|
|
501
|
+
if (websocketStarted) {
|
|
502
|
+
throw error;
|
|
503
|
+
}
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
export { closeOpenAICodexWebSocketSessions2, streamOverWebSocket2 };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getProviderEnvValue2
|
|
3
|
+
} from "./account-limits-cache-b23sbrke.js";
|
|
4
|
+
|
|
5
|
+
// src/env-api-keys.ts
|
|
6
|
+
var ANTHROPIC_AUTH_TOKEN_ENV2 = "ANTHROPIC_AUTH_TOKEN";
|
|
7
|
+
var ANTHROPIC_OAUTH_TOKEN_ENV2 = "ANTHROPIC_OAUTH_TOKEN";
|
|
8
|
+
var ANTHROPIC_API_KEY_ENV2 = "ANTHROPIC_API_KEY";
|
|
9
|
+
var ANTHROPIC_FEDERATION_RULE_ID_ENV2 = "ANTHROPIC_FEDERATION_RULE_ID";
|
|
10
|
+
var ANTHROPIC_ORGANIZATION_ID_ENV2 = "ANTHROPIC_ORGANIZATION_ID";
|
|
11
|
+
var ANTHROPIC_SERVICE_ACCOUNT_ID_ENV2 = "ANTHROPIC_SERVICE_ACCOUNT_ID";
|
|
12
|
+
var ANTHROPIC_IDENTITY_TOKEN_FILE_ENV2 = "ANTHROPIC_IDENTITY_TOKEN_FILE";
|
|
13
|
+
var ANTHROPIC_WORKSPACE_ID_ENV2 = "ANTHROPIC_WORKSPACE_ID";
|
|
14
|
+
var ANTHROPIC_ENV_KEYS2 = [
|
|
15
|
+
ANTHROPIC_AUTH_TOKEN_ENV2,
|
|
16
|
+
ANTHROPIC_OAUTH_TOKEN_ENV2,
|
|
17
|
+
ANTHROPIC_API_KEY_ENV2
|
|
18
|
+
];
|
|
19
|
+
function getApiKeyEnvVars(provider) {
|
|
20
|
+
if (provider === "anthropic") {
|
|
21
|
+
return ANTHROPIC_ENV_KEYS2;
|
|
22
|
+
}
|
|
23
|
+
if (provider === "opencode" || provider === "opencode-go")
|
|
24
|
+
return ["OPENCODE_API_KEY"];
|
|
25
|
+
if (provider === "openai")
|
|
26
|
+
return ["OPENAI_API_KEY"];
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
function findEnvKeys2(provider, env) {
|
|
30
|
+
const envVars = getApiKeyEnvVars(provider);
|
|
31
|
+
if (!envVars)
|
|
32
|
+
return;
|
|
33
|
+
const found = envVars.filter((envVar) => !!getProviderEnvValue2(envVar, env));
|
|
34
|
+
return found.length > 0 ? found : undefined;
|
|
35
|
+
}
|
|
36
|
+
function getEnvApiKey2(provider, env) {
|
|
37
|
+
const envKeys = findEnvKeys2(provider, env);
|
|
38
|
+
if (envKeys?.[0]) {
|
|
39
|
+
const apiKeyEnv = provider === "anthropic" ? envKeys.find((key) => key !== ANTHROPIC_AUTH_TOKEN_ENV2) : envKeys[0];
|
|
40
|
+
if (apiKeyEnv)
|
|
41
|
+
return getProviderEnvValue2(apiKeyEnv, env);
|
|
42
|
+
}
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { ANTHROPIC_AUTH_TOKEN_ENV2, ANTHROPIC_OAUTH_TOKEN_ENV2, ANTHROPIC_API_KEY_ENV2, ANTHROPIC_FEDERATION_RULE_ID_ENV2, ANTHROPIC_ORGANIZATION_ID_ENV2, ANTHROPIC_SERVICE_ACCOUNT_ID_ENV2, ANTHROPIC_IDENTITY_TOKEN_FILE_ENV2, ANTHROPIC_WORKSPACE_ID_ENV2, ANTHROPIC_ENV_KEYS2, findEnvKeys2, getEnvApiKey2 };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// src/utils/abort.ts
|
|
2
|
+
function abortReason(signal) {
|
|
3
|
+
if (signal.reason !== undefined)
|
|
4
|
+
return signal.reason;
|
|
5
|
+
const error = new Error("The operation was aborted");
|
|
6
|
+
error.name = "AbortError";
|
|
7
|
+
return error;
|
|
8
|
+
}
|
|
9
|
+
function sleep2(ms, signal, abortError) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
if (signal?.aborted) {
|
|
12
|
+
reject(abortError());
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const onAbort = () => {
|
|
16
|
+
clearTimeout(timeout);
|
|
17
|
+
reject(abortError());
|
|
18
|
+
};
|
|
19
|
+
const timeout = setTimeout(() => {
|
|
20
|
+
signal?.removeEventListener("abort", onAbort);
|
|
21
|
+
resolve();
|
|
22
|
+
}, Math.max(0, ms));
|
|
23
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function operationSignal2(signal) {
|
|
27
|
+
return signal ?? new AbortController().signal;
|
|
28
|
+
}
|
|
29
|
+
function raceWithAbortSignal2(operation, signal) {
|
|
30
|
+
if (signal.aborted) {
|
|
31
|
+
operation.catch(() => {});
|
|
32
|
+
return Promise.reject(abortReason(signal));
|
|
33
|
+
}
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
let settled = false;
|
|
36
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
37
|
+
const onAbort = () => {
|
|
38
|
+
if (settled)
|
|
39
|
+
return;
|
|
40
|
+
settled = true;
|
|
41
|
+
cleanup();
|
|
42
|
+
reject(abortReason(signal));
|
|
43
|
+
};
|
|
44
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
45
|
+
operation.then((value) => {
|
|
46
|
+
if (settled)
|
|
47
|
+
return;
|
|
48
|
+
settled = true;
|
|
49
|
+
cleanup();
|
|
50
|
+
resolve(value);
|
|
51
|
+
}, (error) => {
|
|
52
|
+
if (settled)
|
|
53
|
+
return;
|
|
54
|
+
settled = true;
|
|
55
|
+
cleanup();
|
|
56
|
+
reject(error);
|
|
57
|
+
});
|
|
58
|
+
if (signal.aborted)
|
|
59
|
+
onAbort();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { sleep2, operationSignal2, raceWithAbortSignal2 };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clampThinkingLevel2
|
|
3
|
+
} from "./account-limits-cache-zgzptd0z.js";
|
|
4
|
+
import {
|
|
5
|
+
estimateContextTokens2
|
|
6
|
+
} from "./account-limits-cache-dejeygdt.js";
|
|
7
|
+
|
|
8
|
+
// src/api/simple-options.ts
|
|
9
|
+
var CONTEXT_SAFETY_TOKENS = 4096;
|
|
10
|
+
var MIN_MAX_TOKENS = 1;
|
|
11
|
+
function clampMaxTokensToContext2(model, context, maxTokens) {
|
|
12
|
+
if (model.contextWindow <= 0)
|
|
13
|
+
return Math.max(MIN_MAX_TOKENS, maxTokens);
|
|
14
|
+
const available = model.contextWindow - estimateContextTokens2(context).tokens - CONTEXT_SAFETY_TOKENS;
|
|
15
|
+
return Math.min(maxTokens, Math.max(MIN_MAX_TOKENS, available));
|
|
16
|
+
}
|
|
17
|
+
function buildBaseOptions2(model, context, options) {
|
|
18
|
+
const samplingParams = model.samplingParams || options?.samplingParams ? { ...model.samplingParams, ...options?.samplingParams } : undefined;
|
|
19
|
+
return {
|
|
20
|
+
temperature: options?.temperature,
|
|
21
|
+
samplingParams,
|
|
22
|
+
maxTokens: clampMaxTokensToContext2(model, context, options?.maxTokens ?? model.maxTokens),
|
|
23
|
+
signal: options?.signal,
|
|
24
|
+
fast: options?.fast,
|
|
25
|
+
apiKey: options?.apiKey,
|
|
26
|
+
fetch: options?.fetch,
|
|
27
|
+
transport: options?.transport,
|
|
28
|
+
cacheRetention: options?.cacheRetention,
|
|
29
|
+
sessionId: options?.sessionId,
|
|
30
|
+
headers: options?.headers,
|
|
31
|
+
onPayload: options?.onPayload,
|
|
32
|
+
onResponse: options?.onResponse,
|
|
33
|
+
onAccountLimits: options?.onAccountLimits,
|
|
34
|
+
timeoutMs: options?.timeoutMs,
|
|
35
|
+
websocketConnectTimeoutMs: options?.websocketConnectTimeoutMs,
|
|
36
|
+
maxRetries: options?.maxRetries,
|
|
37
|
+
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
38
|
+
metadata: options?.metadata,
|
|
39
|
+
env: options?.env,
|
|
40
|
+
toolChoice: options?.toolChoice
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function simpleReasoningEffort2(model, reasoning) {
|
|
44
|
+
const clamped = reasoning ? clampThinkingLevel2(model, reasoning) : undefined;
|
|
45
|
+
return clamped === "off" ? undefined : clamped;
|
|
46
|
+
}
|
|
47
|
+
var MIN_ANSWER_TOKENS = 1024;
|
|
48
|
+
var DEFAULT_THINKING_BUDGETS = {
|
|
49
|
+
minimal: 1024,
|
|
50
|
+
low: 2048,
|
|
51
|
+
medium: 8192,
|
|
52
|
+
high: 16384
|
|
53
|
+
};
|
|
54
|
+
function clampReasoning(effort) {
|
|
55
|
+
return effort === "xhigh" || effort === "max" ? "high" : effort;
|
|
56
|
+
}
|
|
57
|
+
function thinkingBudgetForLevel2(reasoningLevel, customBudgets) {
|
|
58
|
+
const budgets = { ...DEFAULT_THINKING_BUDGETS, ...customBudgets };
|
|
59
|
+
const level = clampReasoning(reasoningLevel);
|
|
60
|
+
return budgets[level];
|
|
61
|
+
}
|
|
62
|
+
function clampThinkingBudgetToAnswerRoom2(thinkingBudget, ceiling) {
|
|
63
|
+
return Math.min(thinkingBudget, Math.max(0, ceiling - MIN_ANSWER_TOKENS));
|
|
64
|
+
}
|
|
65
|
+
function adjustMaxTokensForThinking2(baseMaxTokens, modelMaxTokens, reasoningLevel, customBudgets) {
|
|
66
|
+
let thinkingBudget = thinkingBudgetForLevel2(reasoningLevel, customBudgets);
|
|
67
|
+
const maxTokens = baseMaxTokens === undefined ? modelMaxTokens : Math.min(baseMaxTokens + thinkingBudget, modelMaxTokens);
|
|
68
|
+
if (maxTokens <= thinkingBudget) {
|
|
69
|
+
thinkingBudget = clampThinkingBudgetToAnswerRoom2(thinkingBudget, maxTokens);
|
|
70
|
+
}
|
|
71
|
+
return { maxTokens, thinkingBudget };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { clampMaxTokensToContext2, buildBaseOptions2, simpleReasoningEffort2, thinkingBudgetForLevel2, clampThinkingBudgetToAnswerRoom2, adjustMaxTokensForThinking2 };
|