Skip to content

fix(lark): recognize commands after leading bot mention - #9872

Open
Ars1027 wants to merge 1 commit into
AstrBotDevs:masterfrom
Ars1027:codex/fix/9848-lark-leading-mention
Open

fix(lark): recognize commands after leading bot mention#9872
Ars1027 wants to merge 1 commit into
AstrBotDevs:masterfrom
Ars1027:codex/fix/9848-lark-leading-mention

Conversation

@Ars1027

@Ars1027 Ars1027 commented Aug 30, 2026

Copy link
Copy Markdown

Fixes #9848

Problem

In Lark group chats, a message sent as @bot /stop keeps the leading bot mention in message_str. The mention wakes the bot, but wake-prefix and command matching still receive text such as @Bot /stop, so built-in and plugin commands are not recognized. An active Agent may then capture the message as a follow-up instead of stopping.

Solution

Normalize the textual projection in the Lark adapter. When the first parsed component is an At targeting the bot, omit that component from message_str while preserving it in the structured message chain.

The bot OpenID is the primary identity. The display name is used only when the mention has no identifier. Keeping this normalization in the platform adapter avoids changing shared wake or command behavior for other platforms.

Changes

  • Extend _build_message_str_from_components() with optional bot identity parameters while preserving its default behavior.

  • Remove only a leading bot-self mention from Lark's textual message projection.

  • Preserve non-self mentions, non-leading mentions, structured At components, and existing callers.

  • Add regression tests for built-in commands, plugin commands, natural-language messages, missing mention IDs, same-name users with different OpenIDs, empty input, and backward compatibility.

  • No public API changes or new dependencies.

  • This is NOT a breaking change.

Testing

Automated checks completed locally:

  • ruff format .
  • ruff check .
  • python -m py_compile astrbot/core/platform/sources/lark/lark_adapter.py tests/test_lark_adapter.py
  • git diff --check

The added pytest coverage exercises:

  • @bot /stop -> /stop
  • @bot /plugin_command -> /plugin_command
  • @bot hello -> hello
  • same-name mentions with a different OpenID remain unchanged
  • name fallback applies only when the mention ID is missing
  • calls without bot identity retain the previous behavior

Manual end-to-end verification was completed on AstrBot v4.27.4 in Ubuntu Docker:

  • @bot /help invoked the built-in help handler.
  • @bot /stop stopped an active Agent run.
  • @bot <natural language> continued through the normal LLM path.
  • QQ/OneBot /help remained functional.

Sanitized runtime evidence:

[Core][INFO] [lark] [At:<bot_open_id>] /stop
[Core][INFO] Agent execution was requested to stop by user.
[Core][INFO] Requested to stop 1 running tasks.

Notes for Reviewer

  • The structured At component is intentionally preserved; only message_str is normalized.
  • Only the first component can be omitted, and only when it identifies the bot.
  • OpenID comparison intentionally takes precedence over display-name comparison to avoid stripping mentions of same-name users.
  • Quoted-message text construction keeps the previous behavior because existing callers do not pass bot identity.

Checklist / 检查清单

  • The change fixes an existing reported bug rather than introducing a new feature.
  • Verification steps and sanitized runtime evidence are provided above.
  • No new dependencies are introduced.
  • The change does not introduce malicious code.

Summary by Sourcery

Fix Lark command recognition by normalizing leading bot mentions in message text without altering the structured message chain.

Bug Fixes:

  • Normalize Lark messages with a leading bot mention so built-in, plugin, and natural-language commands are recognized correctly.

Enhancements:

  • Preserve structured mention components while removing only the leading self-mention from the textual message projection, using OpenID-first identity matching with a display-name fallback.

Tests:

  • Add regression coverage for self and non-self mentions, command and natural-language messages, missing identifiers, same-name users, empty input, and backward-compatible calls.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels Aug 30, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 飞书群聊中前置 @机器人 导致 /stop 等指令未被识别

1 participant