toc-ocra 0.3.10 → 0.3.12
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 -6
- package/dist/cli/cli.mjs +167 -291
- package/dist/plugin/renderer.js +1 -1
- package/dist/plugin/renderer.js.map +3 -3
- package/dist/service/agent.mjs +159 -282
- package/dist/src/acp/acp-launch-specs.js +9 -0
- package/dist/src/acp/acp-launch-specs.js.map +1 -1
- package/dist/src/index.js +4 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/platform/win32.js +84 -12
- package/dist/src/platform/win32.js.map +1 -1
- package/dist/src/providers/cli-specs.js +1 -0
- package/dist/src/providers/cli-specs.js.map +1 -1
- package/dist/src/providers/detection.js +10 -6
- package/dist/src/providers/detection.js.map +1 -1
- package/dist/src/providers/provider.js +1 -1
- package/dist/src/providers/provider.js.map +1 -1
- package/dist/src/runtime/agent-runtime.d.ts +1 -2
- package/dist/src/runtime/agent-runtime.js +8 -11
- package/dist/src/runtime/agent-runtime.js.map +1 -1
- package/dist/src/runtime/agent-service.js +4 -2
- package/dist/src/runtime/agent-service.js.map +1 -1
- package/dist/src/shared/node-cli-command-resolution.js +5 -1
- package/dist/src/shared/node-cli-command-resolution.js.map +1 -1
- package/dist/src/types/protocol.d.ts +4 -23
- package/dist/src/types/protocol.js +4 -11
- package/dist/src/types/protocol.js.map +1 -1
- package/dist/src/workspace/directory-browser.d.ts +4 -0
- package/dist/src/workspace/directory-browser.js +43 -5
- package/dist/src/workspace/directory-browser.js.map +1 -1
- package/dist/src/workspace/workspace-manager.d.ts +1 -12
- package/dist/src/workspace/workspace-manager.js +5 -216
- package/dist/src/workspace/workspace-manager.js.map +1 -1
- package/locales/en.json +1 -0
- package/locales/zh-CN.json +1 -0
- package/package.json +1 -1
- package/scripts/service.mjs +48 -12
package/dist/plugin/renderer.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var Ve=Object.create;var Se=Object.defineProperty;var Ge=Object.getOwnPropertyDescriptor;var Xe=Object.getOwnPropertyNames;var Qe=Object.getPrototypeOf,Ze=Object.prototype.hasOwnProperty;var ye=(e,r)=>()=>{try{return r||e((r={exports:{}}).exports,r),r.exports}catch(i){throw r=0,i}};var er=(e,r,i,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let u of Xe(r))!Ze.call(e,u)&&u!==i&&Se(e,u,{get:()=>r[u],enumerable:!(n=Ge(r,u))||n.enumerable});return e};var D=(e,r,i)=>(i=e!=null?Ve(Qe(e)):{},er(r||!e||!e.__esModule?Se(i,"default",{value:e,enumerable:!0}):i,e));var X=ye((Tr,Te)=>{var Pe=globalThis.__TOC_PLUGIN_SHARED__,xe=Pe&&Pe.react;if(!xe)throw new Error("The host does not provide react; this plugin needs a newer app version.");Te.exports=xe});var ie=ye((Lr,Re)=>{var Ee=globalThis.__TOC_PLUGIN_SHARED__,Be=Ee&&Ee["@toc-starter/plugin-sdk/ui"];if(!Be)throw new Error("The host does not provide @toc-starter/plugin-sdk/ui; this plugin needs a newer app version.");Re.exports=Be});var H=ye((Dr,Ae)=>{var De=globalThis.__TOC_PLUGIN_SHARED__,Oe=De&&De["react/jsx-runtime"];if(!Oe)throw new Error("The host does not provide react/jsx-runtime; this plugin needs a newer app version.");Ae.exports=Oe});var S=D(X(),1);var Ie=D(X(),1);function Le(e){if(!e||typeof e.activate!="function")throw new TypeError("definePlugin() needs an object with an activate(context) function");return e}function we(e,r){return(0,Ie.useSyncExternalStore)(i=>e.core.onContextChange(r,i),()=>e.core.context(r))}var g=D(ie(),1);var pe="http://127.0.0.1:17339",z=class extends Error{constructor(i,n,u=""){super(n);this.kind=i;this.code=u}kind;code};async function Q(e,r,i={}){let n=new Headers(i.headers);i.token&&n.set("Authorization",`Bearer ${i.token}`),i.body&&n.set("Content-Type","application/json");let u;try{u=await fetch(`${e.replace(/\/+$/,"")}${r}`,{...i,headers:n,cache:"no-store"})}catch{throw new z("offline","local service unreachable")}let h=await u.json().catch(()=>null);if(u.status===401)throw new z("unauthorized","unauthorized");if(!u.ok){let c=typeof h?.error=="string"?h.error:"",f=typeof h?.message=="string"?h.message:c||`HTTP ${u.status}`;throw new z("rejected",f,c)}return h}var q={info:e=>Q(e,"/control/info"),pair:(e,r)=>Q(e,"/control/pair",{method:"POST",body:JSON.stringify({code:r})}),status:(e,r)=>Q(e,"/control/status",{token:r}),enroll:(e,r,i,n)=>Q(e,"/control/enroll",{method:"POST",token:r,body:JSON.stringify({server_url:i,code:n})}),unpair:(e,r)=>Q(e,"/control/unpair",{method:"POST",token:r}),detect:(e,r)=>Q(e,"/control/detect",{method:"POST",token:r})};function me(e,r){let i=n=>{try{return new URL(n).origin.toLowerCase()}catch{return n.trim().replace(/\/+$/,"").toLowerCase()}};return!!(e&&r)&&i(e)===i(r)}var E=D(X(),1),C=D(ie(),1);var Ue=D(X(),1),$e=D(ie(),1);var ae=D(H(),1),F="controlToken",ue="localUrl",oe="autoBind",ve="localService";function ne(e,r){return r instanceof z&&r.kind==="offline"?e.t("error.offline"):r instanceof Error?r.message:String(r)}function V({ctx:e,value:r}){let[i,n]=(0,Ue.useState)(!1);return(0,ae.jsxs)("div",{className:"da-command",children:[(0,ae.jsx)("code",{children:r}),(0,ae.jsx)($e.Btn,{onClick:()=>{navigator.clipboard.writeText(r).then(()=>{n(!0),window.setTimeout(()=>n(!1),1500)})},children:i?e.t("common.copied"):e.t("common.copy")})]})}var o=D(H(),1),fe=["linux","mac","win"],rr=1e4,tr=3e3,ge="remoteServerUrl",ir=new Set(["127.0.0.1","localhost","[::1]","::1","0.0.0.0"]);function ar(e){try{let r=new URL(e.trim());return r.protocol==="http:"||r.protocol==="https:"?r.origin:null}catch{return null}}function or(e){try{return ir.has(new URL(e).hostname)}catch{return!1}}function nr(e,r,i){let n=`toc-ocra setup --code ${r} --server ${i}`;return e==="win"?`npm install -g toc-ocra@latest; if ($?) { ${n} }`:`npm install -g toc-ocra@latest && ${n}`}function dr({ctx:e,device:r}){return r.status==="online"?(0,o.jsx)(C.Badge,{variant:"ok",children:e.t("remote.status.online")}):r.status==="disabled"?(0,o.jsx)(C.Badge,{variant:"warn",children:e.t("remote.status.disabled")}):r.status==="offline"?(0,o.jsx)(C.Badge,{variant:"err",children:e.t("remote.status.offline")}):(0,o.jsx)(C.Badge,{variant:"muted",children:e.t("remote.status.notConnected")})}function ze({ctx:e,server:r,workspace:i,localDeviceId:n}){let[u,h]=(0,E.useState)(null),[c,f]=(0,E.useState)(""),[v,l]=(0,E.useState)(!1),[b,d]=(0,E.useState)(null),[k,y]=(0,E.useState)(!1),[O,K]=(0,E.useState)(""),[L,B]=(0,E.useState)("linux"),[P,w]=(0,E.useState)(()=>e.storage.get(ge,"")||r?.origin||""),U=(0,E.useRef)(b);U.current=b;let R=(0,E.useRef)(!0);(0,E.useEffect)(()=>{!e.storage.get(ge,"")&&r?.origin&&w(r.origin)},[e,r?.origin]);let $=(0,E.useCallback)(async()=>{if(i?.id)try{let s=await e.core.request(`/workspaces/${encodeURIComponent(i.id)}/devices`);if(!R.current)return;let x=(s.items??[]).filter(M=>M.is_mine&&M.id!==n);h(x);let W=U.current;if(W&&!W.matched){let M=x.find(te=>te.id===W.deviceId);M?.agent_id&&M.agent_id!==W.baseline&&M.terminal_online&&(d({...W,matched:!0}),e.notify({title:e.t("page.title"),body:e.t("remote.matchedNotice",{name:M.name})}))}}catch(s){R.current&&f(ne(e,s))}},[e,n,i?.id]),Y=!!(b&&!b.matched);(0,E.useEffect)(()=>{R.current=!0,$();let s=window.setInterval(()=>{$()},Y?tr:rr);return()=>{R.current=!1,window.clearInterval(s)}},[$,Y]);let p=async s=>{l(!0),f("");try{await s()}catch(x){f(ne(e,x))}finally{l(!1)}},_=ar(P),A=s=>{w(s),s.trim()&&s.trim()!==r?.origin?e.storage.set(ge,s.trim()):e.storage.remove(ge)},j=s=>p(async()=>{if(!_)throw new Error(e.t("remote.serverInvalid"));let x=await e.core.request(`/devices/${encodeURIComponent(s.id)}/pairing-code`,{method:"POST"});d({deviceId:s.id,deviceName:s.name,platform:fe.includes(s.platform)?s.platform:"linux",code:x.code,expiresAt:x.expires_at,baseline:s.agent_id??null,matched:!1})}),J=()=>p(async()=>{if(!i?.id)throw new Error(e.t("bind.noWorkspace"));if(!_)throw new Error(e.t("remote.serverInvalid"));let s=O.trim()||e.t(`remote.platform.${L}`),x=await e.core.request(`/workspaces/${encodeURIComponent(i.id)}/devices`,{method:"POST",body:JSON.stringify({name:s,location:L==="linux"?"cloud":"local",platform:L,note:"",share_to_workspace:!1})}),W=await e.core.request(`/devices/${encodeURIComponent(x.id)}/pairing-code`,{method:"POST"});d({deviceId:x.id,deviceName:x.name||s,platform:L,code:W.code,expiresAt:W.expires_at,baseline:null,matched:!1}),y(!1),K(""),await $()}),re=(s,x)=>p(async()=>{await e.core.request(`/devices/${encodeURIComponent(s.id)}/enabled`,{method:"PUT",body:JSON.stringify({enabled:x})}),await $()});return(0,o.jsxs)(o.Fragment,{children:[c&&(0,o.jsx)("div",{role:"alert",className:"da-alert",children:c}),(0,o.jsxs)(C.Card,{children:[(0,o.jsx)(C.CardTitle,{children:e.t("remote.title")}),(0,o.jsx)("p",{className:"da-text",children:e.t("remote.body")}),(0,o.jsxs)("label",{className:"da-field",children:[(0,o.jsx)("span",{children:e.t("remote.server")}),(0,o.jsx)(C.Input,{value:P,onChange:s=>A(s.target.value),spellCheck:!1,className:"da-mono"})]}),!_&&(0,o.jsx)("p",{className:"da-muted da-warn",children:e.t("remote.serverInvalid")}),_&&or(_)&&(0,o.jsx)("p",{className:"da-muted da-warn",children:e.t("remote.loopback")}),!k&&(0,o.jsx)("div",{className:"da-row",children:(0,o.jsx)(C.Btn,{primary:!0,disabled:v||!i?.id,onClick:()=>y(!0),children:e.t("remote.add")})}),k&&(0,o.jsxs)("form",{className:"da-form",onSubmit:s=>{s.preventDefault(),J()},children:[(0,o.jsxs)("label",{className:"da-field",children:[(0,o.jsx)("span",{children:e.t("remote.platformLabel")}),(0,o.jsx)("select",{className:"da-select",value:L,onChange:s=>B(s.target.value),children:fe.map(s=>(0,o.jsx)("option",{value:s,children:e.t(`remote.platform.${s}`)},s))})]}),(0,o.jsxs)("label",{className:"da-field",children:[(0,o.jsx)("span",{children:e.t("remote.name")}),(0,o.jsx)(C.Input,{value:O,onChange:s=>K(s.target.value),maxLength:100,placeholder:e.t("remote.namePlaceholder")})]}),(0,o.jsxs)("div",{className:"da-row",children:[(0,o.jsx)(C.Btn,{primary:!0,type:"submit",disabled:v||!_,children:e.t(v?"remote.working":"remote.generate")}),(0,o.jsx)(C.Btn,{type:"button",disabled:v,onClick:()=>y(!1),children:e.t("remote.cancel")})]})]})]}),b&&_&&(0,o.jsx)(sr,{ctx:e,pending:b,origin:_,busy:v,onPlatform:s=>d({...b,platform:s}),onRegenerate:()=>{j({id:b.deviceId,name:b.deviceName,platform:b.platform,agent_id:b.baseline})},onClose:()=>d(null)}),(0,o.jsxs)(C.Card,{children:[(0,o.jsx)(C.CardTitle,{children:e.t("remote.listTitle")}),u===null&&(0,o.jsx)("p",{className:"da-muted",children:e.t("common.checking")}),u?.length===0&&(0,o.jsx)("p",{className:"da-muted",children:e.t("remote.empty")}),u&&u.length>0&&(0,o.jsx)("ul",{className:"da-devices",children:u.map(s=>(0,o.jsxs)("li",{children:[(0,o.jsxs)("div",{className:"da-device-main",children:[(0,o.jsx)("span",{className:"da-device-name",children:s.name}),(0,o.jsxs)("span",{className:"da-muted",children:[e.t(`remote.platform.${fe.includes(s.platform)?s.platform:"linux"}`),s.agent_version?` \xB7 toc-ocra ${s.agent_version}`:""]})]}),(0,o.jsx)(dr,{ctx:e,device:s}),(0,o.jsxs)("div",{className:"da-device-actions",children:[(!s.paired||s.status==="offline"||s.status==="not_connected")&&(0,o.jsx)(C.Btn,{disabled:v||!_,onClick:()=>{j(s)},children:e.t(s.paired?"remote.repair":"remote.install")}),s.paired&&(0,o.jsxs)("label",{className:"da-toggle",children:[(0,o.jsx)("span",{children:e.t("remote.enabled")}),(0,o.jsx)(C.Toggle,{checked:!s.disabled,disabled:v,label:e.t("remote.enabled"),onChange:x=>{re(s,x)}})]})]})]},s.id))}),(0,o.jsx)("p",{className:"da-muted",children:e.t("remote.disableHint")}),(0,o.jsx)("div",{className:"da-step",children:e.t("remote.uninstall")}),(0,o.jsx)(V,{ctx:e,value:"toc-ocra service:uninstall"})]})]})}function sr({ctx:e,pending:r,origin:i,busy:n,onPlatform:u,onRegenerate:h,onClose:c}){let[f,v]=(0,E.useState)(()=>Math.floor(Date.now()/1e3));(0,E.useEffect)(()=>{if(r.matched)return;let d=window.setInterval(()=>v(Math.floor(Date.now()/1e3)),1e3);return()=>window.clearInterval(d)},[r.matched]);let l=Math.max(0,r.expiresAt-f),b=!r.matched&&l===0;return(0,o.jsxs)(C.Card,{children:[(0,o.jsx)(C.CardTitle,{children:e.t("remote.pendingTitle",{name:r.deviceName})}),r.matched?(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)("p",{className:"da-text",children:[(0,o.jsx)(C.Badge,{variant:"ok",children:e.t("remote.matched")})," ",e.t("remote.matchedBody",{name:r.deviceName})]}),(0,o.jsx)("div",{className:"da-row",children:(0,o.jsx)(C.Btn,{primary:!0,onClick:c,children:e.t("remote.done")})})]}):(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)("p",{className:"da-text",children:e.t("remote.pendingBody")}),(0,o.jsx)("div",{className:"da-segment",role:"radiogroup","aria-label":e.t("remote.platformLabel"),children:fe.map(d=>(0,o.jsx)("button",{type:"button",role:"radio","aria-checked":r.platform===d,className:r.platform===d?"da-seg da-seg-active":"da-seg",onClick:()=>u(d),children:e.t(`remote.platform.${d}`)},d))}),(0,o.jsx)(V,{ctx:e,value:nr(r.platform,r.code,i)}),(0,o.jsxs)("ul",{className:"da-hints",children:[(0,o.jsx)("li",{children:e.t("remote.hintNode")}),r.platform==="linux"&&(0,o.jsx)("li",{children:e.t("remote.hintLinux")}),r.platform==="win"&&(0,o.jsx)("li",{children:e.t("remote.hintWindows")}),(0,o.jsx)("li",{children:b?e.t("remote.expired"):e.t("remote.expires",{minutes:Math.floor(l/60),seconds:String(l%60).padStart(2,"0")})})]}),(0,o.jsx)("p",{className:"da-muted",children:b?"":e.t("remote.waiting")}),(0,o.jsxs)("div",{className:"da-row",children:[(0,o.jsx)(C.Btn,{disabled:n,onClick:h,children:e.t("remote.regenerate")}),(0,o.jsx)(C.Btn,{disabled:n,onClick:c,children:e.t("remote.cancel")})]})]})]})}var T=D(X(),1),N=D(ie(),1),a=D(H(),1),Me=[{id:"codex",label:"Codex"},{id:"claude",label:"Claude Code"},{id:"gemini",label:"Gemini CLI"}],_e="tb-switch-device";function Z(e){return e.terminal_online??e.online??e.status==="online"}function qe(e){return!e.paired||e.status==="not_connected"?{key:"provider.status.notPaired",variant:"muted"}:e.disabled||e.status==="disabled"?{key:"provider.status.paused",variant:"warn"}:Z(e)?{key:"provider.status.online",variant:"ok"}:{key:"provider.status.offline",variant:"err"}}function lr(e,r){return e.find(i=>i.id===r&&i.paired&&!i.disabled&&Z(i))??e.find(i=>i.is_mine&&i.paired&&!i.disabled&&Z(i))??e.find(i=>i.paired&&!i.disabled&&Z(i))??e.find(i=>i.id===r)??e.find(i=>i.paired)??e[0]}function Ce(e){return e instanceof Error?e.message:String(e)}function Ke({ctx:e,workspace:r,localDeviceId:i}){let[n,u]=(0,T.useState)(null),[h,c]=(0,T.useState)(!1),[f,v]=(0,T.useState)(""),[l,b]=(0,T.useState)({workspaceId:"",deviceId:""}),d=(0,T.useRef)(l);d.current=l;let k=l.workspaceId===r?.id?l.deviceId:"",y=n?.find(m=>m.id===k)??null,[O,K]=(0,T.useState)("codex"),[L,B]=(0,T.useState)(null),[P,w]=(0,T.useState)(!1),[U,R]=(0,T.useState)(""),[$,Y]=(0,T.useState)(!1),[p,_]=(0,T.useState)(""),[A,j]=(0,T.useState)(null),J=(0,T.useRef)(0),re=!!(y&&(y.is_mine||r?.role==="owner"||r?.role==="admin")),s=!!(y?.paired&&!y.disabled&&y.status!=="disabled"&&Z(y)),x=(0,T.useCallback)(async(m=!1)=>{if(!r?.id){u([]);let I={workspaceId:"",deviceId:""};d.current=I,b(I);return}m||c(!0),v("");try{let G=(await e.core.request(`/workspaces/${encodeURIComponent(r.id)}/devices`)).items??[];u(G);let le=d.current,je=le.workspaceId===r.id?le.deviceId:"";if(G.some(he=>he.id===je))return;let Ne="";try{Ne=e.storage.get(`${_e}:${r.id}`,"")}catch{}let Je=G.find(he=>he.id===Ne)??lr(G,i),ce={workspaceId:r.id,deviceId:Je?.id??""};if(d.current=ce,b(ce),ce.deviceId)try{e.storage.set(`${_e}:${r.id}`,ce.deviceId)}catch{}}catch(I){v(Ce(I))}finally{m||c(!1)}},[e,i,r?.id]);(0,T.useEffect)(()=>{x();let m=window.setInterval(()=>{x(!0)},15e3);return()=>window.clearInterval(m)},[x]);let W=m=>{let I={workspaceId:r?.id??"",deviceId:m};if(d.current=I,b(I),_(""),R(""),r?.id)try{e.storage.set(`${_e}:${r.id}`,m)}catch{}},M=y?.id??"",te=(0,T.useCallback)(async()=>{if(!r?.id||!M||!s)return;let m=++J.current;w(!0),R("");try{let I=await e.core.request(`/workspaces/${encodeURIComponent(r.id)}/devices/${encodeURIComponent(M)}/cli-providers/control`,{method:"POST",body:JSON.stringify({app:O,action:"state"})});m===J.current&&B(I)}catch(I){m===J.current&&(B(null),R(Ce(I)))}finally{m===J.current&&w(!1)}},[O,e,s,M,r?.id]);(0,T.useEffect)(()=>{J.current+=1,w(!1),B(null),_(""),s&&te()},[te,s]);let se=async m=>{if(!(!r?.id||!y||!s||m.action!=="state"&&!re||$)){Y(!0),_(""),R("");try{let I=await e.core.request(`/workspaces/${encodeURIComponent(r.id)}/devices/${encodeURIComponent(y.id)}/cli-providers/control`,{method:"POST",body:JSON.stringify({app:O,...m})});B(I),_(m.action==="state"?e.t("provider.refreshed"):e.t("provider.updated"))}catch(I){R(Ce(I))}finally{Y(!1)}}},be=y?qe(y):null,Ye=r?.role==="owner"||r?.role==="admin",ke=n??[];return(0,a.jsxs)("div",{className:"da-provider-page",children:[(0,a.jsxs)("div",{className:"da-provider-toolbar",children:[(0,a.jsxs)("div",{className:"da-provider-device",children:[(0,a.jsx)("label",{htmlFor:"da-provider-device-select",children:e.t("provider.chooseDevice")}),(0,a.jsxs)("select",{id:"da-provider-device-select",className:"da-select",value:k,onChange:m=>W(m.target.value),disabled:h||ke.length===0||$,children:[(0,a.jsx)("option",{value:"",disabled:!0,children:e.t("provider.selectDevice")}),ke.map(m=>{let I=qe(m),G=m.is_mine?e.t("provider.myDevice"):e.t("provider.owner",{owner:m.owner_name||"\u2014"}),le=m.id===i?` \xB7 ${e.t("provider.thisDevice")}`:"";return(0,a.jsxs)("option",{value:m.id,children:[m.name," \xB7 ",e.t(I.key)," \xB7 ",G,le]},m.id)})]}),y&&be&&(0,a.jsx)(N.Badge,{variant:be.variant,children:e.t(be.key)})]}),(0,a.jsx)(N.Btn,{disabled:h,onClick:()=>{x()},children:h?e.t("provider.loading"):e.t("provider.refreshDevices")})]}),h&&n===null&&(0,a.jsx)(N.Card,{children:(0,a.jsx)("span",{className:"da-muted",children:e.t("provider.loadingDevices")})}),f&&(0,a.jsxs)("div",{role:"alert",className:"da-alert",children:[f,(0,a.jsx)(N.Btn,{disabled:h,onClick:()=>{x()},children:e.t("provider.retry")})]}),!r?.id&&(0,a.jsx)(N.Card,{children:(0,a.jsx)(N.CardTitle,{children:e.t("provider.noWorkspace")})}),r?.id&&n?.length===0&&!h&&(0,a.jsxs)(N.Card,{children:[(0,a.jsx)(N.CardTitle,{children:e.t("provider.noDevices")}),(0,a.jsx)("p",{className:"da-text",children:e.t("provider.noDevicesDescription")})]}),y&&(0,a.jsxs)(N.Card,{children:[(0,a.jsxs)("div",{className:"da-provider-heading",children:[(0,a.jsxs)("div",{children:[(0,a.jsx)(N.CardTitle,{children:e.t("provider.title")}),(0,a.jsx)("p",{className:"da-text",children:e.t("provider.description",{name:y.name})})]}),(0,a.jsxs)("div",{className:"da-provider-app-select",children:[(0,a.jsx)("label",{htmlFor:"da-provider-app-select",children:e.t("provider.cli")}),(0,a.jsx)("select",{id:"da-provider-app-select",className:"da-select",value:O,onChange:m=>{K(m.target.value),_("")},disabled:!s||$,children:Me.map(m=>(0,a.jsx)("option",{value:m.id,children:m.label},m.id))}),(0,a.jsx)(N.Btn,{disabled:!s||P||$,onClick:()=>{se({action:"state"})},children:P||$?e.t("provider.loading"):e.t("provider.refresh")})]})]}),y.paired?y.disabled||y.status==="disabled"?(0,a.jsx)("div",{className:"da-provider-message da-provider-warn",children:e.t("provider.paused")}):Z(y)?P&&!L?(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.loadingState",{app:Me.find(m=>m.id===O)?.label})}):U?(0,a.jsxs)("div",{role:"alert",className:"da-provider-message da-provider-error",children:[U,(0,a.jsx)(N.Btn,{disabled:P,onClick:()=>{te()},children:e.t("provider.retry")})]}):L?(0,a.jsx)(cr,{ctx:e,state:L,canEdit:re,busy:$,onProvider:m=>{se({action:"switch_provider",provider_id:m})},onMode:m=>j(m),onRoute:m=>{se({action:"select_route",route_id:m})}}):(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.selectHint")}):(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.offline")}):(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.notPaired")}),y.paired&&!re&&(0,a.jsx)("p",{className:"da-muted",children:Ye?e.t("provider.readOnly"):e.t("provider.ownerOnly")}),p&&(0,a.jsx)("div",{role:"status",className:"da-provider-notice",children:p}),L?.warnings?.length?(0,a.jsxs)("div",{className:"da-provider-warnings",children:[(0,a.jsx)("strong",{children:e.t("provider.warnings")}),(0,a.jsx)("ul",{children:L.warnings.map((m,I)=>(0,a.jsx)("li",{children:m},`${I}:${m}`))})]}):null]}),(0,a.jsx)(N.SystemModal,{open:!!A,title:e.t("provider.confirmModeTitle"),description:A==="multi"?e.t("provider.confirmMulti"):e.t("provider.confirmSingle"),onClose:()=>j(null),footer:(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(N.Btn,{onClick:()=>j(null),children:e.t("provider.cancel")}),(0,a.jsx)(N.Btn,{primary:!0,disabled:$,onClick:()=>{let m=A;j(null),m&&se({action:"set_mode",mode:m})},children:e.t("provider.confirm")})]}),children:(0,a.jsx)("p",{className:"da-text",children:e.t("provider.confirmModeHelp")})})]})}function cr({ctx:e,state:r,canEdit:i,busy:n,onProvider:u,onMode:h,onRoute:c}){let f=r.routes.filter(d=>d.enabled),v=r.providers.find(d=>d.id===r.current_provider_id)?.name,l=d=>e.t(d==="single"?"provider.single":"provider.multi"),b=!r.multi_supported||f.length===0;return(0,a.jsxs)("div",{className:"da-provider-controls",children:[(0,a.jsxs)("section",{children:[(0,a.jsx)("div",{className:"da-provider-section-title",children:e.t("provider.mode")}),(0,a.jsx)("div",{className:"da-provider-mode-grid",children:["single","multi"].map(d=>{let k=r.mode===d;return(0,a.jsxs)("button",{type:"button",className:k?"da-provider-mode da-provider-mode-active":"da-provider-mode",disabled:!i||n||k||d==="multi"&&b,onClick:()=>h(d),children:[(0,a.jsx)("span",{className:k?"da-provider-radio da-provider-radio-active":"da-provider-radio",children:k?"\u2713":""}),(0,a.jsxs)("span",{children:[(0,a.jsx)("strong",{children:l(d)}),(0,a.jsx)("small",{children:e.t(d==="single"?"provider.singleDescription":"provider.multiDescription")})]})]},d)})}),!r.multi_supported&&(0,a.jsx)("p",{className:"da-muted",children:e.t("provider.multiUnsupported")}),r.multi_supported&&f.length===0&&(0,a.jsx)("p",{className:"da-muted",children:e.t("provider.noRoutes")})]}),r.mode==="multi"?(0,a.jsxs)("section",{children:[(0,a.jsxs)("div",{className:"da-provider-section-title",children:[e.t("provider.defaultRoute"),r.proxy_running&&(0,a.jsx)(N.Badge,{variant:"ok",children:e.t("provider.proxyRunning")})]}),f.length===0?(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.noRoutes")}):(0,a.jsx)("div",{className:"da-provider-list",children:f.map(d=>{let k=r.current_route_id===d.id,y=r.providers.find(O=>O.id===d.provider_id);return(0,a.jsxs)("button",{type:"button",className:"da-provider-item",disabled:!i||n||k,onClick:()=>c(d.id),children:[(0,a.jsxs)("span",{className:"da-provider-item-copy",children:[(0,a.jsx)("strong",{children:d.display_name||d.route_model}),(0,a.jsxs)("small",{children:[d.route_model," \u2192 ",y?.name??e.t("provider.providerRemoved")," \xB7 ",d.model]})]}),k&&(0,a.jsx)(N.Badge,{variant:"ok",children:e.t("provider.current")})]},d.id)})})]}):(0,a.jsxs)("section",{children:[(0,a.jsx)("div",{className:"da-provider-section-title",children:e.t("provider.configuredProviders")}),r.providers.length===0?(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.noProviders")}):(0,a.jsx)("div",{className:"da-provider-list",children:r.providers.map(d=>{let k=r.current_provider_id===d.id;return(0,a.jsxs)("button",{type:"button",className:"da-provider-item",disabled:!i||n||k,onClick:()=>u(d.id),children:[(0,a.jsxs)("span",{className:"da-provider-item-copy",children:[(0,a.jsx)("strong",{children:d.name}),(0,a.jsx)("small",{children:e.t(d.category==="official"?"provider.official":"provider.custom")})]}),k&&(0,a.jsx)(N.Badge,{variant:"ok",children:e.t("provider.current")})]},d.id)})}),v&&(0,a.jsx)("p",{className:"da-muted",children:e.t("provider.currentProvider",{name:v})})]})]})}var t=D(H(),1),pr=3e3,mr=1e4,ur=6e4,vr=3,We="tab",gr=new URL("../../assets/icon.svg",import.meta.url).href;function fr(){return(0,t.jsx)("img",{src:gr,alt:"",width:16,height:16,draggable:!1})}function He(e){let r=e.core.service;try{return r&&r.available()?r:void 0}catch{return}}var de=e=>e?.state==="running"||e?.state==="starting";async function Fe(e){try{return await q.info(e),!0}catch{return!1}}var ee;function br({ctx:e}){let r=we(e,"server"),i=we(e,"workspace"),[n,u]=(0,S.useState)(()=>{let p=e.storage.get(We,"local");return p==="remote"||p==="providers"?p:"local"}),h=p=>{e.storage.set(We,p),u(p)},[c]=(0,S.useState)(()=>e.storage.get(ue,pe)),[f,v]=(0,S.useState)(()=>e.secureStorage.get(F,"")),[l,b]=(0,S.useState)({kind:"checking"}),[d,k]=(0,S.useState)(null),[y,O]=(0,S.useState)(""),[K,L]=(0,S.useState)(!1),B=(0,S.useRef)(!0),P=He(e),w=(0,S.useCallback)(async()=>{let p=null;if(P)try{p=await P.status()}catch{p=null}if(!B.current)return;k(p);let _=f;de(p)&&p?.token&&(_=p.token,p.token!==f&&(e.secureStorage.set(F,p.token),v(p.token)));try{await q.info(c)}catch{B.current&&b({kind:de(p)?"starting":"offline"});return}if(!_){B.current&&b({kind:"needsCode"});return}try{let A=await q.status(c,_);B.current&&b({kind:"ready",status:A})}catch(A){if(!B.current)return;A instanceof z&&A.kind==="unauthorized"?(e.secureStorage.remove(F),v(""),b({kind:"needsCode"})):b({kind:"offline"})}},[e,c,P,f]);(0,S.useEffect)(()=>{B.current=!0,w();let p=window.setInterval(()=>{w()},pr);return()=>{B.current=!1,window.clearInterval(p)}},[w]),(0,S.useEffect)(()=>{if(l.kind==="ready"){let p=l.status;e.reportHealth(p.paired&&p.connected?{status:"healthy",summary:e.t("health.online",{name:p.device_name})}:{status:"degraded",summary:e.t(p.paired?"health.disconnected":"health.unpaired")})}else l.kind==="offline"&&e.reportHealth({status:"degraded",summary:e.t(P?"health.disabled":"health.offline")})},[e,l,P]);let U=async p=>{L(!0),O("");try{await p()}catch(_){O(ne(e,_))}finally{L(!1)}},R=p=>U(async()=>{if(!P)return;if(!p){e.storage.set(ve,"off"),k(await P.stop()),await w();return}if(e.storage.set(ve,"on"),e.storage.remove(oe),!de(d)&&await Fe(c)){ee?.(),await w();return}let _=await P.start();k(_),_.token&&(e.secureStorage.set(F,_.token),v(_.token)),await hr(c,()=>B.current)&&ee?.(),await w()}),$=d?.state==="exited"?d:null,Y=!!P&&!de(d)&&(l.kind==="ready"||l.kind==="needsCode");return(0,t.jsxs)("div",{className:"da-app",children:[(0,t.jsx)(g.PageHeader,{title:e.t("page.title"),subtitle:e.t("page.subtitle")}),(0,t.jsx)("div",{className:"da-tabs",role:"tablist","aria-label":e.t("page.title"),children:["local","remote","providers"].map(p=>(0,t.jsx)("button",{type:"button",role:"tab","aria-selected":n===p,className:n===p?"da-tab da-tab-active":"da-tab",onClick:()=>h(p),children:e.t(`tabs.${p}`)},p))}),y&&(0,t.jsx)("div",{role:"alert",className:"da-alert",children:y}),n==="remote"&&(0,t.jsx)(ze,{ctx:e,server:r,workspace:i,localDeviceId:l.kind==="ready"&&l.status.paired?l.status.device_id:""}),n==="providers"&&(0,t.jsx)(Ke,{ctx:e,workspace:i,localDeviceId:l.kind==="ready"&&l.status.paired?l.status.device_id:""}),n==="local"&&(0,t.jsxs)(t.Fragment,{children:[P&&(0,t.jsx)(yr,{ctx:e,status:d,external:Y,exit:$,busy:K,onToggle:p=>{R(p)}}),l.kind==="checking"&&(0,t.jsx)(g.Card,{children:(0,t.jsx)("span",{className:"da-muted",children:e.t("common.checking")})}),l.kind==="starting"&&(0,t.jsx)(g.Card,{children:(0,t.jsx)("span",{className:"da-muted",children:e.t("service.starting")})}),l.kind==="offline"&&!P&&(0,t.jsx)(wr,{ctx:e,localUrl:c}),l.kind==="needsCode"&&(0,t.jsx)(_r,{ctx:e,busy:K,onPair:p=>U(async()=>{let _=await q.pair(c,p).catch(A=>{throw A instanceof z&&A.code==="code_wrong"?new Error(e.t("pair.wrong")):A instanceof z&&A.code==="locked"?new Error(e.t("pair.locked")):A});e.secureStorage.set(F,_.token),v(_.token)})}),l.kind==="ready"&&(0,t.jsx)(Cr,{ctx:e,status:l.status,server:r,workspace:i,busy:K,run:U,localUrl:c,token:f,onStatus:p=>b({kind:"ready",status:p})})]})]})}async function hr(e,r){for(let i=0;i<30&&r();i+=1)try{return await q.info(e),!0}catch{await new Promise(n=>window.setTimeout(n,500))}return!1}function yr({ctx:e,status:r,external:i,exit:n,busy:u,onToggle:h}){let c=de(r),f=!c&&i,v=c||f,l=n?.exitCode===vr;return(0,t.jsxs)(g.Card,{children:[(0,t.jsxs)("div",{className:"da-switch",children:[(0,t.jsxs)("div",{children:[(0,t.jsx)(g.CardTitle,{children:e.t("service.title")}),(0,t.jsx)("p",{className:"da-text",children:e.t(f?"service.onExternalBody":v?"service.onBody":"service.offBody")})]}),(0,t.jsx)(g.Toggle,{checked:v,disabled:u||f,label:e.t("service.title"),onChange:h})]}),c&&(0,t.jsx)("p",{className:"da-muted",children:e.t("service.followsApp")}),f&&(0,t.jsx)("p",{className:"da-muted",children:e.t("service.external")}),!v&&l&&(0,t.jsx)("p",{className:"da-muted",children:e.t("service.portTaken")}),n&&!l&&n.exitCode!==0&&(0,t.jsx)("div",{role:"alert",className:"da-alert",children:e.t("service.crashed",{error:n.error||`exit ${n.exitCode??n.signal??"?"}`})})]})}function wr({ctx:e,localUrl:r}){return(0,t.jsxs)(g.Card,{children:[(0,t.jsx)(g.CardTitle,{children:e.t("offline.title")}),(0,t.jsx)("p",{className:"da-text",children:e.t("offline.body",{url:r})}),(0,t.jsx)("div",{className:"da-step",children:e.t("offline.once")}),(0,t.jsx)(V,{ctx:e,value:"npx toc-ocra@latest run"}),(0,t.jsx)("div",{className:"da-step",children:e.t("offline.service")}),(0,t.jsx)(V,{ctx:e,value:"npm install -g toc-ocra@latest && toc-ocra service:install"}),(0,t.jsx)("p",{className:"da-muted",children:e.t("offline.node")})]})}function _r({ctx:e,busy:r,onPair:i}){let[n,u]=(0,S.useState)(""),h=/^\d{6}$/.test(n);return(0,t.jsxs)(g.Card,{children:[(0,t.jsx)(g.CardTitle,{children:e.t("pair.title")}),(0,t.jsx)("p",{className:"da-text",children:e.t("pair.body")}),(0,t.jsx)(V,{ctx:e,value:"toc-ocra code"}),(0,t.jsxs)("form",{className:"da-row",onSubmit:c=>{c.preventDefault(),h&&i(n)},children:[(0,t.jsx)(g.Input,{value:n,onChange:c=>u(c.target.value.replace(/\D/g,"").slice(0,6)),placeholder:e.t("pair.placeholder"),inputMode:"numeric",autoComplete:"one-time-code","aria-label":e.t("pair.placeholder"),className:"da-code-input"}),(0,t.jsx)(g.Btn,{primary:!0,type:"submit",disabled:!h||r,children:e.t("pair.submit")})]})]})}function Cr({ctx:e,status:r,server:i,workspace:n,busy:u,run:h,localUrl:c,token:f,onStatus:v}){let l=r.paired&&i&&!me(r.server_url,i.origin),b=()=>h(async()=>{r.device_id&&i&&me(r.server_url,i.origin)&&await e.core.request(`/devices/${encodeURIComponent(r.device_id)}/agent`,{method:"DELETE"}).catch(()=>{}),e.storage.set(oe,"off"),v(await q.unpair(c,f))});return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(g.Card,{children:[(0,t.jsx)(g.CardTitle,{children:e.t("status.title")}),(0,t.jsxs)("dl",{className:"da-grid",children:[(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.service")}),(0,t.jsxs)("dd",{children:["toc-ocra ",r.version," \xB7 ",r.hostname]})]}),(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.binding")}),(0,t.jsx)("dd",{children:(0,t.jsx)(kr,{ctx:e,status:r})})]}),r.paired&&(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.device")}),(0,t.jsx)("dd",{children:r.device_name})]}),r.paired&&(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.server")}),(0,t.jsx)("dd",{className:"da-mono",children:r.server_url})]}),r.paired&&(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.runs")}),(0,t.jsx)("dd",{children:r.active_runs})]})]}),r.paired&&!r.connected&&r.connection_error&&(0,t.jsx)("p",{className:"da-muted",children:e.t("status.lastError",{error:r.connection_error})}),r.token_from_env&&(0,t.jsx)("p",{className:"da-muted",children:e.t("status.tokenFromEnv")}),r.paired&&(0,t.jsx)("div",{className:"da-row",children:(0,t.jsx)(g.Btn,{danger:!0,disabled:u||r.token_from_env,onClick:()=>{b()},children:e.t("status.unpair")})})]}),(!r.paired||r.unauthorized||l)&&(0,t.jsx)(Nr,{ctx:e,status:r,server:i,workspace:n,busy:u,run:h,localUrl:c,token:f,onStatus:v,rebind:r.paired}),(0,t.jsxs)(g.Card,{children:[(0,t.jsx)(g.CardTitle,{children:e.t("cli.title")}),(0,t.jsxs)("ul",{className:"da-list",children:[r.providers.filter(d=>d.installed).map(d=>(0,t.jsxs)("li",{children:[(0,t.jsx)("span",{className:"da-mono",children:d.id}),(0,t.jsx)("span",{className:"da-muted",children:d.version}),d.runnable?(0,t.jsx)(g.Badge,{variant:"ok",children:e.t("cli.runnable")}):(0,t.jsx)(g.Badge,{variant:"muted",children:e.t("cli.detectOnly")})]},d.id)),!r.providers.some(d=>d.installed)&&(0,t.jsx)("li",{className:"da-muted",children:e.t("cli.none")})]}),(0,t.jsx)("div",{className:"da-row",children:(0,t.jsx)(g.Btn,{disabled:u,onClick:()=>{h(async()=>v(await q.detect(c,f)))},children:e.t("cli.redetect")})})]})]})}function kr({ctx:e,status:r}){return r.paired?r.unauthorized?(0,t.jsx)(g.Badge,{variant:"err",children:e.t("binding.revoked")}):r.connected?(0,t.jsx)(g.Badge,{variant:"ok",children:e.t("binding.online")}):(0,t.jsx)(g.Badge,{variant:"warn",children:e.t("binding.connecting")}):(0,t.jsx)(g.Badge,{variant:"muted",children:e.t("binding.unpaired")})}function Nr({ctx:e,status:r,server:i,workspace:n,busy:u,run:h,localUrl:c,token:f,onStatus:v,rebind:l}){let[b,d]=(0,S.useState)([]),[k,y]=(0,S.useState)("new"),[O,K]=(0,S.useState)(r.hostname),[L,B]=(0,S.useState)(i?.origin??"");(0,S.useEffect)(()=>{i?.origin&&B(i.origin)},[i?.origin]),(0,S.useEffect)(()=>{if(!n?.id)return;let w=!1;return e.core.request(`/workspaces/${encodeURIComponent(n.id)}/devices`).then(U=>{w||d((U.items??[]).filter(R=>R.is_mine))}).catch(()=>{w||d([])}),()=>{w=!0}},[e,n?.id]);let P=()=>h(async()=>{if(!n?.id)throw new Error(e.t("bind.noWorkspace"));if(!L.trim())throw new Error(e.t("bind.noServer"));let w=k,U;if(k==="new"){if(!r.machine_id)throw new Error(e.t("bind.identityRequired"));let R=await e.core.request("/devices/machine/pairing-code",{method:"POST",body:JSON.stringify({machine_id:r.machine_id,hostname:O.trim()||r.hostname,platform:r.platform,current_device_id:r.paired?r.device_id:""})});w=R.device_id,y(w),U=R}else U=await e.core.request(`/devices/${encodeURIComponent(w)}/pairing-code`,{method:"POST"});v(await q.enroll(c,f,L.trim(),U.code)),e.storage.remove(oe),e.notify({title:e.t("page.title"),body:e.t("bind.done",{server:i?.name??L})})});return(0,t.jsxs)(g.Card,{children:[(0,t.jsx)(g.CardTitle,{children:e.t(l?"bind.rebindTitle":"bind.title")}),l&&r.unauthorized&&(0,t.jsx)("p",{className:"da-text",children:e.t("bind.revoked")}),l&&!r.unauthorized&&(0,t.jsx)("p",{className:"da-text",children:e.t("bind.elsewhere",{bound:r.server_url,current:i?.origin??""})}),(0,t.jsx)("p",{className:"da-text",children:e.t("bind.body",{server:i?.name??"",workspace:n?.name??""})}),(0,t.jsxs)("label",{className:"da-field",children:[(0,t.jsx)("span",{children:e.t("bind.server")}),(0,t.jsx)(g.Input,{value:L,onChange:w=>B(w.target.value),spellCheck:!1,className:"da-mono"})]}),(0,t.jsxs)("label",{className:"da-field",children:[(0,t.jsx)("span",{children:e.t("bind.device")}),(0,t.jsxs)("select",{className:"da-select",value:k,onChange:w=>y(w.target.value),children:[(0,t.jsx)("option",{value:"new",children:e.t("bind.newDevice")}),b.map(w=>(0,t.jsxs)("option",{value:w.id,children:[w.name,w.paired?` \xB7 ${e.t("bind.replaces")}`:""]},w.id))]})]}),k==="new"&&(0,t.jsxs)("label",{className:"da-field",children:[(0,t.jsx)("span",{children:e.t("bind.name")}),(0,t.jsx)(g.Input,{value:O,onChange:w=>K(w.target.value),maxLength:100})]}),(0,t.jsx)("div",{className:"da-row",children:(0,t.jsx)(g.Btn,{primary:!0,disabled:u||!n?.id,onClick:()=>{P()},children:e.t(u?"bind.working":"bind.submit")})})]})}function Sr(e){let r=!1,i="",n=new Map,u=async()=>{if(r||e.signal.aborted)return;let c=e.core.context("server"),f=e.secureStorage.get(F,"");if(!(!c?.origin||!f||e.storage.get(oe,"on")==="off")&&!(Date.now()-(n.get(c.origin)??0)<ur)){r=!0;try{let v=e.storage.get(ue,pe),l=await q.status(v,f);if(!l.machine_id||l.token_from_env)return;if(l.paired&&(l.unauthorized||me(l.server_url,c.origin))){i="";return}if(l.active_runs>0){i!==c.origin&&(i=c.origin,e.notify({title:e.t("page.title"),body:e.t("auto.deferred",{server:c.name||c.origin})}));return}let b=await e.core.request("/devices/machine/pairing-code",{method:"POST",body:JSON.stringify({machine_id:l.machine_id,hostname:l.hostname,platform:l.platform,current_device_id:l.paired?l.device_id:""})});await q.enroll(v,f,c.origin,b.code),i="",n.delete(c.origin),e.notify({title:e.t("page.title"),body:e.t("auto.done",{server:c.name||c.origin,device:b.device_name})})}catch(v){(!(v instanceof z)||v.kind==="rejected")&&n.set(c.origin,Date.now())}finally{r=!1}}},h=window.setInterval(()=>{u()},mr);ee=()=>{u()},e.registerDisposer(()=>{window.clearInterval(h),ee=void 0}),e.registerDisposer(e.core.onContextChange("server",()=>{u()})),window.setTimeout(()=>{u()},1500)}function Pr(e){let r=He(e);if(!r||e.storage.get(ve,"off")!=="on")return;let i=e.storage.get(ue,pe);Fe(i).then(n=>{if(n){window.setTimeout(()=>ee?.(),3e3);return}return r.start()}).then(n=>{n&&(n.token&&e.secureStorage.set(F,n.token),window.setTimeout(()=>ee?.(),3e3))}).catch(()=>{})}var Yr=Le({activate(e){e.registerNavigation("agent.nav",(0,t.jsx)(fr,{})),e.registerRoute("agent.home",(0,t.jsx)(br,{ctx:e})),Pr(e),Sr(e)}});export{Yr as default};
|
|
1
|
+
var Ve=Object.create;var Se=Object.defineProperty;var Ge=Object.getOwnPropertyDescriptor;var Xe=Object.getOwnPropertyNames;var Qe=Object.getPrototypeOf,Ze=Object.prototype.hasOwnProperty;var ye=(e,r)=>()=>{try{return r||e((r={exports:{}}).exports,r),r.exports}catch(i){throw r=0,i}};var er=(e,r,i,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let v of Xe(r))!Ze.call(e,v)&&v!==i&&Se(e,v,{get:()=>r[v],enumerable:!(n=Ge(r,v))||n.enumerable});return e};var D=(e,r,i)=>(i=e!=null?Ve(Qe(e)):{},er(r||!e||!e.__esModule?Se(i,"default",{value:e,enumerable:!0}):i,e));var Q=ye((Tr,Te)=>{var Pe=globalThis.__TOC_PLUGIN_SHARED__,xe=Pe&&Pe.react;if(!xe)throw new Error("The host does not provide react; this plugin needs a newer app version.");Te.exports=xe});var ae=ye((Lr,Re)=>{var Ee=globalThis.__TOC_PLUGIN_SHARED__,Be=Ee&&Ee["@toc-starter/plugin-sdk/ui"];if(!Be)throw new Error("The host does not provide @toc-starter/plugin-sdk/ui; this plugin needs a newer app version.");Re.exports=Be});var H=ye((Dr,Oe)=>{var De=globalThis.__TOC_PLUGIN_SHARED__,Ae=De&&De["react/jsx-runtime"];if(!Ae)throw new Error("The host does not provide react/jsx-runtime; this plugin needs a newer app version.");Oe.exports=Ae});var S=D(Q(),1);var Ie=D(Q(),1);function Le(e){if(!e||typeof e.activate!="function")throw new TypeError("definePlugin() needs an object with an activate(context) function");return e}function we(e,r){return(0,Ie.useSyncExternalStore)(i=>e.core.onContextChange(r,i),()=>e.core.context(r))}var b=D(ae(),1);var pe="http://127.0.0.1:17339",M=class extends Error{constructor(i,n,v=""){super(n);this.kind=i;this.code=v}kind;code};async function Z(e,r,i={}){let n=new Headers(i.headers);i.token&&n.set("Authorization",`Bearer ${i.token}`),i.body&&n.set("Content-Type","application/json");let v;try{v=await fetch(`${e.replace(/\/+$/,"")}${r}`,{...i,headers:n,cache:"no-store"})}catch{throw new M("offline","local service unreachable")}let g=await v.json().catch(()=>null);if(v.status===401)throw new M("unauthorized","unauthorized");if(!v.ok){let f=typeof g?.error=="string"?g.error:"",l=typeof g?.message=="string"?g.message:f||`HTTP ${v.status}`;throw new M("rejected",l,f)}return g}var q={info:e=>Z(e,"/control/info"),pair:(e,r)=>Z(e,"/control/pair",{method:"POST",body:JSON.stringify({code:r})}),status:(e,r)=>Z(e,"/control/status",{token:r}),enroll:(e,r,i,n)=>Z(e,"/control/enroll",{method:"POST",token:r,body:JSON.stringify({server_url:i,code:n})}),unpair:(e,r)=>Z(e,"/control/unpair",{method:"POST",token:r}),detect:(e,r)=>Z(e,"/control/detect",{method:"POST",token:r})};function me(e,r){let i=n=>{try{return new URL(n).origin.toLowerCase()}catch{return n.trim().replace(/\/+$/,"").toLowerCase()}};return!!(e&&r)&&i(e)===i(r)}var E=D(Q(),1),C=D(ae(),1);var Ue=D(Q(),1),$e=D(ae(),1);var oe=D(H(),1),F="controlToken",ue="localUrl",ne="autoBind",ve="localService";function ee(e,r){return r instanceof M&&r.kind==="offline"?e.t("error.offline"):r instanceof Error?r.message:String(r)}function V({ctx:e,value:r}){let[i,n]=(0,Ue.useState)(!1);return(0,oe.jsxs)("div",{className:"da-command",children:[(0,oe.jsx)("code",{children:r}),(0,oe.jsx)($e.Btn,{onClick:()=>{navigator.clipboard.writeText(r).then(()=>{n(!0),window.setTimeout(()=>n(!1),1500)})},children:i?e.t("common.copied"):e.t("common.copy")})]})}var o=D(H(),1),fe=["linux","mac","win"],rr=1e4,tr=3e3,ge="remoteServerUrl",ir=new Set(["127.0.0.1","localhost","[::1]","::1","0.0.0.0"]);function ar(e){try{let r=new URL(e.trim());return r.protocol==="http:"||r.protocol==="https:"?r.origin:null}catch{return null}}function or(e){try{return ir.has(new URL(e).hostname)}catch{return!1}}function nr(e,r,i){let n=`toc-ocra setup --code ${r} --server ${i}`;if(e==="win"){let v=g=>`'${g.replaceAll("'","''")}'`;return`npm.cmd install -g toc-ocra@latest; if ($?) { toc-ocra.cmd setup --code ${v(r)} --server ${v(i)} }`}return`npm install -g toc-ocra@latest && ${n}`}function dr({ctx:e,device:r}){return r.status==="online"?(0,o.jsx)(C.Badge,{variant:"ok",children:e.t("remote.status.online")}):r.status==="disabled"?(0,o.jsx)(C.Badge,{variant:"warn",children:e.t("remote.status.disabled")}):r.status==="offline"?(0,o.jsx)(C.Badge,{variant:"err",children:e.t("remote.status.offline")}):(0,o.jsx)(C.Badge,{variant:"muted",children:e.t("remote.status.notConnected")})}function Me({ctx:e,server:r,workspace:i,localDeviceId:n}){let[v,g]=(0,E.useState)(null),[f,l]=(0,E.useState)(""),[h,c]=(0,E.useState)(!1),[p,d]=(0,E.useState)(null),[k,y]=(0,E.useState)(!1),[A,K]=(0,E.useState)(""),[L,B]=(0,E.useState)("linux"),[P,w]=(0,E.useState)(()=>e.storage.get(ge,"")||r?.origin||""),U=(0,E.useRef)(p);U.current=p;let R=(0,E.useRef)(!0);(0,E.useEffect)(()=>{!e.storage.get(ge,"")&&r?.origin&&w(r.origin)},[e,r?.origin]);let $=(0,E.useCallback)(async()=>{if(i?.id)try{let s=await e.core.request(`/workspaces/${encodeURIComponent(i.id)}/devices`);if(!R.current)return;let x=(s.items??[]).filter(z=>z.is_mine&&z.id!==n);g(x);let W=U.current;if(W&&!W.matched){let z=x.find(ie=>ie.id===W.deviceId);z?.agent_id&&z.agent_id!==W.baseline&&z.terminal_online&&(d({...W,matched:!0}),e.notify({title:e.t("page.title"),body:e.t("remote.matchedNotice",{name:z.name})}))}}catch(s){R.current&&l(ee(e,s))}},[e,n,i?.id]),Y=!!(p&&!p.matched);(0,E.useEffect)(()=>{R.current=!0,$();let s=window.setInterval(()=>{$()},Y?tr:rr);return()=>{R.current=!1,window.clearInterval(s)}},[$,Y]);let m=async s=>{c(!0),l("");try{await s()}catch(x){l(ee(e,x))}finally{c(!1)}},_=ar(P),O=s=>{w(s),s.trim()&&s.trim()!==r?.origin?e.storage.set(ge,s.trim()):e.storage.remove(ge)},j=s=>m(async()=>{if(!_)throw new Error(e.t("remote.serverInvalid"));let x=await e.core.request(`/devices/${encodeURIComponent(s.id)}/pairing-code`,{method:"POST"});d({deviceId:s.id,deviceName:s.name,platform:fe.includes(s.platform)?s.platform:"linux",code:x.code,expiresAt:x.expires_at,baseline:s.agent_id??null,matched:!1})}),J=()=>m(async()=>{if(!i?.id)throw new Error(e.t("bind.noWorkspace"));if(!_)throw new Error(e.t("remote.serverInvalid"));let s=A.trim()||e.t(`remote.platform.${L}`),x=await e.core.request(`/workspaces/${encodeURIComponent(i.id)}/devices`,{method:"POST",body:JSON.stringify({name:s,location:L==="linux"?"cloud":"local",platform:L,note:"",share_to_workspace:!1})}),W=await e.core.request(`/devices/${encodeURIComponent(x.id)}/pairing-code`,{method:"POST"});d({deviceId:x.id,deviceName:x.name||s,platform:L,code:W.code,expiresAt:W.expires_at,baseline:null,matched:!1}),y(!1),K(""),await $()}),te=(s,x)=>m(async()=>{await e.core.request(`/devices/${encodeURIComponent(s.id)}/enabled`,{method:"PUT",body:JSON.stringify({enabled:x})}),await $()});return(0,o.jsxs)(o.Fragment,{children:[f&&(0,o.jsx)("div",{role:"alert",className:"da-alert",children:f}),(0,o.jsxs)(C.Card,{children:[(0,o.jsx)(C.CardTitle,{children:e.t("remote.title")}),(0,o.jsx)("p",{className:"da-text",children:e.t("remote.body")}),(0,o.jsxs)("label",{className:"da-field",children:[(0,o.jsx)("span",{children:e.t("remote.server")}),(0,o.jsx)(C.Input,{value:P,onChange:s=>O(s.target.value),spellCheck:!1,className:"da-mono"})]}),!_&&(0,o.jsx)("p",{className:"da-muted da-warn",children:e.t("remote.serverInvalid")}),_&&or(_)&&(0,o.jsx)("p",{className:"da-muted da-warn",children:e.t("remote.loopback")}),!k&&(0,o.jsx)("div",{className:"da-row",children:(0,o.jsx)(C.Btn,{primary:!0,disabled:h||!i?.id,onClick:()=>y(!0),children:e.t("remote.add")})}),k&&(0,o.jsxs)("form",{className:"da-form",onSubmit:s=>{s.preventDefault(),J()},children:[(0,o.jsxs)("label",{className:"da-field",children:[(0,o.jsx)("span",{children:e.t("remote.platformLabel")}),(0,o.jsx)("select",{className:"da-select",value:L,onChange:s=>B(s.target.value),children:fe.map(s=>(0,o.jsx)("option",{value:s,children:e.t(`remote.platform.${s}`)},s))})]}),(0,o.jsxs)("label",{className:"da-field",children:[(0,o.jsx)("span",{children:e.t("remote.name")}),(0,o.jsx)(C.Input,{value:A,onChange:s=>K(s.target.value),maxLength:100,placeholder:e.t("remote.namePlaceholder")})]}),(0,o.jsxs)("div",{className:"da-row",children:[(0,o.jsx)(C.Btn,{primary:!0,type:"submit",disabled:h||!_,children:e.t(h?"remote.working":"remote.generate")}),(0,o.jsx)(C.Btn,{type:"button",disabled:h,onClick:()=>y(!1),children:e.t("remote.cancel")})]})]})]}),p&&_&&(0,o.jsx)(sr,{ctx:e,pending:p,origin:_,busy:h,onPlatform:s=>d({...p,platform:s}),onRegenerate:()=>{j({id:p.deviceId,name:p.deviceName,platform:p.platform,agent_id:p.baseline})},onClose:()=>d(null)}),(0,o.jsxs)(C.Card,{children:[(0,o.jsx)(C.CardTitle,{children:e.t("remote.listTitle")}),v===null&&(0,o.jsx)("p",{className:"da-muted",children:e.t("common.checking")}),v?.length===0&&(0,o.jsx)("p",{className:"da-muted",children:e.t("remote.empty")}),v&&v.length>0&&(0,o.jsx)("ul",{className:"da-devices",children:v.map(s=>(0,o.jsxs)("li",{children:[(0,o.jsxs)("div",{className:"da-device-main",children:[(0,o.jsx)("span",{className:"da-device-name",children:s.name}),(0,o.jsxs)("span",{className:"da-muted",children:[e.t(`remote.platform.${fe.includes(s.platform)?s.platform:"linux"}`),s.agent_version?` \xB7 toc-ocra ${s.agent_version}`:""]})]}),(0,o.jsx)(dr,{ctx:e,device:s}),(0,o.jsxs)("div",{className:"da-device-actions",children:[(!s.paired||s.status==="offline"||s.status==="not_connected")&&(0,o.jsx)(C.Btn,{disabled:h||!_,onClick:()=>{j(s)},children:e.t(s.paired?"remote.repair":"remote.install")}),s.paired&&(0,o.jsxs)("label",{className:"da-toggle",children:[(0,o.jsx)("span",{children:e.t("remote.enabled")}),(0,o.jsx)(C.Toggle,{checked:!s.disabled,disabled:h,label:e.t("remote.enabled"),onChange:x=>{te(s,x)}})]})]})]},s.id))}),(0,o.jsx)("p",{className:"da-muted",children:e.t("remote.disableHint")}),(0,o.jsx)("div",{className:"da-step",children:e.t("remote.uninstall")}),(0,o.jsx)(V,{ctx:e,value:"toc-ocra service:uninstall"})]})]})}function sr({ctx:e,pending:r,origin:i,busy:n,onPlatform:v,onRegenerate:g,onClose:f}){let[l,h]=(0,E.useState)(()=>Math.floor(Date.now()/1e3));(0,E.useEffect)(()=>{if(r.matched)return;let d=window.setInterval(()=>h(Math.floor(Date.now()/1e3)),1e3);return()=>window.clearInterval(d)},[r.matched]);let c=Math.max(0,r.expiresAt-l),p=!r.matched&&c===0;return(0,o.jsxs)(C.Card,{children:[(0,o.jsx)(C.CardTitle,{children:e.t("remote.pendingTitle",{name:r.deviceName})}),r.matched?(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)("p",{className:"da-text",children:[(0,o.jsx)(C.Badge,{variant:"ok",children:e.t("remote.matched")})," ",e.t("remote.matchedBody",{name:r.deviceName})]}),(0,o.jsx)("div",{className:"da-row",children:(0,o.jsx)(C.Btn,{primary:!0,onClick:f,children:e.t("remote.done")})})]}):(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)("p",{className:"da-text",children:e.t("remote.pendingBody")}),(0,o.jsx)("div",{className:"da-segment",role:"radiogroup","aria-label":e.t("remote.platformLabel"),children:fe.map(d=>(0,o.jsx)("button",{type:"button",role:"radio","aria-checked":r.platform===d,className:r.platform===d?"da-seg da-seg-active":"da-seg",onClick:()=>v(d),children:e.t(`remote.platform.${d}`)},d))}),(0,o.jsx)(V,{ctx:e,value:nr(r.platform,r.code,i)}),(0,o.jsxs)("ul",{className:"da-hints",children:[(0,o.jsx)("li",{children:e.t("remote.hintNode")}),r.platform==="linux"&&(0,o.jsx)("li",{children:e.t("remote.hintLinux")}),r.platform==="win"&&(0,o.jsx)("li",{children:e.t("remote.hintWindows")}),(0,o.jsx)("li",{children:p?e.t("remote.expired"):e.t("remote.expires",{minutes:Math.floor(c/60),seconds:String(c%60).padStart(2,"0")})})]}),(0,o.jsx)("p",{className:"da-muted",children:p?"":e.t("remote.waiting")}),(0,o.jsxs)("div",{className:"da-row",children:[(0,o.jsx)(C.Btn,{disabled:n,onClick:g,children:e.t("remote.regenerate")}),(0,o.jsx)(C.Btn,{disabled:n,onClick:f,children:e.t("remote.cancel")})]})]})]})}var T=D(Q(),1),N=D(ae(),1),a=D(H(),1),ze=[{id:"codex",label:"Codex"},{id:"claude",label:"Claude Code"},{id:"gemini",label:"Gemini CLI"}],_e="tb-switch-device";function re(e){return e.terminal_online??e.online??e.status==="online"}function qe(e){return!e.paired||e.status==="not_connected"?{key:"provider.status.notPaired",variant:"muted"}:e.disabled||e.status==="disabled"?{key:"provider.status.paused",variant:"warn"}:re(e)?{key:"provider.status.online",variant:"ok"}:{key:"provider.status.offline",variant:"err"}}function lr(e,r){return e.find(i=>i.id===r&&i.paired&&!i.disabled&&re(i))??e.find(i=>i.is_mine&&i.paired&&!i.disabled&&re(i))??e.find(i=>i.paired&&!i.disabled&&re(i))??e.find(i=>i.id===r)??e.find(i=>i.paired)??e[0]}function Ce(e){return e instanceof Error?e.message:String(e)}function Ke({ctx:e,workspace:r,localDeviceId:i}){let[n,v]=(0,T.useState)(null),[g,f]=(0,T.useState)(!1),[l,h]=(0,T.useState)(""),[c,p]=(0,T.useState)({workspaceId:"",deviceId:""}),d=(0,T.useRef)(c);d.current=c;let k=c.workspaceId===r?.id?c.deviceId:"",y=n?.find(u=>u.id===k)??null,[A,K]=(0,T.useState)("codex"),[L,B]=(0,T.useState)(null),[P,w]=(0,T.useState)(!1),[U,R]=(0,T.useState)(""),[$,Y]=(0,T.useState)(!1),[m,_]=(0,T.useState)(""),[O,j]=(0,T.useState)(null),J=(0,T.useRef)(0),te=!!(y&&(y.is_mine||r?.role==="owner"||r?.role==="admin")),s=!!(y?.paired&&!y.disabled&&y.status!=="disabled"&&re(y)),x=(0,T.useCallback)(async(u=!1)=>{if(!r?.id){v([]);let I={workspaceId:"",deviceId:""};d.current=I,p(I);return}u||f(!0),h("");try{let X=(await e.core.request(`/workspaces/${encodeURIComponent(r.id)}/devices`)).items??[];v(X);let le=d.current,je=le.workspaceId===r.id?le.deviceId:"";if(X.some(he=>he.id===je))return;let Ne="";try{Ne=e.storage.get(`${_e}:${r.id}`,"")}catch{}let Je=X.find(he=>he.id===Ne)??lr(X,i),ce={workspaceId:r.id,deviceId:Je?.id??""};if(d.current=ce,p(ce),ce.deviceId)try{e.storage.set(`${_e}:${r.id}`,ce.deviceId)}catch{}}catch(I){h(Ce(I))}finally{u||f(!1)}},[e,i,r?.id]);(0,T.useEffect)(()=>{x();let u=window.setInterval(()=>{x(!0)},15e3);return()=>window.clearInterval(u)},[x]);let W=u=>{let I={workspaceId:r?.id??"",deviceId:u};if(d.current=I,p(I),_(""),R(""),r?.id)try{e.storage.set(`${_e}:${r.id}`,u)}catch{}},z=y?.id??"",ie=(0,T.useCallback)(async()=>{if(!r?.id||!z||!s)return;let u=++J.current;w(!0),R("");try{let I=await e.core.request(`/workspaces/${encodeURIComponent(r.id)}/devices/${encodeURIComponent(z)}/cli-providers/control`,{method:"POST",body:JSON.stringify({app:A,action:"state"})});u===J.current&&B(I)}catch(I){u===J.current&&(B(null),R(Ce(I)))}finally{u===J.current&&w(!1)}},[A,e,s,z,r?.id]);(0,T.useEffect)(()=>{J.current+=1,w(!1),B(null),_(""),s&&ie()},[ie,s]);let se=async u=>{if(!(!r?.id||!y||!s||u.action!=="state"&&!te||$)){Y(!0),_(""),R("");try{let I=await e.core.request(`/workspaces/${encodeURIComponent(r.id)}/devices/${encodeURIComponent(y.id)}/cli-providers/control`,{method:"POST",body:JSON.stringify({app:A,...u})});B(I),_(u.action==="state"?e.t("provider.refreshed"):e.t("provider.updated"))}catch(I){R(Ce(I))}finally{Y(!1)}}},be=y?qe(y):null,Ye=r?.role==="owner"||r?.role==="admin",ke=n??[];return(0,a.jsxs)("div",{className:"da-provider-page",children:[(0,a.jsxs)("div",{className:"da-provider-toolbar",children:[(0,a.jsxs)("div",{className:"da-provider-device",children:[(0,a.jsx)("label",{htmlFor:"da-provider-device-select",children:e.t("provider.chooseDevice")}),(0,a.jsxs)("select",{id:"da-provider-device-select",className:"da-select",value:k,onChange:u=>W(u.target.value),disabled:g||ke.length===0||$,children:[(0,a.jsx)("option",{value:"",disabled:!0,children:e.t("provider.selectDevice")}),ke.map(u=>{let I=qe(u),X=u.is_mine?e.t("provider.myDevice"):e.t("provider.owner",{owner:u.owner_name||"\u2014"}),le=u.id===i?` \xB7 ${e.t("provider.thisDevice")}`:"";return(0,a.jsxs)("option",{value:u.id,children:[u.name," \xB7 ",e.t(I.key)," \xB7 ",X,le]},u.id)})]}),y&&be&&(0,a.jsx)(N.Badge,{variant:be.variant,children:e.t(be.key)})]}),(0,a.jsx)(N.Btn,{disabled:g,onClick:()=>{x()},children:g?e.t("provider.loading"):e.t("provider.refreshDevices")})]}),g&&n===null&&(0,a.jsx)(N.Card,{children:(0,a.jsx)("span",{className:"da-muted",children:e.t("provider.loadingDevices")})}),l&&(0,a.jsxs)("div",{role:"alert",className:"da-alert",children:[l,(0,a.jsx)(N.Btn,{disabled:g,onClick:()=>{x()},children:e.t("provider.retry")})]}),!r?.id&&(0,a.jsx)(N.Card,{children:(0,a.jsx)(N.CardTitle,{children:e.t("provider.noWorkspace")})}),r?.id&&n?.length===0&&!g&&(0,a.jsxs)(N.Card,{children:[(0,a.jsx)(N.CardTitle,{children:e.t("provider.noDevices")}),(0,a.jsx)("p",{className:"da-text",children:e.t("provider.noDevicesDescription")})]}),y&&(0,a.jsxs)(N.Card,{children:[(0,a.jsxs)("div",{className:"da-provider-heading",children:[(0,a.jsxs)("div",{children:[(0,a.jsx)(N.CardTitle,{children:e.t("provider.title")}),(0,a.jsx)("p",{className:"da-text",children:e.t("provider.description",{name:y.name})})]}),(0,a.jsxs)("div",{className:"da-provider-app-select",children:[(0,a.jsx)("label",{htmlFor:"da-provider-app-select",children:e.t("provider.cli")}),(0,a.jsx)("select",{id:"da-provider-app-select",className:"da-select",value:A,onChange:u=>{K(u.target.value),_("")},disabled:!s||$,children:ze.map(u=>(0,a.jsx)("option",{value:u.id,children:u.label},u.id))}),(0,a.jsx)(N.Btn,{disabled:!s||P||$,onClick:()=>{se({action:"state"})},children:P||$?e.t("provider.loading"):e.t("provider.refresh")})]})]}),y.paired?y.disabled||y.status==="disabled"?(0,a.jsx)("div",{className:"da-provider-message da-provider-warn",children:e.t("provider.paused")}):re(y)?P&&!L?(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.loadingState",{app:ze.find(u=>u.id===A)?.label})}):U?(0,a.jsxs)("div",{role:"alert",className:"da-provider-message da-provider-error",children:[U,(0,a.jsx)(N.Btn,{disabled:P,onClick:()=>{ie()},children:e.t("provider.retry")})]}):L?(0,a.jsx)(cr,{ctx:e,state:L,canEdit:te,busy:$,onProvider:u=>{se({action:"switch_provider",provider_id:u})},onMode:u=>j(u),onRoute:u=>{se({action:"select_route",route_id:u})}}):(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.selectHint")}):(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.offline")}):(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.notPaired")}),y.paired&&!te&&(0,a.jsx)("p",{className:"da-muted",children:Ye?e.t("provider.readOnly"):e.t("provider.ownerOnly")}),m&&(0,a.jsx)("div",{role:"status",className:"da-provider-notice",children:m}),L?.warnings?.length?(0,a.jsxs)("div",{className:"da-provider-warnings",children:[(0,a.jsx)("strong",{children:e.t("provider.warnings")}),(0,a.jsx)("ul",{children:L.warnings.map((u,I)=>(0,a.jsx)("li",{children:u},`${I}:${u}`))})]}):null]}),(0,a.jsx)(N.SystemModal,{open:!!O,title:e.t("provider.confirmModeTitle"),description:O==="multi"?e.t("provider.confirmMulti"):e.t("provider.confirmSingle"),onClose:()=>j(null),footer:(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(N.Btn,{onClick:()=>j(null),children:e.t("provider.cancel")}),(0,a.jsx)(N.Btn,{primary:!0,disabled:$,onClick:()=>{let u=O;j(null),u&&se({action:"set_mode",mode:u})},children:e.t("provider.confirm")})]}),children:(0,a.jsx)("p",{className:"da-text",children:e.t("provider.confirmModeHelp")})})]})}function cr({ctx:e,state:r,canEdit:i,busy:n,onProvider:v,onMode:g,onRoute:f}){let l=r.routes.filter(d=>d.enabled),h=r.providers.find(d=>d.id===r.current_provider_id)?.name,c=d=>e.t(d==="single"?"provider.single":"provider.multi"),p=!r.multi_supported||l.length===0;return(0,a.jsxs)("div",{className:"da-provider-controls",children:[(0,a.jsxs)("section",{children:[(0,a.jsx)("div",{className:"da-provider-section-title",children:e.t("provider.mode")}),(0,a.jsx)("div",{className:"da-provider-mode-grid",children:["single","multi"].map(d=>{let k=r.mode===d;return(0,a.jsxs)("button",{type:"button",className:k?"da-provider-mode da-provider-mode-active":"da-provider-mode",disabled:!i||n||k||d==="multi"&&p,onClick:()=>g(d),children:[(0,a.jsx)("span",{className:k?"da-provider-radio da-provider-radio-active":"da-provider-radio",children:k?"\u2713":""}),(0,a.jsxs)("span",{children:[(0,a.jsx)("strong",{children:c(d)}),(0,a.jsx)("small",{children:e.t(d==="single"?"provider.singleDescription":"provider.multiDescription")})]})]},d)})}),!r.multi_supported&&(0,a.jsx)("p",{className:"da-muted",children:e.t("provider.multiUnsupported")}),r.multi_supported&&l.length===0&&(0,a.jsx)("p",{className:"da-muted",children:e.t("provider.noRoutes")})]}),r.mode==="multi"?(0,a.jsxs)("section",{children:[(0,a.jsxs)("div",{className:"da-provider-section-title",children:[e.t("provider.defaultRoute"),r.proxy_running&&(0,a.jsx)(N.Badge,{variant:"ok",children:e.t("provider.proxyRunning")})]}),l.length===0?(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.noRoutes")}):(0,a.jsx)("div",{className:"da-provider-list",children:l.map(d=>{let k=r.current_route_id===d.id,y=r.providers.find(A=>A.id===d.provider_id);return(0,a.jsxs)("button",{type:"button",className:"da-provider-item",disabled:!i||n||k,onClick:()=>f(d.id),children:[(0,a.jsxs)("span",{className:"da-provider-item-copy",children:[(0,a.jsx)("strong",{children:d.display_name||d.route_model}),(0,a.jsxs)("small",{children:[d.route_model," \u2192 ",y?.name??e.t("provider.providerRemoved")," \xB7 ",d.model]})]}),k&&(0,a.jsx)(N.Badge,{variant:"ok",children:e.t("provider.current")})]},d.id)})})]}):(0,a.jsxs)("section",{children:[(0,a.jsx)("div",{className:"da-provider-section-title",children:e.t("provider.configuredProviders")}),r.providers.length===0?(0,a.jsx)("div",{className:"da-provider-message",children:e.t("provider.noProviders")}):(0,a.jsx)("div",{className:"da-provider-list",children:r.providers.map(d=>{let k=r.current_provider_id===d.id;return(0,a.jsxs)("button",{type:"button",className:"da-provider-item",disabled:!i||n||k,onClick:()=>v(d.id),children:[(0,a.jsxs)("span",{className:"da-provider-item-copy",children:[(0,a.jsx)("strong",{children:d.name}),(0,a.jsx)("small",{children:e.t(d.category==="official"?"provider.official":"provider.custom")})]}),k&&(0,a.jsx)(N.Badge,{variant:"ok",children:e.t("provider.current")})]},d.id)})}),h&&(0,a.jsx)("p",{className:"da-muted",children:e.t("provider.currentProvider",{name:h})})]})]})}var t=D(H(),1),pr=3e3,mr=1e4,ur=6e4,vr=3,We="tab",gr=new URL("../../assets/icon.svg",import.meta.url).href;function fr(){return(0,t.jsx)("img",{src:gr,alt:"",width:16,height:16,draggable:!1})}function He(e){let r=e.core.service;try{return r&&r.available()?r:void 0}catch{return}}var de=e=>e?.state==="running"||e?.state==="starting";async function Fe(e){try{return await q.info(e),!0}catch{return!1}}var G;function br({ctx:e}){let r=we(e,"server"),i=we(e,"workspace"),[n,v]=(0,S.useState)(()=>{let m=e.storage.get(We,"local");return m==="remote"||m==="providers"?m:"local"}),g=m=>{e.storage.set(We,m),v(m)},[f]=(0,S.useState)(()=>e.storage.get(ue,pe)),[l,h]=(0,S.useState)(()=>e.secureStorage.get(F,"")),[c,p]=(0,S.useState)({kind:"checking"}),[d,k]=(0,S.useState)(null),[y,A]=(0,S.useState)(""),[K,L]=(0,S.useState)(!1),B=(0,S.useRef)(!0),P=He(e),w=(0,S.useCallback)(async()=>{let m=null;if(P)try{m=await P.status()}catch{m=null}if(!B.current)return;k(m);let _=l;de(m)&&m?.token&&(_=m.token,m.token!==l&&(e.secureStorage.set(F,m.token),h(m.token)));try{await q.info(f)}catch{B.current&&p({kind:de(m)?"starting":"offline"});return}if(!_){B.current&&p({kind:"needsCode"});return}try{let O=await q.status(f,_);B.current&&p({kind:"ready",status:O})}catch(O){if(!B.current)return;O instanceof M&&O.kind==="unauthorized"?(e.secureStorage.remove(F),h(""),p({kind:"needsCode"})):p({kind:"offline"})}},[e,f,P,l]);(0,S.useEffect)(()=>{B.current=!0,w();let m=window.setInterval(()=>{w()},pr);return()=>{B.current=!1,window.clearInterval(m)}},[w]),(0,S.useEffect)(()=>{if(c.kind==="ready"){let m=c.status;e.reportHealth(m.paired&&m.connected?{status:"healthy",summary:e.t("health.online",{name:m.device_name})}:{status:"degraded",summary:e.t(m.paired?"health.disconnected":"health.unpaired")})}else c.kind==="offline"&&e.reportHealth({status:"degraded",summary:e.t(P?"health.disabled":"health.offline")})},[e,c,P]);let U=async m=>{L(!0),A("");try{await m()}catch(_){A(ee(e,_))}finally{L(!1)}},R=m=>U(async()=>{if(!P)return;if(!m){e.storage.set(ve,"off"),k(await P.stop()),await w();return}if(e.storage.set(ve,"on"),e.storage.remove(ne),!de(d)&&await Fe(f)){G?.(),await w();return}let _=await P.start();k(_),_.token&&(e.secureStorage.set(F,_.token),h(_.token)),await hr(f,()=>B.current)&&G?.(),await w()}),$=d?.state==="exited"?d:null,Y=!!P&&!de(d)&&(c.kind==="ready"||c.kind==="needsCode");return(0,t.jsxs)("div",{className:"da-app",children:[(0,t.jsx)(b.PageHeader,{title:e.t("page.title"),subtitle:e.t("page.subtitle")}),(0,t.jsx)("div",{className:"da-tabs",role:"tablist","aria-label":e.t("page.title"),children:["local","remote","providers"].map(m=>(0,t.jsx)("button",{type:"button",role:"tab","aria-selected":n===m,className:n===m?"da-tab da-tab-active":"da-tab",onClick:()=>g(m),children:e.t(`tabs.${m}`)},m))}),y&&(0,t.jsx)("div",{role:"alert",className:"da-alert",children:y}),n==="remote"&&(0,t.jsx)(Me,{ctx:e,server:r,workspace:i,localDeviceId:c.kind==="ready"&&c.status.paired?c.status.device_id:""}),n==="providers"&&(0,t.jsx)(Ke,{ctx:e,workspace:i,localDeviceId:c.kind==="ready"&&c.status.paired?c.status.device_id:""}),n==="local"&&(0,t.jsxs)(t.Fragment,{children:[P&&(0,t.jsx)(yr,{ctx:e,status:d,external:Y,exit:$,busy:K,onToggle:m=>{R(m)}}),c.kind==="checking"&&(0,t.jsx)(b.Card,{children:(0,t.jsx)("span",{className:"da-muted",children:e.t("common.checking")})}),c.kind==="starting"&&(0,t.jsx)(b.Card,{children:(0,t.jsx)("span",{className:"da-muted",children:e.t("service.starting")})}),c.kind==="offline"&&!P&&(0,t.jsx)(wr,{ctx:e,localUrl:f}),c.kind==="needsCode"&&(0,t.jsx)(_r,{ctx:e,busy:K,onPair:m=>U(async()=>{let _=await q.pair(f,m).catch(O=>{throw O instanceof M&&O.code==="code_wrong"?new Error(e.t("pair.wrong")):O instanceof M&&O.code==="locked"?new Error(e.t("pair.locked")):O});e.secureStorage.set(F,_.token),h(_.token),G?.()})}),c.kind==="ready"&&(0,t.jsx)(Cr,{ctx:e,status:c.status,server:r,workspace:i,busy:K,run:U,localUrl:f,token:l,onStatus:m=>p({kind:"ready",status:m})})]})]})}async function hr(e,r){for(let i=0;i<30&&r();i+=1)try{return await q.info(e),!0}catch{await new Promise(n=>window.setTimeout(n,500))}return!1}function yr({ctx:e,status:r,external:i,exit:n,busy:v,onToggle:g}){let f=de(r),l=!f&&i,h=f||l,c=n?.exitCode===vr;return(0,t.jsxs)(b.Card,{children:[(0,t.jsxs)("div",{className:"da-switch",children:[(0,t.jsxs)("div",{children:[(0,t.jsx)(b.CardTitle,{children:e.t("service.title")}),(0,t.jsx)("p",{className:"da-text",children:e.t(l?"service.onExternalBody":h?"service.onBody":"service.offBody")})]}),(0,t.jsx)(b.Toggle,{checked:h,disabled:v||l,label:e.t("service.title"),onChange:g})]}),f&&(0,t.jsx)("p",{className:"da-muted",children:e.t("service.followsApp")}),l&&(0,t.jsx)("p",{className:"da-muted",children:e.t("service.external")}),!h&&c&&(0,t.jsx)("p",{className:"da-muted",children:e.t("service.portTaken")}),n&&!c&&n.exitCode!==0&&(0,t.jsx)("div",{role:"alert",className:"da-alert",children:e.t("service.crashed",{error:n.error||`exit ${n.exitCode??n.signal??"?"}`})})]})}function wr({ctx:e,localUrl:r}){return(0,t.jsxs)(b.Card,{children:[(0,t.jsx)(b.CardTitle,{children:e.t("offline.title")}),(0,t.jsx)("p",{className:"da-text",children:e.t("offline.body",{url:r})}),(0,t.jsx)("div",{className:"da-step",children:e.t("offline.once")}),(0,t.jsx)(V,{ctx:e,value:"npx toc-ocra@latest run"}),(0,t.jsx)("div",{className:"da-step",children:e.t("offline.service")}),(0,t.jsx)(V,{ctx:e,value:"npm install -g toc-ocra@latest && toc-ocra service:install"}),(0,t.jsx)("p",{className:"da-muted",children:e.t("offline.node")})]})}function _r({ctx:e,busy:r,onPair:i}){let[n,v]=(0,S.useState)(""),g=/^\d{6}$/.test(n);return(0,t.jsxs)(b.Card,{children:[(0,t.jsx)(b.CardTitle,{children:e.t("pair.title")}),(0,t.jsx)("p",{className:"da-text",children:e.t("pair.body")}),(0,t.jsx)(V,{ctx:e,value:"toc-ocra code"}),(0,t.jsxs)("form",{className:"da-row",onSubmit:f=>{f.preventDefault(),g&&i(n)},children:[(0,t.jsx)(b.Input,{value:n,onChange:f=>v(f.target.value.replace(/\D/g,"").slice(0,6)),placeholder:e.t("pair.placeholder"),inputMode:"numeric",autoComplete:"one-time-code","aria-label":e.t("pair.placeholder"),className:"da-code-input"}),(0,t.jsx)(b.Btn,{primary:!0,type:"submit",disabled:!g||r,children:e.t("pair.submit")})]})]})}function Cr({ctx:e,status:r,server:i,workspace:n,busy:v,run:g,localUrl:f,token:l,onStatus:h}){let c=r.paired&&i&&!me(r.server_url,i.origin),p=()=>g(async()=>{r.device_id&&i&&me(r.server_url,i.origin)&&await e.core.request(`/devices/${encodeURIComponent(r.device_id)}/agent`,{method:"DELETE"}).catch(()=>{}),e.storage.set(ne,"off"),h(await q.unpair(f,l))});return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(b.Card,{children:[(0,t.jsx)(b.CardTitle,{children:e.t("status.title")}),(0,t.jsxs)("dl",{className:"da-grid",children:[(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.service")}),(0,t.jsxs)("dd",{children:["toc-ocra ",r.version," \xB7 ",r.hostname]})]}),(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.binding")}),(0,t.jsx)("dd",{children:(0,t.jsx)(kr,{ctx:e,status:r})})]}),r.paired&&(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.device")}),(0,t.jsx)("dd",{children:r.device_name})]}),r.paired&&(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.server")}),(0,t.jsx)("dd",{className:"da-mono",children:r.server_url})]}),r.paired&&(0,t.jsxs)("div",{children:[(0,t.jsx)("dt",{children:e.t("status.runs")}),(0,t.jsx)("dd",{children:r.active_runs})]})]}),r.paired&&!r.connected&&r.connection_error&&(0,t.jsx)("p",{className:"da-muted",children:e.t("status.lastError",{error:r.connection_error})}),r.token_from_env&&(0,t.jsx)("p",{className:"da-muted",children:e.t("status.tokenFromEnv")}),r.paired&&(0,t.jsx)("div",{className:"da-row",children:(0,t.jsx)(b.Btn,{danger:!0,disabled:v||r.token_from_env,onClick:()=>{p()},children:e.t("status.unpair")})})]}),(!r.paired||r.unauthorized||c)&&(0,t.jsx)(Nr,{ctx:e,status:r,server:i,workspace:n,busy:v,run:g,localUrl:f,token:l,onStatus:h,rebind:r.paired}),(0,t.jsxs)(b.Card,{children:[(0,t.jsx)(b.CardTitle,{children:e.t("cli.title")}),(0,t.jsxs)("ul",{className:"da-list",children:[r.providers.filter(d=>d.installed).map(d=>(0,t.jsxs)("li",{children:[(0,t.jsx)("span",{className:"da-mono",title:d.path,children:d.id}),(0,t.jsx)("span",{className:"da-muted",children:d.version}),d.runnable?(0,t.jsx)(b.Badge,{variant:"ok",children:e.t("cli.runnable")}):(0,t.jsx)(b.Badge,{variant:"muted",children:e.t("cli.detectOnly")})]},d.id)),!r.providers.some(d=>d.installed)&&(0,t.jsx)("li",{className:"da-muted",children:e.t("cli.none")})]}),(0,t.jsx)("div",{className:"da-row",children:(0,t.jsx)(b.Btn,{disabled:v,onClick:()=>{g(async()=>h(await q.detect(f,l)))},children:e.t("cli.redetect")})})]})]})}function kr({ctx:e,status:r}){return r.paired?r.unauthorized?(0,t.jsx)(b.Badge,{variant:"err",children:e.t("binding.revoked")}):r.connected?(0,t.jsx)(b.Badge,{variant:"ok",children:e.t("binding.online")}):(0,t.jsx)(b.Badge,{variant:"warn",children:e.t("binding.connecting")}):(0,t.jsx)(b.Badge,{variant:"muted",children:e.t("binding.unpaired")})}function Nr({ctx:e,status:r,server:i,workspace:n,busy:v,run:g,localUrl:f,token:l,onStatus:h,rebind:c}){let[p,d]=(0,S.useState)([]),[k,y]=(0,S.useState)("new"),[A,K]=(0,S.useState)(r.hostname),[L,B]=(0,S.useState)(i?.origin??"");(0,S.useEffect)(()=>{i?.origin&&B(i.origin)},[i?.origin]),(0,S.useEffect)(()=>{if(!n?.id)return;let w=!1;return e.core.request(`/workspaces/${encodeURIComponent(n.id)}/devices`).then(U=>{w||d((U.items??[]).filter(R=>R.is_mine))}).catch(()=>{w||d([])}),()=>{w=!0}},[e,n?.id]);let P=()=>g(async()=>{if(!n?.id)throw new Error(e.t("bind.noWorkspace"));if(!L.trim())throw new Error(e.t("bind.noServer"));let w=k,U;if(k==="new"){if(!r.machine_id)throw new Error(e.t("bind.identityRequired"));let R=await e.core.request("/devices/machine/pairing-code",{method:"POST",body:JSON.stringify({machine_id:r.machine_id,hostname:A.trim()||r.hostname,platform:r.platform,current_device_id:r.paired?r.device_id:""})});w=R.device_id,y(w),U=R}else U=await e.core.request(`/devices/${encodeURIComponent(w)}/pairing-code`,{method:"POST"});h(await q.enroll(f,l,L.trim(),U.code)),e.storage.remove(ne),e.notify({title:e.t("page.title"),body:e.t("bind.done",{server:i?.name??L})})});return(0,t.jsxs)(b.Card,{children:[(0,t.jsx)(b.CardTitle,{children:e.t(c?"bind.rebindTitle":"bind.title")}),c&&r.unauthorized&&(0,t.jsx)("p",{className:"da-text",children:e.t("bind.revoked")}),c&&!r.unauthorized&&(0,t.jsx)("p",{className:"da-text",children:e.t("bind.elsewhere",{bound:r.server_url,current:i?.origin??""})}),(0,t.jsx)("p",{className:"da-text",children:e.t("bind.body",{server:i?.name??"",workspace:n?.name??""})}),(0,t.jsxs)("label",{className:"da-field",children:[(0,t.jsx)("span",{children:e.t("bind.server")}),(0,t.jsx)(b.Input,{value:L,onChange:w=>B(w.target.value),spellCheck:!1,className:"da-mono"})]}),(0,t.jsxs)("label",{className:"da-field",children:[(0,t.jsx)("span",{children:e.t("bind.device")}),(0,t.jsxs)("select",{className:"da-select",value:k,onChange:w=>y(w.target.value),children:[(0,t.jsx)("option",{value:"new",children:e.t("bind.newDevice")}),p.map(w=>(0,t.jsxs)("option",{value:w.id,children:[w.name,w.paired?` \xB7 ${e.t("bind.replaces")}`:""]},w.id))]})]}),k==="new"&&(0,t.jsxs)("label",{className:"da-field",children:[(0,t.jsx)("span",{children:e.t("bind.name")}),(0,t.jsx)(b.Input,{value:A,onChange:w=>K(w.target.value),maxLength:100})]}),(0,t.jsx)("div",{className:"da-row",children:(0,t.jsx)(b.Btn,{primary:!0,disabled:v||!n?.id,onClick:()=>{P()},children:e.t(v?"bind.working":"bind.submit")})})]})}function Sr(e){let r=!1,i="",n=new Map,v=new Map,g=async()=>{if(r||e.signal.aborted)return;let l=e.core.context("server"),h=e.secureStorage.get(F,"");if(!(!l?.origin||!h||e.storage.get(ne,"on")==="off")&&!(Date.now()-(n.get(l.origin)??0)<ur)){r=!0;try{let c=e.storage.get(ue,pe),p=await q.status(c,h);if(!p.machine_id||p.token_from_env)return;if(p.paired&&(p.unauthorized||me(p.server_url,l.origin))){i="";return}if(p.active_runs>0){i!==l.origin&&(i=l.origin,e.notify({title:e.t("page.title"),body:e.t("auto.deferred",{server:l.name||l.origin})}));return}let d=await e.core.request("/devices/machine/pairing-code",{method:"POST",body:JSON.stringify({machine_id:p.machine_id,hostname:p.hostname,platform:p.platform,current_device_id:p.paired?p.device_id:""})});await q.enroll(c,h,l.origin,d.code),i="",n.delete(l.origin),v.delete(l.origin),e.notify({title:e.t("page.title"),body:e.t("auto.done",{server:l.name||l.origin,device:d.device_name})})}catch(c){if(!(c instanceof M)||c.kind==="rejected"){n.set(l.origin,Date.now());let p=ee(e,c);v.get(l.origin)!==p&&(v.set(l.origin,p),e.notify({title:e.t("page.title"),body:e.t("auto.failed",{server:l.name||l.origin,error:p})}))}}finally{r=!1}}},f=window.setInterval(()=>{g()},mr);G=()=>{g()},e.registerDisposer(()=>{window.clearInterval(f),G=void 0}),e.registerDisposer(e.core.onContextChange("server",()=>{g()})),window.setTimeout(()=>{g()},1500)}function Pr(e){let r=He(e);if(!r||e.storage.get(ve,"off")!=="on")return;let i=e.storage.get(ue,pe);Fe(i).then(n=>{if(n){window.setTimeout(()=>G?.(),3e3);return}return r.start()}).then(n=>{n&&(n.token&&e.secureStorage.set(F,n.token),window.setTimeout(()=>G?.(),3e3))}).catch(()=>{})}var Yr=Le({activate(e){e.registerNavigation("agent.nav",(0,t.jsx)(fr,{})),e.registerRoute("agent.home",(0,t.jsx)(br,{ctx:e})),Pr(e),Sr(e)}});export{Yr as default};
|
|
2
2
|
//# sourceMappingURL=renderer.js.map
|