@swirls/looms 0.9.0 → 0.10.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 (82) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/actor/routing.js +2 -1
  3. package/dist/agent/debugger/agent-chat.js +6 -5
  4. package/dist/agent/debugger/index.js +2 -1
  5. package/dist/agent/module.d.ts +799 -593
  6. package/dist/agent/module.js +31 -0
  7. package/dist/agent/scope.js +1 -0
  8. package/dist/agent/signals.d.ts +108 -15
  9. package/dist/agent/signals.js +70 -47
  10. package/dist/approval/debugger/approvals-view.js +1 -1
  11. package/dist/approval/index.d.ts +1 -0
  12. package/dist/approval/index.js +1 -0
  13. package/dist/approval/module.d.ts +42 -1
  14. package/dist/approval/module.js +53 -0
  15. package/dist/approval/scope.js +1 -0
  16. package/dist/approval/signals.d.ts +19 -13
  17. package/dist/approval/signals.js +25 -45
  18. package/dist/approval/steps.d.ts +11 -0
  19. package/dist/approval/steps.js +39 -0
  20. package/dist/cli/commands.js +2 -2
  21. package/dist/client/client.d.ts +211 -0
  22. package/dist/client/client.js +18 -1
  23. package/dist/core/index.d.ts +1 -0
  24. package/dist/core/index.js +1 -0
  25. package/dist/core/module-scope.d.ts +50 -5
  26. package/dist/core/module-scope.js +22 -2
  27. package/dist/core/module.d.ts +29 -2
  28. package/dist/core/projection.d.ts +1 -0
  29. package/dist/core/published.d.ts +59 -1
  30. package/dist/core/published.js +97 -1
  31. package/dist/core/signals.d.ts +56 -0
  32. package/dist/core/signals.js +93 -0
  33. package/dist/core/validate-event.d.ts +12 -1
  34. package/dist/core/validate-event.js +34 -11
  35. package/dist/debugger/app/assets/index-CO5gIvV8.css +2 -0
  36. package/dist/debugger/app/assets/{index-CMRl_aWQ.js → index-D-2Q7ecG.js} +2 -2
  37. package/dist/debugger/app/assets/routes-lvmllT60.js +47 -0
  38. package/dist/debugger/app/index.html +2 -2
  39. package/dist/debugger/index.d.ts +1 -1
  40. package/dist/debugger/index.js +1 -1
  41. package/dist/debugger/lib/cn.d.ts +1 -1
  42. package/dist/debugger/lib/cn.js +12 -1
  43. package/dist/debugger/plugin.d.ts +11 -1
  44. package/dist/debugger/plugin.js +41 -0
  45. package/dist/debugger/shell/debugger-shell.js +1 -1
  46. package/dist/debugger/shell/form-fields.d.ts +17 -0
  47. package/dist/debugger/shell/form-fields.js +38 -0
  48. package/dist/debugger/shell/run-action.d.ts +6 -0
  49. package/dist/debugger/shell/run-action.js +15 -0
  50. package/dist/debugger/shell/run-controls.js +3 -55
  51. package/dist/debugger/shell/schema-form.d.ts +10 -1
  52. package/dist/debugger/shell/schema-form.js +108 -10
  53. package/dist/debugger/shell/signal-form.d.ts +18 -0
  54. package/dist/debugger/shell/signal-form.js +185 -0
  55. package/dist/debugger/shell/start-form.js +2 -32
  56. package/dist/debugger/shell/use-debounced-preview.d.ts +22 -0
  57. package/dist/debugger/shell/use-debounced-preview.js +68 -0
  58. package/dist/evaluator/index.d.ts +1 -1
  59. package/dist/evaluator/index.js +1 -1
  60. package/dist/evaluator/module.d.ts +8 -1
  61. package/dist/evaluator/module.js +33 -0
  62. package/dist/evaluator/scope.js +1 -0
  63. package/dist/runtime/server/routes/definitions.d.ts +1 -1
  64. package/dist/runtime/server/routes/definitions.js +58 -2
  65. package/dist/runtime/server/routes/runs.js +25 -1
  66. package/dist/schedule/module.d.ts +117 -1
  67. package/dist/schedule/module.js +30 -0
  68. package/dist/schedule/scope.js +1 -0
  69. package/dist/schedule/signals.d.ts +59 -7
  70. package/dist/schedule/signals.js +35 -17
  71. package/dist/testing/corpus-module.d.ts +1 -1
  72. package/dist/testing/index.js +2 -1
  73. package/dist/testing/module-examples.d.ts +6 -0
  74. package/dist/testing/module-examples.js +66 -0
  75. package/dist/workflow/module.d.ts +3 -1
  76. package/dist/workflow/module.js +13 -0
  77. package/dist/workflow/scope.js +1 -0
  78. package/package.json +1 -1
  79. package/dist/debugger/app/assets/index-B7GdT1UA.css +0 -2
  80. package/dist/debugger/app/assets/routes-CfXxKG45.js +0 -47
  81. /package/dist/evaluator/{signals.d.ts → steps.d.ts} +0 -0
  82. /package/dist/evaluator/{signals.js → steps.js} +0 -0
