dsh-jiuling-remote-mobile 1.0.3 → 1.0.4

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/lib/index.js +37 -11
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -549,7 +549,7 @@ export function apply(ctx) {
549
549
  }
550
550
 
551
551
  /**
552
- * 挑一个**手机本地**的 cwd 给新建的远程会话用。
552
+ * 挑一个**手机本地**的工作区给新建的远程会话用。
553
553
  *
554
554
  * ## ⚠️ 为什么不能直接用 `process.cwd()`
555
555
  *
@@ -567,24 +567,25 @@ export function apply(ctx) {
567
567
  * `files/workspace/deepseek-harness/default-workspace`(**更深一层**),
568
568
  * 两者不相等,正好踩中这个坑。
569
569
  *
570
- * 优先级:默认工作区 → 任意已有工作区 → 引擎 cwd。
571
- * 前两个都拿不到时回退 `process.cwd()`(新建的工作区会按 cwd 反向认领它,
572
- * 见 `dsh-workspace` 的 `bootstrap`/`groupsByPath`,不算死路)。
570
+ * ⚠️ **光把 cwd 设对还不够**:`sessionPaths` 索引只在该注册表启动
571
+ * (`indexHeader`)时建一次,新建的会话**不在索引里** ⇒ `sessionPath(id)`
572
+ * 返回 `undefined` ≠ `record.path`,照样被 getter 过滤掉。
573
+ * 所以创建完必须再 `attachSession()` 一次(见 `createSession`),
574
+ * 它会 `rememberSessionPath()` 把这条补进索引。
573
575
  *
574
576
  * @param ctx - 宿主 Cordis 上下文。
575
- * @returns 手机本地的绝对路径。
577
+ * @returns 工作区实体;没有可用工作区时 `undefined`。
576
578
  */
577
- function localCwdFor(ctx) {
579
+ function localWorkspaceFor(ctx) {
578
580
  try {
579
581
  const registry = ctx.get?.('workspaceRegistry');
580
582
  const all = registry?.list?.() ?? [];
581
- if (all.length === 0) return process.cwd();
583
+ if (all.length === 0) return undefined;
582
584
  const preferredId = registry?.state?.defaultWorkspaceId;
583
585
  const chosen = all.find((entity) => entity.id === preferredId) ?? all[0];
584
- const path = chosen?.path;
585
- return typeof path === 'string' && path !== '' ? path : process.cwd();
586
+ return typeof chosen?.path === 'string' && chosen.path !== '' ? chosen : undefined;
586
587
  } catch {
587
- return process.cwd();
588
+ return undefined;
588
589
  }
589
590
  }
590
591
 
@@ -623,7 +624,8 @@ async function handleBridge(ctx, config, io) {
623
624
  // ⚠️ 手机端 cwd 必须是**手机本地**的绝对路径,绝不把电脑路径传进来
624
625
  // (Android 是 posix 语义,`path.posix.isAbsolute('C:/...') === false`,
625
626
  // 且官方 createOrAdopt 会 `mkdir(cwd, {recursive:true})` 真去建目录)。
626
- const cwd = localCwdFor(ctx);
627
+ const workspace = localWorkspaceFor(ctx);
628
+ const cwd = workspace?.path ?? process.cwd();
627
629
  // ⚠️ **必须走 `sessionController.create`,不能走 `ctx.sessions.create`。**
628
630
  //
629
631
  // `ctx.sessions.create()` 只把会话塞进内存 store:持久化层既没
@@ -641,6 +643,30 @@ async function handleBridge(ctx, config, io) {
641
643
  throw new Error('sessionController.create 不可用,无法在手机本地建会话');
642
644
  }
643
645
  await ctx.sessionController.create({ sessionId, cwd });
646
+ // ⚠️ 建完必须**挂到工作区**上,否则前端看不到这个会话。
647
+ //
648
+ // 归属判据是 `dsh-workspace` 的 `sessionIds` getter:
649
+ // `record.sessionIds.filter(id => host.sessionPath(id) === record.path)`。
650
+ // 而 `sessionPaths` 索引只在注册表启动时建一次(`indexHeader`),
651
+ // 新建的会话不在索引里 ⇒ `sessionPath(id)` 是 `undefined` ≠ `record.path`
652
+ // ⇒ 被 getter 过滤掉:侧边栏没有它、composer 判成「没选工作区」而禁用。
653
+ //
654
+ // `attachSession()` 会 `rememberSessionPath()` 把这条补进索引,并写进
655
+ // `record.sessionIds` —— 正是官方「添加工作区」那条路的落点。
656
+ // 它要求 `header.cwd` 的 realpath **等于**工作区 path,所以上面的 cwd
657
+ // 必须取自同一个工作区实体,不能是 `process.cwd()`。
658
+ //
659
+ // ⚠️ 失败不致命(最坏是回到「会话在盘上、界面看不见」的旧症状),
660
+ // 所以只记日志,不让整个创建失败。
661
+ if (workspace !== undefined && typeof workspace.attachSession === 'function') {
662
+ try {
663
+ await workspace.attachSession(sessionId);
664
+ } catch (error) {
665
+ ctx.logger?.warn?.(
666
+ `[dsh-remote-mobile] 会话 ${sessionId} 挂到工作区失败(界面可能看不到它): ${String(error?.message ?? error)}`,
667
+ );
668
+ }
669
+ }
644
670
  try {
645
671
  await callRemote(config, 'createSession', { sessionId, cwd: params.path });
646
672
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-jiuling-remote-mobile",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "DSH 远程目录(手机端):在选文件夹流程里多出「电脑」页签,进入电脑目录、在电脑上发起对话,并把电脑的实时消息流同步到手机本地界面(只传文本,不映射桌面)。",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",