@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.
- package/CHANGELOG.md +12 -0
- package/dist/actor/routing.js +2 -1
- package/dist/agent/debugger/agent-chat.js +6 -5
- package/dist/agent/debugger/index.js +2 -1
- package/dist/agent/module.d.ts +799 -593
- package/dist/agent/module.js +31 -0
- package/dist/agent/scope.js +1 -0
- package/dist/agent/signals.d.ts +108 -15
- package/dist/agent/signals.js +70 -47
- package/dist/approval/debugger/approvals-view.js +1 -1
- package/dist/approval/index.d.ts +1 -0
- package/dist/approval/index.js +1 -0
- package/dist/approval/module.d.ts +42 -1
- package/dist/approval/module.js +53 -0
- package/dist/approval/scope.js +1 -0
- package/dist/approval/signals.d.ts +19 -13
- package/dist/approval/signals.js +25 -45
- package/dist/approval/steps.d.ts +11 -0
- package/dist/approval/steps.js +39 -0
- package/dist/cli/commands.js +87 -16
- package/dist/client/client.d.ts +211 -0
- package/dist/client/client.js +18 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/module-scope.d.ts +50 -5
- package/dist/core/module-scope.js +22 -2
- package/dist/core/module.d.ts +29 -2
- package/dist/core/projection.d.ts +1 -0
- package/dist/core/published.d.ts +59 -1
- package/dist/core/published.js +97 -1
- package/dist/core/signals.d.ts +56 -0
- package/dist/core/signals.js +93 -0
- package/dist/core/validate-event.d.ts +12 -1
- package/dist/core/validate-event.js +34 -11
- package/dist/debugger/app/assets/index-CO5gIvV8.css +2 -0
- package/dist/debugger/app/assets/{index-CMRl_aWQ.js → index-D-2Q7ecG.js} +2 -2
- package/dist/debugger/app/assets/routes-lvmllT60.js +47 -0
- package/dist/debugger/app/index.html +2 -2
- package/dist/debugger/index.d.ts +1 -1
- package/dist/debugger/index.js +1 -1
- package/dist/debugger/lib/cn.d.ts +1 -1
- package/dist/debugger/lib/cn.js +12 -1
- package/dist/debugger/plugin.d.ts +11 -1
- package/dist/debugger/plugin.js +41 -0
- package/dist/debugger/shell/debugger-shell.js +1 -1
- package/dist/debugger/shell/form-fields.d.ts +17 -0
- package/dist/debugger/shell/form-fields.js +38 -0
- package/dist/debugger/shell/run-action.d.ts +6 -0
- package/dist/debugger/shell/run-action.js +15 -0
- package/dist/debugger/shell/run-controls.js +3 -55
- package/dist/debugger/shell/schema-form.d.ts +10 -1
- package/dist/debugger/shell/schema-form.js +108 -10
- package/dist/debugger/shell/signal-form.d.ts +18 -0
- package/dist/debugger/shell/signal-form.js +185 -0
- package/dist/debugger/shell/start-form.js +2 -32
- package/dist/debugger/shell/use-debounced-preview.d.ts +22 -0
- package/dist/debugger/shell/use-debounced-preview.js +68 -0
- package/dist/evaluator/index.d.ts +1 -1
- package/dist/evaluator/index.js +1 -1
- package/dist/evaluator/module.d.ts +8 -1
- package/dist/evaluator/module.js +33 -0
- package/dist/evaluator/scope.js +1 -0
- package/dist/projectors/event-log-ops.d.ts +68 -0
- package/dist/projectors/event-log-ops.js +79 -0
- package/dist/projectors/event-log.d.ts +4 -68
- package/dist/projectors/event-log.js +4 -79
- package/dist/projectors/replica/migrations.d.ts +4 -6
- package/dist/projectors/replica/migrations.js +18 -1
- package/dist/projectors/replica/sqlite-bun.d.ts +5 -0
- package/dist/projectors/replica/sqlite-bun.js +39 -0
- package/dist/projectors/replica/sqlite-migrations.d.ts +5 -0
- package/dist/projectors/replica/sqlite-migrations.js +99 -0
- package/dist/projectors/replica/sqlite.d.ts +95 -0
- package/dist/projectors/replica/sqlite.js +517 -0
- package/dist/runtime/server/routes/definitions.d.ts +1 -1
- package/dist/runtime/server/routes/definitions.js +58 -2
- package/dist/runtime/server/routes/runs.js +25 -1
- package/dist/schedule/module.d.ts +117 -1
- package/dist/schedule/module.js +30 -0
- package/dist/schedule/scope.js +1 -0
- package/dist/schedule/signals.d.ts +59 -7
- package/dist/schedule/signals.js +35 -17
- package/dist/testing/corpus-module.d.ts +1 -1
- package/dist/testing/index.js +2 -1
- package/dist/testing/module-examples.d.ts +6 -0
- package/dist/testing/module-examples.js +66 -0
- package/dist/workflow/module.d.ts +3 -1
- package/dist/workflow/module.js +13 -0
- package/dist/workflow/scope.js +1 -0
- package/package.json +17 -1
- package/dist/debugger/app/assets/index-B7GdT1UA.css +0 -2
- package/dist/debugger/app/assets/routes-CfXxKG45.js +0 -47
- /package/dist/evaluator/{signals.d.ts → steps.d.ts} +0 -0
- /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
|
+
}
|
package/dist/cli/commands.js
CHANGED
|
@@ -6,7 +6,9 @@ import { decision } from '@swirls/looms/approval';
|
|
|
6
6
|
import { createLoomsClient } from '@swirls/looms/client';
|
|
7
7
|
import { stringifyJson, stringifyJsonPretty } from '@swirls/looms/core';
|
|
8
8
|
import { postgresReplica } from '@swirls/looms/projectors/replica';
|
|
9
|
-
import { generateReplicaMigrations } from '@swirls/looms/projectors/replica/migrations';
|
|
9
|
+
import { generateReplicaMigrations, } from '@swirls/looms/projectors/replica/migrations';
|
|
10
|
+
import {} from '@swirls/looms/projectors/replica/sqlite';
|
|
11
|
+
import { openSqliteReplicaFile } from '@swirls/looms/projectors/replica/sqlite-bun';
|
|
10
12
|
import { createLooms } from '@swirls/looms/runtime';
|
|
11
13
|
const VERSION = '0.0.1';
|
|
12
14
|
const urlFlag = Flag.string('url').pipe(Flag.withDescription('Looms host base URL (env LOOMS_URL)'), Flag.withFallbackConfig(Config.string('LOOMS_URL')), Flag.withDefault('http://127.0.0.1:8787'), Flag.map((url) => url.replace(/\/$/, '')));
|
|
@@ -74,7 +76,7 @@ const approve = Command.make('approve', {
|
|
|
74
76
|
}
|
|
75
77
|
const outcome = accept ? 'approve' : 'reject';
|
|
76
78
|
const { url } = yield* loomsBase;
|
|
77
|
-
const result = yield* tryClient(() => clientFor(url).signal(runId, [decision(approvalId, outcome)]));
|
|
79
|
+
const result = yield* tryClient(() => clientFor(url).signal(runId, [decision({ approvalId, choice: outcome })]));
|
|
78
80
|
yield* Console.log(stringifyJsonPretty(result));
|
|
79
81
|
return undefined;
|
|
80
82
|
})).pipe(Command.withDescription('Approve or reject a pending approval'), Command.withShortDescription('Decide an approval'));
|
|
@@ -101,7 +103,7 @@ const steer = Command.make('steer', {
|
|
|
101
103
|
interrupt: Flag.boolean('interrupt').pipe(Flag.withDescription('Interrupt the current turn (use --no-interrupt to disable)'), Flag.withDefault(true)),
|
|
102
104
|
}, Effect.fn('steer')(function* ({ runId, message, interrupt }) {
|
|
103
105
|
const { url } = yield* loomsBase;
|
|
104
|
-
const result = yield* tryClient(() => clientFor(url).signal(runId, [steerEvent(message.join(' '),
|
|
106
|
+
const result = yield* tryClient(() => clientFor(url).signal(runId, [steerEvent({ content: message.join(' '), interrupt })]));
|
|
105
107
|
yield* Console.log(stringifyJsonPretty(result));
|
|
106
108
|
})).pipe(Command.withDescription('Send a steering message to a running agent'), Command.withShortDescription('Steer an agent'));
|
|
107
109
|
const tail = Command.make('tail', {
|
|
@@ -118,21 +120,63 @@ const tail = Command.make('tail', {
|
|
|
118
120
|
yield* Stream.fromQueue(queue).pipe(Stream.runForEach((line) => Console.log(line)));
|
|
119
121
|
}));
|
|
120
122
|
})).pipe(Command.withDescription('Stream events for a run'), Command.withShortDescription('Tail run events'));
|
|
121
|
-
const databaseFlag = Flag.string('database').pipe(Flag.withDescription('Postgres connection string
|
|
123
|
+
const databaseFlag = Flag.string('database').pipe(Flag.withDescription('Postgres connection string, or a SQLite file path with --dialect sqlite (env DATABASE_URL)'), Flag.withFallbackConfig(Config.string('DATABASE_URL')));
|
|
124
|
+
const dialectFlag = Flag.string('dialect').pipe(Flag.withDescription('Replica dialect: postgres (default) or sqlite'), Flag.withDefault('postgres'));
|
|
125
|
+
function asDialect(value) {
|
|
126
|
+
switch (value) {
|
|
127
|
+
case 'postgres':
|
|
128
|
+
case 'sqlite':
|
|
129
|
+
return value;
|
|
130
|
+
default:
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function openReplica(dialect, database, migrate) {
|
|
135
|
+
if (dialect === 'sqlite') {
|
|
136
|
+
const replica = openSqliteReplicaFile(database, migrate);
|
|
137
|
+
return {
|
|
138
|
+
pendingMigrations: () => replica.pendingMigrations(),
|
|
139
|
+
migrate: () => replica.migrate(),
|
|
140
|
+
status: (runId) => replica.status(runId),
|
|
141
|
+
dispose: () => replica.dispose?.() ?? Promise.resolve(),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const replica = postgresReplica({ url: database, migrate });
|
|
145
|
+
return {
|
|
146
|
+
pendingMigrations: () => replica.pendingMigrations(),
|
|
147
|
+
migrate: () => replica.migrate(),
|
|
148
|
+
status: (runId) => replica.status(runId),
|
|
149
|
+
dispose: () => replica.dispose?.() ?? Promise.resolve(),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
122
152
|
const generate = Command.make('generate', {
|
|
123
153
|
out: Flag.string('out').pipe(Flag.withDescription('Directory of SQL migrations to write into')),
|
|
124
|
-
|
|
154
|
+
dialect: dialectFlag,
|
|
155
|
+
journal: Flag.optional(Flag.string('journal').pipe(Flag.withDescription('Also update a migration journal. Only `drizzle` is supported, and only for Postgres'))),
|
|
125
156
|
check: Flag.boolean('check').pipe(Flag.withDescription('Exit non-zero when a migration is pending, and write nothing'), Flag.withDefault(false)),
|
|
126
|
-
}, Effect.fn('replica.generate')(function* ({ out, journal, check }) {
|
|
157
|
+
}, Effect.fn('replica.generate')(function* ({ out, dialect, journal, check }) {
|
|
158
|
+
const parsed = asDialect(dialect);
|
|
159
|
+
if (!parsed) {
|
|
160
|
+
return yield* new CliError.UserError({
|
|
161
|
+
cause: dialect,
|
|
162
|
+
userMessage: `Unknown dialect "${dialect}". Use "postgres" or "sqlite".`,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
127
165
|
if (Option.isSome(journal) && journal.value !== 'drizzle') {
|
|
128
166
|
return yield* new CliError.UserError({
|
|
129
167
|
cause: journal.value,
|
|
130
168
|
userMessage: `Unknown journal "${journal.value}". Only "drizzle" is supported.`,
|
|
131
169
|
});
|
|
132
170
|
}
|
|
171
|
+
if (parsed === 'sqlite' && Option.isSome(journal)) {
|
|
172
|
+
return yield* new CliError.UserError({
|
|
173
|
+
cause: journal.value,
|
|
174
|
+
userMessage: 'Drizzle journal output is only supported for the Postgres replica.',
|
|
175
|
+
});
|
|
176
|
+
}
|
|
133
177
|
const generateOptions = Option.isSome(journal)
|
|
134
|
-
? { out, check, journal: 'drizzle' }
|
|
135
|
-
: { out, check };
|
|
178
|
+
? { out, check, dialect: parsed, journal: 'drizzle' }
|
|
179
|
+
: { out, check, dialect: parsed };
|
|
136
180
|
const result = yield* tryClient(() => generateReplicaMigrations(generateOptions));
|
|
137
181
|
if (check && result.pending.length > 0) {
|
|
138
182
|
return yield* new CliError.UserError({
|
|
@@ -148,16 +192,43 @@ const generate = Command.make('generate', {
|
|
|
148
192
|
}
|
|
149
193
|
return yield* Effect.void;
|
|
150
194
|
})).pipe(Command.withDescription('Write replica migrations the host does not already have'), Command.withShortDescription('Write replica SQL'));
|
|
151
|
-
const migrate = Command.make('migrate', { database: databaseFlag }, Effect.fn('replica.migrate')(function* ({ database }) {
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
195
|
+
const migrate = Command.make('migrate', { database: databaseFlag, dialect: dialectFlag }, Effect.fn('replica.migrate')(function* ({ database, dialect }) {
|
|
196
|
+
const parsed = asDialect(dialect);
|
|
197
|
+
if (!parsed) {
|
|
198
|
+
return yield* new CliError.UserError({
|
|
199
|
+
cause: dialect,
|
|
200
|
+
userMessage: `Unknown dialect "${dialect}". Use "postgres" or "sqlite".`,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
if (parsed === 'sqlite' && /^postgres(ql)?:\/\//.test(database)) {
|
|
204
|
+
return yield* new CliError.UserError({
|
|
205
|
+
cause: database,
|
|
206
|
+
userMessage: 'SQLite replica --database is a file path, not a Postgres URL.',
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
const replica = openReplica(parsed, database, 'auto');
|
|
210
|
+
yield* tryClient(() => replica.migrate().then(() => replica.dispose()));
|
|
211
|
+
return yield* Console.log('Looms replica migrations applied');
|
|
155
212
|
})).pipe(Command.withDescription('Apply pending replica migrations'), Command.withShortDescription('Apply replica SQL'));
|
|
156
213
|
const status = Command.make('status', {
|
|
157
214
|
database: databaseFlag,
|
|
215
|
+
dialect: dialectFlag,
|
|
158
216
|
run: Flag.optional(Flag.string('run').pipe(Flag.withDescription('Also print freshness for this run id'))),
|
|
159
|
-
}, Effect.fn('replica.status')(function* ({ database, run }) {
|
|
160
|
-
const
|
|
217
|
+
}, Effect.fn('replica.status')(function* ({ database, dialect, run }) {
|
|
218
|
+
const parsed = asDialect(dialect);
|
|
219
|
+
if (!parsed) {
|
|
220
|
+
return yield* new CliError.UserError({
|
|
221
|
+
cause: dialect,
|
|
222
|
+
userMessage: `Unknown dialect "${dialect}". Use "postgres" or "sqlite".`,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (parsed === 'sqlite' && /^postgres(ql)?:\/\//.test(database)) {
|
|
226
|
+
return yield* new CliError.UserError({
|
|
227
|
+
cause: database,
|
|
228
|
+
userMessage: 'SQLite replica --database is a file path, not a Postgres URL.',
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
const replica = openReplica(parsed, database, 'verify');
|
|
161
232
|
const pending = yield* tryClient(() => replica.pendingMigrations());
|
|
162
233
|
yield* Console.log(pending.length === 0
|
|
163
234
|
? 'Looms replica schema is up to date'
|
|
@@ -166,9 +237,9 @@ const status = Command.make('status', {
|
|
|
166
237
|
const row = yield* tryClient(() => replica.status(run.value));
|
|
167
238
|
yield* Console.log(row ? stringifyJson(row) : `No replica row for ${run.value}`);
|
|
168
239
|
}
|
|
169
|
-
yield* tryClient(() => replica.dispose
|
|
240
|
+
return yield* tryClient(() => replica.dispose());
|
|
170
241
|
})).pipe(Command.withDescription('Show which replica migrations are applied'), Command.withShortDescription('Replica schema status'));
|
|
171
|
-
const replica = Command.make('replica').pipe(Command.withDescription('Copy runs into a Postgres database you own'), Command.withShortDescription('
|
|
242
|
+
const replica = Command.make('replica').pipe(Command.withDescription('Copy runs into a Postgres or SQLite database you own'), Command.withShortDescription('Event replica'), Command.withSubcommands([generate, migrate, status]));
|
|
172
243
|
export const looms = loomsBase.pipe(Command.withSubcommands([
|
|
173
244
|
serve,
|
|
174
245
|
start,
|
package/dist/client/client.d.ts
CHANGED
|
@@ -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: {
|
package/dist/client/client.js
CHANGED
|
@@ -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,
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.js
CHANGED
|
@@ -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
|
|
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 {};
|