@swirls/looms 0.8.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 (94) hide show
  1. package/CHANGELOG.md +12 -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 +87 -16
  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/projectors/event-log-ops.d.ts +68 -0
  64. package/dist/projectors/event-log-ops.js +79 -0
  65. package/dist/projectors/event-log.d.ts +4 -68
  66. package/dist/projectors/event-log.js +4 -79
  67. package/dist/projectors/replica/migrations.d.ts +4 -6
  68. package/dist/projectors/replica/migrations.js +18 -1
  69. package/dist/projectors/replica/sqlite-bun.d.ts +5 -0
  70. package/dist/projectors/replica/sqlite-bun.js +39 -0
  71. package/dist/projectors/replica/sqlite-migrations.d.ts +5 -0
  72. package/dist/projectors/replica/sqlite-migrations.js +99 -0
  73. package/dist/projectors/replica/sqlite.d.ts +95 -0
  74. package/dist/projectors/replica/sqlite.js +517 -0
  75. package/dist/runtime/server/routes/definitions.d.ts +1 -1
  76. package/dist/runtime/server/routes/definitions.js +58 -2
  77. package/dist/runtime/server/routes/runs.js +25 -1
  78. package/dist/schedule/module.d.ts +117 -1
  79. package/dist/schedule/module.js +30 -0
  80. package/dist/schedule/scope.js +1 -0
  81. package/dist/schedule/signals.d.ts +59 -7
  82. package/dist/schedule/signals.js +35 -17
  83. package/dist/testing/corpus-module.d.ts +1 -1
  84. package/dist/testing/index.js +2 -1
  85. package/dist/testing/module-examples.d.ts +6 -0
  86. package/dist/testing/module-examples.js +66 -0
  87. package/dist/workflow/module.d.ts +3 -1
  88. package/dist/workflow/module.js +13 -0
  89. package/dist/workflow/scope.js +1 -0
  90. package/package.json +17 -1
  91. package/dist/debugger/app/assets/index-B7GdT1UA.css +0 -2
  92. package/dist/debugger/app/assets/routes-CfXxKG45.js +0 -47
  93. /package/dist/evaluator/{signals.d.ts → steps.d.ts} +0 -0
  94. /package/dist/evaluator/{signals.js → steps.js} +0 -0
@@ -6,6 +6,7 @@ import { callLlmEffect, executeToolEffect } from './effects.js';
6
6
  import { llmFromAdapter, LlmTag, StubLlmLive } from './llm.js';
7
7
  import { conversation, tokenUsage, turnActivity } from './projections.js';
8
8
  import { agentModule } from './scope.js';
9
+ import { agentSessionMessageEvents, cancelAgentTurn, steer, stopAgentTurn, submitAgentMessage, userMessage, } from './signals.js';
9
10
  import { agentThread } from './threads.js';
