-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistoryManager.cs
More file actions
84 lines (75 loc) · 2.12 KB
/
Copy pathHistoryManager.cs
File metadata and controls
84 lines (75 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;
namespace GIDE
{
public class HistoryManager
{
private string _historyPath;
private List<ChatMessage> _messages = new List<ChatMessage>();
public HistoryManager(string workDir)
{
_historyPath = Path.Combine(workDir, ".gide_history.json");
Load();
}
public void AddUserMessage(string content)
{
ChatMessage msg = new ChatMessage();
msg.Role = "user";
msg.Content = content;
_messages.Add(msg);
Trim();
Save();
}
public void AddAssistantMessage(string content)
{
ChatMessage msg = new ChatMessage();
msg.Role = "assistant";
msg.Content = content;
_messages.Add(msg);
Trim();
Save();
}
private void Trim()
{
if (_messages.Count > 200)
_messages.RemoveRange(0, _messages.Count - 200);
}
private void Load()
{
if (File.Exists(_historyPath))
{
try
{
string json = File.ReadAllText(_historyPath);
JavaScriptSerializer serializer = new JavaScriptSerializer();
_messages = serializer.Deserialize<List<ChatMessage>>(json) ?? new List<ChatMessage>();
}
catch
{
_messages = new List<ChatMessage>();
}
}
}
private void Save()
{
try
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(_messages);
File.WriteAllText(_historyPath, json);
}
catch { }
}
public List<ChatMessage> GetMessages()
{
return _messages;
}
public void Clear()
{
_messages.Clear();
Save();
}
}
}