Repository navigation
fix(feishu): pass quoted message media to Hermes when replying #37262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wengxiaoxiong
wants to merge
2
commits into
NousResearch:main
Choose a base branch
from
wengxiaoxiong:fix/feishu-reply-media-passthrough
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3065,7 +3065,23 @@ async def _process_inbound_message( | |
| or getattr(message, "root_id", None) | ||
| or None | ||
| ) | ||
| reply_to_text = await self._fetch_message_text(reply_to_message_id) if reply_to_message_id else None | ||
| parent_media_urls: List[str] = [] | ||
| parent_media_types: List[str] = [] | ||
| if reply_to_message_id: | ||
| reply_to_text, parent_media_urls, parent_media_types = ( | ||
| await self._fetch_parent_message_with_media(reply_to_message_id) | ||
| ) | ||
| else: | ||
| reply_to_text = None | ||
| if parent_media_urls: | ||
| media_urls = list(media_urls) + parent_media_urls | ||
| media_types = list(media_types) + parent_media_types | ||
| # If the current message is plain text but the quoted message has media, upgrade type | ||
| if inbound_type == MessageType.TEXT and parent_media_types: | ||
| try: | ||
| inbound_type = MessageType(parent_media_types[0]) | ||
| except ValueError: | ||
| inbound_type = MessageType.PHOTO | ||
|
|
||
| sender_primary = ( | ||
| getattr(sender_id, "open_id", None) | ||
|
|
@@ -3989,6 +4005,57 @@ async def _fetch_message_text(self, message_id: str) -> Optional[str]: | |
| logger.warning("[Feishu] Failed to fetch parent message %s", message_id, exc_info=True) | ||
| return None | ||
|
|
||
| async def _fetch_parent_message_with_media( | ||
| self, message_id: str | ||
| ) -> tuple[Optional[str], List[str], List[str]]: | ||
| """Fetch a parent/quoted message and return (text, media_urls, media_types). | ||
|
|
||
| Unlike _fetch_message_text, this also downloads images and files attached to | ||
| the quoted message so Hermes can see them when the user replies to a media message. | ||
| """ | ||
| if not self._client or not message_id: | ||
| return None, [], [] | ||
| # Text-only result may already be cached; re-use it (media already empty for pure-text). | ||
| if message_id in self._message_text_cache: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cache hit drops media for every later reply to the same quoted image/file. Cache the complete parent context including media paths and MIME types, or bypass the text-only cache for this lookup; add a repeated-reply regression test. |
||
| return self._message_text_cache[message_id], [], [] | ||
| try: | ||
| request = self._build_get_message_request(message_id) | ||
| response = await asyncio.to_thread(self._client.im.v1.message.get, request) | ||
| if not response or getattr(response, "success", lambda: False)() is False: | ||
| code = getattr(response, "code", "unknown") | ||
| msg = getattr(response, "msg", "message lookup failed") | ||
| logger.warning("[Feishu] Failed to fetch parent message %s: [%s] %s", message_id, code, msg) | ||
| return None, [], [] | ||
| items = getattr(getattr(response, "data", None), "items", None) or [] | ||
| parent = items[0] if items else None | ||
| body = getattr(parent, "body", None) | ||
| msg_type = getattr(parent, "msg_type", "") or "" | ||
| raw_content = getattr(body, "content", "") or "" | ||
| parent_mentions = getattr(parent, "mentions", None) if parent else None | ||
| normalized = normalize_feishu_message( | ||
| message_type=msg_type, | ||
| raw_content=raw_content, | ||
| mentions=parent_mentions, | ||
| bot=self._bot_identity(), | ||
| ) | ||
| text = normalized.text_content | ||
| if not text: | ||
| placeholder = normalized.metadata.get("placeholder_text") if isinstance(normalized.metadata, dict) else None | ||
| text = str(placeholder).strip() or None | ||
| self._message_text_cache[message_id] = text | ||
| while len(self._message_text_cache) > _FEISHU_MESSAGE_TEXT_CACHE_SIZE: | ||
| self._message_text_cache.popitem(last=False) | ||
|
|
||
| # Download images and files from the quoted message | ||
| media_urls, media_types = await self._download_feishu_message_resources( | ||
| message_id=message_id, | ||
| normalized=normalized, | ||
| ) | ||
| return text, media_urls, media_types | ||
| except Exception: | ||
| logger.warning("[Feishu] Failed to fetch parent message %s", message_id, exc_info=True) | ||
| return None, [], [] | ||
|
|
||
| def _extract_text_from_raw_content( | ||
| self, | ||
| *, | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parent_media_types[0]is a MIME value (for exampleimage/jpeg), not aMessageTypevalue (photo,audio, etc.), so this always falls into the PHOTO fallback. Please keep the triggering reply as TEXT and let the existing per-attachment MIME routing handle the merged parent media.