@ui-tars/operator-browserbase
Version:
Operator Browserbase SDK for UI-TARS
108 lines (106 loc) • 5.26 kB
JavaScript
/**
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
import { Operator, StatusEnum } from "@ui-tars/sdk/core";
import { Stagehand } from "@browserbasehq/stagehand";
function _define_property(obj, key, value) {
if (key in obj) Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
else obj[key] = value;
return obj;
}
class BrowserbaseOperator extends Operator {
async getStagehand() {
if (this.stagehand) return this.stagehand;
const stagehand = new Stagehand(this.options);
await stagehand.init();
this.stagehand = stagehand;
return this.stagehand;
}
async screenshot() {
const stagehand = await this.getStagehand();
const page = stagehand.page;
const cdpSession = await page.context().newCDPSession(page);
const { data: base64 } = await cdpSession.send('Page.captureScreenshot');
return {
base64,
scaleFactor: 1
};
}
async execute(params) {
var _parsedPrediction_action_inputs, _parsedPrediction_action_inputs1;
const { parsedPrediction } = params;
console.log('params', params);
const stagehand = await this.getStagehand();
const page = stagehand.page;
const instruction = null == (_parsedPrediction_action_inputs = parsedPrediction.action_inputs) ? void 0 : _parsedPrediction_action_inputs.content;
const description = null == (_parsedPrediction_action_inputs1 = parsedPrediction.action_inputs) ? void 0 : _parsedPrediction_action_inputs1.description;
switch(parsedPrediction.action_type){
case 'GOTO':
var _parsedPrediction_action_inputs2;
const url = null == (_parsedPrediction_action_inputs2 = parsedPrediction.action_inputs) ? void 0 : _parsedPrediction_action_inputs2.url;
if (url) await page.goto(url, {
waitUntil: 'commit',
timeout: 60000
});
break;
case 'ACT':
if (description) await page.act(description);
break;
case 'EXTRACT':
{
const { extraction } = await page.extract(description);
console.log('extraction', extraction);
break;
}
case 'OBSERVE':
await page.observe({
instruction: description,
useAccessibilityTree: true
});
break;
case 'WAIT':
await new Promise((resolve)=>setTimeout(resolve, Number(instruction)));
break;
case 'NAVBACK':
await page.goBack();
break;
case 'CLOSE':
await stagehand.close();
return {
status: StatusEnum.END
};
}
}
constructor(options){
super(), _define_property(this, "options", void 0), _define_property(this, "stagehand", void 0), this.options = options, this.stagehand = null;
this.options = options;
console.log('this.options', this.options);
}
}
_define_property(BrowserbaseOperator, "MANUAL", {
ACTION_SPACES: [
"GOTO(url='') # determine the best URL to start from, then navigate to the target page",
"ACT(description='') # next step `action`(natural language) in current page to description for instruction",
"EXTRACT(description='') # Extract information from the page",
"OBSERVE(description='') # Observe and analyze the webpage to get the next action",
'CLOSE() # Finish the task, no more actions needed',
'NAVBACK() # Go back to the previous page'
],
EXAMPLES: [
`\`\`\`
User Instruction: Who is the top GitHub contributor to Stagehand by Browserbase?
1. 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')
2. 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.')
3. 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.')
4. 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()
\`\`\``
]
});
export { BrowserbaseOperator };
//# sourceMappingURL=index.mjs.map