You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

think-block.tsx 2.9KB

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