@swirls/looms 0.11.0 → 0.11.2

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @swirls/looms
2
2
 
3
+ ## 0.11.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 58c214c: Rerun a conversational agent turn when a steer or user message arrives while the LLM call is in flight, so the late message is not lost after a final reply.
8
+
9
+ ## 0.11.1
10
+
11
+ ### Patch Changes
12
+
13
+ - 2ff6701: Wake idle agents on a non-interrupt steer. Queued steers still wait for open tool calls to finish, so in-flight work is not abandoned.
14
+
3
15
  ## 0.11.0
4
16
 
5
17
  ### Minor Changes
@@ -11,11 +23,11 @@
11
23
  ```ts
12
24
  const attributes = defineRunAttributes({
13
25
  tenantId: { required: true, index: true },
14
- })
26
+ });
15
27
 
16
- createLooms({ modules, attributes })
17
- postgresReplica({ url, additionalFields: attributes })
18
- loomsReplicaTables({ additionalFields: attributes })
28
+ createLooms({ modules, attributes });
29
+ postgresReplica({ url, additionalFields: attributes });
30
+ loomsReplicaTables({ additionalFields: attributes });
19
31
  ```
20
32
 
21
33
  - Each attribute is a text column named in snake_case unless `fieldName` is set. `index: true` adds an index. Each column and index is its own migration, and a new column is backfilled from the `attributes` JSON. `postgresEventLog` records these migrations in `looms_schema_migrations` and applies each one once.
@@ -21,6 +21,8 @@ export function summarizeAgentEvent(event) {
21
21
  return { title: 'user message', detail: messageContent(event.payload) };
22
22
  case 'agent.turn.started':
23
23
  return { title: 'turn started', detail: `turn ${jsonNumberText(payload.turn) ?? ''}` };
24
+ case 'agent.turn.settled':
25
+ return { title: 'turn settled', detail: `turn ${jsonNumberText(payload.turn) ?? ''}` };
24
26
  case 'agent.turn.text_delta':
25
27
  return { title: 'text delta', detail: jsonText(payload.delta) };
26
28
  case 'agent.message':
@@ -243,10 +243,16 @@ export declare const callLlmEffect: NoInfer<import("@swirls/looms/core").LocalEf
243
243
  readonly arguments: Schema.Json;
244
244
  };
245
245
  };
246
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
247
+ readonly type: "agent.turn.settled";
248
+ readonly payload: {
249
+ readonly turn: number;
250
+ };
246
251
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
247
252
  readonly type: "agent.turn.started";
248
253
  readonly payload: {
249
254
  readonly turn: number;
255
+ readonly messageCount?: number | undefined;
250
256
  };
251
257
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
252
258
  readonly type: "agent.turn.stopped";
@@ -743,10 +749,16 @@ export declare const executeToolEffect: NoInfer<import("@swirls/looms/core").Loc
743
749
  readonly arguments: Schema.Json;
744
750
  };
745
751
  };
752
+ }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
753
+ readonly type: "agent.turn.settled";
754
+ readonly payload: {
755
+ readonly turn: number;
756
+ };
746
757
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
747
758
  readonly type: "agent.turn.started";
748
759
  readonly payload: {
749
760
  readonly turn: number;
761
+ readonly messageCount?: number | undefined;
750
762
  };