10
11
  export function agent(options = {}) {
11
12
  const definitions = options.definitions ?? [];
@@ -29,6 +30,36 @@ export function agent(options = {}) {
29
30
  tokenUsage,
30
31
  turnActivity,
31
32
  },
33
+ signals: {
34
+ userMessage,
35
+ steer,
36
+ stopAgentTurn,
37
+ submitAgentMessage,
38
+ cancelAgentTurn,
39
+ agentSessionMessageEvents,
40
+ },
41
+ examples: [
42
+ {
43
+ name: 'a reply updates conversation and token usage',
44
+ events: [
45
+ userMessage({ content: 'Hello' }),
46
+ agentModule.input('message', {
47
+ turn: 1,
48
+ message: { role: 'assistant', content: 'Hi there' },
49
+ usage: { input: 4, output: 2 },
50
+ }),
51
+ ],
52
+ projections: {
53
+ conversation: {
54
+ lines: [
55
+ { role: 'user', content: 'Hello' },
56
+ { role: 'assistant', content: 'Hi there' },
57
+ ],
58
+ },
59
+ tokenUsage: { input: 4, output: 2 },
60
+ },
61
+ },
62
+ ],
32
63
  services: () => Layer.merge(llmLayer, AgentDefinitionsLive(agentDefinitions)),
33
64
  interruptsOnSignal,
34
65
  bindHost(host) {
@@ -3,5 +3,6 @@ import { agentCatalog } from './events.js';
3
3
  export const agentModule = createModuleScope({
4
4
  namespace: 'agent',
5
5
  protocolVersion: '1.0.0',
6
+ description: 'Conversational and tool-using model runs.',
6
7
  events: agentCatalog,
7
8
  });
@@ -1,33 +1,93 @@
1
+ import { Schema } from 'effect';
1
2
  import type { EventInput, JsonValue } from '@swirls/looms/core';
2
3
  import type { AgentMessageDelivery } from './types';
3
- export interface AgentSignalOptions {
4
- /** Target thread. Defaults to the run's root thread when omitted. */
5
- readonly threadId?: string;
6
- }
7
4
  /**
8
5
  * Build the `agent.message.received` event for a conversational agent.
9
- * Send it with `looms.signal(runId, [userMessage('Also greet Maya')])`.
6
+ * Send it with `looms.signal(runId, [userMessage({ content: 'Also greet Maya' })])`.
10
7
  */
11
- export declare function userMessage(content: string, options?: AgentSignalOptions): EventInput;
8
+ export declare const userMessage: import("@swirls/looms/core").SignalDefinition<{
9
+ readonly content: string;
10
+ }, Omit<EventInput, "payload" | "type"> & {
11
+ readonly type: "agent.message.received";
12
+ readonly payload: {
13
+ readonly message: {
14
+ readonly role: "assistant" | "system" | "tool" | "user";
15
+ readonly content: string;
16
+ readonly toolCallId?: string | undefined;
17
+ readonly name?: string | undefined;
18
+ readonly toolCalls?: {
19
+ readonly id: string;
20
+ readonly name: string;
21
+ readonly arguments: Schema.Json;
22
+ }[] | undefined;
23
+ };
24
+ };
25
+ }>;
12
26
  /**
13
27
  * Build the `agent.steered` event: inject a user message mid-run, optionally
14
28
  * interrupting the current turn so the agent replans immediately.
15
29
  */
16
- export declare function steer(content: string, options?: AgentSignalOptions & {
17
- readonly interrupt?: boolean;
18
- }): EventInput;
30
+ export declare const steer: import("@swirls/looms/core").SignalDefinition<{
31
+ readonly content: string;
32
+ readonly interrupt?: boolean | undefined;
33
+ }, Omit<EventInput, "payload" | "type"> & {
34
+ readonly type: "agent.steered";
35
+ readonly payload: {
36
+ readonly turn: number;
37
+ readonly messageId?: string | undefined;
38
+ readonly message: {
39
+ readonly role: "assistant" | "system" | "tool" | "user";
40
+ readonly content: string;
41
+ readonly toolCallId?: string | undefined;
42
+ readonly name?: string | undefined;
43
+ readonly toolCalls?: {
44
+ readonly id: string;
45
+ readonly name: string;
46
+ readonly arguments: Schema.Json;
47
+ }[] | undefined;
48
+ };
49
+ readonly interrupt?: boolean | undefined;
50
+ };
51
+ }>;
19
52
  /**
20
53
  * Build the `agent.turn.stopped` event. A conversational agent aborts its in-flight turn and
21
54
  * waits for the next message; an agent session cancels its active turn and stays open.
22
55
  */
23
- export declare function stopAgentTurn(options?: AgentSignalOptions): EventInput;
24
- export interface AgentSessionMessageOptions extends AgentSignalOptions {
56
+ export declare const stopAgentTurn: import("@swirls/looms/core").SignalDefinition<undefined, Omit<EventInput, "payload" | "type"> & {
57
+ readonly type: "agent.turn.stopped";
58
+ readonly payload: {};
59
+ }>;
60
+ export interface AgentSessionMessageOptions {
61
+ readonly threadId?: string;
25
62
  readonly delivery?: AgentMessageDelivery;
26
63
  /** Set by the client helper when a steer is aimed at an active child. */
27
64
  readonly activeChildThreadId?: string;
28
65
  }
29
- export declare function submitAgentMessage(messageId: string, text: string, options?: AgentSessionMessageOptions): EventInput;
30
- export declare function cancelAgentTurn(childThreadId: string, options?: AgentSignalOptions): EventInput[];
66
+ export declare const submitAgentMessage: import("@swirls/looms/core").SignalDefinition<{
67
+ readonly messageId: string;
68
+ readonly text: string;
69
+ readonly delivery?: "followUp" | "nextTurn" | "steer" | undefined;
70
+ readonly activeChildThreadId?: string | undefined;
71
+ }, Omit<EventInput, "payload" | "type"> & {
72
+ readonly type: "agent.session.message.submitted";
73
+ readonly payload: {
74
+ readonly messageId: string;
75
+ readonly text: string;
76
+ readonly delivery?: "followUp" | "nextTurn" | "steer" | undefined;
77
+ readonly activeChildThreadId?: string | undefined;
78
+ };
79
+ }>;
80
+ export declare const cancelAgentTurn: import("@swirls/looms/core").SignalDefinition<{
81
+ readonly childThreadId: string;
82
+ }, ({
83
+ type: 'runtime.thread.cancel.requested';
84
+ payload: {
85
+ threadId: string;
86
+ };
87
+ } | (Omit<EventInput, "payload" | "type"> & {
88
+ readonly type: "agent.session.cancel.requested";
89
+ readonly payload: {};
90
+ }))[]>;
31
91
  interface AgentSessionSignalClient<Result> {
32
92
  getRun(runId: string): Promise<{
33
93
  rootThreadId: string | null;
@@ -41,7 +101,40 @@ interface AgentSessionSignalClient<Result> {
41
101
  cancel?(runId: string, threadId?: string): Promise<Result>;
42
102
  }
43
103
  /** Events for one session message. A steer aimed at `activeChildThreadId` interrupts that turn in the same batch. */
44
- export declare function agentSessionMessageEvents(messageId: string, text: string, options?: AgentSessionMessageOptions): EventInput[];
104
+ export declare const agentSessionMessageEvents: import("@swirls/looms/core").SignalDefinition<{
105
+ readonly messageId: string;
106
+ readonly text: string;
107
+ readonly delivery?: "followUp" | "nextTurn" | "steer" | undefined;
108
+ readonly activeChildThreadId?: string | undefined;
109
+ }, ((Omit<EventInput, "payload" | "type"> & {
110
+ readonly type: "agent.session.message.submitted";
111
+ readonly payload: {
112
+ readonly messageId: string;
113
+ readonly text: string;
114
+ readonly delivery?: "followUp" | "nextTurn" | "steer" | undefined;
115
+ readonly activeChildThreadId?: string | undefined;
116
+ };
117
+ }) | (Omit<EventInput, "payload" | "type"> & {
118
+ readonly type: "agent.steered";
119
+ readonly payload: {
120
+ readonly turn: number;
121
+ readonly messageId?: string | undefined;
122
+ readonly message: {
123
+ readonly role: "assistant" | "system" | "tool" | "user";
124
+ readonly content: string;
125
+ readonly toolCallId?: string | undefined;
126
+ readonly name?: string | undefined;
127
+ readonly toolCalls?: {
128
+ readonly id: string;
129
+ readonly name: string;
130
+ readonly arguments: Schema.Json;
131
+ }[] | undefined;
132
+ };
133
+ readonly interrupt?: boolean | undefined;
134
+ };
135
+ }))[]>;
45
136
  export declare function sendAgentSessionMessage<Result>(client: AgentSessionSignalClient<Result>, runId: string, messageId: string, text: string, options?: Omit<AgentSessionMessageOptions, 'activeChildThreadId'>): Promise<Result>;
46
- export declare function cancelActiveAgentTurn<Result>(client: AgentSessionSignalClient<Result>, runId: string, options?: AgentSignalOptions): Promise<Result | Awaited<ReturnType<AgentSessionSignalClient<Result>['getRun']>>>;
137
+ export declare function cancelActiveAgentTurn<Result>(client: AgentSessionSignalClient<Result>, runId: string, options?: {
138
+ readonly threadId?: string;
139
+ }): Promise<Result | Awaited<ReturnType<AgentSessionSignalClient<Result>['getRun']>>>;
47
140
  export {};
@@ -1,51 +1,64 @@
1
- import { Predicate } from 'effect';
1
+ import { Predicate, Schema } from 'effect';
2
+ import { AgentSessionMessageSubmittedPayloadSchema } from './events.js';
2
3
  import { agentModule } from './scope.js';
4
+ const UserMessageInputSchema = Schema.Struct({
5
+ content: Schema.String,
6
+ });
7
+ const SteerInputSchema = Schema.Struct({
8
+ content: Schema.String,
9
+ interrupt: Schema.optional(Schema.Boolean),
10
+ });
11
+ const CancelAgentTurnInputSchema = Schema.Struct({
12
+ childThreadId: Schema.String,
13
+ });
3
14
  /**
4
15
  * Build the `agent.message.received` event for a conversational agent.
5
- * Send it with `looms.signal(runId, [userMessage('Also greet Maya')])`.
16
+ * Send it with `looms.signal(runId, [userMessage({ content: 'Also greet Maya' })])`.
6
17
  */
7
- export function userMessage(content, options = {}) {
8
- return agentModule.input('message.received', { message: { role: 'user', content } }, { threadId: options.threadId });
9
- }
18
+ export const userMessage = agentModule.signal({
19
+ description: 'Append a user message to a conversational agent.',
20
+ input: UserMessageInputSchema,
21
+ build: ({ content }) => agentModule.input('message.received', { message: { role: 'user', content } }),
22
+ });
10
23
  /**
11
24
  * Build the `agent.steered` event: inject a user message mid-run, optionally
12
25
  * interrupting the current turn so the agent replans immediately.
13
26
  */
14
- export function steer(content, options = {}) {
15
- return agentModule.input('steered', {
27
+ export const steer = agentModule.signal({
28
+ description: 'Inject a user message mid-run, optionally interrupting the current turn.',
29
+ input: SteerInputSchema,
30
+ build: ({ content, interrupt }) => agentModule.input('steered', {
16
31
  turn: 0,
17
- interrupt: options.interrupt ?? true,
32
+ interrupt: interrupt ?? true,
18
33
  message: { role: 'user', content },
19
- }, { threadId: options.threadId });
20
- }
34
+ }),
35
+ });
21
36
  /**
22
37
  * Build the `agent.turn.stopped` event. A conversational agent aborts its in-flight turn and
23
38
  * waits for the next message; an agent session cancels its active turn and stays open.
24
39
  */
25
- export function stopAgentTurn(options = {}) {
26
- return agentModule.input('turn.stopped', {}, { threadId: options.threadId });
27
- }
28
- export function submitAgentMessage(messageId, text, options = {}) {
29
- return agentModule.input('session.message.submitted', {
30
- messageId,
31
- text,
32
- delivery: options.delivery,
33
- activeChildThreadId: options.activeChildThreadId,
34
- }, {
35
- threadId: options.threadId,
36
- idempotencyKey: `agent-session-message:${messageId}`,
37
- });
38
- }
39
- export function cancelAgentTurn(childThreadId, options = {}) {
40
- return [
41
- agentModule.input('session.cancel.requested', {}, { threadId: options.threadId }),
40
+ export const stopAgentTurn = agentModule.signal({
41
+ description: 'Stop the in-flight agent turn without ending the run.',
42
+ build: () => agentModule.input('turn.stopped', {}),
43
+ });
44
+ export const submitAgentMessage = agentModule.signal({
45
+ description: 'Submit a message to an agent session.',
46
+ input: AgentSessionMessageSubmittedPayloadSchema,
47
+ build: (input) => agentModule.input('session.message.submitted', input, {
48
+ idempotencyKey: `agent-session-message:${input.messageId}`,
49
+ }),
50
+ });
51
+ export const cancelAgentTurn = agentModule.signal({
52
+ description: 'Cancel the active child turn of an agent session.',
53
+ input: CancelAgentTurnInputSchema,
54
+ build: ({ childThreadId }) => [
55
+ agentModule.input('session.cancel.requested', {}),
42
56
  {
43
57
  type: 'runtime.thread.cancel.requested',
44
58
  payload: { threadId: childThreadId },
45
- threadId: options.threadId,
46
59
  },
47
- ];
48
- }
60
+ ],
61
+ });
49
62
  function activeTurnFrom(value) {
50
63
  if (!Predicate.isObject(value) || !Predicate.isObject(value.activeTurn)) {
51
64
  return null;
@@ -62,27 +75,37 @@ function activeTurnFrom(value) {
62
75
  : null;
63
76
  }
64
77
  /** Events for one session message. A steer aimed at `activeChildThreadId` interrupts that turn in the same batch. */
65
- export function agentSessionMessageEvents(messageId, text, options = {}) {
66
- const events = [submitAgentMessage(messageId, text, options)];
67
- if (options.delivery === 'steer' && options.activeChildThreadId) {
68
- events.push(agentModule.input('steered', {
69
- turn: 0,
70
- messageId,
71
- message: { role: 'user', content: text },
72
- interrupt: true,
73
- }, { threadId: options.activeChildThreadId }));
74
- }
75
- return events;
76
- }
78
+ export const agentSessionMessageEvents = agentModule.signal({
79
+ description: 'Submit a session message, and steer the active child when delivery is steer.',
80
+ input: AgentSessionMessageSubmittedPayloadSchema,
81
+ build: (input) => {
82
+ const submitted = agentModule.input('session.message.submitted', input, {
83
+ idempotencyKey: `agent-session-message:${input.messageId}`,
84
+ });
85
+ if (input.delivery === 'steer' && input.activeChildThreadId) {
86
+ return [
87
+ submitted,
88
+ agentModule.input('steered', {
89
+ turn: 0,
90
+ messageId: input.messageId,
91
+ message: { role: 'user', content: input.text },
92
+ interrupt: true,
93
+ }, { threadId: input.activeChildThreadId }),
94
+ ];
95
+ }
96
+ return [submitted];
97
+ },
98
+ });
77
99
  export function sendAgentSessionMessage(client, runId, messageId, text, options = {}) {
78
100
  return client.getRun(runId).then((run) => {
79
101
  const threadId = options.threadId ?? run.rootThreadId ?? undefined;
80
102
  const active = options.delivery === 'steer' && threadId ? activeTurnFrom(run.threads[threadId]?.state) : null;
81
- return client.signal(runId, agentSessionMessageEvents(messageId, text, {
82
- ...options,
83
- threadId,
103
+ return client.signal(runId, agentSessionMessageEvents({
104
+ messageId,
105
+ text,
106
+ delivery: options.delivery,
84
107
  activeChildThreadId: active?.childThreadId,
85
- }));
108
+ }, { threadId }));
86
109
  });
87
110
  }
88
111
  export function cancelActiveAgentTurn(client, runId, options = {}) {
@@ -100,6 +123,6 @@ export function cancelActiveAgentTurn(client, runId, options = {}) {
100
123
  const cancelled = cancel(runId, active.childThreadId);
101
124
  return Promise.all([requested, cancelled]).then(([, result]) => result);
102
125
  }
103
- return client.signal(runId, cancelAgentTurn(active.childThreadId, { threadId }));
126
+ return client.signal(runId, cancelAgentTurn({ childThreadId: active.childThreadId }, { threadId }));
104
127
  });
105
128
  }
@@ -12,7 +12,7 @@ export function ApprovalsView({ runId }) {
12
12
  return _jsx("p", { className: "text-muted-foreground text-xs", children: "No approvals requested." });
13
13
  }
14
14
  function decide(approvalId, outcome) {
15
- void client.signal(runId, [decision(approvalId, outcome)]);
15
+ void client.signal(runId, [decision({ approvalId, choice: outcome })]);
16
16
  }
17
17
  return (_jsx("ul", { className: "grid gap-2", children: state.items.map((item) => (_jsxs("li", { className: "border-border bg-card/60 rounded-lg border p-2.5", children: [_jsxs("div", { className: "flex items-center gap-2 text-xs", children: [_jsx("span", { className: "min-w-0 flex-1 truncate font-medium", children: item.title || item.approvalId }), _jsx(StatusBadge, { status: item.status })] }), item.status === 'pending' && !finished ? (_jsxs("div", { className: "mt-2 flex gap-1.5", children: [_jsx(Button, { size: "xs", onClick: () => decide(item.approvalId, 'approve'), children: "Approve" }), _jsx(Button, { size: "xs", variant: "outline", onClick: () => decide(item.approvalId, 'reject'), children: "Reject" })] })) : null] }, item.approvalId))) }));
18
18
  }
@@ -3,4 +3,5 @@ export * from './scope';
3
3
  export * from './effects';
4
4
  export * from './projections';
5
5
  export * from './signals';
6
+ export * from './steps';
6
7
  export * from './module';
@@ -3,4 +3,5 @@ export * from './scope.js';
3
3
  export * from './effects.js';
4
4
  export * from './projections.js';
5
5
  export * from './signals.js';
6
+ export * from './steps.js';
6
7
  export * from './module.js';
@@ -1,3 +1,4 @@
1
+ import { gate } from './steps';
1
2
  export declare function approval(): import("@swirls/looms/core").RuntimeModule<"approval", import("@swirls/looms/core").EventCatalog<"approval", {
2
3
  requested: import("effect/Schema").Struct<{
3
4
  readonly approvalId: import("effect/Schema").String;
@@ -313,7 +314,26 @@ export declare function approval(): import("@swirls/looms/core").RuntimeModule<"
313
314
  readonly threadId: string | null;
314
315
  }[];
315
316
  }>;
316
- }, import("effect/Context").Service.Any> & {
317
+ }, import("effect/Context").Service.Any, {
318
+ decision: import("@swirls/looms/core").SignalDefinition<{
319
+ readonly approvalId: string;
320
+ readonly choice: "approve" | "reject" | {
321
+ readonly actionId: string;
322
+ readonly outcome: "approve" | "reject";
323
+ readonly payload?: import("effect/Schema").Json | undefined;
324
+ };
325
+ }, Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
326
+ readonly type: "approval.decided";
327
+ readonly payload: {
328
+ readonly approvalId: string;
329
+ readonly actionId: string;
330
+ readonly outcome: "approve" | "reject";
331
+ readonly payload?: import("effect/Schema").Json | undefined;
332
+ };
333
+ }>;
334
+ }, {
335
+ gate: typeof gate;
336
+ }> & {
317
337
  readonly effects: {
318
338
  request: import("@swirls/looms/core").LocalEffectDefinition<{
319
339
  readonly approvalId: string;
@@ -611,4 +631,25 @@ export declare function approval(): import("@swirls/looms/core").RuntimeModule<"
611
631
  }[];
612
632
  }>;
613
633
  };
634
+ readonly signals: {
635
+ decision: import("@swirls/looms/core").SignalDefinition<{
636
+ readonly approvalId: string;
637
+ readonly choice: "approve" | "reject" | {
638
+ readonly actionId: string;
639
+ readonly outcome: "approve" | "reject";
640
+ readonly payload?: import("effect/Schema").Json | undefined;
641
+ };
642
+ }, Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
643
+ readonly type: "approval.decided";
644
+ readonly payload: {
645
+ readonly approvalId: string;
646
+ readonly actionId: string;
647
+ readonly outcome: "approve" | "reject";
648
+ readonly payload?: import("effect/Schema").Json | undefined;
649
+ };
650
+ }>;
651
+ };
652
+ readonly steps: {
653
+ gate: typeof gate;
654
+ };
614
655
  };
