diff --git a/frontend/src/components/message/PromptInput.command.test.tsx b/frontend/src/components/message/PromptInput.command.test.tsx index 3c6ab98d..3438631b 100644 --- a/frontend/src/components/message/PromptInput.command.test.tsx +++ b/frontend/src/components/message/PromptInput.command.test.tsx @@ -147,9 +147,6 @@ describe('PromptInput command submission', () => { const defaultProps = { directory: '/test', sessionID: 'test-session', - messages: [], - pending: [], - transcriptStatus: 'idle' as const, showScrollButton: false, isSessionActive: false, isStreamingResponse: false, diff --git a/frontend/src/components/message/PromptInput.composer.test.tsx b/frontend/src/components/message/PromptInput.composer.test.tsx index c44ed1e1..e291a622 100644 --- a/frontend/src/components/message/PromptInput.composer.test.tsx +++ b/frontend/src/components/message/PromptInput.composer.test.tsx @@ -93,9 +93,6 @@ const createTestQueryClient = () => new QueryClient({ const defaultProps = { directory: '/test', sessionID: 'test-session', - messages: [], - pending: [], - transcriptStatus: 'idle' as const, showScrollButton: false, isSessionActive: false, isStreamingResponse: false, diff --git a/frontend/src/components/message/PromptInput.goal.test.tsx b/frontend/src/components/message/PromptInput.goal.test.tsx index addbb86d..da4a8efd 100644 --- a/frontend/src/components/message/PromptInput.goal.test.tsx +++ b/frontend/src/components/message/PromptInput.goal.test.tsx @@ -145,9 +145,6 @@ describe('PromptInput goal mode', () => { const defaultProps = { directory: '/test', sessionID: 'test-session', - messages: [], - pending: [], - transcriptStatus: 'idle' as const, showScrollButton: false, isSessionActive: false, isStreamingResponse: false, diff --git a/frontend/src/components/message/PromptInput.mention.test.tsx b/frontend/src/components/message/PromptInput.mention.test.tsx index d243ff90..fa7ae0ed 100644 --- a/frontend/src/components/message/PromptInput.mention.test.tsx +++ b/frontend/src/components/message/PromptInput.mention.test.tsx @@ -133,9 +133,6 @@ describe('PromptInput agent mention submission', () => { const defaultProps = { directory: '/test', sessionID: 'test-session', - messages: [], - pending: [], - transcriptStatus: 'idle' as const, showScrollButton: false, isSessionActive: false, isStreamingResponse: false, diff --git a/frontend/src/components/message/PromptInput.stt.test.tsx b/frontend/src/components/message/PromptInput.stt.test.tsx index 80d50b3e..ae5078ce 100644 --- a/frontend/src/components/message/PromptInput.stt.test.tsx +++ b/frontend/src/components/message/PromptInput.stt.test.tsx @@ -2,6 +2,10 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' import { render, screen, fireEvent, waitFor } from '@testing-library/react' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { PromptInput } from './PromptInput' +import { + SessionTranscriptContext, + type SessionTranscriptSnapshot, +} from '@/contexts/session-transcript-context' import { createCommandActionsMock, createVoiceConversationControllerMock, @@ -10,6 +14,7 @@ import type { UseVoiceConversationInput, VoiceConversationController, } from '@/hooks/useVoiceConversation' +import type { SessionMessageInfo } from '@opencode-manager/shared/opencode' const createTestQueryClient = () => new QueryClient({ defaultOptions: { @@ -146,9 +151,6 @@ describe('PromptInput voice conversation', () => { const defaultProps = { directory: '/test', sessionID: 'test-session', - messages: [], - pending: [], - transcriptStatus: 'idle' as const, showScrollButton: false, isSessionActive: false, isStreamingResponse: false, @@ -157,17 +159,41 @@ describe('PromptInput voice conversation', () => { onPromptChange: vi.fn(), } - const renderComponent = (overrides: Partial = {}) => { + const createUserMessage = (id: string, text: string): SessionMessageInfo => ({ + id, + type: 'user', + text, + time: { created: 1000 }, + } as SessionMessageInfo) + + const buildSnapshot = (overrides: Partial = {}): SessionTranscriptSnapshot => ({ + messages: [], + pending: [], + status: 'idle', + ...overrides, + }) + + const renderComponent = ( + overrides: Partial = {}, + snapshotOverrides: Partial = {}, + ) => { const queryClient = createTestQueryClient() - const tree = (nextOverrides: Partial) => ( + const tree = ( + nextOverrides: Partial, + snapshot = buildSnapshot(snapshotOverrides), + ) => ( - + + + ) const view = render(tree({})) return { ...view, - rerenderWith: (nextOverrides: Partial) => view.rerender(tree(nextOverrides)), + rerenderWith: (nextOverrides: Partial) => { + view.rerender(tree(nextOverrides, buildSnapshot(snapshotOverrides))) + }, } } @@ -251,6 +277,16 @@ describe('PromptInput voice conversation', () => { }) }) + it('passes the published transcript to the voice conversation', () => { + const messages = [createUserMessage('msg_voice_1', 'hello there')] + + renderComponent({}, { messages, status: 'busy' }) + + expect(mocks.useVoiceConversation).toHaveBeenCalledWith( + expect.objectContaining({ messages, status: 'busy' }), + ) + }) + describe('while a conversation is active', () => { it('replaces the composer with the conversation bar and keeps the draft after exit', () => { const onVoiceConversationActiveChange = vi.fn() @@ -259,14 +295,14 @@ describe('PromptInput voice conversation', () => { fireEvent.change(promptInput(), { target: { value: 'keep this draft' } }) setVoiceController({ isActive: true, phase: 'listening', label: 'Listening' }) - view.rerenderWith({ messages: [] }) + view.rerenderWith({}) expect(screen.queryByPlaceholderText('Send a message...')).toBeNull() expect(screen.getByText('Listening')).toBeInTheDocument() expect(onVoiceConversationActiveChange).toHaveBeenLastCalledWith(true) setVoiceController() - view.rerenderWith({ messages: [] }) + view.rerenderWith({}) expect(promptInput()).toHaveValue('keep this draft') expect(onVoiceConversationActiveChange).toHaveBeenLastCalledWith(false) @@ -279,7 +315,7 @@ describe('PromptInput voice conversation', () => { fireEvent.click(screen.getByRole('button', { name: 'Edit in text box' })) setVoiceController() - view.rerenderWith({ messages: [] }) + view.rerenderWith({}) expect(promptInput()).toHaveValue('dictated words') }) @@ -290,11 +326,11 @@ describe('PromptInput voice conversation', () => { fireEvent.change(promptInput(), { target: { value: 'draft' } }) setVoiceController({ isActive: true, phase: 'listening', canEditInTextBox: true }, 'dictated words') - view.rerenderWith({ messages: [] }) + view.rerenderWith({}) fireEvent.click(screen.getByRole('button', { name: 'Edit in text box' })) setVoiceController() - view.rerenderWith({ messages: [] }) + view.rerenderWith({}) expect(promptInput()).toHaveValue('draft dictated words') }) @@ -305,11 +341,11 @@ describe('PromptInput voice conversation', () => { fireEvent.change(promptInput(), { target: { value: 'draft' } }) setVoiceController({ isActive: true, phase: 'listening', canEditInTextBox: true }, 'dictated words') - view.rerenderWith({ messages: [] }) + view.rerenderWith({}) fireEvent.click(screen.getByRole('button', { name: 'Edit in text box' })) setVoiceController() - view.rerenderWith({ messages: [] }) + view.rerenderWith({}) await waitFor(() => { const input = promptInput() as HTMLTextAreaElement @@ -366,9 +402,6 @@ describe('PromptInput submission', () => { const defaultProps = { directory: '/test', sessionID: 'test-session', - messages: [], - pending: [], - transcriptStatus: 'idle' as const, showScrollButton: false, isSessionActive: false, isStreamingResponse: false, diff --git a/frontend/src/components/message/PromptInput.tsx b/frontend/src/components/message/PromptInput.tsx index 78e8a075..e416b0a7 100644 --- a/frontend/src/components/message/PromptInput.tsx +++ b/frontend/src/components/message/PromptInput.tsx @@ -7,8 +7,6 @@ import { useModelSelection } from '@/hooks/useModelSelection' import { useVariants } from '@/hooks/useVariants' import { useSessionAgent } from '@/hooks/useSessionAgent' import { useSttAvailability } from '@/hooks/useSTT' -import { useVoiceConversation } from '@/hooks/useVoiceConversation' -import { useVoicePauseSetting } from '@/hooks/useVoicePauseSetting' import { useUserBash } from '@/stores/userBashStore' import { useSessionAgentStore } from '@/stores/sessionAgentStore' @@ -30,7 +28,7 @@ import { PromptSuggestions, type PromptSuggestion } from './PromptSuggestions' import { ComposerActivityBar } from './ComposerActivityBar' import { ModelQuickSelect } from '@/components/model/ModelQuickSelect' import { AgentQuickSelect } from '@/components/agent/AgentQuickSelect' -import { VoiceConversationBar } from './VoiceConversationBar' +import { VoiceConversation, type VoiceConversationHandle } from './VoiceConversation' import { PermissionModeToggle } from '@/components/session/PermissionModeToggle' import { SessionSandboxMenu } from '@/components/session/SessionSandboxMenu' import { ComposerToolsMenu } from './ComposerToolsMenu' @@ -47,7 +45,7 @@ import { isOpenSessionGoal } from '@opencode-manager/shared/schemas' import { useProviders } from '@/hooks/useProviders' -import type { CommandInfo, SessionInboxInfo, SessionMessageInfo, VoiceSessionStatus } from '@opencode-manager/shared/opencode' +import type { CommandInfo } from '@opencode-manager/shared/opencode' import type { FileAttachmentInfo, ImageAttachment } from '@/api/types' import { isBuiltinCommand, type CommandActions, type PageCommandActions } from '@/lib/builtinCommands' @@ -120,9 +118,6 @@ export interface PromptInputHandle { interface PromptInputProps { directory?: string sessionID: string - messages: SessionMessageInfo[] - pending: SessionInboxInfo[] - transcriptStatus: VoiceSessionStatus onVoiceConversationActiveChange?: (active: boolean) => void showScrollButton?: boolean isSessionActive?: boolean @@ -135,9 +130,6 @@ interface PromptInputProps { export const PromptInput = memo(forwardRef(function PromptInput({ directory, sessionID, - messages, - pending, - transcriptStatus, onVoiceConversationActiveChange, showScrollButton, isSessionActive = false, @@ -162,6 +154,8 @@ export const PromptInput = memo(forwardRef( const [selectedCommandIndex, setSelectedCommandIndex] = useState(0) const [localMode, setLocalMode] = useState(null) const [isPromptFocused, setIsPromptFocused] = useState(false) + const [isVoiceActive, setIsVoiceActive] = useState(false) + const voiceRef = useRef(null) const promptBlurTimerRef = useRef | null>(null) const pendingCommandSubmitRef = useRef(false) const handleSubmitRef = useRef<() => void>(() => {}) @@ -1093,7 +1087,6 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read) : 'Goal mode: the next message becomes the objective' const isGoalModeDisabled = lockedReason !== null || hasOpenGoal || isBashMode || startGoal.isPending const toggleGoalArmed = () => setIsGoalArmed((value) => !value) - const { pauseMs } = useVoicePauseSetting() const sendVoiceTurn = useCallback( async (text: string, options: { busy: boolean }): Promise => { @@ -1118,22 +1111,14 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read) focusPromptAt(next.length) }, [focusPromptAt]) - const voiceConversation = useVoiceConversation({ - sessionID, - serverId, - messages, - pending, - status: transcriptStatus, - hasPendingApproval: hasPendingPermissionForSession, - pauseMs, - sendTurn: sendVoiceTurn, - interrupt: interruptTurn, - onEditInTextBox: handleVoiceEditInTextBox, - }) + const handleVoiceActiveChange = useCallback((active: boolean) => { + setIsVoiceActive(active) + onVoiceConversationActiveChange?.(active) + }, [onVoiceConversationActiveChange]) - useEffect(() => { - onVoiceConversationActiveChange?.(voiceConversation.isActive) - }, [onVoiceConversationActiveChange, voiceConversation.isActive]) + const handleViewApproval = useCallback(() => { + setShowDialog(true) + }, [setShowDialog]) const hasPromptContent = prompt.trim().length > 0 || imageAttachments.length > 0 const showVoiceMic = sttAvailability.isEnabled && sttAvailability.isSupported && !hasPendingPermissionForSession @@ -1147,7 +1132,7 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read) onMouseDown={(event) => event.preventDefault()} onClick={() => { textareaRef.current?.blur() - voiceConversation.start() + voiceRef.current?.start() }} className={isMobile ? 'min-h-11 min-w-11 px-0 rounded-lg transition-all duration-150 flex items-center justify-center border bg-muted hover:bg-muted-foreground/20 text-muted-foreground hover:text-foreground border-border active:bg-muted-foreground/30 active:scale-95' @@ -1228,13 +1213,19 @@ return ( {composerModeStyle.label} )} - {voiceConversation.isActive ? ( - setShowDialog(true)} - /> - ) : ( + + + {!isVoiceActive && ( <>