66 type AutocompleteState ,
77} from '@algolia/autocomplete-core' ;
88import { useTheme } from '@docsearch/core/useTheme' ;
9+ import type { ChatRequestOptions } from 'ai' ;
910import { DefaultChatTransport , lastAssistantMessageIsCompleteWithToolCalls } from 'ai' ;
1011import type { SearchResponse } from 'algoliasearch/lite' ;
1112import React , { type JSX } from 'react' ;
@@ -22,9 +23,17 @@ import { ScreenState } from './ScreenState';
2223import type { SearchBoxTranslations } from './SearchBox' ;
2324import { SearchBox } from './SearchBox' ;
2425import { createStoredConversations , createStoredSearches } from './stored-searches' ;
25- import type { DocSearchHit , DocSearchState , InternalDocSearchHit , StoredAskAiState , StoredDocSearchHit } from './types' ;
26+ import type {
27+ DocSearchHit ,
28+ DocSearchState ,
29+ InternalDocSearchHit ,
30+ StoredAskAiState ,
31+ StoredDocSearchHit ,
32+ SuggestedQuestionHit ,
33+ } from './types' ;
2634import type { AIMessage , AskAiState } from './types/AskiAi' ;
2735import { useSearchClient } from './useSearchClient' ;
36+ import { useSuggestedQuestions } from './useSuggestedQuestions' ;
2837import { useTouchEvents } from './useTouchEvents' ;
2938import { useTrapFocus } from './useTrapFocus' ;
3039import { groupBy , identity , noop , removeHighlightTags , isModifierEvent , scrollTo as scrollToUtils } from './utils' ;
@@ -341,6 +350,11 @@ export function DocSearchModal({
341350 const askAiConfigurationId = typeof askAi === 'string' ? askAi : askAiConfig ?. assistantId || null ;
342351 const askAiSearchParameters = askAiConfig ?. searchParameters ;
343352 const [ askAiState , setAskAiState ] = React . useState < AskAiState > ( 'initial' ) ;
353+ const suggestedQuestions = useSuggestedQuestions ( {
354+ assistantId : askAiConfigurationId ,
355+ searchClient,
356+ suggestedQuestionsEnabled : askAiConfig ?. suggestedQuestions ,
357+ } ) ;
344358
345359 // Format the `indexes` to be used until `indexName` and `searchParameters` props are fully removed.
346360 const indexes : DocSearchIndex [ ] = [ ] ;
@@ -517,24 +531,35 @@ export function DocSearchModal({
517531 > ( undefined ) ;
518532
519533 const handleSelectAskAiQuestion = React . useCallback (
520- ( toggle : boolean , query : string ) => {
534+ ( toggle : boolean , query : string , suggestedQuestion : SuggestedQuestionHit | undefined = undefined ) => {
521535 if ( toggle && askAiState === 'new-conversation' ) {
522536 // We're starting a new conversation, clear out current messages
523537 setMessages ( [ ] ) ;
524538 setAskAiState ( 'initial' ) ;
525539 }
526540
541+ const messageOptions : ChatRequestOptions = { } ;
542+
543+ if ( suggestedQuestion ) {
544+ messageOptions . body = {
545+ suggestedQuestionId : suggestedQuestion . objectID ,
546+ } ;
547+ }
548+
527549 onAskAiToggle ( toggle ) ;
528550 setStoppedStream ( false ) ;
529- sendMessage ( {
530- role : 'user' ,
531- parts : [
532- {
533- type : 'text' ,
534- text : query ,
535- } ,
536- ] ,
537- } ) ;
551+ sendMessage (
552+ {
553+ role : 'user' ,
554+ parts : [
555+ {
556+ type : 'text' ,
557+ text : query ,
558+ } ,
559+ ] ,
560+ } ,
561+ messageOptions ,
562+ ) ;
538563
539564 if ( dropdownRef . current ) {
540565 // some test environments (like jsdom) don't implement element.scrollTo
@@ -809,6 +834,10 @@ export function DocSearchModal({
809834 setAskAiState ( 'conversation-history' ) ;
810835 } ;
811836
837+ const selectSuggestedQuestion = ( suggestedQuestion : SuggestedQuestionHit ) : void => {
838+ handleSelectAskAiQuestion ( true , suggestedQuestion . question , suggestedQuestion ) ;
839+ } ;
840+
812841 // hide the dropdown on idle and no collections
813842 let showDocsearchDropdown = true ;
814843 const hasCollections = state . collections . some ( ( collection ) => collection . items . length > 0 ) ;
@@ -885,6 +914,8 @@ export function DocSearchModal({
885914 hasCollections = { hasCollections }
886915 askAiState = { askAiState }
887916 selectAskAiQuestion = { handleSelectAskAiQuestion }
917+ suggestedQuestions = { suggestedQuestions }
918+ selectSuggestedQuestion = { selectSuggestedQuestion }
888919 onAskAiToggle = { onAskAiToggle }
889920 onItemClick = { ( item , event ) => {
890921 // if the item is askAI toggle the screen
0 commit comments