botversion-sdk 1.0.3 → 1.0.5
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/bin/init.js +184 -135
- package/cli/detector.js +360 -173
- package/cli/generator.js +18 -476
- package/cli/prompts.js +40 -42
- package/cli/writer.js +118 -42
- package/client.js +214 -36
- package/index.js +74 -391
- package/interceptor.js +112 -124
- package/package.json +3 -2
- package/scanner.js +792 -27
package/index.js
CHANGED
|
@@ -10,15 +10,6 @@ var BotVersion = {
|
|
|
10
10
|
_initialized: false,
|
|
11
11
|
|
|
12
12
|
init: function (appOrOptions, optionsArg) {
|
|
13
|
-
console.log("=== INIT CALLED ===");
|
|
14
|
-
console.log("appOrOptions type:", typeof appOrOptions);
|
|
15
|
-
console.log(
|
|
16
|
-
"appOrOptions is express app:",
|
|
17
|
-
appOrOptions && typeof appOrOptions.use === "function",
|
|
18
|
-
);
|
|
19
|
-
console.log("optionsArg type:", typeof optionsArg);
|
|
20
|
-
console.log("optionsArg value:", JSON.stringify(optionsArg));
|
|
21
|
-
console.log("===================");
|
|
22
13
|
var app = null;
|
|
23
14
|
var options = {};
|
|
24
15
|
|
|
@@ -35,7 +26,6 @@ var BotVersion = {
|
|
|
35
26
|
app = null;
|
|
36
27
|
}
|
|
37
28
|
|
|
38
|
-
console.log("options received:", JSON.stringify(options));
|
|
39
29
|
if (!options.apiKey) {
|
|
40
30
|
console.error("[BotVersion SDK] ❌ apiKey is required.");
|
|
41
31
|
return;
|
|
@@ -61,7 +51,7 @@ var BotVersion = {
|
|
|
61
51
|
|
|
62
52
|
this._client = new BotVersionClient({
|
|
63
53
|
apiKey: options.apiKey,
|
|
64
|
-
platformUrl: options.platformUrl || "http://localhost:3000
|
|
54
|
+
platformUrl: options.platformUrl || "http://localhost:3000",
|
|
65
55
|
debug: options.debug || false,
|
|
66
56
|
timeout: options.timeout || 30000,
|
|
67
57
|
});
|
|
@@ -92,9 +82,6 @@ var BotVersion = {
|
|
|
92
82
|
console.error(
|
|
93
83
|
"[BotVersion SDK] ❌ Currently supports Express and Next.js only.",
|
|
94
84
|
);
|
|
95
|
-
console.error(
|
|
96
|
-
"[BotVersion SDK] ❌ Visit https://docs.botversion.com for supported frameworks.",
|
|
97
|
-
);
|
|
98
85
|
return;
|
|
99
86
|
}
|
|
100
87
|
if (debug) {
|
|
@@ -111,7 +98,6 @@ var BotVersion = {
|
|
|
111
98
|
exclude: options.exclude || [],
|
|
112
99
|
apiPrefix: options.apiPrefix || null,
|
|
113
100
|
debug: debug,
|
|
114
|
-
getUserContext: options.getUserContext || null,
|
|
115
101
|
});
|
|
116
102
|
if (debug) {
|
|
117
103
|
console.log("[BotVersion SDK] ✅ Runtime interceptor attached");
|
|
@@ -128,68 +114,78 @@ var BotVersion = {
|
|
|
128
114
|
setTimeout(function () {
|
|
129
115
|
var endpoints = [];
|
|
130
116
|
|
|
117
|
+
// Express scan
|
|
131
118
|
// Express scan
|
|
132
119
|
if (app) {
|
|
133
120
|
console.log("[BotVersion SDK] Scanning Express routes...");
|
|
134
|
-
|
|
121
|
+
var cwd = options.cwd || process.cwd();
|
|
122
|
+
endpoints = scanner.scanExpressRoutes(app, cwd);
|
|
135
123
|
console.log(
|
|
136
124
|
"[BotVersion SDK] Found",
|
|
137
125
|
endpoints.length,
|
|
138
126
|
"Express routes",
|
|
139
127
|
);
|
|
140
|
-
console.log(
|
|
141
|
-
"[BotVersion SDK] Endpoints:",
|
|
142
|
-
JSON.stringify(endpoints, null, 2),
|
|
143
|
-
);
|
|
144
128
|
|
|
145
129
|
if (endpoints.length === 0) {
|
|
146
130
|
console.warn("[BotVersion SDK] ⚠ No endpoints found.");
|
|
147
131
|
console.warn(
|
|
148
132
|
"[BotVersion SDK] ⚠ Make sure routes are defined BEFORE BotVersion.init()",
|
|
149
133
|
);
|
|
150
|
-
console.warn(
|
|
151
|
-
"[BotVersion SDK] ⚠ and BotVersion.init() is called BEFORE app.listen().",
|
|
152
|
-
);
|
|
153
134
|
}
|
|
154
135
|
}
|
|
155
136
|
|
|
156
|
-
// Next.js scan
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
);
|
|
162
|
-
var nextRoutes = scanner.scanNextJsRoutes(options.pagesDir);
|
|
163
|
-
endpoints = endpoints.concat(nextRoutes);
|
|
164
|
-
console.log(
|
|
165
|
-
"[BotVersion SDK] Found",
|
|
166
|
-
nextRoutes.length,
|
|
167
|
-
"Next.js routes",
|
|
168
|
-
);
|
|
169
|
-
console.log(
|
|
170
|
-
"[BotVersion SDK] Next.js routes:",
|
|
171
|
-
JSON.stringify(nextRoutes, null, 2),
|
|
172
|
-
);
|
|
137
|
+
// Next.js scan
|
|
138
|
+
// Next.js scan — auto detect all possible structures
|
|
139
|
+
if (!app) {
|
|
140
|
+
const fs = require("fs");
|
|
141
|
+
const path = require("path");
|
|
142
|
+
const cwd = process.cwd();
|
|
173
143
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
)
|
|
144
|
+
const possibleAppDirs = [
|
|
145
|
+
path.join(cwd, "app"),
|
|
146
|
+
path.join(cwd, "src", "app"),
|
|
147
|
+
];
|
|
148
|
+
|
|
149
|
+
const possiblePagesDirs = [
|
|
150
|
+
path.join(cwd, "pages"),
|
|
151
|
+
path.join(cwd, "src", "pages"),
|
|
152
|
+
];
|
|
153
|
+
|
|
154
|
+
for (const dir of possibleAppDirs) {
|
|
155
|
+
if (fs.existsSync(dir)) {
|
|
156
|
+
console.log("[BotVersion SDK] Scanning App Router routes at:", dir);
|
|
157
|
+
const routes = scanner.scanNextJsAppRoutes(dir);
|
|
158
|
+
endpoints = endpoints.concat(routes);
|
|
159
|
+
console.log(
|
|
160
|
+
"[BotVersion SDK] Found",
|
|
161
|
+
routes.length,
|
|
162
|
+
"App Router routes",
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
for (const dir of possiblePagesDirs) {
|
|
168
|
+
if (fs.existsSync(dir)) {
|
|
169
|
+
console.log(
|
|
170
|
+
"[BotVersion SDK] Scanning Pages Router routes at:",
|
|
171
|
+
dir,
|
|
172
|
+
);
|
|
173
|
+
const routes = scanner.scanNextJsRoutes(dir);
|
|
174
|
+
endpoints = endpoints.concat(routes);
|
|
175
|
+
console.log(
|
|
176
|
+
"[BotVersion SDK] Found",
|
|
177
|
+
routes.length,
|
|
178
|
+
"Pages Router routes",
|
|
179
|
+
);
|
|
180
|
+
}
|
|
182
181
|
}
|
|
183
182
|
}
|
|
184
183
|
|
|
185
184
|
// Neither Express nor pagesDir
|
|
186
|
-
|
|
187
|
-
|
|
185
|
+
// Neither Express nor Next.js routes found
|
|
186
|
+
if (!app && endpoints.length === 0) {
|
|
188
187
|
console.error(
|
|
189
|
-
"[BotVersion SDK] ❌
|
|
190
|
-
);
|
|
191
|
-
console.error(
|
|
192
|
-
"[BotVersion SDK] ❌ For Next.js: pass pagesDir — BotVersion.init({ apiKey, pagesDir: path.join(process.cwd(), 'pages') })",
|
|
188
|
+
"[BotVersion SDK] ❌ No routes found. Make sure your app/api or pages/api folder exists.",
|
|
193
189
|
);
|
|
194
190
|
return;
|
|
195
191
|
}
|
|
@@ -218,6 +214,29 @@ var BotVersion = {
|
|
|
218
214
|
});
|
|
219
215
|
}
|
|
220
216
|
|
|
217
|
+
var routePatterns = scanner.scanFrontendRoutes(
|
|
218
|
+
options.cwd || process.cwd(),
|
|
219
|
+
);
|
|
220
|
+
if (routePatterns.length > 0) {
|
|
221
|
+
console.log(
|
|
222
|
+
"[BotVersion SDK] Found",
|
|
223
|
+
routePatterns.length,
|
|
224
|
+
"frontend route patterns",
|
|
225
|
+
);
|
|
226
|
+
self._client
|
|
227
|
+
.registerRoutePatterns(routePatterns)
|
|
228
|
+
.then(function () {
|
|
229
|
+
console.log("[BotVersion SDK] ✅ Route patterns registered");
|
|
230
|
+
})
|
|
231
|
+
.catch(function (err) {
|
|
232
|
+
console.error(
|
|
233
|
+
"[BotVersion SDK] ❌ Failed to register route patterns:",
|
|
234
|
+
err.message,
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
self._client.connect();
|
|
221
240
|
console.log("[BotVersion SDK] ✅ Initialization complete");
|
|
222
241
|
}, 500);
|
|
223
242
|
},
|
|
@@ -239,354 +258,18 @@ var BotVersion = {
|
|
|
239
258
|
}
|
|
240
259
|
return this._client.registerEndpoints([endpoint]);
|
|
241
260
|
},
|
|
242
|
-
|
|
243
|
-
chat: function (req, res) {
|
|
244
|
-
if (!this._client) {
|
|
245
|
-
return res.status(500).json({ error: "BotVersion SDK not initialized." });
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
var getUserContext = this._options && this._options.getUserContext;
|
|
249
|
-
var userContext = getUserContext
|
|
250
|
-
? getUserContext(req)
|
|
251
|
-
: extractDefaultContext(req);
|
|
252
|
-
|
|
253
|
-
this._client
|
|
254
|
-
.agentChat({
|
|
255
|
-
message: req.body.message,
|
|
256
|
-
conversationHistory: req.body.conversationHistory || [],
|
|
257
|
-
pageContext: req.body.pageContext || {},
|
|
258
|
-
userContext: userContext,
|
|
259
|
-
workspaceKey: this._options.apiKey,
|
|
260
|
-
chatbotId: req.body.chatbotId,
|
|
261
|
-
})
|
|
262
|
-
.then(function (response) {
|
|
263
|
-
return res.status(200).json(response);
|
|
264
|
-
})
|
|
265
|
-
.catch(function (err) {
|
|
266
|
-
console.error("[BotVersion SDK] chat error:", err);
|
|
267
|
-
return res.status(500).json({ error: "Agent error" });
|
|
268
|
-
});
|
|
269
|
-
},
|
|
270
261
|
};
|
|
271
262
|
|
|
272
|
-
|
|
273
|
-
var user = req.user || (req.session && req.session.user) || {};
|
|
274
|
-
var sensitiveKeys = [
|
|
275
|
-
"password",
|
|
276
|
-
"passwd",
|
|
277
|
-
"pwd",
|
|
278
|
-
"token",
|
|
279
|
-
"accesstoken",
|
|
280
|
-
"refreshtoken",
|
|
281
|
-
"bearertoken",
|
|
282
|
-
"secret",
|
|
283
|
-
"privatesecret",
|
|
284
|
-
"apikey",
|
|
285
|
-
"api_key",
|
|
286
|
-
"privatekey",
|
|
287
|
-
"private_key",
|
|
288
|
-
"signingkey",
|
|
289
|
-
"hash",
|
|
290
|
-
"passwordhash",
|
|
291
|
-
"salt",
|
|
292
|
-
"cvv",
|
|
293
|
-
"ssn",
|
|
294
|
-
"pin",
|
|
295
|
-
"creditcard",
|
|
296
|
-
"credit_card",
|
|
297
|
-
"cardnumber",
|
|
298
|
-
"card_number",
|
|
299
|
-
"otp",
|
|
300
|
-
"mfa",
|
|
301
|
-
"totp",
|
|
302
|
-
"image",
|
|
303
|
-
"avatar",
|
|
304
|
-
"photo",
|
|
305
|
-
];
|
|
306
|
-
|
|
307
|
-
// Step 1: Flatten nested objects
|
|
308
|
-
function flattenObject(obj, prefix) {
|
|
309
|
-
prefix = prefix || "";
|
|
310
|
-
var result = {};
|
|
311
|
-
Object.keys(obj).forEach(function (key) {
|
|
312
|
-
var value = obj[key];
|
|
313
|
-
var fullKey = prefix ? prefix + "_" + key : key;
|
|
314
|
-
if (value === null || value === undefined) return;
|
|
315
|
-
if (typeof value === "object" && !Array.isArray(value)) {
|
|
316
|
-
var nested = flattenObject(value, fullKey);
|
|
317
|
-
Object.keys(nested).forEach(function (k) {
|
|
318
|
-
result[k] = nested[k];
|
|
319
|
-
});
|
|
320
|
-
} else if (typeof value !== "object") {
|
|
321
|
-
result[fullKey] = value;
|
|
322
|
-
}
|
|
323
|
-
});
|
|
324
|
-
return result;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
var flatUser = flattenObject(user);
|
|
328
|
-
|
|
329
|
-
// Step 2: Strip sensitive keys
|
|
330
|
-
var context = {};
|
|
331
|
-
Object.keys(flatUser).forEach(function (key) {
|
|
332
|
-
var isSensitive = sensitiveKeys.some(function (sk) {
|
|
333
|
-
return key.toLowerCase().includes(sk);
|
|
334
|
-
});
|
|
335
|
-
if (!isSensitive) {
|
|
336
|
-
context[key] = flatUser[key];
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
// Step 3: Smart aliasing
|
|
341
|
-
var idSuffixes = ["id", "key", "code", "ref", "slug", "uuid", "num", "no"];
|
|
342
|
-
var cleanPrefixes = [
|
|
343
|
-
"active",
|
|
344
|
-
"current",
|
|
345
|
-
"selected",
|
|
346
|
-
"default",
|
|
347
|
-
"my",
|
|
348
|
-
"the",
|
|
349
|
-
"this",
|
|
350
|
-
];
|
|
351
|
-
|
|
352
|
-
Object.keys(context).forEach(function (key) {
|
|
353
|
-
var lowerKey = key.toLowerCase();
|
|
354
|
-
var isIdField = idSuffixes.some(function (suffix) {
|
|
355
|
-
return lowerKey.endsWith(suffix);
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
if (isIdField && context[key]) {
|
|
359
|
-
var cleanKey = key;
|
|
360
|
-
cleanPrefixes.forEach(function (prefix) {
|
|
361
|
-
var regex = new RegExp("^" + prefix, "i");
|
|
362
|
-
if (regex.test(cleanKey)) {
|
|
363
|
-
cleanKey = cleanKey.replace(regex, "");
|
|
364
|
-
cleanKey = cleanKey.charAt(0).toLowerCase() + cleanKey.slice(1);
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
if (cleanKey !== key && !context[cleanKey]) {
|
|
369
|
-
context[cleanKey] = context[key];
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
return context;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// Named exports for both CJS and ESM
|
|
263
|
+
// Named exports
|
|
378
264
|
BotVersion.init = BotVersion.init.bind(BotVersion);
|
|
379
265
|
BotVersion.getEndpoints = BotVersion.getEndpoints.bind(BotVersion);
|
|
380
266
|
BotVersion.registerEndpoint = BotVersion.registerEndpoint.bind(BotVersion);
|
|
381
|
-
BotVersion.chat = BotVersion.chat.bind(BotVersion);
|
|
382
|
-
|
|
383
|
-
BotVersion.nextHandler = function (options) {
|
|
384
|
-
options = options || {};
|
|
385
|
-
var self = this;
|
|
386
|
-
var hasScanned = false;
|
|
387
|
-
|
|
388
|
-
return async function handler(req, res) {
|
|
389
|
-
// ── Initialize on first request if not already done ──
|
|
390
|
-
if (!self._initialized || !self._client) {
|
|
391
|
-
var apiKey = options.apiKey;
|
|
392
|
-
if (!apiKey) {
|
|
393
|
-
console.error(
|
|
394
|
-
"[BotVersion SDK] ❌ apiKey is required in nextHandler options.",
|
|
395
|
-
);
|
|
396
|
-
return res
|
|
397
|
-
.status(500)
|
|
398
|
-
.json({ error: "BotVersion SDK not configured." });
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
self._client = new BotVersionClient({
|
|
402
|
-
apiKey: apiKey,
|
|
403
|
-
platformUrl: options.platformUrl || "http://localhost:3000",
|
|
404
|
-
debug: options.debug || false,
|
|
405
|
-
timeout: options.timeout || 30000,
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
global._botVersionClient = self._client;
|
|
409
|
-
global._botVersionOptions = options;
|
|
410
|
-
self._initialized = true;
|
|
411
|
-
self._options = options;
|
|
412
|
-
|
|
413
|
-
console.log("[BotVersion SDK] ✅ Initialized from nextHandler");
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// Restore from global if hot reload lost the instance
|
|
417
|
-
if (!self._client && global._botVersionClient) {
|
|
418
|
-
self._client = global._botVersionClient;
|
|
419
|
-
self._options = global._botVersionOptions;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
// ── Handle the chat request ───────────────────────────────────────────
|
|
423
|
-
var session = null;
|
|
424
|
-
if (options.getSession) {
|
|
425
|
-
session = await options.getSession(req, res);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
req.user = (session && session.user) || {};
|
|
429
|
-
|
|
430
|
-
var getUserContext =
|
|
431
|
-
(self._options && self._options.getUserContext) ||
|
|
432
|
-
function (req) {
|
|
433
|
-
var user = req.user || {};
|
|
434
|
-
var sensitiveKeys = [
|
|
435
|
-
"password",
|
|
436
|
-
"passwd",
|
|
437
|
-
"pwd",
|
|
438
|
-
"token",
|
|
439
|
-
"accesstoken",
|
|
440
|
-
"refreshtoken",
|
|
441
|
-
"bearertoken",
|
|
442
|
-
"secret",
|
|
443
|
-
"privatesecret",
|
|
444
|
-
"apikey",
|
|
445
|
-
"api_key",
|
|
446
|
-
"privatekey",
|
|
447
|
-
"private_key",
|
|
448
|
-
"signingkey",
|
|
449
|
-
"hash",
|
|
450
|
-
"passwordhash",
|
|
451
|
-
"salt",
|
|
452
|
-
"cvv",
|
|
453
|
-
"ssn",
|
|
454
|
-
"pin",
|
|
455
|
-
"creditcard",
|
|
456
|
-
"credit_card",
|
|
457
|
-
"cardnumber",
|
|
458
|
-
"card_number",
|
|
459
|
-
"otp",
|
|
460
|
-
"mfa",
|
|
461
|
-
"totp",
|
|
462
|
-
"image",
|
|
463
|
-
"avatar",
|
|
464
|
-
"photo",
|
|
465
|
-
];
|
|
466
|
-
|
|
467
|
-
// Step 1: Flatten nested objects
|
|
468
|
-
function flattenObject(obj, prefix) {
|
|
469
|
-
prefix = prefix || "";
|
|
470
|
-
var result = {};
|
|
471
|
-
Object.keys(obj).forEach(function (key) {
|
|
472
|
-
var value = obj[key];
|
|
473
|
-
var fullKey = prefix ? prefix + "_" + key : key;
|
|
474
|
-
if (value === null || value === undefined) return;
|
|
475
|
-
if (typeof value === "object" && !Array.isArray(value)) {
|
|
476
|
-
var nested = flattenObject(value, fullKey);
|
|
477
|
-
Object.keys(nested).forEach(function (k) {
|
|
478
|
-
result[k] = nested[k];
|
|
479
|
-
});
|
|
480
|
-
} else if (typeof value !== "object") {
|
|
481
|
-
result[fullKey] = value;
|
|
482
|
-
}
|
|
483
|
-
});
|
|
484
|
-
return result;
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
var flatUser = flattenObject(user);
|
|
488
|
-
|
|
489
|
-
// Step 2: Strip sensitive keys
|
|
490
|
-
var context = {};
|
|
491
|
-
Object.keys(flatUser).forEach(function (key) {
|
|
492
|
-
var isSensitive = sensitiveKeys.some(function (sk) {
|
|
493
|
-
return key.toLowerCase().includes(sk);
|
|
494
|
-
});
|
|
495
|
-
if (!isSensitive) {
|
|
496
|
-
context[key] = flatUser[key];
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
// Step 3: Smart aliasing
|
|
501
|
-
var idSuffixes = [
|
|
502
|
-
"id",
|
|
503
|
-
"key",
|
|
504
|
-
"code",
|
|
505
|
-
"ref",
|
|
506
|
-
"slug",
|
|
507
|
-
"uuid",
|
|
508
|
-
"num",
|
|
509
|
-
"no",
|
|
510
|
-
];
|
|
511
|
-
var cleanPrefixes = [
|
|
512
|
-
"active",
|
|
513
|
-
"current",
|
|
514
|
-
"selected",
|
|
515
|
-
"default",
|
|
516
|
-
"my",
|
|
517
|
-
"the",
|
|
518
|
-
"this",
|
|
519
|
-
];
|
|
520
|
-
|
|
521
|
-
Object.keys(context).forEach(function (key) {
|
|
522
|
-
var lowerKey = key.toLowerCase();
|
|
523
|
-
var isIdField = idSuffixes.some(function (suffix) {
|
|
524
|
-
return lowerKey.endsWith(suffix);
|
|
525
|
-
});
|
|
526
|
-
|
|
527
|
-
if (isIdField && context[key]) {
|
|
528
|
-
var cleanKey = key;
|
|
529
|
-
cleanPrefixes.forEach(function (prefix) {
|
|
530
|
-
var regex = new RegExp("^" + prefix, "i");
|
|
531
|
-
if (regex.test(cleanKey)) {
|
|
532
|
-
cleanKey = cleanKey.replace(regex, "");
|
|
533
|
-
cleanKey = cleanKey.charAt(0).toLowerCase() + cleanKey.slice(1);
|
|
534
|
-
}
|
|
535
|
-
});
|
|
536
|
-
|
|
537
|
-
if (cleanKey !== key && !context[cleanKey]) {
|
|
538
|
-
context[cleanKey] = context[key];
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
});
|
|
542
|
-
|
|
543
|
-
console.log("[BotVersion] userContext being sent:", context);
|
|
544
|
-
return context;
|
|
545
|
-
};
|
|
546
|
-
|
|
547
|
-
return new Promise(function (resolve) {
|
|
548
|
-
console.log("[BotVersion] req.body:", JSON.stringify(req.body));
|
|
549
|
-
console.log("[BotVersion] chatbotId from body:", req.body.chatbotId);
|
|
550
|
-
self._client
|
|
551
|
-
.agentChat({
|
|
552
|
-
message: req.body.message,
|
|
553
|
-
conversationHistory: req.body.conversationHistory || [],
|
|
554
|
-
pageContext: req.body.pageContext || {},
|
|
555
|
-
userContext: getUserContext(req),
|
|
556
|
-
chatbotId: req.body.chatbotId,
|
|
557
|
-
publicKey: req.body.publicKey,
|
|
558
|
-
})
|
|
559
|
-
.then(function (response) {
|
|
560
|
-
res.status(200).json(response);
|
|
561
|
-
resolve();
|
|
562
|
-
})
|
|
563
|
-
.catch(function (err) {
|
|
564
|
-
console.error("[BotVersion SDK] chat error:", err);
|
|
565
|
-
res.status(500).json({ error: "Agent error" });
|
|
566
|
-
resolve();
|
|
567
|
-
});
|
|
568
|
-
});
|
|
569
|
-
};
|
|
570
|
-
};
|
|
571
|
-
|
|
572
|
-
BotVersion.nextHandler = BotVersion.nextHandler.bind(BotVersion);
|
|
573
|
-
|
|
574
|
-
BotVersion.appRouterHandler = function () {
|
|
575
|
-
throw new Error(
|
|
576
|
-
"[BotVersion SDK] appRouterHandler is not supported. " +
|
|
577
|
-
"Please run: npx botversion-sdk init --key YOUR_KEY --force " +
|
|
578
|
-
"to regenerate the correct route file.",
|
|
579
|
-
);
|
|
580
|
-
};
|
|
581
267
|
|
|
582
268
|
module.exports = BotVersion;
|
|
583
269
|
module.exports.default = BotVersion;
|
|
584
270
|
module.exports.init = BotVersion.init;
|
|
585
271
|
module.exports.getEndpoints = BotVersion.getEndpoints;
|
|
586
272
|
module.exports.registerEndpoint = BotVersion.registerEndpoint;
|
|
587
|
-
module.exports.chat = BotVersion.chat;
|
|
588
|
-
module.exports.nextHandler = BotVersion.nextHandler;
|
|
589
|
-
module.exports.appRouterHandler = BotVersion.appRouterHandler;
|
|
590
273
|
|
|
591
274
|
// ── Framework detection ──────────────────────────────────────────────────────
|
|
592
275
|
function detectFramework(app) {
|