| 
									
										
										
										
											2024-01-25 12:36:55 +08:00
										 |  |  | import { useState } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { useToastContext } from '@/app/components/base/toast' | 
					
						
							|  |  |  | import { ssePost } from '@/service/base' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const useTextGeneration = () => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const { notify } = useToastContext() | 
					
						
							| 
									
										
										
										
											2024-03-07 10:20:35 +08:00
										 |  |  |   const [isResponding, setIsResponding] = useState(false) | 
					
						
							| 
									
										
										
										
											2024-01-25 12:36:55 +08:00
										 |  |  |   const [completion, setCompletion] = useState('') | 
					
						
							|  |  |  |   const [messageId, setMessageId] = useState<string | null>(null) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleSend = async ( | 
					
						
							|  |  |  |     url: string, | 
					
						
							|  |  |  |     data: any, | 
					
						
							|  |  |  |   ) => { | 
					
						
							| 
									
										
										
										
											2024-03-07 10:20:35 +08:00
										 |  |  |     if (isResponding) { | 
					
						
							| 
									
										
										
										
											2024-01-25 12:36:55 +08:00
										 |  |  |       notify({ type: 'info', message: t('appDebug.errorMessage.waitForResponse') }) | 
					
						
							|  |  |  |       return false | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 10:20:35 +08:00
										 |  |  |     setIsResponding(true) | 
					
						
							| 
									
										
										
										
											2024-01-25 12:36:55 +08:00
										 |  |  |     setCompletion('') | 
					
						
							|  |  |  |     setMessageId('') | 
					
						
							|  |  |  |     let res: string[] = [] | 
					
						
							|  |  |  |     ssePost( | 
					
						
							|  |  |  |       url, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           response_mode: 'streaming', | 
					
						
							|  |  |  |           ...data, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         onData: (data: string, _isFirstMessage: boolean, { messageId }) => { | 
					
						
							|  |  |  |           res.push(data) | 
					
						
							|  |  |  |           setCompletion(res.join('')) | 
					
						
							|  |  |  |           setMessageId(messageId) | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onMessageReplace: (messageReplace) => { | 
					
						
							|  |  |  |           res = [messageReplace.answer] | 
					
						
							|  |  |  |           setCompletion(res.join('')) | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onCompleted() { | 
					
						
							| 
									
										
										
										
											2024-03-07 10:20:35 +08:00
										 |  |  |           setIsResponding(false) | 
					
						
							| 
									
										
										
										
											2024-01-25 12:36:55 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         onError() { | 
					
						
							| 
									
										
										
										
											2024-03-07 10:20:35 +08:00
										 |  |  |           setIsResponding(false) | 
					
						
							| 
									
										
										
										
											2024-01-25 12:36:55 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     return true | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     completion, | 
					
						
							| 
									
										
										
										
											2024-03-07 10:20:35 +08:00
										 |  |  |     isResponding, | 
					
						
							|  |  |  |     setIsResponding, | 
					
						
							| 
									
										
										
										
											2024-01-25 12:36:55 +08:00
										 |  |  |     handleSend, | 
					
						
							|  |  |  |     messageId, | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |