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.

14.9 KB
Raw Blame
import { DEFAULT_PROVIDER } from '@lobechat/business-const';
import {
  DEFAULT_AGENT_CONFIG,
  DEFAULT_BACKGROUND_COLOR,
  DEFAULT_INBOX_AVATAR,
  DEFAULT_INBOX_TITLE,
  DEFAULT_MODEL,
  DEFAUTT_AGENT_TTS_CONFIG,
  isDesktop,
} from '@lobechat/const';
import {
  agentDisplayName,
  type AgentMode,
  type AgentProfile,
  getActivePluginIds,
  getWorkingDirEffectivePath,
  type KnowledgeItem,
  KnowledgeType,
  type LobeAgentConfig,
  type LobeAgentTTSConfig,
  type MetaData,
  type RuntimeEnvConfig,
} from '@lobechat/types';

import { DEFAULT_OPENING_QUESTIONS } from '@/features/AgentSetting/store/selectors';
import { resolveTargetDeviceId } from '@/helpers/agentWorkingDirectory';
import { globalAgentContextManager } from '@/helpers/GlobalAgentContextManager';
import { filterToolIds } from '@/helpers/toolFilters';

import { type AgentStoreState } from '../initialState';
import { builtinAgentSelectors } from './builtinAgentSelectors';

// ==========   Meta   ============== //

const currentAgentData = (s: AgentStoreState) =>
  s.activeAgentId ? s.agentMap[s.activeAgentId] : undefined;

const currentAgentTitle = (s: AgentStoreState) => currentAgentData(s)?.title;

/**
 * The label to show for the active agent: personal name first, role as the
 * fallback. Use this for rendering; `currentAgentTitle` stays the raw role for
 * anything that edits or reasons about it.
 */
const currentAgentDisplayName = (s: AgentStoreState) => agentDisplayName(currentAgentData(s));

const getDefaultAvatarByAgentId = (s: AgentStoreState, agentId?: string) => {
  const inboxAgentId = builtinAgentSelectors.inboxAgentId(s);

  return agentId && inboxAgentId === agentId ? DEFAULT_INBOX_AVATAR : undefined;
};

const currentAgentAvatar = (s: AgentStoreState) =>
  currentAgentData(s)?.avatar || getDefaultAvatarByAgentId(s, s.activeAgentId);

const currentAgentDescription = (s: AgentStoreState) => currentAgentData(s)?.description;

const currentAgentBackgroundColor = (s: AgentStoreState) =>
  currentAgentData(s)?.backgroundColor || 'transparent';

const currentAgentTags = (s: AgentStoreState) => currentAgentData(s)?.tags || [];

const currentAgentAuthorId = (s: AgentStoreState) => currentAgentData(s)?.userId;

const currentAgentCreatedAt = (s: AgentStoreState) => currentAgentData(s)?.createdAt;

const currentAgentVisibility = (s: AgentStoreState) => currentAgentData(s)?.visibility;

/** An agent's visibility by id — for a conversation whose agent is not the active one. */
const getAgentVisibilityById = (agentId: string) => (s: AgentStoreState) =>
  s.agentMap[agentId]?.visibility;

/**
 * Get complete meta data for the current agent
 * Used to replace sessionMetaSelectors.currentAgentMeta
 */
const currentAgentMeta = (s: AgentStoreState): MetaData => {
  const data = currentAgentData(s);
  return {
    avatar: data?.avatar || getDefaultAvatarByAgentId(s, s.activeAgentId),
    backgroundColor: data?.backgroundColor || DEFAULT_BACKGROUND_COLOR,
    description: data?.description || undefined,
    marketIdentifier: data?.marketIdentifier || undefined,
    name: data?.name || undefined,
    tags: data?.tags,
    title: data?.title || undefined,
  };
};

/**
 * Get agent meta by agent ID (for group chat)
 * Used to replace sessionMetaSelectors.getAgentMetaByAgentId
 */
const getAgentMetaById =
  (agentId: string) =>
  (s: AgentStoreState): MetaData => {
    const data = s.agentMap[agentId];
    if (!data) return {};

    return {
      avatar: data.avatar || getDefaultAvatarByAgentId(s, agentId),
      backgroundColor: data.backgroundColor || DEFAULT_BACKGROUND_COLOR,
      description: data.description || undefined,
      marketIdentifier: data.marketIdentifier || undefined,
      name: data.name || undefined,
      tags: data.tags,
      title: data.title || undefined,
    };
  };

