think-block.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if (!(props['data-think'] ?? false))
  57. return (<details {...props}>{children}</details>)
  58. return (
  59. <details {...(!isComplete && { open: true })} className="group">
  60. <summary className="flex cursor-pointer select-none list-none items-center whitespace-nowrap pl-2 font-bold text-gray-500">
  61. <div className="flex shrink-0 items-center">
  62. <svg
  63. className="mr-2 h-3 w-3 transition-transform duration-500 group-open:rotate-90"
  64. fill="none"
  65. stroke="currentColor"
  66. viewBox="0 0 24 24"
  67. >
  68. <path
  69. strokeLinecap="round"
  70. strokeLinejoin="round"
  71. strokeWidth={2}
  72. d="M9 5l7 7-7 7"
  73. />
  74. </svg>
  75. {isComplete ? `${t('common.chat.thought')}(${elapsedTime.toFixed(1)}s)` : `${t('common.chat.thinking')}(${elapsedTime.toFixed(1)}s)`}
  76. </div>
  77. </summary>
  78. <div className="ml-2 border-l border-gray-300 bg-gray-50 p-3 text-gray-500">
  79. {displayContent}
  80. </div>
  81. </details>
  82. )
  83. }
  84. export default ThinkBlock