The API server's _resolve_media_to_data_urls ran MEDIA_TAG_CLEANUP_RE over the raw final_response, bypassing the sentinel seam this branch added in gateway/platforms/base.py. A provider that leaked its end-of-sequence token glued to the last MEDIA path (MEDIA:/x.png<|eos|>) therefore still produced no inline image and returned the raw tag to the HTTP client (non-stream and SSE chat completions) - the exact symptom fixed for the chat platforms. The scan now stops at _terminal_sentinel_start and drops the control token only when a tag actually resolved, so unrelated responses stay byte-identical. _terminal_sentinel_start also recognises a run of repeated exact tokens (MEDIA:/x.png<|eos|><|eos|>), so a doubled leak no longer hides the attachment either; mid-text or non-exact sentinels are still not a media boundary. Review finding: api_server sibling MEDIA surface bypassed _mask_media_scan_text; doubled terminal sentinel still leaked
63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
"""MEDIA: tag → base64 data-URL resolution for the API server (salvage of #2696).
|
|
|
|
Remote OpenAI-compatible frontends can't read local file paths, so
|
|
``MEDIA:<path>`` image tags in final responses are inlined as markdown
|
|
data URLs before crossing the HTTP boundary.
|
|
"""
|
|
|
|
import base64
|
|
import unittest
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("aiohttp")
|
|
|
|
from gateway.platforms.api_server import _resolve_media_to_data_urls # noqa: E402
|
|
|
|
# 1x1 transparent PNG
|
|
_PNG_BYTES = base64.b64decode(
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNgYGBgAAAABQAB"
|
|
"h6FO1AAAAABJRU5ErkJggg=="
|
|
)
|
|
|
|
|
|
class TestResolveMediaToDataUrls(unittest.TestCase):
|
|
def _write_png(self, tmpdir_name="hermes_media_test"):
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
d = Path(tempfile.mkdtemp(prefix=tmpdir_name))
|
|
p = d / "shot.png"
|
|
p.write_bytes(_PNG_BYTES)
|
|
return p
|
|
|
|
def test_media_tag_inlined(self):
|
|
p = self._write_png()
|
|
out = _resolve_media_to_data_urls(f"Here you go: MEDIA:{p}")
|
|
self.assertIn("data:image/png;base64,", out)
|
|
self.assertNotIn("MEDIA:", out)
|
|
|
|
def test_backtick_wrapped_tag(self):
|
|
p = self._write_png()
|
|
out = _resolve_media_to_data_urls(f"See `MEDIA:{p}` above")
|
|
self.assertIn("data:image/png;base64,", out)
|
|
|
|
def test_terminal_eos_sentinel_does_not_block_inlining(self):
|
|
"""A leaked terminal ``<|eos|>`` glued to the tag (#111046) inlines exactly like the clean
|
|
response; the control token is dropped rather than returned to the HTTP client."""
|
|
p = self._write_png()
|
|
clean = f"Here you go: MEDIA:{p}"
|
|
self.assertEqual(_resolve_media_to_data_urls(clean + "<|eos|>"), _resolve_media_to_data_urls(clean))
|
|
|
|
def test_missing_file_left_untouched(self):
|
|
text = "MEDIA:/nonexistent/path/shot.png"
|
|
self.assertEqual(_resolve_media_to_data_urls(text), text)
|
|
|
|
def test_non_image_left_untouched(self):
|
|
text = "MEDIA:/tmp/archive.zip"
|
|
self.assertEqual(_resolve_media_to_data_urls(text), text)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|