import {z} from 'zod' export type OptionalSearchSetting = 'always' | 'never' | 'auto'; export type DocumentSource = '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 type Tag = {tag_key: string, tag_value: string}; export type BaseFilters = {source_type?: Array | undefined, document_set?: Array | undefined, time_cutoff?: string | undefined, tags?: Array | undefined}; export type RetrievalDetails = {run_search?: OptionalSearchSetting | undefined, real_time?: boolean | undefined, filters?: BaseFilters | undefined, enable_auto_detect_filters?: boolean | undefined, offset?: number | undefined, limit?: number | undefined}; export type CreateChatMessageRequest = {chat_session_id: number, parent_message_id?: number | undefined, message: string, prompt_id?: number | undefined, search_doc_ids?: Array | undefined, retrieval_options?: RetrievalDetails | undefined, query_override?: string | undefined, no_ai_answer?: boolean | undefined}; export type CreateApiChatSendMessageArgs = {body: CreateChatMessageRequest}; export type CreateApiChatSendMessageOptions = {baseUrl: string, APIKeyCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiChatSendMessageResponse = z.unknown(); 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 }; /** * Handle New Chat Message * * POST /chat/send-message */ export const createApiChatSendMessage = async (args: CreateApiChatSendMessageArgs, options: CreateApiChatSendMessageOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyCookie !== undefined) cookies.push("fastapiusersauth=" + encodeURIComponent(APIKeyCookie)) const url = `${baseUrl.replace(/\/+$/, '')}/chat/send-message${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiChatSendMessageResponse, await res.json()) };