Skip to content

CanvasAnvil is an open source AI diagram maker and flowchart generator - an infinite-canvas diagram editor and self-hosted drawio alternative that writes editable .drawio files. Text to diagram, then fix nodes, layout and labels in place. Also AI presentations, interior design CAD/BOM, posters and infographics.

3.5 KB
Raw Blame
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { OPENAI_DEFAULT_BASE_URL } from "./image-route.mjs";

// The skill's own config file, found from this script rather than from the
// caller's working directory, so the scripts can run from anywhere.
export const DEFAULT_IMAGE_CONFIG_FILE = fileURLToPath(
  new URL("../../config/image-provider.json", import.meta.url),
);

export function fail(message) {
  console.error(message);
  process.exit(1);
}

export function ensureDir(target) {
  fs.mkdirSync(target, { recursive: true });
}

export function readJson(filePath) {
  return JSON.parse(fs.readFileSync(filePath, "utf8"));
}

export function writeJson(filePath, value) {
  ensureDir(path.dirname(filePath));
  fs.writeFileSync(filePath, JSON.stringify(value, null, 2), "utf8");
}

export function resolveFromCwd(inputPath) {
  return path.resolve(process.cwd(), inputPath);
}

export function loadImageConfig(configFile) {
  const target = configFile ? resolveFromCwd(configFile) : DEFAULT_IMAGE_CONFIG_FILE;
  if (!fs.existsSync(target)) {
    fail(`Missing image config file: ${target}. Copy image-provider.example.json next to it and fill it in.`);
  }
  const raw = readJson(target);
  const apiKey = String(raw.apiKey || "").trim();
  const model = String(raw.model || "").trim();
  const baseUrl = String(raw.baseUrl || OPENAI_DEFAULT_BASE_URL).trim();
  if (!apiKey || !model) {
    fail("Missing image configuration. Required: apiKey, model.");
  }
  return {
    apiKey,
    model,
    baseUrl,
    sourceFile: target,
  };
}

export function decodeDataUrl(dataUrl) {
  const match = String(dataUrl || "").match(/^data:([^;]+);base64,(.*)$/);
  if (!match) fail("Image generation did not return a base64 data URL.");
  return {
    mime: match[1],
    buffer: Buffer.from(match[2], "base64"),
  };
}

export function imageExtensionFromMime(mime, fallback = "png") {
  const normalized = String(mime || "").toLowerCase();
  if (normalized.includes("jpeg") || normalized.includes("jpg")) return "jpg";
  if (normalized.includes("webp")) return "webp";
  if (normalized.includes("png")) return "png";
  return fallback;
}

export function readPromptInput({ prompt, promptFile }) {
  if (promptFile) {
    const filePath = resolveFromCwd(promptFile);
    const text = fs.readFileSync(filePath, "utf8");
    return { text, filePath };
  }
  return { text: String(prompt || ""), filePath: "" };
}

export function safeSlug(value, fallback = "slide") {
  const normalized = String(value || "")
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, "-")
    .replace(/^-+|-+$/g, "");
  return normalized || fallback;
}

export function loadManifest(filePath) {
  const target = resolveFromCwd(filePath);
  if (!fs.existsSync(target)) fail(`Missing manifest file: ${target}`);
  return { manifest: readJson(target), manifestPath: target };
}

export function fileToDataUrl(filePath) {
  const abs = resolveFromCwd(filePath);
  const ext = path.extname(abs).toLowerCase();
  const mime =
    ext === ".jpg" || ext === ".jpeg" ? "image/jpeg"
    : ext === ".webp" ? "image/webp"
    : "image/png";
  const buffer = fs.readFileSync(abs);
  return `data:${mime};base64,${buffer.toString("base64")}`;
}

export function parseSlideSize(value) {
  const raw = String(value || "16:9").trim();
  if (raw === "4:3") {
    return { layout: "LAYOUT_STANDARD", widthInches: 10, heightInches: 7.5, aspectRatio: raw };
  }
  return { layout: "LAYOUT_WIDE", widthInches: 13.333, heightInches: 7.5, aspectRatio: "16:9" };
}