diff --git a/frontend/src/api/repos.ts b/frontend/src/api/repos.ts index b97eddc2..02961efc 100644 --- a/frontend/src/api/repos.ts +++ b/frontend/src/api/repos.ts @@ -136,7 +136,7 @@ export async function switchBranch(id: number, branch: string): Promise { } } -interface GitBranch { +export interface GitBranch { name: string type: 'local' | 'remote' current: boolean diff --git a/frontend/src/components/message/ComposerToolsMenu.test.tsx b/frontend/src/components/message/ComposerToolsMenu.test.tsx new file mode 100644 index 00000000..053d4240 --- /dev/null +++ b/frontend/src/components/message/ComposerToolsMenu.test.tsx @@ -0,0 +1,128 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import { render, screen } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import { ComposerToolsMenu } from './ComposerToolsMenu' +import { useSessionPermissionMode, useSetSessionPermissionMode } from '@/hooks/useSessionPermissionMode' +import type { SessionPermissionModeState } from '@opencode-manager/shared/schemas' + +vi.mock('@/hooks/useSessionPermissionMode') + +const baseState: SessionPermissionModeState = { + sessionId: 'ses_1', + rootSessionId: 'ses_1', + mode: 'ask', + lockedReason: null, +} + +function mockPermissionMode(state: Partial = {}, mutate = vi.fn()) { + vi.mocked(useSessionPermissionMode).mockReturnValue({ + data: { ...baseState, ...state }, + isError: false, + } as ReturnType) + vi.mocked(useSetSessionPermissionMode).mockReturnValue({ + mutate, + isPending: false, + } as unknown as ReturnType) + return mutate +} + +function renderMenu(overrides: Partial> = {}) { + const props = { + sessionID: 'ses_1', + directory: '/repo', + goalArmed: false, + goalDisabled: false, + goalLabel: 'Goal mode: the next message becomes the objective', + onToggleGoal: vi.fn(), + onAttachFile: vi.fn(), + ...overrides, + } + render() + return props +} + +describe('ComposerToolsMenu', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('does not highlight the trigger when no tool is active', () => { + mockPermissionMode() + renderMenu() + + expect(screen.getByRole('button', { name: 'Composer options' })).not.toHaveClass('bg-highlight') + }) + + it('highlights the trigger while permissions accept everything', () => { + mockPermissionMode({ mode: 'auto' }) + renderMenu() + + expect(screen.getByRole('button', { name: 'Composer options' })).toHaveClass('bg-highlight') + }) + + it('highlights the trigger while goal mode is armed', () => { + mockPermissionMode() + renderMenu({ goalArmed: true }) + + expect(screen.getByRole('button', { name: 'Composer options' })).toHaveClass('bg-highlight') + }) + + it('switches permissions to accept everything', async () => { + const user = userEvent.setup() + const mutate = mockPermissionMode() + renderMenu() + + await user.click(screen.getByRole('button', { name: 'Composer options' })) + const item = await screen.findByRole('menuitemcheckbox', { name: /Accept all permissions/ }) + expect(item).toHaveAttribute('aria-checked', 'false') + await user.click(item) + + expect(mutate).toHaveBeenCalledWith({ directory: '/repo', mode: 'auto' }) + }) + + it('shows why permissions cannot be changed', async () => { + const user = userEvent.setup() + mockPermissionMode({ lockedReason: 'child' }) + renderMenu() + + await user.click(screen.getByRole('button', { name: 'Composer options' })) + const item = await screen.findByRole('menuitemcheckbox', { name: /Accept all permissions/ }) + + expect(item).toHaveAttribute('aria-disabled', 'true') + expect(item).toHaveTextContent('Inherited from parent session') + }) + + it('toggles goal mode', async () => { + const user = userEvent.setup() + mockPermissionMode() + const props = renderMenu() + + await user.click(screen.getByRole('button', { name: 'Composer options' })) + await user.click(await screen.findByRole('menuitemcheckbox', { name: /Goal mode/ })) + + expect(props.onToggleGoal).toHaveBeenCalledTimes(1) + }) + + it('shows why goal mode is unavailable', async () => { + const user = userEvent.setup() + mockPermissionMode() + renderMenu({ goalDisabled: true, goalLabel: 'A goal is already active for this session' }) + + await user.click(screen.getByRole('button', { name: 'Composer options' })) + const item = await screen.findByRole('menuitemcheckbox', { name: /Goal mode/ }) + + expect(item).toHaveAttribute('aria-disabled', 'true') + expect(item).toHaveTextContent('A goal is already active for this session') + }) + + it('opens the file picker', async () => { + const user = userEvent.setup() + mockPermissionMode() + const props = renderMenu() + + await user.click(screen.getByRole('button', { name: 'Composer options' })) + await user.click(await screen.findByRole('menuitem', { name: 'Attach image or PDF' })) + + expect(props.onAttachFile).toHaveBeenCalledTimes(1) + }) +}) diff --git a/frontend/src/components/message/ComposerToolsMenu.tsx b/frontend/src/components/message/ComposerToolsMenu.tsx new file mode 100644 index 00000000..fb93b8de --- /dev/null +++ b/frontend/src/components/message/ComposerToolsMenu.tsx @@ -0,0 +1,85 @@ +import type { ReactNode } from 'react' +import { Paperclip, Plus, Shield, ShieldCheck, Target } from 'lucide-react' +import { + DropdownMenu, + DropdownMenuCheckboxItem, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuSeparator, + DropdownMenuTrigger, +} from '@/components/ui/dropdown-menu' +import { getIconToggleButtonClassName } from '@/components/ui/icon-toggle-button' +import { usePermissionModeToggle } from '@/hooks/usePermissionModeToggle' + +interface ComposerToolsMenuProps { + sessionID: string + directory: string + goalArmed: boolean + goalDisabled: boolean + goalLabel: string + onToggleGoal: () => void + onAttachFile: () => void +} + +export function ComposerToolsMenu({ + sessionID, + directory, + goalArmed, + goalDisabled, + goalLabel, + onToggleGoal, + onAttachFile, +}: ComposerToolsMenuProps) { + const permission = usePermissionModeToggle(sessionID, directory) + + return ( + + + + + + + : } + title="Accept all permissions" + detail={permission.disabled ? permission.label : undefined} + /> + + + } + title="Goal mode" + detail={goalDisabled ? goalLabel : undefined} + /> + + + + } title="Attach image or PDF" /> + + + + ) +} + +function ToolMenuItemContent({ icon, title, detail }: { icon: ReactNode; title: string; detail?: string }) { + return ( + + {icon} + + {title} + {detail ? {detail} : null} + + + ) +} diff --git a/frontend/src/components/message/PromptInput.goal.test.tsx b/frontend/src/components/message/PromptInput.goal.test.tsx index 6a23d77d..211494ff 100644 --- a/frontend/src/components/message/PromptInput.goal.test.tsx +++ b/frontend/src/components/message/PromptInput.goal.test.tsx @@ -102,7 +102,19 @@ vi.mock('@/components/model/ModelQuickSelect', () => ({ })) vi.mock('@/components/session/PermissionModeToggle', () => ({ - PermissionModeToggle: () => null, + PermissionModeToggle: () =>
, +})) + +vi.mock('./ComposerToolsMenu', () => ({ + ComposerToolsMenu: ({ goalDisabled, goalLabel, onToggleGoal }: { + goalDisabled: boolean + goalLabel: string + onToggleGoal: () => void + }) => ( + + ), })) vi.mock('@/components/ui/session-status-indicator', () => ({ @@ -318,6 +330,54 @@ describe('PromptInput goal mode', () => { expect(button).toBeDisabled() }) + it('keeps permission and goal toggles inline on desktop', async () => { + stubMatchMedia(true) + renderComponent() + + expect(await screen.findByRole('button', { name: GOAL_BUTTON })).toBeInTheDocument() + expect(screen.getByTestId('permission-mode-toggle')).toBeInTheDocument() + expect(screen.queryByTestId('composer-tools-menu')).not.toBeInTheDocument() + }) + + it('moves permission and goal toggles into the composer options menu on mobile', async () => { + stubMatchMedia(true) + mocks.useMobile.mockReturnValue(true) + renderComponent() + + const menu = await screen.findByTestId('composer-tools-menu') + expect(menu).toHaveAttribute('title', GOAL_BUTTON) + expect(screen.queryByRole('button', { name: GOAL_BUTTON })).not.toBeInTheDocument() + expect(screen.queryByTestId('permission-mode-toggle')).not.toBeInTheDocument() + }) + + it('starts a goal armed from the mobile composer options menu', async () => { + stubMatchMedia(true) + mocks.useMobile.mockReturnValue(true) + renderComponent() + + const input = await screen.findByPlaceholderText('Send a message...') + fireEvent.change(input, { target: { value: 'Ship the feature' } }) + fireEvent.click(screen.getByTestId('composer-tools-menu')) + fireEvent.click(screen.getByTitle('Send')) + + await waitFor(() => expect(mocks.startGoal).toHaveBeenCalledWith({ + sessionId: 'test-session', + directory: '/test', + objective: 'Ship the feature', + })) + }) + + it('passes the goal lock reason to the mobile composer options menu', async () => { + stubMatchMedia(true) + mocks.useMobile.mockReturnValue(true) + mocks.useSessionGoal.mockReturnValue({ data: { status: 'active' } }) + renderComponent() + + const menu = await screen.findByTestId('composer-tools-menu') + expect(menu).toHaveAttribute('title', OPEN_GOAL_BUTTON) + expect(menu).toBeDisabled() + }) + it('disables goal mode with a reason for child sessions', async () => { stubMatchMedia(true) mocks.useSessionPermissionMode.mockReturnValue({ diff --git a/frontend/src/components/message/PromptInput.stt.test.tsx b/frontend/src/components/message/PromptInput.stt.test.tsx index afe22556..f1feacd8 100644 --- a/frontend/src/components/message/PromptInput.stt.test.tsx +++ b/frontend/src/components/message/PromptInput.stt.test.tsx @@ -104,6 +104,10 @@ vi.mock('@/components/session/PermissionModeToggle', () => ({ PermissionModeToggle: () => null, })) +vi.mock('./ComposerToolsMenu', () => ({ + ComposerToolsMenu: () => null, +})) + vi.mock('@/hooks/useSessionGoals', () => ({ useSessionGoal: () => ({ data: undefined }), useStartSessionGoal: () => ({ mutateAsync: vi.fn(), isPending: false }), diff --git a/frontend/src/components/message/PromptInput.tsx b/frontend/src/components/message/PromptInput.tsx index 8a20ec31..3e4da9f4 100644 --- a/frontend/src/components/message/PromptInput.tsx +++ b/frontend/src/components/message/PromptInput.tsx @@ -29,6 +29,7 @@ import { ModelQuickSelect } from '@/components/model/ModelQuickSelect' import { AgentQuickSelect } from '@/components/agent/AgentQuickSelect' import { VoiceStatusOverlay, type VoiceStatusOverlayState } from './VoiceStatusOverlay' import { PermissionModeToggle } from '@/components/session/PermissionModeToggle' +import { ComposerToolsMenu } from './ComposerToolsMenu' import { useSessionGoal, useStartSessionGoal } from '@/hooks/useSessionGoals' import { useSessionPermissionMode } from '@/hooks/useSessionPermissionMode' import { detectMentionTrigger, parsePromptToInput, getFilename, filterAgentsByQuery } from '@/lib/promptParser' @@ -201,6 +202,10 @@ export const PromptInput = memo(forwardRef( clearSubmittedPrompt(pending.prompt, pending.files, pending.images) }, [isStreamingResponse, clearSubmittedPrompt]) + const openFilePicker = useCallback(() => { + fileInputRef.current?.click() + }, []) + useImperativeHandle(ref, () => ({ setPromptValue: (value: string) => { setPrompt(value) @@ -219,13 +224,11 @@ export const PromptInput = memo(forwardRef( } textareaRef.current?.focus() }, - triggerFileUpload: () => { - fileInputRef.current?.click() - }, + triggerFileUpload: openFilePicker, openModelPicker: () => { setIsModelPickerOpen(true) } - }), [imageAttachments, clearSTT, isRecording, abortRecording, resetVoiceGestureState]) + }), [imageAttachments, clearSTT, isRecording, abortRecording, resetVoiceGestureState, openFilePicker]) const sessionAgent = useSessionAgent(sessionID, directory) const currentMode = localMode ?? sessionAgent.agent const setStoredAgent = useSessionAgentStore((s) => s.setAgent) @@ -1187,16 +1190,8 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read) : isGoalArmed ? 'Goal mode armed: the next message becomes the objective' : 'Goal mode: the next message becomes the objective' - const goalModeButton = directory ? ( - setIsGoalArmed((value) => !value)} - > - - - ) : null + const isGoalModeDisabled = lockedReason !== null || hasOpenGoal || isBashMode || startGoal.isPending + const toggleGoalArmed = () => setIsGoalArmed((value) => !value) const voiceFeedbackState: VoiceStatusOverlayState | null = isTogglingRecording ? 'starting' : isProcessing @@ -1320,16 +1315,6 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read) return (
- {showStopButton && ( - - )} -