@lynn123411/dsh-chat-translate 3.4.0 → 3.5.3

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
@@ -1,4 +1,4 @@
1
- // node_modules/.pnpm/@deepseek-ai+cosmokit@1.8.6-alpha.1/node_modules/@deepseek-ai/cosmokit/lib/index.js
1
+ // node_modules/.pnpm/@deepseek-ai+cosmokit@1.8.6-alpha.2/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.18.5-alpha.1/node_modules/@deepseek-ai/schemastery/lib/index.mjs
226
+ // node_modules/.pnpm/@deepseek-ai+schemastery@3.18.5-alpha.2/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;
@@ -904,6 +904,23 @@ var ConfigManager = class {
904
904
  config.baseUrl.trim() && config.model.trim() && this.credentials.getApiKey()
905
905
  );
906
906
  }
907
+ /**
908
+ * Name every missing piece of the AI channel, as the reply path reports a
909
+ * setup failure. This travels to the hover bubble and the screen reader, so
910
+ * it names fields rather than values and never echoes the key.
911
+ *
912
+ * @param config - the resolved config to inspect; defaults to the current one.
913
+ * @returns the unset pieces by name, or a generic phrase when all are set
914
+ * (the adapter may still refuse the configuration).
915
+ */
916
+ describeMissing(config = this.getConfig()) {
917
+ const missing = [];
918
+ if (!config.baseUrl.trim()) missing.push("Base URL");
919
+ if (!config.model.trim()) missing.push("model");
920
+ if (!this.credentials.getApiKey()) missing.push("API key");
921
+ if (missing.length === 0) return "the translation channel is not configured";
922
+ return `not configured yet: ${missing.join(", ")}`;
923
+ }
907
924
  onConfigChange(listener) {
908
925
  return this.source.watch(listener);
909
926
  }
