dsh-watch-together 0.2.0 → 0.2.1
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 +49 -0
- package/README.md +10 -2
- package/docs/INTERFACES.zh.md +73 -0
- package/dsh.plugin.json +1 -1
- package/examples/floating-chat/glass_window.py +7 -0
- package/examples/floating-chat/glass_window_gpu.py +502 -0
- package/examples/floating-chat/mic-dsh.png +0 -0
- package/examples/floating-chat/requirements-glass.txt +8 -9
- package/examples/floating-chat/window.py +1454 -3372
- package/examples/floating-chat/window_protocol.py +167 -0
- package/lib/client.js +306 -0
- package/package.json +100 -89
- package/src/chat-server.js +201 -198
- package/src/chat-store.js +10 -0
- package/src/handler.js +266 -261
- package/src/index.js +156 -130
- package/src/input-surface.js +156 -154
- package/src/interfaces.js +43 -0
- package/src/public-api.js +24 -0
- package/src/recognition-interface.js +43 -0
- package/src/register.js +7 -2
- package/src/settings/config.js +146 -0
- package/src/settings/deps.js +163 -0
- package/src/settings/page.html +233 -0
- package/src/settings/page.js +11 -0
- package/src/settings/server.js +195 -0
- package/src/tts-interface.js +59 -0
- package/src/wake-failure.js +67 -0
- package/src/watch-loop.js +37 -11
- package/src/watch-run.js +1007 -1002
- package/src/window-interface.js +81 -0
- package/src/window-protocol.js +122 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
6
|
+
<meta name="referrer" content="no-referrer">
|
|
7
|
+
<title>一起看 · 设置</title>
|
|
8
|
+
<style>
|
|
9
|
+
:root{color-scheme:light dark;--bg:#f6f7f9;--card:#fff;--text:#1c1f24;--muted:#68707d;--line:#dfe3e9;--accent:#2563eb;--ok:#12805c;--bad:#b42318;--code:#f0f2f5}
|
|
10
|
+
@media(prefers-color-scheme:dark){:root{--bg:#111418;--card:#191d23;--text:#e8eaed;--muted:#9aa3af;--line:#2b313a;--accent:#7aa2ff;--ok:#5bd6a8;--bad:#ff8b82;--code:#0e1115}}
|
|
11
|
+
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--text);font:14px/1.55 system-ui,-apple-system,"Segoe UI",sans-serif}
|
|
12
|
+
main{max-width:900px;margin:0 auto;padding:32px 20px 64px}h1{font-size:24px;margin:0 0 4px}h2{font-size:16px;margin:0 0 12px}p{margin:6px 0;color:var(--muted)}
|
|
13
|
+
.card{background:var(--card);border:1px solid var(--line);border-radius:12px;padding:18px;margin:16px 0}
|
|
14
|
+
.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:12px}
|
|
15
|
+
label.field{display:flex;flex-direction:column;gap:6px;font-weight:600;font-size:13px}
|
|
16
|
+
input[type=text],input[type=password],input[type=number],select{width:100%;padding:9px 10px;border:1px solid var(--line);border-radius:8px;background:var(--bg);color:var(--text);font:inherit}
|
|
17
|
+
input[type=checkbox]{margin-right:8px}.check{display:flex;align-items:center;font-weight:500;color:var(--text)}
|
|
18
|
+
.hint{font-size:12px;color:var(--muted);font-weight:400}.actions{display:flex;gap:10px;align-items:center;flex-wrap:wrap;margin-top:16px}
|
|
19
|
+
button{padding:9px 14px;border:1px solid var(--line);border-radius:8px;background:var(--card);color:var(--text);font:inherit;cursor:pointer}
|
|
20
|
+
button.primary{background:var(--accent);border-color:var(--accent);color:#fff}button:disabled{opacity:.55;cursor:default}
|
|
21
|
+
#status{font-size:13px;color:var(--muted)}#status.ok{color:var(--ok)}#status.bad{color:var(--bad)}
|
|
22
|
+
.paths{font-size:12px;color:var(--muted);margin-top:10px}.paths code{background:var(--code);padding:2px 5px;border-radius:4px;color:var(--text)}
|
|
23
|
+
.dep{display:flex;gap:10px;padding:10px 0;border-bottom:1px solid var(--line)}.dep:last-child{border-bottom:0}
|
|
24
|
+
.dot{width:9px;height:9px;border-radius:50%;margin-top:6px;flex:none;background:var(--muted)}.dot.ok{background:var(--ok)}.dot.bad{background:var(--bad)}
|
|
25
|
+
.dep .name{font-weight:600}.dep .detail{font-size:12px;color:var(--muted)}
|
|
26
|
+
.dep .links{display:flex;gap:8px;flex-wrap:wrap;margin-top:7px}
|
|
27
|
+
.dep a.dl{display:inline-block;font-size:12px;font-weight:600;text-decoration:none;padding:5px 9px;border:1px solid var(--accent);border-radius:7px;color:var(--accent)}
|
|
28
|
+
.dep a.dl:hover{background:var(--accent);color:#fff}
|
|
29
|
+
.copy{font-size:12px;padding:2px 7px;margin-left:6px;border-radius:6px}
|
|
30
|
+
.help h3{font-size:14px;margin:14px 0 4px}.help ul{margin:6px 0 0;padding-left:20px}.help li{margin:5px 0;color:var(--muted)}
|
|
31
|
+
code{font:12px/1.5 ui-monospace,SFMono-Regular,Consolas,monospace;background:var(--code);padding:2px 5px;border-radius:4px;color:var(--text)}
|
|
32
|
+
.banner{padding:10px 12px;border-radius:8px;background:var(--code);font-size:12px;color:var(--muted);margin:10px 0}
|
|
33
|
+
</style>
|
|
34
|
+
</head>
|
|
35
|
+
<body><main>
|
|
36
|
+
<h1>一起看 · 设置</h1>
|
|
37
|
+
<p>本页只写本机 JSON。语音适配器在下一场陪看开始时挂载;聊天端口由聊天服务启动时读取,服务已在跑时需重启 DSH。台词/ASR 的 key 在“视频分镜”设置页里配。</p>
|
|
38
|
+
|
|
39
|
+
<div class="card">
|
|
40
|
+
<h2>位置与聊天</h2>
|
|
41
|
+
<div class="grid">
|
|
42
|
+
<label class="field">浏览器调试端口
|
|
43
|
+
<input id="browserPort" type="number" min="1" max="65535" placeholder="9333;留空关闭">
|
|
44
|
+
<span class="hint">Edge 带 --remote-debugging-port=9333 时,插件才能直接读 <video>.currentTime;扩展上报优先于它。</span>
|
|
45
|
+
</label>
|
|
46
|
+
<label class="field">聊天服务端口
|
|
47
|
+
<input id="chatPort" type="number" min="1" max="65535" placeholder="9377">
|
|
48
|
+
<span class="hint">悬浮窗/扩展插话都写同一个 chat.jsonl;端口被占时换一个。</span>
|
|
49
|
+
</label>
|
|
50
|
+
</div>
|
|
51
|
+
<div class="banner" id="positionHint">位置来源顺序:扩展上报 → 调试端口 → 系统媒体会话(SMTC);都没有时才需要手动传 cursor。</div>
|
|
52
|
+
</div>
|
|
53
|
+
|
|
54
|
+
<div class="card">
|
|
55
|
+
<h2>声音</h2>
|
|
56
|
+
<div class="grid">
|
|
57
|
+
<label class="field">语音适配器
|
|
58
|
+
<input id="adapter" type="text" placeholder="D:/code/my-voice.mjs 或包名;留空关闭">
|
|
59
|
+
<span class="hint">核心只负责 import 和转发,不自带 TTS。示例见 examples/voice-edge-tts。</span>
|
|
60
|
+
</label>
|
|
61
|
+
</div>
|
|
62
|
+
<div class="banner" id="voiceHint">留空 = 台词只写进聊天记录,不念出来。</div>
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
<div class="card">
|
|
66
|
+
<h2>依赖与下载</h2>
|
|
67
|
+
<p>缺少的依赖会出现官方下载按钮;插件只打开官方来源,不静默安装、不执行下载的文件。装好后回到本页点“重新检查”。</p>
|
|
68
|
+
<div id="deps"></div>
|
|
69
|
+
<div class="help">
|
|
70
|
+
<h3>Python(悬浮窗)</h3>
|
|
71
|
+
<ul>
|
|
72
|
+
<li>Windows 从 <a href="https://www.python.org/downloads/windows/" target="_blank" rel="noopener noreferrer">python.org 下载</a>,安装时勾选 “Add python.exe to PATH”。</li>
|
|
73
|
+
<li>装完终端运行 <code>python --version</code><button class="copy" data-copy="python --version">复制</button>;没有悬浮窗时仍可用浏览器页面兜底。</li>
|
|
74
|
+
</ul>
|
|
75
|
+
<h3>浏览器扩展</h3>
|
|
76
|
+
<ul>
|
|
77
|
+
<li>Edge 打开 <code>edge://extensions</code><button class="copy" data-copy="edge://extensions">复制</button>,开启“开发人员模式”,选择仓库的 <code>extension/</code> 目录“加载解压缩的扩展”。</li>
|
|
78
|
+
<li>扩展目录可从这里查看:<a href="https://github.com/SAOS-3624/dsh-watch-together/tree/main/extension" target="_blank" rel="noopener noreferrer">extension/</a>;不需要从商店安装。</li>
|
|
79
|
+
<li>没有扩展时,仍可用调试端口或 SMTC 读位置;只是浏览器页面内播放不一定能上报。</li>
|
|
80
|
+
</ul>
|
|
81
|
+
<h3>ffmpeg / ffprobe</h3>
|
|
82
|
+
<ul>
|
|
83
|
+
<li>Windows:<a href="https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip" target="_blank" rel="noopener noreferrer">下载官方 zip</a>,解压后把 <code>bin</code> 加进 PATH;或 <code>winget install Gyan.FFmpeg</code><button class="copy" data-copy="winget install Gyan.FFmpeg">复制</button>。</li>
|
|
84
|
+
<li>macOS:<code>brew install ffmpeg</code><button class="copy" data-copy="brew install ffmpeg">复制</button>。建段和截图都依赖它们;没有 ffmpeg 时陪看无法生成图片。</li>
|
|
85
|
+
</ul>
|
|
86
|
+
<h3>dsh-video-storyboard</h3>
|
|
87
|
+
<ul>
|
|
88
|
+
<li>陪看插件单向依赖它建段;缺它时工具会报错。先安装/启用分镜插件:<a href="https://github.com/SAOS-3624/dsh-video-storyboard" target="_blank" rel="noopener noreferrer">分镜插件仓库</a>。</li>
|
|
89
|
+
</ul>
|
|
90
|
+
<h3>语音适配器</h3>
|
|
91
|
+
<ul>
|
|
92
|
+
<li>示例:<a href="https://github.com/SAOS-3624/dsh-watch-together/tree/main/examples/voice-edge-tts" target="_blank" rel="noopener noreferrer">examples/voice-edge-tts/</a>;也可以填自己的 .mjs 路径或包名。</li>
|
|
93
|
+
<li>适配器导入失败只会没声音,不会让陪看起不来;错误会出现在下一场开始时的 note 里。</li>
|
|
94
|
+
</ul>
|
|
95
|
+
</div>
|
|
96
|
+
</div>
|
|
97
|
+
|
|
98
|
+
<div class="card">
|
|
99
|
+
<div class="paths" id="paths"></div>
|
|
100
|
+
<div class="actions"><button class="primary" id="save">保存设置</button><button id="check">重新检查环境</button><span id="status"></span></div>
|
|
101
|
+
</div>
|
|
102
|
+
</main>
|
|
103
|
+
<script>
|
|
104
|
+
(() => {
|
|
105
|
+
const token = new URLSearchParams(location.search).get('token') || '';
|
|
106
|
+
const apiBase = '/dsh-watch-together/settings/api';
|
|
107
|
+
const $ = (id) => document.getElementById(id);
|
|
108
|
+
const state = { video: null, watch: null, voice: null, checks: [], paths: {} };
|
|
109
|
+
|
|
110
|
+
async function api(path, body) {
|
|
111
|
+
const res = await fetch(apiBase + path, {
|
|
112
|
+
method: body ? 'POST' : 'GET',
|
|
113
|
+
headers: { 'x-settings-token': token, 'content-type': 'application/json' },
|
|
114
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
115
|
+
});
|
|
116
|
+
const data = await res.json().catch(() => ({}));
|
|
117
|
+
if (!res.ok) throw new Error(data.error || ('HTTP ' + res.status));
|
|
118
|
+
return data;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function setStatus(text, kind) {
|
|
122
|
+
const el = $('status');
|
|
123
|
+
el.textContent = text || '';
|
|
124
|
+
el.className = kind || '';
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function escapeHtml(value) {
|
|
128
|
+
return String(value || '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function render() {
|
|
132
|
+
const v = state.video || {};
|
|
133
|
+
const w = state.watch || {};
|
|
134
|
+
const s = state.voice || {};
|
|
135
|
+
$('browserPort').value = v.browserPort || '';
|
|
136
|
+
$('chatPort').value = w.chatPort || '';
|
|
137
|
+
$('adapter').value = s.adapter || '';
|
|
138
|
+
$('positionHint').textContent = v.browserPort
|
|
139
|
+
? '浏览器调试端口:' + v.browserPort + ';扩展上报优先,端口用于直接读页面。'
|
|
140
|
+
: '未配置调试端口:仍可用扩展上报或 SMTC;都没有时才需要手动传 cursor。';
|
|
141
|
+
$('voiceHint').textContent = s.adapter
|
|
142
|
+
? '当前适配器:' + s.adapter + '(下一场开始时挂载)'
|
|
143
|
+
: '留空 = 台词只写进聊天记录,不念出来。';
|
|
144
|
+
$('paths').innerHTML = '<div>video.json:<code>' + escapeHtml(state.paths.video || '') + '</code></div>'
|
|
145
|
+
+ '<div>watch.json:<code>' + escapeHtml(state.paths.watch || '') + '</code></div>'
|
|
146
|
+
+ '<div>voice.json:<code>' + escapeHtml(state.paths.voice || '') + '</code></div>';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function renderChecks() {
|
|
150
|
+
const box = $('deps');
|
|
151
|
+
if (!state.checks.length) { box.textContent = '尚未检查。'; return; }
|
|
152
|
+
box.innerHTML = '';
|
|
153
|
+
for (const c of state.checks) {
|
|
154
|
+
const row = document.createElement('div');
|
|
155
|
+
row.className = 'dep';
|
|
156
|
+
const dot = document.createElement('span');
|
|
157
|
+
dot.className = 'dot ' + (c.ok ? 'ok' : 'bad');
|
|
158
|
+
const body = document.createElement('div');
|
|
159
|
+
const name = document.createElement('div');
|
|
160
|
+
name.className = 'name';
|
|
161
|
+
name.textContent = c.label || c.id;
|
|
162
|
+
const detail = document.createElement('div');
|
|
163
|
+
detail.className = 'detail';
|
|
164
|
+
detail.textContent = c.detail || (c.ok ? '可用' : '不可用');
|
|
165
|
+
body.append(name, detail);
|
|
166
|
+
if (!c.ok && /^https:\/\//.test(c.downloadUrl || '')) {
|
|
167
|
+
const links = document.createElement('div');
|
|
168
|
+
links.className = 'links';
|
|
169
|
+
const a = document.createElement('a');
|
|
170
|
+
a.className = 'dl';
|
|
171
|
+
a.href = c.downloadUrl;
|
|
172
|
+
a.target = '_blank';
|
|
173
|
+
a.rel = 'noopener noreferrer';
|
|
174
|
+
a.textContent = c.downloadLabel || '下载';
|
|
175
|
+
links.append(a);
|
|
176
|
+
body.append(links);
|
|
177
|
+
}
|
|
178
|
+
row.append(dot, body);
|
|
179
|
+
box.append(row);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function load() {
|
|
184
|
+
setStatus('加载中…');
|
|
185
|
+
try {
|
|
186
|
+
const data = await api('/state');
|
|
187
|
+
state.video = data.video; state.watch = data.watch; state.voice = data.voice;
|
|
188
|
+
state.checks = data.checks || []; state.paths = data.paths || {};
|
|
189
|
+
render(); renderChecks();
|
|
190
|
+
setStatus('');
|
|
191
|
+
check();
|
|
192
|
+
} catch (e) { setStatus(e.message, 'bad'); }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function save() {
|
|
196
|
+
setStatus('保存中…');
|
|
197
|
+
try {
|
|
198
|
+
const video = {
|
|
199
|
+
browserPort: $('browserPort').value === '' ? false : Number($('browserPort').value),
|
|
200
|
+
};
|
|
201
|
+
const watch = { chatPort: Number($('chatPort').value) };
|
|
202
|
+
const voice = { adapter: $('adapter').value };
|
|
203
|
+
const data = await api('/save', { video, watch, voice });
|
|
204
|
+
state.video = data.video; state.watch = data.watch; state.voice = data.voice;
|
|
205
|
+
render();
|
|
206
|
+
setStatus('已保存;语音下一场生效,聊天端口若服务已在跑需重启 DSH。', 'ok');
|
|
207
|
+
} catch (e) { setStatus(e.message, 'bad'); }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function check() {
|
|
211
|
+
setStatus('检查中…');
|
|
212
|
+
try {
|
|
213
|
+
const data = await api('/check', {});
|
|
214
|
+
state.checks = data.checks || [];
|
|
215
|
+
renderChecks();
|
|
216
|
+
setStatus('检查完成。', 'ok');
|
|
217
|
+
} catch (e) { setStatus(e.message, 'bad'); }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
$('save').addEventListener('click', save);
|
|
221
|
+
$('check').addEventListener('click', check);
|
|
222
|
+
document.addEventListener('click', async (event) => {
|
|
223
|
+
const btn = event.target.closest('button.copy');
|
|
224
|
+
if (!btn) return;
|
|
225
|
+
try {
|
|
226
|
+
await navigator.clipboard.writeText(btn.dataset.copy || '');
|
|
227
|
+
setStatus('命令已复制。', 'ok');
|
|
228
|
+
} catch (e) { setStatus('复制失败,请手动选择命令。', 'bad'); }
|
|
229
|
+
});
|
|
230
|
+
load();
|
|
231
|
+
})();
|
|
232
|
+
</script>
|
|
233
|
+
</body></html>
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* settings/server.js — 陪看插件自己的本机设置页与 API。
|
|
3
|
+
*
|
|
4
|
+
* 同分镜插件:页面本体是独立 HTML;DSH 里的设置页由 lib/client.js 用
|
|
5
|
+
* 原生 React 表单渲染,直接调用同一套 API,不再套 iframe。工具也仍返回同一地址。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { randomBytes } from 'node:crypto';
|
|
9
|
+
|
|
10
|
+
import { checkEnvironment } from './deps.js';
|
|
11
|
+
import {
|
|
12
|
+
configPaths, readVideoSettings, readVoiceSettings, readWatchSettings,
|
|
13
|
+
writeVideoSettings, writeVoiceSettings, writeWatchSettings,
|
|
14
|
+
} from './config.js';
|
|
15
|
+
import { settingsPage } from './page.js';
|
|
16
|
+
|
|
17
|
+
const PAGE_PATH = '/dsh-watch-together/settings';
|
|
18
|
+
const API_PREFIX = PAGE_PATH + '/api';
|
|
19
|
+
const MAX_BODY = 64 * 1024;
|
|
20
|
+
const INDEX_GLOBAL = '__DSH_WATCH_TOGETHER_SETTINGS__';
|
|
21
|
+
|
|
22
|
+
function json(res, status, data) {
|
|
23
|
+
const body = JSON.stringify(data);
|
|
24
|
+
res.writeHead(status, {
|
|
25
|
+
'content-type': 'application/json; charset=utf-8',
|
|
26
|
+
'cache-control': 'no-store',
|
|
27
|
+
'content-length': Buffer.byteLength(body),
|
|
28
|
+
});
|
|
29
|
+
res.end(body);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function sameToken(actual, expected) {
|
|
33
|
+
if (typeof actual !== 'string' || actual.length !== expected.length) return false;
|
|
34
|
+
let diff = 0;
|
|
35
|
+
for (let i = 0; i < actual.length; i++) diff |= actual.charCodeAt(i) ^ expected.charCodeAt(i);
|
|
36
|
+
return diff === 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function readBody(req) {
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
let size = 0;
|
|
42
|
+
const chunks = [];
|
|
43
|
+
req.on('data', (chunk) => {
|
|
44
|
+
size += chunk.length;
|
|
45
|
+
if (size > MAX_BODY) {
|
|
46
|
+
reject(new Error('请求体太大'));
|
|
47
|
+
req.destroy();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
chunks.push(chunk);
|
|
51
|
+
});
|
|
52
|
+
req.on('end', () => {
|
|
53
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
54
|
+
if (!text) return resolve({});
|
|
55
|
+
try { resolve(JSON.parse(text)); } catch { reject(new Error('请求体不是合法 JSON')); }
|
|
56
|
+
});
|
|
57
|
+
req.on('error', reject);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function statePayload(env) {
|
|
62
|
+
return {
|
|
63
|
+
video: readVideoSettings(env),
|
|
64
|
+
watch: readWatchSettings(env),
|
|
65
|
+
voice: readVoiceSettings(env),
|
|
66
|
+
paths: configPaths(env),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {{ ctx: object, env?: NodeJS.ProcessEnv, log?: Function }} options
|
|
72
|
+
* @returns {null | { url: string, tool: object, dispose: () => void }}
|
|
73
|
+
*/
|
|
74
|
+
export function createSettingsPage({ ctx, env = process.env, log = () => {} } = {}) {
|
|
75
|
+
const webServer = ctx?.get?.('webServer');
|
|
76
|
+
if (!webServer || typeof webServer.register !== 'function') return null;
|
|
77
|
+
const token = randomBytes(32).toString('hex');
|
|
78
|
+
let disposed = false;
|
|
79
|
+
|
|
80
|
+
const handle = async (req, res) => {
|
|
81
|
+
try {
|
|
82
|
+
const url = new URL(req.url || '/', 'http://127.0.0.1');
|
|
83
|
+
if (url.pathname === PAGE_PATH) {
|
|
84
|
+
if (!sameToken(url.searchParams.get('token'), token)) return json(res, 403, { error: 'token 无效' });
|
|
85
|
+
const html = settingsPage();
|
|
86
|
+
res.writeHead(200, {
|
|
87
|
+
'content-type': 'text/html; charset=utf-8',
|
|
88
|
+
'cache-control': 'no-store',
|
|
89
|
+
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; base-uri 'none'; form-action 'none'",
|
|
90
|
+
});
|
|
91
|
+
res.end(html);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (!url.pathname.startsWith(API_PREFIX + '/')) return json(res, 404, { error: 'not found' });
|
|
95
|
+
if (!sameToken(req.headers['x-settings-token'], token)) return json(res, 401, { error: 'token 无效' });
|
|
96
|
+
|
|
97
|
+
const action = url.pathname.slice(API_PREFIX.length);
|
|
98
|
+
if (req.method === 'GET' && action === '/state') {
|
|
99
|
+
return json(res, 200, statePayload(env));
|
|
100
|
+
}
|
|
101
|
+
if (req.method === 'POST' && action === '/save') {
|
|
102
|
+
const body = await readBody(req);
|
|
103
|
+
if (!body || typeof body !== 'object') throw new Error('请求体必须是对象');
|
|
104
|
+
if (body.video !== undefined) writeVideoSettings(body.video, env);
|
|
105
|
+
if (body.watch !== undefined) writeWatchSettings(body.watch, env);
|
|
106
|
+
if (body.voice !== undefined) writeVoiceSettings(body.voice, env);
|
|
107
|
+
return json(res, 200, statePayload(env));
|
|
108
|
+
}
|
|
109
|
+
if (req.method === 'POST' && action === '/check') {
|
|
110
|
+
return json(res, 200, { checks: await checkEnvironment(ctx, env) });
|
|
111
|
+
}
|
|
112
|
+
return json(res, 404, { error: 'not found' });
|
|
113
|
+
} catch (e) {
|
|
114
|
+
const message = String(e?.message ?? e).slice(0, 300);
|
|
115
|
+
if (!res.headersSent) json(res, 400, { error: message });
|
|
116
|
+
else res.end();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
let routeDispose = null;
|
|
121
|
+
try {
|
|
122
|
+
routeDispose = webServer.register({ kind: 'prefix', path: PAGE_PATH, handler: handle });
|
|
123
|
+
} catch (e) {
|
|
124
|
+
log('[dsh-watch-together] settings page unavailable: ' + String(e?.message ?? e));
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const origin = webServer.getOrigin?.() || ('http://127.0.0.1:' + webServer.port);
|
|
129
|
+
const url = origin + PAGE_PATH + '?token=' + token;
|
|
130
|
+
|
|
131
|
+
// The DSH settings tab runs in the same browser origin; hand it the tokenized
|
|
132
|
+
// URL through the boot injection table instead of adding a second API.
|
|
133
|
+
let offIndex = null;
|
|
134
|
+
if (typeof ctx.on === 'function') {
|
|
135
|
+
try {
|
|
136
|
+
offIndex = ctx.on('webserver/index-inject', (table) => {
|
|
137
|
+
table.push({ kind: 'global', name: INDEX_GLOBAL, value: { url } });
|
|
138
|
+
});
|
|
139
|
+
} catch (e) {
|
|
140
|
+
log('[dsh-watch-together] settings index injection unavailable: ' + String(e?.message ?? e));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const tool = {
|
|
145
|
+
name: 'watch_together_settings',
|
|
146
|
+
description: [
|
|
147
|
+
'Open the local watch_together settings page: browser debugging port,',
|
|
148
|
+
'chat service port, voice adapter path, and a read-only dependency check',
|
|
149
|
+
'with official download buttons for Python, the browser extension, and ffmpeg.',
|
|
150
|
+
'Returns a tokenized local URL; the Web GUI also renders the same settings natively in the bundle detail page and Settings > Built-in plugins.',
|
|
151
|
+
].join(' '),
|
|
152
|
+
parameters: {},
|
|
153
|
+
output: {
|
|
154
|
+
schema: {
|
|
155
|
+
type: 'object',
|
|
156
|
+
additionalProperties: false,
|
|
157
|
+
properties: {
|
|
158
|
+
url: { type: 'string', required: true },
|
|
159
|
+
available: { type: 'boolean', required: true },
|
|
160
|
+
text: { type: 'string', required: true },
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
render(_args, value) {
|
|
164
|
+
return [{ type: 'text', text: value.text }];
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
timeoutMs: 10_000,
|
|
168
|
+
isConcurrencySafe() { return true; },
|
|
169
|
+
async execute() {
|
|
170
|
+
if (disposed) throw new Error('settings page is no longer available');
|
|
171
|
+
return {
|
|
172
|
+
url,
|
|
173
|
+
available: true,
|
|
174
|
+
text: [
|
|
175
|
+
'一起看设置页:',
|
|
176
|
+
url,
|
|
177
|
+
'在浏览器打开;页面里的 API 用这个 URL 的 token 校验,不要把它发给别人。',
|
|
178
|
+
'语音适配器下一场开始时挂载;聊天端口在聊天服务启动时读取(服务已在跑需重启 DSH)。',
|
|
179
|
+
'台词/ASR 的 key 在视频分镜设置页配置。',
|
|
180
|
+
].join('\n'),
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
url,
|
|
187
|
+
tool,
|
|
188
|
+
dispose() {
|
|
189
|
+
if (disposed) return;
|
|
190
|
+
disposed = true;
|
|
191
|
+
try { offIndex?.(); } catch { /* ignore */ }
|
|
192
|
+
try { routeDispose?.(); } catch { /* ignore */ }
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tts-interface.js — the **voice/TTS** side of the plugin.
|
|
3
|
+
*
|
|
4
|
+
* The core never imports an engine. It emits timed speech events; a consumer
|
|
5
|
+
* may speak them, caption them, or ignore them. This adapter freezes the
|
|
6
|
+
* event-bus surface so a host can replace it without touching the watch loop.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const TTS_INTERFACE_VERSION = 1;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {{
|
|
13
|
+
* onSpeech: Function,
|
|
14
|
+
* emitSpeech: Function,
|
|
15
|
+
* emitSpoken?: Function,
|
|
16
|
+
* speechSubscribers: Function,
|
|
17
|
+
* lastSpeech: Function,
|
|
18
|
+
* attachConfiguredVoice?: Function,
|
|
19
|
+
* attachedAdapters?: Function,
|
|
20
|
+
* configuredVoiceAdapter?: Function,
|
|
21
|
+
* }} bus
|
|
22
|
+
*/
|
|
23
|
+
export function createTtsInterface(bus = {}) {
|
|
24
|
+
const {
|
|
25
|
+
onSpeech, emitSpeech, emitSpoken, speechSubscribers, lastSpeech,
|
|
26
|
+
attachConfiguredVoice, attachedAdapters, configuredVoiceAdapter,
|
|
27
|
+
} = bus;
|
|
28
|
+
for (const [name, fn] of Object.entries({ onSpeech, emitSpeech, speechSubscribers, lastSpeech })) {
|
|
29
|
+
if (typeof fn !== 'function') throw new TypeError(`tts: ${name}() is required`);
|
|
30
|
+
}
|
|
31
|
+
return Object.freeze({
|
|
32
|
+
kind: 'tts',
|
|
33
|
+
version: TTS_INTERFACE_VERSION,
|
|
34
|
+
subscribe(handler, options) {
|
|
35
|
+
return onSpeech(handler, options);
|
|
36
|
+
},
|
|
37
|
+
due(event) {
|
|
38
|
+
return emitSpeech({ ...event, state: 'due' });
|
|
39
|
+
},
|
|
40
|
+
scheduled(event) {
|
|
41
|
+
return emitSpeech({ ...event, state: 'scheduled' });
|
|
42
|
+
},
|
|
43
|
+
spoken(event) {
|
|
44
|
+
return (emitSpoken ?? emitSpeech)({ ...event, state: 'spoken' });
|
|
45
|
+
},
|
|
46
|
+
skipped(event) {
|
|
47
|
+
return emitSpeech({ ...event, state: 'skipped' });
|
|
48
|
+
},
|
|
49
|
+
subscribers() {
|
|
50
|
+
return speechSubscribers();
|
|
51
|
+
},
|
|
52
|
+
last() {
|
|
53
|
+
return lastSpeech();
|
|
54
|
+
},
|
|
55
|
+
attachConfigured: typeof attachConfiguredVoice === 'function' ? attachConfiguredVoice : null,
|
|
56
|
+
attached: typeof attachedAdapters === 'function' ? attachedAdapters : () => [],
|
|
57
|
+
configured: typeof configuredVoiceAdapter === 'function' ? configuredVoiceAdapter : () => null,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wake-failure.js — 叫醒失败的重试与去重策略。
|
|
3
|
+
*
|
|
4
|
+
* 背景(2026-11-01 实报):悬浮窗里出现几十条
|
|
5
|
+
* `[一起看] 叫醒失败:no-session-controller`。
|
|
6
|
+
*
|
|
7
|
+
* 两个问题:
|
|
8
|
+
* 1. no-session-controller / no-session-id 是**确定性**失败,重试 800ms 没有意义;
|
|
9
|
+
* 2. 每一拍/每句插话都写一条 meta,失败持续时会把聊天流刷满。
|
|
10
|
+
*
|
|
11
|
+
* 这里只做纯策略判断,不碰 fs / 网络,方便单测。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** 同样的失败在这么久之内只显示一次(期间次数折叠到下一次)。 */
|
|
15
|
+
export const WAKE_FAILURE_QUIET_MS = 60_000;
|
|
16
|
+
|
|
17
|
+
/** 这些原因不是“偶发”,重试不会变好。 */
|
|
18
|
+
const DETERMINISTIC_FAILURES = new Set([
|
|
19
|
+
'no-session-controller',
|
|
20
|
+
'no-session-id',
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
/** 是否值得等 800ms 再试一次。 */
|
|
24
|
+
export function isRetryableWakeFailure(reason) {
|
|
25
|
+
const r = String(reason ?? '');
|
|
26
|
+
return !DETERMINISTIC_FAILURES.has(r);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 给用户看一句能行动的提示(只对最常见的接线错误加)。 */
|
|
30
|
+
export function wakeFailureHint(reason) {
|
|
31
|
+
return String(reason ?? '') === 'no-session-controller'
|
|
32
|
+
? '(当前进程没有 DSH 会话控制器,不能叫醒模型;请从 DSH 里启动,或先关掉陪看循环)'
|
|
33
|
+
: '';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 失败提示的节流器。
|
|
38
|
+
*
|
|
39
|
+
* `admit(key, at)` 返回:
|
|
40
|
+
* { allowed: boolean, suppressed: number }
|
|
41
|
+
*
|
|
42
|
+
* `suppressed` 是上一次显示之后、到这次为止折叠掉的次数;
|
|
43
|
+
* 第一次显示为 0,静默期内返回 false 但累加次数。
|
|
44
|
+
*/
|
|
45
|
+
export function createWakeFailureGate({ quietMs = WAKE_FAILURE_QUIET_MS, now = Date.now } = {}) {
|
|
46
|
+
const state = new Map();
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
admit(key, at = now()) {
|
|
50
|
+
const k = String(key ?? 'unknown');
|
|
51
|
+
const prev = state.get(k);
|
|
52
|
+
if (prev && at - prev.at < quietMs) {
|
|
53
|
+
prev.suppressed += 1;
|
|
54
|
+
return { allowed: false, suppressed: prev.suppressed };
|
|
55
|
+
}
|
|
56
|
+
const suppressed = prev?.suppressed ?? 0;
|
|
57
|
+
state.set(k, { at, suppressed: 0 });
|
|
58
|
+
return { allowed: true, suppressed };
|
|
59
|
+
},
|
|
60
|
+
reset() {
|
|
61
|
+
state.clear();
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 进程级默认节流器:所有 watch loop 共用。 */
|
|
67
|
+
export const wakeFailureGate = createWakeFailureGate();
|