@hjd-hitsz/claudex-web 0.0.0-stage → 4.1.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/.env.example +27 -0
- package/CHANGELOG.md +495 -0
- package/LICENSE +674 -0
- package/README.md +106 -2
- package/bin/claudex-web.mjs +129 -0
- package/build-info.json +8 -0
- package/client/dist/_baseMerge.js +1 -0
- package/client/dist/abnfDiagram-WEYOIA3C.js +1 -0
- package/client/dist/apl.js +1 -0
- package/client/dist/app.js +392 -0
- package/client/dist/apple-touch-icon.png +0 -0
- package/client/dist/arc.js +1 -0
- package/client/dist/architectureDiagram-ZHVYKT53.js +36 -0
- package/client/dist/asciiarmor.js +1 -0
- package/client/dist/asn1.js +1 -0
- package/client/dist/asterisk.js +1 -0
- package/client/dist/blockDiagram-RJAE7QA3.js +140 -0
- package/client/dist/brainfuck.js +1 -0
- package/client/dist/c4Diagram-XDPPPE54.js +38 -0
- package/client/dist/channel.js +1 -0
- package/client/dist/chunk-2E7WHVMD.js +88 -0
- package/client/dist/chunk-2Q5K7J3B.js +1 -0
- package/client/dist/chunk-4EMNKR2T.js +1 -0
- package/client/dist/chunk-55XCU76K.js +1 -0
- package/client/dist/chunk-5VM5RSS4.js +15 -0
- package/client/dist/chunk-JWPE2WC7.js +1 -0
- package/client/dist/chunk-VQ5W62QI.js +1 -0
- package/client/dist/chunk-XXDRQBXY.js +1 -0
- package/client/dist/chunk-ZCHMQSOR.js +2 -0
- package/client/dist/classDiagram-v2-4MYWR3UA.js +217 -0
- package/client/dist/clike.js +1 -0
- package/client/dist/clojure.js +1 -0
- package/client/dist/cmake.js +1 -0
- package/client/dist/cobol.js +1 -0
- package/client/dist/code-highlight-style.js +13 -0
- package/client/dist/coffeescript.js +1 -0
- package/client/dist/commonlisp.js +1 -0
- package/client/dist/cose-bilkent-JH36ORCC.js +1 -0
- package/client/dist/crystal.js +1 -0
- package/client/dist/css.js +1 -0
- package/client/dist/cynefin-EF2NZ3EQ.js +141 -0
- package/client/dist/cynefinDiagram-WF24UDRP.js +62 -0
- package/client/dist/cypher.js +1 -0
- package/client/dist/cytoscape.esm.js +321 -0
- package/client/dist/d.js +1 -0
- package/client/dist/dagre-NK4GIZVX.js +4 -0
- package/client/dist/defaultLocale.js +1 -0
- package/client/dist/diagram-CBK3MWTC.js +24 -0
- package/client/dist/diagram-I5XIIV2D.js +24 -0
- package/client/dist/diagram-ODIL2UCW.js +30 -0
- package/client/dist/diagram-PFBEJUXE.js +3 -0
- package/client/dist/diagram-RJA3I4VV.js +41 -0
- package/client/dist/diagram-TYUNFSNR.js +200 -0
- package/client/dist/diff.js +1 -0
- package/client/dist/dockerfile.js +1 -0
- package/client/dist/document-editor.js +1040 -0
- package/client/dist/dtd.js +1 -0
- package/client/dist/dylan.js +1 -0
- package/client/dist/ebnf.js +1 -0
- package/client/dist/ebnfDiagram-V4P3TTPT.js +1 -0
- package/client/dist/ecl.js +1 -0
- package/client/dist/eiffel.js +1 -0
- package/client/dist/elk-QONTBCTQ.js +27 -0
- package/client/dist/elm.js +1 -0
- package/client/dist/erDiagram-KYQIGQAA.js +99 -0
- package/client/dist/erlang.js +1 -0
- package/client/dist/factor.js +1 -0
- package/client/dist/favicon.ico +0 -0
- package/client/dist/favicon.png +0 -0
- package/client/dist/fcl.js +1 -0
- package/client/dist/file-panel.js +1 -0
- package/client/dist/flowDiagram-IUGLLWBU.js +213 -0
- package/client/dist/forth.js +1 -0
- package/client/dist/fortran.js +1 -0
- package/client/dist/ganttDiagram-R7GPNTJY.js +292 -0
- package/client/dist/gas.js +1 -0
- package/client/dist/gherkin.js +1 -0
- package/client/dist/gitGraphDiagram-PDYL7XHY.js +106 -0
- package/client/dist/graph.js +1 -0
- package/client/dist/groovy.js +1 -0
- package/client/dist/haskell.js +1 -0
- package/client/dist/haxe.js +1 -0
- package/client/dist/http.js +1 -0
- package/client/dist/idl.js +1 -0
- package/client/dist/index.html +25 -0
- package/client/dist/index.js +2 -0
- package/client/dist/index10.js +1 -0
- package/client/dist/index11.js +1 -0
- package/client/dist/index12.js +1 -0
- package/client/dist/index13.js +7 -0
- package/client/dist/index14.js +1 -0
- package/client/dist/index15.js +1 -0
- package/client/dist/index16.js +6 -0
- package/client/dist/index17.js +1 -0
- package/client/dist/index18.js +3 -0
- package/client/dist/index19.js +1 -0
- package/client/dist/index2.js +1 -0
- package/client/dist/index20.js +1 -0
- package/client/dist/index21.js +1 -0
- package/client/dist/index22.js +1 -0
- package/client/dist/index3.js +1 -0
- package/client/dist/index4.js +1 -0
- package/client/dist/index5.js +1 -0
- package/client/dist/index6.js +13 -0
- package/client/dist/index7.js +1 -0
- package/client/dist/index8.js +1 -0
- package/client/dist/index9.js +1 -0
- package/client/dist/infoDiagram-MVDRKAX5.js +2 -0
- package/client/dist/init.js +1 -0
- package/client/dist/isArrayLikeObject.js +1 -0
- package/client/dist/ishikawaDiagram-HPEHV2GE.js +70 -0
- package/client/dist/javascript.js +1 -0
- package/client/dist/journeyDiagram-BOSHW7O7.js +139 -0
- package/client/dist/julia.js +1 -0
- package/client/dist/kanban-definition-FNSA4HC5.js +89 -0
- package/client/dist/katex.js +257 -0
- package/client/dist/linear.js +1 -0
- package/client/dist/livescript.js +1 -0
- package/client/dist/lua.js +1 -0
- package/client/dist/mathematica.js +1 -0
- package/client/dist/mbox.js +1 -0
- package/client/dist/mermaid.core.js +310 -0
- package/client/dist/mindmap-definition-6OY57UG3.js +98 -0
- package/client/dist/mirc.js +1 -0
- package/client/dist/mllike.js +1 -0
- package/client/dist/modelica.js +1 -0
- package/client/dist/mscgen.js +1 -0
- package/client/dist/mumps.js +1 -0
- package/client/dist/nginx.js +1 -0
- package/client/dist/notification-worker.js +45 -0
- package/client/dist/nsis.js +1 -0
- package/client/dist/ntriples.js +1 -0
- package/client/dist/octave.js +1 -0
- package/client/dist/ordinal.js +1 -0
- package/client/dist/oz.js +1 -0
- package/client/dist/pascal.js +1 -0
- package/client/dist/pegDiagram-ECIUJNBX.js +1 -0
- package/client/dist/perl.js +1 -0
- package/client/dist/pieDiagram-GNMPY74T.js +39 -0
- package/client/dist/pig.js +1 -0
- package/client/dist/powershell.js +1 -0
- package/client/dist/properties.js +1 -0
- package/client/dist/protobuf.js +1 -0
- package/client/dist/pug.js +1 -0
- package/client/dist/puppet.js +1 -0
- package/client/dist/purify.es.js +3 -0
- package/client/dist/python.js +1 -0
- package/client/dist/q.js +1 -0
- package/client/dist/quadrantDiagram-D57K4MC3.js +7 -0
- package/client/dist/r.js +1 -0
- package/client/dist/railroadDiagram-Y72NRBL7.js +1 -0
- package/client/dist/requirementDiagram-EQYSW5RA.js +84 -0
- package/client/dist/rpm.js +1 -0
- package/client/dist/ruby.js +1 -0
- package/client/dist/sankeyDiagram-SY24JHO5.js +40 -0
- package/client/dist/sas.js +1 -0
- package/client/dist/scheme.js +1 -0
- package/client/dist/sequenceDiagram-BLEHNHQU.js +169 -0
- package/client/dist/shell.js +1 -0
- package/client/dist/sieve.js +1 -0
- package/client/dist/simple-mode.js +1 -0
- package/client/dist/sizeCapture-INFHLROL.js +1 -0
- package/client/dist/smalltalk.js +1 -0
- package/client/dist/solr.js +1 -0
- package/client/dist/sparql.js +1 -0
- package/client/dist/spreadsheet.js +1 -0
- package/client/dist/sql.js +1 -0
- package/client/dist/stateDiagram-v2-A6IESKWZ.js +291 -0
- package/client/dist/stex.js +1 -0
- package/client/dist/style.css +1 -0
- package/client/dist/stylus.js +1 -0
- package/client/dist/swift.js +1 -0
- package/client/dist/swimlanes-B3ENRKFQ.js +1 -0
- package/client/dist/swimlanesDiagram-AYQRV4AK.js +8 -0
- package/client/dist/tcl.js +1 -0
- package/client/dist/terminal-panel.js +18 -0
- package/client/dist/textile.js +1 -0
- package/client/dist/tiddlywiki.js +1 -0
- package/client/dist/tiki.js +1 -0
- package/client/dist/timeline-definition-JXYKGLGB.js +120 -0
- package/client/dist/toml.js +1 -0
- package/client/dist/troff.js +1 -0
- package/client/dist/ttcn-cfg.js +1 -0
- package/client/dist/ttcn.js +1 -0
- package/client/dist/turtle.js +1 -0
- package/client/dist/usecaseDiagram-WPOLBNNX.js +370 -0
- package/client/dist/vb.js +1 -0
- package/client/dist/vbscript.js +1 -0
- package/client/dist/velocity.js +1 -0
- package/client/dist/vennDiagram-Q3Y3IDCW.js +34 -0
- package/client/dist/verilog.js +1 -0
- package/client/dist/vhdl.js +1 -0
- package/client/dist/wardleyDiagram-IVAIDKWA.js +78 -0
- package/client/dist/webidl.js +1 -0
- package/client/dist/xquery.js +1 -0
- package/client/dist/xychartDiagram-4URNHDU4.js +7 -0
- package/client/dist/yacas.js +1 -0
- package/client/dist/z80.js +1 -0
- package/docs/architecture/backend-regressions.md +53 -0
- package/docs/architecture/backends.md +115 -0
- package/docs/architecture/behavior.md +228 -0
- package/docs/architecture.md +335 -0
- package/docs/background-tasks.md +39 -0
- package/docs/cli-command-coverage.md +60 -0
- package/docs/deployment.md +289 -0
- package/docs/development.md +396 -0
- package/docs/images/desktop-dark.png +0 -0
- package/docs/images/desktop-light.png +0 -0
- package/docs/images/mobile-sidebar.png +0 -0
- package/docs/images/mobile.png +0 -0
- package/docs/releases/v2.0.0.md +5 -0
- package/docs/releases/v2.1.0.md +6 -0
- package/docs/releases/v2.1.1.md +6 -0
- package/docs/releases/v2.1.2.md +8 -0
- package/docs/releases/v2.2.0.md +11 -0
- package/docs/releases/v2.3.0.md +12 -0
- package/docs/releases/v2.3.1.md +8 -0
- package/docs/releases/v2.4.0.md +8 -0
- package/docs/releases/v2.4.1.md +8 -0
- package/docs/releases/v2.4.2.md +8 -0
- package/docs/releases/v3.0.0.md +9 -0
- package/docs/releases/v3.1.0.md +10 -0
- package/docs/releases/v3.1.1.md +7 -0
- package/docs/releases/v4.0.0-beta.1.md +10 -0
- package/docs/releases/v4.0.0.md +20 -0
- package/docs/releases/v4.0.1.md +10 -0
- package/docs/releases/v4.1.0.md +21 -0
- package/docs/versioning.md +129 -0
- package/package.json +87 -4
- package/packages/translation-engine/LICENSE +674 -0
- package/packages/translation-engine/README.md +69 -0
- package/packages/translation-engine/bin/tokenizer-linux-x64 +0 -0
- package/packages/translation-engine/bin/translate.mjs +29 -0
- package/packages/translation-engine/native/build.mjs +15 -0
- package/packages/translation-engine/native/tokenizer.c +73 -0
- package/packages/translation-engine/package.json +29 -0
- package/packages/translation-engine/src/catalog.mjs +103 -0
- package/packages/translation-engine/src/cpu-runtime.mjs +90 -0
- package/packages/translation-engine/src/download.mjs +94 -0
- package/packages/translation-engine/src/index.mjs +70 -0
- package/packages/translation-engine/src/inference.mjs +177 -0
- package/packages/translation-engine/src/model-store.mjs +197 -0
- package/packages/translation-engine/src/tokenizer.mjs +77 -0
- package/packages/translation-engine/src/worker.mjs +83 -0
- package/scripts/install-services.mjs +135 -0
- package/scripts/restart-when-idle.mjs +125 -0
- package/scripts/start-backend.mjs +29 -0
- package/server/src/agent-messages.mjs +124 -0
- package/server/src/application.mjs +168 -0
- package/server/src/auth.mjs +328 -0
- package/server/src/backends/claude/account.mjs +171 -0
- package/server/src/backends/claude/adapter.mjs +366 -0
- package/server/src/backends/claude/agent-history.mjs +70 -0
- package/server/src/backends/claude/background-tasks.mjs +168 -0
- package/server/src/backends/claude/catalogue.mjs +268 -0
- package/server/src/backends/claude/execution-errors.mjs +44 -0
- package/server/src/backends/claude/execution-session.mjs +396 -0
- package/server/src/backends/claude/git-tool-changes.mjs +77 -0
- package/server/src/backends/claude/goals.mjs +319 -0
- package/server/src/backends/claude/history-actions.mjs +170 -0
- package/server/src/backends/claude/history.mjs +65 -0
- package/server/src/backends/claude/hooks.mjs +232 -0
- package/server/src/backends/claude/input-stream.mjs +24 -0
- package/server/src/backends/claude/messages-request.mjs +34 -0
- package/server/src/backends/claude/messages.mjs +301 -0
- package/server/src/backends/claude/model-options.mjs +18 -0
- package/server/src/backends/claude/native-commands.mjs +48 -0
- package/server/src/backends/claude/native-records.mjs +76 -0
- package/server/src/backends/claude/process-ownership.mjs +82 -0
- package/server/src/backends/claude/project-instructions.mjs +40 -0
- package/server/src/backends/claude/provider-balance.mjs +40 -0
- package/server/src/backends/claude/provider-environment.mjs +95 -0
- package/server/src/backends/claude/provider-transport.mjs +70 -0
- package/server/src/backends/claude/recovery.mjs +54 -0
- package/server/src/backends/claude/runtime.mjs +463 -0
- package/server/src/backends/claude/session-worker.mjs +32 -0
- package/server/src/backends/claude/sessions.mjs +348 -0
- package/server/src/backends/claude/token-usage.mjs +98 -0
- package/server/src/backends/claude/worker-client.mjs +351 -0
- package/server/src/backends/claude/worker-launcher.mjs +119 -0
- package/server/src/backends/claude/worker-location.mjs +43 -0
- package/server/src/backends/claude/worker-revision.mjs +45 -0
- package/server/src/backends/claude/worker-server.mjs +290 -0
- package/server/src/backends/cli-installation.mjs +140 -0
- package/server/src/backends/codex/adapter.mjs +155 -0
- package/server/src/backends/codex/compaction-recovery.mjs +78 -0
- package/server/src/backends/codex/events.mjs +125 -0
- package/server/src/backends/codex/methods.mjs +17 -0
- package/server/src/backends/codex/reference-mcp.mjs +97 -0
- package/server/src/backends/codex/reference-tools.mjs +71 -0
- package/server/src/backends/codex/requests.mjs +170 -0
- package/server/src/backends/codex/subscriptions.mjs +91 -0
- package/server/src/backends/codex/token-usage.mjs +140 -0
- package/server/src/backends/codex/upstream.mjs +148 -0
- package/server/src/backends/configuration.mjs +122 -0
- package/server/src/backends/identity-mapping.mjs +87 -0
- package/server/src/backends/manager.mjs +478 -0
- package/server/src/backends/network-proxy.mjs +55 -0
- package/server/src/backends/provider-presets.mjs +45 -0
- package/server/src/backends/providers.mjs +135 -0
- package/server/src/backends/storage.mjs +39 -0
- package/server/src/bridge/conversation-policy.mjs +29 -0
- package/server/src/bridge/file-operations.mjs +47 -0
- package/server/src/bridge/product-methods.mjs +66 -0
- package/server/src/compaction.mjs +82 -0
- package/server/src/config.mjs +64 -0
- package/server/src/file-content.mjs +99 -0
- package/server/src/file-undo.mjs +142 -0
- package/server/src/http/maintenance.mjs +28 -0
- package/server/src/http/router.mjs +98 -0
- package/server/src/http/static.mjs +62 -0
- package/server/src/product-config.mjs +80 -0
- package/server/src/project-models.mjs +59 -0
- package/server/src/rpc-transport.mjs +21 -0
- package/server/src/server.mjs +29 -0
- package/server/src/service-maintenance-worker.mjs +124 -0
- package/server/src/service-maintenance.mjs +222 -0
- package/server/src/summary-translation.mjs +71 -0
- package/server/src/system-auth.mjs +45 -0
- package/server/src/thread-folders.mjs +76 -0
- package/server/src/thread-names.mjs +264 -0
- package/server/src/thread-projects.mjs +36 -0
- package/server/src/tls.mjs +87 -0
- package/server/src/token-usage-reconciliation.mjs +105 -0
- package/server/src/token-usage.mjs +208 -0
- package/server/src/translation-service.mjs +133 -0
- package/server/src/version.mjs +55 -0
- package/server/src/web-config-migration.mjs +48 -0
- package/server/src/workspace/commands.mjs +33 -0
- package/server/src/workspace/files.mjs +44 -0
- package/server/src/workspace/service.mjs +27 -0
- package/server/src/workspace/terminals.mjs +122 -0
- package/shared/backend-activity.mjs +13 -0
- package/shared/backend-capabilities.mjs +33 -0
- package/shared/backend-identity.mjs +17 -0
- package/shared/compaction-state.mjs +49 -0
- package/shared/file-types.mjs +82 -0
- package/shared/product-contract.mjs +26 -0
- package/shared/rpc-limits.mjs +2 -0
- package/shared/terminal-workspace.mjs +26 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# 本地翻译引擎
|
|
2
|
+
|
|
3
|
+
`@claudex-web/translation-engine` 提供独立的英译中 CPU 推理与模型管理,可供网页服务或命令行程序使用。实现包含 Microsoft Edge 翻译组件的下载、ONNX 图解包、原生分词及自有编码器与解码器推理循环。运行环境为 Linux x64(glibc 2.34+)、Node.js 24.5+、npm 和 tar。
|
|
4
|
+
|
|
5
|
+
## 构建与命令行
|
|
6
|
+
|
|
7
|
+
在仓库根目录安装依赖并构建分词器,需要 C 编译器。
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install
|
|
11
|
+
npm run build --workspace @claudex-web/translation-engine
|
|
12
|
+
node packages/translation-engine/bin/translate.mjs download
|
|
13
|
+
node packages/translation-engine/bin/translate.mjs translate "Read the complete message before translating."
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
独立命令默认将模型保存在当前用户的 `~/.local/share/claudex-translation/models`,可通过 `CLAUDEX_TRANSLATION_MODEL_DIR` 指定其他目录。Claudex Web 固定使用服务账户的 `~/.local/share/claudex-web/models`。
|
|
17
|
+
|
|
18
|
+
## JavaScript 接口
|
|
19
|
+
|
|
20
|
+
调用者明确传入持久数据目录,首次 `prepare()` 下载组件,后续调用校验并复用本地版本。
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import { createTranslationEngine } from "@claudex-web/translation-engine";
|
|
24
|
+
|
|
25
|
+
const engine = createTranslationEngine({
|
|
26
|
+
directory: process.env.MODEL_DIRECTORY,
|
|
27
|
+
onProgress(progress) {
|
|
28
|
+
console.log(progress.stage, progress.received, progress.total);
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
try {
|
|
32
|
+
const model = await engine.prepare();
|
|
33
|
+
console.log(model.id, model.version);
|
|
34
|
+
const translations = await engine.translate([
|
|
35
|
+
"Read the complete message before translating.",
|
|
36
|
+
]);
|
|
37
|
+
console.log(translations);
|
|
38
|
+
} finally {
|
|
39
|
+
await engine.close();
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
输入为已完成的普通文字,输出数组与输入顺序一致。调用方负责 Markdown 解析和展示。较长文字在引擎内部按句子拆分,以满足模型上下文限制。推理使用 ONNX Runtime CPU 单线程,分词通过随包构建的辅助进程调用下载组件中的原生分词库。
|
|
44
|
+
|
|
45
|
+
## 下载与存储
|
|
46
|
+
|
|
47
|
+
首次下载从 Microsoft 组件目录取得模型与分词运行库,并通过 npm 获取固定版本的 ONNX Runtime CPU 文件,合计约 300 MB。HTTPS 目录响应提供版本、文件大小与 SHA-256,组件文件沿用其官方地址。安装前校验完整下载,解包后只保留英译中所需文件,当前安装约 147 MiB。完整模型准备后可离线使用。
|
|
48
|
+
|
|
49
|
+
```text
|
|
50
|
+
模型目录/
|
|
51
|
+
edge-en-zh/
|
|
52
|
+
installed.json
|
|
53
|
+
模型版本-分词版本/
|
|
54
|
+
ready.json
|
|
55
|
+
model/
|
|
56
|
+
runtime/
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
跨进程安装锁合并并发下载。下载中断会清理本次临时目录,进程异常留下的锁通过进程存活与租约检查恢复。版本索引在校验后原子写入,文件损坏或缺失会触发重新安装。`close()` 取消下载并释放推理及分词进程,模型文件可在升级后复用。
|
|
60
|
+
|
|
61
|
+
## 验证与许可证
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
npm test --workspace @claudex-web/translation-engine
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
自动测试使用临时目录和本地组件夹具,覆盖并发安装、完整性校验、取消、损坏恢复及离线复用。真实模型测试可使用命令行与调用方提供的普通文本进行。
|
|
68
|
+
|
|
69
|
+
子项目源码采用 [GNU GPL v3.0](LICENSE),SPDX 标识为 `GPL-3.0-only`。下载的 Microsoft 组件和 npm 依赖分别沿用其提供方的条款。发行包交付引擎源码与分词辅助程序,模型、Microsoft 分词库和 ONNX Runtime 通过首次启用下载。
|
|
Binary file
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
createTranslationEngine,
|
|
4
|
+
defaultModelDirectory,
|
|
5
|
+
} from "../src/index.mjs";
|
|
6
|
+
const directory =
|
|
7
|
+
process.env.CLAUDEX_TRANSLATION_MODEL_DIR || defaultModelDirectory();
|
|
8
|
+
const [command, ...words] = process.argv.slice(2);
|
|
9
|
+
if (
|
|
10
|
+
!["download", "translate"].includes(command) ||
|
|
11
|
+
(command === "translate" && !words.length)
|
|
12
|
+
) {
|
|
13
|
+
console.log(
|
|
14
|
+
"用法:claudex-translate download | translate <英文文本>\n模型目录通过 CLAUDEX_TRANSLATION_MODEL_DIR 设置。",
|
|
15
|
+
);
|
|
16
|
+
process.exitCode = 1;
|
|
17
|
+
} else {
|
|
18
|
+
const engine = createTranslationEngine({
|
|
19
|
+
directory,
|
|
20
|
+
onProgress: (status) => console.error(JSON.stringify(status)),
|
|
21
|
+
});
|
|
22
|
+
try {
|
|
23
|
+
await engine.prepare();
|
|
24
|
+
if (command === "translate")
|
|
25
|
+
console.log((await engine.translate([words.join(" ")]))[0]);
|
|
26
|
+
} finally {
|
|
27
|
+
await engine.close();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
if (process.platform !== "linux" || process.arch !== "x64") {
|
|
4
|
+
console.log("本地翻译引擎的原生分词器构建适用于 Linux x64");
|
|
5
|
+
process.exit(0);
|
|
6
|
+
}
|
|
7
|
+
const source = fileURLToPath(new URL("./tokenizer.c", import.meta.url));
|
|
8
|
+
const output = fileURLToPath(
|
|
9
|
+
new URL("../bin/tokenizer-linux-x64", import.meta.url),
|
|
10
|
+
);
|
|
11
|
+
execFileSync(
|
|
12
|
+
process.env.CC || "cc",
|
|
13
|
+
["-O2", "-Wall", "-Wextra", source, "-ldl", "-o", output],
|
|
14
|
+
{ stdio: "inherit" },
|
|
15
|
+
);
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#include <dlfcn.h>
|
|
2
|
+
#include <stdint.h>
|
|
3
|
+
#include <stdbool.h>
|
|
4
|
+
#include <stdio.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
#include <string.h>
|
|
7
|
+
|
|
8
|
+
typedef void OgaTokenizer;
|
|
9
|
+
typedef void OgaSequences;
|
|
10
|
+
typedef void OgaResult;
|
|
11
|
+
static const char *(*result_error)(const OgaResult *);
|
|
12
|
+
static void check(OgaResult *result) {
|
|
13
|
+
if (result) { fprintf(stderr, "%s\n", result_error(result)); exit(1); }
|
|
14
|
+
}
|
|
15
|
+
#define LOAD(name, result, arguments) \
|
|
16
|
+
result (*name) arguments = dlsym(library, #name); \
|
|
17
|
+
if (!name) { fprintf(stderr, "Missing tokenizer symbol: %s\n", #name); return 1; }
|
|
18
|
+
|
|
19
|
+
int main(int argc, char **argv) {
|
|
20
|
+
if (argc != 3) return 1;
|
|
21
|
+
setenv("ORT_DISABLE_TELEMETRY", "1", 1);
|
|
22
|
+
void *library = dlopen(argv[2], RTLD_NOW | RTLD_LOCAL);
|
|
23
|
+
if (!library) { fprintf(stderr, "%s\n", dlerror()); return 1; }
|
|
24
|
+
result_error = dlsym(library, "OgaResultGetError");
|
|
25
|
+
if (!result_error) return 1;
|
|
26
|
+
LOAD(OgaCreateTokenizerFromPath, OgaResult *, (const char *, OgaTokenizer **));
|
|
27
|
+
LOAD(OgaCreateSequences, OgaResult *, (OgaSequences **));
|
|
28
|
+
LOAD(OgaTokenizerEncode, OgaResult *, (const OgaTokenizer *, const char *, OgaSequences *));
|
|
29
|
+
LOAD(OgaSequencesGetSequenceCount, size_t, (const OgaSequences *, size_t));
|
|
30
|
+
LOAD(OgaSequencesGetSequenceData, const int32_t *, (const OgaSequences *, size_t));
|
|
31
|
+
LOAD(OgaTokenizerDecode, OgaResult *, (const OgaTokenizer *, const int32_t *, size_t, const char **));
|
|
32
|
+
LOAD(OgaDestroyString, void, (const char *));
|
|
33
|
+
LOAD(OgaDestroySequences, void, (OgaSequences *));
|
|
34
|
+
LOAD(OgaDestroyTokenizer, void, (OgaTokenizer *));
|
|
35
|
+
LOAD(OgaShutdown, void, (void));
|
|
36
|
+
LOAD(OgaSetTelemetryEnabled, void, (bool));
|
|
37
|
+
OgaSetTelemetryEnabled(false);
|
|
38
|
+
OgaTokenizer *tokenizer;
|
|
39
|
+
check(OgaCreateTokenizerFromPath(argv[1], &tokenizer));
|
|
40
|
+
unsigned char header[5];
|
|
41
|
+
while (fread(header, 1, sizeof(header), stdin) == sizeof(header)) {
|
|
42
|
+
uint32_t length;
|
|
43
|
+
memcpy(&length, header + 1, sizeof(length));
|
|
44
|
+
if (length > 4 * 1024 * 1024) return 1;
|
|
45
|
+
char *input = malloc(length + 1);
|
|
46
|
+
if (!input || fread(input, 1, length, stdin) != length) return 1;
|
|
47
|
+
input[length] = '\0';
|
|
48
|
+
if (header[0] == 'E') {
|
|
49
|
+
OgaSequences *sequences;
|
|
50
|
+
check(OgaCreateSequences(&sequences));
|
|
51
|
+
check(OgaTokenizerEncode(tokenizer, input, sequences));
|
|
52
|
+
size_t count = OgaSequencesGetSequenceCount(sequences, 0);
|
|
53
|
+
const int32_t *ids = OgaSequencesGetSequenceData(sequences, 0);
|
|
54
|
+
uint32_t bytes = count * sizeof(int32_t);
|
|
55
|
+
fwrite(&bytes, 1, sizeof(bytes), stdout);
|
|
56
|
+
fwrite(ids, 1, bytes, stdout);
|
|
57
|
+
OgaDestroySequences(sequences);
|
|
58
|
+
} else if (header[0] == 'D' && length % sizeof(int32_t) == 0) {
|
|
59
|
+
const char *decoded;
|
|
60
|
+
check(OgaTokenizerDecode(tokenizer, (const int32_t *)input, length / sizeof(int32_t), &decoded));
|
|
61
|
+
uint32_t bytes = strlen(decoded);
|
|
62
|
+
fwrite(&bytes, 1, sizeof(bytes), stdout);
|
|
63
|
+
fwrite(decoded, 1, bytes, stdout);
|
|
64
|
+
OgaDestroyString(decoded);
|
|
65
|
+
} else { free(input); return 1; }
|
|
66
|
+
fflush(stdout);
|
|
67
|
+
free(input);
|
|
68
|
+
}
|
|
69
|
+
OgaDestroyTokenizer(tokenizer);
|
|
70
|
+
OgaShutdown();
|
|
71
|
+
dlclose(library);
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@claudex-web/translation-engine",
|
|
3
|
+
"version": "4.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "GPL-3.0-only",
|
|
6
|
+
"description": "独立的本地英译中引擎与按需模型管理",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=24.5"
|
|
9
|
+
},
|
|
10
|
+
"exports": "./src/index.mjs",
|
|
11
|
+
"bin": {
|
|
12
|
+
"claudex-translate": "bin/translate.mjs"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src/",
|
|
16
|
+
"native/",
|
|
17
|
+
"bin/",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "node native/build.mjs",
|
|
23
|
+
"test": "node --test tests/*.test.mjs",
|
|
24
|
+
"prepack": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"fflate": "0.8.2"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { totalmem } from "node:os";
|
|
3
|
+
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
4
|
+
export const modelId = "edge-en-zh";
|
|
5
|
+
export const components = {
|
|
6
|
+
model: "jlkoiilgoccnckeddpanmoamiopnegnn",
|
|
7
|
+
tokenizer: "oiidchjihdkkgdjmohdkalnimpcedjmb",
|
|
8
|
+
};
|
|
9
|
+
export async function resolveComponents(signal, request = fetch) {
|
|
10
|
+
const version = "154.0.4258.48";
|
|
11
|
+
const cpu = await readFile("/proc/cpuinfo", "utf8");
|
|
12
|
+
const flags = cpu.match(/^flags\s*:\s*(.+)$/m)?.[1].split(/\s+/) || [];
|
|
13
|
+
const hw = Object.fromEntries(
|
|
14
|
+
["avx", "sse", "sse2", "sse3", "sse41", "sse42", "ssse3"].map((flag) => [
|
|
15
|
+
flag,
|
|
16
|
+
Number(flags.includes(flag.replace(/(sse)(\d)(\d)/, "$1$2_$3"))),
|
|
17
|
+
]),
|
|
18
|
+
);
|
|
19
|
+
hw.physmemory = Math.round(totalmem() / 1024 ** 3);
|
|
20
|
+
const body = JSON.stringify({
|
|
21
|
+
request: {
|
|
22
|
+
"@os": "linux",
|
|
23
|
+
"@updater": "msedge",
|
|
24
|
+
acceptformat: "crx3,download,run,xz,zucc",
|
|
25
|
+
apps: Object.values(components).map((appid) => ({
|
|
26
|
+
appid,
|
|
27
|
+
lang: "zh-CN",
|
|
28
|
+
installdate: -1,
|
|
29
|
+
ping: { r: -2 },
|
|
30
|
+
enabled: true,
|
|
31
|
+
installsource: "ondemand",
|
|
32
|
+
version: "0.0.0.0",
|
|
33
|
+
updatecheck: {},
|
|
34
|
+
targetingattributes: {
|
|
35
|
+
AppMajorVersion: "154",
|
|
36
|
+
AppVersion: version,
|
|
37
|
+
IsInternalUser: false,
|
|
38
|
+
Priority: true,
|
|
39
|
+
},
|
|
40
|
+
})),
|
|
41
|
+
arch: "x64",
|
|
42
|
+
dedup: "cr",
|
|
43
|
+
hw,
|
|
44
|
+
ismachine: 0,
|
|
45
|
+
os: { arch: "x86_64", platform: "Linux", version: "6.8" },
|
|
46
|
+
prodversion: version,
|
|
47
|
+
protocol: "4.0",
|
|
48
|
+
requestid: `{${randomUUID()}}`,
|
|
49
|
+
sessionid: `{${randomUUID()}}`,
|
|
50
|
+
updaterversion: version,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
const query = new URLSearchParams({
|
|
54
|
+
cup2key: `7:${randomBytes(32).toString("base64url")}`,
|
|
55
|
+
cup2hreq: createHash("sha256").update(body).digest("hex"),
|
|
56
|
+
});
|
|
57
|
+
const response = await request(
|
|
58
|
+
`https://edge.microsoft.com/componentupdater/api/v1/update?${query}`,
|
|
59
|
+
{
|
|
60
|
+
method: "POST",
|
|
61
|
+
body,
|
|
62
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(30000)]),
|
|
63
|
+
headers: {
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
"X-Microsoft-Update-AppId": Object.values(components).join(","),
|
|
66
|
+
"X-Microsoft-Update-Interactivity": "fg",
|
|
67
|
+
"X-Microsoft-Update-Updater": `msedge-${version}`,
|
|
68
|
+
"Sec-Mesh-Client-Edge-Version": version,
|
|
69
|
+
"Sec-Mesh-Client-Edge-Channel": "stable",
|
|
70
|
+
"Sec-Mesh-Client-OS": "Linux",
|
|
71
|
+
"Sec-Mesh-Client-Arch": "x86_64",
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
if (!response.ok) throw new Error(`模型目录读取失败 (${response.status})`);
|
|
76
|
+
const data = JSON.parse((await response.text()).replace(/^\)\]\}'\s*/, ""));
|
|
77
|
+
return Object.fromEntries(
|
|
78
|
+
Object.entries(components).map(([kind, id]) => {
|
|
79
|
+
const update = data.response.apps.find(
|
|
80
|
+
(app) => app.appid === id,
|
|
81
|
+
)?.updatecheck;
|
|
82
|
+
const operation = update?.pipelines
|
|
83
|
+
?.flatMap((p) => p.operations)
|
|
84
|
+
.find((op) => op.type === "download");
|
|
85
|
+
if (!operation || !/^\d+(\.\d+){3}$/.test(update.nextversion))
|
|
86
|
+
throw new Error("模型下载目录未提供有效版本,请稍后重试");
|
|
87
|
+
const url = new URL(operation.urls[0].url);
|
|
88
|
+
if (!url.hostname.endsWith(".microsoft.com"))
|
|
89
|
+
throw new Error("模型下载来源无效");
|
|
90
|
+
if (!["http:", "https:"].includes(url.protocol))
|
|
91
|
+
throw new Error("模型下载协议无效");
|
|
92
|
+
return [
|
|
93
|
+
kind,
|
|
94
|
+
{
|
|
95
|
+
version: update.nextversion,
|
|
96
|
+
url: url.href,
|
|
97
|
+
size: operation.size,
|
|
98
|
+
sha256: operation.out.sha256,
|
|
99
|
+
},
|
|
100
|
+
];
|
|
101
|
+
}),
|
|
102
|
+
);
|
|
103
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import {
|
|
4
|
+
mkdir,
|
|
5
|
+
mkdtemp,
|
|
6
|
+
readFile,
|
|
7
|
+
writeFile,
|
|
8
|
+
link,
|
|
9
|
+
rm,
|
|
10
|
+
} from "node:fs/promises";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { createHash } from "node:crypto";
|
|
13
|
+
const execute = promisify(execFile);
|
|
14
|
+
export const runtimeVersion = "1.30.0";
|
|
15
|
+
|
|
16
|
+
export async function installCpuRuntime(directory, signal) {
|
|
17
|
+
const temporary = await mkdtemp(join(directory, ".cpu-runtime-"));
|
|
18
|
+
const hashes = {};
|
|
19
|
+
try {
|
|
20
|
+
for (const name of ["onnxruntime-node", "onnxruntime-common"]) {
|
|
21
|
+
const { stdout } = await execute(
|
|
22
|
+
"npm",
|
|
23
|
+
[
|
|
24
|
+
"pack",
|
|
25
|
+
`${name}@${runtimeVersion}`,
|
|
26
|
+
"--ignore-scripts",
|
|
27
|
+
"--prefer-offline",
|
|
28
|
+
"--json",
|
|
29
|
+
"--pack-destination",
|
|
30
|
+
temporary,
|
|
31
|
+
],
|
|
32
|
+
{ signal, maxBuffer: 1024 * 1024 },
|
|
33
|
+
);
|
|
34
|
+
const [pack] = JSON.parse(stdout);
|
|
35
|
+
const archive = join(temporary, pack.filename);
|
|
36
|
+
const files = pack.files
|
|
37
|
+
.map((file) => file.path)
|
|
38
|
+
.filter(
|
|
39
|
+
(file) =>
|
|
40
|
+
file === "package.json" ||
|
|
41
|
+
/(^|\/)(LICENSE|ThirdPartyNotices.txt|README.md)$/.test(file) ||
|
|
42
|
+
(name === "onnxruntime-node"
|
|
43
|
+
? /^dist\/.*\.js$/.test(file) ||
|
|
44
|
+
/^bin\/napi-v6\/linux\/x64\/(onnxruntime_binding.node|libonnxruntime.so.1)$/.test(
|
|
45
|
+
file,
|
|
46
|
+
)
|
|
47
|
+
: /^dist\/cjs\/.*\.(js|json)$/.test(file)),
|
|
48
|
+
);
|
|
49
|
+
const target = join(directory, "node_modules", name);
|
|
50
|
+
await mkdir(target, { recursive: true });
|
|
51
|
+
await execute(
|
|
52
|
+
"tar",
|
|
53
|
+
[
|
|
54
|
+
"-xzf",
|
|
55
|
+
archive,
|
|
56
|
+
"--strip-components=1",
|
|
57
|
+
"-C",
|
|
58
|
+
target,
|
|
59
|
+
...files.map((file) => `package/${file}`),
|
|
60
|
+
],
|
|
61
|
+
{ signal },
|
|
62
|
+
);
|
|
63
|
+
for (const file of files)
|
|
64
|
+
hashes[`node_modules/${name}/${file}`] = createHash("sha256")
|
|
65
|
+
.update(await readFile(join(target, file)))
|
|
66
|
+
.digest("hex");
|
|
67
|
+
}
|
|
68
|
+
await writeFile(
|
|
69
|
+
join(directory, "package.json"),
|
|
70
|
+
JSON.stringify({ private: true }),
|
|
71
|
+
{ mode: 0o600 },
|
|
72
|
+
);
|
|
73
|
+
hashes["package.json"] = createHash("sha256")
|
|
74
|
+
.update(await readFile(join(directory, "package.json")))
|
|
75
|
+
.digest("hex");
|
|
76
|
+
await link(
|
|
77
|
+
join(
|
|
78
|
+
directory,
|
|
79
|
+
"node_modules/onnxruntime-node/bin/napi-v6/linux/x64/libonnxruntime.so.1",
|
|
80
|
+
),
|
|
81
|
+
join(directory, "libonnxruntime.so"),
|
|
82
|
+
);
|
|
83
|
+
hashes["libonnxruntime.so"] = createHash("sha256")
|
|
84
|
+
.update(await readFile(join(directory, "libonnxruntime.so")))
|
|
85
|
+
.digest("hex");
|
|
86
|
+
return hashes;
|
|
87
|
+
} finally {
|
|
88
|
+
await rm(temporary, { recursive: true, force: true });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { createHash, createDecipheriv, hkdfSync } from "node:crypto";
|
|
2
|
+
import { open, mkdir, writeFile, readFile, rm } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { unzipSync } from "fflate";
|
|
5
|
+
|
|
6
|
+
export async function downloadArchive(
|
|
7
|
+
component,
|
|
8
|
+
directory,
|
|
9
|
+
signal,
|
|
10
|
+
progress,
|
|
11
|
+
request = fetch,
|
|
12
|
+
) {
|
|
13
|
+
const path = join(directory, "component.download");
|
|
14
|
+
const response = await request(component.url, { signal });
|
|
15
|
+
if (!response.ok) throw new Error(`模型下载失败 (${response.status})`);
|
|
16
|
+
const file = await open(path, "w", 0o600);
|
|
17
|
+
const hash = createHash("sha256");
|
|
18
|
+
let received = 0;
|
|
19
|
+
try {
|
|
20
|
+
for await (const chunk of response.body) {
|
|
21
|
+
signal.throwIfAborted();
|
|
22
|
+
received += chunk.length;
|
|
23
|
+
if (received > component.size)
|
|
24
|
+
throw new Error("模型下载大小超出目录记录");
|
|
25
|
+
hash.update(chunk);
|
|
26
|
+
await file.write(chunk);
|
|
27
|
+
progress(received, component.size);
|
|
28
|
+
}
|
|
29
|
+
} finally {
|
|
30
|
+
await file.close();
|
|
31
|
+
}
|
|
32
|
+
if (received !== component.size || hash.digest("hex") !== component.sha256)
|
|
33
|
+
throw new Error("模型下载校验失败,请重试");
|
|
34
|
+
const archive = await readFile(path);
|
|
35
|
+
if (
|
|
36
|
+
archive.toString("ascii", 0, 4) !== "Cr24" ||
|
|
37
|
+
archive.readUInt32LE(4) !== 3
|
|
38
|
+
)
|
|
39
|
+
throw new Error("模型组件格式无效");
|
|
40
|
+
const start = 12 + archive.readUInt32LE(8);
|
|
41
|
+
return archive.subarray(start);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function extractComponent(zip, directory, kind) {
|
|
45
|
+
const wanted =
|
|
46
|
+
kind === "model"
|
|
47
|
+
? new Set([
|
|
48
|
+
"encoder.onnx",
|
|
49
|
+
"decoder.onnx",
|
|
50
|
+
"encoder.onnx.data",
|
|
51
|
+
"decoder.onnx.data",
|
|
52
|
+
"tokenizer.json",
|
|
53
|
+
"tokenizer_config.json",
|
|
54
|
+
"config.json",
|
|
55
|
+
"genai_config.json",
|
|
56
|
+
])
|
|
57
|
+
: new Set(["libonnxruntime-genai.so"]);
|
|
58
|
+
const entries = unzipSync(zip, {
|
|
59
|
+
filter: (entry) =>
|
|
60
|
+
kind === "model"
|
|
61
|
+
? entry.name.startsWith("en_zh_nmt/") &&
|
|
62
|
+
wanted.has(entry.name.split("/").at(-1))
|
|
63
|
+
: wanted.has(entry.name.split("/").at(-1)),
|
|
64
|
+
});
|
|
65
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
66
|
+
const hashes = {};
|
|
67
|
+
for (const [path, bytes] of Object.entries(entries)) {
|
|
68
|
+
const name = path.split("/").at(-1);
|
|
69
|
+
let output = Buffer.from(bytes);
|
|
70
|
+
if (kind === "model" && ["encoder.onnx", "decoder.onnx"].includes(name)) {
|
|
71
|
+
const derived = Buffer.from(
|
|
72
|
+
hkdfSync(
|
|
73
|
+
"sha256",
|
|
74
|
+
Buffer.from(`en_zh_nmt/${name}`),
|
|
75
|
+
Buffer.alloc(0),
|
|
76
|
+
Buffer.alloc(0),
|
|
77
|
+
48,
|
|
78
|
+
),
|
|
79
|
+
);
|
|
80
|
+
const decipher = createDecipheriv(
|
|
81
|
+
"aes-256-cbc",
|
|
82
|
+
derived.subarray(16),
|
|
83
|
+
derived.subarray(0, 16),
|
|
84
|
+
);
|
|
85
|
+
output = Buffer.concat([decipher.update(output), decipher.final()]);
|
|
86
|
+
}
|
|
87
|
+
await writeFile(join(directory, name), output, { mode: 0o600 });
|
|
88
|
+
hashes[name] = createHash("sha256").update(output).digest("hex");
|
|
89
|
+
}
|
|
90
|
+
if (Object.keys(hashes).length !== wanted.size)
|
|
91
|
+
throw new Error("下载组件缺少翻译模型文件");
|
|
92
|
+
await rm(join(directory, "component.download"), { force: true });
|
|
93
|
+
return hashes;
|
|
94
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { fork } from "node:child_process";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
export { ModelStore, defaultModelDirectory } from "./model-store.mjs";
|
|
4
|
+
export { modelId } from "./catalog.mjs";
|
|
5
|
+
|
|
6
|
+
export function createTranslationEngine({ directory, onProgress = () => {} }) {
|
|
7
|
+
let worker;
|
|
8
|
+
let exited;
|
|
9
|
+
let failure;
|
|
10
|
+
let nextId = 0;
|
|
11
|
+
let closed = false;
|
|
12
|
+
const pending = new Map();
|
|
13
|
+
function fail(error) {
|
|
14
|
+
error.code = "TRANSLATION_PROCESS_EXITED";
|
|
15
|
+
failure = error;
|
|
16
|
+
for (const request of pending.values()) request.reject(error);
|
|
17
|
+
pending.clear();
|
|
18
|
+
}
|
|
19
|
+
function call(action, texts) {
|
|
20
|
+
if (closed || failure)
|
|
21
|
+
return Promise.reject(failure || new Error("翻译引擎已关闭"));
|
|
22
|
+
if (!worker) {
|
|
23
|
+
worker = fork(
|
|
24
|
+
fileURLToPath(new URL("./worker.mjs", import.meta.url)),
|
|
25
|
+
[],
|
|
26
|
+
{
|
|
27
|
+
execArgv: [],
|
|
28
|
+
env: { ...process.env, ORT_DISABLE_TELEMETRY: "1" },
|
|
29
|
+
stdio: ["ignore", "ignore", "pipe", "ipc"],
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
let diagnostic = "";
|
|
33
|
+
worker.stderr.on("data", (bytes) => {
|
|
34
|
+
diagnostic = (diagnostic + bytes).slice(-2000);
|
|
35
|
+
});
|
|
36
|
+
exited = new Promise((resolve) => worker.once("close", resolve));
|
|
37
|
+
worker.on("message", (message) => {
|
|
38
|
+
if (message.progress) {
|
|
39
|
+
onProgress(message.progress);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const request = pending.get(message.id);
|
|
43
|
+
pending.delete(message.id);
|
|
44
|
+
if (message.error) request?.reject(new Error(message.error));
|
|
45
|
+
else request?.resolve(message.result);
|
|
46
|
+
});
|
|
47
|
+
worker.on("error", fail);
|
|
48
|
+
worker.on("exit", () =>
|
|
49
|
+
fail(new Error(diagnostic || "翻译工作进程已结束")),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
const id = ++nextId;
|
|
54
|
+
pending.set(id, { resolve, reject });
|
|
55
|
+
worker.send({ id, action, texts, directory });
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
prepare: () => call("prepare"),
|
|
60
|
+
translate: (texts) => call("translate", texts),
|
|
61
|
+
async close() {
|
|
62
|
+
if (!closed) {
|
|
63
|
+
closed = true;
|
|
64
|
+
if (worker && !failure) worker.send({ action: "close" });
|
|
65
|
+
fail(new Error("翻译已关闭"));
|
|
66
|
+
}
|
|
67
|
+
await exited;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|