dsh-oh-my-terminal 0.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 (56) hide show
  1. package/CLAUDE.md +167 -0
  2. package/LICENSE +218 -0
  3. package/README.md +103 -0
  4. package/README.zh.md +101 -0
  5. package/cordis.patch.yml +3 -0
  6. package/docs/git-workflow.md +227 -0
  7. package/docs/type_script_style.md +392 -0
  8. package/lib/client.js +11894 -0
  9. package/lib/index.js +1296 -0
  10. package/lib/xterm.css +285 -0
  11. package/package.json +60 -0
  12. package/pnpm-workspace.yaml +3 -0
  13. package/scripts/build.ts +60 -0
  14. package/src/client/clipboard.ts +148 -0
  15. package/src/client/dropdown.tsx +172 -0
  16. package/src/client/hooks.ts +20 -0
  17. package/src/client/icons.tsx +148 -0
  18. package/src/client/settings/api.ts +190 -0
  19. package/src/client/settings/card.tsx +393 -0
  20. package/src/client/settings/profile-table.tsx +652 -0
  21. package/src/client/settings/store.ts +260 -0
  22. package/src/client/settings/styles.ts +75 -0
  23. package/src/client/settings/types.ts +115 -0
  24. package/src/client/shortcut-bridge.ts +100 -0
  25. package/src/client/side-list.tsx +310 -0
  26. package/src/client/styles.ts +211 -0
  27. package/src/client/term-pane.tsx +282 -0
  28. package/src/client/terminal/reducer.ts +200 -0
  29. package/src/client/terminal/use-panel-geometry.ts +138 -0
  30. package/src/client/terminal/use-tabs.ts +386 -0
  31. package/src/client/terminal/use-terminal-state.ts +119 -0
  32. package/src/client/types.ts +144 -0
  33. package/src/client.tsx +492 -0
  34. package/src/constants.ts +108 -0
  35. package/src/index.ts +539 -0
  36. package/src/logger.ts +115 -0
  37. package/src/persistence.ts +319 -0
  38. package/src/platform.ts +173 -0
  39. package/src/routes.ts +457 -0
  40. package/src/server-command.ts +55 -0
  41. package/src/settings/bridge.ts +341 -0
  42. package/src/settings/index.ts +73 -0
  43. package/src/settings/namespace.ts +51 -0
  44. package/src/settings/patch.ts +101 -0
  45. package/src/shortcut.ts +142 -0
  46. package/src/terminal/detect.ts +146 -0
  47. package/src/terminal/index.ts +44 -0
  48. package/src/terminal/kinds.ts +179 -0
  49. package/src/terminal/resolve.ts +97 -0
  50. package/src/terminal/store.ts +268 -0
  51. package/src/ws-handler.ts +164 -0
  52. package/tests/unit/config-schema.test.ts +119 -0
  53. package/tests/unit/server-command.test.ts +188 -0
  54. package/tests/unit/shortcut-bridge.test.ts +83 -0
  55. package/tests/unit/shortcut.test.ts +371 -0
  56. package/tsconfig.json +21 -0
