import {z} from 'zod' export type SearchType = 'keyword' | 'semantic' | 'hybrid'; 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 DocumentSearchRequest = {message: string, search_type: SearchType, retrieval_options: RetrievalDetails, recency_bias_multiplier?: number | undefined, skip_rerank?: boolean | undefined}; export type CreateApiQueryDocumentSearchArgs = {disable_llm_chunk_filter?: boolean | undefined, body: DocumentSearchRequest}; export type CreateApiQueryDocumentSearchOptions = {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 searchResponse = z.object({top_documents: z.array(savedSearchDoc), llm_indices: z.array(z.number().int())}); 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 Search Request * * POST /query/document-search */ export const createApiQueryDocumentSearch = async (args: CreateApiQueryDocumentSearchArgs, options: CreateApiQueryDocumentSearchOptions) => { 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 (args.disable_llm_chunk_filter != null) query.append("disable_llm_chunk_filter", String(args.disable_llm_chunk_filter)) if (APIKeyCookie !== undefined) cookies.push("fastapiusersauth=" + encodeURIComponent(APIKeyCookie)) const url = `${baseUrl.replace(/\/+$/, '')}/query/document-search${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(searchResponse, await res.json()) };