DummyApiClient.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using LLM.Editor.Core;
  5. using LLM.Editor.Data;
  6. using System.Threading;
  7. using LLM.Editor.Commands;
  8. using System.Threading.Tasks;
  9. using System.Collections.Generic;
  10. using LLM.Editor.Tests.Integration;
  11. namespace LLM.Editor.Client
  12. {
  13. /// <summary>
  14. /// A mock client that simulates an LLM response for testing purposes.
  15. /// It now discovers and runs test cases from C# classes that implement IDummyTestCase.
  16. /// </summary>
  17. public class DummyApiClient : ILlmApiClient
  18. {
  19. private static List<IDummyTestCase> _testCases;
  20. private static bool _testCasesLoaded;
  21. private IDummyTestCase _activeTestCase;
  22. private int _activeResponseStep;
  23. public static List<IDummyTestCase> GetTestCases()
  24. {
  25. if (!_testCasesLoaded)
  26. {
  27. DiscoverTestCases();
  28. }
  29. return _testCases;
  30. }
  31. private static void DiscoverTestCases()
  32. {
  33. _testCases = new List<IDummyTestCase>();
  34. var testCaseType = typeof(IDummyTestCase);
  35. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  36. foreach (var assembly in assemblies)
  37. {
  38. try
  39. {
  40. var types = assembly.GetTypes();
  41. foreach (var type in types)
  42. {
  43. if (testCaseType.IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
  44. {
  45. var instance = (IDummyTestCase)Activator.CreateInstance(type);
  46. _testCases.Add(instance);
  47. }
  48. }
  49. }
  50. catch (Exception e)
  51. {
  52. Debug.LogWarning($"[DummyApiClient] Could not inspect assembly {assembly.FullName} for test cases: {e.Message}");
  53. }
  54. }
  55. _testCases = _testCases.OrderBy(tc => tc.TriggerPhrase).ToList();
  56. _testCasesLoaded = true;
  57. Debug.Log($"[DummyApiClient] Discovered {_testCases.Count} C# test cases.");
  58. }
  59. public Task<bool> Initialize()
  60. {
  61. // Dummy client needs no async initialization.
  62. return Task.FromResult(true);
  63. }
  64. public async Task SendPrompt(string prompt, List<UnityEngine.Object> stagedContext, CancellationToken cancellationToken = default)
  65. {
  66. if (!_testCasesLoaded) DiscoverTestCases();
  67. _activeTestCase = _testCases.FirstOrDefault(
  68. c => prompt.Equals(c.TriggerPhrase, StringComparison.Ordinal)
  69. );
  70. // Simulate network delay
  71. await Task.Delay(500, cancellationToken);
  72. if (cancellationToken.IsCancellationRequested) return;
  73. if (_activeTestCase != null)
  74. {
  75. Debug.Log($"[DummyApiClient] Triggered test case for: '{_activeTestCase.TriggerPhrase}'");
  76. _activeResponseStep = 0;
  77. ExecuteStep();
  78. }
  79. else
  80. {
  81. DisplayError("No test case was triggered by your prompt.");
  82. }
  83. }
  84. public async Task SendFollowUp(string detailedContext, CancellationToken cancellationToken = default)
  85. {
  86. // Simulate network delay
  87. await Task.Delay(500, cancellationToken);
  88. if (cancellationToken.IsCancellationRequested) return;
  89. if (_activeTestCase != null)
  90. {
  91. _activeResponseStep++;
  92. Debug.Log($"[DummyApiClient] Executing follow-up step #{_activeResponseStep + 1} for test case: '{_activeTestCase.TriggerPhrase}'");
  93. ExecuteStep();
  94. }
  95. else
  96. {
  97. Debug.LogWarning("[DummyApiClient] Received a follow-up call, but no active test case was found.");
  98. DisplayError("No active test case for follow-up.");
  99. }
  100. }
  101. public string GetAuthToken()
  102. {
  103. return "dummy-token";
  104. }
  105. private void ExecuteStep()
  106. {
  107. var commandSteps = _activeTestCase?.GetCommandSteps();
  108. if (commandSteps == null || _activeResponseStep >= commandSteps.Count)
  109. {
  110. Debug.Log("[DummyApiClient] Test case finished or has no more steps.");
  111. _activeTestCase = null; // End the test case
  112. return;
  113. }
  114. var commands = commandSteps[_activeResponseStep];
  115. CommandExecutor.SetQueue(commands, _activeTestCase.TriggerPhrase);
  116. }
  117. private void DisplayError(string message)
  118. {
  119. var commandData = new CommandData
  120. {
  121. commandName = nameof(DisplayMessageCommand),
  122. jsonData = new Newtonsoft.Json.Linq.JObject
  123. {
  124. ["outcome"] = "Error",
  125. ["message"] = $"[Dummy Client Error] {message}"
  126. }
  127. };
  128. CommandExecutor.SetQueue(new List<CommandData> { commandData }, "Error");
  129. }
  130. }
  131. }