@b3os/sdk 0.1.0 → 0.1.3

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 (204) hide show
  1. package/README.md +171 -42
  2. package/dist/cjs/core/bytes.d.ts +30 -0
  3. package/dist/cjs/core/bytes.js +115 -0
  4. package/dist/cjs/core/bytes.js.map +1 -0
  5. package/dist/cjs/core/client.d.ts +61 -3
  6. package/dist/cjs/core/client.js +211 -33
  7. package/dist/cjs/core/client.js.map +1 -1
  8. package/dist/cjs/core/errors.d.ts +16 -4
  9. package/dist/cjs/core/errors.js +9 -6
  10. package/dist/cjs/core/errors.js.map +1 -1
  11. package/dist/cjs/core/handle.d.ts +31 -0
  12. package/dist/cjs/core/handle.js +86 -0
  13. package/dist/cjs/core/handle.js.map +1 -0
  14. package/dist/cjs/core/index.d.ts +6 -4
  15. package/dist/cjs/core/index.js +8 -1
  16. package/dist/cjs/core/index.js.map +1 -1
  17. package/dist/cjs/core/login.d.ts +24 -0
  18. package/dist/cjs/core/login.js +93 -0
  19. package/dist/cjs/core/login.js.map +1 -0
  20. package/dist/cjs/core/namespace.d.ts +1 -1
  21. package/dist/cjs/core/namespace.js +1 -1
  22. package/dist/cjs/core/op.d.ts +11 -1
  23. package/dist/cjs/core/timing.d.ts +1 -1
  24. package/dist/cjs/core/timing.js +8 -1
  25. package/dist/cjs/core/timing.js.map +1 -1
  26. package/dist/cjs/core/transport.js +1 -1
  27. package/dist/cjs/core/transport.js.map +1 -1
  28. package/dist/cjs/core/util.d.ts +15 -3
  29. package/dist/cjs/core/util.js +54 -2
  30. package/dist/cjs/core/util.js.map +1 -1
  31. package/dist/cjs/core/ws.d.ts +4 -0
  32. package/dist/cjs/core/ws.js +7 -4
  33. package/dist/cjs/core/ws.js.map +1 -1
  34. package/dist/cjs/generated/client.d.ts +2 -2
  35. package/dist/cjs/generated/client.js +2 -2
  36. package/dist/cjs/generated/errors.d.ts +1 -1
  37. package/dist/cjs/generated/errors.js +30 -0
  38. package/dist/cjs/generated/errors.js.map +1 -1
  39. package/dist/cjs/generated/jobs.js +1 -1
  40. package/dist/cjs/generated/ns/actions.d.ts +48 -6
  41. package/dist/cjs/generated/ns/actions.js +59 -6
  42. package/dist/cjs/generated/ns/actions.js.map +1 -1
  43. package/dist/cjs/generated/ns/infer.d.ts +185 -54
  44. package/dist/cjs/generated/ns/infer.js +28 -74
  45. package/dist/cjs/generated/ns/infer.js.map +1 -1
  46. package/dist/cjs/generated/ns/keys.d.ts +12 -0
  47. package/dist/cjs/generated/ns/keys.js +22 -0
  48. package/dist/cjs/generated/ns/keys.js.map +1 -1
  49. package/dist/cjs/generated/ns/render.d.ts +95 -6
  50. package/dist/cjs/generated/ns/render.js +124 -7
  51. package/dist/cjs/generated/ns/render.js.map +1 -1
  52. package/dist/cjs/generated/ns/requests.d.ts +3 -1
  53. package/dist/cjs/generated/ns/requests.js +3 -1
  54. package/dist/cjs/generated/ns/requests.js.map +1 -1
  55. package/dist/cjs/generated/ns/routines.d.ts +131 -9
  56. package/dist/cjs/generated/ns/routines.js +186 -9
  57. package/dist/cjs/generated/ns/routines.js.map +1 -1
  58. package/dist/cjs/generated/ns/sandboxes.d.ts +298 -31
  59. package/dist/cjs/generated/ns/sandboxes.js +368 -29
  60. package/dist/cjs/generated/ns/sandboxes.js.map +1 -1
  61. package/dist/cjs/generated/ns/vision.d.ts +133 -14
  62. package/dist/cjs/generated/ns/vision.js +158 -12
  63. package/dist/cjs/generated/ns/vision.js.map +1 -1
  64. package/dist/cjs/generated/ns/webhooks.d.ts +8 -5
  65. package/dist/cjs/generated/ns/webhooks.js +8 -5
  66. package/dist/cjs/generated/ns/webhooks.js.map +1 -1
  67. package/dist/cjs/generated/types.d.ts +1956 -301
  68. package/dist/cjs/helpers/agents.d.ts +95 -0
  69. package/dist/cjs/helpers/agents.js +205 -0
  70. package/dist/cjs/helpers/agents.js.map +1 -0
  71. package/dist/cjs/helpers/render.d.ts +15 -0
  72. package/dist/cjs/helpers/render.js +74 -0
  73. package/dist/cjs/helpers/render.js.map +1 -0
  74. package/dist/cjs/helpers/routines.d.ts +61 -0
  75. package/dist/cjs/helpers/routines.js +234 -0
  76. package/dist/cjs/helpers/routines.js.map +1 -0
  77. package/dist/cjs/helpers/sandboxes.d.ts +237 -0
  78. package/dist/cjs/helpers/sandboxes.js +608 -0
  79. package/dist/cjs/helpers/sandboxes.js.map +1 -0
  80. package/dist/cjs/index.d.ts +4 -1
  81. package/dist/cjs/index.js +8 -5
  82. package/dist/cjs/index.js.map +1 -1
  83. package/dist/cjs/routines.d.ts +1 -0
  84. package/dist/cjs/routines.js +21 -0
  85. package/dist/cjs/routines.js.map +1 -0
  86. package/dist/cjs/webhooks.js.map +1 -1
  87. package/dist/esm/core/bytes.d.ts +30 -0
  88. package/dist/esm/core/bytes.js +111 -0
  89. package/dist/esm/core/bytes.js.map +1 -0
  90. package/dist/esm/core/client.d.ts +61 -3
  91. package/dist/esm/core/client.js +208 -31
  92. package/dist/esm/core/client.js.map +1 -1
  93. package/dist/esm/core/errors.d.ts +16 -4
  94. package/dist/esm/core/errors.js +9 -6
  95. package/dist/esm/core/errors.js.map +1 -1
  96. package/dist/esm/core/handle.d.ts +31 -0
  97. package/dist/esm/core/handle.js +82 -0
  98. package/dist/esm/core/handle.js.map +1 -0
  99. package/dist/esm/core/index.d.ts +6 -4
  100. package/dist/esm/core/index.js +4 -2
  101. package/dist/esm/core/index.js.map +1 -1
  102. package/dist/esm/core/login.d.ts +24 -0
  103. package/dist/esm/core/login.js +85 -0
  104. package/dist/esm/core/login.js.map +1 -0
  105. package/dist/esm/core/namespace.d.ts +1 -1
  106. package/dist/esm/core/namespace.js +1 -1
  107. package/dist/esm/core/op.d.ts +11 -1
  108. package/dist/esm/core/timing.d.ts +1 -1
  109. package/dist/esm/core/timing.js +8 -1
  110. package/dist/esm/core/timing.js.map +1 -1
  111. package/dist/esm/core/transport.js +1 -1
  112. package/dist/esm/core/transport.js.map +1 -1
  113. package/dist/esm/core/util.d.ts +15 -3
  114. package/dist/esm/core/util.js +48 -1
  115. package/dist/esm/core/util.js.map +1 -1
  116. package/dist/esm/core/ws.d.ts +4 -0
  117. package/dist/esm/core/ws.js +7 -4
  118. package/dist/esm/core/ws.js.map +1 -1
  119. package/dist/esm/generated/client.d.ts +2 -2
  120. package/dist/esm/generated/client.js +2 -2
  121. package/dist/esm/generated/errors.d.ts +1 -1
  122. package/dist/esm/generated/errors.js +30 -0
  123. package/dist/esm/generated/errors.js.map +1 -1
  124. package/dist/esm/generated/jobs.js +1 -1
  125. package/dist/esm/generated/ns/actions.d.ts +48 -6
  126. package/dist/esm/generated/ns/actions.js +59 -6
  127. package/dist/esm/generated/ns/actions.js.map +1 -1
  128. package/dist/esm/generated/ns/infer.d.ts +185 -54
  129. package/dist/esm/generated/ns/infer.js +28 -74
  130. package/dist/esm/generated/ns/infer.js.map +1 -1
  131. package/dist/esm/generated/ns/keys.d.ts +12 -0
  132. package/dist/esm/generated/ns/keys.js +22 -0
  133. package/dist/esm/generated/ns/keys.js.map +1 -1
  134. package/dist/esm/generated/ns/render.d.ts +95 -6
  135. package/dist/esm/generated/ns/render.js +124 -7
  136. package/dist/esm/generated/ns/render.js.map +1 -1
  137. package/dist/esm/generated/ns/requests.d.ts +3 -1
  138. package/dist/esm/generated/ns/requests.js +3 -1
  139. package/dist/esm/generated/ns/requests.js.map +1 -1
  140. package/dist/esm/generated/ns/routines.d.ts +131 -9
  141. package/dist/esm/generated/ns/routines.js +186 -9
  142. package/dist/esm/generated/ns/routines.js.map +1 -1
  143. package/dist/esm/generated/ns/sandboxes.d.ts +298 -31
  144. package/dist/esm/generated/ns/sandboxes.js +368 -29
  145. package/dist/esm/generated/ns/sandboxes.js.map +1 -1
  146. package/dist/esm/generated/ns/vision.d.ts +133 -14
  147. package/dist/esm/generated/ns/vision.js +158 -12
  148. package/dist/esm/generated/ns/vision.js.map +1 -1
  149. package/dist/esm/generated/ns/webhooks.d.ts +8 -5
  150. package/dist/esm/generated/ns/webhooks.js +8 -5
  151. package/dist/esm/generated/ns/webhooks.js.map +1 -1
  152. package/dist/esm/generated/types.d.ts +1956 -301
  153. package/dist/esm/helpers/agents.d.ts +95 -0
  154. package/dist/esm/helpers/agents.js +199 -0
  155. package/dist/esm/helpers/agents.js.map +1 -0
  156. package/dist/esm/helpers/render.d.ts +15 -0
  157. package/dist/esm/helpers/render.js +70 -0
  158. package/dist/esm/helpers/render.js.map +1 -0
  159. package/dist/esm/helpers/routines.d.ts +61 -0
  160. package/dist/esm/helpers/routines.js +226 -0
  161. package/dist/esm/helpers/routines.js.map +1 -0
  162. package/dist/esm/helpers/sandboxes.d.ts +237 -0
  163. package/dist/esm/helpers/sandboxes.js +600 -0
  164. package/dist/esm/helpers/sandboxes.js.map +1 -0
  165. package/dist/esm/index.d.ts +4 -1
  166. package/dist/esm/index.js +6 -5
  167. package/dist/esm/index.js.map +1 -1
  168. package/dist/esm/routines.d.ts +1 -0
  169. package/dist/esm/routines.js +5 -0
  170. package/dist/esm/routines.js.map +1 -0
  171. package/dist/esm/webhooks.js.map +1 -1
  172. package/package.json +14 -3
  173. package/src/core/bytes.ts +112 -0
  174. package/src/core/client.ts +253 -33
  175. package/src/core/errors.ts +27 -10
  176. package/src/core/handle.ts +97 -0
  177. package/src/core/index.ts +7 -2
  178. package/src/core/login.ts +98 -0
  179. package/src/core/namespace.ts +1 -1
  180. package/src/core/op.ts +11 -1
  181. package/src/core/timing.ts +7 -2
  182. package/src/core/transport.ts +1 -1
  183. package/src/core/util.ts +53 -3
  184. package/src/core/ws.ts +11 -4
  185. package/src/generated/client.ts +2 -2
  186. package/src/generated/errors.ts +60 -0
  187. package/src/generated/jobs.ts +1 -1
  188. package/src/generated/ns/actions.ts +69 -6
  189. package/src/generated/ns/infer.ts +229 -61
  190. package/src/generated/ns/keys.ts +23 -0
  191. package/src/generated/ns/render.ts +152 -7
  192. package/src/generated/ns/requests.ts +3 -1
  193. package/src/generated/ns/routines.ts +205 -9
  194. package/src/generated/ns/sandboxes.ts +434 -34
  195. package/src/generated/ns/vision.ts +168 -14
  196. package/src/generated/ns/webhooks.ts +8 -5
  197. package/src/generated/types.ts +2211 -422
  198. package/src/helpers/agents.ts +300 -0
  199. package/src/helpers/render.ts +91 -0
  200. package/src/helpers/routines.ts +278 -0
  201. package/src/helpers/sandboxes.ts +788 -0
  202. package/src/index.ts +29 -5
  203. package/src/routines.ts +5 -0
  204. package/src/webhooks.ts +5 -1
