@hjd-hitsz/claudex-web 4.0.1 → 4.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/CHANGELOG.md +71 -0
- package/README.md +33 -55
- package/build-info.json +4 -4
- package/client/dist/app.js +95 -95
- package/client/dist/document-editor.js +96 -93
- package/client/dist/style.css +1 -1
- package/docs/architecture/backends.md +2 -0
- package/docs/architecture/behavior.md +34 -6
- package/docs/architecture.md +42 -5
- package/docs/deployment.md +34 -14
- package/docs/development.md +31 -4
- package/docs/images/desktop-dark.png +0 -0
- package/docs/images/desktop-light.png +0 -0
- package/docs/images/mobile.png +0 -0
- package/docs/releases/v4.1.0.md +21 -0
- package/docs/releases/v4.1.1.md +19 -0
- package/docs/versioning.md +10 -0
- package/package.json +25 -8
- 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 +5 -4
- package/scripts/start-backend.mjs +7 -1
- package/server/src/application.mjs +40 -5
- package/server/src/backends/claude/catalogue.mjs +5 -3
- package/server/src/backends/claude/execution-session.mjs +7 -1
- package/server/src/backends/claude/messages.mjs +2 -1
- package/server/src/backends/claude/provider-environment.mjs +6 -4
- package/server/src/backends/claude/sessions.mjs +1 -0
- package/server/src/backends/claude/worker-client.mjs +13 -6
- package/server/src/backends/claude/worker-location.mjs +12 -0
- package/server/src/backends/cli-installation.mjs +28 -13
- package/server/src/backends/cli-package.mjs +116 -0
- package/server/src/backends/codex/adapter.mjs +2 -0
- package/server/src/backends/codex/events.mjs +1 -1
- package/server/src/backends/codex/token-usage.mjs +140 -0
- package/server/src/backends/manager.mjs +87 -21
- package/server/src/backends/provider-presets.mjs +1 -1
- package/server/src/backends/providers.mjs +6 -3
- package/server/src/backends/storage.mjs +7 -3
- package/server/src/config.mjs +7 -0
- package/server/src/http/maintenance.mjs +28 -0
- package/server/src/http/router.mjs +20 -2
- package/server/src/server.mjs +9 -0
- package/server/src/service-maintenance-worker.mjs +124 -0
- package/server/src/service-maintenance.mjs +222 -0
- package/server/src/summary-translation.mjs +42 -53
- 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
|
@@ -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
|
+
});
|
|
@@ -51,9 +51,6 @@ export async function installServices({ configDir = root, cli = false } = {}) {
|
|
|
51
51
|
|
|
52
52
|
const backendPath = join(userUnits, "codex-app-server.service");
|
|
53
53
|
if (manageCodex && !(await exists(backendPath))) {
|
|
54
|
-
const codex = execFileSync("which", [codexBackend.executable || "codex"], {
|
|
55
|
-
encoding: "utf8",
|
|
56
|
-
}).trim();
|
|
57
54
|
await writeFile(
|
|
58
55
|
backendPath,
|
|
59
56
|
`[Unit]
|
|
@@ -64,8 +61,12 @@ After=network.target
|
|
|
64
61
|
Type=simple
|
|
65
62
|
WorkingDirectory=${unitPath(homedir())}
|
|
66
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)}
|
|
67
68
|
EnvironmentFile=-${unitPath(join(configDir, ".env.backend"))}
|
|
68
|
-
ExecStart=${quote(
|
|
69
|
+
ExecStart=${quote(process.execPath)} ${quote(join(root, "scripts/start-backend.mjs"))}
|
|
69
70
|
Restart=on-failure
|
|
70
71
|
RestartSec=5
|
|
71
72
|
UMask=0077
|
|
@@ -6,6 +6,12 @@ const config = readConfig();
|
|
|
6
6
|
const { backends } = await createBackendConfiguration(config).read();
|
|
7
7
|
const backend = backends.find((b) => b.id === "codex" && b.enabled);
|
|
8
8
|
if (!backend) throw new Error("Codex 后端未启用");
|
|
9
|
+
const url = new URL(backend.url);
|
|
10
|
+
if (url.protocol !== "ws:" || url.pathname !== "/" || url.search || url.hash)
|
|
11
|
+
throw new Error(
|
|
12
|
+
"Codex 本机监听地址须使用 ws://主机:端口,且不含路径、查询参数或片段",
|
|
13
|
+
);
|
|
14
|
+
const listenUrl = `ws://${url.hostname}:${url.port || 80}`;
|
|
9
15
|
const child = spawn(
|
|
10
16
|
backend.executable || "codex",
|
|
11
17
|
[
|
|
@@ -14,7 +20,7 @@ const child = spawn(
|
|
|
14
20
|
"thread_unload_delay_secs=0",
|
|
15
21
|
"app-server",
|
|
16
22
|
"--listen",
|
|
17
|
-
|
|
23
|
+
listenUrl,
|
|
18
24
|
],
|
|
19
25
|
{ stdio: "inherit", env: { ...process.env, CODEX_HOME: backend.dataDir } },
|
|
20
26
|
);
|
|
@@ -1,9 +1,22 @@
|
|
|
1
|
+
import { hasBackendWork } from "../../shared/backend-activity.mjs";
|
|
2
|
+
import { createServiceMaintenance } from "./service-maintenance.mjs";
|
|
3
|
+
import { createTranslationService } from "./translation-service.mjs";
|
|
1
4
|
import https from "node:https";
|
|
2
5
|
import { WebSocketServer } from "ws";
|
|
3
6
|
import { createHttpHandler } from "./http/router.mjs";
|
|
4
7
|
import { createBackendManager } from "./backends/manager.mjs";
|
|
5
8
|
import { sendRpcText } from "./rpc-transport.mjs";
|
|
6
9
|
export function createWebApplication({ config, tls, authentication, version }) {
|
|
10
|
+
let requestsInFlight = 0;
|
|
11
|
+
const maintenance = createServiceMaintenance({
|
|
12
|
+
config,
|
|
13
|
+
version,
|
|
14
|
+
isBusy: () =>
|
|
15
|
+
!bridge.maintenanceState().params.ready ||
|
|
16
|
+
requestsInFlight > 0 ||
|
|
17
|
+
hasBackendWork(bridge.state().params),
|
|
18
|
+
});
|
|
19
|
+
const translation = createTranslationService(config);
|
|
7
20
|
const clients = new WebSocketServer({ noServer: true });
|
|
8
21
|
const monitors = new WebSocketServer({ noServer: true });
|
|
9
22
|
function send(client, message) {
|
|
@@ -30,6 +43,8 @@ export function createWebApplication({ config, tls, authentication, version }) {
|
|
|
30
43
|
tls,
|
|
31
44
|
createHttpHandler({
|
|
32
45
|
config,
|
|
46
|
+
translation,
|
|
47
|
+
maintenance,
|
|
33
48
|
tls,
|
|
34
49
|
authentication,
|
|
35
50
|
version,
|
|
@@ -83,12 +98,28 @@ export function createWebApplication({ config, tls, authentication, version }) {
|
|
|
83
98
|
} catch {
|
|
84
99
|
return;
|
|
85
100
|
}
|
|
86
|
-
|
|
101
|
+
if (maintenance.switching) {
|
|
87
102
|
send(client, {
|
|
88
103
|
id: message.id,
|
|
89
|
-
error: {
|
|
90
|
-
|
|
91
|
-
|
|
104
|
+
error: {
|
|
105
|
+
code: -32000,
|
|
106
|
+
message: "服务正在更新或重启,请等待连接恢复",
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
requestsInFlight++;
|
|
112
|
+
bridge
|
|
113
|
+
.receive(client, message)
|
|
114
|
+
.finally(() => {
|
|
115
|
+
requestsInFlight--;
|
|
116
|
+
})
|
|
117
|
+
.catch((error) =>
|
|
118
|
+
send(client, {
|
|
119
|
+
id: message.id,
|
|
120
|
+
error: { code: -32000, message: error.message },
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
92
123
|
});
|
|
93
124
|
});
|
|
94
125
|
const heartbeat = setInterval(() => {
|
|
@@ -108,6 +139,8 @@ export function createWebApplication({ config, tls, authentication, version }) {
|
|
|
108
139
|
return {
|
|
109
140
|
server,
|
|
110
141
|
async start() {
|
|
142
|
+
maintenance.initialize();
|
|
143
|
+
await translation.initialize();
|
|
111
144
|
await new Promise((resolve, reject) => {
|
|
112
145
|
server.once("error", reject);
|
|
113
146
|
server.listen(config.port, config.host, () => {
|
|
@@ -119,7 +152,9 @@ export function createWebApplication({ config, tls, authentication, version }) {
|
|
|
119
152
|
},
|
|
120
153
|
async close() {
|
|
121
154
|
clearInterval(heartbeat);
|
|
122
|
-
|
|
155
|
+
maintenance.close();
|
|
156
|
+
await translation.close();
|
|
157
|
+
await bridge.close();
|
|
123
158
|
for (const client of [...clients.clients, ...monitors.clients])
|
|
124
159
|
client.terminate();
|
|
125
160
|
clients.close();
|
|
@@ -140,13 +140,15 @@ export function createClaudeCatalogue({
|
|
|
140
140
|
throw new Error("Claude Code 插件目录格式无效");
|
|
141
141
|
const records = provider
|
|
142
142
|
? provider.models.map((value) => {
|
|
143
|
-
const metadata =
|
|
144
|
-
(provider.protocol || "messages") === "anthropic"
|
|
143
|
+
const metadata = {
|
|
144
|
+
...((provider.protocol || "messages") === "anthropic"
|
|
145
145
|
? models.find(
|
|
146
146
|
(model) =>
|
|
147
147
|
model.value === value || model.resolvedModel === value,
|
|
148
148
|
)
|
|
149
|
-
:
|
|
149
|
+
: {}),
|
|
150
|
+
...provider.modelCapabilities?.[value],
|
|
151
|
+
};
|
|
150
152
|
return modelRecord(
|
|
151
153
|
{
|
|
152
154
|
...metadata,
|