Skip to content

The agent that grows with you

fix: the connectors card paints its rows at once and keeps them across remounts

alt-glitchd06ada6b0ba8
diff --git a/apps/desktop/src/app/contrib/onboarding-kickoff.ts b/apps/desktop/src/app/contrib/onboarding-kickoff.ts
index 249acb7e912..10076778ba0 100644
--- a/apps/desktop/src/app/contrib/onboarding-kickoff.ts
+++ b/apps/desktop/src/app/contrib/onboarding-kickoff.ts
@@ -11,11 +11,13 @@ import {
} from '@/components/onboarding-chat/assembly'
import { $setupSession, guideSourceConnectionId, SETUP_CHAT_TITLE } from '@/components/onboarding-chat/setup-profile'
import { isOnboardingEnabled } from '@/lib/onboarding-enabled'
+import { prefetchConnectorCatalog } from '@/store/connector-catalog'
import { activeGatewayConnectionId, requestGatewayForProfile } from '@/store/gateway'
import { loadMachineProfile } from '@/store/machine'
import { notify } from '@/store/notifications'
import { readOnboardingCapabilities } from '@/store/onboarding-capabilities'
import { skipGuide } from '@/store/onboarding-gate'
+import { prefetchOnboardingPlugins } from '@/store/onboarding-plugins'
import { buildChatOnboardingSeedMessages } from '@/store/onboarding-script'
import {
$activeGatewayProfile,
@@ -28,6 +30,14 @@ import { $activeSessionId, $selectedStoredSessionId } from '@/store/session'
import type { AmbientGatewayRequest } from './session-rpc-dispatcher'
+/** The connectors card is two turns after the guide opens; its two reads are slow cold, so they start now. */
+function prefetchGuideCatalogs(storedId: null | string, runtimeId: string): void {
+ if (storedId) {
+ prefetchConnectorCatalog(storedId, runtimeId)
+ prefetchOnboardingPlugins(storedId)
+ }
+}
+
export interface OnboardingKickoffOptions extends Pick<
ReturnType<typeof useSessionActions>,
'createBackendSessionForSend' | 'resumeSession'
@@ -64,6 +74,7 @@ async function adoptGuideSession(
runtimeId: adoptedRuntimeId ?? canonical.id,
storedId: canonical.id
})
+ prefetchGuideCatalogs(canonical.id, adoptedRuntimeId ?? canonical.id)
if (freeTier) {
await guideRequest('config.set', {
@@ -165,6 +176,7 @@ export function useOnboardingKickoff({
const storedId = $selectedStoredSessionId.get()
$chatOnboardingThreadIds.set(storedId ? [storedId, runtimeId] : [runtimeId])
+ prefetchGuideCatalogs(storedId, runtimeId)
$setupSession.set({
connectionId: guideSourceConnectionId(storedId),
profile: setupProfile,
diff --git a/apps/desktop/src/store/connector-catalog.ts b/apps/desktop/src/store/connector-catalog.ts
index 00d69385688..ab992919c80 100644
--- a/apps/desktop/src/store/connector-catalog.ts
+++ b/apps/desktop/src/store/connector-catalog.ts
@@ -1,5 +1,5 @@
/**
- * The live connector catalog for a chat session, read once per mount.
+ * The live connector catalog for a chat session, read once per session and cached.
*
* The onboarding picker used to be a hardcoded list, and it drifted from the
* deployed catalog: it offered apps the gateway does not carry and spelled
@@ -15,12 +15,13 @@
* and the probe runs once they arrive, so the card never waits on a request
* that was never sent.
*/
-import { useEffect, useState } from 'react'
+import { useQuery } from '@tanstack/react-query'
import { resolveSessionOwner } from '@/app/session/hooks/use-session-actions/utils'
import { translateNow } from '@/i18n'
import type { ConnectorRow } from '@/lib/connector-tools'
import { isMissingRpcMethod, isOutOfSyncRpcParams } from '@/lib/gateway-rpc'
+import { queryClient } from '@/lib/query-client'
import { requestGatewayForAgent } from '@/store/gateway'
import { notifyError } from '@/store/notifications'
import { $activeGatewayProfile } from '@/store/profile'
@@ -30,56 +31,58 @@ import { isSessionOwnerRoute } from '@/store/session-request-router'
export type ConnectorCatalog =
{ status: 'loading' } | { status: 'ready'; rows: ConnectorRow[] } | { status: 'unavailable' }
-export function useConnectorCatalog(storedId: null | string, runtimeId: null | string): ConnectorCatalog {
- const [catalog, setCatalog] = useState<ConnectorCatalog>(() =>
- storedId && runtimeId ? { status: 'loading' } : { status: 'unavailable' }
- )
+/** The picks card remounts on every transcript rebuild (each hidden submit, each turn end), so the read is
+ * held in the query cache per session: a remount paints the rows it already has instead of flashing back to
+ * the skeleton for another round trip. */
+async function readConnectorCatalog(storedId: string, runtimeId: string): Promise<ConnectorCatalog> {
+ try {
+ const scope = await resolveSessionOwner(storedId)
+ assertSessionOwnerResolved(scope, { method: 'connectors.list', sessionId: storedId })
+ const connectionId = isSessionOwnerRoute(scope) ? scope.connectionId : null
+ const profile = isSessionOwnerRoute(scope) ? scope.profile : scope || $activeGatewayProfile.get()
- useEffect(() => {
- if (!storedId || !runtimeId) {
- setCatalog({ status: 'unavailable' })
+ const response = await requestGatewayForAgent<{ available: boolean; connectors: ConnectorRow[] }>(
+ connectionId,
+ profile,
+ 'connectors.list',
+ { owner: { session_id: runtimeId, type: 'session' } },
+ 15000
+ )
- return
+ return response.available ? { rows: response.connectors, status: 'ready' } : { status: 'unavailable' }
+ } catch (error) {
+ if (isMissingRpcMethod(error) || isOutOfSyncRpcParams(error instanceof Error ? error : String(error))) {
+ notifyError(error, translateNow('connectors.unavailable'), { id: 'connectors-rpc-out-of-sync' })
}
- setCatalog({ status: 'loading' })
- let cancelled = false
- const ambientProfile = $activeGatewayProfile.get()
-
- void resolveSessionOwner(storedId)
- .then(scope => {
- assertSessionOwnerResolved(scope, { method: 'connectors.list', sessionId: storedId })
+ return { status: 'unavailable' }
+ }
+}
- const connectionId = isSessionOwnerRoute(scope) ? scope.connectionId : null
- const profile = isSessionOwnerRoute(scope) ? scope.profile : scope || ambientProfile
+const catalogKey = (storedId: null | string, runtimeId: null | string) =>
+ ['onboarding', 'connectors.list', storedId, runtimeId] as const
- return requestGatewayForAgent<{ available: boolean; connectors: ConnectorRow[] }>(
- connectionId,
- profile,
- 'connectors.list',
- { owner: { session_id: runtimeId, type: 'session' } },
- 15000
- )
- })
- .then(response => {
- if (!cancelled) {
- setCatalog(response.available ? { rows: response.connectors, status: 'ready' } : { status: 'unavailable' })
- }
- })
- .catch(error => {
- if (!cancelled) {
- setCatalog({ status: 'unavailable' })
+/** Start the read when the guide session opens: cold, it takes several seconds, and the card that needs it is
+ * two turns away. */
+export function prefetchConnectorCatalog(storedId: string, runtimeId: string): void {
+ void queryClient.prefetchQuery({
+ queryFn: () => readConnectorCatalog(storedId, runtimeId),
+ queryKey: catalogKey(storedId, runtimeId),
+ staleTime: Infinity
+ })
+}
- if (isMissingRpcMethod(error) || isOutOfSyncRpcParams(error)) {
- notifyError(error, translateNow('connectors.unavailable'), { id: 'connectors-rpc-out-of-sync' })
- }
- }
- })
+export function useConnectorCatalog(storedId: null | string, runtimeId: null | string): ConnectorCatalog {
+ const query = useQuery({
+ enabled: Boolean(storedId && runtimeId),
+ queryFn: () => readConnectorCatalog(storedId!, runtimeId!),
+ queryKey: catalogKey(storedId, runtimeId),
+ staleTime: Infinity
+ })
- return () => {
- cancelled = true
- }
- }, [storedId, runtimeId])
+ if (!storedId || !runtimeId) {
+ return { status: 'unavailable' }
+ }
- return catalog
+ return query.data ?? { status: 'loading' }
}
diff --git a/apps/desktop/src/store/onboarding-plugins.ts b/apps/desktop/src/store/onboarding-plugins.ts
index 270bcc55e2d..c09ad73922c 100644
--- a/apps/desktop/src/store/onboarding-plugins.ts
+++ b/apps/desktop/src/store/onboarding-plugins.ts
@@ -6,9 +6,10 @@
* orders and draws them. A failed or missing RPC is an empty list: the connectors half still works.
*/
import type { OnboardingCatalogPlugin } from '@hermes/shared'
-import { useEffect, useState } from 'react'
+import { useQuery } from '@tanstack/react-query'
import { resolveSessionOwner } from '@/app/session/hooks/use-session-actions/utils'
+import { queryClient } from '@/lib/query-client'
import { requestGatewayForAgent } from '@/store/gateway'
import { $activeGatewayProfile } from '@/store/profile'
import { isSessionOwnerRoute } from '@/store/session-request-router'
@@ -18,45 +19,45 @@ export type OnboardingPlugin = OnboardingCatalogPlugin
/** A plugin whose app is not on this machine stays pickable; the row says what is missing (D5). */
export const pluginNeedsApp = (plugin: OnboardingPlugin): boolean => plugin.app_state === 'missing_app'
+async function readOnboardingPlugins(storedId: string): Promise<OnboardingPlugin[]> {
+ try {
+ const scope = await resolveSessionOwner(storedId)
+ const connectionId = isSessionOwnerRoute(scope) ? scope.connectionId : null
+ const profile = isSessionOwnerRoute(scope) ? scope.profile : scope || $activeGatewayProfile.get()
+
+ const response = await requestGatewayForAgent<{ onboarding?: OnboardingPlugin[] | null }>(
+ connectionId,
+ profile,
+ 'plugins.manage',
+ { action: 'onboarding' },
+ 20000
+ )
+
+ return response.onboarding ?? []
+ } catch {
+ return []
+ }
+}
+
+const pluginsKey = (storedId: null | string) => ['onboarding', 'plugins.manage:onboarding', storedId] as const
+
+/** Started with the guide session, like the connector read. */
+export function prefetchOnboardingPlugins(storedId: string): void {
+ void queryClient.prefetchQuery({
+ queryFn: () => readOnboardingPlugins(storedId),
+ queryKey: pluginsKey(storedId),
+ staleTime: Infinity
+ })
+}
+
+/** Cached per session like the connector read, so the card's remounts keep the rows. */
export function useOnboardingPlugins(storedId: null | string): OnboardingPlugin[] {
- const [plugins, setPlugins] = useState<OnboardingPlugin[]>([])
-
- useEffect(() => {
- if (!storedId) {
- return
- }
-
- let cancelled = false
- const ambientProfile = $activeGatewayProfile.get()
-
- void resolveSessionOwner(storedId)
- .then(scope => {
- const connectionId = isSessionOwnerRoute(scope) ? scope.connectionId : null
- const profile = isSessionOwnerRoute(scope) ? scope.profile : scope || ambientProfile
-
- return requestGatewayForAgent<{ onboarding?: OnboardingPlugin[] | null }>(
- connectionId,
- profile,
- 'plugins.manage',
- { action: 'onboarding' },
- 20000
- )
- })
- .then(response => {
- if (!cancelled) {
- setPlugins(response.onboarding ?? [])
- }
- })
- .catch(() => {
- if (!cancelled) {
- setPlugins([])
- }
- })
-
- return () => {
- cancelled = true
- }
- }, [storedId])
-
- return plugins
+ const query = useQuery({
+ enabled: Boolean(storedId),
+ queryFn: () => readOnboardingPlugins(storedId!),
+ queryKey: pluginsKey(storedId),
+ staleTime: Infinity
+ })
+
+ return query.data ?? []
}