@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
3 lines (2 loc) • 4.04 kB
JavaScript
import { createRequire as __piCreateRequire } from "node:module"; const require = __piCreateRequire(import.meta.url); { const __piWorkerThreads = require("node:worker_threads"); if (typeof __piWorkerThreads.markAsUncloneable !== "function") { __piWorkerThreads.markAsUncloneable = () => {}; } }
import{DEFAULT_SCROLLBACK,MAX_SESSION_OUTPUT_CHARS,SecretOutputRedactor,loadPty}from"./chunk-5GCZFK4X.js";import"./chunk-IKOFCODM.js";var pty=await loadPty(),RELEASED_SCREEN={cols:0,rows:0,visibleGrid:[],scrollback:[],cursor:{x:0,y:0}},TerminalRuntimeSession=class{constructor(command,options,bootAtMs){this.finalScreen=null,this.decoder=new TextDecoder("utf-8",{fatal:!1}),this.buffer="",this.droppedChars=0,this.consumed=0,this.outputListeners=new Set,this.disposeListeners=new Set,this.unsubscribeData=null,this.lastScreenWrite=null,this.bootAtMs=bootAtMs,this.redactor=options.redactSecretsInOutput===!0?new SecretOutputRedactor:void 0,this.command=command,this.argv=[options.command??command,...options.args??[]],this.screen=new pty.TerminalScreen({cols:options.cols,rows:options.rows,scrollback:options.scrollback??DEFAULT_SCROLLBACK}),this.session=new pty.TerminalSession(options),this.unsubscribeData=this.session.onData(chunk=>{let text=this.decoder.decode(chunk,{stream:!0});this.ingest(this.redactor?this.redactor.push(text):text);let redactor=this.redactor;redactor&&(clearTimeout(this.redactionIdleTimer),this.redactionIdleTimer=setTimeout(()=>{this.redactionIdleTimer=void 0,this.ingest(redactor.flushIdle())},50),this.redactionIdleTimer.unref?.())}),this.screenRelease=new Promise(resolve=>{this.session.onExit(()=>{clearTimeout(this.redactionIdleTimer),this.redactionIdleTimer=void 0,this.redactor&&this.ingest(this.redactor.push(this.decoder.decode())+this.redactor.finish()),this.releaseScreen().catch(()=>{}).finally(resolve)})}),this.session.start(),this.startedAtMs=Date.now()}screenReleased(){return this.screenRelease}async releaseScreen(){let screen=this.screen;if(screen&&(await screen.flush().catch(()=>{}),this.screen===screen)){this.screen=null;try{this.finalScreen={...screen.snapshot(),scrollback:[]}}finally{screen.dispose()}}}get backend(){return this.session.backend}identity(){let pid=this.session.pid;if(pid===void 0)return;let processGroupId=this.session.processGroupId;return{pid,...processGroupId===void 0?{}:{processGroupId},startedAtMs:this.startedAtMs,bootAtMs:this.bootAtMs,argv:this.argv}}get exited(){return this.session.exited}get exitResult(){return this.session.exitResult}get totalChars(){return this.droppedChars+this.buffer.length}ingest(text){if(text.length!==0){if(this.screen&&this.ownScreenWrite(this.screen.feed(text)),this.buffer+=text,this.buffer.length>MAX_SESSION_OUTPUT_CHARS){let overflow=this.buffer.length-MAX_SESSION_OUTPUT_CHARS;this.buffer=this.buffer.slice(overflow),this.droppedChars+=overflow}for(let listener of this.outputListeners)try{listener(text)}catch{}}}onOutput(listener){return this.outputListeners.add(listener),()=>this.outputListeners.delete(listener)}onDispose(listener){return this.disposeListeners.add(listener),()=>this.disposeListeners.delete(listener)}readDelta(){let start=Math.max(this.consumed,this.droppedChars),dropped=Math.max(0,this.droppedChars-this.consumed),text=this.buffer.slice(start-this.droppedChars);return this.consumed=this.totalChars,{text,droppedChars:dropped}}fullOutput(){return this.buffer}snapshot(){return this.screen?this.screen.snapshot():this.finalScreen??RELEASED_SCREEN}resizeScreen(cols,rows){this.screen&&this.ownScreenWrite(this.screen.resize(cols,rows))}ownScreenWrite(written){written!==this.lastScreenWrite&&(this.lastScreenWrite=written,written.catch(()=>{}))}dispose(){clearTimeout(this.redactionIdleTimer),this.redactionIdleTimer=void 0,this.unsubscribeData?.(),this.unsubscribeData=null,this.outputListeners.clear(),this.screen?.dispose(),this.screen=null;let disposeListeners=[...this.disposeListeners];this.disposeListeners.clear();for(let listener of disposeListeners)listener()}};export{TerminalRuntimeSession};