diff --git a/src/modules/PlayerbotV2/BOT_BRIDGE.md b/src/modules/PlayerbotV2/BOT_BRIDGE.md index d3f41dac21..d673215b55 100644 --- a/src/modules/PlayerbotV2/BOT_BRIDGE.md +++ b/src/modules/PlayerbotV2/BOT_BRIDGE.md @@ -39,7 +39,7 @@ Every bot event has: `id`, `t` (UTC time), `type`, `bot`, `guid`, `class`, `race |---|---|---| | `chat_in` | a player whispers a bot, talks in its party or raid, or says something to it in /say (the same lines `Playerbot.ChatModel` answers, with its switches and limits) | `channel`, `from`, `from_is_bot`, `locale` (his game client's language, `ruRU`, `enUS`...: answer a Russian client in Russian when his line shows no language of its own), `text`, `prompt` (a description of the bot and how to answer, the one the built-in chat model uses), `weather` (the zone's: clear, rain, snow, ...), `time_of_day` (morning, afternoon, evening, night), `swearing` (1 when the realm allows everyday bad language, `Playerbot.Bridge.Swearing`), `social` (from a player: the bot's guild and his, the guild charter he carries with its signatures and whether this bot signed it or can, and what the bot would do with his group invite, in words) | | `moment` | a bot grouped with a real player would say a line on its own in party (it looted something, gained a level, the group wiped or killed a boss, came to a new zone, a ready check, it died or was brought back, it joined the group). Only while a program answers chat and `Moments` is on; the bots' usual chance and cooldowns (`Playerbot.Chatter.*`, `PartyBanter`) decide when, at most `MomentsPerMinute` (6) for the server. Answer it like a `chat_in` (`reply_to` = its `id`; it goes to party; an empty `say` means the bot says nothing). Without a program, or past the limit, the bot says its fixed line as before | `what` (`loot`, `quest`, `kill`, `player_kill`, `level_up`, `greet`, `ready_check`, `wipe`, `rezzed`, `died`, `boss_kill`, `new_zone`, and `need` / `offer`: an owned bot near its owner whose bags are full or gear nearly broken says so, and one that carries an upgrade or a quest item for him offers it, at most one per owner every 5 minutes), `detail` (what happened, in plain words), `example` (the fixed line the bot would have said, an idea; missing for `new_zone`), `to` (the player it is said to: the bot's owner when he is in the group), `to_is_owner`, `can` (with actions on, checked for `to`), `weather`, `time_of_day`, `swearing` | -| `talk` | world bots chat with each other in /say (the gatherings outside the city gates and the street chats inside the capitals, `Playerbot.World.Gathering.*`): the bot's next line. Only while a program answers chat and `Talk` is on, at most `Gathering.ChatModelPerMinute` (4) a minute, and not while two players' lines wait for an answer. Answer it like a `chat_in` (`reply_to` = its `id`, `say` the line); the next bot's `talk` carries it. No answer within 25 s ends that talk | `channel` (say), `scene` (who the bot is, where, with whom, what about), `talk` (the lines so far, `Name: line` each; empty: start the talk), `weather`, `time_of_day`, `swearing`. With `channel` `general` or `trade` it is a world bot's post there (WTS, WTB, LFM, a question; at most `MomentsPerMinute` a minute, the bots' usual suggestion cooldowns decide when) or another bot's answer to one (two posts in five, at most 2 a minute): answer with that `channel`. An item's or quest's name in a post becomes its link; a post that leaves the name out is the bot's fixed line instead. A player who then whispers the bot gets `your_post` in his `chat_in` (its post of the last 15 minutes). A `talk` whose scene says a player walks past is a bot that just waved at him: one short /say line to him, or an empty `say` | +| `talk` | world bots chat with each other in /say (the gatherings outside the city gates and the street chats inside the capitals, `Playerbot.World.Gathering.*`): the bot's next line. Only while a program answers chat and `Talk` is on, at most `Gathering.ChatModelPerMinute` (4) a minute, and not while two players' lines wait for an answer. Answer it like a `chat_in` (`reply_to` = its `id`, `say` the line); the next bot's `talk` carries it. No answer within 25 s ends that talk | `channel` (say), `scene` (who the bot is, where, with whom, what about), `talk` (the lines so far, `Name: line` each; empty: start the talk), `weather`, `time_of_day`, `swearing`. With `channel` `general` or `trade` it is a world bot's post there (WTS, WTB, LFM, a question; at most `MomentsPerMinute` a minute, the bots' usual suggestion cooldowns decide when) or another bot's answer to one (two posts in five, at most 2 a minute): answer with that `channel`. An item's or quest's name in a post becomes its link; a post that leaves the name out is the bot's fixed line instead. A player who then whispers the bot gets `your_post` in his `chat_in` (its post of the last 15 minutes). A `talk` whose scene says a player walks past is a bot that just waved at him: one short /say line to him, or an empty `say`. `locale`: the language most real players on that map play in (else the realm's): write the line in it | | `stuck` | a bot tried to reach a place for over 30 s without moving and dropped the goal | `detail` | | `quest_fail` | a bot dropped a quest that it can never finish or turn in | `detail` | | `spell_fail` | the server refused the same spell `SpellFailCount` times in a minute | `spell_id`, `spell`, `reason` (a server cast-result number; 1000 and up are the bot's own checks) | diff --git a/src/modules/PlayerbotV2/Bot/BotBridge.cpp b/src/modules/PlayerbotV2/Bot/BotBridge.cpp index 1132ec7c1c..a2421f7f4a 100644 --- a/src/modules/PlayerbotV2/Bot/BotBridge.cpp +++ b/src/modules/PlayerbotV2/Bot/BotBridge.cpp @@ -1985,6 +1985,29 @@ std::string ClientLocale(Player const* player) return l < TOTAL_LOCALES ? localeNames[l] : "enUS"; } +// The language most real players on the bot's map play in, for lines no one player asked for (a street talk, a General or +// Trade post): a Russian realm got English posts (Geshka, 2026-11-09). No player there: the realm's own language. +std::string ListenersLocale(Player const* bot) +{ + std::array count{}; + if (Map* map = bot ? bot->GetMap() : nullptr) + for (MapReference const& ref : map->GetPlayers()) + { + Player const* p = ref.GetSource(); + if (p && p->GetSession() && !Services::Registry().has(p->GetGUID().GetCounter())) + { + LocaleConstant const l = p->GetSession()->GetSessionDbcLocale(); + if (l < TOTAL_LOCALES) + ++count[l]; + } + } + std::size_t best = std::size_t(sWorld->GetDefaultDbcLocale()); + for (std::size_t l = 0; l < count.size(); ++l) + if (count[l] > count[best]) + best = l; + return best < TOTAL_LOCALES ? localeNames[best] : "enUS"; +} + void WriteMoment(ConfigReader::BridgeConfig const& cfg, Moment const& m, uint32 now_ms) { Player* bot = FindBot(m.bot); @@ -2074,6 +2097,7 @@ uint64 WriteChannelTalk(ConfigReader::BridgeConfig const& cfg, Player* bot, uint e.fields.emplace_back("channel", ChannelWord(channel_id)); e.fields.emplace_back("scene", scene); e.fields.emplace_back("talk", ""); + e.fields.emplace_back("locale", ListenersLocale(bot)); if (!keep.empty()) e.fields.emplace_back("keep", keep); if (cfg.swearing) @@ -2407,6 +2431,7 @@ bool BotBridge::AskTalk(Player* bot, std::string const& scene, std::string const e.fields.emplace_back("channel", "say"); e.fields.emplace_back("scene", scene.size() > 1500 ? scene.substr(0, 1500) : scene); e.fields.emplace_back("talk", chat.size() > 1500 ? chat.substr(chat.size() - 1500) : chat); + e.fields.emplace_back("locale", ListenersLocale(bot)); if (Services::Config().bridge().swearing) e.numbers.emplace_back("swearing", 1); e.fields.emplace_back("weather", WeatherWord(bot->GetMap() ? bot->GetMap()->GetZoneWeather(bot->GetZoneId()) : WEATHER_STATE_FINE)); diff --git a/src/modules/PlayerbotV2/tools/bot_bridge_ollama.py b/src/modules/PlayerbotV2/tools/bot_bridge_ollama.py index e92cca45cf..3c4abf81da 100644 --- a/src/modules/PlayerbotV2/tools/bot_bridge_ollama.py +++ b/src/modules/PlayerbotV2/tools/bot_bridge_ollama.py @@ -76,6 +76,7 @@ PLACE_WORDS = ["auction house", "flight master", "flight path", "stable master", "bank", "inn", "auctioneer", "vendor", "merchant", "repair", "trainer", "graveyard", "mail", "ah"] EMOTES = ["wave", "hello", "bye", "bow", "thank", "cheer", "laugh", "dance", "salute", "point", "yes", "nod", "no", "cry", "roar", "flex", "kneel", "applaud", "shy", "talk", "question", "exclamation", "rude", "chicken", "beg", "shout", "eat"] +TALK_TIMEOUT_S = 20 # bots' own talk: dropped sooner than a player's answer MAX_AGE_S = 50 # the server drops answers older than ReplyMaxAgeSec (60 s by default) HISTORY_LINES = 6 # lines of each conversation the model sees POLL_S = 0.2 # how often the events file is read @@ -415,7 +416,7 @@ class ChatApi: log("model service %s: %s" % (self.url, "ready, model " + self.model if self.up else "not reachable, fixed answers for now")) return self.up - def chat(self, system, user): + def chat(self, system, user, timeout=None): # 160 tokens is room for the longest answer (a line, an action and a memory); fewer would cut the JSON off body = {"model": self.model, "temperature": 0.8, "max_tokens": 160, "messages": [{"role": "system", "content": system}, {"role": "user", "content": user}]} @@ -431,7 +432,7 @@ class ChatApi: req = urllib.request.Request(self.url + "/chat/completions", data=json.dumps(body).encode("utf-8"), headers=self._headers()) try: t0 = time.time() - with urllib.request.urlopen(req, timeout=self.timeout) as r: + with urllib.request.urlopen(req, timeout=timeout or self.timeout) as r: out = json.load(r) with self.lock: self.avg_s = 0.8 * self.avg_s + 0.2 * (time.time() - t0) @@ -962,6 +963,23 @@ def answer(event, api, actions_on, minds, opts): 0.4 + 0.15 * abs(mood), now) minds.save(bot) reply = reply if (reply.get("say") or reply.get("do")) else None + if reply and reply.get("say") and wants_russian(event) and not mostly_cyrillic(reply["say"]): + # Small models slip into English now and then (qwen2.5:3b, Geshka 2026-11-09): one more ask, else a Russian line. + log("%s's line for %s is not Russian (%s): asking again" % (bot, player, reply["say"])) + try: + again = parse_json(api.chat(system, user + "\n(Your \"say\" was in English. Write it again in Russian.)")) + say2, why2 = check_line(str((again or {}).get("say", "")).strip().strip('"').replace("\n", " ")[:200], swearing) + except (OSError, ValueError) as e: + log("second ask failed (%s)" % e) + say2, why2 = "", "failed" + if say2 and not why2 and mostly_cyrillic(say2): + reply["say"] = say2 + elif event.get("_moment"): + reply.pop("say", None) + elif "do" in reply: + reply["say"] = "ок" # the action speaks for itself + else: + reply["say"] = (fixed_answer(event) or {}).get("say", "") if not reply and event.get("_moment") and data: log("%s -> %s [%s, %.1f s, moment %s]: (says nothing)" % (bot, player, source, time.time() - started, event.get("what"))) return # the model chose to say nothing about it: fine, people don't comment on everything @@ -1016,14 +1034,20 @@ def answer_talk(event, api, minds, opts): system += "Now: " + str(event.get("scene", "")) + "\n" if event.get("weather") or event.get("time_of_day"): system += "Weather here: %s. Time of day: %s.\n" % (event.get("weather", "clear"), event.get("time_of_day", "day")) + # The players around read it: a Russian realm gets Russian street talk and posts ("locale" is theirs, from the server). + russian = str(event.get("locale", "")).lower() == "ruru" + if russian: + system += language_line(True).replace("The player writes", "The players here write") + "\n" talk = str(event.get("talk", "") or "").strip() user = ("The talk so far:\n%s\nYour next line (answer the others, don't repeat what was said):" % talk) if talk else \ "Start the talk with one line." + if russian: + user += "\n(Answer in Russian.)" try: - data = parse_json(api.chat(system, user)) + # A talk line is never worth a long wait: on a slow PC it would hold the model while a player's line waits. + data = parse_json(api.chat(system, user, timeout=min(api.timeout, TALK_TIMEOUT_S))) except (OSError, ValueError) as e: log("talk line of %s failed (%s)" % (bot, e)) - api.next_check = 0 return say = str((data or {}).get("say", "")).strip().strip('"').replace("\n", " ")[:200] if ":" in say[:24] and say.split(":", 1)[0].strip().lower() == bot.lower(): @@ -1031,6 +1055,9 @@ def answer_talk(event, api, minds, opts): say, why = check_line(say, swearing) if why: log("talk line of %s %s" % (bot, why)) + if russian and say and not mostly_cyrillic(say): + log("talk line of %s not in Russian, not said: %s" % (bot, say)) + return if not say: return rng = random.Random() @@ -1108,7 +1135,7 @@ def main(): threading.Thread(target=api.warm, daemon=True).start() # loads the model now, not on the first player's line last_warm, last_chat = time.time(), 0.0 pool = concurrent.futures.ThreadPoolExecutor(max_workers=max(1, opts.workers) + 1) # one more for warm() - pending = set() + pending, chat_pending = set(), set() log("bot bridge: folder %s, model %s at %s, actions %s" % (folder, opts.model, url, "on" if actions_on else "off")) current, offset, last_ping, carry = None, 0, 0.0, b"" @@ -1157,12 +1184,16 @@ def main(): write_reply(folder, reply) log("%s -> %s [fixed, model busy]: %s" % (e.get("from"), e.get("bot"), reply.get("say", ""))) continue - pending.add(pool.submit(safe_answer, e, api, actions_on, minds, opts)) + fut = pool.submit(safe_answer, e, api, actions_on, minds, opts) + pending.add(fut) + chat_pending.add(fut) last_chat = time.time() elif kind == "talk": # World bots chatting with each other: only when no player's line waits (players always come first). pending = {p for p in pending if not p.done()} - if len(pending) >= opts.workers: + chat_pending = {p for p in chat_pending if not p.done()} + # Not even next to a player's answer: two at once on a small card made his take 41 s (Geshka, 2026-11-09). + if len(pending) >= opts.workers or chat_pending: log("talk line of %s skipped: players' chat comes first" % e.get("bot")) continue e["_seen"], e["_folder"] = time.time(), folder