@midsummer-ai/sdk 0.1.0-staging.3428 → 0.1.0-staging.3434

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 (43) hide show
  1. package/auth-B7rCtXFQ.d.mts +36 -0
  2. package/{build-DQo-JrlN.d.mts → build-BhmOZmGQ.d.mts} +2 -27
  3. package/client.mjs +1 -1
  4. package/contract.mjs +2 -2
  5. package/database/build.d.mts +1 -1
  6. package/database/build.mjs +35 -2
  7. package/database/server.mjs +1 -1
  8. package/database/stand-in.d.mts +2 -1
  9. package/database/stand-in.mjs +2 -2
  10. package/email/client.d.mts +5 -4
  11. package/email/client.mjs +1 -1
  12. package/email/contract.d.mts +3 -1
  13. package/email/contract.mjs +111 -4
  14. package/email/index.d.mts +85 -16
  15. package/email/index.mjs +2 -2
  16. package/email/smtp.mjs +24 -8
  17. package/email/stand-in.d.mts +3 -0
  18. package/email/stand-in.mjs +17 -7
  19. package/email-IH5OllU2.mjs +337 -0
  20. package/errors-B1tO_wdm.mjs +28 -0
  21. package/flows/contract.d.mts +13 -0
  22. package/flows/contract.mjs +483 -0
  23. package/flows/index.d.mts +148 -0
  24. package/flows/index.mjs +347 -0
  25. package/index.mjs +2 -2
  26. package/{names-DfCBKupJ.mjs → names-Do0ot5Ha.mjs} +2 -28
  27. package/package.json +29 -1
  28. package/queues/contract.mjs +144 -1
  29. package/queues/index.d.mts +149 -5
  30. package/queues/index.mjs +56 -12
  31. package/queues/stand-in.mjs +333 -31
  32. package/queues/wire.mjs +302 -16
  33. package/s3.mjs +3 -2
  34. package/{server-EDRbF2u-.mjs → server-Drjni3-F.mjs} +4 -2
  35. package/socket.mjs +1 -1
  36. package/{src-BKrQAqJN.mjs → src-CkP59_Qu.mjs} +2 -1
  37. package/stand-in.mjs +2 -1
  38. package/storage/contract.d.mts +25 -0
  39. package/storage/contract.mjs +153 -0
  40. package/storage/files.d.mts +144 -0
  41. package/storage/files.mjs +513 -0
  42. package/wire.mjs +2 -1
  43. package/email-DXtpr7pT.mjs +0 -214
