@astermind/cybernetic-chatbot-client 1.0.6 → 1.0.9

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 CHANGED
@@ -241,6 +241,86 @@ For licensing questions, contact support@astermind.ai.
241
241
 
242
242
  ## Configuration
243
243
 
244
+ All configuration is done in **your own project**—you never need to modify `node_modules` or the package source code.
245
+
246
+ ### Multi-Method Configuration
247
+
248
+ The client supports multiple configuration methods with a priority-based fallback chain. This allows you to use the most appropriate method for your hosting environment.
249
+
250
+ **Priority Order (highest to lowest):**
251
+
252
+ 1. **Constructor config** - Direct configuration passed to `CyberneticClient` or `createClient()`
253
+ 2. **Environment variables** - `VITE_ASTERMIND_RAG_API_KEY`, `REACT_APP_ASTERMIND_RAG_API_KEY`, etc.
254
+ 3. **SSR-injected config** - `window.__ASTERMIND_CONFIG__` (for server-side rendering)
255
+ 4. **Global object** - `window.astermindConfig`
256
+ 5. **Script data attributes** - `data-astermind-key`, `data-astermind-url`
257
+
258
+ #### Environment Variables
259
+
260
+ For bundled applications (Vite, Create React App, etc.), you can configure the client using environment variables:
261
+
262
+ **Vite:**
263
+ ```env
264
+ VITE_ASTERMIND_RAG_API_KEY=am_your_api_key
265
+ VITE_ASTERMIND_RAG_API_SERVER_URL=https://api.astermind.ai
266
+ ```
267
+
268
+ **Create React App:**
269
+ ```env
270
+ REACT_APP_ASTERMIND_RAG_API_KEY=am_your_api_key
271
+ REACT_APP_ASTERMIND_RAG_API_SERVER_URL=https://api.astermind.ai
272
+ ```
273
+
274
+ **Node.js / Server:**
275
+ ```env
276
+ ASTERMIND_RAG_API_KEY=am_your_api_key
277
+ ASTERMIND_RAG_API_SERVER_URL=https://api.astermind.ai
278
+ ```
279
+
280
+ #### Auto-Loading Configuration
281
+
282
+ Use `loadConfig()` to automatically detect configuration from available sources:
283
+
284
+ ```typescript
285
+ import { loadConfig, createClient } from '@astermind/cybernetic-chatbot-client';
286
+
287
+ // Auto-detect configuration (throws if no API key found)
288
+ const config = loadConfig();
289
+ const client = createClient(config);
290
+
291
+ // Or suppress errors and handle missing config gracefully
292
+ const config = loadConfig({ throwOnMissingKey: false });
293
+ if (config) {
294
+ const client = createClient(config);
295
+ } else {
296
+ console.log('Chatbot not configured');
297
+ }
298
+ ```
299
+
300
+ #### SSR / Runtime Injection
301
+
302
+ For server-side rendered applications, inject configuration at runtime:
303
+
304
+ ```html
305
+ <!-- In your SSR template -->
306
+ <script>
307
+ window.__ASTERMIND_CONFIG__ = {
308
+ apiKey: '<%= process.env.ASTERMIND_RAG_API_KEY %>',
309
+ apiUrl: '<%= process.env.ASTERMIND_RAG_API_SERVER_URL %>'
310
+ };
311
+ </script>
312
+ ```
313
+
314
+ #### Configuration Source Debugging
315
+
316
+ The loaded configuration includes a `_source` field for debugging:
317
+
318
+ ```typescript
319
+ const config = loadConfig();
320
+ console.log(config._source);
321
+ // 'env' | 'vite' | 'window' | 'data-attr' | 'props'
322
+ ```
323
+
244
324
  ### Full Configuration Interface
245
325
 
246
326
  ```typescript
@@ -323,7 +403,7 @@ interface AgenticConfig {
323
403
 
324
404
  ### Offline-First Architecture
325
405
 
326
- The client uses IndexedDB for local caching and TF-IDF for similarity search, enabling offline functionality:
406
+ The client includes **built-in offline fallback** with IndexedDB caching and TF-IDF local search—no additional setup required. When the server is unreachable, the client automatically serves cached responses:
327
407
 
328
408
  ```typescript
