Commit b9deab47 by Liu

feat:创建会话时增加参数

parent 2b1e4e52
......@@ -42,7 +42,6 @@ export const TacticsChat: React.FC = () => {
} = useAppSelector((state: RootState) => state.tactics)
const [searchParams] = useSearchParams()
const hasFetched = useRef(false)
const hasCreatedRef = useRef(false)
// 使用 useLocalStorageState 管理 token,与原有逻辑保持一致
const [token, setToken] = useLocalStorageState<string | undefined>('__TOKEN__', {
defaultValue: '',
......@@ -122,6 +121,23 @@ export const TacticsChat: React.FC = () => {
}, [])
const userMeta = useMemo(() => userMetaFromState || userMetaFromQuery || userMetaFromStorage, [userMetaFromState, userMetaFromQuery, userMetaFromStorage])
// 仅用于创建会话的额外参数(create_conversation)
const getConversationExtra = useCallback(() => {
if (userMeta?.place === 'user') {
return {
description: '02',
busiId: userMeta.userId,
}
}
if (tacticsMeta) {
return {
description: '02',
busiId: tacticsMeta.taskId,
}
}
return {}
}, [tacticsMeta, userMeta])
// 根据 userMeta.numberType 与调用类型(自动/正常)计算请求参数用的 numberType(A03/A04...)
const getNumberTypeWithUserMeta = useCallback(
(defaultNumberType: string, isAuto: boolean): string => {
......@@ -168,24 +184,6 @@ export const TacticsChat: React.FC = () => {
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
}
// 创建新会话(仅在 tactics 聊天页面且没有 id 时调用)
const initTacticsConversation = useCallback(() => {
// 只有在 tactics 聊天页面且没有会话 id 时才创建新对话
if (!id && location.pathname.startsWith('/tactics/chat')) {
if (hasCreatedRef.current) {
return
}
hasCreatedRef.current = true
dispatch(
createTacticsConversation({
conversationData: {},
shouldNavigate: true,
shouldSendQuestion: '',
}),
)
}
}, [id, location.pathname, dispatch])
// 登录逻辑(复用原有逻辑,与 TacticsHome.tsx 保持一致)
const login = useCallback(async () => {
if (hasFetched.current) {
......@@ -210,14 +208,10 @@ export const TacticsChat: React.FC = () => {
storageArea: localStorage,
}),
)
// 登录成功后,如果是 tactics 聊天页面且没有 id,则创建会话
initTacticsConversation()
dispatch(fetchTacticsConversations())
}
}
else {
// 如果没有 loginCode,但已有 token,直接尝试创建会话
initTacticsConversation()
dispatch(fetchTacticsConversations())
}
}
......@@ -234,12 +228,10 @@ export const TacticsChat: React.FC = () => {
storageArea: localStorage,
}),
)
// 登录成功后,如果是 tactics 聊天页面且没有 id,则创建会话
initTacticsConversation()
dispatch(fetchTacticsConversations())
}
}
}, [setToken, dispatch, token, initTacticsConversation, viteOutputObj])
}, [setToken, dispatch, token, viteOutputObj])
/** 处理正常stream的数据 */
const handleStreamMesageData = (msg: any, question: string) => {
......@@ -389,6 +381,15 @@ export const TacticsChat: React.FC = () => {
else if (tacticsMeta && shouldIncludeTacticsMeta) {
Object.assign(requestBody, safeTacticsMeta)
}
// from=tactics 场景下补充业务识别参数
if (userMeta?.place === 'user') {
requestBody.description ??= '02'
requestBody.busiId ??= userMeta.userId
}
else if (tacticsMeta) {
requestBody.description ??= '02'
requestBody.busiId ??= tacticsMeta.taskId
}
if (shouldSendQuestion) {
requestBody.question = question ?? ''
}
......@@ -500,7 +501,7 @@ export const TacticsChat: React.FC = () => {
setHistoryDividerTime(null)
dispatch(
createTacticsConversation({
conversationData: {},
conversationData: getConversationExtra(),
shouldNavigate: false,
shouldSendQuestion: '',
}),
......@@ -509,7 +510,7 @@ export const TacticsChat: React.FC = () => {
catch (error) {
console.error('清除记录失败:', error)
}
}, [dispatch])
}, [dispatch, getConversationExtra])
const handleReanalyze = useCallback(async () => {
// 重新触发一次提问,与首次自动调用保持一致:
......
......@@ -40,6 +40,8 @@ export const TacticsHome: React.FC = () => {
partOrAll: searchParams.get('partOrAll') || undefined,
channel: searchParams.get('channel') || undefined,
channelName: searchParams.get('channelName') || undefined,
description: '02',
busiId: searchParams.get('taskId') || undefined,
}
}, [searchParams])
const tacticsMetaFromStorage = useMemo(() => {
......@@ -71,6 +73,8 @@ export const TacticsHome: React.FC = () => {
cstId: searchParams.get('cstId') || undefined,
userId: searchParams.get('userId') || undefined,
numberType: searchParams.get('numberType') || undefined,
description: '02',
busiId: searchParams.get('userId') || undefined,
}
}, [searchParams])
const userMetaFromStorage = useMemo(() => {
......@@ -91,6 +95,23 @@ export const TacticsHome: React.FC = () => {
}, [])
const userMeta = useMemo(() => userMetaFromSearch || userMetaFromStorage, [userMetaFromSearch, userMetaFromStorage])
// 仅用于创建会话的额外参数(对应 create_conversation)
const getConversationExtra = useCallback(() => {
if (userMeta?.place === 'user') {
return {
description: '02',
busiId: userMeta.userId,
}
}
if (tacticsMeta) {
return {
description: '02',
busiId: tacticsMeta.taskId,
}
}
return {}
}, [tacticsMeta, userMeta])
// 同步到 sessionStorage,便于跳转后读取;仅在有 meta 时写入,避免覆盖重定向缓存
useEffect(() => {
if (tacticsMeta) {
......@@ -111,7 +132,7 @@ export const TacticsHome: React.FC = () => {
if (!fromCollect && location.pathname === '/tactics') {
dispatch(
createTacticsConversation({
conversationData: {},
conversationData: getConversationExtra(),
shouldNavigate: true,
shouldSendQuestion: '',
}),
......@@ -139,12 +160,12 @@ export const TacticsHome: React.FC = () => {
// 如果没有会话,才创建新会话
dispatch(
createTacticsConversation({
conversationData: {},
conversationData: getConversationExtra(),
shouldNavigate: true,
shouldSendQuestion: question,
}),
)
}, [dispatch, currentConversationId, navigate, tacticsMeta, userMeta])
}, [dispatch, currentConversationId, navigate, tacticsMeta, userMeta, getConversationExtra])
// 监听导航到新对话
useEffect(() => {
......
......@@ -29,23 +29,27 @@ export function withRouteChangeHandler(WrappedComponent: React.ComponentType) {
try {
if (place) {
const userMeta = {
cstId: searchParams.get('cstId') || undefined,
userId: searchParams.get('userId') || undefined,
numberType: searchParams.get('numberType') || undefined,
place,
cstId: searchParams.get('cstId') || undefined,
userId: searchParams.get('userId') || undefined,
numberType: searchParams.get('numberType') || undefined,
place,
description: '02',
busiId: searchParams.get('userId') || undefined,
}
sessionStorage.setItem('userMeta', JSON.stringify(userMeta))
}
else {
else {
const meta = {
version: searchParams.get('version') || undefined,
taskId: searchParams.get('taskId') || undefined,
pinBeginTime: searchParams.get('pinBeginTime') || undefined,
pinEndTime: searchParams.get('pinEndTime') || undefined,
partOrAll: searchParams.get('partOrAll') || undefined,
channel: searchParams.get('channel') || undefined,
channelName: searchParams.get('channelName') || undefined,
from,
version: searchParams.get('version') || undefined,
taskId: searchParams.get('taskId') || undefined,
pinBeginTime: searchParams.get('pinBeginTime') || undefined,
pinEndTime: searchParams.get('pinEndTime') || undefined,
partOrAll: searchParams.get('partOrAll') || undefined,
channel: searchParams.get('channel') || undefined,
channelName: searchParams.get('channelName') || undefined,
from,
description: '02',
busiId: searchParams.get('taskId') || undefined,
}
sessionStorage.setItem('tacticsMeta', JSON.stringify(meta))
}
......
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