diff --git a/packages/ui/src/components/prompt-input/usePromptVoiceInput.ts b/packages/ui/src/components/prompt-input/usePromptVoiceInput.ts index 0b8f93f8..6bcc8746 100644 --- a/packages/ui/src/components/prompt-input/usePromptVoiceInput.ts +++ b/packages/ui/src/components/prompt-input/usePromptVoiceInput.ts @@ -169,18 +169,25 @@ export function usePromptVoiceInput(options: UsePromptVoiceInputOptions) { const textarea = options.getTextarea() const start = textarea ? textarea.selectionStart : current.length const end = textarea ? textarea.selectionEnd : current.length + const wasCursorAtEnd = end === current.length + const wasScrolledToBottom = textarea + ? textarea.scrollHeight - (textarea.scrollTop + textarea.clientHeight) <= 4 + : false const before = current.slice(0, start) const after = current.slice(end) - const prefix = before.length > 0 && !/\s$/.test(before) ? " " : "" - const suffix = after.length > 0 && !/^\s/.test(after) ? " " : "" + const prefix = "" + const suffix = after.length > 0 && !after.startsWith("\n") ? "\n" : "" const nextValue = `${before}${prefix}${text}${suffix}${after}` - const cursor = before.length + prefix.length + text.length + const cursor = before.length + prefix.length + text.length + suffix.length options.setPrompt(nextValue) if (textarea) { setTimeout(() => { textarea.focus() textarea.setSelectionRange(cursor, cursor) + if (wasCursorAtEnd || wasScrolledToBottom) { + textarea.scrollTop = textarea.scrollHeight + } }, 0) } }