@dropout-ai/runtime 0.1.0 → 0.2.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +91 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dropout-ai/runtime",
3
- "version": "0.1.0",
3
+ "version": "0.2.01",
4
4
  "description": "Invisible Node.js runtime for capturing AI interactions.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -1,11 +1,24 @@
1
+ const originalFetch = global.fetch;
2
+
3
+ // 4. Internal telemetry sender (Neutral naming)
4
+ async function safeSendTelemetry(payload) {
5
+ try {
6
+ await originalFetch('http://localhost:4000/capture', {
7
+ method: 'POST',
8
+ headers: { 'Content-Type': 'application/json' },
9
+ body: JSON.stringify(payload)
10
+ });
11
+ } catch (e) {
12
+ // Fail silently
13
+ }
14
+ }
15
+
1
16
  (function () {
2
17
  // 1. Guard global.fetch existence and idempotency
3
18
  if (typeof global.fetch !== 'function' || global.fetch.__dropout_patched__) {
4
19
  return;
5
20
  }
6
21
 
7
- const originalFetch = global.fetch;
8
-
9
22
  // 2. Default Configuration
10
23
  let config = {
11
24
  version: 1,
@@ -27,30 +40,50 @@
27
40
  }
28
41
  }, 0);
29
42
 
30
- // 4. Internal telemetry sender (Neutral naming)
31
- async function safeSendTelemetry(payload) {
43
+
44
+ // 5. Detection Helpers
45
+ function matchesKnownProvider(url) {
46
+ return (
47
+ url &&
48
+ (
49
+ url.includes('openai.com/v1') ||
50
+ url.includes('api.anthropic.com') ||
51
+ url.includes('generativelanguage.googleapis.com') ||
52
+ url.includes('api-inference.huggingface.co')
53
+ )
54
+ );
55
+ }
56
+
57
+ function matchesGenericLLMPayload(init) {
32
58
  try {
33
- await originalFetch('http://localhost:4000/capture', {
34
- method: 'POST',
35
- headers: { 'Content-Type': 'application/json' },
36
- body: JSON.stringify(payload)
37
- });
59
+ if (!init || typeof init.body !== 'string') return false;
60
+
61
+ const body = JSON.parse(init.body);
62
+
63
+ // Common LLM request shapes
64
+ return (
65
+ body.messages ||
66
+ body.prompt ||
67
+ body.input
68
+ );
38
69
  } catch (e) {
39
- // Fail silently
70
+ return false;
40
71
  }
41
72
  }
42
73
 
43
- // 5. The Monkey Patch
74
+ // 6. The Monkey Patch
44
75
  global.fetch = async function (input, init) {
45
76
  const start = Date.now();
46
77
  const response = await originalFetch(input, init);
47
78
 
48
79
  try {
49
- // 6. Detection Logic (Dumb detection + Config Check)
80
+ // 7. Detection Logic (Dumb detection + Config Check)
50
81
  const url = typeof input === 'string' ? input : (input && input.url);
51
- const isAIProvider = config.enabledProviders.some(p => url && url.includes(`${p}.com/v1`));
82
+ const isLikelyAI =
83
+ matchesKnownProvider(url) ||
84
+ matchesGenericLLMPayload(init);
52
85
 
53
- if (url && isAIProvider) {
86
+ if (url && isLikelyAI) {
54
87
  const latency = Date.now() - start;
55
88
  const contentType = response.headers.get('content-type') || '';
56
89
  const isStream = contentType.includes('text/event-stream');
@@ -80,7 +113,8 @@
80
113
  }
81
114
 
82
115
  const payload = {
83
- provider: 'openai',
116
+ provider: url.includes('openai.com') ? 'openai' : 'unknown',
117
+ confidence: matchesKnownProvider(url) ? 'high' : 'heuristic',
84
118
  url,
85
119
  model,
86
120
  prompt,
@@ -99,3 +133,45 @@
99
133
 
100
134
  global.fetch.__dropout_patched__ = true;
101
135
  })();
136
+
137
+ // 11. Manual capture (combo mode)
138
+ async function capture(target, options = {}) {
139
+ const start = Date.now();
140
+
141
+ try {
142
+ const result =
143
+ typeof target === 'function'
144
+ ? await target()
145
+ : await Promise.resolve(target);
146
+
147
+ const payload = {
148
+ provider: options.provider || 'manual',
149
+ model: options.model,
150
+ prompt: options.prompt,
151
+ output: typeof result === 'string'
152
+ ? result
153
+ : JSON.stringify(result).slice(0, 20000),
154
+ latency_ms: Date.now() - start,
155
+ timestamp: Math.floor(Date.now() / 1000),
156
+ mode: 'manual'
157
+ };
158
+
159
+ setTimeout(() => safeSendTelemetry(payload), 0);
160
+ return result;
161
+ } catch (error) {
162
+ const payload = {
163
+ provider: options.provider || 'manual',
164
+ error: error?.message || String(error),
165
+ latency_ms: Date.now() - start,
166
+ timestamp: Math.floor(Date.now() / 1000),
167
+ mode: 'manual'
168
+ };
169
+
170
+ setTimeout(() => safeSendTelemetry(payload), 0);
171
+ throw error;
172
+ }
173
+ }
174
+
175
+ module.exports = {
176
+ capture
177
+ };