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.

4.1 KB
Raw Blame
/**
 * The contract between the browser and the API.
 *
 * The client sends its AI settings with every request and the server
 * interprets them, so both sides need the same shape and the same
 * normalisation. Nothing here touches a browser or a Node API, which is what
 * lets it sit outside both.
 */

export interface AIChannel {
  apiKey: string;
  baseUrl: string;
  model: string;
}

/**
 * Persisted settings. Text and image are separate channels because the two
 * are commonly served by different endpoints; each is still plain OpenAI.
 */
export interface AIConfig {
  textApiKey: string;
  textBaseUrl: string;
  textModel: string;
  imageApiKey: string;
  imageBaseUrl: string;
  imageModel: string;
  fileParserApiToken: string;
  systemPrompt: string;
}

export type ChatRole = "system" | "user" | "assistant";

export type ChatContentPart =
  | { type: "text"; text: string }
  | { type: "image_url"; image_url: { url: string } };

/** The common case: a text-only turn. */
export interface ChatMessage {
  role: ChatRole;
  content: string;
}

/** A turn that may also carry images, using OpenAI's content-part array. */
export interface MultimodalMessage {
  role: ChatRole;
  content: string | ChatContentPart[];
}

export interface TextRequest {
  channel: AIChannel;
  messages: MultimodalMessage[];
  signal?: AbortSignal;
}

/**
 * Every size and quality a client may ask for. The server validates against
 * these same lists, so a value offered in the UI can never be silently
 * dropped on the way to the model.
 */
export const IMAGE_SIZES = [
  "auto",
  "256x256",
  "512x512",
  "1024x1024",
  "1536x1024",
  "1024x1536",
  "1792x1024",
  "1024x1792",
] as const;
export const IMAGE_QUALITIES = ["auto", "low", "medium", "high"] as const;

export type ImageSize = (typeof IMAGE_SIZES)[number];
export type ImageQuality = (typeof IMAGE_QUALITIES)[number];

export interface ImageRequest {
  channel: AIChannel;
  prompt: string;
  /** Primary reference image. Its presence selects the `/images/edits` route. */
  referenceImageUrl?: string;
  additionalReferenceImageUrls?: string[];
  maskImageUrl?: string;
  /** gpt-image-* size/quality knobs; ignored by the chat-completions fallback route. */
  size?: ImageSize;
  quality?: ImageQuality;
  signal?: AbortSignal;
}

export const DEFAULT_BASE_URL = "https://api.openai.com/v1";

export const DEFAULT_AI_CONFIG: AIConfig = {
  textApiKey: "",
  textBaseUrl: DEFAULT_BASE_URL,
  textModel: "gpt-4o-mini",
  imageApiKey: "",
  imageBaseUrl: DEFAULT_BASE_URL,
  imageModel: "gpt-image-1",
  fileParserApiToken: "",
  systemPrompt: "",
};

const str = (value: unknown, fallback = "") => {
  const text = String(value ?? "").trim();
  return text || fallback;
};

/**
 * Coerce anything that claims to be an AIConfig into a complete one.
 *
 * Also migrates configs written by the pre-OpenAI-only settings dialog, which
 * stored a single shared `apiKey`/`baseUrl` alongside per-channel overrides
 * and named the models `chatModel` / `imageModelLegacy`.
 */
export function normalizeAIConfig(raw: unknown): AIConfig {
  const input = (raw && typeof raw === "object" ? raw : {}) as Record<string, unknown>;
  const sharedApiKey = str(input.apiKey);
  const sharedBaseUrl = str(input.baseUrl);

  return {
    textApiKey: str(input.textApiKey, sharedApiKey),
    textBaseUrl: str(input.textBaseUrl, sharedBaseUrl || DEFAULT_BASE_URL),
    textModel: str(input.textModel, str(input.chatModel)),
    imageApiKey: str(input.imageApiKey, sharedApiKey),
    imageBaseUrl: str(input.imageBaseUrl, sharedBaseUrl || DEFAULT_BASE_URL),
    imageModel: str(input.imageModel, str(input.imageModelLegacy)),
    fileParserApiToken: str(input.fileParserApiToken),
    systemPrompt: String(input.systemPrompt ?? "").trim(),
  };
}

export function getTextChannel(config: AIConfig): AIChannel {
  return {
    apiKey: config.textApiKey,
    baseUrl: config.textBaseUrl || DEFAULT_BASE_URL,
    model: config.textModel,
  };
}

export function getImageChannel(config: AIConfig): AIChannel {
  return {
    apiKey: config.imageApiKey,
    baseUrl: config.imageBaseUrl || DEFAULT_BASE_URL,
    model: config.imageModel,
  };
}