import {z} from 'zod' export type QAFeedbackType = 'like' | 'dislike'; export type GetApiAdminChatSessionHistoryArgs = {feedback_type?: QAFeedbackType | undefined, start?: string | undefined, end?: string | undefined}; export type GetApiAdminChatSessionHistoryOptions = {baseUrl: string, APIKeyCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const messageType = z.enum(["system", "user", "assistant"]); export const abridgedSearchDoc = z.object({document_id: z.string(), semantic_identifier: z.string(), link: z.string().optional()}); export const qAFeedbackType = z.enum(["like", "dislike"]); export const messageSnapshot = z.object({message: z.string(), message_type: messageType, documents: z.array(abridgedSearchDoc), feedback: qAFeedbackType.optional(), time_created: z.string()}); export const chatSessionSnapshot = z.object({id: z.number().int(), user_email: z.string().optional(), name: z.string().optional(), messages: z.array(messageSnapshot), time_created: z.string()}); export const getApiAdminChatSessionHistoryResponse = z.array(chatSessionSnapshot); 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 Chat Session History * * GET /admin/chat-session-history */ export const getApiAdminChatSessionHistory = async (args: GetApiAdminChatSessionHistoryArgs, options: GetApiAdminChatSessionHistoryOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.feedback_type != null) query.append("feedback_type", String(args.feedback_type)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (APIKeyCookie !== undefined) cookies.push("fastapiusersauth=" + encodeURIComponent(APIKeyCookie)) const url = `${baseUrl.replace(/\/+$/, '')}/admin/chat-session-history${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(getApiAdminChatSessionHistoryResponse, await res.json()) };