dsh-fish-soul 0.19.17 → 0.19.18
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/README.md +1 -1
- package/lib/bili-playback.js +44 -15
- package/lib/browse.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
package/lib/bili-playback.js
CHANGED
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
*/
|
|
21
21
|
import { spawn } from 'node:child_process';
|
|
22
22
|
import { mkdirSync } from 'node:fs';
|
|
23
|
-
import { homedir } from 'node:os';
|
|
24
23
|
import { join } from 'node:path';
|
|
25
24
|
import { detectBrowsers, pickBrowser } from './browsers.js';
|
|
26
25
|
import { dshHome } from './db.js';
|
|
@@ -30,33 +29,61 @@ const VIDEO_READY_TIMEOUT_MS = 25_000;
|
|
|
30
29
|
const POLL_MS = 1000;
|
|
31
30
|
|
|
32
31
|
/** 极简 CDP 客户端:连浏览器的 webSocketDebuggerUrl,按 sessionId 收发。 */
|
|
33
|
-
class Cdp {
|
|
32
|
+
export class Cdp {
|
|
34
33
|
constructor(ws) {
|
|
35
34
|
this.ws = ws;
|
|
36
35
|
this.nextId = 1;
|
|
37
36
|
this.pending = new Map();
|
|
37
|
+
this.closed = false;
|
|
38
38
|
ws.addEventListener('message', (ev) => {
|
|
39
39
|
let msg = null;
|
|
40
40
|
try { msg = JSON.parse(String(ev.data)); } catch { return; }
|
|
41
41
|
if (!msg?.id || !this.pending.has(msg.id)) return;
|
|
42
|
-
const { resolve, reject } = this.pending.get(msg.id);
|
|
42
|
+
const { resolve, reject, timer } = this.pending.get(msg.id);
|
|
43
43
|
this.pending.delete(msg.id);
|
|
44
|
+
clearTimeout(timer);
|
|
44
45
|
if (msg.error) reject(new Error(`${msg.error.message} (${msg.error.code})`));
|
|
45
46
|
else resolve(msg.result ?? {});
|
|
46
47
|
});
|
|
48
|
+
const disconnect = () => {
|
|
49
|
+
if (this.closed) return;
|
|
50
|
+
this.closed = true;
|
|
51
|
+
this.rejectAll(new Error('CDP 连接已关闭'));
|
|
52
|
+
};
|
|
53
|
+
ws.addEventListener('close', disconnect);
|
|
54
|
+
ws.addEventListener('error', disconnect);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
rejectAll(error) {
|
|
58
|
+
for (const { reject, timer } of this.pending.values()) {
|
|
59
|
+
clearTimeout(timer);
|
|
60
|
+
reject(error);
|
|
61
|
+
}
|
|
62
|
+
this.pending.clear();
|
|
47
63
|
}
|
|
48
64
|
|
|
49
|
-
send(method, params = {}, sessionId) {
|
|
65
|
+
send(method, params = {}, sessionId, { timeoutMs = 15_000 } = {}) {
|
|
66
|
+
if (this.closed) return Promise.reject(new Error('CDP 连接已关闭'));
|
|
50
67
|
const id = this.nextId++;
|
|
51
68
|
const msg = { id, method, params };
|
|
52
69
|
if (sessionId) msg.sessionId = sessionId;
|
|
53
70
|
return new Promise((resolve, reject) => {
|
|
54
|
-
|
|
55
|
-
|
|
71
|
+
const timer = setTimeout(() => {
|
|
72
|
+
this.pending.delete(id);
|
|
73
|
+
reject(new Error(`CDP ${method} 超过 ${timeoutMs}ms 未响应`));
|
|
74
|
+
}, timeoutMs);
|
|
75
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
76
|
+
try { this.ws.send(JSON.stringify(msg)); } catch (error) {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
this.pending.delete(id);
|
|
79
|
+
reject(error);
|
|
80
|
+
}
|
|
56
81
|
});
|
|
57
82
|
}
|
|
58
83
|
|
|
59
84
|
close() {
|
|
85
|
+
this.closed = true;
|
|
86
|
+
this.rejectAll(new Error('CDP 连接已关闭'));
|
|
60
87
|
try { this.ws.close(); } catch { /* 已经关了 */ }
|
|
61
88
|
}
|
|
62
89
|
}
|
|
@@ -89,7 +116,8 @@ export class BiliPlayback {
|
|
|
89
116
|
}
|
|
90
117
|
|
|
91
118
|
cfg() {
|
|
92
|
-
const
|
|
119
|
+
const root = this.getConfig?.() ?? {};
|
|
120
|
+
const browse = (root.bili ?? root).browse ?? {};
|
|
93
121
|
return browse.playback ?? {};
|
|
94
122
|
}
|
|
95
123
|
|
|
@@ -212,7 +240,10 @@ export class BiliPlayback {
|
|
|
212
240
|
const ws = new WebSocket(wsUrl);
|
|
213
241
|
await new Promise((resolve, reject) => {
|
|
214
242
|
ws.addEventListener('open', resolve, { once: true });
|
|
215
|
-
ws.addEventListener('error', () =>
|
|
243
|
+
ws.addEventListener('error', () => {
|
|
244
|
+
try { ws.close(); } catch { /* 已经关了 */ }
|
|
245
|
+
reject(new Error('CDP 连接失败'));
|
|
246
|
+
}, { once: true });
|
|
216
247
|
});
|
|
217
248
|
const cdp = new Cdp(ws);
|
|
218
249
|
state.cdp = cdp;
|
|
@@ -242,7 +273,7 @@ export class BiliPlayback {
|
|
|
242
273
|
}, sessionId);
|
|
243
274
|
} catch { /* 单个 cookie 失败不致命 */ }
|
|
244
275
|
}
|
|
245
|
-
if (state.aborted)
|
|
276
|
+
if (state.aborted) return { ok: false, aborted: true, bvid };
|
|
246
277
|
await cdp.send('Page.navigate', { url: `https://www.bilibili.com/video/${bvid}` }, sessionId);
|
|
247
278
|
|
|
248
279
|
const evalJs = async (expression) => {
|
|
@@ -262,7 +293,7 @@ export class BiliPlayback {
|
|
|
262
293
|
return !v.paused || v.currentTime > 0;
|
|
263
294
|
})()`));
|
|
264
295
|
}
|
|
265
|
-
if (!started)
|
|
296
|
+
if (!started) return { ok: false, error: '播放器没起来', bvid };
|
|
266
297
|
|
|
267
298
|
const deadline = Date.now() + seconds * 1000 + 5000;
|
|
268
299
|
// 注意单位:currentTime 是「片子里的秒」,走完一条片子要 duration 秒的片子时间,
|
|
@@ -282,16 +313,14 @@ export class BiliPlayback {
|
|
|
282
313
|
if (last >= mediaTarget - 0.5) break; // 目标长度到了
|
|
283
314
|
}
|
|
284
315
|
state.played = last;
|
|
285
|
-
await closeTarget();
|
|
286
|
-
await release(cdp);
|
|
287
|
-
cdp.close();
|
|
288
316
|
return { ok: true, bvid, seconds, rate: speed, played: Math.round(last), aborted: state.aborted };
|
|
289
317
|
} catch (error) {
|
|
290
318
|
this.log('warn', `[fish-soul] 播放留痕失败(${bvid}):${String(error instanceof Error ? error.message : error)}`);
|
|
319
|
+
return { ok: false, bvid, error: String(error instanceof Error ? error.message : error) };
|
|
320
|
+
} finally {
|
|
291
321
|
try { await closeTarget(); } catch { /* 标签可能已经被收掉 */ }
|
|
292
322
|
try { await release(state.cdp); } catch { /* 浏览器已经没了 */ }
|
|
293
|
-
|
|
294
|
-
return { ok: false, bvid, error: String(error instanceof Error ? error.message : error) };
|
|
323
|
+
state.cdp?.close();
|
|
295
324
|
}
|
|
296
325
|
})();
|
|
297
326
|
|
package/lib/browse.js
CHANGED
|
@@ -80,7 +80,7 @@ export class BiliBrowser {
|
|
|
80
80
|
* @param {(level: string, msg: string) => void} [deps.log]
|
|
81
81
|
* @param {[number, number]} [deps.watchDelayMs] 两条视频之间"看"多久(真实毫秒)
|
|
82
82
|
*/
|
|
83
|
-
constructor({ db, clock, client, getConfig, callLlm, persona, worker, log, watchDelayMs = [8000, 20000], share, shareFallback, note, frames, playback
|
|
83
|
+
constructor({ db, clock, client, getConfig, callLlm, persona, worker, log, watchDelayMs = [8000, 20000], share, shareFallback, note, frames, playback }) {
|
|
84
84
|
this.share = share ?? null; // 刷到特别想分享的:交给自走层排一条主动消息(带链接)
|
|
85
85
|
// #133:模型没标 [分享] 时的兜底(最近几天没分享过就主动挑一条)。
|
|
86
86
|
// ⚠️ 漏了这个参数整个兜底就是死代码(afterRound 里 self.shareFallback 恒 undefined)。
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-fish-soul",
|
|
3
|
-
"version": "0.19.
|
|
4
|
-
"description": "鱼 · 人格、按人记忆(可纠正、有印象、能导入旧聊天记录)、日程自走与主动开口,直连 SnowLuma 收发 QQ(群里会自己插话),B站 / 抖音小号私信与自己刷视频;按功能选模型并统计用量花费,能听会说 QQ 语音、收藏表情包、学群里的梗,可选上网查;心情影响说话、去 AI 味、每个群的设定、按聊天选模型、每晚写日记、每周记忆存档与可选周记(DSH 插件,单角色 feiyu
|
|
3
|
+
"version": "0.19.18",
|
|
4
|
+
"description": "鱼 · 人格、按人记忆(可纠正、有印象、能导入旧聊天记录)、日程自走与主动开口,直连 SnowLuma 收发 QQ(群里会自己插话),B站 / 抖音小号私信与自己刷视频;按功能选模型并统计用量花费,能听会说 QQ 语音、收藏表情包、学群里的梗,可选上网查;心情影响说话、去 AI 味、每个群的设定、按聊天选模型、每晚写日记、每周记忆存档与可选周记(DSH 插件,单角色 feiyu)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"files": [
|