@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,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GITHUB_COPILOT_DEFAULT_ORIGIN2
|
|
3
|
+
} from "./account-limits-cache-za42x8a3.js";
|
|
4
|
+
|
|
5
|
+
// src/providers/github-copilot-catalog.ts
|
|
6
|
+
import { Type } from "typebox";
|
|
7
|
+
import { Value } from "typebox/value";
|
|
8
|
+
var CatalogResponse = Type.Object({ data: Type.Array(Type.Unknown()) });
|
|
9
|
+
var CatalogModel = Type.Object({
|
|
10
|
+
id: Type.String({ minLength: 1 }),
|
|
11
|
+
model_picker_enabled: Type.Optional(Type.Boolean()),
|
|
12
|
+
policy: Type.Optional(Type.Object({ state: Type.Optional(Type.String()) })),
|
|
13
|
+
capabilities: Type.Optional(Type.Object({
|
|
14
|
+
supports: Type.Optional(Type.Object({ tool_calls: Type.Optional(Type.Boolean()) }))
|
|
15
|
+
}))
|
|
16
|
+
});
|
|
17
|
+
function parseGitHubCopilotCatalog2(value, origin, knownModelIds) {
|
|
18
|
+
if (!Value.Check(CatalogResponse, value)) {
|
|
19
|
+
throw new Error("GitHub Copilot catalog response is not a model list");
|
|
20
|
+
}
|
|
21
|
+
const models = value.data.filter((entry) => Value.Check(CatalogModel, entry)).filter((model) => model.capabilities?.supports?.tool_calls !== false && model.policy?.state !== "disabled");
|
|
22
|
+
const individualFallback = origin === GITHUB_COPILOT_DEFAULT_ORIGIN2 && !models.some((model) => model.model_picker_enabled);
|
|
23
|
+
const visible = models.filter((model) => individualFallback || model.model_picker_enabled);
|
|
24
|
+
return {
|
|
25
|
+
availableModelIds: [
|
|
26
|
+
...new Set(visible.filter((model) => individualFallback ? model.policy?.state === "enabled" : model.policy?.state !== "unconfigured").map((model) => model.id))
|
|
27
|
+
],
|
|
28
|
+
needsApproval: visible.some((model) => model.policy?.state === "unconfigured" && knownModelIds.has(model.id))
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { parseGitHubCopilotCatalog2 };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
openAIResponsesApi2
|
|
3
|
+
} from "./account-limits-cache-q1ebtvet.js";
|
|
4
|
+
import {
|
|
5
|
+
envApiKeyAuth2
|
|
6
|
+
} from "./account-limits-cache-6x05n06z.js";
|
|
7
|
+
import {
|
|
8
|
+
verifyWithRequest2
|
|
9
|
+
} from "./account-limits-cache-ydq8p8qp.js";
|
|
10
|
+
import {
|
|
11
|
+
createProvider2
|
|
12
|
+
} from "./account-limits-cache-ykbqsvv6.js";
|
|
13
|
+
|
|
14
|
+
// src/providers/openai.ts
|
|
15
|
+
var OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
16
|
+
function openaiProvider2() {
|
|
17
|
+
return createProvider2({
|
|
18
|
+
id: "openai",
|
|
19
|
+
name: "OpenAI",
|
|
20
|
+
baseUrl: OPENAI_BASE_URL,
|
|
21
|
+
promptCache: {
|
|
22
|
+
minimumRetentionMs: { short: 5 * 60000, long: 24 * 60 * 60000 }
|
|
23
|
+
},
|
|
24
|
+
auth: {
|
|
25
|
+
apiKey: envApiKeyAuth2("OpenAI API key", ["OPENAI_API_KEY"]),
|
|
26
|
+
verify: ({ auth, signal }) => verifyWithRequest2({
|
|
27
|
+
provider: "OpenAI",
|
|
28
|
+
url: `${auth.baseUrl ?? OPENAI_BASE_URL}/models`,
|
|
29
|
+
headers: { ...auth.headers, Authorization: `Bearer ${auth.apiKey ?? ""}` },
|
|
30
|
+
signal
|
|
31
|
+
})
|
|
32
|
+
},
|
|
33
|
+
api: { "openai-responses": openAIResponsesApi2() }
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { openaiProvider2 };
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// src/api/constrained-sampling.ts
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import {
|
|
4
|
+
IsAdditionalProperties,
|
|
5
|
+
IsAnyOf,
|
|
6
|
+
IsConst,
|
|
7
|
+
IsEnum,
|
|
8
|
+
IsItems,
|
|
9
|
+
IsProperties,
|
|
10
|
+
IsRequired,
|
|
11
|
+
IsSchemaObject,
|
|
12
|
+
IsType
|
|
13
|
+
} from "typebox/schema";
|
|
14
|
+
import { Value } from "typebox/value";
|
|
15
|
+
|
|
16
|
+
class UnsupportedStrictJsonSchemaError extends Error {
|
|
17
|
+
}
|
|
18
|
+
var UNSUPPORTED_STRICT_SCHEMA_KEYS = [
|
|
19
|
+
"$ref",
|
|
20
|
+
"$defs",
|
|
21
|
+
"definitions",
|
|
22
|
+
"allOf",
|
|
23
|
+
"oneOf",
|
|
24
|
+
"patternProperties",
|
|
25
|
+
"dependentSchemas",
|
|
26
|
+
"dependencies",
|
|
27
|
+
"unevaluatedProperties",
|
|
28
|
+
"propertyNames",
|
|
29
|
+
"contains",
|
|
30
|
+
"prefixItems",
|
|
31
|
+
"not",
|
|
32
|
+
"if",
|
|
33
|
+
"then",
|
|
34
|
+
"else"
|
|
35
|
+
];
|
|
36
|
+
function schemaTypes(schema) {
|
|
37
|
+
return IsType(schema) ? [schema.type].flat() : [];
|
|
38
|
+
}
|
|
39
|
+
function describesObjectOrArray(schema) {
|
|
40
|
+
if (!IsSchemaObject(schema))
|
|
41
|
+
return false;
|
|
42
|
+
const types = schemaTypes(schema);
|
|
43
|
+
return types.includes("object") || types.includes("array") || "properties" in schema || "items" in schema;
|
|
44
|
+
}
|
|
45
|
+
function schemaAllowsNull(schema) {
|
|
46
|
+
if (!IsSchemaObject(schema))
|
|
47
|
+
return false;
|
|
48
|
+
if (schemaTypes(schema).includes("null"))
|
|
49
|
+
return true;
|
|
50
|
+
if (IsConst(schema) && schema.const === null || IsEnum(schema) && schema.enum.includes(null))
|
|
51
|
+
return true;
|
|
52
|
+
return IsAnyOf(schema) && schema.anyOf.some((variant) => schemaAllowsNull(variant));
|
|
53
|
+
}
|
|
54
|
+
function makeJsonSchemaNodeStrict(schema, isUnsupportedKeyword) {
|
|
55
|
+
if (!IsSchemaObject(schema)) {
|
|
56
|
+
throw new UnsupportedStrictJsonSchemaError("boolean schemas are unsupported");
|
|
57
|
+
}
|
|
58
|
+
for (const key of UNSUPPORTED_STRICT_SCHEMA_KEYS) {
|
|
59
|
+
if (key in schema) {
|
|
60
|
+
throw new UnsupportedStrictJsonSchemaError(`${key} schemas are unsupported`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (isUnsupportedKeyword) {
|
|
64
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
65
|
+
if (isUnsupportedKeyword(key, value)) {
|
|
66
|
+
throw new UnsupportedStrictJsonSchemaError(`${key}: ${JSON.stringify(value)} is unsupported`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if ("anyOf" in schema) {
|
|
71
|
+
if (!IsAnyOf(schema) || schema.anyOf.length === 0) {
|
|
72
|
+
throw new UnsupportedStrictJsonSchemaError("anyOf must contain at least one schema");
|
|
73
|
+
}
|
|
74
|
+
for (const variant of schema.anyOf) {
|
|
75
|
+
if (describesObjectOrArray(variant)) {
|
|
76
|
+
throw new UnsupportedStrictJsonSchemaError("object and array unions are unsupported");
|
|
77
|
+
}
|
|
78
|
+
makeJsonSchemaNodeStrict(variant, isUnsupportedKeyword);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if ("items" in schema) {
|
|
82
|
+
if (!IsItems(schema)) {
|
|
83
|
+
throw new UnsupportedStrictJsonSchemaError("items must be a schema");
|
|
84
|
+
}
|
|
85
|
+
if (Array.isArray(schema.items)) {
|
|
86
|
+
throw new UnsupportedStrictJsonSchemaError("tuple schemas are unsupported");
|
|
87
|
+
}
|
|
88
|
+
makeJsonSchemaNodeStrict(schema.items, isUnsupportedKeyword);
|
|
89
|
+
}
|
|
90
|
+
const isObjectSchema = IsType(schema) && schema.type === "object";
|
|
91
|
+
if ("properties" in schema && !isObjectSchema) {
|
|
92
|
+
throw new UnsupportedStrictJsonSchemaError("properties require type object");
|
|
93
|
+
}
|
|
94
|
+
if (!isObjectSchema)
|
|
95
|
+
return;
|
|
96
|
+
if ("additionalProperties" in schema && !(IsAdditionalProperties(schema) && schema.additionalProperties === false)) {
|
|
97
|
+
throw new UnsupportedStrictJsonSchemaError("schema-valued or true additionalProperties is unsupported");
|
|
98
|
+
}
|
|
99
|
+
if ("properties" in schema && !IsProperties(schema)) {
|
|
100
|
+
throw new UnsupportedStrictJsonSchemaError("object properties must be a schema map");
|
|
101
|
+
}
|
|
102
|
+
if ("required" in schema && !IsRequired(schema)) {
|
|
103
|
+
throw new UnsupportedStrictJsonSchemaError("object required must be a string array");
|
|
104
|
+
}
|
|
105
|
+
const properties = IsProperties(schema) ? schema.properties : {};
|
|
106
|
+
const propertyNames = Object.keys(properties);
|
|
107
|
+
const required = new Set(IsRequired(schema) ? schema.required : []);
|
|
108
|
+
if ([...required].some((key) => !propertyNames.includes(key))) {
|
|
109
|
+
throw new UnsupportedStrictJsonSchemaError("required contains an unknown property");
|
|
110
|
+
}
|
|
111
|
+
for (const [key, property] of Object.entries(properties)) {
|
|
112
|
+
makeJsonSchemaNodeStrict(property, isUnsupportedKeyword);
|
|
113
|
+
if (!required.has(key) && !schemaAllowsNull(property)) {
|
|
114
|
+
properties[key] = { anyOf: [property, { type: "null" }] };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
Object.assign(schema, { required: propertyNames, additionalProperties: false });
|
|
118
|
+
}
|
|
119
|
+
function makeStrictJsonSchema(schema, isUnsupportedKeyword) {
|
|
120
|
+
const cloned = structuredClone(schema);
|
|
121
|
+
if (!IsSchemaObject(cloned)) {
|
|
122
|
+
throw new UnsupportedStrictJsonSchemaError("root schema must have type object");
|
|
123
|
+
}
|
|
124
|
+
makeJsonSchemaNodeStrict(cloned, isUnsupportedKeyword);
|
|
125
|
+
if (!IsType(cloned) || cloned.type !== "object") {
|
|
126
|
+
throw new UnsupportedStrictJsonSchemaError("root schema must have type object");
|
|
127
|
+
}
|
|
128
|
+
return cloned;
|
|
129
|
+
}
|
|
130
|
+
function getJsonSchemaToolParameters2(tool, strict) {
|
|
131
|
+
return { ...strict === true ? makeStrictJsonSchema(tool.parameters) : tool.parameters };
|
|
132
|
+
}
|
|
133
|
+
function getGrammarToolInput2(toolName, arguments_, inputProperty) {
|
|
134
|
+
if (!Value.Check(Type.Object({ [inputProperty]: Type.String() }), arguments_)) {
|
|
135
|
+
throw new Error(`Grammar tool call "${toolName}" requires argument "${inputProperty}" to be a string.`);
|
|
136
|
+
}
|
|
137
|
+
return arguments_[inputProperty];
|
|
138
|
+
}
|
|
139
|
+
function appendGrammarToolInputJsonDelta2(buffer, inputProperty, nextInput, close) {
|
|
140
|
+
if (buffer.closed) {
|
|
141
|
+
if (close && nextInput === buffer.input)
|
|
142
|
+
return;
|
|
143
|
+
throw new Error(`grammar tool input for property "${inputProperty}" changed after it was closed`);
|
|
144
|
+
}
|
|
145
|
+
if (!nextInput.startsWith(buffer.input)) {
|
|
146
|
+
throw new Error(`grammar tool input for property "${inputProperty}" changed non-monotonically`);
|
|
147
|
+
}
|
|
148
|
+
const inputDelta = nextInput.slice(buffer.input.length);
|
|
149
|
+
if (!close && inputDelta.length === 0)
|
|
150
|
+
return;
|
|
151
|
+
let delta = "";
|
|
152
|
+
if (!buffer.started) {
|
|
153
|
+
delta += `{${JSON.stringify(inputProperty)}:"`;
|
|
154
|
+
buffer.started = true;
|
|
155
|
+
}
|
|
156
|
+
delta += JSON.stringify(inputDelta).slice(1, -1);
|
|
157
|
+
buffer.input = nextInput;
|
|
158
|
+
if (close) {
|
|
159
|
+
delta += '"}';
|
|
160
|
+
buffer.closed = true;
|
|
161
|
+
}
|
|
162
|
+
return delta;
|
|
163
|
+
}
|
|
164
|
+
function inferGrammarInputProperty(tool) {
|
|
165
|
+
const schema = tool.parameters;
|
|
166
|
+
if (!IsSchemaObject(schema) || !IsType(schema) || schema.type !== "object") {
|
|
167
|
+
throw new Error("grammar constrained sampling requires an object parameter schema");
|
|
168
|
+
}
|
|
169
|
+
if (!IsRequired(schema) || schema.required.length !== 1) {
|
|
170
|
+
throw new Error("grammar constrained sampling requires exactly one required string property");
|
|
171
|
+
}
|
|
172
|
+
const [inputProperty] = schema.required;
|
|
173
|
+
const property = IsProperties(schema) ? schema.properties[inputProperty] : undefined;
|
|
174
|
+
if (!property) {
|
|
175
|
+
throw new Error(`grammar constrained sampling requires a properties entry for ${inputProperty}`);
|
|
176
|
+
}
|
|
177
|
+
if (!IsSchemaObject(property) || !IsType(property) || property.type !== "string") {
|
|
178
|
+
throw new Error(`grammar constrained sampling property ${inputProperty} must have type string`);
|
|
179
|
+
}
|
|
180
|
+
return inputProperty;
|
|
181
|
+
}
|
|
182
|
+
function resolveJsonSchemaStrictSampling2(tool, supportsStrictMode, isUnsupportedKeyword) {
|
|
183
|
+
const config = tool.constrainedSampling;
|
|
184
|
+
if (!config || config.type !== "json_schema")
|
|
185
|
+
return;
|
|
186
|
+
if (supportsStrictMode) {
|
|
187
|
+
try {
|
|
188
|
+
makeStrictJsonSchema(tool.parameters, isUnsupportedKeyword);
|
|
189
|
+
return true;
|
|
190
|
+
} catch (error) {
|
|
191
|
+
if (!(error instanceof UnsupportedStrictJsonSchemaError))
|
|
192
|
+
throw error;
|
|
193
|
+
if (config.strict !== "require")
|
|
194
|
+
return;
|
|
195
|
+
throw new Error(`Tool "${tool.name}" requires JSON-schema constrained sampling, but ${error.message}.`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (config.strict === "require") {
|
|
199
|
+
throw new Error(`Tool "${tool.name}" requires JSON-schema constrained sampling, but strict tools are unsupported.`);
|
|
200
|
+
}
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
function resolveGrammarConstrainedSampling2(tool, supportsOpenAIGrammarTools) {
|
|
204
|
+
const config = tool.constrainedSampling;
|
|
205
|
+
if (!config || config.type !== "grammar") {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (!supportsOpenAIGrammarTools) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const larkDefinition = config.variants.openai_lark;
|
|
212
|
+
const regexDefinition = config.variants.openai_regex;
|
|
213
|
+
const variant = larkDefinition?.trim() ? { format: "lark", definition: larkDefinition } : regexDefinition?.trim() ? { format: "regex", definition: regexDefinition } : undefined;
|
|
214
|
+
if (!variant) {
|
|
215
|
+
throw new Error(`Tool "${tool.name}" cannot use grammar constrained sampling: no supported grammar variant was provided.`);
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
return { ...variant, inputProperty: inferGrammarInputProperty(tool) };
|
|
219
|
+
} catch (error) {
|
|
220
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
221
|
+
throw new Error(`Tool "${tool.name}" cannot use grammar constrained sampling: ${message}.`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function createGrammarToolInputProperties2(tools, supportsOpenAIGrammarTools) {
|
|
225
|
+
const properties = new Map;
|
|
226
|
+
for (const tool of tools ?? []) {
|
|
227
|
+
const grammar = resolveGrammarConstrainedSampling2(tool, supportsOpenAIGrammarTools);
|
|
228
|
+
if (grammar) {
|
|
229
|
+
properties.set(tool.name, grammar.inputProperty);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return properties;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export { getJsonSchemaToolParameters2, getGrammarToolInput2, appendGrammarToolInputJsonDelta2, resolveJsonSchemaStrictSampling2, resolveGrammarConstrainedSampling2, createGrammarToolInputProperties2 };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
operationSignal2,
|
|
3
|
+
raceWithAbortSignal2
|
|
4
|
+
} from "./account-limits-cache-j2bt392s.js";
|
|
5
|
+
|
|
6
|
+
// src/account-limits-cache.ts
|
|
7
|
+
var FRESHNESS_MS = 60000;
|
|
8
|
+
var RETRY_MS = 15000;
|
|
9
|
+
var TIMEOUT_MS = 15000;
|
|
10
|
+
function mergeLimits(entry, next, requestRevision = entry.revision) {
|
|
11
|
+
const previous = entry.limits;
|
|
12
|
+
const windows = new Map(previous?.windows.map((window) => [window.id, window]));
|
|
13
|
+
entry.revision += 1;
|
|
14
|
+
for (const window of next.windows) {
|
|
15
|
+
const observed = entry.observed.get(window.id);
|
|
16
|
+
if (observed !== undefined && (observed.at > next.observedAt || observed.revision > requestRevision))
|
|
17
|
+
continue;
|
|
18
|
+
windows.set(window.id, window);
|
|
19
|
+
entry.observed.set(window.id, { at: next.observedAt, revision: entry.revision });
|
|
20
|
+
}
|
|
21
|
+
entry.limits = {
|
|
22
|
+
...previous,
|
|
23
|
+
...next,
|
|
24
|
+
windows: [...windows.values()],
|
|
25
|
+
observedAt: Math.max(previous?.observedAt ?? 0, next.observedAt)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function snapshot(entry) {
|
|
29
|
+
if (entry.limits === undefined)
|
|
30
|
+
return entry.unavailable ?? { kind: "unavailable", reason: "failed", message: "No usage available." };
|
|
31
|
+
return {
|
|
32
|
+
kind: "ready",
|
|
33
|
+
limits: entry.limits,
|
|
34
|
+
stale: entry.fetchedAt === null || Date.now() - entry.fetchedAt >= FRESHNESS_MS,
|
|
35
|
+
error: entry.error
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function changedAccount() {
|
|
39
|
+
return { kind: "unavailable", reason: "failed", message: "The active account changed." };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class AccountLimitsCache2 {
|
|
43
|
+
providers = new Map;
|
|
44
|
+
get(providerId, accountId) {
|
|
45
|
+
const provider = this.providers.get(providerId);
|
|
46
|
+
return provider?.accounts.get(accountId === undefined ? provider.lastAccount : accountId)?.limits;
|
|
47
|
+
}
|
|
48
|
+
invalidate(providerId) {
|
|
49
|
+
const provider = this.providers.get(providerId);
|
|
50
|
+
if (provider === undefined)
|
|
51
|
+
return;
|
|
52
|
+
provider.generation += 1;
|
|
53
|
+
for (const entry of provider.accounts.values())
|
|
54
|
+
entry.flight?.abort.abort();
|
|
55
|
+
provider.accounts.clear();
|
|
56
|
+
}
|
|
57
|
+
scope(providerId) {
|
|
58
|
+
let provider = this.providers.get(providerId);
|
|
59
|
+
if (provider === undefined) {
|
|
60
|
+
provider = { generation: 0, lastAccount: null, accounts: new Map };
|
|
61
|
+
this.providers.set(providerId, provider);
|
|
62
|
+
}
|
|
63
|
+
const state = provider;
|
|
64
|
+
const generation = state.generation;
|
|
65
|
+
const accountEntry = (accountId) => {
|
|
66
|
+
const key = accountId ?? null;
|
|
67
|
+
state.lastAccount = key;
|
|
68
|
+
const existing = state.accounts.get(key);
|
|
69
|
+
if (existing !== undefined)
|
|
70
|
+
return existing;
|
|
71
|
+
const entry = {
|
|
72
|
+
limits: undefined,
|
|
73
|
+
revision: 0,
|
|
74
|
+
observed: new Map,
|
|
75
|
+
fetchedAt: null,
|
|
76
|
+
retryAt: 0,
|
|
77
|
+
error: null,
|
|
78
|
+
unavailable: null,
|
|
79
|
+
flight: null
|
|
80
|
+
};
|
|
81
|
+
state.accounts.set(key, entry);
|
|
82
|
+
return entry;
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
observe(accountId, limits) {
|
|
86
|
+
if (state.generation !== generation)
|
|
87
|
+
return;
|
|
88
|
+
const entry = accountEntry(accountId);
|
|
89
|
+
mergeLimits(entry, limits);
|
|
90
|
+
},
|
|
91
|
+
async fetch(accountId, load, options) {
|
|
92
|
+
options?.signal?.throwIfAborted();
|
|
93
|
+
if (state.generation !== generation)
|
|
94
|
+
return changedAccount();
|
|
95
|
+
const entry = accountEntry(accountId);
|
|
96
|
+
if (entry.flight !== null)
|
|
97
|
+
return raceWithAbortSignal2(entry.flight.result, operationSignal2(options?.signal));
|
|
98
|
+
if (Date.now() < entry.retryAt)
|
|
99
|
+
return snapshot(entry);
|
|
100
|
+
const abort = new AbortController;
|
|
101
|
+
const signal = AbortSignal.any([abort.signal, AbortSignal.timeout(TIMEOUT_MS)]);
|
|
102
|
+
const requestRevision = entry.revision;
|
|
103
|
+
const result = (async () => {
|
|
104
|
+
try {
|
|
105
|
+
const limits = await raceWithAbortSignal2(Promise.resolve().then(() => load(signal)), signal);
|
|
106
|
+
if (state.generation !== generation)
|
|
107
|
+
return changedAccount();
|
|
108
|
+
if (limits === undefined) {
|
|
109
|
+
entry.unavailable = {
|
|
110
|
+
kind: "unavailable",
|
|
111
|
+
reason: "unsupported",
|
|
112
|
+
message: "This account does not provide subscription usage."
|
|
113
|
+
};
|
|
114
|
+
entry.retryAt = Date.now() + RETRY_MS;
|
|
115
|
+
return snapshot(entry);
|
|
116
|
+
}
|
|
117
|
+
mergeLimits(entry, limits, requestRevision);
|
|
118
|
+
entry.fetchedAt = Date.now();
|
|
119
|
+
entry.retryAt = entry.fetchedAt + FRESHNESS_MS;
|
|
120
|
+
entry.error = null;
|
|
121
|
+
entry.unavailable = null;
|
|
122
|
+
return snapshot(entry);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (state.generation !== generation)
|
|
125
|
+
return changedAccount();
|
|
126
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
127
|
+
entry.error = message;
|
|
128
|
+
entry.unavailable = { kind: "unavailable", reason: "failed", message };
|
|
129
|
+
entry.retryAt = Date.now() + RETRY_MS;
|
|
130
|
+
return snapshot(entry);
|
|
131
|
+
} finally {
|
|
132
|
+
entry.flight = null;
|
|
133
|
+
}
|
|
134
|
+
})();
|
|
135
|
+
entry.flight = { result, abort };
|
|
136
|
+
return raceWithAbortSignal2(result, operationSignal2(options?.signal));
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export { AccountLimitsCache2 };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// src/api/openai-completions-reasoning.ts
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import { Value } from "typebox/value";
|
|
4
|
+
var reasoningDetailFields = {
|
|
5
|
+
id: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
6
|
+
format: Type.Optional(Type.String()),
|
|
7
|
+
index: Type.Optional(Type.Number())
|
|
8
|
+
};
|
|
9
|
+
var OpenAIReasoningDetailSchema2 = Type.Union([
|
|
10
|
+
Type.Object({
|
|
11
|
+
...reasoningDetailFields,
|
|
12
|
+
type: Type.Literal("reasoning.summary"),
|
|
13
|
+
summary: Type.String()
|
|
14
|
+
}),
|
|
15
|
+
Type.Object({
|
|
16
|
+
...reasoningDetailFields,
|
|
17
|
+
type: Type.Literal("reasoning.encrypted"),
|
|
18
|
+
data: Type.String()
|
|
19
|
+
}),
|
|
20
|
+
Type.Object({
|
|
21
|
+
...reasoningDetailFields,
|
|
22
|
+
type: Type.Literal("reasoning.text"),
|
|
23
|
+
text: Type.String(),
|
|
24
|
+
signature: Type.Optional(Type.Union([Type.String(), Type.Null()]))
|
|
25
|
+
})
|
|
26
|
+
]);
|
|
27
|
+
var OpenAIReasoningDetailsSchema = Type.Array(OpenAIReasoningDetailSchema2, { minItems: 1 });
|
|
28
|
+
function parseOpenAIReasoningDetails2(signature) {
|
|
29
|
+
if (!signature)
|
|
30
|
+
return;
|
|
31
|
+
try {
|
|
32
|
+
const parsed = JSON.parse(signature);
|
|
33
|
+
return Value.Check(OpenAIReasoningDetailsSchema, parsed) ? parsed : undefined;
|
|
34
|
+
} catch {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function parseLegacyEncryptedReasoningDetail2(signature) {
|
|
39
|
+
if (!signature)
|
|
40
|
+
return;
|
|
41
|
+
try {
|
|
42
|
+
const parsed = JSON.parse(signature);
|
|
43
|
+
return Value.Check(OpenAIReasoningDetailSchema2, parsed) && parsed.type === "reasoning.encrypted" && parsed.id !== undefined && parsed.id !== null && parsed.id.length > 0 && parsed.data.length > 0 ? parsed : undefined;
|
|
44
|
+
} catch {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function fillMissingCommonReasoningDetailFields(target, source) {
|
|
49
|
+
target.id ??= source.id;
|
|
50
|
+
target.format ||= source.format;
|
|
51
|
+
target.index ??= source.index;
|
|
52
|
+
}
|
|
53
|
+
function appendOpenAIReasoningDetail2(details, detail) {
|
|
54
|
+
const lastDetail = details.at(-1);
|
|
55
|
+
if (detail.type === "reasoning.text" && lastDetail?.type === "reasoning.text") {
|
|
56
|
+
lastDetail.text += detail.text;
|
|
57
|
+
lastDetail.signature ||= detail.signature;
|
|
58
|
+
fillMissingCommonReasoningDetailFields(lastDetail, detail);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (detail.type === "reasoning.summary" && lastDetail?.type === "reasoning.summary") {
|
|
62
|
+
lastDetail.summary += detail.summary;
|
|
63
|
+
fillMissingCommonReasoningDetailFields(lastDetail, detail);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
details.push({ ...detail });
|
|
67
|
+
}
|
|
68
|
+
var OPENAI_COMPLETIONS_REASONING_FIELDS = [
|
|
69
|
+
"reasoning",
|
|
70
|
+
"reasoning_content",
|
|
71
|
+
"reasoning_text"
|
|
72
|
+
];
|
|
73
|
+
var OpenAICompletionsReasoningFieldSchema2 = Type.Enum(OPENAI_COMPLETIONS_REASONING_FIELDS);
|
|
74
|
+
var CopilotReasoningOpaqueSchema2 = Type.Object({
|
|
75
|
+
reasoning_opaque: Type.String({ minLength: 1 })
|
|
76
|
+
});
|
|
77
|
+
var REASONING_DELTA_SCHEMAS2 = ["reasoning_content", "reasoning", "reasoning_text"].map((field) => ({ field, schema: Type.Object({ [field]: Type.String({ minLength: 1 }) }) }));
|
|
78
|
+
var ReasoningDetailsDeltaSchema2 = Type.Object({
|
|
79
|
+
reasoning_details: Type.Array(Type.Unknown())
|
|
80
|
+
});
|
|
81
|
+
function parseCopilotReasoningOpaque2(signature) {
|
|
82
|
+
if (!signature)
|
|
83
|
+
return;
|
|
84
|
+
try {
|
|
85
|
+
const parsed = JSON.parse(signature);
|
|
86
|
+
return Value.Check(CopilotReasoningOpaqueSchema2, parsed) ? parsed.reasoning_opaque : undefined;
|
|
87
|
+
} catch {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { OpenAIReasoningDetailSchema2, parseOpenAIReasoningDetails2, parseLegacyEncryptedReasoningDetail2, appendOpenAIReasoningDetail2, OpenAICompletionsReasoningFieldSchema2, CopilotReasoningOpaqueSchema2, REASONING_DELTA_SCHEMAS2, ReasoningDetailsDeltaSchema2, parseCopilotReasoningOpaque2 };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_CODEX_BASE_URL2,
|
|
3
|
+
extractAccountId2,
|
|
4
|
+
buildBaseCodexHeaders2
|
|
5
|
+
} from "./account-limits-cache-fvadc36m.js";
|
|
6
|
+
import {
|
|
7
|
+
limitWindow2,
|
|
8
|
+
fetchAccountUsage2
|
|
9
|
+
} from "./account-limits-cache-ehy0ze2b.js";
|
|
10
|
+
|
|
11
|
+
// src/providers/openai-codex-limits.ts
|
|
12
|
+
import { Type } from "typebox";
|
|
13
|
+
import { Value } from "typebox/value";
|
|
14
|
+
var OptionalJsonNumber = Type.Optional(Type.Union([Type.Number(), Type.Null()]));
|
|
15
|
+
var CodexUsageWindowSchema = Type.Object({
|
|
16
|
+
used_percent: Type.Number(),
|
|
17
|
+
limit_window_seconds: OptionalJsonNumber,
|
|
18
|
+
reset_at: OptionalJsonNumber
|
|
19
|
+
});
|
|
20
|
+
var CodexUsageResponseSchema = Type.Object({
|
|
21
|
+
rate_limit: Type.Optional(Type.Union([
|
|
22
|
+
Type.Object({
|
|
23
|
+
primary_window: Type.Optional(Type.Unknown()),
|
|
24
|
+
secondary_window: Type.Optional(Type.Unknown())
|
|
25
|
+
}),
|
|
26
|
+
Type.Null()
|
|
27
|
+
]))
|
|
28
|
+
});
|
|
29
|
+
var CodexPlanSchema = Type.Object({ plan_type: Type.String() });
|
|
30
|
+
var CodexRateLimitsFrameSchema = Type.Object({
|
|
31
|
+
type: Type.Literal("codex.rate_limits"),
|
|
32
|
+
plan_type: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
33
|
+
rate_limits: Type.Optional(Type.Union([
|
|
34
|
+
Type.Object({
|
|
35
|
+
primary: Type.Optional(Type.Unknown()),
|
|
36
|
+
secondary: Type.Optional(Type.Unknown())
|
|
37
|
+
}),
|
|
38
|
+
Type.Null()
|
|
39
|
+
]))
|
|
40
|
+
});
|
|
41
|
+
var CodexRateLimitWindowSchema = Type.Object({
|
|
42
|
+
used_percent: Type.Number(),
|
|
43
|
+
window_minutes: OptionalJsonNumber,
|
|
44
|
+
reset_at: OptionalJsonNumber
|
|
45
|
+
});
|
|
46
|
+
var FIVE_HOURS_SECONDS = 5 * 60 * 60;
|
|
47
|
+
var WEEK_SECONDS = 7 * 24 * 60 * 60;
|
|
48
|
+
function durationLabel(seconds) {
|
|
49
|
+
if (seconds === WEEK_SECONDS)
|
|
50
|
+
return "Weekly";
|
|
51
|
+
if (seconds % (24 * 60 * 60) === 0)
|
|
52
|
+
return `${String(seconds / (24 * 60 * 60))}-day`;
|
|
53
|
+
if (seconds % (60 * 60) === 0)
|
|
54
|
+
return `${String(seconds / (60 * 60))}-hour`;
|
|
55
|
+
return `${String(Math.round(seconds / 60))}-minute`;
|
|
56
|
+
}
|
|
57
|
+
function codexLimitWindow(fallbackId, usedPercent, windowSeconds, resetAtSeconds) {
|
|
58
|
+
const id = windowSeconds === FIVE_HOURS_SECONDS ? "five_hour" : windowSeconds === WEEK_SECONDS ? "seven_day" : fallbackId;
|
|
59
|
+
const label = windowSeconds === undefined || windowSeconds === null ? fallbackId === "primary" ? "Primary" : "Secondary" : durationLabel(windowSeconds);
|
|
60
|
+
return limitWindow2(id, label, usedPercent, resetAtSeconds === undefined || resetAtSeconds === null ? undefined : resetAtSeconds * 1000, windowSeconds === undefined || windowSeconds === null ? undefined : windowSeconds / 60);
|
|
61
|
+
}
|
|
62
|
+
function minutesWindow(fallbackId, window) {
|
|
63
|
+
if (!window.window_minutes)
|
|
64
|
+
return;
|
|
65
|
+
return codexLimitWindow(fallbackId, window.used_percent, window.window_minutes * 60, window.reset_at);
|
|
66
|
+
}
|
|
67
|
+
function accountLimits(providerId, windows, plan) {
|
|
68
|
+
if (windows.length === 0)
|
|
69
|
+
return;
|
|
70
|
+
const limits = { providerId, windows, observedAt: Date.now() };
|
|
71
|
+
if (plan)
|
|
72
|
+
limits.plan = plan;
|
|
73
|
+
return limits;
|
|
74
|
+
}
|
|
75
|
+
function codexFrameLimits2(frame, providerId) {
|
|
76
|
+
if (!Value.Check(CodexRateLimitsFrameSchema, frame))
|
|
77
|
+
return;
|
|
78
|
+
const windows = [];
|
|
79
|
+
for (const id of ["primary", "secondary"]) {
|
|
80
|
+
const window = frame.rate_limits?.[id];
|
|
81
|
+
const parsed = Value.Check(CodexRateLimitWindowSchema, window) ? minutesWindow(id, window) : undefined;
|
|
82
|
+
if (parsed !== undefined)
|
|
83
|
+
windows.push(parsed);
|
|
84
|
+
}
|
|
85
|
+
return accountLimits(providerId, windows, frame.plan_type);
|
|
86
|
+
}
|
|
87
|
+
function codexHeaderLimits2(headers, providerId) {
|
|
88
|
+
const windows = [];
|
|
89
|
+
for (const id of ["primary", "secondary"]) {
|
|
90
|
+
const usedPercent = Number(headers[`x-codex-${id}-used-percent`]);
|
|
91
|
+
if (!Number.isFinite(usedPercent))
|
|
92
|
+
continue;
|
|
93
|
+
const resetHeader = headers[`x-codex-${id}-reset-at`];
|
|
94
|
+
const resetAt = resetHeader ? Number(resetHeader) : Number.NaN;
|
|
95
|
+
const parsed = minutesWindow(id, {
|
|
96
|
+
used_percent: usedPercent,
|
|
97
|
+
window_minutes: Number(headers[`x-codex-${id}-window-minutes`]),
|
|
98
|
+
reset_at: Number.isFinite(resetAt) ? resetAt : null
|
|
99
|
+
});
|
|
100
|
+
if (parsed !== undefined)
|
|
101
|
+
windows.push(parsed);
|
|
102
|
+
}
|
|
103
|
+
return accountLimits(providerId, windows, headers["x-codex-plan-type"]);
|
|
104
|
+
}
|
|
105
|
+
async function fetchOpenAICodexAccountLimits2(model, options) {
|
|
106
|
+
const apiKey = options?.apiKey;
|
|
107
|
+
if (!apiKey)
|
|
108
|
+
throw new Error(`No API key for provider: ${model.provider}`);
|
|
109
|
+
const headers = buildBaseCodexHeaders2(model.headers, options?.headers, extractAccountId2(apiKey), apiKey);
|
|
110
|
+
headers.set("accept", "application/json");
|
|
111
|
+
const baseUrl = (model.baseUrl?.trim() || DEFAULT_CODEX_BASE_URL2).replace(/\/+$/, "").replace(/\/codex$/, "");
|
|
112
|
+
const decoded = await fetchAccountUsage2(`${baseUrl}/wham/usage`, headers, options, CodexUsageResponseSchema, "Codex");
|
|
113
|
+
const windows = [];
|
|
114
|
+
for (const [id, window] of [
|
|
115
|
+
["primary", decoded.rate_limit?.primary_window],
|
|
116
|
+
["secondary", decoded.rate_limit?.secondary_window]
|
|
117
|
+
]) {
|
|
118
|
+
if (!Value.Check(CodexUsageWindowSchema, window))
|
|
119
|
+
continue;
|
|
120
|
+
windows.push(codexLimitWindow(id, window.used_percent, window.limit_window_seconds, window.reset_at));
|
|
121
|
+
}
|
|
122
|
+
const limits = { providerId: model.provider, windows, observedAt: Date.now() };
|
|
123
|
+
if (Value.Check(CodexPlanSchema, decoded))
|
|
124
|
+
limits.plan = decoded.plan_type;
|
|
125
|
+
return limits;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { codexFrameLimits2, codexHeaderLimits2, fetchOpenAICodexAccountLimits2 };
|