@assistant-ui/react-statewire 0.20.0 → 0.22.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/StatewireThread.d.ts +28 -13
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +102 -47
- package/dist/StatewireThread.js.map +1 -1
- package/dist/runCommands.d.ts +20 -2
- package/dist/runCommands.d.ts.map +1 -1
- package/dist/runCommands.js +44 -1
- package/dist/runCommands.js.map +1 -1
- package/dist/runsOptimistic.d.ts.map +1 -1
- package/dist/runsOptimistic.js +15 -9
- package/dist/runsOptimistic.js.map +1 -1
- package/package.json +2 -2
- package/src/StatewireThread.failed.test.tsx +279 -0
- package/src/StatewireThread.runs.test.tsx +2 -7
- package/src/StatewireThread.test.tsx +104 -3
- package/src/StatewireThread.ts +161 -49
- package/src/runCommands.ts +73 -0
- package/src/runsOptimistic.test.ts +65 -3
- package/src/runsOptimistic.ts +32 -37
|
@@ -563,7 +563,7 @@ describe("StatewireThread", () => {
|
|
|
563
563
|
});
|
|
564
564
|
|
|
565
565
|
await waitFor(() => expect(statuses).toEqual(["live"]));
|
|
566
|
-
await mock.finish("
|
|
566
|
+
await mock.finish("gone");
|
|
567
567
|
await waitFor(() => expect(statuses).toEqual(["live", "gone"]));
|
|
568
568
|
await new Promise((r) => setTimeout(r, 20));
|
|
569
569
|
expect(statuses).toEqual(["live", "gone"]);
|
|
@@ -1168,6 +1168,107 @@ describe("StatewireThread", () => {
|
|
|
1168
1168
|
);
|
|
1169
1169
|
});
|
|
1170
1170
|
|
|
1171
|
+
describe("branch switching", () => {
|
|
1172
|
+
const withBranch = (
|
|
1173
|
+
branch: StatewireThread.BranchMetadata,
|
|
1174
|
+
): StatewireThread.StateConverter<ServerState> => {
|
|
1175
|
+
const branchConverter: StatewireThread.StateConverter<ServerState> = (
|
|
1176
|
+
state,
|
|
1177
|
+
) => ({
|
|
1178
|
+
messages: state.messages.map((m) =>
|
|
1179
|
+
m.id === "u1"
|
|
1180
|
+
? ({
|
|
1181
|
+
...toThreadMessage(m),
|
|
1182
|
+
metadata: { custom: { branch } },
|
|
1183
|
+
} as ReturnType<typeof toThreadMessage>)
|
|
1184
|
+
: toThreadMessage(m),
|
|
1185
|
+
),
|
|
1186
|
+
});
|
|
1187
|
+
return branchConverter;
|
|
1188
|
+
};
|
|
1189
|
+
|
|
1190
|
+
const createBranchServer = (options?: { reject?: boolean }) => {
|
|
1191
|
+
const received: unknown[] = [];
|
|
1192
|
+
const transport = resource(() => {
|
|
1193
|
+
const [state] = useStatewireState<ServerState>(() => ({
|
|
1194
|
+
messages: [
|
|
1195
|
+
{ id: "u1", role: "user", text: "hi" },
|
|
1196
|
+
{ id: "a1", role: "assistant", text: "hello" },
|
|
1197
|
+
],
|
|
1198
|
+
status: "ready",
|
|
1199
|
+
runs: [],
|
|
1200
|
+
}));
|
|
1201
|
+
const commands = useStatewireCommands({
|
|
1202
|
+
"message/switch-branch": (
|
|
1203
|
+
command: StatewireThread.SwitchBranchCommand,
|
|
1204
|
+
) => {
|
|
1205
|
+
received.push(command);
|
|
1206
|
+
if (options?.reject) throw new StatewireReject("host busy");
|
|
1207
|
+
},
|
|
1208
|
+
});
|
|
1209
|
+
return { state, commands };
|
|
1210
|
+
})();
|
|
1211
|
+
return { transport, received };
|
|
1212
|
+
};
|
|
1213
|
+
|
|
1214
|
+
it("projects branch metadata into the picker and dispatches message/switch-branch", async () => {
|
|
1215
|
+
const server = createBranchServer();
|
|
1216
|
+
const { aui } = mountThread({
|
|
1217
|
+
transport: server.transport,
|
|
1218
|
+
converter: withBranch({ id: 2, siblings: [1, 2, 3] }),
|
|
1219
|
+
capabilities: { unstable_switchBranch: true },
|
|
1220
|
+
});
|
|
1221
|
+
|
|
1222
|
+
await waitFor(() => expect(threadTexts(aui)).toEqual(["hi", "hello"]));
|
|
1223
|
+
const messageState = aui().thread().message({ id: "u1" }).getState();
|
|
1224
|
+
expect(messageState.branchNumber).toBe(2);
|
|
1225
|
+
expect(messageState.branchCount).toBe(3);
|
|
1226
|
+
|
|
1227
|
+
aui()
|
|
1228
|
+
.thread()
|
|
1229
|
+
.message({ id: "u1" })
|
|
1230
|
+
.switchToBranch({ position: "previous" });
|
|
1231
|
+
|
|
1232
|
+
await waitFor(() =>
|
|
1233
|
+
expect(server.received).toEqual([
|
|
1234
|
+
{ type: "message/switch-branch", branchId: 1 },
|
|
1235
|
+
]),
|
|
1236
|
+
);
|
|
1237
|
+
});
|
|
1238
|
+
|
|
1239
|
+
it("propagates a rejected message/switch-branch through onError", async () => {
|
|
1240
|
+
const server = createBranchServer({ reject: true });
|
|
1241
|
+
const errors: Error[] = [];
|
|
1242
|
+
const { aui } = mountThread({
|
|
1243
|
+
transport: server.transport,
|
|
1244
|
+
converter: withBranch({ id: 2, siblings: [1, 2, 3] }),
|
|
1245
|
+
capabilities: { unstable_switchBranch: true },
|
|
1246
|
+
onError: (error) => {
|
|
1247
|
+
errors.push(error);
|
|
1248
|
+
},
|
|
1249
|
+
});
|
|
1250
|
+
|
|
1251
|
+
await waitFor(() => expect(threadTexts(aui)).toEqual(["hi", "hello"]));
|
|
1252
|
+
aui().thread().message({ id: "u1" }).switchToBranch({ position: "next" });
|
|
1253
|
+
|
|
1254
|
+
await waitFor(() => expect(errors).toHaveLength(1));
|
|
1255
|
+
expect(errors[0]!.message).toContain("host busy");
|
|
1256
|
+
});
|
|
1257
|
+
|
|
1258
|
+
it("leaves the branch surface off without the capability", async () => {
|
|
1259
|
+
const server = createBranchServer();
|
|
1260
|
+
const { aui } = mountThread({
|
|
1261
|
+
transport: server.transport,
|
|
1262
|
+
converter: withBranch({ id: 2, siblings: [1, 2, 3] }),
|
|
1263
|
+
});
|
|
1264
|
+
|
|
1265
|
+
await waitFor(() => expect(threadTexts(aui)).toEqual(["hi", "hello"]));
|
|
1266
|
+
const messageState = aui().thread().message({ id: "u1" }).getState();
|
|
1267
|
+
expect(messageState.branchNumber).toBe(1);
|
|
1268
|
+
expect(messageState.branchCount).toBe(1);
|
|
1269
|
+
});
|
|
1270
|
+
});
|
|
1271
|
+
|
|
1171
1272
|
describe("connection", () => {
|
|
1172
1273
|
const trackStatuses = (seen: string[]) => {
|
|
1173
1274
|
const trackingConverter: StatewireThread.StateConverter<ServerState> = (
|
|
@@ -1192,7 +1293,7 @@ describe("StatewireThread", () => {
|
|
|
1192
1293
|
await waitFor(() => expect(threadStatus(aui())).toBe("ready"));
|
|
1193
1294
|
expect(seen).toEqual(["connecting", "live"]);
|
|
1194
1295
|
|
|
1195
|
-
await mock.finish("
|
|
1296
|
+
await mock.finish("gone", "taken over");
|
|
1196
1297
|
await waitFor(() => expect(seen.at(-1)).toBe("gone"));
|
|
1197
1298
|
});
|
|
1198
1299
|
|
|
@@ -1216,7 +1317,7 @@ describe("StatewireThread", () => {
|
|
|
1216
1317
|
});
|
|
1217
1318
|
|
|
1218
1319
|
await waitFor(() => expect(threadStatus(aui())).toBe("ready"));
|
|
1219
|
-
await mock.finish("
|
|
1320
|
+
await mock.finish("gone");
|
|
1220
1321
|
await waitFor(() => expect(seen.at(-1)).toBe("gone"));
|
|
1221
1322
|
|
|
1222
1323
|
asStatewireExtras(aui().thread().getState().extras).reconnect();
|
package/src/StatewireThread.ts
CHANGED
|
@@ -30,8 +30,10 @@ import type { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
|
30
30
|
import { useStatewire } from "statewire";
|
|
31
31
|
import type { Statewire, StatewireClient } from "statewire";
|
|
32
32
|
import {
|
|
33
|
+
createBranchCommandAdapter,
|
|
33
34
|
createRunQueueAdapter,
|
|
34
35
|
resolveEditCommand,
|
|
36
|
+
retryCommand,
|
|
35
37
|
resolveReloadCommand,
|
|
36
38
|
sendFields,
|
|
37
39
|
toWireMessage,
|
|
@@ -65,6 +67,30 @@ const RESTING_RUNS: StatewireThread.Runs = {
|
|
|
65
67
|
runId: null,
|
|
66
68
|
queue: [],
|
|
67
69
|
steerQueue: [],
|
|
70
|
+
failed: [],
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** The failed run sends the thread can project: enqueue/steer/edit invocations carrying a message. */
|
|
74
|
+
const failedRunCommand = (
|
|
75
|
+
entry: StatewireClient.FailedCommand,
|
|
76
|
+
):
|
|
77
|
+
| ((
|
|
78
|
+
| StatewireThread.RunEnqueueCommand
|
|
79
|
+
| StatewireThread.RunSteerCommand
|
|
80
|
+
| StatewireThread.RunEditCommand
|
|
81
|
+
) & { message: StatewireThread.UserMessage })
|
|
82
|
+
| undefined => {
|
|
83
|
+
if (
|
|
84
|
+
entry.command !== "run/enqueue" &&
|
|
85
|
+
entry.command !== "run/steer" &&
|
|
86
|
+
entry.command !== "run/edit"
|
|
87
|
+
)
|
|
88
|
+
return undefined;
|
|
89
|
+
const command = entry.params[0] as
|
|
90
|
+
| { message?: StatewireThread.UserMessage }
|
|
91
|
+
| undefined;
|
|
92
|
+
if (command?.message === undefined) return undefined;
|
|
93
|
+
return command as ReturnType<typeof failedRunCommand>;
|
|
68
94
|
};
|
|
69
95
|
|
|
70
96
|
// Derived presentation while the connection can't vouch for progress:
|
|
@@ -110,10 +136,15 @@ const useStatewireThread = (
|
|
|
110
136
|
),
|
|
111
137
|
);
|
|
112
138
|
|
|
113
|
-
const {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
139
|
+
const {
|
|
140
|
+
state,
|
|
141
|
+
commands,
|
|
142
|
+
connection,
|
|
143
|
+
setContext,
|
|
144
|
+
isBusy,
|
|
145
|
+
failed,
|
|
146
|
+
discardFailed,
|
|
147
|
+
} = useStatewire<unknown, AnyCommands>({
|
|
117
148
|
transport: options.transport,
|
|
118
149
|
storage: options.storage,
|
|
119
150
|
initialState: options.initialState,
|
|
@@ -130,8 +161,22 @@ const useStatewireThread = (
|
|
|
130
161
|
onStateChange: (state, ctx) => latest.current.onStateChange?.(state, ctx),
|
|
131
162
|
onConnectionChange: (connection) =>
|
|
132
163
|
latest.current.onConnectionChange?.(connection),
|
|
164
|
+
failureCovered: (state, entry) => {
|
|
165
|
+
const command = failedRunCommand(entry);
|
|
166
|
+
if (command === undefined) return false;
|
|
167
|
+
const messages = (state as { messages?: readonly { id: string }[] })
|
|
168
|
+
.messages;
|
|
169
|
+
return messages?.some((m) => m.id === command.message.id) ?? false;
|
|
170
|
+
},
|
|
133
171
|
});
|
|
134
172
|
|
|
173
|
+
// Failures the thread cannot project stay onError-only, as before the lane.
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
for (const entry of failed) {
|
|
176
|
+
if (failedRunCommand(entry) === undefined) discardFailed(entry);
|
|
177
|
+
}
|
|
178
|
+
}, [failed, discardFailed]);
|
|
179
|
+
|
|
135
180
|
const clientRef = useAssistantClientRef();
|
|
136
181
|
const setContextRef = useRef(setContext);
|
|
137
182
|
setContextRef.current = setContext;
|
|
@@ -196,33 +241,48 @@ const useStatewireThread = (
|
|
|
196
241
|
state as { runs?: readonly StatewireThread.RunState[] } | undefined
|
|
197
242
|
)?.runs;
|
|
198
243
|
const entry = entries?.[0];
|
|
244
|
+
const failedEntries = failed.flatMap(
|
|
245
|
+
(failure): StatewireThread.RunsFailedEntry[] => {
|
|
246
|
+
const command = failedRunCommand(failure);
|
|
247
|
+
return command === undefined
|
|
248
|
+
? []
|
|
249
|
+
: [{ ...command.message, sendFailure: failure.failure }];
|
|
250
|
+
},
|
|
251
|
+
);
|
|
199
252
|
return {
|
|
200
253
|
status: presentStatus(
|
|
201
254
|
entries === undefined ? "loading" : (entry?.status ?? "ready"),
|
|
202
255
|
connection,
|
|
203
256
|
),
|
|
204
257
|
runs:
|
|
205
|
-
entry === undefined
|
|
258
|
+
entry === undefined && failedEntries.length === 0
|
|
206
259
|
? RESTING_RUNS
|
|
207
260
|
: {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
261
|
+
...(entry === undefined
|
|
262
|
+
? RESTING_RUNS
|
|
263
|
+
: {
|
|
264
|
+
runId: entry.runId,
|
|
265
|
+
...(entry.epoch !== undefined && { epoch: entry.epoch }),
|
|
266
|
+
queue: entry.queue,
|
|
267
|
+
steerQueue: entry.steerQueue,
|
|
268
|
+
...(entry.dispatching !== undefined && {
|
|
269
|
+
dispatching: entry.dispatching,
|
|
270
|
+
}),
|
|
271
|
+
...(entry.stopping !== undefined && {
|
|
272
|
+
stopping: entry.stopping,
|
|
273
|
+
}),
|
|
274
|
+
...(entry.nextDispatch !== undefined && {
|
|
275
|
+
nextDispatch: entry.nextDispatch,
|
|
276
|
+
}),
|
|
277
|
+
...(entry.inputRequests !== undefined && {
|
|
278
|
+
inputRequests: entry.inputRequests,
|
|
279
|
+
}),
|
|
280
|
+
...(entry.error !== undefined && { error: entry.error }),
|
|
281
|
+
}),
|
|
282
|
+
failed: failedEntries,
|
|
223
283
|
},
|
|
224
284
|
};
|
|
225
|
-
}, [state, connection]);
|
|
285
|
+
}, [state, connection, failed]);
|
|
226
286
|
|
|
227
287
|
const { converter } = options;
|
|
228
288
|
const metadata = useMemo(
|
|
@@ -256,10 +316,23 @@ const useStatewireThread = (
|
|
|
256
316
|
[messages, liveRunId, runs, dispatch, dispatchSend],
|
|
257
317
|
);
|
|
258
318
|
|
|
319
|
+
const branches = useMemo(
|
|
320
|
+
() => createBranchCommandAdapter(messages, dispatch),
|
|
321
|
+
[messages, dispatch],
|
|
322
|
+
);
|
|
323
|
+
|
|
259
324
|
const inputRequests = runs.inputRequests ?? NO_INPUT_REQUESTS;
|
|
260
325
|
|
|
261
|
-
const extras = useMemo(
|
|
262
|
-
():
|
|
326
|
+
const extras = useMemo((): StatewireThread.Extras => {
|
|
327
|
+
const takeFailed = (messageId: string): StatewireClient.FailedCommand => {
|
|
328
|
+
const entry = failed.find(
|
|
329
|
+
(f) => failedRunCommand(f)?.message.id === messageId,
|
|
330
|
+
);
|
|
331
|
+
if (entry === undefined)
|
|
332
|
+
throw new Error(`statewire: unknown failed entry "${messageId}"`);
|
|
333
|
+
return entry;
|
|
334
|
+
};
|
|
335
|
+
return {
|
|
263
336
|
[symbolStatewireExtras]: true,
|
|
264
337
|
sendCommand: dispatch,
|
|
265
338
|
reconnect: connection.reconnect,
|
|
@@ -268,17 +341,38 @@ const useStatewireThread = (
|
|
|
268
341
|
isBusy,
|
|
269
342
|
inputRequests,
|
|
270
343
|
runs,
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
344
|
+
retryFailed: (messageId) => {
|
|
345
|
+
const entry = takeFailed(messageId);
|
|
346
|
+
const command = retryCommand(
|
|
347
|
+
messages,
|
|
348
|
+
liveRunId,
|
|
349
|
+
failedRunCommand(entry)!,
|
|
350
|
+
);
|
|
351
|
+
discardFailed(entry);
|
|
352
|
+
void dispatch(command);
|
|
353
|
+
},
|
|
354
|
+
discardFailed: (messageId) => discardFailed(takeFailed(messageId)),
|
|
355
|
+
popFailed: (messageId) => {
|
|
356
|
+
const entry = takeFailed(messageId);
|
|
357
|
+
discardFailed(entry);
|
|
358
|
+
return failedRunCommand(entry)!
|
|
359
|
+
.message.parts.flatMap((p) => (p.type === "text" ? [p.text] : []))
|
|
360
|
+
.join("\n\n");
|
|
361
|
+
},
|
|
362
|
+
};
|
|
363
|
+
}, [
|
|
364
|
+
dispatch,
|
|
365
|
+
connection.reconnect,
|
|
366
|
+
state,
|
|
367
|
+
status,
|
|
368
|
+
isBusy,
|
|
369
|
+
inputRequests,
|
|
370
|
+
runs,
|
|
371
|
+
failed,
|
|
372
|
+
discardFailed,
|
|
373
|
+
messages,
|
|
374
|
+
liveRunId,
|
|
375
|
+
]);
|
|
282
376
|
|
|
283
377
|
const handleAppend = (message: AppendMessage) => {
|
|
284
378
|
dispatchSend(
|
|
@@ -324,6 +418,7 @@ const useStatewireThread = (
|
|
|
324
418
|
}),
|
|
325
419
|
}),
|
|
326
420
|
...(options.capabilities?.reload && { onReload: handleReload }),
|
|
421
|
+
...(options.capabilities?.unstable_switchBranch && { branches }),
|
|
327
422
|
onAddToolResult: (toolOptions: AddToolResultOptions) =>
|
|
328
423
|
dispatch(resolveToolResultInput(inputRequests, toolOptions, liveRunId)),
|
|
329
424
|
onRespondToToolApproval: (approval: RespondToToolApprovalOptions) =>
|
|
@@ -445,6 +540,12 @@ export namespace StatewireThread {
|
|
|
445
540
|
isBusy: boolean;
|
|
446
541
|
inputRequests: readonly InputRequestState[];
|
|
447
542
|
runs: Runs;
|
|
543
|
+
/** Re-dispatches the failed entry against the current thread and removes it; keyed by the entry's message id. */
|
|
544
|
+
retryFailed: (messageId: string) => void;
|
|
545
|
+
/** Removes the failed entry; keyed by the entry's message id. */
|
|
546
|
+
discardFailed: (messageId: string) => void;
|
|
547
|
+
/** Removes the failed entry and returns its text for the composer; keyed by the entry's message id. */
|
|
548
|
+
popFailed: (messageId: string) => string;
|
|
448
549
|
};
|
|
449
550
|
|
|
450
551
|
export type TextPart = {
|
|
@@ -472,8 +573,8 @@ export namespace StatewireThread {
|
|
|
472
573
|
readonly insertBefore?: string | null;
|
|
473
574
|
};
|
|
474
575
|
|
|
475
|
-
|
|
476
|
-
readonly type:
|
|
576
|
+
type RunSendCommand<TType extends "run/enqueue" | "run/steer"> = {
|
|
577
|
+
readonly type: TType;
|
|
477
578
|
/** The run this command targets — the live run's id, or a fresh client-minted uuid on run-creating sends. */
|
|
478
579
|
readonly runId: string;
|
|
479
580
|
} & (
|
|
@@ -486,19 +587,9 @@ export namespace StatewireThread {
|
|
|
486
587
|
) &
|
|
487
588
|
Placement;
|
|
488
589
|
|
|
489
|
-
export type
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
readonly runId: string;
|
|
493
|
-
} & (
|
|
494
|
-
| {
|
|
495
|
-
readonly message: UserMessage;
|
|
496
|
-
/** Run anchor: the message the new content grows after, null asserting an empty thread. Present on run-creating sends; absent when targeting the live run. */
|
|
497
|
-
readonly runAnchorMessageId?: string | null;
|
|
498
|
-
}
|
|
499
|
-
| { readonly messageId: string }
|
|
500
|
-
) &
|
|
501
|
-
Placement;
|
|
590
|
+
export type RunEnqueueCommand = RunSendCommand<"run/enqueue">;
|
|
591
|
+
|
|
592
|
+
export type RunSteerCommand = RunSendCommand<"run/steer">;
|
|
502
593
|
|
|
503
594
|
export type RunDequeueCommand = {
|
|
504
595
|
readonly type: "run/dequeue";
|
|
@@ -561,6 +652,17 @@ export namespace StatewireThread {
|
|
|
561
652
|
readonly meta?: ReadonlyJSONValue;
|
|
562
653
|
};
|
|
563
654
|
|
|
655
|
+
/** Per-message branch metadata read from `metadata.custom.branch`: the message's own branch id and the fan-out's branch ids in creation order (own id included). */
|
|
656
|
+
export type BranchMetadata = {
|
|
657
|
+
readonly id: number;
|
|
658
|
+
readonly siblings: readonly number[];
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
export type SwitchBranchCommand = {
|
|
662
|
+
readonly type: "message/switch-branch";
|
|
663
|
+
readonly branchId: number;
|
|
664
|
+
};
|
|
665
|
+
|
|
564
666
|
export type RunReloadCommand = {
|
|
565
667
|
readonly type: "run/reload";
|
|
566
668
|
/** The live run's id when redirecting it, or a fresh client-minted uuid when idle. */
|
|
@@ -604,6 +706,11 @@ export namespace StatewireThread {
|
|
|
604
706
|
readonly meta?: ReadonlyJSONValue;
|
|
605
707
|
};
|
|
606
708
|
|
|
709
|
+
/** A failed run send projected out of the retained lane: the entry shape plus its send failure. */
|
|
710
|
+
export type RunsFailedEntry = RunsQueueEntry & {
|
|
711
|
+
readonly sendFailure: StatewireClient.SendFailure;
|
|
712
|
+
};
|
|
713
|
+
|
|
607
714
|
/** An input outcome riding a dispatch record: the request with its response (null = abandoned) and `run/input` meta. */
|
|
608
715
|
export type RunsInputOutcome = {
|
|
609
716
|
readonly request: InputRequest;
|
|
@@ -694,6 +801,8 @@ export namespace StatewireThread {
|
|
|
694
801
|
readonly inputRequests?: readonly InputRequestState[];
|
|
695
802
|
/** Set only by an unhandled throw; cleared by the next user-initiated run. */
|
|
696
803
|
readonly error?: RunsError;
|
|
804
|
+
/** Run sends retained after a non-accepted settle; retry, discard, or pop via the thread extras. */
|
|
805
|
+
readonly failed: readonly RunsFailedEntry[];
|
|
697
806
|
};
|
|
698
807
|
|
|
699
808
|
export type ConnectionMetadata = {
|
|
@@ -772,6 +881,8 @@ export namespace StatewireThread {
|
|
|
772
881
|
continue?: boolean | undefined;
|
|
773
882
|
/** The host supports `run/reload` (regenerating an assistant message). */
|
|
774
883
|
reload?: boolean | undefined;
|
|
884
|
+
/** The host supports `message/switch-branch` (thread-global branch switching driven by `metadata.custom.branch`). */
|
|
885
|
+
unstable_switchBranch?: boolean | undefined;
|
|
775
886
|
}
|
|
776
887
|
| undefined;
|
|
777
888
|
/**
|
|
@@ -806,6 +917,7 @@ declare module "@assistant-ui/core" {
|
|
|
806
917
|
runContinue: StatewireThread.RunContinueCommand;
|
|
807
918
|
runReload: StatewireThread.RunReloadCommand;
|
|
808
919
|
runInput: StatewireThread.RunInputCommand;
|
|
920
|
+
messageSwitchBranch: StatewireThread.SwitchBranchCommand;
|
|
809
921
|
}
|
|
810
922
|
}
|
|
811
923
|
}
|
package/src/runCommands.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AppendMessage,
|
|
3
|
+
ExternalThreadBranchAdapter,
|
|
3
4
|
ExternalThreadQueueAdapter,
|
|
4
5
|
QueuePlacement,
|
|
5
6
|
} from "@assistant-ui/core";
|
|
@@ -120,6 +121,38 @@ export const sendFields = (
|
|
|
120
121
|
runAnchorMessageId: viewedLeafId(messages),
|
|
121
122
|
};
|
|
122
123
|
|
|
124
|
+
/** Rebuilds a failed send against the current thread: edits re-anchor at the source's current parent, sends whose target run is gone fall back to a fresh enqueue. */
|
|
125
|
+
export const retryCommand = (
|
|
126
|
+
messages: readonly { id: string; status?: { type: string } }[],
|
|
127
|
+
runId: string | null,
|
|
128
|
+
command:
|
|
129
|
+
| (StatewireThread.RunEnqueueCommand & {
|
|
130
|
+
message: StatewireThread.UserMessage;
|
|
131
|
+
})
|
|
132
|
+
| (StatewireThread.RunSteerCommand & {
|
|
133
|
+
message: StatewireThread.UserMessage;
|
|
134
|
+
})
|
|
135
|
+
| StatewireThread.RunEditCommand,
|
|
136
|
+
): StatewireThread.RunCommand => {
|
|
137
|
+
if (command.type === "run/edit") {
|
|
138
|
+
const index = messages.findIndex((m) => m.id === command.sourceId);
|
|
139
|
+
if (index === -1)
|
|
140
|
+
throw new Error(
|
|
141
|
+
`statewire: run/edit source "${command.sourceId}" is unknown`,
|
|
142
|
+
);
|
|
143
|
+
return {
|
|
144
|
+
...command,
|
|
145
|
+
runId: runId ?? crypto.randomUUID(),
|
|
146
|
+
runAnchorMessageId: messages[index - 1]?.id ?? null,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
if (runId !== null && command.runId === runId) return command;
|
|
150
|
+
return {
|
|
151
|
+
type: "run/enqueue",
|
|
152
|
+
...sendFields(messages, runId, command.message),
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
|
|
123
156
|
export const createRunQueueAdapter = (
|
|
124
157
|
messages: readonly { id: string }[],
|
|
125
158
|
runId: string | null,
|
|
@@ -191,6 +224,46 @@ export const createRunQueueAdapter = (
|
|
|
191
224
|
};
|
|
192
225
|
};
|
|
193
226
|
|
|
227
|
+
const branchOf = (
|
|
228
|
+
message: { metadata: { custom: Record<string, unknown> } } | undefined,
|
|
229
|
+
): StatewireThread.BranchMetadata | undefined => {
|
|
230
|
+
const branch = message?.metadata.custom["branch"];
|
|
231
|
+
if (branch === undefined) return undefined;
|
|
232
|
+
const { id, siblings } = branch as StatewireThread.BranchMetadata;
|
|
233
|
+
if (
|
|
234
|
+
!Number.isInteger(id) ||
|
|
235
|
+
!Array.isArray(siblings) ||
|
|
236
|
+
!siblings.every(Number.isInteger) ||
|
|
237
|
+
!siblings.includes(id)
|
|
238
|
+
)
|
|
239
|
+
throw new Error("statewire: malformed branch metadata");
|
|
240
|
+
return { id, siblings };
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/** Branch surface over the host's thread-global branch: siblings come from `metadata.custom.branch`, switching sends `message/switch-branch`. */
|
|
244
|
+
export const createBranchCommandAdapter = (
|
|
245
|
+
messages: readonly {
|
|
246
|
+
id: string;
|
|
247
|
+
metadata: { custom: Record<string, unknown> };
|
|
248
|
+
}[],
|
|
249
|
+
dispatch: (command: StatewireThread.SwitchBranchCommand) => void,
|
|
250
|
+
): ExternalThreadBranchAdapter => ({
|
|
251
|
+
getBranches: (messageId) => {
|
|
252
|
+
const branch = branchOf(messages.find((m) => m.id === messageId));
|
|
253
|
+
if (branch === undefined) return [];
|
|
254
|
+
// the own slot carries the message id so the picker can locate its position
|
|
255
|
+
return branch.siblings.map((sibling) =>
|
|
256
|
+
sibling === branch.id ? messageId : String(sibling),
|
|
257
|
+
);
|
|
258
|
+
},
|
|
259
|
+
switchToBranch: (branchId) => {
|
|
260
|
+
const target = Number(branchId);
|
|
261
|
+
if (!Number.isInteger(target))
|
|
262
|
+
throw new Error(`statewire: unknown branch "${branchId}"`);
|
|
263
|
+
dispatch({ type: "message/switch-branch", branchId: target });
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
|
|
194
267
|
export const resolveReloadCommand = (
|
|
195
268
|
messages: readonly { id: string; role: string }[],
|
|
196
269
|
parentId: string | null,
|
|
@@ -270,7 +270,7 @@ describe("runsOptimistic", () => {
|
|
|
270
270
|
});
|
|
271
271
|
});
|
|
272
272
|
|
|
273
|
-
it("run/stop
|
|
273
|
+
it("run/stop leaves a staged rewind and its window in place", () => {
|
|
274
274
|
const draft = mount({ status: "running", runId: "run-1" });
|
|
275
275
|
apply(draft, "run/edit", {
|
|
276
276
|
type: "run/edit",
|
|
@@ -280,11 +280,73 @@ describe("runsOptimistic", () => {
|
|
|
280
280
|
runAnchorMessageId: "p0",
|
|
281
281
|
});
|
|
282
282
|
apply(draft, "run/stop", { runId: "run-1" });
|
|
283
|
-
expect(run(draft).stopping).toEqual({ reason: "
|
|
284
|
-
expect(run(draft).nextDispatch).
|
|
283
|
+
expect(run(draft).stopping).toEqual({ reason: "message-edit" });
|
|
284
|
+
expect(run(draft).nextDispatch).toEqual({
|
|
285
|
+
trigger: "message-edit",
|
|
286
|
+
messages: [message("m1b")],
|
|
287
|
+
rollbackTo: "p0",
|
|
288
|
+
});
|
|
285
289
|
expect(run(draft).status).toBe("running");
|
|
286
290
|
});
|
|
287
291
|
|
|
292
|
+
it("a second run/edit appends to the staged rewind, keeping its anchor", () => {
|
|
293
|
+
const draft = mount({ status: "running", runId: "run-1" });
|
|
294
|
+
apply(draft, "run/edit", {
|
|
295
|
+
type: "run/edit",
|
|
296
|
+
runId: "run-1",
|
|
297
|
+
message: message("m1b"),
|
|
298
|
+
sourceId: "m1",
|
|
299
|
+
runAnchorMessageId: "p0",
|
|
300
|
+
});
|
|
301
|
+
apply(draft, "run/edit", {
|
|
302
|
+
type: "run/edit",
|
|
303
|
+
runId: "run-1",
|
|
304
|
+
message: message("m2b"),
|
|
305
|
+
sourceId: "m2",
|
|
306
|
+
runAnchorMessageId: "a1",
|
|
307
|
+
});
|
|
308
|
+
expect(run(draft).nextDispatch).toEqual({
|
|
309
|
+
trigger: "message-edit",
|
|
310
|
+
messages: [message("m1b"), message("m2b")],
|
|
311
|
+
rollbackTo: "p0",
|
|
312
|
+
});
|
|
313
|
+
expect(run(draft).stopping).toEqual({ reason: "message-edit" });
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("run/edit replaces a staged reload; run/reload leaves a staged rewind", () => {
|
|
317
|
+
const draft = mount({ status: "running", runId: "run-1" });
|
|
318
|
+
apply(draft, "run/reload", {
|
|
319
|
+
type: "run/reload",
|
|
320
|
+
runId: "run-1",
|
|
321
|
+
sourceId: "a1",
|
|
322
|
+
runAnchorMessageId: "m1",
|
|
323
|
+
});
|
|
324
|
+
apply(draft, "run/edit", {
|
|
325
|
+
type: "run/edit",
|
|
326
|
+
runId: "run-1",
|
|
327
|
+
message: message("m1b"),
|
|
328
|
+
sourceId: "m1",
|
|
329
|
+
runAnchorMessageId: "p0",
|
|
330
|
+
});
|
|
331
|
+
expect(run(draft).nextDispatch).toEqual({
|
|
332
|
+
trigger: "message-edit",
|
|
333
|
+
messages: [message("m1b")],
|
|
334
|
+
rollbackTo: "p0",
|
|
335
|
+
});
|
|
336
|
+
apply(draft, "run/reload", {
|
|
337
|
+
type: "run/reload",
|
|
338
|
+
runId: "run-1",
|
|
339
|
+
sourceId: "a1",
|
|
340
|
+
runAnchorMessageId: "m1",
|
|
341
|
+
});
|
|
342
|
+
expect(run(draft).nextDispatch).toEqual({
|
|
343
|
+
trigger: "message-edit",
|
|
344
|
+
messages: [message("m1b")],
|
|
345
|
+
rollbackTo: "p0",
|
|
346
|
+
});
|
|
347
|
+
expect(run(draft).stopping).toEqual({ reason: "message-edit" });
|
|
348
|
+
});
|
|
349
|
+
|
|
288
350
|
it("run/reload predicts the rewind dispatch record", () => {
|
|
289
351
|
const draft = mount({
|
|
290
352
|
status: "stopped",
|