@swirls/looms 0.11.3 → 0.12.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @swirls/looms
2
2
 
3
+ ## 0.12.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 76fd14d: Add commit-only start APIs parallel to the existing wake-waiting ones:
8
+
9
+ | | Waits for first wake | Returns after commit |
10
+ | -------------------------- | -------------------- | -------------------- |
11
+ | Definition object | `start` | `launch` |
12
+ | `{ kind, definitionName }` | `startRun` | `launchRun` |
13
+ | HTTP | `POST /runs` | `POST /runs/launch` |
14
+
15
+ `launch` / `launchRun` / `POST /runs/launch` leave the first wake to the host's scheduler (the Durable Object alarm on Cloudflare). The runtime method stays `launchRun`; actor `cell.launch` is unchanged (cross-run launcher, returns void).
16
+
3
17
  ## 0.11.3
4
18
 
5
19
  ### Patch Changes
@@ -29,11 +43,11 @@
29
43
  ```ts
30
44
  const attributes = defineRunAttributes({
31
45
  tenantId: { required: true, index: true },
32
- });
46
+ })
33
47
 
34
- createLooms({ modules, attributes });
35
- postgresReplica({ url, additionalFields: attributes });
36
- loomsReplicaTables({ additionalFields: attributes });
48
+ createLooms({ modules, attributes })
49
+ postgresReplica({ url, additionalFields: attributes })
50
+ loomsReplicaTables({ additionalFields: attributes })
37
51
  ```
38
52
 
39
53
  - Each attribute is a text column named in snake_case unless `fieldName` is set. `index: true` adds an index. Each column and index is its own migration, and a new column is backfilled from the `attributes` JSON. `postgresEventLog` records these migrations in `looms_schema_migrations` and applies each one once.
@@ -31,8 +31,8 @@ export declare class RunTargetError extends RunTargetError_base<{
31
31
  }
32
32
  /**
33
33
  * Resolves the destination `runId` for routing to an actor cell.
34
- * For `POST /runs` without a `runId`, creates a fresh `runId`, injects it
35
- * into the body, and sets `x-looms-run-id`.
34
+ * For `POST /runs` and `POST /runs/launch` without a `runId`, creates a fresh
35
+ * `runId`, injects it into the body, and sets `x-looms-run-id`.
36
36
  */
37
37
  export declare function resolveRunTarget(req: Request): Promise<ResolvedRunTarget>;
38
38
  export {};
@@ -5,6 +5,9 @@ const RunTargetBodySchema = Schema.Struct({
5
5
  });
6
6
  const decodeRunTargetBody = Schema.decodeUnknownOption(Schema.fromJsonString(RunTargetBodySchema));
7
7
  const decodePayloadRecord = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Record(Schema.String, Schema.Json)));
8
+ function isStartRunRequest(req, pathname) {
9
+ return req.method === 'POST' && (pathname === '/runs' || pathname === '/runs/launch');
10
+ }
8
11
  /**
9
12
  * Extracts the target `runId` from a Looms HTTP request.
10
13
  * Inspects `x-looms-run-id` and `/runs/:runId/*`.
@@ -16,6 +19,9 @@ export function runIdFromRequest(req) {
16
19
  }
17
20
  const url = new URL(req.url);
18
21
  const path = url.pathname;
22
+ if (isStartRunRequest(req, path)) {
23
+ return null;
24
+ }
19
25
  const runsMatch = path.match(/^\/runs\/([^/]+)(?:\/.*)?$/);
20
26
  if (runsMatch && runsMatch[1]) {
21
27
  return decodeURIComponent(runsMatch[1]);
@@ -71,8 +77,8 @@ export class RunTargetError extends Data.TaggedError('RunTargetError') {
71
77
  }
72
78
  /**
73
79
  * Resolves the destination `runId` for routing to an actor cell.
74
- * For `POST /runs` without a `runId`, creates a fresh `runId`, injects it
75
- * into the body, and sets `x-looms-run-id`.
80
+ * For `POST /runs` and `POST /runs/launch` without a `runId`, creates a fresh
81
+ * `runId`, injects it into the body, and sets `x-looms-run-id`.
76
82
  */
