dsh-session-ops 0.2.5 → 0.2.6

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/README.md CHANGED
@@ -53,14 +53,20 @@ shows the current Session's durable artifact:
53
53
 
54
54
  - size, compression format (`zstd` / uncompressed), header format classification (current /
55
55
  migration-required);
56
- - the log file's absolute path;
56
+ - the log file's **directory** (the full file path is in the row's tooltip);
57
57
  - whether the Session is resident in the Host process and mid-turn, plus its workspace.
58
58
 
59
+ The section also carries an **打开目录** button: one click opens that directory in the OS file
60
+ manager (Finder / Explorer / xdg-open). It sends only the Session id to
61
+ `/session-admin/api/openDir`; the Host derives the directory from its own
62
+ `sessionPersistence.stat(sessionId)` result, so the browser never gets a generic
63
+ "open any folder" capability. A failed open is reported inline instead of throwing.
64
+
59
65
  The data comes from one read-only `sessionPersistence.stat(sessionId)` call
60
66
  (`/session-admin/api/health`). The route **never decompresses or reads the event log** and never
61
- enumerates all sessions; the section offers a manual refresh only, and deeper checks such as
62
- frame-level verification are out of scope. It renders a placeholder while loading so the panel
63
- never hides the section title for lack of content, and lowers its density when shown as an overlay.
67
+ enumerates all sessions; deeper checks such as frame-level verification are out of scope. It
68
+ renders a placeholder while loading so the panel never hides the section title for lack of
69
+ content, and lowers its density when shown as an overlay.
64
70
 
65
71
  On 0.2.1 hosts the slot is not declared: the registration stays pending without an error, and the
66
72
  tabs, header icon, context menu, model tools and both HTTP prefixes keep working.
package/docs/DETAIL.md CHANGED
@@ -16,7 +16,7 @@ between workspaces while keeping the registry consistent.
16
16
 
17
17
  Registers one read-only section in the conversation header's Session panel: slot `conversation.session.panel.section`, id `dsh-session-ops:session`, order 350 (after environment 0 / change-requests 100 / subagents 200 / shared-tasks 250 / background-jobs 300).
18
18
 
19
- - Data plane: `/session-admin/api/health` — one `ctx.sessionPersistence.stat(sessionId)` call returns `path` / `sizeBytes` / `compression` / `formatStatus` / `workspaceId` / `resident` / `running`.
19
+ - Data plane: `/session-admin/api/health` — one `ctx.sessionPersistence.stat(sessionId)` call returns `path` / `dir` / `sizeBytes` / `compression` / `formatStatus` / `workspaceId` / `resident` / `running`; the panel shows the directory and carries an **open directory** button (`/session-admin/api/openDir`), which takes only the session id and lets the Host derive the path.
20
20
  - No list, no scan, no full log read (`readStoredLog` decompresses the whole frame chain); the section offers a manual refresh, and deeper checks are left for later.
21
21
  - A missing Session, a host without `stat`, or a throwing `stat` all come back as 200 + `{found:false, reason}`; an invalid id (empty string, path separators, …) is a structured 400.
22
22
  - A 0.2.1 host does not declare the slot: the registration stays pending without an error, and every other entry keeps working.
package/docs/DETAIL.zh.md CHANGED
@@ -17,8 +17,8 @@
17
17
 
18
18
  在会话标题栏的会话面板里注册一个只读 section:槽 `conversation.session.panel.section`、id `dsh-session-ops:session`、order 350(排在 environment 0 / change-requests 100 / subagents 200 / shared-tasks 250 / background-jobs 300 之后)。
19
19
 
