import re TRUNCATION_MARKER = "[内容已由 Knowledge Adapter 截断]" _MARKDOWN_LINK_PATTERN = re.compile(r"!?\[[^\]\n]*\]\([^\)\n]*\)") _FENCED_CODE_PATTERN = re.compile(r"```[\s\S]*?(?:```|\Z)") def _safe_cutoff(content: str, cutoff: int) -> int: protected_starts = [ match.start() for pattern in (_MARKDOWN_LINK_PATTERN, _FENCED_CODE_PATTERN) for match in pattern.finditer(content) if match.start() < cutoff < match.end() ] return min(protected_starts, default=cutoff) def truncate_content(content: str, *, max_chars: int) -> str: if len(content) <= max_chars: return content if max_chars < len(TRUNCATION_MARKER): raise ValueError("max_chars is too small for the truncation marker") prefix_chars = max_chars - len(TRUNCATION_MARKER) prefix_chars = _safe_cutoff(content, prefix_chars) return f"{content[:prefix_chars]}{TRUNCATION_MARKER}"