dsh-podwave 0.11.27 → 0.11.29

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/src/translate.js CHANGED
@@ -18,12 +18,45 @@ import { artifactByDir } from './catalog.js'
18
18
  const PYTHON = venvPython()
19
19
  const SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'scripts', 'translate_local.py')
20
20
 
21
+ // API 地址/密钥/对话模型统一放在 config.api,转写和翻译共用,用户只填一次。
22
+ // 这里把 api 段叠加到 translate 段上,下游代码照旧读 config.apiBase / apiModel / apiKey。
21
23
  export function loadConfig() {
22
- return getSection('translate')
24
+ const translate = getSection('translate') || {}
25
+ const api = getSection('api') || {}
26
+ return {
27
+ ...translate,
28
+ apiBase: api.base || translate.apiBase || 'https://api.deepseek.com',
29
+ apiModel: api.model || translate.apiModel || 'deepseek-chat',
30
+ apiKey: api.key || translate.apiKey || '',
31
+ }
23
32
  }
24
33
 
25
34
  export function saveConfig(patch) {
26
- return updateSection('translate', patch)
35
+ const rest = { ...patch }
36
+ const api = {}
37
+ if (typeof rest.apiBase === 'string') {
38
+ api.base = rest.apiBase.trim() || 'https://api.deepseek.com'
39
+ delete rest.apiBase
40
+ }
41
+ if (typeof rest.apiModel === 'string') {
42
+ api.model = rest.apiModel.trim() || 'deepseek-chat'
43
+ delete rest.apiModel
44
+ }
45
+ if (typeof rest.apiKey === 'string') {
46
+ api.key = rest.apiKey.trim()
47
+ delete rest.apiKey
48
+ }
49
+ if (Object.keys(api).length) {
50
+ updateSection('api', api)
51
+ // 老版本插件仍从 translate 段读这三项,顺手镜像一份,降级回去也不会丢 key。
52
+ const mirror = {}
53
+ if (api.base !== undefined) mirror.apiBase = api.base
54
+ if (api.model !== undefined) mirror.apiModel = api.model
55
+ if (api.key !== undefined) mirror.apiKey = api.key
56
+ updateSection('translate', mirror)
57
+ }
58
+ if (Object.keys(rest).length) updateSection('translate', rest)
59
+ return loadConfig()
27
60
  }
28
61
 
29
62
  export function currentLocalModel() {
@@ -52,17 +85,55 @@ export function publicConfig() {
52
85
  }
53
86
  }
54
87
 
