ContextBuilder.cs 9.3 KB

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