import {z} from 'zod' export type MotionAspectRatio = '16:9' | '9:16' | '1:1' | '4:5' | '21:9'; export type MotionDurationBucket = '<10s' | '10-30s' | '30s-1min' | '1-5min'; export type MotionDesignSystemId = 'mosaic' | 'apple' | 'claude' | 'cursor' | 'linear' | 'vercel' | 'stripe' | 'figma' | 'notion' | 'spotify' | 'supabase' | 'raycast' | 'framer' | 'resend' | 'mintlify' | 'sentry' | 'tesla' | 'nike' | 'shopify' | 'airbnb' | 'posthog'; export type MotionDesignMd = {filename?: string | undefined, sourceUrl?: string | undefined, content: string, jsonFilename?: string | undefined, jsonContent?: string | undefined}; export type MotionAttachment = {url: string, name?: string | undefined, type?: ('image' | 'video' | 'audio' | 'file') | undefined, content_type?: string | undefined}; export type CreateMotionSessionRequest = {prompt: string, aspect_ratio?: MotionAspectRatio | undefined, duration?: MotionDurationBucket | undefined, design_system_id?: MotionDesignSystemId | undefined, design_md?: (string | MotionDesignMd) | undefined, style_reference_url?: string | undefined, attachments?: Array | undefined, engine?: string | undefined, effort?: ('standard' | 'extended' | 'light' | 'medium' | 'high') | undefined, mode?: ('light' | 'medium' | 'high') | undefined, template?: string | undefined}; export type CreateApiSessionsJobIdFollowupsArgs = {job_id: string, body: CreateMotionSessionRequest}; export type CreateApiSessionsJobIdFollowupsOptions = {baseUrl?: string | undefined, MotionApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createMotionSessionResponse = z.object({job_id: z.string(), status: z.literal("queued"), status_url: z.string(), chat_url: z.string().nullable().optional(), engine: z.string().nullable().optional(), effort: z.enum(["standard", "extended", "light", "medium", "high", "null"]).nullable().optional(), template: z.string().nullable().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 }; /** * Create a followup for a completed or input-awaiting video job * * POST /sessions/{job_id}/followups */ export const createApiSessionsJobIdFollowups = async (args: CreateApiSessionsJobIdFollowupsArgs, options?: CreateApiSessionsJobIdFollowupsOptions) => { const {baseUrl = "https://api.motion.so/api/motion", fetch: fetchFn = fetch, MotionApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (MotionApiKey !== undefined) headers.set('authorization', `Bearer ${MotionApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/sessions/${encodeURIComponent(String(args.job_id))}/followups${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(createMotionSessionResponse, await res.json()) };