page-agent 1.5.1 β†’ 1.5.2

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
@@ -13,7 +13,7 @@ The GUI Agent Living in Your Webpage. Control web interfaces with natural langua
13
13
 
14
14
  πŸ‘‰ <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>
15
15
 
16
- <video id="demo-video" src="https://github.com/user-attachments/assets/11aed429-b69c-49d5-8982-fa99b4a0e9a8" controls crossorigin muted></video>
16
+ <video id="demo-video" src="https://github.com/user-attachments/assets/a1f2eae2-13fb-4aae-98cf-a3fc1620a6c2" controls crossorigin muted></video>
17
17
 
18
18
  ---
19
19
 
@@ -49,8 +49,8 @@ Fastest way to try PageAgent with our free Demo LLM:
49
49
 
50
50
  | Mirrors | URL |
51
51
  | ------- | ---------------------------------------------------------------------------------- |
52
- | Global | https://cdn.jsdelivr.net/npm/page-agent@1.5.1/dist/iife/page-agent.demo.js |
53
- | China | https://registry.npmmirror.com/page-agent/1.5.1/files/dist/iife/page-agent.demo.js |
52
+ | Global | https://cdn.jsdelivr.net/npm/page-agent@1.5.2/dist/iife/page-agent.demo.js |
53
+ | China | https://registry.npmmirror.com/page-agent/1.5.2/files/dist/iife/page-agent.demo.js |
54
54
 
55
55
  > **⚠️ For technical evaluation only.** This demo CDN uses our free [testing LLM API](https://alibaba.github.io/page-agent/docs/features/models#free-testing-api). By using it, you agree to its [terms](https://github.com/alibaba/page-agent/blob/main/docs/terms-and-privacy.md).
56
56
 
@@ -195,7 +195,7 @@ Here are examples of good output patterns. Use them as reference but never copy
195
195
  }
196
196
  }
197
197
  </output>
