ApiClientFactory.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. public static ILlmApiClient GetClient()
  13. {
  14. if (_clientInstance == null)
  15. {
  16. var settings = LoadSettings();
  17. if (settings == null) return null;
  18. _clientInstance = settings.useDummyClient ? new DummyApiClient() : new GeminiApiClient();
  19. // Initialize the client after creating it.
  20. // We don't need to wait for the result here, but we fire it off.
  21. _ = _clientInstance.Initialize();
  22. }
  23. return _clientInstance;
  24. }
  25. private static Settings.MCPSettings LoadSettings()
  26. {
  27. var guids = AssetDatabase.FindAssets("t:MCPSettings");
  28. if (guids.Length == 0)
  29. {
  30. Debug.LogError("Could not find MCPSettings asset. Please create one.");
  31. return null;
  32. }
  33. var path = AssetDatabase.GUIDToAssetPath(guids[0]);
  34. return AssetDatabase.LoadAssetAtPath<Settings.MCPSettings>(path);
  35. }
  36. // Call this if settings are changed to force the factory to create a new client on next request.
  37. public static void InvalidateClient()
  38. {
  39. _clientInstance = null;
  40. }
  41. }
  42. }