55
- function cjkRatio(text) {
56
- const chars = Array.from(String(text || ''))
57
- if (!chars.length) return 0
58
- const cjk = chars.filter((char) => /[\u3040-\u30ff\u3400-\u9fff\uac00-\ud7af]/.test(char)).length
59
- return cjk / chars.length
88
+ const KANA_RE = /[\u3040-\u30ff\u31f0-\u31ff\uff66-\uff9f]/
89
+ const HANGUL_RE = /[\u1100-\u11ff\u3130-\u318f\uac00-\ud7af]/
90
+ const HAN_RE = /[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/
91
+ // 拉丁字母 + 常见带音标的拉丁字母(法/德/西/葡…)+ 希腊/西里尔,都算「非汉字」。
92
+ const LATIN_RE = /[A-Za-z\u00c0-\u024f\u0370-\u03ff\u0400-\u04ff]/
93
+
94
+ export function scriptCounts(text) {
95
+ const counts = { han: 0, kana: 0, hangul: 0, latin: 0 }
96
+ for (const char of Array.from(String(text || ''))) {
97
+ if (KANA_RE.test(char)) counts.kana += 1
98
+ else if (HANGUL_RE.test(char)) counts.hangul += 1
99
+ else if (HAN_RE.test(char)) counts.han += 1
100
+ else if (LATIN_RE.test(char)) counts.latin += 1
101
+ }
102
+ return counts
103
+ }
104
+
105
+ function looksChinese(text) {
106
+ const { han, kana, hangul, latin } = scriptCounts(text)
107
+ if (!han || kana || hangul) return false
108
+ return han >= latin
60
109
  }
61
110
 
62
- // 只翻与目标语言不同的行:目标中文翻英文行,目标英文翻中文行。
63
- function needsTranslation(line, target) {
64
- const ratio = cjkRatio(line.text)
65
- return target === 'zh' ? ratio < 0.35 : ratio > 0.15
111
+ function looksEnglish(text) {
112
+ const { han, kana, hangul, latin } = scriptCounts(text)
113
+ if (!latin) return false
114
+ return latin >= han + kana + hangul
115
+ }
116
+
117
+ /**
118
+ * 只翻与目标语言不同的行。
119
+ *
120
+ * 以前按「CJK 字符占比」判断,假名和谚文都被算进 CJK:日语行占比接近 1,
121
+ * 就被当成「已经是中文 → 不用翻」,日语/韩语视频一行译文都生不出来。
122
+ * 现在按文字体系判断:有假名或谚文一定不是中文,纯汉字才算中文。
123
+ */
124
+ export function needsTranslation(line, target) {
125
+ const text = String(line?.text || '')
126
+ const { han, kana, hangul, latin } = scriptCounts(text)
127
+ if (!han && !kana && !hangul && !latin) return false // 纯数字/标点不用翻
128
+ if (target === 'zh') {
129
+ if (kana || hangul) return true
130
+ return !looksChinese(text)
131
+ }
132
+ if (target === 'en') {
133
+ if (han || kana || hangul) return true
134
+ return !looksEnglish(text)
135
+ }
136
+ return true
66
137
  }
67
138
 
68
139
  export function parseNumbered(text, count) {
@@ -113,6 +184,46 @@ async function fetchWithRetry(url, init, { attempts = 3, timeoutMs = 120000 } =
113
184
  throw lastError
114
185
  }
115
186
 
187
+ async function requestApiBatch(batch, system, config) {
188
+ const content = batch.map((line, index) => `${index + 1}. ${line.text}`).join('\n')
189
+ const response = await fetchWithRetry(`${config.apiBase.replace(/\/+$/, '')}/chat/completions`, {
190
+ method: 'POST',
191
+ headers: {
192
+ 'content-type': 'application/json',
193
+ authorization: `Bearer ${config.apiKey}`,
194
+ },
195
+ body: JSON.stringify({
196
+ model: config.apiModel,
197
+ temperature: 0.2,
198
+ messages: [
199
+ { role: 'system', content: system },
200
+ { role: 'user', content },
201
+ ],
202
+ }),
203
+ })
204
+ if (!response.ok) {
205
+ const detail = await response.text().catch(() => '')
206
+ throw new Error(`翻译 API ${response.status}:${detail.slice(0, 200)}`)
207
+ }
208
+ const data = await response.json().catch(() => null)
209
+ if (!data || !Array.isArray(data.choices) || typeof data.choices[0]?.message?.content !== 'string') {
210
+ throw new Error(data?.error?.message ? `翻译 API:${String(data.error.message).slice(0, 200)}` : '翻译 API 返回格式不正确:没有字幕内容')
211
+ }
212
+ const parsed = parseNumbered(data.choices[0].message.content, batch.length)
213
+ if (parsed.size === batch.length) return parsed
214
+ // 行数对不上说明模型把几行并成一行(短行尤其常见),编号会整体后移。
215
+ // 降批重试,直到一行对一行;单行不可能再错位。
216
+ if (batch.length > 1) {
217
+ const mid = Math.ceil(batch.length / 2)
218
+ const merged = new Map()
219
+ for (const [index, text] of await requestApiBatch(batch.slice(0, mid), system, config)) merged.set(index, text)
220
+ for (const [index, text] of await requestApiBatch(batch.slice(mid), system, config)) merged.set(index + mid, text)
221
+ return merged
222
+ }
223
+ const single = String(parsed.get(1) || '').trim()
224
+ return single ? new Map([[1, single]]) : new Map()
225
+ }
226
+
116
227
  export async function translateWithApi(items, target, config, { onBatch, shouldContinue = () => true } = {}) {
117
228
  const language = target === 'zh' ? 'Simplified Chinese' : 'English'
118
229
  const system = `You are a professional subtitle translator. Translate each numbered line into ${language}. Preserve proper names, numbers and terminology. Output only numbered lines in the format "1. translation".`
@@ -122,31 +233,7 @@ export async function translateWithApi(items, target, config, { onBatch, shouldC
122
233
  for (let start = 0; start < items.length; start += batchSize) {
123
234
  if (!shouldContinue()) throw new Error('翻译已取消')
124
235
  const batch = items.slice(start, start + batchSize)
125
- const content = batch.map((line, index) => `${index + 1}. ${line.text}`).join('\n')
126
- const response = await fetchWithRetry(`${config.apiBase.replace(/\/+$/, '')}/chat/completions`, {
127
- method: 'POST',
128
- headers: {
129
- 'content-type': 'application/json',
130
- authorization: `Bearer ${config.apiKey}`,
131
- },
132
- body: JSON.stringify({
133
- model: config.apiModel,
134
- temperature: 0.2,
135
- messages: [
136
- { role: 'system', content: system },
137
- { role: 'user', content },
138
- ],
139
- }),
140
- })
141
- if (!response.ok) {
142
- const detail = await response.text().catch(() => '')
143
- throw new Error(`翻译 API ${response.status}:${detail.slice(0, 200)}`)
144
- }
145
- const data = await response.json().catch(() => null)
146
- if (!data || !Array.isArray(data.choices) || typeof data.choices[0]?.message?.content !== 'string') {
147
- throw new Error(data?.error?.message ? `翻译 API:${String(data.error.message).slice(0, 200)}` : '翻译 API 返回格式不正确:没有字幕内容')
148
- }
149
- const parsed = parseNumbered(data.choices[0].message.content, batch.length)
236
+ const parsed = await requestApiBatch(batch, system, config)
150
237
  const missing = missingNumberedItems(batch, parsed)
151
238
  incomplete += missing.length
152
239
  batch.forEach((line, index) => {
@@ -164,10 +251,21 @@ export async function translateWithApi(items, target, config, { onBatch, shouldC
164
251
  return result
165
252
  }
166
253
 
254
+ // llama-server 也会把短行并起来,编号一错就整批错位;行数对不上就降批重试。
255
+ async function translateWithLlamaAligned(items, target) {
256
+ const parsed = await translateWithLlama(items, target)
257
+ if (parsed.length === items.length) return parsed
258
+ if (items.length <= 1) return parsed.slice(0, 1)
259
+ const mid = Math.ceil(items.length / 2)
260
+ const left = await translateWithLlamaAligned(items.slice(0, mid), target)
261
+ const right = await translateWithLlamaAligned(items.slice(mid), target)
262
+ return [...left, ...right.map((entry) => ({ index: entry.index + mid, text: entry.text }))]
263
+ }
264
+
167
265
  async function translateWithLocal(items, target) {
168
266
  const model = currentLocalModel()
169
267
  if (model.engine === 'llama') {
170
- const parsed = await translateWithLlama(items, target)
268
+ const parsed = await translateWithLlamaAligned(items, target)
171
269
  const result = new Map()
172
270
  for (const entry of parsed) {
173
271
  const line = items[entry.index]