@@ -2,9 +2,62 @@ import { defineModule } from '@swirls/looms/core';
2
2
  import { requestApprovalEffect } from './effects.js';
3
3
  import { pendingApprovals } from './projections.js';
4
4
  import { approvalModule } from './scope.js';
5
+ import { decision } from './signals.js';
6
+ import { gate } from './steps.js';
5
7
  export function approval() {
6
8
  return defineModule(approvalModule, () => ({
7
9
  effects: { request: requestApprovalEffect },
8
10
  projections: { pendingApprovals },
11
+ signals: { decision },
12
+ steps: { gate },
13
+ examples: [
14
+ {
15
+ name: 'a decision resolves the pending approval',
16
+ events: [
17
+ approvalModule.input('requested', {
18
+ approvalId: 'apr_1',
19
+ title: 'Charge $40',
20
+ description: 'Card charge',
21
+ actions: [{ id: 'approve', label: 'Approve', outcome: 'approve' }],
22
+ }, { threadId: 'thread_1' }),
23
+ decision({ approvalId: 'apr_1', choice: 'approve' }),
24
+ ],
25
+ projections: {
26
+ pendingApprovals: {
27
+ items: [
28
+ {
29
+ approvalId: 'apr_1',
30
+ title: 'Charge $40',
31
+ description: 'Card charge',
32
+ status: 'approved',
33
+ threadId: 'thread_1',
34
+ },
35
+ ],
36
+ },
37
+ },
38
+ },
39
+ {
40
+ name: 'a request without a description stays pending',
41
+ events: [
42
+ approvalModule.input('requested', {
43
+ approvalId: 'apr_2',
44
+ title: 'Refund',
45
+ actions: [{ id: 'approve', label: 'Approve', outcome: 'approve' }],
46
+ }, { threadId: 'thread_2' }),
47
+ ],
48
+ projections: {
49
+ pendingApprovals: {
50
+ items: [
51
+ {
52
+ approvalId: 'apr_2',
53
+ title: 'Refund',
54
+ status: 'pending',
55
+ threadId: 'thread_2',
56
+ },
57
+ ],
58
+ },
59
+ },
60
+ },
61
+ ],
9
62
  }));
10
63
  }
