Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

16.5 KB
Raw Blame
import type { AutoPick, AutoWhy, Operation } from "./catalog/types";

/*
 * Auto: the open image choice that picks the model for each run (the Image studio's default on the open engine). It is
 * a choice, not a model of its own: the server plans the run on the model it picks (src/server/generations.ts autoRouted)
 * and keeps the pick with the run (GenerationRequest.auto), so the run, its tile and the Library say which model made it.
 * Pure and browser-safe: the composer, the Settings page and the server read the same rules.
 */

/** Auto's id in the catalog (src/lib/catalog/models.ts). */
export const AUTO_IMAGE = "oss-auto-image";
export const AUTO_NAME = "Auto";

/**
 * Every plain prompt and every lettering prompt starts here, an account's, a guest's or the owner's: photoreal people and
 * products, painting, illustration and exact lettering. Its lane needs a large GPU (AUTO_LARGE_MODELS): where the GPUs a
 * run goes to can't take it, Auto starts on the next model.
 */
export const AUTO_LEAD_MODEL = "oss-minimax-h3-image";
/** Next for a plain prompt, and first in the built-in order for prompts the patterns match: natural skin, light and faces. */
export const AUTO_PHOTO_MODEL = "oss-krea2-realism";
/** Next for lettering (posters, logos, signs, words in quotes): MOSS-Image follows the words and draws them in place. */
export const AUTO_TEXT_MODEL = "oss-moss-image";
/**
 * Auto's text-to-image models, in order for a plain prompt (lettering takes MOSS-Image before Krea 2 Realism; a prompt the
 * patterns match follows the order for matched prompts, DEFAULT_TRIGGER_MODELS or the saved one). When one is switched off or can't run here, the run is planned on the next;
 * when it fails, the next makes the same run (src/server/generations.ts autoFails), each model at most once.
 */
export const AUTO_T2I_MODELS = [AUTO_LEAD_MODEL, AUTO_PHOTO_MODEL, AUTO_TEXT_MODEL] as const;
/**
 * Auto's models whose lane needs a large GPU (MiniMax H3 Image: about 32 GB for its 21 GB model and 27 GB text encoder).
 * The server plans one only where the GPUs the run goes to can take that lane and hold its files (src/server/auto-gpu.ts);
 * elsewhere the run starts on the next model.
 */
export const AUTO_LARGE_MODELS: ReadonlySet<string> = new Set([AUTO_LEAD_MODEL]);
/**
 * Ideogram 4 picked by name (Auto never plans it): when it declines a run (its grey "blocked" card for every picture),
 * the same run is made by these, in order, each only if the one before fails.
 */
export const DECLINED_FALLBACKS = ["oss-krea2-realism", "oss-fibo2-dev", "oss-moss-image"] as const;
/**
 * A run with a picture to work from goes to the studio's own default for its operation (the Edit and Inpaint studios'
 * Qwen-Image 2.1). FIBO 2 Dev, which also edits, is next where Qwen-Image 2.1 is switched off.
 */
export const AUTO_EDIT_MODELS: Readonly<Partial<Record<Operation, readonly string[]>>> = {
  edit: ["oss-qwen-image-21", "oss-fibo2-dev"],
  inpaint: ["oss-qwen-image-21"],
  outpaint: ["oss-qwen-image-21"],
};

/**
 * The built-in order for a prompt the setting's patterns match. Settings → Auto image model can save another
 * (autoTriggerModels, key auto.triggerModels: model ids, comma-separated); an empty value means this one.
 */
export const DEFAULT_TRIGGER_MODELS: readonly string[] = [AUTO_PHOTO_MODEL, AUTO_LEAD_MODEL, AUTO_TEXT_MODEL];
/** How many models the order may list. */
export const AUTO_TRIGGER_MAX = 10;

/** The fields of a catalog model the order's checks read. */
export interface AutoModelLike {
  engine: string;
  kind: string;
  hidden?: boolean;
  routes: readonly { op: string }[];
}

/** A model the order may list: an open-engine image model in the catalog that makes text to image (never Auto itself). */
export const autoT2iModel = (id: string, m: AutoModelLike | undefined): boolean =>
  !!m && id !== AUTO_IMAGE && m.engine === "open" && m.kind === "image" && !m.hidden && m.routes.some((r) => r.op === "t2i");

/** The setting's value as model ids, in order (comma-separated; blanks left out). */
export const triggerModelIds = (text: string): string[] =>
  text
    .split(",")
    .map((s) => s.trim())
    .filter(Boolean);

/**
 * Why the order can't be saved, one line per problem (empty: it can): 1 to AUTO_TRIGGER_MAX models, each one the catalog
 * knows that makes text to image on the open engine, none twice. An empty value is the built-in order.
 */
