Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

4.2 KB
Raw Blame
// Browser-side MCP Apps lifecycle. Source-checked postMessage remains inside the host bridge.
export const MSO_PAGE_BRIDGE_SCRIPT = String.raw`
let hostConnected=false, hostContext={}, lastOutputKey="", viewCleanup=()=>{};
let lastPageSize="";
function reportPageSize(){
  if(document.documentElement.dataset.displayMode==="fullscreen")return;const root=document.querySelector(".surface");if(!root)return;
  const rect=root.getBoundingClientRect(),style=getComputedStyle(document.body);
  const height=Math.ceil(rect.height+(parseFloat(style.paddingTop)||0)+(parseFloat(style.paddingBottom)||0));
  const width=Math.ceil(document.documentElement.clientWidth),key=width+","+height;
  if(height<1||key===lastPageSize)return;lastPageSize=key;
  if(hostConnected)window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/size-changed",params:{width,height}},"*");
  else if(typeof window.openai?.notifyIntrinsicHeight==="function")window.openai.notifyIntrinsicHeight(height);
}
const pageResizeObserver=typeof ResizeObserver==="function"?new ResizeObserver(()=>requestAnimationFrame(reportPageSize)):null;

function rpcRequest(method,params){
  const id=nextRpcId++;
  return new Promise((resolve,reject)=>{
    const timer=setTimeout(()=>{pending.delete(id);reject(new Error("Host request timed out"))},10000);
    pending.set(id,{resolve,reject,timer});
    window.parent.postMessage({jsonrpc:"2.0",id,method,params},"*");
  });
}
function acceptPageResult(result){
  captureIntegrationAccess(result);captureIntegrationAccess(window.openai&&window.openai.toolResponseMetadata);
  const value=unbox(result);
  if(!value||typeof value!=="object"||!validRoute(value.route)||!["home","monitor","project","diff","browser","app","integrations","sessions","assets"].includes(value.kind))return false;
  if(value.kind==="app"&&!safeAppResult(value.app))return false;
  const key=JSON.stringify([value.route,value.kind,value.project,value.sha,value.app?.url,value.setup?.expiresAt]);
  if(key===lastOutputKey){if(value.kind==="integrations"&&value.setup&&integrationAccess&&!body.querySelector("form")){current=value;render()}return true;}
  lastOutputKey=key;current=value;render();return true;
}
function acceptPageInput(params){
  if(lastOutputKey)return;
  const args=params&&params.arguments;
  if(args&&validRoute(args.route)){routeEl.textContent=args.route;body.replaceChildren(el("div","loading","Waiting for page data…"))}
}
function onHostMessage(event){
  if(event.source!==window.parent)return;
  const msg=event.data;if(!msg||msg.jsonrpc!=="2.0")return;
  if(msg.id!==undefined&&pending.has(msg.id)){
    const request=pending.get(msg.id);pending.delete(msg.id);clearTimeout(request.timer);
    msg.error?request.reject(new Error(text(msg.error.message,"Host request failed"))):request.resolve(msg.result);return;
  }
  if(msg.method==="ui/notifications/tool-result"){if(msg.params?.isError)showError(new Error("The page tool reported an error. Retry from chat."));else if(!acceptPageResult(msg.params))showError(new Error("The host returned invalid page data."))}
  if(msg.method==="ui/notifications/tool-input")acceptPageInput(msg.params);
  if(msg.method==="ui/notifications/host-context-changed"){hostContext={...hostContext,...msg.params};applyHostGlobals()}
  if(msg.method==="ui/resource-teardown"&&msg.id!==undefined){viewCleanup();pageResizeObserver?.disconnect();window.parent.postMessage({jsonrpc:"2.0",id:msg.id,result:{}},"*")}
}
async function initializeMcpPage(){
  try{
    const result=await rpcRequest("ui/initialize",{
      protocolVersion:"2026-01-26",
      appInfo:{name:"MSO Page",version:"3.0.0"},
      appCapabilities:{availableDisplayModes:["inline","fullscreen","pip"]}
    });
    if(!result||typeof result.protocolVersion!=="string")throw new Error("Invalid host initialization response");
    hostConnected=true;lastPageSize="";hostContext=result.hostContext||{};applyHostGlobals();
    window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/initialized",params:{}},"*");
    const root=document.querySelector(".surface");if(root)pageResizeObserver?.observe(root);reportPageSize();
  }catch(error){if(!lastOutputKey)showError(new Error("The chat host did not initialize the page. Reopen the preview or refresh the connector."))}
}
`;