@swirls/looms 0.25.0 → 0.26.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 (85) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/actor/cell.d.ts +3 -1
  3. package/dist/actor/cell.js +1 -0
  4. package/dist/actor/host.d.ts +3 -1
  5. package/dist/actor/host.js +2 -1
  6. package/dist/actor/routing.d.ts +6 -3
  7. package/dist/actor/routing.js +6 -4
  8. package/dist/agent/agent-session.js +2 -2
  9. package/dist/agent/debugger/agent-chat.js +4 -3
  10. package/dist/agent/definitions.d.ts +3 -1
  11. package/dist/agent/effects.js +5 -3
  12. package/dist/agent/module.d.ts +730 -730
  13. package/dist/approval/steps.d.ts +3 -1
  14. package/dist/approval/steps.js +2 -2
  15. package/dist/client/client.d.ts +7 -1
  16. package/dist/client/client.js +5 -2
  17. package/dist/cloudflare/durable-object.d.ts +3 -1
  18. package/dist/cloudflare/durable-object.js +1 -0
  19. package/dist/cloudflare/queue-consumer.d.ts +3 -1
  20. package/dist/cloudflare/queue-consumer.js +2 -1
  21. package/dist/cloudflare/router.d.ts +3 -1
  22. package/dist/cloudflare/router.js +1 -1
  23. package/dist/core/compose.d.ts +8 -2
  24. package/dist/core/compose.js +6 -3
  25. package/dist/core/effects.d.ts +3 -0
  26. package/dist/core/envelope.d.ts +2 -0
  27. package/dist/core/envelope.js +2 -2
  28. package/dist/core/event-codec.d.ts +4 -3
  29. package/dist/core/event-codec.js +7 -7
  30. package/dist/core/fold.d.ts +3 -0
  31. package/dist/core/fold.js +3 -1
  32. package/dist/core/fork.js +1 -1
  33. package/dist/core/ids.d.ts +70 -5
  34. package/dist/core/ids.js +164 -41
  35. package/dist/core/snapshots.d.ts +2 -0
  36. package/dist/core/snapshots.js +2 -1
  37. package/dist/core/thread.d.ts +8 -0
  38. package/dist/debugger/app/assets/{index-GLtrulmj.js → index-D_FKxPrN.js} +2 -2
  39. package/dist/debugger/app/assets/routes-CkwzFejg.js +47 -0
  40. package/dist/debugger/app/index.html +1 -1
  41. package/dist/debugger/context.d.ts +4 -1
  42. package/dist/debugger/context.js +4 -4
  43. package/dist/debugger/shell/debugger-shell.d.ts +4 -1
  44. package/dist/debugger/shell/debugger-shell.js +2 -2
  45. package/dist/debugger/shell/use-start-run.d.ts +1 -1
  46. package/dist/debugger/shell/use-start-run.js +1 -2
  47. package/dist/evaluator/effects.d.ts +33 -33
  48. package/dist/evaluator/module.d.ts +66 -66
  49. package/dist/evaluator/steps.d.ts +2 -2
  50. package/dist/evaluator/steps.js +3 -3
  51. package/dist/react/react.d.ts +8 -3
  52. package/dist/react/react.js +15 -2
  53. package/dist/react/store.d.ts +5 -1
  54. package/dist/react/store.js +5 -4
  55. package/dist/runtime/cancellation.js +5 -5
  56. package/dist/runtime/deadline-transition-planner.d.ts +1 -0
  57. package/dist/runtime/deadline-transition-planner.js +8 -7
  58. package/dist/runtime/effect-admission.js +1 -0
  59. package/dist/runtime/helpers.d.ts +6 -5
  60. package/dist/runtime/helpers.js +8 -8
  61. package/dist/runtime/local-effect-executor.js +15 -10
  62. package/dist/runtime/looms.d.ts +6 -1
  63. package/dist/runtime/looms.js +4 -1
  64. package/dist/runtime/retry-policy.d.ts +2 -1
  65. package/dist/runtime/retry-policy.js +2 -1
  66. package/dist/runtime/runtime.js +10 -10
  67. package/dist/runtime/server/routes/runs.js +5 -5
  68. package/dist/runtime/types.d.ts +7 -1
  69. package/dist/runtime/wake-engine.js +10 -9
  70. package/dist/runtime/worker-callbacks.js +9 -8
  71. package/dist/runtime/worker-effect-dispatch.d.ts +3 -2
  72. package/dist/runtime/worker-effect-dispatch.js +7 -5
  73. package/dist/schedule/module.d.ts +36 -36
  74. package/dist/swarm/steps.d.ts +3 -3
  75. package/dist/swarm/steps.js +5 -5
  76. package/dist/swarm/tools.d.ts +3 -2
  77. package/dist/swarm/tools.js +13 -13
  78. package/dist/workflow/effects.js +6 -5
  79. package/dist/workflow/graph.d.ts +3 -3
  80. package/dist/workflow/graph.js +5 -5
  81. package/dist/workflow/module.d.ts +8 -2
  82. package/dist/workflow/threads.d.ts +14 -2
  83. package/dist/workflow/threads.js +54 -44
  84. package/package.json +1 -1
  85. package/dist/debugger/app/assets/routes-CECZJJob.js +0 -47
