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.

2.8 KB
Raw Blame
'use client'

import type { AgentSoulConfig } from '@dify/contracts/api/console/agent/types.gen'
import type { AgentConfigureSoulSource } from './state'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from '@/app/notifications'
import { consoleQuery } from '@/service/console'

export function usePrepareAgentBuildDraftBeforeRun({
  agentId,
  buildDraftAgentSoulConfig,
  isBuildDraftActive,
  rebaseComposerDraft,
  saveDraft,
  setSoulSourceOverride,
}: {
  agentId?: string
  buildDraftAgentSoulConfig?: AgentSoulConfig
  isBuildDraftActive: boolean
  rebaseComposerDraft?: (agentSoulConfig?: AgentSoulConfig) => void
  saveDraft: () => Promise<unknown>
  setSoulSourceOverride?: (source: AgentConfigureSoulSource) => void
}) {
  const { t } = useTranslation(['common'])
  const queryClient = useQueryClient()
  const buildDraftQueryOptions = consoleQuery.agent.byAgentId.buildDraft.get.queryOptions({
    input: {
      params: {
        agent_id: agentId ?? '',
      },
    },
  })
  const checkoutBuildDraftMutation = useMutation(
    consoleQuery.agent.byAgentId.buildDraft.checkout.post.mutationOptions({
      context: { silent: true },
    }),
  )
  const { mutateAsync: checkoutBuildDraft, isPending: isCheckingOutBuildDraft } =
    checkoutBuildDraftMutation

  const checkoutBuildDraftFromNormalDraft = useCallback(
    async (force: boolean) => {
      if (!agentId) return

      const buildDraft = await checkoutBuildDraft({
        params: {
          agent_id: agentId,
        },
        body: {
          force,
        },
      }).catch((error: unknown) => {
        toast.error(error instanceof Error ? error.message : t(($) => $['api.actionFailed']))
        throw error
      })
      queryClient.setQueryData(buildDraftQueryOptions.queryKey, buildDraft)
      rebaseComposerDraft?.(buildDraft.agent_soul as AgentSoulConfig | undefined)
      setSoulSourceOverride?.('build-draft')
      return buildDraft.agent_soul as AgentSoulConfig | undefined
    },
    [
      agentId,
      buildDraftQueryOptions.queryKey,
      checkoutBuildDraft,
      queryClient,
      rebaseComposerDraft,
      setSoulSourceOverride,
      t,
    ],
  )

  const prepareBuildDraftBeforeRun = useCallback(async () => {
    if (!agentId) return

    await saveDraft()

    if (isBuildDraftActive) return buildDraftAgentSoulConfig

    return checkoutBuildDraftFromNormalDraft(false)
  }, [
    agentId,
    buildDraftAgentSoulConfig,
    checkoutBuildDraftFromNormalDraft,
    isBuildDraftActive,
    saveDraft,
  ])
  const forceCheckoutBuildDraft = useCallback(
    () => checkoutBuildDraftFromNormalDraft(true),
    [checkoutBuildDraftFromNormalDraft],
  )

  return {
    forceCheckoutBuildDraft,
    isCheckingOutBuildDraft,
    prepareBuildDraftBeforeRun,
  }
}