Skip to content

Commit 7970bdf

Browse files
committed
fixup params
1 parent 2786c95 commit 7970bdf

File tree

7 files changed

+111
-24
lines changed

7 files changed

+111
-24
lines changed

codegen/generator/src/Visitors/PaginationVisitor.cs

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Custom/Chat/Internal/GeneratorStubs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,5 @@ internal partial class InternalUnknownChatCompletionRequestMessageContentPart {
121121
[CodeGenType("ChatCompletionMessageListObject")] internal readonly partial struct InternalChatCompletionMessageListObject {}
122122
[CodeGenType("ChatCompletionList")] internal partial class InternalChatCompletionList {}
123123
[CodeGenType("ChatCompletionMessageList")] internal partial class InternalChatCompletionMessageList {}
124-
[CodeGenType("ChatCompletionCollectionOptions")] internal partial class ChatCompletionCollectionOptions {}
125-
[CodeGenType("ChatCompletionCollectionOrder")] internal readonly partial struct ChatCompletionCollectionOrder { }
124+
[CodeGenType("ChatCompletionCollectionOptions")] public partial class ChatCompletionCollectionOptions {}
125+
[CodeGenType("ChatCompletionCollectionOrder")] public readonly partial struct ChatCompletionCollectionOrder { }

src/Generated/ChatClient.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,28 @@ public virtual AsyncCollectionResult GetChatCompletionsAsync(string after, int?
5050
}
5151

5252
[Experimental("OPENAI001")]
53-
public virtual CollectionResult<ChatCompletion> GetChatCompletions(ChatCompletion options = default, CancellationToken cancellationToken = default)
53+
public virtual CollectionResult<ChatCompletion> GetChatCompletions(ChatCompletionCollectionOptions options = default, CancellationToken cancellationToken = default)
5454
{
5555
return new ChatClientGetChatCompletionsCollectionResultOfT(
5656
this,
57-
after,
58-
limit,
59-
order?.ToString(),
60-
metadata,
61-
model,
57+
options?.AfterId,
58+
options?.PageSizeLimit,
59+
options?.Order?.ToString(),
60+
options?.Metadata,
61+
options?.Model,
6262
cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
6363
}
6464

6565
[Experimental("OPENAI001")]
66-
public virtual AsyncCollectionResult<ChatCompletion> GetChatCompletionsAsync(ChatCompletion options = default, CancellationToken cancellationToken = default)
66+
public virtual AsyncCollectionResult<ChatCompletion> GetChatCompletionsAsync(ChatCompletionCollectionOptions options = default, CancellationToken cancellationToken = default)
6767
{
6868
return new ChatClientGetChatCompletionsAsyncCollectionResultOfT(
6969
this,
70-
after,
71-
limit,
72-
order?.ToString(),
73-
metadata,
74-
model,
70+
options?.AfterId,
71+
options?.PageSizeLimit,
72+
options?.Order?.ToString(),
73+
options?.Metadata,
74+
options?.Model,
7575
cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
7676
}
7777

src/Generated/Models/Chat/ChatCompletionCollectionOptions.Serialization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace OpenAI.Chat
1212
{
13-
internal partial class ChatCompletionCollectionOptions : IJsonModel<ChatCompletionCollectionOptions>
13+
public partial class ChatCompletionCollectionOptions : IJsonModel<ChatCompletionCollectionOptions>
1414
{
1515
void IJsonModel<ChatCompletionCollectionOptions>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
1616
{

src/Generated/Models/Chat/ChatCompletionCollectionOptions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
78
using OpenAI;
89

910
namespace OpenAI.Chat
1011
{
11-
internal partial class ChatCompletionCollectionOptions
12+
[Experimental("OPENAI001")]
13+
public partial class ChatCompletionCollectionOptions
1214
{
1315
private protected IDictionary<string, BinaryData> _additionalBinaryDataProperties;
1416

@@ -31,7 +33,7 @@ internal ChatCompletionCollectionOptions(string afterId, int? pageSizeLimit, Cha
3133

3234
public int? PageSizeLimit { get; set; }
3335

34-
internal ChatCompletionCollectionOrder? Order { get; set; }
36+
public ChatCompletionCollectionOrder? Order { get; set; }
3537

3638
public IDictionary<string, string> Metadata { get; }
3739

src/Generated/Models/Chat/ChatCompletionCollectionOrder.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
using System;
66
using System.ComponentModel;
7+
using System.Diagnostics.CodeAnalysis;
78
using OpenAI;
89

910
namespace OpenAI.Chat
1011
{
11-
internal readonly partial struct ChatCompletionCollectionOrder : IEquatable<ChatCompletionCollectionOrder>
12+
[Experimental("OPENAI001")]
13+
public readonly partial struct ChatCompletionCollectionOrder : IEquatable<ChatCompletionCollectionOrder>
1214
{
1315
private readonly string _value;
1416
private const string AscValue = "asc";
@@ -21,9 +23,9 @@ public ChatCompletionCollectionOrder(string value)
2123
_value = value;
2224
}
2325

24-
internal static ChatCompletionCollectionOrder Asc { get; } = new ChatCompletionCollectionOrder(AscValue);
26+
public static ChatCompletionCollectionOrder Asc { get; } = new ChatCompletionCollectionOrder(AscValue);
2527

26-
internal static ChatCompletionCollectionOrder Desc { get; } = new ChatCompletionCollectionOrder(DescValue);
28+
public static ChatCompletionCollectionOrder Desc { get; } = new ChatCompletionCollectionOrder(DescValue);
2729

2830
public static bool operator ==(ChatCompletionCollectionOrder left, ChatCompletionCollectionOrder right) => left.Equals(right);
2931

src/Generated/OpenAIModelFactory.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,5 +1035,18 @@ public static AudioTokenLogProbabilityDetails AudioTokenLogProbabilityDetails(st
10351035
{
10361036
return new AudioTokenLogProbabilityDetails(token, logProbability, utf8Bytes, additionalBinaryDataProperties: null);
10371037
}
1038+
1039+
public static ChatCompletionCollectionOptions ChatCompletionCollectionOptions(string afterId = default, int? pageSizeLimit = default, ChatCompletionCollectionOrder? order = default, IDictionary<string, string> metadata = default, string model = default)
1040+
{
1041+
metadata ??= new ChangeTrackingDictionary<string, string>();
1042+
1043+
return new ChatCompletionCollectionOptions(
1044+
afterId,
1045+
pageSizeLimit,
1046+
order,
1047+
metadata,
1048+
model,
1049+
additionalBinaryDataProperties: null);
1050+
}
10381051
}
10391052
}

0 commit comments

Comments
 (0)