Sign In

@ai-sdk/gateway

Package Overview
Dependencies
Maintainers
3
Versions
664
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ai-sdk/gateway - npm Package Compare versions

Comparing version
4.0.56
to
4.0.57
+35
src/errors/gateway-not-found-error.ts
import { GatewayError } from './gateway-error';
const name = 'GatewayNotFoundError';
const marker = `vercel.ai.gateway.error.${name}`;
const symbol = Symbol.for(marker);
/**
* Not found - the requested Gateway resource does not exist or is not
* visible to the caller (e.g. an unknown async batch/video job id).
* Distinct from `GatewayModelNotFoundError`, which is model-specific.
*/
export class GatewayNotFoundError extends GatewayError {
private readonly [symbol] = true; // used in isInstance
readonly name = name;
readonly type = 'not_found';
constructor({
message = 'Resource not found',
statusCode = 404,
cause,
generationId,
}: {
message?: string;
statusCode?: number;
cause?: unknown;
generationId?: string;
} = {}) {
super({ message, statusCode, cause, generationId });
}
static isInstance(error: unknown): error is GatewayNotFoundError {
return GatewayError.hasMarker(error) && symbol in error;
}
}
import {
APICallError,
type Experimental_BatchLanguageModelV4 as BatchLanguageModelV4,
type Experimental_BatchV4ItemResult as BatchV4ItemResult,
type Experimental_BatchV4OperationOptions as BatchV4OperationOptions,
type Experimental_BatchV4StartOptions as BatchV4StartOptions,
type Experimental_BatchV4StartResult as BatchV4StartResult,
type Experimental_BatchV4Status as BatchV4Status,
type Experimental_LanguageModelV4BatchRequest as LanguageModelV4BatchRequest,
type LanguageModelV4GenerateResult,
type SharedV4ProviderMetadata,
type SharedV4ProviderOptions,
} from '@ai-sdk/provider';
import {
combineHeaders,
convertAsyncIteratorToReadableStream,
createJsonErrorResponseHandler,
createJsonResponseHandler,
getErrorMessage,
parseJSON,
postJsonToApi,
resolve,
WORKFLOW_SERIALIZE,
WORKFLOW_DESERIALIZE,
} from '@ai-sdk/provider-utils';
import { z } from './zod';
import {
GatewayLanguageModel,
type GatewayChatConfig,
} from './gateway-language-model';
import type { GatewayModelId } from './gateway-language-model-settings';
import { asGatewayError } from './errors';
import { parseAuthMethod } from './errors/parse-auth-method';
export class GatewayBatchLanguageModel
extends GatewayLanguageModel
implements BatchLanguageModelV4
{
static [WORKFLOW_SERIALIZE](model: GatewayBatchLanguageModel) {
return GatewayLanguageModel[WORKFLOW_SERIALIZE](model);
}
static [WORKFLOW_DESERIALIZE](options: {
modelId: GatewayModelId;
config: GatewayChatConfig;
}) {
return new GatewayBatchLanguageModel(options.modelId, options.config);
}
constructor(modelId: GatewayModelId, config: GatewayChatConfig) {
super(modelId, config);
}
/**
* Starts a durable batch of text-generation requests through the Gateway's
* async batch surface (`POST {baseURL}/batch/start`). The returned
* `batchId` is the Gateway job id — provider-native batch ids stay
* server-side, so status and results always route back through the
* Gateway job.
*/
async experimental_doStartBatch({
requests,
providerOptions,
headers,
abortSignal,
}: BatchV4StartOptions<LanguageModelV4BatchRequest>): Promise<BatchV4StartResult> {
const resolvedHeaders = this.config.headers
? await resolve(this.config.headers)
: undefined;
const idempotencyKey = getGatewayBatchIdempotencyKey(providerOptions);
const forwardedProviderOptions = omitGatewayIdempotencyKey(providerOptions);
try {
const { value: responseBody } = await postJsonToApi({
url: this.getBatchUrl('start'),
headers: combineHeaders(
resolvedHeaders,
headers,
this.getBatchConfigHeaders(),
await resolve(this.config.o11yHeaders),
idempotencyKey != null
? { 'idempotency-key': idempotencyKey }
: undefined,
),
body: {
modelId: this.modelId,
requests: requests.map(request => ({
id: request.id,
options: this.maybeEncodeFileParts(request.options),
})),
...(forwardedProviderOptions != null && {
providerOptions: forwardedProviderOptions,
}),
},
successfulResponseHandler: createJsonResponseHandler(
gatewayBatchStartResponseSchema,
),
failedResponseHandler: createJsonErrorResponseHandler({
errorSchema: z.any(),
errorToMessage: data => getErrorMessage(data) ?? 'unknown error',
}),
...(abortSignal && { abortSignal }),
fetch: this.config.fetch,
});
return {
batchId: responseBody.batchId,
...convertGatewayBatchStatus(responseBody),
warnings: (responseBody.warnings ??
[]) as unknown as BatchV4StartResult['warnings'],
};
} catch (error) {
// Preserve cancellation: an aborted batch start may still have been
// accepted server-side, so it must not surface as a retryable 500.
if (isAbortOrTimeoutError(error)) {
throw error;
}
throw await asGatewayError(
error,
await parseAuthMethod(resolvedHeaders ?? {}),
);
}
}
/**
* Retrieves the lifecycle status of a Gateway batch job
* (`POST {baseURL}/batch/status`).
*/
async experimental_doGetBatchStatus({
batchId,
headers,
abortSignal,
}: BatchV4OperationOptions): Promise<BatchV4Status> {
const resolvedHeaders = this.config.headers
? await resolve(this.config.headers)
: undefined;
try {
const { value: responseBody } = await postJsonToApi({
url: this.getBatchUrl('status'),
headers: combineHeaders(
resolvedHeaders,
headers,
this.getBatchConfigHeaders(),
await resolve(this.config.o11yHeaders),
),
body: { batchId },
successfulResponseHandler: createJsonResponseHandler(
gatewayBatchStatusResponseSchema,
),
failedResponseHandler: createJsonErrorResponseHandler({
errorSchema: z.any(),
errorToMessage: data => getErrorMessage(data) ?? 'unknown error',
}),
...(abortSignal && { abortSignal }),
fetch: this.config.fetch,
});
return convertGatewayBatchStatus(responseBody);
} catch (error) {
if (isAbortOrTimeoutError(error)) {
throw error;
}
throw await asGatewayError(
error,
await parseAuthMethod(resolvedHeaders ?? {}),
);
}
}
/**
* Streams the per-request results of a terminal Gateway batch job
* (`POST {baseURL}/batch/results`, `application/x-ndjson`: one
* `BatchV4ItemResult` JSON object per line). Items are validated minimally
* (id + status) and passed through — the Gateway sanitizes them
* server-side. The route responds 400 while the batch is non-terminal.
*/
async experimental_doGetBatchResults({
batchId,
headers,
abortSignal,
}: BatchV4OperationOptions): Promise<
ReadableStream<BatchV4ItemResult<LanguageModelV4GenerateResult>>
> {
const resolvedHeaders = this.config.headers
? await resolve(this.config.headers)
: undefined;
try {
const { value: stream } = await postJsonToApi({
url: this.getBatchUrl('results'),
headers: combineHeaders(
resolvedHeaders,
headers,
this.getBatchConfigHeaders(),
await resolve(this.config.o11yHeaders),
),
body: { batchId },
successfulResponseHandler: async ({
response,
url,
requestBodyValues,
}: {
url: string;
requestBodyValues: unknown;
response: Response;
}) => {
if (response.body == null) {
throw new APICallError({
message: 'Batch results response body is empty',
url,
requestBodyValues,
statusCode: response.status,
});
}
return {
value: response.body,
responseHeaders: Object.fromEntries([...response.headers]),
};
},
failedResponseHandler: createJsonErrorResponseHandler({
errorSchema: z.any(),
errorToMessage: data => getErrorMessage(data) ?? 'unknown error',
}),
...(abortSignal && { abortSignal }),
fetch: this.config.fetch,
});
return convertAsyncIteratorToReadableStream(
parseGatewayBatchResultLines(stream),
);
} catch (error) {
if (isAbortOrTimeoutError(error)) {
throw error;
}
throw await asGatewayError(
error,
await parseAuthMethod(resolvedHeaders ?? {}),
);
}
}
private getBatchUrl(path: 'results' | 'start' | 'status') {
return `${this.config.baseURL}/batch/${path}`;
}
private getBatchConfigHeaders() {
return {
'ai-model-id': this.modelId,
};
}
}
/**
* Extracts the optional Gateway idempotency key from
* `providerOptions.gateway.idempotencyKey`. It is sent as the
* `idempotency-key` request header — the Gateway's replay contract for batch
* starts — and stripped from the forwarded body by
* `omitGatewayIdempotencyKey`.
*/
function getGatewayBatchIdempotencyKey(
providerOptions: SharedV4ProviderOptions | undefined,
): string | undefined {
const gatewayOptions = providerOptions?.gateway;
if (
gatewayOptions == null ||
typeof gatewayOptions !== 'object' ||
Array.isArray(gatewayOptions)
) {
return undefined;
}
const key = (gatewayOptions as { idempotencyKey?: unknown }).idempotencyKey;
return typeof key === 'string' && key.length > 0 ? key : undefined;
}
/**
* Removes `gateway.idempotencyKey` from the providerOptions forwarded in the
* request body. The key is transport metadata (it rides the `idempotency-key`
* header); the Gateway hashes the raw body for replay payload identity but
* normalizes the header separately, so keeping it out of the payload prevents
* equivalent retries from producing different digests (a false 422).
*/
function omitGatewayIdempotencyKey(
providerOptions: SharedV4ProviderOptions | undefined,
): SharedV4ProviderOptions | undefined {
const gatewayOptions = providerOptions?.gateway;
if (
gatewayOptions == null ||
typeof gatewayOptions !== 'object' ||
Array.isArray(gatewayOptions) ||
!('idempotencyKey' in gatewayOptions)
) {
return providerOptions;
}
const { idempotencyKey: _idempotencyKey, ...restGatewayOptions } =
gatewayOptions as Record<string, unknown>;
const restProviderOptions: Record<string, unknown> = { ...providerOptions };
if (Object.keys(restGatewayOptions).length === 0) {
delete restProviderOptions.gateway;
} else {
restProviderOptions.gateway = restGatewayOptions;
}
if (Object.keys(restProviderOptions).length === 0) {
return undefined;
}
return restProviderOptions as SharedV4ProviderOptions;
}
/**
* Matches cancellation errors (`AbortError`/`TimeoutError`), which
* `asGatewayError` would otherwise wrap into a retryable Gateway 500. Kept
* local because `isAbortError` is not exported from `@ai-sdk/provider-utils`;
* `DOMException` does not extend `Error`, so both must be checked.
*/
function isAbortOrTimeoutError(error: unknown): boolean {
if (!(error instanceof Error || error instanceof DOMException)) {
return false;
}
return error.name === 'AbortError' || error.name === 'TimeoutError';
}
function convertGatewayBatchStatus(body: {
status: 'completed' | 'failed' | 'pending';
rawStatus?: string | null;
requestCounts?: {
total?: number | null;
pending?: number | null;
completed?: number | null;
failed?: number | null;
} | null;
error?: {
message: string;
type?: string | null;
code?: string | null;
statusCode?: number | null;
} | null;
createdAt?: string | null;
expiresAt?: string | null;
providerMetadata?: Record<string, Record<string, unknown>> | null;
}): BatchV4Status {
const requestCounts = convertGatewayBatchRequestCounts(body.requestCounts);
return {
status: body.status,
...(body.rawStatus != null && { rawStatus: body.rawStatus }),
...(requestCounts != null && { requestCounts }),
...(body.error != null && {
error: {
message: body.error.message,
...(body.error.type != null && { type: body.error.type }),
...(body.error.code != null && { code: body.error.code }),
...(body.error.statusCode != null && {
statusCode: body.error.statusCode,
}),
},
}),
...(body.createdAt != null && { createdAt: body.createdAt }),
...(body.expiresAt != null && { expiresAt: body.expiresAt }),
...(body.providerMetadata != null && {
providerMetadata: body.providerMetadata as SharedV4ProviderMetadata,
}),
};
}
/**
* The spec's `requestCounts` requires all four counters; the Gateway's
* persisted descriptor allows partial counts. Only forward counts when the
* full set is present rather than fabricating zeros.
*/
function convertGatewayBatchRequestCounts(
counts:
| {
total?: number | null;
pending?: number | null;
completed?: number | null;
failed?: number | null;
}
| null
| undefined,
): BatchV4Status['requestCounts'] | undefined {
if (
counts == null ||
typeof counts.total !== 'number' ||
typeof counts.pending !== 'number' ||
typeof counts.completed !== 'number' ||
typeof counts.failed !== 'number'
) {
return undefined;
}
return {
total: counts.total,
pending: counts.pending,
completed: counts.completed,
failed: counts.failed,
};
}
/**
* Incremental NDJSON line splitter for the batch results stream: buffers
* partial lines across chunks and flushes a trailing line without a final
* newline. Each non-empty line is one `BatchV4ItemResult` JSON object.
*
* @param stream - The raw NDJSON byte stream from the batch results route.
* @yields One minimally-validated `BatchV4ItemResult` per non-empty line.
*/
async function* parseGatewayBatchResultLines(
stream: ReadableStream<Uint8Array>,
): AsyncGenerator<BatchV4ItemResult<LanguageModelV4GenerateResult>> {
const reader = stream.getReader();
const decoder = new TextDecoder();
let buffer = '';
let finished = false;
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
finished = true;
buffer += decoder.decode();
break;
}
buffer += decoder.decode(value, { stream: true });
let lineEnd = buffer.indexOf('\n');
while (lineEnd !== -1) {
const line = buffer.slice(0, lineEnd).replace(/\r$/, '');
buffer = buffer.slice(lineEnd + 1);
if (line.trim().length > 0) {
yield await parseGatewayBatchResultLine(line);
}
lineEnd = buffer.indexOf('\n');
}
}
const finalLine = buffer.replace(/\r$/, '');
if (finalLine.trim().length > 0) {
yield await parseGatewayBatchResultLine(finalLine);
}
} finally {
if (!finished) {
await reader.cancel().catch(() => {});
}
reader.releaseLock();
}
}
async function parseGatewayBatchResultLine(
line: string,
): Promise<BatchV4ItemResult<LanguageModelV4GenerateResult>> {
// Minimal validation (id + status); items pass through otherwise — the
// Gateway already sanitizes them server-side.
const parsed = await parseJSON({
text: line,
schema: gatewayBatchItemResultLineSchema,
});
const item =
parsed as unknown as BatchV4ItemResult<LanguageModelV4GenerateResult>;
// JSON carries `response.timestamp` as an ISO string; core expects a Date
// (`GeneratedFile`-style consumers call `.toISOString()`).
if (item.status === 'succeeded') {
const response = item.result?.response;
if (response !== undefined && typeof response.timestamp === 'string') {
response.timestamp = new Date(response.timestamp);
}
}
return item;
}
const gatewayBatchItemResultLineSchema = z
.object({
id: z.string(),
status: z.enum(['cancelled', 'expired', 'failed', 'succeeded']),
})
.catchall(z.unknown());
const gatewayBatchErrorSchema = z.object({
message: z.string(),
type: z.string().nullish(),
code: z.string().nullish(),
statusCode: z.number().nullish(),
});
const gatewayBatchRequestCountsSchema = z.object({
total: z.number().nullish(),
pending: z.number().nullish(),
completed: z.number().nullish(),
failed: z.number().nullish(),
});
const gatewayBatchProviderMetadataSchema = z.record(
z.string(),
z.record(z.string(), z.unknown()),
);
const gatewayBatchStatusFieldsSchema = z.object({
status: z.enum(['completed', 'failed', 'pending']),
rawStatus: z.string().nullish(),
requestCounts: gatewayBatchRequestCountsSchema.nullish(),
error: gatewayBatchErrorSchema.nullish(),
createdAt: z.string().nullish(),
expiresAt: z.string().nullish(),
providerMetadata: gatewayBatchProviderMetadataSchema.nullish(),
});
const gatewayBatchStartResponseSchema = gatewayBatchStatusFieldsSchema.extend({
batchId: z.string(),
warnings: z
.array(
z
.object({
requestId: z.string().nullish(),
warning: z.unknown(),
})
.catchall(z.unknown()),
)
.nullish(),
});
const gatewayBatchStatusResponseSchema = gatewayBatchStatusFieldsSchema;
+43
-19

