feat: add configurable history timeout and polling interval for ComfyUI API

This commit is contained in:
syntaxbullet
2026-07-05 15:47:08 +02:00
parent 0b6b064085
commit a83024c35c
3 changed files with 24 additions and 4 deletions

View File

@@ -49,6 +49,8 @@ type ComfyObjectInfo = {
};
const comfyBaseUrl = process.env.COMFYUI_URL ?? "http://127.0.0.1:8188";
const comfyHistoryTimeoutMs = parsePositiveInteger(process.env.COMFYUI_HISTORY_TIMEOUT_MS, 20 * 60 * 1000);
const comfyHistoryPollIntervalMs = parsePositiveInteger(process.env.COMFYUI_HISTORY_POLL_INTERVAL_MS, 1000);
const defaultModels: Record<GenerateArchitecture, string> = {
sdxl: "sd_xl_base_1.0.safetensors",
"z-image": "z_image_bf16.safetensors",
@@ -170,15 +172,18 @@ async function uploadDataUrl(dataUrl: string, filename: string) {
}
async function waitForHistory(promptId: string) {
for (let attempt = 0; attempt < 240; attempt++) {
const startedAt = Date.now();
let attempts = 0;
while (Date.now() - startedAt < comfyHistoryTimeoutMs) {
const response = await fetch(`${comfyBaseUrl}/history/${promptId}`);
attempts += 1;
if (response.ok) {
const history = await response.json() as Record<string, unknown>;
if (history[promptId]) return history[promptId];
}
await Bun.sleep(500);
await Bun.sleep(comfyHistoryPollIntervalMs);
}
throw new Error(`Timed out waiting for ComfyUI prompt ${promptId}`);
throw new Error(`Timed out waiting for ComfyUI prompt ${promptId} after ${Math.round(comfyHistoryTimeoutMs / 1000)} seconds and ${attempts} checks`);
}
export function selectGeneratedOutputImage(history: unknown): { filename: string; subfolder?: string; type?: string } | undefined {
@@ -413,6 +418,12 @@ function unique<T>(values: T[]) {
return Array.from(new Set(values));
}
function parsePositiveInteger(value: string | undefined, fallback: number) {
if (!value) return fallback;
const parsed = Number(value);
return Number.isFinite(parsed) && parsed > 0 ? Math.round(parsed) : fallback;
}
function nodeErrorsMessage(nodeErrors: unknown): string | undefined {
if (!nodeErrors) return undefined;
if (Array.isArray(nodeErrors) && nodeErrors.length === 0) return undefined;