@assistant-ui/react-statewire 0.2.0 → 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 +58 -46
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +56 -5
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +58 -99
- 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 +23 -3
- 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 +116 -6
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/AssistantTransportThread.ts +112 -55
- package/src/StatewireThread.test.tsx +293 -22
- package/src/StatewireThread.ts +163 -118
- package/src/__tests__/testHelpers.tsx +1 -1
- package/src/athena.e2e.test.tsx +36 -27
- package/src/host.ts +23 -11
- package/src/index.ts +22 -2
- package/src/inputRequests.ts +57 -0
- package/src/legacy-diff.test.tsx +19 -12
- package/src/runCommands.ts +146 -0
- package/src/transport.test.tsx +589 -65
- package/src/types.ts +141 -6
package/src/StatewireThread.ts
CHANGED
|
@@ -9,23 +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,
|
|
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";
|
|
28
|
-
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";
|
|
29
40
|
|
|
30
41
|
type AnyCommands = Record<string, (...args: any[]) => unknown>;
|
|
31
42
|
|
|
@@ -40,71 +51,37 @@ const ambientContextOf = (context: ModelContext): Record<string, unknown> => ({
|
|
|
40
51
|
...(context.config !== undefined && { config: context.config }),
|
|
41
52
|
});
|
|
42
53
|
|
|
43
|
-
const
|
|
54
|
+
const convertAppendMessageToCommand = (
|
|
44
55
|
message: AppendMessage,
|
|
45
|
-
|
|
46
|
-
): StatewireThread.UserMessage => {
|
|
56
|
+
): AddMessageCommand | null => {
|
|
47
57
|
if (message.role !== "user")
|
|
48
|
-
throw new Error("
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
const
|
|
58
|
+
throw new Error("Only user messages are supported");
|
|
59
|
+
|
|
60
|
+
const parts: UserMessagePart[] = [];
|
|
61
|
+
const content = [
|
|
52
62
|
...message.content,
|
|
53
63
|
...(message.attachments?.flatMap((a) => a.content) ?? []),
|
|
54
64
|
];
|
|
55
|
-
for (const
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
} else if (
|
|
59
|
-
|
|
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}"`);
|
|
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 });
|
|
69
70
|
}
|
|
70
71
|
}
|
|
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
72
|
|
|
83
|
-
|
|
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
|
-
});
|
|
73
|
+
if (parts.length === 0) return null;
|
|
99
74
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
75
|
+
return {
|
|
76
|
+
type: "add-message",
|
|
77
|
+
message: {
|
|
78
|
+
role: "user",
|
|
79
|
+
parts,
|
|
80
|
+
},
|
|
81
|
+
parentId: message.parentId,
|
|
82
|
+
sourceId: message.sourceId,
|
|
83
|
+
};
|
|
84
|
+
};
|
|
108
85
|
|
|
109
86
|
const withSessionLostFallback = (
|
|
110
87
|
transport: StatewireThread.Transport<any>,
|
|
@@ -243,51 +220,24 @@ const useStatewireThread = (
|
|
|
243
220
|
" on first convert and changed since",
|
|
244
221
|
);
|
|
245
222
|
|
|
246
|
-
const queue = useMemo(
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
type: "run/steer",
|
|
265
|
-
message: toWireMessage(message, crypto.randomUUID()),
|
|
266
|
-
}),
|
|
267
|
-
move: (queueItemId, placement) => {
|
|
268
|
-
const lane = placement.lane ?? laneOf(queueItemId);
|
|
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]);
|
|
223
|
+
const queue = useMemo(
|
|
224
|
+
(): ExternalThreadQueueAdapter | undefined =>
|
|
225
|
+
queueItems
|
|
226
|
+
? createRunQueueAdapter(queueItems, steerQueueItems, dispatch)
|
|
227
|
+
: undefined,
|
|
228
|
+
[queueItems, steerQueueItems, dispatch],
|
|
229
|
+
);
|
|
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
|
+
);
|
|
291
241
|
|
|
292
242
|
const extras = useMemo(
|
|
293
243
|
(): AssistantTransportExtras => ({
|
|
@@ -295,8 +245,9 @@ const useStatewireThread = (
|
|
|
295
245
|
sendCommand: dispatch,
|
|
296
246
|
reconnect: connection.reconnect,
|
|
297
247
|
state,
|
|
248
|
+
...(inputRequests !== undefined && { inputRequests }),
|
|
298
249
|
}),
|
|
299
|
-
[dispatch, connection.reconnect, state],
|
|
250
|
+
[dispatch, connection.reconnect, state, inputRequests],
|
|
300
251
|
);
|
|
301
252
|
|
|
302
253
|
const handleAppend = (message: AppendMessage) => {
|
|
@@ -305,6 +256,10 @@ const useStatewireThread = (
|
|
|
305
256
|
dispatch(command);
|
|
306
257
|
};
|
|
307
258
|
|
|
259
|
+
const handleReload = (parentId: string | null) => {
|
|
260
|
+
dispatch(resolveReloadCommand(converted.messages, parentId));
|
|
261
|
+
};
|
|
262
|
+
|
|
308
263
|
const handleEdit = (message: AppendMessage) => {
|
|
309
264
|
if (!queueItems) return handleAppend(message);
|
|
310
265
|
if (message.sourceId == null)
|
|
@@ -330,18 +285,42 @@ const useStatewireThread = (
|
|
|
330
285
|
...(options.capabilities?.continue && {
|
|
331
286
|
onResume: () => dispatch({ type: "run/continue" }),
|
|
332
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
|
+
}),
|
|
333
314
|
onCancel: () => {
|
|
334
|
-
const payload = latest.current.
|
|
315
|
+
const payload = latest.current.stopPayload?.();
|
|
335
316
|
if (
|
|
336
317
|
payload !== undefined &&
|
|
337
318
|
(typeof payload !== "object" ||
|
|
338
319
|
payload === null ||
|
|
339
320
|
Array.isArray(payload))
|
|
340
321
|
)
|
|
341
|
-
throw new Error(
|
|
342
|
-
|
|
343
|
-
);
|
|
344
|
-
void commands["run/cancel"]!(
|
|
322
|
+
throw new Error("statewire: stopPayload must return a plain object");
|
|
323
|
+
void commands["run/stop"]!(
|
|
345
324
|
...(payload === undefined ? [] : [payload]),
|
|
346
325
|
).catch((error: Error) => {
|
|
347
326
|
latest.current.onError?.(error);
|
|
@@ -409,12 +388,69 @@ export namespace StatewireThread {
|
|
|
409
388
|
readonly type: "run/continue";
|
|
410
389
|
};
|
|
411
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
|
+
|
|
412
446
|
export type RunCommand =
|
|
413
447
|
| RunEnqueueCommand
|
|
414
448
|
| RunSteerCommand
|
|
415
449
|
| RunDequeueCommand
|
|
416
450
|
| RunEditCommand
|
|
417
|
-
| RunContinueCommand
|
|
451
|
+
| RunContinueCommand
|
|
452
|
+
| RunReloadCommand
|
|
453
|
+
| RunInputCommand;
|
|
418
454
|
|
|
419
455
|
export type Command = AssistantTransportCommand | RunCommand;
|
|
420
456
|
|
|
@@ -437,6 +473,13 @@ export namespace StatewireThread {
|
|
|
437
473
|
readonly queue?: readonly QueueItem[];
|
|
438
474
|
/** The host's steer lane; requires `queue`. Absent means empty. */
|
|
439
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[];
|
|
440
483
|
};
|
|
441
484
|
|
|
442
485
|
export type ConnectionMetadata = {
|
|
@@ -452,10 +495,10 @@ export namespace StatewireThread {
|
|
|
452
495
|
) => State;
|
|
453
496
|
|
|
454
497
|
/**
|
|
455
|
-
* Payload of the built-in `run/
|
|
498
|
+
* Payload of the built-in `run/stop` command. `reason` is the one well-known
|
|
456
499
|
* field; apps extend it with their own JSON-serializable fields.
|
|
457
500
|
*/
|
|
458
|
-
export type
|
|
501
|
+
export type StopPayload = {
|
|
459
502
|
readonly reason?: string | undefined;
|
|
460
503
|
readonly [key: string]: ReadonlyJSONValue | undefined;
|
|
461
504
|
};
|
|
@@ -470,8 +513,8 @@ export namespace StatewireThread {
|
|
|
470
513
|
* on connect and whenever it changes. Requires a remote transport.
|
|
471
514
|
*/
|
|
472
515
|
contextSync?: "command-send" | undefined;
|
|
473
|
-
/** Supplies the `run/
|
|
474
|
-
|
|
516
|
+
/** Supplies the `run/stop` command's payload; omitted, stop sends no payload. */
|
|
517
|
+
stopPayload?: (() => StopPayload) | undefined;
|
|
475
518
|
/**
|
|
476
519
|
* Offered each pending command when a remote transport's session is
|
|
477
520
|
* evicted; return "replay" to re-enqueue it under the rotated client id.
|
|
@@ -487,6 +530,8 @@ export namespace StatewireThread {
|
|
|
487
530
|
edit?: boolean | undefined;
|
|
488
531
|
/** The host supports `run/continue` (a continue affordance on aborted runs). */
|
|
489
532
|
continue?: boolean | undefined;
|
|
533
|
+
/** The host supports `run/reload` (regenerating an assistant message). */
|
|
534
|
+
reload?: boolean | undefined;
|
|
490
535
|
}
|
|
491
536
|
| undefined;
|
|
492
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 message
|
|
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
|
|
|
@@ -384,13 +393,13 @@ describe("deep-agent e2e", () => {
|
|
|
384
393
|
});
|
|
385
394
|
servers[0]!.done();
|
|
386
395
|
|
|
387
|
-
// 3 — frontend tool executes and posts tool-result
|
|
396
|
+
// 3 — frontend tool executes and posts add-tool-result in a follow-up run.
|
|
388
397
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
389
398
|
const r2 = fetchMock.requests[1]!;
|
|
390
399
|
expect(r2.url).toBe("https://sync.example.com/api/chat");
|
|
391
400
|
expect(r2.body.commands).toEqual([
|
|
392
401
|
{
|
|
393
|
-
type: "tool-result
|
|
402
|
+
type: "add-tool-result",
|
|
394
403
|
toolCallId: "tc1",
|
|
395
404
|
toolName: "get_weather",
|
|
396
405
|
result: { temp: 20, city: "SF" },
|
|
@@ -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.
|
package/src/host.ts
CHANGED
|
@@ -11,7 +11,6 @@ import type { AssistantTransportStateOperation } from "assistant-stream";
|
|
|
11
11
|
import { asAsyncIterableStream } from "assistant-stream/utils";
|
|
12
12
|
import type { Statewire } from "statewire";
|
|
13
13
|
import {
|
|
14
|
-
CommandExecution,
|
|
15
14
|
useStatewireCommands,
|
|
16
15
|
useStatewireEmit,
|
|
17
16
|
useStatewireState,
|
|
@@ -61,13 +60,18 @@ export type AssistantTransportHostCommands = {
|
|
|
61
60
|
setParentId(parentId: string | null): void;
|
|
62
61
|
cancel(): void;
|
|
63
62
|
resume(): void;
|
|
64
|
-
reload(parentId: string | null): void;
|
|
65
63
|
importState(state: unknown): void;
|
|
66
64
|
};
|
|
67
65
|
|
|
66
|
+
type EntrySettlement = {
|
|
67
|
+
ack: () => void;
|
|
68
|
+
resolve: () => void;
|
|
69
|
+
reject: (error: unknown) => void;
|
|
70
|
+
};
|
|
71
|
+
|
|
68
72
|
type QueueEntry = {
|
|
69
73
|
command: AssistantTransportCommand;
|
|
70
|
-
execution:
|
|
74
|
+
execution: EntrySettlement;
|
|
71
75
|
};
|
|
72
76
|
|
|
73
77
|
type EntryQueue = {
|
|
@@ -367,14 +371,26 @@ const useHostLoop = (
|
|
|
367
371
|
};
|
|
368
372
|
|
|
369
373
|
return {
|
|
370
|
-
send: (
|
|
371
|
-
|
|
374
|
+
send: function (
|
|
375
|
+
this: StatewireHost.Ctx,
|
|
376
|
+
command: AssistantTransportCommand,
|
|
377
|
+
opts?: SendOptions,
|
|
378
|
+
) {
|
|
379
|
+
let resolve!: () => void;
|
|
380
|
+
let reject!: (error: unknown) => void;
|
|
381
|
+
const promise = new Promise<void>((res, rej) => {
|
|
382
|
+
resolve = res;
|
|
383
|
+
reject = rej;
|
|
384
|
+
});
|
|
372
385
|
setQueue({
|
|
373
|
-
queued: [
|
|
386
|
+
queued: [
|
|
387
|
+
...s.queue.queued,
|
|
388
|
+
{ command, execution: { ack: this.ack, resolve, reject } },
|
|
389
|
+
],
|
|
374
390
|
inTransit: s.queue.inTransit,
|
|
375
391
|
});
|
|
376
392
|
if (opts?.schedule !== false) schedule();
|
|
377
|
-
return
|
|
393
|
+
return promise;
|
|
378
394
|
},
|
|
379
395
|
setParentId: (id: string | null) => {
|
|
380
396
|
s.parentId = id;
|
|
@@ -391,10 +407,6 @@ const useHostLoop = (
|
|
|
391
407
|
s.resumeRequested = true;
|
|
392
408
|
schedule();
|
|
393
409
|
},
|
|
394
|
-
reload: (id: string | null) => {
|
|
395
|
-
s.parentId = id;
|
|
396
|
-
schedule();
|
|
397
|
-
},
|
|
398
410
|
importState: replaceAgent,
|
|
399
411
|
};
|
|
400
412
|
};
|
package/src/index.ts
CHANGED
|
@@ -15,13 +15,33 @@ export namespace AssistantTransportThread {
|
|
|
15
15
|
export type State = types.AssistantTransportState;
|
|
16
16
|
export type ConnectionMetadata = types.AssistantTransportConnectionMetadata;
|
|
17
17
|
export type StateConverter<T> = types.AssistantTransportStateConverter<T>;
|
|
18
|
-
export type
|
|
19
|
-
export type
|
|
18
|
+
export type AddMessageCommand = types.AddMessageCommand;
|
|
19
|
+
export type AddToolResultCommand = types.AddToolResultCommand;
|
|
20
|
+
export type RunCommand = types.RunCommand;
|
|
21
|
+
export type RunEnqueueCommand = types.RunEnqueueCommand;
|
|
22
|
+
export type RunSteerCommand = types.RunSteerCommand;
|
|
23
|
+
export type RunDequeueCommand = types.RunDequeueCommand;
|
|
24
|
+
export type RunEditCommand = types.RunEditCommand;
|
|
25
|
+
export type RunContinueCommand = types.RunContinueCommand;
|
|
26
|
+
export type RunReloadCommand = types.RunReloadCommand;
|
|
27
|
+
export type RunInputCommand = types.RunInputCommand;
|
|
28
|
+
export type InputRequest = types.InputRequest;
|
|
29
|
+
export type ToolCallInputRequest = types.ToolCallInputRequest;
|
|
30
|
+
export type ToolApprovalInputRequest = types.ToolApprovalInputRequest;
|
|
31
|
+
export type CustomInputRequest = types.CustomInputRequest;
|
|
32
|
+
export type InputRequestState = types.InputRequestState;
|
|
33
|
+
export type InputResponse = types.InputResponse;
|
|
34
|
+
export type ToolCallInputResponse = types.ToolCallInputResponse;
|
|
35
|
+
export type ToolApprovalInputResponse = types.ToolApprovalInputResponse;
|
|
36
|
+
export type RunUserMessage = types.RunUserMessage;
|
|
37
|
+
export type RunQueueItem = types.RunQueueItem;
|
|
38
|
+
export type RunPlacement = types.RunPlacement;
|
|
20
39
|
export type SendCommandsRequestBody = types.SendCommandsRequestBody;
|
|
21
40
|
export type UserMessage = types.UserMessage;
|
|
22
41
|
export type AssistantMessage = types.AssistantMessage;
|
|
23
42
|
export type UserMessagePart = types.UserMessagePart;
|
|
24
43
|
export type TextPart = types.TextPart;
|
|
44
|
+
export type FilePart = types.FilePart;
|
|
25
45
|
export type ImagePart = types.ImagePart;
|
|
26
46
|
export type HeadersValue = types.HeadersValue;
|
|
27
47
|
}
|