import {z} from 'zod' export type GetApiApiV1ReportAiTrendScansScanIdArgs = {scan_id: string}; export type GetApiApiV1ReportAiTrendScansScanIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const trendScanStatus = z.enum(["running", "completed", "failed"]); export const trendScanRunRead = z.object({id: z.string(), tenant_id: z.string(), report_agent_id: z.string(), report_agent_name: z.string().nullable().optional(), window_start: z.string(), window_end: z.string(), status: trendScanStatus, current_stage: z.string().nullable(), triggered_by_user_id: z.string().nullable(), started_at: z.string(), completed_at: z.string().nullable(), total_eligible_investigations: z.number().int(), total_investigations_scanned: z.number().int(), excluded: z.record(z.string(), z.unknown()).nullable(), cached_tag_count: z.number().int(), fresh_tag_count: z.number().int(), total_trends_found: z.number().int(), tagger_version: z.string(), error_message: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); export const trendCategory = z.enum(["demand_revenue", "guest_experience", "service_operations", "loyalty_retention", "routine_informational"]); export const trendRead = z.object({id: z.string(), tenant_id: z.string(), scan_run_id: z.string(), report_agent_id: z.string(), category: trendCategory, sub_topic: z.string(), title: z.string(), headline: z.string(), narrative: z.string(), suggested_action: z.string().nullable(), citation_sets: z.record(z.string(), z.unknown()).nullable(), occurrence_count: z.number().int(), unique_customer_count: z.number().int(), anonymous_count: z.number().int(), cross_channel_guest_count: z.number().int(), channel_breakdown: z.record(z.string(), z.unknown()), time_series: z.array(z.record(z.string(), z.unknown())), flags_summary: z.record(z.string(), z.unknown()), agent_gap_share: z.number(), properties: z.record(z.string(), z.unknown()).nullable(), confidence: z.number(), first_seen_at: z.string(), last_seen_at: z.string(), first_detected_at: z.string().nullable(), recurrence: z.record(z.string(), z.unknown()).nullable(), delta: z.record(z.string(), z.unknown()).nullable(), suppressed_by_feedback: z.boolean(), boosted_by_feedback: z.boolean(), created_at: z.string(), updated_at: z.string()}); export const routineSummary = z.object({count: z.number().int(), sub_topic_summary: z.string()}); export const scanDetailResponse = z.object({scan: trendScanRunRead, trends: z.array(trendRead), routine_summary: routineSummary}); 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 Scan Detail * * GET /api/v1/report-ai/trend-scans/{scan_id} */ export const getApiApiV1ReportAiTrendScansScanId = async (args: GetApiApiV1ReportAiTrendScansScanIdArgs, options: GetApiApiV1ReportAiTrendScansScanIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/report-ai/trend-scans/${encodeURIComponent(String(args.scan_id))}${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(scanDetailResponse, await res.json()) };