@swirls/looms 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/actor/cell.d.ts +1 -1
  3. package/dist/actor/routing.d.ts +5 -1
  4. package/dist/actor/routing.js +14 -3
  5. package/dist/agent/effects.d.ts +20 -2
  6. package/dist/agent/effects.js +2 -0
  7. package/dist/agent/module.d.ts +540 -504
  8. package/dist/approval/effects.d.ts +36 -27
  9. package/dist/approval/effects.js +1 -0
  10. package/dist/approval/module.d.ts +20 -2
  11. package/dist/client/client.d.ts +38 -10
  12. package/dist/client/client.js +2 -1
  13. package/dist/cloudflare/durable-object.d.ts +1 -1
  14. package/dist/cloudflare/index.d.ts +1 -1
  15. package/dist/cloudflare/index.js +1 -1
  16. package/dist/cloudflare/queue-consumer.d.ts +17 -10
  17. package/dist/cloudflare/queue-consumer.js +63 -27
  18. package/dist/cloudflare/queue-message.d.ts +4 -1
  19. package/dist/cloudflare/queue-message.js +4 -1
  20. package/dist/cloudflare/queue-worker.d.ts +7 -3
  21. package/dist/cloudflare/queue-worker.js +7 -2
  22. package/dist/cloudflare/queues.d.ts +1 -1
  23. package/dist/cloudflare/queues.js +1 -1
  24. package/dist/core/compose.d.ts +2 -1
  25. package/dist/core/compose.js +5 -0
  26. package/dist/core/effects.d.ts +84 -39
  27. package/dist/core/effects.js +29 -8
  28. package/dist/core/fold.js +87 -14
  29. package/dist/core/module-scope.d.ts +22 -8
  30. package/dist/core/module-scope.js +3 -0
  31. package/dist/core/module.d.ts +40 -9
  32. package/dist/core/module.js +25 -6
  33. package/dist/core/protocol.d.ts +12 -1
  34. package/dist/core/protocol.js +12 -0
  35. package/dist/core/published.d.ts +52 -0
  36. package/dist/core/published.js +45 -0
  37. package/dist/core/replay.d.ts +4 -2
  38. package/dist/core/state.d.ts +8 -2
  39. package/dist/core/state.js +2 -0
  40. package/dist/debugger/app/assets/{index-CUO1xyYW.js → index-CMRl_aWQ.js} +2 -2
  41. package/dist/debugger/app/assets/routes-CfXxKG45.js +47 -0
  42. package/dist/debugger/app/index.html +1 -1
  43. package/dist/debugger/events/protocol-summarize.js +2 -0
  44. package/dist/evaluator/effects.d.ts +10 -1
  45. package/dist/evaluator/effects.js +1 -0
  46. package/dist/evaluator/module.d.ts +20 -2
  47. package/dist/runtime/cancellation.js +1 -1
  48. package/dist/runtime/dispatch-effect.d.ts +2 -2
  49. package/dist/runtime/dispatch-effect.js +12 -4
  50. package/dist/runtime/effect-admission.d.ts +45 -0
  51. package/dist/runtime/effect-admission.js +70 -0
  52. package/dist/runtime/helpers.d.ts +6 -1
  53. package/dist/runtime/helpers.js +14 -5
  54. package/dist/runtime/index.d.ts +1 -0
  55. package/dist/runtime/index.js +1 -0
  56. package/dist/runtime/local-effect-executor.js +30 -8
  57. package/dist/runtime/retry-policy.js +6 -1
  58. package/dist/runtime/runtime.js +7 -2
  59. package/dist/runtime/server/routes/definitions.js +8 -1
  60. package/dist/runtime/types.d.ts +20 -5
  61. package/dist/runtime/wake-engine.js +20 -0
  62. package/dist/runtime/worker-callbacks.js +2 -2
  63. package/dist/runtime/worker-effect-dispatch.d.ts +35 -9
  64. package/dist/runtime/worker-effect-dispatch.js +105 -26
  65. package/dist/schedule/effects.d.ts +20 -2
  66. package/dist/schedule/effects.js +2 -0
  67. package/dist/schedule/module.d.ts +40 -4
  68. package/dist/testing/corpus-module.d.ts +2 -2
  69. package/dist/testing/corpus-module.js +27 -19
  70. package/dist/testing/index.js +12 -23
  71. package/dist/workflow/effects.d.ts +20 -2
  72. package/dist/workflow/effects.js +2 -0
  73. package/dist/workflow/module.d.ts +40 -4
  74. package/package.json +1 -1
  75. package/dist/debugger/app/assets/routes-lt_6J025.js +0 -47
