From 2fcbcb7a4bd86804140ce3c687c8df49208424ff Mon Sep 17 00:00:00 2001 From: Jose Arriaga Maldonado Date: Sat, 3 Aug 2024 23:06:41 -0700 Subject: [PATCH 1/2] Improve Export-API console output --- .scripts/Export-API.ps1 | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/.scripts/Export-API.ps1 b/.scripts/Export-API.ps1 index c9939b2dc..b9129d003 100644 --- a/.scripts/Export-API.ps1 +++ b/.scripts/Export-API.ps1 @@ -7,11 +7,14 @@ $projectPath = Join-Path $sourceFolder OpenAI.csproj $assemblyPath = Join-Path $sourceFolder bin\Debug $platformTarget OpenAI.dll $outputPath = Join-Path $apiFolder "OpenAI.$($platformTarget).cs" -Write-Output "Building OpenAI.dll" +Write-Output "Building OpenAI.dll..." +Write-Output "" dotnet build $projectPath +Write-Output "" -Write-Output "Generating OpenAI.netstandard2.0.cs" +Write-Output "Generating OpenAI.netstandard2.0.cs..." +Write-Output "" $net80ref = Get-ChildItem -Recurse ` -Path "$($env:ProgramFiles)\dotnet\packs\Microsoft.NETCore.App.Ref" ` @@ -26,12 +29,12 @@ $microsoftBclAsyncinterfacesRef = Get-ChildItem -Recurse ` -Path "$($env:UserProfile)\.nuget\packages\microsoft.bcl.asyncinterfaces\1.1.0" ` -Include "netstandard2.0" | Select-Object -Last 1 -Write-Output "" -Write-Output "Using NETCore assembly path: $($net80ref)" -Write-Output "Using System.Memory.Data assembly path: $($systemMemoryDataRef)" -Write-Output "Using System.ClientModel assembly path: $($systemClientmodelRef)" -Write-Output "Using Microsoft.Bcl.AsyncInterfaces assembly path: $($microsoftBclAsyncinterfacesRef)" -Write-Output "Note: if any of the above are empty, tool output may be inaccurate." +Write-Output "Assembly reference paths:" +Write-Output "* NETCore: $($net80ref)" +Write-Output "* System.Memory.Data: $($systemMemoryDataRef)" +Write-Output "* System.ClientModel: $($systemClientmodelRef)" +Write-Output "* Microsoft.Bcl.AsyncInterfaces: $($microsoftBclAsyncinterfacesRef)" +Write-Output "NOTE: if any of the above are empty, tool output may be inaccurate." Write-Output "" genapi --assembly $assemblyPath --output-path $outputPath ` @@ -40,7 +43,8 @@ genapi --assembly $assemblyPath --output-path $outputPath ` --assembly-reference $systemClientmodelRef ` --assembly-reference $microsoftBclAsyncinterfacesRef -Write-Output "Cleaning up OpenAI.netstandard2.0.cs" +Write-Output "Cleaning up OpenAI.netstandard2.0.cs..." +Write-Output "" $content = Get-Content $outputPath -Raw From 4e14b006d9f2de16cd6a6d60090f0e05f0e9ab38 Mon Sep 17 00:00:00 2001 From: Jose Arriaga Maldonado Date: Mon, 5 Aug 2024 12:03:41 -0700 Subject: [PATCH 2/2] Add Run-Checks.ps1 script --- .../CancellationTokenExtensions.cs | 2 + .../IInternalListResponseOfT.cs} | 1 - .../InternalFunctionDefinition.cs | 0 .scripts/Invoke-CodeGen.ps1 | 1 + .scripts/Run-Checks.ps1 | 139 ++++++++++++++++++ 5 files changed, 142 insertions(+), 1 deletion(-) rename .dotnet/src/Custom/{Common => Internal}/CancellationTokenExtensions.cs (97%) rename .dotnet/src/Custom/{Common/GeneratorStubs.cs => Internal/IInternalListResponseOfT.cs} (93%) rename .dotnet/src/Custom/{Common => Internal}/InternalFunctionDefinition.cs (100%) create mode 100644 .scripts/Run-Checks.ps1 diff --git a/.dotnet/src/Custom/Common/CancellationTokenExtensions.cs b/.dotnet/src/Custom/Internal/CancellationTokenExtensions.cs similarity index 97% rename from .dotnet/src/Custom/Common/CancellationTokenExtensions.cs rename to .dotnet/src/Custom/Internal/CancellationTokenExtensions.cs index 500a1fdc8..d0a5bc81f 100644 --- a/.dotnet/src/Custom/Common/CancellationTokenExtensions.cs +++ b/.dotnet/src/Custom/Internal/CancellationTokenExtensions.cs @@ -1,6 +1,8 @@ using System.ClientModel.Primitives; using System.Threading; +namespace OpenAI; + internal static class CancellationTokenExtensions { public static RequestOptions ToRequestOptions(this CancellationToken cancellationToken, bool streaming = false) diff --git a/.dotnet/src/Custom/Common/GeneratorStubs.cs b/.dotnet/src/Custom/Internal/IInternalListResponseOfT.cs similarity index 93% rename from .dotnet/src/Custom/Common/GeneratorStubs.cs rename to .dotnet/src/Custom/Internal/IInternalListResponseOfT.cs index dde4243c2..bc02a852a 100644 --- a/.dotnet/src/Custom/Common/GeneratorStubs.cs +++ b/.dotnet/src/Custom/Internal/IInternalListResponseOfT.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; namespace OpenAI; diff --git a/.dotnet/src/Custom/Common/InternalFunctionDefinition.cs b/.dotnet/src/Custom/Internal/InternalFunctionDefinition.cs similarity index 100% rename from .dotnet/src/Custom/Common/InternalFunctionDefinition.cs rename to .dotnet/src/Custom/Internal/InternalFunctionDefinition.cs diff --git a/.scripts/Invoke-CodeGen.ps1 b/.scripts/Invoke-CodeGen.ps1 index 36c6f0489..a398b4ae3 100644 --- a/.scripts/Invoke-CodeGen.ps1 +++ b/.scripts/Invoke-CodeGen.ps1 @@ -14,6 +14,7 @@ try { Invoke { npm exec --no -- tsp compile . } Invoke { .$PSScriptRoot\Update-ClientModel.ps1 } Invoke { .$PSScriptRoot\Edit-Serialization.ps1 } + Invoke { .$PSScriptRoot\Run-Checks.ps1 } } finally { Pop-Location diff --git a/.scripts/Run-Checks.ps1 b/.scripts/Run-Checks.ps1 new file mode 100644 index 000000000..2f75a1995 --- /dev/null +++ b/.scripts/Run-Checks.ps1 @@ -0,0 +1,139 @@ +function Run-ModelsSubnamespaceCheck { + Write-Output "" + Write-Output "Checking for unknown files using the OpenAI.Models namespace..." + + $root = Split-Path $PSScriptRoot -Parent + $directory = Join-Path -Path $root -ChildPath ".dotnet\src" + $files = Get-ChildItem -Path $($directory + "\*") -Include "*.cs" -Recurse + + $exclusions = @( + "GeneratorStubs.cs", + "InternalDeleteModelResponse.cs", + "InternalDeleteModelResponse.Serialization.cs", + "InternalDeleteModelResponseObject.cs", + "InternalListModelsResponseObject.cs", + "InternalModelObject.cs", + "ModelClient.cs", + "ModelClient.Protocol.cs", + "OpenAIModelInfo.cs", + "OpenAIModelInfo.Serialization.cs", + "OpenAIModelInfoCollection.cs", + "OpenAIModelInfoCollection.Serialization.cs" + ) + + $failures = @() + + foreach ($file in $files) { + $content = Get-Content -Path $file -Raw + + if ($file.Name -in $exclusions) { + Write-Output "Skipped $($file.FullName)" + continue + } + + if ($content -cmatch '(?m)^namespace OpenAI\.Models(;)?(\r)?$') { + $failures += $file + } + } + + if ($failures.Length -gt 0) { + $message = "" + + foreach ($failure in $failures) { + $message += "$failure " + } + + $exception = ("One or more unknown files with the OpenAI.Models namespace were detected." + + " The OpenAI.Models namespace is reserved for OpenAI's Models API." + + " If this change is intentional, please add the filenames to the exclusion list:" + + " $($message)") + + throw $exception + } + + Write-Output "" + Write-Output "The check was successful." +} + +function Run-TopLevelNamespaceCheck { + Write-Output "" + Write-Output "Checking for unknown files using the OpenAI namespace..." + + $root = Split-Path $PSScriptRoot -Parent + $directory = Join-Path -Path $root -ChildPath ".dotnet\src" + $files = Get-ChildItem -Path $($directory + "\*") -Include "*.cs" -Recurse + + $exclusions = @( + # Public types + "ListOrder.cs", + "OpenAIClient.cs", + "OpenAIClientOptions.cs", + "OpenAIModelFactory.cs", + + # Internal types + "Argument.cs", + "BinaryContentHelper.cs", + "CancellationTokenExtensions.cs", + "ChangeTrackingDictionary.cs", + "ChangeTrackingList.cs", + "ClientPipelineExtensions.cs", + "ClientUriBuilder.cs", + "ErrorResult.cs", + "IInternalListResponseOfT.cs", + "InternalFunctionDefinition.cs", + "InternalFunctionDefinition.Serialization.cs", + "ModelSerializationExtensions.cs", + "Optional.cs", + "TelemetryDetails.cs", + "Utf8JsonBinaryContent.cs", + + # Utilities + "CodeGenClientAttribute.cs", + "CodeGenMemberAttribute.cs", + "CodeGenModelAttribute.cs", + "CodeGenSerializationAttribute.cs", + "CodeGenSuppressAttribute.cs", + "CodeGenTypeAttribute.cs", + "CustomSerializationHelpers.cs", + "GenericActionPipelinePolicy.cs", + "MultipartFormDataBinaryContent.cs", + "PageCollectionHelpers.cs", + "PageEnumerator.cs", + "PageResultEnumerator.cs" + ) + + $failures = @() + + foreach ($file in $files) { + $content = Get-Content -Path $file -Raw + + if ($file.Name -in $exclusions) { + Write-Output "Skipped $($file.FullName)" + continue + } + + if ($content -cmatch '(?m)^namespace OpenAI(;)?(\r)?$') { + $failures += $file + } + } + + if ($failures.Length -gt 0) { + $message = "" + + foreach ($failure in $failures) { + $message += "$failure " + } + + $exception = ("One or more unknown files with the OpenAI namespace were detected." + + " If this change is intentional, please add the filenames to the exclusion list:\:" + + " $($message)") + + throw $exception + } + + Write-Output "" + Write-Output "The check was successful." +} + +Run-ModelsSubnamespaceCheck +Run-TopLevelNamespaceCheck \ No newline at end of file