198
- `,log=console.log.bind(console,chalk.yellow("[autoFixer]"));function normalizeResponse(e,t){let o=null;const n=e.choices?.[0];if(!n)throw new Error("No choices in response");const r=n.message;if(!r)throw new Error("No message in choice");const i=r.tool_calls?.[0];if(i?.function?.arguments)o=safeJsonParse(i.function.arguments),i.function.name&&i.function.name!=="AgentOutput"&&(log("#1: fixing tool_call"),o={action:safeJsonParse(o)});else if(r.content){const s=r.content.trim(),c=retrieveJsonFromString(s);if(c)o=safeJsonParse(c),o?.name==="AgentOutput"&&(log("#2: fixing tool_call"),o=safeJsonParse(o.arguments)),o?.type==="function"&&(log("#3: fixing tool_call"),o=safeJsonParse(o.function.arguments)),!o?.action&&!o?.evaluation_previous_goal&&!o?.memory&&!o?.next_goal&&!o?.thinking&&(log("#4: fixing tool_call"),o={action:safeJsonParse(o)});else throw new Error("No tool_call and the message content does not contain valid JSON")}else throw new Error("No tool_call nor message content is present");return o=safeJsonParse(o),o.action&&(o.action=safeJsonParse(o.action)),o.action&&t&&(o.action=validateAction(o.action,t)),o.action||(log("#5: fixing tool_call"),o.action={name:"wait",input:{seconds:1}}),{...e,choices:[{...n,message:{...r,tool_calls:[{...i||{},function:{...i?.function||{},name:"AgentOutput",arguments:JSON.stringify(o)}}]}}]}}a(normalizeResponse,"normalizeResponse");function validateAction(e,t){if(typeof e!="object"||e===null)return e;const o=Object.keys(e)[0];if(!o)return e;const n=t.get(o);if(!n){const c=Array.from(t.keys()).join(", ");throw new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,`Unknown action "${o}". Available: ${c}`)}let r=e[o];const i=n.inputSchema;if(i instanceof ZodObject&&r!==null&&typeof r!="object"){const c=Object.keys(i.shape).find(l=>!i.shape[l].safeParse(void 0).success);c&&(log(`coercing primitive action input for "${o}"`),r={[c]:r})}const s=i.safeParse(r);if(!s.success)throw new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,`Invalid input for action "${o}": ${prettifyError(s.error)}`);return{[o]:s.data}}a(validateAction,"validateAction");function safeJsonParse(e){if(typeof e=="string")try{return JSON.parse(e.trim())}catch{return e}return e}a(safeJsonParse,"safeJsonParse");function retrieveJsonFromString(e){try{const t=/({[\s\S]*})/.exec(e)??[];return t.length===0?null:JSON.parse(t[0])}catch{return null}}a(retrieveJsonFromString,"retrieveJsonFromString");async function waitFor$1(e){await new Promise(t=>setTimeout(t,e*1e3))}a(waitFor$1,"waitFor$1");function truncate$1(e,t){return e.length>t?e.substring(0,t)+"...":e}a(truncate$1,"truncate$1");function randomID(e){let t=Math.random().toString(36).substring(2,11);if(!e)return t;const o=1e3;let n=0;for(;e.includes(t);)if(t=Math.random().toString(36).substring(2,11),n++,n>o)throw new Error("randomID: too many try");return t}a(randomID,"randomID");const _global=globalThis;_global.__PAGE_AGENT_IDS__||(_global.__PAGE_AGENT_IDS__=[]);const ids=_global.__PAGE_AGENT_IDS__;function uid(){const e=randomID(ids);return ids.push(e),e}a(uid,"uid");const llmsTxtCache=new Map;async function fetchLlmsTxt(e){const t=new URL(e).origin;if(llmsTxtCache.has(t))return llmsTxtCache.get(t);const o=`${t}/llms.txt`;let n=null;try{console.log(chalk.gray(`[llms.txt] Fetching ${o}`));const r=await fetch(o,{signal:AbortSignal.timeout(3e3)});r.ok?(n=await r.text(),console.log(chalk.green(`[llms.txt] Found (${n.length} chars)`)),n.length>1e3&&(console.log(chalk.yellow("[llms.txt] Truncating to 1000 chars")),n=truncate$1(n,1e3))):console.debug(chalk.gray(`[llms.txt] ${r.status} for ${o}`))}catch(r){console.debug(chalk.gray(`[llms.txt] not found for ${o}`),r)}return llmsTxtCache.set(t,n),n}a(fetchLlmsTxt,"fetchLlmsTxt");function assert(e,t,o){if(!e){const n=t??"Assertion failed";throw console.error(chalk.red(`❌ assert: ${n}`)),new Error(n)}}a(assert,"assert");function tool(e){return e}a(tool,"tool");const tools=new Map;tools.set("done",{description:"Complete task. Text is your final response to the user β€” keep it concise unless the user explicitly asks for detail.",inputSchema:object({text:string(),success:boolean().default(!0)}),execute:a(async function(e){return Promise.resolve("Task completed")},"execute")}),tools.set("wait",{description:"Wait for x seconds. Can be used to wait until the page or data is fully loaded.",inputSchema:object({seconds:number().min(1).max(10).default(1)}),execute:a(async function(e){const t=await this.pageController.getLastUpdateTime(),o=Math.max(0,e.seconds-(Date.now()-t)/1e3);return console.log(`actualWaitTime: ${o} seconds`),await waitFor$1(o),`βœ… Waited for ${e.seconds} seconds.`},"execute")}),tools.set("ask_user",{description:"Ask the user a question and wait for their answer. Use this if you need more information or clarification.",inputSchema:object({question:string()}),execute:a(async function(e){if(!this.onAskUser)throw new Error("ask_user tool requires onAskUser callback to be set");return`User answered: ${await this.onAskUser(e.question)}`},"execute")}),tools.set("click_element_by_index",{description:"Click element by index",inputSchema:object({index:int().min(0)}),execute:a(async function(e){return(await this.pageController.clickElement(e.index)).message},"execute")}),tools.set("input_text",{description:"Click and type text into an interactive input element",inputSchema:object({index:int().min(0),text:string()}),execute:a(async function(e){return(await this.pageController.inputText(e.index,e.text)).message},"execute")}),tools.set("select_dropdown_option",{description:"Select dropdown option for interactive element index by the text of the option you want to select",inputSchema:object({index:int().min(0),text:string()}),execute:a(async function(e){return(await this.pageController.selectOption(e.index,e.text)).message},"execute")}),tools.set("scroll",{description:"Scroll the page vertically. Use index for scroll elements (dropdowns/custom UI).",inputSchema:object({down:boolean().default(!0),num_pages:number().min(0).max(10).optional().default(.1),pixels:number().int().min(0).optional(),index:number().int().min(0).optional()}),execute:a(async function(e){return(await this.pageController.scroll({...e,numPages:e.num_pages})).message},"execute")}),tools.set("scroll_horizontally",{description:"Scroll the page horizontally, or within a specific element by index. Useful for wide tables.",inputSchema:object({right:boolean().default(!0),pixels:number().int().min(0),index:number().int().min(0).optional()}),execute:a(async function(e){return(await this.pageController.scrollHorizontally(e)).message},"execute")}),tools.set("execute_javascript",{description:"Execute JavaScript code on the current page. Supports async/await syntax. Use with caution!",inputSchema:object({script:string()}),execute:a(async function(e){return(await this.pageController.executeJavascript(e.script)).message},"execute")});const ut=class ut extends EventTarget{constructor(o){super();Z(this,R);ne(this,"id",uid());ne(this,"config");ne(this,"tools");ne(this,"pageController");ne(this,"task","");ne(this,"taskId","");ne(this,"history",[]);ne(this,"disposed",!1);ne(this,"onAskUser");Z(this,Ue,"idle");Z(this,Oe);Z(this,ie,new AbortController);Z(this,$e,[]);Z(this,Q,{totalWaitTime:0,lastURL:"",browserState:null});if(this.config={...o,maxSteps:o.maxSteps||40},S(this,Oe,new LLM(this.config)),this.tools=new Map(tools),this.pageController=o.pageController,p(this,Oe).addEventListener("retry",n=>{const{attempt:r,maxAttempts:i}=n.detail;b(this,R,Me).call(this,{type:"retrying",attempt:r,maxAttempts:i}),this.history.push({type:"retry",message:`LLM retry attempt ${r} of ${i}`,attempt:r,maxAttempts:i}),b(this,R,Pe).call(this)}),p(this,Oe).addEventListener("error",n=>{const r=n.detail.error;if(r?.rawError?.name==="AbortError")return;const i=String(r);b(this,R,Me).call(this,{type:"error",message:i}),this.history.push({type:"error",message:i,rawResponse:r.rawResponse}),b(this,R,Pe).call(this)}),this.config.customTools)for(const[n,r]of Object.entries(this.config.customTools)){if(r===null){this.tools.delete(n);continue}this.tools.set(n,r)}this.config.experimentalScriptExecutionTool||this.tools.delete("execute_javascript")}get status(){return p(this,Ue)}pushObservation(o){p(this,$e).push(o)}stop(){this.pageController.cleanUpHighlights(),this.pageController.hideMask(),p(this,ie).abort()}async execute(o){if(this.disposed)throw new Error("PageAgent has been disposed. Create a new instance.");if(!o)throw new Error("Task is required");this.task=o,this.taskId=uid(),this.onAskUser||this.tools.delete("ask_user");const n=this.config.onBeforeStep,r=this.config.onAfterStep,i=this.config.onBeforeTask,s=this.config.onAfterTask;await i?.(this),await this.pageController.showMask(),p(this,ie)&&(p(this,ie).abort(),S(this,ie,new AbortController)),this.history=[],b(this,R,_t).call(this,"running"),b(this,R,Pe).call(this),S(this,$e,[]),S(this,Q,{totalWaitTime:0,lastURL:"",browserState:null});let c=0;for(;;){try{console.group(`step: ${c}`),await n?.(this,c),console.log(chalk.blue.bold("πŸ‘€ Observing...")),p(this,Q).browserState=await this.pageController.getBrowserState(),await b(this,R,At).call(this,c);const l=[{role:"system",content:b(this,R,It).call(this)},{role:"user",content:await b(this,R,Pt).call(this)}],d={AgentOutput:b(this,R,Tt).call(this)};console.log(chalk.blue.bold("🧠 Thinking...")),b(this,R,Me).call(this,{type:"thinking"});const h=await p(this,Oe).invoke(l,d,p(this,ie).signal,{toolChoiceName:"AgentOutput",normalizeResponse:a(C=>normalizeResponse(C,this.tools),"normalizeResponse")}),f=h.toolResult,g=f.input,m=f.output,T={evaluation_previous_goal:g.evaluation_previous_goal,memory:g.memory,next_goal:g.next_goal},I=Object.keys(g.action)[0],A={name:I,input:g.action[I],output:m};if(this.history.push({type:"step",stepIndex:c,reflection:T,action:A,usage:h.usage,rawResponse:h.rawResponse,rawRequest:h.rawRequest}),b(this,R,Pe).call(this),await r?.(this,this.history),console.groupEnd(),I==="done"){const C=A.input?.success??!1,D=A.input?.text||"no text provided";console.log(chalk.green.bold("Task completed"),C,D),b(this,R,st).call(this,C);const k={success:C,data:D,history:this.history};return await s?.(this,k),k}}catch(l){console.groupEnd();const d=l?.rawError?.name==="AbortError";console.error("Task failed",l);const h=d?"Task stopped":String(l);b(this,R,Me).call(this,{type:"error",message:h}),this.history.push({type:"error",message:h,rawResponse:l}),b(this,R,Pe).call(this),b(this,R,st).call(this,!1);const f={success:!1,data:h,history:this.history};return await s?.(this,f),f}if(c++,c>this.config.maxSteps){const l="Step count exceeded maximum limit";this.history.push({type:"error",message:l}),b(this,R,Pe).call(this),b(this,R,st).call(this,!1);const d={success:!1,data:l,history:this.history};return await s?.(this,d),d}await waitFor$1(.4)}}dispose(){console.log("Disposing PageAgent..."),this.disposed=!0,this.pageController.dispose(),p(this,ie).abort(),this.dispatchEvent(new Event("dispose")),this.config.onDispose?.(this)}};Ue=new WeakMap,Oe=new WeakMap,ie=new WeakMap,$e=new WeakMap,Q=new WeakMap,R=new WeakSet,St=a(function(){this.dispatchEvent(new Event("statuschange"))},"#emitStatusChange"),Pe=a(function(){this.dispatchEvent(new Event("historychange"))},"#emitHistoryChange"),Me=a(function(o){this.dispatchEvent(new CustomEvent("activity",{detail:o}))},"#emitActivity"),_t=a(function(o){p(this,Ue)!==o&&(S(this,Ue,o),b(this,R,St).call(this))},"#setStatus"),Tt=a(function(){const o=this.tools,n=Array.from(o.entries()).map(([s,c])=>object({[s]:c.inputSchema}).describe(c.description)),r=union(n);return{description:"You MUST call this tool every step!",inputSchema:object({evaluation_previous_goal:string().optional(),memory:string().optional(),next_goal:string().optional(),action:r}),execute:a(async s=>{if(p(this,ie).signal.aborted)throw new Error("AbortError");console.log(chalk.blue.bold("MacroTool input"),s);const c=s.action,l=Object.keys(c)[0],d=c[l],h=[];s.evaluation_previous_goal&&h.push(`βœ…: ${s.evaluation_previous_goal}`),s.memory&&h.push(`πŸ’Ύ: ${s.memory}`),s.next_goal&&h.push(`🎯: ${s.next_goal}`);const f=h.length>0?h.join(`
198
+ `,log=console.log.bind(console,chalk.yellow("[autoFixer]"));function normalizeResponse(e,t){let o=null;const n=e.choices?.[0];if(!n)throw new Error("No choices in response");const r=n.message;if(!r)throw new Error("No message in choice");const i=r.tool_calls?.[0];if(i?.function?.arguments)o=safeJsonParse(i.function.arguments),i.function.name&&i.function.name!=="AgentOutput"&&(log("#1: fixing tool_call"),o={action:safeJsonParse(o)});else if(r.content){const s=r.content.trim(),c=retrieveJsonFromString(s);if(c)o=safeJsonParse(c),o?.name==="AgentOutput"&&(log("#2: fixing tool_call"),o=safeJsonParse(o.arguments)),o?.type==="function"&&(log("#3: fixing tool_call"),o=safeJsonParse(o.function.arguments)),!o?.action&&!o?.evaluation_previous_goal&&!o?.memory&&!o?.next_goal&&!o?.thinking&&(log("#4: fixing tool_call"),o={action:safeJsonParse(o)});else throw new Error("No tool_call and the message content does not contain valid JSON")}else throw new Error("No tool_call nor message content is present");return o=safeJsonParse(o),o.action&&(o.action=safeJsonParse(o.action)),o.action&&t&&(o.action=validateAction(o.action,t)),o.action||(log("#5: fixing tool_call"),o.action={name:"wait",input:{seconds:1}}),{...e,choices:[{...n,message:{...r,tool_calls:[{...i||{},function:{...i?.function||{},name:"AgentOutput",arguments:JSON.stringify(o)}}]}}]}}a(normalizeResponse,"normalizeResponse");function validateAction(e,t){if(typeof e!="object"||e===null)return e;const o=Object.keys(e)[0];if(!o)return e;const n=t.get(o);if(!n){const c=Array.from(t.keys()).join(", ");throw new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,`Unknown action "${o}". Available: ${c}`)}let r=e[o];const i=n.inputSchema;if(i instanceof ZodObject&&r!==null&&typeof r!="object"){const c=Object.keys(i.shape).find(l=>!i.shape[l].safeParse(void 0).success);c&&(log(`coercing primitive action input for "${o}"`),r={[c]:r})}const s=i.safeParse(r);if(!s.success)throw new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,`Invalid input for action "${o}": ${prettifyError(s.error)}`);return{[o]:s.data}}a(validateAction,"validateAction");function safeJsonParse(e){if(typeof e=="string")try{return JSON.parse(e.trim())}catch{return e}return e}a(safeJsonParse,"safeJsonParse");function retrieveJsonFromString(e){try{const t=/({[\s\S]*})/.exec(e)??[];return t.length===0?null:JSON.parse(t[0])}catch{return null}}a(retrieveJsonFromString,"retrieveJsonFromString");async function waitFor$1(e){await new Promise(t=>setTimeout(t,e*1e3))}a(waitFor$1,"waitFor$1");function truncate$1(e,t){return e.length>t?e.substring(0,t)+"...":e}a(truncate$1,"truncate$1");function randomID(e){let t=Math.random().toString(36).substring(2,11);if(!e)return t;const o=1e3;let n=0;for(;e.includes(t);)if(t=Math.random().toString(36).substring(2,11),n++,n>o)throw new Error("randomID: too many try");return t}a(randomID,"randomID");const _global=globalThis;_global.__PAGE_AGENT_IDS__||(_global.__PAGE_AGENT_IDS__=[]);const ids=_global.__PAGE_AGENT_IDS__;function uid(){const e=randomID(ids);return ids.push(e),e}a(uid,"uid");const llmsTxtCache=new Map;async function fetchLlmsTxt(e){const t=new URL(e).origin;if(llmsTxtCache.has(t))return llmsTxtCache.get(t);const o=`${t}/llms.txt`;let n=null;try{console.log(chalk.gray(`[llms.txt] Fetching ${o}`));const r=await fetch(o,{signal:AbortSignal.timeout(3e3)});r.ok?(n=await r.text(),console.log(chalk.green(`[llms.txt] Found (${n.length} chars)`)),n.length>1e3&&(console.log(chalk.yellow("[llms.txt] Truncating to 1000 chars")),n=truncate$1(n,1e3))):console.debug(chalk.gray(`[llms.txt] ${r.status} for ${o}`))}catch(r){console.debug(chalk.gray(`[llms.txt] not found for ${o}`),r)}return llmsTxtCache.set(t,n),n}a(fetchLlmsTxt,"fetchLlmsTxt");function assert(e,t,o){if(!e){const n=t??"Assertion failed";throw console.error(chalk.red(`❌ assert: ${n}`)),new Error(n)}}a(assert,"assert");function tool(e){return e}a(tool,"tool");const tools=new Map;tools.set("done",{description:"Complete task. Text is your final response to the user β€” keep it concise unless the user explicitly asks for detail.",inputSchema:object({text:string(),success:boolean().default(!0)}),execute:a(async function(e){return Promise.resolve("Task completed")},"execute")}),tools.set("wait",{description:"Wait for x seconds. Can be used to wait until the page or data is fully loaded.",inputSchema:object({seconds:number().min(1).max(10).default(1)}),execute:a(async function(e){const t=await this.pageController.getLastUpdateTime(),o=Math.max(0,e.seconds-(Date.now()-t)/1e3);return console.log(`actualWaitTime: ${o} seconds`),await waitFor$1(o),`βœ… Waited for ${e.seconds} seconds.`},"execute")}),tools.set("ask_user",{description:"Ask the user a question and wait for their answer. Use this if you need more information or clarification.",inputSchema:object({question:string()}),execute:a(async function(e){if(!this.onAskUser)throw new Error("ask_user tool requires onAskUser callback to be set");return`User answered: ${await this.onAskUser(e.question)}`},"execute")}),tools.set("click_element_by_index",{description:"Click element by index",inputSchema:object({index:int().min(0)}),execute:a(async function(e){return(await this.pageController.clickElement(e.index)).message},"execute")}),tools.set("input_text",{description:"Click and type text into an interactive input element",inputSchema:object({index:int().min(0),text:string()}),execute:a(async function(e){return(await this.pageController.inputText(e.index,e.text)).message},"execute")}),tools.set("select_dropdown_option",{description:"Select dropdown option for interactive element index by the text of the option you want to select",inputSchema:object({index:int().min(0),text:string()}),execute:a(async function(e){return(await this.pageController.selectOption(e.index,e.text)).message},"execute")}),tools.set("scroll",{description:"Scroll the page vertically. Use index for scroll elements (dropdowns/custom UI).",inputSchema:object({down:boolean().default(!0),num_pages:number().min(0).max(10).optional().default(.1),pixels:number().int().min(0).optional(),index:number().int().min(0).optional()}),execute:a(async function(e){return(await this.pageController.scroll({...e,numPages:e.num_pages})).message},"execute")}),tools.set("scroll_horizontally",{description:"Scroll the page horizontally, or within a specific element by index. Useful for wide tables.",inputSchema:object({right:boolean().default(!0),pixels:number().int().min(0),index:number().int().min(0).optional()}),execute:a(async function(e){return(await this.pageController.scrollHorizontally(e)).message},"execute")}),tools.set("execute_javascript",{description:"Execute JavaScript code on the current page. Supports async/await syntax. Use with caution!",inputSchema:object({script:string()}),execute:a(async function(e){return(await this.pageController.executeJavascript(e.script)).message},"execute")});const ut=class ut extends EventTarget{constructor(o){super();Z(this,R);ne(this,"id",uid());ne(this,"config");ne(this,"tools");ne(this,"pageController");ne(this,"task","");ne(this,"taskId","");ne(this,"history",[]);ne(this,"disposed",!1);ne(this,"onAskUser");Z(this,Ue,"idle");Z(this,Oe);Z(this,ie,new AbortController);Z(this,$e,[]);Z(this,Q,{totalWaitTime:0,lastURL:"",browserState:null});if(this.config={...o,maxSteps:o.maxSteps??40},S(this,Oe,new LLM(this.config)),this.tools=new Map(tools),this.pageController=o.pageController,p(this,Oe).addEventListener("retry",n=>{const{attempt:r,maxAttempts:i}=n.detail;b(this,R,Me).call(this,{type:"retrying",attempt:r,maxAttempts:i}),this.history.push({type:"retry",message:`LLM retry attempt ${r} of ${i}`,attempt:r,maxAttempts:i}),b(this,R,Pe).call(this)}),p(this,Oe).addEventListener("error",n=>{const r=n.detail.error;if(r?.rawError?.name==="AbortError")return;const i=String(r);b(this,R,Me).call(this,{type:"error",message:i}),this.history.push({type:"error",message:i,rawResponse:r.rawResponse}),b(this,R,Pe).call(this)}),this.config.customTools)for(const[n,r]of Object.entries(this.config.customTools)){if(r===null){this.tools.delete(n);continue}this.tools.set(n,r)}this.config.experimentalScriptExecutionTool||this.tools.delete("execute_javascript")}get status(){return p(this,Ue)}pushObservation(o){p(this,$e).push(o)}stop(){this.pageController.cleanUpHighlights(),this.pageController.hideMask(),p(this,ie).abort()}async execute(o){if(this.disposed)throw new Error("PageAgent has been disposed. Create a new instance.");if(!o)throw new Error("Task is required");this.task=o,this.taskId=uid(),this.onAskUser||this.tools.delete("ask_user");const n=this.config.onBeforeStep,r=this.config.onAfterStep,i=this.config.onBeforeTask,s=this.config.onAfterTask;await i?.(this),await this.pageController.showMask(),p(this,ie)&&(p(this,ie).abort(),S(this,ie,new AbortController)),this.history=[],b(this,R,_t).call(this,"running"),b(this,R,Pe).call(this),S(this,$e,[]),S(this,Q,{totalWaitTime:0,lastURL:"",browserState:null});let c=0;for(;;){try{console.group(`step: ${c}`),await n?.(this,c),console.log(chalk.blue.bold("πŸ‘€ Observing...")),p(this,Q).browserState=await this.pageController.getBrowserState(),await b(this,R,At).call(this,c);const l=[{role:"system",content:b(this,R,It).call(this)},{role:"user",content:await b(this,R,Pt).call(this)}],d={AgentOutput:b(this,R,Tt).call(this)};console.log(chalk.blue.bold("🧠 Thinking...")),b(this,R,Me).call(this,{type:"thinking"});const h=await p(this,Oe).invoke(l,d,p(this,ie).signal,{toolChoiceName:"AgentOutput",normalizeResponse:a(C=>normalizeResponse(C,this.tools),"normalizeResponse")}),f=h.toolResult,g=f.input,m=f.output,T={evaluation_previous_goal:g.evaluation_previous_goal,memory:g.memory,next_goal:g.next_goal},I=Object.keys(g.action)[0],A={name:I,input:g.action[I],output:m};if(this.history.push({type:"step",stepIndex:c,reflection:T,action:A,usage:h.usage,rawResponse:h.rawResponse,rawRequest:h.rawRequest}),b(this,R,Pe).call(this),await r?.(this,this.history),console.groupEnd(),I==="done"){const C=A.input?.success??!1,D=A.input?.text||"no text provided";console.log(chalk.green.bold("Task completed"),C,D),b(this,R,st).call(this,C);const k={success:C,data:D,history:this.history};return await s?.(this,k),k}}catch(l){console.groupEnd();const d=l?.rawError?.name==="AbortError";console.error("Task failed",l);const h=d?"Task stopped":String(l);b(this,R,Me).call(this,{type:"error",message:h}),this.history.push({type:"error",message:h,rawResponse:l}),b(this,R,Pe).call(this),b(this,R,st).call(this,!1);const f={success:!1,data:h,history:this.history};return await s?.(this,f),f}if(c++,c>this.config.maxSteps){const l="Step count exceeded maximum limit";this.history.push({type:"error",message:l}),b(this,R,Pe).call(this),b(this,R,st).call(this,!1);const d={success:!1,data:l,history:this.history};return await s?.(this,d),d}await waitFor$1(.4)}}dispose(){console.log("Disposing PageAgent..."),this.disposed=!0,this.pageController.dispose(),p(this,ie).abort(),this.dispatchEvent(new Event("dispose")),this.config.onDispose?.(this)}};Ue=new WeakMap,Oe=new WeakMap,ie=new WeakMap,$e=new WeakMap,Q=new WeakMap,R=new WeakSet,St=a(function(){this.dispatchEvent(new Event("statuschange"))},"#emitStatusChange"),Pe=a(function(){this.dispatchEvent(new Event("historychange"))},"#emitHistoryChange"),Me=a(function(o){this.dispatchEvent(new CustomEvent("activity",{detail:o}))},"#emitActivity"),_t=a(function(o){p(this,Ue)!==o&&(S(this,Ue,o),b(this,R,St).call(this))},"#setStatus"),Tt=a(function(){const o=this.tools,n=Array.from(o.entries()).map(([s,c])=>object({[s]:c.inputSchema}).describe(c.description)),r=union(n);return{description:"You MUST call this tool every step!",inputSchema:object({evaluation_previous_goal:string().optional(),memory:string().optional(),next_goal:string().optional(),action:r}),execute:a(async s=>{if(p(this,ie).signal.aborted)throw new Error("AbortError");console.log(chalk.blue.bold("MacroTool input"),s);const c=s.action,l=Object.keys(c)[0],d=c[l],h=[];s.evaluation_previous_goal&&h.push(`βœ…: ${s.evaluation_previous_goal}`),s.memory&&h.push(`πŸ’Ύ: ${s.memory}`),s.next_goal&&h.push(`🎯: ${s.next_goal}`);const f=h.length>0?h.join(`
199
199
  `):"";f&&console.log(f);const g=o.get(l);assert(g,`Tool ${l} not found`),console.log(chalk.blue.bold(`Executing tool: ${l}`),d),b(this,R,Me).call(this,{type:"executing",tool:l,input:d});const m=Date.now(),T=await g.execute.bind(this)(d),I=Date.now()-m;return console.log(chalk.green.bold(`Tool (${l}) executed for ${I}ms`),T),b(this,R,Me).call(this,{type:"executed",tool:l,input:d,output:T,duration:I}),l==="wait"?p(this,Q).totalWaitTime+=d?.seconds||0:p(this,Q).totalWaitTime=0,{input:s,output:T}},"execute")}},"#packMacroTool"),It=a(function(){if(this.config.customSystemPrompt)return this.config.customSystemPrompt;const o=this.config.language==="zh-CN"?"δΈ­ζ–‡":"English";return SYSTEM_PROMPT.replace(/Default working language: \*\*.*?\*\*/,`Default working language: **${o}**`)},"#getSystemPrompt"),zt=a(async function(){const{instructions:o,experimentalLlmsTxt:n}=this.config,r=o?.system?.trim();let i;const s=p(this,Q).browserState?.url||"";if(o?.getPageInstructions&&s)try{i=o.getPageInstructions(s)?.trim()}catch(d){console.error(chalk.red("[PageAgent] Failed to execute getPageInstructions callback:"),d)}const c=n&&s?await fetchLlmsTxt(s):void 0;if(!r&&!i&&!c)return"";let l=`<instructions>
