import {z} from 'zod' export type SpanEvaluationsRequest = {search_expression?: (string | null) | undefined}; export type CreateApiV1ObservabilitySpansEvaluationsSearchArgs = {from?: (string | null) | undefined, to?: (string | null) | undefined, page_size?: number | undefined, cursor?: (string | null) | undefined, body: SpanEvaluationsRequest}; export type CreateApiV1ObservabilitySpansEvaluationsSearchOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getSpanEvaluation = z.object({customer_id: z.string(), organization_id: z.string(), workspace_id: z.string(), user_id: z.string(), trace_id: z.string(), span_id: z.string(), response_id: z.string(), conversation_id: z.string(), timestamp: z.string(), evaluation_name: z.string(), score_value: z.number(), score_label: z.string(), explanation: z.string(), metadata: z.record(z.string(), z.string())}); export const feedResultGetSpanEvaluation = z.object({results: z.array(getSpanEvaluation).optional(), next: z.string().nullable().optional(), cursor: z.string().nullable().optional()}); export const getSpanEvaluations = z.object({span_evaluations: feedResultGetSpanEvaluation}); 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 span evaluations * * POST /v1/observability/spans/evaluations/search */ export const createApiV1ObservabilitySpansEvaluationsSearch = async (args: CreateApiV1ObservabilitySpansEvaluationsSearchArgs, options?: CreateApiV1ObservabilitySpansEvaluationsSearchOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/observability/spans/evaluations/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(getSpanEvaluations, await res.json()) };