-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcapture-ai-live.ts
More file actions
258 lines (218 loc) · 8.17 KB
/
capture-ai-live.ts
File metadata and controls
258 lines (218 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* Live capture of Ask AI requests by injecting a fetch interceptor
* and monitoring Network events simultaneously
*/
import WebSocket from "ws";
const CDP_URL = "http://localhost:9333";
interface CDPTarget {
id: string;
title: string;
url: string;
webSocketDebuggerUrl?: string;
}
async function getSuperhuman(): Promise<CDPTarget | null> {
const response = await fetch(`${CDP_URL}/json`);
const targets: CDPTarget[] = await response.json();
const superhuman = targets.find(t =>
t.url.includes("mail.superhuman.com") &&
!t.url.includes("background") &&
t.webSocketDebuggerUrl
);
return superhuman || null;
}
async function captureAILive() {
console.log("=== Live Ask AI Request Capture ===\n");
const target = await getSuperhuman();
if (!target) {
console.error("Superhuman not found. Make sure it's running with --remote-debugging-port=9333");
process.exit(1);
}
console.log(`Connected to: ${target.title}`);
console.log(`URL: ${target.url}\n`);
const ws = new WebSocket(target.webSocketDebuggerUrl!);
let msgId = 0;
const send = (method: string, params: any = {}): Promise<any> => {
return new Promise((resolve, reject) => {
const id = ++msgId;
const timeout = setTimeout(() => {
ws.off("message", handler);
reject(new Error(`Timeout waiting for response ${id}`));
}, 10000);
const handler = (data: Buffer) => {
const msg = JSON.parse(data.toString());
if (msg.id === id) {
clearTimeout(timeout);
ws.off("message", handler);
resolve(msg);
}
};
ws.on("message", handler);
ws.send(JSON.stringify({ id, method, params }));
});
};
const capturedRequests: any[] = [];
// Listen for ALL network events
ws.on("message", (data: Buffer) => {
const msg = JSON.parse(data.toString());
if (msg.method === "Network.requestWillBeSent") {
const { requestId, request } = msg.params;
const { url, method, postData } = request;
// Check for AI/askAI endpoints
if (url.includes("askAI") || url.includes("/ai.") ||
url.includes("ai-") || url.includes("/v3/ai")) {
console.log(`\n${"=".repeat(60)}`);
console.log(`[REQUEST] ${method} ${url}`);
console.log(`Request ID: ${requestId}`);
if (postData) {
try {
const body = JSON.parse(postData);
console.log("\n--- Request Body ---");
console.log(JSON.stringify(body, null, 2));
// Extract key IDs
if (body.question_event_id) {
console.log(`\n>>> question_event_id: ${body.question_event_id}`);
}
if (body.session_id) {
console.log(`>>> session_id: ${body.session_id}`);
}
if (body.agent_session_id) {
console.log(`>>> agent_session_id: ${body.agent_session_id}`);
}
capturedRequests.push({
url,
requestId,
body,
timestamp: new Date().toISOString()
});
} catch {
console.log("Raw body (not JSON):", postData.slice(0, 500));
}
}
console.log("=".repeat(60) + "\n");
}
}
if (msg.method === "Network.responseReceived") {
const { requestId, response } = msg.params;
if (response.url.includes("askAI") || response.url.includes("/ai.")) {
console.log(`[RESPONSE] ${response.status} ${response.url}`);
}
}
});
ws.on("open", async () => {
console.log("CDP connection established\n");
// Enable Network monitoring with request bodies
console.log("Enabling Network monitoring...");
await send("Network.enable", {
maxPostDataSize: 65536 // Capture up to 64KB of POST data
});
console.log("Network monitoring enabled\n");
// Also inject a fetch wrapper for redundancy
console.log("Injecting fetch interceptor...");
const injectResult = await send("Runtime.evaluate", {
expression: `
(function() {
if (window.__aiCaptureInstalled) return 'Already installed';
window.__aiCaptureInstalled = true;
window.__capturedAIRequests = [];
const originalFetch = window.fetch;
window.fetch = async function(url, options) {
const urlStr = typeof url === 'string' ? url : url.toString();
if (urlStr.includes('askAI') || urlStr.includes('/ai.') || urlStr.includes('/v3/ai')) {
console.log('[AI CAPTURE] Intercepted:', urlStr);
if (options?.body) {
try {
const body = typeof options.body === 'string'
? JSON.parse(options.body)
: options.body;
console.log('[AI CAPTURE] Body:', JSON.stringify(body, null, 2));
window.__capturedAIRequests.push({
url: urlStr,
body: body,
timestamp: new Date().toISOString()
});
} catch (e) {
console.log('[AI CAPTURE] Raw body:', options.body);
}
}
}
return originalFetch.apply(this, arguments);
};
return 'Fetch interceptor installed';
})()
`,
returnByValue: true
});
console.log("Inject result:", injectResult?.result?.result?.value);
console.log("\n" + "=".repeat(60));
console.log("MONITORING FOR ASK AI REQUESTS");
console.log("Press J in Superhuman to open Ask AI and ask a question");
console.log("Press Ctrl+C to stop and see summary");
console.log("=".repeat(60) + "\n");
// Poll for captured requests from the injected interceptor
setInterval(async () => {
try {
const result = await send("Runtime.evaluate", {
expression: "JSON.stringify(window.__capturedAIRequests || [])",
returnByValue: true
});
const requests = JSON.parse(result?.result?.result?.value || "[]");
if (requests.length > capturedRequests.length) {
console.log(`\n[INJECTED CAPTURE] ${requests.length} total requests captured`);
}
} catch {}
}, 5000);
});
// Handle Ctrl+C
process.on("SIGINT", async () => {
console.log("\n\n" + "=".repeat(60));
console.log("CAPTURE SUMMARY");
console.log("=".repeat(60) + "\n");
// Get final state from injected interceptor
try {
const result = await send("Runtime.evaluate", {
expression: "JSON.stringify(window.__capturedAIRequests || [])",
returnByValue: true
});
const injectedRequests = JSON.parse(result?.result?.result?.value || "[]");
if (injectedRequests.length > 0) {
console.log(`From injected interceptor: ${injectedRequests.length} requests\n`);
for (const req of injectedRequests) {
console.log(`URL: ${req.url}`);
console.log(`Body: ${JSON.stringify(req.body, null, 2)}`);
console.log();
}
}
} catch {}
if (capturedRequests.length > 0) {
console.log(`From CDP Network: ${capturedRequests.length} requests\n`);
for (const req of capturedRequests) {
console.log(`URL: ${req.url}`);
console.log(`Timestamp: ${req.timestamp}`);
if (req.body) {
console.log(`Body:`);
console.log(JSON.stringify(req.body, null, 2));
// Highlight important IDs
if (req.body.question_event_id) {
console.log(`\n*** question_event_id: ${req.body.question_event_id}`);
console.log(` Length: ${req.body.question_event_id.length}`);
// Analyze format
if (req.body.question_event_id.startsWith("event_")) {
const suffix = req.body.question_event_id.replace("event_", "");
console.log(` Prefix: event_`);
console.log(` Suffix: ${suffix} (${suffix.length} chars)`);
}
}
}
console.log();
}
} else {
console.log("No AI requests captured from CDP Network.");
}
ws.close();
process.exit(0);
});
ws.on("error", (err) => {
console.error("WebSocket error:", err);
});
}
captureAILive().catch(console.error);