20
- - 数据面:`/session-admin/api/health`,一次 `ctx.sessionPersistence.stat(sessionId)` 返回 `path` / `sizeBytes` / `compression` / `formatStatus` / `workspaceId` / `resident` / `running`。
21
- - 不 list、不 scan、不整读日志(`readStoredLog` 会解压整条帧链);面板里只有刷新,深度检查留给后续。
20
+ - 数据面:`/session-admin/api/health`,一次 `ctx.sessionPersistence.stat(sessionId)` 返回 `path` / `dir` / `sizeBytes` / `compression` / `formatStatus` / `workspaceId` / `resident` / `running`。
21
+ - 不 list、不 scan、不整读日志(`readStoredLog` 会解压整条帧链);面板显示日志**目录**,并提供「打开目录」按钮(`/session-admin/api/openDir`):浏览器只传 sessionId,目录由 Host 从 stat 结果推导,没有任意路径打开能力;打开失败在面板内显示一句话。深度检查留给后续。
22
22
  - 会话不存在、宿主没有 `stat`、`stat` 抛错都返回 200 + `{found:false, reason}`;非法 id(空串、路径分隔符等)返回 400 结构化错误。
23
23
  - 0.2.1 宿主不声明该槽:注册挂起、不报错,其余入口照旧。
24
24
 
package/lib/admin.js CHANGED
@@ -30,9 +30,10 @@
30
30
  *
31
31
  * @module dsh-session-ops/admin
32
32
  */
33
+ import { spawn } from 'node:child_process';
33
34
  import { existsSync, readFileSync, readdirSync, rmSync, statSync } from 'node:fs';
34
35
  import { tmpdir } from 'node:os';
35
- import { join, resolve, sep } from 'node:path';
36
+ import { dirname, join, resolve, sep } from 'node:path';
36
37
  import { readSessionHeader } from './store/frames.js';
37
38
  import { dshHome } from './store/host.js';
38
39
  import { createApiHandler } from './store/http.js';
@@ -597,6 +598,7 @@ export async function sessionHealth(sessionId, source, holds) {
597
598
  id,
598
599
  found: false,
599
600
  path: null,
601
+ dir: null,
600
602
  sizeBytes: null,
601
603
  compression: compressionOf(source),
602
604
  formatStatus: null,
@@ -622,16 +624,76 @@ export async function sessionHealth(sessionId, source, holds) {
622
624
  }
623
625
  if (snapshot === undefined || snapshot === null)
624
626
  return { ...base, reason: 'not-found' };
627
+ const path = typeof snapshot.location?.path === 'string' ? snapshot.location.path : null;
625
628
  return {
626
629
  ...base,
627
630
  found: true,
628
- path: typeof snapshot.location?.path === 'string' ? snapshot.location.path : null,
631
+ path,
632
+ dir: path === null ? null : dirname(path),
629
633
  sizeBytes: typeof snapshot.sizeBytes === 'number' && Number.isFinite(snapshot.sizeBytes)
630
634
  ? snapshot.sizeBytes
631
635
  : null,
632
636
  formatStatus: typeof snapshot.formatStatus === 'string' ? snapshot.formatStatus : null,
633
637
  };
634
638
  }