@@ -0,0 +1,39 @@
1
+ import { createWaitId, invoke, wait } from '@swirls/looms/core';
2
+ import { requestApprovalEffect } from './effects.js';
3
+ export function gate(args) {
4
+ const approvalId = args.approvalId ?? createWaitId('approval');
5
+ const actions = args.actions ?? [
6
+ { id: 'approve', label: 'Approve', outcome: 'approve' },
7
+ { id: 'reject', label: 'Reject', outcome: 'reject' },
8
+ ];
9
+ const effects = [
10
+ invoke(requestApprovalEffect, {
11
+ approvalId,
12
+ title: args.title,
13
+ description: args.description,
14
+ actions,
15
+ schema: args.schema,
16
+ }, `request_${approvalId}`),
17
+ wait({
18
+ waitId: createWaitId(approvalId, 'decision'),
19
+ on: { type: 'approval.decided', match: { approvalId } },
20
+ tag: { approvalId, raceId: approvalId },
21
+ }),
22
+ ];
23
+ if (args.timeoutAt !== undefined) {
24
+ if (!Number.isFinite(args.timeoutAt)) {
25
+ throw new Error('Approval timeoutAt must be a finite epoch timestamp');
26
+ }
27
+ effects.push(wait({
28
+ waitId: createWaitId(approvalId, 'timeout'),
29
+ on: { timerAt: args.timeoutAt },
30
+ tag: {
31
+ approvalId,
32
+ raceId: approvalId,
33
+ outcome: 'timeout',
34
+ error: `Approval ${approvalId} timed out`,
35
+ },
36
+ }));
37
+ }
38
+ return effects;
39
+ }
@@ -76,7 +76,7 @@ const approve = Command.make('approve', {
76
76
  }
77
77
  const outcome = accept ? 'approve' : 'reject';
78
78
  const { url } = yield* loomsBase;
79
- const result = yield* tryClient(() => clientFor(url).signal(runId, [decision(approvalId, outcome)]));
79
+ const result = yield* tryClient(() => clientFor(url).signal(runId, [decision({ approvalId, choice: outcome })]));
80
80
  yield* Console.log(stringifyJsonPretty(result));
81
81
  return undefined;
82
82
  })).pipe(Command.withDescription('Approve or reject a pending approval'), Command.withShortDescription('Decide an approval'));
@@ -103,7 +103,7 @@ const steer = Command.make('steer', {
103
103
  interrupt: Flag.boolean('interrupt').pipe(Flag.withDescription('Interrupt the current turn (use --no-interrupt to disable)'), Flag.withDefault(true)),
104
104
  }, Effect.fn('steer')(function* ({ runId, message, interrupt }) {
105
105
  const { url } = yield* loomsBase;
106
- const result = yield* tryClient(() => clientFor(url).signal(runId, [steerEvent(message.join(' '), { interrupt })]));
106
+ const result = yield* tryClient(() => clientFor(url).signal(runId, [steerEvent({ content: message.join(' '), interrupt })]));
107
107
  yield* Console.log(stringifyJsonPretty(result));
108
108
  })).pipe(Command.withDescription('Send a steering message to a running agent'), Command.withShortDescription('Steer an agent'));
109
109
  const tail = Command.make('tail', {
@@ -906,6 +906,178 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
906
906
  readonly childCloseSent: boolean;
907
907
  };
908
908
  }>;
