import {z} from 'zod' export type AssetId = string; export type LibraryId = string; export type ExecutionPolicy = 'local_only' | 'prefer_local' | 'cloud_allowed' | 'cloud_required'; export type IndexRequest = {asset_id: AssetId, library_id: LibraryId, execution_policy: ExecutionPolicy}; export type CreateApiV1IndexArgs = {'Idempotency-Key': string, body: IndexRequest}; export type CreateApiV1IndexOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const requestId = z.string(); export const runtimeLocation = z.enum(["local", "cloud"]); export const execution = z.object({location: runtimeLocation, capability_id: z.string().nullable().optional(), capability_version: z.string().nullable().optional()}); export const usage = z.object({billable: z.boolean(), quantity: z.number().gte(0), unit: z.string(), credits: z.number().gte(0).optional()}); export const jobId = z.string(); export const workflowReference = z.object({id: z.string(), version: z.string()}); export const executionPolicy = z.enum(["local_only", "prefer_local", "cloud_allowed", "cloud_required"]); export const libraryId = z.string(); export const assetId = z.string(); export const scope = z.object({library_ids: z.array(libraryId), asset_ids: z.array(assetId)}); export const job = z.object({id: jobId, status: z.enum(["queued", "running", "succeeded", "failed", "cancel_requested", "canceled"]), capability_id: z.string().nullable().optional(), capability_version: z.string().nullable().optional(), workflow: workflowReference.nullable().optional(), execution_policy: executionPolicy, scope: scope, created_at: z.string(), updated_at: z.string()}); export const jobResponse = z.object({request_id: requestId, execution: execution, usage: usage, warnings: z.array(z.string()), data: job}); 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 an ingestion job for an asset * * POST /v1/index * * Also accepts auth: apiKeyAuth */ export const createApiV1Index = async (args: CreateApiV1IndexArgs, options?: CreateApiV1IndexOptions) => { const {baseUrl = "https://api.cerul.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/index${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(jobResponse, await res.json()) };