import {z} from 'zod' export type ReadFileRequest = {path: string, pages?: (Array | null) | undefined, component?: (string | null) | undefined, max_chars?: number | undefined, line_start?: (number | null) | undefined, line_end?: (number | null) | undefined}; export type CreateApiV1WorkspacesIdToolsReadFileArgs = {id: string, 'Idempotency-Key'?: string | undefined, body: ReadFileRequest}; export type CreateApiV1WorkspacesIdToolsReadFileOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const readFileTable = z.object({first_line: z.number().int(), last_line: z.number().int(), title: z.string().nullable(), header: z.string(), rows: z.number().int().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const readFileResponse = z.object({path: z.string(), file_id: z.string(), content: z.string(), pages_returned: z.array(z.number().int()), truncated: z.boolean(), components_returned: z.array(z.string()), components_omitted: z.array(z.string()), sql_tables: z.array(z.string()).optional(), line_start: z.number().int().nullable().optional(), line_end: z.number().int().nullable().optional(), line_count: z.number().int().nullable().optional(), table: readFileTable.nullable().optional(), job_id: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); 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 }; /** * Read a file * * POST /v1/workspaces/{id}/tools/read-file */ export const createApiV1WorkspacesIdToolsReadFile = async (args: CreateApiV1WorkspacesIdToolsReadFileArgs, options?: CreateApiV1WorkspacesIdToolsReadFileOptions) => { const {baseUrl = "https://console.nace.ai", fetch: fetchFn = fetch, bearerAuth} = 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.id))}/tools/read-file${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(readFileResponse, await res.json()) };