rename next state and prep some prompt builder features

This commit is contained in:
Green Sky 2024-01-27 17:29:54 +01:00
parent fec97a89c7
commit ffb0719be5
No known key found for this signature in database
6 changed files with 52 additions and 22 deletions

View File

@ -72,8 +72,7 @@ std::string MessagePromptBuilder::buildPromptMessage(const Message3Handle m) {
return ""; return "";
} }
// TODO: cache as comp const auto line_prefix = promptMessagePrefixSimple(m) + ": ";
const auto line_prefix = promptMessagePrefixSimple(m);
// TODO: trim // TODO: trim
std::string message_lines = line_prefix + m.get<Message::Components::MessageText>().text; std::string message_lines = line_prefix + m.get<Message::Components::MessageText>().text;
@ -82,15 +81,42 @@ std::string MessagePromptBuilder::buildPromptMessage(const Message3Handle m) {
nlpos += line_prefix.size(); nlpos += line_prefix.size();
} }
// TODO: cache as comp
return message_lines; return message_lines;
} }
std::string MessagePromptBuilder::promptMessagePrefixSimple(const Message3Handle m) { std::string MessagePromptBuilder::promptMessagePrefixSimple(const Message3Handle m) {
const Contact3 from = m.get<Message::Components::ContactFrom>().c; const Contact3 from = m.get<Message::Components::ContactFrom>().c;
if (names.count(from)) { if (names.count(from)) {
return std::string{names[from]} + ": "; return std::string{names[from]};
} else { } else {
return "<unk-user>: "; return "<unk-user>";
} }
} }
std::string MessagePromptBuilder::promptMessagePrefixDirected(const Message3Handle m) {
// with both contacts (eg: "Name1 to Name2"; or "Name1 to Everyone"
const Contact3 from = m.get<Message::Components::ContactFrom>().c;
const Contact3 to = m.get<Message::Components::ContactTo>().c;
std::string res;
if (names.count(from)) {
res = std::string{names[from]};
} else {
res = "<unk-user>";
}
res += " to ";
if (names.count(to)) {
return std::string{names[to]};
} else {
return "<unk-user>";
}
return res;
}

View File

@ -24,5 +24,9 @@ struct MessagePromptBuilder {
// generate prompt prefix (just "name:") // generate prompt prefix (just "name:")
std::string promptMessagePrefixSimple(const Message3Handle m); 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);
}; };

View File

@ -18,7 +18,7 @@
#include <cstdint> #include <cstdint>
template<> 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 // collect promp
MessagePromptBuilder mpb{_cr, c, _rmm, {}}; MessagePromptBuilder mpb{_cr, c, _rmm, {}};
@ -95,12 +95,12 @@ void RPBot::stateTransition(const Contact3 c, const StateIdle& from, StateNext&
} }
template<> 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); to.timeout = std::uniform_real_distribution<>{10.f, 45.f}(_rng);
} }
template<> 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? to.prompt = from.prompt; // TODO: move from?
assert(_cr.all_of<Contact::Components::Self>(c)); assert(_cr.all_of<Contact::Components::Self>(c));
const Contact3 self = _cr.get<Contact::Components::Self>(c).self; const Contact3 self = _cr.get<Contact::Components::Self>(c).self;
@ -177,7 +177,7 @@ float RPBot::doAllIdle(float time_delta) {
min_tick_interval = 0.1f; min_tick_interval = 0.1f;
// transition to Next // transition to Next
emplaceStateTransition<StateNext>(_cr, c, state); emplaceStateTransition<StateNextActor>(_cr, c, state);
} else { } else {
// check-in in another 15-45s // check-in in another 15-45s
state.timeout = std::uniform_real_distribution<>{15.f, 45.f}(_rng); 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 RPBot::doAllNext(float) {
float min_tick_interval = std::numeric_limits<float>::max(); float min_tick_interval = std::numeric_limits<float>::max();
std::vector<Contact3> to_remove; std::vector<Contact3> to_remove;
auto view = _cr.view<StateNext>(); auto view = _cr.view<StateNextActor>();
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? // TODO: how to timeout?
if (state.future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) { if (state.future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) {
to_remove.push_back(c); to_remove.push_back(c);
@ -224,7 +224,7 @@ float RPBot::doAllNext(float) {
} }
}); });
_cr.remove<StateNext>(to_remove.cbegin(), to_remove.cend()); _cr.remove<StateNextActor>(to_remove.cbegin(), to_remove.cend());
return min_tick_interval; return min_tick_interval;
} }

View File

@ -12,7 +12,7 @@
// fwd // fwd
struct StateIdle; struct StateIdle;
struct StateNext; struct StateNextActor;
struct StateGenerateMsg; struct StateGenerateMsg;
struct StateTimingCheck; struct StateTimingCheck;

View File

