dsh-workbuddy-bridge-mobile 1.2.1 → 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.
Files changed (2) hide show
  1. package/host.js +63 -23
  2. package/package.json +1 -1
package/host.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * dsh-workbuddy-bridge-mobile — WorkBuddy 账号桥的 DSH 宿主插件。
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
- // 启动后等桥就绪,自动尝试挂载模型路由(仅首次;失败留待 wb_sync_models)
412
- if (config.autoSyncModels !== false) {
413
- setTimeout(() => {
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
- getHealth(3000).then(async (h) => {
416
- if (!h.up) return;
417
- const r = await syncModels(false).catch((e) => ({ error: String(e) }));
418
- routeSynced = Boolean(r?.synced || r?.skipped);
419
- if (r?.synced) ctx.logger?.info?.(`workbuddy-bridge: 已挂载路由 ${JSON.stringify(r.routes ?? {})}(重启 DSH 生效)`);
420
- }).catch(() => {});
421
- }, 8000);
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 分钟内有效)。完成后用 wb_status 确认,wb_models 看模型。",
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
- join(homedir(), "../app_webview/Default/Cookies"),
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.1",
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",