@assistant-ui/react-ag-ui 0.0.57 → 0.0.59
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 +16 -0
- package/dist/index.d.ts +2 -2
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts +19 -8
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts.map +1 -1
- package/dist/runtime/AgUiThreadRuntimeCore.js +118 -86
- package/dist/runtime/AgUiThreadRuntimeCore.js.map +1 -1
- package/dist/runtime/adapter/conversions.d.ts.map +1 -1
- package/dist/runtime/adapter/conversions.js +34 -11
- package/dist/runtime/adapter/conversions.js.map +1 -1
- package/dist/runtime/adapter/run-aggregator.d.ts +13 -3
- package/dist/runtime/adapter/run-aggregator.d.ts.map +1 -1
- package/dist/runtime/adapter/run-aggregator.js +385 -119
- package/dist/runtime/adapter/run-aggregator.js.map +1 -1
- package/dist/runtime/adapter/subscriber.d.ts +9 -0
- package/dist/runtime/adapter/subscriber.d.ts.map +1 -1
- package/dist/runtime/adapter/subscriber.js +4 -1
- package/dist/runtime/adapter/subscriber.js.map +1 -1
- package/dist/runtime/adapter/tool-approval.d.ts +3 -1
- package/dist/runtime/adapter/tool-approval.d.ts.map +1 -1
- package/dist/runtime/adapter/tool-approval.js +11 -12
- package/dist/runtime/adapter/tool-approval.js.map +1 -1
- package/dist/runtime/event-parser.d.ts.map +1 -1
- package/dist/runtime/event-parser.js +176 -45
- package/dist/runtime/event-parser.js.map +1 -1
- package/dist/runtime/types.d.ts +67 -1
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/useAgUiRuntime.d.ts.map +1 -1
- package/dist/useAgUiRuntime.js +15 -5
- package/dist/useAgUiRuntime.js.map +1 -1
- package/package.json +9 -9
- package/src/index.ts +1 -0
- package/src/runtime/AgUiThreadRuntimeCore.test.ts +18 -0
- package/src/runtime/AgUiThreadRuntimeCore.ts +204 -120
- package/src/runtime/adapter/conversions.ts +87 -17
- package/src/runtime/adapter/run-aggregator.approval.test.ts +128 -0
- package/src/runtime/adapter/run-aggregator.ts +595 -139
- package/src/runtime/adapter/subscriber.ts +10 -1
- package/src/runtime/adapter/tool-approval.test.ts +75 -0
- package/src/runtime/adapter/tool-approval.ts +17 -12
- package/src/runtime/event-parser.ts +216 -36
- package/src/runtime/types.ts +96 -12
- package/src/useAgUiRuntime.approval.test.tsx +34 -24
- package/src/useAgUiRuntime.queue.test.tsx +54 -0
- package/src/useAgUiRuntime.thread-switch.test.tsx +199 -0
- package/src/useAgUiRuntime.toolDeferral.test.tsx +186 -0
- package/src/useAgUiRuntime.ts +15 -3
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
4
|
+
import { act, cleanup, render, renderHook } from "@testing-library/react";
|
|
5
|
+
import type { AssistantRuntime, ToolCallMessagePart } from "@assistant-ui/core";
|
|
6
|
+
import {
|
|
7
|
+
AssistantRuntimeProvider,
|
|
8
|
+
useAssistantTool,
|
|
9
|
+
} from "@assistant-ui/core/react";
|
|
10
|
+
import type { HttpAgent } from "@ag-ui/client";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import { useAgUiRuntime } from "./useAgUiRuntime";
|
|
13
|
+
import type { AgUiInterrupt } from "./runtime/types";
|
|
14
|
+
|
|
15
|
+
type Subscriber = Record<string, ((payload: any) => void) | undefined>;
|
|
16
|
+
|
|
17
|
+
const GATE: AgUiInterrupt = {
|
|
18
|
+
id: "int-1",
|
|
19
|
+
reason: "tool_call",
|
|
20
|
+
toolCallId: "tc-1",
|
|
21
|
+
message: "Delete /tmp/a?",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const toolCalls = (runtime: AssistantRuntime): ToolCallMessagePart[] =>
|
|
25
|
+
runtime.thread
|
|
26
|
+
.getState()
|
|
27
|
+
.messages.flatMap((message) => message.content as readonly unknown[])
|
|
28
|
+
.filter(
|
|
29
|
+
(part): part is ToolCallMessagePart =>
|
|
30
|
+
(part as ToolCallMessagePart).type === "tool-call",
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const flush = () =>
|
|
34
|
+
act(async () => void (await new Promise((r) => setTimeout(r, 0))));
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Holds the run open after the tool call's arguments complete, so the window
|
|
38
|
+
* between complete args and the run's outcome is observable as its own render
|
|
39
|
+
* rather than coalescing with the tail.
|
|
40
|
+
*/
|
|
41
|
+
const renderHeldRun = async (tail: (subscriber: Subscriber) => void) => {
|
|
42
|
+
let releaseTail!: () => void;
|
|
43
|
+
const held = new Promise<void>((r) => (releaseTail = r));
|
|
44
|
+
let argsDelivered!: () => void;
|
|
45
|
+
const argsDeliveredP = new Promise<void>((r) => (argsDelivered = r));
|
|
46
|
+
|
|
47
|
+
const runAgent = vi.fn(async (_input: unknown, subscriber: Subscriber) => {
|
|
48
|
+
if (runAgent.mock.calls.length > 1) {
|
|
49
|
+
subscriber.onRunFinalized?.(undefined);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
subscriber.onToolCallStartEvent?.({
|
|
53
|
+
event: {
|
|
54
|
+
type: "TOOL_CALL_START",
|
|
55
|
+
toolCallId: "tc-1",
|
|
56
|
+
toolCallName: "delete_file",
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
subscriber.onToolCallArgsEvent?.({
|
|
60
|
+
event: {
|
|
61
|
+
type: "TOOL_CALL_ARGS",
|
|
62
|
+
toolCallId: "tc-1",
|
|
63
|
+
delta: '{"path":"/tmp/a"}',
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
subscriber.onToolCallEndEvent?.({
|
|
67
|
+
event: { type: "TOOL_CALL_END", toolCallId: "tc-1" },
|
|
68
|
+
});
|
|
69
|
+
argsDelivered();
|
|
70
|
+
await held;
|
|
71
|
+
tail(subscriber);
|
|
72
|
+
subscriber.onRunFinalized?.(undefined);
|
|
73
|
+
});
|
|
74
|
+
const agent = { runAgent, abortRun: vi.fn() } as unknown as HttpAgent;
|
|
75
|
+
|
|
76
|
+
const execute = vi.fn(async () => ({ deleted: true }));
|
|
77
|
+
const DeleteFileTool = () => {
|
|
78
|
+
useAssistantTool({
|
|
79
|
+
toolName: "delete_file",
|
|
80
|
+
description: "delete a file",
|
|
81
|
+
parameters: z.object({ path: z.string() }),
|
|
82
|
+
execute,
|
|
83
|
+
});
|
|
84
|
+
return null;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const { result } = renderHook(() => useAgUiRuntime({ agent }));
|
|
88
|
+
render(
|
|
89
|
+
<AssistantRuntimeProvider runtime={result.current}>
|
|
90
|
+
<DeleteFileTool />
|
|
91
|
+
</AssistantRuntimeProvider>,
|
|
92
|
+
);
|
|
93
|
+
await flush();
|
|
94
|
+
|
|
95
|
+
await act(async () => {
|
|
96
|
+
void result.current.thread.append({
|
|
97
|
+
role: "user",
|
|
98
|
+
content: [{ type: "text", text: "delete it" }],
|
|
99
|
+
});
|
|
100
|
+
await argsDeliveredP;
|
|
101
|
+
});
|
|
102
|
+
await flush();
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
runtime: result,
|
|
106
|
+
execute,
|
|
107
|
+
settle: async () => {
|
|
108
|
+
await act(async () => {
|
|
109
|
+
releaseTail();
|
|
110
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
111
|
+
});
|
|
112
|
+
await flush();
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
afterEach(() => cleanup());
|
|
118
|
+
|
|
119
|
+
describe("useAgUiRuntime frontend tool deferral", () => {
|
|
120
|
+
it("does not run a frontend execute on a call the provider is about to gate", async () => {
|
|
121
|
+
const { runtime, execute, settle } = await renderHeldRun((subscriber) => {
|
|
122
|
+
subscriber.onRunFinishedEvent?.({
|
|
123
|
+
event: {
|
|
124
|
+
type: "RUN_FINISHED",
|
|
125
|
+
runId: "run-1",
|
|
126
|
+
outcome: { type: "interrupt", interrupts: [GATE] },
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const inWindow = toolCalls(runtime.current)[0]!;
|
|
132
|
+
expect(inWindow.argsText).toBe('{"path":"/tmp/a"}');
|
|
133
|
+
expect(inWindow.approval).toBeUndefined();
|
|
134
|
+
expect(inWindow.result).toBeUndefined();
|
|
135
|
+
expect(execute).not.toHaveBeenCalled();
|
|
136
|
+
|
|
137
|
+
await settle();
|
|
138
|
+
|
|
139
|
+
const gated = toolCalls(runtime.current)[0]!;
|
|
140
|
+
expect(gated.approval).toEqual({
|
|
141
|
+
id: "int-1",
|
|
142
|
+
prompt: "Delete /tmp/a?",
|
|
143
|
+
});
|
|
144
|
+
expect(gated.result).toBeUndefined();
|
|
145
|
+
expect(execute).not.toHaveBeenCalled();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("does not run a frontend execute on a call the agent answers itself", async () => {
|
|
149
|
+
const { runtime, execute, settle } = await renderHeldRun((subscriber) => {
|
|
150
|
+
subscriber.onToolCallResultEvent?.({
|
|
151
|
+
event: {
|
|
152
|
+
type: "TOOL_CALL_RESULT",
|
|
153
|
+
messageId: "m-result",
|
|
154
|
+
toolCallId: "tc-1",
|
|
155
|
+
content: "server did the delete",
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
subscriber.onRunFinishedEvent?.({
|
|
159
|
+
event: { type: "RUN_FINISHED", runId: "run-1" },
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
expect(toolCalls(runtime.current)[0]!.result).toBeUndefined();
|
|
164
|
+
expect(execute).not.toHaveBeenCalled();
|
|
165
|
+
|
|
166
|
+
await settle();
|
|
167
|
+
|
|
168
|
+
expect(toolCalls(runtime.current)[0]!.result).toBe("server did the delete");
|
|
169
|
+
expect(execute).not.toHaveBeenCalled();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("runs a frontend execute once the run settles with no outcome for the call", async () => {
|
|
173
|
+
const { runtime, execute, settle } = await renderHeldRun((subscriber) => {
|
|
174
|
+
subscriber.onRunFinishedEvent?.({
|
|
175
|
+
event: { type: "RUN_FINISHED", runId: "run-1" },
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
expect(execute).not.toHaveBeenCalled();
|
|
180
|
+
|
|
181
|
+
await settle();
|
|
182
|
+
|
|
183
|
+
expect(execute).toHaveBeenCalledTimes(1);
|
|
184
|
+
expect(toolCalls(runtime.current)[0]!.result).toEqual({ deleted: true });
|
|
185
|
+
});
|
|
186
|
+
});
|
package/src/useAgUiRuntime.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import {
|
|
4
4
|
useCallback,
|
|
5
5
|
useEffect,
|
|
6
|
+
useLayoutEffect,
|
|
6
7
|
useMemo,
|
|
7
8
|
useRef,
|
|
8
9
|
useState,
|
|
@@ -62,6 +63,7 @@ export function useAgUiRuntime(
|
|
|
62
63
|
const [_version, setVersion] = useState(0);
|
|
63
64
|
const notifyUpdate = useCallback(() => setVersion((v) => v + 1), []);
|
|
64
65
|
const coreRef = useRef<AgUiThreadRuntimeCore | null>(null);
|
|
66
|
+
const threadSwitchGenerationRef = useRef(0);
|
|
65
67
|
const runtimeAdapters = useRuntimeAdapters();
|
|
66
68
|
|
|
67
69
|
const historyAdapter = options.adapters?.history ?? runtimeAdapters?.history;
|
|
@@ -72,6 +74,7 @@ export function useAgUiRuntime(
|
|
|
72
74
|
agent: options.agent,
|
|
73
75
|
logger,
|
|
74
76
|
showThinking: options.showThinking ?? true,
|
|
77
|
+
resumeTranscript: options.resumeTranscript,
|
|
75
78
|
autoCancelPendingToolCalls: options.autoCancelPendingToolCalls,
|
|
76
79
|
...(options.onError && { onError: options.onError }),
|
|
77
80
|
...(options.onCancel && { onCancel: options.onCancel }),
|
|
@@ -86,6 +89,7 @@ export function useAgUiRuntime(
|
|
|
86
89
|
agent: options.agent,
|
|
87
90
|
logger,
|
|
88
91
|
showThinking: options.showThinking ?? true,
|
|
92
|
+
resumeTranscript: options.resumeTranscript,
|
|
89
93
|
autoCancelPendingToolCalls: options.autoCancelPendingToolCalls,
|
|
90
94
|
...(options.onError && { onError: options.onError }),
|
|
91
95
|
...(options.onCancel && { onCancel: options.onCancel }),
|
|
@@ -136,13 +140,16 @@ export function useAgUiRuntime(
|
|
|
136
140
|
});
|
|
137
141
|
},
|
|
138
142
|
});
|
|
139
|
-
} else if (!options.unstable_enableMessageQueue && queueRef.current) {
|
|
140
|
-
queueRef.current.clear();
|
|
141
|
-
queueRef.current = null;
|
|
142
143
|
}
|
|
143
144
|
const queueController = options.unstable_enableMessageQueue
|
|
144
145
|
? queueRef.current
|
|
145
146
|
: null;
|
|
147
|
+
useLayoutEffect(() => {
|
|
148
|
+
if (options.unstable_enableMessageQueue || !queueRef.current) return;
|
|
149
|
+
const controller = queueRef.current;
|
|
150
|
+
queueRef.current = null;
|
|
151
|
+
controller.clear();
|
|
152
|
+
}, [options.unstable_enableMessageQueue]);
|
|
146
153
|
|
|
147
154
|
// Feeds the store memo below: the runtime core skips an adapter whose
|
|
148
155
|
// identity is unchanged, so queue items have to move the store reference or
|
|
@@ -182,17 +189,21 @@ export function useAgUiRuntime(
|
|
|
182
189
|
...rest,
|
|
183
190
|
onSwitchToNewThread: onSwitchToNewThread
|
|
184
191
|
? async () => {
|
|
192
|
+
const generation = ++threadSwitchGenerationRef.current;
|
|
185
193
|
await onSwitchToNewThread();
|
|
194
|
+
if (generation !== threadSwitchGenerationRef.current) return;
|
|
186
195
|
core.applyExternalMessages([]);
|
|
187
196
|
core.resetState();
|
|
188
197
|
}
|
|
189
198
|
: undefined,
|
|
190
199
|
onSwitchToThread: onSwitchToThread
|
|
191
200
|
? async (threadId: string) => {
|
|
201
|
+
const generation = ++threadSwitchGenerationRef.current;
|
|
192
202
|
// Clear before the thread id flips, or the old messages leak
|
|
193
203
|
// into the new thread as a sibling branch.
|
|
194
204
|
core.applyExternalMessages([]);
|
|
195
205
|
const result = await onSwitchToThread(threadId);
|
|
206
|
+
if (generation !== threadSwitchGenerationRef.current) return;
|
|
196
207
|
core.applyExternalMessages(result.messages);
|
|
197
208
|
if (result.state !== undefined) {
|
|
198
209
|
core.loadExternalState(result.state);
|
|
@@ -260,6 +271,7 @@ export function useAgUiRuntime(
|
|
|
260
271
|
onRespondToToolApproval: (options) =>
|
|
261
272
|
core.respondToToolApproval(options).catch((error: unknown) => {
|
|
262
273
|
core.reportError(error);
|
|
274
|
+
throw error;
|
|
263
275
|
}),
|
|
264
276
|
onResume: (config) => core.resume(config),
|
|
265
277
|
setMessages: (messages: readonly ThreadMessage[]) =>
|