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.

hooks.ts 3.1KB

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