dsh-watch-together 0.3.4 → 0.3.6
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/CHANGELOG.md +76 -0
- package/bin/tts.mjs +331 -232
- package/docs/CONFIG.zh.md +6 -1
- package/docs/VOICE.zh.md +2 -2
- package/docs/plugin.zh.md +1 -1
- package/dsh.plugin.json +1 -1
- package/examples/floating-chat/window.py +142 -12
- package/examples/voice-edge-tts/index.mjs +15 -10
- package/examples/voice-edge-tts/stream/client.mjs +8 -7
- package/examples/voice-edge-tts/stream/daemon.py +136 -42
- package/lib/client.js +167 -4
- package/package.json +2 -2
- package/scripts/check-chat-append.py +130 -0
- package/scripts/check-input-select.py +144 -0
- package/src/settings/config.js +81 -0
- package/src/settings/page.html +112 -5
- package/src/settings/server.js +13 -2
- package/src/settings/tts-test.js +243 -0
package/lib/client.js
CHANGED
|
@@ -67,10 +67,11 @@ window.__ModuleLoader__.load({
|
|
|
67
67
|
return { token: token, base: base };
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
async function apiCall(api, action, body) {
|
|
70
|
+
async function apiCall(api, action, body, signal) {
|
|
71
71
|
const res = await fetch(api.base + action, {
|
|
72
72
|
method: body === undefined ? 'GET' : 'POST',
|
|
73
73
|
cache: 'no-store',
|
|
74
|
+
signal: signal || undefined,
|
|
74
75
|
headers: body === undefined
|
|
75
76
|
? { 'x-settings-token': api.token }
|
|
76
77
|
: { 'x-settings-token': api.token, 'content-type': 'application/json' },
|
|
@@ -85,6 +86,7 @@ window.__ModuleLoader__.load({
|
|
|
85
86
|
const video = state && state.video ? state.video : {};
|
|
86
87
|
const watch = state && state.watch ? state.watch : {};
|
|
87
88
|
const voice = state && state.voice ? state.voice : {};
|
|
89
|
+
const tts = state && state.tts ? state.tts : {};
|
|
88
90
|
const win = state && state.window ? state.window : {};
|
|
89
91
|
return {
|
|
90
92
|
browserPort: video.browserPort ? String(video.browserPort) : '',
|
|
@@ -92,6 +94,12 @@ window.__ModuleLoader__.load({
|
|
|
92
94
|
adapter: typeof voice.adapter === 'string' ? voice.adapter : '',
|
|
93
95
|
windowStyle: typeof win.style === 'string' ? win.style : 'auto',
|
|
94
96
|
voiceEnvOverrides: Array.isArray(voice.envOverrides) ? voice.envOverrides : [],
|
|
97
|
+
ttsTestText: '',
|
|
98
|
+
ttsEngine: typeof tts.engine === 'string' ? tts.engine : '',
|
|
99
|
+
ttsVoice: typeof tts.voice === 'string' && tts.voice ? tts.voice : 'zh-CN-XiaoxiaoNeural',
|
|
100
|
+
ttsRate: String(tts.rate === undefined ? 0 : tts.rate),
|
|
101
|
+
ttsPitch: String(tts.pitch === undefined || tts.pitch === '' ? 0 : tts.pitch).replace(/[^\d+-]/g, ''),
|
|
102
|
+
ttsVolume: String(tts.volume === undefined ? 100 : tts.volume),
|
|
95
103
|
};
|
|
96
104
|
}
|
|
97
105
|
|
|
@@ -105,6 +113,9 @@ window.__ModuleLoader__.load({
|
|
|
105
113
|
placeholder: props.placeholder || '',
|
|
106
114
|
autoComplete: 'off',
|
|
107
115
|
spellCheck: false,
|
|
116
|
+
min: props.min,
|
|
117
|
+
max: props.max,
|
|
118
|
+
step: props.step,
|
|
108
119
|
onChange: event => props.onChange(event.target.value),
|
|
109
120
|
}),
|
|
110
121
|
props.hint ? h('span', { className: 'dsh-vs-hint' }, props.hint) : null,
|
|
@@ -145,12 +156,16 @@ window.__ModuleLoader__.load({
|
|
|
145
156
|
const [form, setForm] = useState(null);
|
|
146
157
|
const [checks, setChecks] = useState([]);
|
|
147
158
|
const [checking, setChecking] = useState(false);
|
|
159
|
+
const [testing, setTesting] = useState(false);
|
|
160
|
+
const [testStatus, setTestStatus] = useState('');
|
|
148
161
|
const [error, setError] = useState('');
|
|
149
162
|
const [notice, setNotice] = useState('');
|
|
150
163
|
const formRef = React.useRef(null);
|
|
151
164
|
const firstRender = React.useRef(true);
|
|
152
165
|
const saving = React.useRef(false);
|
|
153
166
|
const pending = React.useRef(false);
|
|
167
|
+
const testAbort = React.useRef(null); // 在飞试听的 AbortController(新顶旧)
|
|
168
|
+
const testSeq = React.useRef(0); // 试听代际:旧回执不许覆盖新状态
|
|
154
169
|
formRef.current = form;
|
|
155
170
|
|
|
156
171
|
async function runCheck(api) {
|
|
@@ -203,6 +218,60 @@ window.__ModuleLoader__.load({
|
|
|
203
218
|
return '';
|
|
204
219
|
}
|
|
205
220
|
|
|
221
|
+
// 保存和试听共用同一份“面板当前 tts 值”的换算
|
|
222
|
+
function ttsPayloadOf(current) {
|
|
223
|
+
const pitch = String(current.ttsPitch || '').trim();
|
|
224
|
+
return {
|
|
225
|
+
engine: current.ttsEngine,
|
|
226
|
+
voice: current.ttsVoice,
|
|
227
|
+
rate: Number(current.ttsRate) || 0,
|
|
228
|
+
pitch: pitch && Number(pitch) !== 0 ? (Number(pitch) > 0 ? '+' + Number(pitch) : pitch) : '',
|
|
229
|
+
volume: Number(current.ttsVolume),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function ttsResultText(r) {
|
|
234
|
+
const engine = {
|
|
235
|
+
'edge-stream': '微软神经音(常驻通道)', 'edge-tts': '微软神经音(逐句兜底)', sapi: '本地系统嗓(SAPI)',
|
|
236
|
+
}[r.engine] || r.engine;
|
|
237
|
+
return '已试听:' + engine + '(首声 ' + (r.firstMs ?? r.ms ?? '?') + 'ms)。';
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function ttsTest() {
|
|
241
|
+
const current = formRef.current;
|
|
242
|
+
if (!current) return;
|
|
243
|
+
const api = apiConfig();
|
|
244
|
+
if (!api) {
|
|
245
|
+
setError('设置接口未注入,请重启 DSH。');
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
// **新顶旧**(和陪看发声同一原则):上一次还在跑就掐掉重来。
|
|
249
|
+
// 按钮永不吞点击 —— 跑着的时候静默空转,用户看到的就是「点了没反应」。
|
|
250
|
+
if (testAbort.current) {
|
|
251
|
+
try { testAbort.current.abort(); } catch { /* 已经结束 */ }
|
|
252
|
+
}
|
|
253
|
+
const controller = new AbortController();
|
|
254
|
+
testAbort.current = controller;
|
|
255
|
+
testSeq.current += 1;
|
|
256
|
+
const seq = testSeq.current;
|
|
257
|
+
setTesting(true);
|
|
258
|
+
setTestStatus('试听中…(合成器常驻时约半秒出声;再点一次可重试)');
|
|
259
|
+
try {
|
|
260
|
+
const r = await apiCall(api, '/tts-test',
|
|
261
|
+
{ text: current.ttsTestText, tts: ttsPayloadOf(current) }, controller.signal);
|
|
262
|
+
if (seq !== testSeq.current) return; // 已被更新的一次顶掉,回执作废
|
|
263
|
+
setTestStatus(r.ok ? ttsResultText(r) : '试听失败:' + (r.reason || '未知原因'));
|
|
264
|
+
} catch (e) {
|
|
265
|
+
if (seq !== testSeq.current) return;
|
|
266
|
+
setTestStatus('试听失败:' + String(e && e.message ? e.message : e));
|
|
267
|
+
} finally {
|
|
268
|
+
if (seq === testSeq.current) {
|
|
269
|
+
setTesting(false);
|
|
270
|
+
testAbort.current = null;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
206
275
|
async function save() {
|
|
207
276
|
const current = formRef.current;
|
|
208
277
|
if (!current) return;
|
|
@@ -231,8 +300,9 @@ window.__ModuleLoader__.load({
|
|
|
231
300
|
const watch = { chatPort: Number(current.chatPort) };
|
|
232
301
|
const voice = { adapter: current.adapter };
|
|
233
302
|
const win = { style: current.windowStyle };
|
|
234
|
-
|
|
235
|
-
|
|
303
|
+
const tts = ttsPayloadOf(current);
|
|
304
|
+
await apiCall(api, '/save', { video: video, watch: watch, voice: voice, window: win, tts: tts });
|
|
305
|
+
setNotice('已自动保存。悬浮窗样式立刻生效;声音参数下一场陪看生效;聊天端口改动在下次启动 DSH 后生效。');
|
|
236
306
|
} catch (e) {
|
|
237
307
|
setError(String(e && e.message ? e.message : e));
|
|
238
308
|
} finally {
|
|
@@ -252,7 +322,9 @@ window.__ModuleLoader__.load({
|
|
|
252
322
|
}
|
|
253
323
|
const timer = setTimeout(() => { void save(); }, 550);
|
|
254
324
|
return () => clearTimeout(timer);
|
|
255
|
-
}, [form && form.browserPort, form && form.chatPort, form && form.adapter, form && form.windowStyle
|
|
325
|
+
}, [form && form.browserPort, form && form.chatPort, form && form.adapter, form && form.windowStyle,
|
|
326
|
+
form && form.ttsEngine, form && form.ttsVoice, form && form.ttsRate, form && form.ttsPitch,
|
|
327
|
+
form && form.ttsVolume]);
|
|
256
328
|
|
|
257
329
|
if (!form) {
|
|
258
330
|
return h('div', { 'data-dsh-vs': 'watch', className: 'dsh-vs dsh-vs-loading' }, error || '正在加载设置…');
|
|
@@ -299,6 +371,80 @@ window.__ModuleLoader__.load({
|
|
|
299
371
|
hint: '留空则台词只写入聊天记录;下一次陪看开始时挂载。',
|
|
300
372
|
onChange: value => setForm(prev => ({ ...prev, adapter: value })),
|
|
301
373
|
}),
|
|
374
|
+
h(SelectField, {
|
|
375
|
+
label: '引擎',
|
|
376
|
+
value: form.ttsEngine,
|
|
377
|
+
options: [
|
|
378
|
+
{ value: '', label: '自动 —— 默认本地 SAPI;配了 edgeTts 路径/环境变量才走云端' },
|
|
379
|
+
{ value: 'sapi', label: '本地 SAPI —— 文本不出机器(系统嗓)' },
|
|
380
|
+
{ value: 'edge', label: '微软 Edge —— 神经音色(文本会发给微软)' },
|
|
381
|
+
],
|
|
382
|
+
hint: '选 Edge = 要念的文本离开这台机器;不介意再选。',
|
|
383
|
+
onChange: value => setForm(prev => ({ ...prev, ttsEngine: value })),
|
|
384
|
+
}),
|
|
385
|
+
h(SelectField, {
|
|
386
|
+
label: '人声选择',
|
|
387
|
+
value: form.ttsVoice,
|
|
388
|
+
options: [
|
|
389
|
+
{ value: 'zh-CN-XiaoxiaoNeural', label: '中文-普通-女声 晓晓' },
|
|
390
|
+
{ value: 'zh-CN-XiaoxuanNeural', label: '中文-普通-女声 晓萱' },
|
|
391
|
+
{ value: 'zh-CN-XiaoyiNeural', label: '中文-普通-女声 晓伊' },
|
|
392
|
+
{ value: 'zh-CN-YunxiNeural', label: '中文-普通-男声 云希' },
|
|
393
|
+
{ value: 'zh-CN-YunjianNeural', label: '中文-普通-男声 云健' },
|
|
394
|
+
{ value: 'zh-CN-YunyangNeural', label: '中文-多风格-男声 云扬' },
|
|
395
|
+
{ value: 'zh-CN-YunxiaNeural', label: '中文-普通-男声 云夏' },
|
|
396
|
+
{ value: 'zh-CN-YunzeNeural', label: '中文-普通-男声 云泽' },
|
|
397
|
+
{ value: 'zh-CN-liaoning-XiaobeiNeural', label: '中文-辽宁-女声 晓北' },
|
|
398
|
+
{ value: 'zh-CN-shaanxi-XiaoniNeural', label: '中文-陕西-女声 晓妮' },
|
|
399
|
+
],
|
|
400
|
+
hint: 'Edge 引擎的神经音色;本地 SAPI 只有系统嗓,选了不生效。',
|
|
401
|
+
onChange: value => setForm(prev => ({ ...prev, ttsVoice: value })),
|
|
402
|
+
}),
|
|
403
|
+
h(TextField, {
|
|
404
|
+
label: '朗读速度',
|
|
405
|
+
value: form.ttsRate,
|
|
406
|
+
type: 'range',
|
|
407
|
+
min: -10,
|
|
408
|
+
max: 10,
|
|
409
|
+
step: 0.5,
|
|
410
|
+
hint: '当前 ' + form.ttsRate + ' 档(约 ' + Math.round(Number(form.ttsRate) * 3) + '%);-10 ~ +10。',
|
|
411
|
+
onChange: value => setForm(prev => ({ ...prev, ttsRate: value })),
|
|
412
|
+
}),
|
|
413
|
+
h(TextField, {
|
|
414
|
+
label: '朗读声调',
|
|
415
|
+
value: form.ttsPitch,
|
|
416
|
+
type: 'range',
|
|
417
|
+
min: -20,
|
|
418
|
+
max: 20,
|
|
419
|
+
step: 1,
|
|
420
|
+
hint: '当前 ' + (Number(form.ttsPitch) > 0 ? '+' : '') + form.ttsPitch + 'Hz;负数更低沉、正数更清亮。',
|
|
421
|
+
onChange: value => setForm(prev => ({ ...prev, ttsPitch: value })),
|
|
422
|
+
}),
|
|
423
|
+
h(TextField, {
|
|
424
|
+
label: '朗读音量',
|
|
425
|
+
value: form.ttsVolume,
|
|
426
|
+
type: 'range',
|
|
427
|
+
min: 0,
|
|
428
|
+
max: 100,
|
|
429
|
+
step: 5,
|
|
430
|
+
hint: '当前 ' + form.ttsVolume + ';100 = 不增不减。',
|
|
431
|
+
onChange: value => setForm(prev => ({ ...prev, ttsVolume: value })),
|
|
432
|
+
}),
|
|
433
|
+
h(TextField, {
|
|
434
|
+
label: '试听文本',
|
|
435
|
+
value: form.ttsTestText,
|
|
436
|
+
placeholder: '这是一句试听文本,听起来怎么样?',
|
|
437
|
+
hint: '按面板当前参数念一句;试听不保存设置。',
|
|
438
|
+
onChange: value => setForm(prev => ({ ...prev, ttsTestText: value })),
|
|
439
|
+
}),
|
|
440
|
+
h('div', { className: 'dsh-vs-field' },
|
|
441
|
+
h('span', { className: 'dsh-vs-label' }, '试听'),
|
|
442
|
+
h('button', {
|
|
443
|
+
className: 'dsh-vs-btn dsh-vs-btn-primary',
|
|
444
|
+
onClick: () => { void ttsTest(); },
|
|
445
|
+
}, testing ? '试听中…(点击重试)' : '试听'),
|
|
446
|
+
h('span', { className: 'dsh-vs-hint' }, testStatus || '点一下出声;结果会写明实际用的引擎。'),
|
|
447
|
+
),
|
|
302
448
|
),
|
|
303
449
|
form.voiceEnvOverrides.length
|
|
304
450
|
? h('p', { className: 'dsh-vs-effective' },
|
|
@@ -386,6 +532,23 @@ window.__ModuleLoader__.load({
|
|
|
386
532
|
name: 'plugins.bundle.config',
|
|
387
533
|
key: 'dsh-watch-together',
|
|
388
534
|
}, BundleConfig));
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* **设置里单独一栏**(和 see-now / storyboard 同一种接法)。
|
|
538
|
+
*
|
|
539
|
+
* 只有 `plugins.bundle.config` 时,入口藏在"插件 → 组合包配置"里 —— 用户按名字翻不到,
|
|
540
|
+
* 社区反馈就是"我是不是没装这个插件"(storyboard 那边已经踩过一模一样的坑)。
|
|
541
|
+
*
|
|
542
|
+
* 数据源不变:`src/settings/server.js` 通过 `webserver/index-inject` 把带 token 的 URL
|
|
543
|
+
* 注成全局 `__DSH_WATCH_TOGETHER_SETTINGS__`,所以这一栏里 `apiConfig()` 照样能读写。
|
|
544
|
+
* `order` 排在 see-now(43)、视频分镜(44) 前面,三个"看"的设置挨在一起。
|
|
545
|
+
*/
|
|
546
|
+
ctx.slots.inject('settings.section', () => ctx.slots.register({
|
|
547
|
+
name: 'settings.section',
|
|
548
|
+
id: 'watch-together',
|
|
549
|
+
order: 42,
|
|
550
|
+
label: () => '一起看视频',
|
|
551
|
+
}, () => h(SettingsTab)));
|
|
389
552
|
}
|
|
390
553
|
|
|
391
554
|
exports.apply = apply;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-watch-together",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "Watch a video together, in sync with the user's real playback: every segment arrives as one storyboard image plus its subtitle lines just before it ends, while the floating window carries spoken replies and live room audio — picture and voice, not a transcript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
"e2e": "node scripts/watch-e2e.mjs"
|
|
105
105
|
},
|
|
106
106
|
"dependencies": {
|
|
107
|
-
"dsh-video-storyboard": "^0.
|
|
107
|
+
"dsh-video-storyboard": "^0.8.0"
|
|
108
108
|
},
|
|
109
109
|
"peerDependencies": {
|
|
110
110
|
"@deepseek-ai/dsh-tools": ">=0.1.0-rc.7"
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""`append_user` 在 EACCES 下要能自愈:python scripts/check-chat-append.py
|
|
2
|
+
|
|
3
|
+
社区 issue(dsh-see-now#75):同一台机器上 PowerShell 能写 chat.jsonl、Python append 报
|
|
4
|
+
`Permission denied`。`append_user` 现在失败时会"整份读出来 + 新行写成临时文件再 os.replace",
|
|
5
|
+
用目录继承的 ACL 把坏文件换掉。这个脚本把两条路都钉住:
|
|
6
|
+
|
|
7
|
+
1. 正常 append:只多一行,历史不动
|
|
8
|
+
2. append 抛 PermissionError:仍然写得进去,历史一行不丢,临时文件不残留
|
|
9
|
+
3. 连 replace 都失败:把**第一个** PermissionError 抛给上层(状态栏要显示真因),临时文件清掉
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import builtins
|
|
14
|
+
import json
|
|
15
|
+
import os
|
|
16
|
+
import sys
|
|
17
|
+
import tempfile
|
|
18
|
+
import types
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
HERE = Path(__file__).resolve().parent
|
|
22
|
+
sys.path.insert(0, str(HERE.parent / "examples" / "floating-chat"))
|
|
23
|
+
|
|
24
|
+
# 本机可能没装 mss(窗口只在抓屏时才用它,模块顶层却 import)
|
|
25
|
+
if "mss" not in sys.modules:
|
|
26
|
+
stub = types.ModuleType("mss")
|
|
27
|
+
stub.mss = lambda *a, **k: None
|
|
28
|
+
sys.modules["mss"] = stub
|
|
29
|
+
|
|
30
|
+
tmp = Path(tempfile.mkdtemp(prefix="chat-append-"))
|
|
31
|
+
os.environ["DSH_VIDEO_CACHE_DIR"] = str(tmp)
|
|
32
|
+
os.environ["DSH_HOME"] = str(tmp / "home")
|
|
33
|
+
|
|
34
|
+
import window # noqa: E402
|
|
35
|
+
|
|
36
|
+
failures: list[str] = []
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def check(name, cond, detail=""):
|
|
40
|
+
if not cond:
|
|
41
|
+
failures.append(f"{name}{(': ' + detail) if detail else ''}")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def reset(text="历史一行\n"):
|
|
45
|
+
(tmp / "chat.jsonl").write_text(text, encoding="utf-8")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def lines():
|
|
49
|
+
return (tmp / "chat.jsonl").read_text(encoding="utf-8").splitlines()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def check_normal():
|
|
53
|
+
reset()
|
|
54
|
+
window.append_user("正常一句")
|
|
55
|
+
got = lines()
|
|
56
|
+
check(len(got) == 2, f"正常 append 只多一行({len(got)})")
|
|
57
|
+
check(json.loads(got[-1])["text"] == "正常一句", "最后一行是新消息")
|
|
58
|
+
check(json.loads(got[-1])["role"] == "user", "role 是 user")
|
|
59
|
+
check(not list(tmp.glob("*.tmp")), "正常路径不留临时文件")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def check_self_heal():
|
|
63
|
+
reset("assistant 写的\n")
|
|
64
|
+
real_open = builtins.open
|
|
65
|
+
target = Path(window.CHAT_FILE)
|
|
66
|
+
|
|
67
|
+
def blocked(file, mode="r", *a, **k):
|
|
68
|
+
if "a" in str(mode) and Path(file) == target:
|
|
69
|
+
raise PermissionError(13, "Permission denied", str(file))
|
|
70
|
+
return real_open(file, mode, *a, **k)
|
|
71
|
+
|
|
72
|
+
builtins.open = blocked
|
|
73
|
+
try:
|
|
74
|
+
window.append_user("坏了也要发出去")
|
|
75
|
+
except Exception as exc: # noqa: BLE001
|
|
76
|
+
check("append 被拒后仍能写进去", False, repr(exc))
|
|
77
|
+
finally:
|
|
78
|
+
builtins.open = real_open
|
|
79
|
+
got = lines()
|
|
80
|
+
check(len(got) == 2, f"自愈后历史 + 1 行({len(got)})")
|
|
81
|
+
check(got[0] == "assistant 写的", "历史一行没丢")
|
|
82
|
+
check(json.loads(got[-1])["text"] == "坏了也要发出去", "新消息真的落盘了")
|
|
83
|
+
check(not list(tmp.glob("*.tmp")), "自愈路径不留临时文件")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def check_replace_fails():
|
|
87
|
+
reset()
|
|
88
|
+
real_open = builtins.open
|
|
89
|
+
real_replace = os.replace
|
|
90
|
+
target = Path(window.CHAT_FILE)
|
|
91
|
+
|
|
92
|
+
def blocked(file, mode="r", *a, **k):
|
|
93
|
+
if "a" in str(mode) and Path(file) == target:
|
|
94
|
+
raise PermissionError(13, "Permission denied", "append")
|
|
95
|
+
return real_open(file, mode, *a, **k)
|
|
96
|
+
|
|
97
|
+
def no_replace(src, dst, *a, **k):
|
|
98
|
+
raise PermissionError(13, "Permission denied", "replace")
|
|
99
|
+
|
|
100
|
+
builtins.open = blocked
|
|
101
|
+
os.replace = no_replace
|
|
102
|
+
try:
|
|
103
|
+
window.append_user("写不进去")
|
|
104
|
+
check("replace 也失败时要抛出", False, "没抛异常")
|
|
105
|
+
except PermissionError as exc:
|
|
106
|
+
check("抛的是 append 那条 PermissionError", "append" in str(exc), str(exc))
|
|
107
|
+
except Exception as exc: # noqa: BLE001
|
|
108
|
+
check("抛的是 PermissionError", False, repr(exc))
|
|
109
|
+
finally:
|
|
110
|
+
builtins.open = real_open
|
|
111
|
+
os.replace = real_replace
|
|
112
|
+
check(not list(tmp.glob("*.tmp")), "失败路径也要清掉临时文件")
|
|
113
|
+
check(lines() == ["历史一行"], "失败时不动原文件")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def main() -> int:
|
|
117
|
+
check_normal()
|
|
118
|
+
check_self_heal()
|
|
119
|
+
check_replace_fails()
|
|
120
|
+
if failures:
|
|
121
|
+
print(f"FAIL: {len(failures)} 项")
|
|
122
|
+
for f in failures:
|
|
123
|
+
print(" -", f)
|
|
124
|
+
return 1
|
|
125
|
+
print("OK: 正常 append / EACCES 自愈 / 连 replace 都失败时抛真因")
|
|
126
|
+
return 0
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
if __name__ == "__main__":
|
|
130
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""顶边缩放与输入框选字的回归:python scripts/check-input-select.py"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
HERE = Path(__file__).resolve().parent
|
|
9
|
+
sys.path.insert(0, str(HERE.parent / "examples" / "floating-chat"))
|
|
10
|
+
|
|
11
|
+
CHAR_W = 10
|
|
12
|
+
|
|
13
|
+
failures: list[str] = []
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def check(name, cond, detail=""):
|
|
17
|
+
if not cond:
|
|
18
|
+
failures.append(f"{name}{(': ' + detail) if detail else ''}")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class FakeFont:
|
|
22
|
+
def measure(self, s):
|
|
23
|
+
return len(s) * CHAR_W
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class FakeFonts:
|
|
27
|
+
def get(self, _size, _bold=False):
|
|
28
|
+
return FakeFont()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class FakeRoot:
|
|
32
|
+
def __init__(self, w, h):
|
|
33
|
+
self.w, self.h = w, h
|
|
34
|
+
|
|
35
|
+
def winfo_width(self):
|
|
36
|
+
return self.w
|
|
37
|
+
|
|
38
|
+
def winfo_height(self):
|
|
39
|
+
return self.h
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def stub(w=760, h=560):
|
|
43
|
+
from window import LiveGlass
|
|
44
|
+
|
|
45
|
+
s = object.__new__(LiveGlass)
|
|
46
|
+
s.root = FakeRoot(w, h)
|
|
47
|
+
s.fonts = FakeFonts()
|
|
48
|
+
return s
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def check_top_edge():
|
|
52
|
+
import window
|
|
53
|
+
from window import LiveGlass
|
|
54
|
+
|
|
55
|
+
s = stub()
|
|
56
|
+
w = s.root.w
|
|
57
|
+
top = getattr(window, "TOP_EDGE_PX", 14)
|
|
58
|
+
for x in (40, w // 2, w - 40):
|
|
59
|
+
check(f"顶边 y=0 x={x} 往上拉", LiveGlass._edge_at(s, x, 0) == "n",
|
|
60
|
+
repr(LiveGlass._edge_at(s, x, 0)))
|
|
61
|
+
check(f"顶边 y={top} x={x} 往上拉", LiveGlass._edge_at(s, x, top) == "n")
|
|
62
|
+
check(f"顶边 y={top + 1} x={x} 拖窗", LiveGlass._edge_at(s, x, top + 1) is None,
|
|
63
|
+
repr(LiveGlass._edge_at(s, x, top + 1)))
|
|
64
|
+
check("左上角", LiveGlass._edge_at(s, 5, 5) == "nw")
|
|
65
|
+
check("右上角", LiveGlass._edge_at(s, w - 5, 5) == "ne")
|
|
66
|
+
check("右边", LiveGlass._edge_at(s, w - 3, 300) == "e")
|
|
67
|
+
check("左边", LiveGlass._edge_at(s, 3, 300) == "w")
|
|
68
|
+
check("底边", LiveGlass._edge_at(s, 300, s.root.h - 3) == "s")
|
|
69
|
+
check("中间", LiveGlass._edge_at(s, 300, 300) is None)
|
|
70
|
+
check("命中带不压到标题栏 item", top < 17, f"TOP_EDGE_PX={top}")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def check_index_at():
|
|
74
|
+
from window import LiveGlass
|
|
75
|
+
|
|
76
|
+
if not hasattr(LiveGlass, "_input_index_at"):
|
|
77
|
+
check("点击定位", False, "LiveGlass 没有 _input_index_at")
|
|
78
|
+
return
|
|
79
|
+
s = stub()
|
|
80
|
+
x0 = 100
|
|
81
|
+
s._input_view = (x0, 0, "abcdef")
|
|
82
|
+
cases = {x0 - 50: 0, x0: 0, x0 + 4: 0, x0 + 5: 1, x0 + 14: 1, x0 + 15: 2,
|
|
83
|
+
x0 + 59: 6, x0 + 500: 6}
|
|
84
|
+
for px, want in cases.items():
|
|
85
|
+
got = LiveGlass._input_index_at(s, px)
|
|
86
|
+
check(f"可见段从 0 起 px={px}", got == want, f"got {got} want {want}")
|
|
87
|
+
s._input_view = (x0, 7, "hijk")
|
|
88
|
+
check("可见段偏移 px=x0", LiveGlass._input_index_at(s, x0) == 7)
|
|
89
|
+
check("可见段偏移 px=x0+25", LiveGlass._input_index_at(s, x0 + 25) == 10)
|
|
90
|
+
check("可见段偏移 末尾", LiveGlass._input_index_at(s, x0 + 999) == 11)
|
|
91
|
+
s._input_view = None
|
|
92
|
+
check("没有视图时返回 0", LiveGlass._input_index_at(s, 123) == 0)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def check_ctrl_a():
|
|
96
|
+
try:
|
|
97
|
+
import tkinter as tk
|
|
98
|
+
root = tk.Tk()
|
|
99
|
+
except Exception as exc:
|
|
100
|
+
print(f"SKIP: Ctrl+A 需要 Tk({type(exc).__name__})")
|
|
101
|
+
return
|
|
102
|
+
try:
|
|
103
|
+
from window import LiveGlass
|
|
104
|
+
|
|
105
|
+
if not hasattr(LiveGlass, "_input_select_all"):
|
|
106
|
+
check("Ctrl+A 全选", False, "LiveGlass 没有 _input_select_all")
|
|
107
|
+
return
|
|
108
|
+
root.geometry("200x40+-3000+-3000")
|
|
109
|
+
entry = tk.Entry(root)
|
|
110
|
+
entry.pack()
|
|
111
|
+
s = object.__new__(LiveGlass)
|
|
112
|
+
s._entry = entry
|
|
113
|
+
s._render_input = lambda: None
|
|
114
|
+
for seq in ("<Control-a>", "<Control-A>"):
|
|
115
|
+
entry.bind(seq, lambda e: LiveGlass._input_select_all(s, e))
|
|
116
|
+
entry.insert(0, "你好世界 hello")
|
|
117
|
+
entry.icursor(3)
|
|
118
|
+
root.update()
|
|
119
|
+
entry.focus_force()
|
|
120
|
+
root.update()
|
|
121
|
+
entry.event_generate("<Control-a>")
|
|
122
|
+
root.update()
|
|
123
|
+
sel = (entry.index("sel.first"), entry.index("sel.last")) if entry.select_present() else None
|
|
124
|
+
check("Ctrl+A 选中全文", sel == (0, len("你好世界 hello")), repr(sel))
|
|
125
|
+
check("Ctrl+A 后插入点在末尾", entry.index("insert") == len("你好世界 hello"))
|
|
126
|
+
finally:
|
|
127
|
+
root.destroy()
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def main() -> int:
|
|
131
|
+
check_top_edge()
|
|
132
|
+
check_index_at()
|
|
133
|
+
check_ctrl_a()
|
|
134
|
+
if failures:
|
|
135
|
+
print(f"FAIL: {len(failures)} 项")
|
|
136
|
+
for f in failures:
|
|
137
|
+
print(" -", f)
|
|
138
|
+
return 1
|
|
139
|
+
print("OK: 顶边可往上拉、标题栏照样拖窗、点击定位下标正确、Ctrl+A 全选")
|
|
140
|
+
return 0
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
if __name__ == "__main__":
|
|
144
|
+
raise SystemExit(main())
|
package/src/settings/config.js
CHANGED
|
@@ -209,11 +209,92 @@ export function writeVoiceSettings(patch = {}, env = process.env) {
|
|
|
209
209
|
return readVoiceSettings(env);
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
+
export function ttsConfigPath(env = process.env) { return join(dshHome(env), 'tts.json'); }
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 声音调节(`~/.dsh/tts.json`)。字段口径和 `bin/tts.mjs`、`examples/voice-edge-tts` 共用:
|
|
216
|
+
* `voice` / `rate`(-10..10) / `pitch`(±Hz) / `volume`(0-100),
|
|
217
|
+
* 另有引擎策略键 `engine` / `edgeTts`(改它们 = 切换本地/云端,保存时单独校验)。
|
|
218
|
+
*/
|
|
219
|
+
export function readTtsSettings(env = process.env) {
|
|
220
|
+
const path = ttsConfigPath(env);
|
|
221
|
+
const raw = readObject(path);
|
|
222
|
+
const d = raw.data || {};
|
|
223
|
+
return {
|
|
224
|
+
path,
|
|
225
|
+
exists: raw.exists,
|
|
226
|
+
error: raw.error,
|
|
227
|
+
engine: d.engine === 'edge' || d.engine === 'sapi' ? d.engine : '',
|
|
228
|
+
voice: typeof d.voice === 'string' ? d.voice : '',
|
|
229
|
+
rate: Number.isFinite(Number(d.rate)) ? Number(d.rate) : 0,
|
|
230
|
+
pitch: d.pitch === undefined || d.pitch === null ? '' : String(d.pitch),
|
|
231
|
+
volume: Number.isFinite(Number(d.volume)) ? Number(d.volume) : 100,
|
|
232
|
+
edgeTts: typeof d.edgeTts === 'string' ? d.edgeTts : '',
|
|
233
|
+
envOverrides: ['DSH_TTS_ENGINE', 'DSH_TTS_BIN']
|
|
234
|
+
.filter((k) => env[k] !== undefined && env[k] !== ''),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 把一个 patch 应用到 tts 配置对象上(就地修改),校验和收敛口径只此一份:
|
|
240
|
+
* 保存(`writeTtsSettings`)和试听(`effectiveTtsSettings`)都用它,
|
|
241
|
+
* 保证「试听听到的参数」和「陪看会用的参数」永远同一套换算。
|
|
242
|
+
*/
|
|
243
|
+
export function applyTtsPatch(next, patch = {}) {
|
|
244
|
+
const num = (v, lo, hi, field) => {
|
|
245
|
+
const n = Number(v);
|
|
246
|
+
if (!Number.isFinite(n)) throw new Error(`${field} 必须是数字`);
|
|
247
|
+
return Math.max(lo, Math.min(hi, n));
|
|
248
|
+
};
|
|
249
|
+
if (patch.engine !== undefined) {
|
|
250
|
+
const e = String(patch.engine ?? '').trim().toLowerCase();
|
|
251
|
+
if (!e) delete next.engine;
|
|
252
|
+
else if (e === 'edge' || e === 'sapi') next.engine = e;
|
|
253
|
+
else throw new Error('engine 只能是 edge / sapi(留空 = 自动)');
|
|
254
|
+
}
|
|
255
|
+
if (patch.voice !== undefined) {
|
|
256
|
+
const v = String(patch.voice ?? '').trim();
|
|
257
|
+
if (v.length > MAX_TEXT) throw new Error('voice 太长');
|
|
258
|
+
if (v) next.voice = v; else delete next.voice;
|
|
259
|
+
}
|
|
260
|
+
if (patch.rate !== undefined) next.rate = num(patch.rate, -10, 10, 'rate');
|
|
261
|
+
if (patch.pitch !== undefined) {
|
|
262
|
+
const p = String(patch.pitch ?? '').trim();
|
|
263
|
+
if (p && !/^[+-]?\d+(?:\.\d+)?(?:\s*Hz)?$/i.test(p)) throw new Error('pitch 写法如 "+2" 或 "+2Hz"');
|
|
264
|
+
if (p && Number.parseFloat(p) !== 0) next.pitch = p; else delete next.pitch;
|
|
265
|
+
}
|
|
266
|
+
if (patch.volume !== undefined) next.volume = num(patch.volume, 0, 100, 'volume');
|
|
267
|
+
if (patch.edgeTts !== undefined) {
|
|
268
|
+
const t = String(patch.edgeTts ?? '').trim();
|
|
269
|
+
if (t.length > MAX_TEXT) throw new Error('edgeTts 太长');
|
|
270
|
+
if (t) next.edgeTts = t; else delete next.edgeTts;
|
|
271
|
+
}
|
|
272
|
+
return next;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export function writeTtsSettings(patch = {}, env = process.env) {
|
|
276
|
+
const path = ttsConfigPath(env);
|
|
277
|
+
const { data } = readObject(path);
|
|
278
|
+
const next = applyTtsPatch({ ...data }, patch);
|
|
279
|
+
writeObject(path, next);
|
|
280
|
+
return readTtsSettings(env);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* 「这套设置生效后 tts.json 长什么样」——**不落盘**。
|
|
285
|
+
* 试听按钮用它拿面板当前值(patch 盖在已保存值上)喂合成器。
|
|
286
|
+
*/
|
|
287
|
+
export function effectiveTtsSettings(patch = {}, env = process.env) {
|
|
288
|
+
const { data } = readObject(ttsConfigPath(env));
|
|
289
|
+
return applyTtsPatch({ ...data }, patch);
|
|
290
|
+
}
|
|
291
|
+
|
|
212
292
|
export function configPaths(env = process.env) {
|
|
213
293
|
return {
|
|
214
294
|
home: dshHome(env),
|
|
215
295
|
video: videoConfigPath(env),
|
|
216
296
|
voice: voiceConfigPath(env),
|
|
297
|
+
tts: ttsConfigPath(env),
|
|
217
298
|
watch: watchConfigPath(env),
|
|
218
299
|
window: windowConfigPath(env),
|
|
219
300
|
};
|