@assistant-ui/react-statewire 0.2.0 → 0.4.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 +52 -54
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +62 -5
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +33 -106
- package/dist/StatewireThread.js.map +1 -1
- package/dist/host.d.ts +1 -3
- 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 -7
- 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 +27 -0
- package/dist/runCommands.d.ts.map +1 -0
- package/dist/runCommands.js +109 -0
- package/dist/runCommands.js.map +1 -0
- package/dist/types.d.ts +110 -29
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/AssistantTransportThread.ts +117 -69
- package/src/StatewireThread.test.tsx +294 -40
- package/src/StatewireThread.ts +146 -133
- package/src/__tests__/testHelpers.tsx +19 -2
- package/src/athena.e2e.test.tsx +55 -31
- package/src/host.ts +23 -11
- package/src/index.ts +20 -6
- package/src/inputRequests.ts +60 -0
- package/src/legacy-diff.test.tsx +56 -13
- package/src/runCommands.ts +157 -0
- package/src/transport.test.tsx +609 -86
- package/src/types.ts +138 -32
package/src/transport.test.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
|
|
3
|
-
import { render, waitFor } from "@testing-library/react";
|
|
4
|
-
import { StrictMode } from "react";
|
|
3
|
+
import { act, render, waitFor } from "@testing-library/react";
|
|
4
|
+
import { StrictMode, useState } from "react";
|
|
5
5
|
import type { FC } from "react";
|
|
6
6
|
import { resource } from "@assistant-ui/tap";
|
|
7
7
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
@@ -11,7 +11,11 @@ import {
|
|
|
11
11
|
ExternalThread,
|
|
12
12
|
SimpleTextAttachmentAdapter,
|
|
13
13
|
} from "@assistant-ui/react";
|
|
14
|
-
import {
|
|
14
|
+
import { ToolResponse } from "assistant-stream";
|
|
15
|
+
import {
|
|
16
|
+
asAssistantTransportExtras,
|
|
17
|
+
AssistantTransportThread,
|
|
18
|
+
} from "./AssistantTransportThread";
|
|
15
19
|
import {
|
|
16
20
|
useAssistantTransportSendCommand,
|
|
17
21
|
useAssistantTransportState,
|
|
@@ -19,6 +23,7 @@ import {
|
|
|
19
23
|
import type {
|
|
20
24
|
AssistantTransportCommand,
|
|
21
25
|
AssistantTransportConnectionMetadata,
|
|
26
|
+
InputRequestState,
|
|
22
27
|
} from "./types";
|
|
23
28
|
import {
|
|
24
29
|
INITIAL_STATE,
|
|
@@ -56,10 +61,13 @@ describe("AssistantTransportThread", () => {
|
|
|
56
61
|
expect(fetchMock.requests[0]!.url).toBe("https://example.com/api");
|
|
57
62
|
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
58
63
|
{
|
|
59
|
-
type: "
|
|
60
|
-
message: {
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
type: "run/steer",
|
|
65
|
+
message: {
|
|
66
|
+
id: expect.any(String),
|
|
67
|
+
role: "user",
|
|
68
|
+
parts: [{ type: "text", text: "hello" }],
|
|
69
|
+
},
|
|
70
|
+
anchorMessageId: null,
|
|
63
71
|
},
|
|
64
72
|
]);
|
|
65
73
|
expect(fetchMock.requests[0]!.body.state).toEqual(INITIAL_STATE);
|
|
@@ -147,7 +155,7 @@ describe("AssistantTransportThread", () => {
|
|
|
147
155
|
const meta = metas.at(-1)!;
|
|
148
156
|
expect(meta.isSending).toBe(true);
|
|
149
157
|
expect(meta.pendingCommands).toHaveLength(1);
|
|
150
|
-
expect(meta.pendingCommands[0]).toMatchObject({ type: "
|
|
158
|
+
expect(meta.pendingCommands[0]).toMatchObject({ type: "run/steer" });
|
|
151
159
|
});
|
|
152
160
|
|
|
153
161
|
server.emit({
|
|
@@ -239,10 +247,12 @@ describe("AssistantTransportThread", () => {
|
|
|
239
247
|
});
|
|
240
248
|
|
|
241
249
|
sendCommand(aui, {
|
|
242
|
-
type: "
|
|
243
|
-
message: {
|
|
244
|
-
|
|
245
|
-
|
|
250
|
+
type: "run/steer",
|
|
251
|
+
message: {
|
|
252
|
+
id: "u1",
|
|
253
|
+
role: "user",
|
|
254
|
+
parts: [{ type: "text", text: "hello" }],
|
|
255
|
+
},
|
|
246
256
|
});
|
|
247
257
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
248
258
|
await waitFor(() =>
|
|
@@ -562,32 +572,19 @@ describe("AssistantTransportThread", () => {
|
|
|
562
572
|
expect(errors).toEqual([]);
|
|
563
573
|
});
|
|
564
574
|
|
|
565
|
-
it("
|
|
575
|
+
it("throws on appends that produce zero parts", async () => {
|
|
566
576
|
const fetchMock = installFetch();
|
|
567
577
|
const { aui } = mountThread();
|
|
568
578
|
|
|
569
|
-
aui().thread().append({ role: "user", content: [] })
|
|
579
|
+
expect(() => aui().thread().append({ role: "user", content: [] })).toThrow(
|
|
580
|
+
"statewire: cannot send an empty message",
|
|
581
|
+
);
|
|
570
582
|
|
|
571
583
|
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
572
584
|
expect(fetchMock.requests).toHaveLength(0);
|
|
573
585
|
expect(aui().thread().getState().messages).toHaveLength(0);
|
|
574
586
|
});
|
|
575
587
|
|
|
576
|
-
it("records parentId from appends that produce zero parts", async () => {
|
|
577
|
-
const fetchMock = installFetch();
|
|
578
|
-
const { aui } = mountThread();
|
|
579
|
-
|
|
580
|
-
aui().thread().append({ role: "user", content: [], parentId: "p1" });
|
|
581
|
-
|
|
582
|
-
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
583
|
-
expect(fetchMock.requests).toHaveLength(0);
|
|
584
|
-
|
|
585
|
-
sendCommand(aui, { type: "custom-a" } as never);
|
|
586
|
-
|
|
587
|
-
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
588
|
-
expect(fetchMock.requests[0]!.body.parentId).toBe("p1");
|
|
589
|
-
});
|
|
590
|
-
|
|
591
588
|
it("resumes runs against resumeApi with empty commands", async () => {
|
|
592
589
|
const fetchMock = installFetch();
|
|
593
590
|
fetchMock.setHandler(() => {
|
|
@@ -1126,12 +1123,10 @@ describe("AssistantTransportThread", () => {
|
|
|
1126
1123
|
expect(aui().thread().getState().isRunning).toBe(false),
|
|
1127
1124
|
);
|
|
1128
1125
|
|
|
1129
|
-
// Give any stray
|
|
1126
|
+
// Give any stray run/input dispatch a chance to fire.
|
|
1130
1127
|
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
1131
1128
|
expect(
|
|
1132
|
-
allCommands(fetchMock.requests).filter(
|
|
1133
|
-
(c) => c.type === "tool-result/add",
|
|
1134
|
-
),
|
|
1129
|
+
allCommands(fetchMock.requests).filter((c) => c.type === "run/input"),
|
|
1135
1130
|
).toEqual([]);
|
|
1136
1131
|
});
|
|
1137
1132
|
|
|
@@ -1156,7 +1151,7 @@ describe("AssistantTransportThread", () => {
|
|
|
1156
1151
|
);
|
|
1157
1152
|
});
|
|
1158
1153
|
|
|
1159
|
-
it("folds completed attachments into the
|
|
1154
|
+
it("folds completed attachments into the run/steer command (CompositeAttachmentAdapter)", async () => {
|
|
1160
1155
|
const fetchMock = installFetch();
|
|
1161
1156
|
const { aui } = mountThread({
|
|
1162
1157
|
adapters: {
|
|
@@ -1180,14 +1175,15 @@ describe("AssistantTransportThread", () => {
|
|
|
1180
1175
|
|
|
1181
1176
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
1182
1177
|
const command = fetchMock.requests[0]!.body.commands[0];
|
|
1183
|
-
expect(command.type).toBe("
|
|
1178
|
+
expect(command.type).toBe("run/steer");
|
|
1184
1179
|
const texts = command.message.parts.map((p: any) => p.text);
|
|
1185
|
-
expect(texts
|
|
1186
|
-
expect(texts[
|
|
1187
|
-
expect(texts[
|
|
1180
|
+
expect(texts).toHaveLength(1);
|
|
1181
|
+
expect(texts[0]).toContain("see file");
|
|
1182
|
+
expect(texts[0]).toContain("attached body");
|
|
1183
|
+
expect(texts[0]).toContain("notes.txt");
|
|
1188
1184
|
});
|
|
1189
1185
|
|
|
1190
|
-
it("edits send the edited message
|
|
1186
|
+
it("edits send run/edit with the edited message as sourceId", async () => {
|
|
1191
1187
|
const fetchMock = installFetch();
|
|
1192
1188
|
const { aui } = mountThread({ capabilities: { edit: true } });
|
|
1193
1189
|
|
|
@@ -1212,13 +1208,16 @@ describe("AssistantTransportThread", () => {
|
|
|
1212
1208
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
1213
1209
|
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
1214
1210
|
{
|
|
1215
|
-
type: "
|
|
1216
|
-
message: {
|
|
1217
|
-
|
|
1211
|
+
type: "run/edit",
|
|
1212
|
+
message: {
|
|
1213
|
+
id: expect.any(String),
|
|
1214
|
+
role: "user",
|
|
1215
|
+
parts: [{ type: "text", text: "edited" }],
|
|
1216
|
+
},
|
|
1218
1217
|
sourceId: "u2",
|
|
1219
1218
|
},
|
|
1220
1219
|
]);
|
|
1221
|
-
expect(fetchMock.requests[0]!.body
|
|
1220
|
+
expect("parentId" in fetchMock.requests[0]!.body).toBe(false);
|
|
1222
1221
|
});
|
|
1223
1222
|
|
|
1224
1223
|
it("composer sends carry the last message's id as parentId; the run consumes it", async () => {
|
|
@@ -1244,10 +1243,13 @@ describe("AssistantTransportThread", () => {
|
|
|
1244
1243
|
expect(body.parentId).toBe("a1");
|
|
1245
1244
|
expect(body.commands).toEqual([
|
|
1246
1245
|
{
|
|
1247
|
-
type: "
|
|
1248
|
-
message: {
|
|
1249
|
-
|
|
1250
|
-
|
|
1246
|
+
type: "run/steer",
|
|
1247
|
+
message: {
|
|
1248
|
+
id: expect.any(String),
|
|
1249
|
+
role: "user",
|
|
1250
|
+
parts: [{ type: "text", text: "again" }],
|
|
1251
|
+
},
|
|
1252
|
+
anchorMessageId: "a1",
|
|
1251
1253
|
},
|
|
1252
1254
|
]);
|
|
1253
1255
|
|
|
@@ -1306,7 +1308,7 @@ describe("AssistantTransportThread", () => {
|
|
|
1306
1308
|
servers[1]!.done();
|
|
1307
1309
|
});
|
|
1308
1310
|
|
|
1309
|
-
it("executes frontend tools and posts
|
|
1311
|
+
it("executes frontend tools and posts run/input in a follow-up run", async () => {
|
|
1310
1312
|
const fetchMock = installFetch();
|
|
1311
1313
|
const servers: StreamResponse[] = [];
|
|
1312
1314
|
fetchMock.setHandler(() => {
|
|
@@ -1353,11 +1355,9 @@ describe("AssistantTransportThread", () => {
|
|
|
1353
1355
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
1354
1356
|
expect(fetchMock.requests[1]!.body.commands).toEqual([
|
|
1355
1357
|
{
|
|
1356
|
-
type: "
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
result: { probed: true },
|
|
1360
|
-
isError: false,
|
|
1358
|
+
type: "run/input",
|
|
1359
|
+
requestId: "req-tc1",
|
|
1360
|
+
response: { output: { probed: true } },
|
|
1361
1361
|
},
|
|
1362
1362
|
]);
|
|
1363
1363
|
servers[1]!.done();
|
|
@@ -1414,11 +1414,9 @@ describe("AssistantTransportThread", () => {
|
|
|
1414
1414
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
1415
1415
|
expect(fetchMock.requests[1]!.body.commands).toEqual([
|
|
1416
1416
|
{
|
|
1417
|
-
type: "
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
result: { answer: "yes" },
|
|
1421
|
-
isError: false,
|
|
1417
|
+
type: "run/input",
|
|
1418
|
+
requestId: "req-tc1",
|
|
1419
|
+
response: { output: { answer: "yes" } },
|
|
1422
1420
|
},
|
|
1423
1421
|
]);
|
|
1424
1422
|
servers[1]!.done();
|
|
@@ -1463,11 +1461,9 @@ describe("AssistantTransportThread", () => {
|
|
|
1463
1461
|
|
|
1464
1462
|
await waitFor(() =>
|
|
1465
1463
|
expect(allCommands(fetchMock.requests)).toContainEqual({
|
|
1466
|
-
type: "
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
result: "Tool execution was cancelled.",
|
|
1470
|
-
isError: true,
|
|
1464
|
+
type: "run/input",
|
|
1465
|
+
requestId: "req-tc1",
|
|
1466
|
+
response: { output: "Tool execution was cancelled.", isError: true },
|
|
1471
1467
|
}),
|
|
1472
1468
|
);
|
|
1473
1469
|
servers.at(-1)!.done();
|
|
@@ -1515,20 +1511,18 @@ describe("AssistantTransportThread", () => {
|
|
|
1515
1511
|
await waitFor(() => {
|
|
1516
1512
|
const commands = allCommands(fetchMock.requests);
|
|
1517
1513
|
expect(commands).toContainEqual({
|
|
1518
|
-
type: "
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
result: "Tool execution was cancelled.",
|
|
1522
|
-
isError: true,
|
|
1514
|
+
type: "run/input",
|
|
1515
|
+
requestId: "req-tc1",
|
|
1516
|
+
response: { output: "Tool execution was cancelled.", isError: true },
|
|
1523
1517
|
});
|
|
1524
1518
|
expect(commands).toContainEqual({
|
|
1525
|
-
type: "
|
|
1519
|
+
type: "run/steer",
|
|
1526
1520
|
message: {
|
|
1521
|
+
id: expect.any(String),
|
|
1527
1522
|
role: "user",
|
|
1528
1523
|
parts: [{ type: "text", text: "next question" }],
|
|
1529
1524
|
},
|
|
1530
|
-
|
|
1531
|
-
sourceId: null,
|
|
1525
|
+
anchorMessageId: "a-tool",
|
|
1532
1526
|
});
|
|
1533
1527
|
});
|
|
1534
1528
|
await waitFor(() => expect(metas.at(-1)!.toolStatuses).toEqual({}));
|
|
@@ -1960,20 +1954,31 @@ describe("reload", () => {
|
|
|
1960
1954
|
);
|
|
1961
1955
|
};
|
|
1962
1956
|
|
|
1963
|
-
it("regenerates
|
|
1957
|
+
it("regenerates via a run/reload command targeting the assistant message", async () => {
|
|
1964
1958
|
const fetchMock = installFetch();
|
|
1965
1959
|
const { aui } = mountThread();
|
|
1966
1960
|
await completeFirstTurn(fetchMock, aui);
|
|
1967
1961
|
|
|
1968
1962
|
aui().thread().message({ id: "m2" }).reload();
|
|
1969
1963
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
1970
|
-
expect(fetchMock.requests[1]!.body.commands).toEqual([
|
|
1971
|
-
|
|
1964
|
+
expect(fetchMock.requests[1]!.body.commands).toEqual([
|
|
1965
|
+
{
|
|
1966
|
+
type: "run/reload",
|
|
1967
|
+
sourceId: "m2",
|
|
1968
|
+
messageId: expect.stringMatching(/^[0-9a-f-]{36}$/),
|
|
1969
|
+
},
|
|
1970
|
+
]);
|
|
1971
|
+
expect("parentId" in fetchMock.requests[1]!.body).toBe(false);
|
|
1972
1972
|
});
|
|
1973
1973
|
|
|
1974
|
-
it("
|
|
1974
|
+
it("surfaces the error and recovers when the reload run fails", async () => {
|
|
1975
|
+
const errors: string[] = [];
|
|
1975
1976
|
const fetchMock = installFetch();
|
|
1976
|
-
const { aui } = mountThread(
|
|
1977
|
+
const { aui } = mountThread({
|
|
1978
|
+
onError: (error) => {
|
|
1979
|
+
errors.push(error.message);
|
|
1980
|
+
},
|
|
1981
|
+
});
|
|
1977
1982
|
await completeFirstTurn(fetchMock, aui);
|
|
1978
1983
|
|
|
1979
1984
|
fetchMock.setHandler(() => new Response(null, { status: 500 }));
|
|
@@ -1982,21 +1987,22 @@ describe("reload", () => {
|
|
|
1982
1987
|
await waitFor(() =>
|
|
1983
1988
|
expect(aui().thread().getState().isRunning).toBe(false),
|
|
1984
1989
|
);
|
|
1985
|
-
expect(
|
|
1990
|
+
await waitFor(() => expect(errors).toHaveLength(1));
|
|
1986
1991
|
|
|
1987
1992
|
fetchMock.setHandler(() => {
|
|
1988
1993
|
const server = createStreamResponse();
|
|
1989
1994
|
server.done();
|
|
1990
1995
|
return server.response;
|
|
1991
1996
|
});
|
|
1992
|
-
|
|
1993
|
-
type: "message/add",
|
|
1994
|
-
message: { role: "user", parts: [{ type: "text", text: "retry" }] },
|
|
1995
|
-
parentId: null,
|
|
1996
|
-
sourceId: null,
|
|
1997
|
-
});
|
|
1997
|
+
aui().thread().message({ id: "m2" }).reload();
|
|
1998
1998
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(3));
|
|
1999
|
-
expect(fetchMock.requests[2]!.body.
|
|
1999
|
+
expect(fetchMock.requests[2]!.body.commands).toEqual([
|
|
2000
|
+
{
|
|
2001
|
+
type: "run/reload",
|
|
2002
|
+
sourceId: "m2",
|
|
2003
|
+
messageId: expect.stringMatching(/^[0-9a-f-]{36}$/),
|
|
2004
|
+
},
|
|
2005
|
+
]);
|
|
2000
2006
|
});
|
|
2001
2007
|
});
|
|
2002
2008
|
|
|
@@ -2042,7 +2048,7 @@ describe("message queue", () => {
|
|
|
2042
2048
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
2043
2049
|
expect(fetchMock.requests[1]!.body.commands).toMatchObject([
|
|
2044
2050
|
{
|
|
2045
|
-
type: "
|
|
2051
|
+
type: "run/steer",
|
|
2046
2052
|
message: { role: "user", parts: [{ type: "text", text: "second" }] },
|
|
2047
2053
|
},
|
|
2048
2054
|
]);
|
|
@@ -2101,7 +2107,7 @@ describe("message queue", () => {
|
|
|
2101
2107
|
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
2102
2108
|
expect(fetchMock.requests[1]!.body.commands).toMatchObject([
|
|
2103
2109
|
{
|
|
2104
|
-
type: "
|
|
2110
|
+
type: "run/steer",
|
|
2105
2111
|
message: { parts: [{ type: "text", text: "second" }] },
|
|
2106
2112
|
},
|
|
2107
2113
|
]);
|
|
@@ -2138,3 +2144,520 @@ describe("message queue", () => {
|
|
|
2138
2144
|
);
|
|
2139
2145
|
});
|
|
2140
2146
|
});
|
|
2147
|
+
|
|
2148
|
+
describe("run/continue", () => {
|
|
2149
|
+
it("resumes via a run/continue command under capabilities.continue", async () => {
|
|
2150
|
+
const fetchMock = installFetch();
|
|
2151
|
+
const { aui } = mountThread({ capabilities: { continue: true } });
|
|
2152
|
+
|
|
2153
|
+
aui().thread().resumeRun({ parentId: null });
|
|
2154
|
+
|
|
2155
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2156
|
+
expect(fetchMock.requests[0]!.url).toBe("https://example.com/api");
|
|
2157
|
+
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
2158
|
+
{ type: "run/continue" },
|
|
2159
|
+
]);
|
|
2160
|
+
});
|
|
2161
|
+
});
|
|
2162
|
+
|
|
2163
|
+
describe("server queue projection", () => {
|
|
2164
|
+
type QueueState = TestState & {
|
|
2165
|
+
queue?: { id: string; parts: { type: "text"; text: string }[] }[];
|
|
2166
|
+
steerQueue?: { id: string; parts: { type: "text"; text: string }[] }[];
|
|
2167
|
+
};
|
|
2168
|
+
|
|
2169
|
+
const queueConverter: typeof defaultConverter = (state, meta) => ({
|
|
2170
|
+
...defaultConverter(state, meta),
|
|
2171
|
+
queue: (state as QueueState).queue ?? [],
|
|
2172
|
+
steerQueue: (state as QueueState).steerQueue ?? [],
|
|
2173
|
+
});
|
|
2174
|
+
|
|
2175
|
+
const textEntry = (id: string, text: string) => ({
|
|
2176
|
+
id,
|
|
2177
|
+
parts: [{ type: "text" as const, text }],
|
|
2178
|
+
});
|
|
2179
|
+
|
|
2180
|
+
it("routes appends through run/enqueue and projects the lanes steer-first", async () => {
|
|
2181
|
+
const fetchMock = installFetch();
|
|
2182
|
+
const { aui } = mountThread({
|
|
2183
|
+
converter: queueConverter,
|
|
2184
|
+
initialState: {
|
|
2185
|
+
messages: [],
|
|
2186
|
+
queue: [textEntry("q1", "first")],
|
|
2187
|
+
steerQueue: [textEntry("s1", "urgent")],
|
|
2188
|
+
} as QueueState,
|
|
2189
|
+
});
|
|
2190
|
+
|
|
2191
|
+
await waitFor(() =>
|
|
2192
|
+
expect(aui().thread().composer().getState().queue).toMatchObject([
|
|
2193
|
+
{ id: "s1", prompt: "urgent" },
|
|
2194
|
+
{ id: "q1", prompt: "first" },
|
|
2195
|
+
]),
|
|
2196
|
+
);
|
|
2197
|
+
|
|
2198
|
+
aui().thread().append("hello");
|
|
2199
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2200
|
+
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
2201
|
+
{
|
|
2202
|
+
type: "run/enqueue",
|
|
2203
|
+
message: {
|
|
2204
|
+
id: expect.any(String),
|
|
2205
|
+
role: "user",
|
|
2206
|
+
parts: [{ type: "text", text: "hello" }],
|
|
2207
|
+
},
|
|
2208
|
+
anchorMessageId: null,
|
|
2209
|
+
},
|
|
2210
|
+
]);
|
|
2211
|
+
});
|
|
2212
|
+
|
|
2213
|
+
it("steers when the composer sends mid-run", async () => {
|
|
2214
|
+
const fetchMock = installFetch();
|
|
2215
|
+
const servers: StreamResponse[] = [];
|
|
2216
|
+
fetchMock.setHandler(() => {
|
|
2217
|
+
const server = createStreamResponse();
|
|
2218
|
+
servers.push(server);
|
|
2219
|
+
return server.response;
|
|
2220
|
+
});
|
|
2221
|
+
|
|
2222
|
+
const { aui } = mountThread({ converter: queueConverter });
|
|
2223
|
+
|
|
2224
|
+
aui().thread().append("hello");
|
|
2225
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2226
|
+
await waitFor(() => expect(aui().thread().getState().isRunning).toBe(true));
|
|
2227
|
+
|
|
2228
|
+
aui().thread().composer().setText("steer me");
|
|
2229
|
+
await waitFor(() =>
|
|
2230
|
+
expect(aui().thread().getState().composer.text).toBe("steer me"),
|
|
2231
|
+
);
|
|
2232
|
+
aui().thread().composer().send();
|
|
2233
|
+
|
|
2234
|
+
servers[0]!.done();
|
|
2235
|
+
await waitFor(() =>
|
|
2236
|
+
expect(allCommands(fetchMock.requests)).toContainEqual({
|
|
2237
|
+
type: "run/steer",
|
|
2238
|
+
message: {
|
|
2239
|
+
id: expect.any(String),
|
|
2240
|
+
role: "user",
|
|
2241
|
+
parts: [{ type: "text", text: "steer me" }],
|
|
2242
|
+
},
|
|
2243
|
+
anchorMessageId: null,
|
|
2244
|
+
}),
|
|
2245
|
+
);
|
|
2246
|
+
});
|
|
2247
|
+
|
|
2248
|
+
it("sends item controls as run/* commands with placement", async () => {
|
|
2249
|
+
const fetchMock = installFetch();
|
|
2250
|
+
const { aui } = mountThread({
|
|
2251
|
+
converter: queueConverter,
|
|
2252
|
+
initialState: {
|
|
2253
|
+
messages: [],
|
|
2254
|
+
queue: [textEntry("q1", "first"), textEntry("q2", "second")],
|
|
2255
|
+
steerQueue: [textEntry("s1", "urgent")],
|
|
2256
|
+
} as QueueState,
|
|
2257
|
+
});
|
|
2258
|
+
|
|
2259
|
+
await waitFor(() =>
|
|
2260
|
+
expect(aui().thread().composer().getState().queue).toHaveLength(3),
|
|
2261
|
+
);
|
|
2262
|
+
|
|
2263
|
+
aui().thread().composer().queueItem({ id: "q1" }).steer();
|
|
2264
|
+
await waitFor(() =>
|
|
2265
|
+
expect(allCommands(fetchMock.requests)).toContainEqual({
|
|
2266
|
+
type: "run/steer",
|
|
2267
|
+
messageId: "q1",
|
|
2268
|
+
insertAfter: null,
|
|
2269
|
+
}),
|
|
2270
|
+
);
|
|
2271
|
+
|
|
2272
|
+
aui()
|
|
2273
|
+
.thread()
|
|
2274
|
+
.composer()
|
|
2275
|
+
.queueItem({ id: "q2" })
|
|
2276
|
+
.move({ lane: "queue", insertBefore: "q1" });
|
|
2277
|
+
await waitFor(() =>
|
|
2278
|
+
expect(allCommands(fetchMock.requests)).toContainEqual({
|
|
2279
|
+
type: "run/enqueue",
|
|
2280
|
+
messageId: "q2",
|
|
2281
|
+
insertBefore: "q1",
|
|
2282
|
+
}),
|
|
2283
|
+
);
|
|
2284
|
+
|
|
2285
|
+
aui().thread().composer().queueItem({ id: "s1" }).remove();
|
|
2286
|
+
await waitFor(() =>
|
|
2287
|
+
expect(allCommands(fetchMock.requests)).toContainEqual({
|
|
2288
|
+
type: "run/dequeue",
|
|
2289
|
+
messageId: "s1",
|
|
2290
|
+
}),
|
|
2291
|
+
);
|
|
2292
|
+
});
|
|
2293
|
+
|
|
2294
|
+
it("throws when steerQueue is projected without queue", () => {
|
|
2295
|
+
installFetch();
|
|
2296
|
+
expect(() =>
|
|
2297
|
+
mountThread({
|
|
2298
|
+
converter: (state, meta) => ({
|
|
2299
|
+
...defaultConverter(state, meta),
|
|
2300
|
+
steerQueue: [],
|
|
2301
|
+
}),
|
|
2302
|
+
}),
|
|
2303
|
+
).toThrow(
|
|
2304
|
+
"statewire: the converter's steerQueue projection requires the queue projection",
|
|
2305
|
+
);
|
|
2306
|
+
});
|
|
2307
|
+
|
|
2308
|
+
it("throws when the queue projection is combined with capabilities.queue", () => {
|
|
2309
|
+
installFetch();
|
|
2310
|
+
expect(() =>
|
|
2311
|
+
mountThread({
|
|
2312
|
+
converter: queueConverter,
|
|
2313
|
+
capabilities: { queue: true },
|
|
2314
|
+
}),
|
|
2315
|
+
).toThrow(
|
|
2316
|
+
"statewire: the converter's queue projection excludes capabilities.queue",
|
|
2317
|
+
);
|
|
2318
|
+
});
|
|
2319
|
+
|
|
2320
|
+
it("throws when the queue projection appears after the first convert", () => {
|
|
2321
|
+
installFetch();
|
|
2322
|
+
const withQueue: typeof defaultConverter = (state, meta) => ({
|
|
2323
|
+
...defaultConverter(state, meta),
|
|
2324
|
+
queue: [],
|
|
2325
|
+
});
|
|
2326
|
+
|
|
2327
|
+
const captured: { setConverter?: (c: typeof defaultConverter) => void } =
|
|
2328
|
+
{};
|
|
2329
|
+
const App: FC = () => {
|
|
2330
|
+
const [converter, setConverter] = useState(() => defaultConverter);
|
|
2331
|
+
captured.setConverter = (c) => setConverter(() => c);
|
|
2332
|
+
const aui = useAui({
|
|
2333
|
+
thread: AssistantTransportThread<TestState>({
|
|
2334
|
+
initialState: INITIAL_STATE,
|
|
2335
|
+
api: "https://example.com/api",
|
|
2336
|
+
headers: {},
|
|
2337
|
+
converter,
|
|
2338
|
+
protocol: "assistant-transport",
|
|
2339
|
+
}),
|
|
2340
|
+
});
|
|
2341
|
+
return <AuiProvider value={aui}>{null}</AuiProvider>;
|
|
2342
|
+
};
|
|
2343
|
+
const { unmount } = render(<App />);
|
|
2344
|
+
|
|
2345
|
+
expect(() => act(() => captured.setConverter!(withQueue))).toThrow(
|
|
2346
|
+
/queue projection must be stable/,
|
|
2347
|
+
);
|
|
2348
|
+
unmount();
|
|
2349
|
+
});
|
|
2350
|
+
});
|
|
2351
|
+
|
|
2352
|
+
describe("input requests", () => {
|
|
2353
|
+
type InputState = TestState & {
|
|
2354
|
+
toolCall?: { id: string; name: string; argsText: string };
|
|
2355
|
+
inputRequests?: InputRequestState[];
|
|
2356
|
+
};
|
|
2357
|
+
|
|
2358
|
+
const requestConverter = (approval?: {
|
|
2359
|
+
id: string;
|
|
2360
|
+
}): typeof defaultConverter => {
|
|
2361
|
+
const inner = toolCallConverter([]);
|
|
2362
|
+
return (state, meta) => {
|
|
2363
|
+
const converted = inner(state, meta);
|
|
2364
|
+
return {
|
|
2365
|
+
...converted,
|
|
2366
|
+
messages: approval
|
|
2367
|
+
? converted.messages.map((m) =>
|
|
2368
|
+
m.id === "a-tool"
|
|
2369
|
+
? ({
|
|
2370
|
+
...m,
|
|
2371
|
+
content: [{ ...(m.content[0] as object), approval }],
|
|
2372
|
+
} as unknown as typeof m)
|
|
2373
|
+
: m,
|
|
2374
|
+
)
|
|
2375
|
+
: converted.messages,
|
|
2376
|
+
inputRequests: (state as InputState).inputRequests ?? [],
|
|
2377
|
+
};
|
|
2378
|
+
};
|
|
2379
|
+
};
|
|
2380
|
+
|
|
2381
|
+
const toolCallPart = (aui: () => ReturnType<typeof useAui>) =>
|
|
2382
|
+
aui().thread().message({ id: "a-tool" }).part({ toolCallId: "tc1" });
|
|
2383
|
+
|
|
2384
|
+
it("routes addToolResult through run/input when a pending tool-call request matches", async () => {
|
|
2385
|
+
const fetchMock = installFetch();
|
|
2386
|
+
const { aui } = mountThread({
|
|
2387
|
+
converter: requestConverter(),
|
|
2388
|
+
initialState: {
|
|
2389
|
+
messages: [],
|
|
2390
|
+
toolCall: { id: "tc1", name: "probe_tool", argsText: "{}" },
|
|
2391
|
+
inputRequests: [
|
|
2392
|
+
{ type: "tool-call", id: "req1", toolCallId: "tc1", response: null },
|
|
2393
|
+
],
|
|
2394
|
+
} as InputState,
|
|
2395
|
+
});
|
|
2396
|
+
|
|
2397
|
+
await waitFor(() =>
|
|
2398
|
+
expect(aui().thread().getState().messages).toHaveLength(1),
|
|
2399
|
+
);
|
|
2400
|
+
toolCallPart(aui).addToolResult({ ok: true });
|
|
2401
|
+
|
|
2402
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2403
|
+
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
2404
|
+
{
|
|
2405
|
+
type: "run/input",
|
|
2406
|
+
requestId: "req1",
|
|
2407
|
+
response: { output: { ok: true } },
|
|
2408
|
+
},
|
|
2409
|
+
]);
|
|
2410
|
+
});
|
|
2411
|
+
|
|
2412
|
+
it("passes isError and artifact through the run/input response", async () => {
|
|
2413
|
+
const fetchMock = installFetch();
|
|
2414
|
+
const { aui } = mountThread({
|
|
2415
|
+
converter: requestConverter(),
|
|
2416
|
+
initialState: {
|
|
2417
|
+
messages: [],
|
|
2418
|
+
toolCall: { id: "tc1", name: "probe_tool", argsText: "{}" },
|
|
2419
|
+
inputRequests: [
|
|
2420
|
+
{ type: "tool-call", id: "req1", toolCallId: "tc1", response: null },
|
|
2421
|
+
],
|
|
2422
|
+
} as InputState,
|
|
2423
|
+
});
|
|
2424
|
+
|
|
2425
|
+
await waitFor(() =>
|
|
2426
|
+
expect(aui().thread().getState().messages).toHaveLength(1),
|
|
2427
|
+
);
|
|
2428
|
+
toolCallPart(aui).addToolResult(
|
|
2429
|
+
new ToolResponse({
|
|
2430
|
+
result: { reason: "boom" },
|
|
2431
|
+
isError: true,
|
|
2432
|
+
artifact: { log: "trace" },
|
|
2433
|
+
}),
|
|
2434
|
+
);
|
|
2435
|
+
|
|
2436
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2437
|
+
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
2438
|
+
{
|
|
2439
|
+
type: "run/input",
|
|
2440
|
+
requestId: "req1",
|
|
2441
|
+
response: {
|
|
2442
|
+
output: { reason: "boom" },
|
|
2443
|
+
isError: true,
|
|
2444
|
+
artifact: { log: "trace" },
|
|
2445
|
+
},
|
|
2446
|
+
},
|
|
2447
|
+
]);
|
|
2448
|
+
});
|
|
2449
|
+
|
|
2450
|
+
it("throws without a matching pending request", async () => {
|
|
2451
|
+
const fetchMock = installFetch();
|
|
2452
|
+
const { aui } = mountThread({
|
|
2453
|
+
converter: requestConverter(),
|
|
2454
|
+
initialState: {
|
|
2455
|
+
messages: [],
|
|
2456
|
+
toolCall: { id: "tc1", name: "probe_tool", argsText: "{}" },
|
|
2457
|
+
inputRequests: [
|
|
2458
|
+
{
|
|
2459
|
+
type: "tool-call",
|
|
2460
|
+
id: "req1",
|
|
2461
|
+
toolCallId: "other",
|
|
2462
|
+
response: null,
|
|
2463
|
+
},
|
|
2464
|
+
],
|
|
2465
|
+
} as InputState,
|
|
2466
|
+
});
|
|
2467
|
+
|
|
2468
|
+
await waitFor(() =>
|
|
2469
|
+
expect(aui().thread().getState().messages).toHaveLength(1),
|
|
2470
|
+
);
|
|
2471
|
+
expect(() => toolCallPart(aui).addToolResult({ ok: true })).toThrow(
|
|
2472
|
+
'statewire: no tool-call input request for tool call "tc1"',
|
|
2473
|
+
);
|
|
2474
|
+
|
|
2475
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
2476
|
+
expect(fetchMock.requests).toHaveLength(0);
|
|
2477
|
+
});
|
|
2478
|
+
|
|
2479
|
+
it("throws on answering a request that already carries a response", async () => {
|
|
2480
|
+
installFetch();
|
|
2481
|
+
const { aui } = mountThread({
|
|
2482
|
+
converter: requestConverter(),
|
|
2483
|
+
initialState: {
|
|
2484
|
+
messages: [],
|
|
2485
|
+
toolCall: { id: "tc1", name: "probe_tool", argsText: "{}" },
|
|
2486
|
+
inputRequests: [
|
|
2487
|
+
{
|
|
2488
|
+
type: "tool-call",
|
|
2489
|
+
id: "req1",
|
|
2490
|
+
toolCallId: "tc1",
|
|
2491
|
+
response: { output: "done" },
|
|
2492
|
+
},
|
|
2493
|
+
],
|
|
2494
|
+
} as InputState,
|
|
2495
|
+
});
|
|
2496
|
+
|
|
2497
|
+
await waitFor(() =>
|
|
2498
|
+
expect(aui().thread().getState().messages).toHaveLength(1),
|
|
2499
|
+
);
|
|
2500
|
+
expect(() => toolCallPart(aui).addToolResult({ ok: true })).toThrow(
|
|
2501
|
+
'statewire: input request "req1" is already answered',
|
|
2502
|
+
);
|
|
2503
|
+
});
|
|
2504
|
+
|
|
2505
|
+
it("answers tool approvals via run/input decisions", async () => {
|
|
2506
|
+
const fetchMock = installFetch();
|
|
2507
|
+
const { aui } = mountThread({
|
|
2508
|
+
converter: requestConverter({ id: "req2" }),
|
|
2509
|
+
initialState: {
|
|
2510
|
+
messages: [],
|
|
2511
|
+
toolCall: { id: "tc1", name: "probe_tool", argsText: "{}" },
|
|
2512
|
+
inputRequests: [
|
|
2513
|
+
{
|
|
2514
|
+
type: "tool-approval",
|
|
2515
|
+
id: "req2",
|
|
2516
|
+
toolCallId: "tc1",
|
|
2517
|
+
response: null,
|
|
2518
|
+
},
|
|
2519
|
+
],
|
|
2520
|
+
} as InputState,
|
|
2521
|
+
});
|
|
2522
|
+
|
|
2523
|
+
await waitFor(() =>
|
|
2524
|
+
expect(aui().thread().getState().messages).toHaveLength(1),
|
|
2525
|
+
);
|
|
2526
|
+
toolCallPart(aui).respondToToolApproval({
|
|
2527
|
+
approved: false,
|
|
2528
|
+
reason: "not allowed",
|
|
2529
|
+
});
|
|
2530
|
+
|
|
2531
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2532
|
+
expect(fetchMock.requests[0]!.body.commands).toEqual([
|
|
2533
|
+
{
|
|
2534
|
+
type: "run/input",
|
|
2535
|
+
requestId: "req2",
|
|
2536
|
+
response: { decision: "reject", message: "not allowed" },
|
|
2537
|
+
},
|
|
2538
|
+
]);
|
|
2539
|
+
});
|
|
2540
|
+
|
|
2541
|
+
it("posts frontend tool results as run/input when the host parks a matching request", async () => {
|
|
2542
|
+
const fetchMock = installFetch();
|
|
2543
|
+
const servers: StreamResponse[] = [];
|
|
2544
|
+
fetchMock.setHandler(() => {
|
|
2545
|
+
const server = createStreamResponse();
|
|
2546
|
+
servers.push(server);
|
|
2547
|
+
return server.response;
|
|
2548
|
+
});
|
|
2549
|
+
|
|
2550
|
+
const { aui } = mountThread({ converter: requestConverter() });
|
|
2551
|
+
aui()
|
|
2552
|
+
.modelContext()
|
|
2553
|
+
.register({
|
|
2554
|
+
getModelContext: () => ({
|
|
2555
|
+
tools: {
|
|
2556
|
+
probe_tool: {
|
|
2557
|
+
parameters: { type: "object", properties: {} },
|
|
2558
|
+
execute: async () => ({ probed: true }),
|
|
2559
|
+
},
|
|
2560
|
+
},
|
|
2561
|
+
}),
|
|
2562
|
+
});
|
|
2563
|
+
|
|
2564
|
+
sendCommand(aui, { type: "custom-a" } as never);
|
|
2565
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2566
|
+
servers[0]!.emit({
|
|
2567
|
+
type: "update-state",
|
|
2568
|
+
operations: [
|
|
2569
|
+
{
|
|
2570
|
+
type: "set",
|
|
2571
|
+
path: ["toolCall"],
|
|
2572
|
+
value: { id: "tc1", name: "probe_tool", argsText: '{"x":1}' },
|
|
2573
|
+
},
|
|
2574
|
+
{
|
|
2575
|
+
type: "set",
|
|
2576
|
+
path: ["inputRequests"],
|
|
2577
|
+
value: [
|
|
2578
|
+
{
|
|
2579
|
+
type: "tool-call",
|
|
2580
|
+
id: "req1",
|
|
2581
|
+
toolCallId: "tc1",
|
|
2582
|
+
response: null,
|
|
2583
|
+
},
|
|
2584
|
+
],
|
|
2585
|
+
},
|
|
2586
|
+
],
|
|
2587
|
+
});
|
|
2588
|
+
servers[0]!.done();
|
|
2589
|
+
|
|
2590
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
2591
|
+
expect(fetchMock.requests[1]!.body.commands).toEqual([
|
|
2592
|
+
{
|
|
2593
|
+
type: "run/input",
|
|
2594
|
+
requestId: "req1",
|
|
2595
|
+
response: { output: { probed: true } },
|
|
2596
|
+
},
|
|
2597
|
+
]);
|
|
2598
|
+
servers[1]!.done();
|
|
2599
|
+
});
|
|
2600
|
+
|
|
2601
|
+
it("surfaces custom request types through extras.inputRequests", async () => {
|
|
2602
|
+
installFetch();
|
|
2603
|
+
const { aui } = mountThread({
|
|
2604
|
+
converter: requestConverter(),
|
|
2605
|
+
initialState: {
|
|
2606
|
+
messages: [],
|
|
2607
|
+
inputRequests: [
|
|
2608
|
+
{
|
|
2609
|
+
type: "human-review",
|
|
2610
|
+
id: "req9",
|
|
2611
|
+
prompt: "check this",
|
|
2612
|
+
response: null,
|
|
2613
|
+
},
|
|
2614
|
+
],
|
|
2615
|
+
} as InputState,
|
|
2616
|
+
});
|
|
2617
|
+
|
|
2618
|
+
await waitFor(() =>
|
|
2619
|
+
expect(
|
|
2620
|
+
asAssistantTransportExtras(aui().thread().getState().extras)
|
|
2621
|
+
.inputRequests,
|
|
2622
|
+
).toEqual([
|
|
2623
|
+
{
|
|
2624
|
+
type: "human-review",
|
|
2625
|
+
id: "req9",
|
|
2626
|
+
prompt: "check this",
|
|
2627
|
+
response: null,
|
|
2628
|
+
},
|
|
2629
|
+
]),
|
|
2630
|
+
);
|
|
2631
|
+
});
|
|
2632
|
+
|
|
2633
|
+
it("throws when the inputRequests projection appears after the first convert", () => {
|
|
2634
|
+
installFetch();
|
|
2635
|
+
const withRequests: typeof defaultConverter = (state, meta) => ({
|
|
2636
|
+
...defaultConverter(state, meta),
|
|
2637
|
+
inputRequests: [],
|
|
2638
|
+
});
|
|
2639
|
+
|
|
2640
|
+
const captured: { setConverter?: (c: typeof defaultConverter) => void } =
|
|
2641
|
+
{};
|
|
2642
|
+
const App: FC = () => {
|
|
2643
|
+
const [converter, setConverter] = useState(() => defaultConverter);
|
|
2644
|
+
captured.setConverter = (c) => setConverter(() => c);
|
|
2645
|
+
const aui = useAui({
|
|
2646
|
+
thread: AssistantTransportThread<TestState>({
|
|
2647
|
+
initialState: INITIAL_STATE,
|
|
2648
|
+
api: "https://example.com/api",
|
|
2649
|
+
headers: {},
|
|
2650
|
+
converter,
|
|
2651
|
+
protocol: "assistant-transport",
|
|
2652
|
+
}),
|
|
2653
|
+
});
|
|
2654
|
+
return <AuiProvider value={aui}>{null}</AuiProvider>;
|
|
2655
|
+
};
|
|
2656
|
+
const { unmount } = render(<App />);
|
|
2657
|
+
|
|
2658
|
+
expect(() => act(() => captured.setConverter!(withRequests))).toThrow(
|
|
2659
|
+
/inputRequests projection must be stable/,
|
|
2660
|
+
);
|
|
2661
|
+
unmount();
|
|
2662
|
+
});
|
|
2663
|
+
});
|