@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/host.ts
CHANGED
|
@@ -11,7 +11,6 @@ import type { AssistantTransportStateOperation } from "assistant-stream";
|
|
|
11
11
|
import { asAsyncIterableStream } from "assistant-stream/utils";
|
|
12
12
|
import type { Statewire } from "statewire";
|
|
13
13
|
import {
|
|
14
|
-
CommandExecution,
|
|
15
14
|
useStatewireCommands,
|
|
16
15
|
useStatewireEmit,
|
|
17
16
|
useStatewireState,
|
|
@@ -61,13 +60,18 @@ export type AssistantTransportHostCommands = {
|
|
|
61
60
|
setParentId(parentId: string | null): void;
|
|
62
61
|
cancel(): void;
|
|
63
62
|
resume(): void;
|
|
64
|
-
reload(parentId: string | null): void;
|
|
65
63
|
importState(state: unknown): void;
|
|
66
64
|
};
|
|
67
65
|
|
|
66
|
+
type EntrySettlement = {
|
|
67
|
+
ack: () => void;
|
|
68
|
+
resolve: () => void;
|
|
69
|
+
reject: (error: unknown) => void;
|
|
70
|
+
};
|
|
71
|
+
|
|
68
72
|
type QueueEntry = {
|
|
69
73
|
command: AssistantTransportCommand;
|
|
70
|
-
execution:
|
|
74
|
+
execution: EntrySettlement;
|
|
71
75
|
};
|
|
72
76
|
|
|
73
77
|
type EntryQueue = {
|
|
@@ -367,14 +371,26 @@ const useHostLoop = (
|
|
|
367
371
|
};
|
|
368
372
|
|
|
369
373
|
return {
|
|
370
|
-
send: (
|
|
371
|
-
|
|
374
|
+
send: function (
|
|
375
|
+
this: StatewireHost.Ctx,
|
|
376
|
+
command: AssistantTransportCommand,
|
|
377
|
+
opts?: SendOptions,
|
|
378
|
+
) {
|
|
379
|
+
let resolve!: () => void;
|
|
380
|
+
let reject!: (error: unknown) => void;
|
|
381
|
+
const promise = new Promise<void>((res, rej) => {
|
|
382
|
+
resolve = res;
|
|
383
|
+
reject = rej;
|
|
384
|
+
});
|
|
372
385
|
setQueue({
|
|
373
|
-
queued: [
|
|
386
|
+
queued: [
|
|
387
|
+
...s.queue.queued,
|
|
388
|
+
{ command, execution: { ack: this.ack, resolve, reject } },
|
|
389
|
+
],
|
|
374
390
|
inTransit: s.queue.inTransit,
|
|
375
391
|
});
|
|
376
392
|
if (opts?.schedule !== false) schedule();
|
|
377
|
-
return
|
|
393
|
+
return promise;
|
|
378
394
|
},
|
|
379
395
|
setParentId: (id: string | null) => {
|
|
380
396
|
s.parentId = id;
|
|
@@ -391,10 +407,6 @@ const useHostLoop = (
|
|
|
391
407
|
s.resumeRequested = true;
|
|
392
408
|
schedule();
|
|
393
409
|
},
|
|
394
|
-
reload: (id: string | null) => {
|
|
395
|
-
s.parentId = id;
|
|
396
|
-
schedule();
|
|
397
|
-
},
|
|
398
410
|
importState: replaceAgent,
|
|
399
411
|
};
|
|
400
412
|
};
|
package/src/index.ts
CHANGED
|
@@ -15,13 +15,27 @@ export namespace AssistantTransportThread {
|
|
|
15
15
|
export type State = types.AssistantTransportState;
|
|
16
16
|
export type ConnectionMetadata = types.AssistantTransportConnectionMetadata;
|
|
17
17
|
export type StateConverter<T> = types.AssistantTransportStateConverter<T>;
|
|
18
|
-
export type
|
|
19
|
-
export type
|
|
18
|
+
export type RunCommand = types.RunCommand;
|
|
19
|
+
export type RunEnqueueCommand = types.RunEnqueueCommand;
|
|
20
|
+
export type RunSteerCommand = types.RunSteerCommand;
|
|
21
|
+
export type RunDequeueCommand = types.RunDequeueCommand;
|
|
22
|
+
export type RunEditCommand = types.RunEditCommand;
|
|
23
|
+
export type RunContinueCommand = types.RunContinueCommand;
|
|
24
|
+
export type RunReloadCommand = types.RunReloadCommand;
|
|
25
|
+
export type RunInputCommand = types.RunInputCommand;
|
|
26
|
+
export type InputRequest = types.InputRequest;
|
|
27
|
+
export type ToolCallInputRequest = types.ToolCallInputRequest;
|
|
28
|
+
export type ToolApprovalInputRequest = types.ToolApprovalInputRequest;
|
|
29
|
+
export type CustomInputRequest = types.CustomInputRequest;
|
|
30
|
+
export type InputRequestState = types.InputRequestState;
|
|
31
|
+
export type InputResponse = types.InputResponse;
|
|
32
|
+
export type ToolCallInputResponse = types.ToolCallInputResponse;
|
|
33
|
+
export type ToolApprovalInputResponse = types.ToolApprovalInputResponse;
|
|
34
|
+
export type RunUserMessage = types.RunUserMessage;
|
|
35
|
+
export type RunQueueItem = types.RunQueueItem;
|
|
36
|
+
export type RunPlacement = types.RunPlacement;
|
|
20
37
|
export type SendCommandsRequestBody = types.SendCommandsRequestBody;
|
|
21
|
-
export type UserMessage = types.UserMessage;
|
|
22
|
-
export type AssistantMessage = types.AssistantMessage;
|
|
23
|
-
export type UserMessagePart = types.UserMessagePart;
|
|
24
38
|
export type TextPart = types.TextPart;
|
|
25
|
-
export type
|
|
39
|
+
export type FilePart = types.FilePart;
|
|
26
40
|
export type HeadersValue = types.HeadersValue;
|
|
27
41
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { RespondToToolApprovalOptions } from "@assistant-ui/core";
|
|
2
|
+
import type { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
3
|
+
import type { InputRequestState, RunInputCommand } from "./types";
|
|
4
|
+
|
|
5
|
+
const assertUnanswered = (request: InputRequestState) => {
|
|
6
|
+
if (request.response !== null)
|
|
7
|
+
throw new Error(
|
|
8
|
+
`statewire: input request "${request.id}" is already answered`,
|
|
9
|
+
);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const resolveToolResultInput = (
|
|
13
|
+
inputRequests: readonly InputRequestState[],
|
|
14
|
+
options: {
|
|
15
|
+
toolCallId: string;
|
|
16
|
+
result: ReadonlyJSONValue;
|
|
17
|
+
isError: boolean;
|
|
18
|
+
artifact?: ReadonlyJSONValue | undefined;
|
|
19
|
+
},
|
|
20
|
+
): RunInputCommand => {
|
|
21
|
+
const request = inputRequests.find(
|
|
22
|
+
(r) => r.type === "tool-call" && r.toolCallId === options.toolCallId,
|
|
23
|
+
);
|
|
24
|
+
if (request === undefined)
|
|
25
|
+
throw new Error(
|
|
26
|
+
`statewire: no tool-call input request for tool call "${options.toolCallId}"`,
|
|
27
|
+
);
|
|
28
|
+
assertUnanswered(request);
|
|
29
|
+
return {
|
|
30
|
+
type: "run/input",
|
|
31
|
+
requestId: request.id,
|
|
32
|
+
response: {
|
|
33
|
+
output: options.result,
|
|
34
|
+
...(options.isError && { isError: true }),
|
|
35
|
+
...(options.artifact !== undefined && { artifact: options.artifact }),
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const resolveToolApprovalInput = (
|
|
41
|
+
inputRequests: readonly InputRequestState[],
|
|
42
|
+
options: RespondToToolApprovalOptions,
|
|
43
|
+
): RunInputCommand => {
|
|
44
|
+
const request = inputRequests.find(
|
|
45
|
+
(r) => r.type === "tool-approval" && r.id === options.approvalId,
|
|
46
|
+
);
|
|
47
|
+
if (request === undefined)
|
|
48
|
+
throw new Error(
|
|
49
|
+
`statewire: no tool-approval input request "${options.approvalId}"`,
|
|
50
|
+
);
|
|
51
|
+
assertUnanswered(request);
|
|
52
|
+
return {
|
|
53
|
+
type: "run/input",
|
|
54
|
+
requestId: request.id,
|
|
55
|
+
response: {
|
|
56
|
+
decision: options.approved ? "approve" : "reject",
|
|
57
|
+
...(options.reason !== undefined && { message: options.reason }),
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
package/src/legacy-diff.test.tsx
CHANGED
|
@@ -46,7 +46,10 @@ const converter: AssistantTransportStateConverter<TestState> = (
|
|
|
46
46
|
}) as unknown as ExternalThreadMessage,
|
|
47
47
|
),
|
|
48
48
|
...meta.pendingCommands.flatMap((c, i) =>
|
|
49
|
-
|
|
49
|
+
// The legacy runtime still surfaces add-message pending commands.
|
|
50
|
+
(c.type === "run/steer" || (c.type as string) === "add-message") &&
|
|
51
|
+
"message" in c &&
|
|
52
|
+
c.message.role === "user"
|
|
50
53
|
? [
|
|
51
54
|
{
|
|
52
55
|
id: `optimistic-${i}`,
|
|
@@ -71,18 +74,36 @@ const converter: AssistantTransportStateConverter<TestState> = (
|
|
|
71
74
|
|
|
72
75
|
type RecordedRequest = { url: string; body: Record<string, any> };
|
|
73
76
|
|
|
74
|
-
// deliberate divergence: the
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
"add-tool-result": "tool-result/add",
|
|
80
|
-
};
|
|
77
|
+
// deliberate divergence: the re-implementation speaks the run/* dialect —
|
|
78
|
+
// user messages go out as run/steer (client-generated message id, no
|
|
79
|
+
// per-command parentId/sourceId — parentId rides the request body) and tool
|
|
80
|
+
// results as run/input against the parked request; legacy add-message /
|
|
81
|
+
// add-tool-result bodies are translated before comparison.
|
|
81
82
|
const renameWire = (commands: unknown) =>
|
|
82
|
-
(commands as
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
(commands as Record<string, any>[]).map((c) =>
|
|
84
|
+
c.type === "add-message"
|
|
85
|
+
? {
|
|
86
|
+
type: "run/steer",
|
|
87
|
+
message: {
|
|
88
|
+
id: expect.any(String),
|
|
89
|
+
role: c.message.role,
|
|
90
|
+
parts: c.message.parts,
|
|
91
|
+
},
|
|
92
|
+
anchorMessageId: expect.toSatisfy(
|
|
93
|
+
(v) => v === null || typeof v === "string",
|
|
94
|
+
),
|
|
95
|
+
}
|
|
96
|
+
: c.type === "add-tool-result"
|
|
97
|
+
? {
|
|
98
|
+
type: "run/input",
|
|
99
|
+
requestId: `req-${c.toolCallId}`,
|
|
100
|
+
response: {
|
|
101
|
+
output: c.result,
|
|
102
|
+
...(c.isError && { isError: true }),
|
|
103
|
+
},
|
|
104
|
+
}
|
|
105
|
+
: c,
|
|
106
|
+
);
|
|
86
107
|
|
|
87
108
|
// Scripted server: request N gets transcript N, streamed whole.
|
|
88
109
|
const installScriptedFetch = (transcript: string[][]) => {
|
|
@@ -258,6 +279,12 @@ const observeNew = async (): Promise<{
|
|
|
258
279
|
|
|
259
280
|
type ToolState = TestState & {
|
|
260
281
|
toolCall?: { id: string; name: string; argsText: string } | null;
|
|
282
|
+
inputRequests?: {
|
|
283
|
+
type: "tool-call";
|
|
284
|
+
id: string;
|
|
285
|
+
toolCallId: string;
|
|
286
|
+
response: null;
|
|
287
|
+
}[];
|
|
261
288
|
note?: string;
|
|
262
289
|
};
|
|
263
290
|
|
|
@@ -265,7 +292,10 @@ const toolConverter: AssistantTransportStateConverter<ToolState> = (
|
|
|
265
292
|
state,
|
|
266
293
|
meta,
|
|
267
294
|
) => {
|
|
268
|
-
const base =
|
|
295
|
+
const base = {
|
|
296
|
+
...converter(state, meta),
|
|
297
|
+
inputRequests: state.inputRequests ?? [],
|
|
298
|
+
};
|
|
269
299
|
if (!state.toolCall) return base;
|
|
270
300
|
return {
|
|
271
301
|
...base,
|
|
@@ -311,6 +341,18 @@ const TOOL_TRANSCRIPT: string[][] = [
|
|
|
311
341
|
path: ["toolCall"],
|
|
312
342
|
value: { id: "tc1", name: "probe_tool", argsText: '{"x":1}' },
|
|
313
343
|
},
|
|
344
|
+
{
|
|
345
|
+
type: "set",
|
|
346
|
+
path: ["inputRequests"],
|
|
347
|
+
value: [
|
|
348
|
+
{
|
|
349
|
+
type: "tool-call",
|
|
350
|
+
id: "req-tc1",
|
|
351
|
+
toolCallId: "tc1",
|
|
352
|
+
response: null,
|
|
353
|
+
},
|
|
354
|
+
],
|
|
355
|
+
},
|
|
314
356
|
],
|
|
315
357
|
}),
|
|
316
358
|
"data: [DONE]\n\n",
|
|
@@ -320,6 +362,7 @@ const TOOL_TRANSCRIPT: string[][] = [
|
|
|
320
362
|
type: "update-state",
|
|
321
363
|
operations: [
|
|
322
364
|
{ type: "set", path: ["toolCall"], value: null },
|
|
365
|
+
{ type: "set", path: ["inputRequests"], value: [] },
|
|
323
366
|
{
|
|
324
367
|
type: "set",
|
|
325
368
|
path: ["messages"],
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AppendMessage,
|
|
3
|
+
ExternalThreadQueueAdapter,
|
|
4
|
+
QueuePlacement,
|
|
5
|
+
} from "@assistant-ui/core";
|
|
6
|
+
import type {
|
|
7
|
+
FilePart,
|
|
8
|
+
RunCommand,
|
|
9
|
+
RunQueueItem,
|
|
10
|
+
RunReloadCommand,
|
|
11
|
+
RunUserMessage,
|
|
12
|
+
TextPart,
|
|
13
|
+
} from "./types";
|
|
14
|
+
|
|
15
|
+
export const toWireMessage = (
|
|
16
|
+
message: AppendMessage,
|
|
17
|
+
id: string,
|
|
18
|
+
): RunUserMessage => {
|
|
19
|
+
if (message.role !== "user")
|
|
20
|
+
throw new Error("statewire: only user messages are supported");
|
|
21
|
+
const files: FilePart[] = [];
|
|
22
|
+
const texts: string[] = [];
|
|
23
|
+
const source = [
|
|
24
|
+
...message.content,
|
|
25
|
+
...(message.attachments?.flatMap((a) => a.content) ?? []),
|
|
26
|
+
];
|
|
27
|
+
for (const part of source) {
|
|
28
|
+
if (part.type === "text") {
|
|
29
|
+
texts.push(part.text);
|
|
30
|
+
} else if (part.type === "image") {
|
|
31
|
+
files.push({ type: "file", mediaType: "image/*", url: part.image });
|
|
32
|
+
} else if (part.type === "file") {
|
|
33
|
+
files.push({
|
|
34
|
+
type: "file",
|
|
35
|
+
mediaType: part.mimeType,
|
|
36
|
+
url: part.data,
|
|
37
|
+
...(part.filename !== undefined && { filename: part.filename }),
|
|
38
|
+
});
|
|
39
|
+
} else {
|
|
40
|
+
throw new Error(`statewire: unsupported message part "${part.type}"`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// default-safe against host capabilities: files first, one merged text part
|
|
44
|
+
const parts: (TextPart | FilePart)[] = [
|
|
45
|
+
...files,
|
|
46
|
+
...(texts.length > 0
|
|
47
|
+
? [{ type: "text" as const, text: texts.join("\n\n") }]
|
|
48
|
+
: []),
|
|
49
|
+
];
|
|
50
|
+
if (parts.length === 0)
|
|
51
|
+
throw new Error("statewire: cannot send an empty message");
|
|
52
|
+
return { id, role: "user", parts };
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const toQueueItemState = (item: RunQueueItem) => ({
|
|
56
|
+
id: item.id,
|
|
57
|
+
prompt: item.parts
|
|
58
|
+
.flatMap((part) => (part.type === "text" ? [part.text] : []))
|
|
59
|
+
.join("\n\n"),
|
|
60
|
+
parts: item.parts.map((part) =>
|
|
61
|
+
part.type === "text"
|
|
62
|
+
? part
|
|
63
|
+
: {
|
|
64
|
+
type: "file" as const,
|
|
65
|
+
data: part.url,
|
|
66
|
+
mimeType: part.mediaType,
|
|
67
|
+
...(part.filename !== undefined && { filename: part.filename }),
|
|
68
|
+
},
|
|
69
|
+
),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const placementFields = (placement: QueuePlacement) => ({
|
|
73
|
+
...(placement.insertAfter !== undefined && {
|
|
74
|
+
insertAfter: placement.insertAfter,
|
|
75
|
+
}),
|
|
76
|
+
...(placement.insertBefore !== undefined && {
|
|
77
|
+
insertBefore: placement.insertBefore,
|
|
78
|
+
}),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export const viewedLeafId = (
|
|
82
|
+
messages: readonly { id: string }[],
|
|
83
|
+
): string | null => messages.at(-1)?.id ?? null;
|
|
84
|
+
|
|
85
|
+
export const createRunQueueAdapter = (
|
|
86
|
+
messages: readonly { id: string }[],
|
|
87
|
+
queueItems: readonly RunQueueItem[],
|
|
88
|
+
steerQueueItems: readonly RunQueueItem[] | undefined,
|
|
89
|
+
dispatch: (command: RunCommand) => void,
|
|
90
|
+
): ExternalThreadQueueAdapter => {
|
|
91
|
+
const laneOf = (queueItemId: string): "queue" | "steer" => {
|
|
92
|
+
if (steerQueueItems?.some((item) => item.id === queueItemId))
|
|
93
|
+
return "steer";
|
|
94
|
+
if (queueItems.some((item) => item.id === queueItemId)) return "queue";
|
|
95
|
+
throw new Error(`statewire: unknown queue item "${queueItemId}"`);
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
items: queueItems.map(toQueueItemState),
|
|
99
|
+
steerItems: (steerQueueItems ?? []).map(toQueueItemState),
|
|
100
|
+
enqueue: (message) =>
|
|
101
|
+
dispatch({
|
|
102
|
+
type: "run/enqueue",
|
|
103
|
+
message: toWireMessage(message, crypto.randomUUID()),
|
|
104
|
+
anchorMessageId: viewedLeafId(messages),
|
|
105
|
+
}),
|
|
106
|
+
steer: (message) =>
|
|
107
|
+
dispatch({
|
|
108
|
+
type: "run/steer",
|
|
109
|
+
message: toWireMessage(message, crypto.randomUUID()),
|
|
110
|
+
anchorMessageId: viewedLeafId(messages),
|
|
111
|
+
}),
|
|
112
|
+
move: (queueItemId, placement) => {
|
|
113
|
+
const lane = placement.lane ?? laneOf(queueItemId);
|
|
114
|
+
const fields = {
|
|
115
|
+
messageId: queueItemId,
|
|
116
|
+
...placementFields(placement),
|
|
117
|
+
};
|
|
118
|
+
dispatch(
|
|
119
|
+
lane === "steer"
|
|
120
|
+
? { type: "run/steer", ...fields }
|
|
121
|
+
: { type: "run/enqueue", ...fields },
|
|
122
|
+
);
|
|
123
|
+
},
|
|
124
|
+
edit: (queueItemId, message) => {
|
|
125
|
+
const wireMessage = toWireMessage(message, queueItemId);
|
|
126
|
+
dispatch(
|
|
127
|
+
laneOf(queueItemId) === "steer"
|
|
128
|
+
? { type: "run/steer", message: wireMessage }
|
|
129
|
+
: { type: "run/enqueue", message: wireMessage },
|
|
130
|
+
);
|
|
131
|
+
},
|
|
132
|
+
remove: (queueItemId) =>
|
|
133
|
+
dispatch({ type: "run/dequeue", messageId: queueItemId }),
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export const resolveReloadCommand = (
|
|
138
|
+
messages: readonly { id: string; role: string }[],
|
|
139
|
+
parentId: string | null,
|
|
140
|
+
): RunReloadCommand => {
|
|
141
|
+
const index =
|
|
142
|
+
parentId === null
|
|
143
|
+
? 0
|
|
144
|
+
: messages.findIndex((message) => message.id === parentId) + 1;
|
|
145
|
+
if (parentId !== null && index === 0)
|
|
146
|
+
throw new Error(`statewire: run/reload parent "${parentId}" is unknown`);
|
|
147
|
+
const target = messages[index];
|
|
148
|
+
if (target === undefined || target.role !== "assistant")
|
|
149
|
+
throw new Error(
|
|
150
|
+
"statewire: run/reload requires an assistant message to regenerate",
|
|
151
|
+
);
|
|
152
|
+
return {
|
|
153
|
+
type: "run/reload",
|
|
154
|
+
sourceId: target.id,
|
|
155
|
+
messageId: crypto.randomUUID(),
|
|
156
|
+
};
|
|
157
|
+
};
|