diff --git a/src/renderer/src/components/feature-tips/ChatUiHereCornerCard.tsx b/src/renderer/src/components/feature-tips/ChatUiHereCornerCard.tsx index f2595d1a068..7216955c64e 100644 --- a/src/renderer/src/components/feature-tips/ChatUiHereCornerCard.tsx +++ b/src/renderer/src/components/feature-tips/ChatUiHereCornerCard.tsx @@ -61,14 +61,20 @@ export function ChatUiHereCornerCard({ onClose }: { onClose: () => void }): JSX. } focusSwitchedNote.current = event.detail === 0 && document.activeElement === event.currentTarget setState({ stage: 'saving' }) - const saved = await setAgentDefaultSurfaceFromFeatureTip('chat', { confirmWithToast: false }) - if (mountedRef.current) { - setState( - saved - ? { stage: 'switched', focusSettings: focusSwitchedNote.current } - : { stage: 'prompt' } - ) + const result = await setAgentDefaultSurfaceFromFeatureTip('chat', { confirmWithToast: false }) + if (!mountedRef.current) { + return } + if (result === 'reported') { + // The notice already told the user what changed; a second confirmation would contradict it. + onClose() + return + } + setState( + result === 'saved' + ? { stage: 'switched', focusSettings: focusSwitchedNote.current } + : { stage: 'prompt' } + ) } const handleStayInTerminal = (): void => { diff --git a/src/renderer/src/components/feature-tips/chat-ui-here-actions.test.ts b/src/renderer/src/components/feature-tips/chat-ui-here-actions.test.ts new file mode 100644 index 00000000000..437d596c97c --- /dev/null +++ b/src/renderer/src/components/feature-tips/chat-ui-here-actions.test.ts @@ -0,0 +1,44 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { toast } from 'sonner' +import type { GlobalSettings } from '../../../../shared/settings/global-settings-types' +import { getDefaultSettings } from '../../../../shared/orca-profiles/constants' +import { useAppStore } from '@/store' +import { setAgentDefaultSurfaceFromFeatureTip } from './chat-ui-here-actions' + +vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn(), dismiss: vi.fn() } })) +vi.mock('@/diagnostics/telemetry/telemetry', () => ({ track: vi.fn() })) + +function seedSettings(overrides: Partial): void { + const updateSettingsOrThrow = (updates: Partial): Promise => { + const settings = useAppStore.getState().settings + useAppStore.setState({ settings: settings && { ...settings, ...updates } }) + return Promise.resolve() + } + useAppStore.setState({ + settings: { ...getDefaultSettings('/home/test'), ...overrides }, + updateSettingsOrThrow + }) +} + +describe('setAgentDefaultSurfaceFromFeatureTip', () => { + beforeEach(() => { + vi.mocked(toast.success).mockClear() + }) + + it.each([ + { from: 'terminal', to: 'chat', claudeChat: false }, + { from: 'chat', to: 'terminal', claudeChat: true } + ] as const)( + 'names agents kept on their own surface when switching to $to', + async ({ from, to, claudeChat }) => { + seedSettings({ agentDefaultSurface: from, nativeChatProviders: { claude: claudeChat } }) + + await expect(setAgentDefaultSurfaceFromFeatureTip(to)).resolves.toBe('reported') + + expect(toast.success).toHaveBeenCalledTimes(1) + const options = vi.mocked(toast.success).mock.calls[0]?.[1] + expect(String(options?.description)).toContain('Claude') + expect(useAppStore.getState().settings?.agentDefaultSurface).toBe(to) + } + ) +}) diff --git a/src/renderer/src/components/feature-tips/chat-ui-here-actions.ts b/src/renderer/src/components/feature-tips/chat-ui-here-actions.ts index 20beed12043..d6e8e784744 100644 --- a/src/renderer/src/components/feature-tips/chat-ui-here-actions.ts +++ b/src/renderer/src/components/feature-tips/chat-ui-here-actions.ts @@ -4,15 +4,21 @@ import { normalizeAgentDefaultSurface } from '../../../../shared/native-chat/set import { translate } from '@/i18n/i18n' import { useAppStore } from '@/store' import { CHAT_LAUNCH_DEFAULT_SECTION_ID } from '../settings/chat/chat-launch-search' +import { showKeptSurfaceNotice } from '../settings/agents/surfaces/kept-surface-notice' import { trackAgentDefaultSurfaceChanged, trackChatUiFeatureTipAction } from './feature-tip-telemetry' +const KEPT_SURFACE_NOTICE_ID = 'feature-tip-kept-surface' + +/** `saved` leaves confirmation to the caller; `reported` means a notice named the agents kept on their own surface, or a later change superseded this one. */ +export type FeatureTipSurfaceResult = 'failed' | 'saved' | 'reported' + export async function setAgentDefaultSurfaceFromFeatureTip( surface: AgentDefaultSurface, { confirmWithToast = true }: { confirmWithToast?: boolean } = {} -): Promise { +): Promise { const { settings, updateSettingsOrThrow } = useAppStore.getState() const fromSurface = normalizeAgentDefaultSurface(settings?.agentDefaultSurface) try { @@ -28,6 +34,21 @@ export async function setAgentDefaultSurfaceFromFeatureTip( source: 'launch_card', launch_tip_shown: true }) + const latest = useAppStore.getState().settings + // A later write superseded this one, so there is nothing to confirm. + if (!latest || normalizeAgentDefaultSurface(latest.agentDefaultSurface) !== surface) { + return 'reported' + } + // Agents set for themselves still open on the other surface; name them instead of the generic confirmation. + if ( + showKeptSurfaceNotice({ + id: KEPT_SURFACE_NOTICE_ID, + surface, + writer: { fallbackSettings: latest, updateSettings: updateSettingsOrThrow } + }) + ) { + return 'reported' + } if (surface === 'chat' && confirmWithToast) { toast.success( translate( @@ -36,7 +57,7 @@ export async function setAgentDefaultSurfaceFromFeatureTip( ) ) } - return true + return 'saved' } catch { toast.error( surface === 'chat' @@ -49,7 +70,7 @@ export async function setAgentDefaultSurfaceFromFeatureTip( 'Could not switch to terminal. Turn off Chat UI in Settings → Chat UI.' ) ) - return false + return 'failed' } } diff --git a/src/renderer/src/components/settings/agents/surfaces/AgentDefaultSurfaceSection.tsx b/src/renderer/src/components/settings/agents/surfaces/AgentDefaultSurfaceSection.tsx index 61e697ef680..7bc11819c92 100644 --- a/src/renderer/src/components/settings/agents/surfaces/AgentDefaultSurfaceSection.tsx +++ b/src/renderer/src/components/settings/agents/surfaces/AgentDefaultSurfaceSection.tsx @@ -9,9 +9,7 @@ import { LoadingSpinner } from '@/components/LoadingSpinner' import { useImmediateMutation } from '@/hooks/useImmediateMutation' import { useMountedRef } from '@/hooks/useMountedRef' import { motionSafeScrollBehavior } from '@/hooks/usePrefersReducedMotion' -import { translate } from '@/i18n/i18n' -import { useIntlLocale, useTranslate } from '@/i18n/use-translate' -import { getAgentLabel } from '@/lib/agent-catalog/agent-catalog' +import { useTranslate } from '@/i18n/use-translate' import { cn } from '@/lib/class-names' import { useAppStore } from '@/store/index' import { trackAgentDefaultSurfaceChanged } from '../../../feature-tips/feature-tip-telemetry' @@ -23,27 +21,15 @@ import { AGENT_DEFAULT_SURFACE_SECTION_ID, AGENTS_INSTALLED_SECTION_ID } from '. import { SettingsRow } from '../../form-controls/SettingsFormControls' import { SettingsGroup } from '../../section-layout/SettingsGroup' import { Button } from '../../../ui/button' +import { saveAgentDefaultSurface } from '../../../../lib/agent-session/launch/native-chat-provider-choice' import { - agentsKeepingOwnSurface, - followAgentDefaultSurface, - saveAgentDefaultSurface -} from '../../../../lib/agent-session/launch/native-chat-provider-choice' + agentSettingsSaveFailed, + showKeptSurfaceNotice, + type KeptSurfaceWriter +} from './kept-surface-notice' -function saveFailed(): void { - toast.error( - translate('settings.agents.localSaveFailed', 'Agent settings could not be saved. Try again.') - ) -} - -type Writer = { - fallbackSettings: GlobalSettings - updateSettings: (updates: Partial) => void | Promise -} - -/** After the default changes, names the agents set for themselves to the other surface. */ -function useKeptSurfaceNotice(): (surface: AgentDefaultSurface, writer: Writer) => void { - const intlLocale = useIntlLocale() - const t = useTranslate() +/** Settings-owned notice: only while mounted, and dismissed when Settings unmounts. */ +function useKeptSurfaceNotice(): (surface: AgentDefaultSurface, writer: KeptSurfaceWriter) => void { const id = useId() const mounted = useMountedRef() useEffect( @@ -53,53 +39,9 @@ function useKeptSurfaceNotice(): (surface: AgentDefaultSurface, writer: Writer) [id] ) return (surface, writer) => { - const latest = useAppStore.getState().settings ?? writer.fallbackSettings - // A failed or superseded write leaves nothing to report. - if (!mounted.current || normalizeAgentDefaultSurface(latest.agentDefaultSurface) !== surface) { - return + if (mounted.current) { + showKeptSurfaceNotice({ id, surface, writer }) } - const kept = agentsKeepingOwnSurface(latest, surface) - if (kept.length === 0) { - toast.dismiss(id) - return - } - const values = { - agents: new Intl.ListFormat(intlLocale, { type: 'conjunction' }).format( - kept.map((agent) => getAgentLabel(agent)) - ), - count: kept.length - } - const chat = surface === 'chat' - toast.success( - chat - ? t('settings.agents.defaultSurfaceNowChat', 'Agents now open as chats') - : t('settings.agents.defaultSurfaceNowTerminal', 'Agents now open in the terminal'), - { - id, - description: chat - ? t( - 'settings.agents.defaultSurfaceKeptTerminal', - '{{agents}} stay in the terminal, as you set.', - { - ...values, - count: values.count, - defaultValue_one: '{{agents}} stays in the terminal, as you set.' - } - ) - : t('settings.agents.defaultSurfaceKeptChat', '{{agents}} stay as chats, as you set.', { - ...values, - count: values.count, - defaultValue_one: '{{agents}} stays as a chat, as you set.' - }), - action: { - label: chat - ? t('settings.agents.defaultSurfaceSetAllChat', 'Set all to Chat') - : t('settings.agents.defaultSurfaceSetAllTerminal', 'Set all to Terminal'), - onClick: () => - void followAgentDefaultSurface({ agents: kept, surface, ...writer }).catch(saveFailed) - } - } - ) } } @@ -193,7 +135,7 @@ export function AgentDefaultSurfaceSection({ }) notify(next, writer) }, - onError: saveFailed + onError: agentSettingsSaveFailed }) } const showInstalledList = (): void => { diff --git a/src/renderer/src/components/settings/agents/surfaces/kept-surface-notice.ts b/src/renderer/src/components/settings/agents/surfaces/kept-surface-notice.ts new file mode 100644 index 00000000000..2a99191a8c6 --- /dev/null +++ b/src/renderer/src/components/settings/agents/surfaces/kept-surface-notice.ts @@ -0,0 +1,88 @@ +import { toast } from 'sonner' +import type { GlobalSettings } from '../../../../../../shared/settings/global-settings-types' +import type { AgentDefaultSurface } from '../../../../../../shared/native-chat/settings/native-chat-appearance-settings' +import { normalizeAgentDefaultSurface } from '../../../../../../shared/native-chat/settings/native-chat-providers' +import { getIntlLocale, translate } from '@/i18n/i18n' +import { getAgentLabel } from '@/lib/agent-catalog/agent-catalog' +import { useAppStore } from '@/store/index' +import { + agentsKeepingOwnSurface, + followAgentDefaultSurface +} from '../../../../lib/agent-session/launch/native-chat-provider-choice' + +export function agentSettingsSaveFailed(): void { + toast.error( + translate('settings.agents.localSaveFailed', 'Agent settings could not be saved. Try again.') + ) +} + +export type KeptSurfaceWriter = { + fallbackSettings: GlobalSettings + updateSettings: (updates: Partial) => void | Promise +} + +/** After the default changes, names the agents set for themselves to the other surface; returns whether it did. */ +export function showKeptSurfaceNotice({ + id, + surface, + writer +}: { + id: string + surface: AgentDefaultSurface + writer: KeptSurfaceWriter +}): boolean { + const latest = useAppStore.getState().settings ?? writer.fallbackSettings + // A superseded write leaves nothing to report. + if (normalizeAgentDefaultSurface(latest.agentDefaultSurface) !== surface) { + return false + } + const kept = agentsKeepingOwnSurface(latest, surface) + if (kept.length === 0) { + toast.dismiss(id) + return false + } + const values = { + agents: new Intl.ListFormat(getIntlLocale(), { type: 'conjunction' }).format( + kept.map((agent) => getAgentLabel(agent)) + ), + count: kept.length + } + const chat = surface === 'chat' + toast.success( + chat + ? translate('settings.agents.defaultSurfaceNowChat', 'Agents now open as chats') + : translate('settings.agents.defaultSurfaceNowTerminal', 'Agents now open in the terminal'), + { + id, + description: chat + ? translate( + 'settings.agents.defaultSurfaceKeptTerminal', + '{{agents}} stay in the terminal, as you set.', + { + ...values, + count: values.count, + defaultValue_one: '{{agents}} stays in the terminal, as you set.' + } + ) + : translate( + 'settings.agents.defaultSurfaceKeptChat', + '{{agents}} stay as chats, as you set.', + { + ...values, + count: values.count, + defaultValue_one: '{{agents}} stays as a chat, as you set.' + } + ), + action: { + label: chat + ? translate('settings.agents.defaultSurfaceSetAllChat', 'Set all to Chat') + : translate('settings.agents.defaultSurfaceSetAllTerminal', 'Set all to Terminal'), + onClick: () => + void followAgentDefaultSurface({ agents: kept, surface, ...writer }).catch( + agentSettingsSaveFailed + ) + } + } + ) + return true +}