openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines (3 loc) • 14.9 kB
JavaScript
import{n as e,r as t}from"./rolldown-runtime-CNOWq4Kh.js";import{Hi as n,Qt as r,Xt as i,et as a,tt as o,vi as s,wa as c,wi as l,xa as u}from"./control-ui-connection-Bfj9beF_.js";import{$d as d,Jd as f,Kp as p,LD as m,PD as h,Qd as g,bD as _,fD as v,iD as y,nD as b,oD as x,oT as ee,pD as te,qd as ne,qp as S,sT as C,tD as w,vD as T}from"./control-ui-boot-shared-DFTSUQQ0.js";import{gr as E,hr as re,mr as ie}from"./deferred-ui-support-D-VedBtc.js";import{n as ae,t as oe}from"./en-session-organization-D0fXDuFy.js";import{r as se,s as ce,t as le}from"./custom-snooze-CYC5cBal.js";import{a as ue,i as D,n as O,o as k,r as de,t as A}from"./session-workspace-recovery.runtime-ONPZ4H21.js";import{r as j}from"./session-mutation-runtime-Bc_oy7yw.js";function M(e,t,n){let r=b(t.gateway.snapshot,n);return r.allowed?!0:(e.sessionData.publishSessionMutationError(t,r.reason),!1)}async function N(e,t,r){let i=[...new Set(r.map(e=>n(e,t.selectedAgentId)))],a=e.sidebarSessionStatusFilter()!==`active`;for(let n of i){if(!e.sessionData.isSessionMutationScopeCurrent(t))return`stale`;try{let r=await t.sessions.reconcileMutation(n);if(!e.sessionData.isSessionMutationScopeCurrent(t))return`stale`;if(r.status!==`refreshed`)return r.status===`failed`&&e.sessionData.publishSessionMutationError(t,r.error),r.status;a&&await e.sessionData.refreshSidebarSessions(n)}catch(n){return e.sessionData.isSessionMutationScopeCurrent(t)?(e.sessionData.publishSessionMutationError(t,n),`failed`):`stale`}}return e.sessionData.isSessionMutationScopeCurrent(t)?`completed`:`stale`}async function P(e,t,r,o,s={}){if(typeof r.archived==`boolean`&&t.some(e=>!e.sessionId?.trim()))return e.sessionData.publishSessionMutationError(o,`Session lifecycle action requires a durable session identity.`),null;let c=o.gateway.snapshot;if(typeof r.archived==`boolean`&&Object.keys(r).length===1&&!t.some(e=>e.archiveGuard)&&(c.phase!==`connected`||c.deferredIntentCount||c.deferredIntentDraining)){let i=[];for(let a of t){if(!e.sessionData.isSessionMutationScopeCurrent(o))return null;let t={key:a.key,agentId:n(a,o.selectedAgentId),expectedSessionId:a.sessionId,...r};if(!M(e,o,{method:`sessions.patch`,params:t,session:a,sessionScope:s.sessionScope,allowDeferred:!0}))return null;await o.sessions.patch(a.key,r,{agentId:t.agentId,expectedSessionId:a.sessionId,onConfirmed:()=>{r.archived&&e.pruneSidebarSessionEntry(a.key)}})&&i.push(a)}return i}let l=[],u=null;for(let i=0;i<t.length;i+=100){if(!e.sessionData.isSessionMutationScopeCurrent(o))return null;let a=t.slice(i,i+100),c={targets:a.map(e=>({key:e.key,agentId:n(e,o.selectedAgentId),...e.sessionId?{expectedSessionId:e.sessionId}:{},...r.archived===!0?e.archiveGuard:{}})),patch:r},d=b(o.gateway.snapshot,{method:`sessions.patchMany`,params:c,sessionScope:s.sessionScope,session:y(a)});if(!d.allowed){u=d.reason,l.length===0&&e.sessionData.publishSessionMutationError(o,d.reason);break}try{let t=await o.sessions.patchMany(c.targets,c.patch);if(!t||!e.sessionData.isSessionMutationScopeCurrent(o))return null;l.push({rows:a,result:t})}catch(t){u=t,l.length===0&&e.sessionData.publishSessionMutationError(o,t);break}}if(l.length===0||!s.deferListRefresh&&await N(e,o,t)===`stale`||!e.sessionData.isSessionMutationScopeCurrent(o))return null;let d=[],f=l.flatMap(({rows:t,result:n})=>n.outcomes.flatMap((n,i)=>{if(!n.ok)return d.push(`${n.key}: ${A(new a(n.error))}`),[];let o=t[i];return o&&r.archived===!0&&e.pruneSidebarSessionEntry(o.key),o?[o]:[]})),p=u===null?``:i(u);return p&&d.push(p),d.length>0&&e.sessionData.publishSessionMutationError(o,d.join(`; `)),f}async function F(e,t,r,i){if(!e.sessionData.isSessionMutationScopeCurrent(i)||!t.sessionId)return;let a=n(t,i.selectedAgentId);if(M(e,i,{method:`sessions.setInvolvement`,requiredScope:`operator.read`,sessionScope:!0}))try{if(await C(i.client,{key:t.key,agentId:a,expectedSessionId:t.sessionId,hidden:r}),!e.sessionData.isSessionMutationScopeCurrent(i))return;i.sessions.patchRowLocal(t.key,{hiddenFromInvolvingMe:r},{agentId:a,sessionId:t.sessionId}),await e.sessionData.refreshSidebarSessions(a)}catch(t){e.sessionData.isSessionMutationScopeCurrent(i)&&e.sessionData.publishSessionMutationError(i,t)}}function I(){return(I=e((()=>{o(),r(),w(),s(),ee(),O()})))()}async function L(e,t,r,i,a={}){if(!e.sessionData.isSessionMutationScopeCurrent(i))return`stale`;let o=n(t,i.selectedAgentId),s={key:t.key,...r,agentId:o,...t.sessionId?{expectedSessionId:t.sessionId}:{}};if((typeof r.archived==`boolean`||r.snoozedUntil!==void 0)&&!t.sessionId?.trim())return e.sessionData.publishSessionMutationError(i,`Session lifecycle action requires a durable session identity.`),`failed`;if(!M(e,i,{method:`sessions.patch`,params:s,allowDeferred:x(r),sessionScope:a.sessionScope,session:t}))return`failed`;try{let n=i.gateway.snapshot,s=r.archived===!0&&(n.phase!==`connected`||n.deferredIntentCount||n.deferredIntentDraining),c=()=>i.sessions.patch(t.key,r,{agentId:o,...t.sessionId?{expectedSessionId:t.sessionId}:{},...a.deferListRefresh?{deferListRefresh:!0}:{},...s||a.onConfirmed?{onConfirmed:n=>{s&&e.pruneSidebarSessionEntry(t.key),a.onConfirmed?.(n)}}:{},...a.onDeferredSettled?{onDeferredSettled:a.onDeferredSettled}:{}}),l=r.archived===!0?await de({action:`archive`,session:{...t,agentId:o},scope:i,isCurrent:()=>e.sessionData.isSessionMutationScopeCurrent(i),request:c}):await c();return e.sessionData.isSessionMutationScopeCurrent(i)?l?`queued`in l?`completed`:((r.pinned===!1||r.archived===!0)&&e.pruneSidebarSessionEntry(t.key),!a.deferListRefresh&&e.sidebarSessionStatusFilter()!==`active`&&(await e.sessionData.refreshSidebarSessions(o),!e.sessionData.isSessionMutationScopeCurrent(i))?`stale`:`completed`):(i.sessions.state.error&&e.sessionData.publishSessionMutationError(i,i.sessions.state.error),`failed`):`stale`}catch(t){return e.sessionData.isSessionMutationScopeCurrent(i)?(a.handleError?.(t)||e.sessionData.publishSessionMutationError(i,t),`failed`):`stale`}}function R(){return(R=e((()=>{j(),s(),I(),O()})))()}var z=t({archiveConfirmedSessionsWithUndo:()=>J,archiveSessionWithUndo:()=>K,archiveSessionsWithUndo:()=>q,captureSessionArchivePins:()=>X,confirmRunningSessionsArchive:()=>G,confirmSessionArchive:()=>H,promoteSession:()=>V,snoozeSessionWithUndo:()=>B});async function B(e,t,n,r){let{snoozedUntil:i,rememberCustom:a}=n;if(await L(e,t,{snoozedUntil:i},r,{sessionScope:!0})!==`completed`||!e.sessionData.isSessionMutationScopeCurrent(r))return;a&&ce(le(r.client.gatewayUrl,r.client.recoveryScope),i);let o=Q(e,r);_({message:c(`sessionsView.sessionSnoozed`,{time:p(i)}),actionLabel:c(`common.undo`),onAction:()=>void L(o,t,{snoozedUntil:null},r,{sessionScope:!0})})}async function V(e,t,n){await L(e,t,{sidebarRoot:!0},n,{sessionScope:!0})===`completed`&&e.sessionData.isSessionMutationScopeCurrent(n)&&_({message:c(`sessionsView.sessionMovedToTopLevel`)})}async function H(e,t,r){if(!r())return null;if(e.hasArchiveDescendants&&t.gateway.snapshot.phase!==`connected`){let n={title:c(`sessionsView.archiveSessionTitle`),message:c(`sessionsView.archiveSessionNestedOffline`,{session:e.label})+(U(e)?`
`+c(`sessionsView.archiveRunningSessions`):``),confirmLabel:c(`sessionsView.archiveThisSession`),signal:t.signal};return await D(W([e],t),e=>E(e?k(n,e):n),Boolean)&&r()?{kind:`single`}:null}if(e.hasArchiveDescendants){let{session:i}=await t.sessions.describe({key:e.key,agentId:n(e,t.selectedAgentId)},{refresh:!0});if(!r())return null;if(!i||!i.sessionId||i.sessionId!==e.sessionId||i.archived)throw Error(c(`sessionsView.archiveTreeChanged`));if(l(i.key))throw Error(c(`sessionsView.archiveTreeRootRequired`));let a=[],o=new Map,s=new Set,u=[{row:i,ancestors:[]}];for(;u.length>0;){let{row:e,ancestors:n}=u.shift();if(s.has(e.key))continue;s.add(e.key);let c=!l(e.key);if(e.archived||c&&e!==i&&(e.sidebarRoot||e.category?.trim()))continue;c&&(a.push(e),o.set(e.key,n));let d=await g({sessions:t.sessions,parentKey:e.key,isCurrent:r});if(!r()||d===null)return null;let f=c?[e,...n]:n;u.push(...d.map(e=>({row:e,ancestors:f})))}if(!r())return null;if(a.some(e=>!e.sessionId))throw Error(c(`sessionsView.archiveTreeChanged`));if(a.length>1){let i=a.toReversed().map(r=>({key:r.key,agentId:r.agentId,sessionId:r.sessionId,sharingRole:r.sharingRole,label:m(r.key,r),pinned:r.pinned===!0,archived:r.archived,category:r.category,active:r.key===e.key&&e.active,archiveGuard:{expectedSidebarRoot:r.sidebarRoot===!0,expectedCategory:r.category??null,expectedArchived:!1,expectedSidebarAncestors:(o.get(r.key)??[]).map(e=>{if(!e.sessionId)throw Error(c(`sessionsView.archiveTreeChanged`));return{key:e.key,agentId:n(e,t.selectedAgentId),expectedSessionId:e.sessionId,expectedSidebarRoot:e.sidebarRoot===!0,expectedCategory:e.category??null}})}})),s=a.filter(e=>o.get(e.key)?.length===1).length,l={title:c(`sessionsView.archiveSessionTitle`),message:c(s===1?`sessionsView.archiveSessionNestedOne`:`sessionsView.archiveSessionNestedOther`,{count:String(s),session:e.label})+(a.some(U)?`
`+c(`sessionsView.archiveRunningSessions`):``),confirmLabel:c(`sessionsView.archiveAllSessions`,{count:String(a.length)}),alternativeLabel:c(`sessionsView.archiveThisSession`),groupChoicesOnMobile:!0,signal:t.signal},u=a.map(e=>({key:e.key,agentId:n(e,t.selectedAgentId),label:m(e.key,e),hasAutomation:e.hasAutomation})),d=await D(W(u,t),e=>re(e?{...k(l,e),confirmLabel:l.confirmLabel}:l),e=>e!==null);return!r()||d===null?null:d===`confirm`?{kind:`tree`,rows:i}:{kind:`single`}}}return await G([e],t)&&r()?{kind:`single`}:null}function U(e){return!!((e.gatewayHasActiveRun??e.hasActiveRun)||e.hasActiveSubagentRun)}function W(e,t){return{action:`archive`,sessions:e,gateway:t.gateway,selectedAgentId:t.selectedAgentId,signal:t.signal}}async function G(e,t){let n=e.some(U),r=e.length===1?e[0]:void 0,i={message:n?r?c(`sessionsView.archiveRunningSessionConfirm`,{session:r.label}):c(`sessionsView.archiveRunningSessions`):``,confirmLabel:r?c(`sessionsView.archiveSession`):c(`sessionsView.archiveSessionCount`,{count:String(e.length)}),signal:t.signal};return D(W(e,t),e=>e?E(k(i,e)):n?E(i):Promise.resolve(!0),Boolean)}async function K(e,t,n){if(!e.sessionData.isSessionMutationScopeCurrent(n))return;let r;try{r=await H(t,n,()=>e.sessionData.isSessionMutationScopeCurrent(n))}catch(t){e.sessionData.isSessionMutationScopeCurrent(n)&&e.sessionData.publishSessionMutationError(n,t);return}if(!r)return;if(r.kind===`tree`){await J(e,r.rows,n);return}let i=n.sessions.beginArchive(t.key,t.sessionId);if(!i)return;let a=X(n,[t.key]),o;try{o=await L({sessionData:e.sessionData,selectSession:t=>e.selectSession(t),sidebarSessionStatusFilter:()=>e.sidebarSessionStatusFilter(),pruneSidebarSessionEntry:t=>a.prune(t,()=>e.pruneSidebarSessionEntry(t))},t,{archived:!0},n,{sessionScope:!0})}finally{i()}o===`completed`&&e.sessionData.isSessionMutationScopeCurrent(n)&&_({message:c(`sessionsView.sessionArchived`),actionLabel:c(`common.undo`),onAction:Y(e,[{session:t,pinned:t.pinned}],n,a)})}async function q(e,t,n){e.sessionData.isSessionMutationScopeCurrent(n)&&await G(t,n)&&await J(e,t,n)}async function J(e,t,n){if(t.length===0||!e.sessionData.isSessionMutationScopeCurrent(n))return;let r=Q(e,n),i=t.flatMap(e=>{let t=n.sessions.beginArchive(e.key,e.sessionId);return t?[{row:e,finish:t}]:[]});if(i.length===0)return;let a=i.map(({row:e})=>e),o=X(n,a.map(e=>e.key)),s,l={...r,pruneSidebarSessionEntry:e=>o.prune(e,()=>r.pruneSidebarSessionEntry(e)),sessionData:{...r.sessionData,publishSessionMutationError:(e,t)=>{s={status:`failed`,error:t},r.sessionData.publishSessionMutationError(e,t)}}},u;try{u=await P(l,a,{archived:!0},n,{sessionScope:!0})}finally{for(let{finish:e}of i)e()}if(!u||u.length===0)return s??`stale`;let d=u.map(e=>({session:e,pinned:e.pinned}));return _({message:d.length===1?c(`sessionsView.sessionArchived`):c(`sessionsView.sessionsArchived`,{count:String(d.length)}),actionLabel:c(`common.undo`),onAction:Y(r,d,n,o),fifo:!0}),s??`completed`}function Y(e,t,n,r){let i=Q(e,n);return()=>{r.restore(()=>fe(i,t,n)).catch(e=>{i.sessionData.publishSessionMutationError(n,e)})}}function X(e,t){let{navigation:n}=e.context,r=Z(e),i=t.filter(e=>n.snapshot.sidebarEntries.includes(`session:`+e)),a;return{restore:async e=>{if(!r())return;let t=new Set,o=new Set(n.snapshot.sidebarEntries),s=n.subscribe(({sidebarEntries:e})=>{for(let n of i){let r=`session:`+n;n!==a&&e.includes(r)!==o.has(r)&&t.add(n)}o=new Set(e)});try{let a=await e();if(!r())return;let o=n.snapshot.sidebarEntries,s=i.filter(e=>a.includes(e)&&!t.has(e)&&!o.includes(`session:`+e));s.length>0&&n.update({sidebarEntries:[...o,...s.map(e=>`session:`+e)]})}finally{s()}},prune(e,t){a=e;try{t()}finally{a=void 0}}}}function Z(e){let t=e.sessions.captureConnectionScope();if(t)return()=>e.sessions.isConnectionScopeCurrent(t);let n=v(e.gateway).key;return()=>e.allowDeferred===!0&&f(e.context,e)&&v(e.gateway).key===n}function Q(e,t){let n=Z(t);return{pruneSidebarSessionEntry:t=>e.pruneSidebarSessionEntry(t),selectSession:t=>e.selectSession(t),sidebarSessionStatusFilter:()=>e.sidebarSessionStatusFilter(),sessionData:{refreshSidebarSessions:t=>e.sessionData.refreshSidebarSessions(t),isSessionMutationScopeCurrent:n,publishSessionMutationError:(t,r)=>{e.sessionData.isSessionMutationScopeCurrent(t)?e.sessionData.publishSessionMutationError(t,r):n()&&_({message:i(r)})}}}}async function fe(e,t,n){let r,i=n.gateway.snapshot,a=i.phase!==`connected`||!!(i.deferredIntentCount||i.deferredIntentDraining),o=t.map(e=>e.session);if(a)return(await Promise.all(o.map(async t=>{let r=Promise.withResolvers();return await L(e,t,{archived:!1},n,{deferListRefresh:!0,sessionScope:!0,onConfirmed:e=>r.resolve(e.entry.sessionId===t.sessionId),onDeferredSettled:()=>r.resolve(!1)})===`completed`&&await r.promise?t.key:null}))).filter(e=>e!==null);if(t.length===1){let{session:i,pinned:a}=t[0];if(await L(e,i,{archived:!1,...a?{pinned:!0}:{}},n,{deferListRefresh:!0,sessionScope:!0})!==`completed`)return[];r=[i.key]}else{let i=await P(e,o,{archived:!1},n,{deferListRefresh:!0,sessionScope:!0});if(!i)return[];r=i.map(e=>e.key);let a=t.flatMap(({session:e,pinned:t})=>t&&i.includes(e)?[e]:[]);if(a.length>0&&!await P(e,a,{pinned:!0},n,{deferListRefresh:!0,sessionScope:!0})&&!e.sessionData.isSessionMutationScopeCurrent(n))return[]}if(!e.sessionData.isSessionMutationScopeCurrent(n))return[];n.sessions.invalidate();try{let t=await n.sessions.refreshReplacement();if(!e.sessionData.isSessionMutationScopeCurrent(n))return[];!t&&n.sessions.state.error&&e.sessionData.publishSessionMutationError(n,n.sessions.state.error)}catch(t){e.sessionData.isSessionMutationScopeCurrent(n)&&e.sessionData.publishSessionMutationError(n,t)}return r}function $(){return($=e((()=>{te(),u(),oe(),r(),h(),d(),se(),s(),S(),T(),ie(),ue(),ne(),I(),R(),ae()})))()}export{V as a,R as c,P as d,M as f,$ as i,L as l,q as n,z as o,F as p,G as r,B as s,K as t,I as u};