feat: chat support both in explore and share url

This commit is contained in:
金伟强 2023-05-18 15:56:03 +08:00
parent 1523684bb9
commit 3b94a53693
4 changed files with 82 additions and 93 deletions

View File

@ -9,7 +9,7 @@ import Toast from '@/app/components/base/toast'
import { Feedbacktype } from '@/app/components/app/chat'
import { HandThumbDownIcon, HandThumbUpIcon } from '@heroicons/react/24/outline'
import { useBoolean } from 'ahooks'
import { fetcMoreLikeThis, updateFeedback } from '@/service/share'
import { fetchMoreLikeThis, updateFeedback } from '@/service/share'
const MAX_DEPTH = 3
export interface IGenerationItemProps {
@ -113,7 +113,7 @@ const GenerationItem: FC<IGenerationItemProps> = ({
return
}
startQuerying()
const res: any = await fetcMoreLikeThis(messageId as string)
const res: any = await fetchMoreLikeThis(messageId as string)
setCompletionRes(res.answer)
setChildMessageId(res.id)
stopQuerying()

View File

@ -2,60 +2,40 @@
import React, { FC, useEffect } from 'react'
import { App } from '@/types/app'
import ChatApp from '@/app/components/share/chat'
import { fetchAppDetail } from '@/service/explore'
import Loading from '@/app/components/base/loading'
export interface IInstalledAppProps {
id: string
}
const isMock = true
const appDetail = {
"id": "4dcc2bac-0a48-4633-8e0b-0f4335669335",
"name": "Interviewer",
"mode": "chat",
"icon": null,
"icon_background": null,
"app_model_config": {
"opening_statement": null,
"suggested_questions": [],
"suggested_questions_after_answer": {
"enabled": false
},
"more_like_this": {
"enabled": false
},
"model": {
"provider": "openai",
"name": "gpt-3.5-turbo",
"completion_params": {
"max_tokens": 512,
"temperature": 1,
"top_p": 1,
"presence_penalty": 0,
"frequency_penalty": 0
}
},
"user_input_form": [],
"pre_prompt": null,
"agent_mode": {
"enabled": false,
"tools": []
}
},
} as any
const InstalledApp: FC<IInstalledAppProps> = ({
id,
}) => {
const [app, setApp] = React.useState<App | null>(isMock ? appDetail : null)
const [app, setApp] = React.useState<App | null>(null)
const [isLoaded, setIsLoaded] = React.useState(false)
useEffect(() => {
// TODO
if(!isMock) {
setApp(appDetail)
if(id) {
setIsLoaded(false);
(async () => {
const appDetail = await fetchAppDetail(id)
setApp(appDetail)
setIsLoaded(true)
})()
}
})
}, [id])
if(!isLoaded) {
return (
<div className='flex h-full items-center'>
<Loading type='area' />
</div>
)
}
return (
<div className='h-full'>
<ChatApp isInstalledApp installedAppInfo={appDetail}/>
<ChatApp isInstalledApp installedAppInfo={app as App}/>
</div>
)
}

View File

@ -31,7 +31,7 @@ export type IMainProps = {
}
const Main: FC<IMainProps> = ({
isInstalledApp,
isInstalledApp = false,
installedAppInfo
}) => {
const { t } = useTranslation()
@ -130,7 +130,7 @@ const Main: FC<IMainProps> = ({
// update chat list of current conversation
if (!isNewConversation && !conversationIdChangeBecauseOfNew && !isResponsing) {
fetchChatList(currConversationId).then((res: any) => {
fetchChatList(currConversationId, isInstalledApp, installedAppInfo?.id).then((res: any) => {
const { data } = res
const newChatList: IChatItem[] = generateNewChatListWithOpenstatement(notSyncToStateIntroduction, notSyncToStateInputs)
@ -224,24 +224,16 @@ const Main: FC<IMainProps> = ({
}
const fetchInitData = () => {
if(isInstalledApp) {
return new Promise((resolve) => {
// TODO: fetchConversations
resolve([{
app_id: installedAppInfo?.id,
site: {
title: installedAppInfo?.name,
prompt_public: false,
copyright: ''
},
model_config: installedAppInfo?.app_model_config,
plan: 'basic',
}, {
data: []
}, installedAppInfo?.app_model_config])
})
}
return Promise.all([fetchAppInfo(), fetchConversations(), fetchAppParams()])
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(), fetchConversations(isInstalledApp, installedAppInfo?.id), fetchAppParams(isInstalledApp, installedAppInfo?.id)])
}
@ -402,7 +394,7 @@ const Main: FC<IMainProps> = ({
}
let currChatList = conversationList
if (getConversationIdChangeBecauseOfNew()) {
const { data: conversations }: any = await fetchConversations()
const { data: conversations }: any = await fetchConversations(isInstalledApp, installedAppInfo?.id)
setConversationList(conversations as ConversationItem[])
currChatList = conversations
}
@ -411,7 +403,7 @@ const Main: FC<IMainProps> = ({
setChatNotStarted()
setCurrConversationId(tempNewConversationId, appId, true)
if (suggestedQuestionsAfterAnswerConfig?.enabled) {
const { data }: any = await fetchSuggestedQuestions(responseItem.id)
const { data }: any = await fetchSuggestedQuestions(responseItem.id, isInstalledApp, installedAppInfo?.id)
setSuggestQuestions(data)
setIsShowSuggestion(true)
}
@ -423,11 +415,11 @@ const Main: FC<IMainProps> = ({
draft.splice(draft.findIndex(item => item.id === placeholderAnswerId), 1)
}))
},
})
}, isInstalledApp, installedAppInfo?.id)
}
const handleFeedback = async (messageId: string, 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)
const newChatList = chatList.map((item) => {
if (item.id === messageId) {
return {

View File

@ -1,14 +1,32 @@
import type { IOnCompleted, IOnData, IOnError } from './base'
import { getPublic as get, postPublic as post, ssePost, delPublic as del } from './base'
import {
get as consoleGet, post as consolePost, del as consoleDel,
getPublic as get, postPublic as post, ssePost, delPublic as del
} from './base'
import type { Feedbacktype } from '@/app/components/app/chat'
function getAction(action: 'get' | 'post' | 'del', isInstalledApp: boolean) {
switch (action) {
case 'get':
return isInstalledApp ? consoleGet : get
case 'post':
return isInstalledApp ? consolePost : post
case 'del':
return isInstalledApp ? consoleDel : del
}
}
function getUrl(url: string, isInstalledApp: boolean, installedAppId: string) {
return isInstalledApp ? `installed-apps/${installedAppId}/${url}` : url
}
export const sendChatMessage = async (body: Record<string, any>, { onData, onCompleted, onError, getAbortController }: {
onData: IOnData
onCompleted: IOnCompleted
onError: IOnError,
getAbortController?: (abortController: AbortController) => void
}) => {
return ssePost('chat-messages', {
}, isInstalledApp: boolean, installedAppId = '') => {
return ssePost(getUrl('chat-messages', isInstalledApp, installedAppId), {
body: {
...body,
response_mode: 'streaming',
@ -20,8 +38,8 @@ export const sendCompletionMessage = async (body: Record<string, any>, { onData,
onData: IOnData
onCompleted: IOnCompleted
onError: IOnError
}) => {
return ssePost('completion-messages', {
}, isInstalledApp: boolean, installedAppId = '') => {
return ssePost(getUrl('completion-messages', isInstalledApp, installedAppId), {
body: {
...body,
response_mode: 'streaming',
@ -33,12 +51,12 @@ export const fetchAppInfo = async () => {
return get('/site')
}
export const fetchConversations = async () => {
return get('conversations', { params: { limit: 20, first_id: '' } })
export const fetchConversations = async (isInstalledApp: boolean, installedAppId='') => {
return getAction('get', isInstalledApp)(getUrl('conversations', isInstalledApp, installedAppId), { params: { limit: 20, first_id: '' } })
}
export const fetchChatList = async (conversationId: string) => {
return get('messages', { params: { conversation_id: conversationId, limit: 20, last_id: '' } })
export const fetchChatList = async (conversationId: string, isInstalledApp: boolean, installedAppId='') => {
return getAction('get', isInstalledApp)(getUrl('messages', isInstalledApp, installedAppId), { params: { conversation_id: conversationId, limit: 20, last_id: '' } })
}
// Abandoned API interface
@ -47,35 +65,34 @@ export const fetchChatList = async (conversationId: string) => {
// }
// init value. wait for server update
export const fetchAppParams = async () => {
return get('parameters')
export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '') => {
return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId))
}
export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }) => {
return post(url, { body })
export const updateFeedback = async ({ url, body }: { url: string; body: Feedbacktype }, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('post', isInstalledApp))(getUrl(url, isInstalledApp, installedAppId), { body })
}
export const fetcMoreLikeThis = async (messageId: string) => {
return get(`/messages/${messageId}/more-like-this`, {
export const fetchMoreLikeThis = async (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/more-like-this`, isInstalledApp, installedAppId), {
params: {
response_mode: 'blocking',
}
})
}
export const saveMessage = (messageId: string) => {
return post('/saved-messages', { body: { message_id: messageId } })
export const saveMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('post', isInstalledApp))(getUrl('/saved-messages', isInstalledApp, installedAppId), { body: { message_id: messageId } })
}
export const fetchSavedMessage = async () => {
return get(`/saved-messages`)
export const fetchSavedMessage = async (isInstalledApp: boolean, installedAppId = '') => {
return (getAction('get', isInstalledApp))(getUrl(`/saved-messages`, isInstalledApp, installedAppId))
}
export const removeMessage = (messageId: string) => {
return del(`/saved-messages/${messageId}`)
export const removeMessage = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('del', isInstalledApp))(getUrl(`/saved-messages/${messageId}`, isInstalledApp, installedAppId))
}
export const fetchSuggestedQuestions = (messageId: string) => {
return get(`/messages/${messageId}/suggested-questions`)
export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boolean, installedAppId = '') => {
return (getAction('get', isInstalledApp))(getUrl(`/messages/${messageId}/suggested-questions`, isInstalledApp, installedAppId))
}