index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { useBoolean, useGetState } from 'ahooks'
  5. import { t } from 'i18next'
  6. import cn from 'classnames'
  7. import TextGenerationRes from '@/app/components/app/text-generate/item'
  8. import NoData from '@/app/components/share/text-generation/no-data'
  9. import Toast from '@/app/components/base/toast'
  10. import { sendCompletionMessage, updateFeedback } from '@/service/share'
  11. import type { Feedbacktype } from '@/app/components/app/chat/type'
  12. import Loading from '@/app/components/base/loading'
  13. import type { PromptConfig } from '@/models/debug'
  14. import type { InstalledApp } from '@/models/explore'
  15. export type IResultProps = {
  16. isCallBatchAPI: boolean
  17. isPC: boolean
  18. isMobile: boolean
  19. isInstalledApp: boolean
  20. installedAppInfo?: InstalledApp
  21. promptConfig: PromptConfig | null
  22. moreLikeThisEnabled: boolean
  23. inputs: Record<string, any>
  24. controlSend?: number
  25. controlStopResponding?: number
  26. onShowRes: () => void
  27. handleSaveMessage: (messageId: string) => void
  28. taskId?: number
  29. onCompleted: (completionRes: string, taskId?: number, success?: boolean) => void
  30. }
  31. const Result: FC<IResultProps> = ({
  32. isCallBatchAPI,
  33. isPC,
  34. isMobile,
  35. isInstalledApp,
  36. installedAppInfo,
  37. promptConfig,
  38. moreLikeThisEnabled,
  39. inputs,
  40. controlSend,
  41. controlStopResponding,
  42. onShowRes,
  43. handleSaveMessage,
  44. taskId,
  45. onCompleted,
  46. }) => {
  47. const [isResponsing, { setTrue: setResponsingTrue, setFalse: setResponsingFalse }] = useBoolean(false)
  48. useEffect(() => {
  49. if (controlStopResponding)
  50. setResponsingFalse()
  51. }, [controlStopResponding])
  52. const [completionRes, setCompletionRes, getCompletionRes] = useGetState('')
  53. const { notify } = Toast
  54. const isNoData = !completionRes
  55. const [messageId, setMessageId] = useState<string | null>(null)
  56. const [feedback, setFeedback] = useState<Feedbacktype>({
  57. rating: null,
  58. })
  59. const handleFeedback = async (feedback: Feedbacktype) => {
  60. await updateFeedback({ url: `/messages/${messageId}/feedbacks`, body: { rating: feedback.rating } }, isInstalledApp, installedAppInfo?.id)
  61. setFeedback(feedback)
  62. }
  63. const logError = (message: string) => {
  64. notify({ type: 'error', message })
  65. }
  66. const checkCanSend = () => {
  67. // batch will check outer
  68. if (isCallBatchAPI)
  69. return true
  70. const prompt_variables = promptConfig?.prompt_variables
  71. if (!prompt_variables || prompt_variables?.length === 0)
  72. return true
  73. let hasEmptyInput = ''
  74. const requiredVars = prompt_variables?.filter(({ key, name, required }) => {
  75. const res = (!key || !key.trim()) || (!name || !name.trim()) || (required || required === undefined || required === null)
  76. return res
  77. }) || [] // compatible with old version
  78. requiredVars.forEach(({ key, name }) => {
  79. if (hasEmptyInput)
  80. return
  81. if (!inputs[key])
  82. hasEmptyInput = name
  83. })
  84. if (hasEmptyInput) {
  85. logError(t('appDebug.errorMessage.valueOfVarRequired', { key: hasEmptyInput }))
  86. return false
  87. }
  88. return !hasEmptyInput
  89. }
  90. const handleSend = async () => {
  91. if (isResponsing) {
  92. notify({ type: 'info', message: t('appDebug.errorMessage.waitForResponse') })
  93. return false
  94. }
  95. if (!checkCanSend())
  96. return
  97. const data = {
  98. inputs,
  99. }
  100. setMessageId(null)
  101. setFeedback({
  102. rating: null,
  103. })
  104. setCompletionRes('')
  105. const res: string[] = []
  106. let tempMessageId = ''
  107. if (!isPC)
  108. onShowRes()
  109. setResponsingTrue()
  110. sendCompletionMessage(data, {
  111. onData: (data: string, _isFirstMessage: boolean, { messageId }) => {
  112. tempMessageId = messageId
  113. res.push(data)
  114. setCompletionRes(res.join(''))
  115. },
  116. onCompleted: () => {
  117. setResponsingFalse()
  118. setMessageId(tempMessageId)
  119. onCompleted(getCompletionRes(), taskId, true)
  120. },
  121. onError() {
  122. setResponsingFalse()
  123. onCompleted(getCompletionRes(), taskId, false)
  124. },
  125. }, isInstalledApp, installedAppInfo?.id)
  126. }
  127. const [controlClearMoreLikeThis, setControlClearMoreLikeThis] = useState(0)
  128. useEffect(() => {
  129. if (controlSend) {
  130. handleSend()
  131. setControlClearMoreLikeThis(Date.now())
  132. }
  133. }, [controlSend])
  134. const renderTextGenerationRes = () => (
  135. <TextGenerationRes
  136. className='mt-3'
  137. content={completionRes}
  138. messageId={messageId}
  139. isInWebApp
  140. moreLikeThis={moreLikeThisEnabled}
  141. onFeedback={handleFeedback}
  142. feedback={feedback}
  143. onSave={handleSaveMessage}
  144. isMobile={isMobile}
  145. isInstalledApp={isInstalledApp}
  146. installedAppId={installedAppInfo?.id}
  147. isLoading={isCallBatchAPI ? (!completionRes && isResponsing) : false}
  148. taskId={isCallBatchAPI ? ((taskId as number) < 10 ? `0${taskId}` : `${taskId}`) : undefined}
  149. controlClearMoreLikeThis={controlClearMoreLikeThis}
  150. />
  151. )
  152. return (
  153. <div className={cn(isNoData && !isCallBatchAPI && 'h-full')}>
  154. {!isCallBatchAPI && (
  155. (isResponsing && !completionRes)
  156. ? (
  157. <div className='flex h-full w-full justify-center items-center'>
  158. <Loading type='area' />
  159. </div>)
  160. : (
  161. <>
  162. {isNoData
  163. ? <NoData />
  164. : renderTextGenerationRes()
  165. }
  166. </>
  167. )
  168. )}
  169. {isCallBatchAPI && (
  170. <div className='mt-2'>
  171. {renderTextGenerationRes()}
  172. </div>
  173. )}
  174. </div>
  175. )
  176. }
  177. export default React.memo(Result)