/ src / settings-ui / Settings.UI.Library / AIServiceTypeExtensions.cs
AIServiceTypeExtensions.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  
 7  namespace Microsoft.PowerToys.Settings.UI.Library
 8  {
 9      public static class AIServiceTypeExtensions
10      {
11          /// <summary>
12          /// Convert a persisted string value into an <see cref="AIServiceType"/>.
13          /// Supports historical casing and aliases.
14          /// </summary>
15          public static AIServiceType ToAIServiceType(this string serviceType)
16          {
17              if (string.IsNullOrWhiteSpace(serviceType))
18              {
19                  return AIServiceType.OpenAI;
20              }
21  
22              var normalized = serviceType.Trim().ToLowerInvariant();
23              return normalized switch
24              {
25                  "openai" => AIServiceType.OpenAI,
26                  "azureopenai" or "azure" => AIServiceType.AzureOpenAI,
27                  "onnx" => AIServiceType.Onnx,
28                  "foundrylocal" or "foundry" or "fl" => AIServiceType.FoundryLocal,
29                  "ml" or "windowsml" or "winml" => AIServiceType.ML,
30                  "mistral" => AIServiceType.Mistral,
31                  "google" or "googleai" or "googlegemini" => AIServiceType.Google,
32                  "azureaiinference" or "azureinference" => AIServiceType.AzureAIInference,
33                  "ollama" => AIServiceType.Ollama,
34                  _ => AIServiceType.Unknown,
35              };
36          }
37  
38          /// <summary>
39          /// Convert an <see cref="AIServiceType"/> to the canonical string used for persistence.
40          /// </summary>
41          public static string ToConfigurationString(this AIServiceType serviceType)
42          {
43              return serviceType switch
44              {
45                  AIServiceType.OpenAI => "OpenAI",
46                  AIServiceType.AzureOpenAI => "AzureOpenAI",
47                  AIServiceType.Onnx => "Onnx",
48                  AIServiceType.FoundryLocal => "FoundryLocal",
49                  AIServiceType.ML => "ML",
50                  AIServiceType.Mistral => "Mistral",
51                  AIServiceType.Google => "Google",
52                  AIServiceType.AzureAIInference => "AzureAIInference",
53                  AIServiceType.Ollama => "Ollama",
54                  AIServiceType.Unknown => string.Empty,
55                  _ => throw new ArgumentOutOfRangeException(nameof(serviceType), serviceType, "Unsupported AI service type."),
56              };
57          }
58  
59          /// <summary>
60          /// Convert an <see cref="AIServiceType"/> into the normalized key used internally.
61          /// </summary>
62          public static string ToNormalizedKey(this AIServiceType serviceType)
63          {
64              return serviceType switch
65              {
66                  AIServiceType.OpenAI => "openai",
67                  AIServiceType.AzureOpenAI => "azureopenai",
68                  AIServiceType.Onnx => "onnx",
69                  AIServiceType.FoundryLocal => "foundrylocal",
70                  AIServiceType.ML => "ml",
71                  AIServiceType.Mistral => "mistral",
72                  AIServiceType.Google => "google",
73                  AIServiceType.AzureAIInference => "azureaiinference",
74                  AIServiceType.Ollama => "ollama",
75                  _ => string.Empty,
76              };
77          }
78      }
79  }