page-agent 0.0.18 β†’ 0.0.20

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 CHANGED
@@ -8,7 +8,7 @@ The GUI Agent Living in Your Webpage. Control web interfaces with natural langua
8
8
 
9
9
  🌐 **English** | [δΈ­ζ–‡](./README-zh.md)
10
10
 
11
- πŸ‘‰ <a href="https://alibaba.github.io/page-agent/" target="_blank"><b>πŸš€ Demo</b></a> | <a href="https://alibaba.github.io/page-agent/#/docs/introduction/overview" target="_blank"><b>πŸ“– Documentation</b></a>
11
+ πŸ‘‰ <a href="https://alibaba.github.io/page-agent/" target="_blank"><b>πŸš€ Demo</b></a> | <a href="https://alibaba.github.io/page-agent/#/docs/introduction/overview" target="_blank"><b>πŸ“– Documentation</b></a>
12
12
 
13
13
  <video id="demo-video" src="https://github.com/user-attachments/assets/de8d1964-8bde-494f-a52f-2975469557a5" width="640" crossorigin muted autoplay loop></video>
14
14
 
@@ -24,22 +24,10 @@ The GUI Agent Living in Your Webpage. Control web interfaces with natural langua
24
24
 
25
25
  ## πŸ—ΊοΈ Roadmap
26
26
 
