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: stop persisting hidden form values after base-ui form migration (#20644)

Innei97ad6cd0008c
diff --git a/src/features/AgentSetting/AgentSelfIteration/index.test.tsx b/src/features/AgentSetting/AgentSelfIteration/index.test.tsx
new file mode 100644
index 00000000000..4b775afadcf
--- /dev/null
+++ b/src/features/AgentSetting/AgentSelfIteration/index.test.tsx
@@ -0,0 +1,63 @@
+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import type { LobeAgentChatConfig } from '@/types/agent';
+
+import type { Store } from '../store/action';
+import AgentSelfIteration from './index';
+
+const testStore = await vi.hoisted(async () => {
+ const { createWithEqualityFn } = await import('zustand/traditional');
+ const { shallow } = await import('zustand/shallow');
+
+ return createWithEqualityFn<
+ Pick<Store, 'disabled' | 'setChatConfig'> & {
+ config: Pick<Store['config'], 'chatConfig'>;
+ }
+ >(() => ({ config: { chatConfig: {} }, disabled: false, setChatConfig: vi.fn() }), shallow);
+});
+
+vi.mock('@/store/agent', () => ({ useAgentStore: () => false }));
+vi.mock('@/store/agent/selectors', () => ({
+ builtinAgentSelectors: { isInboxAgent: () => false },
+}));
+
+vi.mock('../store', () => ({
+ selectors: {
+ currentChatConfig: (state: ReturnType<typeof testStore.getState>) => state.config.chatConfig,
+ },
+ useStore: testStore,
+}));
+
+afterEach(cleanup);
+
+describe('AgentSelfIteration', () => {
+ it.each([false, true])(
+ 'saves only selfIteration when changing enabled from %s',
+ async (enabled) => {
+ const setChatConfig = vi.fn().mockResolvedValue(undefined);
+ const chatConfig: LobeAgentChatConfig = {
+ enableHistoryCount: false,
+ historyCount: 5,
+ selfIteration: { enabled },
+ };
+ testStore.setState({ config: { chatConfig }, disabled: false, setChatConfig });
+
+ const { container } = render(<AgentSelfIteration />);
+
+ // Another settings surface updates the config after the form took its snapshot.
+ act(() => {
+ testStore.setState({
+ config: { chatConfig: { ...chatConfig, enableHistoryCount: true, historyCount: 20 } },
+ });
+ });
+
+ fireEvent.click(screen.getByRole('switch'));
+ expect(screen.getByRole('switch')).toHaveAttribute('aria-checked', String(!enabled));
+ fireEvent.submit(container.querySelector('form')!);
+
+ await waitFor(() => expect(setChatConfig).toHaveBeenCalledTimes(1));
+ expect(setChatConfig).toHaveBeenCalledWith({ selfIteration: { enabled: !enabled } });
+ },
+ );
+});
diff --git a/src/features/AgentSetting/AgentSelfIteration/index.tsx b/src/features/AgentSetting/AgentSelfIteration/index.tsx
index 4cb568d0fcf..936375fdda1 100644
--- a/src/features/AgentSetting/AgentSelfIteration/index.tsx
+++ b/src/features/AgentSetting/AgentSelfIteration/index.tsx
@@ -23,7 +23,7 @@ const AgentSelfIteration = memo(() => {
onSubmit: (values) => {
if (disabled) return;
- updateConfig(values);
+ updateConfig({ selfIteration: values.selfIteration });
},
});
diff --git a/src/features/PluginDevModal/index.tsx b/src/features/PluginDevModal/index.tsx
index 77ba940d6d6..2185ce3e5bb 100644
--- a/src/features/PluginDevModal/index.tsx
+++ b/src/features/PluginDevModal/index.tsx
@@ -12,6 +12,7 @@ import { openConnectorOAuthPopup } from '@/utils/connectorOAuth';
import MCPManifestForm from './MCPManifestForm';
import PluginPreview from './PluginPreview';
+import { pruneHiddenFields } from './pruneHiddenFields';
import { getSaveErrorToast } from './saveErrorToast';
const INITIAL_VALUES = {
@@ -83,7 +84,7 @@ const DevModal = memo<DevModalProps>(
}
setSubmitting(true);
try {
- await onSave(values, ctx);
+ await onSave(pruneHiddenFields(values), ctx);
toast.success(t(isEditMode ? 'dev.updateSuccess' : 'dev.saveSuccess'));
onOpenChange(false);
} catch (error) {
diff --git a/src/features/PluginDevModal/pruneHiddenFields.test.ts b/src/features/PluginDevModal/pruneHiddenFields.test.ts
new file mode 100644
index 00000000000..57d8e75dddd
--- /dev/null
+++ b/src/features/PluginDevModal/pruneHiddenFields.test.ts
@@ -0,0 +1,85 @@
+import { type LobeToolCustomPlugin } from '@lobechat/types';
+import { describe, expect, it } from 'vitest';
+
+import { pruneHiddenFields } from './pruneHiddenFields';
+
+type Mcp = NonNullable<NonNullable<LobeToolCustomPlugin['customParams']>['mcp']>;
+
+const plugin = (mcp: Mcp): LobeToolCustomPlugin => ({
+ customParams: { avatar: 'a.png', description: 'desc', mcp },
+ identifier: 'demo',
+ type: 'customPlugin',
+});
+
+const stale = {
+ args: ['-y', 'server'],
+ auth: { clientId: 'cid', clientSecret: 'secret', token: 'tok', type: 'none' as const },
+ command: 'npx',
+ env: { KEY: 'v' },
+ headers: { 'X-Key': 'h' },
+ url: 'https://old.example.com/mcp',
+};
+
+describe('pruneHiddenFields', () => {
+ it('drops http fields after switching to stdio', () => {
+ const mcp = pruneHiddenFields(plugin({ ...stale, type: 'stdio' })).customParams!.mcp!;
+
+ expect(mcp).toEqual({
+ args: ['-y', 'server'],
+ command: 'npx',
+ env: { KEY: 'v' },
+ type: 'stdio',
+ });
+ });
+
+ it('drops stdio fields and auth secrets when http auth is none', () => {
+ const mcp = pruneHiddenFields(plugin({ ...stale, type: 'http' })).customParams!.mcp!;
+
+ expect(mcp).toEqual({
+ auth: { type: 'none' },
+ headers: { 'X-Key': 'h' },
+ type: 'http',
+ url: 'https://old.example.com/mcp',
+ });
+ });
+
+ it('keeps only the token for bearer auth', () => {
+ const mcp = pruneHiddenFields(
+ plugin({ ...stale, auth: { ...stale.auth, type: 'bearer' }, type: 'http' }),
+ ).customParams!.mcp!;
+
+ expect(mcp.auth).toEqual({ token: 'tok', type: 'bearer' });
+ });
+
+ it('keeps only oauth credentials for oauth2 auth', () => {
+ const mcp = pruneHiddenFields(
+ plugin({
+ ...stale,
+ auth: { ...stale.auth, accessToken: 'at', type: 'oauth2' },
+ type: 'http',
+ }),
+ ).customParams!.mcp!;
+
+ expect(mcp.auth).toEqual({
+ accessToken: 'at',
+ clientId: 'cid',
+ clientSecret: 'secret',
+ type: 'oauth2',
+ });
+ });
+
+ it('keeps fields outside the conditional branches', () => {
+ const result = pruneHiddenFields(plugin({ ...stale, type: 'stdio' }));
+
+ expect(result.identifier).toBe('demo');
+ expect(result.customParams).toMatchObject({ avatar: 'a.png', description: 'desc' });
+ });
+
+ it('leaves cloud and non-mcp plugins untouched', () => {
+ const cloud = plugin({ ...stale, type: 'cloud' });
+ const legacy: LobeToolCustomPlugin = { identifier: 'x', type: 'customPlugin' };
+
+ expect(pruneHiddenFields(cloud)).toBe(cloud);
+ expect(pruneHiddenFields(legacy)).toBe(legacy);
+ });
+});
diff --git a/src/features/PluginDevModal/pruneHiddenFields.ts b/src/features/PluginDevModal/pruneHiddenFields.ts
new file mode 100644
index 00000000000..36dbc6327c2
--- /dev/null
+++ b/src/features/PluginDevModal/pruneHiddenFields.ts
@@ -0,0 +1,28 @@
+import { type LobeToolCustomPlugin } from '@lobechat/types';
+
+// The form keeps values of unmounted conditional fields, so switching transport or auth type
+// would otherwise persist the abandoned branch (e.g. a bearer token after choosing "none").
+export const pruneHiddenFields = (values: LobeToolCustomPlugin): LobeToolCustomPlugin => {
+ const mcp = values.customParams?.mcp;
+ if (!mcp || (mcp.type !== 'http' && mcp.type !== 'stdio')) return values;
+
+ const { args, auth, command, env, headers, url, ...rest } = mcp;
+
+ let nextMcp: typeof mcp;
+ if (mcp.type === 'stdio') {
+ nextMcp = { ...rest, args, command, env };
+ } else {
+ let nextAuth = auth;
+ if (auth) {
+ const { accessToken, clientId, clientSecret, token, ...authRest } = auth;
+ nextAuth = {
+ ...authRest,
+ ...(auth.type === 'bearer' && { token }),
+ ...(auth.type === 'oauth2' && { accessToken, clientId, clientSecret }),
+ };
+ }
+ nextMcp = { ...rest, auth: nextAuth, headers, url };
+ }
+
+ return { ...values, customParams: { ...values.customParams, mcp: nextMcp } };
+};