import {z} from 'zod' export type GetApiV1KnowledgeGapsClustersArgs = {agent_id?: (string | null) | undefined, gap_type?: (string | null) | undefined, product_area?: (string | null) | undefined, priority?: (string | null) | undefined, status?: (string | null) | undefined, owner_user_id?: (string | null) | undefined, sort?: string | undefined, limit?: number | undefined, cursor?: (string | null) | undefined, start_date: string, end_date: string}; export type GetApiV1KnowledgeGapsClustersOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const clusterListRow = z.object({cluster_id: z.string(), label: z.string().nullable().optional(), agent_id: z.string().nullable().optional(), gap_type: z.string(), product_area: z.string().nullable().optional(), priority: z.string().nullable().optional(), status: z.string(), impact_score: z.number(), member_count: z.number().int(), affected_sessions: z.number().int(), escalation_count: z.number().int(), unresolved_count: z.number().int(), gap_event_count: z.number().int(), owner_user_id: z.string().nullable().optional(), first_seen_at: z.string(), last_seen_at: z.string(), fixed_at: z.string().nullable().optional(), reopened_at: z.string().nullable().optional()}); export const clusterListResponse = z.object({items: z.array(clusterListRow), next_cursor: 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 }; /** * Paginated list of knowledge-gap clusters with filters and sort * * GET /v1/knowledge-gaps/clusters */ export const getApiV1KnowledgeGapsClusters = async (args: GetApiV1KnowledgeGapsClustersArgs, options: GetApiV1KnowledgeGapsClustersOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.gap_type != null) query.append("gap_type", String(args.gap_type)) if (args.product_area != null) query.append("product_area", String(args.product_area)) if (args.priority != null) query.append("priority", String(args.priority)) if (args.status != null) query.append("status", String(args.status)) if (args.owner_user_id != null) query.append("owner_user_id", String(args.owner_user_id)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) 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 (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/knowledge-gaps/clusters${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(clusterListResponse, await res.json()) };