@assistant-ui/react-statewire 0.1.1 → 0.3.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 +4 -4
- package/dist/AssistantTransportThread.d.ts.map +1 -1
- package/dist/AssistantTransportThread.js +65 -45
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +122 -10
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +90 -38
- package/dist/StatewireThread.js.map +1 -1
- package/dist/host.d.ts +0 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +20 -12
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +21 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/inputRequests.d.ts +14 -0
- package/dist/inputRequests.d.ts.map +1 -0
- package/dist/inputRequests.js +35 -0
- package/dist/inputRequests.js.map +1 -0
- package/dist/runCommands.d.ts +22 -0
- package/dist/runCommands.d.ts.map +1 -0
- package/dist/runCommands.js +105 -0
- package/dist/runCommands.js.map +1 -0
- package/dist/types.d.ts +114 -17
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/AssistantTransportThread.ts +120 -50
- package/src/StatewireThread.test.tsx +587 -61
- package/src/StatewireThread.ts +310 -47
- package/src/__tests__/testHelpers.tsx +1 -1
- package/src/athena.e2e.test.tsx +34 -25
- package/src/host.ts +23 -11
- package/src/index.ts +20 -4
- package/src/inputRequests.ts +57 -0
- package/src/legacy-diff.test.tsx +32 -8
- package/src/runCommands.ts +146 -0
- package/src/transport.test.tsx +581 -57
- package/src/types.ts +133 -19
|
@@ -10,34 +10,32 @@ import { describe, expect, it } from "vitest";
|
|
|
10
10
|
import { AuiProvider, useAui } from "@assistant-ui/store";
|
|
11
11
|
import { resource } from "@assistant-ui/tap";
|
|
12
12
|
import {
|
|
13
|
-
CommandExecution,
|
|
14
13
|
StatewireReject,
|
|
15
14
|
useStatewireCommands,
|
|
16
15
|
useStatewireState,
|
|
17
16
|
} from "statewire/host";
|
|
18
|
-
import type { StatewireHost } from "statewire/host";
|
|
19
17
|
import { createMockTransport } from "statewire/testing";
|
|
20
18
|
import { StatewireThread } from "./StatewireThread";
|
|
21
19
|
import { asAssistantTransportExtras } from "./AssistantTransportThread";
|
|
22
20
|
import { useAssistantTransportState } from "./hooks";
|
|
23
|
-
import type {
|
|
24
|
-
AddMessageCommand,
|
|
25
|
-
AssistantTransportCommand,
|
|
26
|
-
ClearQueueCommand,
|
|
27
|
-
EnqueueMessageCommand,
|
|
28
|
-
RemoveQueueItemCommand,
|
|
29
|
-
SteerQueueItemCommand,
|
|
30
|
-
} from "./types";
|
|
21
|
+
import type { AddMessageCommand, AssistantTransportCommand } from "./types";
|
|
31
22
|
import {
|
|
32
23
|
sendCommand,
|
|
33
24
|
threadTexts,
|
|
34
25
|
toThreadMessage,
|
|
35
26
|
} from "./__tests__/testHelpers";
|
|
36
27
|
|
|
28
|
+
type QueueEntry = {
|
|
29
|
+
id: string;
|
|
30
|
+
parts: StatewireThread.UserMessage["parts"];
|
|
31
|
+
};
|
|
32
|
+
|
|
37
33
|
type ServerState = {
|
|
38
34
|
messages: { id: string; role: "user" | "assistant"; text: string }[];
|
|
39
35
|
running: boolean;
|
|
40
|
-
queue:
|
|
36
|
+
queue: QueueEntry[];
|
|
37
|
+
steerQueue: QueueEntry[];
|
|
38
|
+
inputRequests: StatewireThread.InputRequestState[];
|
|
41
39
|
};
|
|
42
40
|
|
|
43
41
|
const textOf = (command: Pick<AddMessageCommand, "message">) =>
|
|
@@ -45,6 +43,11 @@ const textOf = (command: Pick<AddMessageCommand, "message">) =>
|
|
|
45
43
|
.flatMap((p) => (p.type === "text" ? [p.text] : []))
|
|
46
44
|
.join("");
|
|
47
45
|
|
|
46
|
+
const textEntry = (id: string, text: string): QueueEntry => ({
|
|
47
|
+
id,
|
|
48
|
+
parts: [{ type: "text", text }],
|
|
49
|
+
});
|
|
50
|
+
|
|
48
51
|
const converter: StatewireThread.StateConverter<ServerState> = (
|
|
49
52
|
state,
|
|
50
53
|
meta,
|
|
@@ -69,27 +72,116 @@ const converter: StatewireThread.StateConverter<ServerState> = (
|
|
|
69
72
|
const createServer = (init?: {
|
|
70
73
|
messages?: ServerState["messages"];
|
|
71
74
|
queue?: ServerState["queue"];
|
|
75
|
+
steerQueue?: ServerState["steerQueue"];
|
|
76
|
+
inputRequests?: ServerState["inputRequests"];
|
|
72
77
|
}) => {
|
|
73
78
|
const received: unknown[] = [];
|
|
74
79
|
const cell: {
|
|
75
80
|
state?: ServerState;
|
|
76
81
|
hold?: boolean;
|
|
77
|
-
resolvers?:
|
|
82
|
+
resolvers?: { resolve: () => void };
|
|
78
83
|
} = {};
|
|
79
84
|
const transport = resource(() => {
|
|
80
85
|
const [state] = useStatewireState<ServerState>(() => ({
|
|
81
86
|
messages: init?.messages ?? [],
|
|
82
87
|
running: false,
|
|
83
88
|
queue: init?.queue ?? [],
|
|
89
|
+
steerQueue: init?.steerQueue ?? [],
|
|
90
|
+
inputRequests: init?.inputRequests ?? [],
|
|
84
91
|
}));
|
|
85
92
|
cell.state = state;
|
|
93
|
+
|
|
94
|
+
const takeEntry = (id: string): QueueEntry | undefined => {
|
|
95
|
+
for (const lane of [state.steerQueue, state.queue]) {
|
|
96
|
+
const index = lane.findIndex((entry) => entry.id === id);
|
|
97
|
+
if (index !== -1) {
|
|
98
|
+
// snapshot before splicing: element proxies are path-based
|
|
99
|
+
const removed = JSON.parse(JSON.stringify(lane[index])) as QueueEntry;
|
|
100
|
+
lane.splice(index, 1);
|
|
101
|
+
return removed;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return undefined;
|
|
105
|
+
};
|
|
106
|
+
const place = (
|
|
107
|
+
lane: QueueEntry[],
|
|
108
|
+
entry: QueueEntry,
|
|
109
|
+
placement: StatewireThread.Placement,
|
|
110
|
+
) => {
|
|
111
|
+
if (placement.insertAfter === null) lane.unshift(entry);
|
|
112
|
+
else if (placement.insertAfter != null)
|
|
113
|
+
lane.splice(
|
|
114
|
+
lane.findIndex((e) => e.id === placement.insertAfter) + 1,
|
|
115
|
+
0,
|
|
116
|
+
entry,
|
|
117
|
+
);
|
|
118
|
+
else if (placement.insertBefore == null) lane.push(entry);
|
|
119
|
+
else
|
|
120
|
+
lane.splice(
|
|
121
|
+
lane.findIndex((e) => e.id === placement.insertBefore),
|
|
122
|
+
0,
|
|
123
|
+
entry,
|
|
124
|
+
);
|
|
125
|
+
};
|
|
126
|
+
const submitSend = (
|
|
127
|
+
lane: "queue" | "steerQueue",
|
|
128
|
+
command:
|
|
129
|
+
| StatewireThread.RunEnqueueCommand
|
|
130
|
+
| StatewireThread.RunSteerCommand,
|
|
131
|
+
) => {
|
|
132
|
+
received.push(command);
|
|
133
|
+
if ("messageId" in command) {
|
|
134
|
+
const entry = takeEntry(command.messageId);
|
|
135
|
+
if (!entry) throw new StatewireReject("unknown-id");
|
|
136
|
+
place(state[lane], entry, command);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const entry: QueueEntry = {
|
|
140
|
+
id: command.message.id,
|
|
141
|
+
parts: [...command.message.parts],
|
|
142
|
+
};
|
|
143
|
+
const replaced = takeEntry(entry.id);
|
|
144
|
+
if (replaced) {
|
|
145
|
+
place(state[lane], entry, command);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (!state.running) {
|
|
149
|
+
state.messages.push({
|
|
150
|
+
id: entry.id,
|
|
151
|
+
role: "user",
|
|
152
|
+
text: command.message.parts
|
|
153
|
+
.flatMap((p) => (p.type === "text" ? [p.text] : []))
|
|
154
|
+
.join(""),
|
|
155
|
+
});
|
|
156
|
+
state.running = true;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
state[lane].push(entry);
|
|
160
|
+
};
|
|
161
|
+
|
|
86
162
|
const commands = useStatewireCommands({
|
|
163
|
+
"run/enqueue": (command: StatewireThread.RunEnqueueCommand) =>
|
|
164
|
+
submitSend("queue", command),
|
|
165
|
+
"run/steer": (command: StatewireThread.RunSteerCommand) =>
|
|
166
|
+
submitSend("steerQueue", command),
|
|
167
|
+
"run/dequeue": (command: StatewireThread.RunDequeueCommand) => {
|
|
168
|
+
received.push(command);
|
|
169
|
+
if (!takeEntry(command.messageId))
|
|
170
|
+
throw new StatewireReject("unknown-id");
|
|
171
|
+
},
|
|
172
|
+
"run/edit": (command: StatewireThread.RunEditCommand) => {
|
|
173
|
+
received.push(command);
|
|
174
|
+
},
|
|
175
|
+
"run/continue": (command: StatewireThread.RunContinueCommand) => {
|
|
176
|
+
received.push(command);
|
|
177
|
+
state.running = true;
|
|
178
|
+
},
|
|
87
179
|
"add-message": (command: AddMessageCommand) => {
|
|
88
180
|
received.push(command);
|
|
89
181
|
if (cell.hold) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
182
|
+
return new Promise<void>((resolve) => {
|
|
183
|
+
cell.resolvers = { resolve };
|
|
184
|
+
});
|
|
93
185
|
}
|
|
94
186
|
state.messages.push({
|
|
95
187
|
id: `u${state.messages.length}`,
|
|
@@ -99,34 +191,23 @@ const createServer = (init?: {
|
|
|
99
191
|
state.running = true;
|
|
100
192
|
return undefined;
|
|
101
193
|
},
|
|
102
|
-
|
|
194
|
+
"run/stop": (payload?: unknown) => {
|
|
103
195
|
received.push(
|
|
104
196
|
payload === undefined
|
|
105
|
-
? { type: "
|
|
106
|
-
: { type: "
|
|
197
|
+
? { type: "run/stop" }
|
|
198
|
+
: { type: "run/stop", payload },
|
|
107
199
|
);
|
|
108
200
|
state.running = false;
|
|
109
201
|
},
|
|
110
|
-
"
|
|
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) => {
|
|
202
|
+
"run/reload": (command: StatewireThread.RunReloadCommand) => {
|
|
118
203
|
received.push(command);
|
|
204
|
+
state.running = true;
|
|
119
205
|
},
|
|
120
|
-
"
|
|
206
|
+
"run/input": (command: StatewireThread.RunInputCommand) => {
|
|
121
207
|
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
208
|
},
|
|
127
|
-
"
|
|
209
|
+
"add-tool-result": (command: AssistantTransportCommand) => {
|
|
128
210
|
received.push(command);
|
|
129
|
-
state.queue.splice(0);
|
|
130
211
|
},
|
|
131
212
|
"reject-me": () => {
|
|
132
213
|
throw new StatewireReject("nope");
|
|
@@ -137,7 +218,13 @@ const createServer = (init?: {
|
|
|
137
218
|
return { transport, received, cell };
|
|
138
219
|
};
|
|
139
220
|
|
|
140
|
-
const INITIAL_STATE: ServerState = {
|
|
221
|
+
const INITIAL_STATE: ServerState = {
|
|
222
|
+
messages: [],
|
|
223
|
+
running: false,
|
|
224
|
+
queue: [],
|
|
225
|
+
steerQueue: [],
|
|
226
|
+
inputRequests: [],
|
|
227
|
+
};
|
|
141
228
|
|
|
142
229
|
const mountThread = (
|
|
143
230
|
options: Omit<StatewireThread.Options<ServerState>, "initialState"> &
|
|
@@ -190,6 +277,8 @@ describe("StatewireThread", () => {
|
|
|
190
277
|
messages: [{ id: "m1", role: "assistant", text: "welcome" }],
|
|
191
278
|
running: false,
|
|
192
279
|
queue: [],
|
|
280
|
+
steerQueue: [],
|
|
281
|
+
inputRequests: [],
|
|
193
282
|
}));
|
|
194
283
|
const commands = useStatewireCommands({});
|
|
195
284
|
return { state: attached ? state : undefined, commands };
|
|
@@ -199,6 +288,8 @@ describe("StatewireThread", () => {
|
|
|
199
288
|
messages: [{ id: "i1", role: "assistant", text: "local" }],
|
|
200
289
|
running: false,
|
|
201
290
|
queue: [],
|
|
291
|
+
steerQueue: [],
|
|
292
|
+
inputRequests: [],
|
|
202
293
|
};
|
|
203
294
|
const observed: unknown[] = [];
|
|
204
295
|
const Probe: FC = () => {
|
|
@@ -278,7 +369,7 @@ describe("StatewireThread", () => {
|
|
|
278
369
|
expect(metas.at(-1)!.pendingCommands).toEqual([]);
|
|
279
370
|
});
|
|
280
371
|
|
|
281
|
-
it("dispatches the
|
|
372
|
+
it("dispatches the run/stop command from cancelRun", async () => {
|
|
282
373
|
const server = createServer();
|
|
283
374
|
const { aui } = mountThread({ transport: server.transport, converter });
|
|
284
375
|
|
|
@@ -288,7 +379,7 @@ describe("StatewireThread", () => {
|
|
|
288
379
|
aui().thread().cancelRun();
|
|
289
380
|
|
|
290
381
|
await waitFor(() =>
|
|
291
|
-
expect(server.received).toContainEqual({ type: "
|
|
382
|
+
expect(server.received).toContainEqual({ type: "run/stop" }),
|
|
292
383
|
);
|
|
293
384
|
await waitFor(() =>
|
|
294
385
|
expect(aui().thread().getState().isRunning).toBe(false),
|
|
@@ -337,6 +428,8 @@ describe("StatewireThread", () => {
|
|
|
337
428
|
messages: [],
|
|
338
429
|
running: false,
|
|
339
430
|
queue: [],
|
|
431
|
+
steerQueue: [],
|
|
432
|
+
inputRequests: [],
|
|
340
433
|
});
|
|
341
434
|
const { aui } = mountThread({
|
|
342
435
|
transport: mock.transport,
|
|
@@ -361,6 +454,8 @@ describe("StatewireThread", () => {
|
|
|
361
454
|
messages: [],
|
|
362
455
|
running: false,
|
|
363
456
|
queue: [],
|
|
457
|
+
steerQueue: [],
|
|
458
|
+
inputRequests: [],
|
|
364
459
|
});
|
|
365
460
|
const { aui } = mountThread({
|
|
366
461
|
transport: mock.transport,
|
|
@@ -386,6 +481,8 @@ describe("StatewireThread", () => {
|
|
|
386
481
|
messages: [],
|
|
387
482
|
running: false,
|
|
388
483
|
queue: [],
|
|
484
|
+
steerQueue: [],
|
|
485
|
+
inputRequests: [],
|
|
389
486
|
});
|
|
390
487
|
const { aui } = mountThread({ transport: mock.transport, converter });
|
|
391
488
|
await waitFor(() =>
|
|
@@ -395,16 +492,17 @@ describe("StatewireThread", () => {
|
|
|
395
492
|
});
|
|
396
493
|
});
|
|
397
494
|
|
|
398
|
-
describe("
|
|
495
|
+
describe("run/* queue", () => {
|
|
399
496
|
const queueConverter: StatewireThread.StateConverter<ServerState> = (
|
|
400
497
|
state,
|
|
401
498
|
meta,
|
|
402
499
|
) => ({
|
|
403
500
|
...converter(state, meta),
|
|
404
501
|
queue: state.queue,
|
|
502
|
+
steerQueue: state.steerQueue,
|
|
405
503
|
});
|
|
406
504
|
|
|
407
|
-
it("
|
|
505
|
+
it("sends run/enqueue: idle append starts a run, mid-run append becomes a queue item", async () => {
|
|
408
506
|
const server = createServer();
|
|
409
507
|
const { aui } = mountThread({
|
|
410
508
|
transport: server.transport,
|
|
@@ -417,8 +515,18 @@ describe("StatewireThread", () => {
|
|
|
417
515
|
|
|
418
516
|
aui().thread().append("hello");
|
|
419
517
|
await waitFor(() =>
|
|
420
|
-
expect(server.received).toMatchObject([
|
|
518
|
+
expect(server.received).toMatchObject([
|
|
519
|
+
{
|
|
520
|
+
type: "run/enqueue",
|
|
521
|
+
message: {
|
|
522
|
+
id: expect.any(String),
|
|
523
|
+
role: "user",
|
|
524
|
+
parts: [{ type: "text", text: "hello" }],
|
|
525
|
+
},
|
|
526
|
+
},
|
|
527
|
+
]),
|
|
421
528
|
);
|
|
529
|
+
await waitFor(() => expect(threadTexts(aui)).toEqual(["hello"]));
|
|
422
530
|
await waitFor(() =>
|
|
423
531
|
expect(aui().thread().getState().isRunning).toBe(true),
|
|
424
532
|
);
|
|
@@ -426,8 +534,7 @@ describe("StatewireThread", () => {
|
|
|
426
534
|
aui().thread().append("queued follow-up");
|
|
427
535
|
await waitFor(() =>
|
|
428
536
|
expect(server.received.at(-1)).toMatchObject({
|
|
429
|
-
type: "enqueue
|
|
430
|
-
steer: false,
|
|
537
|
+
type: "run/enqueue",
|
|
431
538
|
message: {
|
|
432
539
|
role: "user",
|
|
433
540
|
parts: [{ type: "text", text: "queued follow-up" }],
|
|
@@ -436,14 +543,50 @@ describe("StatewireThread", () => {
|
|
|
436
543
|
);
|
|
437
544
|
await waitFor(() =>
|
|
438
545
|
expect(aui().thread().composer().getState().queue).toMatchObject([
|
|
439
|
-
{
|
|
546
|
+
{
|
|
547
|
+
prompt: "queued follow-up",
|
|
548
|
+
parts: [{ type: "text", text: "queued follow-up" }],
|
|
549
|
+
},
|
|
550
|
+
]),
|
|
551
|
+
);
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
it("steers by default when the composer sends mid-run", async () => {
|
|
555
|
+
const server = createServer();
|
|
556
|
+
const { aui } = mountThread({
|
|
557
|
+
transport: server.transport,
|
|
558
|
+
converter: queueConverter,
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
aui().thread().append("hello");
|
|
562
|
+
await waitFor(() =>
|
|
563
|
+
expect(aui().thread().getState().isRunning).toBe(true),
|
|
564
|
+
);
|
|
565
|
+
|
|
566
|
+
aui().thread().composer().setText("steer me");
|
|
567
|
+
aui().thread().composer().send();
|
|
568
|
+
|
|
569
|
+
await waitFor(() =>
|
|
570
|
+
expect(server.received.at(-1)).toMatchObject({
|
|
571
|
+
type: "run/steer",
|
|
572
|
+
message: {
|
|
573
|
+
id: expect.any(String),
|
|
574
|
+
role: "user",
|
|
575
|
+
parts: [{ type: "text", text: "steer me" }],
|
|
576
|
+
},
|
|
577
|
+
}),
|
|
578
|
+
);
|
|
579
|
+
await waitFor(() =>
|
|
580
|
+
expect(aui().thread().composer().getState().queue).toMatchObject([
|
|
581
|
+
{ prompt: "steer me" },
|
|
440
582
|
]),
|
|
441
583
|
);
|
|
442
584
|
});
|
|
443
585
|
|
|
444
|
-
it("
|
|
586
|
+
it("projects the steer lane ahead of the regular lane", async () => {
|
|
445
587
|
const server = createServer({
|
|
446
|
-
queue: [
|
|
588
|
+
queue: [textEntry("q1", "first"), textEntry("q2", "second")],
|
|
589
|
+
steerQueue: [textEntry("s1", "urgent")],
|
|
447
590
|
});
|
|
448
591
|
const { aui } = mountThread({
|
|
449
592
|
transport: server.transport,
|
|
@@ -451,32 +594,88 @@ describe("StatewireThread", () => {
|
|
|
451
594
|
});
|
|
452
595
|
|
|
453
596
|
await waitFor(() =>
|
|
454
|
-
expect(aui().thread().composer().getState().queue).
|
|
597
|
+
expect(aui().thread().composer().getState().queue).toMatchObject([
|
|
598
|
+
{ id: "s1", prompt: "urgent" },
|
|
599
|
+
{ id: "q1", prompt: "first" },
|
|
600
|
+
{ id: "q2", prompt: "second" },
|
|
601
|
+
]),
|
|
602
|
+
);
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
it("sends item controls as run/* commands with placement", async () => {
|
|
606
|
+
const server = createServer({
|
|
607
|
+
queue: [textEntry("q1", "first"), textEntry("q2", "second")],
|
|
608
|
+
steerQueue: [textEntry("s1", "urgent")],
|
|
609
|
+
});
|
|
610
|
+
const { aui } = mountThread({
|
|
611
|
+
transport: server.transport,
|
|
612
|
+
converter: queueConverter,
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
await waitFor(() =>
|
|
616
|
+
expect(aui().thread().composer().getState().queue).toHaveLength(3),
|
|
617
|
+
);
|
|
618
|
+
|
|
619
|
+
aui().thread().composer().queueItem({ id: "q1" }).steer();
|
|
620
|
+
await waitFor(() =>
|
|
621
|
+
expect(server.received).toContainEqual({
|
|
622
|
+
type: "run/steer",
|
|
623
|
+
messageId: "q1",
|
|
624
|
+
insertAfter: null,
|
|
625
|
+
}),
|
|
626
|
+
);
|
|
627
|
+
await waitFor(() =>
|
|
628
|
+
expect(server.cell.state!.steerQueue.map((e) => e.id)).toEqual([
|
|
629
|
+
"q1",
|
|
630
|
+
"s1",
|
|
631
|
+
]),
|
|
632
|
+
);
|
|
633
|
+
|
|
634
|
+
// no lane: repositions within the item's current lane
|
|
635
|
+
aui()
|
|
636
|
+
.thread()
|
|
637
|
+
.composer()
|
|
638
|
+
.queueItem({ id: "s1" })
|
|
639
|
+
.move({ insertBefore: null });
|
|
640
|
+
await waitFor(() =>
|
|
641
|
+
expect(server.received).toContainEqual({
|
|
642
|
+
type: "run/steer",
|
|
643
|
+
messageId: "s1",
|
|
644
|
+
insertBefore: null,
|
|
645
|
+
}),
|
|
455
646
|
);
|
|
456
647
|
|
|
457
|
-
aui()
|
|
648
|
+
aui()
|
|
649
|
+
.thread()
|
|
650
|
+
.composer()
|
|
651
|
+
.queueItem({ id: "q1" })
|
|
652
|
+
.move({ lane: "queue", insertAfter: "q2" });
|
|
458
653
|
await waitFor(() =>
|
|
459
654
|
expect(server.received).toContainEqual({
|
|
460
|
-
type: "
|
|
461
|
-
|
|
655
|
+
type: "run/enqueue",
|
|
656
|
+
messageId: "q1",
|
|
657
|
+
insertAfter: "q2",
|
|
462
658
|
}),
|
|
463
659
|
);
|
|
660
|
+
await waitFor(() =>
|
|
661
|
+
expect(server.cell.state!.queue.map((e) => e.id)).toEqual(["q2", "q1"]),
|
|
662
|
+
);
|
|
464
663
|
|
|
465
|
-
aui().thread().composer().queueItem({
|
|
664
|
+
aui().thread().composer().queueItem({ id: "q1" }).remove();
|
|
466
665
|
await waitFor(() =>
|
|
467
666
|
expect(server.received).toContainEqual({
|
|
468
|
-
type: "
|
|
469
|
-
|
|
667
|
+
type: "run/dequeue",
|
|
668
|
+
messageId: "q1",
|
|
470
669
|
}),
|
|
471
670
|
);
|
|
472
671
|
await waitFor(() =>
|
|
473
|
-
expect(aui().thread().composer().getState().queue).toHaveLength(
|
|
672
|
+
expect(aui().thread().composer().getState().queue).toHaveLength(2),
|
|
474
673
|
);
|
|
475
674
|
});
|
|
476
675
|
|
|
477
|
-
it("
|
|
676
|
+
it("leaves the host queue alone on cancel", async () => {
|
|
478
677
|
const server = createServer({
|
|
479
|
-
queue: [
|
|
678
|
+
queue: [textEntry("q1", "queued")],
|
|
480
679
|
});
|
|
481
680
|
const { aui } = mountThread({
|
|
482
681
|
transport: server.transport,
|
|
@@ -490,27 +689,290 @@ describe("StatewireThread", () => {
|
|
|
490
689
|
|
|
491
690
|
aui().thread().cancelRun();
|
|
492
691
|
|
|
692
|
+
await waitFor(() =>
|
|
693
|
+
expect(server.received).toContainEqual({ type: "run/stop" }),
|
|
694
|
+
);
|
|
695
|
+
expect(server.cell.state!.queue.map((e) => e.id)).toEqual(["q1"]);
|
|
696
|
+
await waitFor(() =>
|
|
697
|
+
expect(aui().thread().composer().getState().queue).toHaveLength(1),
|
|
698
|
+
);
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
it("sends run/edit for message edits", async () => {
|
|
702
|
+
const server = createServer({
|
|
703
|
+
messages: [
|
|
704
|
+
{ id: "h1", role: "user", text: "bad question" },
|
|
705
|
+
{ id: "a1", role: "assistant", text: "bad answer" },
|
|
706
|
+
],
|
|
707
|
+
});
|
|
708
|
+
const { aui } = mountThread({
|
|
709
|
+
transport: server.transport,
|
|
710
|
+
converter: queueConverter,
|
|
711
|
+
capabilities: { edit: true },
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
await waitFor(() =>
|
|
715
|
+
expect(threadTexts(aui)).toEqual(["bad question", "bad answer"]),
|
|
716
|
+
);
|
|
717
|
+
|
|
718
|
+
const composer = aui().thread().message({ id: "h1" }).composer();
|
|
719
|
+
composer.beginEdit();
|
|
720
|
+
await waitFor(() => expect(composer.getState().isEditing).toBe(true));
|
|
721
|
+
composer.setText("better question");
|
|
722
|
+
composer.send();
|
|
723
|
+
|
|
724
|
+
await waitFor(() =>
|
|
725
|
+
expect(server.received).toContainEqual({
|
|
726
|
+
type: "run/edit",
|
|
727
|
+
sourceId: "h1",
|
|
728
|
+
message: {
|
|
729
|
+
id: expect.any(String),
|
|
730
|
+
role: "user",
|
|
731
|
+
parts: [{ type: "text", text: "better question" }],
|
|
732
|
+
},
|
|
733
|
+
}),
|
|
734
|
+
);
|
|
735
|
+
const command = server.received.at(-1) as StatewireThread.RunEditCommand;
|
|
736
|
+
expect(command.message.id).not.toBe("h1");
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
it("dispatches run/continue from resumeRun under the continue capability", async () => {
|
|
740
|
+
const server = createServer();
|
|
741
|
+
const { aui } = mountThread({
|
|
742
|
+
transport: server.transport,
|
|
743
|
+
converter: queueConverter,
|
|
744
|
+
capabilities: { continue: true },
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
await waitFor(() =>
|
|
748
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
749
|
+
);
|
|
750
|
+
|
|
751
|
+
aui().thread().resumeRun({ parentId: null });
|
|
752
|
+
|
|
753
|
+
await waitFor(() =>
|
|
754
|
+
expect(server.received).toContainEqual({ type: "run/continue" }),
|
|
755
|
+
);
|
|
756
|
+
await waitFor(() =>
|
|
757
|
+
expect(aui().thread().getState().isRunning).toBe(true),
|
|
758
|
+
);
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
it("rejects resumeRun without the continue capability", async () => {
|
|
762
|
+
const server = createServer();
|
|
763
|
+
const { aui } = mountThread({
|
|
764
|
+
transport: server.transport,
|
|
765
|
+
converter: queueConverter,
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
await waitFor(() =>
|
|
769
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
770
|
+
);
|
|
771
|
+
|
|
772
|
+
expect(() => aui().thread().resumeRun({ parentId: null })).toThrow(
|
|
773
|
+
/does not support resuming/,
|
|
774
|
+
);
|
|
775
|
+
});
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
describe("run/input", () => {
|
|
779
|
+
const toolConverter =
|
|
780
|
+
(approval?: {
|
|
781
|
+
id: string;
|
|
782
|
+
}): StatewireThread.StateConverter<ServerState> =>
|
|
783
|
+
(state, meta) => ({
|
|
784
|
+
...converter(state, meta),
|
|
785
|
+
messages: [
|
|
786
|
+
{
|
|
787
|
+
id: "a-tool",
|
|
788
|
+
role: "assistant",
|
|
789
|
+
createdAt: new Date(0),
|
|
790
|
+
content: [
|
|
791
|
+
{
|
|
792
|
+
type: "tool-call",
|
|
793
|
+
toolCallId: "tc1",
|
|
794
|
+
toolName: "probe_tool",
|
|
795
|
+
args: {},
|
|
796
|
+
argsText: "{}",
|
|
797
|
+
...(approval && { approval }),
|
|
798
|
+
},
|
|
799
|
+
],
|
|
800
|
+
attachments: [],
|
|
801
|
+
metadata: { custom: {} },
|
|
802
|
+
status: { type: "requires-action", reason: "tool-call" },
|
|
803
|
+
},
|
|
804
|
+
] as unknown as StatewireThread.State["messages"],
|
|
805
|
+
inputRequests: state.inputRequests,
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
const toolCallPart = (aui: () => ReturnType<typeof useAui>) =>
|
|
809
|
+
aui().thread().message({ id: "a-tool" }).part({ toolCallId: "tc1" });
|
|
810
|
+
|
|
811
|
+
it("answers a pending tool-call request via run/input", async () => {
|
|
812
|
+
const server = createServer({
|
|
813
|
+
inputRequests: [
|
|
814
|
+
{ type: "tool-call", id: "req1", toolCallId: "tc1", response: null },
|
|
815
|
+
],
|
|
816
|
+
});
|
|
817
|
+
const { aui } = mountThread({
|
|
818
|
+
transport: server.transport,
|
|
819
|
+
converter: toolConverter(),
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
await waitFor(() =>
|
|
823
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
824
|
+
);
|
|
825
|
+
toolCallPart(aui).addToolResult({ ok: true });
|
|
826
|
+
|
|
827
|
+
await waitFor(() =>
|
|
828
|
+
expect(server.received).toContainEqual({
|
|
829
|
+
type: "run/input",
|
|
830
|
+
requestId: "req1",
|
|
831
|
+
response: { output: { ok: true } },
|
|
832
|
+
}),
|
|
833
|
+
);
|
|
834
|
+
});
|
|
835
|
+
|
|
836
|
+
it("falls back to add-tool-result without a matching pending request", async () => {
|
|
837
|
+
const server = createServer();
|
|
838
|
+
const { aui } = mountThread({
|
|
839
|
+
transport: server.transport,
|
|
840
|
+
converter: toolConverter(),
|
|
841
|
+
});
|
|
842
|
+
|
|
843
|
+
await waitFor(() =>
|
|
844
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
845
|
+
);
|
|
846
|
+
toolCallPart(aui).addToolResult({ ok: true });
|
|
847
|
+
|
|
848
|
+
await waitFor(() =>
|
|
849
|
+
expect(server.received).toContainEqual({
|
|
850
|
+
type: "add-tool-result",
|
|
851
|
+
toolCallId: "tc1",
|
|
852
|
+
toolName: "probe_tool",
|
|
853
|
+
result: { ok: true },
|
|
854
|
+
isError: false,
|
|
855
|
+
}),
|
|
856
|
+
);
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
it("throws on answering a request that already carries a response", async () => {
|
|
860
|
+
const server = createServer({
|
|
861
|
+
inputRequests: [
|
|
862
|
+
{
|
|
863
|
+
type: "tool-call",
|
|
864
|
+
id: "req1",
|
|
865
|
+
toolCallId: "tc1",
|
|
866
|
+
response: { output: "done" },
|
|
867
|
+
},
|
|
868
|
+
],
|
|
869
|
+
});
|
|
870
|
+
const { aui } = mountThread({
|
|
871
|
+
transport: server.transport,
|
|
872
|
+
converter: toolConverter(),
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
await waitFor(() =>
|
|
876
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
877
|
+
);
|
|
878
|
+
expect(() => toolCallPart(aui).addToolResult({ ok: true })).toThrow(
|
|
879
|
+
'statewire: input request "req1" is already answered',
|
|
880
|
+
);
|
|
881
|
+
});
|
|
882
|
+
|
|
883
|
+
it("answers tool approvals via run/input decisions", async () => {
|
|
884
|
+
const server = createServer({
|
|
885
|
+
inputRequests: [
|
|
886
|
+
{
|
|
887
|
+
type: "tool-approval",
|
|
888
|
+
id: "req2",
|
|
889
|
+
toolCallId: "tc1",
|
|
890
|
+
response: null,
|
|
891
|
+
},
|
|
892
|
+
],
|
|
893
|
+
});
|
|
894
|
+
const { aui } = mountThread({
|
|
895
|
+
transport: server.transport,
|
|
896
|
+
converter: toolConverter({ id: "req2" }),
|
|
897
|
+
});
|
|
898
|
+
|
|
899
|
+
await waitFor(() =>
|
|
900
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
901
|
+
);
|
|
902
|
+
toolCallPart(aui).respondToToolApproval({ approved: true });
|
|
903
|
+
|
|
493
904
|
await waitFor(() =>
|
|
494
905
|
expect(server.received).toContainEqual({
|
|
495
|
-
type: "
|
|
496
|
-
|
|
906
|
+
type: "run/input",
|
|
907
|
+
requestId: "req2",
|
|
908
|
+
response: { decision: "approve" },
|
|
497
909
|
}),
|
|
498
910
|
);
|
|
911
|
+
});
|
|
912
|
+
|
|
913
|
+
it("surfaces custom request types through extras.inputRequests", async () => {
|
|
914
|
+
const server = createServer({
|
|
915
|
+
inputRequests: [
|
|
916
|
+
{
|
|
917
|
+
type: "human-review",
|
|
918
|
+
id: "req9",
|
|
919
|
+
prompt: "check this",
|
|
920
|
+
response: null,
|
|
921
|
+
},
|
|
922
|
+
],
|
|
923
|
+
});
|
|
924
|
+
const { aui } = mountThread({
|
|
925
|
+
transport: server.transport,
|
|
926
|
+
converter: toolConverter(),
|
|
927
|
+
});
|
|
928
|
+
|
|
499
929
|
await waitFor(() =>
|
|
500
|
-
expect(
|
|
930
|
+
expect(
|
|
931
|
+
asAssistantTransportExtras(aui().thread().getState().extras)
|
|
932
|
+
.inputRequests,
|
|
933
|
+
).toEqual([
|
|
934
|
+
{
|
|
935
|
+
type: "human-review",
|
|
936
|
+
id: "req9",
|
|
937
|
+
prompt: "check this",
|
|
938
|
+
response: null,
|
|
939
|
+
},
|
|
940
|
+
]),
|
|
501
941
|
);
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
it("leaves tool results unsupported when inputRequests is not projected", async () => {
|
|
945
|
+
const server = createServer();
|
|
946
|
+
const { aui } = mountThread({
|
|
947
|
+
transport: server.transport,
|
|
948
|
+
converter: (state, meta) => {
|
|
949
|
+
const { inputRequests: _omitted, ...rest } = toolConverter()(
|
|
950
|
+
state,
|
|
951
|
+
meta,
|
|
952
|
+
);
|
|
953
|
+
return rest;
|
|
954
|
+
},
|
|
955
|
+
});
|
|
956
|
+
|
|
502
957
|
await waitFor(() =>
|
|
503
|
-
expect(aui().thread().
|
|
958
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
504
959
|
);
|
|
960
|
+
expect(() => toolCallPart(aui).addToolResult({ ok: true })).toThrow(
|
|
961
|
+
/does not support tool results/,
|
|
962
|
+
);
|
|
963
|
+
expect(
|
|
964
|
+
asAssistantTransportExtras(aui().thread().getState().extras)
|
|
965
|
+
.inputRequests,
|
|
966
|
+
).toBeUndefined();
|
|
505
967
|
});
|
|
506
968
|
});
|
|
507
969
|
|
|
508
|
-
it("sends the
|
|
970
|
+
it("sends the stopPayload with the run/stop command", async () => {
|
|
509
971
|
const server = createServer();
|
|
510
972
|
const { aui } = mountThread({
|
|
511
973
|
transport: server.transport,
|
|
512
974
|
converter,
|
|
513
|
-
|
|
975
|
+
stopPayload: () => ({ reason: "user-stop", surface: "test" }),
|
|
514
976
|
});
|
|
515
977
|
|
|
516
978
|
aui().thread().append("hello");
|
|
@@ -520,12 +982,72 @@ describe("StatewireThread", () => {
|
|
|
520
982
|
|
|
521
983
|
await waitFor(() =>
|
|
522
984
|
expect(server.received).toContainEqual({
|
|
523
|
-
type: "
|
|
985
|
+
type: "run/stop",
|
|
524
986
|
payload: { reason: "user-stop", surface: "test" },
|
|
525
987
|
}),
|
|
526
988
|
);
|
|
527
989
|
});
|
|
528
990
|
|
|
991
|
+
it("dispatches run/reload for the assistant message under the reload capability", async () => {
|
|
992
|
+
const server = createServer({
|
|
993
|
+
messages: [
|
|
994
|
+
{ id: "h1", role: "user", text: "question" },
|
|
995
|
+
{ id: "a1", role: "assistant", text: "answer" },
|
|
996
|
+
],
|
|
997
|
+
});
|
|
998
|
+
const { aui } = mountThread({
|
|
999
|
+
transport: server.transport,
|
|
1000
|
+
converter,
|
|
1001
|
+
capabilities: { reload: true },
|
|
1002
|
+
});
|
|
1003
|
+
|
|
1004
|
+
await waitFor(() =>
|
|
1005
|
+
expect(threadTexts(aui)).toEqual(["question", "answer"]),
|
|
1006
|
+
);
|
|
1007
|
+
expect(aui().thread().getState().capabilities.reload).toBe(true);
|
|
1008
|
+
|
|
1009
|
+
aui().thread().message({ id: "a1" }).reload();
|
|
1010
|
+
|
|
1011
|
+
await waitFor(() =>
|
|
1012
|
+
expect(server.received).toContainEqual({
|
|
1013
|
+
type: "run/reload",
|
|
1014
|
+
sourceId: "a1",
|
|
1015
|
+
}),
|
|
1016
|
+
);
|
|
1017
|
+
});
|
|
1018
|
+
|
|
1019
|
+
it("resolves run/reload on the first message when it has no parent", async () => {
|
|
1020
|
+
const server = createServer({
|
|
1021
|
+
messages: [{ id: "a0", role: "assistant", text: "welcome" }],
|
|
1022
|
+
});
|
|
1023
|
+
const { aui } = mountThread({
|
|
1024
|
+
transport: server.transport,
|
|
1025
|
+
converter,
|
|
1026
|
+
capabilities: { reload: true },
|
|
1027
|
+
});
|
|
1028
|
+
|
|
1029
|
+
await waitFor(() => expect(threadTexts(aui)).toEqual(["welcome"]));
|
|
1030
|
+
|
|
1031
|
+
aui().thread().message({ id: "a0" }).reload();
|
|
1032
|
+
|
|
1033
|
+
await waitFor(() =>
|
|
1034
|
+
expect(server.received).toContainEqual({
|
|
1035
|
+
type: "run/reload",
|
|
1036
|
+
sourceId: "a0",
|
|
1037
|
+
}),
|
|
1038
|
+
);
|
|
1039
|
+
});
|
|
1040
|
+
|
|
1041
|
+
it("leaves the reload capability off by default", async () => {
|
|
1042
|
+
const server = createServer();
|
|
1043
|
+
const { aui } = mountThread({ transport: server.transport, converter });
|
|
1044
|
+
|
|
1045
|
+
await waitFor(() =>
|
|
1046
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
1047
|
+
);
|
|
1048
|
+
expect(aui().thread().getState().capabilities.reload).toBe(false);
|
|
1049
|
+
});
|
|
1050
|
+
|
|
529
1051
|
it("enables the edit capability from options", async () => {
|
|
530
1052
|
const server = createServer();
|
|
531
1053
|
const { aui } = mountThread({
|
|
@@ -558,6 +1080,8 @@ describe("StatewireThread", () => {
|
|
|
558
1080
|
messages: [],
|
|
559
1081
|
running: false,
|
|
560
1082
|
queue: [],
|
|
1083
|
+
steerQueue: [],
|
|
1084
|
+
inputRequests: [],
|
|
561
1085
|
});
|
|
562
1086
|
const { aui } = mountThread({
|
|
563
1087
|
transport: mock.transport,
|
|
@@ -590,6 +1114,8 @@ describe("StatewireThread", () => {
|
|
|
590
1114
|
messages: [],
|
|
591
1115
|
running: false,
|
|
592
1116
|
queue: [],
|
|
1117
|
+
steerQueue: [],
|
|
1118
|
+
inputRequests: [],
|
|
593
1119
|
});
|
|
594
1120
|
const { aui } = mountThread({
|
|
595
1121
|
transport: mock.transport,
|