From 69e02194dbd35e5a418a07583c2003b496714703 Mon Sep 17 00:00:00 2001 From: Wendong-Fan Date: Wed, 12 Nov 2025 01:34:53 +0800 Subject: [PATCH] enhance and bug fix --- src/lib/replay.ts | 6 +++--- src/store/projectStore.ts | 44 +++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/lib/replay.ts b/src/lib/replay.ts index f5278eb6e..fcb4ed261 100644 --- a/src/lib/replay.ts +++ b/src/lib/replay.ts @@ -59,10 +59,10 @@ export const replayActiveTask = async ( if (project && project.chatStores) { Object.entries(project.chatStores).forEach(([chatStoreId, chatStoreData]: [string, any]) => { const timestamp = project.chatStoreTimestamps[chatStoreId] || 0; - chatStoreData = chatStoreData.getState(); + const chatState = chatStoreData.getState(); - if (chatStoreData.tasks) { - Object.values(chatStoreData.tasks).forEach((task: any) => { + if (chatState.tasks) { + Object.values(chatState.tasks).forEach((task: any) => { // Check messages for user content if (task.messages && task.messages.length > 0) { const userMessage = task.messages.find((msg: any) => msg.role === 'user'); diff --git a/src/store/projectStore.ts b/src/store/projectStore.ts index 6fcdb22ab..c7273d95d 100644 --- a/src/store/projectStore.ts +++ b/src/store/projectStore.ts @@ -473,27 +473,35 @@ const projectStore = create()((set, get) => ({ } console.log(`[ProjectStore] Created replay project ${replayProjectId} for ${taskIds.length} tasks`); - + // For each taskId, create a chat store within the project and call replay - taskIds.forEach(async (taskId, index) => { - console.log(`[ProjectStore] Creating replay for task ${index + 1}/${taskIds.length}: ${taskId}`); - - // Create a new chat store for this task - const chatId = createChatStore(replayProjectId, `Task ${taskId}`); - - if (chatId) { - const project = get().projects[replayProjectId]; - const chatStore = project.chatStores[chatId]; - - if (chatStore) { - // Call replay on the chat store with the taskId, question, and 0 delay - await chatStore.getState().replay(taskId, question, 0.2); - console.log(`[ProjectStore] Started replay for task ${taskId}`); + // Use for...of loop instead of forEach to properly handle async operations + (async () => { + for (let index = 0; index < taskIds.length; index++) { + const taskId = taskIds[index]; + console.log(`[ProjectStore] Creating replay for task ${index + 1}/${taskIds.length}: ${taskId}`); + + // Create a new chat store for this task + const chatId = createChatStore(replayProjectId, `Task ${taskId}`); + + if (chatId) { + const project = get().projects[replayProjectId]; + const chatStore = project.chatStores[chatId]; + + if (chatStore) { + // Call replay on the chat store with the taskId, question, and 0 delay + try { + await chatStore.getState().replay(taskId, question, 0.2); + console.log(`[ProjectStore] Started replay for task ${taskId}`); + } catch (error) { + console.error(`[ProjectStore] Failed to replay task ${taskId}:`, error); + } + } } } - }); - - console.log(`[ProjectStore] Completed replay setup for ${taskIds.length} tasks`); + console.log(`[ProjectStore] Completed replay setup for ${taskIds.length} tasks`); + })(); + return replayProjectId; },