@@ -1,4 +1,4 @@
1
- export declare const requestApprovalEffect: NoInfer<import("@swirls/looms/core").EffectDefinition<{
1
+ export declare const requestApprovalEffect: NoInfer<import("@swirls/looms/core").LocalEffectDefinition<{
2
2
  readonly approvalId: string;
3
3
  readonly title: string;
4
4
  readonly description?: string | undefined;
@@ -9,6 +9,32 @@ export declare const requestApprovalEffect: NoInfer<import("@swirls/looms/core")
9
9
  }[];
10
10
  readonly schema?: import("effect/Schema").Json | undefined;
11
11
  }, never, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
12
+ readonly type: "approval.decided";
13
+ readonly payload: {
14
+ readonly approvalId: string;
15
+ readonly actionId: string;
16
+ readonly outcome: "approve" | "reject";
17
+ readonly payload?: import("effect/Schema").Json | undefined;
18
+ };
19
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
20
+ readonly type: "approval.requested";
21
+ readonly payload: {
22
+ readonly approvalId: string;
23
+ readonly title: string;
24
+ readonly description?: string | undefined;
25
+ readonly actions: readonly {
26
+ readonly id: string;
27
+ readonly label: string;
28
+ readonly outcome: "approve" | "reject";
29
+ }[];
30
+ readonly schema?: import("effect/Schema").Json | undefined;
31
+ };
32
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
33
+ readonly type: "approval.timed_out";
34
+ readonly payload: {
35
+ readonly approvalId: string;
36
+ };
37
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
12
38
  readonly type: "runtime.child.close.requested";
13
39
  readonly payload: {
14
40
  readonly policy: "terminate" | "wait";
@@ -53,6 +79,15 @@ export declare const requestApprovalEffect: NoInfer<import("@swirls/looms/core")
53
79
  readonly effectId: string;
54
80
  readonly attempt: number;
55
81
  };
82
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
83
+ readonly type: "runtime.effect.dispatch.deferred";
84
+ readonly payload: {
85
+ readonly effectId: string;
86
+ readonly attempt: number;
87
+ readonly retryAt: number;
88
+ readonly error: string;
89
+ readonly try: number;
90
+ };
56
91
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
57
92
  readonly type: "runtime.effect.dispatched";
58
93
  readonly payload: {
@@ -245,30 +280,4 @@ export declare const requestApprovalEffect: NoInfer<import("@swirls/looms/core")
245
280
  readonly payload: import("effect/Schema").Json;
246
281
  };
247
282
  };
248
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
249
- readonly type: "approval.decided";
250
- readonly payload: {
251
- readonly approvalId: string;
252
- readonly actionId: string;
253
- readonly outcome: "approve" | "reject";
254
- readonly payload?: import("effect/Schema").Json | undefined;
255
- };
256
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
257
- readonly type: "approval.requested";
258
- readonly payload: {
259
- readonly approvalId: string;
260
- readonly title: string;
261
- readonly description?: string | undefined;
262
- readonly actions: readonly {
263
- readonly id: string;
264
- readonly label: string;
265
- readonly outcome: "approve" | "reject";
266
- }[];
267
- readonly schema?: import("effect/Schema").Json | undefined;
268
- };
269
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
270
- readonly type: "approval.timed_out";
271
- readonly payload: {
272
- readonly approvalId: string;
273
- };
274
283
  })>>;
@@ -2,6 +2,7 @@ import { ApprovalRequestedPayloadSchema } from './events.js';
2
2
  import { approvalModule } from './scope.js';
