@assistant-ui/react-statewire 0.7.0 → 0.9.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.
@@ -0,0 +1,91 @@
1
+ // Contract tests for the run-dialect materialize fold: the dispatch record
2
+ // becomes concrete through app-shaped effects, in record order.
3
+
4
+ import { describe, expect, it } from "vitest";
5
+ import { runsMaterialize } from "./runsMaterialize";
6
+
7
+ type Draft = Record<string, any>;
8
+
9
+ const mount = (dispatching: Draft | null): Draft => ({
10
+ status: "running",
11
+ runs: [
12
+ {
13
+ runId: "run-1",
14
+ status: "running",
15
+ queue: [],
16
+ steerQueue: [],
17
+ dispatching,
18
+ error: null,
19
+ },
20
+ ],
21
+ });
22
+
23
+ const message = (id: string) => ({
24
+ id,
25
+ role: "user" as const,
26
+ parts: [{ type: "text" as const, text: id }],
27
+ });
28
+
29
+ const record = () => {
30
+ const appended: string[] = [];
31
+ const rollbacks: (string | null)[] = [];
32
+ return {
33
+ appended,
34
+ rollbacks,
35
+ effects: {
36
+ append: (m: { id: string }) => appended.push(m.id),
37
+ rollback: (to: string | null) => {
38
+ rollbacks.push(to);
39
+ appended.push("(rollback)");
40
+ },
41
+ },
42
+ };
43
+ };
44
+
45
+ describe("runsMaterialize", () => {
46
+ it("does nothing while resting or between dispatches", () => {
47
+ const { appended, effects } = record();
48
+ runsMaterialize({ status: "ready", runs: [] }, effects);
49
+ runsMaterialize(mount(null), effects);
50
+ expect(appended).toEqual([]);
51
+ });
52
+
53
+ it("appends each dispatched message in record order", () => {
54
+ const { appended, effects } = record();
55
+ runsMaterialize(
56
+ mount({
57
+ trigger: "message-send",
58
+ messages: [message("m1"), message("m2")],
59
+ }),
60
+ effects,
61
+ );
62
+ expect(appended).toEqual(["m1", "m2"]);
63
+ });
64
+
65
+ it("rolls back before appending on rewind dispatches, null naming the root", () => {
66
+ const { appended, rollbacks, effects } = record();
67
+ runsMaterialize(
68
+ mount({
69
+ trigger: "message-edit",
70
+ messages: [message("m3")],
71
+ rollbackTo: "m1",
72
+ }),
73
+ effects,
74
+ );
75
+ runsMaterialize(
76
+ mount({ trigger: "message-reload", messages: [], rollbackTo: null }),
77
+ effects,
78
+ );
79
+ expect(appended).toEqual(["(rollback)", "m3", "(rollback)"]);
80
+ expect(rollbacks).toEqual(["m1", null]);
81
+ });
82
+
83
+ it("throws on a rewind dispatch without a rollback effect", () => {
84
+ expect(() =>
85
+ runsMaterialize(
86
+ mount({ trigger: "message-edit", messages: [], rollbackTo: "m1" }),
87
+ { append: () => {} },
88
+ ),
89
+ ).toThrow("rollbackTo");
90
+ });
91
+ });
@@ -0,0 +1,23 @@
1
+ import type { StatewireThread } from "./StatewireThread";
2
+
3
+ /**
4
+ * The run dialect's materialize fold: makes the mount's dispatch record
5
+ * concrete through app-shaped effects, in record order.
6
+ */
7
+ export const runsMaterialize = (
8
+ mount: unknown,
9
+ effects: StatewireThread.MaterializeEffects,
10
+ ): void => {
11
+ const record =
12
+ (mount as StatewireThread.RunsState).runs?.[0]?.dispatching ?? null;
13
+ if (record === null) return;
14
+ if ("rollbackTo" in record) {
15
+ if (effects.rollback === undefined) {
16
+ throw new Error(
17
+ "statewire: the dispatch record carries rollbackTo but no rollback effect was provided",
18
+ );
19
+ }
20
+ effects.rollback(record.rollbackTo ?? null);
21
+ }
22
+ for (const message of record.messages) effects.append(message);
23
+ };
@@ -20,7 +20,6 @@ const mount = (entry?: Draft): Draft =>
20
20
  steerQueue: [],
21
21
  dispatching: null,
22
22
  error: null,
23
- runLeafMessageId: null,
24
23
  ...entry,
25
24
  },
26
25
  ],
@@ -176,7 +175,7 @@ describe("runsOptimistic", () => {
176
175
  expect(run(draft).steerQueue).toEqual([]);
177
176
  });
178
177
 
