"""Bounded replacement frames for DSH's provisional assistant text.""" import hashlib import re import time _PRIVATE_OUTPUT = re.compile( r"(?i)https?://|bearer\s|api[_-]?key\b|authorization\b|clientKey\b|accessToken\b|\bsk-[a-z0-9_-]{10,}|" r"quality-turn:|(?:scope|execution|attempt|release|actor|session)[_-]?id\b|" r"(?:^|\n)\s*(?:\{|```)|<\s*/?\s*[a-z][a-z0-9:-]*(?:\s|/?>)|" r"系统提示词|内部请求归属标记|本轮已确认任务、身份和上下文|本运行时使用DSH|" r"【平台能力与职责】|【默认业务口径】|【内容组织】" ) _UUID = re.compile(r"(?i)\b[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}\b") _NONPRINTING = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]") def display_text(value): if not isinstance(value, str) or _PRIVATE_OUTPUT.search(value): return "" return _NONPRINTING.sub("", _UUID.sub("[标识已隐藏]", value)).strip()[:4000] class DraftPublisher: def __init__(self, emit, *, clock=time.monotonic): self.emit = emit self.clock = clock self.last_time = float("-inf") self.current = "" def update(self, attempt, text): candidate = display_text(text) if len(candidate) < 16: self.clear() return now = self.clock() if candidate == self.current or now - self.last_time < 0.25: return self.current = candidate self.last_time = now key = hashlib.sha256(attempt.encode("utf-8")).hexdigest()[:24] self.emit({"type": "progress", "dshDraft": candidate, "dshAttemptId": key}) def clear(self): if not self.current: return self.current = "" self.emit({"type": "progress", "dshDraft": ""})