MCPWindow.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using UnityEditor;
  2. using UnityEngine;
  3. using LLM.Editor.Core;
  4. using LLM.Editor.Client;
  5. using LLM.Editor.Commands;
  6. using LLM.Editor.Analysis;
  7. using System.Threading.Tasks;
  8. using System.Collections.Generic;
  9. namespace LLM.Editor.GUI
  10. {
  11. public class MCPWindow : EditorWindow
  12. {
  13. private string _promptText = "The grenade launcher should hit the target.";
  14. private Vector2 _scrollPos;
  15. private bool _isRequestInProgress;
  16. private readonly List<Object> _stagedContextObjects = new();
  17. private List<string> _requestedRoles = new();
  18. private bool _isAnalysisContextRequested;
  19. private ILlmApiClient _apiClient;
  20. [MenuItem("Tools/LLM/MCP Assistant")]
  21. public static void ShowWindow()
  22. {
  23. GetWindow<MCPWindow>("MCP Assistant");
  24. }
  25. private void OnEnable()
  26. {
  27. CommandExecutor.OnQueueUpdated += Repaint;
  28. RequestAnalysisContextCommand.OnAnalysisContextRequested += HandleAnalysisContextRequested;
  29. CommandExecutor.OnContextReadyForNextTurn += HandleContextReadyForNextTurn;
  30. _apiClient = ApiClientFactory.GetClient();
  31. }
  32. private void OnDisable()
  33. {
  34. CommandExecutor.OnQueueUpdated -= Repaint;
  35. RequestAnalysisContextCommand.OnAnalysisContextRequested -= HandleAnalysisContextRequested;
  36. CommandExecutor.OnContextReadyForNextTurn -= HandleContextReadyForNextTurn;
  37. }
  38. private void HandleAnalysisContextRequested(RequestAnalysisContextParams contextParams)
  39. {
  40. _isAnalysisContextRequested = true;
  41. _requestedRoles = new List<string>(contextParams.subjectRoles);
  42. // Ensure our staging list has enough slots for what was requested
  43. while (_stagedContextObjects.Count < _requestedRoles.Count)
  44. {
  45. _stagedContextObjects.Add(null);
  46. }
  47. Repaint();
  48. }
  49. private void HandleContextReadyForNextTurn(string detailedContext)
  50. {
  51. Debug.Log("[MCPWindow] Received detailed context. Sending follow-up to LLM.");
  52. _ = SendFollowUpAsync(detailedContext);
  53. }
  54. private void OnGUI()
  55. {
  56. EditorGUILayout.LabelField("LLM Co-Pilot", EditorStyles.boldLabel);
  57. DrawSessionManagement();
  58. EditorGUILayout.Space();
  59. UnityEngine.GUI.enabled = SessionManager.HasActiveSession();
  60. DrawContextStagingArea();
  61. EditorGUILayout.Space();
  62. EditorGUILayout.LabelField("Enter your request:");
  63. _promptText = EditorGUILayout.TextArea(_promptText, GUILayout.Height(60));
  64. DrawActionButton();
  65. EditorGUILayout.Space();
  66. DrawCommandQueue();
  67. UnityEngine.GUI.enabled = true;
  68. }
  69. private void DrawSessionManagement()
  70. {
  71. EditorGUILayout.BeginHorizontal();
  72. if (SessionManager.HasActiveSession())
  73. {
  74. EditorGUILayout.LabelField($"Session: {SessionManager.GetCurrentSessionId()}");
  75. if (GUILayout.Button("End Session"))
  76. {
  77. SessionManager.EndSession();
  78. _stagedContextObjects.Clear();
  79. _isAnalysisContextRequested = false;
  80. }
  81. }
  82. else
  83. {
  84. if (GUILayout.Button("Start New Session"))
  85. {
  86. SessionManager.StartNewSession();
  87. }
  88. }
  89. EditorGUILayout.EndHorizontal();
  90. }
  91. private void DrawContextStagingArea()
  92. {
  93. EditorGUILayout.LabelField("Context (Drag & Drop Assets Here)", EditorStyles.boldLabel);
  94. if (GUILayout.Button("Add Context Slot", GUILayout.Width(150)))
  95. {
  96. _stagedContextObjects.Add(null);
  97. if(_isAnalysisContextRequested) _requestedRoles.Add("Custom");
  98. }
  99. var indexToRemove = -1;
  100. for (var i = 0; i < _stagedContextObjects.Count; i++)
  101. {
  102. EditorGUILayout.BeginHorizontal();
  103. var label = (_isAnalysisContextRequested && i < _requestedRoles.Count) ? _requestedRoles[i] : $"Context {i + 1}";
  104. _stagedContextObjects[i] = EditorGUILayout.ObjectField(label, _stagedContextObjects[i], typeof(Object), true);
  105. if (GUILayout.Button("X", GUILayout.Width(20)))
  106. {
  107. indexToRemove = i;
  108. }
  109. EditorGUILayout.EndHorizontal();
  110. }
  111. if (indexToRemove == -1) return;
  112. _stagedContextObjects.RemoveAt(indexToRemove);
  113. if(_isAnalysisContextRequested && indexToRemove < _requestedRoles.Count)
  114. {
  115. _requestedRoles.RemoveAt(indexToRemove);
  116. }
  117. Repaint();
  118. }
  119. private void DrawActionButton()
  120. {
  121. UnityEngine.GUI.enabled = SessionManager.HasActiveSession() && !_isRequestInProgress;
  122. if (_isAnalysisContextRequested)
  123. {
  124. if (!GUILayout.Button("Provide Staged Context to LLM")) return;
  125. var contextSummary = ContextBuilder.BuildTier1Summary(_stagedContextObjects);
  126. var followUpMessage = "Here is the context you requested:\n" + contextSummary;
  127. _ = SendFollowUpAsync(followUpMessage);
  128. }
  129. else
  130. {
  131. if (GUILayout.Button(_isRequestInProgress ? "Waiting for response..." : "Send Prompt"))
  132. {
  133. _ = SendInitialPromptAsync();
  134. }
  135. }
  136. }
  137. private void DrawCommandQueue()
  138. {
  139. UnityEngine.GUI.enabled = !_isRequestInProgress;
  140. EditorGUILayout.LabelField("Pending Commands", EditorStyles.boldLabel);
  141. _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, EditorStyles.helpBox);
  142. if (CommandExecutor.HasPendingCommands())
  143. {
  144. var nextCommand = CommandExecutor.GetNextCommand();
  145. EditorGUILayout.LabelField("Next Up: " + nextCommand.commandName);
  146. if (GUILayout.Button("Execute Next Command"))
  147. {
  148. CommandExecutor.ExecuteNextCommand();
  149. }
  150. }
  151. else
  152. {
  153. EditorGUILayout.LabelField("No pending commands.");
  154. }
  155. EditorGUILayout.EndScrollView();
  156. }
  157. private async Task SendInitialPromptAsync()
  158. {
  159. _isRequestInProgress = true;
  160. Repaint();
  161. // Pass the staged objects to the API client
  162. await _apiClient.SendPrompt(_promptText, _stagedContextObjects);
  163. _isRequestInProgress = false;
  164. ClearStagingArea();
  165. Repaint();
  166. }
  167. private async Task SendFollowUpAsync(string followUpMessage)
  168. {
  169. _isRequestInProgress = true;
  170. Repaint();
  171. await _apiClient.SendFollowUp(followUpMessage);
  172. _isRequestInProgress = false;
  173. // Only clear the staging area if the follow-up was initiated by the user
  174. // providing context. This preserves the UI state otherwise.
  175. if (_isAnalysisContextRequested)
  176. {
  177. ClearStagingArea();
  178. }
  179. Repaint();
  180. }
  181. private void ClearStagingArea()
  182. {
  183. _isAnalysisContextRequested = false;
  184. _stagedContextObjects.Clear();
  185. _requestedRoles.Clear();
  186. }
  187. }
  188. }