import {z} from 'zod' export type AiThreadCreatedFrom = 'slack' | 'web_app' | 'evals' | 'scheduler'; export type GetApiApiV1ProjectsProjectUuidAiAgentsThreadsArgs = {projectUuid: string, page?: number | undefined, pageSize?: number | undefined, agentUuid?: string | undefined, createdFrom?: AiThreadCreatedFrom | undefined, search?: string | undefined}; export type GetApiApiV1ProjectsProjectUuidAiAgentsThreadsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const aiAgentUser = z.object({name: z.string(), uuid: z.string()}); export const aiThreadCreatedFrom = z.enum(["slack", "web_app", "evals", "scheduler"]); export const aiAgentProjectThreadSummary = z.object({user: aiAgentUser, firstMessage: z.object({message: z.string(), uuid: z.string()}), titleGeneratedAt: z.string().nullable(), title: z.string().nullable(), createdFrom: aiThreadCreatedFrom, createdAt: z.string(), agentUuid: z.string(), uuid: z.string(), agentImageUrl: z.string().nullable(), agentName: z.string()}); export const knexPaginatedDataAiAgentProjectThreadSummaryArray = z.object({pagination: z.object({page: z.number(), pageSize: z.number(), totalResults: z.number(), totalPageCount: z.number()}).optional(), data: z.array(aiAgentProjectThreadSummary)}); export const apiAiAgentProjectThreadSummaryListResponse = z.object({results: knexPaginatedDataAiAgentProjectThreadSummaryArray, 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 }; /** * GET /api/v1/projects/{projectUuid}/aiAgents/threads */ export const getApiApiV1ProjectsProjectUuidAiAgentsThreads = async (args: GetApiApiV1ProjectsProjectUuidAiAgentsThreadsArgs, options: GetApiApiV1ProjectsProjectUuidAiAgentsThreadsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = 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.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.agentUuid != null) query.append("agentUuid", String(args.agentUuid)) if (args.createdFrom != null) query.append("createdFrom", String(args.createdFrom)) if (args.search != null) query.append("search", String(args.search)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.projectUuid))}/aiAgents/threads${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(apiAiAgentProjectThreadSummaryListResponse, await res.json()) };