import {z} from 'zod' export type GetApiV1ChatWebsearchArgs = {queries: Array, lookback?: (number | null) | undefined, start_datetime?: (string | null) | undefined, end_datetime?: (string | null) | undefined, engine?: ('v1' | 'v1.5') | undefined, domains?: (Array | null) | undefined, strict?: boolean | undefined, offset?: (number | string | null) | undefined}; export type GetApiV1ChatWebsearchOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webSearchResult = z.object({title: z.string(), url: z.string().min(1).max(2083), source: z.string(), published: z.string(), key_points: z.array(z.string()), raw_text: z.string().optional(), as_string_key: z.string().nullable().optional()}); export const webSearchResponse = z.object({as_string: z.string(), as_dicts: z.array(webSearchResult), offset: z.union([z.number().int(), z.string()]).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 }; /** * Run a live websearch. * * GET /v1/chat/websearch */ export const getApiV1ChatWebsearch = async (args: GetApiV1ChatWebsearchArgs, options?: GetApiV1ChatWebsearchOptions) => { 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.lookback != null) query.append("lookback", String(args.lookback)) if (args.start_datetime != null) query.append("start_datetime", String(args.start_datetime)) if (args.end_datetime != null) query.append("end_datetime", String(args.end_datetime)) if (args.engine != null) query.append("engine", String(args.engine)) for (const item of args.domains ?? []) query.append("domains", String(item)) if (args.strict != null) query.append("strict", String(args.strict)) if (args.offset != null) query.append("offset", String(args.offset)) if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/chat/websearch${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(webSearchResponse, await res.json()) };