humanbehavior-js 0.4.13 → 0.4.14

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.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import * as fs from 'fs';
3
3
  import * as path from 'path';
4
- import * as readline from 'readline';
4
+ import * as clack from '@clack/prompts';
5
5
 
6
6
  /******************************************************************************
7
7
  Copyright (c) Microsoft Corporation.
@@ -165,6 +165,24 @@ class AutoInstallationWizard {
165
165
  projectRoot: this.projectRoot
166
166
  };
167
167
  }
168
+ else if (dependencies.astro) {
169
+ framework = {
170
+ name: 'astro',
171
+ type: 'astro',
172
+ hasTypeScript: !!dependencies.typescript || !!dependencies['@astrojs/ts-plugin'],
173
+ hasRouter: true,
174
+ projectRoot: this.projectRoot
175
+ };
176
+ }
177
+ else if (dependencies.gatsby) {
178
+ framework = {
179
+ name: 'gatsby',
180
+ type: 'gatsby',
181
+ hasTypeScript: !!dependencies.typescript || !!dependencies['@types/react'],
182
+ hasRouter: true,
183
+ projectRoot: this.projectRoot
184
+ };
185
+ }
168
186
  // Detect bundler
169
187
  if (dependencies.vite) {
170
188
  framework.bundler = 'vite';
@@ -196,13 +214,18 @@ class AutoInstallationWizard {
196
214
  */
197
215
  installPackage() {
198
216
  return __awaiter(this, void 0, void 0, function* () {
199
- var _a, _b;
217
+ var _a, _b, _c, _d;
200
218
  const { execSync } = yield import('child_process');
201
- const command = ((_a = this.framework) === null || _a === void 0 ? void 0 : _a.packageManager) === 'yarn'
219
+ // Build base command
220
+ let command = ((_a = this.framework) === null || _a === void 0 ? void 0 : _a.packageManager) === 'yarn'
202
221
  ? 'yarn add humanbehavior-js'
203
222
  : ((_b = this.framework) === null || _b === void 0 ? void 0 : _b.packageManager) === 'pnpm'
204
223
  ? 'pnpm add humanbehavior-js'
205
224
  : 'npm install humanbehavior-js';
225
+ // Add legacy peer deps flag for npm to handle dependency conflicts
226
+ if (((_c = this.framework) === null || _c === void 0 ? void 0 : _c.packageManager) !== 'yarn' && ((_d = this.framework) === null || _d === void 0 ? void 0 : _d.packageManager) !== 'pnpm') {
227
+ command += ' --legacy-peer-deps';
228
+ }
206
229
  try {
207
230
  execSync(command, { cwd: this.projectRoot, stdio: 'inherit' });
208
231
  }
@@ -228,6 +251,12 @@ class AutoInstallationWizard {
228
251
  case 'nuxt':
229
252
  modifications.push(...yield this.generateNuxtModifications());
230
253
  break;
254
+ case 'astro':
255
+ modifications.push(...yield this.generateAstroModifications());
256
+ break;
257
+ case 'gatsby':
258
+ modifications.push(...yield this.generateGatsbyModifications());
259
+ break;
231
260
  case 'remix':
232
261
  modifications.push(...yield this.generateRemixModifications());
233
262
  break;
@@ -339,6 +368,84 @@ export function Providers({ children }: { children: React.ReactNode }) {
339
368
  return modifications;
340
369
  });
341
370
  }
371
+ /**
372
+ * Generate Astro-specific modifications
373
+ */
374
+ generateAstroModifications() {
375
+ return __awaiter(this, void 0, void 0, function* () {
376
+ const modifications = [];
377
+ // Create Astro component for HumanBehavior
378
+ const astroComponentPath = path.join(this.projectRoot, 'src', 'components', 'HumanBehavior.astro');
379
+ const astroComponentContent = `---
380
+ // This component will only run on the client side
381
+ ---
382
+
383
+ <script>
384
+ import { HumanBehaviorTracker } from 'humanbehavior-js';
385
+
386
+ // Get API key from environment variable
387
+ const apiKey = import.meta.env.PUBLIC_HUMANBEHAVIOR_API_KEY;
388
+
389
+ console.log('HumanBehavior: API key found:', apiKey ? 'Yes' : 'No');
390
+
391
+ if (apiKey) {
392
+ try {
393
+ const tracker = HumanBehaviorTracker.init(apiKey);
394
+ console.log('HumanBehavior: Tracker initialized successfully');
395
+
396
+ // Test event to verify tracking is working
397
+ setTimeout(() => {
398
+ tracker.customEvent('astro_page_view', {
399
+ page: window.location.pathname,
400
+ framework: 'astro'
401
+ }).then(() => {
402
+ console.log('HumanBehavior: Test event sent successfully');
403
+ }).catch((error) => {
404
+ console.error('HumanBehavior: Failed to send test event:', error);
405
+ });
406
+ }, 1000);
407
+
408
+ } catch (error) {
409
+ console.error('HumanBehavior: Failed to initialize tracker:', error);
410
+ }
411
+ } else {
412
+ console.error('HumanBehavior: No API key found');
413
+ }
414
+ </script>`;
415
+ modifications.push({
416
+ filePath: astroComponentPath,
417
+ action: 'create',
418
+ content: astroComponentContent,
419
+ description: 'Created Astro component for HumanBehavior SDK'
420
+ });
421
+ // Find and update layout file
422
+ const layoutFiles = [
423
+ path.join(this.projectRoot, 'src', 'layouts', 'Layout.astro'),
424
+ path.join(this.projectRoot, 'src', 'layouts', 'layout.astro'),
425
+ path.join(this.projectRoot, 'src', 'layouts', 'BaseLayout.astro')
426
+ ];
427
+ let layoutFile = null;
428
+ for (const file of layoutFiles) {
429
+ if (fs.existsSync(file)) {
430
+ layoutFile = file;
431
+ break;
432
+ }
433
+ }
434
+ if (layoutFile) {
435
+ const content = fs.readFileSync(layoutFile, 'utf8');
436
+ const modifiedContent = this.injectAstroLayout(content);
437
+ modifications.push({
438
+ filePath: layoutFile,
439
+ action: 'modify',
440
+ content: modifiedContent,
441
+ description: 'Added HumanBehavior component to Astro layout'
442
+ });
443
+ }
444
+ // Add environment variable
445
+ modifications.push(this.createEnvironmentModification(this.framework));
446
+ return modifications;
447
+ });
448
+ }
342
449
  /**
343
450
  * Generate Nuxt-specific modifications
344
451
  */
@@ -559,6 +666,50 @@ export default defineNuxtPlugin(() => {
559
666
  return modifications;
560
667
  });
561
668
  }
669
+ /**
670
+ * Generate Gatsby-specific modifications
671
+ */
672
+ generateGatsbyModifications() {
673
+ return __awaiter(this, void 0, void 0, function* () {
674
+ const modifications = [];
675
+ // Modify or create gatsby-browser.js for Gatsby
676
+ const gatsbyBrowserFile = path.join(this.projectRoot, 'gatsby-browser.js');
677
+ if (fs.existsSync(gatsbyBrowserFile)) {
678
+ const content = fs.readFileSync(gatsbyBrowserFile, 'utf8');
679
+ const modifiedContent = this.injectGatsbyBrowser(content);
680
+ modifications.push({
681
+ filePath: gatsbyBrowserFile,
682
+ action: 'modify',
683
+ content: modifiedContent,
684
+ description: 'Added HumanBehavior initialization to Gatsby browser'
685
+ });
686
+ }
687
+ else {
688
+ // Create gatsby-browser.js if it doesn't exist
689
+ modifications.push({
690
+ filePath: gatsbyBrowserFile,
691
+ action: 'create',
692
+ content: `import { HumanBehaviorTracker } from 'humanbehavior-js';
693
+
694
+ export const onClientEntry = () => {
695
+ console.log('Gatsby browser entry point loaded');
696
+ const apiKey = process.env.GATSBY_HUMANBEHAVIOR_API_KEY;
697
+ console.log('API Key found:', apiKey ? 'Yes' : 'No');
698
+ if (apiKey) {
699
+ const tracker = HumanBehaviorTracker.init(apiKey);
700
+ console.log('HumanBehavior SDK initialized for Gatsby');
701
+ } else {
702
+ console.log('No API key found in environment variables');
703
+ }
704
+ };`,
705
+ description: 'Created gatsby-browser.js with HumanBehavior initialization'
706
+ });
707
+ }
708
+ // Create or append to environment file
709
+ modifications.push(this.createEnvironmentModification(this.framework));
710
+ return modifications;
711
+ });
712
+ }
562
713
  /**
563
714
  * Apply modifications to the codebase
564
715
  */
@@ -864,6 +1015,37 @@ if (typeof window !== 'undefined') {
864
1015
  </script>`;
865
1016
  return content.replace(/<\/head>/, ` ${cdnScript}\n ${initScript}\n</head>`);
866
1017
  }
1018
+ /**
1019
+ * Inject Astro layout with HumanBehavior component
1020
+ */
1021
+ injectAstroLayout(content) {
1022
+ // Check if HumanBehavior component is already imported
1023
+ if (content.includes('HumanBehavior') || content.includes('humanbehavior-js')) {
1024
+ return content; // Already has HumanBehavior
1025
+ }
1026
+ // Add import inside frontmatter if not present
1027
+ let modifiedContent = content;
1028
+ if (!content.includes('import HumanBehavior')) {
1029
+ const importStatement = 'import HumanBehavior from \'../components/HumanBehavior.astro\';';
1030
+ const frontmatterEndIndex = content.indexOf('---', 3);
1031
+ if (frontmatterEndIndex !== -1) {
1032
+ // Insert import inside frontmatter, before the closing ---
1033
+ modifiedContent = content.slice(0, frontmatterEndIndex) + '\n' + importStatement + '\n' + content.slice(frontmatterEndIndex);
1034
+ }
1035
+ else {
1036
+ // No frontmatter, add at the very beginning
1037
+ modifiedContent = '---\n' + importStatement + '\n---\n\n' + content;
1038
+ }
1039
+ }
1040
+ // Find the closing </body> tag and add HumanBehavior component before it
1041
+ const bodyCloseIndex = modifiedContent.lastIndexOf('</body>');
1042
+ if (bodyCloseIndex === -1) {
1043
+ // No body tag found, append to end
1044
+ return modifiedContent + '\n\n<HumanBehavior />';
1045
+ }
1046
+ // Add component before closing body tag
1047
+ return modifiedContent.slice(0, bodyCloseIndex) + ' <HumanBehavior />\n' + modifiedContent.slice(bodyCloseIndex);
1048
+ }
867
1049
  injectNuxtConfig(content) {
868
1050
  if (content.includes('humanBehaviorApiKey')) {
869
1051
  return content;
@@ -876,6 +1058,45 @@ if (typeof window !== 'undefined') {
876
1058
  }
877
1059
  },`);
878
1060
  }
