openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
71 lines • 17.6 kB
JavaScript
import{n as e}from"./rolldown-runtime-8BhlS34s.js";import{Po as t,Pu as n,Wl as r,Zl as i,ac as a,jo as o,kl as s,nc as c,wl as l,xu as u,yu as d,zl as f,zu as p}from"./control-ui-core-CBJ5efDh.js";import{ca as m,sa as ee}from"./control-ui-core-DtLivg_r.js";import{G as h,J as g,W as te,Z as ne,at as _,c as re,l as v,rt as y}from"./lit-runtime-IOnANfcZ.js";import{A as ie,O as ae,Xt as oe,Yt as b,gr as se,mr as ce}from"./control-ui-boot-shared-CkXlyZBY.js";import{$n as le,It as ue,fr as de,ir as x,pr as fe,ur as pe,zt as me}from"./control-ui-boot-shared-UG5ko-VG.js";import{Po as he,Zr as S,mt as C,uo as w,xa as T}from"./ui-CmSP4d14.js";import{_ as ge,m as _e,y as ve}from"./control-ui-boot-shared-SQHdJTzb.js";import{di as ye,fi as E,lr as D,pi as O,ui as k}from"./control-ui-boot-shared-XreXSF7h.js";import{$a as A,$i as j,Uc as M,Vc as N,Zi as P,ac as F,af as I,ea as be,eo as L,ic as xe,no as Se,of as Ce,qi as we,rc as R,sf as z,ta as Te,to as Ee}from"./control-ui-boot-shared-dOnP4mAA.js";import{nt as B,ot as V}from"./control-ui-boot-chat-CGYcTLro.js";import{n as De,r as Oe}from"./chat-session-panels-DQzw7cXS.js";function H(e,t,n){let r=T(t);if(e===`item`){let e=ge(b,{...r});return!ve(b,e)||e.kind!==`tool`?void 0:e.suppressChannelProgress&&n?n:e}let i=(typeof r?.name==`string`?r.name.trim():``)||n?.name;if(e===`tool`&&typeof r?.toolCallId==`string`&&r.toolCallId&&i)return r.suppressChannelProgress===!0&&n?n:{...n,itemId:n?.itemId??r.toolCallId,toolCallId:r.toolCallId,kind:`tool`,name:i,title:V({name:i}).label,phase:r.phase===`result`?`end`:r.phase===`start`?`start`:`update`,...r.hideFromChannelProgress===!0?{hideFromChannelProgress:!0}:{},...r.suppressChannelProgress===!0?{suppressChannelProgress:!0}:{}}}function ke(e){let t=e.messages??[],n=t.map(e=>({cards:le(e),refs:Ce(e),runId:se(e)?.runId??he(T(e)?.runId),activity:Array.isArray(T(e)?.activity)?fe(e):void 0})),r=!0,i=(e,t,n)=>{if(!t?.trim()){r=!1;return}let i=new Set(e.refs.flatMap(e=>e.id===t&&e.runId?[e.runId]:[])),a=n??(i.size===1?[...i][0]:e.runId);if(!a||!n&&i.size>1){r=!1;return}return I(a,t)},a=new Set(n.flatMap(e=>e.activity===void 0?[]:[...e.cards.flatMap(t=>t.callId?[i(e,t.callId,t.runId)]:[]),...e.activity.filter(e=>e.kind===`tool`).map(t=>i(e,t.toolCallId))])),o=new Map,s;for(let e of n){let{cards:t,activity:n}=e;if(n!==void 0){for(let t of n){if(t.kind!==`tool`)continue;let n=i(e,t.toolCallId);n&&o.set(n,H(`item`,t,o.get(n))??t);let r=O(`item`,t,s);r!==void 0&&(s=r??void 0)}continue}for(let n of t){if(!n.callId){r=!1;continue}let t=i(e,n.callId,n.runId);if(!t){s={name:n.name,toolCallId:n.callId};continue}if(a.has(t))continue;let c=pe(n,!1),l={itemId:n.callId,toolCallId:n.callId,kind:`tool`,name:n.name,title:V({name:n.name}).label,phase:`end`,status:c===`succeeded`?`completed`:c===`unknown`?void 0:c};o.set(t,l),s={name:n.name,toolCallId:n.callId}}}for(let t of e.inFlightRun?.events??[]){let e=t.data.toolCallId,n=typeof e==`string`?I(t.runId,e):void 0,i=H(t.stream,t.data,n?o.get(n):void 0);i&&(i.toolCallId?.trim()?o.set(I(t.runId,i.toolCallId),i):r=!1);let a=O(t.stream,t.data,s);a!==void 0&&(s=a??void 0)}return{calls:o,complete:r&&(e.completeSnapshot===!0||e.hasMore===!1&&(e.offset??0)===0&&e.totalMessages===t.length),tool:s}}function Ae(e){return e.complete?ie(Array.from(e.calls,([e,t])=>Object.assign({},t,{toolCallId:e}))).total:void 0}function U(){return(U=e((()=>{ce(),_e(),oe(),ae(),E(),de(),x(),B(),z()})))()}function W(e){return JSON.stringify([e.sessionId,e.lastRunId,e.activeRunIds,e.status])}function G(e){return{identity:e,calls:new Map,complete:!1,pending:!1,read:!1,liveToolObserved:!1,preparedCalls:new Set,unidentifiedCall:!1}}var K,q,J,Y;function X(){return(X=e((()=>{ye(),E(),B(),u(),M(),s(),o(),Ee(),we(),be(),A(),R(),U(),z(),K=20,q=2,J=1024,Y=class{constructor(e,t){this.context=e,this.changed=t,this.rows=[],this.loading=!1,this.error=null,this.hasMore=!1,this.hasResult=!1,this.input=null,this.scope=null,this.observation=null,this.pendingListRead=null,this.generation=0,this.disposed=!1,this.nextOffset=null,this.sessions=[],this.metrics=new Map,this.historyReads=0,this.tools=new Map,this.stopping=new Set,this.messageSubscriptions=new Map,this.visibilityDocument=globalThis.document,this.visibilityChanged=()=>this.reconcile(),this.narrationSource={subscribeMessages:async(t,n)=>{let r=this.generation,i=await e.sessions.subscribeMessages(t,n);if(this.current(r)){let e=this.messageSubscriptions.get(t)??new Set;e.add(i),this.messageSubscriptions.set(t,e),this.readMetrics()}return i},unsubscribeMessages:async t=>{await e.sessions.unsubscribeMessages(t);for(let[e,n]of this.messageSubscriptions)n.delete(t),n.size===0&&this.messageSubscriptions.delete(e)}},this.narration=new k(()=>void 0,()=>void 0,e=>{this.tools=e,this.publish()}),this.unsubscribe=[e.gateway.subscribe(()=>{this.reconcile(),this.publish()}),e.gateway.subscribeEvents(e=>this.handleEvent(e))],this.visibilityDocument?.addEventListener(`visibilitychange`,this.visibilityChanged)}sync(e){let t=e.sessionKey!==this.input?.sessionKey||w(e.agentId)!==w(this.input?.agentId);(t||e.presented!==this.input?.presented)&&(this.input=e,t&&this.retire(),this.reconcile())}reconcile(){if(this.disposed)return;let e=this.input?.presented&&this.visibilityDocument?.visibilityState!==`hidden`?this.context.sessions.captureConnectionScope():null;if(!e||!this.input?.sessionKey){this.retire(),this.publish();return}if(this.scope&&!this.context.sessions.isConnectionScopeCurrent(this.scope)&&this.retire(),this.observation)return;this.scope=e;let t=this.generation;this.loading=!0,this.error=null,this.observation=this.context.sessions.observeList(this.childQuery(this.input),e=>{this.current(t)&&this.applyList(e)}),this.refresh()}childQuery(e){let t=r({assistantAgentId:e.agentId,agentsList:this.context.agents.state.agentsList,hello:this.context.gateway.snapshot.hello},e.sessionKey,e.agentId);return{...N(t.sessionKey,K),...t.sessionKey===`global`?{agentId:t.agentId}:{}}}async refresh(){let e=this.observation;e&&await this.readList(()=>(this.metrics.clear(),this.error=null,e.refresh()))}async loadMore(){this.input&&!this.loading&&this.hasMore&&this.nextOffset!==null&&await this.readList(()=>!this.input||this.loading||!this.hasMore||this.nextOffset===null?Promise.resolve():this.context.sessions.refreshList({...this.childQuery(this.input),offset:this.nextOffset,append:!0}))}async readList(e){let t=this.generation;for(;this.pendingListRead&&this.current(t);)await this.pendingListRead.catch(()=>void 0);if(!this.current(t))return;let n=Promise.resolve().then(()=>this.current(t)?e():void 0);this.pendingListRead=n;try{await n}catch(e){this.current(t)&&(this.error=d(e),this.publish())}finally{this.pendingListRead===n&&(this.pendingListRead=null)}}applyList(e){this.loading=e.loading,this.error=e.error;let t=e.result;if(e.error)this.sessions=[],this.metrics.clear(),this.hasMore=!1,this.nextOffset=null;else if(t){this.hasResult=!0,this.hasMore=t.hasMore===!0,this.nextOffset=t.nextOffset??null;let e=Date.now(),n=new Map(this.sessions.map(e=>[e.key,e]));this.sessions=t.sessions.filter(xe).map(t=>{let r=n.get(t.key),a=r&&W(r)===W(t)&&r.runtimeMs===t.runtimeMs;return Object.assign({},t,{agentId:i(t,this.input?.agentId??`main`),runtimeSampledAt:t.runtimeSampledAt??(a?r.runtimeSampledAt:void 0)??e})});let r=new Set(this.sessions.map(e=>e.key));for(let e of this.metrics.keys())r.has(e)||this.metrics.delete(e);for(let e of this.sessions){let t=W(e);this.metrics.get(e.key)?.identity!==t&&this.metrics.set(e.key,G(t))}}this.syncNarration(),this.publish(),this.readMetrics()}syncNarration(){this.narration.sync({enabled:!!(this.input?.presented&&this.scope),connected:!!this.context.sessions.captureConnectionScope(),connectionIdentity:this.context.gateway.snapshot.hello,source:this.narrationSource,rows:this.sessions.map(e=>({key:e.key,hasActiveRun:C(e),startedAt:e.startedAt,updatedAt:e.updatedAt})),openSessionKey:this.input?.sessionKey??``,agentId:this.input?.agentId??`main`})}readMetrics(){if(this.scope&&this.current(this.generation))for(let e of this.sessions){let t=this.metrics.get(e.key);if(!t||t.read||t.pending||this.historyReads>=q||C(e)&&!this.messageSubscriptions.has(e.key))continue;t.pending=!0,this.historyReads+=1;let n=this.generation,r=this.scope,i=()=>this.current(n)&&this.metrics.get(e.key)===t,a=`subagent-panel\u0000${e.key}\u0000${e.sessionId??``}`;P(null,r.client,a,`chat.history`,e.key,e.agentId,this,{isCurrent:i}).then(n=>{if(!i()||Te(n)||!e.sessionId||j(n)!==e.sessionId)return;let r=ke(n);for(let[e,n]of t.calls)if(r.calls.get(e)?.phase!==`end`||n.phase===`end`){let i=t.preparedCalls.has(e),a=H(i?`item`:`tool`,i?n:{...n,phase:n.phase===`end`?`result`:n.phase},r.calls.get(e));a&&r.calls.set(e,a)}r.calls.size>J&&(r.calls.clear(),t.preparedCalls.clear(),r.complete=!1),t.calls=r.calls,t.complete=r.complete&&!t.unidentifiedCall,t.tool=r.tool}).catch(()=>{}).finally(()=>{--this.historyReads,i()&&(t.pending=!1,t.read=!0,this.publish()),this.readMetrics()})}}handleEvent(e){if(!this.current(this.generation))return;if(e.event===`sessions.changed`){let n=t(e.payload)?.[0];if(n&&[`reset`,`rewind`,`branch-switch`,`compact`].includes(n.reason??``)){let e=this.sessions.find(e=>l(e.key,n.key));e&&(this.metrics.set(e.key,G(W(e))),this.publish(),this.readMetrics())}return}if(e.event!==`agent`&&e.event!==`session.tool`)return;let n=T(e.payload),r=n?.stream;if(r!==`tool`&&r!==`item`&&r!==`lifecycle`||r===`item`&&T(n?.data)?.kind!==`tool`||typeof n?.sessionKey!=`string`||typeof n.runId!=`string`)return;let i=n.sessionKey,a=n.runId,o=this.sessions.find(e=>l(e.key,i)&&e.activeRunIds?.includes(a)&&(typeof n.agentId!=`string`||w(n.agentId)===e.agentId));if(!o)return;if(r===`lifecycle`){this.narration.handleEvent(e);return}let s=this.metrics.get(o.key);if(!s)return;let c=!s.liveToolObserved&&s.tool&&!this.tools.has(o.key),u=O(r,n.data,s.tool);u!==void 0&&(s.liveToolObserved=!0),this.narration.handleEvent(e);let d=T(n.data)?.toolCallId,f=typeof d==`string`?I(a,d):void 0,p=H(n.stream,n.data,f?s.calls.get(f):void 0);if(!p){c&&u!==void 0&&!this.tools.has(o.key)&&this.publish();return}if(s.liveToolObserved=!0,!p.toolCallId?.trim()){s.unidentifiedCall=!0,s.complete=!1,this.publish();return}let m=I(a,p.toolCallId);s.calls.size>=J&&!s.calls.has(m)?s.complete=!1:(n.stream===`item`&&s.preparedCalls.add(m),s.calls.set(m,p)),this.publish()}publish(){this.disposed||(this.rows=this.sessions.map(e=>{let t=this.metrics.get(e.key),n=C(e)?this.tools.get(e.key)??(t?.liveToolObserved?void 0:t?.tool):void 0,r=L(this.context.gateway.snapshot,!0,{session:e,sessionAbortable:!0}).abort;return{session:e,callCount:t?Ae(t):void 0,activity:n?.text,toolDisplayName:n?V({name:n.name}).label:void 0,canStop:C(e)&&e.activeRunIds?.length===1&&r.allowed,stopping:this.stopping.has(e.key),stopAccess:r}}),this.changed())}async stop(e){let t=this.sessions.find(t=>t.key===e.session.key&&t.sessionId===e.session.sessionId),n=e.session.activeRunIds?.length===1?e.session.activeRunIds[0]:void 0,r=this.scope;if(!t||!n||!t.activeRunIds?.includes(n)||!r||!this.current(this.generation)||this.stopping.has(t.key))return;let i=L(this.context.gateway.snapshot,!0,{session:t,sessionAbortable:!0}).abort;if(!i.allowed){this.error=i.reason,this.publish();return}let a=this.generation;this.stopping.add(t.key),this.publish();let o=await Se(r.client,{sessionKey:t.key,agentId:t.agentId,runId:n,sessionAbortable:!0});this.current(a)&&(this.stopping.delete(t.key),this.error=o.ok?o.warning??null:d(o.error),this.publish(),o.ok&&this.refresh())}current(e){return!!(!this.disposed&&e===this.generation&&this.scope&&this.context.sessions.isConnectionScopeCurrent(this.scope))}retire(){this.generation+=1,this.observation?.dispose(),this.observation=null,this.pendingListRead=null,this.scope=null,this.sessions=[],this.metrics.clear(),this.tools=new Map,this.stopping.clear(),this.messageSubscriptions.clear(),this.loading=!1,this.hasMore=!1,this.hasResult=!1,this.nextOffset=null,this.error=null,this.syncNarration()}dispose(){this.disposed=!0,this.retire(),this.narration.dispose();for(let e of this.unsubscribe)e();this.visibilityDocument?.removeEventListener(`visibilitychange`,this.visibilityChanged)}}})))()}var Z,Q;function $(){return($=e((()=>{te(),ne(),re(),m(),n(),me(),c(),s(),R(),X(),Oe(),D(),Z=0,Q=class extends De{constructor(){super(),this.paneId=`single`,this.presentationId=`single`,this.inputRegion=`page`,this.selected=null,this.finishedOpen=!0,this.dataType=Y,this.backToSubagents=()=>{this.selected=null},this.navigateSession=(e,t,n)=>this.onSessionSelect?.(t,n),Z+=1,this.finishedId=`chat-subagents-finished-${Z}`}clearSelection(){this.selected=null}connectedCallback(){super.connectedCallback(),this.requestUpdate()}willUpdate(e){super.willUpdate(e);let t=e.has(`showRequest`)?this.showRequest?.():void 0;if(t!==void 0&&(this.selected=t?{key:t,agentId:this.subagentAgentId(t)}:null),this.selected&&this.data&&!this.data.loading){let e=this.data.error&&this.data.rows.length===0,t=this.data.hasResult&&!this.data.error&&!this.data.hasMore&&!this.data.rows.some(e=>e.session.key===this.selected?.key);(e||t)&&(this.selected=null)}}subagentAgentId(e){return this.data?.rows.find(t=>t.session.key===e)?.session?.agentId??f(e)?.agentId??this.agentId}select(e){this.selected={key:e.session.key,agentId:this.subagentAgentId(e.session.key)}}renderElapsed(e){let{session:t}=e,n=C(t);return t.runtimeMs==null?t.startedAt==null||!n&&t.endedAt==null?h:g`<openclaw-elapsed-time
.startMs=${t.startedAt}
.endMs=${n?null:t.endedAt}
></openclaw-elapsed-time>`:n&&t.runtimeSampledAt!=null?g`<openclaw-elapsed-time
.startMs=${t.runtimeSampledAt-t.runtimeMs}
></openclaw-elapsed-time>`:ue(t.runtimeMs)}renderRow(e){let{session:t}=e,n=C(t),r=a(t.key,t),i=t.status===`queued`?p(`common.queued`):n?e.activity||e.toolDisplayName:void 0,o=e.stopping?p(`chat.subagentsPanel.stopping`):p(`chat.subagentsPanel.stop`,{name:r});return g`<div class="chat-subagents__item" role="listitem" data-session-key=${t.key}>
<div class="chat-subagents__item-heading">
<button
class="chat-subagents__open"
type="button"
title=${r}
=${()=>this.select(e)}
>
${r}
</button>
${n&&t.activeRunIds?.length===1?g`<button
class="chat-subagents__stop"
type="button"
aria-label=${o}
title=${e.stopAccess.allowed?o:e.stopAccess.reason}
?disabled=${e.stopping||!e.canStop}
=${()=>void this.data?.stop(e)}
>
${ee.square}
</button>`:h}
</div>
<div class="chat-subagents__metadata">
<span class="chat-subagents__work">
${e.callCount===void 0?h:g`<span class="chat-subagents__calls"
>${p(e.callCount===1?`chat.subagentsPanel.callsOne`:`chat.subagentsPanel.callsMany`,{count:String(e.callCount)})}</span
>`}
${e.callCount!==void 0&&i?g`<span aria-hidden="true">·</span>`:h}
${i?g`<span class="chat-subagents__activity" title=${i}>${i}</span>`:h}
</span>
<span
class="chat-subagents__elapsed"
title=${p(n?`chat.subagentsPanel.elapsed`:`chat.subagentsPanel.duration`)}
>${this.renderElapsed(e)}</span
>
</div>
</div>`}render(){if(this.selected){let{key:e,agentId:t}=this.selected,n=`${this.presentationId}:subagent:${e}`;return v(n,g`<openclaw-chat-pane
class="chat-subagents__detail-pane"
.paneId=${this.paneId}
.presentationId=${n}
.sessionKey=${e}
.agentId=${t}
.inputRegion=${this.inputRegion}
.compact=${!0}
.active=${!1}
.presented=${this.presented}
.onBackToSubagents=${this.backToSubagents}
.onPaneSessionChange=${this.navigateSession}
></openclaw-chat-pane>`)}let e=this.data?.rows??[],t=e=>e.session.status===`queued`||F(e.session),n=e.filter(t),r=e.filter(e=>!t(e)),i=this.data?.loading??!1,a=this.data?.hasResult&&!this.data.hasMore&&!this.data.error;return g`<div class="chat-subagents__list" aria-busy=${i}>
${this.data?.error?g`<div class="chat-subagents__error" role="alert">
<span>${this.data.error}</span>
<button class="btn btn--sm" type="button" =${()=>this.refresh()}>
${p(`common.retry`)}
</button>
</div>`:h}
${e.length?this.renderGroups(`subagents`,n,r,e=>e.session.key,e=>this.renderRow(e),this.finishedId):i||a?g`<div class="chat-subagents__empty" role="status">
${p(i?`common.loading`:`chat.subagentsPanel.empty`)}
</div>`:h}
${this.data?.hasMore?g`<button
class="chat-subagents__load-more btn btn--sm"
type="button"
?disabled=${i}
=${()=>this.data?.loadMore()}
>
${p(i?`common.loading`:`chat.subagentsPanel.loadMore`)}
</button>`:h}
</div>`}},S([_({attribute:!1})],Q.prototype,`paneId`,void 0),S([_({attribute:!1})],Q.prototype,`presentationId`,void 0),S([_({attribute:!1})],Q.prototype,`inputRegion`,void 0),S([_({attribute:!1})],Q.prototype,`showRequest`,void 0),S([_({attribute:!1})],Q.prototype,`onSessionSelect`,void 0),S([y()],Q.prototype,`selected`,void 0),S([y()],Q.prototype,`finishedOpen`,void 0),customElements.get(`openclaw-chat-subagents-panel`)||customElements.define(`openclaw-chat-subagents-panel`,Q)})))()}$();