@assistant-ui/react-statewire 0.1.0 → 0.2.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.
@@ -56,7 +56,7 @@ describe("AssistantTransportThread", () => {
56
56
  expect(fetchMock.requests[0]!.url).toBe("https://example.com/api");
57
57
  expect(fetchMock.requests[0]!.body.commands).toEqual([
58
58
  {
59
- type: "add-message",
59
+ type: "message/add",
60
60
  message: { role: "user", parts: [{ type: "text", text: "hello" }] },
61
61
  parentId: null,
62
62
  sourceId: null,
@@ -147,7 +147,7 @@ describe("AssistantTransportThread", () => {
147
147
  const meta = metas.at(-1)!;
148
148
  expect(meta.isSending).toBe(true);
149
149
  expect(meta.pendingCommands).toHaveLength(1);
150
- expect(meta.pendingCommands[0]).toMatchObject({ type: "add-message" });
150
+ expect(meta.pendingCommands[0]).toMatchObject({ type: "message/add" });
151
151
  });
152
152
 
153
153
  server.emit({
@@ -239,7 +239,7 @@ describe("AssistantTransportThread", () => {
239
239
  });
240
240
 
241
241
  sendCommand(aui, {
242
- type: "add-message",
242
+ type: "message/add",
243
243
  message: { role: "user", parts: [{ type: "text", text: "hello" }] },
244
244
  parentId: null,
245
245
  sourceId: null,
@@ -1126,11 +1126,11 @@ describe("AssistantTransportThread", () => {
1126
1126
  expect(aui().thread().getState().isRunning).toBe(false),
1127
1127
  );
1128
1128
 
1129
- // Give any stray add-tool-result dispatch a chance to fire.
1129
+ // Give any stray tool-result/add dispatch a chance to fire.
1130
1130
  await new Promise((resolve) => setTimeout(resolve, 20));
1131
1131
  expect(
1132
1132
  allCommands(fetchMock.requests).filter(
1133
- (c) => c.type === "add-tool-result",
1133
+ (c) => c.type === "tool-result/add",
1134
1134
  ),
1135
1135
  ).toEqual([]);
1136
1136
  });
@@ -1156,7 +1156,7 @@ describe("AssistantTransportThread", () => {
1156
1156
  );
1157
1157
  });
1158
1158
 
1159
- it("folds completed attachments into the add-message command (CompositeAttachmentAdapter)", async () => {
1159
+ it("folds completed attachments into the message/add command (CompositeAttachmentAdapter)", async () => {
1160
1160
  const fetchMock = installFetch();
1161
1161
  const { aui } = mountThread({
1162
1162
  adapters: {
@@ -1180,7 +1180,7 @@ describe("AssistantTransportThread", () => {
1180
1180
 
1181
1181
  await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
1182
1182
  const command = fetchMock.requests[0]!.body.commands[0];
1183
- expect(command.type).toBe("add-message");
1183
+ expect(command.type).toBe("message/add");
1184
1184
  const texts = command.message.parts.map((p: any) => p.text);
1185
1185
  expect(texts[0]).toBe("see file");
1186
1186
  expect(texts[1]).toContain("attached body");
@@ -1212,7 +1212,7 @@ describe("AssistantTransportThread", () => {
1212
1212
  await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
1213
1213
  expect(fetchMock.requests[0]!.body.commands).toEqual([
1214
1214
  {
1215
- type: "add-message",
1215
+ type: "message/add",
1216
1216
  message: { role: "user", parts: [{ type: "text", text: "edited" }] },
1217
1217
  parentId: "a1",
1218
1218
  sourceId: "u2",
@@ -1244,7 +1244,7 @@ describe("AssistantTransportThread", () => {
1244
1244
  expect(body.parentId).toBe("a1");
1245
1245
  expect(body.commands).toEqual([
1246
1246
  {
1247
- type: "add-message",
1247
+ type: "message/add",
1248
1248
  message: { role: "user", parts: [{ type: "text", text: "again" }] },
1249
1249
  parentId: "a1",
1250
1250
  sourceId: null,
@@ -1306,7 +1306,7 @@ describe("AssistantTransportThread", () => {
1306
1306
  servers[1]!.done();
1307
1307
  });
1308
1308
 
1309
- it("executes frontend tools and posts add-tool-result in a follow-up run", async () => {
1309
+ it("executes frontend tools and posts tool-result/add in a follow-up run", async () => {
1310
1310
  const fetchMock = installFetch();
1311
1311
  const servers: StreamResponse[] = [];
1312
1312
  fetchMock.setHandler(() => {
@@ -1353,7 +1353,7 @@ describe("AssistantTransportThread", () => {
1353
1353
  await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
1354
1354
  expect(fetchMock.requests[1]!.body.commands).toEqual([
1355
1355
  {
1356
- type: "add-tool-result",
1356
+ type: "tool-result/add",
1357
1357
  toolCallId: "tc1",
1358
1358
  toolName: "probe_tool",
1359
1359
  result: { probed: true },
@@ -1414,7 +1414,7 @@ describe("AssistantTransportThread", () => {
1414
1414
  await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
1415
1415
  expect(fetchMock.requests[1]!.body.commands).toEqual([
1416
1416
  {
1417
- type: "add-tool-result",
1417
+ type: "tool-result/add",
1418
1418
  toolCallId: "tc1",
1419
1419
  toolName: "probe_tool",
1420
1420
  result: { answer: "yes" },
@@ -1463,7 +1463,7 @@ describe("AssistantTransportThread", () => {
1463
1463
 
1464
1464
  await waitFor(() =>
1465
1465
  expect(allCommands(fetchMock.requests)).toContainEqual({
1466
- type: "add-tool-result",
1466
+ type: "tool-result/add",
1467
1467
  toolCallId: "tc1",
1468
1468
  toolName: "probe_tool",
1469
1469
  result: "Tool execution was cancelled.",
@@ -1515,14 +1515,14 @@ describe("AssistantTransportThread", () => {
1515
1515
  await waitFor(() => {
1516
1516
  const commands = allCommands(fetchMock.requests);
1517
1517
  expect(commands).toContainEqual({
1518
- type: "add-tool-result",
1518
+ type: "tool-result/add",
1519
1519
  toolCallId: "tc1",
1520
1520
  toolName: "probe_tool",
1521
1521
  result: "Tool execution was cancelled.",
1522
1522
  isError: true,
1523
1523
  });
1524
1524
  expect(commands).toContainEqual({
1525
- type: "add-message",
1525
+ type: "message/add",
1526
1526
  message: {
1527
1527
  role: "user",
1528
1528
  parts: [{ type: "text", text: "next question" }],
@@ -1990,7 +1990,7 @@ describe("reload", () => {
1990
1990
  return server.response;
1991
1991
  });
1992
1992
  sendCommand(aui, {
1993
- type: "add-message",
1993
+ type: "message/add",
1994
1994
  message: { role: "user", parts: [{ type: "text", text: "retry" }] },
1995
1995
  parentId: null,
1996
1996
  sourceId: null,
@@ -1999,3 +1999,142 @@ describe("reload", () => {
1999
1999
  expect(fetchMock.requests[2]!.body.parentId).toBe("m1");
2000
2000
  });
2001
2001
  });
2002
+
2003
+ describe("message queue", () => {
2004
+ it("buffers a send during a run and dispatches it once the run settles", async () => {
2005
+ const fetchMock = installFetch();
2006
+ const servers: StreamResponse[] = [];
2007
+ fetchMock.setHandler(() => {
2008
+ const server = createStreamResponse();
2009
+ servers.push(server);
2010
+ return server.response;
2011
+ });
2012
+
2013
+ const { aui } = mountThread({ capabilities: { queue: true } });
2014
+ aui().thread().append("first");
2015
+ await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
2016
+ await waitFor(() => expect(aui().thread().getState().isRunning).toBe(true));
2017
+ expect(aui().thread().getState().capabilities.queue).toBe(true);
2018
+
2019
+ aui().thread().append("second");
2020
+ await waitFor(() =>
2021
+ expect(aui().thread().composer().getState().queue).toMatchObject([
2022
+ { prompt: "second" },
2023
+ ]),
2024
+ );
2025
+ expect(fetchMock.requests).toHaveLength(1);
2026
+
2027
+ servers[0]!.emit({
2028
+ type: "update-state",
2029
+ operations: [
2030
+ {
2031
+ type: "set",
2032
+ path: ["messages"],
2033
+ value: [
2034
+ { id: "m1", role: "user", text: "first" },
2035
+ { id: "m2", role: "assistant", text: "reply" },
2036
+ ],
2037
+ },
2038
+ ],
2039
+ });
2040
+ servers[0]!.done();
2041
+
2042
+ await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
2043
+ expect(fetchMock.requests[1]!.body.commands).toMatchObject([
2044
+ {
2045
+ type: "message/add",
2046
+ message: { role: "user", parts: [{ type: "text", text: "second" }] },
2047
+ },
2048
+ ]);
2049
+ // restamped at dispatch time: parented under the finished run's head
2050
+ expect(fetchMock.requests[1]!.body.parentId).toBe("m2");
2051
+ await waitFor(() =>
2052
+ expect(aui().thread().composer().getState().queue).toHaveLength(0),
2053
+ );
2054
+ });
2055
+
2056
+ it("holds the queue through the stop-settling window until the transport releases", async () => {
2057
+ const fetchMock = installFetch();
2058
+ const servers: StreamResponse[] = [];
2059
+ fetchMock.setHandler(() => {
2060
+ const server = createStreamResponse();
2061
+ servers.push(server);
2062
+ return server.response;
2063
+ });
2064
+
2065
+ type RunningState = TestState & { running?: boolean };
2066
+ const { aui } = mountThread({
2067
+ capabilities: { queue: true },
2068
+ converter: (state, meta) => ({
2069
+ ...defaultConverter(state, meta),
2070
+ isRunning: (state as RunningState).running ?? false,
2071
+ }),
2072
+ });
2073
+
2074
+ aui().thread().append("first");
2075
+ await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
2076
+ servers[0]!.emit({
2077
+ type: "update-state",
2078
+ operations: [{ type: "set", path: ["running"], value: true }],
2079
+ });
2080
+ await waitFor(() => expect(aui().thread().getState().isRunning).toBe(true));
2081
+
2082
+ aui().thread().append("second");
2083
+ await waitFor(() =>
2084
+ expect(aui().thread().composer().getState().queue).toHaveLength(1),
2085
+ );
2086
+
2087
+ // state says the run stopped, but the stream has not settled yet: the
2088
+ // queued message must not dispatch into this window
2089
+ servers[0]!.emit({
2090
+ type: "update-state",
2091
+ operations: [{ type: "set", path: ["running"], value: false }],
2092
+ });
2093
+ await waitFor(() =>
2094
+ expect(aui().thread().getState().isRunning).toBe(false),
2095
+ );
2096
+ expect(fetchMock.requests).toHaveLength(1);
2097
+ expect(aui().thread().composer().getState().queue).toHaveLength(1);
2098
+
2099
+ servers[0]!.done();
2100
+
2101
+ await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
2102
+ expect(fetchMock.requests[1]!.body.commands).toMatchObject([
2103
+ {
2104
+ type: "message/add",
2105
+ message: { parts: [{ type: "text", text: "second" }] },
2106
+ },
2107
+ ]);
2108
+ });
2109
+
2110
+ it("drops queued messages when the run is cancelled", async () => {
2111
+ const fetchMock = installFetch();
2112
+ fetchMock.setHandler(({ init }) => abortableStream(init).response);
2113
+
2114
+ const { aui } = mountThread({ capabilities: { queue: true } });
2115
+ aui().thread().append("first");
2116
+ await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
2117
+ await waitFor(() => expect(aui().thread().getState().isRunning).toBe(true));
2118
+
2119
+ aui().thread().append("second");
2120
+ await waitFor(() =>
2121
+ expect(aui().thread().composer().getState().queue).toHaveLength(1),
2122
+ );
2123
+
2124
+ aui().thread().cancelRun();
2125
+
2126
+ await waitFor(() =>
2127
+ expect(aui().thread().getState().isRunning).toBe(false),
2128
+ );
2129
+ expect(aui().thread().composer().getState().queue).toHaveLength(0);
2130
+ expect(fetchMock.requests).toHaveLength(1);
2131
+ });
2132
+
2133
+ it("stays off without the capability", async () => {
2134
+ installFetch();
2135
+ const { aui } = mountThread();
2136
+ await waitFor(() =>
2137
+ expect(aui().thread().getState().capabilities.queue).toBe(false),
2138
+ );
2139
+ });
2140
+ });
package/src/types.ts CHANGED
@@ -36,15 +36,21 @@ export type AssistantMessage = {
36
36
  readonly parts: readonly TextPart[];
37
37
  };
38
38
 
39
- export type AddMessageCommand = {
40
- readonly type: "add-message";
39
+ export type MessageAddCommand = {
40
+ readonly type: "message/add";
41
41
  readonly message: UserMessage | AssistantMessage;
42
42
  readonly parentId: string | null;
43
43
  readonly sourceId: string | null;
44
+ /**
45
+ * How the host handles the message while a run is active: `"queue"`
46
+ * appends to the queue, `"steer"` moves it to the front and interrupts
47
+ * the run. Absent, the host decides. Ignored when the host is idle.
48
+ */
49
+ readonly behavior?: "queue" | "steer";
44
50
  };
45
51
 
46
- export type AddToolResultCommand = {
47
- readonly type: "add-tool-result";
52
+ export type ToolResultAddCommand = {
53
+ readonly type: "tool-result/add";
48
54
  readonly toolCallId: string;
49
55
  readonly toolName: string;
50
56
  readonly result: ReadonlyJSONValue;
@@ -54,8 +60,8 @@ export type AddToolResultCommand = {
54
60
  };
55
61
 
56
62
  export type AssistantTransportCommand =
57
- | AddMessageCommand
58
- | AddToolResultCommand
63
+ | MessageAddCommand
64
+ | ToolResultAddCommand
59
65
  | UserCommands;
60
66
 
61
67
  export type AssistantTransportState = {
@@ -130,6 +136,11 @@ export type AssistantTransportOptions<T> = {
130
136
  capabilities?:
131
137
  | {
132
138
  edit?: boolean | undefined;
139
+ /**
140
+ * Buffer messages sent during a run in an in-memory queue and dispatch
141
+ * them once the run settles. Steering interrupts the active run.
142
+ */
143
+ queue?: boolean | undefined;
133
144
  }
134
145
  | undefined;
135
146
  adapters?: