-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest-ds-cdp.mjs
More file actions
155 lines (135 loc) · 5.86 KB
/
Copy pathtest-ds-cdp.mjs
File metadata and controls
155 lines (135 loc) · 5.86 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
import WebSocket from 'ws';
async function cdpSend(ws, method, params) {
const id = Math.random().toString(36).slice(2);
return new Promise((resolve, reject) => {
const handler = (data) => {
const msg = JSON.parse(data.toString());
if (msg.id === id) {
ws.removeListener('message', handler);
resolve(msg.result || msg.error);
}
};
ws.on('message', handler);
ws.send(JSON.stringify({ id, method, params }));
setTimeout(() => { ws.removeListener('message', handler); reject(new Error('timeout')); }, 15000);
});
}
async function main() {
const res = await fetch('http://127.0.0.1:9222/json/version');
const info = await res.json();
console.log('Browser:', info.Browser);
const ws = new WebSocket(info.webSocketDebuggerUrl);
await new Promise((r) => ws.on('open', r));
// Find existing DeepSeek page or navigate
const { targetInfos } = await cdpSend(ws, 'Target.getTargets');
let dsTarget = targetInfos.find(t => t.url && t.url.includes('deepseek') && t.type === 'page');
let targetId;
if (dsTarget) {
targetId = dsTarget.targetId;
console.log('Found existing DeepSeek page:', dsTarget.url);
} else {
const { targetId: newId } = await cdpSend(ws, 'Target.createTarget', { url: 'https://chat.deepseek.com/chat' });
targetId = newId;
console.log('Created new page, waiting for load...');
await new Promise(r => setTimeout(r, 8000));
}
// Attach to the page
const pageWs = new WebSocket(info.webSocketDebuggerUrl.replace(/(\/ws\/).*/, '$1') + (await cdpSend(ws, 'Target.attachToTarget', { targetId, flatten: true })).sessionId);
await new Promise((r) => pageWs.on('open', r));
await cdpSend(pageWs, 'Runtime.enable');
await cdpSend(pageWs, 'Page.enable');
// Check current URL
const urlEval = await cdpSend(pageWs, 'Runtime.evaluate', { expression: 'location.href' });
console.log('Current URL:', urlEval.result?.value);
if (urlEval.result?.value?.includes('sign_in')) {
console.log('ERROR: On login page — session expired');
ws.close();
pageWs.close();
process.exit(1);
}
// Extract token
console.log('\n--- Extracting token ---');
const tokenEval = await cdpSend(pageWs, 'Runtime.evaluate', {
expression: `(() => { const raw = localStorage.getItem('userToken'); if (!raw) return JSON.stringify({error:'no token'}); try { const p = JSON.parse(raw); return p.value || p.token || raw; } catch { return raw; } })()`
});
const token = tokenEval.result?.value;
if (!token || token.length < 20) {
console.log('ERROR: No valid token. Result:', JSON.stringify(tokenEval.result));
process.exit(1);
}
console.log('Token:', token.slice(0, 50) + '...');
// Test browser chat
console.log('\n--- Testing chat via browser fetch ---');
const chatEval = await cdpSend(pageWs, 'Runtime.evaluate', {
expression: `(async () => {
const token = ${JSON.stringify(token)};
const base = 'https://chat.deepseek.com';
const results = {};
// Step 1: PoW challenge
const p1 = await fetch(base + '/api/v0/chat/create_pow_challenge', {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
body: JSON.stringify({ target_path: '/api/v0/chat/completion' }),
credentials: 'include',
});
results.powStatus = p1.status;
if (!p1.ok) { results.powBody = (await p1.text()).slice(0, 300); return JSON.stringify(results); }
const powData = await p1.json();
results.powChallenge = powData.data?.biz_data?.challenge || powData.data;
// Step 2: Create session
const p2 = await fetch(base + '/api/v0/chat_session/create', {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
body: '{}',
credentials: 'include',
});
results.sessionStatus = p2.status;
if (!p2.ok) { results.sessionBody = (await p2.text()).slice(0, 300); return JSON.stringify(results); }
const sData = await p2.json();
results.sessionId = sData.data?.biz_data?.chat_session?.id || sData.data?.chat_session?.id || sData.id;
// Step 3: Send chat
const p3 = await fetch(base + '/api/v0/chat/completion', {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_session_id: results.sessionId,
parent_message_id: null,
prompt: 'User: Say hello in exactly 5 words.',
ref_file_ids: [], thinking_enabled: false, search_enabled: false, action: null, preempt: false,
}),
credentials: 'include',
});
results.chatStatus = p3.status;
if (!p3.ok) { results.chatBody = (await p3.text()).slice(0, 500); return JSON.stringify(results); }
const text = await p3.text();
results.responseLen = text.length;
results.responsePreview = text.slice(0, 300);
// Parse content
let content = '';
for (const line of text.split('\\n')) {
if (!line.startsWith('data: ')) continue;
const d = line.slice(6);
if (d === '[DONE]') continue;
try {
const p = JSON.parse(d);
if (typeof p.v === 'string') content += p.v;
else if (p.v?.response?.fragments) {
for (const f of p.v.response.fragments) if (f.content) content += f.content;
}
} catch {}
}
results.content = content;
return JSON.stringify(results);
})()`,
awaitPromise: true,
timeout: 45000,
});
console.log('\nChat Result:', chatEval.result?.value);
// Save token
const fs = await import('fs');
fs.writeFileSync('.auth/deepseek-token.txt', token);
console.log('\nToken saved to .auth/deepseek-token.txt');
ws.close();
pageWs.close();
}
main().catch(e => { console.error('FATAL:', e.message); process.exit(1); });