The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): hold an image tag glued to punctuation or split across lines
Teknium4dcef215cfe4
diff --git a/gateway/stream_consumer_directives.py b/gateway/stream_consumer_directives.pyindex 822e0ccd2e4..e047dfe5f3a 100644--- a/gateway/stream_consumer_directives.py+++ b/gateway/stream_consumer_directives.py@@ -18,20 +18,23 @@ _PENDING_MEDIA_RE = re.compile(re.IGNORECASE)_PENDING_FLAG_RE = re.compile(r"\[\[?[A-Za-z_]*\]?$")_STREAM_FLAGS = ("[[audio_as_voice]]", "[[as_document]]")-# An image tag still being typed (``extract_images`` hides the complete one): `` keep streaming. Whitespace in the URL ends the hold: that tag is-# never extracted, so it stays visible either way.+# The end of the text is still a prefix of an image tag ``extract_images`` could hide: ``!``,+# ``, and holding it would keep every exclamation off the live draft.+# Anything that leaves the tag's grammar (whitespace in the URL, ``<img alt``) ends the hold: that+# text is never extracted, so it stays visible either way._PENDING_IMAGE_RE = re.compile(- r"""!\[[^\]\n]*(?:\](?:\([^)\s]*)?)?$|(?<!\S)!$"""- r"""|<img\s[^>\n]*$|(?<!\S)<(?:i(?:m(?:g)?)?)?$""",- re.IGNORECASE)+ r"""!\[[^\]]*(?:\](?:\([^)\s]*)?)?\Z|(?<![\w)\]"'”’!?.*_~`])!\Z"""+ r"""|<(?:i(?:m(?:g(?:\s+(?:s(?:r(?:c(?:=["']?[^\s"'<>]*["']?\s*/?)?)?)?)?)?)?)?)?\Z"""+ r"""|</(?:i(?:m(?:g)?)?)?\Z""")def strip_image_tags_for_display(text: str) -> str:"""Hide the image tags ``extract_images`` delivers as attachments post-stream.- Each tag goes with the spaces and up to two line breaks after it, and nothing else is trimmed+ Each tag goes with up to two line breaks after it, and nothing else is trimmedor collapsed: an interim frame must stay a prefix of the next one, so text in front of the tag(already shown) is never rewritten."""@@ -42,20 +45,21 @@ def strip_image_tags_for_display(text: str) -> str:return texturls = {url for url, _ in images}for pattern, url_group in ((MD_IMAGE_TAG_PATTERN, 2), (HTML_IMAGE_TAG_PATTERN, 1)):- text = re.sub(f"(?:{pattern})[ \\t]*(?:\\r?\\n){{0,2}}",+ text = re.sub(f"(?:{pattern})(?:\\r?\\n){{0,2}}",lambda m, g=url_group: "" if m.group(g) in urls else m.group(0), text)return textdef has_pending_media_directive(cleaned: str) -> bool:- """True while the open line of cleaned streamed text may still become a hidden attachment.+ """True while the end of cleaned streamed text may still become a hidden attachment.Complete deliverable tags are already gone from *cleaned*, so a ``MEDIA:`` or image tag leftthere is unfinished or undeliverable; the final (non-interim) flush still shows the latter.Showing the unfinished part and hiding it later would break prefix-stable draft frames.+ Directives live on one line; an image tag's alt text may span several."""line = cleaned.rsplit("\n", 1)[-1]- if _PENDING_MEDIA_RE.search(line) or _PENDING_IMAGE_RE.search(line):+ if _PENDING_MEDIA_RE.search(line) or _PENDING_IMAGE_RE.search(cleaned):return Trueflag = _PENDING_FLAG_RE.search(line)return bool(flag) and any(f.startswith(flag.group(0)) for f in _STREAM_FLAGS)diff --git a/tests/gateway/test_streamed_markdown_images.py b/tests/gateway/test_streamed_markdown_images.pyindex 8bcf24ee084..5053edcb5f4 100644--- a/tests/gateway/test_streamed_markdown_images.py+++ b/tests/gateway/test_streamed_markdown_images.py@@ -187,21 +187,27 @@ async def _stream_frames(deltas):return [f.replace(cursor, "") if cursor else f for f in frames][email protected]("deltas, final", [+ (["Here is the render.\n\n!", "[barrel ren", "der](https://exa", "mple.com/r/b", "arrel.png)",+ "\n\nWant another ", "angle?"], "Here is the render.\n\nWant another angle?"),+ # "!" glued to prose: the extractor has no left boundary, so the "!" must not be shown first.+ (["Here is the chart:!", "[chart](https://example.com/chart.png) More"], "Here is the chart: More"),+ # The alt text may span lines; the hold covers the whole unfinished tag, not just the last line.+ (["Look: ", "done"], "Look: done"),+])@pytest.mark.asyncio-async def test_streamed_image_tag_never_shows_and_frames_stay_prefix_stable():+async def test_streamed_image_tag_never_shows_and_frames_stay_prefix_stable(deltas, final):"""No frame shows any part of the tag, and each frame extends the previous one: showing``", "\n\nWant another ", "angle?"])+ frames = await _stream_frames(deltas)assert frames, "nothing was streamed"for frame in frames:- assert " for f in frames[:-1]]for prev, nxt in itertools.pairwise(interim):assert nxt.startswith(prev), (prev, nxt)- assert frames[-1].strip() == "Here is the render.\n\nWant another angle?"+ assert frames[-1].strip() == final@pytest.mark.asyncio