Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🎙️ fix: Prefer Compatible Formats for External STT Recording (#14864)

Marco Berettab2016898a592
diff --git a/client/src/hooks/Input/useSpeechToTextExternal.spec.ts b/client/src/hooks/Input/useSpeechToTextExternal.spec.ts
new file mode 100644
index 0000000000..a48e536e73
--- /dev/null
+++ b/client/src/hooks/Input/useSpeechToTextExternal.spec.ts
@@ -0,0 +1,40 @@
+import { getBestSupportedMimeType } from './useSpeechToTextExternal';
+
+const preferredMimeTypes = [
+ 'audio/ogg;codecs=opus',
+ 'audio/ogg',
+ 'audio/wav',
+ 'audio/webm',
+ 'audio/webm;codecs=opus',
+ 'audio/mp4',
+];
+
+describe('getBestSupportedMimeType', () => {
+ it.each(
+ preferredMimeTypes.map((expected, index) => ({
+ expected,
+ supportedTypes: new Set(preferredMimeTypes.slice(index)),
+ probedTypes: preferredMimeTypes.slice(0, index + 1),
+ })),
+ )(
+ 'selects $expected before lower-priority formats',
+ ({ expected, supportedTypes, probedTypes }) => {
+ const isTypeSupported = jest.fn((type: string) => supportedTypes.has(type));
+
+ expect(getBestSupportedMimeType(isTypeSupported, 'test browser')).toBe(expected);
+ expect(isTypeSupported.mock.calls.map(([type]) => type)).toEqual(probedTypes);
+ },
+ );
+
+ it.each([
+ ['Safari', 'Mozilla/5.0 Version/17.0 Safari/605.1.15', 'audio/mp4'],
+ ['Firefox', 'Mozilla/5.0 Firefox/128.0', 'audio/ogg'],
+ ['Chrome', 'Mozilla/5.0 Chrome/128.0 Safari/537.36', 'audio/webm'],
+ ['an unknown browser', 'test browser', 'audio/webm'],
+ ])('falls back to %s defaults when no format is supported', (_browser, userAgent, expected) => {
+ const isTypeSupported = jest.fn((_type: string) => false);
+
+ expect(getBestSupportedMimeType(isTypeSupported, userAgent)).toBe(expected);
+ expect(isTypeSupported.mock.calls.map(([type]) => type)).toEqual(preferredMimeTypes);
+ });
+});
diff --git a/client/src/hooks/Input/useSpeechToTextExternal.ts b/client/src/hooks/Input/useSpeechToTextExternal.ts
index 8876fffb75..6dc698ceeb 100644
--- a/client/src/hooks/Input/useSpeechToTextExternal.ts
+++ b/client/src/hooks/Input/useSpeechToTextExternal.ts
@@ -4,6 +4,36 @@ import { useToastContext } from '@librechat/client';
import { useSpeechToTextMutation } from '~/data-provider';
import store from '~/store';
+export const getBestSupportedMimeType = (
+ isTypeSupported: (type: string) => boolean = (type) =>
+ typeof MediaRecorder !== 'undefined' && MediaRecorder.isTypeSupported(type),
+ userAgent: string = typeof navigator !== 'undefined' ? navigator.userAgent : '',
+) => {
+ const types = [
+ 'audio/ogg;codecs=opus',
+ 'audio/ogg',
+ 'audio/wav',
+ 'audio/webm',
+ 'audio/webm;codecs=opus',
+ 'audio/mp4',
+ ];
+
+ for (const type of types) {
+ if (isTypeSupported(type)) {
+ return type;
+ }
+ }
+
+ const ua = userAgent.toLowerCase();
+ if (ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1) {
+ return 'audio/mp4';
+ } else if (ua.indexOf('firefox') !== -1) {
+ return 'audio/ogg';
+ }
+
+ return 'audio/webm';
+};
+
const useSpeechToTextExternal = (
setText: (text: string) => void,
onTranscriptionComplete: (text: string) => void,
@@ -46,34 +76,6 @@ const useSpeechToTextExternal = (
},
});
- function getBestSupportedMimeType() {
- const types = [
- 'audio/webm',
- 'audio/webm;codecs=opus',
- 'audio/mp4',
- 'audio/ogg;codecs=opus',
- 'audio/ogg',
- 'audio/wav',
- ];
-
- for (const type of types) {
- if (typeof MediaRecorder !== 'undefined' && MediaRecorder.isTypeSupported(type)) {
- return type;
- }
- }
-
- if (typeof navigator !== 'undefined') {
- const ua = navigator.userAgent.toLowerCase();
- if (ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1) {
- return 'audio/mp4';
- } else if (ua.indexOf('firefox') !== -1) {
- return 'audio/ogg';
- }
- }
-
- return 'audio/webm';
- }
-
const getFileExtension = (mimeType: string) => {
if (mimeType.includes('mp4')) {
return 'm4a';