751
763
  }) | (Omit<import("@swirls/looms/core").EventInput, "payload" | "type"> & {
752
764
  readonly type: "agent.turn.stopped";
@@ -133,7 +133,7 @@ function runCallLlm(args) {
133
133
  const events = [
134
134
  {
135
135
  type: 'agent.turn.started',
136
- payload: { turn },
136
+ payload: { turn, messageCount: messages.length },
137
137
  threadId,
138
138
  },
139
139
  ];
@@ -227,28 +227,17 @@ function runCallLlm(args) {
227
227
  threadId,
228
228
  });
229
229
  }
230
- if (shouldStop) {
231
- if (!definition.conversational) {
232
- const output = structuredOutput ?? result.output ?? { text: result.message.content };
230
+ if (!shouldStop) {
231
+ for (const toolCall of toolCalls) {
233
232
  events.push({
234
- type: 'runtime.thread.completed',
235
- payload: {
236
- threadId,
237
- output: consumedSteerMessageIds.length === 0 ? output : { output, consumedSteerMessageIds },
238
- },
233
+ type: 'agent.tool_call.requested',
234
+ payload: { turn, toolCall },
239
235
  threadId,
240
236
  });
241
237
  }
242
- return events;
243
- }
244
- for (const toolCall of toolCalls) {
245
- events.push({
246
- type: 'agent.tool_call.requested',
247
- payload: { turn, toolCall },
248
- threadId,
249
- });
250
238
  }
251
- if (toolCalls.length === 0 && !definition.conversational) {
239
+ const completeThread = !definition.conversational && (shouldStop || toolCalls.length === 0);
240
+ if (completeThread) {
252
241
  const output = structuredOutput ?? result.output ?? { text: result.message.content };
253
242
  events.push({
254
243
  type: 'runtime.thread.completed',
@@ -259,6 +248,11 @@ function runCallLlm(args) {
259
248
  threadId,
260
249
  });
261
250
  }
251
+ events.push({
252
+ type: 'agent.turn.settled',
253
+ payload: { turn },
254
+ threadId,
255
+ });
262
256
  return events;
263
257
  });
264
258
  }
@@ -14,6 +14,11 @@ export declare const AgentMessageReceivedPayloadSchema: Schema.Struct<{
14
14
  }>;
15
15
  export declare const AgentTurnStartedPayloadSchema: Schema.Struct<{
16
16
  readonly turn: Schema.Finite;
17
+ /** Snapshot of `lines.length` when the LLM call began. Used to detect mid-call steers. */
18
+ readonly messageCount: Schema.optional<Schema.Finite>;
19
+ }>;
20
+ export declare const AgentTurnSettledPayloadSchema: Schema.Struct<{
21
+ readonly turn: Schema.Finite;
17
22
  }>;
18
23
  export declare const AgentTurnTextDeltaPayloadSchema: Schema.Struct<{
19
24
  readonly turn: Schema.Finite;
@@ -223,6 +228,11 @@ export declare const agentCatalog: import("@swirls/looms/core").EventCatalog<"ag
223
228
  }>;
224
229
  'turn.started': Schema.Struct<{
225
230
  readonly turn: Schema.Finite;
231
+ /** Snapshot of `lines.length` when the LLM call began. Used to detect mid-call steers. */
232
+ readonly messageCount: Schema.optional<Schema.Finite>;
233
+ }>;
234
+ 'turn.settled': Schema.Struct<{
235
+ readonly turn: Schema.Finite;
226
236
  }>;
227
237
  'turn.text_delta': Schema.Struct<{
228
238
  readonly turn: Schema.Finite;
@@ -6,6 +6,11 @@ export const AgentMessageReceivedPayloadSchema = Schema.Struct({
6
6
  });
7
7
  export const AgentTurnStartedPayloadSchema = Schema.Struct({
8
8
  turn: Schema.Finite,
9
+ /** Snapshot of `lines.length` when the LLM call began. Used to detect mid-call steers. */
10
+ messageCount: Schema.optional(Schema.Finite),
11
+ });
12
+ export const AgentTurnSettledPayloadSchema = Schema.Struct({
13
+ turn: Schema.Finite,
9
14
  });
10
15
  export const AgentTurnTextDeltaPayloadSchema = Schema.Struct({
11
16
  turn: Schema.Finite,
@@ -104,6 +109,7 @@ export const AgentEffectsRequestedPayloadSchema = Schema.Struct({
104
109
  export const agentCatalog = defineEventCatalog('agent', {
105
110
  'message.received': AgentMessageReceivedPayloadSchema,
106
111
  'turn.started': AgentTurnStartedPayloadSchema,
112
+ 'turn.settled': AgentTurnSettledPayloadSchema,
107
113
  'turn.text_delta': AgentTurnTextDeltaPayloadSchema,
108
114
  message: AgentMessageEventPayloadSchema,
109
115
  'tool_call.requested': AgentToolCallRequestedPayloadSchema,
@@ -23,6 +23,10 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
23
23
  }>;
24
24
  'turn.started': import("effect/Schema").Struct<{
25
25
  readonly turn: import("effect/Schema").Finite;
26
+ readonly messageCount: import("effect/Schema").optional<import("effect/Schema").Finite>;
27
+ }>;
28
+ 'turn.settled': import("effect/Schema").Struct<{
29
+ readonly turn: import("effect/Schema").Finite;
26
30
  }>;
27
31
  'turn.text_delta': import("effect/Schema").Struct<{
28
32
  readonly turn: import("effect/Schema").Finite;
@@ -455,10 +459,16 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
455
459
  readonly arguments: import("effect/Schema").Json;
456
460
  };
457
461
  };
462
+ }) | (Omit<EventInput, "payload" | "type"> & {
463
+ readonly type: "agent.turn.settled";
464
+ readonly payload: {
465
+ readonly turn: number;
466
+ };
458
467
  }) | (Omit<EventInput, "payload" | "type"> & {
459
468
  readonly type: "agent.turn.started";
460
469
  readonly payload: {
461
470
  readonly turn: number;
471
+ readonly messageCount?: number | undefined;
462
472
  };
463
473
  }) | (Omit<EventInput, "payload" | "type"> & {
464
474
  readonly type: "agent.turn.stopped";
@@ -955,10 +965,16 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
955
965
  readonly arguments: import("effect/Schema").Json;
956
966
  };
957
967
  };
968
+ }) | (Omit<EventInput, "payload" | "type"> & {
969
+ readonly type: "agent.turn.settled";
970
+ readonly payload: {
971
+ readonly turn: number;
972
+ };
958
973
  }) | (Omit<EventInput, "payload" | "type"> & {
959
974
  readonly type: "agent.turn.started";
960
975
  readonly payload: {
961
976
  readonly turn: number;
977
+ readonly messageCount?: number | undefined;
962
978
  };
963
979
  }) | (Omit<EventInput, "payload" | "type"> & {
964
980
  readonly type: "agent.turn.stopped";
@@ -1592,10 +1608,16 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
1592
1608
  readonly arguments: import("effect/Schema").Json;
1593
1609
  };
1594
1610
  };
1611
+ }) | (Omit<EventInput, "payload" | "type"> & {
1612
+ readonly type: "agent.turn.settled";
1613
+ readonly payload: {
1614
+ readonly turn: number;
1615
+ };
1595
1616
  }) | (Omit<EventInput, "payload" | "type"> & {
1596
1617
  readonly type: "agent.turn.started";
1597
1618
  readonly payload: {
1598
1619
  readonly turn: number;
1620
+ readonly messageCount?: number | undefined;
1599
1621
  };
1600
1622
  }) | (Omit<EventInput, "payload" | "type"> & {
1601
1623
  readonly type: "agent.turn.stopped";
@@ -2092,10 +2114,16 @@ export declare function agent(options?: AgentModuleOptions): import("@swirls/loo
2092
2114
  readonly arguments: import("effect/Schema").Json;
2093
2115
  };
2094
2116
  };
2117
+ }) | (Omit<EventInput, "payload" | "type"> & {
2118
+ readonly type: "agent.turn.settled";
2119
+ readonly payload: {
2120
+ readonly turn: number;
2121
+ };
2095
2122
  }) | (Omit<EventInput, "payload" | "type"> & {
2096
2123
  readonly type: "agent.turn.started";
2097
2124
  readonly payload: {
2098
2125
  readonly turn: number;
2126
+ readonly messageCount?: number | undefined;
2099
2127
  };
2100
2128
  }) | (Omit<EventInput, "payload" | "type"> & {
2101
2129
  readonly type: "agent.turn.stopped";
@@ -15,6 +15,10 @@ export declare const agentModule: import("@swirls/looms/core").ModuleScope<"agen
15
15
  }>;
16
16
  'turn.started': import("effect/Schema").Struct<{
17
17
  readonly turn: import("effect/Schema").Finite;
18
+ readonly messageCount: import("effect/Schema").optional<import("effect/Schema").Finite>;
19
+ }>;
20
+ 'turn.settled': import("effect/Schema").Struct<{
21
+ readonly turn: import("effect/Schema").Finite;
18
22
  }>;
19
23
  'turn.text_delta': import("effect/Schema").Struct<{
20
24
  readonly turn: import("effect/Schema").Finite;
@@ -222,6 +226,10 @@ export declare const agentModule: import("@swirls/looms/core").ModuleScope<"agen
222
226
  }>;
223
227
  'turn.started': import("effect/Schema").Struct<{
224
228
  readonly turn: import("effect/Schema").Finite;
229
+ readonly messageCount: import("effect/Schema").optional<import("effect/Schema").Finite>;
230
+ }>;
231
+ 'turn.settled': import("effect/Schema").Struct<{
232
+ readonly turn: import("effect/Schema").Finite;
225
233
  }>;
226
234
  'turn.text_delta': import("effect/Schema").Struct<{
227
235
  readonly turn: import("effect/Schema").Finite;
@@ -24,8 +24,12 @@ export declare const userMessage: import("@swirls/looms/core").SignalDefinition<
24
24
  };
25
25
  }>;
