From bf34c4eeebda3725078a6d1dd928bdbeaf320893 Mon Sep 17 00:00:00 2001 From: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Date: Fri, 17 Jul 2026 21:59:27 -0400 Subject: [PATCH] refactor(desktop): extract agent query invalidation Move the cohesive background query-invalidation helpers out of the oversized agents hooks module. Preserve the existing hooks exports and behavior while restoring meaningful headroom under the desktop file-size limit. Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> --- .../features/agents/agentQueryInvalidation.ts | 77 +++++++++++++++++ desktop/src/features/agents/hooks.ts | 83 +++---------------- 2 files changed, 88 insertions(+), 72 deletions(-) create mode 100644 desktop/src/features/agents/agentQueryInvalidation.ts diff --git a/desktop/src/features/agents/agentQueryInvalidation.ts b/desktop/src/features/agents/agentQueryInvalidation.ts new file mode 100644 index 000000000..2fc4752c8 --- /dev/null +++ b/desktop/src/features/agents/agentQueryInvalidation.ts @@ -0,0 +1,77 @@ +import type { QueryClient } from "@tanstack/react-query"; + +import { channelsQueryKey } from "@/features/channels/hooks"; +import type { Channel } from "@/shared/api/types"; + +export const relayAgentsQueryKey = ["relay-agents"] as const; +export const managedAgentsQueryKey = ["managed-agents"] as const; + +type InvalidateAgentQueriesOptions = { + refetchChannels?: boolean; +}; + +async function invalidateAgentQueries( + queryClient: QueryClient, + channelId: string | null, + options: InvalidateAgentQueriesOptions = {}, +) { + await Promise.all([ + queryClient.invalidateQueries({ queryKey: managedAgentsQueryKey }), + queryClient.invalidateQueries({ queryKey: relayAgentsQueryKey }), + queryClient.invalidateQueries({ + queryKey: channelsQueryKey, + refetchType: options.refetchChannels === false ? "none" : "active", + }), + ...(channelId + ? [ + queryClient.invalidateQueries({ + queryKey: ["channels", channelId, "members"], + }), + ] + : []), + ]); +} + +function refreshAgentQueriesInBackground(task: () => Promise) { + void task().catch((error) => { + console.error("Failed to refresh agent queries", error); + }); +} + +export function invalidateAgentQueriesInBackground( + queryClient: QueryClient, + channelId: string | null, + options?: InvalidateAgentQueriesOptions, +) { + refreshAgentQueriesInBackground(() => + invalidateAgentQueries(queryClient, channelId, options), + ); +} + +export function isCachedDmChannel( + queryClient: QueryClient, + channelId: string | null, +) { + if (!channelId) { + return false; + } + + return Boolean( + queryClient + .getQueryData(channelsQueryKey) + ?.some( + (channel) => channel.id === channelId && channel.channelType === "dm", + ), + ); +} + +export function invalidateManagedAgentQueriesInBackground( + queryClient: QueryClient, +) { + refreshAgentQueriesInBackground(() => + Promise.all([ + queryClient.invalidateQueries({ queryKey: managedAgentsQueryKey }), + queryClient.invalidateQueries({ queryKey: relayAgentsQueryKey }), + ]), + ); +} diff --git a/desktop/src/features/agents/hooks.ts b/desktop/src/features/agents/hooks.ts index 852172f9e..9b9005fe5 100644 --- a/desktop/src/features/agents/hooks.ts +++ b/desktop/src/features/agents/hooks.ts @@ -2,6 +2,13 @@ import * as React from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { hasRunningAgentInCommunity } from "@/features/agents/agentRelayScope"; +import { + invalidateAgentQueriesInBackground, + invalidateManagedAgentQueriesInBackground, + isCachedDmChannel, + managedAgentsQueryKey, + relayAgentsQueryKey, +} from "@/features/agents/agentQueryInvalidation"; import { connectAcpRuntime, discoverAcpAuthMethods, @@ -102,8 +109,10 @@ export type { ProvisionChannelManagedAgentResult, } from "@/features/agents/channelAgents"; -export const relayAgentsQueryKey = ["relay-agents"] as const; -export const managedAgentsQueryKey = ["managed-agents"] as const; +export { + managedAgentsQueryKey, + relayAgentsQueryKey, +} from "@/features/agents/agentQueryInvalidation"; export const personasQueryKey = ["personas"] as const; export const teamsQueryKey = ["teams"] as const; export const acpRuntimesQueryKey = ["acp-runtimes"] as const; @@ -112,76 +121,6 @@ export const managedAgentPrereqsQueryKey = ["managed-agent-prereqs"] as const; export const backendProvidersQueryKey = ["backend-providers"] as const; export const gitBashPrerequisiteQueryKey = ["git-bash-prerequisite"] as const; -type InvalidateAgentQueriesOptions = { - refetchChannels?: boolean; -}; - -async function invalidateAgentQueries( - queryClient: ReturnType, - channelId: string | null, - options: InvalidateAgentQueriesOptions = {}, -) { - await Promise.all([ - queryClient.invalidateQueries({ queryKey: managedAgentsQueryKey }), - queryClient.invalidateQueries({ queryKey: relayAgentsQueryKey }), - queryClient.invalidateQueries({ - queryKey: channelsQueryKey, - refetchType: options.refetchChannels === false ? "none" : "active", - }), - ...(channelId - ? [ - queryClient.invalidateQueries({ - queryKey: ["channels", channelId, "members"], - }), - ] - : []), - ]); -} - -function refreshAgentQueriesInBackground(task: () => Promise) { - void task().catch((error) => { - console.error("Failed to refresh agent queries", error); - }); -} - -function invalidateAgentQueriesInBackground( - queryClient: ReturnType, - channelId: string | null, - options?: InvalidateAgentQueriesOptions, -) { - refreshAgentQueriesInBackground(() => - invalidateAgentQueries(queryClient, channelId, options), - ); -} - -function isCachedDmChannel( - queryClient: ReturnType, - channelId: string | null, -) { - if (!channelId) { - return false; - } - - return Boolean( - queryClient - .getQueryData(channelsQueryKey) - ?.some( - (channel) => channel.id === channelId && channel.channelType === "dm", - ), - ); -} - -function invalidateManagedAgentQueriesInBackground( - queryClient: ReturnType, -) { - refreshAgentQueriesInBackground(() => - Promise.all([ - queryClient.invalidateQueries({ queryKey: managedAgentsQueryKey }), - queryClient.invalidateQueries({ queryKey: relayAgentsQueryKey }), - ]), - ); -} - export function useAcpRuntimesQuery(options?: { enabled?: boolean }) { return useQuery({ enabled: options?.enabled ?? true,