import {z} from 'zod' export type GetApiSessionsJobIdArgs = {job_id: string}; export type GetApiSessionsJobIdOptions = {baseUrl?: string | undefined, MotionApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const motionJobOutput = z.object({status: z.enum(["pending", "queued", "processing", "completed", "failed", "outdated"]), download_url: z.string().nullable(), expires_at: z.string().nullable(), completed_at: z.string().nullable(), error: z.string().nullable()}); export const motionCompositionSourceUsage = z.object({start_seconds: z.number().nullable().optional(), end_seconds: z.number().nullable().optional(), duration_seconds: z.number().nullable().optional(), scene_id: z.string().nullable().optional(), scene_index: z.number().nullable().optional(), slot_key: z.string().nullable().optional(), element_tag: z.string().nullable().optional(), composition_file: z.string().nullable().optional(), composition_id: z.string().nullable().optional(), track_index: z.number().nullable().optional()}); export const motionCompositionSource = z.object({id: z.string(), source_kind: z.enum(["licensed_library", "article", "web_search", "web", "connector", "upload", "generated", "unknown"]), provider: z.string(), provider_asset_id: z.string().nullable(), source_url: z.string().nullable(), display_url: z.string().nullable(), source_domain: z.string().nullable(), media_type: z.enum(["image", "video", "audio", "file", "unknown"]), title: z.string().nullable(), credit: z.string().nullable(), rights_status: z.enum(["licensed", "public", "restricted", "user_provided", "generated", "unknown"]), usages: z.array(motionCompositionSourceUsage), source_metadata: z.record(z.string(), z.unknown()), rights_metadata: z.record(z.string(), z.unknown())}); export const motionJobStatus = z.object({job_id: z.string(), status: z.enum(["queued", "created", "running", "awaiting_user_input", "completed", "failed"]), chat_url: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), completed_at: z.string().nullable(), error: z.string().nullable(), pending_user_input: z.boolean(), output: motionJobOutput.nullable(), sources: z.array(motionCompositionSource).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 }; /** * Get video job status * * GET /sessions/{job_id} */ export const getApiSessionsJobId = async (args: GetApiSessionsJobIdArgs, options?: GetApiSessionsJobIdOptions) => { const {baseUrl = "https://api.motion.so/api/motion", fetch: fetchFn = fetch, MotionApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (MotionApiKey !== undefined) headers.set('authorization', `Bearer ${MotionApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/sessions/${encodeURIComponent(String(args.job_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(motionJobStatus, await res.json()) };