@@ -0,0 +1,226 @@
1
+ // @b3os/sdk/routines: build routine definitions (packages/spec/schemas/routine.schema.json) in TypeScript, with the
2
+ // product steps' `with` typed as their API request bodies, then publish them with `b3os.routines.publishRoutine`.
3
+ //
4
+ // import { defineRoutine, expr, step } from "@b3os/sdk/routines";
5
+ // const digest = defineRoutine({
6
+ // name: "hn-digest",
7
+ // inputs: { type: "object", properties: { url: { type: "string" } }, required: ["url"] },
8
+ // triggers: [{ type: "cron", cron: "0 0 8 * * *", tz: "America/Los_Angeles" }],
9
+ // steps: [
10
+ // step.vision.scrape("page", { url: expr("inputs.url"), formats: ["markdown"] }),
11
+ // step.infer.chat("summary", { model: "b3os/qwen3.5-9b", messages: [{ role: "user", content: expr("steps.page.outputs.markdown") }] }, { needs: ["page"] }),
12
+ // ],
13
+ // output: expr("steps.summary.outputs.choices[0].message.content"),
14
+ // });
15
+ // await b3os.routines.publishRoutine(digest.name, digest);
16
+ //
17
+ // `defineRoutine` checks what can be checked without the node: step ids unique per scope, `needs` naming steps of
18
+ // the same scope, `uses` in the callable grammar, and every `${{` closed (scanned as the node scans it). The node
19
+ // validates the rest at publish (CEL names, schemas).
20
+ /** `${{ <cel> }}`: a CEL expression, evaluated when the step runs. */
21
+ export function expr(cel) {
22
+ return `\${{ ${cel} }}`;
23
+ }
24
+ /** `${{ steps.<id>.outputs.<path> }}`. */
25
+ export function output(stepId, path = "") {
26
+ return expr(`steps.${stepId}.outputs${path ? (path.startsWith("[") ? path : `.${path}`) : ""}`);
27
+ }
28
+ function callStep(uses) {
29
+ return (id, w, options = {}) => ({
30
+ id,
31
+ uses,
32
+ with: w,
33
+ ...options,
34
+ });
35
+ }
36
+ /** Steps for B3OS's first-party callables and marketplace actions. */
37
+ export const step = {
38
+ vision: {
39
+ scrape: callStep("vision/scrape@1"),
40
+ },
41
+ render: {
42
+ screenshot: callStep("render/screenshot@1"),
43
+ },
44
+ sandboxes: {
45
+ /** `sandboxes/exec@1`: `with` is the exec body plus the sandbox (`sandbox_id`). */
46
+ exec: callStep("sandboxes/exec@1"),
47
+ },
48
+ infer: {
49
+ chat: callStep("infer/chat@1"),
50
+ },
51
+ core: {
52
+ http: callStep("core/http@1"),
53
+ transform: (id, value, o = {}) => ({
54
+ id,
55
+ uses: "core/transform@1",
56
+ with: { value },
57
+ ...o,
58
+ }),
59
+ log: callStep("core/log@1"),
60
+ /** Waits for a signal (`routine signal`), until a time, or for a duration. */
61
+ wait: (id, wait, o = {}) => ({
62
+ id,
63
+ wait,
64
+ ...o,
65
+ }),
66
+ sleep: (id, ms, o = {}) => ({
67
+ id,
68
+ sleep_ms: ms,
69
+ ...o,
70
+ }),
71
+ },
72
+ /** A marketplace action: `io.b3os/github/create-issue@1.2.0` with its input. */
73
+ action: (id, ref, input, o = {}) => ({
74
+ id,
75
+ uses: ref,
76
+ with: input,
77
+ ...o,
78
+ }),
79
+ /** Another routine (`routine/<id>@<version>`). */
80
+ routine: (id, ref, input, o = {}) => ({
81
+ id,
82
+ uses: ref.startsWith("routine/") ? ref : `routine/${ref}`,
83
+ with: input,
84
+ ...o,
85
+ }),
86
+ /** Runs `steps` once per item of `over` (a CEL list), `concurrency` at a time. */
87
+ map: (id, over, steps, o = {}) => {
88
+ const { concurrency, ...rest } = o;
89
+ return { id, map: { over, steps, ...(concurrency !== undefined ? { concurrency } : {}) }, ...rest };
90
+ },
91
+ };
92
+ /** A definition that does not pass the client-side checks; `problems` lists each as `<pointer>: <detail>`. */
93
+ export class RoutineDefinitionError extends Error {
94
+ problems;
95
+ constructor(problems) {
96
+ super(`invalid routine definition: ${problems.join("; ")}`);
97
+ this.name = "RoutineDefinitionError";
98
+ this.problems = problems;
99
+ }
100
+ }
101
+ // routine.schema.json's `name`, `$defs.ident` and `$defs.callable` (a test keeps these equal to the schema).
102
+ export const NAME_PATTERN = "^[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?$";
103
+ export const IDENT_PATTERN = "^[a-z_][a-z0-9_]{0,63}$";
104
+ const RESERVED = new Set("true false null in as break const continue else for function if import let loop package namespace return var void while".split(" "));
105
+ export const CALLABLE_PATTERNS = [
106
+ "^(vision|render|sandboxes|infer)/[a-z][a-z0-9_-]{0,63}@[1-9][0-9]{0,3}$",
107
+ "^core/[a-z][a-z0-9_-]{0,63}@[1-9][0-9]{0,3}$",
108
+ "^routine/rtn_[0-9a-hjkmnp-tv-z]{26}@ver_[0-9a-hjkmnp-tv-z]{26}$",
109
+ "^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)+/[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?/[a-z][a-z0-9]*(-[a-z0-9]+)*@((0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*)?|[1-9][0-9]*)$",
110
+ ];
111
+ const IDENT = new RegExp(IDENT_PATTERN);
112
+ const CALLABLES = CALLABLE_PATTERNS.map((p) => new RegExp(p));
113
+ const isCallable = (s) => CALLABLES.some((r) => r.test(s)) && !/^core\/(map|wait|sleep)@/.test(s);
114
+ function strings(v, at, out) {
115
+ if (typeof v === "string")
116
+ out.push([at, v]);
117
+ else if (Array.isArray(v))
118
+ v.forEach((x, i) => strings(x, `${at}/${i}`, out));
119
+ else if (v && typeof v === "object")
120
+ for (const [k, x] of Object.entries(v))
121
+ strings(x, `${at}/${k}`, out);
122
+ }
123
+ function checkSteps(steps, at, problems) {
124
+ const ids = new Set();
125
+ steps.forEach((s, i) => {
126
+ const p = `${at}/${i}`;
127
+ if (!IDENT.test(s.id) || RESERVED.has(s.id))
128
+ problems.push(`${p}/id: ${JSON.stringify(s.id)} is not a step id (a CEL identifier: [a-z_][a-z0-9_]*)`);
129
+ if (ids.has(s.id))
130
+ problems.push(`${p}/id: duplicate step id ${s.id}`);
131
+ ids.add(s.id);
132
+ const kinds = [s.uses !== undefined, s.map !== undefined, s.wait !== undefined, s.sleep_ms !== undefined].filter(Boolean);
133
+ if (kinds.length !== 1)
134
+ problems.push(`${p}: a step has exactly one of uses, map, wait, sleep_ms`);
135
+ if (s.uses !== undefined && !isCallable(s.uses))
136
+ problems.push(`${p}/uses: ${s.uses} is not <ref>@<version>`);
137
+ if (s.map)
138
+ checkSteps(s.map.steps, `${p}/map/steps`, problems);
139
+ });
140
+ steps.forEach((s, i) => {
141
+ for (const n of s.needs ?? []) {
142
+ if (!ids.has(n))
143
+ problems.push(`${at}/${i}/needs: no step ${n} in this scope`);
144
+ if (n === s.id)
145
+ problems.push(`${at}/${i}/needs: a step cannot need itself`);
146
+ }
147
+ });
148
+ }
149
+ /**
150
+ * The offset of the first `${{` that is never closed, or -1. Scans as the node does (crates/b3os-routines
151
+ * src/cel/template.rs): an expression runs to the first `}}` at brace depth 0 outside a CEL string literal (quoted
152
+ * with ', ", three ' or three ", with backslash escapes); a `}}` outside any `${{` is literal text.
153
+ */
154
+ export function unterminatedTemplate(s) {
155
+ let i = 0;
156
+ while (i < s.length) {
157
+ if (s.startsWith("${{", i)) {
158
+ const end = expressionEnd(s, i + 3);
159
+ if (end < 0)
160
+ return i;
161
+ i = end + 2;
162
+ }
163
+ else
164
+ i += 1;
165
+ }
166
+ return -1;
167
+ }
168
+ function expressionEnd(s, from) {
169
+ let j = from;
170
+ let depth = 0;
171
+ let quote;
172
+ while (j < s.length) {
173
+ if (quote !== undefined) {
174
+ if (s[j] === "\\")
175
+ j += 2;
176
+ else if (s.startsWith(quote, j)) {
177
+ j += quote.length;
178
+ quote = undefined;
179
+ }
180
+ else
181
+ j += 1;
182
+ continue;
183
+ }
184
+ if (s.startsWith("'''", j) || s.startsWith('"""', j)) {
185
+ quote = s.slice(j, j + 3);
186
+ j += 3;
187
+ continue;
188
+ }
189
+ const c = s[j];
190
+ if (c === "'" || c === '"') {
191
+ quote = c;
192
+ j += 1;
193
+ continue;
194
+ }
195
+ if (c === "{")
196
+ depth += 1;
197
+ else if (c === "}") {
198
+ if (depth === 0 && s[j + 1] === "}")
199
+ return j;
200
+ depth -= 1;
201
+ }
202
+ j += 1;
203
+ }
204
+ return -1;
205
+ }
206
+ /** Checks a routine definition client-side and returns it unchanged (typed for publishRoutine). */
207
+ export function defineRoutine(def) {
208
+ const problems = [];
209
+ if (!new RegExp(NAME_PATTERN).test(def.name))
210
+ problems.push(`/name: ${JSON.stringify(def.name)} is not a routine name`);
211
+ if (!def.steps?.length)
212
+ problems.push("/steps: a routine needs at least one step");
213
+ else
214
+ checkSteps(def.steps, "/steps", problems);
215
+ const texts = [];
216
+ strings(def, "", texts);
217
+ for (const [at, t] of texts) {
218
+ const open = unterminatedTemplate(t);
219
+ if (open >= 0)
220
+ problems.push(`${at}: unbalanced \${{ }}: the \${{ at offset ${open} is never closed in ${JSON.stringify(t.slice(0, 60))}`);
221
+ }
222
+ if (problems.length)
223
+ throw new RoutineDefinitionError(problems);
224
+ return def;
225
+ }
226
+ //# sourceMappingURL=routines.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routines.js","sourceRoot":"","sources":["../../../src/helpers/routines.ts"],"names":[],"mappings":"AAAA,oHAAoH;AACpH,kHAAkH;AAClH,EAAE;AACF,oEAAoE;AACpE,mCAAmC;AACnC,yBAAyB;AACzB,8FAA8F;AAC9F,oFAAoF;AACpF,eAAe;AACf,wFAAwF;AACxF,mKAAmK;AACnK,SAAS;AACT,wEAAwE;AACxE,QAAQ;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AE,MAAM,IAAI,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IAChE,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,237 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { BinaryInput, RequestOptions } from "../core/client.ts";
3
+ import { APIPromise } from "../core/promise.ts";
4
+ import type { PtySession } from "../core/ws.ts";
5
+ import type { RenderAPI } from "../generated/ns/render.ts";
6
+ import type { SandboxesAPI } from "../generated/ns/sandboxes.ts";
7
+ import type { VisionAPI } from "../generated/ns/vision.ts";
8
+ import type * as T from "../generated/types.ts";
9
+ /** What "ready" means for a sandbox. */
10
+ export type ReadyProbe =
11
+ /** A TCP connection to `host:port` inside the sandbox succeeds (default host 127.0.0.1). */
12
+ {
13
+ port: number;
14
+ host?: string;
15
+ }
16
+ /** An HTTP GET inside the sandbox answers `status` (a code, a list, or a test; default 200-399). */
17
+ | {
18
+ http: string | {
19
+ port: number;
20
+ path?: string;
21
+ };
22
+ status?: number | readonly number[] | ((s: number) => boolean);
23
+ }
24
+ /** A command exits 0 (argv array, or a string run with /bin/sh -c). */
25
+ | {
26
+ exec: string | readonly string[];
27
+ }
28
+ /** A background process prints a whole line matching `log` (stdout or stderr; tested once the line is complete). */
29
+ | {
30
+ log: string | RegExp;
31
+ pid: number;
32
+ };
33
+ export interface ReadyOptions extends RequestOptions {
34
+ /** Give up after this long (default 60 s). */
35
+ timeoutMs?: number;
36
+ /** First retry after this long, then backing off (default 25 ms). */
37
+ intervalMs?: number;
38
+ /** Longest gap between attempts (default 250 ms). */
39
+ maxIntervalMs?: number;
40
+ /** A background process the readiness depends on: fail at once if it exits before the probe passes. */
41
+ pid?: number;
42
+ }
43
+ export interface ReadyResult {
44
+ /** Milliseconds from the call to the first passing probe (client side). */
45
+ readyMs: number;
46
+ /** Probe attempts made (1 when it was ready on the first try). */
47
+ attempts: number;
48
+ }
49
+ /**
50
+ * Polls a probe inside the sandbox until it passes, the process it depends on exits, or `timeoutMs` passes.
51
+ * The first attempt runs at once; later ones back off from `intervalMs` to `maxIntervalMs`.
52
+ */
53
+ export declare function waitUntilReady(api: SandboxesAPI, sandboxId: string, probe: ReadyProbe, options?: ReadyOptions): Promise<ReadyResult>;
54
+ export interface ServiceOptions extends ReadyOptions {
55
+ cmd: string | readonly string[];
56
+ cwd?: string;
57
+ env?: Record<string, string>;
58
+ user?: "user" | "root";
59
+ /** When the service counts as up; without one, startService returns once the process started. */
60
+ ready?: ReadyProbe;
61
+ /** Mint a preview URL for this port once ready (the port must be in the sandbox's `ports`). */
62
+ previewPort?: number;
63
+ /** Lifetime of a private preview URL, in seconds. */
64
+ previewTtlS?: number;
65
+ }
66
+ export interface Service {
67
+ pid: number;
68
+ process: T.SandboxProcess;
69
+ /** Client-side milliseconds from the start request to the passing probe (absent without `ready`). */
70
+ readyMs?: number;
71
+ /** The preview URL, when `previewPort` was given. */
72
+ url?: string;
73
+ /** Sends TERM (or the given signal) to the service's process group. */
74
+ stop(signal?: "TERM" | "KILL" | "INT" | "HUP"): Promise<T.SandboxProcess>;
75
+ }
76
+ /** Starts a background process (a server, a worker) and waits until it is ready. */
77
+ export declare function startService(api: SandboxesAPI, sandboxId: string, options: ServiceOptions): Promise<Service>;
78
+ /** What happens to a sandbox at `expires_at`: `stop` (the default) or `pause`. */
79
+ export type OnTimeout = "stop" | "pause";
80
+ /**
81
+ * The body `createSandbox` and `sandboxes.create` take: the API's `SandboxCreateRequest`, with `on_timeout` for
82
+ * what happens at `expires_at`. The API calls that setting `on_idle`, a name that suggests idleness; it is renamed
83
+ * `on_timeout` before it becomes a stored setting (dx#7). The SDK sends it as `on_idle` until the API takes the
84
+ * new name too, and `on_idle` keeps working.
85
+ */
86
+ export type SandboxCreateBody = Omit<T.SandboxCreateRequest, "on_idle"> & {
87
+ /**
88
+ * What happens at `expires_at`: `stop` (default) destroys the sandbox; `pause` pauses it and keeps it for one
89
+ * hour, so you can extend its timeout and resume it.
90
+ */
91
+ on_timeout?: OnTimeout;
92
+ /** @deprecated The old name of `on_timeout`; it does the same. */
93
+ on_idle?: OnTimeout;
94
+ };
95
+ /** The create body as the API reads it today: `on_timeout` goes as `on_idle`. Both, set differently, is refused. */
96
+ export declare function createBodyOnWire(body: SandboxCreateBody | undefined): T.SandboxCreateRequest;
97
+ /**
98
+ * createSandbox, sent again with the same Idempotency-Key after a lost answer. b3osd runs a keyed create to the end
99
+ * and stores its answer even when the client goes away (crates/b3os-core/src/web.rs `run_detached`; b3osd test
100
+ * `idempotent_requests_finish_when_the_client_disconnects`), so the resend gets the first create's sandbox
101
+ * (`Idempotent-Replayed: true`) instead of making a second one. While that create still runs the node answers
102
+ * 409 `idempotency_request_in_progress`; those waits (Retry-After, at most 60 s in all) are not counted as
103
+ * retries. Answers that refused the create before it ran (429, 503 with a problem body) are retried too. The body
104
+ * goes as you gave it, with `on_timeout` sent as `on_idle` (createBodyOnWire): the key never enters your
105
+ * `metadata` (dx#9). `options.maxRetries` overrides the client's `maxRetries` (`clientMaxRetries`), as for every
106
+ * other call.
107
+ */
108
+ export declare function createSandboxSafely(send: (body: T.SandboxCreateRequest, o: RequestOptions) => APIPromise<T.Sandbox>, body: SandboxCreateBody | undefined, options?: RequestOptions, clientMaxRetries?: number): APIPromise<T.Sandbox>;
109
+ /**
110
+ * What a {@link Sandbox} handle needs of a client: `new B3OS()` has all of it. `render` and `vision` are used only
111
+ * by `ports.screenshot` and `ports.scrape`.
112
+ */
113
+ export interface SandboxClient {
114
+ sandboxes: SandboxesAPI;
115
+ render?: Pick<RenderAPI, "createScreenshot">;
116
+ vision?: Pick<VisionAPI, "scrape">;
117
+ }
118
+ export interface SandboxExecOptions extends RequestOptions {
119
+ /** Called with each stdout chunk as it arrives (the command is then streamed: up to an hour). */
120
+ onStdout?: (data: string) => void;
121
+ /** Called with each stderr chunk as it arrives (the command is then streamed). */
122
+ onStderr?: (data: string) => void;
123
+ cwd?: string;
124
+ env?: Record<string, string>;
125
+ /** Written to standard input, which is then closed. */
126
+ stdin?: string;
127
+ /** Kill the command after this long (default 30 s; at most 60 s unless streamed). */
128
+ commandTimeoutMs?: number;
129
+ user?: "user" | "root";
130
+ }
131
+ /** A finished command. */
132
+ export interface SandboxExecOutput {
133
+ /** The exit status; `null` when a signal killed it. */
134
+ exitCode: number | null;
135
+ signal: string | null;
136
+ timedOut: boolean;
137
+ stdout: string;
138
+ stderr: string;
139
+ durationMs: number | null;
140
+ }
141
+ /** Files inside the sandbox (`/v1/sandboxes/{id}/files`, `files/stat`, `files/move`, `files/mkdir`). */
142
+ export interface SandboxFiles {
143
+ /** The file's bytes. */
144
+ read(path: string, options?: RequestOptions): Promise<Uint8Array>;
145
+ /** The file as UTF-8 text. */
146
+ readText(path: string, options?: RequestOptions): Promise<string>;
147
+ /** Write (replace) a file; `mkdir` creates missing parent directories. */
148
+ write(path: string, data: string | BinaryInput, options?: RequestOptions & {
149
+ mkdir?: boolean;
150
+ mode?: string;
151
+ }): Promise<T.FileEntry>;
152
+ /** The entries of a directory (or the one entry of a file). */
153
+ list(path: string, options?: RequestOptions): Promise<T.FileEntry[]>;
154
+ /** The entry at `path`, not following a final symlink; a missing path throws `file_not_found`. */
155
+ stat(path: string, options?: RequestOptions): Promise<T.FileEntry>;
156
+ /** Delete a file, a symlink or an empty directory; `recursive` deletes a directory and everything in it. */
157
+ delete(path: string, options?: RequestOptions & {
158
+ recursive?: boolean;
159
+ }): Promise<T.FileDeleted>;
160
+ /** Move or rename; an existing `to` throws `file_exists` unless `overwrite`. Answers the entry at `to`. */
161
+ move(from: string, to: string, options?: RequestOptions & {
162
+ overwrite?: boolean;
163
+ }): Promise<T.FileEntry>;
164
+ /** Create a directory and its missing parents (`mkdir -p`); an existing directory is answered as it is. */
165
+ mkdir(path: string, options?: RequestOptions & {
166
+ mode?: string;
167
+ }): Promise<T.FileEntry>;
168
+ }
169
+ /** Ports inside the sandbox, reached through preview URLs. */
170
+ export interface SandboxPorts {
171
+ /** A URL that reaches `port` (it must be declared in `ports` at create): signed, valid `ttlS` (default 600 s). */
172
+ url(port: number, options?: RequestOptions & {
173
+ ttlS?: number;
174
+ }): Promise<string>;
175
+ /**
176
+ * A styled screenshot of the app on `port` (render), through a fresh preview URL. `path` is appended to it;
177
+ * `body` takes every other screenshot field.
178
+ */
179
+ screenshot(port: number, body?: Omit<T.ScreenshotCreateRequest, "url" | "html"> & {
180
+ path?: string;
181
+ }, options?: RequestOptions): Promise<T.Screenshot>;
182
+ /** The page on `port` at `path`, scraped (vision), through a fresh preview URL. */
183
+ scrape(port: number, path?: string, body?: Omit<T.ScrapeRequest, "url">, options?: RequestOptions): Promise<T.ScrapeResult>;
184
+ }
185
+ /**
186
+ * A sandbox you hold: every operation of `b3os.sandboxes` bound to one id, plus `files`, `ports` and disposal. The
187
+ * API's sandbox object (what `createSandbox` and `getSandbox` return) is the type `SandboxInfo`, in `sbx.info`.
188
+ *
189
+ * await using sbx = await Sandbox.create(b3os, { template: "base" });
190
+ * await sbx.exec("echo hi", { onStdout: (d) => process.stdout.write(d) });
191
+ * // leaving the scope deletes the sandbox
192
+ *
193
+ * `create` is idempotent across retries (createSandboxSafely). Disposal deletes the sandbox and treats an
194
+ * already-gone one as deleted; call `detach()` first to keep it running past the scope.
195
+ */
196
+ export declare class Sandbox implements AsyncDisposable {
197
+ #private;
198
+ readonly id: string;
199
+ /** The sandbox as last seen (create, fork, refresh, pause, resume, extend): a `SandboxInfo`. */
200
+ info: T.Sandbox;
201
+ readonly files: SandboxFiles;
202
+ readonly ports: SandboxPorts;
203
+ protected constructor(client: SandboxClient, info: T.Sandbox);
204
+ /** Create a sandbox and return its handle (`POST /v1/sandboxes`, idempotent across retries). */
205
+ static create(client: SandboxClient, body?: SandboxCreateBody, options?: RequestOptions): Promise<Sandbox>;
206
+ /** A handle for an existing sandbox (`GET /v1/sandboxes/{id}`). */
207
+ static connect(client: SandboxClient, sandboxId: string, options?: RequestOptions): Promise<Sandbox>;
208
+ /**
209
+ * Run a command and wait for it. With `onStdout` or `onStderr` the output streams to them as it arrives;
210
+ * either way the result carries all of it. A string runs through `/bin/sh -c`, an array is executed directly.
211
+ */
212
+ exec(cmd: string | readonly string[], options?: SandboxExecOptions): Promise<SandboxExecOutput>;
213
+ /** Start a background process (a server, a worker); read its output with `logs`. */
214
+ background(cmd: string | readonly string[], options?: SandboxExecOptions): Promise<T.SandboxProcess>;
215
+ /** Start a service and wait until it is ready (`startService`). */
216
+ service(options: ServiceOptions): Promise<Service>;
217
+ /** Wait until a probe passes inside the sandbox (`waitUntilReady`). */
218
+ waitUntilReady(probe: ReadyProbe, options?: ReadyOptions): Promise<ReadyResult>;
219
+ /** Open a terminal (WebSocket PTY). */
220
+ pty(params?: T.SandboxesOpenPtyParams, options?: RequestOptions): Promise<PtySession>;
221
+ /** Snapshot the sandbox (it keeps running). */
222
+ snapshot(body?: T.SnapshotCreateRequest, options?: RequestOptions): Promise<T.Snapshot>;
223
+ /** Fork into `count` new sandboxes (the source must be `forkable`); one handle each. */
224
+ fork(count?: number, body?: Omit<T.ForkRequest, "count">, options?: RequestOptions): Promise<Sandbox[]>;
225
+ /** Keep the sandbox alive `seconds` from now (`POST …/timeout`). */
226
+ extend(seconds: number, options?: RequestOptions): Promise<T.Sandbox>;
227
+ pause(options?: RequestOptions): Promise<T.Sandbox>;
228
+ resume(options?: RequestOptions): Promise<T.Sandbox>;
229
+ /** Read the sandbox again (`GET`). */
230
+ refresh(options?: RequestOptions): Promise<T.Sandbox>;
231
+ /** Keep the sandbox running when the handle is disposed. */
232
+ detach(): this;
233
+ /** Delete the sandbox. Once only; a sandbox that is already gone counts as deleted. */
234
+ kill(options?: RequestOptions): Promise<void>;
235
+ /** `await using`: delete the sandbox when the scope ends, unless `detach()` was called. */
236
+ [Symbol.asyncDispose](): Promise<void>;
237
+ }