diff --git a/qingjian_cs/app/client.py b/qingjian_cs/app/client.py index 2124355..42551c6 100644 --- a/qingjian_cs/app/client.py +++ b/qingjian_cs/app/client.py @@ -99,6 +99,24 @@ class QingjianClient: head = head[:max_len].rstrip(",,;;:: ") return head or t[:max_len] + @staticmethod + def _is_invalid_ai_reply(text: str) -> bool: + t = str(text or "").strip().lower() + if not t: + return True + if "i noticed that you have interrupted me" in t: + return True + if t.startswith("action:") or t.startswith("{"): + return True + return False + + def _fallback_reply(self, action: str) -> str: + if action == "quote": + return "我先看下,马上给你报价。" + if action == "transfer": + return "我给你转人工处理哈。" + return "收到,我先看一下哈。" + def _is_outbound_echo(self, data: dict, msg: str) -> bool: """ 轻简可能会把我方刚发送文本回推为“收到消息”。 @@ -159,27 +177,31 @@ class QingjianClient: ) if decision.action == "transfer": - text = decision.transfer_msg or "我给你转接人工处理哈。" + text = (decision.transfer_msg or "").strip() + if self._is_invalid_ai_reply(text): + text = self._fallback_reply("transfer") await self.send_reply(data, text, trace_id=trace_id) self.last_reply_key[key] = text await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "transfer", "reply": text}) return if decision.action == "quote": - if decision.reply: - await self.send_reply(data, decision.reply, trace_id=trace_id) - quote_text = "图我收齐了,我这边看完马上给你报具体价格。" - if self.last_reply_key.get(key) != quote_text: - await self.send_reply(data, quote_text, trace_id=trace_id) - self.last_reply_key[key] = quote_text - await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "quote", "reply": quote_text}) + text = (decision.reply or "").strip() + if self._is_invalid_ai_reply(text): + text = self._fallback_reply("quote") + if self.last_reply_key.get(key) != text: + await self.send_reply(data, text, trace_id=trace_id) + self.last_reply_key[key] = text + await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "quote", "reply": text}) return if decision.action == "noop": await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "noop", "reply": ""}) return - text = decision.reply or "收到,我先看一下哈,稍等哈。" + text = (decision.reply or "").strip() + if self._is_invalid_ai_reply(text): + text = self._fallback_reply("reply") if self.last_reply_key.get(key) != text: await self.send_reply(data, text, trace_id=trace_id) self.last_reply_key[key] = text