@assistant-ui/react-statewire 0.4.3 → 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 +193 -4
- 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 +92 -11
- 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 +8 -3
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +10 -1
- package/dist/hooks.js.map +1 -1
- package/dist/host.d.ts +5 -5
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +3 -38
- package/dist/index.js +2 -2
- package/dist/inputRequests.d.ts +4 -4
- package/dist/inputRequests.d.ts.map +1 -1
- package/dist/inputRequests.js.map +1 -1
- package/dist/runCommands.d.ts +8 -7
- 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 +267 -24
- package/src/StatewireThread.runs.test.tsx +405 -0
- package/src/StatewireThread.test.tsx +180 -3
- package/src/StatewireThread.ts +223 -80
- package/src/__tests__/testHelpers.tsx +11 -17
- package/src/athena.e2e.test.tsx +15 -19
- package/src/hooks.ts +19 -2
- package/src/host.ts +11 -13
- package/src/index.ts +2 -36
- package/src/inputRequests.ts +8 -6
- package/src/legacy-diff.test.tsx +4 -7
- package/src/runCommands.ts +42 -28
- package/src/runsOptimistic.test.ts +225 -0
- package/src/runsOptimistic.ts +164 -0
- package/src/transport.test.tsx +12 -16
- package/dist/index.d.ts.map +0 -1
- package/dist/types.d.ts +0 -189
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -0
- package/src/types.ts +0 -255
package/src/host.ts
CHANGED
|
@@ -18,11 +18,7 @@ import {
|
|
|
18
18
|
import type { StatewireHost } from "statewire/host";
|
|
19
19
|
import { createReplayBoundaryStream } from "./replayBoundaryStream";
|
|
20
20
|
import { translateStateOp } from "./stateOps";
|
|
21
|
-
import type {
|
|
22
|
-
AssistantTransportCommand,
|
|
23
|
-
AssistantTransportOptions,
|
|
24
|
-
SendCommandsRequestBody,
|
|
25
|
-
} from "./types";
|
|
21
|
+
import type { AssistantTransportThread } from "./AssistantTransportThread";
|
|
26
22
|
|
|
27
23
|
export type SendOptions = {
|
|
28
24
|
schedule?: boolean;
|
|
@@ -34,8 +30,8 @@ export type HostRunContext = {
|
|
|
34
30
|
};
|
|
35
31
|
|
|
36
32
|
export type HostQueue = {
|
|
37
|
-
queued:
|
|
38
|
-
inTransit:
|
|
33
|
+
queued: AssistantTransportThread.Command[];
|
|
34
|
+
inTransit: AssistantTransportThread.Command[];
|
|
39
35
|
};
|
|
40
36
|
|
|
41
37
|
// Statewire state sanitizes values on apply; command objects must keep their
|
|
@@ -43,7 +39,7 @@ export type HostQueue = {
|
|
|
43
39
|
// cell and bumps queueVersion in state to signal the change.
|
|
44
40
|
export type HostQueueCell = { current: HostQueue };
|
|
45
41
|
|
|
46
|
-
export type HostOptions =
|
|
42
|
+
export type HostOptions = AssistantTransportThread.Options<unknown> & {
|
|
47
43
|
getRunContext: () => HostRunContext;
|
|
48
44
|
queueCell: HostQueueCell;
|
|
49
45
|
};
|
|
@@ -56,7 +52,7 @@ export type AssistantTransportHostState = {
|
|
|
56
52
|
};
|
|
57
53
|
|
|
58
54
|
export type AssistantTransportHostCommands = {
|
|
59
|
-
send(command:
|
|
55
|
+
send(command: AssistantTransportThread.Command, opts?: SendOptions): void;
|
|
60
56
|
setParentId(parentId: string | null): void;
|
|
61
57
|
cancel(): void;
|
|
62
58
|
resume(): void;
|
|
@@ -70,7 +66,7 @@ type EntrySettlement = {
|
|
|
70
66
|
};
|
|
71
67
|
|
|
72
68
|
type QueueEntry = {
|
|
73
|
-
command:
|
|
69
|
+
command: AssistantTransportThread.Command;
|
|
74
70
|
execution: EntrySettlement;
|
|
75
71
|
};
|
|
76
72
|
|
|
@@ -176,7 +172,7 @@ const useHostLoop = (
|
|
|
176
172
|
|
|
177
173
|
const buildRequestBody = async (
|
|
178
174
|
o: HostOptions,
|
|
179
|
-
commands:
|
|
175
|
+
commands: AssistantTransportThread.Command[],
|
|
180
176
|
runParentId: string | null | undefined,
|
|
181
177
|
) => {
|
|
182
178
|
const bodyValue = typeof o.body === "function" ? await o.body() : o.body;
|
|
@@ -200,7 +196,9 @@ const useHostLoop = (
|
|
|
200
196
|
};
|
|
201
197
|
|
|
202
198
|
if (!o.prepareSendCommandsRequest) return requestBody;
|
|
203
|
-
return o.prepareSendCommandsRequest(
|
|
199
|
+
return o.prepareSendCommandsRequest(
|
|
200
|
+
requestBody as AssistantTransportThread.SendCommandsRequestBody,
|
|
201
|
+
);
|
|
204
202
|
};
|
|
205
203
|
|
|
206
204
|
const run = async (signal: AbortSignal) => {
|
|
@@ -373,7 +371,7 @@ const useHostLoop = (
|
|
|
373
371
|
return {
|
|
374
372
|
send: function (
|
|
375
373
|
this: StatewireHost.Ctx,
|
|
376
|
-
command:
|
|
374
|
+
command: AssistantTransportThread.Command,
|
|
377
375
|
opts?: SendOptions,
|
|
378
376
|
) {
|
|
379
377
|
let resolve!: () => void;
|
package/src/index.ts
CHANGED
|
@@ -1,41 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import { AssistantTransportThread } from "./AssistantTransportThread";
|
|
3
|
-
|
|
4
|
-
export { AssistantTransportThread };
|
|
1
|
+
export { AssistantTransportThread } from "./AssistantTransportThread";
|
|
5
2
|
export { StatewireThread } from "./StatewireThread";
|
|
6
3
|
export {
|
|
7
4
|
useAssistantTransportSendCommand,
|
|
8
5
|
useAssistantTransportState,
|
|
6
|
+
useStatewireRuns,
|
|
9
7
|
} from "./hooks";
|
|
10
|
-
|
|
11
|
-
export namespace AssistantTransportThread {
|
|
12
|
-
export type Options<T> = types.AssistantTransportOptions<T>;
|
|
13
|
-
export type Command = types.AssistantTransportCommand;
|
|
14
|
-
export type Protocol = types.AssistantTransportProtocol;
|
|
15
|
-
export type State = types.AssistantTransportState;
|
|
16
|
-
export type ConnectionMetadata = types.AssistantTransportConnectionMetadata;
|
|
17
|
-
export type StateConverter<T> = types.AssistantTransportStateConverter<T>;
|
|
18
|
-
export type RunCommand = types.RunCommand;
|
|
19
|
-
export type RunEnqueueCommand = types.RunEnqueueCommand;
|
|
20
|
-
export type RunSteerCommand = types.RunSteerCommand;
|
|
21
|
-
export type RunDequeueCommand = types.RunDequeueCommand;
|
|
22
|
-
export type RunEditCommand = types.RunEditCommand;
|
|
23
|
-
export type RunContinueCommand = types.RunContinueCommand;
|
|
24
|
-
export type RunReloadCommand = types.RunReloadCommand;
|
|
25
|
-
export type RunInputCommand = types.RunInputCommand;
|
|
26
|
-
export type InputRequest = types.InputRequest;
|
|
27
|
-
export type ToolCallInputRequest = types.ToolCallInputRequest;
|
|
28
|
-
export type ToolApprovalInputRequest = types.ToolApprovalInputRequest;
|
|
29
|
-
export type CustomInputRequest = types.CustomInputRequest;
|
|
30
|
-
export type InputRequestState = types.InputRequestState;
|
|
31
|
-
export type InputResponse = types.InputResponse;
|
|
32
|
-
export type ToolCallInputResponse = types.ToolCallInputResponse;
|
|
33
|
-
export type ToolApprovalInputResponse = types.ToolApprovalInputResponse;
|
|
34
|
-
export type RunUserMessage = types.RunUserMessage;
|
|
35
|
-
export type RunQueueItem = types.RunQueueItem;
|
|
36
|
-
export type RunPlacement = types.RunPlacement;
|
|
37
|
-
export type SendCommandsRequestBody = types.SendCommandsRequestBody;
|
|
38
|
-
export type TextPart = types.TextPart;
|
|
39
|
-
export type FilePart = types.FilePart;
|
|
40
|
-
export type HeadersValue = types.HeadersValue;
|
|
41
|
-
}
|
package/src/inputRequests.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { RespondToToolApprovalOptions } from "@assistant-ui/core";
|
|
2
2
|
import type { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
3
|
-
import type {
|
|
3
|
+
import type { AssistantTransportThread } from "./AssistantTransportThread";
|
|
4
4
|
|
|
5
|
-
const assertUnanswered = (
|
|
5
|
+
const assertUnanswered = (
|
|
6
|
+
request: AssistantTransportThread.InputRequestState,
|
|
7
|
+
) => {
|
|
6
8
|
if (request.response !== null)
|
|
7
9
|
throw new Error(
|
|
8
10
|
`statewire: input request "${request.id}" is already answered`,
|
|
@@ -10,14 +12,14 @@ const assertUnanswered = (request: InputRequestState) => {
|
|
|
10
12
|
};
|
|
11
13
|
|
|
12
14
|
export const resolveToolResultInput = (
|
|
13
|
-
inputRequests: readonly InputRequestState[],
|
|
15
|
+
inputRequests: readonly AssistantTransportThread.InputRequestState[],
|
|
14
16
|
options: {
|
|
15
17
|
toolCallId: string;
|
|
16
18
|
result: ReadonlyJSONValue;
|
|
17
19
|
isError: boolean;
|
|
18
20
|
artifact?: ReadonlyJSONValue | undefined;
|
|
19
21
|
},
|
|
20
|
-
): RunInputCommand => {
|
|
22
|
+
): AssistantTransportThread.RunInputCommand => {
|
|
21
23
|
const request = inputRequests.find(
|
|
22
24
|
(r) => r.type === "tool-call" && r.toolCallId === options.toolCallId,
|
|
23
25
|
);
|
|
@@ -38,9 +40,9 @@ export const resolveToolResultInput = (
|
|
|
38
40
|
};
|
|
39
41
|
|
|
40
42
|
export const resolveToolApprovalInput = (
|
|
41
|
-
inputRequests: readonly InputRequestState[],
|
|
43
|
+
inputRequests: readonly AssistantTransportThread.InputRequestState[],
|
|
42
44
|
options: RespondToToolApprovalOptions,
|
|
43
|
-
): RunInputCommand => {
|
|
45
|
+
): AssistantTransportThread.RunInputCommand => {
|
|
44
46
|
const request = inputRequests.find(
|
|
45
47
|
(r) => r.type === "tool-approval" && r.id === options.approvalId,
|
|
46
48
|
);
|
package/src/legacy-diff.test.tsx
CHANGED
|
@@ -14,10 +14,7 @@ import {
|
|
|
14
14
|
} from "@assistant-ui/react";
|
|
15
15
|
import type { ExternalThreadMessage } from "@assistant-ui/react";
|
|
16
16
|
import { AssistantTransportThread } from "./AssistantTransportThread";
|
|
17
|
-
|
|
18
|
-
AssistantTransportOptions,
|
|
19
|
-
AssistantTransportStateConverter,
|
|
20
|
-
} from "./types";
|
|
17
|
+
|
|
21
18
|
import { sseEvent } from "./__tests__/testHelpers";
|
|
22
19
|
|
|
23
20
|
type TestState = {
|
|
@@ -26,7 +23,7 @@ type TestState = {
|
|
|
26
23
|
|
|
27
24
|
const INITIAL_STATE: TestState = { messages: [] };
|
|
28
25
|
|
|
29
|
-
const converter:
|
|
26
|
+
const converter: AssistantTransportThread.StateConverter<TestState> = (
|
|
30
27
|
state,
|
|
31
28
|
meta,
|
|
32
29
|
) => ({
|
|
@@ -232,7 +229,7 @@ const observeNew = async (): Promise<{
|
|
|
232
229
|
captured.aui = useAui();
|
|
233
230
|
return null;
|
|
234
231
|
};
|
|
235
|
-
const options:
|
|
232
|
+
const options: AssistantTransportThread.Options<TestState> = {
|
|
236
233
|
initialState: INITIAL_STATE,
|
|
237
234
|
api: "https://example.com/api",
|
|
238
235
|
protocol: "assistant-transport",
|
|
@@ -288,7 +285,7 @@ type ToolState = TestState & {
|
|
|
288
285
|
note?: string;
|
|
289
286
|
};
|
|
290
287
|
|
|
291
|
-
const toolConverter:
|
|
288
|
+
const toolConverter: AssistantTransportThread.StateConverter<ToolState> = (
|
|
292
289
|
state,
|
|
293
290
|
meta,
|
|
294
291
|
) => {
|
package/src/runCommands.ts
CHANGED
|
@@ -3,22 +3,15 @@ import type {
|
|
|
3
3
|
ExternalThreadQueueAdapter,
|
|
4
4
|
QueuePlacement,
|
|
5
5
|
} from "@assistant-ui/core";
|
|
6
|
-
import type {
|
|
7
|
-
FilePart,
|
|
8
|
-
RunCommand,
|
|
9
|
-
RunQueueItem,
|
|
10
|
-
RunReloadCommand,
|
|
11
|
-
RunUserMessage,
|
|
12
|
-
TextPart,
|
|
13
|
-
} from "./types";
|
|
6
|
+
import type { AssistantTransportThread } from "./AssistantTransportThread";
|
|
14
7
|
|
|
15
8
|
export const toWireMessage = (
|
|
16
9
|
message: AppendMessage,
|
|
17
10
|
id: string,
|
|
18
|
-
): RunUserMessage => {
|
|
11
|
+
): AssistantTransportThread.RunUserMessage => {
|
|
19
12
|
if (message.role !== "user")
|
|
20
13
|
throw new Error("statewire: only user messages are supported");
|
|
21
|
-
const files: FilePart[] = [];
|
|
14
|
+
const files: AssistantTransportThread.FilePart[] = [];
|
|
22
15
|
const texts: string[] = [];
|
|
23
16
|
const source = [
|
|
24
17
|
...message.content,
|
|
@@ -41,7 +34,10 @@ export const toWireMessage = (
|
|
|
41
34
|
}
|
|
42
35
|
}
|
|
43
36
|
// default-safe against host capabilities: files first, one merged text part
|
|
44
|
-
const parts: (
|
|
37
|
+
const parts: (
|
|
38
|
+
| AssistantTransportThread.TextPart
|
|
39
|
+
| AssistantTransportThread.FilePart
|
|
40
|
+
)[] = [
|
|
45
41
|
...files,
|
|
46
42
|
...(texts.length > 0
|
|
47
43
|
? [{ type: "text" as const, text: texts.join("\n\n") }]
|
|
@@ -52,7 +48,9 @@ export const toWireMessage = (
|
|
|
52
48
|
return { id, role: "user", parts };
|
|
53
49
|
};
|
|
54
50
|
|
|
55
|
-
export const toQueueItemState = (
|
|
51
|
+
export const toQueueItemState = (
|
|
52
|
+
item: AssistantTransportThread.RunQueueItem,
|
|
53
|
+
) => ({
|
|
56
54
|
id: item.id,
|
|
57
55
|
prompt: item.parts
|
|
58
56
|
.flatMap((part) => (part.type === "text" ? [part.text] : []))
|
|
@@ -82,11 +80,23 @@ export const viewedLeafId = (
|
|
|
82
80
|
messages: readonly { id: string }[],
|
|
83
81
|
): string | null => messages.at(-1)?.id ?? null;
|
|
84
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
|
+
|
|
85
95
|
export const createRunQueueAdapter = (
|
|
86
96
|
messages: readonly { id: string }[],
|
|
87
|
-
queueItems: readonly RunQueueItem[],
|
|
88
|
-
steerQueueItems: readonly RunQueueItem[] | undefined,
|
|
89
|
-
dispatch: (command: RunCommand) => void,
|
|
97
|
+
queueItems: readonly AssistantTransportThread.RunQueueItem[],
|
|
98
|
+
steerQueueItems: readonly AssistantTransportThread.RunQueueItem[] | undefined,
|
|
99
|
+
dispatch: (command: AssistantTransportThread.RunCommand) => void,
|
|
90
100
|
): ExternalThreadQueueAdapter => {
|
|
91
101
|
const laneOf = (queueItemId: string): "queue" | "steer" => {
|
|
92
102
|
if (steerQueueItems?.some((item) => item.id === queueItemId))
|
|
@@ -94,21 +104,25 @@ export const createRunQueueAdapter = (
|
|
|
94
104
|
if (queueItems.some((item) => item.id === queueItemId)) return "queue";
|
|
95
105
|
throw new Error(`statewire: unknown queue item "${queueItemId}"`);
|
|
96
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
|
+
};
|
|
97
121
|
return {
|
|
98
122
|
items: queueItems.map(toQueueItemState),
|
|
99
123
|
steerItems: (steerQueueItems ?? []).map(toQueueItemState),
|
|
100
|
-
enqueue: (message) =>
|
|
101
|
-
|
|
102
|
-
type: "run/enqueue",
|
|
103
|
-
message: toWireMessage(message, crypto.randomUUID()),
|
|
104
|
-
anchorMessageId: viewedLeafId(messages),
|
|
105
|
-
}),
|
|
106
|
-
steer: (message) =>
|
|
107
|
-
dispatch({
|
|
108
|
-
type: "run/steer",
|
|
109
|
-
message: toWireMessage(message, crypto.randomUUID()),
|
|
110
|
-
anchorMessageId: viewedLeafId(messages),
|
|
111
|
-
}),
|
|
124
|
+
enqueue: (message) => dispatch(sendCommand("queue", message)),
|
|
125
|
+
steer: (message) => dispatch(sendCommand("steer", message)),
|
|
112
126
|
move: (queueItemId, placement) => {
|
|
113
127
|
const lane = placement.lane ?? laneOf(queueItemId);
|
|
114
128
|
const fields = {
|
|
@@ -137,7 +151,7 @@ export const createRunQueueAdapter = (
|
|
|
137
151
|
export const resolveReloadCommand = (
|
|
138
152
|
messages: readonly { id: string; role: string }[],
|
|
139
153
|
parentId: string | null,
|
|
140
|
-
): RunReloadCommand => {
|
|
154
|
+
): AssistantTransportThread.RunReloadCommand => {
|
|
141
155
|
const index =
|
|
142
156
|
parentId === null
|
|
143
157
|
? 0
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// Contract tests for the run-dialect optimistic reducer: unsettled run/*
|
|
2
|
+
// commands fold into the runs mount draft.
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { runsOptimistic } from "./runsOptimistic";
|
|
6
|
+
|
|
7
|
+
type Draft = Record<string, any>;
|
|
8
|
+
|
|
9
|
+
const ctx = { ts: 0, seq: 7 };
|
|
10
|
+
|
|
11
|
+
const mount = (entry?: Draft): Draft =>
|
|
12
|
+
entry === undefined
|
|
13
|
+
? { status: "ready", runs: [] }
|
|
14
|
+
: {
|
|
15
|
+
status: entry.status,
|
|
16
|
+
runs: [
|
|
17
|
+
{
|
|
18
|
+
runId: null,
|
|
19
|
+
queue: [],
|
|
20
|
+
steerQueue: [],
|
|
21
|
+
error: null,
|
|
22
|
+
runLeafMessageId: null,
|
|
23
|
+
...entry,
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const run = (draft: Draft): Draft => draft.runs[0];
|
|
29
|
+
|
|
30
|
+
const message = (id: string) => ({
|
|
31
|
+
id,
|
|
32
|
+
role: "user" as const,
|
|
33
|
+
parts: [{ type: "text" as const, text: id }],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const apply = (draft: Draft, method: string, param?: unknown) =>
|
|
37
|
+
runsOptimistic(
|
|
38
|
+
draft,
|
|
39
|
+
{ method, params: param === undefined ? [] : [param] } as never,
|
|
40
|
+
ctx,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
describe("runsOptimistic", () => {
|
|
44
|
+
it("parks a steer add in the lane while running", () => {
|
|
45
|
+
const draft = mount({ status: "running", runId: "run-1" });
|
|
46
|
+
apply(draft, "run/steer", {
|
|
47
|
+
type: "run/steer",
|
|
48
|
+
message: message("s1"),
|
|
49
|
+
anchorMessageId: null,
|
|
50
|
+
});
|
|
51
|
+
expect(run(draft).steerQueue.map((e: Draft) => e.id)).toEqual(["s1"]);
|
|
52
|
+
expect(draft.status).toBe("running");
|
|
53
|
+
expect(run(draft).status).toBe("running");
|
|
54
|
+
expect(run(draft).runId).toBe("run-1");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("dispatches a steer while no run is active: the whole lane enters the thread", () => {
|
|
58
|
+
const draft = mount({
|
|
59
|
+
status: "stopped",
|
|
60
|
+
steerQueue: [message("s1")],
|
|
61
|
+
error: { message: "boom" },
|
|
62
|
+
});
|
|
63
|
+
apply(draft, "run/steer", {
|
|
64
|
+
type: "run/steer",
|
|
65
|
+
message: message("s2"),
|
|
66
|
+
anchorMessageId: null,
|
|
67
|
+
});
|
|
68
|
+
expect(draft.status).toBe("running");
|
|
69
|
+
expect(run(draft).status).toBe("running");
|
|
70
|
+
expect(run(draft).runId).toBe("optimistic-7");
|
|
71
|
+
expect(run(draft).steerQueue).toEqual([]);
|
|
72
|
+
expect(run(draft).error).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("dispatches an enqueue add only in ready or in error/stop with empty lanes", () => {
|
|
76
|
+
const ready = mount();
|
|
77
|
+
apply(ready, "run/enqueue", {
|
|
78
|
+
type: "run/enqueue",
|
|
79
|
+
message: message("a"),
|
|
80
|
+
anchorMessageId: null,
|
|
81
|
+
});
|
|
82
|
+
expect(ready.status).toBe("running");
|
|
83
|
+
expect(run(ready).runId).toBe("optimistic-7");
|
|
84
|
+
expect(run(ready).queue).toEqual([]);
|
|
85
|
+
|
|
86
|
+
const stopped = mount({ status: "stopped", queue: [message("q1")] });
|
|
87
|
+
apply(stopped, "run/enqueue", {
|
|
88
|
+
type: "run/enqueue",
|
|
89
|
+
message: message("a"),
|
|
90
|
+
anchorMessageId: null,
|
|
91
|
+
});
|
|
92
|
+
expect(stopped.status).toBe("stopped");
|
|
93
|
+
expect(run(stopped).queue.map((e: Draft) => e.id)).toEqual(["q1", "a"]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("parks an enqueue add at its placement while running", () => {
|
|
97
|
+
const draft = mount({
|
|
98
|
+
status: "running",
|
|
99
|
+
runId: "r",
|
|
100
|
+
queue: [message("a"), message("b")],
|
|
101
|
+
});
|
|
102
|
+
apply(draft, "run/enqueue", {
|
|
103
|
+
type: "run/enqueue",
|
|
104
|
+
message: message("x"),
|
|
105
|
+
anchorMessageId: null,
|
|
106
|
+
insertAfter: "a",
|
|
107
|
+
});
|
|
108
|
+
expect(run(draft).queue.map((e: Draft) => e.id)).toEqual(["a", "x", "b"]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("applies moves, edits of queued items, and dequeue", () => {
|
|
112
|
+
const draft = mount({
|
|
113
|
+
status: "running",
|
|
114
|
+
runId: "r",
|
|
115
|
+
queue: [{ ...message("q1"), meta: { m: 1 } }, message("q2")],
|
|
116
|
+
});
|
|
117
|
+
apply(draft, "run/steer", { type: "run/steer", messageId: "q1" });
|
|
118
|
+
expect(run(draft).queue.map((e: Draft) => e.id)).toEqual(["q2"]);
|
|
119
|
+
expect(run(draft).steerQueue.map((e: Draft) => e.id)).toEqual(["q1"]);
|
|
120
|
+
expect(run(draft).steerQueue[0].meta).toEqual({ m: 1 });
|
|
121
|
+
|
|
122
|
+
apply(draft, "run/enqueue", {
|
|
123
|
+
type: "run/enqueue",
|
|
124
|
+
message: { ...message("q2"), parts: [{ type: "text", text: "new" }] },
|
|
125
|
+
});
|
|
126
|
+
expect(run(draft).queue.map((e: Draft) => e.id)).toEqual(["q2"]);
|
|
127
|
+
expect(run(draft).queue[0].parts).toEqual([{ type: "text", text: "new" }]);
|
|
128
|
+
|
|
129
|
+
apply(draft, "run/dequeue", { type: "run/dequeue", messageId: "q1" });
|
|
130
|
+
expect(run(draft).steerQueue).toEqual([]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("run/edit, run/reload, and run/continue start the run optimistically", () => {
|
|
134
|
+
const draft = mount({
|
|
135
|
+
status: "input-required",
|
|
136
|
+
runId: "run-1",
|
|
137
|
+
inputRequests: [{ type: "custom", id: "r1", response: null }],
|
|
138
|
+
});
|
|
139
|
+
apply(draft, "run/edit", {
|
|
140
|
+
type: "run/edit",
|
|
141
|
+
message: message("m2b"),
|
|
142
|
+
sourceId: "m2",
|
|
143
|
+
});
|
|
144
|
+
expect(draft.status).toBe("running");
|
|
145
|
+
expect(run(draft).runId).toBe("optimistic-7");
|
|
146
|
+
expect(run(draft).inputRequests).toBeUndefined();
|
|
147
|
+
|
|
148
|
+
const stopped = mount({ status: "stopped", steerQueue: [message("s1")] });
|
|
149
|
+
apply(stopped, "run/continue", { type: "run/continue" });
|
|
150
|
+
expect(stopped.status).toBe("running");
|
|
151
|
+
expect(run(stopped).steerQueue).toEqual([]);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("dispatches from a bare ready mount by creating the run entry", () => {
|
|
155
|
+
const draft = mount();
|
|
156
|
+
apply(draft, "run/steer", {
|
|
157
|
+
type: "run/steer",
|
|
158
|
+
message: message("s1"),
|
|
159
|
+
anchorMessageId: null,
|
|
160
|
+
});
|
|
161
|
+
expect(draft.status).toBe("running");
|
|
162
|
+
expect(draft.runs).toHaveLength(1);
|
|
163
|
+
expect(run(draft)).toEqual({
|
|
164
|
+
runId: "optimistic-7",
|
|
165
|
+
status: "running",
|
|
166
|
+
queue: [],
|
|
167
|
+
steerQueue: [],
|
|
168
|
+
error: null,
|
|
169
|
+
runLeafMessageId: null,
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("echoes run/input answers and dispatches once the set completes", () => {
|
|
174
|
+
const draft = mount({
|
|
175
|
+
status: "input-required",
|
|
176
|
+
runId: "run-1",
|
|
177
|
+
inputRequests: [
|
|
178
|
+
{ type: "custom", id: "r1", response: null },
|
|
179
|
+
{ type: "custom", id: "r2", response: null },
|
|
180
|
+
],
|
|
181
|
+
});
|
|
182
|
+
apply(draft, "run/input", {
|
|
183
|
+
type: "run/input",
|
|
184
|
+
requestId: "r1",
|
|
185
|
+
response: { ok: true },
|
|
186
|
+
});
|
|
187
|
+
expect(draft.status).toBe("input-required");
|
|
188
|
+
expect(run(draft).inputRequests[0].response).toEqual({ ok: true });
|
|
189
|
+
|
|
190
|
+
apply(draft, "run/input", {
|
|
191
|
+
type: "run/input",
|
|
192
|
+
requestId: "r2",
|
|
193
|
+
response: { ok: true },
|
|
194
|
+
});
|
|
195
|
+
expect(draft.status).toBe("running");
|
|
196
|
+
expect(run(draft).inputRequests).toBeUndefined();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("dispatches a steer over a partially answered set: the answered subset rides, requests clear", () => {
|
|
200
|
+
const draft = mount({
|
|
201
|
+
status: "input-required",
|
|
202
|
+
runId: "run-1",
|
|
203
|
+
inputRequests: [
|
|
204
|
+
{ type: "custom", id: "r1", response: { ok: true } },
|
|
205
|
+
{ type: "custom", id: "r2", response: null },
|
|
206
|
+
],
|
|
207
|
+
});
|
|
208
|
+
apply(draft, "run/steer", {
|
|
209
|
+
type: "run/steer",
|
|
210
|
+
message: message("s1"),
|
|
211
|
+
anchorMessageId: null,
|
|
212
|
+
});
|
|
213
|
+
expect(draft.status).toBe("running");
|
|
214
|
+
expect(run(draft).runId).toBe("optimistic-7");
|
|
215
|
+
expect(run(draft).inputRequests).toBeUndefined();
|
|
216
|
+
expect(run(draft).steerQueue).toEqual([]);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("leaves the draft untouched for run/stop and unknown moves", () => {
|
|
220
|
+
const draft = mount({ status: "running", runId: "run-1" });
|
|
221
|
+
apply(draft, "run/stop", { runId: "run-1" });
|
|
222
|
+
apply(draft, "run/enqueue", { type: "run/enqueue", messageId: "ghost" });
|
|
223
|
+
expect(draft).toEqual(mount({ status: "running", runId: "run-1" }));
|
|
224
|
+
});
|
|
225
|
+
});
|