@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,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,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 };
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GITHUB_COPILOT_API_VERSION2,
|
|
3
|
+
GITHUB_COPILOT_DEFAULT_ORIGIN2,
|
|
4
|
+
GITHUB_COPILOT_HEADERS2
|
|
5
|
+
} from "./account-limits-cache-za42x8a3.js";
|
|
6
|
+
import {
|
|
7
|
+
parseGitHubCopilotCatalog2
|
|
8
|
+
} from "./account-limits-cache-gs8a3fhw.js";
|
|
9
|
+
import {
|
|
10
|
+
raceWithAbortSignal2
|
|
11
|
+
} from "./account-limits-cache-j2bt392s.js";
|
|
12
|
+
import {
|
|
13
|
+
pollOAuthDeviceCodeFlow2
|
|
14
|
+
} from "./account-limits-cache-4hptgd2h.js";
|
|
15
|
+
|
|
16
|
+
// src/auth/oauth/github-copilot.ts
|
|
17
|
+
import { Type } from "typebox";
|
|
18
|
+
import { Value } from "typebox/value";
|
|
19
|
+
var DEFAULT_CLIENT_ID = "Iv1.b507a08c87ecfe98";
|
|
20
|
+
var DEVICE_CODE_LIFETIME_SECONDS = 15 * 60;
|
|
21
|
+
var EXPIRED_MESSAGE = "The device code expired before GitHub approved it. Sign in again.";
|
|
22
|
+
var CREDENTIAL_ERROR = "Invalid stored GitHub Copilot credential. Sign in to GitHub Copilot again.";
|
|
23
|
+
var KNOWN_GRANT_ERRORS = new Set([
|
|
24
|
+
"unsupported_grant_type",
|
|
25
|
+
"incorrect_client_credentials",
|
|
26
|
+
"incorrect_device_code",
|
|
27
|
+
"device_flow_disabled"
|
|
28
|
+
]);
|
|
29
|
+
var DeviceCodeResponse = Type.Object({
|
|
30
|
+
device_code: Type.String({ minLength: 1 }),
|
|
31
|
+
user_code: Type.String({ minLength: 1 }),
|
|
32
|
+
verification_uri: Type.String({ minLength: 1 }),
|
|
33
|
+
interval: Type.Optional(Type.Unknown()),
|
|
34
|
+
expires_in: Type.Optional(Type.Unknown())
|
|
35
|
+
});
|
|
36
|
+
var GitHubToken = Type.Object({ access_token: Type.String({ minLength: 1 }) });
|
|
37
|
+
var GrantError = Type.Object({ error: Type.String(), interval: Type.Optional(Type.Unknown()) });
|
|
38
|
+
var PositiveSeconds = Type.Number({ exclusiveMinimum: 0 });
|
|
39
|
+
var CopilotToken = Type.Object({
|
|
40
|
+
token: Type.String({ minLength: 1 }),
|
|
41
|
+
expires_at: Type.Number()
|
|
42
|
+
});
|
|
43
|
+
var CopilotCredential = Type.Object({
|
|
44
|
+
type: Type.Literal("oauth"),
|
|
45
|
+
refresh: Type.String({ minLength: 1 }),
|
|
46
|
+
access: Type.String({ minLength: 1 }),
|
|
47
|
+
expires: Type.Integer({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER }),
|
|
48
|
+
availableModelIds: Type.Array(Type.String({ minLength: 1 }))
|
|
49
|
+
});
|
|
50
|
+
function matchesCopilotCredential2(value) {
|
|
51
|
+
return Value.Check(CopilotCredential, value);
|
|
52
|
+
}
|
|
53
|
+
function copilotOrigin(token) {
|
|
54
|
+
const proxy = /(?:^|;)proxy-ep=([^;]+)/u.exec(token)?.[1];
|
|
55
|
+
if (proxy === undefined)
|
|
56
|
+
return GITHUB_COPILOT_DEFAULT_ORIGIN2;
|
|
57
|
+
const url = URL.parse(`https://${proxy.replace(/^proxy\./u, "api.")}`);
|
|
58
|
+
if (!url || url.protocol !== "https:" || !url.hostname.endsWith(".githubcopilot.com") || url.port !== "" || url.username !== "" || url.password !== "" || url.pathname !== "/" || url.search !== "" || url.hash !== "") {
|
|
59
|
+
throw new Error("GitHub Copilot token contains an untrusted API endpoint");
|
|
60
|
+
}
|
|
61
|
+
return url.origin;
|
|
62
|
+
}
|
|
63
|
+
async function requestCopilot(input) {
|
|
64
|
+
const bounded = AbortSignal.any([input.signal, AbortSignal.timeout(15000)]);
|
|
65
|
+
let response;
|
|
66
|
+
try {
|
|
67
|
+
response = await raceWithAbortSignal2((input.fetch ?? globalThis.fetch)(input.url, {
|
|
68
|
+
...input.init,
|
|
69
|
+
signal: bounded,
|
|
70
|
+
redirect: "error"
|
|
71
|
+
}), bounded);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (input.signal.aborted)
|
|
74
|
+
throw new Error("Login cancelled", { cause: error });
|
|
75
|
+
if (bounded.aborted)
|
|
76
|
+
throw new Error(`${input.describe} timed out`, { cause: error });
|
|
77
|
+
throw new Error(`${input.describe} failed: network error`, { cause: error });
|
|
78
|
+
}
|
|
79
|
+
if (!response.ok)
|
|
80
|
+
throw new Error(`${input.describe} failed (HTTP ${String(response.status)})`);
|
|
81
|
+
try {
|
|
82
|
+
return await raceWithAbortSignal2(response.json(), bounded);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (input.signal.aborted)
|
|
85
|
+
throw new Error("Login cancelled", { cause: error });
|
|
86
|
+
if (bounded.aborted)
|
|
87
|
+
throw new Error(`${input.describe} timed out`, { cause: error });
|
|
88
|
+
throw new Error(`${input.describe} returned an unreadable response`, { cause: error });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
var DEVICE_HEADERS = {
|
|
92
|
+
Accept: "application/json",
|
|
93
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
94
|
+
"User-Agent": GITHUB_COPILOT_HEADERS2["User-Agent"]
|
|
95
|
+
};
|
|
96
|
+
async function startDeviceAuthorization(input) {
|
|
97
|
+
const json = await requestCopilot({
|
|
98
|
+
fetch: input.fetch,
|
|
99
|
+
url: "https://github.com/login/device/code",
|
|
100
|
+
init: {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers: DEVICE_HEADERS,
|
|
103
|
+
body: new URLSearchParams({ client_id: input.clientId, scope: "read:user" })
|
|
104
|
+
},
|
|
105
|
+
signal: input.signal,
|
|
106
|
+
describe: "GitHub device code request"
|
|
107
|
+
});
|
|
108
|
+
if (!Value.Check(DeviceCodeResponse, json))
|
|
109
|
+
throw new Error("GitHub device code response is missing required fields");
|
|
110
|
+
const url = URL.parse(json.verification_uri);
|
|
111
|
+
if (!url || url.protocol !== "https:" || url.hostname !== "github.com" && url.hostname !== "www.github.com" || url.port !== "" || url.username !== "" || url.password !== "" || !url.pathname.startsWith("/login/")) {
|
|
112
|
+
throw new Error("GitHub device code response named a verification page outside github.com");
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
...json,
|
|
116
|
+
verification_uri: url.href,
|
|
117
|
+
interval: Value.Check(PositiveSeconds, json.interval) ? json.interval : undefined,
|
|
118
|
+
expires_in: Math.min(DEVICE_CODE_LIFETIME_SECONDS, Value.Check(PositiveSeconds, json.expires_in) ? json.expires_in : DEVICE_CODE_LIFETIME_SECONDS)
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
var LIFETIME_GRACE_MS = 1000;
|
|
122
|
+
async function pollForGitHubToken(input) {
|
|
123
|
+
const lifetime = AbortSignal.timeout(Math.ceil(input.device.expires_in * 1000) + LIFETIME_GRACE_MS);
|
|
124
|
+
const pollSignal = AbortSignal.any([input.signal, lifetime]);
|
|
125
|
+
try {
|
|
126
|
+
return await pollOAuthDeviceCodeFlow2({
|
|
127
|
+
intervalSeconds: input.device.interval,
|
|
128
|
+
expiresInSeconds: input.device.expires_in,
|
|
129
|
+
waitBeforeFirstPoll: true,
|
|
130
|
+
signal: pollSignal,
|
|
131
|
+
poll: async () => {
|
|
132
|
+
const json = await requestCopilot({
|
|
133
|
+
fetch: input.fetch,
|
|
134
|
+
url: "https://github.com/login/oauth/access_token",
|
|
135
|
+
init: {
|
|
136
|
+
method: "POST",
|
|
137
|
+
headers: DEVICE_HEADERS,
|
|
138
|
+
body: new URLSearchParams({
|
|
139
|
+
client_id: input.clientId,
|
|
140
|
+
device_code: input.device.device_code,
|
|
141
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
|
|
142
|
+
})
|
|
143
|
+
},
|
|
144
|
+
signal: pollSignal,
|
|
145
|
+
describe: "GitHub device authorization"
|
|
146
|
+
});
|
|
147
|
+
if (Value.Check(GitHubToken, json))
|
|
148
|
+
return { status: "complete", value: json };
|
|
149
|
+
if (!Value.Check(GrantError, json))
|
|
150
|
+
return { status: "failed", message: "GitHub device authorization response is malformed" };
|
|
151
|
+
switch (json.error) {
|
|
152
|
+
case "authorization_pending":
|
|
153
|
+
return { status: "pending" };
|
|
154
|
+
case "slow_down":
|
|
155
|
+
return {
|
|
156
|
+
status: "slow_down",
|
|
157
|
+
intervalSeconds: Value.Check(PositiveSeconds, json.interval) ? json.interval : undefined
|
|
158
|
+
};
|
|
159
|
+
case "expired_token":
|
|
160
|
+
return { status: "failed", message: EXPIRED_MESSAGE };
|
|
161
|
+
case "access_denied":
|
|
162
|
+
return { status: "failed", message: "GitHub sign-in was denied." };
|
|
163
|
+
default:
|
|
164
|
+
return {
|
|
165
|
+
status: "failed",
|
|
166
|
+
message: KNOWN_GRANT_ERRORS.has(json.error) ? `GitHub device authorization failed: ${json.error}` : "GitHub device authorization failed with an unrecognized error"
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (input.signal.aborted)
|
|
173
|
+
throw new Error("Login cancelled", { cause: error });
|
|
174
|
+
if (lifetime.aborted)
|
|
175
|
+
throw new Error(EXPIRED_MESSAGE, { cause: error });
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
async function loadCopilotCredential(input) {
|
|
180
|
+
const token = await requestCopilot({
|
|
181
|
+
fetch: input.fetch,
|
|
182
|
+
url: "https://api.github.com/copilot_internal/v2/token",
|
|
183
|
+
init: {
|
|
184
|
+
headers: {
|
|
185
|
+
Accept: "application/json",
|
|
186
|
+
Authorization: `Bearer ${input.github.access_token}`,
|
|
187
|
+
...GITHUB_COPILOT_HEADERS2
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
signal: input.signal,
|
|
191
|
+
describe: "GitHub Copilot token exchange"
|
|
192
|
+
});
|
|
193
|
+
if (!Value.Check(CopilotToken, token) || !Number.isSafeInteger(token.expires_at * 1000) || token.expires_at * 1000 <= Date.now()) {
|
|
194
|
+
throw new Error("GitHub Copilot token exchange returned an invalid token or expiry. Sign in to GitHub Copilot again.");
|
|
195
|
+
}
|
|
196
|
+
const origin = copilotOrigin(token.token);
|
|
197
|
+
const catalog = parseGitHubCopilotCatalog2(await requestCopilot({
|
|
198
|
+
fetch: input.fetch,
|
|
199
|
+
url: `${origin}/models`,
|
|
200
|
+
init: {
|
|
201
|
+
headers: {
|
|
202
|
+
Accept: "application/json",
|
|
203
|
+
Authorization: `Bearer ${token.token}`,
|
|
204
|
+
...GITHUB_COPILOT_HEADERS2,
|
|
205
|
+
"X-GitHub-Api-Version": GITHUB_COPILOT_API_VERSION2
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
signal: input.signal,
|
|
209
|
+
describe: "GitHub Copilot catalog request"
|
|
210
|
+
}), origin, input.knownModelIds);
|
|
211
|
+
input.signal.throwIfAborted();
|
|
212
|
+
return {
|
|
213
|
+
credential: {
|
|
214
|
+
type: "oauth",
|
|
215
|
+
refresh: input.github.access_token,
|
|
216
|
+
access: token.token,
|
|
217
|
+
expires: token.expires_at * 1000,
|
|
218
|
+
availableModelIds: catalog.availableModelIds
|
|
219
|
+
},
|
|
220
|
+
needsApproval: catalog.needsApproval
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function githubCopilotOAuth2(options = {}, knownModelIds = () => new Set) {
|
|
224
|
+
return {
|
|
225
|
+
name: "GitHub Copilot",
|
|
226
|
+
isSubscription: true,
|
|
227
|
+
loginLabel: "Sign in with GitHub",
|
|
228
|
+
login: async (interaction) => {
|
|
229
|
+
interaction.signal.throwIfAborted();
|
|
230
|
+
const environment = typeof process === "undefined" ? undefined : process.env.NYTE_GITHUB_COPILOT_CLIENT_ID;
|
|
231
|
+
const clientId = options.clientId || environment || DEFAULT_CLIENT_ID;
|
|
232
|
+
const device = await startDeviceAuthorization({
|
|
233
|
+
fetch: options.fetch,
|
|
234
|
+
clientId,
|
|
235
|
+
signal: interaction.signal
|
|
236
|
+
});
|
|
237
|
+
if (interaction.signal.aborted)
|
|
238
|
+
throw new Error("Login cancelled");
|
|
239
|
+
const deviceCode = {
|
|
240
|
+
type: "device_code",
|
|
241
|
+
userCode: device.user_code,
|
|
242
|
+
verificationUri: device.verification_uri,
|
|
243
|
+
intervalSeconds: device.interval,
|
|
244
|
+
expiresInSeconds: device.expires_in
|
|
245
|
+
};
|
|
246
|
+
if (clientId === DEFAULT_CLIENT_ID) {
|
|
247
|
+
deviceCode.instructions = "This sign-in uses GitHub's Copilot app, not a Nyte OAuth app.";
|
|
248
|
+
}
|
|
249
|
+
interaction.notify(deviceCode);
|
|
250
|
+
const github = await pollForGitHubToken({
|
|
251
|
+
fetch: options.fetch,
|
|
252
|
+
clientId,
|
|
253
|
+
device,
|
|
254
|
+
signal: interaction.signal
|
|
255
|
+
});
|
|
256
|
+
interaction.notify({ type: "progress", message: "Connecting to GitHub Copilot" });
|
|
257
|
+
const result = await loadCopilotCredential({
|
|
258
|
+
fetch: options.fetch,
|
|
259
|
+
github,
|
|
260
|
+
knownModelIds: knownModelIds(),
|
|
261
|
+
signal: interaction.signal
|
|
262
|
+
});
|
|
263
|
+
if (result.needsApproval)
|
|
264
|
+
interaction.notify({
|
|
265
|
+
type: "info",
|
|
266
|
+
message: "Some Copilot models need account approval. Enable them in your Copilot settings, then sign in again."
|
|
267
|
+
});
|
|
268
|
+
return result.credential;
|
|
269
|
+
},
|
|
270
|
+
refresh: async (previous, signal) => {
|
|
271
|
+
if (!matchesCopilotCredential2(previous))
|
|
272
|
+
throw new Error(CREDENTIAL_ERROR);
|
|
273
|
+
const result = await loadCopilotCredential({
|
|
274
|
+
fetch: options.fetch,
|
|
275
|
+
github: { access_token: previous.refresh },
|
|
276
|
+
knownModelIds: knownModelIds(),
|
|
277
|
+
signal
|
|
278
|
+
});
|
|
279
|
+
return result.credential;
|
|
280
|
+
},
|
|
281
|
+
async toAuth(credential) {
|
|
282
|
+
if (!matchesCopilotCredential2(credential))
|
|
283
|
+
throw new Error(CREDENTIAL_ERROR);
|
|
284
|
+
return {
|
|
285
|
+
apiKey: credential.access,
|
|
286
|
+
baseUrl: copilotOrigin(credential.access),
|
|
287
|
+
headers: GITHUB_COPILOT_HEADERS2
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export { matchesCopilotCredential2, githubCopilotOAuth2 };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/auth/oauth/authorization-input.ts
|
|
2
|
+
function parseAuthorizationInput2(input) {
|
|
3
|
+
const value = input.trim();
|
|
4
|
+
if (!value)
|
|
5
|
+
return {};
|
|
6
|
+
try {
|
|
7
|
+
const url = new URL(value);
|
|
8
|
+
return {
|
|
9
|
+
code: url.searchParams.get("code") ?? undefined,
|
|
10
|
+
state: url.searchParams.get("state") ?? undefined
|
|
11
|
+
};
|
|
12
|
+
} catch {}
|
|
13
|
+
if (value.includes("#")) {
|
|
14
|
+
const [code, state] = value.split("#", 2);
|
|
15
|
+
return { code, state };
|
|
16
|
+
}
|
|
17
|
+
if (value.includes("code=")) {
|
|
18
|
+
const params = new URLSearchParams(value);
|
|
19
|
+
return {
|
|
20
|
+
code: params.get("code") ?? undefined,
|
|
21
|
+
state: params.get("state") ?? undefined
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return { code: value };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { parseAuthorizationInput2 };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getProviderEnvValue2
|
|
3
|
+
} from "./account-limits-cache-b23sbrke.js";
|
|
4
|
+
|
|
5
|
+
// src/prompt-cache.ts
|
|
6
|
+
function resolveCacheRetention2(retention, env) {
|
|
7
|
+
if (retention !== undefined)
|
|
8
|
+
return retention;
|
|
9
|
+
return getProviderEnvValue2("NYTE_CACHE_RETENTION", env) === "short" ? "short" : "long";
|
|
10
|
+
}
|
|
11
|
+
function promptCacheMinimumTtlMs2(policy, retention) {
|
|
12
|
+
if (policy === undefined || retention === "none")
|
|
13
|
+
return;
|
|
14
|
+
return policy.minimumRetentionMs[retention];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { resolveCacheRetention2, promptCacheMinimumTtlMs2 };
|