Refactor sanitize_ai_text() (#649)

Co-authored-by: UltralyticsAssistant <[email protected]>
This commit is contained in:
Glenn Jocher
2025-11-09 22:16:29 +00:00
committed by GitHub
co-authored by UltralyticsAssistant
parent f9ca60959b
commit 2d05a2524c
3 changed files with 21 additions and 10 deletions
+11 -10
View File
@@ -6,7 +6,15 @@ import json
import re
from pathlib import Path
from .utils import ACTIONS_CREDIT, GITHUB_API_URL, MAX_PROMPT_CHARS, Action, get_response, remove_html_comments
from .utils import (
ACTIONS_CREDIT,
GITHUB_API_URL,
MAX_PROMPT_CHARS,
Action,
get_response,
remove_html_comments,
sanitize_ai_text,
)
REVIEW_MARKER = "## 🔍 PR Review"
ERROR_MARKER = "⚠️ Review generation encountered an error"
@@ -31,13 +39,6 @@ SKIP_PATTERNS = [
]
def _sanitize_ai_text(s: str) -> str:
"""Strip private-use citation tokens like '' and normalize whitespace."""
return re.sub(
r"[\uE000-\uF8FF]*\bcite[\uE000-\uF8FF]*(turn\d+(?:search|view)\d+|[\w\d]+)[\uE000-\uF8FF]*", "", s or ""
)
def parse_diff_files(diff_text: str) -> tuple[dict, str]:
"""Parse diff and return file mapping with line numbers AND augmented diff with explicit line numbers.
@@ -237,10 +238,10 @@ def generate_pr_review(
)
# Sanitize leaked tool-citation tokens from model output
response["summary"] = _sanitize_ai_text(response.get("summary", ""))
response["summary"] = sanitize_ai_text(response.get("summary", ""))
for c in response.get("comments", []):
if "message" in c:
c["message"] = _sanitize_ai_text(c["message"])
c["message"] = sanitize_ai_text(c["message"])
print(json.dumps(response, indent=2))
+2
View File
@@ -17,6 +17,7 @@ from .openai_utils import (
get_pr_summary_guidelines,
get_pr_summary_prompt,
get_response,
sanitize_ai_text,
)
from .version_utils import check_pubdev_version, check_pypi_version
@@ -39,5 +40,6 @@ __all__ = (
"get_pr_summary_prompt",
"get_response",
"remove_html_comments",
"sanitize_ai_text",
"ultralytics_actions_info",
)
+8
View File
@@ -4,6 +4,7 @@ from __future__ import annotations
import json
import os
import re
import time
import requests
@@ -33,6 +34,13 @@ SYSTEM_PROMPT_ADDITION = """Guidance:
"""
def sanitize_ai_text(s: str) -> str:
"""Strip private-use citation tokens like '' and normalize whitespace."""
return re.sub(
r"[\uE000-\uF8FF]*\bcite[\uE000-\uF8FF]*(turn\d+(?:search|view)\d+|[\w\d]+)[\uE000-\uF8FF]*", "", s or ""
)
def remove_outer_codeblocks(string):
"""Removes outer code block markers and language identifiers from a string while preserving inner content."""
string = string.strip()