@nyte-ai/ai 0.0.0-stage → 0.0.19
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 +866 -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-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-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-wtg64ez7.js +69 -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-yzyp0c2h.js +251 -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,114 @@
|
|
|
1
|
+
// src/utils/estimate.ts
|
|
2
|
+
import { getSystemMessageText } from "@nyte-ai/schema/model";
|
|
3
|
+
var CHARS_PER_TOKEN = 4;
|
|
4
|
+
var ESTIMATED_IMAGE_CHARS = 4800;
|
|
5
|
+
function calculateContextTokens2(usage) {
|
|
6
|
+
return usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
|
|
7
|
+
}
|
|
8
|
+
function safeJsonStringify(value) {
|
|
9
|
+
try {
|
|
10
|
+
return JSON.stringify(value) ?? "undefined";
|
|
11
|
+
} catch {
|
|
12
|
+
return "[unserializable]";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function estimateTextAndImageContentChars(content) {
|
|
16
|
+
if (!Array.isArray(content))
|
|
17
|
+
return content.length;
|
|
18
|
+
let chars = 0;
|
|
19
|
+
for (const block of content)
|
|
20
|
+
chars += block.type === "text" ? block.text.length : ESTIMATED_IMAGE_CHARS;
|
|
21
|
+
return chars;
|
|
22
|
+
}
|
|
23
|
+
function estimateTextTokens2(text) {
|
|
24
|
+
return Math.ceil(text.length / CHARS_PER_TOKEN);
|
|
25
|
+
}
|
|
26
|
+
function estimateTextAndImageContentTokens2(content) {
|
|
27
|
+
return Math.ceil(estimateTextAndImageContentChars(content) / CHARS_PER_TOKEN);
|
|
28
|
+
}
|
|
29
|
+
function estimateMessageTokens2(message) {
|
|
30
|
+
let chars = 0;
|
|
31
|
+
if (message.role === "system") {
|
|
32
|
+
return estimateTextTokens2(getSystemMessageText(message)) + estimateToolsTokens(message.toolsAdded) + estimateToolsTokens(message.toolsRemoved);
|
|
33
|
+
}
|
|
34
|
+
if (message.role === "user")
|
|
35
|
+
return estimateTextAndImageContentTokens2(message.content);
|
|
36
|
+
if (message.role === "toolResult")
|
|
37
|
+
return estimateTextAndImageContentTokens2(message.content);
|
|
38
|
+
for (const block of message.content) {
|
|
39
|
+
if (block.type === "text") {
|
|
40
|
+
chars += block.text.length;
|
|
41
|
+
} else if (block.type === "thinking") {
|
|
42
|
+
chars += block.thinking.length;
|
|
43
|
+
} else {
|
|
44
|
+
chars += block.name.length + safeJsonStringify(block.arguments).length;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return Math.ceil(chars / CHARS_PER_TOKEN);
|
|
48
|
+
}
|
|
49
|
+
function getLastAssistantUsageInfo(messages) {
|
|
50
|
+
let latestPrefixTimestamp = Number.NEGATIVE_INFINITY;
|
|
51
|
+
let usageInfo;
|
|
52
|
+
for (let i = 0;i < messages.length; i++) {
|
|
53
|
+
const message = messages[i];
|
|
54
|
+
if (message.role === "assistant") {
|
|
55
|
+
const usageAppliesToPrefix = message.timestamp >= latestPrefixTimestamp;
|
|
56
|
+
if (usageAppliesToPrefix && message.stopReason !== "aborted" && message.stopReason !== "error" && calculateContextTokens2(message.usage) > 0) {
|
|
57
|
+
usageInfo = { usage: message.usage, index: i };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
latestPrefixTimestamp = Math.max(latestPrefixTimestamp, message.timestamp);
|
|
61
|
+
}
|
|
62
|
+
return usageInfo;
|
|
63
|
+
}
|
|
64
|
+
function estimateMessages(messages) {
|
|
65
|
+
const usageInfo = getLastAssistantUsageInfo(messages);
|
|
66
|
+
if (usageInfo) {
|
|
67
|
+
const usageTokens = calculateContextTokens2(usageInfo.usage);
|
|
68
|
+
let trailingTokens = 0;
|
|
69
|
+
for (let i = usageInfo.index + 1;i < messages.length; i++) {
|
|
70
|
+
trailingTokens += estimateMessageTokens2(messages[i]);
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
tokens: usageTokens + trailingTokens,
|
|
74
|
+
usageTokens,
|
|
75
|
+
trailingTokens,
|
|
76
|
+
lastUsageIndex: usageInfo.index
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
let tokens = 0;
|
|
80
|
+
for (const message of messages)
|
|
81
|
+
tokens += estimateMessageTokens2(message);
|
|
82
|
+
return { tokens, usageTokens: 0, trailingTokens: tokens, lastUsageIndex: null };
|
|
83
|
+
}
|
|
84
|
+
function estimateToolsTokens(tools) {
|
|
85
|
+
if (!tools || tools.length === 0)
|
|
86
|
+
return 0;
|
|
87
|
+
return estimateTextTokens2(safeJsonStringify(tools));
|
|
88
|
+
}
|
|
89
|
+
function estimateContextTokens2(context) {
|
|
90
|
+
if (!("messages" in context))
|
|
91
|
+
return estimateMessages(context);
|
|
92
|
+
const estimate = estimateMessages(context.messages);
|
|
93
|
+
if (!("systemPrompt" in context) && !("tools" in context))
|
|
94
|
+
return estimate;
|
|
95
|
+
if (estimate.lastUsageIndex !== null) {
|
|
96
|
+
const addedNames = new Set(context.messages.slice(estimate.lastUsageIndex + 1).filter((message) => message.role === "toolResult").flatMap((message) => message.addedToolNames ?? []));
|
|
97
|
+
const addedToolTokens = estimateToolsTokens(context.tools?.filter((tool) => addedNames.has(tool.name)));
|
|
98
|
+
return {
|
|
99
|
+
tokens: estimate.tokens + addedToolTokens,
|
|
100
|
+
usageTokens: estimate.usageTokens,
|
|
101
|
+
trailingTokens: estimate.trailingTokens + addedToolTokens,
|
|
102
|
+
lastUsageIndex: estimate.lastUsageIndex
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const prefixTokens = (context.systemPrompt ? estimateTextTokens2(context.systemPrompt) : 0) + estimateToolsTokens(context.tools);
|
|
106
|
+
return {
|
|
107
|
+
tokens: estimate.tokens + prefixTokens,
|
|
108
|
+
usageTokens: estimate.usageTokens,
|
|
109
|
+
trailingTokens: estimate.trailingTokens + prefixTokens,
|
|
110
|
+
lastUsageIndex: estimate.lastUsageIndex
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export { calculateContextTokens2, estimateTextTokens2, estimateTextAndImageContentTokens2, estimateMessageTokens2, estimateContextTokens2 };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/api/openai-codex-compaction-error.ts
|
|
2
|
+
class OpenAICodexCompactionError2 extends Error {
|
|
3
|
+
usage;
|
|
4
|
+
constructor(message, usage) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "OpenAICodexCompactionError";
|
|
7
|
+
this.usage = usage;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { OpenAICodexCompactionError2 };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/providers/account-limits.ts
|
|
2
|
+
import { Value } from "typebox/value";
|
|
3
|
+
function limitWindow2(id, label, usedPercent, resetsAt, windowMinutes) {
|
|
4
|
+
const window = {
|
|
5
|
+
id,
|
|
6
|
+
label,
|
|
7
|
+
usedPercent: Math.max(0, Math.min(100, usedPercent))
|
|
8
|
+
};
|
|
9
|
+
if (resetsAt !== undefined)
|
|
10
|
+
window.resetsAt = resetsAt;
|
|
11
|
+
if (windowMinutes !== undefined)
|
|
12
|
+
window.windowMinutes = windowMinutes;
|
|
13
|
+
return window;
|
|
14
|
+
}
|
|
15
|
+
async function fetchAccountUsage2(url, headers, options, schema, label) {
|
|
16
|
+
const timeoutSignal = options?.timeoutMs !== undefined && options.timeoutMs > 0 ? AbortSignal.timeout(Math.floor(options.timeoutMs)) : undefined;
|
|
17
|
+
const signal = AbortSignal.any([options?.signal, timeoutSignal].filter((candidate) => candidate !== undefined));
|
|
18
|
+
const response = await (options?.fetch ?? globalThis.fetch)(url, {
|
|
19
|
+
method: "GET",
|
|
20
|
+
headers,
|
|
21
|
+
signal
|
|
22
|
+
});
|
|
23
|
+
if (!response.ok) {
|
|
24
|
+
const text = await response.text().catch(() => "");
|
|
25
|
+
throw new Error(`${label} usage request failed (${String(response.status)}): ${text || response.statusText}`);
|
|
26
|
+
}
|
|
27
|
+
const decoded = await response.json();
|
|
28
|
+
if (!Value.Check(schema, decoded))
|
|
29
|
+
throw new Error(`${label} usage response was not an object`);
|
|
30
|
+
return decoded;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { limitWindow2, fetchAccountUsage2 };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
operationSignal2,
|
|
3
|
+
raceWithAbortSignal2
|
|
4
|
+
} from "./account-limits-cache-j2bt392s.js";
|
|
5
|
+
|
|
6
|
+
// src/auth/credential-store.ts
|
|
7
|
+
import { keyedSerial } from "@nyte-ai/schema/serial";
|
|
8
|
+
class InMemoryCredentialStore2 {
|
|
9
|
+
credentials = new Map;
|
|
10
|
+
writes = keyedSerial();
|
|
11
|
+
enqueue(providerId, task, options) {
|
|
12
|
+
const signal = operationSignal2(options?.signal);
|
|
13
|
+
const queued = this.writes.run(providerId, () => {
|
|
14
|
+
signal.throwIfAborted();
|
|
15
|
+
return task();
|
|
16
|
+
});
|
|
17
|
+
return raceWithAbortSignal2(queued, signal);
|
|
18
|
+
}
|
|
19
|
+
async read(providerId, options) {
|
|
20
|
+
options?.signal?.throwIfAborted();
|
|
21
|
+
return this.credentials.get(providerId);
|
|
22
|
+
}
|
|
23
|
+
async list(options) {
|
|
24
|
+
options?.signal?.throwIfAborted();
|
|
25
|
+
return [...this.credentials].flatMap(([providerId, stored]) => stored.accounts.map((account) => ({
|
|
26
|
+
providerId,
|
|
27
|
+
id: account.id,
|
|
28
|
+
label: account.label,
|
|
29
|
+
type: account.credential.type,
|
|
30
|
+
active: stored.active === account.id
|
|
31
|
+
})));
|
|
32
|
+
}
|
|
33
|
+
modify(providerId, fn, options) {
|
|
34
|
+
return this.enqueue(providerId, async () => {
|
|
35
|
+
const current = this.credentials.get(providerId);
|
|
36
|
+
const next = await fn(current);
|
|
37
|
+
if (next !== undefined) {
|
|
38
|
+
if (next.accounts.length === 0)
|
|
39
|
+
this.credentials.delete(providerId);
|
|
40
|
+
else
|
|
41
|
+
this.credentials.set(providerId, next);
|
|
42
|
+
}
|
|
43
|
+
return next ?? current;
|
|
44
|
+
}, options);
|
|
45
|
+
}
|
|
46
|
+
delete(providerId, options) {
|
|
47
|
+
return this.enqueue(providerId, async () => {
|
|
48
|
+
this.credentials.delete(providerId);
|
|
49
|
+
}, options);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { InMemoryCredentialStore2 };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/model-pricing.ts
|
|
2
|
+
var ANTHROPIC_FAST_MODE_COST_MULTIPLIER2 = 2;
|
|
3
|
+
function getServiceTierCostMultiplier(model, serviceTier) {
|
|
4
|
+
switch (serviceTier) {
|
|
5
|
+
case "flex":
|
|
6
|
+
return 0.5;
|
|
7
|
+
case "priority":
|
|
8
|
+
case "fast":
|
|
9
|
+
return model.id === "gpt-5.5" ? 2.5 : 2;
|
|
10
|
+
default:
|
|
11
|
+
return 1;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function applyServiceTierPricing2(usage, serviceTier, model) {
|
|
15
|
+
const multiplier = getServiceTierCostMultiplier(model, serviceTier ?? undefined);
|
|
16
|
+
if (multiplier === 1)
|
|
17
|
+
return;
|
|
18
|
+
usage.cost.input *= multiplier;
|
|
19
|
+
usage.cost.output *= multiplier;
|
|
20
|
+
usage.cost.cacheRead *= multiplier;
|
|
21
|
+
usage.cost.cacheWrite *= multiplier;
|
|
22
|
+
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
23
|
+
}
|
|
24
|
+
function getFastModeCostMultiplier2(model) {
|
|
25
|
+
switch (model.api) {
|
|
26
|
+
case "openai-responses":
|
|
27
|
+
case "openai-codex-responses":
|
|
28
|
+
return getServiceTierCostMultiplier(model, "priority");
|
|
29
|
+
case "anthropic-messages":
|
|
30
|
+
return model.provider === "anthropic" ? ANTHROPIC_FAST_MODE_COST_MULTIPLIER2 : undefined;
|
|
31
|
+
default:
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { ANTHROPIC_FAST_MODE_COST_MULTIPLIER2, applyServiceTierPricing2, getFastModeCostMultiplier2 };
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// src/utils/json-parse.ts
|
|
2
|
+
import { parse as partialParse } from "partial-json";
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
import { Value } from "typebox/value";
|
|
5
|
+
var ToolArgumentsSchema = Type.Record(Type.String(), Type.Unknown());
|
|
6
|
+
var VALID_JSON_ESCAPES = new Set(['"', "\\", "/", "b", "f", "n", "r", "t", "u"]);
|
|
7
|
+
function escapeControlCharacter(char) {
|
|
8
|
+
switch (char) {
|
|
9
|
+
case "\b":
|
|
10
|
+
return "\\b";
|
|
11
|
+
case "\f":
|
|
12
|
+
return "\\f";
|
|
13
|
+
case `
|
|
14
|
+
`:
|
|
15
|
+
return "\\n";
|
|
16
|
+
case "\r":
|
|
17
|
+
return "\\r";
|
|
18
|
+
case "\t":
|
|
19
|
+
return "\\t";
|
|
20
|
+
default:
|
|
21
|
+
return `\\u${char.codePointAt(0)?.toString(16).padStart(4, "0") ?? "0000"}`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function repairJson2(json) {
|
|
25
|
+
let repaired = "";
|
|
26
|
+
let inString = false;
|
|
27
|
+
for (let index = 0;index < json.length; index++) {
|
|
28
|
+
const char = json[index];
|
|
29
|
+
if (!inString) {
|
|
30
|
+
repaired += char;
|
|
31
|
+
if (char === '"') {
|
|
32
|
+
inString = true;
|
|
33
|
+
}
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (char === '"') {
|
|
37
|
+
repaired += char;
|
|
38
|
+
inString = false;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (char === "\\") {
|
|
42
|
+
const nextChar = json[index + 1];
|
|
43
|
+
if (nextChar === undefined) {
|
|
44
|
+
repaired += "\\\\";
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (nextChar === "u") {
|
|
48
|
+
const unicodeDigits = json.slice(index + 2, index + 6);
|
|
49
|
+
if (/^[0-9a-fA-F]{4}$/.test(unicodeDigits)) {
|
|
50
|
+
repaired += `\\u${unicodeDigits}`;
|
|
51
|
+
index += 5;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (VALID_JSON_ESCAPES.has(nextChar)) {
|
|
56
|
+
repaired += `\\${nextChar}`;
|
|
57
|
+
index += 1;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
repaired += "\\\\";
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const codePoint = char.codePointAt(0) ?? 32;
|
|
64
|
+
repaired += codePoint <= 31 ? escapeControlCharacter(char) : char;
|
|
65
|
+
}
|
|
66
|
+
return repaired;
|
|
67
|
+
}
|
|
68
|
+
function parseStreamingJson2(partialJson) {
|
|
69
|
+
if (!partialJson || partialJson.trim() === "")
|
|
70
|
+
return {};
|
|
71
|
+
let value;
|
|
72
|
+
try {
|
|
73
|
+
value = JSON.parse(partialJson);
|
|
74
|
+
} catch {
|
|
75
|
+
const repaired = repairJson2(partialJson);
|
|
76
|
+
try {
|
|
77
|
+
value = JSON.parse(repaired);
|
|
78
|
+
} catch {
|
|
79
|
+
try {
|
|
80
|
+
value = partialParse(repaired);
|
|
81
|
+
} catch {
|
|
82
|
+
return {};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return Value.Check(ToolArgumentsSchema, value) ? value : {};
|
|
87
|
+
}
|
|
88
|
+
function parseToolCallArguments2(json) {
|
|
89
|
+
if (!json || json.trim() === "")
|
|
90
|
+
return { arguments: {} };
|
|
91
|
+
let value;
|
|
92
|
+
try {
|
|
93
|
+
value = JSON.parse(json);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
try {
|
|
96
|
+
value = JSON.parse(repairJson2(json));
|
|
97
|
+
} catch {
|
|
98
|
+
return unparsedToolCallArguments(error instanceof Error ? error.message : String(error), json);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return Value.Check(ToolArgumentsSchema, value) ? { arguments: value } : unparsedToolCallArguments("Tool arguments must be a JSON object", json);
|
|
102
|
+
}
|
|
103
|
+
function unfinishedToolCallArguments2(json) {
|
|
104
|
+
return unparsedToolCallArguments("The response ended before this tool call finished", json);
|
|
105
|
+
}
|
|
106
|
+
function unparsedToolCallArguments(reason, json) {
|
|
107
|
+
return { arguments: {}, argumentsError: `${reason}
|
|
108
|
+
|
|
109
|
+
Received arguments:
|
|
110
|
+
${json}` };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export { repairJson2, parseStreamingJson2, parseToolCallArguments2, unfinishedToolCallArguments2 };
|