@ -20,14 +20,14 @@ void RPBot::registerCommands(void) {
if (params.empty()) { if (params.empty()) {
// contact_to should be the contact this is for // contact_to should be the contact this is for
if (_cr.any_of<StateIdle, StateGenerateMsg, StateNext, StateTimingCheck>(contact_to)) { if (_cr.any_of<StateIdle, StateGenerateMsg, StateNextActor, StateTimingCheck>(contact_to)) {
_rmm.sendText( _rmm.sendText(
contact_from, contact_from,
"error: already running" "error: already running"
); );
return true; return true;
} }
if (_cr.any_of<StateIdle, StateGenerateMsg, StateNext, StateTimingCheck>(contact_from)) { if (_cr.any_of<StateIdle, StateGenerateMsg, StateNextActor, StateTimingCheck>(contact_from)) {
_rmm.sendText( _rmm.sendText(
contact_from, contact_from,
"error: already running" "error: already running"
@ -96,7 +96,7 @@ void RPBot::registerCommands(void) {
if (params.empty()) { if (params.empty()) {
// contact_to should be the contact this is for // contact_to should be the contact this is for
if (_cr.any_of<StateIdle, StateGenerateMsg, StateNext, StateTimingCheck>(contact_to)) { if (_cr.any_of<StateIdle, StateGenerateMsg, StateNextActor, StateTimingCheck>(contact_to)) {
_cr.emplace_or_replace<TagStopRPBot>(contact_to); _cr.emplace_or_replace<TagStopRPBot>(contact_to);
_rmm.sendText( _rmm.sendText(
contact_from, contact_from,
@ -104,7 +104,7 @@ void RPBot::registerCommands(void) {
); );
return true; return true;
} }
if (_cr.any_of<StateIdle, StateGenerateMsg, StateNext, StateTimingCheck>(contact_from)) { if (_cr.any_of<StateIdle, StateGenerateMsg, StateNextActor, StateTimingCheck>(contact_from)) {
_cr.emplace_or_replace<TagStopRPBot>(contact_from); _cr.emplace_or_replace<TagStopRPBot>(contact_from);
_rmm.sendText( _rmm.sendText(
contact_from, contact_from,
@ -133,7 +133,7 @@ void RPBot::registerCommands(void) {
auto view = _cr.view<Contact::Components::ID>(); auto view = _cr.view<Contact::Components::ID>();
for (auto it = view.begin(), it_end = view.end(); it != it_end; it++) { for (auto it = view.begin(), it_end = view.end(); it != it_end; it++) {
if (view.get<Contact::Components::ID>(*it).data == id_bin) { if (view.get<Contact::Components::ID>(*it).data == id_bin) {
if (_cr.any_of<StateIdle, StateGenerateMsg, StateNext, StateTimingCheck>(*it)) { if (_cr.any_of<StateIdle, StateGenerateMsg, StateNextActor, StateTimingCheck>(*it)) {
_cr.emplace_or_replace<TagStopRPBot>(*it); _cr.emplace_or_replace<TagStopRPBot>(*it);
_rmm.sendText( _rmm.sendText(
contact_from, contact_from,
@ -170,7 +170,7 @@ void RPBot::registerCommands(void) {
if (params.empty()) { if (params.empty()) {
// contact_to should be the contact this is for // contact_to should be the contact this is for
if (_cr.any_of<StateIdle, StateGenerateMsg, StateNext, StateTimingCheck>(contact_to)) { if (_cr.any_of<StateIdle, StateGenerateMsg, StateNextActor, StateTimingCheck>(contact_to)) {
if (_cr.all_of<StateIdle>(contact_to)) { if (_cr.all_of<StateIdle>(contact_to)) {
_cr.get<StateIdle>(contact_to).force = true; _cr.get<StateIdle>(contact_to).force = true;
_cr.get<StateIdle>(contact_to).timeout = 2.f; _cr.get<StateIdle>(contact_to).timeout = 2.f;
@ -181,7 +181,7 @@ void RPBot::registerCommands(void) {
} }
return true; return true;
} }
if (_cr.any_of<StateIdle, StateGenerateMsg, StateNext, StateTimingCheck>(contact_from)) { if (_cr.any_of<StateIdle, StateGenerateMsg, StateNextActor, StateTimingCheck>(contact_from)) {
if (_cr.all_of<StateIdle>(contact_from)) { if (_cr.all_of<StateIdle>(contact_from)) {
_cr.get<StateIdle>(contact_from).force = true; _cr.get<StateIdle>(contact_from).force = true;
_cr.get<StateIdle>(contact_from).timeout = 2.f; _cr.get<StateIdle>(contact_from).timeout = 2.f;
@ -213,7 +213,7 @@ void RPBot::registerCommands(void) {
auto view = _cr.view<Contact::Components::ID>(); auto view = _cr.view<Contact::Components::ID>();
for (auto it = view.begin(), it_end = view.end(); it != it_end; it++) { for (auto it = view.begin(), it_end = view.end(); it != it_end; it++) {
if (view.get<Contact::Components::ID>(*it).data == id_bin) { if (view.get<Contact::Components::ID>(*it).data == id_bin) {
if (_cr.any_of<StateIdle, StateGenerateMsg, StateNext, StateTimingCheck>(*it)) { if (_cr.any_of<StateIdle, StateGenerateMsg, StateNextActor, StateTimingCheck>(*it)) {
if (_cr.all_of<StateIdle>(*it)) { if (_cr.all_of<StateIdle>(*it)) {
_cr.get<StateIdle>(*it).force = true; _cr.get<StateIdle>(*it).force = true;
_cr.get<StateIdle>(*it).timeout = 2.f; _cr.get<StateIdle>(*it).timeout = 2.f;

View File

@ -17,8 +17,8 @@ struct StateIdle {
}; };
// determines if self should generate a message // determines if self should generate a message
struct StateNext { struct StateNextActor {
static constexpr const char* name {"StateNext"}; static constexpr const char* name {"StateNextActor"};
std::string prompt; std::string prompt;
std::vector<std::string> possible_names; std::vector<std::string> possible_names;