magpie-select-translate 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,65 @@
1
- # Temporary Holding Version
1
+ # magpie-select-translate
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 划词翻译:在任意程序里选中文字,按 `Alt+D`,鼠标旁弹出浮窗,流式显示 magpie 模型的译文。
4
+ Select-to-translate for magpie: select text anywhere, press `Alt+D`, and the translation streams in a popover next to the cursor.
5
+
6
+ ## 它是什么
7
+
8
+ - 一个 magpie **页面插件**(Plugins → 划词翻译):配置热键、模型、目标语言、术语表。
9
+ - 一个原生 helper 进程 `helper.exe`(Go,单文件,约 6MB):全局热键、取词、浮窗、调网关。
10
+ 翻译请求走 `{gateway}/v1/chat/completions`(SSE、`temperature:0`、`reasoning_effort:low`)。
11
+ - 插件被禁用或加载失败也不影响 helper 自启(启动文件夹独立)。
12
+
13
+ ## 安装
14
+
15
+ ```sh
16
+ magpie plugin add magpie-select-translate # 从 npm(或 @scope/magpie-select-translate)
17
+ magpie plugin add ./plugin # 从本地目录
18
+ ```
19
+
20
+ helper 由插件在加载时拉起;包里有 `bin/helper.exe` 时会自动装到
21
+ `%LOCALAPPDATA%\magpie-translate\helper.exe`。没带二进制时自行编译:
22
+
23
+ ```powershell
24
+ go build -ldflags "-H windowsgui -s -w" -o "$env:LOCALAPPDATA\magpie-translate\helper.exe" ./cmd/helper
25
+ ```
26
+
27
+ ## 用法
28
+
29
+ 1. 选中文字(浏览器、记事本、Word、VS Code……)。
30
+ 2. 按热键(默认 `Alt+D`)。
31
+ 3. 浮窗里可以选中复制,点「复制」写入剪贴板;`Esc` 关闭;按住顶部可拖动。
32
+
33
+ `Windows Terminal`、`conhost`、远程桌面、密码管理器、资源管理器默认在黑名单里:
34
+ Ctrl+C 在这些程序里有副作用,命中时只提示「该应用不支持划词取词」。
35
+
36
+ ## 设置项
37
+
38
+ | 键 | 说明 |
39
+ |---|---|
40
+ | `port` | helper 本地端口,默认 3477 |
41
+ | `hotkey` | 全局热键,默认 `Alt+D`(改完要重启 helper) |
42
+ | `model` | 模型 id,留空用网关 `/v1/models` 的第一个 |
43
+ | `target` | `auto` / `zh` / `en` |
44
+ | `glossaryFile` | 术语表文件,每行 `原文=译文` |
45
+
46
+ 设置页在沙箱里没有网络(`connect-src 'none'`),所以它只读写 `magpie.options`;
47
+ 翻译状态请看浮窗本身。
48
+
49
+ ## 隐私
50
+
51
+ - helper 只监听 `127.0.0.1`,校验 `Host`。
52
+ - 日志只有时间、事件、耗时、字数,**不记原文**。
53
+ - 取词后会把剪贴板全量还原(图片等非 HGLOBAL 格式除外)。
54
+
55
+ ## 卸载
56
+
57
+ 1. `magpie plugin remove <包名>` 移除插件。
58
+ 2. 结束残留的 helper:任务管理器结束 `magpie-translate-helper.exe`,
59
+ 或运行 `%LOCALAPPDATA%\magpie-translate\uninstall.ps1`(插件首次启动时会把这份脚本拷过去)。
60
+
61
+ helper 只在 magpie 启动插件时被拉起,magpie 不开它就不会常驻,也不写注册表。
62
+
63
+ ## License
64
+
65
+ MIT
package/bin/helper.exe ADDED
Binary file
@@ -0,0 +1,18 @@
1
+ # 卸载 helper:停进程、删自启、删安装目录
2
+ #
3
+ # powershell -ExecutionPolicy Bypass -File scripts\uninstall.ps1
4
+ #
5
+ $ErrorActionPreference = "Stop"
6
+ $dest = Join-Path $env:LOCALAPPDATA "magpie-translate"
7
+ $vbs = Join-Path ([Environment]::GetFolderPath("Startup")) "start-magpie-translate.vbs"
8
+
9
+ Get-Process magpie-translate-helper -ErrorAction SilentlyContinue | Stop-Process -Force
10
+ if (Test-Path $vbs) {
11
+ Remove-Item $vbs -Force
12
+ Write-Host "==> 已删除自启 $vbs"
13
+ }
14
+ if (Test-Path $dest) {
15
+ Remove-Item $dest -Recurse -Force
16
+ Write-Host "==> 已删除 $dest"
17
+ }
18
+ Write-Host "==> 完成。magpie 里的插件用 magpie plugin remove magpie-select-translate 移除"
package/index.mjs ADDED
@@ -0,0 +1,61 @@
1
+ // magpie 插件入口:只负责探活 + 拉起 helper,翻译逻辑全在 helper 里。
2
+ import { spawn } from "node:child_process"
3
+ import { copyFileSync, existsSync, mkdirSync } from "node:fs"
4
+ import { dirname, join } from "node:path"
5
+ import { fileURLToPath } from "node:url"
6
+
7
+ const here = dirname(fileURLToPath(import.meta.url))
8
+ const home = join(process.env.LOCALAPPDATA || process.env.HOME || ".", "magpie-translate")
9
+
10
+ const alive = async (port) => {
11
+ try {
12
+ const res = await fetch(`http://127.0.0.1:${port}/api/ping`, {
13
+ signal: AbortSignal.timeout(800),
14
+ })
15
+ return res.ok
16
+ } catch {
17
+ return false
18
+ }
19
+ }
20
+
21
+ // 优先用已安装(%LOCALAPPDATA%)的那份;npm 包自带 bin/helper.exe 时就地装过去。
22
+ // 装到固定目录是为了让设置、日志、卸载脚本只有一份。
23
+ const ensureHelper = () => {
24
+ const installed = join(home, "helper.exe")
25
+ if (existsSync(installed)) return installed
26
+ const bundled = join(here, "bin", "helper.exe")
27
+ if (!existsSync(bundled)) return ""
28
+ try {
29
+ mkdirSync(home, { recursive: true })
30
+ copyFileSync(bundled, installed)
31
+ // 顺手留个卸载脚本,用户不用回来翻 node_modules
32
+ const uninstall = join(here, "bin", "uninstall.ps1")
33
+ if (existsSync(uninstall)) copyFileSync(uninstall, join(home, "uninstall.ps1"))
34
+ } catch {
35
+ return bundled // 装不过去就直接跑包里的那份
36
+ }
37
+ return installed
38
+ }
39
+
40
+ export default {
41
+ id: "magpie-select-translate",
42
+ server: async (_input, options) => {
43
+ const port = Number(options?.port) || 3477
44
+ if (await alive(port)) return { port }
45
+
46
+ const exe = ensureHelper()
47
+ if (!exe) {
48
+ return {
49
+ port,
50
+ error:
51
+ "helper 未安装:运行仓库里的 scripts/install.ps1,或安装带 bin/helper.exe 的发行包",
52
+ }
53
+ }
54
+ spawn(exe, ["--port", String(port)], {
55
+ detached: true,
56
+ stdio: "ignore",
57
+ windowsHide: true,
58
+ }).unref()
59
+ return { port }
60
+ },
61
+ }
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
- {
1
+ {
2
2
  "name": "magpie-select-translate",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "description": "鍒掕瘝缈昏瘧锛氶€変腑鏂囧瓧鎸夌儹閿紝榧犳爣鏃佹诞绐楁祦寮忔樉绀?magpie 妯″瀷鐨勮瘧鏂囥€係elect-to-translate popover for magpie: hit a hotkey on any selection and the translation streams next to your cursor.",
5
+ "type": "module",
6
+ "main": "./index.mjs",
7
+ "files": [
8
+ "index.mjs",
9
+ "page",
10
+ "bin",
11
+ "README.md"
12
+ ],
13
+ "keywords": [
14
+ "magpie",
15
+ "magpie-page",
16
+ "magpie-plugin",
17
+ "translation",
18
+ "translate",
19
+ "selection"
20
+ ],
21
+ "license": "MIT",
22
+ "engines": {
23
+ "magpie": ">=0.1.1340"
24
+ },
25
+ "magpie": {
26
+ "page": {
27
+ "title": "Translate",
28
+ "titles": {
29
+ "zh": "鍒掕瘝缈昏瘧",
30
+ "en": "Translate"
31
+ },
32
+ "file": "page/index.html"
33
+ },
34
+ "options": {
35
+ "port": 3477,
36
+ "hotkey": "Alt+D",
37
+ "model": "",
38
+ "target": "auto",
39
+ "glossaryFile": ""
40
+ }
41
+ }
6
42
  }
@@ -0,0 +1,120 @@
1
+ <!doctype html>
2
+ <html lang="zh">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>划词翻译</title>
6
+ <style>
7
+ :root { color-scheme: light dark; }
8
+ body {
9
+ margin: 0; padding: 20px 22px;
10
+ font: 14px/1.7 "Microsoft YaHei UI", "Segoe UI", system-ui, sans-serif;
11
+ }
12
+ h1 { font-size: 16px; margin: 0 0 4px; }
13
+ p.sub { margin: 0 0 18px; opacity: .7; font-size: 12px; }
14
+ label { display: block; margin: 14px 0 4px; font-size: 12px; opacity: .8; }
15
+ input, select {
16
+ width: 100%; box-sizing: border-box; padding: 7px 9px; font: inherit;
17
+ border: 1px solid color-mix(in srgb, currentColor 20%, transparent);
18
+ border-radius: 8px; background: transparent; color: inherit;
19
+ }
20
+ .row { display: flex; gap: 10px; }
21
+ .row > div { flex: 1; }
22
+ #status { margin-top: 16px; font-size: 12px; opacity: .75; min-height: 1.4em; }
23
+ .hint { margin-top: 18px; font-size: 12px; opacity: .6; }
24
+ code { font-family: Consolas, monospace; }
25
+ </style>
26
+ </head>
27
+ <body>
28
+ <h1>划词翻译</h1>
29
+ <p class="sub">选中文字后按热键,浮窗流式显示译文。</p>
30
+
31
+ <div class="row">
32
+ <div>
33
+ <label for="hotkey">热键(改完重启 helper 生效)</label>
34
+ <input id="hotkey" placeholder="Alt+D">
35
+ </div>
36
+ <div>
37
+ <label for="target">目标语言</label>
38
+ <select id="target">
39
+ <option value="auto">自动判定</option>
40
+ <option value="zh">中文</option>
41
+ <option value="en">English</option>
42
+ </select>
43
+ </div>
44
+ </div>
45
+
46
+ <label for="model">模型 id(留空用网关第一个模型)</label>
47
+ <input id="model" placeholder="group/deepseek-v4.1-flash">
48
+
49
+ <label for="glossaryFile">术语表文件(每行 原文=译文)</label>
50
+ <input id="glossaryFile" placeholder="D:\work\glossary.txt">
51
+
52
+ <label for="port">helper 端口</label>
53
+ <input id="port" type="number" placeholder="3477">
54
+
55
+ <div id="status"></div>
56
+ <p class="hint">
57
+ 设置页在沙箱里不能联网,所以这里只读写 magpie 的选项;翻译状态请按热键看浮窗。
58
+ helper 未安装时运行 <code>scripts/install.ps1</code>。
59
+ </p>
60
+
61
+ <script>
62
+ const fields = ["hotkey", "model", "target", "glossaryFile", "port"]
63
+ const $ = (id) => document.getElementById(id)
64
+ let timer = null
65
+
66
+ const status = (msg, ok) => {
67
+ $("status").textContent = msg
68
+ $("status").style.color = ok === false ? "#e5484d" : ""
69
+ }
70
+
71
+ async function boot() {
72
+ const api = window.magpie
73
+ if (!api || !api.options) {
74
+ status("不在 magpie 里打开时无法保存设置", false)
75
+ return
76
+ }
77
+ // magpie.ready 是个 Promise:宿主把语言/主题/选项准备好后才会 resolve。
78
+ try {
79
+ await api.ready
80
+ } catch {
81
+ /* 没有宿主时保持默认 */
82
+ }
83
+ try {
84
+ const opts = (await api.options.get()) || {}
85
+ for (const f of fields) if (opts[f] !== undefined) $(f).value = opts[f]
86
+ status("")
87
+ } catch (e) {
88
+ status("读取设置失败:" + e.message, false)
89
+ return
90
+ }
91
+ for (const f of fields) {
92
+ $(f).addEventListener("input", () => {
93
+ clearTimeout(timer)
94
+ timer = setTimeout(save, 1000)
95
+ })
96
+ $(f).addEventListener("change", () => {
97
+ clearTimeout(timer)
98
+ timer = setTimeout(save, 300)
99
+ })
100
+ }
101
+ }
102
+
103
+ async function save() {
104
+ const opts = {}
105
+ for (const f of fields) {
106
+ const v = $(f).value.trim()
107
+ if (v !== "") opts[f] = f === "port" ? Number(v) : v
108
+ }
109
+ try {
110
+ await window.magpie.options.set(opts)
111
+ status("已保存 · " + new Date().toLocaleTimeString())
112
+ } catch (e) {
113
+ status("保存失败:" + e.message, false)
114
+ }
115
+ }
116
+
117
+ boot()
118
+ </script>
119
+ </body>
120
+ </html>