Skip to content

feat: add mention entities support with structured @[uid:name] format#134

Merged
yujiawei merged 3 commits intodmwork-org:mainfrom
Jerry-Xin:feat/mention-refactor
Mar 30, 2026
Merged

feat: add mention entities support with structured @[uid:name] format#134
yujiawei merged 3 commits intodmwork-org:mainfrom
Jerry-Xin:feat/mention-refactor

Conversation

@Jerry-Xin
Copy link
Copy Markdown
Collaborator

Closes #133

What

Add support for the new mention.entities protocol in the bot adapter, enabling structured mention parsing for LLM consumption.

Key Changes

  • mention-utils.ts: Full rewrite — parseMentionEntities(), convertContentForLLM(), buildSenderPrefix()
  • inbound.ts: Integrate mention-utils, build sender prefix as displayName(uid)
  • types.ts: Add MentionEntity interface
  • api-fetch.ts: Fetch member info for mention resolution
  • v2 entities parsing with v1 fallback (uids + regex)

Protocol

Input (from frontend):

content: "Hey @[jerry:Jerry Xin] check this"
mention.entities: [{uid:"jerry", offset:4, length:14}]

Output (to LLM):

Jerry Xin(jerry): Hey @Jerry Xin check this

Tests

237 unit tests covering:

  • Entity parsing (v1/v2/mixed)
  • Content conversion with offset/length validation
  • Sender prefix formatting
  • Edge cases: emoji, CJK, surrogate pairs, overlapping entities

Files Changed (8 files, +1089/-173)

File Changes
mention-utils.ts New: full mention processing module
mention-utils.test.ts 237 tests
inbound.ts Integrate mention-utils
inbound.test.ts Updated tests
types.ts +MentionEntity interface
api-fetch.ts +member info fetching
channel.ts Config updates
actions.ts Action handler updates

Related PRs

  • dmwork-org/dmworkim#824: backend validation tests
  • dmwork-org/dmwork-web#737: frontend mention rendering

- Add MentionEntity interface and entities field to MentionPayload
- Fix MENTION_PATTERN with lookbehind for email exclusion and extended
  Unicode support (Latin accented, Japanese kana, Korean syllables)
- Add parseStructuredMentions for @[uid:name] format parsing
- Add convertStructuredMentions with incremental offset tracking
- Add buildEntitiesFromFallback for plain @name entity construction
- Add extractMentionUids with invalid-entities-to-uids fallback
- Add convertContentForLLM for history message mention conversion
- Add learnMembersFromMessage with entities-first learning
- Extend sendMessage/sendMediaMessage to accept mentionEntities
- Fix sendText name-as-uid bug in channel.ts
- Inject group member list into LLM prompt for @[uid:name] learning
- Support mixed structured + fallback mentions in reply processing
- Rebuild uids from sorted entities for consistent ordering
- types.ts: add MentionEntity interface + entities field to MentionPayload
- mention-utils.ts: add parseStructuredMentions, convertContentForLLM,
  buildSenderPrefix, extractMentionUids with email-aware lookbehind
- inbound.ts: add learnMembersFromMessage with entities v2 priority path,
  v1 positional fallback; integrate sender prefix + content conversion
- actions.ts: use buildEntitiesFromFallback for outbound mentions
- api-fetch.ts: support mentionEntities in sendMessage/sendMediaMessage

Fixes email-in-content causing positional mapping offset (Bug 3/6)
- Delete learnMembersFromMessage entirely (API is single source of truth)
- convertContentForLLM v1 fallback: memberMap name->uid lookup instead of
  positional pairing with uids (fixes email/non-member offset bug)
- buildMemberListPrefix: ≤10 members full list, >10 members tool hint
- 251 tests passing
@yujiawei yujiawei merged commit ee814ff into dmwork-org:main Mar 30, 2026
1 check passed
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.

feat: add mention entities support with structured @[uid:name] format

2 participants