import {z} from 'zod' export type MetricAggregation = 'count' | 'count_distinct' | 'sum' | 'avg' | 'min' | 'max' | 'p50' | 'p90' | 'p95' | 'p99'; export type MetricDefinition = {measure: string, aggregation: MetricAggregation}; export type Granularity = 'auto' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month'; export type TimeDimension = {granularity?: Granularity | undefined}; export type OrderByClause = {field: string, direction?: ('asc' | 'desc') | undefined}; export type AggregationRequest = {metric: MetricDefinition, dimensions?: Array | undefined, time_dimension?: (TimeDimension | null) | undefined, search_expression?: (string | null) | undefined, order_by?: (Array | null) | undefined, limit?: number | undefined}; export type CreateApiV1ObservabilityTracesAggregateArgs = {from?: (string | null) | undefined, to?: (string | null) | undefined, body: AggregationRequest}; export type CreateApiV1ObservabilityTracesAggregateOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const aggregationRow = z.object({time_bucket: z.string().nullable().optional(), dimensions: z.record(z.string(), z.unknown()).optional(), metric_name: z.string(), metric_value: z.union([z.number(), z.number().int()]).nullable().optional()}); export const aggregationMeta = z.object({from_timestamp: z.string(), to_timestamp: z.string(), granularity_seconds: z.number().int().nullable().optional()}); export const aggregation = z.object({data: z.array(aggregationRow), meta: aggregationMeta}); 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 }; /** * Aggregate traces * * POST /v1/observability/traces/aggregate */ export const createApiV1ObservabilityTracesAggregate = async (args: CreateApiV1ObservabilityTracesAggregateArgs, options?: CreateApiV1ObservabilityTracesAggregateOptions) => { 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 (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/observability/traces/aggregate${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(aggregation, await res.json()) };