test_business_assistant.py 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. from __future__ import annotations
  2. import json
  3. from datetime import UTC, datetime, timedelta
  4. from types import SimpleNamespace
  5. import pytest
  6. from aiohttp import CookieJar
  7. from aiohttp.test_utils import TestClient, TestServer
  8. from pyrogram.enums import ChatMemberStatus
  9. def connection_payload(connection_id: str = "conn-a", *, can_reply: bool = True) -> dict:
  10. return {
  11. "id": connection_id,
  12. "user": {"id": 900, "username": "owner", "first_name": "店主"},
  13. "user_chat_id": 900,
  14. "date": int(datetime.now(UTC).timestamp()),
  15. "rights": {"can_reply": can_reply, "can_read_messages": True},
  16. "is_enabled": True,
  17. }
  18. def customer_message(
  19. *,
  20. message_id: int,
  21. text: str | None = "营业时间是什么?",
  22. chat_id: int = 501,
  23. sender_id: int = 501,
  24. sent_at: datetime | None = None,
  25. **values,
  26. ) -> dict:
  27. message = {
  28. "business_connection_id": "conn-a",
  29. "message_id": message_id,
  30. "date": int((sent_at or datetime.now(UTC)).timestamp()),
  31. "chat": {"id": chat_id, "type": "private"},
  32. "from": {"id": sender_id, "username": f"user{sender_id}", "first_name": "客户"},
  33. **values,
  34. }
  35. if text is not None:
  36. message["text"] = text
  37. return message
  38. class FakeProvider:
  39. configured = True
  40. def __init__(self, *, error: Exception | None = None) -> None:
  41. self.error = error
  42. self.calls: list[dict] = []
  43. self.extraction_calls: list[dict] = []
  44. async def decide(self, **values):
  45. self.calls.append(values)
  46. if self.error:
  47. raise self.error
  48. entry_id = str(values["knowledge"][0]["entry_id"])
  49. return {
  50. "action": "answer",
  51. "reply": "每天 09:00 至 18:00 营业。",
  52. "handoff_reason": "",
  53. "matched_entry_ids": [entry_id],
  54. "summary": "客户询问营业时间。",
  55. }
  56. async def test_connection(self):
  57. return {"ok": True, "model": "test-model", "response_id": "response-1"}
  58. async def extract_knowledge(self, **values):
  59. self.extraction_calls.append(values)
  60. if self.error:
  61. raise self.error
  62. return [
  63. {
  64. "question": values.get("question_context") or "如何办理?",
  65. "aliases": [],
  66. "keywords": ["办理"],
  67. "answer": values["content"],
  68. "tags": ["自动采集"],
  69. "confidence": 0.92,
  70. }
  71. ]
  72. class FakeBusinessApi:
  73. def __init__(self) -> None:
  74. self.sent: list[dict] = []
  75. self.read: list[tuple[str, int, int]] = []
  76. async def send_message(
  77. self,
  78. chat_id: int,
  79. text: str,
  80. *,
  81. business_connection_id: str = "",
  82. reply_markup: dict | None = None,
  83. ) -> dict:
  84. self.sent.append(
  85. {
  86. "chat_id": chat_id,
  87. "text": text,
  88. "business_connection_id": business_connection_id,
  89. "reply_markup": reply_markup,
  90. }
  91. )
  92. return {
  93. "message_id": 1000 + len(self.sent),
  94. "sender_business_bot": {"id": 999},
  95. }
  96. async def read_business_message(
  97. self, connection_id: str, chat_id: int, message_id: int
  98. ) -> None:
  99. self.read.append((connection_id, chat_id, message_id))
  100. class FakeFeishuWebhook:
  101. def __init__(self) -> None:
  102. self.sent: list[dict] = []
  103. async def send(self, webhook_url: str, text: str, *, signing_secret: str = "") -> None:
  104. self.sent.append(
  105. {
  106. "webhook_url": webhook_url,
  107. "text": text,
  108. "signing_secret": signing_secret,
  109. }
  110. )
  111. async def send_post(
  112. self,
  113. webhook_url: str,
  114. *,
  115. title: str,
  116. content: list[list[dict[str, str]]],
  117. signing_secret: str = "",
  118. ) -> None:
  119. self.sent.append(
  120. {
  121. "webhook_url": webhook_url,
  122. "title": title,
  123. "content": content,
  124. "text": "\n".join(
  125. "".join(segment.get("text", "") for segment in line)
  126. for line in content
  127. ),
  128. "signing_secret": signing_secret,
  129. }
  130. )
  131. async def prepare_runtime(app_modules, *, provider: FakeProvider | None = None):
  132. dbassistant = app_modules.load("wbb.utils.dbassistant")
  133. service = app_modules.load("wbb.services.business_assistant")
  134. await dbassistant.upsert_business_connection(connection_payload())
  135. await dbassistant.update_account_settings("conn-a", {"assistant_enabled": True})
  136. knowledge = await dbassistant.create_knowledge_entry(
  137. "conn-a",
  138. {
  139. "question": "营业时间是什么?",
  140. "aliases": ["几点营业"],
  141. "keywords": ["营业时间", "营业"],
  142. "answer": "每天 09:00 至 18:00 营业。",
  143. "priority": 10,
  144. },
  145. )
  146. selected_provider = provider or FakeProvider()
  147. runtime = service.BusinessAssistantRuntime(
  148. token="123456:" + "A" * 30,
  149. session=SimpleNamespace(),
  150. provider=selected_provider,
  151. )
  152. runtime.api = FakeBusinessApi()
  153. return dbassistant, service, runtime, knowledge
  154. async def test_connection_settings_knowledge_quota_and_bot_scope(app_modules):
  155. dbassistant = app_modules.load("wbb.utils.dbassistant")
  156. await dbassistant.upsert_business_connection(connection_payload("conn-a"))
  157. await dbassistant.upsert_business_connection(connection_payload("conn-b"))
  158. settings = await dbassistant.update_account_settings(
  159. "conn-a",
  160. {
  161. "assistant_enabled": True,
  162. "account_daily_limit": 2,
  163. "customer_daily_limit": 1,
  164. "timezone": "Asia/Shanghai",
  165. },
  166. )
  167. first = await dbassistant.create_knowledge_entry(
  168. "conn-a",
  169. {
  170. "question": "营业时间",
  171. "aliases": ["几点开门"],
  172. "keywords": ["开门"],
  173. "answer": "九点开门。",
  174. "priority": 20,
  175. },
  176. )
  177. await dbassistant.create_knowledge_entry(
  178. "conn-b",
  179. {"question": "营业时间", "keywords": ["开门"], "answer": "十点开门。"},
  180. )
  181. await app_modules.wbb.db.business_assistant_knowledge.insert_one(
  182. {
  183. "bot_id": "foreign-bot",
  184. "entry_id": "foreign-entry",
  185. "connection_id": "conn-a",
  186. "question": "营业时间",
  187. "aliases": [],
  188. "keywords": ["开门"],
  189. "answer": "不应跨 Bot 返回。",
  190. "priority": 1000,
  191. "enabled": True,
  192. "updated_at": datetime.now(UTC),
  193. }
  194. )
  195. matched = await dbassistant.match_knowledge("conn-a", "你们几点开门?")
  196. assert [item["entry_id"] for item in matched] == [first["entry_id"]]
  197. items, total = await dbassistant.list_knowledge_entries("conn-a")
  198. assert total == 1
  199. assert items[0]["answer"] == "九点开门。"
  200. await app_modules.wbb.db.business_assistant_connections.insert_one(
  201. {
  202. "bot_id": "foreign-bot",
  203. "connection_id": "foreign-connection",
  204. "updated_at": datetime.now(UTC),
  205. }
  206. )
  207. connections, total = await dbassistant.list_business_connections(page_size=100)
  208. assert total == 2
  209. assert {item["connection_id"] for item in connections} == {"conn-a", "conn-b"}
  210. assert await dbassistant.reserve_ai_usage("conn-a", 101, settings) == (True, "")
  211. assert await dbassistant.reserve_ai_usage("conn-a", 101, settings) == (
  212. False,
  213. "customer_daily_limit",
  214. )
  215. assert await dbassistant.reserve_ai_usage("conn-a", 102, settings) == (True, "")
  216. assert await dbassistant.reserve_ai_usage("conn-a", 103, settings) == (
  217. False,
  218. "account_daily_limit",
  219. )
  220. usage = await dbassistant.usage_metrics(connection_id="conn-a")
  221. assert usage["ai_calls"] == 2
  222. assert usage["customers"] == 2
  223. with pytest.raises(dbassistant.AssistantDataError) as error:
  224. dbassistant.normalize_account_settings({"timezone": "invalid/timezone"})
  225. assert error.value.code == "invalid_setting"
  226. async def test_feishu_settings_are_validated_and_never_exposed(app_modules):
  227. dbassistant = app_modules.load("wbb.utils.dbassistant")
  228. await dbassistant.upsert_business_connection(connection_payload())
  229. webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/12345678-abcd-4321-abcd-123456789abc"
  230. stored = await dbassistant.update_account_settings(
  231. "conn-a",
  232. {
  233. "feishu_webhook_enabled": True,
  234. "feishu_webhook_url": webhook_url,
  235. "feishu_webhook_signing_secret": "signing-secret",
  236. "feishu_message_preview_enabled": True,
  237. },
  238. )
  239. assert stored["feishu_webhook_url"] == webhook_url
  240. assert stored["feishu_webhook_signing_secret"] == "signing-secret"
  241. public = dbassistant.public_account_settings(stored)
  242. assert public["feishu_webhook_url"] == ""
  243. assert public["feishu_webhook_signing_secret"] == ""
  244. assert public["feishu_webhook_configured"] is True
  245. assert public["feishu_signing_secret_configured"] is True
  246. connections, _total = await dbassistant.list_business_connections()
  247. assert connections[0]["settings"]["feishu_webhook_url"] == ""
  248. assert connections[0]["settings"]["feishu_webhook_configured"] is True
  249. with pytest.raises(dbassistant.AssistantDataError):
  250. dbassistant.normalize_account_settings(
  251. {
  252. "feishu_webhook_enabled": True,
  253. "feishu_webhook_url": "https://example.com/internal-hook",
  254. }
  255. )
  256. def test_ai_contract_and_reply_window_validation(app_modules):
  257. service = app_modules.load("wbb.services.business_assistant")
  258. parsed = service.parse_ai_decision(
  259. json.dumps(
  260. {
  261. "action": "answer",
  262. "reply": "标准答案",
  263. "matched_entry_ids": ["entry-1", "foreign"],
  264. "summary": "摘要",
  265. }
  266. ),
  267. allowed_entry_ids={"entry-1"},
  268. )
  269. assert parsed["matched_entry_ids"] == ["entry-1"]
  270. with pytest.raises(service.AssistantProviderError):
  271. service.parse_ai_decision(
  272. '{"action":"answer","reply":"没有引用"}', allowed_entry_ids={"entry-1"}
  273. )
  274. now = datetime.now(UTC)
  275. assert service.business_reply_window_open(
  276. {"date": int((now - timedelta(hours=23)).timestamp())}, now=now
  277. )
  278. assert not service.business_reply_window_open(
  279. {"date": int((now - timedelta(hours=25)).timestamp())}, now=now
  280. )
  281. assert not service.business_reply_window_open({}, now=now)
  282. extracted = service.parse_knowledge_extraction(
  283. json.dumps(
  284. {
  285. "items": [
  286. {
  287. "question": "如何配送?",
  288. "aliases": ["配送范围"],
  289. "keywords": ["配送"],
  290. "answer": "仅支持市区配送。",
  291. "tags": ["配送"],
  292. "confidence": 0.9,
  293. }
  294. ]
  295. }
  296. )
  297. )
  298. assert extracted[0]["confidence"] == 0.9
  299. assert (
  300. service.redact_business_knowledge_text(
  301. "电话 138 0013 8000,邮箱 user@example.com,Telegram @private_user"
  302. )
  303. == "电话 [电话已脱敏],邮箱 [邮箱已脱敏],Telegram [用户名已脱敏]"
  304. )
  305. async def test_source_event_candidates_publish_edit_delete_and_isolate(app_modules):
  306. dbassistant = app_modules.load("wbb.utils.dbassistant")
  307. await dbassistant.upsert_business_connection(connection_payload("conn-a"))
  308. await dbassistant.upsert_business_connection(connection_payload("conn-b"))
  309. source = await dbassistant.create_knowledge_source(
  310. "conn-a",
  311. {
  312. "source_type": "channel",
  313. "chat_id": -100100,
  314. "title": "官方频道",
  315. "publication_mode": "auto",
  316. "author_policy": "admins_or_allowlist",
  317. },
  318. )
  319. event, changed = await dbassistant.record_source_event(
  320. source,
  321. event_key="telegram:-100100:10",
  322. content="每天九点营业。",
  323. metadata={"chat_id": -100100, "message_id": 10, "trusted_author": True},
  324. )
  325. assert changed is True
  326. candidates = await dbassistant.replace_event_candidates(
  327. source,
  328. event,
  329. [
  330. {
  331. "question": "几点营业?",
  332. "aliases": ["营业时间"],
  333. "keywords": ["营业"],
  334. "answer": "每天九点营业。",
  335. "tags": ["营业"],
  336. "confidence": 0.95,
  337. }
  338. ],
  339. auto_publish=True,
  340. )
  341. candidate = candidates[0]
  342. first_entry_id = candidate["knowledge_entry_id"]
  343. first_entry = await app_modules.wbb.db.business_assistant_knowledge.find_one(
  344. {"bot_id": "primary", "entry_id": first_entry_id}
  345. )
  346. assert candidate["status"] == "published"
  347. assert first_entry["enabled"] is True
  348. edited_event, changed = await dbassistant.record_source_event(
  349. source,
  350. event_key="telegram:-100100:10",
  351. content="每天十点营业。",
  352. metadata={"chat_id": -100100, "message_id": 10, "trusted_author": True},
  353. )
  354. assert changed is True
  355. edited = await dbassistant.replace_event_candidates(
  356. source,
  357. edited_event,
  358. [
  359. {
  360. "question": "几点营业?",
  361. "aliases": [],
  362. "keywords": ["营业"],
  363. "answer": "每天十点营业。",
  364. "tags": ["营业"],
  365. "confidence": 0.96,
  366. }
  367. ],
  368. auto_publish=True,
  369. )
  370. assert edited[0]["knowledge_entry_id"] == first_entry_id
  371. updated_entry = await app_modules.wbb.db.business_assistant_knowledge.find_one(
  372. {"bot_id": "primary", "entry_id": first_entry_id}
  373. )
  374. assert updated_entry["answer"] == "每天十点营业。"
  375. assert await dbassistant.mark_source_event_deleted(
  376. source["source_id"], "telegram:-100100:10"
  377. )
  378. stale = await dbassistant.get_knowledge_candidate(candidate["candidate_id"])
  379. disabled_entry = await app_modules.wbb.db.business_assistant_knowledge.find_one(
  380. {"bot_id": "primary", "entry_id": first_entry_id}
  381. )
  382. assert stale["status"] == "stale"
  383. assert disabled_entry["enabled"] is False
  384. sources_a, total_a = await dbassistant.list_knowledge_sources("conn-a")
  385. sources_b, total_b = await dbassistant.list_knowledge_sources("conn-b")
  386. assert total_a == 1 and sources_a[0]["source_id"] == source["source_id"]
  387. assert total_b == 0 and sources_b == []
  388. event_indexes = await app_modules.wbb.db.business_assistant_source_events.index_information()
  389. assert event_indexes["expires_at_1"]["expireAfterSeconds"] == 0
  390. async def test_business_human_reply_learns_only_when_source_enabled(app_modules):
  391. dbassistant, _service, runtime, _knowledge = await prepare_runtime(app_modules)
  392. await runtime.process_update(
  393. {
  394. "update_id": 9,
  395. "business_message": customer_message(
  396. message_id=9, text="配送到哪里?", chat_id=509, sender_id=509
  397. ),
  398. }
  399. )
  400. await runtime.process_update(
  401. {
  402. "update_id": 10,
  403. "business_message": customer_message(
  404. message_id=10, text="仅支持市区配送。", chat_id=509, sender_id=900
  405. ),
  406. }
  407. )
  408. assert runtime.provider.extraction_calls == []
  409. await dbassistant.create_knowledge_source(
  410. "conn-a",
  411. {
  412. "source_type": "business",
  413. "title": "人工接待对话",
  414. "publication_mode": "auto",
  415. },
  416. )
  417. await runtime.process_update(
  418. {
  419. "update_id": 15,
  420. "business_message": customer_message(
  421. message_id=15, text="支持哪些区域?", chat_id=515, sender_id=515
  422. ),
  423. }
  424. )
  425. await runtime.process_update(
  426. {
  427. "update_id": 16,
  428. "business_message": customer_message(
  429. message_id=16, text="仅支持市区配送。", chat_id=515, sender_id=900
  430. ),
  431. }
  432. )
  433. assert len(runtime.provider.extraction_calls) == 1
  434. candidates, total = await dbassistant.list_knowledge_candidates(
  435. "conn-a", status="published"
  436. )
  437. assert total == 1
  438. assert candidates[0]["answer"] == "仅支持市区配送。"
  439. entry_id = candidates[0]["knowledge_entry_id"]
  440. await runtime.process_update(
  441. {
  442. "update_id": 17,
  443. "edited_business_message": customer_message(
  444. message_id=16, text="仅二环内支持配送。", chat_id=515, sender_id=900
  445. ),
  446. }
  447. )
  448. edited = await dbassistant.get_knowledge_candidate(candidates[0]["candidate_id"])
  449. assert edited["answer"] == "仅二环内支持配送。"
  450. assert edited["knowledge_entry_id"] == entry_id
  451. await runtime.process_update(
  452. {
  453. "update_id": 18,
  454. "deleted_business_messages": {
  455. "business_connection_id": "conn-a",
  456. "chat": {"id": 515},
  457. "message_ids": [16],
  458. },
  459. }
  460. )
  461. stale = await dbassistant.get_knowledge_candidate(candidates[0]["candidate_id"])
  462. entry = await app_modules.wbb.db.business_assistant_knowledge.find_one(
  463. {"bot_id": "primary", "entry_id": entry_id}
  464. )
  465. assert stale["status"] == "stale"
  466. assert entry["enabled"] is False
  467. async def test_group_collection_requires_trusted_author_for_auto_publish(app_modules):
  468. dbassistant, _service, runtime, _knowledge = await prepare_runtime(app_modules)
  469. module = app_modules.load("wbb.modules.business_assistant")
  470. module._runtime = runtime
  471. await dbassistant.create_knowledge_source(
  472. "conn-a",
  473. {
  474. "source_type": "group",
  475. "chat_id": -100300,
  476. "title": "运营讨论群",
  477. "publication_mode": "auto",
  478. "author_policy": "admins_or_allowlist",
  479. },
  480. )
  481. def group_message(message_id: int, author_id: int, text: str):
  482. return SimpleNamespace(
  483. id=message_id,
  484. text=text,
  485. caption=None,
  486. outgoing=False,
  487. is_automatic_forward=False,
  488. date=datetime.now(UTC),
  489. chat=SimpleNamespace(id=-100300),
  490. from_user=SimpleNamespace(id=author_id, is_bot=False),
  491. )
  492. ordinary = group_message(1, 301, "市区支持当日配送。")
  493. await module.process_knowledge_source_message(ordinary)
  494. pending, pending_total = await dbassistant.list_knowledge_candidates(
  495. "conn-a", status="pending"
  496. )
  497. assert pending_total == 1
  498. assert pending[0]["status"] == "pending"
  499. app_modules.app.members[(-100300, 302)] = SimpleNamespace(
  500. status=ChatMemberStatus.ADMINISTRATOR
  501. )
  502. admin_message = group_message(2, 302, "每周一至周五提供配送。")
  503. await module.process_knowledge_source_message(admin_message)
  504. published, published_total = await dbassistant.list_knowledge_candidates(
  505. "conn-a", status="published"
  506. )
  507. assert published_total == 1
  508. assert published[0]["source_snapshot"]["author_id"] == 302
  509. extraction_count = len(runtime.provider.extraction_calls)
  510. await module.process_knowledge_source_message(admin_message)
  511. assert len(runtime.provider.extraction_calls) == extraction_count
  512. async def test_business_updates_are_idempotent_and_manual_reply_pauses(app_modules):
  513. dbassistant, _service, runtime, knowledge = await prepare_runtime(app_modules)
  514. update = {
  515. "update_id": 11,
  516. "business_message": customer_message(message_id=1),
  517. }
  518. await runtime.process_update(update)
  519. await runtime.process_update(update)
  520. assert len(runtime.api.sent) == 1
  521. assert runtime.api.sent[0]["business_connection_id"] == "conn-a"
  522. assert runtime.api.read == [("conn-a", 501, 1)]
  523. assert len(runtime.provider.calls) == 1
  524. assert runtime.provider.calls[0]["knowledge"][0]["entry_id"] == knowledge["entry_id"]
  525. conversation = await dbassistant.get_conversation_by_chat("conn-a", 501)
  526. assert conversation["summary"] == "客户询问营业时间。"
  527. messages = await dbassistant.recent_conversation_messages(
  528. conversation["conversation_id"], limit=10
  529. )
  530. assert [item["direction"] for item in messages] == ["incoming", "assistant"]
  531. assert messages[0]["expires_at"] - messages[0]["created_at"] == timedelta(days=30)
  532. message_indexes = await app_modules.wbb.db.business_assistant_messages.index_information()
  533. assert message_indexes["expires_at_1"]["expireAfterSeconds"] == 0
  534. await runtime.process_update(
  535. {
  536. "update_id": 12,
  537. "business_message": customer_message(
  538. message_id=2, text="账号本人回复", sender_id=900
  539. ),
  540. }
  541. )
  542. paused = await dbassistant.get_conversation(conversation["conversation_id"])
  543. assert paused["status"] == "human_paused"
  544. assert paused["handoff_reason"] == "human_reply_detected"
  545. assert paused["customer"]["id"] == 501
  546. await runtime.process_update(
  547. {
  548. "update_id": 13,
  549. "business_message": customer_message(
  550. message_id=3,
  551. sender_business_bot={"id": 999},
  552. ),
  553. }
  554. )
  555. await runtime.process_update(
  556. {
  557. "update_id": 14,
  558. "business_message": customer_message(
  559. message_id=4,
  560. is_from_offline=True,
  561. ),
  562. }
  563. )
  564. assert len(runtime.api.sent) == 1
  565. async def test_every_user_message_notifies_feishu_once(app_modules):
  566. dbassistant, _service, runtime, _knowledge = await prepare_runtime(app_modules)
  567. webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/12345678-abcd-4321-abcd-123456789abc"
  568. await dbassistant.update_account_settings(
  569. "conn-a",
  570. {
  571. "assistant_enabled": False,
  572. "feishu_webhook_enabled": True,
  573. "feishu_webhook_url": webhook_url,
  574. "feishu_webhook_signing_secret": "secret",
  575. "feishu_message_preview_enabled": False,
  576. },
  577. )
  578. feishu = FakeFeishuWebhook()
  579. runtime.feishu = feishu
  580. await runtime.process_update(
  581. {"update_id": 101, "business_message": customer_message(message_id=1)}
  582. )
  583. await runtime.process_update(
  584. {"update_id": 102, "business_message": customer_message(message_id=1)}
  585. )
  586. assert len(feishu.sent) == 1
  587. assert feishu.sent[0]["webhook_url"] == webhook_url
  588. assert feishu.sent[0]["signing_secret"] == "secret"
  589. assert "营业时间是什么" not in feishu.sent[0]["text"]
  590. assert feishu.sent[0]["title"] == "消息提醒"
  591. assert "用户:" in feishu.sent[0]["text"]
  592. assert "客户:" not in feishu.sent[0]["text"]
  593. assert "消息标识:" not in feishu.sent[0]["text"]
  594. assert feishu.sent[0]["content"][1] == [
  595. {"tag": "text", "text": "用户:"},
  596. {"tag": "a", "text": "客户", "href": "https://t.me/user501"},
  597. ]
  598. await dbassistant.update_account_settings(
  599. "conn-a", {"feishu_message_preview_enabled": True}
  600. )
  601. await runtime.process_update(
  602. {
  603. "update_id": 103,
  604. "business_message": customer_message(
  605. message_id=2, text="第二条用户消息"
  606. ),
  607. }
  608. )
  609. assert len(feishu.sent) == 2
  610. assert "第二条用户消息" in feishu.sent[1]["text"]
  611. await runtime.process_update(
  612. {
  613. "update_id": 1030,
  614. "business_message": customer_message(
  615. message_id=20,
  616. text=None,
  617. caption="图片说明",
  618. photo=[{"file_id": "photo"}],
  619. ),
  620. }
  621. )
  622. assert len(feishu.sent) == 3
  623. assert "类型:非文本" in feishu.sent[2]["text"]
  624. assert "内容:图片说明" in feishu.sent[2]["text"]
  625. await runtime.process_update(
  626. {
  627. "update_id": 1031,
  628. "business_message": customer_message(
  629. message_id=21,
  630. chat_id=502,
  631. sender_id=502,
  632. **{"from": {"id": 502, "first_name": "无用户名用户"}},
  633. ),
  634. }
  635. )
  636. assert len(feishu.sent) == 4
  637. assert feishu.sent[3]["content"][1] == [
  638. {"tag": "text", "text": "用户:"},
  639. {"tag": "text", "text": "无用户名用户"},
  640. ]
  641. await runtime.process_update(
  642. {
  643. "update_id": 104,
  644. "business_message": customer_message(
  645. message_id=3, text="账号本人回复", sender_id=900
  646. ),
  647. }
  648. )
  649. await runtime.process_update(
  650. {
  651. "update_id": 105,
  652. "business_message": customer_message(message_id=4, is_from_offline=True),
  653. }
  654. )
  655. await runtime.process_update(
  656. {
  657. "update_id": 106,
  658. "business_message": customer_message(
  659. message_id=5, sender_business_bot={"id": 999}
  660. ),
  661. }
  662. )
  663. assert len(feishu.sent) == 4
  664. conversation = await dbassistant.get_conversation_by_chat("conn-a", 501)
  665. messages = await dbassistant.recent_conversation_messages(
  666. conversation["conversation_id"], limit=10
  667. )
  668. incoming = [item for item in messages if item["telegram_message_id"] == 2][0]
  669. assert incoming["metadata"]["feishu_notification_status"] == "sent"
  670. async def test_handoff_sensitive_non_text_provider_error_and_expired_window(app_modules):
  671. dbassistant, service, runtime, _knowledge = await prepare_runtime(app_modules)
  672. await dbassistant.update_account_settings(
  673. "conn-a",
  674. {
  675. "notification_destination": "both",
  676. "ops_group_id": -100123,
  677. "account_daily_limit": 1,
  678. "customer_daily_limit": 1,
  679. },
  680. )
  681. await runtime.process_update(
  682. {
  683. "update_id": 21,
  684. "business_message": customer_message(
  685. message_id=1, text="我要退款", chat_id=601, sender_id=601
  686. ),
  687. }
  688. )
  689. sensitive = await dbassistant.get_conversation_by_chat("conn-a", 601)
  690. assert sensitive["status"] == "handoff"
  691. assert sensitive["handoff_reason"] == "sensitive_request"
  692. assert runtime.api.sent[-1]["business_connection_id"] == ""
  693. assert "message_id=1" in runtime.api.sent[-1]["text"]
  694. assert {item["chat_id"] for item in runtime.api.sent[-2:]} == {900, -100123}
  695. await runtime.process_update(
  696. {
  697. "update_id": 22,
  698. "business_message": customer_message(
  699. message_id=2, text=None, chat_id=602, sender_id=602, photo=[{"file_id": "x"}]
  700. ),
  701. }
  702. )
  703. unsupported = await dbassistant.get_conversation_by_chat("conn-a", 602)
  704. assert unsupported["handoff_reason"] == "unsupported_message"
  705. runtime.provider = FakeProvider(error=service.AssistantProviderError("模型超时"))
  706. await runtime.process_update(
  707. {
  708. "update_id": 23,
  709. "business_message": customer_message(
  710. message_id=3, chat_id=603, sender_id=603
  711. ),
  712. }
  713. )
  714. provider_failed = await dbassistant.get_conversation_by_chat("conn-a", 603)
  715. assert provider_failed["handoff_reason"] == "provider_error"
  716. await runtime.process_update(
  717. {
  718. "update_id": 230,
  719. "business_message": customer_message(
  720. message_id=30, chat_id=605, sender_id=605
  721. ),
  722. }
  723. )
  724. quota_handoff = await dbassistant.get_conversation_by_chat("conn-a", 605)
  725. assert quota_handoff["handoff_reason"] == "account_daily_limit"
  726. sent_before = len(runtime.api.sent)
  727. await runtime.process_update(
  728. {
  729. "update_id": 24,
  730. "business_message": customer_message(
  731. message_id=4,
  732. chat_id=604,
  733. sender_id=604,
  734. sent_at=datetime.now(UTC) - timedelta(hours=25),
  735. ),
  736. }
  737. )
  738. expired = await dbassistant.get_conversation_by_chat("conn-a", 604)
  739. assert expired["handoff_reason"] == "reply_window_expired"
  740. new_sends = runtime.api.sent[sent_before:]
  741. assert len(new_sends) == 2
  742. assert all(item["business_connection_id"] == "" for item in new_sends)
  743. async def test_failed_update_goes_to_dead_letter_without_blocking(app_modules, monkeypatch):
  744. dbassistant, _service, runtime, _knowledge = await prepare_runtime(app_modules)
  745. async def fail(_message):
  746. raise RuntimeError("persistent failure")
  747. async def no_sleep(_seconds):
  748. return None
  749. runtime._handle_business_message = fail
  750. monkeypatch.setattr("wbb.services.business_assistant.asyncio.sleep", no_sleep)
  751. await runtime.process_update(
  752. {"update_id": 31, "business_message": customer_message(message_id=1)}
  753. )
  754. dead_letter = await app_modules.wbb.db.business_assistant_dead_letters.find_one(
  755. {"bot_id": "primary", "update_id": 31}
  756. )
  757. update = await app_modules.wbb.db.business_assistant_updates.find_one(
  758. {"bot_id": "primary", "update_id": 31}
  759. )
  760. assert dead_letter["error"] == "persistent failure"
  761. assert update["status"] == "done"
  762. assert await dbassistant.claim_update(31) is False
  763. async def test_connection_updates_revoke_permissions_and_persist_offset(app_modules):
  764. service = app_modules.load("wbb.services.business_assistant")
  765. dbassistant = app_modules.load("wbb.utils.dbassistant")
  766. runtime = service.BusinessAssistantRuntime(
  767. token="123456:" + "A" * 30,
  768. session=SimpleNamespace(),
  769. provider=FakeProvider(),
  770. )
  771. runtime.api = FakeBusinessApi()
  772. await runtime.process_update(
  773. {"update_id": 41, "business_connection": connection_payload(can_reply=True)}
  774. )
  775. connected = await dbassistant.get_business_connection("conn-a")
  776. assert connected["is_enabled"] is True
  777. assert connected["rights"]["can_reply"] is True
  778. await dbassistant.update_account_settings("conn-a", {"assistant_enabled": True})
  779. revoked = connection_payload(can_reply=False)
  780. revoked["is_enabled"] = False
  781. await runtime.process_update({"update_id": 42, "business_connection": revoked})
  782. disconnected = await dbassistant.get_business_connection("conn-a")
  783. assert disconnected["is_enabled"] is False
  784. assert "can_reply" not in disconnected["rights"]
  785. await runtime.process_update(
  786. {"update_id": 420, "business_message": customer_message(message_id=20)}
  787. )
  788. assert runtime.api.sent == []
  789. await dbassistant.save_update_offset(43)
  790. assert await dbassistant.load_update_offset() == 43
  791. await dbassistant.save_update_offset(44)
  792. assert await dbassistant.load_update_offset() == 44
  793. class StartupApi:
  794. def __init__(self, *, supported: bool, webhook_url: str = "") -> None:
  795. self.supported = supported
  796. self.webhook_url = webhook_url
  797. async def get_me(self):
  798. return {"username": "business_bot", "can_connect_to_business": self.supported}
  799. async def get_webhook_info(self):
  800. return {"url": self.webhook_url}
  801. async def test_runtime_blocks_webhook_conflict_and_disabled_business_mode(app_modules):
  802. service = app_modules.load("wbb.services.business_assistant")
  803. runtime = service.BusinessAssistantRuntime(
  804. token="123456:" + "A" * 30,
  805. session=SimpleNamespace(),
  806. provider=FakeProvider(),
  807. )
  808. runtime.api = StartupApi(supported=True, webhook_url="https://hooks.example.com/tg")
  809. webhook_status = await runtime.start()
  810. assert webhook_status["polling_state"] == "blocked"
  811. assert webhook_status["webhook_conflict"] is True
  812. assert runtime._poll_task is None
  813. runtime.api = StartupApi(supported=False)
  814. business_status = await runtime.start()
  815. assert business_status["polling_state"] == "blocked"
  816. assert business_status["business_mode_supported"] is False
  817. assert "BotFather" in business_status["last_error"]
  818. class FakeResponse:
  819. def __init__(self, status: int, payload: dict) -> None:
  820. self.status = status
  821. self.payload = payload
  822. self.headers: dict[str, str] = {}
  823. async def __aenter__(self):
  824. return self
  825. async def __aexit__(self, *_args):
  826. return None
  827. async def json(self, **_kwargs):
  828. return self.payload
  829. class RetrySession:
  830. def __init__(self, responses: list[FakeResponse]) -> None:
  831. self.responses = responses
  832. self.calls = 0
  833. def post(self, *_args, **_kwargs):
  834. response = self.responses[self.calls]
  835. self.calls += 1
  836. return response
  837. async def test_telegram_api_retries_429_and_5xx(app_modules, monkeypatch):
  838. service = app_modules.load("wbb.services.business_assistant")
  839. session = RetrySession(
  840. [
  841. FakeResponse(
  842. 429,
  843. {
  844. "ok": False,
  845. "error_code": 429,
  846. "description": "Too Many Requests",
  847. "parameters": {"retry_after": 3},
  848. },
  849. ),
  850. FakeResponse(
  851. 502,
  852. {"ok": False, "error_code": 502, "description": "Bad Gateway"},
  853. ),
  854. FakeResponse(200, {"ok": True, "result": {"id": 999}}),
  855. ]
  856. )
  857. sleeps: list[int] = []
  858. async def record_sleep(seconds):
  859. sleeps.append(seconds)
  860. monkeypatch.setattr("wbb.services.business_assistant.asyncio.sleep", record_sleep)
  861. api = service.TelegramBusinessApi("123456:" + "A" * 30, session)
  862. assert await api.get_me() == {"id": 999}
  863. assert session.calls == 3
  864. assert sleeps == [3, 2]
  865. async def test_feishu_webhook_signature_and_success_contract(app_modules):
  866. service = app_modules.load("wbb.services.business_assistant")
  867. response = FakeResponse(200, {"code": 0, "msg": "success"})
  868. class CaptureSession:
  869. def __init__(self) -> None:
  870. self.payload = None
  871. def post(self, _url, *, json, timeout):
  872. self.payload = json
  873. assert timeout.total == 8
  874. return response
  875. session = CaptureSession()
  876. client = service.FeishuWebhookClient(session)
  877. await client.send(
  878. "https://open.feishu.cn/open-apis/bot/v2/hook/12345678-abcd-4321-abcd-123456789abc",
  879. "消息提醒",
  880. signing_secret="secret",
  881. )
  882. assert session.payload["msg_type"] == "text"
  883. assert session.payload["content"]["text"] == "消息提醒"
  884. assert session.payload["timestamp"]
  885. assert session.payload["sign"]
  886. post_content = [
  887. [
  888. {"tag": "text", "text": "用户:"},
  889. {"tag": "a", "text": "测试用户", "href": "https://t.me/test_user"},
  890. ]
  891. ]
  892. await client.send_post(
  893. "https://open.feishu.cn/open-apis/bot/v2/hook/12345678-abcd-4321-abcd-123456789abc",
  894. title="消息提醒",
  895. content=post_content,
  896. )
  897. assert session.payload == {
  898. "msg_type": "post",
  899. "content": {
  900. "post": {
  901. "zh_cn": {
  902. "title": "消息提醒",
  903. "content": post_content,
  904. }
  905. }
  906. },
  907. }
  908. async def _login_and_change_password(client: TestClient) -> str:
  909. login = await client.post(
  910. "/api/admin/v1/auth/login",
  911. json={"username": "admin", "password": "qwe0.123456"},
  912. )
  913. login_data = (await login.json())["data"]
  914. changed = await client.put(
  915. "/api/admin/v1/auth/password",
  916. headers={"X-CSRF-Token": login_data["csrf_token"]},
  917. json={
  918. "current_password": "qwe0.123456",
  919. "new_password": "changed-pass-123",
  920. },
  921. )
  922. return (await changed.json())["data"]["csrf_token"]
  923. async def test_business_assistant_api_permission_csrf_audit_and_clear(app_modules):
  924. dbassistant = app_modules.load("wbb.utils.dbassistant")
  925. await dbassistant.upsert_business_connection(connection_payload())
  926. conversation = await dbassistant.get_or_create_conversation(
  927. "conn-a", 701, customer={"id": 701, "first_name": "待清理客户"}
  928. )
  929. await dbassistant.append_conversation_message(
  930. conversation["conversation_id"],
  931. direction="incoming",
  932. telegram_message_id=1,
  933. text="请清理",
  934. )
  935. await dbassistant.update_conversation_summary(conversation["conversation_id"], "待清理摘要")
  936. admin_api = app_modules.load("wbb.admin.api")
  937. application = admin_api.build_admin_application()
  938. await application["admin_api"].initialize()
  939. client = TestClient(TestServer(application), cookie_jar=CookieJar(unsafe=True))
  940. await client.start_server()
  941. try:
  942. csrf = await _login_and_change_password(client)
  943. app_modules.wbb.BOT_PERMISSIONS = set()
  944. denied = await client.get("/api/admin/v1/business-assistant/status")
  945. assert denied.status == 403
  946. app_modules.wbb.BOT_PERMISSIONS = {"business_assistant.manage"}
  947. no_csrf = await client.put(
  948. "/api/admin/v1/business-assistant/settings",
  949. json={"connection_id": "conn-a", "assistant_enabled": True, "confirm": True},
  950. )
  951. assert no_csrf.status == 403
  952. unconfirmed = await client.put(
  953. "/api/admin/v1/business-assistant/settings",
  954. headers={"X-CSRF-Token": csrf},
  955. json={"connection_id": "conn-a", "assistant_enabled": True},
  956. )
  957. assert unconfirmed.status == 409
  958. saved = await client.put(
  959. "/api/admin/v1/business-assistant/settings",
  960. headers={"X-CSRF-Token": csrf},
  961. json={
  962. "connection_id": "conn-a",
  963. "assistant_enabled": True,
  964. "feishu_webhook_enabled": True,
  965. "feishu_webhook_url": "https://open.feishu.cn/open-apis/bot/v2/hook/12345678-abcd-4321-abcd-123456789abc",
  966. "feishu_webhook_signing_secret": "api-secret",
  967. "confirm": True,
  968. },
  969. )
  970. assert saved.status == 200
  971. saved_data = (await saved.json())["data"]
  972. assert saved_data["assistant_enabled"] is True
  973. assert saved_data["feishu_webhook_url"] == ""
  974. assert saved_data["feishu_webhook_signing_secret"] == ""
  975. assert saved_data["feishu_webhook_configured"] is True
  976. stored_settings = await dbassistant.get_account_settings("conn-a")
  977. assert stored_settings["feishu_webhook_signing_secret"] == "api-secret"
  978. listed_connections = await client.get(
  979. "/api/admin/v1/business-assistant/connections?page=1&page_size=100"
  980. )
  981. listed_settings = (await listed_connections.json())["data"]["items"][0][
  982. "settings"
  983. ]
  984. assert listed_settings["feishu_webhook_url"] == ""
  985. assert listed_settings["feishu_webhook_signing_secret"] == ""
  986. class FakeRuntime:
  987. def __init__(self) -> None:
  988. self.calls = []
  989. async def test_feishu_webhook(self, connection_id, overrides):
  990. self.calls.append((connection_id, overrides))
  991. return {"ok": True, "account": "店主"}
  992. module = app_modules.load("wbb.modules.business_assistant")
  993. fake_runtime = FakeRuntime()
  994. module._runtime = fake_runtime
  995. tested = await client.post(
  996. "/api/admin/v1/business-assistant/feishu-webhook/test",
  997. headers={"X-CSRF-Token": csrf},
  998. json={"connection_id": "conn-a", "confirm": True},
  999. )
  1000. assert tested.status == 200
  1001. assert (await tested.json())["data"]["ok"] is True
  1002. assert fake_runtime.calls == [("conn-a", {})]
  1003. created = await client.post(
  1004. "/api/admin/v1/business-assistant/knowledge",
  1005. headers={"X-CSRF-Token": csrf},
  1006. json={
  1007. "connection_id": "conn-a",
  1008. "question": "配送范围",
  1009. "keywords": ["配送"],
  1010. "answer": "仅限市区。",
  1011. "confirm": True,
  1012. },
  1013. )
  1014. assert created.status == 201
  1015. listed = await client.get(
  1016. "/api/admin/v1/business-assistant/knowledge?connection_id=conn-a"
  1017. )
  1018. assert (await listed.json())["data"]["total"] == 1
  1019. created_source = await client.post(
  1020. "/api/admin/v1/business-assistant/knowledge-sources",
  1021. headers={"X-CSRF-Token": csrf},
  1022. json={
  1023. "connection_id": "conn-a",
  1024. "source_type": "business",
  1025. "title": "人工对话",
  1026. "publication_mode": "review",
  1027. "confirm": True,
  1028. },
  1029. )
  1030. assert created_source.status == 201
  1031. source = (await created_source.json())["data"]
  1032. event, _ = await dbassistant.record_source_event(
  1033. source,
  1034. event_key="business:701:8",
  1035. content="市区可配送。",
  1036. metadata={"chat_id": 701, "message_id": 8, "trusted_author": True},
  1037. )
  1038. candidates = await dbassistant.replace_event_candidates(
  1039. source,
  1040. event,
  1041. [
  1042. {
  1043. "question": "配送范围?",
  1044. "keywords": ["配送"],
  1045. "answer": "市区可配送。",
  1046. "confidence": 0.9,
  1047. }
  1048. ],
  1049. auto_publish=False,
  1050. )
  1051. candidate_page = await client.get(
  1052. "/api/admin/v1/business-assistant/knowledge-candidates"
  1053. "?connection_id=conn-a&status=pending"
  1054. )
  1055. assert candidate_page.status == 200
  1056. assert (await candidate_page.json())["data"]["total"] == 1
  1057. approved = await client.post(
  1058. "/api/admin/v1/business-assistant/knowledge-candidates/"
  1059. f"{candidates[0]['candidate_id']}/actions",
  1060. headers={"X-CSRF-Token": csrf},
  1061. json={"action": "approve", "confirm": True},
  1062. )
  1063. assert approved.status == 200
  1064. assert (await approved.json())["data"]["status"] == "published"
  1065. cleared = await client.post(
  1066. f"/api/admin/v1/business-assistant/conversations/{conversation['conversation_id']}/actions",
  1067. headers={"X-CSRF-Token": csrf},
  1068. json={"action": "clear", "confirm": True},
  1069. )
  1070. assert cleared.status == 200
  1071. detail = await dbassistant.conversation_detail(conversation["conversation_id"])
  1072. assert detail["summary"] == ""
  1073. assert detail["messages"] == []
  1074. audit = await app_modules.wbb.db.admin_audit_logs.find_one(
  1075. {"action": "business_assistant.conversation.clear"}
  1076. )
  1077. assert audit["success"] is True
  1078. finally:
  1079. await client.close()