mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
feat(frontend): replace the mobile voice overlay with an in-row recorder
This commit is contained in:
1 parent
577d7e8b5b
commit
a2ae98107d
5 files changed
+488
-203
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen, fireEvent, act, waitFor } from '@testing-library/react'
|
||||
import { render, screen, fireEvent, act, waitFor, within } from '@testing-library/react'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { PromptInput } from './PromptInput'
|
||||
import { createCommandActionsMock } from '@/test/test-utils'
|
||||
@@ -466,7 +466,7 @@ describe('PromptInput STT Gesture Tests', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('pointer down while recording sets up swipe gesture', async () => {
|
||||
it('pointer down while recording starts a drag gesture', async () => {
|
||||
renderComponent({ isRecording: true })
|
||||
|
||||
const container = getMobileVoiceButtonContainer()
|
||||
@@ -588,4 +588,157 @@ describe('PromptInput STT Gesture Tests', () => {
|
||||
expect(queryMobileVoiceButton()).not.toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('in-row mobile voice recorder', () => {
|
||||
const renderRecorder = (sttOverrides: Partial<MockSTTReturn> = {}) => {
|
||||
const queryClient = createTestQueryClient()
|
||||
const buildTree = (overrides: Partial<MockSTTReturn>) => {
|
||||
mocks.useSTT.mockReturnValue({
|
||||
isRecording: false,
|
||||
isProcessing: false,
|
||||
isSupported: true,
|
||||
isEnabled: true,
|
||||
interimTranscript: '',
|
||||
transcript: '',
|
||||
startRecording: mockStartRecording,
|
||||
stopRecording: mockStopRecording,
|
||||
abortRecording: mockAbortRecording,
|
||||
reset: mockReset,
|
||||
clear: mockClear,
|
||||
...overrides,
|
||||
} as unknown as MockSTTReturn)
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<PromptInput {...defaultProps} onPromptChange={(hasContent) => defaultProps.onPromptChange(hasContent)} />
|
||||
</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
const view = render(buildTree(sttOverrides))
|
||||
return { rerender: (overrides: Partial<MockSTTReturn>) => view.rerender(buildTree(overrides)) }
|
||||
}
|
||||
|
||||
const getControlsRow = () => screen.getByTestId('composer-controls-row')
|
||||
|
||||
it('swaps the controls row for discard, strip, mic and send while recording', () => {
|
||||
renderComponent({ isRecording: true })
|
||||
|
||||
const row = getControlsRow()
|
||||
|
||||
expect(within(row).getByRole('button', { name: 'Discard recording' })).toBeInTheDocument()
|
||||
expect(within(row).getByRole('button', { name: 'Send recording' })).toBeInTheDocument()
|
||||
expect(within(row).getByText('0:00')).toBeInTheDocument()
|
||||
expect(within(row).getByRole('status')).toHaveTextContent('‹ discard · send ›')
|
||||
})
|
||||
|
||||
it('hides the mobile insert keys and renders nothing above the mic', () => {
|
||||
renderComponent({ isRecording: true })
|
||||
|
||||
fireEvent.focus(screen.getByPlaceholderText('Send a message...'))
|
||||
|
||||
expect(screen.queryByRole('toolbar', { name: 'Insert' })).not.toBeInTheDocument()
|
||||
expect(getMobileVoiceButtonContainer().children).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('stops recording and marks the transcript for auto-send when send is tapped', async () => {
|
||||
const view = renderRecorder({ isRecording: true })
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Send recording' }))
|
||||
|
||||
expect(mockStopRecording).toHaveBeenCalledTimes(1)
|
||||
|
||||
view.rerender({ isRecording: false, isProcessing: true })
|
||||
|
||||
expect(screen.getByText('Transcribing & sending…')).toBeInTheDocument()
|
||||
|
||||
view.rerender({ isRecording: false, isProcessing: false, transcript: 'hello there' })
|
||||
|
||||
await waitFor(() => {
|
||||
const [variables] = mocks.useSendPromptMutate.mock.calls[0] as [{ text: string }]
|
||||
expect(variables.text).toBe('hello there')
|
||||
})
|
||||
})
|
||||
|
||||
it('upgrades a transcription in progress to send when send is tapped', async () => {
|
||||
const view = renderRecorder({ isRecording: false, isProcessing: true })
|
||||
|
||||
expect(screen.getByText('Transcribing…')).toBeInTheDocument()
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Send recording' }))
|
||||
|
||||
expect(mockStopRecording).not.toHaveBeenCalled()
|
||||
|
||||
view.rerender({ isRecording: false, isProcessing: false, transcript: 'upgraded text' })
|
||||
|
||||
await waitFor(() => {
|
||||
const [variables] = mocks.useSendPromptMutate.mock.calls[0] as [{ text: string }]
|
||||
expect(variables.text).toBe('upgraded text')
|
||||
})
|
||||
})
|
||||
|
||||
it('aborts recording when discard is tapped', () => {
|
||||
renderComponent({ isRecording: true })
|
||||
mockAbortRecording.mockClear()
|
||||
|
||||
const discardButton = screen.getByRole('button', { name: 'Discard recording' })
|
||||
|
||||
fireEvent.pointerDown(discardButton)
|
||||
fireEvent.click(discardButton)
|
||||
|
||||
expect(mockAbortRecording).toHaveBeenCalledTimes(1)
|
||||
expect(mockStopRecording).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('arms send on a right drag and sends on release', async () => {
|
||||
renderComponent({ isRecording: true })
|
||||
|
||||
const container = getMobileVoiceButtonContainer()
|
||||
|
||||
await act(async () => {
|
||||
fireEvent.pointerDown(container, { clientX: 100 })
|
||||
fireEvent.pointerMove(container, { clientX: 130 })
|
||||
})
|
||||
|
||||
expect(screen.getByText('Release to send')).toBeInTheDocument()
|
||||
expect(mockStopRecording).not.toHaveBeenCalled()
|
||||
|
||||
await act(async () => {
|
||||
fireEvent.pointerUp(container, { clientX: 130 })
|
||||
})
|
||||
|
||||
expect(mockStopRecording).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('arms discard on a left drag past the threshold and aborts on release', async () => {
|
||||
renderComponent({ isRecording: true })
|
||||
mockAbortRecording.mockClear()
|
||||
|
||||
const container = getMobileVoiceButtonContainer()
|
||||
|
||||
await act(async () => {
|
||||
fireEvent.pointerDown(container, { clientX: 200 })
|
||||
fireEvent.pointerMove(container, { clientX: 100 })
|
||||
})
|
||||
|
||||
expect(screen.getByText('Release to discard')).toBeInTheDocument()
|
||||
|
||||
await act(async () => {
|
||||
fireEvent.pointerUp(container, { clientX: 100 })
|
||||
})
|
||||
|
||||
expect(mockAbortRecording).toHaveBeenCalledTimes(1)
|
||||
expect(mockStopRecording).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('leaves the desktop composer without the recorder row', () => {
|
||||
mocks.useMobile.mockReturnValue(false)
|
||||
|
||||
renderComponent({ isRecording: true })
|
||||
|
||||
expect(screen.queryByRole('button', { name: 'Discard recording' })).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('button', { name: 'Send recording' })).not.toBeInTheDocument()
|
||||
expect(screen.queryByText('Release to send')).not.toBeInTheDocument()
|
||||
expect(document.querySelector('[data-voice-variant="desktop"]')).not.toBeNull()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -19,7 +19,7 @@ import { FINE_POINTER_MEDIA_QUERY, useMediaQuery } from '@/hooks/useMediaQuery'
|
||||
|
||||
import { usePermissions } from '@/contexts/EventContext'
|
||||
import { isLocalOpenCodeServer, useOpenCodeServerId } from '@/contexts/OpenCodeServerContext'
|
||||
import { ArrowDown, Upload, X, Mic, MicOff, Target } from 'lucide-react'
|
||||
import { ArrowDown, ArrowUp, Upload, X, Mic, MicOff, Target } from 'lucide-react'
|
||||
|
||||
import { SquareFill } from '@/components/ui/square-fill'
|
||||
import { IconToggleButton, getIconToggleButtonClassName } from '@/components/ui/icon-toggle-button'
|
||||
@@ -28,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 { VoiceStatusOverlay, type VoiceStatusOverlayState } from './VoiceStatusOverlay'
|
||||
import { VoiceRecorderStrip, type VoiceRecorderStripState } from './VoiceRecorderStrip'
|
||||
import { PermissionModeToggle } from '@/components/session/PermissionModeToggle'
|
||||
import { SessionSandboxMenu } from '@/components/session/SessionSandboxMenu'
|
||||
import { ComposerToolsMenu } from './ComposerToolsMenu'
|
||||
@@ -104,8 +104,11 @@ const COMPOSER_MODE_STYLES: Record<ComposerMode, { label: string, frame: string,
|
||||
const shiftRanges = (ranges: MatchRange[], offset: number): MatchRange[] =>
|
||||
ranges.map(([start, end]) => [start + offset, end + offset])
|
||||
|
||||
const VOICE_SEND_SWIPE_ARM_THRESHOLD = 24
|
||||
const VOICE_SEND_SWIPE_DISARM_THRESHOLD = 8
|
||||
const VOICE_SEND_DRAG_ARM_THRESHOLD = 24
|
||||
const VOICE_SEND_DRAG_DISARM_THRESHOLD = 8
|
||||
const VOICE_CANCEL_DRAG_ARM_THRESHOLD = 96
|
||||
const VOICE_CANCEL_DRAG_DISARM_THRESHOLD = VOICE_CANCEL_DRAG_ARM_THRESHOLD - 8
|
||||
const VOICE_CLICK_SUPPRESSION_MS = 400
|
||||
type VoiceButtonVariant = 'desktop' | 'mobile'
|
||||
|
||||
|
||||
@@ -156,15 +159,18 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
const [selectedCommandIndex, setSelectedCommandIndex] = useState(0)
|
||||
const [localMode, setLocalMode] = useState<string | null>(null)
|
||||
const [isTogglingRecording, setIsTogglingRecording] = useState(false)
|
||||
const [isVoiceSwipeArmed, setIsVoiceSwipeArmed] = useState(false)
|
||||
const [isVoiceSendArmed, setIsVoiceSendArmed] = useState(false)
|
||||
const [isVoiceCancelArmed, setIsVoiceCancelArmed] = useState(false)
|
||||
const [isVoiceAutoSendPending, setIsVoiceAutoSendPending] = useState(false)
|
||||
const [isVoiceAutoSendWaitingForTranscript, setIsVoiceAutoSendWaitingForTranscript] = useState(false)
|
||||
const [voiceRecordingStartedAt, setVoiceRecordingStartedAt] = useState<number | null>(null)
|
||||
const [isPromptFocused, setIsPromptFocused] = useState(false)
|
||||
const promptBlurTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
const pendingCommandSubmitRef = useRef(false)
|
||||
const lastAddedTranscriptRef = useRef('')
|
||||
const voiceGestureStartYRef = useRef<number | null>(null)
|
||||
const voiceSwipeArmedRef = useRef(false)
|
||||
const voiceGestureStartXRef = useRef<number | null>(null)
|
||||
const voiceSendArmedRef = useRef(false)
|
||||
const voiceCancelArmedRef = useRef(false)
|
||||
const pendingVoiceAutoSubmitRef = useRef(false)
|
||||
const ignoreVoiceClickUntilRef = useRef(0)
|
||||
const voiceStartRequestRef = useRef(0)
|
||||
@@ -189,7 +195,9 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null)
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
const voiceButtonContainerRef = useRef<HTMLDivElement | null>(null)
|
||||
const composerControlsRowRef = useRef<HTMLDivElement | null>(null)
|
||||
const hasFinePointer = useMediaQuery(FINE_POINTER_MEDIA_QUERY)
|
||||
const isMobile = useMobile()
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasFinePointer) return
|
||||
@@ -199,11 +207,13 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
}, [hasFinePointer, sessionID])
|
||||
|
||||
const resetVoiceGestureState = useCallback(() => {
|
||||
voiceGestureStartYRef.current = null
|
||||
voiceSwipeArmedRef.current = false
|
||||
voiceGestureStartXRef.current = null
|
||||
voiceSendArmedRef.current = false
|
||||
voiceCancelArmedRef.current = false
|
||||
pendingVoiceAutoSubmitRef.current = false
|
||||
ignoreVoiceClickUntilRef.current = 0
|
||||
setIsVoiceSwipeArmed(false)
|
||||
setIsVoiceSendArmed(false)
|
||||
setIsVoiceCancelArmed(false)
|
||||
setIsVoiceAutoSendPending(false)
|
||||
setIsVoiceAutoSendWaitingForTranscript(false)
|
||||
}, [])
|
||||
@@ -617,8 +627,10 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
pendingVoiceAutoSubmitRef.current = false
|
||||
setIsVoiceAutoSendPending(false)
|
||||
setIsVoiceAutoSendWaitingForTranscript(false)
|
||||
setIsVoiceSwipeArmed(false)
|
||||
voiceSwipeArmedRef.current = false
|
||||
setIsVoiceSendArmed(false)
|
||||
voiceSendArmedRef.current = false
|
||||
setIsVoiceCancelArmed(false)
|
||||
voiceCancelArmedRef.current = false
|
||||
setIsTogglingRecording(true)
|
||||
|
||||
const started = await startRecording()
|
||||
@@ -650,8 +662,10 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
pendingVoiceAutoSubmitRef.current = false
|
||||
setIsVoiceAutoSendPending(false)
|
||||
setIsVoiceAutoSendWaitingForTranscript(false)
|
||||
setIsVoiceSwipeArmed(false)
|
||||
voiceSwipeArmedRef.current = false
|
||||
setIsVoiceSendArmed(false)
|
||||
voiceSendArmedRef.current = false
|
||||
setIsVoiceCancelArmed(false)
|
||||
voiceCancelArmedRef.current = false
|
||||
stopRecording()
|
||||
return
|
||||
}
|
||||
@@ -659,79 +673,6 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
await startVoiceRecording()
|
||||
}
|
||||
|
||||
const handleVoicePointerDown = (event: ReactPointerEvent<HTMLDivElement>) => {
|
||||
if ((event.pointerType === 'mouse' && event.button !== 0) || isProcessing || !isRecording) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
voiceGestureStartYRef.current = event.clientY
|
||||
voiceSwipeArmedRef.current = false
|
||||
setIsVoiceSwipeArmed(false)
|
||||
|
||||
if (event.currentTarget.setPointerCapture) {
|
||||
event.currentTarget.setPointerCapture(event.pointerId)
|
||||
}
|
||||
}
|
||||
|
||||
const handleVoicePointerMove = (event: ReactPointerEvent<HTMLDivElement>) => {
|
||||
if (!isRecording || voiceGestureStartYRef.current === null) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
const deltaY = voiceGestureStartYRef.current - event.clientY
|
||||
const nextIsSwipeArmed = voiceSwipeArmedRef.current
|
||||
? deltaY >= VOICE_SEND_SWIPE_DISARM_THRESHOLD
|
||||
: deltaY >= VOICE_SEND_SWIPE_ARM_THRESHOLD
|
||||
|
||||
if (nextIsSwipeArmed !== voiceSwipeArmedRef.current) {
|
||||
voiceSwipeArmedRef.current = nextIsSwipeArmed
|
||||
setIsVoiceSwipeArmed(nextIsSwipeArmed)
|
||||
|
||||
if (nextIsSwipeArmed && typeof navigator !== 'undefined' && navigator.vibrate) {
|
||||
navigator.vibrate(10)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleVoicePointerEnd = (event: ReactPointerEvent<HTMLDivElement>, canceled = false) => {
|
||||
if (event.currentTarget.releasePointerCapture && event.currentTarget.hasPointerCapture(event.pointerId)) {
|
||||
event.currentTarget.releasePointerCapture(event.pointerId)
|
||||
}
|
||||
|
||||
if (voiceGestureStartYRef.current === null) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
voiceGestureStartYRef.current = null
|
||||
|
||||
if (canceled || !voiceSwipeArmedRef.current) {
|
||||
ignoreVoiceClickUntilRef.current = Date.now() + 400
|
||||
voiceSwipeArmedRef.current = false
|
||||
setIsVoiceSwipeArmed(false)
|
||||
pendingVoiceAutoSubmitRef.current = false
|
||||
setIsVoiceAutoSendPending(false)
|
||||
setIsVoiceAutoSendWaitingForTranscript(false)
|
||||
stopRecording()
|
||||
return
|
||||
}
|
||||
|
||||
ignoreVoiceClickUntilRef.current = Date.now() + 400
|
||||
voiceSwipeArmedRef.current = false
|
||||
setIsVoiceSwipeArmed(false)
|
||||
pendingVoiceAutoSubmitRef.current = true
|
||||
setIsVoiceAutoSendPending(true)
|
||||
setIsVoiceAutoSendWaitingForTranscript(true)
|
||||
|
||||
if (typeof navigator !== 'undefined' && navigator.vibrate) {
|
||||
navigator.vibrate([10, 20, 10])
|
||||
}
|
||||
|
||||
stopRecording()
|
||||
}
|
||||
|
||||
const cancelVoiceInput = useCallback(() => {
|
||||
voiceStartRequestRef.current += 1
|
||||
resetVoiceGestureState()
|
||||
@@ -742,6 +683,115 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
}
|
||||
}, [abortRecording, isProcessing, isRecording, isTogglingRecording, resetVoiceGestureState])
|
||||
|
||||
const clearVoiceGestureArmedState = useCallback(() => {
|
||||
ignoreVoiceClickUntilRef.current = Date.now() + VOICE_CLICK_SUPPRESSION_MS
|
||||
voiceSendArmedRef.current = false
|
||||
voiceCancelArmedRef.current = false
|
||||
setIsVoiceSendArmed(false)
|
||||
setIsVoiceCancelArmed(false)
|
||||
}, [])
|
||||
|
||||
const armVoiceAutoSend = useCallback(() => {
|
||||
pendingVoiceAutoSubmitRef.current = true
|
||||
setIsVoiceAutoSendPending(true)
|
||||
setIsVoiceAutoSendWaitingForTranscript(true)
|
||||
|
||||
if (typeof navigator !== 'undefined' && navigator.vibrate) {
|
||||
navigator.vibrate([10, 20, 10])
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleVoiceSendRequest = useCallback(() => {
|
||||
clearVoiceGestureArmedState()
|
||||
armVoiceAutoSend()
|
||||
|
||||
if (isRecording) {
|
||||
stopRecording()
|
||||
}
|
||||
}, [armVoiceAutoSend, clearVoiceGestureArmedState, isRecording, stopRecording])
|
||||
|
||||
const handleVoicePointerDown = (event: ReactPointerEvent<HTMLDivElement>) => {
|
||||
if ((event.pointerType === 'mouse' && event.button !== 0) || isProcessing || !isRecording) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
voiceGestureStartXRef.current = event.clientX
|
||||
voiceSendArmedRef.current = false
|
||||
voiceCancelArmedRef.current = false
|
||||
setIsVoiceSendArmed(false)
|
||||
setIsVoiceCancelArmed(false)
|
||||
|
||||
if (event.currentTarget.setPointerCapture) {
|
||||
event.currentTarget.setPointerCapture(event.pointerId)
|
||||
}
|
||||
}
|
||||
|
||||
const handleVoicePointerMove = (event: ReactPointerEvent<HTMLDivElement>) => {
|
||||
if (!isRecording || voiceGestureStartXRef.current === null) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
const deltaX = event.clientX - voiceGestureStartXRef.current
|
||||
const nextIsSendArmed = voiceSendArmedRef.current
|
||||
? deltaX >= VOICE_SEND_DRAG_DISARM_THRESHOLD
|
||||
: deltaX >= VOICE_SEND_DRAG_ARM_THRESHOLD
|
||||
const nextIsCancelArmed = !nextIsSendArmed && (voiceCancelArmedRef.current
|
||||
? deltaX <= -VOICE_CANCEL_DRAG_DISARM_THRESHOLD
|
||||
: deltaX <= -VOICE_CANCEL_DRAG_ARM_THRESHOLD)
|
||||
|
||||
if (nextIsSendArmed !== voiceSendArmedRef.current) {
|
||||
voiceSendArmedRef.current = nextIsSendArmed
|
||||
setIsVoiceSendArmed(nextIsSendArmed)
|
||||
|
||||
if (nextIsSendArmed && typeof navigator !== 'undefined' && navigator.vibrate) {
|
||||
navigator.vibrate(10)
|
||||
}
|
||||
}
|
||||
|
||||
if (nextIsCancelArmed !== voiceCancelArmedRef.current) {
|
||||
voiceCancelArmedRef.current = nextIsCancelArmed
|
||||
setIsVoiceCancelArmed(nextIsCancelArmed)
|
||||
|
||||
if (nextIsCancelArmed && typeof navigator !== 'undefined' && navigator.vibrate) {
|
||||
navigator.vibrate(10)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleVoicePointerEnd = (event: ReactPointerEvent<HTMLDivElement>, canceled = false) => {
|
||||
if (event.currentTarget.releasePointerCapture && event.currentTarget.hasPointerCapture(event.pointerId)) {
|
||||
event.currentTarget.releasePointerCapture(event.pointerId)
|
||||
}
|
||||
|
||||
if (voiceGestureStartXRef.current === null) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
voiceGestureStartXRef.current = null
|
||||
|
||||
const wasSendArmed = voiceSendArmedRef.current
|
||||
const wasCancelArmed = voiceCancelArmedRef.current
|
||||
clearVoiceGestureArmedState()
|
||||
|
||||
if (canceled || (!wasSendArmed && !wasCancelArmed)) {
|
||||
pendingVoiceAutoSubmitRef.current = false
|
||||
setIsVoiceAutoSendPending(false)
|
||||
setIsVoiceAutoSendWaitingForTranscript(false)
|
||||
stopRecording()
|
||||
return
|
||||
}
|
||||
|
||||
if (wasCancelArmed) {
|
||||
cancelVoiceInput()
|
||||
return
|
||||
}
|
||||
|
||||
handleVoiceSendRequest()
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const isVoiceFeedbackVisible = isRecording || isTogglingRecording || isProcessing || isVoiceAutoSendPending
|
||||
|
||||
@@ -760,11 +810,17 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
return
|
||||
}
|
||||
|
||||
if (isMobile && composerControlsRowRef.current?.contains(target)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (isRecording) {
|
||||
voiceGestureStartYRef.current = null
|
||||
voiceSwipeArmedRef.current = false
|
||||
voiceGestureStartXRef.current = null
|
||||
voiceSendArmedRef.current = false
|
||||
voiceCancelArmedRef.current = false
|
||||
pendingVoiceAutoSubmitRef.current = false
|
||||
setIsVoiceSwipeArmed(false)
|
||||
setIsVoiceSendArmed(false)
|
||||
setIsVoiceCancelArmed(false)
|
||||
setIsVoiceAutoSendPending(false)
|
||||
setIsVoiceAutoSendWaitingForTranscript(false)
|
||||
stopRecording()
|
||||
@@ -786,7 +842,7 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
return () => {
|
||||
document.removeEventListener('pointerdown', handleOutsidePointerDown, true)
|
||||
}
|
||||
}, [cancelVoiceInput, isProcessing, isRecording, isTogglingRecording, isVoiceAutoSendPending, stopRecording])
|
||||
}, [cancelVoiceInput, isMobile, isProcessing, isRecording, isTogglingRecording, isVoiceAutoSendPending, stopRecording])
|
||||
|
||||
useEffect(() => {
|
||||
const textToUse = transcript || interimTranscript
|
||||
@@ -821,6 +877,10 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
}
|
||||
}, [isTogglingRecording])
|
||||
|
||||
useEffect(() => {
|
||||
setVoiceRecordingStartedAt(isRecording ? Date.now() : null)
|
||||
}, [isRecording])
|
||||
|
||||
useEffect(() => {
|
||||
if (!pendingVoiceAutoSubmitRef.current || isVoiceAutoSendWaitingForTranscript || isRecording || isProcessing || !prompt.trim()) {
|
||||
return
|
||||
@@ -832,6 +892,14 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
handleSubmitRef.current()
|
||||
}, [prompt, isRecording, isProcessing, isVoiceAutoSendWaitingForTranscript])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isVoiceAutoSendWaitingForTranscript || isRecording || isProcessing || !transcript.trim()) {
|
||||
return
|
||||
}
|
||||
|
||||
setIsVoiceAutoSendWaitingForTranscript(false)
|
||||
}, [isRecording, isProcessing, transcript, isVoiceAutoSendWaitingForTranscript])
|
||||
|
||||
useEffect(() => {
|
||||
if (pendingVoiceAutoSubmitRef.current && !isRecording && !isProcessing && !transcript.trim() && !interimTranscript.trim()) {
|
||||
pendingVoiceAutoSubmitRef.current = false
|
||||
@@ -1255,7 +1323,6 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read)
|
||||
|
||||
return info?.name || model.modelID || currentModel
|
||||
}, [currentModel, model, providersData])
|
||||
const isMobile = useMobile()
|
||||
const { setShowDialog, hasForSession: hasPermissionsForSession } = usePermissions()
|
||||
const hasPendingPermissionForSession = hasPermissionsForSession(sessionID)
|
||||
const { hasVariants, currentVariant, cycleVariant } = useVariants(directory, modelSelectionSession)
|
||||
@@ -1354,34 +1421,25 @@ 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 voiceFeedbackState: VoiceStatusOverlayState | null = isTogglingRecording
|
||||
const voiceFeedbackState: VoiceRecorderStripState | null = isTogglingRecording
|
||||
? 'starting'
|
||||
: isProcessing
|
||||
? isVoiceAutoSendPending
|
||||
? 'sending'
|
||||
: 'processing'
|
||||
: isRecording
|
||||
? isVoiceSwipeArmed
|
||||
? 'readyToSend'
|
||||
: 'recording'
|
||||
? isVoiceCancelArmed
|
||||
? 'cancelArmed'
|
||||
: isVoiceSendArmed
|
||||
? 'readyToSend'
|
||||
: 'recording'
|
||||
: isVoiceAutoSendPending
|
||||
? 'sending'
|
||||
: null
|
||||
const showVoiceFeedback = voiceFeedbackState !== null
|
||||
const mobileVoiceRecorderState = isMobile ? voiceFeedbackState : null
|
||||
const hasPromptContent = prompt.trim().length > 0 || imageAttachments.length > 0
|
||||
const canUseMobileVoice = isMobile && sttEnabled && sttSupported && !hasPendingPermissionForSession
|
||||
const showMobileKeys = isMobile && isPromptFocused && !hasPendingPermissionForSession
|
||||
const voiceFeedbackLabel = voiceFeedbackState === 'starting'
|
||||
? 'Starting microphone...'
|
||||
: voiceFeedbackState === 'sending'
|
||||
? 'Transcribing and sending...'
|
||||
: voiceFeedbackState === 'processing'
|
||||
? 'Transcribing...'
|
||||
: voiceFeedbackState === 'readyToSend'
|
||||
? 'Release to send'
|
||||
: voiceFeedbackState === 'recording'
|
||||
? 'Recording... swipe up to send'
|
||||
: null
|
||||
const showMobileKeys = isMobile && isPromptFocused && !hasPendingPermissionForSession && mobileVoiceRecorderState === null
|
||||
const voiceButtonTitle = voiceFeedbackState === 'starting'
|
||||
? 'Starting microphone'
|
||||
: voiceFeedbackState === 'sending'
|
||||
@@ -1390,9 +1448,22 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read)
|
||||
? 'Transcribing speech'
|
||||
: voiceFeedbackState === 'readyToSend'
|
||||
? 'Release to send'
|
||||
: voiceFeedbackState === 'recording'
|
||||
? 'Tap to transcribe'
|
||||
: 'Tap to speak'
|
||||
: voiceFeedbackState === 'cancelArmed'
|
||||
? 'Release to discard'
|
||||
: voiceFeedbackState === 'recording'
|
||||
? 'Tap to transcribe'
|
||||
: 'Tap to speak'
|
||||
|
||||
const handleSendVoiceRecording = () => {
|
||||
if (isRecording) {
|
||||
handleVoiceSendRequest()
|
||||
return
|
||||
}
|
||||
|
||||
if (isProcessing && !isVoiceAutoSendPending) {
|
||||
armVoiceAutoSend()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1427,10 +1498,6 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read)
|
||||
className={containerClassName}
|
||||
{...voiceGestureHandlers}
|
||||
>
|
||||
{!isDesktop && showVoiceFeedback && (
|
||||
<div aria-hidden="true" className="absolute inset-x-0 bottom-full z-20 h-44 touch-none" />
|
||||
)}
|
||||
<VoiceStatusOverlay show={!isDesktop && showVoiceFeedback} label={voiceFeedbackLabel} state={voiceFeedbackState} />
|
||||
<button
|
||||
type="button"
|
||||
data-voice-variant={variant}
|
||||
@@ -1595,9 +1662,27 @@ return (
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div data-testid="composer-controls-row" className="flex gap-1.5 md:gap-2 items-center justify-between">
|
||||
<div ref={composerControlsRowRef} data-testid="composer-controls-row" className="flex gap-1.5 md:gap-2 items-center justify-between">
|
||||
<div className="flex flex-1 md:flex-initial gap-1.5 md:gap-2 items-center min-w-0">
|
||||
{showMobileKeys ? renderMobileKeys() : (
|
||||
{mobileVoiceRecorderState ? (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Discard recording"
|
||||
title="Discard recording"
|
||||
onMouseDown={(e) => e.preventDefault()}
|
||||
onClick={cancelVoiceInput}
|
||||
className={cn(
|
||||
getIconToggleButtonClassName(false),
|
||||
'flex items-center justify-center size-10 p-0 flex-shrink-0',
|
||||
isVoiceCancelArmed && 'bg-destructive hover:bg-destructive/90 text-destructive-foreground border-destructive',
|
||||
)}
|
||||
>
|
||||
<X className="w-5 h-5" />
|
||||
</button>
|
||||
<VoiceRecorderStrip state={mobileVoiceRecorderState} startedAt={voiceRecordingStartedAt} />
|
||||
</>
|
||||
) : showMobileKeys ? renderMobileKeys() : (
|
||||
<>
|
||||
<AgentQuickSelect
|
||||
directory={directory}
|
||||
@@ -1674,7 +1759,27 @@ return (
|
||||
{canUseMobileVoice && (
|
||||
renderVoiceButton('mobile')
|
||||
)}
|
||||
{showMobileScrollButton ? (
|
||||
{mobileVoiceRecorderState ? (
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Send recording"
|
||||
title="Send recording"
|
||||
disabled={mobileVoiceRecorderState === 'starting'}
|
||||
onMouseDown={(e) => e.preventDefault()}
|
||||
onClick={handleSendVoiceRecording}
|
||||
className={cn(
|
||||
getIconToggleButtonClassName(false),
|
||||
'flex items-center justify-center size-10 p-0 flex-shrink-0',
|
||||
isVoiceSendArmed && 'bg-success hover:bg-success/90 text-success-foreground border-success',
|
||||
)}
|
||||
>
|
||||
{mobileVoiceRecorderState === 'sending' ? (
|
||||
<div className="w-5 h-5 animate-spin rounded-full border-2 border-foreground border-t-transparent" />
|
||||
) : (
|
||||
<ArrowUp className="w-5 h-5" />
|
||||
)}
|
||||
</button>
|
||||
) : showMobileScrollButton ? (
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Scroll to bottom"
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { LoaderCircle } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
export type VoiceRecorderStripState =
|
||||
| 'starting'
|
||||
| 'recording'
|
||||
| 'readyToSend'
|
||||
| 'cancelArmed'
|
||||
| 'processing'
|
||||
| 'sending'
|
||||
|
||||
interface VoiceRecorderStripProps {
|
||||
state: VoiceRecorderStripState
|
||||
startedAt: number | null
|
||||
}
|
||||
|
||||
const WAVEFORM_BAR_COUNT = 5
|
||||
const WAVEFORM_BAR_DELAY_MS = 120
|
||||
|
||||
const STRIP_TONES: Record<VoiceRecorderStripState, string> = {
|
||||
starting: 'border-border bg-muted/50 text-muted-foreground',
|
||||
recording: 'border-border bg-muted/50 text-foreground',
|
||||
readyToSend: 'border-success/60 bg-success/10 text-success',
|
||||
cancelArmed: 'border-destructive/60 bg-destructive/10 text-destructive',
|
||||
processing: 'border-border bg-muted/50 text-muted-foreground',
|
||||
sending: 'border-border bg-muted/50 text-muted-foreground',
|
||||
}
|
||||
|
||||
function WaveformBars() {
|
||||
return (
|
||||
<div aria-hidden="true" className="flex h-4 items-end gap-[2px] text-destructive">
|
||||
{Array.from({ length: WAVEFORM_BAR_COUNT }, (_, index) => (
|
||||
<span
|
||||
key={index}
|
||||
className="animate-voice-wave-bar h-full w-[3px] rounded-full bg-current motion-reduce:animate-none"
|
||||
style={{ animationDelay: `${index * WAVEFORM_BAR_DELAY_MS}ms` }}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function useElapsedLabel(startedAt: number | null): string | null {
|
||||
const [elapsedSeconds, setElapsedSeconds] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
if (startedAt === null) return
|
||||
|
||||
const tick = () => setElapsedSeconds(Math.max(0, Math.floor((Date.now() - startedAt) / 1000)))
|
||||
tick()
|
||||
const interval = setInterval(tick, 1000)
|
||||
|
||||
return () => clearInterval(interval)
|
||||
}, [startedAt])
|
||||
|
||||
if (startedAt === null) return null
|
||||
|
||||
return `${Math.floor(elapsedSeconds / 60)}:${String(elapsedSeconds % 60).padStart(2, '0')}`
|
||||
}
|
||||
|
||||
export function VoiceRecorderStrip({ state, startedAt }: VoiceRecorderStripProps) {
|
||||
const elapsedLabel = useElapsedLabel(state === 'recording' ? startedAt : null)
|
||||
|
||||
return (
|
||||
<div
|
||||
role="status"
|
||||
aria-live="polite"
|
||||
className={cn(
|
||||
'flex min-h-10 min-w-0 flex-1 items-center gap-2 rounded-lg border px-2.5 text-xs font-medium',
|
||||
STRIP_TONES[state],
|
||||
)}
|
||||
>
|
||||
{state === 'starting' && (
|
||||
<>
|
||||
<LoaderCircle className="size-4 shrink-0 animate-spin" />
|
||||
<span className="truncate">Starting mic…</span>
|
||||
</>
|
||||
)}
|
||||
{state === 'recording' && (
|
||||
<>
|
||||
<span aria-hidden="true" className="size-2 shrink-0 animate-pulse rounded-full bg-destructive" />
|
||||
<span aria-hidden="true" className="shrink-0 font-mono tabular-nums">{elapsedLabel}</span>
|
||||
<WaveformBars />
|
||||
<span className="ml-auto shrink-0 truncate text-[10px] uppercase tracking-wide opacity-60">‹ discard · send ›</span>
|
||||
</>
|
||||
)}
|
||||
{state === 'readyToSend' && <span className="truncate">Release to send</span>}
|
||||
{state === 'cancelArmed' && <span className="truncate">Release to discard</span>}
|
||||
{state === 'processing' && (
|
||||
<>
|
||||
<LoaderCircle className="size-4 shrink-0 animate-spin" />
|
||||
<span className="truncate">Transcribing…</span>
|
||||
</>
|
||||
)}
|
||||
{state === 'sending' && (
|
||||
<>
|
||||
<LoaderCircle className="size-4 shrink-0 animate-spin" />
|
||||
<span className="truncate">Transcribing & sending…</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
import { ArrowUp, LoaderCircle, X } from 'lucide-react'
|
||||
|
||||
export type VoiceStatusOverlayState = 'starting' | 'recording' | 'readyToSend' | 'processing' | 'sending'
|
||||
|
||||
interface VoiceStatusOverlayProps {
|
||||
show: boolean
|
||||
label: string | null
|
||||
state: VoiceStatusOverlayState | null
|
||||
}
|
||||
|
||||
function WaveformBars() {
|
||||
return (
|
||||
<div className="flex items-end gap-[3px] h-8">
|
||||
{[0, 1, 2, 3, 4].map((i) => (
|
||||
<div
|
||||
key={i}
|
||||
className="w-1.5 rounded-full bg-success-foreground"
|
||||
style={{
|
||||
height: '100%',
|
||||
animation: `waveBar 0.9s ease-in-out infinite`,
|
||||
animationDelay: `${i * 0.12}s`,
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
<style>{`
|
||||
@keyframes waveBar {
|
||||
0%, 100% { transform: scaleY(0.2); }
|
||||
50% { transform: scaleY(1); }
|
||||
}
|
||||
`}</style>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function VoiceStatusOverlay({ show, label, state }: VoiceStatusOverlayProps) {
|
||||
if (!show || !label || !state) {
|
||||
return null
|
||||
}
|
||||
|
||||
const isLoading = state === 'starting' || state === 'processing' || state === 'sending'
|
||||
const actionWords = state === 'readyToSend'
|
||||
? ['Release', 'To', 'Send']
|
||||
: ['Swipe', 'To', 'Send']
|
||||
|
||||
return (
|
||||
<div
|
||||
aria-live="polite"
|
||||
className="pointer-events-none absolute inset-x-0 bottom-0 z-10"
|
||||
>
|
||||
<span className="sr-only">{label}</span>
|
||||
<div className="animate-voice-overlay-in relative flex h-44 w-full flex-col items-center justify-between overflow-hidden rounded-xl border border-success/70 bg-success px-1 py-4 text-success-foreground shadow-lg shadow-success/40">
|
||||
<div className="absolute inset-x-1 top-1 h-10 rounded-full bg-success-foreground/20 blur-sm" />
|
||||
<div className="relative flex flex-1 flex-col items-center justify-center gap-1">
|
||||
{isLoading ? (
|
||||
state === 'processing' ? (
|
||||
<WaveformBars />
|
||||
) : (
|
||||
<LoaderCircle className="h-6 w-6 animate-spin" />
|
||||
)
|
||||
) : (
|
||||
<>
|
||||
<ArrowUp className="h-8 w-8 animate-bounce" />
|
||||
<div className="flex flex-col items-center text-[9px] font-bold uppercase leading-none tracking-tight">
|
||||
{actionWords.map((word) => (
|
||||
<span key={word}>{word}</span>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
{!isLoading && (
|
||||
<X className="relative h-4 w-4" />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -175,13 +175,13 @@ body {
|
||||
animation: page-slide-in-from-left 300ms cubic-bezier(0.2, 0.9, 0.3, 1) forwards;
|
||||
}
|
||||
|
||||
@keyframes voice-overlay-in {
|
||||
from { transform: translateY(12px) scale(0.97); opacity: 0; }
|
||||
to { transform: translateY(0) scale(1); opacity: 1; }
|
||||
@keyframes voice-wave-bar {
|
||||
0%, 100% { transform: scaleY(0.25); }
|
||||
50% { transform: scaleY(1); }
|
||||
}
|
||||
|
||||
.animate-voice-overlay-in {
|
||||
animation: voice-overlay-in 180ms cubic-bezier(0.2, 0.9, 0.3, 1);
|
||||
.animate-voice-wave-bar {
|
||||
animation: voice-wave-bar 0.9s ease-in-out infinite;
|
||||
}
|
||||
|
||||
@keyframes prompt-takeover-rise {
|
||||
|
||||
Reference in new issue
Block a user