|
|
|
@ -14,12 +14,15 @@ import dynamic from 'next/dynamic'; |
|
|
|
|
import { useSearchParams } from 'next/navigation'; |
|
|
|
|
import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'; |
|
|
|
|
|
|
|
|
|
// 动态导入DbEditor组件,SSR设置为false
|
|
|
|
|
const DbEditor = dynamic(() => import('@/components/chat/db-editor'), { |
|
|
|
|
ssr: false, |
|
|
|
|
}); |
|
|
|
|
// 动态导入ChatContainer组件,SSR设置为false
|
|
|
|
|
const ChatContainer = dynamic(() => import('@/components/chat/chat-container'), { ssr: false }); |
|
|
|
|
const { Content } = Layout; |
|
|
|
|
|
|
|
|
|
// 定义ChatContentProps接口,用于ChatContentContext的类型
|
|
|
|
|
interface ChatContentProps { |
|
|
|
|
history: ChatHistoryResponse; // 会话记录列表
|
|
|
|
|
replyLoading: boolean; // 对话回复loading
|
|
|
|
@ -47,6 +50,8 @@ interface ChatContentProps { |
|
|
|
|
refreshAppInfo: () => void; |
|
|
|
|
setHistory: React.Dispatch<React.SetStateAction<ChatHistoryResponse>>; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 创建ChatContentContext上下文
|
|
|
|
|
export const ChatContentContext = createContext<ChatContentProps>({ |
|
|
|
|
history: [], |
|
|
|
|
replyLoading: false, |
|
|
|
@ -75,6 +80,7 @@ export const ChatContentContext = createContext<ChatContentProps>({ |
|
|
|
|
handleChat: () => Promise.resolve(), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// 定义Chat组件,为React函数组件
|
|
|
|
|
const Chat: React.FC = () => { |
|
|
|
|
const { model, currentDialogInfo } = useContext(ChatContext); |
|
|
|
|
const { isContract, setIsContract, setIsMenuExpand } = useContext(ChatContext); |
|
|
|
@ -103,6 +109,7 @@ const Chat: React.FC = () => { |
|
|
|
|
const [modelValue, setModelValue] = useState<string>(''); |
|
|
|
|
const { mode } = useContext(ChatContext); |
|
|
|
|
|
|
|
|
|
// 更新温度值、最大新令牌值、模型值和资源值
|
|
|
|
|
useEffect(() => { |
|
|
|
|
setTemperatureValue(appInfo?.param_need?.filter(item => item.type === 'temperature')[0]?.value || 0.6); |
|
|
|
|
setMaxNewTokensValue(appInfo?.param_need?.filter(item => item.type === 'max_new_tokens')[0]?.value || 4000); |
|
|
|
@ -112,16 +119,15 @@ const Chat: React.FC = () => { |
|
|
|
|
); |
|
|
|
|
}, [appInfo, dbName, knowledgeId, model]); |
|
|
|
|
|
|
|
|
|
// 根据不同的场景设置菜单展开状态和合约状态
|
|
|
|
|
useEffect(() => { |
|
|
|
|
// 仅初始化执行,防止dashboard页面无法切换状态
|
|
|
|
|
setIsMenuExpand(scene !== 'chat_dashboard'); |
|
|
|
|
// 路由变了要取消Editor模式,再进来是默认的Preview模式
|
|
|
|
|
if (chatId && scene) { |
|
|
|
|
setIsContract(false); |
|
|
|
|
} |
|
|
|
|
}, [chatId, scene]); |
|
|
|
|
|
|
|
|
|
// 是否是默认小助手
|
|
|
|
|
// 判断是否为默认小助手
|
|
|
|
|
const isChatDefault = useMemo(() => { |
|
|
|
|
return !chatId && !scene; |
|
|
|
|
}, [chatId, scene]); |
|
|
|
@ -152,12 +158,13 @@ const Chat: React.FC = () => { |
|
|
|
|
}, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// 列表当前活跃对话
|
|
|
|
|
// 获取当前活跃对话
|
|
|
|
|
const currentDialogue = useMemo(() => { |
|
|
|
|
const [, list] = dialogueList; |
|
|
|
|
return list?.find(item => item.conv_uid === chatId) || ({} as IChatDialogueSchema); |
|
|
|
|
}, [chatId, dialogueList]); |
|
|
|
|
|
|
|
|
|
// 判断是否需要查询应用信息
|
|
|
|
|
useEffect(() => { |
|
|
|
|
const initMessage = getInitMessage(); |
|
|
|
|
if (currentDialogInfo.chat_scene === scene && !isChatDefault && !(initMessage && initMessage.message)) { |
|
|
|
@ -182,7 +189,7 @@ const Chat: React.FC = () => { |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// 会话提问
|
|
|
|
|
// 处理会话逻辑
|
|
|
|
|
const handleChat = useCallback( |
|
|
|
|
(content: string, data?: Record<string, any>) => { |
|
|
|
|
return new Promise<void>(resolve => { |
|
|
|
@ -258,8 +265,8 @@ const Chat: React.FC = () => { |
|
|
|
|
[chatId, history, modelValue, chat, scene], |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// 初始化获取会话历史记录
|
|
|
|
|
useAsyncEffect(async () => { |
|
|
|
|
// 如果是默认小助手,不获取历史记录
|
|
|
|
|
if (isChatDefault) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
@ -270,6 +277,7 @@ const Chat: React.FC = () => { |
|
|
|
|
await getHistory(); |
|
|
|
|
}, [chatId, scene, getHistory]); |
|
|
|
|
|
|
|
|
|
// 重置会话历史记录和顺序
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if (isChatDefault) { |
|
|
|
|
order.current = 1; |
|
|
|
@ -277,7 +285,9 @@ const Chat: React.FC = () => { |
|
|
|
|
} |
|
|
|
|
}, [isChatDefault]); |
|
|
|
|
|
|
|
|
|
// 根据不同的场景渲染内容
|
|
|
|
|
const contentRender = () => { |
|
|
|
|
console.log('render content', scene, isChatDefault, isContract); |
|
|
|
|
if (scene === 'chat_dashboard') { |
|
|
|
|
return isContract ? <DbEditor /> : <ChatContainer />; |
|
|
|
|
} else { |
|
|
|
@ -296,6 +306,7 @@ const Chat: React.FC = () => { |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// 提供ChatContentContext上下文,包含会话相关的状态和方法
|
|
|
|
|
return ( |
|
|
|
|
<ChatContentContext.Provider |
|
|
|
|
value={{ |
|
|
|
@ -345,4 +356,4 @@ const Chat: React.FC = () => { |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export default Chat; |
|
|
|
|
export default Chat; |