@assistant-ui/react-statewire 0.0.5 → 0.1.1
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 +1 -0
- package/dist/AssistantTransportThread.d.ts.map +1 -1
- package/dist/AssistantTransportThread.js +48 -7
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +25 -1
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +51 -15
- package/dist/StatewireThread.js.map +1 -1
- package/dist/host.d.ts +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/types.d.ts +26 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/AssistantTransportThread.ts +75 -7
- package/src/StatewireThread.test.tsx +279 -12
- package/src/StatewireThread.ts +91 -12
- package/src/index.ts +4 -0
- package/src/transport.test.tsx +145 -3
- package/src/types.ts +32 -0
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useMemo,
|
|
5
|
+
useRef,
|
|
6
|
+
useState,
|
|
7
|
+
useSyncExternalStore,
|
|
8
|
+
} from "react";
|
|
2
9
|
import { resource, useResource } from "@assistant-ui/tap";
|
|
3
10
|
import type { ResourceElement } from "@assistant-ui/tap";
|
|
4
11
|
import { ExternalThread } from "@assistant-ui/react";
|
|
@@ -8,9 +15,12 @@ import {
|
|
|
8
15
|
useAssistantClientRef,
|
|
9
16
|
} from "@assistant-ui/store";
|
|
10
17
|
import type { ClientOutput } from "@assistant-ui/store";
|
|
18
|
+
import { createMessageQueue } from "@assistant-ui/core";
|
|
11
19
|
import type {
|
|
12
20
|
AddToolResultOptions,
|
|
13
21
|
AppendMessage,
|
|
22
|
+
MessageQueueController,
|
|
23
|
+
MessageQueueDriver,
|
|
14
24
|
ResumeToolCallOptions,
|
|
15
25
|
ThreadMessage,
|
|
16
26
|
ToolExecutionStatus,
|
|
@@ -35,6 +45,8 @@ import type {
|
|
|
35
45
|
} from "./types";
|
|
36
46
|
|
|
37
47
|
const EMPTY_TOOL_STATUSES: Record<string, ToolExecutionStatus> = {};
|
|
48
|
+
const EMPTY_QUEUE_ITEMS: MessageQueueController["adapter"]["items"] = [];
|
|
49
|
+
const subscribeNoop = () => () => {};
|
|
38
50
|
|
|
39
51
|
export const symbolAssistantTransportExtras = Symbol(
|
|
40
52
|
"assistant-transport-extras",
|
|
@@ -43,6 +55,7 @@ export const symbolAssistantTransportExtras = Symbol(
|
|
|
43
55
|
export type AssistantTransportExtras = {
|
|
44
56
|
[symbolAssistantTransportExtras]: true;
|
|
45
57
|
sendCommand: (command: AssistantTransportCommand) => void;
|
|
58
|
+
reconnect: () => void;
|
|
46
59
|
state: unknown;
|
|
47
60
|
};
|
|
48
61
|
|
|
@@ -127,7 +140,7 @@ const useAssistantTransportThread = (
|
|
|
127
140
|
const [queueCell] = useState(
|
|
128
141
|
(): HostQueueCell => ({ current: { queued: [], inTransit: [] } }),
|
|
129
142
|
);
|
|
130
|
-
const { state, commands } = useStatewire<
|
|
143
|
+
const { state, commands, connection } = useStatewire<
|
|
131
144
|
AssistantTransportHostState,
|
|
132
145
|
AssistantTransportHostCommands
|
|
133
146
|
>({
|
|
@@ -202,9 +215,10 @@ const useAssistantTransportThread = (
|
|
|
202
215
|
(): AssistantTransportExtras => ({
|
|
203
216
|
[symbolAssistantTransportExtras]: true,
|
|
204
217
|
sendCommand: dispatch,
|
|
218
|
+
reconnect: connection.reconnect,
|
|
205
219
|
state: agentState,
|
|
206
220
|
}),
|
|
207
|
-
[dispatch, agentState],
|
|
221
|
+
[dispatch, connection.reconnect, agentState],
|
|
208
222
|
);
|
|
209
223
|
|
|
210
224
|
const handleAppend = (message: AppendMessage) => {
|
|
@@ -224,6 +238,60 @@ const useAssistantTransportThread = (
|
|
|
224
238
|
}
|
|
225
239
|
};
|
|
226
240
|
|
|
241
|
+
const handleCancel = () => {
|
|
242
|
+
void toolInvocations.abort();
|
|
243
|
+
void commands.cancel().catch(() => {});
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const driverRef = useRef<{
|
|
247
|
+
run: MessageQueueDriver["run"];
|
|
248
|
+
cancel: () => void;
|
|
249
|
+
}>(undefined as never);
|
|
250
|
+
driverRef.current = {
|
|
251
|
+
// the queued message was stamped with the head at enqueue time; restamp so
|
|
252
|
+
// it appends to the thread the finished run left behind
|
|
253
|
+
run: (message) =>
|
|
254
|
+
handleAppend({
|
|
255
|
+
...message,
|
|
256
|
+
parentId: convertedRef.current.messages.at(-1)?.id ?? null,
|
|
257
|
+
}),
|
|
258
|
+
cancel: handleCancel,
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const enableQueue = options.capabilities?.queue === true;
|
|
262
|
+
const queueRef = useRef<MessageQueueController | null>(null);
|
|
263
|
+
if (enableQueue && !queueRef.current) {
|
|
264
|
+
queueRef.current = createMessageQueue({
|
|
265
|
+
run: (message, opts) => driverRef.current.run(message, opts),
|
|
266
|
+
cancel: () => driverRef.current.cancel(),
|
|
267
|
+
});
|
|
268
|
+
} else if (!enableQueue && queueRef.current) {
|
|
269
|
+
queueRef.current.adapter.clear("cancel-run");
|
|
270
|
+
queueRef.current = null;
|
|
271
|
+
}
|
|
272
|
+
const messageQueue = queueRef.current;
|
|
273
|
+
|
|
274
|
+
// fresh view per snapshot: the controller mutates adapter.items in place,
|
|
275
|
+
// which a stable prop identity would hide from the thread resource
|
|
276
|
+
const queueItems = useSyncExternalStore(
|
|
277
|
+
messageQueue?.subscribe ?? subscribeNoop,
|
|
278
|
+
() => messageQueue?.adapter.items ?? EMPTY_QUEUE_ITEMS,
|
|
279
|
+
() => EMPTY_QUEUE_ITEMS,
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
// settled edge: the queue only advances once the host run loop has fully
|
|
283
|
+
// released (isSending), the converted state agrees, and no client tool from
|
|
284
|
+
// the finished run is still executing
|
|
285
|
+
const busy =
|
|
286
|
+
isSending || converted.isRunning || Object.keys(toolStatuses).length > 0;
|
|
287
|
+
const wasBusyRef = useRef(false);
|
|
288
|
+
useEffect(() => {
|
|
289
|
+
if (wasBusyRef.current === busy) return;
|
|
290
|
+
wasBusyRef.current = busy;
|
|
291
|
+
if (busy) messageQueue?.notifyBusy();
|
|
292
|
+
else messageQueue?.notifyIdle();
|
|
293
|
+
});
|
|
294
|
+
|
|
227
295
|
return useResource(
|
|
228
296
|
ExternalThread({
|
|
229
297
|
messages: converted.messages as readonly ExternalThreadMessage[],
|
|
@@ -233,16 +301,16 @@ const useAssistantTransportThread = (
|
|
|
233
301
|
extras,
|
|
234
302
|
attachmentAdapter: options.adapters?.attachments,
|
|
235
303
|
onNew: handleAppend,
|
|
304
|
+
...(messageQueue && {
|
|
305
|
+
queue: { ...messageQueue.adapter, items: queueItems },
|
|
306
|
+
}),
|
|
236
307
|
...(options.capabilities?.edit && { onEdit: handleAppend }),
|
|
237
308
|
onReload: (parentId: string | null) => {
|
|
238
309
|
void toolInvocations.abort().then(() => {
|
|
239
310
|
void commands.reload(parentId).catch(() => {});
|
|
240
311
|
});
|
|
241
312
|
},
|
|
242
|
-
onCancel:
|
|
243
|
-
void toolInvocations.abort();
|
|
244
|
-
void commands.cancel().catch(() => {});
|
|
245
|
-
},
|
|
313
|
+
onCancel: handleCancel,
|
|
246
314
|
onResume: () => {
|
|
247
315
|
if (!options.resumeApi)
|
|
248
316
|
throw new Error("Must pass resumeApi to options to resume runs");
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// statewire host stands in for the server.
|
|
5
5
|
|
|
6
6
|
import { render, waitFor } from "@testing-library/react";
|
|
7
|
-
import type { FC } from "react";
|
|
7
|
+
import type { FC, ReactNode } from "react";
|
|
8
8
|
import { useState } from "react";
|
|
9
9
|
import { describe, expect, it } from "vitest";
|
|
10
10
|
import { AuiProvider, useAui } from "@assistant-ui/store";
|
|
@@ -18,7 +18,16 @@ import {
|
|
|
18
18
|
import type { StatewireHost } from "statewire/host";
|
|
19
19
|
import { createMockTransport } from "statewire/testing";
|
|
20
20
|
import { StatewireThread } from "./StatewireThread";
|
|
21
|
-
import
|
|
21
|
+
import { asAssistantTransportExtras } from "./AssistantTransportThread";
|
|
22
|
+
import { useAssistantTransportState } from "./hooks";
|
|
23
|
+
import type {
|
|
24
|
+
AddMessageCommand,
|
|
25
|
+
AssistantTransportCommand,
|
|
26
|
+
ClearQueueCommand,
|
|
27
|
+
EnqueueMessageCommand,
|
|
28
|
+
RemoveQueueItemCommand,
|
|
29
|
+
SteerQueueItemCommand,
|
|
30
|
+
} from "./types";
|
|
22
31
|
import {
|
|
23
32
|
sendCommand,
|
|
24
33
|
threadTexts,
|
|
@@ -28,9 +37,10 @@ import {
|
|
|
28
37
|
type ServerState = {
|
|
29
38
|
messages: { id: string; role: "user" | "assistant"; text: string }[];
|
|
30
39
|
running: boolean;
|
|
40
|
+
queue: { id: string; prompt: string }[];
|
|
31
41
|
};
|
|
32
42
|
|
|
33
|
-
const textOf = (command: AddMessageCommand) =>
|
|
43
|
+
const textOf = (command: Pick<AddMessageCommand, "message">) =>
|
|
34
44
|
command.message.parts
|
|
35
45
|
.flatMap((p) => (p.type === "text" ? [p.text] : []))
|
|
36
46
|
.join("");
|
|
@@ -56,7 +66,10 @@ const converter: StatewireThread.StateConverter<ServerState> = (
|
|
|
56
66
|
isRunning: state.running,
|
|
57
67
|
});
|
|
58
68
|
|
|
59
|
-
const createServer = (init?: {
|
|
69
|
+
const createServer = (init?: {
|
|
70
|
+
messages?: ServerState["messages"];
|
|
71
|
+
queue?: ServerState["queue"];
|
|
72
|
+
}) => {
|
|
60
73
|
const received: unknown[] = [];
|
|
61
74
|
const cell: {
|
|
62
75
|
state?: ServerState;
|
|
@@ -67,6 +80,7 @@ const createServer = (init?: { messages?: ServerState["messages"] }) => {
|
|
|
67
80
|
const [state] = useStatewireState<ServerState>(() => ({
|
|
68
81
|
messages: init?.messages ?? [],
|
|
69
82
|
running: false,
|
|
83
|
+
queue: init?.queue ?? [],
|
|
70
84
|
}));
|
|
71
85
|
cell.state = state;
|
|
72
86
|
const commands = useStatewireCommands({
|
|
@@ -85,10 +99,35 @@ const createServer = (init?: { messages?: ServerState["messages"] }) => {
|
|
|
85
99
|
state.running = true;
|
|
86
100
|
return undefined;
|
|
87
101
|
},
|
|
88
|
-
cancel: () => {
|
|
89
|
-
received.push(
|
|
102
|
+
cancel: (payload?: unknown) => {
|
|
103
|
+
received.push(
|
|
104
|
+
payload === undefined
|
|
105
|
+
? { type: "cancel" }
|
|
106
|
+
: { type: "cancel", payload },
|
|
107
|
+
);
|
|
90
108
|
state.running = false;
|
|
91
109
|
},
|
|
110
|
+
"enqueue-message": (command: EnqueueMessageCommand) => {
|
|
111
|
+
received.push(command);
|
|
112
|
+
state.queue.push({
|
|
113
|
+
id: `q${state.queue.length}`,
|
|
114
|
+
prompt: textOf(command),
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
"steer-queue-item": (command: SteerQueueItemCommand) => {
|
|
118
|
+
received.push(command);
|
|
119
|
+
},
|
|
120
|
+
"remove-queue-item": (command: RemoveQueueItemCommand) => {
|
|
121
|
+
received.push(command);
|
|
122
|
+
const index = state.queue.findIndex(
|
|
123
|
+
(item) => item.id === command.queueItemId,
|
|
124
|
+
);
|
|
125
|
+
if (index !== -1) state.queue.splice(index, 1);
|
|
126
|
+
},
|
|
127
|
+
"clear-queue": (command: ClearQueueCommand) => {
|
|
128
|
+
received.push(command);
|
|
129
|
+
state.queue.splice(0);
|
|
130
|
+
},
|
|
92
131
|
"reject-me": () => {
|
|
93
132
|
throw new StatewireReject("nope");
|
|
94
133
|
},
|
|
@@ -98,17 +137,26 @@ const createServer = (init?: { messages?: ServerState["messages"] }) => {
|
|
|
98
137
|
return { transport, received, cell };
|
|
99
138
|
};
|
|
100
139
|
|
|
101
|
-
const
|
|
140
|
+
const INITIAL_STATE: ServerState = { messages: [], running: false, queue: [] };
|
|
141
|
+
|
|
142
|
+
const mountThread = (
|
|
143
|
+
options: Omit<StatewireThread.Options<ServerState>, "initialState"> &
|
|
144
|
+
Partial<Pick<StatewireThread.Options<ServerState>, "initialState">>,
|
|
145
|
+
children?: ReactNode,
|
|
146
|
+
) => {
|
|
102
147
|
const captured: { aui?: ReturnType<typeof useAui> } = {};
|
|
103
148
|
const Capture: FC = () => {
|
|
104
149
|
captured.aui = useAui();
|
|
105
150
|
return null;
|
|
106
151
|
};
|
|
107
152
|
const App: FC = () => {
|
|
108
|
-
const aui = useAui({
|
|
153
|
+
const aui = useAui({
|
|
154
|
+
thread: StatewireThread({ initialState: INITIAL_STATE, ...options }),
|
|
155
|
+
});
|
|
109
156
|
return (
|
|
110
157
|
<AuiProvider value={aui}>
|
|
111
158
|
<Capture />
|
|
159
|
+
{children}
|
|
112
160
|
</AuiProvider>
|
|
113
161
|
);
|
|
114
162
|
};
|
|
@@ -133,7 +181,7 @@ describe("StatewireThread", () => {
|
|
|
133
181
|
expect(aui().thread().getState().isRunning).toBe(false);
|
|
134
182
|
});
|
|
135
183
|
|
|
136
|
-
it("
|
|
184
|
+
it("serves initialState until the first snapshot replaces it", async () => {
|
|
137
185
|
const cell: { attach?: () => void } = {};
|
|
138
186
|
const transport = resource(() => {
|
|
139
187
|
const [attached, setAttached] = useState(false);
|
|
@@ -141,20 +189,36 @@ describe("StatewireThread", () => {
|
|
|
141
189
|
const [state] = useStatewireState<ServerState>(() => ({
|
|
142
190
|
messages: [{ id: "m1", role: "assistant", text: "welcome" }],
|
|
143
191
|
running: false,
|
|
192
|
+
queue: [],
|
|
144
193
|
}));
|
|
145
194
|
const commands = useStatewireCommands({});
|
|
146
195
|
return { state: attached ? state : undefined, commands };
|
|
147
196
|
})();
|
|
148
197
|
|
|
149
|
-
const
|
|
198
|
+
const initialState: ServerState = {
|
|
199
|
+
messages: [{ id: "i1", role: "assistant", text: "local" }],
|
|
200
|
+
running: false,
|
|
201
|
+
queue: [],
|
|
202
|
+
};
|
|
203
|
+
const observed: unknown[] = [];
|
|
204
|
+
const Probe: FC = () => {
|
|
205
|
+
observed.push(useAssistantTransportState());
|
|
206
|
+
return null;
|
|
207
|
+
};
|
|
208
|
+
const { aui } = mountThread(
|
|
209
|
+
{ transport, converter, initialState },
|
|
210
|
+
<Probe />,
|
|
211
|
+
);
|
|
150
212
|
|
|
151
|
-
|
|
152
|
-
expect(threadTexts(aui)).toEqual([]);
|
|
213
|
+
expect(observed[0]).toBe(initialState);
|
|
214
|
+
expect(threadTexts(aui)).toEqual(["local"]);
|
|
215
|
+
expect(aui().thread().getState().isLoading).toBe(true);
|
|
153
216
|
|
|
154
217
|
cell.attach!();
|
|
155
218
|
|
|
156
219
|
await waitFor(() => expect(threadTexts(aui)).toEqual(["welcome"]));
|
|
157
220
|
expect(aui().thread().getState().isLoading).toBe(false);
|
|
221
|
+
expect((observed.at(-1) as ServerState).messages[0]!.text).toBe("welcome");
|
|
158
222
|
});
|
|
159
223
|
|
|
160
224
|
it("round-trips an appended message through the add-message command", async () => {
|
|
@@ -272,6 +336,7 @@ describe("StatewireThread", () => {
|
|
|
272
336
|
const mock = createMockTransport<ServerState>({
|
|
273
337
|
messages: [],
|
|
274
338
|
running: false,
|
|
339
|
+
queue: [],
|
|
275
340
|
});
|
|
276
341
|
const { aui } = mountThread({
|
|
277
342
|
transport: mock.transport,
|
|
@@ -295,6 +360,7 @@ describe("StatewireThread", () => {
|
|
|
295
360
|
const mock = createMockTransport<ServerState>({
|
|
296
361
|
messages: [],
|
|
297
362
|
running: false,
|
|
363
|
+
queue: [],
|
|
298
364
|
});
|
|
299
365
|
const { aui } = mountThread({
|
|
300
366
|
transport: mock.transport,
|
|
@@ -319,6 +385,7 @@ describe("StatewireThread", () => {
|
|
|
319
385
|
const mock = createMockTransport<ServerState>({
|
|
320
386
|
messages: [],
|
|
321
387
|
running: false,
|
|
388
|
+
queue: [],
|
|
322
389
|
});
|
|
323
390
|
const { aui } = mountThread({ transport: mock.transport, converter });
|
|
324
391
|
await waitFor(() =>
|
|
@@ -328,6 +395,137 @@ describe("StatewireThread", () => {
|
|
|
328
395
|
});
|
|
329
396
|
});
|
|
330
397
|
|
|
398
|
+
describe("message queue", () => {
|
|
399
|
+
const queueConverter: StatewireThread.StateConverter<ServerState> = (
|
|
400
|
+
state,
|
|
401
|
+
meta,
|
|
402
|
+
) => ({
|
|
403
|
+
...converter(state, meta),
|
|
404
|
+
queue: state.queue,
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("routes composer sends through enqueue-message while a run is active and mirrors the server queue", async () => {
|
|
408
|
+
const server = createServer();
|
|
409
|
+
const { aui } = mountThread({
|
|
410
|
+
transport: server.transport,
|
|
411
|
+
converter: queueConverter,
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
await waitFor(() =>
|
|
415
|
+
expect(aui().thread().getState().capabilities.queue).toBe(true),
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
aui().thread().append("hello");
|
|
419
|
+
await waitFor(() =>
|
|
420
|
+
expect(server.received).toMatchObject([{ type: "add-message" }]),
|
|
421
|
+
);
|
|
422
|
+
await waitFor(() =>
|
|
423
|
+
expect(aui().thread().getState().isRunning).toBe(true),
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
aui().thread().append("queued follow-up");
|
|
427
|
+
await waitFor(() =>
|
|
428
|
+
expect(server.received.at(-1)).toMatchObject({
|
|
429
|
+
type: "enqueue-message",
|
|
430
|
+
steer: false,
|
|
431
|
+
message: {
|
|
432
|
+
role: "user",
|
|
433
|
+
parts: [{ type: "text", text: "queued follow-up" }],
|
|
434
|
+
},
|
|
435
|
+
}),
|
|
436
|
+
);
|
|
437
|
+
await waitFor(() =>
|
|
438
|
+
expect(aui().thread().composer().getState().queue).toMatchObject([
|
|
439
|
+
{ id: "q0", prompt: "queued follow-up" },
|
|
440
|
+
]),
|
|
441
|
+
);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it("sends item controls as queue commands", async () => {
|
|
445
|
+
const server = createServer({
|
|
446
|
+
queue: [{ id: "q1", prompt: "queued" }],
|
|
447
|
+
});
|
|
448
|
+
const { aui } = mountThread({
|
|
449
|
+
transport: server.transport,
|
|
450
|
+
converter: queueConverter,
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
await waitFor(() =>
|
|
454
|
+
expect(aui().thread().composer().getState().queue).toHaveLength(1),
|
|
455
|
+
);
|
|
456
|
+
|
|
457
|
+
aui().thread().composer().queueItem({ index: 0 }).steer();
|
|
458
|
+
await waitFor(() =>
|
|
459
|
+
expect(server.received).toContainEqual({
|
|
460
|
+
type: "steer-queue-item",
|
|
461
|
+
queueItemId: "q1",
|
|
462
|
+
}),
|
|
463
|
+
);
|
|
464
|
+
|
|
465
|
+
aui().thread().composer().queueItem({ index: 0 }).remove();
|
|
466
|
+
await waitFor(() =>
|
|
467
|
+
expect(server.received).toContainEqual({
|
|
468
|
+
type: "remove-queue-item",
|
|
469
|
+
queueItemId: "q1",
|
|
470
|
+
}),
|
|
471
|
+
);
|
|
472
|
+
await waitFor(() =>
|
|
473
|
+
expect(aui().thread().composer().getState().queue).toHaveLength(0),
|
|
474
|
+
);
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it("sends clear-queue on cancel", async () => {
|
|
478
|
+
const server = createServer({
|
|
479
|
+
queue: [{ id: "q1", prompt: "queued" }],
|
|
480
|
+
});
|
|
481
|
+
const { aui } = mountThread({
|
|
482
|
+
transport: server.transport,
|
|
483
|
+
converter: queueConverter,
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
aui().thread().append("hello");
|
|
487
|
+
await waitFor(() =>
|
|
488
|
+
expect(aui().thread().getState().isRunning).toBe(true),
|
|
489
|
+
);
|
|
490
|
+
|
|
491
|
+
aui().thread().cancelRun();
|
|
492
|
+
|
|
493
|
+
await waitFor(() =>
|
|
494
|
+
expect(server.received).toContainEqual({
|
|
495
|
+
type: "clear-queue",
|
|
496
|
+
reason: "cancel-run",
|
|
497
|
+
}),
|
|
498
|
+
);
|
|
499
|
+
await waitFor(() =>
|
|
500
|
+
expect(server.received).toContainEqual({ type: "cancel" }),
|
|
501
|
+
);
|
|
502
|
+
await waitFor(() =>
|
|
503
|
+
expect(aui().thread().composer().getState().queue).toHaveLength(0),
|
|
504
|
+
);
|
|
505
|
+
});
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
it("sends the cancelPayload with the cancel command", async () => {
|
|
509
|
+
const server = createServer();
|
|
510
|
+
const { aui } = mountThread({
|
|
511
|
+
transport: server.transport,
|
|
512
|
+
converter,
|
|
513
|
+
cancelPayload: () => ({ reason: "user-stop", surface: "test" }),
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
aui().thread().append("hello");
|
|
517
|
+
await waitFor(() => expect(aui().thread().getState().isRunning).toBe(true));
|
|
518
|
+
|
|
519
|
+
aui().thread().cancelRun();
|
|
520
|
+
|
|
521
|
+
await waitFor(() =>
|
|
522
|
+
expect(server.received).toContainEqual({
|
|
523
|
+
type: "cancel",
|
|
524
|
+
payload: { reason: "user-stop", surface: "test" },
|
|
525
|
+
}),
|
|
526
|
+
);
|
|
527
|
+
});
|
|
528
|
+
|
|
331
529
|
it("enables the edit capability from options", async () => {
|
|
332
530
|
const server = createServer();
|
|
333
531
|
const { aui } = mountThread({
|
|
@@ -340,4 +538,73 @@ describe("StatewireThread", () => {
|
|
|
340
538
|
expect(aui().thread().getState().capabilities.edit).toBe(true),
|
|
341
539
|
);
|
|
342
540
|
});
|
|
541
|
+
|
|
542
|
+
describe("connection", () => {
|
|
543
|
+
const trackStatuses = (seen: string[]) => {
|
|
544
|
+
const trackingConverter: StatewireThread.StateConverter<ServerState> = (
|
|
545
|
+
state,
|
|
546
|
+
meta,
|
|
547
|
+
) => {
|
|
548
|
+
if (seen.at(-1) !== meta.connection.status)
|
|
549
|
+
seen.push(meta.connection.status);
|
|
550
|
+
return converter(state, meta);
|
|
551
|
+
};
|
|
552
|
+
return trackingConverter;
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
it("projects connection transitions into the converter", async () => {
|
|
556
|
+
const seen: string[] = [];
|
|
557
|
+
const mock = createMockTransport<ServerState>({
|
|
558
|
+
messages: [],
|
|
559
|
+
running: false,
|
|
560
|
+
queue: [],
|
|
561
|
+
});
|
|
562
|
+
const { aui } = mountThread({
|
|
563
|
+
transport: mock.transport,
|
|
564
|
+
converter: trackStatuses(seen),
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
await waitFor(() =>
|
|
568
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
569
|
+
);
|
|
570
|
+
expect(seen).toEqual(["connecting", "live"]);
|
|
571
|
+
|
|
572
|
+
await mock.finish("superseded", "taken over");
|
|
573
|
+
await waitFor(() => expect(seen.at(-1)).toBe("gone"));
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
it("reports live without re-converting on a local transport", async () => {
|
|
577
|
+
const seen: string[] = [];
|
|
578
|
+
const server = createServer();
|
|
579
|
+
mountThread({
|
|
580
|
+
transport: server.transport,
|
|
581
|
+
converter: trackStatuses(seen),
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
await waitFor(() => expect(seen).toEqual(["live"]));
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
it("exposes reconnect through extras", async () => {
|
|
588
|
+
const seen: string[] = [];
|
|
589
|
+
const mock = createMockTransport<ServerState>({
|
|
590
|
+
messages: [],
|
|
591
|
+
running: false,
|
|
592
|
+
queue: [],
|
|
593
|
+
});
|
|
594
|
+
const { aui } = mountThread({
|
|
595
|
+
transport: mock.transport,
|
|
596
|
+
converter: trackStatuses(seen),
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
await waitFor(() =>
|
|
600
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
601
|
+
);
|
|
602
|
+
await mock.finish("superseded");
|
|
603
|
+
await waitFor(() => expect(seen.at(-1)).toBe("gone"));
|
|
604
|
+
|
|
605
|
+
asAssistantTransportExtras(aui().thread().getState().extras).reconnect();
|
|
606
|
+
|
|
607
|
+
await waitFor(() => expect(seen.at(-1)).toBe("reconnecting"));
|
|
608
|
+
});
|
|
609
|
+
});
|
|
343
610
|
});
|