export function autoTriggerModelsProblems(text: string, lookup: (id: string) => AutoModelLike | undefined): string[] {
  if (!text.trim()) return [];
  const ids = triggerModelIds(text);
  const problems: string[] = [];
  if (!ids.length) problems.push("List at least one model");
  if (ids.length > AUTO_TRIGGER_MAX) problems.push(`Keep it to ${AUTO_TRIGGER_MAX} models`);
  const seen = new Set<string>();
  for (const id of ids) {
    const m = lookup(id);
    if (!m) problems.push(`${id.slice(0, 80)} isn't a model this install has`);
    else if (!autoT2iModel(id, m)) problems.push(`${id} doesn't make pictures from words on the open engine`);
    if (seen.has(id)) problems.push(`${id.slice(0, 80)} is listed twice`);
    seen.add(id);
  }
  return [...new Set(problems)];
}

/**
 * The saved order as Auto uses it: the models it may list, in order, each once. The built-in order when the value is
 * empty or lists none it may (a model the catalog no longer has is left out).
 */
export function triggerOrder(text: string, lookup: (id: string) => AutoModelLike | undefined): string[] {
  const ids = [...new Set(triggerModelIds(text))].filter((id) => autoT2iModel(id, lookup(id))).slice(0, AUTO_TRIGGER_MAX);
  return ids.length ? ids : [...DEFAULT_TRIGGER_MODELS];
}

/**
 * An account's own order for its plain and matched prompts on this install's GPUs, as the billing service's policy gives
 * it (Policy.auto.image): the models Auto may list, in order, each once, at most AUTO_TRIGGER_MAX. Null when it lists
 * none (the install's own orders then apply).
 */
export function ownAutoOrder(ids: unknown, lookup: (id: string) => AutoModelLike | undefined): string[] | null {
  if (!Array.isArray(ids)) return null;
  const out = [...new Set(ids.filter((id): id is string => typeof id === "string").map((id) => id.trim()))]
    .filter((id) => autoT2iModel(id, lookup(id)))
    .slice(0, AUTO_TRIGGER_MAX);
  return out.length ? out : null;
}

/**
 * The models Auto may plan an operation on: text to image on AUTO_T2I_MODELS and the order for matched prompts (`trigger`,
 * DEFAULT_TRIGGER_MODELS when not given), the others on AUTO_EDIT_MODELS.
 */
export const autoModelsFor = (op: Operation, trigger: readonly string[] = DEFAULT_TRIGGER_MODELS): readonly string[] =>
  op === "t2i" ? [...new Set([...AUTO_T2I_MODELS, ...trigger])] : (AUTO_EDIT_MODELS[op] ?? []);

/** Every model Auto may plan a run on with the built-in order for matched prompts. */
export const AUTO_MODELS: ReadonlySet<string> = new Set([...AUTO_T2I_MODELS, ...Object.values(AUTO_EDIT_MODELS).flat()]);

/**
 * Prompts about lettering, which go to MOSS-Image right after MiniMax H3 Image. Words in quotes count too ("a sign that
 * says “OPEN”").
 */
const TEXT_WORDS =
  /\b(text|texts|typography|typographic|typeface|font|fonts|lettering|letters|logo|logos|logotype|wordmark|monogram|poster|posters|signage|signs?|billboard|banner|headline|caption|calligraphy|graffiti|label|labels|menu|book cover|album cover|magazine cover|infographic|says|that reads)\b/i;
