fix: preserve camera permission intent and distinguish unknown reply expression

This commit is contained in:
zenord
2026-10-12 11:33:36 +08:00
parent d43c7d4e80
commit b521d368a5
26 changed files with 544 additions and 101 deletions
+32 -16
View File
@@ -12,6 +12,8 @@ class ReplyEmotion:
emotion: str = 'neutral'
intensity: int = 0
source: str = 'fallback'
reason: str = 'invalid_json'
parse_branch: str = 'safe_reply'
def clean_reply(value):
@@ -23,37 +25,51 @@ def clean_reply(value):
return value
def parse_reply(raw):
if not isinstance(raw, str) or len(raw) > 8192: return ReplyEmotion(SAFE_REPLY)
def parse_reply(raw, finish_reason=None):
# A damaged envelope alone is not evidence of a token cutoff.
def fallback(reply, reason, branch):
return ReplyEmotion(reply, reason='truncated' if finish_reason == 'length' else reason, parse_branch=branch)
if not isinstance(raw, str) or len(raw) > 8192:
return fallback(SAFE_REPLY, 'input_limit', 'safe_reply')
body = raw.strip()
if body.startswith('```'):
fenced = body.startswith('```')
if fenced:
body = re.sub(r'^```(?:json)?\s*', '', body, count=1).removesuffix('```').strip()
try: data = json.loads(body)
except (ValueError, RecursionError): data = None
reason = 'invalid_json'
if isinstance(data, dict):
reply = clean_reply(data.get('reply'))
if reply:
emotion, intensity = data.get('emotion'), data.get('intensity')
if (set(data) == {'reply', 'emotion', 'intensity'} and type(intensity) is int and
((emotion == 'neutral' and intensity == 0) or
(emotion in ('happy', 'angry', 'sad') and intensity in (1, 2)))):
return ReplyEmotion(reply, emotion, intensity, 'model')
return ReplyEmotion(reply)
required = {'reply', 'emotion', 'intensity'}
if not required.issubset(data): reason = 'missing_fields'
elif set(data) != required: reason = 'unexpected_fields'
elif not reply: reason = 'invalid_reply'
elif not isinstance(data['emotion'], str) or data['emotion'] not in ('neutral', 'happy', 'angry', 'sad'):
reason = 'invalid_emotion'
elif type(data['intensity']) is not int or not (
data['emotion'] == 'neutral' and data['intensity'] == 0 or
data['emotion'] != 'neutral' and data['intensity'] in (1, 2)):
reason = 'invalid_intensity'
else:
return ReplyEmotion(reply, data['emotion'], data['intensity'], 'model', 'none',
'fenced_json' if fenced else 'strict_json')
if reply: return fallback(reply, reason, 'json_reply')
for key in ('text', 'answer'):
reply = clean_reply(data.get(key))
if reply: return ReplyEmotion(reply)
# Recover a usable quoted reply from a damaged/truncated envelope without another model call.
if reply: return fallback(reply, reason, 'alternate_field')
# Recover only the reply string, never speak the envelope or make another model request.
match = re.search(r'"reply"\s*:\s*(")', body)
if match:
literal = body[match.start(1):]
branch = 'quoted_reply'
try: value, _ = json.JSONDecoder().raw_decode(literal)
except ValueError:
# Only a truncated string, never arbitrary suffix/JSON syntax recovery.
branch = 'repaired_string'
try: value = json.loads(literal.rstrip('\\') + '"')
except ValueError: value = None
reply = clean_reply(value)
if reply: return ReplyEmotion(reply)
if reply: return fallback(reply, reason, branch)
if not any(c in body for c in '{}[]') and not re.search(r'\b(?:reply|emotion|intensity)\s*[:=]', body):
reply = clean_reply(body)
if reply: return ReplyEmotion(reply)
return ReplyEmotion(SAFE_REPLY)
if reply: return fallback(reply, 'non_json', 'plain_text')
return fallback(SAFE_REPLY, reason, 'safe_reply')