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.

12.3 KB
Raw Blame
'use client'

import type { PostAgentByAgentIdConfigSkillsUploadResponse } from '@dify/contracts/api/console/agent/types.gen'
import type { PostAppsByAppIdAgentConfigSkillsUploadResponse } from '@dify/contracts/api/console/apps/types.gen'
import type { ChangeEvent, DragEvent } from 'react'
import type { AgentConfigApiContext } from '../config-context'
import type { AgentSkill } from '@/features/agent-v2/agent-composer/form-state'
import { Alert, AlertTitle } from '@langgenius/dify-ui/alert'
import { Button } from '@langgenius/dify-ui/button'
import { cn } from '@langgenius/dify-ui/cn'
import {
  Dialog,
  DialogBody,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogHeaderCloseButton,
  DialogTitle,
} from '@langgenius/dify-ui/dialog'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip'
import { useMutation, useQuery } from '@tanstack/react-query'
import { useRef, useState } from 'react'
import { Trans, useTranslation } from 'react-i18next'
import { getFileSizeLimit } from '@/app/components/base/file-uploader/hooks'
import { toast } from '@/app/notifications'
import Link from '@/next/link'
import { consoleQuery } from '@/service/console'
import { formatFileSize } from '@/utils/format'

const skillPackageAccept = '.zip,.skill'
const skillPackageExtensions = ['.zip', '.skill']

const getSkillNameFromFile = (file: File) =>
  file.name.replace(/\.(?:skill|zip)$/iu, '') || file.name

const toUploadedSkill = (
  response:
    | PostAgentByAgentIdConfigSkillsUploadResponse
    | PostAppsByAppIdAgentConfigSkillsUploadResponse,
  file: File,
): AgentSkill => {
  const name = response.skill?.name ?? getSkillNameFromFile(file)

  return {
    description: response.skill?.description ?? undefined,
    fileId: response.skill?.file_id ?? undefined,
    hash: response.skill?.hash ?? undefined,
    id: name,
    mimeType: response.skill?.mime_type ?? undefined,
    name,
    size: response.skill?.size ?? undefined,
  }
}

function isSupportedSkillPackage(file: File) {
  const fileName = file.name.toLowerCase()

  return skillPackageExtensions.some((extension) => fileName.endsWith(extension))
}

function hasDraggedFiles(event: DragEvent<HTMLDivElement>) {
  return Array.from(event.dataTransfer.types).includes('Files')
}

