UNPKG

comfyui-mcp

Version:

Local-first, agent-native control plane for ComfyUI — MCP server + autonomous sidebar agent that drives your live graph in natural language on ANY LLM: Claude/ChatGPT/Gemini on your subscription (no API key), free local models via Ollama (fully offline),

326 lines • 14 kB
import { getClient, getHistory, getQueue as clientGetQueue, interrupt as clientInterrupt, deleteQueueItem as clientDeleteQueueItem, clearQueue as clientClearQueue, enqueuePrompt as clientEnqueuePrompt, freeMemory as clientFreeMemory, } from "../comfyui/client.js"; import * as cloudClient from "../comfyui/cloud-client.js"; import { isCloudMode } from "../config.js"; import { logger } from "../utils/logger.js"; import { ComfyUIError, ValidationError } from "../utils/errors.js"; import { analyzeHistoryEntry, } from "./job-history.js"; import { JobWatcher } from "./job-watcher.js"; function asRecord(value) { return value && typeof value === "object" && !Array.isArray(value) ? value : undefined; } function queueWorkflow(item) { const prompt = asRecord(item[2]); return prompt ? prompt : undefined; } function queueExtraData(item) { return asRecord(item[3]); } function extractJobInfo(items, includeWorkflow = false) { return items.map((item) => { const out = { number: item[0], prompt_id: item[1], }; if (includeWorkflow) { const workflow = queueWorkflow(item); const extraData = queueExtraData(item); if (workflow) out.workflow = workflow; if (extraData && Object.keys(extraData).length > 0) out.extra_data = extraData; } return out; }); } export async function getQueueSummary(opts = {}) { const queue = await clientGetQueue(); const includeWorkflow = !!opts.include_workflows; return { running: queue.queue_running.length, pending: queue.queue_pending.length, running_jobs: extractJobInfo(queue.queue_running, includeWorkflow), pending_jobs: extractJobInfo(queue.queue_pending, includeWorkflow), }; } function findPending(queue, promptId) { const idx = queue.queue_pending.findIndex((item) => item[1] === promptId); if (idx < 0) { throw new ComfyUIError(`Pending job ${promptId} was not found. Only pending jobs can be edited or requeued; running jobs must be interrupted.`, "QUEUE_JOB_NOT_FOUND", { prompt_id: promptId }); } return { item: queue.queue_pending[idx], position: idx + 1 }; } export async function getQueuedWorkflow(promptId) { const queue = await clientGetQueue(); const { item, position } = findPending(queue, promptId); const workflow = queueWorkflow(item); if (!workflow) { throw new ComfyUIError(`Pending job ${promptId} did not include a workflow payload in /queue.`, "QUEUE_PAYLOAD_UNAVAILABLE", { prompt_id: promptId }); } const extraData = queueExtraData(item); return { number: item[0], prompt_id: item[1], position, workflow, ...(extraData && Object.keys(extraData).length > 0 ? { extra_data: extraData } : {}), }; } function cloneWorkflow(workflow) { return JSON.parse(JSON.stringify(workflow)); } function applyInputUpdates(workflow, updates) { if (!updates) return workflow; for (const [nodeId, inputs] of Object.entries(updates)) { if (!inputs || typeof inputs !== "object" || Array.isArray(inputs)) { throw new ValidationError(`node_inputs.${nodeId} must be an object.`); } const node = workflow[nodeId]; if (!node) throw new ValidationError(`Cannot edit queued workflow: node ${nodeId} does not exist.`); node.inputs = { ...(node.inputs ?? {}), ...inputs }; } return workflow; } /** * Compute the real number of jobs remaining in the queue (running + pending), * clamped to >= 0. We do NOT trust the `number` field ComfyUI returns from * POST /prompt: that is the queue's monotonic priority counter, and it is * NEGATIVE when a job is enqueued at the front (front:true). Reusing it as a * "remaining" count produced nonsensical values like -17. A direct /queue read * is the authoritative count. Falls back to the (clamped) enqueue hint if the * queue can't be read. */ async function computeQueueRemaining(fallback) { try { const queue = await clientGetQueue(); return queue.queue_running.length + queue.queue_pending.length; } catch (err) { logger.debug("Could not read /queue for remaining count; using enqueue hint", { err }); if (typeof fallback !== "number" || !Number.isFinite(fallback)) return undefined; return Math.max(0, fallback); } } async function requeuePendingJob(promptId, workflow, extraData, position) { await clientDeleteQueueItem(promptId); const result = await clientEnqueuePrompt(workflow, extraData, { front: position === "front" }); JobWatcher.watch(result.prompt_id, workflow); const queue_remaining = await computeQueueRemaining(result.queue_remaining); return { old_prompt_id: promptId, new_prompt_id: result.prompt_id, queue_remaining, position, message: `Pending job ${promptId} was requeued at the ${position}; new prompt_id is ${result.prompt_id}.`, }; } export async function moveQueuedJob(promptId, position) { const queued = await getQueuedWorkflow(promptId); const workflow = cloneWorkflow(queued.workflow); return requeuePendingJob(promptId, workflow, queued.extra_data, position); } export async function editQueuedJob(opts) { const queued = await getQueuedWorkflow(opts.prompt_id); const base = opts.workflow ? cloneWorkflow(opts.workflow) : cloneWorkflow(queued.workflow); const workflow = applyInputUpdates(base, opts.node_inputs); return requeuePendingJob(opts.prompt_id, workflow, queued.extra_data, opts.position ?? "back"); } async function cloudJobStatus(promptId) { // Cloud /api/job/<id>/status returns // { status: "pending" | "in_progress" | "completed" | "failed", error?, prompt_id? } // Map onto local JobStatus shape so callers don't care about the backend. const cloud = await cloudClient.getJobStatus(promptId); const done = cloud.status === "completed" || cloud.status === "failed"; const base = { running: cloud.status === "in_progress", pending: cloud.status === "pending", done, status_str: cloud.status, }; if (!done) return base; // Try to enrich completed jobs from /api/history_v2/<id>; if that fails, // fall back to the bare cloud status (with the error message if present). try { const history = await getHistory(promptId); const entry = history[promptId]; if (!entry) { return cloud.error ? { ...base, error: { node_id: "", node_type: "", exception_message: cloud.error, }, } : base; } const analysis = analyzeHistoryEntry(entry); return { ...base, status_str: entry.status?.status_str ?? cloud.status, error: analysis.error, execution_stats: analysis.execution_stats, }; } catch (err) { logger.warn("Cloud: could not enrich job status from history", { prompt_id: promptId, error: err instanceof Error ? err.message : err, }); return cloud.error ? { ...base, error: { node_id: "", node_type: "", exception_message: cloud.error, }, } : base; } } export async function getJobStatus(promptId) { if (isCloudMode()) return cloudJobStatus(promptId); const client = getClient(); const status = await client.getPromptStatus(promptId); if (!status.done) return status; try { const history = await getHistory(promptId); const entry = history[promptId]; if (!entry) return status; const analysis = analyzeHistoryEntry(entry); return { ...status, status_str: entry.status.status_str, error: analysis.error, execution_stats: analysis.execution_stats, ...(analysis.text_outputs ? { text_outputs: analysis.text_outputs } : {}), }; } catch (err) { logger.warn("Could not enrich job status from history", { prompt_id: promptId, error: err instanceof Error ? err.message : err, }); return status; } } export async function cancelRunningJob(promptId) { await clientInterrupt(promptId); logger.info("Job interrupted", { prompt_id: promptId ?? "current" }); } const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); /** How long to wait for an interrupt to actually stop the running job before * escalating. ComfyUI only checks the interrupt flag BETWEEN nodes/steps, so a * multi-minute single step won't honor it — that wait is what detects the wedge. * Tunable via COMFYUI_MCP_INTERRUPT_S (seconds); default 30. */ function interruptHonorMs() { const s = Number(process.env.COMFYUI_MCP_INTERRUPT_S); return Number.isFinite(s) && s > 0 ? Math.round(s * 1000) : 30000; } /** Poll /queue until the target running job is gone (or any-running is gone when * no id given), or the timeout elapses. Returns true if it cleared. */ async function waitForRunningCleared(promptId, timeoutMs) { const start = Date.now(); while (Date.now() - start < timeoutMs) { await sleep(1500); const q = await getQueueSummary().catch(() => null); if (!q) continue; if (q.running === 0) return true; // A DIFFERENT job is now running → the one we targeted has cleared. if (promptId && !q.running_jobs.some((j) => j.prompt_id === promptId)) return true; } return false; } /** * Cancel the running job ROBUSTLY: optionally clear all pending first (so a * re-queue can't stack behind a backlog), interrupt, then WAIT and verify the * job actually stopped. If the interrupt isn't honored within the window, escalate * to POST /free and re-check; if it STILL won't die it's wedged inside a single * step — HTTP can't kill that, so report that a ComfyUI restart is required rather * than letting the agent re-queue on top of a zombie. */ export async function cancelRunningJobEscalating(opts) { let pending_cleared; if (opts.clear_pending) { const before = await getQueueSummary().catch(() => null); await clearAllQueued().catch((err) => logger.warn("clear_pending failed (continuing)", { err })); pending_cleared = before?.pending; } // Identify the job we're trying to stop so we can verify it actually clears. const pre = await getQueueSummary().catch(() => null); const runningId = opts.prompt_id ?? pre?.running_jobs?.[0]?.prompt_id; if (pre && pre.running === 0 && !opts.prompt_id) { return { interrupted: false, honored: true, freed_vram: false, wedged: false, pending_cleared, message: `No job is running.${pending_cleared != null ? ` Cleared ${pending_cleared} pending.` : ""}`, }; } await clientInterrupt(opts.prompt_id); logger.info("Interrupt sent (escalating cancel)", { prompt_id: runningId ?? "current" }); if (await waitForRunningCleared(runningId, interruptHonorMs())) { return { interrupted: true, honored: true, freed_vram: false, wedged: false, pending_cleared, running_prompt_id: runningId, message: `Interrupted the running job${runningId ? ` (${runningId})` : ""}.${pending_cleared != null ? ` Cleared ${pending_cleared} pending.` : ""}`, }; } // Not honored — the step is long-running. Free VRAM and re-check. logger.warn("Interrupt not honored in window; escalating to /free", { prompt_id: runningId ?? "current" }); await clientFreeMemory({ unload_models: true, free_memory: true }).catch((err) => logger.warn("/free during cancel escalation failed (continuing)", { err })); if (await waitForRunningCleared(runningId, Math.min(interruptHonorMs(), 12000))) { return { interrupted: true, honored: true, freed_vram: true, wedged: false, pending_cleared, running_prompt_id: runningId, message: `The job didn't stop on interrupt; freeing VRAM cleared it${runningId ? ` (${runningId})` : ""}.${pending_cleared != null ? ` Cleared ${pending_cleared} pending.` : ""}`, }; } return { interrupted: true, honored: false, freed_vram: true, wedged: true, pending_cleared, running_prompt_id: runningId, message: `⚠️ The running job${runningId ? ` (${runningId})` : ""} did NOT stop after interrupt + VRAM free within ` + `~${Math.round(interruptHonorMs() / 1000)}s — it is wedged inside a single step (ComfyUI only honors interrupts ` + `BETWEEN steps, so a multi-minute step ignores cancel). An HTTP cancel cannot kill this; restart ComfyUI ` + `(panel_restart_comfyui, or restart_comfyui) to clear it. ` + `${opts.clear_pending ? `Pending jobs were cleared (${pending_cleared ?? 0}).` : "Pending jobs were NOT cleared — pass clear_pending:true or call clear_queue."} Do NOT queue another run until this is gone.`, }; } export async function cancelQueuedJob(promptId) { await clientDeleteQueueItem(promptId); logger.info("Queued job removed", { prompt_id: promptId }); } export async function clearAllQueued() { await clientClearQueue(); logger.info("All pending queue items cleared"); } //# sourceMappingURL=queue-manager.js.map