@assistant-ui/react-statewire 0.13.0 → 0.14.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.
Files changed (54) hide show
  1. package/dist/StatewireThread.d.ts +62 -97
  2. package/dist/StatewireThread.d.ts.map +1 -1
  3. package/dist/StatewireThread.js +31 -46
  4. package/dist/StatewireThread.js.map +1 -1
  5. package/dist/hooks.d.ts +6 -9
  6. package/dist/hooks.d.ts.map +1 -1
  7. package/dist/hooks.js +9 -20
  8. package/dist/hooks.js.map +1 -1
  9. package/dist/index.d.ts +2 -3
  10. package/dist/index.js +2 -3
  11. package/dist/inputRequests.d.ts +4 -4
  12. package/dist/inputRequests.d.ts.map +1 -1
  13. package/dist/inputRequests.js.map +1 -1
  14. package/dist/runCommands.d.ts +8 -24
  15. package/dist/runCommands.d.ts.map +1 -1
  16. package/dist/runCommands.js +3 -3
  17. package/dist/runCommands.js.map +1 -1
  18. package/dist/runsOptimistic.d.ts.map +1 -1
  19. package/dist/runsOptimistic.js +3 -2
  20. package/dist/runsOptimistic.js.map +1 -1
  21. package/package.json +4 -5
  22. package/src/StatewireThread.runs.test.tsx +10 -15
  23. package/src/StatewireThread.test.tsx +227 -405
  24. package/src/StatewireThread.ts +98 -210
  25. package/src/__tests__/testHelpers.tsx +6 -226
  26. package/src/hooks.ts +11 -32
  27. package/src/index.ts +2 -4
  28. package/src/inputRequests.ts +6 -8
  29. package/src/runCommands.ts +17 -23
  30. package/src/runsOptimistic.ts +5 -3
  31. package/dist/AssistantTransportThread.d.ts +0 -227
  32. package/dist/AssistantTransportThread.d.ts.map +0 -1
  33. package/dist/AssistantTransportThread.js +0 -264
  34. package/dist/AssistantTransportThread.js.map +0 -1
  35. package/dist/host.d.ts +0 -38
  36. package/dist/host.d.ts.map +0 -1
  37. package/dist/host.js +0 -299
  38. package/dist/host.js.map +0 -1
  39. package/dist/replayBoundaryStream.d.ts +0 -6
  40. package/dist/replayBoundaryStream.d.ts.map +0 -1
  41. package/dist/replayBoundaryStream.js +0 -49
  42. package/dist/replayBoundaryStream.js.map +0 -1
  43. package/dist/stateOps.d.ts +0 -10
  44. package/dist/stateOps.d.ts.map +0 -1
  45. package/dist/stateOps.js +0 -114
  46. package/dist/stateOps.js.map +0 -1
  47. package/src/AssistantTransportThread.ts +0 -791
  48. package/src/athena.e2e.test.tsx +0 -654
  49. package/src/host.ts +0 -437
  50. package/src/legacy-diff.test.tsx +0 -628
  51. package/src/replayBoundaryStream.ts +0 -71
  52. package/src/stateOps.test.ts +0 -165
  53. package/src/stateOps.ts +0 -126
  54. package/src/transport.test.tsx +0 -3001
@@ -1,38 +1,46 @@
1
- import { AssistantTransportThread } from "./AssistantTransportThread.js";
2
1
  import { ResourceElement } from "@assistant-ui/tap";
3
2
  import { ClientOutput } from "@assistant-ui/store";
4
- import { AttachmentAdapter, FeedbackAdapter, SpeechSynthesisAdapter, ThreadMessage } from "@assistant-ui/core";
5
3
  import { Statewire, StatewireClient } from "statewire";
4
+ import { Assistant, AttachmentAdapter, FeedbackAdapter, SpeechSynthesisAdapter, ThreadMessage } from "@assistant-ui/core";
6
5
  import { ReadonlyJSONValue } from "assistant-stream/utils";
7
6
  //#region src/StatewireThread.d.ts