179
- it("run/edit, run/reload, and run/continue start the run optimistically", () => {
178
+ it("run/edit predicts the rewind dispatch record", () => {
180
179
  const draft = mount({
181
180
  status: "input-required",
182
181
  runId: "run-1",
@@ -186,11 +185,36 @@ describe("runsOptimistic", () => {
186
185
  type: "run/edit",
187
186
  message: message("m2b"),
188
187
  sourceId: "m2",
188
+ rollbackTo: "a1",
189
189
  });
190
190
  expect(draft.status).toBe("running");
191
191
  expect(run(draft).runId).toBe("optimistic-7");
192
192
  expect(run(draft).inputRequests).toBeUndefined();
193
+ expect(run(draft).dispatching).toEqual({
194
+ trigger: "message-edit",
195
+ messages: [message("m2b")],
196
+ rollbackTo: "a1",
197
+ });
198
+ });
199
+
200
+ it("run/reload predicts the rewind dispatch record", () => {
201
+ const draft = mount({ status: "stopped", error: { message: "boom" } });
202
+ apply(draft, "run/reload", {
203
+ type: "run/reload",
204
+ sourceId: "a1",
205
+ rollbackTo: null,
206
+ });
207
+ expect(draft.status).toBe("running");
208
+ expect(run(draft).runId).toBe("optimistic-7");
209
+ expect(run(draft).error).toBeNull();
210
+ expect(run(draft).dispatching).toEqual({
211
+ trigger: "message-reload",
212
+ messages: [],
213
+ rollbackTo: null,
214
+ });
215
+ });
193
216
 
217
+ it("run/continue starts the run and drains the steer lane optimistically", () => {
194
218
  const stopped = mount({ status: "stopped", steerQueue: [message("s1")] });
195
219
  apply(stopped, "run/continue", { type: "run/continue" });
196
220
  expect(stopped.status).toBe("running");
@@ -213,7 +237,6 @@ describe("runsOptimistic", () => {
213
237
  steerQueue: [],
214
238
  dispatching: { trigger: "message-send", messages: [message("s1")] },
215
239
  error: null,
216
- runLeafMessageId: null,
217
240
  });
218
241
  });
219
242
 
@@ -21,7 +21,6 @@ type RunEntry = {
21
21
  } | null;
22
22
  inputRequests?: { id: string; response: unknown }[];
23
23
  error: unknown;
24
- runLeafMessageId: string | null;
25
24
  };
26
25
 
27
26
  type RunsDraft = {
@@ -37,7 +36,6 @@ const startRun = (draft: RunsDraft, runId: string): void => {
37
36
  steerQueue: [],
38
37
  dispatching: null,
39
38
  error: null,
40
- runLeafMessageId: null,
41
39
  });
42
40
  entry.status = "running";
43
41
  entry.runId = runId;
@@ -146,9 +144,26 @@ export const runsOptimistic = (
146
144
  );
147
145
  break;
148
146
  }
149
- case "run/edit":
147
+ case "run/edit": {
148
+ const { message, rollbackTo } = command
149
+ .params[0] as StatewireThread.RunEditCommand;
150
+ startRun(draft, runId);
151
+ draft.runs[0]!.dispatching = {
152
+ trigger: "message-edit",
153
+ messages: [{ ...message }],
154
+ rollbackTo,
155
+ };
156
+ break;
157
+ }
150
158
  case "run/reload": {
159
+ const { rollbackTo } = command
160
+ .params[0] as StatewireThread.RunReloadCommand;
151
161
  startRun(draft, runId);
162
+ draft.runs[0]!.dispatching = {
163
+ trigger: "message-reload",
164
+ messages: [],
165
+ rollbackTo,
166
+ };
152
167
  break;
153
168
  }
154
169
  case "run/continue": {
@@ -1322,6 +1322,7 @@ describe("AssistantTransportThread", () => {
1322
1322
  parts: [{ type: "text", text: "edited" }],
1323
1323
  },
1324
1324
  sourceId: "u2",
1325
+ rollbackTo: "a1",
1325
1326
  },
1326
1327
  ]);
1327
1328
  expect("parentId" in fetchMock.requests[0]!.body).toBe(false);
@@ -2059,6 +2060,7 @@ describe("reload", () => {
2059
2060
  {
2060
2061
  type: "run/reload",
2061
2062
  sourceId: "m2",
2063
+ rollbackTo: "m1",
2062
2064
  },
2063
2065
  ]);
2064
2066
  expect("parentId" in fetchMock.requests[1]!.body).toBe(false);
@@ -2093,6 +2095,7 @@ describe("reload", () => {
2093
2095
  {
2094
2096
  type: "run/reload",
2095
2097
  sourceId: "m2",
2098
+ rollbackTo: "m1",
2096
2099
  },
2097
2100
  ]);
2098
2101
  });