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.

23.8 KB
Raw Blame
import type { InstalledAppResponse } from '@dify/contracts/api/console/installed-apps/types.gen'
import type { ExtraContent } from '../chat/type'
import type { Callback, ChatConfig, ChatItem, OnFeedback } from '../types'
import type { AppData, ConversationItem } from '@/models/share'
import type { HumanInputFilledFormData, HumanInputFormData } from '@/types/workflow'
import { noop } from 'es-toolkit/function'
import { produce } from 'immer'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import {
  useConversationSelection,
  useWebAppSidebarCollapseState,
} from '@/app/components/base/chat/storage'
import { getProcessedFilesFromResponse } from '@/app/components/base/file-uploader/utils'
import { InputVarType } from '@/app/components/workflow/types'
import { toast } from '@/app/notifications'
import { useWebAppStore } from '@/context/web-app-context'
import { useAppFavicon } from '@/hooks/use-app-favicon'
import { changeLanguage } from '@/i18n/client'
import {
  AppSourceType,
  delConversation,
  pinConversation,
  renameConversation,
  unpinConversation,
  updateFeedback,
} from '@/service/share'
import {
  EnvironmentConversationNotFoundError,
  useInvalidateShareConversations,
  useShareChatList,
  useShareConversationName,
  useShareConversations,
} from '@/service/use-share'
import { getWebAppConversationScopeId, resolveWebAppAddress } from '@/service/webapp-address'
import { TransferMethod } from '@/types/app'
import { addFileInfos, sortAgentSorts } from '../../../tools/utils'
import { enrichSubmittedHumanInputFormData } from '../chat/answer/human-input-content/submitted-utils'
import {
  buildChatItemTree,
  getProcessedSystemVariablesFromUrlParams,
  getRawInputsFromUrlParams,
  getRawUserVariablesFromUrlParams,
} from '../utils'

