@the-open-engine/zeroshot 6.29.0 → 6.30.0

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 (347) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/cli/index.js +2 -4
  3. package/lib/agent-cli-provider/adapters/codex.d.ts.map +1 -1
  4. package/lib/agent-cli-provider/adapters/codex.js +0 -1
  5. package/lib/agent-cli-provider/adapters/codex.js.map +1 -1
  6. package/lib/agent-cli-provider/contract-actions.d.ts +1 -1
  7. package/lib/agent-cli-provider/contract-actions.d.ts.map +1 -1
  8. package/lib/agent-cli-provider/contract-actions.js +5 -5
  9. package/lib/agent-cli-provider/contract-actions.js.map +1 -1
  10. package/lib/agent-cli-provider/contract-invoke.d.ts +1 -1
  11. package/lib/agent-cli-provider/contract-invoke.d.ts.map +1 -1
  12. package/lib/agent-cli-provider/contract-invoke.js +2 -2
  13. package/lib/agent-cli-provider/contract-invoke.js.map +1 -1
  14. package/lib/agent-cli-provider/contract-support.d.ts +1 -1
  15. package/lib/agent-cli-provider/contract-support.d.ts.map +1 -1
  16. package/lib/agent-cli-provider/contract-support.js +2 -2
  17. package/lib/agent-cli-provider/contract-support.js.map +1 -1
  18. package/lib/agent-cli-provider/contract.d.ts +1 -0
  19. package/lib/agent-cli-provider/contract.d.ts.map +1 -1
  20. package/lib/agent-cli-provider/contract.js +1 -1
  21. package/lib/agent-cli-provider/contract.js.map +1 -1
  22. package/lib/agent-cli-provider/index.d.ts +1 -1
  23. package/lib/agent-cli-provider/index.d.ts.map +1 -1
  24. package/lib/agent-cli-provider/index.js +2 -1
  25. package/lib/agent-cli-provider/index.js.map +1 -1
  26. package/lib/agent-cli-provider/provider-registry.d.ts +1 -0
  27. package/lib/agent-cli-provider/provider-registry.d.ts.map +1 -1
  28. package/lib/agent-cli-provider/provider-registry.js +4 -0
  29. package/lib/agent-cli-provider/provider-registry.js.map +1 -1
  30. package/lib/agent-cli-provider/single-agent-runtime.d.ts +3 -3
  31. package/lib/agent-cli-provider/single-agent-runtime.d.ts.map +1 -1
  32. package/lib/agent-cli-provider/single-agent-runtime.js +22 -48
  33. package/lib/agent-cli-provider/single-agent-runtime.js.map +1 -1
  34. package/lib/cluster/client.cjs +30 -7
  35. package/lib/cluster/client.d.cts +52 -0
  36. package/lib/cluster/client.d.mts +52 -0
  37. package/lib/cluster/client.d.ts +4 -6
  38. package/lib/cluster/client.mjs +32 -9
  39. package/lib/cluster/connection-state.cjs +25 -0
  40. package/lib/cluster/connection-state.d.cts +13 -0
  41. package/lib/cluster/connection-state.d.mts +13 -0
  42. package/lib/cluster/connection-state.d.ts +13 -0
  43. package/lib/cluster/connection-state.mjs +21 -0
  44. package/lib/{target/hosted-run/contracts.js → cluster/connection-types.cjs} +0 -1
  45. package/lib/cluster/connection-types.d.cts +33 -0
  46. package/lib/cluster/connection-types.d.mts +33 -0
  47. package/lib/cluster/connection-types.d.ts +33 -0
  48. package/lib/cluster/connection-types.mjs +1 -0
  49. package/lib/cluster/connection.cjs +18 -46
  50. package/lib/cluster/connection.d.cts +26 -0
  51. package/lib/cluster/connection.d.mts +26 -0
  52. package/lib/cluster/connection.d.ts +7 -25
  53. package/lib/cluster/connection.mjs +12 -42
  54. package/lib/cluster/errors.cjs +9 -1
  55. package/lib/cluster/errors.d.cts +29 -0
  56. package/lib/cluster/errors.d.mts +29 -0
  57. package/lib/cluster/errors.d.ts +4 -0
  58. package/lib/cluster/errors.mjs +7 -0
  59. package/lib/cluster/frames.d.cts +12 -0
  60. package/lib/cluster/frames.d.mts +12 -0
  61. package/lib/cluster/generated/protocol-schema.d.cts +1 -0
  62. package/lib/cluster/generated/protocol-schema.d.mts +1 -0
  63. package/lib/cluster/generated/protocol.d.cts +781 -0
  64. package/lib/cluster/generated/protocol.d.mts +781 -0
  65. package/lib/cluster/index.cjs +2 -2
  66. package/lib/cluster/index.d.cts +13 -0
  67. package/lib/cluster/index.d.mts +13 -0
  68. package/lib/cluster/index.d.ts +3 -3
  69. package/lib/cluster/index.mjs +2 -2
  70. package/lib/cluster/json-source.cjs +1 -0
  71. package/lib/cluster/json-source.d.cts +5 -0
  72. package/lib/cluster/json-source.d.mts +5 -0
  73. package/lib/cluster/json-source.d.ts +1 -0
  74. package/lib/cluster/json-source.mjs +1 -0
  75. package/lib/cluster/payload-value.cjs +1 -0
  76. package/lib/cluster/payload-value.d.cts +8 -0
  77. package/lib/cluster/payload-value.d.mts +8 -0
  78. package/lib/cluster/payload-value.d.ts +1 -0
  79. package/lib/cluster/payload-value.mjs +1 -0
  80. package/lib/cluster/queue.d.cts +15 -0
  81. package/lib/cluster/queue.d.mts +15 -0
  82. package/lib/cluster/socket.cjs +8 -0
  83. package/lib/cluster/socket.d.cts +13 -0
  84. package/lib/cluster/socket.d.mts +13 -0
  85. package/lib/cluster/socket.d.ts +2 -0
  86. package/lib/cluster/socket.mjs +7 -0
  87. package/lib/cluster/subscriptions.d.cts +73 -0
  88. package/lib/cluster/subscriptions.d.mts +73 -0
  89. package/lib/cluster/subscriptions.d.ts +5 -1
  90. package/lib/cluster/validators.cjs +1 -0
  91. package/lib/cluster/validators.d.cts +7 -0
  92. package/lib/cluster/validators.d.mts +7 -0
  93. package/lib/cluster/validators.d.ts +1 -0
  94. package/lib/cluster/validators.mjs +1 -0
  95. package/lib/cluster-worker/engine-adapter-common.js +37 -0
  96. package/lib/cluster-worker/engine-adapter.js +24 -30
  97. package/lib/cluster-worker/engine-start.js +59 -0
  98. package/lib/cluster-worker/index.js +49 -67
  99. package/lib/cluster-worker/profiles.js +1 -42
  100. package/lib/cluster-worker/runtime-dependencies.js +49 -0
  101. package/lib/cluster-worker/runtime-support.js +2 -34
  102. package/lib/cluster-worker/terminal-normalizer.js +9 -10
  103. package/lib/hosted-session/authority.cjs +61 -0
  104. package/lib/hosted-session/authority.d.cts +3 -0
  105. package/lib/hosted-session/authority.d.mts +3 -0
  106. package/lib/hosted-session/authority.d.ts +3 -0
  107. package/lib/hosted-session/authority.mjs +57 -0
  108. package/lib/hosted-session/coordinator.cjs +78 -51
  109. package/lib/hosted-session/coordinator.d.cts +11 -0
  110. package/lib/hosted-session/coordinator.d.mts +11 -0
  111. package/lib/hosted-session/coordinator.d.ts +4 -2
  112. package/lib/hosted-session/coordinator.mjs +75 -51
  113. package/lib/hosted-session/errors.cjs +25 -0
  114. package/lib/hosted-session/errors.d.cts +10 -0
  115. package/lib/hosted-session/errors.d.mts +10 -0
  116. package/lib/hosted-session/errors.d.ts +10 -0
  117. package/lib/hosted-session/errors.mjs +19 -0
  118. package/lib/hosted-session/index.cjs +4 -1
  119. package/lib/hosted-session/index.d.cts +2 -0
  120. package/lib/hosted-session/index.d.mts +2 -0
  121. package/lib/hosted-session/index.d.ts +2 -2
  122. package/lib/hosted-session/index.mjs +1 -1
  123. package/lib/hosted-session/reconnecting-watch.cjs +131 -0
  124. package/lib/hosted-session/reconnecting-watch.d.cts +13 -0
  125. package/lib/hosted-session/reconnecting-watch.d.mts +13 -0
  126. package/lib/hosted-session/reconnecting-watch.d.ts +13 -0
  127. package/lib/hosted-session/reconnecting-watch.mjs +127 -0
  128. package/lib/hosted-session/types.d.cts +32 -0
  129. package/lib/hosted-session/types.d.mts +32 -0
  130. package/lib/hosted-session/types.d.ts +19 -7
  131. package/lib/hosted-target/access-url.cjs +44 -0
  132. package/lib/hosted-target/access-url.d.cts +2 -0
  133. package/lib/hosted-target/access-url.d.mts +2 -0
  134. package/lib/hosted-target/access-url.d.ts +2 -0
  135. package/lib/hosted-target/access-url.mjs +41 -0
  136. package/lib/hosted-target/adapter-request.cjs +12 -0
  137. package/lib/hosted-target/adapter-request.d.cts +17 -0
  138. package/lib/hosted-target/adapter-request.d.mts +17 -0
  139. package/lib/hosted-target/adapter-request.d.ts +17 -0
  140. package/lib/hosted-target/adapter-request.mjs +9 -0
  141. package/lib/hosted-target/adapter-types.cjs +2 -0
  142. package/lib/hosted-target/adapter-types.d.cts +27 -0
  143. package/lib/hosted-target/adapter-types.d.mts +27 -0
  144. package/lib/hosted-target/adapter-types.d.ts +27 -0
  145. package/lib/hosted-target/adapter-types.mjs +1 -0
  146. package/lib/hosted-target/bounds.cjs +10 -0
  147. package/lib/hosted-target/bounds.d.cts +7 -0
  148. package/lib/hosted-target/bounds.d.mts +7 -0
  149. package/lib/hosted-target/bounds.d.ts +7 -0
  150. package/lib/hosted-target/bounds.mjs +7 -0
  151. package/lib/hosted-target/capsule-error-response.cjs +76 -0
  152. package/lib/hosted-target/capsule-error-response.d.cts +2 -0
  153. package/lib/hosted-target/capsule-error-response.d.mts +2 -0
  154. package/lib/hosted-target/capsule-error-response.d.ts +2 -0
  155. package/lib/hosted-target/capsule-error-response.mjs +73 -0
  156. package/lib/hosted-target/errors.cjs +88 -0
  157. package/lib/hosted-target/errors.d.cts +36 -0
  158. package/lib/hosted-target/errors.d.mts +36 -0
  159. package/lib/hosted-target/errors.d.ts +36 -0
  160. package/lib/hosted-target/errors.mjs +75 -0
  161. package/lib/hosted-target/index.cjs +16 -0
  162. package/lib/hosted-target/index.d.cts +3 -0
  163. package/lib/hosted-target/index.d.mts +3 -0
  164. package/lib/hosted-target/index.d.ts +3 -0
  165. package/lib/hosted-target/index.mjs +2 -0
  166. package/lib/hosted-target/response-validation.cjs +169 -0
  167. package/lib/hosted-target/response-validation.d.cts +5 -0
  168. package/lib/hosted-target/response-validation.d.mts +5 -0
  169. package/lib/hosted-target/response-validation.d.ts +5 -0
  170. package/lib/hosted-target/response-validation.mjs +163 -0
  171. package/lib/hosted-target/retry-executor.cjs +51 -0
  172. package/lib/hosted-target/retry-executor.d.cts +8 -0
  173. package/lib/hosted-target/retry-executor.d.mts +8 -0
  174. package/lib/hosted-target/retry-executor.d.ts +8 -0
  175. package/lib/hosted-target/retry-executor.mjs +48 -0
  176. package/lib/hosted-target/retry.cjs +78 -0
  177. package/lib/hosted-target/retry.d.cts +9 -0
  178. package/lib/hosted-target/retry.d.mts +9 -0
  179. package/lib/hosted-target/retry.d.ts +9 -0
  180. package/lib/hosted-target/retry.mjs +73 -0
  181. package/lib/hosted-target/target-adapter.cjs +10 -0
  182. package/lib/hosted-target/target-adapter.d.cts +3 -0
  183. package/lib/hosted-target/target-adapter.d.mts +3 -0
  184. package/lib/hosted-target/target-adapter.d.ts +3 -0
  185. package/lib/hosted-target/target-adapter.mjs +7 -0
  186. package/lib/hosted-target/types.cjs +4 -0
  187. package/lib/hosted-target/types.d.cts +50 -0
  188. package/lib/hosted-target/types.d.mts +50 -0
  189. package/lib/hosted-target/types.d.ts +50 -0
  190. package/lib/hosted-target/types.mjs +1 -0
  191. package/lib/hosted-target/zero-cloud-v1-adapter.cjs +185 -0
  192. package/lib/hosted-target/zero-cloud-v1-adapter.d.cts +13 -0
  193. package/lib/hosted-target/zero-cloud-v1-adapter.d.mts +13 -0
  194. package/lib/hosted-target/zero-cloud-v1-adapter.d.ts +13 -0
  195. package/lib/hosted-target/zero-cloud-v1-adapter.mjs +181 -0
  196. package/lib/start-cluster.js +3 -3
  197. package/lib/target/bounded-response.cjs +51 -0
  198. package/lib/target/bounded-response.d.cts +1 -0
  199. package/lib/target/bounded-response.d.mts +1 -0
  200. package/lib/target/bounded-response.d.ts +1 -0
  201. package/lib/target/bounded-response.js +51 -0
  202. package/lib/target/bounded-response.mjs +48 -0
  203. package/lib/target/credential-lock.js +1 -3
  204. package/lib/target/credential-store.js +9 -4
  205. package/lib/target/device-flow.cjs +213 -0
  206. package/lib/target/device-flow.d.cts +54 -0
  207. package/lib/target/device-flow.d.mts +54 -0
  208. package/lib/target/device-flow.d.ts +26 -14
  209. package/lib/target/device-flow.js +162 -58
  210. package/lib/target/device-flow.mjs +203 -0
  211. package/lib/target/discovery-errors.cjs +10 -0
  212. package/lib/target/discovery-errors.d.cts +3 -0
  213. package/lib/target/discovery-errors.d.mts +3 -0
  214. package/lib/target/discovery-errors.d.ts +3 -0
  215. package/lib/target/discovery-errors.js +10 -0
  216. package/lib/target/discovery-errors.mjs +6 -0
  217. package/lib/target/discovery-sections.cjs +191 -0
  218. package/lib/target/discovery-sections.d.cts +52 -0
  219. package/lib/target/discovery-sections.d.mts +52 -0
  220. package/lib/target/discovery-sections.d.ts +52 -0
  221. package/lib/target/discovery-sections.js +191 -0
  222. package/lib/target/discovery-sections.mjs +179 -0
  223. package/lib/target/discovery-validation.cjs +109 -0
  224. package/lib/target/discovery-validation.d.cts +28 -0
  225. package/lib/target/discovery-validation.d.mts +28 -0
  226. package/lib/target/discovery-validation.d.ts +28 -0
  227. package/lib/target/discovery-validation.js +109 -0
  228. package/lib/target/discovery-validation.mjs +99 -0
  229. package/lib/target/discovery.cjs +112 -0
  230. package/lib/target/discovery.d.cts +74 -0
  231. package/lib/target/discovery.d.mts +74 -0
  232. package/lib/target/discovery.d.ts +67 -5
  233. package/lib/target/discovery.js +94 -79
  234. package/lib/target/discovery.mjs +105 -0
  235. package/lib/target/index.d.ts +6 -6
  236. package/lib/target/index.js +36 -36
  237. package/lib/target/oauth-http.d.ts +2 -0
  238. package/lib/target/oauth-http.js +25 -0
  239. package/lib/target/refresh-exchange.d.ts +11 -0
  240. package/lib/target/refresh-exchange.js +24 -0
  241. package/lib/target/refresh-revocation.d.ts +8 -0
  242. package/lib/target/refresh-revocation.js +19 -0
  243. package/lib/target/route-template.cjs +85 -0
  244. package/lib/target/route-template.d.cts +7 -0
  245. package/lib/target/route-template.d.mts +7 -0
  246. package/lib/target/route-template.d.ts +7 -0
  247. package/lib/target/route-template.js +85 -0
  248. package/lib/target/route-template.mjs +81 -0
  249. package/lib/target/session-errors.d.ts +4 -0
  250. package/lib/target/session-errors.js +12 -0
  251. package/lib/target/session-verification.d.ts +10 -0
  252. package/lib/target/session-verification.js +42 -0
  253. package/lib/target/target-registry.d.ts +7 -3
  254. package/lib/target/target-registry.js +41 -10
  255. package/lib/target/target-session-manager.d.ts +48 -0
  256. package/lib/target/target-session-manager.js +226 -0
  257. package/lib/target/target-session.d.ts +39 -32
  258. package/lib/target/target-session.js +63 -151
  259. package/lib/target/token-response.cjs +51 -0
  260. package/lib/target/token-response.d.cts +7 -0
  261. package/lib/target/token-response.d.mts +7 -0
  262. package/lib/target/token-response.d.ts +7 -0
  263. package/lib/target/token-response.js +51 -0
  264. package/lib/target/token-response.mjs +48 -0
  265. package/package.json +14 -7
  266. package/protocol/openengine-cluster/v1/worker.schema.json +171 -9
  267. package/scripts/build-cluster.js +23 -3
  268. package/scripts/opcore-introduced-check.js +0 -2
  269. package/src/agent-cli-provider/adapters/codex.ts +0 -1
  270. package/src/agent-cli-provider/contract-actions.ts +9 -5
  271. package/src/agent-cli-provider/contract-invoke.ts +3 -2
  272. package/src/agent-cli-provider/contract-support.ts +12 -6
  273. package/src/agent-cli-provider/contract.ts +6 -1
  274. package/src/agent-cli-provider/index.ts +1 -0
  275. package/src/agent-cli-provider/provider-registry.ts +4 -0
  276. package/src/agent-cli-provider/single-agent-runtime.ts +32 -45
  277. package/src/cluster/client.ts +42 -15
  278. package/src/cluster/connection-state.ts +33 -0
  279. package/src/cluster/connection-types.ts +28 -0
  280. package/src/cluster/connection.ts +37 -68
  281. package/src/cluster/errors.ts +5 -0
  282. package/src/cluster/index.ts +3 -1
  283. package/src/cluster/json-source.ts +1 -0
  284. package/src/cluster/payload-value.ts +1 -0
  285. package/src/cluster/socket.ts +13 -0
  286. package/src/cluster/subscriptions.ts +7 -2
  287. package/src/cluster/validators.ts +1 -0
  288. package/src/cluster/ws.d.ts +1 -1
  289. package/src/hosted-session/authority.ts +77 -0
  290. package/src/hosted-session/coordinator.ts +113 -56
  291. package/src/hosted-session/errors.ts +21 -0
  292. package/src/hosted-session/index.ts +14 -2
  293. package/src/hosted-session/reconnecting-watch.ts +148 -0
  294. package/src/hosted-session/types.ts +22 -8
  295. package/src/hosted-target/access-url.ts +46 -0
  296. package/src/hosted-target/adapter-request.ts +28 -0
  297. package/src/hosted-target/adapter-types.ts +39 -0
  298. package/src/hosted-target/bounds.ts +1 -0
  299. package/src/hosted-target/capsule-error-response.ts +107 -0
  300. package/src/hosted-target/errors.ts +36 -35
  301. package/src/hosted-target/index.ts +10 -23
  302. package/src/hosted-target/response-validation.ts +170 -62
  303. package/src/hosted-target/retry-executor.ts +62 -0
  304. package/src/hosted-target/retry.ts +47 -11
  305. package/src/hosted-target/target-adapter.ts +12 -8
  306. package/src/hosted-target/types.ts +19 -18
  307. package/src/hosted-target/zero-cloud-v1-adapter.ts +229 -316
  308. package/src/isolation-manager.js +1 -16
  309. package/src/target/bounded-response.ts +68 -0
  310. package/src/target/credential-lock.ts +1 -3
  311. package/src/target/credential-store.ts +14 -10
  312. package/src/target/device-flow.ts +209 -85
  313. package/src/target/discovery-errors.ts +6 -0
  314. package/src/target/discovery-sections.ts +251 -0
  315. package/src/target/discovery-validation.ts +144 -0
  316. package/src/target/discovery.ts +163 -97
  317. package/src/target/index.ts +15 -11
  318. package/src/target/oauth-http.ts +27 -0
  319. package/src/target/refresh-exchange.ts +36 -0
  320. package/src/target/refresh-revocation.ts +29 -0
  321. package/src/target/route-template.ts +105 -0
  322. package/src/target/session-errors.ts +9 -0
  323. package/src/target/session-verification.ts +51 -0
  324. package/src/target/target-registry.ts +60 -27
  325. package/src/target/target-session-manager.ts +285 -0
  326. package/src/target/target-session.ts +105 -226
  327. package/src/target/token-response.ts +62 -0
  328. package/docker/zeroshot-oecp/Cargo.toml +0 -12
  329. package/docker/zeroshot-oecp/Dockerfile +0 -65
  330. package/docker/zeroshot-oecp/src/main.rs +0 -32
  331. package/lib/target/bounded-json.d.ts +0 -6
  332. package/lib/target/bounded-json.js +0 -43
  333. package/lib/target/hosted-run/client.d.ts +0 -26
  334. package/lib/target/hosted-run/client.js +0 -158
  335. package/lib/target/hosted-run/commands.d.ts +0 -4
  336. package/lib/target/hosted-run/commands.js +0 -113
  337. package/lib/target/hosted-run/contracts.d.ts +0 -49
  338. package/lib/target/hosted-run/input.d.ts +0 -5
  339. package/lib/target/hosted-run/input.js +0 -200
  340. package/lib/target/hosted-run.d.ts +0 -4
  341. package/lib/target/hosted-run.js +0 -12
  342. package/src/target/bounded-json.ts +0 -48
  343. package/src/target/hosted-run/client.ts +0 -198
  344. package/src/target/hosted-run/commands.ts +0 -140
  345. package/src/target/hosted-run/contracts.ts +0 -53
  346. package/src/target/hosted-run/input.ts +0 -199
  347. package/src/target/hosted-run.ts +0 -8