/**
 * The inbox agent's label: the name the user gave it, else its title, else `DEFAULT_INBOX_TITLE`.
 * Use it wherever the inbox is named, rather than the brand constant.
 */
const inboxAgentDisplayName = (s: AgentStoreState) =>
  agentDisplayName(
    getAgentMetaById(builtinAgentSelectors.inboxAgentId(s) ?? '')(s),
    DEFAULT_INBOX_TITLE,
  );

/** The inbox agent's avatar, else `DEFAULT_INBOX_AVATAR`. */
const inboxAgentAvatar = (s: AgentStoreState) =>
  getAgentMetaById(builtinAgentSelectors.inboxAgentId(s) ?? '')(s).avatar || DEFAULT_INBOX_AVATAR;

/**
 * Full-body artwork of the agent's character, or `undefined` when it has none.
 * Kept out of {@link getAgentMetaById} because `MetaData` is shared with
 * sessions and groups, which have no character sheet.
 */
/**
 * The avatar actually stored on the agent — unlike {@link getAgentMetaById},
 * which substitutes a default so every surface has something to render. Use
 * this to decide whether there is anything to remove.
 */
const getAgentStoredAvatarById =
  (agentId: string) =>
  (s: AgentStoreState): string | undefined =>
    s.agentMap[agentId]?.avatar || undefined;

const getAgentProfileById =
  (agentId: string) =>
  (s: AgentStoreState): AgentProfile | undefined =>
    s.agentMap[agentId]?.profile ?? undefined;

const getAgentFullBodyArtworkById =
  (agentId: string) =>
  (s: AgentStoreState): string | undefined =>
    s.agentMap[agentId]?.profile?.fullBodyArtwork || undefined;

// ==========   Config   ============== //

const inboxAgentConfig = (s: AgentStoreState) => {
  const id = builtinAgentSelectors.inboxAgentId(s);
  // Server returns inbox config already merged with DEFAULT_AGENT_CONFIG and serverDefaultAgentConfig,
  // so we can directly use it. Fallback to DEFAULT_AGENT_CONFIG if not initialized yet.
  return id ? (s.agentMap[id] as LobeAgentConfig) : DEFAULT_AGENT_CONFIG;
};
const inboxAgentModel = (s: AgentStoreState) => inboxAgentConfig(s).model;

const getAgentConfigById =
  (agentId: string) =>
  (s: AgentStoreState): LobeAgentConfig =>
    s.agentMap[agentId] as LobeAgentConfig;

export const currentAgentConfig = (s: AgentStoreState): LobeAgentConfig =>
  getAgentConfigById(s.activeAgentId || '')(s);

const currentAgentSystemRole = (s: AgentStoreState) => {
  return currentAgentConfig(s)?.systemRole;
};

const currentAgentModel = (s: AgentStoreState): string => {
  const config = currentAgentConfig(s);

  return config?.model || DEFAULT_MODEL;
};

const currentAgentModelProvider = (s: AgentStoreState) => {
  const config = currentAgentConfig(s);

  return config?.provider || DEFAULT_PROVIDER;
};

/**
 * Pinned plugin identifiers for the current agent — disabled entries are
 * excluded. Matches the pre-tri-state semantics where array-membership meant
 * pinned; consumers that need the disabled set use `getDisabledPluginIds`
 * directly.
 */
const currentAgentPlugins = (s: AgentStoreState) => {
  const config = currentAgentConfig(s);

  return getActivePluginIds(config?.plugins);
};

/**
 * Get displayable agent plugins by filtering out platform-specific tools
 * that shouldn't be shown in the current environment
 */
const displayableAgentPlugins = (s: AgentStoreState) => {
  const plugins = currentAgentPlugins(s);
  return filterToolIds(plugins);
};

const currentAgentKnowledgeBases = (s: AgentStoreState) => {
  const config = currentAgentConfig(s);

  return config?.knowledgeBases || [];
};

const currentAgentFiles = (s: AgentStoreState) => {
  const config = currentAgentConfig(s);

  return config?.files || [];
};

const currentAgentTTS = (s: AgentStoreState): LobeAgentTTSConfig => {
  const config = currentAgentConfig(s);

  return config?.tts || DEFAUTT_AGENT_TTS_CONFIG;
};

const currentAgentTTSVoice = (s: AgentStoreState): string =>
  currentAgentTTS(s).voice?.openai || 'alloy';

