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.

🐛 fix: guard missing agent config in workspace profile editor (#17425)

Rdmclin21d75113f5255
diff --git a/src/routes/(main)/agent/profile/features/ProfileEditor/WorkspaceAgentModelPolicy.test.tsx b/src/routes/(main)/agent/profile/features/ProfileEditor/WorkspaceAgentModelPolicy.test.tsx
new file mode 100644
index 00000000000..c3fb3dce3fb
--- /dev/null
+++ b/src/routes/(main)/agent/profile/features/ProfileEditor/WorkspaceAgentModelPolicy.test.tsx
@@ -0,0 +1,63 @@
+import { render, screen } from '@testing-library/react';
+import type { ReactNode } from 'react';
+import { describe, expect, it, vi } from 'vitest';
+
+import { WorkspaceAgentModelPolicy } from './WorkspaceAgentModelPolicy';
+
+const testState = vi.hoisted(() => ({
+ agent: {
+ agentMap: {
+ 'agent-1': {
+ agencyConfig: {
+ modelSelectionPolicy: 'member' as const,
+ },
+ model: 'gpt-4',
+ visibility: 'public' as 'private' | 'public',
+ workspaceId: 'workspace-1',
+ },
+ } as Record<string, object>,
+ updateAgentConfigById: vi.fn(),
+ },
+}));
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+vi.mock('@/hooks/usePermission', () => ({
+ usePermission: () => ({ allowed: true }),
+}));
+
+vi.mock('@/features/ModelSelect', () => ({
+ default: () => <div data-testid="model-select" />,
+}));
+
+vi.mock('./WorkspaceAgentPolicyCard', () => ({
+ WorkspaceAgentPolicyCard: ({ children, title }: { children: ReactNode; title: string }) => (
+ <div>
+ <span>{title}</span>
+ {children}
+ </div>
+ ),
+ WorkspaceAgentSelectionPolicyMenu: () => <div data-testid="policy-menu" />,
+}));
+
+vi.mock('@/store/agent', () => ({
+ useAgentStore: (selector: (state: typeof testState.agent) => unknown) =>
+ selector(testState.agent),
+}));
+
+describe('WorkspaceAgentModelPolicy', () => {
+ it('renders the policy card for a loaded workspace agent', () => {
+ render(<WorkspaceAgentModelPolicy agentId="agent-1" />);
+
+ expect(screen.getByText('settingAgent.modelPolicy.title')).toBeTruthy();
+ expect(screen.getByTestId('model-select')).toBeTruthy();
+ });
+
+ it('renders nothing instead of crashing while the agent config is not loaded yet', () => {
+ const { container } = render(<WorkspaceAgentModelPolicy agentId="missing-agent" />);
+
+ expect(container.firstChild).toBeNull();
+ });
+});
diff --git a/src/routes/(main)/agent/profile/features/ProfileEditor/WorkspaceAgentModelPolicy.tsx b/src/routes/(main)/agent/profile/features/ProfileEditor/WorkspaceAgentModelPolicy.tsx
index 23df4e7d3bf..accb08adbf8 100644
--- a/src/routes/(main)/agent/profile/features/ProfileEditor/WorkspaceAgentModelPolicy.tsx
+++ b/src/routes/(main)/agent/profile/features/ProfileEditor/WorkspaceAgentModelPolicy.tsx
@@ -26,9 +26,9 @@ export const WorkspaceAgentModelPolicy = memo<WorkspaceAgentModelPolicyProps>(({
const config = useAgentStore(agentSelectors.getAgentConfigById(agentId), isEqual);
const agent = useAgentStore(agentByIdSelectors.getAgentById(agentId));
const updateAgentConfigById = useAgentStore((s) => s.updateAgentConfigById);
- const isLocked = config.agencyConfig?.modelSelectionPolicy !== 'member';
+ if (!agent?.workspaceId || !config) return null;
- if (!agent?.workspaceId) return null;
+ const isLocked = config.agencyConfig?.modelSelectionPolicy !== 'member';
const labelKeys = getWorkspaceAgentSelectionPolicyLabelKeys(agent.visibility === 'private');
diff --git a/src/routes/(main)/agent/profile/features/ProfileEditor/index.tsx b/src/routes/(main)/agent/profile/features/ProfileEditor/index.tsx
index 5253791c459..ef8c6f07742 100644
--- a/src/routes/(main)/agent/profile/features/ProfileEditor/index.tsx
+++ b/src/routes/(main)/agent/profile/features/ProfileEditor/index.tsx
@@ -56,7 +56,7 @@ const ProfileEditor = memo(() => {
const isWorkspaceAgent = useAgentStore(agentByIdSelectors.isWorkspaceAgentById(agentId));
const updateAgentConfigById = useAgentStore((s) => s.updateAgentConfigById);
const isHeterogeneous = useAgentStore(agentSelectors.isCurrentAgentHeterogeneous);
- const heterogeneousProvider = config.agencyConfig?.heterogeneousProvider;
+ const heterogeneousProvider = config?.agencyConfig?.heterogeneousProvider;
const updateHeterogeneousCommand = async (command: string) => {
if (!canEdit) return;
@@ -80,7 +80,7 @@ const ProfileEditor = memo(() => {
const updateBoundDeviceId = async (boundDeviceId: string) => {
await updateAgentConfigById(agentId, {
- agencyConfig: { ...config.agencyConfig, boundDeviceId, executionTarget: 'device' },
+ agencyConfig: { ...config?.agencyConfig, boundDeviceId, executionTarget: 'device' },
});
};
@@ -174,8 +174,8 @@ const ProfileEditor = memo(() => {
disabled={!canEdit}
popupWidth={400}
value={{
- model: config.model,
- provider: config.provider,
+ model: config?.model,
+ provider: config?.provider,
}}
onChange={(value) => {
if (!canEdit) return;