@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,177 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { EdgeTokenizer } from "./tokenizer.mjs";
|
|
5
|
+
|
|
6
|
+
export class EdgeTranslation {
|
|
7
|
+
async load(directory, runtime) {
|
|
8
|
+
this.ort = createRequire(join(runtime, "package.json"))("onnxruntime-node");
|
|
9
|
+
this.config = JSON.parse(
|
|
10
|
+
await readFile(`${directory}/config.json`, "utf8"),
|
|
11
|
+
);
|
|
12
|
+
this.generation = JSON.parse(
|
|
13
|
+
await readFile(`${directory}/genai_config.json`, "utf8"),
|
|
14
|
+
);
|
|
15
|
+
this.tokenizer = new EdgeTokenizer(directory, runtime);
|
|
16
|
+
if (this.generation.model.type !== "marian-ssru")
|
|
17
|
+
throw new Error("不支持当前翻译模型结构");
|
|
18
|
+
const options = {
|
|
19
|
+
executionProviders: ["cpu"],
|
|
20
|
+
intraOpNumThreads: 1,
|
|
21
|
+
interOpNumThreads: 1,
|
|
22
|
+
};
|
|
23
|
+
this.encoder = await this.ort.InferenceSession.create(
|
|
24
|
+
`${directory}/encoder.onnx`,
|
|
25
|
+
options,
|
|
26
|
+
);
|
|
27
|
+
this.decoder = await this.ort.InferenceSession.create(
|
|
28
|
+
`${directory}/decoder.onnx`,
|
|
29
|
+
options,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async encode(text) {
|
|
34
|
+
const prefix = this.config.input_prefix.replace("{tgt_upper}", "CHS");
|
|
35
|
+
return [
|
|
36
|
+
...(await this.tokenizer.encode(`${prefix}${text}`)),
|
|
37
|
+
this.config.eos_id,
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async translateSegment(text) {
|
|
42
|
+
const ids = await this.encode(text);
|
|
43
|
+
if (ids.length > this.generation.model.context_length)
|
|
44
|
+
throw new Error("Source exceeds the model context length");
|
|
45
|
+
const input = new this.ort.Tensor(
|
|
46
|
+
"int64",
|
|
47
|
+
BigInt64Array.from(ids, BigInt),
|
|
48
|
+
[1, ids.length],
|
|
49
|
+
);
|
|
50
|
+
const mask = new this.ort.Tensor(
|
|
51
|
+
"int32",
|
|
52
|
+
new Int32Array(ids.length).fill(1),
|
|
53
|
+
[1, ids.length],
|
|
54
|
+
);
|
|
55
|
+
let encoded;
|
|
56
|
+
let state;
|
|
57
|
+
try {
|
|
58
|
+
encoded = await this.encoder.run({
|
|
59
|
+
input_ids: input,
|
|
60
|
+
attention_mask: mask,
|
|
61
|
+
});
|
|
62
|
+
const { encoder_outputs: hidden, ...projections } = encoded;
|
|
63
|
+
state = new this.ort.Tensor(
|
|
64
|
+
"float32",
|
|
65
|
+
new Float32Array(this.config.decoder_layers * this.config.d_model),
|
|
66
|
+
[this.config.decoder_layers, 1, this.config.d_model],
|
|
67
|
+
);
|
|
68
|
+
const tokens = [];
|
|
69
|
+
let previous = this.config.bos_id;
|
|
70
|
+
let ended = false;
|
|
71
|
+
for (
|
|
72
|
+
let position = 0;
|
|
73
|
+
position < this.generation.search.max_length;
|
|
74
|
+
position++
|
|
75
|
+
) {
|
|
76
|
+
const token = new this.ort.Tensor(
|
|
77
|
+
"int64",
|
|
78
|
+
BigInt64Array.of(BigInt(previous)),
|
|
79
|
+
[1],
|
|
80
|
+
);
|
|
81
|
+
const length = new this.ort.Tensor(
|
|
82
|
+
"int64",
|
|
83
|
+
BigInt64Array.of(BigInt(position)),
|
|
84
|
+
[],
|
|
85
|
+
);
|
|
86
|
+
let output;
|
|
87
|
+
try {
|
|
88
|
+
output = await this.decoder.run({
|
|
89
|
+
input_ids: token,
|
|
90
|
+
encoder_hidden_states: hidden,
|
|
91
|
+
encoder_attention_mask: mask,
|
|
92
|
+
rnn_states_prev: state,
|
|
93
|
+
past_key_values_length: length,
|
|
94
|
+
...projections,
|
|
95
|
+
});
|
|
96
|
+
} finally {
|
|
97
|
+
token.dispose();
|
|
98
|
+
length.dispose();
|
|
99
|
+
}
|
|
100
|
+
let selected = 0;
|
|
101
|
+
const logits = output.logits.data;
|
|
102
|
+
for (let id = 1; id < logits.length; id++)
|
|
103
|
+
if (logits[id] > logits[selected]) selected = id;
|
|
104
|
+
state.dispose();
|
|
105
|
+
state = output.rnn_states;
|
|
106
|
+
output.logits.dispose();
|
|
107
|
+
if (selected === this.config.eos_id) {
|
|
108
|
+
ended = true;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
tokens.push(selected);
|
|
112
|
+
previous = selected;
|
|
113
|
+
}
|
|
114
|
+
if (!ended)
|
|
115
|
+
throw new Error("Decoder reached its output limit before EOS");
|
|
116
|
+
return {
|
|
117
|
+
text: (await this.tokenizer.decode(tokens)).trim(),
|
|
118
|
+
sourceTokens: ids.length,
|
|
119
|
+
outputTokens: tokens.length,
|
|
120
|
+
ended,
|
|
121
|
+
};
|
|
122
|
+
} finally {
|
|
123
|
+
state?.dispose();
|
|
124
|
+
input.dispose();
|
|
125
|
+
mask.dispose();
|
|
126
|
+
Object.values(encoded || {}).forEach((value) => value.dispose());
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async translate(text, signal) {
|
|
131
|
+
signal?.throwIfAborted();
|
|
132
|
+
const [, before, content, after] = text.match(/^(\s*)([\s\S]*?)(\s*)$/);
|
|
133
|
+
if (!content) return { text, segments: [] };
|
|
134
|
+
if (
|
|
135
|
+
content.length <= 512 &&
|
|
136
|
+
(await this.encode(content)).length <=
|
|
137
|
+
this.generation.model.context_length
|
|
138
|
+
) {
|
|
139
|
+
const result = await this.translateSegment(content);
|
|
140
|
+
return { text: before + result.text + after, segments: [result] };
|
|
141
|
+
}
|
|
142
|
+
const sentences = [
|
|
143
|
+
...new Intl.Segmenter(undefined, { granularity: "sentence" }).segment(
|
|
144
|
+
content,
|
|
145
|
+
),
|
|
146
|
+
].map((part) => part.segment);
|
|
147
|
+
if (sentences.length === 1) {
|
|
148
|
+
const characters = Array.from(content);
|
|
149
|
+
let boundary = characters
|
|
150
|
+
.slice(0, Math.floor(characters.length / 2))
|
|
151
|
+
.join("").length;
|
|
152
|
+
const space = content.lastIndexOf(" ", boundary);
|
|
153
|
+
if (space > boundary / 2) boundary = space + 1;
|
|
154
|
+
sentences.splice(
|
|
155
|
+
0,
|
|
156
|
+
1,
|
|
157
|
+
content.slice(0, boundary),
|
|
158
|
+
content.slice(boundary),
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
const results = [];
|
|
162
|
+
for (const sentence of sentences)
|
|
163
|
+
results.push(await this.translate(sentence, signal));
|
|
164
|
+
return {
|
|
165
|
+
text: before + results.map((result) => result.text).join("") + after,
|
|
166
|
+
segments: results.flatMap((result) => result.segments),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async close() {
|
|
171
|
+
await Promise.allSettled([
|
|
172
|
+
this.encoder?.release(),
|
|
173
|
+
this.decoder?.release(),
|
|
174
|
+
this.tokenizer?.close(),
|
|
175
|
+
]);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import {
|
|
2
|
+
mkdir,
|
|
3
|
+
mkdtemp,
|
|
4
|
+
readFile,
|
|
5
|
+
writeFile,
|
|
6
|
+
rename,
|
|
7
|
+
rm,
|
|
8
|
+
stat,
|
|
9
|
+
utimes,
|
|
10
|
+
} from "node:fs/promises";
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { homedir } from "node:os";
|
|
14
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
15
|
+
import { installCpuRuntime, runtimeVersion } from "./cpu-runtime.mjs";
|
|
16
|
+
import { modelId, resolveComponents } from "./catalog.mjs";
|
|
17
|
+
import { downloadArchive, extractComponent } from "./download.mjs";
|
|
18
|
+
|
|
19
|
+
export function defaultModelDirectory() {
|
|
20
|
+
return join(homedir(), ".local", "share", "claudex-translation", "models");
|
|
21
|
+
}
|
|
22
|
+
class ModelIntegrityError extends Error {}
|
|
23
|
+
|
|
24
|
+
export class ModelStore {
|
|
25
|
+
constructor(
|
|
26
|
+
directory = defaultModelDirectory(),
|
|
27
|
+
{
|
|
28
|
+
catalog = resolveComponents,
|
|
29
|
+
request = fetch,
|
|
30
|
+
installRuntime = installCpuRuntime,
|
|
31
|
+
} = {},
|
|
32
|
+
) {
|
|
33
|
+
this.directory = join(directory, modelId);
|
|
34
|
+
this.catalog = catalog;
|
|
35
|
+
this.request = request;
|
|
36
|
+
this.installRuntime = installRuntime;
|
|
37
|
+
}
|
|
38
|
+
async installed() {
|
|
39
|
+
let index;
|
|
40
|
+
try {
|
|
41
|
+
index = JSON.parse(
|
|
42
|
+
await readFile(join(this.directory, "installed.json"), "utf8"),
|
|
43
|
+
);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (error.code === "ENOENT") return null;
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
if (!/^\d+(\.\d+){3}-\d+(\.\d+){3}$/.test(index.version))
|
|
49
|
+
throw new ModelIntegrityError("模型版本记录无效");
|
|
50
|
+
const directory = join(this.directory, index.version);
|
|
51
|
+
const receipt = JSON.parse(
|
|
52
|
+
await readFile(join(directory, "ready.json"), "utf8"),
|
|
53
|
+
);
|
|
54
|
+
if (receipt.runtimeVersion !== runtimeVersion)
|
|
55
|
+
throw new ModelIntegrityError("CPU 运行库版本需要更新");
|
|
56
|
+
if (!receipt.files || Object.keys(receipt.files).length < 10)
|
|
57
|
+
throw new ModelIntegrityError("模型文件清单不完整");
|
|
58
|
+
for (const file of Object.keys(receipt.files)) {
|
|
59
|
+
if (!/^(model|runtime)\//.test(file) || file.split("/").includes(".."))
|
|
60
|
+
throw new ModelIntegrityError("模型文件路径无效");
|
|
61
|
+
const entry = await stat(join(directory, file));
|
|
62
|
+
if (entry.size !== receipt.files[file].size)
|
|
63
|
+
throw new ModelIntegrityError("模型文件不完整,请重新下载");
|
|
64
|
+
}
|
|
65
|
+
return { ...index, directory, receipt };
|
|
66
|
+
}
|
|
67
|
+
async prepare(signal, progress = () => {}) {
|
|
68
|
+
await mkdir(this.directory, { recursive: true, mode: 0o700 });
|
|
69
|
+
const lock = join(this.directory, ".download-lock");
|
|
70
|
+
while (true) {
|
|
71
|
+
signal.throwIfAborted();
|
|
72
|
+
try {
|
|
73
|
+
await mkdir(lock);
|
|
74
|
+
break;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (error.code !== "EEXIST") throw error;
|
|
77
|
+
const owner = await readFile(join(lock, "pid"), "utf8").catch(() => "");
|
|
78
|
+
if (owner) {
|
|
79
|
+
try {
|
|
80
|
+
process.kill(Number(owner), 0);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (error.code === "ESRCH") {
|
|
83
|
+
await rm(lock, { recursive: true, force: true });
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const entry = await stat(lock).catch((error) => {
|
|
89
|
+
if (error.code === "ENOENT") return null;
|
|
90
|
+
throw error;
|
|
91
|
+
});
|
|
92
|
+
if (!entry) continue;
|
|
93
|
+
if (Date.now() - entry.mtimeMs > 60000) {
|
|
94
|
+
await rm(lock, { recursive: true, force: true });
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
await delay(250, undefined, { signal });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const heartbeat = setInterval(() => {
|
|
101
|
+
const now = new Date();
|
|
102
|
+
utimes(lock, now, now).catch(() => {});
|
|
103
|
+
}, 10000);
|
|
104
|
+
let stage;
|
|
105
|
+
try {
|
|
106
|
+
await writeFile(join(lock, "pid"), String(process.pid), { mode: 0o600 });
|
|
107
|
+
try {
|
|
108
|
+
const installed = await this.installed();
|
|
109
|
+
if (installed) {
|
|
110
|
+
await this.verify(installed);
|
|
111
|
+
return installed;
|
|
112
|
+
}
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (!(
|
|
115
|
+
error instanceof ModelIntegrityError ||
|
|
116
|
+
error instanceof SyntaxError ||
|
|
117
|
+
error.code === "ENOENT"
|
|
118
|
+
))
|
|
119
|
+
throw error;
|
|
120
|
+
await rm(join(this.directory, "installed.json"), { force: true });
|
|
121
|
+
}
|
|
122
|
+
progress({ stage: "catalog", received: 0, total: 0 });
|
|
123
|
+
const components = await this.catalog(signal, this.request);
|
|
124
|
+
const version = `${components.model.version}-${components.tokenizer.version}`;
|
|
125
|
+
stage = await mkdtemp(join(this.directory, ".install-"));
|
|
126
|
+
const total = components.model.size + components.tokenizer.size;
|
|
127
|
+
let completed = 0;
|
|
128
|
+
const hashes = {};
|
|
129
|
+
for (const kind of ["model", "tokenizer"]) {
|
|
130
|
+
const target = join(stage, kind === "model" ? "model" : "runtime");
|
|
131
|
+
await mkdir(target, { recursive: true });
|
|
132
|
+
const zip = await downloadArchive(
|
|
133
|
+
components[kind],
|
|
134
|
+
target,
|
|
135
|
+
signal,
|
|
136
|
+
(received) =>
|
|
137
|
+
progress({
|
|
138
|
+
stage: "downloading",
|
|
139
|
+
received: completed + received,
|
|
140
|
+
total,
|
|
141
|
+
}),
|
|
142
|
+
this.request,
|
|
143
|
+
);
|
|
144
|
+
signal.throwIfAborted();
|
|
145
|
+
progress({
|
|
146
|
+
stage: "extracting",
|
|
147
|
+
received: completed + components[kind].size,
|
|
148
|
+
total,
|
|
149
|
+
});
|
|
150
|
+
const files = await extractComponent(zip, target, kind);
|
|
151
|
+
for (const [file, sha256] of Object.entries(files))
|
|
152
|
+
hashes[`${kind === "model" ? "model" : "runtime"}/${file}`] = sha256;
|
|
153
|
+
completed += components[kind].size;
|
|
154
|
+
}
|
|
155
|
+
progress({ stage: "runtime", received: completed, total });
|
|
156
|
+
const runtime = await this.installRuntime(join(stage, "runtime"), signal);
|
|
157
|
+
for (const [file, hash] of Object.entries(runtime))
|
|
158
|
+
hashes[`runtime/${file}`] = hash;
|
|
159
|
+
const files = {};
|
|
160
|
+
for (const [file, sha256] of Object.entries(hashes))
|
|
161
|
+
files[file] = { sha256, size: (await stat(join(stage, file))).size };
|
|
162
|
+
await writeFile(
|
|
163
|
+
join(stage, "ready.json"),
|
|
164
|
+
JSON.stringify({ version, runtimeVersion, components, files }),
|
|
165
|
+
{ mode: 0o600 },
|
|
166
|
+
);
|
|
167
|
+
signal.throwIfAborted();
|
|
168
|
+
const destination = join(this.directory, version);
|
|
169
|
+
await rm(destination, { recursive: true, force: true });
|
|
170
|
+
await rename(stage, destination);
|
|
171
|
+
stage = null;
|
|
172
|
+
await writeFile(
|
|
173
|
+
join(this.directory, "installed.json.tmp"),
|
|
174
|
+
JSON.stringify({ id: modelId, version }),
|
|
175
|
+
{ mode: 0o600 },
|
|
176
|
+
);
|
|
177
|
+
await rename(
|
|
178
|
+
join(this.directory, "installed.json.tmp"),
|
|
179
|
+
join(this.directory, "installed.json"),
|
|
180
|
+
);
|
|
181
|
+
return this.installed();
|
|
182
|
+
} finally {
|
|
183
|
+
clearInterval(heartbeat);
|
|
184
|
+
if (stage) await rm(stage, { recursive: true, force: true });
|
|
185
|
+
await rm(lock, { recursive: true, force: true });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async verify(installed) {
|
|
189
|
+
for (const [file, expected] of Object.entries(installed.receipt.files)) {
|
|
190
|
+
const hash = createHash("sha256")
|
|
191
|
+
.update(await readFile(join(installed.directory, file)))
|
|
192
|
+
.digest("hex");
|
|
193
|
+
if (hash !== expected.sha256)
|
|
194
|
+
throw new ModelIntegrityError("模型文件校验失败,请重新下载");
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { once } from "node:events";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
export class EdgeTokenizer {
|
|
7
|
+
constructor(directory, runtime) {
|
|
8
|
+
this.pending = [];
|
|
9
|
+
this.buffer = Buffer.alloc(0);
|
|
10
|
+
this.error = null;
|
|
11
|
+
this.child = spawn(
|
|
12
|
+
fileURLToPath(new URL("../bin/tokenizer-linux-x64", import.meta.url)),
|
|
13
|
+
[directory, join(runtime, "libonnxruntime-genai.so")],
|
|
14
|
+
{
|
|
15
|
+
env: { ...process.env, LD_LIBRARY_PATH: runtime },
|
|
16
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
17
|
+
},
|
|
18
|
+
);
|
|
19
|
+
let diagnostic = "";
|
|
20
|
+
this.child.stderr.on("data", (bytes) => {
|
|
21
|
+
diagnostic = (diagnostic + bytes).slice(-2000);
|
|
22
|
+
});
|
|
23
|
+
const failed = (error) => {
|
|
24
|
+
this.error = error;
|
|
25
|
+
for (const request of this.pending.splice(0)) request.reject(error);
|
|
26
|
+
};
|
|
27
|
+
this.child.on("error", failed);
|
|
28
|
+
this.child.on("exit", (code) =>
|
|
29
|
+
failed(new Error(diagnostic || `翻译分词进程已结束 (${code})`)),
|
|
30
|
+
);
|
|
31
|
+
this.child.stdin.on("error", failed);
|
|
32
|
+
this.child.stdout.on("data", (bytes) => {
|
|
33
|
+
this.buffer = Buffer.concat([this.buffer, bytes]);
|
|
34
|
+
while (this.buffer.length >= 4) {
|
|
35
|
+
const length = this.buffer.readUInt32LE();
|
|
36
|
+
if (length > 4 * 1024 * 1024) {
|
|
37
|
+
failed(new Error("分词响应过长"));
|
|
38
|
+
this.child.kill();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (this.buffer.length < length + 4) return;
|
|
42
|
+
const result = this.buffer.subarray(4, length + 4);
|
|
43
|
+
this.buffer = this.buffer.subarray(length + 4);
|
|
44
|
+
this.pending.shift()?.resolve(result);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
request(operation, input) {
|
|
49
|
+
if (this.error) return Promise.reject(this.error);
|
|
50
|
+
const header = Buffer.alloc(5);
|
|
51
|
+
header.write(operation);
|
|
52
|
+
header.writeUInt32LE(input.length, 1);
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
this.pending.push({ resolve, reject });
|
|
55
|
+
this.child.stdin.write(Buffer.concat([header, input]));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
async encode(text) {
|
|
59
|
+
const bytes = await this.request("E", Buffer.from(text));
|
|
60
|
+
return Array.from({ length: bytes.length / 4 }, (_, index) =>
|
|
61
|
+
bytes.readInt32LE(index * 4),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
async decode(tokens) {
|
|
65
|
+
const bytes = Buffer.alloc(tokens.length * 4);
|
|
66
|
+
tokens.forEach((token, index) => bytes.writeInt32LE(token, index * 4));
|
|
67
|
+
return (await this.request("D", bytes)).toString("utf8");
|
|
68
|
+
}
|
|
69
|
+
async close() {
|
|
70
|
+
if (this.child.exitCode !== null || this.child.signalCode) return;
|
|
71
|
+
const exited = once(this.child, "exit");
|
|
72
|
+
const timer = setTimeout(() => this.child.kill("SIGKILL"), 2000);
|
|
73
|
+
this.child.stdin.end();
|
|
74
|
+
await exited;
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { setGlobalProxyFromEnv } from "node:http";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { ModelStore } from "./model-store.mjs";
|
|
4
|
+
import { EdgeTranslation } from "./inference.mjs";
|
|
5
|
+
process.env.ORT_DISABLE_TELEMETRY = "1";
|
|
6
|
+
setGlobalProxyFromEnv();
|
|
7
|
+
let store;
|
|
8
|
+
const reply = (message) => {
|
|
9
|
+
if (process.connected) process.send(message);
|
|
10
|
+
};
|
|
11
|
+
const controller = new AbortController();
|
|
12
|
+
let engine;
|
|
13
|
+
let model;
|
|
14
|
+
let queue = Promise.resolve();
|
|
15
|
+
let closing = false;
|
|
16
|
+
function close() {
|
|
17
|
+
if (closing) return;
|
|
18
|
+
closing = true;
|
|
19
|
+
controller.abort(new Error("翻译已关闭"));
|
|
20
|
+
queue.finally(async () => {
|
|
21
|
+
await engine?.close();
|
|
22
|
+
if (process.connected) process.disconnect();
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
process.on("disconnect", close);
|
|
26
|
+
process.on("message", (message) => {
|
|
27
|
+
if (message.action === "close") {
|
|
28
|
+
close();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const task = queue.then(async () => {
|
|
32
|
+
controller.signal.throwIfAborted();
|
|
33
|
+
if (message.action === "prepare") {
|
|
34
|
+
if (model) return model;
|
|
35
|
+
if (process.platform !== "linux" || process.arch !== "x64")
|
|
36
|
+
throw new Error("本地翻译引擎目前支持 Linux x64");
|
|
37
|
+
const glibc =
|
|
38
|
+
process.report.getReport().header.glibcVersionRuntime || "0.0";
|
|
39
|
+
const [major, minor] = glibc.split(".").map(Number);
|
|
40
|
+
if (major < 2 || (major === 2 && minor < 34))
|
|
41
|
+
throw new Error("本地翻译引擎需要 glibc 2.34 或更高版本");
|
|
42
|
+
store ??= new ModelStore(message.directory);
|
|
43
|
+
const installed = await store.prepare(controller.signal, (progress) =>
|
|
44
|
+
reply({ progress }),
|
|
45
|
+
);
|
|
46
|
+
engine = new EdgeTranslation();
|
|
47
|
+
try {
|
|
48
|
+
await engine.load(
|
|
49
|
+
join(installed.directory, "model"),
|
|
50
|
+
join(installed.directory, "runtime"),
|
|
51
|
+
);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
await engine.close();
|
|
54
|
+
engine = null;
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
model = {
|
|
58
|
+
id: installed.id,
|
|
59
|
+
version: installed.version,
|
|
60
|
+
directory: installed.directory,
|
|
61
|
+
};
|
|
62
|
+
return model;
|
|
63
|
+
}
|
|
64
|
+
if (!engine) throw new Error("翻译模型尚未就绪");
|
|
65
|
+
const result = [];
|
|
66
|
+
for (const text of message.texts) {
|
|
67
|
+
controller.signal.throwIfAborted();
|
|
68
|
+
result.push((await engine.translate(text, controller.signal)).text);
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
});
|
|
72
|
+
queue = task.catch(() => {});
|
|
73
|
+
task.then(
|
|
74
|
+
(result) => reply({ id: message.id, result }),
|
|
75
|
+
(error) =>
|
|
76
|
+
reply({
|
|
77
|
+
id: message.id,
|
|
78
|
+
error: error.cause?.code
|
|
79
|
+
? `${error.message} (${error.cause.code})`
|
|
80
|
+
: error.message,
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { createBackendConfiguration } from "../server/src/backends/configuration.mjs";
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { access, copyFile, mkdir, writeFile } from "node:fs/promises";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { dirname, join, resolve } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { migrateProductConfig } from "../server/src/product-config.mjs";
|
|
8
|
+
import { readConfig } from "../server/src/config.mjs";
|
|
9
|
+
|
|
10
|
+
const root = fileURLToPath(new URL("../", import.meta.url));
|
|
11
|
+
export async function installServices({ configDir = root, cli = false } = {}) {
|
|
12
|
+
if (process.platform !== "linux")
|
|
13
|
+
throw new Error("后台服务安装需要 Linux 和 systemd 用户服务。");
|
|
14
|
+
execFileSync("systemctl", ["--user", "show-environment"], { stdio: "pipe" });
|
|
15
|
+
const config = readConfig();
|
|
16
|
+
await migrateProductConfig(config);
|
|
17
|
+
const configured = await createBackendConfiguration(config).read();
|
|
18
|
+
const codexBackend = configured.backends.find(
|
|
19
|
+
(b) => b.id === "codex" && b.type === "codex" && b.enabled,
|
|
20
|
+
);
|
|
21
|
+
const manageCodex =
|
|
22
|
+
codexBackend &&
|
|
23
|
+
["localhost", "127.0.0.1", "[::1]"].includes(
|
|
24
|
+
new URL(codexBackend.url).hostname,
|
|
25
|
+
);
|
|
26
|
+
const userUnits = join(
|
|
27
|
+
process.env.XDG_CONFIG_HOME || join(homedir(), ".config"),
|
|
28
|
+
"systemd/user",
|
|
29
|
+
);
|
|
30
|
+
const backups = join(
|
|
31
|
+
process.env.XDG_STATE_HOME || join(homedir(), ".local/state"),
|
|
32
|
+
"claudex-web/service-backups",
|
|
33
|
+
new Date().toISOString().replaceAll(":", "-"),
|
|
34
|
+
);
|
|
35
|
+
const quote = (value) => JSON.stringify(value.replaceAll("%", "%%"));
|
|
36
|
+
const unitPath = (value) =>
|
|
37
|
+
value
|
|
38
|
+
.replaceAll("%", "%%")
|
|
39
|
+
.replace(
|
|
40
|
+
/[\s\\"]/g,
|
|
41
|
+
(char) => "\\x" + char.charCodeAt(0).toString(16).padStart(2, "0"),
|
|
42
|
+
);
|
|
43
|
+
const exists = async (path) =>
|
|
44
|
+
access(path).then(
|
|
45
|
+
() => true,
|
|
46
|
+
() => false,
|
|
47
|
+
);
|
|
48
|
+
await access(join(root, "client/dist/index.html"));
|
|
49
|
+
await mkdir(userUnits, { recursive: true });
|
|
50
|
+
await mkdir(backups, { recursive: true });
|
|
51
|
+
|
|
52
|
+
const backendPath = join(userUnits, "codex-app-server.service");
|
|
53
|
+
if (manageCodex && !(await exists(backendPath))) {
|
|
54
|
+
await writeFile(
|
|
55
|
+
backendPath,
|
|
56
|
+
`[Unit]
|
|
57
|
+
Description=Codex app-server
|
|
58
|
+
After=network.target
|
|
59
|
+
|
|
60
|
+
[Service]
|
|
61
|
+
Type=simple
|
|
62
|
+
WorkingDirectory=${unitPath(homedir())}
|
|
63
|
+
Environment=${quote("PATH=" + dirname(process.execPath) + ":" + process.env.PATH)}
|
|
64
|
+
Environment=${quote("CLAUDEX_WEB_CONFIG_DIR=" + configDir)}
|
|
65
|
+
Environment=${quote("CLAUDEX_WEB_BACKENDS_FILE=" + config.backendsFile)}
|
|
66
|
+
Environment=${quote("CODEX_HOME=" + codexBackend.dataDir)}
|
|
67
|
+
Environment=${quote("CODEX_APP_SERVER_URL=" + codexBackend.url)}
|
|
68
|
+
EnvironmentFile=-${unitPath(join(configDir, ".env.backend"))}
|
|
69
|
+
ExecStart=${quote(process.execPath)} ${quote(join(root, "scripts/start-backend.mjs"))}
|
|
70
|
+
Restart=on-failure
|
|
71
|
+
RestartSec=5
|
|
72
|
+
UMask=0077
|
|
73
|
+
|
|
74
|
+
[Install]
|
|
75
|
+
WantedBy=default.target
|
|
76
|
+
`,
|
|
77
|
+
);
|
|
78
|
+
} else if (manageCodex)
|
|
79
|
+
console.log(
|
|
80
|
+
"保留现有 codex-app-server.service 的登录环境、代理和启动配置。",
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const webPath = join(userUnits, "claudex-web.service");
|
|
84
|
+
if (await exists(webPath))
|
|
85
|
+
await copyFile(webPath, join(backups, "claudex-web.service"));
|
|
86
|
+
await writeFile(
|
|
87
|
+
webPath,
|
|
88
|
+
`[Unit]
|
|
89
|
+
Description=Claudex Web
|
|
90
|
+
${manageCodex ? "Wants=codex-app-server.service\nAfter=network.target codex-app-server.service" : "After=network.target"}
|
|
91
|
+
|
|
92
|
+
[Service]
|
|
93
|
+
Type=simple
|
|
94
|
+
WorkingDirectory=${unitPath(configDir)}
|
|
95
|
+
Environment=${quote("PATH=" + dirname(process.execPath) + ":" + process.env.PATH)}
|
|
96
|
+
${cli ? `Environment=${quote("CLAUDEX_WEB_CONFIG_DIR=" + configDir)}\n` : ""}EnvironmentFile=-${unitPath(join(configDir, ".env"))}
|
|
97
|
+
ExecStart=${quote(process.execPath)} ${quote(join(root, cli ? "bin/claudex-web.mjs" : "server/src/server.mjs"))}${cli ? " start" : ""}
|
|
98
|
+
Restart=on-failure
|
|
99
|
+
RestartSec=5
|
|
100
|
+
UMask=0077
|
|
101
|
+
|
|
102
|
+
[Install]
|
|
103
|
+
WantedBy=default.target
|
|
104
|
+
`,
|
|
105
|
+
);
|
|
106
|
+
execFileSync(
|
|
107
|
+
"systemd-analyze",
|
|
108
|
+
["--user", "verify", webPath, ...(manageCodex ? [backendPath] : [])],
|
|
109
|
+
{
|
|
110
|
+
stdio: "inherit",
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
execFileSync("systemctl", ["--user", "daemon-reload"], { stdio: "inherit" });
|
|
114
|
+
execFileSync(
|
|
115
|
+
"systemctl",
|
|
116
|
+
[
|
|
117
|
+
"--user",
|
|
118
|
+
"enable",
|
|
119
|
+
"--now",
|
|
120
|
+
...(manageCodex ? ["codex-app-server.service"] : []),
|
|
121
|
+
"claudex-web.service",
|
|
122
|
+
],
|
|
123
|
+
{ stdio: "inherit" },
|
|
124
|
+
);
|
|
125
|
+
console.log(`服务配置已安装,旧网页服务配置备份在 ${backups}`);
|
|
126
|
+
console.log(
|
|
127
|
+
`正在运行的网页服务会继续运行;需要切换桥接服务代码时执行 ${cli ? "claudex-web service restart-when-idle" : "npm run service:restart-when-idle"}。`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (
|
|
132
|
+
process.argv[1] &&
|
|
133
|
+
resolve(process.argv[1]) === fileURLToPath(import.meta.url)
|
|
134
|
+
)
|
|
135
|
+
await installServices();
|