This commit is contained in:
gr
2026-09-28 10:02:07 +08:00
parent d79a2fa489
commit b112dcc6be
303 changed files with 5039 additions and 49659 deletions
+38 -3
View File
@@ -586,6 +586,16 @@ class ProtocolBot:
try:self.log(f'[协议会话异常] {detail};{type(error).__name__}: {error}')
except Exception:pass
def _close_without_reply(self,key,state,closing):
"""Customer only acknowledged; end the task like a human would, without a reply."""
self.log(f"[协议收信] {state.get('display_name','')} · {closing.reason},本轮不回复")
try:
from queue_log import QueueLog
QueueLog().append(key,state.get('display_name',''),'已收尾',closing.reason+',不再自动回复')
except Exception:pass
with self._lock:
if self._pending.get(key) is state:self._pending.pop(key,None)
self._save()
def poll_once(self):
if self.stop_event.is_set():return
try:self._flush_assistant_cancellations()
@@ -728,11 +738,25 @@ class ProtocolBot:
current=(context or {}).get('last_message') or {}
if current.get('dedup_key')!=state.get('dedup_key') or not _is_text_message(current):
manual_reason=manual_reason or MANUAL_NON_TEXT_REASON
if manual_reason in (MANUAL_MEDIA_REASON,MANUAL_NON_TEXT_REASON) and current.get('dedup_key')==state.get('dedup_key'):
# A sticker or emoji after "谢谢" is a reaction, not a question: no manual task, no reply.
from conversation_closing import decide_for_messages, is_reaction_batch
reaction_batch=unanswered_messages(context)
if is_reaction_batch(reaction_batch):
closing=decide_for_messages((context or {}).get('messages'),reaction_batch)
if closing.skip:
self._close_without_reply(key,state,closing);return
if not manual_reason:
# The current question/risk check and archive receive the same transcribed batch as the model.
batch=unanswered_messages(context)
state.update(staged_user_text='\n'.join(str(m.get('content') or '') for m in batch),
last_lines=[str(m.get('content') or '') for m in batch])
# Like a human agent: do not answer a bare 好的/谢谢 that only closes the conversation.
from conversation_closing import decide_for_messages
closing=decide_for_messages((context or {}).get('messages'),batch)
if closing.skip:
self._close_without_reply(key,state,closing);return
if isinstance(context,dict):context['closing_hint']=closing.hint
if manual_reason:
lines=[]
for message in reversed((context or {}).get('messages') or []):
@@ -838,6 +862,12 @@ class ProtocolBot:
if self.send_mode=='review' and not self._assistant_call('resumed',state.get('conv_id','')):
return True,'人工审核后点击“通过并发送”'
return False,''
def _order_mcp_context(self,context):
"""Identifiers for the built-in order MCP; the backend re-checks the patient binding."""
conv_id=str((context or {}).get('conv_id') or '').strip()
account=str((context or {}).get('account') or self.account or '').strip()
if not conv_id or not account or account!=str(self.account):return {}
return {'external_account_id':account,'conversation_external_id':conv_id}
def _generate(self,context):
import ai_config,ai_chat,model_router,uuid
key=session_key(self.account,context['conv_id']);task_id=uuid.uuid4().hex
@@ -847,9 +877,14 @@ class ProtocolBot:
generation_state.update(model_task_id=task_id,judge_risk='',judge_mode='')
if not ai_config.AI_ENABLED:return self.reply_text
context={**context,'text':model_context_text(context)}
# Protocol-mode tasks never pass through WeChatBot._mcp_order_context; hand the
# read-only order MCP the same two identifiers here so bound patients get real
# order lookups instead of the no-tools fallback wording.
mcp_context=self._order_mcp_context(context)
turn_hint=str((context or {}).get('closing_hint') or '')
if ai_chat.gateway_provider() is not None:
ai_chat.take_last_gateway_trace()
reply=ai_chat.get_ai_reply(chat_text=context['text'])
reply=ai_chat.get_ai_reply(chat_text=context['text'],mcp_context=mcp_context,turn_hint=turn_hint)
trace=ai_chat.take_last_gateway_trace()
key=session_key(self.account,context['conv_id'])
with self._lock:
@@ -860,10 +895,10 @@ class ProtocolBot:
self._pending[key]['model_task_id']=trace.get('task_id') or ''
return reply
provider=ai_chat.current_provider();judge=provider if getattr(ai_config,'AI_JUDGE_ENABLED',False) else None
if judge is None:return ai_chat.get_ai_reply(chat_text=context['text'])
if judge is None:return ai_chat.get_ai_reply(chat_text=context['text'],mcp_context=mcp_context,turn_hint=turn_hint)
from voice_messages import unanswered_messages
customer_text='\n'.join(str(message.get('content') or '') for message in unanswered_messages(context))
result=model_router.answer(chat_text=context['text'],answer_providers=[provider],judge_provider=judge,judge_mode=getattr(ai_config,'AI_JUDGE_MODE','shadow'),customer_text=customer_text)
result=model_router.answer(chat_text=context['text'],answer_providers=[provider],judge_provider=judge,judge_mode=getattr(ai_config,'AI_JUDGE_MODE','shadow'),customer_text=customer_text,mcp_context=mcp_context,turn_hint=turn_hint)
key=session_key(self.account,context['conv_id'])
with self._lock:
if self._pending.get(key) is generation_state and generation_state is not None and generation_state.get('model_task_id')==task_id: