@@ -22,6 +22,7 @@ interface ChatLogicProps {
2222 onToggleFavorite : ( messageId : string ) => void
2323 onModelChangeForMessage : ( messageId : string , newModelId : string ) => Promise < void >
2424 onDeleteMessage : ( messageId : string ) => Promise < void >
25+ onTriggerFollowUpQuestion : ( ) => Promise < void >
2526 } ) => React . ReactNode
2627}
2728
@@ -77,8 +78,7 @@ export default function ChatLogic({
7778 taskType ,
7879 taskContext ,
7980 async ( messageId : string ) => {
80- // 只在普通聊天任务完成后触发自动提问
81- if ( autoQuestionEnabled && taskType === 'chat' ) {
81+ if ( autoQuestionEnabled ) {
8282 try {
8383 setTimeout ( async ( ) => {
8484 await autoQuestion . generateAndSendFollowUpQuestion ( messageId )
@@ -112,6 +112,54 @@ export default function ChatLogic({
112112 sendMessageRef . current = messageOperations . handleSendMessage
113113 } , [ messageOperations . handleSendMessage ] )
114114
115+ // 获取最新的AI消息ID
116+ const getLatestAIMessageId = useCallback ( ( ) => {
117+ if ( ! chat ?. messages ) return null
118+
119+ // 获取当前路径的消息
120+ const currentPath = chat . currentPath || [ ]
121+ let messages : any [ ] = [ ]
122+
123+ if ( currentPath . length > 0 ) {
124+ // 使用当前路径
125+ messages = currentPath
126+ . map ( ( id : string ) => chat . messages . find ( ( msg : any ) => msg . id === id ) )
127+ . filter ( Boolean )
128+ } else {
129+ // 使用消息树获取默认路径
130+ messages = messageTree . getCurrentPathMessages ( )
131+ }
132+
133+ // 从后往前找最新的AI消息
134+ for ( let i = messages . length - 1 ; i >= 0 ; i -- ) {
135+ if ( messages [ i ] . role === 'assistant' ) {
136+ return messages [ i ] . id
137+ }
138+ }
139+
140+ return null
141+ } , [ chat , messageTree ] )
142+
143+ // 立即触发追问
144+ const handleTriggerFollowUpQuestion = useCallback ( async ( ) => {
145+ if ( ! autoQuestionEnabled ) {
146+ console . warn ( '自动追问功能未开启' )
147+ return
148+ }
149+
150+ const latestAIMessageId = getLatestAIMessageId ( )
151+ if ( ! latestAIMessageId ) {
152+ console . warn ( '未找到AI消息,无法触发追问' )
153+ return
154+ }
155+
156+ try {
157+ await autoQuestion . generateAndSendFollowUpQuestion ( latestAIMessageId )
158+ } catch ( error ) {
159+ console . error ( '立即触发追问失败:' , error )
160+ }
161+ } , [ autoQuestionEnabled , getLatestAIMessageId , autoQuestion ] )
162+
115163 const handleModelChange = ( modelId : string ) => {
116164 setSelectedModel ( modelId )
117165 }
@@ -133,6 +181,7 @@ export default function ChatLogic({
133181 onEditAndResendMessage : messageOperations . handleEditAndResendMessage ,
134182 onToggleFavorite : messageOperations . handleToggleFavorite ,
135183 onModelChangeForMessage : messageOperations . handleModelChangeForMessage ,
136- onDeleteMessage : messageOperations . handleDeleteMessage
184+ onDeleteMessage : messageOperations . handleDeleteMessage ,
185+ onTriggerFollowUpQuestion : handleTriggerFollowUpQuestion
137186 } )
138187}
0 commit comments