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 { AgentConfigureRightPanelMode } from './state'
import { useSuspenseQuery } from '@tanstack/react-query'
import { useAtomValue, useSetAtom } from 'jotai'
import { ScopeProvider } from 'jotai-scope'
import { parseAsStringLiteral, useQueryState } from 'nuqs'
import { useCallback } from 'react'
import { systemFeaturesQueryOptions } from '@/features/system-features/client'
import { AgentConfigureComposerScope } from './components/composer-session'
import { useAgentConfigureData } from './hooks'
import {
  AGENT_CONFIGURE_RIGHT_PANEL_MODES,
  agentConfigureComposerRebaseRevisionAtom,
  agentConfigureScopedAtoms,
  agentConfigureSelectedVersionIdAtom,
  agentConfigureSelectVersionAtom,
  rebaseAgentConfigureComposerAtom,
} from './state'

const agentConfigureModeQueryParser = parseAsStringLiteral(AGENT_CONFIGURE_RIGHT_PANEL_MODES)
  .withDefault('build')
  .withOptions({ history: 'replace' })

type AgentConfigurePageProps = {
  agentId: string
}

export function AgentConfigurePage({ agentId }: AgentConfigurePageProps) {
  return (
    <ScopeProvider key={agentId} atoms={agentConfigureScopedAtoms} name="AgentConfigure">
      <AgentConfigurePageContent agentId={agentId} />
    </ScopeProvider>
  )
}

function AgentConfigurePageContent({ agentId }: AgentConfigurePageProps) {
  const [requestedMode, setRequestedMode] = useQueryState('mode', agentConfigureModeQueryParser)
  const selectedVersionId = useAtomValue(agentConfigureSelectedVersionIdAtom)
  const composerRebaseRevision = useAtomValue(agentConfigureComposerRebaseRevisionAtom)
  const rebaseComposer = useSetAtom(rebaseAgentConfigureComposerAtom)
  const selectVersion = useSetAtom(agentConfigureSelectVersionAtom)
  const configureData = useAgentConfigureData(agentId, selectedVersionId)
  const { data: deploymentEdition } = useSuspenseQuery({
    ...systemFeaturesQueryOptions(),
    select: (systemFeatures) => systemFeatures.deployment_edition,
  })
  const { canBuild, canTestAndRun } = configureData.capabilities
  const previewEnabled = canTestAndRun && deploymentEdition !== 'COMMUNITY'
  const rightPanelMode =
    !canBuild || (requestedMode === 'preview' && previewEnabled) ? 'preview' : 'build'
  const changeRightPanelMode = useCallback(
    (nextMode: AgentConfigureRightPanelMode) => {
      if (nextMode === 'preview' && !previewEnabled) return
      if (nextMode === 'build' && !canBuild) return

      return setRequestedMode(nextMode)
    },
    [canBuild, previewEnabled, setRequestedMode],
  )

  return (
    <AgentConfigureComposerScope
      agentId={agentId}
      composerRebaseRevision={composerRebaseRevision}
      configureData={configureData}
      previewEnabled={previewEnabled}
      rightPanelMode={rightPanelMode}
      onComposerRebase={rebaseComposer}
      onRightPanelModeChange={changeRightPanelMode}
      onSelectVersion={selectVersion}
    />
  )
}