const currentEnabledKnowledge = (s: AgentStoreState) => {
  const knowledgeBases = currentAgentKnowledgeBases(s);
  const files = currentAgentFiles(s);

  return [
    ...files
      .filter((f) => f.enabled)
      .map((f) => ({ fileType: f.type, id: f.id, name: f.name, type: KnowledgeType.File })),
    ...knowledgeBases
      .filter((k) => k.enabled)
      .map((k) => ({ id: k.id, name: k.name, type: KnowledgeType.KnowledgeBase })),
  ] as KnowledgeItem[];
};

const hasSystemRole = (s: AgentStoreState) => {
  const config = currentAgentConfig(s);

  return !!config?.systemRole;
};

const hasKnowledgeBases = (s: AgentStoreState) => {
  const knowledgeBases = currentAgentKnowledgeBases(s);

  return knowledgeBases.length > 0;
};

const hasFiles = (s: AgentStoreState) => {
  const files = currentAgentFiles(s);

  return files.length > 0;
};

const hasKnowledge = (s: AgentStoreState) => hasKnowledgeBases(s) || hasFiles(s);
const hasEnabledKnowledge = (s: AgentStoreState) => currentEnabledKnowledge(s).length > 0;
const hasEnabledKnowledgeBases = (s: AgentStoreState) =>
  currentAgentKnowledgeBases(s).some((s) => s.enabled);

const currentKnowledgeIds = (s: AgentStoreState) => {
  return {
    fileIds: currentAgentFiles(s)
      .filter((item) => item.enabled)
      .map((f) => f.id),
    knowledgeBaseIds: currentAgentKnowledgeBases(s)
      .filter((item) => item.enabled)
      .map((k) => k.id),
  };
};

const isAgentConfigLoading = (s: AgentStoreState) =>
  // A not-found agent never lands in `agentMap` — treat it as settled so the
  // UI renders the 404 card instead of an endless skeleton.
  !s.activeAgentId || (!s.agentMap[s.activeAgentId] && !s.agentNotFoundMap[s.activeAgentId]);

/**
 * The active agent's config fetch settled on `null` — the agent doesn't exist
 * or the caller lost access (e.g. a workspace agent switched back to private).
 */
const isCurrentAgentNotFound = (s: AgentStoreState): boolean =>
  !!s.activeAgentId && !!s.agentNotFoundMap[s.activeAgentId];

/**
 * Fetch error for the active agent's config (undefined when none).
 * Distinguishes "fetch failed" from `isAgentConfigLoading`'s "no data yet",
 * so failure surfaces a retry UI instead of an endless skeleton.
 */
const currentAgentConfigError = (s: AgentStoreState): string | undefined =>
  s.activeAgentId ? s.agentConfigErrorMap[s.activeAgentId] : undefined;

const isAgentConfigError = (s: AgentStoreState) => !!currentAgentConfigError(s);

/**
 * Get agent's slug by ID (used to identify builtin agents)
 */
const getAgentSlugById = (agentId: string) => (s: AgentStoreState) => s.agentMap[agentId]?.slug;

const openingQuestions = (s: AgentStoreState) =>
  currentAgentConfig(s)?.openingQuestions || DEFAULT_OPENING_QUESTIONS;
const openingMessage = (s: AgentStoreState) => currentAgentConfig(s)?.openingMessage || '';

// ==========   Agent Mode Config   ============== //

/**
 * Get current agent's mode.
 * Agent mode is the default — only an explicit `chatConfig.enableAgentMode === false`
 * collapses the agent to chat mode.
 */
const currentAgentMode = (s: AgentStoreState): AgentMode | undefined => {
  const config = currentAgentConfig(s);
  const chatConfig = config?.chatConfig;
  return chatConfig?.enableAgentMode === false ? undefined : 'auto';
};

/**
 * Check if current agent mode is enabled
 */
const isAgentModeEnabled = (s: AgentStoreState): boolean => currentAgentMode(s) !== undefined;

/**
 * Get current agent's runtime env config
 * Now reads from chatConfig.runtimeEnv
 */
const currentAgentRuntimeEnvConfig = (s: AgentStoreState): RuntimeEnvConfig | undefined =>
  currentAgentConfig(s)?.chatConfig?.runtimeEnv;

