61 lines
4.0 KiB
Python
61 lines
4.0 KiB
Python
from pathlib import Path
|
|
|
|
path=Path('C:/wechat_rpa/protocol_engine.py')
|
|
text=path.read_text(encoding='utf-8')
|
|
def replace(old,new,count=1):
|
|
global text
|
|
assert text.count(old)==count,(str(path),old[:90],text.count(old),count)
|
|
text=text.replace(old,new)
|
|
replace("self._since=time.time();self._pending={};self._seen={};self._contact_blocked={}", "self._since=time.time();self._pending={};self._seen={};self._contact_blocked={};self._manual_handoffs={}")
|
|
replace(" self._pending={k:v for k,v in raw.get('pending',{}).items() if isinstance(v,dict)}", """ self._pending={k:v for k,v in raw.get('pending',{}).items() if isinstance(v,dict)}
|
|
self._manual_handoffs=raw.get('manual_handoffs',{})
|
|
if not isinstance(self._manual_handoffs,dict):raise NativeUnavailable('人工接管记录格式错误,已停止启动')""")
|
|
replace("'contactBlocked':self._contact_blocked},f", "'contactBlocked':self._contact_blocked,'manual_handoffs':self._manual_handoffs},f")
|
|
replace(" removed=[]\n with self._lock:\n for key in dict.fromkeys", """ removed=[]
|
|
with self._lock:
|
|
previous_pending=dict(self._pending);previous_seen=dict(self._seen)
|
|
previous_handoffs=dict(self._manual_handoffs);previous_attention=self.needs_attention
|
|
for key in dict.fromkeys""")
|
|
replace(" if state.get('dedup_key'):self._seen[key]=state['dedup_key']\n self._pending.pop(key,None)", """ snapshot=json.loads(json.dumps(state,ensure_ascii=False))
|
|
self._manual_handoffs[key]={'state':snapshot,'handed_off_at':time.time(),
|
|
'receipt_checked_by_user':bool(confirm_uncertain),
|
|
'delivery_confirmed':False}
|
|
if state.get('dedup_key'):self._seen[key]=state['dedup_key']
|
|
self._pending.pop(key,None)""")
|
|
replace(" for s in self._pending.values())\n self._save()\n self._reply_wakeup.set()", """ for s in self._pending.values())
|
|
try:self._save()
|
|
except Exception:
|
|
self._pending=previous_pending;self._seen=previous_seen
|
|
self._manual_handoffs=previous_handoffs;self.needs_attention=previous_attention
|
|
raise
|
|
self._reply_wakeup.set()""")
|
|
compile(text,str(path),'exec')
|
|
path.write_text(text,encoding='utf-8')
|
|
|
|
for root in (Path('C:/wechat_rpa'),Path('C:/kefu/wechat_rpa')):
|
|
path=root/'gui_runtime.py'
|
|
text=path.read_text(encoding='utf-8')
|
|
replace(' pending = raw["pending"] if isinstance(raw.get("pending"), dict) else raw\n removed = []', ''' pending = raw["pending"] if isinstance(raw.get("pending"), dict) else raw
|
|
if handoff:
|
|
history = raw.get("manual_handoffs", {})
|
|
if not isinstance(history, dict):
|
|
result["error"] = "人工接管记录格式不正确,无法安全转交人工"
|
|
return result
|
|
if pending is raw:
|
|
pending = {key: state for key, state in raw.items() if key != "manual_handoffs"}
|
|
raw = {"pending": pending, "manual_handoffs": history}
|
|
else:
|
|
raw["manual_handoffs"] = history
|
|
removed = []''')
|
|
replace(' if handoff and protocol and state.get("dedup_key"):\n raw.setdefault("seen", {})[key] = state["dedup_key"]\n pending.pop(key, None)', ''' if handoff and protocol and state.get("dedup_key"):
|
|
raw.setdefault("seen", {})[key] = state["dedup_key"]
|
|
if handoff:
|
|
raw["manual_handoffs"][key] = {
|
|
"state": dict(state), "handed_off_at": time.time(),
|
|
"receipt_checked_by_user": bool(confirm_uncertain), "delivery_confirmed": False,
|
|
}
|
|
pending.pop(key, None)''')
|
|
compile(text,str(path),'exec')
|
|
path.write_text(text,encoding='utf-8')
|
|
print('Preserving full handoff evidence in the same atomically replaced queue file.')
|