@providerkit/core 0.27.2 → 0.28.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/presets.d.ts +50 -0
- package/dist/presets.d.ts.map +1 -1
- package/dist/presets.js +82 -0
- package/dist/presets.js.map +1 -1
- package/dist/providers/gemini-speech.d.ts +26 -0
- package/dist/providers/gemini-speech.d.ts.map +1 -0
- package/dist/providers/gemini-speech.js +171 -0
- package/dist/providers/gemini-speech.js.map +1 -0
- package/dist/providers/openai-speech.d.ts +23 -0
- package/dist/providers/openai-speech.d.ts.map +1 -0
- package/dist/providers/openai-speech.js +86 -0
- package/dist/providers/openai-speech.js.map +1 -0
- package/dist/providers/speech-factory.d.ts +35 -0
- package/dist/providers/speech-factory.d.ts.map +1 -0
- package/dist/providers/speech-factory.js +47 -0
- package/dist/providers/speech-factory.js.map +1 -0
- package/dist/speech-core.d.ts +184 -0
- package/dist/speech-core.d.ts.map +1 -0
- package/dist/speech-core.js +349 -0
- package/dist/speech-core.js.map +1 -0
- package/dist/speech.d.ts +6 -0
- package/dist/speech.d.ts.map +1 -0
- package/dist/speech.js +12 -0
- package/dist/speech.js.map +1 -0
- package/dist/transport.d.ts +42 -0
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +154 -11
- package/dist/transport.js.map +1 -1
- package/package.json +6 -1
- package/src/index.ts +14 -1
- package/src/presets.ts +113 -0
- package/src/providers/gemini-speech.ts +234 -0
- package/src/providers/openai-speech.ts +125 -0
- package/src/providers/speech-factory.ts +85 -0
- package/src/speech-core.ts +507 -0
- package/src/speech.ts +24 -0
- package/src/transport.ts +185 -11
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// The Gemini speech wire: POST /v1beta/interactions, JSON in, JSON out with the audio
|
|
2
|
+
// as base64. One request per call; speech-core.ts says why.
|
|
3
|
+
//
|
|
4
|
+
// The request is the one an adopter has already run against the live API. Fields it
|
|
5
|
+
// has not proven are not sent: `mime_type` rides only on a raw PCM request, and
|
|
6
|
+
// `scripts/probe-speech.ts` is how a maintainer settles the rest with a real key.
|
|
7
|
+
import {
|
|
8
|
+
asRecord,
|
|
9
|
+
decodeBase64,
|
|
10
|
+
describeAudio,
|
|
11
|
+
inBandFailure,
|
|
12
|
+
prepareSpeech,
|
|
13
|
+
resolveLimits,
|
|
14
|
+
synthesizeOnce,
|
|
15
|
+
unusable,
|
|
16
|
+
type SpeechFormat,
|
|
17
|
+
type SpeechLimits,
|
|
18
|
+
type SpeechProvider,
|
|
19
|
+
type SpeechUsage,
|
|
20
|
+
} from "../speech-core.ts";
|
|
21
|
+
import { apiUrl, postBytes, versionedPath } from "../transport.ts";
|
|
22
|
+
|
|
23
|
+
export interface GeminiSpeechConfig extends SpeechLimits {
|
|
24
|
+
apiKey: string;
|
|
25
|
+
model: string;
|
|
26
|
+
/** Default `https://generativelanguage.googleapis.com`. */
|
|
27
|
+
baseUrl?: string;
|
|
28
|
+
/** Default `/v1beta/interactions`. */
|
|
29
|
+
path?: string;
|
|
30
|
+
/** The preset id. Names the provider in errors and on `SpeechProvider.id`. Default "google". */
|
|
31
|
+
id?: string;
|
|
32
|
+
/** Used when a call names no voice. */
|
|
33
|
+
voice?: string;
|
|
34
|
+
/** "wav" (the default) or "pcm". Gemini makes no other format. */
|
|
35
|
+
format?: SpeechFormat;
|
|
36
|
+
/**
|
|
37
|
+
* Caps the audio tokens generated. Not sent unless set. Google enforces it (checked live on
|
|
38
|
+
* 2026-11-03): a clip cut by it comes back `incomplete`, which is reported as an error that
|
|
39
|
+
* carries the usage billed. The text limit is still the bound on cost you can rely on.
|
|
40
|
+
*/
|
|
41
|
+
maxOutputTokens?: number;
|
|
42
|
+
headers?: Record<string, string>;
|
|
43
|
+
/** Swapped in tests, or to route through a proxy. Defaults to global fetch. */
|
|
44
|
+
fetchImpl?: typeof fetch;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const FORMATS: readonly SpeechFormat[] = ["wav", "pcm"];
|
|
48
|
+
|
|
49
|
+
/** The rate a raw PCM request asks for, and the only one this adapter asks for. */
|
|
50
|
+
const PCM_RATE = 24_000;
|
|
51
|
+
|
|
52
|
+
const count = (value: unknown): number | undefined =>
|
|
53
|
+
typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : undefined;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The provider's own token counts, or nothing. Counts that do not add up are
|
|
57
|
+
* dropped whole: a wrong number in a ledger is worse than a missing one.
|
|
58
|
+
*/
|
|
59
|
+
function readUsage(raw: unknown): SpeechUsage | undefined {
|
|
60
|
+
const usage = asRecord(raw);
|
|
61
|
+
const input = count(usage?.total_input_tokens);
|
|
62
|
+
const output = count(usage?.total_output_tokens);
|
|
63
|
+
const total = count(usage?.total_tokens);
|
|
64
|
+
if (input === undefined || output === undefined || total === undefined) return undefined;
|
|
65
|
+
if (!Number.isSafeInteger(input + output) || total < input + output) return undefined;
|
|
66
|
+
// The cached count is a part of the input count and is not what a charge is made of. One that
|
|
67
|
+
// cannot be right (a string, a negative, more than the input) reads as none cached, because
|
|
68
|
+
// the type needs a number and none cached never understates what was billed. The counts a
|
|
69
|
+
// charge is made of stay. Absent also means none.
|
|
70
|
+
const reported = usage?.total_cached_tokens === undefined ? 0 : count(usage.total_cached_tokens);
|
|
71
|
+
return {
|
|
72
|
+
inputTokens: input,
|
|
73
|
+
cachedInputTokens: reported !== undefined && reported <= input ? reported : 0,
|
|
74
|
+
outputTokens: output,
|
|
75
|
+
totalTokens: total,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function createGeminiSpeechProvider(config: GeminiSpeechConfig): SpeechProvider {
|
|
80
|
+
const id = config.id ?? "google";
|
|
81
|
+
const provider = `speech:${id}`;
|
|
82
|
+
const baseUrl = config.baseUrl ?? "https://generativelanguage.googleapis.com";
|
|
83
|
+
const limits = resolveLimits(config);
|
|
84
|
+
if (config.maxOutputTokens !== undefined && (count(config.maxOutputTokens) ?? 0) < 1) {
|
|
85
|
+
throw new RangeError("[providerkit] maxOutputTokens must be a positive integer");
|
|
86
|
+
}
|
|
87
|
+
// The reply is JSON with the audio as base64: four characters for every three bytes,
|
|
88
|
+
// plus a bounded envelope around it.
|
|
89
|
+
const maxBase64 = Math.ceil(limits.maxAudioBytes / 3) * 4;
|
|
90
|
+
return {
|
|
91
|
+
id,
|
|
92
|
+
model: config.model,
|
|
93
|
+
async synthesize(request) {
|
|
94
|
+
const { text, voice, format } = prepareSpeech(provider, request, {
|
|
95
|
+
apiKey: config.apiKey,
|
|
96
|
+
voice: config.voice,
|
|
97
|
+
format: config.format,
|
|
98
|
+
maxInputChars: limits.maxInputChars,
|
|
99
|
+
formats: FORMATS,
|
|
100
|
+
});
|
|
101
|
+
return synthesizeOnce(provider, limits.timeoutMs, request, async (signal) => {
|
|
102
|
+
const { bytes, contentType } = await postBytes({
|
|
103
|
+
url: apiUrl(baseUrl, config.path ?? versionedPath(baseUrl, "/interactions", "v1beta")),
|
|
104
|
+
headers: {
|
|
105
|
+
...(config.apiKey ? { "x-goog-api-key": config.apiKey } : {}),
|
|
106
|
+
...config.headers,
|
|
107
|
+
},
|
|
108
|
+
body: {
|
|
109
|
+
model: config.model,
|
|
110
|
+
// Do not keep the person's text on the provider's side.
|
|
111
|
+
store: false,
|
|
112
|
+
input: [{ type: "user_input", content: [{ type: "text", text }] }],
|
|
113
|
+
response_format:
|
|
114
|
+
format === "pcm"
|
|
115
|
+
? { type: "audio", mime_type: "audio/l16", sample_rate: PCM_RATE }
|
|
116
|
+
: { type: "audio" },
|
|
117
|
+
generation_config: {
|
|
118
|
+
...(config.maxOutputTokens ? { max_output_tokens: config.maxOutputTokens } : {}),
|
|
119
|
+
// ponytail: Google's speech guide writes `speech_config`, its API reference writes
|
|
120
|
+
// `structured_speech_config`. This is the spelling that has run live. One field is
|
|
121
|
+
// the whole fix if the probe ever shows it stale.
|
|
122
|
+
speech_config: [{ voice }],
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
provider,
|
|
126
|
+
signal,
|
|
127
|
+
redirect: "error",
|
|
128
|
+
maxBytes: maxBase64 + 64 * 1024,
|
|
129
|
+
redactErrors: true,
|
|
130
|
+
...(config.fetchImpl ? { fetchImpl: config.fetchImpl } : {}),
|
|
131
|
+
});
|
|
132
|
+
const envelope = contentType === "application/json" ? parseEnvelope(bytes) : undefined;
|
|
133
|
+
if (!envelope)
|
|
134
|
+
throw unusable(provider, "unknown", "bad_audio", "the reply is not a JSON object");
|
|
135
|
+
// From here on the provider has answered, so it has billed: every failure
|
|
136
|
+
// below carries the usage it stated.
|
|
137
|
+
const usage = readUsage(envelope.usage);
|
|
138
|
+
// `error: null` is "no error", as it is in most JSON APIs: it must not turn a good clip away.
|
|
139
|
+
if (envelope.error !== undefined && envelope.error !== null)
|
|
140
|
+
throw inBandFailure(provider, envelope, usage);
|
|
141
|
+
const status = envelope.status;
|
|
142
|
+
if (status === "incomplete") {
|
|
143
|
+
throw unusable(
|
|
144
|
+
provider,
|
|
145
|
+
"invalid",
|
|
146
|
+
"incomplete",
|
|
147
|
+
"the audio stopped before it finished: shorten the text or raise maxOutputTokens",
|
|
148
|
+
usage,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
if (status === "failed" || status === "cancelled") {
|
|
152
|
+
const reported = Array.isArray(envelope.errors) ? envelope.errors[0] : undefined;
|
|
153
|
+
throw reported !== undefined
|
|
154
|
+
? inBandFailure(provider, reported, usage)
|
|
155
|
+
: unusable(provider, "unknown", status, `the interaction ${status}`, usage);
|
|
156
|
+
}
|
|
157
|
+
if (status !== "completed") {
|
|
158
|
+
throw unusable(
|
|
159
|
+
provider,
|
|
160
|
+
"unknown",
|
|
161
|
+
"unexpected_status",
|
|
162
|
+
"the interaction did not complete",
|
|
163
|
+
usage,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
const block = lastAudioBlock(envelope);
|
|
167
|
+
if (!block)
|
|
168
|
+
throw unusable(provider, "overload", "empty_audio", "the reply holds no audio", usage);
|
|
169
|
+
// A link instead of data is refused and never fetched: a reply must not choose
|
|
170
|
+
// which address this package calls next.
|
|
171
|
+
if (typeof block.data !== "string" || block.data === "") {
|
|
172
|
+
throw unusable(
|
|
173
|
+
provider,
|
|
174
|
+
"unknown",
|
|
175
|
+
"bad_audio",
|
|
176
|
+
"the audio block has no inline data",
|
|
177
|
+
usage,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
const tooLarge = () =>
|
|
181
|
+
unusable(
|
|
182
|
+
provider,
|
|
183
|
+
"invalid",
|
|
184
|
+
"response_too_large",
|
|
185
|
+
"the audio is larger than the limit",
|
|
186
|
+
usage,
|
|
187
|
+
);
|
|
188
|
+
if (block.data.length > maxBase64) throw tooLarge();
|
|
189
|
+
const audio = decodeBase64(block.data);
|
|
190
|
+
if (!audio)
|
|
191
|
+
throw unusable(provider, "unknown", "bad_audio", "the audio is not valid base64", usage);
|
|
192
|
+
if (audio.byteLength > limits.maxAudioBytes) throw tooLarge();
|
|
193
|
+
const result = describeAudio(provider, audio, format, {
|
|
194
|
+
model: config.model,
|
|
195
|
+
voice,
|
|
196
|
+
characters: text.length,
|
|
197
|
+
...(usage ? { usage } : {}),
|
|
198
|
+
// Raw PCM has no header. The rate was in the request, and the block may state
|
|
199
|
+
// the channel count. ponytail: these two block field names come from the API
|
|
200
|
+
// reference and are unverified, which is why a wrong value is ignored.
|
|
201
|
+
...(format === "pcm" ? { sampleRate: count(block.sample_rate) || PCM_RATE } : {}),
|
|
202
|
+
...(format === "pcm" && count(block.channels) ? { channels: count(block.channels) } : {}),
|
|
203
|
+
});
|
|
204
|
+
signal.throwIfAborted();
|
|
205
|
+
return result;
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** The last audio block across the `model_output` steps wins: an earlier invalid block
|
|
212
|
+
* is never preferred over it. */
|
|
213
|
+
function lastAudioBlock(envelope: Record<string, unknown>): Record<string, unknown> | undefined {
|
|
214
|
+
const steps: unknown[] = Array.isArray(envelope.steps) ? envelope.steps : [];
|
|
215
|
+
let block: Record<string, unknown> | undefined;
|
|
216
|
+
for (const step of steps) {
|
|
217
|
+
const record = asRecord(step);
|
|
218
|
+
if (record?.type !== "model_output" || !Array.isArray(record.content)) continue;
|
|
219
|
+
const parts: unknown[] = record.content;
|
|
220
|
+
for (const part of parts) {
|
|
221
|
+
const candidate = asRecord(part);
|
|
222
|
+
if (candidate?.type === "audio") block = candidate;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return block;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function parseEnvelope(bytes: Uint8Array): Record<string, unknown> | undefined {
|
|
229
|
+
try {
|
|
230
|
+
return asRecord(JSON.parse(new TextDecoder().decode(bytes)));
|
|
231
|
+
} catch {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// The OpenAI speech wire: POST {base}/audio/speech, JSON in, audio bytes out.
|
|
2
|
+
// Serves OpenAI, OpenRouter and any server that copies the route (a local Kokoro,
|
|
3
|
+
// a proxy). One request per call; speech-core.ts says why.
|
|
4
|
+
import { attributionHeaders, type Attribution } from "../attribution.ts";
|
|
5
|
+
import {
|
|
6
|
+
asRecord,
|
|
7
|
+
describeAudio,
|
|
8
|
+
inBandFailure,
|
|
9
|
+
prepareSpeech,
|
|
10
|
+
resolveLimits,
|
|
11
|
+
sniffFormat,
|
|
12
|
+
SPEECH_FORMATS,
|
|
13
|
+
synthesizeOnce,
|
|
14
|
+
unusable,
|
|
15
|
+
type SpeechError,
|
|
16
|
+
type SpeechFormat,
|
|
17
|
+
type SpeechLimits,
|
|
18
|
+
type SpeechProvider,
|
|
19
|
+
} from "../speech-core.ts";
|
|
20
|
+
import { apiUrl, postBytes, versionedPath } from "../transport.ts";
|
|
21
|
+
|
|
22
|
+
export interface OpenAISpeechConfig extends SpeechLimits, Attribution {
|
|
23
|
+
/** "" sends no Authorization header, for a local server that wants none. */
|
|
24
|
+
apiKey: string;
|
|
25
|
+
model: string;
|
|
26
|
+
/** Default `https://api.openai.com`. */
|
|
27
|
+
baseUrl?: string;
|
|
28
|
+
/** Default `/v1/audio/speech`, or `/audio/speech` when the base already ends in a version. */
|
|
29
|
+
path?: string;
|
|
30
|
+
/** The preset id. Names the provider in errors and on `SpeechProvider.id`. Default "openai". */
|
|
31
|
+
id?: string;
|
|
32
|
+
/** Used when a call names no voice. */
|
|
33
|
+
voice?: string;
|
|
34
|
+
/** Used when a call names no format. Default "wav". It is always sent: each host's own
|
|
35
|
+
* default differs (mp3, wav, pcm), and "I never asked" must not choose for the caller. */
|
|
36
|
+
format?: SpeechFormat;
|
|
37
|
+
headers?: Record<string, string>;
|
|
38
|
+
/** Swapped in tests, or to route through a proxy. Defaults to global fetch. */
|
|
39
|
+
fetchImpl?: typeof fetch;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// A JSON or text body is an error that arrived with a 200, not audio.
|
|
43
|
+
const TEXT_TYPE = /^(application\/(?:[\w.+-]*\+)?json|text\/)/;
|
|
44
|
+
|
|
45
|
+
/** First byte that is not blank or a control character is "{". */
|
|
46
|
+
const startsLikeJson = (bytes: Uint8Array) => bytes.subarray(0, 16).find((b) => b > 0x20) === 0x7b;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A 200 reply that is not audio: a JSON error, or JSON or text where audio belongs.
|
|
50
|
+
* Audio is named by its bytes first, so a server that sends audio under a wrong
|
|
51
|
+
* content type still gets through. `undefined` means the bytes may be audio.
|
|
52
|
+
*/
|
|
53
|
+
function notAudio(
|
|
54
|
+
provider: string,
|
|
55
|
+
bytes: Uint8Array,
|
|
56
|
+
contentType: string,
|
|
57
|
+
): SpeechError | undefined {
|
|
58
|
+
if (bytes.byteLength === 0 || sniffFormat(bytes)) return undefined;
|
|
59
|
+
const textual = TEXT_TYPE.test(contentType);
|
|
60
|
+
const body = textual || startsLikeJson(bytes) ? parseObject(bytes) : undefined;
|
|
61
|
+
if (body) {
|
|
62
|
+
return body.error !== undefined || body.errors !== undefined
|
|
63
|
+
? inBandFailure(provider, body)
|
|
64
|
+
: unusable(provider, "unknown", "bad_audio", "the reply is JSON and holds no audio");
|
|
65
|
+
}
|
|
66
|
+
return textual
|
|
67
|
+
? unusable(provider, "unknown", "bad_audio", "the reply is text, not audio")
|
|
68
|
+
: undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Only the head is read: an error object sits at the start of a body. */
|
|
72
|
+
function parseObject(bytes: Uint8Array): Record<string, unknown> | undefined {
|
|
73
|
+
try {
|
|
74
|
+
return asRecord(JSON.parse(new TextDecoder().decode(bytes.subarray(0, 64 * 1024))));
|
|
75
|
+
} catch {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function createOpenAISpeechProvider(config: OpenAISpeechConfig): SpeechProvider {
|
|
81
|
+
const id = config.id ?? "openai";
|
|
82
|
+
const provider = `speech:${id}`;
|
|
83
|
+
const baseUrl = config.baseUrl ?? "https://api.openai.com";
|
|
84
|
+
const limits = resolveLimits(config);
|
|
85
|
+
return {
|
|
86
|
+
id,
|
|
87
|
+
model: config.model,
|
|
88
|
+
async synthesize(request) {
|
|
89
|
+
const { text, voice, format } = prepareSpeech(provider, request, {
|
|
90
|
+
apiKey: config.apiKey,
|
|
91
|
+
voice: config.voice,
|
|
92
|
+
format: config.format,
|
|
93
|
+
maxInputChars: limits.maxInputChars,
|
|
94
|
+
formats: SPEECH_FORMATS,
|
|
95
|
+
});
|
|
96
|
+
return synthesizeOnce(provider, limits.timeoutMs, request, async (signal) => {
|
|
97
|
+
const { bytes, contentType } = await postBytes({
|
|
98
|
+
url: apiUrl(baseUrl, config.path ?? versionedPath(baseUrl, "/audio/speech")),
|
|
99
|
+
headers: {
|
|
100
|
+
...(config.apiKey ? { authorization: `Bearer ${config.apiKey}` } : {}),
|
|
101
|
+
...attributionHeaders(config, config.headers),
|
|
102
|
+
...config.headers,
|
|
103
|
+
},
|
|
104
|
+
body: { model: config.model, input: text, voice, response_format: format },
|
|
105
|
+
provider,
|
|
106
|
+
signal,
|
|
107
|
+
redirect: "error",
|
|
108
|
+
maxBytes: limits.maxAudioBytes,
|
|
109
|
+
redactErrors: true,
|
|
110
|
+
...(config.fetchImpl ? { fetchImpl: config.fetchImpl } : {}),
|
|
111
|
+
});
|
|
112
|
+
// A failure inside a 200 is still a failure (invariant 11).
|
|
113
|
+
const problem = notAudio(provider, bytes, contentType);
|
|
114
|
+
if (problem) throw problem;
|
|
115
|
+
const result = describeAudio(provider, bytes, format, {
|
|
116
|
+
model: config.model,
|
|
117
|
+
voice,
|
|
118
|
+
characters: text.length,
|
|
119
|
+
});
|
|
120
|
+
signal.throwIfAborted();
|
|
121
|
+
return result;
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// The speech twin of `createPresetProvider`: name a preset, hand over a key, get a
|
|
2
|
+
// speech provider. The preset row (`ProviderPreset.speech`) picks the wire.
|
|
3
|
+
import {
|
|
4
|
+
PROVIDER_PRESETS,
|
|
5
|
+
SPEECH_PRESET_IDS,
|
|
6
|
+
type ProviderPreset,
|
|
7
|
+
type ProviderPresetId,
|
|
8
|
+
} from "../presets.ts";
|
|
9
|
+
import type { Attribution } from "../attribution.ts";
|
|
10
|
+
import type { SpeechFormat, SpeechLimits, SpeechProvider } from "../speech-core.ts";
|
|
11
|
+
import { createGeminiSpeechProvider } from "./gemini-speech.ts";
|
|
12
|
+
import { createOpenAISpeechProvider } from "./openai-speech.ts";
|
|
13
|
+
|
|
14
|
+
export interface SpeechProviderConfig extends SpeechLimits, Attribution {
|
|
15
|
+
apiKey: string;
|
|
16
|
+
/** Absent: the preset's `speech.defaultModel`. A preset without one needs this. */
|
|
17
|
+
model?: string;
|
|
18
|
+
/** Used when a call names no voice. Absent: the preset's `speech.defaultVoice`. */
|
|
19
|
+
voice?: string;
|
|
20
|
+
/** Used when a call names no format. Absent: the preset's `speech.defaultFormat`, else "wav". */
|
|
21
|
+
format?: SpeechFormat;
|
|
22
|
+
/**
|
|
23
|
+
* Replaces the preset's host, for a server that copies its route (a local speech
|
|
24
|
+
* server behind the `openai` preset, a proxy). The preset's default model, voice,
|
|
25
|
+
* format and text limit are facts about its own host, so they do not follow you
|
|
26
|
+
* here: pass the `model`, and a `voice` unless every call names one.
|
|
27
|
+
*/
|
|
28
|
+
baseUrl?: string;
|
|
29
|
+
headers?: Record<string, string>;
|
|
30
|
+
/** The `google` preset only: caps the audio tokens generated. Refused on a wire that has no such field. */
|
|
31
|
+
maxOutputTokens?: number;
|
|
32
|
+
/** Swapped in tests, or to route through a proxy. Defaults to global fetch. */
|
|
33
|
+
fetchImpl?: typeof fetch;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Build a speech provider for a preset that can speak. A preset without a
|
|
38
|
+
* `speech` row throws, naming the ones that do (`SPEECH_PRESET_IDS`).
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* const speech = createSpeechProvider("google", { apiKey });
|
|
42
|
+
* const { bytes, mimeType } = await speech.synthesize({ text: "Click Settings." });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function createSpeechProvider(
|
|
46
|
+
id: ProviderPresetId,
|
|
47
|
+
config: SpeechProviderConfig,
|
|
48
|
+
): SpeechProvider {
|
|
49
|
+
const preset: ProviderPreset = PROVIDER_PRESETS[id];
|
|
50
|
+
const spec = preset.speech;
|
|
51
|
+
if (!spec) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`[providerkit] Preset "${id}" has no speech endpoint. Presets that do: ${SPEECH_PRESET_IDS.join(", ")}.`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
// A default is a fact about the host that documented it.
|
|
57
|
+
const own = config.baseUrl === undefined;
|
|
58
|
+
const model = config.model ?? (own ? spec.defaultModel : undefined);
|
|
59
|
+
if (!model) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`[providerkit] Speech on "${id}" has no default model${own ? "" : " for another host"}. Pass a model.`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const shared = {
|
|
65
|
+
...config,
|
|
66
|
+
id,
|
|
67
|
+
model,
|
|
68
|
+
baseUrl: config.baseUrl ?? spec.baseUrl ?? preset.baseUrl,
|
|
69
|
+
voice: config.voice ?? (own ? spec.defaultVoice : undefined),
|
|
70
|
+
format: config.format ?? (own ? spec.defaultFormat : undefined),
|
|
71
|
+
maxInputChars: config.maxInputChars ?? (own ? spec.maxInputChars : undefined),
|
|
72
|
+
};
|
|
73
|
+
switch (spec.shape) {
|
|
74
|
+
case "openai": {
|
|
75
|
+
if (config.maxOutputTokens !== undefined) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`[providerkit] Speech on "${id}" uses the openai shape, which has no maxOutputTokens. Remove it.`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return createOpenAISpeechProvider(shared);
|
|
81
|
+
}
|
|
82
|
+
case "gemini":
|
|
83
|
+
return createGeminiSpeechProvider(shared);
|
|
84
|
+
}
|
|
85
|
+
}
|