import {z} from 'zod' export type FileItem = {path: string, content: string}; export type DatabaseFileItem = {filename: string, content: string}; export type SourceCreateRequest = {type: 'repository' | 'documentation' | 'research_paper' | 'huggingface_dataset' | 'local_folder' | 'slack' | 'google_drive', repository?: (string | null) | undefined, branch?: (string | null) | undefined, ref?: (string | null) | undefined, url?: (string | null) | undefined, url_patterns?: (Array | null) | undefined, exclude_patterns?: (Array | null) | undefined, project_id?: (string | null) | undefined, max_age?: (number | null) | undefined, formats?: (Array | null) | undefined, only_main_content?: (boolean | null) | undefined, limit?: (number | null) | undefined, max_depth?: (number | null) | undefined, crawl_entire_domain?: (boolean | null) | undefined, wait_for?: (number | null) | undefined, include_screenshot?: (boolean | null) | undefined, check_llms_txt?: (boolean | null) | undefined, llms_txt_strategy?: (string | null) | undefined, add_as_global_source?: (boolean | null) | undefined, is_pdf?: (boolean | null) | undefined, is_spreadsheet?: (boolean | null) | undefined, display_name?: (string | null) | undefined, gcs_path?: (string | null) | undefined, focus_instructions?: (string | null) | undefined, extract_branding?: (boolean | null) | undefined, extract_images?: (boolean | null) | undefined, config?: (string | null) | undefined, folder_name?: (string | null) | undefined, folder_path?: (string | null) | undefined, files?: (Array | null) | undefined, database?: (DatabaseFileItem | null) | undefined, slack_installation_id?: (string | null) | undefined, google_drive_installation_id?: (string | null) | undefined, google_drive_file_ids?: (Array | null) | undefined, google_drive_folder_ids?: (Array | null) | undefined}; export type CreateApiSourcesArgs = {body: SourceCreateRequest}; export type CreateApiSourcesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sourceTrustSignals = z.object({trust_level: z.enum(["low", "medium", "high"]).optional(), trust_tier: z.enum(["low", "medium", "high", "verified"]).optional(), scope: z.enum(["private", "organization", "global"]).optional(), is_global: z.boolean().optional(), is_verified: z.boolean().optional(), overlay_available: z.boolean().optional(), annotation_count: z.number().int().optional()}); export const sourceCurationSummary = z.object({trust_signals: sourceTrustSignals.optional(), overlay_summary: z.string().nullable().optional(), recommended_queries: z.array(z.string()).optional()}); export const source = z.object({id: z.string(), type: z.enum(["repository", "documentation", "research_paper", "huggingface_dataset", "local_folder", "slack", "google_drive"]), identifier: z.string().nullable().optional(), display_name: z.string().nullable().optional(), status: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), category_id: z.string().nullable().optional(), is_global: z.boolean().nullable().optional(), global_source_id: z.string().nullable().optional(), global_namespace: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).optional(), curation: sourceCurationSummary.nullable().optional()}); 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 Source * * POST /sources */ export const createApiSources = async (args: CreateApiSourcesArgs, options?: CreateApiSourcesOptions) => { const {baseUrl = "https://apigcp.trynia.ai/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/sources${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(source, await res.json()) };