import {z} from 'zod' export type CreateApiV1WikiSearchBatchArgs = {queries: Array, neighbor_chunks?: (number | null) | undefined, n_documents?: (number | null) | undefined, full_articles?: (boolean | null) | undefined, hybrid_search?: (boolean | null) | undefined, string_guarantee?: (Array | null) | undefined, diversify?: (number | null) | undefined, include_main_section?: (boolean | null) | undefined, has_wikidata?: (boolean | null) | undefined}; export type CreateApiV1WikiSearchBatchOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const cirrusMetadata = z.object({create_timestamp: z.string().nullable().optional(), wikibase_item: z.string().nullable().optional(), version: z.number().int().nullable().optional(), popularity_score: z.number().nullable().optional(), text_bytes: z.number().int().nullable().optional(), heading: z.array(z.string()).nullable().optional(), incoming_links: z.number().int().nullable().optional(), outgoing_links: z.number().int().nullable().optional()}); export const wikiResponseDictItem = z.object({content: z.string(), title: z.string(), url: z.string(), categories: z.array(z.string()), timestamp: z.string(), cirrus_metadata: cirrusMetadata.nullable().optional(), point_id: z.string().nullable().optional(), point_id_main_section: z.string().nullable().optional(), point_id_full_page: z.string().nullable().optional(), has_main_section: z.boolean().nullable().optional()}); export const wikiSearchResponse = z.object({documents: z.array(wikiResponseDictItem)}); export const wikiBatchSearchResponse = z.object({results: z.array(wikiSearchResponse)}); 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 }; /** * Batch search for wiki chunks * * POST /v1/wiki/search/batch */ export const createApiV1WikiSearchBatch = async (args: CreateApiV1WikiSearchBatchArgs, options?: CreateApiV1WikiSearchBatchOptions) => { const {baseUrl = "https://api.asknews.app", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.queries ?? []) query.append("queries", String(item)) if (args.neighbor_chunks != null) query.append("neighbor_chunks", String(args.neighbor_chunks)) if (args.n_documents != null) query.append("n_documents", String(args.n_documents)) if (args.full_articles != null) query.append("full_articles", String(args.full_articles)) if (args.hybrid_search != null) query.append("hybrid_search", String(args.hybrid_search)) for (const item of args.string_guarantee ?? []) query.append("string_guarantee", String(item)) if (args.diversify != null) query.append("diversify", String(args.diversify)) if (args.include_main_section != null) query.append("include_main_section", String(args.include_main_section)) if (args.has_wikidata != null) query.append("has_wikidata", String(args.has_wikidata)) if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/wiki/search/batch${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(wikiBatchSearchResponse, await res.json()) };