import {z} from 'zod' export type GetApiV1ObservabilityLogsFieldsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const metricAggregation = z.enum(["count", "count_distinct", "sum", "avg", "min", "max", "p50", "p90", "p95", "p99"]); export const otelFieldDefinition = z.object({name: z.string(), label: z.string(), type: z.enum(["ENUM", "TEXT", "INT", "FLOAT", "BOOL", "TIMESTAMP", "ARRAY", "MAP"]), group: z.string().nullable().optional(), source_attribute_key: z.string().nullable().optional(), supported_operators: z.array(z.enum(["eq", "neq", "lt", "lte", "gt", "gte", "like", "ilike", "not_like", "not_ilike", "between", "not_between", "in", "not_in", "exists", "not_exists", "regexp", "not_regexp", "contains", "not_contains", "has", "hasAny", "hasAll", "hasToken"])), supported_aggregations: z.array(metricAggregation)}); export const getLogFields = z.object({field_definitions: z.array(otelFieldDefinition)}); 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 log field definitions * * GET /v1/observability/logs/fields */ export const getApiV1ObservabilityLogsFields = async (options?: GetApiV1ObservabilityLogsFieldsOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/observability/logs/fields${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(getLogFields, await res.json()) };