DirectoryStructureProvider.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace LLM.Editor.Analysis
  6. {
  7. /// <summary>
  8. /// Provides information about the project's directory structure.
  9. /// Can list directories at the root or the contents of a specific folder.
  10. /// </summary>
  11. public class DirectoryStructureProvider : IContextProvider
  12. {
  13. public class DirectoryContents
  14. {
  15. public List<string> directories;
  16. public List<string> files;
  17. }
  18. public object GetContext(Object target, string qualifier)
  19. {
  20. var rootPath = Application.dataPath;
  21. var searchPath = rootPath;
  22. if (!string.IsNullOrEmpty(qualifier))
  23. {
  24. searchPath = Path.Combine(rootPath, qualifier);
  25. }
  26. if (!Directory.Exists(searchPath))
  27. {
  28. return $"Error: Directory does not exist at path: '{qualifier}'";
  29. }
  30. try
  31. {
  32. var result = new DirectoryContents
  33. {
  34. directories = Directory.GetDirectories(searchPath).Select(Path.GetFileName).ToList(),
  35. files = Directory.GetFiles(searchPath).Select(Path.GetFileName).Where(f => !f.EndsWith(".meta")).ToList()
  36. };
  37. return result;
  38. }
  39. catch (System.Exception e)
  40. {
  41. return $"Error reading directory contents for '{qualifier}': {e.Message}";
  42. }
  43. }
  44. }
  45. }