Skip to content

Commit cf8bcef

Browse files
committed
fix(composer): retain automatic mention provenance
Signed-off-by: morgmart <[email protected]>
1 parent 2ce0137 commit cf8bcef

3 files changed

Lines changed: 76 additions & 11 deletions

File tree

‎desktop/src/features/messages/ui/MessageComposer.tsx‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,21 +464,20 @@ function MessageComposerImpl({
464464
pubkeys: suggestion.pubkey ? [suggestion.pubkey] : [],
465465
});
466466
},
467+
onImplicitPrefixInserted: (prefix) => {
468+
if (!effectiveDraftKey) return;
469+
implicitAgentMentionPrefixByDraftRef.current.set(
470+
effectiveDraftKey,
471+
prefix,
472+
);
473+
trimMapToSize(implicitAgentMentionPrefixByDraftRef.current, 200);
474+
},
467475
onPulseAddressLock: addressPulse.pulseOne,
468476
profiles,
469477
richText,
470478
});
471479
addressedMentionRestore.restoreAddressedAgentMentionsRef.current =
472480
restoreAddressedAgentMentions;
473-
if (effectiveDraftKey) {
474-
implicitAgentMentionPrefixByDraftRef.current.set(
475-
effectiveDraftKey,
476-
lockedAgents.length > 0
477-
? `${lockedAgents.map((agent) => `@${agent.displayName}`).join(" ")} `
478-
: "",
479-
);
480-
trimMapToSize(implicitAgentMentionPrefixByDraftRef.current, 200);
481-
}
482481
React.useLayoutEffect(() => {
483482
if (!audienceScope || editTarget != null) return;
484483
restoreAddressedAgentMentions();

‎desktop/src/features/messages/ui/useAgentAddressLockPicker.test.mjs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,49 @@ test("repeatedly selecting an explicitly unpinned agent keeps its mentions manua
789789
assert.deepEqual(pulsedPubkeys, []);
790790
});
791791

792+
test("restoring after an agent rename keeps the existing automatic mention", async () => {
793+
const { act, renderHook } = await import("@testing-library/react");
794+
const { useAgentAddressLockPicker } = await import(
795+
"./useAgentAddressLockPicker.ts"
796+
);
797+
const appliedEdits = [];
798+
const registeredMentions = [];
799+
const oldName = "OldName";
800+
const newName = "NewName";
801+
const { result, rerender } = renderHook(
802+
({ displayName }) =>
803+
useAgentAddressLockPicker({
804+
applyAutocompleteEdit: (edit) => appliedEdits.push(edit),
805+
audience: { pubkeys: ["agent-pubkey"], addPubkey: () => {} },
806+
audienceScope: "channel-scope",
807+
mentions: {
808+
getDraftMentionRefs: () => [
809+
{ displayName: oldName, pubkey: "agent-pubkey", isAgent: true },
810+
],
811+
getMentionDisplayName: () => displayName,
812+
registerMentionPubkey: (...args) => registeredMentions.push(args),
813+
},
814+
onPulseAddressLock: () => {},
815+
profiles: {},
816+
richText: {
817+
getPlainTextAndCursor: () => ({
818+
text: `@${oldName} authored draft`,
819+
cursor: 23,
820+
}),
821+
},
822+
}),
823+
{ initialProps: { displayName: oldName } },
824+
);
825+
826+
rerender({ displayName: newName });
827+
act(() => result.current.restoreAddressedAgentMentions());
828+
829+
assert.deepEqual(appliedEdits, []);
830+
assert.deepEqual(registeredMentions, [
831+
[newName, "agent-pubkey", { isAgent: true }],
832+
]);
833+
});
834+
792835
test("an addressed agent keeps its resolved name while mention state clears during send", async () => {
793836
const { renderHook } = await import("@testing-library/react");
794837
const { useAgentAddressLockPicker } = await import(

‎desktop/src/features/messages/ui/useAgentAddressLockPicker.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export function useAgentAddressLockPicker({
6060
mentions,
6161
onAddressAgentMention,
6262
onAutoPinAgentMention,
63+
onImplicitPrefixInserted,
6364
onPulseAddressLock,
6465
profiles,
6566
richText,
@@ -73,6 +74,8 @@ export function useAgentAddressLockPicker({
7374
suggestion: MentionSuggestion,
7475
options: { reinstateExcluded: boolean },
7576
) => void;
77+
/** Records the exact automatic prefix at the moment it is inserted. */
78+
onImplicitPrefixInserted?: (prefix: string) => void;
7679
onPulseAddressLock: (pubkey: string) => void;
7780
profiles?: UserProfileLookup;
7881
richText: UseRichTextEditorResult;
@@ -241,10 +244,12 @@ export function useAgentAddressLockPicker({
241244
});
242245
const { text } = richText.getPlainTextAndCursor();
243246
if (getMentionOffsets(text, suggestion.displayName).length === 0) {
247+
const insertedText = `@${suggestion.displayName} `;
248+
onImplicitPrefixInserted?.(insertedText);
244249
applyAutocompleteEdit({
245250
replaceFromOffset: 0,
246251
replaceToOffset: 0,
247-
insertText: `@${suggestion.displayName} `,
252+
insertText: insertedText,
248253
preserveSelection: text.length > 0,
249254
reassertMentionCaret: false,
250255
});
@@ -297,6 +302,7 @@ export function useAgentAddressLockPicker({
297302
mentions.openMentionPicker,
298303
mentions.registerMentionPubkey,
299304
onAddressAgentMention,
305+
onImplicitPrefixInserted,
300306
onPulseAddressLock,
301307
removeAddressedAgentMentions,
302308
richText.getPlainTextAndCursor,
@@ -386,8 +392,21 @@ export function useAgentAddressLockPicker({
386392
return { pubkey, displayName };
387393
});
388394
const { text } = richText.getPlainTextAndCursor();
395+
// A profile can rename an agent while this draft is off-screen. Mention
396+
// refs retain the identity of its already-inserted automatic prefix, so
397+
// use that identity as well as the current display name when deciding
398+
// whether restoration is needed.
399+
const presentAgentPubkeys = new Set(
400+
mentions
401+
.getDraftMentionRefs(text)
402+
.filter((ref) => ref.isAgent)
403+
.map((ref) => normalizePubkey(ref.pubkey)),
404+
);
389405
for (const agent of targetAgents) {
390-
if (getMentionOffsets(text, agent.displayName).length > 0) {
406+
if (
407+
presentAgentPubkeys.has(agent.pubkey) ||
408+
getMentionOffsets(text, agent.displayName).length > 0
409+
) {
391410
mentions.registerMentionPubkey(agent.displayName, agent.pubkey, {
392411
isAgent: true,
393412
});
@@ -398,6 +417,7 @@ export function useAgentAddressLockPicker({
398417
(agent) =>
399418
(!unpinnedAgentPubkeysRef.current.has(agent.pubkey) ||
400419
allowedUnpinned.has(agent.pubkey)) &&
420+
!presentAgentPubkeys.has(agent.pubkey) &&
401421
getMentionOffsets(text, agent.displayName).length === 0,
402422
);
403423
if (missingAgents.length === 0) return text;
@@ -410,6 +430,7 @@ export function useAgentAddressLockPicker({
410430
const insertedText = `${missingAgents
411431
.map((agent) => `@${agent.displayName}`)
412432
.join(" ")} `;
433+
onImplicitPrefixInserted?.(insertedText);
413434
applyAutocompleteEdit({
414435
replaceFromOffset: 0,
415436
replaceToOffset: 0,
@@ -426,8 +447,10 @@ export function useAgentAddressLockPicker({
426447
[
427448
applyAutocompleteEdit,
428449
audience.pubkeys,
450+
mentions.getDraftMentionRefs,
429451
mentions.getMentionDisplayName,
430452
mentions.registerMentionPubkey,
453+
onImplicitPrefixInserted,
431454
profiles,
432455
richText.focusEnd,
433456
richText.getPlainTextAndCursor,

0 commit comments

Comments
 (0)