refactor: generalize voice audio compatibility

This commit is contained in:
Peter Steinberger
2026-04-22 06:58:45 +01:00
parent 8b2ef40775
commit 4d6756d45d
3 changed files with 12 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
import { isTelegramVoiceCompatibleAudio } from "openclaw/plugin-sdk/media-runtime";
import { isVoiceCompatibleAudio } from "openclaw/plugin-sdk/media-runtime";
export function resolveTelegramVoiceDecision(opts: {
wantsVoice: boolean;
@@ -8,7 +8,7 @@ export function resolveTelegramVoiceDecision(opts: {
if (!opts.wantsVoice) {
return { useVoice: false };
}
if (isTelegramVoiceCompatibleAudio(opts)) {
if (isVoiceCompatibleAudio(opts)) {
return { useVoice: true };
}
const contentType = opts.contentType ?? "unknown";

View File

@@ -1,8 +1,8 @@
import { describe, expect, it } from "vitest";
import {
isVoiceCompatibleAudio,
TELEGRAM_VOICE_AUDIO_EXTENSIONS,
TELEGRAM_VOICE_MIME_TYPES,
VOICE_MESSAGE_AUDIO_EXTENSIONS,
VOICE_MESSAGE_MIME_TYPES,
} from "./audio.js";
describe("isVoiceCompatibleAudio", () => {
@@ -28,7 +28,7 @@ describe("isVoiceCompatibleAudio", () => {
{
name: "returns true for supported MIME types",
cases: [
...Array.from(TELEGRAM_VOICE_MIME_TYPES, (contentType) => ({
...Array.from(VOICE_MESSAGE_MIME_TYPES, (contentType) => ({
opts: { contentType, fileName: null },
expected: true,
})),
@@ -38,7 +38,7 @@ describe("isVoiceCompatibleAudio", () => {
},
{
name: "returns true for supported extensions",
cases: Array.from(TELEGRAM_VOICE_AUDIO_EXTENSIONS, (ext) => ({
cases: Array.from(VOICE_MESSAGE_AUDIO_EXTENSIONS, (ext) => ({
opts: { fileName: `voice${ext}` },
expected: true,
})),

View File

@@ -1,14 +1,12 @@
import { normalizeOptionalString } from "../shared/string-coerce.js";
import { getFileExtension, normalizeMimeType } from "./mime.js";
export const TELEGRAM_VOICE_AUDIO_EXTENSIONS = new Set([".oga", ".ogg", ".opus", ".mp3", ".m4a"]);
export const VOICE_MESSAGE_AUDIO_EXTENSIONS = new Set([".oga", ".ogg", ".opus", ".mp3", ".m4a"]);
/**
* MIME types compatible with voice messages.
* Telegram sendVoice supports OGG/Opus, MP3, and M4A.
* https://core.telegram.org/bots/api#sendvoice
*/
export const TELEGRAM_VOICE_MIME_TYPES = new Set([
export const VOICE_MESSAGE_MIME_TYPES = new Set([
"audio/ogg",
"audio/opus",
"audio/mpeg",
@@ -18,12 +16,12 @@ export const TELEGRAM_VOICE_MIME_TYPES = new Set([
"audio/m4a",
]);
export function isTelegramVoiceCompatibleAudio(opts: {
export function isVoiceMessageCompatibleAudio(opts: {
contentType?: string | null;
fileName?: string | null;
}): boolean {
const mime = normalizeMimeType(opts.contentType);
if (mime && TELEGRAM_VOICE_MIME_TYPES.has(mime)) {
if (mime && VOICE_MESSAGE_MIME_TYPES.has(mime)) {
return true;
}
const fileName = normalizeOptionalString(opts.fileName);
@@ -34,16 +32,12 @@ export function isTelegramVoiceCompatibleAudio(opts: {
if (!ext) {
return false;
}
return TELEGRAM_VOICE_AUDIO_EXTENSIONS.has(ext);
return VOICE_MESSAGE_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);
return isVoiceMessageCompatibleAudio(opts);
}