@@ -31,6 +31,11 @@ export type AIChatState = {
3131 */
3232 query : string | null ;
3333
34+ /**
35+ * The first query sent to the AI. This is appended to the URL when the AI chat is opened.
36+ */
37+ initialQuery : string | null ;
38+
3439 /**
3540 * Messages in the session.
3641 */
@@ -79,6 +84,7 @@ const globalState = zustand.create<{
7984 followUpSuggestions : [ ] ,
8085 loading : false ,
8186 error : false ,
87+ initialQuery : null ,
8288 } ,
8389 setState : ( fn ) => set ( ( state ) => ( { state : { ...state . state , ...fn ( state . state ) } } ) ) ,
8490 } ;
@@ -102,17 +108,14 @@ export function useAIChatController(): AIChatController {
102108 const trackEvent = useTrackEvent ( ) ;
103109 const [ searchState , setSearchState ] = useSearch ( true ) ;
104110
105- // Track if we've initialized from the URL ask parameter
106- const hasInitializedFromUrlRef = React . useRef < boolean > ( false ) ;
107-
108111 // Open AI chat and sync with search state
109112 const onOpen = React . useCallback ( ( ) => {
110- const { messages } = globalState . getState ( ) . state ;
113+ const { initialQuery } = globalState . getState ( ) . state ;
111114 setState ( ( state ) => ( { ...state , opened : true } ) ) ;
112115
113116 // Update search state to show ask mode with first message or current ask value
114117 setSearchState ( ( prev ) => ( {
115- ask : prev ?. ask ?? messages [ 0 ] ?. query ?? '' ,
118+ ask : prev ?. ask ?? initialQuery ?? '' ,
116119 query : prev ?. query ?? null ,
117120 global : prev ?. global ?? false ,
118121 open : false , // Close search popover when opening chat
@@ -249,11 +252,9 @@ export function useAIChatController(): AIChatController {
249252 followUpSuggestions : [ ] ,
250253 responseId : null ,
251254 error : false ,
255+ initialQuery : null ,
252256 } ) ) ;
253257
254- // Reset initialization flag so URL ask can be processed again
255- hasInitializedFromUrlRef . current = false ;
256-
257258 // Reset ask parameter to empty string (keeps chat open but clears content)
258259 setSearchState ( ( prev ) => ( {
259260 ask : '' ,
@@ -277,25 +278,29 @@ export function useAIChatController(): AIChatController {
277278
278279 // Auto-post the message if ask has content
279280 if ( searchState ?. ask ?. trim ( ) ) {
281+ const trimmedAsk = searchState . ask . trim ( ) ;
282+ const { loading, initialQuery } = globalState . getState ( ) . state ;
283+
280284 // Don't trigger if we're already posting a message
281- const loading = globalState . getState ( ) . state . loading ;
282285 if ( loading ) return ;
283286
284- // Only initialize once from URL
285- if ( hasInitializedFromUrlRef . current ) return ;
287+ // Only initialize once per URL ask value
288+ if ( initialQuery === trimmedAsk ) return ;
286289
287290 // Wait for messageContextRef to be defined before proceeding
288291 if ( ! messageContextRef . current ?. location ) return ;
289292
290- hasInitializedFromUrlRef . current = true ;
291- onPostMessage ( { message : searchState . ask . trim ( ) } ) ;
293+ // Mark this ask value as processed
294+ setState ( ( state ) => ( { ...state , initialQuery : trimmedAsk } ) ) ;
295+ onPostMessage ( { message : trimmedAsk } ) ;
292296 }
293297 } , [
294298 searchState ?. ask ,
295299 searchState ?. query ,
296300 searchState ?. open ,
297301 messageContextRef ,
298302 onOpen ,
303+ setState ,
299304 onPostMessage ,
300305 ] ) ;
301306
0 commit comments