Files
DemonGatanjieu-Anomalous_Mo…/web/modules/model_source.js
T
DemonGatanjieuandClaude Opus 5.5 e647ad03b1 feat(models): mark where a model's information came from; fix the locale file; check syntax
Cards look as before for models matched on Civitai. Only the exceptions are
marked: a base model inferred from the file shows "≈ <base>" with a dashed
outline, and a .safetensors model not scanned yet says so; both explain
themselves in a tooltip (model_source.js). The detail header's line adds
the source and the fields the user edited; the green title that meant
"renamed" gives way to it (ui_detail.js 755 -> 752 lines).

Fix: the previous commit's locales.js had raw line breaks inside the
"fetch again" confirmation strings, so the plugin did not load. Neither
`node --check` (it passes such .js files on Node 24) nor the structure
check noticed. check_structure.mjs now compiles every web file as the ES
module the browser loads and fails on a parse error; AGENTS.md says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 01:56:40 +08:00

63 lines
3.1 KiB
JavaScript

/**
* Where a model's information came from, shown on cards and in the detail header.
* Matched on Civitai is the normal case and looks as before; only the exceptions are
* marked: base model inferred from the file (≈), and not scanned yet. The detail header
* also names the fields the user set (metadata.user_fields, from <model>.anomalous.json).
*/
import { translate as t } from './locales.js';
import { escapeHtml } from './safe_dom.js';
const FIELD_KEYS = { custom_name: 'modelSourceFieldName', custom_notes: 'modelSourceFieldNotes', source_url: 'modelSourceFieldLink' };
function colourClass(baseModel) {
const lower = baseModel.toLowerCase();
if (lower.includes('flux')) return 'badge-flux';
if (lower.includes('pony') || lower.includes('illustrious') || lower.includes('anime')) return 'badge-rose';
if (lower.includes('sdxl') || lower.includes('xl')) return 'badge-gold';
return 'badge-amber';
}
/** Only .safetensors files are scanned, so only they can be "not scanned yet". */
function scannable(model) {
return /\.safetensors$/i.test(String(model?.filename || ''));
}
/** The card's top-left badge, or null. */
export function renderSourceBadge(model) {
const meta = model?.metadata || {};
const baseModel = String(meta.baseModel || '');
const badge = document.createElement('div');
badge.className = 'anomalous-card-badge';
if (meta.info_source === 'local') {
badge.classList.add('is-inferred', ...(baseModel ? [colourClass(baseModel)] : []));
badge.textContent = `≈ ${baseModel || t('modelSourceLocalShort')}`;
badge.title = t('modelSourceLocalHint');
} else if (!meta.info_source && scannable(model)) {
badge.classList.add('is-unscanned');
badge.textContent = t('modelSourceNotScanned');
badge.title = t('modelSourceNotScannedHint');
} else if (baseModel) {
badge.classList.add(colourClass(baseModel));
badge.textContent = baseModel;
} else {
return null;
}
return badge;
}
/** The detail header's line: size, base model, where the information came from, what the user set. */
export function renderSourceLine(span, model, meta) {
const parts = [`<strong>Size:</strong> ${escapeHtml(model.size_mb)} MB`];
if (meta.baseModel) parts.push(`<strong>Base:</strong> ${meta.info_source === 'local' ? '≈ ' : ''}${escapeHtml(meta.baseModel)}`);
const source = meta.info_source === 'civitai' ? t('modelSourceCivitai')
: meta.info_source === 'local' ? t('modelSourceLocal')
: scannable(model) ? t('modelSourceNotScanned') : '';
if (source) parts.push(`<strong>${escapeHtml(t('modelSourceLabel'))}:</strong> ${escapeHtml(source)}`);
const edited = (meta.user_fields || []).map(field => t(FIELD_KEYS[field] || field));
if (edited.length) parts.push(escapeHtml(t('modelSourceEdited', { fields: edited.join(t('modelSourceListSep')) })));
span.innerHTML = parts.join('<span class="anomalous-source-sep"> · </span>');
span.title = meta.info_source === 'local' ? t('modelSourceLocalHint')
: !meta.info_source && scannable(model) ? t('modelSourceNotScannedHint') : '';
}