import {z} from 'zod' export type GetApiApiV2ProjectsProjectIdInsightsArgs = {project_id: string, page?: number | undefined, page_size?: number | undefined, prompt_template_id?: (string | null) | undefined, agent_id?: (string | null) | undefined}; export type GetApiApiV2ProjectsProjectIdInsightsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const insight = z.object({id: z.string(), name: z.string(), description: z.string(), prompt_template_id: z.string().nullable(), agent_id: z.string().nullable(), record_count: z.number().int(), context_source: z.string(), first_seen: z.number(), last_seen: z.number()}); export const paginationInfo = z.object({page: z.number().int(), page_size: z.number().int(), has_next: z.boolean()}); export const getApiApiV2ProjectsProjectIdInsightsResponse = z.object({data: z.array(insight), pagination: paginationInfo}); 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 Insights * * GET /api/v2/projects/{project_id}/insights */ export const getApiApiV2ProjectsProjectIdInsights = async (args: GetApiApiV2ProjectsProjectIdInsightsArgs, options: GetApiApiV2ProjectsProjectIdInsightsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.prompt_template_id != null) query.append("prompt_template_id", String(args.prompt_template_id)) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/projects/${encodeURIComponent(String(args.project_id))}/insights${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(getApiApiV2ProjectsProjectIdInsightsResponse, await res.json()) };