@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
@@ -38,11 +38,20 @@
38
38
  * returns the first Message's id and sends nothing. Schedules send with one, so a Schedule's minute
39
39
  * sends once however often it is reached.
40
40
  *
41
+ * Messages sent with the same `group` are delivered one at a time, in the order sent: the next is
42
+ * delivered only once the one before it is acked or moved to the dead-letter Queue, and a retried one
43
+ * keeps its place, so order-dependent work (a customer's payments, one document's edits) never runs
44
+ * out of order. Each group goes on apart from the others. A Queue can let a group have more at once,
45
+ * and limit deliveries a period (`midsummer queue edit`); its limits hold Messages back, never drop them.
46
+ *
47
+ * A receiver whose work takes longer than its visibility window extends it with `extend`, as often as
48
+ * it needs, before the window ends.
49
+ *
41
50
  * Names and bodies are checked here, before anything leaves the caller. Every failure is a
42
51
  * `QueueError`, whose `code` is the one the wire carries.
43
52
  */
44
- /** Why a Queues call failed: as Storage's codes say (`invalid`, `forbidden`, `quota`, `unavailable` …). */
45
- type QueueErrorCode = "invalid" | "forbidden" | "quota" | "unavailable";
53
+ /** Why a Queues call failed: as Storage's codes say (`invalid`, `forbidden`, `quota`, `unavailable` …), and `not-found` for a Flow run there is none of. */
54
+ type QueueErrorCode = "invalid" | "forbidden" | "quota" | "unavailable" | "not-found";
46
55
  declare const queueErrorCodes: ReadonlyArray<QueueErrorCode>;