77
83
  export function resolveRunTarget(req) {
78
84
  return Effect.runPromise(Effect.gen(function* () {
@@ -94,7 +100,7 @@ export function resolveRunTarget(req) {
94
100
  return { runId: existing, request };
95
101
  }
96
102
  const url = new URL(req.url);
97
- if (req.method === 'POST' && url.pathname === '/runs') {
103
+ if (isStartRunRequest(req, url.pathname)) {
98
104
  const text = yield* Effect.promise(() => req.clone().text());
99
105
  const decoded = decodeRunTargetBody(text);
100
106
  let runId;
@@ -32,7 +32,10 @@ export interface StreamHandle extends AsyncIterable<EventEnvelope> {
32
32
  readonly runId: string;
33
33
  }
34
34
  export declare function createLoomsClient<const Fields extends RunAttributeFields = RunAttributeFields>(options?: LoomsClientOptions<Fields>): {
35
- /** Start a run by `{ kind, definitionName }` when you only have names. */
35
+ /**
36
+ * Start a run by `{ kind, definitionName }` when you only have names. Answers after the first
37
+ * wake, so a short run can come back finished.
38
+ */
36
39
  startRun: (args: ClientStartArgs<Fields> & {
37
40
  secrets?: SecretContext;
38
41
  }) => Promise<{
@@ -204,6 +207,181 @@ export declare function createLoomsClient<const Fields extends RunAttributeField
204
207
  readonly childCloseSent: boolean;
205
208
  };
206
209
  }>;
210
+ /**
211
+ * Like `startRun`, but answers once the start is committed and leaves the first wake to the
212
+ * host's scheduler. The returned state is the freshly started run; read or subscribe for progress.
213
+ */
214
+ launchRun: (args: ClientStartArgs<Fields> & {
215
+ secrets?: SecretContext;
216
+ }) => Promise<{
217
+ readonly runId: string;
218
+ readonly threadId: string;
219
+ readonly state: {
220
+ readonly runId: string;
221
+ readonly status: "cancelled" | "completed" | "failed" | "running";
222
+ readonly rootThreadId: string | null;
223
+ readonly startIdentity: {
224
+ readonly kind: string;
225
+ readonly definitionName: string;
226
+ readonly definitionVersion: string;
227
+ readonly input: Schema.Json;
228
+ readonly requestedThreadId: string | null;
229
+ readonly idempotencyKey: string | null;
230
+ readonly rootThreadId: string;
231
+ readonly launchedBy: {
232
+ readonly runId: string;
233
+ readonly threadId: string;
234
+ readonly effectId: string;
235
+ readonly causationId: string;
236
+ readonly causationSeq: number;
237
+ } | null;
238
+ readonly correlationId: string | null;
239
+ readonly attributes: {
240
+ readonly [x: string]: string;
241
+ };
242
+ readonly trace: {
243
+ readonly traceparent: string;
244
+ readonly tracestate?: string | undefined;
245
+ } | null;
246
+ } | null;
247
+ readonly attributes: {
248
+ readonly [x: string]: string;
249
+ };
250
+ readonly forkedFrom: {
251
+ readonly runId: string;
252
+ readonly seq: number;
253
+ } | null;
254
+ readonly threads: {
255
+ readonly [x: string]: {
256
+ readonly threadId: string;
257
+ readonly kind: string;
258
+ readonly definitionName: string;
259
+ readonly definitionVersion: string;
260
+ readonly parentThreadId: string | null;
261
+ readonly status: "cancelled" | "completed" | "failed" | "running" | "waiting";
262
+ readonly input: Schema.Json;
263
+ readonly output: Schema.Json;
264
+ readonly error: string | null;
265
+ readonly state: Schema.Json;
266
+ };
267
+ };
268
+ readonly waits: {
269
+ readonly [x: string]: {
270
+ readonly waitId: string;
271
+ readonly threadId: string;
272
+ readonly on: {
273
+ readonly type: string | readonly string[];
274
+ readonly match?: Schema.Json | undefined;
275
+ } | {
276
+ readonly timerAt: number;
277
+ };
278
+ readonly tag?: Schema.Json | undefined;
279
+ };
280
+ };
281
+ readonly outstandingEffects: readonly {
282
+ readonly effectId: string;
283
+ readonly threadId: string;
284
+ readonly causingSeq: number;
285
+ readonly causingEventId: string;
286
+ readonly effect: {
287
+ readonly type: "runtime.spawn";
288
+ readonly childThreadId: string;
289
+ readonly kind: string;
290
+ readonly definitionName: string;
291
+ readonly definitionVersion: string;
292
+ readonly input: Schema.Json;
293
+ } | {
294
+ readonly type: "runtime.wait";
295
+ readonly waitId: string;
296
+ readonly on: {
297
+ readonly type: string | readonly string[];
298
+ readonly match?: Schema.Json | undefined;
299
+ } | {
300
+ readonly timerAt: number;
301
+ };
302
+ readonly tag?: Schema.Json | undefined;
303
+ } | {
304
+ readonly type: "runtime.emit";
305
+ readonly event: {
306
+ readonly type: string;
307
+ readonly payload: Schema.Json;
308
+ readonly threadId?: string | null | undefined;
309
+ readonly parentThreadId?: string | null | undefined;
310
+ readonly causationId?: string | null | undefined;
311
+ readonly correlationId?: string | null | undefined;
312
+ readonly effectId?: string | null | undefined;
313
+ readonly origin?: {
314
+ readonly type: "thread";
315
+ readonly threadId: string;
316
+ } | {
317
+ readonly type: "external";
318
+ readonly actorId?: string | undefined;
319
+ } | {
320
+ readonly type: "system";
321
+ } | {
322
+ readonly type: "run";
323
+ readonly runId: string;
324
+ } | undefined;
325
+ readonly ephemeral?: boolean | undefined;
326
+ readonly id?: string | undefined;
327
+ readonly ts?: number | undefined;
328
+ readonly idempotencyKey?: string | null | undefined;
329
+ };
330
+ readonly tag?: string | undefined;
331
+ } | {
332
+ readonly type: "runtime.complete";
333
+ readonly output: Schema.Json;
334
+ } | {
335
+ readonly type: "runtime.fail";
336
+ readonly error: string;
337
+ } | {
338
+ readonly type: "runtime.cancel";
339
+ readonly threadId: string;
340
+ readonly tag?: string | undefined;
341
+ } | {
342
+ readonly type: "runtime.launch";
343
+ readonly runId: string;
344
+ readonly kind: string;
345
+ readonly definitionName: string;
346
+ readonly definitionVersion: string;
347
+ readonly input: Schema.Json;
348
+ readonly close: "terminate" | "wait";
349
+ readonly tag?: string | undefined;
350
+ } | {
351
+ readonly type: "runtime.cancelRun";
352
+ readonly runId: string;
353
+ readonly tag?: string | undefined;
354
+ } | {
355
+ readonly type: string;
356
+ readonly input: Schema.Json;
357
+ readonly tag?: string | undefined;
358
+ readonly at?: number | undefined;
359
+ };
360
+ }[];
361
+ readonly effectExecutions: {
362
+ readonly [x: string]: {
363
+ readonly effectId: string;
364
+ readonly attempt: number;
365
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
366
+ readonly nextAttemptAt: number | null;
367
+ readonly deadlineAt: number | null;
368
+ readonly startedAt: number | null;
369
+ readonly lastHeartbeatAt: number | null;
370
+ readonly heartbeatDetails: Schema.Json;
371
+ readonly lastError: string | null;
372
+ readonly dispatchTry: number;
373
+ };
374
+ };
375
+ readonly completedEffectIds: readonly string[];
376
+ readonly processedIdempotencyKeys: readonly string[];
377
+ readonly launchedRuns: readonly {
378
+ readonly runId: string;
379
+ readonly close: "terminate" | "wait";
380
+ }[];
381
+ readonly closeWaiting: boolean;
382
+ readonly childCloseSent: boolean;
383
+ };
384
+ }>;
207
385
  /** Start a run from a definition object (agent, workflow, or your own kind). */
208
386
  start: <TDef extends DefinitionRef>(definition: TDef, input?: DefinitionInput<TDef>) => Promise<{
209
387
  readonly runId: string;
@@ -374,6 +552,179 @@ export declare function createLoomsClient<const Fields extends RunAttributeField
374
552
  readonly childCloseSent: boolean;
375
553
  };
376
554
  }>;
555
+ /**
556
+ * Like `start`, but answers once the start is committed and leaves the first wake to the
557
+ * host's scheduler.
558
+ */
559
+ launch: <TDef extends DefinitionRef>(definition: TDef, input?: DefinitionInput<TDef>) => Promise<{
560
+ readonly runId: string;
561
+ readonly threadId: string;
562
+ readonly state: {
563
+ readonly runId: string;
564
+ readonly status: "cancelled" | "completed" | "failed" | "running";
565
+ readonly rootThreadId: string | null;
566
+ readonly startIdentity: {
567
+ readonly kind: string;
568
+ readonly definitionName: string;
569
+ readonly definitionVersion: string;
570
+ readonly input: Schema.Json;
571
+ readonly requestedThreadId: string | null;
572
+ readonly idempotencyKey: string | null;
573
+ readonly rootThreadId: string;
574
+ readonly launchedBy: {
575
+ readonly runId: string;
576
+ readonly threadId: string;
577
+ readonly effectId: string;
578
+ readonly causationId: string;
579
+ readonly causationSeq: number;
580
+ } | null;
581
+ readonly correlationId: string | null;
582
+ readonly attributes: {
583
+ readonly [x: string]: string;
584
+ };
585
+ readonly trace: {
586
+ readonly traceparent: string;
587
+ readonly tracestate?: string | undefined;
588
+ } | null;
589
+ } | null;
590
+ readonly attributes: {
591
+ readonly [x: string]: string;
592
+ };
593
+ readonly forkedFrom: {
594
+ readonly runId: string;
595
+ readonly seq: number;
596
+ } | null;
597
+ readonly threads: {
598
+ readonly [x: string]: {
599
+ readonly threadId: string;
600
+ readonly kind: string;
601
+ readonly definitionName: string;
602
+ readonly definitionVersion: string;
603
+ readonly parentThreadId: string | null;
604
+ readonly status: "cancelled" | "completed" | "failed" | "running" | "waiting";
605
+ readonly input: Schema.Json;
606
+ readonly output: Schema.Json;
607
+ readonly error: string | null;
608
+ readonly state: Schema.Json;
609
+ };
610
+ };
611
+ readonly waits: {
612
+ readonly [x: string]: {
613
+ readonly waitId: string;
614
+ readonly threadId: string;
615
+ readonly on: {
616
+ readonly type: string | readonly string[];
617
+ readonly match?: Schema.Json | undefined;
618
+ } | {
619
+ readonly timerAt: number;
620
+ };
621
+ readonly tag?: Schema.Json | undefined;
622
+ };
623
+ };
624
+ readonly outstandingEffects: readonly {
625
+ readonly effectId: string;
626
+ readonly threadId: string;
627
+ readonly causingSeq: number;
628
+ readonly causingEventId: string;
629
+ readonly effect: {
630
+ readonly type: "runtime.spawn";
631
+ readonly childThreadId: string;
632
+ readonly kind: string;
633
+ readonly definitionName: string;
634
+ readonly definitionVersion: string;
635
+ readonly input: Schema.Json;
636
+ } | {
637
+ readonly type: "runtime.wait";
638
+ readonly waitId: string;
639
+ readonly on: {
640
+ readonly type: string | readonly string[];
641
+ readonly match?: Schema.Json | undefined;
642
+ } | {
643
+ readonly timerAt: number;
644
+ };
645
+ readonly tag?: Schema.Json | undefined;
646
+ } | {
647
+ readonly type: "runtime.emit";
648
+ readonly event: {
649
+ readonly type: string;
650
+ readonly payload: Schema.Json;
651
+ readonly threadId?: string | null | undefined;
652
+ readonly parentThreadId?: string | null | undefined;
653
+ readonly causationId?: string | null | undefined;
654
+ readonly correlationId?: string | null | undefined;
655
+ readonly effectId?: string | null | undefined;
656
+ readonly origin?: {
657
+ readonly type: "thread";
658
+ readonly threadId: string;
659
+ } | {
660
+ readonly type: "external";
661
+ readonly actorId?: string | undefined;
662
+ } | {
663
+ readonly type: "system";
664
+ } | {
665
+ readonly type: "run";
666
+ readonly runId: string;
667
+ } | undefined;
668
+ readonly ephemeral?: boolean | undefined;
669
+ readonly id?: string | undefined;
670
+ readonly ts?: number | undefined;
671
+ readonly idempotencyKey?: string | null | undefined;
672
+ };
673
+ readonly tag?: string | undefined;
674
+ } | {
675
+ readonly type: "runtime.complete";
676
+ readonly output: Schema.Json;
677
+ } | {
678
+ readonly type: "runtime.fail";
679
+ readonly error: string;
680
+ } | {
681
+ readonly type: "runtime.cancel";
682
+ readonly threadId: string;
683
+ readonly tag?: string | undefined;
684
+ } | {
685
+ readonly type: "runtime.launch";
686
+ readonly runId: string;
687
+ readonly kind: string;
688
+ readonly definitionName: string;
689
+ readonly definitionVersion: string;
690
+ readonly input: Schema.Json;
691
+ readonly close: "terminate" | "wait";
692
+ readonly tag?: string | undefined;
693
+ } | {
694
+ readonly type: "runtime.cancelRun";
695
+ readonly runId: string;
696
+ readonly tag?: string | undefined;
697
+ } | {
698
+ readonly type: string;
699
+ readonly input: Schema.Json;
700
+ readonly tag?: string | undefined;
701
+ readonly at?: number | undefined;
702
+ };
703
+ }[];
704
+ readonly effectExecutions: {
705
+ readonly [x: string]: {
706
+ readonly effectId: string;
707
+ readonly attempt: number;
708
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
709
+ readonly nextAttemptAt: number | null;
710
+ readonly deadlineAt: number | null;
711
+ readonly startedAt: number | null;
712
+ readonly lastHeartbeatAt: number | null;
713
+ readonly heartbeatDetails: Schema.Json;
714
+ readonly lastError: string | null;
715
+ readonly dispatchTry: number;
716
+ };
717
+ };
718
+ readonly completedEffectIds: readonly string[];
719
+ readonly processedIdempotencyKeys: readonly string[];
720
+ readonly launchedRuns: readonly {
721
+ readonly runId: string;
722
+ readonly close: "terminate" | "wait";
723
+ }[];
724
+ readonly closeWaiting: boolean;
725
+ readonly childCloseSent: boolean;
726
+ };
727
+ }>;
377
728
  getRun: (runId: string) => Promise<{
378
729
  readonly runId: string;
379
730
  readonly state: {
@@ -167,14 +167,25 @@ export function createLoomsClient(options = {}) {
167
167
  controller.abort();
168
168
  };
169
169
  };
170
- const startRun = (args) => {
170
+ const postStart = (path, args) => {
171
171
  const { trace, ...body } = args;
172
- return request('/runs', StartResultSchema, {
172
+ return request(path, StartResultSchema, {
173
173
  method: 'POST',
174
174
  headers: traceHeaders(trace),
175
175
  body: stringifyJson(body),
176
176
  });
177
177
  };
178
+ const startRun = (args) => postStart('/runs', args);
179
+ const launchRun = (args) => postStart('/runs/launch', args);
180
+ const startFromDefinition = (path, definition, input) => request(path, StartResultSchema, {
181
+ method: 'POST',
182
+ body: stringifyJson({
183
+ kind: definition.kind,
184
+ definitionName: definition.name,
185
+ definitionVersion: definition.version,
186
+ input,
187
+ }),
188
+ });
178
189
  const workerHeaders = options.workerCallbackToken
179
190
  ? { authorization: `Bearer ${options.workerCallbackToken}` }
180
191
  : undefined;
@@ -219,18 +230,23 @@ export function createLoomsClient(options = {}) {
219
230
  return Object.assign(Stream.toAsyncIterable(stream), { runId });
220
231
  };
221
232
  return {
222
- /** Start a run by `{ kind, definitionName }` when you only have names. */
233
+ /**
234
+ * Start a run by `{ kind, definitionName }` when you only have names. Answers after the first
235
+ * wake, so a short run can come back finished.
236
+ */
223
237
  startRun,
238
+ /**
239
+ * Like `startRun`, but answers once the start is committed and leaves the first wake to the
240
+ * host's scheduler. The returned state is the freshly started run; read or subscribe for progress.
241
+ */
242
+ launchRun,
224
243
  /** Start a run from a definition object (agent, workflow, or your own kind). */
225
- start: (definition, input) => request('/runs', StartResultSchema, {
226
- method: 'POST',
227
- body: stringifyJson({
228
- kind: definition.kind,
229
- definitionName: definition.name,
230
- definitionVersion: definition.version,
231
- input,
232
- }),
233
- }),
244
+ start: (definition, input) => startFromDefinition('/runs', definition, input),
245
+ /**
246
+ * Like `start`, but answers once the start is committed and leaves the first wake to the
247
+ * host's scheduler.
248
+ */
249
+ launch: (definition, input) => startFromDefinition('/runs/launch', definition, input),
234
250
  getRun: (runId) => request(`/runs/${encodeURIComponent(runId)}`, RunResultSchema),
235
251
  getState: (runId) => request(`/runs/${encodeURIComponent(runId)}`, RunResultSchema),
236
252
  getEvents: (runId, opts) => {