UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 10.7 kB
import{n as e,r as t}from"./rolldown-runtime-CNOWq4Kh.js";import{Dl as n,Ol as r,Rl as i,Vl as a,v as o,y as s}from"./_4____________________________.js";import{i as c,l,n as u}from"./account-BSv2LwQC.js";import{B as d,F as f,I as p,L as m,M as h,N as g,R as _,S as v,_ as y,a as ee,b,c as x,d as te,f as ne,j as S,l as C,m as w,o as T,p as E,t as D,u as re,v as O,z as ie}from"./src-UIxrOiBg.js";import{a as k,i as A,o as j,r as M}from"./device-key-DH9ZSZCT.js";function N(e){return e.request(`openclawId.authority`,{action:`device_mail`,operation:`context`})}async function ae(e,t){let n=await N(e()),r=n.link.clawId,i=r?await k(r):void 0,a=i?h(i.publicKey):null,o=n.link.ownerDevices?.find(e=>e.devicePub===a);if(!r||!i||!o?.cert.body.caps.includes(`approve_device`))throw Error(`This browser does not hold an active owner device key.`);let s=P(e,r,i,o.cert,t),l=c();if(!l)throw Error(`Sign in with OpenClaw ID to approve another device.`);let u=await A(r),d=Math.floor(Date.now()/1e3),f={account_id:l.sub,device_pub:o.cert.body.device_pub,peer_key:g(u.publicKey),ts:d},p=_(await u.sign(y(f)));return await s.call(`bind`,{...f,account_id:void 0,label:o.label,peer_key_proof:p}),{...s,signer:i,own:o.cert,context:n,clawId:r}}function P(e,t,n,r,i){return{async call(a,o,s=`0`){i.throwIfAborted();let l=c();if(!l)throw Error(`OpenClaw ID sign-in expired. Sign in again to continue.`);let u=w(a,t,s),d=o===void 0?``:JSON.stringify(o),f=await O({...u,body:new TextEncoder().encode(d),ts:Math.floor(Date.now()/1e3)},n);i.throwIfAborted();let p=await e().request(`openclawId.authority`,{action:`device_mail`,operation:a,body:d,after:s,signature:f,idToken:l.idToken,certificate:r},{timeoutMs:E,signal:i});if(i.throwIfAborted(),p.status<200||p.status>=300)throw Error(`Owner-device mail failed (HTTP ${p.status}).`);return p.body?JSON.parse(p.body):void 0}}}function F(){return(F=e((()=>{D(),u(),M()})))()}function oe(e,t){if(e===void 0)return;if(typeof e!=`object`||!e||Array.isArray(e))return null;let{peer_key:n,description:r,...i}=e;return Object.keys(i).length>0||!p(n)||n===t||!b(r)?null:{peer_key:n,description:r}}function I(e,t,r=()=>{},i=()=>{}){let s=new AbortController,c=e,l=new Map,u=new Map,g=new Set,_,y=null,b=`0`,w,E=!1,D=!1,O=!1,k=!1,A,j=Promise.withResolvers(),M=()=>g.forEach(e=>e()),P=(e,t)=>({id:e,label:t.start.label,...t.start.node?{tools:{commands:[...t.start.node.description.commands],caps:[...t.start.node.description.caps]}}:{},code:t.code,busy:t.busy,error:t.error??(t.expires<=L()?R:void 0),expired:t.expires<=L(),uncertain:!!(t.submissionStarted&&t.error)}),F=(e,t=!1)=>{let n=l.get(e);n?.busy&&!t?(n.cancelled=!0,M()):(clearTimeout(n?.expiryTimer),n?.secret?.fill(0),l.delete(e),M())},I=e=>{let t=l.get(e);if(!t)return;clearTimeout(t.expiryTimer);let n=t.expires*1e3-Date.now();n>0?t.expiryTimer=setTimeout(()=>I(e),n):(t.busy||u.set(e,P(e,t)),F(e))},H=async(e,t,n)=>{s.signal.throwIfAborted(),await _.call(`send`,{to:e.sender,payload:{kind:`enrollment`,request_id:e.start.request_id,step:t,data:n}})},U=async(e,n)=>{let r=n.data;if(l.size>=16||l.has(n.request_id)||!m(n.request_id)||e.received_at<L()-300||e.received_at>L()+30||r.claw_id!==_.clawId||!f(r.device_pub)||!p(r.peer_key)||typeof r.label!=`string`||r.label.length<1||r.label.length>64||typeof r.commit!=`string`||!/^[a-f0-9]{64}$/.test(r.commit))return;let i=oe(r.node,r.peer_key);if(i===null)return;let a=await S(r.device_pub);if(e.from.device_fp!==a)return;let o={start:{request_id:n.request_id,claw_id:_.clawId,device_pub:r.device_pub,peer_key:r.peer_key,label:r.label,...i?{node:i}:{}},commitment:r.commit,sender:a,expires:e.received_at+300,ephemeral:await T(),busy:!1};if(s.signal.throwIfAborted(),![...l.values()].some(e=>e.sender===a&&e.busy)){for(let[e,t]of l)t.sender===a&&F(e);u.clear(),l.set(n.request_id,o),I(n.request_id);try{await H(o,`accept`,{eph_pub:ie(o.ephemeral.publicKey),device_pub:h(_.signer.publicKey)})}catch(e){throw F(n.request_id),e}M(),t()}},W=async e=>{let t=e.payload;if(t.kind===`notice`&&e.claw_id===_.clawId){t.event===`kit_used`?r((await N(c)).link.ownerDevices?.find(e=>e.deviceFp===t.ref)?.label):t.event===`member_device_added`&&i();return}if(t.kind!==`enrollment`||e.claw_id!==_.clawId)return;if(t.step===`start`){await U(e,t);return}let n=l.get(t.request_id);if(!n||e.from.device_fp!==n.sender)return;if(t.step===`abort`){F(t.request_id);return}if(t.step!==`key`||n.code||n.expires<=L())return;if(typeof t.data.eph_pub!=`string`||!/^[a-f0-9]{64}$/.test(t.data.eph_pub)){F(t.request_id);return}let a=d(t.data.eph_pub);await ne(n.commitment,a,n.start)?(n.secret=await C(n.ephemeral,a),n.transcript=await re({start:n.start,commit:n.commitment,ephemeralPublicE:n.ephemeral.publicKey,ephemeralPublicN:a,devicePubE:h(_.signer.publicKey)}),n.code=await x(n.secret,n.transcript),s.signal.throwIfAborted(),M()):(await H(n,`abort`,{reason:`commitment_mismatch`}),F(t.request_id))},G=async()=>{let e=[...l].filter(([,e])=>e.submissionStarted&&e.error&&e.sealed&&!e.cancelled&&e.expires>L());if(e.length===0)return;let{link:t}=await N(c);for(let[n,r]of e)t.ownerDevices?.some(e=>e.devicePub===r.start.device_pub)&&l.get(n)===r&&!r.cancelled&&r.expires>L()&&!k&&(await H(r,`sealed`,r.sealed),F(n,!0))},K=e=>e instanceof n||(e instanceof o?e.gatewayCode===`UNAVAILABLE`&&e.retryable:!c.connected),q=async e=>{let t=new AbortController;A=()=>t.abort(),await a(e,AbortSignal.any([s.signal,t.signal])).catch(()=>{}),A=void 0},J=async()=>{if(O)A?.();else{O=!0;try{let e=B;for(;!s.signal.aborted;)if(k)await q(V);else try{_||(_=await ae(()=>c,s.signal),M());let t=await _.call(`receive`,void 0,b);e=B,y&&(y=null,M());for(let[e,t]of l)t.expires<=L()&&I(e);for(let e of t.messages)await W(e);t.cursor&&(b=t.cursor),await G()}catch(t){if(s.signal.aborted||!k&&!K(t))throw t;Y(t),await q(e),e=Math.min(e*2,V)}}finally{O=!1}}},Y=e=>{s.signal.aborted||(y=e instanceof Error?e.message:String(e),M())},X=async()=>{let{link:e}=await N(c);if(s.signal.throwIfAborted(),w=e.hostname,!navigator.locks||!e.clawId)throw Error(`This browser needs Web Locks to coordinate owner approval across tabs.`);await navigator.locks.request(`openclaw-id-approval:${e.clawId}`,{ifAvailable:!0},async e=>{if(!e)throw Error(`Another tab is listening for owner approval. Use that tab, or close it and Retry here.`);D=!0,J().catch(Y),await j.promise})};return{read(){return{ready:!!_,error:y,hostname:w,requests:[...u.values(),...[...l].map(([e,t])=>P(e,t))]}},subscribe(e){return g.add(e),()=>{g.delete(e)}},start(){E||(E=!0,X().catch(Y))},suspend(){k=!0},resume(e){c=e,k=!1,A?.(),this.retry()},retry(){y&&!s.signal.aborted&&(y=null,M(),D?J().catch(Y):X().catch(Y))},stop(){s.abort(),j.resolve(),u.clear();for(let e of l.keys())F(e,!0);g.clear()},async decide(e,t,n=null){let r=l.get(e);if(!r||r.busy)return;if(r.expires<=L()){I(e);return}let i=()=>{if(s.signal.throwIfAborted(),k)throw Error(z);if(l.get(e)!==r||r.cancelled||r.expires<=L())throw Error(R)};r.busy=!0,r.error=void 0,M();try{if(i(),!t){await H(r,`abort`,{reason:`sas_mismatch`}),F(e,!0);return}if(!r.code||!r.secret||!r.transcript)throw Error(`Wait for the matching number on both screens.`);let a=await N(c),o=a.link.ownerDevices?.find(e=>e.devicePub===h(_.signer.publicKey));if(!o||!o.cert.body.caps.includes(`approve_device`)||a.link.clawId!==_.clawId||!a.binding)throw Error(`This browser is no longer an active owner device.`);let l=await v(`OwnerDeviceCert`,ee({device_pub:r.start.device_pub,peer_keys:[r.start.peer_key],label:r.start.label,caps:o.cert.body.caps,now:L(),expires_at:n}),_.signer),u=await v(`MembershipChange`,{claw_id:_.clawId,seq:a.link.seq+1,target:{op:`add_owner_device`,cert:l,...r.start.node?{node:{peer_key:r.start.node.peer_key,label:r.start.label,description:r.start.node.description}}:{}},issued_at:L()},_.signer),d={root_pub:a.link.rootPub,binding:a.binding,cert:l};r.sealed=await te(r.secret,r.transcript,d),i(),r.submissionStarted=!0,await c.request(`openclawId.authority`,{action:`submit`,statement:u},{signal:s.signal,retainSentResultOnAbort:!0}),i(),await H(r,`sealed`,r.sealed),F(e,!0)}catch(e){let t=e instanceof Error?e.message:String(e);r.error=r.submissionStarted?`${t} Check Your devices: if this browser was admitted, revoke it before starting a new request.`:t,r.busy=!!r.submissionStarted,M()}}}}var L,R,z,B,V;function H(){return(H=e((()=>{r(),i(),D(),s(),F(),L=()=>Math.floor(Date.now()/1e3),R=`This device request expired or was cancelled.`,z=`Reconnecting to the Gateway. Try again in a moment.`,B=1e3,V=15e3})))()}async function U(e){let t=e.clawId?await k(e.clawId):void 0,n=t?h(t.publicKey):void 0;return e.ownerDevices?.find(e=>e.devicePub===n)}function W(e,t){let n=new Set([t.deviceFp]),r=!0;for(;r;){r=!1;for(let t of e)!n.has(t.deviceFp)&&n.has(t.approvedBy)&&(n.add(t.deviceFp),r=!0)}return e.filter(e=>e.deviceFp!==t.deviceFp&&n.has(e.deviceFp))}function G(e,t){if(t.approvedBy===`root`)return{kind:`kit`};let n=e.find(e=>e.deviceFp===t.approvedBy);return n?{kind:`device`,label:n.label}:{kind:`removed`}}async function K(e,t){let n=e.clawId?await k(e.clawId):void 0,r=n?h(n.publicKey):void 0,i=e.ownerDevices?.find(e=>e.devicePub===r);if(!n||!i||!e.clawId||e.seq===void 0)throw new q;return v(`Revocation`,{claw_id:e.clawId,seq:e.seq,target_fp:await S(t.devicePub),target_kind:`owner_device`,reason:`revoked from the Control UI`,root_epoch:1,issued_at:Math.floor(Date.now()/1e3)},n)}var q;function J(){return(J=e((()=>{D(),M(),q=class extends Error{constructor(){super(`This browser is not an owner device for this claw.`)}}})))()}var Y=t({bindOwnerApproval:()=>se});function X(e){return Z.get(e)}function se(e,t,n,r){let i=null,a=null,o=c(),s=0,u=!1,d=()=>{s+=1,a&&=(a.owner.stop(),Z.delete(a.client),null)},f=()=>{d(),i=null},p=e=>{let o=s;(async()=>{let c=await e.request(`openclawId.status`,{}),l=c.channel===`local_operator`&&c.owner&&c.link.linked?(await e.request(`openclawId.people`,{})).link:null,f=!!(l&&await U(l));if(u||o!==s||i!==e)return;if(a){if(!f||l?.clawId!==a.clawId){d();return}Z.delete(a.client),a.client=e,Z.set(e,a.owner),a.owner.resume(e);return}if(!f||!l?.clawId)return;let p=I(e,()=>t(a?.client??e),t=>n(a?.client??e,t),r);a={owner:p,client:e,clawId:l.clawId},Z.set(e,p),p.start()})().catch(()=>{})},m=()=>{if(u)return;let t=e.snapshot,n=t.phase===`connected`&&o?t.client:null,r=e=>!!(a&&e?.gatewayUrl===a.client.gatewayUrl),s=o&&(n?r(n):Q.has(t.phase)&&r(t.client));a&&!s&&d(),n!==i&&(a?.owner.suspend(),i=n,n&&p(n))},h=()=>{f(),m()},g=[e.subscribe(m),l(e=>{o=e,h()}),j(h)];return m(),()=>{u=!0,g.forEach(e=>e()),f()}}var Z,Q;function $(){return($=e((()=>{u(),M(),H(),J(),Z=new WeakMap,Q=new Set([`connecting`,`starting`,`reconnecting`,`offline`])})))()}export{W as a,U as c,G as i,X as n,J as o,Y as r,K as s,$ as t};