@astermind/cybernetic-chatbot-client 2.2.74 → 2.3.4

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.
@@ -216,6 +216,89 @@
216
216
  }
217
217
  }
218
218
 
219
+ // src/SalesApiClient.ts
220
+ // API client for Sales Agent external endpoints (/api/external/sales/*)
221
+ /**
222
+ * Client for the Sales Agent external API endpoints.
223
+ * Handles visitor tracking, sales-aware chat, and lead capture.
224
+ */
225
+ class SalesApiClient {
226
+ constructor(apiUrl, apiKey) {
227
+ // Normalize base URL (strip trailing slash)
228
+ this.baseUrl = apiUrl.replace(/\/$/, '');
229
+ this.apiKey = apiKey;
230
+ }
231
+ /**
232
+ * Initialize or resume a visitor session.
233
+ * Call on first page load to register the visitor with the backend.
234
+ */
235
+ async initVisitor(visitorId, pageUrl, referrer, utmParams) {
236
+ return this.post('visitor/init', {
237
+ visitorId,
238
+ pageUrl,
239
+ referrer,
240
+ utmParams,
241
+ });
242
+ }
243
+ /**
244
+ * Track a page view event.
245
+ * Call when the visitor navigates to a new page.
246
+ */
247
+ async trackPageView(visitorId, url, title, timeOnPageMs, scrollDepth) {
248
+ return this.post('visitor/pageview', {
249
+ visitorId,
250
+ url,
251
+ title,
252
+ timeOnPageMs,
253
+ scrollDepth,
254
+ });
255
+ }
256
+ /**
257
+ * Associate a visitor with identity data (email, name, etc.).
258
+ * Call when the visitor provides identifying information.
259
+ */
260
+ async identifyVisitor(visitorId, data) {
261
+ return this.post('visitor/identify', {
262
+ visitorId,
263
+ ...data,
264
+ });
265
+ }
266
+ /**
267
+ * Send a sales-aware chat message.
268
+ * Returns the assistant reply along with sales context (lead score, CTA buttons, etc.).
269
+ */
270
+ async salesChat(message, options = {}) {
271
+ return this.post('chat', {
272
+ message,
273
+ ...options,
274
+ });
275
+ }
276
+ /**
277
+ * Submit a lead capture form.
278
+ * Creates or updates a lead record in the backend.
279
+ */
280
+ async captureLeadForm(data) {
281
+ return this.post('lead/capture', data);
282
+ }
283
+ // ─── Private ─────────────────────────────────────────────────────────────────
284
+ async post(path, body) {
285
+ const url = `${this.baseUrl}/api/external/sales/${path}`;
286
+ const response = await fetch(url, {
287
+ method: 'POST',
288
+ headers: {
289
+ 'Content-Type': 'application/json',
290
+ 'X-Api-Key': this.apiKey,
291
+ },
292
+ body: JSON.stringify(body),
293
+ });
294
+ if (!response.ok) {
295
+ const err = await response.json().catch(() => ({ error: 'Unknown error' }));
296
+ throw new Error(err.error || `Sales API error: ${response.status}`);
297
+ }
298
+ return response.json();
299
+ }
300
+ }
301
+
219
302
  const instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);
220
303
 
221
304
  let idbProxyableTypes;
@@ -2177,8 +2260,10 @@ LJ5AZXvOhHaXdHzMuYKX5BpK4w7TqbPvJ6QPvKmLKvHh1VKcUJ6mJQgJJw==
2177
2260
  /**
2178
2261
  * License enforcement message shown in production when license is invalid
2179
2262
  */
2180
- const LICENSE_WARNING_MESSAGE = '\n\n---\n⚠️ License Notice: Your AsterMind license key needs to be updated. ' +
2181
- 'Please contact support@astermind.ai or visit https://astermind.ai/license to renew your license.';
2263
+ // TODO: Re-enable when license key system is fully implemented
2264
+ // const LICENSE_WARNING_MESSAGE =
2265
+ // '\n\n---\n⚠️ License Notice: Your AsterMind license key needs to be updated. ' +
2266
+ // 'Please contact support@astermind.ai or visit https://astermind.ai/license to renew your license.';
2182
2267
  /**
2183
2268
  * Detect current environment based on URL and other signals
2184
2269
  */
