import {z} from 'zod' export type GetApiAskArgs = {query: string, site?: string | undefined, mode?: string | undefined, streaming?: boolean | undefined, limit?: number | undefined, query_id?: string | undefined}; export type GetApiAskOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAskResponse = z.union([z.object({_meta: z.object({version: z.literal("0.55"), request_id: z.string().optional(), response_type: z.literal("answer"), response_format: z.literal("conversational_search"), release: z.object({id: z.string()})}).and(z.record(z.string(), z.unknown())), results: z.array(z.object({"@type": z.literal("WebPage"), url: z.string(), name: z.string(), site: z.string(), score: z.number(), description: z.string(), grounding: z.object({evidence: z.array(z.object({id: z.string(), documentId: z.string(), url: z.string(), text: z.string(), start: z.number().int().gte(0).lte(9007199254740991), end: z.number().int().gte(0).lte(9007199254740991), view: z.enum(["origin", "published"]), context: z.object({profile: z.literal("adjacent-passages-v1"), anchorStart: z.number().int().gte(0).lte(9007199254740991), anchorEnd: z.number().int().gte(0).lte(9007199254740991), passageIds: z.array(z.string())}).optional()}))})}))}), z.object({_meta: z.object({version: z.literal("0.55"), request_id: z.string().optional(), response_type: z.literal("failure")}).and(z.record(z.string(), z.unknown())), error: z.object({code: z.string(), message: z.string(), status: z.number().int().gte(-9007199254740991).lte(9007199254740991).optional(), retry_after: z.number().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 }; /** * Retrieve published website evidence * * GET /ask */ export const getApiAsk = async (args: GetApiAskArgs, options?: GetApiAskOptions) => { const {baseUrl = "https://appsflyer.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.site != null) query.append("site", String(args.site)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.streaming != null) query.append("streaming", String(args.streaming)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.query_id != null) query.append("query_id", String(args.query_id)) const url = `${baseUrl.replace(/\/+$/, '')}/ask${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(getApiAskResponse, await res.json()) };