dsh-bilingual-reader 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/LICENSE +21 -0
- package/README.en.md +146 -0
- package/README.md +145 -2
- package/client/client.js +88 -0
- package/cordis.patch.yml +7 -0
- package/docs/architecture.md +66 -0
- package/docs/demo.jpg +0 -0
- package/docs/release-v0.1.0.md +47 -0
- package/docs/release-v0.1.1.md +31 -0
- package/index.js +82 -0
- package/package.json +76 -4
- package/src/cache.js +76 -0
- package/src/client.js +59 -0
- package/src/config.js +74 -0
- package/src/extract.js +259 -0
- package/src/fetch.js +183 -0
- package/src/glossary.js +74 -0
- package/src/prompt.js +125 -0
- package/src/protect.js +71 -0
- package/src/routes.js +317 -0
- package/src/security.js +169 -0
- package/src/segment.js +116 -0
- package/src/store.js +325 -0
- package/src/tools.js +220 -0
- package/src/translator.js +357 -0
- package/src/util.js +62 -0
- package/web/app.js +477 -0
- package/web/favicon.svg +5 -0
- package/web/index.html +99 -0
- package/web/style.css +488 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bit0620
|
|
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.en.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# DSH Bilingual Reader
|
|
2
|
+
|
|
3
|
+
[中文](README.md) | **English**
|
|
4
|
+
|
|
5
|
+
> **Read the web bilingually: the translation sits under each paragraph while code, formulas, links and tables stay intact.** A local-first reading workbench for DeepSeek Harness.
|
|
6
|
+
|
|
7
|
+
[](https://github.com/bit0620/dsh-bilingual-reader/actions/workflows/test.yml)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
[](package.json)
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## Why
|
|
14
|
+
|
|
15
|
+
DSH already has 297 translation-related plugins, but most cover a single entry point: selection translation, assistant-reply translation, PDF translation, or paper/book reading. **None completes the chain from an arbitrary web article to clean text, paragraph-level bilingual output, a glossary, a cache, export, and agent tools.**
|
|
16
|
+
|
|
17
|
+
This plugin replicates the core experience of Immersive Translate and Read Frog, but in a DSH-native way:
|
|
18
|
+
|
|
19
|
+
- **Bilingual next to the original** — every paragraph keeps its translation underneath; no separate translation window.
|
|
20
|
+
- **Read while it translates** — finished paragraphs appear in place, and progress updates replace only the blocks that changed, so your scroll position stays put.
|
|
21
|
+
- **Structure preserved** — code blocks, inline code, LaTeX, link URLs, tables and heading levels are protected; dropped tokens are detected and retried.
|
|
22
|
+
- **Token-efficient** — blocks are batched into one request; translations are cached by `original + target language + glossary hash`.
|
|
23
|
+
- **Reusable** — a glossary enforces consistent terms; bilingual Markdown exports; agents read and write the same data.
|
|
24
|
+
- **Local-first** — data lives in `~/.dsh-bilingual-reader/`; no telemetry, no separate API key.
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
Requires **DSH >= 0.2.0 < 0.3.0** and **Node.js >= 22.12**.
|
|
29
|
+
|
|
30
|
+
From npm:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
dsh plugin --profile web add dsh-bilingual-reader
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
From GitHub:
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
dsh plugin --profile web add github:Bit0620/dsh-bilingual-reader
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Local development:
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
dsh plugin --profile web add file:C:/path/to/dsh-bilingual-reader
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Restart DSH and open **Bilingual Reader** in the sidebar. The plugin uses the **DSH default model**, so no separate API key is required. If the profile has no model, the UI explains it; already translated documents remain readable.
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
### Sidebar
|
|
53
|
+
|
|
54
|
+
Paste a public article URL, choose the target language (Simplified Chinese, Traditional Chinese, English, Japanese, Korean) and click **Read and translate**.
|
|
55
|
+
|
|
56
|
+
- **Inline** (default) — the translation sits directly under each paragraph, best for narrow windows and immersive reading.
|
|
57
|
+
- **Bilingual** — original on the left, translation on the right.
|
|
58
|
+
- **Original** / **Translation** — single-column reading; the view choice is remembered.
|
|
59
|
+
- **Glossary** — add terms such as `token → 令牌`; translations must use them.
|
|
60
|
+
- **Export bilingual Markdown** — headings, lists, tables and code blocks are kept.
|
|
61
|
+
- **Retry failed blocks** — completed blocks are not translated again.
|
|
62
|
+
|
|
63
|
+
### Command
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
/bilingual https://example.com/article
|
|
67
|
+
/bilingual https://example.com/article zh-TW
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The command returns a document id immediately; translation continues in the background.
|
|
71
|
+
|
|
72
|
+
### Agent tools
|
|
73
|
+
|
|
74
|
+
| Tool | Purpose |
|
|
75
|
+
| --- | --- |
|
|
76
|
+
| `bilingual_read_url` | Fetch a public page, extract the article, translate and store a bilingual document; waits by default, `wait=false` for long reads |
|
|
77
|
+
| `bilingual_translate` | Translate Markdown/plain text; `bilingual=true` returns original + translation |
|
|
78
|
+
| `bilingual_status` | Show progress, failed blocks and partial translations |
|
|
79
|
+
| `bilingual_glossary` | List, add or remove glossary terms |
|
|
80
|
+
|
|
81
|
+
## How it works
|
|
82
|
+
|
|
83
|
+
1. `fetchHtml` fetches the page with manual redirects, per-hop SSRF checks, DNS-rebinding protection and GBK/Big5 charset detection.
|
|
84
|
+
2. Readability extracts the article; Turndown converts it to Markdown.
|
|
85
|
+
3. `segmentMarkdown` splits it into blocks: headings, paragraphs, lists, tables and quotes are translatable; code fences, rules and image-only blocks are skipped.
|
|
86
|
+
4. `protectInline` replaces inline code, LaTeX, link URLs, bare URLs and HTML tags with `⟦n⟧` tokens.
|
|
87
|
+
5. Up to 6 blocks / 3500 characters are sent per request; the model must return an ordered JSON array. A failed batch falls back to single-block requests, and 2 batches run at a time by default.
|
|
88
|
+
6. Tokens are restored, heading prefixes are repaired, and translations are written to the cache by content hash.
|
|
89
|
+
7. Documents live under `~/.dsh-bilingual-reader/docs/<id>/`; the UI defaults to inline bilingual view and exports bilingual Markdown.
|
|
90
|
+
|
|
91
|
+
## Configuration
|
|
92
|
+
|
|
93
|
+
| Variable | Default | Meaning |
|
|
94
|
+
| --- | --- | --- |
|
|
95
|
+
| `DSH_BILINGUAL_READER_DIR` | `~/.dsh-bilingual-reader` | Data directory |
|
|
96
|
+
| `DSH_BILINGUAL_TARGET_LANG` | `zh-CN` | Default target language |
|
|
97
|
+
| `DSH_BILINGUAL_BATCH_BLOCKS` | `6` | Max blocks per request |
|
|
98
|
+
| `DSH_BILINGUAL_BATCH_CHARS` | `3500` | Max characters per request |
|
|
99
|
+
| `DSH_BILINGUAL_CONCURRENCY` | `2` | Concurrent translation batches (1–4) |
|
|
100
|
+
| `DSH_BILINGUAL_MAX_BLOCKS` | `400` | Max blocks per document |
|
|
101
|
+
| `DSH_BILINGUAL_MAX_BYTES` | `5242880` | Page size cap |
|
|
102
|
+
| `DSH_BILINGUAL_TIMEOUT_MS` | `20000` | Fetch timeout |
|
|
103
|
+
| `DSH_BILINGUAL_PROXY` | environment proxy | Fetch proxy |
|
|
104
|
+
| `DSH_BILINGUAL_ALLOW_PRIVATE` | `0` | Allow private hosts (tests only) |
|
|
105
|
+
| `DSH_BILINGUAL_BLOCK_FAKE_IP` | `0` | Reject 198.18/15 fake-IPs (breaks Clash TUN) |
|
|
106
|
+
|
|
107
|
+
## Security and privacy
|
|
108
|
+
|
|
109
|
+
- http/https only; loopback, private, link-local, CGNAT, multicast and cloud-metadata hosts are rejected, and every redirect is re-checked.
|
|
110
|
+
- Rendered HTML is filtered through a `sanitize-html` allowlist; HTTP routes are loopback-only.
|
|
111
|
+
- Article text is never uploaded; no telemetry. Model calls go through the DSH provider.
|
|
112
|
+
- Cache and glossary are local JSON files that can be backed up or deleted.
|
|
113
|
+
|
|
114
|
+
## Limitations
|
|
115
|
+
|
|
116
|
+
- Public pages only; login walls, SPAs and anti-bot pages may fail extraction and will suggest pasting the text.
|
|
117
|
+
- No PDF/EPUB/DOCX layout translation in v0.1; convert PDFs to Markdown first.
|
|
118
|
+
- Does not translate assistant replies (covered by `dsh-bilingual` / `dsh-think-translate`); it focuses on web and document reading.
|
|
119
|
+
- Translation quality depends on the DSH default model; malformed batch responses fall back to single blocks.
|
|
120
|
+
- Cache entries are shared across documents; changing the glossary produces new cache keys.
|
|
121
|
+
|
|
122
|
+
## Development
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
npm install
|
|
126
|
+
npm test # 36 tests: segmentation, protection, prompts, cache, glossary, security, store/translator
|
|
127
|
+
npm run verify # local HTTP fixture → fetch → translate → export → cache
|
|
128
|
+
npm run dev # local UI with a demo model: http://127.0.0.1:4183/dsh-bilingual-reader/
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Demo article for `npm run dev`: `http://127.0.0.1:4183/demo-article.html`.
|
|
132
|
+
|
|
133
|
+
Architecture and data layout: [docs/architecture.md](docs/architecture.md).
|
|
134
|
+
|
|
135
|
+
## Inspiration
|
|
136
|
+
|
|
137
|
+
Product shape inspired by [Immersive Translate](https://github.com/immersive-translate/immersive-translate), [Read Frog](https://github.com/tomode1/read-frog), [NextAI Translator](https://github.com/nextai-translator/nextai-translator) and [Saladict](https://github.com/crimx/ext-saladict). The implementation is independent; no affiliation.
|
|
138
|
+
|
|
139
|
+
## Release notes
|
|
140
|
+
|
|
141
|
+
- [v0.1.1](docs/release-v0.1.1.md): inline bilingual view by default, incremental progress rendering, parallel batches, cache and write consistency fixes.
|
|
142
|
+
- [v0.1.0](docs/release-v0.1.0.md): first release — web/document bilingual reading, glossary, cache, export and agent tools.
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
MIT
|
package/README.md
CHANGED
|
@@ -1,3 +1,146 @@
|
|
|
1
|
-
#
|
|
1
|
+
# DSH 双语阅读
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**中文** | [English](README.en.md)
|
|
4
|
+
|
|
5
|
+
> **把网页读成双语:原文和译文贴着排,代码、公式、链接和表格保持原样。** 面向 DeepSeek Harness 的本地优先阅读工作台。
|
|
6
|
+
|
|
7
|
+
[](https://github.com/bit0620/dsh-bilingual-reader/actions/workflows/test.yml)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
[](package.json)
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## 为什么做这个
|
|
14
|
+
|
|
15
|
+
DSH 里已经有 297 个和翻译相关的插件,但大多只覆盖一个入口:选中文本翻译、助手回复翻译、PDF 翻译,或者论文/书籍阅读。**没有一个把“任意网页文章 → 干净正文 → 段落级双语 → 术语表 → 缓存 → 导出 → Agent 工具”这条链路做完整。**
|
|
16
|
+
|
|
17
|
+
这个插件复刻的是 Immersive Translate / 陪读蛙最核心的体验,但按 DSH 的方式做:
|
|
18
|
+
|
|
19
|
+
- **双语贴着原文**:每段原文下方就是译文,不跳转到另一个翻译窗口。
|
|
20
|
+
- **边译边读**:完成的段落立即出现,进度更新只替换变化的段落,不会重置滚动位置。
|
|
21
|
+
- **保结构**:代码块、行内代码、公式、链接 URL、表格、标题层级不翻译;模型丢失保护片段会被检测并重试。
|
|
22
|
+
- **省 token**:多段合并成一次请求;按“原文 + 目标语言 + 术语表哈希”缓存,重复段落不再调用模型。
|
|
23
|
+
- **可沉淀**:术语表强制统一译法;双语 Markdown 可导出;Agent 可以读写同一份数据。
|
|
24
|
+
- **本地优先**:数据在 `~/.dsh-bilingual-reader/`,不发送遥测,不引入独立 API key。
|
|
25
|
+
|
|
26
|
+
## 安装
|
|
27
|
+
|
|
28
|
+
要求 **DSH >= 0.2.0 < 0.3.0**、**Node.js >= 22.12**。
|
|
29
|
+
|
|
30
|
+
从 npm 安装:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
dsh plugin --profile web add dsh-bilingual-reader
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
直接从 GitHub 安装:
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
dsh plugin --profile web add github:Bit0620/dsh-bilingual-reader
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
本地开发:
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
dsh plugin --profile web add file:C:/path/to/dsh-bilingual-reader
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
安装后重启 DSH,侧栏会出现“双语阅读”。插件使用 **DSH 默认模型**,不需要额外配置 API key;如果 profile 没有可用模型,界面会提示,已翻译的文档仍可浏览。
|
|
49
|
+
|
|
50
|
+
## 使用
|
|
51
|
+
|
|
52
|
+
### 侧栏
|
|
53
|
+
|
|
54
|
+
粘贴公开网页链接,选择目标语言(简体中文 / 繁體中文 / English / 日本語 / 한국어),点击“读取并翻译”。
|
|
55
|
+
|
|
56
|
+
- **上下**(默认):原文在上,译文紧随其下,适合窄窗口和沉浸阅读。
|
|
57
|
+
- **对照**:左原文、右译文。
|
|
58
|
+
- **原文** / **译文**:单栏阅读;视图选择会记住。
|
|
59
|
+
- **术语表**:添加 `token → 令牌` 这类术语,翻译时强制使用。
|
|
60
|
+
- **导出双语 Markdown**:保留标题、列表、表格和代码块。
|
|
61
|
+
- 失败的段落可以“重试失败段”,不会重跑已完成的段落。
|
|
62
|
+
|
|
63
|
+
### 命令
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
/bilingual https://example.com/article
|
|
67
|
+
/bilingual https://example.com/article zh-TW
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
命令会立即返回文档 id,翻译在后台进行,可在侧栏查看进度。
|
|
71
|
+
|
|
72
|
+
### Agent 工具
|
|
73
|
+
|
|
74
|
+
| 工具 | 作用 |
|
|
75
|
+
| --- | --- |
|
|
76
|
+
| `bilingual_read_url` | 抓取公开网页、提取正文、翻译并保存双语文档;默认等待完成,长文可 `wait=false` |
|
|
77
|
+
| `bilingual_translate` | 翻译一段 Markdown/纯文本;`bilingual=true` 返回原文 + 译文 |
|
|
78
|
+
| `bilingual_status` | 查看文档进度、失败段落和部分译文 |
|
|
79
|
+
| `bilingual_glossary` | 术语表 list / add / remove |
|
|
80
|
+
|
|
81
|
+
## 工作原理
|
|
82
|
+
|
|
83
|
+
1. `fetchHtml` 抓取页面:手动重定向、逐跳 SSRF 校验、DNS rebinding 防护、GBK/Big5 字符集检测。
|
|
84
|
+
2. Readability 抽取正文,Turndown 转成 Markdown。
|
|
85
|
+
3. `segmentMarkdown` 按块切分:标题、段落、列表、表格、引用可翻译;代码块、分隔线、纯图片跳过。
|
|
86
|
+
4. `protectInline` 把行内代码、LaTeX、链接 URL、裸 URL、HTML 标签替换成 `⟦n⟧` 占位符。
|
|
87
|
+
5. 默认每 6 段 / 3500 字符合并成一次模型请求,要求返回同序 JSON 数组;失败拆成单段重试。默认同时跑 2 个批次。
|
|
88
|
+
6. 译文恢复占位符;标题补回 `#` 前缀;按内容哈希写入缓存。
|
|
89
|
+
7. 文档写入 `~/.dsh-bilingual-reader/docs/<id>/`,前端默认以“上下对照”逐段呈现,导出时生成双语 Markdown。
|
|
90
|
+
|
|
91
|
+
## 配置
|
|
92
|
+
|
|
93
|
+
| 环境变量 | 默认 | 说明 |
|
|
94
|
+
| --- | --- | --- |
|
|
95
|
+
| `DSH_BILINGUAL_READER_DIR` | `~/.dsh-bilingual-reader` | 数据目录 |
|
|
96
|
+
| `DSH_BILINGUAL_TARGET_LANG` | `zh-CN` | 默认目标语言 |
|
|
97
|
+
| `DSH_BILINGUAL_BATCH_BLOCKS` | `6` | 每次请求最多段落数 |
|
|
98
|
+
| `DSH_BILINGUAL_BATCH_CHARS` | `3500` | 每次请求最多字符数 |
|
|
99
|
+
| `DSH_BILINGUAL_CONCURRENCY` | `2` | 同时翻译的批次数(1–4) |
|
|
100
|
+
| `DSH_BILINGUAL_MAX_BLOCKS` | `400` | 单篇最多段落数 |
|
|
101
|
+
| `DSH_BILINGUAL_MAX_BYTES` | `5242880` | 页面大小上限 |
|
|
102
|
+
| `DSH_BILINGUAL_TIMEOUT_MS` | `20000` | 抓取超时 |
|
|
103
|
+
| `DSH_BILINGUAL_PROXY` | 环境代理 | 抓取代理 |
|
|
104
|
+
| `DSH_BILINGUAL_ALLOW_PRIVATE` | `0` | 允许抓取内网(仅测试) |
|
|
105
|
+
| `DSH_BILINGUAL_BLOCK_FAKE_IP` | `0` | 拒绝 198.18/15 fake-IP(会破坏 Clash TUN) |
|
|
106
|
+
|
|
107
|
+
## 安全与隐私
|
|
108
|
+
|
|
109
|
+
- 只接受 http/https;拒绝回环、私网、链路本地、CGNAT、组播、云 metadata 地址;每次重定向重新校验。
|
|
110
|
+
- 页面渲染使用 `sanitize-html` 白名单;HTTP 路由只允许回环请求。
|
|
111
|
+
- 不上传正文,不发送遥测;模型调用走 DSH 已配置的 provider。
|
|
112
|
+
- 缓存和术语表都是本地 JSON,可直接备份或删除。
|
|
113
|
+
|
|
114
|
+
## 限制
|
|
115
|
+
|
|
116
|
+
- 只支持公开页面;登录墙、SPA、反爬页可能提取失败,会提示改用粘贴正文。
|
|
117
|
+
- 第一版不做 PDF / EPUB / DOCX 版面翻译;PDF 可以先用其他工具转 Markdown。
|
|
118
|
+
- 不翻译助手回复(已有 `dsh-bilingual` / `dsh-think-translate` 覆盖),专注网页与文档阅读。
|
|
119
|
+
- 翻译质量取决于 DSH 默认模型;批量解析失败会自动降级到单段。
|
|
120
|
+
- 缓存按段落命中,跨文档共享;修改术语表会产生新的缓存键。
|
|
121
|
+
|
|
122
|
+
## 开发
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
npm install
|
|
126
|
+
npm test # 36 项单测:切分、保护、提示词、缓存、术语表、安全、store/translator
|
|
127
|
+
npm run verify # 本地 HTTP fixture → 抓取 → 翻译 → 导出 → 缓存
|
|
128
|
+
npm run dev # 带示范模型的本地 UI:http://127.0.0.1:4183/dsh-bilingual-reader/
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`npm run dev` 的示范文章:`http://127.0.0.1:4183/demo-article.html`。
|
|
132
|
+
|
|
133
|
+
架构与数据格式见 [docs/architecture.md](docs/architecture.md)。
|
|
134
|
+
|
|
135
|
+
## 灵感与致谢
|
|
136
|
+
|
|
137
|
+
产品形态参考 [Immersive Translate](https://github.com/immersive-translate/immersive-translate)、[陪读蛙 Read Frog](https://github.com/tomode1/read-frog)、[NextAI Translator](https://github.com/nextai-translator/nextai-translator) 和 [Saladict](https://github.com/crimx/ext-saladict);代码为独立实现,与这些项目没有隶属关系。
|
|
138
|
+
|
|
139
|
+
## 版本说明
|
|
140
|
+
|
|
141
|
+
- [v0.1.1](docs/release-v0.1.1.md):默认“上下对照”视图,逐段渐进更新,批量并行翻译,缓存与并发写入修正。
|
|
142
|
+
- [v0.1.0](docs/release-v0.1.0.md):首个版本,网页/文档双语阅读、术语表、缓存、导出与 Agent 工具。
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
MIT
|
package/client/client.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/* dsh-bilingual-reader client — built by scripts/build-client.mjs */
|
|
2
|
+
window.__ModuleLoader__.load({
|
|
3
|
+
id: 'dsh-bilingual-reader',
|
|
4
|
+
factory: (e) => {
|
|
5
|
+
const module = { exports: {} }
|
|
6
|
+
const exports = module.exports
|
|
7
|
+
const require = e
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name2 in all)
|
|
14
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
|
|
26
|
+
// src/client.js
|
|
27
|
+
var client_exports = {};
|
|
28
|
+
__export(client_exports, {
|
|
29
|
+
apply: () => apply,
|
|
30
|
+
inject: () => inject,
|
|
31
|
+
name: () => name
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(client_exports);
|
|
34
|
+
var import_react = require("react");
|
|
35
|
+
var name = "dsh-bilingual-reader-client";
|
|
36
|
+
var inject = ["slots"];
|
|
37
|
+
var ID = "dsh-bilingual-reader";
|
|
38
|
+
var LABEL = "\u53CC\u8BED\u9605\u8BFB";
|
|
39
|
+
function TranslateIcon(owner = {}) {
|
|
40
|
+
const size = owner.size ?? 16;
|
|
41
|
+
return (0, import_react.createElement)(
|
|
42
|
+
"svg",
|
|
43
|
+
{
|
|
44
|
+
width: size,
|
|
45
|
+
height: size,
|
|
46
|
+
viewBox: "0 0 24 24",
|
|
47
|
+
fill: "none",
|
|
48
|
+
stroke: "currentColor",
|
|
49
|
+
strokeWidth: 2,
|
|
50
|
+
strokeLinecap: "round",
|
|
51
|
+
strokeLinejoin: "round",
|
|
52
|
+
"aria-hidden": "true"
|
|
53
|
+
},
|
|
54
|
+
(0, import_react.createElement)("path", { d: "M4 5h8M8 3v2c0 4-1.5 7-4 9" }),
|
|
55
|
+
(0, import_react.createElement)("path", { d: "M5 11c2 2.5 4.5 4 7 5" }),
|
|
56
|
+
(0, import_react.createElement)("path", { d: "M14 20l4-10 4 10" }),
|
|
57
|
+
(0, import_react.createElement)("path", { d: "M15.5 16h5" })
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
function apply(ctx) {
|
|
61
|
+
const slots = ctx.slots;
|
|
62
|
+
slots.inject("sidebar.panellist", () => slots.register({
|
|
63
|
+
name: "sidebar.panellist",
|
|
64
|
+
id: ID,
|
|
65
|
+
order: 3,
|
|
66
|
+
label: () => LABEL
|
|
67
|
+
}, TranslateIcon));
|
|
68
|
+
slots.inject("main", () => slots.register({
|
|
69
|
+
name: "main",
|
|
70
|
+
id: ID,
|
|
71
|
+
key: ID,
|
|
72
|
+
order: 3
|
|
73
|
+
}, () => (0, import_react.createElement)("iframe", {
|
|
74
|
+
src: "/dsh-bilingual-reader/",
|
|
75
|
+
title: LABEL,
|
|
76
|
+
style: {
|
|
77
|
+
display: "block",
|
|
78
|
+
width: "100%",
|
|
79
|
+
height: "100%",
|
|
80
|
+
border: "0",
|
|
81
|
+
background: "transparent"
|
|
82
|
+
}
|
|
83
|
+
})));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return module.exports
|
|
87
|
+
},
|
|
88
|
+
});
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# dsh-bilingual-reader is a self-contained bundle: it stores documents and the
|
|
2
|
+
# translation cache under ~/.dsh-bilingual-reader, serves its own reading UI on
|
|
3
|
+
# the host's webserver, and registers bilingual_* tools. No MCP row and no
|
|
4
|
+
# credentials are involved.
|
|
5
|
+
- insert:
|
|
6
|
+
- id: dsh-bilingual-reader
|
|
7
|
+
name: dsh-bilingual-reader
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# 架构说明
|
|
2
|
+
|
|
3
|
+
`dsh-bilingual-reader` 是一个 DSH 插件 bundle:服务端注册工具和 HTTP 路由,前端是同一路由下的独立页面,通过 iframe 放进 DSH 侧栏与主区域。
|
|
4
|
+
|
|
5
|
+
## 数据流
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
URL ──fetchHtml──> HTML ──Readability + Turndown──> Markdown
|
|
9
|
+
│
|
|
10
|
+
segmentMarkdown
|
|
11
|
+
│
|
|
12
|
+
┌───────────────┬─────────────┴─────────────┐
|
|
13
|
+
│ │ │
|
|
14
|
+
cache 命中 批量翻译(默认 6 段 / 3500 字符,2 批并行) 单段重试
|
|
15
|
+
│ │ │
|
|
16
|
+
└───────────────┴─────────────┬─────────────┘
|
|
17
|
+
│
|
|
18
|
+
doc.json(导出时生成 bilingual.md)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 模块
|
|
22
|
+
|
|
23
|
+
| 文件 | 职责 |
|
|
24
|
+
| --- | --- |
|
|
25
|
+
| `index.js` | 插件入口:安装 store、路由、工具、`/bilingual` 命令与 system prompt |
|
|
26
|
+
| `src/fetch.js` | 带手动重定向、SSRF 校验、DNS rebinding 防护、字符集检测的抓取 |
|
|
27
|
+
| `src/security.js` | 内网 / 回环 / 链路本地 / CGNAT / IPv6 映射地址判定 |
|
|
28
|
+
| `src/extract.js` | Readability 正文抽取、Turndown Markdown、`renderMarkdown` 安全渲染 |
|
|
29
|
+
| `src/segment.js` | Markdown 块切分;代码块 / 分隔线 / 纯图片不翻译 |
|
|
30
|
+
| `src/protect.js` | 行内代码、LaTeX、链接 URL、裸 URL、HTML 标签的占位保护与恢复 |
|
|
31
|
+
| `src/prompt.js` | 批次切分、术语筛选、提示词、容错 JSON 解析 |
|
|
32
|
+
| `src/translator.js` | 缓存优先、批量请求(默认 2 批并行)、单段回退、标题前缀修复、进度统计 |
|
|
33
|
+
| `src/store.js` | 文档目录、任务状态、恢复中断、导出、术语表与缓存实例 |
|
|
34
|
+
| `src/routes.js` | 回环限定的 HTTP API 与静态页面 |
|
|
35
|
+
| `src/client.js` | DSH 侧栏入口 + iframe 主面板 |
|
|
36
|
+
|
|
37
|
+
## 存储
|
|
38
|
+
|
|
39
|
+
`~/.dsh-bilingual-reader/`(可用 `DSH_BILINGUAL_READER_DIR` 覆盖):
|
|
40
|
+
|
|
41
|
+
- `docs/<id>/article.md`:抓取后的原始 Markdown
|
|
42
|
+
- `docs/<id>/doc.json`:块、译文、状态、统计、provenance
|
|
43
|
+
- `glossary.json`:`{ source, target, note }`
|
|
44
|
+
- `cache.json`:`sha256(目标语言 + 术语表哈希 + 原文) -> 译文`
|
|
45
|
+
|
|
46
|
+
缓存只存译文,不存原文;删除文档不会删除缓存。`doc.json` 是唯一事实来源,`bilingual.md` 在导出时生成。
|
|
47
|
+
|
|
48
|
+
## 一致性与失败处理
|
|
49
|
+
|
|
50
|
+
- 翻译任务在内存中运行;进程重启后 `recover()` 把 `running` 标记为 `interrupted`,用户可以重试。
|
|
51
|
+
- 一个批次失败会拆成单段重试;单段仍失败只标记该段,不影响其他段落。
|
|
52
|
+
- 模型丢失保护 token 时先重试单段;仍丢失则保留译文并给该段加 `warning`,不会静默删掉链接或代码。
|
|
53
|
+
- 所有写文件使用唯一临时文件 + rename,避免半截 JSON;同一 store 的文档写入串行化,并行批次不会用旧快照覆盖新快照。
|
|
54
|
+
|
|
55
|
+
## 安全边界
|
|
56
|
+
|
|
57
|
+
- 只接受 http/https;解析 DNS 后拒绝私网、回环、链路本地、CGNAT、组播、云 metadata;每次重定向重新校验;自定义 lookup 关闭 DNS rebinding 窗口。
|
|
58
|
+
- 页面 HTML 只在服务端经过 Readability/Turndown;前端渲染的 HTML 由 `sanitize-html` 白名单过滤。
|
|
59
|
+
- HTTP 路由只允许回环请求,并校验 Origin / Sec-Fetch-Site。
|
|
60
|
+
- 不发送遥测;不引入独立 API key,模型调用走 DSH 默认模型。
|
|
61
|
+
|
|
62
|
+
## 测试
|
|
63
|
+
|
|
64
|
+
- `npm test`:切分、保护、提示词解析、缓存、术语表、抓取安全、正文抽取、store/translator 集成。
|
|
65
|
+
- `npm run verify`:本地 HTTP fixture → 抓取 → 翻译 → 导出 → 缓存,端到端检查。
|
|
66
|
+
- CI:Node 22/24 矩阵,`npm ci`、语法检查、构建、测试、verify、`npm pack --dry-run`。
|
package/docs/demo.jpg
ADDED
|
Binary file
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# dsh-bilingual-reader v0.1.0 / 首个版本
|
|
2
|
+
|
|
3
|
+
面向 DeepSeek Harness 的双语阅读工作台:把网页或 Markdown 文档读成“原文 + 译文”的结构化双语页面,代码、公式、链接和表格保持原样。
|
|
4
|
+
|
|
5
|
+
A bilingual reading workbench for DeepSeek Harness: read web articles or Markdown documents with the translation beside each block while code, formulas, links and tables stay intact.
|
|
6
|
+
|
|
7
|
+
- GitHub: <https://github.com/bit0620/dsh-bilingual-reader>
|
|
8
|
+
- npm: <https://www.npmjs.com/package/dsh-bilingual-reader>
|
|
9
|
+
- License: MIT
|
|
10
|
+
|
|
11
|
+
## 亮点 / Highlights
|
|
12
|
+
|
|
13
|
+
- **网页双语**:URL → Readability 正文 → 段落翻译 → 对照 / 原文 / 译文三种视图。
|
|
14
|
+
- **保结构**:代码块不翻译;行内代码、LaTeX、链接 URL、HTML 标签用占位符保护,丢失会重试。
|
|
15
|
+
- **省 token**:默认 6 段 / 3500 字符合并请求;按“原文 + 目标语言 + 术语表哈希”缓存。
|
|
16
|
+
- **术语表**:本地 `glossary.json`,翻译时强制统一译法,Agent 可增删查。
|
|
17
|
+
- **双语导出**:保留标题层级、列表、表格和代码块。
|
|
18
|
+
- **Agent 工具**:`bilingual_read_url`、`bilingual_translate`、`bilingual_status`、`bilingual_glossary`。
|
|
19
|
+
- **本地优先**:数据在 `~/.dsh-bilingual-reader/`,无遥测,无独立 API key,使用 DSH 默认模型。
|
|
20
|
+
|
|
21
|
+
## 安装 / Install
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
dsh plugin --profile web add dsh-bilingual-reader
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
或从 GitHub:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
dsh plugin --profile web add github:Bit0620/dsh-bilingual-reader
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
重启 DSH,从侧栏打开“双语阅读”。
|
|
34
|
+
|
|
35
|
+
## 验证 / Verification
|
|
36
|
+
|
|
37
|
+
- 34 项单测:切分、保护、提示词解析、缓存、术语表、SSRF、正文抽取、store/translator 集成。
|
|
38
|
+
- `npm run verify`:本地 HTTP fixture → 抓取 → 翻译 → 导出 → 缓存,端到端通过。
|
|
39
|
+
- GitHub Actions Node 22/24 矩阵、`npm pack --dry-run` 全部通过。
|
|
40
|
+
- 独立 `DSH_HOME` + `dsh plugin add` 安装后,`--dump-config` 能组合出插件条目。
|
|
41
|
+
|
|
42
|
+
## 限制 / Limitations
|
|
43
|
+
|
|
44
|
+
- 只支持公开页面;登录墙、SPA、反爬页可能提取失败。
|
|
45
|
+
- 第一版不做 PDF / EPUB / DOCX 版面翻译、浏览器扩展、对话回复翻译。
|
|
46
|
+
- 翻译质量取决于 DSH 默认模型;批量解析失败会降级到单段。
|
|
47
|
+
- 缓存按段落跨文档共享;修改术语表会产生新的缓存键。
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# dsh-bilingual-reader v0.1.1 / 阅读体验与翻译速度
|
|
2
|
+
|
|
3
|
+
v0.1.1 把默认阅读方式改成“上下对照”:译文紧跟在每一段原文下面,和 README 里承诺的沉浸式阅读一致;同时让翻译并行运行、进度逐段呈现。
|
|
4
|
+
|
|
5
|
+
v0.1.1 makes inline bilingual reading the default — the translation sits directly under each paragraph — and speeds up translation with bounded concurrency and incremental progress rendering.
|
|
6
|
+
|
|
7
|
+
- GitHub: <https://github.com/bit0620/dsh-bilingual-reader>
|
|
8
|
+
- npm: <https://www.npmjs.com/package/dsh-bilingual-reader>
|
|
9
|
+
- 版本:`0.1.1`
|
|
10
|
+
- 升级:`dsh plugin --profile web add dsh-bilingual-reader` 或重新安装 GitHub 源
|
|
11
|
+
|
|
12
|
+
## 变化 / Changes
|
|
13
|
+
|
|
14
|
+
- **上下对照成为默认视图**:原文在上、译文紧随其下;新增“上下 / 对照 / 原文 / 译文”四个视图,选择会记在浏览器里。
|
|
15
|
+
- **边译边读**:轮询只替换发生变化的那几个段落,不再整页重绘;滚动位置和文本选择不会被打断。
|
|
16
|
+
- **并行翻译**:默认同时跑 2 个批次(`DSH_BILINGUAL_CONCURRENCY` 可调,范围 1–4),长文完成得更快;进度仍按批次保存。
|
|
17
|
+
- **缓存一致性**:一轮翻译固定使用开始时的术语表哈希,运行中修改术语表不会把结果写到错误的缓存键;保护片段重试成功后会清掉旧警告。
|
|
18
|
+
- **并发写入更稳**:文档写入串行化,临时文件名加入随机后缀,避免并行批次在 Windows 上互相覆盖。
|
|
19
|
+
|
|
20
|
+
## 验证 / Verification
|
|
21
|
+
|
|
22
|
+
- 36 项单测:新增批量并发上限、运行中术语表哈希一致性两项;原有切分、保护、提示词、缓存、术语表、SSRF、抽取、store/translator 测试全部通过。
|
|
23
|
+
- `npm run verify`:本地 HTTP fixture → 抓取 → 翻译 → 导出 → 缓存,端到端通过。
|
|
24
|
+
- 本地演示 UI:默认 `上下` 视图;翻译过程中检查了 DOM 节点,未变化的代码块保持同一个节点,只有译文段落被替换。
|
|
25
|
+
- GitHub Actions Node 22/24 矩阵与 `npm pack --dry-run` 通过。
|
|
26
|
+
|
|
27
|
+
## 兼容性 / Compatibility
|
|
28
|
+
|
|
29
|
+
- 环境变量新增 `DSH_BILINGUAL_CONCURRENCY`(默认 `2`)。
|
|
30
|
+
- 文档格式和缓存格式不变;0.1.0 的文档可以直接打开。
|
|
31
|
+
- 仅支持 DSH `>=0.2.0 <0.3.0`、Node.js `>=22.12`。
|
package/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url'
|
|
2
|
+
import { loadConfig, normalizeTargetLang, targetLanguageName } from './src/config.js'
|
|
3
|
+
import { createRoutes } from './src/routes.js'
|
|
4
|
+
import { BilingualStore } from './src/store.js'
|
|
5
|
+
import { registerTools } from './src/tools.js'
|
|
6
|
+
import { createCompletion } from './src/translator.js'
|
|
7
|
+
|
|
8
|
+
export const name = 'dsh-bilingual-reader'
|
|
9
|
+
export const inject = ['webServer', 'tools', 'commands', 'systemPrompt']
|
|
10
|
+
|
|
11
|
+
const webDir = fileURLToPath(new URL('./web/', import.meta.url))
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Mount the bilingual reader: storage under ~/.dsh-bilingual-reader, a web
|
|
15
|
+
* panel on the host webserver, four bilingual_* tools, a /bilingual command and
|
|
16
|
+
* one system prompt section so the model knows when to read, translate and
|
|
17
|
+
* manage the glossary.
|
|
18
|
+
*/
|
|
19
|
+
export function apply(ctx) {
|
|
20
|
+
const config = loadConfig()
|
|
21
|
+
const store = new BilingualStore(config, { complete: createCompletion(ctx) })
|
|
22
|
+
|
|
23
|
+
void store.ready().catch(error => {
|
|
24
|
+
console.error(`[dsh-bilingual-reader] 初始化失败:${error instanceof Error ? error.message : String(error)}`)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
registerTools(ctx, { store })
|
|
28
|
+
|
|
29
|
+
ctx.effect(() => {
|
|
30
|
+
const routes = createRoutes({ store, webDir })
|
|
31
|
+
const disposers = routes.map(route => ctx.webServer.register(route))
|
|
32
|
+
return () => {
|
|
33
|
+
for (const dispose of disposers) dispose()
|
|
34
|
+
}
|
|
35
|
+
}, 'dsh-bilingual-reader: web routes')
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
ctx.effect(() => ctx.commands.register({
|
|
39
|
+
name: 'bilingual',
|
|
40
|
+
description: '把网页读成双语对照(也可从侧栏打开“双语阅读”)',
|
|
41
|
+
input: { hint: '<url> [zh-CN|zh-TW|en|ja|ko]' },
|
|
42
|
+
async handler({ rawInput }) {
|
|
43
|
+
const input = String(rawInput ?? '').trim()
|
|
44
|
+
if (input === '') {
|
|
45
|
+
return { kind: 'error', text: '用法:/bilingual <url> [目标语言];也可以从侧栏打开“双语阅读”。' }
|
|
46
|
+
}
|
|
47
|
+
const [url, lang] = input.split(/\s+/)
|
|
48
|
+
const targetLang = normalizeTargetLang(lang, config.targetLang)
|
|
49
|
+
try {
|
|
50
|
+
const doc = await store.createDocFromUrl(url, { targetLang })
|
|
51
|
+
await store.startJob(doc)
|
|
52
|
+
return {
|
|
53
|
+
kind: 'success',
|
|
54
|
+
text: `已开始翻译《${doc.title}》(${targetLanguageName(doc.targetLang)})。从侧栏打开“双语阅读”查看进度,或稍后用 bilingual_status 查询(id: ${doc.id})。`,
|
|
55
|
+
}
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return { kind: 'error', text: `读取失败:${error instanceof Error ? error.message : String(error)}` }
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
}), 'dsh-bilingual-reader: /bilingual command')
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.warn(`[dsh-bilingual-reader] /bilingual command unavailable: ${error instanceof Error ? error.message : String(error)}`)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
ctx.effect(() => ctx.systemPrompt.section({
|
|
67
|
+
name: 'plugin:bilingual-reader',
|
|
68
|
+
order: 150,
|
|
69
|
+
text: [
|
|
70
|
+
'本机已安装双语阅读工作台(dsh-bilingual-reader)。',
|
|
71
|
+
'当用户说“翻译这个网页 / 双语阅读这个链接 / 读懂这篇文章”时,调用 bilingual_read_url(需要 http/https 公开链接)。',
|
|
72
|
+
'当用户粘贴一段文字要求翻译时,调用 bilingual_translate;需要对照时传 bilingual=true。',
|
|
73
|
+
'长文章的翻译在后台进行;bilingual_read_url 返回“翻译仍在后台进行”时,用 bilingual_status 查询 id,必要时 includeTranslations=true 取回已完成段落。',
|
|
74
|
+
'用户要求统一术语(例如 token 译作“令牌”)时,调用 bilingual_glossary 添加术语;翻译时会强制使用。',
|
|
75
|
+
'侧栏“双语阅读”面板提供对照 / 原文 / 译文三种视图和双语 Markdown 导出。',
|
|
76
|
+
'不要编造翻译结果;如果模型不可用或抓取失败,明确说明原因。',
|
|
77
|
+
].join(''),
|
|
78
|
+
}), 'dsh-bilingual-reader: system prompt')
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.warn(`[dsh-bilingual-reader] system prompt section unavailable: ${error instanceof Error ? error.message : String(error)}`)
|
|
81
|
+
}
|
|
82
|
+
}
|