@ggboy123/dsh-paper-reader 1.6.0 → 1.7.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.
@@ -17,6 +17,7 @@
17
17
  import { createHash } from 'node:crypto';
18
18
  import { compactLatex } from "./formulas.js";
19
19
  import { sanitizeDetail } from "./mineru.js";
20
+ import { isOpenCodeEndpoint, openCodeSessionHeaders, openCodeSessionId } from "./opencode.js";
20
21
  /* ── 冻结参数表(§3.4;调参必须回改需求文档)────────────────────────────── */
21
22
  /** 最短可对齐归一化长度 */
22
23
  export const MIN_NORM = 4;
@@ -710,6 +711,11 @@ export async function translateSelection(endpoint, source, deps = {}) {
710
711
  const fetchImpl = (deps.fetchImpl ?? globalThis.fetch);
711
712
  const url = baseUrl.replace(/\/+$/, '') + '/chat/completions';
712
713
  const headers = { 'content-type': 'application/json', authorization: `Bearer ${apiKey}` };
714
+ if (isOpenCodeEndpoint(baseUrl)) {
715
+ // OpenCode Go 要求每次推理请求都带稳定的会话 ID:同一篇论文跨请求、跨重启都不变
716
+ const sessionId = deps.openCodeSessionId ?? await openCodeSessionId(`translate-selection:${baseUrl.replace(/\/+$/, '')}|${model}`);
717
+ Object.assign(headers, openCodeSessionHeaders(baseUrl, sessionId));
718
+ }
713
719
  const started = Date.now();
714
720
  const send = async (body) => {
715
721
  try {
package/dist/translate.js CHANGED
@@ -14,6 +14,7 @@ import { StringDecoder } from 'node:string_decoder';
14
14
  import { fileURLToPath } from 'node:url';
15
15
  import { dshHome } from "./library.js";
16
16
  import { ensureBabeldoc } from "./babeldoc-install.js";
17
+ import { isOpenCodeEndpoint, openCodeDriverEnv, openCodeSessionId } from "./opencode.js";
17
18
  export function zhPdfPath(ref) {
18
19
  return ref.pdfPath.slice(0, -extname(ref.pdfPath).length) + '-zh.pdf';
19
20
  }
@@ -249,6 +250,11 @@ async function launch(ref, dataDir, endpoint, bin) {
249
250
  busy.set(ref.pdfPath, null); // 清掉安装阶段说明,回到「翻译进行中」
250
251
  const stem = basename(ref.pdfPath, extname(ref.pdfPath));
251
252
  const dstStem = ref.pdfPath.slice(0, -extname(ref.pdfPath).length);
253
+ // OpenCode Go:整篇翻译是一个「会话」,同一篇论文(含之后的划词翻译)共用一个稳定 ID,
254
+ // 经环境变量交给驱动脚本,由它加进每次 LLM 请求的 x-opencode-session。其它端点不加任何东西。
255
+ const ocSession = isOpenCodeEndpoint(ep.baseUrl)
256
+ ? await openCodeSessionId(`paper:${ref.topic ?? ''}/${ref.name ?? stem}`)
257
+ : '';
252
258
  // 推理型模型(glm-5.x / k3 / deepseek-flash)默认每段都先跑隐藏推理,翻译慢好几倍还偶尔返回空。
253
259
  // 这三家的 OpenAI 兼容端点都接受 DeepSeek 风格 thinking 开关(babeldoc --openai-thinking 即发该字段);
254
260
  // 自定义端点不加——OpenAI 官方等严格校验参数的端点会因未知字段 400。
@@ -304,7 +310,7 @@ async function launch(ref, dataDir, endpoint, bin) {
304
310
  const child = spawnError
305
311
  ? null
306
312
  : spawn(cmd, args, {
307
- env: { ...process.env, HF_ENDPOINT: 'https://hf-mirror.com', PYTHONIOENCODING: 'utf-8' },
313
+ env: { ...process.env, HF_ENDPOINT: 'https://hf-mirror.com', PYTHONIOENCODING: 'utf-8', ...openCodeDriverEnv(ep.baseUrl, ocSession) },
308
314
  windowsHide: true,
309
315
  stdio: ['ignore', logFd, logFd],
310
316
  });
package/lib/client.js CHANGED
@@ -83,11 +83,12 @@ window.__ModuleLoader__.load({
83
83
  setEmbedTitle: '嵌入模型检索(可选)',
84
84
  setEmbedDesc: 'search_paper 的语义召回:分块文本会发送到该 OpenAI 兼容 /embeddings 端点,用向量把「关键词一个字都没命中」的语义相近片段也捞进候选(与关键词结果融合,不替代)。留空即关闭,检索行为与现在完全一致。注意:分块文本(论文正文)会发往该端点。',
85
85
  setTranslateTitle: '翻译端点',
86
- setTranslateDesc: '「生成中文版」(babeldoc)用的 OpenAI 兼容端点,三项必填。保存前会先测连接。',
86
+ setTranslateDesc: '「生成中文版」(babeldoc)与划词翻译共用的 OpenAI 兼容端点,三项必填。OpenCode Go 填 https://opencode.ai/inference/go/openai/v1(插件会自动带 x-opencode-session)。保存前会先测连接。',
87
87
  setBaseUrl: '端点 URL', setModel: '模型', setKey: 'API Key',
88
88
  setSave: '测试并保存', setSaving: '测试连接中…', setClear: '清除自填配置',
89
89
  setSaved: '已保存,立即生效', setCleared: '已清除,回落到部署方默认',
90
90
  setKeyKeep: '留空沿用已保存的 ', setOptional: '可选',
91
+ setEmbedBatchSize: '批大小(每次请求的分块数,端点限制更小时调低,如百炼部分模型 10/20)',
91
92
  setSourceFile: '当前来源:设置页', setSourceProfile: '当前来源:配置文件(部署方默认)',
92
93
  setSourceEnv: '当前来源:环境变量', setSourceNone: '未配置',
93
94
  setMineruTitle: 'MinerU 解析后端',
@@ -164,11 +165,12 @@ window.__ModuleLoader__.load({
164
165
  setEmbedTitle: 'Embedding search (optional)',
165
166
  setEmbedDesc: 'Semantic recall for search_paper: chunk texts are sent to this OpenAI-compatible /embeddings endpoint so passages that share no keywords but are semantically close still enter the candidate set (fused with keyword hits, never replacing them). Leave empty to keep it off — search behaviour stays exactly as today. Note: chunk texts (paper body) are sent to this endpoint.',
166
167
  setTranslateTitle: 'Translate endpoint',
167
- setTranslateDesc: 'OpenAI-compatible endpoint for "Generate Chinese version" (babeldoc). All three fields required. Connection is tested before saving.',
168
+ setTranslateDesc: 'OpenAI-compatible endpoint shared by "Generate Chinese version" (babeldoc) and selection translation; all three fields required. For OpenCode Go use https://opencode.ai/inference/go/openai/v1 (the plugin adds x-opencode-session automatically). Connection is tested before saving.',
168
169
  setBaseUrl: 'Endpoint URL', setModel: 'Model', setKey: 'API Key',
169
170
  setSave: 'Test & save', setSaving: 'Testing…', setClear: 'Clear my config',
170
171
  setSaved: 'Saved, effective immediately', setCleared: 'Cleared, falling back to deployment defaults',
171
172
  setKeyKeep: 'Leave empty to keep saved ', setOptional: 'optional',
173
+ setEmbedBatchSize: 'Batch size (chunks per request; lower it when the endpoint caps inputs, e.g. 10/20 on some Bailian models)',
172
174
  setSourceFile: 'Source: settings page', setSourceProfile: 'Source: config file (deployment default)',
173
175
  setSourceEnv: 'Source: environment variable', setSourceNone: 'Not configured',
174
176
  setMineruTitle: 'MinerU parser',
@@ -1405,13 +1407,19 @@ window.__ModuleLoader__.load({
1405
1407
 
1406
1408
  function EndpointCard(props) {
1407
1409
  const [cfg, setCfg] = useState(null) // GET 结果(含 apiKeyHint/source)
1408
- const [form, setForm] = useState({ baseUrl: '', model: '', apiKey: '' })
1410
+ const [form, setForm] = useState({ baseUrl: '', model: '', apiKey: '', batchSize: '' })
1409
1411
  const [busy, setBusy] = useState(false)
1410
1412
  const [msg, setMsg] = useState(null) // { ok, text }
1411
1413
  const load = useCallback(() => {
1412
1414
  api(props.path).then((d) => {
1413
1415
  setCfg(d)
1414
- setForm({ baseUrl: d.baseUrl || '', model: d.model || '', apiKey: '' })
1416
+ setForm({
1417
+ baseUrl: d.baseUrl || '',
1418
+ model: d.model || '',
1419
+ apiKey: '',
1420
+ // null = 未显式配置(回落 profile / 默认 10),输入框留空展示占位符
1421
+ ...(props.batchSize ? { batchSize: d.batchSize != null ? String(d.batchSize) : '' } : {}),
1422
+ })
1415
1423
  }).catch((e) => setMsg({ ok: false, text: String(e.message || e) }))
1416
1424
  }, [props.path])
1417
1425
  useEffect(load, [load])
@@ -1448,6 +1456,8 @@ window.__ModuleLoader__.load({
1448
1456
  field('baseUrl', t('setBaseUrl'), props.baseUrlPlaceholder),
1449
1457
  field('model', t('setModel'), props.modelPlaceholder),
1450
1458
  field('apiKey', t('setKey'), cfg?.hasApiKey ? t('setKeyKeep') + cfg.apiKeyHint : props.keyPlaceholder, 'password'),
1459
+ // 批大小仅嵌入卡片有(issue #14):预检会按这个值真打一批,端点限流当场暴露而不是首次检索才失败
1460
+ props.batchSize ? field('batchSize', t('setEmbedBatchSize'), '10') : null,
1451
1461
  h('div', { style: SET.row },
1452
1462
  h('button', { type: 'button', style: SET.btn, disabled: busy, onClick: save }, busy ? t('setSaving') : t('setSave')),
1453
1463
  cfg?.source === 'file' ? h('button', { type: 'button', style: SET.btnGhost, disabled: busy, onClick: clear }, t('setClear')) : null,
@@ -1506,6 +1516,7 @@ window.__ModuleLoader__.load({
1506
1516
  h(EndpointCard, {
1507
1517
  path: '/embed/config', title: t('setEmbedTitle'), desc: t('setEmbedDesc'), optional: true,
1508
1518
  baseUrlPlaceholder: 'https://api.openai.com/v1', modelPlaceholder: 'text-embedding-3-small', keyPlaceholder: 'sk-...',
1519
+ batchSize: true,
1509
1520
  }),
1510
1521
  h(EndpointCard, {
1511
1522
  path: '/translate/config', title: t('setTranslateTitle'), desc: t('setTranslateDesc'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ggboy123/dsh-paper-reader",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "DeepSeek Harness 插件:论文伴读工作台 — PDF 转录 / 检索 / 原生对话伴读 + 内置阅读器页面。",
5
5
  "license": "MIT",
6
6
  "type": "module",