dsh-github-flow 0.1.4 → 0.1.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.en.md CHANGED
@@ -32,13 +32,36 @@
32
32
  - **Global Cockpit** (`sidebar.panellist`, Order 20): Account-wide repository overview, local workspace matrix, and pending personal PRs/issues.
33
33
  - **Right Sidebar Panel** (`sidebarRightTabs`, Order 40): Shows current repository status, PR list with CI check pills, and a 2x2 quick action grid (Open Repo, View PRs, Create PR, New Issue) with zero popup dependencies.
34
34
  - **Native Light/Dark Dual-Theme Adaptation**: Deeply aligned with DSH official DSW (DeepSeek Web) design tokens, seamlessly reacting to `body[data-ds-dark-theme]` without lag or flickering.
35
- 5. **Standard Cordis Microkernel Compliance**:
35
+ 5. **Robust Environment Diagnosis & Three-State Setup Engine**:
36
+ - Zero-guesswork onboarding: Accurately differentiates `NOT_INSTALLED` (`Missing`), `NOT_LOGGED_IN` (`Required`), and `AUTHENTICATED` (`Active`).
37
+ - OS-aware installation guides: Auto-detects platform (Windows/macOS/Linux) and suggests native package manager commands (`winget install --id GitHub.cli`, `brew install gh`) with official installer links.
38
+ - Interactive helpers: One-click command copying and security-whitelisted terminal launcher.
39
+ - **Focus Auto-Recovery**: Automatically detects authorization completion when returning to the DSH window, transitioning to full workspace view without manual page reloads.
40
+ 6. **Standard Cordis Microkernel Compliance**:
36
41
  - Exposes native `GitHubService` (`ctx.github`) for ecosystem extensibility;
37
42
  - Strongly typed runtime validation via Standard Schema (`Config`);
38
43
  - Domain event broadcasting (`github/pr:create`, `github/issue:create`).
39
44
 
40
45
  ---
41
46
 
47
+ ## 📸 UI Previews
48
+
49
+ ### Global Cockpit (Main Navigation)
50
+ Overview of account-wide repositories, local workspace matrix mapping, and pending personal PRs/issues:
51
+
52
+ <p align="center">
53
+ <img src="./docs/images/1.png" alt="GitHub Global Cockpit" width="100%" />
54
+ </p>
55
+
56
+ ### Session Right Sidebar Panel (In-Session Collaboration)
57
+ Automatically detects current workspace repository, displaying live PR checks, Actions pipelines, and quick action shortcuts:
58
+
59
+ <p align="center">
60
+ <img src="./docs/images/2.png" alt="Session Right Sidebar Panel" width="100%" />
61
+ </p>
62
+
63
+ ---
64
+
42
65
  ## 🚀 Installation
43
66
 
44
67
  Run in your DSH terminal to install directly from npm:
@@ -61,7 +84,7 @@ dsh plugin --profile web add dsh-github-flow
61
84
  | `github_issue` | `list`, `view`, `create`, `comment`, `close`, `reopen` | Search and view issues, create issues, add comments |
62
85
  | `github_run` | `list`, `view`, `log_failed`, `rerun`, `cancel` | Monitor Actions workflows; `log_failed` extracts failure logs for automatic fixes |
63
86
  | `github_repo` | `view`, `search_code`, `search_repos` | Query repository metadata, search cross-repo code |
64
- | `github_api` | Any GitHub REST / GraphQL endpoint | Universal escape hatch for custom queries (supports `--jq`) |
87
+ | `github_api` | Any GitHub REST / GraphQL endpoint | Universal escape hatch for custom queries (supports `--jq`, plus a `raw` response-body mode) |
65
88
 
66
89
  ---
67
90
 
@@ -85,10 +108,17 @@ Customize options in your `cordis.patch.yml` or profile:
85
108
  ghPath: 'gh' # Custom path to gh binary
86
109
  defaultTimeoutMs: 30000 # Timeout in milliseconds
87
110
  maxOutputChars: 24000 # Maximum characters per output (Token guard)
88
- cacheTtlMs: 15000 # Web overview cache time
111
+ maxOutputBytes: 96000 # Hard UTF-8 byte ceiling per output, defaults to maxOutputChars * 4
112
+ cacheTtlMs: 15000 # Single-repo overview cache TTL (ms); the global cockpit caches for 20x this, floor 5 minutes
89
113
  defaultListLimit: 20 # Default page size
