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),
103 lines • 4.17 kB
JavaScript
// User-editable overrides for EVERY agent prompt the orchestrator controls.
//
// Design: each prompt registers its built-in DEFAULT (id + label + default text).
// The user's override is stored SEPARATELY (~/.comfyui-mcp/panel-prompts.json), so
// a default is never destroyed — an empty override or an explicit Reset always falls
// back to the built-in. This makes "edit any prompt" safe: a bad edit that breaks
// tool use is one Reset away.
//
// resolvePrompt(id, fallback) is what call-sites use — it returns the override when
// present and non-empty, else the inline default (which is also what gets registered
// for the console listing). The store imports nothing from the backends, so wiring a
// call-site (backends import THIS) never risks a circular import; defaults are
// registered centrally at startup (see registerBuiltinPrompts in index.ts).
import { EventEmitter } from "node:events";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { logger } from "../utils/logger.js";
const REGISTRY = new Map();
const emitter = new EventEmitter();
/** Overrides file path. Overridable for tests. */
export function panelPromptsPath() {
return (process.env.COMFYUI_MCP_PANEL_PROMPTS ||
join(homedir(), ".comfyui-mcp", "panel-prompts.json"));
}
function readOverrides() {
const p = panelPromptsPath();
if (!existsSync(p))
return {};
try {
const parsed = JSON.parse(readFileSync(p, "utf-8"));
if (!parsed || typeof parsed !== "object")
return {};
const out = {};
for (const [k, v] of Object.entries(parsed)) {
if (typeof v === "string")
out[k] = v;
}
return out;
}
catch (err) {
logger.warn(`[prompt-overrides] could not parse ${p}: ${err instanceof Error ? err.message : String(err)}`);
return {};
}
}
function writeOverrides(o) {
const p = panelPromptsPath();
mkdirSync(dirname(p), { recursive: true });
writeFileSync(p, JSON.stringify(o, null, 2), "utf-8");
}
/** Register a prompt's built-in default so the editor can list + reset it. Idempotent. */
export function registerPrompt(id, label, def, help) {
REGISTRY.set(id, { id, label, default: def, help });
}
/**
* The effective prompt text for `id`: the stored override if present and non-empty,
* otherwise `fallback` (the call-site's inline default, which also seeds the registry
* default). A whitespace-only override counts as "use default".
*/
export function resolvePrompt(id, fallback) {
const ov = readOverrides()[id];
if (typeof ov === "string" && ov.trim().length > 0)
return ov;
return fallback;
}
/** Persist an override. Empty/whitespace clears it (→ back to default). Emits change. */
export function setPromptOverride(id, value) {
const o = readOverrides();
if (!value || !value.trim())
delete o[id];
else
o[id] = value;
writeOverrides(o);
emitter.emit("change", id);
}
/** Remove an override → back to the built-in default. Emits change. */
export function clearPromptOverride(id) {
const o = readOverrides();
if (id in o) {
delete o[id];
writeOverrides(o);
}
emitter.emit("change", id);
}
/** All registered prompts with their default + current override, for the editor. */
export function listPrompts() {
const ov = readOverrides();
return [...REGISTRY.values()].map((e) => {
const o = ov[e.id];
const overridden = typeof o === "string" && o.trim().length > 0;
return { id: e.id, label: e.label, help: e.help, default: e.default, override: overridden ? o : null, overridden };
});
}
/** Is `id` a prompt we know about? (Guards the write endpoint.) */
export function isKnownPrompt(id) {
return REGISTRY.has(id);
}
/** Subscribe to "a prompt override changed" (arg: the changed id). Returns unsubscribe. */
export function onPromptsChanged(cb) {
emitter.on("change", cb);
return () => emitter.off("change", cb);
}
//# sourceMappingURL=prompt-overrides.js.map