π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { DEFAULT_PROVIDER } from '@lobechat/business-const';
import {
DEFAULT_MODEL,
DEFAUTT_AGENT_TTS_CONFIG,
isAgentShareModelRestrictionExempt,
isDesktop,
} from '@lobechat/const';
import { type AgentBuilderContext } from '@lobechat/context-engine';
import {
type AgentMode,
getActivePluginIds,
getOptedInPluginIds,
getWorkingDirEffectivePath,
type LobeAgentAgencyConfig,
type LobeAgentTTSConfig,
type RuntimeEnvConfig,
} from '@lobechat/types';
import { resolveTargetDeviceId } from '@/helpers/agentWorkingDirectory';
import { globalAgentContextManager } from '@/helpers/GlobalAgentContextManager';
import { type AgentStoreState } from '../initialState';
import { agentSelectors } from './selectors';
/**
* Selectors that get agent config by agentId parameter.
* Used in ChatInput components where agentId is passed as prop.
*/
const getAgentModelById =
(agentId: string) =>
(s: AgentStoreState): string =>
agentSelectors.getAgentConfigById(agentId)(s)?.model || DEFAULT_MODEL;
const getAgentModelProviderById =
(agentId: string) =>
(s: AgentStoreState): string =>
agentSelectors.getAgentConfigById(agentId)(s)?.provider || DEFAULT_PROVIDER;
/**
* Pinned plugin identifiers for the agent β disabled entries are excluded.
* Every current consumer (token estimation, share-image preview, auth alerts,
* the Skills panel) wants "what's actually configured/active", matching the
* pre-tri-state semantics where array-membership meant pinned.
*/
const getAgentPluginsById =
(agentId: string) =>
(s: AgentStoreState): string[] =>
getActivePluginIds(agentSelectors.getAgentConfigById(agentId)(s)?.plugins);
const getAgentSystemRoleById =
(agentId: string) =>
(s: AgentStoreState): string | undefined =>
agentSelectors.getAgentConfigById(agentId)(s)?.systemRole;
const getAgentTTSById =
(agentId: string) =>
(s: AgentStoreState): LobeAgentTTSConfig =>
agentSelectors.getAgentConfigById(agentId)(s)?.tts || DEFAUTT_AGENT_TTS_CONFIG;
const getAgentTTSVoiceById =
(agentId: string) =>
(s: AgentStoreState): string =>
getAgentTTSById(agentId)(s).voice?.openai || 'alloy';
const getAgentConfigErrorById =
(agentId: string) =>
(s: AgentStoreState): string | undefined =>
agentId ? s.agentConfigErrorMap[agentId] : undefined;
// A mount whose file / library the caller can no longer see (flipped back to
// private, another member's private item) comes back from the server with the
// entity fields nulled out β drop it so menus never render a nameless row.
const getAgentFilesById = (agentId: string) => (s: AgentStoreState) =>
(agentSelectors.getAgentConfigById(agentId)(s)?.files || []).filter((item) => !!item.id);
const getAgentKnowledgeBasesById = (agentId: string) => (s: AgentStoreState) =>
(agentSelectors.getAgentConfigById(agentId)(s)?.knowledgeBases || []).filter((item) => !!item.id);
/**
* The config fetch settled on `null` β the agent doesn't exist or the caller
* lost access (e.g. a workspace agent switched back to private). Render a
* 404 / no-access card, not a skeleton.
*/
const isAgentNotFoundById =
(agentId: string) =>
(s: AgentStoreState): boolean =>
!!agentId && !!s.agentNotFoundMap[agentId];
const isAgentConfigLoadingById = (agentId: string) => (s: AgentStoreState) =>
// A not-found agent never lands in `agentMap`; without this guard the
// data-presence check would report "loading" forever.
!agentId || (!s.agentMap[agentId] && !s.agentNotFoundMap[agentId]);
/**
* Whether the config for `agentId` belongs to `scope` and may be rendered.
* `agentMap` only ever holds one scope; this guards the frame between a scope
* switch and the replica dropping the previous identity's entries.
*/
const hasAgentConfigInScope =
(agentId: string, scope: string) =>
(s: AgentStoreState): boolean =>
!!agentId && !!s.agentMap[agentId] && s.agentConfigReplica.scope === scope;
/**
* Get agent mode by agentId.
* Agent mode is the default β only an explicit `chatConfig.enableAgentMode === false`
* collapses the agent to chat mode.
*/
const getAgentModeById =
(agentId: string) =>
(s: AgentStoreState): AgentMode | undefined => {
const config = agentSelectors.getAgentConfigById(agentId)(s);
const chatConfig = config?.chatConfig;
return chatConfig?.enableAgentMode === false ? undefined : 'auto';
};
/**
* Check if agent mode is enabled by agentId.
* Defaults to true; only explicit `chatConfig.enableAgentMode === false` returns false.
*/
const getAgentEnableModeById =
(agentId: string) =>
(s: AgentStoreState): boolean => {
const config = agentSelectors.getAgentConfigById(agentId)(s);
const chatConfig = config?.chatConfig;
return chatConfig?.enableAgentMode !== false;
};
/**
* Get runtime env config by agentId
* Now reads from chatConfig.runtimeEnv
*/
const getAgentRuntimeEnvConfigById =
(agentId: string) =>
(s: AgentStoreState): RuntimeEnvConfig | undefined =>
agentSelectors.getAgentConfigById(agentId)(s)?.chatConfig?.runtimeEnv;
/**
* Get the agent-level working directory by agentId.
*
* Precedence (the agent-owned slice only β topic overrides and device defaults
* are layered on by callers):
*
* agent's per-device choice (`agencyConfig.workingDirByDevice[targetDeviceId]`)
* > legacy per-agent localStorage value (pre-migration fallback)
* > desktop path > home path
*
* `currentDeviceId` is passed in (not read cross-store) so hook callers stay
* reactive to device changes. The target device is resolved from it via
* `resolveTargetDeviceId`, so a device-bound agent reads its bound device's
* choice rather than the local machine's.
*/
const getAgentWorkingDirectoryById =
(agentId: string, currentDeviceId?: string) =>
(s: AgentStoreState): string | undefined => {
if (!isDesktop) return;
const ctx = globalAgentContextManager.getContext();
const agencyConfig = agentSelectors.getAgentConfigById(agentId)(s)?.agencyConfig;
const targetDeviceId = resolveTargetDeviceId(agencyConfig, currentDeviceId);
const agentChoice = targetDeviceId
? getWorkingDirEffectivePath(agencyConfig?.workingDirByDevice?.[targetDeviceId])
: undefined;
return (
agentChoice ?? s.localAgentWorkingDirectoryMap[agentId] ?? ctx.desktopPath ?? ctx.homePath
);
};
/**
* Get agent builder context by agentId
* Used for injecting current agent config/meta into Agent Builder context
*/
const getAgentBuilderContextById =
(agentId: string) =>
(s: AgentStoreState): AgentBuilderContext => {
const config = agentSelectors.getAgentConfigById(agentId)(s);
const meta = agentSelectors.getAgentMetaById(agentId)(s);
return {
config: {
chatConfig: config?.chatConfig,
model: config?.model,
openingMessage: config?.openingMessage,
openingQuestions: config?.openingQuestions,
params: config?.params,
// Opted-in identifiers (`auto` + legacy `pinned`) β excludes explicitly disabled plugins.
plugins: getOptedInPluginIds(config?.plugins),
provider: config?.provider,
systemRole: config?.systemRole,
},
meta,
};
};
/**
* Get agencyConfig by agentId
*/
const getAgencyConfigById =
(agentId: string) =>
(s: AgentStoreState): LobeAgentAgencyConfig | undefined =>
agentSelectors.getAgentConfigById(agentId)(s)?.agencyConfig;
/**
* Whether the agent is driven by an external heterogeneous runtime
* (e.g. Claude Code) β by agentId.
*/
const isAgentHeterogeneousById =
(agentId: string) =>
(s: AgentStoreState): boolean =>
!!getAgencyConfigById(agentId)(s)?.heterogeneousProvider;
/**
* Whether a shared agent is exempt from the share model-provider restriction
* (a Claude Code / Codex agent) β by agentId. Mirrors the server check.
*/
const isAgentShareModelRestrictionExemptById =
(agentId: string) =>
(s: AgentStoreState): boolean =>
isAgentShareModelRestrictionExempt({
agencyConfig: getAgencyConfigById(agentId)(s),
provider: agentSelectors.getAgentConfigById(agentId)(s)?.provider,
});
/**
* Get full agent data by agentId
* Returns the complete agent object including metadata fields like updatedAt
*/
const getAgentById = (agentId: string) => (s: AgentStoreState) => s.agentMap[agentId];
/**
* Workspace-scoped agent: shared across workspace members, so it executes on
* the workspace device pool / sandbox β never on the current member's own
* client. Feed this into `resolveExecutionTarget`'s `workspaceScoped` option.
*/
const isWorkspaceAgentById =
(agentId: string) =>
(s: AgentStoreState): boolean =>
!!s.agentMap[agentId]?.workspaceId;
export const agentByIdSelectors = {
getAgencyConfigById,
getAgentBuilderContextById,
getAgentById,
getAgentConfigById: agentSelectors.getAgentConfigById,
getAgentConfigErrorById,
getAgentEnableModeById,
getAgentFilesById,
getAgentKnowledgeBasesById,
getAgentRuntimeEnvConfigById,
getAgentModeById,
getAgentModelById,
getAgentModelProviderById,
getAgentPluginsById,
getAgentSystemRoleById,
getAgentTTSById,
getAgentTTSVoiceById,
getAgentWorkingDirectoryById,
hasAgentConfigInScope,
isAgentConfigLoadingById,
isAgentHeterogeneousById,
isAgentNotFoundById,
isAgentShareModelRestrictionExemptById,
isWorkspaceAgentById,
};