Address review: module-level limit, remove rAF to fix race

- Move CHAT_HISTORY_REQUEST_LIMIT to module scope (align with views/chat.ts).
- Apply chatMessages synchronously; drop requestAnimationFrame to avoid
  overwriting later mutations (e.g. user message or second load).

Made-with: Cursor
This commit is contained in:
OpenClaw Contributor
2026-03-16 18:43:42 +00:00
parent cc968d8bc1
commit 2dc40bfc7c

View File

@@ -7,6 +7,9 @@ import { generateUUID } from "../uuid.ts";
const SILENT_REPLY_PATTERN = /^\s*NO_REPLY\s*$/;
/** Limit chat.history request to avoid huge payloads and main-thread freeze (see CHAT_HISTORY_RENDER_LIMIT in views/chat.ts). */
const CHAT_HISTORY_REQUEST_LIMIT = 25;
function isSilentReplyStream(text: string): boolean {
return SILENT_REPLY_PATTERN.test(text);
}
@@ -71,9 +74,6 @@ export async function loadChatHistory(state: ChatState) {
state.chatLoading = true;
state.lastError = null;
try {
// Request a small batch to avoid huge payloads and main-thread freeze when
// parsing JSON and rendering many markdown messages (browser "tab unresponsive").
const CHAT_HISTORY_REQUEST_LIMIT = 25;
const res = await state.client.request<{ messages?: Array<unknown>; thinkingLevel?: string }>(
"chat.history",
{
@@ -87,11 +87,7 @@ export async function loadChatHistory(state: ChatState) {
maybeResetToolStream(state);
state.chatStream = null;
state.chatStreamStartedAt = null;
// Defer applying messages so the UI can paint "Loading chat..." before the heavy render.
state.chatLoading = false;
requestAnimationFrame(() => {
state.chatMessages = filtered;
});
state.chatMessages = filtered;
} catch (err) {
state.lastError = String(err);
} finally {