Skip to content

Commit b7bcb1d

Browse files
baxenmbot
andauthored
Tag the last-tagged agent when a Solo chat composer is shown empty (#1615)
Signed-off-by: Bradley Axen <[email protected]> Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
1 parent 9e9cec2 commit b7bcb1d

5 files changed

Lines changed: 236 additions & 10 deletions

File tree

‎src/bundled/channels/ConversationTab.test.tsx‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,16 @@ vi.mock("./ChannelBody", () => ({
6767
),
6868
}));
6969
vi.mock("../../features/messages/MessageComposer", () => ({
70-
MessageComposer: ({ focusRequest }: { focusRequest?: number }) => (
71-
<div data-focus-request={focusRequest}>Composer content</div>
70+
MessageComposer: ({
71+
focusRequest,
72+
shown,
73+
}: {
74+
focusRequest?: number;
75+
shown?: boolean;
76+
}) => (
77+
<div data-focus-request={focusRequest} data-shown={shown}>
78+
Composer content
79+
</div>
7280
),
7381
}));
7482
const owners: ReturnType<typeof createOutbox>[] = [];
@@ -662,3 +670,36 @@ it("lets the tab supply Solo chat identity without repeating a visible heading",
662670
expect(within(conversation).getByText("Conversation content")).toBeVisible();
663671
expect(within(conversation).getByText("Composer content")).toBeVisible();
664672
});
673+
674+
it("tells the composer when its tab is shown, apart from whether showing visits", () => {
675+
const channel = { id: "beta", name: "Chat", channelType: "solo" as const };
676+
const snapshot = { status: "ready", channels: [channel] };
677+
const idle = () => () => {};
678+
const profiles = new Map();
679+
const session = {
680+
channels: { list: () => snapshot, subscribeList: idle },
681+
profiles: { snapshot: () => profiles, subscribe: idle },
682+
agentChoices: { snapshot: () => undefined, subscribe: idle },
683+
} as unknown as RelaySession;
684+
const tab = (active: boolean) => (
685+
<ConversationTab
686+
tab={{ id: "chat", kind: "conversation", channelId: "beta" }}
687+
channel={channel}
688+
session={session}
689+
scope="tab-shown-test"
690+
openLink={() => false}
691+
canOpenLink={() => false}
692+
openThread={() => {}}
693+
close={() => {}}
694+
active={active}
695+
visit={false}
696+
/>
697+
);
698+
const view = render(tab(true));
699+
const composer = screen.getByText("Composer content");
700+
expect(composer).toHaveAttribute("data-shown", "true");
701+
view.rerender(tab(false));
702+
expect(composer).toHaveAttribute("data-shown", "false");
703+
view.rerender(tab(true));
704+
expect(composer).toHaveAttribute("data-shown", "true");
705+
});

