From ffb0719be58c279fe881118e0e267405a9bbdfb8 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 27 Jan 2024 17:29:54 +0100 Subject: [PATCH] rename next state and prep some prompt builder features --- .../rpbot/message_prompt_builder.cpp | 34 ++++++++++++++++--- .../rpbot/message_prompt_builder.hpp | 4 +++ src/solanaceae/rpbot/rpbot.cpp | 14 ++++---- src/solanaceae/rpbot/rpbot.hpp | 2 +- src/solanaceae/rpbot/rpbot_commands.cpp | 16 ++++----- src/solanaceae/rpbot/rpbot_states.hpp | 4 +-- 6 files changed, 52 insertions(+), 22 deletions(-) diff --git a/src/solanaceae/rpbot/message_prompt_builder.cpp b/src/solanaceae/rpbot/message_prompt_builder.cpp index dd091b2..e957297 100644 --- a/src/solanaceae/rpbot/message_prompt_builder.cpp +++ b/src/solanaceae/rpbot/message_prompt_builder.cpp @@ -72,8 +72,7 @@ std::string MessagePromptBuilder::buildPromptMessage(const Message3Handle m) { return ""; } - // TODO: cache as comp - const auto line_prefix = promptMessagePrefixSimple(m); + const auto line_prefix = promptMessagePrefixSimple(m) + ": "; // TODO: trim std::string message_lines = line_prefix + m.get().text; @@ -82,15 +81,42 @@ std::string MessagePromptBuilder::buildPromptMessage(const Message3Handle m) { nlpos += line_prefix.size(); } + // TODO: cache as comp + return message_lines; } std::string MessagePromptBuilder::promptMessagePrefixSimple(const Message3Handle m) { const Contact3 from = m.get().c; if (names.count(from)) { - return std::string{names[from]} + ": "; + return std::string{names[from]}; } else { - return ": "; + return ""; } } +std::string MessagePromptBuilder::promptMessagePrefixDirected(const Message3Handle m) { + // with both contacts (eg: "Name1 to Name2"; or "Name1 to Everyone" + + const Contact3 from = m.get().c; + const Contact3 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; +} + diff --git a/src/solanaceae/rpbot/message_prompt_builder.hpp b/src/solanaceae/rpbot/message_prompt_builder.hpp index b671007..61de8ef 100644 --- a/src/solanaceae/rpbot/message_prompt_builder.hpp +++ b/src/solanaceae/rpbot/message_prompt_builder.hpp @@ -24,5 +24,9 @@ struct MessagePromptBuilder { // generate prompt prefix (just "name:") std::string promptMessagePrefixSimple(const Message3Handle m); + + // with both contacts (eg: "Name1 to Name2:"; or "Name1 to Everyone:" + // this maps to private messages, might need fewshot of private or explainer system prompt + std::string promptMessagePrefixDirected(const Message3Handle m); }; diff --git a/src/solanaceae/rpbot/rpbot.cpp b/src/solanaceae/rpbot/rpbot.cpp index 6747365..9df6879 100644 --- a/src/solanaceae/rpbot/rpbot.cpp +++ b/src/solanaceae/rpbot/rpbot.cpp @@ -18,7 +18,7 @@ #include template<> -void RPBot::stateTransition(const Contact3 c, const StateIdle& from, StateNext& to) { +void RPBot::stateTransition(const Contact3 c, const StateIdle& from, StateNextActor& to) { // collect promp MessagePromptBuilder mpb{_cr, c, _rmm, {}}; @@ -95,12 +95,12 @@ void RPBot::stateTransition(const Contact3 c, const StateIdle& from, StateNext& } template<> -void RPBot::stateTransition(const Contact3, const StateNext&, StateIdle& to) { +void RPBot::stateTransition(const Contact3, const StateNextActor&, StateIdle& to) { to.timeout = std::uniform_real_distribution<>{10.f, 45.f}(_rng); } template<> -void RPBot::stateTransition(const Contact3 c, const StateNext& from, StateGenerateMsg& to) { +void RPBot::stateTransition(const Contact3 c, const StateNextActor& from, StateGenerateMsg& to) { to.prompt = from.prompt; // TODO: move from? assert(_cr.all_of(c)); const Contact3 self = _cr.get(c).self; @@ -177,7 +177,7 @@ float RPBot::doAllIdle(float time_delta) { min_tick_interval = 0.1f; // transition to Next - emplaceStateTransition(_cr, c, state); + emplaceStateTransition(_cr, c, state); } else { // check-in in another 15-45s state.timeout = std::uniform_real_distribution<>{15.f, 45.f}(_rng); @@ -198,9 +198,9 @@ float RPBot::doAllIdle(float time_delta) { float RPBot::doAllNext(float) { float min_tick_interval = std::numeric_limits::max(); std::vector to_remove; - auto view = _cr.view(); + auto view = _cr.view(); - view.each([this, &to_remove, &min_tick_interval](const Contact3 c, StateNext& state) { + view.each([this, &to_remove, &min_tick_interval](const Contact3 c, StateNextActor& state) { // TODO: how to timeout? if (state.future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) { to_remove.push_back(c); @@ -224,7 +224,7 @@ float RPBot::doAllNext(float) { } }); - _cr.remove(to_remove.cbegin(), to_remove.cend()); + _cr.remove(to_remove.cbegin(), to_remove.cend()); return min_tick_interval; } diff --git a/src/solanaceae/rpbot/rpbot.hpp b/src/solanaceae/rpbot/rpbot.hpp index 6b6a8de..0de6563 100644 --- a/src/solanaceae/rpbot/rpbot.hpp +++ b/src/solanaceae/rpbot/rpbot.hpp @@ -12,7 +12,7 @@ // fwd struct StateIdle; -struct StateNext; +struct StateNextActor; struct StateGenerateMsg; struct StateTimingCheck; diff --git a/src/solanaceae/rpbot/rpbot_commands.cpp b/src/solanaceae/rpbot/rpbot_commands.cpp index 4ca76b2..23539a2 100644 --- a/src/solanaceae/rpbot/rpbot_commands.cpp +++ b/src/solanaceae/rpbot/rpbot_commands.cpp @@ -20,14 +20,14 @@ void RPBot::registerCommands(void) { if (params.empty()) { // contact_to should be the contact this is for - if (_cr.any_of(contact_to)) { + if (_cr.any_of(contact_to)) { _rmm.sendText( contact_from, "error: already running" ); return true; } - if (_cr.any_of(contact_from)) { + if (_cr.any_of(contact_from)) { _rmm.sendText( contact_from, "error: already running" @@ -96,7 +96,7 @@ void RPBot::registerCommands(void) { if (params.empty()) { // contact_to should be the contact this is for - if (_cr.any_of(contact_to)) { + if (_cr.any_of(contact_to)) { _cr.emplace_or_replace(contact_to); _rmm.sendText( contact_from, @@ -104,7 +104,7 @@ void RPBot::registerCommands(void) { ); return true; } - if (_cr.any_of(contact_from)) { + if (_cr.any_of(contact_from)) { _cr.emplace_or_replace(contact_from); _rmm.sendText( contact_from, @@ -133,7 +133,7 @@ void RPBot::registerCommands(void) { auto view = _cr.view(); for (auto it = view.begin(), it_end = view.end(); it != it_end; it++) { if (view.get(*it).data == id_bin) { - if (_cr.any_of(*it)) { + if (_cr.any_of(*it)) { _cr.emplace_or_replace(*it); _rmm.sendText( contact_from, @@ -170,7 +170,7 @@ void RPBot::registerCommands(void) { if (params.empty()) { // contact_to should be the contact this is for - if (_cr.any_of(contact_to)) { + if (_cr.any_of(contact_to)) { if (_cr.all_of(contact_to)) { _cr.get(contact_to).force = true; _cr.get(contact_to).timeout = 2.f; @@ -181,7 +181,7 @@ void RPBot::registerCommands(void) { } return true; } - if (_cr.any_of(contact_from)) { + if (_cr.any_of(contact_from)) { if (_cr.all_of(contact_from)) { _cr.get(contact_from).force = true; _cr.get(contact_from).timeout = 2.f; @@ -213,7 +213,7 @@ void RPBot::registerCommands(void) { auto view = _cr.view(); for (auto it = view.begin(), it_end = view.end(); it != it_end; it++) { if (view.get(*it).data == id_bin) { - if (_cr.any_of(*it)) { + if (_cr.any_of(*it)) { if (_cr.all_of(*it)) { _cr.get(*it).force = true; _cr.get(*it).timeout = 2.f; diff --git a/src/solanaceae/rpbot/rpbot_states.hpp b/src/solanaceae/rpbot/rpbot_states.hpp index 64dabe6..92e5f70 100644 --- a/src/solanaceae/rpbot/rpbot_states.hpp +++ b/src/solanaceae/rpbot/rpbot_states.hpp @@ -17,8 +17,8 @@ struct StateIdle { }; // determines if self should generate a message -struct StateNext { - static constexpr const char* name {"StateNext"}; +struct StateNextActor { + static constexpr const char* name {"StateNextActor"}; std::string prompt; std::vector possible_names;