909
+ sendSignal: (runId: string, namespace: string, name: string, body?: {
910
+ input?: JsonValue;
911
+ threadId?: string;
912
+ idempotencyKey?: string;
913
+ }) => Promise<{
914
+ readonly runId: string;
915
+ readonly state: {
916
+ readonly runId: string;
917
+ readonly status: "cancelled" | "completed" | "failed" | "running";
918
+ readonly rootThreadId: string | null;
919
+ readonly startIdentity: {
920
+ readonly kind: string;
921
+ readonly definitionName: string;
922
+ readonly definitionVersion: string;
923
+ readonly input: Schema.Json;
924
+ readonly requestedThreadId: string | null;
925
+ readonly idempotencyKey: string | null;
926
+ readonly rootThreadId: string;
927
+ readonly launchedBy: {
928
+ readonly runId: string;
929
+ readonly threadId: string;
930
+ readonly effectId: string;
931
+ readonly causationId: string;
932
+ readonly causationSeq: number;
933
+ } | null;
934
+ readonly correlationId: string | null;
935
+ readonly attributes: {
936
+ readonly [x: string]: string;
937
+ };
938
+ readonly trace: {
939
+ readonly traceparent: string;
940
+ readonly tracestate?: string | undefined;
941
+ } | null;
942
+ } | null;
943
+ readonly attributes: {
944
+ readonly [x: string]: string;
945
+ };
946
+ readonly forkedFrom: {
947
+ readonly runId: string;
948
+ readonly seq: number;
949
+ } | null;
950
+ readonly threads: {
951
+ readonly [x: string]: {
952
+ readonly threadId: string;
953
+ readonly kind: string;
954
+ readonly definitionName: string;
955
+ readonly definitionVersion: string;
956
+ readonly parentThreadId: string | null;
957
+ readonly status: "cancelled" | "completed" | "failed" | "running" | "waiting";
958
+ readonly input: Schema.Json;
959
+ readonly output: Schema.Json;
960
+ readonly error: string | null;
961
+ readonly state: Schema.Json;
962
+ };
963
+ };
964
+ readonly waits: {
965
+ readonly [x: string]: {
966
+ readonly waitId: string;
967
+ readonly threadId: string;
968
+ readonly on: {
969
+ readonly type: string | readonly string[];
970
+ readonly match?: Schema.Json | undefined;
971
+ } | {
972
+ readonly timerAt: number;
973
+ };
974
+ readonly tag?: Schema.Json | undefined;
975
+ };
976
+ };
977
+ readonly outstandingEffects: readonly {
978
+ readonly effectId: string;
979
+ readonly threadId: string;
980
+ readonly causingSeq: number;
981
+ readonly causingEventId: string;
982
+ readonly effect: {
983
+ readonly type: "runtime.spawn";
984
+ readonly childThreadId: string;
985
+ readonly kind: string;
986
+ readonly definitionName: string;
987
+ readonly definitionVersion: string;
988
+ readonly input: Schema.Json;
989
+ } | {
990
+ readonly type: "runtime.wait";
991
+ readonly waitId: string;
992
+ readonly on: {
993
+ readonly type: string | readonly string[];
994
+ readonly match?: Schema.Json | undefined;
995
+ } | {
996
+ readonly timerAt: number;
997
+ };
998
+ readonly tag?: Schema.Json | undefined;
999
+ } | {
1000
+ readonly type: "runtime.emit";
1001
+ readonly event: {
1002
+ readonly type: string;
1003
+ readonly payload: Schema.Json;
1004
+ readonly threadId?: string | null | undefined;
1005
+ readonly parentThreadId?: string | null | undefined;
1006
+ readonly causationId?: string | null | undefined;
1007
+ readonly correlationId?: string | null | undefined;
1008
+ readonly effectId?: string | null | undefined;
1009
+ readonly origin?: {
1010
+ readonly type: "thread";
1011
+ readonly threadId: string;
1012
+ } | {
1013
+ readonly type: "external";
1014
+ readonly actorId?: string | undefined;
1015
+ } | {
1016
+ readonly type: "system";
1017
+ } | {
1018
+ readonly type: "run";
1019
+ readonly runId: string;
1020
+ } | undefined;
1021
+ readonly ephemeral?: boolean | undefined;
1022
+ readonly id?: string | undefined;
1023
+ readonly ts?: number | undefined;
1024
+ readonly idempotencyKey?: string | null | undefined;
1025
+ };
1026
+ readonly tag?: string | undefined;
1027
+ } | {
1028
+ readonly type: "runtime.complete";
1029
+ readonly output: Schema.Json;
1030
+ } | {
1031
+ readonly type: "runtime.fail";
1032
+ readonly error: string;
1033
+ } | {
1034
+ readonly type: "runtime.cancel";
1035
+ readonly threadId: string;
1036
+ readonly tag?: string | undefined;
1037
+ } | {
1038
+ readonly type: "runtime.launch";
1039
+ readonly runId: string;
1040
+ readonly kind: string;
1041
+ readonly definitionName: string;
1042
+ readonly definitionVersion: string;
1043
+ readonly input: Schema.Json;
1044
+ readonly close: "terminate" | "wait";
1045
+ readonly tag?: string | undefined;
1046
+ } | {
1047
+ readonly type: "runtime.cancelRun";
1048
+ readonly runId: string;
1049
+ readonly tag?: string | undefined;
1050
+ } | {
1051
+ readonly type: string;
1052
+ readonly input: Schema.Json;
1053
+ readonly tag?: string | undefined;
1054
+ readonly at?: number | undefined;
1055
+ };
1056
+ }[];
1057
+ readonly effectExecutions: {
1058
+ readonly [x: string]: {
1059
+ readonly effectId: string;
1060
+ readonly attempt: number;
1061
+ readonly status: "ambiguous" | "cancel_requested" | "dispatch_wait" | "dispatched" | "heartbeat" | "queued" | "retry_wait" | "running" | "started" | "timed_out";
1062
+ readonly nextAttemptAt: number | null;
1063
+ readonly deadlineAt: number | null;
1064
+ readonly startedAt: number | null;
1065
+ readonly lastHeartbeatAt: number | null;
1066
+ readonly heartbeatDetails: Schema.Json;
1067
+ readonly lastError: string | null;
1068
+ readonly dispatchTry: number;
1069
+ };
1070
+ };
1071
+ readonly completedEffectIds: readonly string[];
1072
+ readonly processedIdempotencyKeys: readonly string[];
1073
+ readonly launchedRuns: readonly {
1074
+ readonly runId: string;
1075
+ readonly close: "terminate" | "wait";
1076
+ }[];
1077
+ readonly closeWaiting: boolean;
1078
+ readonly childCloseSent: boolean;
1079
+ };
1080
+ }>;
909
1081
  wake: (runId: string) => Promise<{
910
1082
  readonly runId: string;
911
1083
  readonly state: {
@@ -1438,6 +1610,11 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
1438
1610
  readonly inputSchema?: Schema.Json | undefined;
1439
1611
  }[];
1440
1612
  readonly projections: readonly string[];
1613
+ readonly modules: readonly {
1614
+ readonly namespace: string;
1615
+ readonly protocolVersion: string;
1616
+ readonly description?: string | undefined;
1617
+ }[];
1441
1618
  }>;
