@assistant-ui/react-statewire 0.11.1 → 0.12.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 +2 -0
- package/dist/AssistantTransportThread.d.ts.map +1 -1
- package/dist/AssistantTransportThread.js.map +1 -1
- package/dist/StatewireThread.d.ts +7 -5
- package/dist/StatewireThread.d.ts.map +1 -1
- package/dist/StatewireThread.js +12 -2
- package/dist/StatewireThread.js.map +1 -1
- package/dist/hooks.d.ts +3 -1
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +11 -2
- package/dist/hooks.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/runCommands.d.ts +3 -0
- package/dist/runCommands.d.ts.map +1 -1
- package/dist/runCommands.js +4 -1
- package/dist/runCommands.js.map +1 -1
- package/dist/runsOptimistic.d.ts.map +1 -1
- package/dist/runsOptimistic.js +12 -13
- package/dist/runsOptimistic.js.map +1 -1
- package/package.json +1 -1
- package/src/AssistantTransportThread.ts +2 -0
- package/src/StatewireThread.runs.test.tsx +50 -16
- package/src/StatewireThread.test.tsx +49 -48
- package/src/StatewireThread.ts +27 -13
- package/src/hooks.ts +11 -0
- package/src/index.ts +1 -0
- package/src/runCommands.ts +8 -2
- package/src/runsOptimistic.ts +18 -18
- package/src/transport.test.tsx +52 -4
package/src/StatewireThread.ts
CHANGED
|
@@ -58,6 +58,15 @@ const READY_RUNS: StatewireThread.Runs = {
|
|
|
58
58
|
error: null,
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
+
const LOADING_RUNS: StatewireThread.Runs = {
|
|
62
|
+
status: "loading",
|
|
63
|
+
runId: null,
|
|
64
|
+
queue: [],
|
|
65
|
+
steerQueue: [],
|
|
66
|
+
dispatching: null,
|
|
67
|
+
error: null,
|
|
68
|
+
};
|
|
69
|
+
|
|
61
70
|
const ambientContextOf = (context: ModelContext): Record<string, unknown> => ({
|
|
62
71
|
...(context.system !== undefined && { system: context.system }),
|
|
63
72
|
...(context.tools !== undefined && {
|
|
@@ -197,7 +206,7 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
197
206
|
|
|
198
207
|
const runs = useMemo((): StatewireThread.Runs | undefined => {
|
|
199
208
|
if (runsOption === undefined) return undefined;
|
|
200
|
-
if (state === undefined) return
|
|
209
|
+
if (state === undefined) return LOADING_RUNS;
|
|
201
210
|
const mount = (runsOption === true ? state : runsOption(state)) as
|
|
202
211
|
| StatewireThread.RunsState
|
|
203
212
|
| undefined;
|
|
@@ -296,16 +305,24 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
296
305
|
" on first convert and changed since",
|
|
297
306
|
);
|
|
298
307
|
|
|
308
|
+
const status: StatewireThread.ThreadStatus =
|
|
309
|
+
runs !== undefined
|
|
310
|
+
? runs.status
|
|
311
|
+
: state === undefined
|
|
312
|
+
? "loading"
|
|
313
|
+
: "ready";
|
|
314
|
+
|
|
299
315
|
const extras = useMemo(
|
|
300
316
|
(): AssistantTransportExtras => ({
|
|
301
317
|
[symbolAssistantTransportExtras]: true,
|
|
302
318
|
sendCommand: dispatch,
|
|
303
319
|
reconnect: connection.reconnect,
|
|
304
320
|
state,
|
|
321
|
+
status,
|
|
305
322
|
...(inputRequests !== undefined && { inputRequests }),
|
|
306
323
|
...(runs !== undefined && { runs }),
|
|
307
324
|
}),
|
|
308
|
-
[dispatch, connection.reconnect, state, inputRequests, runs],
|
|
325
|
+
[dispatch, connection.reconnect, state, status, inputRequests, runs],
|
|
309
326
|
);
|
|
310
327
|
|
|
311
328
|
const handleAppend = (message: AppendMessage) => {
|
|
@@ -334,7 +351,6 @@ const useStatewireThread = (options: AnyOptions): ClientOutput<"thread"> => {
|
|
|
334
351
|
messages: messages as readonly ExternalThreadMessage[],
|
|
335
352
|
isRunning:
|
|
336
353
|
runs !== undefined ? runs.status === "running" : converted.isRunning,
|
|
337
|
-
isLoading: state === undefined,
|
|
338
354
|
state: converted.state,
|
|
339
355
|
extras,
|
|
340
356
|
attachmentAdapter: options.adapters?.attachments,
|
|
@@ -557,12 +573,10 @@ export namespace StatewireThread {
|
|
|
557
573
|
};
|
|
558
574
|
|
|
559
575
|
/** Run subsystem status replicated by the host. */
|
|
560
|
-
export type RunStatus =
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
| "error"
|
|
565
|
-
| "input-required";
|
|
576
|
+
export type RunStatus = "running" | "stopped" | "error" | "input-required";
|
|
577
|
+
|
|
578
|
+
/** The thread's status: `"loading"` while awaiting the attach snapshot, `"ready"` while resting, otherwise the run's status. */
|
|
579
|
+
export type ThreadStatus = "loading" | "ready" | RunStatus;
|
|
566
580
|
|
|
567
581
|
/** A queue-lane entry as replicated: the user message plus host-stamped `meta`. */
|
|
568
582
|
export type RunsQueueEntry = UserMessage & {
|
|
@@ -604,7 +618,7 @@ export namespace StatewireThread {
|
|
|
604
618
|
export type RunState = {
|
|
605
619
|
/** The run's identity — minted at dispatch, persists through input-required, null in error/stop. */
|
|
606
620
|
readonly runId: string | null;
|
|
607
|
-
readonly status:
|
|
621
|
+
readonly status: RunStatus;
|
|
608
622
|
readonly queue: readonly RunsQueueEntry[];
|
|
609
623
|
readonly steerQueue: readonly RunsQueueEntry[];
|
|
610
624
|
/** The dispatch record — the unacked batch handed to the run, cleared at the ack. */
|
|
@@ -625,14 +639,14 @@ export namespace StatewireThread {
|
|
|
625
639
|
/** The runs mount as replicated: the run entries plus the top-level status projection. */
|
|
626
640
|
export type RunsState = {
|
|
627
641
|
/** Projection of `runs[0]?.status ?? "ready"`. */
|
|
628
|
-
readonly status:
|
|
642
|
+
readonly status: Exclude<ThreadStatus, "loading">;
|
|
629
643
|
/** At most one entry; empty while resting. */
|
|
630
644
|
readonly runs: readonly RunState[];
|
|
631
645
|
};
|
|
632
646
|
|
|
633
|
-
/** The run subsystem's typed read — `runs[0]` of the selected mount with unsettled commands folded in, ready defaults while resting. */
|
|
647
|
+
/** The run subsystem's typed read — `runs[0]` of the selected mount with unsettled commands folded in, ready defaults while resting, loading defaults while awaiting the attach snapshot. */
|
|
634
648
|
export type Runs = {
|
|
635
|
-
readonly status:
|
|
649
|
+
readonly status: ThreadStatus;
|
|
636
650
|
/** The live run's id — non-null in running and input-required; stamped onto `run/stop`. */
|
|
637
651
|
readonly runId: string | null;
|
|
638
652
|
readonly queue: readonly RunsQueueEntry[];
|
package/src/hooks.ts
CHANGED
|
@@ -19,6 +19,17 @@ export const useAssistantTransportSendCommand = () => {
|
|
|
19
19
|
};
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
/** The thread status; requires StatewireThread. */
|
|
23
|
+
export const useStatewireThreadStatus = (): StatewireThread.ThreadStatus =>
|
|
24
|
+
useAuiState((s) => {
|
|
25
|
+
const { status } = asAssistantTransportExtras(s.thread.extras);
|
|
26
|
+
if (status === undefined)
|
|
27
|
+
throw new Error(
|
|
28
|
+
"statewire: useStatewireThreadStatus requires StatewireThread",
|
|
29
|
+
);
|
|
30
|
+
return status;
|
|
31
|
+
});
|
|
32
|
+
|
|
22
33
|
/** The typed runs read; requires StatewireThread with the `runs` option. */
|
|
23
34
|
export const useStatewireRuns = (): StatewireThread.Runs =>
|
|
24
35
|
useAuiState((s) => {
|
package/src/index.ts
CHANGED
package/src/runCommands.ts
CHANGED
|
@@ -77,8 +77,14 @@ const placementFields = (placement: QueuePlacement) => ({
|
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
export const viewedLeafId = (
|
|
80
|
-
messages: readonly { id: string }[],
|
|
81
|
-
): string | null =>
|
|
80
|
+
messages: readonly { id: string; status?: { type: string } }[],
|
|
81
|
+
): string | null => {
|
|
82
|
+
// An in-flight assistant message is not host-committed history: anchor below it.
|
|
83
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
84
|
+
if (messages[i]!.status?.type !== "running") return messages[i]!.id;
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
};
|
|
82
88
|
|
|
83
89
|
export const resolveEditCommand = (
|
|
84
90
|
messages: readonly { id: string }[],
|
package/src/runsOptimistic.ts
CHANGED
|
@@ -24,12 +24,12 @@ type RunEntry = {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
type RunsDraft = {
|
|
27
|
-
status: StatewireThread.
|
|
28
|
-
runs
|
|
27
|
+
status: Exclude<StatewireThread.ThreadStatus, "loading">;
|
|
28
|
+
runs?: RunEntry[];
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
const startRun = (draft: RunsDraft, runId: string):
|
|
32
|
-
const entry = (draft.runs[0] ??= {
|
|
31
|
+
const startRun = (draft: RunsDraft, runId: string): RunEntry => {
|
|
32
|
+
const entry = ((draft.runs ??= [])[0] ??= {
|
|
33
33
|
runId: null,
|
|
34
34
|
status: "running",
|
|
35
35
|
queue: [],
|
|
@@ -42,6 +42,7 @@ const startRun = (draft: RunsDraft, runId: string): void => {
|
|
|
42
42
|
entry.error = null;
|
|
43
43
|
delete entry.inputRequests;
|
|
44
44
|
draft.status = "running";
|
|
45
|
+
return entry;
|
|
45
46
|
};
|
|
46
47
|
|
|
47
48
|
const insert = (
|
|
@@ -65,7 +66,9 @@ const enqueueOrSteer = (
|
|
|
65
66
|
command: StatewireThread.RunEnqueueCommand | StatewireThread.RunSteerCommand,
|
|
66
67
|
runId: string,
|
|
67
68
|
): void => {
|
|
68
|
-
const run = draft.runs[0];
|
|
69
|
+
const run = draft.runs?.[0];
|
|
70
|
+
// The entry status is the truth; hosts replicating the top-level projection agree.
|
|
71
|
+
const status = run?.status ?? "ready";
|
|
69
72
|
const id = "message" in command ? command.message.id : command.messageId;
|
|
70
73
|
// In dispatch limbo the canonical state already tells the story: no fold.
|
|
71
74
|
if (run?.dispatching?.messages.some((e) => e.id === id)) return;
|
|
@@ -91,16 +94,15 @@ const enqueueOrSteer = (
|
|
|
91
94
|
|
|
92
95
|
const steer = command.type === "run/steer";
|
|
93
96
|
const direct = steer
|
|
94
|
-
?
|
|
97
|
+
? status === "ready" || status === "input-required"
|
|
95
98
|
: sourceLane === undefined &&
|
|
96
|
-
(
|
|
97
|
-
((
|
|
99
|
+
(status === "ready" ||
|
|
100
|
+
((status === "stopped" || status === "error") &&
|
|
98
101
|
run !== undefined &&
|
|
99
102
|
run.queue.length === 0 &&
|
|
100
103
|
run.steerQueue.length === 0));
|
|
101
104
|
if (direct) {
|
|
102
|
-
startRun(draft, runId);
|
|
103
|
-
const target = draft.runs[0]!;
|
|
105
|
+
const target = startRun(draft, runId);
|
|
104
106
|
if (steer) {
|
|
105
107
|
// The whole steer lane rides the dispatch, in lane order.
|
|
106
108
|
if (sourceLane === target.steerQueue)
|
|
@@ -113,7 +115,7 @@ const enqueueOrSteer = (
|
|
|
113
115
|
} else target.dispatching = { trigger: "message-send", messages: [entry] };
|
|
114
116
|
return;
|
|
115
117
|
}
|
|
116
|
-
const continues = steer &&
|
|
118
|
+
const continues = steer && status !== "running";
|
|
117
119
|
|
|
118
120
|
if (run === undefined) return;
|
|
119
121
|
const lane = steer ? run.steerQueue : run.queue;
|
|
@@ -147,8 +149,7 @@ export const runsOptimistic = (
|
|
|
147
149
|
case "run/edit": {
|
|
148
150
|
const { message, rollbackTo } = command
|
|
149
151
|
.params[0] as StatewireThread.RunEditCommand;
|
|
150
|
-
startRun(draft, runId)
|
|
151
|
-
draft.runs[0]!.dispatching = {
|
|
152
|
+
startRun(draft, runId).dispatching = {
|
|
152
153
|
trigger: "message-edit",
|
|
153
154
|
messages: [{ ...message }],
|
|
154
155
|
rollbackTo,
|
|
@@ -158,8 +159,7 @@ export const runsOptimistic = (
|
|
|
158
159
|
case "run/reload": {
|
|
159
160
|
const { rollbackTo } = command
|
|
160
161
|
.params[0] as StatewireThread.RunReloadCommand;
|
|
161
|
-
startRun(draft, runId)
|
|
162
|
-
draft.runs[0]!.dispatching = {
|
|
162
|
+
startRun(draft, runId).dispatching = {
|
|
163
163
|
trigger: "message-reload",
|
|
164
164
|
messages: [],
|
|
165
165
|
rollbackTo,
|
|
@@ -167,7 +167,7 @@ export const runsOptimistic = (
|
|
|
167
167
|
break;
|
|
168
168
|
}
|
|
169
169
|
case "run/continue": {
|
|
170
|
-
const run = draft.runs[0];
|
|
170
|
+
const run = draft.runs?.[0];
|
|
171
171
|
if (run !== undefined) run.steerQueue = [];
|
|
172
172
|
startRun(draft, runId);
|
|
173
173
|
break;
|
|
@@ -175,7 +175,7 @@ export const runsOptimistic = (
|
|
|
175
175
|
case "run/dequeue": {
|
|
176
176
|
const { messageId } = command
|
|
177
177
|
.params[0] as StatewireThread.RunDequeueCommand;
|
|
178
|
-
const run = draft.runs[0];
|
|
178
|
+
const run = draft.runs?.[0];
|
|
179
179
|
if (run === undefined) break;
|
|
180
180
|
for (const lane of [run.queue, run.steerQueue]) {
|
|
181
181
|
const index = lane.findIndex((e) => e.id === messageId);
|
|
@@ -186,7 +186,7 @@ export const runsOptimistic = (
|
|
|
186
186
|
case "run/input": {
|
|
187
187
|
const { requestId, response } = command
|
|
188
188
|
.params[0] as StatewireThread.RunInputCommand;
|
|
189
|
-
const requests = draft.runs[0]?.inputRequests;
|
|
189
|
+
const requests = draft.runs?.[0]?.inputRequests;
|
|
190
190
|
const target = requests?.find((r) => r.id === requestId);
|
|
191
191
|
if (target === undefined) break;
|
|
192
192
|
target.response = response;
|
package/src/transport.test.tsx
CHANGED
|
@@ -2762,8 +2762,8 @@ describe("input requests", () => {
|
|
|
2762
2762
|
|
|
2763
2763
|
describe("runs fold and materialize", () => {
|
|
2764
2764
|
type RunsTestState = TestState & {
|
|
2765
|
-
status
|
|
2766
|
-
runs
|
|
2765
|
+
status?: "ready" | "running";
|
|
2766
|
+
runs?: {
|
|
2767
2767
|
runId: string | null;
|
|
2768
2768
|
status: string;
|
|
2769
2769
|
queue: { id: string }[];
|
|
@@ -2805,7 +2805,9 @@ describe("runs fold and materialize", () => {
|
|
|
2805
2805
|
},
|
|
2806
2806
|
});
|
|
2807
2807
|
|
|
2808
|
-
const mountRuns = (
|
|
2808
|
+
const mountRuns = (
|
|
2809
|
+
init: RunsTestState = { messages: [], status: "ready", runs: [] },
|
|
2810
|
+
) => {
|
|
2809
2811
|
const captured: { aui?: ReturnType<typeof useAui> } = {};
|
|
2810
2812
|
const Capture: FC = () => {
|
|
2811
2813
|
captured.aui = useAui();
|
|
@@ -2814,7 +2816,7 @@ describe("runs fold and materialize", () => {
|
|
|
2814
2816
|
const App: FC = () => {
|
|
2815
2817
|
const aui = useAui({
|
|
2816
2818
|
thread: AssistantTransportThread<RunsTestState>({
|
|
2817
|
-
initialState:
|
|
2819
|
+
initialState: init,
|
|
2818
2820
|
api: "https://example.com/api",
|
|
2819
2821
|
headers: {},
|
|
2820
2822
|
converter: runsConverter,
|
|
@@ -2899,6 +2901,8 @@ describe("runs fold and materialize", () => {
|
|
|
2899
2901
|
{ id: "u1", role: "user", text: "one" },
|
|
2900
2902
|
{ id: "a1", role: "assistant", text: "re one" },
|
|
2901
2903
|
],
|
|
2904
|
+
status: "ready",
|
|
2905
|
+
runs: [],
|
|
2902
2906
|
});
|
|
2903
2907
|
sendCommand(aui, {
|
|
2904
2908
|
type: "run/edit",
|
|
@@ -2913,4 +2917,48 @@ describe("runs fold and materialize", () => {
|
|
|
2913
2917
|
|
|
2914
2918
|
await waitFor(() => expect(threadTexts(aui)).toEqual(["one'"]));
|
|
2915
2919
|
});
|
|
2920
|
+
|
|
2921
|
+
it("folds a send into a mount without a top-level status — absent means ready", async () => {
|
|
2922
|
+
const fetchMock = installFetch();
|
|
2923
|
+
fetchMock.setHandler(() => createStreamResponse().response);
|
|
2924
|
+
|
|
2925
|
+
const { aui } = mountRuns({ messages: [], runs: [] });
|
|
2926
|
+
aui().thread().append("go");
|
|
2927
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2928
|
+
|
|
2929
|
+
expect(threadTexts(aui)).toEqual(["go"]);
|
|
2930
|
+
expect(aui().thread().getState().isRunning).toBe(true);
|
|
2931
|
+
});
|
|
2932
|
+
|
|
2933
|
+
it("creates the runs mount when the canonical state carries none", async () => {
|
|
2934
|
+
const fetchMock = installFetch();
|
|
2935
|
+
let server!: StreamResponse;
|
|
2936
|
+
fetchMock.setHandler(() => {
|
|
2937
|
+
server = createStreamResponse();
|
|
2938
|
+
return server.response;
|
|
2939
|
+
});
|
|
2940
|
+
|
|
2941
|
+
const { aui } = mountRuns({ messages: [], runs: [] });
|
|
2942
|
+
aui().thread().append("one");
|
|
2943
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(1));
|
|
2944
|
+
server.emit({
|
|
2945
|
+
type: "update-state",
|
|
2946
|
+
operations: [
|
|
2947
|
+
{
|
|
2948
|
+
type: "set",
|
|
2949
|
+
path: [],
|
|
2950
|
+
value: { messages: [{ id: "u1", role: "user", text: "one" }] },
|
|
2951
|
+
},
|
|
2952
|
+
],
|
|
2953
|
+
});
|
|
2954
|
+
server.done();
|
|
2955
|
+
await waitFor(() =>
|
|
2956
|
+
expect(aui().thread().getState().isRunning).toBe(false),
|
|
2957
|
+
);
|
|
2958
|
+
|
|
2959
|
+
aui().thread().append("two");
|
|
2960
|
+
await waitFor(() => expect(fetchMock.requests).toHaveLength(2));
|
|
2961
|
+
expect(threadTexts(aui)).toEqual(["one", "two"]);
|
|
2962
|
+
expect(aui().thread().getState().isRunning).toBe(true);
|
|
2963
|
+
});
|
|
2916
2964
|
});
|