@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.
Files changed (47) hide show
  1. package/dist/index.d.ts +2 -2
  2. package/dist/index.js +2 -2
  3. package/dist/runtime/AgUiThreadRuntimeCore.d.ts +25 -15
  4. package/dist/runtime/AgUiThreadRuntimeCore.d.ts.map +1 -1
  5. package/dist/runtime/AgUiThreadRuntimeCore.js +257 -210
  6. package/dist/runtime/AgUiThreadRuntimeCore.js.map +1 -1
  7. package/dist/runtime/adapter/conversions.d.ts +13 -3
  8. package/dist/runtime/adapter/conversions.d.ts.map +1 -1
  9. package/dist/runtime/adapter/conversions.js +77 -39
  10. package/dist/runtime/adapter/conversions.js.map +1 -1
  11. package/dist/runtime/adapter/run-aggregator.d.ts +23 -2
  12. package/dist/runtime/adapter/run-aggregator.d.ts.map +1 -1
  13. package/dist/runtime/adapter/run-aggregator.js +155 -41
  14. package/dist/runtime/adapter/run-aggregator.js.map +1 -1
  15. package/dist/runtime/adapter/subscriber.d.ts +3 -0
  16. package/dist/runtime/adapter/subscriber.d.ts.map +1 -1
  17. package/dist/runtime/adapter/subscriber.js +9 -0
  18. package/dist/runtime/adapter/subscriber.js.map +1 -1
  19. package/dist/runtime/adapter/tool-approval.d.ts +56 -0
  20. package/dist/runtime/adapter/tool-approval.d.ts.map +1 -0
  21. package/dist/runtime/adapter/tool-approval.js +244 -0
  22. package/dist/runtime/adapter/tool-approval.js.map +1 -0
  23. package/dist/runtime/event-parser.d.ts.map +1 -1
  24. package/dist/runtime/event-parser.js +2 -1
  25. package/dist/runtime/event-parser.js.map +1 -1
  26. package/dist/runtime/interrupt-internals.d.ts +8 -0
  27. package/dist/runtime/interrupt-internals.d.ts.map +1 -0
  28. package/dist/runtime/interrupt-internals.js +25 -0
  29. package/dist/runtime/interrupt-internals.js.map +1 -0
  30. package/dist/useAgUiRuntime.js +14 -9
  31. package/dist/useAgUiRuntime.js.map +1 -1
  32. package/package.json +9 -9
  33. package/src/index.ts +8 -2
  34. package/src/runtime/AgUiThreadRuntimeCore.test.ts +105 -0
  35. package/src/runtime/AgUiThreadRuntimeCore.ts +436 -272
  36. package/src/runtime/adapter/conversions.content.test.ts +61 -3
  37. package/src/runtime/adapter/conversions.ts +123 -57
  38. package/src/runtime/adapter/run-aggregator.approval.test.ts +103 -0
  39. package/src/runtime/adapter/run-aggregator.ts +234 -47
  40. package/src/runtime/adapter/subscriber.ts +14 -0
  41. package/src/runtime/adapter/tool-approval.test.ts +681 -0
  42. package/src/runtime/adapter/tool-approval.ts +366 -0
  43. package/src/runtime/event-parser.ts +10 -1
  44. package/src/runtime/interrupt-internals.ts +35 -0
  45. package/src/useAgUiRuntime.approval.test.tsx +885 -0
  46. package/src/useAgUiRuntime.history.test.tsx +76 -0
  47. 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
+ });
@@ -81,14 +81,16 @@ export function useAgUiRuntime(
81
81
  }
82
82
 
83
83
  const core = coreRef.current;
84
- core.updateOptions({
85
- agent: options.agent,
86
- logger,
87
- showThinking: options.showThinking ?? true,
88
- autoCancelPendingToolCalls: options.autoCancelPendingToolCalls,
89
- ...(options.onError && { onError: options.onError }),
90
- ...(options.onCancel && { onCancel: options.onCancel }),
91
- ...(historyAdapter && { history: historyAdapter }),
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),