Files
kefu/deploy/review-assistant-20260917/patch_protocol_late_manual.py
T
2026-09-21 10:34:06 +08:00

86 lines
4.7 KiB
Python

from pathlib import Path
p=Path(r'C:\wechat_rpa\protocol_engine.py')
s=p.read_text(encoding='utf-8')
s=s.replace(" if not self._assistant_call('enabled'):return\n if _send_protection(state)", " if not self._assistant_call('enabled') or state.get('assistant_review_id'):return\n if _send_protection(state)", 1)
anchor=' def _reconcile_assistant_review(self,key,state,context=None):'
addition=''' @staticmethod
def _message_order(message):
try:return (float(message.get('send_time') or 0),int(message.get('rowid') or 0))
except (TypeError,ValueError):return (0.0,0)
def _assistant_context(self,state,context):
# A phone reply may reach the local DB after a newer customer question.
# Only this proven resumed batch needs chronological context; normal DB
# polling remains in authoritative row order.
if not state.get('assistant_resume_evidence') or not context:return context
messages=list(context.get('messages') or [])
messages.sort(key=self._message_order)
if not messages:return context
updated={**context,'messages':messages,'last_message':messages[-1]}
updated['text']=model_context_text(updated)
return updated
'''
assert anchor in s
s=s.replace(anchor,addition+anchor,1)
anchor=""" with self._lock:
if self._pending.get(key) is not state or _send_protection(state):return False
previous_seen=self._seen.get(key)
if state.get('dedup_key'):self._seen[key]=state['dedup_key']
self._pending.pop(key,None)
try:self._save()
except Exception:
"""
replacement=""" evidence_order=self._message_order(evidence)
following=[m for m in (context or {}).get('messages',[])
if m.get('account')==self.account and m.get('conv_id')==state.get('conv_id')
and m.get('is_self') is False and m.get('dedup_key')
and self._message_order(m)>evidence_order]
followup=max(following,key=self._message_order) if following else None
with self._lock:
if self._pending.get(key) is not state or _send_protection(state):return False
previous_seen=self._seen.get(key)
if state.get('dedup_key'):self._seen[key]=state['dedup_key']
self._pending.pop(key,None)
try:
if followup:
if self._seen.get(key)==followup['dedup_key']:self._seen.pop(key,None)
self._upsert({**followup,'assistant_resume_evidence':evidence})
else:self._save()
except Exception:
"""
assert anchor in s
s=s.replace(anchor,replacement,1)
old=""" last=(context or {}).get('last_message') or {}
if (last and not last.get('is_self') and last.get('dedup_key')!=state.get('dedup_key')
and last.get('account')==self.account and last.get('conv_id')==state.get('conv_id')):
self._upsert(last)
"""
assert old in s
s=s.replace(old,'',1)
s=s.replace("'database_event':dict(message)}", "'database_event':dict(message),'assistant_resume_evidence':message.get('assistant_resume_evidence')}",1)
s=s.replace(" context=self.db.get_conversation_context_by_id(self.account,state['conv_id'],limit=100)\n for source", " context=self.db.get_conversation_context_by_id(self.account,state['conv_id'],limit=100)\n context=self._assistant_context(state,context)\n for source",1)
s=s.replace(" context=self.db.get_conversation_context_by_id(self.account,state['conv_id'],limit=101)\n for source", " context=self.db.get_conversation_context_by_id(self.account,state['conv_id'],limit=101)\n context=self._assistant_context(state,context)\n for source",1)
p.write_text(s,encoding='utf-8',newline='')
t=Path(r'C:\wechat_rpa\test_protocol_review_assistant.py')
s=t.read_text(encoding='utf-8')
anchor=' def test_sending_unknown_and_failed_evidence_are_never_auto_completed(self):'
addition=''' def test_late_phone_sync_does_not_swallow_newer_already_queued_question(self):
self.review()
self.db.add(message(4,text='new question after manual reply time'))
self.bot.poll_once()
self.assertTrue(self.bot._pending[self.key]['awaiting_review'])
self.db.add({**message(5,self=True,text='manual reply'), 'send_time':3})
self.bot.poll_once()
self.assertEqual(len(self.sender.calls),1)
self.assertEqual(self.sender.calls[0][0],message(4)['dedup_key'])
self.assertEqual(self.generate.call_args.args[0]['last_message']['rowid'],4)
'''
assert anchor in s
s=s.replace(anchor,addition+anchor,1)
t.write_text(s,encoding='utf-8',newline='')
print('late manual synchronization preserves and regenerates later customer question')