@@ -4,7 +4,7 @@
4
4
  <meta charset="UTF-8" />
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
6
  <title>Looms debugger</title>
7
- <script type="module" crossorigin src="./assets/index-GLtrulmj.js"></script>
7
+ <script type="module" crossorigin src="./assets/index-D_FKxPrN.js"></script>
8
8
  <link rel="modulepreload" crossorigin href="./assets/rolldown-runtime-CbXtAM7H.js">
9
9
  <link rel="stylesheet" crossorigin href="./assets/index-CO5gIvV8.css">
10
10
  </head>
@@ -1,5 +1,6 @@
1
1
  import { type ReactNode } from 'react';
2
2
  import { type LoomsClient } from '@swirls/looms/client';
3
+ import type { LoomsIds } from '@swirls/looms/core';
3
4
  import type { DebuggerPlugin } from './plugin';
4
5
  /** Where to land in a run another view links to. */
5
6
  export interface OpenRunTarget {
@@ -12,8 +13,10 @@ export interface DebuggerContextValue {
12
13
  readonly openRun?: (runId: string, target?: OpenRunTarget) => void;
13
14
  }
14
15
  /** Supplies one Looms host to both the API client and the run stores. */
15
- export declare function DebuggerProvider({ endpoint, plugins, openRun, children, }: {
16
+ export declare function DebuggerProvider({ endpoint, plugins, openRun, ids, children, }: {
16
17
  endpoint?: string;
18
+ /** The id format for runs and messages started here. Match the host's `ids`. */
19
+ ids?: LoomsIds;
17
20
  plugins: readonly DebuggerPlugin[];
18
21
  openRun?: (runId: string, target?: OpenRunTarget) => void;
19
22
  children: ReactNode;
@@ -4,7 +4,7 @@ import { createLoomsClient } from '@swirls/looms/client';
4
4
  import { LoomsProvider } from '@swirls/looms/react';
5
5
  const DebuggerContext = createContext(null);
6
6
  /** Supplies one Looms host to both the API client and the run stores. */
7
- export function DebuggerProvider({ endpoint = '', plugins, openRun, children, }) {
7
+ export function DebuggerProvider({ endpoint = '', plugins, openRun, ids, children, }) {
8
8
  const openRunRef = useRef(openRun);
9
9
  openRunRef.current = openRun;
10
10
  const hasOpenRun = openRun !== undefined;
@@ -12,13 +12,13 @@ export function DebuggerProvider({ endpoint = '', plugins, openRun, children, })
12
12
  openRunRef.current?.(runId, target);
13
13
  }, []);
14
14
  const value = useMemo(() => {
15
- const client = createLoomsClient({ baseUrl: endpoint });
15
+ const client = createLoomsClient({ baseUrl: endpoint, ids });
16
16
  if (!hasOpenRun) {
17
17
  return { client, plugins };
18
18
  }
19
19
  return { client, plugins, openRun: stableOpenRun };
20
- }, [endpoint, plugins, hasOpenRun, stableOpenRun]);
21
- return (_jsx(LoomsProvider, { endpoint: endpoint, children: _jsx(DebuggerContext.Provider, { value: value, children: children }) }));
20
+ }, [endpoint, ids, plugins, hasOpenRun, stableOpenRun]);
21
+ return (_jsx(LoomsProvider, { endpoint: endpoint, ids: ids, children: _jsx(DebuggerContext.Provider, { value: value, children: children }) }));
22
22
  }
23
23
  export function useDebugger() {
24
24
  const value = useContext(DebuggerContext);
@@ -1,3 +1,4 @@
1
+ import type { LoomsIds } from '@swirls/looms/core';
1
2
  import type { DebuggerPlugin } from '../plugin';
2
3
  export interface DebuggerSelection {
3
4
  readonly kind?: string;
@@ -6,10 +7,12 @@ export interface DebuggerSelection {
6
7
  readonly thread?: string;
7
8
  readonly seq?: number;
8
9
  }
9
- export declare function DebuggerShell({ plugins, selection, onSelectionChange, endpoint, }: {
10
+ export declare function DebuggerShell({ plugins, selection, onSelectionChange, endpoint, ids, }: {
10
11
  plugins: readonly DebuggerPlugin[];
11
12
  selection: DebuggerSelection;
12
13
  onSelectionChange: (selection: DebuggerSelection) => void;
13
14
  /** Looms host base URL. Defaults to the page origin. */
14
15
  endpoint?: string;
16
+ /** The id format for runs started here. Match the host's `ids`. Defaults to `typeIds`. */
17
+ ids?: LoomsIds;
15
18
  }): import("react").JSX.Element;
@@ -38,6 +38,6 @@ function DebuggerFrame({ selection, onSelectionChange, }) {
38
38
  run: run.runId,
39
39
  }) }), _jsxs("div", { className: "ml-auto flex min-w-0 items-center gap-2", children: [error ? (_jsx("p", { className: "text-status-failed truncate text-[11px]", title: error, children: error })) : null, selection.run ? (_jsx(RunControls, { runId: selection.run, onForked: (run) => onSelectionChange({ kind, name, run }) })) : null] })] }), _jsxs(ResizablePanelGroup, { id: "looms-debugger-columns", className: "min-h-0 flex-1", defaultLayout: layout.defaultLayout, onLayoutChanged: layout.onLayoutChanged, children: [_jsx(ResizablePanel, { id: "tree", defaultSize: "17%", minSize: "12rem", maxSize: "30%", className: "bg-sidebar", children: _jsx("section", { "aria-label": "Run tree", className: "h-full min-h-0 overflow-hidden", children: selection.run ? (_jsx(RunTreePane, { runId: selection.run, threadId: selection.thread, onSelectThread: (thread) => onSelectionChange({ ...selection, thread, seq: undefined }) })) : (_jsxs("div", { className: "flex h-full flex-col", children: [_jsx(PanelHeader, { children: _jsx(PanelTitle, { children: "Run tree" }) }), _jsx(EmptyState, { icon: _jsx(ListTree, {}), title: "No run selected", children: "Threads and child runs appear here once a run starts." })] })) }) }), _jsx(ResizableHandle, {}), _jsx(ResizablePanel, { id: "workspace", defaultSize: "29%", minSize: "16rem", children: _jsx("section", { "aria-label": "Workspace", className: "@container h-full min-h-0 overflow-hidden", children: _jsx(Workspace, { definition: selected, runId: selection.run, onStarted: (run) => onSelectionChange({ kind, name, run }) }) }) }), _jsx(ResizableHandle, {}), _jsx(ResizablePanel, { id: "events", defaultSize: "32%", minSize: "16rem", children: _jsx("section", { "aria-label": "Events", className: "h-full min-h-0 overflow-hidden", children: selection.run ? (_jsx(EventsPane, { runId: selection.run, threadId: selection.thread, seq: selection.seq, onSelectSeq: (seq) => onSelectionChange({ ...selection, seq }), onClearThread: () => onSelectionChange({ ...selection, thread: undefined }) })) : (_jsxs("div", { className: "flex h-full flex-col", children: [_jsx(PanelHeader, { children: _jsx(PanelTitle, { children: "Events" }) }), _jsx(EmptyState, { icon: _jsx(Activity, {}), title: "No events yet", children: "Events stream in live once a run starts." })] })) }) }), _jsx(ResizableHandle, {}), _jsx(ResizablePanel, { id: "projections", defaultSize: "22%", minSize: "14rem", className: "bg-sidebar", children: _jsx("section", { "aria-label": "Projections", className: "h-full min-h-0 overflow-hidden", children: _jsx(ProjectionsPanel, { runId: selection.run, names: catalog.projections }) }) })] })] }));
40
40
  }
41
- export function DebuggerShell({ plugins, selection, onSelectionChange, endpoint, }) {
42
- return (_jsx(DebuggerProvider, { endpoint: endpoint, plugins: plugins, openRun: (runId, target) => onSelectionChange({ run: runId, seq: target?.seq }), children: _jsx(DebuggerFrame, { selection: selection, onSelectionChange: onSelectionChange }) }));
41
+ export function DebuggerShell({ plugins, selection, onSelectionChange, endpoint, ids, }) {
42
+ return (_jsx(DebuggerProvider, { endpoint: endpoint, ids: ids, plugins: plugins, openRun: (runId, target) => onSelectionChange({ run: runId, seq: target?.seq }), children: _jsx(DebuggerFrame, { selection: selection, onSelectionChange: onSelectionChange }) }));
43
43
  }
@@ -1,4 +1,4 @@
1
- import { type JsonValue } from '@swirls/looms/core';
1
+ import type { JsonValue } from '@swirls/looms/core';
2
2
  import type { WorkspaceContext } from '../plugin';
3
3
  export interface StartedRun {
4
4
  readonly runId: string;
@@ -1,5 +1,4 @@
1
1
  import { useState } from 'react';
2
- import { createRunId } from '@swirls/looms/core';
3
2
  import { useDebugger } from '../context.js';
4
3
  import { errorMessage } from '../lib/cn.js';
5
4
  export function useStartRun({ definition, onStarted }) {
@@ -7,7 +6,7 @@ export function useStartRun({ definition, onStarted }) {
7
6
  const [pending, setPending] = useState(false);
8
7
  const [error, setError] = useState(null);
9
8
  function start(input, after) {
10
- const runId = createRunId();
9
+ const runId = client.ids.create('run');
11
10
  setPending(true);
12
11
  setError(null);
13
12
  onStarted(runId);
@@ -7,39 +7,6 @@ export declare const evaluateEffect: NoInfer<import("@swirls/looms/core").LocalE
7
7
  readonly definitionVersion?: string | undefined;
8
8
  readonly input?: Schema.Json | undefined;
9
9
  }, EvaluatorDefinitionsTag | EvaluatorTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
10
- readonly type: "evaluator.evaluated";
11
- readonly payload: {
12
- readonly evaluationId: string;
13
- readonly definitionName?: string | undefined;
14
- readonly answers: {
15
- readonly [x: string]: {
16
- readonly type: "boolean";
17
- readonly value: boolean;
18
- readonly probability: number;
19
- } | {
20
- readonly type: "choice";
21
- readonly value: string;
22
- } | {
23
- readonly type: "score";
24
- readonly value: number;
25
- };
26
- };
27
- readonly route: string;
28
- readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
29
- readonly usage?: {
30
- readonly input: number;
31
- readonly output: number;
32
- } | undefined;
33
- };
34
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
35
- readonly type: "evaluator.requested";
36
- readonly payload: {
37
- readonly evaluationId: string;
38
- readonly definitionName?: string | undefined;
39
- readonly state: string;
40
- readonly model?: string | undefined;
41
- };
42
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
43
10
  readonly type: "runtime.child.close.requested";
44
11
  readonly payload: {
45
12
  readonly policy: "terminate" | "wait";
@@ -285,4 +252,37 @@ export declare const evaluateEffect: NoInfer<import("@swirls/looms/core").LocalE
285
252
  readonly payload: Schema.Json;
286
253
  };
287
254
  };
255
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
256
+ readonly type: "evaluator.evaluated";
257
+ readonly payload: {
258
+ readonly evaluationId: string;
259
+ readonly definitionName?: string | undefined;
260
+ readonly answers: {
261
+ readonly [x: string]: {
262
+ readonly type: "boolean";
263
+ readonly value: boolean;
264
+ readonly probability: number;
265
+ } | {
266
+ readonly type: "choice";
267
+ readonly value: string;
268
+ } | {
269
+ readonly type: "score";
270
+ readonly value: number;
271
+ };
272
+ };
273
+ readonly route: string;
274
+ readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
275
+ readonly usage?: {
276
+ readonly input: number;
277
+ readonly output: number;
278
+ } | undefined;
279
+ };
280
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
281
+ readonly type: "evaluator.requested";
282
+ readonly payload: {
283
+ readonly evaluationId: string;
284
+ readonly definitionName?: string | undefined;
285
+ readonly state: string;
286
+ readonly model?: string | undefined;
287
+ };
288
288
  })>>;
@@ -43,39 +43,6 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
43
43
  readonly definitionVersion?: string | undefined;
44
44
  readonly input?: import("effect/Schema").Json | undefined;
45
45
  }, import("./definitions-store").EvaluatorDefinitionsTag | EvaluatorTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
