diff --git a/apps/web/src/components/SettingsDialog.tsx b/apps/web/src/components/SettingsDialog.tsx index 8a6193b7c2..90d7162fe8 100644 --- a/apps/web/src/components/SettingsDialog.tsx +++ b/apps/web/src/components/SettingsDialog.tsx @@ -1696,6 +1696,34 @@ export function SettingsDialog({ ); } +/** + * The four UI states the Composio API key field can be in. + * + * `saved-pending` exists so the saved-key indicator stays visible while + * the user types a draft replacement. Previously the badge was tied to + * `!hasPendingEdit`, which made it vanish on the first keystroke and + * trained users to think the original key had already been overwritten + * (issue #741). Treating "saved key plus draft" as its own state lets + * the badge stay anchored while the hint text differentiates the + * unsaved replacement from a fully-saved value. + */ +export type ComposioCredentialState = + | 'empty' + | 'pending-new' + | 'saved' + | 'saved-pending'; + +export function deriveComposioCredentialState( + composio: { apiKey?: string; apiKeyConfigured?: boolean } | null | undefined, +): ComposioCredentialState { + const hasPendingEdit = Boolean(composio?.apiKey?.trim()); + const hasSavedKey = Boolean(composio?.apiKeyConfigured); + if (hasSavedKey && hasPendingEdit) return 'saved-pending'; + if (hasSavedKey) return 'saved'; + if (hasPendingEdit) return 'pending-new'; + return 'empty'; +} + function ComposioSection({ cfg, setCfg, @@ -1708,9 +1736,9 @@ function ComposioSection({ const updateComposio = (patch: NonNullable) => { setCfg((curr) => ({ ...curr, composio: { ...(curr.composio ?? {}), ...patch } })); }; - const hasPendingEdit = Boolean(composio.apiKey?.trim()); - const apiKeyConfigured = Boolean(hasPendingEdit || composio.apiKeyConfigured); - const isSavedState = apiKeyConfigured && !hasPendingEdit; + const credentialState = deriveComposioCredentialState(composio); + const hasSavedKey = credentialState === 'saved' || credentialState === 'saved-pending'; + const apiKeyConfigured = credentialState !== 'empty'; const tail = composio.apiKeyTail?.trim(); return ( @@ -1725,7 +1753,7 @@ function ComposioSection({ Composio API Key - {isSavedState ? ( + {hasSavedKey ? ( {tail ? `Saved · ••••${tail}` : 'Saved'} @@ -1745,7 +1773,7 @@ function ComposioSection({ updateComposio({ apiKey: e.target.value })} aria-describedby="composio-api-key-help" /> @@ -1759,11 +1787,13 @@ function ComposioSection({ - {isSavedState - ? 'Your key stays in the local daemon. Paste a new key above to replace it, or Clear to remove.' - : apiKeyConfigured - ? 'Unsaved changes — click Save to store this key in the local daemon.' - : 'Keys are stored locally in the daemon and never sent through environment variables.'} + {credentialState === 'saved-pending' + ? 'Unsaved replacement. Click Save to overwrite the saved key, or Clear to discard the draft and the saved key.' + : credentialState === 'saved' + ? 'Your key stays in the local daemon. Paste a new key above to replace it, or Clear to remove.' + : credentialState === 'pending-new' + ? 'Unsaved changes. Click Save to store this key in the local daemon.' + : 'Keys are stored locally in the daemon and never sent through environment variables.'} diff --git a/apps/web/tests/components/SettingsDialog.test.ts b/apps/web/tests/components/SettingsDialog.test.ts index fc1f385444..62f35b7071 100644 --- a/apps/web/tests/components/SettingsDialog.test.ts +++ b/apps/web/tests/components/SettingsDialog.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'; import { agentRefreshOptionsForConfig, canRunProviderConnectionTest, + deriveComposioCredentialState, isValidApiBaseUrl, shouldShowCustomModelInput, switchApiProtocolConfig, @@ -326,3 +327,51 @@ describe('SettingsDialog agent CLI env settings', () => { }); }); }); + +describe('deriveComposioCredentialState', () => { + // Issue #741: when a Composio API key is already saved and the user + // starts typing a draft replacement, the saved-key indicator must + // stay visible. The previous code conflated `saved + draft` with + // `draft only` and made the badge vanish on the first keystroke. + + it('returns "empty" when nothing is configured and the field is empty', () => { + expect(deriveComposioCredentialState({})).toBe('empty'); + expect(deriveComposioCredentialState(null)).toBe('empty'); + expect(deriveComposioCredentialState(undefined)).toBe('empty'); + expect(deriveComposioCredentialState({ apiKey: '' })).toBe('empty'); + expect(deriveComposioCredentialState({ apiKey: ' ' })).toBe('empty'); + }); + + it('returns "saved" when a key is configured and no draft is being typed', () => { + expect( + deriveComposioCredentialState({ apiKeyConfigured: true }), + ).toBe('saved'); + expect( + deriveComposioCredentialState({ apiKey: '', apiKeyConfigured: true }), + ).toBe('saved'); + expect( + deriveComposioCredentialState({ apiKey: ' ', apiKeyConfigured: true }), + ).toBe('saved'); + }); + + it('returns "pending-new" when only a draft is being typed (no saved key)', () => { + expect(deriveComposioCredentialState({ apiKey: 'sk-draft' })).toBe('pending-new'); + expect( + deriveComposioCredentialState({ apiKey: 'sk-draft', apiKeyConfigured: false }), + ).toBe('pending-new'); + }); + + it('returns "saved-pending" when a key is saved AND a draft is being typed', () => { + // Regression: this is the state that previously masqueraded as + // "pending-new" and made the saved-key badge disappear. + expect( + deriveComposioCredentialState({ apiKey: 'sk-replacement', apiKeyConfigured: true }), + ).toBe('saved-pending'); + }); + + it('treats whitespace-only drafts as no draft so the badge is anchored on "saved"', () => { + expect( + deriveComposioCredentialState({ apiKey: ' \t\n', apiKeyConfigured: true }), + ).toBe('saved'); + }); +});