page-agent 1.5.1 β 1.5.3
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 +12 -4
- package/dist/iife/page-agent.demo.js +3 -3
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -11,9 +11,9 @@ The GUI Agent Living in Your Webpage. Control web interfaces with natural langua
|
|
|
11
11
|
|
|
12
12
|
π **English** | [δΈζ](./docs/README-zh.md)
|
|
13
13
|
|
|
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>
|
|
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> | <a href="https://news.ycombinator.com/item?id=47264138" target="_blank">π’ Join HN Discussion</a>
|
|
15
15
|
|
|
16
|
-
<video id="demo-video" src="https://github.com/user-attachments/assets/
|
|
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.
|
|
53
|
-
| China | https://registry.npmmirror.com/page-agent/1.5.
|
|
52
|
+
| Global | https://cdn.jsdelivr.net/npm/page-agent@1.5.3/dist/iife/page-agent.demo.js |
|
|
53
|
+
| China | https://registry.npmmirror.com/page-agent/1.5.3/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
|
|
|
@@ -111,3 +111,11 @@ file and in the node_modules directory after installation.
|
|
|
111
111
|
---
|
|
112
112
|
|
|
113
113
|
**β Star this repo if you find PageAgent helpful!**
|
|
114
|
+
|
|
115
|
+
<a href="https://www.star-history.com/?repos=alibaba%2Fpage-agent&type=date">
|
|
116
|
+
<picture>
|
|
117
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/image?repos=alibaba/page-agent&type=date&theme=dark" />
|
|
118
|
+
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/image?repos=alibaba/page-agent&type=date" />
|
|
119
|
+
<img alt="Star History Chart" src="https://api.star-history.com/image?repos=alibaba/page-agent&type=date" />
|
|
120
|
+
</picture>
|
|
121
|
+
</a>
|
|
@@ -42,7 +42,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
|
|
|
42
42
|
`:`
|
|
43
43
|
`)+o,r=n+1,n=e.indexOf(`
|
|
44
44
|
`,r)}while(n!==-1);return i+=e.slice(r),i}a(stringEncaseCRLFWithFirstIndex,"stringEncaseCRLFWithFirstIndex");const{stdout:stdoutColor,stderr:stderrColor}=supportsColor,GENERATOR=Symbol("GENERATOR"),STYLER=Symbol("STYLER"),IS_EMPTY=Symbol("IS_EMPTY"),levelMapping=["ansi","ansi","ansi256","ansi16m"],styles$2=Object.create(null),applyOptions=a((e,t={})=>{if(t.level&&!(Number.isInteger(t.level)&&t.level>=0&&t.level<=3))throw new Error("The `level` option should be an integer from 0 to 3");const o=stdoutColor?stdoutColor.level:0;e.level=t.level===void 0?o:t.level},"applyOptions"),chalkFactory=a(e=>{const t=a((...o)=>o.join(" "),"chalk");return applyOptions(t,e),Object.setPrototypeOf(t,createChalk.prototype),t},"chalkFactory");function createChalk(e){return chalkFactory(e)}a(createChalk,"createChalk"),Object.setPrototypeOf(createChalk.prototype,Function.prototype);for(const[e,t]of Object.entries(ansiStyles))styles$2[e]={get(){const o=createBuilder(this,createStyler(t.open,t.close,this[STYLER]),this[IS_EMPTY]);return Object.defineProperty(this,e,{value:o}),o}};styles$2.visible={get(){const e=createBuilder(this,this[STYLER],!0);return Object.defineProperty(this,"visible",{value:e}),e}};const getModelAnsi=a((e,t,o,...n)=>e==="rgb"?t==="ansi16m"?ansiStyles[o].ansi16m(...n):t==="ansi256"?ansiStyles[o].ansi256(ansiStyles.rgbToAnsi256(...n)):ansiStyles[o].ansi(ansiStyles.rgbToAnsi(...n)):e==="hex"?getModelAnsi("rgb",t,o,...ansiStyles.hexToRgb(...n)):ansiStyles[o][e](...n),"getModelAnsi"),usedModels=["rgb","hex","ansi256"];for(const e of usedModels){styles$2[e]={get(){const{level:o}=this;return function(...n){const r=createStyler(getModelAnsi(e,levelMapping[o],"color",...n),ansiStyles.color.close,this[STYLER]);return createBuilder(this,r,this[IS_EMPTY])}}};const t="bg"+e[0].toUpperCase()+e.slice(1);styles$2[t]={get(){const{level:o}=this;return function(...n){const r=createStyler(getModelAnsi(e,levelMapping[o],"bgColor",...n),ansiStyles.bgColor.close,this[STYLER]);return createBuilder(this,r,this[IS_EMPTY])}}}}const proto=Object.defineProperties(()=>{},{...styles$2,level:{enumerable:!0,get(){return this[GENERATOR].level},set(e){this[GENERATOR].level=e}}}),createStyler=a((e,t,o)=>{let n,r;return o===void 0?(n=e,r=t):(n=o.openAll+e,r=t+o.closeAll),{open:e,close:t,openAll:n,closeAll:r,parent:o}},"createStyler"),createBuilder=a((e,t,o)=>{const n=a((...r)=>applyStyle(n,r.length===1?""+r[0]:r.join(" ")),"builder");return Object.setPrototypeOf(n,proto),n[GENERATOR]=e,n[STYLER]=t,n[IS_EMPTY]=o,n},"createBuilder"),applyStyle=a((e,t)=>{if(e.level<=0||!t)return e[IS_EMPTY]?"":t;let o=e[STYLER];if(o===void 0)return t;const{openAll:n,closeAll:r}=o;if(t.includes("\x1B"))for(;o!==void 0;)t=stringReplaceAll(t,o.close,o.open),o=o.parent;const i=t.indexOf(`
|
|
45
|
-
`);return i!==-1&&(t=stringEncaseCRLFWithFirstIndex(t,r,n,i)),n+t+r},"applyStyle");Object.defineProperties(createChalk.prototype,styles$2);const chalk=createChalk();createChalk({level:stderrColor?stderrColor.level:0});const debug=console.debug.bind(console,chalk.gray("[LLM]"));function zodToOpenAITool(e,t){return{type:"function",function:{name:e,description:t.description,parameters:toJSONSchema(t.inputSchema,{target:"openapi-3.0"})}}}a(zodToOpenAITool,"zodToOpenAITool");function modelPatch(e){const t=e.model||"";if(!t)return e;const o=normalizeModelName(t);return o.startsWith("qwen")&&(debug("Applying Qwen patch: use higher temperature for auto fixing"),e.temperature=Math.max(e.temperature||0,1),e.enable_thinking=!1),o.startsWith("claude")&&(debug("Applying Claude patch: disable thinking"),e.thinking={type:"disabled"},e.tool_choice==="required"?(debug('Applying Claude patch: convert tool_choice "required" to { type: "any" }'),e.tool_choice={type:"any"}):e.tool_choice?.function?.name&&(debug("Applying Claude patch: convert tool_choice format"),e.tool_choice={type:"tool",name:e.tool_choice.function.name})),o.startsWith("grok")&&(debug("Applying Grok patch: removing tool_choice"),delete e.tool_choice,debug("Applying Grok patch: disable reasoning and thinking"),e.thinking={type:"disabled",effort:"minimal"},e.reasoning={enabled:!1,effort:"low"}),o.startsWith("gpt")&&(debug("Applying GPT patch: set verbosity to low"),e.verbosity="low",o.startsWith("gpt-52")?(debug("Applying GPT-52 patch: disable reasoning"),e.reasoning_effort="none"):o.startsWith("gpt-51")?(debug("Applying GPT-51 patch: disable reasoning"),e.reasoning_effort="none"):o.startsWith("gpt-5-mini")?(debug("Applying GPT-5-mini patch: set reasoning effort to low, temperature to 1"),e.reasoning_effort="low",e.temperature=1):o.startsWith("gpt-5")&&(debug("Applying GPT-5 patch: set reasoning effort to low"),e.reasoning_effort="low")),o.startsWith("gemini")&&(debug("Applying Gemini patch: set reasoning effort to minimal"),e.reasoning_effort="minimal"),e}a(modelPatch,"modelPatch");function normalizeModelName(e){let t=e.toLowerCase();return t.includes("/")&&(t=t.split("/")[1]),t=t.replace(/_/g,""),t=t.replace(/\./g,""),t}a(normalizeModelName,"normalizeModelName");const ct=class ct{config;fetch;constructor(t){this.config=t,this.fetch=t.customFetch}async invoke(t,o,n,r){const i=Object.entries(o).map(([k,v])=>zodToOpenAITool(k,v)),s={model:this.config.model,temperature:this.config.temperature,messages:t,tools:i,parallel_tool_calls:!1,tool_choice:r?.toolChoiceName?{type:"function",function:{name:r.toolChoiceName}}:"required"};modelPatch(s);let c;try{c=await this.fetch(`${this.config.baseURL}/chat/completions`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(s),signal:n})}catch(k){const v=k?.name==="AbortError",O=v?"Network request aborted":"Network request failed";throw v||console.error(k),new InvokeError(InvokeErrorType.NETWORK_ERROR,O,k)}if(!c.ok){const k=await c.json().catch(),v=k.error?.message||c.statusText;throw c.status===401||c.status===403?new InvokeError(InvokeErrorType.AUTH_ERROR,`Authentication failed: ${v}`,k):c.status===429?new InvokeError(InvokeErrorType.RATE_LIMIT,`Rate limit exceeded: ${v}`,k):c.status>=500?new InvokeError(InvokeErrorType.SERVER_ERROR,`Server error: ${v}`,k):new InvokeError(InvokeErrorType.UNKNOWN,`HTTP ${c.status}: ${v}`,k)}const l=await c.json(),d=l.choices?.[0];if(!d)throw new InvokeError(InvokeErrorType.UNKNOWN,"No choices in response",l);switch(d.finish_reason){case"tool_calls":case"function_call":case"stop":break;case"length":throw new InvokeError(InvokeErrorType.CONTEXT_LENGTH,"Response truncated: max tokens reached",void 0,l);case"content_filter":throw new InvokeError(InvokeErrorType.CONTENT_FILTER,"Content filtered by safety system",void 0,l);default:throw new InvokeError(InvokeErrorType.UNKNOWN,`Unexpected finish_reason: ${d.finish_reason}`,void 0,l)}const f=(r?.normalizeResponse?r.normalizeResponse(l):l).choices?.[0],g=f?.message?.tool_calls?.[0]?.function?.name;if(!g)throw new InvokeError(InvokeErrorType.NO_TOOL_CALL,"No tool call found in response",void 0,l);const m=o[g];if(!m)throw new InvokeError(InvokeErrorType.UNKNOWN,`Tool "${g}" not found in tools`,void 0,l);const T=f.message?.tool_calls?.[0]?.function?.arguments;if(!T)throw new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,"No tool call arguments found",void 0,l);let I;try{I=JSON.parse(T)}catch(k){throw new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,"Failed to parse tool arguments as JSON",k,l)}const A=m.inputSchema.safeParse(I);if(!A.success)throw console.error(prettifyError(A.error)),new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,"Tool arguments validation failed",A.error,l);const C=A.data;let D;try{D=await m.execute(C)}catch(k){throw new InvokeError(InvokeErrorType.TOOL_EXECUTION_ERROR,`Tool execution failed: ${k.message}`,k,l)}return{toolCall:{name:g,args:C},toolResult:D,usage:{promptTokens:l.usage?.prompt_tokens??0,completionTokens:l.usage?.completion_tokens??0,totalTokens:l.usage?.total_tokens??0,cachedTokens:l.usage?.prompt_tokens_details?.cached_tokens,reasoningTokens:l.usage?.completion_tokens_details?.reasoning_tokens},rawResponse:l,rawRequest:s}}};a(ct,"OpenAIClient");let OpenAIClient=ct;const LLM_MAX_RETRIES=2,DEFAULT_TEMPERATURE=.7;function parseLLMConfig(e){if(!e.baseURL||!e.apiKey||!e.model)throw new Error("[PageAgent] LLM configuration required. Please provide: baseURL, apiKey, model. See: https://alibaba.github.io/page-agent/docs/features/models");return{baseURL:e.baseURL,apiKey:e.apiKey,model:e.model,temperature:e.temperature??DEFAULT_TEMPERATURE,maxRetries:e.maxRetries??LLM_MAX_RETRIES,customFetch:(e.customFetch??fetch).bind(globalThis)}}a(parseLLMConfig,"parseLLMConfig");const lt=class lt extends EventTarget{config;client;constructor(t){super(),this.config=parseLLMConfig(t),this.client=new OpenAIClient(this.config)}async invoke(t,o,n,r){return await withRetry(async()=>{if(n.aborted)throw new Error("AbortError");return await this.client.invoke(t,o,n,r)},{maxRetries:this.config.maxRetries,onRetry:a(i=>{this.dispatchEvent(new CustomEvent("retry",{detail:{attempt:i,maxAttempts:this.config.maxRetries}}))},"onRetry"),onError:a(i=>{this.dispatchEvent(new CustomEvent("error",{detail:{error:i}}))},"onError")})}};a(lt,"LLM");let LLM=lt;async function withRetry(e,t){let o=0,n=null;for(;o<=t.maxRetries;){o>0&&(t.onRetry(o),await new Promise(r=>setTimeout(r,100)));try{return await e()}catch(r){if(r?.rawError?.name==="AbortError"||(console.error(r),t.onError(r),r instanceof InvokeError&&!r.retryable))throw r;n=r,o++,await new Promise(i=>setTimeout(i,100))}}throw n}a(withRetry,"withRetry");const SYSTEM_PROMPT=`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>.
|
|
45
|
+
`);return i!==-1&&(t=stringEncaseCRLFWithFirstIndex(t,r,n,i)),n+t+r},"applyStyle");Object.defineProperties(createChalk.prototype,styles$2);const chalk=createChalk();createChalk({level:stderrColor?stderrColor.level:0});const debug=console.debug.bind(console,chalk.gray("[LLM]"));function zodToOpenAITool(e,t){return{type:"function",function:{name:e,description:t.description,parameters:toJSONSchema(t.inputSchema,{target:"openapi-3.0"})}}}a(zodToOpenAITool,"zodToOpenAITool");function modelPatch(e){const t=e.model||"";if(!t)return e;const o=normalizeModelName(t);return o.startsWith("qwen")&&(debug("Applying Qwen patch: use higher temperature for auto fixing"),e.temperature=Math.max(e.temperature||0,1),e.enable_thinking=!1),o.startsWith("claude")&&(debug("Applying Claude patch: disable thinking"),e.thinking={type:"disabled"},e.tool_choice==="required"?(debug('Applying Claude patch: convert tool_choice "required" to { type: "any" }'),e.tool_choice={type:"any"}):e.tool_choice?.function?.name&&(debug("Applying Claude patch: convert tool_choice format"),e.tool_choice={type:"tool",name:e.tool_choice.function.name})),o.startsWith("grok")&&(debug("Applying Grok patch: removing tool_choice"),delete e.tool_choice,debug("Applying Grok patch: disable reasoning and thinking"),e.thinking={type:"disabled",effort:"minimal"},e.reasoning={enabled:!1,effort:"low"}),o.startsWith("gpt")&&(debug("Applying GPT patch: set verbosity to low"),e.verbosity="low",o.startsWith("gpt-52")?(debug("Applying GPT-52 patch: disable reasoning"),e.reasoning_effort="none"):o.startsWith("gpt-51")?(debug("Applying GPT-51 patch: disable reasoning"),e.reasoning_effort="none"):o.startsWith("gpt-54")?(debug("Applying GPT-5.4 patch: skip reasoning_effort because chat/completions rejects it with function tools"),delete e.reasoning_effort):o.startsWith("gpt-5-mini")?(debug("Applying GPT-5-mini patch: set reasoning effort to low, temperature to 1"),e.reasoning_effort="low",e.temperature=1):o.startsWith("gpt-5")&&(debug("Applying GPT-5 patch: set reasoning effort to low"),e.reasoning_effort="low")),o.startsWith("gemini")&&(debug("Applying Gemini patch: set reasoning effort to minimal"),e.reasoning_effort="minimal"),e}a(modelPatch,"modelPatch");function normalizeModelName(e){let t=e.toLowerCase();return t.includes("/")&&(t=t.split("/")[1]),t=t.replace(/_/g,""),t=t.replace(/\./g,""),t}a(normalizeModelName,"normalizeModelName");const ct=class ct{config;fetch;constructor(t){this.config=t,this.fetch=t.customFetch}async invoke(t,o,n,r){const i=Object.entries(o).map(([k,v])=>zodToOpenAITool(k,v)),s={model:this.config.model,temperature:this.config.temperature,messages:t,tools:i,parallel_tool_calls:!1,tool_choice:r?.toolChoiceName?{type:"function",function:{name:r.toolChoiceName}}:"required"};modelPatch(s);let c;try{c=await this.fetch(`${this.config.baseURL}/chat/completions`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(s),signal:n})}catch(k){const v=k?.name==="AbortError",O=v?"Network request aborted":"Network request failed";throw v||console.error(k),new InvokeError(InvokeErrorType.NETWORK_ERROR,O,k)}if(!c.ok){const k=await c.json().catch(),v=k.error?.message||c.statusText;throw c.status===401||c.status===403?new InvokeError(InvokeErrorType.AUTH_ERROR,`Authentication failed: ${v}`,k):c.status===429?new InvokeError(InvokeErrorType.RATE_LIMIT,`Rate limit exceeded: ${v}`,k):c.status>=500?new InvokeError(InvokeErrorType.SERVER_ERROR,`Server error: ${v}`,k):new InvokeError(InvokeErrorType.UNKNOWN,`HTTP ${c.status}: ${v}`,k)}const l=await c.json(),d=l.choices?.[0];if(!d)throw new InvokeError(InvokeErrorType.UNKNOWN,"No choices in response",l);switch(d.finish_reason){case"tool_calls":case"function_call":case"stop":break;case"length":throw new InvokeError(InvokeErrorType.CONTEXT_LENGTH,"Response truncated: max tokens reached",void 0,l);case"content_filter":throw new InvokeError(InvokeErrorType.CONTENT_FILTER,"Content filtered by safety system",void 0,l);default:throw new InvokeError(InvokeErrorType.UNKNOWN,`Unexpected finish_reason: ${d.finish_reason}`,void 0,l)}const f=(r?.normalizeResponse?r.normalizeResponse(l):l).choices?.[0],g=f?.message?.tool_calls?.[0]?.function?.name;if(!g)throw new InvokeError(InvokeErrorType.NO_TOOL_CALL,"No tool call found in response",void 0,l);const m=o[g];if(!m)throw new InvokeError(InvokeErrorType.UNKNOWN,`Tool "${g}" not found in tools`,void 0,l);const T=f.message?.tool_calls?.[0]?.function?.arguments;if(!T)throw new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,"No tool call arguments found",void 0,l);let I;try{I=JSON.parse(T)}catch(k){throw new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,"Failed to parse tool arguments as JSON",k,l)}const A=m.inputSchema.safeParse(I);if(!A.success)throw console.error(prettifyError(A.error)),new InvokeError(InvokeErrorType.INVALID_TOOL_ARGS,"Tool arguments validation failed",A.error,l);const C=A.data;let D;try{D=await m.execute(C)}catch(k){throw new InvokeError(InvokeErrorType.TOOL_EXECUTION_ERROR,`Tool execution failed: ${k.message}`,k,l)}return{toolCall:{name:g,args:C},toolResult:D,usage:{promptTokens:l.usage?.prompt_tokens??0,completionTokens:l.usage?.completion_tokens??0,totalTokens:l.usage?.total_tokens??0,cachedTokens:l.usage?.prompt_tokens_details?.cached_tokens,reasoningTokens:l.usage?.completion_tokens_details?.reasoning_tokens},rawResponse:l,rawRequest:s}}};a(ct,"OpenAIClient");let OpenAIClient=ct;const LLM_MAX_RETRIES=2,DEFAULT_TEMPERATURE=.7;function parseLLMConfig(e){if(!e.baseURL||!e.apiKey||!e.model)throw new Error("[PageAgent] LLM configuration required. Please provide: baseURL, apiKey, model. See: https://alibaba.github.io/page-agent/docs/features/models");return{baseURL:e.baseURL,apiKey:e.apiKey,model:e.model,temperature:e.temperature??DEFAULT_TEMPERATURE,maxRetries:e.maxRetries??LLM_MAX_RETRIES,customFetch:(e.customFetch??fetch).bind(globalThis)}}a(parseLLMConfig,"parseLLMConfig");const lt=class lt extends EventTarget{config;client;constructor(t){super(),this.config=parseLLMConfig(t),this.client=new OpenAIClient(this.config)}async invoke(t,o,n,r){return await withRetry(async()=>{if(n.aborted)throw new Error("AbortError");return await this.client.invoke(t,o,n,r)},{maxRetries:this.config.maxRetries,onRetry:a(i=>{this.dispatchEvent(new CustomEvent("retry",{detail:{attempt:i,maxAttempts:this.config.maxRetries}}))},"onRetry"),onError:a(i=>{this.dispatchEvent(new CustomEvent("error",{detail:{error:i}}))},"onError")})}};a(lt,"LLM");let LLM=lt;async function withRetry(e,t){let o=0,n=null;for(;o<=t.maxRetries;){o>0&&(t.onRetry(o),await new Promise(r=>setTimeout(r,100)));try{return await e()}catch(r){if(r?.rawError?.name==="AbortError"||(console.error(r),t.onError(r),r instanceof InvokeError&&!r.retryable))throw r;n=r,o++,await new Promise(i=>setTimeout(i,100))}}throw n}a(withRetry,"withRetry");const SYSTEM_PROMPT=`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>.
|
|
46
46
|
|
|
47
47
|
<intro>
|
|
48
48
|
You excel at following tasks:
|
|
@@ -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.
|
|
4
|
+
"version": "1.5.3",
|
|
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.
|
|
48
|
-
"@page-agent/llms": "1.5.
|
|
49
|
-
"@page-agent/page-controller": "1.5.
|
|
50
|
-
"@page-agent/ui": "1.5.
|
|
47
|
+
"@page-agent/core": "1.5.3",
|
|
48
|
+
"@page-agent/llms": "1.5.3",
|
|
49
|
+
"@page-agent/page-controller": "1.5.3",
|
|
50
|
+
"@page-agent/ui": "1.5.3",
|
|
51
51
|
"chalk": "^5.6.2"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|