| 644 | } |
| 645 | |
| 646 | const handleThinkingEvent = (data, duration) => { |
| 647 | if (data && duration === undefined) { |
| 648 | // Still thinking - append content |
| 649 | setIsThinking(true) |
| 650 | setMessages((prevMessages) => { |
| 651 | let allMessages = [...cloneDeep(prevMessages)] |
| 652 | if (allMessages[allMessages.length - 1].type === 'userMessage') return allMessages |
| 653 | const lastMessage = allMessages[allMessages.length - 1] |
| 654 | lastMessage.thinking = (lastMessage.thinking || '') + data |
| 655 | lastMessage.isThinking = true |
| 656 | return allMessages |
| 657 | }) |
| 658 | } else if (data === '' && duration !== undefined) { |
| 659 | // Thinking finished - set duration |
| 660 | setIsThinking(false) |
| 661 | setMessages((prevMessages) => { |
| 662 | let allMessages = [...cloneDeep(prevMessages)] |
| 663 | if (allMessages[allMessages.length - 1].type === 'userMessage') return allMessages |
| 664 | const lastMessage = allMessages[allMessages.length - 1] |
| 665 | lastMessage.thinkingDuration = duration |
| 666 | lastMessage.isThinking = false |
| 667 | return allMessages |
| 668 | }) |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | const finalizeThinking = () => { |
| 673 | // Clean up thinking state if stream ends unexpectedly |