SessionManager.cs 4.1 KB

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