File size: 18,396 Bytes
f6a18fc
466f6e2
d12c6be
f6a18fc
 
 
 
 
 
 
466f6e2
 
d12c6be
 
f6a18fc
 
 
 
d12c6be
 
f6a18fc
 
 
 
 
 
 
 
 
 
 
d12c6be
 
 
f7df793
d12c6be
 
 
 
f7df793
 
 
 
 
 
 
 
 
 
d12c6be
 
 
 
 
 
 
f6a18fc
 
171bb28
 
 
 
 
 
 
 
da32f5f
d12c6be
 
 
 
da32f5f
f6a18fc
d12c6be
 
 
f6a18fc
 
fc44ec7
d12c6be
f6a18fc
 
d12c6be
fc44ec7
d12c6be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6a18fc
 
d12c6be
f6a18fc
 
 
d12c6be
f6a18fc
 
 
 
2940fed
 
 
 
 
 
d12c6be
 
 
 
2940fed
 
 
 
 
466f6e2
2940fed
 
 
 
d12c6be
 
2940fed
d12c6be
 
2940fed
f6a18fc
466f6e2
f6a18fc
4b9c49a
 
 
 
d12c6be
4b9c49a
d12c6be
4b9c49a
f6a18fc
d12c6be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6a18fc
b454740
f6a18fc
f7df793
466f6e2
f6a18fc
 
 
d12c6be
 
 
b454740
f6a18fc
f7df793
466f6e2
f6a18fc
 
 
d12c6be
 
b454740
f6a18fc
f7df793
466f6e2
f6a18fc
 
 
d12c6be
f6a18fc
 
d12c6be
f7df793
 
 
f6a18fc
 
 
d12c6be
3e65e33
 
 
 
d12c6be
 
 
3e65e33
fc44ec7
d12c6be
f6a18fc
466f6e2
f6a18fc
 
 
 
2940fed
d12c6be
 
2940fed
466f6e2
d12c6be
 
2940fed
d12c6be
 
 
 
 
 
 
 
 
 
 
 
2940fed
 
 
d12c6be
 
 
 
2940fed
 
d12c6be
2940fed
d12c6be
 
2940fed
d12c6be
2940fed
d12c6be
2940fed
d12c6be
 
 
2940fed
 
466f6e2
f6a18fc
d12c6be
f6a18fc
d12c6be
fc6d3fc
d12c6be
fc6d3fc
d12c6be
fc6d3fc
d12c6be
fc6d3fc
 
d12c6be
50d75a2
fc6d3fc
d12c6be
f6a18fc
d12c6be
f6a18fc
 
 
d12c6be
 
 
3e65e33
d12c6be
99338d0
3e65e33
99338d0
f6a18fc
466f6e2
f6a18fc
 
3e65e33
 
 
 
 
 
99338d0
d12c6be
99338d0
d12c6be
 
ac9cc0b
d12c6be
ac9cc0b
d12c6be
 
57be122
d12c6be
 
 
 
 
 
 
 
99338d0
d12c6be
 
 
 
 
 
879a171
 
57be122
d12c6be
879a171
 
d12c6be
879a171
d12c6be
 
ac9cc0b
d12c6be
 
 
 
 
99338d0
 
d12c6be
f6a18fc
fc44ec7
d12c6be
f6a18fc
 
99338d0
d12c6be
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44ec7
d12c6be
f6a18fc
99338d0
 
f6a18fc
 
d12c6be
f6a18fc
 
 
d12c6be
 
 
3e65e33
d12c6be
f6a18fc
d12c6be
fc44ec7
f6a18fc
 
99338d0
d12c6be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc44ec7
d12c6be
f6a18fc
fc44ec7
f6a18fc
466f6e2
f6a18fc
 
 
 
d12c6be
 
 
 
 
 
 
 
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import asyncio
import logging
import os
import random
import time
from pathlib import Path
from typing import AsyncGenerator, Optional

