@assistant-ui/react-statewire 0.1.1 → 0.2.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 +2 -2
- package/dist/AssistantTransportThread.d.ts.map +1 -1
- package/dist/AssistantTransportThread.js +14 -6
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +69 -8
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +126 -33
- package/dist/StatewireThread.js.map +1 -1
- package/dist/index.d.ts +3 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/types.d.ts +12 -25
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/AssistantTransportThread.ts +23 -10
- package/src/StatewireThread.test.tsx +320 -65
- package/src/StatewireThread.ts +255 -37
- package/src/__tests__/testHelpers.tsx +1 -1
- package/src/athena.e2e.test.tsx +17 -17
- package/src/index.ts +2 -6
- package/src/legacy-diff.test.tsx +25 -8
- package/src/transport.test.tsx +18 -18
- package/src/types.ts +12 -33
package/src/StatewireThread.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
AttachmentAdapter,
|
|
14
14
|
ExternalThreadQueueAdapter,
|
|
15
15
|
ModelContext,
|
|
16
|
+
QueuePlacement,
|
|
16
17
|
ThreadMessage,
|
|
17
18
|
} from "@assistant-ui/core";
|
|
18
19
|
import { toToolsJSONSchema } from "assistant-stream";
|
|
@@ -39,19 +40,120 @@ const ambientContextOf = (context: ModelContext): Record<string, unknown> => ({
|
|
|
39
40
|
...(context.config !== undefined && { config: context.config }),
|
|
40
41
|
});
|
|
41
42
|
|
|
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
|
+
const withSessionLostFallback = (
|
|
110
|
+
transport: StatewireThread.Transport<any>,
|
|
111
|
+
latest: { current: StatewireThread.Options<any> },
|
|
112
|
+
): StatewireThread.Transport<any> => {
|
|
113
|
+
const [transportOptions, ...rest] = transport.args as [
|
|
114
|
+
Record<string, unknown>,
|
|
115
|
+
...unknown[],
|
|
116
|
+
];
|
|
117
|
+
if (
|
|
118
|
+
rest.length > 0 ||
|
|
119
|
+
typeof transportOptions !== "object" ||
|
|
120
|
+
transportOptions === null
|
|
121
|
+
) {
|
|
122
|
+
throw new Error(
|
|
123
|
+
"statewire: onCommandSessionLost requires a transport element that takes a single options object",
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
const transportLevel = transportOptions.onCommandSessionLost as
|
|
127
|
+
| ((command: StatewireClient.SessionLostCommand) => "replay" | void)
|
|
128
|
+
| undefined;
|
|
129
|
+
return {
|
|
130
|
+
...transport,
|
|
131
|
+
args: [
|
|
132
|
+
{
|
|
133
|
+
...transportOptions,
|
|
134
|
+
onCommandSessionLost: (
|
|
135
|
+
command: StatewireClient.SessionLostCommand,
|
|
136
|
+
): "replay" | void =>
|
|
137
|
+
transportLevel?.(command) ??
|
|
138
|
+
latest.current.onCommandSessionLost?.(command),
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
42
144
|
const useStatewireThread = (
|
|
43
145
|
options: StatewireThread.Options<any>,
|
|
44
146
|
): ClientOutput<"thread"> => {
|
|
45
147
|
const latest = useRef(options);
|
|
46
148
|
latest.current = options;
|
|
47
149
|
|
|
48
|
-
const [pending, setPending] = useState<readonly
|
|
150
|
+
const [pending, setPending] = useState<readonly StatewireThread.Command[]>(
|
|
49
151
|
[],
|
|
50
152
|
);
|
|
51
153
|
const [retire] = useState(
|
|
52
154
|
() => (command: unknown) =>
|
|
53
155
|
setPending((prev) =>
|
|
54
|
-
prev.includes(command as
|
|
156
|
+
prev.includes(command as StatewireThread.Command)
|
|
55
157
|
? prev.filter((c) => c !== command)
|
|
56
158
|
: prev,
|
|
57
159
|
),
|
|
@@ -63,7 +165,10 @@ const useStatewireThread = (
|
|
|
63
165
|
connection,
|
|
64
166
|
setContext,
|
|
65
167
|
} = useStatewire<unknown, AnyCommands>({
|
|
66
|
-
transport:
|
|
168
|
+
transport:
|
|
169
|
+
options.onCommandSessionLost === undefined
|
|
170
|
+
? options.transport
|
|
171
|
+
: withSessionLostFallback(options.transport, latest),
|
|
67
172
|
onCommandChange: (update) => {
|
|
68
173
|
if (update.status === "acked" || update.status === "settled")
|
|
69
174
|
retire(update.params[0]);
|
|
@@ -99,7 +204,7 @@ const useStatewireThread = (
|
|
|
99
204
|
}, [contextSync, clientRef, syncState]);
|
|
100
205
|
|
|
101
206
|
const dispatch = useCallback(
|
|
102
|
-
(command:
|
|
207
|
+
(command: StatewireThread.Command) => {
|
|
103
208
|
setPending((prev) => [...prev, command]);
|
|
104
209
|
void commands[command.type]!(command).catch((error: Error) => {
|
|
105
210
|
retire(command);
|
|
@@ -122,10 +227,12 @@ const useStatewireThread = (
|
|
|
122
227
|
() => converter(state, metadata),
|
|
123
228
|
[converter, state, metadata],
|
|
124
229
|
);
|
|
125
|
-
const convertedRef = useRef(converted);
|
|
126
|
-
convertedRef.current = converted;
|
|
127
|
-
|
|
128
230
|
const queueItems = converted.queue;
|
|
231
|
+
const steerQueueItems = converted.steerQueue;
|
|
232
|
+
if (steerQueueItems !== undefined && queueItems === undefined)
|
|
233
|
+
throw new Error(
|
|
234
|
+
"statewire: the converter's steerQueue projection requires the queue projection",
|
|
235
|
+
);
|
|
129
236
|
const [queueSeam] = useState(() => ({
|
|
130
237
|
present: queueItems !== undefined,
|
|
131
238
|
}));
|
|
@@ -136,27 +243,51 @@ const useStatewireThread = (
|
|
|
136
243
|
" on first convert and changed since",
|
|
137
244
|
);
|
|
138
245
|
|
|
139
|
-
const queue = useMemo(
|
|
140
|
-
()
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
246
|
+
const queue = useMemo((): ExternalThreadQueueAdapter | undefined => {
|
|
247
|
+
if (!queueItems) return undefined;
|
|
248
|
+
const laneOf = (queueItemId: string): "queue" | "steer" => {
|
|
249
|
+
if (steerQueueItems?.some((item) => item.id === queueItemId))
|
|
250
|
+
return "steer";
|
|
251
|
+
if (queueItems.some((item) => item.id === queueItemId)) return "queue";
|
|
252
|
+
throw new Error(`statewire: unknown queue item "${queueItemId}"`);
|
|
253
|
+
};
|
|
254
|
+
return {
|
|
255
|
+
items: queueItems.map(toQueueItemState),
|
|
256
|
+
steerItems: (steerQueueItems ?? []).map(toQueueItemState),
|
|
257
|
+
enqueue: (message) =>
|
|
258
|
+
dispatch({
|
|
259
|
+
type: "run/enqueue",
|
|
260
|
+
message: toWireMessage(message, crypto.randomUUID()),
|
|
261
|
+
}),
|
|
262
|
+
steer: (message) =>
|
|
263
|
+
dispatch({
|
|
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
|
+
);
|
|
157
278
|
},
|
|
158
|
-
|
|
159
|
-
|
|
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]);
|
|
160
291
|
|
|
161
292
|
const extras = useMemo(
|
|
162
293
|
(): AssistantTransportExtras => ({
|
|
@@ -174,6 +305,17 @@ const useStatewireThread = (
|
|
|
174
305
|
dispatch(command);
|
|
175
306
|
};
|
|
176
307
|
|
|
308
|
+
const handleEdit = (message: AppendMessage) => {
|
|
309
|
+
if (!queueItems) return handleAppend(message);
|
|
310
|
+
if (message.sourceId == null)
|
|
311
|
+
throw new Error("statewire: run/edit requires a sourceId");
|
|
312
|
+
dispatch({
|
|
313
|
+
type: "run/edit",
|
|
314
|
+
message: toWireMessage(message, crypto.randomUUID()),
|
|
315
|
+
sourceId: message.sourceId,
|
|
316
|
+
});
|
|
317
|
+
};
|
|
318
|
+
|
|
177
319
|
return useResource(
|
|
178
320
|
ExternalThread({
|
|
179
321
|
messages: converted.messages as readonly ExternalThreadMessage[],
|
|
@@ -184,7 +326,10 @@ const useStatewireThread = (
|
|
|
184
326
|
attachmentAdapter: options.adapters?.attachments,
|
|
185
327
|
onNew: handleAppend,
|
|
186
328
|
...(queue && { queue }),
|
|
187
|
-
...(options.capabilities?.edit && { onEdit:
|
|
329
|
+
...(options.capabilities?.edit && { onEdit: handleEdit }),
|
|
330
|
+
...(options.capabilities?.continue && {
|
|
331
|
+
onResume: () => dispatch({ type: "run/continue" }),
|
|
332
|
+
}),
|
|
188
333
|
onCancel: () => {
|
|
189
334
|
const payload = latest.current.cancelPayload?.();
|
|
190
335
|
if (
|
|
@@ -196,7 +341,7 @@ const useStatewireThread = (
|
|
|
196
341
|
throw new Error(
|
|
197
342
|
"statewire: cancelPayload must return a plain object",
|
|
198
343
|
);
|
|
199
|
-
void commands["cancel"]!(
|
|
344
|
+
void commands["run/cancel"]!(
|
|
200
345
|
...(payload === undefined ? [] : [payload]),
|
|
201
346
|
).catch((error: Error) => {
|
|
202
347
|
latest.current.onError?.(error);
|
|
@@ -214,9 +359,69 @@ export const StatewireThread = <T>(
|
|
|
214
359
|
export namespace StatewireThread {
|
|
215
360
|
export type Transport<T> = ResourceElement<Statewire<T | undefined, any>>;
|
|
216
361
|
|
|
362
|
+
export type TextPart = {
|
|
363
|
+
readonly type: "text";
|
|
364
|
+
readonly text: string;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
export type FilePart = {
|
|
368
|
+
readonly type: "file";
|
|
369
|
+
readonly mediaType: string;
|
|
370
|
+
readonly url: string;
|
|
371
|
+
readonly filename?: string;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
export type UserMessage = {
|
|
375
|
+
readonly id: string;
|
|
376
|
+
readonly role: "user";
|
|
377
|
+
readonly parts: readonly (TextPart | FilePart)[];
|
|
378
|
+
readonly metadata?: Record<string, unknown>;
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
/** Queue placement fields, flat on the command. Absent is load-bearing: it means default placement; `null` names a queue boundary. */
|
|
382
|
+
export type Placement = {
|
|
383
|
+
readonly insertAfter?: string | null;
|
|
384
|
+
readonly insertBefore?: string | null;
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
export type RunEnqueueCommand = {
|
|
388
|
+
readonly type: "run/enqueue";
|
|
389
|
+
} & ({ readonly message: UserMessage } | { readonly messageId: string }) &
|
|
390
|
+
Placement;
|
|
391
|
+
|
|
392
|
+
export type RunSteerCommand = {
|
|
393
|
+
readonly type: "run/steer";
|
|
394
|
+
} & ({ readonly message: UserMessage } | { readonly messageId: string }) &
|
|
395
|
+
Placement;
|
|
396
|
+
|
|
397
|
+
export type RunDequeueCommand = {
|
|
398
|
+
readonly type: "run/dequeue";
|
|
399
|
+
readonly messageId: string;
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
export type RunEditCommand = {
|
|
403
|
+
readonly type: "run/edit";
|
|
404
|
+
readonly message: UserMessage;
|
|
405
|
+
readonly sourceId: string;
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
export type RunContinueCommand = {
|
|
409
|
+
readonly type: "run/continue";
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
export type RunCommand =
|
|
413
|
+
| RunEnqueueCommand
|
|
414
|
+
| RunSteerCommand
|
|
415
|
+
| RunDequeueCommand
|
|
416
|
+
| RunEditCommand
|
|
417
|
+
| RunContinueCommand;
|
|
418
|
+
|
|
419
|
+
export type Command = AssistantTransportCommand | RunCommand;
|
|
420
|
+
|
|
421
|
+
/** A queued message projected out of the host state, wire shape. */
|
|
217
422
|
export type QueueItem = {
|
|
218
423
|
readonly id: string;
|
|
219
|
-
readonly
|
|
424
|
+
readonly parts: readonly (TextPart | FilePart)[];
|
|
220
425
|
};
|
|
221
426
|
|
|
222
427
|
export type State = {
|
|
@@ -224,16 +429,18 @@ export namespace StatewireThread {
|
|
|
224
429
|
readonly state?: ReadonlyJSONValue;
|
|
225
430
|
readonly isRunning: boolean;
|
|
226
431
|
/**
|
|
227
|
-
*
|
|
228
|
-
* (even empty) turns on the queue capability: sends
|
|
229
|
-
* `
|
|
230
|
-
* Presence must be stable across conversions.
|
|
432
|
+
* The host's regular queue lane projected out of the state. Returning it
|
|
433
|
+
* (even empty) turns on the queue capability: sends become `run/enqueue`
|
|
434
|
+
* / `run/steer` commands and item controls send the `run/*` queue
|
|
435
|
+
* commands. Presence must be stable across conversions.
|
|
231
436
|
*/
|
|
232
437
|
readonly queue?: readonly QueueItem[];
|
|
438
|
+
/** The host's steer lane; requires `queue`. Absent means empty. */
|
|
439
|
+
readonly steerQueue?: readonly QueueItem[];
|
|
233
440
|
};
|
|
234
441
|
|
|
235
442
|
export type ConnectionMetadata = {
|
|
236
|
-
pendingCommands:
|
|
443
|
+
pendingCommands: Command[];
|
|
237
444
|
isSending: boolean;
|
|
238
445
|
/** Statewire connection status; local transports are always `live`. */
|
|
239
446
|
connection: StatewireClient.ConnectionState;
|
|
@@ -245,7 +452,7 @@ export namespace StatewireThread {
|
|
|
245
452
|
) => State;
|
|
246
453
|
|
|
247
454
|
/**
|
|
248
|
-
* Payload of the built-in `cancel` command. `reason` is the one well-known
|
|
455
|
+
* Payload of the built-in `run/cancel` command. `reason` is the one well-known
|
|
249
456
|
* field; apps extend it with their own JSON-serializable fields.
|
|
250
457
|
*/
|
|
251
458
|
export type CancelPayload = {
|
|
@@ -263,12 +470,23 @@ export namespace StatewireThread {
|
|
|
263
470
|
* on connect and whenever it changes. Requires a remote transport.
|
|
264
471
|
*/
|
|
265
472
|
contextSync?: "command-send" | undefined;
|
|
266
|
-
/** Supplies the `cancel` command's payload; omitted, cancel sends no payload. */
|
|
473
|
+
/** Supplies the `run/cancel` command's payload; omitted, cancel sends no payload. */
|
|
267
474
|
cancelPayload?: (() => CancelPayload) | undefined;
|
|
475
|
+
/**
|
|
476
|
+
* Offered each pending command when a remote transport's session is
|
|
477
|
+
* evicted; return "replay" to re-enqueue it under the rotated client id.
|
|
478
|
+
* A callback set on the transport options is consulted first; this one
|
|
479
|
+
* runs for commands it declined.
|
|
480
|
+
*/
|
|
481
|
+
onCommandSessionLost?:
|
|
482
|
+
| ((command: StatewireClient.SessionLostCommand) => "replay" | void)
|
|
483
|
+
| undefined;
|
|
268
484
|
onError?: ((error: Error) => void) | undefined;
|
|
269
485
|
capabilities?:
|
|
270
486
|
| {
|
|
271
487
|
edit?: boolean | undefined;
|
|
488
|
+
/** The host supports `run/continue` (a continue affordance on aborted runs). */
|
|
489
|
+
continue?: boolean | undefined;
|
|
272
490
|
}
|
|
273
491
|
| undefined;
|
|
274
492
|
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 === "add
|
|
48
|
+
c.type === "message/add" && c.message.role === "user"
|
|
49
49
|
? [
|
|
50
50
|
toThreadMessage({
|
|
51
51
|
id: `optimistic-${i}`,
|
package/src/athena.e2e.test.tsx
CHANGED
|
@@ -13,7 +13,7 @@ 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
|
+
MessageAddCommand,
|
|
17
17
|
AssistantTransportCommand,
|
|
18
18
|
AssistantTransportOptions,
|
|
19
19
|
AssistantTransportStateConverter,
|
|
@@ -41,13 +41,13 @@ type LangGraphState = {
|
|
|
41
41
|
|
|
42
42
|
const INITIAL_STATE: LangGraphState = { messages: [], todos: [] };
|
|
43
43
|
|
|
44
|
-
// Athena's optimistic dedup: each add
|
|
44
|
+
// Athena's optimistic dedup: each message/add command gets a stable
|
|
45
45
|
// trackingId; the server echoes it on the human message, which retires the
|
|
46
46
|
// optimistic copy without a flicker frame or a phantom duplicate.
|
|
47
47
|
const createConverter = () => {
|
|
48
|
-
const trackingIds = new WeakMap<
|
|
48
|
+
const trackingIds = new WeakMap<MessageAddCommand, string>();
|
|
49
49
|
let nextTrackingId = 0;
|
|
50
|
-
const trackingIdOf = (command:
|
|
50
|
+
const trackingIdOf = (command: MessageAddCommand) => {
|
|
51
51
|
let id = trackingIds.get(command);
|
|
52
52
|
if (id === undefined) {
|
|
53
53
|
id = `track-${nextTrackingId++}`;
|
|
@@ -108,8 +108,8 @@ const createConverter = () => {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
for (const c of meta.pendingCommands) {
|
|
111
|
-
if (c.type !== "add
|
|
112
|
-
const trackingId = trackingIdOf(c as
|
|
111
|
+
if (c.type !== "message/add" || c.message.role !== "user") continue;
|
|
112
|
+
const trackingId = trackingIdOf(c as MessageAddCommand);
|
|
113
113
|
const echoed = state.messages.some(
|
|
114
114
|
(m) => m.type === "human" && m.trackingId === trackingId,
|
|
115
115
|
);
|
|
@@ -168,13 +168,13 @@ const mountDeepAgent = () => {
|
|
|
168
168
|
prepareSendCommandsRequest: (body) => ({
|
|
169
169
|
...body,
|
|
170
170
|
commands: body.commands.map((c) =>
|
|
171
|
-
c.type === "add
|
|
171
|
+
c.type === "message/add"
|
|
172
172
|
? {
|
|
173
173
|
...c,
|
|
174
|
-
trackingId: trackingIdOf(c as
|
|
174
|
+
trackingId: trackingIdOf(c as MessageAddCommand),
|
|
175
175
|
message: {
|
|
176
|
-
...(c as
|
|
177
|
-
parts: (c as
|
|
176
|
+
...(c as MessageAddCommand).message,
|
|
177
|
+
parts: (c as MessageAddCommand).message.parts.filter(
|
|
178
178
|
(p) => p.type !== "image",
|
|
179
179
|
),
|
|
180
180
|
},
|
|
@@ -197,7 +197,7 @@ const mountDeepAgent = () => {
|
|
|
197
197
|
messages: [
|
|
198
198
|
...state.messages,
|
|
199
199
|
...commands.flatMap((c, i) =>
|
|
200
|
-
c.type === "add
|
|
200
|
+
c.type === "message/add" && c.message.role === "user"
|
|
201
201
|
? [
|
|
202
202
|
{
|
|
203
203
|
id: `reattached-${i}`,
|
|
@@ -205,7 +205,7 @@ const mountDeepAgent = () => {
|
|
|
205
205
|
content:
|
|
206
206
|
c.message.parts.find((p) => p.type === "text")?.text ??
|
|
207
207
|
"",
|
|
208
|
-
trackingId: trackingIdOf(c as
|
|
208
|
+
trackingId: trackingIdOf(c as MessageAddCommand),
|
|
209
209
|
},
|
|
210
210
|
]
|
|
211
211
|
: [],
|
|
@@ -308,7 +308,7 @@ describe("deep-agent e2e", () => {
|
|
|
308
308
|
// prepareSendCommandsRequest stripped the image part, kept trackingId.
|
|
309
309
|
expect(r1.body.commands).toEqual([
|
|
310
310
|
{
|
|
311
|
-
type: "add
|
|
311
|
+
type: "message/add",
|
|
312
312
|
trackingId: "track-0",
|
|
313
313
|
message: {
|
|
314
314
|
role: "user",
|
|
@@ -384,13 +384,13 @@ describe("deep-agent e2e", () => {
|
|
|
384
384
|
});
|
|
385
385
|
servers[0]!.done();
|
|
386
386
|
|
|
387
|
-
// 3 — frontend tool executes and posts
|
|
387
|
+
// 3 — frontend tool executes and posts tool-result/add in a follow-up run.
|
|
388
388
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
389
389
|
const r2 = fetchMock.requests[1]!;
|
|
390
390
|
expect(r2.url).toBe("https://sync.example.com/api/chat");
|
|
391
391
|
expect(r2.body.commands).toEqual([
|
|
392
392
|
{
|
|
393
|
-
type: "
|
|
393
|
+
type: "tool-result/add",
|
|
394
394
|
toolCallId: "tc1",
|
|
395
395
|
toolName: "get_weather",
|
|
396
396
|
result: { temp: 20, city: "SF" },
|
|
@@ -547,7 +547,7 @@ describe("deep-agent e2e", () => {
|
|
|
547
547
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
548
548
|
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
549
549
|
{
|
|
550
|
-
type: "add
|
|
550
|
+
type: "message/add",
|
|
551
551
|
trackingId: "track-0",
|
|
552
552
|
message: {
|
|
553
553
|
role: "user",
|
|
@@ -613,7 +613,7 @@ describe("deep-agent e2e", () => {
|
|
|
613
613
|
aui().thread().append("save this message");
|
|
614
614
|
|
|
615
615
|
await waitFor(() => expect(harness.errored).toHaveLength(1));
|
|
616
|
-
expect(harness.errored[0]!.map((c) => c.type)).toEqual(["add
|
|
616
|
+
expect(harness.errored[0]!.map((c) => c.type)).toEqual(["message/add"]);
|
|
617
617
|
|
|
618
618
|
// The command left the queue (no longer optimistic/pending) but the
|
|
619
619
|
// re-attached copy keeps it visible.
|
package/src/index.ts
CHANGED
|
@@ -15,12 +15,8 @@ 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
|
|
20
|
-
export type EnqueueMessageCommand = types.EnqueueMessageCommand;
|
|
21
|
-
export type SteerQueueItemCommand = types.SteerQueueItemCommand;
|
|
22
|
-
export type RemoveQueueItemCommand = types.RemoveQueueItemCommand;
|
|
23
|
-
export type ClearQueueCommand = types.ClearQueueCommand;
|
|
18
|
+
export type MessageAddCommand = types.MessageAddCommand;
|
|
19
|
+
export type ToolResultAddCommand = types.ToolResultAddCommand;
|
|
24
20
|
export type SendCommandsRequestBody = types.SendCommandsRequestBody;
|
|
25
21
|
export type UserMessage = types.UserMessage;
|
|
26
22
|
export type AssistantMessage = types.AssistantMessage;
|
package/src/legacy-diff.test.tsx
CHANGED
|
@@ -46,7 +46,7 @@ const converter: AssistantTransportStateConverter<TestState> = (
|
|
|
46
46
|
}) as unknown as ExternalThreadMessage,
|
|
47
47
|
),
|
|
48
48
|
...meta.pendingCommands.flatMap((c, i) =>
|
|
49
|
-
c.type === "add
|
|
49
|
+
c.type === "message/add" && c.message.role === "user"
|
|
50
50
|
? [
|
|
51
51
|
{
|
|
52
52
|
id: `optimistic-${i}`,
|
|
@@ -71,6 +71,19 @@ const converter: AssistantTransportStateConverter<TestState> = (
|
|
|
71
71
|
|
|
72
72
|
type RecordedRequest = { url: string; body: Record<string, any> };
|
|
73
73
|
|
|
74
|
+
// deliberate divergence: the namespaced wire rename ("add-message" →
|
|
75
|
+
// "message/add", "add-tool-result" → "tool-result/add") postdates the legacy
|
|
76
|
+
// runtime; legacy bodies are renamed before comparison.
|
|
77
|
+
const WIRE_RENAMES: Record<string, string> = {
|
|
78
|
+
"add-message": "message/add",
|
|
79
|
+
"add-tool-result": "tool-result/add",
|
|
80
|
+
};
|
|
81
|
+
const renameWire = (commands: unknown) =>
|
|
82
|
+
(commands as { type: string }[]).map((c) => ({
|
|
83
|
+
...c,
|
|
84
|
+
type: WIRE_RENAMES[c.type] ?? c.type,
|
|
85
|
+
}));
|
|
86
|
+
|
|
74
87
|
// Scripted server: request N gets transcript N, streamed whole.
|
|
75
88
|
const installScriptedFetch = (transcript: string[][]) => {
|
|
76
89
|
const requests: RecordedRequest[] = [];
|
|
@@ -153,7 +166,7 @@ const observeLegacy = async (): Promise<{
|
|
|
153
166
|
api: "https://example.com/api",
|
|
154
167
|
protocol: "assistant-transport",
|
|
155
168
|
headers: {},
|
|
156
|
-
converter,
|
|
169
|
+
converter: converter as never,
|
|
157
170
|
});
|
|
158
171
|
return (
|
|
159
172
|
<AssistantRuntimeProvider runtime={captured.runtime}>
|
|
@@ -362,8 +375,8 @@ const installToolScriptedFetch = (transcript: string[][]) => {
|
|
|
362
375
|
return requests;
|
|
363
376
|
};
|
|
364
377
|
|
|
365
|
-
// Inferred literal type —
|
|
366
|
-
//
|
|
378
|
+
// Inferred literal type — written against the new options type; the legacy
|
|
379
|
+
// call site casts (the command union diverged with the wire rename).
|
|
367
380
|
const toolOptions = (events: CallbackEvent[]) => ({
|
|
368
381
|
initialState: { messages: [] } as ToolState,
|
|
369
382
|
api: "https://example.com/api",
|
|
@@ -420,7 +433,7 @@ const observeLegacyToolRun = async (): Promise<ToolObserved> => {
|
|
|
420
433
|
} = {};
|
|
421
434
|
const App: FC = () => {
|
|
422
435
|
captured.runtime = useAssistantTransportRuntime<ToolState>(
|
|
423
|
-
toolOptions(events),
|
|
436
|
+
toolOptions(events) as never,
|
|
424
437
|
);
|
|
425
438
|
return (
|
|
426
439
|
<AssistantRuntimeProvider runtime={captured.runtime}>
|
|
@@ -543,7 +556,7 @@ describe("legacy diff", () => {
|
|
|
543
556
|
legacy.requests[0]!.body,
|
|
544
557
|
modern.requests[0]!.body,
|
|
545
558
|
];
|
|
546
|
-
expect(modernBody.commands).toEqual(legacyBody.commands);
|
|
559
|
+
expect(modernBody.commands).toEqual(renameWire(legacyBody.commands));
|
|
547
560
|
expect(modernBody.state).toEqual(legacyBody.state);
|
|
548
561
|
expect(modernBody.system).toEqual(legacyBody.system);
|
|
549
562
|
expect(modernBody.tools).toEqual(legacyBody.tools);
|
|
@@ -563,7 +576,7 @@ describe("legacy diff", () => {
|
|
|
563
576
|
modern.requests[i]!.body,
|
|
564
577
|
];
|
|
565
578
|
expect(modern.requests[i]!.url).toEqual(legacy.requests[i]!.url);
|
|
566
|
-
expect(modernBody.commands).toEqual(legacyBody.commands);
|
|
579
|
+
expect(modernBody.commands).toEqual(renameWire(legacyBody.commands));
|
|
567
580
|
expect(modernBody.state).toEqual(legacyBody.state);
|
|
568
581
|
// deliberate divergence (sw-73): the tool-result follow-up does not
|
|
569
582
|
// repeat the parentId the first run already consumed
|
|
@@ -572,7 +585,11 @@ describe("legacy diff", () => {
|
|
|
572
585
|
expect(modernBody.tools).toEqual(legacyBody.tools);
|
|
573
586
|
}
|
|
574
587
|
|
|
575
|
-
expect(modern.events).toEqual(
|
|
588
|
+
expect(modern.events).toEqual(
|
|
589
|
+
legacy.events.map((e) =>
|
|
590
|
+
"commands" in e ? { ...e, commands: renameWire(e.commands) } : e,
|
|
591
|
+
),
|
|
592
|
+
);
|
|
576
593
|
expect(modern.texts).toEqual(legacy.texts);
|
|
577
594
|
expect(modern.ids).toEqual(legacy.ids);
|
|
578
595
|
expect(modern.isRunning).toEqual(legacy.isRunning);
|