dsh-watch-together 0.2.0 → 0.2.2
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 +57 -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 +24 -2
- 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
package/src/chat-server.js
CHANGED
|
@@ -1,198 +1,201 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* chat-server.js — 聊天框的 localhost 服务:一条页面路由 + 四个 API + 生命周期。
|
|
3
|
-
*
|
|
4
|
-
* 路由:
|
|
5
|
-
* GET / 完整版页面(消息列表 + 输入框,只读兜底)
|
|
6
|
-
* GET /api/messages 自 `since` 起的增量,给页面每 600ms 轮询
|
|
7
|
-
* POST /api/say 用户说的话,写进流并回调 onUser
|
|
8
|
-
* POST /api/position 浏览器扩展上报播放位置(见 position-bridge.js)
|
|
9
|
-
* GET /api/position 查最近一次上报(popup / doctor 用)
|
|
10
|
-
*
|
|
11
|
-
* 页面内容在 `chat-view.js`,流在 `chat-store.js`。这里只做"收请求、派给谁"。
|
|
12
|
-
* CLI(`watch.mjs`)和 DSH 插件(`watch_together`)都读同一个文件、同一套 watcher,
|
|
13
|
-
* 谁先起服务谁占端口,另一个复用(`chatAlive` 判)。
|
|
14
|
-
*
|
|
15
|
-
* 输入框的主入口是 Python 悬浮窗(`floating-window.js`),不是这个页面:
|
|
16
|
-
* 浏览器置顶小窗(`GET /widget` + `documentPictureInPicture`)那条老路已删除。
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { createServer } from 'node:http';
|
|
20
|
-
|
|
21
|
-
import { CHAT_PAGE, renderPage } from './chat-view.js';
|
|
22
|
-
import { appendChat, readChat } from './chat-store.js';
|
|
23
|
-
import { readBridge, readHello, writeBridge, writeHello } from './position-bridge.js';
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
'access-control-allow-
|
|
44
|
-
'access-control-allow-
|
|
45
|
-
'
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
*
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
1
|
+
/**
|
|
2
|
+
* chat-server.js — 聊天框的 localhost 服务:一条页面路由 + 四个 API + 生命周期。
|
|
3
|
+
*
|
|
4
|
+
* 路由:
|
|
5
|
+
* GET / 完整版页面(消息列表 + 输入框,只读兜底)
|
|
6
|
+
* GET /api/messages 自 `since` 起的增量,给页面每 600ms 轮询
|
|
7
|
+
* POST /api/say 用户说的话,写进流并回调 onUser
|
|
8
|
+
* POST /api/position 浏览器扩展上报播放位置(见 position-bridge.js)
|
|
9
|
+
* GET /api/position 查最近一次上报(popup / doctor 用)
|
|
10
|
+
*
|
|
11
|
+
* 页面内容在 `chat-view.js`,流在 `chat-store.js`。这里只做"收请求、派给谁"。
|
|
12
|
+
* CLI(`watch.mjs`)和 DSH 插件(`watch_together`)都读同一个文件、同一套 watcher,
|
|
13
|
+
* 谁先起服务谁占端口,另一个复用(`chatAlive` 判)。
|
|
14
|
+
*
|
|
15
|
+
* 输入框的主入口是 Python 悬浮窗(`floating-window.js`),不是这个页面:
|
|
16
|
+
* 浏览器置顶小窗(`GET /widget` + `documentPictureInPicture`)那条老路已删除。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { createServer } from 'node:http';
|
|
20
|
+
|
|
21
|
+
import { CHAT_PAGE, renderPage } from './chat-view.js';
|
|
22
|
+
import { appendChat, readChat } from './chat-store.js';
|
|
23
|
+
import { readBridge, readHello, writeBridge, writeHello } from './position-bridge.js';
|
|
24
|
+
import { configuredChatPort, DEFAULT_CHAT_PORT } from './settings/config.js';
|
|
25
|
+
|
|
26
|
+
export { DEFAULT_CHAT_PORT };
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 起聊天服务(localhost)。返回 { port, server, close },调用方决定常驻还是用完就关。
|
|
30
|
+
*
|
|
31
|
+
* @param {{ port?: number, env?: object, onUser?: (text:string)=>void, log?: Function }} o
|
|
32
|
+
*/
|
|
33
|
+
export async function startChatServer({
|
|
34
|
+
port = null, env = process.env, onUser = null, log = null,
|
|
35
|
+
} = {}) {
|
|
36
|
+
const listenPort = port ?? configuredChatPort(env);
|
|
37
|
+
const page = renderPage(CHAT_PAGE, listenPort);
|
|
38
|
+
|
|
39
|
+
// 扩展从**网页的源**发请求,属于跨域;而且是从公网页面访问本地地址,
|
|
40
|
+
// Chrome 的 Private Network Access 还要求预检里看到 allow-private-network。
|
|
41
|
+
// 我们只暴露"上报位置"和"读位置",没有敏感信息,所以直接放开。
|
|
42
|
+
const cors = {
|
|
43
|
+
'access-control-allow-origin': '*',
|
|
44
|
+
'access-control-allow-methods': 'GET, POST, OPTIONS',
|
|
45
|
+
'access-control-allow-headers': 'content-type',
|
|
46
|
+
'access-control-allow-private-network': 'true',
|
|
47
|
+
'cache-control': 'no-store',
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const server = createServer(async (req, res) => {
|
|
51
|
+
let u;
|
|
52
|
+
try { u = new URL(req.url, 'http://127.0.0.1'); } catch { res.writeHead(400); res.end(); return; }
|
|
53
|
+
|
|
54
|
+
// 扩展直接 fetch(不经过 service worker)时会有这一趟预检
|
|
55
|
+
if (req.method === 'OPTIONS') {
|
|
56
|
+
res.writeHead(204, cors);
|
|
57
|
+
res.end();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ---- 扩展加载时打的招呼:用来区分"没装"和"装了但没视频"
|
|
62
|
+
if (u.pathname === '/api/hello') {
|
|
63
|
+
if (req.method === 'POST') {
|
|
64
|
+
let body = '';
|
|
65
|
+
req.on('data', (c) => { body += c; if (body.length > 4000) req.destroy(); });
|
|
66
|
+
req.on('end', async () => {
|
|
67
|
+
let j = {};
|
|
68
|
+
try { j = JSON.parse(body || '{}'); } catch { /* 空 body 也算打招呼 */ }
|
|
69
|
+
await writeHello(j, { env }).catch(() => null);
|
|
70
|
+
res.writeHead(200, { ...cors, 'content-type': 'application/json; charset=utf-8' });
|
|
71
|
+
res.end('{"ok":true}');
|
|
72
|
+
});
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const h = await readHello({ env }).catch(() => null);
|
|
76
|
+
res.writeHead(200, { ...cors, 'content-type': 'application/json; charset=utf-8' });
|
|
77
|
+
res.end(JSON.stringify(h ? { ok: true, ...h } : { ok: false }));
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---- 浏览器扩展上报位置
|
|
82
|
+
if (u.pathname === '/api/position') {
|
|
83
|
+
if (req.method === 'POST') {
|
|
84
|
+
let body = '';
|
|
85
|
+
req.on('data', (c) => { body += c; if (body.length > 16_000) req.destroy(); });
|
|
86
|
+
req.on('end', async () => {
|
|
87
|
+
let sample = null;
|
|
88
|
+
try { sample = await writeBridge(JSON.parse(body), { env }); } catch { /* 坏请求就当没收到 */ }
|
|
89
|
+
res.writeHead(sample ? 200 : 400, { ...cors, 'content-type': 'application/json; charset=utf-8' });
|
|
90
|
+
res.end(JSON.stringify(sample ? { ok: true, pos: sample.pos } : { ok: false }));
|
|
91
|
+
});
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// 这个处理器是 async 的,异常必须自己收住 —— 否则会在 server 里变成
|
|
95
|
+
// unhandledRejection,把整个进程带下去(Node 15 起默认行为)。
|
|
96
|
+
const b = await readBridge({ env }).catch(() => null);
|
|
97
|
+
res.writeHead(200, { ...cors, 'content-type': 'application/json; charset=utf-8' });
|
|
98
|
+
res.end(JSON.stringify(b ?? { ok: false, reason: 'no-fresh-position' }));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (u.pathname === '/' || u.pathname === '/index.html') {
|
|
103
|
+
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
104
|
+
res.end(page);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (u.pathname === '/api/messages') {
|
|
108
|
+
const since = Math.max(0, Number(u.searchParams.get('since')) || 0);
|
|
109
|
+
const all = readChat(env);
|
|
110
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
111
|
+
res.end(JSON.stringify({ total: all.length, msgs: all.slice(since) }));
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (u.pathname === '/api/say' && req.method === 'POST') {
|
|
115
|
+
let body = '';
|
|
116
|
+
req.on('data', (c) => { body += c; if (body.length > 8000) req.destroy(); });
|
|
117
|
+
req.on('end', () => {
|
|
118
|
+
let ok = false;
|
|
119
|
+
try {
|
|
120
|
+
const j = JSON.parse(body);
|
|
121
|
+
const text = String(j?.text ?? '').trim().slice(0, 500);
|
|
122
|
+
if (text) {
|
|
123
|
+
appendChat('user', text, {}, env);
|
|
124
|
+
ok = true;
|
|
125
|
+
log?.(`[用户] ${text}`);
|
|
126
|
+
onUser?.(text);
|
|
127
|
+
}
|
|
128
|
+
} catch { /* 坏请求就算了 */ }
|
|
129
|
+
// ok:false 让页面能把话回填进输入框 —— 之前一律 ok:true,
|
|
130
|
+
// 服务端没写进去用户也不知道,字就丢了
|
|
131
|
+
res.writeHead(200, { 'content-type': 'application/json' });
|
|
132
|
+
res.end(JSON.stringify({ ok }));
|
|
133
|
+
});
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
res.writeHead(404, { 'content-type': 'text/plain' });
|
|
137
|
+
res.end('not found');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await new Promise((resolve, reject) => {
|
|
141
|
+
server.once('error', reject);
|
|
142
|
+
server.listen(listenPort, '127.0.0.1', resolve);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
return { port: listenPort, server, close: () => new Promise((r) => server.close(() => r())) };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 把聊天框**准备好**(幂等):服务没起来就起,起来了就什么都不做。
|
|
150
|
+
*
|
|
151
|
+
* 为什么需要它:第一次调用要绑端口 + 可能要开标签页,是异步的;而调用方(`watch_together`
|
|
152
|
+
* 进入阻塞之前)只想要一句 `void ensureChatSession(port)` —— 起不来也不该让陪看失败,
|
|
153
|
+
* 所以这里把异常吞掉,只留一个失败的 promise。
|
|
154
|
+
*/
|
|
155
|
+
let chatSession = null;
|
|
156
|
+
export function ensureChatSession(port = null) {
|
|
157
|
+
// 测试或"不想起服务"的场景:`DSH_WATCH_NO_CHAT=1` 直接不给。否则它会一直 listen,
|
|
158
|
+
// 进程(尤其实在 `node --test` 里)因为一个活着的 server 退不出去 —— 实测卡满 60 秒。
|
|
159
|
+
if (process.env.DSH_WATCH_NO_CHAT === '1') return Promise.resolve(null);
|
|
160
|
+
if (!chatSession) {
|
|
161
|
+
// **起不来不许把失败永久缓存**:以前一被赋成"失败的 promise",这一整个进程里就再也不会
|
|
162
|
+
// 尝试监听 —— 端口被别人占过一下,插话通道就永久废掉,而且完全没有提示。
|
|
163
|
+
chatSession = startChatServer({ port })
|
|
164
|
+
.catch(() => null)
|
|
165
|
+
.then((srv) => {
|
|
166
|
+
if (!srv) chatSession = null;
|
|
167
|
+
return srv;
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return chatSession;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 把已经起好的那个服务关掉(测试/退出用) */
|
|
174
|
+
export async function closeChatSession() {
|
|
175
|
+
const s = chatSession;
|
|
176
|
+
chatSession = null;
|
|
177
|
+
if (s) {
|
|
178
|
+
try { const r = await s; await r?.close?.(); } catch { /* 关不掉就算了 */ }
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** 这个端口上有没有已经跑着的聊天服务 */
|
|
183
|
+
export async function chatAlive(port = null, timeoutMs = 1200) {
|
|
184
|
+
const actualPort = port ?? configuredChatPort();
|
|
185
|
+
const ac = new AbortController();
|
|
186
|
+
const timer = setTimeout(() => ac.abort(), timeoutMs);
|
|
187
|
+
try {
|
|
188
|
+
const r = await fetch(`http://127.0.0.1:${actualPort}/api/messages?since=0`, { signal: ac.signal });
|
|
189
|
+
return r.ok;
|
|
190
|
+
} catch { return false; } finally { clearTimeout(timer); }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 聊天记录页的地址。**只读兜底**:输入框的主入口是 `floating-window.js` 打开的
|
|
195
|
+
* Python 悬浮窗(`examples/floating-chat/window.py`)。两个前端读同一个 `chat.jsonl`,
|
|
196
|
+
* 都开着也不冲突。页面里说的话 → `POST /api/say` → 写进 chat.jsonl → 阻塞循环
|
|
197
|
+
* 每 150–500ms 读一次,立刻把这一句交还给模型(见 interrupt.js / watch-run.js 的 wait 循环)。
|
|
198
|
+
*/
|
|
199
|
+
export function chatPageUrl(port = null) {
|
|
200
|
+
return `http://127.0.0.1:${port ?? configuredChatPort()}/`;
|
|
201
|
+
}
|
package/src/chat-store.js
CHANGED
|
@@ -25,6 +25,7 @@ import { join } from 'node:path';
|
|
|
25
25
|
import { cacheDir } from 'dsh-video-storyboard/cache';
|
|
26
26
|
import { emitSpeech, onSpeech, speechSubscribers } from './voice.js';
|
|
27
27
|
import { noteReply } from './reply-guard.js';
|
|
28
|
+
import { appendWindowEvent } from './window-protocol.js';
|
|
28
29
|
|
|
29
30
|
/** 聊天记录(jsonl,一行一条 {role, text, at}) */
|
|
30
31
|
export function chatFile(env = process.env) {
|
|
@@ -98,6 +99,15 @@ export function appendChat(role, text, meta = {}, env = process.env) {
|
|
|
98
99
|
...(meta?.say === true ? { say: true } : {}),
|
|
99
100
|
};
|
|
100
101
|
appendFileSync(file, `${JSON.stringify(entry)}\n`, 'utf8');
|
|
102
|
+
// 悬浮窗主接口:消息同时写一份给 window-events.jsonl。旧的 chat.jsonl 仍然保留,
|
|
103
|
+
// 浏览器页和旧窗口还能读;窗口按 (role, text, at) 去重,不会出现两遍。
|
|
104
|
+
// 写失败不能影响聊天主流程 —— 窗口是展示层,不是数据层。
|
|
105
|
+
try {
|
|
106
|
+
appendWindowEvent({
|
|
107
|
+
type: role === 'meta' ? 'meta' : 'message',
|
|
108
|
+
role, text: entry.text, at: entry.at, ...(entry.t != null ? { t: entry.t } : {}),
|
|
109
|
+
}, env);
|
|
110
|
+
} catch { /* 展示接口不可用时静默降级到 chat.jsonl */ }
|
|
101
111
|
return entry;
|
|
102
112
|
}
|
|
103
113
|
|