Skip to content

refactor(shared): share the bare-mention scanner across Discord, Teams, and Slack#652

Open
bensabic wants to merge 1 commit into
mainfrom
refactor/shared-bare-mention-scanner
Open

refactor(shared): share the bare-mention scanner across Discord, Teams, and Slack#652
bensabic wants to merge 1 commit into
mainfrom
refactor/shared-bare-mention-scanner

Conversation

@bensabic

Copy link
Copy Markdown
Contributor

The Discord adapter (fixed in #651) converts bare @mentions with a regex. A single-character lookbehind can't tell whether an @ sits inside a URL, a code span, or an email host, so it still mangles cases the regex can't see. The Slack adapter already had a robust character-scanning resolver — replaceBareMentions — that handles exactly those cases, but it lived inside adapter-slack.

This lifts that scanner into @chat-adapter/shared and points every adapter that does bare-mention conversion at it: Slack (dedup, no behavior change), Discord, and Teams — which had the identical /@(\w+)/g<at>$1</at> bug in two places.

What changed

  • @chat-adapter/shared — new replaceBareMentions (+ MentionReplacer type), moved verbatim from Slack. It skips inline/fenced code, scheme + schemeless URLs, and existing <…> tokens before handing each real @name to a platform-specific replacer. Adds a dedicated test file (the scanner had no direct tests before).
  • Slack — sources replaceBareMentions from @chat-adapter/shared; local mentions.ts deleted. Behavior unchanged.
  • Discord — regex → scanner in both conversion sites.
  • Teams — same fix for <at>…</at> mention tags.

What this fixes (Discord + Teams)

Across both the {markdown}/AST and {raw}/plain-string paths:

input before after
https://github.com/@vercel https://github.com/<@vercel> preserved
twitter.com/@jack twitter.com/<@jack> preserved
`ping @here` `ping <@here>` preserved
<@123> (Discord, raw) <<@123>> preserved

Emails (user@example.com), period-prefixed mentions (docs.@everyone), and existing tokens keep working.

Notes

  • .changeset/config.json uses fixed: [["chat", "@chat-adapter/*"]], so the @chat-adapter/shared minor bump carries the whole family to a minor release; the discord / teams patch changesets exist for their changelog text. Slack has no behavioral change, so it gets no changeset.
  • Rebased on top of fix(discord): don't mangle email addresses into mentions #651. That PR's discord-email-mentions.md changeset stays; this PR's scanner supersedes its regex implementation, so the two Discord changelog entries read as a progression.
…ord, teams, slack

Introduce a context-aware bare-@mention resolver in @chat-adapter/shared
(moved verbatim from the slack adapter) that skips code spans, URLs,
schemeless hosts, and existing angle-bracket tokens before converting a
bare @name via a platform-specific replacer.

- shared: export replaceBareMentions + MentionReplacer; add direct tests
- slack: source replaceBareMentions from @chat-adapter/shared (delete the
  local mentions.ts); behavior unchanged
- discord: replace the /@(\w+)/g regex with the scanner in both conversion
  sites, fixing emails, @Handles in URLs, and code-span mentions being
  mangled into mentions and double-wrapping of existing <@id> tokens
- teams: same fix (identical bug) for <at> mention tags

Signed-off-by: Ben Sabic <bensabic@users.noreply.github.com>
@bensabic bensabic requested a review from a team as a code owner June 28, 2026 12:21
@vercel

vercel Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
chat Ready Ready Preview, Comment, Open in v0 Jun 28, 2026 12:21pm
chat-sdk-nextjs-chat Ready Ready Preview, Comment, Open in v0 Jun 28, 2026 12:21pm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant