@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.
Files changed (314) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +170 -2
  3. package/THIRD-PARTY-NOTICES.md +800 -0
  4. package/dist/chunks/account-limits-cache-031vfpt3.js +37 -0
  5. package/dist/chunks/account-limits-cache-09vs98g6.js +41 -0
  6. package/dist/chunks/account-limits-cache-0af5gqeg.js +15 -0
  7. package/dist/chunks/account-limits-cache-0yd6bde8.js +103 -0
  8. package/dist/chunks/account-limits-cache-1jv43fhh.js +22 -0
  9. package/dist/chunks/account-limits-cache-3ykqmnj5.js +88 -0
  10. package/dist/chunks/account-limits-cache-4b8ckwqw.js +293 -0
  11. package/dist/chunks/account-limits-cache-4fn5w63p.js +295 -0
  12. package/dist/chunks/account-limits-cache-4hptgd2h.js +49 -0
  13. package/dist/chunks/account-limits-cache-53e64mhx.js +417 -0
  14. package/dist/chunks/account-limits-cache-5jdhn0e1.js +562 -0
  15. package/dist/chunks/account-limits-cache-5rx2pfm5.js +8 -0
  16. package/dist/chunks/account-limits-cache-5ygxyyss.js +19 -0
  17. package/dist/chunks/account-limits-cache-6250ykn6.js +21 -0
  18. package/dist/chunks/account-limits-cache-6x05n06z.js +49 -0
  19. package/dist/chunks/account-limits-cache-736tdyme.js +6 -0
  20. package/dist/chunks/account-limits-cache-75smws02.js +213 -0
  21. package/dist/chunks/account-limits-cache-7z9s8j16.js +139 -0
  22. package/dist/chunks/account-limits-cache-8137j3zp.js +37 -0
  23. package/dist/chunks/account-limits-cache-8qzk56vj.js +160 -0
  24. package/dist/chunks/account-limits-cache-8s8bz4dr.js +36 -0
  25. package/dist/chunks/account-limits-cache-8vzx8k6r.js +166 -0
  26. package/dist/chunks/account-limits-cache-8wfqg73s.js +135 -0
  27. package/dist/chunks/account-limits-cache-90c9c2eb.js +50 -0
  28. package/dist/chunks/account-limits-cache-9wqk28n5.js +25 -0
  29. package/dist/chunks/account-limits-cache-a1jyaq00.js +56 -0
  30. package/dist/chunks/account-limits-cache-a558rjbg.js +138 -0
  31. package/dist/chunks/account-limits-cache-a55e14fr.js +70 -0
  32. package/dist/chunks/account-limits-cache-a6cgeagz.js +77 -0
  33. package/dist/chunks/account-limits-cache-ag5b8dja.js +38 -0
  34. package/dist/chunks/account-limits-cache-b0x2sfya.js +45 -0
  35. package/dist/chunks/account-limits-cache-b23sbrke.js +26 -0
  36. package/dist/chunks/account-limits-cache-b4edaybk.js +8 -0
  37. package/dist/chunks/account-limits-cache-bsknrh6t.js +74 -0
  38. package/dist/chunks/account-limits-cache-c7v93krb.js +235 -0
  39. package/dist/chunks/account-limits-cache-csbxvam7.js +12 -0
  40. package/dist/chunks/account-limits-cache-cw7q7vfy.js +293 -0
  41. package/dist/chunks/account-limits-cache-d1ypzqzp.js +27 -0
  42. package/dist/chunks/account-limits-cache-d2a51dka.js +17 -0
  43. package/dist/chunks/account-limits-cache-dejeygdt.js +114 -0
  44. package/dist/chunks/account-limits-cache-eejm0vth.js +11 -0
  45. package/dist/chunks/account-limits-cache-ehy0ze2b.js +33 -0
  46. package/dist/chunks/account-limits-cache-emg5b4hj.js +53 -0
  47. package/dist/chunks/account-limits-cache-eqwweapz.js +6 -0
  48. package/dist/chunks/account-limits-cache-eyx1jcxc.js +8 -0
  49. package/dist/chunks/account-limits-cache-f9z00x8c.js +36 -0
  50. package/dist/chunks/account-limits-cache-fbkaq46e.js +113 -0
  51. package/dist/chunks/account-limits-cache-fdwad56n.js +399 -0
  52. package/dist/chunks/account-limits-cache-fht68jp2.js +22 -0
  53. package/dist/chunks/account-limits-cache-fp03ztsw.js +17 -0
  54. package/dist/chunks/account-limits-cache-fsktnetm.js +69 -0
  55. package/dist/chunks/account-limits-cache-fvadc36m.js +110 -0
  56. package/dist/chunks/account-limits-cache-ghe9ne4j.js +69 -0
  57. package/dist/chunks/account-limits-cache-gknnm1kv.js +13 -0
  58. package/dist/chunks/account-limits-cache-gqsrbky9.js +40 -0
  59. package/dist/chunks/account-limits-cache-gr671dr2.js +0 -0
  60. package/dist/chunks/account-limits-cache-gs8a3fhw.js +32 -0
  61. package/dist/chunks/account-limits-cache-gsa5tzck.js +37 -0
  62. package/dist/chunks/account-limits-cache-h92bzz1j.js +235 -0
  63. package/dist/chunks/account-limits-cache-hemxtapd.js +142 -0
  64. package/dist/chunks/account-limits-cache-hj1h48sc.js +92 -0
  65. package/dist/chunks/account-limits-cache-hj3ek3ty.js +128 -0
  66. package/dist/chunks/account-limits-cache-hwehc67r.js +509 -0
  67. package/dist/chunks/account-limits-cache-hyzn1jmk.js +46 -0
  68. package/dist/chunks/account-limits-cache-j2bt392s.js +63 -0
  69. package/dist/chunks/account-limits-cache-k0pkzhbx.js +74 -0
  70. package/dist/chunks/account-limits-cache-kjx405q0.js +277 -0
  71. package/dist/chunks/account-limits-cache-ks1sjym9.js +37 -0
  72. package/dist/chunks/account-limits-cache-me1x57bs.js +89 -0
  73. package/dist/chunks/account-limits-cache-n3yk6g5c.js +314 -0
  74. package/dist/chunks/account-limits-cache-nbhht7xz.js +72 -0
  75. package/dist/chunks/account-limits-cache-nbkyhs6v.js +42 -0
  76. package/dist/chunks/account-limits-cache-nn9jebsj.js +26 -0
  77. package/dist/chunks/account-limits-cache-nxkprk3g.js +13 -0
  78. package/dist/chunks/account-limits-cache-nyy770yd.js +38 -0
  79. package/dist/chunks/account-limits-cache-pfa86y8e.js +45 -0
  80. package/dist/chunks/account-limits-cache-pfr14mcn.js +59 -0
  81. package/dist/chunks/account-limits-cache-q1ebtvet.js +8 -0
  82. package/dist/chunks/account-limits-cache-q63mc41j.js +51 -0
  83. package/dist/chunks/account-limits-cache-re8hpzwn.js +104 -0
  84. package/dist/chunks/account-limits-cache-rz8p1g5y.js +9 -0
  85. package/dist/chunks/account-limits-cache-skbfmzqe.js +7 -0
  86. package/dist/chunks/account-limits-cache-tc38g0ca.js +20 -0
  87. package/dist/chunks/account-limits-cache-v4n30az2.js +24 -0
  88. package/dist/chunks/account-limits-cache-vwgfz727.js +281 -0
  89. package/dist/chunks/account-limits-cache-x9fvb3s5.js +8 -0
  90. package/dist/chunks/account-limits-cache-xds88kxd.js +209 -0
  91. package/dist/chunks/account-limits-cache-xz65wyd1.js +428 -0
  92. package/dist/chunks/account-limits-cache-ydq8p8qp.js +39 -0
  93. package/dist/chunks/account-limits-cache-ykbqsvv6.js +134 -0
  94. package/dist/chunks/account-limits-cache-za42x8a3.js +25 -0
  95. package/dist/chunks/account-limits-cache-zcbkmrvy.js +79 -0
  96. package/dist/chunks/account-limits-cache-zgzptd0z.js +62 -0
  97. package/dist/chunks/account-limits-cache-zn01kscn.js +252 -0
  98. package/dist/chunks/account-limits-cache-zpjmeyt0.js +113 -0
  99. package/dist/chunks/account-limits-cache-zqdb4yh4.js +8 -0
  100. package/dist/src/account-limits-cache.d.ts +16 -0
  101. package/dist/src/account-limits-cache.js +7 -0
  102. package/dist/src/api/anthropic-client.d.ts +25 -0
  103. package/dist/src/api/anthropic-client.js +30 -0
  104. package/dist/src/api/anthropic-convert.d.ts +17 -0
  105. package/dist/src/api/anthropic-convert.js +14 -0
  106. package/dist/src/api/anthropic-events.d.ts +101 -0
  107. package/dist/src/api/anthropic-events.js +11 -0
  108. package/dist/src/api/anthropic-messages.d.ts +88 -0
  109. package/dist/src/api/anthropic-messages.js +578 -0
  110. package/dist/src/api/anthropic-messages.lazy.d.ts +2 -0
  111. package/dist/src/api/anthropic-messages.lazy.js +11 -0
  112. package/dist/src/api/assistant-stream.d.ts +23 -0
  113. package/dist/src/api/assistant-stream.js +15 -0
  114. package/dist/src/api/claude-code.d.ts +12 -0
  115. package/dist/src/api/claude-code.js +12 -0
  116. package/dist/src/api/cloudflare.d.ts +8 -0
  117. package/dist/src/api/cloudflare.js +11 -0
  118. package/dist/src/api/constrained-sampling.d.ts +24 -0
  119. package/dist/src/api/constrained-sampling.js +16 -0
  120. package/dist/src/api/deferred.d.ts +7 -0
  121. package/dist/src/api/deferred.js +10 -0
  122. package/dist/src/api/github-copilot-headers.d.ts +15 -0
  123. package/dist/src/api/github-copilot-headers.js +12 -0
  124. package/dist/src/api/google-generative-ai.d.ts +22 -0
  125. package/dist/src/api/google-generative-ai.js +583 -0
  126. package/dist/src/api/google-generative-ai.lazy.d.ts +2 -0
  127. package/dist/src/api/google-generative-ai.lazy.js +11 -0
  128. package/dist/src/api/lazy.d.ts +18 -0
  129. package/dist/src/api/lazy.js +12 -0
  130. package/dist/src/api/openai-codex-compact.d.ts +14 -0
  131. package/dist/src/api/openai-codex-compact.js +389 -0
  132. package/dist/src/api/openai-codex-compaction-error.d.ts +7 -0
  133. package/dist/src/api/openai-codex-compaction-error.js +6 -0
  134. package/dist/src/api/openai-codex-protocol.d.ts +125 -0
  135. package/dist/src/api/openai-codex-protocol.js +48 -0
  136. package/dist/src/api/openai-codex-responses.d.ts +4 -0
  137. package/dist/src/api/openai-codex-responses.js +175 -0
  138. package/dist/src/api/openai-codex-responses.lazy.d.ts +2 -0
  139. package/dist/src/api/openai-codex-responses.lazy.js +11 -0
  140. package/dist/src/api/openai-codex-websocket.d.ts +13 -0
  141. package/dist/src/api/openai-codex-websocket.js +37 -0
  142. package/dist/src/api/openai-codex-wire.d.ts +44 -0
  143. package/dist/src/api/openai-codex-wire.js +34 -0
  144. package/dist/src/api/openai-compact.d.ts +9 -0
  145. package/dist/src/api/openai-compact.js +12 -0
  146. package/dist/src/api/openai-completions-convert.d.ts +58 -0
  147. package/dist/src/api/openai-completions-convert.js +21 -0
  148. package/dist/src/api/openai-completions-params.d.ts +29 -0
  149. package/dist/src/api/openai-completions-params.js +20 -0
  150. package/dist/src/api/openai-completions-reasoning.d.ts +54 -0
  151. package/dist/src/api/openai-completions-reasoning.js +22 -0
  152. package/dist/src/api/openai-completions.d.ts +22 -0
  153. package/dist/src/api/openai-completions.js +484 -0
  154. package/dist/src/api/openai-completions.lazy.d.ts +5 -0
  155. package/dist/src/api/openai-completions.lazy.js +11 -0
  156. package/dist/src/api/openai-prompt-cache.d.ts +1 -0
  157. package/dist/src/api/openai-prompt-cache.js +6 -0
  158. package/dist/src/api/openai-responses-convert.d.ts +58 -0
  159. package/dist/src/api/openai-responses-convert.js +16 -0
  160. package/dist/src/api/openai-responses-events.d.ts +379 -0
  161. package/dist/src/api/openai-responses-events.js +12 -0
  162. package/dist/src/api/openai-responses-stream.d.ts +36 -0
  163. package/dist/src/api/openai-responses-stream.js +25 -0
  164. package/dist/src/api/openai-responses.d.ts +18 -0
  165. package/dist/src/api/openai-responses.js +276 -0
  166. package/dist/src/api/openai-responses.lazy.d.ts +2 -0
  167. package/dist/src/api/openai-responses.lazy.js +11 -0
  168. package/dist/src/api/simple-options.d.ts +17 -0
  169. package/dist/src/api/simple-options.js +18 -0
  170. package/dist/src/api/token-count.d.ts +5 -0
  171. package/dist/src/api/token-count.js +8 -0
  172. package/dist/src/api/transform-messages.d.ts +9 -0
  173. package/dist/src/api/transform-messages.js +6 -0
  174. package/dist/src/api-options.d.ts +27 -0
  175. package/dist/src/api-options.js +0 -0
  176. package/dist/src/auth/context.d.ts +11 -0
  177. package/dist/src/auth/context.js +6 -0
  178. package/dist/src/auth/credential-store.d.ts +17 -0
  179. package/dist/src/auth/credential-store.js +7 -0
  180. package/dist/src/auth/helpers.d.ts +24 -0
  181. package/dist/src/auth/helpers.js +8 -0
  182. package/dist/src/auth/oauth/anthropic.d.ts +7 -0
  183. package/dist/src/auth/oauth/anthropic.js +12 -0
  184. package/dist/src/auth/oauth/authorization-input.d.ts +14 -0
  185. package/dist/src/auth/oauth/authorization-input.js +6 -0
  186. package/dist/src/auth/oauth/device-code.d.ts +29 -0
  187. package/dist/src/auth/oauth/device-code.js +9 -0
  188. package/dist/src/auth/oauth/github-copilot.d.ts +23 -0
  189. package/dist/src/auth/oauth/github-copilot.js +12 -0
  190. package/dist/src/auth/oauth/load.d.ts +14 -0
  191. package/dist/src/auth/oauth/load.js +10 -0
  192. package/dist/src/auth/oauth/oauth-page.d.ts +13 -0
  193. package/dist/src/auth/oauth/oauth-page.js +8 -0
  194. package/dist/src/auth/oauth/openai-codex.d.ts +10 -0
  195. package/dist/src/auth/oauth/openai-codex.js +18 -0
  196. package/dist/src/auth/oauth/pkce.d.ts +12 -0
  197. package/dist/src/auth/oauth/pkce.js +6 -0
  198. package/dist/src/auth/resolve.d.ts +38 -0
  199. package/dist/src/auth/resolve.js +12 -0
  200. package/dist/src/auth/store.d.ts +29 -0
  201. package/dist/src/auth/store.js +10 -0
  202. package/dist/src/auth/types.d.ts +291 -0
  203. package/dist/src/auth/types.js +8 -0
  204. package/dist/src/auth/verify.d.ts +13 -0
  205. package/dist/src/auth/verify.js +7 -0
  206. package/dist/src/bun-oauth.d.ts +2 -0
  207. package/dist/src/bun-oauth.js +25 -0
  208. package/dist/src/env-api-keys.d.ts +32 -0
  209. package/dist/src/env-api-keys.js +27 -0
  210. package/dist/src/file-models-store.d.ts +18 -0
  211. package/dist/src/file-models-store.js +9 -0
  212. package/dist/src/index.d.ts +62 -0
  213. package/dist/src/index.js +372 -0
  214. package/dist/src/model-capabilities.d.ts +21 -0
  215. package/dist/src/model-capabilities.js +14 -0
  216. package/dist/src/model-pricing.d.ts +6 -0
  217. package/dist/src/model-pricing.js +10 -0
  218. package/dist/src/model-variants.d.ts +21 -0
  219. package/dist/src/model-variants.js +19 -0
  220. package/dist/src/models-refresh.d.ts +27 -0
  221. package/dist/src/models-refresh.js +9 -0
  222. package/dist/src/models-store.d.ts +31 -0
  223. package/dist/src/models-store.js +6 -0
  224. package/dist/src/models-types.d.ts +206 -0
  225. package/dist/src/models-types.js +0 -0
  226. package/dist/src/models.d.ts +7 -0
  227. package/dist/src/models.js +44 -0
  228. package/dist/src/prompt-cache.d.ts +5 -0
  229. package/dist/src/prompt-cache.js +9 -0
  230. package/dist/src/provider.d.ts +23 -0
  231. package/dist/src/provider.js +14 -0
  232. package/dist/src/providers/account-limits.d.ts +13 -0
  233. package/dist/src/providers/account-limits.js +8 -0
  234. package/dist/src/providers/anthropic-limits.d.ts +9 -0
  235. package/dist/src/providers/anthropic-limits.js +10 -0
  236. package/dist/src/providers/anthropic.d.ts +2 -0
  237. package/dist/src/providers/anthropic.js +25 -0
  238. package/dist/src/providers/github-copilot-catalog.d.ts +5 -0
  239. package/dist/src/providers/github-copilot-catalog.js +7 -0
  240. package/dist/src/providers/github-copilot.d.ts +2 -0
  241. package/dist/src/providers/github-copilot.js +23 -0
  242. package/dist/src/providers/nyte-catalog.d.ts +15 -0
  243. package/dist/src/providers/nyte-catalog.js +63 -0
  244. package/dist/src/providers/openai-codex-limits.d.ts +8 -0
  245. package/dist/src/providers/openai-codex-limits.js +14 -0
  246. package/dist/src/providers/openai-codex.d.ts +2 -0
  247. package/dist/src/providers/openai-codex.js +23 -0
  248. package/dist/src/providers/openai.d.ts +2 -0
  249. package/dist/src/providers/openai.js +19 -0
  250. package/dist/src/providers/opencode-go.d.ts +5 -0
  251. package/dist/src/providers/opencode-go.js +22 -0
  252. package/dist/src/providers/opencode-headers.d.ts +5 -0
  253. package/dist/src/providers/opencode-headers.js +6 -0
  254. package/dist/src/providers/opencode.d.ts +14 -0
  255. package/dist/src/providers/opencode.js +23 -0
  256. package/dist/src/providers/openrouter.d.ts +2 -0
  257. package/dist/src/providers/openrouter.js +19 -0
  258. package/dist/src/providers/vercel-ai-gateway.d.ts +3 -0
  259. package/dist/src/providers/vercel-ai-gateway.js +19 -0
  260. package/dist/src/session-resources.d.ts +9 -0
  261. package/dist/src/session-resources.js +10 -0
  262. package/dist/src/types.d.ts +210 -0
  263. package/dist/src/types.js +1 -0
  264. package/dist/src/utils/abort.d.ts +12 -0
  265. package/dist/src/utils/abort.js +10 -0
  266. package/dist/src/utils/deferred-tools.d.ts +11 -0
  267. package/dist/src/utils/deferred-tools.js +6 -0
  268. package/dist/src/utils/diagnostics.d.ts +11 -0
  269. package/dist/src/utils/diagnostics.js +12 -0
  270. package/dist/src/utils/error-body.d.ts +26 -0
  271. package/dist/src/utils/error-body.js +10 -0
  272. package/dist/src/utils/estimate.d.ts +20 -0
  273. package/dist/src/utils/estimate.js +14 -0
  274. package/dist/src/utils/event-stream.d.ts +22 -0
  275. package/dist/src/utils/event-stream.js +11 -0
  276. package/dist/src/utils/failure.d.ts +49 -0
  277. package/dist/src/utils/failure.js +10 -0
  278. package/dist/src/utils/fifo-queue.d.ts +11 -0
  279. package/dist/src/utils/fifo-queue.js +6 -0
  280. package/dist/src/utils/hash.d.ts +5 -0
  281. package/dist/src/utils/hash.js +6 -0
  282. package/dist/src/utils/headers.d.ts +12 -0
  283. package/dist/src/utils/headers.js +14 -0
  284. package/dist/src/utils/json-parse.d.ts +33 -0
  285. package/dist/src/utils/json-parse.js +12 -0
  286. package/dist/src/utils/node-http-proxy.d.ts +6 -0
  287. package/dist/src/utils/node-http-proxy.js +9 -0
  288. package/dist/src/utils/nyte-home.d.ts +2 -0
  289. package/dist/src/utils/nyte-home.js +6 -0
  290. package/dist/src/utils/nyte-user-agent.d.ts +1 -0
  291. package/dist/src/utils/nyte-user-agent.js +6 -0
  292. package/dist/src/utils/overflow.d.ts +18 -0
  293. package/dist/src/utils/overflow.js +9 -0
  294. package/dist/src/utils/provider-env.d.ts +9 -0
  295. package/dist/src/utils/provider-env.js +6 -0
  296. package/dist/src/utils/provider-retry.d.ts +22 -0
  297. package/dist/src/utils/provider-retry.js +9 -0
  298. package/dist/src/utils/retry.d.ts +66 -0
  299. package/dist/src/utils/retry.js +18 -0
  300. package/dist/src/utils/sanitize-unicode.d.ts +24 -0
  301. package/dist/src/utils/sanitize-unicode.js +6 -0
  302. package/dist/src/utils/sse.d.ts +21 -0
  303. package/dist/src/utils/sse.js +6 -0
  304. package/dist/src/utils/text.d.ts +5 -0
  305. package/dist/src/utils/text.js +10 -0
  306. package/dist/src/utils/transcript.d.ts +5 -0
  307. package/dist/src/utils/transcript.js +36 -0
  308. package/dist/src/utils/typebox-helpers.d.ts +19 -0
  309. package/dist/src/utils/typebox-helpers.js +6 -0
  310. package/dist/src/utils/uuid.d.ts +2 -0
  311. package/dist/src/utils/uuid.js +6 -0
  312. package/dist/src/utils/validation.d.ts +13 -0
  313. package/dist/src/utils/validation.js +8 -0
  314. package/package.json +152 -3
