@assistant-ui/react-statewire 0.8.0 → 0.10.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.
@@ -175,7 +175,7 @@ describe("runsOptimistic", () => {
175
175
  expect(run(draft).steerQueue).toEqual([]);
176
176
  });
177
177
 
178
- it("run/edit, run/reload, and run/continue start the run optimistically", () => {
178
+ it("run/edit predicts the rewind dispatch record", () => {
179
179
  const draft = mount({
180
180
  status: "input-required",
181
181
  runId: "run-1",
@@ -185,11 +185,36 @@ describe("runsOptimistic", () => {
185
185
  type: "run/edit",
186
186
  message: message("m2b"),
187
187
  sourceId: "m2",
188
+ rollbackTo: "a1",
188
189
  });
189
190
  expect(draft.status).toBe("running");
190
191
  expect(run(draft).runId).toBe("optimistic-7");
191
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
+ });
192
216
 
217
+ it("run/continue starts the run and drains the steer lane optimistically", () => {
193
218
  const stopped = mount({ status: "stopped", steerQueue: [message("s1")] });
194
219
  apply(stopped, "run/continue", { type: "run/continue" });
195
220
  expect(stopped.status).toBe("running");
@@ -144,9 +144,26 @@ export const runsOptimistic = (
144
144
  );
145
145
  break;
146
146
  }
147
- 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
+ }
148
158
  case "run/reload": {
159
+ const { rollbackTo } = command
160
+ .params[0] as StatewireThread.RunReloadCommand;
149
161
  startRun(draft, runId);
162
+ draft.runs[0]!.dispatching = {
163
+ trigger: "message-reload",
164
+ messages: [],
165
+ rollbackTo,
166
+ };
150
167
  break;
151
168
  }
152
169
  case "run/continue": {
@@ -20,6 +20,7 @@ import {
20
20
  useAssistantTransportSendCommand,
21
21
  useAssistantTransportState,
22
22
  } from "./hooks";
23
+ import { runsMaterialize } from "./runsMaterialize";
23
24
 
24
25
  import {
25
26
  INITIAL_STATE,
@@ -34,6 +35,7 @@ import {
34
35
  sseEvent,
35
36
  threadTexts,
36
37
  toolCallConverter,
38
+ toThreadMessage,
37
39
  } from "./__tests__/testHelpers";
38
40
  import type { StreamResponse, TestState } from "./__tests__/testHelpers";
39
41
 
@@ -1322,6 +1324,7 @@ describe("AssistantTransportThread", () => {
1322
1324
  parts: [{ type: "text", text: "edited" }],
1323
1325
  },
1324
1326
  sourceId: "u2",
1327
+ rollbackTo: "a1",
1325
1328
  },
1326
1329
  ]);
1327
1330
  expect("parentId" in fetchMock.requests[0]!.body).toBe(false);
@@ -2059,6 +2062,7 @@ describe("reload", () => {
2059
2062
  {
2060
2063
  type: "run/reload",
2061
2064
  sourceId: "m2",
2065
+ rollbackTo: "m1",
2062
2066
  },
2063
2067
  ]);
2064
2068
  expect("parentId" in fetchMock.requests[1]!.body).toBe(false);
@@ -2093,6 +2097,7 @@ describe("reload", () => {
2093
2097
  {
2094
2098
  type: "run/reload",
2095
2099
  sourceId: "m2",
2100
+ rollbackTo: "m1",
2096
2101
  },
2097
2102
  ]);
2098
2103
  });
@@ -2754,3 +2759,158 @@ describe("input requests", () => {
2754
2759
  unmount();
2755
2760
  });
2756
2761
  });