@@ -2246,7 +2331,8 @@ LJ5AZXvOhHaXdHzMuYKX5BpK4w7TqbPvJ6QPvKmLKvHh1VKcUJ6mJQgJJw==
2246
2331
  constructor(config = {}) {
2247
2332
  this.state = null;
2248
2333
  this.verificationPromise = null;
2249
- this.hasLoggedWarning = false;
2334
+ // TODO: Re-enable when license key system is fully implemented
2335
+ // private hasLoggedWarning = false;
2250
2336
  this.loggedMissingFeatures = new Set();
2251
2337
  this.config = config;
2252
2338
  this.environment = config.environment || detectEnvironment();
@@ -2323,25 +2409,30 @@ LJ5AZXvOhHaXdHzMuYKX5BpK4w7TqbPvJ6QPvKmLKvHh1VKcUJ6mJQgJJw==
2323
2409
  * @returns Modified response (or original if license valid)
2324
2410
  */
2325
2411
  processResponse(response) {
2326
- // License not verified yet - allow through
2327
- if (!this.state) {
2328
- return response;
2329
- }
2330
- // Valid license - no modification
2331
- if (this.isValid() && !this.state.inGracePeriod) {
2332
- return response;
2333
- }
2334
- // Handle based on enforcement mode
2335
- if (this.enforcementMode === 'soft') {
2336
- // Development: just log warning (once)
2337
- if (!this.hasLoggedWarning) {
2338
- this.logDevelopmentWarning();
2339
- this.hasLoggedWarning = true;
2340
- }
2341
- return response;
2342
- }
2343
- // Production: append warning to response
2344
- return response + LICENSE_WARNING_MESSAGE;
2412
+ // TODO: License enforcement disabled until license key system is fully implemented
2413
+ return response;
2414
+ // // License not verified yet - allow through
2415
+ // if (!this.state) {
2416
+ // return response;
2417
+ // }
2418
+ //
2419
+ // // Valid license - no modification
2420
+ // if (this.isValid() && !this.state.inGracePeriod) {
2421
+ // return response;
2422
+ // }
2423
+ //
2424
+ // // Handle based on enforcement mode
2425
+ // if (this.enforcementMode === 'soft') {
2426
+ // // Development: just log warning (once)
2427
+ // if (!this.hasLoggedWarning) {
2428
+ // this.logDevelopmentWarning();
2429
+ // this.hasLoggedWarning = true;
2430
+ // }
2431
+ // return response;
2432
+ // }
2433
+ //
2434
+ // // Production: append warning to response
2435
+ // return response + LICENSE_WARNING_MESSAGE;
2345
2436
  }
2346
2437
  /**
2347
2438
  * Get a human-readable license status message
@@ -2373,7 +2464,7 @@ LJ5AZXvOhHaXdHzMuYKX5BpK4w7TqbPvJ6QPvKmLKvHh1VKcUJ6mJQgJJw==
2373
2464
  */
2374
2465
  async reVerify() {
2375
2466
  this.state = null;
2376
- this.hasLoggedWarning = false;
2467
+ // this.hasLoggedWarning = false;
2377
2468
  this.loggedMissingFeatures.clear();
2378
2469
  return this.verify();
2379
2470
  }
@@ -2478,15 +2569,6 @@ LJ5AZXvOhHaXdHzMuYKX5BpK4w7TqbPvJ6QPvKmLKvHh1VKcUJ6mJQgJJw==
2478
2569
  break;
2479
2570
  }
2480
2571
  }
2481
- /**
2482
- * Log development-only warning
2483
- */
2484
- logDevelopmentWarning() {
2485
- console.warn('[Cybernetic License] Running in development mode with ' +
2486
- `${this.state?.status || 'unknown'} license. ` +
2487
- 'In production, a warning will be appended to chatbot responses. ' +
2488
- 'Get a license at https://astermind.ai/license');
2489
- }
2490
2572
  }