@@ -1,2 +0,2 @@

import { LanguageModelV4, ProviderV4, EmbeddingModelV4, ImageModelV4, Experimental_VideoModelV4, RerankingModelV4, SpeechModelV4, TranscriptionModelV4, Experimental_RealtimeFactoryV4, TypeValidationError } from '@ai-sdk/provider';
import { LanguageModelV4, ProviderV4, Experimental_BatchLanguageModelV4, EmbeddingModelV4, ImageModelV4, Experimental_VideoModelV4, RerankingModelV4, SpeechModelV4, TranscriptionModelV4, Experimental_RealtimeFactoryV4, TypeValidationError } from '@ai-sdk/provider';
import * as _ai_sdk_provider_utils from '@ai-sdk/provider-utils';

@@ -667,11 +667,11 @@ import { FetchFunction, WebSocketConstructor, InferSchema } from '@ai-sdk/provider-utils';

interface GatewayProvider extends ProviderV4 {
(modelId: GatewayModelId): LanguageModelV4;
(modelId: GatewayModelId): Experimental_BatchLanguageModelV4;
/**
* Creates a model for text generation.
*/
chat(modelId: GatewayModelId): LanguageModelV4;
chat(modelId: GatewayModelId): Experimental_BatchLanguageModelV4;
/**
* Creates a model for text generation.
*/
languageModel(modelId: GatewayModelId): LanguageModelV4;
languageModel(modelId: GatewayModelId): Experimental_BatchLanguageModelV4;
/**

@@ -847,2 +847,7 @@ * Returns available providers and models for use with the remote provider.

has?: Array<'implicit-caching' | 'vision'>;
/**
* Idempotency key for `experimental_startTextBatch`: retries with the same
* key replay the original batch instead of creating a duplicate.
*/
idempotencyKey?: string;
/** Array of model slugs specifying fallback models to use in order. */

@@ -872,5 +877,5 @@ models?: string[];

declare const symbol$8: unique symbol;
declare const symbol$9: unique symbol;
declare abstract class GatewayError extends Error {
private readonly [symbol$8];
private readonly [symbol$9];
abstract readonly name: string;

@@ -909,3 +914,3 @@ abstract readonly type: string;

declare const symbol$7: unique symbol;
declare const symbol$8: unique symbol;
/**

@@ -915,3 +920,3 @@ * Authentication failed - invalid API key or OIDC token

declare class GatewayAuthenticationError extends GatewayError {
private readonly [symbol$7];
private readonly [symbol$8];
readonly name = "GatewayAuthenticationError";

@@ -939,3 +944,3 @@ readonly type = "authentication_error";

declare const symbol$6: unique symbol;
declare const symbol$7: unique symbol;
/**

@@ -947,3 +952,3 @@ * The request could not be fulfilled because a dependency it relied on was not

declare class GatewayFailedDependencyError extends GatewayError {
private readonly [symbol$6];
private readonly [symbol$7];
readonly name = "GatewayFailedDependencyError";

@@ -960,3 +965,3 @@ readonly type = "failed_dependency";

declare const symbol$5: unique symbol;
declare const symbol$6: unique symbol;
/**

@@ -967,3 +972,3 @@ * Forbidden - the request was rejected by policy (e.g. a routing rule),

declare class GatewayForbiddenError extends GatewayError {
private readonly [symbol$5];
private readonly [symbol$6];
readonly name = "GatewayForbiddenError";

@@ -982,3 +987,3 @@ readonly type = "forbidden";

declare const symbol$4: unique symbol;
declare const symbol$5: unique symbol;
/**

@@ -988,3 +993,3 @@ * Internal server error from the Gateway

declare class GatewayInternalServerError extends GatewayError {
private readonly [symbol$4];
private readonly [symbol$5];
readonly name = "GatewayInternalServerError";

@@ -1001,3 +1006,3 @@ readonly type = "internal_server_error";

declare const symbol$3: unique symbol;
declare const symbol$4: unique symbol;
/**

@@ -1007,3 +1012,3 @@ * Invalid request - missing headers, malformed data, etc.

declare class GatewayInvalidRequestError extends GatewayError {
private readonly [symbol$3];
private readonly [symbol$4];
readonly name = "GatewayInvalidRequestError";

@@ -1020,3 +1025,3 @@ readonly type = "invalid_request_error";

declare const symbol$2: unique symbol;
declare const symbol$3: unique symbol;
/**

@@ -1026,3 +1031,3 @@ * Model not found or not available

declare class GatewayModelNotFoundError extends GatewayError {
private readonly [symbol$2];
private readonly [symbol$3];
readonly name = "GatewayModelNotFoundError";

@@ -1041,2 +1046,21 @@ readonly type = "model_not_found";

declare const symbol$2: unique symbol;
/**
* Not found - the requested Gateway resource does not exist or is not
* visible to the caller (e.g. an unknown async batch/video job id).
* Distinct from `GatewayModelNotFoundError`, which is model-specific.
*/
declare class GatewayNotFoundError extends GatewayError {
private readonly [symbol$2];
readonly name = "GatewayNotFoundError";
readonly type = "not_found";
constructor({ message, statusCode, cause, generationId, }?: {
message?: string;
statusCode?: number;
cause?: unknown;
generationId?: string;
});
static isInstance(error: unknown): error is GatewayNotFoundError;
}
declare const symbol$1: unique symbol;

@@ -1082,2 +1106,2 @@ /**

export { GATEWAY_AUTH_SUBPROTOCOL_PREFIX, GATEWAY_REALTIME_SUBPROTOCOL, GATEWAY_TEAM_SUBPROTOCOL_PREFIX, GATEWAY_TRANSCRIPTION_SUBPROTOCOL, GatewayAuthenticationError, type GatewayCreditsResponse, type GatewayEmbeddingModelId, GatewayError, type GatewayErrorResponse, GatewayFailedDependencyError, GatewayForbiddenError, type GatewayGenerationInfo, type GatewayGenerationInfoParams, type GatewayImageModelId, GatewayInternalServerError, GatewayInvalidRequestError, type GatewayLanguageModelEntry, type GatewayProviderOptions as GatewayLanguageModelOptions, type GatewayLanguageModelSpecification, type GatewayLanguageModelEntry as GatewayModelEntry, type GatewayModelId, GatewayModelNotFoundError, type GatewayProvider, type GatewayProviderOptions, type GatewayProviderSettings, GatewayRateLimitError, type GatewayRealtimeModelId, type GatewayRerankingModelId, GatewayResponseError, type GatewaySpeechModelId, type GatewaySpendReportParams, type GatewaySpendReportResponse, type GatewaySpendReportRow, type GatewayTranscriptionModelId, type GatewayVideoModelId, VERSION, createGateway, createGateway as createGatewayProvider, gateway, getGatewayRealtimeAuthToken, getGatewayRealtimeProtocols, getGatewayRealtimeTeamIdOrSlug, getGatewayTranscriptionProtocols };
export { GATEWAY_AUTH_SUBPROTOCOL_PREFIX, GATEWAY_REALTIME_SUBPROTOCOL, GATEWAY_TEAM_SUBPROTOCOL_PREFIX, GATEWAY_TRANSCRIPTION_SUBPROTOCOL, GatewayAuthenticationError, type GatewayCreditsResponse, type GatewayEmbeddingModelId, GatewayError, type GatewayErrorResponse, GatewayFailedDependencyError, GatewayForbiddenError, type GatewayGenerationInfo, type GatewayGenerationInfoParams, type GatewayImageModelId, GatewayInternalServerError, GatewayInvalidRequestError, type GatewayLanguageModelEntry, type GatewayProviderOptions as GatewayLanguageModelOptions, type GatewayLanguageModelSpecification, type GatewayLanguageModelEntry as GatewayModelEntry, type GatewayModelId, GatewayModelNotFoundError, GatewayNotFoundError, type GatewayProvider, type GatewayProviderOptions, type GatewayProviderSettings, GatewayRateLimitError, type GatewayRealtimeModelId, type GatewayRerankingModelId, GatewayResponseError, type GatewaySpeechModelId, type GatewaySpendReportParams, type GatewaySpendReportResponse, type GatewaySpendReportRow, type GatewayTranscriptionModelId, type GatewayVideoModelId, VERSION, createGateway, createGateway as createGatewayProvider, gateway, getGatewayRealtimeAuthToken, getGatewayRealtimeProtocols, getGatewayRealtimeTeamIdOrSlug, getGatewayTranscriptionProtocols };
{
"name": "@ai-sdk/gateway",
"private": false,
"version": "4.0.56",
"version": "4.0.57",
"type": "module",

@@ -6,0 +6,0 @@ "license": "Apache-2.0",

@@ -10,2 +10,3 @@ import { z } from '../zod';

} from './gateway-model-not-found-error';
import { GatewayNotFoundError } from './gateway-not-found-error';
import { GatewayInternalServerError } from './gateway-internal-server-error';

@@ -103,2 +104,9 @@ import { GatewayFailedDependencyError } from './gateway-failed-dependency-error';

}
case 'not_found':
return new GatewayNotFoundError({
message,
statusCode,
cause,
generationId,
});
case 'internal_server_error':

@@ -105,0 +113,0 @@ return new GatewayInternalServerError({

@@ -17,4 +17,5 @@ export { asGatewayError } from './as-gateway-error';

} from './gateway-model-not-found-error';
export { GatewayNotFoundError } from './gateway-not-found-error';
export { GatewayRateLimitError } from './gateway-rate-limit-error';
export { GatewayResponseError } from './gateway-response-error';
export { GatewayTimeoutError } from './gateway-timeout-error';

@@ -28,3 +28,3 @@ import type {

type GatewayChatConfig = GatewayConfig & {
export type GatewayChatConfig = GatewayConfig & {
provider: string;

@@ -54,3 +54,3 @@ o11yHeaders: Resolvable<Record<string, string>>;

readonly modelId: GatewayModelId,
private readonly config: GatewayChatConfig,
protected readonly config: GatewayChatConfig,
) {}

@@ -201,3 +201,5 @@

*/
private maybeEncodeFileParts(options: LanguageModelV4CallOptions) {
protected maybeEncodeFileParts<
T extends Pick<LanguageModelV4CallOptions, 'prompt'>,
>(options: T): T {
for (const message of options.prompt) {

@@ -204,0 +206,0 @@ if (!Array.isArray(message.content)) {

@@ -24,2 +24,8 @@ // https://vercel.com/docs/ai-gateway/provider-options

/**
* Idempotency key for `experimental_startTextBatch`: retries with the same
* key replay the original batch instead of creating a duplicate.
*/
idempotencyKey?: string;
/** Array of model slugs specifying fallback models to use in order. */

@@ -26,0 +32,0 @@ models?: string[];

@@ -34,3 +34,3 @@ import {

} from './gateway-generation-info';
import { GatewayLanguageModel } from './gateway-language-model';
import { GatewayBatchLanguageModel } from './gateway-language-model-batch';
import { GatewayEmbeddingModel } from './gateway-embedding-model';

@@ -57,4 +57,4 @@ import { GatewayImageModel } from './gateway-image-model';

import type {
LanguageModelV4,
EmbeddingModelV4,
Experimental_BatchLanguageModelV4 as BatchLanguageModelV4,
ImageModelV4,

@@ -72,3 +72,3 @@ RerankingModelV4,

export interface GatewayProvider extends ProviderV4 {
(modelId: GatewayModelId): LanguageModelV4;
(modelId: GatewayModelId): BatchLanguageModelV4;

@@ -78,3 +78,3 @@ /**

*/
chat(modelId: GatewayModelId): LanguageModelV4;
chat(modelId: GatewayModelId): BatchLanguageModelV4;

@@ -84,3 +84,3 @@ /**

*/
languageModel(modelId: GatewayModelId): LanguageModelV4;
languageModel(modelId: GatewayModelId): BatchLanguageModelV4;

@@ -425,3 +425,3 @@ /**

const createLanguageModel = (modelId: GatewayModelId) => {
return new GatewayLanguageModel(modelId, {
return new GatewayBatchLanguageModel(modelId, {
provider: 'gateway',

@@ -428,0 +428,0 @@ baseURL,

@@ -57,2 +57,3 @@ export type { GatewayEmbeddingModelId } from './gateway-embedding-model-settings';

GatewayModelNotFoundError,
GatewayNotFoundError,
GatewayInternalServerError,

@@ -59,0 +60,0 @@ GatewayResponseError,

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display