use-workflow-agent-log.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { useCallback } from 'react'
  2. import produce from 'immer'
  3. import type { AgentLogResponse } from '@/types/workflow'
  4. import { useWorkflowStore } from '@/app/components/workflow/store'
  5. export const useWorkflowAgentLog = () => {
  6. const workflowStore = useWorkflowStore()
  7. const handleWorkflowAgentLog = useCallback((params: AgentLogResponse) => {
  8. const { data } = params
  9. const {
  10. workflowRunningData,
  11. setWorkflowRunningData,
  12. } = workflowStore.getState()
  13. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  14. const currentIndex = draft.tracing!.findIndex(item => item.node_id === data.node_id)
  15. if (currentIndex > -1) {
  16. const current = draft.tracing![currentIndex]
  17. if (current.execution_metadata) {
  18. if (current.execution_metadata.agent_log) {
  19. const currentLogIndex = current.execution_metadata.agent_log.findIndex(log => log.id === data.id)
  20. if (currentLogIndex > -1) {
  21. current.execution_metadata.agent_log[currentLogIndex] = {
  22. ...current.execution_metadata.agent_log[currentLogIndex],
  23. ...data,
  24. }
  25. }
  26. else {
  27. current.execution_metadata.agent_log.push(data)
  28. }
  29. }
  30. else {
  31. current.execution_metadata.agent_log = [data]
  32. }
  33. }
  34. else {
  35. current.execution_metadata = {
  36. agent_log: [data],
  37. } as any
  38. }
  39. }
  40. }))
  41. }, [workflowStore])
  42. return {
  43. handleWorkflowAgentLog,
  44. }
  45. }