feat: text generaion support both url

pull/198/head
金伟强 3 years ago
parent 8208652607
commit 9a167aa688

@ -2,6 +2,7 @@
import React, { FC, useEffect } from 'react' import React, { FC, useEffect } from 'react'
import { App } from '@/types/app' import { App } from '@/types/app'
import ChatApp from '@/app/components/share/chat' import ChatApp from '@/app/components/share/chat'
import TextGenerationApp from '@/app/components/share/text-generation'
import { fetchAppDetail } from '@/service/explore' import { fetchAppDetail } from '@/service/explore'
import Loading from '@/app/components/base/loading' import Loading from '@/app/components/base/loading'
@ -35,7 +36,12 @@ const InstalledApp: FC<IInstalledAppProps> = ({
return ( return (
<div className='h-full'> <div className='h-full'>
<ChatApp isInstalledApp installedAppInfo={app as App}/> {app?.mode === 'chat' ? (
<ChatApp isInstalledApp installedAppInfo={app as App}/>
): (
<TextGenerationApp isInstalledApp installedAppInfo={app as App}/>
)}
</div> </div>
) )
} }

@ -1,5 +1,5 @@
'use client' 'use client'
import React, { useEffect, useState, useRef } from 'react' import React, { FC, useEffect, useState, useRef } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
import cn from 'classnames' import cn from 'classnames'
@ -22,8 +22,17 @@ import TabHeader from '../../base/tab-header'
import { XMarkIcon } from '@heroicons/react/24/outline' import { XMarkIcon } from '@heroicons/react/24/outline'
import s from './style.module.css' import s from './style.module.css'
import Button from '../../base/button' import Button from '../../base/button'
import { App } from '@/types/app'
const TextGeneration = () => { export type IMainProps = {
isInstalledApp?: boolean,
installedAppInfo? : App
}
const TextGeneration: FC<IMainProps> = ({
isInstalledApp = false,
installedAppInfo
}) => {
const { t } = useTranslation() const { t } = useTranslation()
const media = useBreakpoints() const media = useBreakpoints()
const isPC = media === MediaType.pc const isPC = media === MediaType.pc
@ -49,14 +58,14 @@ const TextGeneration = () => {
}) })
const handleFeedback = async (feedback: Feedbacktype) => { const handleFeedback = async (feedback: Feedbacktype) => {
await updateFeedback({ url: `/messages/${messageId}/feedbacks`, body: { rating: feedback.rating } }) await updateFeedback({ url: `/messages/${messageId}/feedbacks`, body: { rating: feedback.rating } }, isInstalledApp, installedAppInfo?.id)
setFeedback(feedback) setFeedback(feedback)
} }
const [savedMessages, setSavedMessages] = useState<SavedMessage[]>([]) const [savedMessages, setSavedMessages] = useState<SavedMessage[]>([])
const fetchSavedMessage = async () => { const fetchSavedMessage = async () => {
const res: any = await doFetchSavedMessage() const res: any = await doFetchSavedMessage(isInstalledApp, installedAppInfo?.id)
setSavedMessages(res.data) setSavedMessages(res.data)
} }
@ -65,13 +74,13 @@ const TextGeneration = () => {
}, []) }, [])
const handleSaveMessage = async (messageId: string) => { const handleSaveMessage = async (messageId: string) => {
await saveMessage(messageId) await saveMessage(messageId, isInstalledApp, installedAppInfo?.id)
notify({ type: 'success', message: t('common.api.saved') }) notify({ type: 'success', message: t('common.api.saved') })
fetchSavedMessage() fetchSavedMessage()
} }
const handleRemoveSavedMessage = async (messageId: string) => { const handleRemoveSavedMessage = async (messageId: string) => {
await removeMessage(messageId) await removeMessage(messageId, isInstalledApp, installedAppInfo?.id)
notify({ type: 'success', message: t('common.api.remove') }) notify({ type: 'success', message: t('common.api.remove') })
fetchSavedMessage() fetchSavedMessage()
} }
@ -151,12 +160,25 @@ const TextGeneration = () => {
onError() { onError() {
setResponsingFalse() setResponsingFalse()
} }
}) }, isInstalledApp, installedAppInfo?.id)
}
const fetchInitData = () => {
return Promise.all([isInstalledApp ? {
app_id: installedAppInfo?.id,
site: {
title: installedAppInfo?.name,
prompt_public: false,
copyright: ''
},
model_config: installedAppInfo?.app_model_config,
plan: 'basic',
}: fetchAppInfo(), fetchAppParams(isInstalledApp, installedAppInfo?.id)])
} }
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const [appData, appParams]: any = await Promise.all([fetchAppInfo(), fetchAppParams()]) const [appData, appParams]: any = await fetchInitData()
const { app_id: appId, site: siteInfo } = appData const { app_id: appId, site: siteInfo } = appData
setAppId(appId) setAppId(appId)
setSiteInfo(siteInfo as SiteInfo) setSiteInfo(siteInfo as SiteInfo)

@ -17,7 +17,7 @@ function getAction(action: 'get' | 'post' | 'del', isInstalledApp: boolean) {
} }
function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) { function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) {
return isInstalledApp ? `installed-apps/${installedAppId}/${url}` : url return isInstalledApp ? `installed-apps/${installedAppId}/${url.startsWith('/') ? url.slice(1) : url}` : url
} }
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController }: { export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController }: {

Loading…
Cancel
Save