vpschatbotwidget 1.0.3 → 1.1.0
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/dist-lib/types/App.d.ts +1 -0
- package/dist-lib/types/chatbot/api/chatbot-api.d.ts +10 -2
- package/dist-lib/types/chatbot/api/chatbot-types.d.ts +21 -0
- package/dist-lib/types/chatbot/core/AgentReasoning.d.ts +9 -0
- package/dist-lib/types/chatbot/core/ChatContext.d.ts +2 -0
- package/dist-lib/types/chatbot/core/ChatMarkdown.d.ts +2 -2
- package/dist-lib/types/chatbot/core/ChatProvider.d.ts +13 -1
- package/dist-lib/types/chatbot/core/ChatTypingIndicator.d.ts +1 -0
- package/dist-lib/types/chatbot/core/GroundedSources.d.ts +14 -0
- package/dist-lib/types/chatbot/embedded/EmbeddedChatbot.d.ts +13 -1
- package/dist-lib/types/chatbot/index.d.ts +1 -1
- package/dist-lib/types/chatbot/widget/ChatbotWidget.d.ts +13 -1
- package/dist-lib/vps-chatbot.cjs +33 -29
- package/dist-lib/vps-chatbot.js +1198 -967
- package/package.json +1 -1
- package/src/chatbot/api/chatbot-api.ts +105 -3
- package/src/chatbot/api/chatbot-types.ts +23 -0
- package/src/chatbot/core/AgentReasoning.tsx +60 -0
- package/src/chatbot/core/ChatContext.tsx +2 -0
- package/src/chatbot/core/ChatMarkdown.tsx +279 -54
- package/src/chatbot/core/ChatMessage.tsx +41 -45
- package/src/chatbot/core/ChatMessageList.tsx +1 -1
- package/src/chatbot/core/ChatProvider.tsx +109 -22
- package/src/chatbot/core/ChatTypingIndicator.tsx +16 -5
- package/src/chatbot/core/GroundedSources.tsx +181 -0
- package/src/chatbot/embedded/EmbeddedChatbot.tsx +33 -4
- package/src/chatbot/index.ts +1 -0
- package/src/chatbot/widget/ChatbotWidget.tsx +33 -4
- package/src/index.css +1051 -68
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import React
|
|
2
|
-
import {
|
|
3
|
-
import type { ChatMessage,
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Check, BotMessageSquare } from "lucide-react";
|
|
3
|
+
import type { ChatMessage, ToolCall } from "../api/chatbot-types";
|
|
4
4
|
import { ChatMarkdown } from "./ChatMarkdown";
|
|
5
5
|
import { ChatTypingIndicator } from "./ChatTypingIndicator";
|
|
6
|
+
import { AgentReasoning } from "./AgentReasoning";
|
|
7
|
+
|
|
8
|
+
import { useOptionalChatContext } from "./ChatContext";
|
|
6
9
|
|
|
7
10
|
function toolLabel(name: string): string {
|
|
8
11
|
if (name === "create_ticket") return "Support ticket processed";
|
|
@@ -11,42 +14,6 @@ function toolLabel(name: string): string {
|
|
|
11
14
|
return "Information retrieved";
|
|
12
15
|
}
|
|
13
16
|
|
|
14
|
-
function Sources({ sources }: { sources: Source[] }) {
|
|
15
|
-
const [open, setOpen] = useState(false);
|
|
16
|
-
if (!sources.length) return null;
|
|
17
|
-
return (
|
|
18
|
-
<div className="sources-panel">
|
|
19
|
-
<button
|
|
20
|
-
type="button"
|
|
21
|
-
className="sources-toggle-btn"
|
|
22
|
-
onClick={() => setOpen((value) => !value)}
|
|
23
|
-
aria-expanded={open}
|
|
24
|
-
>
|
|
25
|
-
<FileText size={12} aria-hidden="true" />
|
|
26
|
-
{sources.length} grounded source{sources.length > 1 ? "s" : ""}
|
|
27
|
-
{open ? <ChevronUp size={12} /> : <ChevronDown size={12} />}
|
|
28
|
-
</button>
|
|
29
|
-
{open && (
|
|
30
|
-
<ul className="sources-list">
|
|
31
|
-
{sources.map((source) => (
|
|
32
|
-
<li
|
|
33
|
-
key={source.chunkId}
|
|
34
|
-
className="source-item"
|
|
35
|
-
>
|
|
36
|
-
<span className="source-name" title={source.source}>
|
|
37
|
-
{source.source.split(/[/\\]/).pop() || source.source}
|
|
38
|
-
</span>
|
|
39
|
-
<span className="source-score">
|
|
40
|
-
{Math.round(source.score * 100)}%
|
|
41
|
-
</span>
|
|
42
|
-
</li>
|
|
43
|
-
))}
|
|
44
|
-
</ul>
|
|
45
|
-
)}
|
|
46
|
-
</div>
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
17
|
function Tools({ toolCalls }: { toolCalls: ToolCall[] }) {
|
|
51
18
|
if (!toolCalls.length) return null;
|
|
52
19
|
return (
|
|
@@ -68,8 +35,26 @@ interface ChatMessageProps {
|
|
|
68
35
|
}
|
|
69
36
|
|
|
70
37
|
export const ChatMessageBubble: React.FC<ChatMessageProps> = ({ message }) => {
|
|
38
|
+
const chatCtx = useOptionalChatContext();
|
|
39
|
+
const hideInspectSources = chatCtx?.hideInspectSources ?? false;
|
|
40
|
+
const showGroundedSources = chatCtx?.showGroundedSources ?? true;
|
|
41
|
+
|
|
71
42
|
const isUser = message.role === "user";
|
|
72
43
|
const isError = message.status === "error";
|
|
44
|
+
const isPending = message.status === "pending";
|
|
45
|
+
const isWelcome =
|
|
46
|
+
message.id === "welcome-default" ||
|
|
47
|
+
message.id.startsWith("welcome") ||
|
|
48
|
+
message.id === "welcome";
|
|
49
|
+
|
|
50
|
+
// Only actual AI responses with retrieval, tools, or live pending state show the reasoning accordion
|
|
51
|
+
const isRealAssistantQuery = Boolean(
|
|
52
|
+
isPending ||
|
|
53
|
+
(message.sources && message.sources.length > 0) ||
|
|
54
|
+
(message.toolCalls && message.toolCalls.length > 0) ||
|
|
55
|
+
message.usage
|
|
56
|
+
);
|
|
57
|
+
const showReasoning = !isUser && !isError && !isWelcome && isRealAssistantQuery;
|
|
73
58
|
|
|
74
59
|
return (
|
|
75
60
|
<div className={`msg-row${isUser ? " msg-row--user" : ""}`}>
|
|
@@ -87,19 +72,30 @@ export const ChatMessageBubble: React.FC<ChatMessageProps> = ({ message }) => {
|
|
|
87
72
|
isUser
|
|
88
73
|
? "msg-bubble msg-bubble--user"
|
|
89
74
|
: isError
|
|
90
|
-
|
|
91
|
-
|
|
75
|
+
? "msg-bubble msg-bubble--error"
|
|
76
|
+
: "msg-bubble msg-bubble--bot"
|
|
92
77
|
}
|
|
93
78
|
>
|
|
94
|
-
{
|
|
95
|
-
|
|
79
|
+
{/* Agent Reasoning Accordion (ChatGPT / Open WebUI style) only on real queries */}
|
|
80
|
+
{showReasoning && (
|
|
81
|
+
<AgentReasoning
|
|
82
|
+
status={isPending ? "pending" : "complete"}
|
|
83
|
+
progressText={message.progressText}
|
|
84
|
+
sourcesCount={message.sources?.length || 5}
|
|
85
|
+
/>
|
|
86
|
+
)}
|
|
87
|
+
|
|
88
|
+
{isPending ? (
|
|
89
|
+
<ChatTypingIndicator text={message.progressText} />
|
|
96
90
|
) : isUser ? (
|
|
97
91
|
<p className="msg-user-pre">{message.content}</p>
|
|
98
92
|
) : (
|
|
99
|
-
<
|
|
93
|
+
<div className="msg-content-wrapper">
|
|
94
|
+
<ChatMarkdown text={message.content} />
|
|
95
|
+
</div>
|
|
100
96
|
)}
|
|
97
|
+
|
|
101
98
|
{message.toolCalls ? <Tools toolCalls={message.toolCalls} /> : null}
|
|
102
|
-
{message.sources ? <Sources sources={message.sources} /> : null}
|
|
103
99
|
</div>
|
|
104
100
|
{isUser && message.status !== "error" && (
|
|
105
101
|
<Check size={13} className="msg-tick" aria-hidden="true" />
|
|
@@ -24,7 +24,7 @@ export const ChatMessageList: React.FC = () => {
|
|
|
24
24
|
<MessageScroller>
|
|
25
25
|
<MessageScrollerViewport aria-label="Chat messages">
|
|
26
26
|
<MessageScrollerContent aria-busy={isBusy}>
|
|
27
|
-
{
|
|
27
|
+
{messages.length === 0 && (
|
|
28
28
|
<MessageScrollerItem messageId="welcome">
|
|
29
29
|
<ChatEmptyState />
|
|
30
30
|
</MessageScrollerItem>
|
|
@@ -11,10 +11,18 @@ import { ChatContext, type ChatUiStatus } from "./ChatContext";
|
|
|
11
11
|
export interface ChatProviderProps {
|
|
12
12
|
children: React.ReactNode;
|
|
13
13
|
userContext?: ChatUserContext;
|
|
14
|
+
/** Direct role name prop (e.g. "VPS Admin", "Client User", "Captain") */
|
|
15
|
+
role?: string;
|
|
16
|
+
/** Direct role ID prop for server-side RAG access control */
|
|
17
|
+
roleId?: number;
|
|
18
|
+
/** Direct client experience prop ("web" | "mobile" | "admin") */
|
|
19
|
+
client?: "mobile" | "web" | "admin";
|
|
20
|
+
/** Optional custom welcome message to display as initial assistant response */
|
|
21
|
+
welcomeMessage?: string;
|
|
14
22
|
suggestions?: Suggestion[];
|
|
15
23
|
disableSuggestions?: boolean;
|
|
16
24
|
initialMessage?: string;
|
|
17
|
-
/** Backend or proxy API base URL
|
|
25
|
+
/** Backend or proxy API base URL. Defaults to VITE_API_BASE or origin */
|
|
18
26
|
apiBaseUrl?: string;
|
|
19
27
|
/** Alias for apiBaseUrl */
|
|
20
28
|
apiUrl?: string;
|
|
@@ -22,8 +30,23 @@ export interface ChatProviderProps {
|
|
|
22
30
|
proxyUrl?: string;
|
|
23
31
|
/** Optional custom chat API client */
|
|
24
32
|
api?: ChatApiClient;
|
|
33
|
+
/** If true, hides the "Inspect Sources" toggle and details drawer */
|
|
34
|
+
hideInspectSources?: boolean;
|
|
35
|
+
/** Whether to render grounded sources (default: true) */
|
|
36
|
+
showGroundedSources?: boolean;
|
|
25
37
|
}
|
|
26
38
|
|
|
39
|
+
const DEFAULT_WELCOME_TEXT = `Hello! I am your **VPS Veritas assistant**.
|
|
40
|
+
|
|
41
|
+
I can help you with:
|
|
42
|
+
- **Fuel Quality Testing (FQT)** & **Oil Condition Monitoring (OCM)**
|
|
43
|
+
- **Fuel Defect Reporting (FDR)** — report and track bunker defect incidents
|
|
44
|
+
- **Bunker Master** — manage bunkering operations, BDN logs, and surveys
|
|
45
|
+
- **FSC & FSQ E-Forms** — paperless fuel system checks and bunker sample pre-registration
|
|
46
|
+
- **Samplogic** sample logistics, report downloads, and support tickets
|
|
47
|
+
|
|
48
|
+
Feel free to ask a question or select one of the suggested topics below!`;
|
|
49
|
+
|
|
27
50
|
function newId(prefix: string): string {
|
|
28
51
|
return `${prefix}-${crypto.randomUUID()}`;
|
|
29
52
|
}
|
|
@@ -41,6 +64,10 @@ function friendlyError(err: unknown): string {
|
|
|
41
64
|
export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
42
65
|
children,
|
|
43
66
|
userContext = {},
|
|
67
|
+
role,
|
|
68
|
+
roleId,
|
|
69
|
+
client: clientChannel,
|
|
70
|
+
welcomeMessage,
|
|
44
71
|
suggestions = DEFAULT_SUGGESTIONS,
|
|
45
72
|
disableSuggestions = false,
|
|
46
73
|
initialMessage,
|
|
@@ -48,6 +75,8 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
48
75
|
apiUrl,
|
|
49
76
|
proxyUrl,
|
|
50
77
|
api,
|
|
78
|
+
hideInspectSources = false,
|
|
79
|
+
showGroundedSources = true,
|
|
51
80
|
}) => {
|
|
52
81
|
const resolvedBaseUrl = apiBaseUrl ?? apiUrl ?? proxyUrl;
|
|
53
82
|
const client = useMemo(
|
|
@@ -55,8 +84,30 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
55
84
|
[api, resolvedBaseUrl]
|
|
56
85
|
);
|
|
57
86
|
|
|
87
|
+
const effectiveUserContext = useMemo<ChatUserContext>(() => {
|
|
88
|
+
return {
|
|
89
|
+
...userContext,
|
|
90
|
+
...(role !== undefined ? { role } : {}),
|
|
91
|
+
...(roleId !== undefined ? { roleId } : {}),
|
|
92
|
+
...(clientChannel !== undefined ? { client: clientChannel } : {}),
|
|
93
|
+
};
|
|
94
|
+
}, [userContext, role, roleId, clientChannel]);
|
|
95
|
+
|
|
96
|
+
const defaultWelcomeMsg = useMemo<ChatMessage | null>(() => {
|
|
97
|
+
if (welcomeMessage === "") return null;
|
|
98
|
+
return {
|
|
99
|
+
id: "welcome-default",
|
|
100
|
+
role: "assistant",
|
|
101
|
+
content: welcomeMessage || DEFAULT_WELCOME_TEXT,
|
|
102
|
+
createdAt: new Date().toISOString(),
|
|
103
|
+
status: "complete",
|
|
104
|
+
};
|
|
105
|
+
}, [welcomeMessage]);
|
|
106
|
+
|
|
58
107
|
const [sessionId, setSessionId] = useState<string | null>(null);
|
|
59
|
-
const [messages, setMessages] = useState<ChatMessage[]>(
|
|
108
|
+
const [messages, setMessages] = useState<ChatMessage[]>(() => {
|
|
109
|
+
return defaultWelcomeMsg ? [defaultWelcomeMsg] : [];
|
|
110
|
+
});
|
|
60
111
|
const [status, setStatus] = useState<ChatUiStatus>("idle");
|
|
61
112
|
const [error, setError] = useState<string | null>(null);
|
|
62
113
|
const lastUserTextRef = useRef<string | null>(null);
|
|
@@ -70,7 +121,6 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
70
121
|
return created.sessionId;
|
|
71
122
|
}, [sessionId, client]);
|
|
72
123
|
|
|
73
|
-
|
|
74
124
|
const sendToBackend = useCallback(
|
|
75
125
|
async (text: string) => {
|
|
76
126
|
lastUserTextRef.current = text;
|
|
@@ -88,27 +138,58 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
88
138
|
content: "",
|
|
89
139
|
createdAt: new Date().toISOString(),
|
|
90
140
|
status: "pending",
|
|
141
|
+
progressText: "Analyzing question & expanding domain terms ",
|
|
91
142
|
};
|
|
92
143
|
|
|
93
144
|
setMessages((prev) => [...prev, userMessage, pendingAssistant]);
|
|
94
145
|
setStatus("awaiting");
|
|
95
146
|
setError(null);
|
|
96
147
|
|
|
148
|
+
const updateProgress = (stageText: string) => {
|
|
149
|
+
setMessages((prev) =>
|
|
150
|
+
prev.map((message) =>
|
|
151
|
+
message.id === pendingId
|
|
152
|
+
? { ...message, progressText: stageText }
|
|
153
|
+
: message
|
|
154
|
+
)
|
|
155
|
+
);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
97
160
|
try {
|
|
98
161
|
const currentSession = await ensureSession();
|
|
99
|
-
const response =
|
|
162
|
+
const response = client.sendMessageStream
|
|
163
|
+
? await client.sendMessageStream(
|
|
164
|
+
currentSession,
|
|
165
|
+
text,
|
|
166
|
+
{
|
|
167
|
+
roleId: effectiveUserContext.roleId,
|
|
168
|
+
client: effectiveUserContext.client,
|
|
169
|
+
},
|
|
170
|
+
(liveStage) => {
|
|
171
|
+
|
|
172
|
+
updateProgress(liveStage);
|
|
173
|
+
}
|
|
174
|
+
)
|
|
175
|
+
: await client.sendMessage(currentSession, text, {
|
|
176
|
+
roleId: effectiveUserContext.roleId,
|
|
177
|
+
client: effectiveUserContext.client,
|
|
178
|
+
});
|
|
179
|
+
|
|
100
180
|
setMessages((prev) =>
|
|
101
181
|
prev.map((message) =>
|
|
102
182
|
message.id === pendingId
|
|
103
183
|
? {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
184
|
+
...message,
|
|
185
|
+
id: response.requestId || pendingId,
|
|
186
|
+
content: response.answer,
|
|
187
|
+
status: "complete",
|
|
188
|
+
progressText: undefined,
|
|
189
|
+
sources: response.sources,
|
|
190
|
+
toolCalls: response.toolCalls,
|
|
191
|
+
usage: response.usage,
|
|
192
|
+
}
|
|
112
193
|
: message
|
|
113
194
|
)
|
|
114
195
|
);
|
|
@@ -118,7 +199,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
118
199
|
setMessages((prev) =>
|
|
119
200
|
prev.map((item) =>
|
|
120
201
|
item.id === pendingId
|
|
121
|
-
? { ...item, content: message, status: "error" }
|
|
202
|
+
? { ...item, content: message, status: "error", progressText: undefined }
|
|
122
203
|
: item
|
|
123
204
|
)
|
|
124
205
|
);
|
|
@@ -126,7 +207,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
126
207
|
setStatus("error");
|
|
127
208
|
}
|
|
128
209
|
},
|
|
129
|
-
[ensureSession, client]
|
|
210
|
+
[ensureSession, client, effectiveUserContext.client, effectiveUserContext.roleId]
|
|
130
211
|
);
|
|
131
212
|
|
|
132
213
|
const sendMessage = useCallback(
|
|
@@ -145,19 +226,19 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
145
226
|
await sendToBackend(last);
|
|
146
227
|
}, [sendToBackend]);
|
|
147
228
|
|
|
148
|
-
const confirmTicket = useCallback(async () => {}, []);
|
|
149
|
-
const cancelTicket = useCallback(() => {}, []);
|
|
229
|
+
const confirmTicket = useCallback(async () => { }, []);
|
|
230
|
+
const cancelTicket = useCallback(() => { }, []);
|
|
150
231
|
|
|
151
232
|
const resetConversation = useCallback(async () => {
|
|
152
233
|
if (sessionId) {
|
|
153
234
|
await client.closeSession(sessionId);
|
|
154
235
|
}
|
|
155
236
|
setSessionId(null);
|
|
156
|
-
setMessages([]);
|
|
237
|
+
setMessages(defaultWelcomeMsg ? [defaultWelcomeMsg] : []);
|
|
157
238
|
setStatus("idle");
|
|
158
239
|
setError(null);
|
|
159
240
|
lastUserTextRef.current = null;
|
|
160
|
-
}, [sessionId, client]);
|
|
241
|
+
}, [sessionId, client, defaultWelcomeMsg]);
|
|
161
242
|
|
|
162
243
|
React.useEffect(() => {
|
|
163
244
|
if (!initialMessage || initialSentRef.current) return;
|
|
@@ -175,10 +256,15 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
175
256
|
ticketBusy: false,
|
|
176
257
|
lastTicket: null,
|
|
177
258
|
suggestions,
|
|
178
|
-
showSuggestions:
|
|
259
|
+
showSuggestions:
|
|
260
|
+
!disableSuggestions &&
|
|
261
|
+
(messages.length === 0 ||
|
|
262
|
+
(messages.length === 1 && messages[0].id === "welcome-default")),
|
|
179
263
|
suggestionBadgesEnabled: !disableSuggestions,
|
|
180
|
-
userContext,
|
|
264
|
+
userContext: effectiveUserContext,
|
|
181
265
|
apiBaseUrl: client.apiBaseUrl,
|
|
266
|
+
hideInspectSources,
|
|
267
|
+
showGroundedSources,
|
|
182
268
|
sendMessage,
|
|
183
269
|
retryLast,
|
|
184
270
|
confirmTicket,
|
|
@@ -192,8 +278,10 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
192
278
|
sessionId,
|
|
193
279
|
suggestions,
|
|
194
280
|
disableSuggestions,
|
|
195
|
-
|
|
281
|
+
effectiveUserContext,
|
|
196
282
|
client.apiBaseUrl,
|
|
283
|
+
hideInspectSources,
|
|
284
|
+
showGroundedSources,
|
|
197
285
|
sendMessage,
|
|
198
286
|
retryLast,
|
|
199
287
|
confirmTicket,
|
|
@@ -202,6 +290,5 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
|
202
290
|
]
|
|
203
291
|
);
|
|
204
292
|
|
|
205
|
-
|
|
206
293
|
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>;
|
|
207
294
|
};
|
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
+
import { Sparkles } from "lucide-react";
|
|
2
3
|
import { cn } from "../../lib/cn";
|
|
3
4
|
|
|
4
|
-
export const ChatTypingIndicator: React.FC<{
|
|
5
|
+
export const ChatTypingIndicator: React.FC<{
|
|
6
|
+
className?: string;
|
|
7
|
+
text?: string;
|
|
8
|
+
}> = ({ className, text }) => {
|
|
5
9
|
return (
|
|
6
10
|
<div
|
|
7
|
-
className={cn("typing-indicator", className)}
|
|
11
|
+
className={cn("typing-indicator-container", className)}
|
|
8
12
|
role="status"
|
|
9
13
|
aria-label="Assistant is preparing a reply"
|
|
10
14
|
>
|
|
11
|
-
<
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
{/* <div className="typing-indicator">
|
|
16
|
+
<span className="typing-dot" />
|
|
17
|
+
<span className="typing-dot" />
|
|
18
|
+
<span className="typing-dot" />
|
|
19
|
+
</div> */}
|
|
20
|
+
<div className="typing-progress-pill">
|
|
21
|
+
<span className="typing-progress-text">
|
|
22
|
+
{text || "Retrieving knowledge & generating answer ..."}
|
|
23
|
+
</span>
|
|
24
|
+
</div>
|
|
14
25
|
</div>
|
|
15
26
|
);
|
|
16
27
|
};
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { BookOpen, ChevronDown, ChevronUp, ExternalLink, ShieldCheck } from "lucide-react";
|
|
3
|
+
import type { Source } from "../api/chatbot-types";
|
|
4
|
+
|
|
5
|
+
// Map document basenames to corresponding portal routes
|
|
6
|
+
const ROUTE_HINTS: Record<string, { label: string; path: string }> = {
|
|
7
|
+
"fuel-defect-reporting": {
|
|
8
|
+
label: "Fuel Defect Reporting",
|
|
9
|
+
path: "/dashboard/fuel-Defect-Reporting",
|
|
10
|
+
},
|
|
11
|
+
"bunker-master": {
|
|
12
|
+
label: "Bunker Master",
|
|
13
|
+
path: "/dashboard/bunker-master",
|
|
14
|
+
},
|
|
15
|
+
"fsc-fsq-e-forms": {
|
|
16
|
+
label: "Pre-Registration & E-Forms",
|
|
17
|
+
path: "/dashboard/pre-registration",
|
|
18
|
+
},
|
|
19
|
+
"pre-registration-ship": {
|
|
20
|
+
label: "Pre-Registration",
|
|
21
|
+
path: "/dashboard/pre-registration",
|
|
22
|
+
},
|
|
23
|
+
"reports-fuel": {
|
|
24
|
+
label: "Fuel Reports",
|
|
25
|
+
path: "/dashboard/reports",
|
|
26
|
+
},
|
|
27
|
+
"sample-fuel": {
|
|
28
|
+
label: "Sample Status",
|
|
29
|
+
path: "/dashboard/sample-status",
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function getRouteHint(sourcePath: string): { label: string; path: string } | null {
|
|
34
|
+
const filename = sourcePath.split(/[/\\]/).pop() || "";
|
|
35
|
+
const stem = filename.replace(/\.md$/i, "").toLowerCase();
|
|
36
|
+
for (const [key, value] of Object.entries(ROUTE_HINTS)) {
|
|
37
|
+
if (stem.includes(key)) {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function formatDocTitle(source: Source): string {
|
|
45
|
+
if (source.title) return source.title;
|
|
46
|
+
const filename = source.source.split(/[/\\]/).pop() || source.source;
|
|
47
|
+
return filename
|
|
48
|
+
.replace(/\.md$/i, "")
|
|
49
|
+
.replace(/[-_]/g, " ")
|
|
50
|
+
.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface GroundedSourcesProps {
|
|
54
|
+
sources: Source[];
|
|
55
|
+
/** If true, hides the "Inspect Sources" toggle button and details list */
|
|
56
|
+
hideInspectSources?: boolean;
|
|
57
|
+
/** Direct alias or inverted flag (default: true) */
|
|
58
|
+
showInspectSources?: boolean;
|
|
59
|
+
/** Whether the details view is open by default (default: false) */
|
|
60
|
+
defaultExpanded?: boolean;
|
|
61
|
+
/** Maximum number of grounded sources to display (default: 5) */
|
|
62
|
+
maxSources?: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const GroundedSources: React.FC<GroundedSourcesProps> = ({
|
|
66
|
+
sources,
|
|
67
|
+
hideInspectSources = false,
|
|
68
|
+
showInspectSources = true,
|
|
69
|
+
defaultExpanded = false,
|
|
70
|
+
maxSources = 5,
|
|
71
|
+
}) => {
|
|
72
|
+
const shouldHideInspect = hideInspectSources || showInspectSources === false;
|
|
73
|
+
const [expanded, setExpanded] = useState(defaultExpanded && !shouldHideInspect);
|
|
74
|
+
const [activeSnippetIdx, setActiveSnippetIdx] = useState<number | null>(null);
|
|
75
|
+
|
|
76
|
+
if (!sources || !sources.length) return null;
|
|
77
|
+
|
|
78
|
+
// Display top 5 grounded sources as requested
|
|
79
|
+
const topSources = sources.slice(0, maxSources);
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<div className="grounded-sources-card">
|
|
83
|
+
<div className="grounded-sources-header">
|
|
84
|
+
<div className="grounded-sources-badge">
|
|
85
|
+
<ShieldCheck size={13} className="grounded-shield-icon" />
|
|
86
|
+
<span>Grounded in {topSources.length} Verified VPS Sources</span>
|
|
87
|
+
</div>
|
|
88
|
+
{!shouldHideInspect && (
|
|
89
|
+
<button
|
|
90
|
+
type="button"
|
|
91
|
+
className="grounded-toggle-btn"
|
|
92
|
+
onClick={() => setExpanded((prev) => !prev)}
|
|
93
|
+
aria-expanded={expanded}
|
|
94
|
+
>
|
|
95
|
+
<span>{expanded ? "Hide Details" : "Inspect Sources"}</span>
|
|
96
|
+
{expanded ? <ChevronUp size={12} /> : <ChevronDown size={12} />}
|
|
97
|
+
</button>
|
|
98
|
+
)}
|
|
99
|
+
</div>
|
|
100
|
+
|
|
101
|
+
<div className="grounded-pills-row">
|
|
102
|
+
{topSources.map((source, idx) => {
|
|
103
|
+
const title = formatDocTitle(source);
|
|
104
|
+
const scorePct = Math.round(source.score * 100);
|
|
105
|
+
const isSelected = activeSnippetIdx === idx;
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<button
|
|
109
|
+
key={`${source.chunkId}-${idx}`}
|
|
110
|
+
type="button"
|
|
111
|
+
className={`grounded-source-pill${isSelected ? " grounded-source-pill--selected" : ""}`}
|
|
112
|
+
onClick={() => {
|
|
113
|
+
if (!shouldHideInspect) {
|
|
114
|
+
setExpanded(true);
|
|
115
|
+
setActiveSnippetIdx(isSelected ? null : idx);
|
|
116
|
+
}
|
|
117
|
+
}}
|
|
118
|
+
title={source.source}
|
|
119
|
+
>
|
|
120
|
+
<BookOpen size={11} className="grounded-book-icon" />
|
|
121
|
+
<span className="grounded-pill-title">{title}</span>
|
|
122
|
+
<span className="grounded-pill-score">{scorePct}% match</span>
|
|
123
|
+
</button>
|
|
124
|
+
);
|
|
125
|
+
})}
|
|
126
|
+
</div>
|
|
127
|
+
|
|
128
|
+
{!shouldHideInspect && expanded && (
|
|
129
|
+
<div className="grounded-sources-details-list">
|
|
130
|
+
{topSources.map((source, idx) => {
|
|
131
|
+
const title = formatDocTitle(source);
|
|
132
|
+
const scorePct = Math.round(source.score * 100);
|
|
133
|
+
const routeHint = getRouteHint(source.source);
|
|
134
|
+
const isHighlighted = activeSnippetIdx === idx;
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<div
|
|
138
|
+
key={`detail-${source.chunkId}-${idx}`}
|
|
139
|
+
className={`grounded-detail-item${isHighlighted ? " grounded-detail-item--highlighted" : ""}`}
|
|
140
|
+
>
|
|
141
|
+
<div className="grounded-detail-top">
|
|
142
|
+
<div className="grounded-detail-title-group">
|
|
143
|
+
<span className="grounded-detail-index">[{idx + 1}]</span>
|
|
144
|
+
<span className="grounded-detail-title">{title}</span>
|
|
145
|
+
</div>
|
|
146
|
+
<div className="grounded-detail-meta">
|
|
147
|
+
<span className="grounded-detail-score">{scorePct}% Match</span>
|
|
148
|
+
{routeHint && (
|
|
149
|
+
<a
|
|
150
|
+
href={routeHint.path}
|
|
151
|
+
onClick={(e) => {
|
|
152
|
+
e.preventDefault();
|
|
153
|
+
window.location.href = routeHint.path;
|
|
154
|
+
}}
|
|
155
|
+
className="grounded-route-link"
|
|
156
|
+
title={`Navigate to ${routeHint.label}`}
|
|
157
|
+
>
|
|
158
|
+
<span>{routeHint.label}</span>
|
|
159
|
+
<ExternalLink size={10} />
|
|
160
|
+
</a>
|
|
161
|
+
)}
|
|
162
|
+
</div>
|
|
163
|
+
</div>
|
|
164
|
+
|
|
165
|
+
<div className="grounded-detail-path">
|
|
166
|
+
<code>{source.source}</code>
|
|
167
|
+
</div>
|
|
168
|
+
|
|
169
|
+
{source.snippet && (
|
|
170
|
+
<p className="grounded-detail-snippet">
|
|
171
|
+
"{source.snippet}"
|
|
172
|
+
</p>
|
|
173
|
+
)}
|
|
174
|
+
</div>
|
|
175
|
+
);
|
|
176
|
+
})}
|
|
177
|
+
</div>
|
|
178
|
+
)}
|
|
179
|
+
</div>
|
|
180
|
+
);
|
|
181
|
+
};
|
|
@@ -15,7 +15,15 @@ export interface EmbeddedChatbotProps {
|
|
|
15
15
|
/** Environment badge stage. Omit for production. */
|
|
16
16
|
envStage?: EnvStage;
|
|
17
17
|
envPulse?: boolean;
|
|
18
|
-
/**
|
|
18
|
+
/** Direct role name (e.g. "VPS Admin", "Client User", "Captain") */
|
|
19
|
+
role?: string;
|
|
20
|
+
/** Direct role ID for server-side RAG access control */
|
|
21
|
+
roleId?: number;
|
|
22
|
+
/** Direct client experience ("web" | "mobile" | "admin") */
|
|
23
|
+
client?: "mobile" | "web" | "admin";
|
|
24
|
+
/** Optional custom initial welcome message */
|
|
25
|
+
welcomeMessage?: string;
|
|
26
|
+
/** Backend or proxy API base URL */
|
|
19
27
|
apiBaseUrl?: string;
|
|
20
28
|
/** Alias for apiBaseUrl */
|
|
21
29
|
apiUrl?: string;
|
|
@@ -29,11 +37,15 @@ export interface EmbeddedChatbotProps {
|
|
|
29
37
|
disableSuggestions?: boolean;
|
|
30
38
|
/** Initial welcome message to trigger on mount */
|
|
31
39
|
initialMessage?: string;
|
|
40
|
+
/** If true, hides the "Inspect Sources" toggle button and drawer in grounded sources */
|
|
41
|
+
hideInspectSources?: boolean;
|
|
42
|
+
/** Whether to show grounded sources (default: true) */
|
|
43
|
+
showGroundedSources?: boolean;
|
|
32
44
|
}
|
|
33
45
|
|
|
34
46
|
const EmbeddedChatbotView: React.FC<Omit<
|
|
35
47
|
EmbeddedChatbotProps,
|
|
36
|
-
"apiBaseUrl" | "apiUrl" | "proxyUrl" | "userContext" | "suggestions" | "disableSuggestions" | "initialMessage"
|
|
48
|
+
"apiBaseUrl" | "apiUrl" | "proxyUrl" | "userContext" | "suggestions" | "disableSuggestions" | "initialMessage" | "hideInspectSources" | "showGroundedSources"
|
|
37
49
|
>> = ({
|
|
38
50
|
defaultOpen = false,
|
|
39
51
|
open,
|
|
@@ -72,22 +84,33 @@ export const EmbeddedChatbot: React.FC<EmbeddedChatbotProps> = ({
|
|
|
72
84
|
apiUrl,
|
|
73
85
|
proxyUrl,
|
|
74
86
|
userContext,
|
|
87
|
+
role,
|
|
88
|
+
roleId,
|
|
89
|
+
client,
|
|
90
|
+
welcomeMessage,
|
|
75
91
|
suggestions,
|
|
76
92
|
disableSuggestions,
|
|
77
93
|
initialMessage,
|
|
94
|
+
hideInspectSources,
|
|
95
|
+
showGroundedSources,
|
|
78
96
|
...viewProps
|
|
79
97
|
}) => {
|
|
80
98
|
const existingContext = useOptionalChatContext();
|
|
81
99
|
const explicitUrl = apiBaseUrl ?? apiUrl ?? proxyUrl;
|
|
82
100
|
|
|
83
|
-
// If already inside ChatProvider and no conflicting apiBaseUrl or userContext override is passed, reuse context
|
|
84
101
|
const canReuseContext =
|
|
85
102
|
Boolean(existingContext) &&
|
|
86
103
|
(!explicitUrl || resolveApiBaseUrl(explicitUrl) === resolveApiBaseUrl(existingContext?.apiBaseUrl)) &&
|
|
87
104
|
!userContext &&
|
|
105
|
+
role === undefined &&
|
|
106
|
+
roleId === undefined &&
|
|
107
|
+
client === undefined &&
|
|
108
|
+
welcomeMessage === undefined &&
|
|
88
109
|
!suggestions &&
|
|
89
110
|
disableSuggestions === undefined &&
|
|
90
|
-
!initialMessage
|
|
111
|
+
!initialMessage &&
|
|
112
|
+
hideInspectSources === undefined &&
|
|
113
|
+
showGroundedSources === undefined;
|
|
91
114
|
|
|
92
115
|
if (canReuseContext) {
|
|
93
116
|
return <EmbeddedChatbotView {...viewProps} />;
|
|
@@ -97,9 +120,15 @@ export const EmbeddedChatbot: React.FC<EmbeddedChatbotProps> = ({
|
|
|
97
120
|
<ChatProvider
|
|
98
121
|
apiBaseUrl={explicitUrl}
|
|
99
122
|
userContext={userContext}
|
|
123
|
+
role={role}
|
|
124
|
+
roleId={roleId}
|
|
125
|
+
client={client}
|
|
126
|
+
welcomeMessage={welcomeMessage}
|
|
100
127
|
suggestions={suggestions}
|
|
101
128
|
disableSuggestions={disableSuggestions}
|
|
102
129
|
initialMessage={initialMessage}
|
|
130
|
+
hideInspectSources={hideInspectSources}
|
|
131
|
+
showGroundedSources={showGroundedSources}
|
|
103
132
|
>
|
|
104
133
|
<EmbeddedChatbotView {...viewProps} />
|
|
105
134
|
</ChatProvider>
|