‎src/bundled/channels/ConversationTab.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ export function ConversationTab({
175175
marksRead={opening?.markRead !== false}
176176
/>
177177
<ConversationComposer
178+
// Shown follows the tab, not whether showing it visits the chat.
179+
shown={active}
178180
onSend={setSent}
179181
{...(focusRequest ? { focusRequest } : {})}
180182
/>

‎src/features/messages/MessageComposer.test.tsx‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8259,6 +8259,123 @@ it("edits the last message from an untouched automatic tag and restores that tag
82598259
expect(h.input()).not.toHaveAccessibleName("Edit message");
82608260
});
82618261

8262+
it("tags the agent last tagged in a Solo chat whenever an empty composer is shown", () => {
8263+
const viewer = "e".repeat(64);
8264+
const h = mount({}, undefined, viewer);
8265+
h.setLibrary([first, second]);
8266+
h.setProfiles(
8267+
new Map([
8268+
[first.pubkey, { name: "Ada", isAgent: true }],
8269+
[second.pubkey, { name: "Bea", isAgent: true }],
8270+
]),
8271+
);
8272+
h.retarget({
8273+
ownSoloConversation: true,
8274+
requireMention: true,
8275+
session: {
8276+
...h.session,
8277+
channels: { ...h.session.channels, list: () => soloList },
8278+
} as unknown as RelaySession,
8279+
});
8280+
expect(h.input()).toHaveValue("");
8281+
h.setRows([
8282+
editableMessage({
8283+
id: "1".repeat(64),
8284+
authorId: viewer,
8285+
mentions: [first.pubkey],
8286+
}),
8287+
editableMessage({
8288+
id: "2".repeat(64),
8289+
authorId: viewer,
8290+
mentions: [second.pubkey],
8291+
}),
8292+
// Neither an agent's message nor a thread reply chooses the agent.
8293+
editableMessage({
8294+
id: "3".repeat(64),
8295+
authorId: second.pubkey,
8296+
mentions: [first.pubkey],
8297+
}),
8298+
editableMessage({
8299+
id: "4".repeat(64),
8300+
authorId: viewer,
8301+
mentions: [first.pubkey],
8302+
threadRootId: "1".repeat(64),
8303+
}),
8304+
]);
8305+
expect(h.input()).toHaveValue("@Bea ");
8306+
expect(h.commands().recipients).toEqual([
8307+
{ pubkey: second.pubkey, name: "Bea" },
8308+
]);
8309+
// A removed tag stays removed while the chat is shown.
8310+
act(() => h.input().removeRecipient(second.pubkey));
8311+
h.fill("");
8312+
expect(h.input()).toHaveValue("");
8313+
// Coming back to the chat tags the agent again, whether the conversation or
8314+
// its retained tab is shown again.
8315+
h.present(false);
8316+
h.present(true);
8317+
expect(h.input()).toHaveValue("@Bea ");
8318+
act(() => h.input().removeRecipient(second.pubkey));
8319+
h.fill("");
8320+
h.retarget({ shown: false });
8321+
expect(h.input()).toHaveValue("");
8322+
h.retarget({ shown: true });
8323+
expect(h.input()).toHaveValue("@Bea ");
8324+
// Text without a tag is left as written.
8325+
act(() => h.input().removeRecipient(second.pubkey));
8326+
h.fill("for people");
8327+
h.present(false);
8328+
h.present(true);
8329+
expect(h.input()).toHaveValue("for people");
8330+
// Off with remembered agents.
8331+
h.fill("");
8332+
setRememberAgentsPreference(false);
8333+
h.present(false);
8334+
h.present(true);
8335+
expect(h.input()).toHaveValue("");
8336+
setRememberAgentsPreference(true);
8337+
});
8338+
8339+
it("waits for complete agent choices before tagging the agent last tagged in a Solo chat", () => {
8340+
const viewer = "e".repeat(64);
8341+
// The local agent is usable before the library holding the latest agent loads.
8342+
const control = localAgentsFixture([
8343+
{ ...localAgent, pubkey: first.pubkey, name: "Ada" },
8344+
]);
8345+
const h = mount(
8346+
{ scope: `https://relay.example.test:${"d".repeat(64)}` },
8347+
control,
8348+
viewer,
8349+
);
8350+
h.setLibrary([], "loading");
8351+
h.retarget({
8352+
ownSoloConversation: true,
8353+
requireMention: true,
8354+
session: {
8355+
...h.session,
8356+
channels: { ...h.session.channels, list: () => soloList },
8357+
} as unknown as RelaySession,
8358+
});
8359+
h.setRows([
8360+
editableMessage({
8361+
id: "1".repeat(64),
8362+
authorId: viewer,
8363+
mentions: [first.pubkey],
8364+
}),
8365+
editableMessage({
8366+
id: "2".repeat(64),
8367+
authorId: viewer,
8368+
mentions: [second.pubkey],
8369+
}),
8370+
]);
8371+
expect(h.input()).toHaveValue("");
8372+
h.setLibrary([{ pubkey: second.pubkey, name: "Bea" }]);
8373+
expect(h.input()).toHaveValue("@Bea ");
8374+
expect(h.commands().recipients).toEqual([
8375+
{ pubkey: second.pubkey, name: "Bea" },
8376+
]);
8377+
});
8378+
82628379
it("keeps remembered agents while typing during upload without taking focus when publication finishes", async () => {
82638380
const key = keypair();
82648381
const agent = { pubkey: key.pubkey, name: "Honey" };

‎src/features/messages/MessageComposer.tsx‎

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { useNonmemberMentions } from "./useNonmemberMentions";
3737
import { useOldAgentMentions } from "./useOldAgentMentions";
3838
import { useKnownAgentPubkeys } from "../agents/use-known";
3939
import { knownAgentPubkeys } from "../agents/known";
40-
import { rememberedMentions } from "./remembered-mentions";
40+
import { lastMentionedAgent, rememberedMentions } from "./remembered-mentions";
4141
import { useAgentChoices } from "../agents/use-choices";
4242
import { ownSolo } from "../solo/labels";
4343
import { soloHasPeople, soloRoster, useSoloHasPeople } from "../solo/people";
@@ -223,6 +223,9 @@ export type MessageComposerProps = {
223223
ownSoloConversation?: boolean | undefined;
224224
/** Seeds an empty new-chat draft; follow-ups use remembered mentions. */
225225
defaultMention?: MentionRecipient | undefined;
226+
/** Whether a retained tab shows this composer, apart from its read policy.
227+
* Showing a Solo chat again re-tags the agent last tagged there. */
228+
shown?: boolean | undefined;
226229
/** Sending needs at least one tagged recipient. */
227230
requireMention?: boolean | undefined;
228231
chatPresentation?: boolean | undefined;
@@ -371,6 +374,7 @@ function Composer({
371374
soloConversation,
372375
ownSoloConversation = false,
373376
defaultMention,
377+
shown = true,
374378
requireMention = false,
375379
chatPresentation = false,
376380
activityClickOpensPanel = false,
@@ -1262,14 +1266,54 @@ function Composer({
12621266
(!ownSoloConversation ||
12631267
session.channels.list().channels.find((item) => item.id === channelId)
12641268
?.channelType === "solo");
1265-
// Only a fresh chat is seeded with the default. Once it has content, respect
1266-
// edits and removals; accepted sends use the shared remembered-mention draft.
1269+
// A saved Solo chat is seeded with the agent the viewer last tagged there.
1270+
const restoresAgent = solo && !submission && !threadRootId && !defaultMention;
1271+
// It waits for the history and for complete agent evidence.
1272+
const subscribeRows = useCallback(
1273+
(listener: () => void) => {
1274+
if (!restoresAgent) return () => {};
1275+
const rows = session.channels.subscribeWindow?.(channelId, listener);
1276+
const choices = session.agentChoices.subscribe(listener);
1277+
return () => {
1278+
rows?.();
1279+
choices();
1280+
};
1281+
},
1282+
[restoresAgent, session.channels, session.agentChoices, channelId],
1283+
);
1284+
// Only an empty untagged draft looks, so typing doesn't rescan the history.
1285+
const presented = active && shown;
1286+
const seeking =
1287+
restoresAgent && presented && !value.recipients.length && !draft.trim();
1288+
const lastAgent = useSyncExternalStore(subscribeRows, () =>
1289+
seeking
1290+
? lastMentionedAgent(
1291+
session,
1292+
session.channels.window?.(channelId).rows ?? [],
1293+
mentionAdmission(session, channelId, agentChoices, mentionRoster),
1294+
)
1295+
: undefined,
1296+
);
1297+
const seedMention =
1298+
defaultMention ??
1299+
(lastAgent
1300+
? (pastedMentionRecipient(
1301+
session,
1302+
channelId,
1303+
lastAgent,
1304+
agentChoices,
1305+
mentionRoster,
1306+
) ?? undefined)
1307+
: undefined);
1308+
// Each time the composer is shown, an empty untagged draft is seeded with
1309+
// that agent. Once it has content, respect edits and removals until the
1310+
// composer is shown again; accepted sends use the remembered-mention draft.
12671311
const autoTagged = useRef<string | undefined>(undefined);
12681312
const automaticTag = useRef<string | undefined>(undefined);
12691313
useEffect(() => {
1314+
if (!presented) autoTagged.current = undefined;
12701315
if (
1271-
!defaultMention ||
1272-
!active ||
1316+
!presented ||
12731317
sendPending ||
12741318
completion.composing.current ||
12751319
editing.target ||
@@ -1288,17 +1332,18 @@ function Composer({
12881332
return;
12891333
}
12901334
if (
1335+
seedMention &&
12911336
mentionAdmission(
12921337
session,
12931338
channelId,
12941339
agentChoices,
12951340
mentionRoster,
1296-
)(defaultMention.pubkey)
1341+
)(seedMention.pubkey)
12971342
) {
12981343
if (
12991344
insert(
1300-
`@${defaultMention.name} `,
1301-
defaultMention,
1345+
`@${seedMention.name} `,
1346+
seedMention,
13021347
{ start: 0, end: 0, query: "" },
13031348
undefined,
13041349
false,

‎src/features/messages/remembered-mentions.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { knownAgentPubkeys } from "../agents/known";
2+
import type { ChannelMessage } from "../relay/contracts";
23
import type { ComposerSession } from "./composer-session";
34
import { archivedMention } from "./mention-admission";
45
import { followupDraft, type MentionDraft } from "./mention-draft";
@@ -25,3 +26,23 @@ export function rememberedMentions(
2526
: [],
2627
);
2728
}
29+
30+
/** The agent the viewer's latest top-level message in these rows tagged that
31+
* can still be tagged here. Off, like the follow-up draft, when agents aren't
32+
* remembered, and until agent choices are complete: a partial roster would
33+
* pick an older agent in place of the latest one. */
34+
export function lastMentionedAgent(
35+
session: ComposerSession,
36+
rows: readonly ChannelMessage[],
37+
admits: (pubkey: string) => boolean,
38+
): string | undefined {
39+
const choices = session.agentChoices.snapshot();
40+
if (!rememberAgentsPreference() || !choices.complete) return;
41+
const agents = knownAgentPubkeys(session.profiles.snapshot(), choices);
42+
for (let index = rows.length - 1; index >= 0; index--) {
43+
const row = rows[index];
44+
if (!row || row.authorId !== session.viewer || row.threadRootId) continue;
45+
const agent = row.mentions.find((key) => agents.has(key) && admits(key));
46+
if (agent) return agent;
47+
}
48+
}

0 commit comments

Comments
 (0)