SessionManager.cs 4.9 KB

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