-
Notifications
You must be signed in to change notification settings - Fork 1
Final dev #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Final dev #159
Changes from all commits
b11cfe1
944e566
ce4eb36
1b28f2d
c9e29d8
b3eb482
c5720fc
e9d1e11
2ef3469
446b94b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,7 @@ export default function LessonChatbot({ lessonId, courseId }: ChatbotProps) { | |||||
| const [inputValue, setInputValue] = useState(''); | ||||||
| const [isLoading, setIsLoading] = useState(false); | ||||||
| const [pendingChatId, setPendingChatId] = useState<string | null>(null); | ||||||
| const [pollStartTime, setPollStartTime] = useState<number | null>(null); | ||||||
|
|
||||||
| const messagesEndRef = useRef<HTMLDivElement>(null); | ||||||
| const lastActivityRef = useRef<number>(Date.now()); | ||||||
|
|
@@ -73,8 +74,27 @@ export default function LessonChatbot({ lessonId, courseId }: ChatbotProps) { | |||||
| useEffect(() => { | ||||||
| if (!pendingChatId) return; | ||||||
|
|
||||||
| const POLLING_TIMEOUT = 180000; // 3 minutes (longer timeout for when course generation is running) | ||||||
|
|
||||||
| const pollAnswer = async () => { | ||||||
| try { | ||||||
| // Check timeout | ||||||
| if (pollStartTime && Date.now() - pollStartTime > POLLING_TIMEOUT) { | ||||||
| setMessages(prev => [ | ||||||
| ...prev, | ||||||
| { | ||||||
| id: pendingChatId, | ||||||
| role: 'assistant', | ||||||
| content: 'Xin lỗi, hệ thống đang bận xử lý. Câu hỏi này có thể mất vài phút để trả lời. Bạn vui lòng thử lại sau nhé! 🙏', | ||||||
| timestamp: new Date(), | ||||||
| }, | ||||||
| ]); | ||||||
| setPendingChatId(null); | ||||||
| setIsLoading(false); | ||||||
| setPollStartTime(null); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const response = await chatbotApi.getAnswer(pendingChatId); | ||||||
| const data = response.data; | ||||||
|
|
||||||
|
|
@@ -90,6 +110,7 @@ export default function LessonChatbot({ lessonId, courseId }: ChatbotProps) { | |||||
| ]); | ||||||
| setPendingChatId(null); | ||||||
| setIsLoading(false); | ||||||
| setPollStartTime(null); | ||||||
| } else if (data.status === 500) { | ||||||
| const errorMessage = data.message || 'Xin lỗi, mình gặp lỗi khi xử lý câu hỏi của bạn. Bạn có thể thử lại không?'; | ||||||
| setMessages(prev => [ | ||||||
|
|
@@ -103,11 +124,21 @@ export default function LessonChatbot({ lessonId, courseId }: ChatbotProps) { | |||||
| ]); | ||||||
| setPendingChatId(null); | ||||||
| setIsLoading(false); | ||||||
| setPollStartTime(null); | ||||||
| } | ||||||
| // For 202 (processing) or 404 (not yet created), continue polling | ||||||
| } catch (error: unknown) { | ||||||
| // If 404, treat as still processing (Lambda hasn't created record yet) | ||||||
| if (error && typeof error === 'object' && 'response' in error) { | ||||||
| const err = error as { response?: { status?: number } }; | ||||||
| if (err.response?.status === 404) { | ||||||
| return; // Continue polling | ||||||
| } | ||||||
| } | ||||||
| } catch (error) { | ||||||
| console.error('Error polling answer:', error); | ||||||
| setPendingChatId(null); | ||||||
| setIsLoading(false); | ||||||
| setPollStartTime(null); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -116,10 +147,11 @@ export default function LessonChatbot({ lessonId, courseId }: ChatbotProps) { | |||||
|
|
||||||
| return () => { | ||||||
| if (pollingIntervalRef.current) { | ||||||
| setPollStartTime(Date.now()); // Start timeout timer | ||||||
|
||||||
| setPollStartTime(Date.now()); // Start timeout timer | |
| setPollStartTime(Date.now()); // Start timeout timer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pollStartTime is initialized to null but never set to Date.now() when polling actually starts. This means the timeout check on line 82 will never trigger because pollStartTime remains null. You should set setPollStartTime(Date.now()) when polling begins, such as right after calling setPendingChatId in the handleSendMessage function (around line 182).