import {z} from 'zod' export type PlaygroundV1GenerateImageRequest = {apiKeyId?: string | undefined, model?: string | undefined, modelId?: string | undefined, n?: number | undefined, prompt?: string | undefined, quality?: string | undefined, responseFormat?: string | undefined, sessionId?: string | undefined, size?: string | undefined, style?: string | undefined}; export type CreateApiV1PlaygroundImagesGenerationsArgs = {body: PlaygroundV1GenerateImageRequest}; export type CreateApiV1PlaygroundImagesGenerationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const playgroundV1GeneratedImage = z.object({b64Json: z.string().optional(), revisedPrompt: z.string().optional(), url: z.string().optional()}); export const playgroundV1GenerateImageReply = z.object({assetId: z.string().optional(), created: z.string().optional(), data: z.array(playgroundV1GeneratedImage).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 }; /** * GenerateImage 图像生成(同步下载上游结果转存对象存储,返回 presign-download URL) * * POST /v1/playground/images/generations */ export const createApiV1PlaygroundImagesGenerations = async (args: CreateApiV1PlaygroundImagesGenerationsArgs, options: CreateApiV1PlaygroundImagesGenerationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/playground/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(playgroundV1GenerateImageReply, await res.json()) };