-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnowledgeCache.cpp
More file actions
185 lines (156 loc) · 6.66 KB
/
Copy pathKnowledgeCache.cpp
File metadata and controls
185 lines (156 loc) · 6.66 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
#define NOMINMAX
#include "KnowledgeCache.h"
#include <cpr/cpr.h>
#include <iostream>
void KnowledgeCache::reloadGuild(dpp::snowflake guild_id, sqlite3* db) {
std::vector<KnowledgeSource> sources = getSources(db, guild_id);
std::string combined_text = "";
for (const auto& source : sources) {
std::cout << " Fetching knowledge source '" << source.name << "' (" << source.url << ")..." << std::endl;
cpr::Response r = cpr::Get(
cpr::Url{source.url},
cpr::Header{{"User-Agent", "VisorBot/1.0"}},
cpr::ConnectTimeout{5000},
cpr::Timeout{15000}
);
if (r.error.code != cpr::ErrorCode::OK) {
std::cerr << " Failed to fetch '" << source.name << "': " << r.error.message << std::endl;
continue;
}
if (r.status_code == 200) {
combined_text += "\n=== SOURCE: " + source.name + " ===\n";
combined_text += r.text + "\n";
} else {
std::cerr << " Source '" << source.name << "' returned HTTP " << r.status_code << std::endl;
}
}
std::lock_guard<std::mutex> lock(cache_mutex);
cache[guild_id] = combined_text;
}
void KnowledgeCache::reloadAll(sqlite3* db) {
std::string sql = "SELECT DISTINCT GuildID FROM KnowledgeSources;";
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char* guild_text = sqlite3_column_text(stmt, 0);
if (guild_text) {
try {
dpp::snowflake guild_id = std::stoull(reinterpret_cast<const char*>(guild_text));
std::cout << "Reloading knowledge sources for guild " << guild_id << "..." << std::endl;
reloadGuild(guild_id, db);
} catch (...) {}
}
}
}
sqlite3_finalize(stmt);
}
std::string KnowledgeCache::getCombinedContext(dpp::snowflake guild_id) const {
std::lock_guard<std::mutex> lock(cache_mutex);
auto it = cache.find(guild_id);
if (it != cache.end()) {
return it->second;
}
return "";
}
bool KnowledgeCache::addSource(sqlite3* db, dpp::snowflake guild_id, const std::string& name, const std::string& url) {
std::string sql = "INSERT INTO KnowledgeSources (GuildID, SourceName, Url) VALUES (?, ?, ?);";
sqlite3_stmt* stmt = nullptr;
bool success = false;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
std::string guild_str = std::to_string(guild_id);
sqlite3_bind_text(stmt, 1, guild_str.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, name.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, url.c_str(), -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_DONE) {
success = true;
}
}
sqlite3_finalize(stmt);
return success;
}
bool KnowledgeCache::removeSource(sqlite3* db, int source_id) {
std::string sql = "DELETE FROM KnowledgeSources WHERE SourceID = ?;";
sqlite3_stmt* stmt = nullptr;
bool success = false;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
sqlite3_bind_int(stmt, 1, source_id);
if (sqlite3_step(stmt) == SQLITE_DONE) {
success = true;
}
}
sqlite3_finalize(stmt);
return success;
}
std::vector<KnowledgeSource> KnowledgeCache::getSources(sqlite3* db, dpp::snowflake guild_id) {
std::vector<KnowledgeSource> sources;
std::string sql = "SELECT SourceID, SourceName, Url FROM KnowledgeSources WHERE GuildID = ?;";
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
std::string guild_str = std::to_string(guild_id);
sqlite3_bind_text(stmt, 1, guild_str.c_str(), -1, SQLITE_STATIC);
while (sqlite3_step(stmt) == SQLITE_ROW) {
int id = sqlite3_column_int(stmt, 0);
const unsigned char* name = sqlite3_column_text(stmt, 1);
const unsigned char* url = sqlite3_column_text(stmt, 2);
if (name && url) {
sources.push_back({
id,
reinterpret_cast<const char*>(name),
reinterpret_cast<const char*>(url)
});
}
}
}
sqlite3_finalize(stmt);
return sources;
}
bool KnowledgeCache::findSourceByUrl(sqlite3* db, dpp::snowflake guild_id, const std::string& url, std::string& out_name) {
std::string sql = "SELECT SourceName FROM KnowledgeSources WHERE GuildID = ? AND Url = ?;";
sqlite3_stmt* stmt = nullptr;
bool found = false;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
std::string guild_str = std::to_string(guild_id);
sqlite3_bind_text(stmt, 1, guild_str.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, url.c_str(), -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char* name = sqlite3_column_text(stmt, 0);
if (name) {
out_name = reinterpret_cast<const char*>(name);
found = true;
}
}
}
sqlite3_finalize(stmt);
return found;
}
bool KnowledgeCache::sourceNameExists(sqlite3* db, dpp::snowflake guild_id, const std::string& name) {
std::string sql = "SELECT 1 FROM KnowledgeSources WHERE GuildID = ? AND SourceName = ?;";
sqlite3_stmt* stmt = nullptr;
bool found = false;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
std::string guild_str = std::to_string(guild_id);
sqlite3_bind_text(stmt, 1, guild_str.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, name.c_str(), -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
found = true;
}
}
sqlite3_finalize(stmt);
return found;
}
bool KnowledgeCache::updateSourceUrl(sqlite3* db, dpp::snowflake guild_id, const std::string& name, const std::string& url) {
std::string sql = "UPDATE KnowledgeSources SET Url = ? WHERE GuildID = ? AND SourceName = ?;";
sqlite3_stmt* stmt = nullptr;
bool success = false;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) {
std::string guild_str = std::to_string(guild_id);
sqlite3_bind_text(stmt, 1, url.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, guild_str.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, name.c_str(), -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_DONE) {
success = true;
}
}
sqlite3_finalize(stmt);
return success;
}