import {z} from 'zod' export type GetApiKnowledgeKnowledgeIdSourcesSourceIdFilesArgs = {knowledge_id: string, source_id: string, prefix?: (string | null) | undefined, limit?: number | undefined, page?: number | undefined, delimiter?: string | undefined, db_id?: (string | null) | undefined}; export type GetApiKnowledgeKnowledgeIdSourcesSourceIdFilesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const sourceFolderSchema = z.object({prefix: z.string(), name: z.string(), is_empty: z.boolean().optional()}); export const sourceFileSchema = z.object({key: z.string(), name: z.string(), size: z.number().int().nullable().optional(), last_modified: z.string().nullable().optional(), content_type: z.string().nullable().optional()}); export const paginationInfo = z.object({page: z.number().int().gte(0).optional(), limit: z.number().int().gte(1).optional(), total_pages: z.number().int().gte(0).optional(), total_count: z.number().int().gte(0).optional(), search_time_ms: z.number().gte(0).optional()}); export const sourceFilesResponseSchema = z.object({source_id: z.string(), source_name: z.string(), prefix: z.string().nullable().optional(), folders: z.array(sourceFolderSchema).optional(), files: z.array(sourceFileSchema).optional(), meta: paginationInfo}); 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 }; /** * List Files in Source * * GET /knowledge/{knowledge_id}/sources/{source_id}/files */ export const getApiKnowledgeKnowledgeIdSourcesSourceIdFiles = async (args: GetApiKnowledgeKnowledgeIdSourcesSourceIdFilesArgs, options: GetApiKnowledgeKnowledgeIdSourcesSourceIdFilesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.prefix != null) query.append("prefix", String(args.prefix)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.delimiter != null) query.append("delimiter", String(args.delimiter)) if (args.db_id != null) query.append("db_id", String(args.db_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/knowledge/${encodeURIComponent(String(args.knowledge_id))}/sources/${encodeURIComponent(String(args.source_id))}/files${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(sourceFilesResponseSchema, await res.json()) };