Commit 1912c563 by Liu

fix:提问带入toolId

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