Skip to content

The agent that grows with you

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.py
index 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): ``![alt``,
-# ``![alt](https://ho`` or ``<img src="…``. A bare "!" or "<" counts only at the start of a word,
-# so prose exclamations ("Great!") 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: ``!``,
+# ``![alt`` (the alt may span lines), ``![alt](https://ho``, ``<img src="…``, a closing ``</img``.
+# The extractor has no left boundary, so "chart:!" waits for the next delta too; only a "!" right
+# after a word, closing bracket/quote or another "!"/"?"/"." streams on, because that is how prose
+# ends a sentence ("world!"), 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 trimmed
or 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 text
urls = {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 text
def 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 left
there 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 True
flag = _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.py
index 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: ![first\nsec", "ond](https://example.com/c.png)", "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
``![barrel](https://ex`` and removing it later would break prefix-stable draft frames."""
- frames = await _stream_frames([
- "Here is the render.\n\n!", "[barrel ren", "der](https://exa", "mple.com/r/b",
- "arrel.png)", "\n\nWant another ", "angle?"])
+ frames = await _stream_frames(deltas)
assert frames, "nothing was streamed"
for frame in frames:
- assert "![" not in frame and "example.com" not in frame and "](" not in frame, frame
+ assert "!" not in frame and "example.com" not in frame and "](" not in frame, frame
interim = [f.rstrip() 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