Skip to content

Commit

Permalink
refactor: Added OpenAI.Generators.Core.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jul 10, 2024
1 parent 2a0ba8f commit 40372c7
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 449 deletions.
7 changes: 7 additions & 0 deletions OpenAI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FixOpenApiSpec", "src\helpe
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "helpers", "helpers", "{1A008ECD-2300-4BE4-A302-49DDF8BE0D54}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.Generators.Core", "src\libs\OpenAI.Generators.Core\OpenAI.Generators.Core.csproj", "{FCBE699C-CCD3-4809-8128-78956BC3F2C3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -73,6 +75,10 @@ Global
{594DCFD8-E707-4232-B878-90FA3D880474}.Debug|Any CPU.Build.0 = Debug|Any CPU
{594DCFD8-E707-4232-B878-90FA3D880474}.Release|Any CPU.ActiveCfg = Release|Any CPU
{594DCFD8-E707-4232-B878-90FA3D880474}.Release|Any CPU.Build.0 = Release|Any CPU
{FCBE699C-CCD3-4809-8128-78956BC3F2C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FCBE699C-CCD3-4809-8128-78956BC3F2C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCBE699C-CCD3-4809-8128-78956BC3F2C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCBE699C-CCD3-4809-8128-78956BC3F2C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -85,6 +91,7 @@ Global
{4F534836-40A3-4E75-8B71-C46CD4FBE137} = {AAA11B78-2764-4520-A97E-46AA7089A588}
{74A51E6E-2955-4909-A075-13D8E620FBFE} = {61E7E11E-4558-434C-ACE8-06316A3097B3}
{594DCFD8-E707-4232-B878-90FA3D880474} = {1A008ECD-2300-4BE4-A302-49DDF8BE0D54}
{FCBE699C-CCD3-4809-8128-78956BC3F2C3} = {61E7E11E-4558-434C-ACE8-06316A3097B3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CED9A020-DBA5-4BE6-8096-75E528648EC1}
Expand Down
14 changes: 0 additions & 14 deletions src/libs/CodeAsOpenApi/Models/ParameterData.cs

This file was deleted.

56 changes: 0 additions & 56 deletions src/libs/CodeAsOpenApi/SourceGenerationHelper.cs

This file was deleted.

74 changes: 40 additions & 34 deletions ...CodeAsOpenApi/OpenAiFunctionsGenerator.cs → ...AI.Generators.Core/Conversion/ToModels.cs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
using System.ComponentModel;
using System.ComponentModel;
using H.Generators;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace H.Generators;
namespace OpenAI.Generators.Core.Conversion;

public class OpenAiFunctionsGenerator
public static class ToModels
{
#region Methods

private static string GetDescription(ISymbol symbol)
{
return symbol.GetAttributes()
.FirstOrDefault(static x => x.AttributeClass?.Name == nameof(DescriptionAttribute))?
.ConstructorArguments.First().Value?.ToString() ?? string.Empty;
}

private static InterfaceData PrepareData(
(SemanticModel SemanticModel, AttributeData AttributeData, InterfaceDeclarationSyntax InterfaceSyntax, INamedTypeSymbol InterfaceSymbol) tuple)
public static InterfaceData PrepareData(
this INamedTypeSymbol interfaceSymbol)
{
var (_, _, _, interfaceSymbol) = tuple;

interfaceSymbol = interfaceSymbol ?? throw new ArgumentNullException(nameof(interfaceSymbol));
var methods = interfaceSymbol
.GetMembers()
.OfType<IMethodSymbol>()
Expand All @@ -29,14 +20,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 OpenApiSchema(
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<OpenApiSchema>(),
DefaultValue: string.Empty)))
.ToArray();

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

private static ParameterData ToParameterData(ITypeSymbol typeSymbol, string? name = null, string? description = null, bool isRequired = true)
private static OpenApiSchema 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<OpenApiSchema>();
OpenApiSchema? arrayItem = null;
switch (typeSymbol.TypeKind)
{
case TypeKind.Enum:
Expand Down Expand Up @@ -148,7 +151,7 @@ private static ParameterData ToParameterData(ITypeSymbol typeSymbol, string? nam
throw new NotImplementedException($"{typeSymbol.TypeKind} is not implemented.");
}

return new ParameterData(
return new OpenApiSchema(
Name: !string.IsNullOrWhiteSpace(name)
? name!
: typeSymbol.Name,
Expand All @@ -160,16 +163,14 @@ 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<OpenApiSchema>(),
EnumValues: typeSymbol.TypeKind == TypeKind.Enum
? typeSymbol
.GetMembers()
.OfType<IFieldSymbol>()
.Select(static x => x.Name.ToLowerInvariant())
.ToArray()
: Array.Empty<string>(),
: [],
IsNullable: IsNullable(typeSymbol),
IsRequired: isRequired);
}
Expand Down Expand Up @@ -204,5 +205,10 @@ private static string GetDefaultValue(ITypeSymbol typeSymbol)
}
}

#endregion
private static string GetDescription(ISymbol symbol)
{
return symbol.GetAttributes()
.FirstOrDefault(static x => x.AttributeClass?.Name == nameof(DescriptionAttribute))?
.ConstructorArguments.First().Value?.ToString() ?? string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using OpenAI.Generators.Core;

namespace H.Generators;

public readonly record struct MethodData(
string Name,
string Description,
bool IsAsync,
bool IsVoid,
IReadOnlyCollection<ParameterData> Parameters);
OpenApiSchema Parameters);
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace H.Generators;
namespace OpenAI.Generators.Core;

public readonly record struct SchemaData(
public readonly record struct OpenApiSchema(
string Name,
string Description,
string Type,
string SchemaType,
string? Format,
IReadOnlyCollection<string> EnumValues,
IReadOnlyCollection<SchemaData> Properties,
IReadOnlyCollection<SchemaData> ArrayItem,
IReadOnlyCollection<OpenApiSchema> Properties,
IReadOnlyCollection<OpenApiSchema> ArrayItem,
bool IsRequired,
bool IsNullable,
string DefaultValue);
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net4.6.2;netstandard2.0;net6.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<NoWarn>$(NoWarn);CA1014;CA1031;CA1308</NoWarn>
<NoWarn>$(NoWarn);CA1308</NoWarn>
</PropertyGroup>

<ItemGroup Label="Global Usings">
<Using Include="System.Net.Http" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="IsExternalInit" Version="1.0.3">
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<Folder Include="Serialization\" />
</ItemGroup>

</Project>
Loading

0 comments on commit 40372c7

Please sign in to comment.