@@ -89,145 +89,157 @@ async function main() {
8989 setup . start ( )
9090
9191 try {
92- await bootstrap ( process . cwd ( ) , async ( ) => {
93- // Subscribe to Bus events for child session tools
94- // This must be inside bootstrap so Instance.provide() context is established
95- busUnsubscribe = Bus . subscribe ( MessageV2 . Event . PartUpdated , ( event ) => {
96- const part = event . properties . part
97- if ( part . type !== "tool" ) return
98-
99- // Find which task this child session belongs to
100- let taskId : string | null = null
101- for ( const [ id , childSessionID ] of taskToChildSession . entries ( ) ) {
102- if ( childSessionID === part . sessionID ) {
103- taskId = id
104- break
92+ await bootstrap (
93+ process . cwd ( ) ,
94+ async ( ) => {
95+ // Subscribe to Bus events for child session tools
96+ // This must be inside bootstrap so Instance.provide() context is established
97+ busUnsubscribe = Bus . subscribe ( MessageV2 . Event . PartUpdated , ( event ) => {
98+ const part = event . properties . part
99+ if ( part . type !== "tool" ) return
100+
101+ // Find which task this child session belongs to
102+ let taskId : string | null = null
103+ for ( const [ id , childSessionID ] of taskToChildSession . entries ( ) ) {
104+ if ( childSessionID === part . sessionID ) {
105+ taskId = id
106+ break
107+ }
105108 }
106- }
107-
108- if ( ! taskId ) return
109-
110- // Update task child tool display
111- if ( part . state . status === "running" ) {
112- const arg = summarizeInput ( part . tool , part . state . input )
113- block . setTaskChildTool ( taskId , part . tool , arg || "" )
114- } else if ( part . state . status === "completed" || part . state . status === "error" ) {
115- block . clearTaskChildTool ( taskId )
116- }
117- } )
118- await Promise . all ( [ Provider . list ( ) , Agent . list ( ) , MCP . clients ( ) , Command . list ( ) ] )
119109
120- setup . stop ( true )
121- write ( `${ fg . green } ✓${ style . reset } Ready\n\n` )
110+ if ( ! taskId ) return
122111
123- currentAgent = await Agent . defaultAgent ( )
124- const agentList = await Agent . list ( )
112+ // Update task child tool display
113+ if ( part . state . status === "running" ) {
114+ const arg = summarizeInput ( part . tool , part . state . input )
115+ block . setTaskChildTool ( taskId , part . tool , arg || "" )
116+ } else if ( part . state . status === "completed" || part . state . status === "error" ) {
117+ block . clearTaskChildTool ( taskId )
118+ }
119+ } )
125120
126- write ( `${ fg . gray } Type /help for commands, Shift+Tab to cycle agents, Ctrl+C to exit${ style . reset } \n\n` )
121+ setup . update ( "Loading providers" )
122+ await Provider . list ( )
123+ setup . update ( "Loading agents" )
124+ await Agent . list ( )
125+ setup . update ( "Connecting to MCP servers" )
126+ await MCP . clients ( )
127+ setup . update ( "Loading commands" )
128+ await Command . list ( )
127129
128- const editor = new LineEditor ( )
129- process . stdin . setRawMode ( true )
130- process . stdin . resume ( )
130+ setup . stop ( true )
131+ write ( `${ fg . green } ✓${ style . reset } Ready\n\n` )
131132
132- editor . render ( renderPrompt ( currentAgent ) )
133+ currentAgent = await Agent . defaultAgent ( )
134+ const agentList = await Agent . list ( )
133135
134- process . stdin . on ( "data" , async ( data : Buffer ) => {
135- const key = parseKey ( data )
136+ write ( `${ fg . gray } Type /help for commands, Shift+Tab to cycle agents, Ctrl+C to exit${ style . reset } \n\n` )
136137
137- if ( key . ctrl && key . name === "c" ) {
138- cleanup ( )
139- process . exit ( 0 )
140- }
138+ const editor = new LineEditor ( )
139+ process . stdin . setRawMode ( true )
140+ process . stdin . resume ( )
141141
142- if ( key . name === "shift_tab" ) {
143- const available = agentList . filter ( ( a ) => a . mode !== "subagent" && ! a . hidden )
144- const index = available . findIndex ( ( a ) => a . name === currentAgent )
145- const next = index === - 1 ? 0 : ( index + 1 ) % available . length
146- currentAgent = available [ next ] . name
147- write ( `\r${ clear . line } ` )
148- editor . render ( renderPrompt ( currentAgent ) )
149- return
150- }
142+ editor . render ( renderPrompt ( currentAgent ) )
151143
152- if ( key . name === "escape" && isOperationInProgress && currentSessionID ) {
153- block . freeze ( )
154- SessionPrompt . cancel ( currentSessionID )
155- isOperationInProgress = false
156- currentSessionID = null
157- write ( "\n" )
158- editor . line = ""
159- editor . cursor = 0
160- editor . render ( renderPrompt ( currentAgent ) )
161- return
162- }
144+ process . stdin . on ( "data" , async ( data : Buffer ) => {
145+ const key = parseKey ( data )
163146
164- if ( isOperationInProgress ) {
165- return
166- }
147+ if ( key . ctrl && key . name === "c" ) {
148+ cleanup ( )
149+ process . exit ( 0 )
150+ }
167151
168- if ( key . ctrl && key . name === "x" ) {
169- Panel . enterNavigationMode ( )
170- return
171- }
152+ if ( key . name === "shift_tab" ) {
153+ const available = agentList . filter ( ( a ) => a . mode !== "subagent" && ! a . hidden )
154+ const index = available . findIndex ( ( a ) => a . name === currentAgent )
155+ const next = index === - 1 ? 0 : ( index + 1 ) % available . length
156+ currentAgent = available [ next ] . name
157+ write ( `\r${ clear . line } ` )
158+ editor . render ( renderPrompt ( currentAgent ) )
159+ return
160+ }
172161
173- if ( Panel . isInNavigationMode ( ) ) {
174- if ( key . name === "escape" ) {
175- Panel . exitNavigationMode ( )
162+ if ( key . name === "escape" && isOperationInProgress && currentSessionID ) {
163+ block . freeze ( )
164+ SessionPrompt . cancel ( currentSessionID )
165+ isOperationInProgress = false
166+ currentSessionID = null
167+ write ( "\n" )
168+ editor . line = ""
169+ editor . cursor = 0
176170 editor . render ( renderPrompt ( currentAgent ) )
177171 return
178172 }
179173
180- let direction : "left" | "right" | "up" | null = null
174+ if ( isOperationInProgress ) {
175+ return
176+ }
181177
182- if ( key . name === "left" || key . name === "h" ) {
183- direction = "left"
184- } else if ( key . name === "right" || key . name === "l" ) {
185- direction = "right"
186- } else if ( key . name === "up" || key . name === "k" ) {
187- direction = "up"
178+ if ( key . ctrl && key . name === "x" ) {
179+ Panel . enterNavigationMode ( )
180+ return
188181 }
189182
190- if ( direction ) {
191- const navigated = Panel . navigate ( direction )
183+ if ( Panel . isInNavigationMode ( ) ) {
184+ if ( key . name === "escape" ) {
185+ Panel . exitNavigationMode ( )
186+ editor . render ( renderPrompt ( currentAgent ) )
187+ return
188+ }
189+
190+ let direction : "left" | "right" | "up" | null = null
192191
193- if ( navigated ) {
194- await Panel . renderPanel ( )
195- const hint = Panel . getHint ( )
196- if ( hint ) {
197- write ( `\n${ hint } \n` )
192+ if ( key . name === "left" || key . name === "h" ) {
193+ direction = "left"
194+ } else if ( key . name === "right" || key . name === "l" ) {
195+ direction = "right"
196+ } else if ( key . name === "up" || key . name === "k" ) {
197+ direction = "up"
198+ }
199+
200+ if ( direction ) {
201+ const navigated = Panel . navigate ( direction )
202+
203+ if ( navigated ) {
204+ await Panel . renderPanel ( )
205+ const hint = Panel . getHint ( )
206+ if ( hint ) {
207+ write ( `\n${ hint } \n` )
208+ }
209+ write ( clear . screen )
210+ write ( cursor . home )
198211 }
199- write ( clear . screen )
200- write ( cursor . home )
201212 }
213+
214+ Panel . exitNavigationMode ( )
215+ editor . render ( renderPrompt ( currentAgent ) )
216+ return
202217 }
203218
204- Panel . exitNavigationMode ( )
205- editor . render ( renderPrompt ( currentAgent ) )
206- return
207- }
219+ if ( key . ctrl && key . name === "t" ) {
220+ block . toggleTasksVisible ( )
221+ editor . render ( renderPrompt ( currentAgent ) )
222+ }
208223
209- if ( key . ctrl && key . name === "t" ) {
210- block . toggleTasksVisible ( )
211- editor . render ( renderPrompt ( currentAgent ) )
212- }
224+ const result = editor . handle ( key )
213225
214- const result = editor . handle ( key )
226+ if ( result !== null ) {
227+ write ( "\n" )
215228
216- if ( result !== null ) {
217- write ( "\n" )
229+ if ( result . startsWith ( "/" ) ) {
230+ await handleCommand ( result )
231+ } else if ( result . trim ( ) ) {
232+ await handleMessage ( result )
233+ }
218234
219- if ( result . startsWith ( "/" ) ) {
220- await handleCommand ( result )
221- } else if ( result . trim ( ) ) {
222- await handleMessage ( result )
235+ editor . render ( renderPrompt ( currentAgent ) )
236+ } else {
237+ editor . render ( renderPrompt ( currentAgent ) )
223238 }
224-
225- editor . render ( renderPrompt ( currentAgent ) )
226- } else {
227- editor . render ( renderPrompt ( currentAgent ) )
228- }
229- } )
230- } )
239+ } )
240+ } ,
241+ ( step ) => setup . update ( step ) ,
242+ )
231243 } catch ( err ) {
232244 setup . stop ( false )
233245 write ( cursor . show )
@@ -377,6 +389,7 @@ interface StreamOptions {
377389
378390async function streamResponse ( source : AsyncIterable < ChatChunk > , options : StreamOptions ) {
379391 block . reset ( )
392+ taskToChildSession . clear ( )
380393 const spinner = new Spinner ( "Thinking" )
381394 spinner . start ( )
382395
@@ -520,7 +533,6 @@ async function streamResponse(source: AsyncIterable<ChatChunk>, options: StreamO
520533 taskToChildSession . set ( id , childSessionID )
521534 }
522535 block . taskEnd ( id )
523- taskToChildSession . delete ( id )
524536 }
525537 }
526538
0 commit comments