2491
2573
  /**
2492
2574
  * Create a pre-configured license manager and verify immediately
@@ -2520,6 +2602,8 @@ LJ5AZXvOhHaXdHzMuYKX5BpK4w7TqbPvJ6QPvKmLKvHh1VKcUJ6mJQgJJw==
2520
2602
  this.offlineStorage = null;
2521
2603
  // Omega RAG (optional, loaded dynamically)
2522
2604
  this.omegaRAG = null;
2605
+ // Sales API client (optional, enabled via salesMode config)
2606
+ this.salesClient = null;
2523
2607
  // Track if offline warning has been shown
2524
2608
  this.offlineWarningShown = false;
2525
2609
  // Resolve WebSocket URL (explicit → env → auto-derived from apiUrl)
@@ -2589,6 +2673,10 @@ LJ5AZXvOhHaXdHzMuYKX5BpK4w7TqbPvJ6QPvKmLKvHh1VKcUJ6mJQgJJw==
2589
2673
  // Silent failure on init - cache may be stale but usable
2590
2674
  });
2591
2675
  }
2676
+ // Initialize sales client if configured
2677
+ if (config.salesMode?.enabled) {
2678
+ this.salesClient = new SalesApiClient(config.apiUrl, config.apiKey);
2679
+ }
2592
2680
  // Initialize offline vector support if configured
2593
2681
  if (this.config.offline?.enabled) {
2594
2682
  this.initializeOffline(this.config.offline).catch((error) => {
@@ -2903,6 +2991,63 @@ LJ5AZXvOhHaXdHzMuYKX5BpK4w7TqbPvJ6QPvKmLKvHh1VKcUJ6mJQgJJw==
2903
2991
  startNewSession(sessionId) {
2904
2992
  this.sessionStorage.startNewSession(sessionId);
2905
2993
  }
2994
+ // ==================== SALES API METHODS ====================
2995
+ /**
2996
+ * Check if sales mode is enabled
2997
+ */
2998
+ isSalesModeEnabled() {
2999
+ return this.salesClient !== null;
3000
+ }
3001
+ /**
3002
+ * Get the sales API client for direct access
3003
+ */
3004
+ getSalesClient() {
3005
+ return this.salesClient;
3006
+ }
3007
+ /**
3008
+ * Send a sales-aware chat message.
3009
+ * Returns the assistant reply with sales context (lead score, CTA buttons, etc.).
3010
+ * Requires salesMode to be enabled in config.
3011
+ */
3012
+ async salesChat(message, options = {}) {
3013
+ if (!this.salesClient) {
3014
+ throw new Error('Sales mode is not enabled. Set salesMode.enabled = true in config.');
3015
+ }
3016
+ return this.salesClient.salesChat(message, options);
3017
+ }
3018
+ /**
3019
+ * Initialize or resume a visitor session.
3020
+ * Call on first page load to register the visitor with the backend.
3021
+ * Requires salesMode to be enabled in config.
3022
+ */
3023
+ async initVisitor(visitorId, pageUrl, referrer, utmParams) {
3024
+ if (!this.salesClient) {
3025
+ throw new Error('Sales mode is not enabled. Set salesMode.enabled = true in config.');
3026
+ }
3027
+ return this.salesClient.initVisitor(visitorId, pageUrl, referrer, utmParams);
3028
+ }
3029
+ /**
3030
+ * Track a page view event for a visitor.
3031
+ * Call when the visitor navigates to a new page.
3032
+ * Requires salesMode to be enabled in config.
3033
+ */
3034
+ async trackPageView(visitorId, url, title, timeOnPageMs, scrollDepth) {
3035
+ if (!this.salesClient) {
3036
+ throw new Error('Sales mode is not enabled. Set salesMode.enabled = true in config.');
3037
+ }
3038
+ return this.salesClient.trackPageView(visitorId, url, title, timeOnPageMs, scrollDepth);
3039
+ }
3040
+ /**
3041
+ * Submit a lead capture form.
3042
+ * Creates or updates a lead record in the backend.
3043
+ * Requires salesMode to be enabled in config.
3044
+ */
3045
+ async captureLeadForm(data) {
3046
+ if (!this.salesClient) {
3047
+ throw new Error('Sales mode is not enabled. Set salesMode.enabled = true in config.');
3048
+ }
3049
+ return this.salesClient.captureLeadForm(data);
3050
+ }
2906
3051
  // ==================== CORE METHODS ====================
2907
3052
  /**
2908
3053
  * Send a message to the chatbot