-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathChatDatabase.cs
More file actions
414 lines (367 loc) · 13.1 KB
/
ChatDatabase.cs
File metadata and controls
414 lines (367 loc) · 13.1 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using SQLite;
using PolyPilot.Models;
using System.Threading;
namespace PolyPilot.Services;
public class ChatMessageEntity
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[Indexed]
public string SessionId { get; set; } = "";
public int OrderIndex { get; set; }
public string MessageType { get; set; } = "User"; // User, Assistant, Reasoning, ToolCall, Error
public string Content { get; set; } = "";
public string? ToolName { get; set; }
public string? ToolCallId { get; set; }
public bool IsComplete { get; set; } = true;
public bool IsSuccess { get; set; }
public string? ReasoningId { get; set; }
public string? Model { get; set; }
public DateTime Timestamp { get; set; }
// Cached rendered HTML for assistant markdown messages
public string? RenderedHtml { get; set; }
// Cached base64 data URI for image tool results
public string? ImageDataUri { get; set; }
// Original user-typed prompt before orchestration wrapper was prepended
public string? OriginalContent { get; set; }
// Tool input (arguments passed to the tool)
public string? ToolInput { get; set; }
// Image fields (for ChatMessageType.Image)
public string? ImagePath { get; set; }
public string? Caption { get; set; }
public ChatMessage ToChatMessage()
{
var type = Enum.TryParse<ChatMessageType>(MessageType, out var mt) ? mt : ChatMessageType.User;
var role = type == ChatMessageType.User ? "user" : "assistant";
var msg = new ChatMessage(role, Content, Timestamp, type)
{
ToolName = ToolName,
ToolCallId = ToolCallId,
ToolInput = ToolInput,
IsComplete = IsComplete,
IsSuccess = IsSuccess,
IsCollapsed = type is ChatMessageType.ToolCall or ChatMessageType.Reasoning,
ReasoningId = ReasoningId,
Model = Model,
OriginalContent = OriginalContent,
ImagePath = ImagePath,
ImageDataUri = ImageDataUri,
Caption = Caption
};
return msg;
}
public static ChatMessageEntity FromChatMessage(ChatMessage msg, string sessionId, int orderIndex)
{
return new ChatMessageEntity
{
SessionId = sessionId,
OrderIndex = orderIndex,
MessageType = msg.MessageType.ToString(),
Content = msg.Content,
ToolName = msg.ToolName,
ToolCallId = msg.ToolCallId,
ToolInput = msg.ToolInput,
IsComplete = msg.IsComplete,
IsSuccess = msg.IsSuccess,
ReasoningId = msg.ReasoningId,
Timestamp = msg.Timestamp,
Model = msg.Model,
OriginalContent = msg.OriginalContent,
ImagePath = msg.ImagePath,
ImageDataUri = msg.ImageDataUri,
Caption = msg.Caption
};
}
}
public class ChatDatabase : IChatDatabase
{
private static SQLiteAsyncConnection? _db;
private static readonly SemaphoreSlim _initLock = new(1, 1);
private static string? _dbPath;
private static string DbPath => _dbPath ??= GetDbPath();
private static string GetDbPath()
{
try
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
if (string.IsNullOrEmpty(home))
home = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return Path.Combine(home, ".polypilot", "chat_history.db");
}
catch
{
return Path.Combine(Path.GetTempPath(), ".polypilot", "chat_history.db");
}
}
public ChatDatabase()
{
}
/// <summary>
/// Override DB path for testing. Clears any cached connection.
/// </summary>
internal static void SetDbPathForTesting(string path)
{
_dbPath = path;
var old = _db;
_db = null;
CloseSynchronouslyForTesting(old);
}
/// <summary>
/// Reset the cached connection (for testing error recovery).
/// </summary>
internal void ResetConnection()
{
var old = _db;
_db = null;
CloseSynchronouslyForTesting(old);
}
private async Task<SQLiteAsyncConnection> GetConnectionAsync()
{
if (_db != null) return _db;
await _initLock.WaitAsync();
try
{
if (_db != null) return _db;
var dir = Path.GetDirectoryName(DbPath)!;
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
var conn = new SQLiteAsyncConnection(DbPath);
try
{
await conn.CreateTableAsync<ChatMessageEntity>();
// Create index for fast session + order lookups
await conn.ExecuteAsync(
"CREATE INDEX IF NOT EXISTS idx_session_order ON ChatMessageEntity (SessionId, OrderIndex)");
_db = conn;
return conn;
}
catch
{
try { await conn.CloseAsync(); } catch { }
throw;
}
}
finally
{
_initLock.Release();
}
}
/// <summary>
/// Check if a session has any stored messages.
/// </summary>
public async Task<bool> HasMessagesAsync(string sessionId)
{
SQLiteAsyncConnection? db = null;
try
{
db = await GetConnectionAsync();
var count = await db.ExecuteScalarAsync<int>(
"SELECT COUNT(*) FROM ChatMessageEntity WHERE SessionId = ?", sessionId);
return count > 0;
}
catch (Exception ex)
{
LogError("HasMessagesAsync", ex, db);
return false;
}
}
/// <summary>
/// Get total message count for a session.
/// </summary>
public async Task<int> GetMessageCountAsync(string sessionId)
{
SQLiteAsyncConnection? db = null;
try
{
db = await GetConnectionAsync();
return await db.ExecuteScalarAsync<int>(
"SELECT COUNT(*) FROM ChatMessageEntity WHERE SessionId = ?", sessionId);
}
catch (Exception ex)
{
LogError("GetMessageCountAsync", ex, db);
return 0;
}
}
/// <summary>
/// Load a page of messages (newest first by default, returned in chronological order).
/// </summary>
public async Task<List<ChatMessage>> GetMessagesAsync(string sessionId, int limit = 50, int offset = 0)
{
SQLiteAsyncConnection? db = null;
try
{
db = await GetConnectionAsync();
var total = await GetMessageCountAsync(sessionId);
// We want the LAST `limit` messages starting from offset from the end
var skipFromStart = Math.Max(0, total - offset - limit);
var take = Math.Min(limit, total - offset);
if (take <= 0) return new List<ChatMessage>();
var entities = await db.QueryAsync<ChatMessageEntity>(
"SELECT * FROM ChatMessageEntity WHERE SessionId = ? ORDER BY OrderIndex ASC LIMIT ? OFFSET ?",
sessionId, take, skipFromStart);
return entities.Select(e => e.ToChatMessage()).ToList();
}
catch (Exception ex)
{
LogError("GetMessagesAsync", ex, db);
return new List<ChatMessage>();
}
}
/// <summary>
/// Load ALL messages for a session (for smaller sessions or when needed).
/// </summary>
public async Task<List<ChatMessage>> GetAllMessagesAsync(string sessionId)
{
SQLiteAsyncConnection? db = null;
try
{
db = await GetConnectionAsync();
var entities = await db.Table<ChatMessageEntity>()
.Where(e => e.SessionId == sessionId)
.OrderBy(e => e.OrderIndex)
.ToListAsync();
return entities.Select(e => e.ToChatMessage()).ToList();
}
catch (Exception ex)
{
LogError("GetAllMessagesAsync", ex, db);
return new List<ChatMessage>();
}
}
/// <summary>
/// Append a single message to a session's history.
/// </summary>
public async Task<int> AddMessageAsync(string sessionId, ChatMessage msg)
{
SQLiteAsyncConnection? db = null;
try
{
db = await GetConnectionAsync();
var maxOrder = await db.ExecuteScalarAsync<int>(
"SELECT COALESCE(MAX(OrderIndex), -1) FROM ChatMessageEntity WHERE SessionId = ?", sessionId);
var entity = ChatMessageEntity.FromChatMessage(msg, sessionId, maxOrder + 1);
await db.InsertAsync(entity);
return entity.Id;
}
catch (Exception ex)
{
LogError("AddMessageAsync", ex, db);
return -1;
}
}
/// <summary>
/// Update a tool call message when it completes.
/// </summary>
public async Task UpdateToolCompleteAsync(string sessionId, string toolCallId, string content, bool isSuccess)
{
SQLiteAsyncConnection? db = null;
try
{
db = await GetConnectionAsync();
await db.ExecuteAsync(
"UPDATE ChatMessageEntity SET Content = ?, IsComplete = 1, IsSuccess = ? WHERE SessionId = ? AND ToolCallId = ?",
content, isSuccess, sessionId, toolCallId);
}
catch (Exception ex)
{
LogError("UpdateToolCompleteAsync", ex, db);
}
}
/// <summary>
/// Update reasoning message content (appending delta text).
/// </summary>
public async Task UpdateReasoningContentAsync(string sessionId, string reasoningId, string content, bool isComplete)
{
SQLiteAsyncConnection? db = null;
try
{
db = await GetConnectionAsync();
await db.ExecuteAsync(
"UPDATE ChatMessageEntity SET Content = ?, IsComplete = ? WHERE SessionId = ? AND ReasoningId = ?",
content, isComplete, sessionId, reasoningId);
}
catch (Exception ex)
{
LogError("UpdateReasoningContentAsync", ex, db);
}
}
/// <summary>
/// Bulk insert messages from events.jsonl parsing (for initial migration).
/// </summary>
public async Task BulkInsertAsync(string sessionId, List<ChatMessage> messages)
{
SQLiteAsyncConnection? db = null;
try
{
db = await GetConnectionAsync();
var entities = messages.Select((m, i) => ChatMessageEntity.FromChatMessage(m, sessionId, i)).ToList();
await db.RunInTransactionAsync(tran =>
{
tran.Execute("DELETE FROM ChatMessageEntity WHERE SessionId = ?", sessionId);
tran.InsertAll(entities);
});
}
catch (Exception ex)
{
LogError("BulkInsertAsync", ex, db);
}
}
/// <summary>
/// Clear all messages for a session.
/// </summary>
public async Task ClearSessionAsync(string sessionId)
{
SQLiteAsyncConnection? db = null;
try
{
db = await GetConnectionAsync();
await db.ExecuteAsync("DELETE FROM ChatMessageEntity WHERE SessionId = ?", sessionId);
}
catch (Exception ex)
{
LogError("ClearSessionAsync", ex, db);
}
}
private void LogError(string method, Exception ex, SQLiteAsyncConnection? failedConn = null)
{
// Only evict _db if it still points to the failed connection — prevents
// closing a healthy replacement created by a concurrent thread.
if (failedConn != null)
{
if (Interlocked.CompareExchange(ref _db!, null!, failedConn) == failedConn)
ObserveClose(failedConn);
}
// When failedConn is null, GetConnectionAsync already cleaned up the failed
// connection before throwing — _db was never set, so there is nothing to evict.
System.Diagnostics.Debug.WriteLine($"[ChatDatabase] {method} failed: {ex.Message}");
}
/// <summary>
/// Fire-and-forget close that observes faults so they don't become
/// unobserved task exceptions. Same class of bug this PR fixes.
/// </summary>
private static void ObserveClose(SQLiteAsyncConnection? conn)
{
if (conn == null) return;
try
{
conn.CloseAsync().ContinueWith(
static t => System.Diagnostics.Debug.WriteLine(
$"[ChatDatabase] CloseAsync failed: {t.Exception?.GetBaseException().Message}"),
TaskContinuationOptions.OnlyOnFaulted);
}
catch { /* sync throw from CloseAsync itself */ }
}
private static void CloseSynchronouslyForTesting(SQLiteAsyncConnection? conn)
{
if (conn == null) return;
try
{
conn.CloseAsync().GetAwaiter().GetResult();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[ChatDatabase] Test close failed: {ex.Message}");
}
}
}