From 51d359268e64142242446ea140b7874e1f69ff95 Mon Sep 17 00:00:00 2001 From: zxhlyh Date: Sun, 4 Feb 2024 16:10:46 +0800 Subject: [PATCH] chore: replace chat in web app (#2373) --- web/app/(shareLayout)/chat/[token]/page.tsx | 6 +- .../chat/chat-with-history/chat-wrapper.tsx | 141 +++++++ .../chat-with-history/config-panel/form.tsx | 82 ++++ .../chat-with-history/config-panel/index.tsx | 158 +++++++ .../base/chat/chat-with-history/context.tsx | 74 ++++ .../chat-with-history/header-in-mobile.tsx | 60 +++ .../base/chat/chat-with-history/header.tsx | 25 ++ .../base/chat/chat-with-history/hooks.tsx | 385 ++++++++++++++++++ .../base/chat/chat-with-history/index.tsx | 195 +++++++++ .../chat/chat-with-history/sidebar/index.tsx | 141 +++++++ .../chat/chat-with-history/sidebar/item.tsx | 58 +++ .../chat/chat-with-history/sidebar/list.tsx | 46 +++ .../base/chat/chat/answer/index.tsx | 2 +- .../base/chat/chat/answer/operation.tsx | 65 +++ .../chat/chat/answer/suggested-questions.tsx | 2 +- web/app/components/base/chat/chat/context.tsx | 3 + web/app/components/base/chat/chat/hooks.ts | 70 ++-- web/app/components/base/chat/chat/index.tsx | 110 +++-- .../components/base/chat/chat/try-to-ask.tsx | 2 +- web/app/components/base/chat/constants.ts | 1 + web/app/components/base/chat/types.ts | 9 + web/app/components/base/confirm/common.tsx | 3 + .../line/alertsAndFeedback/thumbs-down.svg | 10 + .../line/alertsAndFeedback/thumbs-up.svg | 10 + .../assets/vender/line/general/edit-05.svg | 10 + .../assets/vender/line/general/menu-01.svg | 5 + .../assets/vender/line/general/pin-01.svg | 5 + .../assets/vender/solid/shapes/star-06.svg | 9 + .../line/alertsAndFeedback/ThumbsDown.json | 66 +++ .../line/alertsAndFeedback/ThumbsDown.tsx | 16 + .../line/alertsAndFeedback/ThumbsUp.json | 66 +++ .../line/alertsAndFeedback/ThumbsUp.tsx | 16 + .../vender/line/alertsAndFeedback/index.ts | 2 + .../icons/src/vender/line/general/Edit05.json | 66 +++ .../icons/src/vender/line/general/Edit05.tsx | 16 + .../icons/src/vender/line/general/Menu01.json | 39 ++ .../icons/src/vender/line/general/Menu01.tsx | 16 + .../icons/src/vender/line/general/Pin01.json | 39 ++ .../icons/src/vender/line/general/Pin01.tsx | 16 + .../icons/src/vender/line/general/index.ts | 3 + .../icons/src/vender/solid/shapes/Star06.json | 62 +++ .../icons/src/vender/solid/shapes/Star06.tsx | 16 + .../icons/src/vender/solid/shapes/index.ts | 1 + web/app/components/explore/index.tsx | 2 +- .../explore/installed-app/index.tsx | 4 +- web/app/components/share/chat/index.tsx | 2 +- web/models/share.ts | 25 +- web/service/share.ts | 21 +- web/types/app.ts | 11 + 49 files changed, 2100 insertions(+), 92 deletions(-) create mode 100644 web/app/components/base/chat/chat-with-history/chat-wrapper.tsx create mode 100644 web/app/components/base/chat/chat-with-history/config-panel/form.tsx create mode 100644 web/app/components/base/chat/chat-with-history/config-panel/index.tsx create mode 100644 web/app/components/base/chat/chat-with-history/context.tsx create mode 100644 web/app/components/base/chat/chat-with-history/header-in-mobile.tsx create mode 100644 web/app/components/base/chat/chat-with-history/header.tsx create mode 100644 web/app/components/base/chat/chat-with-history/hooks.tsx create mode 100644 web/app/components/base/chat/chat-with-history/index.tsx create mode 100644 web/app/components/base/chat/chat-with-history/sidebar/index.tsx create mode 100644 web/app/components/base/chat/chat-with-history/sidebar/item.tsx create mode 100644 web/app/components/base/chat/chat-with-history/sidebar/list.tsx create mode 100644 web/app/components/base/chat/constants.ts create mode 100644 web/app/components/base/icons/assets/vender/line/alertsAndFeedback/thumbs-down.svg create mode 100644 web/app/components/base/icons/assets/vender/line/alertsAndFeedback/thumbs-up.svg create mode 100644 web/app/components/base/icons/assets/vender/line/general/edit-05.svg create mode 100644 web/app/components/base/icons/assets/vender/line/general/menu-01.svg create mode 100644 web/app/components/base/icons/assets/vender/line/general/pin-01.svg create mode 100644 web/app/components/base/icons/assets/vender/solid/shapes/star-06.svg create mode 100644 web/app/components/base/icons/src/vender/line/alertsAndFeedback/ThumbsDown.json create mode 100644 web/app/components/base/icons/src/vender/line/alertsAndFeedback/ThumbsDown.tsx create mode 100644 web/app/components/base/icons/src/vender/line/alertsAndFeedback/ThumbsUp.json create mode 100644 web/app/components/base/icons/src/vender/line/alertsAndFeedback/ThumbsUp.tsx create mode 100644 web/app/components/base/icons/src/vender/line/general/Edit05.json create mode 100644 web/app/components/base/icons/src/vender/line/general/Edit05.tsx create mode 100644 web/app/components/base/icons/src/vender/line/general/Menu01.json create mode 100644 web/app/components/base/icons/src/vender/line/general/Menu01.tsx create mode 100644 web/app/components/base/icons/src/vender/line/general/Pin01.json create mode 100644 web/app/components/base/icons/src/vender/line/general/Pin01.tsx create mode 100644 web/app/components/base/icons/src/vender/solid/shapes/Star06.json create mode 100644 web/app/components/base/icons/src/vender/solid/shapes/Star06.tsx diff --git a/web/app/(shareLayout)/chat/[token]/page.tsx b/web/app/(shareLayout)/chat/[token]/page.tsx index fbb9d5ade..6c3fe2b4a 100644 --- a/web/app/(shareLayout)/chat/[token]/page.tsx +++ b/web/app/(shareLayout)/chat/[token]/page.tsx @@ -1,12 +1,14 @@ +'use client' + import type { FC } from 'react' import React from 'react' import type { IMainProps } from '@/app/components/share/chat' -import Main from '@/app/components/share/chat' +import ChatWithHistoryWrap from '@/app/components/base/chat/chat-with-history' const Chat: FC = () => { return ( -
+ ) } diff --git a/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx b/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx new file mode 100644 index 000000000..1e58e37b6 --- /dev/null +++ b/web/app/components/base/chat/chat-with-history/chat-wrapper.tsx @@ -0,0 +1,141 @@ +import { useCallback, useEffect, useMemo } from 'react' +import Chat from '../chat' +import type { + ChatConfig, + OnSend, +} from '../types' +import { useChat } from '../chat/hooks' +import { useChatWithHistoryContext } from './context' +import Header from './header' +import ConfigPanel from './config-panel' +import { + fetchSuggestedQuestions, + getUrl, +} from '@/service/share' + +const ChatWrapper = () => { + const { + appParams, + appPrevChatList, + currentConversationId, + currentConversationItem, + inputsForms, + newConversationInputs, + handleNewConversationCompleted, + isMobile, + isInstalledApp, + appId, + appMeta, + handleFeedback, + currentChatInstanceRef, + } = useChatWithHistoryContext() + const appConfig = useMemo(() => { + const config = appParams || {} + + return { + ...config, + supportFeedback: true, + } as ChatConfig + }, [appParams]) + const { + chatList, + handleSend, + handleStop, + isResponsing, + suggestedQuestions, + } = useChat( + appConfig, + undefined, + appPrevChatList, + ) + + useEffect(() => { + if (currentChatInstanceRef.current) + currentChatInstanceRef.current.handleStop = handleStop + }, []) + + const doSend: OnSend = useCallback((message, files) => { + const data: any = { + query: message, + inputs: currentConversationId ? currentConversationItem?.inputs : newConversationInputs, + conversation_id: currentConversationId, + } + + if (appConfig?.file_upload?.image.enabled && files?.length) + data.files = files + + handleSend( + getUrl('chat-messages', isInstalledApp, appId || ''), + data, + { + onGetSuggestedQuestions: responseItemId => fetchSuggestedQuestions(responseItemId, isInstalledApp, appId), + onConversationComplete: currentConversationId ? undefined : handleNewConversationCompleted, + isPublicAPI: !isInstalledApp, + }, + ) + }, [ + appConfig, + currentConversationId, + currentConversationItem, + handleSend, + newConversationInputs, + handleNewConversationCompleted, + isInstalledApp, + appId, + ]) + const chatNode = useMemo(() => { + if (inputsForms.length) { + return ( + <> +
+ { + !currentConversationId && ( +
+
+ +
+
+ ) + } + + ) + } + + return ( +
+ ) + }, [ + currentConversationId, + inputsForms, + currentConversationItem, + isMobile, + ]) + + return ( + + ) +} + +export default ChatWrapper diff --git a/web/app/components/base/chat/chat-with-history/config-panel/form.tsx b/web/app/components/base/chat/chat-with-history/config-panel/form.tsx new file mode 100644 index 000000000..3940568e4 --- /dev/null +++ b/web/app/components/base/chat/chat-with-history/config-panel/form.tsx @@ -0,0 +1,82 @@ +import { useTranslation } from 'react-i18next' +import { useChatWithHistoryContext } from '../context' +import { PortalSelect } from '@/app/components/base/select' + +const Form = () => { + const { t } = useTranslation() + const { + inputsForms, + newConversationInputs, + handleNewConversationInputsChange, + isMobile, + } = useChatWithHistoryContext() + + const handleFormChange = (variable: string, value: string) => { + handleNewConversationInputsChange({ + ...newConversationInputs, + [variable]: value, + }) + } + + const renderField = (form: any) => { + const { + label, + required, + max_length, + variable, + options, + } = form + + if (form.type === 'text-input') { + return ( + handleFormChange(variable, e.target.value)} + placeholder={`${label}${!required ? `(${t('appDebug.variableTable.optional')})` : ''}`} + /> + ) + } + if (form.type === 'paragraph') { + return ( +