@ai-sdk/gateway 4.0.151 → 4.0.152
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/CHANGELOG.md +12 -0
- package/dist/index.d.ts +173 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +238 -5
- package/dist/index.js.map +1 -1
- package/docs/00-ai-gateway.mdx +66 -0
- package/package.json +1 -1
- package/src/gateway-model-catalog.ts +3 -3
- package/src/gateway-provider.ts +84 -17
- package/src/gateway-realtime-model-live.ts +385 -0
- package/src/gateway-realtime-model-settings.ts +16 -0
- package/src/index.ts +6 -1
package/dist/index.js
CHANGED
|
@@ -2926,6 +2926,215 @@ const gatewaySpeechResponseSchema = lazySchema(() => zodSchema(z.object({
|
|
|
2926
2926
|
providerMetadata: z.optional(z.record(z.string(), providerMetadataEntrySchema()))
|
|
2927
2927
|
})));
|
|
2928
2928
|
//#endregion
|
|
2929
|
+
//#region src/gateway-realtime-model-live.ts
|
|
2930
|
+
const supportedOpenAILiveOptions = /* @__PURE__ */ new Set([
|
|
2931
|
+
"delegation",
|
|
2932
|
+
"input",
|
|
2933
|
+
"store",
|
|
2934
|
+
"voice"
|
|
2935
|
+
]);
|
|
2936
|
+
const contextChannels = /* @__PURE__ */ new Set([
|
|
2937
|
+
"commentary",
|
|
2938
|
+
"instructions",
|
|
2939
|
+
"thinking"
|
|
2940
|
+
]);
|
|
2941
|
+
const supportedSessionSettings = /* @__PURE__ */ new Set([
|
|
2942
|
+
"instructions",
|
|
2943
|
+
"voice",
|
|
2944
|
+
"inputAudioFormat",
|
|
2945
|
+
"outputAudioFormat",
|
|
2946
|
+
"providerOptions"
|
|
2947
|
+
]);
|
|
2948
|
+
/**
|
|
2949
|
+
* Live model backed by the AI Gateway.
|
|
2950
|
+
*
|
|
2951
|
+
* Live is a continuous conversation API: one `session-start` opens the
|
|
2952
|
+
* session, PCM audio streams in both directions, and `session-close` ends it
|
|
2953
|
+
* with final usage. The client speaks the Gateway's normalized Live protocol
|
|
2954
|
+
* on `/v4/ai/live-model`, and the Gateway runs the provider mapping
|
|
2955
|
+
* server-side, so server events arrive already normalized.
|
|
2956
|
+
*/
|
|
2957
|
+
var GatewayRealtimeModelLive = class {
|
|
2958
|
+
constructor(modelId, config) {
|
|
2959
|
+
this.specificationVersion = "v4";
|
|
2960
|
+
this.capabilities = {
|
|
2961
|
+
conversation: "continuous",
|
|
2962
|
+
transports: ["websocket"],
|
|
2963
|
+
connections: ["client-secret-websocket", "server-websocket"],
|
|
2964
|
+
startup: "session-start",
|
|
2965
|
+
finalization: "session-close"
|
|
2966
|
+
};
|
|
2967
|
+
this.modelId = modelId;
|
|
2968
|
+
this.provider = config.provider;
|
|
2969
|
+
this.config = config;
|
|
2970
|
+
}
|
|
2971
|
+
/**
|
|
2972
|
+
* Mints a short-lived client secret bound to this Live model. The browser
|
|
2973
|
+
* opens the Live WebSocket with it through the `ai-gateway-auth.<token>`
|
|
2974
|
+
* subprotocol; the session configuration is sent later in `session-start`.
|
|
2975
|
+
*/
|
|
2976
|
+
async doCreateClientSecret(options) {
|
|
2977
|
+
const secret = await this.config.createClientSecret({
|
|
2978
|
+
modelId: this.modelId,
|
|
2979
|
+
routeKind: "live",
|
|
2980
|
+
...options?.expiresAfterSeconds != null && { expiresAfterSeconds: options.expiresAfterSeconds }
|
|
2981
|
+
});
|
|
2982
|
+
return {
|
|
2983
|
+
token: secret.token,
|
|
2984
|
+
url: toGatewayLiveUrl(this.config.baseURL, this.modelId),
|
|
2985
|
+
...secret.expiresAt != null && { expiresAt: secret.expiresAt }
|
|
2986
|
+
};
|
|
2987
|
+
}
|
|
2988
|
+
getWebSocketConfig(options) {
|
|
2989
|
+
return {
|
|
2990
|
+
url: options.url,
|
|
2991
|
+
protocols: getGatewayRealtimeProtocols(options.token, { teamIdOrSlug: this.config.teamIdOrSlug })
|
|
2992
|
+
};
|
|
2993
|
+
}
|
|
2994
|
+
async getServerWebSocketConfig(options) {
|
|
2995
|
+
this.assertConversationIntent(options);
|
|
2996
|
+
return {
|
|
2997
|
+
url: toGatewayLiveUrl(this.config.baseURL, this.modelId),
|
|
2998
|
+
headers: removeUndefinedEntries(await this.config.getHeaders())
|
|
2999
|
+
};
|
|
3000
|
+
}
|
|
3001
|
+
parseServerEvent(raw) {
|
|
3002
|
+
if (raw == null || typeof raw !== "object" || Array.isArray(raw) || typeof raw.type !== "string") return {
|
|
3003
|
+
type: "error",
|
|
3004
|
+
code: "invalid_server_event",
|
|
3005
|
+
message: "Invalid AI Gateway Live server event.",
|
|
3006
|
+
raw
|
|
3007
|
+
};
|
|
3008
|
+
if (raw.type === "error" && !("raw" in raw)) return {
|
|
3009
|
+
...raw,
|
|
3010
|
+
raw
|
|
3011
|
+
};
|
|
3012
|
+
return raw;
|
|
3013
|
+
}
|
|
3014
|
+
serializeClientEvent(event, options) {
|
|
3015
|
+
this.assertConversationIntent(options);
|
|
3016
|
+
const eventId = "eventId" in event && event.eventId != null ? { eventId: event.eventId } : {};
|
|
3017
|
+
switch (event.type) {
|
|
3018
|
+
case "session-start": return {
|
|
3019
|
+
type: "session-start",
|
|
3020
|
+
modelId: this.modelId,
|
|
3021
|
+
config: this.buildSessionConfig(event.config),
|
|
3022
|
+
...eventId
|
|
3023
|
+
};
|
|
3024
|
+
case "session-close":
|
|
3025
|
+
case "input-audio-mute":
|
|
3026
|
+
case "input-audio-unmute": return {
|
|
3027
|
+
type: event.type,
|
|
3028
|
+
...eventId
|
|
3029
|
+
};
|
|
3030
|
+
case "input-audio-append": return {
|
|
3031
|
+
type: "input-audio-append",
|
|
3032
|
+
audio: event.audio,
|
|
3033
|
+
...eventId
|
|
3034
|
+
};
|
|
3035
|
+
case "context-append":
|
|
3036
|
+
validateContextOptions(event.providerOptions);
|
|
3037
|
+
return {
|
|
3038
|
+
type: "context-append",
|
|
3039
|
+
content: event.content,
|
|
3040
|
+
delegationId: event.delegationId,
|
|
3041
|
+
...event.providerOptions != null && { providerOptions: event.providerOptions },
|
|
3042
|
+
...eventId
|
|
3043
|
+
};
|
|
3044
|
+
default: throw new UnsupportedFunctionalityError({ functionality: `AI Gateway Live client event: ${event.type}` });
|
|
3045
|
+
}
|
|
3046
|
+
}
|
|
3047
|
+
buildSessionConfig(config, options) {
|
|
3048
|
+
this.assertConversationIntent(options);
|
|
3049
|
+
for (const [key, value] of Object.entries(config)) if (value != null && !supportedSessionSettings.has(key)) throw new UnsupportedFunctionalityError({ functionality: `AI Gateway Live session setting: ${key}` });
|
|
3050
|
+
validateSessionProviderOptions(config.providerOptions);
|
|
3051
|
+
const input = config.inputAudioFormat;
|
|
3052
|
+
const output = config.outputAudioFormat;
|
|
3053
|
+
for (const [argument, format] of [["inputAudioFormat", input], ["outputAudioFormat", output]]) if (format != null && !isSupportedAudioFormat(format)) throw new UnsupportedFunctionalityError({ functionality: `AI Gateway Live ${argument} ${format.type} at ${format.rate ?? "an unspecified"} Hz; use audio/pcm at 16000 or 24000 Hz, or audio/pcma or audio/pcmu at 8000 Hz` });
|
|
3054
|
+
if (input != null && output != null && (input.type !== output.type || input.rate !== output.rate)) throw new InvalidArgumentError({
|
|
3055
|
+
argument: "outputAudioFormat",
|
|
3056
|
+
message: "AI Gateway Live requires the same input and output audio format."
|
|
3057
|
+
});
|
|
3058
|
+
return removeUndefinedEntries(config);
|
|
3059
|
+
}
|
|
3060
|
+
assertConversationIntent(options) {
|
|
3061
|
+
if (options?.intent != null && options.intent !== "conversation") throw new Error(`AI Gateway Live does not support the '${options.intent}' session intent.`);
|
|
3062
|
+
}
|
|
3063
|
+
};
|
|
3064
|
+
function isRecord(value) {
|
|
3065
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
3066
|
+
}
|
|
3067
|
+
/** Gateway options stay service-owned; OpenAI Live options are WebSocket-only. */
|
|
3068
|
+
function validateSessionProviderOptions(providerOptions) {
|
|
3069
|
+
if (providerOptions == null) return;
|
|
3070
|
+
if (!isRecord(providerOptions)) throw new InvalidArgumentError({
|
|
3071
|
+
argument: "providerOptions",
|
|
3072
|
+
message: "providerOptions must be an object."
|
|
3073
|
+
});
|
|
3074
|
+
for (const [namespace, options] of Object.entries(providerOptions)) {
|
|
3075
|
+
if (options === void 0) continue;
|
|
3076
|
+
if (namespace !== "gateway" && namespace !== "openai") throw new UnsupportedFunctionalityError({ functionality: `AI Gateway Live provider options namespace: ${namespace}` });
|
|
3077
|
+
if (!isRecord(options)) throw new InvalidArgumentError({
|
|
3078
|
+
argument: `providerOptions.${namespace}`,
|
|
3079
|
+
message: `providerOptions.${namespace} must be an object.`
|
|
3080
|
+
});
|
|
3081
|
+
}
|
|
3082
|
+
const openai = providerOptions.openai;
|
|
3083
|
+
if (!isRecord(openai)) return;
|
|
3084
|
+
for (const [key, value] of Object.entries(openai)) {
|
|
3085
|
+
if (value === void 0) continue;
|
|
3086
|
+
if (!supportedOpenAILiveOptions.has(key)) throw new UnsupportedFunctionalityError({ functionality: `AI Gateway Live OpenAI option: ${key}` });
|
|
3087
|
+
if (key === "delegation" && value !== null && !(hasExactKeys(value, ["type"]) && value.type === "client")) throw new UnsupportedFunctionalityError({ functionality: "AI Gateway Live delegation other than { type: \"client\" } or null" });
|
|
3088
|
+
if (!isValidOpenAILiveOption(key, value)) throw new InvalidArgumentError({
|
|
3089
|
+
argument: `providerOptions.openai.${key}`,
|
|
3090
|
+
message: openaiLiveOptionMessages[key]
|
|
3091
|
+
});
|
|
3092
|
+
}
|
|
3093
|
+
}
|
|
3094
|
+
const openaiLiveOptionMessages = {
|
|
3095
|
+
delegation: "delegation must be { type: \"client\" } or null.",
|
|
3096
|
+
input: "input must be at most 128 messages, each { type: \"message\", role, content: [{ type, text }] }.",
|
|
3097
|
+
store: "store must be a boolean.",
|
|
3098
|
+
voice: "voice must be { id: string } with a nonempty id."
|
|
3099
|
+
};
|
|
3100
|
+
function isValidOpenAILiveOption(key, value) {
|
|
3101
|
+
switch (key) {
|
|
3102
|
+
case "delegation": return true;
|
|
3103
|
+
case "store": return typeof value === "boolean";
|
|
3104
|
+
case "voice": return hasExactKeys(value, ["id"]) && typeof value.id === "string" && value.id.length > 0;
|
|
3105
|
+
case "input": return Array.isArray(value) && value.length <= 128 && Array.from(value).every(isInputMessage);
|
|
3106
|
+
default: return false;
|
|
3107
|
+
}
|
|
3108
|
+
}
|
|
3109
|
+
function isInputMessage(message) {
|
|
3110
|
+
if (!hasExactKeys(message, [
|
|
3111
|
+
"type",
|
|
3112
|
+
"role",
|
|
3113
|
+
"content"
|
|
3114
|
+
]) || message.type !== "message" || !Array.isArray(message.content) || message.content.length !== 1) return false;
|
|
3115
|
+
const [part] = message.content;
|
|
3116
|
+
if (!hasExactKeys(part, ["type", "text"]) || typeof part.text !== "string") return false;
|
|
3117
|
+
return message.role === "assistant" ? part.type === "text" || part.type === "output_text" : (message.role === "developer" || message.role === "user") && part.type === "input_text";
|
|
3118
|
+
}
|
|
3119
|
+
function hasExactKeys(value, keys) {
|
|
3120
|
+
if (!isRecord(value)) return false;
|
|
3121
|
+
const present = Object.keys(value).filter((key) => value[key] !== void 0);
|
|
3122
|
+
return present.length === keys.length && keys.every((key) => present.includes(key));
|
|
3123
|
+
}
|
|
3124
|
+
function validateContextOptions(providerOptions) {
|
|
3125
|
+
if (providerOptions == null) return;
|
|
3126
|
+
if (!(isRecord(providerOptions) && Object.entries(providerOptions).every(([namespace, options]) => options === void 0 || namespace === "openai" && isRecord(options) && Object.entries(options).every(([key, value]) => value === void 0 || key === "channel" && typeof value === "string" && contextChannels.has(value))))) throw new UnsupportedFunctionalityError({ functionality: "AI Gateway Live context options other than openai.channel commentary, instructions, or thinking" });
|
|
3127
|
+
}
|
|
3128
|
+
function isSupportedAudioFormat(format) {
|
|
3129
|
+
return format.type === "audio/pcm" ? format.rate === 16e3 || format.rate === 24e3 : (format.type === "audio/pcma" || format.type === "audio/pcmu") && format.rate === 8e3;
|
|
3130
|
+
}
|
|
3131
|
+
/** Live WebSocket URL; the model id rides `?ai-model-id=` like the realtime route. */
|
|
3132
|
+
function toGatewayLiveUrl(baseURL, modelId) {
|
|
3133
|
+
const url = new URL(`${baseURL.replace(/^http/, "ws")}/live-model`);
|
|
3134
|
+
url.searchParams.set("ai-model-id", modelId);
|
|
3135
|
+
return url.toString();
|
|
3136
|
+
}
|
|
3137
|
+
//#endregion
|
|
2929
3138
|
//#region src/gateway-realtime-model.ts
|
|
2930
3139
|
/**
|
|
2931
3140
|
* Realtime model backed by the AI Gateway.
|
|
@@ -3012,6 +3221,13 @@ function toGatewayRealtimeUrl(baseURL, modelId) {
|
|
|
3012
3221
|
return url.toString();
|
|
3013
3222
|
}
|
|
3014
3223
|
//#endregion
|
|
3224
|
+
//#region src/gateway-realtime-model-settings.ts
|
|
3225
|
+
/**
|
|
3226
|
+
* Realtime model IDs that use the continuous Live API by default. Other IDs use
|
|
3227
|
+
* the realtime API unless `{ api: 'live' }` is passed.
|
|
3228
|
+
*/
|
|
3229
|
+
const gatewayLiveModelIds = ["openai/gpt-live-1", "openai/gpt-live-1.1"];
|
|
3230
|
+
//#endregion
|
|
3015
3231
|
//#region src/tool/browserbase-fetch.ts
|
|
3016
3232
|
const jsonObjectSchema = () => z.record(z.string(), z.unknown());
|
|
3017
3233
|
const browserbaseFetchInputSchema = lazySchema(() => zodSchema(z.object({
|
|
@@ -3636,7 +3852,7 @@ async function getVercelRequestId() {
|
|
|
3636
3852
|
}
|
|
3637
3853
|
//#endregion
|
|
3638
3854
|
//#region src/version.ts
|
|
3639
|
-
const VERSION = "4.0.
|
|
3855
|
+
const VERSION = "4.0.152";
|
|
3640
3856
|
//#endregion
|
|
3641
3857
|
//#region src/gateway-provider.ts
|
|
3642
3858
|
const AI_GATEWAY_PROTOCOL_VERSION = "0.0.1";
|
|
@@ -3940,16 +4156,22 @@ function createGateway(options = {}) {
|
|
|
3940
4156
|
...secret.expiresAt != null && { expiresAt: secret.expiresAt }
|
|
3941
4157
|
};
|
|
3942
4158
|
} });
|
|
3943
|
-
const createRealtimeModel = (modelId) => new
|
|
4159
|
+
const createRealtimeModel = (modelId, realtimeOptions) => resolveGatewayRealtimeApi(modelId, realtimeOptions) === "live" ? new GatewayRealtimeModelLive(modelId, {
|
|
4160
|
+
provider: "gateway.live",
|
|
4161
|
+
baseURL,
|
|
4162
|
+
teamIdOrSlug: options.teamIdOrSlug,
|
|
4163
|
+
createClientSecret: mintClientSecret,
|
|
4164
|
+
getHeaders
|
|
4165
|
+
}) : new GatewayRealtimeModel(modelId, {
|
|
3944
4166
|
provider: "gateway.realtime",
|
|
3945
4167
|
baseURL,
|
|
3946
4168
|
teamIdOrSlug: options.teamIdOrSlug,
|
|
3947
4169
|
createClientSecret: mintClientSecret,
|
|
3948
4170
|
getHeaders
|
|
3949
4171
|
});
|
|
3950
|
-
provider.experimental_realtime = Object.assign((modelId) => createRealtimeModel(modelId), { getToken: async (tokenOptions) => {
|
|
3951
|
-
const { model: modelId, ...secretOptions } = tokenOptions;
|
|
3952
|
-
const secret = await createRealtimeModel(modelId).doCreateClientSecret(secretOptions);
|
|
4172
|
+
provider.experimental_realtime = Object.assign((modelId, realtimeOptions) => createRealtimeModel(modelId, realtimeOptions), { getToken: async (tokenOptions) => {
|
|
4173
|
+
const { model: modelId, api, ...secretOptions } = tokenOptions;
|
|
4174
|
+
const secret = await createRealtimeModel(modelId, { api }).doCreateClientSecret(secretOptions);
|
|
3953
4175
|
return {
|
|
3954
4176
|
token: secret.token,
|
|
3955
4177
|
url: secret.url,
|
|
@@ -3994,6 +4216,17 @@ async function getGatewayAuthToken(options) {
|
|
|
3994
4216
|
authMethod: "oidc"
|
|
3995
4217
|
};
|
|
3996
4218
|
}
|
|
4219
|
+
function resolveGatewayRealtimeApi(modelId, options) {
|
|
4220
|
+
const api = options?.api;
|
|
4221
|
+
if (api !== void 0) {
|
|
4222
|
+
if (api !== "live" && api !== "realtime") throw new InvalidArgumentError({
|
|
4223
|
+
argument: "api",
|
|
4224
|
+
message: "AI Gateway realtime api must be \"live\" or \"realtime\"."
|
|
4225
|
+
});
|
|
4226
|
+
return api;
|
|
4227
|
+
}
|
|
4228
|
+
return gatewayLiveModelIds.includes(modelId) ? "live" : "realtime";
|
|
4229
|
+
}
|
|
3997
4230
|
function assertGatewayClientSecretServerEnvironment() {
|
|
3998
4231
|
if (typeof globalThis.window !== "undefined") throw new Error("AI Gateway client secrets must be minted server-side: minting needs your Gateway credential, which must never reach the browser. Call gateway.experimental_realtime.getToken(), gateway.experimental_transcription.getToken(), or gateway.experimental_speech.getToken() from your server and pass the returned token to the client.");
|
|
3999
4232
|
}
|