Skip to content

feat(feishu): include replied-to message content in agent context#472

Open
Cigarrr wants to merge 2 commits intochenhg5:mainfrom
Cigarrr:feat/feishu-reply-quote-context-issue-470
Open

feat(feishu): include replied-to message content in agent context#472
Cigarrr wants to merge 2 commits intochenhg5:mainfrom
Cigarrr:feat/feishu-reply-quote-context-issue-470

Conversation

@Cigarrr
Copy link
Copy Markdown
Contributor

@Cigarrr Cigarrr commented Apr 6, 2026

Summary

When a user replies to a specific message in a Lark/Feishu chat, the agent currently has no visibility into the quoted message content — it only sees the user's reply text. This PR fixes that by fetching the parent message via Im.Message.Get and prepending a formatted quote block to the agent's input.

Closes #470

Environment

  • Version: 1.2.2-beta.5
  • OS: macOS (Darwin 25.4.0, Apple Silicon)
  • Agent: Claude Code
  • Platform: Feishu/Lark (WebSocket mode)

Changes

  • fetchQuotedMessage(parentID): New method that retrieves the parent message content and formats it as [Quoted message from <sender>]:\n<content>\n\n
  • extractPostPlainText(content): New helper for parsing Lark rich-text (post) JSON into plain text, supporting both flat and locale-wrapped formats
  • onMessage: Captures ParentId from the incoming event and passes it to dispatchMessage
  • dispatchMessage: Fetches quoted content when parentID is non-empty, prepends it to text and post message types

Design decisions

  • Graceful degradation: Any API failure silently skips the quote — the user's message is always delivered
  • Reuses existing patterns: Same Im.Message.Get call used by parseMergeForward, same resolveUserName for sender resolution
  • Minimal blast radius: Only platform/feishu/feishu.go modified; no changes to core.Message struct or other platforms
  • Scope: Quote injection applies to text and post messages only (the most common reply scenarios). Image/audio/file replies still work as before, just without the quote prefix.

Test plan

  • go test ./... passes (pre-existing failure in core package unrelated to this change)
  • New unit tests for extractPostPlainText: flat format, locale-wrapped, no title, empty content, non-text tags
  • Manual test: reply to a text message in Lark group → agent sees quoted content
  • Manual test: reply to a post (rich text) message → agent sees extracted plain text
  • Manual test: reply to an image message → agent sees [image] placeholder
  • Manual test: normal (non-reply) message → no change in behavior

When a user replies to a specific message in a Lark/Feishu chat,
the bot now fetches the parent message content via Im.Message.Get
and prepends it as a quoted context block, so the agent can see
what the user is referring to.

- Add fetchQuotedMessage() that retrieves and formats the parent
  message content for text, post (rich text), and other message types
- Add extractPostPlainText() helper for parsing Lark post JSON
  (supports both flat and locale-wrapped formats)
- Graceful degradation: on any API failure the quote is silently
  skipped and the user's message is delivered as before
- Only triggers when ParentId is non-empty (reply scenario)
- Uses the same Im.Message.Get API already used by parseMergeForward

Closes chenhg5#470
@Cigarrr Cigarrr force-pushed the feat/feishu-reply-quote-context-issue-470 branch from 95e3bf6 to 7953fcb Compare April 6, 2026 18:59
Copy link
Copy Markdown
Owner

@chenhg5 chenhg5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great feature addition for Feishu reply context.

What I like:

  • Clean implementation with graceful degradation — API failures don't block the user's message
  • Good test coverage for extractPostPlainText (flat format, locale-wrapped, empty, non-text tags)
  • Minimal blast radius — only platform/feishu/feishu.go modified
  • Reuses existing patterns (Im.Message.Get, resolveUserName)

Edge cases handled:

  • Locale-wrapped post content ({"zh_cn": {...}})
  • Non-text message types (image, file → [image] placeholder)
  • Missing sender info → "unknown" fallback

Thanks for the contribution!

…messages

extractPostPlainText only handled "text" tags, silently dropping link
text, code blocks, and horizontal rules from quoted post messages. Add
a switch on elem.Tag to also extract "a" (link text), "code_block"
(fenced code with language), and "hr" (separator).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
chenhg5 pushed a commit that referenced this pull request Apr 8, 2026
… messages

In extractPostPlainText, now handles:
- "a" (link) tags: extracts link text alongside regular text
- "hr" tags: renders as markdown horizontal rule
- "code_block": skips empty language prefix in fence when language is unset

Based on work from PR #472 by @Cigarrr.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@chenhg5
Copy link
Copy Markdown
Owner

chenhg5 commented Apr 8, 2026

This PR has merge conflicts with main because the core fetchQuotedMessage feature was already merged into main (with additional improvements: context.Context propagation, raw API for card support, interactive card extraction, etc.).

I've cherry-picked the remaining unique improvements from this PR's second commit (link tag, hr tag, and improved code_block formatting in extractPostPlainText) into #508, with proper attribution.

This PR can be closed once #508 is merged. Thanks @Cigarrr for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Include replied-to message content in agent context (Lark/Feishu)

2 participants