ContextBuilder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Linq;
  5. using System.Text;
  6. using LLM.Editor.Helper;
  7. using System.Reflection;
  8. using System.Collections.Generic;
  9. using IntelligentProjectAnalyzer.Editor.Graphing;
  10. namespace LLM.Editor.Analysis
  11. {
  12. /// <summary>
  13. /// Responsible for gathering and serializing context about staged assets
  14. /// into a detailed, human-readable, and referential format for the LLM.
  15. /// </summary>
  16. public static class ContextBuilder
  17. {
  18. /// <summary>
  19. /// Builds a detailed Tier 1 summary of the provided objects, including a pruned hierarchy for GameObjects.
  20. /// </summary>
  21. /// <param name="stagedObjects">The list of objects from the UI's staging area.</param>
  22. /// <returns>A formatted string summarizing the context for the LLM.</returns>
  23. public static string BuildTier1Summary(List<Object> stagedObjects)
  24. {
  25. var summaryBuilder = new StringBuilder();
  26. // 1. Always Prepend Project Info
  27. AppendProjectInfo(summaryBuilder);
  28. // 2. Process Staged Objects
  29. if (stagedObjects is { Count: > 0 } && stagedObjects.Any(o => o != null))
  30. {
  31. for (var i = 0; i < stagedObjects.Count; i++)
  32. {
  33. var obj = stagedObjects[i];
  34. if (!obj) continue;
  35. var stableId = GetStableIdForObject(obj);
  36. summaryBuilder.AppendLine($"\n## Subject {i + 1}: {obj.name} (ID: {stableId})");
  37. summaryBuilder.AppendLine($"- Type: {obj.GetType().FullName}");
  38. switch (obj)
  39. {
  40. case GameObject go:
  41. AppendGameObjectContext(go, summaryBuilder);
  42. break;
  43. case MonoScript script:
  44. AppendScriptContext(script, summaryBuilder);
  45. break;
  46. case ScriptableObject so:
  47. AppendScriptableObjectContext(so, summaryBuilder);
  48. break;
  49. }
  50. }
  51. }
  52. // 3. Always Append Editor Context
  53. summaryBuilder.AppendLine("\n## Editor Context");
  54. // Add Editor State
  55. var editorStateProvider = new EditorStateProvider();
  56. var editorState = editorStateProvider.GetContext(null, null);
  57. summaryBuilder.AppendLine($"- Editor State: {editorState.ToJson()}");
  58. return summaryBuilder.ToString();
  59. }
  60. private static void AppendProjectInfo(StringBuilder builder)
  61. {
  62. builder.AppendLine("## Project Info");
  63. builder.AppendLine($"- Project Name: {Application.productName}");
  64. builder.AppendLine($"- Editor Mode: {(EditorSettings.defaultBehaviorMode == EditorBehaviorMode.Mode2D ? "2D" : "3D")}");
  65. var rpType = "Built-in";
  66. if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset)
  67. {
  68. rpType = UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset.GetType().Name.Replace("Asset", "");
  69. }
  70. builder.AppendLine($"- Render Pipeline: {rpType}");
  71. }
  72. private static void AppendGameObjectContext(GameObject go, StringBuilder builder)
  73. {
  74. builder.AppendLine($"- Tag: {go.tag}");
  75. builder.AppendLine($"- Layer: {LayerMask.LayerToName(go.layer)}");
  76. var prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go);
  77. if (!string.IsNullOrEmpty(prefabPath))
  78. {
  79. builder.AppendLine($"- Prefab Source: {prefabPath}");
  80. }
  81. builder.AppendLine("- Hierarchy (pruned to show nodes with custom scripts):");
  82. AppendHierarchyNode(go.transform, builder, " ");
  83. }
  84. /// <summary>
  85. /// Recursively builds a string representation of the hierarchy, intelligently pruning branches
  86. /// that do not contain any custom user scripts to keep the context concise.
  87. /// </summary>
  88. private static void AppendHierarchyNode(Transform transform, StringBuilder builder, string indent)
  89. {
  90. // Always include the root of the hierarchy being analyzed.
  91. builder.AppendLine($"{indent}- {transform.name} (ID: {transform.gameObject.GetInstanceID()})");
  92. // List components on the current GameObject, prioritizing custom scripts.
  93. var components = transform.GetComponents<Component>().Where(c => c).ToList();
  94. var customScripts = components.Where(IsCustomScript).ToList();
  95. var otherComponents = components.Except(customScripts);
  96. foreach (var script in customScripts)
  97. {
  98. var monoScript = MonoScript.FromMonoBehaviour(script as MonoBehaviour);
  99. var scriptGuid = monoScript ? AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript)) : "N/A";
  100. builder.AppendLine($"{indent} - Component: {script.GetType().FullName} (Custom Script, GUID: {scriptGuid})");
  101. }
  102. foreach (var component in otherComponents)
  103. {
  104. builder.AppendLine($"{indent} - Component: {component.GetType().FullName}");
  105. }
  106. // --- Pruning Logic ---
  107. var childrenWithScripts = new List<Transform>();
  108. var childrenWithoutScripts = new List<Transform>();
  109. foreach (Transform child in transform)
  110. {
  111. if (HasCustomScriptInChildren(child))
  112. {
  113. childrenWithScripts.Add(child);
  114. }
  115. else
  116. {
  117. childrenWithoutScripts.Add(child);
  118. }
  119. }
  120. // Recurse into children that have scripts.
  121. foreach (var child in childrenWithScripts)
  122. {
  123. AppendHierarchyNode(child, builder, indent + " ");
  124. }
  125. // Summarize the pruned children.
  126. if (childrenWithoutScripts.Count > 0)
  127. {
  128. builder.AppendLine($"{indent} [+ {childrenWithoutScripts.Count} other child object(s) with no custom scripts]");
  129. }
  130. }
  131. private static void AppendScriptContext(MonoScript script, StringBuilder builder)
  132. {
  133. var scriptClass = script.GetClass();
  134. if (scriptClass != null)
  135. {
  136. if (scriptClass.BaseType != null && scriptClass.BaseType != typeof(MonoBehaviour))
  137. {
  138. builder.AppendLine($"- Inherits from: {scriptClass.BaseType.FullName}");
  139. }
  140. var interfaces = scriptClass.GetInterfaces();
  141. if (interfaces.Length > 0)
  142. {
  143. builder.AppendLine($"- Implements: {string.Join(", ", interfaces.Select(i => i.Name))}");
  144. }
  145. var methods = scriptClass.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
  146. .Where(m => !m.IsSpecialName); // Exclude property getters/setters, etc.
  147. var methodInfos = methods as MethodInfo[] ?? methods.ToArray();
  148. if (methodInfos.Any())
  149. {
  150. builder.AppendLine("- Public API:");
  151. foreach (var method in methodInfos)
  152. {
  153. var parameters = string.Join(", ", method.GetParameters().Select(p => $"{p.ParameterType.Name} {p.Name}"));
  154. builder.AppendLine($" - {method.ReturnType.Name} {method.Name}({parameters})");
  155. }
  156. }
  157. }
  158. var scriptPath = AssetDatabase.GetAssetPath(script);
  159. if (!File.Exists(scriptPath)) return;
  160. builder.AppendLine("- Script Content:");
  161. builder.AppendLine("```csharp");
  162. builder.AppendLine(File.ReadAllText(scriptPath));
  163. builder.AppendLine("```");
  164. // Append dependency graph if available
  165. var guid = AssetDatabase.AssetPathToGUID(scriptPath);
  166. _ = GraphJsonExporter.TryAppendHumanReadableGraph(guid, builder);
  167. }
  168. private static void AppendScriptableObjectContext(ScriptableObject so, StringBuilder builder)
  169. {
  170. builder.AppendLine("- Note: This is a ScriptableObject data asset.");
  171. var fields = so.GetType()
  172. .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  173. .Where(field => field.IsPublic || field.GetCustomAttribute<SerializeField>() != null);
  174. var fieldInfos = fields as FieldInfo[] ?? fields.ToArray();
  175. if (!fieldInfos.Any()) return;
  176. builder.AppendLine(" - Public Fields:");
  177. foreach (var field in fieldInfos)
  178. {
  179. builder.AppendLine($" - {field.Name}: {field.GetValue(so)}");
  180. }
  181. }
  182. private static string GetStableIdForObject(Object obj)
  183. {
  184. if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out var guid, out long _)) return obj.GetInstanceID().ToString();
  185. if (!string.IsNullOrEmpty(guid) && guid != "00000000000000000000000000000000")
  186. {
  187. return guid;
  188. }
  189. return obj.GetInstanceID().ToString();
  190. }
  191. /// <summary>
  192. /// Checks if a component is a user-defined script (i.e., part of the Assets folder).
  193. /// </summary>
  194. private static bool IsCustomScript(Component component)
  195. {
  196. if (!component || component is not MonoBehaviour monoBehaviour) return false;
  197. var script = MonoScript.FromMonoBehaviour(monoBehaviour);
  198. if (!script) return false;
  199. var path = AssetDatabase.GetAssetPath(script);
  200. return !string.IsNullOrEmpty(path) && path.StartsWith("Assets/");
  201. }
  202. /// <summary>
  203. /// Recursively checks if a transform or any of its descendants contain a custom script.
  204. /// </summary>
  205. private static bool HasCustomScriptInChildren(Transform transform)
  206. {
  207. if (transform.GetComponents<Component>().Any(IsCustomScript))
  208. {
  209. return true;
  210. }
  211. foreach (Transform child in transform)
  212. {
  213. if (HasCustomScriptInChildren(child))
  214. {
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. }
  221. }