@assistant-ui/react-statewire 0.13.0 → 0.15.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 (57) hide show
  1. package/dist/StatewireThread.d.ts +86 -105
  2. package/dist/StatewireThread.d.ts.map +1 -1
  3. package/dist/StatewireThread.js +36 -51
  4. package/dist/StatewireThread.js.map +1 -1
  5. package/dist/hooks.d.ts +6 -9
  6. package/dist/hooks.d.ts.map +1 -1
  7. package/dist/hooks.js +9 -20
  8. package/dist/hooks.js.map +1 -1
  9. package/dist/index.d.ts +2 -3
  10. package/dist/index.js +2 -3
  11. package/dist/inputRequests.d.ts +4 -4
  12. package/dist/inputRequests.d.ts.map +1 -1
  13. package/dist/inputRequests.js +1 -1
  14. package/dist/inputRequests.js.map +1 -1
  15. package/dist/runCommands.d.ts +8 -24
  16. package/dist/runCommands.d.ts.map +1 -1
  17. package/dist/runCommands.js +3 -3
  18. package/dist/runCommands.js.map +1 -1
  19. package/dist/runsOptimistic.d.ts.map +1 -1
  20. package/dist/runsOptimistic.js +30 -13
  21. package/dist/runsOptimistic.js.map +1 -1
  22. package/package.json +4 -5
  23. package/src/StatewireThread.runs.test.tsx +65 -33
  24. package/src/StatewireThread.test.tsx +228 -426
  25. package/src/StatewireThread.ts +133 -224
  26. package/src/__tests__/testHelpers.tsx +6 -226
  27. package/src/hooks.ts +11 -32
  28. package/src/index.ts +2 -4
  29. package/src/inputRequests.ts +7 -9
  30. package/src/runCommands.ts +17 -23
  31. package/src/runsMaterialize.test.ts +7 -8
  32. package/src/runsOptimistic.test.ts +86 -19
  33. package/src/runsOptimistic.ts +49 -19
  34. package/dist/AssistantTransportThread.d.ts +0 -227
  35. package/dist/AssistantTransportThread.d.ts.map +0 -1
  36. package/dist/AssistantTransportThread.js +0 -264
  37. package/dist/AssistantTransportThread.js.map +0 -1
  38. package/dist/host.d.ts +0 -38
  39. package/dist/host.d.ts.map +0 -1
  40. package/dist/host.js +0 -299
  41. package/dist/host.js.map +0 -1
  42. package/dist/replayBoundaryStream.d.ts +0 -6
  43. package/dist/replayBoundaryStream.d.ts.map +0 -1
  44. package/dist/replayBoundaryStream.js +0 -49
  45. package/dist/replayBoundaryStream.js.map +0 -1
  46. package/dist/stateOps.d.ts +0 -10
  47. package/dist/stateOps.d.ts.map +0 -1
  48. package/dist/stateOps.js +0 -114
  49. package/dist/stateOps.js.map +0 -1
  50. package/src/AssistantTransportThread.ts +0 -791
  51. package/src/athena.e2e.test.tsx +0 -654
  52. package/src/host.ts +0 -437
  53. package/src/legacy-diff.test.tsx +0 -628
  54. package/src/replayBoundaryStream.ts +0 -71
  55. package/src/stateOps.test.ts +0 -165
  56. package/src/stateOps.ts +0 -126
  57. package/src/transport.test.tsx +0 -3001
@@ -1,8 +1,9 @@
1
1
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
2
  import { resource, useResource } from "@assistant-ui/tap";
3
3
  import type { ResourceElement } from "@assistant-ui/tap";
