@@ -19,7 +19,15 @@ public class PaginationVisitor : ScmLibraryVisitor
1919{
2020
2121 private static readonly string [ ] _chatParamsToReplace = [ "after" , "before" , "limit" , "order" , "model" , "metadata" ] ;
22-
22+ private static readonly Dictionary < string , string > _paramReplacementMap = new ( )
23+ {
24+ { "after" , "AfterId" } ,
25+ { "before" , "LastId" } ,
26+ { "limit" , "PageSizeLimit" } ,
27+ { "order" , "Order" } ,
28+ { "model" , "Model" } ,
29+ { "metadata" , "Metadata" }
30+ } ;
2331 private static readonly Dictionary < string , ( string ReturnType , string OptionsType , string [ ] ParamsToReplace ) > _optionsReplacements = new ( )
2432 {
2533 {
@@ -36,14 +44,15 @@ public class PaginationVisitor : ScmLibraryVisitor
3644 {
3745 // Check if the method is one of the pagination methods we want to modify.
3846 // If so, we will update its parameters to replace the specified parameters with the options type.
39- if ( method . Signature . ReturnType is not null &&
47+ if (
48+ method . Signature . ReturnType is not null &&
4049 method . Signature . ReturnType . Name . EndsWith ( "CollectionResult" ) &&
4150 _optionsReplacements . TryGetValue ( method . Signature . Name , out var options ) &&
4251 method . Signature . ReturnType . IsGenericType &&
4352 method . Signature . ReturnType . Arguments . Count == 1 &&
4453 method . Signature . ReturnType . Arguments [ 0 ] . Name == options . ReturnType )
4554 {
46- var optionsType = OpenAILibraryGenerator . Instance . OutputLibrary . TypeProviders . SingleOrDefault ( t => t . Type . Name == options . ReturnType ) ;
55+ var optionsType = OpenAILibraryGenerator . Instance . OutputLibrary . TypeProviders . SingleOrDefault ( t => t . Type . Name == options . OptionsType ) ;
4756 if ( optionsType is not null )
4857 {
4958 // replace the method parameters with names in the _paramsToReplace array with the optionsType
@@ -78,7 +87,68 @@ public class PaginationVisitor : ScmLibraryVisitor
7887 methodSignature . ExplicitInterface ,
7988 methodSignature . NonDocumentComment ) ;
8089
81- method . Update ( signature : newSignature ) ;
90+ var optionsParam = newParameters [ lastRemovedIndex ] ;
91+
92+ // Update the method body statements to replace the old parameters with the new options parameter.
93+ var statements = method . BodyStatements ? . ToList ( ) ?? new List < MethodBodyStatement > ( ) ;
94+ VisitExplodedMethodBodyStatements ( statements ! ,
95+ statement =>
96+ {
97+ // Check if the statement is a return statement
98+ if ( statement is ExpressionStatement exp && exp . Expression is KeywordExpression keyword && keyword . Keyword == "return" )
99+ {
100+ // If it is, we will replace the parameters with the options parameter.
101+ if ( keyword . Expression is NewInstanceExpression newInstance &&
102+ newInstance . Parameters . Count > 0 )
103+ {
104+ // Create the new parameters with the options parameter.
105+ var newParameters = new List < ValueExpression > ( ) ;
106+ foreach ( var param in newInstance . Parameters )
107+ {
108+ if ( param is VariableExpression varExpr && options . ParamsToReplace . Contains ( varExpr . Declaration . RequestedName ) )
109+ {
110+ // Replace the parameter with the options parameter.
111+ if ( _paramReplacementMap . TryGetValue ( varExpr . Declaration . RequestedName , out var replacement ) )
112+ {
113+ newParameters . Add ( optionsParam . NullConditional ( ) . Property ( replacement ) ) ;
114+ var foo = optionsParam . NullConditional ( ) . Property ( replacement ) . NullConditional ( ) . Invoke ( "ToString" , Array . Empty < ValueExpression > ( ) ) ;
115+ }
116+ }
117+ else if ( param is InvokeMethodExpression invokeMethod && invokeMethod . MethodName == "ToString" &&
118+ invokeMethod . InstanceReference is NullConditionalExpression nullConditional &&
119+ nullConditional . Inner is VariableExpression varExpr2 &&
120+ options . ParamsToReplace . Contains ( varExpr2 . Declaration . RequestedName ) )
121+ {
122+ // Replace the parameter with the options parameter.
123+ if ( _paramReplacementMap . TryGetValue ( varExpr2 . Declaration . RequestedName , out var replacement ) )
124+ {
125+ newParameters . Add ( optionsParam . NullConditional ( ) . Property ( replacement ) . NullConditional ( ) . Invoke ( "ToString" , Array . Empty < ValueExpression > ( ) ) ) ;
126+ }
127+ }
128+ else
129+ {
130+ // Keep the original parameter.
131+ newParameters . Add ( param ) ;
132+ }
133+ }
134+ // Create a new ExpressionStatement with the same children as the original, but with the new parameters.
135+ var newNewInstanceExpression = new NewInstanceExpression (
136+ newInstance . Type ,
137+ newParameters ) ;
138+
139+ var newKeywordExpression = new KeywordExpression (
140+ keyword . Keyword ,
141+ newNewInstanceExpression ) ;
142+
143+ var newExpressionStatement = new ExpressionStatement ( newKeywordExpression ) ;
144+
145+ return newExpressionStatement ;
146+ }
147+ }
148+ return statement ;
149+ } ) ;
150+
151+ method . Update ( signature : newSignature , bodyStatements : statements ) ;
82152 }
83153 }
84154 }
0 commit comments