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),
128 lines • 4.22 kB
JavaScript
import { readFile, writeFile, mkdir } from "node:fs/promises";
import { existsSync } from "node:fs";
import { dirname, join } from "node:path";
import { homedir } from "node:os";
import { logger } from "../utils/logger.js";
const ENV_PREFIX = "COMFYUI_DEFAULT_";
function defaultConfigPath() {
const xdg = process.env.XDG_CONFIG_HOME;
const root = xdg && xdg.length > 0 ? xdg : join(homedir(), ".config");
return join(root, "comfyui-mcp", "config.json");
}
function defaultEnv() {
return process.env;
}
const state = {
cfg: { configPath: defaultConfigPath(), env: defaultEnv() },
configValues: {},
runtimeValues: {},
};
function parseEnvValue(raw) {
try {
return JSON.parse(raw);
}
catch {
return raw;
}
}
function envDefaults(env) {
const out = {};
for (const [key, val] of Object.entries(env)) {
if (!key.startsWith(ENV_PREFIX) || val === undefined)
continue;
const name = key.slice(ENV_PREFIX.length).toLowerCase();
if (name.length === 0)
continue;
out[name] = parseEnvValue(val);
}
return out;
}
export const DefaultsManager = {
configure(opts) {
if (opts.configPath !== undefined)
state.cfg.configPath = opts.configPath;
if (opts.env !== undefined)
state.cfg.env = opts.env;
},
reset() {
state.cfg = { configPath: defaultConfigPath(), env: defaultEnv() };
state.configValues = {};
state.runtimeValues = {};
},
getConfigPath() {
return state.cfg.configPath;
},
async load() {
state.runtimeValues = {};
if (!existsSync(state.cfg.configPath)) {
state.configValues = {};
return;
}
try {
const raw = await readFile(state.cfg.configPath, "utf-8");
const parsed = JSON.parse(raw);
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
state.configValues = parsed;
}
else {
logger.warn("Defaults config file is not a JSON object, ignoring", {
path: state.cfg.configPath,
});
state.configValues = {};
}
}
catch (err) {
logger.warn("Failed to parse defaults config file, ignoring", {
path: state.cfg.configPath,
error: err instanceof Error ? err.message : err,
});
state.configValues = {};
}
},
/**
* Return the resolved view of all defaults, with source attribution.
* Precedence (lowest → highest): config → env → runtime.
*/
getAll() {
const merged = {};
for (const [k, v] of Object.entries(state.configValues)) {
merged[k] = { value: v, source: "config" };
}
for (const [k, v] of Object.entries(envDefaults(state.cfg.env))) {
merged[k] = { value: v, source: "env" };
}
for (const [k, v] of Object.entries(state.runtimeValues)) {
merged[k] = { value: v, source: "runtime" };
}
return merged;
},
get(key) {
return this.getAll()[key];
},
async set(updates, opts) {
for (const [k, v] of Object.entries(updates)) {
state.runtimeValues[k] = v;
}
if (opts?.persist) {
const merged = { ...state.configValues, ...updates };
state.configValues = merged;
const dir = dirname(state.cfg.configPath);
await mkdir(dir, { recursive: true });
await writeFile(state.cfg.configPath, JSON.stringify(merged, null, 2), "utf-8");
}
},
/**
* Fill missing or undefined fields in `args` from resolved defaults.
* Per-call args win — only undefined-or-missing keys get backfilled.
*/
apply(args) {
const result = { ...args };
const resolved = this.getAll();
for (const [k, { value }] of Object.entries(resolved)) {
if (result[k] === undefined)
result[k] = value;
}
return result;
},
};
//# sourceMappingURL=defaults-manager.js.map