eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.13 kB
JavaScript
import"./rail.js";import{clipVisible,stripTerminalControls}from"#cli/ui/terminal-text.js";const MESSAGE_QUEUE_LIMIT=5;var MessageQueue=class{#e=[];#t;#n=!1;get size(){return this.#e.length}get full(){return this.#e.length>=5}get idle(){return this.#e.length===0&&this.#t===void 0&&!this.#n}enqueue(e){return!this.full&&(this.#e.push(e),!0)}handleEscape(){let e=this.#e.shift();return e===void 0?this.#t===void 0?(this.#n=!0,`cancel`):`steer`:(this.#t=joinMessages(this.#t,e),`steer`)}requestCancellation(){this.#n=!0}beginTurn(){this.#n=!1}takePrompt(){this.#n=!1;let e=this.#t;return e===void 0?this.#r():(this.#t=void 0,e)}restoreDraft(){this.#n=!1;let e=this.#t;return this.#t=void 0,joinOptionalMessages(e,this.#r())}reset(){this.#e=[],this.#t=void 0,this.#n=!1}view(){return{messages:[...this.#e],full:this.full,steering:this.#t!==void 0,cancelling:this.#n}}#r(){if(this.#e.length===0)return;let e=this.#e.reduce(joinOptionalMessages,void 0);return this.#e=[],e}};function joinMessages(e,t){return e===void 0?t:`${e}\n\n${t}`}function joinOptionalMessages(e,t){return t===void 0?e:joinMessages(e,t)}function renderMessageQueueRows(e){let{view:t,width:n,theme:r,working:i}=e,a=r.colors,o=r.glyph;if(t.messages.length===0&&!t.steering)return i&&t.cancelling?[clipVisible(` ${a.yellow(o.dotActive)} ${a.dim(`Cancelling turn… · Ctrl+C to stop waiting, then again to exit`)}`,n)]:[];let s=[clipVisible(` ${a.gray(o.arrowUp)} ${headerBody(t,i,r)}`,n)];for(let[e,r]of t.messages.entries()){let i=e===t.messages.length-1?o.corner:o.rule,c=firstLine(stripTerminalControls(r));s.push(clipVisible(` ${a.dim(i)} ${a.dim(c)}`,n))}return s}function headerBody(e,t,n){let r=n.colors,i=` ${n.glyph.dot} `,a=`${String(e.messages.length)}/5`;if(e.steering){let t=e.messages.length>0?`${i}${a} still queued`:``;return r.dim(`Steering — cancelling the running turn…${t}`)}let o=e.full?`${i}queue full`:``,s=t?`${i}esc or ctrl+c steers with the next message`:``;return`${r.bold(`Queue`)} ${r.dim(`${a}${o}${s}`)}`}function firstLine(e){return e.split(/\r?\n/u,1)[0]??``}export{MESSAGE_QUEUE_LIMIT,MessageQueue,renderMessageQueueRows};