browser-use-typescript
Version:
A TypeScript-based browser automation framework
177 lines • 5.66 kB
JavaScript
/**
* TypeScript interfaces and classes converted from Python
*/
import { z } from "zod";
import { zodToJsonSchema } from 'zod-to-json-schema';
/**
* Model for a registered action
*/
export class RegisteredAction {
name;
description;
function;
paramModel; // Type of BaseModel
constructor(name, description, func, paramModel) {
this.name = name;
this.description = description;
this.function = func;
this.paramModel = paramModel;
}
/**
* Get a description of the action for the prompt
*/
promptDescription() {
const skipKeys = ['title'];
let s = `${this.description}: \n`;
s += '{' + this.name + ': ';
// Get schema properties and filter out skip keys
const schemaProperties = this.paramModel;
const filteredProperties = Object.entries(schemaProperties).reduce((acc, [k, v]) => {
const subProps = Object.entries(v)
.filter(([subK]) => !skipKeys.includes(subK))
.reduce((subAcc, [subK, subV]) => {
subAcc[subK] = subV;
return subAcc;
}, {});
acc[k] = subProps;
return acc;
}, {});
s += JSON.stringify(filteredProperties);
s += '}';
return s;
}
}
const BaseActionModel = z.object({
id: z.string(),
timestamp: z.date(),
});
export function createDynamicActionModel(fields) {
return BaseActionModel.extend(fields);
}
export class ActionModel {
actions;
params;
descriptions;
constructor(actions, params, descriptions) {
this.actions = actions;
this.params = params;
this.descriptions = descriptions;
}
getIndex() {
if (!this.params) {
return "No params defined";
}
for (const param of Object.values(this.params)) {
if (param !== null && typeof param === 'object' && param !== undefined && param["id"] !== undefined) {
return param["id"];
}
}
return null;
}
setIndex(index) {
if (!this.params) {
return;
}
for (const param of Object.values(this.params)) {
if (param !== null && typeof param === 'object' && param !== undefined && param["id"] !== undefined) {
param["id"] = index;
}
}
}
paramToJson() {
// If no params are set, return empty object
if (!this.params) {
return {};
}
// If no actions defined, just return the raw params
if (!this.actions) {
return this.params;
}
// Get the action name (first key in the actions object)
// Return the parameters directly without nesting them under the action name
// This allows functions using object destructuring to access them correctly
return this.params;
}
toJson() {
if (!this.actions) {
return {};
}
try {
// Import the zodToJsonSchema function
const actionData = {};
for (const [actionName] of Object.entries(this.actions)) {
// Get description from the descriptions field, or empty string if not found
const description = this.descriptions?.[actionName] || "";
// Create anyOf structure with reference to action schema and null option
actionData[actionName] = {
"anyOf": [
{ "$ref": `#/$defs/${actionName}Action` },
{ "type": "null" }
],
"default": null,
"description": description
};
}
return actionData;
}
catch (error) {
console.error("Error converting schema to JSON:", error);
return {};
}
}
getActionDefs() {
if (!this.actions) {
return {};
}
try {
// Import the zodToJsonSchema function
const defs = {};
for (const [actionName, schema] of Object.entries(this.actions)) {
// Convert Zod schema to JSON Schema with no references
const jsonSchema = zodToJsonSchema(schema, {
$refStrategy: 'none',
definitionPath: ""
});
// Create action definition with properties and required fields
defs[`${actionName}Action`] = {
"type": "object",
"title": `${actionName}Action`,
"properties": jsonSchema.properties || {},
"required": jsonSchema.required || []
};
}
// Add NoParamsAction for actions without parameters
defs["NoParamsAction"] = {
"type": "object",
"title": "NoParamsAction",
"properties": {}
};
return defs;
}
catch (error) {
console.error("Error generating action defs:", error);
return {};
}
}
}
/**
* Base model for dynamically created action models
*/
/**
* Model representing the action registry
*/
export class ActionRegistry {
actions = {};
/**
* Get a description of all actions for the prompt
*/
getPromptDescription() {
return Object.values(this.actions)
.map(action => action.promptDescription())
.join('\n');
}
}
/**
* Result of an action execution
*/
//# sourceMappingURL=types.js.map