refactor: normalize voice-call runtime defaults

This commit is contained in:
Peter Steinberger
2026-03-08 02:49:40 +00:00
parent 5759b93dda
commit 3087893ef9
6 changed files with 151 additions and 30 deletions

View File

@@ -3,6 +3,8 @@ import type { OpenAITTSConfig } from "./tts-openai.js";
import { OpenAITTSProvider } from "./tts-openai.js";
type ProviderInternals = {
model: string;
voice: string;
speed: number;
};
@@ -27,4 +29,15 @@ describe("OpenAITTSProvider constructor defaults", () => {
expect(provider.speed).toBe(1.0);
});
it("treats blank model and voice overrides as unset", () => {
const provider = readProviderInternals({
apiKey: "sk-test", // pragma: allowlist secret
model: " ",
voice: "",
});
expect(provider.model).toBe("gpt-4o-mini-tts");
expect(provider.voice).toBe("coral");
});
});

View File

@@ -66,6 +66,11 @@ export const OPENAI_TTS_VOICES = [
export type OpenAITTSVoice = (typeof OPENAI_TTS_VOICES)[number];
function trimToUndefined(value: string | undefined): string | undefined {
const trimmed = value?.trim();
return trimmed ? trimmed : undefined;
}
/**
* OpenAI TTS Provider for generating speech audio.
*/
@@ -77,13 +82,14 @@ export class OpenAITTSProvider {
private instructions?: string;
constructor(config: OpenAITTSConfig = {}) {
this.apiKey = config.apiKey || process.env.OPENAI_API_KEY || "";
this.apiKey =
trimToUndefined(config.apiKey) ?? trimToUndefined(process.env.OPENAI_API_KEY) ?? "";
// Default to gpt-4o-mini-tts for intelligent realtime applications
this.model = config.model || "gpt-4o-mini-tts";
this.model = trimToUndefined(config.model) ?? "gpt-4o-mini-tts";
// Default to coral - good balance of quality and natural tone
this.voice = (config.voice as OpenAITTSVoice) || "coral";
this.voice = (trimToUndefined(config.voice) as OpenAITTSVoice | undefined) ?? "coral";
this.speed = config.speed ?? 1.0;
this.instructions = config.instructions;
this.instructions = trimToUndefined(config.instructions);
if (!this.apiKey) {
throw new Error("OpenAI API key required (set OPENAI_API_KEY or pass apiKey)");
@@ -105,7 +111,7 @@ export class OpenAITTSProvider {
};
// Add instructions if using gpt-4o-mini-tts model
const effectiveInstructions = instructions || this.instructions;
const effectiveInstructions = trimToUndefined(instructions) ?? this.instructions;
if (effectiveInstructions && this.model.includes("gpt-4o-mini-tts")) {
body.instructions = effectiveInstructions;
}