MCPWindow.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using UnityEditor;
  2. using UnityEngine;
  3. using LLM.Editor.Core;
  4. using LLM.Editor.Client;
  5. using System.Threading.Tasks;
  6. namespace LLM.Editor.GUI
  7. {
  8. public class MCPWindow : EditorWindow
  9. {
  10. private string _promptText = "Create a script named Rotate with a public float for speed. Then, find the 'Racer' model, make a prefab from it, attach the script, and add it to the scene.";
  11. private Vector2 _scrollPos;
  12. private bool _isRequestInProgress = false;
  13. [MenuItem("Tools/LLM/MCP Assistant")]
  14. public static void ShowWindow()
  15. {
  16. GetWindow<MCPWindow>("MCP Assistant");
  17. }
  18. [MenuItem("Tools/LLM/Print Persistent Data Path")]
  19. public static void PrintPersistantPath()
  20. {
  21. Debug.Log(Application.persistentDataPath);
  22. }
  23. private void OnEnable()
  24. {
  25. // Subscribe to updates from the executor so the window can repaint
  26. CommandExecutor.OnQueueUpdated += Repaint;
  27. }
  28. private void OnDisable()
  29. {
  30. CommandExecutor.OnQueueUpdated -= Repaint;
  31. }
  32. private void OnGUI()
  33. {
  34. EditorGUILayout.LabelField("LLM Co-Pilot", EditorStyles.boldLabel);
  35. EditorGUILayout.BeginHorizontal();
  36. if (SessionManager.HasActiveSession())
  37. {
  38. EditorGUILayout.LabelField($"Session: {SessionManager.GetCurrentSessionId()}");
  39. if (GUILayout.Button("End Session"))
  40. {
  41. SessionManager.EndSession();
  42. }
  43. }
  44. else
  45. {
  46. if (GUILayout.Button("Start New Session"))
  47. {
  48. SessionManager.StartNewSession();
  49. }
  50. }
  51. EditorGUILayout.EndHorizontal();
  52. EditorGUILayout.Space();
  53. UnityEngine.GUI.enabled = SessionManager.HasActiveSession() && !_isRequestInProgress;
  54. EditorGUILayout.LabelField("Enter your request:");
  55. _promptText = EditorGUILayout.TextArea(_promptText, GUILayout.Height(60));
  56. if (GUILayout.Button(_isRequestInProgress ? "Waiting for response..." : "Send"))
  57. {
  58. // Fire off the async task
  59. _ = SendPromptAsync();
  60. }
  61. EditorGUILayout.Space();
  62. UnityEngine.GUI.enabled = !_isRequestInProgress;
  63. EditorGUILayout.LabelField("Pending Commands", EditorStyles.boldLabel);
  64. _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, EditorStyles.helpBox);
  65. if (CommandExecutor.HasPendingCommands())
  66. {
  67. var nextCommand = CommandExecutor.GetNextCommand();
  68. EditorGUILayout.LabelField("Next Up: " + nextCommand.commandName);
  69. if (GUILayout.Button("Execute Next Command"))
  70. {
  71. CommandExecutor.ExecuteNextCommand();
  72. }
  73. }
  74. else
  75. {
  76. EditorGUILayout.LabelField("No pending commands.");
  77. }
  78. EditorGUILayout.EndScrollView();
  79. UnityEngine.GUI.enabled = true;
  80. }
  81. private async Task SendPromptAsync()
  82. {
  83. _isRequestInProgress = true;
  84. Repaint();
  85. await GeminiApiClient.SendPrompt(_promptText);
  86. _isRequestInProgress = false;
  87. Repaint();
  88. }
  89. }
  90. }