From be78474be7d7ff997ed2f57726fa0ea02f4dc282 Mon Sep 17 00:00:00 2001 From: a7m-1st Date: Fri, 10 Oct 2025 13:02:35 +0300 Subject: [PATCH] fix: add message to the new chatStore (delay task appending) --- src/components/ChatBox/index.tsx | 30 +++++++++++--------------- src/store/chatStore.ts | 37 ++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/components/ChatBox/index.tsx b/src/components/ChatBox/index.tsx index ddb50983d..be1445b54 100644 --- a/src/components/ChatBox/index.tsx +++ b/src/components/ChatBox/index.tsx @@ -195,24 +195,13 @@ export default function ChatBox(): JSX.Element { return; } - chatStore.addMessages(_taskId, { - id: generateUniqueId(), - role: "user", - content: tempMessageContent, - attaches: - JSON.parse(JSON.stringify(chatStore.tasks[_taskId]?.attaches)) || [], - }); - setMessage(""); - - setTimeout(() => { - scrollToBottom(); - }, 200); - if (chatStore.tasks[_taskId as string]?.hasWaitComfirm) { // If the task has not started yet (pending status), start it normally if (chatStore.tasks[_taskId as string].status === "pending") { - chatStore.setIsPending(_taskId, true); - chatStore.startTask(_taskId); + setMessage(""); + // Pass the message content to startTask instead of adding it to current chatStore + const attachesToSend = JSON.parse(JSON.stringify(chatStore.tasks[_taskId]?.attaches)) || []; + chatStore.startTask(_taskId, undefined, undefined, undefined, tempMessageContent, attachesToSend); // keep hasWaitComfirm as true so that follow-up improves work as usual } else { // Task already started and is waiting for user confirmation – use improve API @@ -238,8 +227,15 @@ export default function ChatBox(): JSX.Element { proxyFetchPut("/api/user/privacy", requestData); setPrivacy(true); } - chatStore.setIsPending(_taskId, true); - chatStore.startTask(_taskId); + + setTimeout(() => { + scrollToBottom(); + }, 200); + + // For the very first message, add it to the current chatStore first, then call startTask + const attachesToSend = JSON.parse(JSON.stringify(chatStore.tasks[_taskId]?.attaches)) || []; + setMessage(""); + chatStore.startTask(_taskId, undefined, undefined, undefined, tempMessageContent, attachesToSend); chatStore.setHasWaitComfirm(_taskId as string, true); } } diff --git a/src/store/chatStore.ts b/src/store/chatStore.ts index 251942bdb..61dc9ef23 100644 --- a/src/store/chatStore.ts +++ b/src/store/chatStore.ts @@ -51,7 +51,7 @@ export interface ChatStore { setStatus: (taskId: string, status: 'running' | 'finished' | 'pending' | 'pause') => void; setActiveTaskId: (taskId: string) => void; replay: (taskId: string, question: string, time: number) => Promise; - startTask: (taskId: string, type?: string, shareToken?: string, delayTime?: number) => Promise; + startTask: (taskId: string, type?: string, shareToken?: string, delayTime?: number, messageContent?: string, messageAttaches?: File[]) => Promise; handleConfirmTask: (project_id:string, taskId: string, type?: string) => void; addMessages: (taskId: string, messages: Message) => void; setMessages: (taskId: string, messages: Message[]) => void; @@ -173,7 +173,7 @@ const chatStore = (initial?: Partial) => createStore()( }) }) }, - startTask: async (taskId: string, type?: string, shareToken?: string, delayTime?: number) => { + startTask: async (taskId: string, type?: string, shareToken?: string, delayTime?: number, messageContent?: string, messageAttaches?: File[]) => { const { token, language, modelType, cloud_model_type, email } = getAuthStore() const workerList = useWorkerList(); const { getLastUserMessage, setDelayTime, setType } = get(); @@ -191,9 +191,28 @@ const chatStore = (initial?: Partial) => createStore()( const projectStore = useProjectStore.getState(); const project_id = projectStore.activeProjectId; //Create a new chatStore on Start + let newTaskId = taskId; + let targetChatStore = { getState: () => get() }; // Default to current store if(project_id && type !== "replay") { console.log("Creating a new Chat Instance for current project on end") - projectStore.appendInitChatStore(project_id) + const newChatResult = projectStore.appendInitChatStore(project_id); + + if (newChatResult) { + newTaskId = newChatResult.taskId; + targetChatStore = newChatResult.chatStore; + targetChatStore.getState().setIsPending(newTaskId, true); + + //From handleSend if message is given + // Add the message to the new chatStore if provided + if (messageContent) { + targetChatStore.getState().addMessages(newTaskId, { + id: generateUniqueId(), + role: "user", + content: messageContent, + attaches: messageAttaches || [], + }); + } + } } const base_Url = import.meta.env.DEV ? import.meta.env.VITE_PROXY_URL : import.meta.env.VITE_BASE_URL @@ -290,9 +309,9 @@ const chatStore = (initial?: Partial) => createStore()( const obj = { "project_id": project_id, - "task_id": taskId, + "task_id": newTaskId, "user_id": authStore.user_id, - "question": tasks[taskId]?.messages[0]?.content ?? '', + "question": messageContent || (targetChatStore.getState().tasks[newTaskId]?.messages[0]?.content ?? ''), "language": systemLanguage, "model_platform": apiModel.model_platform, "model_type": apiModel.model_type, @@ -314,8 +333,8 @@ const chatStore = (initial?: Partial) => createStore()( headers: { "Content-Type": "application/json", "Authorization": type == 'replay' ? `Bearer ${token}` : undefined as unknown as string }, body: !type ? JSON.stringify({ project_id: project_id, - task_id: taskId, - question: getLastUserMessage()?.content, + task_id: newTaskId, + question: messageContent || targetChatStore.getState().getLastUserMessage()?.content, model_platform: apiModel.model_platform, email, model_type: apiModel.model_type, @@ -325,7 +344,7 @@ const chatStore = (initial?: Partial) => createStore()( installed_mcp: mcpLocal, language: systemLanguage, allow_local_system: true, - attaches: tasks[taskId].attaches.map(f => f.filePath), + attaches: (messageAttaches || targetChatStore.getState().tasks[newTaskId]?.attaches || []).map(f => f.filePath), bun_mirror: systemLanguage === 'zh-cn' ? 'https://registry.npmmirror.com' : '', uvx_mirror: systemLanguage === 'zh-cn' ? 'http://mirrors.aliyun.com/pypi/simple/' : '', summary_prompt: ``, @@ -356,7 +375,7 @@ const chatStore = (initial?: Partial) => createStore()( // Get current active task ID dynamically const getCurrentTaskId = () => { const currentStore = getCurrentChatStore(); - return currentStore.activeTaskId || taskId; + return currentStore.activeTaskId || newTaskId; }; const {