import {z} from 'zod' export type ChapterStyleV0 = 'descriptive' | 'punchy' | 'educational' | 'seo' | 'platform_neutral'; export type ChapterGranularityV0 = 'coarse' | 'balanced' | 'fine'; export type OutputSteeringBrandTermsV0 = Array; export type GenerateChaptersOutputSteeringV0 = {chapter_style?: ChapterStyleV0 | undefined, chapter_granularity?: ChapterGranularityV0 | undefined, audience?: string | undefined, brand_terms?: OutputSteeringBrandTermsV0 | undefined}; export type GenerateChaptersJobParametersV0 = {asset_id: string, language_code?: string | undefined, output_language_code?: string | undefined, output_steering?: GenerateChaptersOutputSteeringV0 | undefined, prompt_overrides?: ({task?: string | undefined, output_format?: string | undefined, chapter_guidelines?: string | undefined, title_guidelines?: string | undefined}) | undefined, update_asset_chapters?: boolean | undefined}; export type CreateGenerateChaptersJobRequestV0 = {passthrough?: string | undefined, parameters: GenerateChaptersJobParametersV0}; export type CreateApiRobotsV0JobsGenerateChaptersArgs = {body: CreateGenerateChaptersJobRequestV0}; export type CreateApiRobotsV0JobsGenerateChaptersOptions = {baseUrl?: string | undefined, accessToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const jobDirectiveContextV0 = z.object({id: z.string(), run_id: z.string()}); export const chapterStyleV0 = z.enum(["descriptive", "punchy", "educational", "seo", "platform_neutral"]); export const chapterGranularityV0 = z.enum(["coarse", "balanced", "fine"]); export const outputSteeringBrandTermsV0 = z.array(z.string().min(1).max(40)); export const generateChaptersOutputSteeringV0 = z.object({chapter_style: chapterStyleV0.optional(), chapter_granularity: chapterGranularityV0.optional(), audience: z.string().min(1).max(160).optional(), brand_terms: outputSteeringBrandTermsV0.optional()}); export const generateChaptersJobParametersV0 = z.object({asset_id: z.string().min(1), language_code: z.string().min(1).optional(), output_language_code: z.string().min(1).optional(), output_steering: generateChaptersOutputSteeringV0.optional(), prompt_overrides: z.object({task: z.string().min(1).optional(), output_format: z.string().min(1).optional(), chapter_guidelines: z.string().min(1).optional(), title_guidelines: z.string().min(1).optional()}).optional(), update_asset_chapters: z.boolean().optional()}); export const jobStatusV0 = z.enum(["pending", "processing", "completed", "errored", "cancelled"]); export const jobErrorV0 = z.object({type: z.string(), message: z.string(), retryable: z.boolean().optional()}); export const slimlineAssetV0 = z.object({id: z.string(), meta: z.object({title: z.string().optional(), creator_id: z.string().optional(), external_id: z.string().optional()}).optional(), passthrough: z.string().optional(), _links: z.object({self: z.object({href: z.string()})})}); export const resourcesV0 = z.object({assets: z.array(slimlineAssetV0)}); export const generateChaptersAssetUpdateStatusV0 = z.enum(["created", "failed"]); export const generateChaptersLegacyAssetUpdateV0 = z.object({status: generateChaptersAssetUpdateStatusV0, track_id: z.string().min(1).optional(), replaced_track_id: z.string().min(1).optional()}); export const generateChaptersAssetUpdateV0 = z.object({status: generateChaptersAssetUpdateStatusV0, track_id: z.string().min(1).optional(), replaced_track_id: z.string().min(1).optional()}); export const generateChaptersAssetUpdateOutcomesV0 = z.object({chapters: generateChaptersAssetUpdateV0.optional()}); export const generateChaptersJobOutputsV0 = z.object({chapters: z.array(z.object({start_time: z.number().gte(0), title: z.string().min(1)})), asset_update: generateChaptersLegacyAssetUpdateV0.optional(), asset_updates: generateChaptersAssetUpdateOutcomesV0.optional()}); export const generateChaptersJobV0 = z.object({id: z.string(), passthrough: z.string().optional(), units_consumed: z.number().int().gte(0), directive: jobDirectiveContextV0.optional(), created_at: z.number().int().gte(0), updated_at: z.number().int().gte(0), workflow: z.literal("generate-chapters"), parameters: generateChaptersJobParametersV0, status: jobStatusV0, errors: z.array(jobErrorV0).optional(), resources: resourcesV0.optional(), outputs: generateChaptersJobOutputsV0.optional()}); export const generateChaptersJobResponseV0 = z.object({data: generateChaptersJobV0}); 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 'generate-chapters' job * * POST /robots/v0/jobs/generate-chapters * * Also accepts auth: authorizationToken */ export const createApiRobotsV0JobsGenerateChapters = async (args: CreateApiRobotsV0JobsGenerateChaptersArgs, options?: CreateApiRobotsV0JobsGenerateChaptersOptions) => { const {baseUrl = "https://api.mux.com", fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (accessToken !== undefined) headers.set('authorization', `Basic ${btoa(`${accessToken.username}:${accessToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/robots/v0/jobs/generate-chapters${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(generateChaptersJobResponseV0, await res.json()) };