90
114
  ```
91
115
 
116
+ > On Web panel refresh cadence: a global cockpit refresh runs a dozen or so `gh` calls
117
+ > (measured 8-13 s on a local machine), so it caches for 5 minutes and re-fetches when you
118
+ > click "🔄 Refresh global data". The workspace matrix only reads local `.git/config` to
119
+ > identify repository ownership and makes no network requests. The `/gh status` command
120
+ > always bypasses the auth cache, so you see the real state immediately after `gh auth login`.
121
+
92
122
  ---
93
123
 
94
124
  ## 🔌 Ecosystem Service Extension (Cordis Service)
package/README.md CHANGED
@@ -32,13 +32,36 @@
32
32
  - **全局驾驶舱**(主导航栏 `sidebar.panellist`,Order 20):透视账号全局仓库、本地工作区矩阵、个人待办 PR 与 Issues;
33
33
  - **右侧栏工作区面板**(`sidebarRightTabs`,Order 40):常驻呈现当前仓库的 PR 状态、Checks 门禁药丸与 2x2 黄金快捷网格(浏览器打开、查看 PRs、快速创建 PR、新建 Issue),一键直达官方比对与创建页,零弹窗依赖;
34
34
  - **全场景双模式原生自适应**:深度契合 DSH 官方 DSW(DeepSeek Web)设计变量规范,通过 `body[data-ds-dark-theme]` 驱动浅色与深色主题实时零延迟平滑切换。
35
- 5. **符合 Cordis 官方微内核架构**:
35
+ 5. **健全的环境诊断与三态就绪引导 (Three-State Setup Engine)**:
36
+ - 告别空状态困惑:精准建立「未安装 CLI (`Missing`)」➜「已安装未登录 (`Required`)」➜「已认证就绪 (`Active`)」三态状态机;
37
+ - 自动识别宿主机操作系统(Windows/macOS/Linux),按平台提供包管理器安装命令(Windows 下 `winget install --id GitHub.cli`、macOS 下 `brew install gh`)与官方安装包直达;
38
+ - 支持一键复制命令与安全白名单系统终端一键唤起;
39
+ - 具备**窗口焦点自愈(Focus Auto-Recovery)**机制:切到外部终端或浏览器授权完毕后,切回 DSH 界面自动探测刷新,无感恢复。
40
+ 6. **符合 Cordis 官方微内核架构**:
36
41
  - 暴露原生 `GitHubService`(`ctx.github`),允许生态其他插件复用;
37
42
  - 具备符合 Standard Schema 规范的运行时配置校验(`Config`);
38
43
  - 支持领域事件系统(`github/pr:create`, `github/issue:create`)。
39
44
 
40
45
  ---
41
46
 
47
+ ## 📸 界面预览
48
+
49
+ ### 全局全景驾驶舱 (Global Cockpit)
50
+ 透视账号云端所有 GitHub 仓库、本地工作区矩阵联动状态,以及待办 PR 和 Issues:
51
+
52
+ <p align="center">
53
+ <img src="./docs/images/1.png" alt="GitHub 全景驾驶舱" width="100%" />
54
+ </p>
55
+
56
+ ### 会话右侧栏工作区面板 (Session Workspace Panel)
57
+ 自动绑定当前工程关联仓库,常驻呈现 PR 状态药丸、Actions 构建流水线与 2x2 黄金操作网格:
58
+
59
+ <p align="center">
60
+ <img src="./docs/images/2.png" alt="右侧栏工作区面板" width="100%" />
61
+ </p>
62
+
63
+ ---
64
+
42
65
  ## 🚀 安装指南
43
66
 
44
67
  在 DSH 终端执行以下命令直接从 npm 安装:
@@ -61,7 +84,7 @@ dsh plugin --profile web add dsh-github-flow
61
84
  | `github_issue` | `list`, `view`, `create`, `comment`, `close`, `reopen` | 查看与搜索 Issue、创建 Issue、追加排查进展评论 |
62
85
  | `github_run` | `list`, `view`, `log_failed`, `rerun`, `cancel` | 查看 CI 构建流水线,特别是 `log_failed` 可直接提取报错日志供 Agent 修复 |
63
86
  | `github_repo` | `view`, `search_code`, `search_repos` | 查看仓库元数据、跨仓库代码搜索 |
64
- | `github_api` | 任意端点(如 `repos/{owner}/{repo}/releases`) | 底层直接调用 REST / GraphQL API(支持 `--jq` 预过滤) |
87
+ | `github_api` | 任意端点(如 `repos/{owner}/{repo}/releases`) | 底层直接调用 REST / GraphQL API(支持 `--jq` 预过滤、`raw` 原始响应体模式) |
65
88
 
66
89
  ---
67
90
 
@@ -85,10 +108,16 @@ dsh plugin --profile web add dsh-github-flow
85
108
  ghPath: 'gh' # GitHub CLI 程序路径(内网或特定环境可指定绝对路径)
86
109
  defaultTimeoutMs: 30000 # 命令执行超时时间(毫秒)
87
110
  maxOutputChars: 24000 # 单次命令最大返回字符数(防 Token 溢出保护)
88
- cacheTtlMs: 15000 # 界面概览数据缓存时间(毫秒)
111
+ maxOutputBytes: 96000 # 单次命令最大返回字节数硬上限,默认 maxOutputChars × 4
112
+ cacheTtlMs: 15000 # 单仓库概览缓存时长(毫秒);全局驾驶舱按此值的 20 倍缓存,下限 5 分钟
89
113
  defaultListLimit: 20 # 默认查询列表返回条数
90
114
  ```
