{"version":3,"file":"agent/prompts/navigator.mjs","sources":["webpack://@agent-infra/browser-use/./src/agent/prompts/navigator.ts"],"sourcesContent":["/**\n * The following code is modified based on\n * https://github.com/nanobrowser/nanobrowser/blob/master/chrome-extension/src/background/agent/prompts/navigator.ts\n *\n * Apache-2.0 License\n * Copyright (c) 2024 alexchenzl\n * https://github.com/nanobrowser/nanobrowser/blob/master/LICENSE\n */\n/* eslint-disable @typescript-eslint/no-unused-vars */\nimport { BasePrompt } from './base';\nimport { type HumanMessage, SystemMessage } from '@langchain/core/messages';\nimport type { AgentContext } from '../types';\n\nexport class NavigatorPrompt extends BasePrompt {\n  private readonly default_action_description =\n    'A placeholder action description';\n\n  constructor(private readonly maxActionsPerStep = 10) {\n    super();\n  }\n\n  importantRules(): string {\n    const text = `\n1. RESPONSE FORMAT: You must ALWAYS respond with valid JSON in this exact format:\n   {\n     \"current_state\": {\n\t\t\"page_summary\": \"Quick detailed summary of new information from the current page which is not yet in the task history memory. Be specific with details which are important for the task. This is not on the meta level, but should be facts. If all the information is already in the task history memory, leave this empty.\",\n\t\t\"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. Ignore the action result. The website is the ground truth. Also mention if something unexpected happened like new suggestions in an input field. 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 actions\"\n     },\n     \"action\": [\n       {\n         \"one_action_name\": {\n           // action-specific parameter\n         }\n       },\n       // ... more actions in sequence\n     ]\n   }\n\n2. ACTIONS: You can specify multiple actions in the list to be executed in sequence. But always specify only one action name per item.\n\n   Common action sequences:\n   - Form filling: [\n       {\"input_text\": {\"desc\": \"Fill title\", \"index\": 1, \"text\": \"example title\"}},\n       {\"input_text\": {\"desc\": \"Fill comment\", \"index\": 2, \"text\": \"example comment\"}},\n       {\"click_element\": {\"desc\": \"Click submit button\", \"index\": 3}}\n     ]\n   - Navigation: [\n       {\"open_tab\": {\"url\": \"https://example.com\"}},\n       {\"go_to_url\": {\"url\": \"https://example.com\"}},\n     ]\n\n\n3. ELEMENT INTERACTION:\n   - Only use indexes that exist in the provided element list\n   - Each element has a unique index number (e.g., \"[33]<button>\")\n   - Elements marked with \"[]Non-interactive text\" are non-interactive (for context only)\n\n4. NAVIGATION & ERROR HANDLING:\n   - If you need to search in Google, use the search_google action. Don't need to input the search query manually, just use the action.\n   - If no suitable elements exist, use other functions to complete the task\n   - If stuck, try alternative approaches - like going back to a previous page, new search, new tab etc.\n   - Handle popups/cookies by accepting or closing them\n   - Use scroll to find elements you are looking for\n   - If you want to research something, open a new tab instead of using the current tab\n   - If captcha pops up, and you cant solve it, either ask for human help or try to continue the task on a different page.\n\n5. TASK COMPLETION:\n   - Use the done action as the last action as soon as the ultimate task is complete\n   - Dont use \"done\" before you are done with everything the user asked you.\n   - If you have to do something repeatedly for example the task says for \"each\", or \"for all\", or \"x times\", count always inside \"memory\" how many times you have done it and how many remain. Don't stop until you have completed like the task asked you. Only call done after the last step.\n   - Don't hallucinate actions\n   - If the ultimate task requires specific information - make sure to include everything in the done function. This is what the user will see. Do not just say you are done, but include the requested information of the task.\n   - Include exact relevant urls if available, but do NOT make up any urls\n\n6. VISUAL CONTEXT:\n   - When an image is provided, use it to understand the page layout\n   - Bounding boxes with labels correspond to element indexes\n   - Each bounding box and its label have the same color\n   - Most often the label is inside the bounding box, on the top right\n   - Visual context helps verify element locations and relationships\n   - sometimes labels overlap, so use the context to verify the correct element\n\n7. Form filling:\n   - If you fill an input field and your action sequence is interrupted, most often a list with suggestions popped up under the field and you need to first select the right element from the suggestion list.\n\n8. ACTION SEQUENCING:\n   - Actions are executed in the order they appear in the list\n   - Each action should logically follow from the previous one\n   - If the page changes after an action, the sequence is interrupted and you get the new state.\n   - If content only disappears the sequence continues.\n   - Only provide the action sequence until you think the page will change.\n   - Try to be efficient, e.g. fill forms at once, or chain actions where nothing changes on the page like saving, extracting, checkboxes...\n   - only use multiple actions if it makes sense.\n\n9. Long tasks:\n- If the task is long keep track of the status in the memory. If the ultimate task requires multiple subinformation, keep track of the status in the memory.\n- If you get stuck,\n\n10. Extraction:\n- When searching for information or conducting research:\n  1. First analyze and extract relevant content from the current visible state\n  2. If the needed information is incomplete:\n     - Use cache_content action to cache the current findings\n     - Scroll down EXACTLY ONE PAGE at a time using scroll_page action\n     - NEVER scroll more than one page at once as this will cause loss of information\n     - Repeat the analyze-cache-scroll cycle until either:\n       * All required information is found, or\n       * Maximum 5 page scrolls have been performed\n  3. Before completing the task:\n     - Combine all cached content with the current state\n     - Verify all required information is collected\n     - Present the complete findings in the done action\n- Important extraction guidelines:\n  - Be thorough and specific when extracting information\n  - Always cache findings before scrolling to avoid losing information\n  - Always verify source information before caching\n  - Scroll down EXACTLY ONE PAGE at a time\n  - Stop after maximum 5 page scrolls\n`;\n    return `${text}   - use maximum ${this.maxActionsPerStep} actions per sequence`;\n  }\n\n  inputFormat(): string {\n    return `\nINPUT STRUCTURE:\n1. Current URL: The webpage you're currently on\n2. Available Tabs: List of open browser tabs\n3. Interactive Elements: List in the format:\n   index[:]<element_type>element_text</element_type>\n   - index: Numeric identifier for interaction\n   - element_type: HTML element type (button, input, etc.)\n   - element_text: Visible text or element description\n\nExample:\n[33]<button>Submit Form</button>\n[] Non-interactive text\n\n\nNotes:\n- Only elements with numeric indexes inside [] are interactive\n- [] elements provide context but cannot be interacted with\n`;\n  }\n\n  getSystemMessage(): SystemMessage {\n    /**\n     * Get the system prompt for the agent.\n     *\n     * @returns SystemMessage containing the formatted system prompt\n     */\n    const AGENT_PROMPT = `You are a precise browser automation agent that interacts with websites through structured commands. Your role is to:\n1. Analyze the provided webpage elements and structure\n2. Use the given information to accomplish the ultimate task\n3. Respond with valid JSON containing your next action sequence and state assessment\n4. If the webpage is asking for login credentials, never try to fill it by yourself. Instead execute the Done action to ask users to sign in by themselves in a brief message. Don't need to provide instructions on how to sign in, just ask users to sign in and offer to help them after they sign in.\n\n${this.inputFormat()}\n\n${this.importantRules()}\n\nFunctions:\n${this.default_action_description}\n\nRemember: Your responses must be valid JSON matching the specified format. Each action in the sequence must be valid.`;\n\n    return new SystemMessage(AGENT_PROMPT);\n  }\n\n  async getUserMessage(context: AgentContext): Promise<HumanMessage> {\n    return await this.buildBrowserStateUserMessage(context);\n  }\n}\n"],"names":["NavigatorPrompt","BasePrompt","text","AGENT_PROMPT","SystemMessage","context","maxActionsPerStep"],"mappings":";;;;;;AAQoD;;;;;;;;;;AAK7C,MAAMA,wBAAwBC;IAQnC,iBAAyB;QACvB,MAAMC,OAAO,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmGlB,CAAC;QACG,OAAO,GAAGA,KAAK,iBAAiB,EAAE,IAAI,CAAC,iBAAiB,CAAC,qBAAqB,CAAC;IACjF;IAEA,cAAsB;QACpB,OAAO,CAAC;;;;;;;;;;;;;;;;;;AAkBZ,CAAC;IACC;IAEA,mBAAkC;QAMhC,MAAMC,eAAe,CAAC;;;;;;AAM1B,EAAE,IAAI,CAAC,WAAW,GAAG;;AAErB,EAAE,IAAI,CAAC,cAAc,GAAG;;;AAGxB,EAAE,IAAI,CAAC,0BAA0B,CAAC;;qHAEmF,CAAC;QAElH,OAAO,IAAIC,cAAcD;IAC3B;IAEA,MAAM,eAAeE,OAAqB,EAAyB;QACjE,OAAO,MAAM,IAAI,CAAC,4BAA4B,CAACA;IACjD;IA5JA,YAA6BC,oBAAoB,EAAE,CAAE;QACnD,KAAK,yDAJP,uBAAiB,8BAAjB,cAG6BA,iBAAiB,GAAjBA,mBAAAA,IAAAA,CAHZ,0BAA0B,GACzC;IAIF;AA2JF"}