Files
moloapp/src/cli/ai/ai_session.cpp
T
retoor 3c4334cec9 feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
2026-10-23 18:20:04 +02:00

59 lines
1.7 KiB
C++

#include "ai/ai_session.hpp"
#include <fstream>
#include "caps.hpp"
namespace molo::ai {
AiSession::AiSession(std::string name)
: name_(std::move(name)), started_(std::chrono::steady_clock::now()) {}
bool AiSession::append(const std::string& role, const std::string& content, std::string& error) {
if (expired()) {
error = "session exceeded 300s cap";
return false;
}
if (transcript_bytes_ + role.size() + content.size() > caps::kAiTranscriptMaxBytes) {
error = "transcript exceeded 8 MB cap";
return false;
}
messages_.push_back(ChatMessage{role, content});
transcript_bytes_ += role.size() + content.size();
return true;
}
bool AiSession::expired() const {
auto elapsed = std::chrono::steady_clock::now() - started_;
return elapsed > std::chrono::seconds(caps::kAiSessionCapSec);
}
std::size_t AiSession::request_write(const std::string& path, const std::string& content) {
if (actions_ >= caps::kAiMaxActions) return static_cast<std::size_t>(-1);
++actions_;
pending_.push_back(PendingWrite{path, content});
return pending_.size() - 1;
}
bool AiSession::confirm_write(std::size_t index, std::string& error) {
if (index >= pending_.size()) {
error = "no such pending write";
return false;
}
const PendingWrite& write = pending_[index];
std::ofstream out(write.path, std::ios::binary | std::ios::trunc);
if (!out.good()) {
error = "cannot write " + write.path;
return false;
}
out << write.content;
pending_.erase(pending_.begin() + static_cast<long>(index));
return true;
}
void AiSession::reject_write(std::size_t index) {
if (index < pending_.size()) pending_.erase(pending_.begin() + static_cast<long>(index));
}
} // namespace molo::ai