91
115
 
116
+ > 关于 Web 面板的刷新节奏:全局驾驶舱每次刷新要执行十余次 `gh`(本机实测 8-13 秒),
117
+ > 因此它按 5 分钟缓存,并在你点击「🔄 刷新全局数据」时强制重取。
118
+ > 工作区矩阵只读取本地 `.git/config` 识别仓库归属,不发起网络请求。
119
+ > `/gh status` 命令始终绕过认证缓存,保证你 `gh auth login` 后立刻能看到真实状态。
120
+
92
121
  ---
93
122
 
94
123
  ## 🔌 生态服务扩展 (Cordis Service)
@@ -73,3 +73,9 @@
73
73
  - **服务依赖就绪**:声明 `['tools', 'commands', 'webServer', 'workspaceRegistry']`,确保 Web 路由与命令服务在环境就绪后激活;
74
74
  - **生命周期 Effect 闭环**:所有 HTTP 路由与命令注册均持有 Disposer 清理函数,插件热重载或重新挂载时自动彻底注销,零内存泄漏;
75
75
  - **配置自校验与服务暴露**:基于 Standard Schema 规范实现强类型校验与默认值注入,通过 `ctx.provide('github', executor)` 向生态插件暴露单例服务。
76
+
77
+ ### 2.6 三态环境诊断与交互引导状态机 (Setup & Authentication Lifecycle Guard)
78
+ - **三态状态模型 (Three-State Machine)**:将环境就绪判定严格划分为 `NOT_INSTALLED`(未安装 CLI)、`NOT_LOGGED_IN`(已安装未登录)、`AUTHENTICATED`(已认证)三态,彻底消除“把未安装当作未登录”的状态屏蔽隐患;
79
+ - **平台自适应安装策略**:后端探测到 `ENOENT` 时根据 `process.platform` 组装对应平台的安装命令(Windows: `winget`,macOS: `brew`,Linux: `apt/dnf`)与官方安装包地址;
80
+ - **安全辅助与白名单终端唤起**:Web 端提供一键命令复制,后端 `/api/github/action` 提供 `open_terminal` 安全白名单唤起接口(只允许预设命令),避免在前端模拟复杂交互式 PTY 的脆弱性;
81
+ - **焦点感知自愈 (Focus Auto-Recovery)**:前端挂载 `window.focus` 事件监听,用户在外部浏览器或终端完成认证后切回 DSH 界面,自动静默重新探测并无缝更新 UI,实现零摩擦闭环体验。
Binary file
Binary file
@@ -1,2 +1,7 @@
1
1
  import type { GhExecutor } from '../executor.js';