from cloakbrowser import launch_persistent_context_async

logger = logging.getLogger(__name__)

SCREENSHOT_DIR = Path(__file__).parent / "static" / "screenshots"


class DeepSeekBrowser:
    DEEPSEEK_URL = "https://chat.deepseek.com"

    def __init__(self, email, password, profile_dir="./profiles",
                 headless=True, humanize=True, proxy=None):
        self.email = email
        self.password = password
        self.profile_dir = Path(profile_dir) / email.replace("@", "_at_").replace("+", "_plus_")
        self.headless = headless
        self.humanize = humanize
        self.proxy = proxy
        self.context = None
        self.page = None
        self._logged_in = False
        self._ready = False

    def _safe_email(self):
        return self.email.replace("@", "_at_").replace("+", "_plus_")

    async def _save_screenshot(self, tag, error_msg=""):
        try:
            SCREENSHOT_DIR.mkdir(parents=True, exist_ok=True)
            path = SCREENSHOT_DIR / f"{tag}_{self._safe_email()}.png"
            await self.page.screenshot(path=str(path))
            # Write companion error context file
            if error_msg:
                txt_path = SCREENSHOT_DIR / f"{tag}_{self._safe_email()}.txt"
                txt_path.write_text(
                    f"Account: {self.email}\n"
                    f"Tag: {tag}\n"
                    f"Time: {time.strftime('%Y-%m-%d %H:%M:%S')}\n"
                    f"Error: {error_msg}\n",
                    encoding="utf-8"
                )
            logger.error("Screenshot saved to %s", path)
        except Exception as e:
            logger.debug("Screenshot failed: %s", e)

    async def _human_delay(self, min_ms=5, max_ms=30):
        await asyncio.sleep(random.uniform(min_ms, max_ms) / 1000)

    async def start(self):
        self.profile_dir.mkdir(parents=True, exist_ok=True)
        # Clean up stale Chromium lock files from previous crashes
        for lock in ["SingletonLock", "SingletonCookie", "SingletonSocket"]:
            lp = self.profile_dir / lock
            if lp.exists():
                try:
                    lp.unlink()
                except Exception:
                    pass
        args = [
            "--disable-gpu", "--disable-dev-shm-usage", "--disable-extensions",
            "--disable-background-networking", "--disable-default-apps",
            "--disable-sync", "--mute-audio", "--no-sandbox",
            "--js-flags=--max-old-space-size=128", "--renderer-process-limit=1",
        ]
        self.context = await launch_persistent_context_async(
            user_data_dir=str(self.profile_dir), headless=self.headless,
            humanize=self.humanize, proxy=self.proxy,
            viewport={"width": 1280, "height": 720}, locale="zh-CN", args=args,
        )
        self.page = await self.context.new_page()
        await self.page.goto(self.DEEPSEEK_URL, timeout=20000)
        await self._wait_for_cloudflare()
        await self._check_login_state()

    async def _wait_for_cloudflare(self):
        deadline = time.time() + 15
        last_url = ""
        while time.time() < deadline:
            try:
                url = self.page.url
                if url == last_url and "/cdn-cgi" not in url:
                    try:
                        await self.page.wait_for_selector(
                            'textarea, input[type="text"], input[type="password"]',
                            timeout=3000)
                        return
                    except Exception:
                        pass
                last_url = url
            except Exception:
                pass
            await asyncio.sleep(1)
        await asyncio.sleep(2)

    async def _check_login_state(self):
        current_url = self.page.url
        if "/sign_in" in current_url:
            await self._auto_login()
        else:
            try:
                await self.page.wait_for_selector("textarea", timeout=8000)
                self._logged_in = True
                self._ready = True
            except Exception:
                await self._auto_login()
        if self._logged_in:
            await self._check_mute()

    async def _check_mute(self):
        try:
            muted, until = await self.page.evaluate("""() => {
                const t = document.body.innerText || '';
                const m = t.match(/禁言至\\s*(\\d{4}[-年]\\d{1,2}[-月]\\d{1,2}[日]?\\s*\\d{1,2}:\\d{2})/);
                if (m) return [true, m[1]];
                if (t.includes('禁言')) return [true, ''];
                return [false, ''];
            }""")
            self._is_muted = muted
            self._muted_until = until
            if muted:
                logger.warning("[mute] %s is muted until %s", self.email, until)
        except Exception:
            self._is_muted = False
            self._muted_until = ""

    def is_muted(self):
        return getattr(self, "_is_muted", False)

    def muted_until(self):
        return getattr(self, "_muted_until", "")

    async def _auto_login(self):
        logger.info("Logging in as %s...", self.email)

        try:
            pwd_tab = self.page.locator('text="密码登录"').first
            if await pwd_tab.is_visible():
                await pwd_tab.click()
                await asyncio.sleep(0.3)
        except Exception as e:
            logger.debug("No password login tab: %s", e)

        try:
            email_input = None
            deadline = time.time() + 15
            while time.time() < deadline:
                for sel in [
                    'input[placeholder*="邮箱"]',
                    'input[placeholder*="手机号"]',
                    'input[placeholder*="Email"]',
                    'input.ds-input__input[type="text"]',
                ]:
                    el = self.page.locator(sel).first
                    try:
                        if await el.count() > 0 and await el.is_visible():
                            email_input = el
                            break
                    except Exception:
                        continue
                if email_input:
                    break
                await asyncio.sleep(0.8)
            if not email_input:
                raise TimeoutError("Email input not found")
            await email_input.fill(self.email)
            await asyncio.sleep(0.1)
        except Exception as e:
            await self._save_screenshot("login_fail_email", str(e))
            logger.error("Email input error: %s", e)
            raise

        try:
            pwd = self.page.locator('input[type="password"]').first
            await pwd.wait_for(state="visible", timeout=5000)
            await pwd.fill(self.password)
            await asyncio.sleep(0.1)
        except Exception as e:
            await self._save_screenshot("login_fail_password", str(e))
            logger.error("Password input error: %s", e)
            raise

        try:
            btn = self.page.locator('button:has-text("登录")').first
            await btn.click()
            await asyncio.sleep(1.5)
        except Exception as e:
            await self._save_screenshot("login_fail_button", str(e))
            logger.error("Login button error: %s", e)
            raise

        try:
            await self.page.wait_for_selector("textarea", timeout=20000)
            self._logged_in = True
            self._ready = True
            logger.info("Login successful for %s", self.email)
        except Exception as e:
            await self._save_screenshot("login_fail_final", str(e))
            raise Exception("Login failed")

    async def new_chat(self):
        try:
            btn = self.page.locator(
                'a:has-text("开启新对话"), button:has-text("开启新对话"), '
                'a:has-text("新对话"), button:has-text("新对话"), '
                '[class*="new-chat"], [class*="newChat"]'
            ).first
            if await btn.count() > 0:
                await btn.click()
                await self.page.wait_for_selector("textarea", timeout=10000)
                return
            await self.page.goto(self.DEEPSEEK_URL, timeout=20000)
            await self.page.wait_for_selector("textarea", timeout=15000)
        except Exception as e:
            logger.error("New chat error: %s", e)
            raise

    async def delete_chat(self):
        try:
            chat_list = self.page.locator(
                'nav, aside, [class*="sidebar"], [class*="Sidebar"], div:has-text("开启新对话")')
            if await chat_list.count() == 0:
                return
            active_item = chat_list.first.locator(
                '[class*="active"], [class*="selected"], [class*="current"]').first
            if await active_item.count() == 0:
                return
            result = await self.page.evaluate("""() => {
                const a = document.querySelector('[class*="active"], [class*="selected"]');
                if (!a) return 'no-active';
                const walk = (n, d) => {
                    if (d > 10) return null;
                    for (const c of n.children || []) {
                        const t = c.tagName, cls = (c.className || '').toString();
                        if ((t === 'BUTTON' || t === 'svg' || cls.includes('icon')
                             || cls.includes('more') || cls.includes('menu') || cls.includes('action'))
                            && c.offsetWidth < 40 && c.offsetWidth > 0) return c;
                        const f = walk(c, d + 1);
                        if (f) return f;
                    }
                    return null;
                };
                const icon = walk(a, 0);
                if (icon) { icon.click(); return 'clicked'; }
                const btn = a.querySelector('button, [role="button"]');
                if (btn) { btn.click(); return 'fallback'; }
                return 'no-icon';
            }""")
            logger.debug("[delete_chat] icon click: %s", result)
            await asyncio.sleep(0.5)
            del_btn = self.page.locator(':has-text("删除"), :has-text("Delete")').first
            if await del_btn.count() == 0:
                return
            await del_btn.click()
            await asyncio.sleep(0.5)
            confirm = self.page.locator(
                'button:has-text("确认"), button:has-text("删除"), '
                'button:has-text("Confirm"), button:has-text("Delete")').last
            if await confirm.count() > 0:
                await confirm.click()
                await asyncio.sleep(1)
        except Exception as e:
            logger.warning("[delete_chat] error: %s", e)

    async def switch_model(self, model):
        try:
            js = """(texts) => {
                const els = Array.from(document.querySelectorAll('*'));
                const t = els.reverse().find(el => {
                    if (!el.innerText || el.children.length > 0) return false;
                    return texts.some(x => el.innerText.includes(x)) && el.offsetParent !== null;
                });
                if (t) { t.click(); return true; }
                return false;
            }"""
            await self.page.evaluate(js, ['深度思考', 'DeepThink', 'R1'])
            await asyncio.sleep(0.5)
        except Exception as e:
            logger.warning("[switch_model] error: %s", e)

    async def send_message(self, prompt, timeout=120, model="deepseek-chat"):
        try:
            await self.new_chat()
            await self.switch_model(model)
            inp = self.page.locator("textarea").first
            await inp.wait_for(state="visible", timeout=15000)
            await inp.fill(prompt)
            await self._human_delay()
            await inp.press("Enter")
            result = await self._wait_for_response(timeout, prompt)
            asyncio.create_task(self._safe_delete_chat())
            return result
        except Exception as e:
            logger.error("Send message error: %s", e)
            raise

    async def _safe_delete_chat(self):
        try:
            await self.delete_chat()
        except Exception as e:
            logger.debug("[safe_delete] %s", e)

    _EXTRACT_JS = """() => {
        const r = {thinking: '', answer: '', done: false};
        const msgs = document.querySelectorAll(
            '[class*="assistant"], [class*="bot-"], [class*="message--"], [class*="message-wrapper"], [class*="chat-message"]');
        let last = null;
        for (let i = msgs.length - 1; i >= 0; i--) {
            if (!(msgs[i].className || '').toLowerCase().includes('user')) { last = msgs[i]; break; }
        }
        if (!last) return r;
        const scope = last;
        const mdEls = Array.from(scope.querySelectorAll(
            '[class*="markdown"], [class*="Markdown"], [class*="answer"], [class*="content"]'));
        const top = mdEls.filter(el => !mdEls.some(p => p !== el && p.contains(el)));
        let think = '', ans = '';
        if (top.length >= 2) { think = top[0].innerText.trim(); ans = top[top.length - 1].innerText.trim(); }
        else if (top.length === 1) {
            const t = top[0].innerText.trim();
            if (scope.innerText.includes('深度思考') && !scope.innerText.includes('已深度思考')) think = t;
            else ans = t;
        }
        const bt = scope.innerText || '';
        const hasMarker = bt.includes('深度思考') || bt.includes('极速思考') || bt.includes('思考过程');
        if (!ans || (!think && hasMarker)) {
            const lines = bt.split('\\n').map(l => l.trim()).filter(Boolean);
            const skip = ['智能搜索','快速模式','专家模式','极速思考','内容由 AI 生成','开启新对话','暂无历史对话'];
            let isTh = false, tl = [], al = [];
            for (const l of lines) {
                if (skip.some(s => l === s)) continue;
                if (l.length < 30 && (l.includes('深度思考') || l.includes('极速思考') || l.includes('思考过程'))) {
                    isTh = !(l.includes('已') || l.includes('用时') || l.includes('完成'));
                    continue;
                }
                if (isTh) tl.push(l); else al.push(l);
            }
            if (tl.length) think = tl.join('\\n');
            if (al.length) ans = al.join('\\n');
        }
        r.thinking = think; r.answer = ans;
        const sb = document.querySelector('[class*="stop"], button[aria-label*="stop"]');
        r.done = (!sb || sb.offsetParent === null);
        if (!r.answer && !r.thinking) r.done = false;
        return r;
    }"""

    async def _wait_for_response(self, timeout, prompt=""):
        deadline = time.time() + timeout
        await asyncio.sleep(0.3)
        last_answer, last_thinking, stable = "", "", 0
        while time.time() < deadline:
            try:
                result = await self.page.evaluate(self._EXTRACT_JS)
            except Exception as e:
                err = str(e)
                if "Target crashed" in err or "Target closed" in err:
                    logger.error("[_wait_for_response] Browser crashed: %s", e)
                    raise
                await asyncio.sleep(0.5)
                continue
            answer = (result.get("answer") or "").strip()
            thinking = (result.get("thinking") or "").strip()
            if answer or thinking:
                if answer != last_answer or thinking != last_thinking:
                    last_answer, last_thinking, stable = answer, thinking, 0
                else:
                    stable += 1
                if stable >= 2:
                    return {"content": last_answer, "reasoning_content": last_thinking}
            await asyncio.sleep(0.5)
        if last_answer or last_thinking:
            return {"content": last_answer, "reasoning_content": last_thinking}
        raise TimeoutError("No response received")

    async def stream_message(self, prompt, timeout=120, model="deepseek-chat"):
        try:
            await self.new_chat()
            await self.switch_model(model)
            inp = self.page.locator("textarea").first
            await inp.wait_for(state="visible", timeout=15000)
            await inp.fill(prompt)
            await self._human_delay()
            await inp.press("Enter")
            deadline = time.time() + timeout
            last_thinking, last_answer, stable = "", "", 0
            await asyncio.sleep(0.3)
            while time.time() < deadline:
                try:
                    result = await self.page.evaluate(self._EXTRACT_JS)
                except Exception as e:
                    err = str(e)
                    if "Target crashed" in err or "Target closed" in err:
                        logger.error("[stream_message] Browser crashed: %s", e)
                        raise
                    await asyncio.sleep(0.3)
                    continue
                thinking = (result.get("thinking") or "").strip()
                answer = (result.get("answer") or "").strip()
                if thinking and thinking != last_thinking:
                    new = thinking[len(last_thinking):]
                    if new:
                        yield {"type": "thinking", "chunk": new}
                    last_thinking = thinking
                if answer and answer != last_answer:
                    new = answer[len(last_answer):]
                    if new:
                        yield {"type": "content", "chunk": new}
                    last_answer, stable = answer, 0
                elif answer:
                    stable += 1
                if stable >= 2:
                    break
                await asyncio.sleep(0.3)
            asyncio.create_task(self._safe_delete_chat())
        except Exception as e:
            logger.error("Stream message error: %s", e)
            raise

    async def close(self):
        if self.context:
            try:
                await self.context.close()
            except Exception as e:
                logger.debug("Error closing browser: %s", e)
            self.context = None
            self.page = None
            self._logged_in = False
            self._ready = False