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.8 KB
Raw Blame
'use client'
import type { HeaderItem } from '../headers-input'
import type { IconPickerValue } from '@/app/components/base/icon-picker'
import type { ToolWithProvider } from '@/app/components/workflow/types'
import { useMutation } from '@tanstack/react-query'
import { useCallback, useMemo, useRef, useState } from 'react'
import { v4 as uuid } from 'uuid'
import { MCPAuthMethod } from '@/app/components/tools/types'
import { toast } from '@/app/notifications'
import { consoleQuery } from '@/service/console'

const DEFAULT_ICON = { type: 'emoji', icon: '🔗', background: '#6366F1' } satisfies IconPickerValue
const extractFileId = (url: string) => {
  const match = /files\/(.+?)\/file-preview/.exec(url)
  return match ? match[1] : null
}
const getIcon = (data?: ToolWithProvider): IconPickerValue => {
  if (!data) return DEFAULT_ICON
  if (typeof data.icon === 'string') {
    const fileId = extractFileId(data.icon)
    return fileId ? { type: 'image', url: data.icon, fileId } : DEFAULT_ICON
  }
  return {
    type: 'emoji',
    icon: data.icon.content,
    background: data.icon.background,
  }
}
const getInitialHeaders = (data?: ToolWithProvider): HeaderItem[] => {
  return Object.entries(data?.masked_headers || {}).map(([key, value]) => ({
    id: uuid(),
    key,
    value,
  }))
}
export const isValidUrl = (string: string) => {
  try {
    const url = new URL(string)
    return url.protocol === 'http:' || url.protocol === 'https:'
  } catch {
    return false
  }
}
export const isValidServerID = (str: string) => {
  return /^[a-z0-9_-]{1,24}$/.test(str)
}
type MCPModalFormState = {
  url: string
  name: string
  appIcon: IconPickerValue
  serverIdentifier: string
  timeout: number
  sseReadTimeout: number
  headers: HeaderItem[]
  isFetchingIcon: boolean
  authMethod: MCPAuthMethod
  isDynamicRegistration: boolean
  clientID: string
  credentials: string
  forwardUserIdentity: boolean
}
type MCPModalFormActions = {
  setUrl: (url: string) => void
  setName: (name: string) => void
  setAppIcon: (icon: IconPickerValue) => void
  setServerIdentifier: (id: string) => void
  setTimeout: (timeout: number) => void
  setSseReadTimeout: (timeout: number) => void
  setHeaders: (headers: HeaderItem[]) => void
  setAuthMethod: (method: string) => void
  setIsDynamicRegistration: (value: boolean) => void
  setClientID: (id: string) => void
  setCredentials: (credentials: string) => void
  setForwardUserIdentity: (value: boolean) => void
  handleUrlBlur: (url: string) => Promise<void>
  resetIcon: () => void
}
/**
 * Custom hook for MCP Modal form state management.
 *
 * Note: This hook uses a `formKey` (data ID or 'create') to reset form state when
 * switching between edit and create modes. All useState initializers read from `data`
 * directly, and the key change triggers a remount of the consumer component.
 */
export const useMCPModalForm = (data?: ToolWithProvider) => {
  const { mutateAsync: uploadRemoteFile } = useMutation(
    consoleQuery.remoteFiles.upload.post.mutationOptions({ context: { silent: true } }),
  )
  const isCreate = !data
  const originalServerUrl = data?.server_url
  const originalServerID = data?.server_identifier
  // Form key for resetting state - changes when data changes
  const formKey = useMemo(() => data?.id ?? 'create', [data?.id])
  // Form state - initialized from data
  const [url, setUrl] = useState(() => data?.server_url || '')
  const [name, setName] = useState(() => data?.name || '')
  const [appIcon, setAppIcon] = useState<IconPickerValue>(() => getIcon(data))
  const [serverIdentifier, setServerIdentifier] = useState(() => data?.server_identifier || '')
  const [timeout, setMcpTimeout] = useState(() => data?.configuration?.timeout || 30)
  const [sseReadTimeout, setSseReadTimeout] = useState(
    () => data?.configuration?.sse_read_timeout || 300,
  )
  const [headers, setHeaders] = useState<HeaderItem[]>(() => getInitialHeaders(data))
  const [isFetchingIcon, setIsFetchingIcon] = useState(false)
  const urlBlurGenerationRef = useRef(0)
  // Auth state
  const [authMethod, setAuthMethod] = useState<MCPAuthMethod>(MCPAuthMethod.authentication)
  const [isDynamicRegistration, setIsDynamicRegistration] = useState(() =>
    isCreate ? true : (data?.is_dynamic_registration ?? true),
  )
  const [clientID, setClientID] = useState(() => data?.authentication?.client_id || '')
  const [credentials, setCredentials] = useState(() => data?.authentication?.client_secret || '')
  // M3 — user-identity forwarding. The UI toggle is true iff the persisted
  // identity_mode is anything other than "off" — currently just "idp_token".
  const [forwardUserIdentity, setForwardUserIdentity] = useState(
    () => (data?.identity_mode ?? 'off') !== 'off',
  )
  const handleUrlBlur = useCallback(
    async (urlValue: string) => {
      if (data) return
      if (!isValidUrl(urlValue)) return

      const blurGeneration = ++urlBlurGenerationRef.current
      const isStale = () => blurGeneration !== urlBlurGenerationRef.current

      setIsFetchingIcon(true)
      try {
        const { getDomain } = await import('tldts')
        if (isStale()) return

        const domain = getDomain(urlValue)
        if (!domain) return

        const remoteIcon = `https://www.google.com/s2/favicons?domain=${domain}&sz=128`
        const res = await uploadRemoteFile({ body: { url: remoteIcon } })
        if (isStale() || !res.url) return

        setAppIcon({ type: 'image', url: res.url, fileId: res.id })
      } catch (e) {
        if (isStale()) return

        let errorMessage = 'Failed to fetch remote icon'
        if (e instanceof Error) {
          errorMessage = e.message
        }
        console.error('Failed to fetch remote icon:', e)
        toast.warning(errorMessage)
      } finally {
        if (!isStale()) setIsFetchingIcon(false)
      }
    },
    [data, uploadRemoteFile],
  )
  const resetIcon = useCallback(() => {
    setAppIcon(getIcon(data))
  }, [data])
  const handleAuthMethodChange = useCallback((value: string) => {
    setAuthMethod(value as MCPAuthMethod)
  }, [])
  return {
    // Key for form reset (use as React key on parent)
    formKey,
    // Metadata
    isCreate,
    originalServerUrl,
    originalServerID,
    // State
    state: {
      url,
      name,
      appIcon,
      serverIdentifier,
      timeout,
      sseReadTimeout,
      headers,
      isFetchingIcon,
      authMethod,
      isDynamicRegistration,
      clientID,
      credentials,
      forwardUserIdentity,
    } satisfies MCPModalFormState,
    // Actions
    actions: {
      setUrl,
      setName,
      setAppIcon,
      setServerIdentifier,
      setTimeout: setMcpTimeout,
      setSseReadTimeout,
      setHeaders,
      setAuthMethod: handleAuthMethodChange,
      setIsDynamicRegistration,
      setClientID,
      setCredentials,
      setForwardUserIdentity,
      handleUrlBlur,
      resetIcon,
    } satisfies MCPModalFormActions,
  }
}