修复访问他人会话时提示无权访问并卡死的问题
- 在 useChatData 中捕获加载历史消息(ChatAPI.history)时的权限和不存在错误 - 发生 500000 无权访问等错误时,触发 roomInvalidTick 状态更新 - ChatPageLogic 监听到失效状态后,自动调用 SessionAPI.create 创建新的会话房间并重定向,同时给予提示main
parent
ecbd687983
commit
bb4c974b74
|
|
@ -105,7 +105,7 @@ export function useChatPageLogic() {
|
|||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [id, roomId]);
|
||||
|
||||
const { agent, agentList, messages, setMessages, branches, setBranches, loadMessages } = useChatData({
|
||||
const { agent, agentList, messages, setMessages, branches, setBranches, loadMessages, roomInvalidTick } = useChatData({
|
||||
agentId: id,
|
||||
roomId,
|
||||
highlightId,
|
||||
|
|
@ -116,6 +116,21 @@ export function useChatPageLogic() {
|
|||
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({
|
||||
agentId: id,
|
||||
agent,
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ export function useChatData(args: {
|
|||
const [agentList, setAgentList] = useState<Agent[]>([]);
|
||||
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
||||
const [branches, setBranches] = useState<Record<string, BranchInfo>>({});
|
||||
const [roomInvalidTick, setRoomInvalidTick] = useState(0);
|
||||
const loadSeqRef = useRef(0);
|
||||
const initialHighlightDoneRef = useRef(false);
|
||||
|
||||
|
|
@ -60,11 +61,22 @@ export function useChatData(args: {
|
|||
if (!roomId) return;
|
||||
const seq = ++loadSeqRef.current;
|
||||
const rid = roomId;
|
||||
const his = await ChatAPI.history(rid);
|
||||
if (seq !== loadSeqRef.current) return;
|
||||
if (rid !== roomId) return;
|
||||
setMessages(Array.isArray(his.messages) ? his.messages : []);
|
||||
setBranches(his.branches || {});
|
||||
try {
|
||||
const his = await ChatAPI.history(rid);
|
||||
if (seq !== loadSeqRef.current) return;
|
||||
if (rid !== roomId) return;
|
||||
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 处理
|
||||
|
|
@ -132,6 +144,7 @@ export function useChatData(args: {
|
|||
setMessages,
|
||||
branches,
|
||||
setBranches,
|
||||
loadMessages
|
||||
loadMessages,
|
||||
roomInvalidTick
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue