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.

3.9 KB
Raw Blame
'use client'
import type { FC } from 'react'
import { Button } from '@langgenius/dify-ui/button'
import { InputGroup, InputGroupAddon, InputGroupInput } from '@langgenius/dify-ui/input-group'
import { Menu, MenuContent, MenuItem, MenuTrigger } from '@langgenius/dify-ui/menu'
import { Popover, PopoverContent, PopoverTrigger } from '@langgenius/dify-ui/popover'
import * as React from 'react'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from '@/app/notifications'
import { importSchemaFromURL } from '@/service/tools'
import examples from './examples'

type Props = Readonly<{
  onChange: (value: string) => void
}>

const GetSchema: FC<Props> = ({ onChange }) => {
  const { t } = useTranslation(['common', 'tools'])
  const [showImportFromUrl, setShowImportFromUrl] = useState(false)
  const [importUrl, setImportUrl] = useState('')
  const [isParsing, setIsParsing] = useState(false)
  const handleImportFromUrl = async () => {
    if (isParsing) return
    if (!importUrl.startsWith('http://') && !importUrl.startsWith('https://')) {
      toast.error(t(($) => $['createTool.urlError'], { ns: 'tools' }))
      return
    }
    setIsParsing(true)
    try {
      const { schema } = await importSchemaFromURL(importUrl)
      setImportUrl('')
      onChange(schema)
    } finally {
      setIsParsing(false)
      setShowImportFromUrl(false)
    }
  }

  return (
    <div className="flex w-56 justify-end gap-1">
      <Popover open={showImportFromUrl} onOpenChange={setShowImportFromUrl}>
        <PopoverTrigger render={<Button size="small" />}>
          <span className="i-ri-add-line size-3" aria-hidden />
          <span className="system-xs-medium text-text-secondary">
            {t(($) => $['createTool.importFromUrl'], { ns: 'tools' })}
          </span>
        </PopoverTrigger>
        <PopoverContent
          align="start"
          sideOffset={2}
          className="w-75 p-2"
          aria-label={t(($) => $['createTool.importFromUrl'], { ns: 'tools' })}
        >
          <form
            onSubmit={(event) => {
              event.preventDefault()
              void handleImportFromUrl()
            }}
          >
            <InputGroup>
              <InputGroupInput
                type="text"
                name="schemaUrl"
                inputMode="url"
                aria-label={t(($) => $['createTool.importFromUrl'], { ns: 'tools' })}
                placeholder={t(($) => $['createTool.importFromUrlPlaceHolder'], { ns: 'tools' })!}
                value={importUrl}
                onValueChange={(value) => setImportUrl(value)}
              />
              <InputGroupAddon align="inline-end" className="pe-1">
                <Button
                  type="submit"
                  size="small"
                  variant="primary"
                  disabled={!importUrl}
                  loading={isParsing}
                >
                  {t(($) => $['operation.ok'], { ns: 'common' })}
                </Button>
              </InputGroupAddon>
            </InputGroup>
          </form>
        </PopoverContent>
      </Popover>
      <Menu>
        <MenuTrigger render={<Button size="small" />}>
          <span className="system-xs-medium text-text-secondary">
            {t(($) => $['createTool.examples'], { ns: 'tools' })}
          </span>
          <span className="i-ri-arrow-down-s-line size-3" aria-hidden />
        </MenuTrigger>
        <MenuContent align="end" sideOffset={2} className="min-w-max">
          {examples.map((item) => (
            <MenuItem
              key={item.key}
              onClick={() => {
                onChange(item.content)
              }}
              className="system-sm-regular whitespace-nowrap text-text-secondary"
            >
              {t(($) => $[`createTool.exampleOptions.${item.key}`], { ns: 'tools' })}
            </MenuItem>
          ))}
        </MenuContent>
      </Menu>
    </div>
  )
}
export default React.memo(GetSchema)