@assistant-ui/react-statewire 0.0.1
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/LICENSE +21 -0
- package/dist/AssistantTransportThread.d.ts +15 -0
- package/dist/AssistantTransportThread.d.ts.map +1 -0
- package/dist/AssistantTransportThread.js +172 -0
- package/dist/AssistantTransportThread.js.map +1 -0
- package/dist/hooks.d.ts +8 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +26 -0
- package/dist/hooks.js.map +1 -0
- package/dist/host.d.ts +41 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +294 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/replayBoundaryStream.d.ts +6 -0
- package/dist/replayBoundaryStream.d.ts.map +1 -0
- package/dist/replayBoundaryStream.js +47 -0
- package/dist/replayBoundaryStream.js.map +1 -0
- package/dist/stateOps.d.ts +10 -0
- package/dist/stateOps.d.ts.map +1 -0
- package/dist/stateOps.js +106 -0
- package/dist/stateOps.js.map +1 -0
- package/dist/types.d.ts +99 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +0 -0
- package/package.json +54 -0
- package/src/AssistantTransportThread.ts +295 -0
- package/src/__tests__/testHelpers.tsx +243 -0
- package/src/athena.e2e.test.tsx +628 -0
- package/src/hooks.ts +28 -0
- package/src/host.ts +413 -0
- package/src/index.ts +23 -0
- package/src/legacy-diff.test.tsx +581 -0
- package/src/replayBoundaryStream.ts +65 -0
- package/src/stateOps.test.ts +125 -0
- package/src/stateOps.ts +122 -0
- package/src/transport.test.tsx +1858 -0
- package/src/types.ts +140 -0
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
// End-to-end simulation of the olympus deep-agent call site: LangGraph-shaped
|
|
4
|
+
// state, optimistic dedup via a WeakMap trackingId cache, image stripping in
|
|
5
|
+
// prepareSendCommandsRequest, async headers, body fn, frontend tool execution,
|
|
6
|
+
// custom resume command, cancel mid-stream, resume via resumeApi.
|
|
7
|
+
|
|
8
|
+
import { render, waitFor } from "@testing-library/react";
|
|
9
|
+
import type { FC } from "react";
|
|
10
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
11
|
+
import { AuiProvider, useAui } from "@assistant-ui/store";
|
|
12
|
+
import type { ExternalThreadMessage } from "@assistant-ui/react";
|
|
13
|
+
import { AssistantTransportThread } from "./AssistantTransportThread";
|
|
14
|
+
import { useAssistantTransportSendCommand } from "./hooks";
|
|
15
|
+
import type {
|
|
16
|
+
AddMessageCommand,
|
|
17
|
+
AssistantTransportCommand,
|
|
18
|
+
AssistantTransportOptions,
|
|
19
|
+
AssistantTransportStateConverter,
|
|
20
|
+
} from "./types";
|
|
21
|
+
import {
|
|
22
|
+
createStreamResponse,
|
|
23
|
+
installFetch,
|
|
24
|
+
threadTexts,
|
|
25
|
+
} from "./__tests__/testHelpers";
|
|
26
|
+
|
|
27
|
+
type LangGraphMessage =
|
|
28
|
+
| { id: string; type: "human"; content: string; trackingId?: string }
|
|
29
|
+
| {
|
|
30
|
+
id: string;
|
|
31
|
+
type: "ai";
|
|
32
|
+
content: string;
|
|
33
|
+
tool_calls?: { id: string; name: string; args: object }[];
|
|
34
|
+
}
|
|
35
|
+
| { id: string; type: "tool"; content: string; tool_call_id: string };
|
|
36
|
+
|
|
37
|
+
type LangGraphState = {
|
|
38
|
+
messages: LangGraphMessage[];
|
|
39
|
+
todos: string[];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const INITIAL_STATE: LangGraphState = { messages: [], todos: [] };
|
|
43
|
+
|
|
44
|
+
// Athena's optimistic dedup: each add-message command gets a stable
|
|
45
|
+
// trackingId; the server echoes it on the human message, which retires the
|
|
46
|
+
// optimistic copy without a flicker frame or a phantom duplicate.
|
|
47
|
+
const createConverter = () => {
|
|
48
|
+
const trackingIds = new WeakMap<AddMessageCommand, string>();
|
|
49
|
+
let nextTrackingId = 0;
|
|
50
|
+
const trackingIdOf = (command: AddMessageCommand) => {
|
|
51
|
+
let id = trackingIds.get(command);
|
|
52
|
+
if (id === undefined) {
|
|
53
|
+
id = `track-${nextTrackingId++}`;
|
|
54
|
+
trackingIds.set(command, id);
|
|
55
|
+
}
|
|
56
|
+
return id;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const converter: AssistantTransportStateConverter<LangGraphState> = (
|
|
60
|
+
state,
|
|
61
|
+
meta,
|
|
62
|
+
) => {
|
|
63
|
+
const toolResults = new Map<string, string>();
|
|
64
|
+
for (const m of state.messages) {
|
|
65
|
+
if (m.type === "tool") toolResults.set(m.tool_call_id, m.content);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const messages: ExternalThreadMessage[] = [];
|
|
69
|
+
for (const m of state.messages) {
|
|
70
|
+
if (m.type === "human") {
|
|
71
|
+
messages.push({
|
|
72
|
+
id: m.id,
|
|
73
|
+
role: "user",
|
|
74
|
+
createdAt: new Date(0),
|
|
75
|
+
content: [{ type: "text", text: m.content }],
|
|
76
|
+
attachments: [],
|
|
77
|
+
metadata: { custom: { trackingId: m.trackingId } },
|
|
78
|
+
} as unknown as ExternalThreadMessage);
|
|
79
|
+
} else if (m.type === "ai") {
|
|
80
|
+
const content = [
|
|
81
|
+
...(m.content ? [{ type: "text", text: m.content }] : []),
|
|
82
|
+
...(m.tool_calls ?? []).map((tc) => ({
|
|
83
|
+
type: "tool-call",
|
|
84
|
+
toolCallId: tc.id,
|
|
85
|
+
toolName: tc.name,
|
|
86
|
+
args: tc.args,
|
|
87
|
+
argsText: JSON.stringify(tc.args),
|
|
88
|
+
...(toolResults.has(tc.id) && {
|
|
89
|
+
result: JSON.parse(toolResults.get(tc.id)!),
|
|
90
|
+
}),
|
|
91
|
+
})),
|
|
92
|
+
];
|
|
93
|
+
const unresolved = (m.tool_calls ?? []).some(
|
|
94
|
+
(tc) => !toolResults.has(tc.id),
|
|
95
|
+
);
|
|
96
|
+
messages.push({
|
|
97
|
+
id: m.id,
|
|
98
|
+
role: "assistant",
|
|
99
|
+
createdAt: new Date(0),
|
|
100
|
+
content,
|
|
101
|
+
attachments: [],
|
|
102
|
+
metadata: { custom: {} },
|
|
103
|
+
status: unresolved
|
|
104
|
+
? { type: "requires-action", reason: "tool-call" }
|
|
105
|
+
: { type: "complete", reason: "stop" },
|
|
106
|
+
} as unknown as ExternalThreadMessage);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
for (const c of meta.pendingCommands) {
|
|
111
|
+
if (c.type !== "add-message" || c.message.role !== "user") continue;
|
|
112
|
+
const trackingId = trackingIdOf(c as AddMessageCommand);
|
|
113
|
+
const echoed = state.messages.some(
|
|
114
|
+
(m) => m.type === "human" && m.trackingId === trackingId,
|
|
115
|
+
);
|
|
116
|
+
if (echoed) continue;
|
|
117
|
+
messages.push({
|
|
118
|
+
id: `optimistic-${trackingId}`,
|
|
119
|
+
role: "user",
|
|
120
|
+
createdAt: new Date(0),
|
|
121
|
+
content: c.message.parts.flatMap((p) =>
|
|
122
|
+
p.type === "text" ? [{ type: "text", text: p.text }] : [],
|
|
123
|
+
),
|
|
124
|
+
attachments: [],
|
|
125
|
+
metadata: { custom: { trackingId } },
|
|
126
|
+
} as unknown as ExternalThreadMessage);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
messages,
|
|
131
|
+
isRunning: meta.isSending,
|
|
132
|
+
state: { todos: state.todos },
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
return { converter, trackingIdOf };
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const mountDeepAgent = () => {
|
|
140
|
+
const { converter, trackingIdOf } = createConverter();
|
|
141
|
+
const cancelled: AssistantTransportCommand[][] = [];
|
|
142
|
+
const errored: AssistantTransportCommand[][] = [];
|
|
143
|
+
const responses: (string | null)[] = [];
|
|
144
|
+
let finishCount = 0;
|
|
145
|
+
|
|
146
|
+
const captured: {
|
|
147
|
+
aui?: ReturnType<typeof useAui>;
|
|
148
|
+
sendCommand?: (command: AssistantTransportCommand) => void;
|
|
149
|
+
} = {};
|
|
150
|
+
const Capture: FC = () => {
|
|
151
|
+
captured.aui = useAui();
|
|
152
|
+
captured.sendCommand = useAssistantTransportSendCommand();
|
|
153
|
+
return null;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const options: AssistantTransportOptions<LangGraphState> = {
|
|
157
|
+
initialState: INITIAL_STATE,
|
|
158
|
+
api: "https://sync.example.com/api/chat",
|
|
159
|
+
resumeApi: "https://sync.example.com/api/resume",
|
|
160
|
+
protocol: "assistant-transport",
|
|
161
|
+
converter,
|
|
162
|
+
capabilities: { edit: true },
|
|
163
|
+
headers: async () => ({
|
|
164
|
+
Authorization: "Bearer test-token",
|
|
165
|
+
"X-Thread-Id": "thread-42",
|
|
166
|
+
}),
|
|
167
|
+
body: () => Promise.resolve({ backendPath: "deep-agent" }),
|
|
168
|
+
prepareSendCommandsRequest: (body) => ({
|
|
169
|
+
...body,
|
|
170
|
+
commands: body.commands.map((c) =>
|
|
171
|
+
c.type === "add-message"
|
|
172
|
+
? {
|
|
173
|
+
...c,
|
|
174
|
+
trackingId: trackingIdOf(c as AddMessageCommand),
|
|
175
|
+
message: {
|
|
176
|
+
...(c as AddMessageCommand).message,
|
|
177
|
+
parts: (c as AddMessageCommand).message.parts.filter(
|
|
178
|
+
(p) => p.type !== "image",
|
|
179
|
+
),
|
|
180
|
+
},
|
|
181
|
+
}
|
|
182
|
+
: c,
|
|
183
|
+
),
|
|
184
|
+
}),
|
|
185
|
+
onResponse: (response) => {
|
|
186
|
+
responses.push(response.headers.get("X-Stream-Status"));
|
|
187
|
+
},
|
|
188
|
+
onFinish: () => {
|
|
189
|
+
finishCount += 1;
|
|
190
|
+
},
|
|
191
|
+
onError: (_error, { commands, updateState }) => {
|
|
192
|
+
errored.push(commands);
|
|
193
|
+
// Athena re-attaches failed user messages into the local state so the
|
|
194
|
+
// UI does not lose them.
|
|
195
|
+
updateState((state) => ({
|
|
196
|
+
...state,
|
|
197
|
+
messages: [
|
|
198
|
+
...state.messages,
|
|
199
|
+
...commands.flatMap((c, i) =>
|
|
200
|
+
c.type === "add-message" && c.message.role === "user"
|
|
201
|
+
? [
|
|
202
|
+
{
|
|
203
|
+
id: `reattached-${i}`,
|
|
204
|
+
type: "human" as const,
|
|
205
|
+
content:
|
|
206
|
+
c.message.parts.find((p) => p.type === "text")?.text ??
|
|
207
|
+
"",
|
|
208
|
+
trackingId: trackingIdOf(c as AddMessageCommand),
|
|
209
|
+
},
|
|
210
|
+
]
|
|
211
|
+
: [],
|
|
212
|
+
),
|
|
213
|
+
],
|
|
214
|
+
}));
|
|
215
|
+
},
|
|
216
|
+
onCancel: ({ commands }) => {
|
|
217
|
+
cancelled.push(commands);
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const App: FC = () => {
|
|
222
|
+
const aui = useAui({
|
|
223
|
+
thread: AssistantTransportThread<LangGraphState>(options),
|
|
224
|
+
});
|
|
225
|
+
return (
|
|
226
|
+
<AuiProvider value={aui}>
|
|
227
|
+
<Capture />
|
|
228
|
+
</AuiProvider>
|
|
229
|
+
);
|
|
230
|
+
};
|
|
231
|
+
const utils = render(<App />);
|
|
232
|
+
return {
|
|
233
|
+
aui: () => captured.aui!,
|
|
234
|
+
sendCommand: (c: AssistantTransportCommand) => captured.sendCommand!(c),
|
|
235
|
+
cancelled,
|
|
236
|
+
errored,
|
|
237
|
+
responses,
|
|
238
|
+
finishCount: () => finishCount,
|
|
239
|
+
...utils,
|
|
240
|
+
};
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
afterEach(() => {
|
|
244
|
+
vi.unstubAllGlobals();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe("deep-agent e2e", () => {
|
|
248
|
+
it("runs a full conversation: send, echo, tool call, follow-up, interrupt, cancel, resume", async () => {
|
|
249
|
+
const fetchMock = installFetch();
|
|
250
|
+
const servers: ReturnType<typeof createStreamResponse>[] = [];
|
|
251
|
+
fetchMock.setHandler(() => {
|
|
252
|
+
const server = createStreamResponse(
|
|
253
|
+
fetchMock.requests.length === 4
|
|
254
|
+
? { headers: { "X-Stream-Status": "active" } }
|
|
255
|
+
: undefined,
|
|
256
|
+
);
|
|
257
|
+
servers.push(server);
|
|
258
|
+
return server.response;
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const harness = mountDeepAgent();
|
|
262
|
+
const { aui, sendCommand, responses } = harness;
|
|
263
|
+
|
|
264
|
+
aui()
|
|
265
|
+
.modelContext()
|
|
266
|
+
.register({
|
|
267
|
+
getModelContext: () => ({
|
|
268
|
+
tools: {
|
|
269
|
+
get_weather: {
|
|
270
|
+
parameters: {
|
|
271
|
+
type: "object",
|
|
272
|
+
properties: { city: { type: "string" } },
|
|
273
|
+
},
|
|
274
|
+
execute: async (args: any) => ({ temp: 20, city: args.city }),
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
}),
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
// 1 — user sends a message with an image attachment part.
|
|
281
|
+
aui()
|
|
282
|
+
.thread()
|
|
283
|
+
.append({
|
|
284
|
+
role: "user",
|
|
285
|
+
content: [
|
|
286
|
+
{ type: "text", text: "what's the weather in SF?" },
|
|
287
|
+
{ type: "image", image: "data:image/png;base64,abc" },
|
|
288
|
+
],
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
// Optimistic message renders before any network round trip completes.
|
|
292
|
+
await waitFor(() =>
|
|
293
|
+
expect(threadTexts(aui)).toEqual(["what's the weather in SF?"]),
|
|
294
|
+
);
|
|
295
|
+
expect(
|
|
296
|
+
aui().thread().getState().messages.at(0)!.id.startsWith("optimistic-"),
|
|
297
|
+
).toBe(true);
|
|
298
|
+
expect(aui().thread().getState().isRunning).toBe(true);
|
|
299
|
+
|
|
300
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
301
|
+
const r1 = fetchMock.requests[0]!;
|
|
302
|
+
expect(r1.url).toBe("https://sync.example.com/api/chat");
|
|
303
|
+
const headers = new Headers(r1.init.headers);
|
|
304
|
+
expect(headers.get("Authorization")).toBe("Bearer test-token");
|
|
305
|
+
expect(headers.get("X-Thread-Id")).toBe("thread-42");
|
|
306
|
+
expect(r1.body.backendPath).toBe("deep-agent");
|
|
307
|
+
expect(r1.body.state).toEqual(INITIAL_STATE);
|
|
308
|
+
// prepareSendCommandsRequest stripped the image part, kept trackingId.
|
|
309
|
+
expect(r1.body.commands).toEqual([
|
|
310
|
+
{
|
|
311
|
+
type: "add-message",
|
|
312
|
+
trackingId: "track-0",
|
|
313
|
+
message: {
|
|
314
|
+
role: "user",
|
|
315
|
+
parts: [{ type: "text", text: "what's the weather in SF?" }],
|
|
316
|
+
},
|
|
317
|
+
parentId: null,
|
|
318
|
+
sourceId: null,
|
|
319
|
+
},
|
|
320
|
+
]);
|
|
321
|
+
|
|
322
|
+
// 2 — server echoes the human message (backend id + trackingId) and
|
|
323
|
+
// streams the AI reply via append-text, then a tool call.
|
|
324
|
+
servers[0]!.emit({
|
|
325
|
+
type: "update-state",
|
|
326
|
+
operations: [
|
|
327
|
+
{
|
|
328
|
+
type: "set",
|
|
329
|
+
path: ["messages"],
|
|
330
|
+
value: [
|
|
331
|
+
{
|
|
332
|
+
id: "h1",
|
|
333
|
+
type: "human",
|
|
334
|
+
content: "what's the weather in SF?",
|
|
335
|
+
trackingId: "track-0",
|
|
336
|
+
},
|
|
337
|
+
{ id: "a1", type: "ai", content: "" },
|
|
338
|
+
],
|
|
339
|
+
},
|
|
340
|
+
],
|
|
341
|
+
});
|
|
342
|
+
await waitFor(() =>
|
|
343
|
+
expect(threadTexts(aui)).toEqual(["what's the weather in SF?", ""]),
|
|
344
|
+
);
|
|
345
|
+
// Optimistic copy retired — the echoed message replaced it, no phantom.
|
|
346
|
+
expect(
|
|
347
|
+
aui()
|
|
348
|
+
.thread()
|
|
349
|
+
.getState()
|
|
350
|
+
.messages.map((m) => m.id),
|
|
351
|
+
).toEqual(["h1", "a1"]);
|
|
352
|
+
|
|
353
|
+
servers[0]!.emit({
|
|
354
|
+
type: "update-state",
|
|
355
|
+
operations: [
|
|
356
|
+
{
|
|
357
|
+
type: "append-text",
|
|
358
|
+
path: ["messages", "1", "content"],
|
|
359
|
+
value: "Let me",
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
type: "append-text",
|
|
363
|
+
path: ["messages", "1", "content"],
|
|
364
|
+
value: " check.",
|
|
365
|
+
},
|
|
366
|
+
],
|
|
367
|
+
});
|
|
368
|
+
await waitFor(() =>
|
|
369
|
+
expect(threadTexts(aui)).toEqual([
|
|
370
|
+
"what's the weather in SF?",
|
|
371
|
+
"Let me check.",
|
|
372
|
+
]),
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
servers[0]!.emit({
|
|
376
|
+
type: "update-state",
|
|
377
|
+
operations: [
|
|
378
|
+
{
|
|
379
|
+
type: "set",
|
|
380
|
+
path: ["messages", "1", "tool_calls"],
|
|
381
|
+
value: [{ id: "tc1", name: "get_weather", args: { city: "SF" } }],
|
|
382
|
+
},
|
|
383
|
+
],
|
|
384
|
+
});
|
|
385
|
+
servers[0]!.done();
|
|
386
|
+
|
|
387
|
+
// 3 — frontend tool executes and posts add-tool-result in a follow-up run.
|
|
388
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
389
|
+
const r2 = fetchMock.requests[1]!;
|
|
390
|
+
expect(r2.url).toBe("https://sync.example.com/api/chat");
|
|
391
|
+
expect(r2.body.commands).toEqual([
|
|
392
|
+
{
|
|
393
|
+
type: "add-tool-result",
|
|
394
|
+
toolCallId: "tc1",
|
|
395
|
+
toolName: "get_weather",
|
|
396
|
+
result: { temp: 20, city: "SF" },
|
|
397
|
+
isError: false,
|
|
398
|
+
},
|
|
399
|
+
]);
|
|
400
|
+
// The follow-up run posts the converged state from run 1.
|
|
401
|
+
expect(r2.body.state.messages.map((m: LangGraphMessage) => m.id)).toEqual([
|
|
402
|
+
"h1",
|
|
403
|
+
"a1",
|
|
404
|
+
]);
|
|
405
|
+
|
|
406
|
+
servers[1]!.emit({
|
|
407
|
+
type: "update-state",
|
|
408
|
+
operations: [
|
|
409
|
+
{
|
|
410
|
+
type: "set",
|
|
411
|
+
path: ["messages", "2"],
|
|
412
|
+
value: {
|
|
413
|
+
id: "t1",
|
|
414
|
+
type: "tool",
|
|
415
|
+
content: '{"temp":20,"city":"SF"}',
|
|
416
|
+
tool_call_id: "tc1",
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
type: "set",
|
|
421
|
+
path: ["messages", "3"],
|
|
422
|
+
value: { id: "a2", type: "ai", content: "It is 20°C in SF." },
|
|
423
|
+
},
|
|
424
|
+
{ type: "set", path: ["todos"], value: ["report weather"] },
|
|
425
|
+
],
|
|
426
|
+
});
|
|
427
|
+
servers[1]!.done();
|
|
428
|
+
|
|
429
|
+
await waitFor(() =>
|
|
430
|
+
expect(threadTexts(aui)).toEqual([
|
|
431
|
+
"what's the weather in SF?",
|
|
432
|
+
"Let me check.",
|
|
433
|
+
"It is 20°C in SF.",
|
|
434
|
+
]),
|
|
435
|
+
);
|
|
436
|
+
expect(aui().thread().getState().state).toEqual({
|
|
437
|
+
todos: ["report weather"],
|
|
438
|
+
});
|
|
439
|
+
await waitFor(() =>
|
|
440
|
+
expect(aui().thread().getState().isRunning).toBe(false),
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
// 4 — interrupt resolution via a custom resume command (approval banner).
|
|
444
|
+
sendCommand({ type: "resume", payload: { approve: true } } as never);
|
|
445
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(3));
|
|
446
|
+
expect(fetchMock.requests[2]!.body.commands).toEqual([
|
|
447
|
+
{ type: "resume", payload: { approve: true } },
|
|
448
|
+
]);
|
|
449
|
+
|
|
450
|
+
// 5 — the run streams a partial reply, then the user cancels mid-stream.
|
|
451
|
+
servers[2]!.emit({
|
|
452
|
+
type: "update-state",
|
|
453
|
+
operations: [
|
|
454
|
+
{
|
|
455
|
+
type: "set",
|
|
456
|
+
path: ["messages", "4"],
|
|
457
|
+
value: { id: "a3", type: "ai", content: "Working on" },
|
|
458
|
+
},
|
|
459
|
+
],
|
|
460
|
+
});
|
|
461
|
+
await waitFor(() => expect(threadTexts(aui).at(-1)).toBe("Working on"));
|
|
462
|
+
|
|
463
|
+
aui().thread().cancelRun();
|
|
464
|
+
servers[2]!.error(
|
|
465
|
+
new DOMException("The operation was aborted.", "AbortError"),
|
|
466
|
+
);
|
|
467
|
+
await waitFor(() =>
|
|
468
|
+
expect(aui().thread().getState().isRunning).toBe(false),
|
|
469
|
+
);
|
|
470
|
+
// Partial state from before the cancel is preserved.
|
|
471
|
+
expect(threadTexts(aui).at(-1)).toBe("Working on");
|
|
472
|
+
expect(harness.cancelled).toHaveLength(1);
|
|
473
|
+
|
|
474
|
+
// 6 — reconnect: probe says a run is still active, client resumes.
|
|
475
|
+
aui().thread().resumeRun({ parentId: null });
|
|
476
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(4));
|
|
477
|
+
const r4 = fetchMock.requests[3]!;
|
|
478
|
+
expect(r4.url).toBe("https://sync.example.com/api/resume");
|
|
479
|
+
expect(r4.body.commands).toEqual([]);
|
|
480
|
+
await waitFor(() => expect(responses.at(-1)).toBe("active"));
|
|
481
|
+
|
|
482
|
+
servers[3]!.emit({
|
|
483
|
+
type: "update-state",
|
|
484
|
+
operations: [
|
|
485
|
+
{
|
|
486
|
+
type: "set",
|
|
487
|
+
path: ["messages", "4", "content"],
|
|
488
|
+
value: "Working on it. Done.",
|
|
489
|
+
},
|
|
490
|
+
],
|
|
491
|
+
});
|
|
492
|
+
servers[3]!.done();
|
|
493
|
+
|
|
494
|
+
await waitFor(() =>
|
|
495
|
+
expect(threadTexts(aui)).toEqual([
|
|
496
|
+
"what's the weather in SF?",
|
|
497
|
+
"Let me check.",
|
|
498
|
+
"It is 20°C in SF.",
|
|
499
|
+
"Working on it. Done.",
|
|
500
|
+
]),
|
|
501
|
+
);
|
|
502
|
+
await waitFor(() =>
|
|
503
|
+
expect(aui().thread().getState().isRunning).toBe(false),
|
|
504
|
+
);
|
|
505
|
+
expect(harness.finishCount()).toBe(4);
|
|
506
|
+
expect(harness.errored).toHaveLength(0);
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it("edits a user message and dedups the optimistic copy against the echoed branch", async () => {
|
|
510
|
+
const fetchMock = installFetch();
|
|
511
|
+
const servers: ReturnType<typeof createStreamResponse>[] = [];
|
|
512
|
+
fetchMock.setHandler(() => {
|
|
513
|
+
const server = createStreamResponse();
|
|
514
|
+
servers.push(server);
|
|
515
|
+
return server.response;
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
const { aui } = mountDeepAgent();
|
|
519
|
+
|
|
520
|
+
aui().thread().importExternalState!({
|
|
521
|
+
messages: [
|
|
522
|
+
{ id: "h1", type: "human", content: "hello" },
|
|
523
|
+
{ id: "a1", type: "ai", content: "hi" },
|
|
524
|
+
{ id: "h2", type: "human", content: "bad question" },
|
|
525
|
+
{ id: "a2", type: "ai", content: "bad answer" },
|
|
526
|
+
],
|
|
527
|
+
todos: [],
|
|
528
|
+
} satisfies LangGraphState);
|
|
529
|
+
await waitFor(() =>
|
|
530
|
+
expect(threadTexts(aui)).toEqual([
|
|
531
|
+
"hello",
|
|
532
|
+
"hi",
|
|
533
|
+
"bad question",
|
|
534
|
+
"bad answer",
|
|
535
|
+
]),
|
|
536
|
+
);
|
|
537
|
+
|
|
538
|
+
const composer = aui().thread().message({ id: "h2" }).composer();
|
|
539
|
+
composer.beginEdit();
|
|
540
|
+
await waitFor(() => expect(composer.getState().isEditing).toBe(true));
|
|
541
|
+
composer.setText("better question");
|
|
542
|
+
await waitFor(() =>
|
|
543
|
+
expect(composer.getState().text).toBe("better question"),
|
|
544
|
+
);
|
|
545
|
+
composer.send();
|
|
546
|
+
|
|
547
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
548
|
+
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
549
|
+
{
|
|
550
|
+
type: "add-message",
|
|
551
|
+
trackingId: "track-0",
|
|
552
|
+
message: {
|
|
553
|
+
role: "user",
|
|
554
|
+
parts: [{ type: "text", text: "better question" }],
|
|
555
|
+
},
|
|
556
|
+
parentId: "a1",
|
|
557
|
+
sourceId: "h2",
|
|
558
|
+
},
|
|
559
|
+
]);
|
|
560
|
+
expect(fetchMock.requests[0]!.body.parentId).toBe("a1");
|
|
561
|
+
|
|
562
|
+
// Optimistic edit rides alongside the old branch until the server
|
|
563
|
+
// replaces it wholesale with the new branch.
|
|
564
|
+
await waitFor(() =>
|
|
565
|
+
expect(threadTexts(aui).at(-1)).toBe("better question"),
|
|
566
|
+
);
|
|
567
|
+
|
|
568
|
+
servers[0]!.emit({
|
|
569
|
+
type: "update-state",
|
|
570
|
+
operations: [
|
|
571
|
+
{
|
|
572
|
+
type: "set",
|
|
573
|
+
path: ["messages"],
|
|
574
|
+
value: [
|
|
575
|
+
{ id: "h1", type: "human", content: "hello" },
|
|
576
|
+
{ id: "a1", type: "ai", content: "hi" },
|
|
577
|
+
{
|
|
578
|
+
id: "h3",
|
|
579
|
+
type: "human",
|
|
580
|
+
content: "better question",
|
|
581
|
+
trackingId: "track-0",
|
|
582
|
+
},
|
|
583
|
+
{ id: "a3", type: "ai", content: "great answer" },
|
|
584
|
+
],
|
|
585
|
+
},
|
|
586
|
+
],
|
|
587
|
+
});
|
|
588
|
+
servers[0]!.done();
|
|
589
|
+
|
|
590
|
+
await waitFor(() =>
|
|
591
|
+
expect(threadTexts(aui)).toEqual([
|
|
592
|
+
"hello",
|
|
593
|
+
"hi",
|
|
594
|
+
"better question",
|
|
595
|
+
"great answer",
|
|
596
|
+
]),
|
|
597
|
+
);
|
|
598
|
+
expect(
|
|
599
|
+
aui()
|
|
600
|
+
.thread()
|
|
601
|
+
.getState()
|
|
602
|
+
.messages.map((m) => m.id),
|
|
603
|
+
).toEqual(["h1", "a1", "h3", "a3"]);
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
it("re-attaches failed user messages via onError updateState", async () => {
|
|
607
|
+
const fetchMock = installFetch();
|
|
608
|
+
fetchMock.setHandler(() => new Response("backend down", { status: 500 }));
|
|
609
|
+
|
|
610
|
+
const harness = mountDeepAgent();
|
|
611
|
+
const { aui } = harness;
|
|
612
|
+
|
|
613
|
+
aui().thread().append("save this message");
|
|
614
|
+
|
|
615
|
+
await waitFor(() => expect(harness.errored).toHaveLength(1));
|
|
616
|
+
expect(harness.errored[0]!.map((c) => c.type)).toEqual(["add-message"]);
|
|
617
|
+
|
|
618
|
+
// The command left the queue (no longer optimistic/pending) but the
|
|
619
|
+
// re-attached copy keeps it visible.
|
|
620
|
+
await waitFor(() =>
|
|
621
|
+
expect(threadTexts(aui)).toEqual(["save this message"]),
|
|
622
|
+
);
|
|
623
|
+
expect(aui().thread().getState().messages.at(0)!.id).toBe("reattached-0");
|
|
624
|
+
await waitFor(() =>
|
|
625
|
+
expect(aui().thread().getState().isRunning).toBe(false),
|
|
626
|
+
);
|
|
627
|
+
});
|
|
628
|
+
});
|
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useAui, useAuiState } from "@assistant-ui/store";
|
|
4
|
+
import { asAssistantTransportExtras } from "./AssistantTransportThread";
|
|
5
|
+
import type { AssistantTransportCommand, UserExternalState } from "./types";
|
|
6
|
+
|
|
7
|
+
export const useAssistantTransportSendCommand = () => {
|
|
8
|
+
const aui = useAui();
|
|
9
|
+
|
|
10
|
+
return (command: AssistantTransportCommand) => {
|
|
11
|
+
const extras = aui.thread().getState().extras;
|
|
12
|
+
asAssistantTransportExtras(extras).sendCommand(command);
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function useAssistantTransportState(): UserExternalState;
|
|
17
|
+
export function useAssistantTransportState<T>(
|
|
18
|
+
selector: (state: UserExternalState) => T,
|
|
19
|
+
): T;
|
|
20
|
+
export function useAssistantTransportState<T>(
|
|
21
|
+
selector: (state: UserExternalState) => T = (t) => t as T,
|
|
22
|
+
): T | UserExternalState {
|
|
23
|
+
return useAuiState((s) =>
|
|
24
|
+
selector(
|
|
25
|
+
asAssistantTransportExtras(s.thread.extras).state as UserExternalState,
|
|
26
|
+
),
|
|
27
|
+
);
|
|
28
|
+
}
|