Repository navigation
Expand file tree
/
Copy pathengine.py
More file actions
1153 lines (1077 loc) · 51.7 KB
/
Copy pathengine.py
File metadata and controls
1153 lines (1077 loc) · 51.7 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Muse 生成引擎:用真实网页会话驱动 muse.ai 完成生图/生视频。
已验证流程:
注入 cookie -> 打开 https://muse.ai/ -> 定位 textarea(placeholder=消息)
-> Input.insertText 填入 -> 点「发送」
-> 等待新的附件容器 [data-testid^=hatch-chat-attachment-presentation-]
-> 从该容器内的 img/video 取 blob 字节 -> 落盘
"""
from __future__ import annotations
import base64
import mimetypes
import urllib.request
import json
import logging
import os
import re
import shutil
import subprocess
import time
import uuid
from cdp import CDP, http_json
log = logging.getLogger("muse2api")
ATT_SEL = '[data-testid^="hatch-chat-attachment-presentation-"]'
# 决定账号生死的核心 cookie(缺失或过期 = 会话失效)
ESSENTIAL_COOKIES = ("hatch_sess", "hatch_gw", "hatch_vml",
"hatch_native_auth_device")
class MuseAuthError(RuntimeError):
pass
class MuseGenerationError(RuntimeError):
pass
class MuseEngine:
def __init__(self, cfg):
self.cfg = cfg
self.proc: subprocess.Popen | None = None
self.browser: CDP | None = None
self.page: CDP | None = None
self.current_acc_id: str | None = None
self._last_http_renew: dict[str, float] = {}
self._log = None
os.makedirs(cfg.profile_dir, exist_ok=True)
# ---------------- 浏览器生命周期 ----------------
def _debug_url(self):
return f"http://127.0.0.1:{self.cfg.cdp_port}/json/version"
def start(self):
if self.proc and self.proc.poll() is None and self.browser:
return
env = dict(os.environ)
env.setdefault("HOME", self.cfg.home_dir)
env["PATH"] = (self.cfg.extra_path + os.pathsep + env.get("PATH", "")) if self.cfg.extra_path else env.get("PATH", "")
args = [
self.cfg.chromium,
"--headless=new", "--no-sandbox", "--disable-gpu",
"--disable-dev-shm-usage", "--disable-background-networking",
"--no-first-run", "--no-default-browser-check",
"--autoplay-policy=no-user-gesture-required",
"--window-size=1440,2400",
f"--remote-debugging-port={self.cfg.cdp_port}",
"--remote-allow-origins=*",
f"--user-data-dir={self.cfg.profile_dir}",
"about:blank",
]
# 尝试复用已有健康 CDP
if not self.proc:
try:
v = http_json(self._debug_url(), timeout=1)
if v and "webSocketDebuggerUrl" in v:
self.browser = CDP(v["webSocketDebuggerUrl"], timeout=180)
return
except Exception:
pass
# 清理残留锁
for lock_name in ("SingletonLock", "SingletonSocket", "SingletonCookie"):
lp = os.path.join(self.cfg.profile_dir, lock_name)
if os.path.exists(lp) or os.path.islink(lp):
try:
os.unlink(lp)
except Exception:
pass
os.makedirs(self.cfg.data_dir, exist_ok=True)
self._log = open(os.path.join(self.cfg.data_dir, "chromium.log"), "ab", buffering=0)
cwd_dir = self.cfg.home_dir if (self.cfg.home_dir and os.path.isdir(self.cfg.home_dir)) else None
self.proc = subprocess.Popen(args, stdout=self._log, stderr=subprocess.STDOUT,
env=env, cwd=cwd_dir)
last = None
for _ in range(90):
try:
v = http_json(self._debug_url(), timeout=2)
self.browser = CDP(v["webSocketDebuggerUrl"], timeout=180)
return
except Exception as exc: # noqa: BLE001
last = exc
time.sleep(1)
raise MuseGenerationError(f"Chromium 启动失败: {last}")
def stop(self):
for c in (self.page, self.browser):
if c:
c.close()
self.page = self.browser = None
if self.proc and self.proc.poll() is None:
self.proc.terminate()
try:
self.proc.wait(timeout=10)
except Exception: # noqa: BLE001
self.proc.kill()
self.proc = None
# ---------------- 页面 ----------------
def _open_page(self):
import requests
try:
pages = requests.get(f"http://127.0.0.1:{self.cfg.cdp_port}/json/list", timeout=3).json()
for p in pages:
if p.get("type") == "page":
pid = p.get("id")
if pid:
requests.get(f"http://127.0.0.1:{self.cfg.cdp_port}/json/close/{pid}", timeout=2)
except Exception:
pass
tgt = requests.put(
f"http://127.0.0.1:{self.cfg.cdp_port}/json/new?about:blank",
timeout=10).json()
page = CDP(tgt["webSocketDebuggerUrl"], timeout=180)
page.send("Network.enable")
page.send("Page.enable")
page.send("Runtime.enable")
page.send("Browser.setDownloadBehavior",
{"behavior": "allow", "downloadPath": self.cfg.download_dir})
return page
@staticmethod
def renew_session_http(cookies: dict, expires: dict | None = None,
wake_vm: bool = True) -> dict:
"""直接调用 muse.ai/api/session 续签 hatch_vml (+48h) / hatch_sess (+30d) / hatch_gw (+1y),
并按需调用 /api/hatch/vm/wake 唤醒云端工作区 VM。"""
import requests
cur_cookies = dict(cookies or {})
cur_exp = dict(expires or {})
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
"Origin": "https://muse.ai",
"Referer": "https://muse.ai/thread/new",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Cookie": "; ".join(f"{k}={v}" for k, v in cur_cookies.items() if v),
}
try:
r = requests.get("https://muse.ai/api/session", headers=headers,
timeout=12, allow_redirects=False)
except requests.RequestException as exc:
# 不回显请求内容:异常可能包含带凭据的代理 URL。
raise MuseGenerationError(
f"/api/session 网络请求失败 ({type(exc).__name__});请检查服务器网络/代理后重试") from None
if r.status_code == 401:
raise MuseAuthError("会话认证失败 (/api/session HTTP 401),请在官网确认登录后重新导入 cookie")
if r.status_code != 200:
hint = ("访问被拒绝,请检查服务器出口/地区/访问限制;不能据此判定 Cookie 失效"
if r.status_code == 403 else "上游请求未成功,请稍后重试并检查服务器网络")
raise MuseGenerationError(f"/api/session HTTP {r.status_code}:{hint}")
try:
sj = r.json()
except ValueError:
raise MuseGenerationError("/api/session HTTP 200 返回非 JSON;会话状态未确认") from None
if not isinstance(sj, dict) or sj.get("status") != "assigned":
raise MuseGenerationError("/api/session HTTP 200 未返回 assigned 会话;请在官网检查账号/工作区状态")
for c in r.cookies:
if c.value:
cur_cookies[c.name] = c.value
if c.expires:
cur_exp[c.name] = int(float(c.expires))
vm_id = sj.get("vm_id")
vm_state = sj.get("vm_state")
wake_ok = False
if wake_vm and vm_id and vm_state != "DISABLED":
try:
headers["Cookie"] = "; ".join(f"{k}={v}" for k, v in cur_cookies.items() if v)
rw = requests.post("https://muse.ai/api/hatch/vm/wake", headers=headers, json={
"vm_id": vm_id,
"retry_count": 0,
"connect_attempt_id": str(uuid.uuid4()),
}, timeout=8)
wake_ok = rw.status_code == 200
except Exception:
pass
return {
"ok": sj.get("status") == "assigned",
"status": sj.get("status"),
"vm_id": vm_id,
"vm_state": vm_state,
"wake_ok": wake_ok,
"cookies": cur_cookies,
"cookies_exp": cur_exp,
}
def _apply_cookies(self, page: CDP, cookies: dict, expires: dict | None = None):
"""注入 cookie。彻底清空旧账号 cookie 保证隔离,且绝不传入过去时间的 expires 防止 Chromium 丢弃 hatch_vml。"""
try:
page.send("Network.clearBrowserCookies")
except Exception:
pass
now = time.time()
for name, value in cookies.items():
if not value:
continue
exp = (expires or {}).get(name)
try:
exp_val = float(exp) if exp and float(exp) > now + 3600 else (now + 7 * 86400)
except (TypeError, ValueError):
exp_val = now + 7 * 86400
for dom in (".muse.ai", "muse.ai"):
params = {
"name": name,
"value": value,
"domain": dom,
"path": "/",
"secure": True,
"expires": exp_val,
}
try:
page.send("Network.setCookie", params)
except Exception: # noqa: BLE001
pass
def read_cookies(self) -> dict[str, dict]:
"""从当前页面读回 cookie(**包含 httpOnly**,这是网页 JS 做不到的)。
返回 {name: {"value":..., "expires": unix秒 或 -1}}。
用途:muse.ai 在访问时会续期部分 cookie,生成完读回来写进账号池,
账号就不容易过期。
"""
if not self.page:
return {}
try:
msg = self.page.send("Network.getCookies",
{"urls": [self.cfg.site_url]}, timeout=20)
except Exception: # noqa: BLE001
return {}
out: dict[str, dict] = {}
for c in (msg.get("result", {}).get("cookies") or []):
name = c.get("name")
if not name:
continue
try:
exp = int(float(c.get("expires", -1)))
except (TypeError, ValueError):
exp = -1
out[name] = {"value": c.get("value", ""), "expires": exp}
return out
def _wait_ws_ready(self, page: CDP, timeout: float = 15.0) -> bool:
"""等待 muse.ai 页面完成 React hydration 且不再处于 Connecting... 状态。"""
deadline = time.time() + timeout
while time.time() < deadline:
try:
st = page.js("""(function(){
if (document.readyState !== 'complete') return 'loading';
if (!document.querySelector('textarea')) return 'no-ta';
var h = document.querySelector('[data-hatch-shell-hydration-state]');
if (h && h.getAttribute('data-hatch-shell-hydration-state') !== 'hydrated') return 'hydrating';
var b = document.body ? (document.body.innerText || '') : '';
if (b.indexOf('Connecting...') !== -1) return 'connecting';
return 'ready';
})()""")
if st == "ready":
return True
except Exception:
pass
time.sleep(0.08)
return False
def reset_thread(self, for_chat: bool = False):
"""关闭残留弹窗并确保处于干净会话且 WebSocket 已就绪。
对于纯文本对话(for_chat=True),若当前热页面无附件、无卡死且气泡数较少,直接复用现有热连接以实现 2s 级秒回。"""
if not self.page:
return
try:
needs_nav = self.page.js("""(function(forChat){
var d = document.querySelector('[role="dialog"]');
if (d) {
var b = d.querySelector('button[aria-label*="close" i], button');
if (b) b.click();
}
var scope = document.querySelector('main,[class*="chat-scroll"],[class*="hatch-chat-scroll"]') || document.body;
var bubbleCount = scope ? scope.querySelectorAll('div[class*="hatch-chat-groupable-bubble"]').length : 0;
var hasAtts = document.querySelectorAll('[data-testid^="hatch-chat-attachment-presentation-"]').length > 0;
var hasStop = !!document.querySelector('button[aria-label*="Stop" i]');
var bodyTxt = document.body ? (document.body.innerText || '') : '';
var hasStuck = bodyTxt.indexOf('Still sending') !== -1 || bodyTxt.indexOf('Connecting...') !== -1;
if (hasStop || hasStuck || hasAtts) return true;
if (forChat) {
return bubbleCount >= 24;
}
return (window.location.pathname !== '/thread/new') || bubbleCount > 0;
})(%s)""" % ("true" if for_chat else "false"))
if needs_nav:
self.page.send("Page.navigate", {"url": "https://muse.ai/thread/new"})
t_end = time.time() + 10.0
while time.time() < t_end:
time.sleep(0.08)
ready = self.page.js("""(function(){
return document.readyState === 'complete'
&& !!document.querySelector('textarea')
&& document.querySelectorAll('div[class*="hatch-chat-groupable-bubble"]').length === 0;
})()""")
if ready:
break
self._wait_ws_ready(self.page, timeout=12.0)
except Exception:
pass
def ensure_page(self, cookies: dict, expires: dict | None = None, account_id: str | None = None):
if self.page is not None and (account_id is None or getattr(self, "current_acc_id", None) == account_id):
try:
if self.page.js("!!document.querySelector('textarea')"):
return self.page
except Exception:
pass
if self.page:
try:
self.page.close()
except Exception:
pass
self.page = None
# 仅当距离上次 HTTP 续签超过 10 分钟时才在主链路调用 /api/session,避免每次切号重复阻塞
last_map = getattr(self, "_last_http_renew", None)
if last_map is None:
last_map = {}
self._last_http_renew = last_map
now_ts = time.time()
if not account_id or (now_ts - last_map.get(account_id, 0) > 600):
try:
renewed = self.renew_session_http(cookies, expires, wake_vm=True)
if renewed.get("cookies"):
cookies = renewed["cookies"]
if renewed.get("cookies_exp"):
expires = renewed["cookies_exp"]
if account_id:
last_map[account_id] = now_ts
except MuseAuthError:
raise
except Exception as e:
log.warning("预续签 /api/session 失败(继续尝试浏览器加载): %s", e)
page = self._open_page()
self._apply_cookies(page, cookies, expires)
page.send("Page.navigate", {"url": "https://muse.ai/thread/new"})
for _ in range(self.cfg.login_wait * 2):
time.sleep(0.15)
try:
if page.js("!!document.querySelector('textarea')"):
self.page = page
self.current_acc_id = account_id
self._wait_ws_ready(page, timeout=15.0)
return page
except Exception:
pass
try:
if page.js("!!document.querySelector('textarea')"):
self.page = page
self.current_acc_id = account_id
self._wait_ws_ready(page, timeout=15.0)
return page
except Exception: # noqa: BLE001
pass
# 区分「会话失效(被踢回登录页)」和「页面加载卡住」,错误提示才能对症
try:
body = (page.js("document.body.innerText.slice(0,1200)") or "").lower()
except Exception: # noqa: BLE001
body = ""
page.close()
if re.search(r"log in|sign in|create an account|登录|use another account", body):
raise MuseAuthError("会话已被 muse.ai 登出(可能被其它登录挤掉或触发风控),"
"请用浏览器扩展重新导入 cookie")
raise MuseGenerationError("muse.ai 页面加载超时(未出现聊天输入框),请检查服务器网络后重试;未确认会话失效")
def refresh(self, cookies: dict, expires: dict | None = None):
if self.page:
self.page.close()
self.page = None
return self.ensure_page(cookies, expires)
# ---------------- 额度查询(Settings 面板) ----------------
# muse.ai 的额度在底部 Settings 菜单 → Settings 项 → 设置面板的
# General → Usage 区块里,形如:
# Free plan
# Weekly limit resets on Sep 30
# 1% used
# Additional tokens / Never expires / 0% used (2B tokens left)
def _click_point(self, x: int, y: int):
for t in ("mousePressed", "mouseReleased"):
self.page.send("Input.dispatchMouseEvent",
{"type": t, "x": x, "y": y,
"button": "left", "clickCount": 1})
_CLICK_JS = (
"(function(){var sel=%s;"
"var b=[...document.querySelectorAll(sel)]"
".filter(function(x){return x.offsetParent!==null;})[0];"
"if(!b)return null;var r=b.getBoundingClientRect();"
"return JSON.stringify({x:Math.round(r.x+r.width/2),"
"y:Math.round(r.y+r.height/2)});})()")
def quota(self, cookies: dict, expires: dict | None = None) -> dict:
"""打开 Settings 面板读额度。返回结构化 dict;读不到时 raise。"""
self.ensure_page(cookies, expires)
p = self.page
time.sleep(1)
# 1) 点左下角 Settings 按钮(aria-label=Settings)
raw = p.js(self._CLICK_JS % json.dumps('button[aria-label="Settings"]'))
if not raw:
raise MuseGenerationError("找不到 Settings 按钮")
pt = json.loads(raw)
self._click_point(pt["x"], pt["y"])
time.sleep(1.6)
# 2) 点弹出的菜单里文本为 Settings 的项
raw = p.js(
"(function(){"
"var els=[...document.querySelectorAll('div,span,li,[role=menuitem],button')]"
".filter(function(e){return e.offsetParent!==null"
"&&(e.textContent||'').trim()==='Settings'"
"&&e.getAttribute('aria-label')!=='Settings'"
"&&e.children.length<=3;});"
"if(!els.length)return null;"
"var el=els[els.length-1];var r=el.getBoundingClientRect();"
"return JSON.stringify({x:Math.round(r.x+r.width/2),"
"y:Math.round(r.y+r.height/2)});})()")
if not raw:
raise MuseGenerationError("Settings 菜单未弹出")
pt = json.loads(raw)
self._click_point(pt["x"], pt["y"])
time.sleep(3.0)
# 3) 读设置面板文本
txt = ""
for _ in range(6):
txt = p.js(
"(function(){var d=document.querySelector('[role=dialog],[aria-modal=true]');"
"return d?(d.innerText||''):'';})()") or ""
if "Usage" in txt or "used" in txt:
break
time.sleep(1.2)
# 4) 关闭面板(Escape)
for t in ("keyDown", "keyUp"):
p.send("Input.dispatchKeyEvent",
{"type": t, "key": "Escape", "code": "Escape",
"windowsVirtualKeyCode": 27, "nativeVirtualKeyCode": 27})
time.sleep(0.5)
return self._parse_quota(txt)
@staticmethod
def _parse_quota(txt: str) -> dict:
"""从设置面板文本解析额度字段。"""
lines = [ln.strip() for ln in (txt or "").split("\n") if ln.strip()]
out: dict = {"raw": "\n".join(lines[:40])}
# 计划名:Free plan / xxx plan
for ln in lines:
m = re.match(r"^(.+?)\s*plan$", ln, re.I)
if m:
out["plan"] = ln
break
# Weekly limit resets on Sep 30
m = re.search(r"Weekly limit resets? on (.+)", txt or "")
if m:
out["weekly_reset"] = m.group(1).strip()
# 周用量:第一个 "N% used"(出现在 plan 行之后)
m = re.search(r"(\d+)%\s*used", txt or "")
if m:
out["weekly_used_pct"] = int(m.group(1))
# 额外代币:"0% used (2B tokens left)"
m = re.search(r"(\d+)%\s*used\s*\(([^)]+)\)", txt or "")
if m:
out["extra_used_pct"] = int(m.group(1))
out["extra_left"] = m.group(2).strip()
if "Never expires" in (txt or ""):
out["extra_expires"] = "never"
out["found"] = bool(out.get("plan") or "weekly_used_pct" in out)
return out
# ---------------- 附件(生成结果) ----------------
_ATT_JS = (
"(function(){"
"var list = []; var seen = new Set();"
"function addEl(el, tid){"
" if(!el || seen.has(el)) return;"
" seen.add(el);"
" if(el.closest('form, [class*=chat-user-bubble], [class*=\"group/msg\"]')) return;"
" var v = el.querySelector('video') || (el.tagName === 'VIDEO' ? el : null);"
" var img = el.querySelector('img') || (el.tagName === 'IMG' ? el : null);"
" var isVid = (tid || '').includes('video') || !!v;"
" var primary = isVid ? (v || img) : (img || v);"
" var src = primary ? (primary.currentSrc || primary.src || '') : '';"
" if(src && !seen.has(src)){"
" seen.add(src);"
" list.push({"
" tid: tid || el.getAttribute('data-testid') || (isVid ? 'video' : 'image'),"
" hasVideo: !!v,"
" hasImg: !!img,"
" src: src,"
" vSrc: v ? (v.currentSrc || v.src || '') : '',"
" iSrc: img ? (img.currentSrc || img.src || '') : '',"
" w: primary ? (primary.videoWidth || primary.naturalWidth || 0) : 0,"
" h: primary ? (primary.videoHeight || primary.naturalHeight || 0) : 0"
" });"
" }"
"}"
"document.querySelectorAll('[data-testid^=\"hatch-chat-attachment-presentation-\"]').forEach(function(a){ addEl(a, a.getAttribute('data-testid')); });"
"document.querySelectorAll('div[class*=\"hatch-agent-bubble-bg\"] img, div[class*=\"hatch-agent-bubble-bg\"] video').forEach(function(m){"
" var s = m.currentSrc || m.src || '';"
" if(s && !s.includes('avatar') && !s.includes('emoji')) addEl(m.parentElement || m, 'agent-media');"
"});"
"return JSON.stringify(list);"
"})()"
)
def attachments(self) -> list[dict]:
try:
raw = self.page.js(self._ATT_JS)
return json.loads(raw) if raw else []
except Exception: # noqa: BLE001
return []
# ---------------- 发送 ----------------
# 检查「文字真的进了输入框 + Send 按钮真的被渲染出来」。
# 两个条件缺一不可:Send 按钮只有 React state 里有文字才会渲染 ——
# 它在,就说明 React 真的收到了输入(不是 DOM value 被改了而已)。
_SEND_STATE_JS = (
"(function(){var ta=document.querySelector('textarea');"
"var b=[...document.querySelectorAll('button,[role=button]')]"
".find(function(x){return /send/i.test(x.getAttribute('aria-label')||'');});"
"return JSON.stringify({v:ta?ta.value:'',btn:b?(b.disabled?2:1):0});})()"
)
def _send(self, prompt: str):
# 1. 确保 textarea 滚动到视口中央并获得真实焦点
try:
self.page.js("""(function(){
var ta = document.querySelector('textarea');
if (ta) {
ta.scrollIntoView({block: 'center', inline: 'nearest'});
ta.focus();
}
})()""")
except Exception:
pass
time.sleep(0.1)
rect = self.page.js(
"(function(){var t=document.querySelector('textarea');if(!t)return null;"
"var r=t.getBoundingClientRect();"
"return JSON.stringify({x:Math.round(r.left+r.width/2),"
"y:Math.round(r.top+r.height/2)});})()")
if not rect:
raise MuseGenerationError("找不到聊天输入框")
c = json.loads(rect)
for t in ("mousePressed", "mouseReleased"):
self.page.send("Input.dispatchMouseEvent",
{"type": t, "x": c["x"], "y": c["y"],
"button": "left", "clickCount": 1})
time.sleep(0.1)
# 触发 React 18 原型 setter 以及 input/change 事件以同步发送按钮状态
# (对于 DeepSeek/Codex 等 100KB+ 超长上下文,直接走原型 setter 仅需 <1s,避免 Input.insertText 逐字注入卡死)
_SETTER_JS = (
"(function(t){var ta=document.querySelector('textarea');"
"if(!ta) return 0;"
"ta.focus();"
"var s=Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype,'value').set;"
"s.call(ta,t);"
"ta.dispatchEvent(new Event('input',{bubbles:true}));"
"ta.dispatchEvent(new Event('change',{bubbles:true}));"
"return (ta.value||'').length;})(%s)")
val_len = self.page.js(_SETTER_JS % json.dumps(prompt)) or 0
if not val_len and len(prompt) < 500:
self.page.send("Input.insertText", {"text": prompt})
self.page.js(_SETTER_JS % json.dumps(prompt))
# 等待发送按钮就绪并点击
clicked = "no-button"
t_deadline = time.time() + 3.0
while time.time() < t_deadline:
res = self.page.js(
"(function(){var b=[...document.querySelectorAll('button,[role=button]')]"
".filter(function(x){return x.offsetParent!==null;})"
".find(function(x){return /发送|send/i.test(x.getAttribute('aria-label')||'')"
"||/发送|send/i.test(x.getAttribute('data-testid')||'')"
"||/send/i.test(x.innerText||'');});"
"if(!b)return 'no-button';"
"if(b.disabled)return 'disabled';"
"b.click();return 'clicked';})()")
if res == "clicked":
clicked = "clicked"
break
time.sleep(0.08)
if clicked != "clicked":
# 兜底:Ctrl+Enter 或普通 Enter
for combo in ({"modifiers": 1}, {}):
for t in ("keyDown", "char", "keyUp"):
params = {"type": t, "key": "Enter", "code": "Enter",
"windowsVirtualKeyCode": 13, "nativeVirtualKeyCode": 13,
"modifiers": combo.get("modifiers", 0)}
if t == "char":
params["text"] = "\r"
params["unmodifiedText"] = "\r"
self.page.send("Input.dispatchKeyEvent", params)
time.sleep(1.0)
try:
if self.page.js("!(document.querySelector('textarea')||{value:''}).value"):
clicked = "enter-sent"
break
except Exception:
pass
time.sleep(0.3)
return clicked
# ---------------- 等待生成 ----------------
def _last_attachment(self) -> dict | None:
atts = self.attachments()
return atts[-1] if atts else None
def _scroll_bottom(self):
"""滚到聊天底部。muse.ai 的聊天滚动容器是内层 div(不是 document),
虚拟列表按滚动位置渲染节点 —— 不滚到底,新消息根本不在 DOM 里。"""
try:
self.page.js(
"(function(){"
"var els=[...document.querySelectorAll('*')].filter(function(e){"
"var s=getComputedStyle(e);"
"return (s.overflowY==='auto'||s.overflowY==='scroll')"
"&&e.scrollHeight>e.clientHeight+100;});"
"els.sort(function(a,b){return b.scrollHeight-a.scrollHeight;});"
"if(els[0])els[0].scrollTop=els[0].scrollHeight;"
"var s=document.scrollingElement||document.body;"
"s.scrollTop=s.scrollHeight;"
"var el=document.querySelector('textarea');"
"if(el)el.scrollIntoView({block:'end'});return 1;})()")
except Exception: # noqa: BLE001
pass
def _wait_attachment(self, baseline_src: str, timeout: int, expect: str,
on_progress=None, base_agent_cnt: int = 0,
base_att_cnt: int = 0, stop_event=None, baseline_sources=None) -> dict | None:
baseline_sources = set(baseline_sources or ()) | {baseline_src}
deadline = time.time() + timeout
t_start = time.time()
stable_src, stable_n = "", 0
last_txt, txt_stable = "", 0
while time.time() < deadline:
if stop_event is not None and stop_event.is_set():
raise MuseGenerationError("客户端已断开连接,终止生成任务")
time.sleep(0.6)
self._scroll_bottom()
atts = self.attachments()
att = atts[-1] if atts else None
if att:
src = att.get("src") or ""
v_src = att.get("vSrc") or ""
tid = att.get("tid") or ""
w = att.get("w", 0) or 0
h = att.get("h", 0) or 0
has_video = att.get("hasVideo", False)
if expect == "video":
want = has_video or ("video" in tid) or ("video" in src) or ("video" in v_src) or src.endswith((".mp4", ".webm", ".mov"))
else:
want = ("image" in tid) or (not has_video)
check_src = v_src if (expect == "video" and v_src) else src
if check_src and check_src not in baseline_sources and want:
if w > 0 and h > 0:
return att
if check_src == stable_src:
stable_n += 1
else:
stable_src, stable_n = check_src, 0
if stable_n >= 1:
return att
elapsed = time.time() - t_start
if on_progress:
prog = min(92, int(25 + elapsed * 1.0))
try:
on_progress(prog)
except Exception:
pass
try:
st_raw = self.page.js("""(function(){
var bs=[].slice.call(document.querySelectorAll('div[class*="hatch-chat-groupable-bubble"]'))
.filter(function(b){return /hatch-agent-bubble-bg/.test(b.className||'');});
var lastTxt = bs.length ? (bs[bs.length-1].innerText||'').trim() : '';
var hasStop = !!document.querySelector('button[aria-label*="Stop" i]');
var tail = document.body ? (document.body.innerText||'').slice(-700) : '';
return JSON.stringify({cnt: bs.length, txt: lastTxt, stop: hasStop, tail: tail});
})()""")
st = json.loads(st_raw) if st_raw else {}
except Exception:
st = {}
tail = st.get("tail") or ""
if re.search(r"额度不足|积分不足|out of credits|达到上限|token limit", tail):
raise MuseGenerationError("账号额度不足")
# Sidebar/stale connection text does not prove this generation failed.
# The caller's generation deadline remains the bounded timeout.
# 快速失败:如果助手已经完成了纯文字回复(无 Stop 按钮且无新附件),且并非正在生成媒体的报告
cur_cnt = st.get("cnt") or 0
cur_txt = st.get("txt") or ""
has_stop = bool(st.get("stop"))
if cur_cnt > base_agent_cnt and cur_txt and not has_stop and len(atts) <= base_att_cnt:
# 检查是否包含媒体文件生成关键词(如 .webp, .png, .mp4, imagine_media 等),若是则说明正在产出媒体,绝不能误判为纯文本拒答
is_media_report = bool(re.search(r"\.(?:webp|png|jpe?g|mp4|webm)|imagine_media|deliverable|generated\s+.*image|verified\s+generated|artifact", cur_txt, re.I))
if not is_media_report:
if cur_txt == last_txt:
txt_stable += 1
else:
last_txt, txt_stable = cur_txt, 0
if txt_stable >= 15 and elapsed > 8.0:
raise MuseGenerationError(f"模型未生成媒体,仅返回文本: {cur_txt[:120]}")
else:
txt_stable = 0
else:
txt_stable = 0
return None
# ---------------- 取字节 ----------------
_EXTRACT_JS = r"""
(async function(src, expect){
try{
var u = src;
if(!u) return JSON.stringify({ok:false,err:'no-media-src'});
var r = await fetch(u);
if(!r.ok) return JSON.stringify({ok:false,err:'media-http-'+r.status});
var b = await r.blob();
var ab = await b.arrayBuffer();
var bytes = new Uint8Array(ab);
var s = '';
for(var i=0; i<bytes.length; i+=65536){
s += String.fromCharCode.apply(null, bytes.subarray(i, i+65536));
}
return JSON.stringify({ok:true, mime:b.type||'', size:b.size, url:u, b64:btoa(s)});
}catch(e){
return JSON.stringify({ok:false, err:String(e)});
}
})(%s, %s)
"""
# ---------------- 文本 / 代码对话 ----------------
_AGENT_TEXT_JS = (
"(function(){"
"var bs=[].slice.call(document.querySelectorAll("
"'div[class*=\"hatch-chat-groupable-bubble\"]'));"
"for(var i=bs.length-1;i>=0;i--){"
"var cs=bs[i].className||'';"
"if(/hatch-agent-bubble-bg/.test(cs))return bs[i].innerText||'';}"
"return '';})()"
)
_USER_COUNT_JS = (
"(function(){"
"var bs=[].slice.call(document.querySelectorAll("
"'div[class*=\"hatch-chat-groupable-bubble\"]'));"
"var n=0;for(var i=0;i<bs.length;i++){"
"if(/chat-user-bubble/.test(bs[i].className||''))n++;}"
"return String(n);})()"
)
_AGENT_COUNT_JS = (
"(function(){"
"var bs=[].slice.call(document.querySelectorAll("
"'div[class*=\"hatch-chat-groupable-bubble\"]'));"
"var n=0;for(var i=0;i<bs.length;i++){"
"if(/hatch-agent-bubble-bg/.test(bs[i].className||''))n++;}"
"return String(n);})()"
)
_POLL_CHAT_JS = (
"(function(){"
"var els=[...document.querySelectorAll('*')].filter(function(e){"
"var s=getComputedStyle(e);"
"return (s.overflowY==='auto'||s.overflowY==='scroll')&&e.scrollHeight>e.clientHeight+100;});"
"els.sort(function(a,b){return b.scrollHeight-a.scrollHeight;});"
"if(els[0])els[0].scrollTop=els[0].scrollHeight;"
"var scope=document.querySelector('main,[class*=\"chat-scroll\"],[class*=\"hatch-chat-scroll\"]')||document.body;"
"var bs=[].slice.call(scope.querySelectorAll('div[class*=\"hatch-chat-groupable-bubble\"]'))"
".filter(function(b){return /hatch-agent-bubble-bg/.test(b.className||'');});"
"var nonEmpty=bs.filter(function(b){return ((b.innerText||'').trim().length)>0;});"
"var txt=nonEmpty.length?(nonEmpty[nonEmpty.length-1].innerText||'').trim():'';"
"var stop=!!document.querySelector('button[aria-label*=\"Stop\" i]');"
"return JSON.stringify({cnt:nonEmpty.length,total:bs.length,txt:txt,stop:stop});})()"
)
def _agent_text(self) -> str:
"""最后一个助手气泡的文本(取不到就返回空串)。"""
try:
return (self.page.js(self._AGENT_TEXT_JS) or "").strip()
except Exception: # noqa: BLE001
return ""
def _agent_count(self) -> int:
try:
return int(self.page.js(self._AGENT_COUNT_JS) or 0)
except Exception: # noqa: BLE001
return 0
def _user_count(self) -> int:
try:
return int(self.page.js(self._USER_COUNT_JS) or 0)
except Exception: # noqa: BLE001
return 0
def _poll_chat(self) -> tuple[int, str, bool]:
try:
raw = self.page.js(self._POLL_CHAT_JS)
if raw:
d = json.loads(raw)
return int(d.get("cnt") or 0), (d.get("txt") or "").strip(), bool(d.get("stop"))
except Exception:
pass
return 0, "", False
def chat_stream(self, cookies: dict, prompt: str, expires: dict | None = None,
timeout: int | None = None, account_id: str | None = None,
stop_event=None):
"""发一条消息,流式 yield 增量文本。"""
timeout = int(timeout or getattr(self.cfg, "chat_timeout", 300))
self.ensure_page(cookies, expires, account_id=account_id)
self.reset_thread(for_chat=True)
base_agent, base_text, _ = self._poll_chat()
self._send(prompt)
t_sent = time.time()
deadline = t_sent + timeout
first_token_deadline = min(deadline, t_sent + 40.0)
sent, last, stable = "", None, 0
got_first = False
# 1. 等待助手生成并开始吐字(高频 60ms 采样,捕获到首批增量文字瞬间 yield 出去)
while time.time() < first_token_deadline:
if stop_event is not None and stop_event.is_set():
return
time.sleep(0.06)
cnt, cur, has_stop = self._poll_chat()
if not cur or (cnt <= base_agent and cur == base_text):
if time.time() - t_sent > 14.0:
try:
tail = self.page.js("document.body.innerText.slice(-500)") or ""
except Exception:
tail = ""
if "Still sending" in tail or "Connecting..." in tail:
raise MuseGenerationError("云端 VM 连接超时 (Still sending)")
continue
delta = cur[len(sent):] if cur.startswith(sent) else cur
if delta:
sent = cur
yield delta
last = cur
got_first = True
break
if not got_first:
raise MuseGenerationError("等待助手首字响应超时")
# 2. 持续捕获增量文本
while time.time() < deadline:
if stop_event is not None and stop_event.is_set():
return
time.sleep(0.06)
cnt, cur, has_stop = self._poll_chat()
if not cur:
continue
if cur != last:
delta = cur[len(sent):] if cur.startswith(sent) else cur
if delta:
sent = cur
yield delta
last, stable = cur, 0
else:
stable += 1
# Stop 按钮消失说明前端生成彻底结束,连续 3 次(约 0.18s)无新文本即正常退出
if not has_stop and stable >= 3:
return
# Stop 按钮仍在时绝不过早截断(模型思考、代码块或网络抖动),允许等待至 stable >= 80(约 5s)防卡死
if has_stop and stable >= 80:
return
raise MuseGenerationError("等待助手回复超时")
def chat(self, cookies: dict, prompt: str, expires: dict | None = None,
timeout: int | None = None, account_id: str | None = None) -> str:
"""发一条消息,返回完整回复文本(非流式)。"""
out = ""
for chunk in self.chat_stream(cookies, prompt, expires, timeout, account_id=account_id):
out += chunk
return out
def extract_bytes(self, src: str, expect: str = "image", retries: int = 4):
last = "未知"
for _ in range(retries):
raw = self.page.js(self._EXTRACT_JS % (json.dumps(src), json.dumps(expect)),
await_promise=True, timeout=600)
try:
info = json.loads(raw) if isinstance(raw, str) else raw
except Exception: # noqa: BLE001
info = {"ok": False, "err": f"解析失败 {str(raw)[:150]}"}
if info.get("ok"):
return base64.b64decode(info["b64"]), info.get("mime", ""), info.get("url", "")
last = info.get("err", "未知")
time.sleep(2)
raise MuseGenerationError(f"未能取回生成结果: {last}")
# ---------------- 下载兜底 ----------------
def _download_fallback(self, src: str, timeout: int = 180) -> str | None:
before = set(os.listdir(self.cfg.download_dir))
# ponytail: fail closed when the selected result has no local download;
# never click an unrelated/global button that can return the upload.
clicked = self.page.js("""(function(src){
var media=[...document.querySelectorAll('img,video')]
.find(m=>(m.currentSrc||m.src||'')===src);
var node=media && media.closest('[data-testid^="hatch-chat-attachment-presentation-"]');
if(!node) return 'none';
node=node.closest('[class*="group/widget-presentation"]')||node;
var b=[...node.querySelectorAll('button,[role=button]')]
.find(x=>/下载|保存|download/i.test(x.getAttribute('aria-label')||''));
if(!b) return 'none';
b.click();return 'ok';
})(%s)""" % json.dumps(src))
if clicked == "none":
return None
deadline = time.time() + timeout
while time.time() < deadline:
time.sleep(1.5)
new = [f for f in (set(os.listdir(self.cfg.download_dir)) - before)
if not f.endswith(".crdownload")]
if new:
p = os.path.join(self.cfg.download_dir, max(
new, key=lambda f: os.path.getmtime(os.path.join(self.cfg.download_dir, f))))
if os.path.getsize(p) > 0:
return p
return None
@staticmethod
def _normalize_image(img: str) -> tuple[str, str]:
"""将各种形态的图片输入归一为 (base64_str, mime_type)。"""
if not img:
return "", "image/png"
img = str(img).strip()
if img.startswith("data:"):
parts = img.split(",", 1)
mime = "image/png"
if ";" in parts[0]:
mime = parts[0].split(";")[0].replace("data:", "").strip()
return (parts[1].strip() if len(parts) > 1 else ""), mime
if img.startswith("http://") or img.startswith("https://"):
try:
req = urllib.request.Request(img, headers={"User-Agent": "Mozilla/5.0"})
with urllib.request.urlopen(req, timeout=20) as resp:
data = resp.read()
mime = resp.headers.get_content_type() or "image/png"
return base64.b64encode(data).decode("ascii"), mime
except Exception as e:
log.warning("下载远程参考图失败: %s", e)
return "", "image/png"
if os.path.isfile(img):
try:
with open(img, "rb") as f:
data = f.read()
mime = mimetypes.guess_type(img)[0] or "image/png"
return base64.b64encode(data).decode("ascii"), mime
except Exception as e:
log.warning("读取本地参考图失败: %s", e)
return "", "image/png"
return img, "image/png"
def _clear_attachments(self):
"""清除聊天输入框里遗留的附件缩略图。"""
try:
self.page.js(
"(function(){"
"var btns=Array.from(document.querySelectorAll('button')).filter(function(b){"
"return /remove attachment|移除|删除/i.test(b.getAttribute('aria-label')||b.innerText||'');"
"});"
"btns.forEach(function(b){b.click();});"
"var inp=document.querySelector('input[type=\"file\"]');"
"if(inp) inp.value='';"
"return btns.length;"