Files
kefu/wechat_rpa/review_state.py
T
2026-09-21 10:34:06 +08:00

47 lines
3.0 KiB
Python

"""Normalize visual/protocol pending work for display; never authorizes a send."""
def task_review_state(state):
state = state if isinstance(state, dict) else {}
stage = str(state.get('stage') or '')
send_state = str(state.get('send_state') or '')
draft = str(state.get('staged_reply_text') or state.get('reply_text') or '').strip()
delivery_uncertain = send_state in {'uncertain', 'sent_uncommitted', 'unknown'} or stage in {'uncertain', 'receipt_check', 'unknown'}
protected = delivery_uncertain or send_state in {'sending', 'failed'} or stage == 'sending'
approved = bool(state.get('approved') or state.get('review_approved'))
awaiting = bool(state.get('awaiting_review') or stage in {'awaiting_review', 'manual_review'}) and not approved and not protected
human = bool(state.get('manual_required') or state.get('foreign_draft_text') or delivery_uncertain
or send_state == 'failed' or stage in {'manual_takeover', 'error', 'failed'} or (awaiting and not draft))
can_approve = bool(awaiting and draft and not human and not protected and not state.get('cancel_requested'))
reason = str(state.get('manual_reason') or state.get('review_reason') or state.get('stage_error')
or state.get('last_error') or state.get('stage_detail') or '')
if send_state == 'sent_uncommitted':
reason = '回复已发送但归档尚未完成。请先在企业微信核对发送结果,再转交人工;保留发送记录,不会自动重发。' + ('\n' + reason if reason else '')
elif delivery_uncertain:
reason = '发送结果不确定,已暂停自动发送。请先在企业微信核对是否已发出,再转交人工;不会自动重发。' + ('\n' + reason if reason else '')
elif state.get('foreign_draft_text'):
reason = '企业微信输入框已有人工草稿,请人工发送或清空后再继续。'
elif awaiting and not reason:
reason = '请核对回复草稿后通过并发送。' if draft else '尚无可发送的回复草稿,请人工核对来源消息。'
elif human and not reason:
reason = '当前任务需要人工核对,请查看来源消息后处理。'
can_retry = bool(not awaiting and not protected and not state.get('manual_required')
and not state.get('foreign_draft_text') and stage in {'retry_wait', 'error'})
return {'awaitingReview': awaiting, 'canApprove': can_approve, 'needsHuman': human,
'deliveryUncertain': delivery_uncertain, 'reviewReason': reason,
'canRetry': can_retry}
def pending_attention_counts(pending):
"""Count conversations, regardless of the visible queue filter or page."""
review = human = 0
for state in (pending or {}).values():
if not isinstance(state, dict):
continue
flags = task_review_state(state)
if flags['needsHuman']:
human += 1
elif flags['awaitingReview']:
review += 1
return {'review': review, 'human': human, 'total': review + human}