8
- declare const StatewireThread: {
9
- <T>(options: StatewireThread.RunsOptions<T>): ResourceElement<ClientOutput<"thread">>;
10
- <T>(options: StatewireThread.Options<T>): ResourceElement<ClientOutput<"thread">>;
11
- };
7
+ declare const symbolStatewireExtras: unique symbol;
8
+ declare const asStatewireExtras: (extras: unknown) => StatewireThread.Extras;
9
+ declare const StatewireThread: <T>(options: StatewireThread.Options<T>) => ResourceElement<ClientOutput<"thread">>;
12
10
  declare namespace StatewireThread {
13
- type Transport<T> = ResourceElement<Statewire<T | undefined, any>>;
14
- type TextPart = {
11
+ type UserCommands = Assistant.Commands[keyof Assistant.Commands];
12
+ export type Transport<T> = ResourceElement<Statewire<T | undefined, any>>;
13
+ export type Extras = {
14
+ [symbolStatewireExtras]: true;
15
+ sendCommand: (command: Command) => void;
16
+ reconnect: () => void;
17
+ state: unknown;
18
+ status: ThreadStatus;
19
+ inputRequests: readonly InputRequestState[];
20
+ runs: Runs;
21
+ };
22
+ export type TextPart = {
15
23
  readonly type: "text";
16
24
  readonly text: string;
17
25
  };
18
- type FilePart = {
26
+ export type FilePart = {
19
27
  readonly type: "file";
20
28
  readonly mediaType: string;
21
29
  readonly url: string;
22
30
  readonly filename?: string;
23
31
  };
24
- type UserMessage = {
32
+ export type UserMessage = {
25
33
  readonly id: string;
26
34
  readonly role: "user";
27
35
  readonly parts: readonly (TextPart | FilePart)[];
28
36
  readonly metadata?: Record<string, unknown>;
29
37
  };
30
38
  /** Queue placement fields, flat on the command. Absent is load-bearing: it means default placement; `null` names a queue boundary. */
31
- type Placement = {
39
+ export type Placement = {
32
40
  readonly insertAfter?: string | null;
33
41
  readonly insertBefore?: string | null;
34
42
  };
35
- type RunEnqueueCommand = {
43
+ export type RunEnqueueCommand = {
36
44
  readonly type: "run/enqueue";
37
45
  /** The run this command targets — the live run's id, or a fresh client-minted uuid on run-creating sends. */
38
46
  readonly runId: string;
@@ -43,7 +51,7 @@ declare namespace StatewireThread {
43
51
  } | {
44
52
  readonly messageId: string;
45
53
  }) & Placement;
46
- type RunSteerCommand = {
54
+ export type RunSteerCommand = {
47
55
  readonly type: "run/steer";
48
56
  /** The run this command targets — the live run's id, or a fresh client-minted uuid on run-creating sends. */
49
57
  readonly runId: string;
@@ -54,12 +62,12 @@ declare namespace StatewireThread {
54
62
  } | {
55
63
  readonly messageId: string;
56
64
  }) & Placement;
57
- type RunDequeueCommand = {
65
+ export type RunDequeueCommand = {
58
66
  readonly type: "run/dequeue";
59
67
  readonly runId: string;
60
68
  readonly messageId: string;
61
69
  };
62
- type RunEditCommand = {
70
+ export type RunEditCommand = {
63
71
  readonly type: "run/edit";
64
72
  /** The live run's id when redirecting it, or a fresh client-minted uuid when idle. */
65
73
  readonly runId: string;
@@ -68,50 +76,39 @@ declare namespace StatewireThread {
68
76
  /** Asserts `sourceId`'s parent — the rewind target; null names the root. */
69
77
  readonly runAnchorMessageId: string | null;
70
78
  };
71
- type RunContinueCommand = {
79
+ export type RunContinueCommand = {
72
80
  readonly type: "run/continue";
73
81
  readonly runId?: string;
74
82
  };
75
- type ToolCallInputRequest = {
83
+ export type ToolCallInputRequest = {
76
84
  readonly type: "tool-call";
77
85
  readonly id: string;
78
86
  readonly toolCallId: string;
79
87
  };
80
- type ToolApprovalInputRequest = {
88
+ export type ToolApprovalInputRequest = {
81
89
  readonly type: "tool-approval";
82
90
  readonly id: string;
83
91
  readonly toolCallId: string;
84
92
  };
85
- type CustomInputRequest = {
93
+ export type CustomInputRequest = {
86
94
  readonly type: string;
87
95
  readonly id: string;
88
96
  readonly [key: string]: ReadonlyJSONValue | undefined;
89
97
  };
90
98
  /** A host request parking the run until answered; open union for custom types. */
91
- type InputRequest = ToolCallInputRequest | ToolApprovalInputRequest | CustomInputRequest;
92
- type ToolCallInputResponse = {
93
- readonly output: ReadonlyJSONValue;
94
- /** Default false; when true, `output` carries the error payload. */
95
- readonly isError?: boolean;
96
- readonly artifact?: ReadonlyJSONValue;
97
- };
98
- type ToolApprovalInputResponse = {
99
- readonly decision: "approve" | "reject" | "edit" | "respond";
100
- readonly editedArgs?: ReadonlyJSONValue;
101
- readonly message?: string;
102
- };
103
- type InputResponse = ReadonlyJSONValue;
104
- type RunInputCommand = {
99
+ export type InputRequest = ToolCallInputRequest | ToolApprovalInputRequest | CustomInputRequest;
100
+ export type InputResponse = ReadonlyJSONValue;
101
+ export type RunInputCommand = {
105
102
  readonly type: "run/input";
106
103
  readonly runId?: string;
107
104
  readonly requestId: string;
108
105
  readonly response: InputResponse;
109
106
  };
110
107
  /** An input request projected out of the host state with its response echo. */
111
- type InputRequestState = InputRequest & {
108
+ export type InputRequestState = InputRequest & {
112
109
  readonly response: InputResponse | null;
113
110
  };
114
- type RunReloadCommand = {
111
+ export type RunReloadCommand = {
115
112
  readonly type: "run/reload";
116
113
  /** The live run's id when redirecting it, or a fresh client-minted uuid when idle. */
117
114
  readonly runId: string;
@@ -119,53 +116,30 @@ declare namespace StatewireThread {
119
116
  /** Asserts `sourceId`'s parent — the rewind target; null names the root. */
120
117
  readonly runAnchorMessageId: string | null;
121
118
  };
122
- type RunCommand = RunEnqueueCommand | RunSteerCommand | RunDequeueCommand | RunEditCommand | RunContinueCommand | RunReloadCommand | RunInputCommand;
123
- type Command = AssistantTransportThread.Command | RunCommand;
119
+ export type RunCommand = RunEnqueueCommand | RunSteerCommand | RunDequeueCommand | RunEditCommand | RunContinueCommand | RunReloadCommand | RunInputCommand;
120
+ export type Command = RunCommand | UserCommands;
124
121
  /** A queued message projected out of the host state, wire shape. */
125
- type QueueItem = {
122
+ export type RunQueueItem = {
126
123
  readonly id: string;
127
124
  readonly parts: readonly (TextPart | FilePart)[];
128
125
  };
129
- type State = {
130
- readonly messages: readonly ThreadMessage[];
131
- readonly state?: ReadonlyJSONValue;
132
- readonly isRunning: boolean;
133
- /** The live run's id projected out of the host state; stamped onto `run/stop`. */
134
- readonly runId?: string | null;
135
- /**
136
- * The host's regular queue lane projected out of the state. Returning it
137
- * (even empty) turns on the queue capability: sends become `run/enqueue`
138
- * / `run/steer` commands and item controls send the `run/*` queue
139
- * commands. Presence must be stable across conversions.
140
- */
141
- readonly queue?: readonly QueueItem[];
142
- /** The host's steer lane; requires `queue`. Absent means empty. */
143
- readonly steerQueue?: readonly QueueItem[];
144
- /**
145
- * The host's parked input requests projected out of the state. Returning
146
- * it (even empty) routes tool results and approvals with a matching
147
- * pending request through `run/input`. Presence must be stable across
148
- * conversions.
149
- */
150
- readonly inputRequests?: readonly InputRequestState[];
151
- };
152
126
  /** Run subsystem status replicated by the host. */
153
- type RunStatus = "running" | "stopped" | "error" | "input-required";
127
+ export type RunStatus = "running" | "stopped" | "error" | "input-required";
154
128
  /** The thread's status, derived from the state: `"loading"` while the runs mount is absent, `"ready"` while resting, otherwise the run's status. */
155
- type ThreadStatus = "loading" | "ready" | RunStatus;
129
+ export type ThreadStatus = "loading" | "ready" | RunStatus;
156
130
  /** A queue-lane entry as replicated: the user message plus host-stamped `meta`. */
157
- type RunsQueueEntry = UserMessage & {
131
+ export type RunsQueueEntry = UserMessage & {
158
132
  /** Host-stamped opaque per-entry metadata. */
159
133
  readonly meta?: ReadonlyJSONValue;
160
134
  };
161
135
  /** An input outcome riding a dispatch record: the request with its response (null = abandoned) and `run/input` meta. */
162
- type RunsInputOutcome = {
136
+ export type RunsInputOutcome = {
163
137
  readonly request: InputRequest;
164
138
  readonly response: InputResponse | null;
165
139
  readonly meta: ReadonlyJSONValue | null;
166
140
  };
167
141
  /** The dispatch record — the unacked batch handed to the run; null between the ack and the next dispatch or take. */
168
- type RunsDispatch = {
142
+ export type RunsDispatch = {
169
143
  readonly trigger: "message-send" | "message-edit" | "message-reload" | "input-resume" | "error-continue" | "stop-continue" | "steer";
170
144
  readonly messages: readonly RunsQueueEntry[];
171
145
  /** Present on rewind dispatches: truncate the thread at this id first; null names the root. */
@@ -174,12 +148,12 @@ declare namespace StatewireThread {
174
148
  readonly inputOutcomes?: readonly RunsInputOutcome[];
175
149
  };
176
150
  /** The replicated run error — set on an unhandled failure, cleared by the next user-initiated run. */
177
- type RunsError = {
151
+ export type RunsError = {
178
152
  readonly message: string;
179
153
  readonly [key: string]: ReadonlyJSONValue | undefined;
180
154
  };
181
155
  /** A replicated run entry — one element of the mount's `runs` array. */
182
- type RunState = {
156
+ export type RunState = {
183
157
  /** The run's identity — client-minted, adopted by the host at run creation; persists through steers, input-required, and error/stop, retiring only when the queue drains and the run completes. */
184
158
  readonly runId: string | null;
185
159
  readonly status: RunStatus;
@@ -192,21 +166,21 @@ declare namespace StatewireThread {
192
166
  readonly error: RunsError | null;
193
167
  };
194
168
  /** App-shaped effects `runsMaterialize` applies for the dispatch record. */
195
- type MaterializeEffects = {
169
+ export type MaterializeEffects = {
196
170
  /** Append one dispatched message to the app's committed message state. */
197
171
  append: (message: RunsQueueEntry) => void;
198
172
  /** Truncate to the rolled-back view first (null names the root); required for hosts that dispatch edits/reloads. */
199
173
  rollback?: (to: string | null) => void;
200
174
  };
201
175
  /** The runs mount as replicated: the run entries plus the top-level status projection. */
202
- type RunsState = {
176
+ export type RunsState = {
203
177
  /** Projection of `runs[0]?.status ?? "ready"`. */
204
178
  readonly status: Exclude<ThreadStatus, "loading">;
205
179
  /** At most one entry; empty while resting. */
206
180
  readonly runs: readonly RunState[];
207
181
  };
208
- /** The run subsystem's typed read — `runs[0]` of the selected mount with unsettled commands folded in, resting defaults while no entry is live. */
209
- type Runs = {
182
+ /** The run subsystem's typed read — `runs[0]` of the state with unsettled commands folded in, resting defaults while no entry is live. */
183
+ export type Runs = {
210
184
  /** The live run's id — non-null whenever a run entry is live (running, input-required, error/stop); the target for `run/*` sends. */
211
185
  readonly runId: string | null;
212
186
  readonly queue: readonly RunsQueueEntry[];
@@ -217,37 +191,37 @@ declare namespace StatewireThread {
217
191
  readonly inputRequests?: readonly InputRequestState[];
218
192
  readonly error: RunsError | null;
219
193
  };
220
- type ConnectionMetadata = {
194
+ export type ConnectionMetadata = {
221
195
  pendingCommands: Command[];
222
196
  isSending: boolean;
223
197
  /** Statewire connection status; local transports are always `live`. */
224
198
  connection: StatewireClient.ConnectionState;
225
- /** The typed runs read; present iff the `runs` option is set. */
226
- runs?: Runs;
199
+ /** The typed runs read. */
200
+ runs: Runs;
227
201
  };
228
- type StateConverter<T> = (state: T, connectionMetadata: ConnectionMetadata) => State;
229
- /** Converter output in runs mode: app-owned keys only — the lib projects the run mounts itself. */
230
- type RunsConverterState = {
202
+ /** Converter output: app-owned keys only — the lib projects the run mounts itself. */
203
+ export type State = {
231
204
  readonly messages: readonly ThreadMessage[];
232
205
  readonly state?: ReadonlyJSONValue;
233
206
  };
234
- type RunsStateConverter<T> = (state: T, connectionMetadata: ConnectionMetadata & {
235
- runs: Runs;
236
- }) => RunsConverterState;
207
+ export type StateConverter<T> = (state: T, connectionMetadata: ConnectionMetadata) => State;
237
208
  /**
238
209
  * Payload of the built-in `run/stop` command. `reason` is the one well-known
239
210
  * field; apps extend it with their own JSON-serializable fields.
240
211
  */
241
- type StopPayload = {
212
+ export type StopPayload = {
242
213
  readonly reason?: string | undefined;
243
214
  readonly [key: string]: ReadonlyJSONValue | undefined;
244
215
  };
245
- type Options<T> = {
216
+ export type Options<T> = {
246
217
  transport: Transport<T>;
247
- /** The pre-attach state, replaced by the attach snapshot. With runs, `runs: []` projects ready (interactive from the first render); an absent runs mount projects loading until attach. */
218
+ /** The pre-attach state, replaced by the attach snapshot. `runs: []` projects ready (interactive from the first render); an absent runs mount projects loading until attach. */
248
219
  initialState: T;
249
220
  converter: StateConverter<T>;
250
- runs?: undefined;
221
+ /** Folds app-owned keys for an unsettled command into the draft; runs after the lib's run-dialect reducer. */
222
+ optimistic?: StatewireClient.Optimistic<T, Record<string, (...args: any[]) => unknown>> | undefined;
223
+ /** Makes dispatch records concrete in the draft (runs after the optimistic folds); without it, dispatch limbo stays out of `messages`. */
224
+ materialize?: StatewireClient.Materialize<NonNullable<T>> | undefined;
251
225
  /**
252
226
  * "command-send" mirrors the client's ambient model context (system,
253
227
  * tools, callSettings, config) to the server over the command channel —
@@ -280,18 +254,9 @@ declare namespace StatewireThread {
280
254
  feedback?: FeedbackAdapter | undefined;
281
255
  } | undefined;
282
256
  };
283
- /** Options with the runs read enabled: the lib types the run mounts and the converter returns app-owned keys only. */
284
- type RunsOptions<T> = Omit<Options<T>, "converter" | "runs"> & {
285
- /** `true` reads the run mounts off the state root; a selector picks a nested mount. */
286
- runs: true | ((state: T) => unknown);
287
- converter: RunsStateConverter<T>;
288
- /** Folds app-owned keys for an unsettled command into the draft; runs after the lib's run-dialect reducer. */
289
- optimistic?: StatewireClient.Optimistic<T, Record<string, (...args: any[]) => unknown>> | undefined;
290
- /** Makes dispatch records concrete in the draft (runs after the optimistic folds); without it, dispatch limbo stays out of `messages`. */
291
- materialize?: StatewireClient.Materialize<NonNullable<T>> | undefined;
292
- };
257
+ export {};
293
258
  }
294
- declare module "@assistant-ui/react" {
259
+ declare module "@assistant-ui/core" {
295
260
  namespace Assistant {
296
261
  interface Commands {
297
262
  runEnqueue: StatewireThread.RunEnqueueCommand;
@@ -305,5 +270,5 @@ declare module "@assistant-ui/react" {
305
270
  }
306
271
  }
307
272
  //#endregion
308
- export { StatewireThread };
273
+ export { StatewireThread, asStatewireExtras, symbolStatewireExtras };
309
274
  //# sourceMappingURL=StatewireThread.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"StatewireThread.d.ts","names":[],"sources":["../src/StatewireThread.ts"],"mappings":";;;;;;;cAgYa;GACV,GACC,SAAS,gBAAgB,YAAY,KACpC,gBAAgB;GAClB,GACC,SAAS,gBAAgB,QAAQ,KAChC,gBAAgB;;kBAIJ;OACH,UAAU,KAAK,gBAAgB,UAAU;OAEzC;aACD;aACA;;OAGC;aACD;aACA;aACA;aACA;;OAGC;aACD;aACA;aACA,iBAAiB,WAAW;aAC5B,WAAW;;;OAIV;aACD;aACA;;OAGC;aACD;;aAEA;;aAGI,SAAS;;aAET;;aAEA;OAEb;OAEU;aACD;;aAEA;;aAGI,SAAS;;aAET;;aAEA;OAEb;OAEU;aACD;aACA;aACA;;OAGC;aACD;;aAEA;aACA,SAAS;aACT;;aAEA;;OAGC;aACD;aACA;;OAGC;aACD;aACA;aACA;;OAGC;aACD;aACA;aACA;;OAGC;aACD;aACA;cACC,cAAc;;;OAId,eACR,uBACA,2BACA;OAEQ;aACD,QAAQ;;aAER;aACA,WAAW;;OAGV;aACD;aACA,aAAa;aACb;;OAGC,gBAAgB;OAEhB;aACD;aACA;aACA;aACA,UAAU;;;OAIT,oBAAoB;aACrB,UAAU;;OAGT;aACD;;aAEA;aACA;;aAEA;;OAGC,aACR,oBACA,kBACA,oBACA,iBACA,qBACA,mBACA;OAEQ,UAAU,yBAAyB,UAAU;;OAG7C;aACD;aACA,iBAAiB,WAAW;;OAG3B;aACD,mBAAmB;aACnB,QAAQ;aACR;;aAEA;;;;;;;aAOA,iBAAiB;;aAEjB,sBAAsB;;;;;;;aAOtB,yBAAyB;;;OAIxB;;OAGA,qCAAqC;;OAGrC,iBAAiB;;aAElB,OAAO;;;OAIN;aACD,SAAS;aACT,UAAU;aACV,MAAM;;;OAIL;aACD;aAQA,mBAAmB;;aAEnB;;aAEA,yBAAyB;;;OAIxB;aACD;cACC,cAAc;;;OAId;;aAED;aACA,QAAQ;aACR,gBAAgB;aAChB,qBAAqB;;aAErB,aAAa;;aAEb,yBAAyB;aACzB,OAAO;;;OAIN;;IAEV,SAAS,SAAS;;IAElB,YAAY;;;OAIF;;aAED,QAAQ,QAAQ;;aAEhB,eAAe;;;OAId;;aAED;aACA,gBAAgB;aAChB,qBAAqB;;aAErB,aAAa;;aAEb,yBAAyB;aACzB,OAAO;;OAGN;IACV,iBAAiB;IACjB;;IAEA,YAAY,gBAAgB;;IAE5B,OAAO;;OAGG,eAAe,MACzB,OAAO,GACP,oBAAoB,uBACjB;;OAGO;aACD,mBAAmB;aACnB,QAAQ;;OAGP,mBAAmB,MAC7B,OAAO,GACP,oBAAoB;IAAuB,MAAM;QAC9C;;;;;OAMO;aACD;cACC,cAAc;;OAGd,QAAQ;IAClB,WAAW,UAAU;;IAErB,cAAc;IACd,WAAW,eAAe;IAC1B;;;;;;IAMA;;IAEA,qBAAqB;;IAErB,oBAEM,QAAQ,UAAU,cAChB,mBAAmB,2BAErB;MAAO,OAAO,YAAY;;;IAIhC,kBACM,OAAO,YAAY,IAAI;MAAO,SAAS,UAAU;;;IAGvD,uBACM,YAAY,gBAAgB;IAElC,YAAY,OAAO;IACnB;MAEM;;MAEA;;MAEA;;IAGN;MAEM,cAAc;MACd,SAAS;MACT,WAAW;;;;OAMP,YAAY,KAAK,KAAK,QAAQ;;IAExC,eAAe,OAAO;IACtB,WAAW,mBAAmB;;IAE9B,aACI,gBAAgB,WACd,GACA,mBAAmB;;IAIzB,cAAc,gBAAgB,YAAY,YAAY;;;;YAK9C;cACE;MACR,YAAY,gBAAgB;MAC5B,UAAU,gBAAgB;MAC1B,YAAY,gBAAgB;MAC5B,SAAS,gBAAgB;MACzB,aAAa,gBAAgB;MAC7B,WAAW,gBAAgB;MAC3B,UAAU,gBAAgB"}
1
+ {"version":3,"file":"StatewireThread.d.ts","names":[],"sources":["../src/StatewireThread.ts"],"mappings":";;;;;;cAwCa;cAEA,oBAAqB,oBAAkB,gBAAgB;cAiRvD,kBAAmB,GAC9B,SAAS,gBAAgB,QAAQ,OAChC,gBAAgB;kBAGF;OACV,eAAe,UAAU,eAAe,UAAU;cAE3C,UAAU,KAAK,gBAAgB,UAAU;cAEzC;KACT;IACD,cAAc,SAAS;IACvB;IACA;IACA,QAAQ;IACR,wBAAwB;IACxB,MAAM;;cAGI;aACD;aACA;;cAGC;aACD;aACA;aACA;aACA;;cAGC;aACD;aACA;aACA,iBAAiB,WAAW;aAC5B,WAAW;;;cAIV;aACD;aACA;;cAGC;aACD;;aAEA;;aAGI,SAAS;;aAET;;aAEA;OAEb;cAEU;aACD;;aAEA;;aAGI,SAAS;;aAET;;aAEA;OAEb;cAEU;aACD;aACA;aACA;;cAGC;aACD;;aAEA;aACA,SAAS;aACT;;aAEA;;cAGC;aACD;aACA;;cAGC;aACD;aACA;aACA;;cAGC;aACD;aACA;aACA;;cAGC;aACD;aACA;cACC,cAAc;;;cAId,eACR,uBACA,2BACA;cAEQ,gBAAgB;cAEhB;aACD;aACA;aACA;aACA,UAAU;;;cAIT,oBAAoB;aACrB,UAAU;;cAGT;aACD;;aAEA;aACA;;aAEA;;cAGC,aACR,oBACA,kBACA,oBACA,iBACA,qBACA,mBACA;cAEQ,UAAU,aAAa;;cAGvB;aACD;aACA,iBAAiB,WAAW;;;cAI3B;;cAGA,qCAAqC;;cAGrC,iBAAiB;;aAElB,OAAO;;;cAIN;aACD,SAAS;aACT,UAAU;aACV,MAAM;;;cAIL;aACD;aAQA,mBAAmB;;aAEnB;;aAEA,yBAAyB;;;cAIxB;aACD;cACC,cAAc;;;cAId;;aAED;aACA,QAAQ;aACR,gBAAgB;aAChB,qBAAqB;;aAErB,aAAa;;aAEb,yBAAyB;aACzB,OAAO;;;cAIN;;IAEV,SAAS,SAAS;;IAElB,YAAY;;;cAIF;;aAED,QAAQ,QAAQ;;aAEhB,eAAe;;;cAId;;aAED;aACA,gBAAgB;aAChB,qBAAqB;;aAErB,aAAa;;aAEb,yBAAyB;aACzB,OAAO;;cAGN;IACV,iBAAiB;IACjB;;IAEA,YAAY,gBAAgB;;IAE5B,MAAM;;;cAII;aACD,mBAAmB;aACnB,QAAQ;;cAGP,eAAe,MACzB,OAAO,GACP,oBAAoB,uBACjB;;;;;cAMO;aACD;cACC,cAAc;;cAGd,QAAQ;IAClB,WAAW,UAAU;;IAErB,cAAc;IACd,WAAW,eAAe;;IAE1B,aACI,gBAAgB,WACd,GACA,mBAAmB;;IAIzB,cAAc,gBAAgB,YAAY,YAAY;;;;;;IAMtD;;IAEA,qBAAqB;;IAErB,oBAEM,QAAQ,UAAU,cAChB,mBAAmB,2BAErB;MAAO,OAAO,YAAY;;;IAIhC,kBACM,OAAO,YAAY,IAAI;MAAO,SAAS,UAAU;;;IAGvD,uBACM,YAAY,gBAAgB;IAElC,YAAY,OAAO;IACnB;MAEM;;MAEA;;MAEA;;IAGN;MAEM,cAAc;MACd,SAAS;MACT,WAAW;;;;;;YAOT;cACE;MACR,YAAY,gBAAgB;MAC5B,UAAU,gBAAgB;MAC1B,YAAY,gBAAgB;MAC5B,SAAS,gBAAgB;MACzB,aAAa,gBAAgB;MAC7B,WAAW,gBAAgB;MAC3B,UAAU,gBAAgB"}
@@ -1,14 +1,19 @@
1
1
  import { createRunQueueAdapter, resolveEditCommand, resolveReloadCommand, sendFields, toWireMessage } from "./runCommands.js";
2
- import { runsOptimistic } from "./runsOptimistic.js";
3
2
  import { resolveToolApprovalInput, resolveToolResultInput } from "./inputRequests.js";
4
- import { symbolAssistantTransportExtras } from "./AssistantTransportThread.js";
3
+ import { runsOptimistic } from "./runsOptimistic.js";
5
4
  import { useCallback, useEffect, useMemo, useRef, useState } from "@assistant-ui/tap/react-shim";
6
5
  import { resource, useResource } from "@assistant-ui/tap";
7
- import { ExternalThread } from "@assistant-ui/react";
6
+ import { ExternalThread } from "@assistant-ui/core/store";
8
7
  import { forwardTransformScopes, useAssistantClientRef } from "@assistant-ui/store";
9
- import { useStatewire } from "statewire";
10
8
  import { toToolsJSONSchema } from "assistant-stream";
9
+ import { useStatewire } from "statewire";
11
10
  //#region src/StatewireThread.ts
11
+ const symbolStatewireExtras = Symbol("statewire-extras");
12
+ const asStatewireExtras = (extras) => {
13
+ if (typeof extras !== "object" || extras == null || !(symbolStatewireExtras in extras)) throw new Error("This method can only be called when you are using StatewireThread");
14
+ return extras;
15
+ };
16
+ const NO_INPUT_REQUESTS = [];
12
17
  const RESTING_RUNS = {
13
18
  runId: null,
14
19
  queue: [],
@@ -27,21 +32,14 @@ const useStatewireThread = (options) => {
27
32
  latest.current = options;
28
33
  const [pending, setPending] = useState([]);
29
34
  const [retire] = useState(() => (command) => setPending((prev) => prev.includes(command) ? prev.filter((c) => c !== command) : prev));
30
- const runsOption = options.runs;
31
- const [runsSeam] = useState(() => runsOption !== void 0);
32
- if (runsSeam !== (runsOption !== void 0)) throw new Error("statewire: the runs option must be stable — it was " + (runsSeam ? "set" : "unset") + " on first render and changed since");
33
- const materializeOption = options.materialize;
34
- const [materializeSeam] = useState(() => materializeOption !== void 0);
35
- if (materializeSeam !== (materializeOption !== void 0)) throw new Error("statewire: the materialize option must be stable — it was " + (materializeSeam ? "set" : "unset") + " on first render and changed since");
36
35
  const { state: state_0, commands, connection: connection_0, setContext } = useStatewire({
37
36
  transport: options.transport,
38
37
  initialState: options.initialState,
39
- ...runsSeam && { optimistic: (draft, command_0, ctx) => {
40
- const runsOpt = latest.current.runs;
41
- runsOptimistic(runsOpt === true ? draft : runsOpt(draft), command_0, ctx);
38
+ optimistic: (draft, command_0, ctx) => {
39
+ runsOptimistic(draft, command_0, ctx);
42
40
  latest.current.optimistic?.(draft, command_0, ctx);
43
- } },
44
- ...materializeSeam && { materialize: (draft_0) => latest.current.materialize(draft_0) },
41
+ },
42
+ materialize: (draft_0) => latest.current.materialize?.(draft_0),
45
43
  onCommandChange: (update, ctx_0) => {
46
44
  if (update.status === "acked" || update.status === "settled") retire(update.params[0]);
47
45
  latest.current.onCommandChange?.(update, ctx_0);
@@ -93,8 +91,7 @@ const useStatewireThread = (options) => {
93
91
  });
94
92
  }, [dispatch]);
95
93
  const { status, runs } = useMemo(() => {
96
- if (runsOption === void 0) return { status: "ready" };
97
- const entries = (runsOption === true ? state_0 : runsOption(state_0))?.runs;
94
+ const entries = state_0?.runs;
98
95
  const entry = entries?.[0];
99
96
  return {
100
97
  status: entries === void 0 ? "loading" : entry?.status ?? "ready",
@@ -107,13 +104,13 @@ const useStatewireThread = (options) => {
107
104
  error: entry.error
108
105
  }
109
106
  };
110
- }, [runsOption, state_0]);
107
+ }, [state_0]);
111
108
  const { converter } = options;
112
109
  const metadata = useMemo(() => ({
113
110
  pendingCommands: [...pending],
114
111
  isSending: pending.length > 0,
115
112
  connection: connection_0,
116
- ...runs !== void 0 && { runs }
113
+ runs
117
114
  }), [
118
115
  pending,
119
116
  connection_0,
@@ -125,31 +122,23 @@ const useStatewireThread = (options) => {
125
122
  metadata
126
123
  ]);
127
124
  const messages = converted.messages;
128
- const liveRunId = runs !== void 0 ? runs.runId : converted.runId ?? null;
129
- const queueItems = runs !== void 0 ? runs.queue : converted.queue;
130
- const steerQueueItems = runs !== void 0 ? runs.steerQueue : converted.steerQueue;
131
- if (steerQueueItems !== void 0 && queueItems === void 0) throw new Error("statewire: the converter's steerQueue projection requires the queue projection");
132
- const [queueSeam] = useState(() => ({ present: queueItems !== void 0 }));
133
- if (queueSeam.present !== (queueItems !== void 0)) throw new Error("statewire: the converter's queue projection must be stable — it was " + (queueSeam.present ? "defined" : "undefined") + " on first convert and changed since");
134
- const queue = useMemo(() => queueItems ? createRunQueueAdapter(messages, liveRunId, queueItems, steerQueueItems, dispatch, dispatchSend) : void 0, [
125
+ const liveRunId = runs.runId;
126
+ const queue = useMemo(() => createRunQueueAdapter(messages, liveRunId, runs.queue, runs.steerQueue, dispatch, dispatchSend), [
135
127
  messages,
136
128
  liveRunId,
137
- queueItems,
138
- steerQueueItems,
129
+ runs,
139
130
  dispatch,
140
131
  dispatchSend
141
132
  ]);
142
- const inputRequests = runs !== void 0 ? runs.inputRequests ?? [] : converted.inputRequests;
143
- const [inputSeam] = useState(() => ({ present: inputRequests !== void 0 }));
144
- if (inputSeam.present !== (inputRequests !== void 0)) throw new Error("statewire: the converter's inputRequests projection must be stable — it was " + (inputSeam.present ? "defined" : "undefined") + " on first convert and changed since");
133
+ const inputRequests = runs.inputRequests ?? NO_INPUT_REQUESTS;
145
134
  const extras = useMemo(() => ({
146
- [symbolAssistantTransportExtras]: true,
135
+ [symbolStatewireExtras]: true,
147
136
  sendCommand: dispatch,
148
137
  reconnect: connection_0.reconnect,
149
138
  state: state_0,
150
139
  status,
151
- ...inputRequests !== void 0 && { inputRequests },
152
- ...runs !== void 0 && { runs }
140
+ inputRequests,
141
+ runs
153
142
  }), [
154
143
  dispatch,
155
144
  connection_0.reconnect,
@@ -172,33 +161,29 @@ const useStatewireThread = (options) => {
172
161
  };
173
162
  const thread = useResource(ExternalThread({
174
163
  messages,
175
- isRunning: runs !== void 0 ? status === "running" : converted.isRunning,
164
+ isRunning: status === "running",
176
165
  state: converted.state,
177
166
  extras,
178
167
  attachmentAdapter: options.adapters?.attachments,
179
168
  speechAdapter: options.adapters?.speech,
180
169
  feedbackAdapter: options.adapters?.feedback,
181
170
  onNew: handleAppend,
182
- ...queue && { queue },
171
+ queue,
183
172
  ...options.capabilities?.edit && { onEdit: handleEdit },
184
173
  ...options.capabilities?.continue && { onResume: () => dispatch({
185
174
  type: "run/continue",
186
175
  ...liveRunId !== null && { runId: liveRunId }
187
176
  }) },
188
177
  ...options.capabilities?.reload && { onReload: handleReload },
189
- ...inputRequests !== void 0 && {
190
- onAddToolResult: (toolOptions) => dispatch(resolveToolResultInput(inputRequests, toolOptions, liveRunId)),
191
- onRespondToToolApproval: (approval) => dispatch(resolveToolApprovalInput(inputRequests, approval, liveRunId))
192
- },
178
+ onAddToolResult: (toolOptions) => dispatch(resolveToolResultInput(inputRequests, toolOptions, liveRunId)),
179
+ onRespondToToolApproval: (approval) => dispatch(resolveToolApprovalInput(inputRequests, approval, liveRunId)),
193
180
  onCancel: () => {
194
181
  const payload = latest.current.stopPayload?.();
195
182
  if (payload !== void 0 && (typeof payload !== "object" || payload === null || Array.isArray(payload))) throw new Error("statewire: stopPayload must return a plain object");
196
- const runId = runs !== void 0 ? runs.runId : converted.runId;
197
- const args = runId === void 0 ? payload === void 0 ? [] : [payload] : [{
183
+ commands["run/stop"]({
198
184
  ...payload,
199
- runId
200
- }];
201
- commands["run/stop"](...args).catch((error_1) => {
185
+ runId: runs.runId
186
+ }).catch((error_1) => {
202
187
  latest.current.onError?.(error_1);
203
188
  });
204
189
  }
@@ -209,6 +194,6 @@ const useStatewireThread = (options) => {
209
194
  const StatewireThread = (options) => resource(useStatewireThread)(options);
210
195
  forwardTransformScopes(useStatewireThread, ExternalThread(void 0).hook);
211
196
  //#endregion
212
- export { StatewireThread };
197
+ export { StatewireThread, asStatewireExtras, symbolStatewireExtras };
213
198
 
214
199
  //# sourceMappingURL=StatewireThread.js.map