@@ -19,8 +19,20 @@ import { select } from "./select"
1919import type { ChatChunk } from "./session"
2020import { createLiveBlock } from "./liveblock"
2121import { summarizeInput } from "./summary"
22+ import { wrap } from "./wrap"
2223export { summarizeInput }
2324
25+ // UI Constants
26+ const PAD = " "
27+ const MAX_WIDTH = Math . min ( 100 , ( process . stdout . columns || 80 ) - 4 )
28+
29+ function padLines ( text : string ) : string {
30+ return text
31+ . split ( "\n" )
32+ . map ( ( line ) => ( line ? `${ PAD } ${ line } ` : line ) )
33+ . join ( "\n" )
34+ }
35+
2436// UI State
2537let tasksVisible = false
2638let isOperationInProgress = false
@@ -72,10 +84,13 @@ async function main() {
7284 // Bootstrap with Instance context for current directory
7385 try {
7486 await bootstrap ( process . cwd ( ) , async ( ) => {
87+ // Force eager init of heavy subsystems during spinner
88+ await Promise . all ( [ Provider . list ( ) , Agent . list ( ) ] )
89+
7590 setup . stop ( true )
7691 write ( `${ fg . green } ✓${ style . reset } Ready\n\n` )
7792
78- // Initialize default agent
93+ // Initialize default agent (agentList is cached from eager init)
7994 currentAgent = await Agent . defaultAgent ( )
8095 const agentList = await Agent . list ( )
8196
@@ -366,8 +381,8 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
366381 if ( first && chunk . type !== "done" && chunk . type !== "start" ) {
367382 spinner . stop ( true )
368383 first = false
369- write ( `${ fg . gray } (Esc to cancel)${ style . reset } \n` )
370- const rule = `${ style . dim } ${ "─" . repeat ( Math . min ( process . stdout . columns || 80 , 80 ) ) } ${ style . reset } \n`
384+ write ( `${ PAD } ${ fg . gray } (Esc to cancel)${ style . reset } \n` )
385+ const rule = `${ PAD } ${ style . dim } ${ "─" . repeat ( Math . min ( process . stdout . columns || 80 , 80 ) ) } ${ style . reset } \n`
371386 write ( rule )
372387 }
373388
@@ -376,7 +391,9 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
376391 lastToolKey = ""
377392 dedupCount = 0
378393 lastChunkType = "text"
379- write ( md . render ( chunk . content ) )
394+ const rendered = md . render ( chunk . content )
395+ const wrapped = wrap ( rendered , MAX_WIDTH )
396+ write ( padLines ( wrapped ) )
380397 }
381398
382399 if ( chunk . type === "tool_start" && chunk . tool ?. trim ( ) ) {
@@ -424,7 +441,7 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
424441 dedupCount = 0
425442 lastChunkType = "error"
426443 const safeContent = chunk . content . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, "" ) . replace ( / [ \x00 - \x1f \x7f ] / g, "" )
427- write ( `\n${ fg . red } Error: ${ safeContent } ${ style . reset } \n` )
444+ write ( `\n${ PAD } ${ fg . red } Error: ${ safeContent } ${ style . reset } \n` )
428445 }
429446
430447 if ( chunk . tokens !== undefined ) {
@@ -434,10 +451,10 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
434451
435452 block . freeze ( )
436453 write ( md . flush ( ) )
437- const rule = `\n${ style . dim } ${ "─" . repeat ( Math . min ( process . stdout . columns || 80 , 80 ) ) } ${ style . reset } \n`
454+ const rule = `\n${ PAD } ${ style . dim } ${ "─" . repeat ( Math . min ( process . stdout . columns || 80 , 80 ) ) } ${ style . reset } \n`
438455 write ( rule )
439456 const duration = Date . now ( ) - startTime
440- write ( `${ fg . gray } ${ formatDuration ( duration ) } · ${ formatTokens ( totalTokens ) } ${ style . reset } \n` )
457+ write ( `${ PAD } ${ fg . gray } ${ formatDuration ( duration ) } · ${ formatTokens ( totalTokens ) } ${ style . reset } \n` )
441458 } finally {
442459 spinner . stop ( true )
443460 }
0 commit comments