Skip to content

Commit 52b34aa

Browse files
committed
fix: replayActiveTask uses active task question instead of earliest across all tasks (fixes #1367)
The replayActiveTask function was incorrectly searching for the earliest user message across ALL chat stores and tasks in the project, which caused replay to always use the question from task 1 regardless of which task was active. This fix extracts the question directly from the active task's own messages, matching the expected behavior described in issue #1367.
1 parent b5ae10c commit 52b34aa

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

src/lib/replay.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,60 @@ export const replayProject = async (
141141
projectStore.replayProject(taskIdsList, question, projectId, historyId);
142142
navigate({ pathname: '/' });
143143
};
144+
145+
/**
146+
* Replay function specifically for the current active task in ChatBox
147+
* This extracts the necessary data from the current chat store and project store
148+
*
149+
* @param chatStore - The chat store instance
150+
* @param projectStore - The project store instance
151+
* @param navigate - The navigate function from useNavigate hook
152+
*/
153+
export const replayActiveTask = async (
154+
chatStore: ChatStore,
155+
projectStore: ProjectStore,
156+
navigate: NavigateFunction
157+
) => {
158+
const taskId = chatStore.activeTaskId as string;
159+
const projectId = projectStore.activeProjectId as string;
160+
161+
if (!taskId || !projectId) {
162+
console.error('Missing taskId or projectId for replay');
163+
return;
164+
}
165+
166+
// Get the question from the active task's own messages
167+
const activeTask = chatStore.tasks[taskId];
168+
let question = '';
169+
if (activeTask && activeTask.messages && activeTask.messages.length > 0) {
170+
const userMessage = activeTask.messages.find(
171+
(msg: any) => msg.role === 'user'
172+
);
173+
if (userMessage && userMessage.content) {
174+
question = userMessage.content.trim();
175+
}
176+
}
177+
178+
// Fallback to first message if no user message found
179+
if (
180+
!question &&
181+
activeTask &&
182+
activeTask.messages[0] &&
183+
activeTask.messages[0].content
184+
) {
185+
question = activeTask.messages[0].content;
186+
console.log('[REPLAY] question fall back to ', question);
187+
}
188+
189+
const historyId = projectStore.getHistoryId(projectId);
190+
191+
// Use replayProject from projectStore instead of replay from chatStore
192+
const taskIdsList = [taskId];
193+
projectStore.replayProject(
194+
taskIdsList,
195+
question,
196+
projectId,
197+
historyId || undefined
198+
);
199+
navigate('/');
200+
};

0 commit comments

Comments
 (0)