feat: text generaion support both url

This commit is contained in:
金伟强 2023-05-18 16:50:42 +08:00
parent 8208652607
commit 9a167aa688
3 changed files with 38 additions and 10 deletions

View File

@ -2,6 +2,7 @@
import React, { FC, useEffect } from 'react'
import { App } from '@/types/app'
import ChatApp from '@/app/components/share/chat'
import TextGenerationApp from '@/app/components/share/text-generation'
import { fetchAppDetail } from '@/service/explore'
import Loading from '@/app/components/base/loading'
@ -35,7 +36,12 @@ const InstalledApp: FC<IInstalledAppProps> = ({
return (
<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>
)
}

View File

@ -1,5 +1,5 @@
'use client'
import React, { useEffect, useState, useRef } from 'react'
import React, { FC, useEffect, useState, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
import cn from 'classnames'
@ -22,8 +22,17 @@ import TabHeader from '../../base/tab-header'
import { XMarkIcon } from '@heroicons/react/24/outline'
import s from './style.module.css'
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 media = useBreakpoints()
const isPC = media === MediaType.pc
@ -49,14 +58,14 @@ const TextGeneration = () => {
})
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)
}
const [savedMessages, setSavedMessages] = useState<SavedMessage[]>([])
const fetchSavedMessage = async () => {
const res: any = await doFetchSavedMessage()
const res: any = await doFetchSavedMessage(isInstalledApp, installedAppInfo?.id)
setSavedMessages(res.data)
}
@ -65,13 +74,13 @@ const TextGeneration = () => {
}, [])
const handleSaveMessage = async (messageId: string) => {
await saveMessage(messageId)
await saveMessage(messageId, isInstalledApp, installedAppInfo?.id)
notify({ type: 'success', message: t('common.api.saved') })
fetchSavedMessage()
}
const handleRemoveSavedMessage = async (messageId: string) => {
await removeMessage(messageId)
await removeMessage(messageId, isInstalledApp, installedAppInfo?.id)
notify({ type: 'success', message: t('common.api.remove') })
fetchSavedMessage()
}
@ -151,12 +160,25 @@ const TextGeneration = () => {
onError() {
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(() => {
(async () => {
const [appData, appParams]: any = await Promise.all([fetchAppInfo(), fetchAppParams()])
const [appData, appParams]: any = await fetchInitData()
const { app_id: appId, site: siteInfo } = appData
setAppId(appId)
setSiteInfo(siteInfo as SiteInfo)

View File

@ -17,7 +17,7 @@ function getAction(action: 'get' | 'post' | 'del', isInstalledApp: boolean) {
}
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 }: {