import {z} from 'zod' export type GetApiV1WorkflowsWorkflowNameMetricsArgs = {workflow_name: string, start_time?: (string | null) | undefined, end_time?: (string | null) | undefined}; export type GetApiV1WorkflowsWorkflowNameMetricsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const scalarMetric = z.object({value: z.union([z.number().int(), z.number()])}); export const timeSeriesMetric = z.object({value: z.array(z.array(z.unknown()))}); export const workflowMetrics = z.object({execution_count: scalarMetric, success_count: scalarMetric, error_count: scalarMetric, average_latency_ms: scalarMetric, latency_over_time: timeSeriesMetric, retry_rate: scalarMetric}); 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 Workflow Metrics * * GET /v1/workflows/{workflow_name}/metrics */ export const getApiV1WorkflowsWorkflowNameMetrics = async (args: GetApiV1WorkflowsWorkflowNameMetricsArgs, options?: GetApiV1WorkflowsWorkflowNameMetricsOptions) => { 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 (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workflows/${encodeURIComponent(String(args.workflow_name))}/metrics${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(workflowMetrics, await res.json()) };