feat(onebot): get_record out_format — server-side voice transcode (#165)

#165: get_record gains an optional `out_format` (mp3/amr/wma/m4a/spx/ogg/
wav/flac, mirroring NapCat). When set, the QQ voice (SILK/AMR) is
transcoded server-side and the result is attached as `data.base64`; the
original info (file/url/file_size) is preserved. Absent ⇒ behaviour
unchanged. This spares ASR consumers from shipping a SILK decoder.

- protocol: convertAudioBytes(bytes, format, deps?) drives the bundled
  SILK-capable ffmpeg addon's convertFile through temp files (always
  cleaned up), with a format allowlist and an output-size cap. Pure/
  injectable, unit-tested with a fake addon (6 cases).
- core: ExtrasApi.convertRecord(source, format) = loadBinarySource (64 MiB
  cap) → convertAudioBytes.
- onebot: get_record wires out_format; transcode failure (incl. an addon
  lacking SILK decode) degrades to ACTION_FAILED, never crashes.

Adversarially reviewed: no path traversal (format allowlisted), no SSRF
(source is the cached CDN url, not user input), cleanup covers every path,
graceful degradation confirmed. Catalog regenerated. Real-machine e2e
(does the shipped .node actually decode SILK?) is Phase 2.
This commit is contained in:
DrSmoothl committed 2026-06-30 19:25:40 +08:00
1 parent 8fc5917b3b
commit 90456def2e
6 files changed
+241 -5

No files matched your search

+17 -3
View File
@@ -545,18 +545,32 @@ export const actions = [
defineAction({
name: 'get_record',
summary: '获取语音信息',
summary: '获取语音信息;传 out_format 则服务端转码并附带 base64',
readOnly: true,
params: {
file: f.string().default('').role('record'),
file_id: f.string().default('').role('file_id'),
// Optional server-side transcode (#165): SILK/AMR → the requested
// container, returned as `data.base64`. Absent ⇒ behaviour unchanged.
out_format: f.enum('mp3', 'amr', 'wma', 'm4a', 'spx', 'ogg', 'wav', 'flac').optional(),
},
run: async (p, ctx) => {
const file = p.file || p.file_id;
if (!file) return failedResponse(RETCODE.BAD_REQUEST, 'file is required');
const info = await ctx.getRecordInfo(file);
if (info) return okResponse(info);
return failedResponse(RETCODE.ACTION_FAILED, 'record not found in cache');
if (!info) return failedResponse(RETCODE.ACTION_FAILED, 'record not found in cache');
if (!p.out_format) return okResponse(info);
// Transcode from the record's download URL (fall back to file field).
const source = String(info.url || info.file || '');
if (!source) return failedResponse(RETCODE.ACTION_FAILED, 'record has no downloadable source');
try {
// Keep the original record info (file_size stays the source size, like
// NapCat) and just attach the transcoded payload.
const { base64 } = await ctx.bridge.apis.extras.convertRecord(source, p.out_format);
return okResponse({ ...info, out_format: p.out_format, base64 });
} catch (e) {
return failedResponse(RETCODE.ACTION_FAILED, `语音转码失败:${e instanceof Error ? e.message : String(e)}`);
}
},
}),