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.

6.6 KB
Raw Blame
'use client'

import {
  Breadcrumb,
  BreadcrumbItem,
  BreadcrumbList,
  BreadcrumbSeparator,
} from '@langgenius/dify-ui/breadcrumb'
import { cn } from '@langgenius/dify-ui/cn'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Menu, MenuContent, MenuItem, MenuTrigger } from '@langgenius/dify-ui/menu'
import { Fragment } from 'react'
import { useTranslation } from 'react-i18next'

export const AGENT_SAVED_FILES_ROOT_PATH = '~'
export const AGENT_TEMPORARY_FILES_ROOT_PATH = '.'

export type AgentWorkingDirectoryRootPath =
  | typeof AGENT_SAVED_FILES_ROOT_PATH
  | typeof AGENT_TEMPORARY_FILES_ROOT_PATH

export type AgentWorkingDirectoryPath = AgentWorkingDirectoryRootPath | string

type AgentWorkingDirectoryBreadcrumbItemData = {
  iconClassName: string
  label: string
  path: AgentWorkingDirectoryPath
}

const normalizeWorkingDirectoryPath = (path: AgentWorkingDirectoryPath) => {
  if (path === AGENT_TEMPORARY_FILES_ROOT_PATH || path === AGENT_SAVED_FILES_ROOT_PATH) return path

  if (path.startsWith('~/'))
    return `${AGENT_SAVED_FILES_ROOT_PATH}/${path.slice(2).replace(/^\/+|\/+$/g, '')}`

  return path.replace(/^\.\/+/, '').replace(/^\/+|\/+$/g, '')
}

function buildPathFromSegments(rootPath: AgentWorkingDirectoryRootPath, segments: string[]) {
  return segments.length ? `${rootPath}/${segments.join('/')}` : rootPath
}

function getBreadcrumbItems(
  path: AgentWorkingDirectoryPath,
): AgentWorkingDirectoryBreadcrumbItemData[] {
  const normalizedPath = normalizeWorkingDirectoryPath(path)
  const rootPath =
    normalizedPath === AGENT_SAVED_FILES_ROOT_PATH ||
    normalizedPath.startsWith(`${AGENT_SAVED_FILES_ROOT_PATH}/`)
      ? AGENT_SAVED_FILES_ROOT_PATH
      : AGENT_TEMPORARY_FILES_ROOT_PATH
  const segments =
    rootPath === AGENT_SAVED_FILES_ROOT_PATH
      ? normalizedPath.slice(2).split('/').filter(Boolean)
      : normalizedPath
          .replace(/^\.\/?/, '')
          .split('/')
          .filter(Boolean)

  const rootItem: AgentWorkingDirectoryBreadcrumbItemData = {
    iconClassName: 'i-ri-folder-3-line',
    label: rootPath,
    path: rootPath,
  }

  return [
    rootItem,
    ...segments.map((segment, index) => {
      return {
        iconClassName: 'i-ri-folder-3-line',
        label: segment,
        path: buildPathFromSegments(rootPath, segments.slice(0, index + 1)),
      }
    }),
  ]
}

function getVisibleBreadcrumbItems(items: AgentWorkingDirectoryBreadcrumbItemData[]) {
  if (items.length <= 3) {
    return {
      hiddenItems: [],
      visibleItems: items,
    }
  }

  return {
    hiddenItems: items.slice(1, -2),
    visibleItems: [items[0]!, ...items.slice(-2)],
  }
}

function AgentWorkingDirectoryBreadcrumbItem({
  active,
  iconClassName,
  label,
  onClick,
}: {
  active: boolean
  iconClassName: string
  label: string
  onClick: () => void
}) {
  return (
    <button
      type="button"
      onClick={onClick}
      aria-current={active ? 'location' : undefined}
      className={cn(
        'flex min-w-0 items-center justify-center gap-1 rounded-md py-0.5 pr-1 pl-0.5 text-text-tertiary hover:bg-state-base-hover hover:text-text-secondary focus-visible:ring-2 focus-visible:ring-state-accent-solid focus-visible:outline-hidden',
        active && 'text-text-secondary',
      )}
    >
      <span aria-hidden className={cn('size-5 shrink-0', iconClassName)} />
      <span className="min-w-0 truncate system-sm-regular">{label}</span>
    </button>
  )
}

export function AgentWorkingDirectoryBreadcrumb({
  path,
  onPathChange,
}: {
  path: AgentWorkingDirectoryPath
  onPathChange: (path: AgentWorkingDirectoryPath) => void
}) {
  const { t } = useTranslation(['agentV2', 'common'])
  const items = getBreadcrumbItems(path)
  const { hiddenItems, visibleItems } = getVisibleBreadcrumbItems(items)

  return (
    <div className="mb-1 flex w-full shrink-0 flex-col border-y-[0.5px] border-divider-regular px-2.5">
      <Breadcrumb
        aria-label={t(($) => $['agentDetail.configure.workingDirectory.breadcrumbLabel'])}
        className="py-1"
      >
        <BreadcrumbList className="gap-0.5">
          {visibleItems.map((item, index) => {
            const isLastItem = index === visibleItems.length - 1

            return (
              <Fragment key={item.path}>
                {index > 0 && (
                  <BreadcrumbSeparator className="system-xs-regular text-divider-deep" />
                )}
                {index === 1 && hiddenItems.length > 0 && (
                  <>
                    <BreadcrumbItem className="shrink-0">
                      <Menu modal={false}>
                        <MenuTrigger
                          render={
                            <IconButton
                              aria-label={t(($) => $['operation.more'], { ns: 'common' })}
                              className="data-popup-open:bg-state-base-hover data-popup-open:text-text-secondary"
                            >
                              <span aria-hidden className="i-ri-more-fill size-4" />
                            </IconButton>
                          }
                        />
                        <MenuContent align="start" className="w-34">
                          {hiddenItems.map((hiddenItem) => (
                            <MenuItem
                              key={hiddenItem.path}
                              className="gap-1 px-2 py-1.5"
                              onClick={() => onPathChange(hiddenItem.path)}
                            >
                              <span
                                aria-hidden
                                className={cn(
                                  'size-4 shrink-0 text-text-secondary',
                                  hiddenItem.iconClassName,
                                )}
                              />
                              <span className="min-w-0 truncate px-1 system-md-regular">
                                {hiddenItem.label}
                              </span>
                            </MenuItem>
                          ))}
                        </MenuContent>
                      </Menu>
                    </BreadcrumbItem>
                    <BreadcrumbSeparator className="system-xs-regular text-divider-deep" />
                  </>
                )}
                <BreadcrumbItem>
                  <AgentWorkingDirectoryBreadcrumbItem
                    active={isLastItem}
                    iconClassName={item.iconClassName}
                    label={item.label}
                    onClick={() => onPathChange(item.path)}
                  />
                </BreadcrumbItem>
              </Fragment>
            )
          })}
        </BreadcrumbList>
      </Breadcrumb>
    </div>
  )
}