import {z} from 'zod' export type Section = {text: string, link?: string | undefined}; 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 BasicExpertInfo = {display_name?: string | undefined, first_name?: string | undefined, middle_initial?: string | undefined, last_name?: string | undefined, email?: string | undefined}; export type DocumentBase = {id?: string | undefined, sections: Array
, source?: DocumentSource | undefined, semantic_identifier: string, metadata: {[key: string]: string | Array}, doc_updated_at?: string | undefined, primary_owners?: Array | undefined, secondary_owners?: Array | undefined, title?: string | undefined, from_ingestion_api?: boolean | undefined}; export type IngestionDocument = {document: DocumentBase, connector_id?: number | undefined, connector_name?: string | undefined, credential_id?: number | undefined, create_connector?: boolean | undefined, public_doc?: boolean | undefined}; export type CreateApiRECAPApiDocIngestionArgs = {body: IngestionDocument}; export type CreateApiRECAPApiDocIngestionOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const ingestionResult = z.object({document_id: z.string(), already_existed: z.boolean()}); 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 }; /** * Document Ingestion * * POST /RECAP-api/doc-ingestion */ export const createApiRECAPApiDocIngestion = async (args: CreateApiRECAPApiDocIngestionArgs, options: CreateApiRECAPApiDocIngestionOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/RECAP-api/doc-ingestion${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(ingestionResult, await res.json()) };