200
200
  `;return r&&(l+=`<system_instructions>
201
201
  ${r}
@@ -397,4 +397,4 @@ out vec2 vUV;
397
397
  void main() {
398
398
  vUV = aUV;
399
399
  gl_Position = vec4(aPosition, 0.0, 1.0);
400
- }`;const DEFAULT_COLORS=["rgb(57, 182, 255)","rgb(189, 69, 251)","rgb(255, 87, 51)","rgb(255, 214, 0)"];function parseColor(e){const t=e.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);if(!t)throw new Error(`Invalid color format: ${e}`);const[,o,n,r]=t;return[parseInt(o)/255,parseInt(n)/255,parseInt(r)/255]}a(parseColor,"parseColor");const ft=class ft{element;canvas;options;running=!1;disposed=!1;startTime=0;lastTime=0;rafId=null;glr;observer;constructor(t={}){this.options={width:t.width??600,height:t.height??600,ratio:t.ratio??window.devicePixelRatio??1,borderWidth:t.borderWidth??8,glowWidth:t.glowWidth??200,borderRadius:t.borderRadius??8,mode:t.mode??"light",...t},this.canvas=document.createElement("canvas"),this.options.classNames&&(this.canvas.className=this.options.classNames),this.options.styles&&Object.assign(this.canvas.style,this.options.styles),this.canvas.style.display="block",this.canvas.style.transformOrigin="center",this.canvas.style.pointerEvents="none",this.element=this.canvas,this.setupGL(),this.options.skipGreeting||this.greet()}start(){if(this.disposed)throw new Error("Motion instance has been disposed.");if(this.running)return;if(!this.glr){console.error("WebGL resources are not initialized.");return}this.running=!0,this.startTime=performance.now(),this.resize(this.options.width??600,this.options.height??600,this.options.ratio),this.glr.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.glr.gl.useProgram(this.glr.program),this.glr.gl.uniform2f(this.glr.uResolution,this.canvas.width,this.canvas.height),this.checkGLError(this.glr.gl,"start: after initial setup");const t=a(()=>{if(!this.running||!this.glr)return;this.rafId=requestAnimationFrame(t);const o=performance.now();if(o-this.lastTime<1e3/32)return;this.lastTime=o;const r=(o-this.startTime)*.001;this.render(r)},"loop");this.rafId=requestAnimationFrame(t)}pause(){if(this.disposed)throw new Error("Motion instance has been disposed.");this.running=!1,this.rafId!==null&&cancelAnimationFrame(this.rafId)}dispose(){if(this.disposed)return;this.disposed=!0,this.running=!1,this.rafId!==null&&cancelAnimationFrame(this.rafId);const{gl:t,vao:o,positionBuffer:n,uvBuffer:r,program:i}=this.glr;o&&t.deleteVertexArray(o),n&&t.deleteBuffer(n),r&&t.deleteBuffer(r),t.deleteProgram(i),this.observer&&this.observer.disconnect(),this.canvas.remove()}resize(t,o,n){if(this.disposed)throw new Error("Motion instance has been disposed.");if(this.options.width=t,this.options.height=o,n&&(this.options.ratio=n),!this.running)return;const{gl:r,program:i,vao:s,positionBuffer:c,uvBuffer:l,uResolution:d}=this.glr,h=n??this.options.ratio??window.devicePixelRatio??1,f=Math.max(1,Math.floor(t*h)),g=Math.max(1,Math.floor(o*h));this.canvas.style.width=`${t}px`,this.canvas.style.height=`${o}px`,(this.canvas.width!==f||this.canvas.height!==g)&&(this.canvas.width=f,this.canvas.height=g),r.viewport(0,0,this.canvas.width,this.canvas.height),this.checkGLError(r,"resize: after viewport setup");const{positions:m,uvs:T}=computeBorderGeometry(this.canvas.width,this.canvas.height,this.options.borderWidth*h,this.options.glowWidth*h);r.bindVertexArray(s),r.bindBuffer(r.ARRAY_BUFFER,c),r.bufferData(r.ARRAY_BUFFER,m,r.STATIC_DRAW);const I=r.getAttribLocation(i,"aPosition");r.enableVertexAttribArray(I),r.vertexAttribPointer(I,2,r.FLOAT,!1,0,0),this.checkGLError(r,"resize: after position buffer update"),r.bindBuffer(r.ARRAY_BUFFER,l),r.bufferData(r.ARRAY_BUFFER,T,r.STATIC_DRAW);const A=r.getAttribLocation(i,"aUV");r.enableVertexAttribArray(A),r.vertexAttribPointer(A,2,r.FLOAT,!1,0,0),this.checkGLError(r,"resize: after UV buffer update"),r.useProgram(i),r.uniform2f(d,this.canvas.width,this.canvas.height),r.uniform1f(this.glr.uBorderWidth,this.options.borderWidth*h),r.uniform1f(this.glr.uGlowWidth,this.options.glowWidth*h),r.uniform1f(this.glr.uBorderRadius,this.options.borderRadius*h),this.checkGLError(r,"resize: after uniform updates");const C=performance.now();this.lastTime=C;const D=(C-this.startTime)*.001;this.render(D)}autoResize(t){this.observer&&this.observer.disconnect(),this.observer=new ResizeObserver(()=>{const o=t.getBoundingClientRect();this.resize(o.width,o.height)}),this.observer.observe(t)}fadeIn(){if(this.disposed)throw new Error("Motion instance has been disposed.");return new Promise((t,o)=>{const n=this.canvas.animate([{opacity:0,transform:"scale(1.2)"},{opacity:1,transform:"scale(1)"}],{duration:300,easing:"ease-out",fill:"forwards"});n.onfinish=()=>t(),n.oncancel=()=>o("canceled")})}fadeOut(){if(this.disposed)throw new Error("Motion instance has been disposed.");return new Promise((t,o)=>{const n=this.canvas.animate([{opacity:1,transform:"scale(1)"},{opacity:0,transform:"scale(1.2)"}],{duration:300,easing:"ease-in",fill:"forwards"});n.onfinish=()=>t(),n.oncancel=()=>o("canceled")})}checkGLError(t,o){let n=t.getError();if(n!==t.NO_ERROR){for(console.group(`πŸ”΄ WebGL Error in ${o}`);n!==t.NO_ERROR;){const r=this.getGLErrorName(t,n);console.error(`${r} (0x${n.toString(16)})`),n=t.getError()}console.groupEnd()}}getGLErrorName(t,o){switch(o){case t.INVALID_ENUM:return"INVALID_ENUM";case t.INVALID_VALUE:return"INVALID_VALUE";case t.INVALID_OPERATION:return"INVALID_OPERATION";case t.INVALID_FRAMEBUFFER_OPERATION:return"INVALID_FRAMEBUFFER_OPERATION";case t.OUT_OF_MEMORY:return"OUT_OF_MEMORY";case t.CONTEXT_LOST_WEBGL:return"CONTEXT_LOST_WEBGL";default:return"UNKNOWN_ERROR"}}setupGL(){const t=this.canvas.getContext("webgl2",{antialias:!1,alpha:!0});if(!t)throw new Error("WebGL2 is required but not available.");const o=createProgram(t,vertexShaderSource,fragmentShaderSource);this.checkGLError(t,"setupGL: after createProgram");const n=t.createVertexArray();t.bindVertexArray(n),this.checkGLError(t,"setupGL: after VAO creation");const r=this.canvas.width||2,i=this.canvas.height||2,{positions:s,uvs:c}=computeBorderGeometry(r,i,this.options.borderWidth,this.options.glowWidth),l=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,l),t.bufferData(t.ARRAY_BUFFER,s,t.STATIC_DRAW);const d=t.getAttribLocation(o,"aPosition");t.enableVertexAttribArray(d),t.vertexAttribPointer(d,2,t.FLOAT,!1,0,0),this.checkGLError(t,"setupGL: after position buffer setup");const h=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,h),t.bufferData(t.ARRAY_BUFFER,c,t.STATIC_DRAW);const f=t.getAttribLocation(o,"aUV");t.enableVertexAttribArray(f),t.vertexAttribPointer(f,2,t.FLOAT,!1,0,0),this.checkGLError(t,"setupGL: after UV buffer setup");const g=t.getUniformLocation(o,"uResolution"),m=t.getUniformLocation(o,"uTime"),T=t.getUniformLocation(o,"uBorderWidth"),I=t.getUniformLocation(o,"uGlowWidth"),A=t.getUniformLocation(o,"uBorderRadius"),C=t.getUniformLocation(o,"uColors"),D=t.getUniformLocation(o,"uGlowExponent"),k=t.getUniformLocation(o,"uGlowFactor");t.useProgram(o),t.uniform1f(T,this.options.borderWidth),t.uniform1f(I,this.options.glowWidth),t.uniform1f(A,this.options.borderRadius),this.options.mode==="dark"?(t.uniform1f(D,2),t.uniform1f(k,1.8)):(t.uniform1f(D,1),t.uniform1f(k,1));const v=(this.options.colors||DEFAULT_COLORS).map(parseColor);for(let O=0;O<v.length;O++)t.uniform3f(t.getUniformLocation(o,`uColors[${O}]`),...v[O]);this.checkGLError(t,"setupGL: after uniform setup"),t.bindVertexArray(null),t.bindBuffer(t.ARRAY_BUFFER,null),this.glr={gl:t,program:o,vao:n,positionBuffer:l,uvBuffer:h,uResolution:g,uTime:m,uBorderWidth:T,uGlowWidth:I,uBorderRadius:A,uColors:C}}render(t){if(!this.glr)return;const{gl:o,program:n,vao:r,uTime:i}=this.glr;o.useProgram(n),o.bindVertexArray(r),o.uniform1f(i,t),o.disable(o.DEPTH_TEST),o.disable(o.CULL_FACE),o.disable(o.BLEND),o.clearColor(0,0,0,0),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLES,0,24),this.checkGLError(o,"render: after draw call"),o.bindVertexArray(null)}greet(){console.log("%c🌈 ai-motion 0.4.8 🌈","background: linear-gradient(90deg, #39b6ff, #bd45fb, #ff5733, #ffd600); color: white; text-shadow: 0 0 2px rgba(0, 0, 0, 0.2); font-weight: bold; font-size: 1em; padding: 2px 12px; border-radius: 6px;")}};a(ft,"Motion");let Motion=ft;function hasDarkModeClass(){const e=["dark","dark-mode","theme-dark","night","night-mode"],t=document.documentElement,o=document.body||document.documentElement;for(const r of e)if(t.classList.contains(r)||o?.classList.contains(r))return!0;return!!t.getAttribute("data-theme")?.toLowerCase().includes("dark")}a(hasDarkModeClass,"hasDarkModeClass");function parseRgbColor(e){const t=/rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(e);return t?{r:parseInt(t[1]),g:parseInt(t[2]),b:parseInt(t[3])}:null}a(parseRgbColor,"parseRgbColor");function isColorDark(e,t=128){if(!e||e==="transparent"||e.startsWith("rgba(0, 0, 0, 0)"))return!1;const o=parseRgbColor(e);return o?.299*o.r+.587*o.g+.114*o.b<t:!1}a(isColorDark,"isColorDark");function isBackgroundDark(){const e=window.getComputedStyle(document.documentElement),t=window.getComputedStyle(document.body||document.documentElement),o=e.backgroundColor,n=t.backgroundColor;return isColorDark(n)?!0:n==="transparent"||n.startsWith("rgba(0, 0, 0, 0)")?isColorDark(o):!1}a(isBackgroundDark,"isBackgroundDark");function isPageDark(){try{return!!(hasDarkModeClass()||isBackgroundDark())}catch(e){return console.warn("Error determining if page is dark:",e),!1}}a(isPageDark,"isPageDark");const wrapper="_wrapper_gf8tz_1",styles={wrapper},cursor="_cursor_1dgwb_2",cursorBorder="_cursorBorder_1dgwb_10",cursorFilling="_cursorFilling_1dgwb_25",cursorRipple="_cursorRipple_1dgwb_39",clicking="_clicking_1dgwb_57",cursorStyles={cursor,cursorBorder,cursorFilling,cursorRipple,clicking},gt=class gt{constructor(){Z(this,Ne);ne(this,"shown",!1);ne(this,"wrapper",document.createElement("div"));ne(this,"motion",new Motion({mode:isPageDark()?"dark":"light",styles:{position:"absolute",inset:"0"}}));Z(this,Y,document.createElement("div"));Z(this,ce,0);Z(this,le,0);Z(this,Se,0);Z(this,Te,0);this.wrapper.id="page-agent-runtime_simulator-mask",this.wrapper.className=styles.wrapper,this.wrapper.setAttribute("data-browser-use-ignore","true"),this.wrapper.setAttribute("data-page-agent-ignore","true"),this.wrapper.appendChild(this.motion.element),this.motion.autoResize(this.wrapper),this.wrapper.addEventListener("click",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("mousedown",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("mouseup",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("mousemove",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("wheel",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("keydown",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("keyup",t=>{t.stopPropagation(),t.preventDefault()}),b(this,Ne,Yt).call(this),document.body.appendChild(this.wrapper),b(this,Ne,$t).call(this),window.addEventListener("PageAgent::MovePointerTo",t=>{const{x:o,y:n}=t.detail;this.setCursorPosition(o,n)}),window.addEventListener("PageAgent::ClickPointer",t=>{this.triggerClickAnimation()})}setCursorPosition(t,o){S(this,Se,t),S(this,Te,o)}triggerClickAnimation(){p(this,Y).classList.remove(cursorStyles.clicking),p(this,Y).offsetHeight,p(this,Y).classList.add(cursorStyles.clicking)}show(){this.shown||(this.shown=!0,this.motion.start(),this.motion.fadeIn(),this.wrapper.style.display="block",S(this,ce,window.innerWidth/2),S(this,le,window.innerHeight/2),S(this,Se,p(this,ce)),S(this,Te,p(this,le)),p(this,Y).style.left=`${p(this,ce)}px`,p(this,Y).style.top=`${p(this,le)}px`)}hide(){this.shown&&(this.shown=!1,this.motion.fadeOut(),this.motion.pause(),p(this,Y).classList.remove(cursorStyles.clicking),setTimeout(()=>{this.wrapper.style.display="none"},800))}dispose(){this.motion.dispose(),this.wrapper.remove()}};Y=new WeakMap,ce=new WeakMap,le=new WeakMap,Se=new WeakMap,Te=new WeakMap,Ne=new WeakSet,Yt=a(function(){p(this,Y).className=cursorStyles.cursor;const t=document.createElement("div");t.className=cursorStyles.cursorRipple,p(this,Y).appendChild(t);const o=document.createElement("div");o.className=cursorStyles.cursorFilling,p(this,Y).appendChild(o);const n=document.createElement("div");n.className=cursorStyles.cursorBorder,p(this,Y).appendChild(n),this.wrapper.appendChild(p(this,Y))},"#createCursor"),$t=a(function(){const t=p(this,ce)+(p(this,Se)-p(this,ce))*.2,o=p(this,le)+(p(this,Te)-p(this,le))*.2,n=Math.abs(t-p(this,Se));n>0&&(n<2?S(this,ce,p(this,Se)):S(this,ce,t),p(this,Y).style.left=`${p(this,ce)}px`);const r=Math.abs(o-p(this,Te));r>0&&(r<2?S(this,le,p(this,Te)):S(this,le,o),p(this,Y).style.top=`${p(this,le)}px`),requestAnimationFrame(()=>b(this,Ne,$t).call(this))},"#moveCursorToTarget"),a(gt,"SimulatorMask");let SimulatorMask=gt;const SimulatorMask$1=Object.freeze(Object.defineProperty({__proto__:null,SimulatorMask},Symbol.toStringTag,{value:"Module"}))})();
400
+ }`;const DEFAULT_COLORS=["rgb(57, 182, 255)","rgb(189, 69, 251)","rgb(255, 87, 51)","rgb(255, 214, 0)"];function parseColor(e){const t=e.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);if(!t)throw new Error(`Invalid color format: ${e}`);const[,o,n,r]=t;return[parseInt(o)/255,parseInt(n)/255,parseInt(r)/255]}a(parseColor,"parseColor");const ft=class ft{element;canvas;options;running=!1;disposed=!1;startTime=0;lastTime=0;rafId=null;glr;observer;constructor(t={}){this.options={width:t.width??600,height:t.height??600,ratio:t.ratio??window.devicePixelRatio??1,borderWidth:t.borderWidth??8,glowWidth:t.glowWidth??200,borderRadius:t.borderRadius??8,mode:t.mode??"light",...t},this.canvas=document.createElement("canvas"),this.options.classNames&&(this.canvas.className=this.options.classNames),this.options.styles&&Object.assign(this.canvas.style,this.options.styles),this.canvas.style.display="block",this.canvas.style.transformOrigin="center",this.canvas.style.pointerEvents="none",this.element=this.canvas,this.setupGL(),this.options.skipGreeting||this.greet()}start(){if(this.disposed)throw new Error("Motion instance has been disposed.");if(this.running)return;if(!this.glr){console.error("WebGL resources are not initialized.");return}this.running=!0,this.startTime=performance.now(),this.resize(this.options.width??600,this.options.height??600,this.options.ratio),this.glr.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.glr.gl.useProgram(this.glr.program),this.glr.gl.uniform2f(this.glr.uResolution,this.canvas.width,this.canvas.height),this.checkGLError(this.glr.gl,"start: after initial setup");const t=a(()=>{if(!this.running||!this.glr)return;this.rafId=requestAnimationFrame(t);const o=performance.now();if(o-this.lastTime<1e3/32)return;this.lastTime=o;const r=(o-this.startTime)*.001;this.render(r)},"loop");this.rafId=requestAnimationFrame(t)}pause(){if(this.disposed)throw new Error("Motion instance has been disposed.");this.running=!1,this.rafId!==null&&cancelAnimationFrame(this.rafId)}dispose(){if(this.disposed)return;this.disposed=!0,this.running=!1,this.rafId!==null&&cancelAnimationFrame(this.rafId);const{gl:t,vao:o,positionBuffer:n,uvBuffer:r,program:i}=this.glr;o&&t.deleteVertexArray(o),n&&t.deleteBuffer(n),r&&t.deleteBuffer(r),t.deleteProgram(i),this.observer&&this.observer.disconnect(),this.canvas.remove()}resize(t,o,n){if(this.disposed)throw new Error("Motion instance has been disposed.");if(this.options.width=t,this.options.height=o,n&&(this.options.ratio=n),!this.running)return;const{gl:r,program:i,vao:s,positionBuffer:c,uvBuffer:l,uResolution:d}=this.glr,h=n??this.options.ratio??window.devicePixelRatio??1,f=Math.max(1,Math.floor(t*h)),g=Math.max(1,Math.floor(o*h));this.canvas.style.width=`${t}px`,this.canvas.style.height=`${o}px`,(this.canvas.width!==f||this.canvas.height!==g)&&(this.canvas.width=f,this.canvas.height=g),r.viewport(0,0,this.canvas.width,this.canvas.height),this.checkGLError(r,"resize: after viewport setup");const{positions:m,uvs:T}=computeBorderGeometry(this.canvas.width,this.canvas.height,this.options.borderWidth*h,this.options.glowWidth*h);r.bindVertexArray(s),r.bindBuffer(r.ARRAY_BUFFER,c),r.bufferData(r.ARRAY_BUFFER,m,r.STATIC_DRAW);const I=r.getAttribLocation(i,"aPosition");r.enableVertexAttribArray(I),r.vertexAttribPointer(I,2,r.FLOAT,!1,0,0),this.checkGLError(r,"resize: after position buffer update"),r.bindBuffer(r.ARRAY_BUFFER,l),r.bufferData(r.ARRAY_BUFFER,T,r.STATIC_DRAW);const A=r.getAttribLocation(i,"aUV");r.enableVertexAttribArray(A),r.vertexAttribPointer(A,2,r.FLOAT,!1,0,0),this.checkGLError(r,"resize: after UV buffer update"),r.useProgram(i),r.uniform2f(d,this.canvas.width,this.canvas.height),r.uniform1f(this.glr.uBorderWidth,this.options.borderWidth*h),r.uniform1f(this.glr.uGlowWidth,this.options.glowWidth*h),r.uniform1f(this.glr.uBorderRadius,this.options.borderRadius*h),this.checkGLError(r,"resize: after uniform updates");const C=performance.now();this.lastTime=C;const D=(C-this.startTime)*.001;this.render(D)}autoResize(t){this.observer&&this.observer.disconnect(),this.observer=new ResizeObserver(()=>{const o=t.getBoundingClientRect();this.resize(o.width,o.height)}),this.observer.observe(t)}fadeIn(){if(this.disposed)throw new Error("Motion instance has been disposed.");return new Promise((t,o)=>{const n=this.canvas.animate([{opacity:0,transform:"scale(1.2)"},{opacity:1,transform:"scale(1)"}],{duration:300,easing:"ease-out",fill:"forwards"});n.onfinish=()=>t(),n.oncancel=()=>o("canceled")})}fadeOut(){if(this.disposed)throw new Error("Motion instance has been disposed.");return new Promise((t,o)=>{const n=this.canvas.animate([{opacity:1,transform:"scale(1)"},{opacity:0,transform:"scale(1.2)"}],{duration:300,easing:"ease-in",fill:"forwards"});n.onfinish=()=>t(),n.oncancel=()=>o("canceled")})}checkGLError(t,o){let n=t.getError();if(n!==t.NO_ERROR){for(console.group(`πŸ”΄ WebGL Error in ${o}`);n!==t.NO_ERROR;){const r=this.getGLErrorName(t,n);console.error(`${r} (0x${n.toString(16)})`),n=t.getError()}console.groupEnd()}}getGLErrorName(t,o){switch(o){case t.INVALID_ENUM:return"INVALID_ENUM";case t.INVALID_VALUE:return"INVALID_VALUE";case t.INVALID_OPERATION:return"INVALID_OPERATION";case t.INVALID_FRAMEBUFFER_OPERATION:return"INVALID_FRAMEBUFFER_OPERATION";case t.OUT_OF_MEMORY:return"OUT_OF_MEMORY";case t.CONTEXT_LOST_WEBGL:return"CONTEXT_LOST_WEBGL";default:return"UNKNOWN_ERROR"}}setupGL(){const t=this.canvas.getContext("webgl2",{antialias:!1,alpha:!0});if(!t)throw new Error("WebGL2 is required but not available.");const o=createProgram(t,vertexShaderSource,fragmentShaderSource);this.checkGLError(t,"setupGL: after createProgram");const n=t.createVertexArray();t.bindVertexArray(n),this.checkGLError(t,"setupGL: after VAO creation");const r=this.canvas.width||2,i=this.canvas.height||2,{positions:s,uvs:c}=computeBorderGeometry(r,i,this.options.borderWidth,this.options.glowWidth),l=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,l),t.bufferData(t.ARRAY_BUFFER,s,t.STATIC_DRAW);const d=t.getAttribLocation(o,"aPosition");t.enableVertexAttribArray(d),t.vertexAttribPointer(d,2,t.FLOAT,!1,0,0),this.checkGLError(t,"setupGL: after position buffer setup");const h=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,h),t.bufferData(t.ARRAY_BUFFER,c,t.STATIC_DRAW);const f=t.getAttribLocation(o,"aUV");t.enableVertexAttribArray(f),t.vertexAttribPointer(f,2,t.FLOAT,!1,0,0),this.checkGLError(t,"setupGL: after UV buffer setup");const g=t.getUniformLocation(o,"uResolution"),m=t.getUniformLocation(o,"uTime"),T=t.getUniformLocation(o,"uBorderWidth"),I=t.getUniformLocation(o,"uGlowWidth"),A=t.getUniformLocation(o,"uBorderRadius"),C=t.getUniformLocation(o,"uColors"),D=t.getUniformLocation(o,"uGlowExponent"),k=t.getUniformLocation(o,"uGlowFactor");t.useProgram(o),t.uniform1f(T,this.options.borderWidth),t.uniform1f(I,this.options.glowWidth),t.uniform1f(A,this.options.borderRadius),this.options.mode==="dark"?(t.uniform1f(D,2),t.uniform1f(k,1.8)):(t.uniform1f(D,1),t.uniform1f(k,1));const v=(this.options.colors||DEFAULT_COLORS).map(parseColor);for(let O=0;O<v.length;O++)t.uniform3f(t.getUniformLocation(o,`uColors[${O}]`),...v[O]);this.checkGLError(t,"setupGL: after uniform setup"),t.bindVertexArray(null),t.bindBuffer(t.ARRAY_BUFFER,null),this.glr={gl:t,program:o,vao:n,positionBuffer:l,uvBuffer:h,uResolution:g,uTime:m,uBorderWidth:T,uGlowWidth:I,uBorderRadius:A,uColors:C}}render(t){if(!this.glr)return;const{gl:o,program:n,vao:r,uTime:i}=this.glr;o.useProgram(n),o.bindVertexArray(r),o.uniform1f(i,t),o.disable(o.DEPTH_TEST),o.disable(o.CULL_FACE),o.disable(o.BLEND),o.clearColor(0,0,0,0),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLES,0,24),this.checkGLError(o,"render: after draw call"),o.bindVertexArray(null)}greet(){console.log("%c🌈 ai-motion 0.4.8 🌈","background: linear-gradient(90deg, #39b6ff, #bd45fb, #ff5733, #ffd600); color: white; text-shadow: 0 0 2px rgba(0, 0, 0, 0.2); font-weight: bold; font-size: 1em; padding: 2px 12px; border-radius: 6px;")}};a(ft,"Motion");let Motion=ft;function hasDarkModeClass(){const e=["dark","dark-mode","theme-dark","night","night-mode"],t=document.documentElement,o=document.body||document.documentElement;for(const r of e)if(t.classList.contains(r)||o?.classList.contains(r))return!0;return!!t.getAttribute("data-theme")?.toLowerCase().includes("dark")}a(hasDarkModeClass,"hasDarkModeClass");function parseRgbColor(e){const t=/rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(e);return t?{r:parseInt(t[1]),g:parseInt(t[2]),b:parseInt(t[3])}:null}a(parseRgbColor,"parseRgbColor");function isColorDark(e,t=128){if(!e||e==="transparent"||e.startsWith("rgba(0, 0, 0, 0)"))return!1;const o=parseRgbColor(e);return o?.299*o.r+.587*o.g+.114*o.b<t:!1}a(isColorDark,"isColorDark");function isBackgroundDark(){const e=window.getComputedStyle(document.documentElement),t=window.getComputedStyle(document.body||document.documentElement),o=e.backgroundColor,n=t.backgroundColor;return isColorDark(n)?!0:n==="transparent"||n.startsWith("rgba(0, 0, 0, 0)")?isColorDark(o):!1}a(isBackgroundDark,"isBackgroundDark");function isPageDark(){try{return!!(hasDarkModeClass()||isBackgroundDark())}catch(e){return console.warn("Error determining if page is dark:",e),!1}}a(isPageDark,"isPageDark");const wrapper="_wrapper_gf8tz_1",styles={wrapper},cursor="_cursor_1dgwb_2",cursorBorder="_cursorBorder_1dgwb_10",cursorFilling="_cursorFilling_1dgwb_25",cursorRipple="_cursorRipple_1dgwb_39",clicking="_clicking_1dgwb_57",cursorStyles={cursor,cursorBorder,cursorFilling,cursorRipple,clicking},gt=class gt{constructor(){Z(this,Ne);ne(this,"shown",!1);ne(this,"wrapper",document.createElement("div"));ne(this,"motion",null);Z(this,Y,document.createElement("div"));Z(this,ce,0);Z(this,le,0);Z(this,Se,0);Z(this,Te,0);this.wrapper.id="page-agent-runtime_simulator-mask",this.wrapper.className=styles.wrapper,this.wrapper.setAttribute("data-browser-use-ignore","true"),this.wrapper.setAttribute("data-page-agent-ignore","true");try{const t=new Motion({mode:isPageDark()?"dark":"light",styles:{position:"absolute",inset:"0"}});this.motion=t,this.wrapper.appendChild(t.element),t.autoResize(this.wrapper)}catch(t){console.warn("[SimulatorMask] Motion overlay unavailable:",t)}this.wrapper.addEventListener("click",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("mousedown",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("mouseup",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("mousemove",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("wheel",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("keydown",t=>{t.stopPropagation(),t.preventDefault()}),this.wrapper.addEventListener("keyup",t=>{t.stopPropagation(),t.preventDefault()}),b(this,Ne,Yt).call(this),document.body.appendChild(this.wrapper),b(this,Ne,$t).call(this),window.addEventListener("PageAgent::MovePointerTo",t=>{const{x:o,y:n}=t.detail;this.setCursorPosition(o,n)}),window.addEventListener("PageAgent::ClickPointer",t=>{this.triggerClickAnimation()})}setCursorPosition(t,o){S(this,Se,t),S(this,Te,o)}triggerClickAnimation(){p(this,Y).classList.remove(cursorStyles.clicking),p(this,Y).offsetHeight,p(this,Y).classList.add(cursorStyles.clicking)}show(){this.shown||(this.shown=!0,this.motion?.start(),this.motion?.fadeIn(),this.wrapper.style.display="block",S(this,ce,window.innerWidth/2),S(this,le,window.innerHeight/2),S(this,Se,p(this,ce)),S(this,Te,p(this,le)),p(this,Y).style.left=`${p(this,ce)}px`,p(this,Y).style.top=`${p(this,le)}px`)}hide(){this.shown&&(this.shown=!1,this.motion?.fadeOut(),this.motion?.pause(),p(this,Y).classList.remove(cursorStyles.clicking),setTimeout(()=>{this.wrapper.style.display="none"},800))}dispose(){this.motion?.dispose(),this.wrapper.remove()}};Y=new WeakMap,ce=new WeakMap,le=new WeakMap,Se=new WeakMap,Te=new WeakMap,Ne=new WeakSet,Yt=a(function(){p(this,Y).className=cursorStyles.cursor;const t=document.createElement("div");t.className=cursorStyles.cursorRipple,p(this,Y).appendChild(t);const o=document.createElement("div");o.className=cursorStyles.cursorFilling,p(this,Y).appendChild(o);const n=document.createElement("div");n.className=cursorStyles.cursorBorder,p(this,Y).appendChild(n),this.wrapper.appendChild(p(this,Y))},"#createCursor"),$t=a(function(){const t=p(this,ce)+(p(this,Se)-p(this,ce))*.2,o=p(this,le)+(p(this,Te)-p(this,le))*.2,n=Math.abs(t-p(this,Se));n>0&&(n<2?S(this,ce,p(this,Se)):S(this,ce,t),p(this,Y).style.left=`${p(this,ce)}px`);const r=Math.abs(o-p(this,Te));r>0&&(r<2?S(this,le,p(this,Te)):S(this,le,o),p(this,Y).style.top=`${p(this,le)}px`),requestAnimationFrame(()=>b(this,Ne,$t).call(this))},"#moveCursorToTarget"),a(gt,"SimulatorMask");let SimulatorMask=gt;const SimulatorMask$1=Object.freeze(Object.defineProperty({__proto__:null,SimulatorMask},Symbol.toStringTag,{value:"Module"}))})();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "page-agent",
3
3
  "private": false,
4
- "version": "1.5.1",
4
+ "version": "1.5.2",
5
5
  "type": "module",
6
6
  "main": "./dist/esm/page-agent.js",
7
7
  "module": "./dist/esm/page-agent.js",
@@ -44,10 +44,10 @@
44
44
  "postpublish": "node -e \"['README.md','LICENSE'].forEach(f=>{try{require('fs').unlinkSync(f)}catch{}})\""
45
45
  },
46
46
  "dependencies": {
47
- "@page-agent/core": "1.5.1",
48
- "@page-agent/llms": "1.5.1",
49
- "@page-agent/page-controller": "1.5.1",
50
- "@page-agent/ui": "1.5.1",
47
+ "@page-agent/core": "1.5.2",
48
+ "@page-agent/llms": "1.5.2",
49
+ "@page-agent/page-controller": "1.5.2",
50
+ "@page-agent/ui": "1.5.2",
51
51
  "chalk": "^5.6.2"
52
52
  },
53
53
  "peerDependencies": {