function AgentSkillPackageUploader({
  file,
  onChange,
  showWarning,
}: {
  file?: File
  onChange: (file?: File) => void
  showWarning: boolean
}) {
  const { t } = useTranslation(['agentV2'])
  const { t: tCommon } = useTranslation(['common'])
  const { data: fileUploadConfig } = useQuery(consoleQuery.files.upload.get.queryOptions())
  const { skillSizeLimit } = getFileSizeLimit(fileUploadConfig)
  const fileInputRef = useRef<HTMLInputElement>(null)
  const dragDepthRef = useRef(0)
  const [dragging, setDragging] = useState(false)

  const setUploadFiles = (files: File[]) => {
    const [uploadFile] = files
    if (files.length !== 1 || !uploadFile || !isSupportedSkillPackage(uploadFile)) {
      toast.error(t(($) => $['agentDetail.configure.skills.upload.invalidFile']))
      return
    }

    if (uploadFile.size > skillSizeLimit) {
      toast.error(
        t(($) => $['agentDetail.configure.skills.upload.sizeLimit'], {
          sizeLimit: formatFileSize(skillSizeLimit),
        }),
      )
      return
    }

    onChange(uploadFile)
  }

  const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
    const files = Array.from(event.target.files ?? [])
    event.target.value = ''
    setUploadFiles(files)
  }

  const handleDragEnter = (event: DragEvent<HTMLDivElement>) => {
    if (!hasDraggedFiles(event)) return

    event.preventDefault()
    event.stopPropagation()
    dragDepthRef.current += 1
    setDragging(true)
  }

  const handleDragOver = (event: DragEvent<HTMLDivElement>) => {
    if (!hasDraggedFiles(event)) return

    event.preventDefault()
    event.dataTransfer.dropEffect = 'copy'
  }

  const handleDragLeave = (event: DragEvent<HTMLDivElement>) => {
    if (!hasDraggedFiles(event)) return

    event.preventDefault()
    event.stopPropagation()
    dragDepthRef.current = Math.max(0, dragDepthRef.current - 1)
    if (dragDepthRef.current === 0) setDragging(false)
  }

  const handleDrop = (event: DragEvent<HTMLDivElement>) => {
    if (!hasDraggedFiles(event)) return

    event.preventDefault()
    event.stopPropagation()
    dragDepthRef.current = 0
    setDragging(false)

    setUploadFiles(Array.from(event.dataTransfer.files))
  }

  return (
    <div
      role="group"
      aria-label={t(($) => $['agentDetail.configure.skills.upload.title'])}
      onDragEnter={handleDragEnter}
      onDragOver={handleDragOver}
      onDragLeave={handleDragLeave}
      onDrop={handleDrop}
    >
      <input
        ref={fileInputRef}
        className="hidden"
        type="file"
        accept={skillPackageAccept}
        onChange={handleFileChange}
      />
      {!file && (
        <div
          className={cn(
            'relative flex h-16 items-center rounded-[10px] border border-dashed border-components-dropzone-border bg-components-dropzone-bg text-sm font-normal',
            dragging && 'border-components-dropzone-border-accent bg-components-dropzone-bg-accent',
          )}
        >
          <div className="flex w-full items-center justify-center space-x-2">
            <span aria-hidden className="i-ri-upload-cloud-2-line size-6 text-text-tertiary" />
            <div className="text-text-tertiary">
              {t(($) => $['agentDetail.configure.skills.upload.dropzone'])}
              <button
                type="button"
                className="inline cursor-pointer border-none bg-transparent p-0 pl-1 text-left text-text-accent focus-visible:ring-1 focus-visible:ring-components-input-border-active focus-visible:outline-hidden"
                onClick={() => fileInputRef.current?.click()}
              >
                {t(($) => $['agentDetail.configure.skills.upload.browse'])}
              </button>
            </div>
          </div>
          {dragging && <div className="absolute top-0 left-0 size-full" />}
        </div>
      )}
      {file && (
        <div className="flex items-center rounded-lg border-[0.5px] border-components-panel-border bg-components-panel-on-panel-item-bg shadow-xs hover:bg-components-panel-on-panel-item-bg-hover">
          <div className="flex items-center justify-center p-3">
            <span aria-hidden className="i-ri-file-zip-line size-6 shrink-0 text-text-tertiary" />
          </div>
          <div className="flex min-w-0 grow flex-col items-start gap-0.5 py-1 pr-2">
            <span className="max-w-full min-w-0 truncate text-[12px] leading-4 font-medium text-text-secondary">
              {file.name}
            </span>
            <div className="flex h-3 items-center gap-1 self-stretch text-2xs leading-3 font-medium text-text-tertiary uppercase">
              <span>{t(($) => $['agentDetail.configure.skills.upload.fileType'])}</span>
              <span className="text-text-quaternary">·</span>
              <span>{formatFileSize(file.size)}</span>
            </div>
          </div>
          <div className="flex items-center pr-3">
            <Tooltip>
              <TooltipTrigger
                render={
                  <IconButton
                    aria-label={tCommon(($) => $['operation.remove'])}
                    onClick={() => onChange(undefined)}
                  >
                    <span aria-hidden className="i-ri-delete-bin-line size-4" />
                  </IconButton>
                }
              />
              <TooltipContent>{tCommon(($) => $['operation.remove'])}</TooltipContent>
            </Tooltip>
          </div>
        </div>
      )}
      <p className="mt-2 system-xs-regular text-text-tertiary">
        {t(($) => $['agentDetail.configure.skills.upload.sizeLimit'], {
          sizeLimit: formatFileSize(skillSizeLimit),
        })}
      </p>
      {showWarning && (
        <Alert type="warning" className="mt-2">
          <AlertTitle>
            <ul className="list-disc space-y-1 ps-4">
              <li>
                <Trans
                  i18nKey={($) => $['agentDetail.configure.skills.upload.warning.specification']}
                  ns="agentV2"
                  components={{
                    specificationLink: (
                      <Link
                        href="https://agentskills.io/specification"
                        target="_blank"
                        rel="noopener noreferrer"
                        className="rounded-sm underline focus-visible:ring-2 focus-visible:ring-state-accent-solid focus-visible:outline-hidden"
                      />
                    ),
                  }}
                />
              </li>
              <li>{t(($) => $['agentDetail.configure.skills.upload.warning.files'])}</li>
            </ul>
          </AlertTitle>
        </Alert>
      )}
    </div>
  )
}

