sagedesk 1.0.0 → 2.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/README.md +277 -35
- package/dist/next/{SageDeskWidget-P3H2VJR5.js → SageDeskWidget-R3XJ5OUY.js} +149 -25
- package/dist/next/SageDeskWidget-R3XJ5OUY.js.map +1 -0
- package/dist/next/index.cjs +164 -27
- package/dist/next/index.cjs.map +1 -1
- package/dist/next/index.d.cts +10 -3
- package/dist/next/index.d.ts +10 -3
- package/dist/next/index.js +10 -4
- package/dist/next/index.js.map +1 -1
- package/dist/react/index.cjs +148 -24
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +14 -5
- package/dist/react/index.d.ts +14 -5
- package/dist/react/index.js +148 -24
- package/dist/react/index.js.map +1 -1
- package/dist/server/index.cjs +348 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +62 -0
- package/dist/server/index.d.ts +62 -0
- package/dist/server/index.js +312 -0
- package/dist/server/index.js.map +1 -0
- package/dist/vanilla/index.cjs +6 -2
- package/dist/vanilla/index.cjs.map +1 -1
- package/dist/vanilla/index.d.cts +4 -2
- package/dist/vanilla/index.d.ts +4 -2
- package/dist/vanilla/index.js +6 -2
- package/dist/vanilla/index.js.map +1 -1
- package/package.json +10 -3
- package/dist/next/SageDeskWidget-P3H2VJR5.js.map +0 -1
|
@@ -0,0 +1,348 @@
|
|
|
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 __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/server/index.ts
|
|
31
|
+
var server_exports = {};
|
|
32
|
+
__export(server_exports, {
|
|
33
|
+
createSageDeskHandler: () => createSageDeskHandler,
|
|
34
|
+
createSageDeskMiddleware: () => createSageDeskMiddleware
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(server_exports);
|
|
37
|
+
var import_fs = require("fs");
|
|
38
|
+
|
|
39
|
+
// src/core/embedder.ts
|
|
40
|
+
var XENOVA_IDS = {
|
|
41
|
+
"all-MiniLM-L6-v2": "Xenova/all-MiniLM-L6-v2",
|
|
42
|
+
"bge-small-en-v1-5": "Xenova/bge-small-en-v1.5",
|
|
43
|
+
"paraphrase-multilingual-MiniLM-L12-v2": "Xenova/paraphrase-multilingual-MiniLM-L12-v2",
|
|
44
|
+
"all-mpnet-base-v2": "Xenova/all-mpnet-base-v2"
|
|
45
|
+
};
|
|
46
|
+
var EmbedderRuntime = class _EmbedderRuntime {
|
|
47
|
+
constructor() {
|
|
48
|
+
this._ready = false;
|
|
49
|
+
this._failed = false;
|
|
50
|
+
}
|
|
51
|
+
static {
|
|
52
|
+
// Module-level singleton so the WASM model is loaded at most once per page,
|
|
53
|
+
// regardless of how many widget instances exist.
|
|
54
|
+
this._pipelineInstance = null;
|
|
55
|
+
}
|
|
56
|
+
static {
|
|
57
|
+
this._loadingPromise = null;
|
|
58
|
+
}
|
|
59
|
+
async load(model = "all-MiniLM-L6-v2") {
|
|
60
|
+
if (this._ready) return;
|
|
61
|
+
if (this._failed) throw new Error("EmbedderRuntime previously failed to load");
|
|
62
|
+
if (_EmbedderRuntime._loadingPromise) {
|
|
63
|
+
await _EmbedderRuntime._loadingPromise;
|
|
64
|
+
this._ready = true;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const modelId = XENOVA_IDS[model];
|
|
68
|
+
_EmbedderRuntime._loadingPromise = (async () => {
|
|
69
|
+
try {
|
|
70
|
+
const { pipeline } = await import("@huggingface/transformers");
|
|
71
|
+
_EmbedderRuntime._pipelineInstance = await pipeline(
|
|
72
|
+
"feature-extraction",
|
|
73
|
+
modelId,
|
|
74
|
+
{ dtype: "q8" }
|
|
75
|
+
);
|
|
76
|
+
} catch (err) {
|
|
77
|
+
_EmbedderRuntime._loadingPromise = null;
|
|
78
|
+
_EmbedderRuntime._pipelineInstance = null;
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
})();
|
|
82
|
+
try {
|
|
83
|
+
await _EmbedderRuntime._loadingPromise;
|
|
84
|
+
this._ready = true;
|
|
85
|
+
} catch (err) {
|
|
86
|
+
this._failed = true;
|
|
87
|
+
throw err;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async embed(text) {
|
|
91
|
+
if (!_EmbedderRuntime._pipelineInstance) {
|
|
92
|
+
await this.load();
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const output = await _EmbedderRuntime._pipelineInstance(text, {
|
|
96
|
+
pooling: "mean",
|
|
97
|
+
normalize: true
|
|
98
|
+
});
|
|
99
|
+
return output.data;
|
|
100
|
+
} catch (err) {
|
|
101
|
+
throw new Error(`Embedding failed: ${String(err)}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
get isReady() {
|
|
105
|
+
return this._ready;
|
|
106
|
+
}
|
|
107
|
+
get hasFailed() {
|
|
108
|
+
return this._failed;
|
|
109
|
+
}
|
|
110
|
+
/** @internal */
|
|
111
|
+
static _reset() {
|
|
112
|
+
_EmbedderRuntime._pipelineInstance = null;
|
|
113
|
+
_EmbedderRuntime._loadingPromise = null;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/core/search.ts
|
|
118
|
+
function dotProduct(a, b) {
|
|
119
|
+
if (a.length !== b.length) {
|
|
120
|
+
throw new Error(`Vector dimension mismatch: query(${a.length}) vs index(${b.length})`);
|
|
121
|
+
}
|
|
122
|
+
let dot = 0;
|
|
123
|
+
for (let i = 0; i < a.length; i++) dot += a[i] * b[i];
|
|
124
|
+
return dot;
|
|
125
|
+
}
|
|
126
|
+
function search(queryVector, index, topK = 3, minScore = 0.42) {
|
|
127
|
+
const results = [];
|
|
128
|
+
for (const chunk of index) {
|
|
129
|
+
const score = dotProduct(queryVector, chunk.vector384);
|
|
130
|
+
if (score < minScore) continue;
|
|
131
|
+
if (results.length < topK) {
|
|
132
|
+
results.push({ chunk, score });
|
|
133
|
+
results.sort((a, b) => b.score - a.score);
|
|
134
|
+
} else if (score > results[topK - 1].score) {
|
|
135
|
+
results[topK - 1] = { chunk, score };
|
|
136
|
+
results.sort((a, b) => b.score - a.score);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return results;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/core/renderer.ts
|
|
143
|
+
function buildAnswer(results) {
|
|
144
|
+
if (results.length === 0) return "";
|
|
145
|
+
const seen = /* @__PURE__ */ new Set();
|
|
146
|
+
const parts = [];
|
|
147
|
+
for (const r of results) {
|
|
148
|
+
if (!seen.has(r.chunk.sourceId)) {
|
|
149
|
+
seen.add(r.chunk.sourceId);
|
|
150
|
+
parts.push(r.chunk.text);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return parts.join("\n\n");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/server/index.ts
|
|
157
|
+
var PROVIDER_URLS = {
|
|
158
|
+
openai: "https://api.openai.com/v1/chat/completions",
|
|
159
|
+
deepseek: "https://api.deepseek.com/chat/completions",
|
|
160
|
+
groq: "https://api.groq.com/openai/v1/chat/completions",
|
|
161
|
+
gemini: "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions",
|
|
162
|
+
anthropic: "https://api.anthropic.com/v1/messages"
|
|
163
|
+
};
|
|
164
|
+
var DEFAULT_SYSTEM_PROMPT = "You are a helpful support assistant. Answer the user's question based ONLY on the provided context. If the context does not contain a confident answer, respond with a friendly message saying you don't have that information right now. Do not make up information or draw from outside knowledge. Be concise, warm, and helpful.";
|
|
165
|
+
var indexCache = /* @__PURE__ */ new Map();
|
|
166
|
+
var embedderCache = /* @__PURE__ */ new Map();
|
|
167
|
+
function loadIndexFile(indexPath) {
|
|
168
|
+
if (indexCache.has(indexPath)) return indexCache.get(indexPath);
|
|
169
|
+
const raw = (0, import_fs.readFileSync)(indexPath, "utf-8");
|
|
170
|
+
const data = JSON.parse(raw);
|
|
171
|
+
const chunks = Array.isArray(data) ? data : data.chunks;
|
|
172
|
+
for (const chunk of chunks) {
|
|
173
|
+
chunk.textLower = chunk.text.toLowerCase();
|
|
174
|
+
if (Array.isArray(chunk.vector384)) {
|
|
175
|
+
chunk.vector384 = new Float32Array(chunk.vector384);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
indexCache.set(indexPath, chunks);
|
|
179
|
+
return chunks;
|
|
180
|
+
}
|
|
181
|
+
async function getEmbedder(model = "all-MiniLM-L6-v2") {
|
|
182
|
+
if (embedderCache.has(model)) return embedderCache.get(model);
|
|
183
|
+
const embedder = new EmbedderRuntime();
|
|
184
|
+
await embedder.load(model);
|
|
185
|
+
embedderCache.set(model, embedder);
|
|
186
|
+
return embedder;
|
|
187
|
+
}
|
|
188
|
+
function classifyError(error) {
|
|
189
|
+
const msg = String(error).toLowerCase();
|
|
190
|
+
if (msg.includes("401") || msg.includes("403") || msg.includes("unauthorized") || msg.includes("invalid api key")) {
|
|
191
|
+
return "auth-error";
|
|
192
|
+
}
|
|
193
|
+
if (msg.includes("429") || msg.includes("quota") || msg.includes("rate limit")) {
|
|
194
|
+
return "quota-exceeded";
|
|
195
|
+
}
|
|
196
|
+
if (msg.includes("timeout") || msg.includes("aborted")) {
|
|
197
|
+
return "timeout";
|
|
198
|
+
}
|
|
199
|
+
if (msg.includes("malformed") || msg.includes("json")) {
|
|
200
|
+
return "malformed-response";
|
|
201
|
+
}
|
|
202
|
+
return "api-error";
|
|
203
|
+
}
|
|
204
|
+
async function callLLM(provider, apiKey, model, systemPrompt, query, context, timeoutMs = 5e3) {
|
|
205
|
+
const url = PROVIDER_URLS[provider] ?? provider;
|
|
206
|
+
const controller = new AbortController();
|
|
207
|
+
const timeoutHandle = setTimeout(() => controller.abort(), timeoutMs);
|
|
208
|
+
try {
|
|
209
|
+
if (provider === "anthropic") {
|
|
210
|
+
const res2 = await fetch(url, {
|
|
211
|
+
method: "POST",
|
|
212
|
+
headers: {
|
|
213
|
+
"Content-Type": "application/json",
|
|
214
|
+
"x-api-key": apiKey,
|
|
215
|
+
"anthropic-version": "2023-06-01"
|
|
216
|
+
},
|
|
217
|
+
body: JSON.stringify({
|
|
218
|
+
model,
|
|
219
|
+
max_tokens: 512,
|
|
220
|
+
system: systemPrompt,
|
|
221
|
+
messages: [{ role: "user", content: `Context:
|
|
222
|
+
${context}
|
|
223
|
+
|
|
224
|
+
Question: ${query}` }]
|
|
225
|
+
}),
|
|
226
|
+
signal: controller.signal
|
|
227
|
+
});
|
|
228
|
+
if (!res2.ok) {
|
|
229
|
+
const error = classifyError(`${res2.status}`);
|
|
230
|
+
return { answer: "", error };
|
|
231
|
+
}
|
|
232
|
+
const data2 = await res2.json();
|
|
233
|
+
const answer2 = data2.content?.[0]?.text?.trim() ?? "";
|
|
234
|
+
return { answer: answer2 };
|
|
235
|
+
}
|
|
236
|
+
const res = await fetch(url, {
|
|
237
|
+
method: "POST",
|
|
238
|
+
headers: {
|
|
239
|
+
"Content-Type": "application/json",
|
|
240
|
+
Authorization: `Bearer ${apiKey}`
|
|
241
|
+
},
|
|
242
|
+
body: JSON.stringify({
|
|
243
|
+
model,
|
|
244
|
+
messages: [
|
|
245
|
+
{ role: "system", content: systemPrompt },
|
|
246
|
+
{
|
|
247
|
+
role: "user",
|
|
248
|
+
content: `Context:
|
|
249
|
+
${context}
|
|
250
|
+
|
|
251
|
+
Question: ${query}`
|
|
252
|
+
}
|
|
253
|
+
],
|
|
254
|
+
temperature: 0.3,
|
|
255
|
+
max_tokens: 512
|
|
256
|
+
}),
|
|
257
|
+
signal: controller.signal
|
|
258
|
+
});
|
|
259
|
+
if (!res.ok) {
|
|
260
|
+
const error = classifyError(`${res.status}`);
|
|
261
|
+
return { answer: "", error };
|
|
262
|
+
}
|
|
263
|
+
const data = await res.json();
|
|
264
|
+
const answer = data.choices?.[0]?.message?.content?.trim() ?? "";
|
|
265
|
+
return { answer };
|
|
266
|
+
} catch (err) {
|
|
267
|
+
const error = classifyError(err);
|
|
268
|
+
return { answer: "", error };
|
|
269
|
+
} finally {
|
|
270
|
+
clearTimeout(timeoutHandle);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
async function handleQuery(query, config) {
|
|
274
|
+
const {
|
|
275
|
+
indexPath,
|
|
276
|
+
provider,
|
|
277
|
+
apiKey,
|
|
278
|
+
model,
|
|
279
|
+
embeddingModel,
|
|
280
|
+
topK = 5,
|
|
281
|
+
minScore = 0.42,
|
|
282
|
+
systemPrompt = DEFAULT_SYSTEM_PROMPT,
|
|
283
|
+
llmTimeoutMs = 5e3
|
|
284
|
+
} = config;
|
|
285
|
+
const index = loadIndexFile(indexPath);
|
|
286
|
+
const embedder = await getEmbedder(embeddingModel);
|
|
287
|
+
const queryVector = await embedder.embed(query);
|
|
288
|
+
const results = search(queryVector, index, topK, minScore);
|
|
289
|
+
if (results.length === 0) {
|
|
290
|
+
return { answer: "", isFallback: true };
|
|
291
|
+
}
|
|
292
|
+
const context = buildAnswer(results);
|
|
293
|
+
const llmResult = await callLLM(
|
|
294
|
+
provider,
|
|
295
|
+
apiKey,
|
|
296
|
+
model,
|
|
297
|
+
systemPrompt,
|
|
298
|
+
query,
|
|
299
|
+
context,
|
|
300
|
+
llmTimeoutMs
|
|
301
|
+
);
|
|
302
|
+
if (!llmResult.answer) {
|
|
303
|
+
return {
|
|
304
|
+
answer: "",
|
|
305
|
+
isFallback: true,
|
|
306
|
+
fallbackReason: llmResult.error
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
return { answer: llmResult.answer, isFallback: false };
|
|
310
|
+
}
|
|
311
|
+
function createSageDeskHandler(config) {
|
|
312
|
+
return async function POST(request) {
|
|
313
|
+
try {
|
|
314
|
+
const body = await request.json();
|
|
315
|
+
const query = body.query?.trim();
|
|
316
|
+
if (!query) {
|
|
317
|
+
return Response.json({ error: "Missing query" }, { status: 400 });
|
|
318
|
+
}
|
|
319
|
+
const result = await handleQuery(query, config);
|
|
320
|
+
return Response.json(result);
|
|
321
|
+
} catch (err) {
|
|
322
|
+
console.error("[sagedesk/server] Handler error:", err);
|
|
323
|
+
return Response.json({ answer: "", isFallback: true }, { status: 500 });
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function createSageDeskMiddleware(config) {
|
|
328
|
+
return async function sageDeskMiddleware(req, res, next) {
|
|
329
|
+
try {
|
|
330
|
+
const query = req.body?.query?.trim();
|
|
331
|
+
if (!query) {
|
|
332
|
+
res.status(400).json({ error: "Missing query" });
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const result = await handleQuery(query, config);
|
|
336
|
+
res.json(result);
|
|
337
|
+
} catch (err) {
|
|
338
|
+
console.error("[sagedesk/server] Middleware error:", err);
|
|
339
|
+
next(err);
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
344
|
+
0 && (module.exports = {
|
|
345
|
+
createSageDeskHandler,
|
|
346
|
+
createSageDeskMiddleware
|
|
347
|
+
});
|
|
348
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/index.ts","../../src/core/embedder.ts","../../src/core/search.ts","../../src/core/renderer.ts"],"sourcesContent":["import { readFileSync } from 'fs';\nimport { EmbedderRuntime } from '../core/embedder.js';\nimport { search } from '../core/search.js';\nimport { buildAnswer } from '../core/renderer.js';\nimport type { IndexChunk, IndexFile, SageDeskModel, FallbackReason } from '../core/types.js';\n\n// ─── Types ────────────────────────────────────────────────────────────────────\n\nexport interface SageDeskHandlerConfig {\n /** Filesystem path to the pre-built vector index (e.g. \"./public/support-index.json\"). */\n indexPath: string;\n /** LLM provider: 'openai', 'deepseek', 'groq', 'gemini', 'anthropic', or any OpenAI-compatible base URL. */\n provider: string;\n /** API key for the LLM provider. Never sent to the browser. */\n apiKey: string;\n /** LLM model name (e.g. 'deepseek-chat', 'gpt-4o-mini', 'llama3-8b-8192'). */\n model: string;\n /** Embedding model - must match the model used at build time. Defaults to all-MiniLM-L6-v2. */\n embeddingModel?: SageDeskModel;\n /** Number of chunks to retrieve for context. Defaults to 5. */\n topK?: number;\n /** Minimum similarity score for a chunk to be included. Defaults to 0.42. */\n minScore?: number;\n /** Override the system prompt sent to the LLM. */\n systemPrompt?: string;\n /** Timeout for LLM API calls in milliseconds. Defaults to 5000 (5 seconds). */\n llmTimeoutMs?: number;\n}\n\n// ─── Provider URL map ─────────────────────────────────────────────────────────\n\nconst PROVIDER_URLS: Record<string, string> = {\n openai: 'https://api.openai.com/v1/chat/completions',\n deepseek: 'https://api.deepseek.com/chat/completions',\n groq: 'https://api.groq.com/openai/v1/chat/completions',\n gemini: 'https://generativelanguage.googleapis.com/v1beta/openai/chat/completions',\n anthropic: 'https://api.anthropic.com/v1/messages',\n};\n\n// ─── Default system prompt ────────────────────────────────────────────────────\n\nconst DEFAULT_SYSTEM_PROMPT =\n 'You are a helpful support assistant. Answer the user\\'s question based ONLY on the ' +\n 'provided context. If the context does not contain a confident answer, respond with a ' +\n 'friendly message saying you don\\'t have that information right now. Do not make up ' +\n 'information or draw from outside knowledge. Be concise, warm, and helpful.';\n\n// ─── Server-side caches (module-level singletons) ─────────────────────────────\n\nconst indexCache = new Map<string, IndexChunk[]>();\nconst embedderCache = new Map<string, EmbedderRuntime>();\n\nfunction loadIndexFile(indexPath: string): IndexChunk[] {\n if (indexCache.has(indexPath)) return indexCache.get(indexPath)!;\n\n const raw = readFileSync(indexPath, 'utf-8');\n const data = JSON.parse(raw) as IndexFile | IndexChunk[];\n const chunks: IndexChunk[] = Array.isArray(data) ? data : data.chunks;\n\n for (const chunk of chunks) {\n chunk.textLower = chunk.text.toLowerCase();\n if (Array.isArray(chunk.vector384)) {\n chunk.vector384 = new Float32Array(chunk.vector384);\n }\n }\n\n indexCache.set(indexPath, chunks);\n return chunks;\n}\n\nasync function getEmbedder(model: SageDeskModel = 'all-MiniLM-L6-v2'): Promise<EmbedderRuntime> {\n if (embedderCache.has(model)) return embedderCache.get(model)!;\n\n const embedder = new EmbedderRuntime();\n await embedder.load(model);\n embedderCache.set(model, embedder);\n return embedder;\n}\n\n// ─── Helper: Classify error for client-side logging ───────────────────────────\n\nfunction classifyError(error: unknown): FallbackReason {\n const msg = String(error).toLowerCase();\n\n if (msg.includes('401') || msg.includes('403') || msg.includes('unauthorized') || msg.includes('invalid api key')) {\n return 'auth-error';\n }\n if (msg.includes('429') || msg.includes('quota') || msg.includes('rate limit')) {\n return 'quota-exceeded';\n }\n if (msg.includes('timeout') || msg.includes('aborted')) {\n return 'timeout';\n }\n if (msg.includes('malformed') || msg.includes('json')) {\n return 'malformed-response';\n }\n\n return 'api-error';\n}\n\n// ─── LLM call ─────────────────────────────────────────────────────────────────\n\nasync function callLLM(\n provider: string,\n apiKey: string,\n model: string,\n systemPrompt: string,\n query: string,\n context: string,\n timeoutMs: number = 5000\n): Promise<{ answer: string; error?: FallbackReason }> {\n const url = PROVIDER_URLS[provider] ?? provider;\n const controller = new AbortController();\n const timeoutHandle = setTimeout(() => controller.abort(), timeoutMs);\n\n try {\n if (provider === 'anthropic') {\n const res = await fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'x-api-key': apiKey,\n 'anthropic-version': '2023-06-01',\n },\n body: JSON.stringify({\n model,\n max_tokens: 512,\n system: systemPrompt,\n messages: [{ role: 'user', content: `Context:\\n${context}\\n\\nQuestion: ${query}` }],\n }),\n signal: controller.signal,\n });\n\n if (!res.ok) {\n const error = classifyError(`${res.status}`);\n return { answer: '', error };\n }\n\n const data = (await res.json()) as { content: Array<{ type: string; text: string }> };\n const answer = data.content?.[0]?.text?.trim() ?? '';\n return { answer };\n }\n\n const res = await fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${apiKey}`,\n },\n body: JSON.stringify({\n model,\n messages: [\n { role: 'system', content: systemPrompt },\n {\n role: 'user',\n content: `Context:\\n${context}\\n\\nQuestion: ${query}`,\n },\n ],\n temperature: 0.3,\n max_tokens: 512,\n }),\n signal: controller.signal,\n });\n\n if (!res.ok) {\n const error = classifyError(`${res.status}`);\n return { answer: '', error };\n }\n\n const data = (await res.json()) as {\n choices: Array<{ message: { content: string } }>;\n };\n const answer = data.choices?.[0]?.message?.content?.trim() ?? '';\n return { answer };\n } catch (err) {\n const error = classifyError(err);\n return { answer: '', error };\n } finally {\n clearTimeout(timeoutHandle);\n }\n}\n\n// ─── Core handler logic ───────────────────────────────────────────────────────\n\nasync function handleQuery(\n query: string,\n config: SageDeskHandlerConfig\n): Promise<{ answer: string; isFallback: boolean; fallbackReason?: FallbackReason }> {\n const {\n indexPath,\n provider,\n apiKey,\n model,\n embeddingModel,\n topK = 5,\n minScore = 0.42,\n systemPrompt = DEFAULT_SYSTEM_PROMPT,\n llmTimeoutMs = 5000,\n } = config;\n\n const index = loadIndexFile(indexPath);\n const embedder = await getEmbedder(embeddingModel);\n\n const queryVector = await embedder.embed(query);\n const results = search(queryVector, index, topK, minScore);\n\n if (results.length === 0) {\n return { answer: '', isFallback: true };\n }\n\n const context = buildAnswer(results);\n const llmResult = await callLLM(\n provider,\n apiKey,\n model,\n systemPrompt,\n query,\n context,\n llmTimeoutMs\n );\n\n if (!llmResult.answer) {\n return {\n answer: '',\n isFallback: true,\n fallbackReason: llmResult.error,\n };\n }\n\n return { answer: llmResult.answer, isFallback: false };\n}\n\n// ─── Next.js App Router handler ───────────────────────────────────────────────\n\n/**\n * Returns a Next.js App Router POST handler.\n *\n * @example\n * // app/api/sagedesk/route.ts\n * import { createSageDeskHandler } from 'sagedesk/server'\n * export const POST = createSageDeskHandler({\n * indexPath: './public/support-index.json',\n * provider: 'deepseek',\n * apiKey: process.env.SAGEDESK_LLM_API_KEY!,\n * model: 'deepseek-chat',\n * })\n */\nexport function createSageDeskHandler(config: SageDeskHandlerConfig) {\n return async function POST(request: Request): Promise<Response> {\n try {\n const body = (await request.json()) as { query?: string };\n const query = body.query?.trim();\n\n if (!query) {\n return Response.json({ error: 'Missing query' }, { status: 400 });\n }\n\n const result = await handleQuery(query, config);\n return Response.json(result);\n } catch (err) {\n console.error('[sagedesk/server] Handler error:', err);\n return Response.json({ answer: '', isFallback: true }, { status: 500 });\n }\n };\n}\n\n// ─── Express / Connect middleware ─────────────────────────────────────────────\n\ntype ExpressRequest = {\n body: { query?: string };\n};\ntype ExpressResponse = {\n status: (code: number) => ExpressResponse;\n json: (data: unknown) => void;\n};\ntype NextFunction = (err?: unknown) => void;\n\n/**\n * Returns an Express (or any Connect-compatible) middleware.\n *\n * @example\n * // server.ts / index.ts\n * import { createSageDeskMiddleware } from 'sagedesk/server'\n * app.use('/api/sagedesk', express.json(), createSageDeskMiddleware({\n * indexPath: './public/support-index.json',\n * provider: 'openai',\n * apiKey: process.env.SAGEDESK_LLM_API_KEY!,\n * model: 'gpt-4o-mini',\n * }))\n */\nexport function createSageDeskMiddleware(config: SageDeskHandlerConfig) {\n return async function sageDeskMiddleware(\n req: ExpressRequest,\n res: ExpressResponse,\n next: NextFunction\n ): Promise<void> {\n try {\n const query = req.body?.query?.trim();\n\n if (!query) {\n res.status(400).json({ error: 'Missing query' });\n return;\n }\n\n const result = await handleQuery(query, config);\n res.json(result);\n } catch (err) {\n console.error('[sagedesk/server] Middleware error:', err);\n next(err);\n }\n };\n}\n","import type { SageDeskModel } from './types';\n\ntype PipelineFn = (\n text: string,\n options: { pooling: string; normalize: boolean }\n) => Promise<{ data: Float32Array }>;\n\n// Maps each supported model alias to its full Xenova/HuggingFace model ID.\nconst XENOVA_IDS: Record<SageDeskModel, string> = {\n 'all-MiniLM-L6-v2': 'Xenova/all-MiniLM-L6-v2',\n 'bge-small-en-v1-5': 'Xenova/bge-small-en-v1.5',\n 'paraphrase-multilingual-MiniLM-L12-v2': 'Xenova/paraphrase-multilingual-MiniLM-L12-v2',\n 'all-mpnet-base-v2': 'Xenova/all-mpnet-base-v2',\n};\n\nexport class EmbedderRuntime {\n private _ready = false;\n private _failed = false;\n\n // Module-level singleton so the WASM model is loaded at most once per page,\n // regardless of how many widget instances exist.\n private static _pipelineInstance: PipelineFn | null = null;\n private static _loadingPromise: Promise<void> | null = null;\n\n async load(model: SageDeskModel = 'all-MiniLM-L6-v2'): Promise<void> {\n if (this._ready) return;\n if (this._failed) throw new Error('EmbedderRuntime previously failed to load');\n\n if (EmbedderRuntime._loadingPromise) {\n await EmbedderRuntime._loadingPromise;\n this._ready = true;\n return;\n }\n\n const modelId = XENOVA_IDS[model];\n\n EmbedderRuntime._loadingPromise = (async () => {\n try {\n const { pipeline } = await import('@huggingface/transformers');\n EmbedderRuntime._pipelineInstance = (await pipeline(\n 'feature-extraction',\n modelId,\n { dtype: 'q8' }\n )) as unknown as PipelineFn;\n } catch (err) {\n EmbedderRuntime._loadingPromise = null;\n EmbedderRuntime._pipelineInstance = null;\n throw err;\n }\n })();\n\n try {\n await EmbedderRuntime._loadingPromise;\n this._ready = true;\n } catch (err) {\n this._failed = true;\n throw err;\n }\n }\n\n async embed(text: string): Promise<Float32Array> {\n if (!EmbedderRuntime._pipelineInstance) {\n await this.load();\n }\n\n try {\n const output = await EmbedderRuntime._pipelineInstance!(text, {\n pooling: 'mean',\n normalize: true,\n });\n return output.data;\n } catch (err) {\n throw new Error(`Embedding failed: ${String(err)}`);\n }\n }\n\n get isReady(): boolean {\n return this._ready;\n }\n\n get hasFailed(): boolean {\n return this._failed;\n }\n\n /** @internal */\n static _reset(): void {\n EmbedderRuntime._pipelineInstance = null;\n EmbedderRuntime._loadingPromise = null;\n }\n}\n","import type { IndexChunk, SearchResult } from './types';\n\n// Both the query vector (embedder.ts, normalize:true) and stored vectors\n// (builder-embedder.ts, normalize:true) are guaranteed unit-length, so\n// cosine similarity reduces to a plain dot product - no norms needed.\nfunction dotProduct(a: Float32Array, b: Float32Array): number {\n if (a.length !== b.length) {\n throw new Error(`Vector dimension mismatch: query(${a.length}) vs index(${b.length})`);\n }\n let dot = 0;\n for (let i = 0; i < a.length; i++) dot += a[i] * b[i];\n return dot;\n}\n\nexport function search(\n queryVector: Float32Array,\n index: IndexChunk[],\n topK = 3,\n minScore = 0.42\n): SearchResult[] {\n // Use a simple selection sort approach for small topK, \n // or a full sort if index is small. For very large indexes, \n // a proper Heap would be better, but O(N * topK) is already better than O(N log N).\n const results: SearchResult[] = [];\n\n for (const chunk of index) {\n const score = dotProduct(queryVector, chunk.vector384 as Float32Array);\n if (score < minScore) continue;\n\n if (results.length < topK) {\n results.push({ chunk, score });\n results.sort((a, b) => b.score - a.score);\n } else if (score > results[topK - 1].score) {\n results[topK - 1] = { chunk, score };\n results.sort((a, b) => b.score - a.score);\n }\n }\n\n return results;\n}\n\nexport function keywordSearch(\n query: string,\n index: IndexChunk[],\n topK = 3\n): SearchResult[] {\n const terms = query\n .toLowerCase()\n .split(/\\s+/)\n .filter((w) => w.length > 2)\n .map((w) => w.replace(/[^a-z0-9]/g, ''));\n\n if (terms.length === 0) return [];\n\n const results: SearchResult[] = [];\n\n for (const chunk of index) {\n const chunkLower = chunk.textLower || chunk.text.toLowerCase();\n const matchCount = terms.filter((t) => chunkLower.includes(t)).length;\n const score = matchCount / terms.length;\n\n if (score <= 0) continue;\n\n if (results.length < topK) {\n results.push({ chunk, score });\n results.sort((a, b) => b.score - a.score);\n } else if (score > results[topK - 1].score) {\n results[topK - 1] = { chunk, score };\n results.sort((a, b) => b.score - a.score);\n }\n }\n\n return results;\n}\n\nexport async function loadIndex(url: string): Promise<IndexChunk[]> {\n const res = await fetch(url);\n if (!res.ok) {\n throw new Error(`Failed to fetch index (HTTP ${res.status}): ${url}`);\n }\n const data = await res.json();\n // Support both the new { meta, chunks } format and the legacy bare-array format.\n const chunks: IndexChunk[] = Array.isArray(data)\n ? data\n : (data as { chunks: IndexChunk[] }).chunks;\n\n // Materialize lowercase versions and convert vectors to Float32Array once at load time.\n for (const chunk of chunks) {\n chunk.textLower = chunk.text.toLowerCase();\n if (Array.isArray(chunk.vector384)) {\n chunk.vector384 = new Float32Array(chunk.vector384);\n }\n if (Array.isArray(chunk.vector768)) {\n chunk.vector768 = new Float32Array(chunk.vector768);\n }\n }\n\n return chunks;\n}\n","import type { SearchResult } from './types';\n\nexport function buildAnswer(results: SearchResult[]): string {\n if (results.length === 0) return '';\n // Deduplicate by sourceId: query expansion produces multiple chunks per\n // source entry (same answer, different query phrasings) - show each source once.\n const seen = new Set<string>();\n const parts: string[] = [];\n for (const r of results) {\n if (!seen.has(r.chunk.sourceId)) {\n seen.add(r.chunk.sourceId);\n parts.push(r.chunk.text);\n }\n }\n return parts.join('\\n\\n');\n}\n\nexport function extractChips(\n index: { text: string; question?: string; sourceId?: string }[],\n override?: string[]\n): string[] {\n if (override && override.length > 0) return override.slice(0, 5);\n\n const chips: string[] = [];\n const seenText = new Set<string>();\n const seenSource = new Set<string>();\n\n for (const chunk of index) {\n if (chips.length >= 5) break;\n\n // Deduplicate by sourceId if available to ensure variety of answers.\n if (chunk.sourceId) {\n if (seenSource.has(chunk.sourceId)) continue;\n seenSource.add(chunk.sourceId);\n }\n\n const candidate = chunk.question ?? extractFirstSentence(chunk.text);\n if (candidate && !seenText.has(candidate)) {\n seenText.add(candidate);\n chips.push(candidate);\n }\n }\n\n return chips;\n}\n\nfunction extractFirstSentence(text: string): string {\n const match = text.match(/^[^\\n.!?]{10,80}[.!?\\n]?/);\n if (!match) return text.slice(0, 60);\n return match[0].trim();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAA6B;;;ACQ7B,IAAM,aAA4C;AAAA,EAChD,oBAAoB;AAAA,EACpB,qBAAqB;AAAA,EACrB,yCAAyC;AAAA,EACzC,qBAAqB;AACvB;AAEO,IAAM,kBAAN,MAAM,iBAAgB;AAAA,EAAtB;AACL,SAAQ,SAAS;AACjB,SAAQ,UAAU;AAAA;AAAA,EAIlB;AAAA;AAAA;AAAA,SAAe,oBAAuC;AAAA;AAAA,EACtD;AAAA,SAAe,kBAAwC;AAAA;AAAA,EAEvD,MAAM,KAAK,QAAuB,oBAAmC;AACnE,QAAI,KAAK,OAAQ;AACjB,QAAI,KAAK,QAAS,OAAM,IAAI,MAAM,2CAA2C;AAE7E,QAAI,iBAAgB,iBAAiB;AACnC,YAAM,iBAAgB;AACtB,WAAK,SAAS;AACd;AAAA,IACF;AAEA,UAAM,UAAU,WAAW,KAAK;AAEhC,qBAAgB,mBAAmB,YAAY;AAC7C,UAA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Y,OAAO,MAAM;AAC9C,aAAO,SAAS,KAAK,MAAM;AAAA,IAC7B,SAAS,KAAK;AACZ,cAAQ,MAAM,oCAAoC,GAAG;AACrD,aAAO,SAAS,KAAK,EAAE,QAAQ,IAAI,YAAY,KAAK,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,IACxE;AAAA,EACF;AACF;AA0BO,SAAS,yBAAyB,QAA+B;AACtE,SAAO,eAAe,mBACpB,KACA,KACA,MACe;AACf,QAAI;AACF,YAAM,QAAQ,IAAI,MAAM,OAAO,KAAK;AAEpC,UAAI,CAAC,OAAO;AACV,YAAI,OAAO,GAAG,EAAE,KAAK,EAAE,OAAO,gBAAgB,CAAC;AAC/C;AAAA,MACF;AAEA,YAAM,SAAS,MAAM,YAAY,OAAO,MAAM;AAC9C,UAAI,KAAK,MAAM;AAAA,IACjB,SAAS,KAAK;AACZ,cAAQ,MAAM,uCAAuC,GAAG;AACxD,WAAK,GAAG;AAAA,IACV;AAAA,EACF;AACF;","names":["res","data","answer"]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
type SageDeskModel = 'all-MiniLM-L6-v2' | 'bge-small-en-v1-5' | 'paraphrase-multilingual-MiniLM-L12-v2' | 'all-mpnet-base-v2';
|
|
2
|
+
|
|
3
|
+
interface SageDeskHandlerConfig {
|
|
4
|
+
/** Filesystem path to the pre-built vector index (e.g. "./public/support-index.json"). */
|
|
5
|
+
indexPath: string;
|
|
6
|
+
/** LLM provider: 'openai', 'deepseek', 'groq', 'gemini', 'anthropic', or any OpenAI-compatible base URL. */
|
|
7
|
+
provider: string;
|
|
8
|
+
/** API key for the LLM provider. Never sent to the browser. */
|
|
9
|
+
apiKey: string;
|
|
10
|
+
/** LLM model name (e.g. 'deepseek-chat', 'gpt-4o-mini', 'llama3-8b-8192'). */
|
|
11
|
+
model: string;
|
|
12
|
+
/** Embedding model - must match the model used at build time. Defaults to all-MiniLM-L6-v2. */
|
|
13
|
+
embeddingModel?: SageDeskModel;
|
|
14
|
+
/** Number of chunks to retrieve for context. Defaults to 5. */
|
|
15
|
+
topK?: number;
|
|
16
|
+
/** Minimum similarity score for a chunk to be included. Defaults to 0.42. */
|
|
17
|
+
minScore?: number;
|
|
18
|
+
/** Override the system prompt sent to the LLM. */
|
|
19
|
+
systemPrompt?: string;
|
|
20
|
+
/** Timeout for LLM API calls in milliseconds. Defaults to 5000 (5 seconds). */
|
|
21
|
+
llmTimeoutMs?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Returns a Next.js App Router POST handler.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // app/api/sagedesk/route.ts
|
|
28
|
+
* import { createSageDeskHandler } from 'sagedesk/server'
|
|
29
|
+
* export const POST = createSageDeskHandler({
|
|
30
|
+
* indexPath: './public/support-index.json',
|
|
31
|
+
* provider: 'deepseek',
|
|
32
|
+
* apiKey: process.env.SAGEDESK_LLM_API_KEY!,
|
|
33
|
+
* model: 'deepseek-chat',
|
|
34
|
+
* })
|
|
35
|
+
*/
|
|
36
|
+
declare function createSageDeskHandler(config: SageDeskHandlerConfig): (request: Request) => Promise<Response>;
|
|
37
|
+
type ExpressRequest = {
|
|
38
|
+
body: {
|
|
39
|
+
query?: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
type ExpressResponse = {
|
|
43
|
+
status: (code: number) => ExpressResponse;
|
|
44
|
+
json: (data: unknown) => void;
|
|
45
|
+
};
|
|
46
|
+
type NextFunction = (err?: unknown) => void;
|
|
47
|
+
/**
|
|
48
|
+
* Returns an Express (or any Connect-compatible) middleware.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // server.ts / index.ts
|
|
52
|
+
* import { createSageDeskMiddleware } from 'sagedesk/server'
|
|
53
|
+
* app.use('/api/sagedesk', express.json(), createSageDeskMiddleware({
|
|
54
|
+
* indexPath: './public/support-index.json',
|
|
55
|
+
* provider: 'openai',
|
|
56
|
+
* apiKey: process.env.SAGEDESK_LLM_API_KEY!,
|
|
57
|
+
* model: 'gpt-4o-mini',
|
|
58
|
+
* }))
|
|
59
|
+
*/
|
|
60
|
+
declare function createSageDeskMiddleware(config: SageDeskHandlerConfig): (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => Promise<void>;
|
|
61
|
+
|
|
62
|
+
export { type SageDeskHandlerConfig, createSageDeskHandler, createSageDeskMiddleware };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
type SageDeskModel = 'all-MiniLM-L6-v2' | 'bge-small-en-v1-5' | 'paraphrase-multilingual-MiniLM-L12-v2' | 'all-mpnet-base-v2';
|
|
2
|
+
|
|
3
|
+
interface SageDeskHandlerConfig {
|
|
4
|
+
/** Filesystem path to the pre-built vector index (e.g. "./public/support-index.json"). */
|
|
5
|
+
indexPath: string;
|
|
6
|
+
/** LLM provider: 'openai', 'deepseek', 'groq', 'gemini', 'anthropic', or any OpenAI-compatible base URL. */
|
|
7
|
+
provider: string;
|
|
8
|
+
/** API key for the LLM provider. Never sent to the browser. */
|
|
9
|
+
apiKey: string;
|
|
10
|
+
/** LLM model name (e.g. 'deepseek-chat', 'gpt-4o-mini', 'llama3-8b-8192'). */
|
|
11
|
+
model: string;
|
|
12
|
+
/** Embedding model - must match the model used at build time. Defaults to all-MiniLM-L6-v2. */
|
|
13
|
+
embeddingModel?: SageDeskModel;
|
|
14
|
+
/** Number of chunks to retrieve for context. Defaults to 5. */
|
|
15
|
+
topK?: number;
|
|
16
|
+
/** Minimum similarity score for a chunk to be included. Defaults to 0.42. */
|
|
17
|
+
minScore?: number;
|
|
18
|
+
/** Override the system prompt sent to the LLM. */
|
|
19
|
+
systemPrompt?: string;
|
|
20
|
+
/** Timeout for LLM API calls in milliseconds. Defaults to 5000 (5 seconds). */
|
|
21
|
+
llmTimeoutMs?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Returns a Next.js App Router POST handler.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // app/api/sagedesk/route.ts
|
|
28
|
+
* import { createSageDeskHandler } from 'sagedesk/server'
|
|
29
|
+
* export const POST = createSageDeskHandler({
|
|
30
|
+
* indexPath: './public/support-index.json',
|
|
31
|
+
* provider: 'deepseek',
|
|
32
|
+
* apiKey: process.env.SAGEDESK_LLM_API_KEY!,
|
|
33
|
+
* model: 'deepseek-chat',
|
|
34
|
+
* })
|
|
35
|
+
*/
|
|
36
|
+
declare function createSageDeskHandler(config: SageDeskHandlerConfig): (request: Request) => Promise<Response>;
|
|
37
|
+
type ExpressRequest = {
|
|
38
|
+
body: {
|
|
39
|
+
query?: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
type ExpressResponse = {
|
|
43
|
+
status: (code: number) => ExpressResponse;
|
|
44
|
+
json: (data: unknown) => void;
|
|
45
|
+
};
|
|
46
|
+
type NextFunction = (err?: unknown) => void;
|
|
47
|
+
/**
|
|
48
|
+
* Returns an Express (or any Connect-compatible) middleware.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // server.ts / index.ts
|
|
52
|
+
* import { createSageDeskMiddleware } from 'sagedesk/server'
|
|
53
|
+
* app.use('/api/sagedesk', express.json(), createSageDeskMiddleware({
|
|
54
|
+
* indexPath: './public/support-index.json',
|
|
55
|
+
* provider: 'openai',
|
|
56
|
+
* apiKey: process.env.SAGEDESK_LLM_API_KEY!,
|
|
57
|
+
* model: 'gpt-4o-mini',
|
|
58
|
+
* }))
|
|
59
|
+
*/
|
|
60
|
+
declare function createSageDeskMiddleware(config: SageDeskHandlerConfig): (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => Promise<void>;
|
|
61
|
+
|
|
62
|
+
export { type SageDeskHandlerConfig, createSageDeskHandler, createSageDeskMiddleware };
|