vpschatbotwidget 1.0.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/Chatbot.svg +1 -0
- package/README.md +86 -0
- package/dist/assets/index-C0fUDi2s.css +1 -0
- package/dist/assets/index-foGq5_Av.js +159 -0
- package/dist/index.html +17 -0
- package/index.html +16 -0
- package/package.json +47 -0
- package/src/App.tsx +85 -0
- package/src/chatbot/api/chatbot-api.ts +258 -0
- package/src/chatbot/api/chatbot-types.ts +145 -0
- package/src/chatbot/chatbot.test.tsx +274 -0
- package/src/chatbot/config/colors.ts +166 -0
- package/src/chatbot/config/suggestions.ts +21 -0
- package/src/chatbot/core/ChatContext.tsx +43 -0
- package/src/chatbot/core/ChatEmptyState.tsx +21 -0
- package/src/chatbot/core/ChatError.tsx +26 -0
- package/src/chatbot/core/ChatHeader.tsx +108 -0
- package/src/chatbot/core/ChatInput.tsx +103 -0
- package/src/chatbot/core/ChatMarkdown.tsx +87 -0
- package/src/chatbot/core/ChatMessage.tsx +115 -0
- package/src/chatbot/core/ChatMessageList.tsx +47 -0
- package/src/chatbot/core/ChatPanel.tsx +66 -0
- package/src/chatbot/core/ChatProvider.tsx +185 -0
- package/src/chatbot/core/ChatSuggestions.tsx +71 -0
- package/src/chatbot/core/ChatTicketConfirmation.tsx +59 -0
- package/src/chatbot/core/ChatTypingIndicator.tsx +16 -0
- package/src/chatbot/core/ticket-intent.ts +12 -0
- package/src/chatbot/embedded/EmbeddedChatbot.tsx +47 -0
- package/src/chatbot/embedded/EmbeddedChatbotPanel.tsx +56 -0
- package/src/chatbot/embedded/EmbeddedChatbotTrigger.tsx +167 -0
- package/src/chatbot/hooks/useChatbot.ts +5 -0
- package/src/chatbot/index.ts +7 -0
- package/src/chatbot/widget/ChatbotWidget.tsx +43 -0
- package/src/chatbot/widget/ChatbotWidgetPanel.tsx +39 -0
- package/src/chatbot/widget/ChatbotWidgetTrigger.tsx +32 -0
- package/src/components/AgentSummaryCard.tsx +37 -0
- package/src/components/ChatHeader.tsx +142 -0
- package/src/components/ChatInput.tsx +123 -0
- package/src/components/ChatWidget.tsx +295 -0
- package/src/components/ConnectingStatus.tsx +16 -0
- package/src/components/EnvBadge.tsx +103 -0
- package/src/components/FeedbackRatingCard.tsx +123 -0
- package/src/components/MessageItem.tsx +157 -0
- package/src/components/MessageList.tsx +143 -0
- package/src/components/OrderTrackingCard.tsx +104 -0
- package/src/components/QuickPrompts.tsx +46 -0
- package/src/components/ResponseIconMapper.tsx +155 -0
- package/src/components/SourceCitations.tsx +46 -0
- package/src/components/ToolCallBadge.tsx +29 -0
- package/src/components/TypingProgressBar.tsx +113 -0
- package/src/components/ui/message-scroller.tsx +128 -0
- package/src/index.css +550 -0
- package/src/lib/cn.ts +5 -0
- package/src/lib/gibberishDetector.ts +159 -0
- package/src/lib/textHighlighter.tsx +206 -0
- package/src/main.tsx +10 -0
- package/src/services/api.ts +74 -0
- package/src/test/message-scroller-stub.tsx +60 -0
- package/src/test/setup.ts +8 -0
- package/src/types/chat.ts +66 -0
- package/src/vite-env.d.ts +14 -0
- package/tsconfig.json +21 -0
- package/vite.config.ts +28 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import React, { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { ArrowUp } from "lucide-react";
|
|
3
|
+
import { useRestoreInputFocus } from "./ChatHeader";
|
|
4
|
+
import { ChatSuggestions, filterSuggestions } from "./ChatSuggestions";
|
|
5
|
+
import { useChatbot } from "../hooks/useChatbot";
|
|
6
|
+
import type { Suggestion } from "../api/chatbot-types";
|
|
7
|
+
|
|
8
|
+
interface ChatInputProps {
|
|
9
|
+
onSend: (message: string) => void;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
placeholder?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const ChatInput: React.FC<ChatInputProps> = ({
|
|
15
|
+
onSend,
|
|
16
|
+
disabled = false,
|
|
17
|
+
placeholder = "Ask something…",
|
|
18
|
+
}) => {
|
|
19
|
+
const { suggestions, showSuggestions, suggestionBadgesEnabled } = useChatbot();
|
|
20
|
+
const [text, setText] = useState("");
|
|
21
|
+
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
|
22
|
+
const resize = (element?: HTMLTextAreaElement | null) => {
|
|
23
|
+
if (!element) return;
|
|
24
|
+
element.style.height = "auto";
|
|
25
|
+
element.style.height = `${Math.min(element.scrollHeight, 96)}px`;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
useRestoreInputFocus(disabled, textareaRef);
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
resize(textareaRef.current);
|
|
32
|
+
}, [text]);
|
|
33
|
+
|
|
34
|
+
const visibleSuggestions = useMemo(() => {
|
|
35
|
+
if (!suggestionBadgesEnabled || disabled) return [];
|
|
36
|
+
const typed = text.trim();
|
|
37
|
+
if (!typed) return showSuggestions ? suggestions : [];
|
|
38
|
+
return filterSuggestions(suggestions, typed);
|
|
39
|
+
}, [disabled, showSuggestions, suggestionBadgesEnabled, suggestions, text]);
|
|
40
|
+
|
|
41
|
+
const applySuggestion = (suggestion: Suggestion) => {
|
|
42
|
+
setText(suggestion.query);
|
|
43
|
+
const input = textareaRef.current;
|
|
44
|
+
input?.focus();
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const send = () => {
|
|
48
|
+
const value = text.trim();
|
|
49
|
+
if (!value || disabled) return;
|
|
50
|
+
onSend(value);
|
|
51
|
+
setText("");
|
|
52
|
+
if (textareaRef.current) {
|
|
53
|
+
textareaRef.current.style.height = "auto";
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<div className="bg-card px-4 pb-3 pt-2">
|
|
59
|
+
<ChatSuggestions
|
|
60
|
+
variant="badge"
|
|
61
|
+
suggestions={visibleSuggestions}
|
|
62
|
+
onSelect={applySuggestion}
|
|
63
|
+
disabled={disabled}
|
|
64
|
+
/>
|
|
65
|
+
<label htmlFor="vps-chat-input" className="sr-only">
|
|
66
|
+
Chat message
|
|
67
|
+
</label>
|
|
68
|
+
<div className="flex items-end gap-2 rounded-xl bg-[var(--input-bg)] px-2.5 py-1.5">
|
|
69
|
+
<textarea
|
|
70
|
+
id="vps-chat-input"
|
|
71
|
+
ref={textareaRef}
|
|
72
|
+
rows={1}
|
|
73
|
+
value={text}
|
|
74
|
+
disabled={disabled}
|
|
75
|
+
placeholder={placeholder}
|
|
76
|
+
onChange={(event) => {
|
|
77
|
+
setText(event.target.value);
|
|
78
|
+
resize(event.target);
|
|
79
|
+
}}
|
|
80
|
+
onKeyDown={(event) => {
|
|
81
|
+
if (event.key === "Enter" && !event.shiftKey) {
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
send();
|
|
84
|
+
}
|
|
85
|
+
}}
|
|
86
|
+
className="max-h-24 flex-1 resize-none bg-transparent py-1.5 text-[13.5px] text-foreground outline-none placeholder:text-[var(--text-subtle)] disabled:opacity-60"
|
|
87
|
+
/>
|
|
88
|
+
<button
|
|
89
|
+
type="button"
|
|
90
|
+
onClick={send}
|
|
91
|
+
disabled={disabled || !text.trim()}
|
|
92
|
+
aria-label="Send message"
|
|
93
|
+
className="mb-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-action text-action-foreground hover:bg-[var(--action-hover)] disabled:cursor-not-allowed disabled:opacity-40"
|
|
94
|
+
>
|
|
95
|
+
<ArrowUp size={16} />
|
|
96
|
+
</button>
|
|
97
|
+
</div>
|
|
98
|
+
<p className="mt-2 text-center text-[11px] font-medium text-[var(--text-subtle)]">
|
|
99
|
+
Powered by <span className="text-primary font-bold hover:underline cursor-pointer">VPS Veritas AI</span>
|
|
100
|
+
</p>
|
|
101
|
+
</div>
|
|
102
|
+
);
|
|
103
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
function renderInline(text: string): React.ReactNode[] {
|
|
4
|
+
const parts = text.split(/(\*\*[^*]+\*\*|`[^`]+`|https?:\/\/[^\s]+)/g);
|
|
5
|
+
return parts.map((part, index) => {
|
|
6
|
+
if (part.startsWith("**") && part.endsWith("**")) {
|
|
7
|
+
return <strong key={index}>{part.slice(2, -2)}</strong>;
|
|
8
|
+
}
|
|
9
|
+
if (part.startsWith("`") && part.endsWith("`")) {
|
|
10
|
+
return (
|
|
11
|
+
<code
|
|
12
|
+
key={index}
|
|
13
|
+
className="rounded bg-[var(--input-bg)] px-1 py-0.5 font-mono text-[12px] text-foreground"
|
|
14
|
+
>
|
|
15
|
+
{part.slice(1, -1)}
|
|
16
|
+
</code>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
if (/^https?:\/\//.test(part)) {
|
|
20
|
+
return (
|
|
21
|
+
<a
|
|
22
|
+
key={index}
|
|
23
|
+
href={part}
|
|
24
|
+
target="_blank"
|
|
25
|
+
rel="noopener noreferrer"
|
|
26
|
+
className="underline underline-offset-2"
|
|
27
|
+
>
|
|
28
|
+
{part}
|
|
29
|
+
</a>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return <React.Fragment key={index}>{part}</React.Fragment>;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function ChatMarkdown({ text }: { text: string }) {
|
|
37
|
+
if (!text) return null;
|
|
38
|
+
|
|
39
|
+
const blocks = text.split(/\n\n+/);
|
|
40
|
+
return (
|
|
41
|
+
<>
|
|
42
|
+
{blocks.map((block, blockIndex) => {
|
|
43
|
+
if (block.trim().startsWith("- ") || block.trim().startsWith("* ")) {
|
|
44
|
+
const items = block
|
|
45
|
+
.split(/\n/)
|
|
46
|
+
.filter((line) => line.trim().startsWith("- ") || line.trim().startsWith("* "));
|
|
47
|
+
return (
|
|
48
|
+
<ul key={blockIndex} className="my-1.5 ml-4 list-disc space-y-0.5">
|
|
49
|
+
{items.map((item, itemIndex) => (
|
|
50
|
+
<li key={itemIndex}>{renderInline(item.replace(/^[-*]\s+/, ""))}</li>
|
|
51
|
+
))}
|
|
52
|
+
</ul>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (/^\d+\.\s+/.test(block.trim())) {
|
|
57
|
+
const items = block.split(/\n/).filter((line) => /^\d+\.\s+/.test(line.trim()));
|
|
58
|
+
return (
|
|
59
|
+
<ol key={blockIndex} className="my-1.5 ml-4 list-decimal space-y-0.5">
|
|
60
|
+
{items.map((item, itemIndex) => (
|
|
61
|
+
<li key={itemIndex}>{renderInline(item.replace(/^\d+\.\s+/, ""))}</li>
|
|
62
|
+
))}
|
|
63
|
+
</ol>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (block.startsWith("```")) {
|
|
68
|
+
const content = block.replace(/^```[a-zA-Z]*\n?/, "").replace(/```$/, "");
|
|
69
|
+
return (
|
|
70
|
+
<pre
|
|
71
|
+
key={blockIndex}
|
|
72
|
+
className="my-2 overflow-x-auto rounded-lg bg-[var(--code-bg)] p-3 font-mono text-[12px] text-primary-foreground"
|
|
73
|
+
>
|
|
74
|
+
<code>{content}</code>
|
|
75
|
+
</pre>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<p key={blockIndex} className="mb-2 last:mb-0 whitespace-pre-wrap">
|
|
81
|
+
{renderInline(block)}
|
|
82
|
+
</p>
|
|
83
|
+
);
|
|
84
|
+
})}
|
|
85
|
+
</>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { Bot, Check, ChevronDown, ChevronUp, FileText , BotMessageSquare} from "lucide-react";
|
|
3
|
+
import type { ChatMessage, Source, ToolCall } from "../api/chatbot-types";
|
|
4
|
+
import { cn } from "../../lib/cn";
|
|
5
|
+
import { ChatMarkdown } from "./ChatMarkdown";
|
|
6
|
+
import { ChatTypingIndicator } from "./ChatTypingIndicator";
|
|
7
|
+
|
|
8
|
+
function toolLabel(name: string): string {
|
|
9
|
+
if (name === "create_ticket") return "Support ticket processed";
|
|
10
|
+
if (name === "get_customer") return "Account verified";
|
|
11
|
+
if (name === "get_ticket") return "Ticket status loaded";
|
|
12
|
+
return "Information retrieved";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function Sources({ sources }: { sources: Source[] }) {
|
|
16
|
+
const [open, setOpen] = useState(false);
|
|
17
|
+
if (!sources.length) return null;
|
|
18
|
+
return (
|
|
19
|
+
<div className="mt-2 border-t border-border pt-1.5">
|
|
20
|
+
<button
|
|
21
|
+
type="button"
|
|
22
|
+
className="inline-flex items-center gap-1.5 text-[11.5px] font-semibold text-muted-foreground hover:text-foreground"
|
|
23
|
+
onClick={() => setOpen((value) => !value)}
|
|
24
|
+
aria-expanded={open}
|
|
25
|
+
>
|
|
26
|
+
<FileText size={12} aria-hidden="true" />
|
|
27
|
+
{sources.length} grounded source{sources.length > 1 ? "s" : ""}
|
|
28
|
+
{open ? <ChevronUp size={12} /> : <ChevronDown size={12} />}
|
|
29
|
+
</button>
|
|
30
|
+
{open && (
|
|
31
|
+
<ul className="mt-1.5 space-y-1">
|
|
32
|
+
{sources.map((source) => (
|
|
33
|
+
<li
|
|
34
|
+
key={source.chunkId}
|
|
35
|
+
className="flex items-center justify-between rounded-md border border-border bg-muted px-2 py-1 text-[11px] text-muted-foreground"
|
|
36
|
+
>
|
|
37
|
+
<span className="truncate" title={source.source}>
|
|
38
|
+
{source.source.split(/[/\\]/).pop() || source.source}
|
|
39
|
+
</span>
|
|
40
|
+
<span className="ml-2 font-semibold text-success">
|
|
41
|
+
{Math.round(source.score * 100)}%
|
|
42
|
+
</span>
|
|
43
|
+
</li>
|
|
44
|
+
))}
|
|
45
|
+
</ul>
|
|
46
|
+
)}
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function Tools({ toolCalls }: { toolCalls: ToolCall[] }) {
|
|
52
|
+
if (!toolCalls.length) return null;
|
|
53
|
+
return (
|
|
54
|
+
<div className="mt-2 flex flex-wrap gap-1.5">
|
|
55
|
+
{toolCalls.map((call, index) => (
|
|
56
|
+
<span
|
|
57
|
+
key={`${call.toolName}-${index}`}
|
|
58
|
+
className="inline-flex items-center rounded-full border border-border bg-muted px-2 py-0.5 text-[11px] text-muted-foreground"
|
|
59
|
+
>
|
|
60
|
+
{toolLabel(call.toolName)}
|
|
61
|
+
</span>
|
|
62
|
+
))}
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface ChatMessageProps {
|
|
68
|
+
message: ChatMessage;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const ChatMessageBubble: React.FC<ChatMessageProps> = ({ message }) => {
|
|
72
|
+
const isUser = message.role === "user";
|
|
73
|
+
const isError = message.status === "error";
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<div
|
|
77
|
+
className={cn("flex items-start gap-2.5", isUser && "flex-row-reverse")}
|
|
78
|
+
>
|
|
79
|
+
{!isUser && (
|
|
80
|
+
<div
|
|
81
|
+
className="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-primary text-primary-foreground"
|
|
82
|
+
aria-hidden="true"
|
|
83
|
+
>
|
|
84
|
+
<BotMessageSquare size={15} />
|
|
85
|
+
</div>
|
|
86
|
+
)}
|
|
87
|
+
<div className={cn("flex max-w-[82%] flex-col", isUser && "items-end")}>
|
|
88
|
+
<div
|
|
89
|
+
className={cn(
|
|
90
|
+
"rounded-2xl px-3.5 py-2.5 text-[13.5px] leading-[1.55]",
|
|
91
|
+
isUser && "rounded-tr-sm bg-[var(--bubble-user-bg)] text-[var(--bubble-user-text)] font-medium",
|
|
92
|
+
!isUser &&
|
|
93
|
+
!isError &&
|
|
94
|
+
"rounded-tl-sm border border-border bg-[var(--bubble-bot-message-bg)] text-foreground",
|
|
95
|
+
isError &&
|
|
96
|
+
"rounded-tl-sm border border-[var(--error-border)] bg-[var(--error-bg)] text-[var(--error-text)]"
|
|
97
|
+
)}
|
|
98
|
+
>
|
|
99
|
+
{message.status === "pending" ? (
|
|
100
|
+
<ChatTypingIndicator />
|
|
101
|
+
) : isUser ? (
|
|
102
|
+
<p className="whitespace-pre-wrap">{message.content}</p>
|
|
103
|
+
) : (
|
|
104
|
+
<ChatMarkdown text={message.content} />
|
|
105
|
+
)}
|
|
106
|
+
{message.toolCalls ? <Tools toolCalls={message.toolCalls} /> : null}
|
|
107
|
+
{message.sources ? <Sources sources={message.sources} /> : null}
|
|
108
|
+
</div>
|
|
109
|
+
{isUser && message.status !== "error" && (
|
|
110
|
+
<Check size={13} className="mt-1 text-success" aria-hidden="true" />
|
|
111
|
+
)}
|
|
112
|
+
</div>
|
|
113
|
+
</div>
|
|
114
|
+
);
|
|
115
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import {
|
|
3
|
+
MessageScroller,
|
|
4
|
+
MessageScrollerButton,
|
|
5
|
+
MessageScrollerContent,
|
|
6
|
+
MessageScrollerItem,
|
|
7
|
+
MessageScrollerProvider,
|
|
8
|
+
MessageScrollerViewport,
|
|
9
|
+
} from "../../components/ui/message-scroller";
|
|
10
|
+
import { useChatbot } from "../hooks/useChatbot";
|
|
11
|
+
import { ChatEmptyState } from "./ChatEmptyState";
|
|
12
|
+
import { ChatMessageBubble } from "./ChatMessage";
|
|
13
|
+
|
|
14
|
+
export const ChatMessageList: React.FC = () => {
|
|
15
|
+
const { messages, status, showSuggestions } = useChatbot();
|
|
16
|
+
const isBusy = status === "awaiting" || status === "connecting";
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<MessageScrollerProvider
|
|
20
|
+
autoScroll
|
|
21
|
+
defaultScrollPosition="last-anchor"
|
|
22
|
+
scrollPreviousItemPeek={64}
|
|
23
|
+
>
|
|
24
|
+
<MessageScroller>
|
|
25
|
+
<MessageScrollerViewport aria-label="Chat messages">
|
|
26
|
+
<MessageScrollerContent aria-busy={isBusy}>
|
|
27
|
+
{showSuggestions && (
|
|
28
|
+
<MessageScrollerItem messageId="welcome">
|
|
29
|
+
<ChatEmptyState />
|
|
30
|
+
</MessageScrollerItem>
|
|
31
|
+
)}
|
|
32
|
+
{messages.map((message) => (
|
|
33
|
+
<MessageScrollerItem
|
|
34
|
+
key={message.id}
|
|
35
|
+
messageId={message.id}
|
|
36
|
+
scrollAnchor={message.role === "user"}
|
|
37
|
+
>
|
|
38
|
+
<ChatMessageBubble message={message} />
|
|
39
|
+
</MessageScrollerItem>
|
|
40
|
+
))}
|
|
41
|
+
</MessageScrollerContent>
|
|
42
|
+
</MessageScrollerViewport>
|
|
43
|
+
<MessageScrollerButton />
|
|
44
|
+
</MessageScroller>
|
|
45
|
+
</MessageScrollerProvider>
|
|
46
|
+
);
|
|
47
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { useChatbot } from "../hooks/useChatbot";
|
|
3
|
+
import { ChatError } from "./ChatError";
|
|
4
|
+
import { ChatHeader } from "./ChatHeader";
|
|
5
|
+
import { ChatInput } from "./ChatInput";
|
|
6
|
+
import { ChatMessageList } from "./ChatMessageList";
|
|
7
|
+
import { ChatTicketConfirmation } from "./ChatTicketConfirmation";
|
|
8
|
+
import { cn } from "../../lib/cn";
|
|
9
|
+
import { type EnvStage } from "../../components/EnvBadge";
|
|
10
|
+
|
|
11
|
+
interface ChatPanelProps {
|
|
12
|
+
className?: string;
|
|
13
|
+
onClose?: () => void;
|
|
14
|
+
showMinimize?: boolean;
|
|
15
|
+
title?: string;
|
|
16
|
+
subtitle?: string;
|
|
17
|
+
/** Environment badge stage. Omit for production. */
|
|
18
|
+
envStage?: EnvStage;
|
|
19
|
+
envPulse?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const ChatPanel: React.FC<ChatPanelProps> = ({
|
|
23
|
+
className,
|
|
24
|
+
onClose,
|
|
25
|
+
showMinimize,
|
|
26
|
+
title,
|
|
27
|
+
subtitle,
|
|
28
|
+
envStage,
|
|
29
|
+
envPulse = true,
|
|
30
|
+
}) => {
|
|
31
|
+
const { sendMessage, status, error, resetConversation, retryLast, pendingTicket } =
|
|
32
|
+
useChatbot();
|
|
33
|
+
const inputDisabled =
|
|
34
|
+
status === "awaiting" || status === "connecting" || Boolean(pendingTicket);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<section
|
|
38
|
+
className={cn(
|
|
39
|
+
"flex h-full min-h-0 flex-col overflow-hidden bg-card",
|
|
40
|
+
className
|
|
41
|
+
)}
|
|
42
|
+
aria-label="VPS AI Assistant"
|
|
43
|
+
>
|
|
44
|
+
<ChatHeader
|
|
45
|
+
title={title}
|
|
46
|
+
subtitle={subtitle}
|
|
47
|
+
onClose={onClose}
|
|
48
|
+
onReset={() => void resetConversation()}
|
|
49
|
+
showMinimize={showMinimize}
|
|
50
|
+
envStage={envStage}
|
|
51
|
+
envPulse={envPulse}
|
|
52
|
+
/>
|
|
53
|
+
<div className="relative min-h-0 flex-1">
|
|
54
|
+
<ChatMessageList />
|
|
55
|
+
</div>
|
|
56
|
+
{error && status === "error" && (
|
|
57
|
+
<ChatError message={error} onRetry={() => void retryLast()} />
|
|
58
|
+
)}
|
|
59
|
+
<ChatTicketConfirmation />
|
|
60
|
+
<ChatInput
|
|
61
|
+
onSend={(text) => void sendMessage(text)}
|
|
62
|
+
disabled={inputDisabled}
|
|
63
|
+
/>
|
|
64
|
+
</section>
|
|
65
|
+
);
|
|
66
|
+
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import React, { useCallback, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { chatApi } from "../api/chatbot-api";
|
|
3
|
+
import type {
|
|
4
|
+
ChatMessage,
|
|
5
|
+
ChatUserContext,
|
|
6
|
+
Suggestion,
|
|
7
|
+
} from "../api/chatbot-types";
|
|
8
|
+
import { DEFAULT_SUGGESTIONS } from "../config/suggestions";
|
|
9
|
+
import { ChatContext, type ChatUiStatus } from "./ChatContext";
|
|
10
|
+
|
|
11
|
+
export interface ChatProviderProps {
|
|
12
|
+
children: React.ReactNode;
|
|
13
|
+
userContext?: ChatUserContext;
|
|
14
|
+
suggestions?: Suggestion[];
|
|
15
|
+
disableSuggestions?: boolean;
|
|
16
|
+
initialMessage?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function newId(prefix: string): string {
|
|
20
|
+
return `${prefix}-${crypto.randomUUID()}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function friendlyError(err: unknown): string {
|
|
24
|
+
if (err && typeof err === "object" && "message" in err) {
|
|
25
|
+
const message = String((err as { message: unknown }).message);
|
|
26
|
+
if (message && !/traceback|sql|exception|stack|api[_ ]?key|password/i.test(message)) {
|
|
27
|
+
return message;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return "I couldn't process that request right now.";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const ChatProvider: React.FC<ChatProviderProps> = ({
|
|
34
|
+
children,
|
|
35
|
+
userContext = {},
|
|
36
|
+
suggestions = DEFAULT_SUGGESTIONS,
|
|
37
|
+
disableSuggestions = false,
|
|
38
|
+
initialMessage,
|
|
39
|
+
}) => {
|
|
40
|
+
const [sessionId, setSessionId] = useState<string | null>(null);
|
|
41
|
+
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
|
42
|
+
const [status, setStatus] = useState<ChatUiStatus>("idle");
|
|
43
|
+
const [error, setError] = useState<string | null>(null);
|
|
44
|
+
const lastUserTextRef = useRef<string | null>(null);
|
|
45
|
+
const initialSentRef = useRef(false);
|
|
46
|
+
|
|
47
|
+
const ensureSession = useCallback(async (): Promise<string> => {
|
|
48
|
+
if (sessionId) return sessionId;
|
|
49
|
+
setStatus("connecting");
|
|
50
|
+
const created = await chatApi.createSession();
|
|
51
|
+
setSessionId(created.sessionId);
|
|
52
|
+
return created.sessionId;
|
|
53
|
+
}, [sessionId]);
|
|
54
|
+
|
|
55
|
+
const sendToBackend = useCallback(
|
|
56
|
+
async (text: string) => {
|
|
57
|
+
lastUserTextRef.current = text;
|
|
58
|
+
const userMessage: ChatMessage = {
|
|
59
|
+
id: newId("user"),
|
|
60
|
+
role: "user",
|
|
61
|
+
content: text,
|
|
62
|
+
createdAt: new Date().toISOString(),
|
|
63
|
+
status: "complete",
|
|
64
|
+
};
|
|
65
|
+
const pendingId = newId("asst");
|
|
66
|
+
const pendingAssistant: ChatMessage = {
|
|
67
|
+
id: pendingId,
|
|
68
|
+
role: "assistant",
|
|
69
|
+
content: "",
|
|
70
|
+
createdAt: new Date().toISOString(),
|
|
71
|
+
status: "pending",
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
setMessages((prev) => [...prev, userMessage, pendingAssistant]);
|
|
75
|
+
setStatus("awaiting");
|
|
76
|
+
setError(null);
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
const currentSession = await ensureSession();
|
|
80
|
+
const response = await chatApi.sendMessage(currentSession, text);
|
|
81
|
+
setMessages((prev) =>
|
|
82
|
+
prev.map((message) =>
|
|
83
|
+
message.id === pendingId
|
|
84
|
+
? {
|
|
85
|
+
...message,
|
|
86
|
+
id: response.requestId || pendingId,
|
|
87
|
+
content: response.answer,
|
|
88
|
+
status: "complete",
|
|
89
|
+
sources: response.sources,
|
|
90
|
+
toolCalls: response.toolCalls,
|
|
91
|
+
usage: response.usage,
|
|
92
|
+
}
|
|
93
|
+
: message
|
|
94
|
+
)
|
|
95
|
+
);
|
|
96
|
+
setStatus("idle");
|
|
97
|
+
} catch (err) {
|
|
98
|
+
const message = friendlyError(err);
|
|
99
|
+
setMessages((prev) =>
|
|
100
|
+
prev.map((item) =>
|
|
101
|
+
item.id === pendingId
|
|
102
|
+
? { ...item, content: message, status: "error" }
|
|
103
|
+
: item
|
|
104
|
+
)
|
|
105
|
+
);
|
|
106
|
+
setError(message);
|
|
107
|
+
setStatus("error");
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
[ensureSession]
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
const sendMessage = useCallback(
|
|
114
|
+
async (raw: string) => {
|
|
115
|
+
const text = raw.trim();
|
|
116
|
+
if (!text || status === "awaiting" || status === "connecting") return;
|
|
117
|
+
await sendToBackend(text);
|
|
118
|
+
},
|
|
119
|
+
[sendToBackend, status]
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const retryLast = useCallback(async () => {
|
|
123
|
+
const last = lastUserTextRef.current;
|
|
124
|
+
if (!last) return;
|
|
125
|
+
setMessages((prev) => prev.filter((message) => message.status !== "error"));
|
|
126
|
+
await sendToBackend(last);
|
|
127
|
+
}, [sendToBackend]);
|
|
128
|
+
|
|
129
|
+
const confirmTicket = useCallback(async () => {}, []);
|
|
130
|
+
const cancelTicket = useCallback(() => {}, []);
|
|
131
|
+
|
|
132
|
+
const resetConversation = useCallback(async () => {
|
|
133
|
+
if (sessionId) {
|
|
134
|
+
await chatApi.closeSession(sessionId);
|
|
135
|
+
}
|
|
136
|
+
setSessionId(null);
|
|
137
|
+
setMessages([]);
|
|
138
|
+
setStatus("idle");
|
|
139
|
+
setError(null);
|
|
140
|
+
lastUserTextRef.current = null;
|
|
141
|
+
}, [sessionId]);
|
|
142
|
+
|
|
143
|
+
React.useEffect(() => {
|
|
144
|
+
if (!initialMessage || initialSentRef.current) return;
|
|
145
|
+
initialSentRef.current = true;
|
|
146
|
+
void sendMessage(initialMessage);
|
|
147
|
+
}, [initialMessage, sendMessage]);
|
|
148
|
+
|
|
149
|
+
const value = useMemo(
|
|
150
|
+
() => ({
|
|
151
|
+
messages,
|
|
152
|
+
status,
|
|
153
|
+
error,
|
|
154
|
+
sessionId,
|
|
155
|
+
pendingTicket: null,
|
|
156
|
+
ticketBusy: false,
|
|
157
|
+
lastTicket: null,
|
|
158
|
+
suggestions,
|
|
159
|
+
showSuggestions: !disableSuggestions && messages.length === 0,
|
|
160
|
+
suggestionBadgesEnabled: !disableSuggestions,
|
|
161
|
+
userContext,
|
|
162
|
+
sendMessage,
|
|
163
|
+
retryLast,
|
|
164
|
+
confirmTicket,
|
|
165
|
+
cancelTicket,
|
|
166
|
+
resetConversation,
|
|
167
|
+
}),
|
|
168
|
+
[
|
|
169
|
+
messages,
|
|
170
|
+
status,
|
|
171
|
+
error,
|
|
172
|
+
sessionId,
|
|
173
|
+
suggestions,
|
|
174
|
+
disableSuggestions,
|
|
175
|
+
userContext,
|
|
176
|
+
sendMessage,
|
|
177
|
+
retryLast,
|
|
178
|
+
confirmTicket,
|
|
179
|
+
cancelTicket,
|
|
180
|
+
resetConversation,
|
|
181
|
+
]
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>;
|
|
185
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { Suggestion } from "../api/chatbot-types";
|
|
3
|
+
import { cn } from "../../lib/cn";
|
|
4
|
+
|
|
5
|
+
export function filterSuggestions(suggestions: Suggestion[], text: string): Suggestion[] {
|
|
6
|
+
const query = text.trim().toLowerCase();
|
|
7
|
+
if (!query) return suggestions;
|
|
8
|
+
return suggestions.filter(
|
|
9
|
+
(item) =>
|
|
10
|
+
item.label.toLowerCase().includes(query) || item.query.toLowerCase().includes(query)
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ChatSuggestionsProps {
|
|
15
|
+
suggestions: Suggestion[];
|
|
16
|
+
onSelect: (suggestion: Suggestion) => void;
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
variant?: "list" | "badge";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const ChatSuggestions: React.FC<ChatSuggestionsProps> = ({
|
|
22
|
+
suggestions,
|
|
23
|
+
onSelect,
|
|
24
|
+
disabled,
|
|
25
|
+
variant = "list",
|
|
26
|
+
}) => {
|
|
27
|
+
if (!suggestions.length) return null;
|
|
28
|
+
|
|
29
|
+
if (variant === "badge") {
|
|
30
|
+
return (
|
|
31
|
+
<ul
|
|
32
|
+
className="mb-2 flex flex-wrap gap-1.5"
|
|
33
|
+
aria-label="Suggested questions"
|
|
34
|
+
>
|
|
35
|
+
{suggestions.map((item) => (
|
|
36
|
+
<li key={item.query} className="max-w-full">
|
|
37
|
+
<button
|
|
38
|
+
type="button"
|
|
39
|
+
disabled={disabled}
|
|
40
|
+
onClick={() => onSelect(item)}
|
|
41
|
+
title={item.query}
|
|
42
|
+
className={cn(
|
|
43
|
+
"max-w-full truncate rounded-full border border-border bg-card px-2.5 py-1 text-left text-[11.5px] font-semibold text-foreground",
|
|
44
|
+
"hover:border-primary hover:bg-[var(--tint-solus-10)] hover:text-primary",
|
|
45
|
+
"disabled:cursor-not-allowed disabled:opacity-50"
|
|
46
|
+
)}
|
|
47
|
+
>
|
|
48
|
+
{item.label}
|
|
49
|
+
</button>
|
|
50
|
+
</li>
|
|
51
|
+
))}
|
|
52
|
+
</ul>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<div className="mt-3 flex flex-col gap-2">
|
|
58
|
+
{suggestions.map((item) => (
|
|
59
|
+
<button
|
|
60
|
+
key={item.query}
|
|
61
|
+
type="button"
|
|
62
|
+
disabled={disabled}
|
|
63
|
+
onClick={() => onSelect(item)}
|
|
64
|
+
className="rounded-lg border border-border bg-card px-3.5 py-2.5 text-left text-[13px] font-semibold text-foreground shadow-[0_1px_3px_rgba(0,0,0,0.02)] transition hover:-translate-y-px hover:border-vps-light-grey hover:bg-muted disabled:cursor-not-allowed disabled:opacity-50 motion-reduce:transition-none motion-reduce:hover:translate-y-0"
|
|
65
|
+
>
|
|
66
|
+
{item.label}
|
|
67
|
+
</button>
|
|
68
|
+
))}
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
};
|