@lynn123411/dsh-chat-translate 3.8.0 → 3.9.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/lib/index.js CHANGED
@@ -1004,18 +1004,28 @@ var DEFAULT_PROMPT = [
1004
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
1005
  `\u4FDD\u6301\u539F\u6709\u7684 Markdown \u8BB0\u53F7\u4E0D\u53D8\u3002\u53EA\u8F93\u51FA\u7ED3\u679C\u3002`
1006
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`;
1007
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
+ }
1012
1022
 
1013
1023
  // src/server/sampling.ts
1014
1024
  var SAMPLING_TEMPERATURE = 0.7;
1015
1025
  var SAMPLING_TOP_P = 0.6;
1016
1026
 
1017
1027
  // src/server/prompt-revision.ts
1018
- var PROMPT_REVISION = "r6";
1028
+ var PROMPT_REVISION = "r7";
1019
1029
  function cacheGeneration(prompt) {
1020
1030
  const source = [
1021
1031
  resolvePrompt(prompt),
@@ -1186,14 +1196,14 @@ var TranslationFailure = class extends Error {
1186
1196
  };
1187
1197
 
1188
1198
  // src/server/adapters/openai.ts
1189
- function buildRequestPrefix(mode, prompt) {
1190
- const base = resolvePrompt(prompt);
1199
+ function buildRequestPrefix(mode, text, prompt) {
1200
+ const base = promptFor(text, prompt);
1191
1201
  return mode === "plain" ? base : `${base}
1192
1202
 
1193
1203
  ${BATCH_PROMPT_CLAUSE}`;
1194
1204
  }
1195
1205
  function buildUserMessage(mode, text, prompt) {
1196
- return `${buildRequestPrefix(mode, prompt)}
1206
+ return `${buildRequestPrefix(mode, text, prompt)}
1197
1207
 
1198
1208
  ${text}`;
1199
1209
  }
@@ -1502,7 +1512,12 @@ function repairShape(original, translated) {
1502
1512
  const wantFilled = wantLines.filter((line) => !isBlankLine(line));
1503
1513
  const gotFilled = gotLines.filter((line) => !isBlankLine(line));
1504
1514
  if (wantFilled.length !== gotFilled.length) {
1505
- return { text: null, error: `line count changed (${wantLines.length} -> ${gotLines.length})` };
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
+ };
1506
1521
  }
1507
1522
  for (let index = 0; index < wantFilled.length; index++) {
1508
1523
  const sw = lineSignature(wantFilled[index] ?? "");
@@ -1776,10 +1791,11 @@ var TranslationDispatcher = class {
1776
1791
  const key = replyPieceKey(piece);
1777
1792
  if (out.has(key)) continue;
1778
1793
  let accepted = null;
1794
+ let packed = false;
1779
1795
  for (let attempt = 0; attempt < 2; attempt++) {
1780
1796
  let answer2;
1781
1797
  try {
1782
- answer2 = (await this.fetchReplyAnswers(adapter, [piece], config))[0] ?? "";
1798
+ answer2 = (await this.fetchReplyAnswers(adapter, [piece], config, packed))[0] ?? "";
1783
1799
  } catch (err) {
1784
1800
  if (accepted === null) {
1785
1801
  recordLedgerFailure(ledger, [key], classifyError(err));
@@ -1791,6 +1807,7 @@ var TranslationDispatcher = class {
1791
1807
  }
1792
1808
  accepted = { answer: answer2, repair: repairShape(piece.text, answer2) };
1793
1809
  if (accepted.repair.text !== null) break;
1810
+ packed = accepted.repair.missingContent === true;
1794
1811
  }
1795
1812
  if (accepted === null) continue;
1796
1813
  const { answer, repair } = accepted;
@@ -1798,6 +1815,13 @@ var TranslationDispatcher = class {
1798
1815
  out.set(key, this.assemble(piece, repair.text, true));
1799
1816
  continue;
1800
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
+ }
1801
1825
  console.warn(
1802
1826
  `[dsh-chat-translate] reply block ${piece.block} #${piece.index} shape kept as the model wrote it: ${repair.error}`
1803
1827
  );
@@ -1812,14 +1836,17 @@ var TranslationDispatcher = class {
1812
1836
  /**
1813
1837
  * 发出一次正文请求,返回逐片段的裸答案(打包标记已剥除、首尾已 trim)。
1814
1838
  * 空返回与打包拆不回都按通道失败抛出——形状对不对在这里不算败因。
1839
+ *
1840
+ * @param packed - 单个片段也走带标记的打包形状。缺段之后的重问用它:标记把
1841
+ * 这一段框成「一整段待译内容」,模型不再把开头那段当整段交差。
1815
1842
  */
1816
- async fetchReplyAnswers(adapter, batch, config) {
1843
+ async fetchReplyAnswers(adapter, batch, config, packed = false) {
1817
1844
  const timeout = config.aiTimeoutMs || 6e5;
1818
1845
  const abortCtrl = new AbortController();
1819
1846
  const timer = setTimeout(() => abortCtrl.abort(), timeout);
1820
1847
  let answers;
1821
1848
  try {
1822
- if (batch.length === 1) {
1849
+ if (batch.length === 1 && !packed) {
1823
1850
  answers = [
1824
1851
  await adapter.translate(batch[0].text, abortCtrl.signal, config, {
1825
1852
  maxTokens: REPLY_MAX_OUTPUT_TOKENS,