import {z} from 'zod' export type IngestionStatus = 'discovered' | 'queued' | 'ingesting' | 'ingested' | 'stale' | 'expired' | 'failed' | 'not_required'; export type IngestionLane = 'document' | 'table' | 'media' | 'json'; export type FolderMetadataRequest = {category?: (string | null) | undefined, directory?: string | undefined, ingestion_status?: (IngestionStatus | null) | undefined, kind?: (IngestionLane | null) | undefined, name_contains?: (string | null) | undefined, page_size?: number | undefined, recursive?: boolean | undefined, start_after?: (string | null) | undefined}; export type CreateApiV1WorkspacesWorkspaceIdToolsFolderMetadataArgs = {workspace_id: string, 'Idempotency-Key'?: (string | null) | undefined, body: FolderMetadataRequest}; export type CreateApiV1WorkspacesWorkspaceIdToolsFolderMetadataOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const categoryCount = z.object({count: z.number().int(), name: z.string()}); export const coverage = z.object({labels_required: z.array(z.string()).optional(), request_access_hint: z.string().nullable().optional(), restricted_candidates: z.number().int().optional()}); export const ingestionStatus = z.enum(["discovered", "queued", "ingesting", "ingested", "stale", "expired", "failed", "not_required"]); export const folderFileEntry = z.object({category: z.string().nullable().optional(), file_id: z.string().nullable().optional(), ingestion_status: ingestionStatus, kind: z.string().nullable().optional(), path: z.string(), size_bytes: z.number().int(), uploaded_at: z.string().nullable().optional()}); export const subdirectorySummary = z.object({failed_files: z.number().int().nullable().optional(), name: z.string(), overall_files: z.number().int(), top_categories: z.array(categoryCount).optional()}); export const folderMetadataResponse = z.object({categories: z.array(categoryCount), coverage: coverage, credits: z.number().int().gte(0).nullable().optional(), directory: z.string(), file_entries: z.array(folderFileEntry), files_truncated: z.boolean(), hint: z.string().nullable().optional(), ingestion_summary: z.record(z.string(), z.number().int()), job_id: z.string().nullable().optional(), overall_files: z.number().int(), subdirectories: z.array(subdirectorySummary), waiver: z.enum(["repeat_of_job", "no_job"]).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 }; /** * folder_metadata * * POST /v1/workspaces/{workspace_id}/tools/folder-metadata */ export const createApiV1WorkspacesWorkspaceIdToolsFolderMetadata = async (args: CreateApiV1WorkspacesWorkspaceIdToolsFolderMetadataArgs, options?: CreateApiV1WorkspacesWorkspaceIdToolsFolderMetadataOptions) => { const {baseUrl = "https://ndi-api.nace.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace_id))}/tools/folder-metadata${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(folderMetadataResponse, await res.json()) };