import {z} from 'zod' export type GetApiV1AnnotationsArgs = {CONFIDENT_API_KEY: string, traceUuid?: string | undefined, spanUuid?: string | undefined, threadId?: string | undefined, type?: ('THUMBS_RATING' | 'FIVE_STAR_RATING') | undefined, minRating?: string | undefined, maxRating?: string | undefined, page?: number | undefined, pageSize?: number | undefined, start?: string | undefined, end?: string | undefined, sortBy?: 'createdAt' | undefined, ascending?: ('true' | 'false') | undefined}; export type GetApiV1AnnotationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const userReference = z.object({id: z.string(), email: z.string(), name: z.string().nullable(), image: z.string().nullable()}); export const annotation = z.object({id: z.string(), rating: z.number().int(), type: z.enum(["THUMBS_RATING", "FIVE_STAR_RATING"]), name: z.string().optional(), expectedOutcome: z.string().optional(), expectedOutput: z.string().optional(), explanation: z.string().optional(), createdAt: z.string().optional(), traceUuid: z.string().optional(), spanUuid: z.string().optional(), threadId: z.string().optional(), testCaseId: z.string().optional(), user: userReference.nullable().optional(), userEmail: z.string().nullable().optional()}); export const listAnnotationsResponse = z.object({success: z.boolean(), data: z.object({annotations: z.array(annotation), total: z.number().int(), page: z.number().int(), pageSize: z.number().int()})}); 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 }; /** * List Annotations * * GET /v1/annotations */ export const getApiV1Annotations = async (args: GetApiV1AnnotationsArgs, options?: GetApiV1AnnotationsOptions) => { const {baseUrl = "https://api.confident-ai.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.CONFIDENT_API_KEY != null) headers.set("CONFIDENT_API_KEY", String(args.CONFIDENT_API_KEY)) if (args.traceUuid != null) query.append("traceUuid", String(args.traceUuid)) if (args.spanUuid != null) query.append("spanUuid", String(args.spanUuid)) if (args.threadId != null) query.append("threadId", String(args.threadId)) if (args.type != null) query.append("type", String(args.type)) if (args.minRating != null) query.append("minRating", String(args.minRating)) if (args.maxRating != null) query.append("maxRating", String(args.maxRating)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.ascending != null) query.append("ascending", String(args.ascending)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/annotations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listAnnotationsResponse, await res.json()) };