@assistant-ui/react-statewire 0.11.2 → 0.13.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.
Files changed (39) hide show
  1. package/dist/AssistantTransportThread.d.ts +19 -6
  2. package/dist/AssistantTransportThread.d.ts.map +1 -1
  3. package/dist/AssistantTransportThread.js +22 -15
  4. package/dist/AssistantTransportThread.js.map +1 -1
  5. package/dist/StatewireThread.d.ts +27 -15
  6. package/dist/StatewireThread.d.ts.map +1 -1
  7. package/dist/StatewireThread.js +37 -37
  8. package/dist/StatewireThread.js.map +1 -1
  9. package/dist/hooks.d.ts +3 -1
  10. package/dist/hooks.d.ts.map +1 -1
  11. package/dist/hooks.js +11 -2
  12. package/dist/hooks.js.map +1 -1
  13. package/dist/index.d.ts +2 -2
  14. package/dist/index.js +2 -2
  15. package/dist/inputRequests.d.ts +2 -2
  16. package/dist/inputRequests.d.ts.map +1 -1
  17. package/dist/inputRequests.js +4 -2
  18. package/dist/inputRequests.js.map +1 -1
  19. package/dist/runCommands.d.ts +15 -4
  20. package/dist/runCommands.d.ts.map +1 -1
  21. package/dist/runCommands.js +27 -11
  22. package/dist/runCommands.js.map +1 -1
  23. package/dist/runsOptimistic.d.ts.map +1 -1
  24. package/dist/runsOptimistic.js +6 -6
  25. package/dist/runsOptimistic.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/AssistantTransportThread.ts +53 -16
  28. package/src/StatewireThread.runs.test.tsx +69 -55
  29. package/src/StatewireThread.test.tsx +40 -53
  30. package/src/StatewireThread.ts +89 -78
  31. package/src/athena.e2e.test.tsx +4 -2
  32. package/src/hooks.ts +11 -0
  33. package/src/index.ts +1 -0
  34. package/src/inputRequests.ts +4 -0
  35. package/src/legacy-diff.test.tsx +2 -1
  36. package/src/runCommands.ts +48 -10
  37. package/src/runsOptimistic.test.ts +64 -26
  38. package/src/runsOptimistic.ts +10 -7
  39. package/src/transport.test.tsx +46 -9
@@ -32,8 +32,8 @@ import {
32
32
  createRunQueueAdapter,
33
33
  resolveEditCommand,
34
34
  resolveReloadCommand,
35
+ sendFields,
35
36
  toWireMessage,
36
- viewedLeafId,
37
37
  } from "./runCommands";
