@ui-tars/operator-browserbase
Version:
Operator Browserbase SDK for UI-TARS
140 lines (138 loc) • 6.68 kB
JavaScript
/**
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
;
var __webpack_require__ = {};
(()=>{
__webpack_require__.d = (exports1, definition)=>{
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
enumerable: true,
get: definition[key]
});
};
})();
(()=>{
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
__webpack_require__.r = (exports1)=>{
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
value: 'Module'
});
Object.defineProperty(exports1, '__esModule', {
value: true
});
};
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
BrowserbaseOperator: ()=>BrowserbaseOperator
});
const core_namespaceObject = require("@ui-tars/sdk/core");
const stagehand_namespaceObject = require("@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 core_namespaceObject.Operator {
async getStagehand() {
if (this.stagehand) return this.stagehand;
const stagehand = new stagehand_namespaceObject.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 ? void 0 : _parsedPrediction_action_inputs.content;
const description = null === (_parsedPrediction_action_inputs1 = parsedPrediction.action_inputs) || void 0 === _parsedPrediction_action_inputs1 ? 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 ? 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: core_namespaceObject.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()
\`\`\``
]
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
value: true
});
//# sourceMappingURL=index.js.map