|
1 | 1 | //import LangflowLogoColor from "@/assets/LangflowLogocolor.svg?react"; |
2 | 2 |
|
3 | 3 | import { useCallback, useEffect, useRef, useState } from "react"; |
| 4 | +import { v4 as uuid } from "uuid"; |
4 | 5 | import { useShallow } from "zustand/react/shallow"; |
| 6 | +import { markOptimisticMessageFailed } from "@/utils/optimisticMessageUtils"; |
5 | 7 | import ThemeButtons from "@/components/core/appHeaderComponent/components/ThemeButtons"; |
6 | 8 | import { useGetMessagesQuery } from "@/controllers/API/queries/messages"; |
7 | 9 | import { useDeleteSession } from "@/controllers/API/queries/messages/use-delete-sessions"; |
@@ -43,7 +45,6 @@ export default function IOModal({ |
43 | 45 | const outputs = useFlowStore((state) => state.outputs); |
44 | 46 | const nodes = useFlowStore((state) => state.nodes); |
45 | 47 | const buildFlow = useFlowStore((state) => state.buildFlow); |
46 | | - const setIsBuilding = useFlowStore((state) => state.setIsBuilding); |
47 | 48 | const isBuilding = useFlowStore((state) => state.isBuilding); |
48 | 49 | const newChatOnPlayground = useFlowStore( |
49 | 50 | (state) => state.newChatOnPlayground, |
@@ -75,6 +76,7 @@ export default function IOModal({ |
75 | 76 | const setErrorData = useAlertStore((state) => state.setErrorData); |
76 | 77 | const setSuccessData = useAlertStore((state) => state.setSuccessData); |
77 | 78 | const deleteSession = useMessagesStore((state) => state.deleteSession); |
| 79 | + const addMessage = useMessagesStore((state) => state.addMessage); |
78 | 80 | const currentFlowId = useGetFlowId(); |
79 | 81 | const [sidebarOpen, setSidebarOpen] = useState(true); |
80 | 82 |
|
@@ -209,22 +211,58 @@ export default function IOModal({ |
209 | 211 | files?: string[]; |
210 | 212 | }): Promise<void> => { |
211 | 213 | if (isBuilding) return; |
| 214 | + const optimisticMessage = |
| 215 | + playgroundPage && (chatValue || (files && files.length > 0)) |
| 216 | + ? { |
| 217 | + id: `optimistic-${uuid()}`, |
| 218 | + text: chatValue, |
| 219 | + sender: "User", |
| 220 | + sender_name: "User", |
| 221 | + session_id: sessionId, |
| 222 | + timestamp: new Date().toISOString(), |
| 223 | + files: files ?? [], |
| 224 | + edit: false, |
| 225 | + background_color: "", |
| 226 | + text_color: "", |
| 227 | + flow_id: currentFlowId, |
| 228 | + properties: { optimistic: true }, |
| 229 | + } |
| 230 | + : null; |
| 231 | + |
| 232 | + if (optimisticMessage) { |
| 233 | + addMessage(optimisticMessage); |
| 234 | + } |
212 | 235 | setChatValue(""); |
213 | | - for (let i = 0; i < repeat; i++) { |
214 | | - await buildFlow({ |
215 | | - input_value: chatValue, |
216 | | - startNodeId: chatInput?.id, |
217 | | - files: files, |
218 | | - silent: true, |
219 | | - session: sessionId, |
220 | | - eventDelivery: eventDeliveryConfig, |
221 | | - }).catch((err) => { |
222 | | - console.error(err); |
223 | | - throw err; |
224 | | - }); |
| 236 | + try { |
| 237 | + for (let i = 0; i < repeat; i++) { |
| 238 | + await buildFlow({ |
| 239 | + input_value: chatValue, |
| 240 | + startNodeId: chatInput?.id, |
| 241 | + files: files, |
| 242 | + silent: true, |
| 243 | + session: sessionId, |
| 244 | + eventDelivery: eventDeliveryConfig, |
| 245 | + }); |
| 246 | + } |
| 247 | + } catch (error) { |
| 248 | + if (optimisticMessage) { |
| 249 | + addMessage(markOptimisticMessageFailed(optimisticMessage)); |
| 250 | + } |
| 251 | + throw error; |
225 | 252 | } |
226 | 253 | }, |
227 | | - [isBuilding, setIsBuilding, chatValue, chatInput?.id, sessionId, buildFlow], |
| 254 | + [ |
| 255 | + isBuilding, |
| 256 | + addMessage, |
| 257 | + chatValue, |
| 258 | + chatInput?.id, |
| 259 | + sessionId, |
| 260 | + buildFlow, |
| 261 | + currentFlowId, |
| 262 | + eventDeliveryConfig, |
| 263 | + playgroundPage, |
| 264 | + setChatValue, |
| 265 | + ], |
228 | 266 | ); |
229 | 267 |
|
230 | 268 | useEffect(() => { |
|
0 commit comments