46
- readonly type: "evaluator.evaluated";
47
- readonly payload: {
48
- readonly evaluationId: string;
49
- readonly definitionName?: string | undefined;
50
- readonly answers: {
51
- readonly [x: string]: {
52
- readonly type: "boolean";
53
- readonly value: boolean;
54
- readonly probability: number;
55
- } | {
56
- readonly type: "choice";
57
- readonly value: string;
58
- } | {
59
- readonly type: "score";
60
- readonly value: number;
61
- };
62
- };
63
- readonly route: string;
64
- readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
65
- readonly usage?: {
66
- readonly input: number;
67
- readonly output: number;
68
- } | undefined;
69
- };
70
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
71
- readonly type: "evaluator.requested";
72
- readonly payload: {
73
- readonly evaluationId: string;
74
- readonly definitionName?: string | undefined;
75
- readonly state: string;
76
- readonly model?: string | undefined;
77
- };
78
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
79
46
  readonly type: "runtime.child.close.requested";
80
47
  readonly payload: {
81
48
  readonly policy: "terminate" | "wait";
@@ -321,6 +288,39 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
321
288
  readonly payload: import("effect/Schema").Json;
322
289
  };
323
290
  };
291
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
292
+ readonly type: "evaluator.evaluated";
293
+ readonly payload: {
294
+ readonly evaluationId: string;
295
+ readonly definitionName?: string | undefined;
296
+ readonly answers: {
297
+ readonly [x: string]: {
298
+ readonly type: "boolean";
299
+ readonly value: boolean;
300
+ readonly probability: number;
301
+ } | {
302
+ readonly type: "choice";
303
+ readonly value: string;
304
+ } | {
305
+ readonly type: "score";
306
+ readonly value: number;
307
+ };
308
+ };
309
+ readonly route: string;
310
+ readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
311
+ readonly usage?: {
312
+ readonly input: number;
313
+ readonly output: number;
314
+ } | undefined;
315
+ };
316
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
317
+ readonly type: "evaluator.requested";
318
+ readonly payload: {
319
+ readonly evaluationId: string;
320
+ readonly definitionName?: string | undefined;
321
+ readonly state: string;
322
+ readonly model?: string | undefined;
323
+ };
324
324
  })>;
