@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,578 @@
|
|
|
1
|
+
import {
|
|
2
|
+
calculateCost2,
|
|
3
|
+
clampThinkingLevel2
|
|
4
|
+
} from "../../chunks/account-limits-cache-zgzptd0z.js";
|
|
5
|
+
import {
|
|
6
|
+
ANTHROPIC_FAST_MODE_COST_MULTIPLIER2
|
|
7
|
+
} from "../../chunks/account-limits-cache-f9z00x8c.js";
|
|
8
|
+
import"../../chunks/account-limits-cache-b23sbrke.js";
|
|
9
|
+
import {
|
|
10
|
+
resolveCacheRetention2
|
|
11
|
+
} from "../../chunks/account-limits-cache-d2a51dka.js";
|
|
12
|
+
import {
|
|
13
|
+
splitDeferredTools2
|
|
14
|
+
} from "../../chunks/account-limits-cache-8s8bz4dr.js";
|
|
15
|
+
import {
|
|
16
|
+
headersToRecord2
|
|
17
|
+
} from "../../chunks/account-limits-cache-ag5b8dja.js";
|
|
18
|
+
import {
|
|
19
|
+
repairJson2,
|
|
20
|
+
parseStreamingJson2,
|
|
21
|
+
parseToolCallArguments2
|
|
22
|
+
} from "../../chunks/account-limits-cache-fbkaq46e.js";
|
|
23
|
+
import"../../chunks/account-limits-cache-j2bt392s.js";
|
|
24
|
+
import {
|
|
25
|
+
retryProviderRequest2
|
|
26
|
+
} from "../../chunks/account-limits-cache-nbhht7xz.js";
|
|
27
|
+
import {
|
|
28
|
+
iterateServerSentEvents2
|
|
29
|
+
} from "../../chunks/account-limits-cache-me1x57bs.js";
|
|
30
|
+
import {
|
|
31
|
+
sanitizeSurrogates2
|
|
32
|
+
} from "../../chunks/account-limits-cache-eqwweapz.js";
|
|
33
|
+
import {
|
|
34
|
+
getSystemMessageText2
|
|
35
|
+
} from "../../chunks/account-limits-cache-b4edaybk.js";
|
|
36
|
+
import {
|
|
37
|
+
getCurrentTools2,
|
|
38
|
+
getDeclaredTools2,
|
|
39
|
+
getInitialSystemMessage2,
|
|
40
|
+
hasToolRedefinitions2,
|
|
41
|
+
resolveTranscript2
|
|
42
|
+
} from "../../chunks/account-limits-cache-6250ykn6.js";
|
|
43
|
+
import"../../chunks/account-limits-cache-zcbkmrvy.js";
|
|
44
|
+
import"../../chunks/account-limits-cache-nn9jebsj.js";
|
|
45
|
+
import"../../chunks/account-limits-cache-a6cgeagz.js";
|
|
46
|
+
import {
|
|
47
|
+
runAssistantStream2
|
|
48
|
+
} from "../../chunks/account-limits-cache-fsktnetm.js";
|
|
49
|
+
import"../../chunks/account-limits-cache-736tdyme.js";
|
|
50
|
+
import {
|
|
51
|
+
AnthropicEventSchema2,
|
|
52
|
+
AnthropicEventTypeSchema2,
|
|
53
|
+
IgnoredAnthropicEventSchema2
|
|
54
|
+
} from "../../chunks/account-limits-cache-3ykqmnj5.js";
|
|
55
|
+
import {
|
|
56
|
+
buildCopilotDynamicHeaders2
|
|
57
|
+
} from "../../chunks/account-limits-cache-za42x8a3.js";
|
|
58
|
+
import"../../chunks/account-limits-cache-dejeygdt.js";
|
|
59
|
+
import {
|
|
60
|
+
clampMaxTokensToContext2,
|
|
61
|
+
buildBaseOptions2,
|
|
62
|
+
adjustMaxTokensForThinking2
|
|
63
|
+
} from "../../chunks/account-limits-cache-k0pkzhbx.js";
|
|
64
|
+
import {
|
|
65
|
+
transformMessages2
|
|
66
|
+
} from "../../chunks/account-limits-cache-8qzk56vj.js";
|
|
67
|
+
import {
|
|
68
|
+
toClaudeCodeName2,
|
|
69
|
+
fromClaudeCodeName2
|
|
70
|
+
} from "../../chunks/account-limits-cache-ks1sjym9.js";
|
|
71
|
+
import"../../chunks/account-limits-cache-ehy0ze2b.js";
|
|
72
|
+
import {
|
|
73
|
+
claudeHeaderLimits2
|
|
74
|
+
} from "../../chunks/account-limits-cache-re8hpzwn.js";
|
|
75
|
+
import"../../chunks/account-limits-cache-hyzn1jmk.js";
|
|
76
|
+
import"../../chunks/account-limits-cache-gknnm1kv.js";
|
|
77
|
+
import {
|
|
78
|
+
FAST_MODE_BETA2,
|
|
79
|
+
MID_CONVERSATION_TOOL_CHANGES_BETA2,
|
|
80
|
+
shouldUseServerSideFallbackBeta2,
|
|
81
|
+
getAnthropicCompat2,
|
|
82
|
+
mergeAnthropicHeaders2,
|
|
83
|
+
appendAnthropicBeta2,
|
|
84
|
+
assertRequestAuth2,
|
|
85
|
+
getAnthropicFederation2,
|
|
86
|
+
createClient2,
|
|
87
|
+
shouldUseFineGrainedToolStreamingBeta2
|
|
88
|
+
} from "../../chunks/account-limits-cache-75smws02.js";
|
|
89
|
+
import"../../chunks/account-limits-cache-h92bzz1j.js";
|
|
90
|
+
import {
|
|
91
|
+
normalizeToolCallId2,
|
|
92
|
+
convertMessages3,
|
|
93
|
+
convertTools3
|
|
94
|
+
} from "../../chunks/account-limits-cache-4b8ckwqw.js";
|
|
95
|
+
|
|
96
|
+
// src/api/anthropic-messages.ts
|
|
97
|
+
import { Type } from "typebox";
|
|
98
|
+
import { Value } from "typebox/value";
|
|
99
|
+
function getCacheControl(model, cacheRetention, env) {
|
|
100
|
+
const retention = resolveCacheRetention2(cacheRetention, env);
|
|
101
|
+
if (retention === "none")
|
|
102
|
+
return;
|
|
103
|
+
const cacheControl = { type: "ephemeral" };
|
|
104
|
+
if (retention === "long" && getAnthropicCompat2(model).supportsLongCacheRetention) {
|
|
105
|
+
cacheControl.ttl = "1h";
|
|
106
|
+
}
|
|
107
|
+
return cacheControl;
|
|
108
|
+
}
|
|
109
|
+
var ANTHROPIC_EFFORTS = ["low", "medium", "high", "xhigh", "max"];
|
|
110
|
+
var DEFERRED_TOOL_PLACEHOLDER = {
|
|
111
|
+
name: "__pi_deferred_placeholder__",
|
|
112
|
+
description: "Reserved placeholder. Never available. Never call this.",
|
|
113
|
+
input_schema: { type: "object", properties: {}, required: [] },
|
|
114
|
+
defer_loading: true
|
|
115
|
+
};
|
|
116
|
+
function applyAnthropicFastModePricing(usage, fast) {
|
|
117
|
+
if (!fast)
|
|
118
|
+
return;
|
|
119
|
+
usage.cost.input *= ANTHROPIC_FAST_MODE_COST_MULTIPLIER2;
|
|
120
|
+
usage.cost.output *= ANTHROPIC_FAST_MODE_COST_MULTIPLIER2;
|
|
121
|
+
usage.cost.cacheRead *= ANTHROPIC_FAST_MODE_COST_MULTIPLIER2;
|
|
122
|
+
usage.cost.cacheWrite *= ANTHROPIC_FAST_MODE_COST_MULTIPLIER2;
|
|
123
|
+
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
124
|
+
}
|
|
125
|
+
function usesNativeToolChanges(model, context) {
|
|
126
|
+
const compat = getAnthropicCompat2(model);
|
|
127
|
+
const initialTools = getInitialSystemMessage2(context.messages)?.toolsAdded ?? [];
|
|
128
|
+
return compat.supportsMidConvoSystemMessages && compat.supportsMidConvoToolChanges && initialTools.length > 0 && !hasToolRedefinitions2(context.messages);
|
|
129
|
+
}
|
|
130
|
+
var AnthropicErrorEventSchema = Type.Object({
|
|
131
|
+
error: Type.Object({ type: Type.String(), message: Type.String() })
|
|
132
|
+
});
|
|
133
|
+
function anthropicErrorText(data) {
|
|
134
|
+
try {
|
|
135
|
+
const parsed = JSON.parse(data);
|
|
136
|
+
if (!Value.Check(AnthropicErrorEventSchema, parsed))
|
|
137
|
+
return data;
|
|
138
|
+
return parsed.error.message === "" ? parsed.error.type : parsed.error.message;
|
|
139
|
+
} catch {
|
|
140
|
+
return data;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function* iterateAnthropicEvents(response, signal) {
|
|
144
|
+
if (!response.body) {
|
|
145
|
+
throw new Error("Attempted to iterate over an Anthropic response with no body");
|
|
146
|
+
}
|
|
147
|
+
let sawMessageStart = false;
|
|
148
|
+
let sawMessageEnd = false;
|
|
149
|
+
for await (const sse of iterateServerSentEvents2(response.body, signal)) {
|
|
150
|
+
if (sse.event === "error")
|
|
151
|
+
throw new Error(anthropicErrorText(sse.data));
|
|
152
|
+
if (!Value.Check(AnthropicEventTypeSchema2, sse.event))
|
|
153
|
+
continue;
|
|
154
|
+
try {
|
|
155
|
+
let event;
|
|
156
|
+
try {
|
|
157
|
+
event = JSON.parse(sse.data);
|
|
158
|
+
} catch {
|
|
159
|
+
event = JSON.parse(repairJson2(sse.data));
|
|
160
|
+
}
|
|
161
|
+
if (Value.Check(IgnoredAnthropicEventSchema2, event) && event.type === sse.event)
|
|
162
|
+
continue;
|
|
163
|
+
if (!Value.Check(AnthropicEventSchema2, event)) {
|
|
164
|
+
throw new Error("Invalid Anthropic event body");
|
|
165
|
+
}
|
|
166
|
+
if (event.type !== sse.event) {
|
|
167
|
+
throw new Error(`Event type ${event.type} does not match ${sse.event}`);
|
|
168
|
+
}
|
|
169
|
+
if (event.type === "message_start") {
|
|
170
|
+
sawMessageStart = true;
|
|
171
|
+
} else if (event.type === "message_stop") {
|
|
172
|
+
sawMessageEnd = true;
|
|
173
|
+
}
|
|
174
|
+
yield event;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
177
|
+
throw new Error(`Could not parse Anthropic SSE event ${sse.event}: ${message}; data=${sse.data}; raw=${sse.raw.join("\\n")}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (sawMessageStart && !sawMessageEnd) {
|
|
181
|
+
throw new Error("Anthropic stream ended before message_stop");
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
var stream = (model, context, requested) => {
|
|
185
|
+
const options = requested?.fast === true && requested.speed === undefined ? { ...requested, speed: "fast" } : requested;
|
|
186
|
+
const normalizedContext = resolveTranscript2(context, getAnthropicCompat2(model).supportsMidConvoSystemMessages);
|
|
187
|
+
const currentTools = getCurrentTools2(normalizedContext.messages);
|
|
188
|
+
const nativeToolChanges = usesNativeToolChanges(model, normalizedContext);
|
|
189
|
+
return runAssistantStream2(model, options?.signal, "Anthropic", async (output, stream) => {
|
|
190
|
+
if (options?.speed === "fast" && !(model.provider === "anthropic" && model.modes?.includes("fast") === true)) {
|
|
191
|
+
throw new Error(`Anthropic fast mode is not available for ${model.provider}/${model.id}`);
|
|
192
|
+
}
|
|
193
|
+
let client;
|
|
194
|
+
let isOAuth;
|
|
195
|
+
let usageModel = model;
|
|
196
|
+
let usedFastMode = false;
|
|
197
|
+
if (options?.client) {
|
|
198
|
+
client = options.client;
|
|
199
|
+
isOAuth = false;
|
|
200
|
+
} else {
|
|
201
|
+
const apiKey = options?.apiKey;
|
|
202
|
+
const federation = getAnthropicFederation2(model, apiKey, options?.headers, options?.env);
|
|
203
|
+
if (!federation)
|
|
204
|
+
assertRequestAuth2(model.provider, apiKey, options?.headers);
|
|
205
|
+
let copilotDynamicHeaders;
|
|
206
|
+
if (model.provider === "github-copilot") {
|
|
207
|
+
copilotDynamicHeaders = buildCopilotDynamicHeaders2({
|
|
208
|
+
messages: normalizedContext.messages,
|
|
209
|
+
sessionId: options?.sessionId
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
const cacheRetention = resolveCacheRetention2(options?.cacheRetention, options?.env);
|
|
213
|
+
const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
|
|
214
|
+
const created = createClient2(model, apiKey, options?.interleavedThinking ?? true, shouldUseFineGrainedToolStreamingBeta2(model, normalizedContext), shouldUseServerSideFallbackBeta2(model), options?.speed === "fast", nativeToolChanges, options?.headers, options?.fetch, copilotDynamicHeaders, cacheSessionId, federation);
|
|
215
|
+
client = created.client;
|
|
216
|
+
isOAuth = created.isOAuthToken;
|
|
217
|
+
}
|
|
218
|
+
const params = buildParams(model, normalizedContext, isOAuth, nativeToolChanges, options);
|
|
219
|
+
await options?.onPayload?.(params, model);
|
|
220
|
+
const requestOptions = {
|
|
221
|
+
signal: options?.signal,
|
|
222
|
+
maxRetries: 0
|
|
223
|
+
};
|
|
224
|
+
if (options?.timeoutMs !== undefined)
|
|
225
|
+
requestOptions.timeout = options.timeoutMs;
|
|
226
|
+
if (options?.client) {
|
|
227
|
+
const requestBetas = [
|
|
228
|
+
...options.speed === "fast" ? [FAST_MODE_BETA2] : [],
|
|
229
|
+
...nativeToolChanges ? [MID_CONVERSATION_TOOL_CHANGES_BETA2] : []
|
|
230
|
+
];
|
|
231
|
+
if (requestBetas.length > 0) {
|
|
232
|
+
requestOptions.headers = requestBetas.reduce(appendAnthropicBeta2, mergeAnthropicHeaders2(model.headers, options.headers));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const response = await retryProviderRequest2(() => client.beta.messages.create({ ...params, stream: true }, requestOptions).asResponse(), {
|
|
236
|
+
maxRetries: options?.maxRetries,
|
|
237
|
+
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
238
|
+
signal: options?.signal
|
|
239
|
+
});
|
|
240
|
+
const responseHeaders = headersToRecord2(response.headers);
|
|
241
|
+
await options?.onResponse?.({ status: response.status, headers: responseHeaders }, model);
|
|
242
|
+
if (options?.onAccountLimits !== undefined) {
|
|
243
|
+
const limits = claudeHeaderLimits2(responseHeaders, model.provider);
|
|
244
|
+
if (limits !== undefined)
|
|
245
|
+
await options.onAccountLimits(limits, model);
|
|
246
|
+
}
|
|
247
|
+
stream.push({ type: "start", partial: output });
|
|
248
|
+
const blocks = new Map;
|
|
249
|
+
for await (const event of iterateAnthropicEvents(response, options?.signal)) {
|
|
250
|
+
if (event.type === "message_start") {
|
|
251
|
+
output.responseId = event.message.id;
|
|
252
|
+
const responseModel = event.message.model;
|
|
253
|
+
if (responseModel !== model.id)
|
|
254
|
+
output.responseModel = responseModel;
|
|
255
|
+
const fallbackCost = responseModel === model.id ? undefined : model.compat?.allowedFallbackModels?.find((fallback) => fallback.provider === model.provider && fallback.model === responseModel)?.cost;
|
|
256
|
+
usageModel = fallbackCost ? { ...model, id: responseModel, cost: fallbackCost } : model;
|
|
257
|
+
usedFastMode = event.message.usage.speed === "fast";
|
|
258
|
+
output.usage.input = event.message.usage.input_tokens || 0;
|
|
259
|
+
output.usage.output = event.message.usage.output_tokens || 0;
|
|
260
|
+
output.usage.cacheRead = event.message.usage.cache_read_input_tokens || 0;
|
|
261
|
+
output.usage.cacheWrite = event.message.usage.cache_creation_input_tokens || 0;
|
|
262
|
+
output.usage.cacheWrite1h = event.message.usage.cache_creation?.ephemeral_1h_input_tokens || 0;
|
|
263
|
+
output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
264
|
+
calculateCost2(usageModel, output.usage);
|
|
265
|
+
applyAnthropicFastModePricing(output.usage, usedFastMode);
|
|
266
|
+
} else if (event.type === "content_block_start") {
|
|
267
|
+
const content = event.content_block;
|
|
268
|
+
let block;
|
|
269
|
+
switch (content.type) {
|
|
270
|
+
case "text":
|
|
271
|
+
block = { type: "text", text: content.text ?? "" };
|
|
272
|
+
break;
|
|
273
|
+
case "thinking":
|
|
274
|
+
block = {
|
|
275
|
+
type: "thinking",
|
|
276
|
+
thinking: content.thinking ?? "",
|
|
277
|
+
thinkingSignature: content.signature ?? ""
|
|
278
|
+
};
|
|
279
|
+
break;
|
|
280
|
+
case "redacted_thinking":
|
|
281
|
+
block = {
|
|
282
|
+
type: "thinking",
|
|
283
|
+
thinking: "[Reasoning redacted]",
|
|
284
|
+
thinkingSignature: content.data,
|
|
285
|
+
redacted: true
|
|
286
|
+
};
|
|
287
|
+
break;
|
|
288
|
+
case "tool_use":
|
|
289
|
+
block = {
|
|
290
|
+
type: "toolCall",
|
|
291
|
+
id: content.id,
|
|
292
|
+
name: isOAuth ? fromClaudeCodeName2(content.name, currentTools) : content.name,
|
|
293
|
+
arguments: content.input ?? {}
|
|
294
|
+
};
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
const contentIndex = output.content.push(block) - 1;
|
|
298
|
+
if (block.type === "toolCall") {
|
|
299
|
+
blocks.set(event.index, { kind: "toolCall", contentIndex, block, json: "" });
|
|
300
|
+
stream.push({ type: "toolcall_start", contentIndex, partial: output });
|
|
301
|
+
} else {
|
|
302
|
+
blocks.set(event.index, { kind: "content", contentIndex, block });
|
|
303
|
+
stream.push({
|
|
304
|
+
type: block.type === "text" ? "text_start" : "thinking_start",
|
|
305
|
+
contentIndex,
|
|
306
|
+
partial: output
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
} else if (event.type === "content_block_delta") {
|
|
310
|
+
const slot = blocks.get(event.index);
|
|
311
|
+
if (!slot)
|
|
312
|
+
continue;
|
|
313
|
+
const { block, contentIndex } = slot;
|
|
314
|
+
const delta = event.delta;
|
|
315
|
+
if (delta.type === "text_delta" && block.type === "text") {
|
|
316
|
+
block.text += delta.text;
|
|
317
|
+
stream.push({ type: "text_delta", contentIndex, delta: delta.text, partial: output });
|
|
318
|
+
} else if (delta.type === "thinking_delta" && block.type === "thinking") {
|
|
319
|
+
block.thinking += delta.thinking;
|
|
320
|
+
stream.push({
|
|
321
|
+
type: "thinking_delta",
|
|
322
|
+
contentIndex,
|
|
323
|
+
delta: delta.thinking,
|
|
324
|
+
partial: output
|
|
325
|
+
});
|
|
326
|
+
} else if (delta.type === "input_json_delta" && slot.kind === "toolCall") {
|
|
327
|
+
slot.json += delta.partial_json;
|
|
328
|
+
slot.block.arguments = parseStreamingJson2(slot.json);
|
|
329
|
+
stream.push({
|
|
330
|
+
type: "toolcall_delta",
|
|
331
|
+
contentIndex,
|
|
332
|
+
delta: delta.partial_json,
|
|
333
|
+
partial: output
|
|
334
|
+
});
|
|
335
|
+
} else if (delta.type === "signature_delta" && block.type === "thinking") {
|
|
336
|
+
block.thinkingSignature = (block.thinkingSignature ?? "") + delta.signature;
|
|
337
|
+
}
|
|
338
|
+
} else if (event.type === "content_block_stop") {
|
|
339
|
+
const slot = blocks.get(event.index);
|
|
340
|
+
if (!slot)
|
|
341
|
+
continue;
|
|
342
|
+
blocks.delete(event.index);
|
|
343
|
+
const { block, contentIndex } = slot;
|
|
344
|
+
if (slot.kind === "toolCall") {
|
|
345
|
+
if (slot.json.length > 0)
|
|
346
|
+
Object.assign(slot.block, parseToolCallArguments2(slot.json));
|
|
347
|
+
stream.push({
|
|
348
|
+
type: "toolcall_end",
|
|
349
|
+
contentIndex,
|
|
350
|
+
toolCall: slot.block,
|
|
351
|
+
partial: output
|
|
352
|
+
});
|
|
353
|
+
} else if (block.type === "text") {
|
|
354
|
+
stream.push({ type: "text_end", contentIndex, content: block.text, partial: output });
|
|
355
|
+
} else if (block.type === "thinking") {
|
|
356
|
+
stream.push({
|
|
357
|
+
type: "thinking_end",
|
|
358
|
+
contentIndex,
|
|
359
|
+
content: block.thinking,
|
|
360
|
+
partial: output
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
} else if (event.type === "message_delta") {
|
|
364
|
+
if (event.delta.stop_reason) {
|
|
365
|
+
output.rawStopReason = event.delta.stop_reason;
|
|
366
|
+
const stopReasonResult = mapStopReason(event.delta);
|
|
367
|
+
output.stopReason = stopReasonResult.stopReason;
|
|
368
|
+
if (stopReasonResult.errorMessage) {
|
|
369
|
+
output.errorMessage = stopReasonResult.errorMessage;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (event.usage) {
|
|
373
|
+
if (event.usage.input_tokens != null) {
|
|
374
|
+
output.usage.input = event.usage.input_tokens;
|
|
375
|
+
}
|
|
376
|
+
if (event.usage.output_tokens != null) {
|
|
377
|
+
output.usage.output = event.usage.output_tokens;
|
|
378
|
+
}
|
|
379
|
+
if (event.usage.cache_read_input_tokens != null) {
|
|
380
|
+
output.usage.cacheRead = event.usage.cache_read_input_tokens;
|
|
381
|
+
}
|
|
382
|
+
if (event.usage.cache_creation_input_tokens != null) {
|
|
383
|
+
output.usage.cacheWrite = event.usage.cache_creation_input_tokens;
|
|
384
|
+
}
|
|
385
|
+
const thinkingTokens = event.usage.output_tokens_details?.thinking_tokens;
|
|
386
|
+
if (thinkingTokens != null) {
|
|
387
|
+
output.usage.reasoning = thinkingTokens;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
391
|
+
calculateCost2(usageModel, output.usage);
|
|
392
|
+
applyAnthropicFastModePricing(output.usage, usedFastMode);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}, (error) => error instanceof Error ? error.message : JSON.stringify(error));
|
|
396
|
+
};
|
|
397
|
+
function mapThinkingLevelToEffort(model, level) {
|
|
398
|
+
const mapped = level ? model.thinkingLevelMap?.[level] : undefined;
|
|
399
|
+
const mappedEffort = ANTHROPIC_EFFORTS.find((effort) => effort === mapped);
|
|
400
|
+
if (mappedEffort !== undefined)
|
|
401
|
+
return mappedEffort;
|
|
402
|
+
switch (level) {
|
|
403
|
+
case "minimal":
|
|
404
|
+
case "low":
|
|
405
|
+
return "low";
|
|
406
|
+
case "medium":
|
|
407
|
+
return "medium";
|
|
408
|
+
case "high":
|
|
409
|
+
return "high";
|
|
410
|
+
default:
|
|
411
|
+
return "high";
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
var streamSimple = (model, context, options) => {
|
|
415
|
+
if (!getAnthropicFederation2(model, options?.apiKey, options?.headers, options?.env)) {
|
|
416
|
+
assertRequestAuth2(model.provider, options?.apiKey, options?.headers);
|
|
417
|
+
}
|
|
418
|
+
const base = buildBaseOptions2(model, context, options);
|
|
419
|
+
if (!options?.reasoning) {
|
|
420
|
+
return stream(model, context, {
|
|
421
|
+
...base,
|
|
422
|
+
thinkingEnabled: false
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
if (model.compat?.forceAdaptiveThinking === true) {
|
|
426
|
+
const effort = mapThinkingLevelToEffort(model, options.reasoning);
|
|
427
|
+
return stream(model, context, {
|
|
428
|
+
...base,
|
|
429
|
+
thinkingEnabled: true,
|
|
430
|
+
effort
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
const reasoning = clampThinkingLevel2(model, options.reasoning);
|
|
434
|
+
if (reasoning === "off") {
|
|
435
|
+
return stream(model, context, {
|
|
436
|
+
...base,
|
|
437
|
+
thinkingEnabled: false
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
const adjusted = adjustMaxTokensForThinking2(base.maxTokens, model.maxTokens, reasoning, options.thinkingBudgets);
|
|
441
|
+
const maxTokens = clampMaxTokensToContext2(model, context, adjusted.maxTokens);
|
|
442
|
+
return stream(model, context, {
|
|
443
|
+
...base,
|
|
444
|
+
maxTokens,
|
|
445
|
+
thinkingEnabled: true,
|
|
446
|
+
thinkingBudgetTokens: Math.min(adjusted.thinkingBudget, Math.max(0, maxTokens - 1024))
|
|
447
|
+
});
|
|
448
|
+
};
|
|
449
|
+
function systemBlock(text, cacheControl) {
|
|
450
|
+
const block = { type: "text", text };
|
|
451
|
+
if (cacheControl)
|
|
452
|
+
block.cache_control = cacheControl;
|
|
453
|
+
return block;
|
|
454
|
+
}
|
|
455
|
+
var AnthropicMetadataSchema = Type.Object({ user_id: Type.String() });
|
|
456
|
+
function buildParams(model, context, isOAuthToken, nativeToolChanges, options) {
|
|
457
|
+
const cacheControl = getCacheControl(model, options?.cacheRetention, options?.env);
|
|
458
|
+
const compat = getAnthropicCompat2(model);
|
|
459
|
+
const initialSystemMessage = getInitialSystemMessage2(context.messages);
|
|
460
|
+
const initialSystemText = initialSystemMessage ? getSystemMessageText2(initialSystemMessage) : "";
|
|
461
|
+
const transformedMessages = transformMessages2(context.messages, model, normalizeToolCallId2);
|
|
462
|
+
const conversationMessages = initialSystemMessage ? transformedMessages.slice(1) : transformedMessages;
|
|
463
|
+
const normalizeToolName = isOAuthToken ? toClaudeCodeName2 : (name) => name;
|
|
464
|
+
const initialTools = initialSystemMessage?.toolsAdded ?? [];
|
|
465
|
+
const toolPlacement = splitDeferredTools2({ messages: conversationMessages, tools: getCurrentTools2(context.messages) }, compat.supportsToolReferences && !nativeToolChanges, normalizeToolName);
|
|
466
|
+
let immediateTools = toolPlacement.immediate;
|
|
467
|
+
let deferredTools = [...toolPlacement.deferred.values()];
|
|
468
|
+
if (immediateTools.length === 0 && deferredTools.length > 0) {
|
|
469
|
+
immediateTools = deferredTools;
|
|
470
|
+
deferredTools = [];
|
|
471
|
+
}
|
|
472
|
+
const deferredToolNames = new Set(deferredTools.map((tool) => normalizeToolName(tool.name)));
|
|
473
|
+
const params = {
|
|
474
|
+
model: model.id,
|
|
475
|
+
messages: convertMessages3(conversationMessages, isOAuthToken, cacheControl, compat.allowEmptySignature, deferredToolNames, normalizeToolName, nativeToolChanges),
|
|
476
|
+
max_tokens: options?.maxTokens ?? model.maxTokens,
|
|
477
|
+
stream: true
|
|
478
|
+
};
|
|
479
|
+
if (options?.speed !== undefined) {
|
|
480
|
+
params.speed = options.speed;
|
|
481
|
+
}
|
|
482
|
+
if (isOAuthToken) {
|
|
483
|
+
params.system = [
|
|
484
|
+
systemBlock("You are Claude Code, Anthropic's official CLI for Claude.", cacheControl)
|
|
485
|
+
];
|
|
486
|
+
if (initialSystemText) {
|
|
487
|
+
params.system.push(systemBlock(sanitizeSurrogates2(initialSystemText), cacheControl));
|
|
488
|
+
}
|
|
489
|
+
} else if (initialSystemText) {
|
|
490
|
+
params.system = [systemBlock(sanitizeSurrogates2(initialSystemText), cacheControl)];
|
|
491
|
+
}
|
|
492
|
+
if (options?.temperature !== undefined && !options?.thinkingEnabled && compat.supportsTemperature) {
|
|
493
|
+
params.temperature = options.temperature;
|
|
494
|
+
}
|
|
495
|
+
const toolCacheControl = compat.supportsCacheControlOnTools ? cacheControl : undefined;
|
|
496
|
+
if (nativeToolChanges) {
|
|
497
|
+
const initialNames = new Set(initialTools.map((tool) => tool.name));
|
|
498
|
+
const laterTools = getDeclaredTools2(context.messages).filter((tool) => !initialNames.has(tool.name));
|
|
499
|
+
params.tools = [
|
|
500
|
+
...convertTools3(initialTools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools, toolCacheControl),
|
|
501
|
+
DEFERRED_TOOL_PLACEHOLDER,
|
|
502
|
+
...convertTools3(laterTools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools).map((tool) => ({ ...tool, defer_loading: true }))
|
|
503
|
+
];
|
|
504
|
+
} else if (immediateTools.length > 0 || deferredTools.length > 0) {
|
|
505
|
+
params.tools = [
|
|
506
|
+
...convertTools3(immediateTools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools, toolCacheControl),
|
|
507
|
+
...convertTools3(deferredTools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools, undefined, true)
|
|
508
|
+
];
|
|
509
|
+
}
|
|
510
|
+
if (model.reasoning) {
|
|
511
|
+
if (options?.thinkingEnabled) {
|
|
512
|
+
const display = options.thinkingDisplay ?? "summarized";
|
|
513
|
+
if (model.compat?.forceAdaptiveThinking === true) {
|
|
514
|
+
params.thinking = { type: "adaptive", display };
|
|
515
|
+
if (options.effort) {
|
|
516
|
+
params.output_config = { effort: options.effort };
|
|
517
|
+
}
|
|
518
|
+
} else {
|
|
519
|
+
params.thinking = {
|
|
520
|
+
type: "enabled",
|
|
521
|
+
budget_tokens: options.thinkingBudgetTokens || 1024,
|
|
522
|
+
display
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
} else if (options?.thinkingEnabled === false && model.thinkingLevelMap?.off !== null) {
|
|
526
|
+
params.thinking = { type: "disabled" };
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
const metadata = options?.metadata;
|
|
530
|
+
if (Value.Check(AnthropicMetadataSchema, metadata)) {
|
|
531
|
+
params.metadata = { user_id: metadata.user_id };
|
|
532
|
+
}
|
|
533
|
+
const toolChoice = options?.toolChoice;
|
|
534
|
+
switch (toolChoice) {
|
|
535
|
+
case undefined:
|
|
536
|
+
break;
|
|
537
|
+
case "auto":
|
|
538
|
+
case "any":
|
|
539
|
+
case "none":
|
|
540
|
+
params.tool_choice = { type: toolChoice };
|
|
541
|
+
break;
|
|
542
|
+
default:
|
|
543
|
+
params.tool_choice = toolChoice;
|
|
544
|
+
}
|
|
545
|
+
const allowedFallbackModels = model.compat?.allowedFallbackModels;
|
|
546
|
+
if (allowedFallbackModels && allowedFallbackModels.length > 0) {
|
|
547
|
+
params.fallbacks = allowedFallbackModels.map((fallback) => ({ model: fallback.model }));
|
|
548
|
+
}
|
|
549
|
+
return params;
|
|
550
|
+
}
|
|
551
|
+
function mapStopReason(delta) {
|
|
552
|
+
const reason = delta.stop_reason;
|
|
553
|
+
switch (reason) {
|
|
554
|
+
case "end_turn":
|
|
555
|
+
return { stopReason: "stop" };
|
|
556
|
+
case "max_tokens":
|
|
557
|
+
return { stopReason: "length" };
|
|
558
|
+
case "tool_use":
|
|
559
|
+
return { stopReason: "toolUse" };
|
|
560
|
+
case "refusal":
|
|
561
|
+
return {
|
|
562
|
+
stopReason: "error",
|
|
563
|
+
errorMessage: delta.stop_details?.explanation || `The model refused to complete the request`
|
|
564
|
+
};
|
|
565
|
+
case "pause_turn":
|
|
566
|
+
return { stopReason: "stop" };
|
|
567
|
+
case "stop_sequence":
|
|
568
|
+
return { stopReason: "stop" };
|
|
569
|
+
case "sensitive":
|
|
570
|
+
return { stopReason: "error", errorMessage: "Provider stopped with: sensitive" };
|
|
571
|
+
default:
|
|
572
|
+
throw new Error(`Unhandled stop reason: ${reason}`);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
export {
|
|
576
|
+
stream,
|
|
577
|
+
streamSimple
|
|
578
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
anthropicMessagesApi2
|
|
3
|
+
} from "../../chunks/account-limits-cache-zqdb4yh4.js";
|
|
4
|
+
import"../../chunks/account-limits-cache-90c9c2eb.js";
|
|
5
|
+
import"../../chunks/account-limits-cache-fsktnetm.js";
|
|
6
|
+
import"../../chunks/account-limits-cache-zcbkmrvy.js";
|
|
7
|
+
import"../../chunks/account-limits-cache-a6cgeagz.js";
|
|
8
|
+
import"../../chunks/account-limits-cache-nn9jebsj.js";
|
|
9
|
+
export {
|
|
10
|
+
anthropicMessagesApi2 as anthropicMessagesApi
|
|
11
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The envelope every adapter's `stream` shares: the empty output message, the async run behind
|
|
3
|
+
* the returned stream, and the success and failure endings of the event protocol.
|
|
4
|
+
*/
|
|
5
|
+
import type { Api, AssistantMessage, Model, Usage } from "../types.ts";
|
|
6
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
|
|
7
|
+
/** A cost of zero, for `calculateCost` to fill. */
|
|
8
|
+
export declare function emptyCost(): Usage["cost"];
|
|
9
|
+
/** The assistant message `model` starts from before any provider event arrives. */
|
|
10
|
+
export declare function emptyAssistantOutput(model: Model<Api>): AssistantMessage;
|
|
11
|
+
/**
|
|
12
|
+
* Strips the scratch fields adapters keep on tool-call blocks while arguments are still
|
|
13
|
+
* arriving. They are parsing state, never conversation content.
|
|
14
|
+
*/
|
|
15
|
+
export declare function stripStreamingScratchState(content: AssistantMessage["content"]): void;
|
|
16
|
+
/**
|
|
17
|
+
* Runs `body` behind the returned stream. `body` fills `output` and pushes `start` and content
|
|
18
|
+
* events; this ends the stream with `done` when `output` settled on a successful stop, and with
|
|
19
|
+
* `error` (`aborted` once `signal` fired) when `body` threw, the stream ended without a stop
|
|
20
|
+
* reason, or the provider stopped with an error. `label` names the provider in the missing-stop
|
|
21
|
+
* message and `formatError` turns the thrown value into `errorMessage`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function runAssistantStream(model: Model<Api>, signal: AbortSignal | undefined, label: string, body: (output: AssistantMessage, stream: AssistantMessageEventStream) => Promise<void>, formatError?: (error: unknown) => string): AssistantMessageEventStream;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
emptyCost2,
|
|
3
|
+
emptyAssistantOutput2,
|
|
4
|
+
stripStreamingScratchState2,
|
|
5
|
+
runAssistantStream2
|
|
6
|
+
} from "../../chunks/account-limits-cache-fsktnetm.js";
|
|
7
|
+
import"../../chunks/account-limits-cache-zcbkmrvy.js";
|
|
8
|
+
import"../../chunks/account-limits-cache-a6cgeagz.js";
|
|
9
|
+
import"../../chunks/account-limits-cache-nn9jebsj.js";
|
|
10
|
+
export {
|
|
11
|
+
emptyAssistantOutput2 as emptyAssistantOutput,
|
|
12
|
+
emptyCost2 as emptyCost,
|
|
13
|
+
runAssistantStream2 as runAssistantStream,
|
|
14
|
+
stripStreamingScratchState2 as stripStreamingScratchState
|
|
15
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude Code identity for Anthropic OAuth requests: the CLI version it
|
|
3
|
+
* announces, its tool names, and the token marker that selects it.
|
|
4
|
+
*/
|
|
5
|
+
import type { Tool } from "../types.ts";
|
|
6
|
+
export declare const claudeCodeVersion = "2.1.280";
|
|
7
|
+
/** A tool name as Claude Code spells it, or `name` when Claude Code has no such tool. */
|
|
8
|
+
export declare const toClaudeCodeName: (name: string) => string;
|
|
9
|
+
/** The caller's spelling of a tool name Claude Code sent, matched case-insensitively. */
|
|
10
|
+
export declare const fromClaudeCodeName: (name: string, tools?: Tool[]) => string;
|
|
11
|
+
/** A Claude subscription OAuth token, which signs requests in as Claude Code. */
|
|
12
|
+
export declare function usesClaudeSubscription(apiKey: string): boolean;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
claudeCodeVersion2,
|
|
3
|
+
toClaudeCodeName2,
|
|
4
|
+
fromClaudeCodeName2,
|
|
5
|
+
usesClaudeSubscription2
|
|
6
|
+
} from "../../chunks/account-limits-cache-ks1sjym9.js";
|
|
7
|
+
export {
|
|
8
|
+
claudeCodeVersion2 as claudeCodeVersion,
|
|
9
|
+
fromClaudeCodeName2 as fromClaudeCodeName,
|
|
10
|
+
toClaudeCodeName2 as toClaudeCodeName,
|
|
11
|
+
usesClaudeSubscription2 as usesClaudeSubscription
|
|
12
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Workers AI direct endpoint. */
|
|
2
|
+
export declare const CLOUDFLARE_WORKERS_AI_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/v1";
|
|
3
|
+
/** AI Gateway Unified API. https://developers.cloudflare.com/ai-gateway/usage/unified-api/ */
|
|
4
|
+
export declare const CLOUDFLARE_AI_GATEWAY_COMPAT_BASE_URL = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/compat";
|
|
5
|
+
/** AI Gateway → OpenAI passthrough. Used until /compat supports /v1/responses. */
|
|
6
|
+
export declare const CLOUDFLARE_AI_GATEWAY_OPENAI_BASE_URL = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/openai";
|
|
7
|
+
/** AI Gateway → Anthropic passthrough. */
|
|
8
|
+
export declare const CLOUDFLARE_AI_GATEWAY_ANTHROPIC_BASE_URL = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/anthropic";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/api/cloudflare.ts
|
|
2
|
+
var CLOUDFLARE_WORKERS_AI_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/v1";
|
|
3
|
+
var CLOUDFLARE_AI_GATEWAY_COMPAT_BASE_URL = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/compat";
|
|
4
|
+
var CLOUDFLARE_AI_GATEWAY_OPENAI_BASE_URL = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/openai";
|
|
5
|
+
var CLOUDFLARE_AI_GATEWAY_ANTHROPIC_BASE_URL = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/anthropic";
|
|
6
|
+
export {
|
|
7
|
+
CLOUDFLARE_AI_GATEWAY_ANTHROPIC_BASE_URL,
|
|
8
|
+
CLOUDFLARE_AI_GATEWAY_COMPAT_BASE_URL,
|
|
9
|
+
CLOUDFLARE_AI_GATEWAY_OPENAI_BASE_URL,
|
|
10
|
+
CLOUDFLARE_WORKERS_AI_BASE_URL
|
|
11
|
+
};
|