@schandlergarcia/sf-web-components 1.9.37 → 1.9.39
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/package.json +4 -1
- package/scripts/postinstall.mjs +116 -65
- package/src/components/library/cards/ActionList.jsx +38 -0
- package/src/components/library/cards/ActivityCard.jsx +56 -0
- package/src/components/library/cards/BaseCard.jsx +109 -0
- package/src/components/library/cards/CalloutCard.jsx +37 -0
- package/src/components/library/cards/ChartCard.jsx +105 -0
- package/src/components/library/cards/FeedPanel.jsx +39 -0
- package/src/components/library/cards/ListCard.jsx +193 -0
- package/src/components/library/cards/MetricCard.jsx +109 -0
- package/src/components/library/cards/MetricsStrip.jsx +78 -0
- package/src/components/library/cards/SectionCard.jsx +83 -0
- package/src/components/library/cards/SemanticMetricCard.jsx +52 -0
- package/src/components/library/cards/SemanticMetricCardWithLoading.jsx +23 -0
- package/src/components/library/cards/SemanticTableCard.jsx +48 -0
- package/src/components/library/cards/SemanticTableCardWithLoading.jsx +22 -0
- package/src/components/library/cards/StatusCard.jsx +220 -0
- package/src/components/library/cards/TableCard.jsx +337 -0
- package/src/components/library/cards/WidgetCard.jsx +90 -0
- package/src/components/library/charts/D3Chart.jsx +109 -0
- package/src/components/library/charts/D3ChartTemplates.jsx +126 -0
- package/src/components/library/charts/GeoMap.jsx +293 -0
- package/src/components/library/chat/ChatBar.jsx +256 -0
- package/src/components/library/chat/ChatInput.jsx +89 -0
- package/src/components/library/chat/ChatMessage.jsx +178 -0
- package/src/components/library/chat/ChatMessageList.jsx +73 -0
- package/src/components/library/chat/ChatPanel.jsx +97 -0
- package/src/components/library/chat/ChatSuggestions.jsx +28 -0
- package/src/components/library/chat/ChatToolCall.jsx +100 -0
- package/src/components/library/chat/ChatTypingIndicator.jsx +23 -0
- package/src/components/library/chat/ChatWelcome.jsx +43 -0
- package/src/components/library/chat/index.jsx +10 -0
- package/src/components/library/chat/useChatState.jsx +130 -0
- package/src/components/library/data/DataModeProvider.jsx +67 -0
- package/src/components/library/data/DataModeToggle.jsx +36 -0
- package/src/components/library/data/chartDataProvider.jsx +61 -0
- package/src/components/library/data/filterUtils.jsx +141 -0
- package/src/components/library/data/useDataSource.jsx +33 -0
- package/src/components/library/data/usePageFilters.jsx +99 -0
- package/src/components/library/filters/FilterBar.jsx +95 -0
- package/src/components/library/filters/SearchFilter.jsx +36 -0
- package/src/components/library/filters/SelectFilter.jsx +55 -0
- package/src/components/library/filters/ToggleFilter.jsx +52 -0
- package/src/components/library/filters/index.jsx +4 -0
- package/src/components/library/forms/FormField.jsx +291 -0
- package/src/components/library/forms/FormModal.jsx +201 -0
- package/src/components/library/forms/FormRenderer.jsx +46 -0
- package/src/components/library/forms/FormSection.jsx +69 -0
- package/src/components/library/forms/index.jsx +5 -0
- package/src/components/library/forms/useFormState.jsx +165 -0
- package/src/components/library/heroui/Accordion.jsx +26 -0
- package/src/components/library/heroui/Alert.jsx +8 -0
- package/src/components/library/heroui/Badge.jsx +8 -0
- package/src/components/library/heroui/Breadcrumbs.jsx +22 -0
- package/src/components/library/heroui/Button.jsx +58 -0
- package/src/components/library/heroui/Card.jsx +8 -0
- package/src/components/library/heroui/Collapsible.jsx +42 -0
- package/src/components/library/heroui/DatePicker.jsx +34 -0
- package/src/components/library/heroui/Dialog.jsx +37 -0
- package/src/components/library/heroui/Drawer.jsx +32 -0
- package/src/components/library/heroui/Dropdown.jsx +28 -0
- package/src/components/library/heroui/Field.jsx +51 -0
- package/src/components/library/heroui/Input.jsx +6 -0
- package/src/components/library/heroui/Kbd.jsx +8 -0
- package/src/components/library/heroui/Meter.jsx +8 -0
- package/src/components/library/heroui/Modal.jsx +32 -0
- package/src/components/library/heroui/Pagination.jsx +8 -0
- package/src/components/library/heroui/Popover.jsx +64 -0
- package/src/components/library/heroui/ProgressBar.jsx +8 -0
- package/src/components/library/heroui/ProgressCircle.jsx +8 -0
- package/src/components/library/heroui/ScrollShadow.jsx +8 -0
- package/src/components/library/heroui/Select.jsx +37 -0
- package/src/components/library/heroui/Separator.jsx +8 -0
- package/src/components/library/heroui/Skeleton.jsx +8 -0
- package/src/components/library/heroui/Tabs.jsx +26 -0
- package/src/components/library/heroui/Toast.jsx +25 -0
- package/src/components/library/heroui/Toggle.jsx +14 -0
- package/src/components/library/heroui/Tooltip.jsx +21 -0
- package/src/components/library/index.jsx +146 -0
- package/src/components/library/layout/PageContainer.jsx +11 -0
- package/src/components/library/skeletons/CardSkeleton.jsx +30 -0
- package/src/components/library/theme/AppThemeProvider.jsx +67 -0
- package/src/components/library/theme/tokens.jsx +72 -0
- package/src/components/library/ui/Alert.jsx +80 -0
- package/src/components/library/ui/Avatar.jsx +44 -0
- package/src/components/library/ui/BreadcrumbExtras.tsx +120 -0
- package/src/components/library/ui/Button.jsx +61 -0
- package/src/components/library/ui/Card.jsx +117 -0
- package/src/components/library/ui/Checkbox.jsx +17 -0
- package/src/components/library/ui/Chip.jsx +38 -0
- package/src/components/library/ui/Collapsible.tsx +31 -0
- package/src/components/library/ui/Container.jsx +56 -0
- package/src/components/library/ui/DatePicker.tsx +34 -0
- package/src/components/library/ui/Dialog.tsx +141 -0
- package/src/components/library/ui/EmptyState.jsx +46 -0
- package/src/components/library/ui/Field.tsx +82 -0
- package/src/components/library/ui/FieldGroup.jsx +17 -0
- package/src/components/library/ui/Input.jsx +21 -0
- package/src/components/library/ui/Label.jsx +22 -0
- package/src/components/library/ui/PaginationExtras.tsx +142 -0
- package/src/components/library/ui/Popover.tsx +39 -0
- package/src/components/library/ui/Select.tsx +113 -0
- package/src/components/library/ui/Spinner.d.ts +10 -0
- package/src/components/library/ui/Spinner.jsx +64 -0
- package/src/components/library/ui/Text.jsx +46 -0
- package/src/components/library/ui/Toggle.jsx +42 -0
- package/src/components/workspace/ComponentRegistry.jsx +297 -0
- package/src/lib/index.ts +1 -0
- package/src/lib/utils.ts +6 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import React, { useState, useRef, useEffect, useCallback } from "react";
|
|
2
|
+
import { createPortal } from "react-dom";
|
|
3
|
+
import { AnimatePresence, motion } from "framer-motion";
|
|
4
|
+
import {
|
|
5
|
+
SparklesIcon,
|
|
6
|
+
XMarkIcon,
|
|
7
|
+
TrashIcon,
|
|
8
|
+
ArrowTopRightOnSquareIcon,
|
|
9
|
+
} from "@heroicons/react/24/outline";
|
|
10
|
+
import ChatMessageList from "./ChatMessageList";
|
|
11
|
+
import ChatInput from "./ChatInput";
|
|
12
|
+
import useChatState from "./useChatState";
|
|
13
|
+
|
|
14
|
+
const BACKDROP_VARIANTS = {
|
|
15
|
+
hidden: { opacity: 0 },
|
|
16
|
+
visible: { opacity: 1, transition: { duration: 0.15 } },
|
|
17
|
+
exit: { opacity: 0, transition: { duration: 0.12 } },
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const PANEL_VARIANTS = {
|
|
21
|
+
hidden: { opacity: 0, y: 20, scale: 0.97 },
|
|
22
|
+
visible: {
|
|
23
|
+
opacity: 1,
|
|
24
|
+
y: 0,
|
|
25
|
+
scale: 1,
|
|
26
|
+
transition: { type: "spring", damping: 28, stiffness: 380 },
|
|
27
|
+
},
|
|
28
|
+
exit: { opacity: 0, y: 10, scale: 0.97, transition: { duration: 0.12 } },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Command-palette style AI chat bar.
|
|
33
|
+
*
|
|
34
|
+
* Collapsed: a slim, clickable input strip with sparkle icon and ⌘K hint.
|
|
35
|
+
* Expanded: a centered floating overlay (portal) with backdrop, full chat,
|
|
36
|
+
* suggestions, and message history. No layout shift.
|
|
37
|
+
*
|
|
38
|
+
* Activate: click the bar, press ⌘K / Ctrl+K, or click a suggestion chip.
|
|
39
|
+
* Dismiss: Escape or click the backdrop.
|
|
40
|
+
*/
|
|
41
|
+
export default function ChatBar({
|
|
42
|
+
onSend,
|
|
43
|
+
suggestions = [],
|
|
44
|
+
placeholder = "Ask a question\u2026",
|
|
45
|
+
title = "AI Assistant",
|
|
46
|
+
initialMessages = [],
|
|
47
|
+
className = "",
|
|
48
|
+
panelHeight,
|
|
49
|
+
renderAvatar,
|
|
50
|
+
onOpenInTab,
|
|
51
|
+
}) {
|
|
52
|
+
const [open, setOpen] = useState(false);
|
|
53
|
+
const [portalVisible, setPortalVisible] = useState(false);
|
|
54
|
+
const panelRef = useRef(null);
|
|
55
|
+
const chat = useChatState({ initialMessages, onSend });
|
|
56
|
+
const isEmpty = chat.messages.length === 0;
|
|
57
|
+
|
|
58
|
+
const close = useCallback(() => setOpen(false), []);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (open) setPortalVisible(true);
|
|
62
|
+
}, [open]);
|
|
63
|
+
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
function onKey(e) {
|
|
66
|
+
if ((e.metaKey || e.ctrlKey) && e.key === "k") {
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
setOpen((prev) => !prev);
|
|
69
|
+
}
|
|
70
|
+
if (e.key === "Escape" && open) close();
|
|
71
|
+
}
|
|
72
|
+
document.addEventListener("keydown", onKey);
|
|
73
|
+
return () => document.removeEventListener("keydown", onKey);
|
|
74
|
+
}, [open, close]);
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (open) document.body.style.overflow = "hidden";
|
|
78
|
+
else document.body.style.overflow = "";
|
|
79
|
+
return () => { document.body.style.overflow = ""; };
|
|
80
|
+
}, [open]);
|
|
81
|
+
|
|
82
|
+
function handleSend(content) {
|
|
83
|
+
if (!open) setOpen(true);
|
|
84
|
+
chat.sendMessage(content);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const [shortcutLabel, setShortcutLabel] = useState("⌘K");
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
setShortcutLabel(/Mac|iPhone|iPad/.test(navigator.userAgent) ? "⌘K" : "Ctrl K");
|
|
90
|
+
}, []);
|
|
91
|
+
|
|
92
|
+
const overlay =
|
|
93
|
+
typeof document !== "undefined" && portalVisible
|
|
94
|
+
? createPortal(
|
|
95
|
+
<AnimatePresence onExitComplete={() => setPortalVisible(false)}>
|
|
96
|
+
{open && (
|
|
97
|
+
<>
|
|
98
|
+
{/* Backdrop */}
|
|
99
|
+
<motion.div
|
|
100
|
+
variants={BACKDROP_VARIANTS}
|
|
101
|
+
initial="hidden"
|
|
102
|
+
animate="visible"
|
|
103
|
+
exit="exit"
|
|
104
|
+
className="fixed inset-0 z-50 bg-black/30 backdrop-blur-sm"
|
|
105
|
+
onClick={close}
|
|
106
|
+
aria-hidden="true"
|
|
107
|
+
/>
|
|
108
|
+
|
|
109
|
+
{/* Centered panel */}
|
|
110
|
+
<motion.div
|
|
111
|
+
ref={panelRef}
|
|
112
|
+
variants={PANEL_VARIANTS}
|
|
113
|
+
initial="hidden"
|
|
114
|
+
animate="visible"
|
|
115
|
+
exit="exit"
|
|
116
|
+
className="fixed inset-x-0 top-[10vh] z-50 mx-auto flex w-full max-w-4xl flex-col overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-2xl dark:border-slate-700 dark:bg-slate-900"
|
|
117
|
+
style={{ maxHeight: "75vh" }}
|
|
118
|
+
>
|
|
119
|
+
{/* Header */}
|
|
120
|
+
<div className="flex items-center justify-between border-b border-slate-100 px-4 py-2.5 dark:border-slate-800">
|
|
121
|
+
<div className="flex items-center gap-2">
|
|
122
|
+
<SparklesIcon className="h-4 w-4 text-brand-500" />
|
|
123
|
+
<h3 className="text-sm font-semibold text-slate-900 dark:text-slate-50">
|
|
124
|
+
{title}
|
|
125
|
+
</h3>
|
|
126
|
+
</div>
|
|
127
|
+
<div className="flex items-center gap-1">
|
|
128
|
+
{onOpenInTab ? (
|
|
129
|
+
<button
|
|
130
|
+
type="button"
|
|
131
|
+
onClick={() => {
|
|
132
|
+
setOpen(false);
|
|
133
|
+
setPortalVisible(false);
|
|
134
|
+
document.body.style.overflow = "";
|
|
135
|
+
onOpenInTab(chat.messages);
|
|
136
|
+
}}
|
|
137
|
+
className="rounded p-1 text-slate-400 transition hover:bg-slate-100 hover:text-slate-600 dark:text-slate-500 dark:hover:bg-slate-800 dark:hover:text-slate-300"
|
|
138
|
+
aria-label="Open in new tab"
|
|
139
|
+
title="Open in new tab"
|
|
140
|
+
>
|
|
141
|
+
<ArrowTopRightOnSquareIcon className="h-4 w-4" />
|
|
142
|
+
</button>
|
|
143
|
+
) : null}
|
|
144
|
+
{chat.messages.length > 0 ? (
|
|
145
|
+
<button
|
|
146
|
+
type="button"
|
|
147
|
+
onClick={chat.clearMessages}
|
|
148
|
+
className="rounded p-1 text-slate-400 transition hover:bg-slate-100 hover:text-slate-600 dark:text-slate-500 dark:hover:bg-slate-800 dark:hover:text-slate-300"
|
|
149
|
+
aria-label="Clear chat"
|
|
150
|
+
>
|
|
151
|
+
<TrashIcon className="h-4 w-4" />
|
|
152
|
+
</button>
|
|
153
|
+
) : null}
|
|
154
|
+
<button
|
|
155
|
+
type="button"
|
|
156
|
+
onClick={close}
|
|
157
|
+
className="rounded p-1 text-slate-400 transition hover:bg-slate-100 hover:text-slate-600 dark:text-slate-500 dark:hover:bg-slate-800 dark:hover:text-slate-300"
|
|
158
|
+
aria-label="Close"
|
|
159
|
+
>
|
|
160
|
+
<XMarkIcon className="h-4 w-4" />
|
|
161
|
+
</button>
|
|
162
|
+
<kbd className="ml-1 hidden rounded border border-slate-200 bg-slate-50 px-1.5 py-0.5 text-[10px] font-medium text-slate-400 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-500 sm:inline-block">
|
|
163
|
+
esc
|
|
164
|
+
</kbd>
|
|
165
|
+
</div>
|
|
166
|
+
</div>
|
|
167
|
+
|
|
168
|
+
{/* Body — messages or empty state */}
|
|
169
|
+
<div className="min-h-0 flex-1">
|
|
170
|
+
{isEmpty ? (
|
|
171
|
+
<div className="flex h-full min-h-[200px] flex-col items-center justify-center gap-4 px-6 py-8">
|
|
172
|
+
<div className="flex h-10 w-10 items-center justify-center rounded-full bg-brand-50 dark:bg-brand-950/30">
|
|
173
|
+
<SparklesIcon className="h-5 w-5 text-brand-500" />
|
|
174
|
+
</div>
|
|
175
|
+
<p className="text-sm text-slate-500 dark:text-slate-400">
|
|
176
|
+
Ask me anything about your data.
|
|
177
|
+
</p>
|
|
178
|
+
{suggestions.length > 0 ? (
|
|
179
|
+
<div className="flex flex-wrap justify-center gap-2">
|
|
180
|
+
{suggestions.map((text) => (
|
|
181
|
+
<button
|
|
182
|
+
key={text}
|
|
183
|
+
type="button"
|
|
184
|
+
onClick={() => handleSend(text)}
|
|
185
|
+
className="inline-flex items-center gap-1.5 rounded-full border border-slate-200 bg-white px-3 py-1.5 text-xs font-medium text-slate-600 shadow-sm transition hover:border-brand-300 hover:bg-brand-50 hover:text-brand-700 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-300 dark:hover:border-brand-700 dark:hover:bg-brand-950/30 dark:hover:text-brand-300"
|
|
186
|
+
>
|
|
187
|
+
<SparklesIcon
|
|
188
|
+
className="h-3 w-3"
|
|
189
|
+
aria-hidden="true"
|
|
190
|
+
/>
|
|
191
|
+
{text}
|
|
192
|
+
</button>
|
|
193
|
+
))}
|
|
194
|
+
</div>
|
|
195
|
+
) : null}
|
|
196
|
+
</div>
|
|
197
|
+
) : (
|
|
198
|
+
<ChatMessageList
|
|
199
|
+
messages={chat.messages}
|
|
200
|
+
isLoading={chat.isLoading}
|
|
201
|
+
isStreaming={chat.isStreaming}
|
|
202
|
+
suggestions={suggestions}
|
|
203
|
+
onSuggestion={(text) => chat.sendMessage(text)}
|
|
204
|
+
renderAvatar={renderAvatar}
|
|
205
|
+
/>
|
|
206
|
+
)}
|
|
207
|
+
</div>
|
|
208
|
+
|
|
209
|
+
{/* Input */}
|
|
210
|
+
<div className="border-t border-slate-100 p-3 dark:border-slate-800">
|
|
211
|
+
<ChatInput
|
|
212
|
+
onSend={(content) => chat.sendMessage(content)}
|
|
213
|
+
disabled={chat.isLoading}
|
|
214
|
+
isLoading={chat.isLoading}
|
|
215
|
+
placeholder={placeholder}
|
|
216
|
+
/>
|
|
217
|
+
</div>
|
|
218
|
+
</motion.div>
|
|
219
|
+
</>
|
|
220
|
+
)}
|
|
221
|
+
</AnimatePresence>,
|
|
222
|
+
document.body
|
|
223
|
+
)
|
|
224
|
+
: null;
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<>
|
|
228
|
+
{overlay}
|
|
229
|
+
|
|
230
|
+
{/* Trigger bar */}
|
|
231
|
+
<button
|
|
232
|
+
type="button"
|
|
233
|
+
onClick={() => setOpen(true)}
|
|
234
|
+
className={[
|
|
235
|
+
"group flex w-full items-center gap-3 rounded-xl border border-slate-200 bg-white px-4 py-2.5 text-left shadow-sm transition-all hover:border-brand-300 hover:shadow-md focus:outline-none focus:ring-2 focus:ring-brand-500/20 dark:border-slate-800 dark:bg-slate-900 dark:hover:border-brand-700",
|
|
236
|
+
className,
|
|
237
|
+
]
|
|
238
|
+
.filter(Boolean)
|
|
239
|
+
.join(" ")}
|
|
240
|
+
>
|
|
241
|
+
<SparklesIcon className="h-5 w-5 shrink-0 text-brand-500" />
|
|
242
|
+
<span className="flex-1 text-sm text-slate-400 dark:text-slate-500">
|
|
243
|
+
{placeholder}
|
|
244
|
+
</span>
|
|
245
|
+
{chat.messages.length > 0 ? (
|
|
246
|
+
<span className="rounded-full bg-brand-50 px-2 py-0.5 text-xs font-medium text-brand-600 dark:bg-brand-950/30 dark:text-brand-400">
|
|
247
|
+
{chat.messages.length} msgs
|
|
248
|
+
</span>
|
|
249
|
+
) : null}
|
|
250
|
+
<kbd className="hidden rounded border border-slate-200 bg-slate-50 px-1.5 py-0.5 text-[10px] font-medium text-slate-400 group-hover:border-brand-200 group-hover:text-brand-500 dark:border-slate-700 dark:bg-slate-800 dark:text-slate-500 sm:inline-block">
|
|
251
|
+
{shortcutLabel}
|
|
252
|
+
</kbd>
|
|
253
|
+
</button>
|
|
254
|
+
</>
|
|
255
|
+
);
|
|
256
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import React, { useState, useRef, useEffect } from "react";
|
|
2
|
+
import { PaperAirplaneIcon, StopCircleIcon } from "@heroicons/react/24/solid";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Chat input with auto-resize textarea, Send button, and keyboard shortcuts.
|
|
6
|
+
* Enter sends, Shift+Enter inserts newline.
|
|
7
|
+
*
|
|
8
|
+
* @param {Function} onSend — (content: string) => void
|
|
9
|
+
* @param {boolean} disabled — disable input while agent is processing
|
|
10
|
+
* @param {boolean} isLoading — show stop button instead of send
|
|
11
|
+
* @param {Function} onStop — optional: called when stop is clicked
|
|
12
|
+
* @param {string} placeholder
|
|
13
|
+
* @param {number} maxRows — max visible rows before scroll (default 6)
|
|
14
|
+
*/
|
|
15
|
+
export default function ChatInput({
|
|
16
|
+
onSend,
|
|
17
|
+
disabled = false,
|
|
18
|
+
isLoading = false,
|
|
19
|
+
onStop,
|
|
20
|
+
placeholder = "Type a message…",
|
|
21
|
+
maxRows = 6,
|
|
22
|
+
}) {
|
|
23
|
+
const [value, setValue] = useState("");
|
|
24
|
+
const textareaRef = useRef(null);
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const ta = textareaRef.current;
|
|
28
|
+
if (!ta) return;
|
|
29
|
+
ta.style.height = "auto";
|
|
30
|
+
const lineHeight = 22;
|
|
31
|
+
const maxHeight = lineHeight * maxRows;
|
|
32
|
+
ta.style.height = `${Math.min(ta.scrollHeight, maxHeight)}px`;
|
|
33
|
+
}, [value, maxRows]);
|
|
34
|
+
|
|
35
|
+
function handleSend() {
|
|
36
|
+
if (!value.trim() || disabled) return;
|
|
37
|
+
onSend?.(value);
|
|
38
|
+
setValue("");
|
|
39
|
+
if (textareaRef.current) {
|
|
40
|
+
textareaRef.current.style.height = "auto";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function handleKeyDown(e) {
|
|
45
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
46
|
+
e.preventDefault();
|
|
47
|
+
handleSend();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const canSend = value.trim().length > 0 && !disabled;
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div className="flex items-end gap-2 rounded-xl border border-slate-200 bg-white p-2 shadow-sm transition-colors focus-within:border-brand-300 focus-within:ring-2 focus-within:ring-brand-500/20 dark:border-slate-700 dark:bg-slate-900 dark:focus-within:border-brand-700 dark:focus-within:ring-brand-400/20">
|
|
55
|
+
<textarea
|
|
56
|
+
ref={textareaRef}
|
|
57
|
+
value={value}
|
|
58
|
+
onChange={(e) => setValue(e.target.value)}
|
|
59
|
+
onKeyDown={handleKeyDown}
|
|
60
|
+
placeholder={placeholder}
|
|
61
|
+
disabled={disabled}
|
|
62
|
+
rows={1}
|
|
63
|
+
className="max-h-36 min-h-[22px] flex-1 resize-none bg-transparent px-2 py-1.5 text-sm text-slate-900 placeholder:text-slate-400 focus:outline-none disabled:cursor-not-allowed disabled:opacity-60 dark:text-slate-50 dark:placeholder:text-slate-500"
|
|
64
|
+
aria-label="Chat message input"
|
|
65
|
+
/>
|
|
66
|
+
|
|
67
|
+
{isLoading && onStop ? (
|
|
68
|
+
<button
|
|
69
|
+
type="button"
|
|
70
|
+
onClick={onStop}
|
|
71
|
+
className="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg text-slate-400 transition hover:bg-slate-100 hover:text-slate-600 dark:text-slate-500 dark:hover:bg-slate-800 dark:hover:text-slate-300"
|
|
72
|
+
aria-label="Stop generating"
|
|
73
|
+
>
|
|
74
|
+
<StopCircleIcon className="h-5 w-5" />
|
|
75
|
+
</button>
|
|
76
|
+
) : (
|
|
77
|
+
<button
|
|
78
|
+
type="button"
|
|
79
|
+
onClick={handleSend}
|
|
80
|
+
disabled={!canSend}
|
|
81
|
+
className="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-brand-600 text-white transition hover:bg-brand-500 disabled:cursor-not-allowed disabled:opacity-40 dark:bg-brand-500 dark:hover:bg-brand-400"
|
|
82
|
+
aria-label="Send message"
|
|
83
|
+
>
|
|
84
|
+
<PaperAirplaneIcon className="h-4 w-4" />
|
|
85
|
+
</button>
|
|
86
|
+
)}
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { renderSchemaComponent } from "@/components/workspace/ComponentRegistry";
|
|
3
|
+
import ChatToolCall from "./ChatToolCall";
|
|
4
|
+
import { UserCircleIcon, CpuChipIcon } from "@heroicons/react/24/solid";
|
|
5
|
+
|
|
6
|
+
function cx(...classes) {
|
|
7
|
+
return classes.filter(Boolean).join(" ");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Lightweight inline formatter for assistant messages.
|
|
12
|
+
* Handles code blocks, inline code, bold, italic, and line breaks.
|
|
13
|
+
*/
|
|
14
|
+
function formatContent(text) {
|
|
15
|
+
if (!text) return null;
|
|
16
|
+
const parts = [];
|
|
17
|
+
let key = 0;
|
|
18
|
+
|
|
19
|
+
const codeBlockRegex = /```(\w*)\n?([\s\S]*?)```/g;
|
|
20
|
+
let lastIndex = 0;
|
|
21
|
+
let match;
|
|
22
|
+
|
|
23
|
+
while ((match = codeBlockRegex.exec(text)) !== null) {
|
|
24
|
+
if (match.index > lastIndex) {
|
|
25
|
+
parts.push(
|
|
26
|
+
<span key={key++}>{formatInline(text.slice(lastIndex, match.index))}</span>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
parts.push(
|
|
30
|
+
<pre
|
|
31
|
+
key={key++}
|
|
32
|
+
className="my-2 overflow-x-auto rounded-lg border border-slate-200 bg-slate-50 p-3 text-xs leading-relaxed dark:border-slate-700 dark:bg-slate-800/50"
|
|
33
|
+
>
|
|
34
|
+
<code>{match[2]}</code>
|
|
35
|
+
</pre>
|
|
36
|
+
);
|
|
37
|
+
lastIndex = match.index + match[0].length;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (lastIndex < text.length) {
|
|
41
|
+
parts.push(
|
|
42
|
+
<span key={key++}>{formatInline(text.slice(lastIndex))}</span>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return parts;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function formatInline(text) {
|
|
50
|
+
const tokens = text.split(/(`[^`]+`|\*\*[^*]+\*\*|\*[^*]+\*)/g);
|
|
51
|
+
return tokens.map((token, i) => {
|
|
52
|
+
if (token.startsWith("**") && token.endsWith("**")) {
|
|
53
|
+
return <strong key={i} className="font-semibold">{token.slice(2, -2)}</strong>;
|
|
54
|
+
}
|
|
55
|
+
if (token.startsWith("*") && token.endsWith("*")) {
|
|
56
|
+
return <em key={i}>{token.slice(1, -1)}</em>;
|
|
57
|
+
}
|
|
58
|
+
if (token.startsWith("`") && token.endsWith("`")) {
|
|
59
|
+
return (
|
|
60
|
+
<code
|
|
61
|
+
key={i}
|
|
62
|
+
className="rounded bg-slate-100 px-1 py-0.5 text-xs font-mono dark:bg-slate-800"
|
|
63
|
+
>
|
|
64
|
+
{token.slice(1, -1)}
|
|
65
|
+
</code>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return token.split("\n").map((line, j, arr) => (
|
|
69
|
+
<React.Fragment key={`${i}-${j}`}>
|
|
70
|
+
{line}
|
|
71
|
+
{j < arr.length - 1 ? <br /> : null}
|
|
72
|
+
</React.Fragment>
|
|
73
|
+
));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Renders a single chat message.
|
|
79
|
+
*
|
|
80
|
+
* @param {Object} message — { id, role, content, components?, toolCalls?, isError?, isStreaming?, timestamp? }
|
|
81
|
+
* @param {React.ReactNode} avatar — custom avatar override
|
|
82
|
+
*/
|
|
83
|
+
export default function ChatMessage({ message, avatar }) {
|
|
84
|
+
const isUser = message.role === "user";
|
|
85
|
+
const isSystem = message.role === "system";
|
|
86
|
+
const isAssistant = message.role === "assistant";
|
|
87
|
+
|
|
88
|
+
const defaultAvatar = isUser ? (
|
|
89
|
+
<UserCircleIcon className="h-7 w-7 text-slate-400 dark:text-slate-500" />
|
|
90
|
+
) : (
|
|
91
|
+
<div className="flex h-7 w-7 items-center justify-center rounded-lg bg-brand-100 dark:bg-brand-900/40">
|
|
92
|
+
<CpuChipIcon className="h-4 w-4 text-brand-600 dark:text-brand-400" />
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
if (isSystem) {
|
|
97
|
+
return (
|
|
98
|
+
<div
|
|
99
|
+
className={cx(
|
|
100
|
+
"mx-auto max-w-lg rounded-lg px-4 py-2 text-center text-xs",
|
|
101
|
+
message.isError
|
|
102
|
+
? "bg-red-50 text-red-600 dark:bg-red-950/30 dark:text-red-400"
|
|
103
|
+
: "text-slate-400 dark:text-slate-500"
|
|
104
|
+
)}
|
|
105
|
+
>
|
|
106
|
+
{message.content}
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return (
|
|
112
|
+
<div
|
|
113
|
+
className={cx(
|
|
114
|
+
"flex gap-3",
|
|
115
|
+
isUser ? "flex-row-reverse" : "flex-row"
|
|
116
|
+
)}
|
|
117
|
+
>
|
|
118
|
+
{/* Avatar */}
|
|
119
|
+
<div className="shrink-0 pt-0.5">{avatar ?? defaultAvatar}</div>
|
|
120
|
+
|
|
121
|
+
{/* Bubble */}
|
|
122
|
+
<div
|
|
123
|
+
className={cx(
|
|
124
|
+
"max-w-[80%] space-y-2",
|
|
125
|
+
isUser ? "items-end" : "items-start"
|
|
126
|
+
)}
|
|
127
|
+
>
|
|
128
|
+
{/* Text content */}
|
|
129
|
+
{message.content ? (
|
|
130
|
+
<div
|
|
131
|
+
className={cx(
|
|
132
|
+
"rounded-2xl px-4 py-2.5 text-sm leading-relaxed",
|
|
133
|
+
isUser
|
|
134
|
+
? "rounded-tr-md bg-brand-600 text-white dark:bg-brand-500"
|
|
135
|
+
: "rounded-tl-md bg-slate-100 text-slate-800 dark:bg-slate-800 dark:text-slate-100",
|
|
136
|
+
message.isStreaming && "animate-pulse"
|
|
137
|
+
)}
|
|
138
|
+
>
|
|
139
|
+
{isAssistant ? formatContent(message.content) : message.content}
|
|
140
|
+
</div>
|
|
141
|
+
) : null}
|
|
142
|
+
|
|
143
|
+
{/* Tool calls */}
|
|
144
|
+
{message.toolCalls?.length ? (
|
|
145
|
+
<div className="space-y-1.5 pl-1">
|
|
146
|
+
{message.toolCalls.map((tc, idx) => (
|
|
147
|
+
<ChatToolCall key={tc.id ?? idx} toolCall={tc} />
|
|
148
|
+
))}
|
|
149
|
+
</div>
|
|
150
|
+
) : null}
|
|
151
|
+
|
|
152
|
+
{/* Inline components — render real command center components */}
|
|
153
|
+
{message.components?.length ? (
|
|
154
|
+
<div className="w-full space-y-3 pt-1">
|
|
155
|
+
{message.components.map((comp, idx) =>
|
|
156
|
+
renderSchemaComponent(comp, idx)
|
|
157
|
+
)}
|
|
158
|
+
</div>
|
|
159
|
+
) : null}
|
|
160
|
+
|
|
161
|
+
{/* Timestamp */}
|
|
162
|
+
{message.timestamp ? (
|
|
163
|
+
<div
|
|
164
|
+
className={cx(
|
|
165
|
+
"px-1 text-[10px] text-slate-400 dark:text-slate-500",
|
|
166
|
+
isUser ? "text-right" : "text-left"
|
|
167
|
+
)}
|
|
168
|
+
>
|
|
169
|
+
{new Date(message.timestamp).toLocaleTimeString([], {
|
|
170
|
+
hour: "2-digit",
|
|
171
|
+
minute: "2-digit",
|
|
172
|
+
})}
|
|
173
|
+
</div>
|
|
174
|
+
) : null}
|
|
175
|
+
</div>
|
|
176
|
+
</div>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import React, { useRef, useEffect } from "react";
|
|
2
|
+
import ChatMessage from "./ChatMessage";
|
|
3
|
+
import ChatTypingIndicator from "./ChatTypingIndicator";
|
|
4
|
+
import ChatSuggestions from "./ChatSuggestions";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Scrollable message area with auto-scroll to latest message.
|
|
8
|
+
*
|
|
9
|
+
* @param {Array} messages — array of message objects
|
|
10
|
+
* @param {boolean} isLoading — show typing indicator
|
|
11
|
+
* @param {boolean} isStreaming — agent is streaming (show different indicator text)
|
|
12
|
+
* @param {string[]} suggestions — follow-up suggestions shown after last assistant message
|
|
13
|
+
* @param {Function} onSuggestion — (text) => void
|
|
14
|
+
* @param {Function} renderAvatar — (message) => ReactNode, optional per-message avatar
|
|
15
|
+
*/
|
|
16
|
+
export default function ChatMessageList({
|
|
17
|
+
messages = [],
|
|
18
|
+
isLoading = false,
|
|
19
|
+
isStreaming = false,
|
|
20
|
+
suggestions = [],
|
|
21
|
+
onSuggestion,
|
|
22
|
+
renderAvatar,
|
|
23
|
+
}) {
|
|
24
|
+
const bottomRef = useRef(null);
|
|
25
|
+
const containerRef = useRef(null);
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
29
|
+
}, [messages.length, isLoading, isStreaming]);
|
|
30
|
+
|
|
31
|
+
const lastMessage = messages[messages.length - 1];
|
|
32
|
+
const showSuggestions =
|
|
33
|
+
suggestions.length > 0 &&
|
|
34
|
+
!isLoading &&
|
|
35
|
+
!isStreaming &&
|
|
36
|
+
lastMessage?.role === "assistant";
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div
|
|
40
|
+
ref={containerRef}
|
|
41
|
+
className="flex-1 overflow-y-auto px-4 py-4"
|
|
42
|
+
>
|
|
43
|
+
<div className="mx-auto max-w-3xl space-y-4">
|
|
44
|
+
{messages.map((msg) => (
|
|
45
|
+
<ChatMessage
|
|
46
|
+
key={msg.id}
|
|
47
|
+
message={msg}
|
|
48
|
+
avatar={renderAvatar?.(msg)}
|
|
49
|
+
/>
|
|
50
|
+
))}
|
|
51
|
+
|
|
52
|
+
{showSuggestions ? (
|
|
53
|
+
<div className="pl-10">
|
|
54
|
+
<ChatSuggestions
|
|
55
|
+
suggestions={suggestions}
|
|
56
|
+
onSelect={onSuggestion}
|
|
57
|
+
/>
|
|
58
|
+
</div>
|
|
59
|
+
) : null}
|
|
60
|
+
|
|
61
|
+
{isLoading && !isStreaming ? (
|
|
62
|
+
<ChatTypingIndicator label="Thinking" />
|
|
63
|
+
) : null}
|
|
64
|
+
|
|
65
|
+
{isStreaming ? (
|
|
66
|
+
<ChatTypingIndicator label="Generating" />
|
|
67
|
+
) : null}
|
|
68
|
+
|
|
69
|
+
<div ref={bottomRef} />
|
|
70
|
+
</div>
|
|
71
|
+
</div>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import ChatMessageList from "./ChatMessageList";
|
|
3
|
+
import ChatInput from "./ChatInput";
|
|
4
|
+
import ChatWelcome from "./ChatWelcome";
|
|
5
|
+
import useChatState from "./useChatState";
|
|
6
|
+
import { TrashIcon } from "@heroicons/react/24/outline";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* All-in-one chat panel. Composes ChatMessageList, ChatInput, ChatWelcome,
|
|
10
|
+
* and useChatState into a single drop-in component.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} title — panel header title
|
|
13
|
+
* @param {Function} onSend — async (userMessage, history, helpers) => assistantMessage?
|
|
14
|
+
* @param {Array} initialMessages — seed messages
|
|
15
|
+
* @param {string} welcomeTitle — welcome screen heading
|
|
16
|
+
* @param {string} welcomeSubtitle — welcome screen description
|
|
17
|
+
* @param {string[]} suggestions — starter and follow-up prompts
|
|
18
|
+
* @param {string} placeholder — input placeholder
|
|
19
|
+
* @param {string} className — additional classes on the root container
|
|
20
|
+
* @param {Function} renderAvatar — (message) => ReactNode
|
|
21
|
+
* @param {boolean} showHeader — show the title bar (default true)
|
|
22
|
+
*/
|
|
23
|
+
export default function ChatPanel({
|
|
24
|
+
title = "AI Assistant",
|
|
25
|
+
onSend,
|
|
26
|
+
initialMessages = [],
|
|
27
|
+
welcomeTitle,
|
|
28
|
+
welcomeSubtitle,
|
|
29
|
+
suggestions = [],
|
|
30
|
+
placeholder,
|
|
31
|
+
className = "",
|
|
32
|
+
renderAvatar,
|
|
33
|
+
showHeader = true,
|
|
34
|
+
}) {
|
|
35
|
+
const chat = useChatState({ initialMessages, onSend });
|
|
36
|
+
|
|
37
|
+
const isEmpty = chat.messages.length === 0;
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<div
|
|
41
|
+
className={[
|
|
42
|
+
"flex flex-col overflow-hidden rounded-xl border border-slate-200 bg-white dark:border-slate-800 dark:bg-slate-900",
|
|
43
|
+
className,
|
|
44
|
+
]
|
|
45
|
+
.filter(Boolean)
|
|
46
|
+
.join(" ")}
|
|
47
|
+
>
|
|
48
|
+
{/* Header */}
|
|
49
|
+
{showHeader ? (
|
|
50
|
+
<div className="flex items-center justify-between border-b border-slate-100 px-4 py-3 dark:border-slate-800">
|
|
51
|
+
<h3 className="text-sm font-semibold text-slate-900 dark:text-slate-50">
|
|
52
|
+
{title}
|
|
53
|
+
</h3>
|
|
54
|
+
{chat.messages.length > 0 ? (
|
|
55
|
+
<button
|
|
56
|
+
type="button"
|
|
57
|
+
onClick={chat.clearMessages}
|
|
58
|
+
className="rounded p-1 text-slate-400 transition hover:bg-slate-100 hover:text-slate-600 dark:text-slate-500 dark:hover:bg-slate-800 dark:hover:text-slate-300"
|
|
59
|
+
aria-label="Clear chat"
|
|
60
|
+
>
|
|
61
|
+
<TrashIcon className="h-4 w-4" />
|
|
62
|
+
</button>
|
|
63
|
+
) : null}
|
|
64
|
+
</div>
|
|
65
|
+
) : null}
|
|
66
|
+
|
|
67
|
+
{/* Messages or welcome */}
|
|
68
|
+
{isEmpty ? (
|
|
69
|
+
<ChatWelcome
|
|
70
|
+
title={welcomeTitle}
|
|
71
|
+
subtitle={welcomeSubtitle}
|
|
72
|
+
suggestions={suggestions}
|
|
73
|
+
onSuggestion={(text) => chat.sendMessage(text)}
|
|
74
|
+
/>
|
|
75
|
+
) : (
|
|
76
|
+
<ChatMessageList
|
|
77
|
+
messages={chat.messages}
|
|
78
|
+
isLoading={chat.isLoading}
|
|
79
|
+
isStreaming={chat.isStreaming}
|
|
80
|
+
suggestions={suggestions}
|
|
81
|
+
onSuggestion={(text) => chat.sendMessage(text)}
|
|
82
|
+
renderAvatar={renderAvatar}
|
|
83
|
+
/>
|
|
84
|
+
)}
|
|
85
|
+
|
|
86
|
+
{/* Input */}
|
|
87
|
+
<div className="border-t border-slate-100 p-3 dark:border-slate-800">
|
|
88
|
+
<ChatInput
|
|
89
|
+
onSend={(content) => chat.sendMessage(content)}
|
|
90
|
+
disabled={chat.isLoading}
|
|
91
|
+
isLoading={chat.isLoading}
|
|
92
|
+
placeholder={placeholder}
|
|
93
|
+
/>
|
|
94
|
+
</div>
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
}
|