@@ -5,9 +5,9 @@ import { Spinner } from "./spinner"
55import { chat , command } from "./session"
66import { formatTokens , formatDuration } from "../metrics"
77import { Icons } from "../cmd/tui/util/icons"
8- import { renderTaskPanel , type Task , type AgentStatus } from "./taskpanel"
8+ import { type Task , type AgentStatus } from "./taskpanel"
99import { renderStatusLine , type StatusLineState } from "./statusline"
10- import { renderBottomBar , renderPrompt , type BottomBarState } from "./bottombar"
10+ import { renderPrompt , type BottomBarState } from "./bottombar"
1111import { bootstrap } from "../bootstrap"
1212import { Log } from "../../util/log"
1313import { Global } from "../../global"
@@ -59,16 +59,26 @@ async function main() {
5959 level : "ERROR" ,
6060 } )
6161
62+ // Cleanup function
63+ function cleanup ( ) {
64+ write ( cursor . show )
65+ process . stdin . setRawMode ( false )
66+ }
67+
68+ // Register cleanup before bootstrap
69+ process . on ( "exit" , cleanup )
70+ process . on ( "SIGINT" , ( ) => {
71+ cleanup ( )
72+ process . exit ( 0 )
73+ } )
74+
6275 // Bootstrap with Instance context for current directory
6376 await bootstrap ( process . cwd ( ) , async ( ) => {
6477 // Initialize default agent
6578 currentAgent = await Agent . defaultAgent ( )
6679 const agentList = await Agent . list ( )
6780
6881 // Setup
69- write ( screen . alt )
70- write ( clear . screen )
71- write ( cursor . home )
7282
7383 // Header
7484 write ( `${ fg . brightCyan } ${ style . bold } oclite${ style . reset } ${ fg . gray } v0.1.0${ style . reset } \n` )
@@ -115,6 +125,11 @@ async function main() {
115125 return
116126 }
117127
128+ // Block further input during operation (except Escape and Ctrl+C which are handled above)
129+ if ( isOperationInProgress ) {
130+ return
131+ }
132+
118133 // Ctrl+T to toggle task panel
119134 if ( key . ctrl && key . name === "t" ) {
120135 tasksVisible = ! tasksVisible
@@ -138,19 +153,6 @@ async function main() {
138153 editor . render ( renderPrompt ( currentAgent ) )
139154 }
140155 } )
141-
142- // Cleanup on exit
143- function cleanup ( ) {
144- write ( cursor . show )
145- write ( screen . main )
146- process . stdin . setRawMode ( false )
147- }
148-
149- process . on ( "exit" , cleanup )
150- process . on ( "SIGINT" , ( ) => {
151- cleanup ( )
152- process . exit ( 0 )
153- } )
154156 } )
155157}
156158
@@ -357,55 +359,52 @@ async function handleCustomCommand(name: string, args: string) {
357359 isOperationInProgress = true
358360
359361 try {
360- process . stderr . write ( `[DEBUG] handleCustomCommand: Starting with name="${ name } ", args="${ args } "\n` )
361362 const options = {
362363 model : currentModel || undefined ,
363364 agent : currentAgent ,
364365 sessionID : currentSessionID || undefined ,
365366 }
366367
367- process . stderr . write ( `[DEBUG] handleCustomCommand: Created options, starting iteration\n` )
368- for await ( const chunk of command ( name , args , options ) ) {
369- process . stderr . write ( `[DEBUG] handleCustomCommand: Received chunk type="${ chunk . type } "\n` )
370- if ( chunk . type === "start" && chunk . sessionID && ! currentSessionID ) {
371- currentSessionID = chunk . sessionID
372- }
368+ try {
369+ for await ( const chunk of command ( name , args , options ) ) {
370+ if ( chunk . type === "start" && chunk . sessionID && ! currentSessionID ) {
371+ currentSessionID = chunk . sessionID
372+ }
373373
374- if ( first && chunk . type !== "done" && chunk . type !== "start" ) {
375- spinner . stop ( true )
376- first = false
377- write ( `${ fg . gray } (Esc to cancel)${ style . reset } \n` )
378- }
374+ if ( first && chunk . type !== "done" && chunk . type !== "start" ) {
375+ spinner . stop ( true )
376+ first = false
377+ write ( `${ fg . gray } (Esc to cancel)${ style . reset } \n` )
378+ }
379379
380- if ( chunk . type === "text" && chunk . content ) {
381- write ( chunk . content )
382- }
380+ if ( chunk . type === "text" && chunk . content ) {
381+ write ( chunk . content )
382+ }
383383
384- if ( chunk . type === "tool_start" && chunk . tool ?. trim ( ) ) {
385- write ( `\n ${ Icons . taskIcon ( "progress" ) } ${ chunk . tool } \n ` )
386- }
384+ if ( chunk . type === "tool_start" && chunk . tool ?. trim ( ) ) {
385+ write ( `${ fg . gray } ${ chunk . tool ?. trim ( ) } ${ style . reset } ` )
386+ }
387387
388- if ( chunk . type === "tool_end" && chunk . tool ?. trim ( ) ) {
389- write ( `${ Icons . taskIcon ( "completed" ) } ${ chunk . tool } \n ` )
390- }
388+ if ( chunk . type === "tool_end" && chunk . tool ?. trim ( ) ) {
389+ write ( `${ fg . gray } ✓ ${ style . reset } ` )
390+ }
391391
392- if ( chunk . type === "error" && chunk . content ) {
393- const safeContent = chunk . content . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, "" ) . replace ( / [ \x00 - \x1f \x7f ] / g, "" )
394- write ( `\n${ fg . red } Error: ${ safeContent } ${ style . reset } \n` )
395- }
392+ if ( chunk . type === "error" && chunk . content ) {
393+ const safeContent = chunk . content . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, "" ) . replace ( / [ \x00 - \x1f \x7f ] / g, "" )
394+ write ( `\n${ fg . red } Error: ${ safeContent } ${ style . reset } \n` )
395+ }
396396
397- if ( chunk . tokens !== undefined ) {
398- totalTokens += chunk . tokens
397+ if ( chunk . tokens !== undefined ) {
398+ totalTokens += chunk . tokens
399+ }
399400 }
400- }
401401
402- process . stderr . write ( `[DEBUG] handleCustomCommand: Iteration complete\n` )
403- const duration = Date . now ( ) - startTime
404- write ( `\n${ fg . gray } ${ formatDuration ( duration ) } · ${ formatTokens ( totalTokens ) } ${ style . reset } \n` )
402+ const duration = Date . now ( ) - startTime
403+ write ( `\n${ fg . gray } ${ formatDuration ( duration ) } · ${ formatTokens ( totalTokens ) } ${ style . reset } \n` )
404+ } finally {
405+ write ( cursor . show )
406+ }
405407 } catch ( err ) {
406- process . stderr . write (
407- `[DEBUG] handleCustomCommand: Caught error: ${ err instanceof Error ? err . message : String ( err ) } \n` ,
408- )
409408 if ( first ) spinner . stop ( false )
410409 const msg = err instanceof Error ? err . message : String ( err )
411410 write ( `\n${ fg . red } Error: ${ msg } ${ style . reset } \n` )
@@ -433,42 +432,45 @@ async function handleMessage(message: string) {
433432 sessionID : currentSessionID || undefined ,
434433 }
435434
436- for await ( const chunk of chat ( message , options ) ) {
437- // Capture sessionID from start chunk for cancellation
438- if ( chunk . type === "start" && chunk . sessionID && ! currentSessionID ) {
439- currentSessionID = chunk . sessionID
440- }
435+ try {
436+ for await ( const chunk of chat ( message , options ) ) {
437+ if ( chunk . type === "start" && chunk . sessionID && ! currentSessionID ) {
438+ currentSessionID = chunk . sessionID
439+ }
441440
442- if ( first && chunk . type !== "done" && chunk . type !== "start" ) {
443- spinner . stop ( true )
444- first = false
445- write ( `${ fg . gray } (Esc to cancel)${ style . reset } \n` )
446- }
441+ if ( first && chunk . type !== "done" && chunk . type !== "start" ) {
442+ spinner . stop ( true )
443+ first = false
444+ write ( `${ fg . gray } (Esc to cancel)${ style . reset } \n` )
445+ }
447446
448- if ( chunk . type === "text" && chunk . content ) {
449- write ( chunk . content )
450- }
447+ if ( chunk . type === "text" && chunk . content ) {
448+ write ( chunk . content )
449+ }
451450
452- if ( chunk . type === "tool_start" && chunk . tool ?. trim ( ) ) {
453- write ( `\n ${ Icons . taskIcon ( "progress" ) } ${ chunk . tool } \n ` )
454- }
451+ if ( chunk . type === "tool_start" && chunk . tool ?. trim ( ) ) {
452+ write ( `${ fg . gray } ${ chunk . tool ?. trim ( ) } ${ style . reset } ` )
453+ }
455454
456- if ( chunk . type === "tool_end" && chunk . tool ?. trim ( ) ) {
457- write ( `${ Icons . taskIcon ( "completed" ) } ${ chunk . tool } \n ` )
458- }
455+ if ( chunk . type === "tool_end" && chunk . tool ?. trim ( ) ) {
456+ write ( `${ fg . gray } ✓ ${ style . reset } ` )
457+ }
459458
460- if ( chunk . type === "error" && chunk . content ) {
461- const safeContent = chunk . content . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, "" ) . replace ( / [ \x00 - \x1f \x7f ] / g, "" )
462- write ( `\n${ fg . red } Error: ${ safeContent } ${ style . reset } \n` )
463- }
459+ if ( chunk . type === "error" && chunk . content ) {
460+ const safeContent = chunk . content . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, "" ) . replace ( / [ \x00 - \x1f \x7f ] / g, "" )
461+ write ( `\n${ fg . red } Error: ${ safeContent } ${ style . reset } \n` )
462+ }
464463
465- if ( chunk . tokens !== undefined ) {
466- totalTokens += chunk . tokens
464+ if ( chunk . tokens !== undefined ) {
465+ totalTokens += chunk . tokens
466+ }
467467 }
468- }
469468
470- const duration = Date . now ( ) - startTime
471- write ( `\n${ fg . gray } ${ formatDuration ( duration ) } · ${ formatTokens ( totalTokens ) } ${ style . reset } \n` )
469+ const duration = Date . now ( ) - startTime
470+ write ( `\n${ fg . gray } ${ formatDuration ( duration ) } · ${ formatTokens ( totalTokens ) } ${ style . reset } \n` )
471+ } finally {
472+ write ( cursor . show )
473+ }
472474 } catch ( err ) {
473475 if ( first ) spinner . stop ( false )
474476 const msg = err instanceof Error ? err . message : String ( err )
0 commit comments