import {z} from 'zod' export type FileMetadataRequest = {component?: (string | null) | undefined, path: string}; export type CreateApiV1WorkspacesWorkspaceIdToolsFileMetadataArgs = {workspace_id: string, 'Idempotency-Key'?: (string | null) | undefined, body: FileMetadataRequest}; export type CreateApiV1WorkspacesWorkspaceIdToolsFileMetadataOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const fileKeywordDescription = z.record(z.string(), z.string().nullable()); export const fileComponentInfo = z.object({categories: z.array(z.string()).optional(), column_count: z.number().int().nullable().optional(), error: z.string().nullable().optional(), key_terms: z.array(fileKeywordDescription).optional(), kind: z.enum(["section", "tab", "image", "raw"]), name: z.string(), page_end: z.number().int().nullable().optional(), page_start: z.number().int().nullable().optional(), queryable: z.boolean(), readable: z.boolean(), row_count: z.number().int().nullable().optional(), summary: z.string().optional()}); export const mediaComponentInfo = z.object({duration_ms: z.number().int(), kind: z.literal("media").optional(), modality: z.enum(["audio", "video"]), speaker_count: z.number().int(), summary: z.string().optional()}); export const fileFigureInfo = z.object({description: z.string().nullable().optional(), name: z.string(), page: z.number().int(), text: z.string().optional()}); export const ingestionStatus = z.enum(["discovered", "queued", "ingesting", "ingested", "stale", "expired", "failed", "not_required"]); export const fileReadCoverage = z.object({ocr_used: z.boolean(), pages_failed: z.number().int().gte(0), pages_read: z.number().int().gte(0)}); export const fileMetadataToolResponse = z.object({category: z.string().nullable().optional(), components: z.array(z.discriminatedUnion("kind", [fileComponentInfo, mediaComponentInfo])).optional(), credits: z.number().int().gte(0).nullable().optional(), figures: z.array(fileFigureInfo).optional(), file_id: z.string(), ingestion_error: z.string().nullable().optional(), ingestion_status: ingestionStatus, job_id: z.string().nullable().optional(), kind: z.string().nullable().optional(), page_count: z.number().int().nullable().optional(), path: z.string(), read_coverage: fileReadCoverage.nullable().optional(), size_bytes: z.number().int(), summary: z.string().optional(), tab_count: z.number().int().nullable().optional(), 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 }; /** * file_metadata * * POST /v1/workspaces/{workspace_id}/tools/file-metadata */ export const createApiV1WorkspacesWorkspaceIdToolsFileMetadata = async (args: CreateApiV1WorkspacesWorkspaceIdToolsFileMetadataArgs, options?: CreateApiV1WorkspacesWorkspaceIdToolsFileMetadataOptions) => { 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/file-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(fileMetadataToolResponse, await res.json()) };