dify/web/app/components/workflow/run/hooks.ts

89 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-12-25 16:14:51 +08:00
import {
useCallback,
2024-12-30 10:23:03 +08:00
useRef,
2024-12-25 16:14:51 +08:00
useState,
} from 'react'
import { useBoolean } from 'ahooks'
2024-12-26 15:44:40 +08:00
import type {
AgentLogItemWithChildren,
IterationDurationMap,
NodeTracing,
} from '@/types/workflow'
2024-12-25 16:14:51 +08:00
export const useLogs = () => {
const [showRetryDetail, {
setTrue: setShowRetryDetailTrue,
setFalse: setShowRetryDetailFalse,
}] = useBoolean(false)
const [retryResultList, setRetryResultList] = useState<NodeTracing[]>([])
const handleShowRetryResultList = useCallback((detail: NodeTracing[]) => {
setShowRetryDetailTrue()
setRetryResultList(detail)
}, [setShowRetryDetailTrue, setRetryResultList])
const [showIteratingDetail, {
setTrue: setShowIteratingDetailTrue,
setFalse: setShowIteratingDetailFalse,
}] = useBoolean(false)
const [iterationResultList, setIterationResultList] = useState<NodeTracing[][]>([])
const [iterationResultDurationMap, setIterationResultDurationMap] = useState<IterationDurationMap>({})
const handleShowIterationResultList = useCallback((detail: NodeTracing[][], iterDurationMap: IterationDurationMap) => {
setShowIteratingDetailTrue()
setIterationResultList(detail)
setIterationResultDurationMap(iterDurationMap)
}, [setShowIteratingDetailTrue, setIterationResultList, setIterationResultDurationMap])
2024-12-30 11:14:28 +08:00
const [agentOrToolLogItemStack, setAgentOrToolLogItemStack] = useState<{ id: string; label: string }[]>([])
const agentOrToolLogItemStackRef = useRef(agentOrToolLogItemStack)
2024-12-30 10:23:03 +08:00
const [agentOrToolLogListMap, setAgentOrToolLogListMap] = useState<Record<string, AgentLogItemWithChildren[]>>({})
const agentOrToolLogListMapRef = useRef(agentOrToolLogListMap)
2024-12-30 11:14:28 +08:00
const handleShowAgentOrToolLog = useCallback((detail?: AgentLogItemWithChildren) => {
if (!detail) {
setAgentOrToolLogItemStack([])
agentOrToolLogItemStackRef.current = []
return
}
const { id, label, children } = detail
let currentAgentOrToolLogItemStack = agentOrToolLogItemStackRef.current.slice()
const index = currentAgentOrToolLogItemStack.findIndex(logItem => logItem.id === id)
2024-12-30 10:23:03 +08:00
if (index > -1)
2024-12-30 11:14:28 +08:00
currentAgentOrToolLogItemStack = currentAgentOrToolLogItemStack.slice(0, index + 1)
2024-12-30 10:23:03 +08:00
else
2024-12-30 11:14:28 +08:00
currentAgentOrToolLogItemStack = [...currentAgentOrToolLogItemStack.slice(), { id, label }]
2024-12-30 10:23:03 +08:00
2024-12-30 11:14:28 +08:00
setAgentOrToolLogItemStack(currentAgentOrToolLogItemStack)
agentOrToolLogItemStackRef.current = currentAgentOrToolLogItemStack
2024-12-30 10:23:03 +08:00
if (children) {
setAgentOrToolLogListMap({
...agentOrToolLogListMapRef.current,
[id]: children,
})
}
2024-12-30 11:14:28 +08:00
}, [setAgentOrToolLogItemStack, setAgentOrToolLogListMap])
2024-12-26 10:16:12 +08:00
2024-12-25 16:14:51 +08:00
return {
2024-12-30 11:14:28 +08:00
showSpecialResultPanel: showRetryDetail || showIteratingDetail || !!agentOrToolLogItemStack.length,
2024-12-25 16:14:51 +08:00
showRetryDetail,
setShowRetryDetailTrue,
setShowRetryDetailFalse,
retryResultList,
setRetryResultList,
handleShowRetryResultList,
2024-12-26 10:16:12 +08:00
2024-12-25 16:14:51 +08:00
showIteratingDetail,
setShowIteratingDetailTrue,
setShowIteratingDetailFalse,
iterationResultList,
setIterationResultList,
iterationResultDurationMap,
setIterationResultDurationMap,
handleShowIterationResultList,
2024-12-26 10:16:12 +08:00
2024-12-30 11:14:28 +08:00
agentOrToolLogItemStack,
2024-12-30 10:23:03 +08:00
agentOrToolLogListMap,
handleShowAgentOrToolLog,
2024-12-25 16:14:51 +08:00
}
}