send-to-pi 1.0.0
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/LICENSE +21 -0
- package/README.md +140 -0
- package/README_CN.md +140 -0
- package/chrome-extension/background.js +112 -0
- package/chrome-extension/icons/icon128.png +0 -0
- package/chrome-extension/icons/icon16.png +0 -0
- package/chrome-extension/icons/icon48.png +0 -0
- package/chrome-extension/manifest.json +26 -0
- package/chrome-extension/options.html +78 -0
- package/chrome-extension/options.js +21 -0
- package/package.json +41 -0
- package/pi-extension/README.md +27 -0
- package/pi-extension/index.ts +128 -0
- package/scripts/start-mac.sh +31 -0
- package/scripts/start-win.vbs +11 -0
- package/server/bin/send-to-pi.js +69 -0
- package/server/src/config.js +39 -0
- package/server/src/index.js +177 -0
- package/server/src/launchers/darwin.js +90 -0
- package/server/src/launchers/index.js +28 -0
- package/server/src/launchers/linux.js +51 -0
- package/server/src/launchers/win32.js +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wude (Wude-m)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# 🚀 send-to-pi
|
|
2
|
+
|
|
3
|
+
[**简体中文**](./README_CN.md) | **English**
|
|
4
|
+
|
|
5
|
+
> Seamlessly bridge your web browser to the [Pi Coding Agent](https://github.com/earendil-works/pi-coding-agent). Send snippets, articles, or links directly into your Pi terminal prompt with one right-click.
|
|
6
|
+
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
[](https://nodejs.org/)
|
|
9
|
+
[]()
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 💡 Why send-to-pi?
|
|
14
|
+
|
|
15
|
+
Modern AI coding agents (like Pi, Claude Code, Aider) shine inside the terminal, but **a massive portion of a developer's daily context lives inside the browser**—GitHub Issues, PR reviews, documentation, Stack Overflow threads, error logs, and technical blog posts.
|
|
16
|
+
|
|
17
|
+
Traditionally, sending webpage context to an agent means:
|
|
18
|
+
1. Selecting text
|
|
19
|
+
2. Switching windows
|
|
20
|
+
3. Pasting into the terminal
|
|
21
|
+
4. Typing instructions
|
|
22
|
+
|
|
23
|
+
**With `send-to-pi`:**
|
|
24
|
+
Right-click on any selected text, link, or page ➔ **The text is instantly prefilled into your Pi terminal input box**, with the source title & URL cleanly formatted.
|
|
25
|
+
- 🎯 **Prefill Mode (No Auto-Submit)**: The prompt is populated into the editor without auto-submitting. You can review, refine, or append instructions before pressing <kbd>Enter</kbd>.
|
|
26
|
+
- ⚡ **Cold-Start Auto-Wake**: If Pi isn't running, it automatically opens your terminal, starts Pi, and populates the text once the session boots up.
|
|
27
|
+
- 🍏 **True Cross-Platform**: Native terminal launch support for **macOS** (Terminal.app, iTerm2, Ghostty, WezTerm), **Windows** (Windows Terminal, PowerShell), and **Linux**.
|
|
28
|
+
- 🔒 **100% Local & Private**: All communication happens via `127.0.0.1`. No cloud proxy, no data logging.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 🛠️ Architecture
|
|
33
|
+
|
|
34
|
+
```mermaid
|
|
35
|
+
sequenceDiagram
|
|
36
|
+
autonumber
|
|
37
|
+
actor User as Developer
|
|
38
|
+
participant Browser as Chrome Extension
|
|
39
|
+
participant Bridge as Bridge Daemon (:18090)
|
|
40
|
+
participant Pi as Pi Agent Session (:18091)
|
|
41
|
+
|
|
42
|
+
User->>Browser: Right-click & "Send to Pi"
|
|
43
|
+
Browser->>Bridge: HTTP POST /send (Snippet, URL, Title)
|
|
44
|
+
alt Active Pi Running
|
|
45
|
+
Bridge->>Pi: Forward to 127.0.0.1:18091/receive
|
|
46
|
+
Pi->>Pi: ctx.ui.setEditorText() (Prefill)
|
|
47
|
+
Bridge-->>Browser: 200 OK (Prefilled)
|
|
48
|
+
else Pi Not Running (Cold Start)
|
|
49
|
+
Bridge->>Bridge: Save to ~/.pi/pending-browser-input.txt
|
|
50
|
+
Bridge->>User: Launch Terminal (osascript / wt.exe) & run `pi`
|
|
51
|
+
User->>Pi: Session Boots
|
|
52
|
+
Pi->>Pi: Read pending file & prefill input box
|
|
53
|
+
Bridge-->>Browser: 200 OK (Launched)
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## 📦 Quick Start (3 Steps)
|
|
60
|
+
|
|
61
|
+
### Step 1: Install the Pi Extension
|
|
62
|
+
You can install it directly via Pi's built-in package manager:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pi install git:github.com/Wude-m/send-to-pi
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
*(Alternatively, you can manually copy `pi-extension/index.ts` into your `~/.pi/agent/extensions/send-to-pi.ts`)*
|
|
69
|
+
|
|
70
|
+
### Step 2: Start the Bridge Daemon
|
|
71
|
+
The bridge daemon runs in the background on your machine:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
# Using npx directly
|
|
75
|
+
npx send-to-pi start
|
|
76
|
+
|
|
77
|
+
# Or clone and run
|
|
78
|
+
git clone https://github.com/Wude-m/send-to-pi.git
|
|
79
|
+
cd send-to-pi
|
|
80
|
+
npm start
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
*(Tip: macOS users can run `bash scripts/start-mac.sh` to run in background. Windows users can double-click `scripts/start-win.vbs` for silent background execution.)*
|
|
84
|
+
|
|
85
|
+
### Step 3: Load the Chrome Extension
|
|
86
|
+
1. Open Google Chrome or Microsoft Edge and navigate to `chrome://extensions/`.
|
|
87
|
+
2. Toggle on **Developer mode** in the top-right corner.
|
|
88
|
+
3. Click **Load unpacked** and select the `chrome-extension` folder from this repository.
|
|
89
|
+
4. You're all set!
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## ⌨️ How to Use
|
|
94
|
+
|
|
95
|
+
1. **Send Selected Text**: Highlight code or text on any webpage ➔ Right-click ➔ Choose `🚀 Send Selection to Pi`.
|
|
96
|
+
2. **Send Whole Page Link & Title**: Right-click anywhere on the page ➔ Choose `🚀 Send Current Page to Pi`.
|
|
97
|
+
3. **Send Hyperlink**: Right-click any link ➔ Choose `🚀 Send Link to Pi`.
|
|
98
|
+
|
|
99
|
+
The formatted context will immediately appear at your terminal cursor:
|
|
100
|
+
```text
|
|
101
|
+
[Source]: GitHub - earendil-works/pi-coding-agent (https://github.com/...)
|
|
102
|
+
|
|
103
|
+
Here is the code snippet or issue description...
|
|
104
|
+
_ [Cursor waits here for your instructions]
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## ⚙️ Configuration & Terminal Customization
|
|
110
|
+
|
|
111
|
+
You can configure options via environment variables when launching the daemon:
|
|
112
|
+
|
|
113
|
+
| Environment Variable | Default | Description |
|
|
114
|
+
| :--- | :--- | :--- |
|
|
115
|
+
| `PI_BRIDGE_PORT` | `18090` | Port listened by the Bridge Daemon |
|
|
116
|
+
| `PI_INTERNAL_PORT` | `18091` | Internal port used by the active Pi session |
|
|
117
|
+
| `PI_WORKDIR` | Current directory | Default working directory when auto-launching Pi |
|
|
118
|
+
| `PI_TERMINAL` | Auto-detect | Preferred terminal: `iterm2`, `ghostty`, `terminal`, `wezterm`, `alacritty` |
|
|
119
|
+
|
|
120
|
+
Example on macOS:
|
|
121
|
+
```bash
|
|
122
|
+
PI_TERMINAL=ghostty PI_WORKDIR=~/mycode npx send-to-pi start
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## 🤝 Contributing
|
|
128
|
+
|
|
129
|
+
Contributions are warmly welcome! Whether it's:
|
|
130
|
+
- Supporting Firefox / Safari extensions
|
|
131
|
+
- Adding more terminal emulators
|
|
132
|
+
- Providing Homebrew / AUR packages
|
|
133
|
+
|
|
134
|
+
Please feel free to open an Issue or Pull Request.
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## 📄 License
|
|
139
|
+
|
|
140
|
+
MIT License © 2026 Wude
|
package/README_CN.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# 🚀 send-to-pi (浏览器网页一键发送至 Pi Agent)
|
|
2
|
+
|
|
3
|
+
**简体中文** | [**English**](./README.md)
|
|
4
|
+
|
|
5
|
+
> 让你的网页浏览器与 [Pi 终端编程助手](https://github.com/earendil-works/pi-coding-agent) 无缝打通。在网页上选中文字、链接或页面,一键右键直达终端输入框,自动排版并预填上下文。
|
|
6
|
+
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
[](https://nodejs.org/)
|
|
9
|
+
[]()
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 💡 为什么需要 send-to-pi?
|
|
14
|
+
|
|
15
|
+
现代 AI Coding Agent(如 Pi、Claude Code、Aider 等)在命令行终端里表现极其出色,但日常开发中**绝大部分上下文其实都在浏览器里**——比如正在查阅的 GitHub Issue、PR 代码 Review、官方文档、Stack Overflow 问答、前端报错页面以及技术博文。
|
|
16
|
+
|
|
17
|
+
以往把网页内容喂给终端 Agent 的流程非常繁琐:
|
|
18
|
+
1. 鼠标划词复制
|
|
19
|
+
2. Alt+Tab 切到终端窗口
|
|
20
|
+
3. 粘贴进输入框
|
|
21
|
+
4. 手动打字写“请参考以上网页/报错...”
|
|
22
|
+
|
|
23
|
+
**有了 `send-to-pi` 之后:**
|
|
24
|
+
在任何网页上划选文字、右键链接或页面 ➔ **内容自动携带网页标题与来源 URL,毫秒级预填进 Pi 终端输入框**。
|
|
25
|
+
- 🎯 **Prefill 预填模式(绝不自作主张回车)**:内容送到输入框后,光标停留在末尾,你可以从容补充具体指令(如“请根据这段代码重构...”),最后按回车自主发送。
|
|
26
|
+
- ⚡ **冷启动自动唤醒终端**:就算你当前没有开着 Pi 终端,右键发送也会自动为你拉起终端、运行 `pi`,并在会话初始化完成后自动填入内容!
|
|
27
|
+
- 🍏 **全平台终端适配**:原生支持 **macOS**(Terminal.app、iTerm2、Ghostty、WezTerm 等)、**Windows**(Windows Terminal、PowerShell)以及 **Linux**。
|
|
28
|
+
- 🔒 **纯本地与隐私安全**:全链路通过本地环回地址 `127.0.0.1` 传输,无云端中转,零数据收集。
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 🛠️ 系统架构与工作流程
|
|
33
|
+
|
|
34
|
+
```mermaid
|
|
35
|
+
sequenceDiagram
|
|
36
|
+
autonumber
|
|
37
|
+
actor User as 开发者
|
|
38
|
+
participant Browser as Chrome 浏览器扩展
|
|
39
|
+
participant Bridge as Bridge 调度守护进程 (:18090)
|
|
40
|
+
participant Pi as Pi Agent 终端会话 (:18091)
|
|
41
|
+
|
|
42
|
+
User->>Browser: 右键菜单点击 "发送给 Pi"
|
|
43
|
+
Browser->>Bridge: HTTP POST /send (正文、URL、标题)
|
|
44
|
+
alt Pi 正在运行
|
|
45
|
+
Bridge->>Pi: 转发至 127.0.0.1:18091/receive
|
|
46
|
+
Pi->>Pi: ctx.ui.setEditorText() (预填到输入框)
|
|
47
|
+
Bridge-->>Browser: 200 OK (已填入输入框)
|
|
48
|
+
else Pi 未运行 (冷启动模式)
|
|
49
|
+
Bridge->>Bridge: 写入暂存文件 ~/.pi/pending-browser-input.txt
|
|
50
|
+
Bridge->>User: 自动拉起终端并运行 `pi`
|
|
51
|
+
User->>Pi: 会话初始化完成
|
|
52
|
+
Pi->>Pi: 读取暂存文件并填入输入框
|
|
53
|
+
Bridge-->>Browser: 200 OK (已唤醒终端)
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## 📦 三步快速上手
|
|
60
|
+
|
|
61
|
+
### 第一步:安装 Pi 原生扩展
|
|
62
|
+
你可以直接使用 Pi 内置的包管理器一键安装:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pi install git:github.com/Wude-m/send-to-pi
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
*(或者,你也可以手动将 `pi-extension/index.ts` 复制到本机 `~/.pi/agent/extensions/send-to-pi.ts`)*
|
|
69
|
+
|
|
70
|
+
### 第二步:启动 Bridge 守护进程
|
|
71
|
+
调度守护进程负责在后台监听浏览器请求并在需要时唤醒终端:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
# 直接使用 npx 运行
|
|
75
|
+
npx send-to-pi start
|
|
76
|
+
|
|
77
|
+
# 或者克隆本仓库后运行
|
|
78
|
+
git clone https://github.com/Wude-m/send-to-pi.git
|
|
79
|
+
cd send-to-pi
|
|
80
|
+
npm start
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
> **后台常驻小提示**:
|
|
84
|
+
> - **macOS / Linux 用户**:直接运行 `bash scripts/start-mac.sh` 即可在后台静默运行。
|
|
85
|
+
> - **Windows 用户**:双击 `scripts/start-win.vbs` 即可无黑框后台静默运行。
|
|
86
|
+
|
|
87
|
+
### 第三步:加载 Chrome 浏览器扩展
|
|
88
|
+
1. 打开 Chrome 或 Edge 浏览器,在地址栏输入 `chrome://extensions/`;
|
|
89
|
+
2. 开启右上角的 **“开发者模式”**(Developer mode);
|
|
90
|
+
3. 点击左上角的 **“加载已解压的扩展程序”**(Load unpacked);
|
|
91
|
+
4. 选择本仓库的 `chrome-extension` 目录即可!
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## ⌨️ 日常使用演示
|
|
96
|
+
|
|
97
|
+
1. **发送划选内容**:网页上鼠标选中任何代码或文本 ➔ 右键 ➔ 点击 `🚀 发送选中内容给 Pi`;
|
|
98
|
+
2. **发送整个网页**:在页面空白处 ➔ 右键 ➔ 点击 `🚀 发送当前网页给 Pi`;
|
|
99
|
+
3. **发送超链接**:在任意链接上 ➔ 右键 ➔ 点击 `🚀 发送此链接给 Pi`。
|
|
100
|
+
|
|
101
|
+
终端输入框中会立即呈现带有来源标题与 URL 的整洁内容:
|
|
102
|
+
```text
|
|
103
|
+
【来源】:GitHub - earendil-works/pi-coding-agent (https://github.com/...)
|
|
104
|
+
|
|
105
|
+
这里是选中的代码片段或技术文档内容...
|
|
106
|
+
_ [光标停在此处,等待你补充提示词]
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## ⚙️ 环境变量与终端自定义
|
|
112
|
+
|
|
113
|
+
支持通过环境变量调整守护进程行为:
|
|
114
|
+
|
|
115
|
+
| 环境变量 | 默认值 | 说明 |
|
|
116
|
+
| :--- | :--- | :--- |
|
|
117
|
+
| `PI_BRIDGE_PORT` | `18090` | Bridge 调度进程监听的端口 |
|
|
118
|
+
| `PI_INTERNAL_PORT` | `18091` | 活跃 Pi 会话内部接收端口 |
|
|
119
|
+
| `PI_WORKDIR` | 当前目录 | 自动唤醒 Pi 时默认进入的工作目录 |
|
|
120
|
+
| `PI_TERMINAL` | 自动探测 | macOS/Linux 指定偏好终端(如 `iterm2`, `ghostty`, `terminal`, `alacritty`) |
|
|
121
|
+
|
|
122
|
+
macOS 示例:
|
|
123
|
+
```bash
|
|
124
|
+
PI_TERMINAL=ghostty PI_WORKDIR=~/mycode npx send-to-pi start
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## 🤝 欢迎贡献
|
|
130
|
+
|
|
131
|
+
欢迎提交 PR 或 Issue!无论是:
|
|
132
|
+
- 适配 Firefox / Safari 扩展
|
|
133
|
+
- 支持更多小众终端仿真器
|
|
134
|
+
- 完善打包与系统服务脚本
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## 📄 开源协议
|
|
139
|
+
|
|
140
|
+
基于 [MIT License](./LICENSE) 开源 © 2026 Wude
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Send to Pi - Chrome Background Service Worker
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const DEFAULT_PORT = 18090;
|
|
6
|
+
const isChinese = (navigator.language || '').toLowerCase().startsWith('zh');
|
|
7
|
+
|
|
8
|
+
const i18n = {
|
|
9
|
+
menuSelection: isChinese ? "🚀 发送选中内容给 Pi" : "🚀 Send Selection to Pi",
|
|
10
|
+
menuPage: isChinese ? "🚀 发送当前网页给 Pi" : "🚀 Send Current Page to Pi",
|
|
11
|
+
menuLink: isChinese ? "🚀 发送此链接给 Pi" : "🚀 Send Link to Pi",
|
|
12
|
+
notifyLaunchedTitle: isChinese ? "已唤起终端并预填内容" : "Terminal Launched & Prefilled",
|
|
13
|
+
notifyLaunchedMsg: (term) =>
|
|
14
|
+
isChinese ? `已启动 ${term || '终端'},内容已在 Pi 输入框就绪!` : `Started ${term || 'terminal'}, prompt is ready in Pi!`,
|
|
15
|
+
notifyPrefilledTitle: isChinese ? "已填入 Pi 终端输入框" : "Prefilled into Pi Terminal",
|
|
16
|
+
notifyPrefilledMsg: isChinese
|
|
17
|
+
? "内容已送达,光标位于末尾,可自由补充要求或按 Enter 发送!"
|
|
18
|
+
: "Content delivered to terminal prompt. Append your instructions or press Enter!",
|
|
19
|
+
notifyFailedTitle: isChinese ? "发送失败" : "Failed to Send",
|
|
20
|
+
notifyFailedMsg: (code) =>
|
|
21
|
+
isChinese ? `Bridge 响应状态码: ${code}` : `Bridge daemon responded with status: ${code}`,
|
|
22
|
+
notifyNoConnTitle: isChinese ? "无法连接到 Bridge 守护进程" : "Cannot Connect to Bridge Daemon",
|
|
23
|
+
notifyNoConnMsg: isChinese
|
|
24
|
+
? "请确保本地后台 send-to-pi 守护进程已启动(默认端口 18090)。"
|
|
25
|
+
: "Please ensure the local send-to-pi daemon is running (default port: 18090).",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
async function getBridgeUrl() {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
chrome.storage.sync.get({ bridgePort: DEFAULT_PORT }, (items) => {
|
|
31
|
+
const port = items.bridgePort || DEFAULT_PORT;
|
|
32
|
+
resolve(`http://127.0.0.1:${port}/send`);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 注册右键菜单
|
|
38
|
+
chrome.runtime.onInstalled.addListener(() => {
|
|
39
|
+
chrome.contextMenus.create({
|
|
40
|
+
id: "send_selection",
|
|
41
|
+
title: i18n.menuSelection,
|
|
42
|
+
contexts: ["selection"],
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
chrome.contextMenus.create({
|
|
46
|
+
id: "send_page",
|
|
47
|
+
title: i18n.menuPage,
|
|
48
|
+
contexts: ["page"],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
chrome.contextMenus.create({
|
|
52
|
+
id: "send_link",
|
|
53
|
+
title: i18n.menuLink,
|
|
54
|
+
contexts: ["link"],
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// 处理右键点击事件
|
|
59
|
+
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
60
|
+
const payload = {
|
|
61
|
+
type: "page",
|
|
62
|
+
text: "",
|
|
63
|
+
title: tab ? tab.title : "",
|
|
64
|
+
url: tab ? tab.url : "",
|
|
65
|
+
lang: isChinese ? "zh" : "en",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
if (info.menuItemId === "send_selection") {
|
|
69
|
+
payload.type = "selection";
|
|
70
|
+
payload.text = info.selectionText || "";
|
|
71
|
+
} else if (info.menuItemId === "send_link") {
|
|
72
|
+
payload.type = "link";
|
|
73
|
+
payload.url = info.linkUrl || "";
|
|
74
|
+
} else if (info.menuItemId === "send_page") {
|
|
75
|
+
payload.type = "page";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const endpoint = await getBridgeUrl();
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const res = await fetch(endpoint, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
"Content-Type": "application/json",
|
|
85
|
+
},
|
|
86
|
+
body: JSON.stringify(payload),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
if (res.ok) {
|
|
90
|
+
const data = await res.json();
|
|
91
|
+
if (data.mode === "launched") {
|
|
92
|
+
showNotify(i18n.notifyLaunchedTitle, i18n.notifyLaunchedMsg(data.terminal));
|
|
93
|
+
} else {
|
|
94
|
+
showNotify(i18n.notifyPrefilledTitle, i18n.notifyPrefilledMsg);
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
showNotify(i18n.notifyFailedTitle, i18n.notifyFailedMsg(res.status));
|
|
98
|
+
}
|
|
99
|
+
} catch (_err) {
|
|
100
|
+
showNotify(i18n.notifyNoConnTitle, i18n.notifyNoConnMsg);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
function showNotify(title, message) {
|
|
105
|
+
chrome.notifications.create({
|
|
106
|
+
type: "basic",
|
|
107
|
+
iconUrl: "icons/icon128.png",
|
|
108
|
+
title: title,
|
|
109
|
+
message: message,
|
|
110
|
+
priority: 1,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"manifest_version": 3,
|
|
3
|
+
"name": "Send to Pi",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Send webpage snippets, URLs, or articles directly to your local Pi coding agent terminal.",
|
|
6
|
+
"permissions": [
|
|
7
|
+
"contextMenus",
|
|
8
|
+
"notifications",
|
|
9
|
+
"storage"
|
|
10
|
+
],
|
|
11
|
+
"host_permissions": [
|
|
12
|
+
"http://127.0.0.1:*/*"
|
|
13
|
+
],
|
|
14
|
+
"background": {
|
|
15
|
+
"service_worker": "background.js"
|
|
16
|
+
},
|
|
17
|
+
"options_ui": {
|
|
18
|
+
"page": "options.html",
|
|
19
|
+
"open_in_tab": false
|
|
20
|
+
},
|
|
21
|
+
"icons": {
|
|
22
|
+
"16": "icons/icon16.png",
|
|
23
|
+
"48": "icons/icon48.png",
|
|
24
|
+
"128": "icons/icon128.png"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>Send to Pi Settings</title>
|
|
6
|
+
<style>
|
|
7
|
+
body {
|
|
8
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
9
|
+
padding: 16px 20px;
|
|
10
|
+
width: 320px;
|
|
11
|
+
color: #333;
|
|
12
|
+
margin: 0;
|
|
13
|
+
}
|
|
14
|
+
h2 {
|
|
15
|
+
font-size: 16px;
|
|
16
|
+
margin-top: 0;
|
|
17
|
+
margin-bottom: 12px;
|
|
18
|
+
display: flex;
|
|
19
|
+
align-items: center;
|
|
20
|
+
gap: 8px;
|
|
21
|
+
}
|
|
22
|
+
.field {
|
|
23
|
+
margin-bottom: 14px;
|
|
24
|
+
}
|
|
25
|
+
label {
|
|
26
|
+
display: block;
|
|
27
|
+
font-size: 13px;
|
|
28
|
+
font-weight: 600;
|
|
29
|
+
margin-bottom: 6px;
|
|
30
|
+
}
|
|
31
|
+
input[type="number"] {
|
|
32
|
+
width: 100%;
|
|
33
|
+
box-sizing: border-box;
|
|
34
|
+
padding: 8px 10px;
|
|
35
|
+
border: 1px solid #ccc;
|
|
36
|
+
border-radius: 6px;
|
|
37
|
+
font-size: 14px;
|
|
38
|
+
}
|
|
39
|
+
.hint {
|
|
40
|
+
font-size: 12px;
|
|
41
|
+
color: #666;
|
|
42
|
+
margin-top: 4px;
|
|
43
|
+
}
|
|
44
|
+
button {
|
|
45
|
+
background: #2563eb;
|
|
46
|
+
color: #fff;
|
|
47
|
+
border: none;
|
|
48
|
+
padding: 8px 16px;
|
|
49
|
+
border-radius: 6px;
|
|
50
|
+
font-size: 13px;
|
|
51
|
+
font-weight: 500;
|
|
52
|
+
cursor: pointer;
|
|
53
|
+
width: 100%;
|
|
54
|
+
}
|
|
55
|
+
button:hover {
|
|
56
|
+
background: #1d4ed8;
|
|
57
|
+
}
|
|
58
|
+
#status {
|
|
59
|
+
margin-top: 10px;
|
|
60
|
+
font-size: 12px;
|
|
61
|
+
text-align: center;
|
|
62
|
+
color: #16a34a;
|
|
63
|
+
min-height: 16px;
|
|
64
|
+
}
|
|
65
|
+
</style>
|
|
66
|
+
</head>
|
|
67
|
+
<body>
|
|
68
|
+
<h2>🚀 Send to Pi</h2>
|
|
69
|
+
<div class="field">
|
|
70
|
+
<label for="bridgePort">Bridge Daemon Port</label>
|
|
71
|
+
<input type="number" id="bridgePort" min="1024" max="65535" value="18090" />
|
|
72
|
+
<div class="hint">Default port is 18090, matching the send-to-pi bridge daemon.</div>
|
|
73
|
+
</div>
|
|
74
|
+
<button id="save">Save Settings</button>
|
|
75
|
+
<div id="status"></div>
|
|
76
|
+
<script src="options.js"></script>
|
|
77
|
+
</body>
|
|
78
|
+
</html>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const DEFAULT_PORT = 18090;
|
|
2
|
+
|
|
3
|
+
function restoreOptions() {
|
|
4
|
+
chrome.storage.sync.get({ bridgePort: DEFAULT_PORT }, (items) => {
|
|
5
|
+
document.getElementById('bridgePort').value = items.bridgePort;
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function saveOptions() {
|
|
10
|
+
const port = parseInt(document.getElementById('bridgePort').value, 10) || DEFAULT_PORT;
|
|
11
|
+
chrome.storage.sync.set({ bridgePort: port }, () => {
|
|
12
|
+
const status = document.getElementById('status');
|
|
13
|
+
status.textContent = '✅ Settings saved!';
|
|
14
|
+
setTimeout(() => {
|
|
15
|
+
status.textContent = '';
|
|
16
|
+
}, 2000);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
document.addEventListener('DOMContentLoaded', restoreOptions);
|
|
21
|
+
document.getElementById('save').addEventListener('click', saveOptions);
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "send-to-pi",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Send webpage snippets, URLs, or articles directly to your local Pi coding agent terminal.",
|
|
5
|
+
"main": "server/src/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"send-to-pi": "server/bin/send-to-pi.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node server/src/index.js",
|
|
11
|
+
"status": "node server/bin/send-to-pi.js status",
|
|
12
|
+
"test": "node server/bin/send-to-pi.js --help"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"pi-package",
|
|
16
|
+
"pi-extension",
|
|
17
|
+
"pi",
|
|
18
|
+
"pi-coding-agent",
|
|
19
|
+
"chrome-extension",
|
|
20
|
+
"terminal",
|
|
21
|
+
"ai-agent",
|
|
22
|
+
"developer-tools"
|
|
23
|
+
],
|
|
24
|
+
"pi": {
|
|
25
|
+
"extensions": [
|
|
26
|
+
"./pi-extension/index.ts"
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
31
|
+
},
|
|
32
|
+
"author": "Wude (Wude-m)",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/Wude-m/send-to-pi.git"
|
|
36
|
+
},
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=16.0.0"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# send-to-pi: Pi Coding Agent Extension
|
|
2
|
+
|
|
3
|
+
这是 `send-to-pi` 运行在 Pi coding agent 端的核心扩展。
|
|
4
|
+
|
|
5
|
+
## 作用
|
|
6
|
+
1. 监听本地内部端口 `127.0.0.1:18091`,接收调度服务(Bridge Daemon)转发过来的网页摘录;
|
|
7
|
+
2. 收到内容后,通过 Pi 的原生 API `ctx.ui.setEditorText()` **预填到终端输入框(Prefill 模式)**;
|
|
8
|
+
3. **绝不自作主张自动回车发送**,光标停留在内容末尾,让开发者可以自由补充指令或微调内容;
|
|
9
|
+
4. 终端会话启动时,自动读取冷启动时暂存的文件(`~/.pi/pending-browser-input.txt`),无缝实现“唤醒终端并填入内容”。
|
|
10
|
+
|
|
11
|
+
## 安装方式
|
|
12
|
+
|
|
13
|
+
将 `index.ts` 复制或软链接到你的 Pi 扩展目录中:
|
|
14
|
+
|
|
15
|
+
### macOS / Linux
|
|
16
|
+
```bash
|
|
17
|
+
mkdir -p ~/.pi/agent/extensions
|
|
18
|
+
cp index.ts ~/.pi/agent/extensions/send-to-pi.ts
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Windows (PowerShell)
|
|
22
|
+
```powershell
|
|
23
|
+
New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.pi\agent\extensions"
|
|
24
|
+
Copy-Item index.ts "$env:USERPROFILE\.pi\agent\extensions\send-to-pi.ts"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
重启 `pi` 会话后即可自动加载!
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* send-to-pi Extension for Pi Coding Agent
|
|
3
|
+
*
|
|
4
|
+
* Listens on 127.0.0.1:18091 for incoming browser payloads.
|
|
5
|
+
* Automatically prefills text into the terminal editor with setEditorText()
|
|
6
|
+
* without executing immediately, giving the user full control to refine prompts!
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
10
|
+
import * as http from "node:http";
|
|
11
|
+
import * as fs from "node:fs";
|
|
12
|
+
import * as path from "node:path";
|
|
13
|
+
import * as os from "node:os";
|
|
14
|
+
|
|
15
|
+
const INTERNAL_PORT = parseInt(process.env.PI_INTERNAL_PORT || "", 10) || 18091;
|
|
16
|
+
|
|
17
|
+
function getPendingFilePaths(): string[] {
|
|
18
|
+
const piHome = path.join(os.homedir(), ".pi");
|
|
19
|
+
return [
|
|
20
|
+
path.join(piHome, "pending-browser-input.txt"),
|
|
21
|
+
path.join(os.tmpdir(), "pi-pending-browser-input.txt"),
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default function (pi: ExtensionAPI) {
|
|
26
|
+
let server: http.Server | null = null;
|
|
27
|
+
let activeCtx: ExtensionContext | null = null;
|
|
28
|
+
|
|
29
|
+
function prefillEditor(ctx: ExtensionContext, textToInsert: string) {
|
|
30
|
+
if (!ctx || !ctx.ui) return;
|
|
31
|
+
try {
|
|
32
|
+
const current = ctx.ui.getEditorText ? ctx.ui.getEditorText() : "";
|
|
33
|
+
const finalContent =
|
|
34
|
+
current && current.trim().length > 0
|
|
35
|
+
? `${current.trim()}\n\n${textToInsert}`
|
|
36
|
+
: textToInsert;
|
|
37
|
+
|
|
38
|
+
ctx.ui.setEditorText(finalContent);
|
|
39
|
+
ctx.ui.notify("🚀 已将浏览器内容填入输入框,可补充指令或直接回车!", "info");
|
|
40
|
+
} catch (e: any) {
|
|
41
|
+
console.error("[SendToPi] setEditorText error:", e.message);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
46
|
+
activeCtx = ctx;
|
|
47
|
+
|
|
48
|
+
// 检查是否有冷启动唤醒时暂存的内容
|
|
49
|
+
for (const pendingFile of getPendingFilePaths()) {
|
|
50
|
+
if (fs.existsSync(pendingFile)) {
|
|
51
|
+
try {
|
|
52
|
+
const content = fs.readFileSync(pendingFile, "utf8");
|
|
53
|
+
fs.unlinkSync(pendingFile);
|
|
54
|
+
setTimeout(() => {
|
|
55
|
+
prefillEditor(ctx, content);
|
|
56
|
+
}, 800);
|
|
57
|
+
break;
|
|
58
|
+
} catch (err: any) {
|
|
59
|
+
console.error("[SendToPi] Failed to read pending file:", err.message);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (server) return;
|
|
65
|
+
|
|
66
|
+
server = http.createServer((req, res) => {
|
|
67
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
68
|
+
res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
|
|
69
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
70
|
+
|
|
71
|
+
if (req.method === "OPTIONS") {
|
|
72
|
+
res.writeHead(204);
|
|
73
|
+
res.end();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (req.method === "GET" && req.url === "/health") {
|
|
78
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
79
|
+
res.end(JSON.stringify({ status: "alive", port: INTERNAL_PORT }));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (req.method === "POST" && req.url === "/receive") {
|
|
84
|
+
let body = "";
|
|
85
|
+
req.on("data", (chunk) => {
|
|
86
|
+
body += chunk;
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
req.on("end", () => {
|
|
90
|
+
try {
|
|
91
|
+
const data = JSON.parse(body || "{}");
|
|
92
|
+
const { content } = data;
|
|
93
|
+
|
|
94
|
+
if (content && activeCtx) {
|
|
95
|
+
prefillEditor(activeCtx, content);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
99
|
+
res.end(JSON.stringify({ success: true, mode: "prefilled" }));
|
|
100
|
+
} catch (err: any) {
|
|
101
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
102
|
+
res.end(JSON.stringify({ success: false, error: err.message }));
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
109
|
+
res.end(JSON.stringify({ error: "Not Found" }));
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
server.listen(INTERNAL_PORT, "127.0.0.1");
|
|
113
|
+
|
|
114
|
+
server.on("error", (e: any) => {
|
|
115
|
+
if (e.code !== "EADDRINUSE") {
|
|
116
|
+
console.error("[SendToPi Internal Server Error]:", e);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
pi.on("session_shutdown", async () => {
|
|
122
|
+
if (server) {
|
|
123
|
+
server.close();
|
|
124
|
+
server = null;
|
|
125
|
+
}
|
|
126
|
+
activeCtx = null;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+
# send-to-pi: Background Launcher for macOS / Linux
|
|
4
|
+
|
|
5
|
+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
6
|
+
PID_FILE="$HOME/.pi/send-to-pi.pid"
|
|
7
|
+
|
|
8
|
+
mkdir -p "$HOME/.pi"
|
|
9
|
+
|
|
10
|
+
# 检查是否已在运行
|
|
11
|
+
if [ -f "$PID_FILE" ]; then
|
|
12
|
+
PID=$(cat "$PID_FILE")
|
|
13
|
+
if ps -p "$PID" > /dev/null 2>&1; then
|
|
14
|
+
echo "✅ send-to-pi daemon is already running (PID: $PID)"
|
|
15
|
+
exit 0
|
|
16
|
+
fi
|
|
17
|
+
fi
|
|
18
|
+
|
|
19
|
+
echo "🚀 Starting send-to-pi daemon in background..."
|
|
20
|
+
nohup node "$DIR/server/src/index.js" > "$HOME/.pi/send-to-pi.log" 2>&1 &
|
|
21
|
+
NEW_PID=$!
|
|
22
|
+
echo "$NEW_PID" > "$PID_FILE"
|
|
23
|
+
|
|
24
|
+
sleep 0.5
|
|
25
|
+
if ps -p "$NEW_PID" > /dev/null 2>&1; then
|
|
26
|
+
echo "✨ send-to-pi daemon started successfully (PID: $NEW_PID)!"
|
|
27
|
+
echo "📜 Logs: $HOME/.pi/send-to-pi.log"
|
|
28
|
+
else
|
|
29
|
+
echo "❌ Failed to start daemon. Please check $HOME/.pi/send-to-pi.log"
|
|
30
|
+
exit 1
|
|
31
|
+
fi
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
' send-to-pi: Silent Background Launcher for Windows
|
|
2
|
+
Set WshShell = CreateObject("WScript.Shell")
|
|
3
|
+
Set fso = CreateObject("Scripting.FileSystemObject")
|
|
4
|
+
|
|
5
|
+
' 获取项目根目录
|
|
6
|
+
scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
|
|
7
|
+
rootDir = fso.GetParentFolderName(scriptDir)
|
|
8
|
+
targetScript = rootDir & "\server\src\index.js"
|
|
9
|
+
|
|
10
|
+
' 0 = 隐藏黑框静默运行
|
|
11
|
+
WshShell.Run "node """ & targetScript & """", 0, False
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* CLI Entry point for send-to-pi
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { startDaemon } = require('../src/index');
|
|
8
|
+
const http = require('http');
|
|
9
|
+
const { BRIDGE_PORT } = require('../src/config');
|
|
10
|
+
|
|
11
|
+
const args = process.argv.slice(2);
|
|
12
|
+
const command = args[0] || 'start';
|
|
13
|
+
|
|
14
|
+
if (command === 'status' || command === 'check') {
|
|
15
|
+
const req = http.request(
|
|
16
|
+
{
|
|
17
|
+
hostname: '127.0.0.1',
|
|
18
|
+
port: BRIDGE_PORT,
|
|
19
|
+
path: '/health',
|
|
20
|
+
method: 'GET',
|
|
21
|
+
timeout: 1500,
|
|
22
|
+
},
|
|
23
|
+
(res) => {
|
|
24
|
+
let data = '';
|
|
25
|
+
res.on('data', (c) => (data += c));
|
|
26
|
+
res.on('end', () => {
|
|
27
|
+
try {
|
|
28
|
+
const info = JSON.parse(data);
|
|
29
|
+
console.log(`✅ send-to-pi daemon is running healthy on http://127.0.0.1:${BRIDGE_PORT}`);
|
|
30
|
+
console.log(JSON.stringify(info, null, 2));
|
|
31
|
+
} catch {
|
|
32
|
+
console.log(`✅ Daemon is alive (status ${res.statusCode})`);
|
|
33
|
+
}
|
|
34
|
+
process.exit(0);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
req.on('error', () => {
|
|
40
|
+
console.log(`❌ send-to-pi daemon is NOT running on port ${BRIDGE_PORT}`);
|
|
41
|
+
process.exit(1);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
req.on('timeout', () => {
|
|
45
|
+
console.log(`⚠️ Connection to daemon timed out`);
|
|
46
|
+
req.destroy();
|
|
47
|
+
process.exit(1);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
req.end();
|
|
51
|
+
} else if (command === 'help' || command === '--help' || command === '-h') {
|
|
52
|
+
console.log(`
|
|
53
|
+
Usage: send-to-pi [command] [options]
|
|
54
|
+
|
|
55
|
+
Commands:
|
|
56
|
+
start Start the bridge daemon (default)
|
|
57
|
+
status Check if the bridge daemon is running
|
|
58
|
+
help Show this help message
|
|
59
|
+
|
|
60
|
+
Options via Environment Variables:
|
|
61
|
+
PI_BRIDGE_PORT Daemon listening port (default: 18090)
|
|
62
|
+
PI_INTERNAL_PORT Pi session receiver port (default: 18091)
|
|
63
|
+
PI_WORKDIR Working directory to launch Pi terminal in
|
|
64
|
+
PI_TERMINAL Preferred terminal emulator (e.g. ghostty, iterm2, alacritty)
|
|
65
|
+
`);
|
|
66
|
+
process.exit(0);
|
|
67
|
+
} else {
|
|
68
|
+
startDaemon();
|
|
69
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
|
|
5
|
+
const BRIDGE_PORT = parseInt(process.env.PI_BRIDGE_PORT, 10) || 18090;
|
|
6
|
+
const PI_INTERNAL_PORT = parseInt(process.env.PI_INTERNAL_PORT, 10) || 18091;
|
|
7
|
+
|
|
8
|
+
// 确保 ~/.pi 目录存在
|
|
9
|
+
const piHomeDir = path.join(os.homedir(), '.pi');
|
|
10
|
+
if (!fs.existsSync(piHomeDir)) {
|
|
11
|
+
try {
|
|
12
|
+
fs.mkdirSync(piHomeDir, { recursive: true });
|
|
13
|
+
} catch (err) {
|
|
14
|
+
// 忽略创建失败(如权限受限),由操作系统临时目录兜底
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const PENDING_FILE = fs.existsSync(piHomeDir)
|
|
19
|
+
? path.join(piHomeDir, 'pending-browser-input.txt')
|
|
20
|
+
: path.join(os.tmpdir(), 'pi-pending-browser-input.txt');
|
|
21
|
+
|
|
22
|
+
// 默认工作目录:环境变量优先,其次当前工作目录,兜底家目录
|
|
23
|
+
function getWorkDir() {
|
|
24
|
+
if (process.env.PI_WORKDIR && fs.existsSync(process.env.PI_WORKDIR)) {
|
|
25
|
+
return process.env.PI_WORKDIR;
|
|
26
|
+
}
|
|
27
|
+
const cwd = process.cwd();
|
|
28
|
+
if (cwd && fs.existsSync(cwd) && cwd !== '/' && !cwd.endsWith('\\Windows\\system32')) {
|
|
29
|
+
return cwd;
|
|
30
|
+
}
|
|
31
|
+
return os.homedir();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = {
|
|
35
|
+
BRIDGE_PORT,
|
|
36
|
+
PI_INTERNAL_PORT,
|
|
37
|
+
PENDING_FILE,
|
|
38
|
+
getWorkDir,
|
|
39
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* send-to-pi Bridge Daemon
|
|
3
|
+
*
|
|
4
|
+
* Listens on: 127.0.0.1:18090 (receives Chrome extension requests)
|
|
5
|
+
* Probes: 127.0.0.1:18091 (internal receiver in active Pi session)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const http = require('http');
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const { BRIDGE_PORT, PI_INTERNAL_PORT, PENDING_FILE, getWorkDir } = require('./config');
|
|
11
|
+
const { launchTerminal } = require('./launchers');
|
|
12
|
+
|
|
13
|
+
function forwardToActivePi(content) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
const data = JSON.stringify({ content });
|
|
16
|
+
const req = http.request(
|
|
17
|
+
{
|
|
18
|
+
hostname: '127.0.0.1',
|
|
19
|
+
port: PI_INTERNAL_PORT,
|
|
20
|
+
path: '/receive',
|
|
21
|
+
method: 'POST',
|
|
22
|
+
headers: {
|
|
23
|
+
'Content-Type': 'application/json',
|
|
24
|
+
'Content-Length': Buffer.byteLength(data),
|
|
25
|
+
},
|
|
26
|
+
timeout: 1000,
|
|
27
|
+
},
|
|
28
|
+
(res) => {
|
|
29
|
+
if (res.statusCode === 200) {
|
|
30
|
+
resolve(true);
|
|
31
|
+
} else {
|
|
32
|
+
reject(new Error(`Pi receiver responded with HTTP ${res.statusCode}`));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
req.on('error', (err) => reject(err));
|
|
38
|
+
req.on('timeout', () => {
|
|
39
|
+
req.destroy();
|
|
40
|
+
reject(new Error('Pi receiver request timed out'));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
req.write(data);
|
|
44
|
+
req.end();
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function formatContent(payload) {
|
|
49
|
+
const { type, text, title, url, lang } = payload;
|
|
50
|
+
const isZh = lang === 'zh';
|
|
51
|
+
|
|
52
|
+
if (type === 'selection') {
|
|
53
|
+
const label = isZh ? '【来源】' : '[Source]';
|
|
54
|
+
return `${label}: ${title || (isZh ? '网页摘录' : 'Web Snippet')} (${url || ''})\n\n${text}\n\n`;
|
|
55
|
+
}
|
|
56
|
+
if (type === 'link') {
|
|
57
|
+
const label = isZh ? '【网页链接】' : '[Link]';
|
|
58
|
+
return `${label}: ${url}\n\n`;
|
|
59
|
+
}
|
|
60
|
+
const titleLabel = isZh ? '【网页标题】' : '[Title]';
|
|
61
|
+
const linkLabel = isZh ? '【网页链接】' : '[Link]';
|
|
62
|
+
return `${titleLabel}: ${title || (isZh ? '网页' : 'Page')}\n${linkLabel}: ${url || ''}\n\n`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function createBridgeServer() {
|
|
66
|
+
const server = http.createServer(async (req, res) => {
|
|
67
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
68
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
69
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
70
|
+
|
|
71
|
+
if (req.method === 'OPTIONS') {
|
|
72
|
+
res.writeHead(204);
|
|
73
|
+
res.end();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (req.method === 'GET' && req.url === '/health') {
|
|
78
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
79
|
+
res.end(
|
|
80
|
+
JSON.stringify({
|
|
81
|
+
status: 'ok',
|
|
82
|
+
platform: process.platform,
|
|
83
|
+
bridgePort: BRIDGE_PORT,
|
|
84
|
+
internalPort: PI_INTERNAL_PORT,
|
|
85
|
+
workDir: getWorkDir(),
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (req.method === 'POST' && req.url === '/send') {
|
|
92
|
+
let body = '';
|
|
93
|
+
req.on('data', (chunk) => {
|
|
94
|
+
body += chunk;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
req.on('end', async () => {
|
|
98
|
+
try {
|
|
99
|
+
const payload = JSON.parse(body || '{}');
|
|
100
|
+
const content = formatContent(payload);
|
|
101
|
+
|
|
102
|
+
// 1. 尝试直接转发给当前正在运行的 Pi 会话
|
|
103
|
+
try {
|
|
104
|
+
await forwardToActivePi(content);
|
|
105
|
+
console.log('[Bridge] Successfully prefilled text into active Pi terminal.');
|
|
106
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
107
|
+
res.end(
|
|
108
|
+
JSON.stringify({
|
|
109
|
+
success: true,
|
|
110
|
+
mode: 'prefilled',
|
|
111
|
+
message: '已直接填入活跃的 Pi 输入框!',
|
|
112
|
+
})
|
|
113
|
+
);
|
|
114
|
+
return;
|
|
115
|
+
} catch (_err) {
|
|
116
|
+
// 2. 当前没有运行中的 Pi,进行冷启动唤醒流程
|
|
117
|
+
console.log('[Bridge] No active Pi detected. Writing pending file and launching terminal...');
|
|
118
|
+
fs.writeFileSync(PENDING_FILE, content, 'utf8');
|
|
119
|
+
|
|
120
|
+
const workDir = getWorkDir();
|
|
121
|
+
const launchResult = await launchTerminal(workDir);
|
|
122
|
+
|
|
123
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
124
|
+
res.end(
|
|
125
|
+
JSON.stringify({
|
|
126
|
+
success: true,
|
|
127
|
+
mode: 'launched',
|
|
128
|
+
terminal: launchResult.terminal || 'Default Terminal',
|
|
129
|
+
message: '已启动终端唤醒 Pi,内容已在输入框备好!',
|
|
130
|
+
})
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
} catch (e) {
|
|
134
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
135
|
+
res.end(JSON.stringify({ success: false, error: e.message }));
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
142
|
+
res.end(JSON.stringify({ error: 'Not Found' }));
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
return server;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function startDaemon() {
|
|
149
|
+
const server = createBridgeServer();
|
|
150
|
+
|
|
151
|
+
server.listen(BRIDGE_PORT, '127.0.0.1', () => {
|
|
152
|
+
console.log(`===============================================`);
|
|
153
|
+
console.log(`🚀 send-to-pi Bridge Daemon is running!`);
|
|
154
|
+
console.log(`📡 Listening on: http://127.0.0.1:${BRIDGE_PORT}`);
|
|
155
|
+
console.log(`💻 Platform: ${process.platform}`);
|
|
156
|
+
console.log(`📂 Work Dir: ${getWorkDir()}`);
|
|
157
|
+
console.log(`===============================================`);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
server.on('error', (err) => {
|
|
161
|
+
if (err.code === 'EADDRINUSE') {
|
|
162
|
+
console.error(`[Bridge Error] Port ${BRIDGE_PORT} is already in use.`);
|
|
163
|
+
console.error(`If a previous instance is already running, you can keep using it.`);
|
|
164
|
+
process.exit(1);
|
|
165
|
+
} else {
|
|
166
|
+
console.error('[Bridge Error] Unexpected server error:', err);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
return server;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (require.main === module) {
|
|
174
|
+
startDaemon();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
module.exports = { startDaemon, createBridgeServer };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* macOS Terminal Launcher for send-to-pi
|
|
3
|
+
*
|
|
4
|
+
* Supports:
|
|
5
|
+
* - Terminal.app (macOS Built-in default)
|
|
6
|
+
* - iTerm2 (Most popular developer terminal)
|
|
7
|
+
* - Ghostty / WezTerm / Alacritty / Kitty (Modern GPU terminals)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const { execFile, spawn } = require('child_process');
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
|
|
13
|
+
function runAppleScript(script) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
execFile('osascript', ['-e', script], (error, stdout, stderr) => {
|
|
16
|
+
if (error) {
|
|
17
|
+
reject(new Error(stderr || error.message));
|
|
18
|
+
} else {
|
|
19
|
+
resolve(stdout.trim());
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function launchDarwin(workDir) {
|
|
26
|
+
const customTerminal = (process.env.PI_TERMINAL || '').toLowerCase();
|
|
27
|
+
|
|
28
|
+
// 1. 指定或探测常见现代独立终端 (Ghostty, Alacritty, WezTerm, Kitty)
|
|
29
|
+
if (customTerminal === 'ghostty' || (!customTerminal && fs.existsSync('/Applications/Ghostty.app'))) {
|
|
30
|
+
try {
|
|
31
|
+
const child = spawn('open', ['-a', 'Ghostty', '--args', '-e', `sh -c "cd '${workDir}' && pi"`], {
|
|
32
|
+
detached: true,
|
|
33
|
+
stdio: 'ignore',
|
|
34
|
+
});
|
|
35
|
+
child.unref();
|
|
36
|
+
return { success: true, terminal: 'Ghostty' };
|
|
37
|
+
} catch (_err) {}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (customTerminal === 'wezterm' || (!customTerminal && fs.existsSync('/Applications/WezTerm.app'))) {
|
|
41
|
+
try {
|
|
42
|
+
const child = spawn('open', ['-a', 'WezTerm', '--args', 'start', '--cwd', workDir, 'pi'], {
|
|
43
|
+
detached: true,
|
|
44
|
+
stdio: 'ignore',
|
|
45
|
+
});
|
|
46
|
+
child.unref();
|
|
47
|
+
return { success: true, terminal: 'WezTerm' };
|
|
48
|
+
} catch (_err) {}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 2. 探测 iTerm2
|
|
52
|
+
const hasITerm = fs.existsSync('/Applications/iTerm.app') || customTerminal === 'iterm2' || customTerminal === 'iterm';
|
|
53
|
+
if (hasITerm && customTerminal !== 'terminal') {
|
|
54
|
+
try {
|
|
55
|
+
const itermScript = `
|
|
56
|
+
tell application "iTerm"
|
|
57
|
+
activate
|
|
58
|
+
try
|
|
59
|
+
set newWindow to (create window with default profile)
|
|
60
|
+
tell current session of newWindow
|
|
61
|
+
write text "cd " & quoted form of "${workDir}" & " && pi"
|
|
62
|
+
end tell
|
|
63
|
+
on error
|
|
64
|
+
-- Fallback for older iTerm versions
|
|
65
|
+
tell current window
|
|
66
|
+
create tab with default profile
|
|
67
|
+
tell current session to write text "cd " & quoted form of "${workDir}" & " && pi"
|
|
68
|
+
end tell
|
|
69
|
+
end try
|
|
70
|
+
end tell
|
|
71
|
+
`;
|
|
72
|
+
await runAppleScript(itermScript);
|
|
73
|
+
return { success: true, terminal: 'iTerm2' };
|
|
74
|
+
} catch (err) {
|
|
75
|
+
console.warn('[darwin-launcher] iTerm launch failed, falling back to Terminal.app:', err.message);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 3. 兜底 macOS 官方原生 Terminal.app
|
|
80
|
+
const terminalScript = `
|
|
81
|
+
tell application "Terminal"
|
|
82
|
+
activate
|
|
83
|
+
do script "cd " & quoted form of "${workDir}" & " && pi"
|
|
84
|
+
end tell
|
|
85
|
+
`;
|
|
86
|
+
await runAppleScript(terminalScript);
|
|
87
|
+
return { success: true, terminal: 'Terminal.app' };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
module.exports = launchDarwin;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-Platform Terminal Launcher Dispatcher
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const launchDarwin = require('./darwin');
|
|
7
|
+
const launchWin32 = require('./win32');
|
|
8
|
+
const launchLinux = require('./linux');
|
|
9
|
+
|
|
10
|
+
async function launchTerminal(workDir) {
|
|
11
|
+
const platform = process.platform;
|
|
12
|
+
|
|
13
|
+
switch (platform) {
|
|
14
|
+
case 'darwin':
|
|
15
|
+
return await launchDarwin(workDir);
|
|
16
|
+
case 'win32':
|
|
17
|
+
return await launchWin32(workDir);
|
|
18
|
+
case 'linux':
|
|
19
|
+
return await launchLinux(workDir);
|
|
20
|
+
default:
|
|
21
|
+
return {
|
|
22
|
+
success: false,
|
|
23
|
+
error: `Unsupported platform: ${platform}`,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
module.exports = { launchTerminal };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Linux Terminal Launcher for send-to-pi
|
|
3
|
+
*
|
|
4
|
+
* Supports:
|
|
5
|
+
* - x-terminal-emulator
|
|
6
|
+
* - gnome-terminal
|
|
7
|
+
* - konsole
|
|
8
|
+
* - kitty / alacritty
|
|
9
|
+
* - xterm
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { spawn } = require('child_process');
|
|
13
|
+
|
|
14
|
+
function trySpawn(cmd, args, resolve) {
|
|
15
|
+
try {
|
|
16
|
+
const child = spawn(cmd, args, {
|
|
17
|
+
detached: true,
|
|
18
|
+
stdio: 'ignore',
|
|
19
|
+
});
|
|
20
|
+
child.on('error', () => resolve(false));
|
|
21
|
+
child.unref();
|
|
22
|
+
setTimeout(() => resolve(true), 200);
|
|
23
|
+
} catch {
|
|
24
|
+
resolve(false);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function launchLinux(workDir) {
|
|
29
|
+
const customTerminal = (process.env.PI_TERMINAL || '').toLowerCase();
|
|
30
|
+
|
|
31
|
+
const candidates = [
|
|
32
|
+
...(customTerminal ? [{ cmd: customTerminal, args: ['-e', `sh -c "cd '${workDir}' && pi; exec sh"`] }] : []),
|
|
33
|
+
{ cmd: 'x-terminal-emulator', args: ['-e', `sh -c "cd '${workDir}' && pi; exec sh"`] },
|
|
34
|
+
{ cmd: 'gnome-terminal', args: ['--working-directory', workDir, '--', 'sh', '-c', 'pi; exec sh'] },
|
|
35
|
+
{ cmd: 'konsole', args: ['--workdir', workDir, '-e', 'sh', '-c', 'pi; exec sh'] },
|
|
36
|
+
{ cmd: 'kitty', args: ['--directory', workDir, 'sh', '-c', 'pi; exec sh'] },
|
|
37
|
+
{ cmd: 'alacritty', args: ['--working-directory', workDir, '-e', 'sh', '-c', 'pi; exec sh'] },
|
|
38
|
+
{ cmd: 'xterm', args: ['-e', `cd '${workDir}' && pi; exec sh`] },
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
for (const item of candidates) {
|
|
42
|
+
const ok = await new Promise((resolve) => trySpawn(item.cmd, item.args, resolve));
|
|
43
|
+
if (ok) {
|
|
44
|
+
return { success: true, terminal: item.cmd };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return { success: false, error: 'No supported terminal emulator found on Linux system.' };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = launchLinux;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Windows Terminal Launcher for send-to-pi
|
|
3
|
+
*
|
|
4
|
+
* Supports:
|
|
5
|
+
* - Windows Terminal (wt.exe)
|
|
6
|
+
* - PowerShell (powershell.exe fallback)
|
|
7
|
+
* - Command Prompt (cmd.exe fallback)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const { spawn } = require('child_process');
|
|
11
|
+
|
|
12
|
+
function launchWin32(workDir) {
|
|
13
|
+
return new Promise((resolve) => {
|
|
14
|
+
// 1. 尝试 Windows Terminal (wt.exe)
|
|
15
|
+
try {
|
|
16
|
+
const wtArgs = ['-d', workDir, 'powershell', '-NoExit', '-Command', 'pi'];
|
|
17
|
+
const child = spawn('wt.exe', wtArgs, {
|
|
18
|
+
detached: true,
|
|
19
|
+
stdio: 'ignore',
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
child.on('error', (_err) => {
|
|
23
|
+
// wt.exe 不存在或失败,回退到 powershell.exe
|
|
24
|
+
fallbackPowerShell(workDir, resolve);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
child.unref();
|
|
28
|
+
// 在 Windows 上若成功触发 spawn 且无即时报错即视为已拉起
|
|
29
|
+
setTimeout(() => resolve({ success: true, terminal: 'Windows Terminal' }), 300);
|
|
30
|
+
} catch (_e) {
|
|
31
|
+
fallbackPowerShell(workDir, resolve);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function fallbackPowerShell(workDir, resolve) {
|
|
37
|
+
try {
|
|
38
|
+
const psArgs = ['-NoExit', '-Command', `Set-Location '${workDir}'; pi`];
|
|
39
|
+
const child = spawn('powershell.exe', psArgs, {
|
|
40
|
+
detached: true,
|
|
41
|
+
stdio: 'ignore',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
child.on('error', (_err) => {
|
|
45
|
+
fallbackCmd(workDir, resolve);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
child.unref();
|
|
49
|
+
setTimeout(() => resolve({ success: true, terminal: 'PowerShell' }), 300);
|
|
50
|
+
} catch (_e) {
|
|
51
|
+
fallbackCmd(workDir, resolve);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function fallbackCmd(workDir, resolve) {
|
|
56
|
+
try {
|
|
57
|
+
const child = spawn('cmd.exe', ['/k', `cd /d "${workDir}" && pi`], {
|
|
58
|
+
detached: true,
|
|
59
|
+
stdio: 'ignore',
|
|
60
|
+
});
|
|
61
|
+
child.unref();
|
|
62
|
+
resolve({ success: true, terminal: 'cmd.exe' });
|
|
63
|
+
} catch (err) {
|
|
64
|
+
resolve({ success: false, error: err.message });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = launchWin32;
|