-
-
Notifications
You must be signed in to change notification settings - Fork 80.8k
Expand file tree
/
Copy pathspeech-provider.ts
More file actions
399 lines (381 loc) · 13.2 KB
/
Copy pathspeech-provider.ts
File metadata and controls
399 lines (381 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Openai provider module implements model/runtime integration.
import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-input";
import type {
SpeechDirectiveTokenParseContext,
SpeechProviderConfig,
SpeechProviderOverrides,
SpeechProviderPlugin,
} from "openclaw/plugin-sdk/speech-core";
import { parseSpeechDirectiveNumberOverride } from "openclaw/plugin-sdk/speech-core";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalLowercaseString,
} from "openclaw/plugin-sdk/string-coerce-runtime";
import {
asFiniteNumber,
asObjectRecord,
resolveOpenAIProviderConfigRecord,
trimToUndefined,
} from "./realtime-provider-shared.js";
import {
DEFAULT_OPENAI_BASE_URL,
isValidOpenAIModel,
isValidOpenAIVoice,
normalizeOpenAITtsBaseUrl,
OPENAI_TTS_MODELS,
OPENAI_TTS_VOICES,
openaiTTS,
} from "./tts.js";
const OPENAI_SPEECH_RESPONSE_FORMATS = ["mp3", "opus", "wav"] as const;
const DEFAULT_GENERATED_AUDIO_MAX_BYTES = 16 * 1024 * 1024;
type OpenAiSpeechResponseFormat = (typeof OPENAI_SPEECH_RESPONSE_FORMATS)[number];
type OpenAITtsProviderConfig = {
apiKey?: string;
baseUrl: string;
model: string;
voice: string;
speed?: number;
instructions?: string;
responseFormat?: OpenAiSpeechResponseFormat;
extraBody?: Record<string, unknown>;
};
type OpenAITtsProviderOverrides = {
model?: string;
voice?: string;
speed?: number;
};
function normalizeOpenAISpeechResponseFormat(
value: unknown,
): OpenAiSpeechResponseFormat | undefined {
const next = normalizeOptionalLowercaseString(value);
if (!next) {
return undefined;
}
if (
OPENAI_SPEECH_RESPONSE_FORMATS.includes(next as (typeof OPENAI_SPEECH_RESPONSE_FORMATS)[number])
) {
return next as OpenAiSpeechResponseFormat;
}
throw new Error(`Invalid OpenAI speech responseFormat: ${next}`);
}
function isGroqSpeechBaseUrl(baseUrl: string): boolean {
try {
const hostname = normalizeLowercaseStringOrEmpty(new URL(baseUrl).hostname);
return hostname === "groq.com" || hostname.endsWith(".groq.com");
} catch {
return false;
}
}
function resolveSpeechResponseFormat(
baseUrl: string,
target: "audio-file" | "voice-note" | "telephony",
configuredFormat?: OpenAiSpeechResponseFormat,
): OpenAiSpeechResponseFormat {
if (configuredFormat) {
return configuredFormat;
}
if (isGroqSpeechBaseUrl(baseUrl)) {
return "wav";
}
return target === "voice-note" ? "opus" : "mp3";
}
function responseFormatToFileExtension(
format: OpenAiSpeechResponseFormat,
): ".mp3" | ".opus" | ".wav" {
switch (format) {
case "opus":
return ".opus";
case "wav":
return ".wav";
default:
return ".mp3";
}
}
function readExtraBody(value: unknown): Record<string, unknown> | undefined {
const body = asObjectRecord(value);
if (!body || Object.keys(body).length === 0) {
return undefined;
}
return body;
}
function normalizeOpenAISpeechSpeed(value: unknown, baseUrl?: string): number | undefined {
const speed = asFiniteNumber(value);
if (speed === undefined) {
return undefined;
}
if (baseUrl !== undefined && normalizeOpenAITtsBaseUrl(baseUrl) !== DEFAULT_OPENAI_BASE_URL) {
return speed;
}
return speed >= 0.25 && speed <= 4 ? speed : undefined;
}
function normalizeOpenAIProviderConfig(
rawConfig: Record<string, unknown>,
): OpenAITtsProviderConfig {
const raw = resolveOpenAIProviderConfigRecord(rawConfig);
const extraBody = readExtraBody(raw?.extraBody) ?? readExtraBody(raw?.extra_body);
const baseUrl = normalizeOpenAITtsBaseUrl(
trimToUndefined(raw?.baseUrl) ??
trimToUndefined(process.env.OPENAI_TTS_BASE_URL) ??
DEFAULT_OPENAI_BASE_URL,
);
return {
apiKey: normalizeResolvedSecretInputString({
value: raw?.apiKey,
path: "messages.tts.providers.openai.apiKey",
}),
baseUrl,
model: trimToUndefined(raw?.model) ?? "gpt-4o-mini-tts",
voice: trimToUndefined(raw?.voice) ?? "coral",
speed: normalizeOpenAISpeechSpeed(raw?.speed, baseUrl),
instructions: trimToUndefined(raw?.instructions),
responseFormat: normalizeOpenAISpeechResponseFormat(raw?.responseFormat),
extraBody,
};
}
function readOpenAIProviderConfig(config: SpeechProviderConfig): OpenAITtsProviderConfig {
const normalized = normalizeOpenAIProviderConfig({});
return {
apiKey: trimToUndefined(config.apiKey) ?? normalized.apiKey,
baseUrl: trimToUndefined(config.baseUrl) ?? normalized.baseUrl,
model: trimToUndefined(config.model) ?? normalized.model,
voice: trimToUndefined(config.voice) ?? normalized.voice,
speed:
normalizeOpenAISpeechSpeed(
config.speed,
trimToUndefined(config.baseUrl) ?? normalized.baseUrl,
) ?? normalized.speed,
instructions: trimToUndefined(config.instructions) ?? normalized.instructions,
responseFormat:
normalizeOpenAISpeechResponseFormat(config.responseFormat) ?? normalized.responseFormat,
extraBody: readExtraBody(config.extraBody) ?? readExtraBody(config.extra_body),
};
}
function readOpenAIOverrides(
overrides: SpeechProviderOverrides | undefined,
baseUrl: string,
): OpenAITtsProviderOverrides {
if (!overrides) {
return {};
}
return {
model: trimToUndefined(overrides.model),
voice: trimToUndefined(overrides.voice),
speed: normalizeOpenAISpeechSpeed(overrides.speed, baseUrl),
};
}
function resolveGeneratedAudioMaxBytes(req: {
cfg: { agents?: { defaults?: { mediaMaxMb?: number } } };
}): number {
const configured = req.cfg.agents?.defaults?.mediaMaxMb;
if (typeof configured === "number" && Number.isFinite(configured) && configured > 0) {
return Math.floor(configured * 1024 * 1024);
}
return DEFAULT_GENERATED_AUDIO_MAX_BYTES;
}
function renderOpenAITtsPersonaInstructions(req: {
label?: string;
prompt?: {
profile?: string;
scene?: string;
sampleContext?: string;
style?: string;
accent?: string;
pacing?: string;
constraints?: string[];
};
}): string | undefined {
const prompt = req.prompt;
if (!prompt) {
return undefined;
}
const lines = [
req.label ? `Persona: ${req.label}` : undefined,
prompt.profile ? `Profile: ${prompt.profile}` : undefined,
prompt.scene ? `Scene: ${prompt.scene}` : undefined,
prompt.style ? `Style: ${prompt.style}` : undefined,
prompt.accent ? `Accent: ${prompt.accent}` : undefined,
prompt.pacing ? `Pacing: ${prompt.pacing}` : undefined,
prompt.sampleContext ? `Sample context: ${prompt.sampleContext}` : undefined,
...(prompt.constraints ?? []).map((constraint) => `Constraint: ${constraint}`),
]
.map((line) => trimToUndefined(line))
.filter((line): line is string => Boolean(line));
return lines.length > 0 ? lines.join("\n") : undefined;
}
function isCustomOpenAITtsBaseUrl(baseUrl: string | undefined): boolean {
if (baseUrl !== undefined) {
return normalizeOpenAITtsBaseUrl(baseUrl) !== DEFAULT_OPENAI_BASE_URL;
}
return normalizeOpenAITtsBaseUrl(process.env.OPENAI_TTS_BASE_URL) !== DEFAULT_OPENAI_BASE_URL;
}
function parseDirectiveToken(ctx: SpeechDirectiveTokenParseContext): {
handled: boolean;
overrides?: SpeechProviderOverrides;
warnings?: string[];
} {
const baseUrl = trimToUndefined(asObjectRecord(ctx.providerConfig)?.baseUrl);
switch (ctx.key) {
case "voice":
case "openai_voice":
case "openaivoice":
if (!ctx.policy.allowVoice) {
return { handled: true };
}
if (!isValidOpenAIVoice(ctx.value, baseUrl)) {
return { handled: true, warnings: [`invalid OpenAI voice "${ctx.value}"`] };
}
return { handled: true, overrides: { voice: ctx.value } };
case "model":
case "openai_model":
case "openaimodel":
if (!ctx.policy.allowModelId) {
return { handled: true };
}
if (!isValidOpenAIModel(ctx.value, baseUrl)) {
return { handled: false };
}
return { handled: true, overrides: { model: ctx.value } };
case "speed":
case "openai_speed":
case "openaispeed": {
const customBaseUrl = isCustomOpenAITtsBaseUrl(baseUrl);
return parseSpeechDirectiveNumberOverride({
ctx,
overrideKey: "speed",
range: customBaseUrl ? {} : { min: 0.25, max: 4 },
warning: (value) =>
customBaseUrl
? `invalid OpenAI-compatible speed "${value}"`
: `invalid OpenAI speed "${value}" (0.25-4.0)`,
});
}
default:
return { handled: false };
}
}
export function buildOpenAISpeechProvider(): SpeechProviderPlugin {
return {
id: "openai",
label: "OpenAI",
autoSelectOrder: 10,
defaultModel: OPENAI_TTS_MODELS[0],
models: OPENAI_TTS_MODELS,
voices: OPENAI_TTS_VOICES,
resolveConfig: ({ rawConfig }) => normalizeOpenAIProviderConfig(rawConfig),
parseDirectiveToken,
resolveTalkConfig: ({ baseTtsConfig, talkProviderConfig }) => {
const base = normalizeOpenAIProviderConfig(baseTtsConfig);
const responseFormat = normalizeOpenAISpeechResponseFormat(talkProviderConfig.responseFormat);
const baseUrl = trimToUndefined(talkProviderConfig.baseUrl) ?? base.baseUrl;
const speed = normalizeOpenAISpeechSpeed(talkProviderConfig.speed, baseUrl);
return {
...base,
...(talkProviderConfig.apiKey === undefined
? {}
: {
apiKey: normalizeResolvedSecretInputString({
value: talkProviderConfig.apiKey,
path: "talk.providers.openai.apiKey",
}),
}),
...(trimToUndefined(talkProviderConfig.baseUrl) == null ? {} : { baseUrl }),
...(trimToUndefined(talkProviderConfig.modelId) == null
? {}
: { model: trimToUndefined(talkProviderConfig.modelId) }),
...(trimToUndefined(talkProviderConfig.voiceId) == null
? {}
: { voice: trimToUndefined(talkProviderConfig.voiceId) }),
...(speed == null ? {} : { speed }),
...(trimToUndefined(talkProviderConfig.instructions) == null
? {}
: { instructions: trimToUndefined(talkProviderConfig.instructions) }),
...(responseFormat == null ? {} : { responseFormat }),
};
},
resolveTalkOverrides: ({ params }) => ({
...(trimToUndefined(params.voiceId) == null
? {}
: { voice: trimToUndefined(params.voiceId) }),
...(trimToUndefined(params.modelId) == null
? {}
: { model: trimToUndefined(params.modelId) }),
...(asFiniteNumber(params.speed) == null ? {} : { speed: asFiniteNumber(params.speed) }),
}),
listVoices: async () => OPENAI_TTS_VOICES.map((voice) => ({ id: voice, name: voice })),
isConfigured: ({ providerConfig }) =>
Boolean(readOpenAIProviderConfig(providerConfig).apiKey || process.env.OPENAI_API_KEY),
prepareSynthesis: (ctx) => {
const config = readOpenAIProviderConfig(ctx.providerConfig);
if (config.instructions) {
return undefined;
}
const instructions = renderOpenAITtsPersonaInstructions({
label: ctx.persona?.label ?? ctx.persona?.id,
prompt: ctx.persona?.prompt,
});
return instructions
? {
providerConfig: {
instructions,
},
}
: undefined;
},
synthesize: async (req) => {
const config = readOpenAIProviderConfig(req.providerConfig);
const overrides = readOpenAIOverrides(req.providerOverrides, config.baseUrl);
const apiKey = config.apiKey || process.env.OPENAI_API_KEY;
if (!apiKey) {
throw new Error("OpenAI API key missing");
}
const responseFormat = resolveSpeechResponseFormat(
config.baseUrl,
req.target,
config.responseFormat,
);
const audioBuffer = await openaiTTS({
text: req.text,
apiKey,
baseUrl: config.baseUrl,
model: overrides.model ?? config.model,
voice: overrides.voice ?? config.voice,
speed: overrides.speed ?? config.speed,
instructions: config.instructions,
responseFormat,
extraBody: config.extraBody,
timeoutMs: req.timeoutMs,
maxBytes: resolveGeneratedAudioMaxBytes(req),
});
return {
audioBuffer,
outputFormat: responseFormat,
fileExtension: responseFormatToFileExtension(responseFormat),
voiceCompatible: req.target === "voice-note" && responseFormat === "opus",
};
},
synthesizeTelephony: async (req) => {
const config = readOpenAIProviderConfig(req.providerConfig);
const overrides = readOpenAIOverrides(req.providerOverrides, config.baseUrl);
const apiKey = config.apiKey || process.env.OPENAI_API_KEY;
if (!apiKey) {
throw new Error("OpenAI API key missing");
}
const outputFormat = "pcm";
const sampleRate = 24_000;
const audioBuffer = await openaiTTS({
text: req.text,
apiKey,
baseUrl: config.baseUrl,
model: overrides.model ?? config.model,
voice: overrides.voice ?? config.voice,
speed: overrides.speed ?? config.speed,
instructions: config.instructions,
responseFormat: outputFormat,
extraBody: config.extraBody,
timeoutMs: req.timeoutMs,
maxBytes: resolveGeneratedAudioMaxBytes(req),
});
return { audioBuffer, outputFormat, sampleRate };
},
};
}