SessionManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using LLM.Editor.Data;
  4. using LLM.Editor.Helper;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace LLM.Editor.Core
  8. {
  9. /// <summary>
  10. /// Handles all file I/O for the session cache.
  11. /// Manages to create new sessions, reading/writing chat logs, and persisting the command queue.
  12. /// </summary>
  13. public static class SessionManager
  14. {
  15. private static readonly string _sessionCacheRoot;
  16. private static string _currentSessionId;
  17. private const string SESSION_ID_KEY = "MCP_CurrentSessionID";
  18. private const string QUEUE_FILE = "queue.json";
  19. private const string CHAT_LOG_FILE = "chat_log.json";
  20. // Helper class for serializing a list to JSON
  21. [System.Serializable] private class CommandListWrapper { public List<CommandData> commands; }
  22. [System.Serializable] private class ChatLogWrapper { public List<ChatEntry> history; }
  23. static SessionManager()
  24. {
  25. _sessionCacheRoot = Path.Combine(Application.persistentDataPath, "LLMCache");
  26. Directory.CreateDirectory(_sessionCacheRoot);
  27. _currentSessionId = SessionState.GetString(SESSION_ID_KEY, null);
  28. Debug.Log($"[SessionManager] Initialized. Current Session: {_currentSessionId}");
  29. }
  30. public static string GetCurrentSessionId() => _currentSessionId;
  31. public static bool HasActiveSession() => !string.IsNullOrEmpty(_currentSessionId) && Directory.Exists(Path.Combine(_sessionCacheRoot, _currentSessionId));
  32. public static void StartNewSession()
  33. {
  34. _currentSessionId = $"session_{System.DateTime.Now:yyyyMMddHHmmss}";
  35. SessionState.SetString(SESSION_ID_KEY, _currentSessionId);
  36. Directory.CreateDirectory(Path.Combine(_sessionCacheRoot, _currentSessionId));
  37. Debug.Log($"[SessionManager] Started new session: {_currentSessionId}");
  38. }
  39. public static void EndSession()
  40. {
  41. _currentSessionId = null;
  42. SessionState.EraseString(SESSION_ID_KEY);
  43. Debug.Log("[SessionManager] Ended session.");
  44. }
  45. public static void SaveCommandQueue(List<CommandData> queue)
  46. {
  47. if (!HasActiveSession())
  48. {
  49. Debug.LogError("[SessionManager] Cannot save queue, no active session.");
  50. return;
  51. }
  52. var wrapper = new CommandListWrapper { commands = queue };
  53. var json = wrapper.ToJson();
  54. var path = Path.Combine(_sessionCacheRoot, _currentSessionId, QUEUE_FILE);
  55. File.WriteAllText(path, json);
  56. Debug.Log($"[SessionManager] Saved {queue.Count} command(s) to queue.");
  57. }
  58. public static List<CommandData> LoadCommandQueue()
  59. {
  60. if (!HasActiveSession()) return new List<CommandData>();
  61. var path = Path.Combine(_sessionCacheRoot, _currentSessionId, QUEUE_FILE);
  62. if (!File.Exists(path)) return new List<CommandData>();
  63. var json = File.ReadAllText(path);
  64. var wrapper = json.FromJson<CommandListWrapper>();
  65. Debug.Log($"[SessionManager] Loaded {wrapper.commands.Count} command(s) from queue.");
  66. return wrapper.commands ?? new List<CommandData>();
  67. }
  68. public static void SaveChatHistory(List<ChatEntry> history)
  69. {
  70. if (!HasActiveSession()) return;
  71. var wrapper = new ChatLogWrapper { history = history };
  72. var json = wrapper.ToJson();
  73. var path = Path.Combine(_sessionCacheRoot, _currentSessionId, CHAT_LOG_FILE);
  74. File.WriteAllText(path, json);
  75. }
  76. public static List<ChatEntry> LoadChatHistory()
  77. {
  78. if (!HasActiveSession()) return new List<ChatEntry>();
  79. var path = Path.Combine(_sessionCacheRoot, _currentSessionId, CHAT_LOG_FILE);
  80. if (!File.Exists(path)) return new List<ChatEntry>();
  81. var json = File.ReadAllText(path);
  82. var wrapper = json.FromJson<ChatLogWrapper>();
  83. return wrapper?.history ?? new List<ChatEntry>();
  84. }
  85. }
  86. }