@ui-tars/operator-browserbase
Version:
Operator Browserbase SDK for UI-TARS
1 lines • 6.99 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["webpack://@ui-tars/operator-browserbase/./src/index.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport {\n Operator,\n type ScreenshotOutput,\n type ExecuteParams,\n type ExecuteOutput,\n StatusEnum,\n} from '@ui-tars/sdk/core';\nimport { Stagehand, ConstructorParams } from '@browserbasehq/stagehand';\n\nexport class BrowserbaseOperator extends Operator {\n private stagehand: Stagehand | null = null;\n\n constructor(private options: ConstructorParams) {\n super();\n this.options = options;\n console.log('this.options', this.options);\n }\n\n static MANUAL = {\n ACTION_SPACES: [\n \"GOTO(url='') # determine the best URL to start from, then navigate to the target page\",\n \"ACT(description='') # next step `action`(natural language) in current page to description for instruction\",\n \"EXTRACT(description='') # Extract information from the page\",\n \"OBSERVE(description='') # Observe and analyze the webpage to get the next action\",\n 'CLOSE() # Finish the task, no more actions needed',\n 'NAVBACK() # Go back to the previous page',\n ],\n EXAMPLES: [\n `\\`\\`\\`\nUser Instruction: Who is the top GitHub contributor to Stagehand by Browserbase?\n1. Thought: The best starting point to find the top GitHub contributor to the Stagehand project by Browserbase is the project's GitHub repository itself. This will provide direct access to the contributors' list, where you can see who has made the most contributions.\\nAction: GOTO(url='https://github.com/browserbase/stagehand')\n2. Thought: The 'Insights' tab on a GitHub repository page provides detailed information about the project's contributors, including the number of contributions each has made. By accessing this tab, we can find the top contributor.\\nAction: ACT(description='Click on the 'Insights' tab.')\n3. The 'Contributors' section under the 'Insights' tab will show a list of contributors along with the number of contributions each has made. This is the most direct way to identify the top contributor to the project.\\nAction: ACT(description='Click on the 'Contributors' link in the left sidebar.')\n4. The screenshot shows the contributors to the Stagehand project, with 'kamath' listed as the top contributor with 69 commits. This information is directly visible in the 'Contributors' section under the 'Insights' tab.\\nAction: CLOSE()\n\n\\`\\`\\``,\n ],\n };\n\n private async getStagehand() {\n if (this.stagehand) {\n return this.stagehand;\n }\n const stagehand = new Stagehand(this.options);\n await stagehand.init();\n this.stagehand = stagehand;\n\n return this.stagehand;\n }\n\n public async screenshot(): Promise<ScreenshotOutput> {\n const stagehand = await this.getStagehand();\n const page = stagehand.page;\n\n const cdpSession = await page.context().newCDPSession(page);\n const { data: base64 } = await cdpSession.send('Page.captureScreenshot');\n\n return {\n base64,\n scaleFactor: 1,\n };\n }\n\n async execute(params: ExecuteParams): Promise<ExecuteOutput> {\n const { parsedPrediction } = params;\n console.log('params', params);\n\n const stagehand = await this.getStagehand();\n const page = stagehand.page;\n\n // @ts-ignore\n const instruction = parsedPrediction.action_inputs?.content;\n // @ts-ignore\n const description = parsedPrediction.action_inputs?.description;\n\n switch (parsedPrediction.action_type) {\n case 'GOTO':\n // @ts-ignore\n const url = parsedPrediction.action_inputs?.url;\n if (url) {\n await page.goto(url, {\n waitUntil: 'commit',\n timeout: 60000,\n });\n }\n break;\n\n case 'ACT':\n // @ts-ignore\n if (description) {\n await page.act(description!);\n }\n break;\n\n case 'EXTRACT': {\n const { extraction } = await page.extract(description!);\n console.log('extraction', extraction);\n // TODO: 需要处理返回的 ObserveResult\n // return extraction;\n break;\n }\n\n case 'OBSERVE':\n await page.observe({\n instruction: description,\n useAccessibilityTree: true,\n });\n break;\n\n case 'WAIT':\n await new Promise((resolve) =>\n setTimeout(resolve, Number(instruction)),\n );\n break;\n\n case 'NAVBACK':\n await page.goBack();\n break;\n\n case 'CLOSE':\n await stagehand.close();\n return { status: StatusEnum.END };\n }\n }\n}\n"],"names":["BrowserbaseOperator","Operator","stagehand","Stagehand","page","cdpSession","base64","params","_parsedPrediction_action_inputs","_parsedPrediction_action_inputs1","parsedPrediction","console","instruction","description","_parsedPrediction_action_inputs2","url","extraction","Promise","resolve","setTimeout","Number","StatusEnum","options"],"mappings":";;;;;;AAGC;;;;;;;;;;AAUM,MAAMA,4BAA4BC,uDAAAA,QAAQA;IA8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