hooks.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {
  2. useCallback,
  3. useRef,
  4. useState,
  5. } from 'react'
  6. import { useBoolean } from 'ahooks'
  7. import type {
  8. AgentLogItemWithChildren,
  9. IterationDurationMap,
  10. LoopDurationMap,
  11. NodeTracing,
  12. } from '@/types/workflow'
  13. export const useLogs = () => {
  14. const [showRetryDetail, {
  15. setTrue: setShowRetryDetailTrue,
  16. setFalse: setShowRetryDetailFalse,
  17. }] = useBoolean(false)
  18. const [retryResultList, setRetryResultList] = useState<NodeTracing[]>([])
  19. const handleShowRetryResultList = useCallback((detail: NodeTracing[]) => {
  20. setShowRetryDetailTrue()
  21. setRetryResultList(detail)
  22. }, [setShowRetryDetailTrue, setRetryResultList])
  23. const [showIteratingDetail, {
  24. setTrue: setShowIteratingDetailTrue,
  25. setFalse: setShowIteratingDetailFalse,
  26. }] = useBoolean(false)
  27. const [iterationResultList, setIterationResultList] = useState<NodeTracing[][]>([])
  28. const [iterationResultDurationMap, setIterationResultDurationMap] = useState<IterationDurationMap>({})
  29. const handleShowIterationResultList = useCallback((detail: NodeTracing[][], iterDurationMap: IterationDurationMap) => {
  30. setShowIteratingDetailTrue()
  31. setIterationResultList(detail)
  32. setIterationResultDurationMap(iterDurationMap)
  33. }, [setShowIteratingDetailTrue, setIterationResultList, setIterationResultDurationMap])
  34. const [showLoopingDetail, {
  35. setTrue: setShowLoopingDetailTrue,
  36. setFalse: setShowLoopingDetailFalse,
  37. }] = useBoolean(false)
  38. const [loopResultList, setLoopResultList] = useState<NodeTracing[][]>([])
  39. const [loopResultDurationMap, setLoopResultDurationMap] = useState<LoopDurationMap>({})
  40. const handleShowLoopResultList = useCallback((detail: NodeTracing[][], loopDurationMap: LoopDurationMap) => {
  41. setShowLoopingDetailTrue()
  42. setLoopResultList(detail)
  43. setLoopResultDurationMap(loopDurationMap)
  44. }, [setShowLoopingDetailTrue, setLoopResultList, setLoopResultDurationMap])
  45. const [agentOrToolLogItemStack, setAgentOrToolLogItemStack] = useState<AgentLogItemWithChildren[]>([])
  46. const agentOrToolLogItemStackRef = useRef(agentOrToolLogItemStack)
  47. const [agentOrToolLogListMap, setAgentOrToolLogListMap] = useState<Record<string, AgentLogItemWithChildren[]>>({})
  48. const agentOrToolLogListMapRef = useRef(agentOrToolLogListMap)
  49. const handleShowAgentOrToolLog = useCallback((detail?: AgentLogItemWithChildren) => {
  50. if (!detail) {
  51. setAgentOrToolLogItemStack([])
  52. agentOrToolLogItemStackRef.current = []
  53. return
  54. }
  55. const { id, children } = detail
  56. let currentAgentOrToolLogItemStack = agentOrToolLogItemStackRef.current.slice()
  57. const index = currentAgentOrToolLogItemStack.findIndex(logItem => logItem.id === id)
  58. if (index > -1)
  59. currentAgentOrToolLogItemStack = currentAgentOrToolLogItemStack.slice(0, index + 1)
  60. else
  61. currentAgentOrToolLogItemStack = [...currentAgentOrToolLogItemStack.slice(), detail]
  62. setAgentOrToolLogItemStack(currentAgentOrToolLogItemStack)
  63. agentOrToolLogItemStackRef.current = currentAgentOrToolLogItemStack
  64. if (children) {
  65. setAgentOrToolLogListMap({
  66. ...agentOrToolLogListMapRef.current,
  67. [id]: children,
  68. })
  69. }
  70. }, [setAgentOrToolLogItemStack, setAgentOrToolLogListMap])
  71. return {
  72. showSpecialResultPanel: showRetryDetail || showIteratingDetail || showLoopingDetail || !!agentOrToolLogItemStack.length,
  73. showRetryDetail,
  74. setShowRetryDetailTrue,
  75. setShowRetryDetailFalse,
  76. retryResultList,
  77. setRetryResultList,
  78. handleShowRetryResultList,
  79. showIteratingDetail,
  80. setShowIteratingDetailTrue,
  81. setShowIteratingDetailFalse,
  82. iterationResultList,
  83. setIterationResultList,
  84. iterationResultDurationMap,
  85. setIterationResultDurationMap,
  86. handleShowIterationResultList,
  87. showLoopingDetail,
  88. setShowLoopingDetailTrue,
  89. setShowLoopingDetailFalse,
  90. loopResultList,
  91. setLoopResultList,
  92. loopResultDurationMap,
  93. setLoopResultDurationMap,
  94. handleShowLoopResultList,
  95. agentOrToolLogItemStack,
  96. agentOrToolLogListMap,
  97. handleShowAgentOrToolLog,
  98. }
  99. }