@@ -1,158 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RunIntentClient = exports.HostedRunHttpError = exports.TERMINAL_STATES = exports.UUID = void 0;
4
- exports.context = context;
5
- const credential_lock_ts_1 = require("../credential-lock.js");
6
- const credential_store_ts_1 = require("../credential-store.js");
7
- const discovery_ts_1 = require("../discovery.js");
8
- const target_registry_ts_1 = require("../target-registry.js");
9
- const target_session_ts_1 = require("../target-session.js");
10
- const bounded_json_ts_1 = require("../bounded-json.js");
11
- const MAX_RESPONSE_BYTES = 1024 * 1024;
12
- exports.UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
13
- exports.TERMINAL_STATES = new Set(['succeeded', 'failed', 'cancelled', 'expired']);
14
- const RUN_INTENT_STATES = new Set([
15
- 'queued',
16
- 'provisioning',
17
- 'running',
18
- 'cancelling',
19
- ...exports.TERMINAL_STATES,
20
- ]);
21
- function wait(milliseconds) {
22
- return new Promise((resolve) => setTimeout(resolve, milliseconds));
23
- }
24
- class HostedRunHttpError extends Error {
25
- status;
26
- code;
27
- constructor(status, message, code = null) {
28
- super(message);
29
- this.name = 'HostedRunHttpError';
30
- this.status = status;
31
- this.code = code;
32
- }
33
- }
34
- exports.HostedRunHttpError = HostedRunHttpError;
35
- function validateIntent(value) {
36
- if (value === null || typeof value !== 'object' || Array.isArray(value)) {
37
- throw new Error('Zero Cloud returned an invalid run intent');
38
- }
39
- const record = value;
40
- if (typeof record['intent_id'] !== 'string' ||
41
- !exports.UUID.test(record['intent_id']) ||
42
- typeof record['state'] !== 'string' ||
43
- !RUN_INTENT_STATES.has(record['state'])) {
44
- throw new Error('Zero Cloud returned an invalid run intent');
45
- }
46
- const result = record['result'];
47
- if (result !== null && (typeof result !== 'object' || Array.isArray(result))) {
48
- throw new Error('Zero Cloud returned an invalid run intent result');
49
- }
50
- return record;
51
- }
52
- class RunIntentClient {
53
- baseUrl;
54
- organization;
55
- tokenProvider;
56
- fetch;
57
- constructor(baseUrl, organization, tokenProvider, fetchImplementation) {
58
- this.baseUrl = baseUrl.replace(/\/$/, '');
59
- this.organization = organization;
60
- this.tokenProvider = tokenProvider;
61
- this.fetch = fetchImplementation;
62
- }
63
- async submit(body, submissionKey) {
64
- let failure;
65
- for (let attempt = 0; attempt < 2; attempt += 1) {
66
- try {
67
- return await this.request('', {
68
- method: 'POST',
69
- body: JSON.stringify(body),
70
- headers: { 'Idempotency-Key': submissionKey },
71
- });
72
- }
73
- catch (error) {
74
- failure = error;
75
- if (error instanceof HostedRunHttpError && error.status < 500)
76
- throw error;
77
- if (attempt === 0)
78
- await wait(250);
79
- }
80
- }
81
- throw failure;
82
- }
83
- get(intentId) {
84
- return this.request(`/${encodeURIComponent(intentId)}`, { method: 'GET' });
85
- }
86
- cancel(intentId) {
87
- return this.request(`/${encodeURIComponent(intentId)}`, { method: 'DELETE' });
88
- }
89
- async request(suffix, options) {
90
- const token = await this.tokenProvider.getAccessToken();
91
- const url = `${this.baseUrl}/orgs/${encodeURIComponent(this.organization)}/run-intents${suffix}`;
92
- const init = {
93
- method: options.method,
94
- redirect: 'error',
95
- headers: {
96
- Accept: 'application/json',
97
- Authorization: `Bearer ${token}`,
98
- 'Content-Type': 'application/json',
99
- ...options.headers,
100
- },
101
- };
102
- if (options.body !== undefined)
103
- init.body = options.body;
104
- let response;
105
- try {
106
- response = await this.fetch(url, init);
107
- }
108
- catch (error) {
109
- throw new Error(`Zero Cloud request failed: ${error instanceof Error ? error.message : String(error)}`);
110
- }
111
- const body = await (0, bounded_json_ts_1.readBoundedJson)(response, MAX_RESPONSE_BYTES, {
112
- tooLarge: () => new Error('Zero Cloud response exceeded 1 MiB'),
113
- invalid: () => new Error('Zero Cloud returned invalid JSON'),
114
- });
115
- if (!response.ok) {
116
- const problem = body;
117
- const code = typeof problem['code'] === 'string' ? problem['code'] : null;
118
- const message = typeof problem['message'] === 'string' ? problem['message'] : 'request failed';
119
- throw new HostedRunHttpError(response.status, `Zero Cloud ${response.status}: ${message}`, code);
120
- }
121
- return validateIntent(body);
122
- }
123
- }
124
- exports.RunIntentClient = RunIntentClient;
125
- async function context(targetName, deps) {
126
- const environment = deps.environment ?? process.env;
127
- const target = (0, target_registry_ts_1.getTarget)(targetName, deps.settings);
128
- if (!target)
129
- throw new Error(`Target "${targetName}" not found.`);
130
- const http = {
131
- fetch: (url, init) => (deps.fetch ?? globalThis.fetch)(url, init),
132
- };
133
- const discovery = await (0, discovery_ts_1.discoverTargetSessionEndpoints)(target.url, http);
134
- const ephemeralToken = environment['ZEROSHOT_TARGET_ACCESS_TOKEN']?.trim();
135
- const ephemeralOrganization = environment['ZEROSHOT_TARGET_ORGANIZATION']?.trim();
136
- if ((ephemeralToken && !ephemeralOrganization) || (!ephemeralToken && ephemeralOrganization)) {
137
- throw new Error('ZEROSHOT_TARGET_ACCESS_TOKEN and ZEROSHOT_TARGET_ORGANIZATION must be provided together');
138
- }
139
- let organization;
140
- let tokenProvider;
141
- if (ephemeralToken && ephemeralOrganization) {
142
- organization = ephemeralOrganization;
143
- tokenProvider = { getAccessToken: async () => ephemeralToken };
144
- }
145
- else {
146
- if (!target.organization) {
147
- throw new Error(`Login required. Run: zeroshot target login ${targetName}`);
148
- }
149
- organization = target.organization.id;
150
- const credentials = await credential_store_ts_1.KeyringCredentialStore.create();
151
- tokenProvider = (0, target_session_ts_1.getAccessTokenProvider)(targetName, target, credentials, () => (0, credential_lock_ts_1.acquireTargetLock)(target.id), { http, discoveryEndpoints: discovery });
152
- }
153
- return {
154
- targetName,
155
- organization,
156
- client: new RunIntentClient(discovery.capsuleApiBaseUrl, organization, tokenProvider, deps.fetch ?? globalThis.fetch),
157
- };
158
- }
@@ -1,4 +0,0 @@
1
- import type { HostedOptions, HostedRunDependencies, HostedRunIntent } from './contracts.ts';
2
- export declare function runHosted(input: string, options: HostedOptions, deps: HostedRunDependencies): Promise<HostedRunIntent | Record<string, unknown> | null>;
3
- export declare function statusHostedRun(targetName: string, intentId: string, shouldFollow: boolean, deps: HostedRunDependencies): Promise<HostedRunIntent | Record<string, unknown> | null>;
4
- export declare function cancelHostedRun(targetName: string, intentId: string, deps: HostedRunDependencies): Promise<HostedRunIntent>;
@@ -1,113 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.runHosted = runHosted;
7
- exports.statusHostedRun = statusHostedRun;
8
- exports.cancelHostedRun = cancelHostedRun;
9
- const node_crypto_1 = __importDefault(require("node:crypto"));
10
- const client_ts_1 = require("./client.js");
11
- const input_ts_1 = require("./input.js");
12
- const DEFAULT_MODEL = 'openai/gpt-5.4';
13
- const RUN_INTENT_VERSION = 'zeroshot.run-intent/v1';
14
- const MAX_RUN_INTENT_BYTES = 1024 * 1024 + 64 * 1024;
15
- const RUN_INTENT_POLL_MS = 500;
16
- function output(deps, value) {
17
- (deps.stdout ?? process.stdout).write(`${value}\n`);
18
- }
19
- function wait(milliseconds) {
20
- return new Promise((resolve) => setTimeout(resolve, milliseconds));
21
- }
22
- function displayState(intent) {
23
- return intent.waiting_reason ? `${intent.state} (${intent.waiting_reason})` : intent.state;
24
- }
25
- function resumeCommand(value, intentId) {
26
- return `zeroshot target status ${value.targetName} ${intentId} --follow`;
27
- }
28
- async function follow(value, initial, deps) {
29
- let intent = initial;
30
- let displayed = null;
31
- for (;;) {
32
- const state = displayState(intent);
33
- if (state !== displayed) {
34
- output(deps, `Run ${intent.intent_id}: ${state}`);
35
- displayed = state;
36
- }
37
- if (client_ts_1.TERMINAL_STATES.has(intent.state))
38
- break;
39
- await (deps.delay ?? wait)(RUN_INTENT_POLL_MS);
40
- intent = await value.client.get(intent.intent_id);
41
- }
42
- if (intent.state === 'succeeded') {
43
- const summary = intent.result?.['summary'];
44
- if (typeof summary === 'string' && summary)
45
- output(deps, summary);
46
- return intent.result;
47
- }
48
- const detail = intent.error_code ? ` (${intent.error_code})` : '';
49
- throw new Error(`hosted run ${intent.state}${detail}`);
50
- }
51
- async function runHosted(input, options, deps) {
52
- (0, input_ts_1.validateHostedOptions)(options);
53
- const environment = deps.environment ?? process.env;
54
- const resolved = await (0, input_ts_1.resolveHostedInput)(input, options, environment);
55
- const value = await (0, client_ts_1.context)(options.target, deps);
56
- const body = {
57
- label: 'zeroshot-cli',
58
- size: options.size ?? 'standard',
59
- intent: {
60
- version: RUN_INTENT_VERSION,
61
- credentials: {
62
- githubToken: (0, input_ts_1.githubToken)(environment),
63
- openrouterApiKey: (0, input_ts_1.providerKey)(environment),
64
- repository: resolved.repository,
65
- model: options.model ?? DEFAULT_MODEL,
66
- },
67
- request: resolved.request,
68
- },
69
- };
70
- if (Buffer.byteLength(JSON.stringify(body)) > MAX_RUN_INTENT_BYTES) {
71
- throw new Error('hosted run intent exceeds the 1088 KiB upload limit');
72
- }
73
- const submissionKey = options.submissionKey ?? node_crypto_1.default.randomUUID();
74
- let created;
75
- try {
76
- created = await value.client.submit(body, submissionKey);
77
- }
78
- catch (error) {
79
- if (error instanceof client_ts_1.HostedRunHttpError)
80
- throw error;
81
- throw new Error(`${error instanceof Error ? error.message : String(error)}. Recover this submission by rerunning ` +
82
- `the same command with --submission-key ${submissionKey}`, { cause: error });
83
- }
84
- output(deps, `Run ${created.intent_id} queued`);
85
- output(deps, `Resume: ${resumeCommand(value, created.intent_id)}`);
86
- if (options.detach)
87
- return created;
88
- output(deps, 'Ctrl+C disconnects without cancelling.');
89
- return follow(value, created, deps);
90
- }
91
- async function statusHostedRun(targetName, intentId, shouldFollow, deps) {
92
- if (!client_ts_1.UUID.test(intentId))
93
- throw new Error('run intent id must be a UUID');
94
- const value = await (0, client_ts_1.context)(targetName, deps);
95
- const intent = await value.client.get(intentId);
96
- if (!shouldFollow) {
97
- output(deps, JSON.stringify(intent, null, 2));
98
- return intent;
99
- }
100
- if (!client_ts_1.TERMINAL_STATES.has(intent.state)) {
101
- output(deps, `Following ${intentId}; Ctrl+C disconnects without cancelling.`);
102
- output(deps, `Resume: ${resumeCommand(value, intentId)}`);
103
- }
104
- return follow(value, intent, deps);
105
- }
106
- async function cancelHostedRun(targetName, intentId, deps) {
107
- if (!client_ts_1.UUID.test(intentId))
108
- throw new Error('run intent id must be a UUID');
109
- const value = await (0, client_ts_1.context)(targetName, deps);
110
- const intent = await value.client.cancel(intentId);
111
- output(deps, `Run ${intent.intent_id}: ${displayState(intent)}`);
112
- return intent;
113
- }
@@ -1,49 +0,0 @@
1
- import type { SettingsPort } from '../target-registry.ts';
2
- export interface HostedOptions {
3
- readonly target?: string;
4
- readonly repository?: string;
5
- readonly model?: string;
6
- readonly size?: string;
7
- readonly submissionKey?: string;
8
- readonly detach?: boolean;
9
- readonly pr?: boolean;
10
- readonly provider?: string;
11
- readonly config?: string;
12
- readonly docker?: boolean;
13
- readonly worktree?: boolean;
14
- readonly dockerImage?: string;
15
- readonly strictSchema?: boolean;
16
- readonly ship?: boolean;
17
- readonly prBase?: string;
18
- readonly mergeQueue?: boolean;
19
- readonly closeIssue?: string;
20
- readonly workers?: number;
21
- readonly gitlab?: boolean;
22
- readonly jira?: boolean;
23
- readonly devops?: boolean;
24
- readonly linear?: boolean;
25
- readonly mount?: readonly string[];
26
- readonly noMounts?: boolean;
27
- readonly containerHome?: string;
28
- }
29
- export interface HostedRunIntent {
30
- readonly intent_id: string;
31
- readonly state: string;
32
- readonly waiting_reason: string | null;
33
- readonly result: Record<string, unknown> | null;
34
- readonly error_code: string | null;
35
- readonly [key: string]: unknown;
36
- }
37
- export interface HostedRunDependencies {
38
- readonly settings: SettingsPort;
39
- readonly environment?: NodeJS.ProcessEnv;
40
- readonly fetch?: typeof globalThis.fetch;
41
- readonly delay?: (milliseconds: number) => Promise<void>;
42
- readonly stdout?: {
43
- write(value: string): void;
44
- };
45
- }
46
- export interface ResolvedInput {
47
- readonly repository: string;
48
- readonly request: Record<string, unknown>;
49
- }
@@ -1,5 +0,0 @@
1
- import type { HostedOptions, ResolvedInput } from './contracts.ts';
2
- export declare function resolveHostedInput(input: string, options: HostedOptions, environment?: NodeJS.ProcessEnv): Promise<ResolvedInput>;
3
- export declare function githubToken(environment: NodeJS.ProcessEnv): string;
4
- export declare function providerKey(environment: NodeJS.ProcessEnv): string;
5
- export declare function validateHostedOptions(options: HostedOptions): void;
@@ -1,200 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.resolveHostedInput = resolveHostedInput;
7
- exports.githubToken = githubToken;
8
- exports.providerKey = providerKey;
9
- exports.validateHostedOptions = validateHostedOptions;
10
- const node_fs_1 = __importDefault(require("node:fs"));
11
- const node_path_1 = __importDefault(require("node:path"));
12
- const node_child_process_1 = require("node:child_process");
13
- const SUBMISSION_KEY = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{12}$/i;
14
- const CAPSULE_SIZES = new Set(['tiny', 'small', 'standard', 'large']);
15
- function validRepository(value) {
16
- if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(value))
17
- return false;
18
- return value.split('/').every((segment) => segment !== '.' && segment !== '..');
19
- }
20
- function validModel(value) {
21
- return (value.length <= 256 && /^[A-Za-z0-9][A-Za-z0-9._-]*\/[A-Za-z0-9][A-Za-z0-9._:-]*$/.test(value));
22
- }
23
- function repositoryFromRemote(cwd = process.cwd()) {
24
- let remote;
25
- try {
26
- remote = (0, node_child_process_1.execFileSync)('git', ['remote', 'get-url', 'origin'], {
27
- cwd,
28
- encoding: 'utf8',
29
- stdio: ['ignore', 'pipe', 'ignore'],
30
- }).trim();
31
- }
32
- catch {
33
- return null;
34
- }
35
- const match = remote.match(/^(?:git@github\.com:|ssh:\/\/git@github\.com\/|https?:\/\/github\.com\/)([^/]+\/[^/]+?)(?:\.git)?$/);
36
- const repository = match?.[1];
37
- return repository && validRepository(repository) ? repository : null;
38
- }
39
- function isolationProfile(options) {
40
- return options.pr ? 'isolation.pr@1' : 'isolation.worktree@1';
41
- }
42
- function providerProfile(options) {
43
- return options.pr ? 'provider.codex-openrouter-pr@1' : 'provider.codex-openrouter@1';
44
- }
45
- function promptRequest(prompt, options) {
46
- return {
47
- source: 'prompt',
48
- prompt,
49
- artifacts: [],
50
- isolationProfile: isolationProfile(options),
51
- providerProfile: providerProfile(options),
52
- };
53
- }
54
- function issueRequest(issue, options) {
55
- return {
56
- source: 'issue',
57
- issue,
58
- artifacts: [],
59
- isolationProfile: isolationProfile(options),
60
- providerProfile: providerProfile(options),
61
- };
62
- }
63
- function issueInput(value, options) {
64
- const shorthand = value.match(/^([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)#([1-9][0-9]*)$/);
65
- if (shorthand?.[1] && shorthand[2] && validRepository(shorthand[1])) {
66
- const issue = `https://github.com/${shorthand[1]}/issues/${shorthand[2]}`;
67
- return { repository: shorthand[1], request: issueRequest(issue, options) };
68
- }
69
- let url;
70
- try {
71
- url = new URL(value);
72
- }
73
- catch {
74
- return null;
75
- }
76
- const match = url.pathname.match(/^\/([^/]+)\/([^/]+)\/issues\/([1-9][0-9]*)\/?$/);
77
- const repository = match?.[1] && match[2] ? `${match[1]}/${match[2]}` : '';
78
- if (url.hostname !== 'github.com' || !match || !validRepository(repository))
79
- return null;
80
- return { repository, request: issueRequest(url.href, options) };
81
- }
82
- async function readStdin() {
83
- if (process.stdin.isTTY)
84
- throw new Error('zeroshot run - requires piped input');
85
- const chunks = [];
86
- let bytes = 0;
87
- for await (const chunk of process.stdin) {
88
- const value = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
89
- bytes += value.length;
90
- if (bytes > 1024 * 1024)
91
- throw new Error('hosted task input exceeds 1 MiB');
92
- chunks.push(value);
93
- }
94
- const value = Buffer.concat(chunks).toString('utf8').trim();
95
- if (!value)
96
- throw new Error('hosted task input is empty');
97
- return value;
98
- }
99
- function readTaskFile(filename) {
100
- const flags = node_fs_1.default.constants.O_RDONLY | (node_fs_1.default.constants.O_NOFOLLOW ?? 0) | (node_fs_1.default.constants.O_NONBLOCK ?? 0);
101
- let descriptor;
102
- try {
103
- descriptor = node_fs_1.default.openSync(filename, flags);
104
- }
105
- catch (error) {
106
- const code = error.code;
107
- if (code === 'ENOENT' || code === 'ENOTDIR' || code === 'EISDIR')
108
- return null;
109
- if (code === 'ELOOP')
110
- throw new Error(`hosted task file must not be a symlink: ${filename}`);
111
- throw error;
112
- }
113
- try {
114
- if (!node_fs_1.default.fstatSync(descriptor).isFile())
115
- return null;
116
- return node_fs_1.default.readFileSync(descriptor, 'utf8');
117
- }
118
- finally {
119
- node_fs_1.default.closeSync(descriptor);
120
- }
121
- }
122
- async function resolveHostedInput(input, options, environment = process.env) {
123
- const explicitIssue = issueInput(input, options);
124
- if (explicitIssue)
125
- return explicitIssue;
126
- const repository = options.repository ?? environment['ZEROSHOT_REPOSITORY'] ?? repositoryFromRemote();
127
- if (!validRepository(repository ?? '')) {
128
- throw new Error('hosted runs need a GitHub repository; use org/repo#123, --repository owner/name, ' +
129
- 'ZEROSHOT_REPOSITORY, or run inside a GitHub checkout');
130
- }
131
- if (/^[1-9][0-9]*$/.test(input)) {
132
- return { repository: repository, request: issueRequest(input, options) };
133
- }
134
- const prompt = input === '-' ? await readStdin() : (readTaskFile(node_path_1.default.resolve(input)) ?? input);
135
- if (!prompt.trim())
136
- throw new Error('hosted task input is empty');
137
- return { repository: repository, request: promptRequest(prompt.trim(), options) };
138
- }
139
- function githubToken(environment) {
140
- const configured = environment['GH_TOKEN'] ?? environment['GITHUB_TOKEN'];
141
- if (configured?.trim())
142
- return configured.trim();
143
- try {
144
- const token = (0, node_child_process_1.execFileSync)('gh', ['auth', 'token'], {
145
- encoding: 'utf8',
146
- stdio: ['ignore', 'pipe', 'ignore'],
147
- }).trim();
148
- if (token)
149
- return token;
150
- }
151
- catch {
152
- // The actionable error below covers missing and unauthenticated gh alike.
153
- }
154
- throw new Error('hosted runs require GH_TOKEN/GITHUB_TOKEN or an authenticated gh CLI');
155
- }
156
- function providerKey(environment) {
157
- const key = environment['OPENROUTER_API_KEY'];
158
- if (!key?.trim())
159
- throw new Error('hosted Codex runs require OPENROUTER_API_KEY');
160
- return key.trim();
161
- }
162
- function validateHostedOptions(options) {
163
- const unsupported = [
164
- ['config', '--config'],
165
- ['docker', '--docker'],
166
- ['worktree', '--worktree'],
167
- ['dockerImage', '--docker-image'],
168
- ['strictSchema', '--strict-schema'],
169
- ['ship', '--ship'],
170
- ['prBase', '--pr-base'],
171
- ['mergeQueue', '--merge-queue'],
172
- ['closeIssue', '--close-issue'],
173
- ['workers', '--workers'],
174
- ['gitlab', '--gitlab'],
175
- ['jira', '--jira'],
176
- ['devops', '--devops'],
177
- ['linear', '--linear'],
178
- ['mount', '--mount'],
179
- ['noMounts', '--no-mounts'],
180
- ['containerHome', '--container-home'],
181
- ];
182
- const selected = unsupported
183
- .filter(([name]) => options[name] !== undefined && options[name] !== false)
184
- .map(([, flag]) => flag);
185
- if (options.provider && options.provider !== 'codex')
186
- selected.push('--provider');
187
- if (selected.length)
188
- throw new Error(`hosted runs do not support ${selected.join(', ')}`);
189
- if (!options.target)
190
- throw new Error('hosted runs require --target');
191
- if (options.model !== undefined && !validModel(options.model)) {
192
- throw new Error('hosted runs require an exact provider/model slug');
193
- }
194
- if (!CAPSULE_SIZES.has(options.size ?? 'standard')) {
195
- throw new Error('hosted runs require --size tiny, small, standard, or large');
196
- }
197
- if (options.submissionKey !== undefined && !SUBMISSION_KEY.test(options.submissionKey)) {
198
- throw new Error('hosted runs require --submission-key to be a random UUID');
199
- }
200
- }
@@ -1,4 +0,0 @@
1
- export { HostedRunHttpError } from './hosted-run/client.ts';
2
- export { cancelHostedRun, runHosted, statusHostedRun } from './hosted-run/commands.ts';
3
- export { resolveHostedInput, validateHostedOptions } from './hosted-run/input.ts';
4
- export type { HostedOptions, HostedRunDependencies, HostedRunIntent, } from './hosted-run/contracts.ts';
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateHostedOptions = exports.resolveHostedInput = exports.statusHostedRun = exports.runHosted = exports.cancelHostedRun = exports.HostedRunHttpError = void 0;
4
- var client_ts_1 = require("./hosted-run/client.js");
5
- Object.defineProperty(exports, "HostedRunHttpError", { enumerable: true, get: function () { return client_ts_1.HostedRunHttpError; } });
6
- var commands_ts_1 = require("./hosted-run/commands.js");
7
- Object.defineProperty(exports, "cancelHostedRun", { enumerable: true, get: function () { return commands_ts_1.cancelHostedRun; } });
8
- Object.defineProperty(exports, "runHosted", { enumerable: true, get: function () { return commands_ts_1.runHosted; } });
9
- Object.defineProperty(exports, "statusHostedRun", { enumerable: true, get: function () { return commands_ts_1.statusHostedRun; } });
10
- var input_ts_1 = require("./hosted-run/input.js");
11
- Object.defineProperty(exports, "resolveHostedInput", { enumerable: true, get: function () { return input_ts_1.resolveHostedInput; } });
12
- Object.defineProperty(exports, "validateHostedOptions", { enumerable: true, get: function () { return input_ts_1.validateHostedOptions; } });
@@ -1,48 +0,0 @@
1
- interface BoundedJsonErrors {
2
- readonly tooLarge: () => Error;
3
- readonly invalid: () => Error;
4
- }
5
-
6
- export async function readBoundedJson(
7
- response: Response,
8
- maxBytes: number,
9
- errors: BoundedJsonErrors
10
- ): Promise<unknown> {
11
- const declared = response.headers.get('content-length');
12
- if (declared !== null && Number(declared) > maxBytes) throw errors.tooLarge();
13
-
14
- const reader = response.body?.getReader();
15
- if (!reader) {
16
- const bytes = new Uint8Array(await response.arrayBuffer());
17
- if (bytes.byteLength > maxBytes) throw errors.tooLarge();
18
- return parseJson(bytes, errors);
19
- }
20
-
21
- const chunks: Uint8Array[] = [];
22
- let total = 0;
23
- for (;;) {
24
- const { done, value } = await reader.read();
25
- if (done) break;
26
- total += value.byteLength;
27
- if (total > maxBytes) {
28
- await reader.cancel();
29
- throw errors.tooLarge();
30
- }
31
- chunks.push(value);
32
- }
33
- const bytes = new Uint8Array(total);
34
- let offset = 0;
35
- for (const chunk of chunks) {
36
- bytes.set(chunk, offset);
37
- offset += chunk.byteLength;
38
- }
39
- return parseJson(bytes, errors);
40
- }
41
-
42
- function parseJson(bytes: Uint8Array, errors: BoundedJsonErrors): unknown {
43
- try {
44
- return JSON.parse(new TextDecoder().decode(bytes));
45
- } catch {
46
- throw errors.invalid();
47
- }
48
- }