@@ -0,0 +1,110 @@
1
+ import {
2
+ providerHeadersToRecord2
3
+ } from "./account-limits-cache-ag5b8dja.js";
4
+ import {
5
+ getNyteUserAgent2
6
+ } from "./account-limits-cache-gknnm1kv.js";
7
+
8
+ // src/api/openai-codex-wire.ts
9
+ import { Type } from "typebox";
10
+ import { Value } from "typebox/value";
11
+ var CodexFrameJson2 = Type.Object({ type: Type.String() });
12
+ var TextJson = Type.String();
13
+ var BooleanJson = Type.Boolean();
14
+ var NumberJson = Type.Number();
15
+ var JsonObjectJson = Type.Record(Type.String(), Type.Unknown());
16
+ function readText2(value) {
17
+ return Value.Check(TextJson, value) ? value : undefined;
18
+ }
19
+ function readBoolean2(value) {
20
+ return Value.Check(BooleanJson, value) ? value : undefined;
21
+ }
22
+ function readNumber2(value) {
23
+ return Value.Check(NumberJson, value) ? value : undefined;
24
+ }
25
+ function readRecord2(value) {
26
+ return Value.Check(JsonObjectJson, value) ? value : undefined;
27
+ }
28
+ var TERMINAL_RESPONSE_TYPES2 = new Set([
29
+ "response.completed",
30
+ "response.done",
31
+ "response.incomplete",
32
+ "response.failed"
33
+ ]);
34
+ var DEFAULT_CODEX_BASE_URL2 = "https://chatgpt.com/backend-api";
35
+ var OPENAI_BETA_RESPONSES_WEBSOCKETS = "responses_websockets=2026-02-06";
36
+ var AccessTokenClaimsSchema = Type.Object({
37
+ "https://api.openai.com/auth": Type.Object({
38
+ chatgpt_account_id: Type.String({ minLength: 1 })
39
+ })
40
+ });
41
+ function normalizeTimeoutMs2(value) {
42
+ if (value === undefined)
43
+ return;
44
+ if (!Number.isFinite(value) || value < 0) {
45
+ throw new Error(`Invalid timeoutMs: ${String(value)}`);
46
+ }
47
+ return Math.floor(value);
48
+ }
49
+ function resolveCodexUrl2(baseUrl) {
50
+ const raw = baseUrl && baseUrl.trim().length > 0 ? baseUrl : DEFAULT_CODEX_BASE_URL2;
51
+ const normalized = raw.replace(/\/+$/, "");
52
+ if (normalized.endsWith("/codex/responses"))
53
+ return normalized;
54
+ if (normalized.endsWith("/codex"))
55
+ return `${normalized}/responses`;
56
+ return `${normalized}/codex/responses`;
57
+ }
58
+ function resolveCodexWebSocketUrl2(baseUrl) {
59
+ const url = new URL(resolveCodexUrl2(baseUrl));
60
+ if (url.protocol === "https:")
61
+ url.protocol = "wss:";
62
+ if (url.protocol === "http:")
63
+ url.protocol = "ws:";
64
+ return url.toString();
65
+ }
66
+ function extractAccountId2(token) {
67
+ const parts = token.split(".");
68
+ let claims;
69
+ try {
70
+ claims = parts.length === 3 ? JSON.parse(atob(parts[1])) : undefined;
71
+ } catch {
72
+ claims = undefined;
73
+ }
74
+ if (!Value.Check(AccessTokenClaimsSchema, claims)) {
75
+ throw new Error("Failed to extract accountId from token");
76
+ }
77
+ return claims["https://api.openai.com/auth"].chatgpt_account_id;
78
+ }
79
+ function buildBaseCodexHeaders2(initHeaders, additionalHeaders, accountId, token) {
80
+ const headers = new Headers(Object.entries(providerHeadersToRecord2(initHeaders, additionalHeaders) ?? {}));
81
+ headers.set("Authorization", `Bearer ${token}`);
82
+ headers.set("chatgpt-account-id", accountId);
83
+ headers.set("originator", "nyte");
84
+ headers.set("User-Agent", getNyteUserAgent2());
85
+ return headers;
86
+ }
87
+ function buildSSEHeaders2(initHeaders, additionalHeaders, accountId, token, sessionId) {
88
+ const headers = buildBaseCodexHeaders2(initHeaders, additionalHeaders, accountId, token);
89
+ headers.set("OpenAI-Beta", "responses=experimental");
90
+ headers.set("accept", "text/event-stream");
91
+ headers.set("content-type", "application/json");
92
+ if (sessionId) {
93
+ headers.set("session-id", sessionId);
94
+ headers.set("x-client-request-id", sessionId);
95
+ }
96
+ return headers;
97
+ }
98
+ function buildWebSocketHeaders2(initHeaders, additionalHeaders, accountId, token, requestId) {
99
+ const headers = buildBaseCodexHeaders2(initHeaders, additionalHeaders, accountId, token);
100
+ headers.delete("accept");
101
+ headers.delete("content-type");
102
+ headers.delete("OpenAI-Beta");
103
+ headers.delete("openai-beta");
104
+ headers.set("OpenAI-Beta", OPENAI_BETA_RESPONSES_WEBSOCKETS);
105
+ headers.set("x-client-request-id", requestId);
106
+ headers.set("session-id", requestId);
107
+ return headers;
108
+ }
109
+
110
+ export { CodexFrameJson2, readText2, readBoolean2, readNumber2, readRecord2, TERMINAL_RESPONSE_TYPES2, DEFAULT_CODEX_BASE_URL2, normalizeTimeoutMs2, resolveCodexUrl2, resolveCodexWebSocketUrl2, extractAccountId2, buildBaseCodexHeaders2, buildSSEHeaders2, buildWebSocketHeaders2 };
@@ -0,0 +1,69 @@
1
+ import {
2
+ defaultProviderAuthContext2
3
+ } from "./account-limits-cache-1jv43fhh.js";
4
+ import {
5
+ FileCredentialStore2
6
+ } from "./account-limits-cache-c7v93krb.js";
7
+ import {
8
+ FileModelsStore2
9
+ } from "./account-limits-cache-a55e14fr.js";
10
+ import {
11
+ createModels2
12
+ } from "./account-limits-cache-5jdhn0e1.js";
13
+ import {
14
+ anthropicProvider2
15
+ } from "./account-limits-cache-8wfqg73s.js";
16
+ import {
17
+ githubCopilotProvider2
18
+ } from "./account-limits-cache-a1jyaq00.js";
19
+ import {
20
+ openaiCodexProvider2
21
+ } from "./account-limits-cache-09vs98g6.js";
22
+ import {
23
+ openaiProvider2
24
+ } from "./account-limits-cache-gsa5tzck.js";
25
+ import {
26
+ opencodeProvider2
27
+ } from "./account-limits-cache-nbkyhs6v.js";
28
+ import {
29
+ opencodeGoProvider2
30
+ } from "./account-limits-cache-nyy770yd.js";
31
+ import {
32
+ openrouterProvider2
33
+ } from "./account-limits-cache-pfa86y8e.js";
34
+ import {
35
+ vercelAiGatewayProvider2
36
+ } from "./account-limits-cache-q63mc41j.js";
37
+
38
+ // src/providers/nyte-catalog.ts
39
+ var NYTE_PROVIDERS = [
40
+ openaiCodexProvider2,
41
+ openaiProvider2,
42
+ anthropicProvider2,
43
+ opencodeProvider2,
44
+ opencodeGoProvider2,
45
+ githubCopilotProvider2,
46
+ vercelAiGatewayProvider2,
47
+ openrouterProvider2
48
+ ];
49
+ var defaultModelPerProvider2 = {
50
+ "openai-codex": "gpt-5.6-luna",
51
+ openai: "gpt-5.6-luna",
52
+ anthropic: "claude-opus-5"
53
+ };
54
+ function createNyteModels2() {
55
+ const models = createModels2({
56
+ credentials: new FileCredentialStore2,
57
+ authContext: defaultProviderAuthContext2(),
58
+ modelsStore: new FileModelsStore2,
59
+ catalog: { url: process.env.NYTE_MODELS_URL }
60
+ });
61
+ for (const provider of nyteProviders2())
62
+ models.setProvider(provider);
63
+ return models;
64
+ }
65
+ function nyteProviders2() {
66
+ return NYTE_PROVIDERS.map((provider) => provider());
67
+ }
68
+
69
+ export { defaultModelPerProvider2, createNyteModels2, nyteProviders2 };
@@ -0,0 +1,13 @@
1
+ // src/utils/nyte-user-agent.ts
2
+ function loadNodeOs() {
3
+ if (typeof process === "undefined" || !(process.versions?.node || process.versions?.bun)) {
4
+ return null;
5
+ }
6
+ return process.getBuiltinModule?.("node:os") ?? null;
7
+ }
8
+ var nodeOs = loadNodeOs();
9
+ function getNyteUserAgent2() {
10
+ return nodeOs ? `nyte (${nodeOs.platform()} ${nodeOs.release()}; ${nodeOs.arch()})` : "nyte (browser)";
11
+ }
12
+
13
+ export { getNyteUserAgent2 };
@@ -0,0 +1,40 @@
1
+ // src/session-resources.ts
2
+ var sessionResourceCleanups = new Set;
3
+ var sessionResourceLeases = new Map;
4
+ function registerSessionResourceCleanup2(cleanup) {
5
+ sessionResourceCleanups.add(cleanup);
6
+ return () => {
7
+ sessionResourceCleanups.delete(cleanup);
8
+ };
9
+ }
10
+ function cleanupSessionResources2(sessionId) {
11
+ const errors = [];
12
+ for (const cleanup of sessionResourceCleanups) {
13
+ try {
14
+ cleanup(sessionId);
15
+ } catch (error) {
16
+ errors.push(error);
17
+ }
18
+ }
19
+ if (errors.length > 0) {
20
+ throw new AggregateError(errors, "Failed to cleanup session resources");
21
+ }
22
+ }
23
+ function acquireSessionResources2(sessionId) {
24
+ sessionResourceLeases.set(sessionId, (sessionResourceLeases.get(sessionId) ?? 0) + 1);
25
+ let released = false;
26
+ return () => {
27
+ if (released)
28
+ return;
29
+ released = true;
30
+ const remaining = (sessionResourceLeases.get(sessionId) ?? 1) - 1;
31
+ if (remaining > 0) {
32
+ sessionResourceLeases.set(sessionId, remaining);
33
+ return;
34
+ }
35
+ sessionResourceLeases.delete(sessionId);
36
+ cleanupSessionResources2(sessionId);
37
+ };
38
+ }
39
+
40
+ export { registerSessionResourceCleanup2, cleanupSessionResources2, acquireSessionResources2 };
File without changes
@@ -0,0 +1,32 @@
1
+ import {
2
+ GITHUB_COPILOT_DEFAULT_ORIGIN2
3
+ } from "./account-limits-cache-za42x8a3.js";
4
+
5
+ // src/providers/github-copilot-catalog.ts
6
+ import { Type } from "typebox";
7
+ import { Value } from "typebox/value";
8
+ var CatalogResponse = Type.Object({ data: Type.Array(Type.Unknown()) });
9
+ var CatalogModel = Type.Object({
10
+ id: Type.String({ minLength: 1 }),
11
+ model_picker_enabled: Type.Optional(Type.Boolean()),
12
+ policy: Type.Optional(Type.Object({ state: Type.Optional(Type.String()) })),
13
+ capabilities: Type.Optional(Type.Object({
14
+ supports: Type.Optional(Type.Object({ tool_calls: Type.Optional(Type.Boolean()) }))
15
+ }))
16
+ });
17
+ function parseGitHubCopilotCatalog2(value, origin, knownModelIds) {
18
+ if (!Value.Check(CatalogResponse, value)) {
19
+ throw new Error("GitHub Copilot catalog response is not a model list");
20
+ }
21
+ const models = value.data.filter((entry) => Value.Check(CatalogModel, entry)).filter((model) => model.capabilities?.supports?.tool_calls !== false && model.policy?.state !== "disabled");
22
+ const individualFallback = origin === GITHUB_COPILOT_DEFAULT_ORIGIN2 && !models.some((model) => model.model_picker_enabled);
23
+ const visible = models.filter((model) => individualFallback || model.model_picker_enabled);
24
+ return {
25
+ availableModelIds: [
26
+ ...new Set(visible.filter((model) => individualFallback ? model.policy?.state === "enabled" : model.policy?.state !== "unconfigured").map((model) => model.id))
27
+ ],
28
+ needsApproval: visible.some((model) => model.policy?.state === "unconfigured" && knownModelIds.has(model.id))
29
+ };
30
+ }
31
+
32
+ export { parseGitHubCopilotCatalog2 };
@@ -0,0 +1,37 @@
1
+ import {
2
+ openAIResponsesApi2
3
+ } from "./account-limits-cache-q1ebtvet.js";
4
+ import {
5
+ envApiKeyAuth2
6
+ } from "./account-limits-cache-6x05n06z.js";
7
+ import {
8
+ verifyWithRequest2
9
+ } from "./account-limits-cache-ydq8p8qp.js";
10
+ import {
11
+ createProvider2
12
+ } from "./account-limits-cache-ykbqsvv6.js";
13
+
14
+ // src/providers/openai.ts
15
+ var OPENAI_BASE_URL = "https://api.openai.com/v1";
16
+ function openaiProvider2() {
17
+ return createProvider2({
18
+ id: "openai",
19
+ name: "OpenAI",
20
+ baseUrl: OPENAI_BASE_URL,
21
+ promptCache: {
22
+ minimumRetentionMs: { short: 5 * 60000, long: 24 * 60 * 60000 }
23
+ },
24
+ auth: {
25
+ apiKey: envApiKeyAuth2("OpenAI API key", ["OPENAI_API_KEY"]),
26
+ verify: ({ auth, signal }) => verifyWithRequest2({
27
+ provider: "OpenAI",
28
+ url: `${auth.baseUrl ?? OPENAI_BASE_URL}/models`,
29
+ headers: { ...auth.headers, Authorization: `Bearer ${auth.apiKey ?? ""}` },
30
+ signal
31
+ })
32
+ },
33
+ api: { "openai-responses": openAIResponsesApi2() }
34
+ });
35
+ }
36
+
37
+ export { openaiProvider2 };
@@ -0,0 +1,235 @@
1
+ // src/api/constrained-sampling.ts
2
+ import { Type } from "typebox";
3
+ import {
4
+ IsAdditionalProperties,
5
+ IsAnyOf,
6
+ IsConst,
7
+ IsEnum,
8
+ IsItems,
9
+ IsProperties,
10
+ IsRequired,
11
+ IsSchemaObject,
12
+ IsType
13
+ } from "typebox/schema";
14
+ import { Value } from "typebox/value";
15
+
16
+ class UnsupportedStrictJsonSchemaError extends Error {
17
+ }
18
+ var UNSUPPORTED_STRICT_SCHEMA_KEYS = [
19
+ "$ref",
20
+ "$defs",
21
+ "definitions",
22
+ "allOf",
23
+ "oneOf",
24
+ "patternProperties",
25
+ "dependentSchemas",
26
+ "dependencies",
27
+ "unevaluatedProperties",
28
+ "propertyNames",
29
+ "contains",
30
+ "prefixItems",
31
+ "not",
32
+ "if",
33
+ "then",
34
+ "else"
35
+ ];
36
+ function schemaTypes(schema) {
37
+ return IsType(schema) ? [schema.type].flat() : [];
38
+ }
39
+ function describesObjectOrArray(schema) {
40
+ if (!IsSchemaObject(schema))
41
+ return false;
42
+ const types = schemaTypes(schema);
43
+ return types.includes("object") || types.includes("array") || "properties" in schema || "items" in schema;
44
+ }
45
+ function schemaAllowsNull(schema) {
46
+ if (!IsSchemaObject(schema))
47
+ return false;
48
+ if (schemaTypes(schema).includes("null"))
49
+ return true;
50
+ if (IsConst(schema) && schema.const === null || IsEnum(schema) && schema.enum.includes(null))
51
+ return true;
52
+ return IsAnyOf(schema) && schema.anyOf.some((variant) => schemaAllowsNull(variant));
53
+ }
54
+ function makeJsonSchemaNodeStrict(schema, isUnsupportedKeyword) {
55
+ if (!IsSchemaObject(schema)) {
56
+ throw new UnsupportedStrictJsonSchemaError("boolean schemas are unsupported");
57
+ }
58
+ for (const key of UNSUPPORTED_STRICT_SCHEMA_KEYS) {
59
+ if (key in schema) {
60
+ throw new UnsupportedStrictJsonSchemaError(`${key} schemas are unsupported`);
61
+ }
62
+ }
63
+ if (isUnsupportedKeyword) {
64
+ for (const [key, value] of Object.entries(schema)) {
65
+ if (isUnsupportedKeyword(key, value)) {
66
+ throw new UnsupportedStrictJsonSchemaError(`${key}: ${JSON.stringify(value)} is unsupported`);
67
+ }
68
+ }
69
+ }
70
+ if ("anyOf" in schema) {
71
+ if (!IsAnyOf(schema) || schema.anyOf.length === 0) {
72
+ throw new UnsupportedStrictJsonSchemaError("anyOf must contain at least one schema");
73
+ }
74
+ for (const variant of schema.anyOf) {
75
+ if (describesObjectOrArray(variant)) {
76
+ throw new UnsupportedStrictJsonSchemaError("object and array unions are unsupported");
77
+ }
78
+ makeJsonSchemaNodeStrict(variant, isUnsupportedKeyword);
79
+ }
80
+ }
81
+ if ("items" in schema) {
82
+ if (!IsItems(schema)) {
83
+ throw new UnsupportedStrictJsonSchemaError("items must be a schema");
84
+ }
85
+ if (Array.isArray(schema.items)) {
86
+ throw new UnsupportedStrictJsonSchemaError("tuple schemas are unsupported");
87
+ }
88
+ makeJsonSchemaNodeStrict(schema.items, isUnsupportedKeyword);
89
+ }
90
+ const isObjectSchema = IsType(schema) && schema.type === "object";
91
+ if ("properties" in schema && !isObjectSchema) {
92
+ throw new UnsupportedStrictJsonSchemaError("properties require type object");
93
+ }
94
+ if (!isObjectSchema)
95
+ return;
96
+ if ("additionalProperties" in schema && !(IsAdditionalProperties(schema) && schema.additionalProperties === false)) {
97
+ throw new UnsupportedStrictJsonSchemaError("schema-valued or true additionalProperties is unsupported");
98
+ }
99
+ if ("properties" in schema && !IsProperties(schema)) {
100
+ throw new UnsupportedStrictJsonSchemaError("object properties must be a schema map");
101
+ }
102
+ if ("required" in schema && !IsRequired(schema)) {
103
+ throw new UnsupportedStrictJsonSchemaError("object required must be a string array");
104
+ }
105
+ const properties = IsProperties(schema) ? schema.properties : {};
106
+ const propertyNames = Object.keys(properties);
107
+ const required = new Set(IsRequired(schema) ? schema.required : []);
108
+ if ([...required].some((key) => !propertyNames.includes(key))) {
109
+ throw new UnsupportedStrictJsonSchemaError("required contains an unknown property");
110
+ }
111
+ for (const [key, property] of Object.entries(properties)) {
112
+ makeJsonSchemaNodeStrict(property, isUnsupportedKeyword);
113
+ if (!required.has(key) && !schemaAllowsNull(property)) {
114
+ properties[key] = { anyOf: [property, { type: "null" }] };
115
+ }
116
+ }
117
+ Object.assign(schema, { required: propertyNames, additionalProperties: false });
118
+ }
119
+ function makeStrictJsonSchema(schema, isUnsupportedKeyword) {
120
+ const cloned = structuredClone(schema);
121
+ if (!IsSchemaObject(cloned)) {
122
+ throw new UnsupportedStrictJsonSchemaError("root schema must have type object");
123
+ }
124
+ makeJsonSchemaNodeStrict(cloned, isUnsupportedKeyword);
125
+ if (!IsType(cloned) || cloned.type !== "object") {
126
+ throw new UnsupportedStrictJsonSchemaError("root schema must have type object");
127
+ }
128
+ return cloned;
129
+ }
130
+ function getJsonSchemaToolParameters2(tool, strict) {
131
+ return { ...strict === true ? makeStrictJsonSchema(tool.parameters) : tool.parameters };
132
+ }
133
+ function getGrammarToolInput2(toolName, arguments_, inputProperty) {
134
+ if (!Value.Check(Type.Object({ [inputProperty]: Type.String() }), arguments_)) {
135
+ throw new Error(`Grammar tool call "${toolName}" requires argument "${inputProperty}" to be a string.`);
136
+ }
137
+ return arguments_[inputProperty];
138
+ }
139
+ function appendGrammarToolInputJsonDelta2(buffer, inputProperty, nextInput, close) {
140
+ if (buffer.closed) {
141
+ if (close && nextInput === buffer.input)
142
+ return;
143
+ throw new Error(`grammar tool input for property "${inputProperty}" changed after it was closed`);
144
+ }
145
+ if (!nextInput.startsWith(buffer.input)) {
146
+ throw new Error(`grammar tool input for property "${inputProperty}" changed non-monotonically`);
147
+ }
148
+ const inputDelta = nextInput.slice(buffer.input.length);
149
+ if (!close && inputDelta.length === 0)
150
+ return;
151
+ let delta = "";
152
+ if (!buffer.started) {
153
+ delta += `{${JSON.stringify(inputProperty)}:"`;
154
+ buffer.started = true;
155
+ }
156
+ delta += JSON.stringify(inputDelta).slice(1, -1);
157
+ buffer.input = nextInput;
158
+ if (close) {
159
+ delta += '"}';
160
+ buffer.closed = true;
161
+ }
162
+ return delta;
163
+ }
164
+ function inferGrammarInputProperty(tool) {
165
+ const schema = tool.parameters;
166
+ if (!IsSchemaObject(schema) || !IsType(schema) || schema.type !== "object") {
167
+ throw new Error("grammar constrained sampling requires an object parameter schema");
168
+ }
169
+ if (!IsRequired(schema) || schema.required.length !== 1) {
170
+ throw new Error("grammar constrained sampling requires exactly one required string property");
171
+ }
172
+ const [inputProperty] = schema.required;
173
+ const property = IsProperties(schema) ? schema.properties[inputProperty] : undefined;
174
+ if (!property) {
175
+ throw new Error(`grammar constrained sampling requires a properties entry for ${inputProperty}`);
176
+ }
177
+ if (!IsSchemaObject(property) || !IsType(property) || property.type !== "string") {
178
+ throw new Error(`grammar constrained sampling property ${inputProperty} must have type string`);
179
+ }
180
+ return inputProperty;
181
+ }
182
+ function resolveJsonSchemaStrictSampling2(tool, supportsStrictMode, isUnsupportedKeyword) {
183
+ const config = tool.constrainedSampling;
184
+ if (!config || config.type !== "json_schema")
185
+ return;
186
+ if (supportsStrictMode) {
187
+ try {
188
+ makeStrictJsonSchema(tool.parameters, isUnsupportedKeyword);
189
+ return true;
190
+ } catch (error) {
191
+ if (!(error instanceof UnsupportedStrictJsonSchemaError))
192
+ throw error;
193
+ if (config.strict !== "require")
194
+ return;
195
+ throw new Error(`Tool "${tool.name}" requires JSON-schema constrained sampling, but ${error.message}.`);
196
+ }
197
+ }
198
+ if (config.strict === "require") {
199
+ throw new Error(`Tool "${tool.name}" requires JSON-schema constrained sampling, but strict tools are unsupported.`);
200
+ }
201
+ return;
202
+ }
203
+ function resolveGrammarConstrainedSampling2(tool, supportsOpenAIGrammarTools) {
204
+ const config = tool.constrainedSampling;
205
+ if (!config || config.type !== "grammar") {
206
+ return;
207
+ }
208
+ if (!supportsOpenAIGrammarTools) {
209
+ return;
210
+ }
211
+ const larkDefinition = config.variants.openai_lark;
212
+ const regexDefinition = config.variants.openai_regex;
213
+ const variant = larkDefinition?.trim() ? { format: "lark", definition: larkDefinition } : regexDefinition?.trim() ? { format: "regex", definition: regexDefinition } : undefined;
214
+ if (!variant) {
215
+ throw new Error(`Tool "${tool.name}" cannot use grammar constrained sampling: no supported grammar variant was provided.`);
216
+ }
217
+ try {
218
+ return { ...variant, inputProperty: inferGrammarInputProperty(tool) };
219
+ } catch (error) {
220
+ const message = error instanceof Error ? error.message : String(error);
221
+ throw new Error(`Tool "${tool.name}" cannot use grammar constrained sampling: ${message}.`);
222
+ }
223
+ }
224
+ function createGrammarToolInputProperties2(tools, supportsOpenAIGrammarTools) {
225
+ const properties = new Map;
226
+ for (const tool of tools ?? []) {
227
+ const grammar = resolveGrammarConstrainedSampling2(tool, supportsOpenAIGrammarTools);
228
+ if (grammar) {
229
+ properties.set(tool.name, grammar.inputProperty);
230
+ }
231
+ }
232
+ return properties;
233
+ }
234
+
235
+ export { getJsonSchemaToolParameters2, getGrammarToolInput2, appendGrammarToolInputJsonDelta2, resolveJsonSchemaStrictSampling2, resolveGrammarConstrainedSampling2, createGrammarToolInputProperties2 };
@@ -0,0 +1,142 @@
1
+ import {
2
+ operationSignal2,
3
+ raceWithAbortSignal2
4
+ } from "./account-limits-cache-j2bt392s.js";
5
+
6
+ // src/account-limits-cache.ts
7
+ var FRESHNESS_MS = 60000;
8
+ var RETRY_MS = 15000;
9
+ var TIMEOUT_MS = 15000;
10
+ function mergeLimits(entry, next, requestRevision = entry.revision) {
11
+ const previous = entry.limits;
12
+ const windows = new Map(previous?.windows.map((window) => [window.id, window]));
13
+ entry.revision += 1;
14
+ for (const window of next.windows) {
15
+ const observed = entry.observed.get(window.id);
16
+ if (observed !== undefined && (observed.at > next.observedAt || observed.revision > requestRevision))
17
+ continue;
18
+ windows.set(window.id, window);
19
+ entry.observed.set(window.id, { at: next.observedAt, revision: entry.revision });
20
+ }
21
+ entry.limits = {
22
+ ...previous,
23
+ ...next,
24
+ windows: [...windows.values()],
25
+ observedAt: Math.max(previous?.observedAt ?? 0, next.observedAt)
26
+ };
27
+ }
28
+ function snapshot(entry) {
29
+ if (entry.limits === undefined)
30
+ return entry.unavailable ?? { kind: "unavailable", reason: "failed", message: "No usage available." };
31
+ return {
32
+ kind: "ready",
33
+ limits: entry.limits,
34
+ stale: entry.fetchedAt === null || Date.now() - entry.fetchedAt >= FRESHNESS_MS,
35
+ error: entry.error
36
+ };
37
+ }
38
+ function changedAccount() {
39
+ return { kind: "unavailable", reason: "failed", message: "The active account changed." };
40
+ }
41
+
42
+ class AccountLimitsCache2 {
43
+ providers = new Map;
44
+ get(providerId, accountId) {
45
+ const provider = this.providers.get(providerId);
46
+ return provider?.accounts.get(accountId === undefined ? provider.lastAccount : accountId)?.limits;
47
+ }
48
+ invalidate(providerId) {
49
+ const provider = this.providers.get(providerId);
50
+ if (provider === undefined)
51
+ return;
52
+ provider.generation += 1;
53
+ for (const entry of provider.accounts.values())
54
+ entry.flight?.abort.abort();
55
+ provider.accounts.clear();
56
+ }
57
+ scope(providerId) {
58
+ let provider = this.providers.get(providerId);
59
+ if (provider === undefined) {
60
+ provider = { generation: 0, lastAccount: null, accounts: new Map };
61
+ this.providers.set(providerId, provider);
62
+ }
63
+ const state = provider;
64
+ const generation = state.generation;
65
+ const accountEntry = (accountId) => {
66
+ const key = accountId ?? null;
67
+ state.lastAccount = key;
68
+ const existing = state.accounts.get(key);
69
+ if (existing !== undefined)
70
+ return existing;
71
+ const entry = {
72
+ limits: undefined,
73
+ revision: 0,
74
+ observed: new Map,
75
+ fetchedAt: null,
76
+ retryAt: 0,
77
+ error: null,
78
+ unavailable: null,
79
+ flight: null
80
+ };
81
+ state.accounts.set(key, entry);
82
+ return entry;
83
+ };
84
+ return {
85
+ observe(accountId, limits) {
86
+ if (state.generation !== generation)
87
+ return;
88
+ const entry = accountEntry(accountId);
89
+ mergeLimits(entry, limits);
90
+ },
91
+ async fetch(accountId, load, options) {
92
+ options?.signal?.throwIfAborted();
93
+ if (state.generation !== generation)
94
+ return changedAccount();
95
+ const entry = accountEntry(accountId);
96
+ if (entry.flight !== null)
97
+ return raceWithAbortSignal2(entry.flight.result, operationSignal2(options?.signal));
98
+ if (Date.now() < entry.retryAt)
99
+ return snapshot(entry);
100
+ const abort = new AbortController;
101
+ const signal = AbortSignal.any([abort.signal, AbortSignal.timeout(TIMEOUT_MS)]);
102
+ const requestRevision = entry.revision;
103
+ const result = (async () => {
104
+ try {
105
+ const limits = await raceWithAbortSignal2(Promise.resolve().then(() => load(signal)), signal);
106
+ if (state.generation !== generation)
107
+ return changedAccount();
108
+ if (limits === undefined) {
109
+ entry.unavailable = {
110
+ kind: "unavailable",
111
+ reason: "unsupported",
112
+ message: "This account does not provide subscription usage."
113
+ };
114
+ entry.retryAt = Date.now() + RETRY_MS;
115
+ return snapshot(entry);
116
+ }
117
+ mergeLimits(entry, limits, requestRevision);
118
+ entry.fetchedAt = Date.now();
119
+ entry.retryAt = entry.fetchedAt + FRESHNESS_MS;
120
+ entry.error = null;
121
+ entry.unavailable = null;
122
+ return snapshot(entry);
123
+ } catch (error) {
124
+ if (state.generation !== generation)
125
+ return changedAccount();
126
+ const message = error instanceof Error ? error.message : String(error);
127
+ entry.error = message;
128
+ entry.unavailable = { kind: "unavailable", reason: "failed", message };
129
+ entry.retryAt = Date.now() + RETRY_MS;
130
+ return snapshot(entry);
131
+ } finally {
132
+ entry.flight = null;
133
+ }
134
+ })();
135
+ entry.flight = { result, abort };
136
+ return raceWithAbortSignal2(result, operationSignal2(options?.signal));
137
+ }
138
+ };
139
+ }
140
+ }
141
+
142
+ export { AccountLimitsCache2 };