import {z} from 'zod' export type GetApiV1AnalyticsToolsDashboardArgs = {tool_type?: (string | null) | undefined, revision_id?: (string | null) | undefined, top_tools_limit?: number | undefined, health_matrix_limit?: number | undefined, recent_errors_limit?: number | undefined, min_invocations?: number | undefined, latency_ceiling_ms?: number | undefined, start_date: string, end_date: string}; export type GetApiV1AnalyticsToolsDashboardOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const srcAnalyticsToolsSchemasDashboardSummaryRow = z.object({current_invocations: z.number().int().nullable().optional(), previous_invocations: z.number().int().nullable().optional(), pct_change_invocations: z.number().nullable().optional(), current_errors: z.number().int().nullable().optional(), previous_errors: z.number().int().nullable().optional(), pct_change_errors: z.number().nullable().optional(), current_success_rate: z.number().nullable().optional(), previous_success_rate: z.number().nullable().optional(), pct_change_success_rate: z.number().nullable().optional(), current_avg_latency_ms: z.number().nullable().optional(), previous_avg_latency_ms: z.number().nullable().optional(), pct_change_avg_latency_ms: z.number().nullable().optional(), current_p95_latency_ms: z.number().nullable().optional(), previous_p95_latency_ms: z.number().nullable().optional(), pct_change_p95_latency_ms: z.number().nullable().optional(), current_active_tools: z.number().int().nullable().optional(), previous_active_tools: z.number().int().nullable().optional(), pct_change_active_tools: z.number().nullable().optional()}); export const dashboardTopToolsRow = z.object({tool_id: z.string(), tool_name: z.string(), tool_type: z.string(), provider: z.string().nullable().optional(), invocations: z.number().int(), error_count: z.number().int(), success_rate_pct: z.number(), avg_latency_ms: z.number()}); export const dashboardHealthMatrixRow = z.object({tool_id: z.string(), tool_name: z.string(), tool_type: z.string(), invocation_count: z.number().int(), success_rate: z.number().nullable().optional(), p95_latency_ms: z.number().nullable().optional(), current_error_rate: z.number(), previous_error_rate: z.number(), error_trend_delta: z.number(), success_rate_score: z.number(), latency_score: z.number(), error_trend_score: z.number(), health_score: z.number()}); export const dashboardRecentErrorRow = z.object({timestamp: z.string(), tool_id: z.string(), tool_name: z.string(), tool_type: z.string(), provider: z.string().nullable().optional(), agent_id: z.string().nullable().optional(), session_id: z.string().nullable().optional(), error_category: z.string(), status_code: z.number().int().nullable().optional(), error_message: z.string().nullable().optional(), latency_ms: z.number()}); export const srcAnalyticsToolsSchemasDashboardEnvelope = z.object({summary: z.array(srcAnalyticsToolsSchemasDashboardSummaryRow), top_tools: z.array(dashboardTopToolsRow), health_matrix: z.array(dashboardHealthMatrixRow), recent_errors: z.array(dashboardRecentErrorRow)}); 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 }; /** * Combined tool analytics dashboard envelope * * GET /v1/analytics/tools/dashboard */ export const getApiV1AnalyticsToolsDashboard = async (args: GetApiV1AnalyticsToolsDashboardArgs, options: GetApiV1AnalyticsToolsDashboardOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tool_type != null) query.append("tool_type", String(args.tool_type)) if (args.revision_id != null) query.append("revision_id", String(args.revision_id)) if (args.top_tools_limit != null) query.append("top_tools_limit", String(args.top_tools_limit)) if (args.health_matrix_limit != null) query.append("health_matrix_limit", String(args.health_matrix_limit)) if (args.recent_errors_limit != null) query.append("recent_errors_limit", String(args.recent_errors_limit)) if (args.min_invocations != null) query.append("min_invocations", String(args.min_invocations)) if (args.latency_ceiling_ms != null) query.append("latency_ceiling_ms", String(args.latency_ceiling_ms)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/tools/dashboard${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(srcAnalyticsToolsSchemasDashboardEnvelope, await res.json()) };