perf(site/src/pages/AgentsPage): combine array iterations (#25614)

This commit is contained in:
Danielle Maywood
2026-05-22 14:23:22 +01:00
committed by GitHub
parent e71710df37
commit 15e63dec6f
4 changed files with 51 additions and 42 deletions
@@ -32,13 +32,13 @@ const buildOrderedMessageIDs = (
// still exists in a later page). The Map-based messagesByID // still exists in a later page). The Map-based messagesByID
// already deduplicates, but orderedMessageIDs must match. // already deduplicates, but orderedMessageIDs must match.
const seen = new Set<number>(); const seen = new Set<number>();
return sorted const orderedMessageIDs: number[] = [];
.map((message) => message.id) for (const message of sorted) {
.filter((id) => { if (seen.has(message.id)) continue;
if (seen.has(id)) return false; seen.add(message.id);
seen.add(id); orderedMessageIDs.push(message.id);
return true; }
}); return orderedMessageIDs;
}; };
const mapsEqualByRef = <K, V>(left: Map<K, V>, right: Map<K, V>): boolean => { const mapsEqualByRef = <K, V>(left: Map<K, V>, right: Map<K, V>): boolean => {
@@ -78,13 +78,17 @@ const MCPIcon: FC<{ iconUrl: string; name: string; className?: string }> = ({
export const getDefaultMCPSelection = ( export const getDefaultMCPSelection = (
servers: readonly TypesGen.MCPServerConfig[], servers: readonly TypesGen.MCPServerConfig[],
): string[] => { ): string[] => {
return servers const ids: string[] = [];
.filter( for (const server of servers) {
(s) => if (
s.enabled && server.enabled &&
(s.availability === "force_on" || s.availability === "default_on"), (server.availability === "force_on" ||
) server.availability === "default_on")
.map((s) => s.id); ) {
ids.push(server.id);
}
}
return ids;
}; };
/** localStorage key for persisting the user's MCP server selection. */ /** localStorage key for persisting the user's MCP server selection. */
@@ -113,13 +117,15 @@ export const mcpSelectionStorageKey = "agents.selected-mcp-server-ids";
if (!Array.isArray(parsed)) { if (!Array.isArray(parsed)) {
return null; return null;
} }
const enabledIds = new Set( const enabledIds = new Set<string>();
servers.filter((s) => s.enabled).map((s) => s.id), const forceOnIds: string[] = [];
); for (const server of servers) {
// Always include force_on servers even if the user didn't save them. if (!server.enabled) continue;
const forceOnIds = servers enabledIds.add(server.id);
.filter((s) => s.enabled && s.availability === "force_on") if (server.availability === "force_on") {
.map((s) => s.id); forceOnIds.push(server.id);
}
}
const restored = parsed.filter( const restored = parsed.filter(
(id): id is string => typeof id === "string" && enabledIds.has(id), (id): id is string => typeof id === "string" && enabledIds.has(id),
); );
@@ -41,12 +41,13 @@ export const getQueuedMessageInfo = (
): QueuedMessageInfo => { ): QueuedMessageInfo => {
const { content } = message; const { content } = message;
const fileBlocks = content.filter((p) => p.type === "file"); const fileBlocks = content.filter((p) => p.type === "file");
const rawText = content const textParts: string[] = [];
.filter((p) => p.type === "text") for (const part of content) {
.map((p) => p.text) if (part.type === "text" && part.text?.trim()) {
.filter((t): t is string => Boolean(t?.trim())) textParts.push(part.text);
.join(" ") }
.trim(); }
const rawText = textParts.join(" ").trim();
if (rawText) { if (rawText) {
return { return {
@@ -60,22 +60,24 @@ export const filterPersonalSkills = (
return skills.toSorted((a, b) => a.name.localeCompare(b.name, "en-US")); return skills.toSorted((a, b) => a.name.localeCompare(b.name, "en-US"));
} }
return skills const rankedSkills: RankedPersonalSkill[] = [];
.map((skill, index): RankedPersonalSkill => { for (const [index, skill] of skills.entries()) {
const name = skill.name.toLocaleLowerCase("en-US"); const name = skill.name.toLocaleLowerCase("en-US");
const description = skill.description.toLocaleLowerCase("en-US"); const description = skill.description.toLocaleLowerCase("en-US");
let rank = Number.POSITIVE_INFINITY; let rank: number | undefined;
if (name.startsWith(normalizedQuery)) { if (name.startsWith(normalizedQuery)) {
rank = 0; rank = 0;
} else if (name.includes(normalizedQuery)) { } else if (name.includes(normalizedQuery)) {
rank = 1; rank = 1;
} else if (description.includes(normalizedQuery)) { } else if (description.includes(normalizedQuery)) {
rank = 2; rank = 2;
} }
if (rank !== undefined) {
rankedSkills.push({ skill, rank, index });
}
}
return { skill, rank, index }; return rankedSkills
})
.filter(({ rank }) => Number.isFinite(rank))
.toSorted((a, b) => { .toSorted((a, b) => {
if (a.rank !== b.rank) { if (a.rank !== b.rank) {
return a.rank - b.rank; return a.rank - b.rank;