MCPWindow.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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;
  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. [MenuItem("Tools/LLM/Print Instance ID (Selected Object)")]
  24. public static void PrintInstanceId()
  25. {
  26. var selection = Selection.activeObject;
  27. if (selection != null)
  28. {
  29. Debug.Log(selection.GetInstanceID());
  30. }
  31. }
  32. [MenuItem("Tools/LLM/Log object with instance ID")]
  33. public static void PrintObjectWithInstanceId()
  34. {
  35. var obj = EditorUtility.InstanceIDToObject(20404);
  36. Debug.Log(obj, obj);
  37. }
  38. private void OnEnable()
  39. {
  40. // Subscribe to updates from the executor so the window can repaint
  41. CommandExecutor.OnQueueUpdated += Repaint;
  42. }
  43. private void OnDisable()
  44. {
  45. CommandExecutor.OnQueueUpdated -= Repaint;
  46. }
  47. private void OnGUI()
  48. {
  49. EditorGUILayout.LabelField("LLM Co-Pilot", EditorStyles.boldLabel);
  50. EditorGUILayout.BeginHorizontal();
  51. if (SessionManager.HasActiveSession())
  52. {
  53. EditorGUILayout.LabelField($"Session: {SessionManager.GetCurrentSessionId()}");
  54. if (GUILayout.Button("End Session"))
  55. {
  56. SessionManager.EndSession();
  57. }
  58. }
  59. else
  60. {
  61. if (GUILayout.Button("Start New Session"))
  62. {
  63. SessionManager.StartNewSession();
  64. }
  65. }
  66. EditorGUILayout.EndHorizontal();
  67. EditorGUILayout.Space();
  68. UnityEngine.GUI.enabled = SessionManager.HasActiveSession() && !_isRequestInProgress;
  69. EditorGUILayout.LabelField("Enter your request:");
  70. _promptText = EditorGUILayout.TextArea(_promptText, GUILayout.Height(60));
  71. if (GUILayout.Button(_isRequestInProgress ? "Waiting for response..." : "Send"))
  72. {
  73. // Fire off the async task
  74. _ = SendPromptAsync();
  75. }
  76. EditorGUILayout.Space();
  77. UnityEngine.GUI.enabled = !_isRequestInProgress;
  78. EditorGUILayout.LabelField("Pending Commands", EditorStyles.boldLabel);
  79. _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, EditorStyles.helpBox);
  80. if (CommandExecutor.HasPendingCommands())
  81. {
  82. var nextCommand = CommandExecutor.GetNextCommand();
  83. EditorGUILayout.LabelField("Next Up: " + nextCommand.commandName);
  84. if (GUILayout.Button("Execute Next Command"))
  85. {
  86. CommandExecutor.ExecuteNextCommand();
  87. }
  88. }
  89. else
  90. {
  91. EditorGUILayout.LabelField("No pending commands.");
  92. }
  93. EditorGUILayout.EndScrollView();
  94. UnityEngine.GUI.enabled = true;
  95. }
  96. private async Task SendPromptAsync()
  97. {
  98. _isRequestInProgress = true;
  99. Repaint();
  100. await GeminiApiClient.SendPrompt(_promptText);
  101. _isRequestInProgress = false;
  102. Repaint();
  103. }
  104. }
  105. }