import {z} from 'zod' export type UUID = string; export type GetApiApiV1EeProjectsProjectUuidAiDeepResearchAiDeepResearchRunUuidEventsArgs = {projectUuid: UUID, aiDeepResearchRunUuid: UUID, cursor?: string | undefined, limit?: number | undefined}; export type GetApiApiV1EeProjectsProjectUuidAiDeepResearchAiDeepResearchRunUuidEventsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const aiDeepResearchRunStatus = z.enum(["queued", "running", "failed", "completed", "cancelled", "partially_completed"]); export const recordStringNever = z.object({}); export const aiDeepResearchActivity = z.enum(["lightdash_metadata", "warehouse_query", "web_search", "web_fetch", "reporting"]); export const aiDeepResearchPhase = z.enum(["planning", "investigating", "validating", "synthesizing"]); export const aiDeepResearchProgress = z.object({total: z.number().nullable(), current: z.number().nullable(), activity: aiDeepResearchActivity.nullable(), phase: aiDeepResearchPhase}); export const aiDeepResearchEvent = z.union([z.object({createdAt: z.string(), payload: z.object({status: aiDeepResearchRunStatus}), eventType: z.literal("status_changed"), aiDeepResearchRunUuid: z.string(), aiDeepResearchEventUuid: z.string()}), z.object({createdAt: z.string(), payload: recordStringNever, eventType: z.literal("cancellation_requested"), aiDeepResearchRunUuid: z.string(), aiDeepResearchEventUuid: z.string()}), z.object({createdAt: z.string(), payload: z.object({progress: aiDeepResearchProgress}), eventType: z.literal("progress"), aiDeepResearchRunUuid: z.string(), aiDeepResearchEventUuid: z.string()})]); export const aiDeepResearchEventsPage = z.object({nextCursor: z.string().nullable(), events: z.array(aiDeepResearchEvent)}); export const apiAiDeepResearchEventsResponse = z.object({results: aiDeepResearchEventsPage, status: z.literal("ok")}); 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 Deep Research events * * GET /api/v1/ee/projects/{projectUuid}/ai-deep-research/{aiDeepResearchRunUuid}/events */ export const getApiApiV1EeProjectsProjectUuidAiDeepResearchAiDeepResearchRunUuidEvents = async (args: GetApiApiV1EeProjectsProjectUuidAiDeepResearchAiDeepResearchRunUuidEventsArgs, options: GetApiApiV1EeProjectsProjectUuidAiDeepResearchAiDeepResearchRunUuidEventsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/ee/projects/${encodeURIComponent(String(args.projectUuid))}/ai-deep-research/${encodeURIComponent(String(args.aiDeepResearchRunUuid))}/events${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(apiAiDeepResearchEventsResponse, await res.json()) };