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:
6 files changed
+241
-5
No files matched your search
@@ -5,6 +5,8 @@ import {
|
||||
} from '@snowluma/protocol/oidb-services/extras/fetch-ai-voice-list';
|
||||
import { GetStrangerStatus, type StrangerStatus as NamespaceStrangerStatus } from '@snowluma/protocol/oidb-services/extras/get-stranger-status';
|
||||
import { GroupTodo } from '@snowluma/protocol/oidb-services/extras/group-todo';
|
||||
import { convertAudioBytes } from '@snowluma/protocol/highway/ffmpeg-addon';
|
||||
import { loadBinarySource } from '@snowluma/protocol/highway/utils';
|
||||
import type { PttTransReq, PttTransResp } from '@snowluma/proto-defs/ptt-trans';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { BridgeContext } from '../bridge-context';
|
||||
@@ -157,4 +159,18 @@ export class ExtrasApi {
|
||||
if (item?.errCode) throw new Error(`ptt translate failed: error=${item.errCode}`);
|
||||
return item?.text ?? ''; // '' = transcribing async; caller awaits the push
|
||||
}
|
||||
|
||||
/**
|
||||
* Transcode a voice record (`get_record out_format`, #165). `source` is a
|
||||
* URL / local path / `base64://…` (the record's download URL in practice);
|
||||
* it is loaded then transcoded to `format` via the bundled SILK-capable
|
||||
* ffmpeg addon. Returns base64 + size. Throws on unsupported format or a
|
||||
* failed conversion.
|
||||
*/
|
||||
async convertRecord(source: string, format: string): Promise<{ base64: string; size: number }> {
|
||||
// Voices are tiny — cap the download well below loadBinarySource's 1 GiB
|
||||
// default so a tampered/oversized source can't be pulled in.
|
||||
const { bytes } = await loadBinarySource(source, 'record', 64 * 1024 * 1024);
|
||||
return convertAudioBytes(bytes, format);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user