2762
+
2763
+ describe("runs fold and materialize", () => {
2764
+ type RunsTestState = TestState & {
2765
+ status: "ready" | "running";
2766
+ runs: {
2767
+ runId: string | null;
2768
+ status: string;
2769
+ queue: { id: string }[];
2770
+ steerQueue: { id: string }[];
2771
+ dispatching: {
2772
+ trigger: string;
2773
+ messages: {
2774
+ id: string;
2775
+ role: "user";
2776
+ parts: { type: "text"; text: string }[];
2777
+ }[];
2778
+ rollbackTo?: string | null;
2779
+ } | null;
2780
+ error: null;
2781
+ }[];
2782
+ };
2783
+
2784
+ const runsConverter: AssistantTransportThread.StateConverter<
2785
+ RunsTestState
2786
+ > = (state) => ({
2787
+ messages: state.messages.map(toThreadMessage),
2788
+ isRunning: state.status === "running",
2789
+ });
2790
+
2791
+ const materialize = (draft: RunsTestState): void =>
2792
+ runsMaterialize(draft, {
2793
+ append: (m) =>
2794
+ draft.messages.push({
2795
+ id: m.id,
2796
+ role: m.role,
2797
+ text: m.parts.map((p) => (p.type === "text" ? p.text : "")).join(""),
2798
+ }),
2799
+ rollback: (to) => {
2800
+ if (to === null) {
2801
+ draft.messages = [];
2802
+ return;
2803
+ }
2804
+ draft.messages.splice(draft.messages.findIndex((m) => m.id === to) + 1);
2805
+ },
2806
+ });
2807
+
2808
+ const mountRuns = (init?: Partial<RunsTestState>) => {
2809
+ const captured: { aui?: ReturnType<typeof useAui> } = {};
2810
+ const Capture: FC = () => {
2811
+ captured.aui = useAui();
2812
+ return null;
2813
+ };
2814
+ const App: FC = () => {
2815
+ const aui = useAui({
2816
+ thread: AssistantTransportThread<RunsTestState>({
2817
+ initialState: { messages: [], status: "ready", runs: [], ...init },
2818
+ api: "https://example.com/api",
2819
+ headers: {},
2820
+ converter: runsConverter,
2821
+ runs: true,
2822
+ materialize,
2823
+ capabilities: { edit: true },
2824
+ protocol: "assistant-transport",
2825
+ }),
2826
+ });
2827
+ return (
2828
+ <AuiProvider value={aui}>
2829
+ <Capture />
2830
+ </AuiProvider>
2831
+ );
2832
+ };
2833
+ render(<App />);
2834
+ return { aui: () => captured.aui! };
2835
+ };
2836
+
2837
+ it("projects a pending send through the runs fold and materialize", async () => {
2838
+ const fetchMock = installFetch();
2839
+ let server!: StreamResponse;
2840
+ fetchMock.setHandler(() => {
2841
+ server = createStreamResponse();
2842
+ return server.response;
2843
+ });
2844
+
2845
+ const { aui } = mountRuns();
2846
+ aui().thread().append("go");
2847
+ await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
2848
+
2849
+ // The lib fold predicts the dispatch record; materialize renders it. One copy.
2850
+ expect(threadTexts(aui)).toEqual(["go"]);
2851
+ expect(aui().thread().getState().isRunning).toBe(true);
2852
+
2853
+ const sent = fetchMock.requests[0]!.body.commands[0] as {
2854
+ message: { id: string };
2855
+ };
2856
+ server.emit({
2857
+ type: "update-state",
2858
+ operations: [
2859
+ {
2860
+ type: "set",
2861
+ path: [],
2862
+ value: {
2863
+ messages: [],
2864
+ status: "running",
2865
+ runs: [
2866
+ {
2867
+ runId: "run-1",
2868
+ status: "running",
2869
+ queue: [],
2870
+ steerQueue: [],
2871
+ dispatching: {
2872
+ trigger: "message-send",
2873
+ messages: [
2874
+ {
2875
+ id: sent.message.id,
2876
+ role: "user",
2877
+ parts: [{ type: "text", text: "go" }],
2878
+ },
2879
+ ],
2880
+ },
2881
+ error: null,
2882
+ },
2883
+ ],
2884
+ },
2885
+ },
2886
+ ],
2887
+ });
2888
+
2889
+ // Canonical echo retires the overlay in the same publish: still one copy.
2890
+ await waitFor(() => expect(threadTexts(aui)).toEqual(["go"]));
2891
+ });
2892
+
2893
+ it("rewinds through the materialize fold for a pending edit", async () => {
2894
+ const fetchMock = installFetch();
2895
+ fetchMock.setHandler(() => createStreamResponse().response);
2896
+
2897
+ const { aui } = mountRuns({
2898
+ messages: [
2899
+ { id: "u1", role: "user", text: "one" },
2900
+ { id: "a1", role: "assistant", text: "re one" },
2901
+ ],
2902
+ });
2903
+ sendCommand(aui, {
2904
+ type: "run/edit",
2905
+ message: {
2906
+ id: "u1b",
2907
+ role: "user",
2908
+ parts: [{ type: "text", text: "one'" }],
2909
+ },
2910
+ sourceId: "u1",
2911
+ rollbackTo: null,
2912
+ });
2913
+
2914
+ await waitFor(() => expect(threadTexts(aui)).toEqual(["one'"]));
2915
+ });
2916
+ });