#include "./message_prompt_builder.hpp" #include "./rpbot.hpp" #include #include #include bool MessagePromptBuilder::buildNameLookup(void) { const auto& cr = _cs.registry(); if (cr.all_of(_c)) { // group rpbot const auto& subs = cr.get(_c).subs; // should include self for (const auto sub_c : subs) { if (cr.all_of(sub_c)) { names[sub_c] = cr.get(sub_c).name; } } } else { // pm rpbot if (cr.all_of(_c)) { names[_c] = cr.get(_c).name; } else { std::cerr << "RPBot error: other missing name\n"; return false; } if (cr.all_of(_c)) { const auto self = cr.get(_c).self; if (cr.all_of(self)) { names[self] = cr.get(self).name; } else { std::cerr << "RPBot error: self missing name\n"; return false; } } else { std::cerr << "RPBot error: contact missing self\n"; return false; } } return true; } std::string MessagePromptBuilder::buildPromptMessageHistory(void) { auto* mr = _rmm.get(_c); assert(mr); std::string prompt; auto view = mr->view(); for (auto view_it = view.rbegin(), view_last = view.rend(); view_it != view_last; view_it++) { const Message3 e = *view_it; // manually filter ("reverse" iteration <.<) // TODO: add mesagetext? if (!mr->all_of(e)) { continue; } //Message::Components::ContactFrom& c_from = mr->get(e); //Message::Components::ContactTo& c_to = msg_reg.get(e); //Message::Components::Timestamp ts = view.get(e); prompt += "\n"; prompt += buildPromptMessage({*mr, e}); } return prompt; } std::string MessagePromptBuilder::buildPromptMessage(const Message3Handle m) { if (!m.all_of()) { // TODO: case for transfers return ""; } const auto line_prefix = promptMessagePrefixSimple(m) + ": "; // TODO: trim std::string message_lines = line_prefix + m.get().text; for (auto nlpos = message_lines.find('\n'); nlpos != std::string::npos; nlpos = message_lines.find('\n', nlpos+1)) { message_lines.insert(nlpos+1, line_prefix); nlpos += line_prefix.size(); } // TODO: cache as comp return message_lines; } std::string MessagePromptBuilder::promptMessagePrefixSimple(const Message3Handle m) { const Contact4 from = m.get().c; if (names.count(from)) { return std::string{names[from]}; } else { return ""; } } std::string MessagePromptBuilder::promptMessagePrefixDirected(const Message3Handle m) { // with both contacts (eg: "Name1 to Name2"; or "Name1 to Everyone" const Contact4 from = m.get().c; const Contact4 to = m.get().c; std::string res; if (names.count(from)) { res = std::string{names[from]}; } else { res = ""; } res += " to "; if (names.count(to)) { return std::string{names[to]}; } else { return ""; } return res; }