329
409
  const client = new CyberneticClient({
package/dist/config.d.ts CHANGED
@@ -1,10 +1,45 @@
1
- import type { CyberneticConfig } from './types.js';
1
+ import type { CyberneticConfig, ConfigOptions } from './types.js';
2
2
  /**
3
3
  * Validate configuration
4
4
  */
5
5
  export declare function validateConfig(config: unknown): config is CyberneticConfig;
6
6
  /**
7
- * Load config from window.astermindConfig or script tag data attributes
7
+ * Load config from Node.js process.env (for bundlers that replace process.env)
8
8
  */
9
- export declare function loadConfig(): CyberneticConfig | null;
9
+ declare function loadFromProcessEnv(): CyberneticConfig | null;
10
+ /**
11
+ * Load config from Vite's import.meta.env (browser environment)
12
+ * Note: This only works at build time when Vite replaces the variables
13
+ */
14
+ declare function loadFromViteEnv(): CyberneticConfig | null;
15
+ /**
16
+ * Load config from window.astermindConfig global object
17
+ */
18
+ declare function loadFromGlobalObject(): CyberneticConfig | null;
19
+ /**
20
+ * Load config from script tag data attributes
21
+ */
22
+ declare function loadFromScriptAttributes(): CyberneticConfig | null;
23
+ /**
24
+ * Load config using priority-based fallback chain:
25
+ * 1. Environment variables (process.env - for bundlers/Node.js)
26
+ * 2. Vite environment variables (import.meta.env or window.__ASTERMIND_CONFIG__)
27
+ * 3. Global object (window.astermindConfig)
28
+ * 4. Script data attributes (data-astermind-key, data-astermind-url)
29
+ *
30
+ * @param options - Configuration options
31
+ * @param options.throwOnMissingKey - If true (default), throws when no API key found. If false, returns null and logs a warning.
32
+ * @returns Configuration object or null if not found and throwOnMissingKey is false
33
+ */
34
+ export declare function loadConfig(options?: ConfigOptions): CyberneticConfig | null;
35
+ /**
36
+ * Export individual loaders for testing and advanced use cases
37
+ */
38
+ export declare const configLoaders: {
39
+ loadFromProcessEnv: typeof loadFromProcessEnv;
40
+ loadFromViteEnv: typeof loadFromViteEnv;
41
+ loadFromGlobalObject: typeof loadFromGlobalObject;
42
+ loadFromScriptAttributes: typeof loadFromScriptAttributes;
43
+ };
44
+ export {};
10
45
  //# sourceMappingURL=config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,gBAAgB,CAoB1E;AAED;;GAEG;AACH,wBAAgB,UAAU,IAAI,gBAAgB,GAAG,IAAI,CAwBpD"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAKlE;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,gBAAgB,CAoB1E;AAED;;GAEG;AACH,iBAAS,kBAAkB,IAAI,gBAAgB,GAAG,IAAI,CA+BrD;AAED;;;GAGG;AACH,iBAAS,eAAe,IAAI,gBAAgB,GAAG,IAAI,CAmClD;AAED;;GAEG;AACH,iBAAS,oBAAoB,IAAI,gBAAgB,GAAG,IAAI,CAgBvD;AAED;;GAEG;AACH,iBAAS,wBAAwB,IAAI,gBAAgB,GAAG,IAAI,CAkB3D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,OAAO,GAAE,aAAkB,GAAG,gBAAgB,GAAG,IAAI,CA0C/E;AAED;;GAEG;AACH,eAAO,MAAM,aAAa;;;;;CAKzB,CAAC"}
@@ -2010,6 +2010,8 @@ class CyberneticClient {
2010
2010
 
2011
2011
  // src/config.ts
2012
2012
  // Configuration loading and validation
2013
+ /** Default API URL when not specified */
2014
+ const DEFAULT_API_URL = 'https://api.astermind.ai';
2013
2015
  /**
2014
2016
  * Validate configuration
2015
2017
  */
@@ -2030,30 +2032,160 @@ function validateConfig(config) {
2030
2032
  return true;
2031
2033
  }
2032
2034
  /**
2033
- * Load config from window.astermindConfig or script tag data attributes
2035
+ * Load config from Node.js process.env (for bundlers that replace process.env)
2034
2036
  */
2035
- function loadConfig() {
2037
+ function loadFromProcessEnv() {
2038
+ // Check if process.env exists (Node.js or bundler-injected)
2039
+ if (typeof process === 'undefined' || !process.env) {
2040
+ return null;
2041
+ }
2042
+ // Check for ASTERMIND_RAG_* env vars (non-prefixed, for Node.js/server environments)
2043
+ const apiKey = process.env.ASTERMIND_RAG_API_KEY;
2044
+ const apiUrl = process.env.ASTERMIND_RAG_API_SERVER_URL;
2045
+ if (apiKey) {
2046
+ return {
2047
+ apiKey,
2048
+ apiUrl: apiUrl || DEFAULT_API_URL,
2049
+ _source: 'env'
2050
+ };
2051
+ }
2052
+ // Check for CRA-style REACT_APP_* env vars
2053
+ const craApiKey = process.env.REACT_APP_ASTERMIND_RAG_API_KEY;
2054
+ const craApiUrl = process.env.REACT_APP_ASTERMIND_RAG_API_SERVER_URL;
2055
+ if (craApiKey) {
2056
+ return {
2057
+ apiKey: craApiKey,
2058
+ apiUrl: craApiUrl || DEFAULT_API_URL,
2059
+ _source: 'env'
2060
+ };
2061
+ }
2062
+ return null;
2063
+ }
2064
+ /**
2065
+ * Load config from Vite's import.meta.env (browser environment)
2066
+ * Note: This only works at build time when Vite replaces the variables
2067
+ */
2068
+ function loadFromViteEnv() {
2069
+ // Check for window.__ASTERMIND_CONFIG__ (SSR-injected config)
2070
+ if (typeof window !== 'undefined') {
2071
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2072
+ const injected = window.__ASTERMIND_CONFIG__;
2073
+ if (injected && typeof injected === 'object' && injected.apiKey) {
2074
+ return {
2075
+ ...injected,
2076
+ apiUrl: injected.apiUrl || DEFAULT_API_URL,
2077
+ _source: 'vite'
2078
+ };
2079
+ }
2080
+ }
2081
+ // Check for Vite's import.meta.env (replaced at build time)
2082
+ // Note: TypeScript doesn't know about import.meta.env, so we use a try-catch
2083
+ try {
2084
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2085
+ const importMeta = globalThis.import?.meta?.env;
2086
+ if (importMeta) {
2087
+ const apiKey = importMeta.VITE_ASTERMIND_RAG_API_KEY;
2088
+ const apiUrl = importMeta.VITE_ASTERMIND_RAG_API_SERVER_URL;
2089
+ if (apiKey) {
2090
+ return {
2091
+ apiKey,
2092
+ apiUrl: apiUrl || DEFAULT_API_URL,
2093
+ _source: 'vite'
2094
+ };
2095
+ }
2096
+ }
2097
+ }
2098
+ catch {
2099
+ // import.meta not available in this environment
2100
+ }
2101
+ return null;
2102
+ }
2103
+ /**
2104
+ * Load config from window.astermindConfig global object
2105
+ */
2106
+ function loadFromGlobalObject() {
2036
2107
  if (typeof window === 'undefined') {
2037
2108
  return null;
2038
2109
  }
2039
- // Check for global config object
2040
2110
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2041
- if (window.astermindConfig) {
2042
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
2043
- const config = window.astermindConfig;
2044
- validateConfig(config);
2045
- return config;
2111
+ const globalConfig = window.astermindConfig;
2112
+ if (globalConfig && typeof globalConfig === 'object' && globalConfig.apiKey) {
2113
+ return {
2114
+ ...globalConfig,
2115
+ apiUrl: globalConfig.apiUrl || DEFAULT_API_URL,
2116
+ _source: 'window'
2117
+ };
2118
+ }
2119
+ return null;
2120
+ }
2121
+ /**
2122
+ * Load config from script tag data attributes
2123
+ */
2124
+ function loadFromScriptAttributes() {
2125
+ if (typeof window === 'undefined' || typeof document === 'undefined') {
2126
+ return null;
2046
2127
  }
2047
- // Check for script tag with data attributes
2048
2128
  const script = document.querySelector('script[data-astermind-key]');
2049
2129
  if (script) {
2050
- return {
2051
- apiUrl: script.getAttribute('data-astermind-url') || 'https://api.astermind.ai',
2052
- apiKey: script.getAttribute('data-astermind-key') || ''
2053
- };
2130
+ const apiKey = script.getAttribute('data-astermind-key');
2131
+ if (apiKey) {
2132
+ return {
2133
+ apiKey,
2134
+ apiUrl: script.getAttribute('data-astermind-url') || DEFAULT_API_URL,
2135
+ _source: 'data-attr'
2136
+ };
2137
+ }
2054
2138
  }
2055
2139
  return null;
2056
2140
  }
2141
+ /**
2142
+ * Load config using priority-based fallback chain:
2143
+ * 1. Environment variables (process.env - for bundlers/Node.js)
2144
+ * 2. Vite environment variables (import.meta.env or window.__ASTERMIND_CONFIG__)
2145
+ * 3. Global object (window.astermindConfig)
2146
+ * 4. Script data attributes (data-astermind-key, data-astermind-url)
2147
+ *
2148
+ * @param options - Configuration options
2149
+ * @param options.throwOnMissingKey - If true (default), throws when no API key found. If false, returns null and logs a warning.
2150
+ * @returns Configuration object or null if not found and throwOnMissingKey is false
2151
+ */
2152
+ function loadConfig(options = {}) {
2153
+ const { throwOnMissingKey = true } = options;
2154
+ // Priority 1: Environment variables (Node.js/bundler)
2155
+ const envConfig = loadFromProcessEnv();
2156
+ if (envConfig) {
2157
+ return envConfig;
2158
+ }
2159
+ // Priority 2: Vite environment variables / SSR-injected config
2160
+ const viteConfig = loadFromViteEnv();
2161
+ if (viteConfig) {
2162
+ return viteConfig;
2163
+ }
2164
+ // Priority 3: Global object (window.astermindConfig)
2165
+ const globalConfig = loadFromGlobalObject();
2166
+ if (globalConfig) {
2167
+ validateConfig(globalConfig);
2168
+ return globalConfig;
2169
+ }
2170
+ // Priority 4: Script data attributes
2171
+ const scriptConfig = loadFromScriptAttributes();
2172
+ if (scriptConfig) {
2173
+ return scriptConfig;
2174
+ }
2175
+ // No config found
2176
+ if (throwOnMissingKey) {
2177
+ throw new Error('AsterMind API key is required. Configure using one of these methods:\n' +
2178
+ ' 1. Set VITE_ASTERMIND_RAG_API_KEY environment variable (Vite)\n' +
2179
+ ' 2. Set REACT_APP_ASTERMIND_RAG_API_KEY environment variable (CRA)\n' +
2180
+ ' 3. Set window.astermindConfig = { apiKey: "am_...", apiUrl: "..." }\n' +
2181
+ ' 4. Add data-astermind-key attribute to your script tag\n' +
2182
+ ' 5. Pass apiKey directly to createClient() or CyberneticClient constructor');
2183
+ }
2184
+ else {
2185
+ console.warn('[AsterMind] No API key found. Chatbot will not function until configured.');
2186
+ return null;
2187
+ }
2188
+ }
2057
2189
 
2058
2190
  // src/agentic/CyberneticIntentClassifier.ts
2059
2191
  // Hybrid intent classification for agentic capabilities