3
3
  export const requestApprovalEffect = approvalModule.effect({
4
4
  type: 'approval.request',
5
+ placement: 'local',
5
6
  input: ApprovalRequestedPayloadSchema,
6
7
  execute: (input, ctx) => [
7
8
  {
@@ -20,7 +20,7 @@ export declare function approval(): import("@swirls/looms/core").RuntimeModule<"
20
20
  readonly approvalId: import("effect/Schema").String;
21
21
  }>;
22
22
  }>, {
23
- request: import("@swirls/looms/core").EffectDefinition<{
23
+ request: import("@swirls/looms/core").LocalEffectDefinition<{
24
24
  readonly approvalId: string;
25
25
  readonly title: string;
26
26
  readonly description?: string | undefined;
@@ -101,6 +101,15 @@ export declare function approval(): import("@swirls/looms/core").RuntimeModule<"
101
101
  readonly effectId: string;
102
102
  readonly attempt: number;
103
103
  };
104
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
105
+ readonly type: "runtime.effect.dispatch.deferred";
106
+ readonly payload: {
107
+ readonly effectId: string;
108
+ readonly attempt: number;
109
+ readonly retryAt: number;
110
+ readonly error: string;
111
+ readonly try: number;
112
+ };
104
113
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
105
114
  readonly type: "runtime.effect.dispatched";
106
115
  readonly payload: {
@@ -306,7 +315,7 @@ export declare function approval(): import("@swirls/looms/core").RuntimeModule<"
306
315
  }>;
307
316
  }, import("effect/Context").Service.Any> & {
308
317
  readonly effects: {
309
- request: import("@swirls/looms/core").EffectDefinition<{
318
+ request: import("@swirls/looms/core").LocalEffectDefinition<{
310
319
  readonly approvalId: string;
311
320
  readonly title: string;
312
321
  readonly description?: string | undefined;
@@ -387,6 +396,15 @@ export declare function approval(): import("@swirls/looms/core").RuntimeModule<"
387
396
  readonly effectId: string;
388
397
  readonly attempt: number;
389
398
  };
399
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
400
+ readonly type: "runtime.effect.dispatch.deferred";
401
+ readonly payload: {
402
+ readonly effectId: string;
403
+ readonly attempt: number;
404
+ readonly retryAt: number;
405
+ readonly error: string;
406
+ readonly try: number;
407
+ };
390
408
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
391
409
  readonly type: "runtime.effect.dispatched";
392
410
  readonly payload: {
@@ -179,13 +179,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
179
179
  readonly [x: string]: {
180
180
  readonly effectId: string;
181
181
  readonly attempt: number;
182
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
182
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
183
183
  readonly nextAttemptAt: number | null;
184
184
  readonly deadlineAt: number | null;
185
185
  readonly startedAt: number | null;
186
186
  readonly lastHeartbeatAt: number | null;
187
187
  readonly heartbeatDetails: Schema.Json;
188
188
  readonly lastError: string | null;
189
+ readonly dispatchTry: number;
189
190
  };
190
191
  };
191
192
  readonly completedEffectIds: readonly string[];
@@ -348,13 +349,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
348
349
  readonly [x: string]: {
349
350
  readonly effectId: string;
350
351
  readonly attempt: number;
351
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
352
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
352
353
  readonly nextAttemptAt: number | null;
353
354
  readonly deadlineAt: number | null;
354
355
  readonly startedAt: number | null;
355
356
  readonly lastHeartbeatAt: number | null;
356
357
  readonly heartbeatDetails: Schema.Json;
357
358
  readonly lastError: string | null;
359
+ readonly dispatchTry: number;
358
360
  };
359
361
  };
360
362
  readonly completedEffectIds: readonly string[];
@@ -515,13 +517,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
515
517
  readonly [x: string]: {
516
518
  readonly effectId: string;
517
519
  readonly attempt: number;
518
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
520
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
519
521
  readonly nextAttemptAt: number | null;
520
522
  readonly deadlineAt: number | null;
521
523
  readonly startedAt: number | null;
522
524
  readonly lastHeartbeatAt: number | null;
523
525
  readonly heartbeatDetails: Schema.Json;
524
526
  readonly lastError: string | null;
527
+ readonly dispatchTry: number;
525
528
  };
526
529
  };
527
530
  readonly completedEffectIds: readonly string[];
@@ -682,13 +685,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
682
685
  readonly [x: string]: {
683
686
  readonly effectId: string;
684
687
  readonly attempt: number;
685
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
688
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
686
689
  readonly nextAttemptAt: number | null;
687
690
  readonly deadlineAt: number | null;
688
691
  readonly startedAt: number | null;
689
692
  readonly lastHeartbeatAt: number | null;
690
693
  readonly heartbeatDetails: Schema.Json;
691
694
  readonly lastError: string | null;
695
+ readonly dispatchTry: number;
692
696
  };
693
697
  };
694
698
  readonly completedEffectIds: readonly string[];
@@ -882,13 +886,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
882
886
  readonly [x: string]: {
883
887
  readonly effectId: string;
884
888
  readonly attempt: number;
885
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
889
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
886
890
  readonly nextAttemptAt: number | null;
887
891
  readonly deadlineAt: number | null;
888
892
  readonly startedAt: number | null;
889
893
  readonly lastHeartbeatAt: number | null;
890
894
  readonly heartbeatDetails: Schema.Json;
891
895
  readonly lastError: string | null;
896
+ readonly dispatchTry: number;
892
897
  };
893
898
  };
894
899
  readonly completedEffectIds: readonly string[];
@@ -1049,13 +1054,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
1049
1054
  readonly [x: string]: {
1050
1055
  readonly effectId: string;
1051
1056
  readonly attempt: number;
1052
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1057
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1053
1058
  readonly nextAttemptAt: number | null;
1054
1059
  readonly deadlineAt: number | null;
1055
1060
  readonly startedAt: number | null;
1056
1061
  readonly lastHeartbeatAt: number | null;
1057
1062
  readonly heartbeatDetails: Schema.Json;
1058
1063
  readonly lastError: string | null;
1064
+ readonly dispatchTry: number;
1059
1065
  };
1060
1066
  };
1061
1067
  readonly completedEffectIds: readonly string[];
@@ -1218,13 +1224,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
1218
1224
  readonly [x: string]: {
1219
1225
  readonly effectId: string;
1220
1226
  readonly attempt: number;
1221
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1227
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1222
1228
  readonly nextAttemptAt: number | null;
1223
1229
  readonly deadlineAt: number | null;
1224
1230
  readonly startedAt: number | null;
1225
1231
  readonly lastHeartbeatAt: number | null;
1226
1232
  readonly heartbeatDetails: Schema.Json;
1227
1233
  readonly lastError: string | null;
1234
+ readonly dispatchTry: number;
1228
1235
  };
1229
1236
  };
