Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;

namespace OpenAI;
Expand Down
22 changes: 13 additions & 9 deletions .scripts/Export-API.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" `
Expand All @@ -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 `
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions .scripts/Invoke-CodeGen.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
139 changes: 139 additions & 0 deletions .scripts/Run-Checks.ps1
Original file line number Diff line number Diff line change
@@ -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