mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 08:07:27 +00:00
refactor(media): centralize voice compatibility policy
This commit is contained in:
@@ -77,13 +77,17 @@ export function resolveMatrixVoiceDecision(opts: {
|
|||||||
if (!opts.wantsVoice) {
|
if (!opts.wantsVoice) {
|
||||||
return { useVoice: false };
|
return { useVoice: false };
|
||||||
}
|
}
|
||||||
if (
|
if (isMatrixVoiceCompatibleAudio(opts)) {
|
||||||
getCore().media.isVoiceCompatibleAudio({
|
|
||||||
contentType: opts.contentType,
|
|
||||||
fileName: opts.fileName,
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
return { useVoice: true };
|
return { useVoice: true };
|
||||||
}
|
}
|
||||||
return { useVoice: false };
|
return { useVoice: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isMatrixVoiceCompatibleAudio(opts: { contentType?: string; fileName?: string }): boolean {
|
||||||
|
// Matrix currently shares the core voice compatibility policy.
|
||||||
|
// Keep this wrapper as the seam if Matrix policy diverges later.
|
||||||
|
return getCore().media.isVoiceCompatibleAudio({
|
||||||
|
contentType: opts.contentType,
|
||||||
|
fileName: opts.fileName,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,22 +1,20 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { isVoiceCompatibleAudio } from "./audio.js";
|
import {
|
||||||
|
isVoiceCompatibleAudio,
|
||||||
|
TELEGRAM_VOICE_AUDIO_EXTENSIONS,
|
||||||
|
TELEGRAM_VOICE_MIME_TYPES,
|
||||||
|
} from "./audio.js";
|
||||||
|
|
||||||
describe("isVoiceCompatibleAudio", () => {
|
describe("isVoiceCompatibleAudio", () => {
|
||||||
it.each([
|
it.each([
|
||||||
{ contentType: "audio/ogg", fileName: null },
|
...Array.from(TELEGRAM_VOICE_MIME_TYPES, (contentType) => ({ contentType, fileName: null })),
|
||||||
{ contentType: "audio/opus", fileName: null },
|
|
||||||
{ contentType: "audio/ogg; codecs=opus", fileName: null },
|
{ contentType: "audio/ogg; codecs=opus", fileName: null },
|
||||||
{ contentType: "audio/mpeg", fileName: null },
|
|
||||||
{ contentType: "audio/mp3", fileName: null },
|
|
||||||
{ contentType: "audio/mp4", fileName: null },
|
|
||||||
{ contentType: "audio/mp4; codecs=mp4a.40.2", fileName: null },
|
{ contentType: "audio/mp4; codecs=mp4a.40.2", fileName: null },
|
||||||
{ contentType: "audio/x-m4a", fileName: null },
|
|
||||||
{ contentType: "audio/m4a", fileName: null },
|
|
||||||
])("returns true for MIME type $contentType", (opts) => {
|
])("returns true for MIME type $contentType", (opts) => {
|
||||||
expect(isVoiceCompatibleAudio(opts)).toBe(true);
|
expect(isVoiceCompatibleAudio(opts)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it.each([".ogg", ".oga", ".opus", ".mp3", ".m4a"])("returns true for extension %s", (ext) => {
|
it.each(Array.from(TELEGRAM_VOICE_AUDIO_EXTENSIONS))("returns true for extension %s", (ext) => {
|
||||||
expect(isVoiceCompatibleAudio({ fileName: `voice${ext}` })).toBe(true);
|
expect(isVoiceCompatibleAudio({ fileName: `voice${ext}` })).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import { getFileExtension } from "./mime.js";
|
import { getFileExtension, normalizeMimeType } from "./mime.js";
|
||||||
|
|
||||||
const VOICE_AUDIO_EXTENSIONS = new Set([".oga", ".ogg", ".opus", ".mp3", ".m4a"]);
|
export const TELEGRAM_VOICE_AUDIO_EXTENSIONS = new Set([".oga", ".ogg", ".opus", ".mp3", ".m4a"]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MIME types compatible with voice messages.
|
* MIME types compatible with voice messages.
|
||||||
* Telegram sendVoice supports OGG/Opus, MP3, and M4A.
|
* Telegram sendVoice supports OGG/Opus, MP3, and M4A.
|
||||||
* https://core.telegram.org/bots/api#sendvoice
|
* https://core.telegram.org/bots/api#sendvoice
|
||||||
*/
|
*/
|
||||||
const VOICE_MIME_TYPES = new Set([
|
export const TELEGRAM_VOICE_MIME_TYPES = new Set([
|
||||||
"audio/ogg",
|
"audio/ogg",
|
||||||
"audio/opus",
|
"audio/opus",
|
||||||
"audio/mpeg",
|
"audio/mpeg",
|
||||||
@@ -17,16 +17,13 @@ const VOICE_MIME_TYPES = new Set([
|
|||||||
"audio/m4a",
|
"audio/m4a",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
export function isVoiceCompatibleAudio(opts: {
|
export function isTelegramVoiceCompatibleAudio(opts: {
|
||||||
contentType?: string | null;
|
contentType?: string | null;
|
||||||
fileName?: string | null;
|
fileName?: string | null;
|
||||||
}): boolean {
|
}): boolean {
|
||||||
const mime = opts.contentType?.toLowerCase().trim();
|
const mime = normalizeMimeType(opts.contentType);
|
||||||
if (mime) {
|
if (mime && TELEGRAM_VOICE_MIME_TYPES.has(mime)) {
|
||||||
const baseMime = mime.split(";")[0].trim();
|
return true;
|
||||||
if (VOICE_MIME_TYPES.has(baseMime)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const fileName = opts.fileName?.trim();
|
const fileName = opts.fileName?.trim();
|
||||||
if (!fileName) {
|
if (!fileName) {
|
||||||
@@ -36,5 +33,16 @@ export function isVoiceCompatibleAudio(opts: {
|
|||||||
if (!ext) {
|
if (!ext) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return VOICE_AUDIO_EXTENSIONS.has(ext);
|
return TELEGRAM_VOICE_AUDIO_EXTENSIONS.has(ext);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Backward-compatible alias used across plugin/runtime call sites.
|
||||||
|
* Keeps existing behavior while making Telegram-specific policy explicit.
|
||||||
|
*/
|
||||||
|
export function isVoiceCompatibleAudio(opts: {
|
||||||
|
contentType?: string | null;
|
||||||
|
fileName?: string | null;
|
||||||
|
}): boolean {
|
||||||
|
return isTelegramVoiceCompatibleAudio(opts);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import JSZip from "jszip";
|
import JSZip from "jszip";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { detectMime, extensionForMime, imageMimeFromFormat, isAudioFileName } from "./mime.js";
|
import {
|
||||||
|
detectMime,
|
||||||
|
extensionForMime,
|
||||||
|
imageMimeFromFormat,
|
||||||
|
isAudioFileName,
|
||||||
|
normalizeMimeType,
|
||||||
|
} from "./mime.js";
|
||||||
|
|
||||||
async function makeOoxmlZip(opts: { mainMime: string; partPath: string }): Promise<Buffer> {
|
async function makeOoxmlZip(opts: { mainMime: string; partPath: string }): Promise<Buffer> {
|
||||||
const zip = new JSZip();
|
const zip = new JSZip();
|
||||||
@@ -110,3 +116,15 @@ describe("isAudioFileName", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("normalizeMimeType", () => {
|
||||||
|
it("normalizes case and strips parameters", () => {
|
||||||
|
expect(normalizeMimeType("Audio/MP4; codecs=mp4a.40.2")).toBe("audio/mp4");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns undefined for empty input", () => {
|
||||||
|
expect(normalizeMimeType(" ")).toBeUndefined();
|
||||||
|
expect(normalizeMimeType(null)).toBeUndefined();
|
||||||
|
expect(normalizeMimeType(undefined)).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ const AUDIO_FILE_EXTENSIONS = new Set([
|
|||||||
".wav",
|
".wav",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
function normalizeHeaderMime(mime?: string | null): string | undefined {
|
export function normalizeMimeType(mime?: string | null): string | undefined {
|
||||||
if (!mime) {
|
if (!mime) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@@ -120,7 +120,7 @@ async function detectMimeImpl(opts: {
|
|||||||
const ext = getFileExtension(opts.filePath);
|
const ext = getFileExtension(opts.filePath);
|
||||||
const extMime = ext ? MIME_BY_EXT[ext] : undefined;
|
const extMime = ext ? MIME_BY_EXT[ext] : undefined;
|
||||||
|
|
||||||
const headerMime = normalizeHeaderMime(opts.headerMime);
|
const headerMime = normalizeMimeType(opts.headerMime);
|
||||||
const sniffed = await sniffMime(opts.buffer);
|
const sniffed = await sniffMime(opts.buffer);
|
||||||
|
|
||||||
// Prefer sniffed types, but don't let generic container types override a more
|
// Prefer sniffed types, but don't let generic container types override a more
|
||||||
@@ -145,10 +145,11 @@ async function detectMimeImpl(opts: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function extensionForMime(mime?: string | null): string | undefined {
|
export function extensionForMime(mime?: string | null): string | undefined {
|
||||||
if (!mime) {
|
const normalized = normalizeMimeType(mime);
|
||||||
|
if (!normalized) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return EXT_BY_MIME[mime.toLowerCase()];
|
return EXT_BY_MIME[normalized];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isGifMedia(opts: {
|
export function isGifMedia(opts: {
|
||||||
|
|||||||
@@ -1,11 +1,4 @@
|
|||||||
import { isVoiceCompatibleAudio } from "../media/audio.js";
|
import { isTelegramVoiceCompatibleAudio } from "../media/audio.js";
|
||||||
|
|
||||||
export function isTelegramVoiceCompatible(opts: {
|
|
||||||
contentType?: string | null;
|
|
||||||
fileName?: string | null;
|
|
||||||
}): boolean {
|
|
||||||
return isVoiceCompatibleAudio(opts);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function resolveTelegramVoiceDecision(opts: {
|
export function resolveTelegramVoiceDecision(opts: {
|
||||||
wantsVoice: boolean;
|
wantsVoice: boolean;
|
||||||
@@ -15,7 +8,7 @@ export function resolveTelegramVoiceDecision(opts: {
|
|||||||
if (!opts.wantsVoice) {
|
if (!opts.wantsVoice) {
|
||||||
return { useVoice: false };
|
return { useVoice: false };
|
||||||
}
|
}
|
||||||
if (isTelegramVoiceCompatible(opts)) {
|
if (isTelegramVoiceCompatibleAudio(opts)) {
|
||||||
return { useVoice: true };
|
return { useVoice: true };
|
||||||
}
|
}
|
||||||
const contentType = opts.contentType ?? "unknown";
|
const contentType = opts.contentType ?? "unknown";
|
||||||
|
|||||||
Reference in New Issue
Block a user