import {z} from 'zod' export type UUIDSchema = string; export type GetApiInspectImageUuidGrepArgs = {image_uuid: UUIDSchema, pattern: string, path?: string | undefined, glob?: string | undefined, max_count?: number | undefined, max_total?: number | undefined, case?: ('sensitive' | 'insensitive' | 'smart') | undefined}; export type GetApiInspectImageUuidGrepOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const grepSubmatch = z.object({text: z.string(), start: z.number().int(), end: z.number().int()}); export const grepMatch = z.object({path: z.string(), line_number: z.number().int(), absolute_offset: z.number().int(), line_text: z.string(), line_bytes: z.number().int(), submatches: z.array(grepSubmatch)}); export const grepResult = z.object({path: z.string(), patterns: z.array(z.string()), matches: z.array(grepMatch), truncated: z.boolean()}); 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 }; /** * Search file contents in image * * GET /inspect/{image_uuid}/grep */ export const getApiInspectImageUuidGrep = async (args: GetApiInspectImageUuidGrepArgs, options?: GetApiInspectImageUuidGrepOptions) => { const {baseUrl = "https://api.tokenfactory.nebius.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pattern != null) query.append("pattern", String(args.pattern)) if (args.path != null) query.append("path", String(args.path)) if (args.glob != null) query.append("glob", String(args.glob)) if (args.max_count != null) query.append("max_count", String(args.max_count)) if (args.max_total != null) query.append("max_total", String(args.max_total)) if (args.case != null) query.append("case", String(args.case)) const url = `${baseUrl.replace(/\/+$/, '')}/inspect/${encodeURIComponent(String(args.image_uuid))}/grep${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(grepResult, await res.json()) };