38
38
  import {
39
39
  resolveToolApprovalInput,
@@ -47,10 +47,7 @@ type AnyOptions =
47
47
  | StatewireThread.Options<any>
48
48
  | StatewireThread.RunsOptions<any>;
49
49
 
50
- const LOADING_STATE: StatewireThread.State = { messages: [], isRunning: false };
51
-
52
- const READY_RUNS: StatewireThread.Runs = {
53
- status: "ready",
50
+ const RESTING_RUNS: StatewireThread.Runs = {
54
51
  runId: null,
55
52
  queue: [],
56
53
  steerQueue: [],
@@ -195,26 +192,32 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
195
192
  [dispatch],
196
193
  );
197
194
 
198
- const runs = useMemo((): StatewireThread.Runs | undefined => {
199
- if (runsOption === undefined) return undefined;
200
- if (state === undefined) return READY_RUNS;
195
+ const { status, runs } = useMemo((): {
196
+ status: StatewireThread.ThreadStatus;
197
+ runs?: StatewireThread.Runs;
198
+ } => {
199
+ if (runsOption === undefined) return { status: "ready" };
201
200
  const mount = (runsOption === true ? state : runsOption(state)) as
202
- | StatewireThread.RunsState
201
+ | { runs?: readonly StatewireThread.RunState[] }
203
202
  | undefined;
204
- const entry = mount?.runs?.[0];
205
- return entry === undefined
206
- ? READY_RUNS
207
- : {
208
- status: entry.status,
209
- runId: entry.runId,
210
- queue: entry.queue,
211
- steerQueue: entry.steerQueue,
212
- dispatching: entry.dispatching,
213
- ...(entry.inputRequests !== undefined && {
214
- inputRequests: entry.inputRequests,
215
- }),
216
- error: entry.error,
217
- };
203
+ const entries = mount?.runs;
204
+ const entry = entries?.[0];
205
+ return {
206
+ status: entries === undefined ? "loading" : (entry?.status ?? "ready"),
207
+ runs:
208
+ entry === undefined
209
+ ? RESTING_RUNS
210
+ : {
211
+ runId: entry.runId,
212
+ queue: entry.queue,
213
+ steerQueue: entry.steerQueue,
214
+ dispatching: entry.dispatching,
215
+ ...(entry.inputRequests !== undefined && {
216
+ inputRequests: entry.inputRequests,
217
+ }),
218
+ error: entry.error,
219
+ },
220
+ };
218
221
  }, [runsOption, state]);
219
222
 
220
223
  const { converter } = options;
@@ -229,19 +232,18 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
229
232
  );
230
233
  const converted = useMemo(
231
234
  (): StatewireThread.State =>
232
- state === undefined
233
- ? LOADING_STATE
234
- : (converter(
235
- state,
236
- metadata as StatewireThread.ConnectionMetadata & {
237
- runs: StatewireThread.Runs;
238
- },
239
- ) as StatewireThread.State),
235
+ converter(
236
+ state,
237
+ metadata as StatewireThread.ConnectionMetadata & {
238
+ runs: StatewireThread.Runs;
239
+ },
240
+ ) as StatewireThread.State,
240
241
  [converter, state, metadata],
241
242
  );
242
243
  // Dispatch limbo is a materialize concern: the fold makes the record
243
244
  // concrete in state before conversion.
244
245
  const messages = converted.messages;
246
+ const liveRunId = runs !== undefined ? runs.runId : (converted.runId ?? null);
245
247
  const queueItems = runs !== undefined ? runs.queue : converted.queue;
246
248
  const steerQueueItems =
247
249
  runs !== undefined ? runs.steerQueue : converted.steerQueue;
@@ -249,16 +251,10 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
249
251
  throw new Error(
250
252
  "statewire: the converter's steerQueue projection requires the queue projection",
251
253
  );
252
- // Seams anchor at the first real convert; the loading placeholder projects nothing.
253
254
  const [queueSeam] = useState(() => ({
254
- present: undefined as boolean | undefined,
255
+ present: queueItems !== undefined,
255
256
  }));
256
- if (state !== undefined && queueSeam.present === undefined)
257
- queueSeam.present = queueItems !== undefined;
258
- if (
259
- queueSeam.present !== undefined &&
260
- queueSeam.present !== (queueItems !== undefined)
261
- )
257
+ if (queueSeam.present !== (queueItems !== undefined))
262
258
  throw new Error(
263
259
  "statewire: the converter's queue projection must be stable — it was " +
264
260
  (queueSeam.present ? "defined" : "undefined") +
@@ -270,26 +266,22 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
270
266
  queueItems
271
267
  ? createRunQueueAdapter(
272
268
  messages,
269
+ liveRunId,
273
270
  queueItems,
274
271
  steerQueueItems,
275
272
  dispatch,
276
273
  dispatchSend,
277
274
  )
278
275
  : undefined,
279
- [messages, queueItems, steerQueueItems, dispatch, dispatchSend],
276
+ [messages, liveRunId, queueItems, steerQueueItems, dispatch, dispatchSend],
280
277
  );
281
278
 
282
279
  const inputRequests =
283
280
  runs !== undefined ? (runs.inputRequests ?? []) : converted.inputRequests;
284
281
  const [inputSeam] = useState(() => ({
285
- present: undefined as boolean | undefined,
282
+ present: inputRequests !== undefined,
286
283
  }));
287
- if (state !== undefined && inputSeam.present === undefined)
288
- inputSeam.present = inputRequests !== undefined;
289
- if (
290
- inputSeam.present !== undefined &&
291
- inputSeam.present !== (inputRequests !== undefined)
292
- )
284
+ if (inputSeam.present !== (inputRequests !== undefined))
293
285
  throw new Error(
294
286
  "statewire: the converter's inputRequests projection must be stable — it was " +
295
287
  (inputSeam.present ? "defined" : "undefined") +
@@ -302,39 +294,42 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
302
294
  sendCommand: dispatch,
303
295
  reconnect: connection.reconnect,
304
296
  state,
297
+ status,
305
298
  ...(inputRequests !== undefined && { inputRequests }),
306
299
  ...(runs !== undefined && { runs }),
307
300
  }),
308
- [dispatch, connection.reconnect, state, inputRequests, runs],
301
+ [dispatch, connection.reconnect, state, status, inputRequests, runs],
309
302
  );
310
303
 
311
304
  const handleAppend = (message: AppendMessage) => {
312
305
  dispatchSend(
313
306
  message.sourceId != null
314
- ? resolveEditCommand(messages, message)
307
+ ? resolveEditCommand(messages, message, liveRunId)
315
308
  : {
316
309
  type: "run/steer",
317
- message: toWireMessage(message, crypto.randomUUID()),
318
- anchorMessageId: viewedLeafId(messages),
310
+ ...sendFields(
311
+ messages,
312
+ liveRunId,
313
+ toWireMessage(message, crypto.randomUUID()),
314
+ ),
319
315
  },
320
316
  message,
321
317
  );
322
318
  };
323
319
 
324
320
  const handleReload = (parentId: string | null) => {
325
- dispatch(resolveReloadCommand(messages, parentId));
321
+ dispatch(resolveReloadCommand(messages, parentId, liveRunId));
326
322
  };
327
323
 
328
324
  const handleEdit = (message: AppendMessage) => {
329
- dispatch(resolveEditCommand(messages, message));
325
+ dispatch(resolveEditCommand(messages, message, liveRunId));
330
326
  };
331
327
 
332
328
  const thread = useResource(
333
329
  ExternalThread({
334
330
  messages: messages as readonly ExternalThreadMessage[],
335
331
  isRunning:
336
- runs !== undefined ? runs.status === "running" : converted.isRunning,
337
- isLoading: state === undefined,
332
+ runs !== undefined ? status === "running" : converted.isRunning,
338
333
  state: converted.state,
339
334
  extras,
340
335
  attachmentAdapter: options.adapters?.attachments,
@@ -344,14 +339,22 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
344
339
  ...(queue && { queue }),
345
340
  ...(options.capabilities?.edit && { onEdit: handleEdit }),
346
341
  ...(options.capabilities?.continue && {
347
- onResume: () => dispatch({ type: "run/continue" }),
342
+ onResume: () =>
343
+ dispatch({
344
+ type: "run/continue",
345
+ ...(liveRunId !== null && { runId: liveRunId }),
346
+ }),
348
347
  }),
349
348
  ...(options.capabilities?.reload && { onReload: handleReload }),
350
349
  ...(inputRequests !== undefined && {
351
350
  onAddToolResult: (toolOptions: AddToolResultOptions) =>
352
- dispatch(resolveToolResultInput(inputRequests, toolOptions)),
351
+ dispatch(
352
+ resolveToolResultInput(inputRequests, toolOptions, liveRunId),
353
+ ),
353
354
  onRespondToToolApproval: (approval: RespondToToolApprovalOptions) =>
354
- dispatch(resolveToolApprovalInput(inputRequests, approval)),
355
+ dispatch(
356
+ resolveToolApprovalInput(inputRequests, approval, liveRunId),
357
+ ),
355
358
  }),
356
359
  onCancel: () => {
357
360
  const payload = latest.current.stopPayload?.();
@@ -419,11 +422,13 @@ export namespace StatewireThread {
419
422
 
420
423
  export type RunEnqueueCommand = {
421
424
  readonly type: "run/enqueue";
425
+ /** The run this command targets — the live run's id, or a fresh client-minted uuid on run-creating sends. */
426
+ readonly runId: string;
422
427
  } & (
423
428
  | {
424
429
  readonly message: UserMessage;
425
- /** Branch anchor: the newest message the client knows on its intended branch, null asserting an empty thread. Required on adds; optional on edits of queued items. */
426
- readonly anchorMessageId?: string | null;
430
+ /** 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. */
431
+ readonly runAnchorMessageId?: string | null;
427
432
  }
428
433
  | { readonly messageId: string }
429
434
  ) &
@@ -431,11 +436,13 @@ export namespace StatewireThread {
431
436
 
432
437
  export type RunSteerCommand = {
433
438
  readonly type: "run/steer";
439
+ /** The run this command targets — the live run's id, or a fresh client-minted uuid on run-creating sends. */
440
+ readonly runId: string;
434
441
  } & (
435
442
  | {
436
443
  readonly message: UserMessage;
437
- /** Branch anchor: the newest message the client knows on its intended branch, null asserting an empty thread. Required on adds; optional on edits of queued items. */
438
- readonly anchorMessageId?: string | null;
444
+ /** 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. */
445
+ readonly runAnchorMessageId?: string | null;
439
446
  }
440
447
  | { readonly messageId: string }
441
448
  ) &
@@ -443,19 +450,23 @@ export namespace StatewireThread {
443
450
 
444
451
  export type RunDequeueCommand = {
445
452
  readonly type: "run/dequeue";
453
+ readonly runId: string;
446
454
  readonly messageId: string;
447
455
  };
448
456
 
449
457
  export type RunEditCommand = {
450
458
  readonly type: "run/edit";
459
+ /** The live run's id when redirecting it, or a fresh client-minted uuid when idle. */
460
+ readonly runId: string;
451
461
  readonly message: UserMessage;
452
462
  readonly sourceId: string;
453
463
  /** Asserts `sourceId`'s parent — the rewind target; null names the root. */
454
- readonly rollbackTo: string | null;
464
+ readonly runAnchorMessageId: string | null;
455
465
  };
456
466
 
457
467
  export type RunContinueCommand = {
458
468
  readonly type: "run/continue";
469
+ readonly runId?: string;
459
470
  };
460
471
 
461
472
  export type ToolCallInputRequest = {
@@ -499,6 +510,7 @@ export namespace StatewireThread {
499
510
 
500
511
  export type RunInputCommand = {
501
512
  readonly type: "run/input";
513
+ readonly runId?: string;
502
514
  readonly requestId: string;
503
515
  readonly response: InputResponse;
504
516
  };
@@ -510,9 +522,11 @@ export namespace StatewireThread {
510
522
 
511
523
  export type RunReloadCommand = {
512
524
  readonly type: "run/reload";
525
+ /** The live run's id when redirecting it, or a fresh client-minted uuid when idle. */
526
+ readonly runId: string;
513
527
  readonly sourceId: string;
514
528
  /** Asserts `sourceId`'s parent — the rewind target; null names the root. */
515
- readonly rollbackTo: string | null;
529
+ readonly runAnchorMessageId: string | null;
516
530
  };
517
531
 
518
532
  export type RunCommand =
@@ -557,12 +571,10 @@ export namespace StatewireThread {
557
571
  };
558
572
 
559
573
  /** Run subsystem status replicated by the host. */
560
- export type RunStatus =
561
- | "ready"
562
- | "running"
563
- | "stopped"
564
- | "error"
565
- | "input-required";
574
+ export type RunStatus = "running" | "stopped" | "error" | "input-required";
575
+
576
+ /** The thread's status, derived from the state: `"loading"` while the runs mount is absent, `"ready"` while resting, otherwise the run's status. */
577
+ export type ThreadStatus = "loading" | "ready" | RunStatus;
566
578
 
567
579
  /** A queue-lane entry as replicated: the user message plus host-stamped `meta`. */
568
580
  export type RunsQueueEntry = UserMessage & {
@@ -602,9 +614,9 @@ export namespace StatewireThread {
602
614
 
603
615
  /** A replicated run entry — one element of the mount's `runs` array. */
604
616
  export type RunState = {
605
- /** The run's identity — minted at dispatch, persists through input-required, null in error/stop. */
617
+ /** The run's identity — client-minted, adopted by the host at run creation; persists through steers, input-required, and error/stop, retiring only when the queue drains and the run completes. */
606
618
  readonly runId: string | null;
607
- readonly status: Exclude<RunStatus, "ready">;
619
+ readonly status: RunStatus;
608
620
  readonly queue: readonly RunsQueueEntry[];
609
621
  readonly steerQueue: readonly RunsQueueEntry[];
610
622
  /** The dispatch record — the unacked batch handed to the run, cleared at the ack. */
@@ -625,15 +637,14 @@ export namespace StatewireThread {
625
637
  /** The runs mount as replicated: the run entries plus the top-level status projection. */
626
638
  export type RunsState = {
627
639
  /** Projection of `runs[0]?.status ?? "ready"`. */
628
- readonly status: RunStatus;
640
+ readonly status: Exclude<ThreadStatus, "loading">;
629
641
  /** At most one entry; empty while resting. */
630
642
  readonly runs: readonly RunState[];
631
643
  };
632
644
 
633
- /** The run subsystem's typed read — `runs[0]` of the selected mount with unsettled commands folded in, ready defaults while resting. */
645
+ /** The run subsystem's typed read — `runs[0]` of the selected mount with unsettled commands folded in, resting defaults while no entry is live. */
634
646
  export type Runs = {
635
- readonly status: RunStatus;
636
- /** The live run's id — non-null in running and input-required; stamped onto `run/stop`. */
647
+ /** The live run's id — non-null whenever a run entry is live (running, input-required, error/stop); the target for `run/*` sends. */
637
648
  readonly runId: string | null;
638
649
  readonly queue: readonly RunsQueueEntry[];
639
650
  readonly steerQueue: readonly RunsQueueEntry[];
@@ -680,8 +691,8 @@ export namespace StatewireThread {
680
691
 
681
692
  export type Options<T> = {
682
693
  transport: Transport<T>;
683
- /** Preloads the thread: published and interactive from the first render, replaced by the attach snapshot. Omitted, the thread stays loading until attach. */
684
- initialState?: T | undefined;
694
+ /** The pre-attach state, replaced by the attach snapshot. With runs, `runs: []` projects ready (interactive from the first render); an absent runs mount projects loading until attach. */
695
+ initialState: T;
685
696
  converter: StateConverter<T>;
686
697
  runs?: undefined;
687
698
  /**
@@ -321,13 +321,14 @@ describe("deep-agent e2e", () => {
321
321
  expect(r1.body.commands).toEqual([
322
322
  {
323
323
  type: "run/steer",
324
+ runId: expect.any(String),
324
325
  trackingId: "track-0",
325
326
  message: {
326
327
  id: expect.any(String),
327
328
  role: "user",
328
329
  parts: [{ type: "text", text: "what's the weather in SF?" }],
329
330
  },
330
- anchorMessageId: null,
331
+ runAnchorMessageId: null,
331
332
  },
332
333
  ]);
333
334
 
@@ -571,6 +572,7 @@ describe("deep-agent e2e", () => {
571
572
  expect(fetchMock.requests[0]!.body.commands).toEqual([
572
573
  {
573
574
  type: "run/edit",
575
+ runId: expect.any(String),
574
576
  trackingId: "track-0",
575
577
  message: {
576
578
  id: expect.any(String),
@@ -578,7 +580,7 @@ describe("deep-agent e2e", () => {
578
580
  parts: [{ type: "text", text: "better question" }],
579
581
  },
580
582
  sourceId: "h2",
581
- rollbackTo: "a1",
583
+ runAnchorMessageId: "a1",
582
584
  },
583
585
  ]);
584
586
  expect("parentId" in fetchMock.requests[0]!.body).toBe(false);
package/src/hooks.ts CHANGED
@@ -19,6 +19,17 @@ export const useAssistantTransportSendCommand = () => {
19
19
  };
20
20
  };
21
21
 
22
+ /** The thread status; requires StatewireThread. */
23
+ export const useStatewireThreadStatus = (): StatewireThread.ThreadStatus =>
24
+ useAuiState((s) => {
25
+ const { status } = asAssistantTransportExtras(s.thread.extras);
26
+ if (status === undefined)
27
+ throw new Error(
28
+ "statewire: useStatewireThreadStatus requires StatewireThread",
29
+ );
30
+ return status;
31
+ });
32
+
22
33
  /** The typed runs read; requires StatewireThread with the `runs` option. */
23
34
  export const useStatewireRuns = (): StatewireThread.Runs =>
24
35
  useAuiState((s) => {
package/src/index.ts CHANGED
@@ -5,4 +5,5 @@ export {
5
5
  useAssistantTransportSendCommand,
6
6
  useAssistantTransportState,
7
7
  useStatewireRuns,
8
+ useStatewireThreadStatus,
8
9
  } from "./hooks";
@@ -19,6 +19,7 @@ export const resolveToolResultInput = (
19
19
  isError: boolean;
20
20
  artifact?: ReadonlyJSONValue | undefined;
21
21
  },
22
+ runId: string | null,
22
23
  ): AssistantTransportThread.RunInputCommand => {
23
24
  const request = inputRequests.find(
24
25
  (r) => r.type === "tool-call" && r.toolCallId === options.toolCallId,
@@ -30,6 +31,7 @@ export const resolveToolResultInput = (
30
31
  assertUnanswered(request);
31
32
  return {
32
33
  type: "run/input",
34
+ ...(runId !== null && { runId }),
33
35
  requestId: request.id,
34
36
  response: {
35
37
  output: options.result,
@@ -42,6 +44,7 @@ export const resolveToolResultInput = (
42
44
  export const resolveToolApprovalInput = (
43
45
  inputRequests: readonly AssistantTransportThread.InputRequestState[],
44
46
  options: RespondToToolApprovalOptions,
47
+ runId: string | null,
45
48
  ): AssistantTransportThread.RunInputCommand => {
46
49
  const request = inputRequests.find(
47
50
  (r) => r.type === "tool-approval" && r.id === options.approvalId,
@@ -53,6 +56,7 @@ export const resolveToolApprovalInput = (
53
56
  assertUnanswered(request);
54
57
  return {
55
58
  type: "run/input",
59
+ ...(runId !== null && { runId }),
56
60
  requestId: request.id,
57
61
  response: {
58
62
  decision: options.approved ? "approve" : "reject",
@@ -66,12 +66,13 @@ const renameWire = (commands: unknown) =>
66
66
  c.type === "add-message"
67
67
  ? {
68
68
  type: "run/steer",
69
+ runId: expect.any(String),
69
70
  message: {
70
71
  id: expect.any(String),
71
72
  role: c.message.role,
72
73
  parts: c.message.parts,
73
74
  },
74
- anchorMessageId: expect.toSatisfy(
75
+ runAnchorMessageId: expect.toSatisfy(
75
76
  (v) => v === null || typeof v === "string",
76
77
  ),
77
78
  }
@@ -89,6 +89,7 @@ export const viewedLeafId = (
89
89
  export const resolveEditCommand = (
90
90
  messages: readonly { id: string }[],
91
91
  message: AppendMessage,
92
+ runId: string | null,
92
93
  ): AssistantTransportThread.RunEditCommand => {
93
94
  if (message.sourceId == null)
94
95
  throw new Error("statewire: run/edit requires a sourceId");
@@ -99,14 +100,34 @@ export const resolveEditCommand = (
99
100
  );
100
101
  return {
101
102
  type: "run/edit",
103
+ runId: runId ?? crypto.randomUUID(),
102
104
  message: toWireMessage(message, crypto.randomUUID()),
103
105
  sourceId: message.sourceId,
104
- rollbackTo: messages[index - 1]?.id ?? null,
106
+ runAnchorMessageId: messages[index - 1]?.id ?? null,
105
107
  };
106
108
  };
107
109
 
110
+ /** The send fields for a run-creating or run-targeting enqueue/steer: target the live run, or mint a fresh one anchored at the viewed leaf. */
111
+ export const sendFields = (
112
+ messages: readonly { id: string; status?: { type: string } }[],
113
+ runId: string | null,
114
+ message: AssistantTransportThread.RunUserMessage,
115
+ ): {
116
+ runId: string;
117
+ message: AssistantTransportThread.RunUserMessage;
118
+ runAnchorMessageId?: string | null;
119
+ } =>
120
+ runId !== null
121
+ ? { runId, message }
122
+ : {
123
+ runId: crypto.randomUUID(),
124
+ message,
125
+ runAnchorMessageId: viewedLeafId(messages),
126
+ };
127
+
108
128
  export const createRunQueueAdapter = (
109
129
  messages: readonly { id: string }[],
130
+ runId: string | null,
110
131
  queueItems: readonly AssistantTransportThread.RunQueueItem[],
111
132
  steerQueueItems: readonly AssistantTransportThread.RunQueueItem[] | undefined,
112
133
  dispatch: (command: AssistantTransportThread.RunCommand) => void,
@@ -115,6 +136,10 @@ export const createRunQueueAdapter = (
115
136
  message: AppendMessage,
116
137
  ) => void,
117
138
  ): ExternalThreadQueueAdapter => {
139
+ const liveRunId = (): string => {
140
+ if (runId === null) throw new Error("statewire: no live run to target");
141
+ return runId;
142
+ };
118
143
  const laneOf = (queueItemId: string): "queue" | "steer" => {
119
144
  if (steerQueueItems?.some((item) => item.id === queueItemId))
120
145
  return "steer";
@@ -126,11 +151,13 @@ export const createRunQueueAdapter = (
126
151
  lane: "queue" | "steer",
127
152
  message: AppendMessage,
128
153
  ): AssistantTransportThread.RunCommand => {
129
- if (message.sourceId != null) return resolveEditCommand(messages, message);
130
- const fields = {
131
- message: toWireMessage(message, crypto.randomUUID()),
132
- anchorMessageId: viewedLeafId(messages),
133
- };
154
+ if (message.sourceId != null)
155
+ return resolveEditCommand(messages, message, runId);
156
+ const fields = sendFields(
157
+ messages,
158
+ runId,
159
+ toWireMessage(message, crypto.randomUUID()),
160
+ );
134
161
  return lane === "steer"
135
162
  ? { type: "run/steer", ...fields }
136
163
  : { type: "run/enqueue", ...fields };
@@ -143,6 +170,7 @@ export const createRunQueueAdapter = (
143
170
  move: (queueItemId, placement) => {
144
171
  const lane = placement.lane ?? laneOf(queueItemId);
145
172
  const fields = {
173
+ runId: liveRunId(),
146
174
  messageId: queueItemId,
147
175
  ...placementFields(placement),
148
176
  };
@@ -156,18 +184,23 @@ export const createRunQueueAdapter = (
156
184
  const wireMessage = toWireMessage(message, queueItemId);
157
185
  dispatch(
158
186
  laneOf(queueItemId) === "steer"
159
- ? { type: "run/steer", message: wireMessage }
160
- : { type: "run/enqueue", message: wireMessage },
187
+ ? { type: "run/steer", runId: liveRunId(), message: wireMessage }
188
+ : { type: "run/enqueue", runId: liveRunId(), message: wireMessage },
161
189
  );
162
190
  },
163
191
  remove: (queueItemId) =>
164
- dispatch({ type: "run/dequeue", messageId: queueItemId }),
192
+ dispatch({
193
+ type: "run/dequeue",
194
+ runId: liveRunId(),
195
+ messageId: queueItemId,
196
+ }),
165
197
  };
166
198
  };
167
199
 
168
200
  export const resolveReloadCommand = (
169
201
  messages: readonly { id: string; role: string }[],
170
202
  parentId: string | null,
203
+ runId: string | null,
171
204
  ): AssistantTransportThread.RunReloadCommand => {
172
205
  const index =
173
206
  parentId === null
@@ -180,5 +213,10 @@ export const resolveReloadCommand = (
180
213
  throw new Error(
181
214
  "statewire: run/reload requires an assistant message to regenerate",
182
215
  );
183
- return { type: "run/reload", sourceId: target.id, rollbackTo: parentId };
216
+ return {
217
+ type: "run/reload",
218
+ runId: runId ?? crypto.randomUUID(),
219
+ sourceId: target.id,
220
+ runAnchorMessageId: parentId,
221
+ };
184
222
  };