import {z} from 'zod' export type AiThreadCreatedFrom = 'slack' | 'web_app' | 'evals' | 'scheduler'; export type AiAgentAdminSortField = 'createdAt' | 'title'; export type GetApiApiV1AiAgentsAdminThreadsArgs = {page?: number | undefined, pageSize?: number | undefined, projectUuids?: Array | undefined, agentUuids?: Array | undefined, userUuids?: Array | undefined, createdFrom?: AiThreadCreatedFrom | undefined, humanScore?: number | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, search?: string | undefined, sortField?: AiAgentAdminSortField | undefined, sortDirection?: ('asc' | 'desc') | undefined}; export type GetApiApiV1AiAgentsAdminThreadsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const aiAgentAdminFeedbackSummary = z.object({total: z.number(), neutral: z.number(), downvotes: z.number(), upvotes: z.number()}); export const pickAiAgentSummaryUuidOrNameOrImageUrl = z.object({name: z.string(), uuid: z.string(), imageUrl: z.string().nullable()}); export const aiAgentAdminThreadSummary = z.object({uuid: z.string(), title: z.string().nullable(), createdAt: z.string(), user: z.object({name: z.string(), uuid: z.string(), email: z.string().nullable(), slackUserId: z.string().nullable()}), createdFrom: z.enum(["slack", "web_app", "evals", "scheduler"]), slackThreadTs: z.string().nullable(), slackChannelId: z.string().nullable(), promptCount: z.number(), feedbackSummary: aiAgentAdminFeedbackSummary, project: z.object({name: z.string(), uuid: z.string()}), agent: pickAiAgentSummaryUuidOrNameOrImageUrl}); export const aiAgentAdminConversationsSummary = z.object({threads: z.array(aiAgentAdminThreadSummary)}); export const knexPaginatedDataAiAgentAdminConversationsSummary = z.object({pagination: z.object({page: z.number(), pageSize: z.number(), totalResults: z.number(), totalPageCount: z.number()}).optional(), data: aiAgentAdminConversationsSummary}); export const apiAiAgentAdminConversationsResponse = z.object({results: knexPaginatedDataAiAgentAdminConversationsSummary, 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 AI agent threads * * GET /api/v1/aiAgents/admin/threads */ export const getApiApiV1AiAgentsAdminThreads = async (args: GetApiApiV1AiAgentsAdminThreadsArgs, options: GetApiApiV1AiAgentsAdminThreadsOptions) => { 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)) for (const item of args.projectUuids ?? []) query.append("projectUuids", String(item)) for (const item of args.agentUuids ?? []) query.append("agentUuids", String(item)) for (const item of args.userUuids ?? []) query.append("userUuids", String(item)) if (args.createdFrom != null) query.append("createdFrom", String(args.createdFrom)) if (args.humanScore != null) query.append("humanScore", String(args.humanScore)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.search != null) query.append("search", String(args.search)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/aiAgents/admin/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(apiAiAgentAdminConversationsResponse, await res.json()) };