@assistant-ui/react-statewire 0.4.4 → 0.6.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.
- package/dist/AssistantTransportThread.d.ts +9 -7
- package/dist/AssistantTransportThread.d.ts.map +1 -1
- package/dist/AssistantTransportThread.js +47 -35
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +90 -9
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +74 -45
- package/dist/StatewireThread.js.map +1 -1
- package/dist/hooks.d.ts +4 -1
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +10 -1
- package/dist/hooks.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/runCommands.d.ts +2 -1
- package/dist/runCommands.d.ts.map +1 -1
- package/dist/runCommands.js +25 -11
- package/dist/runCommands.js.map +1 -1
- package/dist/runsOptimistic.d.ts +7 -0
- package/dist/runsOptimistic.d.ts.map +1 -0
- package/dist/runsOptimistic.js +92 -0
- package/dist/runsOptimistic.js.map +1 -0
- package/package.json +3 -3
- package/src/AssistantTransportThread.ts +77 -33
- package/src/StatewireThread.runs.test.tsx +405 -0
- package/src/StatewireThread.test.tsx +177 -1
- package/src/StatewireThread.ts +218 -77
- package/src/__tests__/testHelpers.tsx +53 -53
- package/src/athena.e2e.test.tsx +26 -23
- package/src/hooks.ts +12 -0
- package/src/index.ts +1 -0
- package/src/legacy-diff.test.tsx +36 -47
- package/src/runCommands.ts +28 -12
- package/src/runsOptimistic.test.ts +225 -0
- package/src/runsOptimistic.ts +164 -0
- package/src/transport.test.tsx +187 -88
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
// Contract tests for StatewireThread's runs mode at its public seam: the run
|
|
4
|
+
// mounts read as typed data, with unsettled commands folded in by the
|
|
5
|
+
// run-dialect optimistic reducer.
|
|
6
|
+
|
|
7
|
+
import { act, render, waitFor } from "@testing-library/react";
|
|
8
|
+
import type { FC } from "react";
|
|
9
|
+
import { describe, expect, it } from "vitest";
|
|
10
|
+
import { AuiProvider, useAui } from "@assistant-ui/store";
|
|
11
|
+
import { resource } from "@assistant-ui/tap";
|
|
12
|
+
import { StatewireSSE } from "statewire";
|
|
13
|
+
import { useStatewireCommands, useStatewireState } from "statewire/host";
|
|
14
|
+
import { StatewireThread } from "./StatewireThread";
|
|
15
|
+
import { useStatewireRuns } from "./hooks";
|
|
16
|
+
import { sendCommand, toThreadMessage } from "./__tests__/testHelpers";
|
|
17
|
+
|
|
18
|
+
type ServerRunEntry = {
|
|
19
|
+
runId: string | null;
|
|
20
|
+
status: StatewireThread.RunStatus;
|
|
21
|
+
queue: {
|
|
22
|
+
id: string;
|
|
23
|
+
role: "user";
|
|
24
|
+
parts: { type: "text"; text: string }[];
|
|
25
|
+
}[];
|
|
26
|
+
steerQueue: ServerRunEntry["queue"];
|
|
27
|
+
error: { message: string } | null;
|
|
28
|
+
runLeafMessageId: string | null;
|
|
29
|
+
inputRequests?: {
|
|
30
|
+
type: string;
|
|
31
|
+
id: string;
|
|
32
|
+
toolCallId?: string;
|
|
33
|
+
response: Record<string, unknown> | null;
|
|
34
|
+
}[];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
type RunsServerState = {
|
|
38
|
+
messages: { id: string; role: "user" | "assistant"; text: string }[];
|
|
39
|
+
status: StatewireThread.RunStatus;
|
|
40
|
+
runs: ServerRunEntry[];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const INITIAL_STATE: RunsServerState = {
|
|
44
|
+
messages: [],
|
|
45
|
+
status: "ready",
|
|
46
|
+
runs: [],
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const withRun = (
|
|
50
|
+
entry: Partial<ServerRunEntry> & { status: StatewireThread.RunStatus },
|
|
51
|
+
): Partial<RunsServerState> => ({
|
|
52
|
+
status: entry.status,
|
|
53
|
+
runs: [
|
|
54
|
+
{
|
|
55
|
+
runId: null,
|
|
56
|
+
queue: [],
|
|
57
|
+
steerQueue: [],
|
|
58
|
+
error: null,
|
|
59
|
+
runLeafMessageId: null,
|
|
60
|
+
...entry,
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const converter: StatewireThread.RunsStateConverter<RunsServerState> = (
|
|
66
|
+
state,
|
|
67
|
+
) => ({
|
|
68
|
+
messages: state.messages.map(toThreadMessage),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const createServer = (init?: Partial<RunsServerState>) => {
|
|
72
|
+
const received: unknown[] = [];
|
|
73
|
+
const cell: { state?: RunsServerState } = {};
|
|
74
|
+
const transport = resource(() => {
|
|
75
|
+
const [state] = useStatewireState<RunsServerState>(() => ({
|
|
76
|
+
...INITIAL_STATE,
|
|
77
|
+
...init,
|
|
78
|
+
}));
|
|
79
|
+
cell.state = state;
|
|
80
|
+
const record = (command: unknown) => {
|
|
81
|
+
received.push(command);
|
|
82
|
+
};
|
|
83
|
+
const commands = useStatewireCommands({
|
|
84
|
+
"run/enqueue": record,
|
|
85
|
+
"run/steer": record,
|
|
86
|
+
"run/dequeue": record,
|
|
87
|
+
"run/edit": record,
|
|
88
|
+
"run/reload": record,
|
|
89
|
+
"run/continue": record,
|
|
90
|
+
"run/stop": record,
|
|
91
|
+
"run/input": record,
|
|
92
|
+
});
|
|
93
|
+
return { state, commands };
|
|
94
|
+
})();
|
|
95
|
+
return { transport, received, cell };
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// A remote SSE fake that delivers the snapshot and acks command posts but
|
|
99
|
+
// never settles them, so optimistic overlays stay visible.
|
|
100
|
+
const createRemoteServer = (init?: Partial<RunsServerState>) => {
|
|
101
|
+
const received: { method: string; params: unknown[] }[] = [];
|
|
102
|
+
const clients = new Map<string, number>();
|
|
103
|
+
const encoder = new TextEncoder();
|
|
104
|
+
const frame = (chunk: unknown) => `data: ${JSON.stringify(chunk)}\n\n`;
|
|
105
|
+
const fetchImpl = (async (input: RequestInfo | URL, init2?: RequestInit) => {
|
|
106
|
+
const url = String(input);
|
|
107
|
+
const headers = new Headers(init2?.headers);
|
|
108
|
+
const clientId = headers.get("Statewire-Client-Id")!;
|
|
109
|
+
if (url.endsWith("/stream")) {
|
|
110
|
+
const known = clients.get(clientId);
|
|
111
|
+
if (known === undefined) clients.set(clientId, 0);
|
|
112
|
+
const body = new ReadableStream<Uint8Array>({
|
|
113
|
+
start: (c) => {
|
|
114
|
+
c.enqueue(
|
|
115
|
+
encoder.encode(
|
|
116
|
+
frame({
|
|
117
|
+
ops: [
|
|
118
|
+
{
|
|
119
|
+
op: "replace",
|
|
120
|
+
path: [],
|
|
121
|
+
value: { ...INITIAL_STATE, ...init },
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
syn: { lastSeq: known ?? -1, lease: "lease-1" },
|
|
125
|
+
}),
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
return new Response(body, {
|
|
131
|
+
status: 200,
|
|
132
|
+
headers: { "content-type": "text/event-stream" },
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
clients.set(clientId, Number(headers.get("Statewire-Command-Seq")));
|
|
136
|
+
received.push(JSON.parse(String(init2?.body)));
|
|
137
|
+
return Response.json({});
|
|
138
|
+
}) as typeof fetch;
|
|
139
|
+
return {
|
|
140
|
+
received,
|
|
141
|
+
transport: StatewireSSE<RunsServerState>({
|
|
142
|
+
url: "http://fake/api",
|
|
143
|
+
fetch: fetchImpl,
|
|
144
|
+
}),
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const mountThread = (
|
|
149
|
+
options: Omit<
|
|
150
|
+
StatewireThread.RunsOptions<RunsServerState>,
|
|
151
|
+
"initialState" | "runs" | "converter"
|
|
152
|
+
> &
|
|
153
|
+
Partial<
|
|
154
|
+
Pick<
|
|
155
|
+
StatewireThread.RunsOptions<RunsServerState>,
|
|
156
|
+
"initialState" | "runs" | "converter"
|
|
157
|
+
>
|
|
158
|
+
>,
|
|
159
|
+
) => {
|
|
160
|
+
const captured: {
|
|
161
|
+
aui?: ReturnType<typeof useAui>;
|
|
162
|
+
runs?: StatewireThread.Runs;
|
|
163
|
+
} = {};
|
|
164
|
+
const Capture: FC = () => {
|
|
165
|
+
captured.aui = useAui();
|
|
166
|
+
captured.runs = useStatewireRuns();
|
|
167
|
+
return null;
|
|
168
|
+
};
|
|
169
|
+
const App: FC = () => {
|
|
170
|
+
const aui = useAui({
|
|
171
|
+
thread: StatewireThread<RunsServerState>({
|
|
172
|
+
initialState: INITIAL_STATE,
|
|
173
|
+
runs: true,
|
|
174
|
+
converter,
|
|
175
|
+
...options,
|
|
176
|
+
}),
|
|
177
|
+
});
|
|
178
|
+
return (
|
|
179
|
+
<AuiProvider value={aui}>
|
|
180
|
+
<Capture />
|
|
181
|
+
</AuiProvider>
|
|
182
|
+
);
|
|
183
|
+
};
|
|
184
|
+
render(<App />);
|
|
185
|
+
return { aui: () => captured.aui!, runs: () => captured.runs! };
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
describe("StatewireThread runs mode", () => {
|
|
189
|
+
it("reads the run mounts as typed data and drives isRunning", async () => {
|
|
190
|
+
const server = createServer({
|
|
191
|
+
messages: [{ id: "m1", role: "user", text: "hi" }],
|
|
192
|
+
...withRun({
|
|
193
|
+
status: "running",
|
|
194
|
+
runId: "run-1",
|
|
195
|
+
queue: [
|
|
196
|
+
{ id: "q1", role: "user", parts: [{ type: "text", text: "next" }] },
|
|
197
|
+
],
|
|
198
|
+
steerQueue: [
|
|
199
|
+
{ id: "s1", role: "user", parts: [{ type: "text", text: "now" }] },
|
|
200
|
+
],
|
|
201
|
+
runLeafMessageId: "m1",
|
|
202
|
+
}),
|
|
203
|
+
});
|
|
204
|
+
const { aui, runs } = mountThread({ transport: server.transport });
|
|
205
|
+
|
|
206
|
+
await waitFor(() => expect(runs().status).toBe("running"));
|
|
207
|
+
expect(runs().runId).toBe("run-1");
|
|
208
|
+
expect(runs().queue.map((e) => e.id)).toEqual(["q1"]);
|
|
209
|
+
expect(runs().steerQueue.map((e) => e.id)).toEqual(["s1"]);
|
|
210
|
+
expect(runs().runLeafMessageId).toBe("m1");
|
|
211
|
+
expect(runs().error).toBeNull();
|
|
212
|
+
expect(aui().thread().getState().isRunning).toBe(true);
|
|
213
|
+
|
|
214
|
+
act(() => {
|
|
215
|
+
server.cell.state!.status = "ready";
|
|
216
|
+
server.cell.state!.runs = [];
|
|
217
|
+
});
|
|
218
|
+
await waitFor(() =>
|
|
219
|
+
expect(aui().thread().getState().isRunning).toBe(false),
|
|
220
|
+
);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("delivers the runs read to the converter as meta.runs", async () => {
|
|
224
|
+
const server = createServer(withRun({ status: "stopped" }));
|
|
225
|
+
const observed: StatewireThread.Runs[] = [];
|
|
226
|
+
mountThread({
|
|
227
|
+
transport: server.transport,
|
|
228
|
+
converter: (state, meta) => {
|
|
229
|
+
observed.push(meta.runs);
|
|
230
|
+
return converter(state, meta);
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
await waitFor(() => expect(observed.at(-1)!.status).toBe("stopped"));
|
|
235
|
+
expect(observed.at(-1)!.runId).toBeNull();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("stamps the live run's id onto run/stop", async () => {
|
|
239
|
+
const server = createServer(withRun({ status: "running", runId: "run-7" }));
|
|
240
|
+
const { aui } = mountThread({ transport: server.transport });
|
|
241
|
+
|
|
242
|
+
await waitFor(() => expect(aui().thread().getState().isRunning).toBe(true));
|
|
243
|
+
act(() => {
|
|
244
|
+
aui().thread().cancelRun();
|
|
245
|
+
});
|
|
246
|
+
await waitFor(() =>
|
|
247
|
+
expect(server.received).toContainEqual({ runId: "run-7" }),
|
|
248
|
+
);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("reads the parked input set and routes approvals through it", async () => {
|
|
252
|
+
const server = createServer(
|
|
253
|
+
withRun({
|
|
254
|
+
status: "input-required",
|
|
255
|
+
runId: "run-1",
|
|
256
|
+
inputRequests: [
|
|
257
|
+
{ type: "tool-approval", id: "r1", toolCallId: "t1", response: null },
|
|
258
|
+
],
|
|
259
|
+
}),
|
|
260
|
+
);
|
|
261
|
+
const toolConverter: StatewireThread.RunsStateConverter<
|
|
262
|
+
RunsServerState
|
|
263
|
+
> = () => ({
|
|
264
|
+
messages: [
|
|
265
|
+
{
|
|
266
|
+
id: "a-tool",
|
|
267
|
+
role: "assistant",
|
|
268
|
+
createdAt: new Date(0),
|
|
269
|
+
content: [
|
|
270
|
+
{
|
|
271
|
+
type: "tool-call",
|
|
272
|
+
toolCallId: "t1",
|
|
273
|
+
toolName: "probe_tool",
|
|
274
|
+
args: {},
|
|
275
|
+
argsText: "{}",
|
|
276
|
+
approval: { id: "r1" },
|
|
277
|
+
},
|
|
278
|
+
],
|
|
279
|
+
attachments: [],
|
|
280
|
+
metadata: { custom: {} },
|
|
281
|
+
status: { type: "requires-action", reason: "tool-call" },
|
|
282
|
+
},
|
|
283
|
+
] as unknown as StatewireThread.RunsConverterState["messages"],
|
|
284
|
+
});
|
|
285
|
+
const { aui, runs } = mountThread({
|
|
286
|
+
transport: server.transport,
|
|
287
|
+
converter: toolConverter,
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
await waitFor(() =>
|
|
291
|
+
expect(runs().inputRequests).toEqual([
|
|
292
|
+
{ type: "tool-approval", id: "r1", toolCallId: "t1", response: null },
|
|
293
|
+
]),
|
|
294
|
+
);
|
|
295
|
+
act(() => {
|
|
296
|
+
aui()
|
|
297
|
+
.thread()
|
|
298
|
+
.message({ id: "a-tool" })
|
|
299
|
+
.part({ toolCallId: "t1" })
|
|
300
|
+
.respondToToolApproval({ approved: true });
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
await waitFor(() =>
|
|
304
|
+
expect(server.received).toContainEqual({
|
|
305
|
+
type: "run/input",
|
|
306
|
+
requestId: "r1",
|
|
307
|
+
response: { decision: "approve" },
|
|
308
|
+
}),
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
act(() => {
|
|
312
|
+
server.cell.state!.runs[0]!.inputRequests![0]!.response = {
|
|
313
|
+
decision: "approve",
|
|
314
|
+
};
|
|
315
|
+
});
|
|
316
|
+
await waitFor(() =>
|
|
317
|
+
expect(runs().inputRequests![0]!.response).toEqual({
|
|
318
|
+
decision: "approve",
|
|
319
|
+
}),
|
|
320
|
+
);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("folds a pending steer into the lane while running", async () => {
|
|
324
|
+
const server = createRemoteServer(
|
|
325
|
+
withRun({ status: "running", runId: "run-1" }),
|
|
326
|
+
);
|
|
327
|
+
const { aui, runs } = mountThread({ transport: server.transport });
|
|
328
|
+
|
|
329
|
+
await waitFor(() => expect(runs().status).toBe("running"));
|
|
330
|
+
act(() => {
|
|
331
|
+
sendCommand(aui, {
|
|
332
|
+
type: "run/steer",
|
|
333
|
+
message: {
|
|
334
|
+
id: "s1",
|
|
335
|
+
role: "user",
|
|
336
|
+
parts: [{ type: "text", text: "hello" }],
|
|
337
|
+
},
|
|
338
|
+
anchorMessageId: null,
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
await waitFor(() => expect(runs().steerQueue).toHaveLength(1));
|
|
343
|
+
expect(runs().steerQueue[0]!.parts).toEqual([
|
|
344
|
+
{ type: "text", text: "hello" },
|
|
345
|
+
]);
|
|
346
|
+
expect(runs().runId).toBe("run-1");
|
|
347
|
+
expect(server.received.map((c) => c.method)).toEqual(["run/steer"]);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("flips the run optimistically for a dispatching send", async () => {
|
|
351
|
+
const server = createRemoteServer();
|
|
352
|
+
const { aui, runs } = mountThread({ transport: server.transport });
|
|
353
|
+
|
|
354
|
+
await waitFor(() =>
|
|
355
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
356
|
+
);
|
|
357
|
+
act(() => {
|
|
358
|
+
aui().thread().append("go");
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
await waitFor(() => expect(runs().status).toBe("running"));
|
|
362
|
+
expect(runs().runId).toMatch(/^optimistic-/);
|
|
363
|
+
expect(runs().steerQueue).toEqual([]);
|
|
364
|
+
expect(aui().thread().getState().isRunning).toBe(true);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("runs the app optimistic reducer after the lib's", async () => {
|
|
368
|
+
const server = createRemoteServer();
|
|
369
|
+
const observedStatus: string[] = [];
|
|
370
|
+
const { aui } = mountThread({
|
|
371
|
+
transport: server.transport,
|
|
372
|
+
optimistic: (draft, command) => {
|
|
373
|
+
if (command.method !== "run/enqueue") return;
|
|
374
|
+
observedStatus.push(draft.status);
|
|
375
|
+
const message = (
|
|
376
|
+
command.params[0] as StatewireThread.RunEnqueueCommand & {
|
|
377
|
+
message: StatewireThread.UserMessage;
|
|
378
|
+
}
|
|
379
|
+
).message;
|
|
380
|
+
draft.messages.push({
|
|
381
|
+
id: message.id,
|
|
382
|
+
role: "user",
|
|
383
|
+
text: (message.parts[0] as StatewireThread.TextPart).text,
|
|
384
|
+
});
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
await waitFor(() =>
|
|
389
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
390
|
+
);
|
|
391
|
+
act(() => {
|
|
392
|
+
aui().thread().append("hi");
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
await waitFor(() =>
|
|
396
|
+
expect(
|
|
397
|
+
aui()
|
|
398
|
+
.thread()
|
|
399
|
+
.getState()
|
|
400
|
+
.messages.map((m) => m.id),
|
|
401
|
+
).toHaveLength(1),
|
|
402
|
+
);
|
|
403
|
+
expect(observedStatus).toEqual(["running"]);
|
|
404
|
+
});
|
|
405
|
+
});
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Contract tests for StatewireThread at its public seam: an in-process
|
|
4
4
|
// statewire host stands in for the server.
|
|
5
5
|
|
|
6
|
-
import { render, waitFor } from "@testing-library/react";
|
|
6
|
+
import { act, render, waitFor } from "@testing-library/react";
|
|
7
7
|
import type { FC, ReactNode } from "react";
|
|
8
8
|
import { useState } from "react";
|
|
9
9
|
import { describe, expect, it } from "vitest";
|
|
@@ -440,6 +440,83 @@ describe("StatewireThread", () => {
|
|
|
440
440
|
await waitFor(() => expect(metas.at(-1)!.pendingCommands).toEqual([]));
|
|
441
441
|
});
|
|
442
442
|
|
|
443
|
+
describe("onStateChange / onConnectionChange", () => {
|
|
444
|
+
it("fires onStateChange with the attach snapshot, then once per state change", async () => {
|
|
445
|
+
const server = createServer({
|
|
446
|
+
messages: [{ id: "m1", role: "user", text: "hi" }],
|
|
447
|
+
});
|
|
448
|
+
const seen: ServerState[] = [];
|
|
449
|
+
mountThread({
|
|
450
|
+
transport: server.transport,
|
|
451
|
+
converter,
|
|
452
|
+
onStateChange: (state) => seen.push(state),
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
await waitFor(() => expect(seen).toHaveLength(1));
|
|
456
|
+
expect(seen[0]!.messages.map((m) => m.text)).toEqual(["hi"]);
|
|
457
|
+
|
|
458
|
+
server.cell.state!.messages.push({
|
|
459
|
+
id: "m2",
|
|
460
|
+
role: "assistant",
|
|
461
|
+
text: "hello",
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
await waitFor(() => expect(seen).toHaveLength(2));
|
|
465
|
+
expect(seen[1]!.messages.map((m) => m.text)).toEqual(["hi", "hello"]);
|
|
466
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
467
|
+
expect(seen).toHaveLength(2);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("fires onConnectionChange with the status at first flush, then once per transition", async () => {
|
|
471
|
+
const statuses: string[] = [];
|
|
472
|
+
const mock = createMockTransport<ServerState>(INITIAL_STATE);
|
|
473
|
+
mountThread({
|
|
474
|
+
transport: mock.transport,
|
|
475
|
+
converter,
|
|
476
|
+
onConnectionChange: (connection) => statuses.push(connection.status),
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
await waitFor(() => expect(statuses).toEqual(["live"]));
|
|
480
|
+
await mock.finish("superseded");
|
|
481
|
+
await waitFor(() => expect(statuses).toEqual(["live", "gone"]));
|
|
482
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
483
|
+
expect(statuses).toEqual(["live", "gone"]);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
it("stays quiet on unrelated re-renders and callback identity changes", async () => {
|
|
487
|
+
const server = createServer();
|
|
488
|
+
const seen: ServerState[] = [];
|
|
489
|
+
const statuses: string[] = [];
|
|
490
|
+
const knobs: { rerender?: () => void } = {};
|
|
491
|
+
const App: FC = () => {
|
|
492
|
+
const [, setTick] = useState(0);
|
|
493
|
+
knobs.rerender = () => setTick((t) => t + 1);
|
|
494
|
+
const aui = useAui({
|
|
495
|
+
thread: StatewireThread({
|
|
496
|
+
initialState: INITIAL_STATE,
|
|
497
|
+
transport: server.transport,
|
|
498
|
+
converter,
|
|
499
|
+
onStateChange: (state) => seen.push(state),
|
|
500
|
+
onConnectionChange: (connection) =>
|
|
501
|
+
statuses.push(connection.status),
|
|
502
|
+
}),
|
|
503
|
+
});
|
|
504
|
+
return <AuiProvider value={aui}>{null}</AuiProvider>;
|
|
505
|
+
};
|
|
506
|
+
render(<App />);
|
|
507
|
+
|
|
508
|
+
await waitFor(() => expect(seen).toHaveLength(1));
|
|
509
|
+
expect(statuses).toEqual(["live"]);
|
|
510
|
+
|
|
511
|
+
act(() => knobs.rerender!());
|
|
512
|
+
act(() => knobs.rerender!());
|
|
513
|
+
|
|
514
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
515
|
+
expect(seen).toHaveLength(1);
|
|
516
|
+
expect(statuses).toEqual(["live"]);
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
|
|
443
520
|
describe("contextSync: command-send", () => {
|
|
444
521
|
const contextValueOf = (command: { context?: unknown }) => {
|
|
445
522
|
const ops = command.context as [
|
|
@@ -763,6 +840,45 @@ describe("StatewireThread", () => {
|
|
|
763
840
|
expect(command.message.id).not.toBe("h1");
|
|
764
841
|
});
|
|
765
842
|
|
|
843
|
+
it("routes sends carrying a sourceId past the queue lanes into run/edit", async () => {
|
|
844
|
+
const server = createServer({
|
|
845
|
+
messages: [
|
|
846
|
+
{ id: "h1", role: "user", text: "bad question" },
|
|
847
|
+
{ id: "a1", role: "assistant", text: "bad answer" },
|
|
848
|
+
],
|
|
849
|
+
});
|
|
850
|
+
const { aui } = mountThread({
|
|
851
|
+
transport: server.transport,
|
|
852
|
+
converter: queueConverter,
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
await waitFor(() =>
|
|
856
|
+
expect(threadTexts(aui)).toEqual(["bad question", "bad answer"]),
|
|
857
|
+
);
|
|
858
|
+
|
|
859
|
+
aui()
|
|
860
|
+
.thread()
|
|
861
|
+
.append({
|
|
862
|
+
role: "user",
|
|
863
|
+
content: [{ type: "text", text: "better question" }],
|
|
864
|
+
sourceId: "h1",
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
await waitFor(() =>
|
|
868
|
+
expect(server.received).toEqual([
|
|
869
|
+
{
|
|
870
|
+
type: "run/edit",
|
|
871
|
+
sourceId: "h1",
|
|
872
|
+
message: {
|
|
873
|
+
id: expect.any(String),
|
|
874
|
+
role: "user",
|
|
875
|
+
parts: [{ type: "text", text: "better question" }],
|
|
876
|
+
},
|
|
877
|
+
},
|
|
878
|
+
]),
|
|
879
|
+
);
|
|
880
|
+
});
|
|
881
|
+
|
|
766
882
|
it("dispatches run/continue from resumeRun under the continue capability", async () => {
|
|
767
883
|
const server = createServer();
|
|
768
884
|
const { aui } = mountThread({
|
|
@@ -1008,6 +1124,30 @@ describe("StatewireThread", () => {
|
|
|
1008
1124
|
);
|
|
1009
1125
|
});
|
|
1010
1126
|
|
|
1127
|
+
it("stamps the projected runId onto run/stop", async () => {
|
|
1128
|
+
const server = createServer();
|
|
1129
|
+
const { aui } = mountThread({
|
|
1130
|
+
transport: server.transport,
|
|
1131
|
+
converter: (state, meta) => ({
|
|
1132
|
+
...converter(state, meta),
|
|
1133
|
+
runId: state.running ? "run-1" : null,
|
|
1134
|
+
}),
|
|
1135
|
+
stopPayload: () => ({ reason: "user-stop" }),
|
|
1136
|
+
});
|
|
1137
|
+
|
|
1138
|
+
aui().thread().append("hello");
|
|
1139
|
+
await waitFor(() => expect(aui().thread().getState().isRunning).toBe(true));
|
|
1140
|
+
|
|
1141
|
+
aui().thread().cancelRun();
|
|
1142
|
+
|
|
1143
|
+
await waitFor(() =>
|
|
1144
|
+
expect(server.received).toContainEqual({
|
|
1145
|
+
type: "run/stop",
|
|
1146
|
+
payload: { reason: "user-stop", runId: "run-1" },
|
|
1147
|
+
}),
|
|
1148
|
+
);
|
|
1149
|
+
});
|
|
1150
|
+
|
|
1011
1151
|
it("dispatches run/reload for the assistant message under the reload capability", async () => {
|
|
1012
1152
|
const server = createServer({
|
|
1013
1153
|
messages: [
|
|
@@ -1068,6 +1208,42 @@ describe("StatewireThread", () => {
|
|
|
1068
1208
|
expect(aui().thread().getState().capabilities.reload).toBe(false);
|
|
1069
1209
|
});
|
|
1070
1210
|
|
|
1211
|
+
it("routes appends carrying a sourceId through run/edit without a queue", async () => {
|
|
1212
|
+
const server = createServer({
|
|
1213
|
+
messages: [
|
|
1214
|
+
{ id: "h1", role: "user", text: "bad question" },
|
|
1215
|
+
{ id: "a1", role: "assistant", text: "bad answer" },
|
|
1216
|
+
],
|
|
1217
|
+
});
|
|
1218
|
+
const { aui } = mountThread({ transport: server.transport, converter });
|
|
1219
|
+
|
|
1220
|
+
await waitFor(() =>
|
|
1221
|
+
expect(threadTexts(aui)).toEqual(["bad question", "bad answer"]),
|
|
1222
|
+
);
|
|
1223
|
+
|
|
1224
|
+
aui()
|
|
1225
|
+
.thread()
|
|
1226
|
+
.append({
|
|
1227
|
+
role: "user",
|
|
1228
|
+
content: [{ type: "text", text: "better question" }],
|
|
1229
|
+
sourceId: "h1",
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1232
|
+
await waitFor(() =>
|
|
1233
|
+
expect(server.received).toEqual([
|
|
1234
|
+
{
|
|
1235
|
+
type: "run/edit",
|
|
1236
|
+
sourceId: "h1",
|
|
1237
|
+
message: {
|
|
1238
|
+
id: expect.any(String),
|
|
1239
|
+
role: "user",
|
|
1240
|
+
parts: [{ type: "text", text: "better question" }],
|
|
1241
|
+
},
|
|
1242
|
+
},
|
|
1243
|
+
]),
|
|
1244
|
+
);
|
|
1245
|
+
});
|
|
1246
|
+
|
|
1071
1247
|
it("enables the edit capability from options", async () => {
|
|
1072
1248
|
const server = createServer();
|
|
1073
1249
|
const { aui } = mountThread({
|