@swirls/looms 0.24.0 → 0.25.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 (46) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/agent/threads.js +4 -1
  3. package/dist/approval/effects.d.ts +26 -26
  4. package/dist/approval/module.d.ts +52 -52
  5. package/dist/debugger/app/assets/index-GLtrulmj.js +23 -0
  6. package/dist/debugger/app/assets/{routes-Ch30UGik.js → routes-CECZJJob.js} +1 -1
  7. package/dist/debugger/app/index.html +1 -1
  8. package/dist/evaluator/effects.d.ts +33 -33
  9. package/dist/evaluator/module.d.ts +66 -66
  10. package/dist/schedule/module.d.ts +36 -36
  11. package/dist/swarm/board.d.ts +292 -0
  12. package/dist/swarm/board.js +123 -0
  13. package/dist/swarm/conformance.d.ts +15 -0
  14. package/dist/swarm/conformance.js +225 -0
  15. package/dist/swarm/definitions.d.ts +94 -0
  16. package/dist/swarm/definitions.js +51 -0
  17. package/dist/swarm/effects.d.ts +4720 -0
  18. package/dist/swarm/effects.js +296 -0
  19. package/dist/swarm/ensure.d.ts +16 -0
  20. package/dist/swarm/ensure.js +24 -0
  21. package/dist/swarm/events.d.ts +240 -0
  22. package/dist/swarm/events.js +49 -0
  23. package/dist/swarm/index.d.ts +15 -0
  24. package/dist/swarm/index.js +15 -0
  25. package/dist/swarm/memory-board.d.ts +10 -0
  26. package/dist/swarm/memory-board.js +344 -0
  27. package/dist/swarm/module.d.ts +9593 -0
  28. package/dist/swarm/module.js +94 -0
  29. package/dist/swarm/projections.d.ts +60 -0
  30. package/dist/swarm/projections.js +102 -0
  31. package/dist/swarm/prompt.d.ts +4 -0
  32. package/dist/swarm/prompt.js +35 -0
  33. package/dist/swarm/scope.d.ts +357 -0
  34. package/dist/swarm/scope.js +8 -0
  35. package/dist/swarm/services.d.ts +29 -0
  36. package/dist/swarm/services.js +40 -0
  37. package/dist/swarm/signals.d.ts +14 -0
  38. package/dist/swarm/signals.js +15 -0
  39. package/dist/swarm/steps.d.ts +15 -0
  40. package/dist/swarm/steps.js +27 -0
  41. package/dist/swarm/threads.d.ts +41 -0
  42. package/dist/swarm/threads.js +244 -0
  43. package/dist/swarm/tools.d.ts +19 -0
  44. package/dist/swarm/tools.js +100 -0
  45. package/package.json +7 -2
  46. package/dist/debugger/app/assets/index-CxbIGkpY.js +0 -22
