vpschatbotwidget 1.2.1 → 1.2.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/README.md +56 -16
- package/dist-lib/types/chatbot/api/chatbot-types.d.ts +8 -1
- package/dist-lib/types/chatbot/core/AgentReasoning.d.ts +4 -1
- package/dist-lib/types/chatbot/core/ChatContext.d.ts +5 -1
- package/dist-lib/types/chatbot/core/ChatHeader.d.ts +2 -0
- package/dist-lib/types/chatbot/core/ChatInlineCitation.d.ts +5 -0
- package/dist-lib/types/chatbot/core/ChatInput.d.ts +5 -0
- package/dist-lib/types/chatbot/core/ChatMarkdown.d.ts +4 -1
- package/dist-lib/types/chatbot/core/ChatMessage.d.ts +1 -0
- package/dist-lib/types/chatbot/core/ChatMessageActions.d.ts +7 -0
- package/dist-lib/types/chatbot/core/ChatMessageList.d.ts +3 -1
- package/dist-lib/types/chatbot/core/ChatProvider.d.ts +2 -2
- package/dist-lib/types/chatbot/core/GroundedSources.d.ts +5 -0
- package/dist-lib/types/chatbot/embedded/EmbeddedChatbot.d.ts +2 -2
- package/dist-lib/types/chatbot/hooks/useCopyText.d.ts +4 -0
- package/dist-lib/types/chatbot/hooks/useResponseSound.d.ts +6 -0
- package/dist-lib/types/chatbot/index.d.ts +1 -1
- package/dist-lib/types/chatbot/widget/ChatbotWidget.d.ts +2 -2
- package/dist-lib/types/components/ai-elements/inline-citation.d.ts +52 -0
- package/dist-lib/types/components/ai-elements/reasoning.d.ts +21 -0
- package/dist-lib/types/components/ai-elements/suggestion.d.ts +9 -0
- package/dist-lib/types/components/ui/bubble.d.ts +6 -0
- package/dist-lib/types/components/ui/button.d.ts +8 -0
- package/dist-lib/types/components/ui/hover-card.d.ts +5 -0
- package/dist-lib/types/components/ui/input-group.d.ts +5 -0
- package/dist-lib/types/components/ui/marker.d.ts +6 -0
- package/dist-lib/types/components/ui/message.d.ts +8 -0
- package/dist-lib/types/components/ui/trace.d.ts +19 -0
- package/dist-lib/types/lib/safeHref.d.ts +1 -0
- package/dist-lib/vps-chatbot.cjs +110 -29
- package/dist-lib/vps-chatbot.js +9540 -1654
- package/package.json +6 -2
- package/src/chatbot/api/chatbot-api.ts +1 -0
- package/src/chatbot/api/chatbot-types.ts +9 -1
- package/src/chatbot/chatbot.test.tsx +125 -7
- package/src/chatbot/core/AgentReasoning.tsx +68 -36
- package/src/chatbot/core/ChatContext.tsx +5 -0
- package/src/chatbot/core/ChatEmptyState.tsx +14 -13
- package/src/chatbot/core/ChatError.tsx +20 -12
- package/src/chatbot/core/ChatHeader.tsx +17 -1
- package/src/chatbot/core/ChatInlineCitation.tsx +63 -0
- package/src/chatbot/core/ChatInput.tsx +73 -51
- package/src/chatbot/core/ChatMarkdown.tsx +70 -24
- package/src/chatbot/core/ChatMessage.tsx +77 -88
- package/src/chatbot/core/ChatMessageActions.tsx +51 -0
- package/src/chatbot/core/ChatMessageList.tsx +15 -7
- package/src/chatbot/core/ChatPanel.tsx +19 -7
- package/src/chatbot/core/ChatProvider.tsx +58 -10
- package/src/chatbot/core/ChatSuggestions.tsx +14 -34
- package/src/chatbot/core/GroundedSources.tsx +113 -90
- package/src/chatbot/core/grounding.test.tsx +111 -4
- package/src/chatbot/embedded/EmbeddedChatbot.tsx +2 -2
- package/src/chatbot/hooks/useCopyText.ts +23 -0
- package/src/chatbot/hooks/useResponseSound.ts +74 -0
- package/src/chatbot/index.ts +1 -0
- package/src/chatbot/widget/ChatbotWidget.tsx +2 -2
- package/src/components/ai-elements/inline-citation.tsx +299 -0
- package/src/components/ai-elements/reasoning.tsx +126 -0
- package/src/components/ai-elements/suggestion.tsx +42 -0
- package/src/components/ui/bubble.tsx +18 -0
- package/src/components/ui/button.tsx +43 -0
- package/src/components/ui/hover-card.tsx +43 -0
- package/src/components/ui/input-group.tsx +18 -0
- package/src/components/ui/marker.tsx +18 -0
- package/src/components/ui/message.tsx +26 -0
- package/src/components/ui/trace.tsx +68 -0
- package/src/index.css +579 -272
- package/src/lib/safeHref.ts +6 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export function useCopyText() {
|
|
4
|
+
const [status, setStatus] = useState<"idle" | "copying" | "copied" | "failed">("idle");
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
if (status !== "copied") return;
|
|
8
|
+
const timeout = window.setTimeout(() => setStatus("idle"), 2000);
|
|
9
|
+
return () => window.clearTimeout(timeout);
|
|
10
|
+
}, [status]);
|
|
11
|
+
|
|
12
|
+
async function copy(text: string) {
|
|
13
|
+
setStatus("copying");
|
|
14
|
+
try {
|
|
15
|
+
await navigator.clipboard.writeText(text);
|
|
16
|
+
setStatus("copied");
|
|
17
|
+
} catch {
|
|
18
|
+
setStatus("failed");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return { status, copy };
|
|
23
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
const preferenceKey = "vps-chatbot-response-sound";
|
|
4
|
+
|
|
5
|
+
export function useResponseSound() {
|
|
6
|
+
const [soundEnabled, setSoundEnabled] = useState(true);
|
|
7
|
+
const enabledRef = useRef(true);
|
|
8
|
+
const audioRef = useRef<AudioContext | null>(null);
|
|
9
|
+
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
try {
|
|
12
|
+
enabledRef.current = window.localStorage.getItem(preferenceKey) !== "off";
|
|
13
|
+
setSoundEnabled(enabledRef.current);
|
|
14
|
+
} catch {
|
|
15
|
+
// The preference still works for the current view if storage is blocked.
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return () => {
|
|
19
|
+
void audioRef.current?.close().catch(() => {});
|
|
20
|
+
audioRef.current = null;
|
|
21
|
+
};
|
|
22
|
+
}, []);
|
|
23
|
+
|
|
24
|
+
const prepareSound = useCallback(() => {
|
|
25
|
+
if (!enabledRef.current || typeof AudioContext === "undefined") return;
|
|
26
|
+
try {
|
|
27
|
+
const audio = audioRef.current ?? (audioRef.current = new AudioContext());
|
|
28
|
+
if (audio.state !== "running") void audio.resume().catch(() => {});
|
|
29
|
+
} catch {
|
|
30
|
+
// Audio availability should never affect chat requests.
|
|
31
|
+
}
|
|
32
|
+
}, []);
|
|
33
|
+
|
|
34
|
+
const toggleSound = useCallback(() => {
|
|
35
|
+
const enabled = !enabledRef.current;
|
|
36
|
+
enabledRef.current = enabled;
|
|
37
|
+
setSoundEnabled(enabled);
|
|
38
|
+
try {
|
|
39
|
+
window.localStorage.setItem(preferenceKey, enabled ? "on" : "off");
|
|
40
|
+
} catch {
|
|
41
|
+
// Keep the in-memory preference.
|
|
42
|
+
}
|
|
43
|
+
if (enabled) prepareSound();
|
|
44
|
+
}, [prepareSound]);
|
|
45
|
+
|
|
46
|
+
const playSound = useCallback(() => {
|
|
47
|
+
const audio = audioRef.current;
|
|
48
|
+
if (!enabledRef.current || audio?.state !== "running") return;
|
|
49
|
+
try {
|
|
50
|
+
const now = audio.currentTime;
|
|
51
|
+
for (const [index, note] of [660, 880].entries()) {
|
|
52
|
+
const start = now + index * 0.09;
|
|
53
|
+
const oscillator = audio.createOscillator();
|
|
54
|
+
const gain = audio.createGain();
|
|
55
|
+
oscillator.frequency.value = note;
|
|
56
|
+
gain.gain.setValueAtTime(0.001, start);
|
|
57
|
+
gain.gain.exponentialRampToValueAtTime(0.06, start + 0.015);
|
|
58
|
+
gain.gain.exponentialRampToValueAtTime(0.001, start + 0.14);
|
|
59
|
+
oscillator.connect(gain);
|
|
60
|
+
gain.connect(audio.destination);
|
|
61
|
+
oscillator.onended = () => {
|
|
62
|
+
oscillator.disconnect();
|
|
63
|
+
gain.disconnect();
|
|
64
|
+
};
|
|
65
|
+
oscillator.start(start);
|
|
66
|
+
oscillator.stop(start + 0.15);
|
|
67
|
+
}
|
|
68
|
+
} catch {
|
|
69
|
+
// The chat reply stays complete if audio playback is unavailable.
|
|
70
|
+
}
|
|
71
|
+
}, []);
|
|
72
|
+
|
|
73
|
+
return { soundEnabled, toggleSound, prepareSound, playSound };
|
|
74
|
+
}
|
package/src/chatbot/index.ts
CHANGED
|
@@ -37,9 +37,9 @@ export interface ChatbotWidgetProps {
|
|
|
37
37
|
disableSuggestions?: boolean;
|
|
38
38
|
/** Initial welcome message to trigger on mount */
|
|
39
39
|
initialMessage?: string;
|
|
40
|
-
/**
|
|
40
|
+
/** Hide inline previews and the source list's inspection controls. */
|
|
41
41
|
hideInspectSources?: boolean;
|
|
42
|
-
/** Whether to show grounded sources (default:
|
|
42
|
+
/** Whether to show grounded sources (default: false) */
|
|
43
43
|
showGroundedSources?: boolean;
|
|
44
44
|
modelId?: string;
|
|
45
45
|
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
useContext,
|
|
4
|
+
useEffect,
|
|
5
|
+
useId,
|
|
6
|
+
useMemo,
|
|
7
|
+
useState,
|
|
8
|
+
type ComponentProps,
|
|
9
|
+
} from "react";
|
|
10
|
+
import useEmblaCarousel, { type UseEmblaCarouselType } from "embla-carousel-react";
|
|
11
|
+
import { ChevronLeft, ChevronRight } from "lucide-react";
|
|
12
|
+
import { cn } from "../../lib/cn";
|
|
13
|
+
import { safeHref } from "../../lib/safeHref";
|
|
14
|
+
import { Button, type ButtonProps } from "../ui/button";
|
|
15
|
+
import { createHoverCardHandle, HoverCard, HoverCardContent, HoverCardTrigger } from "../ui/hover-card";
|
|
16
|
+
|
|
17
|
+
export type InlineCitationProps = ComponentProps<"span">;
|
|
18
|
+
export function InlineCitation({ className, ...props }: InlineCitationProps) {
|
|
19
|
+
return <span className={cn("inline-citation", className)} {...props} />;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type InlineCitationTextProps = ComponentProps<"span">;
|
|
23
|
+
export function InlineCitationText(props: InlineCitationTextProps) {
|
|
24
|
+
return <span {...props} />;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type CardNavigation = {
|
|
28
|
+
handle: ReturnType<typeof createHoverCardHandle>;
|
|
29
|
+
openDelay: number;
|
|
30
|
+
closeDelay: number;
|
|
31
|
+
trigger: HTMLElement | null;
|
|
32
|
+
popup: HTMLElement | null;
|
|
33
|
+
focusRequested: boolean;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const CardNavigationContext = createContext<CardNavigation | null>(null);
|
|
37
|
+
|
|
38
|
+
function focusCard(navigation: CardNavigation) {
|
|
39
|
+
if (!navigation.popup || !navigation.focusRequested || !navigation.handle.isOpen) return;
|
|
40
|
+
navigation.focusRequested = false;
|
|
41
|
+
navigation.popup.focus();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type InlineCitationCardProps = ComponentProps<typeof HoverCard> & {
|
|
45
|
+
openDelay?: number;
|
|
46
|
+
closeDelay?: number;
|
|
47
|
+
};
|
|
48
|
+
export function InlineCitationCard({
|
|
49
|
+
handle,
|
|
50
|
+
onOpenChange,
|
|
51
|
+
openDelay = 80,
|
|
52
|
+
closeDelay = 180,
|
|
53
|
+
...props
|
|
54
|
+
}: InlineCitationCardProps) {
|
|
55
|
+
const [internalHandle] = useState(() => createHoverCardHandle());
|
|
56
|
+
const activeHandle = handle ?? internalHandle;
|
|
57
|
+
const navigation = useMemo<CardNavigation>(() => ({
|
|
58
|
+
handle: activeHandle,
|
|
59
|
+
openDelay,
|
|
60
|
+
closeDelay,
|
|
61
|
+
trigger: null,
|
|
62
|
+
popup: null,
|
|
63
|
+
focusRequested: false,
|
|
64
|
+
}), [activeHandle, openDelay, closeDelay]);
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<CardNavigationContext.Provider value={navigation}>
|
|
68
|
+
<HoverCard
|
|
69
|
+
{...props}
|
|
70
|
+
handle={activeHandle}
|
|
71
|
+
onOpenChange={(open, details) => {
|
|
72
|
+
onOpenChange?.(open, details);
|
|
73
|
+
if (!open && details.reason === "escape-key" && navigation.popup?.contains(document.activeElement)) {
|
|
74
|
+
navigation.trigger?.focus();
|
|
75
|
+
}
|
|
76
|
+
}}
|
|
77
|
+
/>
|
|
78
|
+
</CardNavigationContext.Provider>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function sourceLabel(url: string): string {
|
|
83
|
+
try {
|
|
84
|
+
return new URL(url).hostname || url;
|
|
85
|
+
} catch {
|
|
86
|
+
return url;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type InlineCitationCardTriggerProps = Omit<ButtonProps, "render"> & {
|
|
91
|
+
sources: string[];
|
|
92
|
+
delay?: number;
|
|
93
|
+
closeDelay?: number;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export function InlineCitationCardTrigger({
|
|
97
|
+
sources,
|
|
98
|
+
delay,
|
|
99
|
+
closeDelay,
|
|
100
|
+
id,
|
|
101
|
+
children,
|
|
102
|
+
onClick,
|
|
103
|
+
...props
|
|
104
|
+
}: InlineCitationCardTriggerProps) {
|
|
105
|
+
const navigation = useContext(CardNavigationContext);
|
|
106
|
+
const fallbackId = useId();
|
|
107
|
+
const triggerId = id ?? fallbackId;
|
|
108
|
+
const primary = sources[0];
|
|
109
|
+
const label = primary ? sourceLabel(primary) : "Source";
|
|
110
|
+
|
|
111
|
+
return (
|
|
112
|
+
<HoverCardTrigger
|
|
113
|
+
id={triggerId}
|
|
114
|
+
handle={navigation?.handle}
|
|
115
|
+
delay={delay ?? navigation?.openDelay ?? 80}
|
|
116
|
+
closeDelay={closeDelay ?? navigation?.closeDelay ?? 180}
|
|
117
|
+
render={
|
|
118
|
+
<Button
|
|
119
|
+
variant="secondary"
|
|
120
|
+
size="xs"
|
|
121
|
+
aria-haspopup="dialog"
|
|
122
|
+
{...props}
|
|
123
|
+
onClick={(event) => {
|
|
124
|
+
onClick?.(event);
|
|
125
|
+
if (!navigation || event.defaultPrevented) return;
|
|
126
|
+
navigation.trigger = event.currentTarget;
|
|
127
|
+
navigation.focusRequested = true;
|
|
128
|
+
navigation.handle.open(triggerId);
|
|
129
|
+
focusCard(navigation);
|
|
130
|
+
}}
|
|
131
|
+
/>
|
|
132
|
+
}
|
|
133
|
+
>
|
|
134
|
+
{children ?? (sources.length > 1 ? `${label} +${sources.length - 1}` : label)}
|
|
135
|
+
</HoverCardTrigger>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export type InlineCitationCardBodyProps = ComponentProps<typeof HoverCardContent>;
|
|
140
|
+
export function InlineCitationCardBody({ ref, onBlur, ...props }: InlineCitationCardBodyProps) {
|
|
141
|
+
const navigation = useContext(CardNavigationContext);
|
|
142
|
+
return (
|
|
143
|
+
<HoverCardContent
|
|
144
|
+
tabIndex={-1}
|
|
145
|
+
role="dialog"
|
|
146
|
+
aria-label="Source preview"
|
|
147
|
+
{...props}
|
|
148
|
+
ref={(element) => {
|
|
149
|
+
if (typeof ref === "function") ref(element);
|
|
150
|
+
else if (ref) ref.current = element;
|
|
151
|
+
if (!navigation) return;
|
|
152
|
+
navigation.popup = element;
|
|
153
|
+
if (element) focusCard(navigation);
|
|
154
|
+
}}
|
|
155
|
+
onBlur={(event) => {
|
|
156
|
+
onBlur?.(event);
|
|
157
|
+
if (!navigation || event.defaultPrevented) return;
|
|
158
|
+
if (event.relatedTarget !== navigation.trigger && !event.currentTarget.contains(event.relatedTarget)) {
|
|
159
|
+
navigation.handle.close();
|
|
160
|
+
}
|
|
161
|
+
}}
|
|
162
|
+
/>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
type EmblaApi = NonNullable<UseEmblaCarouselType[1]>;
|
|
167
|
+
type EmblaOptions = NonNullable<Parameters<typeof useEmblaCarousel>[0]>;
|
|
168
|
+
type EmblaPlugins = NonNullable<Parameters<typeof useEmblaCarousel>[1]>;
|
|
169
|
+
|
|
170
|
+
const CarouselContext = createContext<{
|
|
171
|
+
viewportRef: UseEmblaCarouselType[0];
|
|
172
|
+
api: UseEmblaCarouselType[1];
|
|
173
|
+
selected: number;
|
|
174
|
+
count: number;
|
|
175
|
+
} | null>(null);
|
|
176
|
+
|
|
177
|
+
function useInlineCitationCarousel() {
|
|
178
|
+
const carousel = useContext(CarouselContext);
|
|
179
|
+
if (!carousel) throw new Error("Citation carousel parts must be rendered inside InlineCitationCarousel.");
|
|
180
|
+
return carousel;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export type InlineCitationCarouselProps = ComponentProps<"div"> & {
|
|
184
|
+
opts?: EmblaOptions;
|
|
185
|
+
plugins?: EmblaPlugins;
|
|
186
|
+
orientation?: "horizontal" | "vertical";
|
|
187
|
+
setApi?: (api: EmblaApi) => void;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export function InlineCitationCarousel({
|
|
191
|
+
opts,
|
|
192
|
+
plugins,
|
|
193
|
+
orientation = "horizontal",
|
|
194
|
+
setApi,
|
|
195
|
+
className,
|
|
196
|
+
...props
|
|
197
|
+
}: InlineCitationCarouselProps) {
|
|
198
|
+
const [viewportRef, api] = useEmblaCarousel({ ...opts, axis: orientation === "vertical" ? "y" : "x" }, plugins);
|
|
199
|
+
const [{ selected, count }, setPage] = useState({ selected: 0, count: 0 });
|
|
200
|
+
|
|
201
|
+
useEffect(() => {
|
|
202
|
+
if (!api) return;
|
|
203
|
+
setApi?.(api);
|
|
204
|
+
const update = () => setPage({ selected: api.selectedScrollSnap(), count: api.scrollSnapList().length });
|
|
205
|
+
update();
|
|
206
|
+
api.on("select", update).on("reInit", update);
|
|
207
|
+
return () => { api.off("select", update).off("reInit", update); };
|
|
208
|
+
}, [api, setApi]);
|
|
209
|
+
|
|
210
|
+
return (
|
|
211
|
+
<CarouselContext.Provider value={{ viewportRef, api, selected, count }}>
|
|
212
|
+
<div role="region" aria-roledescription="carousel" {...props}
|
|
213
|
+
className={cn("inline-citation-carousel", className)} data-orientation={orientation} />
|
|
214
|
+
</CarouselContext.Provider>
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export type InlineCitationCarouselContentProps = ComponentProps<"div">;
|
|
219
|
+
export function InlineCitationCarouselContent({ className, ...props }: InlineCitationCarouselContentProps) {
|
|
220
|
+
const { viewportRef } = useInlineCitationCarousel();
|
|
221
|
+
return <div ref={viewportRef} className="inline-citation-viewport">
|
|
222
|
+
<div className={cn("inline-citation-track", className)} {...props} />
|
|
223
|
+
</div>;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export type InlineCitationCarouselItemProps = ComponentProps<"div">;
|
|
227
|
+
export function InlineCitationCarouselItem({ className, ...props }: InlineCitationCarouselItemProps) {
|
|
228
|
+
return <div role="group" aria-roledescription="slide" className={cn("inline-citation-slide", className)} {...props} />;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export type InlineCitationCarouselHeaderProps = ComponentProps<"div">;
|
|
232
|
+
export function InlineCitationCarouselHeader({ className, ...props }: InlineCitationCarouselHeaderProps) {
|
|
233
|
+
return <div className={cn("inline-citation-carousel-header", className)} {...props} />;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export type InlineCitationCarouselIndexProps = ComponentProps<"div">;
|
|
237
|
+
export function InlineCitationCarouselIndex({ children, ...props }: InlineCitationCarouselIndexProps) {
|
|
238
|
+
const { selected, count } = useInlineCitationCarousel();
|
|
239
|
+
return <div aria-live="polite" {...props}>{children ?? (count ? `${selected + 1}/${count}` : "")}</div>;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function CarouselButton({ next, children, disabled, onClick, ...props }: ButtonProps & { next: boolean }) {
|
|
243
|
+
const { api } = useInlineCitationCarousel();
|
|
244
|
+
const canScroll = next ? api?.canScrollNext() : api?.canScrollPrev();
|
|
245
|
+
return (
|
|
246
|
+
<Button
|
|
247
|
+
variant="ghost"
|
|
248
|
+
size="icon-xs"
|
|
249
|
+
aria-label={next ? "Next source" : "Previous source"}
|
|
250
|
+
{...props}
|
|
251
|
+
disabled={disabled || !canScroll}
|
|
252
|
+
onClick={(event) => {
|
|
253
|
+
onClick?.(event);
|
|
254
|
+
if (event.defaultPrevented) return;
|
|
255
|
+
if (next) api?.scrollNext();
|
|
256
|
+
else api?.scrollPrev();
|
|
257
|
+
}}
|
|
258
|
+
>
|
|
259
|
+
{children ?? (next ? <ChevronRight size={14} aria-hidden="true" /> : <ChevronLeft size={14} aria-hidden="true" />)}
|
|
260
|
+
</Button>
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export type InlineCitationCarouselPrevProps = ButtonProps;
|
|
265
|
+
export function InlineCitationCarouselPrev(props: InlineCitationCarouselPrevProps) {
|
|
266
|
+
return <CarouselButton {...props} next={false} />;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export type InlineCitationCarouselNextProps = ButtonProps;
|
|
270
|
+
export function InlineCitationCarouselNext(props: InlineCitationCarouselNextProps) {
|
|
271
|
+
return <CarouselButton {...props} next />;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export type InlineCitationSourceProps = ComponentProps<"div"> & {
|
|
275
|
+
title?: string;
|
|
276
|
+
url?: string;
|
|
277
|
+
description?: string;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
export function InlineCitationSource({ title, url, description, className, children, ...props }: InlineCitationSourceProps) {
|
|
281
|
+
const href = url ? safeHref(url) : undefined;
|
|
282
|
+
return (
|
|
283
|
+
<div className={cn("inline-citation-source", className)} {...props}>
|
|
284
|
+
<div className="inline-citation-title">{title || (href ? sourceLabel(href) : "Source")}</div>
|
|
285
|
+
{href && (
|
|
286
|
+
<a href={href} target="_blank" rel="noopener noreferrer">
|
|
287
|
+
{sourceLabel(href)}<span className="sr-only"> (opens in a new tab)</span>
|
|
288
|
+
</a>
|
|
289
|
+
)}
|
|
290
|
+
{description && <p>{description}</p>}
|
|
291
|
+
{children}
|
|
292
|
+
</div>
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export type InlineCitationQuoteProps = ComponentProps<"blockquote">;
|
|
297
|
+
export function InlineCitationQuote({ className, ...props }: InlineCitationQuoteProps) {
|
|
298
|
+
return <blockquote className={cn("inline-citation-quote", className)} {...props} />;
|
|
299
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
lazy,
|
|
4
|
+
Suspense,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
type ComponentProps,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
} from "react";
|
|
12
|
+
import { CheckCircle2, LoaderCircle } from "lucide-react";
|
|
13
|
+
import { cn } from "../../lib/cn";
|
|
14
|
+
import {
|
|
15
|
+
Trace,
|
|
16
|
+
TraceCollapsible,
|
|
17
|
+
TraceCollapsibleContent,
|
|
18
|
+
TraceCollapsibleTrigger,
|
|
19
|
+
TraceDescription,
|
|
20
|
+
TraceIcon,
|
|
21
|
+
TraceLine,
|
|
22
|
+
TraceTitle,
|
|
23
|
+
} from "../ui/trace";
|
|
24
|
+
|
|
25
|
+
const Streamdown = lazy(async () => ({ default: (await import("streamdown")).Streamdown }));
|
|
26
|
+
|
|
27
|
+
const ReasoningContext = createContext<{
|
|
28
|
+
isStreaming: boolean;
|
|
29
|
+
isOpen: boolean;
|
|
30
|
+
setIsOpen: (open: boolean) => void;
|
|
31
|
+
duration?: number;
|
|
32
|
+
} | null>(null);
|
|
33
|
+
|
|
34
|
+
export function useReasoning() {
|
|
35
|
+
const reasoning = useContext(ReasoningContext);
|
|
36
|
+
if (!reasoning) throw new Error("Reasoning parts must be rendered inside Reasoning.");
|
|
37
|
+
return reasoning;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type ReasoningProps = ComponentProps<typeof TraceCollapsible> & {
|
|
41
|
+
isStreaming?: boolean;
|
|
42
|
+
duration?: number;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export function Reasoning({
|
|
46
|
+
isStreaming = false,
|
|
47
|
+
open,
|
|
48
|
+
defaultOpen,
|
|
49
|
+
onOpenChange,
|
|
50
|
+
duration,
|
|
51
|
+
className,
|
|
52
|
+
...props
|
|
53
|
+
}: ReasoningProps) {
|
|
54
|
+
const [userOpen, setUserOpen] = useState(defaultOpen);
|
|
55
|
+
const [measuredDuration, setMeasuredDuration] = useState<number>();
|
|
56
|
+
const startedAt = useRef<number | null>(null);
|
|
57
|
+
const isOpen = open ?? userOpen ?? isStreaming;
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (isStreaming) {
|
|
61
|
+
startedAt.current = performance.now();
|
|
62
|
+
} else if (startedAt.current !== null) {
|
|
63
|
+
setMeasuredDuration(Math.round((performance.now() - startedAt.current) / 1000));
|
|
64
|
+
startedAt.current = null;
|
|
65
|
+
}
|
|
66
|
+
}, [isStreaming]);
|
|
67
|
+
|
|
68
|
+
function setIsOpen(nextOpen: boolean) {
|
|
69
|
+
if (open === undefined) setUserOpen(nextOpen);
|
|
70
|
+
onOpenChange?.(nextOpen);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<ReasoningContext.Provider value={{ isStreaming, isOpen, setIsOpen, duration: duration ?? measuredDuration }}>
|
|
75
|
+
<Trace className={className}>
|
|
76
|
+
<TraceCollapsible {...props} open={isOpen} onOpenChange={setIsOpen} />
|
|
77
|
+
</Trace>
|
|
78
|
+
</ReasoningContext.Provider>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type ReasoningTriggerProps = ComponentProps<typeof TraceCollapsibleTrigger> & {
|
|
83
|
+
getThinkingMessage?: (isStreaming: boolean, duration?: number) => ReactNode;
|
|
84
|
+
icon?: ReactNode;
|
|
85
|
+
description?: ReactNode;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export function ReasoningTrigger({
|
|
89
|
+
getThinkingMessage,
|
|
90
|
+
children,
|
|
91
|
+
icon,
|
|
92
|
+
description,
|
|
93
|
+
...props
|
|
94
|
+
}: ReasoningTriggerProps) {
|
|
95
|
+
const { isStreaming, duration } = useReasoning();
|
|
96
|
+
const title = isStreaming ? "Thinking" : duration === undefined ? "Thought" : `Thought for ${duration}s`;
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<TraceCollapsibleTrigger {...props}>
|
|
100
|
+
<TraceLine status={isStreaming ? "running" : undefined}>
|
|
101
|
+
<TraceIcon>
|
|
102
|
+
{icon ?? (isStreaming ? <LoaderCircle size={14} className="trace-spinner" /> : <CheckCircle2 size={14} />)}
|
|
103
|
+
</TraceIcon>
|
|
104
|
+
<TraceTitle>{children ?? getThinkingMessage?.(isStreaming, duration) ?? title}</TraceTitle>
|
|
105
|
+
{description && <TraceDescription aria-hidden={isStreaming || undefined}>{description}</TraceDescription>}
|
|
106
|
+
</TraceLine>
|
|
107
|
+
</TraceCollapsibleTrigger>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export type ReasoningContentProps = ComponentProps<typeof TraceCollapsibleContent>;
|
|
112
|
+
|
|
113
|
+
export function ReasoningContent({ children, className, ...props }: ReasoningContentProps) {
|
|
114
|
+
const { isStreaming } = useReasoning();
|
|
115
|
+
return (
|
|
116
|
+
<TraceCollapsibleContent className={cn("reasoning-content", className)} {...props}>
|
|
117
|
+
{typeof children === "string" ? (
|
|
118
|
+
<Suspense fallback={<div className="md-content reasoning-text">{children}</div>}>
|
|
119
|
+
<Streamdown className="md-content" isAnimating={isStreaming} mode={isStreaming ? "streaming" : "static"}>
|
|
120
|
+
{children}
|
|
121
|
+
</Streamdown>
|
|
122
|
+
</Suspense>
|
|
123
|
+
) : children}
|
|
124
|
+
</TraceCollapsibleContent>
|
|
125
|
+
);
|
|
126
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ComponentProps } from "react";
|
|
2
|
+
import { cn } from "../../lib/cn";
|
|
3
|
+
import { Button, type ButtonProps } from "../ui/button";
|
|
4
|
+
|
|
5
|
+
export type SuggestionsProps = ComponentProps<"div">;
|
|
6
|
+
|
|
7
|
+
export function Suggestions({ className, ...props }: SuggestionsProps) {
|
|
8
|
+
return (
|
|
9
|
+
<div
|
|
10
|
+
{...props}
|
|
11
|
+
className={cn("flex scroll-fade-x scrollbar-none gap-2 overflow-x-auto overscroll-x-contain", className)}
|
|
12
|
+
data-slot="suggestions"
|
|
13
|
+
/>
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type SuggestionProps = Omit<ButtonProps, "onClick"> & {
|
|
18
|
+
suggestion: string;
|
|
19
|
+
onClick?: (suggestion: string) => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export function Suggestion({
|
|
23
|
+
suggestion,
|
|
24
|
+
onClick,
|
|
25
|
+
children,
|
|
26
|
+
className,
|
|
27
|
+
variant = "outline",
|
|
28
|
+
size = "sm",
|
|
29
|
+
...props
|
|
30
|
+
}: SuggestionProps) {
|
|
31
|
+
return (
|
|
32
|
+
<Button
|
|
33
|
+
{...props}
|
|
34
|
+
variant={variant}
|
|
35
|
+
size={size}
|
|
36
|
+
className={(state) => cn("shrink-0", typeof className === "function" ? className(state) : className)}
|
|
37
|
+
onClick={() => onClick?.(suggestion)}
|
|
38
|
+
>
|
|
39
|
+
{children ?? suggestion}
|
|
40
|
+
</Button>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ComponentProps } from "react";
|
|
2
|
+
import { cn } from "../../lib/cn";
|
|
3
|
+
|
|
4
|
+
export function Bubble({
|
|
5
|
+
align = "start",
|
|
6
|
+
variant = "muted",
|
|
7
|
+
className,
|
|
8
|
+
...props
|
|
9
|
+
}: ComponentProps<"div"> & {
|
|
10
|
+
align?: "start" | "end";
|
|
11
|
+
variant?: "muted" | "destructive";
|
|
12
|
+
}) {
|
|
13
|
+
return <div {...props} data-slot="bubble" data-align={align} data-variant={variant} className={cn("chat-bubble", className)} />;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function BubbleContent({ className, ...props }: ComponentProps<"div">) {
|
|
17
|
+
return <div {...props} data-slot="bubble-content" className={cn("chat-bubble-content", className)} />;
|
|
18
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Button as ButtonPrimitive } from "@base-ui/react/button";
|
|
2
|
+
import { cva, type VariantProps } from "class-variance-authority";
|
|
3
|
+
import { cn } from "../../lib/cn";
|
|
4
|
+
|
|
5
|
+
export const buttonVariants = cva("vps-ui-button", {
|
|
6
|
+
variants: {
|
|
7
|
+
variant: {
|
|
8
|
+
default: "vps-ui-button--default",
|
|
9
|
+
outline: "vps-ui-button--outline",
|
|
10
|
+
secondary: "vps-ui-button--secondary",
|
|
11
|
+
ghost: "vps-ui-button--ghost",
|
|
12
|
+
destructive: "vps-ui-button--destructive",
|
|
13
|
+
link: "vps-ui-button--link",
|
|
14
|
+
},
|
|
15
|
+
size: {
|
|
16
|
+
default: "vps-ui-button--size-default",
|
|
17
|
+
xs: "vps-ui-button--size-xs",
|
|
18
|
+
sm: "vps-ui-button--size-sm",
|
|
19
|
+
lg: "vps-ui-button--size-lg",
|
|
20
|
+
icon: "vps-ui-button--size-icon",
|
|
21
|
+
"icon-xs": "vps-ui-button--size-icon-xs",
|
|
22
|
+
"icon-sm": "vps-ui-button--size-icon-sm",
|
|
23
|
+
"icon-lg": "vps-ui-button--size-icon-lg",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
defaultVariants: { variant: "default", size: "default" },
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export type ButtonProps = ButtonPrimitive.Props & VariantProps<typeof buttonVariants>;
|
|
30
|
+
|
|
31
|
+
export function Button({ className, variant, size, ...props }: ButtonProps) {
|
|
32
|
+
return (
|
|
33
|
+
<ButtonPrimitive
|
|
34
|
+
type="button"
|
|
35
|
+
data-slot="button"
|
|
36
|
+
{...props}
|
|
37
|
+
className={(state) => cn(
|
|
38
|
+
buttonVariants({ variant, size }),
|
|
39
|
+
typeof className === "function" ? className(state) : className
|
|
40
|
+
)}
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { PreviewCard } from "@base-ui/react/preview-card";
|
|
2
|
+
import { cn } from "../../lib/cn";
|
|
3
|
+
|
|
4
|
+
export const createHoverCardHandle = PreviewCard.createHandle;
|
|
5
|
+
|
|
6
|
+
export function HoverCard(props: PreviewCard.Root.Props) {
|
|
7
|
+
return <PreviewCard.Root {...props} />;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function HoverCardTrigger(props: PreviewCard.Trigger.Props) {
|
|
11
|
+
return <PreviewCard.Trigger data-slot="hover-card-trigger" {...props} />;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function HoverCardContent({
|
|
15
|
+
className,
|
|
16
|
+
side = "bottom",
|
|
17
|
+
sideOffset = 6,
|
|
18
|
+
align = "center",
|
|
19
|
+
alignOffset = 0,
|
|
20
|
+
...props
|
|
21
|
+
}: PreviewCard.Popup.Props &
|
|
22
|
+
Pick<PreviewCard.Positioner.Props, "align" | "alignOffset" | "side" | "sideOffset">) {
|
|
23
|
+
return (
|
|
24
|
+
<PreviewCard.Portal>
|
|
25
|
+
<PreviewCard.Positioner
|
|
26
|
+
align={align}
|
|
27
|
+
alignOffset={alignOffset}
|
|
28
|
+
side={side}
|
|
29
|
+
sideOffset={sideOffset}
|
|
30
|
+
className="vps-hover-card-positioner"
|
|
31
|
+
>
|
|
32
|
+
<PreviewCard.Popup
|
|
33
|
+
data-slot="hover-card-content"
|
|
34
|
+
{...props}
|
|
35
|
+
className={(state) => cn(
|
|
36
|
+
"vps-hover-card",
|
|
37
|
+
typeof className === "function" ? className(state) : className
|
|
38
|
+
)}
|
|
39
|
+
/>
|
|
40
|
+
</PreviewCard.Positioner>
|
|
41
|
+
</PreviewCard.Portal>
|
|
42
|
+
);
|
|
43
|
+
}
|