-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepterm-core.js
More file actions
402 lines (361 loc) · 10.9 KB
/
Copy pathdeepterm-core.js
File metadata and controls
402 lines (361 loc) · 10.9 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import fs from 'fs';
import { Buffer } from 'buffer';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import path from 'path';
import FormData from 'form-data';
const wasmFile = './deepseek.wasm';
const __dirname = dirname(fileURLToPath(import.meta.url));
const wasmPath = join(__dirname, wasmFile);
let wasmBytes;
try {
wasmBytes = fs.readFileSync(wasmPath);
} catch (e) {
wasmBytes = null;
}
let wasmInstance, wasmExports, memory, malloc, addToStackPointer;
async function initWasm() {
if (wasmInstance) return;
if (!wasmBytes) throw new Error(`WASM file not found at ${wasmPath}`);
try {
const { instance } = await WebAssembly.instantiate(wasmBytes, {});
wasmInstance = instance;
wasmExports = instance.exports;
memory = wasmExports.memory;
malloc = wasmExports.__wbindgen_malloc || wasmExports.__wbindgen_export_0 || wasmExports.malloc;
addToStackPointer = wasmExports.__wbindgen_add_to_stack_pointer || wasmExports.add_to_stack_pointer;
if (typeof malloc !== 'function') {
throw new Error('WASM allocator (malloc) not found on exports');
}
if (typeof addToStackPointer !== 'function') {
addToStackPointer = null;
}
} catch (e) {
throw new Error(`Failed to initialize WASM: ${e.message || e}`);
}
}
function alloc_utf8(str) {
if (typeof TextEncoder === 'undefined') {
throw new Error('TextEncoder not available');
}
const encoder = new TextEncoder();
const bytes = encoder.encode(str);
const size = bytes.length;
const ptr = malloc(size, 1);
if (!memory || !memory.buffer) throw new Error('WASM memory unavailable');
const view = new Uint8Array(memory.buffer, ptr, size);
view.set(bytes);
return [ptr, size];
}
export async function solvePow(challenge, salt, expireAt, difficulty) {
await initWasm();
const prefix = `${salt}_${expireAt}_`;
const [challengePtr, challengeLen] = alloc_utf8(challenge);
const [prefixPtr, prefixLen] = alloc_utf8(prefix);
let stackPtr = 0;
let usedStackPointer = false;
if (addToStackPointer) {
stackPtr = addToStackPointer(-16);
usedStackPointer = true;
} else {
stackPtr = malloc(16, 1);
}
if (typeof wasmExports.wasm_solve !== 'function') {
throw new Error('wasm_solve not found in wasm exports');
}
wasmExports.wasm_solve(
stackPtr,
challengePtr,
challengeLen,
prefixPtr,
prefixLen,
difficulty
);
const view = new DataView(memory.buffer, stackPtr, 16);
const found = view.getInt32(0, true);
const answer = view.getFloat64(8, true);
if (found === 0) {
throw new Error('POW not found');
}
return Math.floor(answer);
}
function isJsonString(str) {
if (typeof str !== 'string') return false;
try {
const parsed = JSON.parse(str);
return typeof parsed === 'object' && parsed !== null;
} catch (e) {
return false;
}
}
function headers(TOKEN) {
const baseHeaders = {
accept: '*/*',
'accept-language': 'en-US,en;q=0.9',
authorization: `Bearer ${TOKEN}`,
priority: 'u=1, i',
'sec-ch-ua': '"Not)A;Brand";v="8", "Chromium";v="138", "Brave";v="138"',
'sec-ch-ua-arch': '"x86"',
'sec-ch-ua-bitness': '"64"',
'sec-ch-ua-full-version-list': '"Not)A;Brand";v="8.0.0.0", "Chromium";v="138.0.0.0", "Brave";v="138.0.0.0"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-model': '""',
'sec-ch-ua-platform': '"Windows"',
'sec-ch-ua-platform-version': '"19.0.0"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'sec-gpc': '1',
'x-app-version': '20241129.1',
'x-client-locale': 'en_US',
'x-client-platform': 'web',
'x-client-version': '1.3.0-auto-resume'
};
return baseHeaders;
}
export async function getCurrentProfile(TOKEN) {
try {
const response = await fetch('https://chat.deepseek.com/api/v0/users/current', {
method: 'GET',
headers: { ...headers(TOKEN) },
referrer: 'https://chat.deepseek.com/',
mode: 'cors',
credentials: 'include'
});
if (!response.ok) return;
const json = await response.json();
return json;
} catch (e) {
return;
}
}
export async function deleteAllChatSessions(TOKEN, CHAT_SESSION_ID) {
try {
const response = await fetch('https://chat.deepseek.com/api/v0/chat_session/delete_all', {
method: 'POST',
headers: { ...headers(TOKEN) },
referrer: `https://chat.deepseek.com/a/chat/s/${CHAT_SESSION_ID}`,
mode: 'cors',
credentials: 'include'
});
if (!response.ok) return;
return await response.json();
} catch (e) {
return;
}
}
export async function getFileUrl(TOKEN, CHAT_SESSION_ID, file_id) {
try {
const response = await fetch(`https://chat.deepseek.com/api/v0/file/preview?file_id=file-${file_id}`, {
method: 'GET',
headers: { ...headers(TOKEN) },
referrer: `https://chat.deepseek.com/a/chat/s/${CHAT_SESSION_ID}`,
mode: 'cors',
credentials: 'include'
});
if (!response.ok) return;
return await response.json();
} catch (e) {
return;
}
}
export async function uploadFile(TOKEN, CHAT_SESSION_ID, filePath) {
try {
const pow = await generatePowHeader(TOKEN, CHAT_SESSION_ID, filePath);
const form = new FormData();
const fileName = path.basename(filePath);
form.append('file', fs.createReadStream(filePath), {
filename: fileName
});
const response = await fetch('https://chat.deepseek.com/api/v0/file/upload_file', {
method: 'POST',
headers: {
...form.getHeaders(),
...headers(TOKEN),
'x-ds-pow-response': pow,
'x-thinking-enabled': '0'
},
body: form,
referrer: 'https://chat.deepseek.com/',
mode: 'cors',
credentials: 'include'
});
if (!response.ok) return;
return await response.json();
} catch (e) {
return;
}
}
export async function createChatSession(TOKEN) {
try {
const response = await fetch('https://chat.deepseek.com/api/v0/chat_session/create', {
method: 'POST',
headers: { ...headers(TOKEN), 'content-type': 'application/json' },
referrer: 'https://chat.deepseek.com/',
body: JSON.stringify({ character_id: null }),
mode: 'cors',
credentials: 'include'
});
if (!response.ok) return;
return await response.json();
} catch (e) {
return;
}
}
export async function fetchAllChatSessions(TOKEN) {
try {
const response = await fetch('https://chat.deepseek.com/api/v0/chat_session/fetch_page', {
method: 'GET',
headers: { ...headers(TOKEN) },
referrer: 'https://chat.deepseek.com/',
mode: 'cors',
credentials: 'include'
});
if (!response.ok) return;
return await response.json();
} catch (e) {
return;
}
}
export async function generatePowHeader(TOKEN, CHAT_SESSION_ID, targetPath) {
try {
const res = await fetch('https://chat.deepseek.com/api/v0/chat/create_pow_challenge', {
method: 'POST',
headers: { ...headers(TOKEN), 'content-type': 'application/json' },
body: JSON.stringify({ target_path: targetPath })
});
if (!res.ok) return;
const { data } = await res.json();
const {
challenge,
salt,
expire_at,
difficulty,
signature,
algorithm,
target_path
} = data.biz_data.challenge;
const answer = await solvePow(challenge, salt, expire_at, difficulty);
const encoded = Buffer.from(JSON.stringify({
algorithm,
challenge,
salt,
answer,
signature,
target_path
})).toString('base64');
return encoded;
} catch (e) {
return;
}
}
export async function fetchHistoryMessages(TOKEN, sessionId) {
try {
const url = `https://chat.deepseek.com/api/v0/chat/history_messages?chat_session_id=${sessionId}&cache_version=-1`;
const response = await fetch(url, {
method: 'GET',
headers: { ...headers(TOKEN) },
referrer: `https://chat.deepseek.com/a/chat/s/${sessionId}`,
mode: 'cors',
credentials: 'include'
});
if (!response.ok) return;
return await response.json();
} catch (e) {
return;
}
}
export async function* completion(TOKEN, prompt, CHAT_SESSION_ID, parentMessageId, stream = true, search = true, thinking = false, file_ids = []) {
let pow;
try {
pow = await generatePowHeader(TOKEN, CHAT_SESSION_ID, '/api/v0/chat/completion');
} catch (e) {
pow = null;
}
let response;
try {
response = await fetch('https://chat.deepseek.com/api/v0/chat/completion', {
method: 'POST',
headers: {
...headers(TOKEN),
'content-type': 'application/json',
...(pow ? { 'x-ds-pow-response': pow } : {}),
referer: `https://chat.deepseek.com/a/chat/s/${CHAT_SESSION_ID}`
},
body: JSON.stringify({
chat_session_id: CHAT_SESSION_ID,
parent_message_id: parentMessageId,
prompt,
ref_file_ids: file_ids,
thinking_enabled: thinking,
search_enabled: search
})
});
} catch (e) {
if (!stream) return '';
yield '❌ Network error while requesting completion';
return;
}
if (!response || !response.ok) {
if (!stream) return '';
yield `❌ Server returned ${response ? response.status : 'no response'}`;
return;
}
const contentType = response.headers.get('content-type') || '';
if (contentType.includes('application/json')) {
try {
const json = await response.json();
const text = JSON.stringify(json);
if (stream) yield text;
else return text;
return;
} catch (e) {
if (stream) yield '❌ Failed to parse JSON response';
else return '';
return;
}
}
const reader = response.body?.getReader();
if (!reader) {
if (!stream) return '';
yield '❌ No readable stream on response';
return;
}
const decoder = new TextDecoder('utf-8');
let streamresponse = '';
while (true) {
const { value, done } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
if (chunk.includes('challenge-platform')) {
const msg = 'Detected by Cloudflare, please try again and increase the request delay';
if (stream) yield msg;
else streamresponse = msg;
continue;
}
const lines = chunk.split('\n').filter(l => l.trim().startsWith('data:'));
if (lines.length > 0) {
for (const line of lines) {
const payload = line.replace(/^data:\s*/, '').trim();
if (!payload) continue;
try {
const json = JSON.parse(payload);
if (typeof json?.v === 'string' && !['SEARCHING', 'FINISHED', 'ANSWER'].includes(json.v)) {
if (stream) {
yield json.v;
} else {
streamresponse += json.v;
}
}
} catch (e) {
if (stream) yield payload;
else streamresponse += payload;
}
}
} else {
if (stream) yield chunk;
else streamresponse += chunk;
}
}
if (!stream) return streamresponse;
}