StrayWarrior 4 дней назад
Родитель
Сommit
dfd5ee317b
2 измененных файлов с 15 добавлено и 5 удалено
  1. 6 4
      pqai_agent/clients/relation_stage_client.py
  2. 9 1
      pqai_agent/dialogue_manager.py

+ 6 - 4
pqai_agent/clients/relation_stage_client.py

@@ -5,23 +5,25 @@ import requests
 from pqai_agent.logging_service import logger
 
 class RelationStageClient:
+    UNKNOWN_RELATION_STAGE = '未知'
+
     def __init__(self, base_url: Optional[str] = None):
         base_url = base_url or "http://ai-wechat-hook-internal.piaoquantv.com/analyse/getUserEmployeeRelStage"
         self.base_url = base_url
 
-    def get_relation_stage(self, staff_id: str, user_id: str) -> Optional[str]:
+    def get_relation_stage(self, staff_id: str, user_id: str) -> str:
         url = f"{self.base_url}?employeeId={staff_id}&userId={user_id}"
         response = requests.get(url)
         if response.status_code != 200:
             logger.error(f"Request error [{response.status_code}]: {response.text}")
-            return None
+            return self.UNKNOWN_RELATION_STAGE
         data = response.json()
         if not data.get('success', False):
             logger.error(f"Error in response: {data.get('message', 'no message returned')}")
-            return None
+            return self.UNKNOWN_RELATION_STAGE
         if 'data' not in data:
             logger.error("No 'data' field in response")
-            return None
+            return self.UNKNOWN_RELATION_STAGE
         return data.get('data')
 
 if __name__ == "__main__":

+ 9 - 1
pqai_agent/dialogue_manager.py

@@ -14,6 +14,7 @@ import cozepy
 from sqlalchemy.orm import sessionmaker, Session
 
 from pqai_agent import configs
+from pqai_agent.clients.relation_stage_client import RelationStageClient
 from pqai_agent.data_models.agent_push_record import AgentPushRecord
 from pqai_agent.logging_service import logger
 from pqai_agent.database import MySQLManager
@@ -125,6 +126,9 @@ class DialogueManager:
         self.history_dialogue_service = HistoryDialogueService(
             config['storage']['history_dialogue']['api_base_url']
         )
+        # FIXME: 实际为无状态接口,不需要每个DialogueManager持有一个单独实例
+        self.relation_stage_client = RelationStageClient()
+        self.relation_stage = self.relation_stage_client.get_relation_stage(staff_id, user_id)
         self.agent_db_session_maker = agent_db_session_maker
         self._recover_state()
         # 由于本地状态管理过于复杂,引入事务机制做状态回滚
@@ -155,6 +159,10 @@ class DialogueManager:
 
     def refresh_profile(self):
         self.staff_profile = self.user_manager.get_staff_profile(self.staff_id)
+        relation_stage = self.relation_stage_client.get_relation_stage(self.staff_id, self.user_id)
+        if relation_stage and relation_stage != self.relation_stage:
+            logger.info(f"staff[{self.staff_id}], user[{self.user_id}]: relation stage changed from {self.relation_stage} to {relation_stage}")
+            self.relation_stage = relation_stage
 
     def _recover_state(self):
         self.current_state, self.previous_state = self.state_cache.get_state(self.staff_id, self.user_id)
@@ -530,7 +538,6 @@ class DialogueManager:
             return True
         return False
 
-
     def is_in_human_intervention(self) -> bool:
         """检查是否处于人工介入状态"""
         return self.current_state == DialogueState.HUMAN_INTERVENTION
@@ -559,6 +566,7 @@ class DialogueManager:
             "last_interaction_interval": self._get_hours_since_last_interaction(2),
             "if_first_interaction": True if self.previous_state == DialogueState.INITIALIZED else False,
             "if_active_greeting": False if user_message else True,
+            "relation_stage": self.relation_stage,
             "formatted_staff_profile": prompt_utils.format_agent_profile(self.staff_profile),
             "formatted_user_profile": prompt_utils.format_user_profile(self.user_profile),
             **self.user_profile,