浏览代码

Update dialogue_manager: recover interaction status from dialogue history

StrayWarrior 2 周之前
父节点
当前提交
51509daf04
共有 1 个文件被更改,包括 16 次插入1 次删除
  1. 16 1
      dialogue_manager.py

+ 16 - 1
dialogue_manager.py

@@ -62,6 +62,7 @@ class DialogueManager:
         self.user_manager = user_manager
         self.current_state = DialogueState.GREETING
         self.previous_state = None
+        # 目前实际仅用作调试,拼装prompt时使用history_dialogue_service获取
         self.dialogue_history = []
         self.user_profile = self.user_manager.get_user_profile(user_id)
         self.last_interaction_time = 0
@@ -69,12 +70,22 @@ class DialogueManager:
         self.complex_request_counter = 0
         self.human_intervention_triggered = False
         self.vector_memory = DummyVectorMemoryManager(user_id)
-        self.message_aggregation_sec = 5
+        self.message_aggregation_sec = config.get('message_aggregation_sec', 5)
         self.unprocessed_messages = []
         self.history_dialogue_service = HistoryDialogueService(
             config['storage']['history_dialogue']['api_base_url']
         )
+        self._reset_interaction_time()
 
+    def _reset_interaction_time(self):
+        last_message = self.history_dialogue_service.get_dialogue_history(self.staff_id, self.user_id, 1)
+        if last_message:
+            self.last_interaction_time = last_message[0]['timestamp']
+        else:
+            # 默认设置为24小时前
+            self.last_interaction_time = int(time.time() * 1000) - 24 * 3600 * 1000
+
+    @staticmethod
     def get_current_time_context(self) -> TimeContext:
         """获取当前时间上下文"""
         current_hour = datetime.now().hour
@@ -424,8 +435,12 @@ class DialogueManager:
             custom_variables.pop('user_profile', None)
             config['custom_variables'] = custom_variables
             config['bot_id'] = self._select_coze_bot(self.current_state)
+            #FIXME(zhoutian): 这种方法并不可靠,需要通过状态来判断
             if not user_message:
                 messages.append(cozepy.Message.build_user_question_text('请开始对话'))
+            #FIXME(zhoutian): 临时报警
+            if user_message and not messages:
+                logging.error(f"staff[{self.staff_id}], user[{self.user_id}]: inconsistency in messages")
         config['messages'] = messages
 
         return config