[PR #3211] [MERGED] feat: react to individual characters' segments in conversation-mode merged replies (#3210) #5844

Closed
opened 2026-10-22 04:20:06 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/3211
Author: @kolacheee
Created: 7/4/2026
Status: ✅ Merged
Merged: 7/4/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/3210-segment-reactions


📝 Commits (10+)

  • 5a300b9 feat: allow reacting to individual character segments in conversation mode (#3210)
  • d9c67a3 fix: keep segment reaction chips outside card-CSS scope and handle stale segment targets
  • 3724f96 fix: polish segment-reaction chrome (chip width cap, action-bar containing block, empty theme boxes, annotation parser parity)
  • 97358bf feat: attribute segment reactions to the exact quoted part in prompts
  • e931849 fix: harden exact-part reaction attribution (speaker-set parity, canonical names, content shape)
  • 9caea8e feat: inline reaction notes under the targeted part + character-to-character reactions
  • 5080cf8 fix: harden inline reaction notes and targeted reacts (linear react grammar, display-shape parity, verified injection targets, swipe mirror)
  • 2806197 fix: residual polish for inline notes and targeted reacts (marker line-equality, tiered dedupe, quoted-target tolerance, attachment-only targets, id pairing)
  • 7dd6aa1 fix: same-reply targeted reacts only aim at parts before the reactor's turn; clarify react instruction
  • 1ce009c Avoid empty grouped message wrappers

📊 Changes

12 files changed (+1104 additions, -209 deletions)

View changed files

📝 CHANGELOG.md (+3 -0)
📝 packages/client/src/components/chat/ConversationMessage.tsx (+74 -19)
📝 packages/client/src/components/chat/ConversationMessageGrouped.tsx (+133 -52)
📝 packages/client/src/components/chat/ConversationMessageShared.tsx (+20 -79)
📝 packages/client/src/components/chat/ConversationView.tsx (+10 -11)
📝 packages/client/src/components/chat/MessageReactions.tsx (+6 -4)
📝 packages/client/src/lib/reactions.ts (+102 -4)
📝 packages/server/src/routes/generate.routes.ts (+505 -34)
📝 packages/server/src/services/conversation/character-commands.ts (+61 -6)
📝 packages/shared/src/index.ts (+1 -0)
📝 packages/shared/src/types/chat.ts (+13 -0)
➕ packages/shared/src/utils/speaker-segments.ts (+176 -0)

📄 Description

Fixes #3210

Summary

In conversation mode, emoji reactions can now target an individual character's segment of a merged multi-character reply. Each speaker segment in the grouped layout gets its own add-reaction button (beside the speaker's name, on hover/tap) and its own reaction chip row; reactions without a segment target keep applying to the whole message exactly as before.

In the prompt, characters see segment reactions as a [User reacted with 😹, Aurey reacted with 🙄] line injected directly under the exact part they were aimed at (position conveys the target, like a chat app). And characters can now react to each other: [react: emoji="🙄" to "Character Name"] puts the reaction on that character's most recent part — same merged reply or recent history — rendered as a chip under it.

Why

A multi-character reply is stored as one messages row that the client splits into per-speaker segments, but reactions were a single flat list on the whole message — so in a group conversation you could only react to the entire block, never to one character's line (issue #3210). This implements the storage + client workaround suggested in the issue: the merged-generation model and the single PATCH /messages/{id}/extra call are unchanged.

Architecture

Layer Change
packages/shared/src/types/chat.ts MessageReaction gains optional segment (grouped-segment index) + segmentSpeaker (parsed speaker name at react time). Absent = whole-message, so existing data needs no migration.
packages/client/src/lib/reactions.ts toggleReaction identity is now (emoji, segment target); new splitReactionsBySegment routes each reaction to its segment only when the index is still in range and the stored speaker still matches and the segment has text — otherwise it falls back to the whole-message row, so an edit/regeneration that re-segments the content can never mis-attach a reaction to the wrong character (it degrades to whole-message display instead). findRetargetableUserReaction lets a new pick move the user's stale same-emoji/same-speaker entry instead of stacking a duplicate.
ConversationMessage.tsx / ConversationMessageShared.tsx Shell partitions reactions (only while the grouped layout is actually rendered — during edit everything falls back to the bottom row), passes per-segment lists + segment-aware handlers through the render context; block-bottom row now holds whole-message entries + orphans.
ConversationMessageGrouped.tsx Per-segment ReactionAddButton in the speaker name row (hover-toolbar visibility discipline, mobile tap supported) and per-segment MessageReactions chip row, in both classic and bubble styles. The data-card-css attribute moved from the block root onto per-segment content wrappers (+ one trailing-chrome wrapper), so the chip rows render outside card-CSS reach — preserving the same isolation invariant the shell keeps for the whole-message row.
MessageReactions.tsx Chip key includes the segment so an orphaned segment entry and a whole-message entry with the same emoji can coexist; toggle passes the full entry so the right one is toggled.
packages/shared/src/utils/speaker-segments.ts (new) The speaker-segment parsers (parseSpeakerTags, parseNamePrefixFormat, groupConsecutiveSegments, + combined parseGroupedSpeakerSegments) moved from ConversationMessageShared.tsx into shared, so the client's grouped layout and the server's reaction attribution derive segment indexes from the same code and can't drift. Parsers also compute start/end character offsets per group (for the inline injection), and stripLeadingMessageTimestamps — the display-shape timestamp strip both sides segment against — lives here (ConversationView now imports it instead of a local copy).
packages/server/src/routes/generate.routes.ts annotateContentWithReactions injects segment reactions inline under the targeted part in the prompt (grouped per part, [User reacted with 😹, Aurey reacted with 🙄]), resolving the stored index against the display shape and mapping into the prompt shape by (speaker, ordinal) validated by text containment — mapping failure falls back to a quoted end-note, stale index to speaker-only wording, unparseable to plain. Only canonical character names are echoed; disabled/removed characters are covered in the known-speaker set; excerpts truncate on code-point boundaries; segmentation caps at 32 KB; stale cross-swipe twins of inlined reactions are suppressed; the message object is replaced (not mutated) so follow-up generation passes can't double-annotate.
packages/server/src/services/conversation/character-commands.ts + generate.routes.ts [react] handler [react: emoji="🙄" to "Character Name"] — targeted character-to-character reactions. Deliberately a linear single-quantifier regex + deterministic body parser (a suffix-aware regex was super-linear → ReDoS, caught in review). Target resolution: the character's last part in the just-saved reply, else newest-first through recent history (skipping hidden command-anchor rows), resolved against all chat members (disabled included) + each message's author, matching the client's segmentation set. addMessageReactor is segment-aware (identity = emoji + segment + speaker, mirroring the client's toggleReaction), and writes mirror to every swipe row like the client PATCH route, so swiping doesn't drop character reactions. Unresolvable names fall back to the classic user-message react.