@@ -0,0 +1,94 @@
1
+ import { defineModule, makeDefinitionStore } from '@swirls/looms/core';
2
+ import { activateEffect, claimTaskEffect, createTaskEffect, labelEffect, listTasksEffect, postEffect, pullEffect, readEffect, replyEffect, settleTaskEffect, } from './effects.js';
3
+ import { swarmStatus } from './projections.js';
4
+ import { swarmModule } from './scope.js';
5
+ import { SwarmServicesLive, memoryBoards } from './services.js';
6
+ import { nudge, stop } from './signals.js';
7
+ import { createTask, post } from './steps.js';
8
+ import { createSwarmThread } from './threads.js';
9
+ export function swarm(options = {}) {
10
+ const listed = options.definitions ?? [];
11
+ const definitions = makeDefinitionStore('swarm', listed);
12
+ const shared = options.board;
13
+ const boards = options.boards ?? (shared ? () => shared : memoryBoards());
14
+ return defineModule(swarmModule, () => ({
15
+ definitions: listed,
16
+ threads: { swarm: createSwarmThread(definitions) },
17
+ effects: {
18
+ pull: pullEffect,
19
+ activate: activateEffect,
20
+ post: postEffect,
21
+ reply: replyEffect,
22
+ read: readEffect,
23
+ label: labelEffect,
24
+ createTask: createTaskEffect,
25
+ listTasks: listTasksEffect,
26
+ claimTask: claimTaskEffect,
27
+ settleTask: settleTaskEffect,
28
+ },
29
+ projections: { swarmStatus },
30
+ signals: { nudge, stop },
31
+ steps: { post, createTask },
32
+ examples: [
33
+ {
34
+ name: 'an activation marks the member active until its run settles',
35
+ events: [
36
+ swarmModule.input('board.pulled', {
37
+ board: 'acme',
38
+ pull: 0,
39
+ cursor: '000000000001',
40
+ events: [
41
+ {
42
+ cursor: '000000000001',
43
+ type: 'message.posted',
44
+ at: 1_000,
45
+ by: 'dana',
46
+ message: {
47
+ id: 'msg_1',
48
+ channel: 'support',
49
+ text: '@triage a customer cannot log in',
50
+ by: 'dana',
51
+ re: null,
52
+ mentions: ['triage'],
53
+ labels: {},
54
+ createdAt: 1_000,
55
+ },
56
+ },
57
+ ],
58
+ more: false,
59
+ nextPullAt: null,
60
+ }),
61
+ swarmModule.input('member.activated', {
62
+ board: 'acme',
63
+ member: 'triage',
64
+ activation: 1,
65
+ runId: 'swarm_acme:triage:1',
66
+ events: 1,
67
+ at: 1_001,
68
+ }),
69
+ ],
70
+ projections: {
71
+ swarmStatus: {
72
+ status: 'running',
73
+ board: 'acme',
74
+ cursor: '000000000001',
75
+ eventsRead: 1,
76
+ members: {
77
+ triage: {
78
+ status: 'active',
79
+ activations: 1,
80
+ runId: 'swarm_acme:triage:1',
81
+ lastOutcome: null,
82
+ },
83
+ },
84
+ recent: [{ member: 'triage', runId: 'swarm_acme:triage:1', events: 1, at: 1_001 }],
85
+ },
86
+ },
87
+ },
88
+ ],
89
+ services: () => SwarmServicesLive(definitions, boards),
90
+ bindHost(host) {
91
+ definitions.useResolver(host.definitionResolver);
92
+ },
93
+ }));
94
+ }
@@ -0,0 +1,60 @@
1
+ import { Schema } from 'effect';
2
+ export declare const SwarmMemberStatusSchema: Schema.Struct<{
3
+ readonly status: Schema.Union<readonly [Schema.Literal<"idle">, Schema.Literal<"active">]>;
4
+ readonly activations: Schema.Finite;
5
+ /** The member's latest run, in progress or finished. */
6
+ readonly runId: Schema.NullOr<Schema.String>;
7
+ /** How the latest finished run ended. */
8
+ readonly lastOutcome: Schema.NullOr<Schema.Union<readonly [Schema.Literal<"completed">, Schema.Literal<"failed">, Schema.Literal<"cancelled">]>>;
9
+ }>;
10
+ export type SwarmMemberStatus = Schema.Schema.Type<typeof SwarmMemberStatusSchema>;
11
+ export declare const SwarmActivationSchema: Schema.Struct<{
12
+ readonly member: Schema.String;
13
+ readonly runId: Schema.String;
14
+ readonly events: Schema.Finite;
15
+ readonly at: Schema.Finite;
16
+ }>;
17
+ export type SwarmActivation = Schema.Schema.Type<typeof SwarmActivationSchema>;
18
+ export declare const SwarmStatusSchema: Schema.Struct<{
19
+ readonly status: Schema.Union<readonly [Schema.Literal<"running">, Schema.Literal<"stopping">, Schema.Literal<"completed">, Schema.Literal<"failed">, Schema.Literal<"cancelled">]>;
20
+ readonly board: Schema.NullOr<Schema.String>;
21
+ readonly cursor: Schema.NullOr<Schema.String>;
22
+ /** Board events the swarm has read. */
23
+ readonly eventsRead: Schema.Finite;
24
+ readonly members: Schema.$Record<Schema.String, Schema.Struct<{
25
+ readonly status: Schema.Union<readonly [Schema.Literal<"idle">, Schema.Literal<"active">]>;
26
+ readonly activations: Schema.Finite;
27
+ /** The member's latest run, in progress or finished. */
28
+ readonly runId: Schema.NullOr<Schema.String>;
29
+ /** How the latest finished run ended. */
30
+ readonly lastOutcome: Schema.NullOr<Schema.Union<readonly [Schema.Literal<"completed">, Schema.Literal<"failed">, Schema.Literal<"cancelled">]>>;
31
+ }>>;
32
+ readonly recent: Schema.$Array<Schema.Struct<{
33
+ readonly member: Schema.String;
34
+ readonly runId: Schema.String;
35
+ readonly events: Schema.Finite;
36
+ readonly at: Schema.Finite;
37
+ }>>;
38
+ }>;
39
+ export type SwarmStatus = Schema.Schema.Type<typeof SwarmStatusSchema>;
40
+ /** A swarm run at a glance: where it is on the board, and what each member is doing. */
41
+ export declare const swarmStatus: import("@swirls/looms/core").ProjectionDefinition<{
42
+ readonly status: "cancelled" | "completed" | "failed" | "running" | "stopping";
43
+ readonly board: string | null;
44
+ readonly cursor: string | null;
45
+ readonly eventsRead: number;
46
+ readonly members: {
47
+ readonly [x: string]: {
48
+ readonly status: "active" | "idle";
49
+ readonly activations: number;
50
+ readonly runId: string | null;
51
+ readonly lastOutcome: "cancelled" | "completed" | "failed" | null;
52
+ };
53
+ };
54
+ readonly recent: readonly {
55
+ readonly member: string;
56
+ readonly runId: string;
57
+ readonly events: number;
58
+ readonly at: number;
59
+ }[];
60
+ }>;
@@ -0,0 +1,102 @@
1
+ import { Schema } from 'effect';
2
+ import { swarmModule } from './scope.js';
3
+ const RECENT_LIMIT = 20;
4
+ export const SwarmMemberStatusSchema = Schema.Struct({
5
+ status: Schema.Union([Schema.Literal('idle'), Schema.Literal('active')]),
6
+ activations: Schema.Finite,
7
+ /** The member's latest run, in progress or finished. */
8
+ runId: Schema.NullOr(Schema.String),
9
+ /** How the latest finished run ended. */
10
+ lastOutcome: Schema.NullOr(Schema.Union([
11
+ Schema.Literal('completed'),
12
+ Schema.Literal('failed'),
13
+ Schema.Literal('cancelled'),
14
+ ])),
15
+ });
16
+ export const SwarmActivationSchema = Schema.Struct({
17
+ member: Schema.String,
18
+ runId: Schema.String,
19
+ events: Schema.Finite,
20
+ at: Schema.Finite,
21
+ });
22
+ export const SwarmStatusSchema = Schema.Struct({
23
+ status: Schema.Union([
24
+ Schema.Literal('running'),
25
+ Schema.Literal('stopping'),
26
+ Schema.Literal('completed'),
27
+ Schema.Literal('failed'),
28
+ Schema.Literal('cancelled'),
29
+ ]),
30
+ board: Schema.NullOr(Schema.String),
31
+ cursor: Schema.NullOr(Schema.String),
32
+ /** Board events the swarm has read. */
33
+ eventsRead: Schema.Finite,
34
+ members: Schema.Record(Schema.String, SwarmMemberStatusSchema),
35
+ recent: Schema.Array(SwarmActivationSchema),
36
+ });
37
+ /** A swarm run at a glance: where it is on the board, and what each member is doing. */
38
+ export const swarmStatus = swarmModule.projection({
39
+ name: 'swarmStatus',
40
+ shape: SwarmStatusSchema,
41
+ initialState: {
42
+ status: 'running',
43
+ board: null,
44
+ cursor: null,
45
+ eventsRead: 0,
46
+ members: {},
47
+ recent: [],
48
+ },
49
+ reduce(state, event) {
50
+ switch (event.type) {
51
+ case 'swarm.board.pulled':
52
+ return {
53
+ ...state,
54
+ board: event.payload.board,
55
+ cursor: event.payload.cursor,
56
+ eventsRead: state.eventsRead + event.payload.events.length,
57
+ };
58
+ case 'swarm.member.activated': {
59
+ const { member, runId, events, at, activation } = event.payload;
60
+ const recent = [...state.recent, { member, runId, events, at }];
61
+ return {
62
+ ...state,
63
+ members: {
64
+ ...state.members,
65
+ [member]: {
66
+ status: 'active',
67
+ activations: activation,
68
+ runId,
69
+ lastOutcome: state.members[member]?.lastOutcome ?? null,
70
+ },
71
+ },
72
+ recent: recent.slice(Math.max(0, recent.length - RECENT_LIMIT)),
73
+ };
74
+ }
75
+ case 'runtime.child.settled': {
76
+ const entry = Object.entries(state.members).find(([, member]) => member.status === 'active' && member.runId === event.payload.runId);
77
+ if (!entry) {
78
+ return state;
79
+ }
80
+ const [name, member] = entry;
81
+ return {
82
+ ...state,
83
+ members: {
84
+ ...state.members,
85
+ [name]: { ...member, status: 'idle', lastOutcome: event.payload.status },
86
+ },
87
+ };
88
+ }
89
+ case 'swarm.stopped':
90
+ return state.status === 'running' ? { ...state, status: 'stopping' } : state;
91
+ case 'runtime.thread.completed':
92
+ return { ...state, status: 'completed' };
93
+ case 'runtime.thread.failed':
94
+ return { ...state, status: 'failed' };
95
+ case 'runtime.run.cancelled':
96
+ case 'runtime.thread.cancelled':
97
+ return { ...state, status: 'cancelled' };
98
+ default:
99
+ return state;
100
+ }
101
+ },
102
+ });
@@ -0,0 +1,4 @@
1
+ import type { BoardEvent } from './board';
2
+ import type { SwarmDefinition, SwarmMember } from './definitions';
3
+ /** The first user message of a member's run: who it is, who else is here, and what happened. */
4
+ export declare function activationPrompt(definition: SwarmDefinition, member: SwarmMember, events: readonly BoardEvent[]): string;
@@ -0,0 +1,35 @@
1
+ const TEXT_LIMIT = 600;
2
+ function clip(text) {
3
+ return text.length > TEXT_LIMIT ? `${text.slice(0, TEXT_LIMIT)}…` : text;
4
+ }
5
+ function describeEvent(event, member) {
6
+ const { message, task } = event;
7
+ if (task) {
8
+ const assigned = task.for === member ? ' (assigned to you)' : '';
9
+ const what = event.type === 'task.created' ? `created by ${task.by}` : event.type.slice(5);
10
+ return `- task [${task.id}] in ${task.channel}, ${what}, now ${task.state}${assigned}: ${clip(task.text)}`;
11
+ }
12
+ if (message) {
13
+ const where = message.re === null ? message.channel : `${message.channel}, thread [${message.re}]`;
14
+ const mentioned = message.mentions.includes(member) ? ' (mentions you)' : '';
15
+ return `- message [${message.id}] by ${message.by} in ${where}${mentioned}: ${clip(message.text)}`;
16
+ }
17
+ return `- ${event.type} by ${event.by}`;
18
+ }
19
+ /** The first user message of a member's run: who it is, who else is here, and what happened. */
20
+ export function activationPrompt(definition, member, events) {
21
+ const others = definition.members
22
+ .filter((other) => other.name !== member.name)
23
+ .map((other) => `- @${other.name}${other.description ? `: ${other.description}` : ''}`);
24
+ return [
25
+ `You are "${member.name}", a member of the "${definition.name}" swarm.`,
26
+ 'The swarm shares one message board, and it is the only way members coordinate. Nothing you write outside the board reaches anyone. You keep no memory between activations, so put anything worth keeping on the board.',
27
+ 'Use the board tools to read threads, reply, hand work over by mentioning a member with @name or by creating a task, and claim a task before you work on it. Settle every task you claim.',
28
+ definition.instructions,
29
+ others.length > 0 ? `Other members:\n${others.join('\n')}` : undefined,
30
+ `New on the board for you, oldest first:\n${events.map((event) => describeEvent(event, member.name)).join('\n')}`,
31
+ 'Deal with this activity, then end your turn. You will be woken again when the board has something new for you.',
32
+ ]
33
+ .filter((part) => part !== undefined && part !== '')
34
+ .join('\n\n');
35
+ }