openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
mailinhphan/clientOptions

Branches

Tags

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

Clone

HTTPS

Download ZIP

codegen/generator/src/Visitors/ModelDirectoryVisitor.cs

40lines · modecode

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