Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.0 KB
Raw Blame
import type { ReasoningChunkResponse } from '@/types/workflow'
import { produce } from 'immer'
import { useCallback } from 'react'
import { useWorkflowStore } from '@/app/components/workflow/store'

export const useWorkflowReasoning = () => {
  const workflowStore = useWorkflowStore()

  const handleWorkflowReasoning = useCallback(
    (params: ReasoningChunkResponse) => {
      const {
        data: { reasoning, node_id, is_final },
      } = params
      const { workflowRunningData, setWorkflowRunningData } = workflowStore.getState()

      setWorkflowRunningData(
        produce(workflowRunningData!, (draft) => {
          const reasoningContent = (draft.reasoningContent ||= {})
          // key by LLM node so multiple nodes' reasoning stays separated
          const key = node_id || '_'
          if (reasoning) reasoningContent[key] = (reasoningContent[key] || '') + reasoning
          if (is_final) draft.reasoningFinished = true
        }),
      )
    },
    [workflowStore],
  )

  return {
    handleWorkflowReasoning,
  }
}