@@ -3,5 +3,6 @@ import { approvalCatalog } from './events.js';
3
3
  export const approvalModule = createModuleScope({
4
4
  namespace: 'approval',
5
5
  protocolVersion: '1.0.0',
6
+ description: 'Human gates that park a run until someone decides.',
6
7
  events: approvalCatalog,
7
8
  });
@@ -1,14 +1,5 @@
1
- import { type EventInput, type JsonValue, type RuntimeEffect } from '@swirls/looms/core';
2
- import type { ApprovalAction } from './events';
3
- export declare function gate(args: {
4
- title: string;
5
- description?: string;
6
- approvalId?: string;
7
- actions?: ApprovalAction[];
8
- schema?: JsonValue;
9
- /** Absolute epoch milliseconds. Decision and timer waits race; the first wins. */
10
- timeoutAt?: number;
11
- }): RuntimeEffect[];
1
+ import { Schema } from 'effect';
2
+ import { type JsonValue } from '@swirls/looms/core';
12
3
  export type ApprovalChoice = 'approve' | 'reject' | {
13
4
  actionId: string;
14
5
  outcome: 'approve' | 'reject';
@@ -16,6 +7,21 @@ export type ApprovalChoice = 'approve' | 'reject' | {
16
7
  };
17
8
  /**
18
9
  * Build the `approval.decided` event that resolves a pending gate.
19
- * Send it with `looms.signal(runId, [decision(approvalId, 'approve')])`.
10
+ * Send it with `looms.signal(runId, [decision({ approvalId, choice: 'approve' })])`.
20
11
  */
21
- export declare function decision(approvalId: string, choice: ApprovalChoice): EventInput;
12
+ export declare const decision: import("@swirls/looms/core").SignalDefinition<{
13
+ readonly approvalId: string;
14
+ readonly choice: "approve" | "reject" | {
15
+ readonly actionId: string;
16
+ readonly outcome: "approve" | "reject";
17
+ readonly payload?: Schema.Json | undefined;
18
+ };
19
+ }, Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
20
+ readonly type: "approval.decided";
21
+ readonly payload: {
22
+ readonly approvalId: string;
23
+ readonly actionId: string;
24
+ readonly outcome: "approve" | "reject";
25
+ readonly payload?: Schema.Json | undefined;
26
+ };
27
+ }>;
@@ -1,50 +1,30 @@
1
- import { createWaitId, invoke, isJsonString, wait, } from '@swirls/looms/core';
2
- import { requestApprovalEffect } from './effects.js';
1
+ import { Schema } from 'effect';
2
+ import { isJsonString } from '@swirls/looms/core';
3
3
  import { approvalModule } from './scope.js';
