using System.Collections.Generic;
using System.IO;
using LLM.Editor.Data;
using LLM.Editor.Helper;
using UnityEditor;
using UnityEngine;
namespace LLM.Editor.Core
{
///
/// Handles all file I/O for the session cache.
/// Manages to create new sessions, reading/writing chat logs, and persisting the command queue.
///
public static class SessionManager
{
private static readonly string _sessionCacheRoot;
private static string _currentSessionId;
private const string SESSION_ID_KEY = "MCP_CurrentSessionID";
private const string QUEUE_FILE = "queue.json";
private const string CHAT_LOG_FILE = "chat_log.json";
// Helper class for serializing a list to JSON
[System.Serializable] private class CommandListWrapper { public List commands; }
[System.Serializable] private class ChatLogWrapper { public List history; }
static SessionManager()
{
_sessionCacheRoot = Path.Combine(Application.persistentDataPath, "LLMCache");
Directory.CreateDirectory(_sessionCacheRoot);
_currentSessionId = SessionState.GetString(SESSION_ID_KEY, null);
Debug.Log($"[SessionManager] Initialized. Current Session: {_currentSessionId}");
}
public static string GetCurrentSessionId() => _currentSessionId;
public static bool HasActiveSession() => !string.IsNullOrEmpty(_currentSessionId) && Directory.Exists(Path.Combine(_sessionCacheRoot, _currentSessionId));
public static void StartNewSession()
{
_currentSessionId = $"session_{System.DateTime.Now:yyyyMMddHHmmss}";
SessionState.SetString(SESSION_ID_KEY, _currentSessionId);
Directory.CreateDirectory(Path.Combine(_sessionCacheRoot, _currentSessionId));
Debug.Log($"[SessionManager] Started new session: {_currentSessionId}");
}
public static void EndSession()
{
_currentSessionId = null;
SessionState.EraseString(SESSION_ID_KEY);
Debug.Log("[SessionManager] Ended session.");
}
public static void SaveCommandQueue(List queue)
{
if (!HasActiveSession())
{
Debug.LogError("[SessionManager] Cannot save queue, no active session.");
return;
}
var wrapper = new CommandListWrapper { commands = queue };
var json = wrapper.ToJson();
var path = Path.Combine(_sessionCacheRoot, _currentSessionId, QUEUE_FILE);
File.WriteAllText(path, json);
Debug.Log($"[SessionManager] Saved {queue.Count} command(s) to queue.");
}
public static List LoadCommandQueue()
{
if (!HasActiveSession()) return new List();
var path = Path.Combine(_sessionCacheRoot, _currentSessionId, QUEUE_FILE);
if (!File.Exists(path)) return new List();
var json = File.ReadAllText(path);
var wrapper = json.FromJson();
Debug.Log($"[SessionManager] Loaded {wrapper.commands.Count} command(s) from queue.");
return wrapper.commands ?? new List();
}
public static void SaveChatHistory(List history)
{
if (!HasActiveSession()) return;
var wrapper = new ChatLogWrapper { history = history };
var json = wrapper.ToJson();
var path = Path.Combine(_sessionCacheRoot, _currentSessionId, CHAT_LOG_FILE);
File.WriteAllText(path, json);
}
public static List LoadChatHistory()
{
if (!HasActiveSession()) return new List();
var path = Path.Combine(_sessionCacheRoot, _currentSessionId, CHAT_LOG_FILE);
if (!File.Exists(path)) return new List();
var json = File.ReadAllText(path);
var wrapper = json.FromJson();
return wrapper?.history ?? new List();
}
}
}