Families.AutoWriter 1.0.0.0
Families 콘텐츠를 반복 작업 없이 작성·등록하도록 돕는 자동화 작성 도구입니다.
로딩중...
검색중...
일치하는것 없음
PromptHistoryService.cs
이 파일의 문서화 페이지로 가기
1using System.IO;
2using System.Security.Cryptography;
3using System.Text;
4using System.Text.Json;
5
7
16public sealed class PromptHistoryService
17{
26 private static readonly object _fileGate = new();
35 private readonly string _historyFile;
44 private readonly HashSet<string> _sentHashes;
45
55 {
56 var dir = Path.Combine(
57 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
58 "FamiliesAutoWriter");
59 Directory.CreateDirectory(dir);
60 _historyFile = Path.Combine(dir, "prompt_history.json");
61 _sentHashes = Load();
62 }
63
88 public bool IsDuplicate(string prompt)
89 {
90 lock (_fileGate)
91 {
92 foreach (var hash in Load())
93 _sentHashes.Add(hash);
94 return _sentHashes.Contains(Hash(prompt));
95 }
96 }
97
114 public void MarkSent(string prompt)
115 {
116 lock (_fileGate)
117 {
118 foreach (var hash in Load())
119 _sentHashes.Add(hash);
120 _sentHashes.Add(Hash(prompt));
121 Save();
122 }
123 }
124
133 public void Clear()
134 {
135 lock (_fileGate)
136 {
137 _sentHashes.Clear();
138 Save();
139 }
140 }
141
158 private HashSet<string> Load()
159 {
160 if (!File.Exists(_historyFile)) return [];
161 try
162 {
163 var json = File.ReadAllText(_historyFile);
164 return JsonSerializer.Deserialize<HashSet<string>>(json) ?? [];
165 }
166 catch { return []; }
167 }
168
177 private void Save() =>
178 File.WriteAllText(_historyFile, JsonSerializer.Serialize(_sentHashes));
179
204 private static string Hash(string s) =>
205 Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(s.Trim()))).ToLower()[..16];
206}