think-block.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React, { useEffect, useRef, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. const hasEndThink = (children: any): boolean => {
  4. if (typeof children === 'string')
  5. return children.includes('[ENDTHINKFLAG]')
  6. if (Array.isArray(children))
  7. return children.some(child => hasEndThink(child))
  8. if (children?.props?.children)
  9. return hasEndThink(children.props.children)
  10. return false
  11. }
  12. const removeEndThink = (children: any): any => {
  13. if (typeof children === 'string')
  14. return children.replace('[ENDTHINKFLAG]', '')
  15. if (Array.isArray(children))
  16. return children.map(child => removeEndThink(child))
  17. if (children?.props?.children) {
  18. return React.cloneElement(
  19. children,
  20. {
  21. ...children.props,
  22. children: removeEndThink(children.props.children),
  23. },
  24. )
  25. }
  26. return children
  27. }
  28. const useThinkTimer = (children: any) => {
  29. const [startTime] = useState(Date.now())
  30. const [elapsedTime, setElapsedTime] = useState(0)
  31. const [isComplete, setIsComplete] = useState(false)
  32. const timerRef = useRef<NodeJS.Timeout>()
  33. useEffect(() => {
  34. timerRef.current = setInterval(() => {
  35. if (!isComplete)
  36. setElapsedTime(Math.floor((Date.now() - startTime) / 100) / 10)
  37. }, 100)
  38. return () => {
  39. if (timerRef.current)
  40. clearInterval(timerRef.current)
  41. }
  42. }, [startTime, isComplete])
  43. useEffect(() => {
  44. if (hasEndThink(children)) {
  45. setIsComplete(true)
  46. if (timerRef.current)
  47. clearInterval(timerRef.current)
  48. }
  49. }, [children])
  50. return { elapsedTime, isComplete }
  51. }
  52. export const ThinkBlock = ({ children, ...props }: any) => {
  53. const { elapsedTime, isComplete } = useThinkTimer(children)
  54. const displayContent = removeEndThink(children)
  55. const { t } = useTranslation()
  56. return (
  57. <details {...(!isComplete && { open: true })} className="group">
  58. <summary className="text-gray-500 font-bold list-none pl-2 flex items-center cursor-pointer select-none whitespace-nowrap">
  59. <div className="flex-shrink-0 flex items-center">
  60. <svg
  61. className="w-3 h-3 mr-2 transform transition-transform duration-500 group-open:rotate-90"
  62. fill="none"
  63. stroke="currentColor"
  64. viewBox="0 0 24 24"
  65. >
  66. <path
  67. strokeLinecap="round"
  68. strokeLinejoin="round"
  69. strokeWidth={2}
  70. d="M9 5l7 7-7 7"
  71. />
  72. </svg>
  73. {isComplete ? `${t('common.chat.thought')}(${elapsedTime.toFixed(1)}s)` : `${t('common.chat.thinking')}(${elapsedTime.toFixed(1)}s)`}
  74. </div>
  75. </summary>
  76. <div className="text-gray-500 p-3 ml-2 bg-gray-50 border-l border-gray-300">
  77. {displayContent}
  78. </div>
  79. </details>
  80. )
  81. }
  82. export default ThinkBlock