Skip to content

计算机使用 Agent

在 Stagehand 中只需一行代码即可接入 Google、Anthropic、OpenAI 和 Microsoft 的 Computer Use API。

文档索引

可在此获取完整文档索引:https://docs.stagehand.dev/llms.txt

在进一步浏览前,可使用该文件发现所有可用页面。

在 Stagehand 中只需一行代码即可接入 Google、Anthropic、OpenAI 和 Microsoft 的 Computer Use API。

你可能已经听说过 Gemini Computer UseClaude Computer UseOpenAI 的 Computer Using Agent

这些都是强大的工具,能够将自然语言转换为计算机上的操作。不过,如果不使用 Stagehand,你通常需要自己编写代码,把这些操作转换成 Playwright 命令。

Stagehand 不仅能够处理 Computer Use 输出的执行,还允许你只用一行代码就在 Google、OpenAI、Anthropic 和 Microsoft 模型之间热切换。关于不同 Computer Use 模型的性能,你可以访问我们的 评测页面 获取更多信息。

如何在 Stagehand 中使用 Computer Use Agent

Section titled “如何在 Stagehand 中使用 Computer Use Agent”

Stagehand 让你只需一行代码就能使用 Computer Use Agent:

弃用通知

cua: true 选项已弃用,并将在未来版本中移除。请改用 mode: "cua"

浏览器配置会因运行环境而不同:

import { Stagehand } from "@browserbasehq/stagehand";
const stagehand = new Stagehand({
env: "BROWSERBASE",
model: "google/gemini-2.5-flash",
browserbaseSessionCreateParams: {
browserSettings: {
blockAds: true,
viewport: {
width: 1288,
height: 711,
},
},
},
});
await stagehand.init();

在 agent 上调用 execute,即可为 agent 分配任务。

await page.goto("https://www.google.com/");
const agent = stagehand.agent({
mode: "cua",
model: {
modelName: "google/gemini-2.5-computer-use-preview-10-2025",
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY
},
systemPrompt: "You are a helpful assistant...",
});
await agent.execute({
instruction: "Go to Hacker News and find the most controversial post from today, then read the top 3 comments and summarize the debate.",
maxSteps: 20,
highlightCursor: true
})

你可以通过 maxSteps 定义 agent 最多可以执行多少步:

await agent.execute({
instructions: "Apply for a library card at the San Francisco Public Library",
maxSteps: 10,
});

Stagehand 支持来自 Google、Anthropic、OpenAI 和 Microsoft 的 Computer Use 模型。你可以在模型页面查看所有受支持模型。

const agent = stagehand.agent({
mode: "cua",
model: "google/gemini-2.5-computer-use-preview-10-2025",
// GOOGLE_GENERATIVE_AI_API_KEY 会被自动加载——请在你的 .env 中设置
});
-
0:000:00