#include "ai/ai_session.hpp" #include #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(-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(index)); return true; } void AiSession::reject_write(std::size_t index) { if (index < pending_.size()) pending_.erase(pending_.begin() + static_cast(index)); } } // namespace molo::ai