1230
1237
  readonly completedEffectIds: readonly string[];
@@ -1389,13 +1396,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
1389
1396
  readonly [x: string]: {
1390
1397
  readonly effectId: string;
1391
1398
  readonly attempt: number;
1392
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1399
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1393
1400
  readonly nextAttemptAt: number | null;
1394
1401
  readonly deadlineAt: number | null;
1395
1402
  readonly startedAt: number | null;
1396
1403
  readonly lastHeartbeatAt: number | null;
1397
1404
  readonly heartbeatDetails: Schema.Json;
1398
1405
  readonly lastError: string | null;
1406
+ readonly dispatchTry: number;
1399
1407
  };
1400
1408
  };
1401
1409
  readonly completedEffectIds: readonly string[];
@@ -1431,6 +1439,24 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
1431
1439
  }[];
1432
1440
  readonly projections: readonly string[];
1433
1441
  }>;
1442
+ listEffects: () => Promise<{
1443
+ readonly effects: readonly {
1444
+ readonly type: string;
1445
+ readonly placement: "local" | "worker";
1446
+ readonly deterministic?: boolean | undefined;
1447
+ readonly retry?: {
1448
+ readonly maxAttempts: number;
1449
+ readonly backoffMs?: number | undefined;
1450
+ readonly maxBackoffMs?: number | undefined;
1451
+ readonly backoffMultiplier?: number | undefined;
1452
+ readonly scheduleToStartTimeoutMs?: number | undefined;
1453
+ readonly startToCloseTimeoutMs?: number | undefined;
1454
+ readonly heartbeatTimeoutMs?: number | undefined;
1455
+ readonly cancellationTimeoutMs?: number | undefined;
1456
+ } | undefined;
1457
+ readonly inputSchema?: Schema.Json | undefined;
1458
+ }[];
1459
+ }>;
1434
1460
  replayTo: (runId: string, seq: number) => Promise<{
1435
1461
  readonly runId: string;
1436
1462
  readonly step: {
@@ -1608,13 +1634,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
1608
1634
  readonly [x: string]: {
1609
1635
  readonly effectId: string;
1610
1636
  readonly attempt: number;
1611
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1637
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1612
1638
  readonly nextAttemptAt: number | null;
1613
1639
  readonly deadlineAt: number | null;
1614
1640
  readonly startedAt: number | null;
1615
1641
  readonly lastHeartbeatAt: number | null;
1616
1642
  readonly heartbeatDetails: Schema.Json;
1617
1643
  readonly lastError: string | null;
1644
+ readonly dispatchTry: number;
1618
1645
  };
1619
1646
  };
1620
1647
  readonly completedEffectIds: readonly string[];
@@ -1772,13 +1799,14 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
1772
1799
  readonly [x: string]: {
1773
1800
  readonly effectId: string;
1774
1801
  readonly attempt: number;
1775
- readonly status: "ambiguous" | "cancel_requested" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1802
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1776
1803
  readonly nextAttemptAt: number | null;
1777
1804
  readonly deadlineAt: number | null;
1778
1805
  readonly startedAt: number | null;
1779
1806
  readonly lastHeartbeatAt: number | null;
1780
1807
  readonly heartbeatDetails: Schema.Json;
1781
1808
  readonly lastError: string | null;
1809
+ readonly dispatchTry: number;
1782
1810
  };
1783
1811
  };
1784
1812
  readonly completedEffectIds: readonly string[];
@@ -1,5 +1,5 @@
1
1
  import { Data, Effect, Option, Predicate, Schema, Stream } from 'effect';
2
- import { createRunId, DefinitionCatalogSchema, EventEnvelopeSchema, readSubscriptionData, ReplayStepSchema, RunStateSchema, RunSummariesSchema, SecretContextSchema, stringifyJson, traceHeaders, } from '@swirls/looms/core';
2
+ import { createRunId, DefinitionCatalogSchema, EffectCatalogSchema, EventEnvelopeSchema, readSubscriptionData, ReplayStepSchema, RunStateSchema, RunSummariesSchema, SecretContextSchema, stringifyJson, traceHeaders, } from '@swirls/looms/core';
3
3
  import { consumeSseStreamEffect, eventsFromSseData, sseFrames } from './sse.js';
