import {z} from 'zod' export type WebSearchLocation = {country: string, state?: string | undefined, city?: string | undefined}; export type WebSearchFiletype = 'pdf' | 'doc' | 'docx' | 'xls' | 'xlsx' | 'ppt' | 'pptx' | 'html'; export type WebSearchFilters = {exactPhrase?: boolean | undefined, semanticPhrase?: boolean | undefined, excludeTerms?: Array | undefined, boostTerms?: Array | undefined, filetype?: WebSearchFiletype | undefined, site?: string | undefined, excludeSite?: string | undefined, intitle?: string | undefined, inurl?: string | undefined}; export type WebSearchParams = {query: string, page?: number | undefined, maxAgeSeconds?: number | undefined, location?: WebSearchLocation | undefined, filters?: WebSearchFilters | undefined}; export type CreateApiX402WebSearchArgs = {'PAYMENT-SIGNATURE'?: string | undefined, body: WebSearchParams}; export type CreateApiX402WebSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const webSearchStatus = z.enum(["completed", "failed", "pending", "running", "stopped"]); export const webSearchResultItem = z.object({title: z.string(), url: z.string(), description: z.string()}); export const webSearchResponseData = z.object({query: z.string(), results: z.array(webSearchResultItem)}); export const webSearchResponse = z.object({jobId: z.string(), status: webSearchStatus, error: z.string().nullable().optional(), data: webSearchResponseData.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 }; /** * Search the web with X402 payment * * POST /x402/web/search */ export const createApiX402WebSearch = async (args: CreateApiX402WebSearchArgs, options?: CreateApiX402WebSearchOptions) => { const {baseUrl = "https://api.hyperbrowser.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['PAYMENT-SIGNATURE'] != null) headers.set("PAYMENT-SIGNATURE", String(args['PAYMENT-SIGNATURE'])) const url = `${baseUrl.replace(/\/+$/, '')}/x402/web/search${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(webSearchResponse, await res.json()) };