import {z} from 'zod' export type GetApiChatGetChatSessionSessionIdArgs = {session_id: number}; export type GetApiChatGetChatSessionSessionIdOptions = {baseUrl: string, APIKeyCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const documentSource = z.enum(["ingestion_api", "slack", "web", "google_drive", "gmail", "requesttracker", "github", "gitlab", "guru", "bookstack", "confluence", "discourse", "slab", "salesforce", "jira", "productboard", "file", "notion", "zulip", "linear", "hubspot", "document360", "gong", "google_sites", "zendesk", "loopio", "sharepoint", "teams", "freshdesk"]); export const savedSearchDoc = z.object({document_id: z.string(), chunk_ind: z.number().int(), semantic_identifier: z.string(), link: z.string().optional(), blurb: z.string(), source_type: documentSource, boost: z.number().int(), hidden: z.boolean(), metadata: z.record(z.string(), z.union([z.string(), z.array(z.string())])), score: z.number().optional(), match_highlights: z.array(z.string()), updated_at: z.string().optional(), primary_owners: z.array(z.string()).optional(), secondary_owners: z.array(z.string()).optional(), db_doc_id: z.number().int()}); export const retrievalDocs = z.object({top_documents: z.array(savedSearchDoc)}); export const messageType = z.enum(["system", "user", "assistant"]); export const chatMessageDetail = z.object({message_id: z.number().int(), parent_message: z.number().int().optional(), latest_child_message: z.number().int().optional(), message: z.string(), rephrased_query: z.string().optional(), context_docs: retrievalDocs.optional(), message_type: messageType, time_sent: z.string(), citations: z.record(z.string(), z.number().int()).optional()}); export const chatSessionDetailResponse = z.object({chat_session_id: z.number().int(), description: z.string(), persona_id: z.number().int(), messages: z.array(chatMessageDetail)}); 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 * * GET /chat/get-chat-session/{session_id} */ export const getApiChatGetChatSessionSessionId = async (args: GetApiChatGetChatSessionSessionIdArgs, options: GetApiChatGetChatSessionSessionIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyCookie !== undefined) cookies.push("fastapiusersauth=" + encodeURIComponent(APIKeyCookie)) const url = `${baseUrl.replace(/\/+$/, '')}/chat/get-chat-session/${encodeURIComponent(String(args.session_id))}${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(chatSessionDetailResponse, await res.json()) };