@lightblues/jig 0.0.6 → 0.0.8
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/README.md +1 -1
- package/dist/agent-chat/follow.d.ts +44 -0
- package/dist/agent-chat/follow.js +146 -0
- package/dist/agent-chat/index.d.ts +12 -0
- package/dist/agent-chat/index.js +9 -0
- package/dist/agent-chat/react.d.ts +57 -0
- package/dist/agent-chat/react.js +90 -0
- package/dist/agent-chat/reduce.d.ts +103 -0
- package/dist/agent-chat/reduce.js +211 -0
- package/dist/agent-chat/sse.d.ts +13 -0
- package/dist/agent-chat/sse.js +74 -0
- package/dist/agent-pi/index.d.ts +11 -0
- package/dist/agent-pi/index.js +8 -0
- package/dist/agent-pi/models.d.ts +91 -0
- package/dist/agent-pi/models.js +107 -0
- package/dist/agent-pi/run.d.ts +160 -0
- package/dist/agent-pi/run.js +471 -0
- package/dist/agent-pi/store.d.ts +112 -0
- package/dist/agent-pi/store.js +161 -0
- package/package.json +41 -2
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Planned parts (design: baton `.ea/log/2026-11-24-shared-agent-ui.md`, item #274)
|
|
|
7
7
|
| Subpath | What |
|
|
8
8
|
|---|---|
|
|
9
9
|
| `@lightblues/jig/agent-paseo` | server adapter: a Paseo agent's timeline as AG-UI events, with replay and recovery |
|
|
10
|
-
| `@lightblues/jig/agent-pi` | server runtime for in-app pi agents: models
|
|
10
|
+
| `@lightblues/jig/agent-pi` | server runtime for in-app pi agents: router models with tiers, sessions and runs in the host's own Postgres tables, one run as AG-UI events (run registry and SSE route: next) |
|
|
11
11
|
| `@lightblues/jig/agent-chat` | a thin headless client: follow a thread over SSE, reconnect, fold events into messages |
|
|
12
12
|
|
|
13
13
|
The protocol is AG-UI (`@ag-ui/core`), used as is.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { BaseEvent } from "@ag-ui/core";
|
|
2
|
+
import { type ThreadState } from "./reduce.ts";
|
|
3
|
+
export interface FollowOptions {
|
|
4
|
+
/** `fetch` to use (cookies, auth headers, expo/fetch on React Native). Default: the global `fetch`. */
|
|
5
|
+
fetch?: (url: string, init: {
|
|
6
|
+
signal: AbortSignal;
|
|
7
|
+
headers: Record<string, string>;
|
|
8
|
+
credentials?: "include" | "same-origin" | "omit";
|
|
9
|
+
}) => Promise<Response>;
|
|
10
|
+
/** Extra request headers (`accept: text/event-stream` is always sent). */
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
/** Default `same-origin`; ignored by fetches that do not know it. */
|
|
13
|
+
credentials?: "include" | "same-origin" | "omit";
|
|
14
|
+
/** Each new state (after every event and every connect / drop). */
|
|
15
|
+
onState?: (state: ThreadState) => void;
|
|
16
|
+
/** Each event, after it was folded (for a host's own side effects: refetch on `RUN_FINISHED`, a toast on `RUN_ERROR`). */
|
|
17
|
+
onEvent?: (event: BaseEvent, state: ThreadState) => void;
|
|
18
|
+
/** Stops following: aborts the request and any pending retry. */
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
/** Backoff after a failed or dropped connection: `min(baseMs * 2^n, maxMs)`, n reset on each successful connect. Defaults 1000 / 15000. */
|
|
21
|
+
baseMs?: number;
|
|
22
|
+
maxMs?: number;
|
|
23
|
+
/** A connection that delivered nothing for this long is dropped and reopened (hosts send a ping comment every 15 s; set above that). Default 45000; 0 turns it off. */
|
|
24
|
+
idleMs?: number;
|
|
25
|
+
/** Starting state (default `initialState`). */
|
|
26
|
+
state?: ThreadState;
|
|
27
|
+
}
|
|
28
|
+
export interface Follow {
|
|
29
|
+
/** The current state. */
|
|
30
|
+
readonly state: ThreadState;
|
|
31
|
+
/** Stops following (same as aborting `signal`). */
|
|
32
|
+
close(): void;
|
|
33
|
+
/** Drops the connection and reconnects now, without waiting for the backoff (a host's "retry" after its own POST, or `online`). */
|
|
34
|
+
reconnect(): void;
|
|
35
|
+
/** Resolves when following has stopped. */
|
|
36
|
+
done: Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Follows a thread's SSE stream: connects, folds every event with `reduce`, and reconnects with backoff when the
|
|
40
|
+
* response fails or the stream ends or stalls. The server sends `READY` (+ `RUN_STARTED` and a replay of a run in
|
|
41
|
+
* progress) on each connect, which resets the live run, so a reconnect never duplicates text: no event id or
|
|
42
|
+
* client-side dedupe is involved. The state's `connected` turns false on a drop and true on the next response.
|
|
43
|
+
*/
|
|
44
|
+
export declare function followThread(url: string, o?: FollowOptions): Follow;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { initialState, reduce } from "./reduce.js";
|
|
2
|
+
import { readSse } from "./sse.js";
|
|
3
|
+
/**
|
|
4
|
+
* Follows a thread's SSE stream: connects, folds every event with `reduce`, and reconnects with backoff when the
|
|
5
|
+
* response fails or the stream ends or stalls. The server sends `READY` (+ `RUN_STARTED` and a replay of a run in
|
|
6
|
+
* progress) on each connect, which resets the live run, so a reconnect never duplicates text: no event id or
|
|
7
|
+
* client-side dedupe is involved. The state's `connected` turns false on a drop and true on the next response.
|
|
8
|
+
*/
|
|
9
|
+
export function followThread(url, o = {}) {
|
|
10
|
+
const doFetch = o.fetch ?? ((u, init) => fetch(u, init));
|
|
11
|
+
const base = o.baseMs ?? 1000;
|
|
12
|
+
const max = o.maxMs ?? 15_000;
|
|
13
|
+
const idleMs = o.idleMs ?? 45_000;
|
|
14
|
+
const stop = new AbortController();
|
|
15
|
+
let state = o.state ?? initialState;
|
|
16
|
+
let current = null;
|
|
17
|
+
let wake = null;
|
|
18
|
+
/** `reconnect()` asked for the next connection at once. */
|
|
19
|
+
let skip = false;
|
|
20
|
+
const set = (s) => {
|
|
21
|
+
if (s === state || stop.signal.aborted)
|
|
22
|
+
return;
|
|
23
|
+
state = s;
|
|
24
|
+
o.onState?.(s);
|
|
25
|
+
};
|
|
26
|
+
const close = () => {
|
|
27
|
+
stop.abort();
|
|
28
|
+
current?.abort();
|
|
29
|
+
wake?.();
|
|
30
|
+
};
|
|
31
|
+
if (o.signal?.aborted)
|
|
32
|
+
close();
|
|
33
|
+
else
|
|
34
|
+
o.signal?.addEventListener("abort", close, { once: true });
|
|
35
|
+
/** Waits `ms`, or less when closed or `reconnect()` is called. */
|
|
36
|
+
const wait = (ms) => new Promise((resolve) => {
|
|
37
|
+
if (stop.signal.aborted)
|
|
38
|
+
return resolve();
|
|
39
|
+
const t = setTimeout(done, ms);
|
|
40
|
+
function done() {
|
|
41
|
+
clearTimeout(t);
|
|
42
|
+
wake = null;
|
|
43
|
+
resolve();
|
|
44
|
+
}
|
|
45
|
+
wake = done;
|
|
46
|
+
});
|
|
47
|
+
const once = async () => {
|
|
48
|
+
const ctl = new AbortController();
|
|
49
|
+
current = ctl;
|
|
50
|
+
let idle;
|
|
51
|
+
const poke = () => {
|
|
52
|
+
if (!idleMs)
|
|
53
|
+
return;
|
|
54
|
+
clearTimeout(idle);
|
|
55
|
+
idle = setTimeout(() => ctl.abort(), idleMs);
|
|
56
|
+
};
|
|
57
|
+
let ok = false;
|
|
58
|
+
try {
|
|
59
|
+
poke();
|
|
60
|
+
const res = await doFetch(url, {
|
|
61
|
+
signal: ctl.signal,
|
|
62
|
+
headers: { accept: "text/event-stream", ...o.headers },
|
|
63
|
+
...(o.credentials !== undefined ? { credentials: o.credentials } : { credentials: "same-origin" }),
|
|
64
|
+
});
|
|
65
|
+
if (!res.ok || !res.body) {
|
|
66
|
+
void res.body?.cancel().catch(() => { });
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if (stop.signal.aborted || ctl.signal.aborted) {
|
|
70
|
+
// A fetch that ignored the abort: release the body it handed back.
|
|
71
|
+
void res.body.cancel().catch(() => { });
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
set({ ...state, connected: true });
|
|
75
|
+
for await (const e of readSse({ body: tap(res.body, poke) })) {
|
|
76
|
+
if (stop.signal.aborted || ctl.signal.aborted)
|
|
77
|
+
break;
|
|
78
|
+
// Only a connection that delivered an event resets the backoff: a server that answers 200 and closes at
|
|
79
|
+
// once is retried with growing delays, not every `baseMs`.
|
|
80
|
+
ok = true;
|
|
81
|
+
const next = reduce(state, e);
|
|
82
|
+
set(next);
|
|
83
|
+
if (!stop.signal.aborted)
|
|
84
|
+
o.onEvent?.(e, state);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// A network error, an abort (close, reconnect, idle): all mean "reconnect unless closed".
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
clearTimeout(idle);
|
|
92
|
+
if (current === ctl)
|
|
93
|
+
current = null;
|
|
94
|
+
if (state.connected)
|
|
95
|
+
set({ ...state, connected: false });
|
|
96
|
+
}
|
|
97
|
+
return ok;
|
|
98
|
+
};
|
|
99
|
+
const done = (async () => {
|
|
100
|
+
let attempt = 0;
|
|
101
|
+
while (!stop.signal.aborted) {
|
|
102
|
+
const ok = await once();
|
|
103
|
+
if (stop.signal.aborted)
|
|
104
|
+
break;
|
|
105
|
+
if (ok)
|
|
106
|
+
attempt = 0;
|
|
107
|
+
if (!skip) {
|
|
108
|
+
await wait(Math.min(base * 2 ** attempt, max));
|
|
109
|
+
attempt++;
|
|
110
|
+
}
|
|
111
|
+
skip = false;
|
|
112
|
+
}
|
|
113
|
+
o.signal?.removeEventListener("abort", close);
|
|
114
|
+
})();
|
|
115
|
+
return {
|
|
116
|
+
get state() {
|
|
117
|
+
return state;
|
|
118
|
+
},
|
|
119
|
+
close,
|
|
120
|
+
reconnect() {
|
|
121
|
+
if (stop.signal.aborted)
|
|
122
|
+
return;
|
|
123
|
+
// Whichever phase this lands in (request, stream, between the two, backoff), the next connection starts at once.
|
|
124
|
+
skip = true;
|
|
125
|
+
current?.abort();
|
|
126
|
+
wake?.();
|
|
127
|
+
},
|
|
128
|
+
done,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/** Calls `f` on every chunk, so a stalled stream can be told apart from a quiet one. */
|
|
132
|
+
function tap(body, f) {
|
|
133
|
+
const reader = body.getReader();
|
|
134
|
+
return new ReadableStream({
|
|
135
|
+
async pull(c) {
|
|
136
|
+
const { value, done } = await reader.read();
|
|
137
|
+
if (done)
|
|
138
|
+
return c.close();
|
|
139
|
+
f();
|
|
140
|
+
c.enqueue(value);
|
|
141
|
+
},
|
|
142
|
+
cancel(why) {
|
|
143
|
+
return reader.cancel(why);
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@lightblues/jig/agent-chat`: a headless client that follows a thread's AG-UI event stream (agent-paseo's runner and
|
|
3
|
+
* follower, agent-pi) over SSE and folds it into `{ messages, live, error, connected }`. No UI, no Markdown, no store:
|
|
4
|
+
* the host renders. Browser-safe: no `node:` imports, no runtime dependency (`@ag-ui/core` for types only).
|
|
5
|
+
* React bindings: `@lightblues/jig/agent-chat/react`. See `.ea/docs/agent-chat.md`.
|
|
6
|
+
*/
|
|
7
|
+
export { readSse } from "./sse.ts";
|
|
8
|
+
export type { SseSource } from "./sse.ts";
|
|
9
|
+
export { initialState, reduce, runMessages, toolLine, toolViews } from "./reduce.ts";
|
|
10
|
+
export type { LiveActivity, LivePart, LiveRun, LiveTool, RunEnd, ThreadState } from "./reduce.ts";
|
|
11
|
+
export { followThread } from "./follow.ts";
|
|
12
|
+
export type { Follow, FollowOptions } from "./follow.ts";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@lightblues/jig/agent-chat`: a headless client that follows a thread's AG-UI event stream (agent-paseo's runner and
|
|
3
|
+
* follower, agent-pi) over SSE and folds it into `{ messages, live, error, connected }`. No UI, no Markdown, no store:
|
|
4
|
+
* the host renders. Browser-safe: no `node:` imports, no runtime dependency (`@ag-ui/core` for types only).
|
|
5
|
+
* React bindings: `@lightblues/jig/agent-chat/react`. See `.ea/docs/agent-chat.md`.
|
|
6
|
+
*/
|
|
7
|
+
export { readSse } from "./sse.js";
|
|
8
|
+
export { initialState, reduce, runMessages, toolLine, toolViews } from "./reduce.js";
|
|
9
|
+
export { followThread } from "./follow.js";
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@lightblues/jig/agent-chat/react`: React bindings for `agent-chat`. They render nothing, so they work in React DOM
|
|
3
|
+
* and React Native; `useStickToBottom` needs a DOM element and does nothing without one.
|
|
4
|
+
*/
|
|
5
|
+
import { type RefObject } from "react";
|
|
6
|
+
import type { BaseEvent } from "@ag-ui/core";
|
|
7
|
+
import { type FollowOptions } from "./follow.ts";
|
|
8
|
+
import { type ThreadState } from "./reduce.ts";
|
|
9
|
+
export interface UseThreadOptions<TSend extends unknown[] = [string], TSent = unknown> {
|
|
10
|
+
/** The thread's SSE route; `null` follows nothing (no thread yet). A new URL starts over from `initialState`. */
|
|
11
|
+
streamUrl: string | null;
|
|
12
|
+
/** The host's "post a message" call. `useThread` only wraps it to track `sending`. */
|
|
13
|
+
send?: (...args: TSend) => Promise<TSent>;
|
|
14
|
+
/** The host's "stop the run" call. */
|
|
15
|
+
stop?: () => Promise<unknown>;
|
|
16
|
+
/** Each folded event: refetch the host's thread on `RUN_FINISHED` / `RUN_ERROR` / `READY`, for example. */
|
|
17
|
+
onEvent?: (event: BaseEvent, state: ThreadState) => void;
|
|
18
|
+
fetch?: FollowOptions["fetch"];
|
|
19
|
+
headers?: Record<string, string>;
|
|
20
|
+
credentials?: FollowOptions["credentials"];
|
|
21
|
+
baseMs?: number;
|
|
22
|
+
maxMs?: number;
|
|
23
|
+
idleMs?: number;
|
|
24
|
+
}
|
|
25
|
+
export interface UseThread<TSend extends unknown[], TSent> extends ThreadState {
|
|
26
|
+
/** Calls the host's `send`; rejects as it does. */
|
|
27
|
+
send: (...args: TSend) => Promise<TSent>;
|
|
28
|
+
/** Calls the host's `stop`. */
|
|
29
|
+
stop: () => Promise<void>;
|
|
30
|
+
/** A `send` is in flight. */
|
|
31
|
+
sending: boolean;
|
|
32
|
+
/** Reconnect now (after the host's own retry, or when the app comes back to the foreground). */
|
|
33
|
+
reconnect: () => void;
|
|
34
|
+
}
|
|
35
|
+
/** Follows one thread's stream while mounted; the state is `agent-chat`'s `ThreadState`. */
|
|
36
|
+
export declare function useThread<TSend extends unknown[] = [string], TSent = unknown>(o: UseThreadOptions<TSend, TSent>): UseThread<TSend, TSent>;
|
|
37
|
+
/** What `useStickToBottom` needs from the scroller (a DOM element). */
|
|
38
|
+
interface Scroller {
|
|
39
|
+
scrollTop: number;
|
|
40
|
+
scrollHeight: number;
|
|
41
|
+
clientHeight: number;
|
|
42
|
+
addEventListener?: (type: "scroll", f: () => void, o?: {
|
|
43
|
+
passive?: boolean;
|
|
44
|
+
}) => void;
|
|
45
|
+
removeEventListener?: (type: "scroll", f: () => void) => void;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Keeps a scroller at its end while content grows, unless the reader scrolled up (more than `slack` px from the end).
|
|
49
|
+
* `deps` are what grows (`[messages.length, live?.text.length, live?.tools.length]`). Returns `pinned` (a ref, no
|
|
50
|
+
* re-render on scroll) and `toBottom()` to jump to the end and pin again (after the user sends). Without a DOM
|
|
51
|
+
* element (React Native: use `ScrollView.onContentSizeChange` instead) it does nothing.
|
|
52
|
+
*/
|
|
53
|
+
export declare function useStickToBottom(ref: RefObject<Scroller | null>, deps: readonly unknown[], slack?: number): {
|
|
54
|
+
pinned: RefObject<boolean>;
|
|
55
|
+
toBottom: () => void;
|
|
56
|
+
};
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@lightblues/jig/agent-chat/react`: React bindings for `agent-chat`. They render nothing, so they work in React DOM
|
|
3
|
+
* and React Native; `useStickToBottom` needs a DOM element and does nothing without one.
|
|
4
|
+
*/
|
|
5
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
6
|
+
import { followThread } from "./follow.js";
|
|
7
|
+
import { initialState } from "./reduce.js";
|
|
8
|
+
/** Follows one thread's stream while mounted; the state is `agent-chat`'s `ThreadState`. */
|
|
9
|
+
export function useThread(o) {
|
|
10
|
+
const [state, setState] = useState(initialState);
|
|
11
|
+
const [sending, setSending] = useState(0);
|
|
12
|
+
const opts = useRef(o);
|
|
13
|
+
opts.current = o;
|
|
14
|
+
const follow = useRef(null);
|
|
15
|
+
const { streamUrl } = o;
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
setState(initialState);
|
|
18
|
+
if (streamUrl === null)
|
|
19
|
+
return;
|
|
20
|
+
const ctl = new AbortController();
|
|
21
|
+
const f = followThread(streamUrl, {
|
|
22
|
+
signal: ctl.signal,
|
|
23
|
+
fetch: opts.current.fetch,
|
|
24
|
+
headers: opts.current.headers,
|
|
25
|
+
credentials: opts.current.credentials,
|
|
26
|
+
baseMs: opts.current.baseMs,
|
|
27
|
+
maxMs: opts.current.maxMs,
|
|
28
|
+
idleMs: opts.current.idleMs,
|
|
29
|
+
onState: setState,
|
|
30
|
+
onEvent: (e, s) => opts.current.onEvent?.(e, s),
|
|
31
|
+
});
|
|
32
|
+
follow.current = f;
|
|
33
|
+
return () => {
|
|
34
|
+
ctl.abort();
|
|
35
|
+
if (follow.current === f)
|
|
36
|
+
follow.current = null;
|
|
37
|
+
};
|
|
38
|
+
}, [streamUrl]);
|
|
39
|
+
const send = useCallback(async (...args) => {
|
|
40
|
+
const f = opts.current.send;
|
|
41
|
+
if (!f)
|
|
42
|
+
throw new Error("useThread: no send");
|
|
43
|
+
setSending((n) => n + 1);
|
|
44
|
+
try {
|
|
45
|
+
return await f(...args);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
setSending((n) => n - 1);
|
|
49
|
+
}
|
|
50
|
+
}, []);
|
|
51
|
+
const stop = useCallback(async () => {
|
|
52
|
+
await opts.current.stop?.();
|
|
53
|
+
}, []);
|
|
54
|
+
const reconnect = useCallback(() => follow.current?.reconnect(), []);
|
|
55
|
+
return { ...state, send, stop, sending: sending > 0, reconnect };
|
|
56
|
+
}
|
|
57
|
+
/** `useLayoutEffect` where there is a DOM (no flash of the old scroll position), `useEffect` elsewhere (SSR, React Native). */
|
|
58
|
+
const useLayout = typeof document === "undefined" ? useEffect : useLayoutEffect;
|
|
59
|
+
/**
|
|
60
|
+
* Keeps a scroller at its end while content grows, unless the reader scrolled up (more than `slack` px from the end).
|
|
61
|
+
* `deps` are what grows (`[messages.length, live?.text.length, live?.tools.length]`). Returns `pinned` (a ref, no
|
|
62
|
+
* re-render on scroll) and `toBottom()` to jump to the end and pin again (after the user sends). Without a DOM
|
|
63
|
+
* element (React Native: use `ScrollView.onContentSizeChange` instead) it does nothing.
|
|
64
|
+
*/
|
|
65
|
+
export function useStickToBottom(ref, deps, slack = 80) {
|
|
66
|
+
const pinned = useRef(true);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
const el = ref.current;
|
|
69
|
+
if (!el?.addEventListener)
|
|
70
|
+
return;
|
|
71
|
+
const on = () => {
|
|
72
|
+
pinned.current = el.scrollHeight - el.scrollTop - el.clientHeight < slack;
|
|
73
|
+
};
|
|
74
|
+
el.addEventListener("scroll", on, { passive: true });
|
|
75
|
+
return () => el.removeEventListener?.("scroll", on);
|
|
76
|
+
}, [ref, slack]);
|
|
77
|
+
useLayout(() => {
|
|
78
|
+
const el = ref.current;
|
|
79
|
+
if (!pinned.current || !el?.addEventListener)
|
|
80
|
+
return;
|
|
81
|
+
el.scrollTop = el.scrollHeight;
|
|
82
|
+
}, deps);
|
|
83
|
+
const toBottom = useCallback(() => {
|
|
84
|
+
const el = ref.current;
|
|
85
|
+
pinned.current = true;
|
|
86
|
+
if (el?.addEventListener)
|
|
87
|
+
el.scrollTop = el.scrollHeight;
|
|
88
|
+
}, [ref]);
|
|
89
|
+
return { pinned, toBottom };
|
|
90
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { BaseEvent, Message } from "@ag-ui/core";
|
|
2
|
+
/** A tool call as a follower shows it: never its output. */
|
|
3
|
+
export interface LiveTool {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
/** `metadata.summary` of `TOOL_CALL_START`, or `summary` of a `TOOL_CALL_ARGS` delta that is a JSON object (agent-paseo sends each detail whole). */
|
|
7
|
+
summary: string | null;
|
|
8
|
+
/** The `TOOL_CALL_ARGS` deltas joined, as AG-UI defines them. */
|
|
9
|
+
args: string;
|
|
10
|
+
/** `metadata.status` of `TOOL_CALL_END` (`completed`, `failed`, `canceled`), or `failed` / `completed` from `metadata.isError`; `running` before the end. */
|
|
11
|
+
status: string;
|
|
12
|
+
done: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** An `ACTIVITY_SNAPSHOT` (a card): the host draws it by `activityType`. */
|
|
15
|
+
export interface LiveActivity {
|
|
16
|
+
id: string;
|
|
17
|
+
activityType: string;
|
|
18
|
+
content: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
/** The run's parts in the order they came, for hosts that interleave text and tools. */
|
|
21
|
+
export type LivePart = {
|
|
22
|
+
kind: "text";
|
|
23
|
+
id: string;
|
|
24
|
+
text: string;
|
|
25
|
+
} | {
|
|
26
|
+
kind: "reasoning";
|
|
27
|
+
id: string;
|
|
28
|
+
text: string;
|
|
29
|
+
} | {
|
|
30
|
+
kind: "tool";
|
|
31
|
+
id: string;
|
|
32
|
+
} | {
|
|
33
|
+
kind: "activity";
|
|
34
|
+
id: string;
|
|
35
|
+
};
|
|
36
|
+
/** The run in progress. */
|
|
37
|
+
export interface LiveRun {
|
|
38
|
+
runId: string;
|
|
39
|
+
threadId: string;
|
|
40
|
+
/** `RUN_STARTED.metadata` (assay: `{message}`, the stored user message's id). */
|
|
41
|
+
metadata: Record<string, unknown> | null;
|
|
42
|
+
/** The run's user message, when the stream carries it (`TEXT_MESSAGE_*` with `role: "user"`: agent-paseo's `followAgent`). */
|
|
43
|
+
user: {
|
|
44
|
+
id: string;
|
|
45
|
+
text: string;
|
|
46
|
+
} | null;
|
|
47
|
+
/** The assistant texts joined with a blank line. */
|
|
48
|
+
text: string;
|
|
49
|
+
/** The reasoning texts joined with a blank line. */
|
|
50
|
+
reasoning: string;
|
|
51
|
+
tools: LiveTool[];
|
|
52
|
+
activities: LiveActivity[];
|
|
53
|
+
parts: LivePart[];
|
|
54
|
+
}
|
|
55
|
+
/** How the last run ended. `unknown`: it ended while the stream was down (`READY {running:false}` replaced it); refetch. */
|
|
56
|
+
export interface RunEnd {
|
|
57
|
+
runId: string;
|
|
58
|
+
status: "done" | "stopped" | "error" | "unknown";
|
|
59
|
+
/** `RUN_FINISHED.result` (agent-paseo's runner: what the host's `saveAnswer` returned). */
|
|
60
|
+
result?: unknown;
|
|
61
|
+
error?: string;
|
|
62
|
+
}
|
|
63
|
+
export interface ThreadState {
|
|
64
|
+
/**
|
|
65
|
+
* The thread's messages, when the stream carries them: `MESSAGES_SNAPSHOT` replaces them, and from then on each
|
|
66
|
+
* finished run is appended (user message, texts, tool calls grouped as agent-paseo's snapshot groups them). A stream
|
|
67
|
+
* without snapshots (agent-paseo's runner) leaves this empty: the host owns the history there.
|
|
68
|
+
*/
|
|
69
|
+
messages: Message[];
|
|
70
|
+
live: LiveRun | null;
|
|
71
|
+
/** The last run's error (`RUN_ERROR`); cleared when a run starts. A stored answer of a failed turn has it in `last.error`. */
|
|
72
|
+
error: string | null;
|
|
73
|
+
/** The stream is connected (set by `followThread`, not by events). */
|
|
74
|
+
connected: boolean;
|
|
75
|
+
last: RunEnd | null;
|
|
76
|
+
/** A `MESSAGES_SNAPSHOT` has been seen. */
|
|
77
|
+
history: boolean;
|
|
78
|
+
}
|
|
79
|
+
export declare const initialState: ThreadState;
|
|
80
|
+
/**
|
|
81
|
+
* Folds one AG-UI event into the thread state, without changing `state`. Follows the follower contract of
|
|
82
|
+
* agent-paseo and agent-pi: `CUSTOM READY` drops the run being drawn (the stream resyncs: `RUN_STARTED` and a replay
|
|
83
|
+
* follow when one is running), so a reconnect or a server-side gap never duplicates text. Unknown events return
|
|
84
|
+
* `state` unchanged.
|
|
85
|
+
*/
|
|
86
|
+
export declare function reduce(state: ThreadState, event: BaseEvent): ThreadState;
|
|
87
|
+
/** A finished run as AG-UI messages, in agent-paseo's snapshot shape: tool calls grouped in an assistant message, `arguments` = JSON `{summary, status}`. */
|
|
88
|
+
export declare function runMessages(live: LiveRun): Message[];
|
|
89
|
+
/**
|
|
90
|
+
* The tool calls of a message, or of a run's tools: `{name, summary, status}` from `toolCalls[].function.arguments`
|
|
91
|
+
* (agent-paseo's snapshot shape: JSON `{summary, status}`).
|
|
92
|
+
*/
|
|
93
|
+
export declare function toolViews(m: Message): {
|
|
94
|
+
id: string;
|
|
95
|
+
name: string;
|
|
96
|
+
summary: string | null;
|
|
97
|
+
status: string | null;
|
|
98
|
+
}[];
|
|
99
|
+
/** One line for a run of tool calls: the count and the last call (`3 steps · Read notes.md`). */
|
|
100
|
+
export declare function toolLine(tools: {
|
|
101
|
+
name: string;
|
|
102
|
+
summary: string | null;
|
|
103
|
+
}[]): string;
|