@lightblues/jig 0.0.4 → 0.0.6
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/follow.d.ts +64 -0
- package/dist/agent-paseo/follow.js +305 -0
- package/dist/agent-paseo/index.d.ts +2 -0
- package/dist/agent-paseo/index.js +1 -0
- package/dist/agent-paseo/runner.d.ts +8 -1
- package/dist/agent-paseo/runner.js +55 -7
- package/dist/agent-paseo/turn.d.ts +10 -0
- package/dist/agent-paseo/turn.js +6 -0
- package/package.json +1 -1
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { MessagesSnapshotEvent } from "@ag-ui/core";
|
|
2
|
+
import type { Daemon } from "./daemon.ts";
|
|
3
|
+
import { type PaseoEvent } from "./turn.ts";
|
|
4
|
+
/** What a follower receives: the runner's events plus `MESSAGES_SNAPSHOT` for the agent's earlier turns. */
|
|
5
|
+
export type FollowEvent = PaseoEvent | MessagesSnapshotEvent;
|
|
6
|
+
export type FollowListener = (e: FollowEvent) => void;
|
|
7
|
+
export interface AgentFollowerOptions {
|
|
8
|
+
/** Timeline entries read for history and after a gap. Default 300. */
|
|
9
|
+
tailLimit?: number;
|
|
10
|
+
/** How long a running turn may go without an event before the daemon is asked how the agent is. Default 60 s. */
|
|
11
|
+
quietMs?: number;
|
|
12
|
+
/** Where errors of background reads go. Default `console.error`. */
|
|
13
|
+
log?: (message: string) => void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Follows one Paseo agent read-only: never sends, never claims a turn. Every turn of the agent is a run
|
|
17
|
+
* (`runId` = the daemon's `turnId`, `threadId` = the agent id). A listener gets `CUSTOM READY`, a
|
|
18
|
+
* `MESSAGES_SNAPSHOT` of the earlier turns in the tail, and, when a turn is running, `RUN_STARTED` and its
|
|
19
|
+
* replay (the user message as `TEXT_MESSAGE_*` with role `user`, then text and tool calls); then live events.
|
|
20
|
+
* After any gap in the daemon's stream every listener gets that start again, so a follower always resets on
|
|
21
|
+
* `READY`. One subscription to the daemon is shared by all listeners.
|
|
22
|
+
*/
|
|
23
|
+
export declare class AgentFollower {
|
|
24
|
+
readonly agentId: string;
|
|
25
|
+
private daemon;
|
|
26
|
+
private tailLimit;
|
|
27
|
+
private quietMs;
|
|
28
|
+
private log;
|
|
29
|
+
private listeners;
|
|
30
|
+
private history;
|
|
31
|
+
private turn;
|
|
32
|
+
private epoch;
|
|
33
|
+
private seq;
|
|
34
|
+
private buffer;
|
|
35
|
+
private again;
|
|
36
|
+
private unsub;
|
|
37
|
+
private watch;
|
|
38
|
+
private last;
|
|
39
|
+
private closed;
|
|
40
|
+
constructor(daemon: Daemon, agentId: string, o?: AgentFollowerOptions);
|
|
41
|
+
/** Subscribes, then reads the history; events that arrive meanwhile are held and folded after it. */
|
|
42
|
+
start(): Promise<void>;
|
|
43
|
+
/** The turn running now, if any. */
|
|
44
|
+
running(): string | null;
|
|
45
|
+
listen(fn: FollowListener): () => void;
|
|
46
|
+
close(): void;
|
|
47
|
+
private emit;
|
|
48
|
+
private resync;
|
|
49
|
+
private started;
|
|
50
|
+
/** Reads the agent and its tail and rebuilds history and the running turn. The status is read first. */
|
|
51
|
+
private read;
|
|
52
|
+
private onEvent;
|
|
53
|
+
private apply;
|
|
54
|
+
/** A new turn: a turn still open ended without us seeing its end (it is closed as finished). */
|
|
55
|
+
private begin;
|
|
56
|
+
private end;
|
|
57
|
+
private regap;
|
|
58
|
+
private lostSub;
|
|
59
|
+
private drain;
|
|
60
|
+
/** A quiet running turn, or a gap that could not be filled: ask the daemon and re-read when needed. */
|
|
61
|
+
private check;
|
|
62
|
+
}
|
|
63
|
+
/** Starts following an agent read-only; resolves once history is read and the live stream is on. */
|
|
64
|
+
export declare function followAgent(daemon: Daemon, agentId: string, o?: AgentFollowerOptions): Promise<AgentFollower>;
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { EventType } from "@ag-ui/core";
|
|
2
|
+
import { TurnFolder } from "./turn.js";
|
|
3
|
+
/**
|
|
4
|
+
* Follows one Paseo agent read-only: never sends, never claims a turn. Every turn of the agent is a run
|
|
5
|
+
* (`runId` = the daemon's `turnId`, `threadId` = the agent id). A listener gets `CUSTOM READY`, a
|
|
6
|
+
* `MESSAGES_SNAPSHOT` of the earlier turns in the tail, and, when a turn is running, `RUN_STARTED` and its
|
|
7
|
+
* replay (the user message as `TEXT_MESSAGE_*` with role `user`, then text and tool calls); then live events.
|
|
8
|
+
* After any gap in the daemon's stream every listener gets that start again, so a follower always resets on
|
|
9
|
+
* `READY`. One subscription to the daemon is shared by all listeners.
|
|
10
|
+
*/
|
|
11
|
+
export class AgentFollower {
|
|
12
|
+
agentId;
|
|
13
|
+
daemon;
|
|
14
|
+
tailLimit;
|
|
15
|
+
quietMs;
|
|
16
|
+
log;
|
|
17
|
+
listeners = new Set();
|
|
18
|
+
history = [];
|
|
19
|
+
turn = null;
|
|
20
|
+
epoch = null;
|
|
21
|
+
seq = -1;
|
|
22
|
+
buffer = null;
|
|
23
|
+
again = null;
|
|
24
|
+
unsub = null;
|
|
25
|
+
watch = null;
|
|
26
|
+
last = Date.now();
|
|
27
|
+
closed = false;
|
|
28
|
+
constructor(daemon, agentId, o = {}) {
|
|
29
|
+
this.daemon = daemon;
|
|
30
|
+
this.agentId = agentId;
|
|
31
|
+
this.tailLimit = o.tailLimit ?? 300;
|
|
32
|
+
this.quietMs = o.quietMs ?? 60_000;
|
|
33
|
+
this.log = o.log ?? ((m) => console.error(m));
|
|
34
|
+
}
|
|
35
|
+
/** Subscribes, then reads the history; events that arrive meanwhile are held and folded after it. */
|
|
36
|
+
async start() {
|
|
37
|
+
this.buffer = [];
|
|
38
|
+
this.unsub = await this.daemon.subscribe(this.agentId, (e) => this.onEvent(e));
|
|
39
|
+
try {
|
|
40
|
+
await this.read();
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
this.close();
|
|
44
|
+
throw e;
|
|
45
|
+
}
|
|
46
|
+
this.watch = setInterval(() => void this.check(), Math.max(1000, this.quietMs / 2));
|
|
47
|
+
this.watch.unref();
|
|
48
|
+
this.drain();
|
|
49
|
+
}
|
|
50
|
+
/** The turn running now, if any. */
|
|
51
|
+
running() {
|
|
52
|
+
return this.turn?.turnId ?? null;
|
|
53
|
+
}
|
|
54
|
+
listen(fn) {
|
|
55
|
+
this.listeners.add(fn);
|
|
56
|
+
for (const e of this.resync())
|
|
57
|
+
safe(fn, e);
|
|
58
|
+
return () => void this.listeners.delete(fn);
|
|
59
|
+
}
|
|
60
|
+
close() {
|
|
61
|
+
this.closed = true;
|
|
62
|
+
this.unsub?.();
|
|
63
|
+
this.unsub = null;
|
|
64
|
+
if (this.watch)
|
|
65
|
+
clearInterval(this.watch);
|
|
66
|
+
this.watch = null;
|
|
67
|
+
this.listeners.clear();
|
|
68
|
+
}
|
|
69
|
+
// ---- internals
|
|
70
|
+
emit(e) {
|
|
71
|
+
for (const f of [...this.listeners])
|
|
72
|
+
safe(f, e);
|
|
73
|
+
}
|
|
74
|
+
resync() {
|
|
75
|
+
const t = this.turn;
|
|
76
|
+
const value = { threadId: this.agentId, runId: t?.turnId ?? null, running: Boolean(t) };
|
|
77
|
+
const out = [
|
|
78
|
+
{ type: EventType.CUSTOM, name: "READY", value },
|
|
79
|
+
{ type: EventType.MESSAGES_SNAPSHOT, messages: [...this.history] },
|
|
80
|
+
];
|
|
81
|
+
if (t)
|
|
82
|
+
out.push(this.started(t), ...(t.user ? userEvents(t.user) : []), ...t.folder.replay());
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
started(t) {
|
|
86
|
+
return { type: EventType.RUN_STARTED, threadId: this.agentId, runId: t.turnId };
|
|
87
|
+
}
|
|
88
|
+
/** Reads the agent and its tail and rebuilds history and the running turn. The status is read first. */
|
|
89
|
+
async read() {
|
|
90
|
+
const a = await this.daemon.agent(this.agentId);
|
|
91
|
+
const tail = await this.daemon.tail(this.agentId, this.tailLimit);
|
|
92
|
+
if (tail.epoch) {
|
|
93
|
+
this.epoch = tail.epoch;
|
|
94
|
+
this.seq = tail.seq ?? -1;
|
|
95
|
+
}
|
|
96
|
+
const turns = group(tail.entries);
|
|
97
|
+
const runningNow = a?.status === "running";
|
|
98
|
+
const cur = runningNow ? turns.at(-1) ?? null : null;
|
|
99
|
+
this.history = [];
|
|
100
|
+
for (const t of turns)
|
|
101
|
+
if (t !== cur)
|
|
102
|
+
this.history.push(...messages(t));
|
|
103
|
+
this.turn = cur;
|
|
104
|
+
this.last = Date.now();
|
|
105
|
+
}
|
|
106
|
+
onEvent(e) {
|
|
107
|
+
if (this.closed)
|
|
108
|
+
return;
|
|
109
|
+
this.last = Date.now();
|
|
110
|
+
if (e.type === "lost" || e.type === "replacement" || e.type === "subscription_restored") {
|
|
111
|
+
const resubscribe = e.type === "lost";
|
|
112
|
+
if (this.buffer)
|
|
113
|
+
return void (this.again = { resubscribe: resubscribe || Boolean(this.again?.resubscribe) });
|
|
114
|
+
return void this.regap(e.type === "lost" ? e.error : e.type, resubscribe);
|
|
115
|
+
}
|
|
116
|
+
if (this.buffer)
|
|
117
|
+
return void this.buffer.push(e);
|
|
118
|
+
this.apply(e);
|
|
119
|
+
}
|
|
120
|
+
apply(e) {
|
|
121
|
+
if (e.type === "timeline") {
|
|
122
|
+
if (e.seq != null && e.epoch) {
|
|
123
|
+
if (this.epoch && e.epoch !== this.epoch)
|
|
124
|
+
return void this.regap("epoch changed");
|
|
125
|
+
if (e.epoch === this.epoch && e.seq <= this.seq)
|
|
126
|
+
return;
|
|
127
|
+
this.epoch = e.epoch;
|
|
128
|
+
this.seq = e.seq;
|
|
129
|
+
}
|
|
130
|
+
const it = e.item;
|
|
131
|
+
if (e.turnId && this.turn?.turnId !== e.turnId)
|
|
132
|
+
this.begin(e.turnId);
|
|
133
|
+
const t = this.turn;
|
|
134
|
+
if (!t)
|
|
135
|
+
return;
|
|
136
|
+
if (it.type === "user_message") {
|
|
137
|
+
const text = it.text?.trim();
|
|
138
|
+
if (!text || t.user)
|
|
139
|
+
return;
|
|
140
|
+
t.user = { id: it.clientMessageId ?? it.messageId ?? `u-${t.turnId}`, text };
|
|
141
|
+
for (const x of userEvents(t.user))
|
|
142
|
+
this.emit(x);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
for (const x of t.folder.push(it))
|
|
146
|
+
this.emit(x);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (e.type === "turn_started") {
|
|
150
|
+
if (e.turnId && this.turn?.turnId !== e.turnId)
|
|
151
|
+
this.begin(e.turnId);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (e.type !== "turn_completed" && e.type !== "turn_canceled" && e.type !== "turn_failed")
|
|
155
|
+
return;
|
|
156
|
+
const t = this.turn;
|
|
157
|
+
if (!t || (e.turnId && e.turnId !== t.turnId))
|
|
158
|
+
return;
|
|
159
|
+
this.end(t, e.type === "turn_completed" ? "done" : e.type === "turn_canceled" ? "stopped" : "error", e.type === "turn_failed" ? e.error : undefined);
|
|
160
|
+
}
|
|
161
|
+
/** A new turn: a turn still open ended without us seeing its end (it is closed as finished). */
|
|
162
|
+
begin(turnId) {
|
|
163
|
+
if (this.turn)
|
|
164
|
+
this.end(this.turn, "done");
|
|
165
|
+
this.turn = { turnId, user: null, folder: new TurnFolder() };
|
|
166
|
+
this.emit(this.started(this.turn));
|
|
167
|
+
}
|
|
168
|
+
end(t, status, error) {
|
|
169
|
+
for (const x of t.folder.end())
|
|
170
|
+
this.emit(x);
|
|
171
|
+
this.history.push(...messages(t));
|
|
172
|
+
this.turn = null;
|
|
173
|
+
if (status === "error")
|
|
174
|
+
this.emit({ type: EventType.RUN_ERROR, message: error ?? "the turn failed", metadata: { threadId: this.agentId, runId: t.turnId } });
|
|
175
|
+
else
|
|
176
|
+
this.emit({
|
|
177
|
+
type: EventType.RUN_FINISHED,
|
|
178
|
+
threadId: this.agentId,
|
|
179
|
+
runId: t.turnId,
|
|
180
|
+
...(status === "stopped" ? { outcome: { type: "cancelled" }, metadata: { stopped: true } } : {}),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
async regap(why, resubscribe = false) {
|
|
184
|
+
if (this.closed || this.buffer)
|
|
185
|
+
return;
|
|
186
|
+
this.buffer = [];
|
|
187
|
+
try {
|
|
188
|
+
if (resubscribe) {
|
|
189
|
+
this.unsub?.();
|
|
190
|
+
this.unsub = await this.daemon.subscribe(this.agentId, (e) => this.onEvent(e));
|
|
191
|
+
if (this.closed)
|
|
192
|
+
return this.unsub();
|
|
193
|
+
}
|
|
194
|
+
await this.read();
|
|
195
|
+
if (this.closed)
|
|
196
|
+
return;
|
|
197
|
+
for (const e of this.resync())
|
|
198
|
+
this.emit(e);
|
|
199
|
+
this.drain();
|
|
200
|
+
}
|
|
201
|
+
catch (e) {
|
|
202
|
+
this.buffer = null;
|
|
203
|
+
this.again = null;
|
|
204
|
+
this.log(`agent-paseo follow ${this.agentId}: ${why}: ${e.message}`);
|
|
205
|
+
// Try again on the next check; a lost subscription is replaced then.
|
|
206
|
+
this.lostSub = this.lostSub || resubscribe;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
lostSub = false;
|
|
210
|
+
drain() {
|
|
211
|
+
const held = this.buffer ?? [];
|
|
212
|
+
const again = this.again;
|
|
213
|
+
this.buffer = null;
|
|
214
|
+
this.again = null;
|
|
215
|
+
if (again)
|
|
216
|
+
return void this.regap("another gap", again.resubscribe);
|
|
217
|
+
for (const e of held) {
|
|
218
|
+
if (this.closed || this.buffer)
|
|
219
|
+
return;
|
|
220
|
+
this.apply(e);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/** A quiet running turn, or a gap that could not be filled: ask the daemon and re-read when needed. */
|
|
224
|
+
async check() {
|
|
225
|
+
if (this.closed || this.buffer)
|
|
226
|
+
return;
|
|
227
|
+
if (this.lostSub) {
|
|
228
|
+
this.lostSub = false;
|
|
229
|
+
return void this.regap("retry", true);
|
|
230
|
+
}
|
|
231
|
+
if (!this.turn || Date.now() - this.last < this.quietMs)
|
|
232
|
+
return;
|
|
233
|
+
try {
|
|
234
|
+
const a = await this.daemon.agent(this.agentId);
|
|
235
|
+
if (a?.status === "running" || this.closed)
|
|
236
|
+
return void (this.last = Date.now());
|
|
237
|
+
await this.regap("quiet");
|
|
238
|
+
}
|
|
239
|
+
catch (e) {
|
|
240
|
+
this.log(`agent-paseo follow ${this.agentId}: check: ${e.message}`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/** Starts following an agent read-only; resolves once history is read and the live stream is on. */
|
|
245
|
+
export async function followAgent(daemon, agentId, o = {}) {
|
|
246
|
+
const f = new AgentFollower(daemon, agentId, o);
|
|
247
|
+
await f.start();
|
|
248
|
+
return f;
|
|
249
|
+
}
|
|
250
|
+
/** Splits tail entries into turns by `turnId`; items without one join the turn before them. */
|
|
251
|
+
function group(entries) {
|
|
252
|
+
const turns = [];
|
|
253
|
+
for (const x of entries) {
|
|
254
|
+
let t = turns.at(-1);
|
|
255
|
+
if (x.turnId && t?.turnId !== x.turnId)
|
|
256
|
+
turns.push((t = { turnId: x.turnId, user: null, folder: new TurnFolder() }));
|
|
257
|
+
if (!t)
|
|
258
|
+
continue;
|
|
259
|
+
if (x.item.type === "user_message") {
|
|
260
|
+
const text = x.item.text?.trim();
|
|
261
|
+
if (text && !t.user)
|
|
262
|
+
t.user = { id: x.item.clientMessageId ?? x.item.messageId ?? `u-${t.turnId}`, text };
|
|
263
|
+
}
|
|
264
|
+
else
|
|
265
|
+
t.folder.push(x.item);
|
|
266
|
+
}
|
|
267
|
+
return turns;
|
|
268
|
+
}
|
|
269
|
+
/** A finished turn as AG-UI messages: the user message, then assistant texts, tool calls grouped between them. */
|
|
270
|
+
function messages(t) {
|
|
271
|
+
const out = [];
|
|
272
|
+
if (t.user)
|
|
273
|
+
out.push({ id: t.user.id, role: "user", content: t.user.text });
|
|
274
|
+
let tools = null;
|
|
275
|
+
for (const p of t.folder.parts()) {
|
|
276
|
+
if (p.kind === "text") {
|
|
277
|
+
tools = null;
|
|
278
|
+
if (p.text.trim())
|
|
279
|
+
out.push({ id: p.id, role: "assistant", content: p.text });
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
if (!tools)
|
|
283
|
+
out.push((tools = { id: `tools-${p.id}`, role: "assistant", toolCalls: [] }));
|
|
284
|
+
tools.toolCalls.push({
|
|
285
|
+
id: p.id,
|
|
286
|
+
type: "function",
|
|
287
|
+
function: { name: p.tool.name, arguments: JSON.stringify({ summary: p.tool.summary, status: p.tool.status }) },
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return out;
|
|
292
|
+
}
|
|
293
|
+
function userEvents(u) {
|
|
294
|
+
return [
|
|
295
|
+
{ type: EventType.TEXT_MESSAGE_START, messageId: u.id, role: "user" },
|
|
296
|
+
{ type: EventType.TEXT_MESSAGE_CONTENT, messageId: u.id, delta: u.text },
|
|
297
|
+
{ type: EventType.TEXT_MESSAGE_END, messageId: u.id },
|
|
298
|
+
];
|
|
299
|
+
}
|
|
300
|
+
function safe(f, e) {
|
|
301
|
+
try {
|
|
302
|
+
f(e);
|
|
303
|
+
}
|
|
304
|
+
catch { }
|
|
305
|
+
}
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export { assertLocalDaemon, liveEvent, localAddress, paseoDaemon, paseoHome } from "./daemon.ts";
|
|
8
8
|
export type { AgentInfo, Daemon, LiveEvent, PaseoDaemonConfig, Tail } from "./daemon.ts";
|
|
9
|
+
export { AgentFollower, followAgent } from "./follow.ts";
|
|
10
|
+
export type { AgentFollowerOptions, FollowEvent, FollowListener } from "./follow.ts";
|
|
9
11
|
export { PaseoThreadRunner, paseoThreadRunner } from "./runner.ts";
|
|
10
12
|
export type { Listener, PaseoThreadRunnerOptions, ReadyValue, RecoverRequest, RecoverResult, SendRequest, ThreadStore } from "./runner.ts";
|
|
11
13
|
export { TurnFolder, afterMessage, chat, finalText, isProviderError, toolSummary } from "./turn.ts";
|
|
@@ -5,5 +5,6 @@
|
|
|
5
5
|
* See `.ea/docs/agent-paseo.md`.
|
|
6
6
|
*/
|
|
7
7
|
export { assertLocalDaemon, liveEvent, localAddress, paseoDaemon, paseoHome } from "./daemon.js";
|
|
8
|
+
export { AgentFollower, followAgent } from "./follow.js";
|
|
8
9
|
export { PaseoThreadRunner, paseoThreadRunner } from "./runner.js";
|
|
9
10
|
export { TurnFolder, afterMessage, chat, finalText, isProviderError, toolSummary } from "./turn.js";
|
|
@@ -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,9 @@ 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;
|
|
139
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". */
|
|
140
147
|
private retail;
|
|
141
148
|
/** Finishes a turn the re-tail found ended, with the end event held meanwhile when there is one. */
|
|
@@ -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) {
|
|
@@ -77,12 +79,16 @@ export class PaseoThreadRunner {
|
|
|
77
79
|
/**
|
|
78
80
|
* After a restart: a message the host sent and has no answer for is looked up in its agent's timeline. A turn
|
|
79
81
|
* still running is followed (`following`); one that ended is stored (`finished` / `failed`); a message the
|
|
80
|
-
* 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`.
|
|
81
86
|
*/
|
|
82
87
|
async recover(req) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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);
|
|
86
92
|
this.runs.set(run.threadId, run);
|
|
87
93
|
try {
|
|
88
94
|
// Hold events from the first one: the turn may end while we subscribe.
|
|
@@ -90,10 +96,15 @@ export class PaseoThreadRunner {
|
|
|
90
96
|
run.unsub = await this.o.daemon.subscribe(req.agentId, (e) => this.onEvent(run, e));
|
|
91
97
|
const t = await this.retail(run);
|
|
92
98
|
if (!t.found) {
|
|
93
|
-
this.cleanup(run);
|
|
94
99
|
const sent = req.sentAt == null ? 0 : new Date(req.sentAt).getTime();
|
|
95
|
-
|
|
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);
|
|
96
105
|
return "pending";
|
|
106
|
+
}
|
|
107
|
+
this.cleanup(run);
|
|
97
108
|
await this.o.store.saveError(req.threadId, { runId: req.runId, agentId: req.agentId, error: "the agent never got the message" });
|
|
98
109
|
return "failed";
|
|
99
110
|
}
|
|
@@ -115,7 +126,7 @@ export class PaseoThreadRunner {
|
|
|
115
126
|
}
|
|
116
127
|
// ---- internals
|
|
117
128
|
newRun(threadId, runId, agentId, metadata, ours) {
|
|
118
|
-
return { threadId, runId, agentId, metadata, turn: new TurnFolder(), ours, turnId: null, epoch: null, seq: -1, buffer: null, again: null, sentAt: null, stopping: false, 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 };
|
|
119
130
|
}
|
|
120
131
|
started(run) {
|
|
121
132
|
return { type: EventType.RUN_STARTED, threadId: run.threadId, runId: run.runId, ...(run.metadata ? { metadata: run.metadata } : {}) };
|
|
@@ -248,6 +259,42 @@ export class PaseoThreadRunner {
|
|
|
248
259
|
this.fail(run, `${why}: ${e.message}`);
|
|
249
260
|
}
|
|
250
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
|
+
}
|
|
251
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". */
|
|
252
299
|
async retail(run, keep = false) {
|
|
253
300
|
const a = await this.o.daemon.agent(run.agentId);
|
|
@@ -323,6 +370,7 @@ export class PaseoThreadRunner {
|
|
|
323
370
|
}
|
|
324
371
|
cleanup(run) {
|
|
325
372
|
run.done = true;
|
|
373
|
+
run.waiting = null;
|
|
326
374
|
run.unsub?.();
|
|
327
375
|
run.unsub = null;
|
|
328
376
|
if (run.watch)
|
|
@@ -94,6 +94,16 @@ export declare class TurnFolder {
|
|
|
94
94
|
/** The events that rebuild the turn so far, for a follower that joins mid-turn or after a gap. */
|
|
95
95
|
replay(): FoldedEvent[];
|
|
96
96
|
text(): string;
|
|
97
|
+
/** The turn's parts in order: assistant texts and tool calls (for a history snapshot). */
|
|
98
|
+
parts(): ({
|
|
99
|
+
kind: "text";
|
|
100
|
+
id: string;
|
|
101
|
+
text: string;
|
|
102
|
+
} | {
|
|
103
|
+
kind: "tool";
|
|
104
|
+
id: string;
|
|
105
|
+
tool: ToolView;
|
|
106
|
+
})[];
|
|
97
107
|
toolList(): ToolView[];
|
|
98
108
|
/** Reconstruct a TurnFolder from a projected tail slice (afterMessage). Used in recovery. */
|
|
99
109
|
static fromEntries(entries: TimelineEntry[]): TurnFolder;
|
package/dist/agent-paseo/turn.js
CHANGED
|
@@ -199,6 +199,12 @@ export class TurnFolder {
|
|
|
199
199
|
}
|
|
200
200
|
return parts.filter(Boolean).join("\n\n");
|
|
201
201
|
}
|
|
202
|
+
/** The turn's parts in order: assistant texts and tool calls (for a history snapshot). */
|
|
203
|
+
parts() {
|
|
204
|
+
return this.order.map((x) => x.kind === "text"
|
|
205
|
+
? { kind: "text", id: x.id, text: this.texts.get(x.id) }
|
|
206
|
+
: { kind: "tool", id: x.id, tool: (({ name, summary, status }) => ({ name, summary, status }))(this.tools.get(x.id)) });
|
|
207
|
+
}
|
|
202
208
|
toolList() {
|
|
203
209
|
return [...this.tools.values()].map(({ name, summary, status }) => ({ name, summary, status })).slice(-GROUP_TOOLS);
|
|
204
210
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightblues/jig",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
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",
|