UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

15 lines (14 loc) • 8.53 kB
var H=new Set(["operator.read","operator.write"]);function t(f,_,c){let g=document.createElement(f);return _!==void 0&&(g.textContent=_),c&&(g.className=c),g}function W(f){return f.includes("operator.admin")?"Full access, including administration":f.some(_=>!H.has(_))?"Full role access":f.includes("operator.write")?"Read and write":"Read only"}var Y=(f,_)=>{let c=_.host,g=_,m,d,x=g.props.request??"",k=!1,v=!1,S="",R="",b=0,M=!1,V="",B=!1,L,p=t("section",void 0,"mcp-connected-apps"),A=t("div");f.append(p,A);let T=new Intl.DateTimeFormat(c.locale,{dateStyle:"medium",timeStyle:"short"}),q=()=>c.connection.connected&&c.connection.canRead,y=r=>!B&&!g.signal.aborted&&g.presented&&q()&&b===r,$=()=>d?.caller??m?.caller,G=(r=k)=>{let n=$()?.ceiling??[];return r?[...n]:n.filter(a=>H.has(a))},P=()=>{L?.dispose(),L=void 0};function w(r,n,a=!1){let o=t("button",r,a?"btn primary":"btn");return o.type="button",o.disabled=v||!q(),o.addEventListener("click",n),o}function j(r,n,a="mcp-relay-consent-access"){let o=$()?.ceiling??[],e=t("fieldset",void 0,"mcp-connected-apps__access");e.append(t("legend","App access"));let i=o.filter(l=>H.has(l)),s=[{full:!1,label:W(i),detail:i.includes("operator.write")?"Read conversations and send messages within your access.":"Read conversations within your access."}];o.some(l=>!H.has(l))&&s.push({full:!0,label:"Full role access",detail:o.includes("operator.admin")?"Includes Gateway administration and approvals. Only allow an app you trust with these actions.":"Includes all permissions currently allowed by your role."});for(let l of s){let C=t("label",void 0,"mcp-connected-apps__choice"),u=t("input");u.type="radio",u.name=a,u.value=l.full?"full":"read-write",u.checked=r===l.full,u.disabled=v,u.addEventListener("change",()=>{n(l.full)});let h=t("span");h.append(t("strong",l.label),t("small",l.detail)),C.append(u,h),e.append(C)}return e}async function I(r){if(v||!y(b))return;let n=++b;if(v=!0,S="",R="",m){p.querySelector(".mcp-connected-apps__error")?.remove(),p.querySelector(".mcp-connected-apps__feedback").textContent="Loading connected apps\u2026";for(let a of p.querySelectorAll("button, input"))a.disabled=!0}else D();try{await r(n)}catch(a){y(n)&&(S=c.redact(a instanceof Error?a.message:"The request failed. Try again."))}finally{y(n)&&(v=!1,D())}}function O(){return P(),I(async r=>{let n=await c.request("mcp-relay.grants",{});if(y(r)&&(m=n,d=void 0,x)){let a=await c.request("mcp-relay.authorize.describe",{requestId:x});y(r)&&(d=a)}})}function F(r,n){if(!y(b))return;let a=!1,o=t("div",void 0,"mcp-connected-apps mcp-connected-apps__dialog"),e,i=!1,s="",l=async()=>{if(i||!n.canPair||!y(b))return;let u=b;i=!0,s="",C();try{e=await c.request("mcp-relay.pair",{ceiling:G(a)})}catch(h){s=c.redact(h instanceof Error?h.message:"Pairing failed. Try again.")}y(u)&&L&&(i=!1,C())},C=()=>{if(o.replaceChildren(),o.append(t("h2","Pair with code"),t("p",`Connect an app as ${n.principalLabel}.`)),e){o.append(t("p","Enter this single-use code on the app\u2019s connection page.")),o.append(t("code",e.code,"mcp-connected-apps__code")),o.append(t("p",`Expires ${T.format(e.expiresAt)}. Keep this code private.`));let U=t("p");U.append(t("strong","MCP URL: "),t("code",e.mcpUrl)),o.append(U)}else{o.append(j(a,E=>{a=E,C()},"mcp-relay-pair-access"));let U=w(i?"Creating code\u2026":"Create pairing code",()=>{l()},!0);U.disabled=i||!G(a).length,o.append(U);for(let E of o.querySelectorAll("input"))E.disabled=i}let u=w(e?"Done":"Cancel",P);u.disabled=i;let h=t("div",void 0,"mcp-connected-apps__actions");if(h.append(u),o.append(h),s){let U=t("p",s,"mcp-connected-apps__error");U.setAttribute("role","alert"),o.append(U)}};C(),L=c.components.mountDialog(A,{label:"Pair with code",description:`Connect an app as ${n.principalLabel}.`,content:o,returnFocusTarget:r,onCancel:()=>i?!1:(P(),!0)})}function z(r){let n=x,a=m?.relayUrl;if(!d||!a)return;let o=G();I(async e=>{let i=await c.request(r?"mcp-relay.authorize.approve":"mcp-relay.authorize.deny",{requestId:n,...r?{ceiling:o}:{}});if(!y(e))return;let s=new URL(i.continueUrl);if(s.origin!==new URL(a).origin||s.pathname!=="/oauth/continue"||s.username||s.password||s.hash||s.searchParams.get("request")!==n)throw new Error("The relay returned an invalid return address. Reopen the app\u2019s connection page.");window.location.assign(s.href)})}function D(){if(p.innerHTML=` <header class="mcp-connected-apps__header"> <div><h1>Connected apps</h1><p>Manage apps connected to your Gateway through the MCP relay.</p></div> </header>`,p.querySelector("header").append(w("Refresh",()=>{O()})),!q()){p.append(t("p",c.connection.connected?"Read access is required to view connected apps.":"Connect to the Gateway to manage connected apps."));return}if(S){let e=t("p",S,"mcp-connected-apps__error");e.setAttribute("role","alert"),p.append(e)}let r=t("p",v?"Loading connected apps\u2026":R,"mcp-connected-apps__feedback");if(r.setAttribute("role","status"),p.append(r),!m)return;let n=m.caller;if(x&&d){let e=t("section",void 0,"mcp-connected-apps__consent");e.innerHTML=` <span class="mcp-connected-apps__eyebrow"></span><h2></h2> <dl class="mcp-connected-apps__details"> <div><dt>Approving as</dt><dd data-principal></dd></div> <div><dt>Returns to</dt><dd data-redirect></dd></div> <div><dt>Request expires</dt><dd data-expires></dd></div> </dl> <p class="mcp-connected-apps__hint">The app can act only within the access you allow and your current Gateway permissions. The relay can see content in transit.</p>`,e.querySelector("span").textContent=d.kind==="link"?"CONNECT ANOTHER GATEWAY":"CONNECTION REQUEST",e.querySelector("h2").textContent=`Connect ${d.clientName}?`,e.querySelector("[data-principal]").textContent=d.caller.principalLabel,e.querySelector("[data-redirect]").textContent=d.redirectHost,e.querySelector("[data-expires]").textContent=T.format(d.expiresAt),e.querySelector("p").before(j(k,l=>{k=l}));let i=t("div",void 0,"mcp-connected-apps__actions"),s=w("Allow",()=>z(!0),!0);s.disabled=v||!d.caller.canPair||!G().length,i.append(w("Deny",()=>z(!1)),s),e.append(i),d.caller.canPair||e.append(t("p","Your current Gateway permissions do not allow connecting an app.","mcp-connected-apps__hint")),p.append(e)}p.insertAdjacentHTML("beforeend",` <div class="mcp-connected-apps__toolbar"><p></p></div> <div class="mcp-connected-apps__list"></div> <p class="mcp-connected-apps__hint">Revoking an app ends its MCP access and embedded browser sessions. Reconnect the app to grant access again.</p>`);let a=p.querySelector(".mcp-connected-apps__toolbar");if(a.querySelector("p").textContent=n.canAdmin?"All connected apps":`Apps connected as ${n.principalLabel}`,n.canPair){let e=w("Pair with code",()=>F(e,n),!0);a.append(e)}let o=p.querySelector(".mcp-connected-apps__list");m.grants.length||(o.innerHTML="<h2>No connected apps yet</h2><p>Sign in with your Gateway from an app, or pair with a single-use code.</p>");for(let e of m.grants){let i=t("article",void 0,"mcp-connected-apps__grant");i.innerHTML='<div><h2></h2><p class="mcp-connected-apps__hint"></p><small></small></div>',i.querySelector("h2").textContent=e.clientName;let s=e.principal.kind===n.principal.kind&&(e.principal.kind==="owner"||n.principal.kind==="person"&&e.principal.profileId===n.principal.profileId),l=e.principal.kind==="owner"?"Gateway owner":e.principalLabel??(s?n.principalLabel:"Another person");i.querySelector("p").textContent=`${l} \xB7 ${W(e.ceiling)}`,i.querySelector("small").textContent=e.lastUsedAt!==void 0?`Last used ${T.format(e.lastUsedAt)}`:`Connected ${T.format(e.createdAt)} \xB7 Not used yet`,(s||n.canAdmin)&&c.connection.canWrite&&i.append(w("Revoke",()=>{I(async C=>{await c.request("mcp-relay.revoke",{grantId:e.grantId});let u=await c.request("mcp-relay.grants",{});y(C)&&(m=u,R=`${e.clientName} revoked. Its MCP and embedded UI access has ended.`)})})),o.append(i)}}function N(){let r=c.connection,n=`${r.connected}:${r.canRead}:${r.canWrite}`,a=n!==V;V=n;let o=g.props.request??"",e=q()&&g.presented&&!g.signal.aborted;if((e!==M||o!==x)&&(a=!0,M=e,x=o,b+=1,v=!1,m=void 0,d=void 0,k=!1,S="",R="",P(),e)){O();return}a&&D()}let Q=c.subscribe(N);return N(),{update(r){g=r,N()},dispose(){B=!0,b+=1,m=void 0,d=void 0,P(),Q(),p.replaceChildren(),A.replaceChildren(),p.remove(),A.remove()}}},ce={id:"mcp-relay",activate(f){return f.ui.registerPage({id:"connected-apps",label:"Connected apps",mount:Y})}};export{ce as default};