@assistant-ui/react-statewire 0.4.4 → 0.6.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 +9 -7
- package/dist/AssistantTransportThread.d.ts.map +1 -1
- package/dist/AssistantTransportThread.js +47 -35
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +90 -9
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +74 -45
- package/dist/StatewireThread.js.map +1 -1
- package/dist/hooks.d.ts +4 -1
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +10 -1
- package/dist/hooks.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/runCommands.d.ts +2 -1
- package/dist/runCommands.d.ts.map +1 -1
- package/dist/runCommands.js +25 -11
- package/dist/runCommands.js.map +1 -1
- package/dist/runsOptimistic.d.ts +7 -0
- package/dist/runsOptimistic.d.ts.map +1 -0
- package/dist/runsOptimistic.js +92 -0
- package/dist/runsOptimistic.js.map +1 -0
- package/package.json +3 -3
- package/src/AssistantTransportThread.ts +77 -33
- package/src/StatewireThread.runs.test.tsx +405 -0
- package/src/StatewireThread.test.tsx +177 -1
- package/src/StatewireThread.ts +218 -77
- package/src/__tests__/testHelpers.tsx +53 -53
- package/src/athena.e2e.test.tsx +26 -23
- package/src/hooks.ts +12 -0
- package/src/index.ts +1 -0
- package/src/legacy-diff.test.tsx +36 -47
- package/src/runCommands.ts +28 -12
- package/src/runsOptimistic.test.ts +225 -0
- package/src/runsOptimistic.ts +164 -0
- package/src/transport.test.tsx +187 -88
package/src/StatewireThread.ts
CHANGED
|
@@ -13,8 +13,10 @@ import type {
|
|
|
13
13
|
AppendMessage,
|
|
14
14
|
AttachmentAdapter,
|
|
15
15
|
ExternalThreadQueueAdapter,
|
|
16
|
+
FeedbackAdapter,
|
|
16
17
|
ModelContext,
|
|
17
18
|
RespondToToolApprovalOptions,
|
|
19
|
+
SpeechSynthesisAdapter,
|
|
18
20
|
ThreadMessage,
|
|
19
21
|
} from "@assistant-ui/core";
|
|
20
22
|
import { toToolsJSONSchema } from "assistant-stream";
|
|
@@ -28,6 +30,7 @@ import type {
|
|
|
28
30
|
} from "./AssistantTransportThread";
|
|
29
31
|
import {
|
|
30
32
|
createRunQueueAdapter,
|
|
33
|
+
resolveEditCommand,
|
|
31
34
|
resolveReloadCommand,
|
|
32
35
|
toWireMessage,
|
|
33
36
|
viewedLeafId,
|
|
@@ -36,9 +39,23 @@ import {
|
|
|
36
39
|
resolveToolApprovalInput,
|
|
37
40
|
resolveToolResultInput,
|
|
38
41
|
} from "./inputRequests";
|
|
42
|
+
import { runsOptimistic } from "./runsOptimistic";
|
|
39
43
|
|
|
40
44
|
type AnyCommands = Record<string, (...args: any[]) => unknown>;
|
|
41
45
|
|
|
46
|
+
type AnyOptions =
|
|
47
|
+
| StatewireThread.Options<any>
|
|
48
|
+
| StatewireThread.RunsOptions<any>;
|
|
49
|
+
|
|
50
|
+
const READY_RUNS: StatewireThread.Runs = {
|
|
51
|
+
status: "ready",
|
|
52
|
+
runId: null,
|
|
53
|
+
queue: [],
|
|
54
|
+
steerQueue: [],
|
|
55
|
+
error: null,
|
|
56
|
+
runLeafMessageId: null,
|
|
57
|
+
};
|
|
58
|
+
|
|
42
59
|
const ambientContextOf = (context: ModelContext): Record<string, unknown> => ({
|
|
43
60
|
...(context.system !== undefined && { system: context.system }),
|
|
44
61
|
...(context.tools !== undefined && {
|
|
@@ -50,44 +67,7 @@ const ambientContextOf = (context: ModelContext): Record<string, unknown> => ({
|
|
|
50
67
|
...(context.config !== undefined && { config: context.config }),
|
|
51
68
|
});
|
|
52
69
|
|
|
53
|
-
const
|
|
54
|
-
transport: StatewireThread.Transport<any>,
|
|
55
|
-
latest: { current: StatewireThread.Options<any> },
|
|
56
|
-
): StatewireThread.Transport<any> => {
|
|
57
|
-
const [transportOptions, ...rest] = transport.args as [
|
|
58
|
-
Record<string, unknown>,
|
|
59
|
-
...unknown[],
|
|
60
|
-
];
|
|
61
|
-
if (
|
|
62
|
-
rest.length > 0 ||
|
|
63
|
-
typeof transportOptions !== "object" ||
|
|
64
|
-
transportOptions === null
|
|
65
|
-
) {
|
|
66
|
-
throw new Error(
|
|
67
|
-
"statewire: onCommandSessionLost requires a transport element that takes a single options object",
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
const transportLevel = transportOptions.onCommandSessionLost as
|
|
71
|
-
| ((command: StatewireClient.SessionLostCommand) => "replay" | void)
|
|
72
|
-
| undefined;
|
|
73
|
-
return {
|
|
74
|
-
...transport,
|
|
75
|
-
args: [
|
|
76
|
-
{
|
|
77
|
-
...transportOptions,
|
|
78
|
-
onCommandSessionLost: (
|
|
79
|
-
command: StatewireClient.SessionLostCommand,
|
|
80
|
-
): "replay" | void =>
|
|
81
|
-
transportLevel?.(command) ??
|
|
82
|
-
latest.current.onCommandSessionLost?.(command),
|
|
83
|
-
},
|
|
84
|
-
],
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const useStatewireThread = (
|
|
89
|
-
options: StatewireThread.Options<any>,
|
|
90
|
-
): ClientOutput<"thread"> => {
|
|
70
|
+
const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
91
71
|
const latest = useRef(options);
|
|
92
72
|
latest.current = options;
|
|
93
73
|
|
|
@@ -103,21 +83,41 @@ const useStatewireThread = (
|
|
|
103
83
|
),
|
|
104
84
|
);
|
|
105
85
|
|
|
86
|
+
const runsOption = options.runs;
|
|
87
|
+
const [runsSeam] = useState(() => runsOption !== undefined);
|
|
88
|
+
if (runsSeam !== (runsOption !== undefined))
|
|
89
|
+
throw new Error(
|
|
90
|
+
"statewire: the runs option must be stable — it was " +
|
|
91
|
+
(runsSeam ? "set" : "unset") +
|
|
92
|
+
" on first render and changed since",
|
|
93
|
+
);
|
|
94
|
+
|
|
106
95
|
const {
|
|
107
96
|
state: transportState,
|
|
108
97
|
commands,
|
|
109
98
|
connection,
|
|
110
99
|
setContext,
|
|
111
100
|
} = useStatewire<unknown, AnyCommands>({
|
|
112
|
-
transport:
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
101
|
+
transport: options.transport,
|
|
102
|
+
...(runsSeam && {
|
|
103
|
+
optimistic: (draft, command, ctx) => {
|
|
104
|
+
const runsOpt = latest.current.runs!;
|
|
105
|
+
runsOptimistic(runsOpt === true ? draft : runsOpt(draft), command, ctx);
|
|
106
|
+
(latest.current as StatewireThread.RunsOptions<unknown>).optimistic?.(
|
|
107
|
+
draft,
|
|
108
|
+
command,
|
|
109
|
+
ctx,
|
|
110
|
+
);
|
|
111
|
+
},
|
|
112
|
+
}),
|
|
116
113
|
onCommandChange: (update, ctx) => {
|
|
117
114
|
if (update.status === "acked" || update.status === "settled")
|
|
118
115
|
retire(update.params[0]);
|
|
119
116
|
latest.current.onCommandChange?.(update, ctx);
|
|
120
117
|
},
|
|
118
|
+
onStateChange: (state, ctx) => latest.current.onStateChange?.(state, ctx),
|
|
119
|
+
onConnectionChange: (connection) =>
|
|
120
|
+
latest.current.onConnectionChange?.(connection),
|
|
121
121
|
});
|
|
122
122
|
const attached = transportState !== undefined;
|
|
123
123
|
const state = attached ? transportState : options.initialState;
|
|
@@ -159,21 +159,50 @@ const useStatewireThread = (
|
|
|
159
159
|
[commands, retire],
|
|
160
160
|
);
|
|
161
161
|
|
|
162
|
+
const runs = useMemo((): StatewireThread.Runs | undefined => {
|
|
163
|
+
if (runsOption === undefined) return undefined;
|
|
164
|
+
const mount = (runsOption === true ? state : runsOption(state)) as
|
|
165
|
+
| StatewireThread.RunsState
|
|
166
|
+
| undefined;
|
|
167
|
+
const entry = mount?.runs?.[0];
|
|
168
|
+
return entry === undefined
|
|
169
|
+
? READY_RUNS
|
|
170
|
+
: {
|
|
171
|
+
status: entry.status,
|
|
172
|
+
runId: entry.runId,
|
|
173
|
+
queue: entry.queue,
|
|
174
|
+
steerQueue: entry.steerQueue,
|
|
175
|
+
...(entry.inputRequests !== undefined && {
|
|
176
|
+
inputRequests: entry.inputRequests,
|
|
177
|
+
}),
|
|
178
|
+
error: entry.error,
|
|
179
|
+
runLeafMessageId: entry.runLeafMessageId,
|
|
180
|
+
};
|
|
181
|
+
}, [runsOption, state]);
|
|
182
|
+
|
|
162
183
|
const { converter } = options;
|
|
163
184
|
const metadata = useMemo(
|
|
164
185
|
(): StatewireThread.ConnectionMetadata => ({
|
|
165
186
|
pendingCommands: [...pending],
|
|
166
187
|
isSending: pending.length > 0,
|
|
167
188
|
connection,
|
|
189
|
+
...(runs !== undefined && { runs }),
|
|
168
190
|
}),
|
|
169
|
-
[pending, connection],
|
|
191
|
+
[pending, connection, runs],
|
|
170
192
|
);
|
|
171
193
|
const converted = useMemo(
|
|
172
|
-
() =>
|
|
194
|
+
() =>
|
|
195
|
+
converter(
|
|
196
|
+
state,
|
|
197
|
+
metadata as StatewireThread.ConnectionMetadata & {
|
|
198
|
+
runs: StatewireThread.Runs;
|
|
199
|
+
},
|
|
200
|
+
) as StatewireThread.State,
|
|
173
201
|
[converter, state, metadata],
|
|
174
202
|
);
|
|
175
|
-
const queueItems = converted.queue;
|
|
176
|
-
const steerQueueItems =
|
|
203
|
+
const queueItems = runs !== undefined ? runs.queue : converted.queue;
|
|
204
|
+
const steerQueueItems =
|
|
205
|
+
runs !== undefined ? runs.steerQueue : converted.steerQueue;
|
|
177
206
|
if (steerQueueItems !== undefined && queueItems === undefined)
|
|
178
207
|
throw new Error(
|
|
179
208
|
"statewire: the converter's steerQueue projection requires the queue projection",
|
|
@@ -201,7 +230,8 @@ const useStatewireThread = (
|
|
|
201
230
|
[converted.messages, queueItems, steerQueueItems, dispatch],
|
|
202
231
|
);
|
|
203
232
|
|
|
204
|
-
const inputRequests =
|
|
233
|
+
const inputRequests =
|
|
234
|
+
runs !== undefined ? (runs.inputRequests ?? []) : converted.inputRequests;
|
|
205
235
|
const [inputSeam] = useState(() => ({
|
|
206
236
|
present: inputRequests !== undefined,
|
|
207
237
|
}));
|
|
@@ -219,16 +249,21 @@ const useStatewireThread = (
|
|
|
219
249
|
reconnect: connection.reconnect,
|
|
220
250
|
state,
|
|
221
251
|
...(inputRequests !== undefined && { inputRequests }),
|
|
252
|
+
...(runs !== undefined && { runs }),
|
|
222
253
|
}),
|
|
223
|
-
[dispatch, connection.reconnect, state, inputRequests],
|
|
254
|
+
[dispatch, connection.reconnect, state, inputRequests, runs],
|
|
224
255
|
);
|
|
225
256
|
|
|
226
257
|
const handleAppend = (message: AppendMessage) => {
|
|
227
|
-
dispatch(
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
258
|
+
dispatch(
|
|
259
|
+
message.sourceId != null
|
|
260
|
+
? resolveEditCommand(message)
|
|
261
|
+
: {
|
|
262
|
+
type: "run/steer",
|
|
263
|
+
message: toWireMessage(message, crypto.randomUUID()),
|
|
264
|
+
anchorMessageId: viewedLeafId(converted.messages),
|
|
265
|
+
},
|
|
266
|
+
);
|
|
232
267
|
};
|
|
233
268
|
|
|
234
269
|
const handleReload = (parentId: string | null) => {
|
|
@@ -236,23 +271,20 @@ const useStatewireThread = (
|
|
|
236
271
|
};
|
|
237
272
|
|
|
238
273
|
const handleEdit = (message: AppendMessage) => {
|
|
239
|
-
|
|
240
|
-
throw new Error("statewire: run/edit requires a sourceId");
|
|
241
|
-
dispatch({
|
|
242
|
-
type: "run/edit",
|
|
243
|
-
message: toWireMessage(message, crypto.randomUUID()),
|
|
244
|
-
sourceId: message.sourceId,
|
|
245
|
-
});
|
|
274
|
+
dispatch(resolveEditCommand(message));
|
|
246
275
|
};
|
|
247
276
|
|
|
248
277
|
return useResource(
|
|
249
278
|
ExternalThread({
|
|
250
279
|
messages: converted.messages as readonly ExternalThreadMessage[],
|
|
251
|
-
isRunning:
|
|
280
|
+
isRunning:
|
|
281
|
+
runs !== undefined ? runs.status === "running" : converted.isRunning,
|
|
252
282
|
isLoading: !attached,
|
|
253
283
|
state: converted.state,
|
|
254
284
|
extras,
|
|
255
285
|
attachmentAdapter: options.adapters?.attachments,
|
|
286
|
+
speechAdapter: options.adapters?.speech,
|
|
287
|
+
feedbackAdapter: options.adapters?.feedback,
|
|
256
288
|
onNew: handleAppend,
|
|
257
289
|
...(queue && { queue }),
|
|
258
290
|
...(options.capabilities?.edit && { onEdit: handleEdit }),
|
|
@@ -275,9 +307,14 @@ const useStatewireThread = (
|
|
|
275
307
|
Array.isArray(payload))
|
|
276
308
|
)
|
|
277
309
|
throw new Error("statewire: stopPayload must return a plain object");
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
310
|
+
const runId = runs !== undefined ? runs.runId : converted.runId;
|
|
311
|
+
const args =
|
|
312
|
+
runId === undefined
|
|
313
|
+
? payload === undefined
|
|
314
|
+
? []
|
|
315
|
+
: [payload]
|
|
316
|
+
: [{ ...payload, runId }];
|
|
317
|
+
void commands["run/stop"]!(...args).catch((error: Error) => {
|
|
281
318
|
latest.current.onError?.(error);
|
|
282
319
|
});
|
|
283
320
|
},
|
|
@@ -285,10 +322,15 @@ const useStatewireThread = (
|
|
|
285
322
|
);
|
|
286
323
|
};
|
|
287
324
|
|
|
288
|
-
export const StatewireThread
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
325
|
+
export const StatewireThread: {
|
|
326
|
+
<T>(
|
|
327
|
+
options: StatewireThread.RunsOptions<T>,
|
|
328
|
+
): ResourceElement<ClientOutput<"thread">>;
|
|
329
|
+
<T>(
|
|
330
|
+
options: StatewireThread.Options<T>,
|
|
331
|
+
): ResourceElement<ClientOutput<"thread">>;
|
|
332
|
+
} = (options: AnyOptions): ResourceElement<ClientOutput<"thread">> =>
|
|
333
|
+
resource(useStatewireThread)(options);
|
|
292
334
|
|
|
293
335
|
export namespace StatewireThread {
|
|
294
336
|
export type Transport<T> = ResourceElement<Statewire<T | undefined, any>>;
|
|
@@ -433,6 +475,8 @@ export namespace StatewireThread {
|
|
|
433
475
|
readonly messages: readonly ThreadMessage[];
|
|
434
476
|
readonly state?: ReadonlyJSONValue;
|
|
435
477
|
readonly isRunning: boolean;
|
|
478
|
+
/** The live run's id projected out of the host state; stamped onto `run/stop`. */
|
|
479
|
+
readonly runId?: string | null;
|
|
436
480
|
/**
|
|
437
481
|
* The host's regular queue lane projected out of the state. Returning it
|
|
438
482
|
* (even empty) turns on the queue capability: sends become `run/enqueue`
|
|
@@ -451,11 +495,67 @@ export namespace StatewireThread {
|
|
|
451
495
|
readonly inputRequests?: readonly InputRequestState[];
|
|
452
496
|
};
|
|
453
497
|
|
|
498
|
+
/** Run subsystem status replicated by the host. */
|
|
499
|
+
export type RunStatus =
|
|
500
|
+
| "ready"
|
|
501
|
+
| "running"
|
|
502
|
+
| "stopped"
|
|
503
|
+
| "error"
|
|
504
|
+
| "input-required";
|
|
505
|
+
|
|
506
|
+
/** A queue-lane entry as replicated: the user message plus host-stamped `meta`. */
|
|
507
|
+
export type RunsQueueEntry = UserMessage & {
|
|
508
|
+
/** Host-stamped opaque per-entry metadata. */
|
|
509
|
+
readonly meta?: ReadonlyJSONValue;
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
/** The replicated run error — set on an unhandled failure, cleared by the next user-initiated run. */
|
|
513
|
+
export type RunsError = {
|
|
514
|
+
readonly message: string;
|
|
515
|
+
readonly [key: string]: ReadonlyJSONValue | undefined;
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
/** A replicated run entry — one element of the mount's `runs` array. */
|
|
519
|
+
export type RunState = {
|
|
520
|
+
/** The run's identity — minted at dispatch, persists through input-required, null in error/stop. */
|
|
521
|
+
readonly runId: string | null;
|
|
522
|
+
readonly status: Exclude<RunStatus, "ready">;
|
|
523
|
+
readonly queue: readonly RunsQueueEntry[];
|
|
524
|
+
readonly steerQueue: readonly RunsQueueEntry[];
|
|
525
|
+
/** The parked pending set; present only in input-required. */
|
|
526
|
+
readonly inputRequests?: readonly InputRequestState[];
|
|
527
|
+
readonly error: RunsError | null;
|
|
528
|
+
readonly runLeafMessageId: string | null;
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
/** The runs mount as replicated: the run entries plus the top-level status projection. */
|
|
532
|
+
export type RunsState = {
|
|
533
|
+
/** Projection of `runs[0]?.status ?? "ready"`. */
|
|
534
|
+
readonly status: RunStatus;
|
|
535
|
+
/** At most one entry; empty while resting. */
|
|
536
|
+
readonly runs: readonly RunState[];
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
/** The run subsystem's typed read — `runs[0]` of the selected mount with unsettled commands folded in, ready defaults while resting. */
|
|
540
|
+
export type Runs = {
|
|
541
|
+
readonly status: RunStatus;
|
|
542
|
+
/** The live run's id — non-null in running and input-required; stamped onto `run/stop`. */
|
|
543
|
+
readonly runId: string | null;
|
|
544
|
+
readonly queue: readonly RunsQueueEntry[];
|
|
545
|
+
readonly steerQueue: readonly RunsQueueEntry[];
|
|
546
|
+
/** The parked pending set; present only in input-required. */
|
|
547
|
+
readonly inputRequests?: readonly InputRequestState[];
|
|
548
|
+
readonly error: RunsError | null;
|
|
549
|
+
readonly runLeafMessageId: string | null;
|
|
550
|
+
};
|
|
551
|
+
|
|
454
552
|
export type ConnectionMetadata = {
|
|
455
553
|
pendingCommands: Command[];
|
|
456
554
|
isSending: boolean;
|
|
457
555
|
/** Statewire connection status; local transports are always `live`. */
|
|
458
556
|
connection: StatewireClient.ConnectionState;
|
|
557
|
+
/** The typed runs read; present iff the `runs` option is set. */
|
|
558
|
+
runs?: Runs;
|
|
459
559
|
};
|
|
460
560
|
|
|
461
561
|
export type StateConverter<T> = (
|
|
@@ -463,6 +563,17 @@ export namespace StatewireThread {
|
|
|
463
563
|
connectionMetadata: ConnectionMetadata,
|
|
464
564
|
) => State;
|
|
465
565
|
|
|
566
|
+
/** Converter output in runs mode: app-owned keys only — the lib projects the run mounts itself. */
|
|
567
|
+
export type RunsConverterState = {
|
|
568
|
+
readonly messages: readonly ThreadMessage[];
|
|
569
|
+
readonly state?: ReadonlyJSONValue;
|
|
570
|
+
};
|
|
571
|
+
|
|
572
|
+
export type RunsStateConverter<T> = (
|
|
573
|
+
state: T,
|
|
574
|
+
connectionMetadata: ConnectionMetadata & { runs: Runs },
|
|
575
|
+
) => RunsConverterState;
|
|
576
|
+
|
|
466
577
|
/**
|
|
467
578
|
* Payload of the built-in `run/stop` command. `reason` is the one well-known
|
|
468
579
|
* field; apps extend it with their own JSON-serializable fields.
|
|
@@ -476,6 +587,7 @@ export namespace StatewireThread {
|
|
|
476
587
|
transport: Transport<T>;
|
|
477
588
|
initialState: T;
|
|
478
589
|
converter: StateConverter<T>;
|
|
590
|
+
runs?: undefined;
|
|
479
591
|
/**
|
|
480
592
|
* "command-send" mirrors the client's ambient model context (system,
|
|
481
593
|
* tools, callSettings, config) to the server over the command channel —
|
|
@@ -484,15 +596,6 @@ export namespace StatewireThread {
|
|
|
484
596
|
contextSync?: "command-send" | undefined;
|
|
485
597
|
/** Supplies the `run/stop` command's payload; omitted, stop sends no payload. */
|
|
486
598
|
stopPayload?: (() => StopPayload) | undefined;
|
|
487
|
-
/**
|
|
488
|
-
* Offered each pending command when a remote transport's session is
|
|
489
|
-
* evicted; return "replay" to re-enqueue it under the rotated client id.
|
|
490
|
-
* A callback set on the transport options is consulted first; this one
|
|
491
|
-
* runs for commands it declined.
|
|
492
|
-
*/
|
|
493
|
-
onCommandSessionLost?:
|
|
494
|
-
| ((command: StatewireClient.SessionLostCommand) => "replay" | void)
|
|
495
|
-
| undefined;
|
|
496
599
|
/** Observes each command's lifecycle (queued, posted, acked, settled); runs after the built-in pending-command retirement. */
|
|
497
600
|
onCommandChange?:
|
|
498
601
|
| ((
|
|
@@ -502,6 +605,14 @@ export namespace StatewireThread {
|
|
|
502
605
|
ctx: { state: NonNullable<T> },
|
|
503
606
|
) => void)
|
|
504
607
|
| undefined;
|
|
608
|
+
/** Observes the transport state: fires with the attach snapshot, then once per change. */
|
|
609
|
+
onStateChange?:
|
|
610
|
+
| ((state: NonNullable<T>, ctx: { changes: Statewire.Changes }) => void)
|
|
611
|
+
| undefined;
|
|
612
|
+
/** Observes connection status transitions, starting with the initial status. */
|
|
613
|
+
onConnectionChange?:
|
|
614
|
+
| ((connection: StatewireClient.Connection) => void)
|
|
615
|
+
| undefined;
|
|
505
616
|
onError?: ((error: Error) => void) | undefined;
|
|
506
617
|
capabilities?:
|
|
507
618
|
| {
|
|
@@ -515,9 +626,39 @@ export namespace StatewireThread {
|
|
|
515
626
|
adapters?:
|
|
516
627
|
| {
|
|
517
628
|
attachments?: AttachmentAdapter | undefined;
|
|
629
|
+
speech?: SpeechSynthesisAdapter | undefined;
|
|
630
|
+
feedback?: FeedbackAdapter | undefined;
|
|
518
631
|
}
|
|
519
632
|
| undefined;
|
|
520
633
|
};
|
|
634
|
+
|
|
635
|
+
/** Options with the runs read enabled: the lib types the run mounts and the converter returns app-owned keys only. */
|
|
636
|
+
export type RunsOptions<T> = Omit<Options<T>, "converter" | "runs"> & {
|
|
637
|
+
/** `true` reads the run mounts off the state root; a selector picks a nested mount. */
|
|
638
|
+
runs: true | ((state: T) => unknown);
|
|
639
|
+
converter: RunsStateConverter<T>;
|
|
640
|
+
/** Folds app-owned keys for an unsettled command into the draft; runs after the lib's run-dialect reducer. */
|
|
641
|
+
optimistic?:
|
|
642
|
+
| StatewireClient.Optimistic<
|
|
643
|
+
T,
|
|
644
|
+
Record<string, (...args: any[]) => unknown>
|
|
645
|
+
>
|
|
646
|
+
| undefined;
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
declare module "@assistant-ui/react" {
|
|
651
|
+
namespace Assistant {
|
|
652
|
+
interface Commands {
|
|
653
|
+
runEnqueue: StatewireThread.RunEnqueueCommand;
|
|
654
|
+
runSteer: StatewireThread.RunSteerCommand;
|
|
655
|
+
runDequeue: StatewireThread.RunDequeueCommand;
|
|
656
|
+
runEdit: StatewireThread.RunEditCommand;
|
|
657
|
+
runContinue: StatewireThread.RunContinueCommand;
|
|
658
|
+
runReload: StatewireThread.RunReloadCommand;
|
|
659
|
+
runInput: StatewireThread.RunInputCommand;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
521
662
|
}
|
|
522
663
|
|
|
523
664
|
// Scope transforms are keyed by the underlying hook, not the resource wrapper.
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
export type TestState = {
|
|
12
12
|
messages: { id: string; role: "user" | "assistant"; text: string }[];
|
|
13
13
|
todos?: string[];
|
|
14
|
+
isRunning?: boolean;
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
export const INITIAL_STATE: TestState = { messages: [] };
|
|
@@ -34,24 +35,24 @@ export const toThreadMessage = (m: {
|
|
|
34
35
|
|
|
35
36
|
export const defaultConverter: AssistantTransportThread.StateConverter<
|
|
36
37
|
TestState
|
|
37
|
-
> = (state
|
|
38
|
-
messages:
|
|
39
|
-
|
|
40
|
-
...meta.pendingCommands.flatMap((c, i) =>
|
|
41
|
-
c.type === "run/steer" && "message" in c
|
|
42
|
-
? [
|
|
43
|
-
toThreadMessage({
|
|
44
|
-
id: `optimistic-${i}`,
|
|
45
|
-
role: "user",
|
|
46
|
-
text: c.message.parts.find((p) => p.type === "text")?.text ?? "",
|
|
47
|
-
}),
|
|
48
|
-
]
|
|
49
|
-
: [],
|
|
50
|
-
),
|
|
51
|
-
],
|
|
52
|
-
isRunning: meta.isSending,
|
|
38
|
+
> = (state) => ({
|
|
39
|
+
messages: state.messages.map(toThreadMessage),
|
|
40
|
+
isRunning: state.isRunning ?? false,
|
|
53
41
|
});
|
|
54
42
|
|
|
43
|
+
export const defaultOptimistic: AssistantTransportThread.Optimistic<
|
|
44
|
+
TestState
|
|
45
|
+
> = (draft, command, ctx) => {
|
|
46
|
+
draft.isRunning = true;
|
|
47
|
+
if (command.type === "run/steer" && "message" in command) {
|
|
48
|
+
draft.messages.push({
|
|
49
|
+
id: `optimistic-${ctx.seq}`,
|
|
50
|
+
role: "user",
|
|
51
|
+
text: command.message.parts.find((p) => p.type === "text")?.text ?? "",
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
55
56
|
export const sseEvent = (chunk: unknown) =>
|
|
56
57
|
`data: ${JSON.stringify(chunk)}\n\n`;
|
|
57
58
|
export const DONE = "data: [DONE]\n\n";
|
|
@@ -149,6 +150,7 @@ export const mountThread = (
|
|
|
149
150
|
api: "https://example.com/api",
|
|
150
151
|
headers: {},
|
|
151
152
|
converter: defaultConverter,
|
|
153
|
+
optimistic: defaultOptimistic,
|
|
152
154
|
protocol: "assistant-transport",
|
|
153
155
|
...options,
|
|
154
156
|
}),
|
|
@@ -181,46 +183,44 @@ export const threadTexts = (aui: () => ReturnType<typeof useAui>) =>
|
|
|
181
183
|
m.parts.flatMap((p) => (p.type === "text" ? [p.text] : [])).join(""),
|
|
182
184
|
);
|
|
183
185
|
|
|
184
|
-
export const toolCallConverter =
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
inputRequests?: AssistantTransportThread.InputRequestState[];
|
|
190
|
-
};
|
|
191
|
-
return (state, meta) => {
|
|
192
|
-
metas.push(meta);
|
|
193
|
-
const base = {
|
|
194
|
-
...defaultConverter(state, meta),
|
|
195
|
-
inputRequests: (state as ToolCallState).inputRequests ?? [],
|
|
186
|
+
export const toolCallConverter =
|
|
187
|
+
(): AssistantTransportThread.StateConverter<TestState> => {
|
|
188
|
+
type ToolCallState = TestState & {
|
|
189
|
+
toolCall?: { id: string; name: string; argsText: string };
|
|
190
|
+
inputRequests?: AssistantTransportThread.InputRequestState[];
|
|
196
191
|
};
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
192
|
+
return (state) => {
|
|
193
|
+
const base = {
|
|
194
|
+
...defaultConverter(state),
|
|
195
|
+
inputRequests: (state as ToolCallState).inputRequests ?? [],
|
|
196
|
+
};
|
|
197
|
+
const toolCall = (state as ToolCallState).toolCall;
|
|
198
|
+
if (!toolCall) return base;
|
|
199
|
+
return {
|
|
200
|
+
...base,
|
|
201
|
+
messages: [
|
|
202
|
+
...base.messages,
|
|
203
|
+
{
|
|
204
|
+
id: "a-tool",
|
|
205
|
+
role: "assistant",
|
|
206
|
+
createdAt: new Date(0),
|
|
207
|
+
content: [
|
|
208
|
+
{
|
|
209
|
+
type: "tool-call",
|
|
210
|
+
toolCallId: toolCall.id,
|
|
211
|
+
toolName: toolCall.name,
|
|
212
|
+
args: JSON.parse(toolCall.argsText),
|
|
213
|
+
argsText: toolCall.argsText,
|
|
214
|
+
},
|
|
215
|
+
],
|
|
216
|
+
attachments: [],
|
|
217
|
+
metadata: { custom: {} },
|
|
218
|
+
status: { type: "requires-action", reason: "tool-call" },
|
|
219
|
+
} as unknown as ExternalThreadMessage,
|
|
220
|
+
],
|
|
221
|
+
};
|
|
221
222
|
};
|
|
222
223
|
};
|
|
223
|
-
};
|
|
224
224
|
|
|
225
225
|
export const emitToolCall = (server: StreamResponse) => {
|
|
226
226
|
server.emit({
|
package/src/athena.e2e.test.tsx
CHANGED
|
@@ -33,6 +33,7 @@ type LangGraphState = {
|
|
|
33
33
|
messages: LangGraphMessage[];
|
|
34
34
|
todos: string[];
|
|
35
35
|
inputRequests?: AssistantTransportThread.InputRequestState[];
|
|
36
|
+
isRunning?: boolean;
|
|
36
37
|
};
|
|
37
38
|
|
|
38
39
|
const INITIAL_STATE: LangGraphState = { messages: [], todos: [] };
|
|
@@ -64,9 +65,29 @@ const createConverter = () => {
|
|
|
64
65
|
return id;
|
|
65
66
|
};
|
|
66
67
|
|
|
68
|
+
// The optimistic fold stamps isRunning and inserts the pending user
|
|
69
|
+
// message unless the server already echoed its trackingId.
|
|
70
|
+
const optimistic: AssistantTransportThread.Optimistic<LangGraphState> = (
|
|
71
|
+
draft,
|
|
72
|
+
command,
|
|
73
|
+
) => {
|
|
74
|
+
draft.isRunning = true;
|
|
75
|
+
if (!isUserMessageCommand(command)) return;
|
|
76
|
+
const trackingId = trackingIdOf(command);
|
|
77
|
+
const echoed = draft.messages.some(
|
|
78
|
+
(m) => m.type === "human" && m.trackingId === trackingId,
|
|
79
|
+
);
|
|
80
|
+
if (echoed) return;
|
|
81
|
+
draft.messages.push({
|
|
82
|
+
id: `optimistic-${trackingId}`,
|
|
83
|
+
type: "human",
|
|
84
|
+
content: command.message.parts.find((p) => p.type === "text")?.text ?? "",
|
|
85
|
+
trackingId,
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
|
|
67
89
|
const converter: AssistantTransportThread.StateConverter<LangGraphState> = (
|
|
68
90
|
state,
|
|
69
|
-
meta,
|
|
70
91
|
) => {
|
|
71
92
|
const toolResults = new Map<string, string>();
|
|
72
93
|
for (const m of state.messages) {
|
|
@@ -115,38 +136,19 @@ const createConverter = () => {
|
|
|
115
136
|
}
|
|
116
137
|
}
|
|
117
138
|
|
|
118
|
-
for (const c of meta.pendingCommands) {
|
|
119
|
-
if (!isUserMessageCommand(c)) continue;
|
|
120
|
-
const trackingId = trackingIdOf(c);
|
|
121
|
-
const echoed = state.messages.some(
|
|
122
|
-
(m) => m.type === "human" && m.trackingId === trackingId,
|
|
123
|
-
);
|
|
124
|
-
if (echoed) continue;
|
|
125
|
-
messages.push({
|
|
126
|
-
id: `optimistic-${trackingId}`,
|
|
127
|
-
role: "user",
|
|
128
|
-
createdAt: new Date(0),
|
|
129
|
-
content: c.message.parts.flatMap((p) =>
|
|
130
|
-
p.type === "text" ? [{ type: "text", text: p.text }] : [],
|
|
131
|
-
),
|
|
132
|
-
attachments: [],
|
|
133
|
-
metadata: { custom: { trackingId } },
|
|
134
|
-
} as unknown as ExternalThreadMessage);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
139
|
return {
|
|
138
140
|
messages,
|
|
139
|
-
isRunning:
|
|
141
|
+
isRunning: state.isRunning ?? false,
|
|
140
142
|
state: { todos: state.todos },
|
|
141
143
|
inputRequests: state.inputRequests ?? [],
|
|
142
144
|
};
|
|
143
145
|
};
|
|
144
146
|
|
|
145
|
-
return { converter, trackingIdOf };
|
|
147
|
+
return { converter, optimistic, trackingIdOf };
|
|
146
148
|
};
|
|
147
149
|
|
|
148
150
|
const mountDeepAgent = () => {
|
|
149
|
-
const { converter, trackingIdOf } = createConverter();
|
|
151
|
+
const { converter, optimistic, trackingIdOf } = createConverter();
|
|
150
152
|
const cancelled: AssistantTransportThread.Command[][] = [];
|
|
151
153
|
const errored: AssistantTransportThread.Command[][] = [];
|
|
152
154
|
const responses: (string | null)[] = [];
|
|
@@ -168,6 +170,7 @@ const mountDeepAgent = () => {
|
|
|
168
170
|
resumeApi: "https://sync.example.com/api/resume",
|
|
169
171
|
protocol: "assistant-transport",
|
|
170
172
|
converter,
|
|
173
|
+
optimistic,
|
|
171
174
|
capabilities: { edit: true },
|
|
172
175
|
headers: async () => ({
|
|
173
176
|
Authorization: "Bearer test-token",
|