package/CLAUDE.md ADDED
@@ -0,0 +1,167 @@
1
+ # CLAUDE.md
2
+
3
+ 本文件为 `dsh-oh-my-terminal` 仓库提供 Claude Code (claude.ai/code) 的编码指导。
4
+
5
+ ## 必读文档
6
+
7
+ AI agent 或人类开发者在动手修改任何代码或文档之前,**必须先阅读**以下两份规范:
8
+
9
+ - [docs/git-workflow.md](docs/git-workflow.md) — Git 分支管理与提交规范(分支创建、合并方向、commit message 格式、agent 检查清单)
10
+ - [docs/type_script_style.md](docs/type_script_style.md) — TypeScript 编程规范(注释、命名、模块结构、类型约定)
11
+
12
+ 未读这两份文档就动手改代码视为违规。
13
+
14
+ ## 语言要求
15
+
16
+ 本仓库的所有交流、代码注释、提交信息、文档一律使用**中文**。
17
+
18
+ ## 这是什么
19
+
20
+ `dsh-oh-my-terminal` 是 DSH Web GUI 的底部终端面板插件,基于 `@lydell/node-pty` 提供多终端交互式面板(Windows ConPTY / POSIX openpty),经 WebSocket 与浏览器端的 xterm.js 前端通信。用户通过 `dsh plugin --profile web add dsh-oh-my-terminal` 安装。
21
+
22
+ VSCode 风格界面:+号旁下拉菜单(新建/拆分/按种类新建)、拆分终端(同组水平并排)、右侧终端列表(替代水平 tab 栏)、右键重命名。
23
+
24
+ 架构分为两半加共享工具层:
25
+
26
+ - **宿主半**(`src/index.ts` + `src/routes.ts` + `src/ws-handler.ts`):Cordis 插件入口、HTTP 路由分发、WebSocket 升级与数据转发、settings 集成、会话生命周期管理
27
+ - **浏览器半**(`src/client.tsx` + `src/client/`):React 组件,xterm.js 底部面板,useReducer 统一状态管理、拆分终端、右侧列表、下拉菜单、快捷键、拖拽调高、剪贴板
28
+ - **持久化层**(`src/persistence.ts`):`SessionStore` 封装会话日志落盘/清理、元数据读写、启动恢复
29
+ - **平台适配层**(`src/platform.ts`):`PlatformAdapter` 接口与 POSIX/Windows 适配器,封装 OS 差异,动态探测 Git Bash 与默认 shell
30
+ - **常量**(`src/constants.ts`):协议前缀、尺寸约束、快捷键默认值、环境变量名等具名常量
31
+ - **工具函数**(`src/server-command.ts`、`src/shortcut.ts`、`src/logger.ts`):命令行解析、快捷键解析、统一日志
32
+
33
+ **插件形态必须构建**:dsh loader 经纯 ESM import 加载插件、不走 tsx,所以必须用 `scripts/build.ts` 产出 `lib/` 构建产物。`lib/` 纳入版本控制——pnpm 11 对声明了安装类脚本的 git-hosted 包直接报 `ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED`,且只看 `package.json` 字段、不看脚本内容做什么,所以本仓库**不声明任何生命周期脚本**(`prepare`/`postinstall` 都没有)。构建由开发者手动 `pnpm run build` 完成,产物 `lib/` 随源码一起提交。
34
+
35
+ ## 常用命令
36
+
37
+ **本机开发默认 pnpm 11.7.0**(`package.json` 的 `packageManager` 钉版本)。不要用 npm——peer 依赖钉死具体版本,npm 在残留旧树上做增量解析必报 ERESOLVE。
38
+
39
+ ```bash
40
+ # 类型检查
41
+ pnpm run typecheck
42
+
43
+ # 构建(esbuild 双入口 → lib/index.js + lib/client.js + lib/xterm.css)
44
+ pnpm run build
45
+
46
+ # 单元测试(纯函数模块)
47
+ pnpm exec tsx --test tests/unit/*.test.ts
48
+ ```
49
+
50
+ `pnpm-workspace.yaml` 的 `allowBuilds` 只放行 `esbuild`——`@lydell/node-pty` 的各平台二进制随子包 tarball 分发,安装期既不编译也不下载,所以不需要(也不应)为它放行构建脚本;`minimumReleaseAge: 0` 避免拦截当天发布的依赖。
51
+
52
+ ## 架构
53
+
54
+ ```
55
+ src/
56
+ ├── index.ts # 宿主半入口(cordis 插件壳 + settings 集成 + 会话生命周期 + loadPty 懒加载)
57
+ ├── routes.ts # HTTP 路由处理器工厂(createRouteHandler + 辅助函数)
58
+ ├── ws-handler.ts # WebSocket 处理器工厂(createWsHandlers + WebServer 类型导出)
59
+ ├── client.tsx # 浏览器半入口(TerminalPanel 组合壳 + 插件注册 + injectStyles 调用)
60
+ ├── client/
61
+ │ ├── types.ts # 共享类型定义(TerminalInstance, TerminalGroup, TerminalState, TerminalAction, API 响应)
62
+ │ ├── hooks.ts # 自定义 Hooks(terminalReducer + useTerminalState + useConfig + usePanelHeight + usePanelGeometry + useTerminalTabs)
63
+ │ ├── term-pane.tsx # TermPane 组件(xterm + WebSocket + resize)+ RestartButton 组件
64
+ │ ├── styles.ts # CSS 样式常量(PANEL_CSS)+ Campbell 暗色主题 + xterm 调优常量 + injectStyles()
65
+ │ ├── dropdown.tsx # +号旁下拉菜单组件(新建/拆分/按种类新建)
66
+ │ ├── side-list.tsx # 右侧终端列表面板(树形前缀 + 右键重命名 + useMemo 缓存)
67
+ │ ├── terminal/
68
+ │ │ └── use-tabs.ts # 终端标签页状态管理(useTerminalTabs Hook:newTab/splitTerminal/closeTerminal/renameTerminal)
69
+ │ ├── settings/
70
+ │ │ ├── types.ts # 设置页面类型定义(SettingsPageProps、ConfigWithProfiles)
71
+ │ │ ├── store.ts # 设置页面状态管理(useSettingsState Hook)
72
+ │ │ └── card.tsx # 设置卡片组件(SettingsCard)
73
+ │ ├── icons.tsx # SVG 图标组件集合(10 个纯函数)
74
+ │ └── clipboard.ts # 剪贴板纯函数(Async Clipboard API + legacy 双层降级)
75
+ ├── terminal/
76
+ │ ├── index.ts # 终端模块统一导出(kinds/detect/resolve/store)
77
+ │ ├── kinds.ts # 终端种类定义(TerminalKind 联合类型 + KIND_SPECS 平台映射表)
78
+ │ ├── detect.ts # 终端探测(resolveCommand/detectGitBash/detectTerminalProfiles)
79
+ │ ├── resolve.ts # 配置解析(resolveProfile:TerminalProfile → node-pty spawn 参数)
80
+ │ └── store.ts # 配置表校验(validateAdd/validateUpdate/validateDelete/addProfile/updateProfile/deleteProfile)
81
+ ├── settings/
82
+ │ ├── namespace.ts # Settings 命名空间定义(SettingsNamespace)
83
+ │ └── bridge.ts # Settings 桥接层(prepareProfiles:配置表与探测结果合并)
84
+ ├── persistence.ts # 会话持久化层(SessionStore:日志落盘/清理、元数据读写、启动恢复)
85
+ ├── platform.ts # 平台适配层(PlatformAdapter 接口 + POSIX/Windows 适配器 + 默认 shell 探测)
86
+ ├── constants.ts # 协议/尺寸/快捷键/环境变量/文件名常量
87
+ ├── server-command.ts # 命令行解析工具(shell 命令拆分与转义)
88
+ ├── shortcut.ts # 快捷键解析工具(toggle 快捷键字符串 → KeyboardEvent 匹配)
89
+ └── logger.ts # 统一日志工具(createLogger:结构化 [时间戳][级别][模块] 内容)
90
+ ```
91
+
92
+ ### 依赖方向
93
+
94
+ ```
95
+ 宿主半:
96
+ index.ts → routes.ts → terminal/, settings/, constants.ts, platform.ts, persistence.ts, server-command.ts, logger.ts
97
+ index.ts → ws-handler.ts → constants.ts, persistence.ts
98
+ index.ts → terminal/, settings/, constants.ts, platform.ts, persistence.ts, logger.ts
99
+ settings/bridge.ts → terminal/detect.ts, terminal/kinds.ts, logger.ts
100
+ terminal/detect.ts → terminal/kinds.ts
101
+ terminal/resolve.ts → terminal/kinds.ts, platform.ts
102
+ terminal/store.ts → terminal/kinds.ts
103
+ platform.ts → terminal/detect.ts(复用 resolveCommand)
104
+ persistence.ts → constants.ts, logger.ts
105
+
106
+ 浏览器半:
107
+ client.tsx → client/types.ts, client/icons.tsx, client/clipboard.ts,
108
+ client/dropdown.tsx, client/side-list.tsx, client/hooks.ts,
109
+ client/styles.ts, client/term-pane.tsx, client/settings/,
110
+ client/terminal/, logger.ts
111
+ client/hooks.ts → client/types.ts, shortcut.ts, logger.ts
112
+ client/terminal/use-tabs.ts → client/types.ts, logger.ts
113
+ client/settings/card.tsx → client/settings/types.ts, client/settings/store.ts, client/icons.tsx
114
+ client/settings/store.ts → client/settings/types.ts
115
+ client/dropdown.tsx → client/types.ts, client/icons.tsx
116
+ client/side-list.tsx → client/types.ts, client/icons.tsx
117
+ client/term-pane.tsx → client/types.ts, client/clipboard.ts, client/icons.tsx, client/styles.ts
118
+
119
+ 两半经 WebSocket 通信,路由前缀 /api/dsh-oh-my-terminal,不直接 import。
120
+ @lydell/node-pty 是外部原生依赖,宿主半只在首次创建会话时 await import()。
121
+ ```
122
+
123
+ ### 状态管理
124
+
125
+ 浏览器半使用 `useReducer` 统一管理终端状态(`TerminalState`),消除原先 instances/groups/activeInstanceId 三轨独立 state 的同步负担。所有 CRUD 操作通过 `dispatch(TerminalAction)` 提交,reducer 是纯函数。`/config` 只拉取一次(`useConfig` Hook),同时获取快捷键配置和终端配置表(`terminalProfiles`)。
126
+
127
+ ### 终端配置模型(type / name / path)
128
+
129
+ 终端种类不再硬编码在 `platform.ts`,而是由配置表驱动:
130
+
131
+ | 字段 | 说明 |
132
+ |------|------|
133
+ | `type` | 终端类型(`TerminalKind`):pwsh / powershell / cmd / bash / zsh / fish / gitbash / nushell / custom。决定 spawn 语义与交互参数 |
134
+ | `name` | 下拉菜单显示名。用户可改,跨重启保留 |
135
+ | `path` | 可执行文件路径。留空则按 `type` 在 `$PATH` 中解析 |
136
+ | `origin` | `auto`(启动探测)/ `user`(手动新增)。`auto` 项不可删除、path 不可改,但可改名 |
137
+
138
+ **启动流程**:`prepareProfiles()` 在 apply 时跑一次 `$PATH` 探测(`detectTerminalProfiles()`),与已保存的配置表合并(`mergeProfiles()`:已保存项全保留,探测到的新 type/path 补进去)。首次运行(配置表为空)时把结果写回配置。
139
+
140
+ **路径解析**:`resolveProfile()` 把 `TerminalProfile` 转成 node-pty 的 `{ file, args }`。**Windows 上 node-pty 不做 PATH/PATHEXT 查找**,裸命令名(`pwsh`、`cmd`)会直接以 `File not found: ` 失败,必须给完整路径——`resolveCommand()` 用 `where`/`which` 解析,并过滤 `WindowsApps` 下的 App Execution Alias 占位符(0 字节重解析点,spawn 必失败)。
141
+
142
+ **持久化**:配置表在 `Config` schema 里是 **JSON 字符串**(`terminalProfiles`),不是嵌套数组——schemastery 对对象数组的 round-trip 行为不保证,字符串是唯一稳定形态。空串 = 用启动探测结果。
143
+
144
+ **API 契约**:客户端只传 `profileId`(配置表 id),不传路径与参数。`POST /sessions` 的 `profileId` 字段与宿主半 `routes.ts` 的读取字段名必须一致——曾因客户端发 `terminalType`、宿主半读 `profileId` 导致下拉菜单静默失效(始终回落到默认 shell)。
145
+
146
+ ## 经验教训与硬约束
147
+
148
+ - **插件形态**:宿主产物必须 ESM;命令名匹配 `/^[a-z][a-z0-9_-]*$/`;defineTool 的 object 节点必须写 `additionalProperties`;路由只走已鉴权通道
149
+ - **ESM 合规**:所有源文件是 ESM 模块,**禁止使用 `require()`**——必须用顶层 `import` 或动态 `await import()`。esbuild 会把 `require()` 转成 `__require()` 包装,在 DSH 的 Electron 进程中可能失败(曾导致 Git Bash 探测静默失败)
150
+ - **@lydell/node-pty**:`spawn` 时 `cwd` 必须存在且可访问,否则进程立即退出;Windows 上用 ConPTY,POSIX 上用 openpty,不要手动 `fork`
151
+ - **原生绑定懒加载**:宿主半不写顶层静态 `import { spawn } from '@lydell/node-pty'`。ESM 的异常发生在模块求值期,原生绑定一旦加载失败,整个 `lib/index.js` 就变成不可导入、dsh 报 `failed to import`,插件连 `apply` 都执行不到;改成首次创建会话时 `await import()`,失败被收敛在会话创建这一步,错误消息可读,插件其余路由仍可用
152
+ - **精确钉 `@lydell/node-pty@1.1.0`**:它是 microsoft/node-pty 的预编译分发版(API 同源),N-API 产物一份二进制同时覆盖 ABI 127(Node 22)与 ABI 137(Node 24);但该包 `dist-tags.latest` 指向 1.2.0-beta 系列,所以必须精确写 `1.1.0`,不能用 `^` 或 `latest`
153
+ - **为什么换掉 node-pty**:旧包 tarball 的 `prebuilds/` 只有 darwin 与 win32,Linux 上依赖安装期执行 `node scripts/prebuild.js || node-gyp rebuild`;而 pnpm 10 对未授权的构建脚本只警告并跳过,`build/Release/pty.node` 从未生成。`@lydell/node-pty` 拆六个平台子包、二进制在 tarball 内,运行期不下载不编译,且 `package.json` 连 `scripts` 字段都不存在(不是空对象),pnpm 10/11/12 对没有脚本字段的包都不进入构建授权判断分支
154
+ - **WebSocket**:socket error 必须在 destroy 之前挂 error 监听器,未处理 error 事件会直接掀翻进程;关闭时要成对清理 `data`/`close`/`error` 监听器
155
+ - **xterm.css**:构建时从 `node_modules/@xterm/xterm/css/xterm.css` 复制到 `lib/xterm.css`,由宿主半 serve
156
+ - **终端数据绝不进日志**:pty 输出与 WebSocket 数据帧是用户会话内容,不写日志
157
+ - **CSS overflow:hidden 陷阱**:包含 `position:absolute` 浮层(如下拉菜单)的容器不能设 `overflow:hidden`,否则浮层被裁剪不可见
158
+ - **Windows 上 spawn 必须给完整路径**:node-pty 的 `spawn` 在 Windows 上**不做 PATH/PATHEXT 查找**,传裸命令名(`pwsh`、`cmd`、`bash`)直接以 `File not found: ` 失败(错误消息里的路径是空串,极易误判为 cwd 问题)。所有终端命令都要先经 `where`/`which` 解析成绝对路径,并过滤 `WindowsApps` 下的 App Execution Alias 占位符
159
+ - **跨半字段名必须逐字对齐**:宿主半与浏览器半经 HTTP 通信、无编译期校验,字段名拼写不一致会静默回落到默认分支(曾因客户端发 `terminalType`、宿主半读 `profileId`,导致下拉菜单选了任何类型都开出默认 shell,且无任何报错)
160
+ - **单文件行数阈值**:建议 <600 行。超过时按职责边界拆分到子模块,不要按"太长了"随意切半
161
+
162
+ ## 约束
163
+
164
+ - 客户端可能是 Windows/Linux/macOS,宿主半跑在 dsh 所在机器上。`@lydell/node-pty` 自动按平台选 ConPTY 或 openpty。
165
+ - 不落明文凭据:日志和错误消息不打印密钥、令牌、口令内容。
166
+ - 新增运行时依赖必须写进 `package.json`,版本锁定或用窄范围;带原生绑定的依赖(如 `@lydell/node-pty`)一律精确钉版本,不留 `^` 浮动空间。
167
+ - **禁止声明生命周期脚本**:`prepare`/`postinstall` 等一概不要加——pnpm 11 对声明了安装类脚本的 git-hosted 包直接报 `ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED`,拦截只看 `package.json` 字段、不看脚本内容。反过来,选依赖时也可以用这条判断:`@lydell/node-pty` 就是这个约束的正面对照——它的 `package.json` 里根本没有 `scripts` 字段,pnpm 10 的只警告并跳过、11 起的默认报错退出、12 的现状,对没有脚本的包都不进入判断分支,因此不产生任何版本差异。这是本仓库能在 pnpm 10-12 上装得上、跑得起来的关键。
package/LICENSE ADDED
@@ -0,0 +1,218 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
203
+
204
+ ---
205
+
206
+ Copyright 2026 dsh-oh-my-terminal Authors
207
+
208
+ Licensed under the Apache License, Version 2.0 (the "License");
209
+ you may not use this file except in compliance with the License.
210
+ You may obtain a copy of the License at
211
+
212
+ http://www.apache.org/licenses/LICENSE-2.0
213
+
214
+ Unless required by applicable law or agreed to in writing, software
215
+ distributed under the License is distributed on an "AS IS" BASIS,
216
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
217
+ See the License for the specific language governing permissions and
218
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # dsh-oh-my-terminal
2
+
3
+ [![version](https://img.shields.io/badge/version-0.2.1-blue)](package.json)
4
+ [![license](https://img.shields.io/badge/license-Apache--2.0-green)](LICENSE)
5
+ [![node](https://img.shields.io/badge/node-%5E20.19.0%20%7C%7C%20%3E%3D22.0.0-brightgreen)](package.json)
6
+
7
+ A bottom terminal panel plugin for DSH Web GUI. Powered by `@lydell/node-pty`, it provides multi-session interactive terminals over WebSocket using xterm.js in the browser. Supports Windows ConPTY and POSIX openpty with no local compilation required.
8
+
9
+ [中文文档](README.zh.md)
10
+
11
+ ## Features
12
+
13
+ - **Multiple terminal sessions**: manage several sessions simultaneously; a side list replaces the traditional horizontal tab bar, with right-click rename support
14
+ - **Split terminals**: horizontal splits within the same group; a VSCode-style dropdown next to the `+` button (new / split / new by kind)
15
+ - **Session persistence**: sessions survive panel close/reopen and are automatically restored on startup
16
+ - **Configurable shortcut**: toggle the panel with a keyboard shortcut; on DSH 0.1.7-rc.2+ the plugin integrates with the host shortcut system and the panel label updates to reflect the current binding
17
+ - **Resizable panel**: drag the top edge of the panel to adjust its height
18
+ - **Cross-platform**: Windows ConPTY and POSIX openpty are selected automatically by `@lydell/node-pty`; pre-compiled binaries ship with the package, nothing to compile
19
+ - **Configurable shell**: set a custom shell command and arguments
20
+
21
+ ## Installation
22
+
23
+ This package is not yet published to npm. Install directly from GitHub:
24
+
25
+ ```bash
26
+ dsh plugin --profile web add github:btsd321/dsh-oh-my-terminal
27
+ ```
28
+
29
+ ## Configuration
30
+
31
+ | Option | Description | Default |
32
+ |---|---|---|
33
+ | `toggleShortcut` | Shortcut to toggle the terminal panel (legacy host only; see below) | `` Ctrl+` `` |
34
+ | `shellCommand` | Shell command for new terminals | System default (bash / PowerShell) |
35
+
36
+ ## DSH 0.1.7-rc.2 shortcut changes
37
+
38
+ DSH 0.1.7-rc.2 introduced a host-level shortcut system. Its built-in terminal (`` Ctrl+` `` via `terminal.new`) conflicts with the previous default. To avoid double-triggering, this plugin changed its behavior starting with the rc.2-compatible release:
39
+
40
+ - **Integrated with shortcuts system**: the plugin registers the `terminal-panel.toggle` command with a default binding of `` Ctrl+Shift+` `` (no default on `web:linux`; bind it manually in DSH settings). Change the key in DSH Settings - Keyboard Shortcuts; the panel label updates automatically
41
+ - **Legacy host (0.1.7-rc.1 and earlier)**: falls back to a bare `` Ctrl+` `` listener; the `toggleShortcut` setting still applies
42
+ - **Migration note**: after upgrading the host, `` Ctrl+` `` opens the built-in terminal (side panel). Use `` Ctrl+Shift+` `` for this plugin's panel. To reclaim `` Ctrl+` `` for this plugin, first remove the built-in terminal's binding in DSH keyboard settings
43
+
44
+ ## Development
45
+
46
+ ```bash
47
+ # Install dependencies (@lydell/node-pty ships pre-compiled binaries — ready to use immediately)
48
+ pnpm install
49
+
50
+ # Type check
51
+ pnpm run typecheck
52
+
53
+ # Build (esbuild, two entry points, output to lib/)
54
+ pnpm run build
55
+
56
+ # Unit tests
57
+ pnpm exec tsx --test tests/unit/*.test.ts
58
+ ```
59
+
60
+ > Use `pnpm`, not `npm`. Peer dependencies pin exact versions; npm's incremental resolution on an existing tree will produce ERESOLVE errors.
61
+
62
+ ### About the build output
63
+
64
+ `lib/` is committed to version control. The DSH loader imports plugins as plain ESM; it does not run tsx. This repo also declares no lifecycle scripts (`prepare`, `postinstall`, etc.) because pnpm 11 rejects git-hosted packages that declare install-time scripts with `ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED`. Run `pnpm run build` manually and commit the output alongside source.
65
+
66
+ ### Native dependency notes
67
+
68
+ Terminal capability comes from `@lydell/node-pty` (a pre-compiled distribution of microsoft/node-pty with the same API), pinned exactly to `1.1.0`. The `^` range is intentionally omitted: the package's `dist-tags.latest` points at the 1.2.0-beta series.
69
+
70
+ It is an N-API package. Binaries are split into six platform sub-packages and shipped inside the tarball; no download or compilation happens at install time. A single binary supports both Node 22 and Node 24. Its `package.json` has no `scripts` field at all, so pnpm 10, 11, and 12 never enter the build-authorization path, giving consistent behavior across versions.
71
+
72
+ The host side lazy-loads the package (`await import()` on first session creation). If the native binding fails to load, the error is contained to session creation; the plugin module itself remains importable and all other routes stay available.
73
+
74
+ ## Architecture overview
75
+
76
+ ```
77
+ src/
78
+ ├── index.ts # Host entry (Cordis plugin + settings + session lifecycle)
79
+ ├── routes.ts # HTTP route handlers
80
+ ├── ws-handler.ts # WebSocket handler (pty data forwarding)
81
+ ├── client.tsx # Browser entry (React components + plugin registration)
82
+ ├── client/
83
+ │ ├── types.ts # Shared types
84
+ │ ├── hooks.ts # useReducer state management + custom hooks
85
+ │ ├── term-pane.tsx # xterm.js terminal pane component
86
+ │ ├── dropdown.tsx # Dropdown menu next to the + button
87
+ │ ├── side-list.tsx # Right-side terminal list panel
88
+ │ ├── styles.ts # CSS constants + Campbell dark theme
89
+ │ ├── icons.tsx # SVG icon components
90
+ │ └── clipboard.ts # Clipboard utility functions
91
+ ├── persistence.ts # Session persistence (log storage, metadata, startup restore)
92
+ ├── platform.ts # Platform adapter (POSIX / Windows + shell detection)
93
+ ├── constants.ts # Protocol, size, shortcut, and env var constants
94
+ ├── server-command.ts # Command-line parsing utilities
95
+ ├── shortcut.ts # Shortcut parsing utilities
96
+ └── logger.ts # Structured logger
97
+ ```
98
+
99
+ The host side and browser side communicate over WebSocket at `/api/dsh-oh-my-terminal` and do not import each other directly.
100
+
101
+ ## License
102
+
103
+ [Apache-2.0](LICENSE)
package/README.zh.md ADDED
@@ -0,0 +1,101 @@
1
+ # dsh-oh-my-terminal
2
+
3
+ [![version](https://img.shields.io/badge/version-0.2.1-blue)](package.json)
4
+ [![license](https://img.shields.io/badge/license-Apache--2.0-green)](LICENSE)
5
+ [![node](https://img.shields.io/badge/node-%5E20.19.0%20%7C%7C%20%3E%3D22.0.0-brightgreen)](package.json)
6
+
7
+ DSH Web GUI 的底部终端面板插件,基于 `@lydell/node-pty` 提供多终端交互式面板,经 WebSocket 与浏览器端的 xterm.js 通信。支持 Windows ConPTY 与 POSIX openpty,无需本地编译。
8
+
9
+ ## 功能特性
10
+
11
+ - **多标签终端**:同时管理多个会话,右侧列表替代水平 tab 栏,支持右键重命名
12
+ - **拆分终端**:同组内水平并排,+号旁下拉菜单(新建 / 拆分 / 按种类新建),VSCode 风格操作
13
+ - **会话持久化**:面板收起后会话保持存活,重新打开后自动恢复
14
+ - **快捷键开关**:可配置快捷键呼出/收起面板;DSH 0.1.7-rc.2+ 接入宿主统一快捷键系统,在设置界面改键后标签跟随更新
15
+ - **拖拽调高**:拖拽面板上边缘调整高度
16
+ - **跨平台**:Windows ConPTY / POSIX openpty,由 `@lydell/node-pty` 自动选择,二进制随 tarball 分发,安装即用
17
+ - **可配置 shell**:自定义 shell 命令与启动参数
18
+
19
+ ## 安装
20
+
21
+ 本仓库尚未发布到 npm,通过 GitHub 地址安装:
22
+
23
+ ```bash
24
+ dsh plugin --profile web add github:btsd321/dsh-oh-my-terminal
25
+ ```
26
+
27
+ ## 配置
28
+
29
+ | 配置项 | 说明 | 默认值 |
30
+ |---|---|---|
31
+ | `toggleShortcut` | 呼出/收起终端面板的快捷键(仅旧宿主生效,见下方适配说明) | `` Ctrl+` `` |
32
+ | `shellCommand` | 终端使用的 shell 命令 | 系统默认(bash / PowerShell) |
33
+
34
+ ## DSH 0.1.7-rc.2 快捷键适配说明
35
+
36
+ DSH 0.1.7-rc.2 起宿主自带全局快捷键系统,其内置终端(右侧栏 `terminal.new` 命令)占用了 `` Ctrl+` ``。为避免同一按键双重响应,本插件自 rc.2 适配版起做出以下调整:
37
+
38
+ - **接入 shortcuts 系统**:插件注册 `terminal-panel.toggle` 命令,默认绑定 `` Ctrl+Shift+` ``(`web:linux` 无默认键,需在设置界面手动绑定)。改键在 DSH 设置 - 快捷键中进行,面板提示标签跟随当前有效绑定
39
+ - **旧宿主(0.1.7-rc.1 及更早)**:自动降级为裸 `` Ctrl+` `` 监听,`toggleShortcut` 配置照常生效
40
+ - **迁移指引**:升级宿主后,`` Ctrl+` `` 归内置终端使用,插件面板切换改用 `` Ctrl+Shift+` ``;如需改回 `` Ctrl+` ``,请先在 DSH 快捷键设置中解除内置终端的绑定
41
+
42
+ ## 开发
43
+
44
+ ```bash
45
+ # 安装依赖(@lydell/node-pty 自带各平台预编译二进制,装完即用,无需本地编译)
46
+ pnpm install
47
+
48
+ # 类型检查
49
+ pnpm run typecheck
50
+
51
+ # 构建(esbuild 双入口,产物输出到 lib/)
52
+ pnpm run build
53
+
54
+ # 单元测试
55
+ pnpm exec tsx --test tests/unit/*.test.ts
56
+ ```
57
+
58
+ > 请使用 `pnpm`,不要用 `npm`。peer 依赖钉死具体版本,npm 在旧依赖树上做增量解析会报 ERESOLVE。
59
+
60
+ ### 关于构建产物
61
+
62
+ `lib/` 已纳入版本控制。DSH loader 通过纯 ESM import 加载插件,不走 tsx;同时本仓库不声明 `prepare`/`postinstall` 等生命周期脚本(pnpm 11 对声明了安装类脚本的 git-hosted 包直接报 `ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED`),因此构建产物需开发者手动执行 `pnpm run build` 后随源码一起提交。
63
+
64
+ ### 原生依赖说明
65
+
66
+ 终端能力来自 `@lydell/node-pty`(microsoft/node-pty 的预编译分发版,API 同源),版本精确钉在 `1.1.0`:该包 `dist-tags.latest` 指向 1.2.0-beta 系列,用 `^` 会取到 beta。
67
+
68
+ 它是 N-API 产物,二进制按平台拆成六个子包随 tarball 分发,安装期不下载、不编译,同一份二进制同时支持 Node 22 与 Node 24。其 `package.json` 中不存在 `scripts` 字段,pnpm 10/11/12 对没有脚本字段的包不进入构建授权判断分支,行为一致。
69
+
70
+ 宿主半对它是懒加载(首次创建会话时才 `await import()`):原生绑定若加载失败,异常只影响会话创建,不会让插件模块本身导入失败。
71
+
72
+ ## 架构概览
73
+
74
+ ```
75
+ src/
76
+ ├── index.ts # 宿主半入口(Cordis 插件 + settings + 会话生命周期)
77
+ ├── routes.ts # HTTP 路由处理器
78
+ ├── ws-handler.ts # WebSocket 处理器(pty 数据转发)
79
+ ├── client.tsx # 浏览器半入口(React 组件 + 插件注册)
80
+ ├── client/
81
+ │ ├── types.ts # 共享类型
82
+ │ ├── hooks.ts # useReducer 状态管理 + 自定义 Hooks
83
+ │ ├── term-pane.tsx # xterm.js 终端面板组件
84
+ │ ├── dropdown.tsx # +号旁下拉菜单
85
+ │ ├── side-list.tsx # 右侧终端列表
86
+ │ ├── styles.ts # CSS 常量 + Campbell 暗色主题
87
+ │ ├── icons.tsx # SVG 图标组件
88
+ │ └── clipboard.ts # 剪贴板工具函数
89
+ ├── persistence.ts # 会话持久化层(日志落盘 / 元数据 / 启动恢复)
90
+ ├── platform.ts # 平台适配层(POSIX / Windows + shell 探测)
91
+ ├── constants.ts # 协议 / 尺寸 / 快捷键 / 环境变量常量
92
+ ├── server-command.ts # 命令行解析工具
93
+ ├── shortcut.ts # 快捷键解析工具
94
+ └── logger.ts # 统一日志工具
95
+ ```
96
+
97
+ 宿主半与浏览器半经 WebSocket 通信,路由前缀 `/api/dsh-oh-my-terminal`,不直接 import。
98
+
99
+ ## License
100
+
101
+ [Apache-2.0](LICENSE)
@@ -0,0 +1,3 @@
1
+ - insert:
2
+ - id: terminal-panel
3
+ name: dsh-oh-my-terminal