@assistant-ui/react-statewire 0.9.0 → 0.11.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 -0
- package/dist/AssistantTransportThread.d.ts.map +1 -1
- package/dist/AssistantTransportThread.js +40 -15
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +3 -2
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +20 -25
- package/dist/StatewireThread.js.map +1 -1
- package/dist/runCommands.d.ts +2 -4
- package/dist/runCommands.d.ts.map +1 -1
- package/dist/runCommands.js +1 -14
- package/dist/runCommands.js.map +1 -1
- package/package.json +2 -2
- package/src/AssistantTransportThread.ts +59 -23
- package/src/StatewireThread.runs.test.tsx +125 -37
- package/src/StatewireThread.test.tsx +1 -1
- package/src/StatewireThread.ts +47 -44
- package/src/runCommands.ts +0 -22
- package/src/transport.test.tsx +157 -0
|
@@ -67,6 +67,23 @@ const withRun = (
|
|
|
67
67
|
],
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
+
const rollbackMaterialize = (draft: RunsServerState): void =>
|
|
71
|
+
runsMaterialize(draft, {
|
|
72
|
+
append: (m) =>
|
|
73
|
+
draft.messages.push({
|
|
74
|
+
id: m.id,
|
|
75
|
+
role: m.role,
|
|
76
|
+
text: m.parts.map((p) => (p.type === "text" ? p.text : "")).join(""),
|
|
77
|
+
}),
|
|
78
|
+
rollback: (to) => {
|
|
79
|
+
if (to === null) {
|
|
80
|
+
draft.messages = [];
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
draft.messages.splice(draft.messages.findIndex((m) => m.id === to) + 1);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
70
87
|
const converter: StatewireThread.RunsStateConverter<RunsServerState> = (
|
|
71
88
|
state,
|
|
72
89
|
) => ({
|
|
@@ -101,12 +118,23 @@ const createServer = (init?: Partial<RunsServerState>) => {
|
|
|
101
118
|
};
|
|
102
119
|
|
|
103
120
|
// A remote SSE fake that delivers the snapshot and acks command posts but
|
|
104
|
-
// never settles them, so optimistic overlays stay visible.
|
|
105
|
-
|
|
121
|
+
// never settles them, so optimistic overlays stay visible. `hold` parks the
|
|
122
|
+
// snapshot until `release()` to expose the pre-attach window.
|
|
123
|
+
const createRemoteServer = (
|
|
124
|
+
init?: Partial<RunsServerState>,
|
|
125
|
+
opts?: { hold?: boolean },
|
|
126
|
+
) => {
|
|
106
127
|
const received: { method: string; params: unknown[] }[] = [];
|
|
107
128
|
const clients = new Map<string, number>();
|
|
108
129
|
const encoder = new TextEncoder();
|
|
109
130
|
const frame = (chunk: unknown) => `data: ${JSON.stringify(chunk)}\n\n`;
|
|
131
|
+
let released = !(opts?.hold ?? false);
|
|
132
|
+
let parkedSnapshot: (() => void) | undefined;
|
|
133
|
+
const release = () => {
|
|
134
|
+
released = true;
|
|
135
|
+
parkedSnapshot?.();
|
|
136
|
+
parkedSnapshot = undefined;
|
|
137
|
+
};
|
|
110
138
|
const fetchImpl = (async (input: RequestInfo | URL, init2?: RequestInit) => {
|
|
111
139
|
const url = String(input);
|
|
112
140
|
const headers = new Headers(init2?.headers);
|
|
@@ -116,20 +144,23 @@ const createRemoteServer = (init?: Partial<RunsServerState>) => {
|
|
|
116
144
|
if (known === undefined) clients.set(clientId, 0);
|
|
117
145
|
const body = new ReadableStream<Uint8Array>({
|
|
118
146
|
start: (c) => {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
147
|
+
const sendSnapshot = () =>
|
|
148
|
+
c.enqueue(
|
|
149
|
+
encoder.encode(
|
|
150
|
+
frame({
|
|
151
|
+
ops: [
|
|
152
|
+
{
|
|
153
|
+
op: "replace",
|
|
154
|
+
path: [],
|
|
155
|
+
value: { ...INITIAL_STATE, ...init },
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
syn: { lastSeq: known ?? -1, lease: "lease-1" },
|
|
159
|
+
}),
|
|
160
|
+
),
|
|
161
|
+
);
|
|
162
|
+
if (released) sendSnapshot();
|
|
163
|
+
else parkedSnapshot = sendSnapshot;
|
|
133
164
|
},
|
|
134
165
|
});
|
|
135
166
|
return new Response(body, {
|
|
@@ -143,6 +174,7 @@ const createRemoteServer = (init?: Partial<RunsServerState>) => {
|
|
|
143
174
|
}) as typeof fetch;
|
|
144
175
|
return {
|
|
145
176
|
received,
|
|
177
|
+
release,
|
|
146
178
|
transport: StatewireSSE<RunsServerState>({
|
|
147
179
|
url: "http://fake/api",
|
|
148
180
|
fetch: fetchImpl,
|
|
@@ -323,7 +355,7 @@ describe("StatewireThread runs mode", () => {
|
|
|
323
355
|
);
|
|
324
356
|
});
|
|
325
357
|
|
|
326
|
-
it("
|
|
358
|
+
it("keeps dispatch limbo out of messages when no materialize option is set", async () => {
|
|
327
359
|
const server = createServer({
|
|
328
360
|
messages: [{ id: "m1", role: "user", text: "hi" }],
|
|
329
361
|
...withRun({
|
|
@@ -332,7 +364,6 @@ describe("StatewireThread runs mode", () => {
|
|
|
332
364
|
dispatching: {
|
|
333
365
|
trigger: "message-send",
|
|
334
366
|
messages: [
|
|
335
|
-
{ id: "m1", role: "user", parts: [{ type: "text", text: "hi" }] },
|
|
336
367
|
{ id: "d1", role: "user", parts: [{ type: "text", text: "go" }] },
|
|
337
368
|
],
|
|
338
369
|
},
|
|
@@ -340,25 +371,16 @@ describe("StatewireThread runs mode", () => {
|
|
|
340
371
|
});
|
|
341
372
|
const { aui, runs } = mountThread({ transport: server.transport });
|
|
342
373
|
|
|
343
|
-
await waitFor(() => expect(runs().dispatching?.messages).toHaveLength(
|
|
344
|
-
const messages = aui().thread().getState().messages;
|
|
345
|
-
expect(messages.map((m) => m.id)).toEqual(["m1", "d1"]);
|
|
346
|
-
expect(messages[1]!.role).toBe("user");
|
|
347
|
-
|
|
348
|
-
act(() => {
|
|
349
|
-
server.cell.state!.messages.push({ id: "d1", role: "user", text: "go" });
|
|
350
|
-
server.cell.state!.runs[0]!.dispatching = null;
|
|
351
|
-
});
|
|
352
|
-
await waitFor(() => expect(runs().dispatching).toBeNull());
|
|
374
|
+
await waitFor(() => expect(runs().dispatching?.messages).toHaveLength(1));
|
|
353
375
|
expect(
|
|
354
376
|
aui()
|
|
355
377
|
.thread()
|
|
356
378
|
.getState()
|
|
357
379
|
.messages.map((m) => m.id),
|
|
358
|
-
).toEqual(["m1"
|
|
380
|
+
).toEqual(["m1"]);
|
|
359
381
|
});
|
|
360
382
|
|
|
361
|
-
it("
|
|
383
|
+
it("projects the dispatch record through the materialize fold", async () => {
|
|
362
384
|
const server = createServer({
|
|
363
385
|
messages: [{ id: "m1", role: "user", text: "hi" }],
|
|
364
386
|
...withRun({
|
|
@@ -390,7 +412,7 @@ describe("StatewireThread runs mode", () => {
|
|
|
390
412
|
await waitFor(() => expect(runs().dispatching?.messages).toHaveLength(1));
|
|
391
413
|
const messages = aui().thread().getState().messages;
|
|
392
414
|
expect(messages.map((m) => m.id)).toEqual(["m1", "d1"]);
|
|
393
|
-
// Epoch createdAt proves d1 came through the converter
|
|
415
|
+
// Epoch createdAt proves d1 came through the converter (state-side fold).
|
|
394
416
|
expect(messages[1]!.createdAt.getTime()).toBe(0);
|
|
395
417
|
|
|
396
418
|
act(() => {
|
|
@@ -406,7 +428,7 @@ describe("StatewireThread runs mode", () => {
|
|
|
406
428
|
).toEqual(["m1", "d1"]);
|
|
407
429
|
});
|
|
408
430
|
|
|
409
|
-
it("renders the edit replacement in branch position
|
|
431
|
+
it("renders the edit replacement in branch position through the materialize fold", async () => {
|
|
410
432
|
const server = createServer({
|
|
411
433
|
messages: [
|
|
412
434
|
{ id: "u1", role: "user", text: "one" },
|
|
@@ -430,7 +452,10 @@ describe("StatewireThread runs mode", () => {
|
|
|
430
452
|
},
|
|
431
453
|
}),
|
|
432
454
|
});
|
|
433
|
-
const { aui, runs } = mountThread({
|
|
455
|
+
const { aui, runs } = mountThread({
|
|
456
|
+
transport: server.transport,
|
|
457
|
+
materialize: rollbackMaterialize,
|
|
458
|
+
});
|
|
434
459
|
|
|
435
460
|
await waitFor(() => expect(runs().status).toBe("running"));
|
|
436
461
|
expect(
|
|
@@ -441,7 +466,7 @@ describe("StatewireThread runs mode", () => {
|
|
|
441
466
|
).toEqual(["u1", "a1", "u2b"]);
|
|
442
467
|
});
|
|
443
468
|
|
|
444
|
-
it("renders the rolled-back view for a reload dispatch", async () => {
|
|
469
|
+
it("renders the rolled-back view for a reload dispatch through the materialize fold", async () => {
|
|
445
470
|
const server = createServer({
|
|
446
471
|
messages: [
|
|
447
472
|
{ id: "u1", role: "user", text: "one" },
|
|
@@ -457,7 +482,10 @@ describe("StatewireThread runs mode", () => {
|
|
|
457
482
|
},
|
|
458
483
|
}),
|
|
459
484
|
});
|
|
460
|
-
const { aui, runs } = mountThread({
|
|
485
|
+
const { aui, runs } = mountThread({
|
|
486
|
+
transport: server.transport,
|
|
487
|
+
materialize: rollbackMaterialize,
|
|
488
|
+
});
|
|
461
489
|
|
|
462
490
|
await waitFor(() => expect(runs().status).toBe("running"));
|
|
463
491
|
expect(
|
|
@@ -490,7 +518,10 @@ describe("StatewireThread runs mode", () => {
|
|
|
490
518
|
},
|
|
491
519
|
}),
|
|
492
520
|
});
|
|
493
|
-
const { aui, runs } = mountThread({
|
|
521
|
+
const { aui, runs } = mountThread({
|
|
522
|
+
transport: server.transport,
|
|
523
|
+
materialize: rollbackMaterialize,
|
|
524
|
+
});
|
|
494
525
|
|
|
495
526
|
await waitFor(() => expect(runs().status).toBe("running"));
|
|
496
527
|
expect(
|
|
@@ -530,7 +561,10 @@ describe("StatewireThread runs mode", () => {
|
|
|
530
561
|
|
|
531
562
|
it("flips the run optimistically for a dispatching send", async () => {
|
|
532
563
|
const server = createRemoteServer();
|
|
533
|
-
const { aui, runs } = mountThread({
|
|
564
|
+
const { aui, runs } = mountThread({
|
|
565
|
+
transport: server.transport,
|
|
566
|
+
materialize: rollbackMaterialize,
|
|
567
|
+
});
|
|
534
568
|
|
|
535
569
|
await waitFor(() =>
|
|
536
570
|
expect(aui().thread().getState().isLoading).toBe(false),
|
|
@@ -551,6 +585,60 @@ describe("StatewireThread runs mode", () => {
|
|
|
551
585
|
expect(aui().thread().getState().isRunning).toBe(true);
|
|
552
586
|
});
|
|
553
587
|
|
|
588
|
+
it("preloads initialState: interactive before attach, sends fold in", async () => {
|
|
589
|
+
const server = createRemoteServer({}, { hold: true });
|
|
590
|
+
const { aui, runs } = mountThread({
|
|
591
|
+
transport: server.transport,
|
|
592
|
+
materialize: rollbackMaterialize,
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
expect(aui().thread().getState().isLoading).toBe(false);
|
|
596
|
+
act(() => {
|
|
597
|
+
aui().thread().append("hello");
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
// The pre-attach send renders through the same optimistic + materialize folds.
|
|
601
|
+
await waitFor(() => expect(runs().status).toBe("running"));
|
|
602
|
+
expect(runs().runId).toMatch(/^optimistic-/);
|
|
603
|
+
expect(aui().thread().getState().messages).toHaveLength(1);
|
|
604
|
+
expect(server.received).toEqual([]);
|
|
605
|
+
|
|
606
|
+
server.release();
|
|
607
|
+
|
|
608
|
+
// Attach posts the queued command; the still-unacked overlay survives the snapshot.
|
|
609
|
+
await waitFor(() =>
|
|
610
|
+
expect(server.received.map((c) => c.method)).toEqual(["run/enqueue"]),
|
|
611
|
+
);
|
|
612
|
+
expect(aui().thread().getState().messages).toHaveLength(1);
|
|
613
|
+
expect(runs().status).toBe("running");
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
it("stays loading until the snapshot when initialState is omitted", async () => {
|
|
617
|
+
const server = createRemoteServer(
|
|
618
|
+
{ messages: [{ id: "m1", role: "user", text: "hi" }] },
|
|
619
|
+
{ hold: true },
|
|
620
|
+
);
|
|
621
|
+
const { aui } = mountThread({
|
|
622
|
+
transport: server.transport,
|
|
623
|
+
initialState: undefined,
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
expect(aui().thread().getState().isLoading).toBe(true);
|
|
627
|
+
expect(aui().thread().getState().messages).toEqual([]);
|
|
628
|
+
|
|
629
|
+
server.release();
|
|
630
|
+
|
|
631
|
+
await waitFor(() =>
|
|
632
|
+
expect(aui().thread().getState().isLoading).toBe(false),
|
|
633
|
+
);
|
|
634
|
+
expect(
|
|
635
|
+
aui()
|
|
636
|
+
.thread()
|
|
637
|
+
.getState()
|
|
638
|
+
.messages.map((m) => m.id),
|
|
639
|
+
).toEqual(["m1"]);
|
|
640
|
+
});
|
|
641
|
+
|
|
554
642
|
it("runs the app optimistic reducer after the lib's", async () => {
|
|
555
643
|
const server = createRemoteServer();
|
|
556
644
|
const observedStatus: string[] = [];
|
|
@@ -293,7 +293,7 @@ describe("StatewireThread", () => {
|
|
|
293
293
|
|
|
294
294
|
expect(observed[0]).toBe(initialState);
|
|
295
295
|
expect(threadTexts(aui)).toEqual(["local"]);
|
|
296
|
-
expect(aui().thread().getState().isLoading).toBe(
|
|
296
|
+
expect(aui().thread().getState().isLoading).toBe(false);
|
|
297
297
|
|
|
298
298
|
cell.attach!();
|
|
299
299
|
|
package/src/StatewireThread.ts
CHANGED
|
@@ -32,7 +32,6 @@ import {
|
|
|
32
32
|
createRunQueueAdapter,
|
|
33
33
|
resolveEditCommand,
|
|
34
34
|
resolveReloadCommand,
|
|
35
|
-
toDispatchingMessage,
|
|
36
35
|
toWireMessage,
|
|
37
36
|
viewedLeafId,
|
|
38
37
|
} from "./runCommands";
|
|
@@ -48,6 +47,8 @@ type AnyOptions =
|
|
|
48
47
|
| StatewireThread.Options<any>
|
|
49
48
|
| StatewireThread.RunsOptions<any>;
|
|
50
49
|
|
|
50
|
+
const LOADING_STATE: StatewireThread.State = { messages: [], isRunning: false };
|
|
51
|
+
|
|
51
52
|
const READY_RUNS: StatewireThread.Runs = {
|
|
52
53
|
status: "ready",
|
|
53
54
|
runId: null,
|
|
@@ -103,13 +104,20 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
103
104
|
" on first render and changed since",
|
|
104
105
|
);
|
|
105
106
|
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
107
|
+
const [initialSeam] = useState(() => options.initialState !== undefined);
|
|
108
|
+
if (initialSeam !== (options.initialState !== undefined))
|
|
109
|
+
throw new Error(
|
|
110
|
+
"statewire: the initialState option must be stable — it was " +
|
|
111
|
+
(initialSeam ? "set" : "unset") +
|
|
112
|
+
" on first render and changed since",
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const { state, commands, connection, setContext } = useStatewire<
|
|
116
|
+
unknown,
|
|
117
|
+
AnyCommands
|
|
118
|
+
>({
|
|
112
119
|
transport: options.transport,
|
|
120
|
+
initialState: options.initialState,
|
|
113
121
|
...(runsSeam && {
|
|
114
122
|
optimistic: (draft, command, ctx) => {
|
|
115
123
|
const runsOpt = latest.current.runs!;
|
|
@@ -136,8 +144,6 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
136
144
|
onConnectionChange: (connection) =>
|
|
137
145
|
latest.current.onConnectionChange?.(connection),
|
|
138
146
|
});
|
|
139
|
-
const attached = transportState !== undefined;
|
|
140
|
-
const state = attached ? transportState : options.initialState;
|
|
141
147
|
|
|
142
148
|
const clientRef = useAssistantClientRef();
|
|
143
149
|
const setContextRef = useRef(setContext);
|
|
@@ -199,6 +205,7 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
199
205
|
|
|
200
206
|
const runs = useMemo((): StatewireThread.Runs | undefined => {
|
|
201
207
|
if (runsOption === undefined) return undefined;
|
|
208
|
+
if (state === undefined) return READY_RUNS;
|
|
202
209
|
const mount = (runsOption === true ? state : runsOption(state)) as
|
|
203
210
|
| StatewireThread.RunsState
|
|
204
211
|
| undefined;
|
|
@@ -229,36 +236,20 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
229
236
|
[pending, connection, runs],
|
|
230
237
|
);
|
|
231
238
|
const converted = useMemo(
|
|
232
|
-
() =>
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
+
(): StatewireThread.State =>
|
|
240
|
+
state === undefined
|
|
241
|
+
? LOADING_STATE
|
|
242
|
+
: (converter(
|
|
243
|
+
state,
|
|
244
|
+
metadata as StatewireThread.ConnectionMetadata & {
|
|
245
|
+
runs: StatewireThread.Runs;
|
|
246
|
+
},
|
|
247
|
+
) as StatewireThread.State),
|
|
239
248
|
[converter, state, metadata],
|
|
240
249
|
);
|
|
241
|
-
// Dispatch limbo
|
|
242
|
-
//
|
|
243
|
-
|
|
244
|
-
const messages = useMemo((): StatewireThread.State["messages"] => {
|
|
245
|
-
const record = runs?.dispatching ?? null;
|
|
246
|
-
if (record === null || materializeSeam) return converted.messages;
|
|
247
|
-
let base = converted.messages;
|
|
248
|
-
if ("rollbackTo" in record) {
|
|
249
|
-
base =
|
|
250
|
-
record.rollbackTo == null
|
|
251
|
-
? []
|
|
252
|
-
: base.slice(
|
|
253
|
-
0,
|
|
254
|
-
base.findIndex((m) => m.id === record.rollbackTo) + 1,
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
const known = new Set(base.map((m) => m.id));
|
|
258
|
-
const pending = record.messages.filter((e) => !known.has(e.id));
|
|
259
|
-
if (pending.length === 0) return base;
|
|
260
|
-
return [...base, ...pending.map(toDispatchingMessage)];
|
|
261
|
-
}, [converted.messages, runs, materializeSeam]);
|
|
250
|
+
// Dispatch limbo is a materialize concern: the fold makes the record
|
|
251
|
+
// concrete in state before conversion.
|
|
252
|
+
const messages = converted.messages;
|
|
262
253
|
const queueItems = runs !== undefined ? runs.queue : converted.queue;
|
|
263
254
|
const steerQueueItems =
|
|
264
255
|
runs !== undefined ? runs.steerQueue : converted.steerQueue;
|
|
@@ -266,10 +257,16 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
266
257
|
throw new Error(
|
|
267
258
|
"statewire: the converter's steerQueue projection requires the queue projection",
|
|
268
259
|
);
|
|
260
|
+
// Seams anchor at the first real convert; the loading placeholder projects nothing.
|
|
269
261
|
const [queueSeam] = useState(() => ({
|
|
270
|
-
present:
|
|
262
|
+
present: undefined as boolean | undefined,
|
|
271
263
|
}));
|
|
272
|
-
if (queueSeam.present
|
|
264
|
+
if (state !== undefined && queueSeam.present === undefined)
|
|
265
|
+
queueSeam.present = queueItems !== undefined;
|
|
266
|
+
if (
|
|
267
|
+
queueSeam.present !== undefined &&
|
|
268
|
+
queueSeam.present !== (queueItems !== undefined)
|
|
269
|
+
)
|
|
273
270
|
throw new Error(
|
|
274
271
|
"statewire: the converter's queue projection must be stable — it was " +
|
|
275
272
|
(queueSeam.present ? "defined" : "undefined") +
|
|
@@ -293,9 +290,14 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
293
290
|
const inputRequests =
|
|
294
291
|
runs !== undefined ? (runs.inputRequests ?? []) : converted.inputRequests;
|
|
295
292
|
const [inputSeam] = useState(() => ({
|
|
296
|
-
present:
|
|
293
|
+
present: undefined as boolean | undefined,
|
|
297
294
|
}));
|
|
298
|
-
if (inputSeam.present
|
|
295
|
+
if (state !== undefined && inputSeam.present === undefined)
|
|
296
|
+
inputSeam.present = inputRequests !== undefined;
|
|
297
|
+
if (
|
|
298
|
+
inputSeam.present !== undefined &&
|
|
299
|
+
inputSeam.present !== (inputRequests !== undefined)
|
|
300
|
+
)
|
|
299
301
|
throw new Error(
|
|
300
302
|
"statewire: the converter's inputRequests projection must be stable — it was " +
|
|
301
303
|
(inputSeam.present ? "defined" : "undefined") +
|
|
@@ -340,7 +342,7 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
340
342
|
messages: messages as readonly ExternalThreadMessage[],
|
|
341
343
|
isRunning:
|
|
342
344
|
runs !== undefined ? runs.status === "running" : converted.isRunning,
|
|
343
|
-
isLoading:
|
|
345
|
+
isLoading: state === undefined,
|
|
344
346
|
state: converted.state,
|
|
345
347
|
extras,
|
|
346
348
|
attachmentAdapter: options.adapters?.attachments,
|
|
@@ -686,7 +688,8 @@ export namespace StatewireThread {
|
|
|
686
688
|
|
|
687
689
|
export type Options<T> = {
|
|
688
690
|
transport: Transport<T>;
|
|
689
|
-
|
|
691
|
+
/** Preloads the thread: published and interactive from the first render, replaced by the attach snapshot. Omitted, the thread stays loading until attach. */
|
|
692
|
+
initialState?: T | undefined;
|
|
690
693
|
converter: StateConverter<T>;
|
|
691
694
|
runs?: undefined;
|
|
692
695
|
/**
|
|
@@ -745,7 +748,7 @@ export namespace StatewireThread {
|
|
|
745
748
|
Record<string, (...args: any[]) => unknown>
|
|
746
749
|
>
|
|
747
750
|
| undefined;
|
|
748
|
-
/** Makes dispatch records concrete in the draft (runs after the optimistic folds);
|
|
751
|
+
/** Makes dispatch records concrete in the draft (runs after the optimistic folds); without it, dispatch limbo stays out of `messages`. */
|
|
749
752
|
materialize?: StatewireClient.Materialize<NonNullable<T>> | undefined;
|
|
750
753
|
};
|
|
751
754
|
}
|
package/src/runCommands.ts
CHANGED
|
@@ -2,10 +2,8 @@ import type {
|
|
|
2
2
|
AppendMessage,
|
|
3
3
|
ExternalThreadQueueAdapter,
|
|
4
4
|
QueuePlacement,
|
|
5
|
-
ThreadMessage,
|
|
6
5
|
} from "@assistant-ui/core";
|
|
7
6
|
import type { AssistantTransportThread } from "./AssistantTransportThread";
|
|
8
|
-
import type { StatewireThread } from "./StatewireThread";
|
|
9
7
|
|
|
10
8
|
export const toWireMessage = (
|
|
11
9
|
message: AppendMessage,
|
|
@@ -69,26 +67,6 @@ export const toQueueItemState = (
|
|
|
69
67
|
),
|
|
70
68
|
});
|
|
71
69
|
|
|
72
|
-
export const toDispatchingMessage = (
|
|
73
|
-
item: StatewireThread.RunsQueueEntry,
|
|
74
|
-
): ThreadMessage => ({
|
|
75
|
-
id: item.id,
|
|
76
|
-
role: "user",
|
|
77
|
-
createdAt: new Date(),
|
|
78
|
-
content: item.parts.map((part) =>
|
|
79
|
-
part.type === "text"
|
|
80
|
-
? part
|
|
81
|
-
: {
|
|
82
|
-
type: "file" as const,
|
|
83
|
-
data: part.url,
|
|
84
|
-
mimeType: part.mediaType,
|
|
85
|
-
...(part.filename !== undefined && { filename: part.filename }),
|
|
86
|
-
},
|
|
87
|
-
),
|
|
88
|
-
attachments: [],
|
|
89
|
-
metadata: { custom: {} },
|
|
90
|
-
});
|
|
91
|
-
|
|
92
70
|
const placementFields = (placement: QueuePlacement) => ({
|
|
93
71
|
...(placement.insertAfter !== undefined && {
|
|
94
72
|
insertAfter: placement.insertAfter,
|
package/src/transport.test.tsx
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
useAssistantTransportSendCommand,
|
|
21
21
|
useAssistantTransportState,
|
|
22
22
|
} from "./hooks";
|
|
23
|
+
import { runsMaterialize } from "./runsMaterialize";
|
|
23
24
|
|
|
24
25
|
import {
|
|
25
26
|
INITIAL_STATE,
|
|
@@ -34,6 +35,7 @@ import {
|
|
|
34
35
|
sseEvent,
|
|
35
36
|
threadTexts,
|
|
36
37
|
toolCallConverter,
|
|
38
|
+
toThreadMessage,
|
|
37
39
|
} from "./__tests__/testHelpers";
|
|
38
40
|
import type { StreamResponse, TestState } from "./__tests__/testHelpers";
|
|
39
41
|
|
|
@@ -2757,3 +2759,158 @@ describe("input requests", () => {
|
|
|
2757
2759
|
unmount();
|
|
2758
2760
|
});
|
|
2759
2761
|
});
|
|
2762
|
+
|
|
2763
|
+
describe("runs fold and materialize", () => {
|
|
2764
|
+
type RunsTestState = TestState & {
|
|
2765
|
+
status: "ready" | "running";
|
|
2766
|
+
runs: {
|
|
2767
|
+
runId: string | null;
|
|
2768
|
+
status: string;
|
|
2769
|
+
queue: { id: string }[];
|
|
2770
|
+
steerQueue: { id: string }[];
|
|
2771
|
+
dispatching: {
|
|
2772
|
+
trigger: string;
|
|
2773
|
+
messages: {
|
|
2774
|
+
id: string;
|
|
2775
|
+
role: "user";
|
|
2776
|
+
parts: { type: "text"; text: string }[];
|
|
2777
|
+
}[];
|
|
2778
|
+
rollbackTo?: string | null;
|
|
2779
|
+
} | null;
|
|
2780
|
+
error: null;
|
|
2781
|
+
}[];
|
|
2782
|
+
};
|
|
2783
|
+
|
|
2784
|
+
const runsConverter: AssistantTransportThread.StateConverter<
|
|
2785
|
+
RunsTestState
|
|
2786
|
+
> = (state) => ({
|
|
2787
|
+
messages: state.messages.map(toThreadMessage),
|
|
2788
|
+
isRunning: state.status === "running",
|
|
2789
|
+
});
|
|
2790
|
+
|
|
2791
|
+
const materialize = (draft: RunsTestState): void =>
|
|
2792
|
+
runsMaterialize(draft, {
|
|
2793
|
+
append: (m) =>
|
|
2794
|
+
draft.messages.push({
|
|
2795
|
+
id: m.id,
|
|
2796
|
+
role: m.role,
|
|
2797
|
+
text: m.parts.map((p) => (p.type === "text" ? p.text : "")).join(""),
|
|
2798
|
+
}),
|
|
2799
|
+
rollback: (to) => {
|
|
2800
|
+
if (to === null) {
|
|
2801
|
+
draft.messages = [];
|
|
2802
|
+
return;
|
|
2803
|
+
}
|
|
2804
|
+
draft.messages.splice(draft.messages.findIndex((m) => m.id === to) + 1);
|
|
2805
|
+
},
|
|
2806
|
+
});
|
|
2807
|
+
|
|
2808
|
+
const mountRuns = (init?: Partial<RunsTestState>) => {
|
|
2809
|
+
const captured: { aui?: ReturnType<typeof useAui> } = {};
|
|
2810
|
+
const Capture: FC = () => {
|
|
2811
|
+
captured.aui = useAui();
|
|
2812
|
+
return null;
|
|
2813
|
+
};
|
|
2814
|
+
const App: FC = () => {
|
|
2815
|
+
const aui = useAui({
|
|
2816
|
+
thread: AssistantTransportThread<RunsTestState>({
|
|
2817
|
+
initialState: { messages: [], status: "ready", runs: [], ...init },
|
|
2818
|
+
api: "https://example.com/api",
|
|
2819
|
+
headers: {},
|
|
2820
|
+
converter: runsConverter,
|
|
2821
|
+
runs: true,
|
|
2822
|
+
materialize,
|
|
2823
|
+
capabilities: { edit: true },
|
|
2824
|
+
protocol: "assistant-transport",
|
|
2825
|
+
}),
|
|
2826
|
+
});
|
|
2827
|
+
return (
|
|
2828
|
+
<AuiProvider value={aui}>
|
|
2829
|
+
<Capture />
|
|
2830
|
+
</AuiProvider>
|
|
2831
|
+
);
|
|
2832
|
+
};
|
|
2833
|
+
render(<App />);
|
|
2834
|
+
return { aui: () => captured.aui! };
|
|
2835
|
+
};
|
|
2836
|
+
|
|
2837
|
+
it("projects a pending send through the runs fold and materialize", async () => {
|
|
2838
|
+
const fetchMock = installFetch();
|
|
2839
|
+
let server!: StreamResponse;
|
|
2840
|
+
fetchMock.setHandler(() => {
|
|
2841
|
+
server = createStreamResponse();
|
|
2842
|
+
return server.response;
|
|
2843
|
+
});
|
|
2844
|
+
|
|
2845
|
+
const { aui } = mountRuns();
|
|
2846
|
+
aui().thread().append("go");
|
|
2847
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2848
|
+
|
|
2849
|
+
// The lib fold predicts the dispatch record; materialize renders it. One copy.
|
|
2850
|
+
expect(threadTexts(aui)).toEqual(["go"]);
|
|
2851
|
+
expect(aui().thread().getState().isRunning).toBe(true);
|
|
2852
|
+
|
|
2853
|
+
const sent = fetchMock.requests[0]!.body.commands[0] as {
|
|
2854
|
+
message: { id: string };
|
|
2855
|
+
};
|
|
2856
|
+
server.emit({
|
|
2857
|
+
type: "update-state",
|
|
2858
|
+
operations: [
|
|
2859
|
+
{
|
|
2860
|
+
type: "set",
|
|
2861
|
+
path: [],
|
|
2862
|
+
value: {
|
|
2863
|
+
messages: [],
|
|
2864
|
+
status: "running",
|
|
2865
|
+
runs: [
|
|
2866
|
+
{
|
|
2867
|
+
runId: "run-1",
|
|
2868
|
+
status: "running",
|
|
2869
|
+
queue: [],
|
|
2870
|
+
steerQueue: [],
|
|
2871
|
+
dispatching: {
|
|
2872
|
+
trigger: "message-send",
|
|
2873
|
+
messages: [
|
|
2874
|
+
{
|
|
2875
|
+
id: sent.message.id,
|
|
2876
|
+
role: "user",
|
|
2877
|
+
parts: [{ type: "text", text: "go" }],
|
|
2878
|
+
},
|
|
2879
|
+
],
|
|
2880
|
+
},
|
|
2881
|
+
error: null,
|
|
2882
|
+
},
|
|
2883
|
+
],
|
|
2884
|
+
},
|
|
2885
|
+
},
|
|
2886
|
+
],
|
|
2887
|
+
});
|
|
2888
|
+
|
|
2889
|
+
// Canonical echo retires the overlay in the same publish: still one copy.
|
|
2890
|
+
await waitFor(() => expect(threadTexts(aui)).toEqual(["go"]));
|
|
2891
|
+
});
|
|
2892
|
+
|
|
2893
|
+
it("rewinds through the materialize fold for a pending edit", async () => {
|
|
2894
|
+
const fetchMock = installFetch();
|
|
2895
|
+
fetchMock.setHandler(() => createStreamResponse().response);
|
|
2896
|
+
|
|
2897
|
+
const { aui } = mountRuns({
|
|
2898
|
+
messages: [
|
|
2899
|
+
{ id: "u1", role: "user", text: "one" },
|
|
2900
|
+
{ id: "a1", role: "assistant", text: "re one" },
|
|
2901
|
+
],
|
|
2902
|
+
});
|
|
2903
|
+
sendCommand(aui, {
|
|
2904
|
+
type: "run/edit",
|
|
2905
|
+
message: {
|
|
2906
|
+
id: "u1b",
|
|
2907
|
+
role: "user",
|
|
2908
|
+
parts: [{ type: "text", text: "one'" }],
|
|
2909
|
+
},
|
|
2910
|
+
sourceId: "u1",
|
|
2911
|
+
rollbackTo: null,
|
|
2912
|
+
});
|
|
2913
|
+
|
|
2914
|
+
await waitFor(() => expect(threadTexts(aui)).toEqual(["one'"]));
|
|
2915
|
+
});
|
|
2916
|
+
});
|