Files
kefu/wechat_rpa/review_policy.py
T
2026-09-30 19:28:13 +08:00

69 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Shared review decisions; model observation never grants or consumes approval.
A judge verdict of risk 'high' holds the reply for a human in every judge mode
(shadow, score_only, arbitrate, or a verdict recorded without a mode). The admin
「客户端配置」 page promises this built-in rule is always on and cannot be turned
off; the judge mode only decides which candidate is sent, not whether a
high-risk reply may go out unreviewed.
"""
import re
import string
import unicodedata
# Older backends may deliver a pasted "服用,用法" as one keyword.
_KEYWORD_SEPARATORS = re.compile(r'[,,、;;]')
_ASCII_PUNCTUATION = frozenset(string.punctuation)
# Unicode punctuation, separators and invisible format marks (zero-width space).
_IGNORED_CATEGORIES = frozenset({'Pc', 'Pd', 'Ps', 'Pe', 'Pi', 'Pf', 'Po', 'Zs', 'Zl', 'Zp', 'Cf'})
def _match_key(text):
"""NFKC (full-width -> ASCII) + casefold, without whitespace or punctuation,
so '能不 能吃' and '能不能吃?' both match the keyword '能不能吃'."""
text = unicodedata.normalize('NFKC', str(text or '')).casefold()
return ''.join(ch for ch in text if not (
ch.isspace() or ch in _ASCII_PUNCTUATION or unicodedata.category(ch) in _IGNORED_CATEGORIES))
def judge_review_reason(state, *, current_mode=None):
# current_mode is accepted for older callers only; no judge mode releases a
# high-risk verdict.
state = state if isinstance(state, dict) else {}
if str(state.get('judge_risk') or '').strip().lower() != 'high':
return ''
return '模型裁判评估为高风险'
def selective_review_reason(state, customer_text, reply_text, *, rules, current_mode=None):
reason = judge_review_reason(state)
if reason:
return reason
# Normalize each side separately: keywords carry no whitespace after
# normalization, so none can match across the customer/reply boundary.
combined = _match_key(customer_text) + '\n' + _match_key(reply_text)
for rule in rules or []:
if not isinstance(rule, dict) or not rule.get('enabled', True):
continue
keywords = rule.get('keywords') or []
if not isinstance(keywords, (list, tuple)):
continue
words = (_match_key(part) for word in keywords
for part in _KEYWORD_SEPARATORS.split(str(word or '')))
# A keyword that normalizes to nothing (e.g. only punctuation) is ignored.
if any(word and word in combined for word in words):
return '命中审核规则:' + (str(rule.get('label') or '').strip() or '未命名规则')
return ''
def can_recheck_review(state):
"""Policy changes cannot resolve delivery or independent manual safeguards."""
if not isinstance(state, dict):
return False
if state.get('send_state') or str(state.get('stage') or '') in {
'sending', 'uncertain', 'unknown', 'receipt_check', 'failed', 'sent_uncommitted',
'manual_takeover', 'error',
}:
return False
return not (state.get('manual_required') or state.get('foreign_draft_text')
or state.get('foreign_draft_at') is not None or state.get('cancel_requested'))