@@ -111,6 +111,7 @@ public partial class SessionView : UserControl, INotifyPropertyChanged
111111 private const long ContextBaselineTokens = 12000 ;
112112 private static readonly string [ ] FileTreeIgnoredFolders = new [ ] { ".git" , "node_modules" , "bin" , "obj" , ".idea" , ".vs" } ;
113113 private bool _verboseLogging ;
114+ private bool _modelListUseLegacyPaging ;
114115 private bool _mcpEnabled ;
115116 private bool _allowNetworkAccess ;
116117 private bool _loginInProgress ;
@@ -644,19 +645,39 @@ private async Task<List<ModelCatalogEntry>> FetchModelCatalogAsync()
644645
645646 while ( true )
646647 {
647- var parameters = new JObject
648+ var parameters = new JObject ( ) ;
649+ if ( _modelListUseLegacyPaging )
648650 {
649- [ "limit" ] = PageSize ,
650- [ "pageSize" ] = PageSize // legacy field for older Codex builds
651- } ;
651+ parameters [ "pageSize" ] = PageSize ;
652+ }
653+ else
654+ {
655+ parameters [ "limit" ] = PageSize ;
656+ parameters [ "pageSize" ] = PageSize ; // fallback for older builds; ignored by newer ones
657+ }
658+
652659 if ( ! string . IsNullOrWhiteSpace ( cursor ) )
653660 parameters [ "cursor" ] = cursor ;
654661
655- var response = await SendRequestAsync ( "model/list" , parameters ) . ConfigureAwait ( false ) ;
656- if ( response is not JObject obj )
657- break ;
662+ JObject obj ;
663+ try
664+ {
665+ var response = await SendRequestAsync ( "model/list" , parameters ) . ConfigureAwait ( false ) ;
666+ if ( response is not JObject json )
667+ break ;
668+ obj = json ;
669+ }
670+ catch ( Exception ex ) when ( ShouldFallbackToLegacyModelList ( ex ) )
671+ {
672+ if ( _modelListUseLegacyPaging )
673+ throw ;
674+ _modelListUseLegacyPaging = true ;
675+ AppendCliLog ( "System: CLI returned legacy pagination error; retrying model catalog request." ) ;
676+ return await FetchModelCatalogAsync ( ) . ConfigureAwait ( false ) ;
677+ }
658678
659- if ( obj [ "items" ] is JArray items )
679+ var itemsToken = obj [ "items" ] ?? obj [ "data" ] ;
680+ if ( itemsToken is JArray items )
660681 {
661682 foreach ( var token in items . OfType < JObject > ( ) )
662683 {
@@ -666,7 +687,7 @@ private async Task<List<ModelCatalogEntry>> FetchModelCatalogAsync()
666687 }
667688 }
668689
669- var nextCursor = obj [ "nextCursor" ] ? . ToString ( ) ;
690+ var nextCursor = obj [ "nextCursor" ] ? . ToString ( ) ?? obj [ "next_cursor" ] ? . ToString ( ) ;
670691 if ( string . IsNullOrWhiteSpace ( nextCursor ) )
671692 break ;
672693 cursor = nextCursor ;
@@ -3463,6 +3484,14 @@ private static bool IsPathAncestorOrSame(string? ancestor, string? target)
34633484 return separator == Path . DirectorySeparatorChar || separator == Path . AltDirectorySeparatorChar ;
34643485 }
34653486
3487+ private static bool ShouldFallbackToLegacyModelList ( Exception ex )
3488+ {
3489+ if ( ex is not InvalidOperationException ioe )
3490+ return false ;
3491+ var message = ioe . Message ?? string . Empty ;
3492+ return message . IndexOf ( "invalid request" , StringComparison . OrdinalIgnoreCase ) >= 0 ;
3493+ }
3494+
34663495 private void ResetEditorViewport ( TextEditor ? editor )
34673496 {
34683497 if ( editor is null ) return ;
0 commit comments