Skip to content

Commit 7ae1863

Browse files
authored
Merge branch 'main' into model-provider-keys-v2
2 parents 926e979 + 722c6cc commit 7ae1863

2 files changed

Lines changed: 101 additions & 68 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}

src/frontend/src/utils/buildUtils.ts

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
customCancelBuildUrl,
1313
customEventsUrl,
1414
} from "@/customization/utils/custom-buildUtils";
15+
import { customPollBuildEvents } from "@/customization/utils/custom-poll-build-events";
1516
import { useMessagesStore } from "@/stores/messagesStore";
1617
import { BuildStatus, EventDeliveryType } from "../constants/enums";
1718
import { getVerticesOrder, postBuildVertex } from "../controllers/API";
@@ -180,74 +181,14 @@ async function pollBuildEvents(
180181
},
181182
abortController: AbortController,
182183
): Promise<void> {
183-
let isDone = false;
184-
while (!isDone) {
185-
const response = await fetch(
186-
`${url}?event_delivery=${EventDeliveryType.POLLING}`,
187-
{
188-
method: "GET",
189-
headers: {
190-
"Content-Type": "application/json",
191-
Accept: "application/x-ndjson",
192-
},
193-
signal: abortController.signal, // Add abort signal to fetch
194-
},
195-
);
196-
197-
if (!response.ok) {
198-
const errorData = await response.json().catch(() => ({}));
199-
throw new Error(
200-
errorData.detail ||
201-
"Langflow was not able to connect to the server. Please make sure your connection is working properly.",
202-
);
203-
}
204-
205-
// Get the response text - will be NDJSON format (one JSON per line)
206-
const responseText = await response.text();
207-
208-
// Skip if empty response
209-
if (!responseText.trim()) {
210-
await new Promise((resolve) => setTimeout(resolve, 100));
211-
continue;
212-
}
213-
214-
// Split by newlines to get individual JSON objects
215-
const eventLines = responseText.split("\n").filter((line) => line.trim());
216-
217-
// If no events, continue polling
218-
if (eventLines.length === 0) {
219-
await new Promise((resolve) => setTimeout(resolve, 100));
220-
continue;
221-
}
222-
223-
// Process all events in the NDJSON response
224-
for (const eventStr of eventLines) {
225-
// Process the event
226-
const event = JSON.parse(eventStr);
227-
const result = await onEvent(
228-
event.event,
229-
event.data,
230-
buildResults,
231-
verticesStartTimeMs,
232-
callbacks,
233-
);
234-
235-
if (!result) {
236-
isDone = true;
237-
abortController.abort();
238-
break;
239-
}
240-
241-
// Check if this was the end event
242-
if (event.event === "end") {
243-
isDone = true;
244-
break;
245-
}
246-
}
247-
248-
// Add a small delay between polls
249-
await new Promise((resolve) => setTimeout(resolve, BUILD_POLLING_INTERVAL));
250-
}
184+
return customPollBuildEvents(
185+
url,
186+
buildResults,
187+
verticesStartTimeMs,
188+
callbacks,
189+
abortController,
190+
onEvent,
191+
);
251192
}
252193

253194
export async function buildFlowVertices({

0 commit comments

Comments
 (0)