@ai-sdk/gateway 3.0.39 → 3.0.41

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.
@@ -1,8 +1,32 @@
1
1
  import { APICallError } from '@ai-sdk/provider';
2
2
  import { extractApiCallResponse, GatewayError } from '.';
3
3
  import { createGatewayErrorFromResponse } from './create-gateway-error';
4
+ import { GatewayTimeoutError } from './gateway-timeout-error';
4
5
 
5
- export function asGatewayError(
6
+ /**
7
+ * Checks if an error is a timeout error from undici.
8
+ * Only checks undici-specific error codes to avoid false positives.
9
+ */
10
+ function isTimeoutError(error: unknown): boolean {
11
+ if (!(error instanceof Error)) {
12
+ return false;
13
+ }
14
+
15
+ // Check for undici-specific timeout error codes
16
+ const errorCode = (error as any).code;
17
+ if (typeof errorCode === 'string') {
18
+ const undiciTimeoutCodes = [
19
+ 'UND_ERR_HEADERS_TIMEOUT',
20
+ 'UND_ERR_BODY_TIMEOUT',
21
+ 'UND_ERR_CONNECT_TIMEOUT',
22
+ ];
23
+ return undiciTimeoutCodes.includes(errorCode);
24
+ }
25
+
26
+ return false;
27
+ }
28
+
29
+ export async function asGatewayError(
6
30
  error: unknown,
7
31
  authMethod?: 'api-key' | 'oidc',
8
32
  ) {
@@ -10,8 +34,25 @@ export function asGatewayError(
10
34
  return error;
11
35
  }
12
36
 
37
+ // Check if this is a timeout error (or has a timeout error in the cause chain)
38
+ if (isTimeoutError(error)) {
39
+ return GatewayTimeoutError.createTimeoutError({
40
+ originalMessage: error instanceof Error ? error.message : 'Unknown error',
41
+ cause: error,
42
+ });
43
+ }
44
+
45
+ // Check if this is an APICallError caused by a timeout
13
46
  if (APICallError.isInstance(error)) {
14
- return createGatewayErrorFromResponse({
47
+ // Check if the cause is a timeout error
48
+ if (error.cause && isTimeoutError(error.cause)) {
49
+ return GatewayTimeoutError.createTimeoutError({
50
+ originalMessage: error.message,
51
+ cause: error,
52
+ });
53
+ }
54
+
55
+ return await createGatewayErrorFromResponse({
15
56
  response: extractApiCallResponse(error),
16
57
  statusCode: error.statusCode ?? 500,
17
58
  defaultMessage: 'Gateway request failed',
@@ -20,7 +61,7 @@ export function asGatewayError(
20
61
  });
21
62
  }
22
63
 
23
- return createGatewayErrorFromResponse({
64
+ return await createGatewayErrorFromResponse({
24
65
  response: {},
25
66
  statusCode: 500,
26
67
  defaultMessage:
@@ -0,0 +1,59 @@
1
+ import { GatewayError } from './gateway-error';
2
+
3
+ const name = 'GatewayTimeoutError';
4
+ const marker = `vercel.ai.gateway.error.${name}`;
5
+ const symbol = Symbol.for(marker);
6
+
7
+ /**
8
+ * Client request timed out before receiving a response.
9
+ */
10
+ export class GatewayTimeoutError extends GatewayError {
11
+ private readonly [symbol] = true; // used in isInstance
12
+
13
+ readonly name = name;
14
+ readonly type = 'timeout_error';
15
+
16
+ constructor({
17
+ message = 'Request timed out',
18
+ statusCode = 408,
19
+ cause,
20
+ generationId,
21
+ }: {
22
+ message?: string;
23
+ statusCode?: number;
24
+ cause?: unknown;
25
+ generationId?: string;
26
+ } = {}) {
27
+ super({ message, statusCode, cause, generationId });
28
+ }
29
+
30
+ static isInstance(error: unknown): error is GatewayTimeoutError {
31
+ return GatewayError.hasMarker(error) && symbol in error;
32
+ }
33
+
34
+ /**
35
+ * Creates a helpful timeout error message with troubleshooting guidance
36
+ */
37
+ static createTimeoutError({
38
+ originalMessage,
39
+ statusCode = 408,
40
+ cause,
41
+ generationId,
42
+ }: {
43
+ originalMessage: string;
44
+ statusCode?: number;
45
+ cause?: unknown;
46
+ generationId?: string;
47
+ }): GatewayTimeoutError {
48
+ const message = `Gateway request timed out: ${originalMessage}
49
+
50
+ This is a client-side timeout. To resolve this, increase your timeout configuration: https://vercel.com/docs/ai-gateway/capabilities/video-generation#extending-timeouts-for-node.js`;
51
+
52
+ return new GatewayTimeoutError({
53
+ message,
54
+ statusCode,
55
+ cause,
56
+ generationId,
57
+ });
58
+ }
59
+ }
@@ -14,3 +14,4 @@ export {
14
14
  } from './gateway-model-not-found-error';
15
15
  export { GatewayRateLimitError } from './gateway-rate-limit-error';
16
16
  export { GatewayResponseError } from './gateway-response-error';
17
+ export { GatewayTimeoutError } from './gateway-timeout-error';
@@ -2,7 +2,7 @@ import { InferSchema, lazySchema, zodSchema } from '@ai-sdk/provider-utils';
2
2
  import { z } from 'zod/v4';
3
3
 
4
4
  // https://vercel.com/docs/ai-gateway/provider-options
5
- const gatewayProviderOptions = lazySchema(() =>
5
+ const gatewayLanguageModelOptions = lazySchema(() =>
6
6
  zodSchema(
7
7
  z.object({
8
8
  /**
@@ -63,4 +63,6 @@ const gatewayProviderOptions = lazySchema(() =>
63
63
  ),
64
64
  );
65
65
 
66
- export type GatewayProviderOptions = InferSchema<typeof gatewayProviderOptions>;
66
+ export type GatewayLanguageModelOptions = InferSchema<
67
+ typeof gatewayLanguageModelOptions
68
+ >;
package/src/index.ts CHANGED
@@ -15,7 +15,11 @@ export type {
15
15
  GatewayProvider,
16
16
  GatewayProviderSettings,
17
17
  } from './gateway-provider';
18
- export type { GatewayProviderOptions } from './gateway-provider-options';
18
+ export type {
19
+ GatewayLanguageModelOptions,
20
+ /** @deprecated Use `GatewayLanguageModelOptions` instead. */
21
+ GatewayLanguageModelOptions as GatewayProviderOptions,
22
+ } from './gateway-provider-options';
19
23
  export {
20
24
  GatewayError,
21
25
  GatewayAuthenticationError,