SessionManager.cs 6.5 KB

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