@providerkit/core 0.27.1 → 0.28.0
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/auth/with-token.d.ts +3 -1
- package/dist/auth/with-token.d.ts.map +1 -1
- package/dist/auth/with-token.js +23 -4
- package/dist/auth/with-token.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +6 -0
- package/dist/errors.js.map +1 -1
- package/dist/fallback.d.ts +6 -4
- package/dist/fallback.d.ts.map +1 -1
- package/dist/fallback.js +13 -7
- package/dist/fallback.js.map +1 -1
- 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/key-pool.d.ts.map +1 -1
- package/dist/key-pool.js +3 -5
- package/dist/key-pool.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/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +2 -1
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/factory.js +4 -1
- package/dist/providers/factory.js.map +1 -1
- package/dist/providers/gemini-speech.d.ts +25 -0
- package/dist/providers/gemini-speech.d.ts.map +1 -0
- package/dist/providers/gemini-speech.js +172 -0
- package/dist/providers/gemini-speech.js.map +1 -0
- package/dist/providers/gemini.d.ts.map +1 -1
- package/dist/providers/gemini.js +4 -1
- package/dist/providers/gemini.js.map +1 -1
- 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/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +2 -1
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/responses.d.ts.map +1 -1
- package/dist/providers/responses.js +2 -1
- package/dist/providers/responses.js.map +1 -1
- 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/dist/types.d.ts +10 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +6 -1
- package/src/auth/with-token.ts +24 -7
- package/src/errors.ts +7 -0
- package/src/fallback.ts +40 -18
- package/src/index.ts +14 -1
- package/src/key-pool.ts +38 -37
- package/src/presets.ts +113 -0
- package/src/providers/anthropic.ts +8 -1
- package/src/providers/factory.ts +4 -1
- package/src/providers/gemini-speech.ts +232 -0
- package/src/providers/gemini.ts +10 -1
- package/src/providers/openai-speech.ts +125 -0
- package/src/providers/openai.ts +8 -1
- package/src/providers/responses.ts +8 -1
- 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
- package/src/types.ts +10 -0
|
@@ -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
|
+
}
|
package/src/providers/openai.ts
CHANGED
|
@@ -4,7 +4,13 @@
|
|
|
4
4
|
// OpenRouter, DeepSeek, GLM, Kimi, Groq, Together, vLLM, Ollama and LM Studio.
|
|
5
5
|
// Their divergences are small and named where they appear.
|
|
6
6
|
import { repairHistory } from "../context.ts";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
fileRefused,
|
|
9
|
+
ProviderError,
|
|
10
|
+
streamCut,
|
|
11
|
+
streamError,
|
|
12
|
+
validateMaxAttempts,
|
|
13
|
+
} from "../errors.ts";
|
|
8
14
|
import { streamSse, apiUrl, parseFrame } from "../transport.ts";
|
|
9
15
|
import { settleToolCalls, type ToolCallIdentity } from "../tool-args.ts";
|
|
10
16
|
import { attributionHeaders } from "../attribution.ts";
|
|
@@ -563,6 +569,7 @@ export function createOpenAIProvider(config: OpenAIConfig): Provider {
|
|
|
563
569
|
tools: ToolDefinition[],
|
|
564
570
|
opts: StreamOptions = {},
|
|
565
571
|
): AsyncIterable<ProviderChunk> {
|
|
572
|
+
validateMaxAttempts(opts.maxAttempts);
|
|
566
573
|
const effort = opts.effort ?? config.effort;
|
|
567
574
|
const model = opts.model ?? config.model;
|
|
568
575
|
|
|
@@ -11,7 +11,13 @@
|
|
|
11
11
|
// payload's own `type`. The transport yields only `data:` payloads, so this
|
|
12
12
|
// adapter reads `type` — which is what survives, and what gateways agree on.
|
|
13
13
|
import { repairHistory } from "../context.ts";
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
fileRefused,
|
|
16
|
+
ProviderError,
|
|
17
|
+
streamCut,
|
|
18
|
+
streamError,
|
|
19
|
+
validateMaxAttempts,
|
|
20
|
+
} from "../errors.ts";
|
|
15
21
|
import { streamSse, apiUrl, parseFrame } from "../transport.ts";
|
|
16
22
|
import { parseRateLimitReset } from "../rate-limit.ts";
|
|
17
23
|
import { settleToolCalls } from "../tool-args.ts";
|
|
@@ -419,6 +425,7 @@ export function createResponsesProvider(config: ResponsesConfig): Provider {
|
|
|
419
425
|
tools: ToolDefinition[],
|
|
420
426
|
opts: StreamOptions = {},
|
|
421
427
|
): AsyncIterable<ProviderChunk> {
|
|
428
|
+
validateMaxAttempts(opts.maxAttempts);
|
|
422
429
|
const effort = opts.effort ?? config.effort;
|
|
423
430
|
const model = opts.model ?? config.model;
|
|
424
431
|
const museContributor = isMuseSparkContributor(model);
|
|
@@ -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
|
+
}
|