@assistant-ui/react 0.14.14 → 0.14.16
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/README.md +5 -1
- package/dist/client/ExternalThread.d.ts +2 -12
- package/dist/client/ExternalThread.d.ts.map +1 -1
- package/dist/client/ExternalThread.js +30 -29
- package/dist/client/ExternalThread.js.map +1 -1
- package/dist/client/InMemoryThreadList.d.ts.map +1 -1
- package/dist/client/InMemoryThreadList.js +11 -10
- package/dist/client/InMemoryThreadList.js.map +1 -1
- package/dist/client/SingleThreadList.d.ts.map +1 -1
- package/dist/client/SingleThreadList.js +9 -8
- package/dist/client/SingleThreadList.js.map +1 -1
- package/dist/context/providers/ThreadViewportProvider.js +1 -1
- package/dist/context/providers/ThreadViewportProvider.js.map +1 -1
- package/dist/context/react/ThreadViewportContext.js +1 -1
- package/dist/context/react/utils/createContextHook.js +1 -1
- package/dist/context/react/utils/ensureBinding.js.map +1 -1
- package/dist/context/react/utils/useRuntimeState.js +1 -1
- package/dist/context/stores/ThreadViewport.js.map +1 -1
- package/dist/devtools/DevToolsHooks.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/legacy-runtime/AssistantRuntimeProvider.js +1 -1
- package/dist/legacy-runtime/cloud/auiV0.js +1 -1
- package/dist/legacy-runtime/hooks/AssistantContext.js.map +1 -1
- package/dist/legacy-runtime/hooks/AttachmentContext.js.map +1 -1
- package/dist/legacy-runtime/hooks/ComposerContext.js.map +1 -1
- package/dist/legacy-runtime/hooks/MessageContext.js.map +1 -1
- package/dist/legacy-runtime/hooks/MessagePartContext.js.map +1 -1
- package/dist/legacy-runtime/hooks/ThreadContext.js +1 -1
- package/dist/legacy-runtime/hooks/ThreadContext.js.map +1 -1
- package/dist/legacy-runtime/hooks/ThreadListItemContext.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/commandQueue.js +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/replayBoundaryStream.js +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/runManager.js +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.js +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.js +1 -1
- package/dist/mcp-apps/McpAppRenderer.d.ts.map +1 -1
- package/dist/mcp-apps/McpAppRenderer.js +7 -7
- package/dist/mcp-apps/McpAppRenderer.js.map +1 -1
- package/dist/mcp-apps/McpAppsRemoteHost.d.ts.map +1 -1
- package/dist/mcp-apps/McpAppsRemoteHost.js +5 -4
- package/dist/mcp-apps/McpAppsRemoteHost.js.map +1 -1
- package/dist/mcp-apps/app-frame.d.ts +1 -1
- package/dist/mcp-apps/app-frame.d.ts.map +1 -1
- package/dist/mcp-apps/app-frame.js +82 -104
- package/dist/mcp-apps/app-frame.js.map +1 -1
- package/dist/mcp-apps/bridge.d.ts +3 -3
- package/dist/mcp-apps/bridge.d.ts.map +1 -1
- package/dist/mcp-apps/bridge.js +35 -10
- package/dist/mcp-apps/bridge.js.map +1 -1
- package/dist/mcp-apps/types.d.ts +2 -12
- package/dist/mcp-apps/types.d.ts.map +1 -1
- package/dist/mcp-apps/types.js.map +1 -1
- package/dist/model-context/frame/useAssistantFrameHost.js +1 -1
- package/dist/model-context/makeAssistantVisible.js +1 -1
- package/dist/model-context/makeAssistantVisible.js.map +1 -1
- package/dist/primitives/actionBar/ActionBarCopy.js +1 -1
- package/dist/primitives/actionBar/ActionBarExportMarkdown.js +1 -1
- package/dist/primitives/actionBar/ActionBarExportMarkdown.js.map +1 -1
- package/dist/primitives/actionBar/ActionBarFeedbackNegative.js +1 -1
- package/dist/primitives/actionBar/ActionBarFeedbackPositive.js +1 -1
- package/dist/primitives/actionBar/ActionBarInteractionContext.js +1 -1
- package/dist/primitives/actionBar/ActionBarRoot.js +1 -1
- package/dist/primitives/actionBar/ActionBarStopSpeaking.js +1 -1
- package/dist/primitives/actionBarMore/ActionBarMoreContent.js +1 -1
- package/dist/primitives/actionBarMore/ActionBarMoreItem.js +1 -1
- package/dist/primitives/actionBarMore/ActionBarMoreRoot.js +1 -1
- package/dist/primitives/actionBarMore/ActionBarMoreSeparator.js +1 -1
- package/dist/primitives/actionBarMore/ActionBarMoreTrigger.js +1 -1
- package/dist/primitives/assistantModal/AssistantModalAnchor.js +1 -1
- package/dist/primitives/assistantModal/AssistantModalContent.js +1 -1
- package/dist/primitives/assistantModal/AssistantModalRoot.js +1 -1
- package/dist/primitives/assistantModal/AssistantModalTrigger.js +1 -1
- package/dist/primitives/attachment/AttachmentRemove.js +1 -1
- package/dist/primitives/attachment/AttachmentRemove.js.map +1 -1
- package/dist/primitives/attachment/AttachmentRoot.js +1 -1
- package/dist/primitives/attachment/AttachmentThumb.js +1 -1
- package/dist/primitives/branchPicker/BranchPickerRoot.js +1 -1
- package/dist/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.js +1 -1
- package/dist/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.js.map +1 -1
- package/dist/primitives/chainOfThought/ChainOfThoughtRoot.js +1 -1
- package/dist/primitives/composer/ComposerAddAttachment.js +1 -1
- package/dist/primitives/composer/ComposerAddAttachment.js.map +1 -1
- package/dist/primitives/composer/ComposerAttachmentDropzone.js +1 -1
- package/dist/primitives/composer/ComposerAttachmentDropzone.js.map +1 -1
- package/dist/primitives/composer/ComposerDictationTranscript.js +1 -1
- package/dist/primitives/composer/ComposerInput.js +1 -1
- package/dist/primitives/composer/ComposerInput.js.map +1 -1
- package/dist/primitives/composer/ComposerInputPluginContext.js +1 -1
- package/dist/primitives/composer/ComposerQuote.js +1 -1
- package/dist/primitives/composer/ComposerQuote.js.map +1 -1
- package/dist/primitives/composer/ComposerRoot.js +1 -1
- package/dist/primitives/composer/ComposerSend.js +1 -1
- package/dist/primitives/composer/ComposerStopDictation.js +1 -1
- package/dist/primitives/composer/ComposerStopDictation.js.map +1 -1
- package/dist/primitives/composer/trigger/TriggerPopover.js +2 -2
- package/dist/primitives/composer/trigger/TriggerPopover.js.map +1 -1
- package/dist/primitives/composer/trigger/TriggerPopoverAction.js +1 -1
- package/dist/primitives/composer/trigger/TriggerPopoverBack.js +1 -1
- package/dist/primitives/composer/trigger/TriggerPopoverCategories.js +1 -1
- package/dist/primitives/composer/trigger/TriggerPopoverDirective.js +1 -1
- package/dist/primitives/composer/trigger/TriggerPopoverItems.js +1 -1
- package/dist/primitives/composer/trigger/TriggerPopoverResource.d.ts.map +1 -1
- package/dist/primitives/composer/trigger/TriggerPopoverResource.js +8 -7
- package/dist/primitives/composer/trigger/TriggerPopoverResource.js.map +1 -1
- package/dist/primitives/composer/trigger/TriggerPopoverRootContext.js +1 -1
- package/dist/primitives/composer/trigger/triggerDetectionResource.d.ts.map +1 -1
- package/dist/primitives/composer/trigger/triggerDetectionResource.js +5 -4
- package/dist/primitives/composer/trigger/triggerDetectionResource.js.map +1 -1
- package/dist/primitives/composer/trigger/triggerKeyboardResource.d.ts.map +1 -1
- package/dist/primitives/composer/trigger/triggerKeyboardResource.js +8 -7
- package/dist/primitives/composer/trigger/triggerKeyboardResource.js.map +1 -1
- package/dist/primitives/composer/trigger/triggerNavigationResource.d.ts.map +1 -1
- package/dist/primitives/composer/trigger/triggerNavigationResource.js +13 -12
- package/dist/primitives/composer/trigger/triggerNavigationResource.js.map +1 -1
- package/dist/primitives/composer/trigger/triggerSelectionResource.d.ts.map +1 -1
- package/dist/primitives/composer/trigger/triggerSelectionResource.js +7 -6
- package/dist/primitives/composer/trigger/triggerSelectionResource.js.map +1 -1
- package/dist/primitives/error/ErrorMessage.js +1 -1
- package/dist/primitives/error/ErrorRoot.js +1 -1
- package/dist/primitives/message/MessagePartsGrouped.js +1 -1
- package/dist/primitives/message/MessagePartsGrouped.js.map +1 -1
- package/dist/primitives/message/MessageRoot.js +1 -1
- package/dist/primitives/message/MessageRoot.js.map +1 -1
- package/dist/primitives/messagePart/MessagePartImage.js +1 -1
- package/dist/primitives/messagePart/MessagePartText.js +1 -1
- package/dist/primitives/queueItem/QueueItemRemove.js +1 -1
- package/dist/primitives/queueItem/QueueItemRemove.js.map +1 -1
- package/dist/primitives/queueItem/QueueItemSteer.js +1 -1
- package/dist/primitives/queueItem/QueueItemSteer.js.map +1 -1
- package/dist/primitives/queueItem/QueueItemText.js +1 -1
- package/dist/primitives/reasoning/useScrollLock.js +1 -1
- package/dist/primitives/reasoning/useScrollLock.js.map +1 -1
- package/dist/primitives/selectionToolbar/SelectionToolbarQuote.js +1 -1
- package/dist/primitives/selectionToolbar/SelectionToolbarQuote.js.map +1 -1
- package/dist/primitives/selectionToolbar/SelectionToolbarRoot.js +1 -1
- package/dist/primitives/selectionToolbar/SelectionToolbarRoot.js.map +1 -1
- package/dist/primitives/suggestion/SuggestionDescription.js +1 -1
- package/dist/primitives/suggestion/SuggestionTitle.js +1 -1
- package/dist/primitives/suggestion/SuggestionTrigger.js +1 -1
- package/dist/primitives/suggestion/SuggestionTrigger.js.map +1 -1
- package/dist/primitives/thread/ThreadRoot.js +1 -1
- package/dist/primitives/thread/ThreadScrollToBottom.js +1 -1
- package/dist/primitives/thread/ThreadScrollToBottom.js.map +1 -1
- package/dist/primitives/thread/ThreadViewport.js +1 -1
- package/dist/primitives/thread/ThreadViewport.js.map +1 -1
- package/dist/primitives/thread/ThreadViewportFooter.js +1 -1
- package/dist/primitives/thread/ThreadViewportFooter.js.map +1 -1
- package/dist/primitives/thread/topAnchor/topAnchorTurn.js.map +1 -1
- package/dist/primitives/thread/topAnchor/topAnchorUtils.js.map +1 -1
- package/dist/primitives/thread/topAnchor/useTopAnchorReserve.js +1 -1
- package/dist/primitives/thread/useThreadViewportAutoScroll.js +1 -1
- package/dist/primitives/thread/useThreadViewportAutoScroll.js.map +1 -1
- package/dist/primitives/threadList/ThreadListNew.js +1 -1
- package/dist/primitives/threadList/ThreadListRoot.js +1 -1
- package/dist/primitives/threadListItem/ThreadListItemRoot.js +1 -1
- package/dist/primitives/threadListItemMore/ThreadListItemMoreContent.js +1 -1
- package/dist/primitives/threadListItemMore/ThreadListItemMoreItem.js +1 -1
- package/dist/primitives/threadListItemMore/ThreadListItemMoreSeparator.js +1 -1
- package/dist/primitives/threadListItemMore/ThreadListItemMoreTrigger.js +1 -1
- package/dist/sandbox-host/SandboxHost.d.ts +50 -0
- package/dist/sandbox-host/SandboxHost.d.ts.map +1 -0
- package/dist/sandbox-host/SandboxHost.js +85 -0
- package/dist/sandbox-host/SandboxHost.js.map +1 -0
- package/dist/unstable/useMentionAdapter.js +1 -1
- package/dist/unstable/useMentionAdapter.js.map +1 -1
- package/dist/unstable/useSlashCommandAdapter.js +1 -1
- package/dist/unstable/useSlashCommandAdapter.js.map +1 -1
- package/dist/utils/Primitive.js +1 -1
- package/dist/utils/createActionButton.js +1 -1
- package/dist/utils/createActionButton.js.map +1 -1
- package/dist/utils/hooks/useManagedRef.js +1 -1
- package/dist/utils/hooks/useMediaQuery.js +1 -1
- package/dist/utils/hooks/useMediaQuery.js.map +1 -1
- package/dist/utils/hooks/useOnResizeContent.js +1 -1
- package/dist/utils/hooks/useOnScrollToBottom.js +1 -1
- package/dist/utils/hooks/useSizeHandle.js +1 -1
- package/dist/utils/json/is-json.js.map +1 -1
- package/dist/utils/smooth/SmoothContext.js +1 -1
- package/dist/utils/smooth/SmoothContext.js.map +1 -1
- package/dist/utils/smooth/useSmooth.js +1 -1
- package/dist/utils/smooth/useSmooth.js.map +1 -1
- package/dist/utils/useToolArgsFieldStatus.d.ts +2 -2
- package/dist/utils/useToolArgsFieldStatus.d.ts.map +1 -1
- package/package.json +48 -40
- package/src/client/ExternalThread.ts +484 -515
- package/src/client/InMemoryThreadList.ts +153 -162
- package/src/client/SingleThreadList.ts +87 -84
- package/src/context/providers/ThreadViewportProvider.tsx +2 -2
- package/src/index.ts +8 -1
- package/src/mcp-apps/McpAppRenderer.tsx +28 -35
- package/src/mcp-apps/McpAppsRemoteHost.ts +25 -24
- package/src/mcp-apps/app-frame.tsx +100 -141
- package/src/mcp-apps/bridge.test.ts +100 -60
- package/src/mcp-apps/bridge.ts +43 -21
- package/src/mcp-apps/types.ts +2 -12
- package/src/primitives/composer/trigger/TriggerPopover.tsx +1 -1
- package/src/primitives/composer/trigger/TriggerPopoverResource.ts +75 -76
- package/src/primitives/composer/trigger/triggerDetectionResource.ts +6 -5
- package/src/primitives/composer/trigger/triggerKeyboardResource.ts +9 -13
- package/src/primitives/composer/trigger/triggerNavigationResource.ts +14 -19
- package/src/primitives/composer/trigger/triggerSelectionResource.ts +8 -7
- package/src/sandbox-host/SandboxHost.test.tsx +231 -0
- package/src/sandbox-host/SandboxHost.tsx +185 -0
- package/src/tests/local-runtime-queue.test.tsx +305 -0
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
tapState,
|
|
4
|
-
tapMemo,
|
|
5
|
-
tapEffect,
|
|
6
|
-
tapEffectEvent,
|
|
7
|
-
} from "@assistant-ui/tap";
|
|
1
|
+
import { useState, useMemo, useEffect, useEffectEvent } from "react";
|
|
2
|
+
import { resource, withKey } from "@assistant-ui/tap";
|
|
8
3
|
import {
|
|
9
4
|
type ClientElement,
|
|
10
5
|
type ClientOutput,
|
|
11
|
-
|
|
6
|
+
useClientLookup,
|
|
12
7
|
attachTransformScopes,
|
|
13
|
-
|
|
8
|
+
useClientResource,
|
|
14
9
|
Derived,
|
|
15
10
|
} from "@assistant-ui/store";
|
|
16
|
-
|
|
11
|
+
|
|
17
12
|
import type {
|
|
18
13
|
AppendMessage,
|
|
19
14
|
Attachment,
|
|
@@ -21,6 +16,7 @@ import type {
|
|
|
21
16
|
ThreadAssistantMessagePart,
|
|
22
17
|
ThreadUserMessagePart,
|
|
23
18
|
ThreadMessage,
|
|
19
|
+
ExternalThreadQueueAdapter,
|
|
24
20
|
} from "@assistant-ui/core";
|
|
25
21
|
import type { QueueItemState } from "@assistant-ui/core/store";
|
|
26
22
|
import type { ComposerSendOptions } from "@assistant-ui/core/store";
|
|
@@ -34,19 +30,6 @@ export type ExternalThreadMessage = ThreadMessage & {
|
|
|
34
30
|
id: string;
|
|
35
31
|
};
|
|
36
32
|
|
|
37
|
-
export type ExternalThreadQueueAdapter = {
|
|
38
|
-
/** The current queue items. */
|
|
39
|
-
items: readonly QueueItemState[];
|
|
40
|
-
/** Called when a message is submitted via the composer. Receives the steer preference. */
|
|
41
|
-
enqueue: (message: AppendMessage, opts: { steer: boolean }) => void;
|
|
42
|
-
/** Called to promote an existing queue item (cancel current run, run this immediately). */
|
|
43
|
-
steer: (queueItemId: string) => void;
|
|
44
|
-
/** Called to remove an item from the queue. */
|
|
45
|
-
remove: (queueItemId: string) => void;
|
|
46
|
-
/** Called to clear all pending queue items, with the reason for clearing. */
|
|
47
|
-
clear: (reason: "edit" | "reload" | "cancel-run") => void;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
33
|
export type ExternalThreadProps = {
|
|
51
34
|
messages: readonly ExternalThreadMessage[];
|
|
52
35
|
isRunning?: boolean;
|
|
@@ -79,151 +62,147 @@ type MessageClientProps = {
|
|
|
79
62
|
};
|
|
80
63
|
|
|
81
64
|
// Message Client - minimal implementation
|
|
82
|
-
const MessageClient = resource(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
() =>
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
attachment,
|
|
109
|
-
onRemove: () => {},
|
|
110
|
-
}),
|
|
111
|
-
),
|
|
65
|
+
const MessageClient = resource(function MessageClient({
|
|
66
|
+
message,
|
|
67
|
+
index,
|
|
68
|
+
onEdit,
|
|
69
|
+
onReload,
|
|
70
|
+
queue,
|
|
71
|
+
}: MessageClientProps): ClientOutput<"message"> {
|
|
72
|
+
const [isCopied, setIsCopied] = useState(false);
|
|
73
|
+
const [isHovering, setIsHovering] = useState(false);
|
|
74
|
+
const [isEditing, setIsEditing] = useState(false);
|
|
75
|
+
|
|
76
|
+
const partClients = useClientLookup(
|
|
77
|
+
() =>
|
|
78
|
+
message.content.map((part, idx) => withKey(idx, PartResource({ part }))),
|
|
79
|
+
[message.content],
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const attachmentClients = useClientLookup(
|
|
83
|
+
() =>
|
|
84
|
+
(message.attachments ?? []).map((attachment) =>
|
|
85
|
+
withKey(
|
|
86
|
+
attachment.id,
|
|
87
|
+
AttachmentResource({
|
|
88
|
+
attachment,
|
|
89
|
+
onRemove: () => {},
|
|
90
|
+
}),
|
|
112
91
|
),
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const composerClient = tapClientResource(
|
|
135
|
-
ComposerClientResource({
|
|
136
|
-
type: "edit",
|
|
137
|
-
isEditing,
|
|
138
|
-
canCancel: true,
|
|
139
|
-
onCancel: handleCancelEdit,
|
|
140
|
-
onBeginEdit: handleBeginEdit,
|
|
141
|
-
onSend: handleSendEdit,
|
|
142
|
-
message,
|
|
143
|
-
queue,
|
|
144
|
-
}),
|
|
145
|
-
);
|
|
92
|
+
),
|
|
93
|
+
[message.attachments],
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const handleBeginEdit = () => {
|
|
97
|
+
setIsEditing(true);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const handleCancelEdit = () => {
|
|
101
|
+
setIsEditing(false);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const handleSendEdit = (msg: AppendMessage) => {
|
|
105
|
+
queue?.clear("edit");
|
|
106
|
+
onEdit?.({
|
|
107
|
+
...msg,
|
|
108
|
+
parentId: message.id,
|
|
109
|
+
sourceId: message.id,
|
|
110
|
+
});
|
|
111
|
+
setIsEditing(false);
|
|
112
|
+
};
|
|
146
113
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
speech: undefined,
|
|
156
|
-
parts: partClients.state,
|
|
157
|
-
isCopied,
|
|
158
|
-
isHovering,
|
|
159
|
-
index,
|
|
160
|
-
composer: composerClient.state,
|
|
161
|
-
};
|
|
162
|
-
}, [
|
|
114
|
+
const composerClient = useClientResource(
|
|
115
|
+
ComposerClientResource({
|
|
116
|
+
type: "edit",
|
|
117
|
+
isEditing,
|
|
118
|
+
canCancel: true,
|
|
119
|
+
onCancel: handleCancelEdit,
|
|
120
|
+
onBeginEdit: handleBeginEdit,
|
|
121
|
+
onSend: handleSendEdit,
|
|
163
122
|
message,
|
|
123
|
+
queue,
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const state = useMemo(() => {
|
|
128
|
+
return {
|
|
129
|
+
...message,
|
|
130
|
+
attachments: message.attachments ?? [],
|
|
131
|
+
parentId: null,
|
|
132
|
+
isLast: false, // Will be set by thread
|
|
133
|
+
branchNumber: 1,
|
|
134
|
+
branchCount: 1,
|
|
135
|
+
speech: undefined,
|
|
136
|
+
parts: partClients.state,
|
|
164
137
|
isCopied,
|
|
165
138
|
isHovering,
|
|
166
139
|
index,
|
|
167
|
-
composerClient.state,
|
|
168
|
-
partClients.state,
|
|
169
|
-
]);
|
|
170
|
-
|
|
171
|
-
return {
|
|
172
|
-
getState: () => state,
|
|
173
|
-
composer: () => composerClient.methods,
|
|
174
|
-
reload: () => {
|
|
175
|
-
onReload?.();
|
|
176
|
-
},
|
|
177
|
-
speak: () => {},
|
|
178
|
-
stopSpeaking: () => {},
|
|
179
|
-
submitFeedback: () => {},
|
|
180
|
-
switchToBranch: () => {},
|
|
181
|
-
getCopyText: () =>
|
|
182
|
-
message.content.map((c) => ("text" in c ? c.text : "")).join(""),
|
|
183
|
-
part: (selector) => {
|
|
184
|
-
if ("index" in selector) {
|
|
185
|
-
return partClients.get(selector);
|
|
186
|
-
}
|
|
187
|
-
const partIndex = state.parts.findIndex(
|
|
188
|
-
(p) => p.type === "tool-call" && p.toolCallId === selector.toolCallId,
|
|
189
|
-
);
|
|
190
|
-
return partClients.get({ index: partIndex });
|
|
191
|
-
},
|
|
192
|
-
attachment: (selector) => {
|
|
193
|
-
if ("id" in selector) {
|
|
194
|
-
return attachmentClients.get({ key: selector.id });
|
|
195
|
-
}
|
|
196
|
-
return attachmentClients.get(selector);
|
|
197
|
-
},
|
|
198
|
-
setIsCopied,
|
|
199
|
-
setIsHovering,
|
|
140
|
+
composer: composerClient.state,
|
|
200
141
|
};
|
|
201
|
-
},
|
|
202
|
-
|
|
142
|
+
}, [
|
|
143
|
+
message,
|
|
144
|
+
isCopied,
|
|
145
|
+
isHovering,
|
|
146
|
+
index,
|
|
147
|
+
composerClient.state,
|
|
148
|
+
partClients.state,
|
|
149
|
+
]);
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
getState: () => state,
|
|
153
|
+
composer: () => composerClient.methods,
|
|
154
|
+
reload: () => {
|
|
155
|
+
onReload?.();
|
|
156
|
+
},
|
|
157
|
+
speak: () => {},
|
|
158
|
+
stopSpeaking: () => {},
|
|
159
|
+
submitFeedback: () => {},
|
|
160
|
+
switchToBranch: () => {},
|
|
161
|
+
getCopyText: () =>
|
|
162
|
+
message.content.map((c) => ("text" in c ? c.text : "")).join(""),
|
|
163
|
+
part: (selector) => {
|
|
164
|
+
if ("index" in selector) {
|
|
165
|
+
return partClients.get(selector);
|
|
166
|
+
}
|
|
167
|
+
const partIndex = state.parts.findIndex(
|
|
168
|
+
(p) => p.type === "tool-call" && p.toolCallId === selector.toolCallId,
|
|
169
|
+
);
|
|
170
|
+
return partClients.get({ index: partIndex });
|
|
171
|
+
},
|
|
172
|
+
attachment: (selector) => {
|
|
173
|
+
if ("id" in selector) {
|
|
174
|
+
return attachmentClients.get({ key: selector.id });
|
|
175
|
+
}
|
|
176
|
+
return attachmentClients.get(selector);
|
|
177
|
+
},
|
|
178
|
+
setIsCopied,
|
|
179
|
+
setIsHovering,
|
|
180
|
+
};
|
|
181
|
+
});
|
|
203
182
|
|
|
204
183
|
type PartResourceProps = {
|
|
205
184
|
part: ThreadAssistantMessagePart | ThreadUserMessagePart;
|
|
206
185
|
};
|
|
207
186
|
|
|
208
187
|
// Part Client - minimal implementation
|
|
209
|
-
const PartResource = resource(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
);
|
|
188
|
+
const PartResource = resource(function PartResource({
|
|
189
|
+
part,
|
|
190
|
+
}: PartResourceProps): ClientOutput<"part"> {
|
|
191
|
+
const state = useMemo(
|
|
192
|
+
() => ({
|
|
193
|
+
...part,
|
|
194
|
+
status: { type: "complete" as const },
|
|
195
|
+
}),
|
|
196
|
+
[part],
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
getState: () => state,
|
|
201
|
+
addToolResult: () => {},
|
|
202
|
+
resumeToolCall: () => {},
|
|
203
|
+
respondToToolApproval: () => {},
|
|
204
|
+
};
|
|
205
|
+
});
|
|
227
206
|
|
|
228
207
|
type AttachmentResourceProps = {
|
|
229
208
|
attachment: Attachment;
|
|
@@ -231,19 +210,17 @@ type AttachmentResourceProps = {
|
|
|
231
210
|
};
|
|
232
211
|
|
|
233
212
|
// Attachment Client - minimal implementation
|
|
234
|
-
const AttachmentResource = resource(
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
},
|
|
246
|
-
);
|
|
213
|
+
const AttachmentResource = resource(function AttachmentResource({
|
|
214
|
+
attachment,
|
|
215
|
+
onRemove,
|
|
216
|
+
}: AttachmentResourceProps): ClientOutput<"attachment"> {
|
|
217
|
+
return {
|
|
218
|
+
getState: () => attachment,
|
|
219
|
+
remove: async () => {
|
|
220
|
+
onRemove?.();
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
});
|
|
247
224
|
|
|
248
225
|
type ComposerClientResourceProps = {
|
|
249
226
|
type: "thread" | "edit";
|
|
@@ -257,374 +234,366 @@ type ComposerClientResourceProps = {
|
|
|
257
234
|
queue?: ExternalThreadQueueAdapter | undefined;
|
|
258
235
|
};
|
|
259
236
|
|
|
260
|
-
const QueueItemClient = resource(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
},
|
|
276
|
-
);
|
|
237
|
+
const QueueItemClient = resource(function QueueItemClient({
|
|
238
|
+
item,
|
|
239
|
+
onSteer,
|
|
240
|
+
onRemove,
|
|
241
|
+
}: {
|
|
242
|
+
item: QueueItemState;
|
|
243
|
+
onSteer: () => void;
|
|
244
|
+
onRemove: () => void;
|
|
245
|
+
}): ClientOutput<"queueItem"> {
|
|
246
|
+
return {
|
|
247
|
+
getState: () => item,
|
|
248
|
+
steer: onSteer,
|
|
249
|
+
remove: onRemove,
|
|
250
|
+
};
|
|
251
|
+
});
|
|
277
252
|
|
|
278
253
|
// Composer Client - minimal implementation
|
|
279
|
-
const ComposerClientResource = resource(
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
);
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
setAttachments(message.attachments ?? []);
|
|
313
|
-
}
|
|
314
|
-
});
|
|
254
|
+
const ComposerClientResource = resource(function ComposerClientResource({
|
|
255
|
+
type,
|
|
256
|
+
isEditing,
|
|
257
|
+
canCancel,
|
|
258
|
+
isSendDisabled = false,
|
|
259
|
+
onCancel,
|
|
260
|
+
onBeginEdit,
|
|
261
|
+
onSend,
|
|
262
|
+
message,
|
|
263
|
+
queue,
|
|
264
|
+
}: ComposerClientResourceProps): ClientOutput<"composer"> {
|
|
265
|
+
const [text, setText] = useState("");
|
|
266
|
+
const [role, setRole] = useState<"user" | "assistant" | "system">("user");
|
|
267
|
+
const [runConfig, setRunConfig] = useState<Record<string, unknown>>({});
|
|
268
|
+
const [attachments, setAttachments] = useState<readonly Attachment[]>([]);
|
|
269
|
+
const [quote, setQuote] = useState<
|
|
270
|
+
{ readonly text: string; readonly messageId: string } | undefined
|
|
271
|
+
>(undefined);
|
|
272
|
+
|
|
273
|
+
// Update composer values when editing begins
|
|
274
|
+
const updateFromMessage = useEffectEvent(() => {
|
|
275
|
+
if (message) {
|
|
276
|
+
// Extract text from message content (text parts only)
|
|
277
|
+
const textParts = message.content.filter((part) => part.type === "text");
|
|
278
|
+
const messageText = textParts
|
|
279
|
+
.map((part) => ("text" in part ? part.text : ""))
|
|
280
|
+
.join("\n\n");
|
|
281
|
+
|
|
282
|
+
setText(messageText);
|
|
283
|
+
setRole(message.role);
|
|
284
|
+
setAttachments(message.attachments ?? []);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
315
287
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
),
|
|
288
|
+
useEffect(() => {
|
|
289
|
+
if (isEditing) {
|
|
290
|
+
updateFromMessage();
|
|
291
|
+
}
|
|
292
|
+
}, [isEditing]);
|
|
293
|
+
|
|
294
|
+
const attachmentClients = useClientLookup(
|
|
295
|
+
() =>
|
|
296
|
+
attachments.map((attachment, idx) =>
|
|
297
|
+
withKey(
|
|
298
|
+
attachment.id,
|
|
299
|
+
AttachmentResource({
|
|
300
|
+
attachment,
|
|
301
|
+
onRemove: () => {
|
|
302
|
+
setAttachments(attachments.filter((_, i) => i !== idx));
|
|
303
|
+
},
|
|
304
|
+
}),
|
|
334
305
|
),
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
),
|
|
306
|
+
),
|
|
307
|
+
[attachments],
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
const queueItems = queue?.items ?? EMPTY_QUEUE_ITEMS;
|
|
311
|
+
const queueItemClients = useClientLookup(
|
|
312
|
+
() =>
|
|
313
|
+
queueItems.map((item) =>
|
|
314
|
+
withKey(
|
|
315
|
+
item.id,
|
|
316
|
+
QueueItemClient({
|
|
317
|
+
item,
|
|
318
|
+
onSteer: () => queue?.steer(item.id),
|
|
319
|
+
onRemove: () => queue?.remove(item.id),
|
|
320
|
+
}),
|
|
350
321
|
),
|
|
351
|
-
|
|
352
|
-
|
|
322
|
+
),
|
|
323
|
+
[queueItems],
|
|
324
|
+
);
|
|
353
325
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
text,
|
|
358
|
-
role,
|
|
359
|
-
attachments: attachmentClients.state,
|
|
360
|
-
runConfig,
|
|
361
|
-
isEditing,
|
|
362
|
-
canCancel,
|
|
363
|
-
canSend: isEditing && !isEmpty && !isSendDisabled,
|
|
364
|
-
attachmentAccept: "*",
|
|
365
|
-
isEmpty,
|
|
366
|
-
type,
|
|
367
|
-
dictation: undefined,
|
|
368
|
-
quote,
|
|
369
|
-
queue: queueItems,
|
|
370
|
-
};
|
|
371
|
-
}, [
|
|
326
|
+
const state = useMemo(() => {
|
|
327
|
+
const isEmpty = !text.trim() && !attachments.length;
|
|
328
|
+
return {
|
|
372
329
|
text,
|
|
373
330
|
role,
|
|
374
|
-
attachmentClients.state,
|
|
331
|
+
attachments: attachmentClients.state,
|
|
375
332
|
runConfig,
|
|
376
333
|
isEditing,
|
|
377
334
|
canCancel,
|
|
378
|
-
isSendDisabled,
|
|
335
|
+
canSend: isEditing && !isEmpty && !isSendDisabled,
|
|
336
|
+
attachmentAccept: "*",
|
|
337
|
+
isEmpty,
|
|
379
338
|
type,
|
|
380
|
-
|
|
339
|
+
dictation: undefined,
|
|
381
340
|
quote,
|
|
382
|
-
queueItems,
|
|
383
|
-
]);
|
|
384
|
-
|
|
385
|
-
return {
|
|
386
|
-
getState: () => state,
|
|
387
|
-
setText,
|
|
388
|
-
setRole,
|
|
389
|
-
setRunConfig,
|
|
390
|
-
addAttachment: async (fileOrAttachment: File | CreateAttachment) => {
|
|
391
|
-
if (fileOrAttachment instanceof File) {
|
|
392
|
-
const newAttachment: Attachment = {
|
|
393
|
-
id: Math.random().toString(36).substring(7),
|
|
394
|
-
type: "file",
|
|
395
|
-
name: fileOrAttachment.name,
|
|
396
|
-
contentType: fileOrAttachment.type,
|
|
397
|
-
file: fileOrAttachment,
|
|
398
|
-
status: { type: "complete" },
|
|
399
|
-
content: [],
|
|
400
|
-
};
|
|
401
|
-
setAttachments([...attachments, newAttachment]);
|
|
402
|
-
} else {
|
|
403
|
-
const newAttachment: Attachment = {
|
|
404
|
-
id: fileOrAttachment.id ?? Math.random().toString(36).substring(7),
|
|
405
|
-
type: fileOrAttachment.type ?? "document",
|
|
406
|
-
name: fileOrAttachment.name,
|
|
407
|
-
contentType: fileOrAttachment.contentType,
|
|
408
|
-
content: fileOrAttachment.content,
|
|
409
|
-
status: { type: "complete" },
|
|
410
|
-
};
|
|
411
|
-
setAttachments([...attachments, newAttachment]);
|
|
412
|
-
}
|
|
413
|
-
},
|
|
414
|
-
clearAttachments: async () => {
|
|
415
|
-
setAttachments([]);
|
|
416
|
-
},
|
|
417
|
-
attachment: (selector) => {
|
|
418
|
-
if ("id" in selector) {
|
|
419
|
-
return attachmentClients.get({ key: selector.id });
|
|
420
|
-
}
|
|
421
|
-
return attachmentClients.get(selector);
|
|
422
|
-
},
|
|
423
|
-
reset: async () => {
|
|
424
|
-
setText("");
|
|
425
|
-
setRole("user");
|
|
426
|
-
setRunConfig({});
|
|
427
|
-
setAttachments([]);
|
|
428
|
-
setQuote(undefined);
|
|
429
|
-
},
|
|
430
|
-
send: (opts?: ComposerSendOptions) => {
|
|
431
|
-
if (!state.canSend) return;
|
|
432
|
-
|
|
433
|
-
const currentQuote = quote;
|
|
434
|
-
const composedMessage: AppendMessage = {
|
|
435
|
-
role,
|
|
436
|
-
content: text ? [{ type: "text" as const, text }] : [],
|
|
437
|
-
attachments: attachments as any,
|
|
438
|
-
createdAt: new Date(),
|
|
439
|
-
parentId: null,
|
|
440
|
-
sourceId: null,
|
|
441
|
-
runConfig,
|
|
442
|
-
startRun: opts?.startRun,
|
|
443
|
-
metadata: {
|
|
444
|
-
custom: { ...(currentQuote ? { quote: currentQuote } : {}) },
|
|
445
|
-
},
|
|
446
|
-
};
|
|
447
|
-
if (queue) {
|
|
448
|
-
queue.enqueue(composedMessage, { steer: opts?.steer ?? false });
|
|
449
|
-
} else {
|
|
450
|
-
onSend?.(composedMessage);
|
|
451
|
-
}
|
|
452
|
-
setText("");
|
|
453
|
-
setAttachments([]);
|
|
454
|
-
setQuote(undefined);
|
|
455
|
-
},
|
|
456
|
-
cancel: onCancel,
|
|
457
|
-
beginEdit: () => {
|
|
458
|
-
onBeginEdit?.();
|
|
459
|
-
},
|
|
460
|
-
startDictation: () => {},
|
|
461
|
-
stopDictation: () => {},
|
|
462
|
-
setQuote,
|
|
463
|
-
queueItem: (selector: { index: number }) => {
|
|
464
|
-
return queueItemClients.get(selector);
|
|
465
|
-
},
|
|
341
|
+
queue: queueItems,
|
|
466
342
|
};
|
|
467
|
-
},
|
|
468
|
-
|
|
343
|
+
}, [
|
|
344
|
+
text,
|
|
345
|
+
role,
|
|
346
|
+
attachmentClients.state,
|
|
347
|
+
runConfig,
|
|
348
|
+
isEditing,
|
|
349
|
+
canCancel,
|
|
350
|
+
isSendDisabled,
|
|
351
|
+
type,
|
|
352
|
+
attachments.length,
|
|
353
|
+
quote,
|
|
354
|
+
queueItems,
|
|
355
|
+
]);
|
|
356
|
+
|
|
357
|
+
return {
|
|
358
|
+
getState: () => state,
|
|
359
|
+
setText,
|
|
360
|
+
setRole,
|
|
361
|
+
setRunConfig,
|
|
362
|
+
addAttachment: async (fileOrAttachment: File | CreateAttachment) => {
|
|
363
|
+
if (fileOrAttachment instanceof File) {
|
|
364
|
+
const newAttachment: Attachment = {
|
|
365
|
+
id: Math.random().toString(36).substring(7),
|
|
366
|
+
type: "file",
|
|
367
|
+
name: fileOrAttachment.name,
|
|
368
|
+
contentType: fileOrAttachment.type,
|
|
369
|
+
file: fileOrAttachment,
|
|
370
|
+
status: { type: "complete" },
|
|
371
|
+
content: [],
|
|
372
|
+
};
|
|
373
|
+
setAttachments([...attachments, newAttachment]);
|
|
374
|
+
} else {
|
|
375
|
+
const newAttachment: Attachment = {
|
|
376
|
+
id: fileOrAttachment.id ?? Math.random().toString(36).substring(7),
|
|
377
|
+
type: fileOrAttachment.type ?? "document",
|
|
378
|
+
name: fileOrAttachment.name,
|
|
379
|
+
contentType: fileOrAttachment.contentType,
|
|
380
|
+
content: fileOrAttachment.content,
|
|
381
|
+
status: { type: "complete" },
|
|
382
|
+
};
|
|
383
|
+
setAttachments([...attachments, newAttachment]);
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
clearAttachments: async () => {
|
|
387
|
+
setAttachments([]);
|
|
388
|
+
},
|
|
389
|
+
attachment: (selector) => {
|
|
390
|
+
if ("id" in selector) {
|
|
391
|
+
return attachmentClients.get({ key: selector.id });
|
|
392
|
+
}
|
|
393
|
+
return attachmentClients.get(selector);
|
|
394
|
+
},
|
|
395
|
+
reset: async () => {
|
|
396
|
+
setText("");
|
|
397
|
+
setRole("user");
|
|
398
|
+
setRunConfig({});
|
|
399
|
+
setAttachments([]);
|
|
400
|
+
setQuote(undefined);
|
|
401
|
+
},
|
|
402
|
+
send: (opts?: ComposerSendOptions) => {
|
|
403
|
+
if (!state.canSend) return;
|
|
404
|
+
|
|
405
|
+
const currentQuote = quote;
|
|
406
|
+
const composedMessage: AppendMessage = {
|
|
407
|
+
role,
|
|
408
|
+
content: text ? [{ type: "text" as const, text }] : [],
|
|
409
|
+
attachments: attachments as any,
|
|
410
|
+
createdAt: new Date(),
|
|
411
|
+
parentId: null,
|
|
412
|
+
sourceId: null,
|
|
413
|
+
runConfig,
|
|
414
|
+
startRun: opts?.startRun,
|
|
415
|
+
metadata: {
|
|
416
|
+
custom: { ...(currentQuote ? { quote: currentQuote } : {}) },
|
|
417
|
+
},
|
|
418
|
+
};
|
|
419
|
+
if (queue) {
|
|
420
|
+
queue.enqueue(composedMessage, { steer: opts?.steer ?? false });
|
|
421
|
+
} else {
|
|
422
|
+
onSend?.(composedMessage);
|
|
423
|
+
}
|
|
424
|
+
setText("");
|
|
425
|
+
setAttachments([]);
|
|
426
|
+
setQuote(undefined);
|
|
427
|
+
},
|
|
428
|
+
cancel: onCancel,
|
|
429
|
+
beginEdit: () => {
|
|
430
|
+
onBeginEdit?.();
|
|
431
|
+
},
|
|
432
|
+
startDictation: () => {},
|
|
433
|
+
stopDictation: () => {},
|
|
434
|
+
setQuote,
|
|
435
|
+
queueItem: (selector: { index: number }) => {
|
|
436
|
+
return queueItemClients.get(selector);
|
|
437
|
+
},
|
|
438
|
+
};
|
|
439
|
+
});
|
|
469
440
|
|
|
470
441
|
// External Thread Client
|
|
471
|
-
export const ExternalThread = resource(
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
const
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
() =>
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
return withKey(msg.id, MessageClient(props));
|
|
503
|
-
}),
|
|
504
|
-
[messages, onEdit, queue],
|
|
505
|
-
);
|
|
506
|
-
|
|
507
|
-
const handleCancelRun = () => {
|
|
508
|
-
queue?.clear("cancel-run");
|
|
509
|
-
onCancel?.();
|
|
510
|
-
};
|
|
511
|
-
|
|
512
|
-
const handleSendNew = (message: AppendMessage) => {
|
|
513
|
-
onNew?.(message);
|
|
514
|
-
};
|
|
515
|
-
|
|
516
|
-
const composerClient = tapClientResource(
|
|
517
|
-
ComposerClientResource({
|
|
518
|
-
type: "thread",
|
|
519
|
-
isEditing: true,
|
|
520
|
-
canCancel: isRunning,
|
|
521
|
-
isSendDisabled,
|
|
522
|
-
onCancel: handleCancelRun,
|
|
523
|
-
onSend: handleSendNew,
|
|
524
|
-
queue,
|
|
442
|
+
export const ExternalThread = resource(function ExternalThread({
|
|
443
|
+
messages,
|
|
444
|
+
isRunning = false,
|
|
445
|
+
isSendDisabled = false,
|
|
446
|
+
onNew,
|
|
447
|
+
onEdit,
|
|
448
|
+
onReload,
|
|
449
|
+
onStartRun,
|
|
450
|
+
onCancel,
|
|
451
|
+
queue,
|
|
452
|
+
}: ExternalThreadProps): ClientOutput<"thread"> {
|
|
453
|
+
const handleReload = (messageId: string) => {
|
|
454
|
+
const messageIndex = messages.findIndex((m) => m.id === messageId);
|
|
455
|
+
if (messageIndex === -1) return;
|
|
456
|
+
|
|
457
|
+
const parentId = messageIndex > 0 ? messages[messageIndex - 1]!.id : null;
|
|
458
|
+
queue?.clear("reload");
|
|
459
|
+
onReload?.(parentId);
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
const messageClients = useClientLookup(
|
|
463
|
+
() =>
|
|
464
|
+
messages.map((msg, index) => {
|
|
465
|
+
const props: MessageClientProps = {
|
|
466
|
+
message: msg,
|
|
467
|
+
index,
|
|
468
|
+
onReload: () => handleReload(msg.id),
|
|
469
|
+
queue,
|
|
470
|
+
};
|
|
471
|
+
if (onEdit) props.onEdit = onEdit;
|
|
472
|
+
return withKey(msg.id, MessageClient(props));
|
|
525
473
|
}),
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
suggestions: [],
|
|
557
|
-
extras: undefined,
|
|
558
|
-
speech: undefined,
|
|
559
|
-
voice: undefined,
|
|
560
|
-
composer: composerClient.state,
|
|
561
|
-
};
|
|
562
|
-
}, [
|
|
563
|
-
messages,
|
|
564
|
-
isRunning,
|
|
565
|
-
hasQueue,
|
|
566
|
-
messageClients.state,
|
|
567
|
-
composerClient.state,
|
|
568
|
-
]);
|
|
474
|
+
[messages, onEdit, queue],
|
|
475
|
+
);
|
|
476
|
+
|
|
477
|
+
const handleCancelRun = () => {
|
|
478
|
+
queue?.clear("cancel-run");
|
|
479
|
+
onCancel?.();
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
const handleSendNew = (message: AppendMessage) => {
|
|
483
|
+
onNew?.(message);
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
const composerClient = useClientResource(
|
|
487
|
+
ComposerClientResource({
|
|
488
|
+
type: "thread",
|
|
489
|
+
isEditing: true,
|
|
490
|
+
canCancel: isRunning,
|
|
491
|
+
isSendDisabled,
|
|
492
|
+
onCancel: handleCancelRun,
|
|
493
|
+
onSend: handleSendNew,
|
|
494
|
+
queue,
|
|
495
|
+
}),
|
|
496
|
+
);
|
|
497
|
+
|
|
498
|
+
const hasQueue = !!queue;
|
|
499
|
+
const state = useMemo(() => {
|
|
500
|
+
const messageStates = messageClients.state.map((s, idx, arr) => ({
|
|
501
|
+
...s,
|
|
502
|
+
isLast: idx === arr.length - 1,
|
|
503
|
+
}));
|
|
569
504
|
|
|
570
505
|
return {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
parentId: message.parentId ?? messages.at(-1)?.id ?? null,
|
|
589
|
-
sourceId: message.sourceId ?? null,
|
|
590
|
-
role: message.role ?? "user",
|
|
591
|
-
content: message.content,
|
|
592
|
-
attachments: message.attachments ?? [],
|
|
593
|
-
metadata: message.metadata ?? { custom: {} },
|
|
594
|
-
runConfig: message.runConfig ?? {},
|
|
595
|
-
startRun: message.startRun,
|
|
596
|
-
};
|
|
597
|
-
if (queue) {
|
|
598
|
-
queue.enqueue(appendMessage, { steer: false });
|
|
599
|
-
} else {
|
|
600
|
-
onNew?.(appendMessage);
|
|
601
|
-
}
|
|
602
|
-
},
|
|
603
|
-
startRun: () => {
|
|
604
|
-
onStartRun?.();
|
|
605
|
-
},
|
|
606
|
-
resumeRun: () => {},
|
|
607
|
-
cancelRun: handleCancelRun,
|
|
608
|
-
getModelContext: () => ({ tools: {}, config: {} }),
|
|
609
|
-
export: () => ({ messages: [] }),
|
|
610
|
-
import: () => {},
|
|
611
|
-
reset: () => {},
|
|
612
|
-
message: (selector) => {
|
|
613
|
-
if ("id" in selector) {
|
|
614
|
-
return messageClients.get({ key: selector.id });
|
|
615
|
-
}
|
|
616
|
-
return messageClients.get(selector);
|
|
506
|
+
isEmpty: messages.length === 0,
|
|
507
|
+
isDisabled: false,
|
|
508
|
+
isLoading: false,
|
|
509
|
+
isRunning,
|
|
510
|
+
capabilities: {
|
|
511
|
+
edit: false,
|
|
512
|
+
reload: false,
|
|
513
|
+
cancel: isRunning,
|
|
514
|
+
speech: false,
|
|
515
|
+
attachments: false,
|
|
516
|
+
feedback: false,
|
|
517
|
+
voice: false,
|
|
518
|
+
switchToBranch: false,
|
|
519
|
+
switchBranchDuringRun: false,
|
|
520
|
+
unstable_copy: false,
|
|
521
|
+
dictation: false,
|
|
522
|
+
queue: hasQueue,
|
|
617
523
|
},
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
524
|
+
messages: messageStates,
|
|
525
|
+
state: {},
|
|
526
|
+
suggestions: [],
|
|
527
|
+
extras: undefined,
|
|
528
|
+
speech: undefined,
|
|
529
|
+
voice: undefined,
|
|
530
|
+
composer: composerClient.state,
|
|
625
531
|
};
|
|
626
|
-
},
|
|
627
|
-
|
|
532
|
+
}, [
|
|
533
|
+
messages,
|
|
534
|
+
isRunning,
|
|
535
|
+
hasQueue,
|
|
536
|
+
messageClients.state,
|
|
537
|
+
composerClient.state,
|
|
538
|
+
]);
|
|
539
|
+
|
|
540
|
+
return {
|
|
541
|
+
getState: () => state,
|
|
542
|
+
composer: () => composerClient.methods,
|
|
543
|
+
append: (message) => {
|
|
544
|
+
const appendMessage: AppendMessage =
|
|
545
|
+
typeof message === "string"
|
|
546
|
+
? {
|
|
547
|
+
createdAt: new Date(),
|
|
548
|
+
parentId: messages.at(-1)?.id ?? null,
|
|
549
|
+
sourceId: null,
|
|
550
|
+
runConfig: {},
|
|
551
|
+
role: "user",
|
|
552
|
+
content: [{ type: "text", text: message }],
|
|
553
|
+
attachments: [],
|
|
554
|
+
metadata: { custom: {} },
|
|
555
|
+
}
|
|
556
|
+
: {
|
|
557
|
+
createdAt: message.createdAt ?? new Date(),
|
|
558
|
+
parentId: message.parentId ?? messages.at(-1)?.id ?? null,
|
|
559
|
+
sourceId: message.sourceId ?? null,
|
|
560
|
+
role: message.role ?? "user",
|
|
561
|
+
content: message.content,
|
|
562
|
+
attachments: message.attachments ?? [],
|
|
563
|
+
metadata: message.metadata ?? { custom: {} },
|
|
564
|
+
runConfig: message.runConfig ?? {},
|
|
565
|
+
startRun: message.startRun,
|
|
566
|
+
};
|
|
567
|
+
if (queue) {
|
|
568
|
+
queue.enqueue(appendMessage, { steer: false });
|
|
569
|
+
} else {
|
|
570
|
+
onNew?.(appendMessage);
|
|
571
|
+
}
|
|
572
|
+
},
|
|
573
|
+
startRun: () => {
|
|
574
|
+
onStartRun?.();
|
|
575
|
+
},
|
|
576
|
+
resumeRun: () => {},
|
|
577
|
+
cancelRun: handleCancelRun,
|
|
578
|
+
getModelContext: () => ({ tools: {}, config: {} }),
|
|
579
|
+
export: () => ({ messages: [] }),
|
|
580
|
+
import: () => {},
|
|
581
|
+
reset: () => {},
|
|
582
|
+
message: (selector) => {
|
|
583
|
+
if ("id" in selector) {
|
|
584
|
+
return messageClients.get({ key: selector.id });
|
|
585
|
+
}
|
|
586
|
+
return messageClients.get(selector);
|
|
587
|
+
},
|
|
588
|
+
stopSpeaking: () => {},
|
|
589
|
+
connectVoice: () => {},
|
|
590
|
+
disconnectVoice: () => {},
|
|
591
|
+
getVoiceVolume: () => 0,
|
|
592
|
+
subscribeVoiceVolume: () => () => {},
|
|
593
|
+
muteVoice: () => {},
|
|
594
|
+
unmuteVoice: () => {},
|
|
595
|
+
};
|
|
596
|
+
});
|
|
628
597
|
|
|
629
598
|
attachTransformScopes(ExternalThread, (scopes, parent) => {
|
|
630
599
|
if (!scopes.threads && parent.threads.source === null) {
|