qq-bot-roost 1.1.12 → 1.1.13

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.
@@ -17,14 +17,198 @@ function pick(...vals) {
17
17
  return '';
18
18
  }
19
19
 
20
+ /** 官方入站表情标签:`<faceType=1,faceId="187",ext="…">` */
21
+ const FACE_TAG_RE = /<faceType=(\d+),faceId="(\d+)"(?:,ext="([^"]*)")?\s*\/?>/g;
22
+ /** `normalizeInbound()` 翻出来的人话标记:`【表情: 幽灵】` */
23
+ const FACE_MARK_RE = /【表情\s*[::]\s*[^】]{1,24}】/g;
24
+
25
+ /** 正文里翻出来的人话标记要删掉,否则客户端会同时显示 `[幽灵]` 和 `【表情: 幽灵】` */
26
+ function stripFaceMarks(text) {
27
+ return String(text ?? '').replace(FACE_MARK_RE, '');
28
+ }
29
+
30
+ /** CQ 码里的字段值要转义,尤其 json 段的 data 自带逗号和方括号 */
31
+ function cqEscape(v) {
32
+ return String(v ?? '')
33
+ .replace(/&/g, '&amp;')
34
+ .replace(/,/g, '&#44;')
35
+ .replace(/\[/g, '&#91;')
36
+ .replace(/\]/g, '&#93;');
37
+ }
38
+
39
+ function cq(type, fields = {}) {
40
+ const body = Object.entries(fields)
41
+ .filter(([, v]) => v !== '' && v !== null && v !== undefined)
42
+ .map(([k, v]) => `${k}=${cqEscape(v)}`)
43
+ .join(',');
44
+ return body ? `[CQ:${type},${body}]` : `[CQ:${type}]`;
45
+ }
46
+
47
+ /**
48
+ * ARK 卡片 → OneBot `json` 段。
49
+ *
50
+ * snowluma 的 `json` 段 `data.data` 是一段 structmsg JSON;aurabot 的
51
+ * `cardText()` 读的是 `meta.detail_1 / news / music` 的 `title/desc/jumpUrl`
52
+ * 和顶层 `prompt`。官方 ark_data 的字段名是 `fields.title / desc / jump_url`,
53
+ * 所以在这里拼一层,别让客户端只看到「[卡片消息]」四个字。
54
+ */
55
+ function arkSegment(ark) {
56
+ if (!ark || typeof ark !== 'object') return null;
57
+ const f = ark.fields && typeof ark.fields === 'object' ? ark.fields : {};
58
+ const title = String(ark.title ?? f.title ?? '');
59
+ const desc = String(ark.desc ?? f.desc ?? '');
60
+ const url = String(ark.url ?? f.jump_url ?? '');
61
+ const prompt = String(ark.prompt ?? ark.name ?? '');
62
+ const kind = `${ark.type ?? ''} ${ark.name ?? ''}`;
63
+ const isLocation = /map|location|位置/i.test(kind);
64
+ const data = isLocation
65
+ ? { app: 'com.tencent.map', meta: { Location: { title, address: desc || url } } }
66
+ : {
67
+ app: 'com.tencent.structmsg',
68
+ desc: '分享',
69
+ view: 'news',
70
+ prompt,
71
+ meta: {
72
+ detail_1: {
73
+ title,
74
+ desc,
75
+ jumpUrl: url,
76
+ url,
77
+ tag: String(ark.name ?? f.tag ?? ''),
78
+ },
79
+ },
80
+ };
81
+ return { type: 'json', data: { data: JSON.stringify(data) } };
82
+ }
83
+
84
+ /** 官方附件 → 段。以前静默丢掉的 other(表情包/未知附件)现在按 file 发 */
85
+ function attachmentSegments(list) {
86
+ const out = [];
87
+ for (const a of Array.isArray(list) ? list : []) {
88
+ if (!a || typeof a !== 'object') continue;
89
+ if (a.type === 'image') {
90
+ out.push({ type: 'image', data: { file: a.url, url: a.url, summary: '[图片]' } });
91
+ } else if (a.type === 'voice') {
92
+ // 官方直接给了 WAV(SILK 转好的),不用自己转码
93
+ out.push({ type: 'record', data: { file: a.wavUrl || a.url, url: a.wavUrl || a.url } });
94
+ } else if (a.type === 'video') {
95
+ out.push({ type: 'video', data: { file: a.url, url: a.url } });
96
+ } else if (a.type === 'file') {
97
+ out.push({ type: 'file', data: { file: a.url, url: a.url, name: a.filename } });
98
+ } else {
99
+ /** 商城表情:snowluma 把它翻成 image 段 + emoji_id,客户端按表情包渲染 */
100
+ const emojiId = String(a.raw?.emoji_id ?? a.raw?.emojiId ?? '');
101
+ if (emojiId) {
102
+ const dir = emojiId.slice(0, 2);
103
+ out.push({
104
+ type: 'image',
105
+ data: {
106
+ file: `${dir}-${emojiId}.gif`,
107
+ url: `https://gxh.vip.qq.com/club/item/parcel/item/${dir}/${emojiId}/raw300.gif`,
108
+ summary: String(a.filename ?? ''),
109
+ emoji_id: emojiId,
110
+ emoji_package_id: Number(a.raw?.emojiPackageId ?? a.raw?.emoji_package_id ?? 0),
111
+ },
112
+ });
113
+ } else {
114
+ out.push({
115
+ type: 'file',
116
+ data: { file: a.url, url: a.url, name: String(a.filename || a.contentType || '附件') },
117
+ });
118
+ }
119
+ }
120
+ }
121
+ return out;
122
+ }
123
+
124
+ /**
125
+ * 一个 `msg_elements` 子元素的载荷(正文 / 卡片 / 子元素 / 附件)。
126
+ * `dedupe=true` 时跳过已经包含在顶层 `msg.content` 里的文字,避免整条消息说两遍。
127
+ */
128
+ function elementPayloadSegments(el, ctx, depth, dedupe) {
129
+ const out = [];
130
+ const content = String(el?.content ?? '');
131
+ if (content && !(dedupe && ctx.fullText.includes(content))) {
132
+ out.push({ type: 'text', data: { text: content } });
133
+ }
134
+ if (el?.ark) {
135
+ const seg = arkSegment(el.ark);
136
+ const key = seg?.data?.data;
137
+ if (seg && !ctx.seenArk.has(key)) {
138
+ ctx.seenArk.add(key);
139
+ out.push(seg);
140
+ }
141
+ }
142
+ for (const child of Array.isArray(el?.children) ? el.children : []) {
143
+ if (depth < 3) out.push(...elementSegments(child, ctx, depth + 1, dedupe));
144
+ }
145
+ out.push(...attachmentSegments(el?.attachments));
146
+ return out;
147
+ }
148
+
149
+ /** 合并转发里的一个节点:用 snowluma 的转发节点形状(客户端读 message / sender) */
150
+ function forwardNode(el, ctx, depth, index) {
151
+ const childCtx = { ...ctx, fullText: '', replyEmitted: false, seenArk: new Set() };
152
+ const segs = elementSegments(el, childCtx, depth + 1, false);
153
+ const author = el?.author ?? {};
154
+ const uin = author.id ? Number(ctx.ids.user(author.id)) || 0 : 0;
155
+ const nickname = String(author.nickname || author.name || '');
156
+ return {
157
+ self_id: Number(ctx.selfUin) || 0,
158
+ user_id: uin,
159
+ time: Number(el?.raw?.timestamp) || Math.floor(Date.now() / 1000),
160
+ message_id: 0,
161
+ message_seq: index + 1,
162
+ real_id: 0,
163
+ message_type: ctx.isGroup ? 'group' : 'private',
164
+ sender: { user_id: uin, nickname, card: String(author.card ?? '') },
165
+ raw_message: rawMessageOf(segs),
166
+ font: 0,
167
+ sub_type: ctx.isGroup ? 'normal' : 'friend',
168
+ message: segs,
169
+ message_format: 'array',
170
+ post_type: 'message',
171
+ };
172
+ }
173
+
174
+ /** 消息元素 → 段;chatRecord 在这里落成 forward 段 + 转发节点存储 */
175
+ function elementSegments(el, ctx, depth = 0, dedupe = false) {
176
+ if (!el || depth > 3) return [];
177
+ if (el.type === 'chatRecord') {
178
+ /** 没有转发存储(测试桩 / 老 ids)时降级成文字,别发一个客户端拉不到内容的 id */
179
+ if (depth >= 3 || typeof ctx.ids?.bindForward !== 'function') {
180
+ return [{ type: 'text', data: { text: '[合并转发]' } }];
181
+ }
182
+ const children = el.children?.length ? el.children : [el];
183
+ const nodes = children.slice(0, 200).map((child, i) => forwardNode(child, ctx, depth, i));
184
+ const id = ctx.ids.bindForward(nodes);
185
+ return [{ type: 'forward', data: { id: String(id) } }];
186
+ }
187
+ if (el.type === 'quote') {
188
+ const out = [];
189
+ const qid = ctx.replyEmitted ? 0 : ctx.ids?.messageIdOfIdx?.(el.msgIdx);
190
+ if (qid) {
191
+ out.push({ type: 'reply', data: { id: String(qid) } });
192
+ ctx.replyEmitted = true;
193
+ }
194
+ const quoted = String(el.content ?? '');
195
+ if (quoted && !(dedupe && ctx.fullText.includes(quoted))) {
196
+ out.push({ type: 'text', data: { text: `[引用: ${quoted}]` } });
197
+ }
198
+ return out;
199
+ }
200
+ return elementPayloadSegments(el, ctx, depth, dedupe);
201
+ }
202
+
20
203
  /**
21
204
  * 把 `msg` 拆成 OneBot 消息段。
22
205
  *
23
206
  * @param {object} msg buildMessage 的产物
24
207
  * @param {import('./ids.js').OneBotIds} ids
25
208
  * @param {number} [selfUin] 机器人自己的 QQ 号:被 @ 时要用它造 at 段
209
+ * @param {number} [depth] 内部用:合并转发嵌套层数(防畸形数据把栈打穿)
26
210
  */
