claude-dev-server 1.0.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.
package/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # claude-dev-server
2
+
3
+ > 通用开发服务器包装器 - 在浏览器中集成 Claude Code AI 助手终端,支持 Vite、Next.js、Webpack 等所有开发服务器
4
+
5
+ [![npm](https://img.shields.io/npm/v/claude-dev-server)](https://www.npmjs.org/package/claude-dev-server)
6
+
7
+ ## 功能特性
8
+
9
+ - **通用支持** - 适用于 Vite、Next.js、Webpack 或任何 dev server
10
+ - **ttyd 终端** - 使用 ttyd 提供真正的 PTY 终端体验,完整支持 ANSI 转义序列、颜色、光标控制
11
+ - **Claude Code 集成** - 通过 WebSocket 与后台 Claude Code 进程通信,实时 AI 辅助开发
12
+ - **元素检查器** - 点击页面元素,自动定位到对应源代码位置,并发送到 Claude Code 终端
13
+ - **智能提示格式** - 自动格式化选中元素为 `@filename:line <selector> "text content"` 格式
14
+ - **无需安装** - 直接使用 `npx` 运行,自动检测项目类型
15
+ - **快捷键支持** - `Cmd/Ctrl + \\`` 打开/关闭面板,`Escape` 退出检查模式
16
+ - **悬浮按钮** - Claude Code 图标按钮打开面板,Inspect 按钮进入检查模式
17
+
18
+ ## 快速开始
19
+
20
+ ### 前置要求
21
+
22
+ 需要安装 `ttyd`:
23
+
24
+ **macOS:**
25
+ ```bash
26
+ brew install ttyd
27
+ ```
28
+
29
+ **Linux:**
30
+ ```bash
31
+ # Ubuntu/Debian
32
+ sudo apt install ttyd
33
+
34
+ # 或从源码编译
35
+ # 见 https://tsl0922.github.io/ttyd/
36
+ ```
37
+
38
+ ### 使用方法
39
+
40
+ 在任意项目目录下运行:
41
+
42
+ ```bash
43
+ # 默认端口 3000
44
+ npx claude-dev-server
45
+
46
+ # 指定端口
47
+ npx claude-dev-server --port 4000
48
+
49
+ # 指定 Claude 路径
50
+ npx claude-dev-server --claude-path /path/to/claude
51
+ ```
52
+
53
+ 就这么简单!工具会:
54
+ 1. 自动检测项目类型(Vite/Next/Webpack/Custom)
55
+ 2. 启动对应的 dev server
56
+ 3. 自动检测端口号(从 stdout 解析)
57
+ 4. 启动 Claude Code 集成服务器
58
+ 5. 打开浏览器即可使用
59
+
60
+ ## CLI 选项
61
+
62
+ | 选项 | 说明 | 默认值 |
63
+ |------|------|--------|
64
+ | `-p, --port <port>` | 代理服务器端口 | `3000` |
65
+ | `--claude-path <path>` | Claude 可执行文件路径 | `claude` |
66
+ | `-h, --help` | 显示帮助信息 | - |
67
+
68
+ ## 使用方法
69
+
70
+ ### 悬浮按钮
71
+
72
+ 页面右上角会显示两个悬浮按钮:
73
+
74
+ | 按钮 | 功能 |
75
+ |------|------|
76
+ | **Claude Code 图标** | 打开/关闭 Claude Code 面板 |
77
+ | **Inspect 图标** | 进入/退出元素检查模式 |
78
+
79
+ ### 键盘快捷键
80
+
81
+ | 快捷键 | 说明 |
82
+ |--------|--------|
83
+ | `Cmd/Ctrl + \\`` | 打开/关闭 Claude Code 面板 |
84
+ | `Escape` | 退出检查模式或关闭面板 |
85
+
86
+ ### 工作流程
87
+
88
+ 1. 按 `Cmd/Ctrl + \\`` 或点击 Claude Code 图标打开面板
89
+ 2. 在终端中直接与 Claude Code 交互
90
+ 3. 点击 **Inspect** 按钮进入检查模式
91
+ 4. 鼠标移动到页面元素上会高亮显示
92
+ 5. 点击元素后自动发送格式化提示到终端:
93
+ ```
94
+ @src/App.tsx:12 <button className="btn"> "Submit"
95
+ ```
96
+ 6. Claude Code 会根据代码位置上下文回答你的问题
97
+
98
+ ## 提示格式说明
99
+
100
+ 选中元素后,会自动生成以下格式的提示:
101
+
102
+ ```
103
+ @<filename>:<line> <selector> "text content"
104
+ ```
105
+
106
+ - `@filename:line` - 源文件路径和行号(支持 React DevTools、Vue 组件)
107
+ - `<selector>` - CSS 选择器(标签名 + ID + 类名)
108
+ - `"text content"` - 元素文本内容(如果存在)
109
+
110
+ ## 架构
111
+
112
+ ```
113
+ ┌─────────────────────────────────────────────────────────────┐
114
+ │ Claude Dev Server (主进程) │
115
+ │ │
116
+ │ ┌──────────────────┐ ┌──────────────────┐ │
117
+ │ │ HTTP Proxy │ │ Control Server │ │
118
+ │ │ (Port :3000) │ │ (WebSocket) │ │
119
+ │ │ │ │ │ │
120
+ │ │ - 代理转发 │ │ - ttyd 管理 │ │
121
+ │ │ - HTML 注入 │◄───────►│ - Claude 进程 │ │
122
+ │ │ - 中间件 │ │ │ │
123
+ │ └────────┬─────────┘ └──────────────────┘ │
124
+ │ │ │
125
+ │ ▼ │
126
+ │ ┌──────────────────┐ │
127
+ │ │ Target Server │ │
128
+ │ │ (Auto detected) │ │
129
+ │ │ │ │
130
+ │ │ Vite / Next │ │
131
+ │ │ Webpack / etc │ │
132
+ │ └──────────────────┘ │
133
+ └─────────────────────────────────────────────────────────────┘
134
+
135
+
136
+ ┌─────────────────┐
137
+ │ Browser │
138
+ │ │
139
+ │ - App 页面 │
140
+ │ - Claude 终端 │
141
+ │ - Inspector │
142
+ └─────────────────┘
143
+ ```
144
+
145
+ ## 支持的项目类型
146
+
147
+ | 类型 | 检测文件 | 默认命令 |
148
+ |------|----------|----------|
149
+ | Vite | `vite.config.ts/js` | `npm run dev` |
150
+ | Next.js | `next.config.js/mjs` | `npm run dev` |
151
+ | Webpack | `webpack.config.ts/js` | `npm run dev` |
152
+ | Custom | - | `npm run dev` |
153
+
154
+ ## 常见问题
155
+
156
+ ### Q: 如何使用自己的 claude 命令?
157
+
158
+ A: 使用 `--claude-path` 选项:
159
+
160
+ ```bash
161
+ npx claude-dev-server --claude-path /path/to/your/claude
162
+ ```
163
+
164
+ ### Q: Inspect 模式下为什么有些元素无法选中?
165
+
166
+ A: Dev Tools 自身的元素(悬浮按钮、面板等)已被排除,无法选中。
167
+
168
+ ### Q: 支持哪些框架的源码定位?
169
+
170
+ A: 目前支持 React、Vue。使用项目根路径自动计算相对路径。
171
+
172
+ ### Q: 可以在其他端口使用吗?
173
+
174
+ A: 可以,使用 `--port` 选项指定任意端口。
175
+
176
+ ## 技术栈
177
+
178
+ - **HTTP Proxy** - 请求转发和 HTML 注入
179
+ - **ttyd** - 终端 over HTTP/WebSocket
180
+ - **xterm.js** - Web 终端组件(VS Code 同款)
181
+ - **WebSocket (ws)** - 客户端-服务器通信
182
+
183
+ ## License
184
+
185
+ MIT
@@ -0,0 +1,61 @@
1
+ // ttyd-bridge.js - 桥接脚本,用于将外部消息转发到 ttyd 终端
2
+ // 这个文件会被注入到 ttyd-terminal.html 中
3
+
4
+ (function() {
5
+ const urlParams = new URLSearchParams(window.location.search);
6
+ const wsUrl = urlParams.get('ws');
7
+ if (!wsUrl) {
8
+ console.warn('[ttyd-bridge] No ws parameter found');
9
+ return;
10
+ }
11
+
12
+ const wsParsed = new URL(wsUrl);
13
+ const wsHost = wsParsed.host;
14
+ const wsProtocol = wsParsed.protocol === 'wss:' ? 'https:' : 'http:';
15
+
16
+ console.log('[ttyd-bridge] Initializing with wsUrl:', wsUrl);
17
+
18
+ // 拦截 fetch - 重定向 /token 请求
19
+ const originalFetch = window.fetch;
20
+ window.fetch = function(url, options) {
21
+ if (url && typeof url === 'string' && url.includes('/token')) {
22
+ url = wsProtocol + '//' + wsHost + '/token';
23
+ }
24
+ return originalFetch.call(this, url, options);
25
+ };
26
+
27
+ // 拦截 WebSocket - 重定向 /ws 连接
28
+ const OriginalWebSocket = window.WebSocket;
29
+ window.WebSocket = function(url, protocols) {
30
+ let finalUrl = url;
31
+ if (url && typeof url === 'string' && url.includes('/ws')) {
32
+ finalUrl = wsUrl.endsWith('/ws') ? wsUrl : (wsUrl + '/ws');
33
+ }
34
+ return new OriginalWebSocket(finalUrl, protocols);
35
+ };
36
+ Object.setPrototypeOf(window.WebSocket, OriginalWebSocket);
37
+ window.WebSocket.prototype = OriginalWebSocket.prototype;
38
+
39
+ // 暴露发送函数到 window - 使用 sendData 方法
40
+ window.sendToTerminal = function(text) {
41
+ console.log('[ttyd-bridge] Sending to terminal:', text);
42
+
43
+ // 等待 xterm 实例初始化
44
+ if (!window.xterm) {
45
+ console.warn('[ttyd-bridge] xterm not ready yet');
46
+ return false;
47
+ }
48
+
49
+ // 使用 sendData 方法模拟用户输入
50
+ try {
51
+ window.xterm.sendData(text);
52
+ console.log('[ttyd-bridge] Sent via xterm.sendData()');
53
+ return true;
54
+ } catch (e) {
55
+ console.error('[ttyd-bridge] sendData failed:', e);
56
+ return false;
57
+ }
58
+ };
59
+
60
+ console.log('[ttyd-bridge] Loaded successfully');
61
+ })();