{"version":3,"sources":["../src/auto-monitor.ts"],"sourcesContent":["/**\n * Auto-initializing global monitor\n *\n * This module automatically starts monitoring ALL outbound HTTP requests\n * for AI API calls when imported. Configure via environment variables.\n *\n * Environment Variables:\n * - COOLHAND_API_KEY (required)\n * - COOLHAND_SILENT (optional: 'true' | 'false', default: 'true')\n * - COOLHAND_PATTERNS_FILE (optional: path to custom patterns file)\n * - COOLHAND_DEBUG (optional: 'true' | 'false', default: 'false') — enables verbose logging only\n * - COOLHAND_DRY_RUN (optional: 'true' | 'false', default: 'false') — suppresses all API submissions\n * - COOLHAND_BASE_URL (optional: self-hosted endpoint, e.g. 'https://feedback.example.com')\n *\n * Usage:\n * Just import this module at the very top of your main file:\n *\n * import 'coolhand-node/auto-monitor';\n * // or\n * require('coolhand-node/auto-monitor');\n *\n * That's it! All AI API calls will now be automatically logged.\n */\n\nimport { initializeGlobalMonitoring, getGlobalStats, isGlobalMonitoringActive, initGlobalMonitoringCore, loadAndPatchNodeModulesIfNeeded } from './global-monitor.js';\n\n// Auto-initialize if environment variables are present\nconst apiKey = process.env.COOLHAND_API_KEY;\n\nif (apiKey) {\n  const silent = process.env.COOLHAND_SILENT !== 'false'; // Default to true unless explicitly false\n  const patternsFile = process.env.COOLHAND_PATTERNS_FILE;\n  const debug = process.env.COOLHAND_DEBUG === 'true'; // Default to false unless explicitly true\n  const dryRun = process.env.COOLHAND_DRY_RUN === 'true'; // Default to false unless explicitly true\n  const baseUrl = process.env.COOLHAND_BASE_URL;\n\n  if (!isGlobalMonitoringActive()) {\n    if (!silent) {\n      console.log('🔧 Auto-initializing global monitoring...');\n    }\n\n    // Synchronous initialization — in CJS builds this patches https.request immediately\n    // during module evaluation, before any sibling static imports can cache the original.\n    // In native ESM builds only fetch is patched here; loadAndPatchNodeModulesIfNeeded\n    // completes the http/https patching asynchronously below.\n    try {\n      initGlobalMonitoringCore({\n        apiKey,\n        silent,\n        patternsFile,\n        debug,\n        dryRun,\n        baseUrl\n      });\n\n      if (!silent) {\n        console.log('✅ Global monitoring initialized successfully');\n        console.log(`📊 Silent mode: ${silent ? 'ON' : 'OFF'}`);\n\n        if (dryRun) {\n          console.log('🚫 Dry run mode: ON (API calls will be skipped)');\n        }\n\n        if (debug) {\n          console.log('🔬 Debug mode: ON (verbose logging)');\n        }\n\n        if (patternsFile) {\n          console.log(`📁 Custom patterns file: ${patternsFile}`);\n        }\n      }\n    } catch (error) {\n      console.error('❌ Failed to initialize global monitoring:', (error as Error).message);\n    }\n  }\n\n  // Async tail: loads http/https modules in native ESM builds where the synchronous\n  // createRequire path is unavailable. No-op in CJS (modules loaded synchronously above).\n  loadAndPatchNodeModulesIfNeeded().catch((error: Error) => {\n    console.error('❌ Failed to complete global monitoring initialization:', error.message);\n  });\n} else {\n  // Only warn if not in production to avoid noise\n  if (process.env.NODE_ENV !== 'production') {\n    console.warn('⚠️  COOLHAND_API_KEY not found. Global monitoring not initialized.');\n    console.warn('   Set COOLHAND_API_KEY environment variable to enable monitoring.');\n  }\n}\n\n// Re-export functions for backwards compatibility\nexport { initializeGlobalMonitoring, getGlobalStats, isGlobalMonitoringActive };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwBA,4BAAgJ;AAGhJ,MAAM,SAAS,QAAQ,IAAI;AAE3B,IAAI,QAAQ;AACV,QAAM,SAAS,QAAQ,IAAI,oBAAoB;AAC/C,QAAM,eAAe,QAAQ,IAAI;AACjC,QAAM,QAAQ,QAAQ,IAAI,mBAAmB;AAC7C,QAAM,SAAS,QAAQ,IAAI,qBAAqB;AAChD,QAAM,UAAU,QAAQ,IAAI;AAE5B,MAAI,KAAC,gDAAyB,GAAG;AAC/B,QAAI,CAAC,QAAQ;AACX,cAAQ,IAAI,kDAA2C;AAAA,IACzD;AAMA,QAAI;AACF,0DAAyB;AAAA,QACvB;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AAED,U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