@@ -89,6 +89,7 @@ import type {
8989 FileAttachmentForApi ,
9090 MothershipResource ,
9191 MothershipResourceType ,
92+ QueuedMessage ,
9293 WorkspaceResourceRef ,
9394} from './types'
9495
@@ -193,7 +194,7 @@ export function Home({ chatId, userName, userId }: HomeProps) {
193194 * box and the results never show two different queries.
194195 */
195196 useEffect ( ( ) => {
196- if ( searchQuery && composerMode === 'build' ) void setComposerMode ( 'search' )
197+ if ( searchQuery . trim ( ) && composerMode === 'build' ) void setComposerMode ( 'search' )
197198 } , [ searchQuery , composerMode , setComposerMode ] )
198199 /** The bases an Ask turn is grounded in; read through a ref so a list refresh never rebuilds the submit handler. */
199200 const { data : knowledgeBases = EMPTY_KNOWLEDGE_BASES } = useKnowledgeBasesQuery ( workspaceId )
@@ -202,6 +203,7 @@ export function Home({ chatId, userName, userId }: HomeProps) {
202203 const hasCheckedLandingStorageRef = useRef ( false )
203204 const initialViewInputRef = useRef < HTMLDivElement > ( null )
204205 const initialViewUserInputRef = useRef < UserInputHandle > ( null )
206+ const chatViewUserInputRef = useRef < UserInputHandle > ( null )
205207
206208 const [ isInputEntering , setIsInputEntering ] = useState ( false )
207209
@@ -499,6 +501,8 @@ export function Home({ chatId, userName, userId }: HomeProps) {
499501 const mode = modeOverride ?? composerMode
500502 const answering = mode === 'assistant'
501503 if ( mode === 'search' ) {
504+ /** A search sends nothing, so an edit in progress is released rather than left waiting. */
505+ if ( editingQueuedId ) cancelQueueEdit ( )
502506 if ( trimmed ) setSearchQuery ( trimmed )
503507 return
504508 }
@@ -525,23 +529,43 @@ export function Home({ chatId, userName, userId }: HomeProps) {
525529 workspaceId ,
526530 chatId ,
527531 composerMode ,
532+ editingQueuedId ,
533+ cancelQueueEdit ,
528534 prepareResourceViewForAgentTurn ,
529535 sendMessage ,
530536 setSearchQuery ,
531537 ]
532538 )
533539
534- /** An emptied search box returns to the sources; nothing else reads the cleared query. */
535- const clearSearch = useCallback ( ( ) => setSearchQuery ( '' ) , [ setSearchQuery ] )
540+ /**
541+ * A queued message re-enters the composer in the mode it was written in: an
542+ * Assistant question edits as an Assistant question, and never as a Search,
543+ * which submits nothing and would leave the edit stranded.
544+ */
545+ const restoreQueuedMode = useCallback (
546+ ( requestMode : QueuedMessage [ 'requestMode' ] ) => {
547+ void setComposerMode ( requestMode === 'ask' ? 'assistant' : 'build' )
548+ } ,
549+ [ setComposerMode ]
550+ )
551+
552+ /** An emptied search box returns to the sources; a send in any other mode has no search to clear. */
553+ const clearSearch = useCallback ( ( ) => {
554+ if ( searchQueryValue !== null ) setSearchQuery ( '' )
555+ } , [ searchQueryValue , setSearchQuery ] )
536556
537557 /**
538558 * Summarize or Answer on a result: switch to Assistant and hand the question
539559 * to it. The submit reads the mode from this render, so it is sent as an
540- * Assistant turn directly rather than waiting for the URL to update.
560+ * Assistant turn directly rather than waiting for the URL to update, and the
561+ * box is emptied as a send empties it, so the query does not linger as a
562+ * draft under the answer.
541563 */
542564 const handleSummarize = ( prompt : string ) => {
543565 void setComposerMode ( 'assistant' )
544566 setSearchQuery ( '' )
567+ initialViewUserInputRef . current ?. clear ( )
568+ chatViewUserInputRef . current ?. clear ( )
545569 handleSubmit ( prompt , undefined , undefined , 'assistant' )
546570 }
547571 const showSearchResults = composerMode === 'search' && searchQuery . trim ( ) . length > 0
@@ -800,6 +824,9 @@ export function Home({ chatId, userName, userId }: HomeProps) {
800824 messages = { messages }
801825 isSending = { isSending }
802826 searchResults = { searchResults }
827+ searchQuery = { searchQuery }
828+ userInputRef = { chatViewUserInputRef }
829+ onRestoreQueuedMode = { restoreQueuedMode }
803830 isReconnecting = { isReconnecting }
804831 isLoading = { showChatSkeleton }
805832 onSubmit = { handleSubmit }
0 commit comments