Fix #84: AI markdownifyer plugin corrupts valid Markdown structures #123
@ -31,7 +31,13 @@ def modify_text(
|
||||
"The user's message contains an inline instruction marked with @ai. "
|
||||
+ (prompt or DEFAULT_MODIFIER_PROMPT).strip()
|
||||
+ " Return ONLY the resulting text, with no preamble, no explanation, no "
|
||||
"quotes and no code fences."
|
||||
"quotes and no code fences.\n"
|
||||
"Rules:\n"
|
||||
"1. Do NOT modify content inside Markdown fenced code blocks (delimited by "
|
||||
"triple backticks). Preserve the code block fences, the language identifier, "
|
||||
"and the code content exactly as written.\n"
|
||||
"2. Only modify prose outside code blocks. Apply the modification prompt only "
|
||||
"to non-code text."
|
||||
)
|
||||
if context:
|
||||
system += (
|
||||
|
||||
@ -124,8 +124,15 @@ def correct_text(api_key: str, prompt: str, text: str) -> tuple[str, dict | None
|
||||
system = (
|
||||
"You are a text correction engine. Apply the correction instruction below to "
|
||||
"the user's message and return ONLY the resulting text, with no preamble, no "
|
||||
"explanation, no quotes and no code fences. Preserve the original meaning, "
|
||||
"language, line breaks and markdown. Correction instruction: "
|
||||
"explanation, no quotes and no code fences.\n"
|
||||
"Rules:\n"
|
||||
"1. Do NOT modify content inside Markdown fenced code blocks (delimited by "
|
||||
"triple backticks). Preserve the code block fences, the language identifier, "
|
||||
"and the code content exactly as written.\n"
|
||||
"2. Only correct prose outside code blocks. Apply the correction prompt only "
|
||||
"to non-code text.\n"
|
||||
"3. Preserve the original meaning, language, line breaks, and markdown.\n"
|
||||
"Correction instruction: "
|
||||
+ (prompt or DEFAULT_CORRECTION_PROMPT).strip()
|
||||
)
|
||||
return gateway_complete(
|
||||
|
||||
38
tests/unit/services/ai_modifier.py
Normal file
38
tests/unit/services/ai_modifier.py
Normal file
@ -0,0 +1,38 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import unittest.mock
|
||||
|
||||
from devplacepy.services.ai_modifier import modify_text
|
||||
|
||||
|
||||
def test_modify_text_prompt_preserves_fenced_code_blocks():
|
||||
captured = {}
|
||||
|
||||
def fake_gateway(api_key, system, text, timeout, max_growth_factor):
|
||||
captured["system"] = system
|
||||
captured["text"] = text
|
||||
return ("fake result", {"calls": 1})
|
||||
|
||||
with unittest.mock.patch(
|
||||
"devplacepy.services.correction.gateway_complete", fake_gateway
|
||||
):
|
||||
result, _ = modify_text(
|
||||
"test-key",
|
||||
"Make it more formal",
|
||||
"Some text\n```python\nx = 1\n```\nMore text",
|
||||
)
|
||||
|
||||
assert result == "fake result"
|
||||
system = captured["system"]
|
||||
assert (
|
||||
"Do NOT modify content inside Markdown fenced code blocks" in system
|
||||
), "System prompt must instruct the model to preserve code blocks"
|
||||
assert (
|
||||
"Preserve the code block fences, the language identifier" in system
|
||||
), "System prompt must mention preserving language identifiers"
|
||||
assert (
|
||||
"Only modify prose outside code blocks" in system
|
||||
), "System prompt must limit modifications to non-code text"
|
||||
assert (
|
||||
"triple backticks" in system
|
||||
), "System prompt must mention triple backticks as the delimiter"
|
||||
36
tests/unit/services/correction.py
Normal file
36
tests/unit/services/correction.py
Normal file
@ -0,0 +1,36 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import unittest.mock
|
||||
|
||||
from devplacepy.services.correction import correct_text
|
||||
|
||||
|
||||
def test_correct_text_prompt_preserves_fenced_code_blocks():
|
||||
captured = {}
|
||||
|
||||
def fake_gateway(api_key, system, text, timeout, max_growth_factor):
|
||||
captured["system"] = system
|
||||
captured["text"] = text
|
||||
return ("fake result", {"calls": 1})
|
||||
|
||||
with unittest.mock.patch(
|
||||
"devplacepy.services.correction.gateway_complete", fake_gateway
|
||||
):
|
||||
result, _ = correct_text(
|
||||
"test-key", "Fix spelling", "Some text\n```python\nx = 1\n```\nMore text"
|
||||
)
|
||||
|
||||
assert result == "fake result"
|
||||
system = captured["system"]
|
||||
assert (
|
||||
"Do NOT modify content inside Markdown fenced code blocks" in system
|
||||
), "System prompt must instruct the model to preserve code blocks"
|
||||
assert (
|
||||
"Preserve the code block fences, the language identifier" in system
|
||||
), "System prompt must mention preserving language identifiers"
|
||||
assert (
|
||||
"Only correct prose outside code blocks" in system
|
||||
), "System prompt must limit corrections to non-code text"
|
||||
assert (
|
||||
"triple backticks" in system
|
||||
), "System prompt must mention triple backticks as the delimiter"
|
||||
Loading…
Reference in New Issue
Block a user