openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
achandmsft-patch-2

Branches

Tags

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

Clone

HTTPS

Download ZIP

codegen/generator/src/Visitors/ModelDirectoryVisitor.cs

32lines · modecode

1using Microsoft.TypeSpec.Generator.ClientModel;
2using Microsoft.TypeSpec.Generator.Providers;
3using System.IO;
4
5namespace OpenAILibraryPlugin.Visitors;
6
7/// <summary>
8/// A visitor that organizes model files into subdirectories based on their namespace.
9/// </summary>
10public class ModelDirectoryVisitor : ScmLibraryVisitor
11{
12 protected override TypeProvider VisitType(TypeProvider type)
13 {
14 // Only apply to types in the Models folder
15 if (type.RelativeFilePath.Contains("Models"))
16 {
17 var typeNamespace = type.Type.Namespace;
18 var segments = typeNamespace?.Split('.');
19
20 if (segments is { Length: >= 2 } && segments[0] == "OpenAI")
21 {
22 var folderName = segments[1]; // Use second segment, e.g., "Chat" from "OpenAI.Chat"
23 var fileName = Path.GetFileName(type.RelativeFilePath);
24 var newRelativePath = Path.Combine("src", "Generated", "Models", folderName, fileName);
25
26 type.Update(relativeFilePath: newRelativePath);
27 }
28 }
29
30 return type;
31 }
32}
33