639
+ /**
640
+ * 用系统文件管理器打开目录:macOS `open`、Windows `explorer`、其余 `xdg-open`。
641
+ *
642
+ * `detached + unref` 让子进程脱离 Host 的生命周期——面板点一下不该在 Host 里留下
643
+ * 一个常驻子进程;只等 `spawn` 事件确认命令确实起来,不等它退出。
644
+ * @returns 打开动作。
645
+ */
646
+ export function systemDirOpener() {
647
+ const command = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'explorer' : 'xdg-open';
648
+ return (dir) => new Promise((resolveOpen, rejectOpen) => {
649
+ let child;
650
+ try {
651
+ child = spawn(command, [dir], { detached: true, stdio: 'ignore' });
652
+ }
653
+ catch (error) {
654
+ rejectOpen(error);
655
+ return;
656
+ }
657
+ child.once('error', (error) => rejectOpen(error));
658
+ child.once('spawn', () => {
659
+ child.unref();
660
+ resolveOpen();
661
+ });
662
+ });
663
+ }
664
+ /** 当前生效的打开动作;测试通过 {@link __setDirOpenerForTests} 替换。 */
665
+ let dirOpener = systemDirOpener();
666
+ /**
667
+ * 仅供测试:替换/还原 {@link openSessionDir} 使用的系统打开动作。
668
+ * @param opener - 假的打开动作;传 undefined 还原为真实实现。
669
+ */
670
+ export function __setDirOpenerForTests(opener) {
671
+ dirOpener = opener ?? systemDirOpener();
672
+ }
673
+ /**
674
+ * 在系统文件管理器里打开一条会话的日志目录。
675
+ *
676
+ * 目录由 Host 从 `sessionPersistence.stat()` 的结果推导,**浏览器不能传任意路径**——
677
+ * 这个动作只可能打开本机会话日志所在目录,不构成通用的「打开任意文件夹」接口。
678
+ * @param sessionId - 请求里的会话 id(会先校验,非法时抛给路由层转 400)。
679
+ * @param source - 持久化后端与归属查询。
680
+ * @param holds - `{resident, running}`。
681
+ * @returns 结构化结果(`ok` + `dir` / 失败原因)。
682
+ */
683
+ export async function openSessionDir(sessionId, source, holds) {
684
+ const health = await sessionHealth(sessionId, source, holds);
685
+ if (health.found !== true)
686
+ return { ok: false, reason: health.reason ?? 'not-found' };
687
+ if (health.dir === null)
688
+ return { ok: false, reason: 'no-directory' };
689
+ try {
690
+ await dirOpener(health.dir);
691
+ }
692
+ catch (error) {
693
+ return { ok: false, reason: 'open-failed', message: messageOf(error), dir: health.dir };
694
+ }
695
+ return { ok: true, dir: health.dir };
696
+ }
635
697
  /** `GET` 与 `POST` 共用的方法表。 */
