import {z} from 'zod' export type LocateRead = {component?: (string | null) | undefined, line_end?: (number | null) | undefined, line_start?: (number | null) | undefined, pages?: (Array | null) | undefined, path: string}; export type LocatePiece = {component?: (string | null) | undefined, number: number, path: string}; export type LocateRequest = {already_read?: Array | undefined, every?: boolean | undefined, exclude?: Array | undefined, k?: number | undefined, need: string, path_prefix?: (string | null) | undefined, paths?: (Array | null) | undefined, pointer?: boolean | undefined, question?: (string | null) | undefined}; export type CreateApiV1WorkspacesWorkspaceIdToolsLocateArgs = {workspace_id: string, 'Idempotency-Key'?: (string | null) | undefined, body: LocateRequest}; export type CreateApiV1WorkspacesWorkspaceIdToolsLocateOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const locatedPiece = z.object({component: z.string().nullable().optional(), head: z.string(), line_end: z.number().int().nullable().optional(), line_start: z.number().int().nullable().optional(), number: z.number().int(), paged: z.boolean(), path: z.string(), score: z.number()}); export const locatedMember = z.object({path: z.string(), score: z.number()}); export const locatedTableRef = z.object({columns: z.array(z.string()), path: z.string(), relevant_columns: z.array(z.string()), score: z.number(), sql_table: z.string(), tab: z.string().nullable().optional()}); export const locateResponse = z.object({credits: z.number().int().gte(0).nullable().optional(), job_id: z.string().nullable().optional(), pages: z.array(locatedPiece), population: z.array(locatedMember), population_unrated: z.number().int().optional(), tables: z.array(locatedTableRef), text: z.string(), waiver: z.enum(["repeat_of_job", "no_job"]).nullable().optional()}); 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 * * POST /v1/workspaces/{workspace_id}/tools/locate */ export const createApiV1WorkspacesWorkspaceIdToolsLocate = async (args: CreateApiV1WorkspacesWorkspaceIdToolsLocateArgs, options?: CreateApiV1WorkspacesWorkspaceIdToolsLocateOptions) => { const {baseUrl = "https://ndi-api.nace.ai", fetch: fetchFn = fetch, APIKeyHeader} = 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 (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace_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()) };