src/common.js

/*
    Copyright 2022 juenbug12851

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file
 * @brief Shared core imported by both entry points. Owns the batch loop (run / processBatch / upscale) and the settings() accessors. Imports chdir.js FIRST so cwd is pinned to the repo root before any settings load. Notes: notes/systems/overview.md.
 */

// Ensure all relative paths resolve from the project root. Imported first so
// the chdir happens before settings loading reads cwd-relative files.
import "./chdir.js";

// Load imports
import fs from "node:fs";
import path from "node:path";
import { createRequire } from "node:module";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

import saveResults from "./helpers/saveResults.js";
import listFiles from "./helpers/listFiles.js";
import genImage from "./genImg.js";
import upscaleExisting from "./upscaleExisting.js";
import loadSettings from "./loadSettings.js";
import promptFiles from "./promptFilesAndSuggestions.js";
import { nodeLoader } from "./core/nodeLoader.js";

// Prompt modules are loaded by config-driven path; Node 24 can require() ES
// modules synchronously, which the prompt pipeline relies on.
const require = createRequire(import.meta.url);

// The dynamic-prompt suggestion builder is loader-injected; on the Node side it
// reads the catalog from the filesystem. Configure it once here so any code path
// that triggers #random (CLI or server) finds it ready.
promptFiles.configure(nodeLoader);

// Process given command-line arguments
const argv = yargs(hideBin(process.argv)).argv;

const { settings, defSettings, reloadSettings, saveSettings, replaceSettings, userSettings } =
  loadSettings;

// Ensure lastCmd is removed
settings().imageSettings.lastCmd = undefined;

/**
 * Build one prompt through the prompt-module pipeline and, when `generateImages` is
 * on and mode is StableDiffusion, generate its image(s).
 * @param {number} index The 0-based prompt index in the batch.
 * @param {number} total The total prompt count.
 * @returns {Promise<void>}
 */
async function processBatch(index, total) {
  // Copy over prompt from settings
  let prompt = settings().settings.prompt;

  // If an animation prompt has already been set, use that instead
  if (settings().settings.animationPromptSet != undefined)
    prompt = settings().settings.animationPromptSet;

  // Then pass it through prompt modules if any
  for (let i = 0; i < settings().settings.promptModules.length; i++) {
    let promptModuleName = settings().settings.promptModules[i];

    // If animation prompt is set, then only change salt
    if (settings().settings.animationPromptSet != undefined) promptModuleName = `prompt-salt`;

    const promptModuleFunc = require(
      `./${settings().settings.promptModuleFiles}/${promptModuleName}`,
    ).default;
    prompt = promptModuleFunc(
      prompt, // Prompt
      settings().settings, // Settings
      settings().imageSettings, // Image Settings
      settings().upscaleSettings, // Upscale Settings
    );

    // If animation prompt is set, then stop here
    if (settings().settings.animationPromptSet != undefined) break;
  }

  // Remove annoying windows line-endings
  prompt = prompt.replaceAll("\r", "");

  // If this is an animation, then update the prompt, this carries over the salt number
  if (settings().imageSettings.animationOf != undefined) {
    settings().settings.animationPromptSet = prompt;
  }

  // Send to console if not hidden
  if (!settings().settings.hidePrompt) {
    console.log();
    console.log(`Prompt: ${prompt}`);
    console.log();
  }

  // Save prompt
  if (settings().imageSettings.resultPrompts == undefined)
    settings().imageSettings.resultPrompts = [];

  settings().imageSettings.resultPrompts.push(prompt);
  saveResults(settings().imageSettings);

  // Make into image if filled in and not blank and targetting Stabloe Diffusion
  // Disallow if not targetting Stable Diffusion
  if (
    settings().settings.generateImages &&
    prompt != "" &&
    settings().settings.mode == "StableDiffusion"
  )
    await genImage(
      prompt,
      index,
      total,
      settings().settings,
      settings().imageSettings,
      settings().upscaleSettings,
    );

  // Bug Fix
  // If this is not added then for a batch of prompts, only the first prompt
  // has artists and fx added
  delete settings().imageSettings.autoIncludedFx;
  delete settings().imageSettings.autoIncludedArtists;
}

/**
 * Upscale an already-generated image by file id.
 * @param {(string|number)} fileId The image file id.
 * @returns {Promise<void>}
 */
async function upscale(fileId) {
  console.log(`Upscaling File ID: ${fileId.toString()}`);

  await upscaleExisting(
    fileId.toString(),
    settings().settings,
    settings().imageSettings,
    settings().upscaleSettings,
  );

  console.log("Done!");
  return;
}

/**
 * Run the full generate loop: clear results, then build `promptCount` prompts
 * (reloading lists between prompts as configured), awaiting each batch in turn.
 * @returns {Promise<void>}
 */
// Runs the code, this is in it's own function because we use asyc/await
// to make sure the first batch is done before we send another
async function run() {
  // Clear results file
  settings().imageSettings.resultPrompts = [];
  settings().imageSettings.resultImages = [];
  saveResults(settings().imageSettings);

  // Generate a prompt for each prompt count
  for (let i = 0; i < settings().settings.promptCount; i++) {
    // Release all list files from memory and re-scan for list filenames to be reloaded upon request IF
    // * This is the first prompt OR
    // * It is configured to reload lists on prompt change AND lists are confoigured to be unique
    // If duplicate list items are allowed then theres no point in list reloading
    if (
      (settings().settings.reloadListsOnPromptChange && settings().settings.listEntriesUsedOnce) ||
      i == 0
    )
      listFiles.reloadListFiles(settings().settings);

    await processBatch(i, settings().settings.promptCount);
  }
}

let legacyTxtNotice = false;

/**
 * Print the legacy-txt conversion notice once per process.
 * @returns {void}
 */
function sendLegacyTxtNotice() {
  if (legacyTxtNotice) return;

  console.log("Converting legacy txt files to json in output folder...");
  legacyTxtNotice = true;
}

/**
 * Recursively copy legacy `.txt` output sidecars to `.json` and remove the `.txt`.
 * @param {string} directoryName The directory to walk.
 * @returns {void}
 */
// Rename legacy txt files to json
const updateFiles = function (directoryName) {
  // get files in a directory
  const files = fs.readdirSync(directoryName);

  // Loop through them
  files.forEach(function (file) {
    // Get full path
    const fullPath = path.join(directoryName, file);

    // Is it a folder or file?
    const f = fs.statSync(fullPath);

    // Loop through folder if it is one
    if (f.isDirectory()) {
      updateFiles(fullPath);
    } else {
      // Make sure it's a txt file
      const ext = path.extname(fullPath).substring(1);
      if (ext != "txt") return;

      // Announce conversion begin
      sendLegacyTxtNotice();

      // Get path without extension
      const fullPathParsed = path.parse(fullPath);
      const fullBasePath = path.join(fullPathParsed.dir, fullPathParsed.name);

      // Copy it to a json file
      fs.cpSync(`${fullBasePath}.txt`, `${fullBasePath}.json`);

      // Remove txt file
      fs.rmSync(`${fullBasePath}.txt`);
    }
  });
};

// Scan directory
if (!settings().imageSettings.convertedLegacyData) updateFiles(settings().imageSettings.saveTo);

// Save conversion data
settings().imageSettings.convertedLegacyData = true;
saveSettings();

export default {
  argv,

  settings,
  defSettings,
  reloadSettings,
  saveSettings,
  replaceSettings,
  userSettings,

  genImage,
  processBatch,
  upscale,
  run,
};