import {z} from 'zod' export type GetApiApiV1TextAiTextAgentsAgentIdFindingsArgs = {agent_id: string, window?: string | undefined, group_by?: string | undefined, limit?: number | undefined, include_resolved?: boolean | undefined}; export type GetApiApiV1TextAiTextAgentsAgentIdFindingsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const subjectRead = z.object({kind: z.enum(["agent", "group"]), id: z.string()}); export const findingsGroupBucket = z.object({key: z.string(), count: z.number().int()}); export const evaluationFindingRead = z.object({id: z.string(), tenant_id: z.string(), voice_call_id: z.string().nullable(), voice_call_external_id: z.string().nullable().optional(), voice_agent_id: z.string().nullable(), voice_agent_version_id: z.string().nullable(), text_session_id: z.string().nullable().optional(), text_agent_id: z.string().nullable().optional(), text_agent_name: z.string().nullable().optional(), source_environment: z.string().nullable().optional(), raw_payload: z.record(z.string(), z.unknown()), dismissed_reason: z.string().nullable(), dismissed_at: z.string().nullable(), batched_in_flywheel_run_id: z.string().nullable(), resolved_at: z.string().nullable(), resolved_by_user_id: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); export const findingsQueryResponse = z.object({subject: subjectRead, window: z.string(), total: z.number().int(), groups: z.array(findingsGroupBucket), items: z.array(evaluationFindingRead)}); 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 Text Agent Findings Route * * GET /api/v1/text-ai/text-agents/{agent_id}/findings */ export const getApiApiV1TextAiTextAgentsAgentIdFindings = async (args: GetApiApiV1TextAiTextAgentsAgentIdFindingsArgs, options: GetApiApiV1TextAiTextAgentsAgentIdFindingsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.window != null) query.append("window", String(args.window)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.include_resolved != null) query.append("include_resolved", String(args.include_resolved)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/text-ai/text-agents/${encodeURIComponent(String(args.agent_id))}/findings${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(findingsQueryResponse, await res.json()) };