26
26
  /**
27
- * Build the `agent.steered` event: inject a user message mid-run, optionally
28
- * interrupting the current turn so the agent replans immediately.
27
+ * Build the `agent.steered` event: inject a user message mid-run.
28
+ *
29
+ * With `interrupt: true` (the default), the current turn is abandoned and a new
30
+ * LLM call starts immediately. With `interrupt: false`, the message is queued
31
+ * for the next turn — and if the agent is already idle, that next turn starts
32
+ * now so the steer cannot sit forever.
29
33
  */
30
34
  export declare const steer: import("@swirls/looms/core").SignalDefinition<{
31
35
  readonly content: string;
@@ -21,11 +21,15 @@ export const userMessage = agentModule.signal({
21
21
  build: ({ content }) => agentModule.input('message.received', { message: { role: 'user', content } }),
22
22
  });
23
23
  /**
24
- * Build the `agent.steered` event: inject a user message mid-run, optionally
25
- * interrupting the current turn so the agent replans immediately.
24
+ * Build the `agent.steered` event: inject a user message mid-run.
25
+ *
26
+ * With `interrupt: true` (the default), the current turn is abandoned and a new
27
+ * LLM call starts immediately. With `interrupt: false`, the message is queued
28
+ * for the next turn — and if the agent is already idle, that next turn starts
29
+ * now so the steer cannot sit forever.
26
30
  */
27
31
  export const steer = agentModule.signal({
28
- description: 'Inject a user message mid-run, optionally interrupting the current turn.',
32
+ description: 'Inject a user message mid-run. Interrupt abandons the current turn; otherwise the message waits for the next turn (which starts immediately if the agent is idle).',
29
33
  input: SteerInputSchema,
30
34
  build: ({ content, interrupt }) => agentModule.input('steered', {
31
35
  turn: 0,
@@ -35,6 +35,10 @@ function attachToolCall(lines, toolCall) {
35
35
  }
36
36
  return lines.with(index, { ...line, toolCalls: [...(line.toolCalls ?? []), toolCall] });
37
37
  }
38
+ /** Schedule another LLM turn only when no tool calls are still open. */
39
+ function wakeIfIdle(state) {
40
+ return state.pendingToolCalls.length === 0;
41
+ }
38
42
  /** Drop the turn in progress: answer its open tool calls and cancel the children they started. */
39
43
  function abandonTurn(state) {
40
44
  return {
@@ -48,6 +52,8 @@ function abandonTurn(state) {
48
52
  pendingEmits: [],
49
53
  pendingEffectTools: {},
50
54
  needsLlmCall: false,
55
+ deferWake: false,
56
+ midCallMessages: [],
51
57
  abandonedChildThreadIds: [
52
58
  ...(state.abandonedChildThreadIds ?? []),
53
59
  ...state.pendingSpawns.map((item) => item.childThreadId),
@@ -76,6 +82,8 @@ export const agentThread = agentModule.thread({
76
82
  output: null,
77
83
  pendingEffectTools: {},
78
84
  consumedSteerMessageIds: [],
85
+ deferWake: false,
86
+ midCallMessages: [],
79
87
  }),
80
88
  step(state, event, ctx) {
81
89
  switch (event.type) {
@@ -99,22 +107,38 @@ export const agentThread = agentModule.thread({
99
107
  case 'agent.lineage':
100
108
  return { ...state, depth: event.payload.depth };
101
109
  case 'agent.turn.started': {
110
+ // Mid-call steers are already in `lines` when this fires (first event of the batch).
111
+ // Defer the wake until `agent.turn.settled` so we do not schedule the next LLM before
112
+ // `agent.message` / tool-call events from this same turn are applied. Snapshot the
113
+ // extras so compaction can re-attach them instead of losing the steer.
114
+ const messageCount = event.payload.messageCount ?? state.lines.length;
115
+ const midCallMessages = state.lines.slice(messageCount);
116
+ const arrivedMidCall = midCallMessages.length > 0;
102
117
  return {
103
118
  ...state,
104
119
  turn: event.payload.turn,
105
120
  needsLlmCall: false,
121
+ deferWake: arrivedMidCall,
122
+ midCallMessages: arrivedMidCall ? midCallMessages : [],
106
123
  pendingSteer: null,
107
124
  abandonedChildThreadIds: [],
108
125
  };
109
126
  }
127
+ case 'agent.turn.settled': {
128
+ const deferWake = state.deferWake === true;
129
+ return {
130
+ ...state,
131
+ deferWake: false,
132
+ midCallMessages: [],
133
+ needsLlmCall: deferWake && wakeIfIdle(state),
134
+ };
135
+ }
110
136
  case 'agent.message.received': {
111
137
  const message = event.payload.message;
112
- const lines = [...state.lines, message];
113
- const needsLlmCall = state.pendingToolCalls.length === 0;
114
138
  return {
115
139
  ...state,
116
- lines,
117
- needsLlmCall,
140
+ lines: [...state.lines, message],
141
+ needsLlmCall: wakeIfIdle(state),
118
142
  };
119
143
  }
120
144
  case 'agent.message': {
@@ -152,7 +176,6 @@ export const agentThread = agentModule.thread({
152
176
  name: name ?? undefined,
153
177
  },
154
178
  ];
155
- const needsLlmCall = pendingToolCalls.length === 0;
156
179
  return {
157
180
  ...state,
158
181
  lines,
@@ -162,7 +185,7 @@ export const agentThread = agentModule.thread({
162
185
  pendingSpawns,
163
186
  pendingEffects,
164
187
  pendingEmits,
165
- needsLlmCall,
188
+ needsLlmCall: wakeIfIdle({ pendingToolCalls }),
166
189
  };
167
190
  }
168
191
  case 'agent.steered': {
@@ -182,21 +205,28 @@ export const agentThread = agentModule.thread({
182
205
  consumedSteerMessageIds,
183
206
  };
184
207
  }
208
+ // Queue for the next turn. If the agent is idle (no open tool calls), that turn
209
+ // starts now — otherwise the message rides along when tools finish.
185
210
  return {
186
211
  ...state,
187
212
  lines: [...state.lines, message],
188
213
  pendingSteer: message,
214
+ needsLlmCall: wakeIfIdle(state),
189
215
  consumedSteerMessageIds,
190
216
  };
191
217
  }
192
218
  case 'agent.context.compacted': {
193
219
  const compactedMessages = event.payload.messages;
220
+ const midCallMessages = state.midCallMessages ?? [];
194
221
  const alreadyCompactedTurns = state.alreadyCompactedTurns
195
222
  ? [...state.alreadyCompactedTurns, event.payload.turn]
196
223
  : [event.payload.turn];
224
+ const baseLines = compactedMessages ? [...compactedMessages] : state.lines;
197
225
  return {
198
226
  ...state,
199
- lines: compactedMessages ? [...compactedMessages] : state.lines,
227
+ // Compaction replaces `lines`; put mid-call steers back so the deferred wake still
228
+ // has something new to answer.
229
+ lines: midCallMessages.length > 0 ? [...baseLines, ...midCallMessages] : baseLines,
200
230
  alreadyCompactedTurns,
201
231
  };
202
232
  }
@@ -101,6 +101,16 @@ export interface AgentState {
101
101
  * Optional so snapshots taken before turns could be abandoned still fold.
102
102
  */
103
103
  abandonedChildThreadIds?: string[];
104
+ /**
105
+ * Set when a steer arrived during the LLM call. Honored by `agent.turn.settled` after the
106
+ * rest of the turn's events so we do not schedule the next call before `agent.message`.
107
+ */
108
+ deferWake?: boolean;
109
+ /**
110
+ * User/system lines that arrived after the LLM call began. Re-attached after compaction so a
111
+ * mid-call steer is not lost when `lines` are replaced.
112
+ */
113
+ midCallMessages?: Message[];
104
114
  }
105
115
  export type AgentMessageDelivery = 'steer' | 'followUp' | 'nextTurn';
106
116
  export type AgentSessionTurnStatus = 'completed' | 'failed' | 'cancelled';