import {z} from 'zod' export type GetApiV1AnalyticsToolsMonitoringHealthCheckArgs = {silent_lookback_seconds?: number | undefined, min_invocations?: number | undefined, error_rate_warning?: number | undefined, error_rate_critical?: number | undefined, p95_latency_warning_ms?: number | undefined, p95_latency_critical_ms?: number | undefined, tool_id?: (string | null) | undefined, tool_type?: (string | null) | undefined, start_date: string, end_date: string}; export type GetApiV1AnalyticsToolsMonitoringHealthCheckOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const healthCheckRow = z.object({tool_id: z.string(), tool_name: z.string(), tool_type: z.string(), invocation_count: z.number().int(), error_count: z.number().int(), error_rate: z.number(), p95_latency_ms: z.number().nullable().optional(), status: z.string(), violations: z.array(z.string())}); export const getApiV1AnalyticsToolsMonitoringHealthCheckResponse = z.array(healthCheckRow); 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 }; /** * Per-tool health (healthy/degraded/critical) with threshold violations * * GET /v1/analytics/tools/monitoring/health-check */ export const getApiV1AnalyticsToolsMonitoringHealthCheck = async (args: GetApiV1AnalyticsToolsMonitoringHealthCheckArgs, options: GetApiV1AnalyticsToolsMonitoringHealthCheckOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.silent_lookback_seconds != null) query.append("silent_lookback_seconds", String(args.silent_lookback_seconds)) if (args.min_invocations != null) query.append("min_invocations", String(args.min_invocations)) if (args.error_rate_warning != null) query.append("error_rate_warning", String(args.error_rate_warning)) if (args.error_rate_critical != null) query.append("error_rate_critical", String(args.error_rate_critical)) if (args.p95_latency_warning_ms != null) query.append("p95_latency_warning_ms", String(args.p95_latency_warning_ms)) if (args.p95_latency_critical_ms != null) query.append("p95_latency_critical_ms", String(args.p95_latency_critical_ms)) if (args.tool_id != null) query.append("tool_id", String(args.tool_id)) if (args.tool_type != null) query.append("tool_type", String(args.tool_type)) 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/monitoring/health-check${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(getApiV1AnalyticsToolsMonitoringHealthCheckResponse, await res.json()) };