import {z} from 'zod' export type TableParsingStrategy = 'tsr' | 'vlm' | 'unknown'; export type TableOutputMode = 'markdown' | 'html'; export type FormDetectionMode = 'vlm' | 'tsr' | 'unknown'; export type ChunkStrategy = 'page' | 'section' | 'fragment'; export type ModelProvider = 'tensorlake' | 'claude-3-5-sonnet-latest' | 'gpt-4o-mini'; export type Settings = {tableParsingMode?: TableParsingStrategy | undefined, tableOutputMode?: TableOutputMode | undefined, tableSummarization?: boolean | undefined, tableSummarizationPrompt?: (string | null) | undefined, figureSummarization?: boolean | undefined, figureSummarizationPrompt?: (string | null) | undefined, formDetectionMode?: FormDetectionMode | undefined, chunkStrategy?: (ChunkStrategy | null) | undefined, jsonSchema: unknown, structuredExtractionPrompt?: (string | null) | undefined, modelProvider?: (ModelProvider | null) | undefined, deliverWebhook?: boolean | undefined, detectSignature?: boolean | undefined, skewCorrection?: boolean | undefined, disableLayoutDetection?: boolean | undefined, structuredExtractionSkipOcr?: boolean | undefined, detectStrikethrough?: boolean | undefined}; export type CreateDatasetRequest = {name: string, description?: (string | null) | undefined, settings: Settings}; export type CreateApiDocumentsV1DatasetsArgs = {body: CreateDatasetRequest}; export type CreateApiDocumentsV1DatasetsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createDatasetResponse = z.object({id: z.string()}); 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 }; /** * Create a new dataset. They are used in conjunction with the Structurued Data * or Documetn Parsing API requests. Behavior of datasets when used with the * APIs: * * Structured Extraction API - Structured Data from files are automatically * inserted into datasets. A JSON schema is required to guide the extraction. * * Document Parsing API - Chunks of parsed documents from the Document * Parsing API automatically. * * POST /documents/v1/datasets */ export const createApiDocumentsV1Datasets = async (args: CreateApiDocumentsV1DatasetsArgs, options?: CreateApiDocumentsV1DatasetsOptions) => { const {baseUrl = "https://api.tensorlake.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/documents/v1/datasets${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(createDatasetResponse, await res.json()) };