|
| 1 | +import { BUILD_POLLING_INTERVAL } from "@/constants/constants"; |
| 2 | +import { BuildStatus, EventDeliveryType } from "@/constants/enums"; |
| 3 | +import { VertexLayerElementType } from "@/types/zustand/flow"; |
| 4 | + |
| 5 | +export async function customPollBuildEvents( |
| 6 | + url: string, |
| 7 | + buildResults: Array<boolean>, |
| 8 | + verticesStartTimeMs: Map<string, number>, |
| 9 | + callbacks: { |
| 10 | + onBuildStart?: (idList: VertexLayerElementType[]) => void; |
| 11 | + onBuildUpdate?: (data: any, status: BuildStatus, buildId: string) => void; |
| 12 | + onBuildComplete?: (allNodesValid: boolean) => void; |
| 13 | + onBuildError?: ( |
| 14 | + title: string, |
| 15 | + list: string[], |
| 16 | + idList?: VertexLayerElementType[], |
| 17 | + ) => void; |
| 18 | + onGetOrderSuccess?: () => void; |
| 19 | + onValidateNodes?: (nodes: string[]) => void; |
| 20 | + }, |
| 21 | + abortController: AbortController, |
| 22 | + onEvent, |
| 23 | +): Promise<void> { |
| 24 | + let isDone = false; |
| 25 | + while (!isDone) { |
| 26 | + const response = await fetch( |
| 27 | + `${url}?event_delivery=${EventDeliveryType.POLLING}`, |
| 28 | + { |
| 29 | + method: "GET", |
| 30 | + headers: { |
| 31 | + "Content-Type": "application/json", |
| 32 | + Accept: "application/x-ndjson", |
| 33 | + }, |
| 34 | + signal: abortController.signal, // Add abort signal to fetch |
| 35 | + }, |
| 36 | + ); |
| 37 | + |
| 38 | + if (!response.ok) { |
| 39 | + const errorData = await response.json().catch(() => ({})); |
| 40 | + throw new Error( |
| 41 | + errorData.detail || |
| 42 | + "Langflow was not able to connect to the server. Please make sure your connection is working properly.", |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + // Get the response text - will be NDJSON format (one JSON per line) |
| 47 | + const responseText = await response.text(); |
| 48 | + |
| 49 | + // Skip if empty response |
| 50 | + if (!responseText.trim()) { |
| 51 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + // Split by newlines to get individual JSON objects |
| 56 | + const eventLines = responseText.split("\n").filter((line) => line.trim()); |
| 57 | + |
| 58 | + // If no events, continue polling |
| 59 | + if (eventLines.length === 0) { |
| 60 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + // Process all events in the NDJSON response |
| 65 | + for (const eventStr of eventLines) { |
| 66 | + // Process the event |
| 67 | + const event = JSON.parse(eventStr); |
| 68 | + const result = await onEvent( |
| 69 | + event.event, |
| 70 | + event.data, |
| 71 | + buildResults, |
| 72 | + verticesStartTimeMs, |
| 73 | + callbacks, |
| 74 | + ); |
| 75 | + |
| 76 | + if (!result) { |
| 77 | + isDone = true; |
| 78 | + abortController.abort(); |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + // Check if this was the end event |
| 83 | + if (event.event === "end") { |
| 84 | + isDone = true; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Add a small delay between polls |
| 90 | + await new Promise((resolve) => setTimeout(resolve, BUILD_POLLING_INTERVAL)); |
| 91 | + } |
| 92 | +} |
0 commit comments