sdcpp-upscale.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * @node sdcpp-upscale
  3. * @name SD.cpp Upscale
  4. * @category sdcpp
  5. * @version 1.0.0
  6. * @description Enlarge an image with the loaded upscaler
  7. * @icon maximize-2
  8. */
  9. // The credential this node wants, named so it can be found. It is stored as a
  10. // plain basic credential - that is what decides how it is encrypted - and this
  11. // only says which basic credential is the SD.cpp one. Anything that accepts a
  12. // basic credential still accepts this, and this node still accepts a plain
  13. // basic credential, because the shape is identical.
  14. const credentialTypes = [
  15. {
  16. id: 'sdcpp',
  17. label: 'SD.cpp Server',
  18. baseType: 'basic',
  19. description: 'The username and password you sign in to sdcpp-restapi with. The node exchanges them for a token before every call',
  20. usernameLabel: 'Username',
  21. passwordLabel: 'Password'
  22. }
  23. ];
  24. const configSchema = {
  25. type: 'object',
  26. // Generated from the server's own reference at /options/generation - see
  27. // scripts/gen-sdcpp-generation-options.py. Every field the endpoint accepts
  28. // has a setting here, grouped the way the server groups them.
  29. uiGroups: [
  30. {
  31. "title": "Server",
  32. "fields": [
  33. "serverUrl",
  34. "credentialId"
  35. ]
  36. },
  37. {
  38. "title": "/upscale Endpoint",
  39. "fields": [
  40. "imageBase64",
  41. "jobId",
  42. "imageIndex",
  43. "upscaleFactor",
  44. "tileSize",
  45. "repeats"
  46. ]
  47. },
  48. {
  49. "title": "Job",
  50. "fields": [
  51. "title",
  52. "extraOptions",
  53. "timeout"
  54. ]
  55. }
  56. ],
  57. properties: {
  58. serverUrl: {
  59. type: 'string', title: 'Server URL',
  60. description: 'Base address of the sdcpp-restapi server',
  61. default: 'http://localhost:8077'
  62. },
  63. credentialId: {
  64. type: 'string', title: 'Credential',
  65. description: 'A basic credential holding the sdcpp-restapi username and password',
  66. dynamicOptions: { source: 'credentials', filter: { type: ['sdcpp', 'basic'] } }
  67. },
  68. imageBase64: { title: "Source Image (base64)", description: "Base64-encoded source image for upscaling. Recommended: Required. Leave empty for the architecture default.", type: "string" },
  69. 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" },
  70. 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" },
  71. 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" },
  72. 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" },
  73. 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" },
  74. title: { type: "string", title: "Job Title", description: "Optional label stored with the job, useful for finding it again in the queue" },
  75. 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" },
  76. 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 }
  77. },
  78. required: ['credentialId']
  79. };
  80. const inputSchema = { type: 'object', properties: { data: { type: 'any' } } };
  81. const outputSchema = {
  82. type: 'object',
  83. properties: {
  84. jobId: { type: 'string', description: 'Id of the queued job, to pass to SD.cpp Wait For Job' },
  85. status: { type: 'string', description: 'Queue status when the job was accepted, normally pending' },
  86. position: { type: 'number', description: 'Place in the queue' },
  87. request: { type: 'object', description: 'The body actually sent, useful for seeing which defaults were left to the server' }
  88. }
  89. };
  90. function normalizeServer(url) {
  91. const value = String(url || '').trim();
  92. if (!value) {
  93. throw new Error('SD.cpp: a server URL is required, such as http://localhost:8077');
  94. }
  95. return value.replace(/\/+$/, '');
  96. }
  97. function readCredential(credentialId) {
  98. const auth = smartbotic.credentials.get(credentialId);
  99. if (!auth || auth.success !== true) {
  100. throw new Error('SD.cpp: could not read the credential: ' +
  101. ((auth && auth.error) || 'unknown error'));
  102. }
  103. const value = auth.headerValue || '';
  104. if (value.indexOf('Basic ') !== 0) {
  105. throw new Error('SD.cpp: the credential must be a basic one, holding the sdcpp-restapi ' +
  106. 'username and password');
  107. }
  108. const decoded = smartbotic.utils.base64Decode(value.substring(6));
  109. const separator = decoded.indexOf(':');
  110. if (separator < 1) {
  111. throw new Error('SD.cpp: the credential is malformed, expected a username and a password');
  112. }
  113. return {
  114. username: decoded.substring(0, separator),
  115. password: decoded.substring(separator + 1)
  116. };
  117. }
  118. function call(options) {
  119. const response = smartbotic.http.request(options);
  120. let body = response.data;
  121. if (typeof body === 'string' && body.length > 0) {
  122. try {
  123. body = JSON.parse(body);
  124. } catch (e) {
  125. const snippet = body.substring(0, 200).replace(/\s+/g, ' ');
  126. throw new Error('SD.cpp: ' + options.what + ' returned HTTP ' + response.status +
  127. ' with a body that is not JSON: ' + snippet);
  128. }
  129. }
  130. if (response.status < 200 || response.status >= 300) {
  131. const detail = (body && (body.message || body.error)) || ('HTTP ' + response.status);
  132. throw new Error('SD.cpp: ' + options.what + ' failed: ' + detail);
  133. }
  134. return body || {};
  135. }
  136. function login(server, credential, timeout) {
  137. const session = call({
  138. method: 'POST',
  139. url: server + '/auth/login',
  140. headers: { 'Content-Type': 'application/json' },
  141. body: JSON.stringify({
  142. username: credential.username,
  143. password: credential.password
  144. }),
  145. timeout: timeout,
  146. what: 'signing in'
  147. });
  148. if (!session.token) {
  149. throw new Error('SD.cpp: the server accepted the login but returned no token');
  150. }
  151. return session.token;
  152. }
  153. // /health is unauthenticated, and it is the only way to find out what is
  154. // already loaded without asking for a token first.
  155. function readHealth(server, timeout) {
  156. return call({
  157. method: 'GET',
  158. url: server + '/health',
  159. timeout: timeout,
  160. what: 'reading server health'
  161. });
  162. }
  163. function putIfSet(target, key, value) {
  164. if (value === undefined || value === null || value === '') {
  165. return;
  166. }
  167. target[key] = value;
  168. }
  169. // Every generation field the server documents, and the setting it comes
  170. // from. Generated alongside the schema above so the two cannot drift.
  171. const GENERATION_OPTIONS = [
  172. { setting: "imageBase64", server: "image_base64" },
  173. { setting: "imageIndex", server: "image_index" },
  174. { setting: "jobId", server: "job_id" },
  175. { setting: "repeats", server: "repeats" },
  176. { setting: "tileSize", server: "tile_size" },
  177. { setting: "upscaleFactor", server: "upscale_factor" }
  178. ];
  179. async function execute(config, input, context) {
  180. const server = normalizeServer(config.serverUrl);
  181. const timeout = config.timeout || 30000;
  182. const credential = readCredential(config.credentialId);
  183. const token = login(server, credential, timeout);
  184. const body = {};
  185. // Built from the table above rather than field by field, so a setting the
  186. // server documents cannot be quietly missing from the request. A setting
  187. // left empty is left out of the body entirely: the server fills an absent
  188. // field from the loaded model's architecture preset, and sending an empty
  189. // box as 0 would override that preset with nonsense. The test is emptiness,
  190. // never truthiness - seed 0 and clip_skip 0 are legitimate values.
  191. for (let i = 0; i < GENERATION_OPTIONS.length; i++) {
  192. const option = GENERATION_OPTIONS[i];
  193. const value = config[option.setting];
  194. if (Array.isArray(value)) {
  195. if (value.length > 0) {
  196. body[option.server] = value;
  197. }
  198. } else {
  199. putIfSet(body, option.server, value);
  200. }
  201. }
  202. putIfSet(body, 'title', config.title);
  203. if (!body.image_base64) {
  204. throw new Error('SD.cpp: upscale needs an image');
  205. }
  206. // Anything else the API accepts, passed through, so a new server field does
  207. // not need a node change to be reachable.
  208. const extra = config.extraOptions;
  209. if (extra && typeof extra === 'object') {
  210. const keys = Object.keys(extra);
  211. for (let i = 0; i < keys.length; i++) {
  212. putIfSet(body, keys[i], extra[keys[i]]);
  213. }
  214. }
  215. const queued = call({
  216. method: 'POST',
  217. url: server + '/upscale',
  218. headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  219. body: JSON.stringify(body),
  220. timeout: timeout,
  221. what: 'queueing the upscale job'
  222. });
  223. if (!queued.job_id) {
  224. throw new Error('SD.cpp: the job was accepted but no job id came back');
  225. }
  226. smartbotic.log.info('SD.cpp: queued upscale job ' + queued.job_id);
  227. return {
  228. jobId: queued.job_id,
  229. status: queued.status || 'pending',
  230. position: queued.position !== undefined ? queued.position : -1,
  231. request: body
  232. };
  233. }
  234. module.exports = { configSchema, inputSchema, outputSchema, execute };