Commit 107247b0 by Liu

fix:不同工具展示不同欢迎语

parent 0db97af0
......@@ -12,6 +12,7 @@ import { ChatEditor } from '@/components/ChatEditor'
import type { ChatRecord } from '@/types/chat'
import { fetchUserQaRecordPage } from '@/api/conversation'
import { fetchCheckTokenApi, fetchStreamResponse } from '@/api/chat'
import { fetchToolList } from '@/api/home'
import { clearCurrentToolId, clearShouldSendQuestion, fetchConversations, setCurrentToolId } from '@/store/conversationSlice'
import type { RootState } from '@/store'
import { useAppDispatch, useAppSelector } from '@/store/hook'
......@@ -30,7 +31,7 @@ export const Chat: React.FC = () => {
const currentIdRef = useRef<string | undefined>(id)
const lastSentQuestionRef = useRef<string>('')
const abortControllerRef = useRef<AbortController | null>(null)
const [isEfficiencyMode, setIsEfficiencyMode] = useState(false)
const [currentToolName, setCurrentToolName] = useState<string | undefined>(undefined)
/** 处理正常stream的数据 */
const handleStreamMesageData = (msg: any, question: string) => {
......@@ -281,12 +282,60 @@ export const Chat: React.FC = () => {
}
}, [shouldSendQuestion, isLoading, currentToolId])
// 根据 currentToolId 获取对应的 toolName
useEffect(() => {
const getToolNameFromToolId = async () => {
if (currentToolId) {
try {
const userRoles: string[] = []
try {
const searchParams = new URLSearchParams(window.location.search)
const rolesFromRepeatedKeys = searchParams.getAll('userRoles').filter(Boolean)
if (rolesFromRepeatedKeys.length) {
userRoles.push(...Array.from(new Set(rolesFromRepeatedKeys)))
}
else {
const commaSeparated = searchParams.get('userRoles')
if (commaSeparated) {
const roles = commaSeparated
.split(',')
.map(role => role.trim())
.filter(Boolean)
if (roles.length) {
userRoles.push(...Array.from(new Set(roles)))
}
}
}
}
catch {
// 忽略错误
}
const res = await fetchToolList({ userRoles })
if (res?.data) {
const tool = res.data.find((t: any) => t.toolId === currentToolId)
if (tool?.toolName) {
setCurrentToolName(tool.toolName)
}
}
}
catch (error) {
console.error('获取工具列表失败:', error)
}
}
else {
// 通用模式
setCurrentToolName('通用模式')
}
}
getToolNameFromToolId()
}, [currentToolId])
// 监听工具按钮点击事件,更新 ChatWelcome 提示语和 toolId
useEffect(() => {
const handleToolClickEvent = (event: CustomEvent) => {
const { isToolBtn, toolId } = event.detail
// isToolBtn = true 表示通用模式,false 表示提质增效模式
setIsEfficiencyMode(!isToolBtn)
const { isToolBtn, toolId, toolName } = event.detail
// 保存当前选择的 toolName
setCurrentToolName(toolName)
// 保存当前选择的 toolId 到 Redux
if (!isToolBtn && toolId) {
// 提质增效模式,保存 toolId
......@@ -333,7 +382,7 @@ export const Chat: React.FC = () => {
record.question || record.answerList?.[0]?.answer || ''
}`}
>
{record.role === 'system' && <ChatWelcome isEfficiencyMode={isEfficiencyMode} />}
{record.role === 'system' && <ChatWelcome toolName={currentToolName} />}
{record.role === 'user' && <ChatItemUser record={record} />}
{record.role === 'ai' && (
<ChatAnswerBox
......@@ -368,10 +417,10 @@ export const Chat: React.FC = () => {
</div>
<ChatEditor
onSubmit={(question, toolId) => handleSubmitQuestion(question, undefined, toolId)}
onToolClick={(isToolBtn, toolId, shouldChangeStyle) => {
onToolClick={(isToolBtn, toolId, toolName, shouldChangeStyle) => {
// 发送自定义事件到父组件
window.dispatchEvent(new CustomEvent('toolButtonClick', {
detail: { isToolBtn, toolId, shouldChangeStyle },
detail: { isToolBtn, toolId, toolName, shouldChangeStyle },
}))
}}
placeholders={[]}
......
......@@ -4,18 +4,23 @@ import AvatarBot from '@/assets/avatarBot.png'
import AIIcon from '@/assets/ai-icon.png'
interface ChatWelcomeProps {
isEfficiencyMode?: boolean
toolName?: string
}
export const ChatWelcome: React.FC<ChatWelcomeProps> = ({ isEfficiencyMode = false }) => {
export const ChatWelcome: React.FC<ChatWelcomeProps> = ({ toolName }) => {
const viteOutputObj = import.meta.env.VITE_OUTPUT_OBJ || 'open'
// 根据模式显示不同的提示语
// 根据不同的 toolName 显示不同的提示语
const getWelcomeText = () => {
if (isEfficiencyMode) {
return 'HI~我是您的数据助手,可以帮你查询业务数据哦'
switch (toolName) {
case '数据助手':
return 'HI~我是您的数据助手,可以帮你查询业务数据哦'
case '提质增效':
return 'HI~我是您的提质增效助手,有什么可以帮您?'
case '通用模式':
default:
return '您好,有什么我可以帮您的吗?'
}
return '您好,有什么我可以帮您的吗?'
}
return (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment