dsh-workbuddy-bridge-mobile 1.2.0 → 1.2.2
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/host.js +63 -23
- package/package.json +1 -1
- package/wb_bridge.py +41 -1
package/host.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-workbuddy-bridge
|
|
2
|
+
* @local/dsh-workbuddy-bridge — WorkBuddy 账号桥的 DSH 宿主插件。
|
|
3
3
|
*
|
|
4
4
|
* 1. 随 DSH 启动把 wb_bridge.py 拉起在 127.0.0.1:8878,看门狗守护;
|
|
5
5
|
* 2. 注册对话工具 wb_login / wb_status / wb_models / wb_quota / wb_image_generate。
|
|
@@ -45,18 +45,14 @@ function norm(config) {
|
|
|
45
45
|
|
|
46
46
|
function resolvePython(config) {
|
|
47
47
|
if (config.pythonBin && existsSync(config.pythonBin)) return config.pythonBin;
|
|
48
|
-
// 优先 PATH(Termux/proot/桌面都一样),再补充常见绝对位置。
|
|
49
|
-
for (const dir of (process.env.PATH ?? "").split(":")) {
|
|
50
|
-
for (const name of ["python3", "python"]) {
|
|
51
|
-
const p = join(dir, name);
|
|
52
|
-
if (existsSync(p)) return p;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
48
|
const candidates = [
|
|
49
|
+
"/data/data/com.dsharnessmobile.shell/files/usr/bin/python3",
|
|
56
50
|
"/usr/bin/python3",
|
|
57
|
-
"/usr/local/bin/python3",
|
|
58
|
-
"/opt/homebrew/bin/python3",
|
|
59
51
|
];
|
|
52
|
+
for (const dir of (process.env.PATH ?? "").split(":")) {
|
|
53
|
+
const p = join(dir, "python3");
|
|
54
|
+
if (existsSync(p)) return p;
|
|
55
|
+
}
|
|
60
56
|
for (const p of candidates) if (existsSync(p)) return p;
|
|
61
57
|
return "python3";
|
|
62
58
|
}
|
|
@@ -66,6 +62,7 @@ function resolveEngine(name_) {
|
|
|
66
62
|
const anchors = [
|
|
67
63
|
process.env.DSH_INSTALL_DIR,
|
|
68
64
|
process.env.DSH_PROFILE_DIR,
|
|
65
|
+
"/data/data/com.dsharnessmobile.shell/files/usr/lib/node_modules/@deepseek-ai/dsh",
|
|
69
66
|
join(homedir(), ".dsh/profiles/web"),
|
|
70
67
|
].filter(Boolean);
|
|
71
68
|
for (const a of anchors) {
|
|
@@ -75,6 +72,7 @@ function resolveEngine(name_) {
|
|
|
75
72
|
}
|
|
76
73
|
// 兜底:全局 node_modules 里也可能装着引擎
|
|
77
74
|
for (const g of [
|
|
75
|
+
"/data/data/com.dsharnessmobile.shell/files/usr/lib/node_modules",
|
|
78
76
|
"/usr/local/lib/node_modules",
|
|
79
77
|
"/usr/lib/node_modules",
|
|
80
78
|
join(homedir(), ".npm-global/lib/node_modules"),
|
|
@@ -408,22 +406,54 @@ export function apply(ctx, configRaw) {
|
|
|
408
406
|
if (config.enabled) startServe("activate");
|
|
409
407
|
if (config.doubao.enabled) startDoubao("activate");
|
|
410
408
|
|
|
411
|
-
//
|
|
412
|
-
|
|
413
|
-
|
|
409
|
+
// 启动后自动尝试挂载模型路由;**登录晚于启动时会持续重试**。
|
|
410
|
+
//
|
|
411
|
+
// 旧实现只在启动 8 秒后跑一次:那时用户通常还没登录,桥的 /v1/models 返回 401,
|
|
412
|
+
// buildModelEntries() 返回 null,于是什么都不写;而登录又不会触发重挂,
|
|
413
|
+
// 结果就是「登录成功了但模型选择器里空空如也」,只能靠手动 wb_sync_models 救。
|
|
414
|
+
// 现在改成:失败就按退避间隔重试,一旦登录成功(桥能吐出模型目录)自动挂上。
|
|
415
|
+
const AUTO_SYNC_FIRST_DELAY_MS = 8_000;
|
|
416
|
+
const AUTO_SYNC_RETRY_MS = 60_000;
|
|
417
|
+
const AUTO_SYNC_MAX_TRIES = 30; // 约 30 分钟;之后交给 wb_sync_models 手动触发
|
|
418
|
+
let autoSyncTries = 0;
|
|
419
|
+
// routeSynced 必须在 scheduleAutoSync 之前声明:函数体在定义时就可能引用它(let 有 TDZ)。
|
|
420
|
+
let routeSynced = false;
|
|
421
|
+
function scheduleAutoSync(delay) {
|
|
422
|
+
if (state.stopped || !config.autoSyncModels) return;
|
|
423
|
+
const t = setTimeout(async () => {
|
|
414
424
|
if (state.stopped || routeSynced) return;
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
425
|
+
autoSyncTries += 1;
|
|
426
|
+
try {
|
|
427
|
+
const h = await getHealth(3000);
|
|
428
|
+
if (!h?.up) return; // 桥没起来,下一轮再试
|
|
429
|
+
const r = await syncModels(false);
|
|
430
|
+
if (r?.synced || r?.skipped) {
|
|
431
|
+
routeSynced = true;
|
|
432
|
+
if (r?.synced) {
|
|
433
|
+
ctx.logger?.info?.(`workbuddy-bridge: 已挂载路由 ${JSON.stringify(r.routes ?? {})}`);
|
|
434
|
+
ctx.logger?.warn?.("workbuddy-bridge: 路由已写入,**重启一次 DSH** 后模型出现在选择器");
|
|
435
|
+
}
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
// 未登录(拿不到模型目录)或其它可重试原因:继续等
|
|
439
|
+
if (autoSyncTries < AUTO_SYNC_MAX_TRIES) scheduleAutoSync(AUTO_SYNC_RETRY_MS);
|
|
440
|
+
} catch (e) {
|
|
441
|
+
ctx.logger?.warn?.(`workbuddy-bridge: 自动挂载失败,稍后重试: ${String(e?.message ?? e)}`);
|
|
442
|
+
if (autoSyncTries < AUTO_SYNC_MAX_TRIES) scheduleAutoSync(AUTO_SYNC_RETRY_MS);
|
|
443
|
+
}
|
|
444
|
+
}, delay);
|
|
445
|
+
t.unref?.();
|
|
422
446
|
}
|
|
447
|
+
scheduleAutoSync(AUTO_SYNC_FIRST_DELAY_MS);
|
|
448
|
+
// 登录成功后可以被主动唤醒(见 wb_login / wb_status 的 kickAutoSync)。
|
|
449
|
+
const kickAutoSync = () => {
|
|
450
|
+
if (routeSynced || state.stopped) return;
|
|
451
|
+
autoSyncTries = 0;
|
|
452
|
+
scheduleAutoSync(1500);
|
|
453
|
+
};
|
|
423
454
|
|
|
424
455
|
// piece 1 自动挂载:桥起来后把 workbuddy 路由写进 profile patch 的 llm-pi-ai 行。
|
|
425
456
|
// 幂等:已有同名 route 就跳过(用户手改过的不覆盖)。
|
|
426
|
-
let routeSynced = false;
|
|
427
457
|
/** 生成 baseIndent 缩进的单个路由 YAML 行数组。 */
|
|
428
458
|
function routeYaml(routeId, models, apiKey, baseUrl, baseIndent) {
|
|
429
459
|
const pad = (n) => " ".repeat(n);
|
|
@@ -623,6 +653,15 @@ export function apply(ctx, configRaw) {
|
|
|
623
653
|
out.hint = "accessToken 快到期且无 refreshToken,建议重新 wb_login。";
|
|
624
654
|
} else if (!auth.logged_in) {
|
|
625
655
|
out.hint = "未登录:调用 wb_login 生成授权链接。";
|
|
656
|
+
} else {
|
|
657
|
+
// 已登录但模型还没挂上(很可能是「启动时未登录、之后才登录」):
|
|
658
|
+
// 主动补一次挂载,用户不必知道 wb_sync_models 的存在。
|
|
659
|
+
if (!routeSynced) {
|
|
660
|
+
kickAutoSync();
|
|
661
|
+
out.model_mount = "已登录,正在补挂模型目录(几秒后完成,之后需重启一次 DSH)";
|
|
662
|
+
} else {
|
|
663
|
+
out.model_mount = "模型目录已挂载";
|
|
664
|
+
}
|
|
626
665
|
}
|
|
627
666
|
return out;
|
|
628
667
|
},
|
|
@@ -674,7 +713,7 @@ export function apply(ctx, configRaw) {
|
|
|
674
713
|
auth_url: url,
|
|
675
714
|
state: st,
|
|
676
715
|
realm,
|
|
677
|
-
note: "让用户在浏览器打开 auth_url,登录 WorkBuddy 并确认授权(15
|
|
716
|
+
note: "让用户在浏览器打开 auth_url,登录 WorkBuddy 并确认授权(15 分钟内有效)。**授权完成后调用一次 wb_status**:它会自动补挂模型目录,然后重启一次 DSH,模型就出现在选择器里。",
|
|
678
717
|
};
|
|
679
718
|
},
|
|
680
719
|
})));
|
|
@@ -854,7 +893,8 @@ export function apply(ctx, configRaw) {
|
|
|
854
893
|
const candidates = [
|
|
855
894
|
args?.path,
|
|
856
895
|
join(homedir(), "../app_webview/Profile 1/Cookies"),
|
|
857
|
-
|
|
896
|
+
"/data/data/com.dsharnessmobile.shell/app_webview/Profile 1/Cookies",
|
|
897
|
+
"/data/data/com.dsharnessmobile.shell/app_webview/Default/Cookies",
|
|
858
898
|
].filter(Boolean);
|
|
859
899
|
const src = candidates.find((p) => { try { return existsSync(p); } catch { return false; } });
|
|
860
900
|
if (!src) return { error: "找不到 WebView Cookies 数据库,请手动传 path", tried: candidates };
|
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.2",
|
|
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:
|