Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const config = {
host: process.env.HOST || DEFAULT_HOST,

openaiApiKey: process.env.OPENAI_API_KEY || '',
openaiApiVersion: process.env.OPENAI_API_VERSION || process.env.AZURE_OPENAI_API_VERSION || '',
replicateApiToken: process.env.REPLICATE_API_TOKEN || '',
mimoApiKey: process.env.MIMO_API_KEY || '',

Expand Down
107 changes: 107 additions & 0 deletions server/services/openai-whisper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest';

describe('openai-whisper transcribe URL building', () => {
beforeEach(() => {
vi.resetModules();
vi.stubGlobal('fetch', vi.fn());
});

afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
});

it('uses base whisper URL when no API version is configured', async () => {
vi.doMock('../lib/config.js', () => ({
config: { openaiApiKey: 'sk-test', openaiApiVersion: '', language: 'en' },
}));

vi.doMock('../lib/constants.js', () => ({
OPENAI_WHISPER_URL: 'https://api.openai.com/v1/audio/transcriptions',
}));

vi.mocked(fetch).mockResolvedValue(
new Response(JSON.stringify({ text: 'ok' }), { status: 200 }),
);

const { transcribe } = await import('./openai-whisper.js');
const result = await transcribe(Buffer.from('abc'), 'sample.webm');

expect(result).toEqual({ ok: true, text: 'ok' });
expect(fetch).toHaveBeenCalledWith(
'https://api.openai.com/v1/audio/transcriptions',
expect.objectContaining({ method: 'POST' }),
);
});

it('appends api-version when OPENAI_API_VERSION/AZURE_OPENAI_API_VERSION is configured', async () => {
vi.doMock('../lib/config.js', () => ({
config: { openaiApiKey: 'sk-test', openaiApiVersion: '2024-06-01', language: 'en' },
}));

vi.doMock('../lib/constants.js', () => ({
OPENAI_WHISPER_URL:
'https://example.cognitiveservices.azure.com/openai/deployments/whisper/audio/transcriptions',
}));

vi.mocked(fetch).mockResolvedValue(
new Response(JSON.stringify({ text: 'ok' }), { status: 200 }),
);

const { transcribe } = await import('./openai-whisper.js');
const result = await transcribe(Buffer.from('abc'), 'sample.webm');

expect(result).toEqual({ ok: true, text: 'ok' });
expect(fetch).toHaveBeenCalledWith(
'https://example.cognitiveservices.azure.com/openai/deployments/whisper/audio/transcriptions?api-version=2024-06-01',
expect.objectContaining({ method: 'POST' }),
);
});

it('does not override existing api-version query parameter', async () => {
vi.doMock('../lib/config.js', () => ({
config: { openaiApiKey: 'sk-test', openaiApiVersion: '2024-06-01', language: 'en' },
}));

vi.doMock('../lib/constants.js', () => ({
OPENAI_WHISPER_URL:
'https://example.cognitiveservices.azure.com/openai/deployments/whisper/audio/transcriptions?api-version=2025-01-01',
}));

vi.mocked(fetch).mockResolvedValue(
new Response(JSON.stringify({ text: 'ok' }), { status: 200 }),
);

const { transcribe } = await import('./openai-whisper.js');
const result = await transcribe(Buffer.from('abc'), 'sample.webm');

expect(result).toEqual({ ok: true, text: 'ok' });
expect(fetch).toHaveBeenCalledWith(
'https://example.cognitiveservices.azure.com/openai/deployments/whisper/audio/transcriptions?api-version=2025-01-01',
expect.objectContaining({ method: 'POST' }),
);
});

it('uses base whisper URL when API version is whitespace-only', async () => {
vi.doMock('../lib/config.js', () => ({
config: { openaiApiKey: 'sk-test', openaiApiVersion: ' ', language: 'en' },
}));

vi.doMock('../lib/constants.js', () => ({
OPENAI_WHISPER_URL: 'https://api.openai.com/v1/audio/transcriptions',
}));

vi.mocked(fetch).mockResolvedValue(
new Response(JSON.stringify({ text: 'ok' }), { status: 200 }),
);

const { transcribe } = await import('./openai-whisper.js');
const result = await transcribe(Buffer.from('abc'), 'sample.webm');

expect(result).toEqual({ ok: true, text: 'ok' });
expect(fetch).toHaveBeenCalledWith(
'https://api.openai.com/v1/audio/transcriptions',
expect.objectContaining({ method: 'POST' }),
);
});
});
14 changes: 13 additions & 1 deletion server/services/openai-whisper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
import { config } from '../lib/config.js';
import { OPENAI_WHISPER_URL } from '../lib/constants.js';

function getWhisperUrl(): string {
const apiVersion = config.openaiApiVersion?.trim();
if (!apiVersion) return OPENAI_WHISPER_URL;

const url = new URL(OPENAI_WHISPER_URL);
if (!url.searchParams.has('api-version')) {
url.searchParams.set('api-version', apiVersion);
}

return url.toString();
}

export interface WhisperResult {
ok: true;
text: string;
Expand Down Expand Up @@ -55,7 +67,7 @@ export async function transcribe(
footer += `--${boundary}--\r\n`;
const payload = Buffer.concat([header, fileData, Buffer.from(footer)]);

const resp = await fetch(OPENAI_WHISPER_URL, {
const resp = await fetch(getWhisperUrl(), {
method: 'POST',
headers: {
Authorization: `Bearer ${config.openaiApiKey}`,
Expand Down
Loading