@north-light/crouter 0.3.329 → 0.3.330
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
var q=Object.defineProperty;var p=(m,e)=>q(m,"name",{value:e,configurable:!0});import{execFile as N}from"node:child_process";import{matchesKey as O,truncateToWidth as C}from"@earendil-works/pi-tui";import{formatBinding as P,matchesPiTuiInput as H}from"../../../core/keybindings/index.js";import{fullName as B}from"../../../core/canvas/labels.js";import{faultSummary as L}from"../../../core/canvas/status-glyph.js";import{RemoteCanvasSource as G}from"../../../core/canvas/remote-canvas-source.js";import{cliClient as F}from"../../../commands/api-client.js";import{statusRank as S}from"../../../core/canvas/node-order.js";import{navLabel as K,nodeGlyph as W,tokensCell as j,cycleBadge as Y,askBadge as D,activityCell as V,shortId as M,visibleWidth as E,fillBar as k,fillWidth as A,truncate as z,isAttached as U,VIEWPORT_FALLBACK_ROWS as X,DIM as v,RESET as g,BOLD as T,YELLOW as J,REVERSE as Q,BG_ATTACHED as Z,GREEN as tt}from"../../../core/canvas/nav-render.js";const et=.72,st={anchor:"center",width:"72%",minWidth:48,maxHeight:"72%",margin:1},it=[["crtr.graph.dismiss","dismiss"],["crtr.graph.focus","focus"],["crtr.graph.revive","revive"],["crtr.graph.close-subtree","close"],["crtr.graph.focus-manager","manager"]],nt=[["crtr.graph.dismiss","dismiss"]],rt=[["crtr.graph.down","down"],["crtr.graph.up","up"],["crtr.graph.first","first"],["crtr.graph.last","last"],["crtr.graph.collapse-or-manager","fold/manager"],["crtr.graph.expand-or-child","expand/child"]];function ot(m){return{node_id:m.node_id,name:m.name,kind:m.kind,description:m.description??void 0,cycles:m.cycles??void 0,status:m.status,frozen_at:m.frozen_at,pi_pid:m.pi_pid,telemetry_context_tokens:m.telemetry_context_tokens,telemetry_last_activity:m.telemetry_last_activity,fault:m.fault??null,streaming:m.streaming}}p(ot,"graphRow");class $t{static{p(this,"GraphOverlay")}tui;self;getAsks;palette;bindings;source;role;onRefreshFailure;onNotice;handle;userExpanded=new Set;userCollapsed=new Set;folds={userExpanded:this.userExpanded,userCollapsed:this.userCollapsed};cursorId;scrollTop=0;pendingConfirm;snapshot;snapshotSeq=0;remote;constructor(e,s,i,t,n,c,d="controller",l=()=>{},r=()=>{}){this.tui=e,this.self=s,this.getAsks=i,this.palette=t,this.bindings=n,this.source=c,this.role=d,this.onRefreshFailure=l,this.onNotice=r,this.remote=c instanceof G}setBindings(e){this.bindings=e,this.handle!==void 0&&this.tui.requestRender()}matches(e,s){return H(this.bindings,e,s,(i,t)=>O(i,t))}hint(e){return e.flatMap(([s,i])=>this.bindings.gestures(s).length===0?[]:[`${P(this.bindings,s)} ${i}`])}fitHints(e,s){const t=[];let n=0;for(const c of this.hint(e)){const d=(t.length===0?0:3)+E(c);if(n+d>s)break;t.push(c),n+=d}return t.join(" \xB7 ")}isOpen(){return this.handle!==void 0}open(){this.handle===void 0&&(this.scrollTop=0,this.pendingConfirm=void 0,this.cursorId=this.self,this.handle=this.tui.showOverlay(this,st),this.refresh())}close(){this.handle!==void 0&&(this.pendingConfirm=void 0,this.handle.hide(),this.handle=void 0,this.tui.requestRender())}toggle(){this.handle===void 0?this.open():this.close()}refresh(){if(this.handle===void 0)return;const e=++this.snapshotSeq;this.buildSnapshot().then(s=>{e===this.snapshotSeq&&(this.snapshot=s,this.tui.requestRender())}).catch(s=>{this.onRefreshFailure(s),this.tui.requestRender()})}invalidate(){this.refresh()}async fetchInput(){if(this.remote){const t=await this.source.listNodes().catch(()=>[]);return{nodes:new Map(t.map(n=>[n.node_id,n])),subscriptions:p(async n=>(await this.source.subscriptionsOf(n)).map(c=>c.node_id),"subscriptions"),subscribers:p(async n=>(await this.source.subscribersOf(n)).map(c=>c.node_id),"subscribers"),focused:new Set}}const e=await F().canvasGraph(),s=new Map,i=new Map;for(const t of e.edges){const n=s.get(t.from_id);n===void 0?s.set(t.from_id,[t.to_id]):n.push(t.to_id);const c=i.get(t.to_id);c===void 0?i.set(t.to_id,[t.from_id]):c.push(t.from_id)}return{nodes:new Map(e.nodes.map(t=>[t.node_id,ot(t)])),subscriptions:p(t=>Promise.resolve(s.get(t)??[]),"subscriptions"),subscribers:p(t=>Promise.resolve(i.get(t)??[]),"subscribers"),focused:new Set(e.focused_node_ids)}}async buildSnapshot(){const e=await this.fetchInput(),{nodes:s,focused:i}=e,t=new Map,n=new Map,c=new Map,d=new Map,l=p(async o=>{const a=t.get(o);if(a!==void 0)return a;let f;try{f=await e.subscriptions(o)}catch{f=[]}const b=f.filter(w=>s.get(w)?.kind!=="human");return t.set(o,b),b},"children"),r=p(async o=>{if(c.has(o))return c.get(o);let a;try{a=(await e.subscribers(o))[0]}catch{a=void 0}return c.set(o,a),a},"manager"),h=p(async()=>{let o=this.self;const a=new Set([o]);for(;;){const f=await r(o);if(f===void 0||a.has(f))break;a.add(f),o=f}return o},"climbRoot"),u=p(async o=>{const a=n.get(o);if(a!==void 0)return a;const f=[...await l(o)];return f.sort((b,w)=>S(s.get(b)?.status)-S(s.get(w)?.status)),n.set(o,f),f},"sortedChildren"),R=new Set,$=new Set,y=p(async o=>{const a=s.get(o)?.status==="active";if($.has(o))return{active:a?1:0,reveal:a||o===this.self};$.add(o);let f=0,b=!1;for(const w of await l(o)){const x=await y(w);f+=x.active,x.reveal&&(b=!0)}return d.set(o,f),b&&R.add(o),{active:f+(a?1:0),reveal:b||a||o===this.self}},"visitActivity"),I=await h();await y(I),await Promise.all([...t.keys()].map(o=>u(o)));const _={root:I,rows:[],autoExpanded:R,activeBelow:d,nodes:s,childIds:t,sortedChildIds:n,managerOf:c,focused:i};return _.rows=this.projectRows(_),_}projectRows(e){const s=[],i=new Set,t=[{id:e.root,prefix:"",isRoot:!0,isLast:!0}];for(;t.length>0;){const{id:n,prefix:c,isRoot:d,isLast:l}=t.pop(),r=d?"":l?"\u2514\u2500 ":"\u251C\u2500 ";if(i.has(n)){s.push({id:n,hasKids:!1,isSelf:n===this.self,branch:c+r,cycle:!0,collapsed:!1});continue}i.add(n);const h=e.sortedChildIds.get(n)??[],u=this.folds.userExpanded.has(n)?!1:this.folds.userCollapsed.has(n)?!0:!e.autoExpanded.has(n);if(s.push({id:n,hasKids:h.length>0,isSelf:n===this.self,branch:c+r,cycle:!1,collapsed:u}),u)continue;const R=d?"":c+(l?" ":"\u2502 ");for(let $=h.length-1;$>=0;$--)t.push({id:h[$],prefix:R,isRoot:!1,isLast:$===h.length-1})}return s}repaintFolds(){this.snapshot!==void 0&&(this.snapshot.rows=this.projectRows(this.snapshot)),this.tui.requestRender()}renderSnapshotRow(e,s,i,t){const n=p((w,x)=>i?k(w,A(),Q):x?k(w,A(),Z):z(w),"wrap");if(s.cycle){const w=`${s.branch} ${v}\u21BA ${M(s.id)}${g}`;return n(w,!1)}const c=e.nodes.get(s.id)??null,d=W(c),l=K(c,s.id),r=s.isSelf?`${T}${l}${g}`:l,h=`${v}${c?.kind??""}${g}`,u=`${v}${j(c,this.remote)}${g}`,R=s.hasKids&&s.collapsed,$=!i&&R?`${v}\u25B8${g} `:" ",y=Y(c),I=e.childIds.get(s.id)?.length??0,_=c!==null&&I>0?` ${v}\u2933${I}${g}`:"",o=c!==null&&c.status!=="active"&&(e.activeBelow.get(c.node_id)??0)>0?` ${tt}\u21E3${e.activeBelow.get(c.node_id)??0}${g}`:"",a=c?.fault??null,f=a!==null?` \xB7 ${L(a)}`:"",b=`${s.branch}${$}${d} ${r} ${h} ${u}${y}${_}${o}${D(s.id,t)}${V(c,this.remote)}${f}`;return n(b,U(s.id,c,e.focused,this.remote))}render(e){const s=this.getAsks(),i=this.snapshot?.rows??[];let t=i.findIndex(o=>o.id===this.cursorId);t<0&&(t=i.findIndex(o=>o.id===this.self),t<0&&(t=0)),this.cursorId=i[t]?.id??this.self;const n=process.stdout.rows??X,c=Math.max(3,Math.min(Math.floor(n*et),n-2)),d=Math.max(2,Math.min(i.length,c-2));let l=d;for(let o=0;o<4;o++){t<this.scrollTop&&(this.scrollTop=t),t>=this.scrollTop+l&&(this.scrollTop=t-l+1),this.scrollTop=Math.max(0,Math.min(this.scrollTop,Math.max(0,i.length-l)));const a=d-(this.scrollTop>0?1:0)-(this.scrollTop+l<i.length?1:0);if(a===l)break;l=Math.max(1,a)}const r=Math.min(i.length,this.scrollTop+l),h=[];this.scrollTop>0&&h.push(`${v} \u2191 ${this.scrollTop} more${g}`);for(let o=this.scrollTop;o<r;o++){const a=this.snapshot;h.push(a!==void 0?this.renderSnapshotRow(a,i[o],o===t,s):"")}for(r<i.length&&h.push(`${v} \u2193 ${i.length-r} more${g}`);h.length<d;)h.push("");const u=i.length>0?`${T}\u2317 canvas graph${g} ${v}(${t+1}/${i.length})${g}`:`${T}\u2317 canvas graph${g} ${v}(loading)${g}`,R=this.pendingConfirm!==void 0?`${J}${this.pendingConfirm.label} ${T}${this.hint([["crtr.graph.confirm.accept","accept"],["crtr.graph.confirm.cancel","cancel"]]).join(" \xB7 ")}${g}`:`${v}${this.fitHints([...this.remote||this.role!=="controller"?nt:it,...rt],Math.max(1,e-5))}${g}`,$=this.palette.border,y=Math.max(1,e-4),I=h.map(o=>`${$("\u2502")} ${C(o,y,"",!0)} ${$("\u2502")}`),_=this.palette.surface;return[this.borderRow("\u256D","\u256E",u,e),...I,this.borderRow("\u2570","\u256F",R,e)].map(_)}borderRow(e,s,i,t){const n=this.palette.border,c=Math.max(1,t-5),d=C(i,c,"\u2026"),l=Math.max(0,t-5-E(d));return`${n(`${e}\u2500`)} ${d} ${n("\u2500".repeat(l)+s)}`}handleInput(e){try{this.dispatch(e)}catch{}}dispatch(e){if(this.pendingConfirm!==void 0){if(this.matches("crtr.graph.confirm.accept",e)){const r=this.pendingConfirm.action;this.pendingConfirm=void 0,r()}else this.pendingConfirm=void 0;this.tui.requestRender();return}if(this.matches("crtr.graph.dismiss",e)){this.close();return}const s=this.snapshot,i=s?.rows??[];let t=i.findIndex(r=>r.id===this.cursorId);t<0&&(t=Math.max(0,i.findIndex(r=>r.id===this.self)));const n=i[t],c=p(r=>r!==void 0&&!r.cycle,"isSelectable"),d=p((r,h)=>{for(let u=r+h;u>=0&&u<i.length;u+=h)if(c(i[u]))return u;return r},"step"),l=p(r=>{const h=r===1?0:i.length-1;for(let u=h;u>=0&&u<i.length;u+=r)if(c(i[u]))return u;return t},"edge");if(this.matches("crtr.graph.down",e)){t=d(t,1),this.cursorId=i[t]?.id??this.cursorId,this.tui.requestRender();return}if(this.matches("crtr.graph.up",e)){t=d(t,-1),this.cursorId=i[t]?.id??this.cursorId,this.tui.requestRender();return}if(this.matches("crtr.graph.first",e)){this.cursorId=i[l(1)]?.id??this.cursorId,this.tui.requestRender();return}if(this.matches("crtr.graph.last",e)){this.cursorId=i[l(-1)]?.id??this.cursorId,this.tui.requestRender();return}if(this.matches("crtr.graph.collapse-or-manager",e)){if(n!==void 0&&n.hasKids&&!n.collapsed)this.userCollapsed.add(n.id),this.userExpanded.delete(n.id),this.repaintFolds();else{const r=s?.managerOf.get(this.cursorId??this.self);r!==void 0&&i.some(h=>h.id===r)&&(this.cursorId=r),this.tui.requestRender()}return}if(this.matches("crtr.graph.expand-or-child",e)){if(n!==void 0&&n.collapsed&&n.hasKids)this.userExpanded.add(n.id),this.userCollapsed.delete(n.id),this.repaintFolds();else if(n!==void 0&&n.hasKids){const r=s?.sortedChildIds.get(n.id)?.[0];r!==void 0&&(this.cursorId=r),this.tui.requestRender()}return}if(this.matches("crtr.graph.focus",e)){if(this.remote)return;if(this.role!=="controller"){this.onNotice("Read-only observer attach \u2014 cannot focus nodes");return}this.cursorId!==void 0&&this.focusTarget(this.cursorId),this.close();return}if(this.matches("crtr.graph.revive",e)){if(this.remote)return;if(this.role!=="controller"){this.onNotice("Read-only observer attach \u2014 cannot revive nodes");return}const r=this.cursorId,h=r===void 0?null:s?.nodes.get(r)??null;r!==void 0&&h!==null&&h.status!=="active"&&h.status!=="idle"&&(this.reviveTarget(r),this.close());return}if(this.matches("crtr.graph.focus-manager",e)){if(this.remote)return;if(this.role!=="controller"){this.onNotice("Read-only observer attach \u2014 cannot focus nodes");return}const r=s?.managerOf.get(this.self);r!==void 0&&(this.focusTarget(r),this.close());return}if(this.matches("crtr.graph.close-subtree",e)){if(this.remote)return;if(this.role!=="controller"){this.onNotice("Read-only observer attach \u2014 cannot close nodes");return}const r=this.cursorId??this.self,h=s?.nodes.get(r)??null,u=h!==null?B(h):M(r);this.pendingConfirm={label:`kill ${u}?`,action:p(()=>this.shellCrtr(["node","lifecycle","close","--node",r]),"action")},this.tui.requestRender();return}}focusTarget(e){const s=["surface","node","focus",e],i=process.env.TMUX_PANE;i!==void 0&&i!==""&&s.push("--pane",i),this.shellCrtr(s)}reviveTarget(e){this.shellCrtr(["node","lifecycle","revive",e],()=>this.focusTarget(e))}shellCrtr(e,s){try{N("crtr",e,t=>{t==null&&s?.()}).stdin?.end()}catch{}}}export{$t as GraphOverlay};
|
|
1
|
+
var O=Object.defineProperty;var m=(u,t)=>O(u,"name",{value:t,configurable:!0});import{execFile as q}from"node:child_process";import{matchesKey as P,truncateToWidth as T}from"@earendil-works/pi-tui";import{formatBinding as H,matchesPiTuiInput as L}from"../../../core/keybindings/index.js";import{fullName as B}from"../../../core/canvas/labels.js";import{faultSummary as G,resolveNodeVisual as C}from"../../../core/canvas/status-glyph.js";import{RemoteCanvasSource as W}from"../../../core/canvas/remote-canvas-source.js";import{cliClient as F}from"../../../commands/api-client.js";import{statusRank as M}from"../../../core/canvas/node-order.js";import{navLabel as K,nodeGlyph as j,tokensCell as D,cycleBadge as V,askBadge as Y,activityCell as z,shortId as E,visibleWidth as k,fillBar as A,fillWidth as N,truncate as U,isAttached as X,VIEWPORT_FALLBACK_ROWS as J,ESC as Q,DIM as R,RESET as g,BOLD as S,YELLOW as Z,REVERSE as ee,BG_ATTACHED as te,GREEN as se}from"../../../core/canvas/nav-render.js";const ie=.72,ne={anchor:"center",width:"72%",minWidth:48,maxHeight:"72%",margin:1},re=[["crtr.graph.dismiss","dismiss"],["crtr.graph.focus","focus"],["crtr.graph.revive","revive"],["crtr.graph.close-subtree","close"],["crtr.graph.focus-manager","manager"]],oe=[["crtr.graph.dismiss","dismiss"]],ce=[["crtr.graph.down","down"],["crtr.graph.up","up"],["crtr.graph.first","first"],["crtr.graph.last","last"],["crtr.graph.collapse-or-manager","fold/manager"],["crtr.graph.expand-or-child","expand/child"]],ae=[...["active","idle","done","dead","canceled"].map(u=>[C(u,{}),u]),[C("active",{streaming:!0}),"live"],[C("active",{frozen:!0}),"frozen"],[C("active",{hanging:{kind:"wedged"}}),"fault"]].map(([u,t])=>`${Q}${u.color}m${u.glyph}${g} ${R}${t}${g}`).join(" ");function he(u){return{node_id:u.node_id,name:u.name,kind:u.kind,description:u.description??void 0,cycles:u.cycles??void 0,status:u.status,frozen_at:u.frozen_at,pi_pid:u.pi_pid,telemetry_context_tokens:u.telemetry_context_tokens,telemetry_last_activity:u.telemetry_last_activity,fault:u.fault??null,streaming:u.streaming}}m(he,"graphRow");class be{static{m(this,"GraphOverlay")}tui;self;getAsks;palette;bindings;source;role;onRefreshFailure;onNotice;handle;userExpanded=new Set;userCollapsed=new Set;folds={userExpanded:this.userExpanded,userCollapsed:this.userCollapsed};cursorId;scrollTop=0;pendingConfirm;snapshot;snapshotSeq=0;remote;constructor(t,s,i,e,n,o,f="controller",p=()=>{},r=()=>{}){this.tui=t,this.self=s,this.getAsks=i,this.palette=e,this.bindings=n,this.source=o,this.role=f,this.onRefreshFailure=p,this.onNotice=r,this.remote=o instanceof W}setBindings(t){this.bindings=t,this.handle!==void 0&&this.tui.requestRender()}matches(t,s){return L(this.bindings,t,s,(i,e)=>P(i,e))}hint(t){return t.flatMap(([s,i])=>this.bindings.gestures(s).length===0?[]:[`${H(this.bindings,s)} ${i}`])}fitHints(t,s){const e=[];let n=0;for(const o of this.hint(t)){const f=(e.length===0?0:3)+k(o);if(n+f>s)break;e.push(o),n+=f}return e.join(" \xB7 ")}isOpen(){return this.handle!==void 0}open(){this.handle===void 0&&(this.scrollTop=0,this.pendingConfirm=void 0,this.cursorId=this.self,this.handle=this.tui.showOverlay(this,ne),this.refresh())}close(){this.handle!==void 0&&(this.pendingConfirm=void 0,this.handle.hide(),this.handle=void 0,this.tui.requestRender())}toggle(){this.handle===void 0?this.open():this.close()}refresh(){if(this.handle===void 0)return;const t=++this.snapshotSeq;this.buildSnapshot().then(s=>{t===this.snapshotSeq&&(this.snapshot=s,this.tui.requestRender())}).catch(s=>{this.onRefreshFailure(s),this.tui.requestRender()})}invalidate(){this.refresh()}async fetchInput(){if(this.remote){const e=await this.source.listNodes().catch(()=>[]);return{nodes:new Map(e.map(n=>[n.node_id,n])),subscriptions:m(async n=>(await this.source.subscriptionsOf(n)).map(o=>o.node_id),"subscriptions"),subscribers:m(async n=>(await this.source.subscribersOf(n)).map(o=>o.node_id),"subscribers"),focused:new Set}}const t=await F().canvasGraph(),s=new Map,i=new Map;for(const e of t.edges){const n=s.get(e.from_id);n===void 0?s.set(e.from_id,[e.to_id]):n.push(e.to_id);const o=i.get(e.to_id);o===void 0?i.set(e.to_id,[e.from_id]):o.push(e.from_id)}return{nodes:new Map(t.nodes.map(e=>[e.node_id,he(e)])),subscriptions:m(e=>Promise.resolve(s.get(e)??[]),"subscriptions"),subscribers:m(e=>Promise.resolve(i.get(e)??[]),"subscribers"),focused:new Set(t.focused_node_ids)}}async buildSnapshot(){const t=await this.fetchInput(),{nodes:s,focused:i}=t,e=new Map,n=new Map,o=new Map,f=new Map,p=m(async c=>{const l=e.get(c);if(l!==void 0)return l;let a;try{a=await t.subscriptions(c)}catch{a=[]}const $=a.filter(b=>s.get(b)?.kind!=="human");return e.set(c,$),$},"children"),r=m(async c=>{if(o.has(c))return o.get(c);let l;try{l=(await t.subscribers(c))[0]}catch{l=void 0}return o.set(c,l),l},"manager"),h=m(async()=>{let c=this.self;const l=new Set([c]);for(;;){const a=await r(c);if(a===void 0||l.has(a))break;l.add(a),c=a}return c},"climbRoot"),d=m(async c=>{const l=n.get(c);if(l!==void 0)return l;const a=[...await p(c)];return a.sort(($,b)=>M(s.get($)?.status)-M(s.get(b)?.status)),n.set(c,a),a},"sortedChildren"),w=new Set,v=new Set,I=m(async c=>{const l=s.get(c)?.status==="active";if(v.has(c))return{active:l?1:0,reveal:l||c===this.self};v.add(c);let a=0,$=!1;for(const b of await p(c)){const x=await I(b);a+=x.active,x.reveal&&($=!0)}return f.set(c,a),$&&w.add(c),{active:a+(l?1:0),reveal:$||l||c===this.self}},"visitActivity"),_=await h();await I(_),await Promise.all([...e.keys()].map(c=>d(c)));const y={root:_,rows:[],autoExpanded:w,activeBelow:f,nodes:s,childIds:e,sortedChildIds:n,managerOf:o,focused:i};return y.rows=this.projectRows(y),y}projectRows(t){const s=[],i=new Set,e=[{id:t.root,prefix:"",isRoot:!0,isLast:!0}];for(;e.length>0;){const{id:n,prefix:o,isRoot:f,isLast:p}=e.pop(),r=f?"":p?"\u2514\u2500 ":"\u251C\u2500 ";if(i.has(n)){s.push({id:n,hasKids:!1,isSelf:n===this.self,branch:o+r,cycle:!0,collapsed:!1});continue}i.add(n);const h=t.sortedChildIds.get(n)??[],d=this.folds.userExpanded.has(n)?!1:this.folds.userCollapsed.has(n)?!0:!t.autoExpanded.has(n);if(s.push({id:n,hasKids:h.length>0,isSelf:n===this.self,branch:o+r,cycle:!1,collapsed:d}),d)continue;const w=f?"":o+(p?" ":"\u2502 ");for(let v=h.length-1;v>=0;v--)e.push({id:h[v],prefix:w,isRoot:!1,isLast:v===h.length-1})}return s}repaintFolds(){this.snapshot!==void 0&&(this.snapshot.rows=this.projectRows(this.snapshot)),this.tui.requestRender()}renderSnapshotRow(t,s,i,e){const n=m((b,x)=>i?A(b,N(),ee):x?A(b,N(),te):U(b),"wrap");if(s.cycle){const b=`${s.branch} ${R}\u21BA ${E(s.id)}${g}`;return n(b,!1)}const o=t.nodes.get(s.id)??null,f=j(o),p=K(o,s.id),r=s.isSelf?`${S}${p}${g}`:p,h=`${R}${o?.kind??""}${g}`,d=`${R}${D(o,this.remote)}${g}`,w=s.hasKids&&s.collapsed,v=!i&&w?`${R}\u25B8${g} `:" ",I=V(o),_=t.childIds.get(s.id)?.length??0,y=o!==null&&_>0?` ${R}\u2933${_}${g}`:"",c=o!==null&&o.status!=="active"&&(t.activeBelow.get(o.node_id)??0)>0?` ${se}\u21E3${t.activeBelow.get(o.node_id)??0}${g}`:"",l=o?.fault??null,a=l!==null?` \xB7 ${G(l)}`:"",$=`${s.branch}${v}${f} ${r} ${h} ${d}${I}${y}${c}${Y(s.id,e)}${z(o,this.remote)}${a}`;return n($,X(s.id,o,t.focused,this.remote))}render(t){const s=this.getAsks(),i=this.snapshot?.rows??[];let e=i.findIndex(a=>a.id===this.cursorId);e<0&&(e=i.findIndex(a=>a.id===this.self),e<0&&(e=0)),this.cursorId=i[e]?.id??this.self;const n=process.stdout.rows??J,o=Math.max(3,Math.min(Math.floor(n*ie),n-2)),f=Math.min(o-2,Math.max(2,i.length)),p=f-1,r=p>=3;let h=p;for(let a=0;p>0&&a<4;a++){e<this.scrollTop&&(this.scrollTop=e),e>=this.scrollTop+h&&(this.scrollTop=e-h+1),this.scrollTop=Math.max(0,Math.min(this.scrollTop,Math.max(0,i.length-h)));const $=p-(r&&this.scrollTop>0?1:0)-(r&&this.scrollTop+h<i.length?1:0);if($===h)break;h=Math.max(1,$)}const d=Math.min(i.length,this.scrollTop+h),w=[];r&&this.scrollTop>0&&w.push(`${R} \u2191 ${this.scrollTop} more${g}`);for(let a=this.scrollTop;a<d;a++){const $=this.snapshot;w.push($!==void 0?this.renderSnapshotRow($,i[a],a===e,s):"")}for(r&&d<i.length&&w.push(`${R} \u2193 ${i.length-d} more${g}`),w.unshift(ae);w.length<f;)w.push("");const v=i.length>0?`${S}\u2317 canvas graph${g} ${R}(${e+1}/${i.length})${g}`:`${S}\u2317 canvas graph${g} ${R}(loading)${g}`,I=this.pendingConfirm!==void 0?`${Z}${this.pendingConfirm.label} ${S}${this.hint([["crtr.graph.confirm.accept","accept"],["crtr.graph.confirm.cancel","cancel"]]).join(" \xB7 ")}${g}`:`${R}${this.fitHints([...this.remote||this.role!=="controller"?oe:re,...ce],Math.max(1,t-5))}${g}`,_=this.palette.border,y=Math.max(1,t-4),c=w.map(a=>`${_("\u2502")} ${T(a,y,"",!0)} ${_("\u2502")}`),l=this.palette.surface;return[this.borderRow("\u256D","\u256E",v,t),...c,this.borderRow("\u2570","\u256F",I,t)].map(l)}borderRow(t,s,i,e){const n=this.palette.border,o=Math.max(1,e-5),f=T(i,o,"\u2026"),p=Math.max(0,e-5-k(f));return`${n(`${t}\u2500`)} ${f} ${n("\u2500".repeat(p)+s)}`}handleInput(t){try{this.dispatch(t)}catch{}}dispatch(t){if(this.pendingConfirm!==void 0){if(this.matches("crtr.graph.confirm.accept",t)){const r=this.pendingConfirm.action;this.pendingConfirm=void 0,r()}else this.pendingConfirm=void 0;this.tui.requestRender();return}if(this.matches("crtr.graph.dismiss",t)){this.close();return}const s=this.snapshot,i=s?.rows??[];let e=i.findIndex(r=>r.id===this.cursorId);e<0&&(e=Math.max(0,i.findIndex(r=>r.id===this.self)));const n=i[e],o=m(r=>r!==void 0&&!r.cycle,"isSelectable"),f=m((r,h)=>{for(let d=r+h;d>=0&&d<i.length;d+=h)if(o(i[d]))return d;return r},"step"),p=m(r=>{const h=r===1?0:i.length-1;for(let d=h;d>=0&&d<i.length;d+=r)if(o(i[d]))return d;return e},"edge");if(this.matches("crtr.graph.down",t)){e=f(e,1),this.cursorId=i[e]?.id??this.cursorId,this.tui.requestRender();return}if(this.matches("crtr.graph.up",t)){e=f(e,-1),this.cursorId=i[e]?.id??this.cursorId,this.tui.requestRender();return}if(this.matches("crtr.graph.first",t)){this.cursorId=i[p(1)]?.id??this.cursorId,this.tui.requestRender();return}if(this.matches("crtr.graph.last",t)){this.cursorId=i[p(-1)]?.id??this.cursorId,this.tui.requestRender();return}if(this.matches("crtr.graph.collapse-or-manager",t)){if(n!==void 0&&n.hasKids&&!n.collapsed)this.userCollapsed.add(n.id),this.userExpanded.delete(n.id),this.repaintFolds();else{const r=s?.managerOf.get(this.cursorId??this.self);r!==void 0&&i.some(h=>h.id===r)&&(this.cursorId=r),this.tui.requestRender()}return}if(this.matches("crtr.graph.expand-or-child",t)){if(n!==void 0&&n.collapsed&&n.hasKids)this.userExpanded.add(n.id),this.userCollapsed.delete(n.id),this.repaintFolds();else if(n!==void 0&&n.hasKids){const r=s?.sortedChildIds.get(n.id)?.[0];r!==void 0&&(this.cursorId=r),this.tui.requestRender()}return}if(this.matches("crtr.graph.focus",t)){if(this.remote)return;if(this.role!=="controller"){this.onNotice("Read-only observer attach \u2014 cannot focus nodes");return}this.cursorId!==void 0&&this.focusTarget(this.cursorId),this.close();return}if(this.matches("crtr.graph.revive",t)){if(this.remote)return;if(this.role!=="controller"){this.onNotice("Read-only observer attach \u2014 cannot revive nodes");return}const r=this.cursorId,h=r===void 0?null:s?.nodes.get(r)??null;r!==void 0&&h!==null&&h.status!=="active"&&h.status!=="idle"&&(this.reviveTarget(r),this.close());return}if(this.matches("crtr.graph.focus-manager",t)){if(this.remote)return;if(this.role!=="controller"){this.onNotice("Read-only observer attach \u2014 cannot focus nodes");return}const r=s?.managerOf.get(this.self);r!==void 0&&(this.focusTarget(r),this.close());return}if(this.matches("crtr.graph.close-subtree",t)){if(this.remote)return;if(this.role!=="controller"){this.onNotice("Read-only observer attach \u2014 cannot close nodes");return}const r=this.cursorId??this.self,h=s?.nodes.get(r)??null,d=h!==null?B(h):E(r);this.pendingConfirm={label:`kill ${d}?`,action:m(()=>this.shellCrtr(["node","lifecycle","close","--node",r]),"action")},this.tui.requestRender();return}}focusTarget(t){const s=["surface","node","focus",t],i=process.env.TMUX_PANE;i!==void 0&&i!==""&&s.push("--pane",i),this.shellCrtr(s)}reviveTarget(t){this.shellCrtr(["node","lifecycle","revive",t],()=>this.focusTarget(t))}shellCrtr(t,s){try{q("crtr",t,e=>{e==null&&s?.()}).stdin?.end()}catch{}}}export{be as GraphOverlay};
|