4
- import { ExternalThread } from "@assistant-ui/react";
5
- import type { ExternalThreadMessage } from "@assistant-ui/react";
4
+ import { ExternalThread } from "@assistant-ui/core/store";
5
+ import type { ExternalThreadMessage } from "@assistant-ui/core/store";
6
+ import type { Assistant } from "@assistant-ui/core";
6
7
  import {
7
8
  forwardTransformScopes,
8
9
  useAssistantClientRef,
@@ -12,7 +13,6 @@ import type {
12
13
  AddToolResultOptions,
13
14
  AppendMessage,
14
15
  AttachmentAdapter,
15
- ExternalThreadQueueAdapter,
16
16
  FeedbackAdapter,
17
17
  ModelContext,
18
18
  RespondToToolApprovalOptions,
@@ -23,11 +23,6 @@ import { toToolsJSONSchema } from "assistant-stream";
23
23
  import type { ReadonlyJSONValue } from "assistant-stream/utils";
24
24
  import { useStatewire } from "statewire";
25
25
  import type { Statewire, StatewireClient } from "statewire";
26
- import { symbolAssistantTransportExtras } from "./AssistantTransportThread";
27
- import type {
28
- AssistantTransportExtras,
29
- AssistantTransportThread,
30
- } from "./AssistantTransportThread";
31
26
  import {
32
27
  createRunQueueAdapter,
33
28
  resolveEditCommand,
@@ -43,16 +38,27 @@ import { runsOptimistic } from "./runsOptimistic";
43
38
 
44
39
  type AnyCommands = Record<string, (...args: any[]) => unknown>;
45
40
 
46
- type AnyOptions =
47
- | StatewireThread.Options<any>
48
- | StatewireThread.RunsOptions<any>;
41
+ export const symbolStatewireExtras = Symbol("statewire-extras");
42
+
43
+ export const asStatewireExtras = (extras: unknown): StatewireThread.Extras => {
44
+ if (
45
+ typeof extras !== "object" ||
46
+ extras == null ||
47
+ !(symbolStatewireExtras in extras)
48
+ )
49
+ throw new Error(
50
+ "This method can only be called when you are using StatewireThread",
51
+ );
52
+
53
+ return extras as StatewireThread.Extras;
54
+ };
55
+
56
+ const NO_INPUT_REQUESTS: readonly StatewireThread.InputRequestState[] = [];
49
57
 
50
58
  const RESTING_RUNS: StatewireThread.Runs = {
51
59
  runId: null,
52
60
  queue: [],
53
61
  steerQueue: [],
54
- dispatching: null,
55
- error: null,
56
62
  };
57
63
 
58
64
  const ambientContextOf = (context: ModelContext): Record<string, unknown> => ({
@@ -66,7 +72,9 @@ const ambientContextOf = (context: ModelContext): Record<string, unknown> => ({
66
72
  ...(context.config !== undefined && { config: context.config }),
67
73
  });
68
74
 
69
- const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
75
+ const useStatewireThread = (
76
+ options: StatewireThread.Options<any>,
77
+ ): ClientOutput<"thread"> => {
70
78
  const latest = useRef(options);
71
79
  latest.current = options;
72
80
 
@@ -82,48 +90,17 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
82
90
  ),
83
91
  );
84
92
 
85
- const runsOption = options.runs;
86
- const [runsSeam] = useState(() => runsOption !== undefined);
87
- if (runsSeam !== (runsOption !== undefined))
88
- throw new Error(
89
- "statewire: the runs option must be stable — it was " +
90
- (runsSeam ? "set" : "unset") +
91
- " on first render and changed since",
92
- );
93
-
94
- const materializeOption = (options as StatewireThread.RunsOptions<unknown>)
95
- .materialize;
96
- const [materializeSeam] = useState(() => materializeOption !== undefined);
97
- if (materializeSeam !== (materializeOption !== undefined))
98
- throw new Error(
99
- "statewire: the materialize option must be stable — it was " +
100
- (materializeSeam ? "set" : "unset") +
101
- " on first render and changed since",
102
- );
103
-
104
93
  const { state, commands, connection, setContext } = useStatewire<
105
94
  unknown,
106
95
  AnyCommands
107
96
  >({
108
97
  transport: options.transport,
109
98
  initialState: options.initialState,
110
- ...(runsSeam && {
111
- optimistic: (draft, command, ctx) => {
112
- const runsOpt = latest.current.runs!;
113
- runsOptimistic(runsOpt === true ? draft : runsOpt(draft), command, ctx);
114
- (latest.current as StatewireThread.RunsOptions<unknown>).optimistic?.(
115
- draft,
116
- command,
117
- ctx,
118
- );
119
- },
120
- }),
121
- ...(materializeSeam && {
122
- materialize: (draft: {}) =>
123
- (latest.current as StatewireThread.RunsOptions<unknown>).materialize!(
124
- draft,
125
- ),
126
- }),
99
+ optimistic: (draft, command, ctx) => {
100
+ runsOptimistic(draft, command, ctx);
101
+ latest.current.optimistic?.(draft, command, ctx);
102
+ },
103
+ materialize: (draft: {}) => latest.current.materialize?.(draft),
127
104
  onCommandChange: (update, ctx) => {
128
105
  if (update.status === "acked" || update.status === "settled")
129
106
  retire(update.params[0]);
@@ -194,13 +171,11 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
194
171
 
195
172
  const { status, runs } = useMemo((): {
196
173
  status: StatewireThread.ThreadStatus;
197
- runs?: StatewireThread.Runs;
174
+ runs: StatewireThread.Runs;
198
175
  } => {
199
- if (runsOption === undefined) return { status: "ready" };
200
- const mount = (runsOption === true ? state : runsOption(state)) as
201
- | { runs?: readonly StatewireThread.RunState[] }
202
- | undefined;
203
- const entries = mount?.runs;
176
+ const entries = (
177
+ state as { runs?: readonly StatewireThread.RunState[] } | undefined
178
+ )?.runs;
204
179
  const entry = entries?.[0];
205
180
  return {
206
181
  status: entries === undefined ? "loading" : (entry?.status ?? "ready"),
@@ -211,14 +186,20 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
211
186
  runId: entry.runId,
212
187
  queue: entry.queue,
213
188
  steerQueue: entry.steerQueue,
214
- dispatching: entry.dispatching,
189
+ ...(entry.dispatching !== undefined && {
190
+ dispatching: entry.dispatching,
191
+ }),
192
+ ...(entry.stopping !== undefined && { stopping: entry.stopping }),
193
+ ...(entry.nextDispatch !== undefined && {
194
+ nextDispatch: entry.nextDispatch,
195
+ }),
215
196
  ...(entry.inputRequests !== undefined && {
216
197
  inputRequests: entry.inputRequests,
217
198
  }),
218
- error: entry.error,
199
+ ...(entry.error !== undefined && { error: entry.error }),
219
200
  },
220
201
  };
221
- }, [runsOption, state]);
202
+ }, [state]);
222
203
 
223
204
  const { converter } = options;
224
205
  const metadata = useMemo(
@@ -226,77 +207,43 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
226
207
  pendingCommands: [...pending],
227
208
  isSending: pending.length > 0,
228
209
  connection,
229
- ...(runs !== undefined && { runs }),
210
+ runs,
230
211
  }),
231
212
  [pending, connection, runs],
232
213
  );
233
214
  const converted = useMemo(
234
- (): StatewireThread.State =>
235
- converter(
236
- state,
237
- metadata as StatewireThread.ConnectionMetadata & {
238
- runs: StatewireThread.Runs;
239
- },
240
- ) as StatewireThread.State,
215
+ (): StatewireThread.State => converter(state, metadata),
241
216
  [converter, state, metadata],
242
217
  );
243
218
  // Dispatch limbo is a materialize concern: the fold makes the record
244
219
  // concrete in state before conversion.
245
220
  const messages = converted.messages;
246
- const liveRunId = runs !== undefined ? runs.runId : (converted.runId ?? null);
247
- const queueItems = runs !== undefined ? runs.queue : converted.queue;
248
- const steerQueueItems =
249
- runs !== undefined ? runs.steerQueue : converted.steerQueue;
250
- if (steerQueueItems !== undefined && queueItems === undefined)
251
- throw new Error(
252
- "statewire: the converter's steerQueue projection requires the queue projection",
253
- );
254
- const [queueSeam] = useState(() => ({
255
- present: queueItems !== undefined,
256
- }));
257
- if (queueSeam.present !== (queueItems !== undefined))
258
- throw new Error(
259
- "statewire: the converter's queue projection must be stable — it was " +
260
- (queueSeam.present ? "defined" : "undefined") +
261
- " on first convert and changed since",
262
- );
221
+ const liveRunId = runs.runId;
263
222
 
264
223
  const queue = useMemo(
265
- (): ExternalThreadQueueAdapter | undefined =>
266
- queueItems
267
- ? createRunQueueAdapter(
268
- messages,
269
- liveRunId,
270
- queueItems,
271
- steerQueueItems,
272
- dispatch,
273
- dispatchSend,
274
- )
275
- : undefined,
276
- [messages, liveRunId, queueItems, steerQueueItems, dispatch, dispatchSend],
224
+ () =>
225
+ createRunQueueAdapter(
226
+ messages,
227
+ liveRunId,
228
+ runs.queue,
229
+ runs.steerQueue,
230
+ dispatch,
231
+ dispatchSend,
232
+ ),
233
+ [messages, liveRunId, runs, dispatch, dispatchSend],
277
234
  );
278
235
 
279
- const inputRequests =
280
- runs !== undefined ? (runs.inputRequests ?? []) : converted.inputRequests;
281
- const [inputSeam] = useState(() => ({
282
- present: inputRequests !== undefined,
283
- }));
284
- if (inputSeam.present !== (inputRequests !== undefined))
285
- throw new Error(
286
- "statewire: the converter's inputRequests projection must be stable — it was " +
287
- (inputSeam.present ? "defined" : "undefined") +
288
- " on first convert and changed since",
289
- );
236
+ const inputRequests = runs.inputRequests ?? NO_INPUT_REQUESTS;
290
237
 
291
238
  const extras = useMemo(
292
- (): AssistantTransportExtras => ({
293
- [symbolAssistantTransportExtras]: true,
239
+ (): StatewireThread.Extras => ({
240
+ [symbolStatewireExtras]: true,
294
241
  sendCommand: dispatch,
295
242
  reconnect: connection.reconnect,
296
243
  state,
297
244
  status,
298
- ...(inputRequests !== undefined && { inputRequests }),
299
- ...(runs !== undefined && { runs }),
245
+ inputRequests,
246
+ runs,
300
247
  }),
301
248
  [dispatch, connection.reconnect, state, status, inputRequests, runs],
302
249
  );
@@ -328,15 +275,14 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
328
275
  const thread = useResource(
329
276
  ExternalThread({
330
277
  messages: messages as readonly ExternalThreadMessage[],
331
- isRunning:
332
- runs !== undefined ? status === "running" : converted.isRunning,
278
+ isRunning: status === "running",
333
279
  state: converted.state,
334
280
  extras,
335
281
  attachmentAdapter: options.adapters?.attachments,
336
282
  speechAdapter: options.adapters?.speech,
337
283
  feedbackAdapter: options.adapters?.feedback,
338
284
  onNew: handleAppend,
339
- ...(queue && { queue }),
285
+ queue,
340
286
  ...(options.capabilities?.edit && { onEdit: handleEdit }),
341
287
  ...(options.capabilities?.continue && {
342
288
  onResume: () =>
@@ -346,16 +292,10 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
346
292
  }),
347
293
  }),
348
294
  ...(options.capabilities?.reload && { onReload: handleReload }),
349
- ...(inputRequests !== undefined && {
350
- onAddToolResult: (toolOptions: AddToolResultOptions) =>
351
- dispatch(
352
- resolveToolResultInput(inputRequests, toolOptions, liveRunId),
353
- ),
354
- onRespondToToolApproval: (approval: RespondToToolApprovalOptions) =>
355
- dispatch(
356
- resolveToolApprovalInput(inputRequests, approval, liveRunId),
357
- ),
358
- }),
295
+ onAddToolResult: (toolOptions: AddToolResultOptions) =>
296
+ dispatch(resolveToolResultInput(inputRequests, toolOptions, liveRunId)),
297
+ onRespondToToolApproval: (approval: RespondToToolApprovalOptions) =>
298
+ dispatch(resolveToolApprovalInput(inputRequests, approval, liveRunId)),
359
299
  onCancel: () => {
360
300
  const payload = latest.current.stopPayload?.();
361
301
  if (
@@ -365,16 +305,11 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
365
305
  Array.isArray(payload))
366
306
  )
367
307
  throw new Error("statewire: stopPayload must return a plain object");
368
- const runId = runs !== undefined ? runs.runId : converted.runId;
369
- const args =
370
- runId === undefined
371
- ? payload === undefined
372
- ? []
373
- : [payload]
374
- : [{ ...payload, runId }];
375
- void commands["run/stop"]!(...args).catch((error: Error) => {
376
- latest.current.onError?.(error);
377
- });
308
+ void commands["run/stop"]!({ ...payload, runId: runs.runId }).catch(
309
+ (error: Error) => {
310
+ latest.current.onError?.(error);
311
+ },
312
+ );
378
313
  },
379
314
  }),
380
315
  );
@@ -382,19 +317,26 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
382
317
  return thread;
383
318
  };
