@lynn123411/dsh-chat-translate 3.9.6 → 3.9.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -7
- package/lib/client.js +70 -63
- package/lib/client.js.map +3 -3
- package/lib/index.js +67 -47
- package/lib/index.js.map +4 -4
- package/lib/types/client/chat/assistant-step.d.ts +2 -1
- package/lib/types/client/chat/styles.d.ts +2 -2
- package/lib/types/client/chat/translate-store.d.ts +6 -11
- package/lib/types/client/index.d.ts +0 -1
- package/lib/types/server/cache.d.ts +5 -10
- package/lib/types/server/dispatcher.d.ts +4 -0
- package/lib/types/server/prompt-revision.d.ts +4 -16
- package/lib/types/translation-config.d.ts +6 -0
- package/package.json +13 -13
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// node_modules/.pnpm/@deepseek-ai+cosmokit@1.
|
|
1
|
+
// node_modules/.pnpm/@deepseek-ai+cosmokit@1.10.0-alpha.1/node_modules/@deepseek-ai/cosmokit/lib/index.js
|
|
2
2
|
function isNullable(value) {
|
|
3
3
|
return value === null || value === void 0;
|
|
4
4
|
}
|
|
@@ -223,7 +223,7 @@ var Time;
|
|
|
223
223
|
Time2.template = template;
|
|
224
224
|
})(Time || (Time = {}));
|
|
225
225
|
|
|
226
|
-
// node_modules/.pnpm/@deepseek-ai+schemastery@3.
|
|
226
|
+
// node_modules/.pnpm/@deepseek-ai+schemastery@3.20.0-alpha.1/node_modules/@deepseek-ai/schemastery/lib/index.mjs
|
|
227
227
|
var kSchema = Symbol.for("schemastery");
|
|
228
228
|
var kValidationError = Symbol.for("ValidationError");
|
|
229
229
|
globalThis.__schemastery_index__ ??= 0;
|
|
@@ -963,7 +963,7 @@ var CredentialsReader = class {
|
|
|
963
963
|
import * as fs from "node:fs/promises";
|
|
964
964
|
import * as path from "node:path";
|
|
965
965
|
|
|
966
|
-
// node_modules/.pnpm/@deepseek-ai+dsh-home-paths@0.2.
|
|
966
|
+
// node_modules/.pnpm/@deepseek-ai+dsh-home-paths@0.2.3-alpha.2_@deepseek-ai+cordis@4.2.0-alpha.1/node_modules/@deepseek-ai/dsh-home-paths/lib/index.js
|
|
967
967
|
import { homedir } from "node:os";
|
|
968
968
|
import { basename, dirname, join, resolve as resolve2 } from "node:path";
|
|
969
969
|
var DSH_HOME_DIR_NAME = ".dsh";
|
|
@@ -1020,20 +1020,25 @@ function promptFor(text, prompt) {
|
|
|
1020
1020
|
${PARAGRAPH_KEEP_CLAUSE}`;
|
|
1021
1021
|
}
|
|
1022
1022
|
|
|
1023
|
+
// src/translation-config.ts
|
|
1024
|
+
function translationConfigKey(config) {
|
|
1025
|
+
return JSON.stringify([resolvePrompt(config.prompt), config.baseUrl?.trim() ?? "", config.model?.trim() ?? ""]);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1023
1028
|
// src/server/sampling.ts
|
|
1024
1029
|
var SAMPLING_TEMPERATURE = 0.7;
|
|
1025
1030
|
var SAMPLING_TOP_P = 0.6;
|
|
1026
1031
|
|
|
1027
1032
|
// src/server/prompt-revision.ts
|
|
1028
1033
|
var PROMPT_REVISION = "r7";
|
|
1029
|
-
function cacheGeneration(
|
|
1034
|
+
function cacheGeneration(config = {}) {
|
|
1030
1035
|
const source = [
|
|
1031
|
-
|
|
1036
|
+
translationConfigKey(config),
|
|
1032
1037
|
`temperature=${SAMPLING_TEMPERATURE}`,
|
|
1033
1038
|
`top_p=${SAMPLING_TOP_P}`
|
|
1034
1039
|
].join("\0");
|
|
1035
1040
|
const digest = createHash("sha256").update(source, "utf8").digest("hex").slice(0, 12);
|
|
1036
|
-
return
|
|
1041
|
+
return `exact-text:${PROMPT_REVISION}:${digest}`;
|
|
1037
1042
|
}
|
|
1038
1043
|
|
|
1039
1044
|
// src/server/cache.ts
|
|
@@ -1042,37 +1047,36 @@ var LruDiskCache = class {
|
|
|
1042
1047
|
maxEntries;
|
|
1043
1048
|
filePath;
|
|
1044
1049
|
revision;
|
|
1050
|
+
revisionEpoch = 0;
|
|
1045
1051
|
saveTimer = null;
|
|
1046
1052
|
dirty = false;
|
|
1047
|
-
|
|
1053
|
+
saving = Promise.resolve();
|
|
1054
|
+
disposed = false;
|
|
1055
|
+
constructor(maxEntries = 1e3, fileName = "cache.json", revision = cacheGeneration()) {
|
|
1048
1056
|
this.maxEntries = maxEntries;
|
|
1049
1057
|
this.revision = revision;
|
|
1050
1058
|
this.filePath = dshHomePath("dsh-chat-translate", fileName);
|
|
1051
1059
|
}
|
|
1052
|
-
/**
|
|
1053
|
-
* Move the pool to the generation a prompt edit produced.
|
|
1054
|
-
*
|
|
1055
|
-
* Called whenever the prompt in force changes. A different revision means the
|
|
1056
|
-
* entries already held were written under wording the reader has replaced, so
|
|
1057
|
-
* they are dropped rather than served under the new prompt; the same revision
|
|
1058
|
-
* is a no-op, so re-committing the same text costs nothing.
|
|
1059
|
-
*
|
|
1060
|
-
* @param revision - the generation the current prompt resolves to.
|
|
1061
|
-
*/
|
|
1060
|
+
/** 配置换代时清空内存,并安排覆写磁盘中的旧池。 */
|
|
1062
1061
|
setRevision(revision) {
|
|
1063
1062
|
if (revision === this.revision) return;
|
|
1064
1063
|
this.revision = revision;
|
|
1065
|
-
|
|
1064
|
+
this.revisionEpoch += 1;
|
|
1066
1065
|
this.cache.clear();
|
|
1067
1066
|
this.dirty = true;
|
|
1068
1067
|
this.scheduleSave();
|
|
1069
1068
|
}
|
|
1070
1069
|
async init() {
|
|
1070
|
+
const epoch = this.revisionEpoch;
|
|
1071
1071
|
try {
|
|
1072
1072
|
const content = await fs.readFile(this.filePath, "utf-8");
|
|
1073
1073
|
const doc = JSON.parse(content);
|
|
1074
|
+
if (epoch !== this.revisionEpoch || this.dirty || this.cache.size > 0 || this.disposed) return;
|
|
1074
1075
|
if (doc && typeof doc === "object" && doc.rev === this.revision && typeof doc.entries === "object" && doc.entries !== null) {
|
|
1075
1076
|
this.loadEntries(doc.entries);
|
|
1077
|
+
} else {
|
|
1078
|
+
this.dirty = true;
|
|
1079
|
+
this.scheduleSave();
|
|
1076
1080
|
}
|
|
1077
1081
|
} catch {
|
|
1078
1082
|
}
|
|
@@ -1094,6 +1098,7 @@ var LruDiskCache = class {
|
|
|
1094
1098
|
return entry.v;
|
|
1095
1099
|
}
|
|
1096
1100
|
set(key, value) {
|
|
1101
|
+
if (this.disposed) return;
|
|
1097
1102
|
if (this.cache.has(key)) {
|
|
1098
1103
|
this.cache.delete(key);
|
|
1099
1104
|
} else if (this.cache.size >= this.maxEntries) {
|
|
@@ -1107,7 +1112,7 @@ var LruDiskCache = class {
|
|
|
1107
1112
|
this.scheduleSave();
|
|
1108
1113
|
}
|
|
1109
1114
|
scheduleSave() {
|
|
1110
|
-
if (this.saveTimer) return;
|
|
1115
|
+
if (this.disposed || this.saveTimer) return;
|
|
1111
1116
|
this.saveTimer = setTimeout(() => {
|
|
1112
1117
|
this.saveTimer = null;
|
|
1113
1118
|
if (this.dirty) {
|
|
@@ -1117,41 +1122,36 @@ var LruDiskCache = class {
|
|
|
1117
1122
|
}
|
|
1118
1123
|
}, 5e3);
|
|
1119
1124
|
}
|
|
1120
|
-
|
|
1125
|
+
flush() {
|
|
1121
1126
|
if (this.saveTimer) {
|
|
1122
1127
|
clearTimeout(this.saveTimer);
|
|
1123
1128
|
this.saveTimer = null;
|
|
1124
1129
|
}
|
|
1130
|
+
this.saving = this.saving.then(() => this.writeSnapshot());
|
|
1131
|
+
return this.saving;
|
|
1132
|
+
}
|
|
1133
|
+
async writeSnapshot() {
|
|
1134
|
+
if (!this.dirty) return;
|
|
1135
|
+
this.dirty = false;
|
|
1125
1136
|
const tmpPath = `${this.filePath}.tmp.${Date.now()}.${Math.random().toString(36).slice(2)}`;
|
|
1126
1137
|
try {
|
|
1127
|
-
const
|
|
1128
|
-
for (const [k, v] of this.cache.entries()) {
|
|
1129
|
-
entries[k] = v;
|
|
1130
|
-
}
|
|
1131
|
-
const doc = { rev: this.revision, entries };
|
|
1138
|
+
const doc = { rev: this.revision, entries: Object.fromEntries(this.cache) };
|
|
1132
1139
|
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
|
|
1133
1140
|
await fs.writeFile(tmpPath, JSON.stringify(doc, null, 2), "utf-8");
|
|
1134
1141
|
await fs.rename(tmpPath, this.filePath);
|
|
1135
|
-
this.dirty = false;
|
|
1136
1142
|
} catch (err) {
|
|
1143
|
+
this.dirty = true;
|
|
1137
1144
|
console.warn("[dsh-chat-translate] Failed to write cache file atomically:", err);
|
|
1138
1145
|
try {
|
|
1139
1146
|
await fs.unlink(tmpPath);
|
|
1140
1147
|
} catch {
|
|
1141
1148
|
}
|
|
1142
|
-
|
|
1143
|
-
this.scheduleSave();
|
|
1144
|
-
}
|
|
1149
|
+
this.scheduleSave();
|
|
1145
1150
|
}
|
|
1146
1151
|
}
|
|
1147
1152
|
async dispose() {
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
this.saveTimer = null;
|
|
1151
|
-
}
|
|
1152
|
-
if (this.dirty) {
|
|
1153
|
-
await this.flush();
|
|
1154
|
-
}
|
|
1153
|
+
this.disposed = true;
|
|
1154
|
+
await this.flush();
|
|
1155
1155
|
}
|
|
1156
1156
|
};
|
|
1157
1157
|
|
|
@@ -1644,6 +1644,8 @@ var TranslationDispatcher = class {
|
|
|
1644
1644
|
configManager;
|
|
1645
1645
|
cache;
|
|
1646
1646
|
adapters = /* @__PURE__ */ new Map();
|
|
1647
|
+
revision = "";
|
|
1648
|
+
generation = 0;
|
|
1647
1649
|
/** 正文请求的串行队列尾,保证同时最多一个在途请求。 */
|
|
1648
1650
|
replyTail = Promise.resolve();
|
|
1649
1651
|
constructor(configManager, cache, credentials) {
|
|
@@ -1651,6 +1653,19 @@ var TranslationDispatcher = class {
|
|
|
1651
1653
|
this.cache = cache;
|
|
1652
1654
|
this.registerAdapter(new OpenAiCompatibleAdapter(credentials ?? { getApiKey: () => "" }));
|
|
1653
1655
|
}
|
|
1656
|
+
watchConfig() {
|
|
1657
|
+
this.updateGeneration(this.configManager.getConfig());
|
|
1658
|
+
return this.configManager.onConfigChange((config) => this.updateGeneration(config));
|
|
1659
|
+
}
|
|
1660
|
+
updateGeneration(config) {
|
|
1661
|
+
const revision = cacheGeneration(config);
|
|
1662
|
+
if (revision !== this.revision) {
|
|
1663
|
+
this.revision = revision;
|
|
1664
|
+
this.generation += 1;
|
|
1665
|
+
this.cache.setRevision(revision);
|
|
1666
|
+
}
|
|
1667
|
+
return this.generation;
|
|
1668
|
+
}
|
|
1654
1669
|
registerAdapter(adapter) {
|
|
1655
1670
|
this.adapters.set(adapter.id, adapter);
|
|
1656
1671
|
}
|
|
@@ -1676,12 +1691,11 @@ var TranslationDispatcher = class {
|
|
|
1676
1691
|
* 半截结果。
|
|
1677
1692
|
*/
|
|
1678
1693
|
async translateReplyBlocks(blocks) {
|
|
1679
|
-
const
|
|
1680
|
-
|
|
1681
|
-
ok: false
|
|
1682
|
-
}));
|
|
1694
|
+
const originals = () => blocks.map((translated2) => ({ translated: translated2, ok: false }));
|
|
1695
|
+
const results = originals();
|
|
1683
1696
|
const config = this.configManager.getConfig();
|
|
1684
|
-
this.
|
|
1697
|
+
const generation = this.updateGeneration(config);
|
|
1698
|
+
const isCurrent = () => generation === this.updateGeneration(this.configManager.getConfig());
|
|
1685
1699
|
if (!config.enabled) return results;
|
|
1686
1700
|
const adapter = this.adapters.get(CHANNEL_ID);
|
|
1687
1701
|
if (!adapter || !adapter.isAvailable(config)) {
|
|
@@ -1696,7 +1710,7 @@ var TranslationDispatcher = class {
|
|
|
1696
1710
|
for (let block = 0; block < blocks.length; block++) {
|
|
1697
1711
|
const raw = blocks[block] ?? "";
|
|
1698
1712
|
if (raw.trim() === "") continue;
|
|
1699
|
-
const cached = this.cache.get(raw
|
|
1713
|
+
const cached = this.cache.get(raw);
|
|
1700
1714
|
if (cached) {
|
|
1701
1715
|
results[block] = { translated: cached, ok: true };
|
|
1702
1716
|
continue;
|
|
@@ -1726,8 +1740,9 @@ var TranslationDispatcher = class {
|
|
|
1726
1740
|
const translated = /* @__PURE__ */ new Map();
|
|
1727
1741
|
for (const batch of packPieces(pieces)) {
|
|
1728
1742
|
const outcome = await this.runReplySerial(
|
|
1729
|
-
() => this.translateReplyBatch(adapter, batch, config, ledger)
|
|
1743
|
+
() => isCurrent() ? this.translateReplyBatch(adapter, batch, config, ledger, isCurrent) : void 0
|
|
1730
1744
|
);
|
|
1745
|
+
if (outcome === void 0 || !isCurrent()) return originals();
|
|
1731
1746
|
for (const [key, value] of outcome) translated.set(key, value);
|
|
1732
1747
|
}
|
|
1733
1748
|
for (const block of partsByBlock.keys()) {
|
|
@@ -1753,7 +1768,7 @@ var TranslationDispatcher = class {
|
|
|
1753
1768
|
}
|
|
1754
1769
|
const original = blocks[block];
|
|
1755
1770
|
const finalText = parts.join("");
|
|
1756
|
-
this.cache.set(original
|
|
1771
|
+
this.cache.set(original, finalText);
|
|
1757
1772
|
results[block] = { translated: finalText, ok: true };
|
|
1758
1773
|
}
|
|
1759
1774
|
return results;
|
|
@@ -1766,7 +1781,7 @@ var TranslationDispatcher = class {
|
|
|
1766
1781
|
* 每次通道失败都记进片段账本({@link FailureLedger}):批请求的失败摊到
|
|
1767
1782
|
* 批内每个片段,单发的失败只记该片段;块级细节由缺失片段的账本合成。
|
|
1768
1783
|
*/
|
|
1769
|
-
async translateReplyBatch(adapter, batch, config, ledger) {
|
|
1784
|
+
async translateReplyBatch(adapter, batch, config, ledger, isCurrent) {
|
|
1770
1785
|
const out = /* @__PURE__ */ new Map();
|
|
1771
1786
|
const stragglers = [];
|
|
1772
1787
|
try {
|
|
@@ -1793,6 +1808,7 @@ var TranslationDispatcher = class {
|
|
|
1793
1808
|
let accepted = null;
|
|
1794
1809
|
let packed = false;
|
|
1795
1810
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
1811
|
+
if (!isCurrent()) return out;
|
|
1796
1812
|
let answer2;
|
|
1797
1813
|
try {
|
|
1798
1814
|
answer2 = (await this.fetchReplyAnswers(adapter, [piece], config, packed))[0] ?? "";
|
|
@@ -2006,8 +2022,12 @@ function apply(ctx, config) {
|
|
|
2006
2022
|
createLiveConfigSource((listener) => ctx.on("loader/volatile-update", listener), config),
|
|
2007
2023
|
credentials
|
|
2008
2024
|
);
|
|
2009
|
-
const cache = new LruDiskCache(1e3, "cache.json", cacheGeneration(configManager.getConfig()
|
|
2025
|
+
const cache = new LruDiskCache(1e3, "cache.json", cacheGeneration(configManager.getConfig()));
|
|
2010
2026
|
const dispatcher = new TranslationDispatcher(configManager, cache, credentials);
|
|
2027
|
+
ctx.effect(() => dispatcher.watchConfig(), "dsh-chat-translate: cache generation");
|
|
2028
|
+
ctx.effect(() => () => {
|
|
2029
|
+
void cache.dispose();
|
|
2030
|
+
}, "dsh-chat-translate: disk cache");
|
|
2011
2031
|
ctx.inject(["settings"], (child) => {
|
|
2012
2032
|
child.effect(
|
|
2013
2033
|
() => child.settings.configure({ auto: false }, child.fiber),
|
|
@@ -2027,7 +2047,7 @@ function apply(ctx, config) {
|
|
|
2027
2047
|
(route) => connectionCtx.connection.fetch.register(route)
|
|
2028
2048
|
);
|
|
2029
2049
|
return () => {
|
|
2030
|
-
void Promise.all(disposers.map((dispose) => dispose())).
|
|
2050
|
+
void Promise.all(disposers.map((dispose) => dispose())).catch((err) => {
|
|
2031
2051
|
console.warn("[dsh-chat-translate] Dispose translation routes error:", err);
|
|
2032
2052
|
});
|
|
2033
2053
|
};
|