1442
1619
  listEffects: () => Promise<{
1443
1620
  readonly effects: readonly {
@@ -1457,6 +1634,40 @@ export declare function createLoomsClient(options?: LoomsClientOptions): {
1457
1634
  readonly inputSchema?: Schema.Json | undefined;
1458
1635
  }[];
1459
1636
  }>;
1637
+ listSignals: () => Promise<{
1638
+ readonly signals: readonly {
1639
+ readonly namespace: string;
1640
+ readonly name: string;
1641
+ readonly description?: string | undefined;
1642
+ readonly inputSchema?: Schema.Json | undefined;
1643
+ }[];
1644
+ }>;
1645
+ listEvents: () => Promise<{
1646
+ readonly events: readonly {
1647
+ readonly type: string;
1648
+ readonly namespace: string;
1649
+ readonly payloadSchema?: Schema.Json | undefined;
1650
+ }[];
1651
+ }>;
1652
+ previewSignal: (namespace: string, name: string, body?: {
1653
+ input?: JsonValue;
1654
+ threadId?: string;
1655
+ }) => Promise<{
1656
+ readonly events: readonly {
1657
+ readonly type: string;
1658
+ readonly payload: Schema.Json;
1659
+ readonly threadId?: string | null | undefined;
1660
+ readonly idempotencyKey?: string | null | undefined;
1661
+ }[];
1662
+ }>;
1663
+ previewEvents: (events: ReadonlyArray<EventInput>) => Promise<{
1664
+ readonly events: readonly {
1665
+ readonly type: string;
1666
+ readonly payload: Schema.Json;
1667
+ readonly threadId?: string | null | undefined;
1668
+ readonly idempotencyKey?: string | null | undefined;
1669
+ }[];
1670
+ }>;
1460
1671
  replayTo: (runId: string, seq: number) => Promise<{
1461
1672
  readonly runId: string;
1462
1673
  readonly step: {
@@ -1,5 +1,5 @@
1
1
  import { Data, Effect, Option, Predicate, Schema, Stream } from 'effect';
2
- import { createRunId, DefinitionCatalogSchema, EffectCatalogSchema, EventEnvelopeSchema, readSubscriptionData, ReplayStepSchema, RunStateSchema, RunSummariesSchema, SecretContextSchema, stringifyJson, traceHeaders, } from '@swirls/looms/core';
2
+ import { createRunId, DefinitionCatalogSchema, EffectCatalogSchema, EventEnvelopeSchema, EventTypeCatalogSchema, JsonValueSchema, readSubscriptionData, ReplayStepSchema, RunStateSchema, RunSummariesSchema, SecretContextSchema, SignalCatalogSchema, 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) {
@@ -73,6 +73,15 @@ const ProjectionResultSchema = Schema.Struct({
73
73
  name: Schema.String,
74
74
  value: Schema.Unknown,
75
75
  });
76
+ const PreviewEventSchema = Schema.Struct({
77
+ type: Schema.String,
78
+ payload: JsonValueSchema,
79
+ threadId: Schema.optional(Schema.NullOr(Schema.String)),
80
+ idempotencyKey: Schema.optional(Schema.NullOr(Schema.String)),
81
+ });
82
+ const PreviewEventsResultSchema = Schema.Struct({
83
+ events: Schema.Array(PreviewEventSchema),
84
+ });
76
85
  export function createLoomsClient(options = {}) {
77
86
  const baseUrl = (options.baseUrl ?? '').replace(/\/$/, '');
78
87
  const fetchImpl = options.fetch ?? fetch;
@@ -239,6 +248,7 @@ export function createLoomsClient(options = {}) {
239
248
  method: 'POST',
240
249
  body: stringifyJson({ events }),
241
250
  }),
251
+ sendSignal: (runId, namespace, name, body = {}) => request(`/runs/${encodeURIComponent(runId)}/signals/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}`, RunResultSchema, { method: 'POST', body: stringifyJson(body) }),
242
252
  wake: (runId) => request(`/runs/${encodeURIComponent(runId)}/wake`, WakeResultSchema, { method: 'POST' }),
243
253
  /** Cancel a thread and its descendants; omit `threadId` to cancel the whole run. */
244
254
  cancel: (runId, threadId) => request(`/runs/${encodeURIComponent(runId)}/cancel`, RunResultSchema, {
@@ -255,6 +265,13 @@ export function createLoomsClient(options = {}) {
255
265
  listRunSummaries: () => request('/runs/summaries', RunSummariesSchema),
256
266
  listDefinitions: () => request('/definitions', DefinitionCatalogSchema),
257
267
  listEffects: () => request('/definitions/effects', EffectCatalogSchema),
268
+ listSignals: () => request('/definitions/signals', SignalCatalogSchema),
269
+ listEvents: () => request('/definitions/events', EventTypeCatalogSchema),
270
+ previewSignal: (namespace, name, body = {}) => request(`/definitions/signals/${encodeURIComponent(namespace)}/${encodeURIComponent(name)}/preview`, PreviewEventsResultSchema, { method: 'POST', body: stringifyJson(body) }),
271
+ previewEvents: (events) => request('/definitions/events/preview', PreviewEventsResultSchema, {
272
+ method: 'POST',
273
+ body: stringifyJson({ events }),
274
+ }),
258
275
  replayTo: (runId, seq) => request(`/runs/${encodeURIComponent(runId)}/replay?seq=${seq}`, ReplayResultSchema),
259
276
  project: (runId, name) => request(`/runs/${encodeURIComponent(runId)}/projections/${encodeURIComponent(name)}`, ProjectionResultSchema),
260
277
  subscribeEvents,
@@ -7,6 +7,7 @@ export * from './validate-event';
7
7
  export * from './envelope';
8
8
  export * from './catalog';
9
9
  export * from './effects';
10
+ export * from './signals';
10
11
  export * from './protocol';
11
12
  export * from './subscription';
12
13
  export * from './thread';
@@ -6,6 +6,7 @@ export * from './validate-event.js';
6
6
  export * from './envelope.js';
7
7
  export * from './catalog.js';
8
8
  export * from './effects.js';
9
+ export * from './signals.js';
9
10
  export * from './protocol.js';
10
11
  export * from './subscription.js';
11
12
  export * from './thread.js';
@@ -1,17 +1,47 @@
1
- import type { Layer } from 'effect';
1
+ import type { Context, Layer } from 'effect';
2
2
  import { type CatalogEntries, type EventCatalog, type EventInputOf, type EventsOfCatalog, type InferPayload } from './catalog';
3
3
  import type { EventsOf, ProtocolEvents } from './compose';
4
4
  import { type EffectDefinition, type EffectHandlerResult, type EffectPlacement, type EmitEffect, type LocalEffectDefinition, type PlacedEffectDefinition, type PlacementHandler, type RetryPolicy, type RuntimeEffect, type ScopedLocalEffectContext } from './effects';
5
5
  import type { EventEnvelope, EventInput } from './envelope';
6
6
  import type { RunLauncherTag } from './launcher';
7
7
  import { type AnyRuntimeModule, type DefinitionHost, type EffectAdmission, type EffectMiddleware, type RuntimeModule, type DefinitionRef, type RuntimeModuleDependency, type SignalInterruptRule } from './module';
8
- import { type ProjectionDefinition } from './projection';
8
+ import { type ProjectionDefinition, type ProjectionStateOf } from './projection';
9
9
  import type { InferDefinedSchema, InferSchemaOutput, SchemaInput } from './schema';
10
+ import { type AnySignalDefinition, type SignalDefinition } from './signals';
10
11
  import { type StartContext, type ThreadContext, type ThreadDefinition } from './thread';
11
12
  import { type JsonValue } from './types';
13
+ /** Schema-backed event builders returned on `signals`. Each is a {@link SignalDefinition}. */
14
+ export type ModuleSignals = {
15
+ readonly [key: string]: AnySignalDefinition;
16
+ };
17
+ type SignalBuiltOf<T> = T extends (...args: never[]) => infer R ? R : never;
18
+ type SignalEventOf<T> = SignalBuiltOf<T> extends readonly (infer E)[] ? E : SignalBuiltOf<T> extends infer E ? E : never;
19
+ /**
20
+ * Marks each signal whose built events fall outside the module's catalogs, such as one built with
21
+ * another module's scope or the unscoped `defineSignal`.
22
+ */
23
+ export type ForeignSignalsCheck<SG, TAllowed> = {
24
+ readonly [K in keyof SG as [SignalEventOf<SG[K]>] extends [TAllowed] ? never : K]: 'Signal events are outside this module catalog; build it with this module scope';
25
+ };
26
+ /** Effect composers returned on `steps`. Each returns effects for threads and workflow nodes. */
27
+ export type ModuleSteps = {
28
+ readonly [key: string]: (...args: never[]) => readonly RuntimeEffect[];
29
+ };
30
+ type DeepReadonly<T> = T extends object ? {
31
+ readonly [K in keyof T]: DeepReadonly<T[K]>;
32
+ } : T;
33
+ /** Typed example trace. Projection keys are member keys from setup, not projection `.name` values. */
34
+ export interface ModuleExampleOf<U, PR> {
35
+ readonly name: string;
36
+ readonly events: readonly EventInputOf<U>[];
37
+ readonly projections: {
38
+ readonly [K in keyof PR]?: DeepReadonly<ProjectionStateOf<PR[K]>>;
39
+ };
40
+ }
12
41
  export interface DefineModuleOptions<TNamespace extends string, TEvents extends EventCatalog | undefined = undefined, TObserves extends readonly EventCatalog[] | undefined = undefined> {
13
42
  readonly namespace: TNamespace;
14
43
  readonly protocolVersion: string;
44
+ readonly description?: string;
15
45
  readonly events?: TEvents;
16
46
  readonly observes?: TObserves;
17
47
  readonly dependencies?: readonly RuntimeModuleDependency[];
@@ -38,6 +68,7 @@ export type ModuleServicesConfig<TR> = [TR] extends [never] ? {
38
68
  export interface ModuleScope<TNamespace extends string, U extends EventEnvelope<any, any>, TEvents extends EventCatalog | undefined, TObserves extends readonly EventCatalog[] | undefined = readonly EventCatalog[] | undefined> {
39
69
  readonly namespace: TNamespace;
40
70
  readonly protocolVersion: string;
71
+ readonly description?: string;
41
72
  readonly events?: TEvents;
42
73
  readonly observes?: TObserves;
43
74
  readonly dependencies?: readonly RuntimeModuleDependency[];
@@ -83,6 +114,15 @@ export interface ModuleScope<TNamespace extends string, U extends EventEnvelope<
83
114
  retry?: RetryPolicy;
84
115
  deterministic?: boolean;
85
116
  } & PlacementHandler<P, (input: TInput, ctx: ScopedLocalEffectContext<EventInputOf<U>>) => EffectHandlerResult<R, EventInputOf<U>, HandlerError>>): NoInfer<PlacedEffectDefinition<P, TInput, R, EventInputOf<U>>>;
117
+ /**
118
+ * Declare a signal for this module. Callers outside the run invoke it to build typed events.
119
+ * The host publishes its `input` schema and can run `build` over HTTP for the debugger.
120
+ */
121
+ signal<TSchema extends SchemaInput | undefined = undefined, E extends EventInputOf<U> | readonly EventInputOf<U>[] = EventInputOf<U> | readonly EventInputOf<U>[]>(def: {
122
+ readonly description?: string;
123
+ readonly input?: TSchema;
124
+ readonly build: (input: InferDefinedSchema<TSchema, undefined>) => E;
125
+ }): SignalDefinition<InferDefinedSchema<TSchema, undefined>, E>;
86
126
  input<K extends keyof (TEvents extends EventCatalog<any, infer E> ? E : never) & string>(key: K, payload: InferPayload<(TEvents extends EventCatalog<any, infer E> ? E : never)[K]>, meta?: Omit<EventInput, 'type' | 'payload'>): Omit<EventInput, 'type' | 'payload'> & {
87
127
  readonly type: `${TNamespace}.${K}`;
88
128
  readonly payload: InferPayload<(TEvents extends EventCatalog<any, infer E> ? E : never)[K]>;
@@ -94,27 +134,32 @@ export type ThreadStateOf<T> = T extends ThreadDefinition<infer S> ? S : never;
94
134
  export declare function createModuleScope<TNamespace extends string, TEvents extends EventCatalog | undefined = undefined, TObserves extends readonly EventCatalog[] | undefined = undefined>(options: DefineModuleOptions<TNamespace, TEvents, TObserves>): ModuleScope<TNamespace, ScopeUniverse<TEvents, TObserves>, TEvents, TObserves>;
95
135
  /** A catalog may be supplied directly, or created from inline event schemas. */
96
136
  export type ModuleCatalog<N extends string, E> = E extends EventCatalog ? E : E extends CatalogEntries ? EventCatalog<N, E> : undefined;
97
- /** Construct a complete module. Only members returned by setup are installed. */
137
+ /** Construct a complete module. Only members returned by setup are kept. */
98
138
  export declare function defineModule<const N extends string, E extends EventCatalog | CatalogEntries | undefined = undefined, O extends readonly EventCatalog[] | undefined = undefined, FX extends {
99
139
  readonly [key: string]: EffectDefinition<any, any, any>;
100
140
  } = {}, TH extends {
101
141
  readonly [key: string]: ThreadDefinition;
102
142
  } = {}, PR extends {
103
143
  readonly [key: string]: ProjectionDefinition;
104
- } = {}>(options: Omit<DefineModuleOptions<N, ModuleCatalog<N, E>, O>, 'events'> & {
144
+ } = {}, SG extends ModuleSignals = {}, ST extends ModuleSteps = {}, const EX extends readonly ModuleExampleOf<ScopeUniverse<ModuleCatalog<N, E>, O>, PR>[] = []>(options: Omit<DefineModuleOptions<N, ModuleCatalog<N, E>, O>, 'events'> & {
105
145
  readonly events?: E;
106
146
  }, setup: (scope: ModuleScope<N, ScopeUniverse<ModuleCatalog<N, E>, O>, ModuleCatalog<N, E>, O>) => {
107
147
  readonly effects?: FX & ForeignEffectsCheck<FX, EventInputOf<ScopeUniverse<ModuleCatalog<N, E>, O>>>;
108
148
  readonly threads?: TH;
109
149
  readonly projections?: PR;
150
+ readonly signals?: SG & ForeignSignalsCheck<SG, EventInputOf<ScopeUniverse<ModuleCatalog<N, E>, O>>>;
151
+ readonly steps?: ST;
152
+ readonly examples?: EX;
110
153
  readonly definitions?: readonly DefinitionRef[];
111
154
  readonly middleware?: readonly EffectMiddleware[];
112
155
  readonly admission?: readonly EffectAdmission[];
113
156
  readonly interruptsOnSignal?: SignalInterruptRule;
114
157
  readonly bindHost?: (host: DefinitionHost) => void;
115
- } & ModuleServicesConfig<EffectRequirementsOf<FX>>): RuntimeModule<N, Extract<ModuleCatalog<N, E>, EventCatalog>, FX, TH, PR> & {
158
+ } & ModuleServicesConfig<EffectRequirementsOf<FX>>): RuntimeModule<N, Extract<ModuleCatalog<N, E>, EventCatalog>, FX, TH, PR, Context.Service.Any, SG, ST> & {
116
159
  readonly effects: FX;
117
160
  readonly threads: TH;
118
161
  readonly projections: PR;
162
+ readonly signals: SG;
163
+ readonly steps: ST;
119
164
  };
120
165
  export {};
@@ -2,8 +2,16 @@ import { defineEventCatalog, } from './catalog.js';
2
2
  import { emit, defineEffect, } from './effects.js';
3
3
  import {} from './module.js';
4
4
  import { defineProjection } from './projection.js';
5
+ import { defineSignal } from './signals.js';
5
6
  import { defineThread, } from './thread.js';
6
- import { asJson } from './types.js';
7
+ import { asJson, cleanUndefined } from './types.js';
8
+ function storeExamples(examples) {
9
+ return examples.map(({ name, events, projections }) => ({
10
+ name,
11
+ events,
12
+ projections: Object.fromEntries(Object.entries(projections).map(([key, state]) => [key, cleanUndefined(state)])),
13
+ }));
14
+ }
7
15
  function buildScopeInput(namespace, key, payload, meta) {
8
16
  return {
9
17
  ...meta,
@@ -12,10 +20,11 @@ function buildScopeInput(namespace, key, payload, meta) {
12
20
  };
13
21
  }
14
22
  export function createModuleScope(options) {
15
- const { namespace, protocolVersion, events, observes, dependencies } = options;
23
+ const { namespace, protocolVersion, description, events, observes, dependencies } = options;
16
24
  const scope = {
17
25
  namespace,
18
26
  protocolVersion,
27
+ description,
19
28
  events,
20
29
  observes,
21
30
  dependencies,
@@ -28,6 +37,9 @@ export function createModuleScope(options) {
28
37
  effect(def) {
29
38
  return defineEffect(def);
30
39
  },
40
+ signal(def) {
41
+ return defineSignal(def);
42
+ },
31
43
  input(key, payload, meta) {
32
44
  return buildScopeInput(namespace, key, payload, meta);
33
45
  },
@@ -59,13 +71,21 @@ export function defineModule(options, setup) {
59
71
  effects: members.effects ?? {},
60
72
  threads: members.threads ?? {},
61
73
  projections: members.projections ?? {},
74
+ signals: members.signals ?? {},
75
+ steps: members.steps ?? {},
62
76
  };
77
+ if (options.description) {
78
+ module.description = options.description;
79
+ }
63
80
  if (options.observes) {
64
81
  module.observes = options.observes;
65
82
  }
66
83
  if (options.dependencies) {
67
84
  module.dependencies = options.dependencies;
68
85
  }
86
+ if (members.examples) {
87
+ module.examples = storeExamples(members.examples);
88
+ }
69
89
  if (members.definitions) {
70
90
  module.definitions = members.definitions;
71
91
  }
@@ -6,6 +6,7 @@ import type { EventInput } from './envelope';
6
6
  import type { ProjectionDefinition } from './projection';
7
7
  import type { RunAttributes, SecretContext } from './run-context';
8
8
  import { type InferDefinedSchema, type SchemaInput } from './schema';
9
+ import type { AnySignalDefinition } from './signals';
9
10
  import type { ThreadDefinition } from './thread';
10
11
  import type { TraceContext } from './trace';
11
12
  import type { JsonValue } from './types';
@@ -84,6 +85,17 @@ export interface RegisteredDefinition {
84
85
  export type DefinitionInput<TDef> = TDef extends {
85
86
  readonly input?: infer S;
86
87
  } ? InferDefinedSchema<S> : JsonValue;
88
+ /**
89
+ * A trace `moduleConformance` folds. Events are catalog inputs in log order. `projections` maps a
90
+ * projection member key to the JSON state the fold must produce. The host does not read examples.
91
+ */
92
+ export interface ModuleExample {
93
+ readonly name: string;
94
+ readonly events: readonly EventInput[];
95
+ readonly projections: {
96
+ readonly [memberKey: string]: JsonValue;
97
+ };
98
+ }
87
99
  export interface RuntimeModule<TNamespace extends string = string, TEvents extends EventCatalog = EventCatalog, TEffects extends {
88
100
  readonly [key: string]: EffectDefinition;
89
101
  } = {
@@ -96,14 +108,25 @@ export interface RuntimeModule<TNamespace extends string = string, TEvents exten
96
108
  readonly [key: string]: ProjectionDefinition;
97
109
  } = {
98
110
  readonly [key: string]: ProjectionDefinition;
99
- }, TServices = Context.Service.Any> {
111
+ }, TServices = Context.Service.Any, TSignals = {}, TSteps = {}> {
100
112
  readonly namespace: TNamespace;
101
113
  readonly protocolVersion: string;
114
+ /** Published on `GET /definitions` so a catalog can name the module. */
115
+ readonly description?: string;
102
116
  readonly events?: TEvents;
103
117
  readonly observes?: readonly EventCatalog[];
104
118
  readonly threads?: TThreads;
105
119
  readonly effects?: TEffects;
106
120
  readonly projections?: TProjections;
121
+ /**
122
+ * Schema-backed event builders for callers outside a run. Callers import them, and the host
123
+ * publishes their input schemas and can run `build` for preview / named send over HTTP.
124
+ */
125
+ readonly signals?: TSignals;
126
+ /** Effect composers for threads and workflow nodes. The host does not run them. */
127
+ readonly steps?: TSteps;
128
+ /** Sample traces. The host ignores them; conformance folds them. */
129
+ readonly examples?: readonly ModuleExample[];
107
130
  readonly definitions?: readonly DefinitionRef[];
108
131
  readonly dependencies?: readonly RuntimeModuleDependency[];
109
132
  readonly middleware?: readonly EffectMiddleware[];
@@ -127,11 +150,15 @@ export declare function defineRuntimeModule<TNamespace extends string, TEvents e
127
150
  readonly [key: string]: ThreadDefinition;
128
151
  }, TProjections extends {
129
152
  readonly [key: string]: ProjectionDefinition;
130
- }, TServices = Context.Service.Any>(module: RuntimeModule<TNamespace, TEvents, TEffects, TThreads, TProjections, TServices>): RuntimeModule<TNamespace, TEvents, TEffects, TThreads, TProjections, TServices>;
153
+ }, TServices = Context.Service.Any, TSignals = {}, TSteps = {}>(module: RuntimeModule<TNamespace, TEvents, TEffects, TThreads, TProjections, TServices, TSignals, TSteps>): RuntimeModule<TNamespace, TEvents, TEffects, TThreads, TProjections, TServices, TSignals, TSteps>;
131
154
  export type AnyRuntimeModule = RuntimeModule<string, EventCatalog, {
132
155
  readonly [name: string]: EffectDefinition<any, any, any>;
133
156
  }, {
134
157
  readonly [kind: string]: ThreadDefinition;
135
158
  }, {
136
159
  readonly [name: string]: ProjectionDefinition;
160
+ }, Context.Service.Any, {
161
+ readonly [name: string]: AnySignalDefinition;
162
+ }, {
163
+ readonly [name: string]: (...args: never[]) => readonly RuntimeEffect[];
137
164
  }>;
@@ -9,6 +9,7 @@ export interface ProjectionDefinition<S = unknown> {
9
9
  initialState: S;
10
10
  reduce(state: S, event: EventEnvelope): S;
11
11
  }
12
+ export type ProjectionStateOf<T> = T extends ProjectionDefinition<infer S> ? S : never;
12
13
  export declare function defineProjection<S = unknown, TEvent extends AnyEventEnvelope = RegisteredEvent>(def: {
13
14
  readonly name: string;
14
15
  readonly shape?: undefined;