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.8 KB
Raw Blame
import type {
  AgentCliTool,
  AgentFileNode,
  AgentKnowledgeRetrievalItem,
  AgentSkill,
} from '@/features/agent-v2/agent-composer/form-state'
import { createContext, use, useCallback, useEffect, useRef } from 'react'

export type AgentOrchestrateAddActionKey = 'cli' | 'files' | 'knowledge' | 'skills'

export type AgentOrchestrateAddedItem =
  | AgentCliTool
  | AgentFileNode
  | AgentKnowledgeRetrievalItem
  | AgentSkill

export type AgentOrchestrateAddActionOptions = {
  onAdded?: (items: AgentOrchestrateAddedItem[]) => void
  skillSource?: 'library' | 'upload'
}

export type AgentOrchestrateAddAction = (options?: AgentOrchestrateAddActionOptions) => void

export type AgentOrchestrateAddActions = Partial<
  Record<AgentOrchestrateAddActionKey, AgentOrchestrateAddAction>
>

export type AgentOrchestrateAddActionsContextValue = {
  actions: AgentOrchestrateAddActions
  registerAction: (
    key: AgentOrchestrateAddActionKey,
    action: AgentOrchestrateAddAction,
  ) => () => void
}

export const AgentOrchestrateAddActionsContext =
  createContext<AgentOrchestrateAddActionsContextValue | null>(null)

export function useAgentOrchestrateAddActions() {
  const context = use(AgentOrchestrateAddActionsContext)
  if (!context) return {}

  return context.actions
}

export function useRegisterAgentOrchestrateAddAction(
  key: AgentOrchestrateAddActionKey,
  action: AgentOrchestrateAddAction,
) {
  const context = use(AgentOrchestrateAddActionsContext)
  const registerAction = context?.registerAction
  const actionRef = useRef(action)

  useEffect(() => {
    actionRef.current = action
  }, [action])

  const stableAction = useCallback<AgentOrchestrateAddAction>((options) => {
    actionRef.current(options)
  }, [])

  useEffect(() => {
    if (!registerAction) return

    return registerAction(key, stableAction)
  }, [key, registerAction, stableAction])
}