← Back to PRs

#8042: feat(telegram): add media index for reply-to media lookup

by batumilove open 2026-02-03 14:13 View on GitHub →
channel: telegram stale
## Summary When users reply to old messages containing media, OpenClaw can now resolve the cached media file from the original message. This enables workflows where users can reply to an image/document/audio sent hours or days ago and the agent can still access that media. ## Changes - Add `MediaMeta` interface and `index.json` for tracking media by message ID - Update `saveMediaBuffer` to index media when metadata provided - Update `resolveMedia` to pass mediaMeta to saveMediaBuffer - Make `describeReplyTarget` async, lookup cached media for reply targets - Add `ReplyToMediaPath`/`ReplyToMediaType` to message context envelope - Extend `cleanOldMedia` to recurse into subdirs and prune stale index entries ## Motivation Replaces stale PR #5489 (which was based on a badly diverged branch with 4500+ file changes). The original issue: when a user replies to a media message, the agent receives `<media:image>` but cannot actually access the file because it may have been cleaned up or never indexed. ## How It Works 1. When media is saved via `saveMediaBuffer`, if `MediaMeta` is provided, an entry is written to `~/.openclaw/media/{subdir}/index.json` 2. The index maps `{channel}:{chatId}:{messageId}` → `{path, contentType, ts}` 3. When processing a reply-to message with media, `describeReplyTarget` looks up the cached file 4. If found and the file still exists, `ReplyToMediaPath` and `ReplyToMediaType` are included in the envelope 5. `cleanOldMedia` now recurses into subdirs (like `inbound/`) and prunes stale index entries ## Testing - Tested media indexing with Telegram photo/video/document messages - Verified reply-to lookup returns cached media path - Verified cleanup properly prunes both files and index entries <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR adds a lightweight on-disk media index to improve Telegram “reply-to old media” workflows. Media saved via `saveMediaBuffer` can now be indexed by `{channel}:{chatId}:{messageId}` in `~/.openclaw/media/<subdir>/index.json`, and `describeReplyTarget` is made async to look up cached media for replied-to messages and attach `ReplyToMediaPath` / `ReplyToMediaType` to the inbound context envelope. It also extends media cleanup to recurse into media subdirectories and attempts to prune index entries when files are deleted. <h3>Confidence Score: 3/5</h3> - This PR is likely safe to merge, but the new media index logic has some correctness risks under cleanup and concurrent writes. - Core feature wiring (passing MediaMeta, reply-target lookup, envelope fields) is straightforward, but the index maintenance has two notable pitfalls: stale entries may not be pruned unless deletions happen in the same run, and concurrent save operations can lose index updates due to read-modify-write without locking. These are intermittent/long-tail issues rather than immediate runtime failures, but they affect reliability of the new feature. - src/media/store.ts (index pruning and concurrent updates) <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs