#14794: fix: parse inline MEDIA: tokens in agent replies
stale
size: S
Cluster:
Media Handling Improvements
## Problem
Fixes #13790
When an agent reply contains an inline `MEDIA:` tag (e.g. `Here is your audio: MEDIA:/tmp/voice.opus`), it was not detected and delivered as an attachment on Telegram. The literal text path was sent instead.
## Root Cause
`splitMediaFromOutput` in `src/media/parse.ts` had an early-return guard that skipped any line where `MEDIA:` didn't appear at the start of the line (after trimming). This meant inline `MEDIA:` tokens were never matched by the regex.
## Fix
Removed the `trimmedStart.startsWith("MEDIA:")` guard so the `MEDIA_TOKEN_RE` regex runs on every line, catching both line-start and inline tokens.
## Tests
- Added regression test in `src/media/reproduce_issue.test.ts` covering inline MEDIA tokens
- Test fails on `main` (confirms it catches the bug), passes with the fix
- All 59 existing media tests continue to pass
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Changes update `splitMediaFromOutput` to scan every line for `MEDIA:` tokens (not just lines that start with `MEDIA:`), enabling inline tokens like `Here is your audio: MEDIA:/tmp/voice.opus` to be extracted into `mediaUrls` and removed from the rendered text. A small guard was also added to avoid the "path with spaces" fallback when the token is inline (to reduce accidental capture of trailing prose).
A new regression test file exercises inline tokens, spacing after the colon, quoted paths with spaces, and confirms fenced code blocks are not parsed for media tokens.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge and appears to fix the reported Telegram attachment parsing issue.
- The change is localized to media token parsing, includes targeted regression tests (including inline-token coverage), and existing behavior around fenced code blocks and audio tags remains intact based on code review. The only notable risk area is the semantics of extracting inline `MEDIA:` sequences in prose, but that concern is already tracked in prior review threads for this PR.
- src/media/parse.ts (token syntax/edge-case semantics)
<sub>Last reviewed commit: 2a9824d</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#18811: fix(media): require file extension for ambiguous MEDIA: path detection
by aldoeliacim · 2026-02-17
86.2%
#19868: fix: prevent media token regex from matching markdown bold text
by sanketgautam · 2026-02-18
84.2%
#18890: fix(media): parse tool-result MEDIA directives with shared parser
by teededung · 2026-02-17
83.8%
#19399: telegram: fix MEDIA false positives and partial final drop
by HOYALIM · 2026-02-17
83.2%
#13961: fix(telegram): support media attachments in replied Telegram messages
by shan-mx · 2026-02-11
81.8%
#21110: fix(tts): deliver audio via structured mediaUrl instead of MEDIA: t...
by hydro13 · 2026-02-19
81.1%
#23627: fix(telegram,feishu): pass mediaLocalRoots through channel action a...
by rockkoca · 2026-02-22
80.1%
#20294: fix(message): thread mediaLocalRoots through channel plugin dispatch
by odrobnik · 2026-02-18
79.9%
#20735: fix: skip auto-attaching tool MEDIA: paths already sent via message t…
by anillBhoi · 2026-02-19
79.8%
#9817: fix(media): resolve relative paths before reading local files (#8759)
by lailoo · 2026-02-05
79.3%