import {z} from 'zod' export type GatewayJobState = 'JOB_STATE_UNSPECIFIED' | 'JOB_STATE_CREATING' | 'JOB_STATE_RUNNING' | 'JOB_STATE_COMPLETED' | 'JOB_STATE_FAILED' | 'JOB_STATE_CANCELLED' | 'JOB_STATE_DELETING' | 'JOB_STATE_WRITING_RESULTS' | 'JOB_STATE_VALIDATING' | 'JOB_STATE_DELETING_CLEANING_UP' | 'JOB_STATE_PENDING' | 'JOB_STATE_EXPIRED' | 'JOB_STATE_RE_QUEUEING' | 'JOB_STATE_CREATING_INPUT_DATASET' | 'JOB_STATE_IDLE' | 'JOB_STATE_CANCELLING' | 'JOB_STATE_EARLY_STOPPED' | 'JOB_STATE_PAUSED' | 'JOB_STATE_DELETED' | 'JOB_STATE_ARCHIVED'; export type GatewayCode = 'OK' | 'CANCELLED' | 'UNKNOWN' | 'INVALID_ARGUMENT' | 'DEADLINE_EXCEEDED' | 'NOT_FOUND' | 'ALREADY_EXISTS' | 'PERMISSION_DENIED' | 'UNAUTHENTICATED' | 'RESOURCE_EXHAUSTED' | 'FAILED_PRECONDITION' | 'ABORTED' | 'OUT_OF_RANGE' | 'UNIMPLEMENTED' | 'INTERNAL' | 'UNAVAILABLE' | 'DATA_LOSS'; export type GatewayStatus = {code?: GatewayCode | undefined, message?: string | undefined}; export type GatewayBatchInferenceJobInferenceParameters = {maxTokens?: number | undefined, temperature?: number | undefined, topP?: number | undefined, n?: number | undefined, extraBody?: string | undefined, topK?: number | undefined}; export type DeploymentPrecision = 'PRECISION_UNSPECIFIED' | 'FP16' | 'FP8' | 'FP8_MM' | 'FP8_AR' | 'FP8_MM_KV_ATTN' | 'FP8_KV' | 'FP8_MM_V2' | 'FP8_V2' | 'FP8_MM_KV_ATTN_V2' | 'NF4' | 'FP4' | 'BF16' | 'FP4_BLOCKSCALED_MM' | 'FP4_MX_MOE'; export type GatewayJobProgress = {percent?: number | undefined, epoch?: number | undefined, totalInputRequests?: number | undefined, totalProcessedRequests?: number | undefined, successfullyProcessedRequests?: number | undefined, failedRequests?: number | undefined, outputRows?: number | undefined, inputTokens?: number | undefined, outputTokens?: number | undefined, cachedInputTokenCount?: number | undefined}; export type GatewayRegion = 'REGION_UNSPECIFIED' | 'US_IOWA_1' | 'US_VIRGINIA_1' | 'US_VIRGINIA_2' | 'US_ILLINOIS_1' | 'AP_TOKYO_1' | 'US_ARIZONA_1' | 'US_TEXAS_1' | 'US_ILLINOIS_2' | 'EU_FRANKFURT_1' | 'US_TEXAS_2' | 'EU_ICELAND_1' | 'EU_ICELAND_2' | 'US_WASHINGTON_1' | 'US_WASHINGTON_2' | 'US_WASHINGTON_3' | 'AP_TOKYO_2' | 'US_CALIFORNIA_1' | 'US_UTAH_1' | 'US_ARIZONA_3' | 'US_GEORGIA_1' | 'US_GEORGIA_2' | 'US_WASHINGTON_4' | 'US_GEORGIA_3' | 'NA_BRITISHCOLUMBIA_1' | 'US_GEORGIA_4' | 'US_OHIO_1' | 'US_NEWYORK_1' | 'EU_NETHERLANDS_1' | 'US_WASHINGTON_5' | 'US_MINNESOTA_1' | 'US_CALIFORNIA_2' | 'NA_BRITISHCOLUMBIA_2' | 'AP_MALAYSIA_2' | 'US_OREGON_1' | 'NA_BRITISHCOLUMBIA_3' | 'AP_NEWSOUTHWALES_1' | 'AP_TAIWAN_1'; export type GatewayMultiRegion = 'MULTI_REGION_UNSPECIFIED' | 'GLOBAL' | 'US' | 'CANADA' | 'EUROPE' | 'APAC'; export type GatewayPlacement = {region?: GatewayRegion | undefined, multiRegion?: GatewayMultiRegion | undefined, regions?: Array | undefined}; export type BatchInferenceJobLifecycleTimestamps = {validatedTime?: string | undefined, runStartTime?: string | undefined, endTime?: string | undefined}; export type GatewayBatchInferenceJob = {name?: string | undefined, displayName?: string | undefined, createTime?: string | undefined, expireTime?: string | undefined, createdBy?: string | undefined, state?: GatewayJobState | undefined, status?: GatewayStatus | undefined, model?: string | undefined, inputDatasetId?: string | undefined, outputDatasetId?: string | undefined, systemPrompt?: string | undefined, inferenceParameters?: GatewayBatchInferenceJobInferenceParameters | undefined, updateTime?: string | undefined, precision?: DeploymentPrecision | undefined, jobProgress?: GatewayJobProgress | undefined, continuedFromJobName?: string | undefined, placement?: GatewayPlacement | undefined, maxJobDuration?: string | undefined, lifecycle?: BatchInferenceJobLifecycleTimestamps | undefined, waitingOnCapacity?: boolean | undefined}; export type CreateApiV1AccountsAccountIdBatchInferenceJobsArgs = {batchInferenceJobId?: string | undefined, account_id: string, body: GatewayBatchInferenceJob}; export type CreateApiV1AccountsAccountIdBatchInferenceJobsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const gatewayJobState = z.enum(["JOB_STATE_UNSPECIFIED", "JOB_STATE_CREATING", "JOB_STATE_RUNNING", "JOB_STATE_COMPLETED", "JOB_STATE_FAILED", "JOB_STATE_CANCELLED", "JOB_STATE_DELETING", "JOB_STATE_WRITING_RESULTS", "JOB_STATE_VALIDATING", "JOB_STATE_DELETING_CLEANING_UP", "JOB_STATE_PENDING", "JOB_STATE_EXPIRED", "JOB_STATE_RE_QUEUEING", "JOB_STATE_CREATING_INPUT_DATASET", "JOB_STATE_IDLE", "JOB_STATE_CANCELLING", "JOB_STATE_EARLY_STOPPED", "JOB_STATE_PAUSED", "JOB_STATE_DELETED", "JOB_STATE_ARCHIVED"]); export const gatewayCode = z.enum(["OK", "CANCELLED", "UNKNOWN", "INVALID_ARGUMENT", "DEADLINE_EXCEEDED", "NOT_FOUND", "ALREADY_EXISTS", "PERMISSION_DENIED", "UNAUTHENTICATED", "RESOURCE_EXHAUSTED", "FAILED_PRECONDITION", "ABORTED", "OUT_OF_RANGE", "UNIMPLEMENTED", "INTERNAL", "UNAVAILABLE", "DATA_LOSS"]); export const gatewayStatus = z.object({code: gatewayCode.optional(), message: z.string().optional()}); export const gatewayBatchInferenceJobInferenceParameters = z.object({maxTokens: z.number().int().optional(), temperature: z.number().optional(), topP: z.number().optional(), n: z.number().int().optional(), extraBody: z.string().optional(), topK: z.number().int().optional()}); export const deploymentPrecision = z.enum(["PRECISION_UNSPECIFIED", "FP16", "FP8", "FP8_MM", "FP8_AR", "FP8_MM_KV_ATTN", "FP8_KV", "FP8_MM_V2", "FP8_V2", "FP8_MM_KV_ATTN_V2", "NF4", "FP4", "BF16", "FP4_BLOCKSCALED_MM", "FP4_MX_MOE"]); export const gatewayJobProgress = z.object({percent: z.number().int().optional(), epoch: z.number().int().optional(), totalInputRequests: z.number().int().optional(), totalProcessedRequests: z.number().int().optional(), successfullyProcessedRequests: z.number().int().optional(), failedRequests: z.number().int().optional(), outputRows: z.number().int().optional(), inputTokens: z.number().int().optional(), outputTokens: z.number().int().optional(), cachedInputTokenCount: z.number().int().optional()}); export const gatewayRegion = z.enum(["REGION_UNSPECIFIED", "US_IOWA_1", "US_VIRGINIA_1", "US_VIRGINIA_2", "US_ILLINOIS_1", "AP_TOKYO_1", "US_ARIZONA_1", "US_TEXAS_1", "US_ILLINOIS_2", "EU_FRANKFURT_1", "US_TEXAS_2", "EU_ICELAND_1", "EU_ICELAND_2", "US_WASHINGTON_1", "US_WASHINGTON_2", "US_WASHINGTON_3", "AP_TOKYO_2", "US_CALIFORNIA_1", "US_UTAH_1", "US_ARIZONA_3", "US_GEORGIA_1", "US_GEORGIA_2", "US_WASHINGTON_4", "US_GEORGIA_3", "NA_BRITISHCOLUMBIA_1", "US_GEORGIA_4", "US_OHIO_1", "US_NEWYORK_1", "EU_NETHERLANDS_1", "US_WASHINGTON_5", "US_MINNESOTA_1", "US_CALIFORNIA_2", "NA_BRITISHCOLUMBIA_2", "AP_MALAYSIA_2", "US_OREGON_1", "NA_BRITISHCOLUMBIA_3", "AP_NEWSOUTHWALES_1", "AP_TAIWAN_1"]); export const gatewayMultiRegion = z.enum(["MULTI_REGION_UNSPECIFIED", "GLOBAL", "US", "CANADA", "EUROPE", "APAC"]); export const gatewayPlacement = z.object({region: gatewayRegion.optional(), multiRegion: gatewayMultiRegion.optional(), regions: z.array(gatewayRegion).optional()}); export const batchInferenceJobLifecycleTimestamps = z.object({validatedTime: z.string().optional(), runStartTime: z.string().optional(), endTime: z.string().optional()}); export const gatewayBatchInferenceJob = z.object({name: z.string().optional(), displayName: z.string().optional(), createTime: z.string().optional(), expireTime: z.string().optional(), createdBy: z.string().optional(), state: gatewayJobState.optional(), status: gatewayStatus.optional(), model: z.string().optional(), inputDatasetId: z.string().optional(), outputDatasetId: z.string().optional(), systemPrompt: z.string().optional(), inferenceParameters: gatewayBatchInferenceJobInferenceParameters.optional(), updateTime: z.string().optional(), precision: deploymentPrecision.optional(), jobProgress: gatewayJobProgress.optional(), continuedFromJobName: z.string().optional(), placement: gatewayPlacement.optional(), maxJobDuration: z.string().optional(), lifecycle: batchInferenceJobLifecycleTimestamps.optional(), waitingOnCapacity: z.boolean().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create Batch Inference Job * * POST /v1/accounts/{account_id}/batchInferenceJobs */ export const createApiV1AccountsAccountIdBatchInferenceJobs = async (args: CreateApiV1AccountsAccountIdBatchInferenceJobsArgs, options: CreateApiV1AccountsAccountIdBatchInferenceJobsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.batchInferenceJobId != null) query.append("batchInferenceJobId", String(args.batchInferenceJobId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/batchInferenceJobs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(gatewayBatchInferenceJob, await res.json()) };