import * as z from "zod" import { defineAction } from "../../automation/actions" import { getBrowserbaseClient } from "./lib" import { BROWSERBASE_DATE_TIME_SCHEMA, BROWSERBASE_JSON_OBJECT_SCHEMA, } from "./schemas" const BROWSERBASE_SEARCH_RESPONSE_SCHEMA = z.object({ query: z.string(), requestId: z.string(), results: z .object({ author: z.string().optional(), favicon: z.string().optional(), id: z.string(), image: z.string().optional(), publishedDate: BROWSERBASE_DATE_TIME_SCHEMA.optional(), title: z.string(), url: z.string(), }) .array(), }) const BROWSERBASE_FETCH_RESPONSE_SCHEMA = z.object({ content: z.union([z.string(), BROWSERBASE_JSON_OBJECT_SCHEMA]), contentType: z.string(), encoding: z.string(), headers: z.record(z.string(), z.string()), id: z.string(), statusCode: z.number().int(), }) /** Searches the public web through Browserbase. */ export const searchWebWithBrowserbase = defineAction( "Search the web with Browserbase", ) .describe("Runs a Browserbase web search and returns structured results.") .account("browserbase") .input( z.object({ numResults: z.number().int().min(1).max(25).optional(), query: z.string().trim().min(1).max(200), }), ) .output(BROWSERBASE_SEARCH_RESPONSE_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => BROWSERBASE_SEARCH_RESPONSE_SCHEMA.parse( await getBrowserbaseClient(account.secret).search.web(input), ), ) /** Fetches and optionally extracts one public page through Browserbase. */ export const fetchPageWithBrowserbase = defineAction( "Fetch a page with Browserbase", ) .describe( "Fetches one URL as raw text, Markdown, or schema-constrained JSON.", ) .account("browserbase") .input( z .object({ allowInsecureSsl: z.boolean().optional(), allowRedirects: z.boolean().optional(), format: z.enum(["raw", "json", "markdown"]).optional(), proxies: z.boolean().optional(), schema: BROWSERBASE_JSON_OBJECT_SCHEMA.optional(), url: z.url(), }) .superRefine(({ format, schema }, context) => { if (format === "json" && schema === undefined) { context.addIssue({ code: "custom", message: "Provide schema when format is json.", path: ["schema"], }) } if (format !== "json" && schema !== undefined) { context.addIssue({ code: "custom", message: "Set format to json when providing schema.", path: ["format"], }) } }), ) .output(BROWSERBASE_FETCH_RESPONSE_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => BROWSERBASE_FETCH_RESPONSE_SCHEMA.parse( await getBrowserbaseClient(account.secret).fetchAPI.create(input), ), )