Skip to content

Commit fa7fb8b

Browse files
Show optimistic chat messages in playground
1 parent d00bae6 commit fa7fb8b

1 file changed

Lines changed: 35 additions & 28 deletions

File tree

src/frontend/src/modals/IOModal/playground-modal.tsx

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ export default function IOModal({
9090
const {
9191
data: sessionsFromDb,
9292
isLoading: sessionsLoading,
93-
refetch: refetchSessions,
9493
} = useGetSessionsFromFlowQuery(
9594
{
9695
id: currentFlowId,
@@ -211,6 +210,7 @@ export default function IOModal({
211210
files?: string[];
212211
}): Promise<void> => {
213212
if (isBuilding) return;
213+
const activeSessionId = visibleSession ?? sessionId;
214214
const shouldAddOptimisticMessage =
215215
chatValue !== "" || (files && files.length > 0);
216216
if (shouldAddOptimisticMessage) {
@@ -221,7 +221,7 @@ export default function IOModal({
221221
text: chatValue,
222222
sender: "User",
223223
sender_name: "User",
224-
session_id: sessionId,
224+
session_id: activeSessionId,
225225
timestamp: new Date().toISOString(),
226226
files: files ?? [],
227227
edit: false,
@@ -239,7 +239,7 @@ export default function IOModal({
239239
startNodeId: chatInput?.id,
240240
files: files,
241241
silent: true,
242-
session: sessionId,
242+
session: activeSessionId,
243243
eventDelivery: eventDeliveryConfig,
244244
}).catch((err) => {
245245
console.error(err);
@@ -257,6 +257,7 @@ export default function IOModal({
257257
isBuilding,
258258
sessionId,
259259
setChatValue,
260+
visibleSession,
260261
],
261262
);
262263

@@ -265,39 +266,45 @@ export default function IOModal({
265266
window.sessionStorage.setItem(currentFlowId, JSON.stringify(messages));
266267
}
267268
if (newChatOnPlayground && !sessionsLoading) {
268-
const handleRefetchAndSetSession = async () => {
269-
try {
270-
const result = await refetchSessions();
271-
if (result.data?.sessions && result.data.sessions.length > 0) {
272-
setvisibleSession(
273-
result.data.sessions[result.data.sessions.length - 1],
274-
);
275-
}
276-
} catch (error) {
277-
console.error("Error refetching sessions:", error);
269+
const newSessionId = createNewSessionName();
270+
setSessions((prevSessions) => {
271+
if (prevSessions.includes(newSessionId)) {
272+
return prevSessions;
278273
}
279-
};
280-
281-
handleRefetchAndSetSession();
274+
return [newSessionId, ...prevSessions];
275+
});
276+
setvisibleSession(newSessionId);
282277
setNewChatOnPlayground(false);
283278
}
284-
}, [messages, playgroundPage]);
279+
}, [
280+
currentFlowId,
281+
messages,
282+
newChatOnPlayground,
283+
playgroundPage,
284+
sessionsLoading,
285+
setNewChatOnPlayground,
286+
]);
285287

286288
useEffect(() => {
287289
if (!visibleSession) {
288-
setSessionId(createNewSessionName());
289-
setCurrentSessionId(currentFlowId);
290-
} else if (visibleSession) {
291-
setSessionId(visibleSession);
292-
setCurrentSessionId(visibleSession);
293-
if (selectedViewField?.type === "Session") {
294-
setSelectedViewField({
295-
id: visibleSession,
296-
type: "Session",
297-
});
290+
if (newChatOnPlayground) {
291+
return;
298292
}
293+
const newSessionId = createNewSessionName();
294+
setSessionId(newSessionId);
295+
setCurrentSessionId(newSessionId);
296+
setvisibleSession(newSessionId);
297+
return;
299298
}
300-
}, [visibleSession]);
299+
setSessionId(visibleSession);
300+
setCurrentSessionId(visibleSession);
301+
if (selectedViewField?.type === "Session") {
302+
setSelectedViewField({
303+
id: visibleSession,
304+
type: "Session",
305+
});
306+
}
307+
}, [newChatOnPlayground, visibleSession]);
301308

302309
const setPlaygroundScrollBehaves = useUtilityStore(
303310
(state) => state.setPlaygroundScrollBehaves,

0 commit comments

Comments
 (0)