47
56
  declare class QueueError extends Error {
48
57
  readonly code: QueueErrorCode;
@@ -64,12 +73,16 @@ interface Delivery<Body = Json> {
64
73
  readonly receipt: string;
65
74
  /** When the visibility window ends and the Message may be delivered again. */
66
75
  readonly visibleUntil: number;
76
+ /** The Flow run this Message carries (`@midsummer-ai/sdk/flows`); a Flow's `work` runs it. */
77
+ readonly run?: string;
67
78
  }
68
79
  interface SendOptions {
69
- /** Seconds before it can be received: 0 to 43,200 (12 hours), 0 by default. */
80
+ /** Seconds before it can be received: 0 to 31,536,000 (365 days), 0 by default. */
70
81
  readonly delaySeconds?: number;
71
82
  /** Makes the send once: 1 to 128 printable characters. */
72
83
  readonly key?: string;
84
+ /** Delivers it after the group's earlier Messages, one at a time: 1 to 128 printable characters, such as `customer:42`. */
85
+ readonly group?: string;
73
86
  }
74
87
  interface ReceiveOptions {
75
88
  /** At most this many: 1 to 100, 10 by default. */
@@ -127,6 +140,14 @@ interface Queue {
127
140
  retry(receipt: string, options?: {
128
141
  readonly delaySeconds?: number;
129
142
  }): Promise<void>;
143
+ /**
144
+ * Keeps a delivery hidden `visibilitySeconds` (1 to 43,200, 30 by default) from now, for work that
145
+ * takes longer than its window. Returns when the window now ends, or undefined when the receipt has
146
+ * already ended, and the work should stop: the Message may be delivered again.
147
+ */
148
+ extend(receipt: string, options?: {
149
+ readonly visibilitySeconds?: number;
150
+ }): Promise<number | undefined>;
130
151
  stats(): Promise<QueueStats>;
131
152
  /** Where this Queue's Messages go once they have been delivered `maxAttempts` times. Its own Messages never move on. */
132
153
  readonly deadLetter: Queue;
@@ -140,7 +161,64 @@ interface EngineMessage {
140
161
  readonly body: string;
141
162
  readonly delayMs: number;
142
163
  readonly key?: string;
164
+ readonly group?: string;
165
+ }
166
+ /** Where a Flow run is: delivered or ready to be (`running`), parked until it wakes, or ended. */
167
+ type RunStatus = "running" | "sleeping" | "waiting" | "completed" | "failed" | "cancelled";
168
+ /** One recorded step of a run, as an engine keeps it: its value as JSON text. A wait that timed out has none. */
169
+ interface EngineStep {
170
+ /** The step's name, with `#2`, `#3` … after it for its second and later uses in one run. */
171
+ readonly id: string;
172
+ readonly kind: "run" | "sleep" | "wait";
173
+ readonly state: "done" | "failed" | "retrying" | "sleeping" | "waiting";
174
+ readonly value?: string;
175
+ readonly error?: string;
176
+ /** How many times its work threw. */
177
+ readonly failures: number;
178
+ /** When a sleeping or waiting step wakes, or a retrying one runs again. */
179
+ readonly until?: number;
180
+ /** The signal a wait waits for. */
181
+ readonly event?: string;
182
+ readonly at: number;
183
+ }
184
+ /** A Flow run as an engine keeps it: input and output as JSON text, and its steps in the order first recorded. */
185
+ interface EngineRun {
186
+ readonly id: string;
187
+ readonly status: RunStatus;
188
+ readonly input: string;
189
+ readonly startedAt: number;
190
+ readonly endedAt?: number;
191
+ readonly nextAt?: number;
192
+ readonly output?: string;
193
+ readonly error?: string;
194
+ readonly deadline?: number;
195
+ readonly group?: string;
196
+ readonly steps: ReadonlyArray<EngineStep>;
143
197
  }
198
+ /**
199
+ * What a delivery of a run records about one step: a value its work returned, or the error it threw
200
+ * with how many retries the step allows; a sleep of `ms`; or a wait for the signal `event` of at most
201
+ * `timeoutMs`.
202
+ */
203
+ type StepWrite = {
204
+ readonly kind: "run";
205
+ readonly id: string;
206
+ readonly value: string;
207
+ } | {
208
+ readonly kind: "run";
209
+ readonly id: string;
210
+ readonly error: string;
211
+ readonly retries: number;
212
+ } | {
213
+ readonly kind: "sleep";
214
+ readonly id: string;
215
+ readonly ms: number;
216
+ } | {
217
+ readonly kind: "wait";
218
+ readonly id: string;
219
+ readonly event: string;
220
+ readonly timeoutMs: number;
221
+ };
144
222
  /** A delivery as an engine returns it: its body as JSON text. */
145
223
  interface EngineDelivery extends Omit<Delivery, "body"> {
146
224
  readonly body: string;
@@ -161,7 +239,57 @@ interface QueueEngine {
161
239
  ack(queue: string, receipt: string): Promise<void>;
162
240
  /** With no `delayMs`, the backoff after the delivery's attempt (`backoffMs`). */
163
241
  retry(queue: string, receipt: string, delayMs?: number): Promise<void>;
242
+ /** Moves a current delivery's window to end `visibilityMs` from now and returns that time; undefined once the receipt has ended. */
243
+ extend(queue: string, receipt: string, visibilityMs: number): Promise<number | undefined>;
164
244
  stats(queue: string): Promise<QueueStats>;
245
+ /**
246
+ * Starts a Flow run with its input, as one Message of the Queue that carries it: within `timeoutMs`
247
+ * when given, in `group` when given. An id used before starts nothing and returns `started: false`.
248
+ */
249
+ start(queue: string, run: {
250
+ readonly id: string;
251
+ readonly input: string;
252
+ readonly timeoutMs?: number;
253
+ readonly group?: string;
254
+ }): Promise<{
255
+ readonly id: string;
256
+ readonly started: boolean;
257
+ }>;
258
+ /**
259
+ * The run a current delivery carries, with its steps and the engine's time, for its code to run
260
+ * again; undefined once the receipt has ended. A run that has ended, or passed its deadline (failing
261
+ * it), comes back ended: its code doesn't run.
262
+ */
263
+ load(queue: string, runId: string, receipt: string): Promise<{
264
+ readonly now: number;
265
+ readonly run: EngineRun;
266
+ } | undefined>;
267
+ /**
268
+ * Records one step for a current delivery and returns it as now kept: the first value recorded
269
+ * wins, so of two deliveries the second takes the first's. A step that sleeps, waits or retries
270
+ * ends the delivery, its Message delivered again when it wakes. Undefined once the delivery or the
271
+ * run has ended: the code stops.
272
+ */
273
+ record(queue: string, runId: string, receipt: string, step: StepWrite): Promise<EngineStep | undefined>;
274
+ /** Ends a run with what its code returned, or why it failed; a delivery that has ended, or a run that has, changes nothing. */
275
+ finish(queue: string, runId: string, receipt: string, end: {
276
+ readonly output: string;
277
+ } | {
278
+ readonly error: string;
279
+ }): Promise<void>;
280
+ /**
281
+ * Sends a run a signal: `delivered` when the run was waiting for `event` and wakes now, `kept` when
282
+ * it is kept for the run's next wait for it, `ended` when the run has ended, `missing` when there is
283
+ * no such run. A `key` sent before keeps nothing more.
284
+ */
285
+ signal(queue: string, runId: string, signal: {
286
+ readonly event: string;
287
+ readonly value: string;
288
+ readonly key?: string;
289
+ }): Promise<"delivered" | "kept" | "ended" | "missing">;
290
+ /** Cancels a run and returns it; undefined when there is none. */
291
+ cancel(queue: string, runId: string): Promise<EngineRun | undefined>;
292
+ run(queue: string, runId: string): Promise<EngineRun | undefined>;
165
293
  }
166
294
  /** The suffix that marks a Queue's dead-letter Queue; no Queue's own name has a dot. */
167
295
  declare const DEAD = ".dead";
@@ -174,23 +302,39 @@ declare function checkQueueName(name: string): void;
174
302
  /** A name as an engine is given it: a Queue's, or its dead-letter Queue's. */
175
303
  declare function checkEngineQueue(queue: string): void;
176
304
  declare function checkKey(key: string): void;
305
+ /** A group or a run's id: 1 to 128 printable characters, without spaces. */
306
+ declare function checkToken(value: string, what: string): void;
307
+ /** A signal's event: as a step's name is, since a wait's event is its step's name unless it says. */
308
+ declare function checkEvent(event: string): void;
309
+ /** A step's name: 1 to 200 characters, without line breaks or other control characters. */
310
+ declare function checkStepName(name: string): void;
311
+ /** The longest a delay, a sleep, a wait or a run's timeout may be: 365 days. */
312
+ declare const MAX_WAIT_MS: number;
177
313
  /** Bodies are at most this many bytes of JSON. */
178
314
  declare const MAX_BODY: number;
179
315
  /** The JSON text of `body`, which must be a JSON value of at most 128 KiB. */
180
316
  declare function bodyText(body: unknown): string;
181
317
  declare function checkReceipt(receipt: string): void;
182
318
  declare function checkDelaySeconds(seconds: number): void;
319
+ /** A visibility window in milliseconds: whole seconds, 1 to 43,200. */
320
+ declare function checkVisibilityMs(ms: number): void;
183
321
  declare function checkReceive(options: {
184
322
  readonly limit: number;
185
323
  readonly visibilityMs: number;
186
324
  readonly maxAttempts: number;
187
325
  }): void;
188
- /** Queues over `engine`. */
326
+ /**
327
+ * The engine of the environment this code runs in: the request's own in a deployment, or `midsummer
328
+ * dev`'s on this computer. Every call goes to the one current when it is made. Flows reach their
329
+ * Queues through it too.
330
+ */
331
+ declare function hereEngine(): QueueEngine;
189
332
  /**
190
333
  * The queue the environment binds under `name`, from code in a deployment in any framework, or
191
334
  * `midsummer dev`'s queue of that name.
192
335
  */
193
336
  declare function queue(name: string): Queue;
337
+ /** Queues over `engine`: the Stand-in's in tests, or `hereEngine()`. */
194
338
  declare function queues(engine: QueueEngine): Queues;
195
339
  //#endregion
196
- export { DEAD, Delivery, EngineDelivery, EngineMessage, Json, MAX_BODY, Queue, QueueEngine, QueueError, QueueErrorCode, QueueStats, Queues, ReceiveOptions, STATS_CAP, SendOptions, backoffMs, bodyText, checkDelaySeconds, checkEngineQueue, checkKey, checkQueueName, checkReceipt, checkReceive, invalid, queue, queueErrorCodes, queues };
340
+ export { DEAD, Delivery, EngineDelivery, EngineMessage, EngineRun, EngineStep, Json, MAX_BODY, MAX_WAIT_MS, Queue, QueueEngine, QueueError, QueueErrorCode, QueueStats, Queues, ReceiveOptions, RunStatus, STATS_CAP, SendOptions, StepWrite, backoffMs, bodyText, checkDelaySeconds, checkEngineQueue, checkEvent, checkKey, checkQueueName, checkReceipt, checkReceive, checkStepName, checkToken, checkVisibilityMs, hereEngine, invalid, queue, queueErrorCodes, queues };
package/queues/index.mjs CHANGED
@@ -39,6 +39,15 @@ import { n as late, r as processEnv, t as here } from "../here-CeqGJW21.mjs";
39
39
  * returns the first Message's id and sends nothing. Schedules send with one, so a Schedule's minute
40
40
  * sends once however often it is reached.
41
41
  *
42
+ * Messages sent with the same `group` are delivered one at a time, in the order sent: the next is
43
+ * delivered only once the one before it is acked or moved to the dead-letter Queue, and a retried one
44
+ * keeps its place, so order-dependent work (a customer's payments, one document's edits) never runs
45
+ * out of order. Each group goes on apart from the others. A Queue can let a group have more at once,
46
+ * and limit deliveries a period (`midsummer queue edit`); its limits hold Messages back, never drop them.
47
+ *
48
+ * A receiver whose work takes longer than its visibility window extends it with `extend`, as often as
49
+ * it needs, before the window ends.
50
+ *
42
51
  * Names and bodies are checked here, before anything leaves the caller. Every failure is a
43
52
  * `QueueError`, whose `code` is the one the wire carries.
44
53
  */
@@ -49,7 +58,8 @@ const queueErrorCodes = [
49
58
  "invalid",
50
59
  "forbidden",
51
60
  "quota",
52
- "unavailable"
61
+ "unavailable",
62
+ "not-found"
53
63
  ];
54
64
  var QueueError = class extends Error {
55
65
  code;
@@ -81,6 +91,20 @@ function checkEngineQueue(queue) {
81
91
  function checkKey(key) {
82
92
  if (!/^[\x21-\x7e]{1,128}$/.test(key)) invalid("a send's key is 1 to 128 printable characters, without spaces");
83
93
  }
94
+ /** A group or a run's id: 1 to 128 printable characters, without spaces. */
95
+ function checkToken(value, what) {
96
+ if (typeof value !== "string" || !/^[\x21-\x7e]{1,128}$/.test(value)) invalid(`${what} is 1 to 128 printable characters, without spaces`);
97
+ }
98
+ /** A signal's event: as a step's name is, since a wait's event is its step's name unless it says. */
99
+ function checkEvent(event) {
100
+ if (typeof event !== "string" || !/^[^\x00-\x1f\x7f]{1,200}$/.test(event)) invalid("a signal's event is 1 to 200 characters, without line breaks");
101
+ }
102
+ /** A step's name: 1 to 200 characters, without line breaks or other control characters. */
103
+ function checkStepName(name) {
104
+ if (typeof name !== "string" || !/^[^\x00-\x1f\x7f]{1,200}$/.test(name)) invalid("a step's name is 1 to 200 characters, without line breaks");
105
+ }
106
+ /** The longest a delay, a sleep, a wait or a run's timeout may be: 365 days. */
107
+ const MAX_WAIT_MS = 31536e6;
84
108
  /** Bodies are at most this many bytes of JSON. */
85
109
  const MAX_BODY = 131072;
86
110
  const encoder = new TextEncoder();
@@ -101,20 +125,24 @@ function checkReceipt(receipt) {
101
125
  if (!/^[A-Za-z0-9._-]{1,200}$/.test(receipt)) invalid("that is not a receipt");
102
126
  }
103
127
  function checkDelaySeconds(seconds) {
104
- if (!whole(seconds, 0, 43200)) invalid("a delay is a whole number of seconds, 0 to 43,200");
128
+ if (!whole(seconds, 0, 31536e3)) invalid("a delay is a whole number of seconds, 0 to 31,536,000 (365 days)");
129
+ }
130
+ /** A visibility window in milliseconds: whole seconds, 1 to 43,200. */
131
+ function checkVisibilityMs(ms) {
132
+ if (!whole(ms, 1e3, 432e5) || ms % 1e3 !== 0) invalid("a visibility window is a whole number of seconds, 1 to 43,200");
105
133
  }
106
134
  function checkReceive(options) {
107
135
  if (!whole(options.limit, 1, 100)) invalid("a receive's limit is 1 to 100");
108
- if (!whole(options.visibilityMs, 1e3, 432e5) || options.visibilityMs % 1e3 !== 0) invalid("a visibility window is a whole number of seconds, 1 to 43,200");
136
+ checkVisibilityMs(options.visibilityMs);
109
137
  if (!whole(options.maxAttempts, 1, 100)) invalid("maxAttempts is 1 to 100");
110
138
  }
111
- /** Queues over `engine`. */
112
139
  /**
113
- * The queue the environment binds under `name`, from code in a deployment in any framework, or
114
- * `midsummer dev`'s queue of that name.
140
+ * The engine of the environment this code runs in: the request's own in a deployment, or `midsummer
141
+ * dev`'s on this computer. Every call goes to the one current when it is made. Flows reach their
142
+ * Queues through it too.
115
143
  */
116
- function queue(name) {
117
- return queues(late(() => {
144
+ function hereEngine() {
145
+ return late(() => {
118
146
  const handle = here()?.queues;
119
147
  if (handle) return remoteOver(handle);
120
148
  const env = processEnv(), url = env.MIDSUMMER_QUEUES_URL, token = env.MIDSUMMER_QUEUES_TOKEN;
@@ -131,8 +159,16 @@ function queue(name) {
131
159
  return new Uint8Array(await response.arrayBuffer());
132
160
  } });
133
161
  throw new QueueError("forbidden", "This code runs outside Midsummer, so no environment binds a queue for it.");
134
- })).queue(name);
162
+ });
135
163
  }
164
+ /**
165
+ * The queue the environment binds under `name`, from code in a deployment in any framework, or
166
+ * `midsummer dev`'s queue of that name.
167
+ */
168
+ function queue(name) {
169
+ return queues(hereEngine()).queue(name);
170
+ }
171
+ /** Queues over `engine`: the Stand-in's in tests, or `hereEngine()`. */
136
172
  function queues(engine) {
137
173
  const queueOf = (name, engineName, dead) => {
138
174
  const queue = {
@@ -146,13 +182,15 @@ function queues(engine) {
146
182
  },
147
183
  async sendBatch(messages) {
148
184
  if (messages.length < 1 || messages.length > 100) invalid("a batch sends 1 to 100 Messages");
149
- const checked = messages.map(({ body, delaySeconds = 0, key }) => {
185
+ const checked = messages.map(({ body, delaySeconds = 0, key, group }) => {
150
186
  checkDelaySeconds(delaySeconds);
151
187
  if (key !== void 0) checkKey(key);
188
+ if (group !== void 0) checkToken(group, "a group");
152
189
  return {
153
190
  body: bodyText(body),
154
191
  delayMs: delaySeconds * 1e3,
155
- ...key !== void 0 ? { key } : {}
192
+ ...key !== void 0 ? { key } : {},
193
+ ...group !== void 0 ? { group } : {}
156
194
  };
157
195
  });
158
196
  return { ids: await engine.send(engineName, checked) };
@@ -178,6 +216,12 @@ function queues(engine) {
178
216
  if (options.delaySeconds !== void 0) checkDelaySeconds(options.delaySeconds);
179
217
  return engine.retry(engineName, receipt, options.delaySeconds === void 0 ? void 0 : options.delaySeconds * 1e3);
180
218
  },
219
+ async extend(receipt, options = {}) {
220
+ checkReceipt(receipt);
221
+ const visibilityMs = (options.visibilitySeconds ?? 30) * 1e3;
222
+ checkVisibilityMs(visibilityMs);
223
+ return engine.extend(engineName, receipt, visibilityMs);
224
+ },
181
225
  stats() {
182
226
  return engine.stats(engineName);
183
227
  },
@@ -193,4 +237,4 @@ function queues(engine) {
193
237
  } };
194
238
  }
195
239
  //#endregion
196
- export { DEAD, MAX_BODY, QueueError, STATS_CAP, backoffMs, bodyText, checkDelaySeconds, checkEngineQueue, checkKey, checkQueueName, checkReceipt, checkReceive, invalid, queue, queueErrorCodes, queues };
240
+ export { DEAD, MAX_BODY, MAX_WAIT_MS, QueueError, STATS_CAP, backoffMs, bodyText, checkDelaySeconds, checkEngineQueue, checkEvent, checkKey, checkQueueName, checkReceipt, checkReceive, checkStepName, checkToken, checkVisibilityMs, hereEngine, invalid, queue, queueErrorCodes, queues };