@@ -0,0 +1,483 @@
1
+ import { QueueError, queues } from "../queues/index.mjs";
2
+ import { StepFailed, flows } from "./index.mjs";
3
+ //#region src/flows/contract.ts
4
+ /**
5
+ * Flows' contract: what every Queues engine promises a Flow's runs, as cases any test runner can run.
6
+ * The Stand-in runs them in this package; the platform runs the same cases against its own engine,
7
+ * through the wire. A behaviour that is not a case here is not promised.
8
+ *
9
+ * for (const c of flowsContract) test(c.name, () => { const time = clockAt(T0); return c.run({ engine: makeEngine(time.now), advance: time.advance }) })
10
+ *
11
+ * Each case gets an engine with nothing in it whose clock starts at 12:00 UTC on 27 October 2026 and
12
+ * moves only with `advance`, and throws when the engine breaks the promise. Expected values are
13
+ * written out, never computed with the SDK's own code.
14
+ */
15
+ const SECOND = 1e3;
16
+ const T0 = Date.UTC(2026, 9, 27, 12);
17
+ const flowsContract = [
18
+ {
19
+ name: "a run's steps run once each, in order, and its output is kept",
20
+ async run({ engine }) {
21
+ const calls = [];
22
+ const order = flows(engine).flow("orders", async (input, step) => {
23
+ const total = await step.run("price it", () => (calls.push("price"), input.id * 10));
24
+ await step.run("charge it", () => {
25
+ calls.push("charge");
26
+ });
27
+ return { total };
28
+ });
29
+ equal(await order.start({ id: 4 }, { id: "order-4" }), {
30
+ id: "order-4",
31
+ started: true
32
+ }, "the start");
33
+ equal(await order.work(), 1, "deliveries the first work took");
34
+ equal(await order.work(), 0, "deliveries once the run has ended");
35
+ equal(calls, ["price", "charge"], "the steps' work");
36
+ const run = await order.show("order-4");
37
+ equal([
38
+ run?.status,
39
+ run?.input,
40
+ run?.output,
41
+ run?.endedAt
42
+ ], [
43
+ "completed",
44
+ { id: 4 },
45
+ { total: 40 },
46
+ T0
47
+ ], "the run");
48
+ equal(run?.steps.map((s) => [
49
+ s.id,
50
+ s.kind,
51
+ s.state,
52
+ "value" in s ? s.value : "none"
53
+ ]), [[
54
+ "price it",
55
+ "run",
56
+ "done",
57
+ 40
58
+ ], [
59
+ "charge it",
60
+ "run",
61
+ "done",
62
+ null
63
+ ]], "its steps");
64
+ }
65
+ },
66
+ {
67
+ name: "a step that throws runs again after 10 seconds, then 20, and the steps before it never run again",
68
+ async run({ engine, advance }) {
69
+ const calls = [];
70
+ let failures = 2;
71
+ const sync = flows(engine).flow("sync", async (_input, step) => {
72
+ await step.run("read", () => {
73
+ calls.push("read");
74
+ });
75
+ await step.run("write", () => {
76
+ calls.push("write");
77
+ if (failures-- > 0) throw new Error("the service said 503");
78
+ });
79
+ return "synced";
80
+ });
81
+ await sync.start(null, { id: "s1" });
82
+ await sync.work();
83
+ const retrying = await sync.show("s1");
84
+ equal([
85
+ retrying?.status,
86
+ retrying?.nextAt,
87
+ retrying?.steps[1]?.state,
88
+ retrying?.steps[1]?.error,
89
+ retrying?.steps[1]?.failures
90
+ ], [
91
+ "running",
92
+ T0 + 10 * SECOND,
93
+ "retrying",
94
+ "the service said 503",
95
+ 1
96
+ ], "the run after the first failure");
97
+ advance(9 * SECOND);
98
+ equal(await sync.work(), 0, "deliveries 9 seconds after");
99
+ advance(1 * SECOND);
100
+ await sync.work();
101
+ advance(19 * SECOND);
102
+ equal(await sync.work(), 0, "deliveries 19 seconds after the second failure");
103
+ advance(1 * SECOND);
104
+ await sync.work();
105
+ equal(calls, [
106
+ "read",
107
+ "write",
108
+ "write",
109
+ "write"
110
+ ], "the steps' work");
111
+ equal((await sync.show("s1"))?.output, "synced", "the output");
112
+ }
113
+ },
114
+ {
115
+ name: "a step that fails past its retries throws StepFailed, which the run can catch and go on",
116
+ async run({ engine }) {
117
+ const payment = flows(engine).flow("payments", async (_input, step) => {
118
+ await step.run("reserve stock", () => "reserved");
119
+ try {
120
+ await step.run("charge the card", () => {
121
+ throw new Error("card declined");
122
+ }, { retries: 0 });
123
+ } catch (error) {
124
+ if (!(error instanceof StepFailed)) throw error;
125
+ await step.run("release stock", () => "released");
126
+ return {
127
+ charged: false,
128
+ why: error.message
129
+ };
130
+ }
131
+ return { charged: true };
132
+ });
133
+ await payment.start(null, { id: "p1" });
134
+ await payment.work();
135
+ const run = await payment.show("p1");
136
+ equal([run?.status, run?.output], ["completed", {
137
+ charged: false,
138
+ why: "card declined"
139
+ }], "the run");
140
+ equal(run?.steps.map((s) => [s.id, s.state]), [
141
+ ["reserve stock", "done"],
142
+ ["charge the card", "failed"],
143
+ ["release stock", "done"]
144
+ ], "its steps");
145
+ }
146
+ },
147
+ {
148
+ name: "a StepFailed the run doesn't catch fails the run, saying which step",
149
+ async run({ engine }) {
150
+ const report = flows(engine).flow("reports", async (_input, step) => {
151
+ await step.run("render", () => {
152
+ throw new Error("template missing");
153
+ }, { retries: 0 });
154
+ });
155
+ await report.start(null, { id: "r1" });
156
+ await report.work();
157
+ const run = await report.show("r1");
158
+ equal([run?.status, run?.error], ["failed", "Step \"render\" failed: template missing"], "the run");
159
+ equal(await report.work(), 0, "deliveries after it failed");
160
+ }
161
+ },
162
+ {
163
+ name: "a sleep parks the run until it wakes, and the run is not delivered meanwhile",
164
+ async run({ engine, advance }) {
165
+ let after = 0;
166
+ const trial = flows(engine).flow("trials", async (_input, step) => {
167
+ await step.sleep("wait three days", 259200);
168
+ await step.run("end the trial", () => {
169
+ after += 1;
170
+ });
171
+ });
172
+ await trial.start(null, { id: "t1" });
173
+ await trial.work();
174
+ const sleeping = await trial.show("t1");
175
+ equal([
176
+ sleeping?.status,
177
+ sleeping?.nextAt,
178
+ sleeping?.steps[0]?.state
179
+ ], [
180
+ "sleeping",
181
+ T0 + 259200 * SECOND,
182
+ "sleeping"
183
+ ], "the run while it sleeps");
184
+ equal(await queues(engine).queue("trials").stats(), {
185
+ depth: 1,
186
+ ready: 0,
187
+ inFlight: 0,
188
+ delayed: 1,
189
+ oldestSentAt: T0,
190
+ dead: 0
191
+ }, "the queue while it sleeps");
192
+ advance(259200 * SECOND - SECOND);
193
+ equal(await trial.work(), 0, "deliveries a second before it wakes");
194
+ advance(SECOND);
195
+ await trial.work();
196
+ equal([(await trial.show("t1"))?.status, after], ["completed", 1], "the run once it woke");
197
+ }
198
+ },
199
+ {
200
+ name: "a wait takes the signal sent while it waits, and the run goes on at once",
201
+ async run({ engine }) {
202
+ const approval = flows(engine).flow("approvals", async (input, step) => {
203
+ const answer = await step.waitFor("manager approval", {
204
+ event: "approved",
205
+ timeoutSeconds: 604800
206
+ });
207
+ return answer ? {
208
+ amount: input.amount,
209
+ by: answer.by
210
+ } : null;
211
+ });
212
+ await approval.start({ amount: 900 }, { id: "a1" });
213
+ await approval.work();
214
+ const waiting = await approval.show("a1");
215
+ equal([
216
+ waiting?.status,
217
+ waiting?.nextAt,
218
+ waiting?.steps[0]?.event
219
+ ], [
220
+ "waiting",
221
+ T0 + 604800 * SECOND,
222
+ "approved"
223
+ ], "the run while it waits");
224
+ equal(await approval.signal("a1", "approved", {
225
+ approved: true,
226
+ by: "grace"
227
+ }), "delivered", "the signal");
228
+ equal(await approval.work(), 1, "deliveries right after the signal");
229
+ equal((await approval.show("a1"))?.output, {
230
+ amount: 900,
231
+ by: "grace"
232
+ }, "the output");
233
+ }
234
+ },
235
+ {
236
+ name: "a signal sent before the run waits for it is kept for that wait, and a signal sent again with its key keeps nothing more",
237
+ async run({ engine }) {
238
+ const seen = [];
239
+ const shipping = flows(engine).flow("shipping", async (_input, step) => {
240
+ seen.push(await step.waitFor("label printed", { timeoutSeconds: 3600 }));
241
+ seen.push(await step.waitFor("label printed#again", {
242
+ event: "label printed",
243
+ timeoutSeconds: 3600
244
+ }));
245
+ return null;
246
+ });
247
+ await shipping.start(null, { id: "o1" });
248
+ equal(await shipping.signal("o1", "label printed", { label: "A" }, { key: "printer-1" }), "kept", "the first signal");
249
+ equal(await shipping.signal("o1", "label printed", { label: "A" }, { key: "printer-1" }), "kept", "the same signal again");
250
+ await shipping.work();
251
+ equal((await shipping.show("o1"))?.status, "waiting", "the run once the kept signal was taken");
252
+ equal(await shipping.signal("o1", "label printed", { label: "B" }), "delivered", "a second signal");
253
+ await shipping.work();
254
+ equal(seen.filter((value) => value !== void 0), [
255
+ { label: "A" },
256
+ { label: "A" },
257
+ { label: "B" }
258
+ ], "what each delivery's waits returned, recorded values returned again");
259
+ }
260
+ },
261
+ {
262
+ name: "a wait that times out returns undefined",
263
+ async run({ engine, advance }) {
264
+ const reminder = flows(engine).flow("reminders", async (_input, step) => {
265
+ return await step.waitFor("reply", { timeoutSeconds: 3600 }) === void 0 ? "nudged" : "answered";
266
+ });
267
+ await reminder.start(null, { id: "m1" });
268
+ await reminder.work();
269
+ advance(3600 * SECOND);
270
+ await reminder.work();
271
+ const run = await reminder.show("m1");
272
+ equal([
273
+ run?.status,
274
+ run?.output,
275
+ run?.steps[0]?.state,
276
+ run?.steps[0] && "value" in run.steps[0] ? run.steps[0].value : "none"
277
+ ], [
278
+ "completed",
279
+ "nudged",
280
+ "done",
281
+ "none"
282
+ ], "the run");
283
+ }
284
+ },
285
+ {
286
+ name: "a cancelled run records nothing more: it is not delivered again, and a signal finds it ended",
287
+ async run({ engine, advance }) {
288
+ const onboarding = flows(engine).flow("onboarding", async (_input, step) => {
289
+ await step.waitFor("verified", { timeoutSeconds: 86400 });
290
+ });
291
+ await onboarding.start(null, { id: "u1" });
292
+ await onboarding.work();
293
+ equal((await onboarding.cancel("u1")).status, "cancelled", "the cancelled run");
294
+ equal(await onboarding.signal("u1", "verified"), "ended", "a signal after it");
295
+ advance(86400 * SECOND);
296
+ equal(await onboarding.work(), 0, "deliveries after the wait would have timed out");
297
+ equal((await queues(engine).queue("onboarding").stats()).depth, 0, "the queue's depth");
298
+ }
299
+ },
300
+ {
301
+ name: "a run started again with its id starts nothing, and two starts without one are two runs",
302
+ async run({ engine }) {
303
+ let runs = 0;
304
+ const welcome = flows(engine).flow("welcome", async (_input, step) => {
305
+ await step.run("send", () => {
306
+ runs += 1;
307
+ });
308
+ });
309
+ equal(await welcome.start({ to: "ada" }, { id: "welcome-ada" }), {
310
+ id: "welcome-ada",
311
+ started: true
312
+ }, "the first start");
313
+ equal(await welcome.start({ to: "ada" }, { id: "welcome-ada" }), {
314
+ id: "welcome-ada",
315
+ started: false
316
+ }, "the start again");
317
+ const [one, two] = [await welcome.start({ to: "grace" }), await welcome.start({ to: "grace" })];
318
+ equal([
319
+ one.id !== two.id,
320
+ one.started,
321
+ two.started
322
+ ], [
323
+ true,
324
+ true,
325
+ true
326
+ ], "two starts without an id");
327
+ await welcome.work();
328
+ equal(runs, 3, "runs that ran");
329
+ }
330
+ },
331
+ {
332
+ name: "a run past its timeout fails, also while it sleeps",
333
+ async run({ engine, advance }) {
334
+ const slow = flows(engine).flow("imports", async (_input, step) => {
335
+ await step.sleep("wait for the export", 86400);
336
+ });
337
+ await slow.start(null, {
338
+ id: "i1",
339
+ timeoutSeconds: 60
340
+ });
341
+ await slow.work();
342
+ equal((await slow.show("i1"))?.nextAt, T0 + 60 * SECOND, "when it is next delivered: at its deadline, not its wake time");
343
+ advance(60 * SECOND);
344
+ await slow.work();
345
+ const run = await slow.show("i1");
346
+ equal([run?.status, run?.error], ["failed", "It ran past its timeout of 60 seconds."], "the run");
347
+ }
348
+ },
349
+ {
350
+ name: "runs of one group run one at a time in the order started, a run holding its place while it sleeps",
351
+ async run({ engine, advance }) {
352
+ const order = [];
353
+ const account = flows(engine).flow("accounts", async (input, step) => {
354
+ await step.run("start", () => {
355
+ order.push(`${input} start`);
356
+ });
357
+ await step.sleep("settle", 60);
358
+ await step.run("end", () => {
359
+ order.push(`${input} end`);
360
+ });
361
+ });
362
+ await account.start("first", {
363
+ id: "a",
364
+ group: "acct-1"
365
+ });
366
+ await account.start("second", {
367
+ id: "b",
368
+ group: "acct-1"
369
+ });
370
+ await account.start("other", {
371
+ id: "c",
372
+ group: "acct-2"
373
+ });
374
+ await account.work();
375
+ equal([...order].sort(), ["first start", "other start"], "what the first work ran");
376
+ advance(60 * SECOND);
377
+ await account.work();
378
+ await account.work();
379
+ equal([...order].sort(), [
380
+ "first end",
381
+ "first start",
382
+ "other end",
383
+ "other start",
384
+ "second start"
385
+ ], "what ran once they woke");
386
+ equal(order.indexOf("second start") > order.indexOf("first end"), true, "the second run started after the first ended");
387
+ }
388
+ },
389
+ {
390
+ name: "a step's name used twice in one run is two steps",
391
+ async run({ engine }) {
392
+ const digest = flows(engine).flow("digests", async (input, step) => {
393
+ for (const to of input) await step.run("send", () => to);
394
+ });
395
+ await digest.start(["ada", "grace"], { id: "d1" });
396
+ await digest.work();
397
+ equal((await digest.show("d1"))?.steps.map((s) => [s.id, s.value]), [["send", "ada"], ["send#2", "grace"]], "its steps");
398
+ }
399
+ },
400
+ {
401
+ name: "a delivery whose window ended runs nothing, and the run's next delivery goes on",
402
+ async run({ engine, advance }) {
403
+ let runs = 0;
404
+ const job = flows(engine).flow("jobs", async (_input, step) => {
405
+ await step.run("work", () => {
406
+ runs += 1;
407
+ });
408
+ });
409
+ await job.start(null, { id: "j1" });
410
+ const [stale] = await queues(engine).queue("jobs").receive({ visibilitySeconds: 1 });
411
+ advance(SECOND);
412
+ equal(await job.handle({
413
+ run: "j1",
414
+ receipt: stale.receipt
415
+ }), { kind: "stale" }, "the ended delivery");
416
+ equal(runs, 0, "work the ended delivery did");
417
+ await job.work();
418
+ equal([runs, (await job.show("j1"))?.status], [1, "completed"], "after the next delivery");
419
+ }
420
+ },
421
+ {
422
+ name: "code that throws outside a step fails the delivery, and after maxAttempts in a row the run fails",
423
+ async run({ engine, advance }) {
424
+ const broken = flows(engine).flow("broken", async () => {
425
+ throw new Error("not deployed yet");
426
+ });
427
+ await broken.start(null, { id: "b1" });
428
+ await broken.work({ maxAttempts: 2 });
429
+ equal((await broken.show("b1"))?.status, "running", "the run after one failed delivery");
430
+ advance(10 * SECOND);
431
+ await broken.work({ maxAttempts: 2 });
432
+ const run = await broken.show("b1");
433
+ equal([run?.status, run?.error], ["failed", "Its delivery failed 2 times without progress."], "the run");
434
+ equal((await queues(engine).queue("broken").stats()).dead, 1, "the dead-letter queue");
435
+ }
436
+ },
437
+ {
438
+ name: "signalling or cancelling a run there is none of is not-found, and bad names, ids and times are invalid",
439
+ async run({ engine }) {
440
+ const some = flows(engine);
441
+ const job = some.flow("jobs", async (_input, step) => {
442
+ await step.run("x", () => null);
443
+ });
444
+ await rejects(job.signal("nope", "go"), "not-found", "a signal to no run");
445
+ await rejects(job.cancel("nope"), "not-found", "cancelling no run");
446
+ equal(await job.show("nope"), void 0, "showing no run");
447
+ fails(() => some.flow("Bad Name", async () => null), "a Flow's name with spaces");
448
+ await rejects(job.start(null, { id: "has space" }), "invalid", "a run id with a space");
449
+ await rejects(job.start(null, { timeoutSeconds: 31536001 }), "invalid", "a timeout over 365 days");
450
+ await rejects(job.start(void 0), "invalid", "an input that is not JSON");
451
+ await rejects(job.signal("x", "has\na line break"), "invalid", "an event with a line break");
452
+ }
453
+ }
454
+ ];
455
+ function equal(actual, expected, what) {
456
+ const a = JSON.stringify(sorted(actual)), e = JSON.stringify(sorted(expected));
457
+ if (a !== e) throw new Error(`${what}: expected ${e}, got ${a}`);
458
+ }
459
+ function sorted(value) {
460
+ if (Array.isArray(value)) return value.map(sorted);
461
+ if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : 1).map(([k, v]) => [k, sorted(v)]));
462
+ return value;
463
+ }
464
+ function fails(run, what) {
465
+ try {
466
+ run();
467
+ } catch (error) {
468
+ if (error instanceof QueueError && error.code === "invalid") return;
469
+ throw error;
470
+ }
471
+ throw new Error(`${what}: expected invalid`);
472
+ }
473
+ async function rejects(promise, code, what) {
474
+ try {
475
+ await promise;
476
+ } catch (error) {
477
+ if (error instanceof QueueError && error.code === code) return;
478
+ throw error;
479
+ }
480
+ throw new Error(`${what}: expected ${code}`);
481
+ }
482
+ //#endregion
483
+ export { flowsContract };
@@ -0,0 +1,148 @@
1
+ import { Json, QueueEngine, RunStatus } from "../queues/index.mjs";
2
+ //#region src/flows/index.d.ts
3
+ /** What a step that failed for good throws into its Flow's function: `step` is its name, `message` the last error. */
4
+ declare class StepFailed extends Error {
5
+ readonly step: string;
6
+ constructor(step: string, why: string);
7
+ }
8
+ /** What a sleep, a wait or a step's retry throws to end a delivery. Never catch it: the run goes on at its next delivery. */
9
+ declare class FlowPause extends Error {
10
+ constructor();
11
+ }
12
+ /** The steps a Flow's function takes, each recorded with its run. */
13
+ interface Step {
14
+ /**
15
+ * Runs `work` once for the run and returns what it returned, a JSON value of at most 128 KiB
16
+ * (undefined is kept as null). Thrown, it is tried again after the backoff, `retries` times (0 to
17
+ * 100), then throws `StepFailed`.
18
+ */
19
+ run<T extends Json | void>(name: string, work: () => T | Promise<T>, options?: {
20
+ readonly retries?: number;
21
+ }): Promise<T>;
22
+ /** Waits `seconds` (up to 365 days) before the run goes on. */
23
+ sleep(name: string, seconds: number): Promise<void>;
24
+ /** Waits until `at` (a time in milliseconds since the epoch, or a Date, within 365 days) before the run goes on. */
25
+ sleepUntil(name: string, at: number | Date): Promise<void>;
26
+ /**
27
+ * Waits for the signal `event` (the step's name unless given) for at most `timeoutSeconds` (1 to
28
+ * 365 days), and returns the value it was sent with, or undefined when the wait timed out.
29
+ */
30
+ waitFor<T extends Json = Json>(name: string, options: {
31
+ readonly timeoutSeconds: number;
32
+ readonly event?: string;
33
+ }): Promise<T | undefined>;
34
+ }
35
+ /** One recorded step of a run, as `show` returns it. */
36
+ interface RunStep {
37
+ /** Its name, with `#2`, `#3` … after it for its second and later uses in one run. */
38
+ readonly id: string;
39
+ readonly kind: "run" | "sleep" | "wait";
40
+ readonly state: "done" | "failed" | "retrying" | "sleeping" | "waiting";
41
+ readonly value?: Json;
42
+ readonly error?: string;
43
+ readonly failures: number;
44
+ readonly until?: number;
45
+ readonly event?: string;
46
+ readonly at: number;
47
+ }
48
+ /** A run of a Flow, with what it was started with, what it returned and its steps. */
49
+ interface Run<Input = Json, Output = Json> {
50
+ readonly id: string;
51
+ readonly status: RunStatus;
52
+ readonly input: Input;
53
+ readonly output?: Output;
54
+ readonly error?: string;
55
+ readonly startedAt: number;
56
+ readonly endedAt?: number;
57
+ /** When a sleeping or waiting run wakes, or a retrying step runs again. */
58
+ readonly nextAt?: number;
59
+ /** When it fails if it hasn't ended. */
60
+ readonly deadline?: number;
61
+ readonly group?: string;
62
+ readonly steps: ReadonlyArray<RunStep>;
63
+ }
64
+ interface StartOptions {
65
+ /** The run's id, 1 to 128 printable characters: starting again with the same id starts nothing. A fresh one when not given. */
66
+ readonly id?: string;
67
+ /** It fails if it hasn't ended within this many seconds: 1 to 31,536,000 (365 days). */
68
+ readonly timeoutSeconds?: number;
69
+ /** Runs of one group run one at a time, in the order started, a run holding its place while it sleeps or waits. */
70
+ readonly group?: string;
71
+ }
72
+ /** How one delivery of a run went: ended (or there was nothing to run), paused until its next delivery, stale (its receipt had ended), or failed and to be tried again. */
73
+ type Outcome = {
74
+ readonly kind: "ended";
75
+ } | {
76
+ readonly kind: "paused";
77
+ } | {
78
+ readonly kind: "stale";
79
+ } | {
80
+ readonly kind: "retry";
81
+ readonly why: string;
82
+ };
83
+ interface Flow<Input = Json, Output = Json> {
84
+ readonly name: string;
85
+ /** Starts a run with `input`, a JSON value of at most 128 KiB. `started` is false when a run with that id was started before. */
86
+ start(input: Input, options?: StartOptions): Promise<{
87
+ readonly id: string;
88
+ readonly started: boolean;
89
+ }>;
90
+ /**
91
+ * Sends run `id` the signal `event` with `value`: `delivered` when the run was waiting for it and
92
+ * goes on now, `kept` when it is kept for the run's next wait for it, `ended` when the run has
93
+ * ended. A `key` sent before keeps nothing more. Throws `not-found` when there is no such run.
94
+ */
95
+ signal(id: string, event: string, value?: Json, options?: {
96
+ readonly key?: string;
97
+ }): Promise<"delivered" | "kept" | "ended">;
98
+ /** Cancels run `id`: it records nothing more. Throws `not-found` when there is no such run. */
99
+ cancel(id: string): Promise<Run<Input, Output>>;
100
+ /** Run `id`, or undefined when there is none. */
101
+ show(id: string): Promise<Run<Input, Output> | undefined>;
102
+ /**
103
+ * Pulls up to `limit` runs due now (1 to 100, 10 by default) from the Flow's queue and runs each one's
104
+ * delivery, all at once; returns how many it took. Each is hidden `visibilitySeconds` (30 by default)
105
+ * while it runs, and fails after `maxAttempts` deliveries in a row without progress (5 by default).
106
+ */
107
+ work(options?: {
108
+ readonly limit?: number;
109
+ readonly visibilitySeconds?: number;
110
+ readonly maxAttempts?: number;
111
+ }): Promise<number>;
112
+ /** Runs one delivery of a run: what `serveFlows` and `work` call. */
113
+ handle(delivery: {
114
+ readonly run: string;
115
+ readonly receipt: string;
116
+ }): Promise<Outcome>;
117
+ }
118
+ interface FlowOptions {
119
+ /** How many times a step is tried again before it fails for good, unless the step says: 0 to 100, 4 by default. */
120
+ readonly retries?: number;
121
+ }
122
+ interface RunnerOptions {
123
+ /** The clock a delivery's time budget is measured with; `Date.now` when not given. */
124
+ readonly clock?: () => number;
125
+ /**
126
+ * How long one delivery runs steps before it hands the run on to its next delivery, in milliseconds:
127
+ * 20 seconds by default, inside the 30 a pushed delivery is given.
128
+ */
129
+ readonly budgetMs?: number;
130
+ }
131
+ /** Flows over `engine`: the Stand-in's in tests (`flows(standIn({ clock }))`), or `hereEngine()`. */
132
+ declare function flows(engine: QueueEngine, runner?: RunnerOptions): {
133
+ flow<Input extends Json = Json, Output extends Json | void = Json>(name: string, fn: (input: Input, step: Step) => Output | Promise<Output>, options?: FlowOptions): Flow<Input, Output>;
134
+ };
135
+ /**
136
+ * The Flow `name`, on the queue the environment binds under that name, from code in a deployment in
137
+ * any framework, or `midsummer dev`'s queue of that name.
138
+ */
139
+ declare function flow<Input extends Json = Json, Output extends Json | void = Json>(name: string, fn: (input: Input, step: Step) => Output | Promise<Output>, options?: FlowOptions): Flow<Input, Output>;
140
+ /**
141
+ * A request handler for the path a Flow's queue pushes its runs to (`midsummer.json`'s `consumers`):
142
+ * it takes only deliveries Midsummer checked (their `Midsummer-Queue` header is the queue's
143
+ * binding name), runs the run's Flow, and returns 204 once the delivery is done or paused, or 500 to have it
144
+ * tried again. One path can serve several Flows.
145
+ */
146
+ declare function serveFlows(list: ReadonlyArray<Pick<Flow<never, never>, "name" | "handle">>): (request: Request) => Promise<Response>;
147
+ //#endregion
148
+ export { Flow, FlowOptions, FlowPause, type Json, Outcome, Run, type RunStatus, RunStep, RunnerOptions, StartOptions, Step, StepFailed, flow, flows, serveFlows };