import {z} from 'zod' export type GetApiFilesystemSearchPathArgs = {path: string, maxResults?: number | undefined, patterns?: string | undefined, excludeDirs?: string | undefined, excludeHidden?: boolean | undefined}; export type GetApiFilesystemSearchPathOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fuzzySearchMatch = z.object({path: z.string(), score: z.number().int(), type: z.string()}); export const fuzzySearchResponse = z.object({matches: z.array(fuzzySearchMatch), total: z.number().int()}); 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 }; /** * Fuzzy search for files and directories * * GET /filesystem-search/{path} * * Also accepts auth: ApiKeyAuth */ export const getApiFilesystemSearchPath = async (args: GetApiFilesystemSearchPathArgs, options?: GetApiFilesystemSearchPathOptions) => { const {baseUrl = "https://api.blaxel.ai/v0", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.maxResults != null) query.append("maxResults", String(args.maxResults)) if (args.patterns != null) query.append("patterns", String(args.patterns)) if (args.excludeDirs != null) query.append("excludeDirs", String(args.excludeDirs)) if (args.excludeHidden != null) query.append("excludeHidden", String(args.excludeHidden)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/filesystem-search/${encodeURIComponent(String(args.path))}${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(fuzzySearchResponse, await res.json()) };