JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix:尝试修复刷新历史记录前端偶现数据丢失的问题
xiaoyao4296461aa1731b
diff --git a/jiuwenclaw/channels/web/frontend/src/App.tsx b/jiuwenclaw/channels/web/frontend/src/App.tsxindex 59f300a45f..99ac7afecc 100644--- a/jiuwenclaw/channels/web/frontend/src/App.tsx+++ b/jiuwenclaw/channels/web/frontend/src/App.tsx@@ -279,12 +279,14 @@ function AppContent() {/** 仅用于强制重跑「首屏 history」effect:从会话列表恢复时若 sessionId 未变,也要重新拉 history 并恢复 historyPagerMeta */const [historyBootstrapKey, setHistoryBootstrapKey] = useState(0);const sessionIdRef = useRef(sessionId);+ const historyLoadingMoreRef = useRef(false);const historyRestoreHandleRef = useRef<HistoryRestoreHandle | null>(null);const historyPageHandleRef = useRef<HistoryRestoreHandle | null>(null);/** 为 true 表示刚从「会话列表」恢复;history 为空时在 useEffect 的 onEmpty 中提示一次 */const historyRestoreFromPanelHintRef = useRef(false);const disposeInFlightHistoryHandles = useCallback(() => {+ historyLoadingMoreRef.current = false;historyRestoreHandleRef.current?.dispose();historyRestoreHandleRef.current = null;historyPageHandleRef.current?.dispose();@@ -953,18 +955,24 @@ function AppContent() {const handleLoadMoreHistory = useCallback(async () => {if (!sessionId.startsWith('sess_') || !historyPagerMeta) return;- if (historyLoadingMore || historyPagerMeta.loadedPages >= historyPagerMeta.totalPages) return;+ if (historyLoadingMoreRef.current || historyPagerMeta.loadedPages >= historyPagerMeta.totalPages) return;const sid = sessionId;const nextPage = historyPagerMeta.loadedPages + 1;const fallbackTotal = historyPagerMeta.totalPages;+ const finishLoadingMore = () => {+ historyLoadingMoreRef.current = false;+ setHistoryLoadingMore(false);+ };+ historyLoadingMoreRef.current = true;setHistoryLoadingMore(true);const pageHandle = fetchHistoryPage({sessionId: sid,+ pageIdx: nextPage,onReady: ({ messages, toolReplay, harnessReplay, totalPages }) => {if (sessionIdRef.current !== sid) {- setHistoryLoadingMore(false);+ finishLoadingMore();historyPageHandleRef.current = null;return;}@@ -1038,12 +1046,12 @@ function AppContent() {loadedPages: nextPage,totalPages: totalPages ?? fallbackTotal,});- setHistoryLoadingMore(false);+ finishLoadingMore();historyPageHandleRef.current = null;},onEmpty: (emptyTotalPages) => {if (sessionIdRef.current !== sid) {- setHistoryLoadingMore(false);+ finishLoadingMore();historyPageHandleRef.current = null;return;}@@ -1051,7 +1059,7 @@ function AppContent() {loadedPages: nextPage,totalPages: emptyTotalPages ?? fallbackTotal,});- setHistoryLoadingMore(false);+ finishLoadingMore();historyPageHandleRef.current = null;},onError: (message) => {@@ -1069,12 +1077,11 @@ function AppContent() {pageHandle.dispose();historyPageHandleRef.current = null;console.error('Failed to load older history:', error);- setHistoryLoadingMore(false);+ finishLoadingMore();}}, [addToolCall,addToolResult,- historyLoadingMore,historyPagerMeta,prependMessages,request,diff --git a/jiuwenclaw/channels/web/frontend/src/components/ChatPanel/index.tsx b/jiuwenclaw/channels/web/frontend/src/components/ChatPanel/index.tsxindex 3d4a1cb1fd..091cd842e0 100644--- a/jiuwenclaw/channels/web/frontend/src/components/ChatPanel/index.tsx+++ b/jiuwenclaw/channels/web/frontend/src/components/ChatPanel/index.tsx@@ -74,13 +74,14 @@ export function ChatPanel({historyPager = null,}: ChatPanelProps) {const { t } = useTranslation();- const { messages, isThinking } = useChatStore();+ const { messages, isThinking, toolExecutionOrder } = useChatStore();const messagesEndRef = useRef<HTMLDivElement>(null);const scrollContainerRef = useRef<HTMLDivElement>(null);const prependScrollSnapRef = useRef<{ sh: number; st: number } | null>(null);const wasHistoryLoadingRef = useRef(false);const suppressNextScrollToEndRef = useRef(false);const [isSending, setIsSending] = React.useState(false);+ const hasTimelineContent = messages.length > 0 || toolExecutionOrder.length > 0;const suggestions = [t('chat.welcomeSuggestions.journey'),t('chat.welcomeSuggestions.skills'),@@ -196,7 +197,7 @@ export function ChatPanel({onLoadMore={historyPager.onLoadMore}/>)}- {messages.length > 0 ? (+ {hasTimelineContent ? (<><MessageList messages={messages} /><SubtaskProgress />diff --git a/jiuwenclaw/channels/web/frontend/src/features/historyRestore.ts b/jiuwenclaw/channels/web/frontend/src/features/historyRestore.tsindex e0cb1ed769..16e78dca20 100644--- a/jiuwenclaw/channels/web/frontend/src/features/historyRestore.ts+++ b/jiuwenclaw/channels/web/frontend/src/features/historyRestore.ts@@ -377,11 +377,18 @@ function isHistoryBatchEnd(payload: Record<string, unknown>): boolean {* 仅处理属于当前 `history.get` 会话的帧,避免多标签/乱序下的串台。* 无 `session_id` 时:丢弃数据行;仍接受明确的结束帧(兼容未注入 id 的旧链路)。*/-function shouldProcessHistoryPayload(payload: Record<string, unknown>, expectedSessionId: string): boolean {+function shouldProcessHistoryPayload(+ payload: Record<string, unknown>,+ expectedSessionId: string,+ expectedPageIdx?: number+): boolean {const sid = typeof payload.session_id === 'string' ? payload.session_id.trim() : '';if (sid && sid !== expectedSessionId) {return false;}+ if (expectedPageIdx !== undefined && payload.page_idx !== expectedPageIdx) {+ return false;+ }if (!sid) {return isHistoryRestoreDonePayload(payload) || isHistoryBatchEnd(payload);}@@ -534,6 +541,7 @@ export interface FetchHistoryPageResult {export interface FetchHistoryPageOptions {sessionId: string;+ pageIdx: number;onReady: (result: FetchHistoryPageResult) => void;onEmpty?: (totalPages: number | null) => void;onError?: (message: string) => void;@@ -568,7 +576,7 @@ export function fetchHistoryPage(options: FetchHistoryPageOptions): HistoryResto}const payload = event.payload;- if (!shouldProcessHistoryPayload(payload, options.sessionId)) {+ if (!shouldProcessHistoryPayload(payload, options.sessionId, options.pageIdx)) {return;}