import {z} from 'zod' export type LoRA = {url: string, scale: number}; export type ResponseExtension = 'webp' | 'jpg' | 'png'; export type AppRepositoryModelsResponseFormat = 'b64_json' | 'url'; export type CommonImageGenerationRequest = {model: string, loras?: (Array | null) | undefined, prompt: string, width?: number | undefined, height?: number | undefined, num_inference_steps?: (number | null) | undefined, seed?: number | undefined, guidance_scale?: (number | null) | undefined, negative_prompt?: string | undefined, response_extension?: ResponseExtension | undefined, response_format?: AppRepositoryModelsResponseFormat | undefined}; export type CreateApiV1ImagesGenerationsArgs = {ai_project_id?: (string | null) | undefined, body: CommonImageGenerationRequest}; export type CreateApiV1ImagesGenerationsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const imageObject = z.object({b64_json: z.string().nullable().optional(), url: z.string().nullable().optional()}); export const imageGenerationResponse = z.object({data: z.array(imageObject), id: z.string()}); 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 }; /** * Generate * * POST /v1/images/generations */ export const createApiV1ImagesGenerations = async (args: CreateApiV1ImagesGenerationsArgs, options?: CreateApiV1ImagesGenerationsOptions) => { const {baseUrl = "https://api.tokenfactory.nebius.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.ai_project_id != null) query.append("ai_project_id", String(args.ai_project_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/images/generations${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(imageGenerationResponse, await res.json()) };