@@ -245,6 +245,37 @@ export function getAgentsViewDepth(scopeRoot: SessionSummary | undefined): numbe
245245 return scopeRoot ? ( scopeRoot . rlmDepth ?? 0 ) + 1 : 0 ;
246246}
247247
248+ export function createInitialAgentsViewScopeFrames (
249+ initialScopeKey : AgentsViewScopeKey | undefined ,
250+ returnChat : SessionSummary | undefined ,
251+ ) : AgentsViewScopeFrame [ ] {
252+ if ( ! initialScopeKey ) return [ ] ;
253+ return [
254+ {
255+ scope : initialScopeKey ,
256+ ...( returnChat ?. sessionId === initialScopeKey . sessionId ? { returnChat } : { } ) ,
257+ } ,
258+ ] ;
259+ }
260+
261+ export function createInitialAgentsViewPersistentState (
262+ options : Pick < AgentsViewModeOptions , "initialScopeKey" | "initialSession" > ,
263+ ) : AgentsViewPersistentState {
264+ const initialSession = options . initialSession ;
265+ return {
266+ ...( initialSession
267+ ? {
268+ selectedRowIdentity : getSummaryIdentity ( initialSession ) ,
269+ selectedSessionKey : getAgentsViewSelectionKey ( initialSession ) ,
270+ backSession : initialSession ,
271+ }
272+ : { } ) ,
273+ ...( options . initialScopeKey
274+ ? { scopeFrames : createInitialAgentsViewScopeFrames ( options . initialScopeKey , initialSession ) }
275+ : { } ) ,
276+ } ;
277+ }
278+
248279export function createScopeBackReturnChatOpenResult (
249280 result : Extract < AgentsViewRunResult , { type : "scope_back" } > ,
250281) : Extract < AgentsViewRunResult , { type : "open" } > | undefined {
@@ -369,17 +400,7 @@ function isUnknownActiveSessionError(error: unknown): boolean {
369400}
370401
371402export async function runAgentsViewMode ( options : AgentsViewModeOptions ) : Promise < void > {
372- const initialSession = options . initialSession ;
373- const persistentState : AgentsViewPersistentState = {
374- ...( initialSession
375- ? {
376- selectedRowIdentity : getSummaryIdentity ( initialSession ) ,
377- selectedSessionKey : getAgentsViewSelectionKey ( initialSession ) ,
378- backSession : initialSession ,
379- }
380- : { } ) ,
381- ...( options . initialScopeKey ? { scopeFrames : [ { scope : options . initialScopeKey } ] } : { } ) ,
382- } ;
403+ const persistentState = createInitialAgentsViewPersistentState ( options ) ;
383404 const promptStashStore = options . promptStashStore ?? new ClientPromptStashStore ( ) ;
384405
385406 while ( true ) {
@@ -637,7 +658,11 @@ export class AgentsViewMode implements Component, Focusable {
637658 private readonly persistentState : AgentsViewPersistentState = { } ,
638659 ) {
639660 const initialFrames =
640- persistentState . scopeFrames ?? ( options . initialScopeKey ? [ { scope : options . initialScopeKey } ] : [ ] ) ;
661+ persistentState . scopeFrames ??
662+ createInitialAgentsViewScopeFrames (
663+ options . initialScopeKey ,
664+ persistentState . backSession ?? options . initialSession ,
665+ ) ;
641666 persistentState . scopeFrames = initialFrames ;
642667 this . scopeKey = initialFrames . at ( - 1 ) ?. scope ;
643668 this . selectedRowIdentity = persistentState . selectedRowIdentity ;
0 commit comments