import {z} from 'zod' export type GetApiAgentsMetricsResultsArgs = {'Cartesia-Version': '2026-03-01', agent_id?: (string | null) | undefined, deployment_id?: (string | null) | undefined, metric_id?: (string | null) | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, call_id?: (string | null) | undefined, starting_after?: (string | null) | undefined, ending_before?: (string | null) | undefined, limit?: (number | null) | undefined}; export type GetApiAgentsMetricsResultsOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const toolCall = z.object({id: z.string().optional(), name: z.string(), arguments: z.record(z.string(), z.unknown()), result: z.string().optional()}); export const logEvent = z.object({event: z.string(), metadata: z.record(z.string(), z.string()), timestamp: z.number()}); export const logMetric = z.object({name: z.string(), value: z.union([z.number(), z.string(), z.boolean()]), timestamp: z.number()}); export const agentTranscript = z.object({role: z.string(), text: z.string().nullable().optional(), start_timestamp: z.number(), end_timestamp: z.number(), tool_calls: z.array(toolCall).nullable().optional(), stt_ttfb: z.number().nullable().optional(), tts_ttfb: z.number().nullable().optional(), log_event: logEvent.optional(), log_metric: logMetric.optional()}); export const agentMetricResultStatus = z.enum(["completed", "failed"]); export const metricResult = z.object({id: z.string(), metricId: z.string(), metricName: z.string(), metricDisplayName: z.string().nullable().optional(), summary: z.string(), transcript: z.array(agentTranscript).nullable().optional(), agentId: z.string(), callId: z.string(), deploymentId: z.string(), result: z.string(), jsonResult: z.record(z.string(), z.unknown()).nullable().optional(), value: z.unknown(), status: agentMetricResultStatus, runId: z.string().nullable().optional(), createdAt: z.string()}); export const listMetricResultsResponse = z.object({data: z.array(metricResult), has_more: z.boolean(), next_page: z.string().nullable().optional()}); 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 }; /** * List Metric Results * * GET /agents/metrics/results */ export const getApiAgentsMetricsResults = async (args: GetApiAgentsMetricsResultsArgs, options?: GetApiAgentsMetricsResultsOptions) => { const {baseUrl = "https://api.cartesia.ai", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Cartesia-Version'] != null) headers.set("Cartesia-Version", String(args['Cartesia-Version'])) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.deployment_id != null) query.append("deployment_id", String(args.deployment_id)) if (args.metric_id != null) query.append("metric_id", String(args.metric_id)) 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 (args.call_id != null) query.append("call_id", String(args.call_id)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) if (args.limit != null) query.append("limit", String(args.limit)) if (APIKeyAuth !== undefined) headers.set('authorization', `Bearer ${APIKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/agents/metrics/results${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(listMetricResultsResponse, await res.json()) };