Commit 2 (fix: keep segment reaction chips outside card-CSS scope...) applies fixes from an adversarial multi-agent review of commit 1: card-CSS isolation for the chip rows, empty-segment targeting, chips disappearing during edit, duplicate entries across swipe re-segmentation, and prompt annotations claiming a speaker part the active swipe doesn't have. Commit 3 polishes findings from a verification pass over commit 2 (bubble chip width cap, action-bar containing block, phantom theme boxes, parser-precedence parity). Commits 4–5 move the segment parsers to packages/shared and add exact-part attribution, hardened per a further verification pass. Commits 6–7 (maintainer-requested follow-ups) switch the prompt to inline notes injected under the targeted part and add character-to-character targeted reactions, with a hardening round fixing what a fourth adversarial review found — most notably a ReDoS in the first draft of the extended [react:] grammar (linear rewrite, timed at 0.56 ms on inputs that previously hung for minutes) plus display-shape parity, verified injection targets, and swipe mirroring.

Known limitations

  • Reactions stay synced across swipes (existing behavior). A segment reaction made on one swipe usually won't align with another swipe's segmentation, in which case it displays as a whole-message reaction there. Re-reacting to the same speaker on the new swipe moves the user's entry rather than duplicating it.
  • Segment identity is (index, speaker name). If an edit reorders two segments by the same speaker, a reaction may attach to that speaker's other segment; it can never jump to a different character.
  • No affordance for reacting to narration (speaker-less) or empty segments — reactions target character lines with text.
  • The per-segment add button (like the existing hover-toolbar one) sits inside the card-CSS scope; only the chip rows (reaction state) are isolation-protected.
  • Because a grouped message now carries one [data-card-css] wrapper per segment (instead of one on the block root), a theme that styles the container element itself ([data-card-css] { border/background/padding }) draws per-segment boxes on grouped messages instead of one block-wide box. Descendant-selector reach is unchanged. Inherent to keeping chip rows outside theme reach; flagging for theme authors.
  • The server's "to X's part" prompt attribution is gated against the prompt-side content; a display-only regex script that rewrites speaker names can make it fall back to the plain "X reacted with 😀" wording (fail-safe direction — it omits rather than fabricates).

