import {z} from 'zod' export type GetApiApiV1DocumentProjectProjectPkDocumentsIdSimilarArgs = {project_pk: string, id: string, text_max_length?: number | undefined, jq_filters?: {[key: string]: string} | undefined}; export type GetApiApiV1DocumentProjectProjectPkDocumentsIdSimilarOptions = {baseUrl: string, AuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const projectDocumentSimilarity = z.object({document_a_name: z.string(), document_a_id: z.string().optional(), document_b_name: z.string(), document_b_id: z.string().optional(), document_b_text: z.string(), similarity: z.string(), run_id: z.string().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 }; /** * Re-use ProjectDocumentSimilarityListAPIView to get similarities * * GET /api/v1/document/project/{project_pk}/documents/{id}/similar/ */ export const getApiApiV1DocumentProjectProjectPkDocumentsIdSimilar = async (args: GetApiApiV1DocumentProjectProjectPkDocumentsIdSimilarArgs, options: GetApiApiV1DocumentProjectProjectPkDocumentsIdSimilarOptions) => { const {baseUrl, fetch: fetchFn = fetch, AuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.text_max_length != null) query.append("text_max_length", String(args.text_max_length)) if (args.jq_filters != null) query.append("jq_filters", JSON.stringify(args.jq_filters)) if (AuthToken !== undefined) headers.set("Authorization", AuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/document/project/${encodeURIComponent(String(args.project_pk))}/documents/${encodeURIComponent(String(args.id))}/similar/${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(projectDocumentSimilarity, await res.json()) };