AI media harness for planning, generating, and editing locally
TypeScript
Add per-LoRA multipliers
Bitti88ee4c674175
diff --git a/docs/generator-weights.md b/docs/generator-weights.mdindex cb8b62c..bae69d6 100644--- a/docs/generator-weights.md+++ b/docs/generator-weights.md@@ -26,6 +26,13 @@ are passed to SlopFab in displayed order on both CUDA and Vulkan. SlopFab combinadapter updates by summing them. Negative strengths are supported. An activemissing adapter causes an error rather than silently changing the generation.+Open any LoRA from the library to set its **Multiplier** (default **1**).+This multiplies its strength in every generator template. For example, a LoRA+multiplier of **0.75** and a template strength of **0.5** send **0.375** to the+engine. Negative values are supported. A multiplier of **0** disables the+adapter, skips its download, and excludes its step override. Existing LoRAs+without a saved multiplier keep their original strength.+Open any LoRA from the library to edit its optional **Override step count**.Any supported scene step count (a whole number from 2 to 2147483647) is accepted.The highest override among enabled, nonzero-strength LoRAs replaces the scene'sdiff --git a/src/components/LoraSettings.tsx b/src/components/LoraSettings.tsxindex 3d7d5e8..1d16720 100644--- a/src/components/LoraSettings.tsx+++ b/src/components/LoraSettings.tsx@@ -1,7 +1,7 @@import { ArrowDown, ArrowUp, Download, FolderSearch, Layers, Plus, Trash2 } from "lucide-react";import "../styles/loras.css";import { useEffect, useState, useSyncExternalStore } from "react";-import { highestLoraStepOverride, isLoraStepOverride, loadLoras, saveLoras, subscribeLoras, type Lora, type TemplateLora } from "../lib/loras";+import { effectiveLoraStrength, highestLoraStepOverride, isLoraMultiplier, isLoraStepOverride, loadLoras, MAX_LORA_MULTIPLIER, saveLoras, subscribeLoras, type Lora, type TemplateLora } from "../lib/loras";import { MAX_GENERATION_STEPS } from "../lib/project";import { isTauri } from "../lib/persistence";import { chooseEnginePath } from "../lib/runtime";@@ -44,6 +44,8 @@ export function LoraEditor({ loraId, onDone }: { loraId: string; onDone: () => vconst existing = loadLoras().find(({ id }) => id === loraId);const [name, setName] = useState(existing?.name ?? "");const [path, setPath] = useState(existing?.path ?? "");+ const [multiplier, setMultiplier] = useState(String(existing?.multiplier ?? 1));+ const validMultiplier = multiplier.trim() !== "" && isLoraMultiplier(Number(multiplier));const [override, setOverride] = useState(existing?.stepOverride !== undefined);const [steps, setSteps] = useState(String(existing?.stepOverride ?? 3));const [error, setError] = useState<string | null>(null);@@ -56,13 +58,13 @@ export function LoraEditor({ loraId, onDone }: { loraId: string; onDone: () => v} catch (reason) { setError(String(reason)); }};const save = () => {- if (!name.trim() || (!path.trim() && !existing?.url)) return;+ if (!name.trim() || (!path.trim() && !existing?.url) || !validMultiplier) return;if (path.trim() && (!/^(?:[a-z]:[\\/]|\\\\|\/)/i.test(path.trim()) || path.includes("\0"))) {setError("Choose a local LoRA file using its absolute path."); return;}try {const library = loadLoras();- const entry: Lora = { ...library.find(({ id }) => id === loraId), id: loraId, name: name.trim(), path: path.trim(), stepOverride: override ? Number(steps) : undefined };+ const entry: Lora = { ...library.find(({ id }) => id === loraId), id: loraId, name: name.trim(), path: path.trim(), multiplier: Number(multiplier), stepOverride: override ? Number(steps) : undefined };saveLoras(existing ? library.map((lora) => lora.id === loraId ? entry : lora) : [...library, entry]);onDone();} catch (reason) { setError(String(reason)); }@@ -73,11 +75,13 @@ export function LoraEditor({ loraId, onDone }: { loraId: string; onDone: () => v<label>Name<input aria-label="LoRA name" autoFocus value={name} onChange={(event) => setName(event.target.value)} /></label><label>Local file<div className="settings-path__row"><input aria-label="LoRA path" value={path} onChange={(event) => setPath(event.target.value)} placeholder="Absolute path to a .safetensors file" /><button className="secondary-button" type="button" disabled={!isTauri()} onClick={() => void browse()}><FolderSearch size={16} /> Browse</button></div></label>+ <label>Multiplier<input aria-label="LoRA multiplier" aria-describedby="lora-multiplier-help" type="number" min={-MAX_LORA_MULTIPLIER} max={MAX_LORA_MULTIPLIER} step="any" value={multiplier} onChange={(event) => setMultiplier(event.target.value)} /></label>+ <p id="lora-multiplier-help">Multiplies this LoRA's strength in every generator. 1 keeps the original strength, 0 disables it, and negative values reverse its effect.</p><label className="lora-toggle"><input type="checkbox" checked={override} onChange={(event) => setOverride(event.target.checked)} /> Override step count</label>{override && <label>Step count<input aria-label="LoRA step override" type="number" min={2} max={MAX_GENERATION_STEPS} step={1} value={steps} onChange={(event) => setSteps(event.target.value)} /></label>}<p>The highest override among active LoRAs replaces the scene's step count. Without an override, the scene's steps are used.</p><p>The file stays in its current location.</p>- <button className="primary-button" type="button" disabled={!name.trim() || (!path.trim() && !existing?.url) || (override && !isLoraStepOverride(Number(steps)))} onClick={save}>{existing ? "Save Lora" : "Add Lora"}</button>+ <button className="primary-button" type="button" disabled={!name.trim() || (!path.trim() && !existing?.url) || !validMultiplier || (override && !isLoraStepOverride(Number(steps)))} onClick={save}>{existing ? "Save Lora" : "Add Lora"}</button>{error && <p role="alert">{error}</p>}</div></div>;@@ -85,7 +89,8 @@ export function LoraEditor({ loraId, onDone }: { loraId: string; onDone: () => vexport function TemplateLorasEditor({ value, onChange }: { value: TemplateLora[]; onChange: (value: TemplateLora[]) => void }) {const library = useLoras();- const stepOverride = highestLoraStepOverride(value.filter((entry) => entry.enabled && entry.strength !== 0)+ const active = value.filter((entry) => entry.enabled && effectiveLoraStrength(entry, library.find(({ id }) => id === entry.loraId)) !== 0);+ const stepOverride = highestLoraStepOverride(active.flatMap((entry) => library.filter(({ id }) => id === entry.loraId)));const available = library.filter((entry) => (entry.path || entry.url) && !value.some(({ loraId }) => loraId === entry.id));const update = (index: number, patch: Partial<TemplateLora>) => onChange(value.map((entry, i) => i === index ? { ...entry, ...patch } : entry));@@ -95,8 +100,8 @@ export function TemplateLorasEditor({ value, onChange }: { value: TemplateLora[]onChange(next);};return <section className="template-loras" aria-labelledby="template-loras-heading">- <h3 id="template-loras-heading">LoRAs <small>{value.filter((entry) => entry.enabled && entry.strength !== 0).length} active</small></h3>- <p>Enable any number of adapters and arrange their order. Strength 0 disables an adapter.</p>+ <h3 id="template-loras-heading">LoRAs <small>{active.length} active</small></h3>+ <p>Enable adapters and arrange their order. Strength is multiplied by each LoRA's multiplier in its settings. Either value at 0 disables the adapter.</p><p>Missing active LoRAs with download links are downloaded with the generator.</p>{value.map((entry, index) => {const lora = library.find(({ id }) => id === entry.loraId);@@ -105,6 +110,7 @@ export function TemplateLorasEditor({ value, onChange }: { value: TemplateLora[]<span>{index + 1}.</span><label className="lora-toggle"><input type="checkbox" checked={entry.enabled} aria-label={`Enable ${name}`} onChange={(event) => update(index, { enabled: event.target.checked })} /> {name}{!lora?.path && (lora?.url ? " (download required)" : " (file unavailable)")}</label><label>Strength<input type="number" step="0.1" aria-label={`${name} strength`} value={entry.strength} onChange={(event) => { const strength = Number(event.target.value); if (event.target.value && Number.isFinite(strength)) update(index, { strength }); }} /></label>+ {lora?.multiplier !== undefined && lora.multiplier !== 1 && <small aria-label={`${name} effective strength`}>× {lora.multiplier} = {Number(effectiveLoraStrength(entry, lora).toPrecision(6))}</small>}<button className="icon-button" type="button" aria-label={`Move ${name} up`} disabled={index === 0} onClick={() => move(index, -1)}><ArrowUp size={15} /></button><button className="icon-button" type="button" aria-label={`Move ${name} down`} disabled={index === value.length - 1} onClick={() => move(index, 1)}><ArrowDown size={15} /></button><button className="icon-button" type="button" aria-label={`Deactivate and remove ${name}`} onClick={() => onChange(value.filter((_, i) => i !== index))}><Trash2 size={15} /></button>diff --git a/src/components/SettingsView.downloads.test.tsx b/src/components/SettingsView.downloads.test.tsxindex 54c9d77..1f2fade 100644--- a/src/components/SettingsView.downloads.test.tsx+++ b/src/components/SettingsView.downloads.test.tsx@@ -116,18 +116,25 @@ it("downloads TaoMate and persists manual adapters, activation, strength and ordexpect(screen.getByRole("heading", { name: "Add Lora" })).toBeInTheDocument();fireEvent.change(screen.getByLabelText("LoRA name"), { target: { value: "My style" } });fireEvent.change(screen.getByLabelText("LoRA path"), { target: { value: "D:/models/style.safetensors" } });+ expect(screen.getByLabelText("LoRA multiplier")).toHaveValue(1);+ fireEvent.change(screen.getByLabelText("LoRA multiplier"), { target: { value: "" } });+ expect(screen.getByRole("button", { name: "Add Lora" })).toBeDisabled();+ fireEvent.change(screen.getByLabelText("LoRA multiplier"), { target: { value: "0.75" } });fireEvent.click(screen.getByLabelText("Override step count"));fireEvent.change(screen.getByLabelText("LoRA step override"), { target: { value: "8" } });fireEvent.click(screen.getByRole("button", { name: "Add Lora" }));const manual = loadLoras().find(({ name }) => name === "My style")!;expect(manual.stepOverride).toBe(8);+ expect(manual.multiplier).toBe(0.75);fireEvent.click(screen.getByRole("button", { name: "Edit My style LoRA" }));expect(screen.getByLabelText("LoRA step override")).toHaveValue(8);+ expect(screen.getByLabelText("LoRA multiplier")).toHaveValue(0.75);fireEvent.click(screen.getByRole("button", { name: "Generators" }));fireEvent.click(screen.getByRole("button", { name: "Edit Default generator" }));fireEvent.change(screen.getByLabelText("Add LoRA to generator"), { target: { value: TAOMATE_LORA.id } });fireEvent.change(screen.getByLabelText("Add LoRA to generator"), { target: { value: manual.id } });fireEvent.change(screen.getByLabelText("My style strength"), { target: { value: "-0.5" } });+ expect(screen.getByLabelText("My style effective strength")).toHaveTextContent("-0.375");fireEvent.click(screen.getByRole("button", { name: "Move My style up" }));fireEvent.click(screen.getByLabelText("Enable TaoMate 3-Step"));expect(screen.getByText("1 active")).toBeInTheDocument();@@ -138,6 +145,7 @@ it("downloads TaoMate and persists manual adapters, activation, strength and ordrender(<SettingsView onClose={() => undefined} />);fireEvent.click(screen.getByRole("button", { name: "Edit Default generator" }));expect(screen.getByLabelText("My style strength")).toHaveValue(-0.5);+ expect(screen.getByLabelText("My style effective strength")).toHaveTextContent("-0.375");expect(screen.getByLabelText("Enable TaoMate 3-Step")).not.toBeChecked();expect(screen.getByRole("button", { name: "Move My style up" })).toBeDisabled();});diff --git a/src/lib/loras.test.ts b/src/lib/loras.test.tsindex 9681d67..3a3d4c9 100644--- a/src/lib/loras.test.ts+++ b/src/lib/loras.test.ts@@ -1,11 +1,38 @@// @vitest-environment jsdomimport { beforeEach, expect, it } from "vitest";-import { loadLoras, saveLoras, TAOMATE_LORA, TURBO_LORA, VIGGLE_ANIMATE_LORA } from "./loras";+import { downloadableTemplateLoras, loadLoras, MAX_LORA_MULTIPLIER, saveLoras, TAOMATE_LORA, TURBO_LORA, VIGGLE_ANIMATE_LORA } from "./loras";import { createGeneratorTemplate, engineProviderSetting, generationStepsWithLoras, loadGeneratorTemplateSettings, saveGeneratorTemplateSettings, withEngineSettings } from "./settings";import { createProjectConfig } from "./project";beforeEach(() => localStorage.clear());+it("persists library multipliers and combines them with template strengths for generation", () => {+ saveLoras([+ { id: "style", name: "Style", path: "D:/style.safetensors", multiplier: 0.75 },+ { ...TURBO_LORA, multiplier: 0 },+ ]);+ const selection = [+ { loraId: "style", enabled: true, strength: -0.5 },+ { loraId: TURBO_LORA.id, enabled: true, strength: 1 },+ ];+ expect(loadLoras().find(({ id }) => id === "style")?.multiplier).toBe(0.75);+ expect(downloadableTemplateLoras(selection)).toEqual([]);+ const options = engineProviderSetting(createGeneratorTemplate().paths, undefined, "sage2", selection).options;+ expect(JSON.parse(options.loras as string)).toEqual([{ path: "D:/style.safetensors", strength: -0.375 }]);+ expect(options).not.toHaveProperty("stepOverride");+});++it.each([NaN, Infinity, -Infinity, 1e39])("rejects invalid library multiplier %s", (multiplier) => {+ expect(() => saveLoras([{ ...TURBO_LORA, multiplier }])).toThrow("LoRA multiplier must be a finite number");+});++it("rejects combined strengths outside the engine's numeric range", () => {+ saveLoras([{ ...TURBO_LORA, path: "D:/turbo.safetensors", multiplier: MAX_LORA_MULTIPLIER }]);+ expect(() => engineProviderSetting(createGeneratorTemplate().paths, undefined, "sage2", [+ { loraId: TURBO_LORA.id, enabled: true, strength: 2 },+ ])).toThrow("Combined multiplier for LoRA Turbo");+});+it("migrates the old schedule to an editable step override", () => {localStorage.setItem("slopus.loras.v1", JSON.stringify([{ id: "legacy", name: "Legacy", path: "D:/legacy.safetensors", schedule: "taomate-3step" }]));expect(loadLoras().find(({ id }) => id === "legacy")).toMatchObject({ stepOverride: 3 });diff --git a/src/lib/loras.ts b/src/lib/loras.tsindex 3d8d18c..aa0f01e 100644--- a/src/lib/loras.ts+++ b/src/lib/loras.ts@@ -7,6 +7,7 @@ export interface Lora {path: string;url?: string;stepOverride?: number;+ multiplier?: number;}export interface TemplateLora { loraId: string; enabled: boolean; strength: number }export const TAOMATE_LORA: Lora = {@@ -36,6 +37,7 @@ export function loadLoras(): Lora[] {ids.add(entry.id);return [{ id: entry.id, name: entry.name, path: entry.path,...(typeof entry.url === "string" && /^https?:\/\//i.test(entry.url) ? { url: entry.url } : {}),+ ...(isLoraMultiplier(entry.multiplier) ? { multiplier: entry.multiplier } : {}),...(isLoraStepOverride(entry.stepOverride) ? { stepOverride: entry.stepOverride }: entry.stepOverride === undefined && entry.schedule === "taomate-3step" ? { stepOverride: 3 } : {}) }];}) : [];@@ -46,6 +48,9 @@ export function loadLoras(): Lora[] {} catch { return [{ ...TAOMATE_LORA }, { ...TURBO_LORA }, { ...VIGGLE_ANIMATE_LORA }]; }}export function saveLoras(loras: Lora[]): void {+ if (loras.some((lora) => lora.multiplier !== undefined && !isLoraMultiplier(lora.multiplier))) {+ throw new Error("LoRA multiplier must be a finite number within the supported range.");+ }if (loras.some((lora) => lora.stepOverride !== undefined && !isLoraStepOverride(lora.stepOverride))) {throw new Error(`Step override must be a whole number from 2 to ${MAX_GENERATION_STEPS}.`);}@@ -64,27 +69,40 @@ export function normalizeTemplateLoras(value: unknown): TemplateLora[] {if (!entry || typeof entry.loraId !== "string" || !entry.loraId || ids.has(entry.loraId)) return [];ids.add(entry.loraId);return [{ loraId: entry.loraId, enabled: entry.enabled !== false,- strength: typeof entry.strength === "number" && Number.isFinite(entry.strength) && Math.abs(entry.strength) <= 3.4028234663852886e38 ? entry.strength : 1 }];+ strength: isLoraMultiplier(entry.strength) ? entry.strength : 1 }];}) : [];}export function downloadableTemplateLoras(selection: TemplateLora[] = []): Lora[] {const active = new Set(selection.filter((entry) => entry.enabled && entry.strength !== 0).map((entry) => entry.loraId));- return loadLoras().filter((lora) => active.has(lora.id) && lora.url+ return loadLoras().filter((lora) => active.has(lora.id) && lora.multiplier !== 0 && lora.url&& (!lora.path.trim() || /^https?:\/\//i.test(lora.path)));}export function resolveTemplateLoras(selection: TemplateLora[] = []) {const library = loadLoras();- return selection.filter((entry) => entry.enabled && entry.strength !== 0).map((entry) => {+ return selection.filter((entry) => entry.enabled && entry.strength !== 0).flatMap((entry) => {const lora = library.find(({ id }) => id === entry.loraId);+ const strength = effectiveLoraStrength(entry, lora);+ if (!isLoraMultiplier(strength)) throw new Error(`Combined multiplier for LoRA ${lora?.name ?? entry.loraId} is outside the supported range.`);+ if (strength === 0) return [];if (!lora?.path.trim() || /^https?:\/\//i.test(lora.path)) {throw new Error(`Download or locate LoRA ${lora?.name ?? entry.loraId}, or disable it in the generator template.`);}- return { path: lora.path.trim(), strength: entry.strength, stepOverride: lora.stepOverride };+ return [{ path: lora.path.trim(), strength, stepOverride: lora.stepOverride }];});}+export const MAX_LORA_MULTIPLIER = 3.4028234663852886e38;++export function isLoraMultiplier(value: unknown): value is number {+ return typeof value === "number" && Number.isFinite(value) && Math.abs(value) <= MAX_LORA_MULTIPLIER;+}++export function effectiveLoraStrength(entry: TemplateLora, lora?: Lora): number {+ return entry.strength * (lora?.multiplier ?? 1);+}+export function isLoraStepOverride(value: unknown): value is number {return typeof value === "number" && Number.isInteger(value) && value >= 2 && value <= MAX_GENERATION_STEPS;}