fix: reuse saved character in inspections

This commit is contained in:
nyan404 committed 2026-08-16 17:06:53 +03:00
1 parent 16407cd208
commit c15fc81178
2 files changed
+26 -1

No files matched your search

+14 -1
View File
@@ -598,6 +598,7 @@ class BelkiDialog:
natural = parse_natural_bulk(command) natural = parse_natural_bulk(command)
if natural is not None: if natural is not None:
fields, _ = natural fields, _ = natural
self._apply_known_profile(peer_id, fields)
self._states[peer_id] = fields self._states[peer_id] = fields
return "Я нашёл часть данных. " + self._ask_next(self._states[peer_id]) return "Я нашёл часть данных. " + self._ask_next(self._states[peer_id])
if self._bulk_hint(command): if self._bulk_hint(command):
@@ -623,17 +624,29 @@ class BelkiDialog:
self._states[peer_id] = {"mode": "training"} self._states[peer_id] = {"mode": "training"}
return self._ask_next(self._states[peer_id]) return self._ask_next(self._states[peer_id])
if command.lower() in ("/осмотр", "/осмотры"): if command.lower() in ("/осмотр", "/осмотры"):
self._states[peer_id] = {"mode": "inspection"} state = {"mode": "inspection"}
self._apply_known_profile(peer_id, state)
self._states[peer_id] = state
return self._ask_next(self._states[peer_id]) return self._ask_next(self._states[peer_id])
if command.lower().startswith(("/урок ", "/тренировка ", "/осмотр ", "/осмотры ")): if command.lower().startswith(("/урок ", "/тренировка ", "/осмотр ", "/осмотры ")):
try: try:
parsed = parse_text_command(command) parsed = parse_text_command(command)
except (ValueError, IndexError) as exc: except (ValueError, IndexError) as exc:
return f"Не удалось распознать данные скопом: {exc}. Проверьте запись или начните ввод по одному вопросу." return f"Не удалось распознать данные скопом: {exc}. Проверьте запись или начните ввод по одному вопросу."
self._apply_known_profile(peer_id, parsed)
self._states[peer_id] = parsed self._states[peer_id] = parsed
return self._ask_next(self._states[peer_id]) return self._ask_next(self._states[peer_id])
return None return None
def _apply_known_profile(self, actor_id: int, state: dict[str, Any]) -> None:
"""Use the author's saved character for self-referential inspections."""
if state.get("mode") != "inspection" or self._has_field(state, "character"):
return
profile = self.identity_directory.profile(actor_id=actor_id)
if profile:
state["character_name"] = profile.name
state["character_id"] = profile.character_id
def _consume_field(self, state: dict[str, Any], text: str) -> str: def _consume_field(self, state: dict[str, Any], text: str) -> str:
key = next((key for key, _ in self._ORDERS[state["mode"]] if not self._has_field(state, key)), None) key = next((key for key, _ in self._ORDERS[state["mode"]] if not self._has_field(state, key)), None)
if key == "date": if key == "date":
+12
View File
@@ -181,6 +181,18 @@ class VkConversationTests(unittest.TestCase):
restarted = BelkiDialog(IdentityDirectory(db_path=f"{directory}/identities.sqlite3"), onboarding=True) restarted = BelkiDialog(IdentityDirectory(db_path=f"{directory}/identities.sqlite3"), onboarding=True)
self.assertIn("дат", restarted.handle(500, "/лу", user_id=700).lower()) self.assertIn("дат", restarted.handle(500, "/лу", user_id=700).lower())
def test_inspection_reuses_onboarded_author_character(self):
with TemporaryDirectory() as directory:
identities = IdentityDirectory(db_path=f"{directory}/identities.sqlite3")
dialog = BelkiDialog(identities, onboarding=True)
dialog.handle(510, "/осмотр", user_id=710)
dialog.handle(510, "Дикая Вишня [1604614]", user_id=710)
self.assertIn("место", dialog.handle(510, "/осмотр", user_id=710).lower())
dialog.handle(510, "дупло", user_id=710)
self.assertIn("ресурс", dialog.handle(510, "1", user_id=710).lower())
self.assertNotIn("персонаж", dialog.handle(510, "без ресурсов", user_id=710).lower())
def test_independence_accepts_adjectival_forms(self): def test_independence_accepts_adjectival_forms(self):
dialog = BelkiDialog() dialog = BelkiDialog()
dialog.handle(501, "/лу") dialog.handle(501, "/лу")