export function AgentSkillUploadDialog({
  apiContext,
  onUploaded,
  open,
  onOpenChange,
}: {
  apiContext: AgentConfigApiContext
  onUploaded?: (skill: AgentSkill) => void
  open: boolean
  onOpenChange: (open: boolean) => void
}) {
  const { t } = useTranslation(['agentV2', 'common'])
  const { t: tCommon } = useTranslation(['common'])
  const [file, setFile] = useState<File>()
  const uploadAgentSkillMutation = useMutation(
    consoleQuery.agent.byAgentId.config.skills.upload.post.mutationOptions(),
  )
  const uploadWorkflowSkillMutation = useMutation(
    consoleQuery.apps.byAppId.agent.config.skills.upload.post.mutationOptions(),
  )
  const uploadSkillMutation = apiContext.workflow
    ? uploadWorkflowSkillMutation
    : uploadAgentSkillMutation

  const handleUpload = () => {
    if (!file || uploadSkillMutation.isPending) return

    const options = {
      onSuccess: (
        response:
          | PostAgentByAgentIdConfigSkillsUploadResponse
          | PostAppsByAppIdAgentConfigSkillsUploadResponse,
      ) => {
        toast.success(t(($) => $['agentDetail.configure.skills.upload.success']))
        onUploaded?.(toUploadedSkill(response, file))
        onOpenChange(false)
      },
    }

    if (apiContext.workflow) {
      uploadWorkflowSkillMutation.mutate(
        {
          params: {
            app_id: apiContext.workflow.appId,
          },
          query: {
            node_id: apiContext.workflow.nodeId,
            draft_type: apiContext.draftType,
            version_id: apiContext.versionId,
          },
          body: {
            file,
          },
        },
        options,
      )
      return
    }

    uploadAgentSkillMutation.mutate(
      {
        params: {
          agent_id: apiContext.agentId,
        },
        query: {
          draft_type: apiContext.draftType,
          version_id: apiContext.versionId,
        },
        body: {
          file,
        },
      },
      options,
    )
  }

  return (
    <Dialog
      open={open}
      onOpenChange={(nextOpen, details) => {
        if (!nextOpen && uploadSkillMutation.isPending) details.cancel()
        else onOpenChange(nextOpen)
      }}
      onOpenChangeComplete={(open) => {
        if (!open) {
          uploadSkillMutation.reset()
          setFile(undefined)
        }
      }}
      disablePointerDismissal
    >
      <DialogContent backdropProps={{ forceRender: true, className: 'fixed' }}>
        <DialogHeader>
          <DialogTitle>{t(($) => $['agentDetail.configure.skills.upload.title'])}</DialogTitle>
          <DialogDescription>
            {t(($) => $['agentDetail.configure.skills.upload.description'])}
          </DialogDescription>
          <DialogHeaderCloseButton aria-label={t(($) => $['operation.close'], { ns: 'common' })} />
        </DialogHeader>
        <DialogBody>
          <AgentSkillPackageUploader
            file={file}
            onChange={setFile}
            showWarning={uploadSkillMutation.isError}
          />
        </DialogBody>
        <DialogFooter>
          <DialogClose render={<Button disabled={uploadSkillMutation.isPending} />}>
            {tCommon(($) => $['operation.cancel'])}
          </DialogClose>
          <Button
            type="button"
            variant="primary"
            disabled={!file}
            loading={uploadSkillMutation.isPending}
            onClick={handleUpload}
          >
            {t(($) => $['agentDetail.configure.skills.upload.action'])}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}