/** * @node sdcpp-model-load * @name SD.cpp Load Model * @category sdcpp * @version 1.0.0 * @description Make sure a model is loaded, without reloading one that already is * @icon box */ const configSchema = { type: 'object', properties: { serverUrl: { type: 'string', title: 'Server URL', description: 'Base address of the sdcpp-restapi server', default: 'http://localhost:8077' }, credentialId: { type: 'string', title: 'Credential', description: 'A basic credential holding the sdcpp-restapi username and password', dynamicOptions: { source: 'credentials', filter: { type: ['basic'] } } }, modelName: { type: 'string', title: 'Model', description: 'File name of the model, relative to its type directory. Usually supplied by an SD.cpp Model node rather than typed here' }, modelType: { type: 'string', title: 'Model Type', enum: ['', 'checkpoint', 'diffusion'], default: '', description: 'checkpoint bundles U-Net, CLIP and VAE and suits SD1, SD2 and SDXL. diffusion holds only the U-Net or DiT and needs its components named separately, which is how Flux, SD3, Qwen, Wan and Z-Image load' }, vae: { type: 'string', title: 'VAE', description: 'Component file name' }, clipL: { type: 'string', title: 'CLIP-L', description: 'Component file name' }, clipG: { type: 'string', title: 'CLIP-G', description: 'Component file name' }, t5xxl: { type: 'string', title: 'T5-XXL', description: 'Component file name' }, llm: { type: 'string', title: 'LLM', description: 'Component file name, used by Z-Image, Qwen, Anima and Flux2' }, taesd: { type: 'string', title: 'TAESD', description: 'Tiny autoencoder for progress previews' }, controlnet: { type: 'string', title: 'ControlNet', description: 'Component file name' }, options: { type: 'object', title: 'Load Options', description: 'Extra load options passed through, such as flash_attn, enable_mmap, weight_type, stream_layers or max_vram' }, whenDifferent: { type: 'string', title: 'When A Different Model Is Loaded', enum: ['load', 'fail'], default: 'load', description: 'load swaps it. fail stops the run instead - for a workflow that depends on a particular model already being in place and should not quietly spend minutes swapping it' }, force: { type: 'boolean', title: 'Force Reload', default: false, description: 'Load again even when the right model is already loaded. Costs the full load time; useful after changing components or options, which this node cannot see from outside', showWhen: { field: 'whenDifferent', value: 'load' } }, timeout: { type: 'number', title: 'Timeout (ms)', description: 'Loading reads gigabytes from disk and can take minutes', default: 300000 } }, required: [] }; const inputSchema = { type: 'object', properties: { data: { type: 'any' } } }; const outputSchema = { type: 'object', properties: { modelName: { type: 'string', description: 'The model that is loaded now' }, modelType: { type: 'string' }, architecture: { type: 'string', description: 'Architecture the server detected, which decides generation defaults' }, loaded: { type: 'boolean', description: 'True when this node performed a load' }, alreadyLoaded: { type: 'boolean', description: 'True when the right model was already in place and nothing was done' }, previousModel: { type: 'string', description: 'What was loaded before, when this node swapped it' }, loadedComponents: { type: 'object' }, elapsedMs: { type: 'number' } } }; function normalizeServer(url) { const value = String(url || '').trim(); if (!value) { throw new Error('SD.cpp: a server URL is required, such as http://localhost:8077'); } return value.replace(/\/+$/, ''); } function readCredential(credentialId) { const auth = smartbotic.credentials.get(credentialId); if (!auth || auth.success !== true) { throw new Error('SD.cpp: could not read the credential: ' + ((auth && auth.error) || 'unknown error')); } const value = auth.headerValue || ''; if (value.indexOf('Basic ') !== 0) { throw new Error('SD.cpp: the credential must be a basic one, holding the sdcpp-restapi ' + 'username and password'); } const decoded = smartbotic.utils.base64Decode(value.substring(6)); const separator = decoded.indexOf(':'); if (separator < 1) { throw new Error('SD.cpp: the credential is malformed, expected a username and a password'); } return { username: decoded.substring(0, separator), password: decoded.substring(separator + 1) }; } function call(options) { const response = smartbotic.http.request(options); let body = response.data; if (typeof body === 'string' && body.length > 0) { try { body = JSON.parse(body); } catch (e) { const snippet = body.substring(0, 200).replace(/\s+/g, ' '); throw new Error('SD.cpp: ' + options.what + ' returned HTTP ' + response.status + ' with a body that is not JSON: ' + snippet); } } if (response.status < 200 || response.status >= 300) { const detail = (body && (body.message || body.error)) || ('HTTP ' + response.status); throw new Error('SD.cpp: ' + options.what + ' failed: ' + detail); } return body || {}; } function login(server, credential, timeout) { const session = call({ method: 'POST', url: server + '/auth/login', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: credential.username, password: credential.password }), timeout: timeout, what: 'signing in' }); if (!session.token) { throw new Error('SD.cpp: the server accepted the login but returned no token'); } return session.token; } // /health is unauthenticated, and it is the only way to find out what is // already loaded without asking for a token first. function readHealth(server, timeout) { return call({ method: 'GET', url: server + '/health', timeout: timeout, what: 'reading server health' }); } function putIfSet(target, key, value) { if (value === undefined || value === null || value === '') { return; } target[key] = value; } async function execute(config, input, context) { const server = normalizeServer(config.serverUrl); const timeout = config.timeout > 0 ? config.timeout : 300000; const modelName = String(config.modelName || '').trim(); if (!modelName) { throw new Error('SD.cpp: a model name is required. Connect an SD.cpp Model node, ' + 'or type the file name'); } // Ask what is loaded before loading anything. A load takes minutes and // unloads whatever was there, so doing it when the right model is already // resident is pure cost - and on a shared server it disrupts other work. const startedAt = Date.now(); const health = readHealth(server, Math.min(timeout, 15000)); const current = health.model_name || ''; const sameModel = current === modelName; if (sameModel && config.force !== true) { smartbotic.log.info('SD.cpp: ' + modelName + ' is already loaded, nothing to do'); return { modelName: current, modelType: health.model_type || '', architecture: health.model_architecture || '', loaded: false, alreadyLoaded: true, previousModel: '', loadedComponents: health.loaded_components || {}, elapsedMs: Date.now() - startedAt }; } if (!sameModel && (config.whenDifferent || 'load') === 'fail') { throw new Error('SD.cpp: this workflow expects "' + modelName + '" to be loaded, but ' + (current ? 'the server has "' + current + '"' : 'no model is loaded') + '. Set When A Different Model Is Loaded to "load" to swap it automatically'); } const credential = readCredential(config.credentialId); const token = login(server, credential, Math.min(timeout, 30000)); const body = { model_name: modelName }; putIfSet(body, 'model_type', config.modelType); putIfSet(body, 'vae', config.vae); putIfSet(body, 'clip_l', config.clipL); putIfSet(body, 'clip_g', config.clipG); putIfSet(body, 't5xxl', config.t5xxl); putIfSet(body, 'llm', config.llm); putIfSet(body, 'taesd', config.taesd); putIfSet(body, 'controlnet', config.controlnet); if (config.options && typeof config.options === 'object') { body.options = config.options; } smartbotic.log.info('SD.cpp: loading ' + modelName + (current ? ' (replacing ' + current + ')' : '')); // Loading unloads whatever was in the slot first, and the server holds a // mutex for the duration, so this blocks until the weights are resident. const loaded = call({ method: 'POST', url: server + '/models/load', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token }, body: JSON.stringify(body), timeout: timeout, what: 'loading model ' + modelName }); const after = readHealth(server, Math.min(timeout, 15000)); return { modelName: loaded.model_name || modelName, modelType: loaded.model_type || config.modelType || '', architecture: after.model_architecture || '', loaded: true, alreadyLoaded: false, previousModel: sameModel ? '' : current, loadedComponents: loaded.loaded_components || after.loaded_components || {}, elapsedMs: Date.now() - startedAt }; } module.exports = { configSchema, inputSchema, outputSchema, execute };