@swirls/looms 0.24.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/dist/actor/cell.d.ts +3 -1
- package/dist/actor/cell.js +1 -0
- package/dist/actor/host.d.ts +3 -1
- package/dist/actor/host.js +2 -1
- package/dist/actor/routing.d.ts +6 -3
- package/dist/actor/routing.js +6 -4
- package/dist/agent/agent-session.js +2 -2
- package/dist/agent/debugger/agent-chat.js +4 -3
- package/dist/agent/definitions.d.ts +3 -1
- package/dist/agent/effects.js +5 -3
- package/dist/agent/module.d.ts +730 -730
- package/dist/agent/threads.js +4 -1
- package/dist/approval/effects.d.ts +26 -26
- package/dist/approval/module.d.ts +52 -52
- package/dist/approval/steps.d.ts +3 -1
- package/dist/approval/steps.js +2 -2
- package/dist/client/client.d.ts +7 -1
- package/dist/client/client.js +5 -2
- package/dist/cloudflare/durable-object.d.ts +3 -1
- package/dist/cloudflare/durable-object.js +1 -0
- package/dist/cloudflare/queue-consumer.d.ts +3 -1
- package/dist/cloudflare/queue-consumer.js +2 -1
- package/dist/cloudflare/router.d.ts +3 -1
- package/dist/cloudflare/router.js +1 -1
- package/dist/core/compose.d.ts +8 -2
- package/dist/core/compose.js +6 -3
- package/dist/core/effects.d.ts +3 -0
- package/dist/core/envelope.d.ts +2 -0
- package/dist/core/envelope.js +2 -2
- package/dist/core/event-codec.d.ts +4 -3
- package/dist/core/event-codec.js +7 -7
- package/dist/core/fold.d.ts +3 -0
- package/dist/core/fold.js +3 -1
- package/dist/core/fork.js +1 -1
- package/dist/core/ids.d.ts +70 -5
- package/dist/core/ids.js +164 -41
- package/dist/core/snapshots.d.ts +2 -0
- package/dist/core/snapshots.js +2 -1
- package/dist/core/thread.d.ts +8 -0
- package/dist/debugger/app/assets/index-D_FKxPrN.js +23 -0
- package/dist/debugger/app/assets/routes-CkwzFejg.js +47 -0
- package/dist/debugger/app/index.html +1 -1
- package/dist/debugger/context.d.ts +4 -1
- package/dist/debugger/context.js +4 -4
- package/dist/debugger/shell/debugger-shell.d.ts +4 -1
- package/dist/debugger/shell/debugger-shell.js +2 -2
- package/dist/debugger/shell/use-start-run.d.ts +1 -1
- package/dist/debugger/shell/use-start-run.js +1 -2
- package/dist/evaluator/steps.d.ts +2 -2
- package/dist/evaluator/steps.js +3 -3
- package/dist/react/react.d.ts +8 -3
- package/dist/react/react.js +15 -2
- package/dist/react/store.d.ts +5 -1
- package/dist/react/store.js +5 -4
- package/dist/runtime/cancellation.js +5 -5
- package/dist/runtime/deadline-transition-planner.d.ts +1 -0
- package/dist/runtime/deadline-transition-planner.js +8 -7
- package/dist/runtime/effect-admission.js +1 -0
- package/dist/runtime/helpers.d.ts +6 -5
- package/dist/runtime/helpers.js +8 -8
- package/dist/runtime/local-effect-executor.js +15 -10
- package/dist/runtime/looms.d.ts +6 -1
- package/dist/runtime/looms.js +4 -1
- package/dist/runtime/retry-policy.d.ts +2 -1
- package/dist/runtime/retry-policy.js +2 -1
- package/dist/runtime/runtime.js +10 -10
- package/dist/runtime/server/routes/runs.js +5 -5
- package/dist/runtime/types.d.ts +7 -1
- package/dist/runtime/wake-engine.js +10 -9
- package/dist/runtime/worker-callbacks.js +9 -8
- package/dist/runtime/worker-effect-dispatch.d.ts +3 -2
- package/dist/runtime/worker-effect-dispatch.js +7 -5
- package/dist/swarm/board.d.ts +292 -0
- package/dist/swarm/board.js +123 -0
- package/dist/swarm/conformance.d.ts +15 -0
- package/dist/swarm/conformance.js +225 -0
- package/dist/swarm/definitions.d.ts +94 -0
- package/dist/swarm/definitions.js +51 -0
- package/dist/swarm/effects.d.ts +4720 -0
- package/dist/swarm/effects.js +296 -0
- package/dist/swarm/ensure.d.ts +16 -0
- package/dist/swarm/ensure.js +24 -0
- package/dist/swarm/events.d.ts +240 -0
- package/dist/swarm/events.js +49 -0
- package/dist/swarm/index.d.ts +15 -0
- package/dist/swarm/index.js +15 -0
- package/dist/swarm/memory-board.d.ts +10 -0
- package/dist/swarm/memory-board.js +344 -0
- package/dist/swarm/module.d.ts +9593 -0
- package/dist/swarm/module.js +94 -0
- package/dist/swarm/projections.d.ts +60 -0
- package/dist/swarm/projections.js +102 -0
- package/dist/swarm/prompt.d.ts +4 -0
- package/dist/swarm/prompt.js +35 -0
- package/dist/swarm/scope.d.ts +357 -0
- package/dist/swarm/scope.js +8 -0
- package/dist/swarm/services.d.ts +29 -0
- package/dist/swarm/services.js +40 -0
- package/dist/swarm/signals.d.ts +14 -0
- package/dist/swarm/signals.js +15 -0
- package/dist/swarm/steps.d.ts +15 -0
- package/dist/swarm/steps.js +27 -0
- package/dist/swarm/threads.d.ts +41 -0
- package/dist/swarm/threads.js +244 -0
- package/dist/swarm/tools.d.ts +20 -0
- package/dist/swarm/tools.js +100 -0
- package/dist/workflow/effects.js +6 -5
- package/dist/workflow/graph.d.ts +3 -3
- package/dist/workflow/graph.js +5 -5
- package/dist/workflow/module.d.ts +8 -2
- package/dist/workflow/threads.d.ts +14 -2
- package/dist/workflow/threads.js +54 -44
- package/package.json +7 -2
- package/dist/debugger/app/assets/index-CxbIGkpY.js +0 -22
- package/dist/debugger/app/assets/routes-Ch30UGik.js +0 -47
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { Option, Schema } from 'effect';
|
|
2
|
+
import { complete, fail, invoke, parseEffectId, } from '@swirls/looms/core';
|
|
3
|
+
import { channelMatches } from './board.js';
|
|
4
|
+
import { SwarmStartSchema } from './definitions.js';
|
|
5
|
+
import { activateEffect, pullEffect } from './effects.js';
|
|
6
|
+
import { swarmModule } from './scope.js';
|
|
7
|
+
/** Board events a member can have queued. Older ones are dropped and counted. */
|
|
8
|
+
export const INBOX_LIMIT = 50;
|
|
9
|
+
const decodeStart = Schema.decodeUnknownOption(SwarmStartSchema);
|
|
10
|
+
const EMPTY_MEMBER = {
|
|
11
|
+
activations: 0,
|
|
12
|
+
inbox: [],
|
|
13
|
+
batch: null,
|
|
14
|
+
runId: null,
|
|
15
|
+
dropped: 0,
|
|
16
|
+
lastError: null,
|
|
17
|
+
};
|
|
18
|
+
function listens(member, channel) {
|
|
19
|
+
return (member.channels ?? []).some((filter) => channelMatches(filter, channel));
|
|
20
|
+
}
|
|
21
|
+
/** Whether a board event should wake `member`. A member is never woken by its own writes. */
|
|
22
|
+
export function concerns(member, event) {
|
|
23
|
+
if (event.by === member.name) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const { message, task } = event;
|
|
27
|
+
if (message && (event.type === 'message.posted' || event.type === 'task.created')) {
|
|
28
|
+
if (message.mentions.includes(member.name)) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (task) {
|
|
33
|
+
const offered = task.state === 'ready' &&
|
|
34
|
+
(event.type === 'task.created' ||
|
|
35
|
+
event.type === 'task.ready' ||
|
|
36
|
+
event.type === 'task.reopened');
|
|
37
|
+
return (offered && (task.for === member.name || (task.for === null && listens(member, task.channel))));
|
|
38
|
+
}
|
|
39
|
+
return (event.type === 'message.posted' && message !== undefined && listens(member, message.channel));
|
|
40
|
+
}
|
|
41
|
+
function deliver(state, definition, events) {
|
|
42
|
+
const woken = definition.members.flatMap((member) => {
|
|
43
|
+
const concerning = events.filter((event) => concerns(member, event));
|
|
44
|
+
if (concerning.length === 0) {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
const current = state.members[member.name] ?? EMPTY_MEMBER;
|
|
48
|
+
const inbox = [...current.inbox, ...concerning];
|
|
49
|
+
const overflow = Math.max(0, inbox.length - INBOX_LIMIT);
|
|
50
|
+
const next = {
|
|
51
|
+
...current,
|
|
52
|
+
inbox: inbox.slice(overflow),
|
|
53
|
+
dropped: current.dropped + overflow,
|
|
54
|
+
};
|
|
55
|
+
return [[member.name, next]];
|
|
56
|
+
});
|
|
57
|
+
return { ...state.members, ...Object.fromEntries(woken) };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Turn each idle member's inbox into its next activation. The batch is fixed here, in the
|
|
61
|
+
* reducer, so the activation effect has the same input however often its thread is asked for it.
|
|
62
|
+
*/
|
|
63
|
+
function cutBatches(state) {
|
|
64
|
+
if (state.stopped) {
|
|
65
|
+
return state;
|
|
66
|
+
}
|
|
67
|
+
let changed = false;
|
|
68
|
+
const members = {};
|
|
69
|
+
for (const [name, member] of Object.entries(state.members)) {
|
|
70
|
+
if (member.batch === null && member.runId === null && member.inbox.length > 0) {
|
|
71
|
+
changed = true;
|
|
72
|
+
members[name] = {
|
|
73
|
+
...member,
|
|
74
|
+
activations: member.activations + 1,
|
|
75
|
+
batch: member.inbox,
|
|
76
|
+
inbox: [],
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
members[name] = member;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return changed ? { ...state, members } : state;
|
|
84
|
+
}
|
|
85
|
+
/** Ask for a pull now, or after the pull in flight when one may already be reading the board. */
|
|
86
|
+
function requestPull(state) {
|
|
87
|
+
return state.pullNow ? { ...state, dirty: true } : { ...state, pullNow: true };
|
|
88
|
+
}
|
|
89
|
+
function activationTag(member, activation) {
|
|
90
|
+
return `activate:${member}:${activation}`;
|
|
91
|
+
}
|
|
92
|
+
export function createSwarmThread(definitions) {
|
|
93
|
+
return swarmModule.thread({
|
|
94
|
+
kind: 'swarm',
|
|
95
|
+
input: SwarmStartSchema,
|
|
96
|
+
forgetsCompletedEffects: true,
|
|
97
|
+
initialState: (ctx) => {
|
|
98
|
+
const definition = definitions.get(ctx.definitionName, ctx.definitionVersion);
|
|
99
|
+
const start = Option.getOrUndefined(decodeStart(ctx.input));
|
|
100
|
+
const config = start && start !== null ? start : {};
|
|
101
|
+
return {
|
|
102
|
+
definitionName: ctx.definitionName,
|
|
103
|
+
definitionVersion: ctx.definitionVersion,
|
|
104
|
+
board: config.board ?? ctx.runId,
|
|
105
|
+
cursor: config.after ?? null,
|
|
106
|
+
pulls: 0,
|
|
107
|
+
pullNow: true,
|
|
108
|
+
nextPullAt: null,
|
|
109
|
+
dirty: false,
|
|
110
|
+
members: {},
|
|
111
|
+
stopped: false,
|
|
112
|
+
invalid: definition
|
|
113
|
+
? null
|
|
114
|
+
: `Unknown swarm definition "${ctx.definitionName}@${ctx.definitionVersion}"`,
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
step(state, event, ctx) {
|
|
118
|
+
switch (event.type) {
|
|
119
|
+
case 'swarm.board.pulled': {
|
|
120
|
+
if (event.payload.pull !== state.pulls) {
|
|
121
|
+
return state;
|
|
122
|
+
}
|
|
123
|
+
const definition = definitions.get(state.definitionName, state.definitionVersion);
|
|
124
|
+
return cutBatches({
|
|
125
|
+
...state,
|
|
126
|
+
cursor: event.payload.cursor,
|
|
127
|
+
pulls: state.pulls + 1,
|
|
128
|
+
pullNow: state.dirty || event.payload.more,
|
|
129
|
+
nextPullAt: event.payload.nextPullAt,
|
|
130
|
+
dirty: false,
|
|
131
|
+
members: definition ? deliver(state, definition, event.payload.events) : state.members,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
case 'swarm.member.activated': {
|
|
135
|
+
const member = state.members[event.payload.member];
|
|
136
|
+
if (!member || member.activations !== event.payload.activation) {
|
|
137
|
+
return state;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
...state,
|
|
141
|
+
members: {
|
|
142
|
+
...state.members,
|
|
143
|
+
[event.payload.member]: {
|
|
144
|
+
...member,
|
|
145
|
+
batch: null,
|
|
146
|
+
runId: event.payload.runId,
|
|
147
|
+
lastError: null,
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
case 'runtime.child.settled': {
|
|
153
|
+
const entry = Object.entries(state.members).find(([, member]) => member.runId === event.payload.runId);
|
|
154
|
+
if (!entry) {
|
|
155
|
+
return state;
|
|
156
|
+
}
|
|
157
|
+
const [name, member] = entry;
|
|
158
|
+
// The member most likely wrote to the board, so read it again before going idle.
|
|
159
|
+
return cutBatches(requestPull({
|
|
160
|
+
...state,
|
|
161
|
+
members: {
|
|
162
|
+
...state.members,
|
|
163
|
+
[name]: { ...member, runId: null, lastError: event.payload.error },
|
|
164
|
+
},
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
case 'runtime.effect.failed': {
|
|
168
|
+
const tag = event.effectId ? parseEffectId(event.effectId)?.tag : undefined;
|
|
169
|
+
const entry = Object.entries(state.members).find(([name, member]) => member.batch !== null && tag === activationTag(name, member.activations));
|
|
170
|
+
if (!entry || event.threadId !== ctx.threadId) {
|
|
171
|
+
return state;
|
|
172
|
+
}
|
|
173
|
+
const [name, member] = entry;
|
|
174
|
+
// The activation could not start. Drop its batch so the member can be woken again.
|
|
175
|
+
return cutBatches({
|
|
176
|
+
...state,
|
|
177
|
+
members: {
|
|
178
|
+
...state.members,
|
|
179
|
+
[name]: {
|
|
180
|
+
...member,
|
|
181
|
+
batch: null,
|
|
182
|
+
dropped: member.dropped + (member.batch?.length ?? 0),
|
|
183
|
+
lastError: event.payload.error || 'activation failed',
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
case 'swarm.nudged':
|
|
189
|
+
return state.stopped ? state : requestPull(state);
|
|
190
|
+
case 'swarm.stopped': {
|
|
191
|
+
const members = {};
|
|
192
|
+
for (const [name, member] of Object.entries(state.members)) {
|
|
193
|
+
members[name] = { ...member, batch: null, inbox: [] };
|
|
194
|
+
}
|
|
195
|
+
return { ...state, stopped: true, members };
|
|
196
|
+
}
|
|
197
|
+
default:
|
|
198
|
+
return state;
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
effects(state) {
|
|
202
|
+
if (state.invalid) {
|
|
203
|
+
return [fail(state.invalid)];
|
|
204
|
+
}
|
|
205
|
+
const members = Object.entries(state.members);
|
|
206
|
+
if (state.stopped) {
|
|
207
|
+
// Let members that are mid-run finish before the swarm completes.
|
|
208
|
+
return members.some(([, member]) => member.runId !== null)
|
|
209
|
+
? []
|
|
210
|
+
: [
|
|
211
|
+
complete({
|
|
212
|
+
activations: members.reduce((total, [, member]) => total + member.activations, 0),
|
|
213
|
+
}),
|
|
214
|
+
];
|
|
215
|
+
}
|
|
216
|
+
const definition = {
|
|
217
|
+
definitionName: state.definitionName,
|
|
218
|
+
definitionVersion: state.definitionVersion,
|
|
219
|
+
};
|
|
220
|
+
const effects = [];
|
|
221
|
+
const pull = { ...definition, board: state.board, after: state.cursor, pull: state.pulls };
|
|
222
|
+
if (state.pullNow) {
|
|
223
|
+
effects.push(invoke(pullEffect, pull, `pull:${state.pulls}:now`));
|
|
224
|
+
}
|
|
225
|
+
else if (state.nextPullAt !== null) {
|
|
226
|
+
effects.push(invoke(pullEffect, pull, `pull:${state.pulls}:${state.nextPullAt}`, {
|
|
227
|
+
at: state.nextPullAt,
|
|
228
|
+
}));
|
|
229
|
+
}
|
|
230
|
+
for (const [name, member] of members) {
|
|
231
|
+
if (member.batch !== null && member.runId === null) {
|
|
232
|
+
effects.push(invoke(activateEffect, {
|
|
233
|
+
...definition,
|
|
234
|
+
board: state.board,
|
|
235
|
+
member: name,
|
|
236
|
+
activation: member.activations,
|
|
237
|
+
events: member.batch,
|
|
238
|
+
}, activationTag(name, member.activations)));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return effects;
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type EffectsTool } from '@swirls/looms/agent';
|
|
2
|
+
import { type JsonValue, type LoomsIds } from '@swirls/looms/core';
|
|
3
|
+
import type { BoardBinding } from './steps';
|
|
4
|
+
/**
|
|
5
|
+
* The board as agent tools, writing as `binding.as` on `binding.board`. Build a fresh set for
|
|
6
|
+
* each turn (`resolveTools` does), because every tool carries the id of the one call it serves.
|
|
7
|
+
*/
|
|
8
|
+
export declare function boardTools(binding: BoardBinding, ids?: LoomsIds): EffectsTool[];
|
|
9
|
+
/**
|
|
10
|
+
* The board tools of the swarm member a run was started for, or none when the run was not
|
|
11
|
+
* started by a swarm. Pass it as an agent's `resolveTools`:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const triage = defineAgent({ name: 'triage', instructions, resolveTools: memberTools })
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function memberTools(ctx: {
|
|
18
|
+
readonly input: JsonValue;
|
|
19
|
+
readonly ids?: LoomsIds;
|
|
20
|
+
}): EffectsTool[];
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Predicate } from 'effect';
|
|
2
|
+
import { asEffectsTool } from '@swirls/looms/agent';
|
|
3
|
+
import { invoke, typeIds } from '@swirls/looms/core';
|
|
4
|
+
import { readActivation } from './definitions.js';
|
|
5
|
+
import { ClaimTaskArgsSchema, CreateTaskArgsSchema, LabelArgsSchema, ListTasksArgsSchema, PostArgsSchema, ReadArgsSchema, ReplyArgsSchema, SettleTaskArgsSchema, claimTaskEffect, createTaskEffect, labelEffect, listTasksEffect, postEffect, readEffect, replyEffect, settleTaskEffect, } from './effects.js';
|
|
6
|
+
/**
|
|
7
|
+
* A tool whose call becomes one board effect. The agent waits for the event that carries the
|
|
8
|
+
* same `callId`: the board's answer, or `swarm.call.rejected` when the board refused.
|
|
9
|
+
*/
|
|
10
|
+
function boardTool(binding, def, ids) {
|
|
11
|
+
const callId = ids.create('wait');
|
|
12
|
+
return asEffectsTool({
|
|
13
|
+
name: def.name,
|
|
14
|
+
description: def.description,
|
|
15
|
+
input: def.input,
|
|
16
|
+
effects: (args) => {
|
|
17
|
+
const fields = Predicate.isObject(args) ? args : {};
|
|
18
|
+
return [invoke(def.effect.type, { ...fields, callId, board: binding.board, as: binding.as })];
|
|
19
|
+
},
|
|
20
|
+
waitOn: { type: [def.answer, 'swarm.call.rejected'], match: { callId } },
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The board as agent tools, writing as `binding.as` on `binding.board`. Build a fresh set for
|
|
25
|
+
* each turn (`resolveTools` does), because every tool carries the id of the one call it serves.
|
|
26
|
+
*/
|
|
27
|
+
export function boardTools(binding, ids = typeIds) {
|
|
28
|
+
return [
|
|
29
|
+
boardTool(binding, {
|
|
30
|
+
name: 'board_read',
|
|
31
|
+
description: 'Read messages from the board, oldest first. Pass `thread` with a message id to read a whole thread, `channel` to read a channel ("support") or a subtree ("support/"), or `mentionsMe` for messages that mention you. Without `after` you get the latest `limit` matches.',
|
|
32
|
+
input: ReadArgsSchema,
|
|
33
|
+
effect: readEffect,
|
|
34
|
+
answer: 'swarm.messages.read',
|
|
35
|
+
}, ids),
|
|
36
|
+
boardTool(binding, {
|
|
37
|
+
name: 'board_post',
|
|
38
|
+
description: 'Start a new thread in a channel. Mention a member with @name to wake them. To continue an existing thread, use board_reply.',
|
|
39
|
+
input: PostArgsSchema,
|
|
40
|
+
effect: postEffect,
|
|
41
|
+
answer: 'swarm.message.posted',
|
|
42
|
+
}, ids),
|
|
43
|
+
boardTool(binding, {
|
|
44
|
+
name: 'board_reply',
|
|
45
|
+
description: 'Reply in the thread of message `to`. Use it to answer, report progress on a task, or mention a member with @name.',
|
|
46
|
+
input: ReplyArgsSchema,
|
|
47
|
+
effect: replyEffect,
|
|
48
|
+
answer: 'swarm.message.posted',
|
|
49
|
+
}, ids),
|
|
50
|
+
boardTool(binding, {
|
|
51
|
+
name: 'board_label',
|
|
52
|
+
description: 'Set or unset key=value labels on a message to track state. Pass `if` to change them only when the labels still have the values you expect (null means unset).',
|
|
53
|
+
input: LabelArgsSchema,
|
|
54
|
+
effect: labelEffect,
|
|
55
|
+
answer: 'swarm.message.labeled',
|
|
56
|
+
}, ids),
|
|
57
|
+
boardTool(binding, {
|
|
58
|
+
name: 'board_create_task',
|
|
59
|
+
description: 'Put a unit of work on the board for a member to claim. Pass `for` to assign it to one member, and `needs` with task ids that must complete first. The text is the instructions for whoever claims it.',
|
|
60
|
+
input: CreateTaskArgsSchema,
|
|
61
|
+
effect: createTaskEffect,
|
|
62
|
+
answer: 'swarm.task.created',
|
|
63
|
+
}, ids),
|
|
64
|
+
boardTool(binding, {
|
|
65
|
+
name: 'board_list_tasks',
|
|
66
|
+
description: 'List tasks, oldest first, filtered by channel, state, or assignee.',
|
|
67
|
+
input: ListTasksArgsSchema,
|
|
68
|
+
effect: listTasksEffect,
|
|
69
|
+
answer: 'swarm.tasks.listed',
|
|
70
|
+
}, ids),
|
|
71
|
+
boardTool(binding, {
|
|
72
|
+
name: 'board_claim_task',
|
|
73
|
+
description: 'Claim a task before working on it, so no other member does the same work. Pass `task` to claim one task, or `channel` to take the oldest ready task you may work on. The result has a `token`: keep it for board_settle_task. `claim` is null when nothing is ready.',
|
|
74
|
+
input: ClaimTaskArgsSchema,
|
|
75
|
+
effect: claimTaskEffect,
|
|
76
|
+
answer: 'swarm.task.claimed',
|
|
77
|
+
}, ids),
|
|
78
|
+
boardTool(binding, {
|
|
79
|
+
name: 'board_settle_task',
|
|
80
|
+
description: 'Finish a task you claimed, using the token from board_claim_task. Outcome "completed" with the result in `note`, "failed" with the reason, "blocked" with what it is waiting on, or "released" to return it to the queue untouched.',
|
|
81
|
+
input: SettleTaskArgsSchema,
|
|
82
|
+
effect: settleTaskEffect,
|
|
83
|
+
answer: 'swarm.task.settled',
|
|
84
|
+
}, ids),
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The board tools of the swarm member a run was started for, or none when the run was not
|
|
89
|
+
* started by a swarm. Pass it as an agent's `resolveTools`:
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* const triage = defineAgent({ name: 'triage', instructions, resolveTools: memberTools })
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export function memberTools(ctx) {
|
|
96
|
+
const activation = readActivation(ctx.input);
|
|
97
|
+
return activation
|
|
98
|
+
? boardTools({ board: activation.swarm.board, as: activation.swarm.member }, ctx.ids)
|
|
99
|
+
: [];
|
|
100
|
+
}
|
package/dist/workflow/effects.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Clock, Data, Effect, Predicate, Schema } from 'effect';
|
|
2
|
-
import {
|
|
2
|
+
import { createWaitId, DEFAULT_DEFINITION_VERSION, } from '@swirls/looms/core';
|
|
3
3
|
import { NodeStateSchema, nodeResultBuilders, } from './definitions.js';
|
|
4
4
|
import { WorkflowDefinitionsTag } from './definitions-store.js';
|
|
5
5
|
import { assertJsonWithinLimit, DEFAULT_CHECKPOINT_SIZE_LIMIT_BYTES, DEFAULT_GRAPH_CONCURRENCY, getReadyNodeIds, getSkippableNodeIds, getUnusedFallbackIds, iterationChildWorkflowId, nodeChildWorkflowId, nodeResults, nodeRunKey, } from './graph.js';
|
|
@@ -158,6 +158,7 @@ export const runNodeEffect = workflowModule.effect({
|
|
|
158
158
|
: undefined;
|
|
159
159
|
const run = {
|
|
160
160
|
threadId: ctx.threadId,
|
|
161
|
+
ids: ctx.ids,
|
|
161
162
|
nodeId: input.nodeId,
|
|
162
163
|
attempt: input.attempt,
|
|
163
164
|
iteration: input.iteration,
|
|
@@ -207,7 +208,7 @@ function runNode(definition, run) {
|
|
|
207
208
|
}
|
|
208
209
|
function nodeResultEvents(definition, nodeType, run, result) {
|
|
209
210
|
return Effect.gen(function* () {
|
|
210
|
-
const { threadId, nodeId, attempt } = run;
|
|
211
|
+
const { threadId, ids, nodeId, attempt } = run;
|
|
211
212
|
const outputLabel = `Node ${nodeId} output`;
|
|
212
213
|
switch (result.type) {
|
|
213
214
|
case 'value':
|
|
@@ -227,8 +228,8 @@ function nodeResultEvents(definition, nodeType, run, result) {
|
|
|
227
228
|
case 'spawn': {
|
|
228
229
|
const childThreadId = result.childThreadId ??
|
|
229
230
|
(nodeType === 'workflow'
|
|
230
|
-
? nodeChildWorkflowId(threadId, nodeId, attempt)
|
|
231
|
-
:
|
|
231
|
+
? nodeChildWorkflowId(threadId, nodeId, attempt, ids)
|
|
232
|
+
: ids.create('thread'));
|
|
232
233
|
return spawnRequested(definition, run, childThreadId, result, result.input);
|
|
233
234
|
}
|
|
234
235
|
case 'sleep': {
|
|
@@ -283,7 +284,7 @@ function nodeResultEvents(definition, nodeType, run, result) {
|
|
|
283
284
|
if (index >= result.maxIterations) {
|
|
284
285
|
return finished(run, run.iteration?.previous ?? null, null);
|
|
285
286
|
}
|
|
286
|
-
const childThreadId = iterationChildWorkflowId(threadId, nodeId, 'loop', attempt, index);
|
|
287
|
+
const childThreadId = iterationChildWorkflowId(threadId, nodeId, 'loop', attempt, index, ids);
|
|
287
288
|
return spawnRequested(definition, run, childThreadId, result, result.input, index);
|
|
288
289
|
}
|
|
289
290
|
default: {
|
package/dist/workflow/graph.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect } from 'effect';
|
|
2
|
-
import { type JsonValue } from '@swirls/looms/core';
|
|
2
|
+
import { type JsonValue, type LoomsIds } from '@swirls/looms/core';
|
|
3
3
|
import type { NodeState, WorkflowDefinition, WorkflowNodeDefinition } from './definitions';
|
|
4
4
|
export declare const MIN_GRAPH_CONCURRENCY = 1;
|
|
5
5
|
export declare const MAX_GRAPH_CONCURRENCY = 64;
|
|
@@ -34,8 +34,8 @@ export interface WorkflowEdge {
|
|
|
34
34
|
export type GraphNodeStatuses = Record<string, NodeState['status']>;
|
|
35
35
|
/** Distinguishes each attempt, and each loop iteration within it, of one node. */
|
|
36
36
|
export declare function nodeRunKey(nodeId: string, attempt: number, iteration?: number): string;
|
|
37
|
-
export declare function iterationChildWorkflowId(parentThreadId: string, nodeId: string, kind: 'map' | 'loop', attempt: number, index: number): string;
|
|
38
|
-
export declare function nodeChildWorkflowId(parentThreadId: string, nodeId: string, attempt: number): string;
|
|
37
|
+
export declare function iterationChildWorkflowId(parentThreadId: string, nodeId: string, kind: 'map' | 'loop', attempt: number, index: number, ids?: LoomsIds): string;
|
|
38
|
+
export declare function nodeChildWorkflowId(parentThreadId: string, nodeId: string, attempt: number, ids?: LoomsIds): string;
|
|
39
39
|
export declare function assertJsonWithinLimit(value: JsonValue | Readonly<object>, label: string, limitBytes?: number): Effect.Effect<void, WorkflowValueTooLargeError>;
|
|
40
40
|
export declare function resolveGraphConcurrency(value?: number): number;
|
|
41
41
|
export declare function workflowEdges<TInput>(definition: WorkflowDefinition<string, TInput>): WorkflowEdge[];
|
package/dist/workflow/graph.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Data, Effect } from 'effect';
|
|
2
|
-
import {
|
|
2
|
+
import { isJsonString, typeIds, utf8JsonBytes } from '@swirls/looms/core';
|
|
3
3
|
export const MIN_GRAPH_CONCURRENCY = 1;
|
|
4
4
|
export const MAX_GRAPH_CONCURRENCY = 64;
|
|
5
5
|
export const DEFAULT_GRAPH_CONCURRENCY = 8;
|
|
@@ -24,11 +24,11 @@ export class WorkflowValueTooLargeError extends Data.TaggedError('WorkflowValueT
|
|
|
24
24
|
export function nodeRunKey(nodeId, attempt, iteration) {
|
|
25
25
|
return iteration === undefined ? `${nodeId}_${attempt}` : `${nodeId}_${attempt}_${iteration}`;
|
|
26
26
|
}
|
|
27
|
-
export function iterationChildWorkflowId(parentThreadId, nodeId, kind, attempt, index) {
|
|
28
|
-
return
|
|
27
|
+
export function iterationChildWorkflowId(parentThreadId, nodeId, kind, attempt, index, ids = typeIds) {
|
|
28
|
+
return ids.derive('thread', [parentThreadId, nodeId, kind, attempt, index]);
|
|
29
29
|
}
|
|
30
|
-
export function nodeChildWorkflowId(parentThreadId, nodeId, attempt) {
|
|
31
|
-
return
|
|
30
|
+
export function nodeChildWorkflowId(parentThreadId, nodeId, attempt, ids = typeIds) {
|
|
31
|
+
return ids.derive('thread', [parentThreadId, nodeId, 'workflow', attempt]);
|
|
32
32
|
}
|
|
33
33
|
export function assertJsonWithinLimit(value, label, limitBytes = DEFAULT_VALUE_SIZE_LIMIT_BYTES) {
|
|
34
34
|
const actualBytes = utf8JsonBytes(value);
|
|
@@ -1089,7 +1089,10 @@ export declare function workflow(options?: WorkflowModuleOptions): import("@swir
|
|
|
1089
1089
|
readonly idempotencyKey?: string | null | undefined;
|
|
1090
1090
|
};
|
|
1091
1091
|
}[];
|
|
1092
|
-
readonly pendingCancels:
|
|
1092
|
+
readonly pendingCancels: {
|
|
1093
|
+
readonly target: string;
|
|
1094
|
+
readonly execution: "run" | "thread";
|
|
1095
|
+
}[];
|
|
1093
1096
|
readonly status: "completed" | "failed" | "running";
|
|
1094
1097
|
readonly error: string | null;
|
|
1095
1098
|
}>;
|
|
@@ -2078,7 +2081,10 @@ export declare function workflow(options?: WorkflowModuleOptions): import("@swir
|
|
|
2078
2081
|
readonly idempotencyKey?: string | null | undefined;
|
|
2079
2082
|
};
|
|
2080
2083
|
}[];
|
|
2081
|
-
readonly pendingCancels:
|
|
2084
|
+
readonly pendingCancels: {
|
|
2085
|
+
readonly target: string;
|
|
2086
|
+
readonly execution: "run" | "thread";
|
|
2087
|
+
}[];
|
|
2082
2088
|
readonly status: "completed" | "failed" | "running";
|
|
2083
2089
|
readonly error: string | null;
|
|
2084
2090
|
}>;
|
|
@@ -29,6 +29,12 @@ export declare const WorkflowPendingMapSchema: Schema.Struct<{
|
|
|
29
29
|
readonly close: Schema.withDecodingDefaultKey<Schema.Union<readonly [Schema.Literal<"terminate">, Schema.Literal<"wait">]>, never>;
|
|
30
30
|
}>;
|
|
31
31
|
export type WorkflowPendingMap = Schema.Schema.Type<typeof WorkflowPendingMapSchema>;
|
|
32
|
+
/** A child to cancel: a thread of this run, or a run of its own. */
|
|
33
|
+
export declare const WorkflowPendingCancelSchema: Schema.Struct<{
|
|
34
|
+
readonly target: Schema.String;
|
|
35
|
+
readonly execution: Schema.Union<readonly [Schema.Literal<"thread">, Schema.Literal<"run">]>;
|
|
36
|
+
}>;
|
|
37
|
+
export type WorkflowPendingCancel = Schema.Schema.Type<typeof WorkflowPendingCancelSchema>;
|
|
32
38
|
export declare const WorkflowPendingSleepSchema: Schema.Struct<{
|
|
33
39
|
readonly waitId: Schema.String;
|
|
34
40
|
readonly wakeAt: Schema.Finite;
|
|
@@ -300,7 +306,10 @@ export declare const WorkflowStateSchema: Schema.Struct<{
|
|
|
300
306
|
}>;
|
|
301
307
|
}>>>;
|
|
302
308
|
/** Children of stopped nodes that were still running; each is cancelled once. */
|
|
303
|
-
readonly pendingCancels: Schema.withDecodingDefaultKey<Schema.mutable<Schema.$Array<Schema.
|
|
309
|
+
readonly pendingCancels: Schema.withDecodingDefaultKey<Schema.mutable<Schema.$Array<Schema.Struct<{
|
|
310
|
+
readonly target: Schema.String;
|
|
311
|
+
readonly execution: Schema.Union<readonly [Schema.Literal<"thread">, Schema.Literal<"run">]>;
|
|
312
|
+
}>>>, never>;
|
|
304
313
|
readonly status: Schema.Union<readonly [Schema.Literal<"running">, Schema.Literal<"completed">, Schema.Literal<"failed">]>;
|
|
305
314
|
readonly error: Schema.NullOr<Schema.String>;
|
|
306
315
|
}>;
|
|
@@ -465,7 +474,10 @@ export declare const workflowThread: import("@swirls/looms/core").ThreadDefiniti
|
|
|
465
474
|
readonly idempotencyKey?: string | null | undefined;
|
|
466
475
|
};
|
|
467
476
|
}[];
|
|
468
|
-
readonly pendingCancels:
|
|
477
|
+
readonly pendingCancels: {
|
|
478
|
+
readonly target: string;
|
|
479
|
+
readonly execution: "run" | "thread";
|
|
480
|
+
}[];
|
|
469
481
|
readonly status: "completed" | "failed" | "running";
|
|
470
482
|
readonly error: string | null;
|
|
471
483
|
}>;
|