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
'use client'

import { useQuery } from '@tanstack/react-query'
import { useAtomValue } from 'jotai'
import { createContext, use } from 'react'
import { agentComposerFilesAtom } from '@/features/agent-v2/agent-composer/store-modules/files'
import { agentComposerSkillsAtom } from '@/features/agent-v2/agent-composer/store-modules/skills'
import { consoleQuery } from '@/service/console'

export type AgentConfigApiContext = {
  agentId: string
  trialAppId?: string
  draftType?: 'draft' | 'debug_build'
  versionId?: string
  workflow?: {
    appId: string
    nodeId: string
  }
}

const AgentConfigApiContext = createContext<AgentConfigApiContext | null>(null)

export const AgentConfigApiContextProvider = AgentConfigApiContext.Provider

export const useAgentConfigApiContext = () => {
  const context = use(AgentConfigApiContext)
  if (!context) throw new Error('AgentConfigApiContextProvider is required for config-backed UI.')

  return context
}

export const useAgentConfigSkills = () => {
  const apiContext = useAgentConfigApiContext()
  const skills = useAtomValue(agentComposerSkillsAtom)

  return {
    apiContext,
    skills,
  }
}

export const useAgentWorkspaceSkillBindings = () => {
  const { agentId } = useAgentConfigApiContext()
  const { data: enableSkill } = useQuery(
    consoleQuery.features.get.queryOptions({
      select: (features) => features.enable_skill,
    }),
  )

  return useQuery({
    ...consoleQuery.workspaces.current.agents.byAgentId.skills.get.queryOptions({
      input: {
        params: {
          agent_id: agentId,
        },
      },
    }),
    enabled: enableSkill === true,
  })
}

export const useAgentConfigFiles = () => {
  const apiContext = useAgentConfigApiContext()
  const files = useAtomValue(agentComposerFilesAtom)

  return {
    apiContext,
    files,
  }
}