openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
scripts/Export-Api.ps1
297lines · modecode
| 1 | <# |
| 2 | .SYNOPSIS |
| 3 | Cross-platform API export script for OpenAI .NET SDK. |
| 4 | |
| 5 | .DESCRIPTION |
| 6 | This script supports Windows, macOS, and Linux environments. It automatically detects the current platform |
| 7 | and uses the appropriate paths for .NET and NuGet packages. |
| 8 | #> |
| 9 | |
| 10 | function Invoke-DotNetBuild { |
| 11 | param( |
| 12 | [Parameter(Mandatory = $true)] |
| 13 | [string]$ProjectPath |
| 14 | ) |
| 15 | |
| 16 | Write-Output "Building $($ProjectPath)..." |
| 17 | Write-Output "" |
| 18 | & dotnet build $ProjectPath |
| 19 | Write-Output "" |
| 20 | } |
| 21 | |
| 22 | function Invoke-GenAPI { |
| 23 | param ( |
| 24 | [Parameter(Mandatory = $true)] |
| 25 | [string]$TargetFramework, |
| 26 | |
| 27 | [Parameter(Mandatory = $true)] |
| 28 | [string]$AssemblyPath, |
| 29 | |
| 30 | [Parameter(Mandatory = $true)] |
| 31 | [string]$Destination |
| 32 | ) |
| 33 | |
| 34 | Write-Output "Generating $($Destination)..." |
| 35 | Write-Output "" |
| 36 | |
| 37 | Write-Output " Detected platform paths:" |
| 38 | Write-Output "" |
| 39 | |
| 40 | # Set platform-specific paths using PowerShell automatic variables (PowerShell 6.0+) |
| 41 | # Fall back to manual detection for older PowerShell versions |
| 42 | $isWindowsPlatform = $false |
| 43 | $isMacOSPlatform = $false |
| 44 | |
| 45 | if (Get-Variable -Name "IsWindows" -ErrorAction SilentlyContinue) { |
| 46 | $isWindowsPlatform = $IsWindows |
| 47 | $isMacOSPlatform = $IsMacOS |
| 48 | } else { |
| 49 | # Fallback for Windows PowerShell 5.1 and earlier |
| 50 | $isWindowsPlatform = [System.Environment]::OSVersion.Platform -eq [System.PlatformID]::Win32NT |
| 51 | $isMacOSPlatform = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::OSX) |
| 52 | } |
| 53 | |
| 54 | if ($isWindowsPlatform) { |
| 55 | $dotnetPacksPath = Join-Path $env:ProgramFiles "dotnet\packs\Microsoft.NETCore.App.Ref" |
| 56 | $nugetPackagesPath = Join-Path $env:UserProfile ".nuget\packages" |
| 57 | } elseif ($isMacOSPlatform) { |
| 58 | $dotnetPacksPath = "/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Ref" |
| 59 | $nugetPackagesPath = Join-Path $env:HOME ".nuget/packages" |
| 60 | } else { |
| 61 | # Linux or other Unix-like systems |
| 62 | $dotnetPacksPath = "/usr/share/dotnet/packs/Microsoft.NETCore.App.Ref" |
| 63 | $nugetPackagesPath = Join-Path $env:HOME ".nuget/packages" |
| 64 | } |
| 65 | |
| 66 | Write-Output " * .NET packs:" |
| 67 | Write-Output " $($dotnetPacksPath)" |
| 68 | Write-Output "" |
| 69 | Write-Output " * NuGet packages:" |
| 70 | Write-Output " $($nugetPackagesPath)" |
| 71 | Write-Output "" |
| 72 | |
| 73 | Write-Output " Assembly reference paths:" |
| 74 | Write-Output "" |
| 75 | |
| 76 | # .NET |
| 77 | $netRef = $null |
| 78 | if (Test-Path $dotnetPacksPath) { |
| 79 | $netRef = Get-ChildItem -Recurse ` |
| 80 | -Path $dotnetPacksPath ` |
| 81 | -Include "net8.0" | Select-Object -Last 1 |
| 82 | } |
| 83 | |
| 84 | # If not found in primary location, try alternative locations |
| 85 | if (-not $netRef) { |
| 86 | $alternativePaths = @() |
| 87 | if ($isWindowsPlatform) { |
| 88 | $alternativePaths += "${env:ProgramFiles(x86)}\dotnet\packs\Microsoft.NETCore.App.Ref" |
| 89 | } elseif ($isMacOSPlatform) { |
| 90 | $alternativePaths += "/usr/local/share/dotnet/packs/Microsoft.NETCore.App.Ref" |
| 91 | $alternativePaths += "/Library/Frameworks/Microsoft.NETCore.App.Ref" |
| 92 | } else { |
| 93 | $alternativePaths += "/usr/lib/dotnet/packs/Microsoft.NETCore.App.Ref" |
| 94 | $alternativePaths += "/opt/dotnet/packs/Microsoft.NETCore.App.Ref" |
| 95 | } |
| 96 | |
| 97 | foreach ($altPath in $alternativePaths) { |
| 98 | if (Test-Path $altPath) { |
| 99 | $netRef = Get-ChildItem -Recurse -Path $altPath -Include "net8.0" | Select-Object -Last 1 |
| 100 | if ($netRef) { break } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | Write-Output " * .NET:" |
| 106 | Write-Output " $($netRef)" |
| 107 | Write-Output "" |
| 108 | |
| 109 | # System.ClientModel |
| 110 | $systemClientModelPath = Join-Path $nugetPackagesPath "system.clientmodel\1.4.2" |
| 111 | $systemClientModelRef = $null |
| 112 | if (Test-Path $systemClientModelPath) { |
| 113 | $systemClientModelRef = Get-ChildItem ` |
| 114 | -Path $systemClientModelPath ` |
| 115 | -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net8.0") ` |
| 116 | -Recurse | |
| 117 | Select-Object -Last 1 |
| 118 | } |
| 119 | |
| 120 | Write-Output " * System.ClientModel:" |
| 121 | Write-Output " $($systemClientModelRef)" |
| 122 | Write-Output "" |
| 123 | |
| 124 | # Microsoft.Extensions.Logging.Abstractions |
| 125 | $microsoftExtensionsLoggingAbstractionsPath = Join-Path $nugetPackagesPath "microsoft.extensions.logging.abstractions\8.0.3" |
| 126 | $microsoftExtensionsLoggingAbstractionsRef = $null |
| 127 | if (Test-Path $microsoftExtensionsLoggingAbstractionsPath) { |
| 128 | $microsoftExtensionsLoggingAbstractionsRef = Get-ChildItem ` |
| 129 | -Path $microsoftExtensionsLoggingAbstractionsPath ` |
| 130 | -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net8.0") ` |
| 131 | -Recurse | |
| 132 | Select-Object -Last 1 |
| 133 | } |
| 134 | |
| 135 | Write-Output " * Microsoft.Extensions.Logging.Abstractions:" |
| 136 | Write-Output " $($microsoftExtensionsLoggingAbstractionsRef)" |
| 137 | Write-Output "" |
| 138 | |
| 139 | # Microsoft.Extensions.DependencyInjection.Abstractions |
| 140 | $microsoftExtensionsDependencyInjectionAbstractionsPath = Join-Path $nugetPackagesPath "microsoft.extensions.dependencyinjection.abstractions\8.0.2" |
| 141 | $microsoftExtensionsDependencyInjectionAbstractionsRef = $null |
| 142 | if (Test-Path $microsoftExtensionsDependencyInjectionAbstractionsPath) { |
| 143 | $microsoftExtensionsDependencyInjectionAbstractionsRef = Get-ChildItem ` |
| 144 | -Path $microsoftExtensionsDependencyInjectionAbstractionsPath ` |
| 145 | -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net8.0") ` |
| 146 | -Recurse | |
| 147 | Select-Object -Last 1 |
| 148 | } |
| 149 | |
| 150 | Write-Output " * Microsoft.Extensions.DependencyInjection.Abstractions:" |
| 151 | Write-Output " $($microsoftExtensionsDependencyInjectionAbstractionsRef)" |
| 152 | Write-Output "" |
| 153 | |
| 154 | # System.Memory.Data |
| 155 | $systemMemoryDataPath = Join-Path $nugetPackagesPath "system.memory.data\6.0.1" |
| 156 | $systemMemoryDataRef = $null |
| 157 | if (Test-Path $systemMemoryDataPath) { |
| 158 | $systemMemoryDataRef = Get-ChildItem ` |
| 159 | -Path $systemMemoryDataPath ` |
| 160 | -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net6.0") ` |
| 161 | -Recurse | |
| 162 | Select-Object -Last 1 |
| 163 | } |
| 164 | |
| 165 | Write-Output " * System.Memory.Data:" |
| 166 | Write-Output " $($systemMemoryDataRef)" |
| 167 | Write-Output "" |
| 168 | |
| 169 | if ($TargetFramework -eq "netstandard2.0") { |
| 170 | # System.Diagnostics.DiagnosticSource |
| 171 | $systemDiagnosticsDiagnosticSourcePath = Join-Path $nugetPackagesPath "system.diagnostics.diagnosticsource\6.0.1" |
| 172 | $systemDiagnosticsDiagnosticSourceRef = $null |
| 173 | if (Test-Path $systemDiagnosticsDiagnosticSourcePath) { |
| 174 | $systemDiagnosticsDiagnosticSourceRef = Get-ChildItem ` |
| 175 | -Path $systemDiagnosticsDiagnosticSourcePath ` |
| 176 | -Include $(($TargetFramework -eq "netstandard2.0") ? "netstandard2.0" : "net5.0") ` |
| 177 | -Recurse | |
| 178 | Select-Object -Last 1 |
| 179 | } |
| 180 | |
| 181 | Write-Output " * System.Diagnostics.DiagnosticSource:" |
| 182 | Write-Output " $($systemDiagnosticsDiagnosticSourceRef)" |
| 183 | Write-Output "" |
| 184 | |
| 185 | # Microsoft.Bcl.AsyncInterfaces |
| 186 | $microsoftBclAsyncInterfacesPath = Join-Path $nugetPackagesPath "microsoft.bcl.asyncinterfaces\1.1.0" |
| 187 | $microsoftBclAsyncInterfacesRef = $null |
| 188 | if (Test-Path $microsoftBclAsyncInterfacesPath) { |
| 189 | $microsoftBclAsyncInterfacesRef = Get-ChildItem ` |
| 190 | -Path $microsoftBclAsyncInterfacesPath ` |
| 191 | -Include "netstandard2.0" ` |
| 192 | -Recurse | |
| 193 | Select-Object -Last 1 |
| 194 | } |
| 195 | |
| 196 | Write-Output " * Microsoft.Bcl.AsyncInterfaces:" |
| 197 | Write-Output " $($microsoftBclAsyncInterfacesRef)" |
| 198 | Write-Output "" |
| 199 | } |
| 200 | |
| 201 | Write-Output " NOTE: If any of the above are empty, tool output may be inaccurate." |
| 202 | Write-Output "" |
| 203 | |
| 204 | # Build genapi command arguments, excluding null references |
| 205 | $genapiArgs = @( |
| 206 | "--assembly", $AssemblyPath |
| 207 | "--output-path", $Destination |
| 208 | ) |
| 209 | |
| 210 | if ($netRef) { $genapiArgs += @("--assembly-reference", $netRef) } |
| 211 | if ($systemClientModelRef) { $genapiArgs += @("--assembly-reference", $systemClientModelRef) } |
| 212 | if ($microsoftExtensionsLoggingAbstractionsRef) { $genapiArgs += @("--assembly-reference", $microsoftExtensionsLoggingAbstractionsRef) } |
| 213 | if ($microsoftExtensionsDependencyInjectionAbstractionsRef) { $genapiArgs += @("--assembly-reference", $microsoftExtensionsDependencyInjectionAbstractionsRef) } |
| 214 | if ($systemMemoryDataRef) { $genapiArgs += @("--assembly-reference", $systemMemoryDataRef) } |
| 215 | if ($systemDiagnosticsDiagnosticSourceRef) { $genapiArgs += @("--assembly-reference", $systemDiagnosticsDiagnosticSourceRef) } |
| 216 | if ($microsoftBclAsyncInterfacesRef) { $genapiArgs += @("--assembly-reference", $microsoftBclAsyncInterfacesRef) } |
| 217 | |
| 218 | & genapi @genapiArgs |
| 219 | |
| 220 | Write-Output "Cleaning up $($Destination)..." |
| 221 | Write-Output "" |
| 222 | |
| 223 | $content = Get-Content $Destination -Raw |
| 224 | |
| 225 | # Remove empty lines. |
| 226 | $content = $content -creplace '//.*\r?\n', '' |
| 227 | $content = $content -creplace '\r?\n\r?\n', "`n" |
| 228 | $content = $content -creplace '\r?\n *{', " {" |
| 229 | |
| 230 | # Remove fully-qualified names. |
| 231 | $content = $content -creplace "System\.ComponentModel\.", "" |
| 232 | $content = $content -creplace "System\.ClientModel.Primitives\.", "" |
| 233 | $content = $content -creplace "System\.ClientModel\.", "" |
| 234 | $content = $content -creplace "System\.Collections\.Generic\.", "" |
| 235 | $content = $content -creplace "System\.Collections\.", "" |
| 236 | $content = $content -creplace "System\.Threading.Tasks\.", "" |
| 237 | $content = $content -creplace "System\.Threading\.", "" |
| 238 | $content = $content -creplace "System\.Text.Json\.", "" |
| 239 | $content = $content -creplace "System\.Text\.", "" |
| 240 | $content = $content -creplace "System\.IO\.", "" |
| 241 | $content = $content -creplace "System\.", "" |
| 242 | $content = $content -creplace "Assistants\.", "" |
| 243 | $content = $content -creplace "Audio\.", "" |
| 244 | $content = $content -creplace "Batch\.", "" |
| 245 | $content = $content -creplace "Chat\.", "" |
| 246 | $content = $content -creplace "Common\.", "" |
| 247 | $content = $content -creplace "Containers\.", "" |
| 248 | $content = $content -creplace "Embeddings\.", "" |
| 249 | $content = $content -creplace "Evals\.", "" |
| 250 | $content = $content -creplace "Files\.", "" |
| 251 | $content = $content -creplace "FineTuning\.", "" |
| 252 | $content = $content -creplace "Graders\.", "" |
| 253 | $content = $content -creplace "Images\.", "" |
| 254 | $content = $content -creplace "Models\.", "" |
| 255 | $content = $content -creplace "Moderations\.", "" |
| 256 | $content = $content -creplace "Realtime\.", "" |
| 257 | $content = $content -creplace "Responses\.", "" |
| 258 | $content = $content -creplace "VectorStores\.", "" |
| 259 | |
| 260 | # Remove Diagnostics.DebuggerStepThrough attribute. |
| 261 | $content = $content -creplace ".*Diagnostics.DebuggerStepThrough.*\n", "" |
| 262 | |
| 263 | # Remove ModelReaderWriterBuildable attributes. |
| 264 | $content = $content -creplace '\[ModelReaderWriterBuildable\(typeof\([^\)]+\)\)\]\s*', '' |
| 265 | |
| 266 | # Remove internal APIs. |
| 267 | $content = $content -creplace " * internal.*`n", "" |
| 268 | |
| 269 | # Remove IJsonModel/IPersistableModel interface method entries. |
| 270 | $content = $content -creplace " .*(IJsonModel|IPersistableModel).*`n", "" |
| 271 | # $content = $content -creplace " protected (virtual|override) .* (Json|Persistable)Model(Create|Write)Core.*`n", "" |
| 272 | |
| 273 | # Other cosmetic simplifications. |
| 274 | $content = $content -creplace "partial class", "class" |
| 275 | $content = $content -creplace ".*private.*dummy.*`n", "" |
| 276 | $content = $content -creplace " { throw null; }", ";" |
| 277 | $content = $content -creplace " { }", ";" |
| 278 | $content = $content -creplace "Diagnostics.CodeAnalysis.Experimental", "Experimental" |
| 279 | $content = $content -creplace "Diagnostics.CodeAnalysis.SetsRequiredMembers", "SetsRequiredMembers" |
| 280 | |
| 281 | Set-Content -Path $Destination -Value $content -NoNewline |
| 282 | } |
| 283 | |
| 284 | $repoRootPath = Join-Path $PSScriptRoot .. -Resolve |
| 285 | $projectPath = Join-Path $repoRootPath "src\OpenAI.csproj" |
| 286 | |
| 287 | Invoke-DotNetBuild -ProjectPath $projectPath |
| 288 | |
| 289 | $targetFramework = "netstandard2.0" |
| 290 | $assemblyPath = Join-Path $repoRootPath "src\bin\Debug\$($targetFramework)\OpenAI.dll" |
| 291 | $destination = Join-Path $repoRootPath "api\OpenAI.$($targetFramework).cs" |
| 292 | Invoke-GenAPI -TargetFramework $targetFramework -AssemblyPath $assemblyPath -Destination $destination |
| 293 | |
| 294 | $targetFramework = "net8.0" |
| 295 | $assemblyPath = Join-Path $repoRootPath "src\bin\Debug\$($targetFramework)\OpenAI.dll" |
| 296 | $destination = Join-Path $repoRootPath "api\OpenAI.$($targetFramework).cs" |
| 297 | Invoke-GenAPI -TargetFramework $targetFramework -AssemblyPath $assemblyPath -Destination $destination |