import {z} from 'zod' export type PlaygroundV1CreateMediaAssetRequest = {assetType?: string | undefined, modelId?: string | undefined, negativePrompt?: string | undefined, params?: string | undefined, prompt?: string | undefined, sessionId?: string | undefined}; export type CreateApiV1PlaygroundMediaArgs = {body: PlaygroundV1CreateMediaAssetRequest}; export type CreateApiV1PlaygroundMediaOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const playgroundV1MediaAsset = z.object({assetType: z.string().optional(), billedMicros: z.string().optional(), completedAt: z.string().optional(), createdAt: z.string().optional(), durationMs: z.number().int().optional(), failReason: z.string().optional(), height: z.number().int().optional(), id: z.string().optional(), internalCostMicros: z.string().optional(), mimeType: z.string().optional(), modelId: z.string().optional(), negativePrompt: z.string().optional(), params: z.string().optional(), prompt: z.string().optional(), resultUrl: z.string().optional(), sessionId: z.string().optional(), sizeBytes: z.string().optional(), status: z.string().optional(), thumbnailUrl: z.string().optional(), upstreamTaskId: z.string().optional(), userId: z.string().optional(), width: z.number().int().optional()}); export const playgroundV1CreateMediaAssetReply = z.object({asset: playgroundV1MediaAsset.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 }; /** * CreateMediaAsset 创建媒体资产 * * POST /v1/playground/media */ export const createApiV1PlaygroundMedia = async (args: CreateApiV1PlaygroundMediaArgs, options: CreateApiV1PlaygroundMediaOptions) => { 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/media${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(playgroundV1CreateMediaAssetReply, await res.json()) };