UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

3 lines 2.86 kB
import{EventEmitter}from"node:events";const ansiControlSequencePattern=RegExp(`^\x1B\\[([0-9?;]*)([ -/]*)([@-~])`);var MockUserInput=class extends EventEmitter{isTTY=!0;rawModes=[];resumeCalls=0;pauseCalls=0;setRawMode(e){return this.rawModes.push(e),this}resume(){return this.resumeCalls+=1,this}pause(){return this.pauseCalls+=1,this}type(e){this.emit(`data`,Buffer.from(e))}send(e){this.emit(`data`,Buffer.from(e))}enter(){this.send(`\r`)}backspace(){this.send(``)}up(){this.send(`\x1B[A`)}down(){this.send(`\x1B[B`)}left(){this.send(`\x1B[D`)}right(){this.send(`\x1B[C`)}ctrlC(){this.send(``)}},MockScreen=class extends EventEmitter{isTTY=!0;columns;rows;#e=``;#t=[];#n=0;#r=0;#i=[];constructor({columns:e,rows:t}){super(),this.columns=e,this.rows=t}write(e,t,n){let r=String(e);return this.#e+=r,this.#o(r),typeof t==`function`&&t(),n?.(),this.#a(),!0}resize(e,t){this.columns=e,this.rows=t,this.emit(`resize`)}snapshot(){return this.#t.join(` `)}rawOutput(){return this.#e}async waitForText(e,t=1e3,n=()=>this.snapshot()){this.snapshot().includes(e)||await new Promise((r,i)=>{let a={text:e,resolve:r,reject:i,timeout:setTimeout(()=>{this.#i=this.#i.filter(e=>e!==a),i(Error(`Timed out waiting for screen text: ${e}\n\nScreen:\n${n()}`))},t)};this.#i.push(a)})}#a(){let e=this.snapshot();for(let t of this.#i.slice())e.includes(t.text)&&(clearTimeout(t.timeout),this.#i=this.#i.filter(e=>e!==t),t.resolve())}#o(e){let t=0;for(;t<e.length;){if(e[t]===`\x1B`){let n=this.#s(e,t);if(n>t){t=n;continue}}let n=e[t];if(t+=1,n!==void 0){if(n===` `){this.#n+=1,this.#r=0;continue}if(n===`\r`){this.#r=0;continue}this.#u(n)}}}#s(e,n){let r=e.slice(n).match(ansiControlSequencePattern);if(!r)return n;let[i,a=``,,o]=r,s=a.startsWith(`?`),c=a&&!s?a.split(`;`):[],first=e=>c[0]===void 0||c[0]===``?e:Number(c[0]);if(s)return n+i.length;switch(o){case`H`:case`f`:this.#n=first(1)-1,this.#r=(c[1]?Number(c[1]):1)-1;break;case`A`:this.#n=Math.max(0,this.#n-first(1));break;case`B`:this.#n+=first(1);break;case`C`:this.#r+=first(1);break;case`D`:this.#r=Math.max(0,this.#r-first(1));break;case`E`:this.#n+=first(1),this.#r=0;break;case`F`:this.#n=Math.max(0,this.#n-first(1)),this.#r=0;break;case`G`:this.#r=first(1)-1;break;case`J`:this.#c(first(0));break;case`K`:this.#l(first(0));break;default:break}return n+i.length}#c(e){if(e===2||e===3){this.#t=[],this.#n=0,this.#r=0;return}if(e===1){for(let e=0;e<this.#n;e+=1)this.#t[e]=``;this.#l(1);return}this.#l(0),this.#t.length=Math.min(this.#t.length,this.#n+1)}#l(e){let t=this.#t[this.#n]??``;if(e===2){this.#t[this.#n]=``;return}if(e===1){this.#t[this.#n]=` `.repeat(this.#r)+t.slice(this.#r);return}this.#t[this.#n]=t.slice(0,this.#r)}#u(e){let t=(this.#t[this.#n]??``).padEnd(this.#r,` `),n=t.slice(0,this.#r)+e+t.slice(this.#r+e.length);this.#t[this.#n]=n,this.#r+=e.length}};export{MockScreen,MockUserInput};