Files
wecom_it_smart_desk/docs/02-技术文档/architecture/REQ-坐席-010-sequence-diagram.mermaid
T

74 lines
3.4 KiB
Plaintext
Raw Normal View History

%% REQ-坐席-010 AI回复三态开关 — 时序图②:路径B 员工发消息在三态下的门控与推送分支
%% (时序图①「坐席切换 mode 调用链」见 技术方案-REQ-坐席-010-AI回复三态开关.md §4.1
%% 架构师 高见远(Gao 2026-08-04
sequenceDiagram
autonumber
actor Emp as 员工(H5)
participant H5 as api/h5.py<br/>send_message
participant DB as conversations / messages
participant Gate as ai_reply_gate
participant Task as tasks/h5_ai_task.py<br/>process_h5_ai_reply
participant Dify as Dify
participant WS as ws_manager
actor AgentUI as 坐席工作台
Emp->>H5: POST /api/h5/.../messages {content}
H5->>DB: 查/建 conversation(新建 status=ai_handling
H5->>DB: INSERT message(sender_type=employee)
H5->>WS: broadcast(new_message, sender=employee)
WS-->>AgentUI: 员工消息(不受本开关影响,始终可见)
H5->>DB: COMMIT(保证后台任务读得到)
rect rgb(255, 244, 230)
Note over H5,Gate: 门控必须在 asyncio.create_task 之前(h5.py:939 / ws.py:472
H5->>Gate: can_ai_auto_reply_path_b(conversation)
Gate->>Gate: status in {ai_handling, queued}?<br/>AND ai_reply_mode != "off"?
end
alt mode == "off"
Gate-->>H5: False
H5->>H5: logger.info("AI自动回复已关闭,跳过")
H5-->>Emp: 200 {user_message, ai_reply:null}
Note over Task: 后台任务从未创建 → 零 DB session / 零 Dify 调用
else status == "serving"(本次修复的缺口)
Gate-->>H5: False
H5->>H5: logger.info("坐席已接管,跳过AI自动回复")
H5-->>Emp: 200 {user_message, ai_reply:null}
Note over AgentUI: 坐席独占对话,AI 不再抢话(PRD G2 / US2
else 允许触发
Gate-->>H5: True
H5->>Task: asyncio.create_task(process_h5_ai_reply(...))
H5-->>Emp: 200(立即返回,AI 回复走 WS 异步推)
Task->>DB: _step_load_conversation
Task->>Dify: _step_call_dify
Note over Task,WS: ai_thinking 坐席广播(h5_ai_task.py:1603<br/>同样受 should_push_to_agent 门控
Task->>Gate: should_push_to_agent(conversation)
alt mode == employee_and_agent
Task->>WS: broadcast(ai_thinking)
WS-->>AgentUI: AI 思考中…
end
Dify-->>Task: AIReplyResult
Task->>DB: _persist_and_push*INSERT message(sender_type=ai)
Task->>WS: broadcast_to_employees([emp], ai_reply)
WS-->>Emp: AI 回复(三态下只要触发就必推员工)
rect rgb(232, 245, 233)
Note over Task,Gate: 坐席广播门控(4 处:L482 / L749 / L891 / L1603
Task->>Gate: should_push_to_agent(conversation)
end
alt mode == "employee_and_agent"
Gate-->>Task: True
Task->>WS: broadcast(new_message, sender_type=ai)
Task->>WS: broadcast(conversation_updated)
WS-->>AgentUI: 坐席同步看到 AI 回复内容(P0-7 / US3
else mode == "employee_only"(默认)
Gate-->>Task: False
Note over Task,AgentUI: 行为变更:不再广播坐席<br/>员工-AI 会话对坐席不可见
end
end
Note over H5,Task: ADR-6:切 off / 转 serving 不 cancel 已提交的在途 task<br/>让其自然结束(PRD P1-4,避免半截消息)