const QUOTED = /["“”«»][^"“”«»\n]{2,80}["“”«»]/;

/**
 * The setting's list (Settings → Auto image model): prompts Auto starts on the order for matched prompts
 * (DEFAULT_TRIGGER_MODELS, or the saved one), and whose run details name the line they matched. Lettering still follows
 * its own order. One per line: a word or phrase matches as a whole word (a space or a hyphen matches either), any other line is a regular
 * expression, and a line that starts with "#" is a note; case never matters.
 */
export const DEFAULT_AUTO_PATTERNS: readonly string[] = [
  "# Animals or creatures dressed as people",
  String.raw`\b(cats?|kittens?|dogs?|pupp(y|ies)|fox(es)?|bunn(y|ies)|rabbits?|bears?|wol(f|ves)|lions?|tigers?|pandas?|owls?|monkeys?|horses?|deer|mice|mouse|creatures?|animals?)\b.{0,80}\b(gowns?|dress(es)?|suits?|tuxedos?|lingerie|bikinis?|heels|corsets?|skirts?|blouses?)\b`,
  String.raw`\b(gowns?|dress(es)?|suits?|tuxedos?|lingerie|bikinis?|heels|corsets?)\b.{0,80}\b(cats?|kittens?|dogs?|pupp(y|ies)|fox(es)?|bunn(y|ies)|rabbits?|bears?|wol(f|ves)|lions?|tigers?|pandas?|owls?|monkeys?|creatures?|animals?)\b`,
  String.raw`\b(cats?|kittens?|dogs?|pupp(y|ies)|fox(es)?|bunn(y|ies)|rabbits?|bears?|wol(f|ves)|lions?|tigers?|pandas?|owls?|monkeys?|creatures?|animals?)\b.{0,160}\b(her|his) (hair|body|legs|figure|curves|skin)\b`,
  "# Swimwear, underwear and sheer clothes",
  "swimwear",
  "swimsuit",
  "bikini",
  "lingerie",
  "underwear",
  "bodysuit",
  "sheer",
  "see-through",
  "topless",
  "nude-tone",
];

/** The setting's limits: lines, and characters a line. */
export const AUTO_PATTERNS_MAX = { lines: 200, chars: 300 };

/** A line that is a word or phrase (matched as a whole word), not a regular expression. */
const PLAIN = /^[\p{L}\p{N}][\p{L}\p{N}' -]*$/u;

/** One line of the setting as a pattern (case-insensitive). Null: a blank line or a comment ("# …"). Throws on a bad one. */
function lineRegExp(line: string): RegExp | null {
  const s = line.trim();
  if (!s || s.startsWith("#")) return null;
  // A space or a hyphen matches either ("see-through", "see through"); an apostrophe either kind.
  if (PLAIN.test(s)) return new RegExp(`(?<![\\p{L}\\p{N}])${s.split(/[-\s]+/).join("[-\\s]+").replace(/'/g, "['’]")}(?![\\p{L}\\p{N}])`, "iu");
  return new RegExp(s, "i");
}

/**
 * Why the setting's text can't be saved, one line per problem (empty: it can). Each line must be a pattern that can be
 * read, can't match empty words (it would match every prompt), and stays within the limits.
 */
export function autoPatternsProblems(text: string): string[] {
  const lines = text.split(/\r?\n/);
  const problems: string[] = [];
  if (lines.length > AUTO_PATTERNS_MAX.lines) problems.push(`Keep it to ${AUTO_PATTERNS_MAX.lines} lines`);
  lines.forEach((line, i) => {
    if (line.length > AUTO_PATTERNS_MAX.chars) return void problems.push(`Line ${i + 1} is over ${AUTO_PATTERNS_MAX.chars} characters`);
    let re: RegExp | null;
    try {
      re = lineRegExp(line);
    } catch {
      return void problems.push(`Line ${i + 1} isn't a pattern that can be read: ${line.trim().slice(0, 60)}`);
    }
    if (re && re.test("")) problems.push(`Line ${i + 1} matches every prompt: ${line.trim().slice(0, 60)}`);
  });
  return problems;
}

/** The setting's text as patterns (lines that can't be read are left out, as saving refuses them). */
export function autoPatterns(text: string): { line: string; re: RegExp }[] {
  return text.split(/\r?\n/).flatMap((line) => {
    try {
      const re = lineRegExp(line);
      return re && !re.test("") ? [{ line: line.trim(), re }] : [];
    } catch {
      return [];
    }
  });
}

/** The first line of the setting a prompt matches, or undefined. */
export function matchedPattern(prompt: string, patterns: readonly { line: string; re: RegExp }[]): string | undefined {
  return patterns.find((p) => p.re.test(prompt))?.line;
}

/** The words TEXT_WORDS knows, plus watermarks, for the "no …" lists below. */
const TEXT_NOUN = String.raw`(?:text|texts|typography|typeface|fonts?|lettering|letters|logos?|logotypes?|wordmarks?|monograms?|signage|signs?|banners?|headlines?|captions?|calligraphy|graffiti|labels?|watermarks?|words|writing)`;
/**
 * A prompt that only rules lettering out ("no text, no watermark", "without any logos or captions", "text-free"): these
 * words don't ask for it, so they are left out before TEXT_WORDS reads the prompt.
 */
const NO_TEXT = new RegExp(
  String.raw`\b(?:no|not any|without(?: any)?|free of|zero|avoid(?:ing)?|minus)\s+(?:(?:any|visible|readable|legible|added|extra|stray|on-?screen|overlaid|overlay)\s+)*${TEXT_NOUN}(?:\s*(?:,|\/|or|and|nor)\s*(?:(?:no|any)\s+)?${TEXT_NOUN})*\b|\b${TEXT_NOUN}-?(?:free|less)\b`,
  "gi",
);

/** Is this prompt about lettering (posters, logos, signs, typography, words in quotes)? Lettering it rules out doesn't count. */
export const asksForText = (prompt: string): boolean => {
  const asked = prompt.replace(NO_TEXT, " ");
  return TEXT_WORDS.test(asked) || QUOTED.test(asked);
};

export interface AutoAsk {
  prompt?: string;
  op?: Operation;
  /** Pictures the run works from (its references). */
  images?: number;
}

/**
 * The models Auto would plan a run on, best first, and why: a picture to work from → the studio's edit model for its
 * operation; lettering → MiniMax H3 Image, then MOSS-Image, then Krea 2 Realism; a prompt the setting's patterns match →
 * `trigger` in its order (the saved order; DEFAULT_TRIGGER_MODELS when not given or empty); anything else → MiniMax H3
 * Image, then Krea 2 Realism, then MOSS-Image. The same for the owner, an account and a guest. The server plans the first
 * one that is switched on, runs the operation and fits the GPUs the run goes to (src/server/generations.ts autoRouted),
 * and the next one makes the run when it fails (autoNext). `plain`: the order for a plain prompt (AUTO_T2I_MODELS when
 * not given or empty; an account's own order on this install's GPUs, ownAutoOrder).
 */
export function autoCandidates(
  ask: AutoAsk,
  patterns: readonly { line: string; re: RegExp }[],
  trigger: readonly string[] = DEFAULT_TRIGGER_MODELS,
  plain: readonly string[] = AUTO_T2I_MODELS,
): { models: string[]; why: AutoWhy; pattern?: string } {
  const op = ask.op && ask.op !== "t2i" ? ask.op : (ask.images ?? 0) > 0 ? "edit" : "t2i";
  if (op !== "t2i") return { models: [...(AUTO_EDIT_MODELS[op] ?? [])], why: "edit" };
  const words = ask.prompt ?? "";
  if (asksForText(words)) return { models: [AUTO_LEAD_MODEL, AUTO_TEXT_MODEL, AUTO_PHOTO_MODEL], why: "text" };
  const pattern = matchedPattern(words, patterns);
  if (pattern) return { models: [...new Set(trigger.length ? trigger : DEFAULT_TRIGGER_MODELS)], why: "trigger", pattern };
  return { models: [...new Set(plain.length ? plain : AUTO_T2I_MODELS)], why: "plain" };
}

/** A size tier's place in order ("1K" < "1.5K" < "2K" < "4K"); NaN for a size that isn't a tier. */
const tierOf = (r: string) => (/^\d+(\.\d+)?k$/i.test(r) ? parseFloat(r) : NaN);

/**
 * The size a run of Auto asks of the model it picks: as asked, unless every size that model makes is larger (MiniMax H3
 * Image makes 2K and 4K, so Auto's 1K and 1.5K run at its 2K default). A size that isn't a tier stays as it is.
 */
export function autoResolution(asked: string | undefined, model: { options: { resolutions?: readonly string[] }; defaults?: { resolution?: string } } | undefined): string | undefined {
  const sizes = model?.options.resolutions;
  const to = model?.defaults?.resolution;
  if (!asked || !sizes?.length || !to || sizes.includes(asked)) return asked;
  const t = tierOf(asked);
  return Number.isFinite(t) && sizes.every((s) => tierOf(s) > t) ? to : asked;
}

/** Auto's models left for a run, in its order, after the ones that tried it (each model makes a run at most once). */
export const autoNext = (models: readonly string[], tried: readonly string[]): string[] => models.filter((m) => !tried.includes(m));

/** The model that makes a run Ideogram 4 (picked by name) declined after `after` failed too (undefined: none left). */
export function nextDeclinedFallback(after: string | undefined): string | undefined {
  const i = after ? (DECLINED_FALLBACKS as readonly string[]).indexOf(after) : -1;
  return DECLINED_FALLBACKS[i + 1];
}

/** Said after the model that made an Auto run when the model before it couldn't make it. */
export const AUTO_NEXT = "Auto's next model";

/**
 * The line under an Auto run (the feed card, the run details, the Library, Accounts' runs): "Auto · made with Krea 2
 * Realism", and "· Auto's next model" when the model before it couldn't make it (or, on an older run, Ideogram 4
 * declined it). Null for any other run.
 */
export function autoLine(g: { status: string; modelName?: string; modelId: string; request: { modelId: string; auto?: AutoPick } }): string | null {
  if (g.request.modelId !== AUTO_IMAGE || !g.request.auto) return null;
  const name = g.modelName ?? g.modelId;
  const made = g.status === "succeeded" ? `made with ${name}` : g.status === "failed" || g.status === "canceled" ? `tried ${name}` : `making it with ${name}`;
  const next = g.request.auto.why === "fallback" || g.request.auto.why === "declined";
  return [AUTO_NAME, made, next ? AUTO_NEXT : null].filter(Boolean).join(" · ");
}