import {z} from 'zod' 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 GatewayTrainingRunState = 'STATE_UNSPECIFIED' | 'PENDING_MATERIALIZATION' | 'READY' | 'FAILED' | 'EXPIRED'; export type GatewayTrainingRun = {name?: string | undefined, createTime?: string | undefined, updateTime?: string | undefined, createdBy?: string | undefined, baseModel?: string | undefined, loraRank?: number | undefined, status?: GatewayStatus | undefined, sourceTrainingSessionId?: string | undefined, state?: GatewayTrainingRunState | undefined}; export type CreateApiV1AccountsAccountIdTrainingRunsArgs = {account_id: string, body: GatewayTrainingRun}; export type CreateApiV1AccountsAccountIdTrainingRunsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 gatewayTrainingRunState = z.enum(["STATE_UNSPECIFIED", "PENDING_MATERIALIZATION", "READY", "FAILED", "EXPIRED"]); export const gatewayTrainingRun = z.object({name: z.string().optional(), createTime: z.string().optional(), updateTime: z.string().optional(), createdBy: z.string().optional(), baseModel: z.string().optional(), loraRank: z.number().int().optional(), status: gatewayStatus.optional(), sourceTrainingSessionId: z.string().optional(), state: gatewayTrainingRunState.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 a training run: the durable, account-owned model lineage for a * Tinker create_lora_training_client. A session may host many runs, and a run * may span sessions across resume. * * POST /v1/accounts/{account_id}/trainingRuns */ export const createApiV1AccountsAccountIdTrainingRuns = async (args: CreateApiV1AccountsAccountIdTrainingRunsArgs, options: CreateApiV1AccountsAccountIdTrainingRunsOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/trainingRuns${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(gatewayTrainingRun, await res.json()) };