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>
This commit is contained in:
npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757
2026-07-17 21:59:27 -04:00
parent a332b5e534
commit bf34c4eeeb
2 changed files with 88 additions and 72 deletions
@@ -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<unknown>) {
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<Channel[]>(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 }),
]),
);
}
+11 -72
View File
@@ -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<typeof useQueryClient>,
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<unknown>) {
void task().catch((error) => {
console.error("Failed to refresh agent queries", error);
});
}
function invalidateAgentQueriesInBackground(
queryClient: ReturnType<typeof useQueryClient>,
channelId: string | null,
options?: InvalidateAgentQueriesOptions,
) {
refreshAgentQueriesInBackground(() =>
invalidateAgentQueries(queryClient, channelId, options),
);
}
function isCachedDmChannel(
queryClient: ReturnType<typeof useQueryClient>,
channelId: string | null,
) {
if (!channelId) {
return false;
}
return Boolean(
queryClient
.getQueryData<Channel[]>(channelsQueryKey)
?.some(
(channel) => channel.id === channelId && channel.channelType === "dm",
),
);
}
function invalidateManagedAgentQueriesInBackground(
queryClient: ReturnType<typeof useQueryClient>,
) {
refreshAgentQueriesInBackground(() =>
Promise.all([
queryClient.invalidateQueries({ queryKey: managedAgentsQueryKey }),
queryClient.invalidateQueries({ queryKey: relayAgentsQueryKey }),
]),
);
}
export function useAcpRuntimesQuery(options?: { enabled?: boolean }) {
return useQuery({
enabled: options?.enabled ?? true,