Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(screen-worker): take the page off the Stagehand v3 context

nirholase450e16ce94c
diff --git a/workers/agent-screen-worker/index.js b/workers/agent-screen-worker/index.js
index 3a4a53902..9ccd1479c 100644
--- a/workers/agent-screen-worker/index.js
+++ b/workers/agent-screen-worker/index.js
@@ -19,7 +19,7 @@ function push({ agentId, page, activity, type }) {
}
async function main() {
- console.log(`[agent-screen-worker] starting — agent ${cfg.AGENT_ID}`);
+ console.log(`[agent-screen-worker] starting, agent ${cfg.AGENT_ID}`);
const env = cfg.BROWSERBASE_API_KEY ? 'BROWSERBASE' : 'LOCAL';
console.log(`[agent-screen-worker] browser env: ${env}`);
@@ -28,28 +28,31 @@ async function main() {
env,
apiKey: cfg.BROWSERBASE_API_KEY || undefined,
projectId: cfg.BROWSERBASE_PROJECT_ID || undefined,
- // LLM that drives page.act()/page.extract(). The provider-prefixed model
- // name routes straight to Stagehand's Anthropic client (not gated by its
- // built-in allowlist); the key is passed explicitly so the worker never
- // depends on process-wide env resolution inside Stagehand.
- modelName: cfg.MODEL_NAME,
- modelClientOptions: cfg.ANTHROPIC_API_KEY
- ? { apiKey: cfg.ANTHROPIC_API_KEY }
- : undefined,
+ // LLM that drives act()/extract(). The key is passed explicitly so the
+ // worker never depends on process-wide env resolution inside Stagehand;
+ // without one, the bare model name still lets Stagehand boot for the
+ // navigate-and-screenshot path.
+ model: cfg.ANTHROPIC_API_KEY
+ ? { modelName: cfg.MODEL_NAME, apiKey: cfg.ANTHROPIC_API_KEY }
+ : cfg.MODEL_NAME,
+ ...(env === 'LOCAL' ? { localBrowserLaunchOptions: cfg.LOCAL_LAUNCH_OPTIONS } : {}),
verbose: 1,
});
console.log(`[agent-screen-worker] act/extract model: ${cfg.MODEL_NAME}`);
await stagehand.init();
- const { page, context } = stagehand;
+ // Stagehand v3 has no `.page` accessor: pages hang off the CDP-backed context.
+ // init() opens one tab, so activePage() normally hits; newPage() covers a
+ // context that came up empty.
+ const page = stagehand.context.activePage() || (await stagehand.context.newPage());
// Signal the stream is alive immediately
- await push({ agentId: cfg.AGENT_ID, page, activity: 'Agent starting up…', type: 'activity' });
+ await push({ agentId: cfg.AGENT_ID, page, activity: 'Agent starting up', type: 'activity' });
// Graceful shutdown
const shutdown = async (signal) => {
- console.log(`[agent-screen-worker] ${signal} — shutting down`);
+ console.log(`[agent-screen-worker] ${signal}, shutting down`);
await push({ agentId: cfg.AGENT_ID, page, activity: 'Agent shutting down', type: 'activity' }).catch(() => {});
await stagehand.close().catch(() => {});
process.exit(0);
@@ -57,8 +60,9 @@ async function main() {
process.once('SIGINT', () => shutdown('SIGINT'));
process.once('SIGTERM', () => shutdown('SIGTERM'));
- // Stagehand v3: act/extract live on page; pass page directly
- await runTask({ page, context, cfg, push });
+ // Stagehand v3: act/extract live on the Stagehand instance and take the page
+ // as an option, so the runner needs both handles.
+ await runTask({ stagehand, page, cfg, push });
}
main().catch((err) => {