chatbotlite 0.0.1 → 0.3.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/LICENSE +201 -0
- package/README.md +354 -3
- package/dist/client/index.cjs +234 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +72 -0
- package/dist/client/index.d.ts +72 -0
- package/dist/client/index.js +229 -0
- package/dist/client/index.js.map +1 -0
- package/dist/core/index.cjs +73 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +29 -0
- package/dist/core/index.d.ts +29 -0
- package/dist/core/index.js +68 -0
- package/dist/core/index.js.map +1 -0
- package/dist/index.cjs +238 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +229 -0
- package/dist/index.js.map +1 -0
- package/dist/node/index.cjs +41 -0
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.d.cts +37 -0
- package/dist/node/index.d.ts +37 -0
- package/dist/node/index.js +38 -0
- package/dist/node/index.js.map +1 -0
- package/dist/react/index.cjs +594 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +40 -0
- package/dist/react/index.d.ts +40 -0
- package/dist/react/index.js +592 -0
- package/dist/react/index.js.map +1 -0
- package/dist/types-4alyzg8O.d.cts +16 -0
- package/dist/types-4alyzg8O.d.ts +16 -0
- package/dist/types-J7BXpiRU.d.cts +63 -0
- package/dist/types-J7BXpiRU.d.ts +63 -0
- package/package.json +92 -6
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/client/types.ts
|
|
4
|
+
var PROVIDER_NAMES = /* @__PURE__ */ new Set([
|
|
5
|
+
"openai",
|
|
6
|
+
"deepseek",
|
|
7
|
+
"groq",
|
|
8
|
+
"gemini",
|
|
9
|
+
"anthropic",
|
|
10
|
+
"cerebras",
|
|
11
|
+
"sambanova",
|
|
12
|
+
"fireworks",
|
|
13
|
+
"mistral",
|
|
14
|
+
"openrouter",
|
|
15
|
+
"moonshot"
|
|
16
|
+
]);
|
|
17
|
+
function isKnownProvider(name) {
|
|
18
|
+
return PROVIDER_NAMES.has(name);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/client/providers.ts
|
|
22
|
+
var PROVIDER_ENDPOINTS = {
|
|
23
|
+
openai: { baseUrl: "https://api.openai.com/v1", defaultModel: "gpt-4o-mini" },
|
|
24
|
+
deepseek: { baseUrl: "https://api.deepseek.com/v1", defaultModel: "deepseek-chat" },
|
|
25
|
+
groq: { baseUrl: "https://api.groq.com/openai/v1", defaultModel: "llama-3.3-70b-versatile" },
|
|
26
|
+
gemini: { baseUrl: "https://generativelanguage.googleapis.com/v1beta/openai", defaultModel: "gemini-2.5-flash" },
|
|
27
|
+
anthropic: { baseUrl: "https://api.anthropic.com/v1", defaultModel: "claude-haiku-4-5" },
|
|
28
|
+
cerebras: { baseUrl: "https://api.cerebras.ai/v1", defaultModel: "qwen-3-235b-a22b-instruct-2507" },
|
|
29
|
+
sambanova: { baseUrl: "https://api.sambanova.ai/v1", defaultModel: "Meta-Llama-3.3-70B-Instruct" },
|
|
30
|
+
fireworks: { baseUrl: "https://api.fireworks.ai/inference/v1", defaultModel: "accounts/fireworks/models/llama-v3p3-70b-instruct" },
|
|
31
|
+
mistral: { baseUrl: "https://api.mistral.ai/v1", defaultModel: "mistral-small-latest" },
|
|
32
|
+
openrouter: { baseUrl: "https://openrouter.ai/api/v1", defaultModel: "deepseek/deepseek-chat" },
|
|
33
|
+
moonshot: { baseUrl: "https://api.moonshot.ai/v1", defaultModel: "moonshot-v1-32k" }
|
|
34
|
+
};
|
|
35
|
+
function isRetryableError(err) {
|
|
36
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
37
|
+
return /\b(429|rate.?limit|quota|exceed|5\d\d|timeout|ECONNRESET|fetch failed)\b/i.test(msg);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/core/prompts.ts
|
|
41
|
+
function buildSystemPrompt(knowledge) {
|
|
42
|
+
return [
|
|
43
|
+
"You are an AI assistant on a business website. Use ONLY the knowledge below to answer.",
|
|
44
|
+
"",
|
|
45
|
+
"## Business knowledge",
|
|
46
|
+
knowledge.trim(),
|
|
47
|
+
"",
|
|
48
|
+
"## Reply rules",
|
|
49
|
+
"- Reply in 1-2 short sentences, conversational tone.",
|
|
50
|
+
"- NEVER invent prices, availability, dispatch times, appointment confirmations, or facts not present in the business knowledge above.",
|
|
51
|
+
"- For anything not covered in the knowledge above, say the owner will follow up \u2014 do NOT guess.",
|
|
52
|
+
'- If the caller is clearly a vendor/sales pitch, say: "This does not look like a customer service request, so we will not continue this thread."',
|
|
53
|
+
`- If wrong number or asked to stop, say: "Sorry about that. We won't text again."`,
|
|
54
|
+
"- Match the caller's language automatically."
|
|
55
|
+
].join("\n");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/core/guards.ts
|
|
59
|
+
var FORBIDDEN_PHRASES = [
|
|
60
|
+
"help is coming",
|
|
61
|
+
"someone is on the way",
|
|
62
|
+
"technician is on the way",
|
|
63
|
+
"provider is on the way",
|
|
64
|
+
"dispatching someone",
|
|
65
|
+
"i've booked",
|
|
66
|
+
"i have booked",
|
|
67
|
+
"reservation confirmed",
|
|
68
|
+
"your appointment is confirmed",
|
|
69
|
+
"i've scheduled",
|
|
70
|
+
"i have scheduled",
|
|
71
|
+
"we've dispatched",
|
|
72
|
+
"we have dispatched",
|
|
73
|
+
"i can confirm",
|
|
74
|
+
"i guarantee",
|
|
75
|
+
"guaranteed delivery",
|
|
76
|
+
"guaranteed arrival",
|
|
77
|
+
"will arrive at",
|
|
78
|
+
"arriving at",
|
|
79
|
+
"i'll send",
|
|
80
|
+
"i will send"
|
|
81
|
+
];
|
|
82
|
+
function checkForbiddenPhrases(reply) {
|
|
83
|
+
const lower = reply.toLowerCase();
|
|
84
|
+
const violations = [];
|
|
85
|
+
for (const phrase of FORBIDDEN_PHRASES) {
|
|
86
|
+
if (lower.includes(phrase)) {
|
|
87
|
+
violations.push(`Forbidden phrase: "${phrase}"`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { ok: violations.length === 0, violations };
|
|
91
|
+
}
|
|
92
|
+
function stripForbidden(reply) {
|
|
93
|
+
const sentences = reply.split(/(?<=[.!?])\s+/);
|
|
94
|
+
const kept = sentences.filter((s) => {
|
|
95
|
+
const lower = s.toLowerCase();
|
|
96
|
+
return !FORBIDDEN_PHRASES.some((p) => lower.includes(p));
|
|
97
|
+
});
|
|
98
|
+
const trimmed = kept.join(" ").trim();
|
|
99
|
+
if (trimmed.length < 10) {
|
|
100
|
+
return "Thanks for reaching out \u2014 let me check with the owner and get back to you.";
|
|
101
|
+
}
|
|
102
|
+
return trimmed;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/client/chatbot.ts
|
|
106
|
+
var ChatBot = class {
|
|
107
|
+
steps;
|
|
108
|
+
keys;
|
|
109
|
+
fetcher;
|
|
110
|
+
timeoutMs;
|
|
111
|
+
cachedSystemPrompt;
|
|
112
|
+
constructor(init) {
|
|
113
|
+
if (!init.knowledge || typeof init.knowledge !== "string" || init.knowledge.trim().length === 0) {
|
|
114
|
+
throw new Error("chatbotlite: knowledge is required (a non-empty markdown string).");
|
|
115
|
+
}
|
|
116
|
+
this.keys = init.providers.keys ?? {};
|
|
117
|
+
this.steps = resolveChain(init.providers);
|
|
118
|
+
this.fetcher = init.options?.fetch ?? globalThis.fetch.bind(globalThis);
|
|
119
|
+
this.timeoutMs = init.options?.timeoutMs ?? 3e4;
|
|
120
|
+
this.cachedSystemPrompt = buildSystemPrompt(init.knowledge);
|
|
121
|
+
}
|
|
122
|
+
async reply(message, opts = {}) {
|
|
123
|
+
const systemPrompt = opts.systemPrompt ?? this.cachedSystemPrompt;
|
|
124
|
+
const messages = [
|
|
125
|
+
{ role: "system", content: systemPrompt },
|
|
126
|
+
...opts.history ?? [],
|
|
127
|
+
{ role: "user", content: message }
|
|
128
|
+
];
|
|
129
|
+
const attempts = [];
|
|
130
|
+
let lastError;
|
|
131
|
+
for (const step of this.steps) {
|
|
132
|
+
const t0 = Date.now();
|
|
133
|
+
try {
|
|
134
|
+
const result = await this.callProvider(step, messages);
|
|
135
|
+
attempts.push({ provider: step.provider, model: step.model, status: "ok", latencyMs: Date.now() - t0 });
|
|
136
|
+
const guard = checkForbiddenPhrases(result.reply);
|
|
137
|
+
const finalReply = guard.ok ? result.reply : stripForbidden(result.reply);
|
|
138
|
+
return {
|
|
139
|
+
reply: finalReply,
|
|
140
|
+
usedProvider: step.provider,
|
|
141
|
+
usedModel: step.model,
|
|
142
|
+
...result.usage ? { usage: result.usage } : {},
|
|
143
|
+
guardWarnings: guard.violations,
|
|
144
|
+
attempts
|
|
145
|
+
};
|
|
146
|
+
} catch (err) {
|
|
147
|
+
lastError = err;
|
|
148
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
149
|
+
attempts.push({
|
|
150
|
+
provider: step.provider,
|
|
151
|
+
model: step.model,
|
|
152
|
+
status: "error",
|
|
153
|
+
error: errMsg,
|
|
154
|
+
latencyMs: Date.now() - t0
|
|
155
|
+
});
|
|
156
|
+
if (!isRetryableError(err)) {
|
|
157
|
+
throw new Error(`chatbotlite: ${step.label} failed (non-retryable). ${errMsg}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const summary = attempts.map((a) => `${a.provider}/${a.model}:${a.error ?? "ok"}`).join(" \u2192 ");
|
|
162
|
+
throw new Error(`chatbotlite: all chain steps failed. Trace: ${summary}. Last error: ${lastError instanceof Error ? lastError.message : String(lastError)}`);
|
|
163
|
+
}
|
|
164
|
+
async callProvider(step, messages) {
|
|
165
|
+
const endpoint = PROVIDER_ENDPOINTS[step.provider];
|
|
166
|
+
const key = this.keys[step.provider];
|
|
167
|
+
if (!key) throw new Error(`Missing API key for provider: ${step.provider}`);
|
|
168
|
+
const controller = new AbortController();
|
|
169
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
170
|
+
try {
|
|
171
|
+
const res = await this.fetcher(`${endpoint.baseUrl}/chat/completions`, {
|
|
172
|
+
method: "POST",
|
|
173
|
+
headers: {
|
|
174
|
+
"Authorization": `Bearer ${key}`,
|
|
175
|
+
"Content-Type": "application/json"
|
|
176
|
+
},
|
|
177
|
+
body: JSON.stringify({
|
|
178
|
+
model: step.model,
|
|
179
|
+
messages,
|
|
180
|
+
temperature: 0.3,
|
|
181
|
+
max_tokens: 300
|
|
182
|
+
}),
|
|
183
|
+
signal: controller.signal
|
|
184
|
+
});
|
|
185
|
+
if (!res.ok) {
|
|
186
|
+
const body = await res.text();
|
|
187
|
+
throw new Error(`${res.status}: ${body.slice(0, 200)}`);
|
|
188
|
+
}
|
|
189
|
+
const data = await res.json();
|
|
190
|
+
const msg = data.choices?.[0]?.message;
|
|
191
|
+
const reply = (msg?.content?.trim() || msg?.reasoning_content?.trim()) ?? "";
|
|
192
|
+
if (!reply) throw new Error("empty reply from provider");
|
|
193
|
+
const result = { reply };
|
|
194
|
+
if (data.usage) result.usage = data.usage;
|
|
195
|
+
return result;
|
|
196
|
+
} finally {
|
|
197
|
+
clearTimeout(timer);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
function resolveChain(providers) {
|
|
202
|
+
const keys = providers.keys ?? {};
|
|
203
|
+
const explicit = providers.chain;
|
|
204
|
+
if (explicit && explicit.length > 0) {
|
|
205
|
+
return explicit.map((entry) => normalizeChainEntry(entry, keys));
|
|
206
|
+
}
|
|
207
|
+
const orderedProviders = Object.keys(keys).filter((k) => isKnownProvider(k) && keys[k]);
|
|
208
|
+
if (orderedProviders.length === 0) {
|
|
209
|
+
throw new Error("chatbotlite: at least one provider key is required.");
|
|
210
|
+
}
|
|
211
|
+
return orderedProviders.map((provider) => ({
|
|
212
|
+
provider,
|
|
213
|
+
model: PROVIDER_ENDPOINTS[provider].defaultModel,
|
|
214
|
+
label: `${provider}/${PROVIDER_ENDPOINTS[provider].defaultModel}`
|
|
215
|
+
}));
|
|
216
|
+
}
|
|
217
|
+
function normalizeChainEntry(entry, keys) {
|
|
218
|
+
if (!isKnownProvider(entry.provider)) {
|
|
219
|
+
throw new Error(`chatbotlite: unknown provider "${entry.provider}" in chain entry.`);
|
|
220
|
+
}
|
|
221
|
+
const provider = entry.provider;
|
|
222
|
+
const model = entry.model ?? PROVIDER_ENDPOINTS[provider].defaultModel;
|
|
223
|
+
if (!keys[provider]) {
|
|
224
|
+
throw new Error(`chatbotlite: chain entry for "${provider}" needs a matching key in providers.keys.`);
|
|
225
|
+
}
|
|
226
|
+
return { provider, model, label: `${provider}/${model}` };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
exports.ChatBot = ChatBot;
|
|
230
|
+
exports.PROVIDER_ENDPOINTS = PROVIDER_ENDPOINTS;
|
|
231
|
+
exports.isKnownProvider = isKnownProvider;
|
|
232
|
+
exports.isRetryableError = isRetryableError;
|
|
233
|
+
//# sourceMappingURL=index.cjs.map
|
|
234
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/types.ts","../../src/client/providers.ts","../../src/core/prompts.ts","../../src/core/guards.ts","../../src/client/chatbot.ts"],"names":[],"mappings":";;;AA+EA,IAAM,cAAA,uBAA0C,GAAA,CAAI;AAAA,EAClD,QAAA;AAAA,EAAU,UAAA;AAAA,EAAY,MAAA;AAAA,EAAQ,QAAA;AAAA,EAAU,WAAA;AAAA,EACxC,UAAA;AAAA,EAAY,WAAA;AAAA,EAAa,WAAA;AAAA,EAAa,SAAA;AAAA,EAAW,YAAA;AAAA,EAAc;AACjE,CAAC,CAAA;AAEM,SAAS,gBAAgB,IAAA,EAAgC;AAC9D,EAAA,OAAO,cAAA,CAAe,IAAI,IAAI,CAAA;AAChC;;;AC3EO,IAAM,kBAAA,GAAyD;AAAA,EACpE,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B,QAAQ,CAAA,CAAE,YAAA;AAC1D,EAAA,IAAI,CAAC,IAAA,CAAK,QAAQ,CAAA,EAAG;AACnB,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,8BAAA,EAAiC,QAAQ,CAAA,yCAAA,CAA2C,CAAA;AAAA,EACtG;AACA,EAAA,OAAO,EAAE,UAAU,KAAA,EAAO,KAAA,EAAO,GAAG,QAAQ,CAAA,CAAA,EAAI,KAAK,CAAA,CAAA,EAAG;AAC1D","file":"index.cjs","sourcesContent":["// Client types — provider + chain config\n\nexport type Provider =\n | \"openai\"\n | \"deepseek\"\n | \"groq\"\n | \"gemini\"\n | \"anthropic\"\n | \"cerebras\"\n | \"sambanova\"\n | \"fireworks\"\n | \"mistral\"\n | \"openrouter\"\n | \"moonshot\";\n\n/**\n * One step in the fallback chain. Provider is required; model defaults to the provider's preset.\n */\nexport interface ChainEntry {\n provider: Provider;\n model?: string;\n}\n\n/**\n * Provider configuration.\n *\n * `keys` are auth credentials (one key per provider — that key covers all that provider's models).\n * `chain` is the ordered fallback list. Each entry is `{ provider, model? }`.\n *\n * @example\n * ```ts\n * providers: {\n * keys: {\n * deepseek: \"sk-...\",\n * groq: \"gsk-...\",\n * openai: \"sk-...\"\n * },\n * chain: [\n * { provider: \"deepseek\", model: \"deepseek-chat\" },\n * { provider: \"groq\", model: \"llama-3.3-70b-versatile\" },\n * { provider: \"openai\", model: \"gpt-4o-mini\" }\n * ]\n * }\n * ```\n *\n * If `chain` is omitted, defaults to one entry per key (in insertion order) using each provider's\n * default model.\n */\nexport interface ProviderConfig {\n /** API keys per provider. One key covers all that provider's models. */\n keys: Partial<Record<Provider, string>>;\n /**\n * Ordered fallback chain. Each entry: `{ provider, model? }`.\n * Omit `model` to use the provider's default model.\n * Omit `chain` entirely to auto-build from keys.\n */\n chain?: ChainEntry[];\n}\n\nexport interface ClientOptions {\n fetch?: typeof globalThis.fetch;\n timeoutMs?: number;\n}\n\nexport interface ChainStep {\n provider: Provider;\n model: string;\n /** Human-readable label used in attempt traces, e.g. `\"openai/gpt-4o-mini\"`. */\n label: string;\n}\n\nexport interface AttemptInfo {\n provider: Provider;\n model: string;\n status: \"ok\" | \"error\";\n error?: string;\n latencyMs: number;\n}\n\nconst PROVIDER_NAMES: ReadonlySet<string> = new Set([\n \"openai\", \"deepseek\", \"groq\", \"gemini\", \"anthropic\",\n \"cerebras\", \"sambanova\", \"fireworks\", \"mistral\", \"openrouter\", \"moonshot\"\n]);\n\nexport function isKnownProvider(name: string): name is Provider {\n return PROVIDER_NAMES.has(name);\n}\n","import type { Provider } from \"./types.js\";\n\nexport interface ProviderEndpoint {\n baseUrl: string;\n defaultModel: string;\n}\n\n/**\n * Built-in OpenAI-compatible providers. All use /v1/chat/completions\n * with response in OpenAI format. Caller supplies API key per provider.\n */\nexport const PROVIDER_ENDPOINTS: Record<Provider, ProviderEndpoint> = {\n openai: { baseUrl: \"https://api.openai.com/v1\", defaultModel: \"gpt-4o-mini\" },\n deepseek: { baseUrl: \"https://api.deepseek.com/v1\", defaultModel: \"deepseek-chat\" },\n groq: { baseUrl: \"https://api.groq.com/openai/v1\", defaultModel: \"llama-3.3-70b-versatile\" },\n gemini: { baseUrl: \"https://generativelanguage.googleapis.com/v1beta/openai\", defaultModel: \"gemini-2.5-flash\" },\n anthropic: { baseUrl: \"https://api.anthropic.com/v1\", defaultModel: \"claude-haiku-4-5\" },\n cerebras: { baseUrl: \"https://api.cerebras.ai/v1\", defaultModel: \"qwen-3-235b-a22b-instruct-2507\" },\n sambanova: { baseUrl: \"https://api.sambanova.ai/v1\", defaultModel: \"Meta-Llama-3.3-70B-Instruct\" },\n fireworks: { baseUrl: \"https://api.fireworks.ai/inference/v1\", defaultModel: \"accounts/fireworks/models/llama-v3p3-70b-instruct\" },\n mistral: { baseUrl: \"https://api.mistral.ai/v1\", defaultModel: \"mistral-small-latest\" },\n openrouter: { baseUrl: \"https://openrouter.ai/api/v1\", defaultModel: \"deepseek/deepseek-chat\" },\n moonshot: { baseUrl: \"https://api.moonshot.ai/v1\", defaultModel: \"moonshot-v1-32k\" }\n};\n\nexport function isRetryableError(err: unknown): boolean {\n const msg = err instanceof Error ? err.message : String(err);\n return /\\b(429|rate.?limit|quota|exceed|5\\d\\d|timeout|ECONNRESET|fetch failed)\\b/i.test(msg);\n}\n","import type { Knowledge } from \"./types.js\";\n\n/**\n * Build the system prompt by wrapping the user's markdown knowledge\n * with anti-hallucination rules and reply-style guidance.\n *\n * The markdown is injected verbatim — headings, lists, tables all preserved.\n * Works for any vertical because we don't enforce a schema.\n */\nexport function buildSystemPrompt(knowledge: Knowledge): string {\n return [\n \"You are an AI assistant on a business website. Use ONLY the knowledge below to answer.\",\n \"\",\n \"## Business knowledge\",\n knowledge.trim(),\n \"\",\n \"## Reply rules\",\n \"- Reply in 1-2 short sentences, conversational tone.\",\n \"- NEVER invent prices, availability, dispatch times, appointment confirmations, or facts not present in the business knowledge above.\",\n \"- For anything not covered in the knowledge above, say the owner will follow up — do NOT guess.\",\n '- If the caller is clearly a vendor/sales pitch, say: \"This does not look like a customer service request, so we will not continue this thread.\"',\n '- If wrong number or asked to stop, say: \"Sorry about that. We won\\'t text again.\"',\n \"- Match the caller's language automatically.\"\n ].join(\"\\n\");\n}\n","import type { GuardResult } from \"./types.js\";\n\n/**\n * Phrases that almost always indicate hallucination for SMB customer service:\n * inventing dispatch promises, fake confirmations, or appointment locks.\n */\nexport const FORBIDDEN_PHRASES: readonly string[] = [\n \"help is coming\",\n \"someone is on the way\",\n \"technician is on the way\",\n \"provider is on the way\",\n \"dispatching someone\",\n \"i've booked\",\n \"i have booked\",\n \"reservation confirmed\",\n \"your appointment is confirmed\",\n \"i've scheduled\",\n \"i have scheduled\",\n \"we've dispatched\",\n \"we have dispatched\",\n \"i can confirm\",\n \"i guarantee\",\n \"guaranteed delivery\",\n \"guaranteed arrival\",\n \"will arrive at\",\n \"arriving at\",\n \"i'll send\",\n \"i will send\"\n];\n\n/**\n * Check a reply against the built-in forbidden phrase list.\n * Returns ok=true when clean, ok=false with violations when not.\n */\nexport function checkForbiddenPhrases(reply: string): GuardResult {\n const lower = reply.toLowerCase();\n const violations: string[] = [];\n for (const phrase of FORBIDDEN_PHRASES) {\n if (lower.includes(phrase)) {\n violations.push(`Forbidden phrase: \"${phrase}\"`);\n }\n }\n return { ok: violations.length === 0, violations };\n}\n\n/**\n * Remove forbidden sentences from a reply (best-effort sentence drop).\n * If too much is removed, returns a safe fallback.\n */\nexport function stripForbidden(reply: string): string {\n const sentences = reply.split(/(?<=[.!?])\\s+/);\n const kept = sentences.filter((s) => {\n const lower = s.toLowerCase();\n return !FORBIDDEN_PHRASES.some((p) => lower.includes(p));\n });\n const trimmed = kept.join(\" \").trim();\n if (trimmed.length < 10) {\n return \"Thanks for reaching out — let me check with the owner and get back to you.\";\n }\n return trimmed;\n}\n","import type { Knowledge, Message } from \"../core/types.js\";\nimport { buildSystemPrompt } from \"../core/prompts.js\";\nimport { checkForbiddenPhrases, stripForbidden } from \"../core/guards.js\";\nimport type { Provider, ProviderConfig, ClientOptions, ChainStep, ChainEntry, AttemptInfo } from \"./types.js\";\nimport { isKnownProvider } from \"./types.js\";\nimport { PROVIDER_ENDPOINTS, isRetryableError } from \"./providers.js\";\n\nexport interface ChatBotInit {\n /** Markdown describing the business — services, hours, policies, anything. */\n knowledge: Knowledge;\n /** Provider keys + fallback chain. */\n providers: ProviderConfig;\n /** Optional runtime overrides. */\n options?: ClientOptions;\n}\n\nexport interface ReplyOptions {\n /** Conversation history (excluding the new user message). */\n history?: Message[];\n /** Override system prompt — advanced use only. */\n systemPrompt?: string;\n}\n\nexport interface ReplyResult {\n reply: string;\n /** Provider/model that produced the final reply (after fallback). */\n usedProvider: Provider;\n usedModel: string;\n /** Token usage if reported by the final provider. */\n usage?: { prompt_tokens?: number; completion_tokens?: number };\n /** Guard violations the bot caught and stripped, if any. */\n guardWarnings: string[];\n /** Debug trace of every attempt in the chain. */\n attempts: AttemptInfo[];\n}\n\n/**\n * The main ChatBot entry. Holds knowledge + provider chain.\n *\n * @example\n * const bot = new ChatBot({\n * knowledge: `# Acme Plumbing\\n## Services\\n- Sink leak: $95`,\n * providers: {\n * keys: { deepseek: \"sk-...\", openai: \"sk-...\" },\n * chain: [\n * { provider: \"deepseek\", model: \"deepseek-chat\" },\n * { provider: \"openai\", model: \"gpt-4o-mini\" }\n * ]\n * }\n * });\n * const { reply } = await bot.reply(\"My sink is leaking\");\n */\nexport class ChatBot {\n private readonly steps: ChainStep[];\n private readonly keys: Partial<Record<Provider, string>>;\n private readonly fetcher: typeof globalThis.fetch;\n private readonly timeoutMs: number;\n private readonly cachedSystemPrompt: string;\n\n constructor(init: ChatBotInit) {\n if (!init.knowledge || typeof init.knowledge !== \"string\" || init.knowledge.trim().length === 0) {\n throw new Error(\"chatbotlite: knowledge is required (a non-empty markdown string).\");\n }\n this.keys = init.providers.keys ?? {};\n this.steps = resolveChain(init.providers);\n this.fetcher = init.options?.fetch ?? globalThis.fetch.bind(globalThis);\n this.timeoutMs = init.options?.timeoutMs ?? 30_000;\n this.cachedSystemPrompt = buildSystemPrompt(init.knowledge);\n }\n\n async reply(message: string, opts: ReplyOptions = {}): Promise<ReplyResult> {\n const systemPrompt = opts.systemPrompt ?? this.cachedSystemPrompt;\n const messages: Message[] = [\n { role: \"system\", content: systemPrompt },\n ...(opts.history ?? []),\n { role: \"user\", content: message }\n ];\n const attempts: AttemptInfo[] = [];\n let lastError: unknown;\n for (const step of this.steps) {\n const t0 = Date.now();\n try {\n const result = await this.callProvider(step, messages);\n attempts.push({ provider: step.provider, model: step.model, status: \"ok\", latencyMs: Date.now() - t0 });\n const guard = checkForbiddenPhrases(result.reply);\n const finalReply = guard.ok ? result.reply : stripForbidden(result.reply);\n return {\n reply: finalReply,\n usedProvider: step.provider,\n usedModel: step.model,\n ...(result.usage ? { usage: result.usage } : {}),\n guardWarnings: guard.violations,\n attempts\n };\n } catch (err) {\n lastError = err;\n const errMsg = err instanceof Error ? err.message : String(err);\n attempts.push({\n provider: step.provider,\n model: step.model,\n status: \"error\",\n error: errMsg,\n latencyMs: Date.now() - t0\n });\n if (!isRetryableError(err)) {\n throw new Error(`chatbotlite: ${step.label} failed (non-retryable). ${errMsg}`);\n }\n }\n }\n const summary = attempts.map((a) => `${a.provider}/${a.model}:${a.error ?? \"ok\"}`).join(\" → \");\n throw new Error(`chatbotlite: all chain steps failed. Trace: ${summary}. Last error: ${lastError instanceof Error ? lastError.message : String(lastError)}`);\n }\n\n private async callProvider(step: ChainStep, messages: Message[]): Promise<{ reply: string; usage?: { prompt_tokens?: number; completion_tokens?: number } }> {\n const endpoint = PROVIDER_ENDPOINTS[step.provider];\n const key = this.keys[step.provider];\n if (!key) throw new Error(`Missing API key for provider: ${step.provider}`);\n\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), this.timeoutMs);\n try {\n const res = await this.fetcher(`${endpoint.baseUrl}/chat/completions`, {\n method: \"POST\",\n headers: {\n \"Authorization\": `Bearer ${key}`,\n \"Content-Type\": \"application/json\"\n },\n body: JSON.stringify({\n model: step.model,\n messages,\n temperature: 0.3,\n max_tokens: 300\n }),\n signal: controller.signal\n });\n if (!res.ok) {\n const body = await res.text();\n throw new Error(`${res.status}: ${body.slice(0, 200)}`);\n }\n const data = (await res.json()) as {\n choices?: Array<{ message?: { content?: string; reasoning_content?: string } }>;\n usage?: { prompt_tokens?: number; completion_tokens?: number };\n };\n const msg = data.choices?.[0]?.message;\n const reply = (msg?.content?.trim() || msg?.reasoning_content?.trim()) ?? \"\";\n if (!reply) throw new Error(\"empty reply from provider\");\n const result: { reply: string; usage?: { prompt_tokens?: number; completion_tokens?: number } } = { reply };\n if (data.usage) result.usage = data.usage;\n return result;\n } finally {\n clearTimeout(timer);\n }\n }\n}\n\nfunction resolveChain(providers: ProviderConfig): ChainStep[] {\n const keys = providers.keys ?? {};\n const explicit = providers.chain;\n if (explicit && explicit.length > 0) {\n return explicit.map((entry) => normalizeChainEntry(entry, keys));\n }\n const orderedProviders = Object.keys(keys).filter((k) => isKnownProvider(k) && keys[k as Provider]) as Provider[];\n if (orderedProviders.length === 0) {\n throw new Error(\"chatbotlite: at least one provider key is required.\");\n }\n return orderedProviders.map((provider) => ({\n provider,\n model: PROVIDER_ENDPOINTS[provider].defaultModel,\n label: `${provider}/${PROVIDER_ENDPOINTS[provider].defaultModel}`\n }));\n}\n\nfunction normalizeChainEntry(entry: ChainEntry, keys: Partial<Record<Provider, string>>): ChainStep {\n if (!isKnownProvider(entry.provider)) {\n throw new Error(`chatbotlite: unknown provider \"${entry.provider}\" in chain entry.`);\n }\n const provider = entry.provider;\n const model = entry.model ?? PROVIDER_ENDPOINTS[provider].defaultModel;\n if (!keys[provider]) {\n throw new Error(`chatbotlite: chain entry for \"${provider}\" needs a matching key in providers.keys.`);\n }\n return { provider, model, label: `${provider}/${model}` };\n}\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { P as Provider, c as ProviderConfig, b as ClientOptions, A as AttemptInfo } from '../types-J7BXpiRU.cjs';
|
|
2
|
+
export { C as ChainEntry, a as ChainStep, i as isKnownProvider } from '../types-J7BXpiRU.cjs';
|
|
3
|
+
import { K as Knowledge, M as Message } from '../types-4alyzg8O.cjs';
|
|
4
|
+
|
|
5
|
+
interface ProviderEndpoint {
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
defaultModel: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Built-in OpenAI-compatible providers. All use /v1/chat/completions
|
|
11
|
+
* with response in OpenAI format. Caller supplies API key per provider.
|
|
12
|
+
*/
|
|
13
|
+
declare const PROVIDER_ENDPOINTS: Record<Provider, ProviderEndpoint>;
|
|
14
|
+
declare function isRetryableError(err: unknown): boolean;
|
|
15
|
+
|
|
16
|
+
interface ChatBotInit {
|
|
17
|
+
/** Markdown describing the business — services, hours, policies, anything. */
|
|
18
|
+
knowledge: Knowledge;
|
|
19
|
+
/** Provider keys + fallback chain. */
|
|
20
|
+
providers: ProviderConfig;
|
|
21
|
+
/** Optional runtime overrides. */
|
|
22
|
+
options?: ClientOptions;
|
|
23
|
+
}
|
|
24
|
+
interface ReplyOptions {
|
|
25
|
+
/** Conversation history (excluding the new user message). */
|
|
26
|
+
history?: Message[];
|
|
27
|
+
/** Override system prompt — advanced use only. */
|
|
28
|
+
systemPrompt?: string;
|
|
29
|
+
}
|
|
30
|
+
interface ReplyResult {
|
|
31
|
+
reply: string;
|
|
32
|
+
/** Provider/model that produced the final reply (after fallback). */
|
|
33
|
+
usedProvider: Provider;
|
|
34
|
+
usedModel: string;
|
|
35
|
+
/** Token usage if reported by the final provider. */
|
|
36
|
+
usage?: {
|
|
37
|
+
prompt_tokens?: number;
|
|
38
|
+
completion_tokens?: number;
|
|
39
|
+
};
|
|
40
|
+
/** Guard violations the bot caught and stripped, if any. */
|
|
41
|
+
guardWarnings: string[];
|
|
42
|
+
/** Debug trace of every attempt in the chain. */
|
|
43
|
+
attempts: AttemptInfo[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The main ChatBot entry. Holds knowledge + provider chain.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* const bot = new ChatBot({
|
|
50
|
+
* knowledge: `# Acme Plumbing\n## Services\n- Sink leak: $95`,
|
|
51
|
+
* providers: {
|
|
52
|
+
* keys: { deepseek: "sk-...", openai: "sk-..." },
|
|
53
|
+
* chain: [
|
|
54
|
+
* { provider: "deepseek", model: "deepseek-chat" },
|
|
55
|
+
* { provider: "openai", model: "gpt-4o-mini" }
|
|
56
|
+
* ]
|
|
57
|
+
* }
|
|
58
|
+
* });
|
|
59
|
+
* const { reply } = await bot.reply("My sink is leaking");
|
|
60
|
+
*/
|
|
61
|
+
declare class ChatBot {
|
|
62
|
+
private readonly steps;
|
|
63
|
+
private readonly keys;
|
|
64
|
+
private readonly fetcher;
|
|
65
|
+
private readonly timeoutMs;
|
|
66
|
+
private readonly cachedSystemPrompt;
|
|
67
|
+
constructor(init: ChatBotInit);
|
|
68
|
+
reply(message: string, opts?: ReplyOptions): Promise<ReplyResult>;
|
|
69
|
+
private callProvider;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { AttemptInfo, ChatBot, type ChatBotInit, ClientOptions, PROVIDER_ENDPOINTS, Provider, ProviderConfig, type ProviderEndpoint, type ReplyOptions, type ReplyResult, isRetryableError };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { P as Provider, c as ProviderConfig, b as ClientOptions, A as AttemptInfo } from '../types-J7BXpiRU.js';
|
|
2
|
+
export { C as ChainEntry, a as ChainStep, i as isKnownProvider } from '../types-J7BXpiRU.js';
|
|
3
|
+
import { K as Knowledge, M as Message } from '../types-4alyzg8O.js';
|
|
4
|
+
|
|
5
|
+
interface ProviderEndpoint {
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
defaultModel: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Built-in OpenAI-compatible providers. All use /v1/chat/completions
|
|
11
|
+
* with response in OpenAI format. Caller supplies API key per provider.
|
|
12
|
+
*/
|
|
13
|
+
declare const PROVIDER_ENDPOINTS: Record<Provider, ProviderEndpoint>;
|
|
14
|
+
declare function isRetryableError(err: unknown): boolean;
|
|
15
|
+
|
|
16
|
+
interface ChatBotInit {
|
|
17
|
+
/** Markdown describing the business — services, hours, policies, anything. */
|
|
18
|
+
knowledge: Knowledge;
|
|
19
|
+
/** Provider keys + fallback chain. */
|
|
20
|
+
providers: ProviderConfig;
|
|
21
|
+
/** Optional runtime overrides. */
|
|
22
|
+
options?: ClientOptions;
|
|
23
|
+
}
|
|
24
|
+
interface ReplyOptions {
|
|
25
|
+
/** Conversation history (excluding the new user message). */
|
|
26
|
+
history?: Message[];
|
|
27
|
+
/** Override system prompt — advanced use only. */
|
|
28
|
+
systemPrompt?: string;
|
|
29
|
+
}
|
|
30
|
+
interface ReplyResult {
|
|
31
|
+
reply: string;
|
|
32
|
+
/** Provider/model that produced the final reply (after fallback). */
|
|
33
|
+
usedProvider: Provider;
|
|
34
|
+
usedModel: string;
|
|
35
|
+
/** Token usage if reported by the final provider. */
|
|
36
|
+
usage?: {
|
|
37
|
+
prompt_tokens?: number;
|
|
38
|
+
completion_tokens?: number;
|
|
39
|
+
};
|
|
40
|
+
/** Guard violations the bot caught and stripped, if any. */
|
|
41
|
+
guardWarnings: string[];
|
|
42
|
+
/** Debug trace of every attempt in the chain. */
|
|
43
|
+
attempts: AttemptInfo[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The main ChatBot entry. Holds knowledge + provider chain.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* const bot = new ChatBot({
|
|
50
|
+
* knowledge: `# Acme Plumbing\n## Services\n- Sink leak: $95`,
|
|
51
|
+
* providers: {
|
|
52
|
+
* keys: { deepseek: "sk-...", openai: "sk-..." },
|
|
53
|
+
* chain: [
|
|
54
|
+
* { provider: "deepseek", model: "deepseek-chat" },
|
|
55
|
+
* { provider: "openai", model: "gpt-4o-mini" }
|
|
56
|
+
* ]
|
|
57
|
+
* }
|
|
58
|
+
* });
|
|
59
|
+
* const { reply } = await bot.reply("My sink is leaking");
|
|
60
|
+
*/
|
|
61
|
+
declare class ChatBot {
|
|
62
|
+
private readonly steps;
|
|
63
|
+
private readonly keys;
|
|
64
|
+
private readonly fetcher;
|
|
65
|
+
private readonly timeoutMs;
|
|
66
|
+
private readonly cachedSystemPrompt;
|
|
67
|
+
constructor(init: ChatBotInit);
|
|
68
|
+
reply(message: string, opts?: ReplyOptions): Promise<ReplyResult>;
|
|
69
|
+
private callProvider;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { AttemptInfo, ChatBot, type ChatBotInit, ClientOptions, PROVIDER_ENDPOINTS, Provider, ProviderConfig, type ProviderEndpoint, type ReplyOptions, type ReplyResult, isRetryableError };
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// src/client/types.ts
|
|
2
|
+
var PROVIDER_NAMES = /* @__PURE__ */ new Set([
|
|
3
|
+
"openai",
|
|
4
|
+
"deepseek",
|
|
5
|
+
"groq",
|
|
6
|
+
"gemini",
|
|
7
|
+
"anthropic",
|
|
8
|
+
"cerebras",
|
|
9
|
+
"sambanova",
|
|
10
|
+
"fireworks",
|
|
11
|
+
"mistral",
|
|
12
|
+
"openrouter",
|
|
13
|
+
"moonshot"
|
|
14
|
+
]);
|
|
15
|
+
function isKnownProvider(name) {
|
|
16
|
+
return PROVIDER_NAMES.has(name);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/client/providers.ts
|
|
20
|
+
var PROVIDER_ENDPOINTS = {
|
|
21
|
+
openai: { baseUrl: "https://api.openai.com/v1", defaultModel: "gpt-4o-mini" },
|
|
22
|
+
deepseek: { baseUrl: "https://api.deepseek.com/v1", defaultModel: "deepseek-chat" },
|
|
23
|
+
groq: { baseUrl: "https://api.groq.com/openai/v1", defaultModel: "llama-3.3-70b-versatile" },
|
|
24
|
+
gemini: { baseUrl: "https://generativelanguage.googleapis.com/v1beta/openai", defaultModel: "gemini-2.5-flash" },
|
|
25
|
+
anthropic: { baseUrl: "https://api.anthropic.com/v1", defaultModel: "claude-haiku-4-5" },
|
|
26
|
+
cerebras: { baseUrl: "https://api.cerebras.ai/v1", defaultModel: "qwen-3-235b-a22b-instruct-2507" },
|
|
27
|
+
sambanova: { baseUrl: "https://api.sambanova.ai/v1", defaultModel: "Meta-Llama-3.3-70B-Instruct" },
|
|
28
|
+
fireworks: { baseUrl: "https://api.fireworks.ai/inference/v1", defaultModel: "accounts/fireworks/models/llama-v3p3-70b-instruct" },
|
|
29
|
+
mistral: { baseUrl: "https://api.mistral.ai/v1", defaultModel: "mistral-small-latest" },
|
|
30
|
+
openrouter: { baseUrl: "https://openrouter.ai/api/v1", defaultModel: "deepseek/deepseek-chat" },
|
|
31
|
+
moonshot: { baseUrl: "https://api.moonshot.ai/v1", defaultModel: "moonshot-v1-32k" }
|
|
32
|
+
};
|
|
33
|
+
function isRetryableError(err) {
|
|
34
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
35
|
+
return /\b(429|rate.?limit|quota|exceed|5\d\d|timeout|ECONNRESET|fetch failed)\b/i.test(msg);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/core/prompts.ts
|
|
39
|
+
function buildSystemPrompt(knowledge) {
|
|
40
|
+
return [
|
|
41
|
+
"You are an AI assistant on a business website. Use ONLY the knowledge below to answer.",
|
|
42
|
+
"",
|
|
43
|
+
"## Business knowledge",
|
|
44
|
+
knowledge.trim(),
|
|
45
|
+
"",
|
|
46
|
+
"## Reply rules",
|
|
47
|
+
"- Reply in 1-2 short sentences, conversational tone.",
|
|
48
|
+
"- NEVER invent prices, availability, dispatch times, appointment confirmations, or facts not present in the business knowledge above.",
|
|
49
|
+
"- For anything not covered in the knowledge above, say the owner will follow up \u2014 do NOT guess.",
|
|
50
|
+
'- If the caller is clearly a vendor/sales pitch, say: "This does not look like a customer service request, so we will not continue this thread."',
|
|
51
|
+
`- If wrong number or asked to stop, say: "Sorry about that. We won't text again."`,
|
|
52
|
+
"- Match the caller's language automatically."
|
|
53
|
+
].join("\n");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/core/guards.ts
|
|
57
|
+
var FORBIDDEN_PHRASES = [
|
|
58
|
+
"help is coming",
|
|
59
|
+
"someone is on the way",
|
|
60
|
+
"technician is on the way",
|
|
61
|
+
"provider is on the way",
|
|
62
|
+
"dispatching someone",
|
|
63
|
+
"i've booked",
|
|
64
|
+
"i have booked",
|
|
65
|
+
"reservation confirmed",
|
|
66
|
+
"your appointment is confirmed",
|
|
67
|
+
"i've scheduled",
|
|
68
|
+
"i have scheduled",
|
|
69
|
+
"we've dispatched",
|
|
70
|
+
"we have dispatched",
|
|
71
|
+
"i can confirm",
|
|
72
|
+
"i guarantee",
|
|
73
|
+
"guaranteed delivery",
|
|
74
|
+
"guaranteed arrival",
|
|
75
|
+
"will arrive at",
|
|
76
|
+
"arriving at",
|
|
77
|
+
"i'll send",
|
|
78
|
+
"i will send"
|
|
79
|
+
];
|
|
80
|
+
function checkForbiddenPhrases(reply) {
|
|
81
|
+
const lower = reply.toLowerCase();
|
|
82
|
+
const violations = [];
|
|
83
|
+
for (const phrase of FORBIDDEN_PHRASES) {
|
|
84
|
+
if (lower.includes(phrase)) {
|
|
85
|
+
violations.push(`Forbidden phrase: "${phrase}"`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return { ok: violations.length === 0, violations };
|
|
89
|
+
}
|
|
90
|
+
function stripForbidden(reply) {
|
|
91
|
+
const sentences = reply.split(/(?<=[.!?])\s+/);
|
|
92
|
+
const kept = sentences.filter((s) => {
|
|
93
|
+
const lower = s.toLowerCase();
|
|
94
|
+
return !FORBIDDEN_PHRASES.some((p) => lower.includes(p));
|
|
95
|
+
});
|
|
96
|
+
const trimmed = kept.join(" ").trim();
|
|
97
|
+
if (trimmed.length < 10) {
|
|
98
|
+
return "Thanks for reaching out \u2014 let me check with the owner and get back to you.";
|
|
99
|
+
}
|
|
100
|
+
return trimmed;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/client/chatbot.ts
|
|
104
|
+
var ChatBot = class {
|
|
105
|
+
steps;
|
|
106
|
+
keys;
|
|
107
|
+
fetcher;
|
|
108
|
+
timeoutMs;
|
|
109
|
+
cachedSystemPrompt;
|
|
110
|
+
constructor(init) {
|
|
111
|
+
if (!init.knowledge || typeof init.knowledge !== "string" || init.knowledge.trim().length === 0) {
|
|
112
|
+
throw new Error("chatbotlite: knowledge is required (a non-empty markdown string).");
|
|
113
|
+
}
|
|
114
|
+
this.keys = init.providers.keys ?? {};
|
|
115
|
+
this.steps = resolveChain(init.providers);
|
|
116
|
+
this.fetcher = init.options?.fetch ?? globalThis.fetch.bind(globalThis);
|
|
117
|
+
this.timeoutMs = init.options?.timeoutMs ?? 3e4;
|
|
118
|
+
this.cachedSystemPrompt = buildSystemPrompt(init.knowledge);
|
|
119
|
+
}
|
|
120
|
+
async reply(message, opts = {}) {
|
|
121
|
+
const systemPrompt = opts.systemPrompt ?? this.cachedSystemPrompt;
|
|
122
|
+
const messages = [
|
|
123
|
+
{ role: "system", content: systemPrompt },
|
|
124
|
+
...opts.history ?? [],
|
|
125
|
+
{ role: "user", content: message }
|
|
126
|
+
];
|
|
127
|
+
const attempts = [];
|
|
128
|
+
let lastError;
|
|
129
|
+
for (const step of this.steps) {
|
|
130
|
+
const t0 = Date.now();
|
|
131
|
+
try {
|
|
132
|
+
const result = await this.callProvider(step, messages);
|
|
133
|
+
attempts.push({ provider: step.provider, model: step.model, status: "ok", latencyMs: Date.now() - t0 });
|
|
134
|
+
const guard = checkForbiddenPhrases(result.reply);
|
|
135
|
+
const finalReply = guard.ok ? result.reply : stripForbidden(result.reply);
|
|
136
|
+
return {
|
|
137
|
+
reply: finalReply,
|
|
138
|
+
usedProvider: step.provider,
|
|
139
|
+
usedModel: step.model,
|
|
140
|
+
...result.usage ? { usage: result.usage } : {},
|
|
141
|
+
guardWarnings: guard.violations,
|
|
142
|
+
attempts
|
|
143
|
+
};
|
|
144
|
+
} catch (err) {
|
|
145
|
+
lastError = err;
|
|
146
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
147
|
+
attempts.push({
|
|
148
|
+
provider: step.provider,
|
|
149
|
+
model: step.model,
|
|
150
|
+
status: "error",
|
|
151
|
+
error: errMsg,
|
|
152
|
+
latencyMs: Date.now() - t0
|
|
153
|
+
});
|
|
154
|
+
if (!isRetryableError(err)) {
|
|
155
|
+
throw new Error(`chatbotlite: ${step.label} failed (non-retryable). ${errMsg}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const summary = attempts.map((a) => `${a.provider}/${a.model}:${a.error ?? "ok"}`).join(" \u2192 ");
|
|
160
|
+
throw new Error(`chatbotlite: all chain steps failed. Trace: ${summary}. Last error: ${lastError instanceof Error ? lastError.message : String(lastError)}`);
|
|
161
|
+
}
|
|
162
|
+
async callProvider(step, messages) {
|
|
163
|
+
const endpoint = PROVIDER_ENDPOINTS[step.provider];
|
|
164
|
+
const key = this.keys[step.provider];
|
|
165
|
+
if (!key) throw new Error(`Missing API key for provider: ${step.provider}`);
|
|
166
|
+
const controller = new AbortController();
|
|
167
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
168
|
+
try {
|
|
169
|
+
const res = await this.fetcher(`${endpoint.baseUrl}/chat/completions`, {
|
|
170
|
+
method: "POST",
|
|
171
|
+
headers: {
|
|
172
|
+
"Authorization": `Bearer ${key}`,
|
|
173
|
+
"Content-Type": "application/json"
|
|
174
|
+
},
|
|
175
|
+
body: JSON.stringify({
|
|
176
|
+
model: step.model,
|
|
177
|
+
messages,
|
|
178
|
+
temperature: 0.3,
|
|
179
|
+
max_tokens: 300
|
|
180
|
+
}),
|
|
181
|
+
signal: controller.signal
|
|
182
|
+
});
|
|
183
|
+
if (!res.ok) {
|
|
184
|
+
const body = await res.text();
|
|
185
|
+
throw new Error(`${res.status}: ${body.slice(0, 200)}`);
|
|
186
|
+
}
|
|
187
|
+
const data = await res.json();
|
|
188
|
+
const msg = data.choices?.[0]?.message;
|
|
189
|
+
const reply = (msg?.content?.trim() || msg?.reasoning_content?.trim()) ?? "";
|
|
190
|
+
if (!reply) throw new Error("empty reply from provider");
|
|
191
|
+
const result = { reply };
|
|
192
|
+
if (data.usage) result.usage = data.usage;
|
|
193
|
+
return result;
|
|
194
|
+
} finally {
|
|
195
|
+
clearTimeout(timer);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
function resolveChain(providers) {
|
|
200
|
+
const keys = providers.keys ?? {};
|
|
201
|
+
const explicit = providers.chain;
|
|
202
|
+
if (explicit && explicit.length > 0) {
|
|
203
|
+
return explicit.map((entry) => normalizeChainEntry(entry, keys));
|
|
204
|
+
}
|
|
205
|
+
const orderedProviders = Object.keys(keys).filter((k) => isKnownProvider(k) && keys[k]);
|
|
206
|
+
if (orderedProviders.length === 0) {
|
|
207
|
+
throw new Error("chatbotlite: at least one provider key is required.");
|
|
208
|
+
}
|
|
209
|
+
return orderedProviders.map((provider) => ({
|
|
210
|
+
provider,
|
|
211
|
+
model: PROVIDER_ENDPOINTS[provider].defaultModel,
|
|
212
|
+
label: `${provider}/${PROVIDER_ENDPOINTS[provider].defaultModel}`
|
|
213
|
+
}));
|
|
214
|
+
}
|
|
215
|
+
function normalizeChainEntry(entry, keys) {
|
|
216
|
+
if (!isKnownProvider(entry.provider)) {
|
|
217
|
+
throw new Error(`chatbotlite: unknown provider "${entry.provider}" in chain entry.`);
|
|
218
|
+
}
|
|
219
|
+
const provider = entry.provider;
|
|
220
|
+
const model = entry.model ?? PROVIDER_ENDPOINTS[provider].defaultModel;
|
|
221
|
+
if (!keys[provider]) {
|
|
222
|
+
throw new Error(`chatbotlite: chain entry for "${provider}" needs a matching key in providers.keys.`);
|
|
223
|
+
}
|
|
224
|
+
return { provider, model, label: `${provider}/${model}` };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export { ChatBot, PROVIDER_ENDPOINTS, isKnownProvider, isRetryableError };
|
|
228
|
+
//# sourceMappingURL=index.js.map
|
|
229
|
+
//# sourceMappingURL=index.js.map
|