@swirls/looms 0.12.2 → 0.12.4
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 +16 -0
- package/dist/agent/definitions.d.ts +5 -0
- package/dist/agent/effects.d.ts +1 -0
- package/dist/agent/effects.js +9 -2
- package/dist/agent/module.d.ts +652 -650
- package/dist/agent/stop-when.d.ts +5 -0
- package/dist/agent/stop-when.js +1 -0
- package/dist/agent/threads.js +9 -0
- package/dist/agent/types.d.ts +6 -0
- package/dist/core/module.d.ts +5 -2
- package/dist/core/module.js +3 -0
- package/dist/runtime/local-effect-executor.js +15 -7
- package/dist/runtime/runtime.js +3 -1
- package/dist/runtime/wake-engine.js +4 -0
- package/package.json +1 -1
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { AgentTurnResult } from './definitions';
|
|
2
2
|
import type { Message, ToolCall } from './types';
|
|
3
3
|
export interface StopWhenContext {
|
|
4
|
+
/**
|
|
5
|
+
* The model turn that just ran, counted the way `maxTurns` counts: from the start of the
|
|
6
|
+
* thread, or from the latest user message for a conversational agent.
|
|
7
|
+
*/
|
|
4
8
|
readonly turn: number;
|
|
5
9
|
readonly maxTurns: number;
|
|
6
10
|
readonly messages: Message[];
|
|
@@ -9,4 +13,5 @@ export interface StopWhenContext {
|
|
|
9
13
|
}
|
|
10
14
|
export type StopWhen = (ctx: StopWhenContext) => boolean;
|
|
11
15
|
export declare function hasToolCall(name: string): StopWhen;
|
|
16
|
+
/** Stop once `n` model turns have run. For a conversational agent that is `n` turns per user message. */
|
|
12
17
|
export declare function stepCountIs(n: number): StopWhen;
|
package/dist/agent/stop-when.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export function hasToolCall(name) {
|
|
2
2
|
return (ctx) => ctx.toolCalls.some((toolCall) => toolCall.name === name);
|
|
3
3
|
}
|
|
4
|
+
/** Stop once `n` model turns have run. For a conversational agent that is `n` turns per user message. */
|
|
4
5
|
export function stepCountIs(n) {
|
|
5
6
|
return (ctx) => ctx.turn >= n;
|
|
6
7
|
}
|
package/dist/agent/threads.js
CHANGED
|
@@ -120,6 +120,11 @@ export const agentThread = agentModule.thread({
|
|
|
120
120
|
needsLlmCall: false,
|
|
121
121
|
deferWake: arrivedMidCall,
|
|
122
122
|
midCallMessages: arrivedMidCall ? midCallMessages : [],
|
|
123
|
+
// This call never saw a user message that arrived while it ran, so that message's
|
|
124
|
+
// turn count starts after it.
|
|
125
|
+
turnsBeforeMessage: midCallMessages.some((line) => line.role === 'user')
|
|
126
|
+
? event.payload.turn
|
|
127
|
+
: state.turnsBeforeMessage,
|
|
123
128
|
pendingSteer: null,
|
|
124
129
|
abandonedChildThreadIds: [],
|
|
125
130
|
};
|
|
@@ -139,6 +144,7 @@ export const agentThread = agentModule.thread({
|
|
|
139
144
|
...state,
|
|
140
145
|
lines: [...state.lines, message],
|
|
141
146
|
needsLlmCall: wakeIfIdle(state),
|
|
147
|
+
turnsBeforeMessage: state.turn,
|
|
142
148
|
};
|
|
143
149
|
}
|
|
144
150
|
case 'agent.message': {
|
|
@@ -200,6 +206,7 @@ export const agentThread = agentModule.thread({
|
|
|
200
206
|
pendingSteer: message,
|
|
201
207
|
needsLlmCall: true,
|
|
202
208
|
consumedSteerMessageIds,
|
|
209
|
+
turnsBeforeMessage: state.turn,
|
|
203
210
|
};
|
|
204
211
|
}
|
|
205
212
|
// Queue for the next turn. If the agent is idle (no open tool calls), that turn
|
|
@@ -211,6 +218,7 @@ export const agentThread = agentModule.thread({
|
|
|
211
218
|
pendingSteer: message,
|
|
212
219
|
needsLlmCall: wakeIfIdle(state),
|
|
213
220
|
consumedSteerMessageIds,
|
|
221
|
+
turnsBeforeMessage: state.turn,
|
|
214
222
|
};
|
|
215
223
|
}
|
|
216
224
|
case 'agent.context.compacted': {
|
|
@@ -386,6 +394,7 @@ export const agentThread = agentModule.thread({
|
|
|
386
394
|
if (state.needsLlmCall) {
|
|
387
395
|
effects.push(invoke(callLlmEffect, {
|
|
388
396
|
turn: state.turn + 1,
|
|
397
|
+
messageTurn: state.turn + 1 - (state.turnsBeforeMessage ?? 0),
|
|
389
398
|
definitionName: state.definitionName,
|
|
390
399
|
definitionVersion: state.definitionVersion ?? DEFAULT_DEFINITION_VERSION,
|
|
391
400
|
messages: state.lines,
|
package/dist/agent/types.d.ts
CHANGED
|
@@ -111,6 +111,12 @@ export interface AgentState {
|
|
|
111
111
|
* mid-call steer is not lost when `lines` are replaced.
|
|
112
112
|
*/
|
|
113
113
|
midCallMessages?: Message[];
|
|
114
|
+
/**
|
|
115
|
+
* Value of `turn` when the latest user message arrived. A conversational agent counts
|
|
116
|
+
* `maxTurns` from here, so the limit bounds one message and not the whole conversation.
|
|
117
|
+
* Optional so snapshots taken before the count was per message still fold.
|
|
118
|
+
*/
|
|
119
|
+
turnsBeforeMessage?: number;
|
|
114
120
|
}
|
|
115
121
|
export type AgentMessageDelivery = 'steer' | 'followUp' | 'nextTurn';
|
|
116
122
|
export type AgentSessionTurnStatus = 'completed' | 'failed' | 'cancelled';
|
package/dist/core/module.d.ts
CHANGED
|
@@ -22,8 +22,11 @@ export type OnEffectNext = () => Effect.Effect<ReadonlyArray<EventInput>, Error>
|
|
|
22
22
|
* Middleware wraps local (in-process) effect dispatch only. Worker-placed effects never see it —
|
|
23
23
|
* pass a {@link LocalEffectDefinition} so that is a type error rather than a silent no-op.
|
|
24
24
|
* Prefer {@link EffectAdmission} / {@link onAdmit} for gates that must apply on both paths.
|
|
25
|
+
*
|
|
26
|
+
* `R` and `E` are inferred from the definition and otherwise unused, so an effect that requires
|
|
27
|
+
* host services (`agent.callLLM` needs the LLM and the agent definitions) can be wrapped directly.
|
|
25
28
|
*/
|
|
26
|
-
export declare function onEffect<TInput>(definition: LocalEffectDefinition<TInput>, handler: (input: TInput, ctx: LocalEffectContext, next: OnEffectNext) => Effect.Effect<ReadonlyArray<EventInput>, Error>): EffectMiddleware;
|
|
29
|
+
export declare function onEffect<TInput, R = never, E extends EventInput = EventInput>(definition: LocalEffectDefinition<TInput, R, E>, handler: (input: TInput, ctx: LocalEffectContext, next: OnEffectNext) => Effect.Effect<ReadonlyArray<EventInput>, Error>): EffectMiddleware;
|
|
27
30
|
/**
|
|
28
31
|
* Identity of an effect attempt before it runs locally or is dispatched to a worker.
|
|
29
32
|
* Admission hooks see this on both paths; they cannot call `emit` or `heartbeat`.
|
|
@@ -46,7 +49,7 @@ export type EffectAdmission = (effect: RuntimeEffect, ctx: EffectAdmissionContex
|
|
|
46
49
|
* Admission that runs only for one effect definition (local or worker). Prefer this over
|
|
47
50
|
* middleware when the gate must apply to worker-placed effects as well as local ones.
|
|
48
51
|
*/
|
|
49
|
-
export declare function onAdmit<TInput>(definition: EffectDefinition<TInput>, handler: (input: TInput, ctx: EffectAdmissionContext) => Effect.Effect<void, Error>): EffectAdmission;
|
|
52
|
+
export declare function onAdmit<TInput, R = never, E extends EventInput = EventInput>(definition: EffectDefinition<TInput, R, E>, handler: (input: TInput, ctx: EffectAdmissionContext) => Effect.Effect<void, Error>): EffectAdmission;
|
|
50
53
|
/**
|
|
51
54
|
* Names the threads whose in-process effects an admitted signal event interrupts, so a running
|
|
52
55
|
* effect (such as a model call) can stop early. The runtime also interrupts each named thread's
|
package/dist/core/module.js
CHANGED
|
@@ -9,6 +9,9 @@ export { DEFAULT_DEFINITION_VERSION, definitionKey } from './definition-version.
|
|
|
9
9
|
* Middleware wraps local (in-process) effect dispatch only. Worker-placed effects never see it —
|
|
10
10
|
* pass a {@link LocalEffectDefinition} so that is a type error rather than a silent no-op.
|
|
11
11
|
* Prefer {@link EffectAdmission} / {@link onAdmit} for gates that must apply on both paths.
|
|
12
|
+
*
|
|
13
|
+
* `R` and `E` are inferred from the definition and otherwise unused, so an effect that requires
|
|
14
|
+
* host services (`agent.callLLM` needs the LLM and the agent definitions) can be wrapped directly.
|
|
12
15
|
*/
|
|
13
16
|
export function onEffect(definition, handler) {
|
|
14
17
|
return (effect, ctx, next) => {
|
|
@@ -86,13 +86,6 @@ export function executeLocalEffects(options, input) {
|
|
|
86
86
|
produced.push(createEffectFailedEvent(input.runId, item, withdrawnError(failedEffectId)));
|
|
87
87
|
continue;
|
|
88
88
|
}
|
|
89
|
-
const appendLive = (eventInput) => runInContext(Effect.gen(function* () {
|
|
90
|
-
const validated = yield* validateEventInput(registry.catalogs, eventInput);
|
|
91
|
-
const liveEvent = createLiveEvent(input.runId, item, validated);
|
|
92
|
-
if (liveEvent) {
|
|
93
|
-
yield* liveAppends.push(input.runId, liveEvent);
|
|
94
|
-
}
|
|
95
|
-
}));
|
|
96
89
|
const retry = registry.effects.get(item.effect.type)?.retry;
|
|
97
90
|
const launched = [];
|
|
98
91
|
const attemptNumber = input.attempts.get(item.effectId) ?? 1;
|
|
@@ -116,6 +109,20 @@ export function executeLocalEffects(options, input) {
|
|
|
116
109
|
continue;
|
|
117
110
|
}
|
|
118
111
|
const controller = makeAbortController();
|
|
112
|
+
let attemptOpen = true;
|
|
113
|
+
// A handler can outlive its attempt: an aborted model call may still have a delta in
|
|
114
|
+
// hand. Once the attempt is over its live events are dropped. Appending one after the
|
|
115
|
+
// wake has released the run fails, and that failure poisons the run's live appender
|
|
116
|
+
// for the next wake, which then fails the first live event of an unrelated attempt.
|
|
117
|
+
const appendLive = (eventInput) => !attemptOpen || controller.signal.aborted
|
|
118
|
+
? Promise.resolve()
|
|
119
|
+
: runInContext(Effect.gen(function* () {
|
|
120
|
+
const validated = yield* validateEventInput(registry.catalogs, eventInput);
|
|
121
|
+
const liveEvent = createLiveEvent(input.runId, item, validated);
|
|
122
|
+
if (liveEvent) {
|
|
123
|
+
yield* liveAppends.push(input.runId, liveEvent);
|
|
124
|
+
}
|
|
125
|
+
}));
|
|
119
126
|
executionContext.activeEffects.set(item.effectId, {
|
|
120
127
|
controller,
|
|
121
128
|
threadId: item.threadId,
|
|
@@ -136,6 +143,7 @@ export function executeLocalEffects(options, input) {
|
|
|
136
143
|
const settled = yield* (retry?.startToCloseTimeoutMs
|
|
137
144
|
? dispatched.pipe(Effect.timeoutOption(retry.startToCloseTimeoutMs), Effect.map(Option.getOrElse(() => ({ _tag: 'timed-out' }))))
|
|
138
145
|
: dispatched).pipe(Effect.ensuring(Effect.sync(() => {
|
|
146
|
+
attemptOpen = false;
|
|
139
147
|
executionContext.activeEffects.delete(item.effectId);
|
|
140
148
|
})));
|
|
141
149
|
// A launch the target accepted happened even if the attempt was later aborted or failed.
|
package/dist/runtime/runtime.js
CHANGED
|
@@ -26,7 +26,9 @@ function interruptedThreadIds(events, rules, state) {
|
|
|
26
26
|
const cancelled = event.type === 'runtime.thread.cancel.requested' && Predicate.isString(payload.threadId)
|
|
27
27
|
? [payload.threadId]
|
|
28
28
|
: [];
|
|
29
|
-
|
|
29
|
+
// A signal without a thread id is delivered to the root thread, so the rules see it there.
|
|
30
|
+
const delivered = { ...event, threadId: event.threadId ?? state.rootThreadId };
|
|
31
|
+
return [...cancelled, ...rules.flatMap((rule) => rule(delivered))];
|
|
30
32
|
});
|
|
31
33
|
return new Set(named.flatMap((threadId) => [threadId, ...descendantThreadIds(state, threadId)]));
|
|
32
34
|
}
|
|
@@ -386,6 +386,10 @@ export function createWakeEngine(options) {
|
|
|
386
386
|
yield* Fiber.interruptAll([...inFlight.values()].map((entry) => entry.fiber));
|
|
387
387
|
inFlight.clear();
|
|
388
388
|
}
|
|
389
|
+
// Live events are flushed in the background and only drained when an effect produces
|
|
390
|
+
// events. An attempt that was aborted produces none, so flush here: releasing the wake
|
|
391
|
+
// clears the buffer, and a delta emitted just before a stop would be lost.
|
|
392
|
+
yield* liveAppends.drain(runId);
|
|
389
393
|
const root = cursor.state.rootThreadId
|
|
390
394
|
? cursor.state.threads[cursor.state.rootThreadId]
|
|
391
395
|
: undefined;
|