@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,562 @@
|
|
|
1
|
+
import {
|
|
2
|
+
lazyStream2
|
|
3
|
+
} from "./account-limits-cache-90c9c2eb.js";
|
|
4
|
+
import {
|
|
5
|
+
operationSignal2,
|
|
6
|
+
raceWithAbortSignal2
|
|
7
|
+
} from "./account-limits-cache-j2bt392s.js";
|
|
8
|
+
import {
|
|
9
|
+
AccountLimitsCache2
|
|
10
|
+
} from "./account-limits-cache-hemxtapd.js";
|
|
11
|
+
import {
|
|
12
|
+
defaultProviderAuthContext2
|
|
13
|
+
} from "./account-limits-cache-1jv43fhh.js";
|
|
14
|
+
import {
|
|
15
|
+
InMemoryCredentialStore2
|
|
16
|
+
} from "./account-limits-cache-emg5b4hj.js";
|
|
17
|
+
import {
|
|
18
|
+
ModelsError2,
|
|
19
|
+
resolveProviderAuth2,
|
|
20
|
+
DEFAULT_OAUTH_REFRESH_TIMEOUT_MS2
|
|
21
|
+
} from "./account-limits-cache-7z9s8j16.js";
|
|
22
|
+
import {
|
|
23
|
+
InMemoryModelsStore2
|
|
24
|
+
} from "./account-limits-cache-5ygxyyss.js";
|
|
25
|
+
import {
|
|
26
|
+
withFastVariants2,
|
|
27
|
+
baseModel2,
|
|
28
|
+
providerRequest2
|
|
29
|
+
} from "./account-limits-cache-031vfpt3.js";
|
|
30
|
+
import {
|
|
31
|
+
mergeProviderHeaders2
|
|
32
|
+
} from "./account-limits-cache-ag5b8dja.js";
|
|
33
|
+
import {
|
|
34
|
+
uuidv72
|
|
35
|
+
} from "./account-limits-cache-8137j3zp.js";
|
|
36
|
+
import {
|
|
37
|
+
CatalogRefreshes2
|
|
38
|
+
} from "./account-limits-cache-zpjmeyt0.js";
|
|
39
|
+
|
|
40
|
+
// src/models.ts
|
|
41
|
+
import { normalizeContext } from "@nyte-ai/schema/model";
|
|
42
|
+
var DEFAULT_MODELS_CATALOG_URL2 = "https://pub-426b80ba181f408387bc9361b2fcbe3f.r2.dev";
|
|
43
|
+
|
|
44
|
+
class ModelsImpl {
|
|
45
|
+
providers = new Map;
|
|
46
|
+
credentials;
|
|
47
|
+
modelsStore;
|
|
48
|
+
authContext;
|
|
49
|
+
catalog;
|
|
50
|
+
accountLimits;
|
|
51
|
+
activeAccounts;
|
|
52
|
+
refreshes;
|
|
53
|
+
constructor(options, accountLimits = new AccountLimitsCache2, activeAccounts = new Map) {
|
|
54
|
+
this.credentials = options?.credentials ?? new InMemoryCredentialStore2;
|
|
55
|
+
this.modelsStore = options?.modelsStore ?? new InMemoryModelsStore2;
|
|
56
|
+
this.authContext = options?.authContext ?? defaultProviderAuthContext2();
|
|
57
|
+
this.catalog = {
|
|
58
|
+
url: options?.catalog?.url ?? DEFAULT_MODELS_CATALOG_URL2,
|
|
59
|
+
fetch: options?.catalog?.fetch ?? globalThis.fetch
|
|
60
|
+
};
|
|
61
|
+
this.accountLimits = accountLimits;
|
|
62
|
+
this.activeAccounts = activeAccounts;
|
|
63
|
+
this.refreshes = new CatalogRefreshes2(this.modelsStore, this.catalog);
|
|
64
|
+
}
|
|
65
|
+
withProvider(provider) {
|
|
66
|
+
const models = new ModelsImpl({
|
|
67
|
+
credentials: this.credentials,
|
|
68
|
+
modelsStore: this.modelsStore,
|
|
69
|
+
authContext: this.authContext,
|
|
70
|
+
catalog: this.catalog
|
|
71
|
+
}, this.accountLimits, this.activeAccounts);
|
|
72
|
+
for (const entry of this.providers.values())
|
|
73
|
+
models.setProvider(entry);
|
|
74
|
+
models.setProvider(provider);
|
|
75
|
+
return models;
|
|
76
|
+
}
|
|
77
|
+
setProvider(provider) {
|
|
78
|
+
this.refreshes.supersede(provider.id);
|
|
79
|
+
this.providers.set(provider.id, provider);
|
|
80
|
+
}
|
|
81
|
+
deleteProvider(id) {
|
|
82
|
+
this.refreshes.supersede(id);
|
|
83
|
+
this.providers.delete(id);
|
|
84
|
+
}
|
|
85
|
+
clearProviders() {
|
|
86
|
+
for (const id of new Set([...this.providers.keys(), ...this.refreshes.running()])) {
|
|
87
|
+
this.refreshes.supersede(id);
|
|
88
|
+
}
|
|
89
|
+
this.providers.clear();
|
|
90
|
+
}
|
|
91
|
+
getProviders() {
|
|
92
|
+
return Array.from(this.providers.values());
|
|
93
|
+
}
|
|
94
|
+
getProvider(id) {
|
|
95
|
+
return this.providers.get(id);
|
|
96
|
+
}
|
|
97
|
+
getModels(provider) {
|
|
98
|
+
if (provider !== undefined) {
|
|
99
|
+
const entry = this.providers.get(provider);
|
|
100
|
+
if (!entry)
|
|
101
|
+
return [];
|
|
102
|
+
try {
|
|
103
|
+
return withFastVariants2(entry.getModels());
|
|
104
|
+
} catch {
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const models = [];
|
|
109
|
+
for (const entry of this.providers.values()) {
|
|
110
|
+
try {
|
|
111
|
+
models.push(...withFastVariants2(entry.getModels()));
|
|
112
|
+
} catch {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return models;
|
|
117
|
+
}
|
|
118
|
+
getModel(provider, id) {
|
|
119
|
+
return this.getModels(provider).find((model) => model.id === id);
|
|
120
|
+
}
|
|
121
|
+
async refresh(options = {}) {
|
|
122
|
+
const allowNetwork = options.allowNetwork ?? true;
|
|
123
|
+
const callerSignal = operationSignal2(options.signal);
|
|
124
|
+
const errors = new Map;
|
|
125
|
+
if (callerSignal.aborted)
|
|
126
|
+
return { aborted: true, errors };
|
|
127
|
+
const selected = options.providers ? new Set(options.providers) : undefined;
|
|
128
|
+
const refresh = Promise.all(Array.from(this.providers.values()).map(async (provider) => {
|
|
129
|
+
if (provider.refreshModels === undefined || selected && !selected.has(provider.id)) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const error = await this.refreshes.run(provider, allowNetwork, options.force, callerSignal, (signal) => this.readAccounts(provider.id, signal), (accounts, signal) => this.resolveRefreshCredential(provider, accounts, signal));
|
|
133
|
+
if (error !== undefined)
|
|
134
|
+
errors.set(provider.id, error);
|
|
135
|
+
}));
|
|
136
|
+
try {
|
|
137
|
+
await raceWithAbortSignal2(refresh, callerSignal);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (!callerSignal.aborted)
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
return { aborted: callerSignal.aborted, errors: new Map(errors) };
|
|
143
|
+
}
|
|
144
|
+
async resolveRefreshCredential(provider, stored, signal) {
|
|
145
|
+
const account = stored?.accounts.find((entry) => entry.id === stored.active);
|
|
146
|
+
if (stored && !account)
|
|
147
|
+
return;
|
|
148
|
+
const credential = account?.credential;
|
|
149
|
+
if (credential?.type === "oauth") {
|
|
150
|
+
const oauth = provider.auth.oauth;
|
|
151
|
+
if (!oauth)
|
|
152
|
+
return;
|
|
153
|
+
if (Date.now() < credential.expires)
|
|
154
|
+
return credential;
|
|
155
|
+
if (signal.aborted)
|
|
156
|
+
return;
|
|
157
|
+
let refreshed;
|
|
158
|
+
const post = await this.credentials.modify(provider.id, async (current) => {
|
|
159
|
+
const selected = current?.accounts.find((entry) => entry.id === account?.id);
|
|
160
|
+
if (!current || selected?.credential.type !== "oauth")
|
|
161
|
+
return;
|
|
162
|
+
const previous = selected.credential;
|
|
163
|
+
if (Date.now() < previous.expires)
|
|
164
|
+
return;
|
|
165
|
+
if (refreshed?.token !== previous.refresh) {
|
|
166
|
+
const next = await oauth.refresh(previous, AbortSignal.any([signal, AbortSignal.timeout(DEFAULT_OAUTH_REFRESH_TIMEOUT_MS2)]));
|
|
167
|
+
refreshed = { token: previous.refresh, credential: next };
|
|
168
|
+
}
|
|
169
|
+
const next = refreshed.credential;
|
|
170
|
+
return {
|
|
171
|
+
...current,
|
|
172
|
+
accounts: current.accounts.map((entry) => entry.id === account?.id ? { ...entry, credential: next } : entry)
|
|
173
|
+
};
|
|
174
|
+
}, { signal });
|
|
175
|
+
const updated = post?.accounts.find((entry) => entry.id === account?.id)?.credential;
|
|
176
|
+
return updated?.type === "oauth" ? updated : undefined;
|
|
177
|
+
}
|
|
178
|
+
const apiKey = provider.auth.apiKey;
|
|
179
|
+
if (!apiKey)
|
|
180
|
+
return;
|
|
181
|
+
const result = await apiKey.resolve({
|
|
182
|
+
ctx: this.authContext,
|
|
183
|
+
credential: credential?.type === "api_key" ? credential : undefined,
|
|
184
|
+
signal
|
|
185
|
+
});
|
|
186
|
+
if (!result)
|
|
187
|
+
return;
|
|
188
|
+
return { type: "api_key", key: result.auth.apiKey, env: result.env };
|
|
189
|
+
}
|
|
190
|
+
async readAccounts(providerId, signal) {
|
|
191
|
+
try {
|
|
192
|
+
return await this.credentials.read(providerId, { signal });
|
|
193
|
+
} catch (error) {
|
|
194
|
+
throw new ModelsError2("auth", `Credential store read failed for ${providerId}`, {
|
|
195
|
+
cause: error
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
async checkOAuthAuth(provider, signal) {
|
|
200
|
+
const oauth = provider.auth.oauth;
|
|
201
|
+
if (!oauth)
|
|
202
|
+
return;
|
|
203
|
+
if (!oauth.check)
|
|
204
|
+
return { source: "OAuth", type: "oauth" };
|
|
205
|
+
try {
|
|
206
|
+
return await oauth.check({ ctx: this.authContext, signal });
|
|
207
|
+
} catch (error) {
|
|
208
|
+
throw new ModelsError2("auth", `OAuth auth check failed for provider ${provider.id}`, {
|
|
209
|
+
cause: error
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async checkProviderAuth(provider, credential, signal) {
|
|
214
|
+
if (credential?.type === "oauth")
|
|
215
|
+
return this.checkOAuthAuth(provider, signal);
|
|
216
|
+
const apiKey = provider.auth.apiKey;
|
|
217
|
+
if (!apiKey)
|
|
218
|
+
return;
|
|
219
|
+
if (apiKey.check) {
|
|
220
|
+
try {
|
|
221
|
+
return await apiKey.check({
|
|
222
|
+
ctx: this.authContext,
|
|
223
|
+
credential: credential?.type === "api_key" ? credential : undefined,
|
|
224
|
+
signal
|
|
225
|
+
});
|
|
226
|
+
} catch (error) {
|
|
227
|
+
throw new ModelsError2("auth", `API key auth check failed for provider ${provider.id}`, {
|
|
228
|
+
cause: error
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
const resolution = await apiKey.resolve({ ctx: this.authContext, credential, signal });
|
|
234
|
+
return resolution ? { source: resolution.source, type: "api_key" } : undefined;
|
|
235
|
+
} catch (error) {
|
|
236
|
+
throw new ModelsError2("auth", `API key auth check failed for provider ${provider.id}`, {
|
|
237
|
+
cause: error
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
getAccountLimits(providerId, accountId) {
|
|
242
|
+
return this.accountLimits.get(providerId, accountId === undefined ? this.activeAccounts.get(providerId) : accountId);
|
|
243
|
+
}
|
|
244
|
+
async fetchAccountLimits(providerId, options) {
|
|
245
|
+
const signal = operationSignal2(options?.signal);
|
|
246
|
+
signal.throwIfAborted();
|
|
247
|
+
const provider = this.providers.get(providerId);
|
|
248
|
+
const scope = this.accountLimits.scope(providerId);
|
|
249
|
+
const fetchLimits = provider?.fetchAccountLimits;
|
|
250
|
+
if (!fetchLimits)
|
|
251
|
+
return scope.fetch(this.activeAccounts.get(providerId) ?? undefined, async () => {
|
|
252
|
+
return;
|
|
253
|
+
}, {
|
|
254
|
+
signal
|
|
255
|
+
});
|
|
256
|
+
const accounts = await this.listAccounts(providerId, { signal });
|
|
257
|
+
const account = accounts.find((entry) => entry.active);
|
|
258
|
+
if (accounts.length > 0 && !account || !account && await this.checkAuth(providerId, { signal }) === undefined)
|
|
259
|
+
return {
|
|
260
|
+
kind: "unavailable",
|
|
261
|
+
reason: "unconfigured",
|
|
262
|
+
message: "Sign in to view account usage."
|
|
263
|
+
};
|
|
264
|
+
return scope.fetch(account?.id, async (fetchSignal) => {
|
|
265
|
+
const resolution = await this.getAuth(providerId, { signal: fetchSignal });
|
|
266
|
+
if (!resolution || resolution.accountId !== account?.id) {
|
|
267
|
+
this.accountLimits.invalidate(providerId);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
return fetchLimits({ auth: resolution.auth, signal: fetchSignal });
|
|
271
|
+
}, { signal });
|
|
272
|
+
}
|
|
273
|
+
async listAccounts(providerId, options) {
|
|
274
|
+
try {
|
|
275
|
+
const rows = await this.credentials.list(options);
|
|
276
|
+
const selected = providerId === undefined ? rows : rows.filter((row) => row.providerId === providerId);
|
|
277
|
+
const providers = providerId === undefined ? this.providers.keys() : [providerId];
|
|
278
|
+
for (const provider of providers)
|
|
279
|
+
this.activeAccounts.set(provider, selected.find((row) => row.providerId === provider && row.active)?.id ?? null);
|
|
280
|
+
return selected;
|
|
281
|
+
} catch (error) {
|
|
282
|
+
throw new ModelsError2("auth", `Credential store list failed${providerId ? ` for ${providerId}` : ""}`, { cause: error });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
async selectAccount(providerId, accountId, options) {
|
|
286
|
+
const signal = operationSignal2(options?.signal);
|
|
287
|
+
signal.throwIfAborted();
|
|
288
|
+
if (!this.providers.has(providerId))
|
|
289
|
+
throw new ModelsError2("provider", `Unknown provider: ${providerId}`);
|
|
290
|
+
let changed = false;
|
|
291
|
+
await this.credentials.modify(providerId, async (current) => {
|
|
292
|
+
if (!current?.accounts.some((account) => account.id === accountId))
|
|
293
|
+
throw new ModelsError2("auth", `Unknown account for ${providerId}: ${accountId}`);
|
|
294
|
+
if (current.active === accountId)
|
|
295
|
+
return;
|
|
296
|
+
changed = true;
|
|
297
|
+
return { ...current, active: accountId };
|
|
298
|
+
}, { signal });
|
|
299
|
+
this.activeAccounts.set(providerId, accountId);
|
|
300
|
+
if (!changed)
|
|
301
|
+
return;
|
|
302
|
+
this.accountLimits.invalidate(providerId);
|
|
303
|
+
this.refreshes.supersede(providerId);
|
|
304
|
+
}
|
|
305
|
+
checkAuth(providerId, options) {
|
|
306
|
+
const signal = operationSignal2(options?.signal);
|
|
307
|
+
const check = (async () => {
|
|
308
|
+
signal.throwIfAborted();
|
|
309
|
+
const provider = this.providers.get(providerId);
|
|
310
|
+
if (!provider)
|
|
311
|
+
return;
|
|
312
|
+
const accounts = await this.listAccounts(providerId, { signal });
|
|
313
|
+
const stored = accounts.find((account) => account.active);
|
|
314
|
+
if (accounts.length > 0 && stored === undefined)
|
|
315
|
+
return;
|
|
316
|
+
if (stored?.type === "oauth")
|
|
317
|
+
return this.checkOAuthAuth(provider, signal);
|
|
318
|
+
if (stored?.type === "api_key" && provider.auth.apiKey?.check === undefined)
|
|
319
|
+
return provider.auth.apiKey ? { source: "stored credential", type: "api_key" } : undefined;
|
|
320
|
+
const current = stored === undefined ? undefined : await this.readAccounts(providerId, signal);
|
|
321
|
+
return this.checkProviderAuth(provider, current?.accounts.find((account) => account.id === stored?.id)?.credential, signal);
|
|
322
|
+
})();
|
|
323
|
+
return raceWithAbortSignal2(check, signal);
|
|
324
|
+
}
|
|
325
|
+
verifyAuth(providerId, options) {
|
|
326
|
+
const signal = operationSignal2(options?.signal);
|
|
327
|
+
const verification = (async () => {
|
|
328
|
+
signal.throwIfAborted();
|
|
329
|
+
const provider = this.providers.get(providerId);
|
|
330
|
+
if (!provider) {
|
|
331
|
+
return { ok: false, reason: "unconfigured", message: `Unknown provider: ${providerId}` };
|
|
332
|
+
}
|
|
333
|
+
const resolution = await resolveProviderAuth2(provider, this.credentials, this.authContext, {
|
|
334
|
+
signal
|
|
335
|
+
});
|
|
336
|
+
if (!resolution) {
|
|
337
|
+
return {
|
|
338
|
+
ok: false,
|
|
339
|
+
reason: "unconfigured",
|
|
340
|
+
message: `${provider.name} has no credential configured`
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
const source = resolution.source ?? provider.name;
|
|
344
|
+
const verify = provider.auth.verify;
|
|
345
|
+
if (!verify)
|
|
346
|
+
return { ok: true, source, verified: false };
|
|
347
|
+
try {
|
|
348
|
+
const result = await verify({ auth: resolution.auth, ctx: this.authContext, signal });
|
|
349
|
+
return result.ok ? { ...result, source, verified: result.verified ?? true } : result;
|
|
350
|
+
} catch {
|
|
351
|
+
signal.throwIfAborted();
|
|
352
|
+
return {
|
|
353
|
+
ok: false,
|
|
354
|
+
reason: "unreachable",
|
|
355
|
+
message: `${provider.name} could not be reached to verify the credential`
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
})();
|
|
359
|
+
return raceWithAbortSignal2(verification, signal);
|
|
360
|
+
}
|
|
361
|
+
getAvailable(providerId, options) {
|
|
362
|
+
const signal = operationSignal2(options?.signal);
|
|
363
|
+
const available = (async () => {
|
|
364
|
+
signal.throwIfAborted();
|
|
365
|
+
const providers = providerId ? [this.providers.get(providerId)].filter((entry) => entry !== undefined) : this.getProviders();
|
|
366
|
+
const checks = await Promise.all(providers.map(async (provider) => {
|
|
367
|
+
const stored = await this.readAccounts(provider.id, signal);
|
|
368
|
+
const credential = stored?.accounts.find((account) => account.id === stored.active)?.credential;
|
|
369
|
+
return {
|
|
370
|
+
provider,
|
|
371
|
+
credential,
|
|
372
|
+
auth: stored && !credential ? undefined : await this.checkProviderAuth(provider, credential, signal)
|
|
373
|
+
};
|
|
374
|
+
}));
|
|
375
|
+
return checks.flatMap(({ provider, credential, auth }) => {
|
|
376
|
+
if (!auth)
|
|
377
|
+
return [];
|
|
378
|
+
const models = provider.getModels();
|
|
379
|
+
return withFastVariants2(provider.filterModels?.(models, credential) ?? models);
|
|
380
|
+
});
|
|
381
|
+
})();
|
|
382
|
+
return raceWithAbortSignal2(available, signal);
|
|
383
|
+
}
|
|
384
|
+
async getAuth(providerOrModel, overrides) {
|
|
385
|
+
const signal = operationSignal2(overrides?.signal);
|
|
386
|
+
const providerId = typeof providerOrModel === "string" ? providerOrModel : providerOrModel.provider;
|
|
387
|
+
const provider = this.providers.get(providerId);
|
|
388
|
+
if (!provider)
|
|
389
|
+
return;
|
|
390
|
+
const result = await resolveProviderAuth2(provider, this.credentials, this.authContext, {
|
|
391
|
+
...overrides,
|
|
392
|
+
signal
|
|
393
|
+
});
|
|
394
|
+
if (!result || typeof providerOrModel === "string" || !providerOrModel.headers)
|
|
395
|
+
return result;
|
|
396
|
+
return {
|
|
397
|
+
...result,
|
|
398
|
+
auth: {
|
|
399
|
+
...result.auth,
|
|
400
|
+
headers: mergeProviderHeaders2(result.auth.headers, providerOrModel.headers)
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
async login(providerId, type, interaction, label) {
|
|
405
|
+
const signal = operationSignal2(interaction.signal);
|
|
406
|
+
signal.throwIfAborted();
|
|
407
|
+
const provider = this.providers.get(providerId);
|
|
408
|
+
if (!provider)
|
|
409
|
+
throw new ModelsError2("provider", `Unknown provider: ${providerId}`);
|
|
410
|
+
const method = type === "oauth" ? provider.auth.oauth : provider.auth.apiKey;
|
|
411
|
+
if (!method?.login) {
|
|
412
|
+
throw new ModelsError2("auth", `${provider.name} does not support ${type} login`);
|
|
413
|
+
}
|
|
414
|
+
const accounts = await this.listAccounts(providerId, { signal });
|
|
415
|
+
const name = label?.trim() || `Account ${accounts.length + 1}`;
|
|
416
|
+
const loginOperation = method.login({ ...interaction, signal });
|
|
417
|
+
const credential = await raceWithAbortSignal2(loginOperation, signal);
|
|
418
|
+
if (credential.type === "api_key" && credential.key !== undefined && credential.key.trim() === "")
|
|
419
|
+
throw new ModelsError2("auth", "Enter an API key.");
|
|
420
|
+
let mutationStarted = false;
|
|
421
|
+
let markMutationStarted;
|
|
422
|
+
const started = new Promise((resolve) => {
|
|
423
|
+
markMutationStarted = resolve;
|
|
424
|
+
});
|
|
425
|
+
const id = uuidv72();
|
|
426
|
+
const mutation = this.credentials.modify(providerId, async (current) => {
|
|
427
|
+
mutationStarted = true;
|
|
428
|
+
markMutationStarted?.();
|
|
429
|
+
return {
|
|
430
|
+
active: id,
|
|
431
|
+
accounts: [...current?.accounts ?? [], { id, label: name, credential }]
|
|
432
|
+
};
|
|
433
|
+
}, { signal });
|
|
434
|
+
mutation.catch(() => {});
|
|
435
|
+
try {
|
|
436
|
+
await new Promise((resolve, reject) => {
|
|
437
|
+
const onAbort = () => {
|
|
438
|
+
if (!mutationStarted)
|
|
439
|
+
reject(signal.reason);
|
|
440
|
+
};
|
|
441
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
442
|
+
Promise.race([started, mutation]).finally(() => signal.removeEventListener("abort", onAbort)).then(() => resolve(), reject);
|
|
443
|
+
if (signal.aborted)
|
|
444
|
+
onAbort();
|
|
445
|
+
});
|
|
446
|
+
await mutation;
|
|
447
|
+
} catch (error) {
|
|
448
|
+
signal.throwIfAborted();
|
|
449
|
+
throw new ModelsError2("auth", `Credential store modify failed for ${providerId}`, {
|
|
450
|
+
cause: error
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
this.refreshes.supersede(providerId);
|
|
454
|
+
this.activeAccounts.set(providerId, id);
|
|
455
|
+
this.accountLimits.invalidate(providerId);
|
|
456
|
+
return credential;
|
|
457
|
+
}
|
|
458
|
+
async logout(providerId, options) {
|
|
459
|
+
const signal = operationSignal2(options?.signal);
|
|
460
|
+
signal.throwIfAborted();
|
|
461
|
+
try {
|
|
462
|
+
const stored = await this.readAccounts(providerId, signal);
|
|
463
|
+
const id = stored?.active;
|
|
464
|
+
if (id === undefined || id === null)
|
|
465
|
+
return;
|
|
466
|
+
const post = await this.credentials.modify(providerId, async (current) => {
|
|
467
|
+
if (!current?.accounts.some((account) => account.id === id))
|
|
468
|
+
return;
|
|
469
|
+
return {
|
|
470
|
+
active: current.active === id ? null : current.active,
|
|
471
|
+
accounts: current.accounts.filter((account) => account.id !== id)
|
|
472
|
+
};
|
|
473
|
+
}, { signal });
|
|
474
|
+
this.activeAccounts.set(providerId, post?.active ?? null);
|
|
475
|
+
} catch (error) {
|
|
476
|
+
signal.throwIfAborted();
|
|
477
|
+
throw new ModelsError2("auth", `Credential store delete failed for ${providerId}`, {
|
|
478
|
+
cause: error
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
this.refreshes.supersede(providerId);
|
|
482
|
+
this.accountLimits.invalidate(providerId);
|
|
483
|
+
}
|
|
484
|
+
requireProvider(model) {
|
|
485
|
+
const provider = this.providers.get(model.provider);
|
|
486
|
+
if (!provider) {
|
|
487
|
+
throw new ModelsError2("provider", `Unknown provider: ${model.provider}`);
|
|
488
|
+
}
|
|
489
|
+
return provider;
|
|
490
|
+
}
|
|
491
|
+
async applyAuth(selected, selectedOptions) {
|
|
492
|
+
const { model, options } = providerRequest2(selected, selectedOptions);
|
|
493
|
+
const scope = this.accountLimits.scope(model.provider);
|
|
494
|
+
const resolution = await this.getAuth(model, {
|
|
495
|
+
apiKey: options?.apiKey,
|
|
496
|
+
env: options?.env,
|
|
497
|
+
signal: options?.signal
|
|
498
|
+
});
|
|
499
|
+
if (!resolution) {
|
|
500
|
+
throw new ModelsError2("auth", `Provider is not configured: ${model.provider}`);
|
|
501
|
+
}
|
|
502
|
+
const auth = resolution.auth;
|
|
503
|
+
const apiKey = options?.apiKey ?? auth.apiKey;
|
|
504
|
+
let headers = auth.headers || options?.headers ? mergeProviderHeaders2(auth.headers, options?.headers) : undefined;
|
|
505
|
+
if (options?.transformHeaders)
|
|
506
|
+
headers = await options.transformHeaders(headers ?? {});
|
|
507
|
+
const env = resolution.env || options?.env ? { ...resolution.env ?? {}, ...options?.env ?? {} } : undefined;
|
|
508
|
+
const requestModel = auth.baseUrl ? { ...model, baseUrl: auth.baseUrl } : model;
|
|
509
|
+
const { transformHeaders: _transformHeaders, ...providerOptions } = options ?? {};
|
|
510
|
+
const onAccountLimits = async (limits, requested) => {
|
|
511
|
+
scope.observe(resolution.accountId, limits);
|
|
512
|
+
await options?.onAccountLimits?.(limits, requested);
|
|
513
|
+
};
|
|
514
|
+
const requestOptions = { ...providerOptions, apiKey, headers, env, onAccountLimits };
|
|
515
|
+
return { requestModel, requestOptions };
|
|
516
|
+
}
|
|
517
|
+
stream(model, context, options) {
|
|
518
|
+
const transcript = normalizeContext(context);
|
|
519
|
+
return lazyStream2(baseModel2(model), async () => {
|
|
520
|
+
const provider = this.requireProvider(model);
|
|
521
|
+
const { requestModel, requestOptions } = await this.applyAuth(model, options);
|
|
522
|
+
return provider.stream(requestModel, transcript, requestOptions);
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
async complete(model, context, options) {
|
|
526
|
+
return this.stream(model, context, options).result();
|
|
527
|
+
}
|
|
528
|
+
streamSimple(model, context, options) {
|
|
529
|
+
const transcript = normalizeContext(context);
|
|
530
|
+
return lazyStream2(baseModel2(model), async () => {
|
|
531
|
+
const provider = this.requireProvider(model);
|
|
532
|
+
const { requestModel, requestOptions } = await this.applyAuth(model, options);
|
|
533
|
+
return provider.streamSimple(requestModel, transcript, requestOptions);
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
async completeSimple(model, context, options) {
|
|
537
|
+
return this.streamSimple(model, context, options).result();
|
|
538
|
+
}
|
|
539
|
+
async fetchDeferred(model, handle, options) {
|
|
540
|
+
return lazyStream2(baseModel2(model), async () => {
|
|
541
|
+
const provider = this.requireProvider(model);
|
|
542
|
+
if (!provider.fetchDeferred) {
|
|
543
|
+
throw new ModelsError2("provider", `Provider ${model.provider} does not support deferred responses`);
|
|
544
|
+
}
|
|
545
|
+
const { requestModel, requestOptions } = await this.applyAuth(model, options);
|
|
546
|
+
return provider.fetchDeferred(requestModel, handle, requestOptions);
|
|
547
|
+
}).result();
|
|
548
|
+
}
|
|
549
|
+
async cancelDeferred(model, handle, options) {
|
|
550
|
+
const provider = this.requireProvider(model);
|
|
551
|
+
if (!provider.cancelDeferred) {
|
|
552
|
+
throw new ModelsError2("provider", `Provider ${model.provider} does not support deferred responses`);
|
|
553
|
+
}
|
|
554
|
+
const { requestModel, requestOptions } = await this.applyAuth(model, options);
|
|
555
|
+
await provider.cancelDeferred(requestModel, handle, requestOptions);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function createModels2(options) {
|
|
559
|
+
return new ModelsImpl(options);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
export { DEFAULT_MODELS_CATALOG_URL2, createModels2 };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/models-store.ts
|
|
2
|
+
class InMemoryModelsStore2 {
|
|
3
|
+
entries = new Map;
|
|
4
|
+
async read(providerId, options) {
|
|
5
|
+
options?.signal?.throwIfAborted();
|
|
6
|
+
const entry = this.entries.get(providerId);
|
|
7
|
+
return entry ? structuredClone(entry) : undefined;
|
|
8
|
+
}
|
|
9
|
+
async write(providerId, entry, options) {
|
|
10
|
+
options?.signal?.throwIfAborted();
|
|
11
|
+
this.entries.set(providerId, structuredClone(entry));
|
|
12
|
+
}
|
|
13
|
+
async delete(providerId, options) {
|
|
14
|
+
options?.signal?.throwIfAborted();
|
|
15
|
+
this.entries.delete(providerId);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { InMemoryModelsStore2 };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/utils/transcript.ts
|
|
2
|
+
import {
|
|
3
|
+
collapseSystemMessages as collapseSystemMessages2,
|
|
4
|
+
createInitialSystemMessage as createInitialSystemMessage2,
|
|
5
|
+
declarationsEqual as declarationsEqual2,
|
|
6
|
+
getCurrentSystemMessage as getCurrentSystemMessage2,
|
|
7
|
+
getCurrentSystemPrompt as getCurrentSystemPrompt2,
|
|
8
|
+
getCurrentTools as getCurrentTools2,
|
|
9
|
+
getDeclaredTools as getDeclaredTools2,
|
|
10
|
+
getInitialSystemMessage as getInitialSystemMessage2,
|
|
11
|
+
getToolStateChanges as getToolStateChanges2,
|
|
12
|
+
hasNonAdditiveToolChanges as hasNonAdditiveToolChanges2,
|
|
13
|
+
hasToolRedefinitions as hasToolRedefinitions2,
|
|
14
|
+
normalizeContext as normalizeContext2,
|
|
15
|
+
resolveTranscript as resolveTranscript2,
|
|
16
|
+
resolveTranscriptTools as resolveTranscriptTools2,
|
|
17
|
+
toToolDeclaration as toToolDeclaration2,
|
|
18
|
+
withoutInitialSystemMessage as withoutInitialSystemMessage2
|
|
19
|
+
} from "@nyte-ai/schema/model";
|
|
20
|
+
|
|
21
|
+
export { collapseSystemMessages2, createInitialSystemMessage2, declarationsEqual2, getCurrentSystemMessage2, getCurrentSystemPrompt2, getCurrentTools2, getDeclaredTools2, getInitialSystemMessage2, getToolStateChanges2, hasNonAdditiveToolChanges2, hasToolRedefinitions2, normalizeContext2, resolveTranscript2, resolveTranscriptTools2, toToolDeclaration2, withoutInitialSystemMessage2 };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// src/auth/helpers.ts
|
|
2
|
+
function envApiKeyAuth2(name, envVars) {
|
|
3
|
+
return {
|
|
4
|
+
name,
|
|
5
|
+
login: async (interaction) => {
|
|
6
|
+
interaction.signal.throwIfAborted();
|
|
7
|
+
const key = await interaction.prompt({ type: "secret", message: `Enter ${name}` });
|
|
8
|
+
interaction.signal.throwIfAborted();
|
|
9
|
+
return { type: "api_key", key };
|
|
10
|
+
},
|
|
11
|
+
resolve: async ({ ctx, credential, signal }) => {
|
|
12
|
+
signal.throwIfAborted();
|
|
13
|
+
if (credential?.key) {
|
|
14
|
+
return {
|
|
15
|
+
auth: { apiKey: credential.key },
|
|
16
|
+
env: credential.env,
|
|
17
|
+
source: "stored credential"
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
for (const envVar of envVars) {
|
|
21
|
+
const value = await ctx.env(envVar);
|
|
22
|
+
signal.throwIfAborted();
|
|
23
|
+
if (value)
|
|
24
|
+
return { auth: { apiKey: value }, source: envVar };
|
|
25
|
+
}
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function lazyOAuth2(input) {
|
|
31
|
+
let promise;
|
|
32
|
+
const loaded = () => {
|
|
33
|
+
promise ??= input.load().catch((error) => {
|
|
34
|
+
promise = undefined;
|
|
35
|
+
throw error;
|
|
36
|
+
});
|
|
37
|
+
return promise;
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
name: input.name,
|
|
41
|
+
isSubscription: input.isSubscription,
|
|
42
|
+
loginLabel: input.loginLabel,
|
|
43
|
+
login: async (interaction) => (await loaded()).login(interaction),
|
|
44
|
+
refresh: async (credential, signal) => (await loaded()).refresh(credential, signal),
|
|
45
|
+
toAuth: async (credential) => (await loaded()).toAuth(credential)
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { envApiKeyAuth2, lazyOAuth2 };
|