import {z} from 'zod' export type ImageGenerateRequest = {prompt: string, aspect_ratio?: string | undefined, image_size?: string | undefined, reference_image_url?: (string | null) | undefined, skip_default_style?: boolean | undefined}; export type CreateApiV1ImagesGenerateArgs = {body: ImageGenerateRequest}; export type CreateApiV1ImagesGenerateOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const unifiedError = z.object({type: z.string(), title: z.string(), status: z.number().int(), detail: z.string().nullable().optional(), is_retriable: z.boolean().optional(), retry_after_seconds: z.number().int().nullable().optional(), alternative_action: z.string().nullable().optional(), error_category: z.string().optional(), caller_error: z.string().nullable().optional()}); export const unifiedMetadata = z.object({provider_used: z.string().nullable().optional(), providers_tried: z.array(z.string()).optional(), mode_used: z.string().nullable().optional(), response_time_ms: z.number().int().optional(), request_id: z.string().optional(), capability: z.string().nullable().optional(), params: z.record(z.string(), z.unknown()).nullable().optional()}); export const unifiedResponse = z.object({success: z.boolean(), data: z.union([z.record(z.string(), z.unknown()), z.array(z.unknown())]).nullable().optional(), error: unifiedError.nullable().optional(), metadata: unifiedMetadata.optional(), credits_used: z.number().int().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 }; /** * Image Generation * * POST /v1/images/generate */ export const createApiV1ImagesGenerate = async (args: CreateApiV1ImagesGenerateArgs, options?: CreateApiV1ImagesGenerateOptions) => { const {baseUrl = "https://suprsonic.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/images/generate${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(unifiedResponse, await res.json()) };