636
698
  function buildApi(admin, config, log, holds, health) {
637
699
  return {
@@ -661,6 +723,14 @@ function buildApi(admin, config, log, holds, health) {
661
723
  async health(payload) {
662
724
  return sessionHealth(payload?.sessionId, health, holds);
663
725
  },
726
+ /**
727
+ * 在系统文件管理器里打开一条会话的日志目录(会话面板「会话日志」的按钮)。
728
+ *
729
+ * 只按 sessionId 打开本机已知的会话目录,不接受任意路径;非法 id 走 400。
730
+ */
731
+ async openDir(payload) {
732
+ return openSessionDir(payload?.sessionId, health, holds);
733
+ },
664
734
  /**
665
735
  * 只要个数的轻量查询,供电标这类高频调用方使用。
666
736
  *
package/lib/client.js CHANGED
@@ -328,6 +328,23 @@
328
328
  listeners.delete(listener);
329
329
  };
330
330
  },
331
+ /**
332
+ * 在系统文件管理器里打开该会话日志所在目录。
333
+ *
334
+ * 只把 sessionId 交给 Host(目录由 Host 从 stat 结果推导),浏览器拿不到任意
335
+ * 路径的打开能力;失败时返回结构化原因,交给按钮显示一句话。
336
+ * @param sessionId - 会话 id。
337
+ * @returns `{ok:true, dir}` 或 `{ok:false, reason, message?}`(请求本身失败也算结果)。
338
+ */
339
+ async openDir(sessionId) {
340
+ try {
341
+ const result = await api.call('openDir', { sessionId });
342
+ return typeof result === 'object' && result !== null ? result : { ok: false, reason: 'unknown' };
343
+ }
344
+ catch (error) {
345
+ return { ok: false, reason: 'request-failed', message: String(error?.message ?? error) };
346
+ }
347
+ },
331
348
  /** 取消某条会话的在途请求(section 卸载时用:没人等这个结果了)。 */
332
349
  abort(sessionId) {
333
350
  const controller = controllers.get(sessionId);
@@ -372,6 +389,28 @@
372
389
  return `读取失败:${entry?.value?.message ?? '未知错误'}`;
373
390
  return '没有可显示的日志信息';
374
391
  }
392
+ /** 「打开目录」失败时的界面文案。 */
393
+ function describeOpenFailure(reason) {
394
+ if (reason === 'not-found')
395
+ return '这条会话已不在磁盘上';
396
+ if (reason === 'persistence-unavailable')
397
+ return '宿主未暴露会话持久化 stat';
398
+ if (reason === 'read-failed')
399
+ return '读取会话失败';
400
+ if (reason === 'no-directory')
401
+ return '这条会话还没有落盘文件';
402
+ if (reason === 'open-failed')
403
+ return '系统没能打开目录';
404
+ if (reason === 'request-failed')
405
+ return '请求失败';
406
+ return '打开失败';
407
+ }
408
+ /** 从文件路径取目录(浏览器里没有 node:path,只做最小处理)。 */
409
+ function dirOfFile(path) {
410
+ const text = String(path);
411
+ const index = Math.max(text.lastIndexOf('/'), text.lastIndexOf('\\'));
412
+ return index > 0 ? text.slice(0, index) : text;
413
+ }
375
414
  /**
376
415
  * 「会话日志」section:只读展示本会话的落盘工件。
377
416
  *
@@ -402,13 +441,28 @@
402
441
  };
403
442
  }, [sessionId]);
404
443
  const entry = health.entryOf(sessionId);
405
- const refreshButton = h('button', { type: 'button', style: S.button, onClick: () => void health.refresh(sessionId) }, '刷新');
444
+ const [openState, setOpenState] = React.useState({ kind: 'idle', message: '' });
445
+ // 失败/读取中只给「重试」;有数据时给「打开目录」——刷新按钮对只读面板没有意义。
446
+ const retryButton = h('button', { type: 'button', style: S.button, onClick: () => void health.refresh(sessionId) }, '重试');
447
+ const openButton = h('button', {
448
+ type: 'button',
449
+ style: S.button,
450
+ onClick: () => {
451
+ setOpenState({ kind: 'opening', message: '' });
452
+ void health.openDir(sessionId).then((result) => {
453
+ if (result?.ok === true)
454
+ setOpenState({ kind: 'opened', message: '已在文件管理器中打开' });
455
+ else
456
+ setOpenState({ kind: 'error', message: describeOpenFailure(result?.reason) });
457
+ });
458
+ },
459
+ }, '打开目录');
406
460
  if (entry === undefined || entry.state === 'error') {
407
- return h('div', { style: S.log }, h('div', { style: entry === undefined ? S.muted : S.error }, entry === undefined ? '读取中…' : `读取失败:${entry.message}`), entry === undefined ? null : refreshButton);
461
+ return h('div', { style: S.log }, h('div', { style: entry === undefined ? S.muted : S.error }, entry === undefined ? '读取中…' : `读取失败:${entry.message}`), entry === undefined ? null : retryButton);
408
462
  }
409
463
  const value = entry.value;
410
464
  if (value === undefined || value?.found !== true) {
411
- return h('div', { style: S.log }, h('div', { style: S.muted }, describeMissingLog(entry)), refreshButton);
465
+ return h('div', { style: S.log }, h('div', { style: S.muted }, describeMissingLog(entry)), retryButton);
412
466
  }
413
467
  const badges = [
414
468
  h('span', {
@@ -428,9 +482,14 @@
428
482
  badges.push(h('span', { key: 'resident', style: S.badge }, '驻留'));
429
483
  if (entry.state === 'loading')
430
484
  badges.push(h('span', { key: 'loading', style: S.muted }, '刷新中…'));
431
- return h('div', { style: S.log }, h('div', { style: S.logRow }, badges, h('span', { style: S.logRefresh }, refreshButton)), value.path === null
485
+ // 界面上直接给「目录」而不是长文件路径:一键打开的就是这个目录。
486
+ const dir = typeof value.dir === 'string' ? value.dir : value.path === null ? null : dirOfFile(value.path);
487
+ const feedback = openState?.kind === 'opened' || openState?.kind === 'error'
488
+ ? h('div', { style: openState.kind === 'error' ? S.error : S.muted }, openState.message)
489
+ : null;
490
+ return h('div', { style: S.log }, h('div', { style: S.logRow }, badges, h('span', { style: S.logRefresh }, openButton)), dir === null
432
491
  ? null
433
- : h('div', { style: dense ? { ...S.logPath, ...S.logPathDense } : S.logPath, title: value.path }, value.path));
492
+ : h('div', { style: dense ? { ...S.logPath, ...S.logPathDense } : S.logPath, title: value.path ?? undefined }, dir), feedback);
434
493
  };
435
494
  }
436
495
  /**
package/lib/index.js CHANGED
@@ -45,10 +45,10 @@
45
45
  */
46
46
  import z from '@deepseek-ai/schemastery';
47
47
  import { LOG_LINE_LEVEL_FIRST, LOG_LINE_TIME_FIRST, createFileLogger, resolveVolatileConfig, } from './store/host.js';
48
- import { ADMIN_API_PREFIX, createSessionAdmin, isInside, isTrustedRequest as isTrustedAdminRequest, mountAdmin, } from './admin.js';
48
+ import { ADMIN_API_PREFIX, __setDirOpenerForTests, createSessionAdmin, isInside, isTrustedRequest as isTrustedAdminRequest, mountAdmin, openSessionDir, sessionHealth, systemDirOpener, } from './admin.js';
49
49
  import { MOVE_API_PREFIX, isTrustedRequest as isTrustedMoveRequest, mountForkMembershipRepair, mountMove, sweepForkChildrenOnBoot, } from './move.js';
50
50
  import { messageOf } from './store/util.js';
51
- export { ADMIN_API_PREFIX, MOVE_API_PREFIX, createSessionAdmin, isInside, isTrustedAdminRequest, isTrustedMoveRequest, mountAdmin, mountForkMembershipRepair, mountMove, sweepForkChildrenOnBoot, };
51
+ export { ADMIN_API_PREFIX, MOVE_API_PREFIX, __setDirOpenerForTests, createSessionAdmin, isInside, isTrustedAdminRequest, isTrustedMoveRequest, mountAdmin, mountForkMembershipRepair, mountMove, openSessionDir, sessionHealth, sweepForkChildrenOnBoot, systemDirOpener, };
52
52
  /**
53
53
  * 搬迁半边原来的公开名(`dsh-session-move` 的 `API_PREFIX = '/session-move/api'`)。
54
54
  *
@@ -219,6 +219,8 @@ export interface SessionHealth {
219
219
  found: boolean;
220
220
  /** 会话日志的物理路径;后端没有实体文件(未落盘)时为 null。 */
221
221
  path: string | null;
222
+ /** 会话日志所在目录(`dirname(path)`);后端没有实体文件时为 null。 */
223
+ dir: string | null;
222
224
  /** 物理文件字节数;后端给不出时为 null。 */
223
225
  sizeBytes: number | null;
224
226
  /** 压缩格式(`'zstd'` / `''` 未压缩);后端不暴露时为 null。 */
@@ -249,6 +251,42 @@ export interface SessionHealth {
249
251
  * @throws 会话 id 不合法(交给路由层转 400)。
250
252
  */
251
253
  export declare function sessionHealth(sessionId: unknown, source: HealthSource, holds: Holds): Promise<SessionHealth>;
254
+ /** 打开一个目录的宿主动作(默认调用系统文件管理器;测试可替换)。 */
255
+ export type DirOpener = (dir: string) => Promise<void>;
256
+ /**
257
+ * 用系统文件管理器打开目录:macOS `open`、Windows `explorer`、其余 `xdg-open`。
258
+ *
259
+ * `detached + unref` 让子进程脱离 Host 的生命周期——面板点一下不该在 Host 里留下
260
+ * 一个常驻子进程;只等 `spawn` 事件确认命令确实起来,不等它退出。
261
+ * @returns 打开动作。
262
+ */
263
+ export declare function systemDirOpener(): DirOpener;
264
+ /**
265
+ * 仅供测试:替换/还原 {@link openSessionDir} 使用的系统打开动作。
266
+ * @param opener - 假的打开动作;传 undefined 还原为真实实现。
267
+ */
268
+ export declare function __setDirOpenerForTests(opener: DirOpener | undefined): void;
269
+ /** `/session-admin/api/openDir` 的结果。 */
270
+ export interface OpenDirResult {
271
+ ok: boolean;
272
+ /** 打开(或失败时想打开)的目录。 */
273
+ dir?: string | null;
274
+ /** `ok:false` 的机器可读原因。 */
275
+ reason?: 'not-found' | 'persistence-unavailable' | 'read-failed' | 'no-directory' | 'open-failed';
276
+ /** `open-failed` 时的错误正文。 */
277
+ message?: string;
278
+ }
279
+ /**
280
+ * 在系统文件管理器里打开一条会话的日志目录。
281
+ *
282
+ * 目录由 Host 从 `sessionPersistence.stat()` 的结果推导,**浏览器不能传任意路径**——
283
+ * 这个动作只可能打开本机会话日志所在目录,不构成通用的「打开任意文件夹」接口。
284
+ * @param sessionId - 请求里的会话 id(会先校验,非法时抛给路由层转 400)。
285
+ * @param source - 持久化后端与归属查询。
286
+ * @param holds - `{resident, running}`。
287
+ * @returns 结构化结果(`ok` + `dir` / 失败原因)。
288
+ */
289
+ export declare function openSessionDir(sessionId: unknown, source: HealthSource, holds: Holds): Promise<OpenDirResult>;
252
290
  /**
253
291
  * Host cordis 上下文里删除半边用到的那部分(最小本地接口,见 `src/index.ts` 的说明)。
254
292
  */
@@ -44,11 +44,11 @@
44
44
  * @module dsh-session-ops
45
45
  */
46
46
  import z from '@deepseek-ai/schemastery';
47
- import { ADMIN_API_PREFIX, createSessionAdmin, isInside, isTrustedRequest as isTrustedAdminRequest, mountAdmin } from './admin.js';
47
+ import { ADMIN_API_PREFIX, __setDirOpenerForTests, createSessionAdmin, isInside, isTrustedRequest as isTrustedAdminRequest, mountAdmin, openSessionDir, sessionHealth, systemDirOpener } from './admin.js';
48
48
  import { MOVE_API_PREFIX, isTrustedRequest as isTrustedMoveRequest, mountForkMembershipRepair, mountMove, sweepForkChildrenOnBoot } from './move.js';
49
49
  import type { WorkspaceRegistryLike } from './store/registry.js';
50
50
  import type { SessionPersistenceLike } from './move.js';
51
- export { ADMIN_API_PREFIX, MOVE_API_PREFIX, createSessionAdmin, isInside, isTrustedAdminRequest, isTrustedMoveRequest, mountAdmin, mountForkMembershipRepair, mountMove, sweepForkChildrenOnBoot, };
51
+ export { ADMIN_API_PREFIX, MOVE_API_PREFIX, __setDirOpenerForTests, createSessionAdmin, isInside, isTrustedAdminRequest, isTrustedMoveRequest, mountAdmin, mountForkMembershipRepair, mountMove, openSessionDir, sessionHealth, sweepForkChildrenOnBoot, systemDirOpener, };
52
52
  /**
53
53
  * 搬迁半边原来的公开名(`dsh-session-move` 的 `API_PREFIX = '/session-move/api'`)。
54
54
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-ops",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"