@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,77 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FifoQueue2
|
|
3
|
+
} from "./account-limits-cache-nn9jebsj.js";
|
|
4
|
+
|
|
5
|
+
// src/utils/event-stream.ts
|
|
6
|
+
class EventStream2 {
|
|
7
|
+
queue = new FifoQueue2;
|
|
8
|
+
waiting = new FifoQueue2;
|
|
9
|
+
done = false;
|
|
10
|
+
finalResult = Promise.withResolvers();
|
|
11
|
+
isComplete;
|
|
12
|
+
extractResult;
|
|
13
|
+
constructor(isComplete, extractResult) {
|
|
14
|
+
this.isComplete = isComplete;
|
|
15
|
+
this.extractResult = extractResult;
|
|
16
|
+
}
|
|
17
|
+
push(event) {
|
|
18
|
+
if (this.done)
|
|
19
|
+
return;
|
|
20
|
+
if (this.isComplete(event)) {
|
|
21
|
+
this.done = true;
|
|
22
|
+
this.finalResult.resolve(this.extractResult(event));
|
|
23
|
+
}
|
|
24
|
+
const next = { value: event, done: false };
|
|
25
|
+
const waiter = this.waiting.dequeue();
|
|
26
|
+
if (waiter) {
|
|
27
|
+
waiter(next);
|
|
28
|
+
} else {
|
|
29
|
+
this.queue.enqueue(next);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
end(result) {
|
|
33
|
+
this.done = true;
|
|
34
|
+
if (result !== undefined) {
|
|
35
|
+
this.finalResult.resolve(result);
|
|
36
|
+
}
|
|
37
|
+
for (let waiter = this.waiting.dequeue();waiter; waiter = this.waiting.dequeue()) {
|
|
38
|
+
waiter({ value: undefined, done: true });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async* [Symbol.asyncIterator]() {
|
|
42
|
+
while (true) {
|
|
43
|
+
const queued = this.queue.dequeue();
|
|
44
|
+
if (queued) {
|
|
45
|
+
yield queued.value;
|
|
46
|
+
} else if (this.done) {
|
|
47
|
+
return;
|
|
48
|
+
} else {
|
|
49
|
+
const result = await new Promise((resolve) => this.waiting.enqueue(resolve));
|
|
50
|
+
if (result.done)
|
|
51
|
+
return;
|
|
52
|
+
yield result.value;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
result() {
|
|
57
|
+
return this.finalResult.promise;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
class AssistantMessageEventStream2 extends EventStream2 {
|
|
62
|
+
constructor() {
|
|
63
|
+
super((event) => event.type === "done" || event.type === "error", (event) => {
|
|
64
|
+
if (event.type === "done") {
|
|
65
|
+
return event.message;
|
|
66
|
+
} else if (event.type === "error") {
|
|
67
|
+
return event.error;
|
|
68
|
+
}
|
|
69
|
+
throw new Error("Unexpected event type for final result");
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function createAssistantMessageEventStream2() {
|
|
74
|
+
return new AssistantMessageEventStream2;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { EventStream2, AssistantMessageEventStream2, createAssistantMessageEventStream2 };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// src/utils/headers.ts
|
|
2
|
+
function headersToRecord2(headers) {
|
|
3
|
+
return Object.fromEntries(headers.entries());
|
|
4
|
+
}
|
|
5
|
+
function mergeProviderHeaders2(...headerSources) {
|
|
6
|
+
const merged = new Map;
|
|
7
|
+
for (const source of headerSources) {
|
|
8
|
+
for (const [name, value] of Object.entries(source ?? {})) {
|
|
9
|
+
const normalizedName = name.toLowerCase();
|
|
10
|
+
merged.delete(normalizedName);
|
|
11
|
+
merged.set(normalizedName, [name, value]);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return Object.fromEntries(merged.values());
|
|
15
|
+
}
|
|
16
|
+
function providerHeadersToRecord2(...headerSources) {
|
|
17
|
+
const entries = Object.entries(mergeProviderHeaders2(...headerSources)).filter((entry) => entry[1] !== null);
|
|
18
|
+
return entries.length > 0 ? Object.fromEntries(entries) : undefined;
|
|
19
|
+
}
|
|
20
|
+
function hasHeader2(headers, name) {
|
|
21
|
+
if (!headers)
|
|
22
|
+
return false;
|
|
23
|
+
const expected = name.toLowerCase();
|
|
24
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
25
|
+
if (key.toLowerCase() === expected && value !== null && value.trim().length > 0)
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
function getClientApiKey2(provider, apiKey, headers) {
|
|
31
|
+
if (apiKey)
|
|
32
|
+
return apiKey;
|
|
33
|
+
if (hasHeader2(headers, "authorization") || hasHeader2(headers, "cf-aig-authorization"))
|
|
34
|
+
return "unused";
|
|
35
|
+
throw new Error(`No API key for provider: ${provider}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { headersToRecord2, mergeProviderHeaders2, providerHeadersToRecord2, hasHeader2, getClientApiKey2 };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// src/auth/types.ts
|
|
2
|
+
import { typed } from "@nyte-ai/schema/model";
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
import { Value } from "typebox/value";
|
|
5
|
+
var OAuthTokenResponseSchema2 = typed()(Type.Object({
|
|
6
|
+
access_token: Type.String({ minLength: 1 }),
|
|
7
|
+
refresh_token: Type.String({ minLength: 1 }),
|
|
8
|
+
expires_in: Type.Number()
|
|
9
|
+
}));
|
|
10
|
+
var CredentialSchema = Type.Union([
|
|
11
|
+
Type.Object({
|
|
12
|
+
type: Type.Literal("api_key"),
|
|
13
|
+
key: Type.Optional(Type.String()),
|
|
14
|
+
env: Type.Optional(Type.Record(Type.String(), Type.String()))
|
|
15
|
+
}),
|
|
16
|
+
Type.Object({
|
|
17
|
+
type: Type.Literal("oauth"),
|
|
18
|
+
refresh: Type.String(),
|
|
19
|
+
access: Type.String(),
|
|
20
|
+
expires: Type.Number()
|
|
21
|
+
})
|
|
22
|
+
]);
|
|
23
|
+
var CredentialAccountsSchema = Type.Object({
|
|
24
|
+
active: Type.Union([Type.String(), Type.Null()]),
|
|
25
|
+
accounts: Type.Array(Type.Object({
|
|
26
|
+
id: Type.String({ minLength: 1 }),
|
|
27
|
+
label: Type.String({ minLength: 1 }),
|
|
28
|
+
credential: CredentialSchema
|
|
29
|
+
}))
|
|
30
|
+
});
|
|
31
|
+
function readCredentialAccounts2(value) {
|
|
32
|
+
if (Value.Check(CredentialSchema, value))
|
|
33
|
+
return {
|
|
34
|
+
active: "default",
|
|
35
|
+
accounts: [{ id: "default", label: "Default", credential: value }]
|
|
36
|
+
};
|
|
37
|
+
if (!Value.Check(CredentialAccountsSchema, value))
|
|
38
|
+
return;
|
|
39
|
+
const ids = new Set(value.accounts.map((account) => account.id));
|
|
40
|
+
if (ids.size !== value.accounts.length || value.active !== null && !ids.has(value.active))
|
|
41
|
+
return;
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { OAuthTokenResponseSchema2, readCredentialAccounts2 };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// src/utils/provider-env.ts
|
|
2
|
+
var procEnvCache = null;
|
|
3
|
+
function getBunSandboxEnvValue(name) {
|
|
4
|
+
if (typeof process === "undefined" || !process.versions?.bun || Object.keys(process.env).length > 0) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
if (procEnvCache === null) {
|
|
8
|
+
procEnvCache = new Map;
|
|
9
|
+
try {
|
|
10
|
+
const data = process.getBuiltinModule("node:fs").readFileSync("/proc/self/environ", "utf-8");
|
|
11
|
+
for (const entry of data.split("\x00")) {
|
|
12
|
+
const variable = /^([^=]+)=(.*)$/su.exec(entry);
|
|
13
|
+
if (variable)
|
|
14
|
+
procEnvCache.set(variable[1], variable[2]);
|
|
15
|
+
}
|
|
16
|
+
} catch {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return procEnvCache.get(name);
|
|
21
|
+
}
|
|
22
|
+
function getProviderEnvValue2(name, env) {
|
|
23
|
+
return env?.[name] || (typeof process !== "undefined" ? process.env[name] : undefined) || getBunSandboxEnvValue(name) || undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { getProviderEnvValue2 };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// src/utils/text.ts
|
|
2
|
+
import {
|
|
3
|
+
contentText as contentText2,
|
|
4
|
+
getSystemMessageText as getSystemMessageText2,
|
|
5
|
+
renderSystemMessageUpdate as renderSystemMessageUpdate2
|
|
6
|
+
} from "@nyte-ai/schema/model";
|
|
7
|
+
|
|
8
|
+
export { contentText2, getSystemMessageText2, renderSystemMessageUpdate2 };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sleep2
|
|
3
|
+
} from "./account-limits-cache-j2bt392s.js";
|
|
4
|
+
import {
|
|
5
|
+
classifyAssistantFailure2,
|
|
6
|
+
retryCanHelp2
|
|
7
|
+
} from "./account-limits-cache-xds88kxd.js";
|
|
8
|
+
|
|
9
|
+
// src/utils/retry.ts
|
|
10
|
+
var DEFAULT_MAX_AGENT_RETRY_DELAY_MS2 = 60000;
|
|
11
|
+
function retryDelayMs2(policy, attempt) {
|
|
12
|
+
const delay = policy.baseDelayMs * 2 ** Math.max(0, attempt - 1);
|
|
13
|
+
const safeDelay = Number.isSafeInteger(delay) ? delay : Number.MAX_SAFE_INTEGER;
|
|
14
|
+
return Math.min(safeDelay, policy.maxAgentDelayMs ?? DEFAULT_MAX_AGENT_RETRY_DELAY_MS2);
|
|
15
|
+
}
|
|
16
|
+
function assistantRetryDelayMs2(policy, attempt, retryAfterMs) {
|
|
17
|
+
const asked = retryAfterMs ?? 0;
|
|
18
|
+
if (asked > (policy.maxAgentDelayMs ?? DEFAULT_MAX_AGENT_RETRY_DELAY_MS2))
|
|
19
|
+
return null;
|
|
20
|
+
return Math.max(asked, retryDelayMs2(policy, attempt));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class RetrySleepAbortError extends Error {
|
|
24
|
+
constructor() {
|
|
25
|
+
super("Aborted");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function abortableSleep2(ms, signal) {
|
|
29
|
+
return sleep2(ms, signal, () => new RetrySleepAbortError);
|
|
30
|
+
}
|
|
31
|
+
async function retryAssistantCall2(produce, policy, signal, callbacks) {
|
|
32
|
+
const maxAttempts = policy?.enabled ? policy.maxRetries : 0;
|
|
33
|
+
let attempt = 0;
|
|
34
|
+
let lastRetry;
|
|
35
|
+
for (;; ) {
|
|
36
|
+
const response = await produce();
|
|
37
|
+
if (response.stopReason === "aborted") {
|
|
38
|
+
if (lastRetry)
|
|
39
|
+
await callbacks?.onRetryFinished?.(false, lastRetry.attempt);
|
|
40
|
+
return response;
|
|
41
|
+
}
|
|
42
|
+
if (response.stopReason !== "error") {
|
|
43
|
+
if (lastRetry)
|
|
44
|
+
await callbacks?.onRetryFinished?.(true, lastRetry.attempt);
|
|
45
|
+
return response;
|
|
46
|
+
}
|
|
47
|
+
const failure = classifyAssistantFailure2(response);
|
|
48
|
+
const delayMs = policy === undefined || attempt >= maxAttempts || !retryCanHelp2(failure.class) ? null : assistantRetryDelayMs2(policy, attempt + 1, failure.retryAfterMs);
|
|
49
|
+
if (delayMs === null) {
|
|
50
|
+
if (lastRetry)
|
|
51
|
+
await callbacks?.onRetryFinished?.(false, lastRetry.attempt, response.errorMessage);
|
|
52
|
+
return response;
|
|
53
|
+
}
|
|
54
|
+
attempt++;
|
|
55
|
+
lastRetry = { attempt, errorMessage: response.errorMessage || "Unknown error" };
|
|
56
|
+
await callbacks?.onRetryScheduled?.(attempt, maxAttempts, delayMs, lastRetry.errorMessage);
|
|
57
|
+
try {
|
|
58
|
+
await abortableSleep2(delayMs, signal);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
await callbacks?.onRetryFinished?.(false, attempt, lastRetry.errorMessage);
|
|
61
|
+
if (error instanceof RetrySleepAbortError) {
|
|
62
|
+
const { errorMessage: _errorMessage, ...rest } = response;
|
|
63
|
+
return { ...rest, stopReason: "aborted" };
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
await callbacks?.onRetryAttemptStart?.();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function failedTransiently2(message) {
|
|
71
|
+
return message.stopReason === "error" && retryCanHelp2(classifyAssistantFailure2(message).class);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { DEFAULT_MAX_AGENT_RETRY_DELAY_MS2, retryDelayMs2, assistantRetryDelayMs2, abortableSleep2, retryAssistantCall2, failedTransiently2 };
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defaultNyteHome2
|
|
3
|
+
} from "./account-limits-cache-rz8p1g5y.js";
|
|
4
|
+
import {
|
|
5
|
+
readCredentialAccounts2
|
|
6
|
+
} from "./account-limits-cache-b0x2sfya.js";
|
|
7
|
+
|
|
8
|
+
// src/auth/store.ts
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
import {
|
|
11
|
+
closeSync,
|
|
12
|
+
existsSync,
|
|
13
|
+
mkdirSync,
|
|
14
|
+
openSync,
|
|
15
|
+
readFileSync,
|
|
16
|
+
renameSync,
|
|
17
|
+
rmSync,
|
|
18
|
+
statSync,
|
|
19
|
+
writeFileSync,
|
|
20
|
+
writeSync
|
|
21
|
+
} from "node:fs";
|
|
22
|
+
import { hostname } from "node:os";
|
|
23
|
+
import { dirname, join } from "node:path";
|
|
24
|
+
import process from "node:process";
|
|
25
|
+
import { Type } from "typebox";
|
|
26
|
+
import { Value } from "typebox/value";
|
|
27
|
+
function defaultAuthPath2() {
|
|
28
|
+
return join(defaultNyteHome2(), "auth.json");
|
|
29
|
+
}
|
|
30
|
+
var LOCK_TIMEOUT_MS = 30000;
|
|
31
|
+
var LOCK_RETRY_MS = 25;
|
|
32
|
+
var LOCK_ABANDONED_MS = 2 * LOCK_TIMEOUT_MS;
|
|
33
|
+
var RECOVERY_STALE_MS = 1000;
|
|
34
|
+
var CredentialFileSchema = Type.Record(Type.String(), Type.Unknown());
|
|
35
|
+
var SystemErrorSchema = Type.Object({ code: Type.String() });
|
|
36
|
+
function errorCode(cause) {
|
|
37
|
+
return Value.Check(SystemErrorSchema, cause) ? cause.code : undefined;
|
|
38
|
+
}
|
|
39
|
+
function lockOwner() {
|
|
40
|
+
return `${process.pid} ${hostname()} ${randomUUID()}`;
|
|
41
|
+
}
|
|
42
|
+
function readOwner(lockPath) {
|
|
43
|
+
try {
|
|
44
|
+
return readFileSync(lockPath, "utf8");
|
|
45
|
+
} catch {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function localOwnerPid(owner) {
|
|
50
|
+
if (owner === undefined)
|
|
51
|
+
return;
|
|
52
|
+
const [pid, host] = owner.split(" ");
|
|
53
|
+
if (host !== hostname())
|
|
54
|
+
return;
|
|
55
|
+
const parsed = Number(pid);
|
|
56
|
+
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : undefined;
|
|
57
|
+
}
|
|
58
|
+
function processAlive(pid) {
|
|
59
|
+
try {
|
|
60
|
+
process.kill(pid, 0);
|
|
61
|
+
return true;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
return errorCode(error) === "EPERM";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function ageMs(path) {
|
|
67
|
+
try {
|
|
68
|
+
return Date.now() - statSync(path).mtimeMs;
|
|
69
|
+
} catch {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function lockAbandoned(lockPath) {
|
|
74
|
+
const pid = localOwnerPid(readOwner(lockPath));
|
|
75
|
+
if (pid !== undefined && !processAlive(pid))
|
|
76
|
+
return true;
|
|
77
|
+
const age = ageMs(lockPath);
|
|
78
|
+
return age !== undefined && age > LOCK_ABANDONED_MS;
|
|
79
|
+
}
|
|
80
|
+
function createLock(lockPath, owner) {
|
|
81
|
+
let handle;
|
|
82
|
+
try {
|
|
83
|
+
handle = openSync(lockPath, "wx", 384);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (errorCode(error) === "EEXIST")
|
|
86
|
+
return false;
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
writeSync(handle, owner);
|
|
91
|
+
} finally {
|
|
92
|
+
closeSync(handle);
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
function reclaimAbandonedLock(lockPath, owner) {
|
|
97
|
+
const recoveryPath = `${lockPath}.recovery`;
|
|
98
|
+
if (!createLock(recoveryPath, owner)) {
|
|
99
|
+
const age = ageMs(recoveryPath);
|
|
100
|
+
if (age !== undefined && age > RECOVERY_STALE_MS)
|
|
101
|
+
rmSync(recoveryPath, { force: true });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
if (lockAbandoned(lockPath))
|
|
106
|
+
rmSync(lockPath, { force: true });
|
|
107
|
+
} finally {
|
|
108
|
+
rmSync(recoveryPath, { force: true });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function acquireLock(lockPath, signal) {
|
|
112
|
+
const owner = lockOwner();
|
|
113
|
+
const lock = {
|
|
114
|
+
held: () => readOwner(lockPath) === owner,
|
|
115
|
+
release: () => {
|
|
116
|
+
if (readOwner(lockPath) !== owner)
|
|
117
|
+
return;
|
|
118
|
+
try {
|
|
119
|
+
rmSync(lockPath, { force: true });
|
|
120
|
+
} catch {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
126
|
+
for (;; ) {
|
|
127
|
+
signal?.throwIfAborted();
|
|
128
|
+
if (createLock(lockPath, owner))
|
|
129
|
+
return lock;
|
|
130
|
+
if (Date.now() >= deadline) {
|
|
131
|
+
throw new Error(`Timed out waiting for the credential lock at ${lockPath}`);
|
|
132
|
+
}
|
|
133
|
+
if (lockAbandoned(lockPath))
|
|
134
|
+
reclaimAbandonedLock(lockPath, owner);
|
|
135
|
+
await new Promise((resolve) => {
|
|
136
|
+
setTimeout(resolve, LOCK_RETRY_MS);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
class FileCredentialStore2 {
|
|
142
|
+
path;
|
|
143
|
+
lockPath;
|
|
144
|
+
constructor(path = defaultAuthPath2()) {
|
|
145
|
+
this.path = path;
|
|
146
|
+
this.lockPath = `${path}.lock`;
|
|
147
|
+
}
|
|
148
|
+
load() {
|
|
149
|
+
if (!existsSync(this.path))
|
|
150
|
+
return {};
|
|
151
|
+
const text = readFileSync(this.path, "utf8");
|
|
152
|
+
let parsed;
|
|
153
|
+
try {
|
|
154
|
+
parsed = JSON.parse(text);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
throw new Error(`${this.path} is not valid JSON. Fix or remove it, then sign in again.`, {
|
|
157
|
+
cause: error
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
if (!Value.Check(CredentialFileSchema, parsed)) {
|
|
161
|
+
throw new Error(`${this.path} does not hold credentials keyed by provider`);
|
|
162
|
+
}
|
|
163
|
+
return parsed;
|
|
164
|
+
}
|
|
165
|
+
save(lock, entries) {
|
|
166
|
+
if (!lock.held()) {
|
|
167
|
+
throw new Error(`Another process took over ${this.lockPath}; nothing was written`);
|
|
168
|
+
}
|
|
169
|
+
const staging = `${this.path}.${process.pid}.tmp`;
|
|
170
|
+
try {
|
|
171
|
+
writeFileSync(staging, `${JSON.stringify(entries, null, 2)}
|
|
172
|
+
`, { mode: 384 });
|
|
173
|
+
renameSync(staging, this.path);
|
|
174
|
+
} catch (error) {
|
|
175
|
+
rmSync(staging, { force: true });
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
async withLock(run, options) {
|
|
180
|
+
options?.signal?.throwIfAborted();
|
|
181
|
+
mkdirSync(dirname(this.path), { recursive: true, mode: 448 });
|
|
182
|
+
const lock = await acquireLock(this.lockPath, options?.signal);
|
|
183
|
+
try {
|
|
184
|
+
return await run(lock);
|
|
185
|
+
} finally {
|
|
186
|
+
lock.release();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async read(providerId, options) {
|
|
190
|
+
options?.signal?.throwIfAborted();
|
|
191
|
+
return readCredentialAccounts2(this.load()[providerId]);
|
|
192
|
+
}
|
|
193
|
+
async list(options) {
|
|
194
|
+
options?.signal?.throwIfAborted();
|
|
195
|
+
const stored = [];
|
|
196
|
+
for (const [providerId, value] of Object.entries(this.load())) {
|
|
197
|
+
const accounts = readCredentialAccounts2(value);
|
|
198
|
+
if (accounts)
|
|
199
|
+
stored.push(...accounts.accounts.map((account) => ({
|
|
200
|
+
providerId,
|
|
201
|
+
id: account.id,
|
|
202
|
+
label: account.label,
|
|
203
|
+
type: account.credential.type,
|
|
204
|
+
active: account.id === accounts.active
|
|
205
|
+
})));
|
|
206
|
+
}
|
|
207
|
+
return stored;
|
|
208
|
+
}
|
|
209
|
+
modify(providerId, fn, options) {
|
|
210
|
+
return this.withLock(async (lock) => {
|
|
211
|
+
const entries = this.load();
|
|
212
|
+
const current = readCredentialAccounts2(entries[providerId]);
|
|
213
|
+
const next = await fn(current);
|
|
214
|
+
if (next !== undefined) {
|
|
215
|
+
if (next.accounts.length === 0)
|
|
216
|
+
delete entries[providerId];
|
|
217
|
+
else
|
|
218
|
+
entries[providerId] = next;
|
|
219
|
+
this.save(lock, entries);
|
|
220
|
+
}
|
|
221
|
+
return next ?? current;
|
|
222
|
+
}, options);
|
|
223
|
+
}
|
|
224
|
+
delete(providerId, options) {
|
|
225
|
+
return this.withLock(async (lock) => {
|
|
226
|
+
const entries = this.load();
|
|
227
|
+
if (providerId in entries) {
|
|
228
|
+
delete entries[providerId];
|
|
229
|
+
this.save(lock, entries);
|
|
230
|
+
}
|
|
231
|
+
}, options);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export { defaultAuthPath2, FileCredentialStore2 };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/api/openai-prompt-cache.ts
|
|
2
|
+
var OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH = 64;
|
|
3
|
+
function clampOpenAIPromptCacheKey2(key) {
|
|
4
|
+
if (key === undefined)
|
|
5
|
+
return;
|
|
6
|
+
const chars = Array.from(key);
|
|
7
|
+
if (chars.length <= OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH)
|
|
8
|
+
return key;
|
|
9
|
+
return chars.slice(0, OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH).join("");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { clampOpenAIPromptCacheKey2 };
|