CommandExecutor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using LLM.Editor.Data;
  6. using System.Reflection;
  7. using LLM.Editor.Commands;
  8. using System.Collections.Generic;
  9. namespace LLM.Editor.Core
  10. {
  11. /// <summary>
  12. /// Responsible for finding and executing commands from the session queue.
  13. /// </summary>
  14. [InitializeOnLoad]
  15. public static class CommandExecutor
  16. {
  17. private static List<CommandData> _commandQueue;
  18. private static CommandContext _currentContext;
  19. public static Action OnQueueUpdated;
  20. static CommandExecutor()
  21. {
  22. // This will run when the editor loads, including after a recompile.
  23. EditorApplication.delayCall += Initialize;
  24. }
  25. private static void Initialize()
  26. {
  27. Debug.Log("[CommandExecutor] Initializing...");
  28. if (SessionManager.HasActiveSession())
  29. {
  30. _commandQueue = SessionManager.LoadCommandQueue();
  31. if (!_commandQueue.Any())
  32. {
  33. SessionManager.EndSession();
  34. return;
  35. }
  36. Debug.Log($"[CommandExecutor] Resuming session with {_commandQueue.Count} commands in queue.");
  37. _currentContext = new CommandContext();
  38. OnQueueUpdated?.Invoke();
  39. }
  40. else
  41. {
  42. _commandQueue = new List<CommandData>();
  43. }
  44. }
  45. public static void SetQueue(List<CommandData> commands)
  46. {
  47. _commandQueue = commands;
  48. _currentContext = new CommandContext();
  49. SessionManager.SaveCommandQueue(_commandQueue);
  50. OnQueueUpdated?.Invoke();
  51. }
  52. private static void ClearQueue()
  53. {
  54. _commandQueue.Clear();
  55. SessionManager.SaveCommandQueue(_commandQueue);
  56. OnQueueUpdated?.Invoke();
  57. }
  58. public static bool HasPendingCommands() => _commandQueue != null && _commandQueue.Any();
  59. public static CommandData GetNextCommand() => HasPendingCommands() ? _commandQueue.First() : null;
  60. public static void ExecuteNextCommand()
  61. {
  62. if (!HasPendingCommands())
  63. {
  64. Debug.LogWarning("[CommandExecutor] No commands to execute.");
  65. return;
  66. }
  67. var commandData = _commandQueue.First();
  68. _commandQueue.RemoveAt(0);
  69. try
  70. {
  71. var commandInstance = CreateCommandInstance(commandData);
  72. if (commandInstance != null)
  73. {
  74. Debug.Log($"[CommandExecutor] Executing: {commandData.commandName}");
  75. var outcome = commandInstance.Execute(_currentContext);
  76. var message = outcome == CommandOutcome.Success ? commandData.messages?.onSuccess : commandData.messages?.onError;
  77. new DisplayMessageCommand(new DisplayMessageParams { message = message, outcome = outcome }).Execute(_currentContext);
  78. if (outcome == CommandOutcome.Error)
  79. {
  80. Debug.LogError($"[CommandExecutor] Command '{commandData.commandName}' failed. Clearing remaining command queue.");
  81. ClearQueue();
  82. }
  83. }
  84. else
  85. {
  86. Debug.LogError($"[CommandExecutor] Could not create instance for command: {commandData.commandName}");
  87. }
  88. }
  89. catch(Exception e)
  90. {
  91. Debug.LogError($"[CommandExecutor] Failed to execute command '{commandData.commandName}'. Error: {e.Message}");
  92. ClearQueue();
  93. }
  94. // Save the modified queue
  95. SessionManager.SaveCommandQueue(_commandQueue);
  96. OnQueueUpdated?.Invoke();
  97. }
  98. private static ICommand CreateCommandInstance(CommandData data)
  99. {
  100. // Use reflection to find the command class in the Commands namespace
  101. // This makes the system extensible without needing a giant switch statement.
  102. var commandClassName = $"{data.commandName}Command";
  103. var type = Assembly.GetExecutingAssembly().GetTypes()
  104. .FirstOrDefault(t => t.Namespace == "LLM.Editor.Commands" && t.Name == commandClassName);
  105. if (type != null)
  106. {
  107. // Assumes commands have a constructor that takes a single string (the JSON parameters)
  108. return (ICommand)Activator.CreateInstance(type, data.jsonData);
  109. }
  110. Debug.LogError($"[CommandExecutor] Command type '{commandClassName}' not found.");
  111. return null;
  112. }
  113. }
  114. }