fix(gateway): skip MEDIA tags inside code blocks in extract_media#16585
Open
vominh1919 wants to merge 1 commit into
Open
fix(gateway): skip MEDIA tags inside code blocks in extract_media#16585vominh1919 wants to merge 1 commit into
vominh1919 wants to merge 1 commit into
Conversation
MEDIA:<path> tokens inside fenced code blocks and inline code were incorrectly treated as real attachment directives, causing the gateway to attempt delivery of non-existent files and producing noisy warnings. The extract_local_files() method already had code-block awareness, but extract_media() and _clean_for_display() did not. Applied the same pattern: build code_spans list, skip matches inside those spans. Fixes NousResearch#16434
Collaborator
13 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #16434
Problem
When the agent's response contains
MEDIA:tags inside fenced code blocks (``` ... ```) or inline code (`...`), the gateway incorrectly treats them as real attachment directives and tries to deliver the files. This also mangles code examples in streamed display.Root Cause
extract_media()inbase.pyand_clean_for_display()instream_consumer.pyusefinditer()/sub()over raw content with no code-block filtering. Ironically,extract_local_files()in the same file already has this exact code-block awareness pattern.Fix
Applied the same code-block-aware pattern already used by
extract_local_files():base.py—extract_media(): Buildcode_spanslist from fenced/inline code blocks, skip matches inside those spans, and only remove non-code MEDIA tags from cleaned content.stream_consumer.py—_clean_for_display(): Same pattern — build code spans, use_safe_sub()to preserve MEDIA tags inside code blocks.Files Changed
gateway/platforms/base.py— 20 lines added (code-block filtering inextract_media())gateway/stream_consumer.py— 15 lines added (code-block filtering in_clean_for_display())