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.

5.1 KB
Raw Blame
'use client'

import type { TFunction } from 'i18next'
import type { AgentOrchestrateAddActionOptions } from '../add-actions-context'
import type { AgentKnowledgeRetrievalItem } from '@/features/agent-v2/agent-composer/form-state'
import { useAtomValue, useSetAtom } from 'jotai'
import { useId, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import {
  agentComposerKnowledgeRetrievalsAtom,
  getVisibleKnowledgeRetrievals,
  removeKnowledgeRetrievalAtom,
} from '@/features/agent-v2/agent-composer/store-modules/knowledge'
import { agentKnowledgeFsEnabledAtom } from '@/features/knowledge-fs-availability/state'
import { useRegisterAgentOrchestrateAddAction } from '../add-actions-context'
import { ConfigureSectionAddButton } from '../common/add-button'
import { ConfigureSectionConfigurableItem } from '../common/configurable-item'
import { ConfigureSectionEmpty } from '../common/empty'
import { ConfigureSection } from '../common/section'
import { useAgentOrchestrateReadOnly } from '../read-only-context'
import { AgentKnowledgeRetrievalDialog } from './dialog'

function KnowledgeRetrievalIcon() {
  return (
    <span className="flex size-5 shrink-0 items-center justify-center rounded-md border-[0.5px] border-divider-subtle bg-util-colors-green-green-500 p-0.75 text-text-primary-on-surface shadow-xs shadow-shadow-shadow-3">
      <span aria-hidden className="i-ri-book-open-line size-3.5" />
    </span>
  )
}

function getKnowledgeRetrievalName(item: AgentKnowledgeRetrievalItem, t: TFunction<['agentV2']>) {
  const nameKey = item.nameKey
  return item.name ?? (nameKey ? t(($) => $[nameKey]) : item.id)
}

function AgentKnowledgeRetrievalRow({
  onDelete,
  onEdit,
  item,
}: {
  onDelete: () => void
  onEdit?: () => void
  item: AgentKnowledgeRetrievalItem
}) {
  const { t } = useTranslation(['agentV2'])
  const itemName = getKnowledgeRetrievalName(item, t)

  return (
    <ConfigureSectionConfigurableItem
      icon={<KnowledgeRetrievalIcon />}
      label={itemName}
      editAriaLabel={t(($) => $['agentDetail.configure.knowledgeRetrieval.edit'], {
        name: itemName,
      })}
      removeAriaLabel={t(($) => $['agentDetail.configure.knowledgeRetrieval.remove'], {
        name: itemName,
      })}
      onEdit={onEdit}
      onRemove={onDelete}
    />
  )
}

export function AgentKnowledgeRetrieval({ workflowNodeId }: { workflowNodeId?: string }) {
  const { t } = useTranslation(['agentV2'])
  const retrievals = useAtomValue(agentComposerKnowledgeRetrievalsAtom)
  const enabled = useAtomValue(agentKnowledgeFsEnabledAtom)
  const readOnly = useAgentOrchestrateReadOnly()
  const setRetrievals = useSetAtom(agentComposerKnowledgeRetrievalsAtom)
  const removeKnowledgeRetrieval = useSetAtom(removeKnowledgeRetrievalAtom)
  const [isDialogOpen, setIsDialogOpen] = useState(false)
  const addOptionsRef = useRef<AgentOrchestrateAddActionOptions | undefined>(undefined)
  const knowledgeRetrievalTip = t(
    ($) => $['agentDetail.configure.knowledgeRetrieval.empty.description'],
  )
  const retrievalListId = useId()
  const addRetrieval = (options?: AgentOrchestrateAddActionOptions) => {
    if (readOnly) return
    addOptionsRef.current = options
    setIsDialogOpen(true)
  }
  const confirmBindings = (next: AgentKnowledgeRetrievalItem[]) => {
    setRetrievals(next)
    setIsDialogOpen(false)
    const added = next.find((item) => !retrievals.some((previous) => previous.id === item.id))
    if (added) addOptionsRef.current?.onAdded?.([added])
    addOptionsRef.current = undefined
  }
  useRegisterAgentOrchestrateAddAction('knowledge', addRetrieval)

  const visibleRetrievals = getVisibleKnowledgeRetrievals(retrievals, enabled)

  return (
    <>
      <ConfigureSection
        label={t(($) => $['agentDetail.configure.knowledgeRetrieval.label'])}
        labelId={`${retrievalListId}-label`}
        panelId={retrievalListId}
        tip={knowledgeRetrievalTip}
        panelContentClassName="flex flex-col gap-1"
        actions={
          !readOnly ? (
            <ConfigureSectionAddButton
              ariaLabel={t(($) => $['agentDetail.configure.knowledgeRetrieval.add'])}
              onClick={() => addRetrieval()}
            />
          ) : undefined
        }
      >
        {visibleRetrievals.length === 0 ? (
          <ConfigureSectionEmpty
            title={t(($) => $['agentDetail.configure.knowledgeRetrieval.empty.title'])}
            description={knowledgeRetrievalTip}
          />
        ) : (
          visibleRetrievals.map((item) => (
            <AgentKnowledgeRetrievalRow
              key={item.id}
              item={item}
              onDelete={() => removeKnowledgeRetrieval(item.id)}
              onEdit={() => addRetrieval()}
            />
          ))
        )}
      </ConfigureSection>
      <AgentKnowledgeRetrievalDialog
        workflowNodeId={workflowNodeId}
        open={isDialogOpen && !readOnly}
        knowledgeFsEnabled={enabled}
        initialBindings={retrievals}
        onConfirm={confirmBindings}
        onOpenChange={(open) => {
          setIsDialogOpen(open)
          if (!open) addOptionsRef.current = undefined
        }}
      />
    </>
  )
}