1061
+ injectGatsbyLayout(content) {
1062
+ if (content.includes('HumanBehavior')) {
1063
+ return content;
1064
+ }
1065
+ const importStatement = `import HumanBehavior from './HumanBehavior';`;
1066
+ const componentUsage = `<HumanBehavior apiKey={process.env.GATSBY_HUMANBEHAVIOR_API_KEY || ''} />`;
1067
+ // Add import at the top
1068
+ let modifiedContent = content.replace(/import.*from.*['"]\./, `${importStatement}\n$&`);
1069
+ // Add component before closing body tag
1070
+ modifiedContent = modifiedContent.replace(/(\s*<\/body>)/, `\n ${componentUsage}\n$1`);
1071
+ return modifiedContent;
1072
+ }
1073
+ injectGatsbyBrowser(content) {
1074
+ if (content.includes('HumanBehaviorTracker')) {
1075
+ return content;
1076
+ }
1077
+ const importStatement = `import { HumanBehaviorTracker } from 'humanbehavior-js';`;
1078
+ const initCode = `
1079
+ // Initialize HumanBehavior SDK
1080
+ export const onClientEntry = () => {
1081
+ console.log('Gatsby browser entry point loaded');
1082
+ const apiKey = process.env.GATSBY_HUMANBEHAVIOR_API_KEY;
1083
+ console.log('API Key found:', apiKey ? 'Yes' : 'No');
1084
+ if (apiKey) {
1085
+ const tracker = HumanBehaviorTracker.init(apiKey);
1086
+ console.log('HumanBehavior SDK initialized for Gatsby');
1087
+ } else {
1088
+ console.log('No API key found in environment variables');
1089
+ }
1090
+ };`;
1091
+ // If the file already has content, add the import and init code
1092
+ if (content.trim()) {
1093
+ return `${importStatement}${initCode}\n\n${content}`;
1094
+ }
1095
+ else {
1096
+ // If file is empty, just return the new content
1097
+ return `${importStatement}${initCode}`;
1098
+ }
1099
+ }
879
1100
  /**
880
1101
  * Helper method to find the best environment file for a framework
881
1102
  */
@@ -905,6 +1126,8 @@ if (typeof window !== 'undefined') {
905
1126
  nuxt: 'NUXT_PUBLIC_HUMANBEHAVIOR_API_KEY',
906
1127
  remix: 'HUMANBEHAVIOR_API_KEY',
907
1128
  vanilla: 'HUMANBEHAVIOR_API_KEY',
1129
+ astro: 'PUBLIC_HUMANBEHAVIOR_API_KEY',
1130
+ gatsby: 'GATSBY_HUMANBEHAVIOR_API_KEY',
908
1131
  node: 'HUMANBEHAVIOR_API_KEY',
909
1132
  auto: 'HUMANBEHAVIOR_API_KEY'
910
1133
  };
@@ -928,6 +1151,8 @@ if (typeof window !== 'undefined') {
928
1151
  nuxt: '.env',
929
1152
  remix: '.env.local',
930
1153
  vanilla: '.env',
1154
+ astro: '.env',
1155
+ gatsby: '.env.development',
931
1156
  node: '.env',
932
1157
  auto: '.env'
933
1158
  };
@@ -942,6 +1167,8 @@ if (typeof window !== 'undefined') {
942
1167
  */
943
1168
  createEnvironmentModification(framework) {
944
1169
  const { filePath, envVarName } = this.findBestEnvFile(framework);
1170
+ // Clean the API key to prevent formatting issues
1171
+ const cleanApiKey = this.apiKey.trim();
945
1172
  if (fs.existsSync(filePath)) {
946
1173
  // Check if the variable already exists
947
1174
  const content = fs.readFileSync(filePath, 'utf8');
@@ -959,7 +1186,7 @@ if (typeof window !== 'undefined') {
959
1186
  return {
960
1187
  filePath,
961
1188
  action: 'append',
962
- content: `\n${envVarName}=${this.apiKey}`,
1189
+ content: `\n${envVarName}=${cleanApiKey}`,
963
1190
  description: `Added API key to existing ${path.basename(filePath)}`
964
1191
  };
965
1192
  }
@@ -969,309 +1196,13 @@ if (typeof window !== 'undefined') {
969
1196
  return {
970
1197
  filePath,
971
1198
  action: 'create',
972
- content: `${envVarName}=${this.apiKey}`,
1199
+ content: `${envVarName}=${cleanApiKey}`,
973
1200
  description: `Created ${path.basename(filePath)} with API key`
974
1201
  };
975
1202
  }
976
1203
  }
977
1204
  }
978
1205
 
979
- /**
980
- * AI-Enhanced HumanBehavior SDK Auto-Installation Wizard
981
- *
982
- * This wizard uses AI to intelligently detect frameworks, analyze code patterns,
983
- * and generate optimal integration code that's both future-proof and backward-compatible.
984
- *
985
- * 🚀 KEY FEATURES:
986
- * - AI-powered framework detection beyond package.json
987
- * - Intelligent code pattern analysis
988
- * - Future-proof integration strategies
989
- * - Backward compatibility with legacy frameworks
990
- * - Adaptive code generation for new frameworks
991
- * - Smart conflict resolution
992
- * - Learning from user feedback
993
- * - Centralized AI service (no user API keys required)
994
- */
995
- /**
996
- * Default AI Service Implementation
997
- * Uses heuristic analysis when centralized service is not available
998
- */
999
- class DefaultAIService {
1000
- analyzeCodePatterns(codeSamples) {
1001
- return __awaiter(this, void 0, void 0, function* () {
1002
- return this.analyzeWithHeuristics(codeSamples);
1003
- });
1004
- }
1005
- resolveConflicts(conflicts, framework) {
1006
- return __awaiter(this, void 0, void 0, function* () {
1007
- // Default conflict resolution strategies
1008
- const resolutions = [];
1009
- for (const conflict of conflicts) {
1010
- switch (conflict) {
1011
- case 'existing_humanbehavior_code':
1012
- resolutions.push('update_existing_integration');
1013
- break;
1014
- case 'existing_provider':
1015
- resolutions.push('merge_providers');
1016
- break;
1017
- case 'module_system_conflict':
1018
- resolutions.push('hybrid_module_support');
1019
- break;
1020
- default:
1021
- resolutions.push('skip_conflict');
1022
- }
1023
- }
1024
- return resolutions;
1025
- });
1026
- }
1027
- generateOptimizations(framework, patterns) {
1028
- return __awaiter(this, void 0, void 0, function* () {
1029
- const optimizations = [];
1030
- // Framework-specific optimizations
1031
- switch (framework.type) {
1032
- case 'react':
1033
- optimizations.push('Use React.memo for performance optimization');
1034
- optimizations.push('Implement error boundaries for better error tracking');
1035
- optimizations.push('Consider using React.lazy for code splitting');
1036
- break;
1037
- case 'vue':
1038
- optimizations.push('Use Vue 3 Composition API for better performance');
1039
- optimizations.push('Implement proper error handling in components');
1040
- optimizations.push('Consider using Vue Router for navigation tracking');
1041
- break;
1042
- case 'angular':
1043
- optimizations.push('Use Angular standalone components for better tree-shaking');
1044
- optimizations.push('Implement proper error handling with ErrorHandler');
1045
- optimizations.push('Consider using Angular signals for state management');
1046
- break;
1047
- default:
1048
- optimizations.push('Enable performance tracking');
1049
- optimizations.push('Implement error tracking');
1050
- optimizations.push('Consider progressive enhancement');
1051
- }
1052
- return optimizations;
1053
- });
1054
- }
1055
- analyzeWithHeuristics(codeSamples) {
1056
- const patterns = codeSamples.join(' ').toLowerCase();
1057
- // Framework detection
1058
- let framework = { name: 'vanilla', type: 'vanilla' };
1059
- let confidence = 0.5;
1060
- if (patterns.includes('nuxt') || patterns.includes('nuxtjs') || patterns.includes('defineNuxtConfig') || patterns.includes('nuxt.config') || patterns.includes('@nuxt/') || patterns.includes('useNuxtApp') || patterns.includes('useRuntimeConfig') || patterns.includes('useSeoMeta') || patterns.includes('useHead') || patterns.includes('useLazyFetch') || patterns.includes('useFetch') || patterns.includes('useAsyncData') || patterns.includes('#app')) {
1061
- framework = { name: 'nuxt', type: 'nuxt' };
1062
- confidence = 0.95;
1063
- }
1064
- else if (patterns.includes('next') || patterns.includes('nextjs') || patterns.includes('next/link') || patterns.includes('next/image') || patterns.includes('next/navigation') || patterns.includes('next/router') || patterns.includes('getserverSideProps') || patterns.includes('getstaticProps') || patterns.includes('getstaticPaths') || patterns.includes('app/layout') || patterns.includes('app/page') || patterns.includes('pages/')) {
1065
- framework = { name: 'nextjs', type: 'nextjs' };
1066
- confidence = 0.95;
1067
- }
1068
- else if (patterns.includes('react')) {
1069
- framework = { name: 'react', type: 'react' };
1070
- confidence = 0.9;
1071
- }
1072
- else if (patterns.includes('vue')) {
1073
- framework = { name: 'vue', type: 'vue' };
1074
- confidence = 0.9;
1075
- }
1076
- else if (patterns.includes('angular')) {
1077
- framework = { name: 'angular', type: 'angular' };
1078
- confidence = 0.9;
1079
- }
1080
- else if (patterns.includes('svelte')) {
1081
- framework = { name: 'svelte', type: 'svelte' };
1082
- confidence = 0.9;
1083
- }
1084
- // Integration strategy
1085
- let integrationStrategy = 'script';
1086
- if (framework.type === 'react' || framework.type === 'nextjs') {
1087
- integrationStrategy = 'provider';
1088
- }
1089
- else if (framework.type === 'vue') {
1090
- integrationStrategy = 'plugin';
1091
- }
1092
- else if (framework.type === 'angular') {
1093
- integrationStrategy = 'module';
1094
- }
1095
- // Compatibility mode
1096
- let compatibilityMode = 'modern';
1097
- if (patterns.includes('require(') || patterns.includes('var ')) {
1098
- compatibilityMode = 'legacy';
1099
- }
1100
- return {
1101
- framework,
1102
- confidence,
1103
- patterns: codeSamples,
1104
- conflicts: [],
1105
- recommendations: [],
1106
- integrationStrategy,
1107
- compatibilityMode
1108
- };
1109
- }
1110
- }
1111
- /**
1112
- * Browser-based AI installation wizard
1113
- */
1114
- class AIBrowserInstallationWizard {
1115
- constructor(apiKey, aiService) {
1116
- this.apiKey = apiKey;
1117
- this.aiService = aiService || new DefaultAIService();
1118
- }
1119
- install() {
1120
- return __awaiter(this, void 0, void 0, function* () {
1121
- try {
1122
- // AI-powered browser detection
1123
- const aiAnalysis = yield this.performBrowserAIAnalysis();
1124
- // Generate AI-optimized browser modifications
1125
- const modifications = this.generateAIBrowserModifications(aiAnalysis);
1126
- return {
1127
- success: true,
1128
- framework: aiAnalysis.framework,
1129
- modifications,
1130
- errors: [],
1131
- nextSteps: [
1132
- '✅ AI-optimized browser integration ready!',
1133
- `🎯 Framework detected: ${aiAnalysis.framework.name}`,
1134
- `🔧 Integration strategy: ${aiAnalysis.integrationStrategy}`,
1135
- '📋 Copy the generated code to your project',
1136
- '🚀 Your app will be ready to track user behavior'
1137
- ],
1138
- aiAnalysis,
1139
- learningData: {
1140
- patterns: aiAnalysis.patterns,
1141
- framework: aiAnalysis.framework.name,
1142
- success: true
1143
- }
1144
- };
1145
- }
1146
- catch (error) {
1147
- return {
1148
- success: false,
1149
- framework: { name: 'unknown', type: 'vanilla' },
1150
- modifications: [],
1151
- errors: [error instanceof Error ? error.message : 'Unknown error'],
1152
- nextSteps: [],
1153
- aiAnalysis: {
1154
- framework: { name: 'unknown', type: 'vanilla' },
1155
- confidence: 0,
1156
- patterns: [],
1157
- conflicts: [],
1158
- recommendations: [],
1159
- integrationStrategy: 'script',
1160
- compatibilityMode: 'legacy'
1161
- },
1162
- learningData: {
1163
- patterns: [],
1164
- framework: 'unknown',
1165
- success: false
1166
- }
1167
- };
1168
- }
1169
- });
1170
- }
1171
- performBrowserAIAnalysis() {
1172
- return __awaiter(this, void 0, void 0, function* () {
1173
- // Browser-based framework detection
1174
- this.detectBrowserFramework();
1175
- // Analyze browser environment
1176
- const patterns = this.analyzeBrowserPatterns();
1177
- // Use centralized AI service for analysis
1178
- const codeSamples = [`Browser Environment: ${patterns.join(', ')}`];
1179
- return yield this.aiService.analyzeCodePatterns(codeSamples);
1180
- });
1181
- }
1182
- detectBrowserFramework() {
1183
- if (typeof window !== 'undefined') {
1184
- if (window.React) {
1185
- return { name: 'react', type: 'react' };
1186
- }
1187
- if (window.Vue) {
1188
- return { name: 'vue', type: 'vue' };
1189
- }
1190
- if (window.angular) {
1191
- return { name: 'angular', type: 'angular' };
1192
- }
1193
- }
1194
- return { name: 'vanilla', type: 'vanilla' };
1195
- }
1196
- analyzeBrowserPatterns() {
1197
- const patterns = [];
1198
- if (typeof window !== 'undefined') {
1199
- // Analyze global objects
1200
- if (window.React)
1201
- patterns.push('React global detected');
1202
- if (window.Vue)
1203
- patterns.push('Vue global detected');
1204
- if (window.angular)
1205
- patterns.push('Angular global detected');
1206
- // Analyze DOM patterns
1207
- if (document.querySelector('[data-reactroot]'))
1208
- patterns.push('React DOM detected');
1209
- if (document.querySelector('[data-vue]'))
1210
- patterns.push('Vue DOM detected');
1211
- // Analyze script patterns
1212
- const scripts = document.querySelectorAll('script');
1213
- scripts.forEach(script => {
1214
- if (script.src.includes('react'))
1215
- patterns.push('React script detected');
1216
- if (script.src.includes('vue'))
1217
- patterns.push('Vue script detected');
1218
- });
1219
- }
1220
- return patterns;
1221
- }
1222
- generateAIBrowserModifications(aiAnalysis) {
1223
- const modifications = [];
1224
- switch (aiAnalysis.framework.type) {
1225
- case 'react':
1226
- modifications.push({
1227
- filePath: 'App.jsx',
1228
- action: 'create',
1229
- content: `// AI-Optimized React Integration
1230
- import { HumanBehaviorProvider } from 'humanbehavior-js/react';
1231
-
1232
- function App() {
1233
- return (
1234
- <HumanBehaviorProvider
1235
- apiKey="${this.apiKey}"
1236
- config={{
1237
- // AI-generated optimizations
1238
- enablePerformanceTracking: true,
1239
- enableErrorTracking: true,
1240
- framework: '${aiAnalysis.framework.name}',
1241
- integrationStrategy: '${aiAnalysis.integrationStrategy}'
1242
- }}
1243
- >
1244
- {/* Your app components */}
1245
- </HumanBehaviorProvider>
1246
- );
1247
- }
1248
-
1249
- export default App;`,
1250
- description: 'AI-optimized React component with HumanBehaviorProvider'
1251
- });
1252
- break;
1253
- default:
1254
- modifications.push({
1255
- filePath: 'humanbehavior-init.js',
1256
- action: 'create',
1257
- content: `// AI-Optimized Vanilla JS Integration
1258
- import { HumanBehaviorTracker } from 'humanbehavior-js';
1259
-
1260
- const tracker = HumanBehaviorTracker.init('${this.apiKey}', {
1261
- // AI-generated configuration
1262
- framework: '${aiAnalysis.framework.name}',
1263
- integrationStrategy: '${aiAnalysis.integrationStrategy}',
1264
- compatibilityMode: '${aiAnalysis.compatibilityMode}',
1265
- enablePerformanceTracking: true,
1266
- enableErrorTracking: true
1267
- });`,
1268
- description: 'AI-optimized vanilla JS initialization'
1269
- });
1270
- }
1271
- return modifications;
1272
- }
1273
- }
1274
-
1275
1206
  /**
1276
1207
  * Remote AI Service Implementation
1277
1208
  *
@@ -1375,6 +1306,10 @@ class RemoteAIService {
1375
1306
  framework = { name: 'nextjs', type: 'nextjs' };
1376
1307
  confidence = 0.95;
1377
1308
  }
1309
+ else if (patterns.includes('gatsby') || patterns.includes('gatsby-browser') || patterns.includes('gatsby-ssr') || patterns.includes('gatsby-node') || patterns.includes('gatsby-config') || patterns.includes('useStaticQuery') || patterns.includes('graphql')) {
1310
+ framework = { name: 'gatsby', type: 'gatsby' };
1311
+ confidence = 0.95;
1312
+ }
1378
1313
  else if (patterns.includes('react')) {
1379
1314
  framework = { name: 'react', type: 'react' };
1380
1315
  confidence = 0.9;
@@ -1391,9 +1326,13 @@ class RemoteAIService {
1391
1326
  framework = { name: 'svelte', type: 'svelte' };
1392
1327
  confidence = 0.9;
1393
1328
  }
1329
+ else if (patterns.includes('astro')) {
1330
+ framework = { name: 'astro', type: 'astro' };
1331
+ confidence = 0.9;
1332
+ }
1394
1333
  // Integration strategy
1395
1334
  let integrationStrategy = 'script';
1396
- if (framework.type === 'react' || framework.type === 'nextjs') {
1335
+ if (framework.type === 'react' || framework.type === 'nextjs' || framework.type === 'gatsby') {
1397
1336
  integrationStrategy = 'provider';
1398
1337
  }
1399
1338
  else if (framework.type === 'vue') {
@@ -1636,6 +1575,8 @@ class ManualFrameworkInstallationWizard extends AutoInstallationWizard {
1636
1575
  'svelte': { name: 'svelte', type: 'svelte' },
1637
1576
  'sveltekit': { name: 'svelte', type: 'svelte' },
1638
1577
  'remix': { name: 'remix', type: 'remix' },
1578
+ 'astro': { name: 'astro', type: 'astro' },
1579
+ 'gatsby': { name: 'gatsby', type: 'gatsby' },
1639
1580
  'vanilla': { name: 'vanilla', type: 'vanilla' },
1640
1581
  'node': { name: 'node', type: 'node' },
1641
1582
  'auto': { name: 'auto-detected', type: 'auto' }
@@ -1695,67 +1636,46 @@ class ManualFrameworkInstallationWizard extends AutoInstallationWizard {
1695
1636
  class AIAutoInstallCLI {
1696
1637
  constructor(options) {
1697
1638
  this.options = options;
1698
- this.rl = readline.createInterface({
1699
- input: process.stdin,
1700
- output: process.stdout
1701
- });
1702
1639
  }
1703
1640
  run() {
1704
1641
  return __awaiter(this, void 0, void 0, function* () {
1705
- console.log('🤖 AI-Enhanced HumanBehavior SDK Auto-Installation');
1706
- console.log('================================================\n');
1642
+ clack.intro('🤖 AI-Enhanced HumanBehavior SDK Auto-Installation');
1707
1643
  try {
1708
1644
  // Get API key
1709
1645
  const apiKey = yield this.getApiKey();
1710
1646
  if (!apiKey) {
1711
- console.error('API key is required');
1647
+ clack.cancel('API key is required');
1712
1648
  process.exit(1);
1713
1649
  }
1714
1650
  // Get project path
1715
1651
  const projectPath = this.options.projectPath || process.cwd();
1716
- // Choose installation mode
1717
- const installationMode = yield this.chooseInstallationMode();
1652
+ // Choose framework
1653
+ const framework = yield this.chooseFramework();
1654
+ if (!framework) {
1655
+ clack.cancel('Installation cancelled.');
1656
+ process.exit(0);
1657
+ }
1718
1658
  // Confirm installation
1719
1659
  if (!this.options.yes) {
1720
- const confirmed = yield this.confirmInstallation(projectPath, installationMode);
1660
+ const confirmed = yield this.confirmInstallation(projectPath, framework);
1721
1661
  if (!confirmed) {
1722
- console.log('Installation cancelled.');
1662
+ clack.cancel('Installation cancelled.');
1723
1663
  process.exit(0);
1724
1664
  }
1725
1665
  }
1726
1666
  // Run installation
1727
- console.log('🔍 Analyzing your project with AI...');
1728
- let result;
1729
- if (this.options.browser) {
1730
- const wizard = new AIBrowserInstallationWizard(apiKey);
1731
- result = yield wizard.install();
1732
- }
1733
- else if (installationMode === 'manual') {
1734
- // Manual AI-Enhanced framework selection
1735
- const framework = yield this.chooseFramework();
1736
- const wizard = new ManualFrameworkInstallationWizard(apiKey, projectPath, framework);
1737
- result = yield wizard.install();
1738
- }
1739
- else if (installationMode.startsWith('manual:')) {
1740
- // Manual framework selection with pre-selected framework
1741
- const framework = installationMode.split(':')[1];
1742
- const wizard = new ManualFrameworkInstallationWizard(apiKey, projectPath, framework);
1743
- result = yield wizard.install();
1744
- }
1745
- else {
1746
- const wizard = new AutoInstallationWizard(apiKey, projectPath);
1747
- result = yield wizard.install();
1748
- }
1667
+ const spinner = clack.spinner();
1668
+ spinner.start('🔍 Analyzing your project with AI...');
1669
+ const wizard = new ManualFrameworkInstallationWizard(apiKey, projectPath, framework);
1670
+ const result = yield wizard.install();
1671
+ spinner.stop('Analysis complete!');
1749
1672
  // Display results
1750
- this.displayResults(result, installationMode);
1673
+ this.displayResults(result, framework);
1751
1674
  }
1752
1675
  catch (error) {
1753
- console.error('❌ Error:', error instanceof Error ? error.message : error);
1676
+ clack.cancel(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`);
1754
1677
  process.exit(1);
1755
1678
  }
1756
- finally {
1757
- this.rl.close();
1758
- }
1759
1679
  });
1760
1680
  }
1761
1681
  getApiKey() {
@@ -1763,262 +1683,143 @@ class AIAutoInstallCLI {
1763
1683
  if (this.options.apiKey) {
1764
1684
  return this.options.apiKey;
1765
1685
  }
1766
- return new Promise((resolve) => {
1767
- this.rl.question('Enter your HumanBehavior API key: ', (answer) => {
1768
- resolve(answer.trim());
1769
- });
1770
- });
1771
- });
1772
- }
1773
- chooseInstallationMode() {
1774
- return __awaiter(this, void 0, void 0, function* () {
1775
- if (this.options.manual && this.options.framework) {
1776
- return `manual:${this.options.framework}`;
1777
- }
1778
- if (this.options.useAI !== undefined) {
1779
- return this.options.useAI ? 'ai' : 'traditional';
1780
- }
1781
- console.log('🤖 Choose installation mode:');
1782
- console.log('1. Manual AI-Enhanced (recommended) - Choose your framework and use AI for optimization');
1783
- console.log('2. Traditional - Standard framework detection and installation');
1784
- console.log('3. Browser - Browser-based AI detection and code generation\n');
1785
- return new Promise((resolve) => {
1786
- this.rl.question('Select mode (1-3, default: 1): ', (answer) => {
1787
- const choice = answer.trim() || '1';
1788
- if (choice === '1')
1789
- resolve('manual');
1790
- else if (choice === '2')
1791
- resolve('traditional');
1792
- else if (choice === '3')
1793
- resolve('browser');
1794
- else
1795
- resolve('manual');
1796
- });
1686
+ const apiKey = yield clack.text({
1687
+ message: 'Enter your HumanBehavior API key:',
1688
+ placeholder: 'hb_...',
1689
+ validate: (value) => {
1690
+ if (!value)
1691
+ return 'API key is required';
1692
+ if (!value.startsWith('hb_'))
1693
+ return 'API key should start with "hb_"';
1694
+ return undefined;
1695
+ }
1797
1696
  });
1697
+ return apiKey;
1798
1698
  });
1799
1699
  }
1800
- confirmInstallation(projectPath, installationMode) {
1700
+ confirmInstallation(projectPath, framework) {
1801
1701
  return __awaiter(this, void 0, void 0, function* () {
1802
- console.log(`📁 Project path: ${projectPath}`);
1803
- console.log(`🤖 Installation mode: ${this.getInstallationModeDisplay(installationMode)}`);
1804
- console.log('⚠️ This will modify your codebase to integrate HumanBehavior SDK.');
1805
- console.log(' The following changes will be made:');
1806
- console.log(' - Install humanbehavior-js package');
1807
- console.log(' - Analyze code patterns with AI (if enabled)');
1808
- console.log(' - Modify your main app file');
1809
- console.log(' - Create environment files');
1810
- console.log(' - Add AI-optimized SDK initialization code\n');
1811
- return new Promise((resolve) => {
1812
- this.rl.question('Continue with installation? (y/n): ', (answer) => {
1813
- resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes');
1814
- });
1702
+ const confirmed = yield clack.confirm({
1703
+ message: `Ready to install HumanBehavior SDK in ${projectPath} for ${framework}?`
1815
1704
  });
1705
+ return confirmed;
1816
1706
  });
1817
1707
  }
1818
- getInstallationModeDisplay(mode) {
1819
- if (mode === 'manual')
1820
- return 'Manual AI-Enhanced';
1821
- if (mode === 'traditional')
1822
- return 'Traditional';
1823
- if (mode.startsWith('manual:')) {
1824
- const framework = mode.split(':')[1];
1825
- return `Manual AI-Enhanced (${framework})`;
1826
- }
1827
- if (mode === 'browser')
1828
- return 'Browser';
1829
- return 'Unknown';
1830
- }
1831
1708
  chooseFramework() {
1832
1709
  return __awaiter(this, void 0, void 0, function* () {
1833
- console.log('\n🎯 Choose your framework:');
1834
- console.log('1. Vanilla JS (HTML)');
1835
- console.log('2. React');
1836
- console.log('3. Next.js');
1837
- console.log('4. Vue');
1838
- console.log('5. Nuxt');
1839
- console.log('6. Angular');
1840
- console.log('7. Svelte');
1841
- console.log('8. Remix');
1842
- console.log('9. Node.js');
1843
- console.log('10. Other (Auto-detect)\n');
1844
- return new Promise((resolve) => {
1845
- this.rl.question('Select framework (1-10, default: 1): ', (answer) => {
1846
- const choice = answer.trim() || '1';
1847
- const frameworks = ['vanilla', 'react', 'nextjs', 'vue', 'nuxt', 'angular', 'svelte', 'remix', 'node', 'auto'];
1848
- const index = parseInt(choice) - 1;
1849
- resolve(frameworks[index] || 'vanilla');
1850
- });
1710
+ const framework = yield clack.select({
1711
+ message: 'Select your framework:',
1712
+ options: [
1713
+ { label: 'React', value: 'react' },
1714
+ { label: 'Next.js', value: 'nextjs' },
1715
+ { label: 'Vue', value: 'vue' },
1716
+ { label: 'Angular', value: 'angular' },
1717
+ { label: 'Svelte', value: 'svelte' },
1718
+ { label: 'Nuxt.js', value: 'nuxt' },
1719
+ { label: 'Remix', value: 'remix' },
1720
+ { label: 'Astro', value: 'astro' },
1721
+ { label: 'Gatsby', value: 'gatsby' },
1722
+ { label: 'Vanilla JS/TS', value: 'vanilla' }
1723
+ ]
1851
1724
  });
1725
+ return framework;
1852
1726
  });
1853
1727
  }
1854
- displayResults(result, installationMode) {
1728
+ displayResults(result, framework) {
1855
1729
  if (result.success) {
1856
- console.log('\n✅ Installation completed successfully!');
1857
- if (installationMode === 'ai' && result.aiAnalysis) {
1858
- console.log(`🎯 AI Analysis Results:`);
1859
- console.log(` Framework: ${result.aiAnalysis.framework.name} (confidence: ${Math.round(result.aiAnalysis.confidence * 100)}%)`);
1860
- console.log(` Integration Strategy: ${result.aiAnalysis.integrationStrategy}`);
1861
- console.log(` Compatibility Mode: ${result.aiAnalysis.compatibilityMode}`);
1862
- if (result.aiAnalysis.patterns.length > 0) {
1863
- console.log(` Detected Patterns: ${result.aiAnalysis.patterns.length} patterns`);
1730
+ clack.outro('🎉 Installation completed successfully!');
1731
+ // Display framework info
1732
+ clack.note(`Framework detected: ${result.framework.name} (${result.framework.type})`, 'Framework Info');
1733
+ // Display modifications
1734
+ if (result.modifications && result.modifications.length > 0) {
1735
+ const modifications = result.modifications.map((mod) => `${mod.action}: ${mod.filePath} - ${mod.description}`);
1736
+ clack.note(modifications.join('\n'), 'Files Modified');
1737
+ }
1738
+ // Display next steps
1739
+ if (result.nextSteps && result.nextSteps.length > 0) {
1740
+ clack.note(result.nextSteps.join('\n'), 'Next Steps');
1741
+ }
1742
+ // Display AI insights if available
1743
+ if (result.aiAnalysis) {
1744
+ clack.note(`Confidence: ${Math.round(result.aiAnalysis.confidence * 100)}%`, 'AI Analysis');
1745
+ if (result.aiAnalysis.recommendations && result.aiAnalysis.recommendations.length > 0) {
1746
+ clack.note(result.aiAnalysis.recommendations.join('\n'), 'AI Recommendations');
1864
1747
  }
1865
- if (result.aiAnalysis.recommendations.length > 0) {
1866
- console.log(` AI Recommendations:`);
1867
- result.aiAnalysis.recommendations.forEach((rec) => {
1868
- console.log(` • ${rec}`);
1869
- });
1870
- }
1871
- }
1872
- else {
1873
- console.log(`📦 Framework detected: ${result.framework.name}`);
1874
- }
1875
- if (result.framework.bundler) {
1876
- console.log(`🔧 Bundler: ${result.framework.bundler}`);
1877
1748
  }
1878
- if (result.framework.packageManager) {
1879
- console.log(`📋 Package Manager: ${result.framework.packageManager}`);
1880
- }
1881
- console.log('\n📝 Changes made:');
1882
- result.modifications.forEach((mod) => {
1883
- console.log(` ${mod.action === 'create' ? '➕' : '✏️'} ${mod.description}`);
1884
- console.log(` ${mod.filePath}`);
1885
- });
1886
- console.log('\n🎯 Next steps:');
1887
- result.nextSteps.forEach((step) => {
1888
- console.log(` ${step}`);
1889
- });
1890
- if (installationMode === 'ai' && result.learningData) {
1891
- console.log('\n🧠 Learning Data:');
1892
- console.log(` Framework: ${result.learningData.framework}`);
1893
- console.log(` Patterns: ${result.learningData.patterns.length} detected`);
1894
- console.log(` Success: ${result.learningData.success ? 'Yes' : 'No'}`);
1895
- }
1896
- console.log('\n🚀 Your app is now ready to track user behavior!');
1897
- console.log('📊 View sessions in your HumanBehavior dashboard');
1898
1749
  }
1899
1750
  else {
1900
- console.log('\n❌ Installation failed:');
1901
- result.errors.forEach((error) => {
1902
- console.log(` ${error}`);
1903
- });
1904
- console.log('\n💡 Try running with --help for more options');
1751
+ clack.cancel('Installation failed');
1752
+ if (result.errors && result.errors.length > 0) {
1753
+ clack.note(result.errors.join('\n'), 'Errors');
1754
+ }
1905
1755
  }
1906
1756
  }
1907
1757
  }
1908
- // CLI argument parsing
1909
1758
  function parseArgs() {
1910
1759
  const args = process.argv.slice(2);
1911
1760
  const options = {};
1912
1761
  for (let i = 0; i < args.length; i++) {
1913
1762
  const arg = args[i];
1914
- if (arg === '--yes' || arg === '-y') {
1915
- options.yes = true;
1916
- }
1917
- else if (arg === '--dry-run' || arg === '-d') {
1918
- options.dryRun = true;
1919
- }
1920
- else if (arg === '--project' || arg === '-p') {
1921
- options.projectPath = args[i + 1];
1922
- i++;
1923
- }
1924
- else if (arg === '--traditional' || arg === '-t') {
1925
- options.useAI = false;
1926
- }
1927
- else if (arg === '--browser' || arg === '-b') {
1928
- options.browser = true;
1929
- }
1930
- else if (arg === '--manual' || arg === '-m') {
1931
- options.manual = true;
1932
- }
1933
- else if (arg === '--framework' || arg === '-f') {
1934
- options.framework = args[i + 1];
1935
- i++;
1936
- }
1937
- else if (arg === '--help' || arg === '-h') {
1938
- showHelp();
1939
- process.exit(0);
1940
- }
1941
- else if (!options.apiKey) {
1942
- options.apiKey = arg;
1763
+ switch (arg) {
1764
+ case '--help':
1765
+ case '-h':
1766
+ showHelp();
1767
+ process.exit(0);
1768
+ break;
1769
+ case '--yes':
1770
+ case '-y':
1771
+ options.yes = true;
1772
+ break;
1773
+ case '--dry-run':
1774
+ options.dryRun = true;
1775
+ break;
1776
+ case '--project':
1777
+ case '-p':
1778
+ options.projectPath = args[++i];
1779
+ break;
1780
+ case '--framework':
1781
+ case '-f':
1782
+ options.framework = args[++i];
1783
+ break;
1784
+ default:
1785
+ if (!options.apiKey && !arg.startsWith('-')) {
1786
+ options.apiKey = arg;
1787
+ }
1788
+ break;
1943
1789
  }
1944
1790
  }
1945
1791
  return options;
1946
1792
  }
1947
1793
  function showHelp() {
1948
1794
  console.log(`
1949
- AI-Enhanced HumanBehavior SDK Auto-Installation CLI
1795
+ 🤖 HumanBehavior SDK AI Auto-Installation
1950
1796
 
1951
1797
  Usage: npx humanbehavior-js ai-auto-install [api-key] [options]
1952
1798
 
1953
- This tool uses AI to intelligently detect frameworks, analyze code patterns,
1954
- and generate optimal integration code that's both future-proof and backward-compatible.
1955
-
1956
- Arguments:
1957
- api-key Your HumanBehavior API key
1958
-
1959
1799
  Options:
1960
- -y, --yes Skip all prompts and use defaults
1961
- -d, --dry-run Show what would be changed without making changes
1962
- -p, --project <path> Project directory (default: current directory)
1963
- -t, --traditional Force traditional installation mode
1964
- -b, --browser Browser-based AI installation
1965
- -m, --manual Manual framework selection
1966
- -f, --framework <f> Specify framework for manual mode
1967
- -h, --help Show this help message
1800
+ -h, --help Show this help message
1801
+ -y, --yes Skip all prompts and use defaults
1802
+ --dry-run Show what would be changed without making changes
1968
1803
 
1969
- Installation Modes:
1970
- 1. Manual AI-Enhanced (default): Choose your framework and use AI for optimization
1971
- 2. Traditional: Standard framework detection and installation
1972
- 3. Browser: Browser-based AI detection and code generation
1804
+ -p, --project <path> Specify project directory
1805
+ -f, --framework <name> Specify framework manually
1973
1806
 
1974
1807
  Examples:
1975
- npx humanbehavior-js ai-auto-install your-api-key
1976
- npx humanbehavior-js ai-auto-install your-api-key --manual --framework react
1977
- npx humanbehavior-js ai-auto-install your-api-key --traditional
1978
- npx humanbehavior-js ai-auto-install your-api-key --browser
1979
-
1980
- Supported Frameworks:
1981
- ✅ React (CRA, Vite, Webpack)
1982
- ✅ Next.js (App Router, Pages Router)
1983
- ✅ Vue (Vue CLI, Vite)
1984
- ✅ Angular
1985
- ✅ Svelte (SvelteKit, Vite)
1986
- ✅ Remix
1987
- ✅ Vanilla JS/TS
1988
- ✅ Node.js (CommonJS & ESM)
1989
-
1990
- AI Features:
1991
- 🔍 Intelligent framework detection beyond package.json
1992
- 📊 Code pattern analysis and optimization
1993
- 🔄 Future-proof integration strategies
1994
- 🔙 Backward compatibility with legacy frameworks
1995
- 🧠 Learning from installation patterns
1996
- ⚡ Adaptive code generation for new frameworks
1997
- 🔧 Smart conflict resolution
1998
- 📈 Performance optimization recommendations
1999
-
2000
- The AI-enhanced tool will:
2001
- 1. 🔍 Analyze your codebase with AI to detect patterns
2002
- 2. 🎯 Determine optimal integration strategy
2003
- 3. 📦 Install the humanbehavior-js package
2004
- 4. ✏️ Generate AI-optimized integration code
2005
- 5. 🔧 Apply modifications with conflict resolution
2006
- 6. 🧠 Learn from the installation for future improvements
2007
- 7. 🚀 Make your app ready to track user behavior
1808
+ npx humanbehavior-js ai-auto-install
1809
+ npx humanbehavior-js ai-auto-install hb_your_api_key_here
1810
+ npx humanbehavior-js ai-auto-install --project ./my-app --ai
1811
+ npx humanbehavior-js ai-auto-install --framework react --yes
2008
1812
  `);
2009
1813
  }
2010
1814
  // Main execution
2011
- const options = parseArgs();
2012
- // Check if we have enough arguments (api-key is required)
2013
- if (process.argv.length < 3 || process.argv.includes('--help') || process.argv.includes('-h')) {
2014
- showHelp();
2015
- process.exit(0);
1815
+ if (import.meta.url === `file://${process.argv[1]}`) {
1816
+ const options = parseArgs();
1817
+ const cli = new AIAutoInstallCLI(options);
1818
+ cli.run().catch((error) => {
1819
+ clack.cancel(`Unexpected error: ${error.message}`);
1820
+ process.exit(1);
1821
+ });
2016
1822
  }
2017
- const cli = new AIAutoInstallCLI(options);
2018
- cli.run().catch((error) => {
2019
- console.error('❌ Fatal error:', error);
2020
- process.exit(1);
2021
- });
2022
1823
 
2023
1824
  export { AIAutoInstallCLI };
2024
1825
  //# sourceMappingURL=ai-auto-install.js.map