325
325
  }, {
326
326
  evaluator: import("@swirls/looms/core").ThreadDefinition<{
@@ -391,39 +391,6 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
391
391
  readonly definitionVersion?: string | undefined;
392
392
  readonly input?: import("effect/Schema").Json | undefined;
393
393
  }, import("./definitions-store").EvaluatorDefinitionsTag | EvaluatorTag, (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
394
- readonly type: "evaluator.evaluated";
395
- readonly payload: {
396
- readonly evaluationId: string;
397
- readonly definitionName?: string | undefined;
398
- readonly answers: {
399
- readonly [x: string]: {
400
- readonly type: "boolean";
401
- readonly value: boolean;
402
- readonly probability: number;
403
- } | {
404
- readonly type: "choice";
405
- readonly value: string;
406
- } | {
407
- readonly type: "score";
408
- readonly value: number;
409
- };
410
- };
411
- readonly route: string;
412
- readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
413
- readonly usage?: {
414
- readonly input: number;
415
- readonly output: number;
416
- } | undefined;
417
- };
418
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
419
- readonly type: "evaluator.requested";
420
- readonly payload: {
421
- readonly evaluationId: string;
422
- readonly definitionName?: string | undefined;
423
- readonly state: string;
424
- readonly model?: string | undefined;
425
- };
426
- }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
427
394
  readonly type: "runtime.child.close.requested";
428
395
  readonly payload: {
429
396
  readonly policy: "terminate" | "wait";
@@ -669,6 +636,39 @@ export declare function evaluator(options?: EvaluatorModuleOptions): import("@sw
669
636
  readonly payload: import("effect/Schema").Json;
670
637
  };
671
638
  };
639
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
640
+ readonly type: "evaluator.evaluated";
641
+ readonly payload: {
642
+ readonly evaluationId: string;
643
+ readonly definitionName?: string | undefined;
644
+ readonly answers: {
645
+ readonly [x: string]: {
646
+ readonly type: "boolean";
647
+ readonly value: boolean;
648
+ readonly probability: number;
649
+ } | {
650
+ readonly type: "choice";
651
+ readonly value: string;
652
+ } | {
653
+ readonly type: "score";
654
+ readonly value: number;
655
+ };
656
+ };
657
+ readonly route: string;
658
+ readonly reason: "model" | "skipped" | "unavailable" | "uncertain";
659
+ readonly usage?: {
660
+ readonly input: number;
661
+ readonly output: number;
662
+ } | undefined;
663
+ };
664
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
665
+ readonly type: "evaluator.requested";
666
+ readonly payload: {
667
+ readonly evaluationId: string;
668
+ readonly definitionName?: string | undefined;
669
+ readonly state: string;
670
+ readonly model?: string | undefined;
671
+ };
672
672
  })>;
673
673
  };