@@ -977,7 +994,7 @@ var CredentialsReader = class {
977
994
  import * as fs from "node:fs/promises";
978
995
  import * as path from "node:path";
979
996
 
980
- // node_modules/.pnpm/@deepseek-ai+dsh-home-paths@0.2.1-alpha.2_@deepseek-ai+cordis@4.0.5-alpha.1/node_modules/@deepseek-ai/dsh-home-paths/lib/index.js
997
+ // node_modules/.pnpm/@deepseek-ai+dsh-home-paths@0.2.1-alpha.3_@deepseek-ai+cordis@4.0.5-alpha.2/node_modules/@deepseek-ai/dsh-home-paths/lib/index.js
981
998
  import { homedir } from "node:os";
982
999
  import { basename, dirname, join, resolve as resolve2 } from "node:path";
983
1000
  var DSH_HOME_DIR_NAME = ".dsh";
@@ -1139,6 +1156,20 @@ function describeError(err) {
1139
1156
  return parts.join(" \u2190 ");
1140
1157
  }
1141
1158
 
1159
+ // src/server/types.ts
1160
+ var TranslationFailure = class extends Error {
1161
+ reason;
1162
+ /**
1163
+ * @param reason - failure class the client's label branches on.
1164
+ * @param message - technical one-liner safe to show in a hover bubble.
1165
+ */
1166
+ constructor(reason, message) {
1167
+ super(message);
1168
+ this.name = "TranslationFailure";
1169
+ this.reason = reason;
1170
+ }
1171
+ };
1172
+
1142
1173
  // src/server/adapters/openai.ts
1143
1174
  var TARGET_LANGUAGE = "Simplified Chinese";
1144
1175
  function buildSystemPrompt(mode) {
@@ -1159,12 +1190,18 @@ var OpenAiCompatibleAdapter = class {
1159
1190
  async translate(text, signal, config, options) {
1160
1191
  const apiKey = this.credentials.getApiKey();
1161
1192
  if (!apiKey) {
1162
- throw new Error(`TRANSLATE_API_KEY is not configured in ~/.dsh/.credentials.yaml`);
1193
+ throw new TranslationFailure(
1194
+ "not-configured",
1195
+ `${TRANSLATE_API_KEY_REF} is not configured in ~/.dsh/.credentials.yaml`
1196
+ );
1163
1197
  }
1164
1198
  const baseUrl = (config.baseUrl || "").trim().replace(/\/+$/, "");
1165
1199
  const model = (config.model || "").trim();
1166
1200
  if (!baseUrl || !model) {
1167
- throw new Error("OpenAI channel: baseUrl or model is not configured");
1201
+ throw new TranslationFailure(
1202
+ "not-configured",
1203
+ `OpenAI channel: ${!baseUrl ? "baseUrl" : "model"} is not configured`
1204
+ );
1168
1205
  }
1169
1206
  const response = await fetch(`${baseUrl}/chat/completions`, {
1170
1207
  method: "POST",
@@ -1190,7 +1227,10 @@ var OpenAiCompatibleAdapter = class {
1190
1227
  detail = errBody?.error?.message || errBody?.message || "";
1191
1228
  } catch {
1192
1229
  }
1193
- throw new Error(`OpenAI-compatible API responded with ${response.status}${detail ? `: ${detail}` : ""}`);
1230
+ throw new TranslationFailure(
1231
+ "channel",
1232
+ `OpenAI-compatible API responded with ${response.status}${detail ? `: ${detail}` : ""}`
1233
+ );
1194
1234
  }
1195
1235
  const data = await response.json();
1196
1236
  const content = data?.choices?.[0]?.message?.content;
@@ -1538,17 +1578,21 @@ var CHANNEL_ID = "openai";
1538
1578
  function replyPieceKey(piece) {
1539
1579
  return `${piece.block}:${piece.index}`;
1540
1580
  }
1541
- function recordLedgerFailure(ledger, keys, detail) {
1581
+ function classifyError(err) {
1582
+ if (err instanceof TranslationFailure) return { detail: err.message, reason: err.reason };
1583
+ return { detail: describeError(err), reason: "channel" };
1584
+ }
1585
+ function recordLedgerFailure(ledger, keys, failure) {
1542
1586
  for (const key of keys) {
1543
- if (!ledger.has(key)) ledger.set(key, detail);
1587
+ if (!ledger.has(key)) ledger.set(key, failure);
1544
1588
  }
1545
1589
  }
1546
- function blockFailureDetail(ledger, block, missing) {
1590
+ function blockFailure(ledger, block, missing) {
1547
1591
  for (const index of missing) {
1548
- const detail = ledger.get(replyPieceKey({ block, index }));
1549
- if (detail !== void 0) return detail;
1592
+ const failure = ledger.get(replyPieceKey({ block, index }));
1593
+ if (failure !== void 0) return failure;
1550
1594
  }
1551
- return "no translation came back";
1595
+ return { detail: "no translation came back", reason: "channel" };
1552
1596
  }
1553
1597
  function stripEdges(chunk) {
1554
1598
  const head = /^[ \t\r\n]*/.exec(chunk)[0];
@@ -1582,12 +1626,12 @@ var TranslationDispatcher = class {
1582
1626
  *
1583
1627
  * **形状问题不否决内容**:修不好的片段自动重掷一次;再修不好就照收模型的
1584
1628
  * 译文(链接数不等时不回填,其余原样)——原文一键可回,可读的译文优先于
1585
- * 形状完整。失败只剩通道伤一种:超时、断流、空返回,块保持原文并带一句
1586
- * detail 供悬停报因,救活由用户的点击发起。
1629
+ * 形状完整。失败只有两类:通道伤(超时、断流、空返回)与设置没配好;两者
1630
+ * 都让块保持原文并带上类别与一句 detail,救活由用户的点击发起。
1587
1631
  *
1588
1632
  * 调用方传进来的每个块要么整块译出、要么整块保持原文:任一片段缺失都让该块
1589
1633
  * 作废,避免半中半英的段落;失败块带 `detail` 一句技术细节。每行只跑首跑
1590
- * 一次,通道恢复后由点击救活。
1634
+ * 一次(新的写定块由调用方逐次递进来,本方法对它们各自独立决定成败)。
1591
1635
  *
1592
1636
  * 客户端按与宿主同源的 4096 估算 token 切批,所以一个 markdown 块可能跨多次
1593
1637
  * 调用;每次调用都独立决定成败,不会出现「前一段已挂译文、后一段失败」的
@@ -1603,7 +1647,13 @@ var TranslationDispatcher = class {
1603
1647
  const config = this.configManager.getConfig();
1604
1648
  if (!config.enabled) return results;
1605
1649
  const adapter = this.adapters.get(CHANNEL_ID);
1606
- if (!adapter || !adapter.isAvailable(config)) return results;
1650
+ if (!adapter || !adapter.isAvailable(config)) {
1651
+ return results.map((result) => ({
1652
+ ...result,
1653
+ reason: "not-configured",
1654
+ detail: this.configManager.describeMissing(config)
1655
+ }));
1656
+ }
1607
1657
  return this.serveReplyBlocks(blocks, results, adapter, config);
1608
1658
  }
1609
1659
  /** 通道可用之后的正文执行段:切段、打包、请求、拼装。 */
@@ -1663,7 +1713,9 @@ var TranslationDispatcher = class {
1663
1713
  parts.push(value);
1664
1714
  }
1665
1715
  if (missing.length > 0) {
1666
- results[block].detail = blockFailureDetail(ledger, block, missing);
1716
+ const failure = blockFailure(ledger, block, missing);
1717
+ results[block].detail = failure.detail;
1718
+ results[block].reason = failure.reason;
1667
1719
  continue;
1668
1720
  }
1669
1721
  const original = blocks[block];
@@ -1701,7 +1753,7 @@ var TranslationDispatcher = class {
1701
1753
  }
1702
1754
  });
1703
1755
  } catch (err) {
1704
- recordLedgerFailure(ledger, batch.map((piece) => replyPieceKey(piece)), describeError(err));
1756
+ recordLedgerFailure(ledger, batch.map((piece) => replyPieceKey(piece)), classifyError(err));
1705
1757
  console.warn(
1706
1758
  `[dsh-chat-translate] reply batch of ${batch.length} failed, retrying per block: ${describeError(err)}`
1707
1759
  );
@@ -1716,7 +1768,7 @@ var TranslationDispatcher = class {
1716
1768
  answer = (await this.fetchReplyAnswers(adapter, [piece], config))[0] ?? "";
1717
1769
  } catch (err) {
1718
1770
  if (answer === null) {
1719
- recordLedgerFailure(ledger, [key], describeError(err));
1771
+ recordLedgerFailure(ledger, [key], classifyError(err));
1720
1772
  console.warn(
1721
1773
  `[dsh-chat-translate] reply block ${piece.block} #${piece.index} failed, keeping the original: ${describeError(err)}`
1722
1774
  );