function T(i){try{typeof reportError=="function"?reportError(i):setTimeout(()=>{throw i},0)}catch{}}const M=13e4,O=2e3,x=Object.assign(Object.create(null),{authenticated:"connected",not_required:"connected",auth_required:"needs_reauth",token_invalid:"needs_reauth",refresh_failed:"needs_reauth",managed_auth_failed:"needs_reauth"});function I(i){const y=i.shellOrigin,v=t=>{const r=i.capBudgets?.mcp?.[t];return typeof r=="number"&&Number.isFinite(r)&&r>0?Math.min(r,6e5)+O:M};let h=0;const b=(()=>{try{const t=new Uint32Array(1);return crypto.getRandomValues(t),t[0].toString(36)}catch{return Math.random().toString(36).slice(2,10)}})(),s=new Map,l=new Map;addEventListener("message",t=>{if(t.source!==parent||t.origin!==y)return;const r=t.data;if(r&&r.__frame_mcp_watch===!0){const c=typeof r.watchId=="string"?r.watchId:null,a=c!==null?l.get(c):void 0;a&&A(a,r.ev);return}const e=t.data;if(!e||e.__frame_cap_r!==!0)return;const n=typeof e.id=="string"?e.id:null;if(!n)return;const o=s.get(n);o&&(s.delete(n),clearTimeout(o.timer),o.cleanup?.(),e.error!=null?o.reject(e.error):o.resolve(e.result))});const A=(t,r)=>{let e=r;const n=r;if(n&&n.type==="data")try{e={type:"data",result:w(n.result,n.server)}}catch(o){e={type:"error",error:o}}try{t(e)}catch(o){T(o)}},m=(t,r)=>{try{parent.postMessage({__frame_cap:!0,cap:"mcp",id:"c"+ ++h,method:t,args:r},y)}catch{}};addEventListener("pagehide",t=>{if(!t.persisted){for(const r of l.keys())m("unwatchTool",[r]);l.clear()}});const p=(t,r,e)=>new Promise((n,o)=>{const c="c"+ ++h;if(e?.aborted)return o({code:"cancelled",message:"call aborted"});const a=setTimeout(()=>{const u=s.get(c);s.delete(c),u?.cleanup?.(),o({code:"upstream_error",message:"no reply from shell"})},v(t));let f;if(e){const u=()=>{const d=s.get(c);d&&(s.delete(c),clearTimeout(d.timer),d.reject({code:"cancelled",message:"call aborted"}),m("cancelCall",[c]))};e.addEventListener("abort",u,{once:!0}),f=()=>e.removeEventListener("abort",u)}s.set(c,{resolve:n,reject:o,timer:a,cleanup:f});try{parent.postMessage({__frame_cap:!0,cap:"mcp",id:c,method:t,args:r},y)}catch{s.delete(c),clearTimeout(a),f?.(),o({code:"bad_request",message:"arguments are not structured-cloneable"})}}),S=new Set(["signal"]),g=t=>{try{if(typeof t=="object"&&t!==null&&!Array.isArray(t)){const r=Object.create(null);for(const[e,n]of Object.entries(t))if(!S.has(e))try{structuredClone(n),r[e]=n}catch{}return r}return structuredClone(t),t}catch{return}},w=(t,r)=>{if(t===null||typeof t!="object"||Array.isArray(t))return t;const e=t;if(e.isError===!0){const o=(Array.isArray(e.content)?e.content:[]).find(a=>!!a&&typeof a=="object"&&a.type==="text"),c=typeof o?.text=="string"?o.text:"tool reported an error";throw{code:"tool_error",...typeof r=="string"&&{server:r},message:c,result:t}}let n;if(e.structuredContent!==void 0)n=e.structuredContent;else{const o=(Array.isArray(e.content)?e.content:[]).find(c=>!!c&&typeof c=="object"&&c.type==="text");if(o){if(o.json!==void 0)n=o.json;else if(typeof o.text=="string")try{n=JSON.parse(o.text)}catch{n=o.text}}}return n===void 0?t:{...t,payload:n}},E=t=>{if(t===null||typeof t!="object")return t;const r=t.servers;return Array.isArray(r)?{...t,servers:r.map(e=>{if(!e||typeof e!="object")return e;const n=e.authStatus;return{...e,authStatus:typeof n=="string"&&x[n]||"unknown"}})}:t},_=i.pipe("mcp");i.mount("mcp",{callTool:_.wrap("callTool",(t,r,e,n)=>{let o;if(typeof n=="object"&&n!==null)try{const a=n.signal;a instanceof AbortSignal&&(o=a)}catch{}const c=n===void 0?void 0:g(n);return p("callTool",c===void 0?[t,r,e]:[t,r,e,c],o).then(a=>w(a,t))}),listTools:_.wrap("listTools",()=>p("listTools",[]).then(E)),invalidate:_.wrap("invalidate",(...t)=>p("invalidate",t)),watchTool:(t,r,e,n,o)=>{if(typeof n!="function")throw new TypeError("watchTool requires a handler function");const c=`w${b}-${++h}`;l.set(c,n);const a=g(o),f={...typeof a=="object"&&a!==null?a:null,watchId:c};return p("watchTool",[t,r,e,f]).catch(u=>{const d=l.get(c);if(d){l.delete(c);try{d({type:"error",error:u})}catch(j){T(j)}}}),()=>{l.delete(c)&&m("unwatchTool",[c])}}})}export{I as install};
