ApiClientFactory.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace LLM.Editor.Client
  4. {
  5. /// <summary>
  6. /// Responsible for creating and providing the correct instance of an ILlmApiClient
  7. /// based on the project's settings (real or dummy).
  8. /// </summary>
  9. public static class ApiClientFactory
  10. {
  11. private static ILlmApiClient _clientInstance;
  12. private static Settings.MCPSettings _settings;
  13. public static ILlmApiClient GetClient()
  14. {
  15. if (_clientInstance == null)
  16. {
  17. LoadSettings();
  18. _clientInstance = _settings.useDummyClient ? new DummyApiClient() : new GeminiApiClient();
  19. }
  20. return _clientInstance;
  21. }
  22. private static void LoadSettings()
  23. {
  24. if (_settings) return;
  25. var guids = AssetDatabase.FindAssets("t:MCPSettings");
  26. if (guids.Length == 0)
  27. {
  28. Debug.LogError("Could not find MCPSettings asset. Please create one.");
  29. return;
  30. }
  31. var path = AssetDatabase.GUIDToAssetPath(guids[0]);
  32. _settings = AssetDatabase.LoadAssetAtPath<Settings.MCPSettings>(path);
  33. }
  34. // Call this if settings are changed to force the factory to create a new client on next request.
  35. public static void InvalidateClient()
  36. {
  37. _clientInstance = null;
  38. }
  39. }
  40. }