27
- export function segmentsOf(msg, ids, selfUin = 0) {
211
+ export function segmentsOf(msg, ids, selfUin = 0, depth = 0) {
28
212
  const segs = [];
29
213
 
30
214
  /**
@@ -53,33 +237,57 @@ export function segmentsOf(msg, ids, selfUin = 0) {
53
237
  if (qq) segs.push({ type: 'at', data: { qq: String(qq), name: String(m.name ?? '') } });
54
238
  }
55
239
 
56
- if (msg.content) segs.push({ type: 'text', data: { text: String(msg.content) } });
240
+ /** 正文文字:有官方表情标签时同步发 face 段,并把人话标记从正文里剥掉 */
241
+ const rawContent = String(msg.raw?.content ?? '');
242
+ const faces = [];
243
+ if (rawContent.includes('<faceType')) {
244
+ for (const m of rawContent.matchAll(FACE_TAG_RE)) faces.push(Number(m[2]) || 0);
245
+ }
246
+ for (const id of faces.filter(Boolean)) segs.push({ type: 'face', data: { id: String(id) } });
247
+ const text = faces.length ? stripFaceMarks(msg.content) : msg.content;
248
+ if (text) segs.push({ type: 'text', data: { text: String(text) } });
57
249
 
58
- for (const a of Array.isArray(msg.attachments) ? msg.attachments : []) {
59
- if (a.type === 'image') {
60
- segs.push({ type: 'image', data: { file: a.url, url: a.url, summary: '[图片]' } });
61
- } else if (a.type === 'voice') {
62
- // 官方直接给了 WAV(SILK 转好的),不用自己转码
63
- segs.push({ type: 'record', data: { file: a.wavUrl || a.url, url: a.wavUrl || a.url } });
64
- } else if (a.type === 'video') {
65
- segs.push({ type: 'video', data: { file: a.url, url: a.url } });
66
- } else if (a.type === 'file') {
67
- segs.push({ type: 'file', data: { file: a.url, url: a.url, name: a.filename } });
250
+ const ctx = {
251
+ ids,
252
+ selfUin,
253
+ isGroup: msg.kind === 'group',
254
+ fullText: String(text ?? ''),
255
+ replyEmitted: Boolean(quotedId),
256
+ seenArk: new Set(),
257
+ };
258
+
259
+ /** ARK 卡片 + msg_elements(聊天记录 / 引用 / 结构化卡片) */
260
+ if (msg.ark) {
261
+ const seg = arkSegment(msg.ark);
262
+ if (seg) {
263
+ ctx.seenArk.add(seg.data.data);
264
+ segs.push(seg);
68
265
  }
69
266
  }
267
+ for (const el of Array.isArray(msg.elements) ? msg.elements : []) {
268
+ if (depth < 3) segs.push(...elementSegments(el, ctx, depth, true));
269
+ }
270
+
271
+ segs.push(...attachmentSegments(msg.attachments));
70
272
 
71
273
  return segs;
72
274
  }
73
275
 
74
- /** CQ 码形式的纯文本(客户端不解析段时用;只做 text/at/image 三种最常见的) */
276
+ /** CQ 码形式的纯文本(客户端不解析段时用;段类型与 snowluma 的 OneBot 输出对齐) */
75
277
  export function rawMessageOf(segs) {
76
278
  return segs
77
279
  .map((s) => {
78
- if (s.type === 'text') return String(s.data.text ?? '');
79
- if (s.type === 'at') return `[CQ:at,qq=${s.data.qq}]`;
80
- if (s.type === 'image') return '[CQ:image,file=' + String(s.data.file ?? '') + ']';
81
- if (s.type === 'record') return '[CQ:record,file=' + String(s.data.file ?? '') + ']';
82
- if (s.type === 'reply') return `[CQ:reply,id=${s.data.id}]`;
280
+ const d = s.data ?? {};
281
+ if (s.type === 'text') return String(d.text ?? '');
282
+ if (s.type === 'at') return cq('at', { qq: d.qq, name: d.name });
283
+ if (s.type === 'image') return cq('image', { file: d.file, emoji_id: d.emoji_id });
284
+ if (s.type === 'record') return cq('record', { file: d.file });
285
+ if (s.type === 'video') return cq('video', { file: d.file });
286
+ if (s.type === 'file') return cq('file', { file: d.file, name: d.name });
287
+ if (s.type === 'face') return cq('face', { id: d.id });
288
+ if (s.type === 'reply') return cq('reply', { id: d.id });
289
+ if (s.type === 'json') return cq('json', { data: d.data });
290
+ if (s.type === 'forward') return cq('forward', { id: d.id });
83
291
  return '';
84
292
  })
85
293
  .join('');
@@ -6,13 +6,14 @@
6
6
  * 装了才转,没装就明确报错,框架本体不为这条功能破零依赖。
7
7
  *
8
8
  * 三条路径:
9
- * · 已经是 SILK → 直接发
10
- * · WAV → `silk-wasm.encode(buf, 0)`(它自己读 WAV 头,零转码)
11
- * · mp3 / 其它 → 有 ffmpeg 就转 24k 单声道 WAV 再编码
9
+ * · 已经是 SILK → 直接发
10
+ * · WAV(silk-wasm 能直编的采样率)→ `silk-wasm.encode(buf, 0)`(它自己读 WAV 头,零转码)
11
+ * · WAV(怪采样率)/ mp3 / 其它 → 有 ffmpeg 就转 24k 单声道 WAV 再编码
12
12
  *
13
13
  * 两个可选依赖都**不在**根 package.json 里,靠 `ext-deps` 按需补装:
14
14
  * `silk-wasm` 1.5MB,第一次发语音时装;`ffmpeg` 62MB,第一次遇到
15
- * **非 WAV 语音**才装(`@ffmpeg-installer` 的平台子包带静态二进制)。
15
+ * **不能直接编码的语音**(非 WAV、静音档位采样率、超 60 秒等)才装
16
+ *(`@ffmpeg-installer` 的平台子包带静态二进制)。
16
17
  * 面板「外接 → 依赖」也能手动装/卸。
17
18
  */
18
19
 
@@ -31,14 +32,25 @@ const log = createLogger('onebot-voice');
31
32
  export const MAX_VOICE_BYTES = 20 * 1024 * 1024;
32
33
 
33
34
  /**
34
- * QQ 只按 **24kHz** 解码语音 SILK。
35
+ * ffmpeg 转码目标 / 静音自检用的参考解码率,**不是 QQ 的限制**。
35
36
  *
36
- * SILK 容器里**不存采样率**,解码器按约定值读——所以编码前必须先把采样率
37
- * 对齐到 24k,否则客户端解出来就是错的:2026-11-13 实测,22050Hz 的 WAV
38
- * 直接编码会得到**全静音**的码流,语音条发得出去、点开没声音。
37
+ * 2026-11-24 实测纠正了一个持续很久的误解:QQ 官方接口只要求 SILK 容器,
38
+ * 不限采样率;SILK 码流自带采样率,32kHz 编的码流按 24k 或 32k 解码,
39
+ * 音高时长都对。真正的问题是 silk-wasm 对 11025 / 22050 这两个非标准档位
40
+ * 会吐出静音码流(见下面的 SILK_DIRECT_RATES),跟 QQ 无关。
39
41
  */
40
42
  export const QQ_VOICE_RATE = 24000;
41
43
 
44
+ /**
45
+ * 可以跳过 ffmpeg、直接交给 silk-wasm 编码的 WAV 采样率档位。
46
+ *
47
+ * 2026-11-24 实测(1 秒 440Hz 正弦波 → encode → decode):
48
+ * 8000 / 12000 / 16000 / 24000 / 32000 / 44100 / 48000 → 有声,音高时长正确
49
+ * 11025 / 22050 → 静音,体积膨胀约 17 倍
50
+ * 不在表里的采样率一律走 ffmpeg 归一到 24k——宁可多转一次,也不发静音条。
51
+ */
52
+ export const SILK_DIRECT_RATES = new Set([8000, 12000, 16000, 24000, 32000, 44100, 48000]);
53
+
42
54
  /** 读 WAV 头;不是 WAV 或解析失败返回 null */
43
55
  function wavInfoOf(silk, buf) {
44
56
  try {
@@ -255,39 +267,35 @@ export async function toSilk(buf, silk) {
255
267
  if (silk.isSilk?.(buf)) return { silk: buf, via: 'silk' };
256
268
 
257
269
  /**
258
- * **采样率必须归一到 24kHz / 单声道 / 16bit**,否则客户端放不出来。
259
- *
260
- * 2026-11-13 实测(aurabot 用 Windows 本地语音 SAPI,输出 22050Hz / 单声道 /
261
- * 16bit 的 WAV):`silk.encode` 只认 8000 / 12000 / 16000 / 24000 四档,
262
- * 22050 不在表内,它**不报错**,却把码流编成了静音——5 条真实语音解码后
263
- * RMS 全是 1,SILK 体积还膨胀 18–29 倍。QQ 侧表现就是「语音条发得出去、
264
- * 点开没声音」,日志里发送还是成功的。
270
+ * SILK 只吃单声道 16bit PCM;采样率只要在已知安全档位上就能直接编码。
265
271
  *
266
- * 这条路径以前只对「非 WAV」转码,WAV 直接编码,所以 TTS 出 WAV 的用户
267
- * 一直是坏的;走 mp3 的因为经过 ffmpeg,所以从来没暴露过。
272
+ * 历史:2026-11-13 的 22050Hz 静音事故(SAPI 输出)让这里写死了"必须 24k",
273
+ * 2026-11-24 实测推翻了这个前提——QQ 不限采样率,silk-wasm 对 32k 等档位
274
+ * 也编得正常,只有 11025 / 22050 会静音。所以改成白名单判断:GPT-SoVITS
275
+ * 这类 32k 输出不用再白跑一次 ffmpeg 重采样。
268
276
  */
269
277
  const info = wavInfoOf(silk, buf);
270
278
  const fmt = info?.fmt ?? null;
271
279
  const seconds = secondsOf(info);
272
280
  const tooLong = seconds > MAX_VOICE_SEC;
273
- const alreadyQqVoice =
281
+ const directEncode =
274
282
  Boolean(fmt) &&
275
- Number(fmt.sampleRate) === QQ_VOICE_RATE &&
283
+ SILK_DIRECT_RATES.has(Number(fmt.sampleRate)) &&
276
284
  Number(fmt.numberOfChannels) === 1 &&
277
285
  Number(fmt.bitsPerSample) === 16 &&
278
286
  !tooLong;
279
287
 
280
- if (alreadyQqVoice) {
288
+ if (directEncode) {
281
289
  const out = await encodeChecked(silk, buf);
282
290
  log.info(`语音 SILK 就绪:via=wav,${Math.round(Number(silk.getDuration?.(out.data)) || 0)}ms,${out.data.length}B`);
283
291
  return { silk: out.data, via: 'wav' };
284
292
  }
285
293
 
286
294
  /**
287
- * 走到这里说明**不能直接编码**:要么不是 WAV,要么采样率/声道/位深不对,
288
- * 要么时长超限。`silk.encode` 只吃单声道 16bit——双声道、32bit float 的
289
- * WAV 会被它按错误的方式解释、**不会报错**,产出的码流在客户端就是
290
- * 「语音条在、点开放不出来」。参数写进日志,现场不用再猜。
295
+ * 走到这里说明**不能直接编码**:要么不是 WAV,要么采样率在静音档位 /
296
+ * 声道 / 位深不对,要么时长超限。统一走 ffmpeg 归一到 24kHz 单声道 16bit
297
+ * 再编码。双声道、32bit float 的 WAV 会被 `silk.encode` 按错误方式解释、
298
+ * **不会报错**,所以这些参数都要写进日志,现场不用猜。
291
299
  */
292
300
  log.info(
293
301
  fmt
@@ -309,8 +317,8 @@ export async function toSilk(buf, silk) {
309
317
  if (!ffmpeg) {
310
318
  const e = new Error(
311
319
  fmt
312
- ? `这段 WAV 是 ${fmt.sampleRate}Hz / ${fmt.numberOfChannels} 声道,QQ 只认 24000Hz 单声道,` +
313
- '需要 ffmpeg 重采样,自动安装也没成功(原因见日志)。到面板「外接 → 依赖」点『一键安装』。'
320
+ ? `这段 WAV 是 ${fmt.sampleRate}Hz / ${fmt.numberOfChannels} 声道,SILK 编码器不能直接处理,` +
321
+ '需要 ffmpeg 归一到 24kHz 单声道,自动安装也没成功(原因见日志)。到面板「外接 → 依赖」点『一键安装』。'
314
322
  : '这段语音不是 WAV/SILK,需要 ffmpeg 转码,自动安装也没成功(原因见日志)。' +
315
323
  '到面板「外接 → 依赖」点『一键安装』,或让 TTS 直接输出 WAV;装好后下一条语音就能发。',
316
324
  );
@@ -380,10 +388,11 @@ function pcmRms16(buf, maxBytes = 1 << 20) {
380
388
  /**
381
389
  * 编码 + **静音自检**。
382
390
  *
383
- * `silk.encode()` 遇到它不支持的采样率(比如 22050)不会报错,而是吐出一段
391
+ * `silk.encode()` 遇到 11025 / 22050 这类采样率不会报错,而是吐出一段
384
392
  * 能解码、但全静音的码流——消息照常发出,用户点开什么都没有,最难查。
385
393
  * 所以编完自己解码量一次电平:输入明明有声、输出却没声,就当场抛错,
386
394
  * 宁可这条语音报失败,也不发一条「看着在、其实空的」语音条。
395
+ * 解码用 QQ_VOICE_RATE 只是自检的参考采样率,不代表码流本身是 24k。
387
396
  *
388
397
  * 导出给测试用:`test/onebot-voice.mjs` 拿 22050 的 WAV 直接喂它,
389
398
  * 断言它必须拦下来——这是「能发不能播」那道 bug 的回归护栏。
@@ -395,9 +404,9 @@ function pcmRms16(buf, maxBytes = 1 << 20) {
395
404
  * 自动装依赖,`ffmpegToWav` 还会落临时文件——试听若照跑一遍,等于
396
405
  * 「点一下试听,后台开始下 62MB、写一堆盘」,直接违背 5.3 的验收。
397
406
  *
398
- * 所以这里只覆盖**不需要转码**的两种输入:本来就是 SILK,或已经是
399
- * QQ 要的 24kHz / 单声道 / 16bit / ≤60s 的 WAV。这两种走的编码函数
400
- * 与 `toSilk` 的 `alreadyQqVoice` 分支是同一个 `encodeChecked`,
407
+ * 所以这里只覆盖**不需要转码**的两种输入:本来就是 SILK,或采样率在
408
+ * SILK_DIRECT_RATES 白名单内、单声道 / 16bit / ≤60s 的 WAV。这两种走的
409
+ * 编码函数与 `toSilk` 的 `directEncode` 分支是同一个 `encodeChecked`,
401
410
  * 因此「试听校验通过」等价于「这份字节发出去能播」。
402
411
  * 其它格式明确回 `needs_transcode`,让面板说清「发送时框架会转码」,
403
412
  * 而不是假装验过了。
@@ -418,7 +427,7 @@ export async function toSilkDry(buf, silk) {
418
427
  const fmt = info?.fmt ?? null;
419
428
  const seconds = secondsOf(info);
420
429
  const direct = Boolean(fmt)
421
- && Number(fmt.sampleRate) === QQ_VOICE_RATE
430
+ && SILK_DIRECT_RATES.has(Number(fmt.sampleRate))
422
431
  && Number(fmt.numberOfChannels) === 1
423
432
  && Number(fmt.bitsPerSample) === 16
424
433
  && seconds <= MAX_VOICE_SEC;
@@ -427,7 +436,7 @@ export async function toSilkDry(buf, silk) {
427
436
  ok: false,
428
437
  reason: 'needs_transcode',
429
438
  message: fmt
430
- ? `这段 WAV 是 ${fmt.sampleRate}Hz / ${fmt.numberOfChannels} 声道 / ${fmt.bitsPerSample}bit,发送前要 ffmpeg 重采样;试听不跑转码以免写盘`
439
+ ? `这段 WAV 是 ${fmt.sampleRate}Hz / ${fmt.numberOfChannels} 声道 / ${fmt.bitsPerSample}bit,发送前要 ffmpeg 转成 24k 单声道;试听不跑转码以免写盘`
431
440
  : '这段音频不是 WAV/SILK,发送前要 ffmpeg 转码;试听不跑转码以免写盘',
432
441
  };
433
442
  }
package/src/web/server.js CHANGED
@@ -74,6 +74,22 @@ import {
74
74
  const log = createLogger('webui');
75
75
  const WEB_ROOT = path.join(ROOT, 'web');
76
76
 
77
+ /**
78
+ * `/healthz` 的身份指纹:规范化后的代码根路径 sha256 前 12 位。
79
+ *
80
+ * 规范化两件事,因为 dsh-roost 拿的是用户在面板里填的目录字符串:
81
+ * · `path.resolve` 去掉尾斜杠 / `.` / `..`
82
+ * · Windows 盘符统一大写(`c:\x` 与 `C:\x` 是同一份 Roost)
83
+ * 不给明文路径:这个接口免密钥,回环上谁都能读,没必要把本机目录结构送出去。
84
+ *
85
+ * 抽成导出纯函数,让 dsh-roost 侧可以用同一口径做一致性测试。
86
+ */
87
+ export function healthDirHash(dir) {
88
+ const normalized = path.resolve(String(dir || ''))
89
+ .replace(/^([a-z]):/, (_, drive) => drive.toUpperCase() + ':');
90
+ return crypto.createHash('sha256').update(normalized).digest('hex').slice(0, 12);
91
+ }
92
+
77
93
  /**
78
94
  * 进程实例标识。
79
95
  *
@@ -1238,6 +1254,20 @@ async function startWebServerInstance({
1238
1254
  online,
1239
1255
  total: bots.length,
1240
1256
  uptime: Math.round(process.uptime()),
1257
+ /**
1258
+ * 身份字段:让外部客户端(dsh-roost)确认「应答的是哪一份 Roost」。
1259
+ *
1260
+ * 这个接口免密钥、谁都能答,而端口可能被**另一份** Roost 占着。
1261
+ * 以前只给 bots 计数,外部只能靠「有 bots 数组」判在跑,于是另一个
1262
+ * 目录的实例占着同一个端口也会被认成「本目录在跑」——接着端口、
1263
+ * 密钥、机器人配置全串到那份实例上,界面还看起来一切正常。
1264
+ *
1265
+ * pid 只用于排查;dirHash 才是判据:代码根的 sha256 前 12 位,
1266
+ * 既能精确比对又不暴露本机路径。客户端拿自己配置的目录算同一个
1267
+ * 哈希,不匹配就当没探到。没有身份字段的老框架则由客户端自行降级。
1268
+ */
1269
+ pid: process.pid,
1270
+ dirHash: healthDirHash(ROOT),
1241
1271
  }),
1242
1272
  );
1243
1273
  }