@admin-layout/tailwind-chat-pro 12.2.4-alpha.102
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 +77 -0
- package/LICENSE +21 -0
- package/lib/components/index.d.ts +2 -0
- package/lib/components/index.d.ts.map +1 -0
- package/lib/components/messages-container-ui/DefaultInputToolBar.d.ts +21 -0
- package/lib/components/messages-container-ui/DefaultInputToolBar.d.ts.map +1 -0
- package/lib/components/messages-container-ui/DefaultInputToolBar.js +46 -0
- package/lib/components/messages-container-ui/DefaultInputToolBar.js.map +1 -0
- package/lib/components/messages-container-ui/DefaultMessageGroup.d.ts +16 -0
- package/lib/components/messages-container-ui/DefaultMessageGroup.d.ts.map +1 -0
- package/lib/components/messages-container-ui/DefaultMessageGroup.js +64 -0
- package/lib/components/messages-container-ui/DefaultMessageGroup.js.map +1 -0
- package/lib/components/messages-container-ui/ErrorFixCard.d.ts +10 -0
- package/lib/components/messages-container-ui/ErrorFixCard.d.ts.map +1 -0
- package/lib/components/messages-container-ui/ErrorFixCard.js +219 -0
- package/lib/components/messages-container-ui/ErrorFixCard.js.map +1 -0
- package/lib/components/messages-container-ui/FilesList.d.ts +19 -0
- package/lib/components/messages-container-ui/FilesList.d.ts.map +1 -0
- package/lib/components/messages-container-ui/FilesList.js +75 -0
- package/lib/components/messages-container-ui/FilesList.js.map +1 -0
- package/lib/components/messages-container-ui/MessagesContainerUI.d.ts +98 -0
- package/lib/components/messages-container-ui/MessagesContainerUI.d.ts.map +1 -0
- package/lib/components/messages-container-ui/MessagesContainerUI.js +88 -0
- package/lib/components/messages-container-ui/MessagesContainerUI.js.map +1 -0
- package/lib/components/messages-container-ui/ModernMessageGroup.d.ts +46 -0
- package/lib/components/messages-container-ui/ModernMessageGroup.d.ts.map +1 -0
- package/lib/components/messages-container-ui/ModernMessageGroup.js +1568 -0
- package/lib/components/messages-container-ui/ModernMessageGroup.js.map +1 -0
- package/lib/components/messages-container-ui/PlanModeView.d.ts +107 -0
- package/lib/components/messages-container-ui/PlanModeView.d.ts.map +1 -0
- package/lib/components/messages-container-ui/PlanModeView.js +294 -0
- package/lib/components/messages-container-ui/PlanModeView.js.map +1 -0
- package/lib/components/messages-container-ui/index.d.ts +12 -0
- package/lib/components/messages-container-ui/index.d.ts.map +1 -0
- package/lib/components/messages-container-ui/messenger-slot-fill-name-enum.d.ts +16 -0
- package/lib/components/messages-container-ui/messenger-slot-fill-name-enum.d.ts.map +1 -0
- package/lib/components/messages-container-ui/messenger-slot-fill-name-enum.js +16 -0
- package/lib/components/messages-container-ui/messenger-slot-fill-name-enum.js.map +1 -0
- package/lib/components/messages-container-ui/modernStreaming.d.ts +11 -0
- package/lib/components/messages-container-ui/modernStreaming.d.ts.map +1 -0
- package/lib/components/messages-container-ui/modernStreaming.js +17 -0
- package/lib/components/messages-container-ui/modernStreaming.js.map +1 -0
- package/lib/components/messages-container-ui/types.d.ts +63 -0
- package/lib/components/messages-container-ui/types.d.ts.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -0
- package/lib/routes.json +1 -0
- package/package.json +48 -0
- package/rollup.config.mjs +34 -0
- package/src/components/index.ts +1 -0
- package/src/components/messages-container-ui/DefaultInputToolBar.tsx +63 -0
- package/src/components/messages-container-ui/DefaultMessageGroup.tsx +75 -0
- package/src/components/messages-container-ui/ErrorFixCard.tsx +239 -0
- package/src/components/messages-container-ui/FilesList.tsx +89 -0
- package/src/components/messages-container-ui/MessagesContainerUI.tsx +178 -0
- package/src/components/messages-container-ui/ModernMessageGroup.tsx +1885 -0
- package/src/components/messages-container-ui/PlanModeView.tsx +457 -0
- package/src/components/messages-container-ui/index.ts +18 -0
- package/src/components/messages-container-ui/messenger-slot-fill-name-enum.ts +15 -0
- package/src/components/messages-container-ui/modernStreaming.tsx +18 -0
- package/src/components/messages-container-ui/types.ts +68 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +27 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import clsx from 'clsx';
|
|
3
|
+
import { AiAgentMessageRole, PostTypeEnum } from 'common';
|
|
4
|
+
import type { MessageGroupComponentType, PlanMessage } from './types';
|
|
5
|
+
import { DefaultMessageGroup } from './DefaultMessageGroup';
|
|
6
|
+
import { DefaultInputToolBar } from './DefaultInputToolBar';
|
|
7
|
+
|
|
8
|
+
const ASSISTANT_BUBBLE_BG = 'bg-white';
|
|
9
|
+
const BORDER_COLOR = 'border-gray-200';
|
|
10
|
+
|
|
11
|
+
const HIDE_SCROLLBAR_CSS = `
|
|
12
|
+
/* Hide scrollbar while keeping scrollability */
|
|
13
|
+
.ym-hide-scrollbar {
|
|
14
|
+
-ms-overflow-style: none; /* IE/Edge */
|
|
15
|
+
scrollbar-width: none; /* Firefox */
|
|
16
|
+
}
|
|
17
|
+
.ym-hide-scrollbar::-webkit-scrollbar {
|
|
18
|
+
display: none; /* Chrome/Safari */
|
|
19
|
+
width: 0;
|
|
20
|
+
height: 0;
|
|
21
|
+
}
|
|
22
|
+
`;
|
|
23
|
+
|
|
24
|
+
const DEFAULT_ASSISTANT_AUTHOR = {
|
|
25
|
+
id: 'ai-assistant',
|
|
26
|
+
givenName: 'AI',
|
|
27
|
+
familyName: 'Assistant',
|
|
28
|
+
fullName: 'AI Assistant',
|
|
29
|
+
username: 'ai-assistant',
|
|
30
|
+
email: 'ai@assistant.com',
|
|
31
|
+
picture: null,
|
|
32
|
+
alias: [],
|
|
33
|
+
tokens: [],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export interface PlanModeViewInputToolbarProps {
|
|
37
|
+
value: string;
|
|
38
|
+
onChange: (text: string) => void;
|
|
39
|
+
onSend: () => void;
|
|
40
|
+
disabled?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface PlanModeViewProps {
|
|
44
|
+
messages: PlanMessage[];
|
|
45
|
+
streamingContent?: string;
|
|
46
|
+
currentUser: { id: string; name?: string; avatar?: string; [key: string]: any };
|
|
47
|
+
onSend: (text: string) => void;
|
|
48
|
+
disabled?: boolean;
|
|
49
|
+
isLoading?: boolean;
|
|
50
|
+
onStop?: () => void;
|
|
51
|
+
showDate?: boolean;
|
|
52
|
+
showAvatar?: boolean;
|
|
53
|
+
showMessageBubbleBorder?: boolean;
|
|
54
|
+
/** When false (default), do NOT create an internal scroll container (avoids nested scroll). */
|
|
55
|
+
useInternalScroll?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Optional ref for the scroll container element (defaults to the internal
|
|
58
|
+
* message list wrapper).
|
|
59
|
+
*
|
|
60
|
+
* If provided, it will be attached to the internal scroll container div
|
|
61
|
+
* (so parent code can read/scroll it).
|
|
62
|
+
*/
|
|
63
|
+
scrollContainerRef?: React.Ref<HTMLDivElement>;
|
|
64
|
+
/** Optional className applied to the scroll container element. */
|
|
65
|
+
scrollContainerClassName?: string;
|
|
66
|
+
/** When true, hide the scrollbar visuals but keep it scrollable. */
|
|
67
|
+
hideScrollbar?: boolean;
|
|
68
|
+
contentClassName?: string;
|
|
69
|
+
postContainerClassName?: string;
|
|
70
|
+
|
|
71
|
+
/** Optional className applied to the root wrapper element. */
|
|
72
|
+
className?: string;
|
|
73
|
+
/** When true, the built-in input toolbar area is not rendered. */
|
|
74
|
+
hideInputToolbar?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Where to place the input toolbar.
|
|
77
|
+
* - `bottom` (default): render toolbar in the bottom area.
|
|
78
|
+
* - `inlineWhenEmpty`: when there are no messages and no streaming/loading, render the toolbar inline
|
|
79
|
+
* inside the empty state (matches landing UI), otherwise use bottom placement.
|
|
80
|
+
*/
|
|
81
|
+
inputPlacement?: 'bottom' | 'inlineWhenEmpty';
|
|
82
|
+
/**
|
|
83
|
+
* Optional: render the input toolbar; receives value, onChange, onSend, disabled.
|
|
84
|
+
* Defaults to {@link DefaultInputToolBar} (which wraps `@admin-layout/tailwind-ui`'s
|
|
85
|
+
* `InputToolBar`), so the base ships a working composer out of the box.
|
|
86
|
+
*/
|
|
87
|
+
renderInputToolbar?: (props: PlanModeViewInputToolbarProps) => React.ReactNode;
|
|
88
|
+
/** Placeholder for the default input toolbar (ignored when `renderInputToolbar` is provided). */
|
|
89
|
+
inputPlaceholder?: string;
|
|
90
|
+
/** Optional: passed to the injected message-group renderer */
|
|
91
|
+
onOpen?: (element?: any) => void;
|
|
92
|
+
onMessageClick?: (msg: any) => void;
|
|
93
|
+
isDesktopView?: boolean;
|
|
94
|
+
isSmallScreen?: boolean;
|
|
95
|
+
sandboxErrors?: any[];
|
|
96
|
+
currentFiles?: Record<string, string>;
|
|
97
|
+
onFixError?: (errorMessage: string) => Promise<void>;
|
|
98
|
+
/** Optional: error message to show above messages; use with onRetry for retry button */
|
|
99
|
+
error?: string | null;
|
|
100
|
+
/** Optional: called when user clicks retry (e.g. after error) */
|
|
101
|
+
onRetry?: () => void;
|
|
102
|
+
/**
|
|
103
|
+
* Optional: custom content when there are no messages and no streaming/loading.
|
|
104
|
+
* Receives input toolbar props so you can render the composer inline (e.g. landing UI).
|
|
105
|
+
*/
|
|
106
|
+
renderEmptyState?: (ctx: { inputToolbarProps: PlanModeViewInputToolbarProps }) => React.ReactNode;
|
|
107
|
+
/**
|
|
108
|
+
* Optional: when provided, scroll to bottom whenever these deps change (e.g. [streamingContent, messages.length]).
|
|
109
|
+
* Lets the parent force scroll on each stream chunk when the internal ResizeObserver/layout effect miss.
|
|
110
|
+
*/
|
|
111
|
+
scrollToBottomDeps?: React.DependencyList;
|
|
112
|
+
/** When true, fenced / raw code blocks show copy and download actions (default: true). */
|
|
113
|
+
showCopyDownloadOnCodeBlock?: boolean;
|
|
114
|
+
|
|
115
|
+
// ----- Customization (the "base, customizable" surface) -----
|
|
116
|
+
/**
|
|
117
|
+
* The message-group renderer. Each rendered group receives a single mapped post.
|
|
118
|
+
* Defaults to {@link DefaultMessageGroup} (plain-text bubbles) so the base works
|
|
119
|
+
* standalone. Inject a rich renderer (e.g. messenger-box's
|
|
120
|
+
* `ModernMessageGroupComponent`) to get markdown / code / widget-card rendering.
|
|
121
|
+
*/
|
|
122
|
+
MessageGroupComponent?: MessageGroupComponentType;
|
|
123
|
+
/**
|
|
124
|
+
* Optional: how to render the in-flight streaming assistant content. Defaults to a
|
|
125
|
+
* plain-text bubble. Inject a richer renderer (markdown / code) if desired.
|
|
126
|
+
*/
|
|
127
|
+
renderStreamingContent?: (content: string) => React.ReactNode;
|
|
128
|
+
/**
|
|
129
|
+
* Optional: override how a PlanMessage is mapped to the post shape handed to
|
|
130
|
+
* `MessageGroupComponent`. Defaults to {@link defaultPlanMessageToPost}.
|
|
131
|
+
*/
|
|
132
|
+
mapMessageToPost?: (msg: PlanMessage, currentUser: PlanModeViewProps['currentUser']) => any;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Default mapping of a PlanMessage to the post-like shape expected by message-group renderers. */
|
|
136
|
+
export function defaultPlanMessageToPost(msg: PlanMessage, currentUser: PlanModeViewProps['currentUser']): any {
|
|
137
|
+
const isUser = msg.role === 'user';
|
|
138
|
+
const author =
|
|
139
|
+
msg.author ??
|
|
140
|
+
(isUser
|
|
141
|
+
? {
|
|
142
|
+
id: currentUser.id,
|
|
143
|
+
givenName: currentUser.givenName ?? currentUser.name ?? 'User',
|
|
144
|
+
familyName: currentUser.familyName ?? '',
|
|
145
|
+
fullName: currentUser.fullName ?? currentUser.name ?? 'User',
|
|
146
|
+
username: currentUser.username ?? currentUser.id,
|
|
147
|
+
email: currentUser.email ?? '',
|
|
148
|
+
picture: currentUser.picture ?? currentUser.avatar ?? null,
|
|
149
|
+
alias: currentUser.alias ?? [],
|
|
150
|
+
tokens: currentUser.tokens ?? [],
|
|
151
|
+
}
|
|
152
|
+
: DEFAULT_ASSISTANT_AUTHOR);
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
id: msg.id,
|
|
156
|
+
message: msg.content,
|
|
157
|
+
author,
|
|
158
|
+
createdAt: msg.createdAt ?? new Date().toISOString(),
|
|
159
|
+
type: msg.type ?? PostTypeEnum.Aiassistant,
|
|
160
|
+
isDelivered: false,
|
|
161
|
+
isRead: false,
|
|
162
|
+
parentId: null,
|
|
163
|
+
fromServer: null,
|
|
164
|
+
updatedAt: msg.createdAt ?? new Date().toISOString(),
|
|
165
|
+
propsConfiguration: {
|
|
166
|
+
...(msg.propsConfiguration ?? { contents: {} }),
|
|
167
|
+
contents: {
|
|
168
|
+
...((msg.propsConfiguration as any)?.contents ?? {}),
|
|
169
|
+
role:
|
|
170
|
+
(msg.propsConfiguration as any)?.contents?.role ??
|
|
171
|
+
(isUser ? AiAgentMessageRole.User : AiAgentMessageRole.Assistant),
|
|
172
|
+
// Plan/build mode: consumers (e.g. ModernMessageBubble) read propsConfiguration.contents.mode
|
|
173
|
+
...((msg.props as { mode?: string } | undefined)?.mode != null
|
|
174
|
+
? { mode: (msg.props as { mode?: string }).mode }
|
|
175
|
+
: {}),
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
props: msg.props ?? {},
|
|
179
|
+
files: msg.files ?? [],
|
|
180
|
+
replies: null,
|
|
181
|
+
channel: null,
|
|
182
|
+
isPinned: false,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function PlanModeView({
|
|
187
|
+
messages,
|
|
188
|
+
streamingContent,
|
|
189
|
+
currentUser,
|
|
190
|
+
onSend,
|
|
191
|
+
disabled = false,
|
|
192
|
+
isLoading = false,
|
|
193
|
+
onStop,
|
|
194
|
+
showDate = false,
|
|
195
|
+
showAvatar = false,
|
|
196
|
+
showMessageBubbleBorder = false,
|
|
197
|
+
useInternalScroll = false,
|
|
198
|
+
scrollContainerRef,
|
|
199
|
+
scrollContainerClassName,
|
|
200
|
+
hideScrollbar = false,
|
|
201
|
+
contentClassName,
|
|
202
|
+
postContainerClassName,
|
|
203
|
+
className,
|
|
204
|
+
hideInputToolbar = false,
|
|
205
|
+
inputPlacement = 'bottom',
|
|
206
|
+
renderInputToolbar,
|
|
207
|
+
inputPlaceholder,
|
|
208
|
+
onOpen = () => {},
|
|
209
|
+
onMessageClick = () => {},
|
|
210
|
+
isDesktopView = false,
|
|
211
|
+
isSmallScreen = false,
|
|
212
|
+
sandboxErrors = [],
|
|
213
|
+
currentFiles = {},
|
|
214
|
+
onFixError,
|
|
215
|
+
error = null,
|
|
216
|
+
onRetry,
|
|
217
|
+
renderEmptyState,
|
|
218
|
+
scrollToBottomDeps,
|
|
219
|
+
showCopyDownloadOnCodeBlock = true,
|
|
220
|
+
MessageGroupComponent = DefaultMessageGroup,
|
|
221
|
+
renderStreamingContent,
|
|
222
|
+
mapMessageToPost = defaultPlanMessageToPost,
|
|
223
|
+
}: PlanModeViewProps) {
|
|
224
|
+
const [inputValue, setInputValue] = useState('');
|
|
225
|
+
const internalListRef = useRef<HTMLDivElement>(null);
|
|
226
|
+
const contentRef = useRef<HTMLDivElement>(null);
|
|
227
|
+
const shouldAutoScrollRef = useRef(true);
|
|
228
|
+
|
|
229
|
+
const setScrollContainerRef = useCallback(
|
|
230
|
+
(node: HTMLDivElement | null) => {
|
|
231
|
+
internalListRef.current = node;
|
|
232
|
+
|
|
233
|
+
if (!scrollContainerRef) return;
|
|
234
|
+
if (typeof scrollContainerRef === 'function') {
|
|
235
|
+
scrollContainerRef(node);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// RefObject / MutableRefObject
|
|
240
|
+
(scrollContainerRef as React.MutableRefObject<HTMLDivElement | null>).current = node;
|
|
241
|
+
},
|
|
242
|
+
[scrollContainerRef],
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
const scrollToBottom = useCallback(() => {
|
|
246
|
+
const el = internalListRef.current;
|
|
247
|
+
if (el && shouldAutoScrollRef.current) {
|
|
248
|
+
el.scrollTop = el.scrollHeight;
|
|
249
|
+
}
|
|
250
|
+
}, []);
|
|
251
|
+
|
|
252
|
+
const handleSendFromInput = useCallback(() => {
|
|
253
|
+
const t = inputValue.trim();
|
|
254
|
+
if (!t) return;
|
|
255
|
+
onSend(t);
|
|
256
|
+
setInputValue('');
|
|
257
|
+
}, [inputValue, onSend]);
|
|
258
|
+
|
|
259
|
+
const inputToolbarProps: PlanModeViewInputToolbarProps = {
|
|
260
|
+
value: inputValue,
|
|
261
|
+
onChange: setInputValue,
|
|
262
|
+
onSend: handleSendFromInput,
|
|
263
|
+
disabled: disabled || isLoading,
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// Default the input composer to @admin-layout/tailwind-ui's InputToolBar (the shared
|
|
267
|
+
// "ai-tool-box"); consumers can override with `renderInputToolbar` for a richer composer.
|
|
268
|
+
const renderToolbar =
|
|
269
|
+
renderInputToolbar ??
|
|
270
|
+
((p: PlanModeViewInputToolbarProps) => (
|
|
271
|
+
<DefaultInputToolBar {...p} isLoading={isLoading} onStop={onStop} placeholder={inputPlaceholder} />
|
|
272
|
+
));
|
|
273
|
+
|
|
274
|
+
const currentUserForGroup = useMemo(
|
|
275
|
+
() => ({
|
|
276
|
+
id: currentUser.id,
|
|
277
|
+
givenName: currentUser.givenName ?? currentUser.name ?? 'You',
|
|
278
|
+
familyName: currentUser.familyName ?? '',
|
|
279
|
+
fullName: currentUser.fullName ?? currentUser.name ?? 'You',
|
|
280
|
+
username: currentUser.username ?? currentUser.id,
|
|
281
|
+
email: currentUser.email ?? '',
|
|
282
|
+
picture: currentUser.picture ?? currentUser.avatar ?? null,
|
|
283
|
+
alias: currentUser.alias ?? [],
|
|
284
|
+
tokens: currentUser.tokens ?? [],
|
|
285
|
+
}),
|
|
286
|
+
[currentUser],
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
const posts = useMemo(
|
|
290
|
+
() => messages.map((m) => mapMessageToPost(m, currentUser)),
|
|
291
|
+
[messages, currentUser, mapMessageToPost],
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
const isEmpty = posts.length === 0 && !streamingContent?.trim() && !isLoading;
|
|
295
|
+
const shouldInlineToolbar = inputPlacement === 'inlineWhenEmpty' && isEmpty;
|
|
296
|
+
const shouldRenderBottomToolbar = !hideInputToolbar && !shouldInlineToolbar;
|
|
297
|
+
|
|
298
|
+
// Auto-scroll to bottom while user stays near bottom.
|
|
299
|
+
useLayoutEffect(() => {
|
|
300
|
+
if (!useInternalScroll) return;
|
|
301
|
+
if (!shouldAutoScrollRef.current) return;
|
|
302
|
+
scrollToBottom();
|
|
303
|
+
const id = requestAnimationFrame(() => {
|
|
304
|
+
scrollToBottom();
|
|
305
|
+
requestAnimationFrame(scrollToBottom);
|
|
306
|
+
});
|
|
307
|
+
return () => cancelAnimationFrame(id);
|
|
308
|
+
}, [useInternalScroll, scrollToBottom, posts.length, streamingContent, isLoading]);
|
|
309
|
+
|
|
310
|
+
// When internal scroll is (re)enabled, force follow-to-bottom.
|
|
311
|
+
useEffect(() => {
|
|
312
|
+
if (!useInternalScroll) return;
|
|
313
|
+
shouldAutoScrollRef.current = true;
|
|
314
|
+
scrollToBottom();
|
|
315
|
+
}, [useInternalScroll, scrollToBottom]);
|
|
316
|
+
|
|
317
|
+
// When inner content grows (stream chunks, markdown expand), scroll to bottom if user was near bottom.
|
|
318
|
+
// Observing the content wrapper ensures we react to content height changes; the scroll container size often does not change.
|
|
319
|
+
useEffect(() => {
|
|
320
|
+
if (!useInternalScroll) return;
|
|
321
|
+
const el = contentRef.current;
|
|
322
|
+
if (!el) return;
|
|
323
|
+
if (typeof ResizeObserver === 'undefined') return;
|
|
324
|
+
|
|
325
|
+
const ro = new ResizeObserver(() => {
|
|
326
|
+
if (!shouldAutoScrollRef.current) return;
|
|
327
|
+
scrollToBottom();
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
ro.observe(el);
|
|
331
|
+
return () => ro.disconnect();
|
|
332
|
+
}, [useInternalScroll, scrollToBottom]);
|
|
333
|
+
|
|
334
|
+
// Parent-controlled scroll: when scrollToBottomDeps change (e.g. each stream chunk), force scroll to bottom.
|
|
335
|
+
useEffect(() => {
|
|
336
|
+
if (!useInternalScroll || !scrollToBottomDeps?.length) return;
|
|
337
|
+
scrollToBottom();
|
|
338
|
+
const id = requestAnimationFrame(() => {
|
|
339
|
+
scrollToBottom();
|
|
340
|
+
});
|
|
341
|
+
return () => cancelAnimationFrame(id);
|
|
342
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- intentional: parent passes deps
|
|
343
|
+
}, scrollToBottomDeps ?? []);
|
|
344
|
+
|
|
345
|
+
const handleScroll = useCallback(() => {
|
|
346
|
+
const el = internalListRef.current;
|
|
347
|
+
if (!el) return;
|
|
348
|
+
const thresholdPx = 160;
|
|
349
|
+
const distanceFromBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
|
|
350
|
+
shouldAutoScrollRef.current = distanceFromBottom <= thresholdPx;
|
|
351
|
+
}, []);
|
|
352
|
+
|
|
353
|
+
return (
|
|
354
|
+
<div className={clsx('flex flex-col flex-1 min-h-0 overflow-hidden', className)}>
|
|
355
|
+
{useInternalScroll && hideScrollbar && <style>{HIDE_SCROLLBAR_CSS}</style>}
|
|
356
|
+
<div
|
|
357
|
+
ref={setScrollContainerRef}
|
|
358
|
+
onScroll={useInternalScroll ? handleScroll : undefined}
|
|
359
|
+
className={
|
|
360
|
+
useInternalScroll
|
|
361
|
+
? clsx(
|
|
362
|
+
'flex-1 min-h-0 overflow-y-auto',
|
|
363
|
+
hideScrollbar && 'ym-hide-scrollbar',
|
|
364
|
+
scrollContainerClassName,
|
|
365
|
+
)
|
|
366
|
+
: (scrollContainerClassName ?? '')
|
|
367
|
+
}
|
|
368
|
+
>
|
|
369
|
+
<div ref={contentRef} className={clsx('w-full pb-4 pt-2 space-y-3 px-4', contentClassName ?? '')}>
|
|
370
|
+
{error && (
|
|
371
|
+
<div className="rounded-lg border border-red-200 bg-red-50 px-4 py-3 flex items-center justify-between gap-2">
|
|
372
|
+
<span className="text-sm text-red-800">{error}</span>
|
|
373
|
+
{onRetry && (
|
|
374
|
+
<button
|
|
375
|
+
type="button"
|
|
376
|
+
onClick={onRetry}
|
|
377
|
+
className="shrink-0 px-3 py-1.5 text-sm font-medium text-red-700 hover:bg-red-100 rounded-md transition-colors"
|
|
378
|
+
>
|
|
379
|
+
Retry
|
|
380
|
+
</button>
|
|
381
|
+
)}
|
|
382
|
+
</div>
|
|
383
|
+
)}
|
|
384
|
+
{isEmpty && (
|
|
385
|
+
<div className="px-4 py-8 text-center">
|
|
386
|
+
{renderEmptyState ? (
|
|
387
|
+
renderEmptyState({ inputToolbarProps })
|
|
388
|
+
) : (
|
|
389
|
+
<>
|
|
390
|
+
<div className="text-gray-500 text-lg font-medium mb-2">
|
|
391
|
+
Describe your idea and I'll help you create it step by step
|
|
392
|
+
</div>
|
|
393
|
+
<div className="text-gray-400 text-sm">
|
|
394
|
+
Start a new conversation by typing your message below
|
|
395
|
+
</div>
|
|
396
|
+
</>
|
|
397
|
+
)}
|
|
398
|
+
{shouldInlineToolbar && (
|
|
399
|
+
<div className="mt-6 text-left">{renderToolbar(inputToolbarProps)}</div>
|
|
400
|
+
)}
|
|
401
|
+
</div>
|
|
402
|
+
)}
|
|
403
|
+
|
|
404
|
+
{posts.map((post) => (
|
|
405
|
+
<div key={post.id} className={clsx('mb-3', postContainerClassName)}>
|
|
406
|
+
<MessageGroupComponent
|
|
407
|
+
messages={[post]}
|
|
408
|
+
currentUser={currentUserForGroup}
|
|
409
|
+
onOpen={onOpen}
|
|
410
|
+
onMessageClick={onMessageClick}
|
|
411
|
+
isDesktopView={isDesktopView}
|
|
412
|
+
isSmallScreen={isSmallScreen}
|
|
413
|
+
showAuthorName={showAvatar}
|
|
414
|
+
showAvatar={showAvatar}
|
|
415
|
+
showTimestamp={showDate}
|
|
416
|
+
sandboxErrors={sandboxErrors}
|
|
417
|
+
currentFiles={currentFiles}
|
|
418
|
+
onFixError={onFixError}
|
|
419
|
+
showCopyDownloadOnCodeBlock={showCopyDownloadOnCodeBlock}
|
|
420
|
+
/>
|
|
421
|
+
</div>
|
|
422
|
+
))}
|
|
423
|
+
|
|
424
|
+
{streamingContent?.trim() && (
|
|
425
|
+
<div className="flex justify-start">
|
|
426
|
+
<div
|
|
427
|
+
className={clsx(
|
|
428
|
+
'max-w-[85%] w-full rounded-2xl px-4 py-3 text-gray-900',
|
|
429
|
+
ASSISTANT_BUBBLE_BG,
|
|
430
|
+
showMessageBubbleBorder && `border ${BORDER_COLOR}`,
|
|
431
|
+
)}
|
|
432
|
+
>
|
|
433
|
+
<div className="min-w-0 text-[15px] leading-relaxed break-words whitespace-pre-wrap">
|
|
434
|
+
{renderStreamingContent
|
|
435
|
+
? renderStreamingContent(streamingContent)
|
|
436
|
+
: streamingContent}
|
|
437
|
+
</div>
|
|
438
|
+
</div>
|
|
439
|
+
</div>
|
|
440
|
+
)}
|
|
441
|
+
|
|
442
|
+
{isLoading && (
|
|
443
|
+
<div className="flex justify-start">
|
|
444
|
+
<div className="rounded-2xl px-4 py-3 bg-gray-100 border border-gray-200 flex items-center gap-2">
|
|
445
|
+
<div className="w-4 h-4 border-2 border-gray-300 border-t-gray-600 rounded-full animate-spin" />
|
|
446
|
+
<span className="text-sm text-gray-600">Thinking...</span>
|
|
447
|
+
</div>
|
|
448
|
+
</div>
|
|
449
|
+
)}
|
|
450
|
+
</div>
|
|
451
|
+
</div>
|
|
452
|
+
{shouldRenderBottomToolbar && (
|
|
453
|
+
<div className="border-t border-gray-200 bg-white p-3">{renderToolbar(inputToolbarProps)}</div>
|
|
454
|
+
)}
|
|
455
|
+
</div>
|
|
456
|
+
);
|
|
457
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { MessagesContainerUI } from './MessagesContainerUI';
|
|
2
|
+
export type { MessagesContainerUIProps } from './MessagesContainerUI';
|
|
3
|
+
export { PlanModeView, defaultPlanMessageToPost } from './PlanModeView';
|
|
4
|
+
export type { PlanModeViewProps, PlanModeViewInputToolbarProps } from './PlanModeView';
|
|
5
|
+
export { DefaultMessageGroup } from './DefaultMessageGroup';
|
|
6
|
+
export { DefaultInputToolBar } from './DefaultInputToolBar';
|
|
7
|
+
export type { DefaultInputToolBarProps } from './DefaultInputToolBar';
|
|
8
|
+
// Rich (markdown / code / widget-card) renderer, moved here from messenger-box so consumers can
|
|
9
|
+
// render rich chat without depending on @messenger-box/tailwind-ui-inbox. Inject as MessageGroupComponent.
|
|
10
|
+
export {
|
|
11
|
+
ModernMessageGroupComponent,
|
|
12
|
+
FormattedMessageContent,
|
|
13
|
+
CodeFormatter,
|
|
14
|
+
detectContentType,
|
|
15
|
+
} from './ModernMessageGroup';
|
|
16
|
+
export { renderModernStreamingContent } from './modernStreaming';
|
|
17
|
+
export { MessengerSlotFillNameEnum } from './messenger-slot-fill-name-enum';
|
|
18
|
+
export type { PlanMessage, CurrentUser, MessageGroupRenderProps, MessageGroupComponentType } from './types';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export enum MessengerSlotFillNameEnum {
|
|
2
|
+
INBOX_RIGHT_SIDEBAR = 'inbox-right-sidebar',
|
|
3
|
+
INBOX_WITH_AI_RIGHT_SIDEBAR = 'inbox-with-ai-right-sidebar',
|
|
4
|
+
CHAT_SETTINGS = 'chat-settings',
|
|
5
|
+
TOOLUSE_NAME = 'tooluse',
|
|
6
|
+
THINKING_NAME = 'thinking',
|
|
7
|
+
CODEBLOCK_NAME = 'codeblock',
|
|
8
|
+
AIASSISTANT_NAME = 'aiassistant',
|
|
9
|
+
SERVICE_NAME = 'service',
|
|
10
|
+
TRAVEL_NAME = 'travel',
|
|
11
|
+
MEDIA_NAME = 'media',
|
|
12
|
+
WORKFLOW_NAME = 'workflow',
|
|
13
|
+
CONNECTOR_NAME = 'connector',
|
|
14
|
+
GENERIC_NAME = 'generic',
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { CodeFormatter, detectContentType, FormattedMessageContent } from './ModernMessageGroup';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Renders in-flight streaming assistant content with the rich Modern renderer:
|
|
6
|
+
* raw-code content goes through {@link CodeFormatter}, everything else through
|
|
7
|
+
* {@link FormattedMessageContent} (markdown + embedded code blocks).
|
|
8
|
+
*
|
|
9
|
+
* Pass this as `MessagesContainerUI` / `PlanModeView`'s `renderStreamingContent` to keep
|
|
10
|
+
* markdown / code rendering while streaming (the base default is plain text).
|
|
11
|
+
*/
|
|
12
|
+
export function renderModernStreamingContent(content: string, showCopyDownloadOnCodeBlock = true): React.ReactNode {
|
|
13
|
+
return detectContentType(content) === 'code' ? (
|
|
14
|
+
<CodeFormatter content={content} showCopyDownload={showCopyDownloadOnCodeBlock} />
|
|
15
|
+
) : (
|
|
16
|
+
<FormattedMessageContent content={content} showCopyDownloadOnCodeBlock={showCopyDownloadOnCodeBlock} />
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Messages container UI – types for plan mode (chat / search / research).
|
|
5
|
+
* Messages can be a simple PlanMessage or a full post-like shape passed through
|
|
6
|
+
* to the injected message-group renderer.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface PlanMessage {
|
|
10
|
+
id: string;
|
|
11
|
+
role: 'user' | 'assistant' | 'system';
|
|
12
|
+
content: string;
|
|
13
|
+
createdAt?: Date | string;
|
|
14
|
+
/** Optional: pass through to the injected message-group renderer (propsConfiguration, props, files, etc.) */
|
|
15
|
+
propsConfiguration?: any;
|
|
16
|
+
props?: any;
|
|
17
|
+
files?: any;
|
|
18
|
+
author?: any;
|
|
19
|
+
type?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Props passed to the input toolbar render function. */
|
|
23
|
+
export interface PlanModeViewInputToolbarProps {
|
|
24
|
+
value: string;
|
|
25
|
+
onChange: (text: string) => void;
|
|
26
|
+
onSend: () => void;
|
|
27
|
+
disabled?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface CurrentUser {
|
|
31
|
+
id: string;
|
|
32
|
+
name?: string;
|
|
33
|
+
avatar?: string;
|
|
34
|
+
/** Optional: full author shape for the message-group renderer (givenName, familyName, username, etc.) */
|
|
35
|
+
givenName?: string;
|
|
36
|
+
familyName?: string;
|
|
37
|
+
fullName?: string;
|
|
38
|
+
username?: string;
|
|
39
|
+
email?: string;
|
|
40
|
+
picture?: string | null;
|
|
41
|
+
alias?: any[];
|
|
42
|
+
tokens?: any[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Props the base passes to the injected message-group renderer (`MessageGroupComponent`).
|
|
47
|
+
* Mirrors the shape consumed by richer renderers (e.g. messenger-box's
|
|
48
|
+
* `ModernMessageGroupComponent`) so they can be dropped in directly.
|
|
49
|
+
*/
|
|
50
|
+
export interface MessageGroupRenderProps {
|
|
51
|
+
/** One post group to render (the base passes a single mapped post per group). */
|
|
52
|
+
messages: any[];
|
|
53
|
+
currentUser: any;
|
|
54
|
+
onOpen?: (element?: any) => void;
|
|
55
|
+
onMessageClick?: (msg: any) => void;
|
|
56
|
+
isDesktopView?: boolean;
|
|
57
|
+
isSmallScreen?: boolean;
|
|
58
|
+
showAuthorName?: boolean;
|
|
59
|
+
showAvatar?: boolean;
|
|
60
|
+
showTimestamp?: boolean;
|
|
61
|
+
sandboxErrors?: any[];
|
|
62
|
+
currentFiles?: Record<string, string>;
|
|
63
|
+
onFixError?: (errorMessage: string) => Promise<void>;
|
|
64
|
+
showCopyDownloadOnCodeBlock?: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The injectable message-group renderer. Defaults to a plain-text bubble renderer. */
|
|
68
|
+
export type MessageGroupComponentType = React.ComponentType<MessageGroupRenderProps>;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './components';
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"resolveJsonModule": true,
|
|
5
|
+
"skipLibCheck": true,
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"outDir": "lib",
|
|
8
|
+
"declarationDir": "lib",
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"declarationMap": true,
|
|
11
|
+
"jsx": "react-jsx",
|
|
12
|
+
"moduleResolution": "bundler",
|
|
13
|
+
"allowImportingTsExtensions": false,
|
|
14
|
+
"types": ["@types/node", "@types/jest", "../../../typings", "vite/client"]
|
|
15
|
+
},
|
|
16
|
+
"exclude": [
|
|
17
|
+
"node_modules",
|
|
18
|
+
"lib",
|
|
19
|
+
"dist",
|
|
20
|
+
"webpack.config.js",
|
|
21
|
+
"rollup.config.js",
|
|
22
|
+
"src/**/__tests__/**",
|
|
23
|
+
"src/**/*.test.ts",
|
|
24
|
+
"src/**/*.test.tsx"
|
|
25
|
+
],
|
|
26
|
+
"include": ["src"]
|
|
27
|
+
}
|