@assistant-ui/react-statewire 0.4.4 → 0.5.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 +3 -0
- package/dist/AssistantTransportThread.d.ts.map +1 -1
- package/dist/AssistantTransportThread.js +12 -16
- 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 +10 -11
- package/src/StatewireThread.runs.test.tsx +405 -0
- package/src/StatewireThread.test.tsx +177 -1
- package/src/StatewireThread.ts +218 -77
- package/src/hooks.ts +12 -0
- package/src/index.ts +1 -0
- package/src/runCommands.ts +28 -12
- package/src/runsOptimistic.test.ts +225 -0
- package/src/runsOptimistic.ts +164 -0
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.
|
package/src/hooks.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { useAui, useAuiState } from "@assistant-ui/store";
|
|
|
4
4
|
import { asAssistantTransportExtras } from "./AssistantTransportThread";
|
|
5
5
|
import type { Assistant } from "@assistant-ui/react";
|
|
6
6
|
import type { AssistantTransportThread } from "./AssistantTransportThread";
|
|
7
|
+
import type { StatewireThread } from "./StatewireThread";
|
|
7
8
|
|
|
8
9
|
type UserExternalState = keyof Assistant.ExternalState extends never
|
|
9
10
|
? Record<string, unknown>
|
|
@@ -18,6 +19,17 @@ export const useAssistantTransportSendCommand = () => {
|
|
|
18
19
|
};
|
|
19
20
|
};
|
|
20
21
|
|
|
22
|
+
/** The typed runs read; requires StatewireThread with the `runs` option. */
|
|
23
|
+
export const useStatewireRuns = (): StatewireThread.Runs =>
|
|
24
|
+
useAuiState((s) => {
|
|
25
|
+
const { runs } = asAssistantTransportExtras(s.thread.extras);
|
|
26
|
+
if (runs === undefined)
|
|
27
|
+
throw new Error(
|
|
28
|
+
"statewire: useStatewireRuns requires StatewireThread with the runs option enabled",
|
|
29
|
+
);
|
|
30
|
+
return runs;
|
|
31
|
+
});
|
|
32
|
+
|
|
21
33
|
export function useAssistantTransportState(): UserExternalState;
|
|
22
34
|
export function useAssistantTransportState<T>(
|
|
23
35
|
selector: (state: UserExternalState) => T,
|
package/src/index.ts
CHANGED
package/src/runCommands.ts
CHANGED
|
@@ -80,6 +80,18 @@ export const viewedLeafId = (
|
|
|
80
80
|
messages: readonly { id: string }[],
|
|
81
81
|
): string | null => messages.at(-1)?.id ?? null;
|
|
82
82
|
|
|
83
|
+
export const resolveEditCommand = (
|
|
84
|
+
message: AppendMessage,
|
|
85
|
+
): AssistantTransportThread.RunEditCommand => {
|
|
86
|
+
if (message.sourceId == null)
|
|
87
|
+
throw new Error("statewire: run/edit requires a sourceId");
|
|
88
|
+
return {
|
|
89
|
+
type: "run/edit",
|
|
90
|
+
message: toWireMessage(message, crypto.randomUUID()),
|
|
91
|
+
sourceId: message.sourceId,
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
83
95
|
export const createRunQueueAdapter = (
|
|
84
96
|
messages: readonly { id: string }[],
|
|
85
97
|
queueItems: readonly AssistantTransportThread.RunQueueItem[],
|
|
@@ -92,21 +104,25 @@ export const createRunQueueAdapter = (
|
|
|
92
104
|
if (queueItems.some((item) => item.id === queueItemId)) return "queue";
|
|
93
105
|
throw new Error(`statewire: unknown queue item "${queueItemId}"`);
|
|
94
106
|
};
|
|
107
|
+
// edit-composer sends carry a sourceId; they bypass the lanes into run/edit
|
|
108
|
+
const sendCommand = (
|
|
109
|
+
lane: "queue" | "steer",
|
|
110
|
+
message: AppendMessage,
|
|
111
|
+
): AssistantTransportThread.RunCommand => {
|
|
112
|
+
if (message.sourceId != null) return resolveEditCommand(message);
|
|
113
|
+
const fields = {
|
|
114
|
+
message: toWireMessage(message, crypto.randomUUID()),
|
|
115
|
+
anchorMessageId: viewedLeafId(messages),
|
|
116
|
+
};
|
|
117
|
+
return lane === "steer"
|
|
118
|
+
? { type: "run/steer", ...fields }
|
|
119
|
+
: { type: "run/enqueue", ...fields };
|
|
120
|
+
};
|
|
95
121
|
return {
|
|
96
122
|
items: queueItems.map(toQueueItemState),
|
|
97
123
|
steerItems: (steerQueueItems ?? []).map(toQueueItemState),
|
|
98
|
-
enqueue: (message) =>
|
|
99
|
-
|
|
100
|
-
type: "run/enqueue",
|
|
101
|
-
message: toWireMessage(message, crypto.randomUUID()),
|
|
102
|
-
anchorMessageId: viewedLeafId(messages),
|
|
103
|
-
}),
|
|
104
|
-
steer: (message) =>
|
|
105
|
-
dispatch({
|
|
106
|
-
type: "run/steer",
|
|
107
|
-
message: toWireMessage(message, crypto.randomUUID()),
|
|
108
|
-
anchorMessageId: viewedLeafId(messages),
|
|
109
|
-
}),
|
|
124
|
+
enqueue: (message) => dispatch(sendCommand("queue", message)),
|
|
125
|
+
steer: (message) => dispatch(sendCommand("steer", message)),
|
|
110
126
|
move: (queueItemId, placement) => {
|
|
111
127
|
const lane = placement.lane ?? laneOf(queueItemId);
|
|
112
128
|
const fields = {
|