@assistant-ui/react-statewire 0.2.0 → 0.4.0
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/AssistantTransportThread.d.ts +4 -4
- package/dist/AssistantTransportThread.d.ts.map +1 -1
- package/dist/AssistantTransportThread.js +52 -54
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +62 -5
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +33 -106
- package/dist/StatewireThread.js.map +1 -1
- package/dist/host.d.ts +1 -3
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +20 -12
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +21 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/inputRequests.d.ts +14 -0
- package/dist/inputRequests.d.ts.map +1 -0
- package/dist/inputRequests.js +35 -0
- package/dist/inputRequests.js.map +1 -0
- package/dist/runCommands.d.ts +27 -0
- package/dist/runCommands.d.ts.map +1 -0
- package/dist/runCommands.js +109 -0
- package/dist/runCommands.js.map +1 -0
- package/dist/types.d.ts +110 -29
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/AssistantTransportThread.ts +117 -69
- package/src/StatewireThread.test.tsx +294 -40
- package/src/StatewireThread.ts +146 -133
- package/src/__tests__/testHelpers.tsx +19 -2
- package/src/athena.e2e.test.tsx +55 -31
- package/src/host.ts +23 -11
- package/src/index.ts +20 -6
- package/src/inputRequests.ts +60 -0
- package/src/legacy-diff.test.tsx +56 -13
- package/src/runCommands.ts +157 -0
- package/src/transport.test.tsx +609 -86
- package/src/types.ts +138 -32
package/src/StatewireThread.ts
CHANGED
|
@@ -9,22 +9,30 @@ import {
|
|
|
9
9
|
} from "@assistant-ui/store";
|
|
10
10
|
import type { ClientOutput } from "@assistant-ui/store";
|
|
11
11
|
import type {
|
|
12
|
+
AddToolResultOptions,
|
|
12
13
|
AppendMessage,
|
|
13
14
|
AttachmentAdapter,
|
|
14
15
|
ExternalThreadQueueAdapter,
|
|
15
16
|
ModelContext,
|
|
16
|
-
|
|
17
|
+
RespondToToolApprovalOptions,
|
|
17
18
|
ThreadMessage,
|
|
18
19
|
} from "@assistant-ui/core";
|
|
19
20
|
import { toToolsJSONSchema } from "assistant-stream";
|
|
20
21
|
import type { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
21
22
|
import { useStatewire } from "statewire";
|
|
22
23
|
import type { Statewire, StatewireClient } from "statewire";
|
|
23
|
-
import {
|
|
24
|
-
convertAppendMessageToCommand,
|
|
25
|
-
symbolAssistantTransportExtras,
|
|
26
|
-
} from "./AssistantTransportThread";
|
|
24
|
+
import { symbolAssistantTransportExtras } from "./AssistantTransportThread";
|
|
27
25
|
import type { AssistantTransportExtras } from "./AssistantTransportThread";
|
|
26
|
+
import {
|
|
27
|
+
createRunQueueAdapter,
|
|
28
|
+
resolveReloadCommand,
|
|
29
|
+
toWireMessage,
|
|
30
|
+
viewedLeafId,
|
|
31
|
+
} from "./runCommands";
|
|
32
|
+
import {
|
|
33
|
+
resolveToolApprovalInput,
|
|
34
|
+
resolveToolResultInput,
|
|
35
|
+
} from "./inputRequests";
|
|
28
36
|
import type { AssistantTransportCommand } from "./types";
|
|
29
37
|
|
|
30
38
|
type AnyCommands = Record<string, (...args: any[]) => unknown>;
|
|
@@ -40,72 +48,6 @@ const ambientContextOf = (context: ModelContext): Record<string, unknown> => ({
|
|
|
40
48
|
...(context.config !== undefined && { config: context.config }),
|
|
41
49
|
});
|
|
42
50
|
|
|
43
|
-
const toWireMessage = (
|
|
44
|
-
message: AppendMessage,
|
|
45
|
-
id: string,
|
|
46
|
-
): StatewireThread.UserMessage => {
|
|
47
|
-
if (message.role !== "user")
|
|
48
|
-
throw new Error("statewire: only user messages are supported");
|
|
49
|
-
const files: StatewireThread.FilePart[] = [];
|
|
50
|
-
const texts: string[] = [];
|
|
51
|
-
const source = [
|
|
52
|
-
...message.content,
|
|
53
|
-
...(message.attachments?.flatMap((a) => a.content) ?? []),
|
|
54
|
-
];
|
|
55
|
-
for (const part of source) {
|
|
56
|
-
if (part.type === "text") {
|
|
57
|
-
texts.push(part.text);
|
|
58
|
-
} else if (part.type === "image") {
|
|
59
|
-
files.push({ type: "file", mediaType: "image/*", url: part.image });
|
|
60
|
-
} else if (part.type === "file") {
|
|
61
|
-
files.push({
|
|
62
|
-
type: "file",
|
|
63
|
-
mediaType: part.mimeType,
|
|
64
|
-
url: part.data,
|
|
65
|
-
...(part.filename !== undefined && { filename: part.filename }),
|
|
66
|
-
});
|
|
67
|
-
} else {
|
|
68
|
-
throw new Error(`statewire: unsupported message part "${part.type}"`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
// default-safe against host capabilities: files first, one merged text part
|
|
72
|
-
const parts: (StatewireThread.TextPart | StatewireThread.FilePart)[] = [
|
|
73
|
-
...files,
|
|
74
|
-
...(texts.length > 0
|
|
75
|
-
? [{ type: "text" as const, text: texts.join("\n\n") }]
|
|
76
|
-
: []),
|
|
77
|
-
];
|
|
78
|
-
if (parts.length === 0)
|
|
79
|
-
throw new Error("statewire: cannot send an empty message");
|
|
80
|
-
return { id, role: "user", parts };
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const toQueueItemState = (item: StatewireThread.QueueItem) => ({
|
|
84
|
-
id: item.id,
|
|
85
|
-
prompt: item.parts
|
|
86
|
-
.flatMap((part) => (part.type === "text" ? [part.text] : []))
|
|
87
|
-
.join("\n\n"),
|
|
88
|
-
parts: item.parts.map((part) =>
|
|
89
|
-
part.type === "text"
|
|
90
|
-
? part
|
|
91
|
-
: {
|
|
92
|
-
type: "file" as const,
|
|
93
|
-
data: part.url,
|
|
94
|
-
mimeType: part.mediaType,
|
|
95
|
-
...(part.filename !== undefined && { filename: part.filename }),
|
|
96
|
-
},
|
|
97
|
-
),
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const placementFields = (placement: QueuePlacement) => ({
|
|
101
|
-
...(placement.insertAfter !== undefined && {
|
|
102
|
-
insertAfter: placement.insertAfter,
|
|
103
|
-
}),
|
|
104
|
-
...(placement.insertBefore !== undefined && {
|
|
105
|
-
insertBefore: placement.insertBefore,
|
|
106
|
-
}),
|
|
107
|
-
});
|
|
108
|
-
|
|
109
51
|
const withSessionLostFallback = (
|
|
110
52
|
transport: StatewireThread.Transport<any>,
|
|
111
53
|
latest: { current: StatewireThread.Options<any> },
|
|
@@ -243,51 +185,29 @@ const useStatewireThread = (
|
|
|
243
185
|
" on first convert and changed since",
|
|
244
186
|
);
|
|
245
187
|
|
|
246
|
-
const queue = useMemo(
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const fields = {
|
|
270
|
-
messageId: queueItemId,
|
|
271
|
-
...placementFields(placement),
|
|
272
|
-
};
|
|
273
|
-
dispatch(
|
|
274
|
-
lane === "steer"
|
|
275
|
-
? { type: "run/steer", ...fields }
|
|
276
|
-
: { type: "run/enqueue", ...fields },
|
|
277
|
-
);
|
|
278
|
-
},
|
|
279
|
-
edit: (queueItemId, message) => {
|
|
280
|
-
const wireMessage = toWireMessage(message, queueItemId);
|
|
281
|
-
dispatch(
|
|
282
|
-
laneOf(queueItemId) === "steer"
|
|
283
|
-
? { type: "run/steer", message: wireMessage }
|
|
284
|
-
: { type: "run/enqueue", message: wireMessage },
|
|
285
|
-
);
|
|
286
|
-
},
|
|
287
|
-
remove: (queueItemId) =>
|
|
288
|
-
dispatch({ type: "run/dequeue", messageId: queueItemId }),
|
|
289
|
-
};
|
|
290
|
-
}, [queueItems, steerQueueItems, dispatch]);
|
|
188
|
+
const queue = useMemo(
|
|
189
|
+
(): ExternalThreadQueueAdapter | undefined =>
|
|
190
|
+
queueItems
|
|
191
|
+
? createRunQueueAdapter(
|
|
192
|
+
converted.messages,
|
|
193
|
+
queueItems,
|
|
194
|
+
steerQueueItems,
|
|
195
|
+
dispatch,
|
|
196
|
+
)
|
|
197
|
+
: undefined,
|
|
198
|
+
[converted.messages, queueItems, steerQueueItems, dispatch],
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
const inputRequests = converted.inputRequests;
|
|
202
|
+
const [inputSeam] = useState(() => ({
|
|
203
|
+
present: inputRequests !== undefined,
|
|
204
|
+
}));
|
|
205
|
+
if (inputSeam.present !== (inputRequests !== undefined))
|
|
206
|
+
throw new Error(
|
|
207
|
+
"statewire: the converter's inputRequests projection must be stable — it was " +
|
|
208
|
+
(inputSeam.present ? "defined" : "undefined") +
|
|
209
|
+
" on first convert and changed since",
|
|
210
|
+
);
|
|
291
211
|
|
|
292
212
|
const extras = useMemo(
|
|
293
213
|
(): AssistantTransportExtras => ({
|
|
@@ -295,18 +215,24 @@ const useStatewireThread = (
|
|
|
295
215
|
sendCommand: dispatch,
|
|
296
216
|
reconnect: connection.reconnect,
|
|
297
217
|
state,
|
|
218
|
+
...(inputRequests !== undefined && { inputRequests }),
|
|
298
219
|
}),
|
|
299
|
-
[dispatch, connection.reconnect, state],
|
|
220
|
+
[dispatch, connection.reconnect, state, inputRequests],
|
|
300
221
|
);
|
|
301
222
|
|
|
302
223
|
const handleAppend = (message: AppendMessage) => {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
224
|
+
dispatch({
|
|
225
|
+
type: "run/steer",
|
|
226
|
+
message: toWireMessage(message, crypto.randomUUID()),
|
|
227
|
+
anchorMessageId: viewedLeafId(converted.messages),
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const handleReload = (parentId: string | null) => {
|
|
232
|
+
dispatch(resolveReloadCommand(converted.messages, parentId));
|
|
306
233
|
};
|
|
307
234
|
|
|
308
235
|
const handleEdit = (message: AppendMessage) => {
|
|
309
|
-
if (!queueItems) return handleAppend(message);
|
|
310
236
|
if (message.sourceId == null)
|
|
311
237
|
throw new Error("statewire: run/edit requires a sourceId");
|
|
312
238
|
dispatch({
|
|
@@ -330,18 +256,23 @@ const useStatewireThread = (
|
|
|
330
256
|
...(options.capabilities?.continue && {
|
|
331
257
|
onResume: () => dispatch({ type: "run/continue" }),
|
|
332
258
|
}),
|
|
259
|
+
...(options.capabilities?.reload && { onReload: handleReload }),
|
|
260
|
+
...(inputRequests !== undefined && {
|
|
261
|
+
onAddToolResult: (toolOptions: AddToolResultOptions) =>
|
|
262
|
+
dispatch(resolveToolResultInput(inputRequests, toolOptions)),
|
|
263
|
+
onRespondToToolApproval: (approval: RespondToToolApprovalOptions) =>
|
|
264
|
+
dispatch(resolveToolApprovalInput(inputRequests, approval)),
|
|
265
|
+
}),
|
|
333
266
|
onCancel: () => {
|
|
334
|
-
const payload = latest.current.
|
|
267
|
+
const payload = latest.current.stopPayload?.();
|
|
335
268
|
if (
|
|
336
269
|
payload !== undefined &&
|
|
337
270
|
(typeof payload !== "object" ||
|
|
338
271
|
payload === null ||
|
|
339
272
|
Array.isArray(payload))
|
|
340
273
|
)
|
|
341
|
-
throw new Error(
|
|
342
|
-
|
|
343
|
-
);
|
|
344
|
-
void commands["run/cancel"]!(
|
|
274
|
+
throw new Error("statewire: stopPayload must return a plain object");
|
|
275
|
+
void commands["run/stop"]!(
|
|
345
276
|
...(payload === undefined ? [] : [payload]),
|
|
346
277
|
).catch((error: Error) => {
|
|
347
278
|
latest.current.onError?.(error);
|
|
@@ -386,12 +317,26 @@ export namespace StatewireThread {
|
|
|
386
317
|
|
|
387
318
|
export type RunEnqueueCommand = {
|
|
388
319
|
readonly type: "run/enqueue";
|
|
389
|
-
} & (
|
|
320
|
+
} & (
|
|
321
|
+
| {
|
|
322
|
+
readonly message: UserMessage;
|
|
323
|
+
/** Branch anchor: the newest message the client knows on its intended branch, null asserting an empty thread. Required on adds; optional on edits of queued items. */
|
|
324
|
+
readonly anchorMessageId?: string | null;
|
|
325
|
+
}
|
|
326
|
+
| { readonly messageId: string }
|
|
327
|
+
) &
|
|
390
328
|
Placement;
|
|
391
329
|
|
|
392
330
|
export type RunSteerCommand = {
|
|
393
331
|
readonly type: "run/steer";
|
|
394
|
-
} & (
|
|
332
|
+
} & (
|
|
333
|
+
| {
|
|
334
|
+
readonly message: UserMessage;
|
|
335
|
+
/** Branch anchor: the newest message the client knows on its intended branch, null asserting an empty thread. Required on adds; optional on edits of queued items. */
|
|
336
|
+
readonly anchorMessageId?: string | null;
|
|
337
|
+
}
|
|
338
|
+
| { readonly messageId: string }
|
|
339
|
+
) &
|
|
395
340
|
Placement;
|
|
396
341
|
|
|
397
342
|
export type RunDequeueCommand = {
|
|
@@ -409,12 +354,71 @@ export namespace StatewireThread {
|
|
|
409
354
|
readonly type: "run/continue";
|
|
410
355
|
};
|
|
411
356
|
|
|
357
|
+
export type ToolCallInputRequest = {
|
|
358
|
+
readonly type: "tool-call";
|
|
359
|
+
readonly id: string;
|
|
360
|
+
readonly toolCallId: string;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
export type ToolApprovalInputRequest = {
|
|
364
|
+
readonly type: "tool-approval";
|
|
365
|
+
readonly id: string;
|
|
366
|
+
readonly toolCallId: string;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
export type CustomInputRequest = {
|
|
370
|
+
readonly type: string;
|
|
371
|
+
readonly id: string;
|
|
372
|
+
readonly [key: string]: ReadonlyJSONValue | undefined;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
/** A host request parking the run until answered; open union for custom types. */
|
|
376
|
+
export type InputRequest =
|
|
377
|
+
| ToolCallInputRequest
|
|
378
|
+
| ToolApprovalInputRequest
|
|
379
|
+
| CustomInputRequest;
|
|
380
|
+
|
|
381
|
+
export type ToolCallInputResponse = {
|
|
382
|
+
readonly output: ReadonlyJSONValue;
|
|
383
|
+
/** Default false; when true, `output` carries the error payload. */
|
|
384
|
+
readonly isError?: boolean;
|
|
385
|
+
readonly artifact?: ReadonlyJSONValue;
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
export type ToolApprovalInputResponse = {
|
|
389
|
+
readonly decision: "approve" | "reject" | "edit" | "respond";
|
|
390
|
+
readonly editedArgs?: ReadonlyJSONValue;
|
|
391
|
+
readonly message?: string;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
export type InputResponse = ReadonlyJSONValue;
|
|
395
|
+
|
|
396
|
+
export type RunInputCommand = {
|
|
397
|
+
readonly type: "run/input";
|
|
398
|
+
readonly requestId: string;
|
|
399
|
+
readonly response: InputResponse;
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
/** An input request projected out of the host state with its response echo. */
|
|
403
|
+
export type InputRequestState = InputRequest & {
|
|
404
|
+
readonly response: InputResponse | null;
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
export type RunReloadCommand = {
|
|
408
|
+
readonly type: "run/reload";
|
|
409
|
+
readonly sourceId: string;
|
|
410
|
+
/** Client-generated id of the regenerated assistant message. */
|
|
411
|
+
readonly messageId: string;
|
|
412
|
+
};
|
|
413
|
+
|
|
412
414
|
export type RunCommand =
|
|
413
415
|
| RunEnqueueCommand
|
|
414
416
|
| RunSteerCommand
|
|
415
417
|
| RunDequeueCommand
|
|
416
418
|
| RunEditCommand
|
|
417
|
-
| RunContinueCommand
|
|
419
|
+
| RunContinueCommand
|
|
420
|
+
| RunReloadCommand
|
|
421
|
+
| RunInputCommand;
|
|
418
422
|
|
|
419
423
|
export type Command = AssistantTransportCommand | RunCommand;
|
|
420
424
|
|
|
@@ -437,6 +441,13 @@ export namespace StatewireThread {
|
|
|
437
441
|
readonly queue?: readonly QueueItem[];
|
|
438
442
|
/** The host's steer lane; requires `queue`. Absent means empty. */
|
|
439
443
|
readonly steerQueue?: readonly QueueItem[];
|
|
444
|
+
/**
|
|
445
|
+
* The host's parked input requests projected out of the state. Returning
|
|
446
|
+
* it (even empty) routes tool results and approvals with a matching
|
|
447
|
+
* pending request through `run/input`. Presence must be stable across
|
|
448
|
+
* conversions.
|
|
449
|
+
*/
|
|
450
|
+
readonly inputRequests?: readonly InputRequestState[];
|
|
440
451
|
};
|
|
441
452
|
|
|
442
453
|
export type ConnectionMetadata = {
|
|
@@ -452,10 +463,10 @@ export namespace StatewireThread {
|
|
|
452
463
|
) => State;
|
|
453
464
|
|
|
454
465
|
/**
|
|
455
|
-
* Payload of the built-in `run/
|
|
466
|
+
* Payload of the built-in `run/stop` command. `reason` is the one well-known
|
|
456
467
|
* field; apps extend it with their own JSON-serializable fields.
|
|
457
468
|
*/
|
|
458
|
-
export type
|
|
469
|
+
export type StopPayload = {
|
|
459
470
|
readonly reason?: string | undefined;
|
|
460
471
|
readonly [key: string]: ReadonlyJSONValue | undefined;
|
|
461
472
|
};
|
|
@@ -470,8 +481,8 @@ export namespace StatewireThread {
|
|
|
470
481
|
* on connect and whenever it changes. Requires a remote transport.
|
|
471
482
|
*/
|
|
472
483
|
contextSync?: "command-send" | undefined;
|
|
473
|
-
/** Supplies the `run/
|
|
474
|
-
|
|
484
|
+
/** Supplies the `run/stop` command's payload; omitted, stop sends no payload. */
|
|
485
|
+
stopPayload?: (() => StopPayload) | undefined;
|
|
475
486
|
/**
|
|
476
487
|
* Offered each pending command when a remote transport's session is
|
|
477
488
|
* evicted; return "replay" to re-enqueue it under the rotated client id.
|
|
@@ -487,6 +498,8 @@ export namespace StatewireThread {
|
|
|
487
498
|
edit?: boolean | undefined;
|
|
488
499
|
/** The host supports `run/continue` (a continue affordance on aborted runs). */
|
|
489
500
|
continue?: boolean | undefined;
|
|
501
|
+
/** The host supports `run/reload` (regenerating an assistant message). */
|
|
502
|
+
reload?: boolean | undefined;
|
|
490
503
|
}
|
|
491
504
|
| undefined;
|
|
492
505
|
adapters?:
|
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
AssistantTransportConnectionMetadata,
|
|
13
13
|
AssistantTransportOptions,
|
|
14
14
|
AssistantTransportStateConverter,
|
|
15
|
+
InputRequestState,
|
|
15
16
|
} from "../types";
|
|
16
17
|
|
|
17
18
|
export type TestState = {
|
|
@@ -45,7 +46,7 @@ export const defaultConverter: AssistantTransportStateConverter<TestState> = (
|
|
|
45
46
|
messages: [
|
|
46
47
|
...state.messages.map(toThreadMessage),
|
|
47
48
|
...meta.pendingCommands.flatMap((c, i) =>
|
|
48
|
-
c.type === "
|
|
49
|
+
c.type === "run/steer" && "message" in c
|
|
49
50
|
? [
|
|
50
51
|
toThreadMessage({
|
|
51
52
|
id: `optimistic-${i}`,
|
|
@@ -193,10 +194,14 @@ export const toolCallConverter = (
|
|
|
193
194
|
): AssistantTransportStateConverter<TestState> => {
|
|
194
195
|
type ToolCallState = TestState & {
|
|
195
196
|
toolCall?: { id: string; name: string; argsText: string };
|
|
197
|
+
inputRequests?: InputRequestState[];
|
|
196
198
|
};
|
|
197
199
|
return (state, meta) => {
|
|
198
200
|
metas.push(meta);
|
|
199
|
-
const base =
|
|
201
|
+
const base = {
|
|
202
|
+
...defaultConverter(state, meta),
|
|
203
|
+
inputRequests: (state as ToolCallState).inputRequests ?? [],
|
|
204
|
+
};
|
|
200
205
|
const toolCall = (state as ToolCallState).toolCall;
|
|
201
206
|
if (!toolCall) return base;
|
|
202
207
|
return {
|
|
@@ -234,6 +239,18 @@ export const emitToolCall = (server: StreamResponse) => {
|
|
|
234
239
|
path: ["toolCall"],
|
|
235
240
|
value: { id: "tc1", name: "probe_tool", argsText: '{"x":1}' },
|
|
236
241
|
},
|
|
242
|
+
{
|
|
243
|
+
type: "set",
|
|
244
|
+
path: ["inputRequests"],
|
|
245
|
+
value: [
|
|
246
|
+
{
|
|
247
|
+
type: "tool-call",
|
|
248
|
+
id: "req-tc1",
|
|
249
|
+
toolCallId: "tc1",
|
|
250
|
+
response: null,
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
},
|
|
237
254
|
],
|
|
238
255
|
});
|
|
239
256
|
server.done();
|
package/src/athena.e2e.test.tsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
|
|
3
3
|
// End-to-end simulation of the olympus deep-agent call site: LangGraph-shaped
|
|
4
|
-
// state, optimistic dedup via a WeakMap trackingId cache,
|
|
5
|
-
// prepareSendCommandsRequest, async headers, body fn, frontend tool
|
|
6
|
-
// custom resume command, cancel mid-stream, resume via resumeApi.
|
|
4
|
+
// state, optimistic dedup via a WeakMap trackingId cache, attachment stripping
|
|
5
|
+
// in prepareSendCommandsRequest, async headers, body fn, frontend tool
|
|
6
|
+
// execution, custom resume command, cancel mid-stream, resume via resumeApi.
|
|
7
7
|
|
|
8
8
|
import { render, waitFor } from "@testing-library/react";
|
|
9
9
|
import type { FC } from "react";
|
|
@@ -13,7 +13,10 @@ import type { ExternalThreadMessage } from "@assistant-ui/react";
|
|
|
13
13
|
import { AssistantTransportThread } from "./AssistantTransportThread";
|
|
14
14
|
import { useAssistantTransportSendCommand } from "./hooks";
|
|
15
15
|
import type {
|
|
16
|
-
|
|
16
|
+
InputRequestState,
|
|
17
|
+
RunEditCommand,
|
|
18
|
+
RunSteerCommand,
|
|
19
|
+
RunUserMessage,
|
|
17
20
|
AssistantTransportCommand,
|
|
18
21
|
AssistantTransportOptions,
|
|
19
22
|
AssistantTransportStateConverter,
|
|
@@ -37,17 +40,27 @@ type LangGraphMessage =
|
|
|
37
40
|
type LangGraphState = {
|
|
38
41
|
messages: LangGraphMessage[];
|
|
39
42
|
todos: string[];
|
|
43
|
+
inputRequests?: InputRequestState[];
|
|
40
44
|
};
|
|
41
45
|
|
|
42
46
|
const INITIAL_STATE: LangGraphState = { messages: [], todos: [] };
|
|
43
47
|
|
|
44
|
-
// Athena's optimistic dedup: each message
|
|
48
|
+
// Athena's optimistic dedup: each user-message command gets a stable
|
|
45
49
|
// trackingId; the server echoes it on the human message, which retires the
|
|
46
50
|
// optimistic copy without a flicker frame or a phantom duplicate.
|
|
51
|
+
type UserMessageCommand = (RunSteerCommand | RunEditCommand) & {
|
|
52
|
+
message: RunUserMessage;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const isUserMessageCommand = (
|
|
56
|
+
c: AssistantTransportCommand,
|
|
57
|
+
): c is UserMessageCommand =>
|
|
58
|
+
(c.type === "run/steer" || c.type === "run/edit") && "message" in c;
|
|
59
|
+
|
|
47
60
|
const createConverter = () => {
|
|
48
|
-
const trackingIds = new WeakMap<
|
|
61
|
+
const trackingIds = new WeakMap<object, string>();
|
|
49
62
|
let nextTrackingId = 0;
|
|
50
|
-
const trackingIdOf = (command:
|
|
63
|
+
const trackingIdOf = (command: object) => {
|
|
51
64
|
let id = trackingIds.get(command);
|
|
52
65
|
if (id === undefined) {
|
|
53
66
|
id = `track-${nextTrackingId++}`;
|
|
@@ -108,8 +121,8 @@ const createConverter = () => {
|
|
|
108
121
|
}
|
|
109
122
|
|
|
110
123
|
for (const c of meta.pendingCommands) {
|
|
111
|
-
if (c
|
|
112
|
-
const trackingId = trackingIdOf(c
|
|
124
|
+
if (!isUserMessageCommand(c)) continue;
|
|
125
|
+
const trackingId = trackingIdOf(c);
|
|
113
126
|
const echoed = state.messages.some(
|
|
114
127
|
(m) => m.type === "human" && m.trackingId === trackingId,
|
|
115
128
|
);
|
|
@@ -130,6 +143,7 @@ const createConverter = () => {
|
|
|
130
143
|
messages,
|
|
131
144
|
isRunning: meta.isSending,
|
|
132
145
|
state: { todos: state.todos },
|
|
146
|
+
inputRequests: state.inputRequests ?? [],
|
|
133
147
|
};
|
|
134
148
|
};
|
|
135
149
|
|
|
@@ -168,15 +182,13 @@ const mountDeepAgent = () => {
|
|
|
168
182
|
prepareSendCommandsRequest: (body) => ({
|
|
169
183
|
...body,
|
|
170
184
|
commands: body.commands.map((c) =>
|
|
171
|
-
c
|
|
185
|
+
isUserMessageCommand(c)
|
|
172
186
|
? {
|
|
173
187
|
...c,
|
|
174
|
-
trackingId: trackingIdOf(c
|
|
188
|
+
trackingId: trackingIdOf(c),
|
|
175
189
|
message: {
|
|
176
|
-
...
|
|
177
|
-
parts:
|
|
178
|
-
(p) => p.type !== "image",
|
|
179
|
-
),
|
|
190
|
+
...c.message,
|
|
191
|
+
parts: c.message.parts.filter((p) => p.type !== "file"),
|
|
180
192
|
},
|
|
181
193
|
}
|
|
182
194
|
: c,
|
|
@@ -197,7 +209,7 @@ const mountDeepAgent = () => {
|
|
|
197
209
|
messages: [
|
|
198
210
|
...state.messages,
|
|
199
211
|
...commands.flatMap((c, i) =>
|
|
200
|
-
c
|
|
212
|
+
isUserMessageCommand(c)
|
|
201
213
|
? [
|
|
202
214
|
{
|
|
203
215
|
id: `reattached-${i}`,
|
|
@@ -205,7 +217,7 @@ const mountDeepAgent = () => {
|
|
|
205
217
|
content:
|
|
206
218
|
c.message.parts.find((p) => p.type === "text")?.text ??
|
|
207
219
|
"",
|
|
208
|
-
trackingId: trackingIdOf(c
|
|
220
|
+
trackingId: trackingIdOf(c),
|
|
209
221
|
},
|
|
210
222
|
]
|
|
211
223
|
: [],
|
|
@@ -305,17 +317,18 @@ describe("deep-agent e2e", () => {
|
|
|
305
317
|
expect(headers.get("X-Thread-Id")).toBe("thread-42");
|
|
306
318
|
expect(r1.body.backendPath).toBe("deep-agent");
|
|
307
319
|
expect(r1.body.state).toEqual(INITIAL_STATE);
|
|
308
|
-
// prepareSendCommandsRequest stripped the
|
|
320
|
+
// prepareSendCommandsRequest stripped the file part (the converted image
|
|
321
|
+
// attachment), kept trackingId.
|
|
309
322
|
expect(r1.body.commands).toEqual([
|
|
310
323
|
{
|
|
311
|
-
type: "
|
|
324
|
+
type: "run/steer",
|
|
312
325
|
trackingId: "track-0",
|
|
313
326
|
message: {
|
|
327
|
+
id: expect.any(String),
|
|
314
328
|
role: "user",
|
|
315
329
|
parts: [{ type: "text", text: "what's the weather in SF?" }],
|
|
316
330
|
},
|
|
317
|
-
|
|
318
|
-
sourceId: null,
|
|
331
|
+
anchorMessageId: null,
|
|
319
332
|
},
|
|
320
333
|
]);
|
|
321
334
|
|
|
@@ -380,21 +393,32 @@ describe("deep-agent e2e", () => {
|
|
|
380
393
|
path: ["messages", "1", "tool_calls"],
|
|
381
394
|
value: [{ id: "tc1", name: "get_weather", args: { city: "SF" } }],
|
|
382
395
|
},
|
|
396
|
+
{
|
|
397
|
+
type: "set",
|
|
398
|
+
path: ["inputRequests"],
|
|
399
|
+
value: [
|
|
400
|
+
{
|
|
401
|
+
type: "tool-call",
|
|
402
|
+
id: "in-tc1",
|
|
403
|
+
toolCallId: "tc1",
|
|
404
|
+
response: null,
|
|
405
|
+
},
|
|
406
|
+
],
|
|
407
|
+
},
|
|
383
408
|
],
|
|
384
409
|
});
|
|
385
410
|
servers[0]!.done();
|
|
386
411
|
|
|
387
|
-
// 3 — frontend tool executes and
|
|
412
|
+
// 3 — frontend tool executes and answers the parked input request via
|
|
413
|
+
// run/input in a follow-up run.
|
|
388
414
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
389
415
|
const r2 = fetchMock.requests[1]!;
|
|
390
416
|
expect(r2.url).toBe("https://sync.example.com/api/chat");
|
|
391
417
|
expect(r2.body.commands).toEqual([
|
|
392
418
|
{
|
|
393
|
-
type: "
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
result: { temp: 20, city: "SF" },
|
|
397
|
-
isError: false,
|
|
419
|
+
type: "run/input",
|
|
420
|
+
requestId: "in-tc1",
|
|
421
|
+
response: { output: { temp: 20, city: "SF" } },
|
|
398
422
|
},
|
|
399
423
|
]);
|
|
400
424
|
// The follow-up run posts the converged state from run 1.
|
|
@@ -547,17 +571,17 @@ describe("deep-agent e2e", () => {
|
|
|
547
571
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
548
572
|
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
549
573
|
{
|
|
550
|
-
type: "
|
|
574
|
+
type: "run/edit",
|
|
551
575
|
trackingId: "track-0",
|
|
552
576
|
message: {
|
|
577
|
+
id: expect.any(String),
|
|
553
578
|
role: "user",
|
|
554
579
|
parts: [{ type: "text", text: "better question" }],
|
|
555
580
|
},
|
|
556
|
-
parentId: "a1",
|
|
557
581
|
sourceId: "h2",
|
|
558
582
|
},
|
|
559
583
|
]);
|
|
560
|
-
expect(fetchMock.requests[0]!.body
|
|
584
|
+
expect("parentId" in fetchMock.requests[0]!.body).toBe(false);
|
|
561
585
|
|
|
562
586
|
// Optimistic edit rides alongside the old branch until the server
|
|
563
587
|
// replaces it wholesale with the new branch.
|
|
@@ -613,7 +637,7 @@ describe("deep-agent e2e", () => {
|
|
|
613
637
|
aui().thread().append("save this message");
|
|
614
638
|
|
|
615
639
|
await waitFor(() => expect(harness.errored).toHaveLength(1));
|
|
616
|
-
expect(harness.errored[0]!.map((c) => c.type)).toEqual(["
|
|
640
|
+
expect(harness.errored[0]!.map((c) => c.type)).toEqual(["run/steer"]);
|
|
617
641
|
|
|
618
642
|
// The command left the queue (no longer optimistic/pending) but the
|
|
619
643
|
// re-attached copy keeps it visible.
|