assistant/backend/browser/discussion_bounds.py

33 lines
1.8 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.

"""Ground explicit discussion-group bounds only from direct user messages."""
from __future__ import annotations
import re
def requested_mode(messages: object) -> str | None:
mode = None
if not isinstance(messages, list):
return mode
for message in messages:
if not isinstance(message, dict) or message.get("role") != "user" or not isinstance(message.get("text"), str):
continue
text = re.sub(r"```[\s\S]*?```|https?://\S+", "", message["text"])
text = re.sub(r'“[^”]*”|‘[^’]*’|「[^」]*」|『[^』]*』|"[^"\n]*"|`[^`\n]*`', "", text)
current = None
changed = False
for clause in re.split(r"[,,。;;!?!?\n]", text):
if clause.lstrip().startswith(">") or re.search(r"例如|比如|示例|(?:网页|页面|原文)(?:上)?(?:说|写|显示)|引用", clause):
continue
if re.search(r"不限(?:制)?(?:讨论组|评论组)|不限制(?:讨论组|评论组)(?:数|数量|范围)?|(?:不再只|不要只|改为全部|改为所有)[^。;\n]{0,18}(?:讨论|评论)", clause):
current, changed = None, True
continue
first = re.search(r"(?:只|仅|限定)[^。;;\n]{0,18}(?:(?:第一|第1|首)(?:组|个)(?:讨论|评论)|(?:讨论|评论)(?:区)?(?:的)?(?:第一|第1)组)", clause)
one = re.search(r"(?:只|仅|限定)[^。;;\n]{0,16}(?:(?:一|1)(?:个|组)(?:讨论|评论)(?:组)?|(?:讨论|评论)组(?:只|仅)?(?:一|1)个)", clause)
found = first or one
if found and not re.search(r"(?:不要|不用|别|不必|不需要|不是|不)[^,,]{0,4}$", clause[:found.start()]):
current = "first" if first else current or "one"
changed = True
if changed:
mode = current
return mode