@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,417 @@
|
|
|
1
|
+
import {
|
|
2
|
+
calculateCost2
|
|
3
|
+
} from "./account-limits-cache-zgzptd0z.js";
|
|
4
|
+
import {
|
|
5
|
+
parseStreamingJson2,
|
|
6
|
+
parseToolCallArguments2,
|
|
7
|
+
unfinishedToolCallArguments2
|
|
8
|
+
} from "./account-limits-cache-fbkaq46e.js";
|
|
9
|
+
import {
|
|
10
|
+
appendGrammarToolInputJsonDelta2
|
|
11
|
+
} from "./account-limits-cache-h92bzz1j.js";
|
|
12
|
+
import {
|
|
13
|
+
emptyCost2,
|
|
14
|
+
stripStreamingScratchState2
|
|
15
|
+
} from "./account-limits-cache-fsktnetm.js";
|
|
16
|
+
import {
|
|
17
|
+
TokenCount2
|
|
18
|
+
} from "./account-limits-cache-736tdyme.js";
|
|
19
|
+
import {
|
|
20
|
+
encodeTextSignatureV12
|
|
21
|
+
} from "./account-limits-cache-n3yk6g5c.js";
|
|
22
|
+
import {
|
|
23
|
+
TerminalReasoningItemSchema2,
|
|
24
|
+
StoredReasoningItemSchema2,
|
|
25
|
+
ServiceTierSchema2,
|
|
26
|
+
ResponsesStreamEventCheck2
|
|
27
|
+
} from "./account-limits-cache-a558rjbg.js";
|
|
28
|
+
|
|
29
|
+
// src/api/openai-responses-stream.ts
|
|
30
|
+
import { Type } from "typebox";
|
|
31
|
+
import { Value } from "typebox/value";
|
|
32
|
+
function withPriorityTier2(options) {
|
|
33
|
+
return options?.fast === true && options.serviceTier === undefined ? { ...options, serviceTier: "priority" } : options;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class ResponsesUsageError2 extends Error {
|
|
37
|
+
}
|
|
38
|
+
var responseUsage = Type.Object({
|
|
39
|
+
input_tokens: Type.Optional(TokenCount2),
|
|
40
|
+
output_tokens: Type.Optional(TokenCount2),
|
|
41
|
+
total_tokens: Type.Optional(TokenCount2),
|
|
42
|
+
input_tokens_details: Type.Optional(Type.Object({
|
|
43
|
+
cached_tokens: Type.Optional(TokenCount2),
|
|
44
|
+
cache_write_tokens: Type.Optional(TokenCount2)
|
|
45
|
+
})),
|
|
46
|
+
output_tokens_details: Type.Optional(Type.Object({ reasoning_tokens: Type.Optional(TokenCount2) }))
|
|
47
|
+
});
|
|
48
|
+
function appendCustomToolCallInput(block, nextInput, close) {
|
|
49
|
+
const customInput = block.customInput;
|
|
50
|
+
if (!customInput)
|
|
51
|
+
return;
|
|
52
|
+
const delta = appendGrammarToolInputJsonDelta2(customInput.jsonBuffer, customInput.property, nextInput, close);
|
|
53
|
+
customInput.input = nextInput;
|
|
54
|
+
block.arguments = { [customInput.property]: nextInput };
|
|
55
|
+
return delta;
|
|
56
|
+
}
|
|
57
|
+
async function processResponsesStream2(openaiStream, output, stream, model, options) {
|
|
58
|
+
let sawTerminalResponseEvent = false;
|
|
59
|
+
const outputSlots = new Map;
|
|
60
|
+
const unfinishedToolCalls = new Set;
|
|
61
|
+
const reasoningBlocksById = new Map;
|
|
62
|
+
const applyMessagePhaseStopReason = (item) => {
|
|
63
|
+
if (item.type === "message" && item.phase === "final_answer") {
|
|
64
|
+
output.stopReason = "stop";
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const pushToolCallDelta = (slot, delta) => {
|
|
68
|
+
if (delta === undefined)
|
|
69
|
+
return;
|
|
70
|
+
stream.push({
|
|
71
|
+
type: "toolcall_delta",
|
|
72
|
+
contentIndex: slot.contentIndex,
|
|
73
|
+
delta,
|
|
74
|
+
partial: output
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
const createSlot = (outputIndex, item) => {
|
|
78
|
+
if (item.type === "reasoning") {
|
|
79
|
+
const block = { type: "thinking", thinking: "" };
|
|
80
|
+
output.content.push(block);
|
|
81
|
+
const slot = {
|
|
82
|
+
type: "thinking",
|
|
83
|
+
block,
|
|
84
|
+
contentIndex: output.content.length - 1
|
|
85
|
+
};
|
|
86
|
+
outputSlots.set(outputIndex, slot);
|
|
87
|
+
stream.push({ type: "thinking_start", contentIndex: slot.contentIndex, partial: output });
|
|
88
|
+
return slot;
|
|
89
|
+
}
|
|
90
|
+
if (item.type === "message") {
|
|
91
|
+
applyMessagePhaseStopReason(item);
|
|
92
|
+
const block = { type: "text", text: "" };
|
|
93
|
+
output.content.push(block);
|
|
94
|
+
const slot = {
|
|
95
|
+
type: "text",
|
|
96
|
+
block,
|
|
97
|
+
contentIndex: output.content.length - 1
|
|
98
|
+
};
|
|
99
|
+
outputSlots.set(outputIndex, slot);
|
|
100
|
+
stream.push({ type: "text_start", contentIndex: slot.contentIndex, partial: output });
|
|
101
|
+
return slot;
|
|
102
|
+
}
|
|
103
|
+
if (item.type === "function_call") {
|
|
104
|
+
const block = {
|
|
105
|
+
type: "toolCall",
|
|
106
|
+
id: `${item.call_id}|${item.id ?? ""}`,
|
|
107
|
+
name: item.name,
|
|
108
|
+
arguments: {},
|
|
109
|
+
partialJson: item.arguments || ""
|
|
110
|
+
};
|
|
111
|
+
if (item.namespace !== undefined)
|
|
112
|
+
block.namespace = item.namespace;
|
|
113
|
+
output.content.push(block);
|
|
114
|
+
const slot = {
|
|
115
|
+
type: "toolCall",
|
|
116
|
+
block,
|
|
117
|
+
contentIndex: output.content.length - 1
|
|
118
|
+
};
|
|
119
|
+
outputSlots.set(outputIndex, slot);
|
|
120
|
+
unfinishedToolCalls.add(block);
|
|
121
|
+
stream.push({ type: "toolcall_start", contentIndex: slot.contentIndex, partial: output });
|
|
122
|
+
return slot;
|
|
123
|
+
}
|
|
124
|
+
if (item.type === "custom_tool_call") {
|
|
125
|
+
const inputProperty = options?.grammarToolInputProperties?.get(item.name) ?? "input";
|
|
126
|
+
const input = item.input || "";
|
|
127
|
+
const block = {
|
|
128
|
+
type: "toolCall",
|
|
129
|
+
id: `${item.call_id}|${item.id ?? ""}`,
|
|
130
|
+
name: item.name,
|
|
131
|
+
arguments: { [inputProperty]: input },
|
|
132
|
+
customInput: {
|
|
133
|
+
property: inputProperty,
|
|
134
|
+
input,
|
|
135
|
+
jsonBuffer: { input: "", started: false, closed: false }
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
if (item.namespace !== undefined)
|
|
139
|
+
block.namespace = item.namespace;
|
|
140
|
+
output.content.push(block);
|
|
141
|
+
const slot = {
|
|
142
|
+
type: "toolCall",
|
|
143
|
+
block,
|
|
144
|
+
contentIndex: output.content.length - 1
|
|
145
|
+
};
|
|
146
|
+
outputSlots.set(outputIndex, slot);
|
|
147
|
+
unfinishedToolCalls.add(block);
|
|
148
|
+
stream.push({ type: "toolcall_start", contentIndex: slot.contentIndex, partial: output });
|
|
149
|
+
return slot;
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
};
|
|
153
|
+
const getOrCreateSlot = (outputIndex, item) => {
|
|
154
|
+
return outputSlots.get(outputIndex) ?? createSlot(outputIndex, item);
|
|
155
|
+
};
|
|
156
|
+
const backfillReasoningSignatures = (responseOutput) => {
|
|
157
|
+
for (const item of responseOutput) {
|
|
158
|
+
if (!Value.Check(TerminalReasoningItemSchema2, item) || !item.encrypted_content)
|
|
159
|
+
continue;
|
|
160
|
+
const block = reasoningBlocksById.get(item.id);
|
|
161
|
+
if (!block?.thinkingSignature)
|
|
162
|
+
continue;
|
|
163
|
+
const storedItem = JSON.parse(block.thinkingSignature);
|
|
164
|
+
if (!Value.Check(StoredReasoningItemSchema2, storedItem) || storedItem.encrypted_content) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
block.thinkingSignature = JSON.stringify({
|
|
168
|
+
...storedItem,
|
|
169
|
+
encrypted_content: item.encrypted_content
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const finalizeResponse = (response) => {
|
|
174
|
+
sawTerminalResponseEvent = true;
|
|
175
|
+
backfillReasoningSignatures(response.output ?? []);
|
|
176
|
+
if (response?.id) {
|
|
177
|
+
output.responseId = response.id;
|
|
178
|
+
}
|
|
179
|
+
const usage = response.usage;
|
|
180
|
+
if (usage !== undefined && usage !== null) {
|
|
181
|
+
if (!Value.Check(responseUsage, usage)) {
|
|
182
|
+
throw new ResponsesUsageError2("Invalid OpenAI Responses usage: expected finite non-negative integer token counts");
|
|
183
|
+
}
|
|
184
|
+
const cachedTokens = usage.input_tokens_details?.cached_tokens ?? 0;
|
|
185
|
+
const cacheWriteTokens = usage.input_tokens_details?.cache_write_tokens ?? 0;
|
|
186
|
+
output.usage = {
|
|
187
|
+
input: Math.max(0, (usage.input_tokens ?? 0) - cachedTokens - cacheWriteTokens),
|
|
188
|
+
output: usage.output_tokens ?? 0,
|
|
189
|
+
cacheRead: cachedTokens,
|
|
190
|
+
cacheWrite: cacheWriteTokens,
|
|
191
|
+
reasoning: usage.output_tokens_details?.reasoning_tokens ?? 0,
|
|
192
|
+
totalTokens: usage.total_tokens ?? 0,
|
|
193
|
+
cost: emptyCost2()
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
calculateCost2(model, output.usage);
|
|
197
|
+
if (options?.applyServiceTierPricing) {
|
|
198
|
+
const responseTier = Value.Check(ServiceTierSchema2, response.service_tier) ? response.service_tier : undefined;
|
|
199
|
+
const serviceTier = options.resolveServiceTier ? options.resolveServiceTier(responseTier, options.serviceTier) : responseTier ?? options.serviceTier;
|
|
200
|
+
options.applyServiceTierPricing(output.usage, serviceTier);
|
|
201
|
+
}
|
|
202
|
+
const status = response?.status;
|
|
203
|
+
const incompleteReason = response.incomplete_details?.reason;
|
|
204
|
+
output.rawStopReason = incompleteReason ? `${status}.${incompleteReason}` : status;
|
|
205
|
+
const mappedStop = mapStopReason(status, incompleteReason);
|
|
206
|
+
output.stopReason = mappedStop.stopReason;
|
|
207
|
+
output.errorMessage = mappedStop.errorMessage;
|
|
208
|
+
if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") {
|
|
209
|
+
output.stopReason = "toolUse";
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
for await (const event of openaiStream) {
|
|
213
|
+
if (!ResponsesStreamEventCheck2.Check(event))
|
|
214
|
+
continue;
|
|
215
|
+
if (event.type === "response.created") {
|
|
216
|
+
output.responseId = event.response.id;
|
|
217
|
+
} else if (event.type === "response.output_item.added") {
|
|
218
|
+
createSlot(event.output_index, event.item);
|
|
219
|
+
} else if (event.type === "response.reasoning_summary_text.delta") {
|
|
220
|
+
const slot = outputSlots.get(event.output_index);
|
|
221
|
+
if (slot?.type !== "thinking")
|
|
222
|
+
continue;
|
|
223
|
+
slot.block.thinking += event.delta;
|
|
224
|
+
stream.push({
|
|
225
|
+
type: "thinking_delta",
|
|
226
|
+
contentIndex: slot.contentIndex,
|
|
227
|
+
delta: event.delta,
|
|
228
|
+
partial: output
|
|
229
|
+
});
|
|
230
|
+
} else if (event.type === "response.reasoning_summary_part.done") {
|
|
231
|
+
const slot = outputSlots.get(event.output_index);
|
|
232
|
+
if (slot?.type !== "thinking")
|
|
233
|
+
continue;
|
|
234
|
+
slot.block.thinking += `
|
|
235
|
+
|
|
236
|
+
`;
|
|
237
|
+
stream.push({
|
|
238
|
+
type: "thinking_delta",
|
|
239
|
+
contentIndex: slot.contentIndex,
|
|
240
|
+
delta: `
|
|
241
|
+
|
|
242
|
+
`,
|
|
243
|
+
partial: output
|
|
244
|
+
});
|
|
245
|
+
} else if (event.type === "response.reasoning_text.delta") {
|
|
246
|
+
const slot = outputSlots.get(event.output_index);
|
|
247
|
+
if (slot?.type !== "thinking")
|
|
248
|
+
continue;
|
|
249
|
+
slot.block.thinking += event.delta;
|
|
250
|
+
stream.push({
|
|
251
|
+
type: "thinking_delta",
|
|
252
|
+
contentIndex: slot.contentIndex,
|
|
253
|
+
delta: event.delta,
|
|
254
|
+
partial: output
|
|
255
|
+
});
|
|
256
|
+
} else if (event.type === "response.output_text.delta") {
|
|
257
|
+
const slot = outputSlots.get(event.output_index);
|
|
258
|
+
if (slot?.type !== "text")
|
|
259
|
+
continue;
|
|
260
|
+
slot.block.text += event.delta;
|
|
261
|
+
stream.push({
|
|
262
|
+
type: "text_delta",
|
|
263
|
+
contentIndex: slot.contentIndex,
|
|
264
|
+
delta: event.delta,
|
|
265
|
+
partial: output
|
|
266
|
+
});
|
|
267
|
+
} else if (event.type === "response.refusal.delta") {
|
|
268
|
+
const slot = outputSlots.get(event.output_index);
|
|
269
|
+
if (slot?.type !== "text")
|
|
270
|
+
continue;
|
|
271
|
+
slot.block.text += event.delta;
|
|
272
|
+
stream.push({
|
|
273
|
+
type: "text_delta",
|
|
274
|
+
contentIndex: slot.contentIndex,
|
|
275
|
+
delta: event.delta,
|
|
276
|
+
partial: output
|
|
277
|
+
});
|
|
278
|
+
} else if (event.type === "response.function_call_arguments.delta") {
|
|
279
|
+
const slot = outputSlots.get(event.output_index);
|
|
280
|
+
if (slot?.type !== "toolCall" || slot.block.partialJson === undefined)
|
|
281
|
+
continue;
|
|
282
|
+
slot.block.partialJson += event.delta;
|
|
283
|
+
slot.block.arguments = parseStreamingJson2(slot.block.partialJson);
|
|
284
|
+
pushToolCallDelta(slot, event.delta);
|
|
285
|
+
} else if (event.type === "response.function_call_arguments.done") {
|
|
286
|
+
const slot = outputSlots.get(event.output_index);
|
|
287
|
+
if (slot?.type !== "toolCall" || slot.block.partialJson === undefined)
|
|
288
|
+
continue;
|
|
289
|
+
const previousPartialJson = slot.block.partialJson;
|
|
290
|
+
slot.block.partialJson = event.arguments;
|
|
291
|
+
slot.block.arguments = parseStreamingJson2(slot.block.partialJson);
|
|
292
|
+
if (event.arguments.startsWith(previousPartialJson)) {
|
|
293
|
+
const delta = event.arguments.slice(previousPartialJson.length);
|
|
294
|
+
if (delta.length > 0)
|
|
295
|
+
pushToolCallDelta(slot, delta);
|
|
296
|
+
}
|
|
297
|
+
} else if (event.type === "response.custom_tool_call_input.delta") {
|
|
298
|
+
const slot = outputSlots.get(event.output_index);
|
|
299
|
+
if (slot?.type !== "toolCall" || !slot.block.customInput)
|
|
300
|
+
continue;
|
|
301
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, slot.block.customInput.input + event.delta, false));
|
|
302
|
+
} else if (event.type === "response.custom_tool_call_input.done") {
|
|
303
|
+
const slot = outputSlots.get(event.output_index);
|
|
304
|
+
if (slot?.type !== "toolCall" || !slot.block.customInput)
|
|
305
|
+
continue;
|
|
306
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, event.input, true));
|
|
307
|
+
} else if (event.type === "response.output_item.done") {
|
|
308
|
+
const item = event.item;
|
|
309
|
+
applyMessagePhaseStopReason(item);
|
|
310
|
+
const slot = getOrCreateSlot(event.output_index, item);
|
|
311
|
+
if (item.type === "reasoning" && slot?.type === "thinking") {
|
|
312
|
+
const summaryText = item.summary?.map((s) => s.text).join(`
|
|
313
|
+
|
|
314
|
+
`) || "";
|
|
315
|
+
const contentText = item.content?.map((c) => c.text).join(`
|
|
316
|
+
|
|
317
|
+
`) || "";
|
|
318
|
+
slot.block.thinking = summaryText || contentText || slot.block.thinking;
|
|
319
|
+
slot.block.thinkingSignature = JSON.stringify(item);
|
|
320
|
+
reasoningBlocksById.set(item.id, slot.block);
|
|
321
|
+
stream.push({
|
|
322
|
+
type: "thinking_end",
|
|
323
|
+
contentIndex: slot.contentIndex,
|
|
324
|
+
content: slot.block.thinking,
|
|
325
|
+
partial: output
|
|
326
|
+
});
|
|
327
|
+
outputSlots.delete(event.output_index);
|
|
328
|
+
} else if (item.type === "message" && slot?.type === "text") {
|
|
329
|
+
slot.block.text = item.content?.map((c) => c.type === "output_text" ? c.text : c.refusal).join("") || "";
|
|
330
|
+
if (item.id !== undefined) {
|
|
331
|
+
slot.block.textSignature = encodeTextSignatureV12(item.id, item.phase ?? undefined);
|
|
332
|
+
}
|
|
333
|
+
stream.push({
|
|
334
|
+
type: "text_end",
|
|
335
|
+
contentIndex: slot.contentIndex,
|
|
336
|
+
content: slot.block.text,
|
|
337
|
+
partial: output
|
|
338
|
+
});
|
|
339
|
+
outputSlots.delete(event.output_index);
|
|
340
|
+
} else if (item.type === "function_call" && slot?.type === "toolCall" && slot.block.partialJson !== undefined) {
|
|
341
|
+
Object.assign(slot.block, parseToolCallArguments2(item.arguments || slot.block.partialJson));
|
|
342
|
+
if (item.namespace !== undefined)
|
|
343
|
+
slot.block.namespace = item.namespace;
|
|
344
|
+
delete slot.block.partialJson;
|
|
345
|
+
unfinishedToolCalls.delete(slot.block);
|
|
346
|
+
stream.push({
|
|
347
|
+
type: "toolcall_end",
|
|
348
|
+
contentIndex: slot.contentIndex,
|
|
349
|
+
toolCall: slot.block,
|
|
350
|
+
partial: output
|
|
351
|
+
});
|
|
352
|
+
outputSlots.delete(event.output_index);
|
|
353
|
+
} else if (item.type === "custom_tool_call" && slot?.type === "toolCall" && slot.block.customInput) {
|
|
354
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, item.input ?? slot.block.customInput.input, true));
|
|
355
|
+
if (item.namespace !== undefined)
|
|
356
|
+
slot.block.namespace = item.namespace;
|
|
357
|
+
delete slot.block.customInput;
|
|
358
|
+
unfinishedToolCalls.delete(slot.block);
|
|
359
|
+
stream.push({
|
|
360
|
+
type: "toolcall_end",
|
|
361
|
+
contentIndex: slot.contentIndex,
|
|
362
|
+
toolCall: slot.block,
|
|
363
|
+
partial: output
|
|
364
|
+
});
|
|
365
|
+
outputSlots.delete(event.output_index);
|
|
366
|
+
}
|
|
367
|
+
} else if (event.type === "response.completed" || event.type === "response.incomplete") {
|
|
368
|
+
finalizeResponse(event.response);
|
|
369
|
+
} else if (event.type === "error") {
|
|
370
|
+
throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
|
|
371
|
+
} else if (event.type === "response.failed") {
|
|
372
|
+
finalizeResponse(event.response);
|
|
373
|
+
const error = event.response?.error;
|
|
374
|
+
const details = event.response?.incomplete_details;
|
|
375
|
+
const msg = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
|
|
376
|
+
throw new Error(msg);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (!sawTerminalResponseEvent) {
|
|
380
|
+
throw new Error("OpenAI Responses stream ended before a terminal response event");
|
|
381
|
+
}
|
|
382
|
+
for (const block of unfinishedToolCalls) {
|
|
383
|
+
if (output.stopReason === "toolUse") {
|
|
384
|
+
throw new Error(`OpenAI Responses stream completed with an unfinished tool call: ${block.name} (${block.id})`);
|
|
385
|
+
}
|
|
386
|
+
Object.assign(block, unfinishedToolCallArguments2(block.partialJson ?? block.customInput?.input ?? ""));
|
|
387
|
+
stripStreamingScratchState2([block]);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
function mapStopReason(status, incompleteReason) {
|
|
391
|
+
if (!status)
|
|
392
|
+
return { stopReason: "stop" };
|
|
393
|
+
switch (status) {
|
|
394
|
+
case "completed":
|
|
395
|
+
return { stopReason: "stop" };
|
|
396
|
+
case "incomplete":
|
|
397
|
+
if (incompleteReason === "max_output_tokens") {
|
|
398
|
+
return { stopReason: "length" };
|
|
399
|
+
}
|
|
400
|
+
return {
|
|
401
|
+
stopReason: "error",
|
|
402
|
+
errorMessage: incompleteReason ? `Response incomplete: ${incompleteReason}` : "Response incomplete without a provider reason"
|
|
403
|
+
};
|
|
404
|
+
case "failed":
|
|
405
|
+
case "cancelled":
|
|
406
|
+
return { stopReason: "error" };
|
|
407
|
+
case "in_progress":
|
|
408
|
+
case "queued":
|
|
409
|
+
return { stopReason: "stop" };
|
|
410
|
+
default: {
|
|
411
|
+
const _exhaustive = status;
|
|
412
|
+
throw new Error(`Unhandled stop reason: ${_exhaustive}`);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export { withPriorityTier2, ResponsesUsageError2, processResponsesStream2 };
|