@lightblues/jig 0.0.3 → 0.0.5
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/dist/agent-paseo/runner.d.ts +12 -1
- package/dist/agent-paseo/runner.js +114 -43
- package/package.json +1 -1
|
@@ -94,6 +94,7 @@ export declare class PaseoThreadRunner<TResult = unknown> {
|
|
|
94
94
|
private o;
|
|
95
95
|
private quietMs;
|
|
96
96
|
private tailLimit;
|
|
97
|
+
private waitMs;
|
|
97
98
|
private log;
|
|
98
99
|
constructor(o: PaseoThreadRunnerOptions<TResult>);
|
|
99
100
|
running(threadId: string): boolean;
|
|
@@ -116,7 +117,10 @@ export declare class PaseoThreadRunner<TResult = unknown> {
|
|
|
116
117
|
/**
|
|
117
118
|
* After a restart: a message the host sent and has no answer for is looked up in its agent's timeline. A turn
|
|
118
119
|
* still running is followed (`following`); one that ended is stored (`finished` / `failed`); a message the
|
|
119
|
-
* agent never got fails, unless it was sent within `graceMs
|
|
120
|
+
* agent never got fails, unless it was sent within `graceMs`: then the run stays registered (`pending`) and the
|
|
121
|
+
* runner keeps looking, so it resolves by itself to `following` / `finished` / `failed` (`RUN_ERROR` and
|
|
122
|
+
* `saveError`) without the host doing anything. Calling it again for the same run while pending answers
|
|
123
|
+
* `pending`; another run on the thread answers `busy`.
|
|
120
124
|
*/
|
|
121
125
|
recover(req: RecoverRequest): Promise<RecoverResult>;
|
|
122
126
|
close(): void;
|
|
@@ -136,6 +140,13 @@ export declare class PaseoThreadRunner<TResult = unknown> {
|
|
|
136
140
|
* the held events the tail did not have. An ended turn is finished instead.
|
|
137
141
|
*/
|
|
138
142
|
private regap;
|
|
143
|
+
/** A pending recovery: look again every `waitMs` until the message shows up or the deadline passes. */
|
|
144
|
+
private waitFor;
|
|
145
|
+
private checkWaiting;
|
|
146
|
+
/** Reads the agent and its tail and rebuilds the run; a full page without our message is read again, deeper. With `keep`, a tail without our message is an error rather than "not found". */
|
|
147
|
+
private retail;
|
|
148
|
+
/** Finishes a turn the re-tail found ended, with the end event held meanwhile when there is one. */
|
|
149
|
+
private finishHeld;
|
|
139
150
|
/** Replaces the run's folder with the turn in `entries` (after our message); null when our message is not there. */
|
|
140
151
|
private rebuild;
|
|
141
152
|
/** Folds the held events; with `resync`, first sends every follower `READY` and the replay of the rebuilt turn. */
|
|
@@ -14,6 +14,7 @@ export class PaseoThreadRunner {
|
|
|
14
14
|
o;
|
|
15
15
|
quietMs;
|
|
16
16
|
tailLimit;
|
|
17
|
+
waitMs;
|
|
17
18
|
log;
|
|
18
19
|
constructor(o) {
|
|
19
20
|
if (typeof o.filter !== "function")
|
|
@@ -21,6 +22,7 @@ export class PaseoThreadRunner {
|
|
|
21
22
|
this.o = o;
|
|
22
23
|
this.quietMs = o.quietMs ?? 60_000;
|
|
23
24
|
this.tailLimit = o.tailLimit ?? 300;
|
|
25
|
+
this.waitMs = Math.max(100, Math.min(2000, this.quietMs / 4));
|
|
24
26
|
this.log = o.log ?? ((m) => console.error(m));
|
|
25
27
|
}
|
|
26
28
|
running(threadId) {
|
|
@@ -64,43 +66,54 @@ export class PaseoThreadRunner {
|
|
|
64
66
|
const r = this.runs.get(threadId);
|
|
65
67
|
if (!r?.agentId)
|
|
66
68
|
return { stopping: false };
|
|
67
|
-
|
|
69
|
+
r.stopping = true;
|
|
70
|
+
try {
|
|
71
|
+
await this.o.daemon.cancel(r.agentId);
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
r.stopping = false;
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
68
77
|
return { stopping: true };
|
|
69
78
|
}
|
|
70
79
|
/**
|
|
71
80
|
* After a restart: a message the host sent and has no answer for is looked up in its agent's timeline. A turn
|
|
72
81
|
* still running is followed (`following`); one that ended is stored (`finished` / `failed`); a message the
|
|
73
|
-
* agent never got fails, unless it was sent within `graceMs
|
|
82
|
+
* agent never got fails, unless it was sent within `graceMs`: then the run stays registered (`pending`) and the
|
|
83
|
+
* runner keeps looking, so it resolves by itself to `following` / `finished` / `failed` (`RUN_ERROR` and
|
|
84
|
+
* `saveError`) without the host doing anything. Calling it again for the same run while pending answers
|
|
85
|
+
* `pending`; another run on the thread answers `busy`.
|
|
74
86
|
*/
|
|
75
87
|
async recover(req) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
88
|
+
const cur = this.runs.get(req.threadId);
|
|
89
|
+
if (cur)
|
|
90
|
+
return cur.waiting && cur.runId === req.runId ? "pending" : "busy";
|
|
91
|
+
const run = this.newRun(req.threadId, req.runId, req.agentId, req.metadata, false);
|
|
79
92
|
this.runs.set(run.threadId, run);
|
|
80
93
|
try {
|
|
81
|
-
|
|
94
|
+
// Hold events from the first one: the turn may end while we subscribe.
|
|
82
95
|
run.buffer = [];
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
if (!after) {
|
|
87
|
-
this.cleanup(run);
|
|
96
|
+
run.unsub = await this.o.daemon.subscribe(req.agentId, (e) => this.onEvent(run, e));
|
|
97
|
+
const t = await this.retail(run);
|
|
98
|
+
if (!t.found) {
|
|
88
99
|
const sent = req.sentAt == null ? 0 : new Date(req.sentAt).getTime();
|
|
89
|
-
|
|
100
|
+
const deadline = sent + (req.graceMs ?? 60_000);
|
|
101
|
+
if (Date.now() < deadline) {
|
|
102
|
+
run.waiting = { deadline };
|
|
103
|
+
run.buffer = null;
|
|
104
|
+
this.waitFor(run);
|
|
90
105
|
return "pending";
|
|
106
|
+
}
|
|
107
|
+
this.cleanup(run);
|
|
91
108
|
await this.o.store.saveError(req.threadId, { runId: req.runId, agentId: req.agentId, error: "the agent never got the message" });
|
|
92
109
|
return "failed";
|
|
93
110
|
}
|
|
94
|
-
if (
|
|
95
|
-
this.drain(run);
|
|
96
|
-
for (const e of this.resync(run.threadId, run))
|
|
97
|
-
this.emit(run.threadId, e);
|
|
111
|
+
if (t.running) {
|
|
98
112
|
this.watch(run);
|
|
113
|
+
this.drain(run, true);
|
|
99
114
|
return "following";
|
|
100
115
|
}
|
|
101
|
-
run
|
|
102
|
-
const ok = await this.finish(run, "done");
|
|
103
|
-
return ok ? "finished" : "failed";
|
|
116
|
+
return (await this.finishHeld(run)) ? "finished" : "failed";
|
|
104
117
|
}
|
|
105
118
|
catch (e) {
|
|
106
119
|
this.cleanup(run);
|
|
@@ -113,7 +126,7 @@ export class PaseoThreadRunner {
|
|
|
113
126
|
}
|
|
114
127
|
// ---- internals
|
|
115
128
|
newRun(threadId, runId, agentId, metadata, ours) {
|
|
116
|
-
return { threadId, runId, agentId, metadata, turn: new TurnFolder(), ours, turnId: null, epoch: null, seq: -1, buffer: null, unsub: null, last: Date.now(), watch: null, done: false };
|
|
129
|
+
return { threadId, runId, agentId, metadata, turn: new TurnFolder(), ours, turnId: null, epoch: null, seq: -1, buffer: null, again: null, sentAt: null, waiting: null, stopping: false, unsub: null, last: Date.now(), watch: null, done: false };
|
|
117
130
|
}
|
|
118
131
|
started(run) {
|
|
119
132
|
return { type: EventType.RUN_STARTED, threadId: run.threadId, runId: run.runId, ...(run.metadata ? { metadata: run.metadata } : {}) };
|
|
@@ -159,15 +172,18 @@ export class PaseoThreadRunner {
|
|
|
159
172
|
return run.unsub();
|
|
160
173
|
this.watch(run);
|
|
161
174
|
await d.send(agent, text, run.runId);
|
|
175
|
+
run.sentAt = Date.now();
|
|
162
176
|
}
|
|
163
177
|
onEvent(run, e) {
|
|
164
178
|
if (run.done)
|
|
165
179
|
return;
|
|
166
180
|
run.last = Date.now();
|
|
167
|
-
if (e.type === "lost")
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
181
|
+
if (e.type === "lost" || e.type === "replacement" || e.type === "subscription_restored") {
|
|
182
|
+
const resubscribe = e.type === "lost";
|
|
183
|
+
if (run.buffer)
|
|
184
|
+
return void (run.again = { resubscribe: resubscribe || Boolean(run.again?.resubscribe) });
|
|
185
|
+
return void this.regap(run, e.type === "lost" ? e.error : e.type, resubscribe);
|
|
186
|
+
}
|
|
171
187
|
if (run.buffer)
|
|
172
188
|
return void run.buffer.push(e);
|
|
173
189
|
this.apply(run, e);
|
|
@@ -198,14 +214,7 @@ export class PaseoThreadRunner {
|
|
|
198
214
|
this.emit(run.threadId, x);
|
|
199
215
|
return;
|
|
200
216
|
}
|
|
201
|
-
|
|
202
|
-
// Our message started this turn once it was sent (the runner subscribes before it sends).
|
|
203
|
-
if (run.agentId && !run.turnId) {
|
|
204
|
-
run.ours = true;
|
|
205
|
-
run.turnId = e.turnId ?? null;
|
|
206
|
-
}
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
217
|
+
// `turn_started` claims nothing: it may be someone else's turn. The daemon shows our message right after ours starts.
|
|
209
218
|
if (e.type !== "turn_completed" && e.type !== "turn_canceled" && e.type !== "turn_failed")
|
|
210
219
|
return;
|
|
211
220
|
if (!run.ours)
|
|
@@ -232,24 +241,83 @@ export class PaseoThreadRunner {
|
|
|
232
241
|
if (resubscribe) {
|
|
233
242
|
run.unsub?.();
|
|
234
243
|
run.unsub = await this.o.daemon.subscribe(run.agentId, (e) => this.onEvent(run, e));
|
|
244
|
+
if (run.done)
|
|
245
|
+
return run.unsub();
|
|
235
246
|
}
|
|
236
|
-
const
|
|
237
|
-
const tail = await this.o.daemon.tail(run.agentId, this.tailLimit);
|
|
247
|
+
const t = await this.retail(run, true);
|
|
238
248
|
if (run.done)
|
|
239
249
|
return;
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
return
|
|
244
|
-
}
|
|
245
|
-
this.drain(run, true);
|
|
250
|
+
if (t.found && !t.running)
|
|
251
|
+
return void (await this.finishHeld(run));
|
|
252
|
+
if (!t.found && !t.running && run.sentAt != null && Date.now() - run.sentAt >= this.quietMs)
|
|
253
|
+
return this.fail(run, "the agent never got the message");
|
|
246
254
|
run.last = Date.now();
|
|
255
|
+
this.drain(run, true);
|
|
247
256
|
}
|
|
248
257
|
catch (e) {
|
|
249
258
|
run.buffer = null;
|
|
250
259
|
this.fail(run, `${why}: ${e.message}`);
|
|
251
260
|
}
|
|
252
261
|
}
|
|
262
|
+
/** A pending recovery: look again every `waitMs` until the message shows up or the deadline passes. */
|
|
263
|
+
waitFor(run) {
|
|
264
|
+
if (run.watch || run.done)
|
|
265
|
+
return;
|
|
266
|
+
run.watch = setInterval(() => void this.checkWaiting(run), this.waitMs);
|
|
267
|
+
run.watch.unref();
|
|
268
|
+
}
|
|
269
|
+
async checkWaiting(run) {
|
|
270
|
+
if (run.done || !run.waiting || run.buffer)
|
|
271
|
+
return;
|
|
272
|
+
try {
|
|
273
|
+
const t = await this.retail(run);
|
|
274
|
+
if (run.done || !run.waiting)
|
|
275
|
+
return;
|
|
276
|
+
if (!t.found) {
|
|
277
|
+
if (Date.now() < run.waiting.deadline)
|
|
278
|
+
return;
|
|
279
|
+
return this.fail(run, "the agent never got the message");
|
|
280
|
+
}
|
|
281
|
+
const waiting = run.waiting;
|
|
282
|
+
run.waiting = null;
|
|
283
|
+
if (run.watch)
|
|
284
|
+
clearInterval(run.watch);
|
|
285
|
+
run.watch = null;
|
|
286
|
+
if (t.running) {
|
|
287
|
+
this.watch(run);
|
|
288
|
+
this.drain(run, true);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
void waiting;
|
|
292
|
+
await this.finishHeld(run);
|
|
293
|
+
}
|
|
294
|
+
catch (e) {
|
|
295
|
+
this.log(`agent-paseo thread ${run.threadId}: waiting: ${e.message}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/** Reads the agent and its tail and rebuilds the run; a full page without our message is read again, deeper. With `keep`, a tail without our message is an error rather than "not found". */
|
|
299
|
+
async retail(run, keep = false) {
|
|
300
|
+
const a = await this.o.daemon.agent(run.agentId);
|
|
301
|
+
let tail = await this.o.daemon.tail(run.agentId, this.tailLimit);
|
|
302
|
+
const has = () => tail.entries.some((x) => x.item.type === "user_message" && (x.item.clientMessageId === run.runId || x.item.messageId === run.runId));
|
|
303
|
+
if (!has() && tail.entries.length >= this.tailLimit)
|
|
304
|
+
tail = await this.o.daemon.tail(run.agentId, this.tailLimit * 10);
|
|
305
|
+
// A read that failed to reach our message must not replace what a claimed run already folded.
|
|
306
|
+
if (keep && run.ours && !has())
|
|
307
|
+
throw new Error("the turn is no longer in the timeline's tail");
|
|
308
|
+
const after = this.rebuild(run, tail.entries, tail.epoch, tail.seq);
|
|
309
|
+
// A later message in another turn means ours has ended, whatever the agent is doing now.
|
|
310
|
+
const next = Boolean(run.turnId && after?.some((x) => x.item.type === "user_message" && x.turnId && x.turnId !== run.turnId));
|
|
311
|
+
return { found: Boolean(after), running: a?.status === "running" && !next };
|
|
312
|
+
}
|
|
313
|
+
/** Finishes a turn the re-tail found ended, with the end event held meanwhile when there is one. */
|
|
314
|
+
finishHeld(run) {
|
|
315
|
+
const end = run.buffer?.find((e) => (e.type === "turn_completed" || e.type === "turn_canceled" || e.type === "turn_failed") && !(run.turnId && e.turnId && e.turnId !== run.turnId));
|
|
316
|
+
run.buffer = null;
|
|
317
|
+
if (end?.type === "turn_failed")
|
|
318
|
+
return this.finish(run, "error", end.error);
|
|
319
|
+
return this.finish(run, end?.type === "turn_canceled" || (!end && run.stopping) ? "stopped" : "done");
|
|
320
|
+
}
|
|
253
321
|
/** Replaces the run's folder with the turn in `entries` (after our message); null when our message is not there. */
|
|
254
322
|
rebuild(run, entries, epoch, seq) {
|
|
255
323
|
const i = entries.findIndex((x) => x.item.type === "user_message" && (x.item.clientMessageId === run.runId || x.item.messageId === run.runId));
|
|
@@ -270,7 +338,11 @@ export class PaseoThreadRunner {
|
|
|
270
338
|
/** Folds the held events; with `resync`, first sends every follower `READY` and the replay of the rebuilt turn. */
|
|
271
339
|
drain(run, resync = false) {
|
|
272
340
|
const held = run.buffer ?? [];
|
|
341
|
+
const again = run.again;
|
|
273
342
|
run.buffer = null;
|
|
343
|
+
run.again = null;
|
|
344
|
+
if (again)
|
|
345
|
+
return void this.regap(run, "another gap", again.resubscribe);
|
|
274
346
|
if (resync)
|
|
275
347
|
for (const e of this.resync(run.threadId, run))
|
|
276
348
|
this.emit(run.threadId, e);
|
|
@@ -281,11 +353,9 @@ export class PaseoThreadRunner {
|
|
|
281
353
|
}
|
|
282
354
|
}
|
|
283
355
|
/** A quiet or broken stream: the daemon says whether the turn ended while no event came. */
|
|
284
|
-
async check(run
|
|
356
|
+
async check(run) {
|
|
285
357
|
if (run.done || !run.agentId || run.buffer)
|
|
286
358
|
return;
|
|
287
|
-
if (lost)
|
|
288
|
-
return void this.regap(run, lost, true);
|
|
289
359
|
if (Date.now() - run.last < this.quietMs)
|
|
290
360
|
return;
|
|
291
361
|
try {
|
|
@@ -300,6 +370,7 @@ export class PaseoThreadRunner {
|
|
|
300
370
|
}
|
|
301
371
|
cleanup(run) {
|
|
302
372
|
run.done = true;
|
|
373
|
+
run.waiting = null;
|
|
303
374
|
run.unsub?.();
|
|
304
375
|
run.unsub = null;
|
|
305
376
|
if (run.watch)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightblues/jig",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Reusable parts for my apps: agent runtimes (Paseo, pi) that speak AG-UI, a thin thread-following client, and more as they prove reusable.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|