@dropout-ai/runtime 0.3.10 → 0.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dropout-ai/runtime",
3
- "version": "0.3.10",
3
+ "version": "0.4.0",
4
4
  "description": "Invisible Node.js runtime for understanding behavoiral impact of AI interactions.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -30,6 +30,13 @@
30
30
  "url": "git+https://github.com/vaibhavv4re/dropout.git",
31
31
  "directory": "packages/runtime"
32
32
  },
33
+ "dependencies": {
34
+ "dotenv": "^16.4.5"
35
+ },
36
+ "exports": {
37
+ ".": "./src/index.js",
38
+ "./register": "./src/autoload.js"
39
+ },
33
40
  "files": [
34
41
  "src",
35
42
  "bin"
@@ -0,0 +1,35 @@
1
+ // packages/runtime/src/autoload.js
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const Dropout = require('./index');
5
+
6
+ // 1. Force Load Environment Variables (Fixes the "undefined" error)
7
+ try {
8
+ const dotenv = require('dotenv');
9
+ const cwd = process.cwd();
10
+
11
+ // We explicitly look for .env.local first (Next.js standard), then .env
12
+ const envFiles = ['.env.local', '.env'];
13
+
14
+ envFiles.forEach(file => {
15
+ const envPath = path.resolve(cwd, file);
16
+ if (fs.existsSync(envPath)) {
17
+ dotenv.config({ path: envPath, override: true });
18
+ }
19
+ });
20
+ } catch (e) {
21
+ // dotenv might not be installed, ignore if using system env vars
22
+ }
23
+
24
+ // 2. Initialize Immediately
25
+ if (process.env.DROPOUT_PROJECT_ID && process.env.DROPOUT_API_KEY) {
26
+ new Dropout({
27
+ projectId: process.env.DROPOUT_PROJECT_ID,
28
+ apiKey: process.env.DROPOUT_API_KEY,
29
+ debug: process.env.DROPOUT_DEBUG === 'true'
30
+ });
31
+ // Log strictly for debug verification
32
+ if (process.env.DROPOUT_DEBUG === 'true') {
33
+ console.log(`[Dropout] 🚀 Global Autoload Active: ${process.env.DROPOUT_PROJECT_ID}`);
34
+ }
35
+ }
package/src/index.js CHANGED
@@ -17,7 +17,27 @@ const KNOWN_AI_DOMAINS = [
17
17
  ];
18
18
 
19
19
  class Dropout {
20
+ // Singleton Instance Storage
21
+ static instance = null;
22
+
23
+ /**
24
+ * Universal Initialization Method (Idempotent)
25
+ * Users call this explicitly in their code.
26
+ */
27
+ static init(config) {
28
+ if (!Dropout.instance) {
29
+ new Dropout(config);
30
+ }
31
+ return Dropout.instance;
32
+ }
33
+
20
34
  constructor(config = {}) {
35
+ // đŸ›Ąī¸ SINGLETON GUARD (Prevent multiple initializations)
36
+ if (Dropout.instance) {
37
+ if (config.debug) console.log("[Dropout] â„šī¸ Already initialized. Returning existing instance.");
38
+ return Dropout.instance;
39
+ }
40
+
21
41
  // đŸ›Ąī¸ 1. BROWSER GUARD (Client-Side Protection)
22
42
  if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
23
43
  if (config.debug) console.warn("[Dropout] âš ī¸ Skipped: Browser detected. SDK is Server-Only.");
@@ -26,7 +46,9 @@ class Dropout {
26
46
 
27
47
  // đŸ›Ąī¸ 2. CREDENTIAL GUARD
28
48
  if (!config.apiKey || !config.projectId) {
29
- if (config.debug) console.warn("[Dropout] âš ī¸ Skipped: Missing API Key or Project ID.");
49
+ if (config.debug) {
50
+ console.warn("\x1b[31m%s\x1b[0m", "[Dropout] 🔴 Initialization Failed: Missing API Key or Project ID.");
51
+ }
30
52
  return;
31
53
  }
32
54
 
@@ -56,9 +78,10 @@ class Dropout {
56
78
  this.lastPromptHash = null;
57
79
  this.lastResponseHash = null;
58
80
 
59
- // Singleton Guard
81
+ // Global Guard (Extra safety for HMR environments like Next.js)
60
82
  if (global.__dropout_initialized__) {
61
- if (this.debug) console.log("[Dropout] â„šī¸ Already initialized.");
83
+ if (this.debug) console.log("[Dropout] â„šī¸ Global flag found. Skipping re-initialization.");
84
+ Dropout.instance = this; // Re-bind if lost
62
85
  return;
63
86
  }
64
87
  global.__dropout_initialized__ = true;
@@ -68,7 +91,10 @@ class Dropout {
68
91
  global.__dropout_session_id__ = this.generateSessionId();
69
92
  }
70
93
 
71
- if (this.debug) console.log(`[Dropout] đŸŸĸ Online: ${this.projectId}`);
94
+ // ✅ VISUAL SUCCESS INDICATOR
95
+ if (this.debug) {
96
+ console.log('\x1b[32m%s\x1b[0m', `[Dropout] đŸŸĸ Online | Project: ${this.projectId}`);
97
+ }
72
98
 
73
99
  // Bindings
74
100
  this.log = this.log.bind(this);
@@ -77,10 +103,12 @@ class Dropout {
77
103
  // 4. Start Network Interceptor
78
104
  this.patchNetwork();
79
105
 
80
- // 5. SEND STARTUP PING (Always run this, silent by default)
81
- // This turns the dashboard status GREEN immediately on app boot.
106
+ // 5. SEND STARTUP PING
82
107
  this.sendStartupSignal();
83
108
 
109
+ // Lock the instance
110
+ Dropout.instance = this;
111
+
84
112
  } catch (err) {
85
113
  // THE ULTIMATE CATCH-ALL: Ensure app NEVER crashes
86
114
  if (config.debug) console.error("[Dropout] ❌ Initialization Error:", err);
@@ -108,7 +136,6 @@ class Dropout {
108
136
  turn_role: 'system',
109
137
  turn_index: 0,
110
138
  content: 'Dropout SDK Initialized',
111
- // content_blob: 'Dropout SDK Initialized',
112
139
  metadata_flags: {
113
140
  type: 'system_boot',
114
141
  environment: process.env.NODE_ENV || 'development',
@@ -185,7 +212,6 @@ class Dropout {
185
212
  model: payload.model,
186
213
  latency_ms: payload.latency_ms || null,
187
214
  content: payload.content,
188
- // content_blob: payload.content,
189
215
  content_hash: payload.content_hash,
190
216
  metadata_flags: payload.metadata_flags,
191
217
  received_at: new Date().toISOString()
@@ -409,9 +435,9 @@ class Dropout {
409
435
  }
410
436
  }
411
437
 
412
- // Auto-Start (Server-Side Only)
438
+ // Auto-Start (Backwards Compatibility & Environment Variable Support)
413
439
  if (typeof process !== 'undefined' && process.env.DROPOUT_PROJECT_ID && process.env.DROPOUT_API_KEY) {
414
- new Dropout({
440
+ Dropout.init({
415
441
  projectId: process.env.DROPOUT_PROJECT_ID,
416
442
  apiKey: process.env.DROPOUT_API_KEY,
417
443
  debug: process.env.DROPOUT_DEBUG === 'true'