@assistant-ui/react-statewire 0.1.1 → 0.3.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 +65 -45
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +122 -10
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +90 -38
- package/dist/StatewireThread.js.map +1 -1
- package/dist/host.d.ts +0 -1
- 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 -5
- 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 +22 -0
- package/dist/runCommands.d.ts.map +1 -0
- package/dist/runCommands.js +105 -0
- package/dist/runCommands.js.map +1 -0
- package/dist/types.d.ts +114 -17
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/AssistantTransportThread.ts +120 -50
- package/src/StatewireThread.test.tsx +587 -61
- package/src/StatewireThread.ts +310 -47
- package/src/__tests__/testHelpers.tsx +1 -1
- package/src/athena.e2e.test.tsx +34 -25
- package/src/host.ts +23 -11
- package/src/index.ts +20 -4
- package/src/inputRequests.ts +57 -0
- package/src/legacy-diff.test.tsx +32 -8
- package/src/runCommands.ts +146 -0
- package/src/transport.test.tsx +581 -57
- package/src/types.ts +133 -19
package/src/StatewireThread.ts
CHANGED
|
@@ -9,22 +9,34 @@ 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,
|
|
17
|
+
RespondToToolApprovalOptions,
|
|
16
18
|
ThreadMessage,
|
|
17
19
|
} from "@assistant-ui/core";
|
|
18
20
|
import { toToolsJSONSchema } from "assistant-stream";
|
|
19
21
|
import type { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
20
22
|
import { useStatewire } from "statewire";
|
|
21
23
|
import type { Statewire, StatewireClient } from "statewire";
|
|
22
|
-
import {
|
|
23
|
-
convertAppendMessageToCommand,
|
|
24
|
-
symbolAssistantTransportExtras,
|
|
25
|
-
} from "./AssistantTransportThread";
|
|
24
|
+
import { symbolAssistantTransportExtras } from "./AssistantTransportThread";
|
|
26
25
|
import type { AssistantTransportExtras } from "./AssistantTransportThread";
|
|
27
|
-
import
|
|
26
|
+
import {
|
|
27
|
+
createRunQueueAdapter,
|
|
28
|
+
resolveReloadCommand,
|
|
29
|
+
toWireMessage,
|
|
30
|
+
} from "./runCommands";
|
|
31
|
+
import {
|
|
32
|
+
resolveToolApprovalInput,
|
|
33
|
+
resolveToolResultInput,
|
|
34
|
+
} from "./inputRequests";
|
|
35
|
+
import type {
|
|
36
|
+
AddMessageCommand,
|
|
37
|
+
AssistantTransportCommand,
|
|
38
|
+
UserMessagePart,
|
|
39
|
+
} from "./types";
|
|
28
40
|
|
|
29
41
|
type AnyCommands = Record<string, (...args: any[]) => unknown>;
|
|
30
42
|
|
|
@@ -39,19 +51,86 @@ const ambientContextOf = (context: ModelContext): Record<string, unknown> => ({
|
|
|
39
51
|
...(context.config !== undefined && { config: context.config }),
|
|
40
52
|
});
|
|
41
53
|
|
|
54
|
+
const convertAppendMessageToCommand = (
|
|
55
|
+
message: AppendMessage,
|
|
56
|
+
): AddMessageCommand | null => {
|
|
57
|
+
if (message.role !== "user")
|
|
58
|
+
throw new Error("Only user messages are supported");
|
|
59
|
+
|
|
60
|
+
const parts: UserMessagePart[] = [];
|
|
61
|
+
const content = [
|
|
62
|
+
...message.content,
|
|
63
|
+
...(message.attachments?.flatMap((a) => a.content) ?? []),
|
|
64
|
+
];
|
|
65
|
+
for (const contentPart of content) {
|
|
66
|
+
if (contentPart.type === "text") {
|
|
67
|
+
parts.push({ type: "text", text: contentPart.text });
|
|
68
|
+
} else if (contentPart.type === "image") {
|
|
69
|
+
parts.push({ type: "image", image: contentPart.image });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (parts.length === 0) return null;
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
type: "add-message",
|
|
77
|
+
message: {
|
|
78
|
+
role: "user",
|
|
79
|
+
parts,
|
|
80
|
+
},
|
|
81
|
+
parentId: message.parentId,
|
|
82
|
+
sourceId: message.sourceId,
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const withSessionLostFallback = (
|
|
87
|
+
transport: StatewireThread.Transport<any>,
|
|
88
|
+
latest: { current: StatewireThread.Options<any> },
|
|
89
|
+
): StatewireThread.Transport<any> => {
|
|
90
|
+
const [transportOptions, ...rest] = transport.args as [
|
|
91
|
+
Record<string, unknown>,
|
|
92
|
+
...unknown[],
|
|
93
|
+
];
|
|
94
|
+
if (
|
|
95
|
+
rest.length > 0 ||
|
|
96
|
+
typeof transportOptions !== "object" ||
|
|
97
|
+
transportOptions === null
|
|
98
|
+
) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
"statewire: onCommandSessionLost requires a transport element that takes a single options object",
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
const transportLevel = transportOptions.onCommandSessionLost as
|
|
104
|
+
| ((command: StatewireClient.SessionLostCommand) => "replay" | void)
|
|
105
|
+
| undefined;
|
|
106
|
+
return {
|
|
107
|
+
...transport,
|
|
108
|
+
args: [
|
|
109
|
+
{
|
|
110
|
+
...transportOptions,
|
|
111
|
+
onCommandSessionLost: (
|
|
112
|
+
command: StatewireClient.SessionLostCommand,
|
|
113
|
+
): "replay" | void =>
|
|
114
|
+
transportLevel?.(command) ??
|
|
115
|
+
latest.current.onCommandSessionLost?.(command),
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
|
|
42
121
|
const useStatewireThread = (
|
|
43
122
|
options: StatewireThread.Options<any>,
|
|
44
123
|
): ClientOutput<"thread"> => {
|
|
45
124
|
const latest = useRef(options);
|
|
46
125
|
latest.current = options;
|
|
47
126
|
|
|
48
|
-
const [pending, setPending] = useState<readonly
|
|
127
|
+
const [pending, setPending] = useState<readonly StatewireThread.Command[]>(
|
|
49
128
|
[],
|
|
50
129
|
);
|
|
51
130
|
const [retire] = useState(
|
|
52
131
|
() => (command: unknown) =>
|
|
53
132
|
setPending((prev) =>
|
|
54
|
-
prev.includes(command as
|
|
133
|
+
prev.includes(command as StatewireThread.Command)
|
|
55
134
|
? prev.filter((c) => c !== command)
|
|
56
135
|
: prev,
|
|
57
136
|
),
|
|
@@ -63,7 +142,10 @@ const useStatewireThread = (
|
|
|
63
142
|
connection,
|
|
64
143
|
setContext,
|
|
65
144
|
} = useStatewire<unknown, AnyCommands>({
|
|
66
|
-
transport:
|
|
145
|
+
transport:
|
|
146
|
+
options.onCommandSessionLost === undefined
|
|
147
|
+
? options.transport
|
|
148
|
+
: withSessionLostFallback(options.transport, latest),
|
|
67
149
|
onCommandChange: (update) => {
|
|
68
150
|
if (update.status === "acked" || update.status === "settled")
|
|
69
151
|
retire(update.params[0]);
|
|
@@ -99,7 +181,7 @@ const useStatewireThread = (
|
|
|
99
181
|
}, [contextSync, clientRef, syncState]);
|
|
100
182
|
|
|
101
183
|
const dispatch = useCallback(
|
|
102
|
-
(command:
|
|
184
|
+
(command: StatewireThread.Command) => {
|
|
103
185
|
setPending((prev) => [...prev, command]);
|
|
104
186
|
void commands[command.type]!(command).catch((error: Error) => {
|
|
105
187
|
retire(command);
|
|
@@ -122,10 +204,12 @@ const useStatewireThread = (
|
|
|
122
204
|
() => converter(state, metadata),
|
|
123
205
|
[converter, state, metadata],
|
|
124
206
|
);
|
|
125
|
-
const convertedRef = useRef(converted);
|
|
126
|
-
convertedRef.current = converted;
|
|
127
|
-
|
|
128
207
|
const queueItems = converted.queue;
|
|
208
|
+
const steerQueueItems = converted.steerQueue;
|
|
209
|
+
if (steerQueueItems !== undefined && queueItems === undefined)
|
|
210
|
+
throw new Error(
|
|
211
|
+
"statewire: the converter's steerQueue projection requires the queue projection",
|
|
212
|
+
);
|
|
129
213
|
const [queueSeam] = useState(() => ({
|
|
130
214
|
present: queueItems !== undefined,
|
|
131
215
|
}));
|
|
@@ -138,34 +222,32 @@ const useStatewireThread = (
|
|
|
138
222
|
|
|
139
223
|
const queue = useMemo(
|
|
140
224
|
(): ExternalThreadQueueAdapter | undefined =>
|
|
141
|
-
queueItems
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (!command) return;
|
|
146
|
-
dispatch(
|
|
147
|
-
convertedRef.current.isRunning
|
|
148
|
-
? { ...command, type: "enqueue-message", steer }
|
|
149
|
-
: command,
|
|
150
|
-
);
|
|
151
|
-
},
|
|
152
|
-
steer: (queueItemId) =>
|
|
153
|
-
dispatch({ type: "steer-queue-item", queueItemId }),
|
|
154
|
-
remove: (queueItemId) =>
|
|
155
|
-
dispatch({ type: "remove-queue-item", queueItemId }),
|
|
156
|
-
clear: (reason) => dispatch({ type: "clear-queue", reason }),
|
|
157
|
-
},
|
|
158
|
-
[queueItems, dispatch],
|
|
225
|
+
queueItems
|
|
226
|
+
? createRunQueueAdapter(queueItems, steerQueueItems, dispatch)
|
|
227
|
+
: undefined,
|
|
228
|
+
[queueItems, steerQueueItems, dispatch],
|
|
159
229
|
);
|
|
160
230
|
|
|
231
|
+
const inputRequests = converted.inputRequests;
|
|
232
|
+
const [inputSeam] = useState(() => ({
|
|
233
|
+
present: inputRequests !== undefined,
|
|
234
|
+
}));
|
|
235
|
+
if (inputSeam.present !== (inputRequests !== undefined))
|
|
236
|
+
throw new Error(
|
|
237
|
+
"statewire: the converter's inputRequests projection must be stable — it was " +
|
|
238
|
+
(inputSeam.present ? "defined" : "undefined") +
|
|
239
|
+
" on first convert and changed since",
|
|
240
|
+
);
|
|
241
|
+
|
|
161
242
|
const extras = useMemo(
|
|
162
243
|
(): AssistantTransportExtras => ({
|
|
163
244
|
[symbolAssistantTransportExtras]: true,
|
|
164
245
|
sendCommand: dispatch,
|
|
165
246
|
reconnect: connection.reconnect,
|
|
166
247
|
state,
|
|
248
|
+
...(inputRequests !== undefined && { inputRequests }),
|
|
167
249
|
}),
|
|
168
|
-
[dispatch, connection.reconnect, state],
|
|
250
|
+
[dispatch, connection.reconnect, state, inputRequests],
|
|
169
251
|
);
|
|
170
252
|
|
|
171
253
|
const handleAppend = (message: AppendMessage) => {
|
|
@@ -174,6 +256,21 @@ const useStatewireThread = (
|
|
|
174
256
|
dispatch(command);
|
|
175
257
|
};
|
|
176
258
|
|
|
259
|
+
const handleReload = (parentId: string | null) => {
|
|
260
|
+
dispatch(resolveReloadCommand(converted.messages, parentId));
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const handleEdit = (message: AppendMessage) => {
|
|
264
|
+
if (!queueItems) return handleAppend(message);
|
|
265
|
+
if (message.sourceId == null)
|
|
266
|
+
throw new Error("statewire: run/edit requires a sourceId");
|
|
267
|
+
dispatch({
|
|
268
|
+
type: "run/edit",
|
|
269
|
+
message: toWireMessage(message, crypto.randomUUID()),
|
|
270
|
+
sourceId: message.sourceId,
|
|
271
|
+
});
|
|
272
|
+
};
|
|
273
|
+
|
|
177
274
|
return useResource(
|
|
178
275
|
ExternalThread({
|
|
179
276
|
messages: converted.messages as readonly ExternalThreadMessage[],
|
|
@@ -184,19 +281,46 @@ const useStatewireThread = (
|
|
|
184
281
|
attachmentAdapter: options.adapters?.attachments,
|
|
185
282
|
onNew: handleAppend,
|
|
186
283
|
...(queue && { queue }),
|
|
187
|
-
...(options.capabilities?.edit && { onEdit:
|
|
284
|
+
...(options.capabilities?.edit && { onEdit: handleEdit }),
|
|
285
|
+
...(options.capabilities?.continue && {
|
|
286
|
+
onResume: () => dispatch({ type: "run/continue" }),
|
|
287
|
+
}),
|
|
288
|
+
...(options.capabilities?.reload && { onReload: handleReload }),
|
|
289
|
+
...(inputRequests !== undefined && {
|
|
290
|
+
onAddToolResult: (toolOptions: AddToolResultOptions) => {
|
|
291
|
+
const inputCommand = resolveToolResultInput(
|
|
292
|
+
inputRequests,
|
|
293
|
+
toolOptions,
|
|
294
|
+
);
|
|
295
|
+
dispatch(
|
|
296
|
+
inputCommand ?? {
|
|
297
|
+
type: "add-tool-result",
|
|
298
|
+
toolCallId: toolOptions.toolCallId,
|
|
299
|
+
toolName: toolOptions.toolName,
|
|
300
|
+
result: toolOptions.result,
|
|
301
|
+
isError: toolOptions.isError,
|
|
302
|
+
...(toolOptions.artifact !== undefined && {
|
|
303
|
+
artifact: toolOptions.artifact,
|
|
304
|
+
}),
|
|
305
|
+
...(toolOptions.modelContent !== undefined && {
|
|
306
|
+
modelContent: toolOptions.modelContent,
|
|
307
|
+
}),
|
|
308
|
+
},
|
|
309
|
+
);
|
|
310
|
+
},
|
|
311
|
+
onRespondToToolApproval: (approval: RespondToToolApprovalOptions) =>
|
|
312
|
+
dispatch(resolveToolApprovalInput(inputRequests, approval)),
|
|
313
|
+
}),
|
|
188
314
|
onCancel: () => {
|
|
189
|
-
const payload = latest.current.
|
|
315
|
+
const payload = latest.current.stopPayload?.();
|
|
190
316
|
if (
|
|
191
317
|
payload !== undefined &&
|
|
192
318
|
(typeof payload !== "object" ||
|
|
193
319
|
payload === null ||
|
|
194
320
|
Array.isArray(payload))
|
|
195
321
|
)
|
|
196
|
-
throw new Error(
|
|
197
|
-
|
|
198
|
-
);
|
|
199
|
-
void commands["cancel"]!(
|
|
322
|
+
throw new Error("statewire: stopPayload must return a plain object");
|
|
323
|
+
void commands["run/stop"]!(
|
|
200
324
|
...(payload === undefined ? [] : [payload]),
|
|
201
325
|
).catch((error: Error) => {
|
|
202
326
|
latest.current.onError?.(error);
|
|
@@ -214,9 +338,126 @@ export const StatewireThread = <T>(
|
|
|
214
338
|
export namespace StatewireThread {
|
|
215
339
|
export type Transport<T> = ResourceElement<Statewire<T | undefined, any>>;
|
|
216
340
|
|
|
341
|
+
export type TextPart = {
|
|
342
|
+
readonly type: "text";
|
|
343
|
+
readonly text: string;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
export type FilePart = {
|
|
347
|
+
readonly type: "file";
|
|
348
|
+
readonly mediaType: string;
|
|
349
|
+
readonly url: string;
|
|
350
|
+
readonly filename?: string;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
export type UserMessage = {
|
|
354
|
+
readonly id: string;
|
|
355
|
+
readonly role: "user";
|
|
356
|
+
readonly parts: readonly (TextPart | FilePart)[];
|
|
357
|
+
readonly metadata?: Record<string, unknown>;
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
/** Queue placement fields, flat on the command. Absent is load-bearing: it means default placement; `null` names a queue boundary. */
|
|
361
|
+
export type Placement = {
|
|
362
|
+
readonly insertAfter?: string | null;
|
|
363
|
+
readonly insertBefore?: string | null;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
export type RunEnqueueCommand = {
|
|
367
|
+
readonly type: "run/enqueue";
|
|
368
|
+
} & ({ readonly message: UserMessage } | { readonly messageId: string }) &
|
|
369
|
+
Placement;
|
|
370
|
+
|
|
371
|
+
export type RunSteerCommand = {
|
|
372
|
+
readonly type: "run/steer";
|
|
373
|
+
} & ({ readonly message: UserMessage } | { readonly messageId: string }) &
|
|
374
|
+
Placement;
|
|
375
|
+
|
|
376
|
+
export type RunDequeueCommand = {
|
|
377
|
+
readonly type: "run/dequeue";
|
|
378
|
+
readonly messageId: string;
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
export type RunEditCommand = {
|
|
382
|
+
readonly type: "run/edit";
|
|
383
|
+
readonly message: UserMessage;
|
|
384
|
+
readonly sourceId: string;
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
export type RunContinueCommand = {
|
|
388
|
+
readonly type: "run/continue";
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
export type ToolCallInputRequest = {
|
|
392
|
+
readonly type: "tool-call";
|
|
393
|
+
readonly id: string;
|
|
394
|
+
readonly toolCallId: string;
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
export type ToolApprovalInputRequest = {
|
|
398
|
+
readonly type: "tool-approval";
|
|
399
|
+
readonly id: string;
|
|
400
|
+
readonly toolCallId: string;
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
export type CustomInputRequest = {
|
|
404
|
+
readonly type: string;
|
|
405
|
+
readonly id: string;
|
|
406
|
+
readonly [key: string]: ReadonlyJSONValue | undefined;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/** A host request parking the run until answered; open union for custom types. */
|
|
410
|
+
export type InputRequest =
|
|
411
|
+
| ToolCallInputRequest
|
|
412
|
+
| ToolApprovalInputRequest
|
|
413
|
+
| CustomInputRequest;
|
|
414
|
+
|
|
415
|
+
export type ToolCallInputResponse = {
|
|
416
|
+
readonly output: ReadonlyJSONValue;
|
|
417
|
+
/** Default false; when true, `output` carries the error payload. */
|
|
418
|
+
readonly isError?: boolean;
|
|
419
|
+
readonly artifact?: ReadonlyJSONValue;
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
export type ToolApprovalInputResponse = {
|
|
423
|
+
readonly decision: "approve" | "reject" | "edit" | "respond";
|
|
424
|
+
readonly editedArgs?: ReadonlyJSONValue;
|
|
425
|
+
readonly message?: string;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
export type InputResponse = ReadonlyJSONValue;
|
|
429
|
+
|
|
430
|
+
export type RunInputCommand = {
|
|
431
|
+
readonly type: "run/input";
|
|
432
|
+
readonly requestId: string;
|
|
433
|
+
readonly response: InputResponse;
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
/** An input request projected out of the host state with its response echo. */
|
|
437
|
+
export type InputRequestState = InputRequest & {
|
|
438
|
+
readonly response: InputResponse | null;
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
export type RunReloadCommand = {
|
|
442
|
+
readonly type: "run/reload";
|
|
443
|
+
readonly sourceId: string;
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
export type RunCommand =
|
|
447
|
+
| RunEnqueueCommand
|
|
448
|
+
| RunSteerCommand
|
|
449
|
+
| RunDequeueCommand
|
|
450
|
+
| RunEditCommand
|
|
451
|
+
| RunContinueCommand
|
|
452
|
+
| RunReloadCommand
|
|
453
|
+
| RunInputCommand;
|
|
454
|
+
|
|
455
|
+
export type Command = AssistantTransportCommand | RunCommand;
|
|
456
|
+
|
|
457
|
+
/** A queued message projected out of the host state, wire shape. */
|
|
217
458
|
export type QueueItem = {
|
|
218
459
|
readonly id: string;
|
|
219
|
-
readonly
|
|
460
|
+
readonly parts: readonly (TextPart | FilePart)[];
|
|
220
461
|
};
|
|
221
462
|
|
|
222
463
|
export type State = {
|
|
@@ -224,16 +465,25 @@ export namespace StatewireThread {
|
|
|
224
465
|
readonly state?: ReadonlyJSONValue;
|
|
225
466
|
readonly isRunning: boolean;
|
|
226
467
|
/**
|
|
227
|
-
*
|
|
228
|
-
* (even empty) turns on the queue capability: sends
|
|
229
|
-
* `
|
|
230
|
-
* Presence must be stable across conversions.
|
|
468
|
+
* The host's regular queue lane projected out of the state. Returning it
|
|
469
|
+
* (even empty) turns on the queue capability: sends become `run/enqueue`
|
|
470
|
+
* / `run/steer` commands and item controls send the `run/*` queue
|
|
471
|
+
* commands. Presence must be stable across conversions.
|
|
231
472
|
*/
|
|
232
473
|
readonly queue?: readonly QueueItem[];
|
|
474
|
+
/** The host's steer lane; requires `queue`. Absent means empty. */
|
|
475
|
+
readonly steerQueue?: readonly QueueItem[];
|
|
476
|
+
/**
|
|
477
|
+
* The host's parked input requests projected out of the state. Returning
|
|
478
|
+
* it (even empty) routes tool results and approvals with a matching
|
|
479
|
+
* pending request through `run/input`. Presence must be stable across
|
|
480
|
+
* conversions.
|
|
481
|
+
*/
|
|
482
|
+
readonly inputRequests?: readonly InputRequestState[];
|
|
233
483
|
};
|
|
234
484
|
|
|
235
485
|
export type ConnectionMetadata = {
|
|
236
|
-
pendingCommands:
|
|
486
|
+
pendingCommands: Command[];
|
|
237
487
|
isSending: boolean;
|
|
238
488
|
/** Statewire connection status; local transports are always `live`. */
|
|
239
489
|
connection: StatewireClient.ConnectionState;
|
|
@@ -245,10 +495,10 @@ export namespace StatewireThread {
|
|
|
245
495
|
) => State;
|
|
246
496
|
|
|
247
497
|
/**
|
|
248
|
-
* Payload of the built-in `
|
|
498
|
+
* Payload of the built-in `run/stop` command. `reason` is the one well-known
|
|
249
499
|
* field; apps extend it with their own JSON-serializable fields.
|
|
250
500
|
*/
|
|
251
|
-
export type
|
|
501
|
+
export type StopPayload = {
|
|
252
502
|
readonly reason?: string | undefined;
|
|
253
503
|
readonly [key: string]: ReadonlyJSONValue | undefined;
|
|
254
504
|
};
|
|
@@ -263,12 +513,25 @@ export namespace StatewireThread {
|
|
|
263
513
|
* on connect and whenever it changes. Requires a remote transport.
|
|
264
514
|
*/
|
|
265
515
|
contextSync?: "command-send" | undefined;
|
|
266
|
-
/** Supplies the `
|
|
267
|
-
|
|
516
|
+
/** Supplies the `run/stop` command's payload; omitted, stop sends no payload. */
|
|
517
|
+
stopPayload?: (() => StopPayload) | undefined;
|
|
518
|
+
/**
|
|
519
|
+
* Offered each pending command when a remote transport's session is
|
|
520
|
+
* evicted; return "replay" to re-enqueue it under the rotated client id.
|
|
521
|
+
* A callback set on the transport options is consulted first; this one
|
|
522
|
+
* runs for commands it declined.
|
|
523
|
+
*/
|
|
524
|
+
onCommandSessionLost?:
|
|
525
|
+
| ((command: StatewireClient.SessionLostCommand) => "replay" | void)
|
|
526
|
+
| undefined;
|
|
268
527
|
onError?: ((error: Error) => void) | undefined;
|
|
269
528
|
capabilities?:
|
|
270
529
|
| {
|
|
271
530
|
edit?: boolean | undefined;
|
|
531
|
+
/** The host supports `run/continue` (a continue affordance on aborted runs). */
|
|
532
|
+
continue?: boolean | undefined;
|
|
533
|
+
/** The host supports `run/reload` (regenerating an assistant message). */
|
|
534
|
+
reload?: boolean | undefined;
|
|
272
535
|
}
|
|
273
536
|
| undefined;
|
|
274
537
|
adapters?:
|
|
@@ -45,7 +45,7 @@ export const defaultConverter: AssistantTransportStateConverter<TestState> = (
|
|
|
45
45
|
messages: [
|
|
46
46
|
...state.messages.map(toThreadMessage),
|
|
47
47
|
...meta.pendingCommands.flatMap((c, i) =>
|
|
48
|
-
c.type === "
|
|
48
|
+
c.type === "run/steer" && "message" in c
|
|
49
49
|
? [
|
|
50
50
|
toThreadMessage({
|
|
51
51
|
id: `optimistic-${i}`,
|
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,9 @@ 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
|
+
RunEditCommand,
|
|
17
|
+
RunSteerCommand,
|
|
18
|
+
RunUserMessage,
|
|
17
19
|
AssistantTransportCommand,
|
|
18
20
|
AssistantTransportOptions,
|
|
19
21
|
AssistantTransportStateConverter,
|
|
@@ -41,13 +43,22 @@ type LangGraphState = {
|
|
|
41
43
|
|
|
42
44
|
const INITIAL_STATE: LangGraphState = { messages: [], todos: [] };
|
|
43
45
|
|
|
44
|
-
// Athena's optimistic dedup: each
|
|
46
|
+
// Athena's optimistic dedup: each user-message command gets a stable
|
|
45
47
|
// trackingId; the server echoes it on the human message, which retires the
|
|
46
48
|
// optimistic copy without a flicker frame or a phantom duplicate.
|
|
49
|
+
type UserMessageCommand = (RunSteerCommand | RunEditCommand) & {
|
|
50
|
+
message: RunUserMessage;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const isUserMessageCommand = (
|
|
54
|
+
c: AssistantTransportCommand,
|
|
55
|
+
): c is UserMessageCommand =>
|
|
56
|
+
(c.type === "run/steer" || c.type === "run/edit") && "message" in c;
|
|
57
|
+
|
|
47
58
|
const createConverter = () => {
|
|
48
|
-
const trackingIds = new WeakMap<
|
|
59
|
+
const trackingIds = new WeakMap<object, string>();
|
|
49
60
|
let nextTrackingId = 0;
|
|
50
|
-
const trackingIdOf = (command:
|
|
61
|
+
const trackingIdOf = (command: object) => {
|
|
51
62
|
let id = trackingIds.get(command);
|
|
52
63
|
if (id === undefined) {
|
|
53
64
|
id = `track-${nextTrackingId++}`;
|
|
@@ -108,8 +119,8 @@ const createConverter = () => {
|
|
|
108
119
|
}
|
|
109
120
|
|
|
110
121
|
for (const c of meta.pendingCommands) {
|
|
111
|
-
if (c
|
|
112
|
-
const trackingId = trackingIdOf(c
|
|
122
|
+
if (!isUserMessageCommand(c)) continue;
|
|
123
|
+
const trackingId = trackingIdOf(c);
|
|
113
124
|
const echoed = state.messages.some(
|
|
114
125
|
(m) => m.type === "human" && m.trackingId === trackingId,
|
|
115
126
|
);
|
|
@@ -168,15 +179,13 @@ const mountDeepAgent = () => {
|
|
|
168
179
|
prepareSendCommandsRequest: (body) => ({
|
|
169
180
|
...body,
|
|
170
181
|
commands: body.commands.map((c) =>
|
|
171
|
-
c
|
|
182
|
+
isUserMessageCommand(c)
|
|
172
183
|
? {
|
|
173
184
|
...c,
|
|
174
|
-
trackingId: trackingIdOf(c
|
|
185
|
+
trackingId: trackingIdOf(c),
|
|
175
186
|
message: {
|
|
176
|
-
...
|
|
177
|
-
parts:
|
|
178
|
-
(p) => p.type !== "image",
|
|
179
|
-
),
|
|
187
|
+
...c.message,
|
|
188
|
+
parts: c.message.parts.filter((p) => p.type !== "file"),
|
|
180
189
|
},
|
|
181
190
|
}
|
|
182
191
|
: c,
|
|
@@ -197,7 +206,7 @@ const mountDeepAgent = () => {
|
|
|
197
206
|
messages: [
|
|
198
207
|
...state.messages,
|
|
199
208
|
...commands.flatMap((c, i) =>
|
|
200
|
-
c
|
|
209
|
+
isUserMessageCommand(c)
|
|
201
210
|
? [
|
|
202
211
|
{
|
|
203
212
|
id: `reattached-${i}`,
|
|
@@ -205,7 +214,7 @@ const mountDeepAgent = () => {
|
|
|
205
214
|
content:
|
|
206
215
|
c.message.parts.find((p) => p.type === "text")?.text ??
|
|
207
216
|
"",
|
|
208
|
-
trackingId: trackingIdOf(c
|
|
217
|
+
trackingId: trackingIdOf(c),
|
|
209
218
|
},
|
|
210
219
|
]
|
|
211
220
|
: [],
|
|
@@ -305,17 +314,17 @@ describe("deep-agent e2e", () => {
|
|
|
305
314
|
expect(headers.get("X-Thread-Id")).toBe("thread-42");
|
|
306
315
|
expect(r1.body.backendPath).toBe("deep-agent");
|
|
307
316
|
expect(r1.body.state).toEqual(INITIAL_STATE);
|
|
308
|
-
// prepareSendCommandsRequest stripped the
|
|
317
|
+
// prepareSendCommandsRequest stripped the file part (the converted image
|
|
318
|
+
// attachment), kept trackingId.
|
|
309
319
|
expect(r1.body.commands).toEqual([
|
|
310
320
|
{
|
|
311
|
-
type: "
|
|
321
|
+
type: "run/steer",
|
|
312
322
|
trackingId: "track-0",
|
|
313
323
|
message: {
|
|
324
|
+
id: expect.any(String),
|
|
314
325
|
role: "user",
|
|
315
326
|
parts: [{ type: "text", text: "what's the weather in SF?" }],
|
|
316
327
|
},
|
|
317
|
-
parentId: null,
|
|
318
|
-
sourceId: null,
|
|
319
328
|
},
|
|
320
329
|
]);
|
|
321
330
|
|
|
@@ -547,17 +556,17 @@ describe("deep-agent e2e", () => {
|
|
|
547
556
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
548
557
|
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
549
558
|
{
|
|
550
|
-
type: "
|
|
559
|
+
type: "run/edit",
|
|
551
560
|
trackingId: "track-0",
|
|
552
561
|
message: {
|
|
562
|
+
id: expect.any(String),
|
|
553
563
|
role: "user",
|
|
554
564
|
parts: [{ type: "text", text: "better question" }],
|
|
555
565
|
},
|
|
556
|
-
parentId: "a1",
|
|
557
566
|
sourceId: "h2",
|
|
558
567
|
},
|
|
559
568
|
]);
|
|
560
|
-
expect(fetchMock.requests[0]!.body
|
|
569
|
+
expect("parentId" in fetchMock.requests[0]!.body).toBe(false);
|
|
561
570
|
|
|
562
571
|
// Optimistic edit rides alongside the old branch until the server
|
|
563
572
|
// replaces it wholesale with the new branch.
|
|
@@ -613,7 +622,7 @@ describe("deep-agent e2e", () => {
|
|
|
613
622
|
aui().thread().append("save this message");
|
|
614
623
|
|
|
615
624
|
await waitFor(() => expect(harness.errored).toHaveLength(1));
|
|
616
|
-
expect(harness.errored[0]!.map((c) => c.type)).toEqual(["
|
|
625
|
+
expect(harness.errored[0]!.map((c) => c.type)).toEqual(["run/steer"]);
|
|
617
626
|
|
|
618
627
|
// The command left the queue (no longer optimistic/pending) but the
|
|
619
628
|
// re-attached copy keeps it visible.
|