| 
					
				 | 
			
			
				@@ -83,7 +83,9 @@ class DialogueStateCache: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         query = f"INSERT INTO {self.table} (staff_id, user_id, current_state, previous_state)" \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 f" VALUES (%s, %s, %s, %s) " \ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 f"ON DUPLICATE KEY UPDATE current_state=%s, previous_state=%s" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        self.db.execute(query, (staff_id, user_id, state.value, previous_state.value, state.value, previous_state.value)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        rows = self.db.execute(query, (staff_id, user_id, state.value, previous_state.value, state.value, previous_state.value)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        logging.debug("staff[{}], user[{}]: set state: {}, previous state: {}, rows affected: {}" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                      .format(staff_id, user_id, state, previous_state, rows)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 class DialogueManager: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     def __init__(self, staff_id: str, user_id: str, user_manager: UserManager, state_cache: DialogueStateCache): 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -212,7 +214,8 @@ class DialogueManager: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         # 处理连续澄清的情况 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if new_state == DialogueState.CLARIFICATION: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             self.consecutive_clarifications += 1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if self.consecutive_clarifications >= 2: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            # FIXME(zhoutian): 规则过于简单 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if self.consecutive_clarifications >= 10000: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 new_state = DialogueState.HUMAN_INTERVENTION 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 # self._trigger_human_intervention("连续多次澄清请求") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         else: 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -245,17 +248,18 @@ class DialogueManager: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         message_lower = message_text.lower() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         # 判断是否是复杂请求 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        complex_request_keywords = ["帮我", "怎么办", "我需要", "麻烦你", "请帮助", "急", "紧急"] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if any(keyword in message_lower for keyword in complex_request_keywords): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            self.complex_request_counter += 1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            # 如果检测到困难请求且计数达到阈值,触发人工介入 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if self.complex_request_counter >= 1: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                # self._trigger_human_intervention("检测到复杂请求") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return DialogueState.HUMAN_INTERVENTION 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        else: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            # 如果不是复杂请求,重置计数器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            self.complex_request_counter = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        # FIXME(zhoutian): 规则过于简单 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        # complex_request_keywords = ["帮我", "怎么办", "我需要", "麻烦你", "请帮助", "急", "紧急"] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        # if any(keyword in message_lower for keyword in complex_request_keywords): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        #     self.complex_request_counter += 1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        # 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        #     # 如果检测到困难请求且计数达到阈值,触发人工介入 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        #     if self.complex_request_counter >= 1: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        #         # self._trigger_human_intervention("检测到复杂请求") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        #         return DialogueState.HUMAN_INTERVENTION 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        # else: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        #     # 如果不是复杂请求,重置计数器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        #     self.complex_request_counter = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         # 问候检测 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         greeting_keywords = ["你好", "早上好", "中午好", "晚上好", "嗨", "在吗"] 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -501,3 +505,6 @@ class DialogueManager: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return config 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+if __name__ == '__main__': 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    state_cache = DialogueStateCache() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    state_cache.set_state('1688854492669990', '7881302581935903', DialogueState.CHITCHAT, DialogueState.GREETING) 
			 |