mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-06-10 17:35:57 +00:00
fix: ignore stale run reconnect conflicts (#3284)
* fix: ignore stale run reconnect conflicts * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * fix: ignore stale run reconnect conflicts --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -38,6 +38,47 @@ function injectCsrfHeader(_url: URL, init: RequestInit): RequestInit {
|
||||
return { ...init, headers };
|
||||
}
|
||||
|
||||
export function isInactiveRunStreamError(error: unknown): boolean {
|
||||
const status =
|
||||
typeof error === "object" && error !== null
|
||||
? Reflect.get(error, "status")
|
||||
: undefined;
|
||||
const message =
|
||||
typeof error === "string"
|
||||
? error
|
||||
: error instanceof Error
|
||||
? error.message
|
||||
: typeof error === "object" && error !== null
|
||||
? String(Reflect.get(error, "message") ?? "")
|
||||
: "";
|
||||
|
||||
// Match the gateway's store-only run response in
|
||||
// backend/app/gateway/routers/thread_runs.py until the API exposes a
|
||||
// structured error code for inactive run streams.
|
||||
return (
|
||||
(status === 409 || message.includes("HTTP 409")) &&
|
||||
message.includes("not active on this worker") &&
|
||||
message.includes("cannot be streamed")
|
||||
);
|
||||
}
|
||||
|
||||
export function clearReconnectRun(
|
||||
threadId: string | null | undefined,
|
||||
runId: string,
|
||||
): void {
|
||||
if (typeof window === "undefined" || !threadId) return;
|
||||
|
||||
const key = `lg:stream:${threadId}`;
|
||||
try {
|
||||
const storage = window.sessionStorage;
|
||||
if (storage.getItem(key) === runId) {
|
||||
storage.removeItem(key);
|
||||
}
|
||||
} catch {
|
||||
// Ignore storage access failures so reconnect cleanup never throws.
|
||||
}
|
||||
}
|
||||
|
||||
function createCompatibleClient(isMock?: boolean): LangGraphClient {
|
||||
if (isStaticWebsiteOnly() && !isMock) {
|
||||
return createStaticClient();
|
||||
@@ -59,12 +100,21 @@ function createCompatibleClient(isMock?: boolean): LangGraphClient {
|
||||
)) as typeof client.runs.stream;
|
||||
|
||||
const originalJoinStream = client.runs.joinStream.bind(client.runs);
|
||||
client.runs.joinStream = ((threadId, runId, options) =>
|
||||
originalJoinStream(
|
||||
threadId,
|
||||
runId,
|
||||
sanitizeRunStreamOptions(options),
|
||||
)) as typeof client.runs.joinStream;
|
||||
client.runs.joinStream = async function* (threadId, runId, options) {
|
||||
try {
|
||||
yield* originalJoinStream(
|
||||
threadId,
|
||||
runId,
|
||||
sanitizeRunStreamOptions(options),
|
||||
);
|
||||
} catch (error) {
|
||||
if (isInactiveRunStreamError(error)) {
|
||||
clearReconnectRun(threadId, runId);
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
} as typeof client.runs.joinStream;
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user