import {z} from 'zod' export type LibraryId = string; export type AssetId = string; export type Scope = {library_ids: Array, asset_ids: Array}; export type ExecutionPolicy = 'local_only' | 'prefer_local' | 'cloud_allowed' | 'cloud_required'; export type SearchRequest = {query: string, scope: Scope, execution_policy: ExecutionPolicy, limit?: number | undefined}; export type CreateApiV1SearchArgs = {body: SearchRequest}; export type CreateApiV1SearchOptions = {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 evidenceId = z.string(); export const assetId = z.string(); export const evidenceLocator = z.object({type: z.enum(["local", "cloud"]), url: z.string()}); export const evidence = z.object({id: evidenceId, asset_id: assetId, kind: z.enum(["transcript", "frame", "video_clip", "segment"]), start_seconds: z.number().gte(0), end_seconds: z.number().gte(0), quote: z.string(), locators: z.array(evidenceLocator)}); export const searchResult = z.object({evidence: evidence, score: z.number().gte(0).lte(1)}); export const searchResponse = z.object({request_id: requestId, execution: execution, usage: usage, warnings: z.array(z.string()), data: z.array(searchResult)}); 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 }; /** * Search scoped libraries and assets for evidence * * POST /v1/search * * Also accepts auth: apiKeyAuth */ export const createApiV1Search = async (args: CreateApiV1SearchArgs, options?: CreateApiV1SearchOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/search${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(searchResponse, await res.json()) };