feat: use AI-first outbound replies with fallback only on invalid output
Some checks failed
Pre-commit / run (ubuntu-latest) (push) Has been cancelled
Deploy Sphinx documentation to Pages / build_en (ubuntu-latest, 3.10) (push) Has been cancelled
Deploy Sphinx documentation to Pages / build_zh (ubuntu-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.12) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.12) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.12) (push) Has been cancelled

This commit is contained in:
2026-03-02 19:39:40 +08:00
parent 4e5557bcc3
commit c2d8853ae4

View File

@@ -99,6 +99,24 @@ class QingjianClient:
head = head[:max_len].rstrip(",;: ") head = head[:max_len].rstrip(",;: ")
return head or t[:max_len] 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: def _is_outbound_echo(self, data: dict, msg: str) -> bool:
""" """
轻简可能会把我方刚发送文本回推为“收到消息”。 轻简可能会把我方刚发送文本回推为“收到消息”。
@@ -159,27 +177,31 @@ class QingjianClient:
) )
if decision.action == "transfer": 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) await self.send_reply(data, text, trace_id=trace_id)
self.last_reply_key[key] = text self.last_reply_key[key] = text
await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "transfer", "reply": text}) await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "transfer", "reply": text})
return return
if decision.action == "quote": if decision.action == "quote":
if decision.reply: text = (decision.reply or "").strip()
await self.send_reply(data, decision.reply, trace_id=trace_id) if self._is_invalid_ai_reply(text):
quote_text = "图我收齐了,我这边看完马上给你报具体价格。" text = self._fallback_reply("quote")
if self.last_reply_key.get(key) != quote_text: if self.last_reply_key.get(key) != text:
await self.send_reply(data, quote_text, trace_id=trace_id) await self.send_reply(data, text, trace_id=trace_id)
self.last_reply_key[key] = quote_text self.last_reply_key[key] = text
await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "quote", "reply": quote_text}) await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "quote", "reply": text})
return return
if decision.action == "noop": if decision.action == "noop":
await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "noop", "reply": ""}) await post_tianwang_callback("message_processed", data, extra={"trace_id": trace_id, "route": route, "action": "noop", "reply": ""})
return 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: if self.last_reply_key.get(key) != text:
await self.send_reply(data, text, trace_id=trace_id) await self.send_reply(data, text, trace_id=trace_id)
self.last_reply_key[key] = text self.last_reply_key[key] = text