import {z} from 'zod' export type SpansRequest = {search_expression?: (string | null) | undefined}; export type CreateApiV1ObservabilitySpansSearchArgs = {from?: (string | null) | undefined, to?: (string | null) | undefined, page_size?: number | undefined, cursor?: (string | null) | undefined, body: SpansRequest}; export type CreateApiV1ObservabilitySpansSearchOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getSpan = 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(), parent_span_id: z.string(), trace_state: z.string(), start_time: z.string(), end_time: z.string(), duration_ns: z.number().int(), span_name: z.string(), span_kind: z.string(), service_name: z.string(), status_code: z.enum(["Error", "Ok", "Unset"]), status_message: z.string(), error_type: z.string(), operation_name: z.string(), provider_name: z.string(), request_model: z.string(), response_model: z.string(), response_id: z.string(), output_type: z.string(), conversation_id: z.string(), data_source_id: z.string(), agent_id: z.string(), agent_name: z.string(), agent_version: z.string(), agent_description: z.string(), workflow_name: z.string(), prompt_name: z.string(), tool_name: z.string(), tool_type: z.string(), tool_call_id: z.string(), input_messages: z.string(), output_messages: z.string(), system_instructions: z.string(), tool_definitions: z.string(), tool_call_arguments: z.string(), tool_call_result: z.string(), request_choice_count: z.number().int(), request_max_tokens: z.number().int(), request_temperature: z.number().nullable(), request_top_p: z.number().nullable(), request_top_k: z.number().nullable(), request_presence_penalty: z.number().nullable(), request_frequency_penalty: z.number().nullable(), request_seed: z.number().int(), request_stop_sequences: z.array(z.string()), request_encoding_formats: z.array(z.string()), response_finish_reasons: z.array(z.string()), usage_input_tokens: z.number().int(), usage_output_tokens: z.number().int(), usage_cache_read_input_tokens: z.number().int(), usage_cache_creation_input_tokens: z.number().int(), resource_attributes: z.record(z.string(), z.string()), span_attributes: z.record(z.string(), z.string()), scope_name: z.string(), scope_version: z.string()}); export const feedResultGetSpan = z.object({results: z.array(getSpan).optional(), next: z.string().nullable().optional(), cursor: z.string().nullable().optional()}); export const getSpans = z.object({spans: feedResultGetSpan}); 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 spans * * POST /v1/observability/spans/search */ export const createApiV1ObservabilitySpansSearch = async (args: CreateApiV1ObservabilitySpansSearchArgs, options?: CreateApiV1ObservabilitySpansSearchOptions) => { 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/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(getSpans, await res.json()) };