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
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import React from "react";
|
|
1
|
+
import React, { useRef } from "react";
|
|
2
|
+
import { Marker, MarkerContent } from "../../components/ui/marker";
|
|
2
3
|
import { useChatbot } from "../hooks/useChatbot";
|
|
3
4
|
import { ChatError } from "./ChatError";
|
|
4
5
|
import { ChatHeader } from "./ChatHeader";
|
|
5
|
-
import { ChatInput } from "./ChatInput";
|
|
6
|
+
import { ChatInput, type ChatInputHandle } from "./ChatInput";
|
|
6
7
|
import { ChatMessageList } from "./ChatMessageList";
|
|
7
8
|
import { ChatTicketConfirmation } from "./ChatTicketConfirmation";
|
|
8
9
|
import { type EnvStage } from "../../components/EnvBadge";
|
|
@@ -27,10 +28,14 @@ export const ChatPanel: React.FC<ChatPanelProps> = ({
|
|
|
27
28
|
envStage,
|
|
28
29
|
envPulse = true,
|
|
29
30
|
}) => {
|
|
30
|
-
const { sendMessage, status, error, resetConversation, retryLast, pendingTicket } =
|
|
31
|
+
const { messages, sendMessage, stopResponse, status, error, resetConversation, retryLast, pendingTicket, soundEnabled, toggleSound } =
|
|
31
32
|
useChatbot();
|
|
32
|
-
const
|
|
33
|
-
|
|
33
|
+
const composerRef = useRef<ChatInputHandle>(null);
|
|
34
|
+
const responding = status === "awaiting" || status === "connecting";
|
|
35
|
+
const latestMessage = messages[messages.length - 1];
|
|
36
|
+
const progressText = responding
|
|
37
|
+
? latestMessage?.progressText
|
|
38
|
+
: latestMessage?.status === "stopped" ? "Response stopped" : undefined;
|
|
34
39
|
|
|
35
40
|
return (
|
|
36
41
|
<section
|
|
@@ -43,19 +48,26 @@ export const ChatPanel: React.FC<ChatPanelProps> = ({
|
|
|
43
48
|
onClose={onClose}
|
|
44
49
|
onReset={() => void resetConversation()}
|
|
45
50
|
showMinimize={showMinimize}
|
|
51
|
+
soundEnabled={soundEnabled}
|
|
52
|
+
onToggleSound={toggleSound}
|
|
46
53
|
envStage={envStage}
|
|
47
54
|
envPulse={envPulse}
|
|
48
55
|
/>
|
|
49
56
|
<div className="chat-panel-body">
|
|
50
|
-
<ChatMessageList />
|
|
57
|
+
<ChatMessageList onEdit={(text) => composerRef.current?.edit(text)} />
|
|
51
58
|
</div>
|
|
59
|
+
<Marker className="sr-only" role="status">
|
|
60
|
+
<MarkerContent>{progressText}</MarkerContent>
|
|
61
|
+
</Marker>
|
|
52
62
|
{error && status === "error" && (
|
|
53
63
|
<ChatError message={error} onRetry={() => void retryLast()} />
|
|
54
64
|
)}
|
|
55
65
|
<ChatTicketConfirmation />
|
|
56
66
|
<ChatInput
|
|
67
|
+
ref={composerRef}
|
|
57
68
|
onSend={(text) => void sendMessage(text)}
|
|
58
|
-
|
|
69
|
+
onStop={responding ? stopResponse : undefined}
|
|
70
|
+
disabled={responding || Boolean(pendingTicket)}
|
|
59
71
|
/>
|
|
60
72
|
</section>
|
|
61
73
|
);
|
|
@@ -7,7 +7,8 @@ import type {
|
|
|
7
7
|
ModelOption,
|
|
8
8
|
} from "../api/chatbot-types";
|
|
9
9
|
import { DEFAULT_SUGGESTIONS } from "../config/suggestions";
|
|
10
|
-
import {
|
|
10
|
+
import { useResponseSound } from "../hooks/useResponseSound";
|
|
11
|
+
import { ChatContext, type ChatContextValue, type ChatUiStatus } from "./ChatContext";
|
|
11
12
|
|
|
12
13
|
type RenderTiming = {
|
|
13
14
|
messageId: string;
|
|
@@ -46,9 +47,9 @@ export interface ChatProviderProps {
|
|
|
46
47
|
proxyUrl?: string;
|
|
47
48
|
/** Optional custom chat API client */
|
|
48
49
|
api?: ChatApiClient;
|
|
49
|
-
/**
|
|
50
|
+
/** Hide inline previews and the source list's inspection controls. */
|
|
50
51
|
hideInspectSources?: boolean;
|
|
51
|
-
/** Whether to render grounded sources (default:
|
|
52
|
+
/** Whether to render grounded sources (default: false) */
|
|
52
53
|
showGroundedSources?: boolean;
|
|
53
54
|
/** Configured model alias, or auto for server-side routing. */
|
|
54
55
|
modelId?: string;
|
|
@@ -94,7 +95,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
94
95
|
proxyUrl,
|
|
95
96
|
api,
|
|
96
97
|
hideInspectSources = false,
|
|
97
|
-
showGroundedSources =
|
|
98
|
+
showGroundedSources = false,
|
|
98
99
|
modelId = "auto",
|
|
99
100
|
}) => {
|
|
100
101
|
const resolvedBaseUrl = apiBaseUrl ?? apiUrl ?? proxyUrl;
|
|
@@ -136,6 +137,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
136
137
|
const busyRef = useRef(false);
|
|
137
138
|
const [models, setModels] = useState<ModelOption[]>([]);
|
|
138
139
|
const [selectedModelId, setSelectedModelId] = useState(modelId);
|
|
140
|
+
const { soundEnabled, toggleSound, prepareSound, playSound } = useResponseSound();
|
|
139
141
|
|
|
140
142
|
React.useEffect(() => setSelectedModelId(modelId), [modelId]);
|
|
141
143
|
React.useEffect(() => {
|
|
@@ -183,6 +185,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
183
185
|
const sendToBackend = useCallback(
|
|
184
186
|
async (text: string) => {
|
|
185
187
|
if (busyRef.current) return;
|
|
188
|
+
prepareSound();
|
|
186
189
|
busyRef.current = true;
|
|
187
190
|
const controller = new AbortController();
|
|
188
191
|
activeRequest.current = controller;
|
|
@@ -201,13 +204,16 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
201
204
|
status: "complete",
|
|
202
205
|
};
|
|
203
206
|
const pendingId = newId("asst");
|
|
207
|
+
const initialProgress = "Preparing your answer…";
|
|
204
208
|
const pendingAssistant: ChatMessage = {
|
|
205
209
|
id: pendingId,
|
|
210
|
+
streamId: pendingId,
|
|
206
211
|
role: "assistant",
|
|
207
212
|
content: "",
|
|
208
213
|
createdAt: new Date().toISOString(),
|
|
209
214
|
status: "pending",
|
|
210
|
-
progressText:
|
|
215
|
+
progressText: initialProgress,
|
|
216
|
+
progressSteps: [initialProgress],
|
|
211
217
|
};
|
|
212
218
|
const requestStartedAt = performance.now();
|
|
213
219
|
const timing: RenderTiming = { messageId: pendingId, startedAt: requestStartedAt };
|
|
@@ -218,11 +224,16 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
218
224
|
setError(null);
|
|
219
225
|
|
|
220
226
|
const updateProgress = (stageText: string) => {
|
|
221
|
-
|
|
227
|
+
const stage = stageText.trim();
|
|
228
|
+
if (!stage || controller.signal.aborted) return;
|
|
222
229
|
setMessages((prev) =>
|
|
223
230
|
prev.map((message) =>
|
|
224
|
-
message.id === pendingId
|
|
225
|
-
? {
|
|
231
|
+
message.id === pendingId && message.progressText !== stage
|
|
232
|
+
? {
|
|
233
|
+
...message,
|
|
234
|
+
progressText: stage,
|
|
235
|
+
progressSteps: [...(message.progressSteps ?? []), stage],
|
|
236
|
+
}
|
|
226
237
|
: message
|
|
227
238
|
)
|
|
228
239
|
);
|
|
@@ -290,6 +301,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
290
301
|
)
|
|
291
302
|
);
|
|
292
303
|
setStatus("idle");
|
|
304
|
+
playSound();
|
|
293
305
|
} catch (err) {
|
|
294
306
|
cancelFrame();
|
|
295
307
|
if (renderTiming.current === timing) renderTiming.current = null;
|
|
@@ -312,7 +324,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
312
324
|
}
|
|
313
325
|
}
|
|
314
326
|
},
|
|
315
|
-
[ensureSession, client, effectiveUserContext.client, effectiveUserContext.roleId, selectedModelId]
|
|
327
|
+
[ensureSession, client, effectiveUserContext.client, effectiveUserContext.roleId, selectedModelId, prepareSound, playSound]
|
|
316
328
|
);
|
|
317
329
|
|
|
318
330
|
const sendMessage = useCallback(
|
|
@@ -332,6 +344,32 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
332
344
|
await sendToBackend(last);
|
|
333
345
|
}, [sendToBackend]);
|
|
334
346
|
|
|
347
|
+
const stopResponse = useCallback(() => {
|
|
348
|
+
const controller = activeRequest.current;
|
|
349
|
+
if (!controller) return;
|
|
350
|
+
controller.abort();
|
|
351
|
+
activeRequest.current = null;
|
|
352
|
+
busyRef.current = false;
|
|
353
|
+
renderTiming.current = null;
|
|
354
|
+
setMessages((current) => current.map((message) =>
|
|
355
|
+
message.status === "pending"
|
|
356
|
+
? { ...message, status: "stopped", progressText: undefined }
|
|
357
|
+
: message
|
|
358
|
+
));
|
|
359
|
+
setStatus("idle");
|
|
360
|
+
}, []);
|
|
361
|
+
|
|
362
|
+
const toggleMessageFeedback = useCallback<ChatContextValue["toggleMessageFeedback"]>(
|
|
363
|
+
(messageId, feedback) => {
|
|
364
|
+
setMessages((previous) => previous.map((message) =>
|
|
365
|
+
message.id === messageId
|
|
366
|
+
? { ...message, feedback: message.feedback === feedback ? undefined : feedback }
|
|
367
|
+
: message
|
|
368
|
+
));
|
|
369
|
+
},
|
|
370
|
+
[]
|
|
371
|
+
);
|
|
372
|
+
|
|
335
373
|
const confirmTicket = useCallback(async () => { }, []);
|
|
336
374
|
const cancelTicket = useCallback(() => { }, []);
|
|
337
375
|
|
|
@@ -365,14 +403,20 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
365
403
|
suggestions,
|
|
366
404
|
showSuggestions:
|
|
367
405
|
!disableSuggestions &&
|
|
406
|
+
status === "idle" &&
|
|
368
407
|
(messages.length === 0 ||
|
|
369
|
-
(messages.length === 1 && messages[0].id === "welcome-default")
|
|
408
|
+
(messages.length === 1 && messages[0].id === "welcome-default") ||
|
|
409
|
+
messages.some((m) => m.role === "assistant" && m.status === "complete" && !m.id.startsWith("welcome"))),
|
|
370
410
|
suggestionBadgesEnabled: !disableSuggestions,
|
|
371
411
|
userContext: effectiveUserContext,
|
|
372
412
|
apiBaseUrl: client.apiBaseUrl,
|
|
373
413
|
hideInspectSources,
|
|
374
414
|
showGroundedSources,
|
|
415
|
+
soundEnabled,
|
|
416
|
+
toggleSound,
|
|
375
417
|
sendMessage,
|
|
418
|
+
stopResponse,
|
|
419
|
+
toggleMessageFeedback,
|
|
376
420
|
retryLast,
|
|
377
421
|
confirmTicket,
|
|
378
422
|
cancelTicket,
|
|
@@ -392,7 +436,11 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
392
436
|
client.apiBaseUrl,
|
|
393
437
|
hideInspectSources,
|
|
394
438
|
showGroundedSources,
|
|
439
|
+
soundEnabled,
|
|
440
|
+
toggleSound,
|
|
395
441
|
sendMessage,
|
|
442
|
+
stopResponse,
|
|
443
|
+
toggleMessageFeedback,
|
|
396
444
|
retryLast,
|
|
397
445
|
confirmTicket,
|
|
398
446
|
cancelTicket,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
+
import { Suggestion as SuggestionButton, Suggestions } from "../../components/ai-elements/suggestion";
|
|
2
3
|
import type { Suggestion } from "../api/chatbot-types";
|
|
3
4
|
|
|
4
5
|
export function filterSuggestions(suggestions: Suggestion[], text: string): Suggestion[] {
|
|
@@ -25,42 +26,21 @@ export const ChatSuggestions: React.FC<ChatSuggestionsProps> = ({
|
|
|
25
26
|
}) => {
|
|
26
27
|
if (!suggestions.length) return null;
|
|
27
28
|
|
|
28
|
-
if (variant === "badge") {
|
|
29
|
-
return (
|
|
30
|
-
<ul
|
|
31
|
-
className="suggestions-badge-list"
|
|
32
|
-
aria-label="Suggested questions"
|
|
33
|
-
>
|
|
34
|
-
{suggestions.map((item) => (
|
|
35
|
-
<li key={item.query} className="suggestion-badge-item">
|
|
36
|
-
<button
|
|
37
|
-
type="button"
|
|
38
|
-
disabled={disabled}
|
|
39
|
-
onClick={() => onSelect(item)}
|
|
40
|
-
title={item.query}
|
|
41
|
-
className="suggestion-badge-btn"
|
|
42
|
-
>
|
|
43
|
-
{item.label}
|
|
44
|
-
</button>
|
|
45
|
-
</li>
|
|
46
|
-
))}
|
|
47
|
-
</ul>
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
29
|
return (
|
|
52
|
-
<
|
|
30
|
+
<Suggestions className={variant === "badge" ? "chat-suggestion-strip" : "suggestions-list"}
|
|
31
|
+
role="list" aria-label="Suggested questions">
|
|
53
32
|
{suggestions.map((item) => (
|
|
54
|
-
<
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
33
|
+
<div key={item.query} role="listitem">
|
|
34
|
+
<SuggestionButton
|
|
35
|
+
suggestion={item.query}
|
|
36
|
+
disabled={disabled}
|
|
37
|
+
onClick={() => onSelect(item)}
|
|
38
|
+
title={item.query}
|
|
39
|
+
>
|
|
40
|
+
{item.label}
|
|
41
|
+
</SuggestionButton>
|
|
42
|
+
</div>
|
|
63
43
|
))}
|
|
64
|
-
</
|
|
44
|
+
</Suggestions>
|
|
65
45
|
);
|
|
66
46
|
};
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import React, { useState } from "react";
|
|
2
|
-
import { BookOpen,
|
|
2
|
+
import { BookOpen, ExternalLink, Library } from "lucide-react";
|
|
3
|
+
import {
|
|
4
|
+
Trace,
|
|
5
|
+
TraceCollapsible,
|
|
6
|
+
TraceCollapsibleContent,
|
|
7
|
+
TraceCollapsibleTrigger,
|
|
8
|
+
TraceDescription,
|
|
9
|
+
TraceIcon,
|
|
10
|
+
TraceLine,
|
|
11
|
+
TraceTitle,
|
|
12
|
+
} from "../../components/ui/trace";
|
|
3
13
|
import type { Source } from "../api/chatbot-types";
|
|
4
14
|
|
|
5
15
|
// Map document basenames to corresponding portal routes
|
|
@@ -30,7 +40,7 @@ const ROUTE_HINTS: Record<string, { label: string; path: string }> = {
|
|
|
30
40
|
},
|
|
31
41
|
};
|
|
32
42
|
|
|
33
|
-
function getRouteHint(sourcePath: string): { label: string; path: string } | null {
|
|
43
|
+
export function getRouteHint(sourcePath: string): { label: string; path: string } | null {
|
|
34
44
|
const filename = sourcePath.split(/[/\\]/).pop() || "";
|
|
35
45
|
const stem = filename.replace(/\.md$/i, "").toLowerCase();
|
|
36
46
|
for (const [key, value] of Object.entries(ROUTE_HINTS)) {
|
|
@@ -41,7 +51,7 @@ function getRouteHint(sourcePath: string): { label: string; path: string } | nul
|
|
|
41
51
|
return null;
|
|
42
52
|
}
|
|
43
53
|
|
|
44
|
-
function formatDocTitle(source: Source): string {
|
|
54
|
+
export function formatDocTitle(source: Source): string {
|
|
45
55
|
if (source.title) return source.title;
|
|
46
56
|
const filename = source.source.split(/[/\\]/).pop() || source.source;
|
|
47
57
|
return filename
|
|
@@ -75,105 +85,118 @@ export const GroundedSources: React.FC<GroundedSourcesProps> = ({
|
|
|
75
85
|
|
|
76
86
|
if (!sources || !sources.length) return null;
|
|
77
87
|
|
|
78
|
-
// Display top 5 grounded sources as requested
|
|
79
88
|
const topSources = sources.slice(0, maxSources);
|
|
89
|
+
const sourceLabel = topSources.length === 1 ? "source" : "sources";
|
|
90
|
+
const summary = (
|
|
91
|
+
<TraceLine>
|
|
92
|
+
<TraceIcon><Library size={14} /></TraceIcon>
|
|
93
|
+
<TraceTitle>{`${topSources.length} retrieved VPS ${sourceLabel}`}</TraceTitle>
|
|
94
|
+
{!shouldHideInspect && (
|
|
95
|
+
<TraceDescription>{expanded ? "Hide Details" : "Inspect Sources"}</TraceDescription>
|
|
96
|
+
)}
|
|
97
|
+
</TraceLine>
|
|
98
|
+
);
|
|
80
99
|
|
|
81
|
-
|
|
82
|
-
<
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
<
|
|
87
|
-
|
|
88
|
-
|
|
100
|
+
const sourceRows = (
|
|
101
|
+
<Trace className="grounded-source-rows">
|
|
102
|
+
{topSources.map((source, idx) => {
|
|
103
|
+
const isSelected = activeSnippetIdx === idx;
|
|
104
|
+
const line = (
|
|
105
|
+
<TraceLine>
|
|
106
|
+
<TraceIcon><BookOpen size={13} /></TraceIcon>
|
|
107
|
+
<TraceTitle>[S{idx + 1}] {formatDocTitle(source)}</TraceTitle>
|
|
108
|
+
<TraceDescription title="Ranking score, not a probability of correctness">
|
|
109
|
+
Score {source.score.toFixed(2)}
|
|
110
|
+
</TraceDescription>
|
|
111
|
+
</TraceLine>
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return shouldHideInspect ? (
|
|
115
|
+
<div key={`${source.chunkId}-${idx}`}>{line}</div>
|
|
116
|
+
) : (
|
|
89
117
|
<button
|
|
118
|
+
key={`${source.chunkId}-${idx}`}
|
|
90
119
|
type="button"
|
|
91
|
-
className="grounded-
|
|
92
|
-
|
|
93
|
-
|
|
120
|
+
className={`trace-trigger${expanded && isSelected ? " grounded-source-selected" : ""}`}
|
|
121
|
+
aria-pressed={expanded && isSelected}
|
|
122
|
+
onClick={() => {
|
|
123
|
+
setExpanded(true);
|
|
124
|
+
setActiveSnippetIdx(isSelected ? null : idx);
|
|
125
|
+
}}
|
|
126
|
+
title={source.source}
|
|
94
127
|
>
|
|
95
|
-
|
|
96
|
-
{expanded ? <ChevronUp size={12} /> : <ChevronDown size={12} />}
|
|
128
|
+
{line}
|
|
97
129
|
</button>
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
{topSources.map((source, idx) => {
|
|
103
|
-
const title = formatDocTitle(source);
|
|
104
|
-
const isSelected = activeSnippetIdx === idx;
|
|
130
|
+
);
|
|
131
|
+
})}
|
|
132
|
+
</Trace>
|
|
133
|
+
);
|
|
105
134
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
setActiveSnippetIdx(isSelected ? null : idx);
|
|
115
|
-
}
|
|
116
|
-
}}
|
|
117
|
-
title={source.source}
|
|
118
|
-
>
|
|
119
|
-
<BookOpen size={11} className="grounded-book-icon" />
|
|
120
|
-
<span className="grounded-pill-title">[S{idx + 1}] {title}</span>
|
|
121
|
-
<span className="grounded-pill-score" title="Ranking score, not a probability of correctness">Score {source.score.toFixed(2)}</span>
|
|
122
|
-
</button>
|
|
123
|
-
);
|
|
124
|
-
})}
|
|
125
|
-
</div>
|
|
135
|
+
if (shouldHideInspect) {
|
|
136
|
+
return (
|
|
137
|
+
<Trace className="grounded-sources-card">
|
|
138
|
+
{summary}
|
|
139
|
+
{sourceRows}
|
|
140
|
+
</Trace>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
126
143
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
144
|
+
return (
|
|
145
|
+
<Trace className="grounded-sources-card">
|
|
146
|
+
<TraceCollapsible open={expanded} onOpenChange={setExpanded}>
|
|
147
|
+
<TraceCollapsibleTrigger>{summary}</TraceCollapsibleTrigger>
|
|
148
|
+
{sourceRows}
|
|
149
|
+
<TraceCollapsibleContent>
|
|
150
|
+
<div className="grounded-sources-details-list">
|
|
151
|
+
{topSources.map((source, idx) => {
|
|
152
|
+
const title = formatDocTitle(source);
|
|
153
|
+
const routeHint = getRouteHint(source.source);
|
|
154
|
+
const isHighlighted = activeSnippetIdx === idx;
|
|
133
155
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
156
|
+
return (
|
|
157
|
+
<div
|
|
158
|
+
key={`detail-${source.chunkId}-${idx}`}
|
|
159
|
+
className={`grounded-detail-item${isHighlighted ? " grounded-detail-item--highlighted" : ""}`}
|
|
160
|
+
>
|
|
161
|
+
<div className="grounded-detail-top">
|
|
162
|
+
<div className="grounded-detail-title-group">
|
|
163
|
+
<span className="grounded-detail-index">[{idx + 1}]</span>
|
|
164
|
+
<span className="grounded-detail-title">{title}</span>
|
|
165
|
+
</div>
|
|
166
|
+
<div className="grounded-detail-meta">
|
|
167
|
+
<span className="grounded-detail-score">Rank score {source.score.toFixed(2)}</span>
|
|
168
|
+
{routeHint && (
|
|
169
|
+
<a
|
|
170
|
+
href={routeHint.path}
|
|
171
|
+
onClick={(e) => {
|
|
172
|
+
e.preventDefault();
|
|
173
|
+
window.location.href = routeHint.path;
|
|
174
|
+
}}
|
|
175
|
+
className="grounded-route-link"
|
|
176
|
+
title={`Navigate to ${routeHint.label}`}
|
|
177
|
+
>
|
|
178
|
+
<span>{routeHint.label}</span>
|
|
179
|
+
<ExternalLink size={10} aria-hidden="true" />
|
|
180
|
+
</a>
|
|
181
|
+
)}
|
|
182
|
+
</div>
|
|
143
183
|
</div>
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
{
|
|
147
|
-
<a
|
|
148
|
-
href={routeHint.path}
|
|
149
|
-
onClick={(e) => {
|
|
150
|
-
e.preventDefault();
|
|
151
|
-
window.location.href = routeHint.path;
|
|
152
|
-
}}
|
|
153
|
-
className="grounded-route-link"
|
|
154
|
-
title={`Navigate to ${routeHint.label}`}
|
|
155
|
-
>
|
|
156
|
-
<span>{routeHint.label}</span>
|
|
157
|
-
<ExternalLink size={10} />
|
|
158
|
-
</a>
|
|
159
|
-
)}
|
|
184
|
+
|
|
185
|
+
<div className="grounded-detail-path">
|
|
186
|
+
<code>{source.source}</code>
|
|
160
187
|
</div>
|
|
161
|
-
</div>
|
|
162
188
|
|
|
163
|
-
|
|
164
|
-
|
|
189
|
+
{source.snippet && (
|
|
190
|
+
<p className="grounded-detail-snippet">
|
|
191
|
+
"{source.snippet}"
|
|
192
|
+
</p>
|
|
193
|
+
)}
|
|
165
194
|
</div>
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
</div>
|
|
173
|
-
);
|
|
174
|
-
})}
|
|
175
|
-
</div>
|
|
176
|
-
)}
|
|
177
|
-
</div>
|
|
195
|
+
);
|
|
196
|
+
})}
|
|
197
|
+
</div>
|
|
198
|
+
</TraceCollapsibleContent>
|
|
199
|
+
</TraceCollapsible>
|
|
200
|
+
</Trace>
|
|
178
201
|
);
|
|
179
202
|
};
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import userEvent from "@testing-library/user-event";
|
|
2
3
|
import { describe, expect, it } from "vitest";
|
|
4
|
+
import type { ChatMessage } from "../api/chatbot-types";
|
|
5
|
+
import { Reasoning, ReasoningContent, ReasoningTrigger } from "../../components/ai-elements/reasoning";
|
|
3
6
|
import { ChatMarkdown } from "./ChatMarkdown";
|
|
4
7
|
import { ChatMessageBubble } from "./ChatMessage";
|
|
8
|
+
import { ChatProvider } from "./ChatProvider";
|
|
5
9
|
|
|
6
10
|
describe("grounded answer rendering", () => {
|
|
7
11
|
it("does not render executable markdown links", () => {
|
|
@@ -11,11 +15,114 @@ describe("grounded answer rendering", () => {
|
|
|
11
15
|
expect(screen.getByRole("link", { name: "Portal" })).toHaveAttribute("href", "/dashboard/reports");
|
|
12
16
|
});
|
|
13
17
|
|
|
14
|
-
it("
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
it("expands source details while showing the ranking score as a score", async () => {
|
|
19
|
+
const user = userEvent.setup();
|
|
20
|
+
render(
|
|
21
|
+
<ChatProvider showGroundedSources>
|
|
22
|
+
<ChatMessageBubble message={{ id: "reply", role: "assistant", content: "Fuel reports [S1]", createdAt: "2026-01-01", status: "complete",
|
|
23
|
+
sources: [{ chunkId: "c", source: "knowledge/Web/Reports-Fuel.md", score: 0.65 }] }} />
|
|
24
|
+
</ChatProvider>
|
|
25
|
+
);
|
|
26
|
+
expect(screen.getByText("1 retrieved VPS source")).toBeInTheDocument();
|
|
18
27
|
expect(screen.queryByText(/Verified|65%/i)).toBeNull();
|
|
19
28
|
expect(screen.getByText(/\[S1\] Reports Fuel/)).toBeInTheDocument();
|
|
29
|
+
|
|
30
|
+
const inspect = screen.getByRole("button", { name: /inspect sources/i });
|
|
31
|
+
expect(inspect).toHaveAttribute("aria-expanded", "false");
|
|
32
|
+
inspect.focus();
|
|
33
|
+
await user.keyboard("{Enter}");
|
|
34
|
+
expect(inspect).toHaveAttribute("aria-expanded", "true");
|
|
35
|
+
expect(screen.getByRole("link", { name: "Fuel Reports" })).toHaveAttribute("href", "/dashboard/reports");
|
|
36
|
+
await user.keyboard(" ");
|
|
37
|
+
expect(inspect).toHaveAttribute("aria-expanded", "false");
|
|
38
|
+
expect(screen.queryByRole("link", { name: "Fuel Reports" })).not.toBeInTheDocument();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("opens an inline citation from the keyboard and gives the source link a new-tab label", async () => {
|
|
42
|
+
const user = userEvent.setup();
|
|
43
|
+
render(<ChatMarkdown text="Fuel reports [S1] [S9]" sources={[{
|
|
44
|
+
chunkId: "public",
|
|
45
|
+
source: "knowledge/Web/Reports-Fuel.md",
|
|
46
|
+
title: "Fuel Reports",
|
|
47
|
+
url: "https://example.com/fuel",
|
|
48
|
+
score: 0.65,
|
|
49
|
+
snippet: "Open Reports from the portal.",
|
|
50
|
+
}]} />);
|
|
51
|
+
|
|
52
|
+
const citation = screen.getByRole("button", { name: "View sources [S1]" });
|
|
53
|
+
await user.tab();
|
|
54
|
+
expect(citation).toHaveFocus();
|
|
55
|
+
await user.keyboard("{Enter}");
|
|
56
|
+
const link = await screen.findByRole("link", { name: "example.com (opens in a new tab)" });
|
|
57
|
+
expect(link).toHaveAttribute("target", "_blank");
|
|
58
|
+
expect(link).toHaveAttribute("rel", "noopener noreferrer");
|
|
59
|
+
await user.tab();
|
|
60
|
+
expect(link).toHaveFocus();
|
|
61
|
+
expect(screen.getByText("Fuel Reports")).toBeVisible();
|
|
62
|
+
expect(screen.getByText("Open Reports from the portal.")).toBeVisible();
|
|
63
|
+
expect(screen.getByText("[S9]", { exact: false })).toBeVisible();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("updates the live trace and lets readers expand completed tool activity", async () => {
|
|
67
|
+
const user = userEvent.setup();
|
|
68
|
+
const message: ChatMessage = {
|
|
69
|
+
id: "reply",
|
|
70
|
+
role: "assistant",
|
|
71
|
+
content: "",
|
|
72
|
+
createdAt: "2026-01-01",
|
|
73
|
+
status: "pending",
|
|
74
|
+
progressText: "Writing your answer…",
|
|
75
|
+
progressSteps: ["Searching relevant knowledge…", "Writing your answer…"],
|
|
76
|
+
};
|
|
77
|
+
const { rerender } = render(<ChatMessageBubble message={message} />);
|
|
78
|
+
const running = screen.getByRole("button", { name: "Thinking" });
|
|
79
|
+
expect(running.querySelector('[data-slot="trace-line"]'))
|
|
80
|
+
.toHaveAttribute("data-status", "running");
|
|
81
|
+
expect(screen.getByText("Writing your answer…")).toBeVisible();
|
|
82
|
+
await user.click(running);
|
|
83
|
+
await user.click(running);
|
|
84
|
+
|
|
85
|
+
rerender(<ChatMessageBubble message={{
|
|
86
|
+
...message,
|
|
87
|
+
content: "Your ticket is open.",
|
|
88
|
+
status: "complete",
|
|
89
|
+
durationMs: 1200,
|
|
90
|
+
toolCalls: [{ toolName: "get_ticket", arguments: { id: "123" }, result: {} }],
|
|
91
|
+
}} />);
|
|
92
|
+
const activity = screen.getByRole("button", { name: "Thought for 1.2s" });
|
|
93
|
+
expect(activity).toHaveTextContent("1.2s");
|
|
94
|
+
expect(screen.getByText("Ticket status loaded")).toBeVisible();
|
|
95
|
+
await user.click(activity);
|
|
96
|
+
expect(screen.queryByText("Ticket status loaded")).not.toBeInTheDocument();
|
|
97
|
+
await user.click(activity);
|
|
98
|
+
expect(screen.getByText("Ticket status loaded")).toBeVisible();
|
|
99
|
+
expect(screen.getByText("Your ticket is open.")).toBeVisible();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("copies fenced code without its displayed line numbers", async () => {
|
|
103
|
+
const user = userEvent.setup();
|
|
104
|
+
const code = "first line\nsecond line";
|
|
105
|
+
render(<ChatMarkdown text={`\`\`\`text\n${code}\n\`\`\``} />);
|
|
106
|
+
expect(screen.getByText("2", { selector: ".md-code-number" })).toHaveAttribute("aria-hidden", "true");
|
|
107
|
+
await user.click(screen.getByRole("button", { name: "Copy code" }));
|
|
108
|
+
expect(await navigator.clipboard.readText()).toBe(code);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("renders reasoning text as Markdown and respects the controlling open prop", async () => {
|
|
112
|
+
const { rerender } = render(
|
|
113
|
+
<Reasoning open isStreaming>
|
|
114
|
+
<ReasoningTrigger />
|
|
115
|
+
<ReasoningContent>Checking **reports**.</ReasoningContent>
|
|
116
|
+
</Reasoning>
|
|
117
|
+
);
|
|
118
|
+
expect(screen.getByRole("button", { name: "Thinking" })).toHaveAttribute("aria-expanded", "true");
|
|
119
|
+
expect(await screen.findByText("reports", { selector: '[data-streamdown="strong"]' })).toBeVisible();
|
|
120
|
+
rerender(
|
|
121
|
+
<Reasoning open={false} duration={5}>
|
|
122
|
+
<ReasoningTrigger />
|
|
123
|
+
<ReasoningContent>Checking **reports**.</ReasoningContent>
|
|
124
|
+
</Reasoning>
|
|
125
|
+
);
|
|
126
|
+
expect(screen.getByRole("button", { name: "Thought for 5s" })).toHaveAttribute("aria-expanded", "false");
|
|
20
127
|
});
|
|
21
128
|
});
|
|
@@ -37,9 +37,9 @@ export interface EmbeddedChatbotProps {
|
|
|
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
|
}
|