@lynn123411/dsh-chat-translate 3.7.0 → 3.8.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/DETAILS.md +37 -7
- package/README.md +5 -4
- package/assets/icon.svg +5 -5
- package/lib/client.js +11 -11
- package/lib/client.js.map +2 -2
- package/lib/index.js +65 -21
- package/lib/index.js.map +3 -3
- package/lib/types/client/locales.d.ts +1 -1
- package/lib/types/prompt.d.ts +44 -15
- package/lib/types/server/adapters/openai.d.ts +18 -9
- package/lib/types/server/dispatcher.d.ts +3 -0
- package/lib/types/server/pipeline/segments.d.ts +10 -2
- package/lib/types/server/prompt-revision.d.ts +5 -5
- package/lib/types/server/sampling.d.ts +12 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -995,25 +995,44 @@ import { createHash } from "node:crypto";
|
|
|
995
995
|
|
|
996
996
|
// src/prompt.ts
|
|
997
997
|
var DEFAULT_PROMPT = [
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
].join("\n
|
|
1007
|
-
var
|
|
998
|
+
`\u4F8B\uFF1A`,
|
|
999
|
+
`\u8F93\u5165\uFF1AThe build passed.`,
|
|
1000
|
+
`\u8F93\u51FA\uFF1A\u6784\u5EFA\u901A\u8FC7\u3002`,
|
|
1001
|
+
`\u8F93\u5165\uFF1A\u8FD9\u4E00\u7248\u6539\u597D\u4E86\u3002`,
|
|
1002
|
+
`\u8F93\u51FA\uFF1A\u8FD9\u4E00\u7248\u6539\u597D\u4E86\u3002`,
|
|
1003
|
+
``,
|
|
1004
|
+
`\u4E0B\u9762\u8FD9\u6BB5\u6587\u5B57\uFF1A\u5916\u6587\u8BD1\u6210\u7B80\u4F53\u4E2D\u6587\uFF1B\u4E2D\u6587\u539F\u6837\u6284\u5199\uFF0C\u53EA\u6709\u53E5\u5B50\u8BFB\u4E0D\u901A\u65F6\u624D\u6539\u90A3\u51E0\u53E5\u3002`,
|
|
1005
|
+
`\u4FDD\u6301\u539F\u6709\u7684 Markdown \u8BB0\u53F7\u4E0D\u53D8\u3002\u53EA\u8F93\u51FA\u7ED3\u679C\u3002`
|
|
1006
|
+
].join("\n");
|
|
1007
|
+
var PARAGRAPH_KEEP_CLAUSE = `\u539F\u6587\u5206\u51E0\u6BB5\u7684\uFF0C\u8BD1\u6587\u4E5F\u5206\u51E0\u6BB5\uFF0C\u4E00\u6BB5\u90FD\u4E0D\u8981\u5C11\u3002`;
|
|
1008
|
+
var BATCH_PROMPT_CLAUSE = `\u4E0B\u9762\u6BCF\u6BB5\u90FD\u4EE5\u5355\u72EC\u4E00\u884C\u6807\u8BB0\u5F00\u5934\uFF0C\u6807\u8BB0\u7531\u4E24\u4E2A\u62EC\u53F7\u5939\u7740\u4E00\u4E2A\u7F16\u53F7\u3002\u6BCF\u6BB5\u5148\u539F\u6837\u8F93\u51FA\u90A3\u4E00\u884C\u6807\u8BB0\uFF0C\u518D\u5728\u4E0B\u4E00\u884C\u5199\u8FD9\u6BB5\u7684\u8BD1\u6587\uFF0C\u987A\u5E8F\u4E0D\u53D8\uFF0C\u6BB5\u4E0E\u6BB5\u4E0D\u8981\u5408\u5E76\u3002`;
|
|
1008
1009
|
function resolvePrompt(prompt) {
|
|
1009
1010
|
const trimmed = (prompt ?? "").trim();
|
|
1010
1011
|
return trimmed === "" ? DEFAULT_PROMPT : trimmed;
|
|
1011
1012
|
}
|
|
1013
|
+
function paragraphCount(text) {
|
|
1014
|
+
return text.split(/\n\s*\n/).filter((part) => part.trim() !== "").length;
|
|
1015
|
+
}
|
|
1016
|
+
function promptFor(text, prompt) {
|
|
1017
|
+
const base = resolvePrompt(prompt);
|
|
1018
|
+
if (paragraphCount(text) < 2) return base;
|
|
1019
|
+
return `${base}
|
|
1020
|
+
${PARAGRAPH_KEEP_CLAUSE}`;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
// src/server/sampling.ts
|
|
1024
|
+
var SAMPLING_TEMPERATURE = 0.7;
|
|
1025
|
+
var SAMPLING_TOP_P = 0.6;
|
|
1012
1026
|
|
|
1013
1027
|
// src/server/prompt-revision.ts
|
|
1014
|
-
var PROMPT_REVISION = "
|
|
1028
|
+
var PROMPT_REVISION = "r7";
|
|
1015
1029
|
function cacheGeneration(prompt) {
|
|
1016
|
-
const
|
|
1030
|
+
const source = [
|
|
1031
|
+
resolvePrompt(prompt),
|
|
1032
|
+
`temperature=${SAMPLING_TEMPERATURE}`,
|
|
1033
|
+
`top_p=${SAMPLING_TOP_P}`
|
|
1034
|
+
].join("\0");
|
|
1035
|
+
const digest = createHash("sha256").update(source, "utf8").digest("hex").slice(0, 12);
|
|
1017
1036
|
return `${PROMPT_REVISION}:${digest}`;
|
|
1018
1037
|
}
|
|
1019
1038
|
|
|
@@ -1177,12 +1196,17 @@ var TranslationFailure = class extends Error {
|
|
|
1177
1196
|
};
|
|
1178
1197
|
|
|
1179
1198
|
// src/server/adapters/openai.ts
|
|
1180
|
-
function
|
|
1181
|
-
const base =
|
|
1199
|
+
function buildRequestPrefix(mode, text, prompt) {
|
|
1200
|
+
const base = promptFor(text, prompt);
|
|
1182
1201
|
return mode === "plain" ? base : `${base}
|
|
1183
1202
|
|
|
1184
1203
|
${BATCH_PROMPT_CLAUSE}`;
|
|
1185
1204
|
}
|
|
1205
|
+
function buildUserMessage(mode, text, prompt) {
|
|
1206
|
+
return `${buildRequestPrefix(mode, text, prompt)}
|
|
1207
|
+
|
|
1208
|
+
${text}`;
|
|
1209
|
+
}
|
|
1186
1210
|
var OpenAiCompatibleAdapter = class {
|
|
1187
1211
|
id = "openai";
|
|
1188
1212
|
name = "OpenAI \u517C\u5BB9 (Chat Completions)";
|
|
@@ -1217,11 +1241,14 @@ var OpenAiCompatibleAdapter = class {
|
|
|
1217
1241
|
},
|
|
1218
1242
|
body: JSON.stringify({
|
|
1219
1243
|
model,
|
|
1220
|
-
temperature:
|
|
1244
|
+
temperature: SAMPLING_TEMPERATURE,
|
|
1245
|
+
top_p: SAMPLING_TOP_P,
|
|
1221
1246
|
...options?.maxTokens === void 0 ? {} : { max_tokens: options.maxTokens },
|
|
1222
1247
|
messages: [
|
|
1223
|
-
{
|
|
1224
|
-
|
|
1248
|
+
{
|
|
1249
|
+
role: "user",
|
|
1250
|
+
content: buildUserMessage(options?.mode ?? "plain", text, config.prompt)
|
|
1251
|
+
}
|
|
1225
1252
|
]
|
|
1226
1253
|
}),
|
|
1227
1254
|
signal
|
|
@@ -1485,7 +1512,12 @@ function repairShape(original, translated) {
|
|
|
1485
1512
|
const wantFilled = wantLines.filter((line) => !isBlankLine(line));
|
|
1486
1513
|
const gotFilled = gotLines.filter((line) => !isBlankLine(line));
|
|
1487
1514
|
if (wantFilled.length !== gotFilled.length) {
|
|
1488
|
-
|
|
1515
|
+
const short = wantFilled.length > gotFilled.length;
|
|
1516
|
+
return {
|
|
1517
|
+
text: null,
|
|
1518
|
+
error: short ? `content came back short (${wantFilled.length} lines -> ${gotFilled.length})` : `line count changed (${wantLines.length} -> ${gotLines.length})`,
|
|
1519
|
+
missingContent: short
|
|
1520
|
+
};
|
|
1489
1521
|
}
|
|
1490
1522
|
for (let index = 0; index < wantFilled.length; index++) {
|
|
1491
1523
|
const sw = lineSignature(wantFilled[index] ?? "");
|
|
@@ -1759,10 +1791,11 @@ var TranslationDispatcher = class {
|
|
|
1759
1791
|
const key = replyPieceKey(piece);
|
|
1760
1792
|
if (out.has(key)) continue;
|
|
1761
1793
|
let accepted = null;
|
|
1794
|
+
let packed = false;
|
|
1762
1795
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
1763
1796
|
let answer2;
|
|
1764
1797
|
try {
|
|
1765
|
-
answer2 = (await this.fetchReplyAnswers(adapter, [piece], config))[0] ?? "";
|
|
1798
|
+
answer2 = (await this.fetchReplyAnswers(adapter, [piece], config, packed))[0] ?? "";
|
|
1766
1799
|
} catch (err) {
|
|
1767
1800
|
if (accepted === null) {
|
|
1768
1801
|
recordLedgerFailure(ledger, [key], classifyError(err));
|
|
@@ -1774,6 +1807,7 @@ var TranslationDispatcher = class {
|
|
|
1774
1807
|
}
|
|
1775
1808
|
accepted = { answer: answer2, repair: repairShape(piece.text, answer2) };
|
|
1776
1809
|
if (accepted.repair.text !== null) break;
|
|
1810
|
+
packed = accepted.repair.missingContent === true;
|
|
1777
1811
|
}
|
|
1778
1812
|
if (accepted === null) continue;
|
|
1779
1813
|
const { answer, repair } = accepted;
|
|
@@ -1781,6 +1815,13 @@ var TranslationDispatcher = class {
|
|
|
1781
1815
|
out.set(key, this.assemble(piece, repair.text, true));
|
|
1782
1816
|
continue;
|
|
1783
1817
|
}
|
|
1818
|
+
if (repair.missingContent) {
|
|
1819
|
+
recordLedgerFailure(ledger, [key], { detail: repair.error ?? "", reason: "channel" });
|
|
1820
|
+
console.warn(
|
|
1821
|
+
`[dsh-chat-translate] reply block ${piece.block} #${piece.index} came back short, keeping the original: ${repair.error}`
|
|
1822
|
+
);
|
|
1823
|
+
continue;
|
|
1824
|
+
}
|
|
1784
1825
|
console.warn(
|
|
1785
1826
|
`[dsh-chat-translate] reply block ${piece.block} #${piece.index} shape kept as the model wrote it: ${repair.error}`
|
|
1786
1827
|
);
|
|
@@ -1795,14 +1836,17 @@ var TranslationDispatcher = class {
|
|
|
1795
1836
|
/**
|
|
1796
1837
|
* 发出一次正文请求,返回逐片段的裸答案(打包标记已剥除、首尾已 trim)。
|
|
1797
1838
|
* 空返回与打包拆不回都按通道失败抛出——形状对不对在这里不算败因。
|
|
1839
|
+
*
|
|
1840
|
+
* @param packed - 单个片段也走带标记的打包形状。缺段之后的重问用它:标记把
|
|
1841
|
+
* 这一段框成「一整段待译内容」,模型不再把开头那段当整段交差。
|
|
1798
1842
|
*/
|
|
1799
|
-
async fetchReplyAnswers(adapter, batch, config) {
|
|
1843
|
+
async fetchReplyAnswers(adapter, batch, config, packed = false) {
|
|
1800
1844
|
const timeout = config.aiTimeoutMs || 6e5;
|
|
1801
1845
|
const abortCtrl = new AbortController();
|
|
1802
1846
|
const timer = setTimeout(() => abortCtrl.abort(), timeout);
|
|
1803
1847
|
let answers;
|
|
1804
1848
|
try {
|
|
1805
|
-
if (batch.length === 1) {
|
|
1849
|
+
if (batch.length === 1 && !packed) {
|
|
1806
1850
|
answers = [
|
|
1807
1851
|
await adapter.translate(batch[0].text, abortCtrl.signal, config, {
|
|
1808
1852
|
maxTokens: REPLY_MAX_OUTPUT_TOKENS,
|