import {z} from 'zod' export type CreateApiApiAiImageEditArgs = {body: {model: string, image: string | Array, prompt: string, mask?: string | undefined, n?: number | undefined, size?: string | undefined, response_format?: ('url' | 'b64_json') | undefined}}; export type CreateApiApiAiImageEditOptions = {baseUrl?: string | undefined, apikeyHeaderXApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const imageResponseVo = z.object({created: z.number(), list: z.array(z.object({url: z.string().optional(), b64_json: z.string().optional(), revised_prompt: z.string().optional()})), usage: z.object({input_tokens: z.number().optional(), output_tokens: z.number().optional(), total_tokens: z.number().optional()}).optional(), background: z.string().optional(), output_format: z.string().optional(), quality: z.string().optional(), size: z.string().optional()}); export const createApiApiAiImageEditResponse = z.object({data: imageResponseVo.optional(), code: z.number().int(), message: z.string(), requestId: z.string().optional(), timestamp: 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 }; /** * 编辑图像 * * POST /api/ai/image/edit */ export const createApiApiAiImageEdit = async (args: CreateApiApiAiImageEditArgs, options?: CreateApiApiAiImageEditOptions) => { const {baseUrl = "https://aitoearn.cn", fetch: fetchFn = fetch, apikeyHeaderXApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apikeyHeaderXApiKey !== undefined) headers.set("X-Api-Key", apikeyHeaderXApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/ai/image/edit${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(createApiApiAiImageEditResponse, await res.json()) };