| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- /**
- * @node sdcpp-upscaler-load
- * @name SD.cpp Load Upscaler
- * @category sdcpp
- * @version 1.0.0
- * @description Make sure an upscaler is loaded, without reloading one that already is
- * @icon maximize
- */
- 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: 'Upscaler',
- description: 'File name of the ESRGAN-family upscaler. Browse lists what the server has',
- dynamicOptions: {
- source: 'node',
- node: 'sdcpp-model',
- config: { listOnly: true, modelType: 'esrgan' },
- itemsPath: 'models',
- valueKey: 'name',
- labelKey: 'name',
- needs: ['serverUrl', 'credentialId']
- }
- },
- force: {
- type: 'boolean', title: 'Force Reload',
- default: false,
- description: 'Load again even when this upscaler is already loaded'
- },
- timeout: { type: 'number', title: 'Timeout (ms)', default: 120000 }
- },
- required: []
- };
- const inputSchema = { type: 'object', properties: { data: { type: 'any' } } };
- const outputSchema = {
- type: 'object',
- properties: {
- upscalerName: { type: 'string' },
- loaded: { type: 'boolean', description: 'True when this node performed a load' },
- alreadyLoaded: { type: 'boolean' },
- previousUpscaler: { type: 'string' },
- 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 : 120000;
- const modelName = String(config.modelName || '').trim();
- if (!modelName) {
- throw new Error('SD.cpp: an upscaler name is required. Connect an SD.cpp Model node ' +
- 'set to type esrgan, or type the file name');
- }
- const startedAt = Date.now();
- const health = readHealth(server, Math.min(timeout, 15000));
- const current = health.upscaler_name || '';
- // The upscaler lives in its own slot, independent of the main model, so
- // loading one does not disturb whatever is generating.
- if (current === modelName && config.force !== true) {
- smartbotic.log.info('SD.cpp: upscaler ' + modelName + ' is already loaded');
- return {
- upscalerName: current,
- loaded: false,
- alreadyLoaded: true,
- previousUpscaler: '',
- elapsedMs: Date.now() - startedAt
- };
- }
- const credential = readCredential(config.credentialId);
- const token = login(server, credential, Math.min(timeout, 30000));
- call({
- method: 'POST',
- url: server + '/upscaler/load',
- headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
- body: JSON.stringify({ model_name: modelName }),
- timeout: timeout,
- what: 'loading upscaler ' + modelName
- });
- smartbotic.log.info('SD.cpp: loaded upscaler ' + modelName);
- return {
- upscalerName: modelName,
- loaded: true,
- alreadyLoaded: false,
- previousUpscaler: current === modelName ? '' : current,
- elapsedMs: Date.now() - startedAt
- };
- }
- module.exports = { configSchema, inputSchema, outputSchema, execute };
|