fix(cli): Functions CLI updates for `init` and `publish`

Merged
AK
2mo ago
+332-27

why

  • Before Browserbase had secrets support, functions init would install a playwright template. But now we support secrets so we can update the starter template to showcase Stagehand.
  • There were some rough edges around the functions publish command related to pnpm 11. Added a few updates so that the functions CLI works out of the box.

what changed

test plan

It works if nothing in this path errors:

  1. [in stagehand repo] pnpm build && cd packages/cli
  2. alias browse-dev="node $HOME/[your dev path]/stagehand/packages/cli/bin/run.js" to set a local version of the browse cli called browse-dev
  3. cd [your dev path, _not_ in stagehand repo] && browse-dev functions init branch-test && cd branch-test
  4. browse-dev cloud extensions upload node_modules/@browserbasehq/stagehand/dist/assets/stagehand-extension.zip and then replace your-extension-id in index.ts
  5. browse-dev cloud secrets create BROWSERBASE_API_KEY --env BROWSERBASE_API_KEY to store your secret API key for use in the deployed function
  6. browse-dev functions dev index.ts
  7. (in a new terminal) curl -X POST http://127.0.0.1:14113/v1/functions/my-function/invoke -H 'Content-Type: application/json' --data '{}' to make sure it works locally
  8. npm_config_min_release_age=0 browse-dev functions publish index.ts
  9. browse-dev functions secrets attach <functionId> <secretId>
  10. browse-dev functions invoke <functionId> to make sure you got the same result as (7) above
  11. Success!

Summary by cubic

Swaps the Playwright starter in functions init for a Stagehand one, and fixes functions init and functions publish on pnpm 11 and private npm registries.

  • Local dev now sends an empty secrets object to invocations, matching production, so the starter's context.secrets.BROWSERBASE_API_KEY lookup needs no null check and falls back to process.env.BROWSERBASE_API_KEY.
  • functions init installs @browserbasehq/stagehand instead of playwright-core, pins zod to Stagehand's version so schemas pass type checks, and writes a Stagehand starter that extracts Hacker News top stories through the Model Gateway.
  • The starter connects with browserbase.connect into Stagehand.create and expects you to upload the Stagehand extension and paste its ID into index.ts.
  • functions init writes pnpm-workspace.yaml so pnpm 11 allows esbuild's build script, and its next steps use browse commands.
  • functions publish builds package-lock.json from the uploaded files (so local file: dependencies resolve), omits registry-resolved URLs for private registries, checks for an existing lockfile, and prints npm output when generation fails.