4
- export function gate(args) {
5
- const approvalId = args.approvalId ?? createWaitId('approval');
6
- const actions = args.actions ?? [
7
- { id: 'approve', label: 'Approve', outcome: 'approve' },
8
- { id: 'reject', label: 'Reject', outcome: 'reject' },
9
- ];
10
- const effects = [
11
- invoke(requestApprovalEffect, {
12
- approvalId,
13
- title: args.title,
14
- description: args.description,
15
- actions,
16
- schema: args.schema,
17
- }, `request_${approvalId}`),
18
- wait({
19
- waitId: createWaitId(approvalId, 'decision'),
20
- on: { type: 'approval.decided', match: { approvalId } },
21
- tag: { approvalId, raceId: approvalId },
22
- }),
23
- ];
24
- if (args.timeoutAt !== undefined) {
25
- if (!Number.isFinite(args.timeoutAt)) {
26
- throw new Error('Approval timeoutAt must be a finite epoch timestamp');
27
- }
28
- effects.push(wait({
29
- waitId: createWaitId(approvalId, 'timeout'),
30
- on: { timerAt: args.timeoutAt },
31
- tag: {
32
- approvalId,
33
- raceId: approvalId,
34
- outcome: 'timeout',
35
- error: `Approval ${approvalId} timed out`,
36
- },
37
- }));
38
- }
39
- return effects;
4
+ const ApprovalChoiceSchema = Schema.Union([
5
+ Schema.Literal('approve'),
6
+ Schema.Literal('reject'),
7
+ Schema.Struct({
8
+ actionId: Schema.String,
9
+ outcome: Schema.Union([Schema.Literal('approve'), Schema.Literal('reject')]),
10
+ payload: Schema.optional(Schema.Json),
11
+ }),
12
+ ]);
13
+ const DecisionInputSchema = Schema.Struct({
14
+ approvalId: Schema.String,
15
+ choice: ApprovalChoiceSchema,
16
+ });
17
+ function decidedFrom(approvalId, choice) {
18
+ return isJsonString(choice)
19
+ ? { approvalId, actionId: choice, outcome: choice }
20
+ : { approvalId, actionId: choice.actionId, outcome: choice.outcome, payload: choice.payload };
40
21
  }
41
22
  /**
42
23
  * Build the `approval.decided` event that resolves a pending gate.
43
- * Send it with `looms.signal(runId, [decision(approvalId, 'approve')])`.
24
+ * Send it with `looms.signal(runId, [decision({ approvalId, choice: 'approve' })])`.
44
25
  */
45
- export function decision(approvalId, choice) {
46
- const decided = isJsonString(choice)
47
- ? { approvalId, actionId: choice, outcome: choice }
48
- : { approvalId, actionId: choice.actionId, outcome: choice.outcome, payload: choice.payload };
49
- return approvalModule.input('decided', decided);
50
- }
26
+ export const decision = approvalModule.signal({
27
+ description: 'Resolve a pending approval gate.',
28
+ input: DecisionInputSchema,
29
+ build: ({ approvalId, choice }) => approvalModule.input('decided', decidedFrom(approvalId, choice)),
30
+ });
@@ -0,0 +1,11 @@
1
+ import { type JsonValue, type RuntimeEffect } from '@swirls/looms/core';
2
+ import type { ApprovalAction } from './events';
3
+ export declare function gate(args: {
4
+ title: string;
5
+ description?: string;
6
+ approvalId?: string;
7
+ actions?: ApprovalAction[];
8
+ schema?: JsonValue;
9
+ /** Absolute epoch milliseconds. Decision and timer waits race; the first wins. */
10
+ timeoutAt?: number;
11
+ }): RuntimeEffect[];