diff --git a/plugins/ai_auto_response/main.py b/plugins/ai_auto_response/main.py index 2fd581a..5f7c281 100644 --- a/plugins/ai_auto_response/main.py +++ b/plugins/ai_auto_response/main.py @@ -293,7 +293,8 @@ class AIAutoResponsePlugin(MessagePluginInterface): user_prompt, user_id=f"{room_id}:{sender}", image_urls=image_urls, - ) + ), + content, ) if not response: self._log_event( @@ -529,13 +530,53 @@ class AIAutoResponsePlugin(MessagePluginInterface): return bool(current_tokens & previous_tokens) @staticmethod - def _sanitize_response(response: str) -> str: + def _sanitize_response(response: str, current_content: str = "") -> str: if not response: return "" response = response.strip() response = re.sub(r"\n{3,}", "\n\n", response) + current_content = str(current_content or "").strip() + if not response: + return "" + if current_content and AIAutoResponsePlugin._looks_like_prompt_echo(response, current_content): + return "" + if AIAutoResponsePlugin._looks_like_invalid_structured_reply(response, current_content): + return "" return response[:500].strip() + @staticmethod + def _looks_like_prompt_echo(response: str, current_content: str) -> bool: + normalized_response = re.sub(r"\s+", "", str(response or "")) + normalized_current = re.sub(r"\s+", "", str(current_content or "")) + if not normalized_response or not normalized_current: + return False + return normalized_response == normalized_current + + @staticmethod + def _looks_like_invalid_structured_reply(response: str, current_content: str) -> bool: + text = str(response or "").strip() + if not (text.startswith("{") and text.endswith("}")): + return False + try: + data = json.loads(text) + except Exception: + return False + if not isinstance(data, dict): + return False + keys = {str(key).strip().lower() for key in data.keys()} + if not keys: + return False + if keys.issubset({"category", "message", "content", "text", "type"}): + for field in ("message", "content", "text"): + value = str(data.get(field, "") or "").strip() + if not value: + continue + if AIAutoResponsePlugin._looks_like_prompt_echo(value, current_content): + return True + if "category" in keys: + return True + return False + def _finalize_reply(self, response: str, reply_mode: str) -> List[str]: text = (response or "").strip() if not text: