110 lines
3.2 KiB
C#
110 lines
3.2 KiB
C#
using System.Text.Json;
|
|
using Toak.Serialization;
|
|
using Toak.Core.Interfaces;
|
|
|
|
namespace Toak.Core;
|
|
|
|
public class HistoryManager : IHistoryManager
|
|
{
|
|
private readonly string _historyDir = Constants.Paths.AppDataDir;
|
|
private readonly string _historyFile = Constants.Paths.HistoryFile;
|
|
|
|
public void SaveEntry(string rawTranscript, string refinedText, string? skillName, long durationMs)
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(_historyDir))
|
|
{
|
|
Directory.CreateDirectory(_historyDir);
|
|
}
|
|
|
|
var entry = new HistoryEntry
|
|
{
|
|
Timestamp = DateTime.UtcNow,
|
|
RawTranscript = rawTranscript,
|
|
RefinedText = refinedText,
|
|
SkillName = skillName,
|
|
DurationMs = durationMs
|
|
};
|
|
|
|
var json = JsonSerializer.Serialize(entry, CompactJsonSerializerContext.Default.HistoryEntry);
|
|
|
|
// Thread-safe append
|
|
lock (_historyFile)
|
|
{
|
|
File.AppendAllLines(_historyFile, [json]);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogDebug($"Failed to save history: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public List<HistoryEntry> LoadHistory()
|
|
{
|
|
var entries = new List<HistoryEntry>();
|
|
if (!File.Exists(_historyFile)) return entries;
|
|
|
|
try
|
|
{
|
|
string[] lines;
|
|
lock (_historyFile)
|
|
{
|
|
lines = File.ReadAllLines(_historyFile);
|
|
}
|
|
|
|
foreach (var line in lines)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(line)) continue;
|
|
if (!line.Trim().StartsWith("{") || !line.Trim().EndsWith("}")) continue; // Skip malformed old multiline json entries
|
|
|
|
try
|
|
{
|
|
var entry = JsonSerializer.Deserialize(line, CompactJsonSerializerContext.Default.HistoryEntry);
|
|
if (entry != null)
|
|
{
|
|
entries.Add(entry);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Skip entry if deserialization fails
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"CRASH IN LOAD ENTRIES: {ex}");
|
|
Logger.LogDebug($"Failed to load history: {ex.Message}");
|
|
}
|
|
|
|
return entries;
|
|
}
|
|
|
|
public void ClearHistory()
|
|
{
|
|
if (File.Exists(_historyFile))
|
|
{
|
|
try
|
|
{
|
|
lock (_historyFile)
|
|
{
|
|
// Securely delete
|
|
var len = new FileInfo(_historyFile).Length;
|
|
using (var fs = new FileStream(_historyFile, FileMode.Open, FileAccess.Write))
|
|
{
|
|
var blank = new byte[len];
|
|
fs.Write(blank, 0, blank.Length);
|
|
}
|
|
File.Delete(_historyFile);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogDebug($"Failed to shred history: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|