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 V1ImageDetailLevel = 'IMAGE_DETAIL_LEVEL_UNSPECIFIED' | 'IMAGE_DETAIL_LEVEL_VERY_LOW' | 'IMAGE_DETAIL_LEVEL_LOW' | 'IMAGE_DETAIL_LEVEL_MEDIUM' | 'IMAGE_DETAIL_LEVEL_HIGH' | 'IMAGE_DETAIL_LEVEL_VERY_HIGH'; export type V1LabelEvalOptions = {bypassCache?: boolean | undefined, expedited?: boolean | undefined, bundleSize?: number | undefined, threshold?: number | undefined, stack?: string | undefined, imageDetailLevel?: V1ImageDetailLevel | undefined}; export type V1EvaluateLabelsRequest = {contentData: V1ContentData, threshold?: number | undefined, labelVersionIds?: Array | undefined, labelEvalOptions?: V1LabelEvalOptions | undefined, globalContext?: string | undefined, responseKind?: ('score' | 'choice' | 'ordinal' | 'priority') | undefined, abstain?: boolean | undefined}; export type CreateApiV1LabelsEvaluateArgs = {body: V1EvaluateLabelsRequest}; export type CreateApiV1LabelsEvaluateOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; 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 v1EvaluateLabelsResponseLabelResultEvaluation = z.object({score: z.number().optional(), outcome: v1Outcome.optional(), threshold: z.number().optional(), report: v1PolicyEvaluationReport.optional()}); export const v1EvaluateLabelsResponseLabelResult = z.object({labelId: z.string().optional(), labelVersionId: z.string().optional(), jobUuid: z.string().optional(), evaluation: v1EvaluateLabelsResponseLabelResultEvaluation.optional(), error: z.string().optional()}); export const v1LabelChoiceResult = z.object({labelId: z.string().optional(), labelVersionId: z.string().optional(), index: z.number().int().optional(), confidence: z.number().optional(), probabilities: z.record(z.string(), z.number()).optional(), abstained: z.boolean()}); export const v1LabelOrdinalResult = z.object({labelId: z.string().optional(), labelVersionId: z.string().optional(), index: z.number().int().optional(), confidence: z.number().optional(), probabilities: z.record(z.string(), z.number()).optional(), score: z.number().optional(), abstained: z.boolean()}); export const v1LabelPriorityResult = z.object({matched: z.boolean(), labelId: z.string().optional(), labelVersionId: z.string().optional(), index: z.number().int().optional(), score: z.number().optional(), threshold: z.number().optional()}); export const v1LabelAggregateError = z.object({code: z.string().optional(), message: z.string().optional(), labelVersionId: z.string().optional()}); export const v1EvaluateLabelsResponse = z.object({contentHash: z.string().optional(), results: z.array(v1EvaluateLabelsResponseLabelResult).optional(), choice: v1LabelChoiceResult.optional(), ordinal: v1LabelOrdinalResult.optional(), priority: v1LabelPriorityResult.optional(), aggregateError: v1LabelAggregateError.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 }; /** * Evaluate labels * * POST /v1/labels/evaluate */ export const createApiV1LabelsEvaluate = async (args: CreateApiV1LabelsEvaluateArgs, options?: CreateApiV1LabelsEvaluateOptions) => { 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/labels/evaluate${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(v1EvaluateLabelsResponse, await res.json()) };