import {z} from 'zod' export type V1ContentData = {contentHash?: string | undefined, text?: string | undefined, image?: unknown | undefined, imageUrl?: string | undefined, metadata?: {[key: string]: string} | undefined}; export type V1CreateJobRequestWebhook = {url: string, extraHeaders?: {[key: string]: string} | undefined}; export type V1CreateJobRequestOptions = {options?: {[key: string]: string} | undefined}; export type V1CreateJobRequest = {contentData: Array, policyId: string, threshold?: number | undefined, expedited?: boolean | undefined, webhook?: V1CreateJobRequestWebhook | undefined, options?: V1CreateJobRequestOptions | undefined}; export type CreateApiV1JobsArgs = {body: V1CreateJobRequest}; export type CreateApiV1JobsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const v1JobStatus = z.enum(["JOB_STATUS_UNSPECIFIED", "JOB_STATUS_PENDING", "JOB_STATUS_RUNNING", "JOB_STATUS_COMPLETED", "JOB_STATUS_FAILED", "JOB_STATUS_CANCELED"]); export const v1ContentData = z.object({contentHash: z.string().optional(), text: z.string().optional(), image: z.string().optional(), imageUrl: z.string().optional(), metadata: z.record(z.string(), z.string()).optional()}); export const v1Outcome = z.enum(["OUTCOME_UNSPECIFIED", "OUTCOME_FALSE", "OUTCOME_TRUE", "OUTCOME_FAILED"]); export const policyEvaluationReportAssertionEvaluationReport = z.object({result: v1Outcome.optional(), message: z.string().optional(), score: z.number().optional()}); export const policyEvaluationReportReviewResult = z.object({outcome: v1Outcome.optional(), score: z.number().optional()}); export const policyEvaluationReportExceptionEvaluationReport = z.object({result: v1Outcome.optional(), assertionEvaluationReports: z.array(policyEvaluationReportAssertionEvaluationReport).optional(), reviewResult: policyEvaluationReportReviewResult.optional()}); export const policyEvaluationReportSectionEvaluationReport = z.object({name: z.string().optional(), result: v1Outcome.optional(), message: z.string().optional(), assertionEvaluationReports: z.array(policyEvaluationReportAssertionEvaluationReport).optional(), exceptionEvaluationReport: policyEvaluationReportExceptionEvaluationReport.optional(), reviewResult: policyEvaluationReportReviewResult.optional()}); export const policyEvaluationReportTokenUsage = z.object({inputTokens: z.number().int().optional(), billedTokens: z.number().int().optional(), multiplier: z.number().optional()}); export const v1PolicyEvaluationReport = z.object({policyId: z.string().optional(), policyKey: z.string().optional(), policyVersionId: z.string().optional(), result: v1Outcome.optional(), sectionEvaluationReports: z.array(policyEvaluationReportSectionEvaluationReport).optional(), exceptionEvaluationReport: policyEvaluationReportExceptionEvaluationReport.optional(), contentHash: z.string().optional(), contentMetadata: z.record(z.string(), z.string()).optional(), reviewResult: policyEvaluationReportReviewResult.optional(), threshold: z.number().optional(), labelMatches: z.record(z.string(), z.number()).optional(), tokenUsage: policyEvaluationReportTokenUsage.optional(), labelId: z.string().optional(), labelVersionId: z.string().optional()}); export const v1JobResult = z.object({jobUuid: z.string().optional(), report: v1PolicyEvaluationReport.optional(), created: z.string().optional()}); export const v1JobType = z.enum(["JOB_TYPE_UNSPECIFIED", "JOB_TYPE_STANDARD", "JOB_TYPE_BATCH", "JOB_TYPE_FREE", "JOB_TYPE_TEST"]); export const v1JobErrorReason = z.enum(["JOB_ERROR_REASON_UNSPECIFIED", "JOB_ERROR_REASON_JOB_FAILED", "JOB_ERROR_REASON_VIDEO_VALIDATION_FAILURE", "JOB_ERROR_REASON_VIDEO_SCENE_CEILING_EXCEEDED", "JOB_ERROR_REASON_HLS_REMUX_TIMEOUT_EXHAUSTED", "JOB_ERROR_REASON_EXECUTION_DEADLINE_EXCEEDED", "JOB_ERROR_REASON_ADMISSION_QUEUE_TIMEOUT", "JOB_ERROR_REASON_ADMISSION_FAILED", "JOB_ERROR_REASON_TRANSCRIPT_CHUNK_ARTIFACT_INVALID", "JOB_ERROR_REASON_QUOTA_EXCEEDED", "JOB_ERROR_REASON_CONTENT_BLOCKED", "JOB_ERROR_REASON_CONTENT_PRECHECK_FAILED", "JOB_ERROR_REASON_VIDEO_SOURCE_UNREACHABLE", "JOB_ERROR_REASON_CONTENT_TOO_LARGE"]); export const v1JobError = z.object({reason: v1JobErrorReason.optional(), message: z.string().optional()}); export const v1Job = z.object({jobUuid: z.string().optional(), customerId: z.string().optional(), status: v1JobStatus.optional(), contentData: z.array(v1ContentData).optional(), results: z.array(v1JobResult).optional(), created: z.string().optional(), updated: z.string().optional(), completed: z.string().optional(), policyId: z.string().optional(), policyVersionId: z.string().optional(), threshold: z.number().optional(), labelId: z.string().optional(), labelVersionId: z.string().optional(), jobType: v1JobType.optional(), error: v1JobError.optional()}); export const v1CreateJobResponse = z.object({job: v1Job.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 new job * * POST /v1/jobs */ export const createApiV1Jobs = async (args: CreateApiV1JobsArgs, options?: CreateApiV1JobsOptions) => { const {baseUrl = "https://gateway.app.clavata.ai:8443", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/jobs${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(v1CreateJobResponse, await res.json()) };