dsh-workbuddy-bridge-mobile 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/wb_bridge.py +41 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-workbuddy-bridge-mobile",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Runs local account bridges for WorkBuddy and Doubao (OpenAI-compatible gateways plus login/quota/image tools) and mounts both accounts' models into the DSH model selector.",
|
|
6
6
|
"license": "MIT",
|
package/wb_bridge.py
CHANGED
|
@@ -43,6 +43,7 @@ HOME = os.path.expanduser("~/.wb-bridge")
|
|
|
43
43
|
AUTH_FILE = os.path.join(HOME, "auth.json")
|
|
44
44
|
CONF_FILE = os.path.join(HOME, "config.json")
|
|
45
45
|
MODELS_CACHE = os.path.join(HOME, "models.json")
|
|
46
|
+
LAST_FAIL = os.path.join(HOME, "last-11128.json")
|
|
46
47
|
|
|
47
48
|
BASES = {
|
|
48
49
|
"cn": {"chat": "https://copilot.tencent.com",
|
|
@@ -501,6 +502,8 @@ _TRIGGER_RULES = [
|
|
|
501
502
|
(re.compile(r"^(\s*)You are an? ([^\n.]{0,60}?)\s+powered by DeepSeek\s*Harness\.", re.I | re.M), r"\1You are an \2."),
|
|
502
503
|
(re.compile(r"Tokens prefixed with @ are paths", re.I), "Tokens prefixed with (at) are paths"),
|
|
503
504
|
(re.compile(r"prefixed with @ are paths", re.I), "prefixed with (at) are paths"),
|
|
505
|
+
(re.compile(r"You are DeepSeek Harness([,.])", re.I), r"You are an AI agent\1"),
|
|
506
|
+
(re.compile(r"DeepSeek\s+Harness", re.I), "DeepSeek\nHarness"),
|
|
504
507
|
]
|
|
505
508
|
|
|
506
509
|
|
|
@@ -561,6 +564,28 @@ def apply_channel_guard(body: dict) -> dict:
|
|
|
561
564
|
return body
|
|
562
565
|
|
|
563
566
|
|
|
567
|
+
def channel_fallback(body: dict):
|
|
568
|
+
"""11128 兜底:把 system 折进首条 user(上游只扫 system/assistant)。"""
|
|
569
|
+
b = json.loads(json.dumps(body, ensure_ascii=False))
|
|
570
|
+
msgs = b.get("messages")
|
|
571
|
+
if not isinstance(msgs, list):
|
|
572
|
+
return None
|
|
573
|
+
sys_text = "\n\n".join(_content_text(m.get("content")) for m in msgs
|
|
574
|
+
if isinstance(m, dict) and m.get("role") == "system").strip()
|
|
575
|
+
rest = [m for m in msgs if not (isinstance(m, dict) and m.get("role") == "system")]
|
|
576
|
+
if not sys_text:
|
|
577
|
+
return None
|
|
578
|
+
first_user = next((i for i, m in enumerate(rest)
|
|
579
|
+
if isinstance(m, dict) and m.get("role") == "user"), None)
|
|
580
|
+
if first_user is None:
|
|
581
|
+
rest.insert(0, {"role": "user", "content": sys_text})
|
|
582
|
+
else:
|
|
583
|
+
cur = _content_text(rest[first_user].get("content"))
|
|
584
|
+
rest[first_user]["content"] = sys_text + "\n\n" + cur
|
|
585
|
+
b["messages"] = rest
|
|
586
|
+
return b
|
|
587
|
+
|
|
588
|
+
|
|
564
589
|
def normalize_body(body: dict, models: list[dict]) -> dict:
|
|
565
590
|
mid = body.get("model") or ""
|
|
566
591
|
info = next((m for m in models if m["id"] == mid), None)
|
|
@@ -755,18 +780,33 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
|
|
755
780
|
body["stream"] = True
|
|
756
781
|
payload = json.dumps(body, ensure_ascii=False).encode()
|
|
757
782
|
|
|
783
|
+
tried_fallback = False
|
|
758
784
|
for attempt in (1, 2):
|
|
759
785
|
res = upstream_chat(payload, auth)
|
|
760
786
|
if isinstance(res, tuple): # (status, body) 错误
|
|
761
787
|
status, raw = res
|
|
788
|
+
text = raw.decode("utf-8", "replace")
|
|
762
789
|
if status == 401 and attempt == 1 and auth.get("refresh_token"):
|
|
763
790
|
try:
|
|
764
791
|
auth = refresh(auth)
|
|
765
792
|
continue
|
|
766
793
|
except Exception as e:
|
|
767
794
|
log("WARN 401 续期失败:", e)
|
|
795
|
+
if status == 400 and "11128" in text and not tried_fallback:
|
|
796
|
+
tried_fallback = True
|
|
797
|
+
try:
|
|
798
|
+
with open(LAST_FAIL, "wb") as f:
|
|
799
|
+
f.write(payload)
|
|
800
|
+
except Exception:
|
|
801
|
+
pass
|
|
802
|
+
fb = channel_fallback(body)
|
|
803
|
+
if fb is not None:
|
|
804
|
+
log("WARN 11128:system 折进 user 后重试一次")
|
|
805
|
+
body = fb
|
|
806
|
+
payload = json.dumps(body, ensure_ascii=False).encode()
|
|
807
|
+
continue
|
|
768
808
|
return self._send_json(status, {"error": {
|
|
769
|
-
"message":
|
|
809
|
+
"message": text[:2000],
|
|
770
810
|
"type": "upstream"}})
|
|
771
811
|
break
|
|
772
812
|
else:
|