Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.5 KB
Raw Blame
import { useServerConfigStore } from '@/store/serverConfig';
import { serverConfigSelectors } from '@/store/serverConfig/selectors';
import { useUserStore } from '@/store/user';
import { labPreferSelectors } from '@/store/user/selectors';

import { useAgentAccounts } from './useAgentIdentity';

/**
 * Whether the mail page has anything to offer: the user opted into the Labs
 * experiment, and either this deployment can open an address (a provider is
 * configured) or the agent already owns one.
 *
 * Provider availability only decides whether new addresses can be opened. An
 * address that already exists must stay reachable after its provider's
 * credentials are removed, so it can still be read, copied and released.
 */
export const useShowAgentIdentity = (agentId: string): boolean => {
  const enabled = useUserStore(labPreferSelectors.enableAgentIdentity);
  const providers = useServerConfigStore(serverConfigSelectors.agentIdentityProviders);

  // Only ask for the agent's accounts when they are what decides it.
  const needsAccounts = enabled && providers.length === 0 && !!agentId;
  const { data: accounts, error } = useAgentAccounts(needsAccounts ? agentId : '');

  if (!enabled) return false;
  if (providers.length > 0) return true;
  // The read that decides it failed: the agent may own an address, so keep the
  // entry — the page shows the failure and a retry — rather than hide it on a guess.
  if (error && !accounts) return true;

  return (accounts ?? []).some((account) => account.status !== 'revoked');
};