2
- export declare function registerApiRoutes(webServerService: any, executor: GhExecutor, workspaceRegistry?: any): void;
2
+ /**
3
+ * 由用户配置推导全局驾驶舱的缓存时长。
4
+ * 抽成导出的纯函数:TTL 的推导规则是测试断言的对象,埋在闭包里就只能靠猜。
5
+ */
6
+ export declare function globalCacheTtlMs(cacheTtlMs: number): number;
7
+ export declare function registerApiRoutes(ctxOrWebServer: any, executor: GhExecutor, workspaceRegistry?: any, cacheTtlMs?: number): void;
package/lib/api/routes.js CHANGED
@@ -1,16 +1,89 @@
1
- const cacheMap = new Map();
2
- const CACHE_TTL_MS = 15_000;
3
- export function registerApiRoutes(webServerService, executor, workspaceRegistry) {
4
- if (!webServerService || typeof webServerService.register !== 'function')
1
+ import path from 'node:path';
2
+ import { TtlCache } from '../cache.js';
3
+ import { mapLimit, tryReadWorkspaces, findWorkspacePathBySession, findMostRecentWorkspacePath, hasGitDir } from '../workspace.js';
4
+ /** path.basename 在空串/异常输入上会抛错,工作区路径来自磁盘文件,必须防御 */
5
+ function basenameSafe(p) {
6
+ try {
7
+ return path.basename(p) || p;
8
+ }
9
+ catch {
10
+ return p;
11
+ }
12
+ }
13
+ /** 探测单个工作区时在途的 gh 进程数上限;libuv 线程池默认 4,留出余量给其他请求 */
14
+ const WORKSPACE_PROBE_CONCURRENCY = 3;
15
+ /**
16
+ * 全局驾驶舱的缓存时长下限。
17
+ * 这个端点要 spawn 十几次 gh,比单仓库概览重得多,因此不直接沿用 cacheTtlMs(默认 15s)。
18
+ * 定在 5 分钟的依据:它展示的是「我的仓库 / 待办 PR / Issues」这类分钟级不变的数据,
19
+ * 而每次刷新本机实测要 8-13 秒。缓存太短等于让用户反复等这段延迟——实测 60 秒缓存下,
20
+ * 用户 65 秒后再打开面板仍需等 9.7 秒。仍随 cacheTtlMs 缩放,用户调大时这里同步变长。
21
+ */
22
+ const GLOBAL_CACHE_MIN_TTL_MS = 300_000;
23
+ /**
24
+ * 由用户配置推导全局驾驶舱的缓存时长。
25
+ * 抽成导出的纯函数:TTL 的推导规则是测试断言的对象,埋在闭包里就只能靠猜。
26
+ */
27
+ export function globalCacheTtlMs(cacheTtlMs) {
28
+ const base = Number.isFinite(cacheTtlMs) && cacheTtlMs >= 0 ? cacheTtlMs : 15_000;
29
+ return Math.max(base * 20, GLOBAL_CACHE_MIN_TTL_MS);
30
+ }
31
+ /** open_browser 只允许 http/https,杜绝 file:// 与自定义协议被当作打开目标 */
32
+ function isSafeExternalUrl(raw) {
33
+ if (typeof raw !== 'string' || raw.length === 0 || raw.length > 2048)
34
+ return false;
35
+ try {
36
+ const parsed = new URL(raw);
37
+ return parsed.protocol === 'http:' || parsed.protocol === 'https:';
38
+ }
39
+ catch {
40
+ return false;
41
+ }
42
+ }
43
+ export function registerApiRoutes(ctxOrWebServer, executor, workspaceRegistry, cacheTtlMs = 15_000) {
44
+ const ctx = ctxOrWebServer;
45
+ const webServer = ctx?.webServer || ctx;
46
+ if (!webServer || typeof webServer.register !== 'function')
5
47
  return;
48
+ const cacheMap = new TtlCache(cacheTtlMs);
49
+ const globalCache = new TtlCache(globalCacheTtlMs(cacheTtlMs));
6
50
  function sendJson(res, status, data) {
7
51
  res.statusCode = status;
8
52
  res.setHeader('Content-Type', 'application/json; charset=utf-8');
9
53
  res.setHeader('Cache-Control', 'no-store');
10
54
  res.end(JSON.stringify(data));
11
55
  }
56
+ /**
57
+ * 注册单个路由并返回注销器。
58
+ * 为什么需要前置探测与清理:Cordis 插件在经历热重载、异常崩溃或重新启用时,若先前的 disposer
59
+ * 未能顺利执行,宿主全局长生命周期的 WebServer 内部 exact/prefixes 映射表中会残留历史路由;
60
+ * 而 WebServer.register 会在键冲突时直接抛出 duplicate route 异常阻断激活。
61
+ * 前置探测并清理残留条目可实现自愈防护与安全幂等注册。
62
+ */
63
+ const registerOneRoute = (route) => {
64
+ const table = route.kind === 'exact' ? webServer.exact : webServer.prefixes;
65
+ if (table && typeof table.delete === 'function' && table.has(route.path)) {
66
+ table.delete(route.path);
67
+ }
68
+ return webServer.register(route);
69
+ };
70
+ /**
71
+ * 将路由生命周期绑定到 Cordis 上下文作用域。
72
+ * 为什么必须包裹在 ctx.effect 中:Cordis 框架要求外部服务的绑定通过 effect 纳管,
73
+ * 当插件被禁用、配置更新热重载或 scope 销毁时,Cordis 会自动执行返回的 disposer,
74
+ * 干净地从 WebServer 中注销路由,彻底杜绝孤儿路由残留与重复注册报错。
75
+ * 当在缺乏 ctx.effect 的简易单元测试环境下运行时,优雅退化为直接注册。
76
+ */
77
+ const bindRoute = (route) => {
78
+ if (ctx && typeof ctx.effect === 'function') {
79
+ ctx.effect(() => registerOneRoute(route), `github-flow: ${route.kind} ${route.path}`);
80
+ }
81
+ else {
82
+ registerOneRoute(route);
83
+ }
84
+ };
12
85
  // 1. 获取全局与当前仓库的 GitHub 概览
13
- webServerService.register({
86
+ bindRoute({
14
87
  kind: 'exact',
15
88
  path: '/api/github/overview',
16
89
  handler: async (req, res) => {
@@ -22,58 +95,20 @@ export function registerApiRoutes(webServerService, executor, workspaceRegistry)
22
95
  const url = new URL(req.url || '', 'http://127.0.0.1');
23
96
  let cwd = url.searchParams.get('cwd');
24
97
  const sessionId = url.searchParams.get('sessionId');
25
- // 1. 优先通过 sessionId 从 workspace.json 逆向解析物理路径
98
+ // 会话 ID → 物理路径,以及「最近活跃工作区」兜底,统一由 workspace 模块提供
26
99
  if (!cwd && sessionId) {
27
- try {
28
- const fs = await import('node:fs');
29
- const path = await import('node:path');
30
- const os = await import('node:os');
31
- const wsJsonPath = path.join(os.homedir(), '.dsh', 'storages', 'workspace.json');
32
- if (fs.existsSync(wsJsonPath)) {
33
- const wsData = JSON.parse(fs.readFileSync(wsJsonPath, 'utf8'));
34
- const workspaces = wsData.tables?.workspaces || {};
35
- for (const wsId of Object.keys(workspaces)) {
36
- const item = workspaces[wsId];
37
- if (item.sessionIds && item.sessionIds.includes(sessionId)) {
38
- cwd = item.path;
39
- break;
40
- }
41
- }
42
- }
43
- }
44
- catch (e) { }
100
+ cwd = findWorkspacePathBySession(sessionId) || null;
45
101
  }
46
- // 2. 若仍未获取,从 workspace.json 自动匹配最近活跃的工作区
47
102
  if (!cwd) {
48
- try {
49
- const fs = await import('node:fs');
50
- const path = await import('node:path');
51
- const os = await import('node:os');
52
- const wsJsonPath = path.join(os.homedir(), '.dsh', 'storages', 'workspace.json');
53
- if (fs.existsSync(wsJsonPath)) {
54
- const wsData = JSON.parse(fs.readFileSync(wsJsonPath, 'utf8'));
55
- const workspaces = wsData.tables?.workspaces || {};
56
- let latestItem = null;
57
- for (const wsId of Object.keys(workspaces)) {
58
- const item = workspaces[wsId];
59
- if (!latestItem || new Date(item.updatedAt || 0) > new Date(latestItem.updatedAt || 0)) {
60
- latestItem = item;
61
- }
62
- }
63
- if (latestItem && latestItem.path) {
64
- cwd = latestItem.path;
65
- }
66
- }
67
- }
68
- catch (e) { }
103
+ cwd = findMostRecentWorkspacePath() || null;
69
104
  }
70
105
  if (!cwd) {
71
106
  cwd = process.cwd();
72
107
  }
73
108
  const now = Date.now();
74
- const cached = cacheMap.get(cwd);
75
- if (cached && now - cached.time < CACHE_TTL_MS) {
76
- sendJson(res, 200, { ok: true, data: cached.data, fromCache: true, cwd });
109
+ const cachedData = cacheMap.get(cwd, now);
110
+ if (cachedData) {
111
+ sendJson(res, 200, { ok: true, data: cachedData, fromCache: true, cwd });
77
112
  return;
78
113
  }
79
114
  const auth = await executor.checkAuth(cwd);
@@ -84,9 +119,9 @@ export function registerApiRoutes(webServerService, executor, workspaceRegistry)
84
119
  if (repo && auth.loggedIn) {
85
120
  // 并发拉取当前文件夹仓库的 PRs、Issues 和 Runs
86
121
  const [prsRes, issuesRes, runsRes] = await Promise.all([
87
- executor.run(['pr', 'list', '--json', 'number,title,state,author,headRefName,baseRefName,isDraft,mergeable,reviewDecision,statusCheckRollup,url,updatedAt', '-L', '10'], { cwd, timeoutMs: 12_000 }),
88
- executor.run(['issue', 'list', '--json', 'number,title,state,author,labels,assignees,url,updatedAt', '-L', '10'], { cwd, timeoutMs: 12_000 }),
89
- executor.run(['run', 'list', '--json', 'databaseId,name,status,conclusion,event,headBranch,url,createdAt', '-L', '5'], { cwd, timeoutMs: 12_000 }),
122
+ executor.run(['pr', 'list', '--json', 'number,title,state,author,headRefName,baseRefName,isDraft,mergeable,reviewDecision,statusCheckRollup,url,updatedAt', '-L', '10'], { cwd, timeoutMs: 8_000 }),
123
+ executor.run(['issue', 'list', '--json', 'number,title,state,author,labels,assignees,url,updatedAt', '-L', '10'], { cwd, timeoutMs: 8_000 }),
124
+ executor.run(['run', 'list', '--json', 'databaseId,name,status,conclusion,event,headBranch,url,createdAt', '-L', '5'], { cwd, timeoutMs: 8_000 }),
90
125
  ]);
91
126
  if (prsRes.ok && Array.isArray(prsRes.data))
92
127
  pullRequests = prsRes.data;
@@ -103,7 +138,7 @@ export function registerApiRoutes(webServerService, executor, workspaceRegistry)
103
138
  runs,
104
139
  lastUpdated: new Date().toISOString(),
105
140
  };
106
- cacheMap.set(cwd, { data: freshData, time: now });
141
+ cacheMap.set(cwd, freshData, now);
107
142
  sendJson(res, 200, { ok: true, data: freshData, fromCache: false, cwd });
108
143
  }
109
144
  catch (err) {
@@ -112,16 +147,17 @@ export function registerApiRoutes(webServerService, executor, workspaceRegistry)
112
147
  },
113
148
  });
114
149
  // 2. 强制刷新缓存
115
- webServerService.register({
150
+ bindRoute({
116
151
  kind: 'exact',
117
152
  path: '/api/github/refresh',
118
153
  handler: async (req, res) => {
119
154
  cacheMap.clear();
155
+ globalCache.clear();
120
156
  sendJson(res, 200, { ok: true, message: '缓存已清空,下次读取将拉取最新数据' });
121
157
  },
122
158
  });
123
159
  // 3. 右侧栏快捷操作代理接口
124
- webServerService.register({
160
+ bindRoute({
125
161
  kind: 'exact',
126
162
  path: '/api/github/action',
127
163
  handler: async (req, res) => {
@@ -155,14 +191,62 @@ export function registerApiRoutes(webServerService, executor, workspaceRegistry)
155
191
  break;
156
192
  }
157
193
  case 'open_browser': {
158
- if (!payload.url)
159
- throw new Error('缺少 url 参数');
160
- const { exec } = await import('node:child_process');
161
- const openCmd = process.platform === 'win32' ? `start "" "${payload.url}"` : (process.platform === 'darwin' ? `open "${payload.url}"` : `xdg-open "${payload.url}"`);
162
- exec(openCmd);
194
+ // 原实现把 payload.url 直接拼进 shell 字符串再交给 exec(),
195
+ // 形如 https://x" & calc & " 的 URL 可闭合引号并追加任意命令——本机任意页面
196
+ // 都能通过 POST 这个端点触发命令执行。
197
+ // 现在改为:先用 URL 解析白名单协议,再用 execFile 参数数组传递,全程不经过 shell。
198
+ if (!isSafeExternalUrl(payload.url)) {
199
+ throw new Error('url 参数无效:只允许 http/https 开头的完整地址');
200
+ }
201
+ const { execFile } = await import('node:child_process');
202
+ const [bin, args] = process.platform === 'win32'
203
+ ? ['cmd', ['/c', 'start', '', payload.url]]
204
+ : process.platform === 'darwin'
205
+ ? ['open', [payload.url]]
206
+ : ['xdg-open', [payload.url]];
207
+ // 不 await,也不让打开失败反向影响接口语义;仅记录以便排查
208
+ execFile(bin, args, (err) => {
209
+ if (err)
210
+ console.warn('[dsh-github-flow] 打开浏览器失败:', err?.message || err);
211
+ });
163
212
  sendJson(res, 200, { ok: true, message: '已在浏览器打开' });
164
213
  break;
165
214
  }
215
+ case 'open_terminal': {
216
+ const command = typeof payload.command === 'string' ? payload.command.trim() : 'gh auth login';
217
+ // 严格白名单过滤,严防任意命令注入
218
+ const allowedCommands = [
219
+ 'gh auth login',
220
+ 'winget install --id GitHub.cli',
221
+ 'brew install gh',
222
+ 'sudo apt install gh',
223
+ 'sudo dnf install gh',
224
+ ];
225
+ if (!allowedCommands.includes(command)) {
226
+ throw new Error(`不支持唤起该指令: ${command}`);
227
+ }
228
+ const { execFile } = await import('node:child_process');
229
+ let bin = 'cmd';
230
+ let args = [];
231
+ if (process.platform === 'win32') {
232
+ bin = 'cmd';
233
+ args = ['/c', 'start', 'powershell', '-NoExit', '-Command', command];
234
+ }
235
+ else if (process.platform === 'darwin') {
236
+ bin = 'osascript';
237
+ args = ['-e', `tell application "Terminal" to do script "${command}"`, '-e', 'tell application "Terminal" to activate'];
238
+ }
239
+ else {
240
+ bin = 'x-terminal-emulator';
241
+ args = ['-e', command];
242
+ }
243
+ execFile(bin, args, (err) => {
244
+ if (err)
245
+ console.warn('[dsh-github-flow] 唤起终端失败:', err?.message || err);
246
+ });
247
+ sendJson(res, 200, { ok: true, message: '已请求唤起系统终端' });
248
+ break;
249
+ }
166
250
  default:
167
251
  sendJson(res, 400, { ok: false, message: `未知的操作类型: ${payload.action}` });
168
252
  }
@@ -174,7 +258,7 @@ export function registerApiRoutes(webServerService, executor, workspaceRegistry)
174
258
  },
175
259
  });
176
260
  // 4. 获取全局视角的驾驶舱概览数据 (不绑定单一项目)
177
- webServerService.register({
261
+ bindRoute({
178
262
  kind: 'exact',
179
263
  path: '/api/github/global-overview',
180
264
  handler: async (req, res) => {
@@ -182,18 +266,29 @@ export function registerApiRoutes(webServerService, executor, workspaceRegistry)
182
266
  sendJson(res, 405, { ok: false, message: '仅支持 GET 请求' });
183
267
  return;
184
268
  }
269
+ // 全局驾驶舱要 spawn 十几次 gh(认证 + 仓库 + PR + Issue + 每个工作区探测),
270
+ // 实测冷启动曾达 18.9 秒。缓存是这里唯一的解药:面板每次挂载都会来取一次。
271
+ const now = Date.now();
272
+ const cachedGlobal = globalCache.get('global', now);
273
+ if (cachedGlobal) {
274
+ sendJson(res, 200, { ok: true, data: cachedGlobal, fromCache: true });
275
+ return;
276
+ }
185
277
  try {
186
278
  const auth = await executor.checkAuth();
187
279
  let userRepos = [];
188
280
  let myPrs = [];
189
281
  let myIssues = [];
190
282
  let workspaceMatrix = [];
283
+ let workspaceError;
191
284
  if (auth.loggedIn) {
192
285
  // 并发执行:1. 账号下仓库列表 2. 个人 PRs 3. 个人 Issues
286
+ // 超时压到 8 秒:本机实测 `gh search issues --author=@me` 单次就要 10 秒,
287
+ // 它是这组的木桶短板,直接决定首屏等待时间。面板查询不值得为它多等 4 秒。
193
288
  const [reposRes, prsRes, issuesRes] = await Promise.all([
194
- executor.run(['repo', 'list', '--limit', '20', '--json', 'name,nameWithOwner,description,defaultBranchRef,isPrivate,stargazerCount,updatedAt,url'], { timeoutMs: 12_000 }),
195
- executor.run(['search', 'prs', '--author=@me', '--state=open', '--limit', '10', '--json', 'number,title,repository,url,updatedAt'], { timeoutMs: 12_000 }),
196
- executor.run(['search', 'issues', '--author=@me', '--state=open', '--limit', '10', '--json', 'number,title,repository,url,updatedAt'], { timeoutMs: 12_000 }),
289
+ executor.run(['repo', 'list', '--limit', '20', '--json', 'name,nameWithOwner,description,defaultBranchRef,isPrivate,stargazerCount,updatedAt,url'], { timeoutMs: 8_000 }),
290
+ executor.run(['search', 'prs', '--author=@me', '--state=open', '--limit', '10', '--json', 'number,title,repository,url,updatedAt'], { timeoutMs: 8_000 }),
291
+ executor.run(['search', 'issues', '--author=@me', '--state=open', '--limit', '10', '--json', 'number,title,repository,url,updatedAt'], { timeoutMs: 8_000 }),
197
292
  ]);
198
293
  if (reposRes.ok && Array.isArray(reposRes.data)) {
199
294
  userRepos = reposRes.data.map((r) => ({
@@ -214,36 +309,27 @@ export function registerApiRoutes(webServerService, executor, workspaceRegistry)
214
309
  myIssues = issuesRes.data;
215
310
  }
216
311
  }
217
- // 4. 解析本地所有工作区与 GitHub 关联矩阵
218
- try {
219
- const fs = await import('node:fs');
220
- const path = await import('node:path');
221
- const os = await import('node:os');
222
- const wsJsonPath = path.join(os.homedir(), '.dsh', 'storages', 'workspace.json');
223
- if (fs.existsSync(wsJsonPath)) {
224
- const wsData = JSON.parse(fs.readFileSync(wsJsonPath, 'utf8'));
225
- const workspaces = wsData.tables?.workspaces || {};
226
- const wsEntries = Object.keys(workspaces).map((id) => ({ id, ...workspaces[id] }));
227
- // 并发探测每个工作区路径的 Git Remote
228
- const matrixResults = await Promise.all(wsEntries.map(async (ws) => {
229
- const hasGit = fs.existsSync(path.join(ws.path, '.git'));
230
- let repoMeta = undefined;
231
- if (hasGit) {
232
- repoMeta = (await executor.getRepoMetadata(ws.path)) || undefined;
233
- }
234
- return {
235
- id: ws.id,
236
- title: ws.title || path.basename(ws.path),
237
- path: ws.path,
238
- hasGit,
239
- repo: repoMeta,
240
- sessionCount: (ws.sessionIds && ws.sessionIds.length) || 0,
241
- };
242
- }));
243
- workspaceMatrix = matrixResults;
244
- }
312
+ // 解析本地所有工作区与 GitHub 关联矩阵
313
+ const wsRead = tryReadWorkspaces();
314
+ workspaceError = wsRead.error;
315
+ if (!workspaceError) {
316
+ const wsEntries = wsRead.workspaces.filter((ws) => ws.path);
317
+ // 有界并发探测:每个工作区最多 spawn 2 个 gh 进程,无上限并发会让它们全挤进
318
+ // libuv 默认 4 线程的队列里排队,延迟随工作区数量线性劣化。
319
+ // 这里刻意只用本地 .git/config 识别身份、不发网络请求——全局矩阵只需要「属于哪个仓库」,
320
+ // 每次网络往返都要秒级,5 个工作区就足以把该端点拖到 20 秒。
321
+ workspaceMatrix = await mapLimit(wsEntries, WORKSPACE_PROBE_CONCURRENCY, async (ws) => {
322
+ const hasGit = hasGitDir(ws.path);
323
+ return {
324
+ id: ws.id,
325
+ title: ws.title || basenameSafe(ws.path),
326
+ path: ws.path,
327
+ hasGit,
328
+ repo: hasGit ? executor.getRepoIdentityFromLocal(ws.path) || undefined : undefined,
329
+ sessionCount: ws.sessionIds.length,
330
+ };
331
+ });
245
332
  }
246
- catch (e) { }
247
333
  const globalData = {
248
334
  auth,
249
335
  userRepos,
@@ -251,8 +337,10 @@ export function registerApiRoutes(webServerService, executor, workspaceRegistry)
251
337
  myPrs,
252
338
  myIssues,
253
339
  lastUpdated: new Date().toISOString(),
340
+ ...(workspaceError ? { workspaceError } : {}),
254
341
  };
255
- sendJson(res, 200, { ok: true, data: globalData });
342
+ globalCache.set('global', globalData, now);
343
+ sendJson(res, 200, { ok: true, data: globalData, fromCache: false });
256
344
  }
257
345
  catch (err) {
258
346
  sendJson(res, 500, { ok: false, error: err.message || '获取全局驾驶舱数据失败' });
package/lib/cache.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 有上限的 TTL 缓存。
3
+ *
4
+ * 为什么独立成模块:缓存语义(过期、LRU 腾退、容量上限)是缺陷高发区,但嵌在路由文件里
5
+ * 就无法单测——import 一个已执行过的路由模块拿不到干净实例。独立出来才能覆盖边界。
6
+ */
7
+ export declare class TtlCache<T> {
8
+ private ttlMs;
9
+ private maxEntries;
10
+ private store;
11
+ /**
12
+ * @param ttlMs 命中有效期;由面向用户的 cacheTtlMs 配置驱动
13
+ * @param maxEntries 容量上限。键通常是工作区路径,一次会话就可能引入若干键,
14
+ * 没有上限时长期运行的 Web 服务会慢性增长(旧实现只有 /refresh 会清空)
15
+ */
16
+ constructor(ttlMs: number, maxEntries?: number);
17
+ get(key: string, now: number): T | undefined;
18
+ set(key: string, data: T, now: number): void;
19
+ get size(): number;
20
+ clear(): void;
21
+ }
package/lib/cache.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * 有上限的 TTL 缓存。
3
+ *
4
+ * 为什么独立成模块:缓存语义(过期、LRU 腾退、容量上限)是缺陷高发区,但嵌在路由文件里
5
+ * 就无法单测——import 一个已执行过的路由模块拿不到干净实例。独立出来才能覆盖边界。
6
+ */
7
+ export class TtlCache {
8
+ ttlMs;
9
+ maxEntries;
10
+ store = new Map();
11
+ /**
12
+ * @param ttlMs 命中有效期;由面向用户的 cacheTtlMs 配置驱动
13
+ * @param maxEntries 容量上限。键通常是工作区路径,一次会话就可能引入若干键,
14
+ * 没有上限时长期运行的 Web 服务会慢性增长(旧实现只有 /refresh 会清空)
15
+ */
16
+ constructor(ttlMs, maxEntries = 64) {
17
+ this.ttlMs = ttlMs;
18
+ this.maxEntries = maxEntries;
19
+ }
20
+ get(key, now) {
21
+ const hit = this.store.get(key);
22
+ if (!hit)
23
+ return undefined;
24
+ if (now - hit.time >= this.ttlMs) {
25
+ this.store.delete(key);
26
+ return undefined;
27
+ }
28
+ // 命中后重新插入:Map 的迭代顺序即插入顺序,重插让顺序逼近 LRU,腾退时淘汰最久未用者
29
+ this.store.delete(key);
30
+ this.store.set(key, hit);
31
+ return hit.data;
32
+ }
33
+ set(key, data, now) {
34
+ this.store.set(key, { data, time: now });
35
+ while (this.store.size > this.maxEntries) {
36
+ const oldest = this.store.keys().next();
37
+ if (oldest.done)
38
+ break;
39
+ this.store.delete(oldest.value);
40
+ }
41
+ }
42
+ get size() {
43
+ return this.store.size;
44
+ }
45
+ clear() {
46
+ this.store.clear();
47
+ }
48
+ }