/** * @node sdcpp-upscale * @name SD.cpp Upscale * @category sdcpp * @version 1.0.0 * @description Enlarge an image with the loaded upscaler * @icon maximize-2 */ // The credential this node wants, named so it can be found. It is stored as a // plain basic credential - that is what decides how it is encrypted - and this // only says which basic credential is the SD.cpp one. Anything that accepts a // basic credential still accepts this, and this node still accepts a plain // basic credential, because the shape is identical. const credentialTypes = [ { id: 'sdcpp', label: 'SD.cpp Server', baseType: 'basic', description: 'The username and password you sign in to sdcpp-restapi with. The node exchanges them for a token before every call', usernameLabel: 'Username', passwordLabel: 'Password' } ]; const configSchema = { type: 'object', // Generated from the server's own reference at /options/generation - see // scripts/gen-sdcpp-generation-options.py. Every field the endpoint accepts // has a setting here, grouped the way the server groups them. uiGroups: [ { "title": "Server", "fields": [ "serverUrl", "credentialId" ] }, { "title": "/upscale Endpoint", "fields": [ "imageBase64", "jobId", "imageIndex", "upscaleFactor", "tileSize", "repeats" ] }, { "title": "Job", "fields": [ "title", "extraOptions", "timeout" ] } ], prefill: { "label": "Take the architecture defaults", "description": "Fill these in from the preset for whichever model the server has loaded - the same values it would use if these were left empty", "node": "sdcpp-architecture", "needs": [ "serverUrl", "credentialId" ], "map": {} }, 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: ['sdcpp', 'basic'] } } }, imageBase64: { title: "Source Image (base64)", description: "Base64-encoded source image for upscaling. Recommended: Required. Leave empty for the architecture default.", type: "string" }, imageIndex: { title: "Source Image Index", description: "Which output of the source job to upscale. Used with `job_id` when the source job produced multiple images (e.g. batch_count > 1). Ignored when `job_id` is absent. 0-based: 0 = first output. Recommended: Leave at 0 unless the source job had batch_count > 1 and you want a specific image. Leave empty for the architecture default.", type: "number" }, jobId: { title: "Source Job ID", description: "Convenience: use the output of an existing completed job as the source for upscaling, instead of providing `image_base64`. handle_upscale loads `outputs[image_index]` from disk, base64-encodes it, and substitutes `image_base64` before the worker runs — so to the worker the two paths are indistinguishable. Recommended: Use this when chaining 'generate, then upscale' from a queued job. Leave empty for the architecture default.", type: "string" }, repeats: { title: "Upscale Repeats", description: "Run the upscaler N times. With a 4× model: 1 pass = 4×, 2 passes = 16×. Recommended: 1. Leave empty for the architecture default.", type: "number" }, tileSize: { title: "Upscale Tile Size", description: "Tile size for streaming the source image through the upscaler. Smaller = less VRAM, more boundary artifacts. 0 = whole-image. Recommended: 128 default. Drop to 64 if hitting VRAM limits. Leave empty for the architecture default.", type: "number" }, upscaleFactor: { title: "Upscale Factor", description: "Target upscale factor. 4 = 4× the source image (default for ESRGAN). Recommended: Match the loaded upscaler's native factor (4 for most ESRGAN variants). Leave empty for the architecture default.", type: "number" }, title: { type: "string", title: "Job Title", description: "Optional label stored with the job, useful for finding it again in the queue" }, extraOptions: { type: "object", title: "Extra Options", description: "Any other generation field passed straight through. Everything the server documents already has a setting above, so this is only needed for a field a newer server has gained" }, timeout: { type: "number", title: "Timeout (ms)", description: "Applies to queueing the job, not to the render. The call returns as soon as the job is accepted", default: 30000 } }, required: ['credentialId'] }; const inputSchema = { type: 'object', properties: { data: { type: 'any' } } }; const outputSchema = { type: 'object', properties: { jobId: { type: 'string', description: 'Id of the queued job, to pass to SD.cpp Wait For Job' }, status: { type: 'string', description: 'Queue status when the job was accepted, normally pending' }, position: { type: 'number', description: 'Place in the queue' }, request: { type: 'object', description: 'The body actually sent, useful for seeing which defaults were left to the server' } } }; 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; } // Every generation field the server documents, and the setting it comes // from. Generated alongside the schema above so the two cannot drift. const GENERATION_OPTIONS = [ { setting: "imageBase64", server: "image_base64" }, { setting: "imageIndex", server: "image_index" }, { setting: "jobId", server: "job_id" }, { setting: "repeats", server: "repeats" }, { setting: "tileSize", server: "tile_size" }, { setting: "upscaleFactor", server: "upscale_factor" } ]; async function execute(config, input, context) { const server = normalizeServer(config.serverUrl); const timeout = config.timeout || 30000; const credential = readCredential(config.credentialId); const token = login(server, credential, timeout); const body = {}; // Built from the table above rather than field by field, so a setting the // server documents cannot be quietly missing from the request. A setting // left empty is left out of the body entirely: the server fills an absent // field from the loaded model's architecture preset, and sending an empty // box as 0 would override that preset with nonsense. The test is emptiness, // never truthiness - seed 0 and clip_skip 0 are legitimate values. for (let i = 0; i < GENERATION_OPTIONS.length; i++) { const option = GENERATION_OPTIONS[i]; const value = config[option.setting]; if (Array.isArray(value)) { if (value.length > 0) { body[option.server] = value; } } else { putIfSet(body, option.server, value); } } putIfSet(body, 'title', config.title); if (!body.image_base64) { throw new Error('SD.cpp: upscale needs an image'); } // Anything else the API accepts, passed through, so a new server field does // not need a node change to be reachable. const extra = config.extraOptions; if (extra && typeof extra === 'object') { const keys = Object.keys(extra); for (let i = 0; i < keys.length; i++) { putIfSet(body, keys[i], extra[keys[i]]); } } const queued = call({ method: 'POST', url: server + '/upscale', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token }, body: JSON.stringify(body), timeout: timeout, what: 'queueing the upscale job' }); if (!queued.job_id) { throw new Error('SD.cpp: the job was accepted but no job id came back'); } smartbotic.log.info('SD.cpp: queued upscale job ' + queued.job_id); return { jobId: queued.job_id, status: queued.status || 'pending', position: queued.position !== undefined ? queued.position : -1, request: body }; } module.exports = { configSchema, inputSchema, outputSchema, execute };