@retrivora-ai/rag-engine 0.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/.env.example +77 -0
- package/README.md +149 -0
- package/dist/DocumentChunker-BUrIrcPk.d.mts +43 -0
- package/dist/DocumentChunker-BUrIrcPk.d.ts +43 -0
- package/dist/RAGPipeline-BmkIv1HD.d.mts +298 -0
- package/dist/RAGPipeline-BmkIv1HD.d.ts +298 -0
- package/dist/chunk-NCG2JKXB.mjs +1254 -0
- package/dist/chunk-ZPXLQR5Q.mjs +67 -0
- package/dist/handlers/index.d.mts +68 -0
- package/dist/handlers/index.d.ts +68 -0
- package/dist/handlers/index.js +1319 -0
- package/dist/handlers/index.mjs +13 -0
- package/dist/index.d.mts +93 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +612 -0
- package/dist/index.mjs +551 -0
- package/dist/server.d.mts +211 -0
- package/dist/server.d.ts +211 -0
- package/dist/server.js +1457 -0
- package/dist/server.mjs +148 -0
- package/package.json +90 -0
- package/src/app/api/chat/route.ts +4 -0
- package/src/app/api/health/route.ts +4 -0
- package/src/app/api/ingest/route.ts +26 -0
- package/src/app/favicon.ico +0 -0
- package/src/app/globals.css +163 -0
- package/src/app/layout.tsx +35 -0
- package/src/app/page.tsx +506 -0
- package/src/components/ChatWidget.tsx +91 -0
- package/src/components/ChatWindow.tsx +248 -0
- package/src/components/ConfigProvider.tsx +56 -0
- package/src/components/MessageBubble.tsx +99 -0
- package/src/components/SourceCard.tsx +66 -0
- package/src/components/ThemeProvider.tsx +8 -0
- package/src/components/ThemeToggle.tsx +29 -0
- package/src/config/RagConfig.ts +159 -0
- package/src/config/UniversalProfiles.ts +83 -0
- package/src/config/serverConfig.ts +142 -0
- package/src/handlers/index.ts +202 -0
- package/src/hooks/useHydrated.ts +13 -0
- package/src/hooks/useRagChat.ts +167 -0
- package/src/hooks/useStoredMessages.ts +53 -0
- package/src/index.ts +27 -0
- package/src/llm/ILLMProvider.ts +60 -0
- package/src/llm/LLMFactory.ts +54 -0
- package/src/llm/providers/AnthropicProvider.ts +87 -0
- package/src/llm/providers/OllamaProvider.ts +102 -0
- package/src/llm/providers/OpenAIProvider.ts +92 -0
- package/src/llm/providers/UniversalLLMAdapter.ts +154 -0
- package/src/rag/DocumentChunker.ts +105 -0
- package/src/rag/RAGPipeline.ts +196 -0
- package/src/server.ts +30 -0
- package/src/types/pdf-parse.d.ts +13 -0
- package/src/utils/DocumentParser.ts +75 -0
- package/src/utils/templateUtils.ts +78 -0
- package/src/vectordb/IVectorDB.ts +75 -0
- package/src/vectordb/VectorDBFactory.ts +41 -0
- package/src/vectordb/adapters/MongoDbAdapter.ts +175 -0
- package/src/vectordb/adapters/PgVectorAdapter.ts +159 -0
- package/src/vectordb/adapters/PineconeAdapter.ts +115 -0
- package/src/vectordb/adapters/UniversalVectorDBAdapter.ts +177 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __export = (target, all) => {
|
|
23
|
+
for (var name in all)
|
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
|
+
};
|
|
26
|
+
var __copyProps = (to, from, except, desc) => {
|
|
27
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
28
|
+
for (let key of __getOwnPropNames(from))
|
|
29
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
30
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
31
|
+
}
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
34
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
35
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
36
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
37
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
38
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
39
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
40
|
+
mod
|
|
41
|
+
));
|
|
42
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
43
|
+
|
|
44
|
+
// src/index.ts
|
|
45
|
+
var index_exports = {};
|
|
46
|
+
__export(index_exports, {
|
|
47
|
+
ChatWidget: () => ChatWidget,
|
|
48
|
+
ChatWindow: () => ChatWindow,
|
|
49
|
+
ConfigProvider: () => ConfigProvider,
|
|
50
|
+
MessageBubble: () => MessageBubble,
|
|
51
|
+
SourceCard: () => SourceCard,
|
|
52
|
+
useConfig: () => useConfig,
|
|
53
|
+
useRagChat: () => useRagChat
|
|
54
|
+
});
|
|
55
|
+
module.exports = __toCommonJS(index_exports);
|
|
56
|
+
|
|
57
|
+
// src/components/ChatWidget.tsx
|
|
58
|
+
var import_react6 = require("react");
|
|
59
|
+
var import_lucide_react4 = require("lucide-react");
|
|
60
|
+
|
|
61
|
+
// src/components/ChatWindow.tsx
|
|
62
|
+
var import_react5 = require("react");
|
|
63
|
+
var import_lucide_react3 = require("lucide-react");
|
|
64
|
+
|
|
65
|
+
// src/components/MessageBubble.tsx
|
|
66
|
+
var import_react2 = __toESM(require("react"));
|
|
67
|
+
var import_lucide_react2 = require("lucide-react");
|
|
68
|
+
var import_react_markdown = __toESM(require("react-markdown"));
|
|
69
|
+
var import_remark_gfm = __toESM(require("remark-gfm"));
|
|
70
|
+
|
|
71
|
+
// src/components/SourceCard.tsx
|
|
72
|
+
var import_react = __toESM(require("react"));
|
|
73
|
+
var import_lucide_react = require("lucide-react");
|
|
74
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
75
|
+
function SourceCard({ source, index }) {
|
|
76
|
+
var _a, _b;
|
|
77
|
+
const [expanded, setExpanded] = import_react.default.useState(false);
|
|
78
|
+
const scorePercent = Math.round(source.score * 100);
|
|
79
|
+
const scoreColor = source.score >= 0.8 ? "text-emerald-400" : source.score >= 0.6 ? "text-amber-400" : "text-rose-400";
|
|
80
|
+
const scoreBg = source.score >= 0.8 ? "bg-emerald-50 border-emerald-100 dark:bg-emerald-400/10 dark:border-emerald-400/20" : source.score >= 0.6 ? "bg-amber-50 border-amber-100 dark:bg-amber-400/10 dark:border-amber-400/20" : "bg-rose-50 border-rose-100 dark:bg-rose-400/10 dark:border-rose-400/20";
|
|
81
|
+
const preview = source.content.slice(0, 120);
|
|
82
|
+
const hasMore = source.content.length > 120;
|
|
83
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
84
|
+
"div",
|
|
85
|
+
{
|
|
86
|
+
className: `rounded-xl border backdrop-blur-sm p-3 transition-all duration-200 hover:scale-[1.01] cursor-pointer ${scoreBg}`,
|
|
87
|
+
onClick: () => setExpanded((e) => !e),
|
|
88
|
+
children: [
|
|
89
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "flex items-start justify-between gap-2", children: [
|
|
90
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "flex items-center gap-2 min-w-0", children: [
|
|
91
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "flex-shrink-0 w-5 h-5 rounded-full bg-slate-200 dark:bg-white/10 flex items-center justify-center text-[10px] font-bold text-slate-500 dark:text-white/60", children: index + 1 }),
|
|
92
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "text-xs text-slate-500 dark:text-white/50 truncate font-mono", children: (_b = (_a = source.metadata) == null ? void 0 : _a.docId) != null ? _b : source.id })
|
|
93
|
+
] }),
|
|
94
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: `flex-shrink-0 text-xs font-semibold ${scoreColor} tabular-nums`, children: [
|
|
95
|
+
scorePercent,
|
|
96
|
+
"%"
|
|
97
|
+
] })
|
|
98
|
+
] }),
|
|
99
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "mt-2 text-xs text-slate-600 dark:text-white/70 leading-relaxed", children: expanded ? source.content : `${preview}${hasMore && !expanded ? "\u2026" : ""}` }),
|
|
100
|
+
hasMore && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { className: "mt-1 text-[10px] text-indigo-400 hover:text-indigo-300 transition-colors flex items-center gap-0.5", children: expanded ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
101
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_lucide_react.ChevronUp, { className: "w-3 h-3" }),
|
|
102
|
+
" Show less"
|
|
103
|
+
] }) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
104
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_lucide_react.ChevronDown, { className: "w-3 h-3" }),
|
|
105
|
+
" Show more"
|
|
106
|
+
] }) })
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/components/MessageBubble.tsx
|
|
113
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
114
|
+
function MessageBubble({
|
|
115
|
+
message,
|
|
116
|
+
sources,
|
|
117
|
+
isStreaming = false,
|
|
118
|
+
primaryColor = "#6366f1",
|
|
119
|
+
accentColor = "#8b5cf6"
|
|
120
|
+
}) {
|
|
121
|
+
const isUser = message.role === "user";
|
|
122
|
+
const [showSources, setShowSources] = import_react2.default.useState(false);
|
|
123
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: `flex gap-3 ${isUser ? "flex-row-reverse" : "flex-row"} items-start`, children: [
|
|
124
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
125
|
+
"div",
|
|
126
|
+
{
|
|
127
|
+
className: `flex-shrink-0 w-8 h-8 rounded-full flex items-center justify-center shadow-lg ${isUser ? "text-white" : "bg-slate-100 dark:bg-white/10 text-slate-700 dark:text-white/80"}`,
|
|
128
|
+
style: isUser ? { background: `linear-gradient(135deg, ${primaryColor}, ${accentColor})` } : {},
|
|
129
|
+
children: isUser ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_lucide_react2.User, { className: "w-4 h-4 text-white" }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_lucide_react2.Bot, { className: "w-4 h-4" })
|
|
130
|
+
}
|
|
131
|
+
),
|
|
132
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: `flex flex-col gap-1 max-w-[75%] ${isUser ? "items-end" : "items-start"}`, children: [
|
|
133
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
134
|
+
"div",
|
|
135
|
+
{
|
|
136
|
+
className: `relative px-4 py-3 rounded-2xl text-sm leading-relaxed shadow-sm dark:shadow-lg ${isUser ? "text-white rounded-tr-sm" : "bg-white dark:bg-white/5 border border-slate-200 dark:border-white/10 text-slate-800 dark:text-white/90 rounded-tl-sm"}`,
|
|
137
|
+
style: isUser ? { background: `linear-gradient(135deg, ${primaryColor}, ${accentColor})` } : {},
|
|
138
|
+
children: isStreaming ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "flex items-center gap-1", children: [
|
|
139
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "w-1.5 h-1.5 rounded-full bg-slate-400 dark:bg-white/60 animate-bounce [animation-delay:0ms]" }),
|
|
140
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "w-1.5 h-1.5 rounded-full bg-slate-400 dark:bg-white/60 animate-bounce [animation-delay:150ms]" }),
|
|
141
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "w-1.5 h-1.5 rounded-full bg-slate-400 dark:bg-white/60 animate-bounce [animation-delay:300ms]" })
|
|
142
|
+
] }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: `prose prose-sm max-w-none ${isUser ? "prose-invert" : "dark:prose-invert"}`, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_markdown.default, { remarkPlugins: [import_remark_gfm.default], children: message.content }) })
|
|
143
|
+
}
|
|
144
|
+
),
|
|
145
|
+
!isUser && sources && sources.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "w-full", children: [
|
|
146
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
147
|
+
"button",
|
|
148
|
+
{
|
|
149
|
+
onClick: () => setShowSources((s) => !s),
|
|
150
|
+
className: "text-[11px] text-indigo-400 hover:text-indigo-300 transition-colors flex items-center gap-1 mt-1",
|
|
151
|
+
children: [
|
|
152
|
+
showSources ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_lucide_react2.ChevronDown, { className: "w-3 h-3" }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_lucide_react2.ChevronRight, { className: "w-3 h-3" }),
|
|
153
|
+
sources.length,
|
|
154
|
+
" source",
|
|
155
|
+
sources.length !== 1 ? "s" : "",
|
|
156
|
+
" used"
|
|
157
|
+
]
|
|
158
|
+
}
|
|
159
|
+
),
|
|
160
|
+
showSources && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "mt-2 flex flex-col gap-2", children: sources.map((src, i) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(SourceCard, { source: src, index: i }, src.id)) })
|
|
161
|
+
] })
|
|
162
|
+
] })
|
|
163
|
+
] });
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/components/ConfigProvider.tsx
|
|
167
|
+
var import_react3 = require("react");
|
|
168
|
+
|
|
169
|
+
// src/utils/templateUtils.ts
|
|
170
|
+
function mergeDefined(base, override) {
|
|
171
|
+
if (!override) {
|
|
172
|
+
return __spreadValues({}, base);
|
|
173
|
+
}
|
|
174
|
+
const merged = __spreadValues({}, base);
|
|
175
|
+
for (const [key, value] of Object.entries(override)) {
|
|
176
|
+
if (value !== void 0 && value !== null && value !== "") {
|
|
177
|
+
merged[key] = value;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return merged;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// src/components/ConfigProvider.tsx
|
|
184
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
185
|
+
var defaultConfig = {
|
|
186
|
+
projectId: "default",
|
|
187
|
+
ui: {
|
|
188
|
+
title: "AI Assistant",
|
|
189
|
+
subtitle: "Powered by RAG",
|
|
190
|
+
primaryColor: "#6366f1",
|
|
191
|
+
accentColor: "#8b5cf6",
|
|
192
|
+
logoUrl: "",
|
|
193
|
+
placeholder: "Ask me anything\u2026",
|
|
194
|
+
showSources: true,
|
|
195
|
+
welcomeMessage: "Hello! I'm your AI assistant. Ask me anything about your documents.",
|
|
196
|
+
showWidget: true,
|
|
197
|
+
poweredBy: "RAG",
|
|
198
|
+
visualStyle: "glass",
|
|
199
|
+
borderRadius: "xl"
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
var ConfigContext = (0, import_react3.createContext)(defaultConfig);
|
|
203
|
+
function ConfigProvider({
|
|
204
|
+
config,
|
|
205
|
+
children
|
|
206
|
+
}) {
|
|
207
|
+
const merged = {
|
|
208
|
+
projectId: (config == null ? void 0 : config.projectId) || defaultConfig.projectId,
|
|
209
|
+
ui: mergeDefined(defaultConfig.ui, config == null ? void 0 : config.ui)
|
|
210
|
+
};
|
|
211
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ConfigContext.Provider, { value: merged, children });
|
|
212
|
+
}
|
|
213
|
+
function useConfig() {
|
|
214
|
+
return (0, import_react3.useContext)(ConfigContext);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/hooks/useRagChat.ts
|
|
218
|
+
var import_react4 = require("react");
|
|
219
|
+
var import_axios = __toESM(require("axios"));
|
|
220
|
+
|
|
221
|
+
// src/hooks/useStoredMessages.ts
|
|
222
|
+
var React4 = __toESM(require("react"));
|
|
223
|
+
function readStoredMessages(storageKey) {
|
|
224
|
+
if (typeof window === "undefined") {
|
|
225
|
+
return [];
|
|
226
|
+
}
|
|
227
|
+
const saved = window.localStorage.getItem(storageKey);
|
|
228
|
+
if (!saved) {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
try {
|
|
232
|
+
const parsed = JSON.parse(saved);
|
|
233
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
234
|
+
} catch (e) {
|
|
235
|
+
return [];
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function useStoredMessages(storageKey, persist) {
|
|
239
|
+
const [messages, setMessages] = React4.useState(() => persist ? readStoredMessages(storageKey) : []);
|
|
240
|
+
React4.useEffect(() => {
|
|
241
|
+
if (!persist || typeof window === "undefined") {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (messages.length === 0) {
|
|
245
|
+
window.localStorage.removeItem(storageKey);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
window.localStorage.setItem(storageKey, JSON.stringify(messages));
|
|
249
|
+
}, [messages, persist, storageKey]);
|
|
250
|
+
return [messages, setMessages];
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// src/hooks/useRagChat.ts
|
|
254
|
+
function useRagChat(projectId, options = {}) {
|
|
255
|
+
const {
|
|
256
|
+
apiUrl = "/api/chat",
|
|
257
|
+
namespace,
|
|
258
|
+
persist = true,
|
|
259
|
+
onReply,
|
|
260
|
+
onError
|
|
261
|
+
} = options;
|
|
262
|
+
const storageKey = `rag_chat_${projectId}`;
|
|
263
|
+
const [messages, setMessages] = useStoredMessages(storageKey, persist);
|
|
264
|
+
const [isLoading, setIsLoading] = (0, import_react4.useState)(false);
|
|
265
|
+
const [error, setError] = (0, import_react4.useState)(null);
|
|
266
|
+
const lastInputRef = (0, import_react4.useRef)("");
|
|
267
|
+
const messagesRef = (0, import_react4.useRef)(messages);
|
|
268
|
+
(0, import_react4.useEffect)(() => {
|
|
269
|
+
messagesRef.current = messages;
|
|
270
|
+
}, [messages]);
|
|
271
|
+
const sendMessage = (0, import_react4.useCallback)(
|
|
272
|
+
async (text, options2) => {
|
|
273
|
+
var _a, _b, _c;
|
|
274
|
+
const trimmed = text.trim();
|
|
275
|
+
if (!trimmed || isLoading) return;
|
|
276
|
+
lastInputRef.current = trimmed;
|
|
277
|
+
if (!(options2 == null ? void 0 : options2.skipUserMessage)) {
|
|
278
|
+
const userMessage = {
|
|
279
|
+
id: `user_${Date.now()}`,
|
|
280
|
+
role: "user",
|
|
281
|
+
content: trimmed,
|
|
282
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
283
|
+
};
|
|
284
|
+
setMessages((prev) => [...prev, userMessage]);
|
|
285
|
+
}
|
|
286
|
+
setError(null);
|
|
287
|
+
setIsLoading(true);
|
|
288
|
+
try {
|
|
289
|
+
const history = messagesRef.current.map(({ role, content }) => ({ role, content }));
|
|
290
|
+
const { data } = await import_axios.default.post(apiUrl, {
|
|
291
|
+
message: trimmed,
|
|
292
|
+
history,
|
|
293
|
+
namespace: namespace != null ? namespace : projectId
|
|
294
|
+
});
|
|
295
|
+
const assistantMessage = {
|
|
296
|
+
id: `assistant_${Date.now()}`,
|
|
297
|
+
role: "assistant",
|
|
298
|
+
content: data.reply,
|
|
299
|
+
sources: data.sources,
|
|
300
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
301
|
+
};
|
|
302
|
+
setMessages((prev) => [...prev, assistantMessage]);
|
|
303
|
+
onReply == null ? void 0 : onReply(assistantMessage);
|
|
304
|
+
} catch (err) {
|
|
305
|
+
const msg = import_axios.default.isAxiosError(err) ? (_c = (_b = (_a = err.response) == null ? void 0 : _a.data) == null ? void 0 : _b.error) != null ? _c : err.message : "Something went wrong. Please try again.";
|
|
306
|
+
setError(msg);
|
|
307
|
+
onError == null ? void 0 : onError(msg);
|
|
308
|
+
} finally {
|
|
309
|
+
setIsLoading(false);
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
[apiUrl, isLoading, namespace, onError, onReply, projectId, setMessages]
|
|
313
|
+
);
|
|
314
|
+
const clear = (0, import_react4.useCallback)(() => {
|
|
315
|
+
setMessages([]);
|
|
316
|
+
setError(null);
|
|
317
|
+
if (persist) localStorage.removeItem(storageKey);
|
|
318
|
+
}, [persist, setMessages, storageKey]);
|
|
319
|
+
const retry = (0, import_react4.useCallback)(async () => {
|
|
320
|
+
if (lastInputRef.current) {
|
|
321
|
+
const latestMessage = messagesRef.current[messagesRef.current.length - 1];
|
|
322
|
+
await sendMessage(lastInputRef.current, {
|
|
323
|
+
skipUserMessage: (latestMessage == null ? void 0 : latestMessage.role) === "user" && latestMessage.content === lastInputRef.current
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}, [sendMessage]);
|
|
327
|
+
return {
|
|
328
|
+
messages,
|
|
329
|
+
isLoading,
|
|
330
|
+
error,
|
|
331
|
+
send: sendMessage,
|
|
332
|
+
clear,
|
|
333
|
+
retry,
|
|
334
|
+
setMessages
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// src/components/ChatWindow.tsx
|
|
339
|
+
var import_jsx_runtime4 = (
|
|
340
|
+
// eslint-disable-next-line @next/next/no-img-element
|
|
341
|
+
require("react/jsx-runtime")
|
|
342
|
+
);
|
|
343
|
+
function ChatWindow({ className = "", style, onClose, showClose = false }) {
|
|
344
|
+
const { ui, projectId } = useConfig();
|
|
345
|
+
const [input, setInput] = (0, import_react5.useState)("");
|
|
346
|
+
const [mounted, setMounted] = (0, import_react5.useState)(false);
|
|
347
|
+
const bottomRef = (0, import_react5.useRef)(null);
|
|
348
|
+
const inputRef = (0, import_react5.useRef)(null);
|
|
349
|
+
const { messages, send, clear, isLoading, error } = useRagChat(projectId, {
|
|
350
|
+
namespace: projectId
|
|
351
|
+
});
|
|
352
|
+
(0, import_react5.useEffect)(() => {
|
|
353
|
+
setMounted(true);
|
|
354
|
+
}, []);
|
|
355
|
+
(0, import_react5.useEffect)(() => {
|
|
356
|
+
var _a;
|
|
357
|
+
if (messages.length > 0 || isLoading) {
|
|
358
|
+
(_a = bottomRef.current) == null ? void 0 : _a.scrollIntoView({ behavior: "smooth" });
|
|
359
|
+
}
|
|
360
|
+
}, [messages, isLoading]);
|
|
361
|
+
const sendMessage = (0, import_react5.useCallback)(async () => {
|
|
362
|
+
const text = input.trim();
|
|
363
|
+
if (!text || isLoading) return;
|
|
364
|
+
setInput("");
|
|
365
|
+
await send(text);
|
|
366
|
+
window.setTimeout(() => {
|
|
367
|
+
var _a;
|
|
368
|
+
return (_a = inputRef.current) == null ? void 0 : _a.focus();
|
|
369
|
+
}, 50);
|
|
370
|
+
}, [input, isLoading, send]);
|
|
371
|
+
const handleKeyDown = (e) => {
|
|
372
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
373
|
+
e.preventDefault();
|
|
374
|
+
sendMessage();
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
const clearHistory = () => {
|
|
378
|
+
clear();
|
|
379
|
+
};
|
|
380
|
+
const isEmpty = messages.length === 0;
|
|
381
|
+
const borderRadiusMap = {
|
|
382
|
+
none: "rounded-none",
|
|
383
|
+
sm: "rounded-sm",
|
|
384
|
+
md: "rounded-md",
|
|
385
|
+
lg: "rounded-lg",
|
|
386
|
+
xl: "rounded-xl",
|
|
387
|
+
full: "rounded-3xl"
|
|
388
|
+
};
|
|
389
|
+
const currentRadius = borderRadiusMap[ui.borderRadius || "xl"];
|
|
390
|
+
const isGlass = ui.visualStyle !== "solid";
|
|
391
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
392
|
+
"div",
|
|
393
|
+
{
|
|
394
|
+
className: `flex flex-col border border-slate-200 dark:border-white/10 shadow-2xl transition-all duration-300 ${currentRadius} ${isGlass ? "bg-white/90 dark:bg-[#0f0f1a]/90 backdrop-blur-xl" : "bg-white dark:bg-[#0f0f1a]"} ${className}`,
|
|
395
|
+
style: __spreadValues({ "--primary": ui.primaryColor, "--accent": ui.accentColor }, style),
|
|
396
|
+
children: [
|
|
397
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
|
|
398
|
+
"div",
|
|
399
|
+
{
|
|
400
|
+
className: "flex items-center justify-between px-5 py-4 border-b border-slate-200 dark:border-white/10",
|
|
401
|
+
style: { background: `linear-gradient(135deg, ${ui.primaryColor}15, ${ui.accentColor}10)` },
|
|
402
|
+
children: [
|
|
403
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "flex items-center gap-3", children: [
|
|
404
|
+
ui.logoUrl ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("img", { src: ui.logoUrl, alt: "logo", className: "w-8 h-8 rounded-lg object-cover" }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
405
|
+
"div",
|
|
406
|
+
{
|
|
407
|
+
className: `w-9 h-9 flex items-center justify-center shadow-md ${ui.borderRadius === "full" ? "rounded-full" : "rounded-xl"}`,
|
|
408
|
+
style: { background: `linear-gradient(135deg, ${ui.primaryColor}, ${ui.accentColor})` },
|
|
409
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_lucide_react3.Bot, { className: "w-5 h-5 text-white" })
|
|
410
|
+
}
|
|
411
|
+
),
|
|
412
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { children: [
|
|
413
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("h2", { className: "text-slate-900 dark:text-white font-semibold text-sm leading-tight", children: ui.title }),
|
|
414
|
+
ui.poweredBy && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { className: "text-slate-500 dark:text-white/40 text-[11px] leading-tight", children: `Powered by ${ui.poweredBy}` })
|
|
415
|
+
] })
|
|
416
|
+
] }),
|
|
417
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "flex items-center gap-2", children: [
|
|
418
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("span", { className: "flex items-center gap-1 text-[10px] text-emerald-500 dark:text-emerald-400", children: [
|
|
419
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "w-1.5 h-1.5 rounded-full bg-emerald-500 dark:bg-emerald-400 animate-pulse" }),
|
|
420
|
+
"Online"
|
|
421
|
+
] }),
|
|
422
|
+
mounted && messages.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
423
|
+
"button",
|
|
424
|
+
{
|
|
425
|
+
onClick: clearHistory,
|
|
426
|
+
title: "Clear conversation",
|
|
427
|
+
className: "w-7 h-7 rounded-lg flex items-center justify-center text-slate-400 hover:text-slate-600 dark:text-white/40 dark:hover:text-white/70 hover:bg-slate-100 dark:hover:bg-white/10 transition-all cursor-pointer",
|
|
428
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_lucide_react3.Trash2, { className: "w-3.5 h-3.5" })
|
|
429
|
+
}
|
|
430
|
+
),
|
|
431
|
+
showClose && onClose && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
432
|
+
"button",
|
|
433
|
+
{
|
|
434
|
+
onClick: onClose,
|
|
435
|
+
title: "Close chat",
|
|
436
|
+
className: "w-7 h-7 rounded-lg flex items-center justify-center text-slate-400 hover:text-slate-600 dark:text-white/40 dark:hover:text-white/70 hover:bg-slate-100 dark:hover:bg-white/10 transition-all cursor-pointer",
|
|
437
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_lucide_react3.X, { className: "w-4 h-4" })
|
|
438
|
+
}
|
|
439
|
+
)
|
|
440
|
+
] })
|
|
441
|
+
]
|
|
442
|
+
}
|
|
443
|
+
),
|
|
444
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "flex-1 overflow-y-auto px-4 py-4 space-y-5 scrollbar-thin scrollbar-thumb-slate-200 dark:scrollbar-thumb-white/10 scrollbar-track-transparent min-h-0 bg-slate-50 dark:bg-transparent", children: [
|
|
445
|
+
!mounted || isEmpty ? (
|
|
446
|
+
/* Welcome state */
|
|
447
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "h-full flex flex-col items-center justify-center text-center gap-4 px-6 py-12", children: [
|
|
448
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
449
|
+
"div",
|
|
450
|
+
{
|
|
451
|
+
className: `w-16 h-16 flex items-center justify-center shadow-lg ${ui.borderRadius === "full" ? "rounded-full" : "rounded-2xl"}`,
|
|
452
|
+
style: { background: `linear-gradient(135deg, ${ui.primaryColor}, ${ui.accentColor})` },
|
|
453
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_lucide_react3.Sparkles, { className: "w-8 h-8 text-white" })
|
|
454
|
+
}
|
|
455
|
+
),
|
|
456
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { children: [
|
|
457
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { className: "text-slate-800 dark:text-white/80 font-medium leading-relaxed", children: ui.welcomeMessage }),
|
|
458
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { className: "text-slate-500 dark:text-white/30 text-sm mt-2", children: "Ask a question to get started" })
|
|
459
|
+
] }),
|
|
460
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "flex flex-wrap gap-2 justify-center mt-2", children: ["What can you help me with?", "Summarise the key topics", "Show me an example"].map(
|
|
461
|
+
(suggestion) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
462
|
+
"button",
|
|
463
|
+
{
|
|
464
|
+
onClick: () => {
|
|
465
|
+
var _a;
|
|
466
|
+
setInput(suggestion);
|
|
467
|
+
(_a = inputRef.current) == null ? void 0 : _a.focus();
|
|
468
|
+
},
|
|
469
|
+
className: "px-3 py-1.5 rounded-full text-xs border border-slate-200 dark:border-white/10 text-slate-600 dark:text-white/50 hover:text-slate-900 dark:hover:text-white/80 hover:border-slate-300 dark:hover:border-white/20 hover:bg-slate-100 dark:hover:bg-white/5 transition-all",
|
|
470
|
+
children: suggestion
|
|
471
|
+
},
|
|
472
|
+
suggestion
|
|
473
|
+
)
|
|
474
|
+
) })
|
|
475
|
+
] })
|
|
476
|
+
) : messages.map((msg) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
477
|
+
MessageBubble,
|
|
478
|
+
{
|
|
479
|
+
message: msg,
|
|
480
|
+
sources: msg.sources,
|
|
481
|
+
primaryColor: ui.primaryColor,
|
|
482
|
+
accentColor: ui.accentColor
|
|
483
|
+
},
|
|
484
|
+
msg.id
|
|
485
|
+
)),
|
|
486
|
+
isLoading && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
487
|
+
MessageBubble,
|
|
488
|
+
{
|
|
489
|
+
message: { role: "assistant", content: "" },
|
|
490
|
+
isStreaming: true,
|
|
491
|
+
primaryColor: ui.primaryColor,
|
|
492
|
+
accentColor: ui.accentColor
|
|
493
|
+
}
|
|
494
|
+
),
|
|
495
|
+
error && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: "flex items-center gap-2 text-rose-500 dark:text-rose-400 text-sm bg-rose-50 dark:bg-rose-400/10 border border-rose-200 dark:border-rose-400/20 rounded-xl px-4 py-3", children: [
|
|
496
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_lucide_react3.TriangleAlert, { className: "w-4 h-4 flex-shrink-0" }),
|
|
497
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { children: error })
|
|
498
|
+
] }),
|
|
499
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { ref: bottomRef })
|
|
500
|
+
] }),
|
|
501
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: `px-4 pb-4 pt-2 border-t border-slate-200 dark:border-white/10 ${isGlass ? "bg-transparent" : "bg-white dark:bg-[#0f0f1a]"}`, children: [
|
|
502
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: `flex items-end gap-2 bg-slate-50 dark:bg-white/5 border border-slate-200 dark:border-white/10 p-2 focus-within:border-slate-300 dark:focus-within:border-white/20 transition-all ${ui.borderRadius === "full" ? "rounded-3xl" : "rounded-2xl"}`, children: [
|
|
503
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
504
|
+
"textarea",
|
|
505
|
+
{
|
|
506
|
+
ref: inputRef,
|
|
507
|
+
value: input,
|
|
508
|
+
onChange: (e) => setInput(e.target.value),
|
|
509
|
+
onKeyDown: handleKeyDown,
|
|
510
|
+
placeholder: ui.placeholder,
|
|
511
|
+
rows: 1,
|
|
512
|
+
disabled: isLoading,
|
|
513
|
+
className: "flex-1 bg-transparent text-slate-900 dark:text-white/90 placeholder-slate-400 dark:placeholder-white/30 text-sm resize-none outline-none py-1.5 px-2 max-h-32 leading-relaxed disabled:opacity-50",
|
|
514
|
+
style: { scrollbarWidth: "none" }
|
|
515
|
+
}
|
|
516
|
+
),
|
|
517
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
518
|
+
"button",
|
|
519
|
+
{
|
|
520
|
+
onClick: sendMessage,
|
|
521
|
+
disabled: !input.trim() || isLoading,
|
|
522
|
+
className: "flex-shrink-0 w-9 h-9 rounded-xl flex items-center justify-center transition-all disabled:opacity-30 disabled:cursor-not-allowed hover:scale-105 active:scale-95 shadow-md",
|
|
523
|
+
style: {
|
|
524
|
+
background: input.trim() && !isLoading ? `linear-gradient(135deg, ${ui.primaryColor}, ${ui.accentColor})` : "rgba(148, 163, 184, 0.2)"
|
|
525
|
+
// slate-400/20 for light mode disabled
|
|
526
|
+
},
|
|
527
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_lucide_react3.ArrowUp, { className: "w-4 h-4 text-white" })
|
|
528
|
+
}
|
|
529
|
+
)
|
|
530
|
+
] }),
|
|
531
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { className: "text-center text-[10px] text-slate-400 dark:text-white/20 mt-2", children: "Press Enter to send \xB7 Shift+Enter for new line" })
|
|
532
|
+
] })
|
|
533
|
+
]
|
|
534
|
+
}
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// src/components/ChatWidget.tsx
|
|
539
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
540
|
+
function ChatWidget({ position = "bottom-right" }) {
|
|
541
|
+
const { ui } = useConfig();
|
|
542
|
+
const [isOpen, setIsOpen] = (0, import_react6.useState)(false);
|
|
543
|
+
const [hasUnread, setHasUnread] = (0, import_react6.useState)(false);
|
|
544
|
+
if (ui.showWidget === false) return null;
|
|
545
|
+
const positionClass = position === "bottom-left" ? "bottom-6 left-6" : "bottom-6 right-6";
|
|
546
|
+
const windowPositionClass = position === "bottom-left" ? "bottom-20 left-6" : "bottom-20 right-6";
|
|
547
|
+
const handleOpen = () => {
|
|
548
|
+
setIsOpen(true);
|
|
549
|
+
setHasUnread(false);
|
|
550
|
+
};
|
|
551
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
|
|
552
|
+
/* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
|
|
553
|
+
"div",
|
|
554
|
+
{
|
|
555
|
+
className: `fixed z-[99] w-[380px] max-w-[calc(100vw-3rem)] transition-all duration-300 ease-in-out ${windowPositionClass} ${isOpen ? "opacity-100 translate-y-0 pointer-events-auto" : "opacity-0 translate-y-4 pointer-events-none"}`,
|
|
556
|
+
style: { height: "600px", maxHeight: "calc(100vh - 6rem)" },
|
|
557
|
+
children: [
|
|
558
|
+
/* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
559
|
+
ChatWindow,
|
|
560
|
+
{
|
|
561
|
+
className: "h-full relative z-10",
|
|
562
|
+
showClose: true,
|
|
563
|
+
onClose: () => setIsOpen(false)
|
|
564
|
+
}
|
|
565
|
+
),
|
|
566
|
+
/* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
567
|
+
"div",
|
|
568
|
+
{
|
|
569
|
+
className: `absolute -bottom-1.5 w-4 h-4 rotate-45 border-r border-b border-slate-200 dark:border-white/10 z-0 ${ui.visualStyle === "solid" ? "bg-white dark:bg-[#0f0f1a]" : "bg-white/90 dark:bg-[#0f0f1a]/90 backdrop-blur-xl"} ${position === "bottom-left" ? "left-5" : "right-5"}`
|
|
570
|
+
}
|
|
571
|
+
)
|
|
572
|
+
]
|
|
573
|
+
}
|
|
574
|
+
),
|
|
575
|
+
/* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
|
|
576
|
+
"button",
|
|
577
|
+
{
|
|
578
|
+
onClick: isOpen ? () => setIsOpen(false) : handleOpen,
|
|
579
|
+
className: `fixed z-[100] w-14 h-14 shadow-2xl flex items-center justify-center transition-all duration-300 cursor-pointer hover:scale-110 active:scale-95 ${ui.borderRadius === "full" ? "rounded-full" : "rounded-2xl"} ${positionClass}`,
|
|
580
|
+
style: { background: `linear-gradient(135deg, ${ui.primaryColor}, ${ui.accentColor})` },
|
|
581
|
+
"aria-label": "Open chat",
|
|
582
|
+
children: [
|
|
583
|
+
/* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
584
|
+
"span",
|
|
585
|
+
{
|
|
586
|
+
className: "absolute inset-0 rounded-full animate-ping opacity-20",
|
|
587
|
+
style: { background: ui.primaryColor }
|
|
588
|
+
}
|
|
589
|
+
),
|
|
590
|
+
/* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
591
|
+
"span",
|
|
592
|
+
{
|
|
593
|
+
className: `transition-transform duration-300 ${isOpen ? "rotate-90 scale-90" : "rotate-0 scale-100"}`,
|
|
594
|
+
children: isOpen ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_lucide_react4.X, { className: "w-6 h-6 text-white" }) : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_lucide_react4.MessageSquare, { className: "w-6 h-6 text-white" })
|
|
595
|
+
}
|
|
596
|
+
),
|
|
597
|
+
hasUnread && !isOpen && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { className: "absolute top-0 right-0 w-4 h-4 bg-rose-500 rounded-full border-2 border-white flex items-center justify-center text-[9px] font-bold text-white", children: "1" })
|
|
598
|
+
]
|
|
599
|
+
}
|
|
600
|
+
)
|
|
601
|
+
] });
|
|
602
|
+
}
|
|
603
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
604
|
+
0 && (module.exports = {
|
|
605
|
+
ChatWidget,
|
|
606
|
+
ChatWindow,
|
|
607
|
+
ConfigProvider,
|
|
608
|
+
MessageBubble,
|
|
609
|
+
SourceCard,
|
|
610
|
+
useConfig,
|
|
611
|
+
useRagChat
|
|
612
|
+
});
|