384
319
 
385
- export const StatewireThread: {
386
- <T>(
387
- options: StatewireThread.RunsOptions<T>,
388
- ): ResourceElement<ClientOutput<"thread">>;
389
- <T>(
390
- options: StatewireThread.Options<T>,
391
- ): ResourceElement<ClientOutput<"thread">>;
392
- } = (options: AnyOptions): ResourceElement<ClientOutput<"thread">> =>
393
- resource(useStatewireThread)(options);
320
+ export const StatewireThread = <T>(
321
+ options: StatewireThread.Options<T>,
322
+ ): ResourceElement<ClientOutput<"thread">> =>
323
+ resource(useStatewireThread)(options as StatewireThread.Options<any>);
394
324
 
395
325
  export namespace StatewireThread {
326
+ type UserCommands = Assistant.Commands[keyof Assistant.Commands];
327
+
396
328
  export type Transport<T> = ResourceElement<Statewire<T | undefined, any>>;
397
329
 
330
+ export type Extras = {
331
+ [symbolStatewireExtras]: true;
332
+ sendCommand: (command: Command) => void;
333
+ reconnect: () => void;
334
+ state: unknown;
335
+ status: ThreadStatus;
336
+ inputRequests: readonly InputRequestState[];
337
+ runs: Runs;
338
+ };
339
+
398
340
  export type TextPart = {
399
341
  readonly type: "text";
400
342
  readonly text: string;
@@ -493,19 +435,6 @@ export namespace StatewireThread {
493
435
  | ToolApprovalInputRequest
494
436
  | CustomInputRequest;
495
437
 
496
- export type ToolCallInputResponse = {
497
- readonly output: ReadonlyJSONValue;
498
- /** Default false; when true, `output` carries the error payload. */
499
- readonly isError?: boolean;
500
- readonly artifact?: ReadonlyJSONValue;
501
- };
502
-
503
- export type ToolApprovalInputResponse = {
504
- readonly decision: "approve" | "reject" | "edit" | "respond";
505
- readonly editedArgs?: ReadonlyJSONValue;
506
- readonly message?: string;
507
- };
508
-
509
438
  export type InputResponse = ReadonlyJSONValue;
510
439
 
511
440
  export type RunInputCommand = {
@@ -515,9 +444,11 @@ export namespace StatewireThread {
515
444
  readonly response: InputResponse;
516
445
  };
517
446
 
518
- /** An input request projected out of the host state with its response echo. */
447
+ /** An input request projected out of the host state; a present `response` key marks the entry answered. */
519
448
  export type InputRequestState = InputRequest & {
520
- readonly response: InputResponse | null;
449
+ readonly response?: InputResponse;
450
+ /** The answer's `run/input` meta; absent when the answer carried none. */
451
+ readonly meta?: ReadonlyJSONValue;
521
452
  };
522
453
 
523
454
  export type RunReloadCommand = {
@@ -538,38 +469,14 @@ export namespace StatewireThread {
538
469
  | RunReloadCommand
539
470
  | RunInputCommand;
540
471
 
541
- export type Command = AssistantTransportThread.Command | RunCommand;
472
+ export type Command = RunCommand | UserCommands;
542
473
 
543
474
  /** A queued message projected out of the host state, wire shape. */
544
- export type QueueItem = {
475
+ export type RunQueueItem = {
545
476
  readonly id: string;
546
477
  readonly parts: readonly (TextPart | FilePart)[];
547
478
  };
548
479
 
549
- export type State = {
550
- readonly messages: readonly ThreadMessage[];
551
- readonly state?: ReadonlyJSONValue;
552
- readonly isRunning: boolean;
553
- /** The live run's id projected out of the host state; stamped onto `run/stop`. */
554
- readonly runId?: string | null;
555
- /**
556
- * The host's regular queue lane projected out of the state. Returning it
557
- * (even empty) turns on the queue capability: sends become `run/enqueue`
558
- * / `run/steer` commands and item controls send the `run/*` queue
559
- * commands. Presence must be stable across conversions.
560
- */
561
- readonly queue?: readonly QueueItem[];
562
- /** The host's steer lane; requires `queue`. Absent means empty. */
563
- readonly steerQueue?: readonly QueueItem[];
564
- /**
565
- * The host's parked input requests projected out of the state. Returning
566
- * it (even empty) routes tool results and approvals with a matching
567
- * pending request through `run/input`. Presence must be stable across
568
- * conversions.
569
- */
570
- readonly inputRequests?: readonly InputRequestState[];
571
- };
572
-
573
480
  /** Run subsystem status replicated by the host. */
574
481
  export type RunStatus = "running" | "stopped" | "error" | "input-required";
575
482
 
@@ -589,12 +496,12 @@ export namespace StatewireThread {
589
496
  readonly meta: ReadonlyJSONValue | null;
590
497
  };
591
498
 
592
- /** The dispatch record — the unacked batch handed to the run; null between the ack and the next dispatch or take. */
499
+ /** The dispatch record — the unacked batch handed to the run; absent between the ack and the next dispatch or take. */
593
500
  export type RunsDispatch = {
594
501
  readonly trigger:
595
- | "message-send"
596
- | "message-edit"
597
- | "message-reload"
502
+ | "send-message"
503
+ | "edit-message"
504
+ | "reload-message"
598
505
  | "input-resume"
599
506
  | "error-continue"
600
507
  | "stop-continue"
@@ -602,10 +509,17 @@ export namespace StatewireThread {
602
509
  readonly messages: readonly RunsQueueEntry[];
603
510
  /** Present on rewind dispatches: truncate the thread at this id first; null names the root. */
604
511
  readonly rollbackTo?: string | null;
512
+ /** The run-starting op's meta; present only when carried. */
513
+ readonly rootMeta?: ReadonlyJSONValue;
605
514
  /** Present when input outcomes ride the dispatch; visible until the ack. */
606
515
  readonly inputOutcomes?: readonly RunsInputOutcome[];
607
516
  };
608
517
 
518
+ /** The stopping window: present while a stop of the live run is underway; `"edit-message"` / `"reload-message"` pair with `nextDispatch`. */
519
+ export type RunsStopping = {
520
+ readonly reason: "stop" | "edit-message" | "reload-message";
521
+ };
522
+
609
523
  /** The replicated run error — set on an unhandled failure, cleared by the next user-initiated run. */
610
524
  export type RunsError = {
611
525
  readonly message: string;
@@ -620,10 +534,14 @@ export namespace StatewireThread {
620
534
  readonly queue: readonly RunsQueueEntry[];
621
535
  readonly steerQueue: readonly RunsQueueEntry[];
622
536
  /** The dispatch record — the unacked batch handed to the run, cleared at the ack. */
623
- readonly dispatching: RunsDispatch | null;
537
+ readonly dispatching?: RunsDispatch;
538
+ readonly stopping?: RunsStopping;
539
+ /** The complete dispatch staged behind a stopping rewind; promotes under the entry's `runId`. */
540
+ readonly nextDispatch?: RunsDispatch;
624
541
  /** The parked pending set; present only in input-required. */
625
542
  readonly inputRequests?: readonly InputRequestState[];
626
- readonly error: RunsError | null;
543
+ /** Set only by an unhandled throw; cleared by the next user-initiated run. */
544
+ readonly error?: RunsError;
627
545
  };
628
546
 
629
547
  /** App-shaped effects `runsMaterialize` applies for the dispatch record. */
@@ -642,17 +560,21 @@ export namespace StatewireThread {
642
560
  readonly runs: readonly RunState[];
643
561
  };
644
562
 
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. */
563
+ /** The run subsystem's typed read — `runs[0]` of the state with unsettled commands folded in, resting defaults while no entry is live. */
646
564
  export type Runs = {
647
565
  /** The live run's id — non-null whenever a run entry is live (running, input-required, error/stop); the target for `run/*` sends. */
648
566
  readonly runId: string | null;
649
567
  readonly queue: readonly RunsQueueEntry[];
650
568
  readonly steerQueue: readonly RunsQueueEntry[];
651
569
  /** The dispatch record — the unacked batch handed to the run, cleared at the ack. */
652
- readonly dispatching: RunsDispatch | null;
570
+ readonly dispatching?: RunsDispatch;
571
+ readonly stopping?: RunsStopping;
572
+ /** The complete dispatch staged behind a stopping rewind; promotes under the entry's `runId`. */
573
+ readonly nextDispatch?: RunsDispatch;
653
574
  /** The parked pending set; present only in input-required. */
654
575
  readonly inputRequests?: readonly InputRequestState[];
655
- readonly error: RunsError | null;
576
+ /** Set only by an unhandled throw; cleared by the next user-initiated run. */
577
+ readonly error?: RunsError;
656
578
  };
657
579
 
658
580
  export type ConnectionMetadata = {
@@ -660,25 +582,20 @@ export namespace StatewireThread {
660
582
  isSending: boolean;
661
583
  /** Statewire connection status; local transports are always `live`. */
662
584
  connection: StatewireClient.ConnectionState;
663
- /** The typed runs read; present iff the `runs` option is set. */
664
- runs?: Runs;
585
+ /** The typed runs read. */
586
+ runs: Runs;
665
587
  };
666
588
 
667
- export type StateConverter<T> = (
668
- state: T,
669
- connectionMetadata: ConnectionMetadata,
670
- ) => State;
671
-
672
- /** Converter output in runs mode: app-owned keys only — the lib projects the run mounts itself. */
673
- export type RunsConverterState = {
589
+ /** Converter output: app-owned keys only — the lib projects the run mounts itself. */
590
+ export type State = {
674
591
  readonly messages: readonly ThreadMessage[];
675
592
  readonly state?: ReadonlyJSONValue;
676
593
  };
677
594
 
678
- export type RunsStateConverter<T> = (
595
+ export type StateConverter<T> = (
679
596
  state: T,
680
- connectionMetadata: ConnectionMetadata & { runs: Runs },
681
- ) => RunsConverterState;
597
+ connectionMetadata: ConnectionMetadata,
598
+ ) => State;
682
599
 
683
600
  /**
684
601
  * Payload of the built-in `run/stop` command. `reason` is the one well-known
@@ -691,10 +608,18 @@ export namespace StatewireThread {
691
608
 
692
609
  export type Options<T> = {
693
610
  transport: Transport<T>;
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. */
611
+ /** The pre-attach state, replaced by the attach snapshot. `runs: []` projects ready (interactive from the first render); an absent runs mount projects loading until attach. */
695
612
  initialState: T;
696
613
  converter: StateConverter<T>;
697
- runs?: undefined;
614
+ /** Folds app-owned keys for an unsettled command into the draft; runs after the lib's run-dialect reducer. */
615
+ optimistic?:
616
+ | StatewireClient.Optimistic<
617
+ T,
618
+ Record<string, (...args: any[]) => unknown>
619
+ >
620
+ | undefined;
621
+ /** Makes dispatch records concrete in the draft (runs after the optimistic folds); without it, dispatch limbo stays out of `messages`. */
622
+ materialize?: StatewireClient.Materialize<NonNullable<T>> | undefined;
698
623
  /**
699
624
  * "command-send" mirrors the client's ambient model context (system,
700
625
  * tools, callSettings, config) to the server over the command channel —
@@ -738,25 +663,9 @@ export namespace StatewireThread {
738
663
  }
739
664
  | undefined;
740
665
  };
741
-
742
- /** Options with the runs read enabled: the lib types the run mounts and the converter returns app-owned keys only. */
743
- export type RunsOptions<T> = Omit<Options<T>, "converter" | "runs"> & {
744
- /** `true` reads the run mounts off the state root; a selector picks a nested mount. */
745
- runs: true | ((state: T) => unknown);
746
- converter: RunsStateConverter<T>;
747
- /** Folds app-owned keys for an unsettled command into the draft; runs after the lib's run-dialect reducer. */
748
- optimistic?:
749
- | StatewireClient.Optimistic<
750
- T,
751
- Record<string, (...args: any[]) => unknown>
752
- >
753
- | undefined;
754
- /** Makes dispatch records concrete in the draft (runs after the optimistic folds); without it, dispatch limbo stays out of `messages`. */
755
- materialize?: StatewireClient.Materialize<NonNullable<T>> | undefined;
756
- };
757
666
  }
758
667
 
759
- declare module "@assistant-ui/react" {
668
+ declare module "@assistant-ui/core" {
760
669
  namespace Assistant {
761
670
  interface Commands {
762
671
  runEnqueue: StatewireThread.RunEnqueueCommand;