@meshagent/meshagent-tailwind 0.47.0 → 0.48.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/CHANGELOG.md +15 -0
- package/dist/cjs/chat/agent-thread.d.ts +24 -24
- package/dist/cjs/chat/agent-thread.js +575 -808
- package/dist/cjs/chat/chat-bot-view.d.ts +18 -6
- package/dist/cjs/chat/chat-bot-view.js +29 -7
- package/dist/cjs/chat/chat-feed-widget-view.d.ts +7 -0
- package/dist/cjs/chat/chat-feed-widget-view.js +50 -0
- package/dist/cjs/chat/chat-feed-widget.d.ts +16 -0
- package/dist/cjs/chat/chat-feed-widget.js +51 -0
- package/dist/cjs/chat/chat-hooks.d.ts +2 -2
- package/dist/cjs/chat/chat-input.d.ts +3 -3
- package/dist/cjs/chat/chat-input.js +9 -4
- package/dist/cjs/chat/chat-scroller.d.ts +21 -0
- package/dist/cjs/chat/chat-scroller.js +139 -0
- package/dist/cjs/chat/chat-thread-timeline.d.ts +66 -0
- package/dist/cjs/chat/chat-thread-timeline.js +595 -0
- package/dist/cjs/chat/dataset-agent-thread.d.ts +16 -4
- package/dist/cjs/chat/dataset-agent-thread.js +112 -292
- package/dist/cjs/chat/multi-thread-view.d.ts +8 -3
- package/dist/cjs/chat/multi-thread-view.js +10 -2
- package/dist/cjs/chat/new-chat-thread.d.ts +8 -3
- package/dist/cjs/chat/new-chat-thread.js +63 -24
- package/dist/cjs/chat/thread-view.d.ts +18 -5
- package/dist/cjs/chat/thread-view.js +67 -9
- package/dist/cjs/chat/use-chat-virtualizer.d.ts +47 -0
- package/dist/cjs/chat/use-chat-virtualizer.js +336 -0
- package/dist/cjs/chat/virtualization.d.ts +44 -0
- package/dist/cjs/chat/virtualization.js +143 -0
- package/dist/cjs/components/ui/button.d.ts +2 -2
- package/dist/cjs/components/ui/dialog.d.ts +1 -1
- package/dist/cjs/components/ui/dialog.js +11 -1
- package/dist/cjs/components/ui/portal-container.d.ts +7 -0
- package/dist/cjs/components/ui/portal-container.js +46 -0
- package/dist/cjs/file-preview/file-preview.js +1 -1
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +4 -0
- package/dist/cjs/meetings/meeting-scope.d.ts +2 -1
- package/dist/cjs/meetings/meeting-scope.js +1 -0
- package/dist/cjs/react-syntax-highlighter.d.js +1 -0
- package/dist/esm/chat/agent-thread.d.ts +24 -24
- package/dist/esm/chat/agent-thread.js +574 -838
- package/dist/esm/chat/chat-bot-view.d.ts +18 -6
- package/dist/esm/chat/chat-bot-view.js +29 -7
- package/dist/esm/chat/chat-feed-widget-view.d.ts +7 -0
- package/dist/esm/chat/chat-feed-widget-view.js +30 -0
- package/dist/esm/chat/chat-feed-widget.d.ts +16 -0
- package/dist/esm/chat/chat-feed-widget.js +31 -0
- package/dist/esm/chat/chat-hooks.d.ts +2 -2
- package/dist/esm/chat/chat-input.d.ts +3 -3
- package/dist/esm/chat/chat-input.js +9 -4
- package/dist/esm/chat/chat-scroller.d.ts +21 -0
- package/dist/esm/chat/chat-scroller.js +122 -0
- package/dist/esm/chat/chat-thread-timeline.d.ts +66 -0
- package/dist/esm/chat/chat-thread-timeline.js +605 -0
- package/dist/esm/chat/dataset-agent-thread.d.ts +16 -4
- package/dist/esm/chat/dataset-agent-thread.js +114 -307
- package/dist/esm/chat/multi-thread-view.d.ts +8 -3
- package/dist/esm/chat/multi-thread-view.js +10 -2
- package/dist/esm/chat/new-chat-thread.d.ts +8 -3
- package/dist/esm/chat/new-chat-thread.js +64 -25
- package/dist/esm/chat/thread-view.d.ts +18 -5
- package/dist/esm/chat/thread-view.js +67 -9
- package/dist/esm/chat/use-chat-virtualizer.d.ts +47 -0
- package/dist/esm/chat/use-chat-virtualizer.js +331 -0
- package/dist/esm/chat/virtualization.d.ts +44 -0
- package/dist/esm/chat/virtualization.js +123 -0
- package/dist/esm/components/ui/button.d.ts +2 -2
- package/dist/esm/components/ui/dialog.d.ts +1 -1
- package/dist/esm/components/ui/dialog.js +11 -1
- package/dist/esm/components/ui/portal-container.d.ts +7 -0
- package/dist/esm/components/ui/portal-container.js +16 -0
- package/dist/esm/file-preview/file-preview.js +1 -1
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/meetings/meeting-scope.d.ts +2 -1
- package/dist/esm/meetings/meeting-scope.js +1 -0
- package/dist/esm/react-syntax-highlighter.d.js +0 -0
- package/dist/index.css +1 -1
- package/package.json +6 -5
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
-
import {
|
|
3
|
+
import { AgentFileContent, MessagingChatClient } from "@meshagent/meshagent-agents";
|
|
4
|
+
import { resolveClientToolkitDescriptions } from "./chat-feed-widget.js";
|
|
4
5
|
import { defaultThreadDisplayNameFromPath } from "./conversation-descriptor.js";
|
|
5
6
|
import { ChatInput } from "./chat-input.js";
|
|
7
|
+
import { Button } from "../components/ui/button.js";
|
|
6
8
|
import { MeshagentFileUpload, fileToAsyncIterable } from "./file-attachment.js";
|
|
7
9
|
import { Toaster } from "../components/ui/sonner.js";
|
|
8
10
|
class NewThreadCancelledError extends Error {
|
|
@@ -78,7 +80,11 @@ function NewChatThread({
|
|
|
78
80
|
emptyStateTitle,
|
|
79
81
|
emptyStateDescription,
|
|
80
82
|
clientToolkits,
|
|
81
|
-
|
|
83
|
+
chatFeedWidgets,
|
|
84
|
+
toolChoice,
|
|
85
|
+
suggestions,
|
|
86
|
+
enableFileUpload = false,
|
|
87
|
+
onThreadStarted
|
|
82
88
|
}) {
|
|
83
89
|
const [internalThreadPath, setInternalThreadPath] = useState(null);
|
|
84
90
|
const [newThreadDraft, setNewThreadDraft] = useState("");
|
|
@@ -95,6 +101,10 @@ function NewChatThread({
|
|
|
95
101
|
[agentName, chatClient, room]
|
|
96
102
|
);
|
|
97
103
|
const [clientVersion, setClientVersion] = useState(0);
|
|
104
|
+
const resolvedClientToolkits = useMemo(
|
|
105
|
+
() => resolveClientToolkitDescriptions(clientToolkits, chatFeedWidgets),
|
|
106
|
+
[chatFeedWidgets, clientToolkits]
|
|
107
|
+
);
|
|
98
108
|
useEffect(() => {
|
|
99
109
|
return () => {
|
|
100
110
|
activeOperationRef.current += 1;
|
|
@@ -146,8 +156,8 @@ function NewChatThread({
|
|
|
146
156
|
setWaitingForAgent(false);
|
|
147
157
|
setNewThreadError(null);
|
|
148
158
|
}, []);
|
|
149
|
-
const handleCreateThread = useCallback(async () => {
|
|
150
|
-
const text = newThreadDraft.trim();
|
|
159
|
+
const handleCreateThread = useCallback(async (message) => {
|
|
160
|
+
const text = (message?.text ?? newThreadDraft).trim();
|
|
151
161
|
const hasDraft = text !== "" || newThreadAttachments.length > 0;
|
|
152
162
|
if (!hasDraft || creatingNewThread || waitingForAgent) {
|
|
153
163
|
return;
|
|
@@ -172,12 +182,13 @@ function NewChatThread({
|
|
|
172
182
|
}
|
|
173
183
|
const result = await activeChatClient.startThread({
|
|
174
184
|
message: text,
|
|
175
|
-
attachments: newThreadAttachments.map((attachment) => attachment.path),
|
|
185
|
+
attachments: newThreadAttachments.map((attachment) => new AgentFileContent({ url: attachment.path })),
|
|
176
186
|
senderName: getParticipantName(room.localParticipant) || void 0,
|
|
177
|
-
clientToolkits
|
|
178
|
-
toolChoice: toolChoice == null ? void 0 : new ToolChoice({ toolkitName: toolChoice.toolkitName, toolName: toolChoice.toolName })
|
|
187
|
+
clientToolkits: resolvedClientToolkits
|
|
179
188
|
});
|
|
180
189
|
ensureOperationActive(operationId, activeOperationRef);
|
|
190
|
+
await onThreadStarted?.(result.threadPath, result.session.messages);
|
|
191
|
+
ensureOperationActive(operationId, activeOperationRef);
|
|
181
192
|
const threadPath = result.threadPath;
|
|
182
193
|
const normalizedPath = normalizeThreadPath(threadPath);
|
|
183
194
|
const displayName = normalizedPath === null ? null : defaultThreadDisplayNameFromPath(normalizedPath);
|
|
@@ -207,10 +218,10 @@ function NewChatThread({
|
|
|
207
218
|
creatingNewThread,
|
|
208
219
|
newThreadAttachments,
|
|
209
220
|
newThreadDraft,
|
|
210
|
-
|
|
211
|
-
toolChoice,
|
|
221
|
+
resolvedClientToolkits,
|
|
212
222
|
onThreadPathChanged,
|
|
213
223
|
onThreadResolved,
|
|
224
|
+
onThreadStarted,
|
|
214
225
|
room,
|
|
215
226
|
waitingForAgent
|
|
216
227
|
]);
|
|
@@ -223,25 +234,53 @@ function NewChatThread({
|
|
|
223
234
|
return displayParticipantName(targetAgent ? getParticipantName(targetAgent) : null);
|
|
224
235
|
}, [activeChatClient, agentName, clientVersion, room]);
|
|
225
236
|
const pendingStatusText = waitingForAgent ? `Waiting for ${targetAgentLabel} to be ready.` : creatingNewThread ? `Starting a thread with ${targetAgentLabel}.` : null;
|
|
237
|
+
const visibleSuggestions = useMemo(
|
|
238
|
+
() => (suggestions ?? []).filter((suggestion) => suggestion.label.trim() !== ""),
|
|
239
|
+
[suggestions]
|
|
240
|
+
);
|
|
226
241
|
if (activePath !== null) {
|
|
227
242
|
return builder(activePath);
|
|
228
243
|
}
|
|
229
|
-
const composer = /* @__PURE__ */
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
244
|
+
const composer = /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
|
|
245
|
+
visibleSuggestions.length > 0 ? /* @__PURE__ */ jsx(
|
|
246
|
+
"ul",
|
|
247
|
+
{
|
|
248
|
+
"aria-label": "Starter suggestions",
|
|
249
|
+
className: "mx-auto flex flex-wrap w-full max-w-[912px] gap-2 overflow-x-auto px-4 pt-2 pb-1",
|
|
250
|
+
children: visibleSuggestions.map((suggestion, index) => {
|
|
251
|
+
const text = suggestion.prompt?.trim() || suggestion.label.trim();
|
|
252
|
+
return /* @__PURE__ */ jsx("li", { className: "shrink-0", children: /* @__PURE__ */ jsx(
|
|
253
|
+
Button,
|
|
254
|
+
{
|
|
255
|
+
type: "button",
|
|
256
|
+
variant: "outline",
|
|
257
|
+
className: "h-auto min-h-8 rounded-full px-3 py-1.5",
|
|
258
|
+
disabled: creatingNewThread || waitingForAgent,
|
|
259
|
+
onClick: () => void handleCreateThread({ text }),
|
|
260
|
+
children: suggestion.label
|
|
261
|
+
}
|
|
262
|
+
) }, index);
|
|
263
|
+
})
|
|
264
|
+
}
|
|
265
|
+
) : null,
|
|
266
|
+
/* @__PURE__ */ jsx(
|
|
267
|
+
ChatInput,
|
|
268
|
+
{
|
|
269
|
+
onSubmit: handleCreateThread,
|
|
270
|
+
attachments: newThreadAttachments,
|
|
271
|
+
onFilesSelected: selectNewThreadAttachments,
|
|
272
|
+
setAttachments: setNewThreadAttachments,
|
|
273
|
+
enableFileUpload,
|
|
274
|
+
value: newThreadDraft,
|
|
275
|
+
onValueChange: setNewThreadDraft,
|
|
276
|
+
clearOnSubmit: false,
|
|
277
|
+
showCancelButton: creatingNewThread || waitingForAgent,
|
|
278
|
+
onCancelRequest: cancelPendingNewThread,
|
|
279
|
+
disabled: creatingNewThread || waitingForAgent,
|
|
280
|
+
placeholder: agentName.trim() ? `Type a message or @${displayParticipantName(agentName)}` : "Type a message"
|
|
281
|
+
}
|
|
282
|
+
)
|
|
283
|
+
] });
|
|
245
284
|
return /* @__PURE__ */ jsxs("div", { className: "h-full flex flex-1 flex-col", children: [
|
|
246
285
|
centerComposer ? /* @__PURE__ */ jsx("div", { className: "flex min-h-0 flex-1 items-center justify-center px-4 py-6", children: /* @__PURE__ */ jsxs("div", { className: "w-full max-w-[912px] space-y-5", children: [
|
|
247
286
|
/* @__PURE__ */ jsxs("div", { className: "space-y-2 text-center", children: [
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { ReactElement } from "react";
|
|
2
2
|
import type { RoomClient } from "@meshagent/meshagent";
|
|
3
|
-
import type { BaseChatClient, ClientToolkitDescription } from "@meshagent/meshagent-agents";
|
|
4
|
-
import type { AgentToolChoice } from "./agent-thread";
|
|
5
|
-
import { ChatThreadDisplayMode } from "./conversation-descriptor";
|
|
6
|
-
import type { DatasetThreadRowsLoader } from "./dataset-agent-thread";
|
|
3
|
+
import type { AgentMessage, AgentMessageEvent, BaseChatClient, ClientToolkitDescription } from "@meshagent/meshagent-agents";
|
|
4
|
+
import type { AgentThreadSuggestion, AgentToolChoice } from "./agent-thread.js";
|
|
5
|
+
import { ChatThreadDisplayMode } from "./conversation-descriptor.js";
|
|
6
|
+
import type { DatasetThreadRowsLoader } from "./dataset-agent-thread.js";
|
|
7
|
+
import type { ChatFeedWidget } from "./chat-feed-widget.js";
|
|
7
8
|
export interface ThreadViewProps {
|
|
8
9
|
room: RoomClient;
|
|
9
10
|
chatClient?: BaseChatClient | null;
|
|
@@ -24,9 +25,21 @@ export interface ThreadViewProps {
|
|
|
24
25
|
onSelectedThreadResolved?: (path: string | null, displayName: string | null) => void;
|
|
25
26
|
newThreadResetVersion?: number;
|
|
26
27
|
clientToolkits?: ClientToolkitDescription[];
|
|
28
|
+
chatFeedWidgets?: ChatFeedWidget[];
|
|
27
29
|
toolChoice?: AgentToolChoice;
|
|
28
30
|
collapseMessages?: boolean;
|
|
31
|
+
suggestions?: readonly AgentThreadSuggestion[];
|
|
32
|
+
enableFileUpload?: boolean;
|
|
29
33
|
threadSource?: "session" | "dataset";
|
|
30
34
|
rowsLoader?: DatasetThreadRowsLoader;
|
|
35
|
+
persistedEvents?: readonly AgentMessageEvent[];
|
|
36
|
+
loadThread?: boolean;
|
|
37
|
+
injectPersistedEvents?: boolean;
|
|
38
|
+
composerDisabled?: boolean;
|
|
39
|
+
closeThreadOnUnmount?: boolean;
|
|
40
|
+
onPersistedEventsInjectionStarted?: () => void;
|
|
41
|
+
onPersistedEventsInjected?: (error: unknown | null) => void;
|
|
42
|
+
onEventsChanged?: (threadId: string, events: readonly AgentMessageEvent[], latestMessage: AgentMessage | null) => void;
|
|
43
|
+
onThreadStarted?: (threadId: string, events: readonly AgentMessageEvent[]) => Promise<void> | void;
|
|
31
44
|
}
|
|
32
|
-
export declare function ThreadView({ room, chatClient, path, documentPath, agentName, threadDisplayMode, threadDir, toolkit, tool, centerComposer, emptyStateTitle, emptyStateDescription, startNewThreadTitle, startNewThreadDescription, selectedThreadPath, onSelectedThreadPathChanged, onSelectedThreadResolved, newThreadResetVersion, clientToolkits, toolChoice, collapseMessages, threadSource, rowsLoader, }: ThreadViewProps): ReactElement;
|
|
45
|
+
export declare function ThreadView({ room, chatClient, path, documentPath, agentName, threadDisplayMode, threadDir, toolkit, tool, centerComposer, emptyStateTitle, emptyStateDescription, startNewThreadTitle, startNewThreadDescription, selectedThreadPath, onSelectedThreadPathChanged, onSelectedThreadResolved, newThreadResetVersion, clientToolkits, chatFeedWidgets, toolChoice, collapseMessages, suggestions, enableFileUpload, threadSource, rowsLoader, persistedEvents, loadThread, injectPersistedEvents, composerDisabled, closeThreadOnUnmount, onPersistedEventsInjectionStarted, onPersistedEventsInjected, onEventsChanged, onThreadStarted, }: ThreadViewProps): ReactElement;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { AlertTriangle } from "lucide-react";
|
|
3
|
-
import { AgentThread } from "./agent-thread";
|
|
4
|
-
import { ChatThreadDisplayMode, chatDocumentPath } from "./conversation-descriptor";
|
|
5
|
-
import { DatasetAgentThread } from "./dataset-agent-thread";
|
|
6
|
-
import { MultiThreadView } from "./multi-thread-view";
|
|
3
|
+
import { AgentThread } from "./agent-thread.js";
|
|
4
|
+
import { ChatThreadDisplayMode, chatDocumentPath } from "./conversation-descriptor.js";
|
|
5
|
+
import { DatasetAgentThread } from "./dataset-agent-thread.js";
|
|
6
|
+
import { MultiThreadView } from "./multi-thread-view.js";
|
|
7
7
|
function normalizePath(path) {
|
|
8
8
|
const normalized = path?.trim();
|
|
9
9
|
return normalized ? normalized : null;
|
|
@@ -37,10 +37,22 @@ function ThreadView({
|
|
|
37
37
|
onSelectedThreadResolved,
|
|
38
38
|
newThreadResetVersion = 0,
|
|
39
39
|
clientToolkits,
|
|
40
|
+
chatFeedWidgets,
|
|
40
41
|
toolChoice,
|
|
41
42
|
collapseMessages = true,
|
|
43
|
+
suggestions,
|
|
44
|
+
enableFileUpload = false,
|
|
42
45
|
threadSource = "session",
|
|
43
|
-
rowsLoader
|
|
46
|
+
rowsLoader,
|
|
47
|
+
persistedEvents,
|
|
48
|
+
loadThread = true,
|
|
49
|
+
injectPersistedEvents = false,
|
|
50
|
+
composerDisabled = false,
|
|
51
|
+
closeThreadOnUnmount = false,
|
|
52
|
+
onPersistedEventsInjectionStarted,
|
|
53
|
+
onPersistedEventsInjected,
|
|
54
|
+
onEventsChanged,
|
|
55
|
+
onThreadStarted
|
|
44
56
|
}) {
|
|
45
57
|
const resolvedDocumentPath = normalizePath(documentPath ?? path);
|
|
46
58
|
const resolvedSingleThreadPath = resolvedDocumentPath ?? chatDocumentPath(agentName, { threadDir });
|
|
@@ -59,8 +71,18 @@ function ThreadView({
|
|
|
59
71
|
emptyStateTitle,
|
|
60
72
|
emptyStateDescription,
|
|
61
73
|
clientToolkits,
|
|
74
|
+
chatFeedWidgets,
|
|
62
75
|
toolChoice,
|
|
63
|
-
collapseMessages
|
|
76
|
+
collapseMessages,
|
|
77
|
+
suggestions,
|
|
78
|
+
enableFileUpload,
|
|
79
|
+
loadThread,
|
|
80
|
+
injectPersistedEvents,
|
|
81
|
+
composerDisabled,
|
|
82
|
+
closeThreadOnUnmount,
|
|
83
|
+
onPersistedEventsInjectionStarted,
|
|
84
|
+
onPersistedEventsInjected,
|
|
85
|
+
onEventsChanged
|
|
64
86
|
}
|
|
65
87
|
);
|
|
66
88
|
}
|
|
@@ -75,8 +97,19 @@ function ThreadView({
|
|
|
75
97
|
emptyStateTitle,
|
|
76
98
|
emptyStateDescription,
|
|
77
99
|
clientToolkits,
|
|
100
|
+
chatFeedWidgets,
|
|
78
101
|
toolChoice,
|
|
79
|
-
collapseMessages
|
|
102
|
+
collapseMessages,
|
|
103
|
+
suggestions,
|
|
104
|
+
enableFileUpload,
|
|
105
|
+
persistedEvents,
|
|
106
|
+
loadThread,
|
|
107
|
+
injectPersistedEvents,
|
|
108
|
+
composerDisabled,
|
|
109
|
+
closeThreadOnUnmount,
|
|
110
|
+
onPersistedEventsInjectionStarted,
|
|
111
|
+
onPersistedEventsInjected,
|
|
112
|
+
onEventsChanged
|
|
80
113
|
}
|
|
81
114
|
);
|
|
82
115
|
}
|
|
@@ -98,7 +131,11 @@ function ThreadView({
|
|
|
98
131
|
newThreadResetVersion,
|
|
99
132
|
centerComposer,
|
|
100
133
|
clientToolkits,
|
|
134
|
+
chatFeedWidgets,
|
|
101
135
|
toolChoice,
|
|
136
|
+
suggestions,
|
|
137
|
+
enableFileUpload,
|
|
138
|
+
onThreadStarted,
|
|
102
139
|
builder: (threadPath) => threadSource === "dataset" ? /* @__PURE__ */ jsx(
|
|
103
140
|
DatasetAgentThread,
|
|
104
141
|
{
|
|
@@ -111,8 +148,18 @@ function ThreadView({
|
|
|
111
148
|
emptyStateTitle: startNewThreadTitle,
|
|
112
149
|
emptyStateDescription: startNewThreadDescription,
|
|
113
150
|
clientToolkits,
|
|
151
|
+
chatFeedWidgets,
|
|
114
152
|
toolChoice,
|
|
115
|
-
collapseMessages
|
|
153
|
+
collapseMessages,
|
|
154
|
+
suggestions,
|
|
155
|
+
enableFileUpload,
|
|
156
|
+
loadThread,
|
|
157
|
+
injectPersistedEvents,
|
|
158
|
+
composerDisabled,
|
|
159
|
+
closeThreadOnUnmount,
|
|
160
|
+
onPersistedEventsInjectionStarted,
|
|
161
|
+
onPersistedEventsInjected,
|
|
162
|
+
onEventsChanged
|
|
116
163
|
}
|
|
117
164
|
) : /* @__PURE__ */ jsx(
|
|
118
165
|
AgentThread,
|
|
@@ -125,8 +172,19 @@ function ThreadView({
|
|
|
125
172
|
emptyStateTitle: startNewThreadTitle,
|
|
126
173
|
emptyStateDescription: startNewThreadDescription,
|
|
127
174
|
clientToolkits,
|
|
175
|
+
chatFeedWidgets,
|
|
128
176
|
toolChoice,
|
|
129
|
-
collapseMessages
|
|
177
|
+
collapseMessages,
|
|
178
|
+
suggestions,
|
|
179
|
+
enableFileUpload,
|
|
180
|
+
persistedEvents,
|
|
181
|
+
loadThread,
|
|
182
|
+
injectPersistedEvents,
|
|
183
|
+
composerDisabled,
|
|
184
|
+
closeThreadOnUnmount,
|
|
185
|
+
onPersistedEventsInjectionStarted,
|
|
186
|
+
onPersistedEventsInjected,
|
|
187
|
+
onEventsChanged
|
|
130
188
|
}
|
|
131
189
|
)
|
|
132
190
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type RefObject } from "react";
|
|
2
|
+
import { type Page, type VisiblePageRange } from "./virtualization.js";
|
|
3
|
+
export type ChatVirtualizerState = {
|
|
4
|
+
visibleRange: VisiblePageRange;
|
|
5
|
+
measuredHeights: ReadonlyMap<number, number>;
|
|
6
|
+
bottomAdjustment: number;
|
|
7
|
+
};
|
|
8
|
+
export type ChatVirtualizerAction = {
|
|
9
|
+
type: "setVisibleRange";
|
|
10
|
+
range: VisiblePageRange;
|
|
11
|
+
} | {
|
|
12
|
+
type: "syncPageWindow";
|
|
13
|
+
pageCount: number;
|
|
14
|
+
overscanPages: number;
|
|
15
|
+
} | {
|
|
16
|
+
type: "measurePage";
|
|
17
|
+
pageIndex: number;
|
|
18
|
+
height: number;
|
|
19
|
+
shouldAdjustBottom: boolean;
|
|
20
|
+
} | {
|
|
21
|
+
type: "clearBottomAdjustment";
|
|
22
|
+
};
|
|
23
|
+
export type CreateChatVirtualizerStateArgs = {
|
|
24
|
+
pageCount: number;
|
|
25
|
+
overscanPages: number;
|
|
26
|
+
};
|
|
27
|
+
type UseChatVirtualizerOptions<TMessage> = {
|
|
28
|
+
messages: readonly TMessage[];
|
|
29
|
+
pageSize: number;
|
|
30
|
+
overscanPages: number;
|
|
31
|
+
bottomThresholdPx: number;
|
|
32
|
+
getMessageId: (message: TMessage) => string;
|
|
33
|
+
};
|
|
34
|
+
type UseChatVirtualizerResult<TMessage> = {
|
|
35
|
+
scrollRef: RefObject<HTMLDivElement | null>;
|
|
36
|
+
visiblePages: Page<TMessage>[];
|
|
37
|
+
spacerHeight: number;
|
|
38
|
+
windowBottom: number;
|
|
39
|
+
handleScroll: () => void;
|
|
40
|
+
handlePageHeightChange: (pageIndex: number, height: number) => void;
|
|
41
|
+
};
|
|
42
|
+
export declare function getInitialVisibleRange(pageCount: number, overscanPages: number): VisiblePageRange;
|
|
43
|
+
export declare function clampVisibleRange(range: VisiblePageRange, pageCount: number): VisiblePageRange;
|
|
44
|
+
export declare function createChatVirtualizerState({ pageCount, overscanPages, }: CreateChatVirtualizerStateArgs): ChatVirtualizerState;
|
|
45
|
+
export declare function chatVirtualizerReducer(state: ChatVirtualizerState, action: ChatVirtualizerAction): ChatVirtualizerState;
|
|
46
|
+
export declare function useChatVirtualizer<TMessage>({ messages, pageSize, overscanPages, bottomThresholdPx, getMessageId, }: UseChatVirtualizerOptions<TMessage>): UseChatVirtualizerResult<TMessage>;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useReducer,
|
|
7
|
+
useRef
|
|
8
|
+
} from "react";
|
|
9
|
+
import {
|
|
10
|
+
buildPageHeights,
|
|
11
|
+
buildPrefixHeights,
|
|
12
|
+
createPages,
|
|
13
|
+
getVirtualWindowMetrics,
|
|
14
|
+
getVisiblePageRange,
|
|
15
|
+
isNearBottom,
|
|
16
|
+
shouldAdjustScrollForHeightChange
|
|
17
|
+
} from "./virtualization.js";
|
|
18
|
+
function getInitialVisibleRange(pageCount, overscanPages) {
|
|
19
|
+
if (pageCount === 0) {
|
|
20
|
+
return { first: 0, last: -1 };
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
first: Math.max(0, pageCount - 1 - Math.max(0, overscanPages)),
|
|
24
|
+
last: pageCount - 1
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function clampVisibleRange(range, pageCount) {
|
|
28
|
+
if (pageCount === 0) {
|
|
29
|
+
return { first: 0, last: -1 };
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
first: Math.min(pageCount - 1, Math.max(0, range.first)),
|
|
33
|
+
last: Math.min(pageCount - 1, Math.max(0, range.last))
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function createChatVirtualizerState({
|
|
37
|
+
pageCount,
|
|
38
|
+
overscanPages
|
|
39
|
+
}) {
|
|
40
|
+
return {
|
|
41
|
+
visibleRange: getInitialVisibleRange(pageCount, overscanPages),
|
|
42
|
+
measuredHeights: /* @__PURE__ */ new Map(),
|
|
43
|
+
bottomAdjustment: 0
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function areRangesEqual(firstRange, secondRange) {
|
|
47
|
+
return firstRange.first === secondRange.first && firstRange.last === secondRange.last;
|
|
48
|
+
}
|
|
49
|
+
function pruneMeasuredHeights(measuredHeights, pageCount) {
|
|
50
|
+
let didPrune = false;
|
|
51
|
+
const nextMeasuredHeights = /* @__PURE__ */ new Map();
|
|
52
|
+
for (const [pageIndex, height] of measuredHeights) {
|
|
53
|
+
if (pageIndex >= pageCount) {
|
|
54
|
+
didPrune = true;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
nextMeasuredHeights.set(pageIndex, height);
|
|
58
|
+
}
|
|
59
|
+
return didPrune ? nextMeasuredHeights : measuredHeights;
|
|
60
|
+
}
|
|
61
|
+
function chatVirtualizerReducer(state, action) {
|
|
62
|
+
switch (action.type) {
|
|
63
|
+
case "setVisibleRange": {
|
|
64
|
+
if (areRangesEqual(state.visibleRange, action.range)) {
|
|
65
|
+
return state;
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
...state,
|
|
69
|
+
visibleRange: action.range
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
case "syncPageWindow": {
|
|
73
|
+
const visibleRange = state.visibleRange.last === -1 ? getInitialVisibleRange(action.pageCount, action.overscanPages) : clampVisibleRange(state.visibleRange, action.pageCount);
|
|
74
|
+
const measuredHeights = pruneMeasuredHeights(state.measuredHeights, action.pageCount);
|
|
75
|
+
if (areRangesEqual(state.visibleRange, visibleRange) && measuredHeights === state.measuredHeights) {
|
|
76
|
+
return state;
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
...state,
|
|
80
|
+
visibleRange,
|
|
81
|
+
measuredHeights
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
case "measurePage": {
|
|
85
|
+
const roundedHeight = Math.ceil(action.height);
|
|
86
|
+
const previousHeight = state.measuredHeights.get(action.pageIndex) ?? 0;
|
|
87
|
+
if (previousHeight === roundedHeight) {
|
|
88
|
+
return state;
|
|
89
|
+
}
|
|
90
|
+
const heightDelta = roundedHeight - previousHeight;
|
|
91
|
+
const measuredHeights = new Map(state.measuredHeights);
|
|
92
|
+
if (roundedHeight > 0) {
|
|
93
|
+
measuredHeights.set(action.pageIndex, roundedHeight);
|
|
94
|
+
} else {
|
|
95
|
+
measuredHeights.delete(action.pageIndex);
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
...state,
|
|
99
|
+
measuredHeights,
|
|
100
|
+
bottomAdjustment: state.bottomAdjustment + (action.shouldAdjustBottom ? heightDelta : 0)
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
case "clearBottomAdjustment": {
|
|
104
|
+
if (state.bottomAdjustment === 0) {
|
|
105
|
+
return state;
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
...state,
|
|
109
|
+
bottomAdjustment: 0
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function getMessageSelector(messageId) {
|
|
115
|
+
const escapedMessageId = typeof CSS !== "undefined" && typeof CSS.escape === "function" ? CSS.escape(messageId) : messageId.replace(/"/gu, '\\"');
|
|
116
|
+
return '.chat-scroll__message[data-message-id="' + escapedMessageId + '"]';
|
|
117
|
+
}
|
|
118
|
+
function captureScrollAnchor(scrollElement, runtime) {
|
|
119
|
+
if (!scrollElement || runtime.isAtBottom) {
|
|
120
|
+
runtime.anchor = void 0;
|
|
121
|
+
return void 0;
|
|
122
|
+
}
|
|
123
|
+
const scrollRect = scrollElement.getBoundingClientRect();
|
|
124
|
+
const messageElements = scrollElement.querySelectorAll(
|
|
125
|
+
".chat-scroll__message[data-message-id]"
|
|
126
|
+
);
|
|
127
|
+
for (let index = 0; index < messageElements.length; index += 1) {
|
|
128
|
+
const messageElement = messageElements.item(index);
|
|
129
|
+
const messageRect = messageElement.getBoundingClientRect();
|
|
130
|
+
if (messageRect.bottom > scrollRect.top) {
|
|
131
|
+
const messageId = messageElement.dataset.messageId;
|
|
132
|
+
const closestPageElement = messageElement.closest(
|
|
133
|
+
".chat-scroll__page[data-page-index]"
|
|
134
|
+
);
|
|
135
|
+
const pageIndex = closestPageElement instanceof HTMLElement ? Number(closestPageElement.dataset.pageIndex) : Number.NaN;
|
|
136
|
+
if (messageId && Number.isInteger(pageIndex)) {
|
|
137
|
+
const anchor = {
|
|
138
|
+
messageId,
|
|
139
|
+
offsetTop: messageRect.top - scrollRect.top,
|
|
140
|
+
pageIndex
|
|
141
|
+
};
|
|
142
|
+
runtime.anchor = anchor;
|
|
143
|
+
return anchor;
|
|
144
|
+
}
|
|
145
|
+
runtime.anchor = void 0;
|
|
146
|
+
return void 0;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
runtime.anchor = void 0;
|
|
150
|
+
return void 0;
|
|
151
|
+
}
|
|
152
|
+
function restoreScrollAnchor(scrollElement, runtime, bottomAdjustment) {
|
|
153
|
+
const anchor = runtime.anchor;
|
|
154
|
+
if (!anchor || !scrollElement || runtime.isAtBottom) {
|
|
155
|
+
runtime.anchor = void 0;
|
|
156
|
+
return bottomAdjustment !== 0;
|
|
157
|
+
}
|
|
158
|
+
const messageElement = scrollElement.querySelector(
|
|
159
|
+
getMessageSelector(anchor.messageId)
|
|
160
|
+
);
|
|
161
|
+
if (!messageElement) {
|
|
162
|
+
runtime.anchor = void 0;
|
|
163
|
+
return bottomAdjustment !== 0;
|
|
164
|
+
}
|
|
165
|
+
const scrollRect = scrollElement.getBoundingClientRect();
|
|
166
|
+
const messageRect = messageElement.getBoundingClientRect();
|
|
167
|
+
const nextOffsetTop = messageRect.top - scrollRect.top;
|
|
168
|
+
const offsetDelta = nextOffsetTop - anchor.offsetTop;
|
|
169
|
+
if (offsetDelta !== 0) {
|
|
170
|
+
scrollElement.scrollTop += offsetDelta;
|
|
171
|
+
}
|
|
172
|
+
if (bottomAdjustment !== 0) {
|
|
173
|
+
scrollElement.scrollTop += bottomAdjustment;
|
|
174
|
+
}
|
|
175
|
+
runtime.anchor = void 0;
|
|
176
|
+
return bottomAdjustment !== 0;
|
|
177
|
+
}
|
|
178
|
+
function updateVisibleRange(scrollElement, runtime, dispatch) {
|
|
179
|
+
if (!scrollElement) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const nextRange = getVisiblePageRange({
|
|
183
|
+
prefixHeights: runtime.prefixHeights,
|
|
184
|
+
scrollTop: scrollElement.scrollTop,
|
|
185
|
+
viewportHeight: scrollElement.clientHeight,
|
|
186
|
+
overscanPages: runtime.overscanPages
|
|
187
|
+
});
|
|
188
|
+
const clampedNextRange = clampVisibleRange(nextRange, runtime.pageCount);
|
|
189
|
+
if (!areRangesEqual(runtime.visibleRange, clampedNextRange)) {
|
|
190
|
+
captureScrollAnchor(scrollElement, runtime);
|
|
191
|
+
}
|
|
192
|
+
dispatch({ type: "setVisibleRange", range: clampedNextRange });
|
|
193
|
+
}
|
|
194
|
+
function useChatVirtualizer({
|
|
195
|
+
messages,
|
|
196
|
+
pageSize,
|
|
197
|
+
overscanPages,
|
|
198
|
+
bottomThresholdPx,
|
|
199
|
+
getMessageId
|
|
200
|
+
}) {
|
|
201
|
+
const scrollRef = useRef(null);
|
|
202
|
+
const runtimeRef = useRef({
|
|
203
|
+
isAtBottom: true,
|
|
204
|
+
hasMounted: false,
|
|
205
|
+
lastMessageId: void 0,
|
|
206
|
+
anchor: void 0,
|
|
207
|
+
frameId: void 0,
|
|
208
|
+
prefixHeights: [0],
|
|
209
|
+
visibleRange: { first: 0, last: -1 },
|
|
210
|
+
pageCount: 0,
|
|
211
|
+
overscanPages,
|
|
212
|
+
bottomThresholdPx
|
|
213
|
+
});
|
|
214
|
+
const pages = useMemo(
|
|
215
|
+
() => createPages([...messages].reverse(), pageSize),
|
|
216
|
+
[messages, pageSize]
|
|
217
|
+
);
|
|
218
|
+
const pageCount = pages.length;
|
|
219
|
+
const [state, dispatch] = useReducer(
|
|
220
|
+
chatVirtualizerReducer,
|
|
221
|
+
{ pageCount, overscanPages },
|
|
222
|
+
createChatVirtualizerState
|
|
223
|
+
);
|
|
224
|
+
const clampedVisibleRange = clampVisibleRange(state.visibleRange, pageCount);
|
|
225
|
+
const pageHeights = useMemo(
|
|
226
|
+
() => buildPageHeights(pageCount, state.measuredHeights),
|
|
227
|
+
[pageCount, state.measuredHeights]
|
|
228
|
+
);
|
|
229
|
+
const prefixHeights = useMemo(() => buildPrefixHeights(pageHeights), [pageHeights]);
|
|
230
|
+
const virtualWindowMetrics = getVirtualWindowMetrics(prefixHeights, clampedVisibleRange);
|
|
231
|
+
const visiblePages = clampedVisibleRange.last >= clampedVisibleRange.first ? pages.slice(clampedVisibleRange.first, clampedVisibleRange.last + 1) : [];
|
|
232
|
+
const runtime = runtimeRef.current;
|
|
233
|
+
runtime.prefixHeights = prefixHeights;
|
|
234
|
+
runtime.visibleRange = clampedVisibleRange;
|
|
235
|
+
runtime.pageCount = pageCount;
|
|
236
|
+
runtime.overscanPages = overscanPages;
|
|
237
|
+
runtime.bottomThresholdPx = bottomThresholdPx;
|
|
238
|
+
const handleScroll = useCallback(() => {
|
|
239
|
+
const scrollElement = scrollRef.current;
|
|
240
|
+
const currentRuntime = runtimeRef.current;
|
|
241
|
+
if (!scrollElement) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
currentRuntime.isAtBottom = isNearBottom({
|
|
245
|
+
scrollTop: scrollElement.scrollTop,
|
|
246
|
+
scrollHeight: scrollElement.scrollHeight,
|
|
247
|
+
clientHeight: scrollElement.clientHeight,
|
|
248
|
+
thresholdPx: currentRuntime.bottomThresholdPx
|
|
249
|
+
});
|
|
250
|
+
if (currentRuntime.frameId !== void 0) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
currentRuntime.frameId = window.requestAnimationFrame(() => {
|
|
254
|
+
currentRuntime.frameId = void 0;
|
|
255
|
+
updateVisibleRange(scrollRef.current, currentRuntime, dispatch);
|
|
256
|
+
});
|
|
257
|
+
}, []);
|
|
258
|
+
const handlePageHeightChange = useCallback((pageIndex, height) => {
|
|
259
|
+
const currentRuntime = runtimeRef.current;
|
|
260
|
+
const anchor = captureScrollAnchor(scrollRef.current, currentRuntime);
|
|
261
|
+
const shouldAdjustBottom = anchor ? shouldAdjustScrollForHeightChange({
|
|
262
|
+
changedPageIndex: pageIndex,
|
|
263
|
+
firstVisiblePageIndex: anchor.pageIndex,
|
|
264
|
+
isAtBottom: currentRuntime.isAtBottom
|
|
265
|
+
}) : false;
|
|
266
|
+
dispatch({ type: "measurePage", pageIndex, height, shouldAdjustBottom });
|
|
267
|
+
}, []);
|
|
268
|
+
useLayoutEffect(() => {
|
|
269
|
+
const currentRuntime = runtimeRef.current;
|
|
270
|
+
const scrollElement = scrollRef.current;
|
|
271
|
+
const shouldClearBottomAdjustment = restoreScrollAnchor(
|
|
272
|
+
scrollElement,
|
|
273
|
+
currentRuntime,
|
|
274
|
+
state.bottomAdjustment
|
|
275
|
+
);
|
|
276
|
+
if (shouldClearBottomAdjustment) {
|
|
277
|
+
dispatch({ type: "clearBottomAdjustment" });
|
|
278
|
+
}
|
|
279
|
+
if (!scrollElement || !currentRuntime.isAtBottom) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
scrollElement.scrollTop = scrollElement.scrollHeight;
|
|
283
|
+
updateVisibleRange(scrollElement, currentRuntime, dispatch);
|
|
284
|
+
});
|
|
285
|
+
useLayoutEffect(() => {
|
|
286
|
+
const currentRuntime = runtimeRef.current;
|
|
287
|
+
const scrollElement = scrollRef.current;
|
|
288
|
+
const bottomMessage = messages[0];
|
|
289
|
+
const bottomMessageId = bottomMessage ? getMessageId(bottomMessage) : void 0;
|
|
290
|
+
const previousLastMessageId = currentRuntime.lastMessageId;
|
|
291
|
+
dispatch({ type: "syncPageWindow", pageCount, overscanPages });
|
|
292
|
+
currentRuntime.lastMessageId = bottomMessageId;
|
|
293
|
+
if (!scrollElement) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (!currentRuntime.hasMounted) {
|
|
297
|
+
scrollElement.scrollTop = scrollElement.scrollHeight;
|
|
298
|
+
currentRuntime.isAtBottom = true;
|
|
299
|
+
currentRuntime.hasMounted = true;
|
|
300
|
+
updateVisibleRange(scrollElement, currentRuntime, dispatch);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (previousLastMessageId !== void 0 && previousLastMessageId !== bottomMessageId && currentRuntime.isAtBottom) {
|
|
304
|
+
scrollElement.scrollTop = scrollElement.scrollHeight;
|
|
305
|
+
updateVisibleRange(scrollElement, currentRuntime, dispatch);
|
|
306
|
+
}
|
|
307
|
+
}, [getMessageId, messages, overscanPages, pageCount]);
|
|
308
|
+
useEffect(() => {
|
|
309
|
+
return () => {
|
|
310
|
+
const frameId = runtimeRef.current.frameId;
|
|
311
|
+
if (frameId !== void 0) {
|
|
312
|
+
window.cancelAnimationFrame(frameId);
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
}, []);
|
|
316
|
+
return {
|
|
317
|
+
scrollRef,
|
|
318
|
+
visiblePages,
|
|
319
|
+
spacerHeight: virtualWindowMetrics.total,
|
|
320
|
+
windowBottom: virtualWindowMetrics.bottom + state.bottomAdjustment,
|
|
321
|
+
handleScroll,
|
|
322
|
+
handlePageHeightChange
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
export {
|
|
326
|
+
chatVirtualizerReducer,
|
|
327
|
+
clampVisibleRange,
|
|
328
|
+
createChatVirtualizerState,
|
|
329
|
+
getInitialVisibleRange,
|
|
330
|
+
useChatVirtualizer
|
|
331
|
+
};
|