674
674
  readonly threads: {
@@ -1,3 +1,3 @@
1
- import { type JsonValue, type RuntimeEffect } from '@swirls/looms/core';
1
+ import { type JsonValue, type LoomsIds, type RuntimeEffect } from '@swirls/looms/core';
2
2
  import type { EvaluatorDefinition } from './definitions';
3
- export declare function evaluate(definition: EvaluatorDefinition, input: JsonValue): RuntimeEffect[];
3
+ export declare function evaluate(definition: EvaluatorDefinition, input: JsonValue, ids?: LoomsIds): RuntimeEffect[];
@@ -1,7 +1,7 @@
1
- import { createWaitId, invoke, wait } from '@swirls/looms/core';
1
+ import { createWaitId, invoke, typeIds, wait, } from '@swirls/looms/core';
2
2
  import { evaluateEffect } from './effects.js';
3
- export function evaluate(definition, input) {
4
- const evaluationId = createWaitId('evaluator');
3
+ export function evaluate(definition, input, ids = typeIds) {
4
+ const evaluationId = ids.create('wait');
5
5
  return [
6
6
  invoke(evaluateEffect, {
7
7
  evaluationId,
@@ -1,6 +1,6 @@
1
1
  import { type ReactNode } from 'react';
2
2
  import type { SubscriptionSocketConstructor } from '@swirls/looms/client';
3
- import { type AnyEventEnvelope, type ProjectionDefinition, type RegisteredEvent, type RunAttributes, type RunLaunchSource, type RunStatus, type SubscriptionTransport, type ThreadTree } from '@swirls/looms/core';
3
+ import { type AnyEventEnvelope, type LoomsIds, type ProjectionDefinition, type RegisteredEvent, type RunAttributes, type RunLaunchSource, type RunStatus, type SubscriptionTransport, type ThreadTree } from '@swirls/looms/core';
4
4
  import type { EventCache } from './cache';
5
5
  import { defineLiveFold, foldLive, type LiveFoldDefinition } from './derived';
6
6
  import type { StoreObserver } from './observer';
@@ -29,6 +29,8 @@ export interface LoomsProviderProps {
29
29
  * store connects. See `LoomsClientStoreOptions.grant`.
30
30
  */
31
31
  grant?: RunGrantSource;
32
+ /** The id format run stores draw from. See `LoomsClientStoreOptions.ids`. */
33
+ ids?: LoomsIds;
32
34
  }
33
35
  /** What gives a run's current read grant, or undefined when it has none. */
34
36
  export type RunGrantSource = (runId: string) => string | undefined;
@@ -37,6 +39,7 @@ interface StoreReach {
37
39
  readonly fetch?: typeof fetch;
38
40
  readonly webSocket?: SubscriptionSocketConstructor;
39
41
  readonly grant?: RunGrantSource;
42
+ readonly ids?: LoomsIds;
40
43
  }
41
44
  interface ProviderContextValue extends StoreReach {
42
45
  endpoint: string;
@@ -47,7 +50,7 @@ interface ProviderContextValue extends StoreReach {
47
50
  observer?: StoreObserver;
48
51
  }
49
52
  /** Root provider that supplies the Looms host endpoint to run stores. */
50
- export declare function LoomsProvider({ children, endpoint, coalesce, transport, retainMs, cache, observer, fetch, webSocket, grant, }: LoomsProviderProps): import("react").FunctionComponentElement<import("react").ProviderProps<ProviderContextValue>>;
53
+ export declare function LoomsProvider({ children, endpoint, coalesce, transport, retainMs, cache, observer, fetch, webSocket, grant, ids, }: LoomsProviderProps): import("react").FunctionComponentElement<import("react").ProviderProps<ProviderContextValue>>;
51
54
  export interface UseRunStoreOptions {
52
55
  endpoint?: string;
53
56
  reconnectDelayMs?: number;
@@ -62,7 +65,7 @@ export interface UseRunStoreOptions {
62
65
  /**
63
66
  * Everything that decides which store a run gets when there is no provider to supply part of
64
67
  * it. Inside React the provider gives `endpoint`, `coalesce`, `transport`, `retainMs`, `cache`
65
- * `observer`, `fetch`, `webSocket` and `grant`; outside it the caller passes them.
68
+ * `observer`, `fetch`, `webSocket`, `grant` and `ids`; outside it the caller passes them.
66
69
  */
67
70
  export interface RunStoreOptions extends UseRunStoreOptions {
68
71
  /** Makes every HTTP request the store sends. See `LoomsClientStoreOptions.fetch`. */
@@ -71,6 +74,8 @@ export interface RunStoreOptions extends UseRunStoreOptions {
71
74
  webSocket?: SubscriptionSocketConstructor;
72
75
  /** The read grant for a run. See `LoomsClientStoreOptions.grant`. */
73
76
  grant?: RunGrantSource;
77
+ /** The id format the store draws from. See `LoomsClientStoreOptions.ids`. */
78
+ ids?: LoomsIds;
74
79
  /** Keeps the run's durable events on the device. See `indexedDbEventCache`. */
75
80
  cache?: EventCache;
76
81
  /** Told how long the run took to show and how long each commit took. */
@@ -46,6 +46,7 @@ function peekStore(runId, identity) {
46
46
  fetch: identity.fetch,
47
47
  webSocket: identity.webSocket,
48
48
  grant: identity.grant === undefined ? undefined : () => identity.grant?.(runId),
49
+ ids: identity.ids,
49
50
  });
50
51
  const retainMs = Math.max(0, identity.retainMs ?? DEFAULT_RETAIN_MS);
51
52
  registry.set(key, {
@@ -82,8 +83,19 @@ function releaseStore(runId, identity) {
82
83
  }
83
84
  }
84
85
  /** Root provider that supplies the Looms host endpoint to run stores. */
85
- export function LoomsProvider({ children, endpoint = '', coalesce, transport, retainMs, cache, observer, fetch, webSocket, grant, }) {
86
- const value = useMemo(() => ({ endpoint, coalesce, transport, retainMs, cache, observer, fetch, webSocket, grant }), [endpoint, coalesce, transport, retainMs, cache, observer, fetch, webSocket, grant]);
86
+ export function LoomsProvider({ children, endpoint = '', coalesce, transport, retainMs, cache, observer, fetch, webSocket, grant, ids, }) {
87
+ const value = useMemo(() => ({
88
+ endpoint,
89
+ coalesce,
90
+ transport,
91
+ retainMs,
92
+ cache,
93
+ observer,
94
+ fetch,
95
+ webSocket,
96
+ grant,
97
+ ids,
98
+ }), [endpoint, coalesce, transport, retainMs, cache, observer, fetch, webSocket, grant, ids]);
87
99
  return createElement(LoomsContext.Provider, { value, children });
88
100
  }
89
101
  /**
@@ -166,6 +178,7 @@ export function useRunStore(runId, options) {
166
178
  fetch: context.fetch,
167
179
  webSocket: context.webSocket,
168
180
  grant: context.grant,
181
+ ids: context.ids,
169
182
  };
170
183
  const store = peekStore(runId, identity);
171
184
  useEffect(() => {
@@ -1,5 +1,5 @@
1
1
  import { type SubscriptionSocketConstructor } from '@swirls/looms/client';
2
- import { type AnyEventEnvelope, type EventInput, type ProjectionDefinition, type RegisteredEvent, type SubscriptionTransport } from '@swirls/looms/core';
2
+ import { type LoomsIds, type AnyEventEnvelope, type EventInput, type ProjectionDefinition, type RegisteredEvent, type SubscriptionTransport } from '@swirls/looms/core';
3
3
  import type { EventCache } from './cache';
4
4
  import { type LiveFoldDefinition } from './derived';
5
5
  import { type CoalesceOption } from './notify';
@@ -76,6 +76,10 @@ export interface LoomsClientStoreOptions {
76
76
  fromSeq?: number;
77
77
  /** Whether `commit` shows its events before the host confirms them. Defaults to false. */
78
78
  optimistic?: boolean;
79
+ /**
80
+ * The id format for the event ids and idempotency keys of optimistic commits. Match the host's `ids`. Defaults to `typeIds` (TypeID).
81
+ */
82
+ ids?: LoomsIds;
79
83
  /**
80
84
  * Keeps the run's durable events on the device. A store that has followed the run before starts
81
85
  * from them and downloads only what was appended since. Not used together with `fromSeq`.
@@ -1,6 +1,6 @@
1
1
  import { Data, DateTime, Effect, Fiber, Option, Predicate, Schema } from 'effect';
2
2
  import { absoluteHttpBase } from '@swirls/looms/client';
3
- import { createEvent, createEventId, decodeLoomsEvent, readSubscriptionData, stringifyJson, } from '@swirls/looms/core';
3
+ import { createEvent, resolveIds, decodeLoomsEvent, readSubscriptionData, stringifyJson, } from '@swirls/looms/core';
4
4
  import { EventIndex, ProjectionCache } from './derived.js';
5
5
  import { materializeEvents } from './materialize.js';
6
6
  import { createNotifyScheduler } from './notify.js';
@@ -53,6 +53,7 @@ function eventSourceCtor() {
53
53
  export function createLoomsStore(options) {
54
54
  const endpoint = options.endpoint.replace(/\/$/, '');
55
55
  const fetchFn = options.fetch ?? fetch;
56
+ const ids = resolveIds(options.ids);
56
57
  const reconnectDelayMs = options.reconnectDelayMs ?? 1_000;
57
58
  const coalesce = options.coalesce ?? 'adaptive';
58
59
  const listeners = new Set();
@@ -334,7 +335,7 @@ export function createLoomsStore(options) {
334
335
  }
335
336
  const body = decodedBody.value;
336
337
  const pulledEvents = 'events' in body && Array.isArray(body.events) ? body.events : [];
337
- applyBatch(pulledEvents.map((raw) => decodeLoomsEvent(raw, options.storeId)));
338
+ applyBatch(pulledEvents.map((raw) => decodeLoomsEvent(raw, options.storeId, ids)));
338
339
  return undefined;
339
340
  }).pipe(Effect.catchIf((error) => disposed ||
340
341
  error.message.includes('fetch failed') ||
@@ -603,7 +604,7 @@ export function createLoomsStore(options) {
603
604
  commit: (event, commitOptions) => {
604
605
  const inputs = Array.isArray(event) ? event : [event];
605
606
  const optimistic = commitOptions?.optimistic ?? options.optimistic ?? false;
606
- const key = commitOptions?.idempotencyKey ?? (optimistic ? createEventId() : undefined);
607
+ const key = commitOptions?.idempotencyKey ?? (optimistic ? ids.create('event') : undefined);
607
608
  const startedAt = nowMs();
608
609
  let shown;
609
610
  if (optimistic && key !== undefined && inputs.length > 0) {
@@ -619,7 +620,7 @@ export function createLoomsStore(options) {
619
620
  threadId: input.threadId === undefined ? rootThreadId : input.threadId,
620
621
  origin: input.origin ?? { type: 'external' },
621
622
  idempotencyKey: input.idempotencyKey ?? key,
622
- }, { seq: pendingSeq });
623
+ }, { seq: pendingSeq, ids });
623
624
  }),
624
625
  };
625
626
  pendingCommits.push(shown);
@@ -34,7 +34,7 @@ function planCancellation(input) {
34
34
  effectId: item.effectId,
35
35
  origin: { type: 'system' },
36
36
  idempotencyKey,
37
- }));
37
+ }, { ids: registry.ids }));
38
38
  continue;
39
39
  }
40
40
  const timeoutMs = registry.effects.get(item.effect.type)?.retry?.cancellationTimeoutMs;
@@ -49,7 +49,7 @@ function planCancellation(input) {
49
49
  threadId: item.threadId,
50
50
  origin: { type: 'system' },
51
51
  idempotencyKey,
52
- }));
52
+ }, { ids: registry.ids }));
53
53
  }
54
54
  const threadEvents = [...descendants.toReversed(), targetThreadId]
55
55
  .filter((threadId) => threadId.length > 0)
@@ -61,11 +61,11 @@ function planCancellation(input) {
61
61
  threadId,
62
62
  origin: { type: 'external' },
63
63
  idempotencyKey,
64
- });
64
+ }, { ids: registry.ids });
65
65
  });
66
66
  const batch = [...effectEvents, ...threadEvents];
67
67
  const folded = foldRun(batch, registry, { runId, initial: state });
68
- const satisfied = waitSatisfiedEvents(folded, batch).map((event) => createEvent(runId, event));
68
+ const satisfied = waitSatisfiedEvents(folded, batch).map((event) => createEvent(runId, event, { ids: registry.ids }));
69
69
  return { events: [...batch, ...satisfied], threadIds, workerCancellations };
70
70
  }
71
71
  export function createCancellation(options) {
@@ -86,7 +86,7 @@ export function createCancellation(options) {
86
86
  threadId: current.state.rootThreadId,
87
87
  origin: { type: 'system' },
88
88
  idempotencyKey: `child-close:${current.state.rootThreadId ?? runId}`,
89
- });
89
+ }, { ids: registry.ids });
90
90
  return Effect.succeed({
91
91
  events: [requested],
92
92
  value: {
@@ -22,6 +22,7 @@ export declare function planDeadlineTransitions(input: {
22
22
  export declare function planTerminalCancellationTransitions(input: {
23
23
  readonly runId: string;
24
24
  readonly state: RunState;
25
+ readonly registry: ComposedRegistry;
25
26
  readonly now: number;
26
27
  }): ReadonlyArray<EventEnvelope>;
27
28
  /**
@@ -25,14 +25,14 @@ function planDueTimers(input) {
25
25
  payload: { timerId: record.waitId, waitId: record.waitId },
26
26
  threadId: record.threadId,
27
27
  origin: { type: 'system' },
28
- }),
28
+ }, { ids: input.registry.ids }),
29
29
  ]
30
30
  : []);
31
31
  const stateAfterFired = foldRun(fired, input.registry, {
32
32
  runId: input.runId,
33
33
  initial: input.state,
34
34
  });
35
- const satisfied = waitSatisfiedEvents(stateAfterFired, fired).map((event) => createEvent(input.runId, event));
35
+ const satisfied = waitSatisfiedEvents(stateAfterFired, fired).map((event) => createEvent(input.runId, event, { ids: input.registry.ids }));
36
36
  return {
37
37
  events: [...fired, ...satisfied],
38
38
  timerNotices: due.flatMap((record) => isWaitOnTimer(record.on)
@@ -57,7 +57,7 @@ function planExpiredExecutions(input) {
57
57
  const item = input.state.outstandingEffects.find((candidate) => candidate.effectId === execution.effectId);
58
58
  if (!item) {
59
59
  if (execution.status === 'cancel_requested') {
60
- events.push(cancellationAmbiguous(input.runId, execution.effectId, execution.attempt));
60
+ events.push(cancellationAmbiguous(input.runId, execution.effectId, execution.attempt, input.registry.ids));
61
61
  }
62
62
  continue;
63
63
  }
@@ -74,8 +74,9 @@ function planExpiredExecutions(input) {
74
74
  threadId: item.threadId,
75
75
  causationId: item.causingEventId,
76
76
  origin: { type: 'system' },
77
- }));
77
+ }, { ids: input.registry.ids }));
78
78
  events.push(planAttemptFailure({
79
+ ids: input.registry.ids,
79
80
  runId: input.runId,
80
81
  item,
81
82
  attempt: execution.attempt,
@@ -106,7 +107,7 @@ export function planTerminalCancellationTransitions(input) {
106
107
  .filter((execution) => execution.status === 'cancel_requested' &&
107
108
  execution.deadlineAt !== null &&
108
109
  execution.deadlineAt <= input.now)
109
- .map((execution) => cancellationAmbiguous(input.runId, execution.effectId, execution.attempt));
110
+ .map((execution) => cancellationAmbiguous(input.runId, execution.effectId, execution.attempt, input.registry.ids));
110
111
  }
111
112
  /**
112
113
  * The earliest deadline after `after` that needs a wake. A terminal run only waits on
@@ -134,7 +135,7 @@ export function nextRuntimeDeadline(state, after = Number.NEGATIVE_INFINITY) {
134
135
  const deadlines = [...timers, ...executions, ...deferred];
135
136
  return deadlines.length > 0 ? Math.min(...deadlines) : undefined;
136
137
  }
137
- function cancellationAmbiguous(runId, effectId, attempt) {
138
+ function cancellationAmbiguous(runId, effectId, attempt, ids) {
138
139
  return createEvent(runId, {
139
140
  type: 'runtime.effect.ambiguous',
140
141
  payload: {
@@ -144,5 +145,5 @@ function cancellationAmbiguous(runId, effectId, attempt) {
144
145
  },
145
146
  threadId: null,
146
147
  origin: { type: 'system' },
147
- });
148
+ }, { ids });
148
149
  }
@@ -60,6 +60,7 @@ export function admitOrFailAttempt(input) {
60
60
  return { _tag: 'admit', secrets };
61
61
  }
62
62
  const failure = planAttemptFailure({
63
+ ids: input.registry.ids,
63
64
  runId: input.runId,
64
65
  item: input.item,
65
66
  attempt: input.attempt,