Add BLIGHT:: document-scope triggers and case-insensitive matching
- Split TRIGGER_PATTERN into INLINE_PATTERN (BLIGHT:) and DOCUMENT_PATTERN (BLIGHT::), both case-insensitive - Inline triggers replace only the trigger line (existing behaviour) - Document-scope triggers replace the entire file; multiple BLIGHT:: triggers in one file are processed sequentially, each seeing the previous result - Updated FAILED_TEMPLATE to two-line format with BLIGHT_FAILED and BLIGHT_ERROR - Added complete_document() to AIProvider ABC and GeminiProvider with a dedicated system prompt instructing the model to return the full document Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
72
processor.py
72
processor.py
@@ -5,8 +5,12 @@ from ai import GeminiProvider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
TRIGGER_PATTERN = re.compile(r"^BLIGHT:\s+(.+)$", re.MULTILINE)
|
||||
FAILED_TEMPLATE = "<!-- BLIGHT_FAILED: {instruction} -->"
|
||||
# Inline trigger: BLIGHT: <instruction> (single colon, case-insensitive)
|
||||
INLINE_PATTERN = re.compile(r"^BLIGHT:(?!:)\s+(.+)$", re.MULTILINE | re.IGNORECASE)
|
||||
# Document-scope trigger: BLIGHT:: <instruction> (double colon, case-insensitive)
|
||||
DOCUMENT_PATTERN = re.compile(r"^BLIGHT::\s+(.+)$", re.MULTILINE | re.IGNORECASE)
|
||||
|
||||
FAILED_TEMPLATE = "<!-- BLIGHT_FAILED: {instruction} -->\n<!-- BLIGHT_ERROR: {error} -->"
|
||||
|
||||
_MAX_RETRIES = 3
|
||||
_RETRY_DELAYS = [1, 2, 4] # seconds between attempts
|
||||
@@ -17,42 +21,80 @@ _provider = GeminiProvider()
|
||||
def process_document(content: str) -> tuple[str, bool]:
|
||||
"""Scan content for BLIGHT triggers and process each one.
|
||||
|
||||
Inline triggers (BLIGHT:) are processed first in document order, each
|
||||
replacing only the trigger line. Document-scope triggers (BLIGHT::) are
|
||||
processed next in document order, each replacing the entire file content
|
||||
and operating on the result of the previous.
|
||||
|
||||
Returns:
|
||||
(updated_content, changed) where changed is True if any triggers
|
||||
were found and the content was modified.
|
||||
"""
|
||||
triggers = list(TRIGGER_PATTERN.finditer(content))
|
||||
if not triggers:
|
||||
has_inline = bool(INLINE_PATTERN.search(content))
|
||||
has_document = bool(DOCUMENT_PATTERN.search(content))
|
||||
if not has_inline and not has_document:
|
||||
return content, False
|
||||
|
||||
# Process triggers one by one. After each replacement the string length
|
||||
# may change, so we re-search on the updated content each iteration.
|
||||
changed = False
|
||||
for _ in range(len(triggers)):
|
||||
match = TRIGGER_PATTERN.search(content)
|
||||
|
||||
# --- Pass 1: inline triggers ---
|
||||
# Re-search after each replacement since string length may change.
|
||||
inline_count = len(INLINE_PATTERN.findall(content))
|
||||
for _ in range(inline_count):
|
||||
match = INLINE_PATTERN.search(content)
|
||||
if not match:
|
||||
break
|
||||
|
||||
instruction = match.group(1).strip()
|
||||
trigger_line = match.group(0)
|
||||
logger.info("Processing trigger: %s", instruction)
|
||||
logger.info("Processing inline trigger: %s", instruction)
|
||||
|
||||
replacement = _call_with_retry(content, instruction)
|
||||
replacement = _call_with_retry(content, instruction, document_scope=False)
|
||||
content = content[:match.start()] + replacement + content[match.end():]
|
||||
changed = True
|
||||
|
||||
# --- Pass 2: document-scope triggers ---
|
||||
# Each trigger operates on the result of the previous.
|
||||
doc_count = len(DOCUMENT_PATTERN.findall(content))
|
||||
for _ in range(doc_count):
|
||||
match = DOCUMENT_PATTERN.search(content)
|
||||
if not match:
|
||||
break
|
||||
|
||||
instruction = match.group(1).strip()
|
||||
logger.info("Processing document-scope trigger: %s", instruction)
|
||||
|
||||
# Remove the trigger line before passing to AI so it doesn't appear
|
||||
# in the rewritten document. Also consume the trailing newline that
|
||||
# follows the trigger line, if present.
|
||||
trigger_start, trigger_end = match.start(), match.end()
|
||||
if trigger_end < len(content) and content[trigger_end] == "\n":
|
||||
trigger_end += 1
|
||||
content_without_trigger = content[:trigger_start] + content[trigger_end:]
|
||||
|
||||
result = _call_with_retry(content_without_trigger, instruction, document_scope=True)
|
||||
|
||||
if result.startswith("<!-- BLIGHT_FAILED:"):
|
||||
# On failure, restore the trigger line and insert the failure comment.
|
||||
content = content[:trigger_start] + result + content[trigger_end:]
|
||||
else:
|
||||
content = result
|
||||
|
||||
changed = True
|
||||
|
||||
return content, changed
|
||||
|
||||
|
||||
def _call_with_retry(document: str, instruction: str) -> str:
|
||||
def _call_with_retry(document: str, instruction: str, *, document_scope: bool) -> str:
|
||||
"""Call the AI provider with up to _MAX_RETRIES attempts.
|
||||
|
||||
Returns the AI response on success, or a BLIGHT_FAILED comment on
|
||||
exhausted retries.
|
||||
Returns the AI response on success, or BLIGHT_FAILED/BLIGHT_ERROR comments
|
||||
on exhausted retries.
|
||||
"""
|
||||
last_error: Exception | None = None
|
||||
for attempt in range(_MAX_RETRIES):
|
||||
try:
|
||||
if document_scope:
|
||||
return _provider.complete_document(document, instruction)
|
||||
return _provider.complete(document, instruction)
|
||||
except Exception as exc:
|
||||
last_error = exc
|
||||
@@ -74,4 +116,4 @@ def _call_with_retry(document: str, instruction: str) -> str:
|
||||
instruction,
|
||||
last_error,
|
||||
)
|
||||
return FAILED_TEMPLATE.format(instruction=instruction)
|
||||
return FAILED_TEMPLATE.format(instruction=instruction, error=last_error)
|
||||
|
||||
Reference in New Issue
Block a user