* fix(lora): detect LoraManager in ComfyUI's extra custom_nodes folders Auto-detection looked only in ComfyUI's main custom_nodes folder (#144). It now also searches every custom_nodes folder ComfyUI registers through folder_paths, main folder first, skipping folders that cannot be read. * fix(lora): stage imports before replacing the collection (#221) The LoRA import deleted the whole lora-manager category first and then saved each record in its own transaction, so a discovery, download or database failure left the previous collection gone or partial. The import now runs in three stages: discover and fingerprint the metadata files, stage every record (CivitAI downloads go into the image cache first, counted), then replace the category in one BEGIN IMMEDIATE transaction (replace_category_prompts). Any failure leaves the previous prompts, tags and image links exactly as they were. Prompts a saved pair uses block the replacement; an empty replacement of a non-empty collection needs a reviewed import. POST /prompt_manager/lora/import/preview reviews an import without writing or requesting anything; POST /lora/import/apply {token} runs it as the Activity job "LoRA reference import" (records admission, cancellable while it downloads, the safety backup policy before the replace, refused with plan-changed when the files changed since the review). The legacy POST /lora/scan runs the same stages with its SSE event shape. * feat(lora): the LoRA save takes the settings revision POST /prompt_manager/lora/enable accepts the optional expected_revision that GET /settings returns, so the System Save row refuses to write over a config.json changed elsewhere (settings-changed, nothing written). The legacy page sends none and still merges into what is on disk. * feat(system): configure ComfyUI and LoRA integrations natively System › Integrations replaces its hand-off with the concept's two surfaces. ComfyUI: only the send destination and renderer the bridge supports can be chosen; the other options and Return to canvas are disabled with the reason, and Host compatibility shows the renderer the open ComfyUI windows report (each bridge destination now carries renderer: classic or nodes2) (D106). LoRA & resources: detection, the enabled setting and the loaded trigger words read as separate facts, the read-only LoraManager path row follows the toggle (D106), and the CivitAI key is never filled in: typing drafts a new key, blank keeps the saved one and Clear saved key drafts its removal (D114). They save through the one Save row (/lora/enable with the settings revision). LoRA reference data → Review import shows the server's read-only review (replacement, what saved pairs block, downloads, backup box) and Start runs the LoRA reference import Activity job (D107), with its own Activity descriptor and Review again into Integrations when the files or pairs overtook it. * test(system): compare Integrations with the concept system-integrations-concept.spec compares System › Integrations (both themes, 1600 and 390 wide) with the concept, mapping only D106 and D114, and the LoRA import review with the concept's record-changing review, mapping D107; each comparison catches a removed row, a renamed label or option, a re-enabled unsupported option and a swapped token. On the real ComfyUI host, each bridge destination reports the renderer its window draws nodes with, before and after switching it, and System › Integrations shows it. * test(coverage): record the Integrations frontend floors coverage:update after the full frontend run: system-integrations.mjs at its measured floor, and the raised maintenance review, job view and maintenance floors. * test(system): wait for the renderer line before clicking Integrations rows CI Firefox (390 wide) clicked the LoraManager toggle while the renderer check's line above it changed text and moved the rows, so the click did not reach the switch. The specs now wait for that line to settle (a visible state) before interacting. * fix(lora): recheck the files before the replace and stop an abandoned scan Review fixes for the staged LoRA import (#221): - The metadata files are listed and fingerprinted again right before the replace transaction, so a file changed, removed or added while the downloads ran aborts the import with nothing deleted (System job: plan-changed, Review again; legacy scan: an error frame in its own words). The tests now change real files behind a held download. - A legacy scan whose page closes stops staging before its next LoRA and keeps its records hold until staging has stopped; CivitAI images are written to a temp file beside the cache entry and moved in place, so a half-written image is never seen as cached. - Start refuses with plan-changed when the integration was turned off or moved to another install since the review. - A failed job keeps the skipped count and the names of the LoRAs that could not be prepared. - The replace binds the category instead of one parameter per prompt (SQLite's variable limit), /lora/status says whether a detected install can be used, and the legacy scan logs expected refusals as warnings. * fix(system): partial saves, key focus and readable selects in Integrations A save that writes the LoRA part but not the settings part now keeps the saved part, adopts the new revision and highlights only what failed, so the next save needs no conflict. The CivitAI key status is a persistent live region, Clear saved key returns focus to the key field, and the key draft is dropped when the section is left. The send-destination and renderer selects keep their width on narrow screens. A detected LoraManager outside custom_nodes says it cannot be enabled. * fix(lora): keep the save outcome, honour the umask and sweep stale downloads Leaving Integrations drops the CivitAI key draft only when a key was typed, so the last Save's outcome survives and Sources still marks a refused source. The key status line is rewritten only when it changes. Cached CivitAI images are opened like a plain save, so they keep the umask's mode, and each import removes .pm-*.part files older than an hour from the image cache. A legacy scan stopped because its page left ends cancelled in Activity, and staging is awaited to the end even if the handler is cancelled again. * fix(lora): the import review takes a lease beside a restore The LoRA import review reads the library, so like the maintenance and restore previews it now runs under a request lease: it is refused with 409 busy while a restore runs, and a restore waits for it before it switches the file. Tests pin that a restore is refused while an import runs and that the review, the reviewed import and the admin page's scan are refused while a restore runs.
447 lines
24 KiB
JavaScript
447 lines
24 KiB
JavaScript
/**
|
||
* System › Integrations (#249 System S10 Task 27): the concept's `renderSystem` "integrations" body (app.js 682–684).
|
||
*
|
||
* - "ComfyUI": Default send destination, Host compatibility and Return to canvas after send. D106 (Q8): the send
|
||
* bridge always asks for the window and node, and the nodes always follow ComfyUI's own renderer, so only "Ask each
|
||
* time" and "Follow host renderer" can be chosen; the other options and the return toggle are disabled, with the
|
||
* reason under the row, and nothing is stored for them. Host compatibility also says which renderer the open
|
||
* ComfyUI windows use, as the send bridge reports it (`renderer` on each Prompt Manager node it lists).
|
||
* - "LoRA & resources": "LoraManager integration" (enabled, a host draft) says where LoraManager was detected;
|
||
* D106 adds the read-only "LoraManager path" row, as on the legacy page. "Automatically include trigger words"
|
||
* (a host draft) says whether the trigger words are loaded. The three are separate facts (`/lora/status`).
|
||
* D114: the CivitAI key field is never filled in; typing drafts a new key, a blank field keeps the saved one, and
|
||
* "Clear saved key" (shown while a key is saved) drafts its removal; the line under it says "A key is saved." or "No key
|
||
* saved." All of them are
|
||
* saved by the one Save row (`/lora/enable` with the settings revision, system.mjs).
|
||
* - "LoRA reference data" → "Review import" (D107; the concept's button only simulated a job): the server
|
||
* reviews the import (`POST /lora/import/preview`, read-only) and the shared review dialog shows what it replaces,
|
||
* what saved pairs block, what it downloads and the backup box; Start runs the "LoRA reference import" Activity job.
|
||
*/
|
||
import { createButton } from '../shared/toolkit/core.js';
|
||
import { createBridgeClient } from '../shared/toolkit/prompt-bridge.js';
|
||
import { settingRow, operationRow } from './system-controls.mjs';
|
||
import { HOST_UNAVAILABLE } from './system-appearance.mjs';
|
||
import { closeWorkspaceDialog, openContentDialog, openReviewDialog } from './workspace-dialogs.mjs';
|
||
import { BACKUP_COPY, applyPlan, backupText, planNote, protectedText, reviewRefusal } from './maintenance-review.mjs';
|
||
import { downloadDatabaseBackup } from './activity-scan.mjs';
|
||
import { showWorkspaceToast } from './toast.mjs';
|
||
import { navigateTo } from './state.mjs';
|
||
import { noun } from './plural.mjs';
|
||
|
||
export const LORA_STATUS_URL = '/prompt_manager/lora/status';
|
||
export const LORA_ENABLE_URL = '/prompt_manager/lora/enable';
|
||
export const IMPORT_PREVIEW_URL = '/prompt_manager/lora/import/preview';
|
||
export const IMPORT_APPLY_URL = '/prompt_manager/lora/import/apply';
|
||
export const IMPORT_TITLE = 'Review LoRA import';
|
||
export const IMPORT_START = 'Start import';
|
||
/** The host draft keys this section saves through `/lora/enable`; every other host key goes to `/settings`. */
|
||
export const LORA_KEYS = Object.freeze(['lora_enabled', 'lora_trigger_words_enabled', 'civitai_api_key', 'clear_civitai_api_key']);
|
||
|
||
/** D106: why the options the bridge cannot honour are disabled. */
|
||
export const REASONS = Object.freeze({
|
||
send: 'Sending always asks for the ComfyUI window and node, so “Ask each time” is the only option.',
|
||
renderer: 'Prompt Manager nodes always follow ComfyUI’s own renderer setting.',
|
||
returnCanvas: 'Not available: sending applies the text in ComfyUI and keeps this page open.',
|
||
});
|
||
export const RENDERER_COPY = Object.freeze({
|
||
looking: 'Looking for open ComfyUI windows…',
|
||
none: 'No ComfyUI window is open in this browser, so its renderer is not known.',
|
||
noNodes: 'ComfyUI is open; add a Prompt Manager node to see which renderer it uses.',
|
||
unavailable: 'The renderer could not be checked in this browser.',
|
||
});
|
||
const RENDERER_NAMES = Object.freeze({ classic: 'Classic', nodes2: 'Nodes 2.0' });
|
||
export const KEY_COPY = Object.freeze({ saved: 'A key is saved.', none: 'No key saved.',
|
||
clearing: 'The saved key is removed when you save.', replacing: 'The new key replaces the saved one when you save.' });
|
||
export const NOTHING_TO_IMPORT = 'There is nothing to import; nothing was changed.';
|
||
export const BLOCKED_NOTE = 'Delete or change the saved pairs that use these prompts, then review the import again. Nothing was changed.';
|
||
export const CACHE_NOTE = 'Example images downloaded before a failure stay in the image cache.';
|
||
|
||
const number = value => value.toLocaleString('en-US');
|
||
|
||
function el(tag, text, className) {
|
||
const node = document.createElement(tag);
|
||
if (text !== undefined) node.textContent = text;
|
||
if (className) node.className = className;
|
||
return node;
|
||
}
|
||
|
||
/** The host values a `/lora/status` answer gives: drafted ones (enabled, trigger words, clear) and read-only facts. */
|
||
export function loraValues(status) {
|
||
return {
|
||
lora_enabled: status?.enabled === true,
|
||
lora_trigger_words_enabled: status?.trigger_words_enabled === true,
|
||
clear_civitai_api_key: false,
|
||
lora_path: typeof status?.path === 'string' ? status.path : '',
|
||
lora_detected: status?.detected === true,
|
||
lora_detected_path: typeof status?.detected_path === 'string' ? status.detected_path : '',
|
||
lora_detected_usable: status?.detected_usable !== false,
|
||
lora_has_key: status?.has_civitai_api_key === true,
|
||
lora_cache_loaded: status?.trigger_cache_loaded === true,
|
||
};
|
||
}
|
||
|
||
/** Host draft changes split by route: `{lora, settings}`, each null when it has none. */
|
||
export function splitHostChanges(changes) {
|
||
const lora = {};
|
||
const settings = {};
|
||
for (const [key, value] of Object.entries(changes ?? {})) (LORA_KEYS.includes(key) ? lora : settings)[key] = value;
|
||
return { lora: Object.keys(lora).length ? lora : null, settings: Object.keys(settings).length ? settings : null };
|
||
}
|
||
|
||
/**
|
||
* The `/lora/enable` body for the drafted LoRA `changes` over the `saved` host values: the whole LoRA state (the route
|
||
* saves all of it), the saved path, and the key only when one was typed (a blank or absent key keeps the saved one).
|
||
*/
|
||
export function loraPayload(changes, saved) {
|
||
const value = key => (Object.hasOwn(changes, key) ? changes[key] : saved?.[key]);
|
||
const body = { enabled: value('lora_enabled') === true, trigger_words_enabled: value('lora_trigger_words_enabled') === true,
|
||
path: saved?.lora_path || '' };
|
||
if (changes.clear_civitai_api_key === true) body.clear_civitai_api_key = true;
|
||
else if (typeof changes.civitai_api_key === 'string' && changes.civitai_api_key.trim()) body.civitai_api_key = changes.civitai_api_key.trim();
|
||
return body;
|
||
}
|
||
|
||
/**
|
||
* Write host `changes` in their two parts (S10 review I3): the LoRA keys through `postLora` (`/lora/enable`), then the
|
||
* other keys through `postSettings` (`/settings`), each with the revision the previous write left. Resolves with the
|
||
* last answer when everything saved. When only one part saved, rejects with the other part's error carrying
|
||
* `partial: {saved: [keys], revision}`, so the store keeps what saved and adopts the new revision; a
|
||
* conflict on the first write (another page changed config.json) stops before the second.
|
||
*/
|
||
export async function writeHostInParts({ changes, saved, expected_revision, postLora, postSettings }) {
|
||
const { lora, settings } = splitHostChanges(changes);
|
||
let revision = expected_revision;
|
||
let result = null;
|
||
const written = [];
|
||
let refused = null;
|
||
if (lora) {
|
||
try {
|
||
result = await postLora({ ...loraPayload(lora, saved), expected_revision: revision });
|
||
if (typeof result?.revision === 'string') revision = result.revision;
|
||
written.push(...Object.keys(lora));
|
||
} catch (error) {
|
||
if (!settings || error?.code === 'settings-changed') throw error;
|
||
refused = error;
|
||
}
|
||
}
|
||
if (settings) {
|
||
try {
|
||
result = await postSettings({ ...settings, expected_revision: revision });
|
||
if (typeof result?.revision === 'string') revision = result.revision;
|
||
written.push(...Object.keys(settings));
|
||
} catch (error) {
|
||
if (!written.length) throw error;
|
||
throw Object.assign(error, { partial: { saved: written, revision } });
|
||
}
|
||
}
|
||
if (refused) {
|
||
throw Object.assign(refused, { partial: { saved: written, revision } });
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/** The renderer line for discovered bridge hosts (D106). */
|
||
export function rendererText(hosts) {
|
||
if (!Array.isArray(hosts) || hosts.length === 0) return RENDERER_COPY.none;
|
||
const seen = new Set();
|
||
for (const host of hosts) for (const target of host?.targets ?? []) if (RENDERER_NAMES[target?.renderer]) seen.add(target.renderer);
|
||
if (!seen.size) return RENDERER_COPY.noNodes;
|
||
const names = ['classic', 'nodes2'].filter(name => seen.has(name)).map(name => RENDERER_NAMES[name]);
|
||
return `Detected: ${names.join(' and ')} (${number(hosts.length)} ComfyUI ${noun(hosts.length, 'window')}).`;
|
||
}
|
||
|
||
/** The LoraManager row's line: where it was detected, or that it was not. */
|
||
export function detectionText(values) {
|
||
if (values.lora_detected === undefined) return null;
|
||
if (!values.lora_detected) return 'Not detected in ComfyUI’s custom_nodes folders.';
|
||
if (values.lora_detected_usable === false) {
|
||
// A linked install outside custom_nodes is found but cannot be enabled (S10 review N5).
|
||
return `Found at ${values.lora_detected_path || 'a linked folder'}, but outside ComfyUI’s custom_nodes folders, so it can’t be enabled.`;
|
||
}
|
||
return values.lora_detected_path ? `Detected: ${values.lora_detected_path}.` : 'Detected.';
|
||
}
|
||
|
||
/** The trigger-words row's line (the cache, separate from the setting). */
|
||
export function cacheText(values) {
|
||
if (values.lora_cache_loaded === undefined) return null;
|
||
return values.lora_cache_loaded ? 'Trigger words are loaded.' : 'Trigger words are not loaded.';
|
||
}
|
||
|
||
/** D114: the key line for the saved state and the drafts. */
|
||
export function keyText(values, changes) {
|
||
if (changes?.clear_civitai_api_key === true) return KEY_COPY.clearing;
|
||
if (typeof changes?.civitai_api_key === 'string' && changes.civitai_api_key.trim()) return KEY_COPY.replacing;
|
||
if (values.lora_has_key === undefined) return null;
|
||
return values.lora_has_key ? KEY_COPY.saved : KEY_COPY.none;
|
||
}
|
||
|
||
/** D107: the import review's rows from the server's plan. */
|
||
export function importGrid(plan) {
|
||
const downloads = Number.isSafeInteger(plan.planned_downloads) ? plan.planned_downloads : 0;
|
||
const network = downloads
|
||
? `Downloads up to ${number(downloads)} example ${noun(downloads, 'image')} from CivitAI${plan.network?.api_key ? ' with your saved key' : ''}`
|
||
: 'No downloads';
|
||
const found = Number.isSafeInteger(plan.lora_files) ? plan.lora_files : 0;
|
||
const readable = Number.isSafeInteger(plan.with_metadata) ? plan.with_metadata : 0;
|
||
return [
|
||
['Scope', plan.scope?.label || `${number(readable)} ${noun(readable, 'LoRA')}`],
|
||
['Found', `${number(found)} metadata ${noun(found, 'file')} · ${number(readable)} readable`],
|
||
['Protected', protectedText(plan.protected)],
|
||
['Network', network],
|
||
['Backup', backupText(plan.backup)],
|
||
['Media on disk', plan.media?.label || 'No files are removed'],
|
||
];
|
||
}
|
||
|
||
function namesList(plan) {
|
||
const names = (Array.isArray(plan.details) ? plan.details : []).map(row => row?.label).filter(label => typeof label === 'string');
|
||
if (!names.length) return [];
|
||
const list = el('details');
|
||
const count = Number.isSafeInteger(plan.with_metadata) ? plan.with_metadata : names.length;
|
||
list.append(el('summary', names.length < count ? `LoRAs found (${number(names.length)} of ${number(count)})` : `LoRAs found (${number(names.length)})`));
|
||
const body = el('pre', names.join('\n'));
|
||
body.tabIndex = 0;
|
||
list.append(body);
|
||
return [list];
|
||
}
|
||
|
||
function closeOnly({ scope, trigger, plan, note }) {
|
||
const grid = el('dl', undefined, 'meta-grid');
|
||
for (const [term, value] of importGrid(plan)) {
|
||
const entry = el('div');
|
||
entry.append(el('dt', term), el('dd', value));
|
||
grid.append(entry);
|
||
}
|
||
const close = createButton('Close');
|
||
close.addEventListener('click', () => closeWorkspaceDialog());
|
||
const actions = el('div', undefined, 'dialog-actions');
|
||
actions.append(close);
|
||
openContentDialog({ scope, trigger, title: IMPORT_TITLE, nodes: [el('h3', 'LoRA reference data'), grid, ...namesList(plan),
|
||
el('p', note, 'small'), actions] });
|
||
}
|
||
|
||
/** Show the reviewed import; Start applies exactly this token (D107). */
|
||
export function showImportPlan(plan, { api, store, scope, trigger = null, reviewAgain, notify = showWorkspaceToast,
|
||
navigate = navigateTo, backup = () => downloadDatabaseBackup() }) {
|
||
if (!plan.token) {
|
||
closeOnly({ scope, trigger, plan, note: plan.blocked_by_pairs ? BLOCKED_NOTE : NOTHING_TO_IMPORT });
|
||
return;
|
||
}
|
||
if (!plan.with_metadata && !plan.current_imports) {
|
||
closeOnly({ scope, trigger, plan, note: NOTHING_TO_IMPORT });
|
||
return;
|
||
}
|
||
const removesAll = !plan.with_metadata;
|
||
const sentence = [planNote(plan), removesAll ? 'No LoRA metadata was found: this removes every imported LoRA prompt.' : '', CACHE_NOTE]
|
||
.filter(Boolean).join(' ');
|
||
openReviewDialog({ scope, trigger, title: IMPORT_TITLE, heading: 'LoRA reference data', grid: importGrid(plan),
|
||
options: namesList(plan), sentence, backup: { copy: BACKUP_COPY, onBackup: backup }, cancelLabel: 'Cancel',
|
||
primary: { label: IMPORT_START, danger: removesAll },
|
||
onStart: () => applyPlan({ api, store, scope, token: plan.token, reviewAgain, notify, navigate, url: IMPORT_APPLY_URL }) });
|
||
}
|
||
|
||
/** Review the import (read-only) and show its plan; the pressed button reads "Reviewing…" until it answered. */
|
||
export async function reviewImport({ api, store, scope, trigger = null, notify = showWorkspaceToast, navigate = navigateTo }) {
|
||
if (trigger?.getAttribute('aria-disabled') === 'true') return;
|
||
const again = () => reviewImport({ api, store, scope, trigger, notify, navigate });
|
||
const label = trigger?.textContent;
|
||
trigger?.setAttribute('aria-disabled', 'true');
|
||
if (trigger) trigger.textContent = 'Reviewing…';
|
||
try {
|
||
let plan;
|
||
try {
|
||
plan = await api.post(IMPORT_PREVIEW_URL, {}, { signal: scope.signal });
|
||
} catch (error) {
|
||
if (!scope.signal.aborted) notify(reviewRefusal(error));
|
||
return;
|
||
}
|
||
if (!scope.signal.aborted) showImportPlan(plan, { api, store, scope, trigger, notify, navigate, reviewAgain: again });
|
||
} finally {
|
||
trigger?.removeAttribute('aria-disabled');
|
||
if (trigger) trigger.textContent = label;
|
||
}
|
||
}
|
||
|
||
/** The row's control (`data-setting`), wherever the Toolkit switch nests it. */
|
||
const controlOf = (row, id) => row.querySelector(`[data-setting="${id}"]`);
|
||
|
||
/** A `p.small` status line under a row's description, created on first use. */
|
||
function noteOf(row) {
|
||
let note = row.children[0].querySelector(':scope > p.small');
|
||
if (!note) {
|
||
note = el('p', undefined, 'small');
|
||
row.children[0].append(note);
|
||
}
|
||
return note;
|
||
}
|
||
|
||
function setNote(row, text) {
|
||
if (text) noteOf(row).textContent = text;
|
||
else row.children[0].querySelector(':scope > p.small')?.remove();
|
||
}
|
||
|
||
/** A select row whose options after the first are disabled (D106). */
|
||
function onlyFirst(spec) {
|
||
const row = settingRow({ ...spec, control: { type: 'select', options: spec.options } });
|
||
for (const option of controlOf(row, spec.id).options) option.disabled = option.index > 0;
|
||
return row;
|
||
}
|
||
|
||
/** The "ComfyUI" surface (D106) and its renderer detection over the send bridge. */
|
||
function comfySurface({ scope, clientFactory }) {
|
||
const surface = el('section', undefined, 'surface');
|
||
surface.append(el('h2', 'ComfyUI'), el('p', 'One send contract across cards, prompt history and every metadata view.'));
|
||
const send = onlyFirst({ id: 'send-destination', label: 'Default send destination',
|
||
description: 'A destination picker previews both roles before applying.',
|
||
options: ['Ask each time', 'Selected Prompt Manager node', 'Create a Prompt Manager (+/-) node'] });
|
||
const renderer = onlyFirst({ id: 'renderer', label: 'Host compatibility',
|
||
description: 'Use the shared compatibility layer for Classic and Nodes 2.0.',
|
||
options: ['Follow host renderer', 'Classic', 'Nodes 2.0'] });
|
||
const returnRow = settingRow({ id: 'return-canvas', label: 'Return to canvas after send',
|
||
description: 'Apply text without queuing generation.', control: { type: 'toggle', checked: false } });
|
||
controlOf(returnRow, 'return-canvas').disabled = true;
|
||
setNote(send, REASONS.send);
|
||
setNote(returnRow, REASONS.returnCanvas);
|
||
const status = el('p', RENDERER_COPY.looking, 'small');
|
||
status.setAttribute('role', 'status');
|
||
status.id = 'renderer-status';
|
||
renderer.children[0].append(el('p', REASONS.renderer, 'small'), status);
|
||
surface.append(send, renderer, returnRow);
|
||
|
||
let client = null;
|
||
// Overlapping checks (two quick visibility changes) paint only the newest answer (S10 review N2).
|
||
let latest = 0;
|
||
const detect = async () => {
|
||
const check = ++latest;
|
||
let text;
|
||
try {
|
||
client ??= clientFactory();
|
||
text = rendererText(await client.discover({ waitMs: 300 }));
|
||
} catch {
|
||
text = RENDERER_COPY.unavailable;
|
||
}
|
||
if (check === latest && !scope.signal.aborted) status.textContent = text;
|
||
};
|
||
scope.cleanup(() => client?.dispose());
|
||
// Another tab may open or switch ComfyUI meanwhile: look again when this page is shown again.
|
||
scope.listen(document, 'visibilitychange', () => { if (document.visibilityState === 'visible') detect(); });
|
||
detect();
|
||
return surface;
|
||
}
|
||
|
||
function bridgeFactory() {
|
||
const url = new URL(window.ApiClient.resolveUrl('/prompt_manager/'), window.location.href);
|
||
return () => createBridgeClient({ namespace: url.pathname.slice(0, -'prompt_manager/'.length), origin: window.location.origin });
|
||
}
|
||
|
||
/**
|
||
* Mount Integrations in `container` (System's `.settings-layout`); returns the section's two surfaces. `drafts` is the
|
||
* System draft store (its host domain carries the LoRA values, system.mjs), `activity` the page's Activity store.
|
||
*/
|
||
export function mountIntegrations(container, { api, activity, scope, drafts, clientFactory = bridgeFactory(),
|
||
notify = showWorkspaceToast, navigate = navigateTo }) {
|
||
const comfy = comfySurface({ scope, clientFactory });
|
||
const surface = el('section', undefined, 'surface');
|
||
surface.append(el('h2', 'LoRA & resources'), el('p', 'Preserves LoraManager detection, data import and positive-only trigger injection.'));
|
||
const enable = settingRow({ id: 'lora-enable', label: 'LoraManager integration', description: 'Checking for LoraManager…',
|
||
control: { type: 'toggle', checked: false }, scope, onChange: value => drafts.set('host', 'lora_enabled', value) });
|
||
const pathRow = settingRow({ id: 'lora-path', label: 'LoraManager path',
|
||
description: 'Found automatically in ComfyUI’s custom_nodes folders; read-only, as on the existing settings page.',
|
||
control: { type: 'text' } });
|
||
const pathInput = controlOf(pathRow, 'lora-path');
|
||
pathInput.readOnly = true;
|
||
pathInput.placeholder = 'Not detected';
|
||
// Wide enough for the usual install path within the concept's 230px control limit.
|
||
pathInput.size = 34;
|
||
const triggers = settingRow({ id: 'lora-triggers', label: 'Automatically include trigger words',
|
||
description: 'Trigger words apply only to positive text; connected node inputs remain authoritative.',
|
||
control: { type: 'toggle', checked: false }, scope, onChange: value => drafts.set('host', 'lora_trigger_words_enabled', value) });
|
||
const key = settingRow({ id: 'civitai-key', label: 'CivitAI API key', description: 'Credentials stay in the server configuration.',
|
||
control: { type: 'password' } });
|
||
const keyInput = controlOf(key, 'civitai-key');
|
||
// Never a login the browser offers to save or fill (S10 review N3).
|
||
keyInput.autocomplete = 'new-password';
|
||
keyInput.spellcheck = false;
|
||
keyInput.placeholder = 'Optional';
|
||
// D114: the saved-key line is announced (a polite live region) and describes the field (S10 review M2).
|
||
const keyNote = el('p', '', 'small');
|
||
keyNote.id = 'civitai-key-status';
|
||
keyNote.setAttribute('role', 'status');
|
||
key.children[0].append(keyNote);
|
||
keyInput.setAttribute('aria-describedby', keyNote.id);
|
||
const clear = createButton('Clear saved key');
|
||
clear.dataset.setting = 'clear_civitai_api_key';
|
||
clear.dataset.key = 'clear_civitai_api_key';
|
||
const keyActions = el('div', undefined, 'row-actions');
|
||
keyActions.append(clear);
|
||
key.children[0].append(keyActions);
|
||
scope.listen(keyInput, 'input', () => {
|
||
// D114: a blank field keeps the saved key: it is no draft at all.
|
||
drafts.set('host', 'civitai_api_key', keyInput.value.trim() ? keyInput.value : undefined);
|
||
if (keyInput.value.trim()) drafts.set('host', 'clear_civitai_api_key', false);
|
||
});
|
||
scope.listen(clear, 'click', () => {
|
||
keyInput.value = '';
|
||
// The button leaves with the saved key: keep the keyboard on the field it belongs to (S10 review M2).
|
||
keyInput.focus();
|
||
drafts.set('host', 'civitai_api_key', undefined);
|
||
drafts.set('host', 'clear_civitai_api_key', true);
|
||
});
|
||
const importRow = operationRow({ scope, title: 'LoRA reference data', description: 'Review and import resource metadata.',
|
||
label: 'Review import', onClick: trigger => reviewImport({ api, store: activity, scope, trigger, notify, navigate }) });
|
||
surface.append(enable, pathRow, triggers, key, importRow);
|
||
|
||
const controls = [[enable, 'lora-enable', 'lora_enabled'], [triggers, 'lora-triggers', 'lora_trigger_words_enabled']]
|
||
.map(([row, id, name]) => ({ row, name, input: controlOf(row, id) }));
|
||
for (const control of controls) control.input.dataset.key = control.name;
|
||
keyInput.dataset.key = 'civitai_api_key';
|
||
|
||
// The saved LoRA state as last painted: a save (or another tab's change read back) that changes it reads
|
||
// `/lora/status` again, so detection, the key line and the trigger cache follow what the server now holds.
|
||
let shown;
|
||
const repaint = () => {
|
||
const values = drafts.saved('host');
|
||
const changes = drafts.changes('host');
|
||
const unread = values.lora_enabled === undefined;
|
||
const failed = drafts.hostState?.() === 'failed' || (drafts.hostState?.() === 'loaded' && unread);
|
||
for (const { input, name } of controls) {
|
||
const value = drafts.get('host', name);
|
||
input.disabled = value === undefined;
|
||
if (value !== undefined) input.checked = value === true;
|
||
}
|
||
enable.children[0].children[1].textContent = failed && unread ? HOST_UNAVAILABLE
|
||
: detectionText(values) ?? 'Checking for LoraManager…';
|
||
pathInput.value = values.lora_path || values.lora_detected_path || '';
|
||
setNote(triggers, cacheText(values));
|
||
keyInput.disabled = unread;
|
||
// A live region: rewritten only when it says something new, so it is not re-announced on every edit.
|
||
const note = keyText(values, changes) ?? '';
|
||
if (keyNote.textContent !== note) keyNote.textContent = note;
|
||
// D114: "Clear saved key" is offered only while a saved key would stay.
|
||
if (values.lora_has_key === true && changes.clear_civitai_api_key !== true) key.children[0].append(keyActions);
|
||
else {
|
||
if (keyActions.contains(document.activeElement)) keyInput.focus();
|
||
keyActions.remove();
|
||
}
|
||
const state = JSON.stringify([values.lora_enabled, values.lora_trigger_words_enabled, values.civitai_api_key,
|
||
values.clear_civitai_api_key]);
|
||
const saved = values.civitai_api_key !== undefined || values.clear_civitai_api_key === true;
|
||
if (!unread && shown !== undefined && (state !== shown || saved)) {
|
||
if (saved) keyInput.value = '';
|
||
shown = undefined;
|
||
drafts.reloadHost();
|
||
return;
|
||
}
|
||
if (!unread) shown = state;
|
||
};
|
||
scope.cleanup(drafts.subscribe(repaint));
|
||
// A typed key is held only while its field shows it: leaving the section drops it, since the field comes back
|
||
// empty and a hidden key draft could not be seen or discarded (S10 review N4). Only a typed key: any set clears
|
||
// the last Save's outcome, which Sources still needs (S10 re-review R1).
|
||
scope.cleanup(() => {
|
||
if (drafts.changes('host').civitai_api_key !== undefined) drafts.set('host', 'civitai_api_key', undefined);
|
||
});
|
||
repaint();
|
||
return [comfy, surface];
|
||
}
|