page-agent 0.0.23 β 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -23
- package/dist/esm/PageAgent.d.ts +1 -1
- package/dist/esm/page-agent.js +1 -1
- package/dist/esm/page-agent.js.map +1 -1
- package/dist/umd/page-agent.js +9 -9
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -28,7 +28,22 @@ The GUI Agent Living in Your Webpage. Control web interfaces with natural langua
|
|
|
28
28
|
|
|
29
29
|
## π Quick Start
|
|
30
30
|
|
|
31
|
-
###
|
|
31
|
+
### Quick Try (Demo CDN)
|
|
32
|
+
|
|
33
|
+
Fastest way to try PageAgent:
|
|
34
|
+
|
|
35
|
+
```html
|
|
36
|
+
<script src="https://cdn.jsdelivr.net/npm/@page-agent/cdn/dist/page-agent.demo.js" crossorigin="true"></script>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
> β οΈ **For technical evaluation only.** Demo model has rate limits and usage restrictions. Use NPM for production.
|
|
40
|
+
|
|
41
|
+
| Location | URL |
|
|
42
|
+
| -------- | --------------------------------------------------------------------------------------- |
|
|
43
|
+
| Global | https://cdn.jsdelivr.net/npm/@page-agent/cdn/dist/page-agent.demo.js |
|
|
44
|
+
| China | https://registry.npmmirror.com/@page-agent/cdn/latest/files/dist/page-agent.demo.js |
|
|
45
|
+
|
|
46
|
+
### NPM Installation (Recommended)
|
|
32
47
|
|
|
33
48
|
```bash
|
|
34
49
|
npm install page-agent
|
|
@@ -37,40 +52,37 @@ npm install page-agent
|
|
|
37
52
|
```javascript
|
|
38
53
|
import { PageAgent } from 'page-agent'
|
|
39
54
|
|
|
40
|
-
// test server
|
|
41
|
-
// @note: rate limit. prompt limit. Origin limit. May change anytime. Use your own llm!
|
|
42
|
-
// @note Using official DeepSeek-chat(3.2). Go to DeepSeek website for privacy policy.
|
|
43
|
-
const DEMO_MODEL = 'PAGE-AGENT-FREE-TESTING-RANDOM'
|
|
44
|
-
const DEMO_BASE_URL = 'https://hwcxiuzfylggtcktqgij.supabase.co/functions/v1/llm-testing-proxy'
|
|
45
|
-
const DEMO_API_KEY = 'PAGE-AGENT-FREE-TESTING-RANDOM'
|
|
46
|
-
|
|
47
55
|
const agent = new PageAgent({
|
|
48
|
-
model:
|
|
49
|
-
baseURL:
|
|
50
|
-
apiKey:
|
|
56
|
+
model: 'deepseek-chat',
|
|
57
|
+
baseURL: 'https://api.deepseek.com',
|
|
58
|
+
apiKey: 'YOUR_API_KEY',
|
|
51
59
|
language: 'en-US',
|
|
52
60
|
})
|
|
53
61
|
|
|
54
62
|
await agent.execute('Click the login button')
|
|
55
63
|
```
|
|
56
64
|
|
|
57
|
-
### CDN
|
|
58
|
-
|
|
59
|
-
Fastest way to try PageAgent is to include it via CDN. Demo model will be used by default.
|
|
65
|
+
### CDN Build
|
|
60
66
|
|
|
61
|
-
|
|
62
|
-
| -------- | ----------------------------------------------------------------------------- |
|
|
63
|
-
| Global | https://cdn.jsdelivr.net/npm/page-agent@latest/dist/umd/page-agent.js |
|
|
64
|
-
| China | https://registry.npmmirror.com/page-agent/latest/files/dist/umd/page-agent.js |
|
|
67
|
+
For environments where NPM is not available. Usage is identical to NPM:
|
|
65
68
|
|
|
66
69
|
```html
|
|
67
|
-
<script
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
<script src="https://cdn.jsdelivr.net/npm/@page-agent/cdn/dist/page-agent.js" crossorigin="true"></script>
|
|
71
|
+
<script>
|
|
72
|
+
const agent = new PageAgent({
|
|
73
|
+
model: 'deepseek-chat',
|
|
74
|
+
baseURL: 'https://api.deepseek.com',
|
|
75
|
+
apiKey: 'YOUR_API_KEY',
|
|
76
|
+
})
|
|
77
|
+
await agent.execute('Click the login button')
|
|
78
|
+
</script>
|
|
72
79
|
```
|
|
73
80
|
|
|
81
|
+
| Location | URL |
|
|
82
|
+
| -------- | ----------------------------------------------------------------------------------- |
|
|
83
|
+
| Global | https://cdn.jsdelivr.net/npm/@page-agent/cdn/dist/page-agent.js |
|
|
84
|
+
| China | https://registry.npmmirror.com/@page-agent/cdn/latest/files/dist/page-agent.js |
|
|
85
|
+
|
|
74
86
|
## ποΈ Structure
|
|
75
87
|
|
|
76
88
|
PageAgent adopts a simplified monorepo structure:
|
package/dist/esm/PageAgent.d.ts
CHANGED
|
@@ -168,7 +168,7 @@ export declare class PageAgent extends EventTarget {
|
|
|
168
168
|
pageController: PageController;
|
|
169
169
|
/** History records */
|
|
170
170
|
history: AgentHistory[];
|
|
171
|
-
constructor(config
|
|
171
|
+
constructor(config: PageAgentConfig);
|
|
172
172
|
execute(task: string): Promise<ExecutionResult>;
|
|
173
173
|
dispose(reason?: string): void;
|
|
174
174
|
}
|
package/dist/esm/page-agent.js
CHANGED
|
@@ -305,7 +305,7 @@ function assert(condition, message, silent) {
|
|
|
305
305
|
}
|
|
306
306
|
__name(assert, "assert");
|
|
307
307
|
const _PageAgent = class _PageAgent extends EventTarget {
|
|
308
|
-
constructor(config
|
|
308
|
+
constructor(config) {
|
|
309
309
|
super();
|
|
310
310
|
__privateAdd(this, _PageAgent_instances);
|
|
311
311
|
__publicField(this, "config");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page-agent.js","sources":["../../src/config/constants.ts","../../src/prompts/system_prompt.md?raw","../../src/utils/autoFixer.ts","../../src/utils/index.ts","../../src/tools/index.ts","../../src/utils/assert.ts","../../src/PageAgent.ts"],"sourcesContent":["// Agent-specific constants (LLM constants moved to @page-agent/llms)\nexport const MAX_STEPS = 20\n","export default \"You are an AI agent designed to operate in an iterative loop to automate browser tasks. Your ultimate goal is accomplishing the task provided in <user_request>.\\n\\n<intro>\\nYou excel at following tasks:\\n1. Navigating complex websites and extracting precise information\\n2. Automating form submissions and interactive web actions\\n3. Gathering and saving information \\n4. Operate effectively in an agent loop\\n5. Efficiently performing diverse web tasks\\n</intro>\\n\\n<language_settings>\\n- Default working language: **δΈζ**\\n- Use the language that user is using. Return in user's language.\\n</language_settings>\\n\\n<input>\\nAt every step, your input will consist of: \\n1. <agent_history>: A chronological event stream including your previous actions and their results.\\n2. <agent_state>: Current <user_request> and <step_info>.\\n3. <browser_state>: Current URL, interactive elements indexed for actions, and visible page content.\\n</input>\\n\\n<agent_history>\\nAgent history will be given as a list of step information as follows:\\n\\n<step_{step_number}>:\\nEvaluation of Previous Step: Assessment of last action\\nMemory: Your memory of this step\\nNext Goal: Your goal for this step\\nAction Results: Your actions and their results\\n</step_{step_number}>\\n\\nand system messages wrapped in <sys> tag.\\n</agent_history>\\n\\n<user_request>\\nUSER REQUEST: This is your ultimate objective and always remains visible.\\n- This has the highest priority. Make the user happy.\\n- If the user request is very specific - then carefully follow each step and dont skip or hallucinate steps.\\n- If the task is open ended you can plan yourself how to get it done.\\n</user_request>\\n\\n<browser_state>\\n1. Browser State will be given as:\\n\\nCurrent URL: URL of the page you are currently viewing.\\nInteractive Elements: All interactive elements will be provided in format as [index]<type>text</type> where\\n- index: Numeric identifier for interaction\\n- type: HTML element type (button, input, etc.)\\n- text: Element description\\n\\nExamples:\\n[33]<div>User form</div>\\n\\\\t*[35]<button aria-label='Submit form'>Submit</button>\\n\\nNote that:\\n- Only elements with numeric indexes in [] are interactive\\n- (stacked) indentation (with \\\\t) is important and means that the element is a (html) child of the element above (with a lower index)\\n- Elements tagged with `*[` are the new clickable elements that appeared on the website since the last step - if url has not changed.\\n- Pure text elements without [] are not interactive.\\n</browser_state>\\n\\n<browser_rules>\\nStrictly follow these rules while using the browser and navigating the web:\\n- Only interact with elements that have a numeric [index] assigned.\\n- Only use indexes that are explicitly provided.\\n- If the page changes after, for example, an input text action, analyze if you need to interact with new elements, e.g. selecting the right option from the list.\\n- By default, only elements in the visible viewport are listed. Use scrolling actions if you suspect relevant content is offscreen which you need to interact with. Scroll ONLY if there are more pixels below or above the page.\\n- You can scroll by a specific number of pages using the num_pages parameter (e.g., 0.5 for half page, 2.0 for two pages).\\n- All the elements that are scrollable are marked with `data-scrollable` attribute. Including the scrollable distance in every directions. You can scroll *the element* in case some area are overflowed.\\n- If a captcha appears, tell user you can not solve captcha. finished the task and ask user to solve it.\\n- If expected elements are missing, try scrolling, or navigating back.\\n- If the page is not fully loaded, use the `wait` action.\\n- Do not repeat one action for more than 3 times unless some conditions changed.\\n- If you fill an input field and your action sequence is interrupted, most often something changed e.g. suggestions popped up under the field.\\n- If the <user_request> includes specific page information such as product type, rating, price, location, etc., try to apply filters to be more efficient.\\n- The <user_request> is the ultimate goal. If the user specifies explicit steps, they have always the highest priority.\\n- If you input_text into a field, you might need to press enter, click the search button, or select from dropdown for completion.\\n- Don't login into a page if you don't have to. Don't login if you don't have the credentials. \\n- There are 2 types of tasks always first think which type of request you are dealing with:\\n1. Very specific step by step instructions:\\n- Follow them as very precise and don't skip steps. Try to complete everything as requested.\\n2. Open ended tasks. Plan yourself, be creative in achieving them.\\n- If you get stuck e.g. with logins or captcha in open-ended tasks you can re-evaluate the task and try alternative ways, e.g. sometimes accidentally login pops up, even though there some part of the page is accessible or you get some information via web search.\\n</browser_rules>\\n\\n<capability>\\n- You can only handle single page app. Do not jump out of current page.\\n- Do not click on link if it will open in a new page (etc. <a target=\\\"_blank\\\">)\\n- It is ok to fail the task.\\n\\t- User can be wrong. If the request of user is not achievable, inappropriate or you do not have enough information or tools to achieve it. Tell user to make a better request.\\n\\t- Webpage can be broken. All webpages or apps have bugs. Some bug will make it hard for your job. It's encouraged to tell user the problem of current page. Your feedbacks (including failing) are valuable for user.\\n\\t- Trying to hard can be harmful. Repeating some action back and forth or pushing for a complex procedure with little knowledge can cause unwanted result and harmful side-effects. User would rather you to complete the task with a fail.\\n- If you are not clear about the request or steps. `ask_user` to clarify it.\\n- If you do not have knowledge for the current webpage or task. You must require user to give specific instructions and detailed steps.\\n</capability>\\n\\n<task_completion_rules>\\nYou must call the `done` action in one of three cases:\\n- When you have fully completed the USER REQUEST.\\n- When you reach the final allowed step (`max_steps`), even if the task is incomplete.\\n- When you feel stuck or unable to solve user request. Or user request is not clear or contains inappropriate content.\\n- If it is ABSOLUTELY IMPOSSIBLE to continue.\\n\\nThe `done` action is your opportunity to terminate and share your findings with the user.\\n- Set `success` to `true` only if the full USER REQUEST has been completed with no missing components.\\n- If any part of the request is missing, incomplete, or uncertain, set `success` to `false`.\\n- You can use the `text` field of the `done` action to communicate your findings and to provide a coherent reply to the user and fulfill the USER REQUEST.\\n- You are ONLY ALLOWED to call `done` as a single action. Don't call it together with other actions.\\n- If the user asks for specified format, such as \\\"return JSON with following structure\\\", \\\"return a list of format...\\\", MAKE sure to use the right format in your answer.\\n- If the user asks for a structured output, your `done` action's schema may be modified. Take this schema into account when solving the task!\\n</task_completion_rules>\\n\\n<reasoning_rules>\\nExhibit the following reasoning patterns to successfully achieve the <user_request>:\\n\\n- Reason about <agent_history> to track progress and context toward <user_request>.\\n- Analyze the most recent \\\"Next Goal\\\" and \\\"Action Result\\\" in <agent_history> and clearly state what you previously tried to achieve.\\n- Analyze all relevant items in <agent_history> and <browser_state> to understand your state.\\n- Explicitly judge success/failure/uncertainty of the last action. Never assume an action succeeded just because it appears to be executed in your last step in <agent_history>. If the expected change is missing, mark the last action as failed (or uncertain) and plan a recovery.\\n- Analyze whether you are stuck, e.g. when you repeat the same actions multiple times without any progress. Then consider alternative approaches e.g. scrolling for more context or ask user for help.\\n- `ask_user` for help if you have any difficulty. Users want to be kept in the loop.\\n- If you see information relevant to <user_request>, plan saving the information to memory.\\n- Always reason about the <user_request>. Make sure to carefully analyze the specific steps and information required. E.g. specific filters, specific form fields, specific information to search. Make sure to always compare the current trajectory with the user request and think carefully if thats how the user requested it.\\n</reasoning_rules>\\n\\n<examples>\\nHere are examples of good output patterns. Use them as reference but never copy them directly.\\n\\n<evaluation_examples>\\n- Positive Examples:\\n\\\"evaluation_previous_goal\\\": \\\"Successfully navigated to the product page and found the target information. Verdict: Success\\\"\\n\\\"evaluation_previous_goal\\\": \\\"Clicked the login button and user authentication form appeared. Verdict: Success\\\"\\n</evaluation_examples>\\n\\n<memory_examples>\\n\\\"memory\\\": \\\"Found many pending reports that need to be analyzed in the main page. Successfully processed the first 2 reports on quarterly sales data and moving on to inventory analysis and customer feedback reports.\\\"\\n</memory_examples>\\n\\n<next_goal_examples>\\n\\\"next_goal\\\": \\\"Click on the 'Add to Cart' button to proceed with the purchase flow.\\\"\\n\\\"next_goal\\\": \\\"Extract details from the first item on the page.\\\"\\n</next_goal_examples>\\n</examples>\\n\\n<output>\\nYou must ALWAYS respond with a valid JSON in this exact format:\\n\\n{\\n \\\"evaluation_previous_goal\\\": \\\"Concise one-sentence analysis of your last action. Clearly state success, failure, or uncertain.\\\",\\n \\\"memory\\\": \\\"1-3 concise sentences of specific memory of this step and overall progress. You should put here everything that will help you track progress in future steps. Like counting pages visited, items found, etc.\\\",\\n \\\"next_goal\\\": \\\"State the next immediate goal and action to achieve it, in one clear sentence.\\\"\\n \\\"action\\\":{\\\"one_action_name\\\": {// action-specific parameter}}\\n}\\n</output>\\n\"","import chalk from 'chalk'\n\n/**\n * Normalize LLM response and fix common format issues.\n *\n * Handles:\n * - No tool_calls but JSON in message.content (fallback)\n * - Model returns action name as tool call instead of AgentOutput\n * - Arguments wrapped as double JSON string\n * - Nested function call format\n * - Missing action field (fallback to wait)\n * - etc.\n */\nexport function normalizeResponse(response: any): any {\n\tlet resolvedArguments = null as any\n\n\tconst choice = (response as { choices?: Choice[] }).choices?.[0]\n\tif (!choice) throw new Error('No choices in response')\n\n\tconst message = choice.message\n\tif (!message) throw new Error('No message in choice')\n\n\tconst toolCall = message.tool_calls?.[0]\n\n\t// fix level and location of arguments\n\n\tif (toolCall?.function?.arguments) {\n\t\tresolvedArguments = safeJsonParse(toolCall.function.arguments)\n\n\t\t// case: sometimes the model only returns the action level\n\t\tif (toolCall.function.name && toolCall.function.name !== 'AgentOutput') {\n\t\t\tconsole.log(chalk.yellow(`[normalizeResponse] #1: fixing tool_call`))\n\t\t\tresolvedArguments = { action: safeJsonParse(resolvedArguments) }\n\t\t}\n\t} else {\n\t\t// case: sometimes the model returns json in content instead of tool_calls\n\t\tif (message.content) {\n\t\t\tconst content = message.content.trim()\n\t\t\tconst jsonInContent = retrieveJsonFromString(content)\n\t\t\tif (jsonInContent) {\n\t\t\t\tresolvedArguments = safeJsonParse(jsonInContent)\n\n\t\t\t\t// case: sometimes the content json includes upper level wrapper\n\t\t\t\tif (resolvedArguments?.name === 'AgentOutput') {\n\t\t\t\t\tconsole.log(chalk.yellow(`[normalizeResponse] #2: fixing tool_call`))\n\t\t\t\t\tresolvedArguments = safeJsonParse(resolvedArguments.arguments)\n\t\t\t\t}\n\n\t\t\t\t// case: sometimes even 2-levels of wrapping\n\t\t\t\tif (resolvedArguments?.type === 'function') {\n\t\t\t\t\tconsole.log(chalk.yellow(`[normalizeResponse] #3: fixing tool_call`))\n\t\t\t\t\tresolvedArguments = safeJsonParse(resolvedArguments.function.arguments)\n\t\t\t\t}\n\n\t\t\t\t// case: and sometimes action level only\n\t\t\t\t// todo: needs better detection logic\n\t\t\t\tif (\n\t\t\t\t\t!resolvedArguments?.action &&\n\t\t\t\t\t!resolvedArguments?.evaluation_previous_goal &&\n\t\t\t\t\t!resolvedArguments?.memory &&\n\t\t\t\t\t!resolvedArguments?.next_goal &&\n\t\t\t\t\t!resolvedArguments?.thinking\n\t\t\t\t) {\n\t\t\t\t\tconsole.log(chalk.yellow(`[normalizeResponse] #4: fixing tool_call`))\n\t\t\t\t\tresolvedArguments = { action: safeJsonParse(resolvedArguments) }\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthrow new Error('No tool_call and the message content does not contain valid JSON')\n\t\t\t}\n\t\t} else {\n\t\t\tthrow new Error('No tool_call nor message content is present')\n\t\t}\n\t}\n\n\t// fix double stringified arguments\n\tresolvedArguments = safeJsonParse(resolvedArguments)\n\tif (resolvedArguments.action) {\n\t\tresolvedArguments.action = safeJsonParse(resolvedArguments.action)\n\t}\n\n\t// fix incomplete formats\n\tif (!resolvedArguments.action) {\n\t\tconsole.log(chalk.yellow(`[normalizeResponse] #5: fixing tool_call`))\n\t\tresolvedArguments.action = { name: 'wait', input: { seconds: 1 } }\n\t}\n\n\t// pack back to standard format\n\treturn {\n\t\t...response,\n\t\tchoices: [\n\t\t\t{\n\t\t\t\t...choice,\n\t\t\t\tmessage: {\n\t\t\t\t\t...message,\n\t\t\t\t\ttool_calls: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t...(toolCall || {}),\n\t\t\t\t\t\t\tfunction: {\n\t\t\t\t\t\t\t\t...(toolCall?.function || {}),\n\t\t\t\t\t\t\t\tname: 'AgentOutput',\n\t\t\t\t\t\t\t\targuments: JSON.stringify(resolvedArguments),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t],\n\t}\n}\n\n/**\n * Safely parse JSON, return original input if not json.\n */\nfunction safeJsonParse(input: any): any {\n\tif (typeof input === 'string') {\n\t\ttry {\n\t\t\treturn JSON.parse(input.trim())\n\t\t} catch {\n\t\t\treturn input\n\t\t}\n\t}\n\treturn input\n}\n\n/**\n * Extract and parse JSON from a string.\n * - Treat content between the first `{` and the last `}` as JSON.\n * - Try to parse that content as JSON and return the parsed value (object/array/primitive) if successful, otherwise return null.\n */\nfunction retrieveJsonFromString(str: string): any {\n\ttry {\n\t\tconst json = /({[\\s\\S]*})/.exec(str) ?? []\n\t\tif (json.length === 0) {\n\t\t\treturn null\n\t\t}\n\t\treturn JSON.parse(json[0]!)\n\t} catch {\n\t\treturn null\n\t}\n}\n\ninterface Choice {\n\tmessage?: {\n\t\trole?: 'assistant'\n\t\tcontent?: string\n\t\ttool_calls?: {\n\t\t\tid?: string\n\t\t\ttype?: 'function'\n\t\t\tfunction?: {\n\t\t\t\tname?: string\n\t\t\t\targuments?: string\n\t\t\t}\n\t\t}[]\n\t}\n\tindex?: 0\n\tfinish_reason?: 'tool_calls'\n}\n","export { normalizeResponse } from './autoFixer'\n\n/**\n * Wait until condition becomes true\n * @returns Returns when condition becomes true, throws otherwise\n * @param timeout Timeout in milliseconds, default 0 means no timeout, throws error on timeout\n */\nexport async function waitUntil(check: () => boolean, timeout = 60 * 60_1000): Promise<boolean> {\n\tif (check()) return true\n\n\treturn new Promise((resolve, reject) => {\n\t\tconst start = Date.now()\n\t\tconst interval = setInterval(() => {\n\t\t\tif (check()) {\n\t\t\t\tclearInterval(interval)\n\t\t\t\tresolve(true)\n\t\t\t} else if (Date.now() - start > timeout) {\n\t\t\t\tclearInterval(interval)\n\t\t\t\treject(new Error('Timeout waiting for condition to become true'))\n\t\t\t}\n\t\t}, 100)\n\t})\n}\n\nexport async function waitFor(seconds: number): Promise<void> {\n\tawait new Promise((resolve) => setTimeout(resolve, seconds * 1000))\n}\n\n//\n\nexport function truncate(text: string, maxLength: number): string {\n\tif (text.length > maxLength) {\n\t\treturn text.substring(0, maxLength) + '...'\n\t}\n\treturn text\n}\n\n//\n\nexport function trimLines(text: string): string {\n\treturn text\n\t\t.split('\\n')\n\t\t.map((line) => line.trim())\n\t\t.join('\\n')\n}\n\n//\n\nexport function randomID(existingIDs?: string[]): string {\n\tlet id = Math.random().toString(36).substring(2, 11)\n\n\tif (!existingIDs) {\n\t\treturn id\n\t}\n\n\tconst MAX_TRY = 1000\n\tlet tryCount = 0\n\n\twhile (existingIDs.includes(id)) {\n\t\tid = Math.random().toString(36).substring(2, 11)\n\t\ttryCount++\n\t\tif (tryCount > MAX_TRY) {\n\t\t\tthrow new Error('randomID: too many try')\n\t\t}\n\t}\n\n\treturn id\n}\n\n//\n\nif (!window.__PAGE_AGENT_IDS__) {\n\twindow.__PAGE_AGENT_IDS__ = []\n}\n\nconst ids = window.__PAGE_AGENT_IDS__\n\n/**\n * Generate a random ID.\n * @note Unique within this window.\n */\nexport function uid() {\n\tconst id = randomID(ids)\n\tids.push(id)\n\treturn id\n}\n","/**\n * Internal tools for PageAgent.\n * @note Adapted from browser-use\n */\nimport zod, { type z } from 'zod'\n\nimport type { PageAgent } from '../PageAgent'\nimport { waitFor } from '../utils'\n\n/**\n * Internal tool definition that has access to PageAgent `this` context\n */\nexport interface PageAgentTool<TParams = any> {\n\t// name: string\n\tdescription: string\n\tinputSchema: z.ZodType<TParams>\n\texecute: (this: PageAgent, args: TParams) => Promise<string>\n}\n\nexport function tool<TParams>(options: PageAgentTool<TParams>): PageAgentTool<TParams> {\n\treturn options\n}\n\n/**\n * Internal tools for PageAgent.\n * Note: Using any to allow different parameter types for each tool\n */\nexport const tools = new Map<string, PageAgentTool>()\n\ntools.set(\n\t'done',\n\ttool({\n\t\tdescription:\n\t\t\t'Complete task - provide a summary of results for the user. Set success=True if task completed successfully, false otherwise. Text should be your response to the user summarizing results.',\n\t\tinputSchema: zod.object({\n\t\t\ttext: zod.string(),\n\t\t\tsuccess: zod.boolean().default(true),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\t// @note main loop will handle this one\n\t\t\t// this.onDone(input.text, input.success)\n\t\t\treturn Promise.resolve('Task completed')\n\t\t},\n\t})\n)\n\ntools.set(\n\t'wait',\n\ttool({\n\t\tdescription:\n\t\t\t'Wait for x seconds. default 1s (max 10 seconds, min 1 second). This can be used to wait until the page or data is fully loaded.',\n\t\tinputSchema: zod.object({\n\t\t\tseconds: zod.number().min(1).max(10).default(1),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst lastTimeUpdate = await this.pageController.getLastUpdateTime()\n\t\t\tconst actualWaitTime = Math.max(0, input.seconds - (Date.now() - lastTimeUpdate) / 1000)\n\t\t\tconsole.log(`actualWaitTime: ${actualWaitTime} seconds`)\n\t\t\tawait waitFor(actualWaitTime)\n\t\t\treturn `β
Waited for ${input.seconds} seconds.`\n\t\t},\n\t})\n)\n\ntools.set(\n\t'ask_user',\n\ttool({\n\t\tdescription:\n\t\t\t'Ask the user a question and wait for their answer. Use this if you need more information or clarification.',\n\t\tinputSchema: zod.object({\n\t\t\tquestion: zod.string(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst answer = await this.panel.askUser(input.question)\n\t\t\treturn `β
Received user answer: ${answer}`\n\t\t},\n\t})\n)\n\ntools.set(\n\t'click_element_by_index',\n\ttool({\n\t\tdescription: 'Click element by index',\n\t\tinputSchema: zod.object({\n\t\t\tindex: zod.int().min(0),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.clickElement(input.index)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\ntools.set(\n\t'input_text',\n\ttool({\n\t\tdescription: 'Click and input text into a input interactive element',\n\t\tinputSchema: zod.object({\n\t\t\tindex: zod.int().min(0),\n\t\t\ttext: zod.string(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.inputText(input.index, input.text)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\ntools.set(\n\t'select_dropdown_option',\n\ttool({\n\t\tdescription:\n\t\t\t'Select dropdown option for interactive element index by the text of the option you want to select',\n\t\tinputSchema: zod.object({\n\t\t\tindex: zod.int().min(0),\n\t\t\ttext: zod.string(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.selectOption(input.index, input.text)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\n/**\n * @note Reference from browser-use\n */\ntools.set(\n\t'scroll',\n\ttool({\n\t\tdescription:\n\t\t\t'Scroll the page by specified number of pages (set down=True to scroll down, down=False to scroll up, num_pages=number of pages to scroll like 0.5 for half page, 1.0 for one page, etc.). Optional index parameter to scroll within a specific element or its scroll container (works well for dropdowns and custom UI components). Optional pixels parameter to scroll by a specific number of pixels instead of pages.',\n\t\tinputSchema: zod.object({\n\t\t\tdown: zod.boolean().default(true),\n\t\t\tnum_pages: zod.number().min(0).max(10).optional().default(0.1),\n\t\t\tpixels: zod.number().int().min(0).optional(),\n\t\t\tindex: zod.number().int().min(0).optional(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.scroll({\n\t\t\t\t...input,\n\t\t\t\tnumPages: input.num_pages,\n\t\t\t})\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\ntools.set(\n\t'scroll_horizontally',\n\ttool({\n\t\tdescription:\n\t\t\t'Scroll the page or element horizontally (set right=True to scroll right, right=False to scroll left, pixels=number of pixels to scroll). Optional index parameter to scroll within a specific element or its scroll container (works well for wide tables).',\n\t\tinputSchema: zod.object({\n\t\t\tright: zod.boolean().default(true),\n\t\t\tpixels: zod.number().int().min(0),\n\t\t\tindex: zod.number().int().min(0).optional(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.scrollHorizontally(input)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\ntools.set(\n\t'execute_javascript',\n\ttool({\n\t\tdescription:\n\t\t\t'Execute JavaScript code on the current page. Supports async/await syntax. Use with caution!',\n\t\tinputSchema: zod.object({\n\t\t\tscript: zod.string(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.executeJavascript(input.script)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\n// @todo get_dropdown_options\n// @todo select_dropdown_option\n// @todo send_keys\n// @todo upload_file\n// @todo go_back\n// @todo extract_structured_data\n","import chalk from 'chalk'\n\n/**\n * Simple assertion function that throws an error if the condition is falsy\n * @param condition - The condition to assert\n * @param message - Optional error message\n * @throws Error if condition is falsy\n */\nexport function assert(condition: unknown, message?: string, silent?: boolean): asserts condition {\n\tif (!condition) {\n\t\tconst errorMessage = message ?? 'Assertion failed'\n\n\t\tif (!silent) console.error(chalk.red(`β assert: ${errorMessage}`))\n\n\t\tthrow new Error(errorMessage)\n\t}\n}\n","/**\n * Copyright (C) 2025 Alibaba Group Holding Limited\n * All rights reserved.\n */\nimport { LLM, type Tool } from '@page-agent/llms'\nimport { PageController } from '@page-agent/page-controller'\nimport { Panel } from '@page-agent/ui'\nimport chalk from 'chalk'\nimport zod from 'zod'\n\nimport type { PageAgentConfig } from './config'\nimport { MAX_STEPS } from './config/constants'\nimport SYSTEM_PROMPT from './prompts/system_prompt.md?raw'\nimport { tools } from './tools'\nimport { normalizeResponse, trimLines, uid, waitUntil } from './utils'\nimport { assert } from './utils/assert'\n\n/**\n * Agent brain state - the reflection-before-action model\n *\n * Every tool call must first reflect on:\n * - evaluation_previous_goal: How well did the previous action achieve its goal?\n * - memory: Key information to remember for future steps\n * - next_goal: What should be accomplished in the next action?\n */\nexport interface AgentReflection {\n\tevaluation_previous_goal: string\n\tmemory: string\n\tnext_goal: string\n}\n\n/**\n * MacroTool input structure\n *\n * This is the core abstraction that enforces the \"reflection-before-action\" mental model.\n * Before executing any action, the LLM must output its reasoning state.\n */\nexport interface MacroToolInput extends Partial<AgentReflection> {\n\taction: Record<string, any>\n}\n\n/**\n * MacroTool output structure\n */\nexport interface MacroToolResult {\n\tinput: MacroToolInput\n\toutput: string\n}\n\nexport type { PageAgentConfig }\nexport { tool, type PageAgentTool } from './tools'\n\nexport interface AgentHistory {\n\tbrain: Partial<AgentReflection>\n\taction: {\n\t\tname: string\n\t\tinput: any\n\t\toutput: string\n\t}\n\tusage: {\n\t\tpromptTokens: number\n\t\tcompletionTokens: number\n\t\ttotalTokens: number\n\t\tcachedTokens?: number\n\t\treasoningTokens?: number\n\t}\n}\n\nexport interface ExecutionResult {\n\tsuccess: boolean\n\tdata: string\n\thistory: AgentHistory[]\n}\n\nexport class PageAgent extends EventTarget {\n\tconfig: PageAgentConfig\n\tid = uid()\n\tpanel: Panel\n\ttools: typeof tools\n\tpaused = false\n\tdisposed = false\n\ttask = ''\n\ttaskId = ''\n\n\t#llm: LLM\n\t#totalWaitTime = 0\n\t#abortController = new AbortController()\n\t#llmRetryListener: ((e: Event) => void) | null = null\n\t#llmErrorListener: ((e: Event) => void) | null = null\n\t#beforeUnloadListener: ((e: Event) => void) | null = null\n\n\t/** PageController for DOM operations */\n\tpageController: PageController\n\n\t/** History records */\n\thistory: AgentHistory[] = []\n\n\tconstructor(config: PageAgentConfig = {}) {\n\t\tsuper()\n\n\t\tthis.config = config\n\t\tthis.#llm = new LLM(this.config)\n\t\tthis.panel = new Panel({\n\t\t\tlanguage: this.config.language,\n\t\t\tonExecuteTask: (task) => this.execute(task),\n\t\t\tonStop: () => this.dispose(),\n\t\t\tonPauseToggle: () => {\n\t\t\t\tthis.paused = !this.paused\n\t\t\t\treturn this.paused\n\t\t\t},\n\t\t\tgetPaused: () => this.paused,\n\t\t})\n\t\tthis.tools = new Map(tools)\n\n\t\t// Initialize PageController with config (mask enabled by default)\n\t\tthis.pageController = new PageController({\n\t\t\t...this.config,\n\t\t\tenableMask: this.config.enableMask ?? true,\n\t\t})\n\n\t\t// Listen to LLM events\n\t\tthis.#llmRetryListener = (e) => {\n\t\t\tconst { current, max } = (e as CustomEvent).detail\n\t\t\tthis.panel.update({ type: 'retry', current, max })\n\t\t}\n\t\tthis.#llmErrorListener = (e) => {\n\t\t\tconst { error } = (e as CustomEvent).detail\n\t\t\tthis.panel.update({ type: 'error', message: `step failed: ${error.message}` })\n\t\t}\n\t\tthis.#llm.addEventListener('retry', this.#llmRetryListener)\n\t\tthis.#llm.addEventListener('error', this.#llmErrorListener)\n\n\t\tif (this.config.customTools) {\n\t\t\tfor (const [name, tool] of Object.entries(this.config.customTools)) {\n\t\t\t\tif (tool === null) {\n\t\t\t\t\tthis.tools.delete(name)\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tthis.tools.set(name, tool)\n\t\t\t}\n\t\t}\n\n\t\tif (!this.config.experimentalScriptExecutionTool) {\n\t\t\tthis.tools.delete('execute_javascript')\n\t\t}\n\n\t\tthis.#beforeUnloadListener = (e) => {\n\t\t\tif (!this.disposed) this.dispose('PAGE_UNLOADING')\n\t\t}\n\t\twindow.addEventListener('beforeunload', this.#beforeUnloadListener)\n\t}\n\n\tasync execute(task: string): Promise<ExecutionResult> {\n\t\tif (!task) throw new Error('Task is required')\n\t\tthis.task = task\n\t\tthis.taskId = uid()\n\n\t\tconst onBeforeStep = this.config.onBeforeStep || (() => void 0)\n\t\tconst onAfterStep = this.config.onAfterStep || (() => void 0)\n\t\tconst onBeforeTask = this.config.onBeforeTask || (() => void 0)\n\t\tconst onAfterTask = this.config.onAfterTask || (() => void 0)\n\n\t\tawait onBeforeTask.call(this)\n\n\t\t// Show mask and panel\n\t\tthis.pageController.showMask()\n\n\t\tthis.panel.show()\n\t\tthis.panel.reset()\n\n\t\tthis.panel.update({ type: 'input', task: this.task })\n\n\t\tif (this.#abortController) {\n\t\t\tthis.#abortController.abort()\n\t\t\tthis.#abortController = new AbortController()\n\t\t}\n\n\t\tthis.history = []\n\n\t\ttry {\n\t\t\tlet step = 0\n\n\t\t\twhile (true) {\n\t\t\t\tawait onBeforeStep.call(this, step)\n\n\t\t\t\tconsole.group(`step: ${step}`)\n\n\t\t\t\t// abort\n\t\t\t\tif (this.#abortController.signal.aborted) throw new Error('AbortError')\n\t\t\t\t// pause\n\t\t\t\tawait waitUntil(() => !this.paused)\n\n\t\t\t\t// Update status to thinking\n\t\t\t\tconsole.log(chalk.blue('Thinking...'))\n\t\t\t\tthis.panel.update({ type: 'thinking' })\n\n\t\t\t\tconst result = await this.#llm.invoke(\n\t\t\t\t\t[\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\trole: 'system',\n\t\t\t\t\t\t\tcontent: this.#getSystemPrompt(),\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\trole: 'user',\n\t\t\t\t\t\t\tcontent: await this.#assembleUserPrompt(),\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\t{ AgentOutput: this.#packMacroTool() },\n\t\t\t\t\tthis.#abortController.signal,\n\t\t\t\t\t{\n\t\t\t\t\t\ttoolChoiceName: 'AgentOutput',\n\t\t\t\t\t\tnormalizeResponse,\n\t\t\t\t\t}\n\t\t\t\t)\n\n\t\t\t\tconst macroResult = result.toolResult as MacroToolResult\n\t\t\t\tconst input = macroResult.input\n\t\t\t\tconst output = macroResult.output\n\t\t\t\tconst brain = {\n\t\t\t\t\tevaluation_previous_goal: input.evaluation_previous_goal || '',\n\t\t\t\t\tmemory: input.memory || '',\n\t\t\t\t\tnext_goal: input.next_goal || '',\n\t\t\t\t}\n\t\t\t\tconst actionName = Object.keys(input.action)[0]\n\t\t\t\tconst action = {\n\t\t\t\t\tname: actionName,\n\t\t\t\t\tinput: input.action[actionName],\n\t\t\t\t\toutput: output,\n\t\t\t\t}\n\n\t\t\t\tthis.history.push({\n\t\t\t\t\tbrain,\n\t\t\t\t\taction,\n\t\t\t\t\tusage: result.usage,\n\t\t\t\t})\n\n\t\t\t\tconsole.log(chalk.green('Step finished:'), actionName)\n\t\t\t\tconsole.groupEnd()\n\n\t\t\t\tawait onAfterStep.call(this, step, this.history)\n\n\t\t\t\tstep++\n\t\t\t\tif (step > MAX_STEPS) {\n\t\t\t\t\tthis.#onDone('Step count exceeded maximum limit', false)\n\t\t\t\t\tconst result: ExecutionResult = {\n\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\tdata: 'Step count exceeded maximum limit',\n\t\t\t\t\t\thistory: this.history,\n\t\t\t\t\t}\n\t\t\t\t\tawait onAfterTask.call(this, result)\n\t\t\t\t\treturn result\n\t\t\t\t}\n\t\t\t\tif (actionName === 'done') {\n\t\t\t\t\tconst success = action.input?.success ?? false\n\t\t\t\t\tconst text = action.input?.text || 'no text provided'\n\t\t\t\t\tconsole.log(chalk.green.bold('Task completed'), success, text)\n\t\t\t\t\tthis.#onDone(text, success)\n\t\t\t\t\tconst result: ExecutionResult = {\n\t\t\t\t\t\tsuccess,\n\t\t\t\t\t\tdata: text,\n\t\t\t\t\t\thistory: this.history,\n\t\t\t\t\t}\n\t\t\t\t\tawait onAfterTask.call(this, result)\n\t\t\t\t\treturn result\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error: unknown) {\n\t\t\tconsole.error('Task failed', error)\n\t\t\tthis.#onDone(String(error), false)\n\t\t\tconst result: ExecutionResult = {\n\t\t\t\tsuccess: false,\n\t\t\t\tdata: String(error),\n\t\t\t\thistory: this.history,\n\t\t\t}\n\t\t\tawait onAfterTask.call(this, result)\n\t\t\treturn result\n\t\t}\n\t}\n\n\t/**\n\t * Merge all tools into a single MacroTool with the following input:\n\t * - thinking: string\n\t * - evaluation_previous_goal: string\n\t * - memory: string\n\t * - next_goal: string\n\t * - action: { toolName: toolInput }\n\t * where action must be selected from tools defined in this.tools\n\t */\n\t#packMacroTool(): Tool<MacroToolInput, MacroToolResult> {\n\t\tconst tools = this.tools\n\n\t\tconst actionSchemas = Array.from(tools.entries()).map(([toolName, tool]) => {\n\t\t\treturn zod.object({ [toolName]: tool.inputSchema }).describe(tool.description)\n\t\t})\n\n\t\tconst actionSchema = zod.union(\n\t\t\tactionSchemas as unknown as [zod.ZodType, zod.ZodType, ...zod.ZodType[]]\n\t\t)\n\n\t\tconst macroToolSchema = zod.object({\n\t\t\t// thinking: zod.string().optional(),\n\t\t\tevaluation_previous_goal: zod.string().optional(),\n\t\t\tmemory: zod.string().optional(),\n\t\t\tnext_goal: zod.string().optional(),\n\t\t\taction: actionSchema,\n\t\t})\n\n\t\treturn {\n\t\t\tdescription: 'You MUST call this tool every step. Outputs your reflections and next action.',\n\t\t\tinputSchema: macroToolSchema as zod.ZodType<MacroToolInput>,\n\t\t\texecute: async (input: MacroToolInput): Promise<MacroToolResult> => {\n\t\t\t\t// abort\n\t\t\t\tif (this.#abortController.signal.aborted) throw new Error('AbortError')\n\t\t\t\t// pause\n\t\t\t\tawait waitUntil(() => !this.paused)\n\n\t\t\t\tconsole.log(chalk.blue.bold('MacroTool execute'), input)\n\t\t\t\tconst action = input.action\n\n\t\t\t\tconst toolName = Object.keys(action)[0]\n\t\t\t\tconst toolInput = action[toolName]\n\t\t\t\tconst brain = trimLines(`β
: ${input.evaluation_previous_goal}\n\t\t\t\t\t\tπΎ: ${input.memory}\n\t\t\t\t\t\tπ―: ${input.next_goal}\n\t\t\t\t\t`)\n\n\t\t\t\tconsole.log(brain)\n\t\t\t\tthis.panel.update({ type: 'thinking', text: brain })\n\n\t\t\t\t// Find the corresponding tool\n\t\t\t\tconst tool = tools.get(toolName)\n\t\t\t\tassert(tool, `Tool ${toolName} not found. (@note should have been caught before this!!!)`)\n\n\t\t\t\tconsole.log(chalk.blue.bold(`Executing tool: ${toolName}`), toolInput)\n\t\t\t\tthis.panel.update({ type: 'toolExecuting', toolName, args: toolInput })\n\n\t\t\t\tconst startTime = Date.now()\n\n\t\t\t\t// Execute tool, bind `this` to PageAgent\n\t\t\t\tlet result = await tool.execute.bind(this)(toolInput)\n\n\t\t\t\tconst duration = Date.now() - startTime\n\t\t\t\tconsole.log(chalk.green.bold(`Tool (${toolName}) executed for ${duration}ms`), result)\n\n\t\t\t\tif (toolName === 'wait') {\n\t\t\t\t\tthis.#totalWaitTime += Math.round(toolInput.seconds + duration / 1000)\n\t\t\t\t\tresult += `\\n<sys> You have waited ${this.#totalWaitTime} seconds accumulatively.`\n\t\t\t\t\tif (this.#totalWaitTime >= 3)\n\t\t\t\t\t\tresult += '\\nDo NOT wait any longer unless you have a good reason.\\n'\n\t\t\t\t\tresult += '</sys>'\n\t\t\t\t} else {\n\t\t\t\t\t// For other tools, reset wait time\n\t\t\t\t\tthis.#totalWaitTime = 0\n\t\t\t\t}\n\n\t\t\t\t// Briefly display execution result\n\t\t\t\tthis.panel.update({\n\t\t\t\t\ttype: 'toolCompleted',\n\t\t\t\t\ttoolName,\n\t\t\t\t\targs: toolInput,\n\t\t\t\t\tresult,\n\t\t\t\t\tduration,\n\t\t\t\t})\n\n\t\t\t\t// Wait a moment to let user see the result\n\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, 100))\n\n\t\t\t\t// Return structured result\n\t\t\t\treturn {\n\t\t\t\t\tinput,\n\t\t\t\t\toutput: result,\n\t\t\t\t}\n\t\t\t},\n\t\t}\n\t}\n\n\t/**\n\t * Get system prompt, dynamically replace language settings based on configured language\n\t */\n\t#getSystemPrompt(): string {\n\t\tlet systemPrompt = SYSTEM_PROMPT\n\n\t\tconst targetLanguage = this.config.language === 'zh-CN' ? 'δΈζ' : 'English'\n\t\tsystemPrompt = systemPrompt.replace(\n\t\t\t/Default working language: \\*\\*.*?\\*\\*/,\n\t\t\t`Default working language: **${targetLanguage}**`\n\t\t)\n\n\t\treturn systemPrompt\n\t}\n\n\t/**\n\t * Get instructions from config and format as XML block\n\t */\n\tasync #getInstructions(): Promise<string> {\n\t\tconst { instructions } = this.config\n\t\tif (!instructions) return ''\n\n\t\tconst systemInstructions = instructions.system?.trim()\n\t\tconst url = await this.pageController.getCurrentUrl()\n\t\tlet pageInstructions: string | undefined\n\n\t\tif (instructions.getPageInstructions) {\n\t\t\ttry {\n\t\t\t\tpageInstructions = instructions.getPageInstructions(url)?.trim()\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(\n\t\t\t\t\tchalk.red('[PageAgent] Failed to execute getPageInstructions callback:'),\n\t\t\t\t\terror\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\t\tif (!systemInstructions && !pageInstructions) return ''\n\n\t\tlet result = '<instructions>\\n'\n\n\t\tif (systemInstructions) {\n\t\t\tresult += `<system_instructions>\\n${systemInstructions}\\n</system_instructions>\\n`\n\t\t}\n\n\t\tif (pageInstructions) {\n\t\t\tresult += `<page_instructions>\\n${pageInstructions}\\n</page_instructions>\\n`\n\t\t}\n\n\t\tresult += '</instructions>\\n\\n'\n\n\t\treturn result\n\t}\n\n\tasync #assembleUserPrompt(): Promise<string> {\n\t\tlet prompt = ''\n\n\t\t// <instructions> (optional)\n\t\tprompt += await this.#getInstructions()\n\n\t\t// <agent_state>\n\t\t// - <user_request>\n\t\t// - <step_info>\n\t\t// <agent_state>\n\n\t\tprompt += `<agent_state>\n\t\t\t<user_request>\n\t\t\t${this.task}\n\t\t\t</user_request>\n\t\t\t<step_info>\n\t\t\tStep ${this.history.length + 1} of ${MAX_STEPS} max possible steps\n\t\t\tCurrent date and time: ${new Date().toISOString()}\n\t\t\t</step_info>\n\t\t\t</agent_state>\n\t\t`\n\n\t\t// <agent_history>\n\t\t// - <step_>\n\n\t\tprompt += '\\n<agent_history>\\n'\n\n\t\tthis.history.forEach((history, index) => {\n\t\t\tprompt += `<step_${index + 1}>\n\t\t\t\tEvaluation of Previous Step: ${history.brain.evaluation_previous_goal}\n\t\t\t\tMemory: ${history.brain.memory}\n\t\t\t\tNext Goal: ${history.brain.next_goal}\n\t\t\t\tAction Results: ${history.action.output}\n\t\t\t\t</step_${index + 1}>\n\t\t\t`\n\t\t})\n\n\t\tprompt += '</agent_history>\\n\\n'\n\n\t\t// <browser_state>\n\n\t\tprompt += await this.#getBrowserState()\n\n\t\treturn trimLines(prompt)\n\t}\n\n\t#onDone(text: string, success = true) {\n\t\tthis.pageController.cleanUpHighlights()\n\n\t\t// Update panel status\n\t\tif (success) {\n\t\t\tthis.panel.update({ type: 'output', text })\n\t\t} else {\n\t\t\tthis.panel.update({ type: 'error', message: text })\n\t\t}\n\n\t\t// Task completed\n\t\tthis.panel.update({ type: 'completed' })\n\n\t\tthis.pageController.hideMask()\n\n\t\tthis.#abortController.abort()\n\t}\n\n\tasync #getBrowserState(): Promise<string> {\n\t\tconst pageUrl = await this.pageController.getCurrentUrl()\n\t\tconst pageTitle = await this.pageController.getPageTitle()\n\t\tconst pi = await this.pageController.getPageInfo()\n\t\tconst viewportExpansion = await this.pageController.getViewportExpansion()\n\n\t\tawait this.pageController.updateTree()\n\n\t\tlet simplifiedHTML = await this.pageController.getSimplifiedHTML()\n\n\t\tif (this.config.transformPageContent) {\n\t\t\tsimplifiedHTML = await this.config.transformPageContent(simplifiedHTML)\n\t\t}\n\n\t\tlet prompt = trimLines(`<browser_state>\n\t\t\tCurrent Page: [${pageTitle}](${pageUrl})\n\n\t\t\tPage info: ${pi.viewport_width}x${pi.viewport_height}px viewport, ${pi.page_width}x${pi.page_height}px total page size, ${pi.pages_above.toFixed(1)} pages above, ${pi.pages_below.toFixed(1)} pages below, ${pi.total_pages.toFixed(1)} total pages, at ${(pi.current_page_position * 100).toFixed(0)}% of page\n\n\t\t\t${viewportExpansion === -1 ? 'Interactive elements from top layer of the current page (full page):' : 'Interactive elements from top layer of the current page inside the viewport:'}\n\n\t\t`)\n\n\t\t// Page header info\n\t\tconst has_content_above = pi.pixels_above > 4\n\t\tif (has_content_above && viewportExpansion !== -1) {\n\t\t\tprompt += `... ${pi.pixels_above} pixels above (${pi.pages_above.toFixed(1)} pages) - scroll to see more ...\\n`\n\t\t} else {\n\t\t\tprompt += `[Start of page]\\n`\n\t\t}\n\n\t\t// Current viewport info\n\t\tprompt += simplifiedHTML\n\t\tprompt += `\\n`\n\n\t\t// Page footer info\n\t\tconst has_content_below = pi.pixels_below > 4\n\t\tif (has_content_below && viewportExpansion !== -1) {\n\t\t\tprompt += `... ${pi.pixels_below} pixels below (${pi.pages_below.toFixed(1)} pages) - scroll to see more ...\\n`\n\t\t} else {\n\t\t\tprompt += `[End of page]\\n`\n\t\t}\n\n\t\tprompt += `</browser_state>\\n`\n\n\t\treturn prompt\n\t}\n\n\tdispose(reason?: string) {\n\t\tconsole.log('Disposing PageAgent...')\n\t\tthis.disposed = true\n\t\tthis.pageController.dispose()\n\t\tthis.panel.dispose()\n\t\tthis.history = []\n\t\tthis.#abortController.abort(reason ?? 'PageAgent disposed')\n\n\t\t// Clean up LLM event listeners\n\t\tif (this.#llmRetryListener) {\n\t\t\tthis.#llm.removeEventListener('retry', this.#llmRetryListener)\n\t\t\tthis.#llmRetryListener = null\n\t\t}\n\t\tif (this.#llmErrorListener) {\n\t\t\tthis.#llm.removeEventListener('error', this.#llmErrorListener)\n\t\t\tthis.#llmErrorListener = null\n\t\t}\n\n\t\t// Clean up window event listeners\n\t\tif (this.#beforeUnloadListener) {\n\t\t\twindow.removeEventListener('beforeunload', this.#beforeUnloadListener)\n\t\t\tthis.#beforeUnloadListener = null\n\t\t}\n\n\t\tthis.config.onDispose?.call(this, reason)\n\t}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.js","sources":["../../src/config/constants.ts","../../src/prompts/system_prompt.md?raw","../../src/utils/autoFixer.ts","../../src/utils/index.ts","../../src/tools/index.ts","../../src/utils/assert.ts","../../src/PageAgent.ts"],"sourcesContent":["// Agent-specific constants (LLM constants moved to @page-agent/llms)\nexport const MAX_STEPS = 20\n","export default \"You are an AI agent designed to operate in an iterative loop to automate browser tasks. Your ultimate goal is accomplishing the task provided in <user_request>.\\n\\n<intro>\\nYou excel at following tasks:\\n1. Navigating complex websites and extracting precise information\\n2. Automating form submissions and interactive web actions\\n3. Gathering and saving information \\n4. Operate effectively in an agent loop\\n5. Efficiently performing diverse web tasks\\n</intro>\\n\\n<language_settings>\\n- Default working language: **δΈζ**\\n- Use the language that user is using. Return in user's language.\\n</language_settings>\\n\\n<input>\\nAt every step, your input will consist of: \\n1. <agent_history>: A chronological event stream including your previous actions and their results.\\n2. <agent_state>: Current <user_request> and <step_info>.\\n3. <browser_state>: Current URL, interactive elements indexed for actions, and visible page content.\\n</input>\\n\\n<agent_history>\\nAgent history will be given as a list of step information as follows:\\n\\n<step_{step_number}>:\\nEvaluation of Previous Step: Assessment of last action\\nMemory: Your memory of this step\\nNext Goal: Your goal for this step\\nAction Results: Your actions and their results\\n</step_{step_number}>\\n\\nand system messages wrapped in <sys> tag.\\n</agent_history>\\n\\n<user_request>\\nUSER REQUEST: This is your ultimate objective and always remains visible.\\n- This has the highest priority. Make the user happy.\\n- If the user request is very specific - then carefully follow each step and dont skip or hallucinate steps.\\n- If the task is open ended you can plan yourself how to get it done.\\n</user_request>\\n\\n<browser_state>\\n1. Browser State will be given as:\\n\\nCurrent URL: URL of the page you are currently viewing.\\nInteractive Elements: All interactive elements will be provided in format as [index]<type>text</type> where\\n- index: Numeric identifier for interaction\\n- type: HTML element type (button, input, etc.)\\n- text: Element description\\n\\nExamples:\\n[33]<div>User form</div>\\n\\\\t*[35]<button aria-label='Submit form'>Submit</button>\\n\\nNote that:\\n- Only elements with numeric indexes in [] are interactive\\n- (stacked) indentation (with \\\\t) is important and means that the element is a (html) child of the element above (with a lower index)\\n- Elements tagged with `*[` are the new clickable elements that appeared on the website since the last step - if url has not changed.\\n- Pure text elements without [] are not interactive.\\n</browser_state>\\n\\n<browser_rules>\\nStrictly follow these rules while using the browser and navigating the web:\\n- Only interact with elements that have a numeric [index] assigned.\\n- Only use indexes that are explicitly provided.\\n- If the page changes after, for example, an input text action, analyze if you need to interact with new elements, e.g. selecting the right option from the list.\\n- By default, only elements in the visible viewport are listed. Use scrolling actions if you suspect relevant content is offscreen which you need to interact with. Scroll ONLY if there are more pixels below or above the page.\\n- You can scroll by a specific number of pages using the num_pages parameter (e.g., 0.5 for half page, 2.0 for two pages).\\n- All the elements that are scrollable are marked with `data-scrollable` attribute. Including the scrollable distance in every directions. You can scroll *the element* in case some area are overflowed.\\n- If a captcha appears, tell user you can not solve captcha. finished the task and ask user to solve it.\\n- If expected elements are missing, try scrolling, or navigating back.\\n- If the page is not fully loaded, use the `wait` action.\\n- Do not repeat one action for more than 3 times unless some conditions changed.\\n- If you fill an input field and your action sequence is interrupted, most often something changed e.g. suggestions popped up under the field.\\n- If the <user_request> includes specific page information such as product type, rating, price, location, etc., try to apply filters to be more efficient.\\n- The <user_request> is the ultimate goal. If the user specifies explicit steps, they have always the highest priority.\\n- If you input_text into a field, you might need to press enter, click the search button, or select from dropdown for completion.\\n- Don't login into a page if you don't have to. Don't login if you don't have the credentials. \\n- There are 2 types of tasks always first think which type of request you are dealing with:\\n1. Very specific step by step instructions:\\n- Follow them as very precise and don't skip steps. Try to complete everything as requested.\\n2. Open ended tasks. Plan yourself, be creative in achieving them.\\n- If you get stuck e.g. with logins or captcha in open-ended tasks you can re-evaluate the task and try alternative ways, e.g. sometimes accidentally login pops up, even though there some part of the page is accessible or you get some information via web search.\\n</browser_rules>\\n\\n<capability>\\n- You can only handle single page app. Do not jump out of current page.\\n- Do not click on link if it will open in a new page (etc. <a target=\\\"_blank\\\">)\\n- It is ok to fail the task.\\n\\t- User can be wrong. If the request of user is not achievable, inappropriate or you do not have enough information or tools to achieve it. Tell user to make a better request.\\n\\t- Webpage can be broken. All webpages or apps have bugs. Some bug will make it hard for your job. It's encouraged to tell user the problem of current page. Your feedbacks (including failing) are valuable for user.\\n\\t- Trying to hard can be harmful. Repeating some action back and forth or pushing for a complex procedure with little knowledge can cause unwanted result and harmful side-effects. User would rather you to complete the task with a fail.\\n- If you are not clear about the request or steps. `ask_user` to clarify it.\\n- If you do not have knowledge for the current webpage or task. You must require user to give specific instructions and detailed steps.\\n</capability>\\n\\n<task_completion_rules>\\nYou must call the `done` action in one of three cases:\\n- When you have fully completed the USER REQUEST.\\n- When you reach the final allowed step (`max_steps`), even if the task is incomplete.\\n- When you feel stuck or unable to solve user request. Or user request is not clear or contains inappropriate content.\\n- If it is ABSOLUTELY IMPOSSIBLE to continue.\\n\\nThe `done` action is your opportunity to terminate and share your findings with the user.\\n- Set `success` to `true` only if the full USER REQUEST has been completed with no missing components.\\n- If any part of the request is missing, incomplete, or uncertain, set `success` to `false`.\\n- You can use the `text` field of the `done` action to communicate your findings and to provide a coherent reply to the user and fulfill the USER REQUEST.\\n- You are ONLY ALLOWED to call `done` as a single action. Don't call it together with other actions.\\n- If the user asks for specified format, such as \\\"return JSON with following structure\\\", \\\"return a list of format...\\\", MAKE sure to use the right format in your answer.\\n- If the user asks for a structured output, your `done` action's schema may be modified. Take this schema into account when solving the task!\\n</task_completion_rules>\\n\\n<reasoning_rules>\\nExhibit the following reasoning patterns to successfully achieve the <user_request>:\\n\\n- Reason about <agent_history> to track progress and context toward <user_request>.\\n- Analyze the most recent \\\"Next Goal\\\" and \\\"Action Result\\\" in <agent_history> and clearly state what you previously tried to achieve.\\n- Analyze all relevant items in <agent_history> and <browser_state> to understand your state.\\n- Explicitly judge success/failure/uncertainty of the last action. Never assume an action succeeded just because it appears to be executed in your last step in <agent_history>. If the expected change is missing, mark the last action as failed (or uncertain) and plan a recovery.\\n- Analyze whether you are stuck, e.g. when you repeat the same actions multiple times without any progress. Then consider alternative approaches e.g. scrolling for more context or ask user for help.\\n- `ask_user` for help if you have any difficulty. Users want to be kept in the loop.\\n- If you see information relevant to <user_request>, plan saving the information to memory.\\n- Always reason about the <user_request>. Make sure to carefully analyze the specific steps and information required. E.g. specific filters, specific form fields, specific information to search. Make sure to always compare the current trajectory with the user request and think carefully if thats how the user requested it.\\n</reasoning_rules>\\n\\n<examples>\\nHere are examples of good output patterns. Use them as reference but never copy them directly.\\n\\n<evaluation_examples>\\n- Positive Examples:\\n\\\"evaluation_previous_goal\\\": \\\"Successfully navigated to the product page and found the target information. Verdict: Success\\\"\\n\\\"evaluation_previous_goal\\\": \\\"Clicked the login button and user authentication form appeared. Verdict: Success\\\"\\n</evaluation_examples>\\n\\n<memory_examples>\\n\\\"memory\\\": \\\"Found many pending reports that need to be analyzed in the main page. Successfully processed the first 2 reports on quarterly sales data and moving on to inventory analysis and customer feedback reports.\\\"\\n</memory_examples>\\n\\n<next_goal_examples>\\n\\\"next_goal\\\": \\\"Click on the 'Add to Cart' button to proceed with the purchase flow.\\\"\\n\\\"next_goal\\\": \\\"Extract details from the first item on the page.\\\"\\n</next_goal_examples>\\n</examples>\\n\\n<output>\\nYou must ALWAYS respond with a valid JSON in this exact format:\\n\\n{\\n \\\"evaluation_previous_goal\\\": \\\"Concise one-sentence analysis of your last action. Clearly state success, failure, or uncertain.\\\",\\n \\\"memory\\\": \\\"1-3 concise sentences of specific memory of this step and overall progress. You should put here everything that will help you track progress in future steps. Like counting pages visited, items found, etc.\\\",\\n \\\"next_goal\\\": \\\"State the next immediate goal and action to achieve it, in one clear sentence.\\\"\\n \\\"action\\\":{\\\"one_action_name\\\": {// action-specific parameter}}\\n}\\n</output>\\n\"","import chalk from 'chalk'\n\n/**\n * Normalize LLM response and fix common format issues.\n *\n * Handles:\n * - No tool_calls but JSON in message.content (fallback)\n * - Model returns action name as tool call instead of AgentOutput\n * - Arguments wrapped as double JSON string\n * - Nested function call format\n * - Missing action field (fallback to wait)\n * - etc.\n */\nexport function normalizeResponse(response: any): any {\n\tlet resolvedArguments = null as any\n\n\tconst choice = (response as { choices?: Choice[] }).choices?.[0]\n\tif (!choice) throw new Error('No choices in response')\n\n\tconst message = choice.message\n\tif (!message) throw new Error('No message in choice')\n\n\tconst toolCall = message.tool_calls?.[0]\n\n\t// fix level and location of arguments\n\n\tif (toolCall?.function?.arguments) {\n\t\tresolvedArguments = safeJsonParse(toolCall.function.arguments)\n\n\t\t// case: sometimes the model only returns the action level\n\t\tif (toolCall.function.name && toolCall.function.name !== 'AgentOutput') {\n\t\t\tconsole.log(chalk.yellow(`[normalizeResponse] #1: fixing tool_call`))\n\t\t\tresolvedArguments = { action: safeJsonParse(resolvedArguments) }\n\t\t}\n\t} else {\n\t\t// case: sometimes the model returns json in content instead of tool_calls\n\t\tif (message.content) {\n\t\t\tconst content = message.content.trim()\n\t\t\tconst jsonInContent = retrieveJsonFromString(content)\n\t\t\tif (jsonInContent) {\n\t\t\t\tresolvedArguments = safeJsonParse(jsonInContent)\n\n\t\t\t\t// case: sometimes the content json includes upper level wrapper\n\t\t\t\tif (resolvedArguments?.name === 'AgentOutput') {\n\t\t\t\t\tconsole.log(chalk.yellow(`[normalizeResponse] #2: fixing tool_call`))\n\t\t\t\t\tresolvedArguments = safeJsonParse(resolvedArguments.arguments)\n\t\t\t\t}\n\n\t\t\t\t// case: sometimes even 2-levels of wrapping\n\t\t\t\tif (resolvedArguments?.type === 'function') {\n\t\t\t\t\tconsole.log(chalk.yellow(`[normalizeResponse] #3: fixing tool_call`))\n\t\t\t\t\tresolvedArguments = safeJsonParse(resolvedArguments.function.arguments)\n\t\t\t\t}\n\n\t\t\t\t// case: and sometimes action level only\n\t\t\t\t// todo: needs better detection logic\n\t\t\t\tif (\n\t\t\t\t\t!resolvedArguments?.action &&\n\t\t\t\t\t!resolvedArguments?.evaluation_previous_goal &&\n\t\t\t\t\t!resolvedArguments?.memory &&\n\t\t\t\t\t!resolvedArguments?.next_goal &&\n\t\t\t\t\t!resolvedArguments?.thinking\n\t\t\t\t) {\n\t\t\t\t\tconsole.log(chalk.yellow(`[normalizeResponse] #4: fixing tool_call`))\n\t\t\t\t\tresolvedArguments = { action: safeJsonParse(resolvedArguments) }\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthrow new Error('No tool_call and the message content does not contain valid JSON')\n\t\t\t}\n\t\t} else {\n\t\t\tthrow new Error('No tool_call nor message content is present')\n\t\t}\n\t}\n\n\t// fix double stringified arguments\n\tresolvedArguments = safeJsonParse(resolvedArguments)\n\tif (resolvedArguments.action) {\n\t\tresolvedArguments.action = safeJsonParse(resolvedArguments.action)\n\t}\n\n\t// fix incomplete formats\n\tif (!resolvedArguments.action) {\n\t\tconsole.log(chalk.yellow(`[normalizeResponse] #5: fixing tool_call`))\n\t\tresolvedArguments.action = { name: 'wait', input: { seconds: 1 } }\n\t}\n\n\t// pack back to standard format\n\treturn {\n\t\t...response,\n\t\tchoices: [\n\t\t\t{\n\t\t\t\t...choice,\n\t\t\t\tmessage: {\n\t\t\t\t\t...message,\n\t\t\t\t\ttool_calls: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t...(toolCall || {}),\n\t\t\t\t\t\t\tfunction: {\n\t\t\t\t\t\t\t\t...(toolCall?.function || {}),\n\t\t\t\t\t\t\t\tname: 'AgentOutput',\n\t\t\t\t\t\t\t\targuments: JSON.stringify(resolvedArguments),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t],\n\t}\n}\n\n/**\n * Safely parse JSON, return original input if not json.\n */\nfunction safeJsonParse(input: any): any {\n\tif (typeof input === 'string') {\n\t\ttry {\n\t\t\treturn JSON.parse(input.trim())\n\t\t} catch {\n\t\t\treturn input\n\t\t}\n\t}\n\treturn input\n}\n\n/**\n * Extract and parse JSON from a string.\n * - Treat content between the first `{` and the last `}` as JSON.\n * - Try to parse that content as JSON and return the parsed value (object/array/primitive) if successful, otherwise return null.\n */\nfunction retrieveJsonFromString(str: string): any {\n\ttry {\n\t\tconst json = /({[\\s\\S]*})/.exec(str) ?? []\n\t\tif (json.length === 0) {\n\t\t\treturn null\n\t\t}\n\t\treturn JSON.parse(json[0]!)\n\t} catch {\n\t\treturn null\n\t}\n}\n\ninterface Choice {\n\tmessage?: {\n\t\trole?: 'assistant'\n\t\tcontent?: string\n\t\ttool_calls?: {\n\t\t\tid?: string\n\t\t\ttype?: 'function'\n\t\t\tfunction?: {\n\t\t\t\tname?: string\n\t\t\t\targuments?: string\n\t\t\t}\n\t\t}[]\n\t}\n\tindex?: 0\n\tfinish_reason?: 'tool_calls'\n}\n","export { normalizeResponse } from './autoFixer'\n\n/**\n * Wait until condition becomes true\n * @returns Returns when condition becomes true, throws otherwise\n * @param timeout Timeout in milliseconds, default 0 means no timeout, throws error on timeout\n */\nexport async function waitUntil(check: () => boolean, timeout = 60 * 60_1000): Promise<boolean> {\n\tif (check()) return true\n\n\treturn new Promise((resolve, reject) => {\n\t\tconst start = Date.now()\n\t\tconst interval = setInterval(() => {\n\t\t\tif (check()) {\n\t\t\t\tclearInterval(interval)\n\t\t\t\tresolve(true)\n\t\t\t} else if (Date.now() - start > timeout) {\n\t\t\t\tclearInterval(interval)\n\t\t\t\treject(new Error('Timeout waiting for condition to become true'))\n\t\t\t}\n\t\t}, 100)\n\t})\n}\n\nexport async function waitFor(seconds: number): Promise<void> {\n\tawait new Promise((resolve) => setTimeout(resolve, seconds * 1000))\n}\n\n//\n\nexport function truncate(text: string, maxLength: number): string {\n\tif (text.length > maxLength) {\n\t\treturn text.substring(0, maxLength) + '...'\n\t}\n\treturn text\n}\n\n//\n\nexport function trimLines(text: string): string {\n\treturn text\n\t\t.split('\\n')\n\t\t.map((line) => line.trim())\n\t\t.join('\\n')\n}\n\n//\n\nexport function randomID(existingIDs?: string[]): string {\n\tlet id = Math.random().toString(36).substring(2, 11)\n\n\tif (!existingIDs) {\n\t\treturn id\n\t}\n\n\tconst MAX_TRY = 1000\n\tlet tryCount = 0\n\n\twhile (existingIDs.includes(id)) {\n\t\tid = Math.random().toString(36).substring(2, 11)\n\t\ttryCount++\n\t\tif (tryCount > MAX_TRY) {\n\t\t\tthrow new Error('randomID: too many try')\n\t\t}\n\t}\n\n\treturn id\n}\n\n//\n\nif (!window.__PAGE_AGENT_IDS__) {\n\twindow.__PAGE_AGENT_IDS__ = []\n}\n\nconst ids = window.__PAGE_AGENT_IDS__\n\n/**\n * Generate a random ID.\n * @note Unique within this window.\n */\nexport function uid() {\n\tconst id = randomID(ids)\n\tids.push(id)\n\treturn id\n}\n","/**\n * Internal tools for PageAgent.\n * @note Adapted from browser-use\n */\nimport zod, { type z } from 'zod'\n\nimport type { PageAgent } from '../PageAgent'\nimport { waitFor } from '../utils'\n\n/**\n * Internal tool definition that has access to PageAgent `this` context\n */\nexport interface PageAgentTool<TParams = any> {\n\t// name: string\n\tdescription: string\n\tinputSchema: z.ZodType<TParams>\n\texecute: (this: PageAgent, args: TParams) => Promise<string>\n}\n\nexport function tool<TParams>(options: PageAgentTool<TParams>): PageAgentTool<TParams> {\n\treturn options\n}\n\n/**\n * Internal tools for PageAgent.\n * Note: Using any to allow different parameter types for each tool\n */\nexport const tools = new Map<string, PageAgentTool>()\n\ntools.set(\n\t'done',\n\ttool({\n\t\tdescription:\n\t\t\t'Complete task - provide a summary of results for the user. Set success=True if task completed successfully, false otherwise. Text should be your response to the user summarizing results.',\n\t\tinputSchema: zod.object({\n\t\t\ttext: zod.string(),\n\t\t\tsuccess: zod.boolean().default(true),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\t// @note main loop will handle this one\n\t\t\t// this.onDone(input.text, input.success)\n\t\t\treturn Promise.resolve('Task completed')\n\t\t},\n\t})\n)\n\ntools.set(\n\t'wait',\n\ttool({\n\t\tdescription:\n\t\t\t'Wait for x seconds. default 1s (max 10 seconds, min 1 second). This can be used to wait until the page or data is fully loaded.',\n\t\tinputSchema: zod.object({\n\t\t\tseconds: zod.number().min(1).max(10).default(1),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst lastTimeUpdate = await this.pageController.getLastUpdateTime()\n\t\t\tconst actualWaitTime = Math.max(0, input.seconds - (Date.now() - lastTimeUpdate) / 1000)\n\t\t\tconsole.log(`actualWaitTime: ${actualWaitTime} seconds`)\n\t\t\tawait waitFor(actualWaitTime)\n\t\t\treturn `β
Waited for ${input.seconds} seconds.`\n\t\t},\n\t})\n)\n\ntools.set(\n\t'ask_user',\n\ttool({\n\t\tdescription:\n\t\t\t'Ask the user a question and wait for their answer. Use this if you need more information or clarification.',\n\t\tinputSchema: zod.object({\n\t\t\tquestion: zod.string(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst answer = await this.panel.askUser(input.question)\n\t\t\treturn `β
Received user answer: ${answer}`\n\t\t},\n\t})\n)\n\ntools.set(\n\t'click_element_by_index',\n\ttool({\n\t\tdescription: 'Click element by index',\n\t\tinputSchema: zod.object({\n\t\t\tindex: zod.int().min(0),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.clickElement(input.index)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\ntools.set(\n\t'input_text',\n\ttool({\n\t\tdescription: 'Click and input text into a input interactive element',\n\t\tinputSchema: zod.object({\n\t\t\tindex: zod.int().min(0),\n\t\t\ttext: zod.string(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.inputText(input.index, input.text)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\ntools.set(\n\t'select_dropdown_option',\n\ttool({\n\t\tdescription:\n\t\t\t'Select dropdown option for interactive element index by the text of the option you want to select',\n\t\tinputSchema: zod.object({\n\t\t\tindex: zod.int().min(0),\n\t\t\ttext: zod.string(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.selectOption(input.index, input.text)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\n/**\n * @note Reference from browser-use\n */\ntools.set(\n\t'scroll',\n\ttool({\n\t\tdescription:\n\t\t\t'Scroll the page by specified number of pages (set down=True to scroll down, down=False to scroll up, num_pages=number of pages to scroll like 0.5 for half page, 1.0 for one page, etc.). Optional index parameter to scroll within a specific element or its scroll container (works well for dropdowns and custom UI components). Optional pixels parameter to scroll by a specific number of pixels instead of pages.',\n\t\tinputSchema: zod.object({\n\t\t\tdown: zod.boolean().default(true),\n\t\t\tnum_pages: zod.number().min(0).max(10).optional().default(0.1),\n\t\t\tpixels: zod.number().int().min(0).optional(),\n\t\t\tindex: zod.number().int().min(0).optional(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.scroll({\n\t\t\t\t...input,\n\t\t\t\tnumPages: input.num_pages,\n\t\t\t})\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\ntools.set(\n\t'scroll_horizontally',\n\ttool({\n\t\tdescription:\n\t\t\t'Scroll the page or element horizontally (set right=True to scroll right, right=False to scroll left, pixels=number of pixels to scroll). Optional index parameter to scroll within a specific element or its scroll container (works well for wide tables).',\n\t\tinputSchema: zod.object({\n\t\t\tright: zod.boolean().default(true),\n\t\t\tpixels: zod.number().int().min(0),\n\t\t\tindex: zod.number().int().min(0).optional(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.scrollHorizontally(input)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\ntools.set(\n\t'execute_javascript',\n\ttool({\n\t\tdescription:\n\t\t\t'Execute JavaScript code on the current page. Supports async/await syntax. Use with caution!',\n\t\tinputSchema: zod.object({\n\t\t\tscript: zod.string(),\n\t\t}),\n\t\texecute: async function (this: PageAgent, input) {\n\t\t\tconst result = await this.pageController.executeJavascript(input.script)\n\t\t\treturn result.message\n\t\t},\n\t})\n)\n\n// @todo get_dropdown_options\n// @todo select_dropdown_option\n// @todo send_keys\n// @todo upload_file\n// @todo go_back\n// @todo extract_structured_data\n","import chalk from 'chalk'\n\n/**\n * Simple assertion function that throws an error if the condition is falsy\n * @param condition - The condition to assert\n * @param message - Optional error message\n * @throws Error if condition is falsy\n */\nexport function assert(condition: unknown, message?: string, silent?: boolean): asserts condition {\n\tif (!condition) {\n\t\tconst errorMessage = message ?? 'Assertion failed'\n\n\t\tif (!silent) console.error(chalk.red(`β assert: ${errorMessage}`))\n\n\t\tthrow new Error(errorMessage)\n\t}\n}\n","/**\n * Copyright (C) 2025 Alibaba Group Holding Limited\n * All rights reserved.\n */\nimport { LLM, type Tool } from '@page-agent/llms'\nimport { PageController } from '@page-agent/page-controller'\nimport { Panel } from '@page-agent/ui'\nimport chalk from 'chalk'\nimport zod from 'zod'\n\nimport type { PageAgentConfig } from './config'\nimport { MAX_STEPS } from './config/constants'\nimport SYSTEM_PROMPT from './prompts/system_prompt.md?raw'\nimport { tools } from './tools'\nimport { normalizeResponse, trimLines, uid, waitUntil } from './utils'\nimport { assert } from './utils/assert'\n\n/**\n * Agent brain state - the reflection-before-action model\n *\n * Every tool call must first reflect on:\n * - evaluation_previous_goal: How well did the previous action achieve its goal?\n * - memory: Key information to remember for future steps\n * - next_goal: What should be accomplished in the next action?\n */\nexport interface AgentReflection {\n\tevaluation_previous_goal: string\n\tmemory: string\n\tnext_goal: string\n}\n\n/**\n * MacroTool input structure\n *\n * This is the core abstraction that enforces the \"reflection-before-action\" mental model.\n * Before executing any action, the LLM must output its reasoning state.\n */\nexport interface MacroToolInput extends Partial<AgentReflection> {\n\taction: Record<string, any>\n}\n\n/**\n * MacroTool output structure\n */\nexport interface MacroToolResult {\n\tinput: MacroToolInput\n\toutput: string\n}\n\nexport type { PageAgentConfig }\nexport { tool, type PageAgentTool } from './tools'\n\nexport interface AgentHistory {\n\tbrain: Partial<AgentReflection>\n\taction: {\n\t\tname: string\n\t\tinput: any\n\t\toutput: string\n\t}\n\tusage: {\n\t\tpromptTokens: number\n\t\tcompletionTokens: number\n\t\ttotalTokens: number\n\t\tcachedTokens?: number\n\t\treasoningTokens?: number\n\t}\n}\n\nexport interface ExecutionResult {\n\tsuccess: boolean\n\tdata: string\n\thistory: AgentHistory[]\n}\n\nexport class PageAgent extends EventTarget {\n\tconfig: PageAgentConfig\n\tid = uid()\n\tpanel: Panel\n\ttools: typeof tools\n\tpaused = false\n\tdisposed = false\n\ttask = ''\n\ttaskId = ''\n\n\t#llm: LLM\n\t#totalWaitTime = 0\n\t#abortController = new AbortController()\n\t#llmRetryListener: ((e: Event) => void) | null = null\n\t#llmErrorListener: ((e: Event) => void) | null = null\n\t#beforeUnloadListener: ((e: Event) => void) | null = null\n\n\t/** PageController for DOM operations */\n\tpageController: PageController\n\n\t/** History records */\n\thistory: AgentHistory[] = []\n\n\tconstructor(config: PageAgentConfig) {\n\t\tsuper()\n\n\t\tthis.config = config\n\t\tthis.#llm = new LLM(this.config)\n\t\tthis.panel = new Panel({\n\t\t\tlanguage: this.config.language,\n\t\t\tonExecuteTask: (task) => this.execute(task),\n\t\t\tonStop: () => this.dispose(),\n\t\t\tonPauseToggle: () => {\n\t\t\t\tthis.paused = !this.paused\n\t\t\t\treturn this.paused\n\t\t\t},\n\t\t\tgetPaused: () => this.paused,\n\t\t})\n\t\tthis.tools = new Map(tools)\n\n\t\t// Initialize PageController with config (mask enabled by default)\n\t\tthis.pageController = new PageController({\n\t\t\t...this.config,\n\t\t\tenableMask: this.config.enableMask ?? true,\n\t\t})\n\n\t\t// Listen to LLM events\n\t\tthis.#llmRetryListener = (e) => {\n\t\t\tconst { current, max } = (e as CustomEvent).detail\n\t\t\tthis.panel.update({ type: 'retry', current, max })\n\t\t}\n\t\tthis.#llmErrorListener = (e) => {\n\t\t\tconst { error } = (e as CustomEvent).detail\n\t\t\tthis.panel.update({ type: 'error', message: `step failed: ${error.message}` })\n\t\t}\n\t\tthis.#llm.addEventListener('retry', this.#llmRetryListener)\n\t\tthis.#llm.addEventListener('error', this.#llmErrorListener)\n\n\t\tif (this.config.customTools) {\n\t\t\tfor (const [name, tool] of Object.entries(this.config.customTools)) {\n\t\t\t\tif (tool === null) {\n\t\t\t\t\tthis.tools.delete(name)\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tthis.tools.set(name, tool)\n\t\t\t}\n\t\t}\n\n\t\tif (!this.config.experimentalScriptExecutionTool) {\n\t\t\tthis.tools.delete('execute_javascript')\n\t\t}\n\n\t\tthis.#beforeUnloadListener = (e) => {\n\t\t\tif (!this.disposed) this.dispose('PAGE_UNLOADING')\n\t\t}\n\t\twindow.addEventListener('beforeunload', this.#beforeUnloadListener)\n\t}\n\n\tasync execute(task: string): Promise<ExecutionResult> {\n\t\tif (!task) throw new Error('Task is required')\n\t\tthis.task = task\n\t\tthis.taskId = uid()\n\n\t\tconst onBeforeStep = this.config.onBeforeStep || (() => void 0)\n\t\tconst onAfterStep = this.config.onAfterStep || (() => void 0)\n\t\tconst onBeforeTask = this.config.onBeforeTask || (() => void 0)\n\t\tconst onAfterTask = this.config.onAfterTask || (() => void 0)\n\n\t\tawait onBeforeTask.call(this)\n\n\t\t// Show mask and panel\n\t\tthis.pageController.showMask()\n\n\t\tthis.panel.show()\n\t\tthis.panel.reset()\n\n\t\tthis.panel.update({ type: 'input', task: this.task })\n\n\t\tif (this.#abortController) {\n\t\t\tthis.#abortController.abort()\n\t\t\tthis.#abortController = new AbortController()\n\t\t}\n\n\t\tthis.history = []\n\n\t\ttry {\n\t\t\tlet step = 0\n\n\t\t\twhile (true) {\n\t\t\t\tawait onBeforeStep.call(this, step)\n\n\t\t\t\tconsole.group(`step: ${step}`)\n\n\t\t\t\t// abort\n\t\t\t\tif (this.#abortController.signal.aborted) throw new Error('AbortError')\n\t\t\t\t// pause\n\t\t\t\tawait waitUntil(() => !this.paused)\n\n\t\t\t\t// Update status to thinking\n\t\t\t\tconsole.log(chalk.blue('Thinking...'))\n\t\t\t\tthis.panel.update({ type: 'thinking' })\n\n\t\t\t\tconst result = await this.#llm.invoke(\n\t\t\t\t\t[\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\trole: 'system',\n\t\t\t\t\t\t\tcontent: this.#getSystemPrompt(),\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\trole: 'user',\n\t\t\t\t\t\t\tcontent: await this.#assembleUserPrompt(),\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\t{ AgentOutput: this.#packMacroTool() },\n\t\t\t\t\tthis.#abortController.signal,\n\t\t\t\t\t{\n\t\t\t\t\t\ttoolChoiceName: 'AgentOutput',\n\t\t\t\t\t\tnormalizeResponse,\n\t\t\t\t\t}\n\t\t\t\t)\n\n\t\t\t\tconst macroResult = result.toolResult as MacroToolResult\n\t\t\t\tconst input = macroResult.input\n\t\t\t\tconst output = macroResult.output\n\t\t\t\tconst brain = {\n\t\t\t\t\tevaluation_previous_goal: input.evaluation_previous_goal || '',\n\t\t\t\t\tmemory: input.memory || '',\n\t\t\t\t\tnext_goal: input.next_goal || '',\n\t\t\t\t}\n\t\t\t\tconst actionName = Object.keys(input.action)[0]\n\t\t\t\tconst action = {\n\t\t\t\t\tname: actionName,\n\t\t\t\t\tinput: input.action[actionName],\n\t\t\t\t\toutput: output,\n\t\t\t\t}\n\n\t\t\t\tthis.history.push({\n\t\t\t\t\tbrain,\n\t\t\t\t\taction,\n\t\t\t\t\tusage: result.usage,\n\t\t\t\t})\n\n\t\t\t\tconsole.log(chalk.green('Step finished:'), actionName)\n\t\t\t\tconsole.groupEnd()\n\n\t\t\t\tawait onAfterStep.call(this, step, this.history)\n\n\t\t\t\tstep++\n\t\t\t\tif (step > MAX_STEPS) {\n\t\t\t\t\tthis.#onDone('Step count exceeded maximum limit', false)\n\t\t\t\t\tconst result: ExecutionResult = {\n\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\tdata: 'Step count exceeded maximum limit',\n\t\t\t\t\t\thistory: this.history,\n\t\t\t\t\t}\n\t\t\t\t\tawait onAfterTask.call(this, result)\n\t\t\t\t\treturn result\n\t\t\t\t}\n\t\t\t\tif (actionName === 'done') {\n\t\t\t\t\tconst success = action.input?.success ?? false\n\t\t\t\t\tconst text = action.input?.text || 'no text provided'\n\t\t\t\t\tconsole.log(chalk.green.bold('Task completed'), success, text)\n\t\t\t\t\tthis.#onDone(text, success)\n\t\t\t\t\tconst result: ExecutionResult = {\n\t\t\t\t\t\tsuccess,\n\t\t\t\t\t\tdata: text,\n\t\t\t\t\t\thistory: this.history,\n\t\t\t\t\t}\n\t\t\t\t\tawait onAfterTask.call(this, result)\n\t\t\t\t\treturn result\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error: unknown) {\n\t\t\tconsole.error('Task failed', error)\n\t\t\tthis.#onDone(String(error), false)\n\t\t\tconst result: ExecutionResult = {\n\t\t\t\tsuccess: false,\n\t\t\t\tdata: String(error),\n\t\t\t\thistory: this.history,\n\t\t\t}\n\t\t\tawait onAfterTask.call(this, result)\n\t\t\treturn result\n\t\t}\n\t}\n\n\t/**\n\t * Merge all tools into a single MacroTool with the following input:\n\t * - thinking: string\n\t * - evaluation_previous_goal: string\n\t * - memory: string\n\t * - next_goal: string\n\t * - action: { toolName: toolInput }\n\t * where action must be selected from tools defined in this.tools\n\t */\n\t#packMacroTool(): Tool<MacroToolInput, MacroToolResult> {\n\t\tconst tools = this.tools\n\n\t\tconst actionSchemas = Array.from(tools.entries()).map(([toolName, tool]) => {\n\t\t\treturn zod.object({ [toolName]: tool.inputSchema }).describe(tool.description)\n\t\t})\n\n\t\tconst actionSchema = zod.union(\n\t\t\tactionSchemas as unknown as [zod.ZodType, zod.ZodType, ...zod.ZodType[]]\n\t\t)\n\n\t\tconst macroToolSchema = zod.object({\n\t\t\t// thinking: zod.string().optional(),\n\t\t\tevaluation_previous_goal: zod.string().optional(),\n\t\t\tmemory: zod.string().optional(),\n\t\t\tnext_goal: zod.string().optional(),\n\t\t\taction: actionSchema,\n\t\t})\n\n\t\treturn {\n\t\t\tdescription: 'You MUST call this tool every step. Outputs your reflections and next action.',\n\t\t\tinputSchema: macroToolSchema as zod.ZodType<MacroToolInput>,\n\t\t\texecute: async (input: MacroToolInput): Promise<MacroToolResult> => {\n\t\t\t\t// abort\n\t\t\t\tif (this.#abortController.signal.aborted) throw new Error('AbortError')\n\t\t\t\t// pause\n\t\t\t\tawait waitUntil(() => !this.paused)\n\n\t\t\t\tconsole.log(chalk.blue.bold('MacroTool execute'), input)\n\t\t\t\tconst action = input.action\n\n\t\t\t\tconst toolName = Object.keys(action)[0]\n\t\t\t\tconst toolInput = action[toolName]\n\t\t\t\tconst brain = trimLines(`β
: ${input.evaluation_previous_goal}\n\t\t\t\t\t\tπΎ: ${input.memory}\n\t\t\t\t\t\tπ―: ${input.next_goal}\n\t\t\t\t\t`)\n\n\t\t\t\tconsole.log(brain)\n\t\t\t\tthis.panel.update({ type: 'thinking', text: brain })\n\n\t\t\t\t// Find the corresponding tool\n\t\t\t\tconst tool = tools.get(toolName)\n\t\t\t\tassert(tool, `Tool ${toolName} not found. (@note should have been caught before this!!!)`)\n\n\t\t\t\tconsole.log(chalk.blue.bold(`Executing tool: ${toolName}`), toolInput)\n\t\t\t\tthis.panel.update({ type: 'toolExecuting', toolName, args: toolInput })\n\n\t\t\t\tconst startTime = Date.now()\n\n\t\t\t\t// Execute tool, bind `this` to PageAgent\n\t\t\t\tlet result = await tool.execute.bind(this)(toolInput)\n\n\t\t\t\tconst duration = Date.now() - startTime\n\t\t\t\tconsole.log(chalk.green.bold(`Tool (${toolName}) executed for ${duration}ms`), result)\n\n\t\t\t\tif (toolName === 'wait') {\n\t\t\t\t\tthis.#totalWaitTime += Math.round(toolInput.seconds + duration / 1000)\n\t\t\t\t\tresult += `\\n<sys> You have waited ${this.#totalWaitTime} seconds accumulatively.`\n\t\t\t\t\tif (this.#totalWaitTime >= 3)\n\t\t\t\t\t\tresult += '\\nDo NOT wait any longer unless you have a good reason.\\n'\n\t\t\t\t\tresult += '</sys>'\n\t\t\t\t} else {\n\t\t\t\t\t// For other tools, reset wait time\n\t\t\t\t\tthis.#totalWaitTime = 0\n\t\t\t\t}\n\n\t\t\t\t// Briefly display execution result\n\t\t\t\tthis.panel.update({\n\t\t\t\t\ttype: 'toolCompleted',\n\t\t\t\t\ttoolName,\n\t\t\t\t\targs: toolInput,\n\t\t\t\t\tresult,\n\t\t\t\t\tduration,\n\t\t\t\t})\n\n\t\t\t\t// Wait a moment to let user see the result\n\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, 100))\n\n\t\t\t\t// Return structured result\n\t\t\t\treturn {\n\t\t\t\t\tinput,\n\t\t\t\t\toutput: result,\n\t\t\t\t}\n\t\t\t},\n\t\t}\n\t}\n\n\t/**\n\t * Get system prompt, dynamically replace language settings based on configured language\n\t */\n\t#getSystemPrompt(): string {\n\t\tlet systemPrompt = SYSTEM_PROMPT\n\n\t\tconst targetLanguage = this.config.language === 'zh-CN' ? 'δΈζ' : 'English'\n\t\tsystemPrompt = systemPrompt.replace(\n\t\t\t/Default working language: \\*\\*.*?\\*\\*/,\n\t\t\t`Default working language: **${targetLanguage}**`\n\t\t)\n\n\t\treturn systemPrompt\n\t}\n\n\t/**\n\t * Get instructions from config and format as XML block\n\t */\n\tasync #getInstructions(): Promise<string> {\n\t\tconst { instructions } = this.config\n\t\tif (!instructions) return ''\n\n\t\tconst systemInstructions = instructions.system?.trim()\n\t\tconst url = await this.pageController.getCurrentUrl()\n\t\tlet pageInstructions: string | undefined\n\n\t\tif (instructions.getPageInstructions) {\n\t\t\ttry {\n\t\t\t\tpageInstructions = instructions.getPageInstructions(url)?.trim()\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(\n\t\t\t\t\tchalk.red('[PageAgent] Failed to execute getPageInstructions callback:'),\n\t\t\t\t\terror\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\t\tif (!systemInstructions && !pageInstructions) return ''\n\n\t\tlet result = '<instructions>\\n'\n\n\t\tif (systemInstructions) {\n\t\t\tresult += `<system_instructions>\\n${systemInstructions}\\n</system_instructions>\\n`\n\t\t}\n\n\t\tif (pageInstructions) {\n\t\t\tresult += `<page_instructions>\\n${pageInstructions}\\n</page_instructions>\\n`\n\t\t}\n\n\t\tresult += '</instructions>\\n\\n'\n\n\t\treturn result\n\t}\n\n\tasync #assembleUserPrompt(): Promise<string> {\n\t\tlet prompt = ''\n\n\t\t// <instructions> (optional)\n\t\tprompt += await this.#getInstructions()\n\n\t\t// <agent_state>\n\t\t// - <user_request>\n\t\t// - <step_info>\n\t\t// <agent_state>\n\n\t\tprompt += `<agent_state>\n\t\t\t<user_request>\n\t\t\t${this.task}\n\t\t\t</user_request>\n\t\t\t<step_info>\n\t\t\tStep ${this.history.length + 1} of ${MAX_STEPS} max possible steps\n\t\t\tCurrent date and time: ${new Date().toISOString()}\n\t\t\t</step_info>\n\t\t\t</agent_state>\n\t\t`\n\n\t\t// <agent_history>\n\t\t// - <step_>\n\n\t\tprompt += '\\n<agent_history>\\n'\n\n\t\tthis.history.forEach((history, index) => {\n\t\t\tprompt += `<step_${index + 1}>\n\t\t\t\tEvaluation of Previous Step: ${history.brain.evaluation_previous_goal}\n\t\t\t\tMemory: ${history.brain.memory}\n\t\t\t\tNext Goal: ${history.brain.next_goal}\n\t\t\t\tAction Results: ${history.action.output}\n\t\t\t\t</step_${index + 1}>\n\t\t\t`\n\t\t})\n\n\t\tprompt += '</agent_history>\\n\\n'\n\n\t\t// <browser_state>\n\n\t\tprompt += await this.#getBrowserState()\n\n\t\treturn trimLines(prompt)\n\t}\n\n\t#onDone(text: string, success = true) {\n\t\tthis.pageController.cleanUpHighlights()\n\n\t\t// Update panel status\n\t\tif (success) {\n\t\t\tthis.panel.update({ type: 'output', text })\n\t\t} else {\n\t\t\tthis.panel.update({ type: 'error', message: text })\n\t\t}\n\n\t\t// Task completed\n\t\tthis.panel.update({ type: 'completed' })\n\n\t\tthis.pageController.hideMask()\n\n\t\tthis.#abortController.abort()\n\t}\n\n\tasync #getBrowserState(): Promise<string> {\n\t\tconst pageUrl = await this.pageController.getCurrentUrl()\n\t\tconst pageTitle = await this.pageController.getPageTitle()\n\t\tconst pi = await this.pageController.getPageInfo()\n\t\tconst viewportExpansion = await this.pageController.getViewportExpansion()\n\n\t\tawait this.pageController.updateTree()\n\n\t\tlet simplifiedHTML = await this.pageController.getSimplifiedHTML()\n\n\t\tif (this.config.transformPageContent) {\n\t\t\tsimplifiedHTML = await this.config.transformPageContent(simplifiedHTML)\n\t\t}\n\n\t\tlet prompt = trimLines(`<browser_state>\n\t\t\tCurrent Page: [${pageTitle}](${pageUrl})\n\n\t\t\tPage info: ${pi.viewport_width}x${pi.viewport_height}px viewport, ${pi.page_width}x${pi.page_height}px total page size, ${pi.pages_above.toFixed(1)} pages above, ${pi.pages_below.toFixed(1)} pages below, ${pi.total_pages.toFixed(1)} total pages, at ${(pi.current_page_position * 100).toFixed(0)}% of page\n\n\t\t\t${viewportExpansion === -1 ? 'Interactive elements from top layer of the current page (full page):' : 'Interactive elements from top layer of the current page inside the viewport:'}\n\n\t\t`)\n\n\t\t// Page header info\n\t\tconst has_content_above = pi.pixels_above > 4\n\t\tif (has_content_above && viewportExpansion !== -1) {\n\t\t\tprompt += `... ${pi.pixels_above} pixels above (${pi.pages_above.toFixed(1)} pages) - scroll to see more ...\\n`\n\t\t} else {\n\t\t\tprompt += `[Start of page]\\n`\n\t\t}\n\n\t\t// Current viewport info\n\t\tprompt += simplifiedHTML\n\t\tprompt += `\\n`\n\n\t\t// Page footer info\n\t\tconst has_content_below = pi.pixels_below > 4\n\t\tif (has_content_below && viewportExpansion !== -1) {\n\t\t\tprompt += `... ${pi.pixels_below} pixels below (${pi.pages_below.toFixed(1)} pages) - scroll to see more ...\\n`\n\t\t} else {\n\t\t\tprompt += `[End of page]\\n`\n\t\t}\n\n\t\tprompt += `</browser_state>\\n`\n\n\t\treturn prompt\n\t}\n\n\tdispose(reason?: string) {\n\t\tconsole.log('Disposing PageAgent...')\n\t\tthis.disposed = true\n\t\tthis.pageController.dispose()\n\t\tthis.panel.dispose()\n\t\tthis.history = []\n\t\tthis.#abortController.abort(reason ?? 'PageAgent disposed')\n\n\t\t// Clean up LLM event listeners\n\t\tif (this.#llmRetryListener) {\n\t\t\tthis.#llm.removeEventListener('retry', this.#llmRetryListener)\n\t\t\tthis.#llmRetryListener = null\n\t\t}\n\t\tif (this.#llmErrorListener) {\n\t\t\tthis.#llm.removeEventListener('error', this.#llmErrorListener)\n\t\t\tthis.#llmErrorListener = null\n\t\t}\n\n\t\t// Clean up window event listeners\n\t\tif (this.#beforeUnloadListener) {\n\t\t\twindow.removeEventListener('beforeunload', this.#beforeUnloadListener)\n\t\t\tthis.#beforeUnloadListener = null\n\t\t}\n\n\t\tthis.config.onDispose?.call(this, reason)\n\t}\n}\n"],"names":["tool","result","tools"],"mappings":";;;;;;;;;;;;;;;;;;AACO,MAAM,YAAY;ACDzB,MAAA,gBAAe;ACaR,SAAS,kBAAkB,UAAoB;AACrD,MAAI,oBAAoB;AAExB,QAAM,SAAU,SAAoC,UAAU,CAAC;AAC/D,MAAI,CAAC,OAAQ,OAAM,IAAI,MAAM,wBAAwB;AAErD,QAAM,UAAU,OAAO;AACvB,MAAI,CAAC,QAAS,OAAM,IAAI,MAAM,sBAAsB;AAEpD,QAAM,WAAW,QAAQ,aAAa,CAAC;AAIvC,MAAI,UAAU,UAAU,WAAW;AAClC,wBAAoB,cAAc,SAAS,SAAS,SAAS;AAG7D,QAAI,SAAS,SAAS,QAAQ,SAAS,SAAS,SAAS,eAAe;AACvE,cAAQ,IAAI,MAAM,OAAO,0CAA0C,CAAC;AACpE,0BAAoB,EAAE,QAAQ,cAAc,iBAAiB,EAAA;AAAA,IAC9D;AAAA,EACD,OAAO;AAEN,QAAI,QAAQ,SAAS;AACpB,YAAM,UAAU,QAAQ,QAAQ,KAAA;AAChC,YAAM,gBAAgB,uBAAuB,OAAO;AACpD,UAAI,eAAe;AAClB,4BAAoB,cAAc,aAAa;AAG/C,YAAI,mBAAmB,SAAS,eAAe;AAC9C,kBAAQ,IAAI,MAAM,O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