eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
2 lines • 3.84 kB
JavaScript
import{formatValuePretty,truncate}from"./tool-format.js";import{elisionText}from"./rail.js";import{clipVisible,visibleLength,wrapVisibleLine}from"#cli/ui/terminal-text.js";function renderTool(e,t,n,r){return(e.depth??0)===0?renderToolRows(e,t-1,n,r).map(e=>` ${e}`):renderToolRows(e,t,n,r)}function renderToolRows(e,t,n,r){let{icon:i,accent:a}=toolGlyph(e.status??`running`,n,r),o=e.status===`done`&&e.doneTitle!==void 0?e.doneTitle:e.title??`tool`,s=t-3,c=clipVisible(o,s),l=` ${i} ${boldLeadingWord(c,n)}`,u=s-c.length-2,d=e.subtitle??``;d.length>0&&u>=6&&(l+=` ${n.colors.gray(truncate(d,u))}`);let f=[l],p=[];return e.toolGroupItems===void 0?!e.expanded&&showToolDetail(e)&&p.push(...detailLineRows(e.detailLines??[],t,n)):p.push(...toolGroupItemRows(e.toolGroupItems,t,n)),p.length>0&&f.push(...p,` ${n.colors.dim(n.glyph.corner)}`),e.expanded?f.push(...renderToolExpanded(e,t,n)):e.status===`done`&&e.result&&e.result.length>0?f.push(resultLine(n.glyph.arrow,e.result,t,n,a)):e.status===`error`&&e.result?f.push(errorDetailLine(e.result,t,n)):e.status===`denied`&&f.push(resultLine(n.glyph.arrow,`denied`,t,n,n.colors.yellow)),f}function boldLeadingWord(e,t){let n=e.indexOf(` `);return n===-1?t.colors.bold(e):`${t.colors.bold(e.slice(0,n))}${t.colors.dim(e.slice(n))}`}function showToolDetail(e){if(e.detailLines===void 0||e.detailLines.length===0)return!1;let t=e.status??`running`;return t===`running`||t===`approval`||e.keepDetailWhenDone===!0}const maxVisibleToolGroupItems=5;function railRow(e,t){return` ${t.colors.dim(t.glyph.rule)} ${e}`}function elisionRow(e,t){return railRow(elisionText(e,t),t)}function toolGroupItemRows(e,t,n){let r=e.slice(0,5),i=e.length-r.length,a=itemRows(r,t,n);return i>0&&a.push(elisionRow(i,n)),a}function detailLineRows(e,t,n){let r=n.colors,i=e.slice(0,10),a=e.length-i.length,o=i.some(e=>e.kind===`added`||e.kind===`removed`),s=Math.max(1,t-(o?5:4)),c=i.map(e=>{if(e.kind===`gap`)return railRow(r.dim(n.glyph.ellipsis),n);let t=clipVisible(e.text,s);switch(e.kind){case`added`:return` ${r.dim(n.glyph.rule)}${r.green(`+`)} ${r.green(t)}`;case`removed`:return` ${r.dim(n.glyph.rule)}${r.red(`-`)} ${r.red(t)}`;default:return o?` ${r.dim(n.glyph.rule)} ${r.gray(t)}`:railRow(r.gray(t),n)}});return a>0&&c.push(elisionRow(a,n)),c}function itemRows(e,t,n){let r=Math.max(1,t-4);if(!e.some(e=>e.result!==void 0&&e.result.length>0))return e.map(e=>railRow(n.colors.gray(truncate(e.text,r)),n));let i=Math.min(Math.max(...e.map(e=>visibleLength(e.text))),Math.max(8,r-9)),a=Math.max(8,r-i-1);return e.map(e=>{let t=truncate(e.text,i);if(e.result===void 0||e.result.length===0)return railRow(n.colors.gray(t),n);let r=t+` `.repeat(Math.max(0,i-visibleLength(t)));return railRow(`${n.colors.gray(r)} ${n.colors.red(truncate(e.result,a))}`,n)})}function renderToolExpanded(e,t,n){let r=[],push=(e,i,a)=>{if(i!==void 0){r.push(` ${n.colors.dim(e)}`);for(let e of formatValuePretty(i).split(`
`))for(let n of wrapVisibleLine(e,Math.max(1,t-4)))r.push(` ${a(n)}`)}};return push(`input`,e.toolInput,n.colors.gray),e.status===`error`&&e.result?push(`error`,e.result,n.colors.red):push(`output`,e.toolOutput,n.colors.gray),r}function resultLine(e,t,n,r,i){let a=n-4;return` ${r.colors.dim(e)} ${i(truncate(t,a))}`}function errorDetailLine(e,t,n){let r=t-5;return` ${n.colors.dim(n.glyph.corner)} ${n.colors.red(truncate(e,r))}`}function toolGlyph(e,t,n){switch(e){case`done`:return{icon:t.colors.gray(t.glyph.square),accent:t.colors.gray};case`error`:return{icon:t.colors.red(t.glyph.error),accent:t.colors.red};case`denied`:return{icon:t.colors.yellow(t.glyph.warning),accent:t.colors.yellow};case`approval`:return{icon:t.colors.yellow(t.glyph.question),accent:t.colors.yellow};default:return{icon:t.colors.yellow(n.activityPulse),accent:t.colors.gray}}}export{maxVisibleToolGroupItems,renderTool};