Commit 1912c563 by Liu

fix:提问带入toolId

parent 31b09d74
...@@ -8,12 +8,12 @@ import SendIcon from '@/assets/svg/send.svg?react' ...@@ -8,12 +8,12 @@ import SendIcon from '@/assets/svg/send.svg?react'
import { type WithAuthProps, withAuth } from '@/auth/withAuth' import { type WithAuthProps, withAuth } from '@/auth/withAuth'
import { useAppDispatch, useAppSelector } from '@/store/hook' import { useAppDispatch, useAppSelector } from '@/store/hook'
import { fetchToolList } from '@/api/home' import { fetchToolList } from '@/api/home'
import { createConversation } from '@/store/conversationSlice' import { clearCurrentToolId, createConversation, setCurrentToolId } from '@/store/conversationSlice'
function getUserRoleFromRoute() { function getUserRoleFromRoute() {
try { try {
const searchParams = new URLSearchParams(window.location.search) const searchParams = new URLSearchParams(window.location.search)
return searchParams.get('userRole') || undefined return searchParams.get('userRoles') || undefined
} }
catch { catch {
return undefined return undefined
...@@ -47,8 +47,8 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -47,8 +47,8 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
// 获取工具列表 // 获取工具列表
const getToolList = async () => { const getToolList = async () => {
try { try {
const userRole = getUserRoleFromRoute() const userRoles = getUserRoleFromRoute()
const res = await fetchToolList(userRole ? { userRole } : undefined) const res = await fetchToolList(userRoles ? { userRoles } : undefined)
if (res?.data) { if (res?.data) {
// 根据 toolId 去重,防止重复渲染 // 根据 toolId 去重,防止重复渲染
const uniqueList = res.data.filter((tool: any, index: number, self: any[]) => const uniqueList = res.data.filter((tool: any, index: number, self: any[]) =>
...@@ -105,17 +105,13 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -105,17 +105,13 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
// 只在提质增效模式下传递 toolId,通用模式不传 // 只在提质增效模式下传递 toolId,通用模式不传
let toolId: string | undefined let toolId: string | undefined
if (!isToolBtnActive && selectedToolId) { if (selectedToolId) {
// 提质增效模式:使用选中的 toolId // 提质增效模式:使用选中的 toolId
toolId = selectedToolId toolId = selectedToolId
// eslint-disable-next-line no-console
console.log('📤 [ChatEditor] 提质增效模式提交,toolId:', toolId)
} }
else { else {
// 通用模式:不传递 toolId // 通用模式:不传递 toolId
toolId = undefined toolId = undefined
// eslint-disable-next-line no-console
console.log('📤 [ChatEditor] 通用模式提交,不传递 toolId')
} }
onSubmit?.(content.trim(), toolId) onSubmit?.(content.trim(), toolId)
setContent('') setContent('')
...@@ -148,14 +144,17 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -148,14 +144,17 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const handleGeneralClick = async () => { const handleGeneralClick = async () => {
if (!checkAuth()) if (!checkAuth())
return return
// 先更新 Redux,确保状态同步
dispatch(clearCurrentToolId())
// 立即更新本地状态,让 UI 立即响应
setIsToolBtnActive(true)
setSelectedToolId(null)
try { try {
await dispatch(createConversation({ await dispatch(createConversation({
conversationData: {}, conversationData: {},
shouldNavigate: true, shouldNavigate: true,
shouldSendQuestion: '', shouldSendQuestion: '',
})).unwrap() })).unwrap()
setIsToolBtnActive(true)
setSelectedToolId(null)
onToolClick?.(true, undefined, false) onToolClick?.(true, undefined, false)
} }
catch (error) { catch (error) {
...@@ -167,14 +166,15 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -167,14 +166,15 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const handleToolClick = async (tool: any) => { const handleToolClick = async (tool: any) => {
if (!checkAuth()) if (!checkAuth())
return return
dispatch(setCurrentToolId(tool.toolId))
setSelectedToolId(tool.toolId)
setIsToolBtnActive(false)
try { try {
await dispatch(createConversation({ await dispatch(createConversation({
conversationData: {}, conversationData: {},
shouldNavigate: true, shouldNavigate: true,
shouldSendQuestion: '', shouldSendQuestion: '',
})).unwrap() })).unwrap()
setSelectedToolId(tool.toolId)
setIsToolBtnActive(false)
onToolClick?.(false, tool.toolId, true) onToolClick?.(false, tool.toolId, true)
} }
catch (error) { catch (error) {
...@@ -288,7 +288,18 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -288,7 +288,18 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
<div className="absolute left-4 bottom-2 flex items-center gap-3 pointer-events-auto pl-[16px]"> <div className="absolute left-4 bottom-2 flex items-center gap-3 pointer-events-auto pl-[16px]">
{toolList.map((tool: any, index: number) => { {toolList.map((tool: any, index: number) => {
// tool.toolName === '通用模式' 的按钮(通用模式)在默认状态下高亮 // tool.toolName === '通用模式' 的按钮(通用模式)在默认状态下高亮
const isSelected = (selectedToolId === tool.toolId && !isToolBtnActive) || (tool.toolName === '通用模式' && isToolBtnActive) const isSelected = (selectedToolId === tool.toolId) || (tool.toolName === '通用模式' && isToolBtnActive)
const buttonStyles = isSelected
? {
backgroundColor: '#F3F7FF',
borderColor: '#F3F7FF',
color: '#165DFF',
}
: {
backgroundColor: '#FFFFFF',
borderColor: '#E6E8EB',
color: '#111827',
}
const handleButtonPress = async () => { const handleButtonPress = async () => {
// 高亮状态直接返回,避免重复触发 // 高亮状态直接返回,避免重复触发
if (isSelected) if (isSelected)
...@@ -301,14 +312,11 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -301,14 +312,11 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
return ( return (
<Button <Button
key={tool.toolId || `tool-${index}`} key={tool.toolId || `tool-${index}`}
className={`w-auto h-[32px] px-3 rounded-full shadow-none text-[12px] flex items-center gap-2 transition-all duration-200 ${ className="w-auto h-[32px] px-3 rounded-full shadow-none text-[12px] flex items-center gap-2 transition-all duration-200 border"
isSelected
? 'bg-[#F3F7FF] border-[#F3F7FF] text-[#165DFF]'
: 'bg-white border border-[#E6E8EB] text-[#111827]'
}`}
radius="full" radius="full"
variant="bordered" variant="bordered"
onPress={handleButtonPress} onPress={handleButtonPress}
style={buttonStyles}
> >
{tool.toolIcon && ( {tool.toolIcon && (
<img <img
......
...@@ -115,16 +115,7 @@ export const Chat: React.FC = () => { ...@@ -115,16 +115,7 @@ export const Chat: React.FC = () => {
/** 提交问题 */ /** 提交问题 */
const handleSubmitQuestion = async (question: string, productCode?: string, toolId?: string) => { const handleSubmitQuestion = async (question: string, productCode?: string, toolId?: string) => {
// 打印提交的参数 const resolvedToolId = toolId ?? currentToolId ?? undefined
// eslint-disable-next-line no-console
console.log('📤 [Chat] 提交问题:', {
question,
productCode,
toolId,
conversationId: currentIdRef.current,
模式: toolId ? '提质增效模式' : '通用模式',
})
// 停止之前的请求 // 停止之前的请求
if (abortControllerRef.current) { if (abortControllerRef.current) {
abortControllerRef.current.abort() abortControllerRef.current.abort()
...@@ -173,7 +164,7 @@ export const Chat: React.FC = () => { ...@@ -173,7 +164,7 @@ export const Chat: React.FC = () => {
conversationId: currentIdRef.current, conversationId: currentIdRef.current,
stream: true, stream: true,
productCode, productCode,
toolId, toolId: resolvedToolId,
}, },
(msg) => { (msg) => {
// 检查是否已被取消 // 检查是否已被取消
...@@ -235,11 +226,14 @@ export const Chat: React.FC = () => { ...@@ -235,11 +226,14 @@ export const Chat: React.FC = () => {
setAllItems(processedMessages) setAllItems(processedMessages)
const latestToolRecord = [...processedMessages].reverse().find(item => item.role === 'ai' && item.toolId !== undefined) const latestToolRecord = [...processedMessages].reverse().find(item => item.role === 'ai' && item.toolId !== undefined)
const trimmedToolId = latestToolRecord?.toolId?.trim?.() const trimmedToolId = latestToolRecord?.toolId?.trim?.()
if (trimmedToolId) { const hasQaRecords = processedMessages.some(item => item.role !== 'system')
dispatch(setCurrentToolId(trimmedToolId)) if (hasQaRecords) {
} if (trimmedToolId) {
else { dispatch(setCurrentToolId(trimmedToolId))
dispatch(clearCurrentToolId()) }
else {
dispatch(clearCurrentToolId())
}
} }
} }
catch { catch {
...@@ -297,14 +291,10 @@ export const Chat: React.FC = () => { ...@@ -297,14 +291,10 @@ export const Chat: React.FC = () => {
if (!isToolBtn && toolId) { if (!isToolBtn && toolId) {
// 提质增效模式,保存 toolId // 提质增效模式,保存 toolId
dispatch(setCurrentToolId(toolId)) dispatch(setCurrentToolId(toolId))
// eslint-disable-next-line no-console
console.log('🔧 [Chat] 切换到提质增效模式,toolId:', toolId)
} }
else { else {
// 通用模式,清除 toolId // 通用模式,清除 toolId
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
// eslint-disable-next-line no-console
console.log('🔄 [Chat] 切换到通用模式,清除 toolId')
} }
} }
window.addEventListener('toolButtonClick', handleToolClickEvent as EventListener) window.addEventListener('toolButtonClick', handleToolClickEvent as EventListener)
......
...@@ -119,14 +119,10 @@ export const Home: React.FC = () => { ...@@ -119,14 +119,10 @@ export const Home: React.FC = () => {
if (!isToolBtn && toolId) { if (!isToolBtn && toolId) {
// 提质增效模式,保存 toolId // 提质增效模式,保存 toolId
dispatch(setCurrentToolId(toolId)) dispatch(setCurrentToolId(toolId))
// eslint-disable-next-line no-console
console.log('✅ [Home] 已保存 toolId 到 Redux:', toolId)
} }
else { else {
// 通用模式,清除 toolId // 通用模式,清除 toolId
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
// eslint-disable-next-line no-console
console.log('🔄 [Home] 已清除 Redux 中的 toolId')
} }
_handleToolClick(isToolBtn, toolId) _handleToolClick(isToolBtn, toolId)
...@@ -242,7 +238,7 @@ export const Home: React.FC = () => { ...@@ -242,7 +238,7 @@ export const Home: React.FC = () => {
/> />
</motion.div> </motion.div>
{shouldChangeStyle && ( {shouldChangeStyle && (
<div> <div className="w-full flex justify-center mt-auto pb-[24px]">
<img src={SmartIce} alt="Smart Ice" className="w-[260px] h-[218px] mt-[-12px] object-contain" /> <img src={SmartIce} alt="Smart Ice" className="w-[260px] h-[218px] mt-[-12px] object-contain" />
</div> </div>
)} )}
......
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