import {z} from 'zod' export type LocatePiece = {path: string, number: number, component?: (string | null) | undefined}; export type LocateRead = {path: string, pages?: (Array | null) | undefined, line_start?: (number | null) | undefined, line_end?: (number | null) | undefined, component?: (string | null) | undefined}; export type LocateRequest = {need: string, question?: string | undefined, k?: number | undefined, every?: boolean | undefined, path_prefix?: string | undefined, paths?: Array | undefined, exclude?: Array | undefined, already_read?: Array | undefined, pointer?: boolean | undefined}; export type CreateApiV1WorkspacesIdToolsLocateArgs = {id: string, 'Idempotency-Key'?: string | undefined, body: LocateRequest}; export type CreateApiV1WorkspacesIdToolsLocateOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const locatedPiece = z.object({path: z.string(), component: z.string().nullable().optional(), paged: z.boolean(), number: z.number().int(), line_start: z.number().int().nullable().optional(), line_end: z.number().int().nullable().optional(), score: z.number(), head: z.string()}).and(z.record(z.string(), z.unknown())); export const locatedTable = z.object({sql_table: z.string(), path: z.string(), tab: z.string().nullable().optional(), score: z.number(), columns: z.array(z.string()), relevant_columns: z.array(z.string())}).and(z.record(z.string(), z.unknown())); export const locatedMember = z.object({path: z.string(), score: z.number()}).and(z.record(z.string(), z.unknown())); export const locateResponse = z.object({text: z.string(), pages: z.array(locatedPiece), tables: z.array(locatedTable), population: z.array(locatedMember), population_unrated: z.number().int().gte(0).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 }; /** * Locate information * * POST /v1/workspaces/{id}/tools/locate */ export const createApiV1WorkspacesIdToolsLocate = async (args: CreateApiV1WorkspacesIdToolsLocateArgs, options?: CreateApiV1WorkspacesIdToolsLocateOptions) => { 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/locate${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(locateResponse, await res.json()) };