import {z} from 'zod' export type GetApiRunsMetricsArgs = {period_start_at?: string | undefined, period_end_at?: string | undefined, granularity?: ('hour' | 'day') | undefined, time_zone?: string | undefined, group_by?: ('agent' | 'revision' | 'status' | 'trigger' | 'mode' | 'schedule' | 'device_class') | undefined}; export type GetApiRunsMetricsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const runMetricsPoint = z.object({agent: z.string().optional(), cancelled_runs: z.number().int(), completed_runs: z.number().int(), device_class: z.literal("phone").optional(), duration_p50_ms: z.number().int().nullable(), duration_p95_ms: z.number().int().nullable(), failed_runs: z.number().int(), mode: z.enum(["agent", "code"]).optional(), model_steps: z.number().int(), period_start_at: z.string(), revision: z.string().optional(), runs: z.number().int(), schedule: z.string().optional(), status: z.enum(["completed", "failed", "cancelled"]).optional(), success_rate: z.number().gte(0).lte(1), total_cost_microdollars: z.number().int(), trigger: z.enum(["manual", "scheduled", "api"]).optional()}); export const runMetricsResponse = z.object({points: z.array(runMetricsPoint).nullable()}); 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 run metrics * * GET /runs/metrics */ export const getApiRunsMetrics = async (args: GetApiRunsMetricsArgs, options?: GetApiRunsMetricsOptions) => { const {baseUrl = "https://api.axilio.ai/api/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period_start_at != null) query.append("period_start_at", String(args.period_start_at)) if (args.period_end_at != null) query.append("period_end_at", String(args.period_end_at)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.time_zone != null) query.append("time_zone", String(args.time_zone)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/runs/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(runMetricsResponse, await res.json()) };