27
- πŸ‘‰ [**Roadmap**](./ROADMAP.md)
27
+ πŸ‘‰ [**Roadmap**](https://github.com/alibaba/page-agent/issues/96)
28
28
 
29
29
  ## πŸš€ Quick Start
30
30
 
31
- ### CDN Integration
32
-
33
- ```html
34
- // CDN - https://cdn.jsdelivr.net/npm/page-agent@latest/dist/umd/page-agent.js
35
- // Mirror(CN) - https://registry.npmmirror.com/page-agent/latest/files/dist/umd/page-agent.js
36
- <script
37
- src="https://cdn.jsdelivr.net/npm/page-agent@latest/dist/umd/page-agent.js"
38
- crossorigin="true"
39
- type="text/javascript"
40
- ></script>
41
- ```
42
-
43
31
  ### NPM Installation
44
32
 
45
33
  ```bash
@@ -57,7 +45,7 @@ const DEMO_BASE_URL = 'https://hwcxiuzfylggtcktqgij.supabase.co/functions/v1/llm
57
45
  const DEMO_API_KEY = 'PAGE-AGENT-FREE-TESTING-RANDOM'
58
46
 
59
47
  const agent = new PageAgent({
60
- modelName: DEMO_MODEL,
48
+ model: DEMO_MODEL,
61
49
  baseURL: DEMO_BASE_URL,
62
50
  apiKey: DEMO_API_KEY,
63
51
  language: 'en-US',
@@ -66,6 +54,23 @@ const agent = new PageAgent({
66
54
  await agent.execute('Click the login button')
67
55
  ```
68
56
 
57
+ ### CDN Integration
58
+
59
+ Fastest way to try PageAgent is to include it via CDN. Demo model will be used by default.
60
+
61
+ | Location | URL |
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 |
65
+
66
+ ```html
67
+ <script
68
+ src="https://cdn.jsdelivr.net/npm/page-agent@latest/dist/umd/page-agent.js"
69
+ crossorigin="true"
70
+ type="text/javascript"
71
+ ></script>
72
+ ```
73
+
69
74
  ## πŸ—οΈ Structure
70
75
 
71
76
  PageAgent adopts a simplified monorepo structure:
@@ -43,6 +43,22 @@ declare interface AgentConfig {
43
43
  * }
44
44
  */
45
45
  customTools?: Record<string, PageAgentTool | null>;
46
+ /**
47
+ * Instructions to guide the agent's behavior
48
+ */
49
+ instructions?: {
50
+ /**
51
+ * Global system-level instructions, applied to all tasks
52
+ */
53
+ system?: string;
54
+ /**
55
+ * Dynamic page-level instructions callback
56
+ * Called before each step to get instructions for the current page
57
+ * @param url - Current page URL (window.location.href)
58
+ * @returns Instructions string, or undefined/null to skip
59
+ */
60
+ getPageInstructions?: (url: string) => string | undefined | null;
61
+ };
46
62
  onBeforeStep?: (this: PageAgent, stepCnt: number) => Promise<void> | void;
47
63
  onAfterStep?: (this: PageAgent, stepCnt: number, history: AgentHistory[]) => Promise<void> | void;
48
64
  onBeforeTask?: (this: PageAgent) => Promise<void> | void;
@@ -50,6 +66,7 @@ declare interface AgentConfig {
50
66
  /**
51
67
  * @note this hook can block the disposal process
52
68
  * @note when dispose caused by page unload, reason will be 'PAGE_UNLOADING'. this method CANNOT block unloading. async operations may be cut.
69
+ * @todo remove `this` binding, pass agent as explicit parameter instead
53
70
  */
54
71
  onDispose?: (this: PageAgent, reason?: string) => void;
55
72
  /**
@@ -59,10 +76,26 @@ declare interface AgentConfig {
59
76
  * @note May bypass some safe guards and data-masking mechanisms.
60
77
  */
61
78
  experimentalScriptExecutionTool?: boolean;
79
+ /**
80
+ * Transform page content before sending to LLM.
81
+ * Called after DOM extraction and simplification, before LLM invocation.
82
+ * Use cases: inspect extraction results, modify page info, mask sensitive data.
83
+ *
84
+ * @param content - Simplified page content that will be sent to LLM
85
+ * @returns Transformed content
86
+ *
87
+ * @example
88
+ * // Mask phone numbers
89
+ * transformPageContent: async (content) => {
90
+ * return content.replace(/1[3-9]\d{9}/g, '***********')
91
+ * }
92
+ */
93
+ transformPageContent?: (content: string) => Promise<string> | string;
62
94
  /**
63
95
  * TODO: @unimplemented
64
96
  * hook when action causes a new page to be opened
65
97
  * @note PageAgent will try to detect new pages and decide if it's caused by an action. But not very reliable.
98
+ * @todo remove `this` binding, pass agent as explicit parameter instead
66
99
  */
67
100
  onNewPageOpen?: (this: PageAgent, url: string) => Promise<void> | void;
68
101
  /**
@@ -10,7 +10,7 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
10
10
  var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
11
11
  var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
12
12
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
13
- var _llm, _totalWaitTime, _abortController, _llmRetryListener, _llmErrorListener, _beforeUnloadListener, _PageAgent_instances, packMacroTool_fn, getSystemPrompt_fn, assembleUserPrompt_fn, onDone_fn, getBrowserState_fn;
13
+ var _llm, _totalWaitTime, _abortController, _llmRetryListener, _llmErrorListener, _beforeUnloadListener, _PageAgent_instances, packMacroTool_fn, getSystemPrompt_fn, getInstructions_fn, assembleUserPrompt_fn, onDone_fn, getBrowserState_fn;
14
14
  import { LLM } from "@page-agent/llms";
15
15
  import { PageController } from "@page-agent/page-controller";
16
16
  import { SimulatorMask, Panel } from "@page-agent/ui";
@@ -494,19 +494,42 @@ getSystemPrompt_fn = /* @__PURE__ */ __name(function() {
494
494
  );
495
495
  return systemPrompt;
496
496
  }, "#getSystemPrompt");
497
+ getInstructions_fn = /* @__PURE__ */ __name(async function() {
498
+ const { instructions } = this.config;
499
+ if (!instructions) return "";
500
+ const systemInstructions = instructions.system?.trim();
501
+ const url = await this.pageController.getCurrentUrl();
502
+ let pageInstructions;
503
+ if (instructions.getPageInstructions) {
504
+ try {
505
+ pageInstructions = instructions.getPageInstructions(url)?.trim();
506
+ } catch (error) {
507
+ console.error(
508
+ chalk.red("[PageAgent] Failed to execute getPageInstructions callback:"),
509
+ error
510
+ );
511
+ }
512
+ }
513
+ if (!systemInstructions && !pageInstructions) return "";
514
+ let result = "<instructions>\n";
515
+ if (systemInstructions) {
516
+ result += `<system_instructions>
517
+ ${systemInstructions}
518
+ </system_instructions>
519
+ `;
520
+ }
521
+ if (pageInstructions) {
522
+ result += `<page_instructions>
523
+ ${pageInstructions}
524
+ </page_instructions>
525
+ `;
526
+ }
527
+ result += "</instructions>\n\n";
528
+ return result;
529
+ }, "#getInstructions");
497
530
  assembleUserPrompt_fn = /* @__PURE__ */ __name(async function() {
498
531
  let prompt = "";
499
- prompt += "<agent_history>\n";
500
- this.history.forEach((history, index) => {
501
- prompt += `<step_${index + 1}>
502
- Evaluation of Previous Step: ${history.brain.evaluation_previous_goal}
503
- Memory: ${history.brain.memory}
504
- Next Goal: ${history.brain.next_goal}
505
- Action Results: ${history.action.output}
506
- </step_${index + 1}>
507
- `;
508
- });
509
- prompt += "</agent_history>\n\n";
532
+ prompt += await __privateMethod(this, _PageAgent_instances, getInstructions_fn).call(this);
510
533
  prompt += `<agent_state>
511
534
  <user_request>
512
535
  ${this.task}
@@ -517,6 +540,17 @@ assembleUserPrompt_fn = /* @__PURE__ */ __name(async function() {
517
540
  </step_info>
518
541
  </agent_state>
519
542
  `;
543
+ prompt += "\n<agent_history>\n";
544
+ this.history.forEach((history, index) => {
545
+ prompt += `<step_${index + 1}>
546
+ Evaluation of Previous Step: ${history.brain.evaluation_previous_goal}
547
+ Memory: ${history.brain.memory}
548
+ Next Goal: ${history.brain.next_goal}
549
+ Action Results: ${history.action.output}
550
+ </step_${index + 1}>
551
+ `;
552
+ });
553
+ prompt += "</agent_history>\n\n";
520
554
  prompt += await __privateMethod(this, _PageAgent_instances, getBrowserState_fn).call(this);
521
555
  return trimLines(prompt);
522
556
  }, "#assembleUserPrompt");
@@ -539,7 +573,10 @@ getBrowserState_fn = /* @__PURE__ */ __name(async function() {
539
573
  this.mask.wrapper.style.pointerEvents = "none";
540
574
  await this.pageController.updateTree();
541
575
  this.mask.wrapper.style.pointerEvents = "auto";
542
- const simplifiedHTML = await this.pageController.getSimplifiedHTML();
576
+ let simplifiedHTML = await this.pageController.getSimplifiedHTML();
577
+ if (this.config.transformPageContent) {
578
+ simplifiedHTML = await this.config.transformPageContent(simplifiedHTML);
579
+ }
543
580
  let prompt = trimLines(`<browser_state>
544
581
  Current Page: [${pageTitle}](${pageUrl})
545
582
 
@@ -1 +1 @@
1
- {"version":3,"file":"page-agent.js","sources":["../../src/config/constants.ts","../../src/prompts/system_prompt.md?raw","../../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\"","/**\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 {\n\ttype AgentBrain,\n\tLLM,\n\ttype MacroToolInput,\n\ttype MacroToolResult,\n\ttype Tool,\n} from '@page-agent/llms'\nimport { PageController } from '@page-agent/page-controller'\nimport { Panel, SimulatorMask } 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 { trimLines, uid, waitUntil } from './utils'\nimport { assert } from './utils/assert'\n\nexport type { PageAgentConfig }\nexport { tool, type PageAgentTool } from './tools'\nexport type { AgentBrain, MacroToolInput, MacroToolResult }\n\nexport interface AgentHistory {\n\tbrain: AgentBrain\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/** Fullscreen mask */\n\tmask = new SimulatorMask()\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\n\t\tthis.pageController = new PageController(this.config)\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\t/**\n\t * @todo maybe return something?\n\t */\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.mask.show()\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)\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\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\tasync #assembleUserPrompt(): Promise<string> {\n\t\tlet prompt = ''\n\n\t\t// <agent_history>\n\t\t// - <step_>\n\n\t\tprompt += '<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// <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// <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.mask.hide()\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\tthis.mask.wrapper.style.pointerEvents = 'none'\n\t\tawait this.pageController.updateTree()\n\t\tthis.mask.wrapper.style.pointerEvents = 'auto'\n\n\t\tconst simplifiedHTML = await this.pageController.getSimplifiedHTML()\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.mask.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;ACKf,eAAsB,UAAU,OAAsB,UAAU,KAAK,OAA2B;AAC/F,MAAI,MAAA,EAAS,QAAO;AAEpB,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACvC,UAAM,QAAQ,KAAK,IAAA;AACnB,UAAM,WAAW,YAAY,MAAM;AAClC,UAAI,SAAS;AACZ,sBAAc,QAAQ;AACtB,gBAAQ,IAAI;AAAA,MACb,WAAW,KAAK,IAAA,IAAQ,QAAQ,SAAS;AACxC,sBAAc,QAAQ;AACtB,eAAO,IAAI,MAAM,8CAA8C,CAA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AAkB,GAAG,YAAY,QAAQ,CAAC,CAAC;AAAA;AAAA,EAC5E,OAAO;AACN,cAAU;AAAA;AAAA,EACX;AAEA,YAAU;AAAA;AAEV,SAAO;AACR,GA5CM;AAvXoC;AAApC,IAAM,YAAN;"}
1
+ {"version":3,"file":"page-agent.js","sources":["../../src/config/constants.ts","../../src/prompts/system_prompt.md?raw","../../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\"","/**\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 {\n\ttype AgentBrain,\n\tLLM,\n\ttype MacroToolInput,\n\ttype MacroToolResult,\n\ttype Tool,\n} from '@page-agent/llms'\nimport { PageController } from '@page-agent/page-controller'\nimport { Panel, SimulatorMask } 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 { trimLines, uid, waitUntil } from './utils'\nimport { assert } from './utils/assert'\n\nexport type { PageAgentConfig }\nexport { tool, type PageAgentTool } from './tools'\nexport type { AgentBrain, MacroToolInput, MacroToolResult }\n\nexport interface AgentHistory {\n\tbrain: AgentBrain\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/** Fullscreen mask */\n\tmask = new SimulatorMask()\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\n\t\tthis.pageController = new PageController(this.config)\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\t/**\n\t * @todo maybe return something?\n\t */\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.mask.show()\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)\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\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.mask.hide()\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\tthis.mask.wrapper.style.pointerEvents = 'none'\n\t\tawait this.pageController.updateTree()\n\t\tthis.mask.wrapper.style.pointerEvents = 'auto'\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.mask.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;ACKf,eAAsB,UAAU,OAAsB,UAAU,KAAK,OAA2B;AAC/F,MAAI,MAAA,EAAS,QAAO;AAEpB,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACvC,UAAM,QAAQ,KAAK,IAAA;AACnB,UAAM,WAAW,YAAY,MAAM;AAClC,UAAI,SAAS;AACZ,sBAAc,QAAQ;AACtB,gBAAQ,IAAI;AAAA,MACb,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