/**
 * Get the active agent's agent-level working directory.
 *
 * Precedence mirrors `getAgentWorkingDirectoryById` (the agent-owned slice only;
 * topic overrides are layered on by callers):
 *
 *   agent's per-device choice (`agencyConfig.workingDirByDevice[targetDeviceId]`)
 *     > legacy per-agent localStorage value > home path
 *
 * `currentDeviceId` is passed in (not read cross-store) so the target device is
 * resolved correctly for device-bound agents and hook callers stay reactive.
 */
const currentAgentWorkingDirectory =
  (currentDeviceId?: string) =>
  (s: AgentStoreState): string | undefined => {
    if (!isDesktop) return;

    const homePath = globalAgentContextManager.getContext().homePath;
    const activeAgentId = s.activeAgentId;
    if (!activeAgentId) return homePath;

    const agencyConfig = currentAgentConfig(s)?.agencyConfig;
    const targetDeviceId = resolveTargetDeviceId(agencyConfig, currentDeviceId);
    const agentChoice = targetDeviceId
      ? getWorkingDirEffectivePath(agencyConfig?.workingDirByDevice?.[targetDeviceId])
      : undefined;

    return agentChoice ?? s.localAgentWorkingDirectoryMap[activeAgentId] ?? homePath;
  };

const isCurrentAgentExternal = (s: AgentStoreState): boolean => !currentAgentData(s)?.virtual;

/**
 * Whether current agent is driven by an external heterogeneous runtime
 * (e.g. Claude Code). These agents skip LobeHub's message-channel / model
 * pickers because their toolchain is owned by the external runtime.
 */
const isCurrentAgentHeterogeneous = (s: AgentStoreState): boolean =>
  !!currentAgentConfig(s)?.agencyConfig?.heterogeneousProvider;

const currentAgentHeterogeneousProviderType = (s: AgentStoreState) =>
  currentAgentConfig(s)?.agencyConfig?.heterogeneousProvider?.type;

const currentAgentExecutionTarget = (s: AgentStoreState) =>
  currentAgentConfig(s)?.agencyConfig?.executionTarget;

const getAgentDocumentsById = (agentId: string) => (s: AgentStoreState) =>
  s.agentDocumentsMap[agentId];

/** The agent a user-chosen slug names, as known locally (persisted copy first). */
const getAgentIdBySlug = (slug?: string | null) => (s: AgentStoreState) =>
  slug ? s.agentSlugIdMap[slug] : undefined;

/** The server confirmed the slug's agent in this session (not only the persisted copy). */
const isAgentSlugConfirmed = (slug?: string | null) => (s: AgentStoreState) =>
  !!slug && !!s.agentSlugIdMap[slug] && s.agentSlugIdReplica.entries[slug]?.source === 'server';

const heteroServerDefaultCapability = (s: AgentStoreState) => s.heteroServerDefaultCapability;

export const agentSelectors = {
  inboxAgentAvatar,
  inboxAgentDisplayName,
  currentAgentExecutionTarget,
  currentAgentHeterogeneousProviderType,
  currentAgentAuthorId,
  currentAgentAvatar,
  currentAgentBackgroundColor,
  currentAgentCreatedAt,
  currentAgentConfig,
  currentAgentConfigError,
  currentAgentDescription,
  currentAgentFiles,
  currentAgentKnowledgeBases,
  currentAgentRuntimeEnvConfig,
  currentAgentMeta,
  currentAgentMode,
  currentAgentModel,
  currentAgentModelProvider,
  currentAgentPlugins,
  currentAgentSystemRole,
  currentAgentTTS,
  currentAgentTTSVoice,
  currentAgentTags,
  currentAgentDisplayName,
  currentAgentTitle,
  currentAgentVisibility,
  getAgentVisibilityById,
  currentAgentWorkingDirectory,
  currentEnabledKnowledge,
  currentKnowledgeIds,
  displayableAgentPlugins,
  getAgentConfigById,
  getAgentDocumentsById,
  getAgentIdBySlug,
  heteroServerDefaultCapability,
  isAgentSlugConfirmed,
  getAgentFullBodyArtworkById,
  getAgentMetaById,
  getAgentProfileById,
  getAgentStoredAvatarById,
  getAgentSlugById,
  hasEnabledKnowledge,
  hasEnabledKnowledgeBases,
  hasKnowledge,
  hasSystemRole,
  inboxAgentConfig,
  inboxAgentModel,
  isAgentConfigError,
  isAgentConfigLoading,
  isCurrentAgentNotFound,
  isAgentModeEnabled,
  isCurrentAgentExternal,
  isCurrentAgentHeterogeneous,
  openingMessage,
  openingQuestions,
};