@assistant-ui/react-ag-ui 0.0.54 → 0.0.57
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/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts +25 -15
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts.map +1 -1
- package/dist/runtime/AgUiThreadRuntimeCore.js +257 -210
- package/dist/runtime/AgUiThreadRuntimeCore.js.map +1 -1
- package/dist/runtime/adapter/conversions.d.ts +13 -3
- package/dist/runtime/adapter/conversions.d.ts.map +1 -1
- package/dist/runtime/adapter/conversions.js +77 -39
- package/dist/runtime/adapter/conversions.js.map +1 -1
- package/dist/runtime/adapter/run-aggregator.d.ts +23 -2
- package/dist/runtime/adapter/run-aggregator.d.ts.map +1 -1
- package/dist/runtime/adapter/run-aggregator.js +155 -41
- package/dist/runtime/adapter/run-aggregator.js.map +1 -1
- package/dist/runtime/adapter/subscriber.d.ts +3 -0
- package/dist/runtime/adapter/subscriber.d.ts.map +1 -1
- package/dist/runtime/adapter/subscriber.js +9 -0
- package/dist/runtime/adapter/subscriber.js.map +1 -1
- package/dist/runtime/adapter/tool-approval.d.ts +56 -0
- package/dist/runtime/adapter/tool-approval.d.ts.map +1 -0
- package/dist/runtime/adapter/tool-approval.js +244 -0
- package/dist/runtime/adapter/tool-approval.js.map +1 -0
- package/dist/runtime/event-parser.d.ts.map +1 -1
- package/dist/runtime/event-parser.js +2 -1
- package/dist/runtime/event-parser.js.map +1 -1
- package/dist/runtime/interrupt-internals.d.ts +8 -0
- package/dist/runtime/interrupt-internals.d.ts.map +1 -0
- package/dist/runtime/interrupt-internals.js +25 -0
- package/dist/runtime/interrupt-internals.js.map +1 -0
- package/dist/useAgUiRuntime.js +14 -9
- package/dist/useAgUiRuntime.js.map +1 -1
- package/package.json +9 -9
- package/src/index.ts +8 -2
- package/src/runtime/AgUiThreadRuntimeCore.test.ts +105 -0
- package/src/runtime/AgUiThreadRuntimeCore.ts +436 -272
- package/src/runtime/adapter/conversions.content.test.ts +61 -3
- package/src/runtime/adapter/conversions.ts +123 -57
- package/src/runtime/adapter/run-aggregator.approval.test.ts +103 -0
- package/src/runtime/adapter/run-aggregator.ts +234 -47
- package/src/runtime/adapter/subscriber.ts +14 -0
- package/src/runtime/adapter/tool-approval.test.ts +681 -0
- package/src/runtime/adapter/tool-approval.ts +366 -0
- package/src/runtime/event-parser.ts +10 -1
- package/src/runtime/interrupt-internals.ts +35 -0
- package/src/useAgUiRuntime.approval.test.tsx +885 -0
- package/src/useAgUiRuntime.history.test.tsx +76 -0
- package/src/useAgUiRuntime.ts +15 -9
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
4
|
+
import { cleanup, renderHook, waitFor } from "@testing-library/react";
|
|
5
|
+
import type { ThreadHistoryAdapter } from "@assistant-ui/core";
|
|
6
|
+
import type { HttpAgent } from "@ag-ui/client";
|
|
7
|
+
import { useAgUiRuntime } from "./useAgUiRuntime";
|
|
8
|
+
|
|
9
|
+
const agent = { runAgent: vi.fn(), abortRun: vi.fn() } as unknown as HttpAgent;
|
|
10
|
+
|
|
11
|
+
function createHistory(): ThreadHistoryAdapter {
|
|
12
|
+
return {
|
|
13
|
+
load: vi.fn().mockResolvedValue({
|
|
14
|
+
headId: "restored",
|
|
15
|
+
messages: [
|
|
16
|
+
{
|
|
17
|
+
parentId: null,
|
|
18
|
+
message: {
|
|
19
|
+
id: "restored",
|
|
20
|
+
role: "user" as const,
|
|
21
|
+
content: [{ type: "text" as const, text: "hello" }],
|
|
22
|
+
createdAt: new Date(0),
|
|
23
|
+
metadata: { custom: {} },
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
}),
|
|
28
|
+
append: vi.fn().mockResolvedValue(undefined),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
cleanup();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("useAgUiRuntime history", () => {
|
|
37
|
+
it("loads a history adapter that arrives on a later render", async () => {
|
|
38
|
+
const history = createHistory();
|
|
39
|
+
const { result, rerender } = renderHook(
|
|
40
|
+
({ history }: { history?: ThreadHistoryAdapter }) =>
|
|
41
|
+
useAgUiRuntime({ agent, adapters: history ? { history } : {} }),
|
|
42
|
+
{ initialProps: {} },
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
await waitFor(() =>
|
|
46
|
+
expect(result.current.thread.getState().isLoading).toBe(false),
|
|
47
|
+
);
|
|
48
|
+
expect(history.load).not.toHaveBeenCalled();
|
|
49
|
+
expect(result.current.thread.getState().messages).toEqual([]);
|
|
50
|
+
|
|
51
|
+
rerender({ history });
|
|
52
|
+
|
|
53
|
+
await waitFor(() =>
|
|
54
|
+
expect(
|
|
55
|
+
result.current.thread.getState().messages.map((m) => m.id),
|
|
56
|
+
).toEqual(["restored"]),
|
|
57
|
+
);
|
|
58
|
+
expect(history.load).toHaveBeenCalledOnce();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("loads once when the adapter is present from the first render", async () => {
|
|
62
|
+
const history = createHistory();
|
|
63
|
+
const { result, rerender } = renderHook(() =>
|
|
64
|
+
useAgUiRuntime({ agent, adapters: { history } }),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
await waitFor(() =>
|
|
68
|
+
expect(
|
|
69
|
+
result.current.thread.getState().messages.map((m) => m.id),
|
|
70
|
+
).toEqual(["restored"]),
|
|
71
|
+
);
|
|
72
|
+
rerender();
|
|
73
|
+
rerender();
|
|
74
|
+
expect(history.load).toHaveBeenCalledOnce();
|
|
75
|
+
});
|
|
76
|
+
});
|
package/src/useAgUiRuntime.ts
CHANGED
|
@@ -81,14 +81,16 @@ export function useAgUiRuntime(
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
const core = coreRef.current;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
core.updateOptions({
|
|
86
|
+
agent: options.agent,
|
|
87
|
+
logger,
|
|
88
|
+
showThinking: options.showThinking ?? true,
|
|
89
|
+
autoCancelPendingToolCalls: options.autoCancelPendingToolCalls,
|
|
90
|
+
...(options.onError && { onError: options.onError }),
|
|
91
|
+
...(options.onCancel && { onCancel: options.onCancel }),
|
|
92
|
+
...(historyAdapter && { history: historyAdapter }),
|
|
93
|
+
});
|
|
92
94
|
});
|
|
93
95
|
|
|
94
96
|
const [toolStatuses, setToolStatuses] = useState<
|
|
@@ -228,7 +230,7 @@ export function useAgUiRuntime(
|
|
|
228
230
|
isLoading: core.isLoading,
|
|
229
231
|
messageRepository: core.getMessageRepository(),
|
|
230
232
|
state: core.getState(),
|
|
231
|
-
isRunning,
|
|
233
|
+
isRunning: core.isRunning(),
|
|
232
234
|
extras: agUiExtras.provide({
|
|
233
235
|
interrupts:
|
|
234
236
|
core.getPendingInterrupts()?.interrupts ?? EMPTY_INTERRUPTS,
|
|
@@ -255,6 +257,10 @@ export function useAgUiRuntime(
|
|
|
255
257
|
core.cancel();
|
|
256
258
|
},
|
|
257
259
|
onAddToolResult: (options) => core.addToolResult(options),
|
|
260
|
+
onRespondToToolApproval: (options) =>
|
|
261
|
+
core.respondToToolApproval(options).catch((error: unknown) => {
|
|
262
|
+
core.reportError(error);
|
|
263
|
+
}),
|
|
258
264
|
onResume: (config) => core.resume(config),
|
|
259
265
|
setMessages: (messages: readonly ThreadMessage[]) =>
|
|
260
266
|
core.applyExternalMessages(messages),
|