import * as z from "zod" import { defineAction } from "../../automation/actions" import { requestBrowserbase } from "./lib" import { BROWSERBASE_FUNCTION_BUILD_SCHEMA, BROWSERBASE_FUNCTION_BUILD_STATUS_SCHEMA, BROWSERBASE_FUNCTION_SCHEMA, BROWSERBASE_FUNCTION_VERSION_SCHEMA, BROWSERBASE_INVOCATION_SCHEMA, BROWSERBASE_INVOCATION_STATUS_SCHEMA, BROWSERBASE_JSON_OBJECT_SCHEMA, BROWSERBASE_LOG_PAGE_SCHEMA, BROWSERBASE_OFFSET_PAGE_INPUT_SCHEMA, BROWSERBASE_SESSION_CREATE_SCHEMA, BROWSERBASE_UUID_SCHEMA, } from "./schemas" const BROWSERBASE_FUNCTION_PAGE_SCHEMA = z.object({ data: BROWSERBASE_FUNCTION_SCHEMA.array(), total: z.number().int().nonnegative(), }) const BROWSERBASE_FUNCTION_BUILD_PAGE_SCHEMA = z.object({ data: BROWSERBASE_FUNCTION_BUILD_SCHEMA.array(), total: z.number().int().nonnegative(), }) const BROWSERBASE_FUNCTION_VERSION_PAGE_SCHEMA = z.object({ results: BROWSERBASE_FUNCTION_VERSION_SCHEMA.array(), total: z.number().int().nonnegative(), }) const BROWSERBASE_INVOCATION_PAGE_SCHEMA = z.object({ results: BROWSERBASE_INVOCATION_SCHEMA.array(), total: z.number().int().nonnegative(), }) const BROWSERBASE_FUNCTION_SESSION_SCHEMA = BROWSERBASE_SESSION_CREATE_SCHEMA.omit({ keepAlive: true, projectId: true, region: true, }).extend({ timeout: z.number().int().min(60).max(900).optional() }) /** Lists deployed Browserbase Functions. */ export const listBrowserbaseFunctions = defineAction( "List Browserbase functions", ) .describe( "Lists one offset page of functions available to the connected project.", ) .account("browserbase") .input(BROWSERBASE_OFFSET_PAGE_INPUT_SCHEMA) .output(BROWSERBASE_FUNCTION_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => requestBrowserbase(account.secret, "/v1/functions", { query: input, responseSchema: BROWSERBASE_FUNCTION_PAGE_SCHEMA, }), ) /** Gets one deployed Browserbase Function. */ export const getBrowserbaseFunction = defineAction("Get Browserbase function") .describe("Gets one deployed function's identity and timestamps.") .account("browserbase") .input(z.object({ functionId: BROWSERBASE_UUID_SCHEMA })) .output(BROWSERBASE_FUNCTION_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => requestBrowserbase( account.secret, `/v1/functions/${encodeURIComponent(input.functionId)}`, { responseSchema: BROWSERBASE_FUNCTION_SCHEMA }, ), ) /** Starts an invocation of a deployed Browserbase Function. */ export const invokeBrowserbaseFunction = defineAction( "Invoke Browserbase function", ) .describe( "Starts an asynchronous function invocation with optional session overrides.", ) .account("browserbase") .input( z.object({ functionId: BROWSERBASE_UUID_SCHEMA, params: BROWSERBASE_JSON_OBJECT_SCHEMA.optional(), sessionCreateParams: BROWSERBASE_FUNCTION_SESSION_SCHEMA.optional(), }), ) .output(BROWSERBASE_INVOCATION_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input: { functionId, ...body } }) => requestBrowserbase( account.secret, `/v1/functions/${encodeURIComponent(functionId)}/invoke`, { body, method: "POST", responseSchema: BROWSERBASE_INVOCATION_SCHEMA }, ), ) /** Lists versions of one Browserbase Function. */ export const listBrowserbaseFunctionVersions = defineAction( "List Browserbase function versions", ) .describe("Lists one offset page of deployed versions for a function.") .account("browserbase") .input( BROWSERBASE_OFFSET_PAGE_INPUT_SCHEMA.extend({ functionId: BROWSERBASE_UUID_SCHEMA, }), ) .output(BROWSERBASE_FUNCTION_VERSION_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input: { functionId, ...query } }) => requestBrowserbase( account.secret, `/v1/functions/${encodeURIComponent(functionId)}/versions`, { query, responseSchema: BROWSERBASE_FUNCTION_VERSION_PAGE_SCHEMA }, ), ) /** Gets one Browserbase Function version. */ export const getBrowserbaseFunctionVersion = defineAction( "Get Browserbase function version", ) .describe("Gets one immutable function version and its session defaults.") .account("browserbase") .input(z.object({ versionId: BROWSERBASE_UUID_SCHEMA })) .output(BROWSERBASE_FUNCTION_VERSION_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => requestBrowserbase( account.secret, `/v1/functions/versions/${encodeURIComponent(input.versionId)}`, { responseSchema: BROWSERBASE_FUNCTION_VERSION_SCHEMA }, ), ) /** Lists Browserbase Function builds. */ export const listBrowserbaseFunctionBuilds = defineAction( "List Browserbase function builds", ) .describe( "Lists one offset page of function builds with an optional status filter.", ) .account("browserbase") .input( BROWSERBASE_OFFSET_PAGE_INPUT_SCHEMA.extend({ status: BROWSERBASE_FUNCTION_BUILD_STATUS_SCHEMA.optional(), }), ) .output(BROWSERBASE_FUNCTION_BUILD_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => requestBrowserbase(account.secret, "/v1/functions/builds", { query: input, responseSchema: BROWSERBASE_FUNCTION_BUILD_PAGE_SCHEMA, }), ) /** Gets one Browserbase Function build. */ export const getBrowserbaseFunctionBuild = defineAction( "Get Browserbase function build", ) .describe( "Gets build inputs, lifecycle state, output functions, and failure cause.", ) .account("browserbase") .input(z.object({ buildId: BROWSERBASE_UUID_SCHEMA })) .output(BROWSERBASE_FUNCTION_BUILD_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => requestBrowserbase( account.secret, `/v1/functions/builds/${encodeURIComponent(input.buildId)}`, { responseSchema: BROWSERBASE_FUNCTION_BUILD_SCHEMA }, ), ) /** Gets logs for one Browserbase Function build. */ export const getBrowserbaseFunctionBuildLogs = defineAction( "Get Browserbase function build logs", ) .describe("Gets timestamped log messages emitted while a function build ran.") .account("browserbase") .input(z.object({ buildId: BROWSERBASE_UUID_SCHEMA })) .output(BROWSERBASE_LOG_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => requestBrowserbase( account.secret, `/v1/functions/builds/${encodeURIComponent(input.buildId)}/logs`, { responseSchema: BROWSERBASE_LOG_PAGE_SCHEMA }, ), ) /** Gets one Browserbase Function invocation. */ export const getBrowserbaseInvocation = defineAction( "Get Browserbase invocation", ) .describe( "Gets invocation lifecycle, inputs, result, session, and failure cause.", ) .account("browserbase") .input(z.object({ invocationId: BROWSERBASE_UUID_SCHEMA })) .output(BROWSERBASE_INVOCATION_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => requestBrowserbase( account.secret, `/v1/functions/invocations/${encodeURIComponent(input.invocationId)}`, { responseSchema: BROWSERBASE_INVOCATION_SCHEMA }, ), ) /** Gets logs for one Browserbase Function invocation. */ export const getBrowserbaseInvocationLogs = defineAction( "Get Browserbase invocation logs", ) .describe("Gets timestamped logs emitted by one function invocation.") .account("browserbase") .input(z.object({ invocationId: BROWSERBASE_UUID_SCHEMA })) .output(BROWSERBASE_LOG_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => requestBrowserbase( account.secret, `/v1/functions/invocations/${encodeURIComponent(input.invocationId)}/logs`, { responseSchema: BROWSERBASE_LOG_PAGE_SCHEMA }, ), ) /** Lists invocations of one Browserbase Function version. */ export const listBrowserbaseFunctionVersionInvocations = defineAction( "List Browserbase function version invocations", ) .describe( "Lists one offset page of invocations for an immutable function version.", ) .account("browserbase") .input( BROWSERBASE_OFFSET_PAGE_INPUT_SCHEMA.extend({ status: BROWSERBASE_INVOCATION_STATUS_SCHEMA.optional(), versionId: BROWSERBASE_UUID_SCHEMA, }), ) .output(BROWSERBASE_INVOCATION_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input: { versionId, ...query } }) => requestBrowserbase( account.secret, `/v1/functions/versions/${encodeURIComponent(versionId)}/invocations`, { query, responseSchema: BROWSERBASE_INVOCATION_PAGE_SCHEMA }, ), )