Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getSharedChannelIds,
isAgentIdentityInManagedList,
relayAgentIsSharedWithUser,
shouldAdmitMentionCandidate,
shouldHideAgentFromMentions,
} from "./agentAutocompleteEligibility.ts";

Expand Down Expand Up @@ -136,7 +137,50 @@ test("getMentionableAgentPubkeys: keeps managed agents and shared relay agents",
assert.deepEqual(result, new Set([PUB_A, PUB_B, PUB_C]));
});

test("isAgentIdentityInManagedList: keeps people and only current managed agent identities", () => {
test("shouldAdmitMentionCandidate: admits a cross-owner channel bot with anyone access", () => {
const mentionableAgentPubkeys = getMentionableAgentPubkeys({
currentPubkey: CURRENT_PUBKEY,
managedAgentPubkeys: [],
relayAgents: [
{
pubkey: PUB_B,
ownerPubkey: OTHER_OWNER_PUBKEY,
respondTo: "anyone",
respondToAllowlist: [],
channelIds: ["general"],
},
],
sharedChannelIds: new Set(["general"]),
});

assert.equal(
shouldAdmitMentionCandidate({
isArchived: false,
isAgent: true,
isMember: true,
pubkey: PUB_B,
mentionableAgentPubkeys,
directoryAgentPubkeys: new Set([PUB_B]),
}),
true,
);
});

test("shouldAdmitMentionCandidate: rejects archived identities", () => {
assert.equal(
shouldAdmitMentionCandidate({
isArchived: true,
isAgent: false,
isMember: true,
pubkey: PUB_B,
mentionableAgentPubkeys: new Set(),
directoryAgentPubkeys: new Set(),
}),
false,
);
});

test("isAgentIdentityInManagedList: scopes add-member search to managed agent identities", () => {
const managedAgentPubkeys = new Set([PUB_A]);

assert.equal(
Expand Down
14 changes: 14 additions & 0 deletions desktop/src/features/agents/lib/agentAutocompleteEligibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export function isAgentIdentityInManagedList(
candidate: { isAgent?: boolean; pubkey: string },
managedAgentPubkeys: ReadonlySet<string>,
) {
// This is an ownership filter for add-member search. Mention autocomplete
// uses the invocability policy in shouldAdmitMentionCandidate instead.
return (
candidate.isAgent !== true ||
managedAgentPubkeys.has(normalizePubkey(candidate.pubkey))
Expand Down Expand Up @@ -97,6 +99,18 @@ export function shouldHideAgentFromMentions({
return directoryAgentPubkeys.has(normalized);
}

export function shouldAdmitMentionCandidate(args: {
isArchived: boolean;
isAgent: boolean;
isMember: boolean;
pubkey: string;
mentionableAgentPubkeys: ReadonlySet<string>;
directoryAgentPubkeys: ReadonlySet<string>;
}) {
if (args.isArchived) return false;
return !shouldHideAgentFromMentions(args);
}

type AgentAutocompleteCandidate = {
pubkey?: string;
displayName?: string | null;
Expand Down
13 changes: 3 additions & 10 deletions desktop/src/features/messages/lib/useMentions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {
coalesceAutocompleteCandidatesByKey,
getMentionableAgentPubkeys,
getSharedChannelIds,
isAgentIdentityInManagedList,
shouldHideAgentFromMentions,
shouldAdmitMentionCandidate,
} from "@/features/agents/lib/agentAutocompleteEligibility";
import {
useInfiniteUserSearchQuery,
Expand Down Expand Up @@ -243,14 +242,9 @@ export function useMentions(

const addCandidate = (candidate: MentionCandidate & { pubkey: string }) => {
const pubkey = normalizePubkey(candidate.pubkey);
if (isArchivedDiscovery(pubkey)) {
return;
}
if (!isAgentIdentityInManagedList(candidate, managedAgentPubkeys)) {
return;
}
if (
shouldHideAgentFromMentions({
!shouldAdmitMentionCandidate({
isArchived: isArchivedDiscovery(pubkey),
isAgent: candidate.isAgent === true,
isMember: candidate.isMember === true,
pubkey,
Expand Down Expand Up @@ -420,7 +414,6 @@ export function useMentions(
managedAgentNamesByPubkey,
managedAgentPersonaIds,
managedAgentPersonaIdsByPubkey,
managedAgentPubkeys,
managedAgentsQuery.data,
memberPubkeys,
members,
Expand Down
Loading