From 3b94a536932288e1d3c5751e0a1db1f4ec954361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E4=BC=9F=E5=BC=BA?= Date: Thu, 18 May 2023 15:56:03 +0800 Subject: [PATCH] feat: chat support both in explore and share url --- .../app/text-generate/item/index.tsx | 4 +- .../explore/installed-app/index.tsx | 66 +++++++------------ web/app/components/share/chat/index.tsx | 40 +++++------ web/service/share.ts | 65 +++++++++++------- 4 files changed, 82 insertions(+), 93 deletions(-) diff --git a/web/app/components/app/text-generate/item/index.tsx b/web/app/components/app/text-generate/item/index.tsx index f63ede0cae..db4329724a 100644 --- a/web/app/components/app/text-generate/item/index.tsx +++ b/web/app/components/app/text-generate/item/index.tsx @@ -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 = ({ 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() diff --git a/web/app/components/explore/installed-app/index.tsx b/web/app/components/explore/installed-app/index.tsx index fa09501584..cc786f9e10 100644 --- a/web/app/components/explore/installed-app/index.tsx +++ b/web/app/components/explore/installed-app/index.tsx @@ -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 = ({ id, }) => { - const [app, setApp] = React.useState(isMock ? appDetail : null) - + const [app, setApp] = React.useState(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 ( +
+ +
+ ) + } + return (
- +
) } diff --git a/web/app/components/share/chat/index.tsx b/web/app/components/share/chat/index.tsx index 45784c1c06..d8c92f4687 100644 --- a/web/app/components/share/chat/index.tsx +++ b/web/app/components/share/chat/index.tsx @@ -31,7 +31,7 @@ export type IMainProps = { } const Main: FC = ({ - isInstalledApp, + isInstalledApp = false, installedAppInfo }) => { const { t } = useTranslation() @@ -130,7 +130,7 @@ const Main: FC = ({ // 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 = ({ } 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 = ({ } 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 = ({ 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 = ({ 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 { diff --git a/web/service/share.ts b/web/service/share.ts index 7623be5f80..dea35f36be 100644 --- a/web/service/share.ts +++ b/web/service/share.ts @@ -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, { 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, { 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)) }