4
4
  class LoomsClientError extends Data.TaggedError('LoomsClientError') {
5
5
  constructor(message, cause) {
@@ -254,6 +254,7 @@ export function createLoomsClient(options = {}) {
254
254
  workerSecrets,
255
255
  listRunSummaries: () => request('/runs/summaries', RunSummariesSchema),
256
256
  listDefinitions: () => request('/definitions', DefinitionCatalogSchema),
257
+ listEffects: () => request('/definitions/effects', EffectCatalogSchema),
257
258
  replayTo: (runId, seq) => request(`/runs/${encodeURIComponent(runId)}/replay?seq=${seq}`, ReplayResultSchema),
258
259
  project: (runId, name) => request(`/runs/${encodeURIComponent(runId)}/projections/${encodeURIComponent(name)}`, ProjectionResultSchema),
259
260
  subscribeEvents,
@@ -24,7 +24,7 @@ export interface LoomsDurableObjectConfig {
24
24
  */
25
25
  readonly launcher?: RunLauncher;
26
26
  /**
27
- * Dispatches `execution: 'worker'` effects out of this object (typically a
27
+ * Dispatches worker-placed effects out of this object (typically a
28
28
  * Cloudflare Queue producer via `cloudflareQueueWorker`). Without it, worker
29
29
  * effects cannot leave the Durable Object.
30
30
  */
@@ -4,7 +4,7 @@ export { durableObjectLauncher, durableObjectScheduleHost, LoomsDurableObject, t
4
4
  export { r2BlobStore, type R2BucketLike, type R2ListResult, type R2ObjectBodyLike, type R2StoredObject, } from './r2-blob-store';
5
5
  export { routeToDurableObject, type DurableObjectNamespaceLike, type DurableObjectStubLike, } from './router';
6
6
  export { createMemoryQueue, type MemoryQueue, type MemoryQueueOptions } from './memory-queue';
7
- export { processQueueMessage, queueConsumer, QueueTaskError, type QueueBatchLike, type QueueConsumerOptions, type QueueTask, type QueueTaskContext, type QueueTaskHandler, type WorkerCallbackClient, } from './queue-consumer';
7
+ export { processQueueMessage, queueConsumer, QueueTaskError, workerHandlers, type QueueBatchLike, type QueueConsumerOptions, type QueueTask, type QueueTaskHandler, type WorkerCallbackClient, } from './queue-consumer';
8
8
  export { readQueueEffectMessage, type QueueEffectMessage, type QueueMessageLike, } from './queue-message';
9
9
  export { cloudflareQueuePullSource, createQueuePullConsumer, QueuePullError, type CloudflareQueuePullSourceOptions, type PulledQueueMessage, type QueueLeaseRetry, type QueuePullConsumer, type QueuePullConsumerOptions, type QueuePullResult, type QueuePullSource, } from './queue-pull';
10
10
  export { cloudflareQueueWorker, DEFAULT_MAX_QUEUE_MESSAGE_BYTES, QueueDispatchError, type CloudflareQueueWorkerOptions, type QueueProducer, type QueueRoute, } from './queue-worker';
@@ -4,7 +4,7 @@ export { durableObjectLauncher, durableObjectScheduleHost, LoomsDurableObject, }
4
4
  export { r2BlobStore, } from './r2-blob-store.js';
5
5
  export { routeToDurableObject, } from './router.js';
6
6
  export { createMemoryQueue } from './memory-queue.js';
7
- export { processQueueMessage, queueConsumer, QueueTaskError, } from './queue-consumer.js';
7
+ export { processQueueMessage, queueConsumer, QueueTaskError, workerHandlers, } from './queue-consumer.js';
8
8
  export { readQueueEffectMessage, } from './queue-message.js';
9
9
  export { cloudflareQueuePullSource, createQueuePullConsumer, QueuePullError, } from './queue-pull.js';
10
10
  export { cloudflareQueueWorker, DEFAULT_MAX_QUEUE_MESSAGE_BYTES, QueueDispatchError, } from './queue-worker.js';
@@ -1,5 +1,5 @@
1
1
  import { Effect } from 'effect';
2
- import { type BlobStore, type EventInput, type JsonValue, type SecretContext, type WorkerCallback, type WorkerCallbackInput, type WorkerCallbackResult, type WorkerSecretsResult } from '@swirls/looms/core';
2
+ import { type BlobStore, type EventInput, type JsonValue, type WorkerCallback, type WorkerCallbackInput, type WorkerCallbackResult, type WorkerEffectContext, type WorkerEffectDefinition, type WorkerSecretsResult } from '@swirls/looms/core';
3
3
  import { type QueueEffectMessage, type QueueMessageLike } from './queue-message';
4
4
  /** The worker half of the Looms HTTP API. `createLoomsClient({ baseUrl, workerCallbackToken })` is one. */
5
5
  export interface WorkerCallbackClient {
@@ -10,15 +10,22 @@ export interface WorkerCallbackClient {
10
10
  export interface QueueTask extends Omit<QueueEffectMessage, 'v' | 'input' | 'inputRef'> {
11
11
  readonly input: JsonValue;
12
12
  }
13
- export interface QueueTaskContext {
14
- /** Aborts once Looms asks the attempt to stop or no longer counts it as running. */
15
- readonly signal: AbortSignal;
16
- /** Report progress. A later attempt receives the last `details` as `task.details`. */
17
- heartbeat(details?: JsonValue): Promise<void>;
18
- /** The run's secret context. Rejects once the attempt is no longer running. */
19
- secrets(): Promise<SecretContext>;
20
- }
21
- export type QueueTaskHandler = (task: QueueTask, ctx: QueueTaskContext) => Promise<ReadonlyArray<EventInput>> | ReadonlyArray<EventInput>;
13
+ export type QueueTaskHandler = (task: QueueTask, ctx: WorkerEffectContext) => Promise<ReadonlyArray<EventInput>> | ReadonlyArray<EventInput>;
14
+ type WorkerHandlerFn<TInput> = (input: TInput, ctx: WorkerEffectContext) => Promise<ReadonlyArray<EventInput>> | ReadonlyArray<EventInput>;
15
+ type WorkerHandlerBinding<TInput> = readonly [
16
+ WorkerEffectDefinition<TInput>,
17
+ WorkerHandlerFn<TInput>
18
+ ];
19
+ /**
20
+ * Build a `handlers` map from **worker-placed** effect definitions. Input is validated with each
21
+ * definition's schema before the typed handler runs. Passing a local definition is a type error.
22
+ */
23
+ export declare function workerHandlers<const Entries extends ReadonlyArray<WorkerHandlerBinding<any>>>(entries: {
24
+ readonly [K in keyof Entries]: Entries[K] extends readonly [
25
+ WorkerEffectDefinition<infer TInput>,
26
+ unknown
27
+ ] ? WorkerHandlerBinding<TInput> : Entries[K];
28
+ }): Readonly<Record<string, QueueTaskHandler>>;
22
29
  declare const QueueTaskError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
23
30
  readonly _tag: "QueueTaskError";
24
31
  } & Readonly<A>;
@@ -1,6 +1,27 @@
1
1
  import { Data, Effect } from 'effect';
2
- import { resolveJson, } from '@swirls/looms/core';
2
+ import { resolveJson, validateInput, } from '@swirls/looms/core';
3
3
  import { readQueueEffectMessage, } from './queue-message.js';
4
+ /**
5
+ * Build a `handlers` map from **worker-placed** effect definitions. Input is validated with each
6
+ * definition's schema before the typed handler runs. Passing a local definition is a type error.
7
+ */
8
+ export function workerHandlers(entries) {
9
+ // SAFETY: keys are effect types; values are QueueTaskHandler wrappers around typed handlers.
10
+ const handlers = Object.create(null);
11
+ for (const [definition, handler] of entries) {
12
+ handlers[definition.type] = (task, ctx) => {
13
+ if (definition.input) {
14
+ return validateInput(definition.input, task.input).then((input) => {
15
+ // SAFETY: validateInput returns InferSchemaOutput for the definition's schema; TInput is that type.
16
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion
17
+ return handler(input, ctx);
18
+ });
19
+ }
20
+ return Promise.resolve(handler(task.input, ctx));
21
+ };
22
+ }
23
+ return handlers;
24
+ }
4
25
  /** Throw from a handler to control how Looms treats the failed attempt. */
5
26
  export class QueueTaskError extends Data.TaggedError('QueueTaskError') {
6
27
  }
@@ -45,32 +66,47 @@ function runAttempt(task, attempt, handler, cancelRequested, options) {
45
66
  : Effect.sleep(50 * (5 - remaining)).pipe(Effect.andThen(send(remaining - 1)))));
46
67
  return send(4);
47
68
  };
48
- const ctx = {
49
- signal: controller.signal,
50
- heartbeat: (details) => Effect.runPromise(callback(() => options.callbacks.workerCallback(task.runId, details === undefined
51
- ? { ...attempt, kind: 'heartbeat' }
52
- : { ...attempt, kind: 'heartbeat', details })).pipe(Effect.map((result) => {
53
- if (result.status !== 'accepted' || result.cancelRequested) {
54
- controller.abort();
55
- }
56
- }))),
57
- secrets: () => Effect.runPromise(callback(() => options.callbacks.workerSecrets(task.runId, attempt)).pipe(Effect.flatMap((result) => {
58
- if (result.status === 'accepted') {
59
- return Effect.succeed(result.secrets);
60
- }
61
- controller.abort();
62
- return Effect.fail(new CallbackError({ message: `the attempt is ${result.status}` }));
63
- }))),
64
- };
65
- return taskInput(task, options.blobs).pipe(Effect.flatMap((input) => Effect.tryPromise({
66
- try: () => Promise.resolve(handler({ ...taskFields(task), input }, ctx)),
67
- catch: (cause) => cause instanceof QueueTaskError
68
- ? cause
69
- : new QueueTaskError({
70
- message: cause instanceof Error ? cause.message : String(cause),
71
- }),
72
- })), Effect.matchEffect({
73
- onSuccess: (events) => report({ ...attempt, kind: 'complete', events }),
69
+ const emitted = [];
70
+ const loadSecrets = () => callback(() => options.callbacks.workerSecrets(task.runId, attempt)).pipe(Effect.flatMap((result) => {
71
+ if (result.status === 'accepted') {
72
+ return Effect.succeed(result.secrets);
73
+ }
74
+ controller.abort();
75
+ return Effect.fail(new CallbackError({ message: `the attempt is ${result.status}` }));
76
+ }));
77
+ return taskInput(task, options.blobs).pipe(Effect.flatMap((input) => loadSecrets().pipe(Effect.mapError((error) => new QueueTaskError({ message: error.message, nonRetryable: true })), Effect.flatMap((secrets) => {
78
+ const ctx = {
79
+ placement: 'worker',
80
+ effectId: task.effectId,
81
+ runId: task.runId,
82
+ threadId: task.threadId,
83
+ causingEventId: task.causingEventId,
84
+ signal: controller.signal,
85
+ attributes: task.attributes ?? {},
86
+ secrets,
87
+ trace: task.trace ?? null,
88
+ heartbeat: (details) => Effect.runPromise(callback(() => options.callbacks.workerCallback(task.runId, details === undefined
89
+ ? { ...attempt, kind: 'heartbeat' }
90
+ : { ...attempt, kind: 'heartbeat', details })).pipe(Effect.map((result) => {
91
+ if (result.status !== 'accepted' || result.cancelRequested) {
92
+ controller.abort();
93
+ }
94
+ }))),
95
+ emit: (event) => {
96
+ emitted.push(event);
97
+ return Promise.resolve();
98
+ },
99
+ };
100
+ return Effect.tryPromise({
101
+ try: () => Promise.resolve(handler({ ...taskFields(task), input }, ctx)),
102
+ catch: (cause) => cause instanceof QueueTaskError
103
+ ? cause
104
+ : new QueueTaskError({
105
+ message: cause instanceof Error ? cause.message : String(cause),
106
+ }),
107
+ });
108
+ }))), Effect.matchEffect({
109
+ onSuccess: (events) => report({ ...attempt, kind: 'complete', events: [...emitted, ...events] }),
74
110
  onFailure: (error) => report(controller.signal.aborted
75
111
  ? { ...attempt, kind: 'cancelled' }
76
112
  : {
@@ -1,4 +1,4 @@
1
- import { type BlobRef, type JsonValue, type TraceContext } from '@swirls/looms/core';
1
+ import { type BlobRef, type JsonValue, type RunAttributes, type TraceContext } from '@swirls/looms/core';
2
2
  import type { EffectWorkerDeadlines } from '@swirls/looms/runtime';
3
3
  /**
4
4
  * One effect attempt on a Cloudflare Queue. It never carries the run's secrets; the consumer reads
@@ -13,6 +13,9 @@ export interface QueueEffectMessage {
13
13
  readonly input?: JsonValue;
14
14
  /** Set instead of `input` when the input would push the message over the queue's size limit. */
15
15
  readonly inputRef?: BlobRef;
16
+ readonly threadId: string;
17
+ readonly causingEventId: string;
18
+ readonly attributes?: RunAttributes;
16
19
  /** Progress from the previous attempt's last heartbeat. */
17
20
  readonly details?: JsonValue;
18
21
  readonly trace?: TraceContext;
@@ -1,5 +1,5 @@
1
1
  import { Option, Schema } from 'effect';
2
- import { isBlobRef, TraceContextSchema, } from '@swirls/looms/core';
2
+ import { isBlobRef, RunAttributesSchema, TraceContextSchema, } from '@swirls/looms/core';
3
3
  const QueueEffectMessageSchema = Schema.Struct({
4
4
  v: Schema.Literal(1),
5
5
  runId: Schema.String,
@@ -8,6 +8,9 @@ const QueueEffectMessageSchema = Schema.Struct({
8
8
  type: Schema.String,
9
9
  input: Schema.optional(Schema.Json),
10
10
  inputRef: Schema.optional(Schema.Json),
11
+ threadId: Schema.String,
12
+ causingEventId: Schema.String,
13
+ attributes: Schema.optional(RunAttributesSchema),
11
14
  details: Schema.optional(Schema.Json),
12
15
  trace: Schema.optional(TraceContextSchema),
13
16
  deadlines: Schema.optional(Schema.Struct({
@@ -1,5 +1,5 @@
1
1
  import { type Types } from 'effect';
2
- import { type BlobStore } from '@swirls/looms/core';
2
+ import { type BlobStore, type WorkerEffectDefinition } from '@swirls/looms/core';
3
3
  import type { EffectWorker } from '@swirls/looms/runtime';
4
4
  import type { QueueEffectMessage } from './queue-message';
5
5
  /** Leaves room under Cloudflare's 128 KB message limit. */
@@ -10,12 +10,16 @@ export interface QueueProducer {
10
10
  readonly contentType?: 'json';
11
11
  }): Promise<void>;
12
12
  }
13
+ /**
14
+ * Route worker-placed effect definitions to a queue. Passing a local-placed definition is a
15
+ * type error — only {@link WorkerEffectDefinition} values are accepted.
16
+ */
13
17
  export interface QueueRoute {
14
- readonly handles: ReadonlyArray<string> | ((type: string) => boolean);
18
+ readonly effects: ReadonlyArray<WorkerEffectDefinition>;
15
19
  readonly queue: QueueProducer;
16
20
  }
17
21
  export interface CloudflareQueueWorkerOptions {
18
- /** The first route that handles an effect type gets its attempts. */
22
+ /** The first route that lists an effect type gets its attempts. */
19
23
  readonly routes: ReadonlyArray<QueueRoute>;
20
24
  /** Holds an input that would push a message past `maxMessageBytes`. Without it, that dispatch fails. */
21
25
  readonly blobs?: BlobStore;
@@ -1,4 +1,4 @@
1
- import { Data, Effect, Predicate } from 'effect';
1
+ import { Data, Effect } from 'effect';
2
2
  import { claimJson, utf8JsonBytes } from '@swirls/looms/core';
3
3
  /** Leaves room under Cloudflare's 128 KB message limit. */
4
4
  export const DEFAULT_MAX_QUEUE_MESSAGE_BYTES = 100_000;
@@ -12,7 +12,7 @@ export class QueueDispatchError extends Data.TaggedError('QueueDispatchError') {
12
12
  * queued message cannot be recalled.
13
13
  */
14
14
  export function cloudflareQueueWorker(options) {
15
- const routeFor = (type) => options.routes.find((route) => Predicate.isFunction(route.handles) ? route.handles(type) : route.handles.includes(type));
15
+ const routeFor = (type) => options.routes.find((route) => route.effects.some((effect) => effect.type === type));
16
16
  return {
17
17
  dispatchSemantics: 'acknowledged',
18
18
  handles: (type) => routeFor(type) !== undefined,
@@ -37,7 +37,12 @@ function queueMessage(task, options) {
37
37
  effectId: task.effectId,
38
38
  attempt: task.attempt,
39
39
  type: task.type,
40
+ threadId: task.threadId,
41
+ causingEventId: task.causingEventId,
40
42
  };
43
+ if (task.attributes !== undefined) {
44
+ message.attributes = task.attributes;
45
+ }
41
46
  if (task.details !== undefined) {
42
47
  message.details = task.details;
43
48
  }
@@ -4,7 +4,7 @@
4
4
  * only inside the Workers runtime that hosts Durable Objects.
5
5
  */
6
6
  export { createMemoryQueue, type MemoryQueue, type MemoryQueueOptions } from './memory-queue';
7
- export { processQueueMessage, queueConsumer, QueueTaskError, type QueueBatchLike, type QueueConsumerOptions, type QueueTask, type QueueTaskContext, type QueueTaskHandler, type WorkerCallbackClient, } from './queue-consumer';
7
+ export { processQueueMessage, queueConsumer, QueueTaskError, workerHandlers, type QueueBatchLike, type QueueConsumerOptions, type QueueTask, type QueueTaskHandler, type WorkerCallbackClient, } from './queue-consumer';
8
8
  export { readQueueEffectMessage, type QueueEffectMessage, type QueueMessageLike, } from './queue-message';
9
9
  export { cloudflareQueuePullSource, createQueuePullConsumer, QueuePullError, type CloudflareQueuePullSourceOptions, type PulledQueueMessage, type QueueLeaseRetry, type QueuePullConsumer, type QueuePullConsumerOptions, type QueuePullResult, type QueuePullSource, } from './queue-pull';
10
10
  export { cloudflareQueueWorker, DEFAULT_MAX_QUEUE_MESSAGE_BYTES, QueueDispatchError, type CloudflareQueueWorkerOptions, type QueueProducer, type QueueRoute, } from './queue-worker';
@@ -4,7 +4,7 @@
4
4
  * only inside the Workers runtime that hosts Durable Objects.
5
5
  */
6
6
  export { createMemoryQueue } from './memory-queue.js';
7
- export { processQueueMessage, queueConsumer, QueueTaskError, } from './queue-consumer.js';
7
+ export { processQueueMessage, queueConsumer, QueueTaskError, workerHandlers, } from './queue-consumer.js';
8
8
  export { readQueueEffectMessage, } from './queue-message.js';
9
9
  export { cloudflareQueuePullSource, createQueuePullConsumer, QueuePullError, } from './queue-pull.js';
10
10
  export { cloudflareQueueWorker, DEFAULT_MAX_QUEUE_MESSAGE_BYTES, QueueDispatchError, } from './queue-worker.js';