eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 3.66 kB
JavaScript
import{createTraceStore}from"./trace-store.js";import{deriveTraceViewerSurfaces}from"./trace-surfaces.js";import{conversationItemLineCount}from"./trace-conversation.js";import{applyLoadedTrace,applyTraceList,createTraceViewerState,reduceTraceViewerKey}from"./trace-viewer-state.js";import{conversationSelectionText,panelSelectionText,renderTraceViewer}from"./trace-view.js";var TraceViewerSession=class{#e;#t;#n=createTraceViewerState();#r;#i;#a;#o;#s;#c=!1;#l=!1;#u=!1;#d;#f={timelineViewportRows:0,panelViewportRows:0,panelTotalRows:0,contentWidth:0};constructor(e){this.#e=e,this.#t=e.store??createTraceStore({appRoot:e.appRoot}),e.terminalBackground!==void 0&&e.theme.color&&(this.#d=deriveTraceViewerSurfaces(e.terminalBackground)),e.sessionId!==void 0&&(this.#i=e.sessionId),e.reference!==void 0&&(this.#i=void 0,this.#n={...this.#n,notice:`No local trace matches "${e.reference}" yet.`})}start(){this.repaint(),this.#m(),this.#a=setInterval(()=>void this.#m(),1e3),this.#a.unref?.()}handleKey(e){if(this.#c)return`close`;e.type===`text`&&(e.value===`[`||e.value===`]`)&&(this.#r=void 0,this.#i=void 0);let t=this.#n.trace!==void 0,{state:n,effect:r,copySelection:i}=reduceTraceViewerKey(this.#n,e,this.#f);if(this.#n=n,r===`close`)return`close`;i!==void 0&&this.#p(i),this.repaint(),t&&n.trace===void 0&&this.#m()}setTerminalBackground(e){this.#c||!this.#e.theme.color||(this.#d=deriveTraceViewerSurfaces(e),this.repaint())}repaint(){if(this.#c)return;let{width:e,height:t}=this.#e.dimensions(),n=renderTraceViewer(this.#n,{width:e,height:t,theme:this.#e.theme,tracingDisabled:this.#e.tracingDisabled,activeWindowEndNs:this.#_(),toast:this.#o,surfaces:this.#d});this.#f={timelineViewportRows:n.timelineViewportRows,panelViewportRows:n.panelViewportRows,panelTotalRows:n.panelTotalRows,contentWidth:n.contentWidth,conversationLineCounts:this.#n.conversationItems.map((e,t)=>conversationItemLineCount(e,n.contentWidth,this.#e.theme,this.#n.expandedItems.has(t)))},this.#e.paint(n.rows)}dispose(){this.#c=!0,this.#a!==void 0&&(clearInterval(this.#a),this.#a=void 0),this.#s!==void 0&&(clearTimeout(this.#s),this.#s=void 0)}#p(e){let t=e.region===`panel`?panelSelectionText(this.#n,this.#e.dimensions().width,this.#e.theme,e):conversationSelectionText(this.#n,this.#f.contentWidth,this.#e.theme,e,this.#d);t.trim().length!==0&&(this.#e.copyText?.(t),this.#o=`Copied to clipboard`,this.#s!==void 0&&clearTimeout(this.#s),this.#s=setTimeout(()=>{this.#s=void 0,this.#o=void 0,this.repaint()},2e3),this.#s.unref?.())}async#m(){if(!this.#c){if(this.#l){this.#u=!0;return}this.#l=!0;try{do this.#u=!1,await this.#h();while(this.#u&&!this.#c)}finally{this.#l=!1}}}async#h(){try{let e=await this.#t.list(),t=this.#r,n=this.#e.reference;if(t===void 0&&n!==void 0&&(t=e.find(e=>e.traceId.startsWith(n))?.traceId),t===void 0&&this.#i!==void 0&&(t=await this.#g(e,this.#i)),this.#c)return;t!==void 0&&(this.#r=t);let r=e[applyTraceList(this.#n,e,{preferTraceId:t}).traceIndex]?.traceId,i=r===void 0?void 0:await this.#t.read(r);if(this.#c)return;let a=applyTraceList(this.#n,e,{preferTraceId:this.#r});this.#r!==void 0&&e[a.traceIndex]?.traceId===this.#r&&(this.#r=void 0,this.#i=void 0,a.notice?.startsWith(`No local trace matches`)&&(a={...a,notice:void 0})),e[a.traceIndex]?.traceId===r&&(a=applyLoadedTrace(a,i)),this.#n=a,this.repaint()}catch{}}async#g(e,t){for(let n of e){let e=await this.#t.read(n.traceId);if(this.#c)return;if(e?.conversationIds.includes(t))return n.traceId}}#_(){let e=this.#n.traces[this.#n.traceIndex];if(e!==void 0&&this.#n.trace!==void 0&&!(Date.now()-e.lastActivityMs>3e3))return BigInt(Date.now())*1000000n}};export{TraceViewerSession};