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
package/dist/index.html
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%2300B4D8'><path d='M12 2a10 10 0 1 0 10 10A10 10 0 0 0 12 2zm1 14.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0zm0-4.5a1 1 0 0 1-2 0V7a1 1 0 0 1 2 0z'/></svg>" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>VPS Veritas AI Customer Assistant</title>
|
|
8
|
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
9
|
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
10
|
+
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
|
|
11
|
+
<script type="module" crossorigin src="/assets/index-foGq5_Av.js"></script>
|
|
12
|
+
<link rel="stylesheet" crossorigin href="/assets/index-C0fUDi2s.css">
|
|
13
|
+
</head>
|
|
14
|
+
<body>
|
|
15
|
+
<div id="root"></div>
|
|
16
|
+
</body>
|
|
17
|
+
</html>
|
package/index.html
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%2300B4D8'><path d='M12 2a10 10 0 1 0 10 10A10 10 0 0 0 12 2zm1 14.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0zm0-4.5a1 1 0 0 1-2 0V7a1 1 0 0 1 2 0z'/></svg>" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>VPS Veritas AI Customer Assistant</title>
|
|
8
|
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
9
|
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
10
|
+
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
<div id="root"></div>
|
|
14
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
15
|
+
</body>
|
|
16
|
+
</html>
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vpschatbotwidget",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "This is an Package for the chatbot widget ui for our customer portel . ",
|
|
6
|
+
"homepage": "https://github.com/dhineshkumar-natobotics/VPS-Chatbot-Widget#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/dhineshkumar-natobotics/VPS-Chatbot-Widget/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/dhineshkumar-natobotics/VPS-Chatbot-Widget.git"
|
|
13
|
+
},
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"author": "Dhineshkuamr Thirupathi",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "vite",
|
|
20
|
+
"build": "tsc && vite build",
|
|
21
|
+
"preview": "vite preview",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"typecheck": "tsc --noEmit"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@shadcn/react": "^0.3.1",
|
|
27
|
+
"clsx": "^2.1.1",
|
|
28
|
+
"lucide-react": "^0.468.0",
|
|
29
|
+
"react": "^19.3.0",
|
|
30
|
+
"react-dom": "^19.3.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@tailwindcss/vite": "^4.3.3",
|
|
34
|
+
"@testing-library/jest-dom": "^7.0.1",
|
|
35
|
+
"@testing-library/react": "^16.3.3",
|
|
36
|
+
"@testing-library/user-event": "^14.6.7",
|
|
37
|
+
"@types/node": "^22.20.3",
|
|
38
|
+
"@types/react": "^19.3.0",
|
|
39
|
+
"@types/react-dom": "^19.3.0",
|
|
40
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
41
|
+
"jsdom": "^29.1.1",
|
|
42
|
+
"tailwindcss": "^4.3.3",
|
|
43
|
+
"typescript": "^5.6.3",
|
|
44
|
+
"vite": "^6.0.1",
|
|
45
|
+
"vitest": "^5.0.1"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/App.tsx
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { LayoutGrid, Layers, MessageSquare } from "lucide-react";
|
|
3
|
+
import {
|
|
4
|
+
ChatProvider,
|
|
5
|
+
ChatbotWidget,
|
|
6
|
+
EmbeddedChatbot,
|
|
7
|
+
type ChatUserContext,
|
|
8
|
+
} from "./chatbot";
|
|
9
|
+
|
|
10
|
+
type DemoMode = "widget" | "embedded";
|
|
11
|
+
|
|
12
|
+
export const App: React.FC = () => {
|
|
13
|
+
const [mode, setMode] = useState<DemoMode>("widget");
|
|
14
|
+
const [customerId, setCustomerId] = useState("");
|
|
15
|
+
const [email, setEmail] = useState("");
|
|
16
|
+
const [widgetOpen, setWidgetOpen] = useState(false);
|
|
17
|
+
const [embeddedOpen, setEmbeddedOpen] = useState(false);
|
|
18
|
+
|
|
19
|
+
const userContext: ChatUserContext = {
|
|
20
|
+
customerId: customerId.trim() || undefined,
|
|
21
|
+
email: email.trim() || undefined,
|
|
22
|
+
page: "portal-demo",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<ChatProvider userContext={userContext}>
|
|
27
|
+
<div className="flex min-h-screen flex-col bg-background">
|
|
28
|
+
<header className="sticky top-0 z-50 flex flex-wrap items-center justify-between gap-3 border-b border-border bg-card px-7 py-3.5 shadow-sm">
|
|
29
|
+
<div className="flex items-center gap-3">
|
|
30
|
+
|
|
31
|
+
<div>
|
|
32
|
+
<h1 className="text-base font-bold text-foreground">
|
|
33
|
+
Veritas AI Assistant
|
|
34
|
+
</h1>
|
|
35
|
+
<p className="text-xs text-muted-foreground">
|
|
36
|
+
Your AI-powered assistant for Veritas
|
|
37
|
+
</p>
|
|
38
|
+
</div>
|
|
39
|
+
</div>
|
|
40
|
+
<div className="flex flex-wrap items-center gap-1.5">
|
|
41
|
+
<button
|
|
42
|
+
type="button"
|
|
43
|
+
className={`inline-flex items-center gap-1.5 rounded-lg border px-3 py-1.5 text-[12.5px] font-semibold ${
|
|
44
|
+
mode === "widget"
|
|
45
|
+
? "border-action bg-action text-action-foreground"
|
|
46
|
+
: "border-border bg-muted text-muted-foreground"
|
|
47
|
+
}`}
|
|
48
|
+
onClick={() => {
|
|
49
|
+
setMode("widget");
|
|
50
|
+
setEmbeddedOpen(false);
|
|
51
|
+
}}
|
|
52
|
+
>
|
|
53
|
+
<Layers size={14} />
|
|
54
|
+
Widget
|
|
55
|
+
</button>
|
|
56
|
+
<button
|
|
57
|
+
type="button"
|
|
58
|
+
className={`inline-flex items-center gap-1.5 rounded-lg border px-3 py-1.5 text-[12.5px] font-semibold ${
|
|
59
|
+
mode === "embedded"
|
|
60
|
+
? "border-action bg-action text-action-foreground"
|
|
61
|
+
: "border-border bg-muted text-muted-foreground"
|
|
62
|
+
}`}
|
|
63
|
+
onClick={() => {
|
|
64
|
+
setMode("embedded");
|
|
65
|
+
setWidgetOpen(false);
|
|
66
|
+
setEmbeddedOpen(true);
|
|
67
|
+
}}
|
|
68
|
+
>
|
|
69
|
+
<LayoutGrid size={14} />
|
|
70
|
+
Slide-out
|
|
71
|
+
</button>
|
|
72
|
+
</div>
|
|
73
|
+
</header>
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
{mode === "widget" && (
|
|
77
|
+
<ChatbotWidget open={widgetOpen} onOpenChange={setWidgetOpen} envStage="alpha" envPulse/>
|
|
78
|
+
)}
|
|
79
|
+
{mode === "embedded" && (
|
|
80
|
+
<EmbeddedChatbot open={embeddedOpen} onOpenChange={setEmbeddedOpen} envStage="alpha" />
|
|
81
|
+
)}
|
|
82
|
+
</div>
|
|
83
|
+
</ChatProvider>
|
|
84
|
+
);
|
|
85
|
+
};
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ApiErrorDto,
|
|
3
|
+
ChatApiError,
|
|
4
|
+
ChatMessage,
|
|
5
|
+
ChatMessageDto,
|
|
6
|
+
ChatResponseDto,
|
|
7
|
+
SessionCreated,
|
|
8
|
+
SessionMessagesResponseDto,
|
|
9
|
+
SessionResponseDto,
|
|
10
|
+
Source,
|
|
11
|
+
SourceDto,
|
|
12
|
+
Ticket,
|
|
13
|
+
ToolCall,
|
|
14
|
+
ToolCallDto,
|
|
15
|
+
TokenUsage,
|
|
16
|
+
TokenUsageDto,
|
|
17
|
+
} from "./chatbot-types";
|
|
18
|
+
|
|
19
|
+
export function resolveApiBaseUrl(customUrl?: string): string {
|
|
20
|
+
if (customUrl !== undefined && customUrl !== null && customUrl.trim() !== "") {
|
|
21
|
+
return customUrl.trim().replace(/\/+$/, "");
|
|
22
|
+
}
|
|
23
|
+
const envUrl =
|
|
24
|
+
typeof import.meta !== "undefined" && import.meta?.env
|
|
25
|
+
? (import.meta.env.VITE_API_BASE ??
|
|
26
|
+
import.meta.env.VITE_API_BASE_URL ??
|
|
27
|
+
import.meta.env.VITE_API_URL ??
|
|
28
|
+
"")
|
|
29
|
+
: "";
|
|
30
|
+
return String(envUrl).trim().replace(/\/+$/, "");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function buildApiUrl(baseUrl: string | undefined, path: string): string {
|
|
34
|
+
const base = resolveApiBaseUrl(baseUrl);
|
|
35
|
+
if (!base) {
|
|
36
|
+
return path;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Handle case where base ends with /api and path starts with /api/
|
|
40
|
+
if (base.endsWith("/api") && path.startsWith("/api/")) {
|
|
41
|
+
return `${base}${path.slice(4)}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Handle /health endpoint when base ends with /api
|
|
45
|
+
// FastApi serves /health at root level (not /api/health)
|
|
46
|
+
if (base.endsWith("/api") && path === "/health") {
|
|
47
|
+
const root = base.slice(0, -4);
|
|
48
|
+
return root ? `${root}/health` : "/health";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const cleanPath = path.startsWith("/") ? path : `/${path}`;
|
|
52
|
+
return `${base}${cleanPath}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function unwrapData<T>(payload: unknown): T {
|
|
56
|
+
if (
|
|
57
|
+
payload &&
|
|
58
|
+
typeof payload === "object" &&
|
|
59
|
+
"data" in payload &&
|
|
60
|
+
(payload as { success?: boolean }).success !== false
|
|
61
|
+
) {
|
|
62
|
+
return (payload as { data: T }).data;
|
|
63
|
+
}
|
|
64
|
+
return payload as T;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function parseApiError(payload: unknown, fallback: string, status: number): ChatApiError {
|
|
68
|
+
const dto = payload as ApiErrorDto | undefined;
|
|
69
|
+
const message = dto?.error?.message;
|
|
70
|
+
if (message && !looksInternal(message)) {
|
|
71
|
+
return {
|
|
72
|
+
code: dto?.error?.code,
|
|
73
|
+
message,
|
|
74
|
+
requestId: dto?.error?.request_id,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (status >= 500) {
|
|
78
|
+
return { message: "I couldn't process that request right now." };
|
|
79
|
+
}
|
|
80
|
+
if (status === 0) {
|
|
81
|
+
return { message: "Something went wrong while connecting. Please try again." };
|
|
82
|
+
}
|
|
83
|
+
return { message: fallback };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function looksInternal(message: string): boolean {
|
|
87
|
+
return /traceback|sql|exception|stack|api[_ ]?key|password/i.test(message);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function normalizeSource(dto: SourceDto): Source {
|
|
91
|
+
return {
|
|
92
|
+
chunkId: dto.chunk_id,
|
|
93
|
+
source: dto.source,
|
|
94
|
+
score: dto.score,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function normalizeToolCall(dto: ToolCallDto): ToolCall {
|
|
99
|
+
return {
|
|
100
|
+
toolName: dto.tool_name,
|
|
101
|
+
arguments: dto.arguments ?? {},
|
|
102
|
+
result: dto.result ?? {},
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function normalizeUsage(dto: TokenUsageDto): TokenUsage {
|
|
107
|
+
return {
|
|
108
|
+
model: dto.model,
|
|
109
|
+
promptTokens: dto.prompt_tokens,
|
|
110
|
+
completionTokens: dto.completion_tokens,
|
|
111
|
+
totalTokens: dto.total_tokens,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function normalizeChatMessage(dto: ChatMessageDto): ChatMessage {
|
|
116
|
+
const role = dto.role === "user" || dto.role === "system" ? dto.role : "assistant";
|
|
117
|
+
return {
|
|
118
|
+
id: String(dto.id),
|
|
119
|
+
role,
|
|
120
|
+
content: dto.content,
|
|
121
|
+
createdAt: dto.created_at || new Date().toISOString(),
|
|
122
|
+
status: "complete",
|
|
123
|
+
usage: dto.model
|
|
124
|
+
? {
|
|
125
|
+
model: dto.model,
|
|
126
|
+
promptTokens: dto.prompt_tokens || 0,
|
|
127
|
+
completionTokens: dto.completion_tokens || 0,
|
|
128
|
+
totalTokens: (dto.prompt_tokens || 0) + (dto.completion_tokens || 0),
|
|
129
|
+
}
|
|
130
|
+
: undefined,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function readJson(response: Response): Promise<unknown> {
|
|
135
|
+
const text = await response.text();
|
|
136
|
+
if (!text) return {};
|
|
137
|
+
try {
|
|
138
|
+
return JSON.parse(text);
|
|
139
|
+
} catch {
|
|
140
|
+
return {};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface ChatApiClient {
|
|
145
|
+
apiBaseUrl: string;
|
|
146
|
+
createSession(): Promise<SessionCreated>;
|
|
147
|
+
sendMessage(sessionId: string, message: string): Promise<{
|
|
148
|
+
requestId: string;
|
|
149
|
+
sessionId: string;
|
|
150
|
+
answer: string;
|
|
151
|
+
sources: Source[];
|
|
152
|
+
toolCalls: ToolCall[];
|
|
153
|
+
usage: TokenUsage;
|
|
154
|
+
}>;
|
|
155
|
+
getHistory(sessionId: string): Promise<ChatMessage[]>;
|
|
156
|
+
closeSession(sessionId: string): Promise<void>;
|
|
157
|
+
createTicket(): Promise<Ticket>;
|
|
158
|
+
checkHealth(): Promise<boolean>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function createChatApi(initialBaseUrl?: string): ChatApiClient {
|
|
162
|
+
const baseUrl = resolveApiBaseUrl(initialBaseUrl);
|
|
163
|
+
|
|
164
|
+
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
|
165
|
+
let response: Response;
|
|
166
|
+
const url = buildApiUrl(baseUrl, path);
|
|
167
|
+
try {
|
|
168
|
+
response = await fetch(url, {
|
|
169
|
+
...init,
|
|
170
|
+
headers: {
|
|
171
|
+
"Content-Type": "application/json",
|
|
172
|
+
...(init?.headers ?? {}),
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
} catch {
|
|
176
|
+
throw { message: "Something went wrong while connecting. Please try again." } satisfies ChatApiError;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (response.status === 204) {
|
|
180
|
+
return undefined as T;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const payload = await readJson(response);
|
|
184
|
+
if (!response.ok) {
|
|
185
|
+
throw parseApiError(
|
|
186
|
+
payload,
|
|
187
|
+
"I couldn't process that request right now.",
|
|
188
|
+
response.status
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
return unwrapData<T>(payload);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
apiBaseUrl: baseUrl,
|
|
196
|
+
async createSession(): Promise<SessionCreated> {
|
|
197
|
+
const dto = await request<SessionResponseDto>("/api/v1/sessions", {
|
|
198
|
+
method: "POST",
|
|
199
|
+
body: JSON.stringify({}),
|
|
200
|
+
});
|
|
201
|
+
return { sessionId: dto.session_id, createdAt: dto.created_at };
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
async sendMessage(sessionId: string, message: string) {
|
|
205
|
+
const dto = await request<ChatResponseDto>(
|
|
206
|
+
`/api/v1/sessions/${sessionId}/messages`,
|
|
207
|
+
{
|
|
208
|
+
method: "POST",
|
|
209
|
+
body: JSON.stringify({ message }),
|
|
210
|
+
}
|
|
211
|
+
);
|
|
212
|
+
return {
|
|
213
|
+
requestId: dto.request_id,
|
|
214
|
+
sessionId: dto.session_id,
|
|
215
|
+
answer: dto.answer,
|
|
216
|
+
sources: (dto.sources || []).map(normalizeSource),
|
|
217
|
+
toolCalls: (dto.tool_calls || []).map(normalizeToolCall),
|
|
218
|
+
usage: normalizeUsage(dto.usage),
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
|
|
222
|
+
async getHistory(sessionId: string): Promise<ChatMessage[]> {
|
|
223
|
+
try {
|
|
224
|
+
const dto = await request<SessionMessagesResponseDto>(
|
|
225
|
+
`/api/v1/sessions/${sessionId}/messages`
|
|
226
|
+
);
|
|
227
|
+
return (dto.messages || []).map(normalizeChatMessage);
|
|
228
|
+
} catch {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
async closeSession(sessionId: string): Promise<void> {
|
|
234
|
+
try {
|
|
235
|
+
await request(`/api/v1/sessions/${sessionId}`, { method: "DELETE" });
|
|
236
|
+
} catch {
|
|
237
|
+
// Closing is best-effort; a missing session should not block a new chat.
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
async createTicket(): Promise<Ticket> {
|
|
242
|
+
throw new Error("Ticket creation has been removed.");
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
async checkHealth(): Promise<boolean> {
|
|
246
|
+
try {
|
|
247
|
+
const url = buildApiUrl(baseUrl, "/health");
|
|
248
|
+
const response = await fetch(url);
|
|
249
|
+
return response.ok;
|
|
250
|
+
} catch {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export const chatApi: ChatApiClient = createChatApi();
|
|
258
|
+
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
export type ChatRole = "user" | "assistant" | "system";
|
|
2
|
+
|
|
3
|
+
export type MessageStatus = "pending" | "complete" | "error";
|
|
4
|
+
|
|
5
|
+
export interface Source {
|
|
6
|
+
chunkId: string;
|
|
7
|
+
source: string;
|
|
8
|
+
score: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ToolCall {
|
|
12
|
+
toolName: string;
|
|
13
|
+
arguments: Record<string, unknown>;
|
|
14
|
+
result: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface TokenUsage {
|
|
18
|
+
model: string;
|
|
19
|
+
promptTokens: number;
|
|
20
|
+
completionTokens: number;
|
|
21
|
+
totalTokens: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ChatMessage {
|
|
25
|
+
id: string;
|
|
26
|
+
role: ChatRole;
|
|
27
|
+
content: string;
|
|
28
|
+
createdAt: string;
|
|
29
|
+
status?: MessageStatus;
|
|
30
|
+
sources?: Source[];
|
|
31
|
+
toolCalls?: ToolCall[];
|
|
32
|
+
usage?: TokenUsage;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ChatUserContext {
|
|
36
|
+
customerId?: string;
|
|
37
|
+
email?: string;
|
|
38
|
+
name?: string;
|
|
39
|
+
role?: string;
|
|
40
|
+
page?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface Ticket {
|
|
44
|
+
ticketId: string;
|
|
45
|
+
customerId: string;
|
|
46
|
+
customerEmail: string;
|
|
47
|
+
subject: string;
|
|
48
|
+
description: string;
|
|
49
|
+
priority: string;
|
|
50
|
+
status: string;
|
|
51
|
+
createdAt: string;
|
|
52
|
+
updatedAt: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface CreateTicketInput {
|
|
56
|
+
customerIdOrEmail: string;
|
|
57
|
+
subject: string;
|
|
58
|
+
description: string;
|
|
59
|
+
priority?: "low" | "medium" | "high" | "urgent";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface SessionCreated {
|
|
63
|
+
sessionId: string;
|
|
64
|
+
createdAt: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ChatApiError {
|
|
68
|
+
code?: string;
|
|
69
|
+
message: string;
|
|
70
|
+
requestId?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface Suggestion {
|
|
74
|
+
label: string;
|
|
75
|
+
query: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface SourceDto {
|
|
79
|
+
chunk_id: string;
|
|
80
|
+
source: string;
|
|
81
|
+
score: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface ToolCallDto {
|
|
85
|
+
tool_name: string;
|
|
86
|
+
arguments?: Record<string, unknown>;
|
|
87
|
+
result?: Record<string, unknown>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface TokenUsageDto {
|
|
91
|
+
model: string;
|
|
92
|
+
prompt_tokens: number;
|
|
93
|
+
completion_tokens: number;
|
|
94
|
+
total_tokens: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface ChatMessageDto {
|
|
98
|
+
id: string;
|
|
99
|
+
role: string;
|
|
100
|
+
content: string;
|
|
101
|
+
model?: string | null;
|
|
102
|
+
prompt_tokens?: number;
|
|
103
|
+
completion_tokens?: number;
|
|
104
|
+
created_at?: string | null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface SessionResponseDto {
|
|
108
|
+
session_id: string;
|
|
109
|
+
created_at: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface SessionMessagesResponseDto {
|
|
113
|
+
session_id: string;
|
|
114
|
+
messages: ChatMessageDto[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface ChatResponseDto {
|
|
118
|
+
request_id: string;
|
|
119
|
+
session_id: string;
|
|
120
|
+
answer: string;
|
|
121
|
+
sources: SourceDto[];
|
|
122
|
+
tool_calls?: ToolCallDto[];
|
|
123
|
+
usage: TokenUsageDto;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface TicketDto {
|
|
127
|
+
ticket_id: string;
|
|
128
|
+
customer_id: string;
|
|
129
|
+
customer_email: string;
|
|
130
|
+
subject: string;
|
|
131
|
+
description: string;
|
|
132
|
+
priority: string;
|
|
133
|
+
status: string;
|
|
134
|
+
created_at: string;
|
|
135
|
+
updated_at: string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface ApiErrorDto {
|
|
139
|
+
success?: boolean;
|
|
140
|
+
error?: {
|
|
141
|
+
code?: string;
|
|
142
|
+
message?: string;
|
|
143
|
+
request_id?: string;
|
|
144
|
+
};
|
|
145
|
+
}
|