fix: resolve Windows gateway reconnect race condition and translate comments (#787)
This commit is contained in:
committed by
GitHub
Unverified
parent
d8750e135b
commit
c3a735a49c
@@ -684,9 +684,10 @@ function buildSessionSwitchPatch(
|
||||
>,
|
||||
nextSessionKey: string,
|
||||
): Partial<ChatState> {
|
||||
// 仅将没有任何历史记录且无活动时间的会话视为空会话。
|
||||
// 单纯依赖 messages.length 是不可靠的,因为 switchSession 会在真正调用 loadHistory 前抢先清空当前 messages,
|
||||
// 造成竞争条件,使得带有真实历史的会话被判定为空并从侧边栏移除。
|
||||
// Only treat sessions with no history records and no activity timestamp as empty.
|
||||
// Relying solely on messages.length is unreliable because switchSession clears
|
||||
// the current messages before loadHistory runs, creating a race condition that
|
||||
// could cause sessions with real history to be incorrectly removed from the sidebar.
|
||||
const leavingEmpty = !state.currentSessionKey.endsWith(':main')
|
||||
&& state.messages.length === 0
|
||||
&& !state.sessionLastActivity[state.currentSessionKey]
|
||||
@@ -1229,7 +1230,7 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
// sessions.reset archives (renames) the session JSONL file, making old
|
||||
// conversation history inaccessible when the user switches back to it.
|
||||
const { currentSessionKey, messages, sessions, sessionLastActivity, sessionLabels } = get();
|
||||
// 仅将没有任何历史记录且无活动时间的会话视为空会话
|
||||
// Only treat sessions with no history records and no activity timestamp as empty
|
||||
const leavingEmpty = !currentSessionKey.endsWith(':main')
|
||||
&& messages.length === 0
|
||||
&& !sessionLastActivity[currentSessionKey]
|
||||
@@ -1272,8 +1273,8 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
// This mirrors the "leavingEmpty" logic in switchSession so that creating
|
||||
// a new session and immediately navigating away doesn't leave a ghost entry
|
||||
// in the sidebar.
|
||||
// 同样需要综合检查 sessionLastActivity 和 sessionLabels,
|
||||
// 防止因为 switchSession 抢先清空 messages 而误判有历史的会话为空。
|
||||
// Also check sessionLastActivity and sessionLabels comprehensively to prevent
|
||||
// falsely treating sessions with history as empty due to switchSession clearing messages early.
|
||||
const isEmptyNonMain = !currentSessionKey.endsWith(':main')
|
||||
&& messages.length === 0
|
||||
&& !sessionLastActivity[currentSessionKey]
|
||||
@@ -1307,8 +1308,8 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
|
||||
if (!quiet) set({ loading: true, error: null });
|
||||
|
||||
// 安全保护:如果历史记录加载花费太多时间,则强制将 loading 设置为 false
|
||||
// 防止 UI 永远卡在转圈状态。
|
||||
// Safety guard: if history loading takes too long, force loading to false
|
||||
// to prevent the UI from being stuck in a spinner forever.
|
||||
let loadingTimedOut = false;
|
||||
const loadingSafetyTimer = quiet ? null : setTimeout(() => {
|
||||
loadingTimedOut = true;
|
||||
@@ -1494,7 +1495,7 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
try {
|
||||
await loadPromise;
|
||||
} finally {
|
||||
// 正常完成时清除安全定时器
|
||||
// Clear the safety timer on normal completion
|
||||
if (loadingSafetyTimer) clearTimeout(loadingSafetyTimer);
|
||||
if (!loadingTimedOut) {
|
||||
// Only update load time if we actually didn't time out
|
||||
|
||||
@@ -154,9 +154,10 @@ export function createSessionActions(
|
||||
|
||||
switchSession: (key: string) => {
|
||||
const { currentSessionKey, messages, sessionLastActivity, sessionLabels } = get();
|
||||
// 仅将没有任何历史记录且无活动时间的会话视为空会话。
|
||||
// 单纯依赖 messages.length 是不可靠的,因为 switchSession 会在真正调用 loadHistory 前抢先清空当前 messages,
|
||||
// 造成竞争条件,使得带有真实历史的会话被判定为空并从侧边栏移除。
|
||||
// Only treat sessions with no history records and no activity timestamp as empty.
|
||||
// Relying solely on messages.length is unreliable because switchSession clears
|
||||
// the current messages before loadHistory runs, creating a race condition that
|
||||
// could cause sessions with real history to be incorrectly removed from the sidebar.
|
||||
const leavingEmpty = !currentSessionKey.endsWith(':main')
|
||||
&& messages.length === 0
|
||||
&& !sessionLastActivity[currentSessionKey]
|
||||
@@ -253,7 +254,7 @@ export function createSessionActions(
|
||||
// sessions.reset archives (renames) the session JSONL file, making old
|
||||
// conversation history inaccessible when the user switches back to it.
|
||||
const { currentSessionKey, messages, sessionLastActivity, sessionLabels } = get();
|
||||
// 仅将没有任何历史记录且无活动时间的会话视为空会话
|
||||
// Only treat sessions with no history records and no activity timestamp as empty
|
||||
const leavingEmpty = !currentSessionKey.endsWith(':main')
|
||||
&& messages.length === 0
|
||||
&& !sessionLastActivity[currentSessionKey]
|
||||
@@ -294,8 +295,8 @@ export function createSessionActions(
|
||||
// This mirrors the "leavingEmpty" logic in switchSession so that creating
|
||||
// a new session and immediately navigating away doesn't leave a ghost entry
|
||||
// in the sidebar.
|
||||
// 同样需要综合检查 sessionLastActivity 和 sessionLabels,
|
||||
// 防止因为 switchSession 抢先清空 messages 而误判有历史的会话为空。
|
||||
// Also check sessionLastActivity and sessionLabels comprehensively to prevent
|
||||
// falsely treating sessions with history as empty due to switchSession clearing messages early.
|
||||
const isEmptyNonMain = !currentSessionKey.endsWith(':main')
|
||||
&& messages.length === 0
|
||||
&& !sessionLastActivity[currentSessionKey]
|
||||
|
||||
Reference in New Issue
Block a user