import {z} from 'zod' export type GetApiApiV1ReportAiInvestigationsArgs = {report_agent_id?: (string | null) | undefined, report_agent_ids?: (Array | null) | undefined, status?: (string | null) | undefined, lifecycle_status?: (string | null) | undefined, source?: (string | null) | undefined, voice_call_id?: (string | null) | undefined, text_session_id?: (string | null) | undefined, since?: (string | null) | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiApiV1ReportAiInvestigationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const reportAgentType = z.enum(["analysis", "action"]); export const triggerSource = z.enum(["voice_call_ended", "text_session_ended", "crm_webhook", "slack_event", "manual", "scheduled"]); export const investigationStatus = z.enum(["pending", "skipped", "processing", "completed", "failed"]); export const skipReason = z.enum(["empty", "spam", "automated", "duplicate", "low_quality", "unsupported_language"]); export const investigationRead = z.object({id: z.string(), created_at: z.string(), tenant_id: z.string(), report_agent_id: z.string(), report_agent_name: z.string(), report_agent_type: reportAgentType, trigger_source: triggerSource, triggered_by_user_id: z.string().nullable(), external_ticket_id: z.string().nullable(), voice_call_id: z.string().nullable(), text_session_id: z.string().nullable(), interaction_summary: z.string(), interaction_content: z.string(), customer_email: z.string().nullable(), customer_name: z.string().nullable(), customer_phone: z.string().nullable(), interaction_metadata: z.record(z.string(), z.unknown()), structured_extraction: z.record(z.string(), z.unknown()).nullable(), status: investigationStatus, started_at: z.string().nullable(), completed_at: z.string().nullable(), error_message: z.string().nullable(), retry_count: z.number().int(), skip_reason: skipReason.nullable(), triage_confidence: z.number().nullable(), triage_details: z.string().nullable(), matched_scenario_id: z.string().nullable(), matched_scenario_name: z.string().nullable(), report_json: z.record(z.string(), z.unknown()).nullable(), customer_profile_id: z.string().nullable(), thread_id: z.string().nullable().optional(), lifecycle_status: z.string().optional(), closed_reason: z.string().nullable().optional(), closed_at: z.string().nullable().optional(), closed_by_user_id: z.string().nullable().optional(), closed_note: z.string().nullable().optional(), superseded_by_id: z.string().nullable().optional(), opened_by_message_id: z.string().nullable().optional()}); export const investigationListResponse = z.object({items: z.array(investigationRead), total: z.number().int(), page: z.number().int(), per_page: z.number().int()}); 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 Investigations * * GET /api/v1/report-ai/investigations */ export const getApiApiV1ReportAiInvestigations = async (args: GetApiApiV1ReportAiInvestigationsArgs, options: GetApiApiV1ReportAiInvestigationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.report_agent_id != null) query.append("report_agent_id", String(args.report_agent_id)) for (const item of args.report_agent_ids ?? []) query.append("report_agent_ids", String(item)) if (args.status != null) query.append("status", String(args.status)) if (args.lifecycle_status != null) query.append("lifecycle_status", String(args.lifecycle_status)) if (args.source != null) query.append("source", String(args.source)) if (args.voice_call_id != null) query.append("voice_call_id", String(args.voice_call_id)) if (args.text_session_id != null) query.append("text_session_id", String(args.text_session_id)) if (args.since != null) query.append("since", String(args.since)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/report-ai/investigations${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(investigationListResponse, await res.json()) };