@ai-sdk/gateway 4.0.122 → 4.0.123

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.
@@ -30,9 +30,13 @@ import {
30
30
  type WebSocketLike,
31
31
  } from '@ai-sdk/provider-utils';
32
32
  import { z } from './zod';
33
- import { asGatewayError, createGatewayErrorFromResponse } from './errors';
33
+ import { asGatewayError } from './errors';
34
34
  import { parseAuthMethod } from './errors/parse-auth-method';
35
35
  import type { GatewayConfig } from './gateway-config';
36
+ import {
37
+ createErrorFromServerErrorPart,
38
+ errorControllerWithGatewayError,
39
+ } from './gateway-stream-errors';
36
40
  import { VERCEL_AI_GATEWAY_TEAM_HEADER } from './gateway-headers';
37
41
  import {
38
42
  GATEWAY_TRANSCRIPTION_SUBPROTOCOL,
@@ -247,8 +251,18 @@ export function toGatewayTranscriptionUrl(
247
251
  * case-insensitive because `combineHeaders` does not normalize casing and
248
252
  * callers can pass arbitrary casing via the `headers` option.
249
253
  */
250
- function getProtocolsFromHeaders(
254
+ export function getProtocolsFromHeaders(
251
255
  headers: Record<string, string | undefined>,
256
+ {
257
+ marker = GATEWAY_TRANSCRIPTION_SUBPROTOCOL,
258
+ getProtocols = getGatewayTranscriptionProtocols,
259
+ }: {
260
+ marker?: string;
261
+ getProtocols?: (
262
+ token: string,
263
+ options: { teamIdOrSlug?: string },
264
+ ) => string[];
265
+ } = {},
252
266
  ): string[] {
253
267
  const normalizedHeaders = normalizeHeaders(headers);
254
268
  const authorization = normalizedHeaders.authorization;
@@ -257,8 +271,8 @@ function getProtocolsFromHeaders(
257
271
  : undefined;
258
272
 
259
273
  return token == null
260
- ? [GATEWAY_TRANSCRIPTION_SUBPROTOCOL]
261
- : getGatewayTranscriptionProtocols(token, {
274
+ ? [marker]
275
+ : getProtocols(token, {
262
276
  teamIdOrSlug: normalizedHeaders[VERCEL_AI_GATEWAY_TEAM_HEADER],
263
277
  });
264
278
  }
@@ -493,71 +507,3 @@ const gatewayTranscriptionResponseSchema = lazySchema(() =>
493
507
  }),
494
508
  ),
495
509
  );
496
-
497
- /**
498
- * `asGatewayError` is async while the WebSocket event handlers are
499
- * synchronous, hence this helper.
500
- */
501
- async function errorControllerWithGatewayError(
502
- controller: ReadableStreamDefaultController<TranscriptionModelV4StreamPart>,
503
- error: unknown,
504
- authMethod: 'api-key' | 'oidc' | undefined,
505
- ): Promise<void> {
506
- controller.error(await asGatewayError(error, authMethod));
507
- }
508
-
509
- /**
510
- * Extract a human-readable message from an `error` stream part's payload for
511
- * the terminal close error.
512
- */
513
- function getServerErrorMessage(error: unknown): string {
514
- if (
515
- error != null &&
516
- typeof error === 'object' &&
517
- 'message' in error &&
518
- typeof error.message === 'string'
519
- ) {
520
- return error.message;
521
- }
522
- // JSON-stringifies object payloads (`String` would yield '[object Object]').
523
- return getErrorMessage(error);
524
- }
525
-
526
- /** Canonical status codes for server error-part types (no HTTP status exists on the WebSocket). */
527
- const SERVER_ERROR_STATUS_CODES: Record<string, number> = {
528
- authentication_error: 401,
529
- failed_dependency: 424,
530
- forbidden: 403,
531
- internal_server_error: 500,
532
- invalid_request_error: 400,
533
- model_not_found: 404,
534
- rate_limit_exceeded: 429,
535
- };
536
-
537
- /**
538
- * Maps a server error-part payload (`{ message, type }`) to the public
539
- * Gateway error class for its type; unknown shapes keep the generic message.
540
- */
541
- async function createErrorFromServerErrorPart(
542
- error: unknown,
543
- authMethod: 'api-key' | 'oidc' | undefined,
544
- ): Promise<unknown> {
545
- if (
546
- typeof error === 'object' &&
547
- error != null &&
548
- 'message' in error &&
549
- typeof error.message === 'string' &&
550
- 'type' in error &&
551
- typeof error.type === 'string' &&
552
- error.type in SERVER_ERROR_STATUS_CODES
553
- ) {
554
- return createGatewayErrorFromResponse({
555
- response: { error: { message: error.message, type: error.type } },
556
- statusCode: SERVER_ERROR_STATUS_CODES[error.type],
557
- authMethod,
558
- });
559
- }
560
- return new Error(
561
- `AI Gateway transcription stream failed: ${getServerErrorMessage(error)}`,
562
- );
563
- }
package/src/index.ts CHANGED
@@ -8,11 +8,13 @@ export type { GatewayModelId } from './gateway-language-model-settings';
8
8
  export {
9
9
  GATEWAY_AUTH_SUBPROTOCOL_PREFIX,
10
10
  GATEWAY_REALTIME_SUBPROTOCOL,
11
+ GATEWAY_SPEECH_SUBPROTOCOL,
11
12
  GATEWAY_TEAM_SUBPROTOCOL_PREFIX,
12
13
  GATEWAY_TRANSCRIPTION_SUBPROTOCOL,
13
14
  getGatewayRealtimeAuthToken,
14
15
  getGatewayRealtimeProtocols,
15
16
  getGatewayRealtimeTeamIdOrSlug,
17
+ getGatewaySpeechProtocols,
16
18
  getGatewayTranscriptionProtocols,
17
19
  } from './gateway-realtime-auth';
18
20
  export type { GatewayRealtimeModelId } from './gateway-realtime-model-settings';