@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.
@@ -1,36 +1,23 @@
1
+ import type { ReadonlyJSONValue } from "assistant-stream/utils";
1
2
  import type { Statewire, StatewireClient } from "statewire";
2
3
  import type { StatewireThread } from "./StatewireThread";
3
4
 
4
- type LaneEntry = {
5
- id: string;
6
- role: "user";
7
- parts: readonly (StatewireThread.TextPart | StatewireThread.FilePart)[];
8
- metadata?: Record<string, unknown>;
9
- meta?: unknown;
10
- };
5
+ // Opaque payloads (meta, response, rollbackTo) stay readonly: only reassigned, never mutated.
6
+ type Mutable<T> = ReadonlyJSONValue extends T
7
+ ? T
8
+ : T extends object
9
+ ? { -readonly [K in keyof T]: Mutable<T[K]> }
10
+ : T;
11
11
 
12
- type Dispatch = {
13
- trigger: string;
14
- messages: LaneEntry[];
15
- rollbackTo?: string | null;
16
- };
12
+ type LaneEntry = Mutable<StatewireThread.RunsQueueEntry>;
13
+ type Dispatch = Mutable<StatewireThread.RunsDispatch>;
14
+ type RunEntry = Mutable<StatewireThread.RunState>;
15
+ type RunsDraft = Partial<Mutable<StatewireThread.RunsState>>;
17
16
 
18
- type RunEntry = {
19
- runId: string | null;
20
- status: StatewireThread.RunStatus;
21
- queue: LaneEntry[];
22
- steerQueue: LaneEntry[];
23
- dispatching?: Dispatch;
24
- stopping?: { reason: "stop" | "message-edit" | "message-reload" };
25
- nextDispatch?: Dispatch;
26
- inputRequests?: { id: string; response?: unknown }[];
27
- error?: unknown;
28
- };
29
-
30
- type RunsDraft = {
31
- status: "ready" | StatewireThread.RunStatus;
32
- runs?: RunEntry[];
33
- };
17
+ const laneEntry = (message: StatewireThread.UserMessage): LaneEntry => ({
18
+ ...message,
19
+ parts: [...message.parts],
20
+ });
34
21
 
35
22
  const startRun = (draft: RunsDraft, runId: string): RunEntry => {
36
23
  const entry = ((draft.runs ??= [])[0] ??= {
@@ -86,13 +73,13 @@ const enqueueOrSteer = (
86
73
  sourceIndex = sourceLane.findIndex((e) => e.id === id);
87
74
  entry = sourceLane.splice(sourceIndex, 1)[0]!;
88
75
  if ("message" in command) {
89
- entry.parts = command.message.parts;
76
+ entry.parts = [...command.message.parts];
90
77
  if (command.message.metadata !== undefined)
91
78
  entry.metadata = command.message.metadata;
92
79
  }
93
80
  } else {
94
81
  if (!("message" in command)) return;
95
- entry = { ...command.message };
82
+ entry = laneEntry(command.message);
96
83
  }
97
84
 
98
85
  const steer = command.type === "run/steer";
@@ -137,10 +124,18 @@ const enqueueOrSteer = (
137
124
  };
138
125
 
139
126
  const stageRewind = (run: RunEntry, dispatch: Dispatch): void => {
127
+ const staged = run.nextDispatch;
128
+ if (
129
+ staged?.trigger === "message-edit" &&
130
+ dispatch.trigger === "message-edit"
131
+ ) {
132
+ staged.messages.push(...dispatch.messages);
133
+ } else {
134
+ run.nextDispatch = dispatch;
135
+ }
140
136
  run.stopping = {
141
- reason: dispatch.trigger as "message-edit" | "message-reload",
137
+ reason: run.nextDispatch!.trigger as "message-edit" | "message-reload",
142
138
  };
143
- run.nextDispatch = dispatch;
144
139
  };
145
140
 
146
141
  /** The run dialect's optimistic reducer: folds an unsettled `run/*` command into the mount draft. */
@@ -170,7 +165,7 @@ export const runsOptimistic = (
170
165
  .params[0] as StatewireThread.RunEditCommand;
171
166
  const dispatch: Dispatch = {
172
167
  trigger: "message-edit",
173
- messages: [{ ...message }],
168
+ messages: [laneEntry(message)],
174
169
  rollbackTo: runAnchorMessageId,
175
170
  };
176
171
  const run = draft.runs?.[0];
@@ -187,15 +182,15 @@ export const runsOptimistic = (
187
182
  rollbackTo: runAnchorMessageId,
188
183
  };
189
184
  const run = draft.runs?.[0];
190
- if (run?.status === "running") stageRewind(run, dispatch);
191
- else startRun(draft, runId).dispatching = dispatch;
185
+ if (run?.status === "running") {
186
+ if (run.nextDispatch === undefined) stageRewind(run, dispatch);
187
+ } else startRun(draft, runId).dispatching = dispatch;
192
188
  break;
193
189
  }
194
190
  case "run/stop": {
195
191
  const run = draft.runs?.[0];
196
192
  if (run?.status !== "running") break;
197
- run.stopping = { reason: "stop" };
198
- delete run.nextDispatch;
193
+ if (run.nextDispatch === undefined) run.stopping = { reason: "stop" };
199
194
  break;
200
195
  }
201
196
  case "run/continue": {