-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_store.cpp
More file actions
390 lines (330 loc) · 9.94 KB
/
Copy pathmemory_store.cpp
File metadata and controls
390 lines (330 loc) · 9.94 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
#include "memory_store.hpp"
#include "sqlite3.h"
#include <cctype>
#include <ctime>
namespace droidcli::cli {
namespace {
core::String make_timestamp()
{
const std::time_t now = std::time(nullptr);
std::tm local_time{};
#ifdef _WIN32
localtime_s(&local_time, &now);
#else
localtime_r(&now, &local_time);
#endif
char buffer[32] = {};
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &local_time);
return core::String(buffer);
}
// sqlite3_bind_text/column_text take raw C strings; wrapping every call site
// in this pattern keeps the SQLITE_TRANSIENT lifetime rule (SQLite copies
// the string immediately, so a temporary core::String going out of scope
// right after the bind call is safe) explicit at each use.
bool bind_text(sqlite3_stmt* statement, const int index, const core::String& value)
{
return sqlite3_bind_text(statement, index, value.c_str(), -1, SQLITE_TRANSIENT) == SQLITE_OK;
}
} // namespace
MemoryStore::~MemoryStore()
{
if (db_ != nullptr)
{
sqlite3_close(db_);
db_ = nullptr;
}
}
bool MemoryStore::open(const core::String& db_path)
{
if (db_ != nullptr)
{
sqlite3_close(db_);
db_ = nullptr;
}
if (sqlite3_open(db_path.c_str(), &db_) != SQLITE_OK)
{
if (db_ != nullptr)
{
sqlite3_close(db_);
db_ = nullptr;
}
return false;
}
static const char* kCreateTable =
"CREATE TABLE IF NOT EXISTS memory_entries ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"session_id TEXT NOT NULL,"
"hop_index INTEGER NOT NULL,"
"role TEXT NOT NULL,"
"content TEXT NOT NULL,"
"created_at TEXT NOT NULL"
");"
"CREATE INDEX IF NOT EXISTS idx_memory_entries_session "
"ON memory_entries(session_id, hop_index);"
"CREATE TABLE IF NOT EXISTS command_lessons ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"tool TEXT NOT NULL,"
"broken TEXT NOT NULL,"
"failure_reason TEXT NOT NULL,"
"working TEXT NOT NULL,"
"lesson TEXT NOT NULL,"
"created_at TEXT NOT NULL"
");"
"CREATE INDEX IF NOT EXISTS idx_command_lessons_tool "
"ON command_lessons(tool);"
"CREATE TABLE IF NOT EXISTS known_locations ("
"name TEXT PRIMARY KEY COLLATE NOCASE,"
"resolved_path TEXT NOT NULL,"
"created_at TEXT NOT NULL,"
"updated_at TEXT NOT NULL"
");";
char* error_message = nullptr;
if (sqlite3_exec(db_, kCreateTable, nullptr, nullptr, &error_message) != SQLITE_OK)
{
sqlite3_free(error_message);
sqlite3_close(db_);
db_ = nullptr;
return false;
}
return true;
}
bool MemoryStore::is_open() const
{
return db_ != nullptr;
}
bool MemoryStore::append(
const core::String& session_id, const int32_t hop_index, const core::String& role, const core::String& content)
{
if (db_ == nullptr)
{
return false;
}
static const char* kInsert =
"INSERT INTO memory_entries (session_id, hop_index, role, content, created_at) "
"VALUES (?, ?, ?, ?, ?);";
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db_, kInsert, -1, &statement, nullptr) != SQLITE_OK)
{
return false;
}
bool ok = bind_text(statement, 1, session_id)
&& sqlite3_bind_int(statement, 2, hop_index) == SQLITE_OK
&& bind_text(statement, 3, role)
&& bind_text(statement, 4, content)
&& bind_text(statement, 5, make_timestamp());
if (ok)
{
ok = sqlite3_step(statement) == SQLITE_DONE;
}
sqlite3_finalize(statement);
return ok;
}
core::Array<MemoryRecord> MemoryStore::load_session(const core::String& session_id) const
{
core::Array<MemoryRecord> records;
if (db_ == nullptr)
{
return records;
}
static const char* kSelect =
"SELECT hop_index, role, content, created_at FROM memory_entries "
"WHERE session_id = ? ORDER BY hop_index ASC;";
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db_, kSelect, -1, &statement, nullptr) != SQLITE_OK)
{
return records;
}
if (!bind_text(statement, 1, session_id))
{
sqlite3_finalize(statement);
return records;
}
while (sqlite3_step(statement) == SQLITE_ROW)
{
MemoryRecord record;
record.session_id = session_id;
record.hop_index = sqlite3_column_int(statement, 0);
record.role = reinterpret_cast<const char*>(sqlite3_column_text(statement, 1));
record.content = reinterpret_cast<const char*>(sqlite3_column_text(statement, 2));
record.created_at = reinterpret_cast<const char*>(sqlite3_column_text(statement, 3));
records.push_back(record);
}
sqlite3_finalize(statement);
return records;
}
bool MemoryStore::delete_session(const core::String& session_id)
{
if (db_ == nullptr)
{
return false;
}
static const char* kDelete = "DELETE FROM memory_entries WHERE session_id = ?;";
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db_, kDelete, -1, &statement, nullptr) != SQLITE_OK)
{
return false;
}
bool ok = bind_text(statement, 1, session_id);
if (ok)
{
ok = sqlite3_step(statement) == SQLITE_DONE;
}
sqlite3_finalize(statement);
return ok;
}
core::Array<core::String> MemoryStore::list_session_ids() const
{
core::Array<core::String> session_ids;
if (db_ == nullptr)
{
return session_ids;
}
static const char* kSelect =
"SELECT session_id, MAX(created_at) AS last_seen FROM memory_entries "
"GROUP BY session_id ORDER BY last_seen DESC;";
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db_, kSelect, -1, &statement, nullptr) != SQLITE_OK)
{
return session_ids;
}
while (sqlite3_step(statement) == SQLITE_ROW)
{
session_ids.push_back(reinterpret_cast<const char*>(sqlite3_column_text(statement, 0)));
}
sqlite3_finalize(statement);
return session_ids;
}
bool MemoryStore::record_lesson(
const core::String& tool,
const core::String& broken,
const core::String& failure_reason,
const core::String& working,
const core::String& lesson)
{
if (db_ == nullptr)
{
return false;
}
static const char* kInsert =
"INSERT INTO command_lessons (tool, broken, failure_reason, working, lesson, created_at) "
"VALUES (?, ?, ?, ?, ?, ?);";
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db_, kInsert, -1, &statement, nullptr) != SQLITE_OK)
{
return false;
}
bool ok = bind_text(statement, 1, tool)
&& bind_text(statement, 2, broken)
&& bind_text(statement, 3, failure_reason)
&& bind_text(statement, 4, working)
&& bind_text(statement, 5, lesson)
&& bind_text(statement, 6, make_timestamp());
if (ok)
{
ok = sqlite3_step(statement) == SQLITE_DONE;
}
sqlite3_finalize(statement);
return ok;
}
core::Array<CommandLesson> MemoryStore::search_lessons(const core::String& query, const int32_t max_results) const
{
core::Array<CommandLesson> lessons;
if (db_ == nullptr)
{
return lessons;
}
static const char* kSelect =
"SELECT id, tool, broken, failure_reason, working, lesson, created_at FROM command_lessons "
"WHERE LOWER(tool) LIKE ?1 OR LOWER(broken) LIKE ?1 OR LOWER(failure_reason) LIKE ?1 OR LOWER(lesson) LIKE ?1 "
"ORDER BY created_at DESC LIMIT ?2;";
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db_, kSelect, -1, &statement, nullptr) != SQLITE_OK)
{
return lessons;
}
core::String lowered_query;
lowered_query.reserve(query.size());
for (const unsigned char character : query)
{
lowered_query += static_cast<char>(std::tolower(character));
}
const core::String pattern = "%" + lowered_query + "%";
if (!bind_text(statement, 1, pattern) || sqlite3_bind_int(statement, 2, max_results) != SQLITE_OK)
{
sqlite3_finalize(statement);
return lessons;
}
while (sqlite3_step(statement) == SQLITE_ROW)
{
CommandLesson entry;
entry.id = sqlite3_column_int64(statement, 0);
entry.tool = reinterpret_cast<const char*>(sqlite3_column_text(statement, 1));
entry.broken = reinterpret_cast<const char*>(sqlite3_column_text(statement, 2));
entry.failure_reason = reinterpret_cast<const char*>(sqlite3_column_text(statement, 3));
entry.working = reinterpret_cast<const char*>(sqlite3_column_text(statement, 4));
entry.lesson = reinterpret_cast<const char*>(sqlite3_column_text(statement, 5));
entry.created_at = reinterpret_cast<const char*>(sqlite3_column_text(statement, 6));
lessons.push_back(entry);
}
sqlite3_finalize(statement);
return lessons;
}
bool MemoryStore::remember_location(const core::String& name, const core::String& resolved_path)
{
if (db_ == nullptr)
{
return false;
}
// ON CONFLICT upsert keyed on name (COLLATE NOCASE, from the table
// definition) - remembering the same name again updates resolved_path/
// updated_at in place instead of growing a duplicate row, and preserves
// the original created_at.
static const char* kUpsert =
"INSERT INTO known_locations (name, resolved_path, created_at, updated_at) "
"VALUES (?, ?, ?, ?) "
"ON CONFLICT(name) DO UPDATE SET resolved_path = excluded.resolved_path, updated_at = excluded.updated_at;";
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db_, kUpsert, -1, &statement, nullptr) != SQLITE_OK)
{
return false;
}
const core::String now = make_timestamp();
bool ok = bind_text(statement, 1, name)
&& bind_text(statement, 2, resolved_path)
&& bind_text(statement, 3, now)
&& bind_text(statement, 4, now);
if (ok)
{
ok = sqlite3_step(statement) == SQLITE_DONE;
}
sqlite3_finalize(statement);
return ok;
}
core::Array<KnownLocation> MemoryStore::list_locations() const
{
core::Array<KnownLocation> locations;
if (db_ == nullptr)
{
return locations;
}
static const char* kSelect =
"SELECT name, resolved_path, created_at, updated_at FROM known_locations "
"ORDER BY updated_at DESC;";
sqlite3_stmt* statement = nullptr;
if (sqlite3_prepare_v2(db_, kSelect, -1, &statement, nullptr) != SQLITE_OK)
{
return locations;
}
while (sqlite3_step(statement) == SQLITE_ROW)
{
KnownLocation location;
location.name = reinterpret_cast<const char*>(sqlite3_column_text(statement, 0));
location.resolved_path = reinterpret_cast<const char*>(sqlite3_column_text(statement, 1));
location.created_at = reinterpret_cast<const char*>(sqlite3_column_text(statement, 2));
location.updated_at = reinterpret_cast<const char*>(sqlite3_column_text(statement, 3));
locations.push_back(location);
}
sqlite3_finalize(statement);
return locations;
}
} // namespace droidcli::cli