Skip to content

The agent that grows with you

fix(desktop): prefetch a connector's tool list only when it is connected

Siddharth Balyanf1e3f1f2ad47
diff --git a/apps/desktop/src/app/capabilities/connectors/connectors-tab.tsx b/apps/desktop/src/app/capabilities/connectors/connectors-tab.tsx
index 3e940b7053e..96907a4a177 100644
--- a/apps/desktop/src/app/capabilities/connectors/connectors-tab.tsx
+++ b/apps/desktop/src/app/capabilities/connectors/connectors-tab.tsx
@@ -260,11 +260,7 @@ export function ConnectorsTab({ gateway, profile }: ConnectorsTabProps) {
notices={<HostedNotice hasGuest={freeTier?.has_guest === true} phase={hosted.phase} />}
onFilterChange={setFilter}
onOpen={card => setOpenKey(cardKey(card))}
- onPrefetch={card => {
- if (card.ways.hosted) {
- prefetchConnectorTools(profile, card.slug)
- }
- }}
+ onPrefetch={card => prefetchConnectorTools(profile, card)}
onRetryHosted={hosted.refetch}
onServerToggle={(card, next) => {
if (card.plugin === undefined) {
diff --git a/apps/desktop/src/app/capabilities/connectors/data/prefetch.test.ts b/apps/desktop/src/app/capabilities/connectors/data/prefetch.test.ts
new file mode 100644
index 00000000000..073201e308b
--- /dev/null
+++ b/apps/desktop/src/app/capabilities/connectors/data/prefetch.test.ts
@@ -0,0 +1,49 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { queryClient } from '@/lib/query-client'
+
+import type { ConnectorCardModel, ConnectorState } from '../types'
+
+import { prefetchConnectorTools } from './prefetch'
+
+function hostedCard(slug: string, state: ConnectorState): ConnectorCardModel {
+ return {
+ inCatalog: true,
+ name: slug,
+ residency: 'hosted',
+ slug,
+ state,
+ stateWord: 'available',
+ ways: { hosted: { accounts: [], connected: state === 'connected', retiredAccounts: [], state }, local: null }
+ }
+}
+
+function localCard(slug: string): ConnectorCardModel {
+ return {
+ inCatalog: false,
+ name: slug,
+ residency: 'local',
+ slug,
+ state: 'connected',
+ stateWord: 'serverOn',
+ ways: { hosted: null, local: { state: 'connected' } }
+ }
+}
+
+describe('prefetchConnectorTools', () => {
+ afterEach(() => vi.restoreAllMocks())
+
+ it('reads the tool list only for a card whose hosted account is connected', () => {
+ const prefetch = vi.spyOn(queryClient, 'prefetchQuery').mockResolvedValue(undefined)
+ const states: ConnectorState[] = ['available', 'broken', 'connected', 'connecting', 'expired', 'off', 'unknown']
+
+ for (const state of states) {
+ prefetchConnectorTools('default', hostedCard(`app-${state}`, state))
+ }
+
+ prefetchConnectorTools('default', localCard('local-server'))
+
+ expect(prefetch).toHaveBeenCalledTimes(1)
+ expect(JSON.stringify(prefetch.mock.calls[0]?.[0].queryKey)).toContain('app-connected')
+ })
+})
diff --git a/apps/desktop/src/app/capabilities/connectors/data/prefetch.ts b/apps/desktop/src/app/capabilities/connectors/data/prefetch.ts
index edb69ccdb84..1df43a97882 100644
--- a/apps/desktop/src/app/capabilities/connectors/data/prefetch.ts
+++ b/apps/desktop/src/app/capabilities/connectors/data/prefetch.ts
@@ -9,15 +9,19 @@ import { connectorToolsQueryOptions } from './queries'
const PREFETCH_LIMIT = 3
-export function prefetchConnectorTools(scope: ProfileScope, slug: string): void {
- void queryClient.prefetchQuery(connectorToolsQueryOptions(scope, slug))
+// Only a connected hosted account has tools to list; an unconnected card's read is a wasted gateway call.
+export function hasConnectedTools(card: ConnectorCardModel): boolean {
+ return card.ways.hosted?.state === 'connected'
+}
+
+export function prefetchConnectorTools(scope: ProfileScope, card: ConnectorCardModel): void {
+ if (hasConnectedTools(card)) {
+ void queryClient.prefetchQuery(connectorToolsQueryOptions(scope, card.slug))
+ }
}
export function usePrefetchConnectedTools(scope: ProfileScope, cards: readonly ConnectorCardModel[]): void {
- const slugs = useMemo(
- () => cards.filter(card => card.ways.hosted?.state === 'connected').map(card => card.slug),
- [cards]
- )
+ const slugs = useMemo(() => cards.filter(hasConnectedTools).map(card => card.slug), [cards])
useEffect(() => {
let stopped = false