Commit fad5869e by liimin

refactor(chat): 优化聊天组件配置和功能

- 移除不必要的注释代码
- 调整 SWR 请求配置,增加 revalidateOnFocus: false
- 优化 ChatProps 类型定义,注释掉未使用的属性
- 移除未使用的 Operation 组件属性
- 调整 ChatContextProvider 传递的属性
- 优化 chat-container 和 chat-footer 的样式
1 parent e04800df
{ {
"printWidth": 800, "printWidth": 80,
"tabWidth": 2, "tabWidth": 2,
"singleQuote": true, "singleQuote": true,
"trailingComma": "all", "trailingComma": "all",
"semi": false, "semi": false,
"wrap_line_length": 820, "wrap_line_length": 80,
"wrap_attributes": "auto", "wrap_attributes": "auto",
"proseWrap": "always", "proseWrap": "always",
"arrowParens": "avoid", "arrowParens": "avoid",
......
...@@ -46,7 +46,7 @@ function getFormattedChatList(messages: any[]) { ...@@ -46,7 +46,7 @@ function getFormattedChatList(messages: any[]) {
export const useChatWithHistory = (installedAppInfo?: InstalledApp, inputs: Record<string, any> = {}) => { export const useChatWithHistory = (installedAppInfo?: InstalledApp, inputs: Record<string, any> = {}) => {
const isInstalledApp = useMemo(() => !!installedAppInfo, [installedAppInfo]) const isInstalledApp = useMemo(() => !!installedAppInfo, [installedAppInfo])
const { data: appInfo, isLoading: appInfoLoading, error: appInfoError } = useSWR(installedAppInfo ? null : 'appInfo', fetchAppInfo) const { data: appInfo, isLoading: appInfoLoading, error: appInfoError } = useSWR(installedAppInfo ? null : 'appInfo', fetchAppInfo,{ revalidateOnFocus: false})
useAppFavicon({ useAppFavicon({
enable: !installedAppInfo, enable: !installedAppInfo,
icon_type: appInfo?.site.icon_type, icon_type: appInfo?.site.icon_type,
...@@ -141,13 +141,18 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp, inputs: Reco ...@@ -141,13 +141,18 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp, inputs: Reco
// // fetchChatList(chatShouldReloadKey, isInstalledApp, appId) // // fetchChatList(chatShouldReloadKey, isInstalledApp, appId)
// return {"limit": 100, "has_more": false, "data": []} // return {"limit": 100, "has_more": false, "data": []}
// }) // })
const { data: appParams } = useSWR(['appParams', isInstalledApp, appId], () => { const swrKey = useMemo(() => {
if (!appId) return null
return ['appParams', isInstalledApp, appId]
}, [isInstalledApp, appId])
const { data: appParams } = useSWR(swrKey, () => {
return fetchAppParams(isInstalledApp, appId) return fetchAppParams(isInstalledApp, appId)
}) },{ revalidateOnFocus: false})
const { data: appMeta } = useSWR(['appMeta', isInstalledApp, appId], () => { const { data: appMeta } = useSWR(swrKey, () => {
// fetchAppMeta(isInstalledApp, appId) // fetchAppMeta(isInstalledApp, appId)
return {tool_icons:{current_time: "/console/api/workspaces/current/tool-provider/builtin/time/icon"}} as AppMeta return {tool_icons:{current_time: "/console/api/workspaces/current/tool-provider/builtin/time/icon"}} as AppMeta
}) },{ revalidateOnFocus: false})
// const { data: appPinnedConversationData, mutate: mutateAppPinnedConversationData } = useSWR(['appConversationData', isInstalledApp, appId, true], () => { // const { data: appPinnedConversationData, mutate: mutateAppPinnedConversationData } = useSWR(['appConversationData', isInstalledApp, appId, true], () => {
// // fetchConversations(isInstalledApp, appId, undefined, true, 100) // // fetchConversations(isInstalledApp, appId, undefined, true, 100)
// return { limit: 100, has_more: false, data: [] } // return { limit: 100, has_more: false, data: [] }
...@@ -159,11 +164,11 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp, inputs: Reco ...@@ -159,11 +164,11 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp, inputs: Reco
} = useSWR(['appConversationData', isInstalledApp, appId, false], () => { } = useSWR(['appConversationData', isInstalledApp, appId, false], () => {
// fetchConversations(isInstalledApp, appId, undefined, false, 100) // fetchConversations(isInstalledApp, appId, undefined, false, 100)
return { data: [] } return { data: [] }
}) },{ revalidateOnFocus: false})
const { data: appChatListData, isLoading: appChatListDataLoading } = useSWR(chatShouldReloadKey ? ['appChatList', chatShouldReloadKey, isInstalledApp, appId] : null, () => { const { data: appChatListData, isLoading: appChatListDataLoading } = useSWR(chatShouldReloadKey ? ['appChatList', chatShouldReloadKey, isInstalledApp, appId] : null, () => {
// fetchChatList(chatShouldReloadKey, isInstalledApp, appId) // fetchChatList(chatShouldReloadKey, isInstalledApp, appId)
return { data: [] } return { data: [] }
}) },{ revalidateOnFocus: false})
const [clearChatList, setClearChatList] = useState(false) const [clearChatList, setClearChatList] = useState(false)
const [isResponding, setIsResponding] = useState(false) const [isResponding, setIsResponding] = useState(false)
......
...@@ -25,7 +25,7 @@ type OperationProps = { ...@@ -25,7 +25,7 @@ type OperationProps = {
const Operation: FC<OperationProps> = ({ item, isLast, question, index, showPromptLog, maxSize, contentWidth, hasWorkflowProcess, noChatInput }) => { const Operation: FC<OperationProps> = ({ item, isLast, question, index, showPromptLog, maxSize, contentWidth, hasWorkflowProcess, noChatInput }) => {
const { t } = useTranslation() const { t } = useTranslation()
const { config, onAnnotationAdded, onAnnotationEdited, onAnnotationRemoved, onFeedback, onRegenerate } = useChatContext() const { config, onFeedback, onRegenerate } = useChatContext()
const [isShowReplyModal, setIsShowReplyModal] = useState(false) const [isShowReplyModal, setIsShowReplyModal] = useState(false)
const [showLike, setShowLike] = useState(false) const [showLike, setShowLike] = useState(false)
......
...@@ -7,14 +7,14 @@ import type { ChatProps } from './index' ...@@ -7,14 +7,14 @@ import type { ChatProps } from './index'
export type ChatContextValue = Pick<ChatProps, 'config' export type ChatContextValue = Pick<ChatProps, 'config'
| 'isResponding' | 'isResponding'
| 'chatList' | 'chatList'
| 'showPromptLog' // | 'showPromptLog'
| 'questionIcon' | 'questionIcon'
| 'answerIcon' | 'answerIcon'
| 'onSend' | 'onSend'
| 'onRegenerate' | 'onRegenerate'
| 'onAnnotationEdited' // | 'onAnnotationEdited'
| 'onAnnotationAdded' // | 'onAnnotationAdded'
| 'onAnnotationRemoved' // | 'onAnnotationRemoved'
| 'onFeedback' | 'onFeedback'
> >
...@@ -31,14 +31,14 @@ export const ChatContextProvider = ({ ...@@ -31,14 +31,14 @@ export const ChatContextProvider = ({
config, config,
isResponding, isResponding,
chatList, chatList,
showPromptLog, // showPromptLog,
questionIcon, questionIcon,
answerIcon, answerIcon,
onSend, onSend,
onRegenerate, onRegenerate,
onAnnotationEdited, // onAnnotationEdited,
onAnnotationAdded, // onAnnotationAdded,
onAnnotationRemoved, // onAnnotationRemoved,
onFeedback, onFeedback,
}: ChatContextProviderProps) => { }: ChatContextProviderProps) => {
return ( return (
...@@ -46,14 +46,14 @@ export const ChatContextProvider = ({ ...@@ -46,14 +46,14 @@ export const ChatContextProvider = ({
config, config,
isResponding, isResponding,
chatList: chatList || [], chatList: chatList || [],
showPromptLog, // showPromptLog,
questionIcon, questionIcon,
answerIcon, answerIcon,
onSend, onSend,
onRegenerate, onRegenerate,
onAnnotationEdited, // onAnnotationEdited,
onAnnotationAdded, // onAnnotationAdded,
onAnnotationRemoved, // onAnnotationRemoved,
onFeedback, onFeedback,
}}> }}>
{children} {children}
......
...@@ -2,14 +2,21 @@ ...@@ -2,14 +2,21 @@
* @Author: liimin 17653212@qq.com * @Author: liimin 17653212@qq.com
* @Date: 2025-04-16 18:17:38 * @Date: 2025-04-16 18:17:38
* @LastEditors: limin * @LastEditors: limin
* @LastEditTime: 2025-06-26 17:50:10 * @LastEditTime: 2025-06-27 10:27:07
* @FilePath: \ai-assistant\app\components\base\chat\chat\index.tsx * @FilePath: \ai-assistant\app\components\base\chat\chat\index.tsx
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
import type { ReactNode } from 'react' import type { ReactNode } from 'react'
import React, { FC, memo, useCallback, useEffect, useRef } from 'react' import React, { FC, memo, useCallback, useEffect, useRef } from 'react'
// import { useShallow } from 'zustand/react/shallow' // import { useShallow } from 'zustand/react/shallow'
import type { ChatConfig, ChatItem, Feedback, InputMessageProp, OnRegenerate, OnSend } from '../types' import type {
ChatConfig,
ChatItem,
Feedback,
InputMessageProp,
OnRegenerate,
OnSend,
} from '../types'
import type { ThemeBuilder } from '../embedded-chatbot/theme/theme-context' import type { ThemeBuilder } from '../embedded-chatbot/theme/theme-context'
import Question from './question' import Question from './question'
import Answer from './answer' import Answer from './answer'
...@@ -44,18 +51,24 @@ export type ChatProps = { ...@@ -44,18 +51,24 @@ export type ChatProps = {
chatFooterClassName?: string chatFooterClassName?: string
chatFooterInnerClassName?: string chatFooterInnerClassName?: string
suggestedQuestions?: string[] suggestedQuestions?: string[]
showPromptLog?: boolean // showPromptLog?: boolean
questionIcon?: ReactNode questionIcon?: ReactNode
answerIcon?: ReactNode answerIcon?: ReactNode
allToolIcons?: Record<string, string | Emoji> allToolIcons?: Record<string, string | Emoji>
onAnnotationEdited?: (question: string, answer: string, index: number) => void // onAnnotationEdited?: (question: string, answer: string, index: number) => void
onAnnotationAdded?: (annotationId: string, authorName: string, question: string, answer: string, index: number) => void // onAnnotationAdded?: (
onAnnotationRemoved?: (index: number) => void // annotationId: string,
// authorName: string,
// question: string,
// answer: string,
// index: number,
// ) => void
// onAnnotationRemoved?: (index: number) => void
chatNode?: ReactNode chatNode?: ReactNode
onFeedback?: (messageId: string, feedback: Feedback) => void onFeedback?: (messageId: string, feedback: Feedback) => void
chatAnswerContainerInner?: string chatAnswerContainerInner?: string
hideProcessDetail?: boolean // hideProcessDetail?: boolean
hideLogModal?: boolean // hideLogModal?: boolean
themeBuilder?: ThemeBuilder themeBuilder?: ThemeBuilder
switchSibling?: (siblingMessageId: string) => void switchSibling?: (siblingMessageId: string) => void
showFeatureBar?: boolean showFeatureBar?: boolean
...@@ -64,11 +77,50 @@ export type ChatProps = { ...@@ -64,11 +77,50 @@ export type ChatProps = {
noSpacing?: boolean noSpacing?: boolean
inputDisabled?: boolean inputDisabled?: boolean
isMobile?: boolean isMobile?: boolean
sidebarCollapseState?: boolean // sidebarCollapseState?: boolean
isShowPracticeButton: boolean isShowPracticeButton: boolean
} }
const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsForm, onRegenerate, chatList, isResponding, noStopResponding, onStopResponding, noChatInput, chatContainerClassName, chatContainerInnerClassName, chatFooterClassName, chatFooterInnerClassName, suggestedQuestions, showPromptLog, questionIcon, answerIcon, onAnnotationAdded, onAnnotationEdited, onAnnotationRemoved, chatNode, onFeedback, chatAnswerContainerInner, hideProcessDetail, hideLogModal, themeBuilder, switchSibling, showFeatureBar, showFileUpload, onFeatureBarClick, noSpacing, inputDisabled, isMobile, sidebarCollapseState, isShowPracticeButton }) => { const Chat: FC<ChatProps> = ({
appData,
config,
onSend,
onClear,
inputs,
inputsForm,
onRegenerate,
chatList,
isResponding,
noStopResponding,
onStopResponding,
noChatInput,
chatContainerClassName,
chatContainerInnerClassName,
chatFooterClassName,
chatFooterInnerClassName,
suggestedQuestions,
// showPromptLog,
questionIcon,
answerIcon,
// onAnnotationAdded,
// onAnnotationEdited,
// onAnnotationRemoved,
chatNode,
onFeedback,
chatAnswerContainerInner,
// hideProcessDetail,
// hideLogModal,
themeBuilder,
switchSibling,
showFeatureBar,
showFileUpload,
onFeatureBarClick,
noSpacing,
inputDisabled,
isMobile,
// sidebarCollapseState,
isShowPracticeButton,
}) => {
// const { t } = useTranslation() // const { t } = useTranslation()
// const { currentLogItem, setCurrentLogItem, showPromptLogModal, setShowPromptLogModal, showAgentLogModal, setShowAgentLogModal } = useAppStore( // const { currentLogItem, setCurrentLogItem, showPromptLogModal, setShowPromptLogModal, showAgentLogModal, setShowAgentLogModal } = useAppStore(
// useShallow(state => ({ // useShallow(state => ({
...@@ -94,7 +146,10 @@ const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsF ...@@ -94,7 +146,10 @@ const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsF
if (!container) return if (!container) return
// 如果用户没有手动滚动,则直接到底 // 如果用户没有手动滚动,则直接到底
const isAtBottom = Math.abs(container.scrollHeight - container.clientHeight - container.scrollTop) <= 1 const isAtBottom =
Math.abs(
container.scrollHeight - container.clientHeight - container.scrollTop,
) <= 1
if (!userScrolledRef.current || isAtBottom) { if (!userScrolledRef.current || isAtBottom) {
// 添加节流控制 // 添加节流控制
...@@ -215,7 +270,8 @@ const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsF ...@@ -215,7 +270,8 @@ const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsF
if (!container) return if (!container) return
const handleScroll = () => { const handleScroll = () => {
const bottomThreshold = container.scrollHeight - container.clientHeight - 8 const bottomThreshold =
container.scrollHeight - container.clientHeight - 8
const isUserScroll = container.scrollTop < bottomThreshold const isUserScroll = container.scrollTop < bottomThreshold
userScrolledRef.current = isUserScroll userScrolledRef.current = isUserScroll
} }
...@@ -232,22 +288,97 @@ const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsF ...@@ -232,22 +288,97 @@ const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsF
[onSend], [onSend],
) )
return ( return (
<ChatContextProvider config={config} chatList={chatList} isResponding={isResponding} showPromptLog={showPromptLog} questionIcon={questionIcon} answerIcon={answerIcon} onSend={handleSend} onRegenerate={onRegenerate} onAnnotationAdded={onAnnotationAdded} onAnnotationEdited={onAnnotationEdited} onAnnotationRemoved={onAnnotationRemoved} onFeedback={onFeedback}> <ChatContextProvider
config={config}
chatList={chatList}
isResponding={isResponding}
// showPromptLog={showPromptLog}
questionIcon={questionIcon}
answerIcon={answerIcon}
onSend={handleSend}
onRegenerate={onRegenerate}
// onAnnotationAdded={onAnnotationAdded}
// onAnnotationEdited={onAnnotationEdited}
// onAnnotationRemoved={onAnnotationRemoved}
onFeedback={onFeedback}>
<div className="relative h-full"> <div className="relative h-full">
<div ref={chatContainerRef} className={cn('relative h-full overflow-y-auto overflow-x-hidden', chatContainerClassName, chatStyles.chatContainer)}> <div
ref={chatContainerRef}
className={cn(
'relative h-full overflow-y-auto overflow-x-hidden',
chatContainerClassName,
chatStyles.chatContainer,
)}>
{chatNode} {chatNode}
<div ref={chatContainerInnerRef} className={cn('w-full', !noSpacing && 'px-8', chatContainerInnerClassName)}> <div
{config?.initial_message && <InitQuestions config={config} answerIcon={answerIcon} chatAnswerContainerInner={chatAnswerContainerInner} />} ref={chatContainerInnerRef}
className={cn(
'w-full',
!noSpacing && 'px-8',
chatContainerInnerClassName,
)}>
{config?.initial_message && (
<InitQuestions
config={config}
answerIcon={answerIcon}
chatAnswerContainerInner={chatAnswerContainerInner}
/>
)}
{chatList.map((item, index) => { {chatList.map((item, index) => {
if (item.isAnswer) { if (item.isAnswer) {
const isLast = item.id === chatList[chatList.length - 1]?.id const isLast = item.id === chatList[chatList.length - 1]?.id
return <Answer appData={appData} key={item.id} item={item} question={chatList[index - 1]?.content} index={index} config={config} answerIcon={answerIcon} isLast={isLast} responding={isLast && isResponding} showPromptLog={showPromptLog} chatAnswerContainerInner={chatAnswerContainerInner} hideProcessDetail={hideProcessDetail} noChatInput={noChatInput} switchSibling={switchSibling} /> return (
<Answer
appData={appData}
key={item.id}
item={item}
question={chatList[index - 1]?.content}
index={index}
config={config}
answerIcon={answerIcon}
isLast={isLast}
responding={isLast && isResponding}
// showPromptLog={showPromptLog}
chatAnswerContainerInner={chatAnswerContainerInner}
// hideProcessDetail={hideProcessDetail}
noChatInput={noChatInput}
switchSibling={switchSibling}
/>
)
} }
return <Question key={item.id} item={item} questionIcon={questionIcon} theme={themeBuilder?.theme} /> return (
<Question
key={item.id}
item={item}
questionIcon={questionIcon}
theme={themeBuilder?.theme}
/>
)
})} })}
</div> </div>
</div> </div>
<Footer config={config} chatFooterRef={chatFooterRef} chatFooterInnerRef={chatFooterInnerRef} chatFooterClassName={chatFooterClassName} chatFooterInnerClassName={chatFooterInnerClassName} onSend={handleSend} inputs={inputs} inputsForm={inputsForm} onClear={onClear} noStopResponding={noStopResponding} onStopResponding={onStopResponding} noChatInput={noChatInput} suggestedQuestions={suggestedQuestions} showFeatureBar={showFeatureBar} showFileUpload={showFileUpload} onFeatureBarClick={onFeatureBarClick} inputDisabled={inputDisabled} isMobile={isMobile} isShowPracticeButton={isShowPracticeButton} isResponding={isResponding} handleSend={handleSend} /> <Footer
config={config}
chatFooterRef={chatFooterRef}
chatFooterInnerRef={chatFooterInnerRef}
chatFooterClassName={chatFooterClassName}
chatFooterInnerClassName={chatFooterInnerClassName}
onSend={handleSend}
inputs={inputs}
inputsForm={inputsForm}
onClear={onClear}
noStopResponding={noStopResponding}
onStopResponding={onStopResponding}
noChatInput={noChatInput}
suggestedQuestions={suggestedQuestions}
showFeatureBar={showFeatureBar}
showFileUpload={showFileUpload}
onFeatureBarClick={onFeatureBarClick}
inputDisabled={inputDisabled}
isMobile={isMobile}
isShowPracticeButton={isShowPracticeButton}
isResponding={isResponding}
/>
{/* {showPromptLogModal && !hideLogModal && ( {/* {showPromptLogModal && !hideLogModal && (
<PromptLogModal <PromptLogModal
width={width} width={width}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!