修复访问他人会话时提示无权访问并卡死的问题

- 在 useChatData 中捕获加载历史消息(ChatAPI.history)时的权限和不存在错误
- 发生 500000 无权访问等错误时,触发 roomInvalidTick 状态更新
- ChatPageLogic 监听到失效状态后,自动调用 SessionAPI.create 创建新的会话房间并重定向,同时给予提示
main
sp mac bookpro 2605 2026-08-02 15:39:46 +08:00
parent ecbd687983
commit bb4c974b74
2 changed files with 36 additions and 8 deletions

View File

@ -105,7 +105,7 @@ export function useChatPageLogic() {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [id, roomId]); }, [id, roomId]);
const { agent, agentList, messages, setMessages, branches, setBranches, loadMessages } = useChatData({ const { agent, agentList, messages, setMessages, branches, setBranches, loadMessages, roomInvalidTick } = useChatData({
agentId: id, agentId: id,
roomId, roomId,
highlightId, highlightId,
@ -116,6 +116,21 @@ export function useChatPageLogic() {
abort: () => abortRef.current?.abort(), abort: () => abortRef.current?.abort(),
}); });
// 监听会话失效状态,自动创建新会话
useEffect(() => {
if (roomInvalidTick > 0 && id) {
SessionAPI.create(id).then((created) => {
setRoomId(created.id);
setHighlightId(null);
setMessages(() => []);
setBranches({});
message.info('当前会话不可访问,已为您创建新会话');
}).catch((e) => {
message.error('创建房间失败:' + (e?.message ?? e));
});
}
}, [roomInvalidTick, id]);
const sender = useChatSender({ const sender = useChatSender({
agentId: id, agentId: id,
agent, agent,

View File

@ -20,6 +20,7 @@ export function useChatData(args: {
const [agentList, setAgentList] = useState<Agent[]>([]); const [agentList, setAgentList] = useState<Agent[]>([]);
const [messages, setMessages] = useState<ChatMessage[]>([]); const [messages, setMessages] = useState<ChatMessage[]>([]);
const [branches, setBranches] = useState<Record<string, BranchInfo>>({}); const [branches, setBranches] = useState<Record<string, BranchInfo>>({});
const [roomInvalidTick, setRoomInvalidTick] = useState(0);
const loadSeqRef = useRef(0); const loadSeqRef = useRef(0);
const initialHighlightDoneRef = useRef(false); const initialHighlightDoneRef = useRef(false);
@ -60,11 +61,22 @@ export function useChatData(args: {
if (!roomId) return; if (!roomId) return;
const seq = ++loadSeqRef.current; const seq = ++loadSeqRef.current;
const rid = roomId; const rid = roomId;
const his = await ChatAPI.history(rid); try {
if (seq !== loadSeqRef.current) return; const his = await ChatAPI.history(rid);
if (rid !== roomId) return; if (seq !== loadSeqRef.current) return;
setMessages(Array.isArray(his.messages) ? his.messages : []); if (rid !== roomId) return;
setBranches(his.branches || {}); setMessages(Array.isArray(his.messages) ? his.messages : []);
setBranches(his.branches || {});
} catch (e: any) {
// 会话不存在或无权访问时,通知上层重新创建会话
if (seq === loadSeqRef.current && rid === roomId) {
const code = e?.response?.data?.code;
const msg = e?.response?.data?.error ?? e?.message ?? '';
if (code === '500000' || /无权访问|不存在/.test(msg)) {
setRoomInvalidTick((t) => t + 1);
}
}
}
// 历史消息加载完成后的处理 // 历史消息加载完成后的处理
// 注意:这里不再手动触发滚动,全部交给 ChatBody 的 useLayoutEffect 和 useEffect 处理 // 注意:这里不再手动触发滚动,全部交给 ChatBody 的 useLayoutEffect 和 useEffect 处理
@ -132,6 +144,7 @@ export function useChatData(args: {
setMessages, setMessages,
branches, branches,
setBranches, setBranches,
loadMessages loadMessages,
roomInvalidTick
}; };
} }