@ai-sdk/gateway 4.0.151 → 4.0.153
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 +18 -0
- package/dist/index.d.ts +173 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +323 -30
- package/dist/index.js.map +1 -1
- package/docs/00-ai-gateway.mdx +66 -0
- package/package.json +1 -1
- package/src/gateway-batch.ts +3 -2
- package/src/gateway-decision-model.ts +3 -2
- package/src/gateway-embedding-model.ts +3 -2
- package/src/gateway-extraction-model.ts +3 -2
- package/src/gateway-image-model.ts +3 -2
- package/src/gateway-language-model.ts +4 -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/gateway-reranking-model.ts +3 -2
- package/src/gateway-speech-model.ts +22 -20
- package/src/gateway-transcription-model.ts +14 -12
- package/src/gateway-video-model.ts +6 -3
- package/src/index.ts +6 -1
- package/src/redact-gateway-request-credentials.ts +88 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import {
|
|
2
|
+
InvalidArgumentError,
|
|
3
|
+
UnsupportedFunctionalityError,
|
|
4
|
+
type Experimental_RealtimeModelV4 as RealtimeModelV4,
|
|
5
|
+
type Experimental_RealtimeModelV4ClientEvent as RealtimeModelV4ClientEvent,
|
|
6
|
+
type Experimental_RealtimeModelV4ClientSecretOptions as RealtimeModelV4ClientSecretOptions,
|
|
7
|
+
type Experimental_RealtimeModelV4ClientSecretResult as RealtimeModelV4ClientSecretResult,
|
|
8
|
+
type Experimental_RealtimeModelV4ServerConnection as RealtimeModelV4ServerConnection,
|
|
9
|
+
type Experimental_RealtimeModelV4ServerEvent as RealtimeModelV4ServerEvent,
|
|
10
|
+
type Experimental_RealtimeModelV4SessionConfig as RealtimeModelV4SessionConfig,
|
|
11
|
+
type Experimental_RealtimeModelV4SessionIntent as RealtimeModelV4SessionIntent,
|
|
12
|
+
} from '@ai-sdk/provider';
|
|
13
|
+
import { removeUndefinedEntries } from '@ai-sdk/provider-utils';
|
|
14
|
+
import { getGatewayRealtimeProtocols } from './gateway-realtime-auth';
|
|
15
|
+
|
|
16
|
+
export type GatewayRealtimeModelLiveConfig = {
|
|
17
|
+
provider: string;
|
|
18
|
+
baseURL: string;
|
|
19
|
+
teamIdOrSlug?: string;
|
|
20
|
+
getHeaders: () => Promise<Record<string, string | undefined>>;
|
|
21
|
+
/** Mints a Live-bound client secret (`vcst_`) server-side. */
|
|
22
|
+
createClientSecret: (params: {
|
|
23
|
+
modelId: string;
|
|
24
|
+
expiresAfterSeconds?: number;
|
|
25
|
+
routeKind: 'live';
|
|
26
|
+
}) => PromiseLike<{ token: string; expiresAt?: number }>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const supportedOpenAILiveOptions = new Set([
|
|
30
|
+
'delegation',
|
|
31
|
+
'input',
|
|
32
|
+
'store',
|
|
33
|
+
'voice',
|
|
34
|
+
]);
|
|
35
|
+
const contextChannels = new Set(['commentary', 'instructions', 'thinking']);
|
|
36
|
+
|
|
37
|
+
const supportedSessionSettings = new Set([
|
|
38
|
+
'instructions',
|
|
39
|
+
'voice',
|
|
40
|
+
'inputAudioFormat',
|
|
41
|
+
'outputAudioFormat',
|
|
42
|
+
'providerOptions',
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Live model backed by the AI Gateway.
|
|
47
|
+
*
|
|
48
|
+
* Live is a continuous conversation API: one `session-start` opens the
|
|
49
|
+
* session, PCM audio streams in both directions, and `session-close` ends it
|
|
50
|
+
* with final usage. The client speaks the Gateway's normalized Live protocol
|
|
51
|
+
* on `/v4/ai/live-model`, and the Gateway runs the provider mapping
|
|
52
|
+
* server-side, so server events arrive already normalized.
|
|
53
|
+
*/
|
|
54
|
+
export class GatewayRealtimeModelLive implements RealtimeModelV4 {
|
|
55
|
+
readonly specificationVersion = 'v4' as const;
|
|
56
|
+
readonly capabilities = {
|
|
57
|
+
conversation: 'continuous',
|
|
58
|
+
transports: ['websocket'],
|
|
59
|
+
connections: ['client-secret-websocket', 'server-websocket'],
|
|
60
|
+
startup: 'session-start',
|
|
61
|
+
finalization: 'session-close',
|
|
62
|
+
} as const;
|
|
63
|
+
readonly provider: string;
|
|
64
|
+
readonly modelId: string;
|
|
65
|
+
|
|
66
|
+
private readonly config: GatewayRealtimeModelLiveConfig;
|
|
67
|
+
|
|
68
|
+
constructor(modelId: string, config: GatewayRealtimeModelLiveConfig) {
|
|
69
|
+
this.modelId = modelId;
|
|
70
|
+
this.provider = config.provider;
|
|
71
|
+
this.config = config;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Mints a short-lived client secret bound to this Live model. The browser
|
|
76
|
+
* opens the Live WebSocket with it through the `ai-gateway-auth.<token>`
|
|
77
|
+
* subprotocol; the session configuration is sent later in `session-start`.
|
|
78
|
+
*/
|
|
79
|
+
async doCreateClientSecret(
|
|
80
|
+
options?: RealtimeModelV4ClientSecretOptions,
|
|
81
|
+
): Promise<RealtimeModelV4ClientSecretResult> {
|
|
82
|
+
const secret = await this.config.createClientSecret({
|
|
83
|
+
modelId: this.modelId,
|
|
84
|
+
routeKind: 'live',
|
|
85
|
+
...(options?.expiresAfterSeconds != null && {
|
|
86
|
+
expiresAfterSeconds: options.expiresAfterSeconds,
|
|
87
|
+
}),
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
token: secret.token,
|
|
91
|
+
url: toGatewayLiveUrl(this.config.baseURL, this.modelId),
|
|
92
|
+
...(secret.expiresAt != null && { expiresAt: secret.expiresAt }),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getWebSocketConfig(options: { token: string; url: string }): {
|
|
97
|
+
url: string;
|
|
98
|
+
protocols?: string[];
|
|
99
|
+
} {
|
|
100
|
+
return {
|
|
101
|
+
url: options.url,
|
|
102
|
+
protocols: getGatewayRealtimeProtocols(options.token, {
|
|
103
|
+
teamIdOrSlug: this.config.teamIdOrSlug,
|
|
104
|
+
}),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async getServerWebSocketConfig(options?: {
|
|
109
|
+
intent?: RealtimeModelV4SessionIntent;
|
|
110
|
+
}): Promise<RealtimeModelV4ServerConnection> {
|
|
111
|
+
this.assertConversationIntent(options);
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
url: toGatewayLiveUrl(this.config.baseURL, this.modelId),
|
|
115
|
+
headers: removeUndefinedEntries(await this.config.getHeaders()),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
parseServerEvent(raw: unknown): RealtimeModelV4ServerEvent {
|
|
120
|
+
if (
|
|
121
|
+
raw == null ||
|
|
122
|
+
typeof raw !== 'object' ||
|
|
123
|
+
Array.isArray(raw) ||
|
|
124
|
+
typeof (raw as { type?: unknown }).type !== 'string'
|
|
125
|
+
) {
|
|
126
|
+
return {
|
|
127
|
+
type: 'error',
|
|
128
|
+
code: 'invalid_server_event',
|
|
129
|
+
message: 'Invalid AI Gateway Live server event.',
|
|
130
|
+
raw,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// Gateway-originated errors carry no provider payload; keep the frame as `raw`.
|
|
134
|
+
if ((raw as { type: string }).type === 'error' && !('raw' in raw)) {
|
|
135
|
+
return { ...(raw as object), raw } as RealtimeModelV4ServerEvent;
|
|
136
|
+
}
|
|
137
|
+
return raw as RealtimeModelV4ServerEvent;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
serializeClientEvent(
|
|
141
|
+
event: RealtimeModelV4ClientEvent,
|
|
142
|
+
options?: { intent?: RealtimeModelV4SessionIntent },
|
|
143
|
+
): unknown {
|
|
144
|
+
this.assertConversationIntent(options);
|
|
145
|
+
const eventId =
|
|
146
|
+
'eventId' in event && event.eventId != null
|
|
147
|
+
? { eventId: event.eventId }
|
|
148
|
+
: {};
|
|
149
|
+
|
|
150
|
+
switch (event.type) {
|
|
151
|
+
case 'session-start':
|
|
152
|
+
return {
|
|
153
|
+
type: 'session-start',
|
|
154
|
+
modelId: this.modelId,
|
|
155
|
+
config: this.buildSessionConfig(event.config),
|
|
156
|
+
...eventId,
|
|
157
|
+
};
|
|
158
|
+
case 'session-close':
|
|
159
|
+
case 'input-audio-mute':
|
|
160
|
+
case 'input-audio-unmute':
|
|
161
|
+
return { type: event.type, ...eventId };
|
|
162
|
+
case 'input-audio-append':
|
|
163
|
+
return { type: 'input-audio-append', audio: event.audio, ...eventId };
|
|
164
|
+
case 'context-append':
|
|
165
|
+
validateContextOptions(event.providerOptions);
|
|
166
|
+
return {
|
|
167
|
+
type: 'context-append',
|
|
168
|
+
content: event.content,
|
|
169
|
+
delegationId: event.delegationId,
|
|
170
|
+
...(event.providerOptions != null && {
|
|
171
|
+
providerOptions: event.providerOptions,
|
|
172
|
+
}),
|
|
173
|
+
...eventId,
|
|
174
|
+
};
|
|
175
|
+
default:
|
|
176
|
+
throw new UnsupportedFunctionalityError({
|
|
177
|
+
functionality: `AI Gateway Live client event: ${event.type}`,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
buildSessionConfig(
|
|
183
|
+
config: RealtimeModelV4SessionConfig,
|
|
184
|
+
options?: { intent?: RealtimeModelV4SessionIntent },
|
|
185
|
+
): RealtimeModelV4SessionConfig {
|
|
186
|
+
this.assertConversationIntent(options);
|
|
187
|
+
for (const [key, value] of Object.entries(config)) {
|
|
188
|
+
if (value != null && !supportedSessionSettings.has(key)) {
|
|
189
|
+
throw new UnsupportedFunctionalityError({
|
|
190
|
+
functionality: `AI Gateway Live session setting: ${key}`,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
validateSessionProviderOptions(config.providerOptions);
|
|
196
|
+
|
|
197
|
+
const input = config.inputAudioFormat;
|
|
198
|
+
const output = config.outputAudioFormat;
|
|
199
|
+
for (const [argument, format] of [
|
|
200
|
+
['inputAudioFormat', input],
|
|
201
|
+
['outputAudioFormat', output],
|
|
202
|
+
] as const) {
|
|
203
|
+
if (format != null && !isSupportedAudioFormat(format)) {
|
|
204
|
+
throw new UnsupportedFunctionalityError({
|
|
205
|
+
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`,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (
|
|
210
|
+
input != null &&
|
|
211
|
+
output != null &&
|
|
212
|
+
(input.type !== output.type || input.rate !== output.rate)
|
|
213
|
+
) {
|
|
214
|
+
throw new InvalidArgumentError({
|
|
215
|
+
argument: 'outputAudioFormat',
|
|
216
|
+
message:
|
|
217
|
+
'AI Gateway Live requires the same input and output audio format.',
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return removeUndefinedEntries(
|
|
222
|
+
config as Record<string, unknown>,
|
|
223
|
+
) as RealtimeModelV4SessionConfig;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private assertConversationIntent(options?: {
|
|
227
|
+
intent?: RealtimeModelV4SessionIntent;
|
|
228
|
+
}): void {
|
|
229
|
+
if (options?.intent != null && options.intent !== 'conversation') {
|
|
230
|
+
throw new Error(
|
|
231
|
+
`AI Gateway Live does not support the '${options.intent}' session intent.`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
238
|
+
return value != null && typeof value === 'object' && !Array.isArray(value);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Gateway options stay service-owned; OpenAI Live options are WebSocket-only. */
|
|
242
|
+
function validateSessionProviderOptions(providerOptions: unknown): void {
|
|
243
|
+
if (providerOptions == null) return;
|
|
244
|
+
if (!isRecord(providerOptions))
|
|
245
|
+
throw new InvalidArgumentError({
|
|
246
|
+
argument: 'providerOptions',
|
|
247
|
+
message: 'providerOptions must be an object.',
|
|
248
|
+
});
|
|
249
|
+
for (const [namespace, options] of Object.entries(providerOptions)) {
|
|
250
|
+
if (options === undefined) continue;
|
|
251
|
+
if (namespace !== 'gateway' && namespace !== 'openai')
|
|
252
|
+
throw new UnsupportedFunctionalityError({
|
|
253
|
+
functionality: `AI Gateway Live provider options namespace: ${namespace}`,
|
|
254
|
+
});
|
|
255
|
+
if (!isRecord(options))
|
|
256
|
+
throw new InvalidArgumentError({
|
|
257
|
+
argument: `providerOptions.${namespace}`,
|
|
258
|
+
message: `providerOptions.${namespace} must be an object.`,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
const openai = providerOptions.openai;
|
|
262
|
+
if (!isRecord(openai)) return;
|
|
263
|
+
for (const [key, value] of Object.entries(openai)) {
|
|
264
|
+
if (value === undefined) continue;
|
|
265
|
+
if (!supportedOpenAILiveOptions.has(key))
|
|
266
|
+
throw new UnsupportedFunctionalityError({
|
|
267
|
+
functionality: `AI Gateway Live OpenAI option: ${key}`,
|
|
268
|
+
});
|
|
269
|
+
if (
|
|
270
|
+
key === 'delegation' &&
|
|
271
|
+
value !== null &&
|
|
272
|
+
!(hasExactKeys(value, ['type']) && value.type === 'client')
|
|
273
|
+
)
|
|
274
|
+
throw new UnsupportedFunctionalityError({
|
|
275
|
+
functionality:
|
|
276
|
+
'AI Gateway Live delegation other than { type: "client" } or null',
|
|
277
|
+
});
|
|
278
|
+
if (!isValidOpenAILiveOption(key, value))
|
|
279
|
+
throw new InvalidArgumentError({
|
|
280
|
+
argument: `providerOptions.openai.${key}`,
|
|
281
|
+
message: openaiLiveOptionMessages[key],
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const openaiLiveOptionMessages: Record<string, string> = {
|
|
287
|
+
delegation: 'delegation must be { type: "client" } or null.',
|
|
288
|
+
input:
|
|
289
|
+
'input must be at most 128 messages, each { type: "message", role, content: [{ type, text }] }.',
|
|
290
|
+
store: 'store must be a boolean.',
|
|
291
|
+
voice: 'voice must be { id: string } with a nonempty id.',
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
function isValidOpenAILiveOption(key: string, value: unknown): boolean {
|
|
295
|
+
switch (key) {
|
|
296
|
+
case 'delegation':
|
|
297
|
+
return true;
|
|
298
|
+
case 'store':
|
|
299
|
+
return typeof value === 'boolean';
|
|
300
|
+
case 'voice':
|
|
301
|
+
return (
|
|
302
|
+
hasExactKeys(value, ['id']) &&
|
|
303
|
+
typeof value.id === 'string' &&
|
|
304
|
+
value.id.length > 0
|
|
305
|
+
);
|
|
306
|
+
case 'input':
|
|
307
|
+
return (
|
|
308
|
+
Array.isArray(value) &&
|
|
309
|
+
value.length <= 128 &&
|
|
310
|
+
// Array.from visits holes, which `every` would skip.
|
|
311
|
+
Array.from(value).every(isInputMessage)
|
|
312
|
+
);
|
|
313
|
+
default:
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function isInputMessage(message: unknown): boolean {
|
|
319
|
+
if (
|
|
320
|
+
!hasExactKeys(message, ['type', 'role', 'content']) ||
|
|
321
|
+
message.type !== 'message' ||
|
|
322
|
+
!Array.isArray(message.content) ||
|
|
323
|
+
message.content.length !== 1
|
|
324
|
+
)
|
|
325
|
+
return false;
|
|
326
|
+
const [part] = message.content;
|
|
327
|
+
if (!hasExactKeys(part, ['type', 'text']) || typeof part.text !== 'string')
|
|
328
|
+
return false;
|
|
329
|
+
return message.role === 'assistant'
|
|
330
|
+
? part.type === 'text' || part.type === 'output_text'
|
|
331
|
+
: (message.role === 'developer' || message.role === 'user') &&
|
|
332
|
+
part.type === 'input_text';
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function hasExactKeys(
|
|
336
|
+
value: unknown,
|
|
337
|
+
keys: string[],
|
|
338
|
+
): value is Record<string, unknown> {
|
|
339
|
+
if (!isRecord(value)) return false;
|
|
340
|
+
const present = Object.keys(value).filter(key => value[key] !== undefined);
|
|
341
|
+
return (
|
|
342
|
+
present.length === keys.length && keys.every(key => present.includes(key))
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function validateContextOptions(providerOptions: unknown): void {
|
|
347
|
+
if (providerOptions == null) return;
|
|
348
|
+
const valid =
|
|
349
|
+
isRecord(providerOptions) &&
|
|
350
|
+
Object.entries(providerOptions).every(
|
|
351
|
+
([namespace, options]) =>
|
|
352
|
+
options === undefined ||
|
|
353
|
+
(namespace === 'openai' &&
|
|
354
|
+
isRecord(options) &&
|
|
355
|
+
Object.entries(options).every(
|
|
356
|
+
([key, value]) =>
|
|
357
|
+
value === undefined ||
|
|
358
|
+
(key === 'channel' &&
|
|
359
|
+
typeof value === 'string' &&
|
|
360
|
+
contextChannels.has(value)),
|
|
361
|
+
)),
|
|
362
|
+
);
|
|
363
|
+
if (!valid)
|
|
364
|
+
throw new UnsupportedFunctionalityError({
|
|
365
|
+
functionality:
|
|
366
|
+
'AI Gateway Live context options other than openai.channel commentary, instructions, or thinking',
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function isSupportedAudioFormat(format: {
|
|
371
|
+
type: string;
|
|
372
|
+
rate?: number;
|
|
373
|
+
}): boolean {
|
|
374
|
+
return format.type === 'audio/pcm'
|
|
375
|
+
? format.rate === 16000 || format.rate === 24000
|
|
376
|
+
: (format.type === 'audio/pcma' || format.type === 'audio/pcmu') &&
|
|
377
|
+
format.rate === 8000;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Live WebSocket URL; the model id rides `?ai-model-id=` like the realtime route. */
|
|
381
|
+
function toGatewayLiveUrl(baseURL: string, modelId: string): string {
|
|
382
|
+
const url = new URL(`${baseURL.replace(/^http/, 'ws')}/live-model`);
|
|
383
|
+
url.searchParams.set('ai-model-id', modelId);
|
|
384
|
+
return url.toString();
|
|
385
|
+
}
|
|
@@ -3,3 +3,19 @@ import type { GatewayModelIds } from './gateway-model-catalog';
|
|
|
3
3
|
export type GatewayRealtimeModelId =
|
|
4
4
|
| GatewayModelIds['realtime']
|
|
5
5
|
| (string & {});
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Realtime model IDs that use the continuous Live API by default. Other IDs use
|
|
9
|
+
* the realtime API unless `{ api: 'live' }` is passed.
|
|
10
|
+
*/
|
|
11
|
+
export const gatewayLiveModelIds = [
|
|
12
|
+
'openai/gpt-live-1',
|
|
13
|
+
'openai/gpt-live-1.1',
|
|
14
|
+
] as const satisfies readonly GatewayModelIds['realtime'][];
|
|
15
|
+
|
|
16
|
+
export type GatewayKnownLiveModelId = (typeof gatewayLiveModelIds)[number];
|
|
17
|
+
|
|
18
|
+
export type GatewayRealtimeOptions = {
|
|
19
|
+
/** Selects the Gateway API for the model ID, including for new Live models. */
|
|
20
|
+
api?: 'live' | 'realtime';
|
|
21
|
+
};
|
|
@@ -17,6 +17,7 @@ import { z } from './zod';
|
|
|
17
17
|
import { asGatewayError } from './errors';
|
|
18
18
|
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
19
19
|
import type { GatewayConfig } from './gateway-config';
|
|
20
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
20
21
|
|
|
21
22
|
export class GatewayRerankingModel implements RerankingModelV4 {
|
|
22
23
|
readonly specificationVersion = 'v4';
|
|
@@ -59,12 +60,12 @@ export class GatewayRerankingModel implements RerankingModelV4 {
|
|
|
59
60
|
this.getModelConfigHeaders(),
|
|
60
61
|
await resolve(this.config.o11yHeaders),
|
|
61
62
|
),
|
|
62
|
-
body: {
|
|
63
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
63
64
|
documents,
|
|
64
65
|
query,
|
|
65
66
|
...(topN != null ? { topN } : {}),
|
|
66
67
|
...(providerOptions ? { providerOptions } : {}),
|
|
67
|
-
},
|
|
68
|
+
}),
|
|
68
69
|
successfulResponseHandler: createJsonResponseHandler(
|
|
69
70
|
gatewayRerankingResponseSchema,
|
|
70
71
|
),
|
|
@@ -43,6 +43,7 @@ import {
|
|
|
43
43
|
errorControllerWithGatewayError,
|
|
44
44
|
} from './gateway-stream-errors';
|
|
45
45
|
import { getProtocolsFromHeaders } from './gateway-transcription-model';
|
|
46
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
46
47
|
|
|
47
48
|
export class GatewaySpeechModel
|
|
48
49
|
implements SpeechModelV4, StreamingSpeechModelV4
|
|
@@ -93,7 +94,7 @@ export class GatewaySpeechModel
|
|
|
93
94
|
this.getModelConfigHeaders(),
|
|
94
95
|
await resolve(this.config.o11yHeaders),
|
|
95
96
|
),
|
|
96
|
-
body: {
|
|
97
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
97
98
|
text,
|
|
98
99
|
...(voice && { voice }),
|
|
99
100
|
...(outputFormat && { outputFormat }),
|
|
@@ -101,7 +102,7 @@ export class GatewaySpeechModel
|
|
|
101
102
|
...(speed != null && { speed }),
|
|
102
103
|
...(language && { language }),
|
|
103
104
|
...(providerOptions && { providerOptions }),
|
|
104
|
-
},
|
|
105
|
+
}),
|
|
105
106
|
successfulResponseHandler: createJsonResponseHandler(
|
|
106
107
|
gatewaySpeechResponseSchema,
|
|
107
108
|
),
|
|
@@ -149,24 +150,25 @@ export class GatewaySpeechModel
|
|
|
149
150
|
|
|
150
151
|
// First frame after the WebSocket opens, per the speech-stream envelope.
|
|
151
152
|
// Optional keys are omitted so the frame stays minimal.
|
|
152
|
-
const startFrame: Experimental_SpeechStreamStartFrame =
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
153
|
+
const startFrame: Experimental_SpeechStreamStartFrame =
|
|
154
|
+
withGatewayRequestTelemetryRedaction({
|
|
155
|
+
type: SPEECH_STREAM_START_FRAME_TYPE,
|
|
156
|
+
...(options.voice != null && { voice: options.voice }),
|
|
157
|
+
...(options.outputFormat != null && {
|
|
158
|
+
outputFormat: options.outputFormat,
|
|
159
|
+
}),
|
|
160
|
+
...(options.instructions != null && {
|
|
161
|
+
instructions: options.instructions,
|
|
162
|
+
}),
|
|
163
|
+
...(options.speed != null && { speed: options.speed }),
|
|
164
|
+
...(options.language != null && { language: options.language }),
|
|
165
|
+
...(options.providerOptions != null && {
|
|
166
|
+
providerOptions: options.providerOptions,
|
|
167
|
+
}),
|
|
168
|
+
...(options.includeRawChunks != null && {
|
|
169
|
+
includeRawChunks: options.includeRawChunks,
|
|
170
|
+
}),
|
|
171
|
+
});
|
|
170
172
|
|
|
171
173
|
return {
|
|
172
174
|
stream: createGatewaySpeechStream({
|
|
@@ -42,6 +42,7 @@ import {
|
|
|
42
42
|
GATEWAY_TRANSCRIPTION_SUBPROTOCOL,
|
|
43
43
|
getGatewayTranscriptionProtocols,
|
|
44
44
|
} from './gateway-realtime-auth';
|
|
45
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
45
46
|
|
|
46
47
|
export class GatewayTranscriptionModel implements TranscriptionModelV4 {
|
|
47
48
|
readonly specificationVersion = 'v4' as const;
|
|
@@ -87,14 +88,14 @@ export class GatewayTranscriptionModel implements TranscriptionModelV4 {
|
|
|
87
88
|
this.getModelConfigHeaders(),
|
|
88
89
|
await resolve(this.config.o11yHeaders),
|
|
89
90
|
),
|
|
90
|
-
body: {
|
|
91
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
91
92
|
audio:
|
|
92
93
|
audio instanceof Uint8Array
|
|
93
94
|
? convertUint8ArrayToBase64(audio)
|
|
94
95
|
: audio,
|
|
95
96
|
mediaType,
|
|
96
97
|
...(providerOptions && { providerOptions }),
|
|
97
|
-
},
|
|
98
|
+
}),
|
|
98
99
|
successfulResponseHandler: createJsonResponseHandler(
|
|
99
100
|
gatewayTranscriptionResponseSchema,
|
|
100
101
|
),
|
|
@@ -170,16 +171,17 @@ export class GatewayTranscriptionModel implements TranscriptionModelV4 {
|
|
|
170
171
|
|
|
171
172
|
// First frame after the WebSocket opens, per the transcription-stream
|
|
172
173
|
// envelope. Optional keys are omitted so the frame stays minimal.
|
|
173
|
-
const startFrame: Experimental_TranscriptionStreamStartFrame =
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
174
|
+
const startFrame: Experimental_TranscriptionStreamStartFrame =
|
|
175
|
+
withGatewayRequestTelemetryRedaction({
|
|
176
|
+
type: TRANSCRIPTION_STREAM_START_FRAME_TYPE,
|
|
177
|
+
inputAudioFormat: options.inputAudioFormat,
|
|
178
|
+
...(options.providerOptions != null && {
|
|
179
|
+
providerOptions: options.providerOptions,
|
|
180
|
+
}),
|
|
181
|
+
...(options.includeRawChunks != null && {
|
|
182
|
+
includeRawChunks: options.includeRawChunks,
|
|
183
|
+
}),
|
|
184
|
+
});
|
|
183
185
|
|
|
184
186
|
return {
|
|
185
187
|
stream: createGatewayTranscriptionStream({
|
|
@@ -28,6 +28,7 @@ import { z } from './zod';
|
|
|
28
28
|
import type { GatewayConfig } from './gateway-config';
|
|
29
29
|
import { asGatewayError } from './errors';
|
|
30
30
|
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
31
|
+
import { withGatewayRequestTelemetryRedaction } from './redact-gateway-request-credentials';
|
|
31
32
|
|
|
32
33
|
export class GatewayVideoModel implements VideoModelV4 {
|
|
33
34
|
readonly specificationVersion = 'v4' as const;
|
|
@@ -70,7 +71,9 @@ export class GatewayVideoModel implements VideoModelV4 {
|
|
|
70
71
|
await resolve(this.config.o11yHeaders),
|
|
71
72
|
{ accept: 'text/event-stream' },
|
|
72
73
|
),
|
|
73
|
-
body:
|
|
74
|
+
body: withGatewayRequestTelemetryRedaction(
|
|
75
|
+
this.buildRequestBody(options),
|
|
76
|
+
),
|
|
74
77
|
successfulResponseHandler: async ({
|
|
75
78
|
response,
|
|
76
79
|
url,
|
|
@@ -209,12 +212,12 @@ export class GatewayVideoModel implements VideoModelV4 {
|
|
|
209
212
|
this.getModelConfigHeaders(),
|
|
210
213
|
await resolve(this.config.o11yHeaders),
|
|
211
214
|
),
|
|
212
|
-
body: {
|
|
215
|
+
body: withGatewayRequestTelemetryRedaction({
|
|
213
216
|
...this.buildRequestBody(options),
|
|
214
217
|
// The spec option is `webhookUrl`; the Gateway's wire contract for a
|
|
215
218
|
// completion webhook is `callbackUrl`.
|
|
216
219
|
...(webhookUrl && { callbackUrl: webhookUrl }),
|
|
217
|
-
},
|
|
220
|
+
}),
|
|
218
221
|
successfulResponseHandler: createJsonResponseHandler(
|
|
219
222
|
gatewayVideoStartResponseSchema,
|
|
220
223
|
),
|
package/src/index.ts
CHANGED
|
@@ -18,7 +18,11 @@ export {
|
|
|
18
18
|
getGatewaySpeechProtocols,
|
|
19
19
|
getGatewayTranscriptionProtocols,
|
|
20
20
|
} from './gateway-realtime-auth';
|
|
21
|
-
export type {
|
|
21
|
+
export type {
|
|
22
|
+
GatewayKnownLiveModelId,
|
|
23
|
+
GatewayRealtimeModelId,
|
|
24
|
+
GatewayRealtimeOptions,
|
|
25
|
+
} from './gateway-realtime-model-settings';
|
|
22
26
|
export type { GatewayRerankingModelId } from './gateway-reranking-model-settings';
|
|
23
27
|
export type { GatewaySpeechModelId } from './gateway-speech-model-settings';
|
|
24
28
|
export type { GatewayTranscriptionModelId } from './gateway-transcription-model-settings';
|
|
@@ -47,6 +51,7 @@ export {
|
|
|
47
51
|
export type {
|
|
48
52
|
GatewayProvider,
|
|
49
53
|
GatewayProviderSettings,
|
|
54
|
+
GatewayRealtimeFactory,
|
|
50
55
|
} from './gateway-provider';
|
|
51
56
|
export type {
|
|
52
57
|
GatewayAsyncJobMetadata,
|