@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
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
import { resource, useResource } from "@assistant-ui/tap";
|
|
10
10
|
import type { ResourceElement } from "@assistant-ui/tap";
|
|
11
11
|
import { ExternalThread } from "@assistant-ui/react";
|
|
12
|
-
import type { ExternalThreadMessage } from "@assistant-ui/react";
|
|
12
|
+
import type { Assistant, ExternalThreadMessage } from "@assistant-ui/react";
|
|
13
13
|
import {
|
|
14
14
|
forwardTransformScopes,
|
|
15
15
|
useAssistantClientRef,
|
|
@@ -19,6 +19,9 @@ import { createMessageQueue } from "@assistant-ui/core";
|
|
|
19
19
|
import type {
|
|
20
20
|
AddToolResultOptions,
|
|
21
21
|
AppendMessage,
|
|
22
|
+
AttachmentAdapter,
|
|
23
|
+
LanguageModelConfig,
|
|
24
|
+
LanguageModelV1CallSettings,
|
|
22
25
|
MessageQueueController,
|
|
23
26
|
MessageQueueDriver,
|
|
24
27
|
RespondToToolApprovalOptions,
|
|
@@ -26,8 +29,10 @@ import type {
|
|
|
26
29
|
ThreadMessage,
|
|
27
30
|
ToolExecutionStatus,
|
|
28
31
|
} from "@assistant-ui/core";
|
|
32
|
+
import type { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
29
33
|
import { ToolInvocationTracker } from "@assistant-ui/core/internal";
|
|
30
34
|
import { useStatewire } from "statewire";
|
|
35
|
+
import type { StatewireThread } from "./StatewireThread";
|
|
31
36
|
import { AssistantTransportHost } from "./host";
|
|
32
37
|
import type {
|
|
33
38
|
AssistantTransportHostCommands,
|
|
@@ -38,6 +43,7 @@ import type {
|
|
|
38
43
|
} from "./host";
|
|
39
44
|
import {
|
|
40
45
|
createRunQueueAdapter,
|
|
46
|
+
resolveEditCommand,
|
|
41
47
|
resolveReloadCommand,
|
|
42
48
|
toWireMessage,
|
|
43
49
|
viewedLeafId,
|
|
@@ -46,11 +52,6 @@ import {
|
|
|
46
52
|
resolveToolApprovalInput,
|
|
47
53
|
resolveToolResultInput,
|
|
48
54
|
} from "./inputRequests";
|
|
49
|
-
import type {
|
|
50
|
-
AssistantTransportCommand,
|
|
51
|
-
AssistantTransportOptions,
|
|
52
|
-
InputRequestState,
|
|
53
|
-
} from "./types";
|
|
54
55
|
|
|
55
56
|
const EMPTY_TOOL_STATUSES: Record<string, ToolExecutionStatus> = {};
|
|
56
57
|
const EMPTY_QUEUE_ITEMS: MessageQueueController["adapter"]["items"] = [];
|
|
@@ -62,11 +63,13 @@ export const symbolAssistantTransportExtras = Symbol(
|
|
|
62
63
|
|
|
63
64
|
export type AssistantTransportExtras = {
|
|
64
65
|
[symbolAssistantTransportExtras]: true;
|
|
65
|
-
sendCommand: (command:
|
|
66
|
+
sendCommand: (command: AssistantTransportThread.Command) => void;
|
|
66
67
|
reconnect: () => void;
|
|
67
68
|
state: unknown;
|
|
68
69
|
/** The host's parked input requests; absent when the converter does not project them. */
|
|
69
|
-
inputRequests?: readonly InputRequestState[];
|
|
70
|
+
inputRequests?: readonly AssistantTransportThread.InputRequestState[];
|
|
71
|
+
/** The typed runs read; present iff StatewireThread runs with the `runs` option. */
|
|
72
|
+
runs?: StatewireThread.Runs;
|
|
70
73
|
};
|
|
71
74
|
|
|
72
75
|
export const asAssistantTransportExtras = (
|
|
@@ -99,7 +102,7 @@ const messagesContainToolCall = (
|
|
|
99
102
|
);
|
|
100
103
|
|
|
101
104
|
const useAssistantTransportThread = (
|
|
102
|
-
options:
|
|
105
|
+
options: AssistantTransportThread.Options<any>,
|
|
103
106
|
): ClientOutput<"thread"> => {
|
|
104
107
|
const clientRef = useAssistantClientRef();
|
|
105
108
|
const [getRunContext] = useState(() => (): HostRunContext => {
|
|
@@ -128,7 +131,7 @@ const useAssistantTransportThread = (
|
|
|
128
131
|
const queue = queueCell.current;
|
|
129
132
|
|
|
130
133
|
const dispatch = useCallback(
|
|
131
|
-
(command:
|
|
134
|
+
(command: AssistantTransportThread.Command, opts?: SendOptions) => {
|
|
132
135
|
void commands.send(command, opts).catch(() => {});
|
|
133
136
|
},
|
|
134
137
|
[commands],
|
|
@@ -216,9 +219,15 @@ const useAssistantTransportThread = (
|
|
|
216
219
|
[dispatch, connection.reconnect, agentState, inputRequests],
|
|
217
220
|
);
|
|
218
221
|
|
|
222
|
+
const handleEdit = (message: AppendMessage) => {
|
|
223
|
+
const command = resolveEditCommand(message);
|
|
224
|
+
void toolInvocations.abort().then(() => dispatch(command));
|
|
225
|
+
};
|
|
226
|
+
|
|
219
227
|
const handleAppend = (message: AppendMessage) => {
|
|
228
|
+
if (message.sourceId != null) return handleEdit(message);
|
|
220
229
|
const startRun = message.startRun ?? message.role === "user";
|
|
221
|
-
const command:
|
|
230
|
+
const command: AssistantTransportThread.Command = {
|
|
222
231
|
type: "run/steer",
|
|
223
232
|
message: toWireMessage(message, crypto.randomUUID()),
|
|
224
233
|
anchorMessageId: viewedLeafId(convertedRef.current.messages),
|
|
@@ -234,17 +243,6 @@ const useAssistantTransportThread = (
|
|
|
234
243
|
}
|
|
235
244
|
};
|
|
236
245
|
|
|
237
|
-
const handleEdit = (message: AppendMessage) => {
|
|
238
|
-
if (message.sourceId == null)
|
|
239
|
-
throw new Error("statewire: run/edit requires a sourceId");
|
|
240
|
-
const command: AssistantTransportCommand = {
|
|
241
|
-
type: "run/edit",
|
|
242
|
-
message: toWireMessage(message, crypto.randomUUID()),
|
|
243
|
-
sourceId: message.sourceId,
|
|
244
|
-
};
|
|
245
|
-
void toolInvocations.abort().then(() => dispatch(command));
|
|
246
|
-
};
|
|
247
|
-
|
|
248
246
|
const handleCancel = () => {
|
|
249
247
|
void toolInvocations.abort();
|
|
250
248
|
void commands.cancel().catch(() => {});
|
|
@@ -412,12 +410,257 @@ const useAssistantTransportThread = (
|
|
|
412
410
|
};
|
|
413
411
|
|
|
414
412
|
export const AssistantTransportThread = <T>(
|
|
415
|
-
options:
|
|
413
|
+
options: AssistantTransportThread.Options<T>,
|
|
416
414
|
): ResourceElement<ClientOutput<"thread">> =>
|
|
417
415
|
resource(useAssistantTransportThread)(
|
|
418
|
-
options as
|
|
416
|
+
options as AssistantTransportThread.Options<any>,
|
|
419
417
|
);
|
|
420
418
|
|
|
419
|
+
export namespace AssistantTransportThread {
|
|
420
|
+
type UserCommands = Assistant.Commands[keyof Assistant.Commands];
|
|
421
|
+
|
|
422
|
+
export type TextPart = {
|
|
423
|
+
readonly type: "text";
|
|
424
|
+
readonly text: string;
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
export type FilePart = {
|
|
428
|
+
readonly type: "file";
|
|
429
|
+
readonly mediaType: string;
|
|
430
|
+
readonly url: string;
|
|
431
|
+
readonly filename?: string;
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
export type RunUserMessage = {
|
|
435
|
+
readonly id: string;
|
|
436
|
+
readonly role: "user";
|
|
437
|
+
readonly parts: readonly (TextPart | FilePart)[];
|
|
438
|
+
readonly metadata?: Record<string, unknown>;
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
/** Queue placement fields, flat on the command. Absent is load-bearing: it means default placement; `null` names a queue boundary. */
|
|
442
|
+
export type RunPlacement = {
|
|
443
|
+
readonly insertAfter?: string | null;
|
|
444
|
+
readonly insertBefore?: string | null;
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
export type RunEnqueueCommand = {
|
|
448
|
+
readonly type: "run/enqueue";
|
|
449
|
+
} & (
|
|
450
|
+
| {
|
|
451
|
+
readonly message: RunUserMessage;
|
|
452
|
+
/** Branch anchor: the newest message the client knows on its intended branch, null asserting an empty thread. Required on adds; optional on edits of queued items. */
|
|
453
|
+
readonly anchorMessageId?: string | null;
|
|
454
|
+
}
|
|
455
|
+
| { readonly messageId: string }
|
|
456
|
+
) &
|
|
457
|
+
RunPlacement;
|
|
458
|
+
|
|
459
|
+
export type RunSteerCommand = {
|
|
460
|
+
readonly type: "run/steer";
|
|
461
|
+
} & (
|
|
462
|
+
| {
|
|
463
|
+
readonly message: RunUserMessage;
|
|
464
|
+
/** Branch anchor: the newest message the client knows on its intended branch, null asserting an empty thread. Required on adds; optional on edits of queued items. */
|
|
465
|
+
readonly anchorMessageId?: string | null;
|
|
466
|
+
}
|
|
467
|
+
| { readonly messageId: string }
|
|
468
|
+
) &
|
|
469
|
+
RunPlacement;
|
|
470
|
+
|
|
471
|
+
export type RunDequeueCommand = {
|
|
472
|
+
readonly type: "run/dequeue";
|
|
473
|
+
readonly messageId: string;
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
export type RunEditCommand = {
|
|
477
|
+
readonly type: "run/edit";
|
|
478
|
+
readonly message: RunUserMessage;
|
|
479
|
+
readonly sourceId: string;
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
export type RunContinueCommand = {
|
|
483
|
+
readonly type: "run/continue";
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
export type ToolCallInputRequest = {
|
|
487
|
+
readonly type: "tool-call";
|
|
488
|
+
readonly id: string;
|
|
489
|
+
readonly toolCallId: string;
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
export type ToolApprovalInputRequest = {
|
|
493
|
+
readonly type: "tool-approval";
|
|
494
|
+
readonly id: string;
|
|
495
|
+
readonly toolCallId: string;
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
export type CustomInputRequest = {
|
|
499
|
+
readonly type: string;
|
|
500
|
+
readonly id: string;
|
|
501
|
+
readonly [key: string]: ReadonlyJSONValue | undefined;
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
/** A host request parking the run until answered; open union for custom types. */
|
|
505
|
+
export type InputRequest =
|
|
506
|
+
| ToolCallInputRequest
|
|
507
|
+
| ToolApprovalInputRequest
|
|
508
|
+
| CustomInputRequest;
|
|
509
|
+
|
|
510
|
+
export type ToolCallInputResponse = {
|
|
511
|
+
readonly output: ReadonlyJSONValue;
|
|
512
|
+
/** Default false; when true, `output` carries the error payload. */
|
|
513
|
+
readonly isError?: boolean;
|
|
514
|
+
readonly artifact?: ReadonlyJSONValue;
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
export type ToolApprovalInputResponse = {
|
|
518
|
+
readonly decision: "approve" | "reject" | "edit" | "respond";
|
|
519
|
+
readonly editedArgs?: ReadonlyJSONValue;
|
|
520
|
+
readonly message?: string;
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
export type InputResponse = ReadonlyJSONValue;
|
|
524
|
+
|
|
525
|
+
export type RunInputCommand = {
|
|
526
|
+
readonly type: "run/input";
|
|
527
|
+
readonly requestId: string;
|
|
528
|
+
readonly response: InputResponse;
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
/** An input request projected out of the host state with its response echo. */
|
|
532
|
+
export type InputRequestState = InputRequest & {
|
|
533
|
+
readonly response: InputResponse | null;
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
export type RunReloadCommand = {
|
|
537
|
+
readonly type: "run/reload";
|
|
538
|
+
readonly sourceId: string;
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
export type RunCommand =
|
|
542
|
+
| RunEnqueueCommand
|
|
543
|
+
| RunSteerCommand
|
|
544
|
+
| RunDequeueCommand
|
|
545
|
+
| RunEditCommand
|
|
546
|
+
| RunContinueCommand
|
|
547
|
+
| RunReloadCommand
|
|
548
|
+
| RunInputCommand;
|
|
549
|
+
|
|
550
|
+
export type Command = RunCommand | UserCommands;
|
|
551
|
+
|
|
552
|
+
/** A queued message projected out of the host state, wire shape. */
|
|
553
|
+
export type RunQueueItem = {
|
|
554
|
+
readonly id: string;
|
|
555
|
+
readonly parts: readonly (TextPart | FilePart)[];
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
export type State = {
|
|
559
|
+
readonly messages: readonly ThreadMessage[];
|
|
560
|
+
readonly state?: ReadonlyJSONValue;
|
|
561
|
+
readonly isRunning: boolean;
|
|
562
|
+
/**
|
|
563
|
+
* The host's regular queue lane projected out of the state. Returning it
|
|
564
|
+
* (even empty) turns on the server-side queue: sends become `run/enqueue`
|
|
565
|
+
* / `run/steer` commands and item controls send the `run/*` queue
|
|
566
|
+
* commands. Presence must be stable across conversions and excludes the
|
|
567
|
+
* client-local `capabilities.queue` buffer.
|
|
568
|
+
*/
|
|
569
|
+
readonly queue?: readonly RunQueueItem[];
|
|
570
|
+
/** The host's steer lane; requires `queue`. Absent means empty. */
|
|
571
|
+
readonly steerQueue?: readonly RunQueueItem[];
|
|
572
|
+
/**
|
|
573
|
+
* The host's parked input requests projected out of the state. Returning it
|
|
574
|
+
* (even empty) routes tool results and approvals with a matching pending
|
|
575
|
+
* request through `run/input`. Presence must be stable across conversions.
|
|
576
|
+
*/
|
|
577
|
+
readonly inputRequests?: readonly AssistantTransportThread.InputRequestState[];
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
export type ConnectionMetadata = {
|
|
581
|
+
pendingCommands: Command[];
|
|
582
|
+
isSending: boolean;
|
|
583
|
+
toolStatuses: Record<string, ToolExecutionStatus>;
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
export type StateConverter<T> = (
|
|
587
|
+
state: T,
|
|
588
|
+
connectionMetadata: ConnectionMetadata,
|
|
589
|
+
) => State;
|
|
590
|
+
|
|
591
|
+
export type HeadersValue = Record<string, string> | Headers;
|
|
592
|
+
|
|
593
|
+
export type Protocol = "data-stream" | "assistant-transport";
|
|
594
|
+
|
|
595
|
+
export type SendCommandsRequestBody = {
|
|
596
|
+
commands: Command[];
|
|
597
|
+
state: unknown;
|
|
598
|
+
system: string | undefined;
|
|
599
|
+
tools: Record<string, unknown> | undefined;
|
|
600
|
+
callSettings: LanguageModelV1CallSettings | undefined;
|
|
601
|
+
config: LanguageModelConfig | undefined;
|
|
602
|
+
threadId: string | null;
|
|
603
|
+
parentId?: string | null;
|
|
604
|
+
[key: string]: unknown;
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
export type Options<T> = {
|
|
608
|
+
initialState: T;
|
|
609
|
+
converter: StateConverter<T>;
|
|
610
|
+
api: string;
|
|
611
|
+
resumeApi?: string | undefined;
|
|
612
|
+
protocol?: Protocol | undefined;
|
|
613
|
+
headers: HeadersValue | (() => Promise<HeadersValue>);
|
|
614
|
+
body?: object | (() => Promise<object | undefined>) | undefined;
|
|
615
|
+
/** Transform the fully assembled request body before it is sent. */
|
|
616
|
+
prepareSendCommandsRequest?:
|
|
617
|
+
| ((
|
|
618
|
+
body: SendCommandsRequestBody,
|
|
619
|
+
) => Record<string, unknown> | Promise<Record<string, unknown>>)
|
|
620
|
+
| undefined;
|
|
621
|
+
onResponse?: ((response: Response) => void) | undefined;
|
|
622
|
+
onFinish?: (() => void) | undefined;
|
|
623
|
+
onError?:
|
|
624
|
+
| ((
|
|
625
|
+
error: Error,
|
|
626
|
+
params: {
|
|
627
|
+
commands: Command[];
|
|
628
|
+
updateState: (updater: (state: T) => T) => void;
|
|
629
|
+
},
|
|
630
|
+
) => void | Promise<void>)
|
|
631
|
+
| undefined;
|
|
632
|
+
/**
|
|
633
|
+
* Called when commands are cancelled. After an error, queued commands are
|
|
634
|
+
* cancelled once `onError` settles; `error` then carries the cause.
|
|
635
|
+
*/
|
|
636
|
+
onCancel?:
|
|
637
|
+
| ((params: {
|
|
638
|
+
commands: Command[];
|
|
639
|
+
updateState: (updater: (state: T) => T) => void;
|
|
640
|
+
error?: Error;
|
|
641
|
+
}) => void)
|
|
642
|
+
| undefined;
|
|
643
|
+
capabilities?:
|
|
644
|
+
| {
|
|
645
|
+
edit?: boolean | undefined;
|
|
646
|
+
/**
|
|
647
|
+
* Buffer messages sent during a run in an in-memory queue and dispatch
|
|
648
|
+
* them once the run settles. Steering interrupts the active run.
|
|
649
|
+
* Mutually exclusive with the converter's `queue` projection.
|
|
650
|
+
*/
|
|
651
|
+
queue?: boolean | undefined;
|
|
652
|
+
/** The host supports `run/continue` (a continue affordance on aborted runs). */
|
|
653
|
+
continue?: boolean | undefined;
|
|
654
|
+
}
|
|
655
|
+
| undefined;
|
|
656
|
+
adapters?:
|
|
657
|
+
| {
|
|
658
|
+
attachments?: AttachmentAdapter | undefined;
|
|
659
|
+
}
|
|
660
|
+
| undefined;
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
|
|
421
664
|
// Scope transforms are keyed by the underlying hook, not the resource wrapper.
|
|
422
665
|
forwardTransformScopes(
|
|
423
666
|
useAssistantTransportThread,
|