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.

1.6 KB
Raw Blame
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from '@/app/notifications'
import { ssePost } from '@/service/base'

type SendOptions = {
  onNotifyError?: (message: string, code?: string) => void
}

export const useTextGeneration = () => {
  const { t } = useTranslation(['appDebug'])
  const [isResponding, setIsResponding] = useState(false)
  const [completion, setCompletion] = useState('')
  const [messageId, setMessageId] = useState<string | null>(null)
  const handleSend = async (
    url: string,
    data: Record<string, unknown>,
    { onNotifyError }: SendOptions = {},
  ) => {
    if (isResponding) {
      toast.info(t(($) => $['errorMessage.waitForResponse'], { ns: 'appDebug' }))
      return false
    }
    setIsResponding(true)
    setCompletion('')
    setMessageId('')
    let res: string[] = []
    ssePost(
      url,
      {
        body: {
          response_mode: 'streaming',
          ...data,
        },
      },
      {
        onData: (data: string, _isFirstMessage: boolean, { messageId }) => {
          res.push(data)
          setCompletion(res.join(''))
          setMessageId(messageId)
        },
        onMessageReplace: (messageReplace) => {
          res = [messageReplace.answer]
          setCompletion(res.join(''))
        },
        onCompleted() {
          setIsResponding(false)
        },
        onError() {
          setIsResponding(false)
        },
        onNotifyError,
      },
    )
    return true
  }
  return {
    completion,
    isResponding,
    setIsResponding,
    handleSend,
    messageId,
  }
}