Skip to content

Commit

Permalink
fix: Fixed Generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jul 7, 2024
1 parent 6c928ce commit 5e72aee
Show file tree
Hide file tree
Showing 32 changed files with 1,037 additions and 1,339 deletions.
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<PropertyGroup Label="Nuget">
<Version>3.0.0-alpha.2</Version>
<Version>3.0.0-alpha.3</Version>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>tryAGI and contributors</Authors>
Expand Down
69 changes: 46 additions & 23 deletions src/libs/OpenAI.Generators/Generators/OpenAiFunctionsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ public class OpenAiFunctionsGenerator : IIncrementalGenerator

public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.SyntaxProvider
.ForAttributeWithMetadataName("tryAGI.OpenAI.OpenAiFunctionsAttribute")
.SelectManyAllAttributesOfCurrentInterfaceSyntax()
.SelectAndReportExceptions(PrepareData, context, Id)
.SelectAndReportExceptions(GetClientSourceCode, context, Id)
var attributes =
context.SyntaxProvider
.ForAttributeWithMetadataName("OpenAI.OpenAiFunctionsAttribute")
.SelectManyAllAttributesOfCurrentInterfaceSyntax()
.SelectAndReportExceptions(PrepareData, context, Id);

attributes
.SelectAndReportExceptions(AsTools, context, Id)
.AddSource(context);
attributes
.SelectAndReportExceptions(AsCalls, context, Id)
.AddSource(context);
}

Expand All @@ -48,14 +54,26 @@ private static InterfaceData PrepareData(
Description: GetDescription(x),
IsAsync: x.IsAsync || x.ReturnType.Name == "Task",
IsVoid: x.ReturnsVoid,
Parameters: x.Parameters
.Where(static x => x.Type.MetadataName != "CancellationToken")
.Select(static x => ToParameterData(
typeSymbol: x.Type,
name: x.Name,
description: GetDescription(x),
isRequired: !x.IsOptional))
.ToArray()))
Parameters: new SchemaData(
Name: x.Name,
Description: GetDescription(x),
Type: "object",
SchemaType: "object",
Properties:
x.Parameters
.Where(static x => x.Type.MetadataName != "CancellationToken")
.Select(static x => ToParameterData(
typeSymbol: x.Type,
name: x.Name,
description: GetDescription(x),
isRequired: !x.IsOptional))
.ToArray(),
EnumValues: Array.Empty<string>(),
IsNullable: false,
IsRequired: true,
Format: null,
ArrayItem: Array.Empty<SchemaData>(),
DefaultValue: string.Empty)))
.ToArray();

return new InterfaceData(
Expand All @@ -64,12 +82,12 @@ private static InterfaceData PrepareData(
Methods: methods);
}

private static ParameterData ToParameterData(ITypeSymbol typeSymbol, string? name = null, string? description = null, bool isRequired = true)
private static SchemaData ToParameterData(ITypeSymbol typeSymbol, string? name = null, string? description = null, bool isRequired = true)
{
string schemaType;
string? format = null;
var properties = Array.Empty<ParameterData>();
ParameterData? arrayItem = null;
var properties = Array.Empty<SchemaData>();
SchemaData? arrayItem = null;
switch (typeSymbol.TypeKind)
{
case TypeKind.Enum:
Expand Down Expand Up @@ -167,7 +185,7 @@ private static ParameterData ToParameterData(ITypeSymbol typeSymbol, string? nam
throw new NotImplementedException($"{typeSymbol.TypeKind} is not implemented.");
}

return new ParameterData(
return new SchemaData(
Name: !string.IsNullOrWhiteSpace(name)
? name!
: typeSymbol.Name,
Expand All @@ -179,9 +197,7 @@ private static ParameterData ToParameterData(ITypeSymbol typeSymbol, string? nam
SchemaType: schemaType,
Format: format,
Properties: properties,
ArrayItem: arrayItem != null
? new []{ arrayItem.Value }
: Array.Empty<ParameterData>(),
ArrayItem: arrayItem != null ? [arrayItem.Value] : Array.Empty<SchemaData>(),
EnumValues: typeSymbol.TypeKind == TypeKind.Enum
? typeSymbol
.GetMembers()
Expand Down Expand Up @@ -223,11 +239,18 @@ private static string GetDefaultValue(ITypeSymbol typeSymbol)
}
}

private static FileWithName GetClientSourceCode(InterfaceData @interface)
private static FileWithName AsTools(InterfaceData @interface)
{
return new FileWithName(
Name: $"{@interface.Name}.Tools.generated.cs",
Text: Sources.GenerateClientImplementation(@interface));
}

private static FileWithName AsCalls(InterfaceData @interface)
{
return new FileWithName(
Name: $"{@interface.Name}.Functions.generated.cs",
Text: SourceGenerationHelper.GenerateClientImplementation(@interface));
Name: $"{@interface.Name}.Calls.generated.cs",
Text: Sources.GenerateCalls(@interface));
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/libs/OpenAI.Generators/Models/MethodData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public readonly record struct MethodData(
string Description,
bool IsAsync,
bool IsVoid,
IReadOnlyCollection<ParameterData> Parameters);
SchemaData Parameters);
6 changes: 3 additions & 3 deletions src/libs/OpenAI.Generators/Models/ParameterData.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace H.Generators;

public readonly record struct ParameterData(
public readonly record struct SchemaData(
string Name,
string Description,
string Type,
string SchemaType,
string? Format,
IReadOnlyCollection<string> EnumValues,
IReadOnlyCollection<ParameterData> Properties,
IReadOnlyCollection<ParameterData> ArrayItem,
IReadOnlyCollection<SchemaData> Properties,
IReadOnlyCollection<SchemaData> ArrayItem,
bool IsRequired,
bool IsNullable,
string DefaultValue);
Loading

0 comments on commit 5e72aee

Please sign in to comment.