function getFormattedChatList(messages: any[]) {
  const newChatList: ChatItem[] = []
  messages.forEach((item) => {
    const questionFiles =
      item.message_files?.filter((file: any) => file.belongs_to === 'user') || []
    newChatList.push({
      id: `question-${item.id}`,
      content: item.query,
      isAnswer: false,
      message_files: getProcessedFilesFromResponse(
        questionFiles.map((item: any) => ({
          ...item,
          related_id: item.id,
          upload_file_id: item.upload_file_id,
        })),
      ),
      parentMessageId: item.parent_message_id || undefined,
    })
    const answerFiles =
      item.message_files?.filter((file: any) => file.belongs_to === 'assistant') || []
    const answerTokens = item.answer_tokens ?? 0
    const messageTokens = item.message_tokens ?? 0
    const latency = Number(item.provider_response_latency)
    const more =
      item.provider_response_latency == null || !Number.isFinite(latency)
        ? undefined
        : {
            time: '',
            tokens: answerTokens + messageTokens,
            latency: latency.toFixed(2),
            tokens_per_second: latency > 0 ? (answerTokens / latency).toFixed(2) : undefined,
          }
    const humanInputFormDataList: HumanInputFormData[] = []
    const humanInputFilledFormDataList: HumanInputFilledFormData[] = []
    let workflowRunId = ''
    item.extra_contents?.forEach((content: ExtraContent, contentIndex: number) => {
      if (content.type !== 'human_input') return

      const formDefinition = 'form_definition' in content ? content.form_definition : undefined
      if (!content.submitted) {
        if (!formDefinition) return
        humanInputFormDataList.push(formDefinition)
        workflowRunId = content.workflow_run_id || workflowRunId
        return
      }

      if (!('form_submission_data' in content) || !content.form_submission_data) return
      // Legacy submissions without a definition expose no form ID, so give their
      // cards a history-only identity.
      const formId =
        formDefinition?.form_id ??
        `${content.workflow_run_id ?? item.id}:${content.form_submission_data.node_id}:${contentIndex}`
      workflowRunId = content.workflow_run_id || workflowRunId
      humanInputFilledFormDataList.push(
        enrichSubmittedHumanInputFormData(
          { ...content.form_submission_data, form_id: formId },
          formDefinition,
        ),
      )
    })
    newChatList.push({
      id: item.id,
      content: item.answer,
      agent_thoughts: addFileInfos(
        item.agent_thoughts ? sortAgentSorts(item.agent_thoughts) : item.agent_thoughts,
        item.message_files,
      ),
      feedback: item.feedback,
      isAnswer: true,
      citation: item.retriever_resources,
      reasoningContent: item.metadata?.reasoning,
      reasoningFinished: true,
      message_files: getProcessedFilesFromResponse(
        answerFiles.map((item: any) => ({
          ...item,
          related_id: item.id,
          upload_file_id: item.upload_file_id,
        })),
      ),
      parentMessageId: `question-${item.id}`,
      humanInputFormDataList,
      humanInputFilledFormDataList,
      workflow_run_id: workflowRunId,
      more,
    })
  })
  return newChatList
}
export const useChatWithHistory = (installedAppInfo?: InstalledAppResponse) => {
  const isInstalledApp = useMemo(() => !!installedAppInfo, [installedAppInfo])
  const appSourceType = isInstalledApp ? AppSourceType.installedApp : AppSourceType.webApp
  const appInfo = useWebAppStore((s) => s.appInfo)
  const appParams = useWebAppStore((s) => s.appParams)
  const appMeta = useWebAppStore((s) => s.appMeta)
  useAppFavicon({
    enable: !installedAppInfo,
    icon_type: appInfo?.site.icon_type,
    icon: appInfo?.site.icon,
    icon_background: appInfo?.site.icon_background,
    icon_url: appInfo?.site.icon_url,
  })
  const appData = useMemo(() => {
    if (isInstalledApp) {
      const { id, app } = installedAppInfo!
      return {
        app_id: id,
        site: {
          title: app.name,
          description: app.description,
          icon_type: app.icon_type,
          icon: app.icon ?? undefined,
          icon_background: app.icon_background,
          icon_url: app.icon_url,
          prompt_public: false,
          copyright: '',
          show_workflow_steps: true,
          use_icon_as_answer_icon: app.use_icon_as_answer_icon,
        },
        custom_config: null,
      } satisfies AppData
    }
    return appInfo
  }, [isInstalledApp, installedAppInfo, appInfo])
  const appId = useMemo(() => appData?.app_id, [appData])
  const conversationScopeId = getWebAppConversationScopeId(resolveWebAppAddress(), appId)
  const [userId, setUserId] = useState<string>()
  useEffect(() => {
    getProcessedSystemVariablesFromUrlParams().then(({ user_id }) => {
      setUserId(user_id)
    })
  }, [])
  useEffect(() => {
    const setLocaleFromProps = async () => {
      if (appData?.site.default_language) await changeLanguage(appData.site.default_language)
    }
    setLocaleFromProps()
  }, [appData])
  const [storedSidebarCollapseState, setStoredSidebarCollapseState] =
    useWebAppSidebarCollapseState()
  const sidebarCollapseState = storedSidebarCollapseState === 'collapsed'
  const handleSidebarCollapse = useCallback(
    (state: boolean) => {
      if (appId) setStoredSidebarCollapseState(state ? 'collapsed' : 'expanded')
    },
    [appId, setStoredSidebarCollapseState],
  )
  const { currentConversationId, handleConversationIdInfoChange } = useConversationSelection({
    scopeId: isInstalledApp || appData?.end_user_id ? conversationScopeId : '',
    userId: isInstalledApp ? userId : appData?.end_user_id,
  })
  const [newConversationId, setNewConversationId] = useState('')
  const chatShouldReloadKey = useMemo(() => {
    if (currentConversationId === newConversationId) return ''
    return currentConversationId
  }, [currentConversationId, newConversationId])
  const { data: appPinnedConversationData } = useShareConversations(
    {
      appSourceType,
      appId,
      pinned: true,
      limit: 100,
    },
    {
      enabled: !!appId,
      refetchOnWindowFocus: false,
      refetchOnReconnect: false,
    },
  )
  const { data: appConversationData, isLoading: appConversationDataLoading } =
    useShareConversations(
      {
        appSourceType,
        appId,
        pinned: false,
        limit: 100,
      },
      {
        enabled: !!appId,
        refetchOnWindowFocus: false,
        refetchOnReconnect: false,
      },
    )
  const {
    data: appChatListData,
    error: appChatListError,
    isLoading: appChatListDataLoading,
  } = useShareChatList(
    {
      conversationId: chatShouldReloadKey,
      appSourceType,
      appId,
    },
    {
      enabled: !!chatShouldReloadKey,
      refetchOnWindowFocus: false,
      refetchOnReconnect: false,
    },
  )
  const invalidateShareConversations = useInvalidateShareConversations()
  const [clearChatList, setClearChatList] = useState(false)
  const [isResponding, setIsResponding] = useState(false)
  useEffect(() => {
    if (!(appChatListError instanceof EnvironmentConversationNotFoundError)) return

    // oxlint-disable-next-line eslint-react/set-state-in-effect -- A missing Environment conversation resets the active conversation.
    setNewConversationId('')
    handleConversationIdInfoChange('')
    // oxlint-disable-next-line eslint-react/set-state-in-effect -- A missing Environment conversation must clear the rendered chat.
    setClearChatList(true)
  }, [appChatListError, handleConversationIdInfoChange])
  const appPrevChatTree = useMemo(
    () =>
      currentConversationId && appChatListData?.data.length
        ? buildChatItemTree(getFormattedChatList(appChatListData.data))
        : [],
    [appChatListData, currentConversationId],
  )
  const [showNewConversationItemInList, setShowNewConversationItemInList] = useState(false)
  const pinnedConversationList = useMemo(() => {
    return appPinnedConversationData?.data || []
  }, [appPinnedConversationData])
  const { t } = useTranslation(['appDebug', 'common', 'share'])
  const newConversationInputsRef = useRef<Record<string, any>>({})
  const [newConversationInputs, setNewConversationInputs] = useState<Record<string, any>>({})
  const [initInputs, setInitInputs] = useState<Record<string, any>>({})
  const [initUserVariables, setInitUserVariables] = useState<Record<string, any>>({})
  const handleNewConversationInputsChange = useCallback((newInputs: Record<string, any>) => {
    newConversationInputsRef.current = newInputs
    // oxlint-disable-next-line eslint-react/set-state-in-effect -- This handler intentionally syncs derived input defaults when called from the reset effect below.
    setNewConversationInputs(newInputs)
  }, [])
  const inputsForms = useMemo(() => {
    return (appParams?.user_input_form || [])
      .filter((item: any) => !item.external_data_tool)
      .map((item: any) => {
        if (item.paragraph) {
          let value = initInputs[item.paragraph.variable]
          if (value && item.paragraph.max_length && value.length > item.paragraph.max_length)
            value = value.slice(0, item.paragraph.max_length)
          return {
            ...item.paragraph,
            default: value || item.default || item.paragraph.default,
            type: 'paragraph',
          }
        }
        if (item.number) {
          const rawNumber = initInputs[item.number.variable]
          const convertedNumber =
            rawNumber === undefined || rawNumber === null || rawNumber === ''
              ? Number.NaN
              : Number(rawNumber)
          return {
            ...item.number,
            default: Number.isNaN(convertedNumber)
              ? (item.default ?? item.number.default)
              : convertedNumber,
            type: 'number',
          }
        }
        if (item.checkbox) {
          const preset = initInputs[item.checkbox.variable]
          return {
            ...item.checkbox,
            default: typeof preset === 'boolean' ? preset : (item.default ?? item.checkbox.default),
            type: 'checkbox',
          }
        }
        if (item.select) {
          const isInputInOptions = item.select.options.includes(initInputs[item.select.variable])
          return {
            ...item.select,
            default:
              (isInputInOptions ? initInputs[item.select.variable] : undefined) ||
              item.select.default,
            type: 'select',
          }
        }
        if (item['file-list']) {
          return {
            ...item['file-list'],
            type: 'file-list',
          }
        }
        if (item.file) {
          return {
            ...item.file,
            type: 'file',
          }
        }
        if (item.json_object) {
          return {
            ...item.json_object,
            type: 'json_object',
          }
        }
        let value = initInputs[item['text-input'].variable]
        if (value && item['text-input'].max_length && value.length > item['text-input'].max_length)
          value = value.slice(0, item['text-input'].max_length)
        return {
          ...item['text-input'],
          default: value || item.default || item['text-input'].default,
          type: 'text-input',
        }
      })
  }, [initInputs, appParams])
  const allInputsHidden = useMemo(() => {
    return inputsForms.length > 0 && inputsForms.every((item) => item.hide === true)
  }, [inputsForms])
  useEffect(() => {
    // init inputs from url params
    ;(async () => {
      const inputs = await getRawInputsFromUrlParams()
      const userVariables = await getRawUserVariablesFromUrlParams()
      setInitInputs(inputs)
      setInitUserVariables(userVariables)
    })()
  }, [])
  useEffect(() => {
    const conversationInputs: Record<string, any> = {}
    inputsForms.forEach((item: any) => {
      conversationInputs[item.variable] = item.default === '' ? null : (item.default ?? null)
    })
    handleNewConversationInputsChange(conversationInputs)
  }, [handleNewConversationInputsChange, inputsForms])
  const { data: newConversation } = useShareConversationName(
    {
      conversationId: newConversationId,
      appSourceType,
      appId,
    },
    {
      refetchOnWindowFocus: false,
      enabled: !!newConversationId,
    },
  )
  const [originConversationList, setOriginConversationList] = useState<ConversationItem[]>([])
  useEffect(() => {
    if (appConversationData?.data && !appConversationDataLoading)
      // oxlint-disable-next-line eslint-react/set-state-in-effect -- Conversation query results intentionally replace the local editable list.
      setOriginConversationList(appConversationData?.data)
  }, [appConversationData, appConversationDataLoading])
  const conversationList = useMemo(() => {
    const data = originConversationList.slice()
    if (showNewConversationItemInList && data[0]?.id !== '') {
      data.unshift({
        id: '',
        name: t(($) => $['chat.newChatDefaultName'], { ns: 'share' }),
        inputs: {},
        introduction: '',
      })
    }
    return data
  }, [originConversationList, showNewConversationItemInList, t])
  useEffect(() => {
    if (newConversation) {
      // oxlint-disable-next-line eslint-react/set-state-in-effect -- Newly resolved conversation names intentionally patch the local list cache.
      setOriginConversationList(
        produce((draft) => {
          const index = draft.findIndex((item) => item.id === newConversation.id)
          if (index > -1) draft[index] = newConversation
          else draft.unshift(newConversation)
        }),
      )
    }
  }, [newConversation])
  const currentConversationItem = useMemo(() => {
    let conversationItem = conversationList.find((item) => item.id === currentConversationId)
    if (!conversationItem && pinnedConversationList.length)
      conversationItem = pinnedConversationList.find((item) => item.id === currentConversationId)
    return conversationItem
  }, [conversationList, currentConversationId, pinnedConversationList])
  const currentConversationLatestInputs = useMemo(() => {
    if (!currentConversationId || !appChatListData?.data.length)
      return newConversationInputsRef.current || {}
    return appChatListData.data.slice().pop().inputs || {}
  }, [appChatListData, currentConversationId])
  const [currentConversationInputs, setCurrentConversationInputs] = useState<Record<string, any>>(
    currentConversationLatestInputs || {},
  )
  useEffect(() => {
    if (currentConversationItem)
      // oxlint-disable-next-line eslint-react/set-state-in-effect -- Selected conversation changes intentionally resync the editable input snapshot.
      setCurrentConversationInputs(currentConversationLatestInputs || {})
  }, [currentConversationItem, currentConversationLatestInputs])
  const checkInputsRequired = useCallback(
    (silent?: boolean) => {
      if (allInputsHidden) return true
      let hasEmptyInput = ''
      let fileIsUploading = false
      const requiredVars = inputsForms.filter(
        ({ required, type }) => required && type !== InputVarType.checkbox,
      )
      if (requiredVars.length) {
        requiredVars.forEach(({ variable, label, type }) => {
          if (hasEmptyInput) return
          if (fileIsUploading) return
          const inputValue = newConversationInputsRef.current[variable]
          if (
            (inputValue === undefined ||
              inputValue === null ||
              inputValue === '' ||
              (Array.isArray(inputValue) && inputValue.length === 0)) &&
            !silent
          )
            hasEmptyInput = label as string
          if (
            (type === InputVarType.singleFile || type === InputVarType.multiFiles) &&
            newConversationInputsRef.current[variable] &&
            !silent
          ) {
            const files = newConversationInputsRef.current[variable]
            if (Array.isArray(files))
              fileIsUploading = files.find(
                (item) => item.transferMethod === TransferMethod.local_file && !item.uploadedId,
              )
            else
              fileIsUploading =
                files.transferMethod === TransferMethod.local_file && !files.uploadedId
          }
        })
      }
      if (hasEmptyInput) {
        toast.error(
          t(($) => $['errorMessage.valueOfVarRequired'], { ns: 'appDebug', key: hasEmptyInput }),
        )
        return false
      }
      if (fileIsUploading) {
        toast.info(t(($) => $['errorMessage.waitForFileUpload'], { ns: 'appDebug' }))
        return
      }
      return true
    },
    [inputsForms, t, allInputsHidden],
  )
  const handleStartChat = useCallback(
    (callback?: () => void) => {
      if (checkInputsRequired()) {
        setShowNewConversationItemInList(true)
        callback?.()
      }
    },
    [setShowNewConversationItemInList, checkInputsRequired],
  )
  const currentChatInstanceRef = useRef<{
    handleStop: () => void
  }>({ handleStop: noop })
  const handleChangeConversation = useCallback(
    (conversationId: string) => {
      currentChatInstanceRef.current.handleStop()
      setNewConversationId('')
      handleConversationIdInfoChange(conversationId)
      if (conversationId) setClearChatList(false)
    },
    [handleConversationIdInfoChange, setClearChatList],
  )
  const handleNewConversation = useCallback(async () => {
    currentChatInstanceRef.current.handleStop()
    setShowNewConversationItemInList(true)
    const conversationInputs: Record<string, any> = {}
    inputsForms.forEach((item: any) => {
      conversationInputs[item.variable] = item.default === '' ? null : (item.default ?? null)
    })
    handleNewConversationInputsChange(conversationInputs)
    handleChangeConversation('')
    setClearChatList(true)
  }, [
    handleChangeConversation,
    setShowNewConversationItemInList,
    handleNewConversationInputsChange,
    setClearChatList,
    inputsForms,
  ])
  const handleUpdateConversationList = useCallback(() => {
    invalidateShareConversations()
  }, [invalidateShareConversations])
  const handlePinConversation = useCallback(
    async (conversationId: string) => {
      await pinConversation(appSourceType, appId, conversationId)
      toast.success(t(($) => $['api.success'], { ns: 'common' }))
      handleUpdateConversationList()
    },
    [appSourceType, appId, t, handleUpdateConversationList],
  )
  const handleUnpinConversation = useCallback(
    async (conversationId: string) => {
      await unpinConversation(appSourceType, appId, conversationId)
      toast.success(t(($) => $['api.success'], { ns: 'common' }))
      handleUpdateConversationList()
    },
    [appSourceType, appId, t, handleUpdateConversationList],
  )
  const [conversationDeleting, setConversationDeleting] = useState(false)
  const handleDeleteConversation = useCallback(
    async (conversationId: string, { onSuccess }: Callback) => {
      if (conversationDeleting) return
      try {
        setConversationDeleting(true)
        await delConversation(appSourceType, appId, conversationId)
        toast.success(t(($) => $['api.success'], { ns: 'common' }))
        onSuccess()
      } finally {
        setConversationDeleting(false)
      }
      if (conversationId === currentConversationId) handleNewConversation()
      handleUpdateConversationList()
    },
    [
      appSourceType,
      appId,
      t,
      handleUpdateConversationList,
      handleNewConversation,
      currentConversationId,
      conversationDeleting,
    ],
  )
  const [conversationRenaming, setConversationRenaming] = useState(false)
  const handleRenameConversation = useCallback(
    async (conversationId: string, newName: string, { onSuccess }: Callback) => {
      if (conversationRenaming) return
      if (!newName.trim()) {
        toast.error(t(($) => $['chat.conversationNameCanNotEmpty'], { ns: 'common' }))
        return
      }
      setConversationRenaming(true)
      try {
        await renameConversation(appSourceType, appId, conversationId, newName)
        setOriginConversationList(
          produce((draft) => {
            const index = originConversationList.findIndex((item) => item.id === conversationId)
            const item = draft[index]!
            draft[index] = {
              ...item,
              name: newName,
            }
          }),
        )
        onSuccess()
      } finally {
        setConversationRenaming(false)
      }
    },
    [appSourceType, appId, t, conversationRenaming, originConversationList],
  )
  const handleNewConversationCompleted = useCallback(
    (newConversationId: string) => {
      setNewConversationId(newConversationId)
      handleConversationIdInfoChange(newConversationId)
      setShowNewConversationItemInList(false)
      invalidateShareConversations()
    },
    [handleConversationIdInfoChange, invalidateShareConversations],
  )
  const handleFeedback: OnFeedback = useCallback(
    async (messageId, feedback) => {
      await updateFeedback(
        {
          messageId,
          body: { rating: feedback.rating, content: feedback.content },
        },
        appSourceType,
        appId,
      )
    },
    [appSourceType, appId],
  )
  return {
    isInstalledApp,
    appId,
    currentConversationId,
    currentConversationItem,
    handleConversationIdInfoChange,
    appData,
    appParams: appParams || ({} as ChatConfig),
    appMeta,
    appPinnedConversationData,
    appConversationData,
    appConversationDataLoading,
    appChatListData,
    appChatListDataLoading,
    appPrevChatTree,
    pinnedConversationList,
    conversationList,
    setShowNewConversationItemInList,
    newConversationInputs,
    newConversationInputsRef,
    handleNewConversationInputsChange,
    inputsForms,
    handleNewConversation,
    handleStartChat,
    handleChangeConversation,
    handlePinConversation,
    handleUnpinConversation,
    conversationDeleting,
    handleDeleteConversation,
    conversationRenaming,
    handleRenameConversation,
    handleNewConversationCompleted,
    newConversationId,
    chatShouldReloadKey,
    handleFeedback,
    currentChatInstanceRef,
    sidebarCollapseState,
    handleSidebarCollapse,
    clearChatList,
    setClearChatList,
    isResponding,
    setIsResponding,
    currentConversationInputs,
    setCurrentConversationInputs,
    allInputsHidden,
    initUserVariables,
  }
}