import {z} from 'zod' export type EventHistogramParams = {start_time?: string | undefined, end_time?: string | undefined, bucket_seconds?: number | undefined}; export type CreateApiV1EventsMetricsArgs = {limit?: number | undefined, after?: (string | null) | undefined, before?: (string | null) | undefined, include_total?: boolean | undefined, store_identifier?: (string | string | null) | undefined, group_by?: ('store' | null) | undefined, include_previous?: boolean | undefined, body: EventHistogramParams}; export type CreateApiV1EventsMetricsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const eventMetricsIngestion = z.object({files: z.number().int().optional(), high_quality_files: z.number().int().optional(), chunks: z.number().int().optional(), tokens: z.number().int().optional(), bytes: z.number().int().optional(), avg_ingestion_time: z.string().nullable().optional(), avg_queue_time: z.string().nullable().optional()}); export const eventMetricsSearch = z.object({semantic: z.number().int().optional(), reranked: z.number().int().optional(), rewritten: z.number().int().optional(), agentic: z.number().int().optional(), grep: z.number().int().optional(), qa: z.number().int().optional(), avg_search_time: z.string().nullable().optional(), p95_search_time: z.string().nullable().optional(), avg_agentic_search_time: z.string().nullable().optional(), p95_agentic_search_time: z.string().nullable().optional(), agentic_prompt_tokens: z.number().int().optional(), agentic_completion_tokens: z.number().int().optional(), avg_agentic_rounds: z.number().nullable().optional()}); export const eventMetricsCompletion = z.object({count: z.number().int().optional(), responses: z.number().int().optional(), prompt_tokens: z.number().int().optional(), cached_tokens: z.number().int().optional(), completion_tokens: z.number().int().optional(), tool_calls: z.number().int().optional(), function_tool_calls: z.number().int().optional(), avg_total_time: z.string().nullable().optional()}); export const eventMetricsEmbedding = z.object({requests: z.number().int().optional(), tokens: z.number().int().optional(), avg_time: z.string().nullable().optional(), p95_time: z.string().nullable().optional()}); export const eventMetricsReranking = z.object({requests: z.number().int().optional(), tokens: z.number().int().optional(), documents: z.number().int().optional(), avg_time: z.string().nullable().optional(), p95_time: z.string().nullable().optional()}); export const eventMetricsBucket = z.object({bucket_start: z.string(), bucket_end: z.string(), store_id: z.string().nullable().optional(), ingestion: eventMetricsIngestion, search: eventMetricsSearch, completion: eventMetricsCompletion, embedding: eventMetricsEmbedding, reranking: eventMetricsReranking}); export const cursorPaginationResponse = z.object({has_more: z.boolean(), first_cursor: z.string().nullable(), last_cursor: z.string().nullable(), total: z.number().int().nullable().optional()}); export const eventMetricsResponse = z.object({data: z.array(eventMetricsBucket), totals: z.array(eventMetricsBucket), previous_totals: z.array(eventMetricsBucket).nullable().optional(), pagination: cursorPaginationResponse.nullable().optional(), object: z.literal("event_metrics").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 }; /** * Get event metrics * * POST /v1/events/metrics */ export const createApiV1EventsMetrics = async (args: CreateApiV1EventsMetricsArgs, options?: CreateApiV1EventsMetricsOptions) => { const {baseUrl = "https://api.mixedbread.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.include_total != null) query.append("include_total", String(args.include_total)) if (args.store_identifier != null) query.append("store_identifier", String(args.store_identifier)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (args.include_previous != null) query.append("include_previous", String(args.include_previous)) if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/events/metrics${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(eventMetricsResponse, await res.json()) };