### Installing browser-use-typescript with npm Source: https://github.com/jaskirat05/browser-use-typescript/blob/main/README.md This snippet provides the command to install the `browser-use-typescript` library using npm. It specifies installing the latest version of the package. ```bash npm install browser-use-typescript@latest ``` -------------------------------- ### Initializing Agent for Browser Automation with OpenAI LLM (TypeScript) Source: https://github.com/jaskirat05/browser-use-typescript/blob/main/README.md This TypeScript snippet demonstrates how to initialize an `Agent` from `browser-use-typescript` using a `ChatOpenAI` instance. It sets up the LLM model, temperature, and API key, then creates an agent with a natural language instruction for browser automation. ```typescript import { Agent } from 'browser-use-typescript'; import { ChatOpenAI } from "@langchain/openai"; async function example() { const llm=new ChatOpenAI({ modelName: "gpt-4o-mini", temperature: 0, openAIApiKey:"Your API key" // Or setup it in the env }) const agent=new Agent("use the search function,you will see click on the captcha green button, click it and then try using the search function again tell me which domnain you land on the last time",llm) ``` -------------------------------- ### Defining and Testing Custom Browser Actions (TypeScript) Source: https://github.com/jaskirat05/browser-use-typescript/blob/main/README.md This TypeScript snippet illustrates how to define and test custom actions using the `browser-use-typescript` controller. It initializes an `Agent` and extracts its `controller` and `browserContext`. It includes a helper function `testAction` to execute registered actions with parameters and demonstrates calling a 'search' action. ```typescript //You can also use just the controller to perform actions on the browser, or expose them as tools(Example coming soon) const llm = new ChatOpenAI({ modelName: "gpt-4o-mini", temperature: 0, openAIApiKey: "Your OPENAI key" }); agent = new Agent("hey", llm); controller = agent.controller; // Performs Functions on the browser browserContext = agent.browser_context; //Browser bridge }); // Helper function to create action models and test them async function testAction(actionName: string, params: any) { const registry = controller.registry.getActions()[actionName]; if (!registry) { console.warn(`Action "${actionName}" not found in registry`); return null; } // Ensure we have a valid paramModel (empty object if none is defined) const paramModel = registry.paramModel || z.object({}); const actionModel = new ActionModel({ [actionName]: paramModel }, params); console.log(`=== Testing ${actionName} action ===`); try { if (browserContext) { const result = await controller.act(actionModel, browserContext, agent.settings.page_extraction_llm); console.log(`${actionName} result:`, result); return result; } else { console.error("Browser context is null, cannot call controller.act"); return null; } } catch (error) { console.error(`${actionName} action failed:`, error); return null; } } const searchResult= async () => { const result = await testAction('search', { query: "typescript testing" }); } ``` -------------------------------- ### AI Agent Response Format - JSON Source: https://github.com/jaskirat05/browser-use-typescript/blob/main/agent/system_prompt.md Defines the mandatory JSON structure for the AI agent's responses, including the current state evaluation, memory, next goal, and a sequence of actions to be executed. This format ensures consistent communication and task progression. ```JSON {\"current_state\": {\"evaluation_previous_goal\": \"Success|Failed|Unknown - Analyze the current elements and the image to check if the previous goals/actions are successful like intended by the task. Mention if something unexpected happened. Shortly state why/why not\",\n\"memory\": \"Description of what has been done and what you need to remember. Be very specific. Count here ALWAYS how many times you have done something and how many remain. E.g. 0 out of 10 websites analyzed. Continue with abc and xyz\",\n\"next_goal\": \"What needs to be done with the next immediate action\"}},\n\"action\":[{\"one_action_name\": {{\// action-specific parameter}}}}, \// ... more actions in sequence]} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.