Skip to content

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

Merged
Soulter merged 2 commits into
AstrBotDevs:masterfrom
Ars1027:codex/fix/9848-lark-leading-mention
Aug 31, 2026
Merged

fix(lark): recognize commands after leading bot mention#9872
Soulter merged 2 commits into
AstrBotDevs:masterfrom
Ars1027:codex/fix/9848-lark-leading-mention

Conversation

@Ars1027

@Ars1027 Ars1027 commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

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

Normalize leading self-mentions in Lark messages so commands and wake-prefix handling receive the intended message text.

Bug Fixes:

  • Fix Lark command and wake-prefix matching for messages that begin with a mention of the bot.
  • Ensure active Agent runs can be stopped through commands sent after a leading bot mention.

Enhancements:

  • Normalize Lark message text while preserving the structured mention component and existing behavior for non-self mentions and callers without bot identity.

Tests:

  • Add regression coverage for bot mentions, command handling, identity matching, natural-language messages, and backward compatibility.

@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.

@Soulter
Soulter merged commit b0f2f90 into AstrBotDevs:master Aug 31, 2026
20 checks passed
@Ars1027
Ars1027 deleted the codex/fix/9848-lark-leading-mention branch August 31, 2026 03:58
BegoniaHe pushed a commit to Xero-Team/AstrBot that referenced this pull request Sep 1, 2026
…9872)

Skip a leading self mention in the Lark text projection used for wake
and command matching. Keep Comp.At on the message chain, and do not pass
bot identity into quoted-message reconstruction.

Upstream-Commit: b0f2f90
Upstream-Author: Ars1027 <118655114+Ars1027@users.noreply.github.com>
Upstream-PR: AstrBotDevs#9872
Sync-Disposition: adapt
Fork-Adaptation: Apply the skip onto the current helper that already serializes image/file/audio/video components. Keep tests under tests/unit/platform/test_lark_adapter.py.
Tested: uv run pytest tests/unit/platform/test_lark_adapter.py -q
AI-Generated: true
Generated-At: 2026-08-31T23:46:42Z
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 等指令未被识别

2 participants