@octavus/agent 1.0.18 → 1.0.22
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 +4 -3
- package/dist/browser-extension/assets/index.ts-ofuJnrPV.js +8 -0
- package/dist/browser-extension/service-worker-loader.js +1 -1
- package/dist/computer-mcp/linux/linux-driver.py +274 -10
- package/dist/index.js +54 -53
- package/package.json +1 -1
- package/dist/browser-extension/assets/index.ts-Bis5r5UE.js +0 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octavus/agent",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.22",
|
|
4
4
|
"description": "Octavus Agent CLI - run one of your cloud Octavus Agents on a machine you control, using that machine as the agent's computer. The agent lives in the cloud (fully configured and observable); the local machine only supplies the computer and the trigger.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"octavus",
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
var q=Object.defineProperty;var B=(a,e,t)=>e in a?q(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var g=(a,e,t)=>B(a,typeof e!="symbol"?e+"":e,t);import{S as $,O as A}from"./messages-akAS58tQ.js";const F=25e3,U=2e4,O=8e3,V=5e3,W="render_wedged";class H extends Error{constructor(t){super(t);g(this,"code",W);this.name="RenderWedgedError"}}async function j(a,e,t){let r;try{return await Promise.race([a,new Promise((n,o)=>{r=setTimeout(()=>o(new Error(`${t} timed out after ${e}ms`)),e)})])}finally{r&&clearTimeout(r)}}function K(a){return new Promise(e=>{setTimeout(e,a)})}const X=33,G=50,J=45;class z{constructor(){g(this,"attached",new Set);g(this,"queues",new Map);g(this,"pendingDialogs",new Map);g(this,"fileChooserResolvers",new Map)}async enqueue(e,t){const r=this.queues.get(e)??Promise.resolve(),n=()=>{let i;return Promise.race([t().finally(()=>clearTimeout(i)),new Promise((s,c)=>{i=setTimeout(()=>c(new Error(`CDP operation timed out after ${F}ms`)),F)})])},o=r.then(n,n);return this.queues.set(e,o.then(()=>{},()=>{})),await o}async attach(e){if(!this.attached.has(e)){await chrome.debugger.attach({tabId:e},"1.3");try{await Promise.all([chrome.debugger.sendCommand({tabId:e},"Page.enable"),chrome.debugger.sendCommand({tabId:e},"DOM.enable"),chrome.debugger.sendCommand({tabId:e},"Accessibility.enable")])}catch(t){throw await chrome.debugger.detach({tabId:e}).catch(()=>{}),t}this.attached.add(e)}}async detach(e){if(this.attached.has(e)){try{await chrome.debugger.detach({tabId:e})}catch{}this.handleTabDetached(e)}}async captureScreenshot(e){return await this.enqueue(e,async()=>{await this.attach(e);const t=[{label:"surface",budgetMs:O,run:()=>this.captureFromSurface(e)},{label:"renderer",budgetMs:O,run:()=>this.captureFromRenderer(e)},{label:"visible-tab",budgetMs:V,run:()=>this.captureFromVisibleTab(e)}];let r;for(const n of t)try{return await j(n.run(),n.budgetMs,`screenshot(${n.label})`)}catch(o){r=o}throw new H(`Screenshot failed after all capture paths: the browser is not producing frames (render pipeline wedged). Last error: ${r instanceof Error?r.message:String(r)}`)})}async captureFromSurface(e){const t=await this.buildCssViewportClip(e);return(await chrome.debugger.sendCommand({tabId:e},"Page.captureScreenshot",{format:"jpeg",quality:75,...t&&{clip:t,captureBeyondViewport:!0}})).data}async captureFromRenderer(e){const t=await this.buildCssViewportClip(e).catch(()=>{});return(await chrome.debugger.sendCommand({tabId:e},"Page.captureScreenshot",{format:"jpeg",quality:75,fromSurface:!1,captureBeyondViewport:!1,...t&&{clip:t}})).data}async captureFromVisibleTab(e){const t=await chrome.tabs.get(e),[r]=await chrome.tabs.query({active:!0,windowId:t.windowId});if((r==null?void 0:r.id)!==e)throw new Error("target tab is not the active tab for captureVisibleTab");const n=await chrome.tabs.captureVisibleTab(t.windowId,{format:"jpeg",quality:75}),o=await this.getDevicePixelRatio(e).catch(()=>1);if(o>1){const s=await Y(n,1/o);if(s)return s}const[,i]=n.split(",",2);if(!i)throw new Error("captureVisibleTab returned an invalid image payload");return i}async buildCssViewportClip(e){const r=(await chrome.debugger.sendCommand({tabId:e},"Page.getLayoutMetrics")).cssLayoutViewport;if(!r||r.clientWidth<=0||r.clientHeight<=0)return;const n=await this.getDevicePixelRatio(e);return{x:r.pageX,y:r.pageY,width:r.clientWidth,height:r.clientHeight,scale:1/n}}async getDevicePixelRatio(e){var n;const r=(n=(await chrome.debugger.sendCommand({tabId:e},"Runtime.evaluate",{expression:"window.devicePixelRatio",returnByValue:!0})).result)==null?void 0:n.value;return typeof r=="number"&&r>0?r:1}async click(e,t,r){await this.enqueue(e,async()=>{await this.attach(e),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mousePressed",x:t,y:r,button:"left",clickCount:1}),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mouseReleased",x:t,y:r,button:"left",clickCount:1})})}async doubleClick(e,t,r){await this.enqueue(e,async()=>{await this.attach(e);for(let n=1;n<=2;n++)await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mousePressed",x:t,y:r,button:"left",clickCount:n}),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mouseReleased",x:t,y:r,button:"left",clickCount:n})})}async rightClick(e,t,r){await this.enqueue(e,async()=>{await this.attach(e),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mousePressed",x:t,y:r,button:"right",clickCount:1}),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mouseReleased",x:t,y:r,button:"right",clickCount:1})})}async type(e,t){await this.enqueue(e,async()=>{await this.attach(e),await chrome.debugger.sendCommand({tabId:e},"Input.insertText",{text:t})})}async fill(e,t,r,n){await this.enqueue(e,async()=>{await this.attach(e),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mousePressed",x:t,y:r,button:"left",clickCount:1}),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mouseReleased",x:t,y:r,button:"left",clickCount:1}),await chrome.debugger.sendCommand({tabId:e},"Runtime.evaluate",{expression:"(() => { const el = document.activeElement; if (el && typeof el.select === 'function') el.select(); })()",returnByValue:!0,awaitPromise:!0}),await chrome.debugger.sendCommand({tabId:e},"Input.insertText",{text:n})})}async keyPress(e,t){await this.enqueue(e,async()=>{await this.attach(e);const r=t.split("+").map(c=>c.trim()).filter(Boolean),n=r.pop(),o=r.map(c=>c.toLowerCase());let i=0;(o.includes("alt")||o.includes("option"))&&(i|=1),(o.includes("control")||o.includes("ctrl"))&&(i|=2),(o.includes("meta")||o.includes("command")||o.includes("cmd"))&&(i|=4),o.includes("shift")&&(i|=8);const s=ce(n,i);await chrome.debugger.sendCommand({tabId:e},"Input.dispatchKeyEvent",{type:"rawKeyDown",key:s.key,code:s.code,windowsVirtualKeyCode:s.keyCode,modifiers:i}),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchKeyEvent",{type:"keyUp",key:s.key,code:s.code,windowsVirtualKeyCode:s.keyCode,modifiers:i})})}async navigate(e,t){await this.enqueue(e,async()=>{await chrome.tabs.update(e,{url:t})})}async scrollPage(e,t,r){await this.enqueue(e,async()=>{await this.attach(e);const n=t==="down"?1:-1,o=c=>chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mouseWheel",x:100,y:300,deltaX:0,deltaY:c}),i=Math.min(Math.max(Math.ceil(Math.abs(r)/G),1),J),s=r/i;for(let c=0;c<i;c++)await o(n*s),c<i-1&&await K(X)})}async hover(e,t,r){await this.enqueue(e,async()=>{await this.attach(e),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mouseMoved",x:t,y:r})})}async drag(e,t,r,n,o){await this.enqueue(e,async()=>{await this.attach(e);const i=(l,d,u,h,m)=>chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:l,x:d,y:u,...h!==void 0&&{button:h},...m!==void 0&&{clickCount:m}});await i("mouseMoved",t,r),await i("mousePressed",t,r,"left",1);const s=Math.round((t+n)/2),c=Math.round((r+o)/2);await i("mouseMoved",s,c),await i("mouseMoved",n,o),await i("mouseReleased",n,o,"left",1)})}async evaluateScript(e,t){return await this.enqueue(e,async()=>{await this.attach(e);const r=await chrome.debugger.sendCommand({tabId:e},"Runtime.evaluate",{expression:t,returnByValue:!0,awaitPromise:!0});return{value:r.result.value,exceptionDetails:r.exceptionDetails}})}async getAccessibilityTree(e,t,r,n){return await this.enqueue(e,async()=>{await this.attach(e);const o=await chrome.debugger.sendCommand({tabId:e},"Page.getFrameTree"),i=N(o.frameTree),s=[];let c=n;for(const l of i){if(c<=0)break;let d;try{d=await chrome.debugger.sendCommand({tabId:e},"Accessibility.getFullAXTree",{frameId:l.id})}catch{continue}const h=l.id!==o.frameTree.frame.id?`--- iframe${l.url?` (${l.url})`:""} ---`:null,m=h?h.length+1:0,f=ee(d.nodes,t,r,c-m);f&&(h&&s.push(h),s.push(f),c-=m+f.length+1)}return s.join(`
|
|
2
|
-
`)})}async resolveBackendNodeCoordinates(e,t){return await this.enqueue(e,async()=>{await this.attach(e);let r;try{r=await chrome.debugger.sendCommand({tabId:e},"DOM.getBoxModel",{backendNodeId:t})}catch{throw new Error(`Element ax_${t} no longer in the DOM. Re-run get_accessibility_tree to get fresh refs.`)}const n=r.model.border.length>0?r.model.border:r.model.content;return ae(n)})}async getFrameElementOffset(e,t){return await this.enqueue(e,async()=>{await this.attach(e);const r=await chrome.debugger.sendCommand({tabId:e},"DOM.getFrameOwner",{frameId:t}),n=await chrome.debugger.sendCommand({tabId:e},"DOM.getBoxModel",{backendNodeId:r.backendNodeId}),o=n.model.border.length>0?n.model.border:n.model.content;if(o.length<8)throw new Error("Could not resolve iframe coordinates");return{x:Math.min(o[0],o[2],o[4],o[6]),y:Math.min(o[1],o[3],o[5],o[7])}})}async getFrameTree(e){return await this.enqueue(e,async()=>(await this.attach(e),(await chrome.debugger.sendCommand({tabId:e},"Page.getFrameTree")).frameTree))}async uploadFile(e,t,r,n){await this.enqueue(e,async()=>{await this.attach(e),await chrome.debugger.sendCommand({tabId:e},"Page.setInterceptFileChooserDialog",{enabled:!0});try{const o=new Promise(s=>{const c=setTimeout(()=>{this.fileChooserResolvers.delete(e),s(null)},U);this.fileChooserResolvers.set(e,l=>{clearTimeout(c),s(l)})});await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mousePressed",x:t,y:r,button:"left",clickCount:1}),await chrome.debugger.sendCommand({tabId:e},"Input.dispatchMouseEvent",{type:"mouseReleased",x:t,y:r,button:"left",clickCount:1});const i=await o;if(i){await chrome.debugger.sendCommand({tabId:e},"DOM.setFileInputFiles",{files:n,backendNodeId:i.backendNodeId});return}await this.setFilesOnPageInput(e,n)}finally{await chrome.debugger.sendCommand({tabId:e},"Page.setInterceptFileChooserDialog",{enabled:!1}).catch(()=>{})}})}async setFilesOnPageInput(e,t){const r=await chrome.debugger.sendCommand({tabId:e},"DOM.getDocument",{depth:-1,pierce:!0}),n=ne(r.root);if(n.length===0)throw new Error('File chooser did not open and no file input was found on the page - the clicked element may not trigger a file upload. Open the upload control first (e.g. click the "Files" option in an upload menu), then call upload_file on it.');const o=n.filter(s=>!s.disabled&&!s.directory);let i=o.length===1?o[0]:void 0;if(!i&&o.length>1){const s=ie(t[0]),c=s?o.filter(l=>se(l.accept,s)):[];c.length===1&&(i=c[0])}if(!i)throw new Error(`File chooser did not open and the page has ${o.length} possible file inputs, so the upload target is ambiguous. Click the specific upload control and try again, or use the service's connector to upload or replace the file.`);await chrome.debugger.sendCommand({tabId:e},"DOM.setFileInputFiles",{files:t,backendNodeId:i.backendNodeId})}onFileChooserOpened(e,t){const r=this.fileChooserResolvers.get(e);r&&(this.fileChooserResolvers.delete(e),r({backendNodeId:t.backendNodeId}))}onDialogOpening(e,t){this.pendingDialogs.set(e,{type:t.type,message:t.message,defaultPrompt:t.defaultPrompt})}getPendingDialog(e){return this.pendingDialogs.get(e)}async handleDialog(e,t,r){await this.enqueue(e,async()=>{await this.attach(e),await chrome.debugger.sendCommand({tabId:e},"Page.handleJavaScriptDialog",{accept:t,...r!==void 0&&{promptText:r}}),this.pendingDialogs.delete(e)})}handleTabDetached(e){this.attached.delete(e),this.queues.delete(e),this.pendingDialogs.delete(e),this.fileChooserResolvers.delete(e)}}const Q=new Set(["button","checkbox","combobox","link","menuitem","menuitemcheckbox","menuitemradio","option","radio","searchbox","slider","spinbutton","switch","tab","textbox","treeitem"]),Z=new Set(["alertdialog","dialog","form","heading","main","navigation","region","search","tabpanel"]);async function Y(a,e){try{const t=await(await fetch(a)).blob(),r=await createImageBitmap(t),n=Math.max(1,Math.round(r.width*e)),o=Math.max(1,Math.round(r.height*e)),i=new OffscreenCanvas(n,o),s=i.getContext("2d");if(!s)return r.close(),null;s.drawImage(r,0,0,n,o),r.close();const c=await i.convertToBlob({type:"image/jpeg",quality:.75}),l=new Uint8Array(await c.arrayBuffer()),d=32768;let u="";for(let h=0;h<l.length;h+=d)u+=String.fromCharCode(...l.subarray(h,h+d));return btoa(u)}catch{return null}}function N(a){return[a.frame,...(a.childFrames??[]).flatMap(N)]}function ee(a,e,t,r){const n=new Map(a.map(d=>[d.nodeId,d])),o=new Set(a.flatMap(d=>d.childIds??[])),i=a.filter(d=>!o.has(d.nodeId)),s=[];let c=0;function l(d,u){if(c>=r)return;if(d.ignored===!0){for(const T of d.childIds??[]){const b=n.get(T);b&&l(b,u)}return}const h=v(d.role,!1),m=v(d.name)||v(d.description)||v(d.value),f=Q.has(h),p=Z.has(h),P=d.backendDOMNodeId!==void 0&&h!==""&&(e==="interactive"?f||p:f||p||m!=="");if(P){const T=" ".repeat(u),b=`ax_${d.backendDOMNodeId}`,x=m.replace(/"/g,'\\"');let C=`${T}${h}`;x&&(C+=` "${x}"`),C+=` [${b}]`;const _=v(d.value);_&&_!==m&&(C+=` value="${_.replace(/"/g,'\\"')}"`),te(d,"disabled")&&(C+=" disabled"),s.push(C),c+=C.length+1}const R=P?u+1:u;if(!(R>=t))for(const T of d.childIds??[]){const b=n.get(T);b&&l(b,R)}}for(const d of i)l(d,0);return s.length>0?s.join(`
|
|
3
|
-
`):""}function v(a,e=!0){const t=a==null?void 0:a.value;if(typeof t=="string"){const r=t.replace(/\s+/g," ").trim();return e?re(r):r}return typeof t=="number"||typeof t=="boolean"?String(t):""}function te(a,e){var t;return((t=a.properties)==null?void 0:t.some(r=>{var n;return r.name===e&&((n=r.value)==null?void 0:n.value)===!0}))??!1}function re(a,e=100){return a.length<=e?a:`${a.slice(0,e)}...`}function ae(a){if(a.length<8)throw new Error("Could not resolve element coordinates from backend node");const e=[a[0],a[2],a[4],a[6]],t=[a[1],a[3],a[5],a[7]];return{x:Math.round((Math.min(...e)+Math.max(...e))/2),y:Math.round((Math.min(...t)+Math.max(...t))/2)}}function ne(a){var r;const e=[],t=[a];for(;t.length>0;){const n=t.pop();if(((r=n.nodeName)==null?void 0:r.toUpperCase())==="INPUT"&&n.backendNodeId!==void 0){const o=oe(n.attributes);(o.type??"").toLowerCase()==="file"&&e.push({backendNodeId:n.backendNodeId,disabled:"disabled"in o,directory:"webkitdirectory"in o||"directory"in o,accept:o.accept})}for(const o of n.children??[])t.push(o);for(const o of n.shadowRoots??[])t.push(o);n.contentDocument&&t.push(n.contentDocument)}return e}function oe(a){const e={};if(!a)return e;for(let t=0;t+1<a.length;t+=2)e[a[t].toLowerCase()]=a[t+1];return e}function ie(a){if(!a)return"";const e=a.split(/[\\/]/).pop()??"",t=e.lastIndexOf(".");return t>0?e.slice(t).toLowerCase():""}function se(a,e){if(!a)return!0;const t=a.toLowerCase().split(",").map(r=>r.trim()).filter(Boolean);return t.length===0||t.includes("*/*")||t.includes("*")?!0:t.includes(e)}function ce(a,e=0){const t=(e&8)!==0,r={Enter:{key:"Enter",code:"Enter",keyCode:13},Tab:{key:"Tab",code:"Tab",keyCode:9},Escape:{key:"Escape",code:"Escape",keyCode:27},Backspace:{key:"Backspace",code:"Backspace",keyCode:8},Delete:{key:"Delete",code:"Delete",keyCode:46},ArrowUp:{key:"ArrowUp",code:"ArrowUp",keyCode:38},ArrowDown:{key:"ArrowDown",code:"ArrowDown",keyCode:40},ArrowLeft:{key:"ArrowLeft",code:"ArrowLeft",keyCode:37},ArrowRight:{key:"ArrowRight",code:"ArrowRight",keyCode:39},Home:{key:"Home",code:"Home",keyCode:36},End:{key:"End",code:"End",keyCode:35},PageUp:{key:"PageUp",code:"PageUp",keyCode:33},PageDown:{key:"PageDown",code:"PageDown",keyCode:34},Space:{key:" ",code:"Space",keyCode:32}},n={"/":{code:"Slash",keyCode:191,shifted:"?"},".":{code:"Period",keyCode:190,shifted:">"},",":{code:"Comma",keyCode:188,shifted:"<"},";":{code:"Semicolon",keyCode:186,shifted:":"},"'":{code:"Quote",keyCode:222,shifted:'"'},"[":{code:"BracketLeft",keyCode:219,shifted:"{"},"]":{code:"BracketRight",keyCode:221,shifted:"}"},"\\":{code:"Backslash",keyCode:220,shifted:"|"},"-":{code:"Minus",keyCode:189,shifted:"_"},"=":{code:"Equal",keyCode:187,shifted:"+"},"`":{code:"Backquote",keyCode:192,shifted:"~"}},o={"?":"/",">":".","<":",",":":";",'"':"'","{":"[","}":"]","|":"\\",_:"-","+":"=","~":"`"},i=a.charAt(0).toUpperCase()+a.slice(1);if(i in r)return r[i];if(a.length===1){if(/[a-zA-Z]/.test(a)){const c=a.toUpperCase();return{key:t?c:a.toLowerCase(),code:`Key${c}`,keyCode:c.charCodeAt(0)}}if(/[0-9]/.test(a))return{key:a,code:`Digit${a}`,keyCode:a.charCodeAt(0)};if(a in n){const c=n[a];return{key:t?c.shifted:a,code:c.code,keyCode:c.keyCode}}if(a in o){const c=o[a],l=n[c];return{key:a,code:l.code,keyCode:l.keyCode}}const s=a.toUpperCase();return{key:a,code:`Key${s}`,keyCode:s.charCodeAt(0)}}return{key:a,code:a,keyCode:0}}class de{constructor(){g(this,"tabId",null);g(this,"tabs",[])}async getCurrentTabId(){if(this.tabId!==null)try{return await chrome.tabs.get(this.tabId),this.tabId}catch{this.tabId=null}const[e]=await chrome.tabs.query({active:!0,lastFocusedWindow:!0});if((e==null?void 0:e.id)!==void 0)return this.tabId=e.id,this.tabs.includes(e.id)||this.tabs.push(e.id),e.id;throw new Error("No active tab found")}setCurrentTab(e){this.tabId=e,this.tabs.includes(e)||this.tabs.push(e)}removeTab(e){this.tabs=this.tabs.filter(t=>t!==e),this.tabId===e&&(this.tabId=this.tabs[this.tabs.length-1]??null)}getTrackedTabs(){return[...this.tabs]}}const ue="src/offscreen/recording.html";function le(a){return new Promise((e,t)=>{chrome.tabCapture.getMediaStreamId({targetTabId:a},r=>{const n=chrome.runtime.lastError;n||!r?t(new Error((n==null?void 0:n.message)??"Failed to acquire tab media stream")):e(r)})})}async function he(a){try{const[e]=await chrome.scripting.executeScript({target:{tabId:a},func:()=>({width:Math.round(window.innerWidth*window.devicePixelRatio),height:Math.round(window.innerHeight*window.devicePixelRatio)})}),t=e==null?void 0:e.result;if(t&&t.width>0&&t.height>0)return t}catch{}return null}class me{constructor(e,t){g(this,"activeRecordingId",null);this.tabTracker=e,this.forward=t,chrome.runtime.onMessage.addListener(r=>{const n=r;(n==null?void 0:n.target)===$&&(this.forward(n),(n.type==="stopped"||n.type==="error")&&this.activeRecordingId===n.recordingId&&(this.activeRecordingId=null))})}async start(e){if(this.activeRecordingId)throw new Error("A recording is already in progress");const t=await this.tabTracker.getCurrentTabId(),r=await le(t),n=await he(t);await this.ensureOffscreenDocument();const o={target:A,type:"start",recordingId:e.recordingId,streamId:r,videoBitsPerSecond:e.videoBitsPerSecond,audio:e.audio,maxDurationMs:e.maxDurationMs,captureWidth:n==null?void 0:n.width,captureHeight:n==null?void 0:n.height};return await chrome.runtime.sendMessage(o),this.activeRecordingId=e.recordingId,{recordingId:e.recordingId,started:!0}}async stop(e){const t={target:A,type:"stop",recordingId:e};return await chrome.runtime.sendMessage(t),{recordingId:e,stopping:!0}}async ensureOffscreenDocument(){if(!await chrome.offscreen.hasDocument())try{await chrome.offscreen.createDocument({url:ue,reasons:[chrome.offscreen.Reason.USER_MEDIA],justification:"Record the agent browser tab to a video for demos and walkthroughs."})}catch(e){if(!await chrome.offscreen.hasDocument())throw e}}}const fe=async(a,e)=>{const t=await e.tabTracker.getCurrentTabId();return{image:await e.cdp.captureScreenshot(t),format:"jpeg"}},we=12e6,ge=480*6e4,pe=async(a,e)=>{const t=typeof a.recordingId=="string"?a.recordingId:crypto.randomUUID(),r=typeof a.videoBitsPerSecond=="number"?a.videoBitsPerSecond:we,n=typeof a.maxDurationMs=="number"?a.maxDurationMs:ge,o=a.audio===!0;return await e.recording.start({recordingId:t,videoBitsPerSecond:r,audio:o,maxDurationMs:n})},ye=async(a,e)=>{if(typeof a.recordingId!="string"||a.recordingId.length===0)throw new Error("recordingId is required to stop a recording");return await e.recording.stop(a.recordingId)},be=3e4;function S(a,e=be){return new Promise((t,r)=>{let n=null;const o=i=>{i.tabId===a&&i.frameId===0&&(n!==null&&clearTimeout(n),chrome.webNavigation.onCompleted.removeListener(o),t())};n=setTimeout(()=>{chrome.webNavigation.onCompleted.removeListener(o),r(new Error("Navigation timed out"))},e),chrome.webNavigation.onCompleted.addListener(o)})}const Ce=async(a,e)=>{const t=a.url;if(!t)throw new Error("navigate requires a url parameter");const r=await e.tabTracker.getCurrentTabId();return await e.cdp.navigate(r,t),await S(r),{success:!0,url:t}},Te=async(a,e)=>{const t=await e.tabTracker.getCurrentTabId();return await chrome.tabs.goBack(t),{success:!0}},ve=async(a,e)=>{const t=await e.tabTracker.getCurrentTabId();return await chrome.tabs.goForward(t),{success:!0}},Ee=async(a,e)=>{const t=await e.tabTracker.getCurrentTabId();return await chrome.tabs.reload(t),await S(t),{success:!0}};async function y(a,e,t){if(e.startsWith("ax_")){const u=Number(e.slice(3));if(!Number.isInteger(u))throw new Error(`Element ${e} is not a valid accessibility ref`);return await t.resolveBackendNodeCoordinates(a,u)}const r=await chrome.webNavigation.getAllFrames({tabId:a});if(!r||r.length===0)throw new Error(`Element ${e} not found. Re-run get_accessibility_tree to get fresh refs.`);const n=await ke(a,e,r);if(!n)throw new Error(`Element ${e} not found or garbage collected. Re-run get_accessibility_tree to get fresh refs.`);const{x:o,y:i,frameId:s}=n;if(s===0)return{x:o,y:i};const c=await t.getFrameTree(a),d=Me(r,c).get(s);if(!d)return{x:o,y:i};try{const u=await t.getFrameElementOffset(a,d);return{x:Math.round(o+u.x),y:Math.round(i+u.y)}}catch{return{x:o,y:i}}}async function ke(a,e,t){const r=await Promise.allSettled(t.map(async n=>{const[o]=await chrome.scripting.executeScript({target:{tabId:a,frameIds:[n.frameId]},func:_e,args:[e]});return{frameId:n.frameId,point:(o==null?void 0:o.result)??null}}));for(const n of r)if(n.status==="fulfilled"&&n.value.point!==null)return{x:n.value.point.x,y:n.value.point.y,frameId:n.value.frameId};return null}function _e(a){const e=window.__octavusElementMap;if(e===void 0)return null;const t=e[a];if(t===void 0)return null;const r=t.deref();if(!r)return null;const n=r.getBoundingClientRect();return{x:Math.round(n.left+n.width/2),y:Math.round(n.top+n.height/2)}}function Me(a,e){const t=new Map,r=new Map;for(const o of a){if(o.frameId===0)continue;const i=r.get(o.parentFrameId)??[];i.push(o),r.set(o.parentFrameId,i)}function n(o,i){t.set(o,i.frame.id);const s=[...i.childFrames??[]];for(const c of r.get(o)??[]){const l=s.findIndex(u=>u.frame.url===c.url);if(l===-1)continue;const[d]=s.splice(l,1);n(c.frameId,d)}}return n(0,e),t}const Ie=async(a,e)=>{const t=a.ref_id;if(!t)throw new Error("click requires a ref_id parameter");const r=await e.tabTracker.getCurrentTabId(),{x:n,y:o}=await y(r,t,e.cdp);return await e.cdp.click(r,n,o),{success:!0,ref_id:t,x:n,y:o}},Se=async(a,e)=>{const t=a.ref_id;if(!t)throw new Error("double_click requires a ref_id parameter");const r=await e.tabTracker.getCurrentTabId(),{x:n,y:o}=await y(r,t,e.cdp);return await e.cdp.doubleClick(r,n,o),{success:!0,ref_id:t,x:n,y:o}},De=async(a,e)=>{const t=a.ref_id;if(!t)throw new Error("right_click requires a ref_id parameter");const r=await e.tabTracker.getCurrentTabId(),{x:n,y:o}=await y(r,t,e.cdp);return await e.cdp.rightClick(r,n,o),{success:!0,ref_id:t,x:n,y:o}},Pe=async(a,e)=>{const t=a.ref_id;if(!t)throw new Error("hover requires a ref_id parameter");const r=await e.tabTracker.getCurrentTabId(),{x:n,y:o}=await y(r,t,e.cdp);return await e.cdp.hover(r,n,o),{success:!0,ref_id:t,x:n,y:o}},Re=async(a,e)=>{const t=a.from_ref_id,r=a.to_ref_id;if(!t)throw new Error("drag requires a from_ref_id parameter");if(!r)throw new Error("drag requires a to_ref_id parameter");const n=await e.tabTracker.getCurrentTabId(),o=await y(n,t,e.cdp),i=await y(n,r,e.cdp);return await e.cdp.drag(n,o.x,o.y,i.x,i.y),{success:!0,from_ref_id:t,to_ref_id:r}},xe=async(a,e)=>{const t=a.text;if(!t)throw new Error("type requires a text parameter");const r=await e.tabTracker.getCurrentTabId();return await e.cdp.type(r,t),{success:!0,text:t}},Ae=async(a,e)=>{const t=a.key;if(!t)throw new Error("key requires a key parameter");const r=await e.tabTracker.getCurrentTabId();return await e.cdp.keyPress(r,t),{success:!0,key:t}},Fe=500,Oe=async(a,e)=>{const t=a.direction;if(!t||t!=="up"&&t!=="down")throw new Error('scroll requires direction "up" or "down"');const r=a.amount??Fe,n=await e.tabTracker.getCurrentTabId();return await e.cdp.scrollPage(n,t,r),{success:!0,direction:t,amount:r}},Ne=async(a,e)=>{const t=a.filter??"all",r=a.maxDepth??15,n=a.maxLength??5e4,o=await e.tabTracker.getCurrentTabId();if(t==="interactive"){const d=await e.cdp.getAccessibilityTree(o,t,r,n);return d?{tree:d}:{tree:"Error: Could not generate accessibility tree"}}const i=await chrome.webNavigation.getAllFrames({tabId:o});if(!i||i.length===0)return{tree:"Error: Could not enumerate frames"};const s=await Promise.allSettled(i.map(async d=>{const[u]=await chrome.scripting.executeScript({target:{tabId:o,frameIds:[d.frameId]},func:(h,m,f)=>{const p=window.__octavusGenerateAccessibilityTree;return p===void 0?"":p(h,m,f)},args:[t,r,n]});return{frame:d,tree:(u==null?void 0:u.result)??""}})),c=[];for(const d of s){if(d.status!=="fulfilled")continue;const{frame:u,tree:h}=d.value;if(h){if(u.frameId!==0){const m=u.url?` (${u.url})`:"";c.push(`--- iframe${m} ---`)}c.push(h)}}const l=c.join(`
|
|
4
|
-
`);return l?{tree:l}:{tree:"Error: Could not generate accessibility tree"}},Le=async(a,e)=>({tabs:(await chrome.tabs.query({})).filter(r=>r.id!==void 0).map(r=>({tabId:r.id,title:r.title??"",url:r.url??"",active:r.active}))}),qe=async(a,e)=>{const t=a.url,r=await chrome.tabs.create({url:t??"about:blank",active:!0});if(r.id===void 0)throw new Error("Failed to create tab");return e.tabTracker.setCurrentTab(r.id),{success:!0,tabId:r.id,url:t??"about:blank"}},Be=async(a,e)=>{const t=a.tabId;if(t===void 0)throw new Error("switch_tab requires a tabId parameter");return await chrome.tabs.update(t,{active:!0}),e.tabTracker.setCurrentTab(t),{success:!0,tabId:t}},$e=async(a,e)=>{const t=a.tabId??await e.tabTracker.getCurrentTabId(),r=e.tabTracker.getTrackedTabs();if(r.length<=1&&r.includes(t))throw new Error("Cannot close the last tab.");return await e.cdp.detach(t),await chrome.tabs.remove(t),e.tabTracker.removeTab(t),{success:!0,closedTabId:t}},Ue=1e3,Ve=3e4,We=500,He=async(a,e)=>{const t=a.ms??Ue;if(a.for_navigation===!0){const r=await e.tabTracker.getCurrentTabId();return await S(r),{success:!0,waited_for:"navigation"}}return await new Promise(r=>{setTimeout(()=>r(),t)}),{success:!0,waited_ms:t}},je=async(a,e)=>{var d;const t=a.text;if(!Array.isArray(t)||t.length===0)throw new Error("wait_for requires a non-empty text array");const r=a.timeout??Ve,n=await e.tabTracker.getCurrentTabId(),i=`(() => {
|
|
5
|
-
const targets = ${JSON.stringify(t)};
|
|
6
|
-
const body = document.body?.innerText ?? '';
|
|
7
|
-
return targets.find(t => body.includes(t)) ?? null;
|
|
8
|
-
})()`;let s;const c=Date.now()+r;for(;Date.now()<c;){const{value:u,exceptionDetails:h}=await e.cdp.evaluateScript(n,i);if(h!==void 0){const m=h;s=((d=m.exception)==null?void 0:d.description)??m.text??"Script evaluation failed"}else{if(u!=null)return{success:!0,matchedText:u};s=void 0}await new Promise(m=>{setTimeout(()=>m(),We)})}const l=s?`Script error: ${s}`:`text not found: ${t.join(", ")}`;throw new Error(`Timed out after ${r}ms - ${l}`)},Ke=async(a,e)=>{const t=a.ref_id,r=a.value;if(!t)throw new Error("select_option requires a ref_id parameter");if(!r)throw new Error("select_option requires a value parameter");const n=await e.tabTracker.getCurrentTabId(),[o]=await chrome.scripting.executeScript({target:{tabId:n},func:(s,c)=>{var m;const l=window.__octavusElementMap;if(l===void 0)return{error:"Element map not available"};const d=l[s];if(d===void 0)return{error:`Element ${s} not found`};const u=d.deref();if(!u)return{error:`Element ${s} has been garbage collected`};if(!(u instanceof HTMLSelectElement))return{error:`Element ${s} is not a <select> element`};let h=!1;for(const f of u.options)if(f.value===c||((m=f.textContent)==null?void 0:m.trim())===c){f.selected=!0,h=!0;break}return h?(u.dispatchEvent(new Event("change",{bubbles:!0})),{success:!0,selected:c}):{error:`Option "${c}" not found in ${s}`,available:Array.from(u.options).map(f=>{var p;return{value:f.value,text:(p=f.textContent)==null?void 0:p.trim()}})}},args:[t,r]}),i=o==null?void 0:o.result;if(typeof(i==null?void 0:i.error)=="string")throw new Error(i.error);return i??{success:!0}},Xe=async(a,e)=>{const t=a.ref_id,r=a.value;if(!t)throw new Error("fill requires a ref_id parameter");if(r==null)throw new Error("fill requires a value parameter");const n=await e.tabTracker.getCurrentTabId(),{x:o,y:i}=await y(n,t,e.cdp);return await e.cdp.fill(n,o,i,r),{success:!0,ref_id:t}},Ge=async(a,e)=>{const t=a.action;if(t!=="accept"&&t!=="dismiss")throw new Error('handle_dialog requires action to be "accept" or "dismiss"');const r=a.promptText,n=await e.tabTracker.getCurrentTabId(),o=e.cdp.getPendingDialog(n);if(!o)throw new Error("No dialog is currently open on this tab");return await e.cdp.handleDialog(n,t==="accept",r),{success:!0,action:t,dialogType:o.type,dialogMessage:o.message}},Je=async(a,e)=>{var s;const t=a.function;if(!t)throw new Error("evaluate_script requires a function parameter");const r=`(${t})()`,n=await e.tabTracker.getCurrentTabId(),{value:o,exceptionDetails:i}=await e.cdp.evaluateScript(n,r);if(i!==void 0){const c=i,l=((s=c.exception)==null?void 0:s.description)??c.text??"Script evaluation failed";throw new Error(l)}return{success:!0,result:o}},ze=async(a,e)=>{const t=a.ref_id,r=a.filePath;if(!t)throw new Error("upload_file requires a ref_id parameter");if(!r)throw new Error("upload_file requires a filePath parameter");const n=await e.tabTracker.getCurrentTabId(),{x:o,y:i}=await y(n,t,e.cdp);return await e.cdp.uploadFile(n,o,i,[r]),{success:!0,ref_id:t,filePath:r}},Qe={screenshot:fe,start_recording:pe,stop_recording:ye,get_accessibility_tree:Ne,click:Ie,right_click:De,double_click:Se,hover:Pe,drag:Re,type:xe,key:Ae,fill:Xe,navigate:Ce,go_back:Te,go_forward:ve,reload:Ee,scroll:Oe,select_option:Ke,wait:He,wait_for:je,handle_dialog:Ge,evaluate_script:Je,upload_file:ze,list_tabs:Le,new_tab:qe,switch_tab:Be,close_tab:$e};async function Ze(a,e,t){const r=Qe[a];if(r===void 0)throw new Error(`Unknown command: ${a}`);return await r(e,t)}const Ye=2e4,et=1e3,tt=3e4,E=new z,D=new de,rt=new me(D,a=>I(a)),at={cdp:E,tabTracker:D,recording:rt};let w=null,k=null,M=0;async function nt(){const a=chrome.runtime.getURL("config.json"),e=await fetch(a);if(!e.ok)throw new Error(`Failed to load config.json: ${e.status}`);return await e.json()}function L(a){w=new WebSocket(`ws://127.0.0.1:${a}`),w.onopen=()=>{M=0,w.send(JSON.stringify({type:"ready"})),k=setInterval(()=>{(w==null?void 0:w.readyState)===WebSocket.OPEN&&w.send(JSON.stringify({type:"heartbeat"}))},Ye)},w.onmessage=e=>{ot(e.data)},w.onclose=()=>{it(),st(a)},w.onerror=()=>{}}async function ot(a){let e;try{e=JSON.parse(a)}catch{return}const{id:t,cmd:r,args:n}=e;if(!(!t||!r))try{const o=await Ze(r,n??{},at);I({id:t,result:o})}catch(o){const i=o==null?void 0:o.code,s=typeof i=="string"?i:void 0;I({id:t,error:o instanceof Error?o.message:String(o),...s!==void 0?{code:s}:{}})}}function I(a){(w==null?void 0:w.readyState)===WebSocket.OPEN&&w.send(JSON.stringify(a))}function it(){k&&(clearInterval(k),k=null)}function st(a){const e=Math.min(et*2**M,tt);M+=1,setTimeout(()=>L(a),e)}chrome.tabs.onRemoved.addListener(a=>{E.handleTabDetached(a),D.removeTab(a)});chrome.debugger.onDetach.addListener(a=>{a.tabId!==void 0&&E.handleTabDetached(a.tabId)});chrome.debugger.onEvent.addListener((a,e,t)=>{if(a.tabId===void 0)return;const r=t??{};e==="Page.javascriptDialogOpening"?E.onDialogOpening(a.tabId,r):e==="Page.fileChooserOpened"&&E.onFileChooserOpened(a.tabId,r)});nt().then(a=>L(a.wsPort)).catch(a=>console.error("[browser-extension] Failed to start:",a));
|