Test plan (manual verification needed)

  • Conversation mode with 2+ characters (classic style): hover a merged multi-character reply → a smile+ button appears next to each character's name; react to character A's line and a different emoji to character B's line → chips render under the correct segments.
  • After reacting, trigger another reply and Peek Prompt: the reacted message shows [<persona> reacted with <emoji>] on its own line directly under the targeted part — even when that character has several parts in the same merged reply.
  • Character-to-character reactions: with commands enabled, prompt a scene where one character would plausibly react to another (or ask them to). When the model emits [react: emoji="🙄" to "Name"], the chip appears under that character's part with the reacting character in the tooltip, and the next Peek Prompt shows both reactors in the inline note (e.g. [You reacted with 😹, Aurey Louvette reacted with 🙄]).
  • Classic untargeted [react: emoji="😂"] from characters still lands on your latest message, and survives swiping the reacted message.
  • Same in bubble style.
  • Toggle a segment chip off (click it) → chip disappears; server extra updated (reload page, chips persist correctly).
  • Whole-message reactions still work via the hover toolbar's add-reaction button, and pre-existing reactions from older chats still render at the block bottom.
  • 1:1 conversation chat: reaction behavior unchanged.
  • Edit the merged message so segments change (remove a speaker's line) → that segment's reaction falls back to the block-bottom row instead of attaching to the wrong character; it can still be removed there.
  • While the edit textarea is open, all reaction chips (including segment ones) show in the block-bottom row and come back to their segments after cancel/save.
  • Swipe/regenerate a reacted grouped message → stale segment reactions show as whole-message chips on the new swipe; re-reacting with the same emoji to the same character moves the chip instead of duplicating it.
  • A character card with custom CSS (e.g. a bubble theme) cannot restyle or hide the per-segment reaction chips (exclusive card-CSS mode especially).
  • Character [react:] reactions (bot reacting to your message) still work and don't merge into a segment entry.
  • Mobile viewport (~400px): tap a grouped message → per-segment add buttons appear, picker usable, chips don't overflow.
  • Light and dark mode: chips and add button legible in both.
  • Screenshots/GIF of per-segment reactions for this PR body.

Docs touched

  • CHANGELOG.md (Unreleased → Added).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/3211 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 7/4/2026 **Status:** ✅ Merged **Merged:** 7/4/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/3210-segment-reactions` --- ### 📝 Commits (10+) - [`5a300b9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5a300b936235e8f5dafeb6083f56df9052f03b1d) feat: allow reacting to individual character segments in conversation mode (#3210) - [`d9c67a3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d9c67a3a2d8b141157dbfb86c80deb02032e658f) fix: keep segment reaction chips outside card-CSS scope and handle stale segment targets - [`3724f96`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3724f96916a5c5ab33bf3534fee2f3261c3c4bec) fix: polish segment-reaction chrome (chip width cap, action-bar containing block, empty theme boxes, annotation parser parity) - [`97358bf`](https://github.com/Pasta-Devs/Marinara-Engine/commit/97358bfda0d171d797647bf961ff6b5450859785) feat: attribute segment reactions to the exact quoted part in prompts - [`e931849`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e93184937f169f037a22c4c64366158ab36a75ea) fix: harden exact-part reaction attribution (speaker-set parity, canonical names, content shape) - [`9caea8e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9caea8ec9372f8c00d2c4caf1d25590d1e855d31) feat: inline reaction notes under the targeted part + character-to-character reactions - [`5080cf8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5080cf8339d17d34b0811144f1ee906b439a5d9b) fix: harden inline reaction notes and targeted reacts (linear react grammar, display-shape parity, verified injection targets, swipe mirror) - [`2806197`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2806197480803874784e92ac995814315bd20e57) fix: residual polish for inline notes and targeted reacts (marker line-equality, tiered dedupe, quoted-target tolerance, attachment-only targets, id pairing) - [`7dd6aa1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7dd6aa141e7cfe7d1272001b7a69fff01c243b38) fix: same-reply targeted reacts only aim at parts before the reactor's turn; clarify react instruction - [`1ce009c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1ce009cdc422ad9ebca0bee94c5023e674fef973) Avoid empty grouped message wrappers ### 📊 Changes **12 files changed** (+1104 additions, -209 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+3 -0) 📝 `packages/client/src/components/chat/ConversationMessage.tsx` (+74 -19) 📝 `packages/client/src/components/chat/ConversationMessageGrouped.tsx` (+133 -52) 📝 `packages/client/src/components/chat/ConversationMessageShared.tsx` (+20 -79) 📝 `packages/client/src/components/chat/ConversationView.tsx` (+10 -11) 📝 `packages/client/src/components/chat/MessageReactions.tsx` (+6 -4) 📝 `packages/client/src/lib/reactions.ts` (+102 -4) 📝 `packages/server/src/routes/generate.routes.ts` (+505 -34) 📝 `packages/server/src/services/conversation/character-commands.ts` (+61 -6) 📝 `packages/shared/src/index.ts` (+1 -0) 📝 `packages/shared/src/types/chat.ts` (+13 -0) ➕ `packages/shared/src/utils/speaker-segments.ts` (+176 -0) </details> ### 📄 Description Fixes #3210 ## Summary In conversation mode, emoji reactions can now target an individual character's segment of a merged multi-character reply. Each speaker segment in the grouped layout gets its own add-reaction button (beside the speaker's name, on hover/tap) and its own reaction chip row; reactions without a segment target keep applying to the whole message exactly as before. In the prompt, characters see segment reactions as a `[User reacted with 😹, Aurey reacted with 🙄]` line injected **directly under the exact part** they were aimed at (position conveys the target, like a chat app). And characters can now react **to each other**: `[react: emoji="🙄" to "Character Name"]` puts the reaction on that character's most recent part — same merged reply or recent history — rendered as a chip under it. ## Why A multi-character reply is stored as one `messages` row that the client splits into per-speaker segments, but reactions were a single flat list on the whole message — so in a group conversation you could only react to the entire block, never to one character's line (issue #3210). This implements the storage + client workaround suggested in the issue: the merged-generation model and the single `PATCH /messages/{id}/extra` call are unchanged. ## Architecture | Layer | Change | |---|---| | `packages/shared/src/types/chat.ts` | `MessageReaction` gains optional `segment` (grouped-segment index) + `segmentSpeaker` (parsed speaker name at react time). Absent = whole-message, so existing data needs no migration. | | `packages/client/src/lib/reactions.ts` | `toggleReaction` identity is now (emoji, segment target); new `splitReactionsBySegment` routes each reaction to its segment only when the index is still in range **and** the stored speaker still matches **and** the segment has text — otherwise it falls back to the whole-message row, so an edit/regeneration that re-segments the content can never mis-attach a reaction to the wrong character (it degrades to whole-message display instead). `findRetargetableUserReaction` lets a new pick *move* the user's stale same-emoji/same-speaker entry instead of stacking a duplicate. | | `ConversationMessage.tsx` / `ConversationMessageShared.tsx` | Shell partitions reactions (only while the grouped layout is actually rendered — during edit everything falls back to the bottom row), passes per-segment lists + segment-aware handlers through the render context; block-bottom row now holds whole-message entries + orphans. | | `ConversationMessageGrouped.tsx` | Per-segment `ReactionAddButton` in the speaker name row (hover-toolbar visibility discipline, mobile tap supported) and per-segment `MessageReactions` chip row, in both classic and bubble styles. The `data-card-css` attribute moved from the block root onto per-segment content wrappers (+ one trailing-chrome wrapper), so the chip rows render **outside** card-CSS reach — preserving the same isolation invariant the shell keeps for the whole-message row. | | `MessageReactions.tsx` | Chip key includes the segment so an orphaned segment entry and a whole-message entry with the same emoji can coexist; toggle passes the full entry so the right one is toggled. | | `packages/shared/src/utils/speaker-segments.ts` (new) | The speaker-segment parsers (`parseSpeakerTags`, `parseNamePrefixFormat`, `groupConsecutiveSegments`, + combined `parseGroupedSpeakerSegments`) moved from `ConversationMessageShared.tsx` into shared, so the client's grouped layout and the server's reaction attribution derive segment indexes from the same code and can't drift. Parsers also compute `start`/`end` character offsets per group (for the inline injection), and `stripLeadingMessageTimestamps` — the display-shape timestamp strip both sides segment against — lives here (ConversationView now imports it instead of a local copy). | | `packages/server/src/routes/generate.routes.ts` | `annotateContentWithReactions` injects segment reactions **inline under the targeted part** in the prompt (grouped per part, `[User reacted with 😹, Aurey reacted with 🙄]`), resolving the stored index against the display shape and mapping into the prompt shape by (speaker, ordinal) **validated by text containment** — mapping failure falls back to a quoted end-note, stale index to speaker-only wording, unparseable to plain. Only canonical character names are echoed; disabled/removed characters are covered in the known-speaker set; excerpts truncate on code-point boundaries; segmentation caps at 32 KB; stale cross-swipe twins of inlined reactions are suppressed; the message object is replaced (not mutated) so follow-up generation passes can't double-annotate. | | `packages/server/src/services/conversation/character-commands.ts` + `generate.routes.ts` `[react]` handler | `[react: emoji="🙄" to "Character Name"]` — targeted character-to-character reactions. Deliberately a linear single-quantifier regex + deterministic body parser (a suffix-aware regex was super-linear → ReDoS, caught in review). Target resolution: the character's last part in the just-saved reply, else newest-first through recent history (skipping hidden command-anchor rows), resolved against **all** chat members (disabled included) + each message's author, matching the client's segmentation set. `addMessageReactor` is segment-aware (identity = emoji + segment + speaker, mirroring the client's `toggleReaction`), and writes mirror to every swipe row like the client PATCH route, so swiping doesn't drop character reactions. Unresolvable names fall back to the classic user-message react. | Commit 2 (`fix: keep segment reaction chips outside card-CSS scope...`) applies fixes from an adversarial multi-agent review of commit 1: card-CSS isolation for the chip rows, empty-segment targeting, chips disappearing during edit, duplicate entries across swipe re-segmentation, and prompt annotations claiming a speaker part the active swipe doesn't have. Commit 3 polishes findings from a verification pass over commit 2 (bubble chip width cap, action-bar containing block, phantom theme boxes, parser-precedence parity). Commits 4–5 move the segment parsers to `packages/shared` and add exact-part attribution, hardened per a further verification pass. Commits 6–7 (maintainer-requested follow-ups) switch the prompt to **inline notes injected under the targeted part** and add **character-to-character targeted reactions**, with a hardening round fixing what a fourth adversarial review found — most notably a ReDoS in the first draft of the extended `[react:]` grammar (linear rewrite, timed at 0.56 ms on inputs that previously hung for minutes) plus display-shape parity, verified injection targets, and swipe mirroring. ## Known limitations - Reactions stay synced across swipes (existing behavior). A segment reaction made on one swipe usually won't align with another swipe's segmentation, in which case it displays as a whole-message reaction there. Re-reacting to the same speaker on the new swipe *moves* the user's entry rather than duplicating it. - Segment identity is (index, speaker name). If an edit reorders two segments by the *same* speaker, a reaction may attach to that speaker's other segment; it can never jump to a different character. - No affordance for reacting to narration (speaker-less) or empty segments — reactions target character lines with text. - The per-segment add button (like the existing hover-toolbar one) sits inside the card-CSS scope; only the chip rows (reaction *state*) are isolation-protected. - Because a grouped message now carries one `[data-card-css]` wrapper per segment (instead of one on the block root), a theme that styles the container element itself (`[data-card-css] { border/background/padding }`) draws per-segment boxes on grouped messages instead of one block-wide box. Descendant-selector reach is unchanged. Inherent to keeping chip rows outside theme reach; flagging for theme authors. - The server's "to X's part" prompt attribution is gated against the prompt-side content; a display-only regex script that rewrites speaker names can make it fall back to the plain "X reacted with 😀" wording (fail-safe direction — it omits rather than fabricates). ## Test plan (manual verification needed) - [ ] Conversation mode with 2+ characters (classic style): hover a merged multi-character reply → a smile+ button appears next to each character's name; react to character A's line and a different emoji to character B's line → chips render under the correct segments. - [ ] After reacting, trigger another reply and Peek Prompt: the reacted message shows `[<persona> reacted with <emoji>]` on its own line **directly under the targeted part** — even when that character has several parts in the same merged reply. - [ ] Character-to-character reactions: with commands enabled, prompt a scene where one character would plausibly react to another (or ask them to). When the model emits `[react: emoji="🙄" to "Name"]`, the chip appears under that character's part with the reacting character in the tooltip, and the next Peek Prompt shows both reactors in the inline note (e.g. `[You reacted with 😹, Aurey Louvette reacted with 🙄]`). - [ ] Classic untargeted `[react: emoji="😂"]` from characters still lands on your latest message, and survives swiping the reacted message. - [ ] Same in bubble style. - [ ] Toggle a segment chip off (click it) → chip disappears; server extra updated (reload page, chips persist correctly). - [ ] Whole-message reactions still work via the hover toolbar's add-reaction button, and pre-existing reactions from older chats still render at the block bottom. - [ ] 1:1 conversation chat: reaction behavior unchanged. - [ ] Edit the merged message so segments change (remove a speaker's line) → that segment's reaction falls back to the block-bottom row instead of attaching to the wrong character; it can still be removed there. - [ ] While the edit textarea is open, all reaction chips (including segment ones) show in the block-bottom row and come back to their segments after cancel/save. - [ ] Swipe/regenerate a reacted grouped message → stale segment reactions show as whole-message chips on the new swipe; re-reacting with the same emoji to the same character moves the chip instead of duplicating it. - [ ] A character card with custom CSS (e.g. a bubble theme) cannot restyle or hide the per-segment reaction chips (exclusive card-CSS mode especially). - [ ] Character `[react:]` reactions (bot reacting to your message) still work and don't merge into a segment entry. - [ ] Mobile viewport (~400px): tap a grouped message → per-segment add buttons appear, picker usable, chips don't overflow. - [ ] Light and dark mode: chips and add button legible in both. - [ ] Screenshots/GIF of per-segment reactions for this PR body. ## Docs touched - `CHANGELOG.md` (Unreleased → Added). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:20:06 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5844
No description provided.