feat: chat support both in explore and share url

pull/198/head
金伟强 3 years ago
parent 1523684bb9
commit 3b94a53693

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

@ -2,60 +2,40 @@
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 { fetchAppDetail } from '@/service/explore'
import Loading from '@/app/components/base/loading'
export interface IInstalledAppProps { export interface IInstalledAppProps {
id: string 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> = ({ const InstalledApp: FC<IInstalledAppProps> = ({
id, 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(() => { useEffect(() => {
// TODO if(id) {
if(!isMock) { setIsLoaded(false);
(async () => {
const appDetail = await fetchAppDetail(id)
setApp(appDetail) setApp(appDetail)
setIsLoaded(true)
})()
} }
}) }, [id])
if(!isLoaded) {
return (
<div className='flex h-full items-center'>
<Loading type='area' />
</div>
)
}
return ( return (
<div className='h-full'> <div className='h-full'>
<ChatApp isInstalledApp installedAppInfo={appDetail}/> <ChatApp isInstalledApp installedAppInfo={app as App}/>
</div> </div>
) )
} }

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

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

Loading…
Cancel
Save