feat: integrate gemini auto-draw preview via tw_terminator service
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 20:26:44 +08:00
parent 674519709e
commit 4c64a27220
3 changed files with 174 additions and 0 deletions

View File

@@ -7,7 +7,9 @@ from collections import defaultdict
import websockets
from .callbacks import post_tianwang_callback
from .auto_draw import auto_draw_preview
from .config import (
AUTO_DRAW_ENABLED,
AUTO_QUOTE_WAIT_SECONDS,
MESSAGE_DEBOUNCE_SECONDS,
QINGJIAN_WS_URI,
@@ -77,6 +79,28 @@ class QingjianClient:
self.recent_outbound = self.recent_outbound[-200:]
activity_event(self.logger, "send_reply_success", trace_id=trace_id, customer_id=data.get("from_id", "-"), msg=text)
async def send_image(self, data: dict, image_url: str, trace_id: str = "-") -> None:
image_url = str(image_url or "").strip()
if not image_url:
return
msg = {
"msg_id": "",
"acc_id": data.get("acc_id", ""),
"msg": image_url,
"from_id": data.get("from_id", ""),
"from_name": data.get("from_name", data.get("from_id", "")),
"cy_id": data.get("from_id", ""),
"acc_type": data.get("acc_type", "AliWorkbench"),
"msg_type": 1,
"cy_name": data.get("from_name", data.get("from_id", "")),
}
activity_event(self.logger, "send_image_attempt", trace_id=trace_id, customer_id=data.get("from_id", "-"), msg=image_url)
await self.send_message(msg)
self.recent_outbound.append((str(data.get("acc_id", "")), str(data.get("from_id", "")), image_url, time.monotonic()))
if len(self.recent_outbound) > 200:
self.recent_outbound = self.recent_outbound[-200:]
activity_event(self.logger, "send_image_success", trace_id=trace_id, customer_id=data.get("from_id", "-"), msg=image_url)
@staticmethod
def _clean_text(text: str) -> str:
t = str(text or "").strip()
@@ -185,6 +209,49 @@ class QingjianClient:
return
if decision.action == "quote":
if AUTO_DRAW_ENABLED and self.pending_images.get(key):
latest_image = self.pending_images[key][-1]
activity_event(
self.logger,
"auto_draw_start",
trace_id=trace_id,
customer_id=context["customer_id"],
image_url=latest_image,
)
draw_res = await auto_draw_preview(
image_url=latest_image,
customer_id=context["customer_id"],
requirement=merged_msg,
)
if draw_res.get("ok"):
preview_url = str(draw_res.get("url", "") or "")
await self.send_reply(data, "先给你做了预览图。", trace_id=trace_id)
await self.send_image(data, preview_url, trace_id=trace_id)
final_text = "看下预览,满意再拍下付款。"
await self.send_reply(data, final_text, trace_id=trace_id)
self.last_reply_key[key] = final_text
# 预览完成后清掉当前批次,避免同一图重复触发
self.pending_images[key].clear()
activity_event(
self.logger,
"auto_draw_success",
trace_id=trace_id,
customer_id=context["customer_id"],
preview_url=preview_url,
)
await post_tianwang_callback(
"message_processed",
data,
extra={"trace_id": trace_id, "route": route, "action": "quote", "reply": final_text, "auto_draw": True},
)
return
activity_event(
self.logger,
"auto_draw_fail",
trace_id=trace_id,
customer_id=context["customer_id"],
error=str(draw_res.get("error", "unknown")),
)
text = (decision.reply or "").strip()
if self._is_invalid_ai_reply(text):
text = self._fallback_reply("quote")