Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(console): show saved provider settings for known providers

yuxiaojian1fb0652fd6aa
diff --git a/console/apps/switch-console-desktop/src/main/core/providers/agent-payload-builder-settings.test.ts b/console/apps/switch-console-desktop/src/main/core/providers/agent-payload-builder-settings.test.ts
new file mode 100644
index 000000000..a3364035b
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/main/core/providers/agent-payload-builder-settings.test.ts
@@ -0,0 +1,90 @@
+import type { CLIAgentPluginProvider } from '@switch-console/core/agents/plugins';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+// Unlike agent-payload-builder.test.ts, the settings service here is real: only the
+// app_settings table behind it is an in-memory stand-in.
+const rows = new Map<string, string>();
+
+vi.mock('drizzle-orm', () => ({ eq: (_column: unknown, value: unknown) => value }));
+vi.mock('@main/db/schema', () => ({ appSettings: { key: 'key', value: 'value' } }));
+vi.mock('@main/db/client', () => ({
+ db: {
+ select: () => ({
+ from: () => ({
+ where: (key: string) => ({
+ execute: async () => (rows.has(key) ? [{ key, value: rows.get(key) }] : []),
+ }),
+ }),
+ }),
+ },
+}));
+
+vi.mock('@main/core/providers/plugin-registry', () => ({
+ getPlugin: vi.fn(),
+ listPlugins: vi.fn().mockReturnValue([]),
+}));
+vi.mock('@main/core/dependencies/registry', () => ({
+ getDependencyDescriptor: vi.fn().mockReturnValue(undefined),
+ DEPENDENCIES: [],
+}));
+
+const { getPlugin } = await import('@main/core/providers/plugin-registry');
+const { setAgentProviderCatalogue } =
+ await import('@shared/core/providers/agent-provider-registry');
+const { providerOverrideSettings } = await import('../settings/provider-settings-service');
+const { buildAgentPayload } = await import('./agent-payload-builder');
+
+const plugin = (id: string) =>
+ ({
+ metadata: { id, name: id, description: id, websiteUrl: `https://${id}.example.com` },
+ capabilities: {
+ hostDependency: { binaryNames: [id], installCommands: {}, updates: { kind: 'none' } },
+ models: { kind: 'none' },
+ effort: { kind: 'none' },
+ prompt: { kind: 'argv', flag: '' },
+ sessions: { kind: 'resumable' },
+ autoApprove: { kind: 'supported' },
+ mcp: { kind: 'none' },
+ plugins: { kind: 'none' },
+ },
+ assets: { icon: { variants: [], invertInDark: false } },
+ behavior: {},
+ validate: () => [],
+ }) as unknown as CLIAgentPluginProvider;
+
+const catalogued = (id: string) => ({
+ id,
+ name: id,
+ description: '',
+ docUrl: '',
+ cliLabel: id,
+ loginCommand: id,
+ knownAgentType: id,
+});
+
+beforeEach(() => {
+ rows.clear();
+ providerOverrideSettings.invalidateCache();
+ setAgentProviderCatalogue([catalogued('claude')]);
+ vi.mocked(getPlugin).mockImplementation((id: string) => plugin(id));
+});
+
+describe('buildAgentPayload with the real settings service', () => {
+ it('carries a saved provider override into the payload settings', async () => {
+ rows.set(
+ 'providerConfigs',
+ JSON.stringify({ claude: { env: { CLAUDE_CONFIG_DIR: '/SENTINEL/config' } } })
+ );
+ const payload = await buildAgentPayload('claude', 'macos');
+ expect(payload?.settings).toEqual({
+ value: { env: { CLAUDE_CONFIG_DIR: '/SENTINEL/config' } },
+ defaults: {},
+ overrides: { env: { CLAUDE_CONFIG_DIR: '/SENTINEL/config' } },
+ });
+ });
+
+ it('gives a known provider with nothing saved empty settings', async () => {
+ const payload = await buildAgentPayload('claude', 'macos');
+ expect(payload?.settings).toEqual({ value: {}, defaults: {}, overrides: {} });
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/main/core/settings/provider-settings-roundtrip.test.ts b/console/apps/switch-console-desktop/src/main/core/settings/provider-settings-roundtrip.test.ts
new file mode 100644
index 000000000..95d80330c
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/main/core/settings/provider-settings-roundtrip.test.ts
@@ -0,0 +1,155 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+// An in-memory stand-in for the app_settings table: one row per storage key.
+const rows = new Map<string, string>();
+
+vi.mock('drizzle-orm', () => ({ eq: (_column: unknown, value: unknown) => value }));
+vi.mock('@main/db/schema', () => ({ appSettings: { key: 'key', value: 'value' } }));
+vi.mock('@main/db/client', () => ({
+ db: {
+ select: () => ({
+ from: () => ({
+ where: (key: string) => ({
+ execute: async () => (rows.has(key) ? [{ key, value: rows.get(key) }] : []),
+ }),
+ }),
+ }),
+ insert: () => ({
+ values: (row: { key: string; value: string }) => ({
+ onConflictDoUpdate: () => ({
+ execute: async () => {
+ rows.set(row.key, row.value);
+ },
+ }),
+ }),
+ }),
+ delete: () => ({
+ where: (key: string) => ({
+ execute: async () => {
+ rows.delete(key);
+ },
+ }),
+ }),
+ },
+}));
+
+// Imported before any catalogue is installed: building the service must not read it.
+const { providerOverrideSettings } = await import('./provider-settings-service');
+const { setAgentProviderCatalogue } =
+ await import('@shared/core/providers/agent-provider-registry');
+
+const provider = (id: string) => ({
+ id,
+ name: id,
+ description: '',
+ docUrl: '',
+ cliLabel: id,
+ loginCommand: id,
+ knownAgentType: id,
+});
+
+const KEY = 'providerConfigs';
+const SENTINEL = { claude: { env: { CLAUDE_CONFIG_DIR: '/SENTINEL/config' } } };
+
+function stored() {
+ const raw = rows.get(KEY);
+ return raw === undefined ? undefined : JSON.parse(raw);
+}
+
+// What the provider RPC returns for the settings sheet.
+const getSettings = (id: string) => providerOverrideSettings.getItemWithMeta(id);
+const getDefaultSettings = async (id: string) =>
+ (await providerOverrideSettings.getItemWithMeta(id))?.defaults ?? null;
+
+beforeEach(() => {
+ rows.clear();
+ providerOverrideSettings.invalidateCache();
+ setAgentProviderCatalogue([provider('claude'), provider('codex')]);
+});
+
+describe('provider settings for known providers', () => {
+ it('returns a saved override even though no defaults are configured', async () => {
+ rows.set(KEY, JSON.stringify(SENTINEL));
+ expect(await getSettings('claude')).toEqual({
+ value: SENTINEL.claude,
+ defaults: {},
+ overrides: SENTINEL.claude,
+ });
+ });
+
+ it('returns empty metadata for a known provider with nothing saved', async () => {
+ expect(await getSettings('codex')).toEqual({ value: {}, defaults: {}, overrides: {} });
+ expect(await getDefaultSettings('codex')).toEqual({});
+ });
+
+ it('keeps an unknown provider absent, and its saved row through other edits and resets', async () => {
+ rows.set(KEY, JSON.stringify({ ...SENTINEL, legacy: { extraArgs: '--old' } }));
+ expect(await getSettings('legacy')).toBeNull();
+ expect(await getDefaultSettings('legacy')).toBeNull();
+
+ await providerOverrideSettings.updateItem('codex', { env: { A: '1' } });
+ await providerOverrideSettings.updateItem('claude', {});
+ expect(stored()).toEqual({ codex: { env: { A: '1' } }, legacy: { extraArgs: '--old' } });
+ expect(await getSettings('legacy')).toBeNull();
+ });
+
+ it('round-trips edit, add, delete of env rows and an empty value', async () => {
+ await providerOverrideSettings.updateItem('claude', {
+ extraArgs: '--keep',
+ env: { CLAUDE_CONFIG_DIR: '/SENTINEL/config', EMPTY: '' },
+ });
+ expect((await getSettings('claude'))?.value).toEqual({
+ extraArgs: '--keep',
+ env: { CLAUDE_CONFIG_DIR: '/SENTINEL/config', EMPTY: '' },
+ });
+
+ await providerOverrideSettings.updateItem('claude', {
+ extraArgs: '--keep',
+ env: { CLAUDE_CONFIG_DIR: '/SENTINEL/other' },
+ });
+ const edited = await getSettings('claude');
+ expect(edited?.value.env).toEqual({ CLAUDE_CONFIG_DIR: '/SENTINEL/other' });
+ expect(edited?.overrides.extraArgs).toBe('--keep');
+ });
+
+ it('stores nothing for the UI default path: env undefined and no args', async () => {
+ // The settings sheet sends `env: undefined` when no variable is set.
+ await providerOverrideSettings.updateItem('claude', { extraArgs: undefined, env: undefined });
+ expect(stored()).toBeUndefined();
+ expect(await getSettings('claude')).toEqual({ value: {}, defaults: {}, overrides: {} });
+ });
+
+ it('keeps an explicit empty env object as saved (existing store behaviour, not changed here)', async () => {
+ // The sheet never sends `env: {}`, but the generic store keeps what it is given
+ // when it differs from the default, and a provider has no env default.
+ await providerOverrideSettings.updateItem('claude', { env: {} });
+ expect(stored()).toEqual({ claude: { env: {} } });
+ await providerOverrideSettings.updateItem('claude', {});
+ expect(stored()).toBeUndefined();
+ });
+
+ it('reset clears only the selected provider and a re-read does not bring it back', async () => {
+ rows.set(KEY, JSON.stringify({ ...SENTINEL, codex: { env: { A: '1' } } }));
+ expect((await getSettings('claude'))?.overrides).toEqual(SENTINEL.claude);
+
+ // The reset hook writes the defaults back.
+ await providerOverrideSettings.updateItem('claude', (await getDefaultSettings('claude'))!);
+
+ expect(await getSettings('claude')).toEqual({ value: {}, defaults: {}, overrides: {} });
+ expect(await getSettings('codex')).toMatchObject({ overrides: { env: { A: '1' } } });
+ expect(stored()).toEqual({ codex: { env: { A: '1' } } });
+ expect(Object.keys(await providerOverrideSettings.getAll()).sort()).toEqual([
+ 'claude',
+ 'codex',
+ ]);
+ });
+});
+
+describe('catalogue timing', () => {
+ it('reads the catalogue on use, not when the service is imported', async () => {
+ // The module was imported above before any catalogue existed.
+ setAgentProviderCatalogue([provider('opencode')]);
+ expect(await getSettings('opencode')).toEqual({ value: {}, defaults: {}, overrides: {} });
+ expect(await getSettings('claude')).toBeNull();
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/main/core/settings/provider-settings-service.ts b/console/apps/switch-console-desktop/src/main/core/settings/provider-settings-service.ts
index f5b1e6221..5ea32814a 100644
--- a/console/apps/switch-console-desktop/src/main/core/settings/provider-settings-service.ts
+++ b/console/apps/switch-console-desktop/src/main/core/settings/provider-settings-service.ts
@@ -1,14 +1,24 @@
import type { ProviderCustomConfig } from '@shared/core/app-settings';
+import { agentProviderIds } from '@shared/core/providers/agent-provider-registry';
import { OverrideSettings } from './override-settings';
import {
migrateProviderConfigOverrides,
migrateProviderConfigToHostDependencyStore,
} from './provider-config-migrations';
-import { providerConfigDefaults, providerCustomConfigEntrySchema } from './schema';
+import { providerCustomConfigEntrySchema } from './schema';
+
+/**
+ * Every registered provider starts from empty defaults, so its saved settings
+ * are readable and reset has something to write. An id the catalogue does not
+ * know has none, and the store reports it as absent. Read on each call, never
+ * at import: the catalogue is installed as the main process loads its plugins.
+ */
+const providerDefaults = (): Record<string, ProviderCustomConfig> =>
+ Object.fromEntries(agentProviderIds().map((id) => [id, {}]));
export const providerOverrideSettings = new OverrideSettings<ProviderCustomConfig>(
'providerConfigs',
- () => providerConfigDefaults as Record<string, ProviderCustomConfig>,
+ providerDefaults,
providerCustomConfigEntrySchema,
migrateProviderConfigOverrides
);
diff --git a/console/apps/switch-console-desktop/src/main/core/settings/schema.ts b/console/apps/switch-console-desktop/src/main/core/settings/schema.ts
index d3cf2b54b..f98736242 100644
--- a/console/apps/switch-console-desktop/src/main/core/settings/schema.ts
+++ b/console/apps/switch-console-desktop/src/main/core/settings/schema.ts
@@ -47,8 +47,6 @@ export const providerCustomConfigEntrySchema = z.object({
env: z.record(z.string(), z.string()).optional(),
});
-export const providerConfigDefaults: Record<string, unknown> = {};
-
export const interfaceSettingsSchema = z.object({
sessionHoverAction: z.enum(['delete', 'archive']),
autoRightSidebarBehavior: z.boolean(),