131 lines
4.3 KiB
C++
131 lines
4.3 KiB
C++
#include "google/adk/session.h"
|
|||
|
|
|
||
|
|
#include <algorithm>
|
||
|
|
#include <cctype>
|
||
|
|
#include <set>
|
||
|
|
|
||
|
|
#include "google/adk/caps.h"
|
||
|
|
#include "google/adk/error.h"
|
||
|
|
#include "google/resource.h"
|
||
|
|
|
||
|
|
namespace molo::google::adk {
|
||
|
|
|
||
|
|
using clients::body::Json;
|
||
|
|
|
||
|
|
Json Event::to_json() const {
|
||
|
|
Json o = Json::object();
|
||
|
|
o.set("author", Json(author));
|
||
|
|
o.set("content", content.to_json());
|
||
|
|
o.set("final", Json(final));
|
||
|
|
return o;
|
||
|
|
}
|
||
|
|
|
||
|
|
Session InMemorySessionService::create(const std::string& id,
|
||
|
|
const std::string& user_id) {
|
||
|
|
if (!valid_segment(id)) throw AdkError::invalid("invalid session id");
|
||
|
|
if (!valid_segment(user_id)) throw AdkError::invalid("invalid user id");
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
if (sessions_.count(id)) throw AdkError::invalid("session exists: " + id);
|
||
|
|
while (sessions_.size() >= AdkCaps::kMaxSessions && !order_.empty()) {
|
||
|
|
sessions_.erase(order_.front());
|
||
|
|
order_.erase(order_.begin());
|
||
|
|
}
|
||
|
|
Session s;
|
||
|
|
s.id = id;
|
||
|
|
s.user_id = user_id;
|
||
|
|
sessions_[id] = s;
|
||
|
|
order_.push_back(id);
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::optional<Session> InMemorySessionService::get(const std::string& id) {
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
auto it = sessions_.find(id);
|
||
|
|
if (it == sessions_.end()) return std::nullopt;
|
||
|
|
return it->second;
|
||
|
|
}
|
||
|
|
|
||
|
|
void InMemorySessionService::save(const Session& session) {
|
||
|
|
if (!session.state.is_object()) throw AdkError::invalid("state must be an object");
|
||
|
|
if (session.state.as_object().size() > AdkCaps::kMaxStateKeys)
|
||
|
|
throw AdkError::invalid("session state over key cap");
|
||
|
|
for (const auto& [k, v] : session.state.as_object())
|
||
|
|
if (v.dump().size() > AdkCaps::kMaxStateValueBytes)
|
||
|
|
throw AdkError::invalid("session state value over byte cap: " + k);
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
auto it = sessions_.find(session.id);
|
||
|
|
if (it == sessions_.end()) throw AdkError::invalid("unknown session: " + session.id);
|
||
|
|
it->second = session;
|
||
|
|
auto& ev = it->second.events;
|
||
|
|
if (ev.size() > AdkCaps::kMaxEventsPerSession)
|
||
|
|
ev.erase(ev.begin(), ev.begin() + (ev.size() - AdkCaps::kMaxEventsPerSession));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool InMemorySessionService::remove(const std::string& id) {
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
order_.erase(std::remove(order_.begin(), order_.end(), id), order_.end());
|
||
|
|
return sessions_.erase(id) > 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::size_t InMemorySessionService::size() const {
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
return sessions_.size();
|
||
|
|
}
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::set<std::string> words(const std::string& text) {
|
||
|
|
std::set<std::string> out;
|
||
|
|
std::string cur;
|
||
|
|
for (char ch : text) {
|
||
|
|
if (std::isalnum(static_cast<unsigned char>(ch))) {
|
||
|
|
cur += static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
|
||
|
|
} else {
|
||
|
|
if (cur.size() > 2) out.insert(cur);
|
||
|
|
cur.clear();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (cur.size() > 2) out.insert(cur);
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
void InMemoryMemoryService::add_session(const Session& session) {
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
for (const Event& e : session.events) {
|
||
|
|
if (!(e.author == "user" || e.final)) continue;
|
||
|
|
std::string text = e.content.text();
|
||
|
|
if (text.empty()) continue;
|
||
|
|
entries_.push_back({session.user_id, session.id, e.author, text});
|
||
|
|
}
|
||
|
|
if (entries_.size() > AdkCaps::kMaxMemoryEntries)
|
||
|
|
entries_.erase(entries_.begin(),
|
||
|
|
entries_.begin() + (entries_.size() - AdkCaps::kMaxMemoryEntries));
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<MemoryEntry> InMemoryMemoryService::search(
|
||
|
|
const std::string& user_id, const std::string& query, std::size_t limit) {
|
||
|
|
limit = std::min(limit, AdkCaps::kMaxMemoryResults);
|
||
|
|
std::set<std::string> q = words(query);
|
||
|
|
std::vector<std::pair<std::size_t, const MemoryEntry*>> scored;
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
for (const MemoryEntry& e : entries_) {
|
||
|
|
if (e.user_id != user_id) continue;
|
||
|
|
std::size_t score = 0;
|
||
|
|
for (const std::string& w : words(e.text)) score += q.count(w);
|
||
|
|
if (score > 0) scored.push_back({score, &e});
|
||
|
|
}
|
||
|
|
std::stable_sort(scored.begin(), scored.end(),
|
||
|
|
[](const auto& a, const auto& b) { return a.first > b.first; });
|
||
|
|
std::vector<MemoryEntry> out;
|
||
|
|
for (std::size_t i = 0; i < scored.size() && i < limit; ++i)
|
||
|
|
out.push_back(*scored[i].second);
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::size_t InMemoryMemoryService::size() const {
|
||
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
|
return entries_.size();
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google::adk
|