@meshagent/meshagent-tailwind 0.43.1 → 0.43.3
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 +8 -0
- package/dist/cjs/chat/agent-thread.d.ts +15 -0
- package/dist/cjs/chat/agent-thread.js +613 -0
- package/dist/cjs/chat/chat-bot-view.d.ts +3 -2
- package/dist/cjs/chat/chat-bot-view.js +34 -34
- package/dist/cjs/chat/chat-bot-view.test.js +325 -0
- package/dist/cjs/chat/live-chat-widgets.test.js +256 -0
- package/dist/cjs/chat/multi-thread-view.d.ts +3 -2
- package/dist/cjs/chat/multi-thread-view.js +4 -2
- package/dist/cjs/chat/new-chat-thread.d.ts +3 -2
- package/dist/cjs/chat/new-chat-thread.js +5 -2
- package/dist/cjs/file-preview/file-preview.js +2 -2
- package/dist/cjs/index.d.ts +1 -2
- package/dist/cjs/index.js +1 -2
- package/dist/esm/chat/agent-thread.d.ts +15 -0
- package/dist/esm/chat/agent-thread.js +606 -0
- package/dist/esm/chat/chat-bot-view.d.ts +3 -2
- package/dist/esm/chat/chat-bot-view.js +34 -34
- package/dist/esm/chat/chat-bot-view.test.js +336 -0
- package/dist/esm/chat/live-chat-widgets.test.js +262 -0
- package/dist/esm/chat/multi-thread-view.d.ts +3 -2
- package/dist/esm/chat/multi-thread-view.js +4 -2
- package/dist/esm/chat/new-chat-thread.d.ts +3 -2
- package/dist/esm/chat/new-chat-thread.js +5 -2
- package/dist/esm/file-preview/file-preview.js +2 -2
- package/dist/esm/index.d.ts +1 -2
- package/dist/esm/index.js +1 -2
- package/dist/index.css +1 -1
- package/package.json +10 -5
- package/dist/cjs/chat/chat-thread.d.ts +0 -12
- package/dist/cjs/chat/chat-thread.js +0 -815
- package/dist/cjs/chat/dataset-chat-thread.d.ts +0 -16
- package/dist/cjs/chat/dataset-chat-thread.js +0 -1813
- package/dist/esm/chat/chat-thread.d.ts +0 -12
- package/dist/esm/chat/chat-thread.js +0 -785
- package/dist/esm/chat/dataset-chat-thread.d.ts +0 -16
- package/dist/esm/chat/dataset-chat-thread.js +0 -1811
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import {
|
|
4
|
+
AgentFileContentDelta,
|
|
5
|
+
AgentImageGenerationCompleted,
|
|
6
|
+
AgentImageGenerationFailed,
|
|
7
|
+
AgentImageGenerationPartial,
|
|
8
|
+
AgentImageGenerationStarted,
|
|
9
|
+
AgentReasoningContentDelta,
|
|
10
|
+
AgentReasoningContentEnded,
|
|
11
|
+
AgentReasoningContentStarted,
|
|
12
|
+
AgentTextContentDelta,
|
|
13
|
+
AgentTextContentEnded,
|
|
14
|
+
AgentTextContentStarted,
|
|
15
|
+
AgentThreadStatus,
|
|
16
|
+
AgentToolCallEnded,
|
|
17
|
+
AgentToolCallInProgress,
|
|
18
|
+
AgentToolCallPending,
|
|
19
|
+
AgentToolCallStarted,
|
|
20
|
+
MessagingChatClient,
|
|
21
|
+
StartThread,
|
|
22
|
+
TurnStart,
|
|
23
|
+
TurnStartAccepted,
|
|
24
|
+
TurnSteer,
|
|
25
|
+
TurnSteerAccepted
|
|
26
|
+
} from "@meshagent/meshagent-agents";
|
|
27
|
+
import { Download, FileText, ImageOff } from "lucide-react";
|
|
28
|
+
import ReactMarkdown from "react-markdown";
|
|
29
|
+
import rehypeHighlight from "rehype-highlight";
|
|
30
|
+
import rehypeSanitize from "rehype-sanitize";
|
|
31
|
+
import remarkGfm from "remark-gfm";
|
|
32
|
+
import { Spinner } from "../components/ui/spinner.js";
|
|
33
|
+
import { cn } from "../lib/utils.js";
|
|
34
|
+
import { ChatInput } from "./chat-input.js";
|
|
35
|
+
import { ChatTypingIndicator } from "./chat-typing-indicator.js";
|
|
36
|
+
import { MeshagentFileUpload, fileToAsyncIterable } from "./file-attachment.js";
|
|
37
|
+
const stickyBottomThresholdPx = 24;
|
|
38
|
+
function isTypedMessage(message, ctor) {
|
|
39
|
+
return message instanceof ctor;
|
|
40
|
+
}
|
|
41
|
+
function stringValue(value) {
|
|
42
|
+
return typeof value === "string" && value.trim() !== "" ? value.trim() : void 0;
|
|
43
|
+
}
|
|
44
|
+
function displayParticipantName(name) {
|
|
45
|
+
const normalized = name?.trim();
|
|
46
|
+
if (!normalized) {
|
|
47
|
+
return "agent";
|
|
48
|
+
}
|
|
49
|
+
return normalized.split("@")[0]?.trim() || normalized;
|
|
50
|
+
}
|
|
51
|
+
function getParticipantName(participant) {
|
|
52
|
+
const name = participant?.getAttribute("name");
|
|
53
|
+
return typeof name === "string" ? name.trim() : "";
|
|
54
|
+
}
|
|
55
|
+
function findAgentParticipant(room, agentName) {
|
|
56
|
+
const normalizedAgentName = agentName?.trim();
|
|
57
|
+
for (const participant of room.messaging.remoteParticipants) {
|
|
58
|
+
if (normalizedAgentName && getParticipantName(participant) !== normalizedAgentName) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (participant.getAttribute("supports_agent_messages") === true) {
|
|
62
|
+
return participant;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
function timeAgo(date) {
|
|
68
|
+
const seconds = Math.max(0, Math.floor((Date.now() - date.getTime()) / 1e3));
|
|
69
|
+
if (seconds < 60) {
|
|
70
|
+
return "now";
|
|
71
|
+
}
|
|
72
|
+
const minutes = Math.floor(seconds / 60);
|
|
73
|
+
if (minutes < 60) {
|
|
74
|
+
return `${minutes}m`;
|
|
75
|
+
}
|
|
76
|
+
const hours = Math.floor(minutes / 60);
|
|
77
|
+
if (hours < 24) {
|
|
78
|
+
return `${hours}h`;
|
|
79
|
+
}
|
|
80
|
+
return `${Math.floor(hours / 24)}d`;
|
|
81
|
+
}
|
|
82
|
+
function distanceFromBottom(element) {
|
|
83
|
+
return Math.max(element.scrollHeight - element.clientHeight - element.scrollTop, 0);
|
|
84
|
+
}
|
|
85
|
+
function isNearBottom(element) {
|
|
86
|
+
return distanceFromBottom(element) <= stickyBottomThresholdPx;
|
|
87
|
+
}
|
|
88
|
+
function inputContent(message) {
|
|
89
|
+
const textParts = [];
|
|
90
|
+
const attachments = [];
|
|
91
|
+
for (const item of message.content ?? []) {
|
|
92
|
+
if (item.type === "text" && item.text.trim() !== "") {
|
|
93
|
+
textParts.push(item.text);
|
|
94
|
+
} else if (item.type === "file" && item.url.trim() !== "") {
|
|
95
|
+
attachments.push(item.url);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return { text: textParts.join("\n"), attachments };
|
|
99
|
+
}
|
|
100
|
+
function inputItemFromMessage(message, createdAt) {
|
|
101
|
+
if (isTypedMessage(message, StartThread) || isTypedMessage(message, TurnStart) || isTypedMessage(message, TurnSteer)) {
|
|
102
|
+
const content = inputContent(message);
|
|
103
|
+
if (content.text.trim() === "" && content.attachments.length === 0) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
id: message.messageId,
|
|
108
|
+
kind: "message",
|
|
109
|
+
role: "user",
|
|
110
|
+
text: content.text,
|
|
111
|
+
attachments: content.attachments,
|
|
112
|
+
authorName: message.senderName,
|
|
113
|
+
createdAt
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (isTypedMessage(message, TurnStartAccepted) || isTypedMessage(message, TurnSteerAccepted)) {
|
|
117
|
+
const content = inputContent(message);
|
|
118
|
+
if (content.text.trim() === "" && content.attachments.length === 0) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
id: message.sourceMessageId,
|
|
123
|
+
kind: "message",
|
|
124
|
+
role: "user",
|
|
125
|
+
text: content.text,
|
|
126
|
+
attachments: content.attachments,
|
|
127
|
+
authorName: message.senderName,
|
|
128
|
+
createdAt
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
function pendingItemFromInput(pending) {
|
|
134
|
+
const item = inputItemFromMessage(pending.payload, pending.createdAt);
|
|
135
|
+
if (item === null) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
return { ...item, id: pending.messageId };
|
|
139
|
+
}
|
|
140
|
+
function upsertItem(items, item) {
|
|
141
|
+
items.set(item.id, { ...items.get(item.id), ...item });
|
|
142
|
+
}
|
|
143
|
+
function appendText(items, itemId, base, text) {
|
|
144
|
+
if (text === "") {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const existing = items.get(itemId);
|
|
148
|
+
upsertItem(items, {
|
|
149
|
+
...base,
|
|
150
|
+
id: itemId,
|
|
151
|
+
text: `${existing?.text ?? ""}${text}`,
|
|
152
|
+
attachments: existing?.attachments ?? []
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function imageStatus(message) {
|
|
156
|
+
if (isTypedMessage(message, AgentImageGenerationCompleted)) {
|
|
157
|
+
return "completed";
|
|
158
|
+
}
|
|
159
|
+
if (isTypedMessage(message, AgentImageGenerationFailed)) {
|
|
160
|
+
return "failed";
|
|
161
|
+
}
|
|
162
|
+
return "in_progress";
|
|
163
|
+
}
|
|
164
|
+
function feedFromSession(session) {
|
|
165
|
+
if (session === null) {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
const items = /* @__PURE__ */ new Map();
|
|
169
|
+
for (const event of session.messages) {
|
|
170
|
+
const message = event.message;
|
|
171
|
+
const createdAt = /* @__PURE__ */ new Date();
|
|
172
|
+
const inputItem = inputItemFromMessage(message, createdAt);
|
|
173
|
+
if (inputItem !== null && !items.has(inputItem.id)) {
|
|
174
|
+
upsertItem(items, inputItem);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (isTypedMessage(message, AgentTextContentStarted)) {
|
|
178
|
+
upsertItem(items, {
|
|
179
|
+
id: message.itemId,
|
|
180
|
+
kind: "message",
|
|
181
|
+
role: "agent",
|
|
182
|
+
text: "",
|
|
183
|
+
attachments: [],
|
|
184
|
+
createdAt
|
|
185
|
+
});
|
|
186
|
+
} else if (isTypedMessage(message, AgentTextContentDelta)) {
|
|
187
|
+
appendText(items, message.itemId, {
|
|
188
|
+
id: message.itemId,
|
|
189
|
+
kind: "message",
|
|
190
|
+
role: "agent",
|
|
191
|
+
createdAt
|
|
192
|
+
}, message.text);
|
|
193
|
+
} else if (isTypedMessage(message, AgentTextContentEnded)) {
|
|
194
|
+
if (!items.has(message.itemId)) {
|
|
195
|
+
upsertItem(items, {
|
|
196
|
+
id: message.itemId,
|
|
197
|
+
kind: "message",
|
|
198
|
+
role: "agent",
|
|
199
|
+
text: "",
|
|
200
|
+
attachments: [],
|
|
201
|
+
createdAt
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
} else if (isTypedMessage(message, AgentReasoningContentStarted)) {
|
|
205
|
+
upsertItem(items, {
|
|
206
|
+
id: message.itemId,
|
|
207
|
+
kind: "reasoning",
|
|
208
|
+
role: "agent",
|
|
209
|
+
text: "",
|
|
210
|
+
attachments: [],
|
|
211
|
+
createdAt
|
|
212
|
+
});
|
|
213
|
+
} else if (isTypedMessage(message, AgentReasoningContentDelta)) {
|
|
214
|
+
appendText(items, message.itemId, {
|
|
215
|
+
id: message.itemId,
|
|
216
|
+
kind: "reasoning",
|
|
217
|
+
role: "agent",
|
|
218
|
+
createdAt
|
|
219
|
+
}, message.text);
|
|
220
|
+
} else if (isTypedMessage(message, AgentReasoningContentEnded)) {
|
|
221
|
+
if (!items.has(message.itemId)) {
|
|
222
|
+
upsertItem(items, {
|
|
223
|
+
id: message.itemId,
|
|
224
|
+
kind: "reasoning",
|
|
225
|
+
role: "agent",
|
|
226
|
+
text: "",
|
|
227
|
+
attachments: [],
|
|
228
|
+
createdAt
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
} else if (isTypedMessage(message, AgentFileContentDelta)) {
|
|
232
|
+
const existing = items.get(message.itemId);
|
|
233
|
+
const attachments = existing?.attachments ?? [];
|
|
234
|
+
if (!attachments.includes(message.url)) {
|
|
235
|
+
attachments.push(message.url);
|
|
236
|
+
}
|
|
237
|
+
upsertItem(items, {
|
|
238
|
+
id: message.itemId,
|
|
239
|
+
kind: "message",
|
|
240
|
+
role: "agent",
|
|
241
|
+
text: existing?.text ?? "",
|
|
242
|
+
attachments,
|
|
243
|
+
createdAt
|
|
244
|
+
});
|
|
245
|
+
} else if (isTypedMessage(message, AgentToolCallPending) || isTypedMessage(message, AgentToolCallInProgress) || isTypedMessage(message, AgentToolCallStarted) || isTypedMessage(message, AgentToolCallEnded)) {
|
|
246
|
+
upsertItem(items, {
|
|
247
|
+
id: message.itemId,
|
|
248
|
+
kind: "tool_call",
|
|
249
|
+
role: "agent",
|
|
250
|
+
text: [message.toolkit, message.tool].filter((part) => part?.trim()).join(".") || "Tool call",
|
|
251
|
+
attachments: [],
|
|
252
|
+
createdAt
|
|
253
|
+
});
|
|
254
|
+
} else if (isTypedMessage(message, AgentImageGenerationStarted) || isTypedMessage(message, AgentImageGenerationPartial) || isTypedMessage(message, AgentImageGenerationCompleted) || isTypedMessage(message, AgentImageGenerationFailed)) {
|
|
255
|
+
const image = isTypedMessage(message, AgentImageGenerationPartial) ? message.image : isTypedMessage(message, AgentImageGenerationCompleted) ? message.images[0] : void 0;
|
|
256
|
+
upsertItem(items, {
|
|
257
|
+
id: message.itemId,
|
|
258
|
+
kind: "image_generation",
|
|
259
|
+
role: "agent",
|
|
260
|
+
text: "",
|
|
261
|
+
attachments: [],
|
|
262
|
+
createdAt,
|
|
263
|
+
image: {
|
|
264
|
+
uri: image?.uri,
|
|
265
|
+
status: image?.status ?? imageStatus(message),
|
|
266
|
+
statusDetail: isTypedMessage(message, AgentImageGenerationFailed) ? message.error.message : void 0
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
for (const pending of session.pendingInputs) {
|
|
272
|
+
const item = pendingItemFromInput(pending);
|
|
273
|
+
if (item !== null && !items.has(item.id)) {
|
|
274
|
+
upsertItem(items, item);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return [...items.values()].filter((item) => item.text.trim() !== "" || item.attachments.length > 0 || item.image != null || item.kind === "tool_call");
|
|
278
|
+
}
|
|
279
|
+
function latestThreadStatus(session) {
|
|
280
|
+
if (session === null) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
const status = session.threadStatus;
|
|
284
|
+
if (status !== void 0 && isTypedMessage(status, AgentThreadStatus)) {
|
|
285
|
+
return status;
|
|
286
|
+
}
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
function dateFromString(value) {
|
|
290
|
+
if (value == null || value.trim() === "") {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
const parsed = new Date(value);
|
|
294
|
+
return Number.isNaN(parsed.getTime()) ? null : parsed;
|
|
295
|
+
}
|
|
296
|
+
function normalizeAttachmentPath(path) {
|
|
297
|
+
const prefix = "room:///";
|
|
298
|
+
return path.startsWith(prefix) ? path.slice(prefix.length) : path;
|
|
299
|
+
}
|
|
300
|
+
function MarkdownBlock({ text }) {
|
|
301
|
+
return /* @__PURE__ */ jsx(
|
|
302
|
+
ReactMarkdown,
|
|
303
|
+
{
|
|
304
|
+
remarkPlugins: [remarkGfm],
|
|
305
|
+
rehypePlugins: [rehypeSanitize, rehypeHighlight],
|
|
306
|
+
components: {
|
|
307
|
+
pre: ({ className, children, ...props }) => /* @__PURE__ */ jsx("pre", { ...props, className: cn("overflow-x-auto rounded-md border bg-background/80 p-3", className), children }),
|
|
308
|
+
p: ({ children, ...props }) => /* @__PURE__ */ jsx("p", { ...props, className: "mb-2 last:mb-0", children }),
|
|
309
|
+
ul: ({ children, ...props }) => /* @__PURE__ */ jsx("ul", { ...props, className: "mb-2 ml-6 list-disc last:mb-0", children }),
|
|
310
|
+
ol: ({ children, ...props }) => /* @__PURE__ */ jsx("ol", { ...props, className: "mb-2 ml-6 list-decimal last:mb-0", children })
|
|
311
|
+
},
|
|
312
|
+
children: text
|
|
313
|
+
}
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
function ChatBubble({ text, mine }) {
|
|
317
|
+
if (text.trim() === "") {
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
return /* @__PURE__ */ jsx("div", { className: cn(
|
|
321
|
+
"w-fit max-w-[85%] rounded-md px-4 py-3 text-sm leading-6 shadow-xs sm:max-w-2xl",
|
|
322
|
+
mine ? "bg-secondary/85 text-foreground" : "bg-muted/70 text-foreground"
|
|
323
|
+
), children: /* @__PURE__ */ jsx(MarkdownBlock, { text }) });
|
|
324
|
+
}
|
|
325
|
+
function AttachmentView({ room, path }) {
|
|
326
|
+
const preview = normalizeAttachmentPath(path);
|
|
327
|
+
const filename = preview.split("/").pop() ?? preview;
|
|
328
|
+
return /* @__PURE__ */ jsxs(
|
|
329
|
+
"button",
|
|
330
|
+
{
|
|
331
|
+
type: "button",
|
|
332
|
+
className: "inline-flex max-w-full items-center gap-2 rounded-md bg-muted/60 px-3 py-2 text-left shadow-xs transition-colors hover:bg-muted/80",
|
|
333
|
+
onClick: () => {
|
|
334
|
+
void room.storage.downloadUrl(preview).then((url) => {
|
|
335
|
+
window.open(url, "_blank", "noopener,noreferrer");
|
|
336
|
+
});
|
|
337
|
+
},
|
|
338
|
+
children: [
|
|
339
|
+
/* @__PURE__ */ jsx(FileText, { className: "h-4 w-4 shrink-0 text-muted-foreground" }),
|
|
340
|
+
/* @__PURE__ */ jsx("span", { className: "truncate text-sm font-medium", children: filename }),
|
|
341
|
+
/* @__PURE__ */ jsx(Download, { className: "h-4 w-4 shrink-0 text-muted-foreground" })
|
|
342
|
+
]
|
|
343
|
+
}
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
function ThreadMessageView({
|
|
347
|
+
room,
|
|
348
|
+
message,
|
|
349
|
+
previous,
|
|
350
|
+
localParticipantName,
|
|
351
|
+
agentName
|
|
352
|
+
}) {
|
|
353
|
+
if (message.kind === "reasoning" || message.kind === "tool_call") {
|
|
354
|
+
return message.text.trim() === "" ? null : /* @__PURE__ */ jsx("div", { className: "px-6 py-1 text-center text-sm text-muted-foreground", children: message.text });
|
|
355
|
+
}
|
|
356
|
+
const mine = message.role === "user";
|
|
357
|
+
const authorName = message.authorName ?? (mine ? localParticipantName : displayParticipantName(agentName));
|
|
358
|
+
const previousAuthor = previous?.authorName ?? (previous?.role === "user" ? localParticipantName : displayParticipantName(agentName));
|
|
359
|
+
const shouldShowHeader = previous?.kind !== "message" || previousAuthor !== authorName;
|
|
360
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-2", children: [
|
|
361
|
+
shouldShowHeader ? /* @__PURE__ */ jsx("div", { className: cn("flex w-full", mine ? "justify-end" : "justify-start"), children: /* @__PURE__ */ jsx("div", { className: cn("max-w-[85%] px-1 sm:max-w-2xl", mine ? "text-right" : "text-left"), children: /* @__PURE__ */ jsxs("div", { className: cn("flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-muted-foreground", mine ? "justify-end" : "justify-start"), children: [
|
|
362
|
+
authorName.trim() !== "" ? /* @__PURE__ */ jsx("span", { className: "font-semibold text-foreground", children: displayParticipantName(authorName) }) : null,
|
|
363
|
+
/* @__PURE__ */ jsx("span", { children: timeAgo(message.createdAt) })
|
|
364
|
+
] }) }) }) : null,
|
|
365
|
+
message.text.trim() !== "" ? /* @__PURE__ */ jsx("div", { className: cn("flex w-full", mine ? "justify-end" : "justify-start"), children: /* @__PURE__ */ jsx(ChatBubble, { text: message.text, mine }) }) : null,
|
|
366
|
+
message.attachments.length > 0 ? /* @__PURE__ */ jsx("div", { className: cn("flex w-full", mine ? "justify-end" : "justify-start"), children: /* @__PURE__ */ jsx("div", { className: cn("flex max-w-[85%] flex-wrap gap-3 px-1 sm:max-w-2xl", mine ? "justify-end" : "justify-start"), children: message.attachments.map((attachment, index) => /* @__PURE__ */ jsx(AttachmentView, { room, path: attachment }, `${message.id}:attachment:${attachment}:${index}`)) }) }) : null,
|
|
367
|
+
message.image ? /* @__PURE__ */ jsx("div", { className: "flex w-full justify-start", children: message.image.uri ? /* @__PURE__ */ jsx("img", { src: message.image.uri, alt: "Generated image", className: "max-h-[312px] max-w-full rounded-md object-contain shadow-xs" }) : /* @__PURE__ */ jsx("div", { className: "flex h-[240px] w-[240px] items-center justify-center rounded-md border bg-background text-muted-foreground", children: /* @__PURE__ */ jsxs("div", { className: "flex max-w-full flex-col items-center gap-2 px-3 text-center text-xs", children: [
|
|
368
|
+
message.image.status === "failed" ? /* @__PURE__ */ jsx(ImageOff, { className: "h-5 w-5" }) : /* @__PURE__ */ jsx(Spinner, { className: "h-5 w-5" }),
|
|
369
|
+
/* @__PURE__ */ jsx("span", { children: message.image.statusDetail ?? (message.image.status === "failed" ? "Image failed" : "Generating image") })
|
|
370
|
+
] }) }) }) : null
|
|
371
|
+
] });
|
|
372
|
+
}
|
|
373
|
+
function EmptyState({ title, description }) {
|
|
374
|
+
return /* @__PURE__ */ jsxs("div", { className: "mx-auto flex h-full max-w-2xl flex-col items-center justify-center px-6 py-20 text-center", children: [
|
|
375
|
+
/* @__PURE__ */ jsx("h2", { className: "text-4xl font-semibold tracking-tight text-foreground sm:text-5xl", children: title }),
|
|
376
|
+
description?.trim() ? /* @__PURE__ */ jsx("p", { className: "mt-3 max-w-xl text-sm leading-6 text-muted-foreground sm:text-base", children: description }) : null
|
|
377
|
+
] });
|
|
378
|
+
}
|
|
379
|
+
function ErrorBanner({ message }) {
|
|
380
|
+
return /* @__PURE__ */ jsx("div", { className: "mx-auto w-full max-w-[912px] whitespace-pre-wrap rounded-md border border-destructive/30 bg-destructive/5 px-4 py-3 text-sm text-destructive", children: message });
|
|
381
|
+
}
|
|
382
|
+
function describeError(error) {
|
|
383
|
+
if (error instanceof Error && error.message.trim() !== "") {
|
|
384
|
+
return error.message;
|
|
385
|
+
}
|
|
386
|
+
return String(error);
|
|
387
|
+
}
|
|
388
|
+
function normalizeAgentAttachmentUrl(path) {
|
|
389
|
+
const trimmedPath = path.trim();
|
|
390
|
+
if (trimmedPath === "") {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
try {
|
|
394
|
+
const parsed = new URL(trimmedPath);
|
|
395
|
+
if (parsed.protocol !== "") {
|
|
396
|
+
return trimmedPath;
|
|
397
|
+
}
|
|
398
|
+
} catch {
|
|
399
|
+
}
|
|
400
|
+
const roomPath = trimmedPath.startsWith("/") ? trimmedPath.slice(1) : trimmedPath;
|
|
401
|
+
return roomPath === "" ? null : `room:///${roomPath}`;
|
|
402
|
+
}
|
|
403
|
+
function AgentThread({
|
|
404
|
+
room,
|
|
405
|
+
path,
|
|
406
|
+
chatClient,
|
|
407
|
+
disposeChatClient = false,
|
|
408
|
+
agentName,
|
|
409
|
+
emptyStateTitle = "Chat to get started",
|
|
410
|
+
emptyStateDescription,
|
|
411
|
+
clientToolkits
|
|
412
|
+
}) {
|
|
413
|
+
const [attachments, setAttachments] = useState([]);
|
|
414
|
+
const [sendError, setSendError] = useState(null);
|
|
415
|
+
const [version, setVersion] = useState(0);
|
|
416
|
+
const scrollContainerRef = useRef(null);
|
|
417
|
+
const contentRef = useRef(null);
|
|
418
|
+
const stickToBottomRef = useRef(true);
|
|
419
|
+
const sessionRef = useRef(null);
|
|
420
|
+
const ownsChatClient = chatClient == null;
|
|
421
|
+
const activeChatClient = useMemo(
|
|
422
|
+
() => chatClient ?? new MessagingChatClient({ room, agentName }),
|
|
423
|
+
[agentName, chatClient, room]
|
|
424
|
+
);
|
|
425
|
+
const localParticipantName = getParticipantName(room.localParticipant);
|
|
426
|
+
const agentParticipant = activeChatClient.agentParticipant() ?? findAgentParticipant(room, agentName);
|
|
427
|
+
useEffect(() => {
|
|
428
|
+
void activeChatClient.start();
|
|
429
|
+
const handleChange = () => {
|
|
430
|
+
setVersion((current) => current + 1);
|
|
431
|
+
};
|
|
432
|
+
activeChatClient.addListener(handleChange);
|
|
433
|
+
return () => {
|
|
434
|
+
activeChatClient.removeListener(handleChange);
|
|
435
|
+
if (ownsChatClient || disposeChatClient) {
|
|
436
|
+
void activeChatClient.stop();
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
}, [activeChatClient, disposeChatClient, ownsChatClient]);
|
|
440
|
+
useEffect(() => {
|
|
441
|
+
const session2 = activeChatClient.openThread(path);
|
|
442
|
+
sessionRef.current = session2;
|
|
443
|
+
const handleChange = () => {
|
|
444
|
+
setVersion((current) => current + 1);
|
|
445
|
+
};
|
|
446
|
+
session2.addListener(handleChange);
|
|
447
|
+
handleChange();
|
|
448
|
+
return () => {
|
|
449
|
+
session2.removeListener(handleChange);
|
|
450
|
+
if (sessionRef.current === session2) {
|
|
451
|
+
sessionRef.current = null;
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
}, [activeChatClient, path]);
|
|
455
|
+
const session = sessionRef.current;
|
|
456
|
+
const feedItems = useMemo(() => feedFromSession(session), [session, version]);
|
|
457
|
+
const status = useMemo(() => latestThreadStatus(session), [session, version]);
|
|
458
|
+
const statusText = status?.status?.trim() || null;
|
|
459
|
+
const turnId = stringValue(status?.turnId);
|
|
460
|
+
const canInterruptActiveTurn = turnId != null && (agentParticipant != null || chatClient != null);
|
|
461
|
+
const lastItem = feedItems.length > 0 ? feedItems[feedItems.length - 1] : void 0;
|
|
462
|
+
const lastMessageKey = `${lastItem?.id ?? ""}:${lastItem?.text.length ?? 0}:${feedItems.length}:${version}`;
|
|
463
|
+
useEffect(() => {
|
|
464
|
+
const container = scrollContainerRef.current;
|
|
465
|
+
if (!container) {
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
stickToBottomRef.current = true;
|
|
469
|
+
container.scrollTop = container.scrollHeight;
|
|
470
|
+
}, [path]);
|
|
471
|
+
useEffect(() => {
|
|
472
|
+
const container = scrollContainerRef.current;
|
|
473
|
+
if (!container || !stickToBottomRef.current) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
container.scrollTop = container.scrollHeight;
|
|
477
|
+
}, [lastMessageKey, statusText]);
|
|
478
|
+
useEffect(() => {
|
|
479
|
+
const container = scrollContainerRef.current;
|
|
480
|
+
const content = contentRef.current;
|
|
481
|
+
if (!container || !content || typeof ResizeObserver === "undefined") {
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const observer = new ResizeObserver(() => {
|
|
485
|
+
if (stickToBottomRef.current) {
|
|
486
|
+
container.scrollTop = container.scrollHeight;
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
observer.observe(content);
|
|
490
|
+
return () => {
|
|
491
|
+
observer.disconnect();
|
|
492
|
+
};
|
|
493
|
+
}, []);
|
|
494
|
+
const selectAttachments = useCallback((files) => {
|
|
495
|
+
const nextAttachments = files.map((file) => new MeshagentFileUpload(
|
|
496
|
+
room,
|
|
497
|
+
`uploaded-files/${file.name}`,
|
|
498
|
+
fileToAsyncIterable(file),
|
|
499
|
+
file.size
|
|
500
|
+
));
|
|
501
|
+
setAttachments((current) => [...current, ...nextAttachments]);
|
|
502
|
+
}, [room]);
|
|
503
|
+
const handleSend = useCallback(async (message) => {
|
|
504
|
+
if (message.text.trim() === "" && message.attachments.length === 0) {
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
if (!agentParticipant && chatClient == null) {
|
|
508
|
+
setSendError("This thread requires an online agent that supports agent messages.");
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const openSession = sessionRef.current;
|
|
512
|
+
if (openSession === null) {
|
|
513
|
+
setSendError("No thread session is open.");
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
const normalizedAttachments = message.attachments.map(normalizeAgentAttachmentUrl).filter((attachment) => attachment !== null);
|
|
517
|
+
try {
|
|
518
|
+
await openSession.sendText({
|
|
519
|
+
messageId: message.id,
|
|
520
|
+
text: message.text,
|
|
521
|
+
attachments: normalizedAttachments,
|
|
522
|
+
turnId,
|
|
523
|
+
steer: status?.mode === "steerable" && turnId != null,
|
|
524
|
+
senderName: localParticipantName.trim() || void 0,
|
|
525
|
+
clientToolkits
|
|
526
|
+
});
|
|
527
|
+
setSendError(null);
|
|
528
|
+
setVersion((current) => current + 1);
|
|
529
|
+
} catch (error) {
|
|
530
|
+
setSendError(describeError(error));
|
|
531
|
+
}
|
|
532
|
+
}, [agentParticipant, chatClient, clientToolkits, localParticipantName, status?.mode, turnId]);
|
|
533
|
+
const cancelTurn = useCallback(async () => {
|
|
534
|
+
const openSession = sessionRef.current;
|
|
535
|
+
if (openSession === null || turnId == null) {
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
await openSession.interruptTurn(turnId);
|
|
539
|
+
}, [turnId]);
|
|
540
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex h-full min-h-0 flex-1 flex-col", children: [
|
|
541
|
+
/* @__PURE__ */ jsxs("div", { className: "relative flex h-full min-h-0 flex-1 flex-col", children: [
|
|
542
|
+
/* @__PURE__ */ jsx(
|
|
543
|
+
"div",
|
|
544
|
+
{
|
|
545
|
+
ref: scrollContainerRef,
|
|
546
|
+
className: "min-h-0 flex-1 overflow-y-auto overflow-x-hidden [overflow-anchor:none]",
|
|
547
|
+
onScroll: (event) => {
|
|
548
|
+
stickToBottomRef.current = isNearBottom(event.currentTarget);
|
|
549
|
+
},
|
|
550
|
+
children: /* @__PURE__ */ jsxs(
|
|
551
|
+
"div",
|
|
552
|
+
{
|
|
553
|
+
ref: contentRef,
|
|
554
|
+
className: cn(
|
|
555
|
+
"mx-auto flex min-h-full w-full max-w-[912px] flex-col gap-8 px-4 pt-6",
|
|
556
|
+
feedItems.length > 0 ? "justify-end" : null,
|
|
557
|
+
statusText ? "pb-24" : "pb-6"
|
|
558
|
+
),
|
|
559
|
+
children: [
|
|
560
|
+
feedItems.length === 0 ? /* @__PURE__ */ jsx(EmptyState, { title: emptyStateTitle, description: emptyStateDescription }) : null,
|
|
561
|
+
feedItems.map((message, index) => /* @__PURE__ */ jsx(
|
|
562
|
+
ThreadMessageView,
|
|
563
|
+
{
|
|
564
|
+
room,
|
|
565
|
+
message,
|
|
566
|
+
previous: index > 0 ? feedItems[index - 1] : null,
|
|
567
|
+
localParticipantName,
|
|
568
|
+
agentName
|
|
569
|
+
},
|
|
570
|
+
message.id
|
|
571
|
+
))
|
|
572
|
+
]
|
|
573
|
+
}
|
|
574
|
+
)
|
|
575
|
+
}
|
|
576
|
+
),
|
|
577
|
+
statusText ? /* @__PURE__ */ jsx("div", { className: "pointer-events-none absolute inset-x-0 bottom-0 flex justify-center px-4 pb-4", children: /* @__PURE__ */ jsx("div", { className: "pointer-events-auto w-full max-w-[912px]", children: /* @__PURE__ */ jsx(
|
|
578
|
+
ChatTypingIndicator,
|
|
579
|
+
{
|
|
580
|
+
typing: false,
|
|
581
|
+
thinking: false,
|
|
582
|
+
statusText,
|
|
583
|
+
startedAt: dateFromString(status?.startedAt),
|
|
584
|
+
onCancel: canInterruptActiveTurn ? cancelTurn : void 0,
|
|
585
|
+
showCancelButton: status?.mode != null,
|
|
586
|
+
cancelEnabled: true
|
|
587
|
+
}
|
|
588
|
+
) }) }) : null
|
|
589
|
+
] }),
|
|
590
|
+
sendError ? /* @__PURE__ */ jsx("div", { className: "px-4 pb-2", children: /* @__PURE__ */ jsx(ErrorBanner, { message: sendError }) }) : null,
|
|
591
|
+
/* @__PURE__ */ jsx(
|
|
592
|
+
ChatInput,
|
|
593
|
+
{
|
|
594
|
+
onSubmit: handleSend,
|
|
595
|
+
attachments,
|
|
596
|
+
onFilesSelected: selectAttachments,
|
|
597
|
+
setAttachments,
|
|
598
|
+
disabled: agentParticipant == null && chatClient == null,
|
|
599
|
+
placeholder: agentParticipant || chatClient ? "Type a message" : `Waiting for ${displayParticipantName(agentName)}`
|
|
600
|
+
}
|
|
601
|
+
)
|
|
602
|
+
] });
|
|
603
|
+
}
|
|
604
|
+
export {
|
|
605
|
+
AgentThread
|
|
606
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ReactElement } from "react";
|
|
2
2
|
import { Participant, RoomClient } from "@meshagent/meshagent";
|
|
3
|
-
import type { BaseChatClient } from "@meshagent/meshagent-agents";
|
|
3
|
+
import type { BaseChatClient, ClientToolkitDescription } from "@meshagent/meshagent-agents";
|
|
4
4
|
import { ChatThreadDisplayMode } from "./conversation-descriptor.js";
|
|
5
5
|
export { ChatThreadDisplayMode, chatDocumentPath, resolvedThreadListPath, } from "./conversation-descriptor.js";
|
|
6
6
|
export interface ChatBotViewProps {
|
|
@@ -30,5 +30,6 @@ export interface ChatBotViewProps {
|
|
|
30
30
|
showThreadList?: boolean;
|
|
31
31
|
threadListWidth?: number;
|
|
32
32
|
threadListCollapsedHeight?: number;
|
|
33
|
+
clientToolkits?: ClientToolkitDescription[];
|
|
33
34
|
}
|
|
34
|
-
export declare function ChatBotView({ room, chatClient, disposeChatClient, path, documentPath, participants, agentName, threadDisplayMode, threadDir, threadListPath, toolkit, tool, centerComposer, emptyStateTitle, emptyStateDescription, startNewThreadTitle, startNewThreadDescription, selectedThreadPath, onSelectedThreadPathChanged, onSelectedThreadResolved, onThreadResolved, newThreadResetVersion, showThreadList, threadListWidth, threadListCollapsedHeight, }: ChatBotViewProps): ReactElement;
|
|
35
|
+
export declare function ChatBotView({ room, chatClient, disposeChatClient, path, documentPath, participants, agentName, threadDisplayMode, threadDir, threadListPath, toolkit, tool, centerComposer, emptyStateTitle, emptyStateDescription, startNewThreadTitle, startNewThreadDescription, selectedThreadPath, onSelectedThreadPathChanged, onSelectedThreadResolved, onThreadResolved, newThreadResetVersion, showThreadList, threadListWidth, threadListCollapsedHeight, clientToolkits, }: ChatBotViewProps): ReactElement;
|