openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.9.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/Export-Api.ps1

169lines · modecode

1<#
2.SYNOPSIS
3 Generates the public API surface for the OpenAI .NET library using GenAPI.
4
5.DESCRIPTION
6 This script invokes the MSBuild GenerateApi target to produce C# source files
7 representing the public API contract of the OpenAI library. The output files
8 are placed in the 'api' folder at the repository root.
9
10.EXAMPLE
11 .\Export-Api.ps1
12 Generates API for all target frameworks defined in
13 ClientTargetFrameworks (Directory.Build.props) using the Release configuration.
14
15.NOTES
16 Outputs are written to api/OpenAI.<TargetFramework>.cs
17#>
18
19[CmdletBinding()]
20param(
21)
22
23$ErrorActionPreference = "Stop"
24
25$configuration = "Release"
26
27# Resolve paths
28$repoRootPath = Join-Path $PSScriptRoot ".." -Resolve
29$projectPath = Join-Path $repoRootPath "src" "OpenAI.csproj"
30$outputDirectory = Join-Path $repoRootPath "api"
31
32# Get ClientTargetFrameworks from Directory.Build.props
33$propsPath = Join-Path $repoRootPath "Directory.Build.props"
34$clientTargetFrameworks = ""
35if (Test-Path $propsPath) {
36 $propsContent = Get-Content $propsPath -Raw
37 if ($propsContent -match '<ClientTargetFrameworks>([^<]+)</ClientTargetFrameworks>') {
38 $clientTargetFrameworks = $Matches[1]
39 }
40}
41
42if (-not $clientTargetFrameworks) {
43 Write-Error "Could not find ClientTargetFrameworks in Directory.Build.props"
44 exit 1
45}
46
47Write-Host ""
48Write-Host "Target Frameworks: $clientTargetFrameworks" -ForegroundColor Green
49Write-Host "Configuration: $configuration"
50Write-Host ""
51
52# Ensure output directory exists and is clean
53if (Test-Path $outputDirectory) {
54 Write-Host "Cleaning existing output directory..." -ForegroundColor Cyan
55 try {
56 Get-ChildItem -Path $outputDirectory -Force | Remove-Item -Recurse -Force
57 }
58 catch {
59 Write-Warning "Failed to clean some items in output directory: $_"
60 }
61} else {
62 New-Item -ItemType Directory -Path $outputDirectory -Force | Out-Null
63 Write-Host "Created output directory: $outputDirectory"
64}
65
66# Build the dotnet command arguments
67$buildArgs = @(
68 "build"
69 $projectPath
70 "-t:ExportApi"
71 "-c:$configuration"
72 "-p:ExportingApi=true"
73)
74
75Write-Host "Output Directory: $outputDirectory"
76Write-Host ""
77Write-Host "Running GenAPI for all target frameworks..." -ForegroundColor Cyan
78Write-Host ""
79
80# Run a single build command - the MSBuild target handles all frameworks
81& dotnet @buildArgs
82if ($LASTEXITCODE -ne 0) {
83 Write-Error "GenAPI failed with exit code $LASTEXITCODE"
84 exit $LASTEXITCODE
85}
86
87Write-Host ""
88Write-Host "Cleaning up generated files..." -ForegroundColor Cyan
89
90# Clean up each generated file
91Get-ChildItem -Path $outputDirectory -Filter "OpenAI.*.cs" | ForEach-Object {
92 Write-Host " Cleaning $($_.Name)..."
93
94 $content = Get-Content $_.FullName -Raw
95
96 # Normalize line breaks and whitespace.
97 $content = $content -creplace '\r?\n\r?\n', "`n"
98 $content = $content -creplace '\r?\n *{', " {"
99
100 # Remove fully-qualified namespace prefixes.
101 @(
102 "Diagnostics\.CodeAnalysis\.",
103 "System\.ComponentModel\.",
104 "System\.ClientModel\.Primitives\.",
105 "System\.ClientModel\.",
106 "System\.Collections\.Generic\.",
107 "System\.Collections\.",
108 "System\.Threading\.Tasks\.",
109 "System\.Threading\.",
110 "System\.Text\.Json\.",
111 "System\.Text\.",
112 "System\.IO\.",
113 "System\." # System must be last to avoid partial matches
114 ) | ForEach-Object { $content = $content -creplace $_, "" }
115
116 # Remove OpenAI sub-namespace prefixes.
117 @(
118 "Assistants",
119 "Audio",
120 "Batch",
121 "Chat",
122 "Common",
123 "Containers",
124 "Conversations",
125 "Embeddings",
126 "Evals",
127 "Files",
128 "FineTuning",
129 "Graders",
130 "Images",
131 "Models",
132 "Moderations",
133 "Realtime",
134 "Responses",
135 "VectorStores",
136 "Videos"
137 ) | ForEach-Object { $content = $content -creplace "$_\.", "" }
138
139 # Remove non-public APIs.
140 $content = $content -creplace " * internal.*`n", ""
141 $content = $content -creplace ".*private.*dummy.*`n", ""
142
143 # Remove Diagnostics.DebuggerStepThrough attribute.
144 $content = $content -creplace ".*Diagnostics.DebuggerStepThrough.*\n", ""
145
146 # Remove ModelReaderWriterBuildable attributes.
147 $content = $content -creplace '\[ModelReaderWriterBuildable\(typeof\([^\)]+\)\)\]\s*', ''
148
149 # Remove IJsonModel/IPersistableModel interface method entries.
150 $content = $content -creplace " .*(IJsonModel|IPersistableModel).*`n", ""
151
152 # Other cosmetic simplifications.
153 $content = $content -creplace "partial class", "class"
154 $content = $content -creplace " { throw null; }", ";"
155 $content = $content -creplace " { }", ";"
156
157 Set-Content -Path $_.FullName -Value $content -NoNewline
158}
159
160Write-Host ""
161Write-Host "API generation completed successfully." -ForegroundColor Green
162Write-Host ""
163
164# List generated files
165Write-Host "Generated files:" -ForegroundColor Cyan
166Get-ChildItem -Path $outputDirectory -Filter "OpenAI.*.cs" | ForEach-Object {
167 Write-Host " - $($_.Name)"
168}
169Write-Host ""
170