sdcpp-architecture.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @node sdcpp-architecture
  3. * @name SD.cpp Architecture Preset
  4. * @category sdcpp
  5. * @version 1.0.0
  6. * @description Read the generation defaults SD.cpp uses for a model's architecture
  7. * @icon sliders-horizontal
  8. */
  9. const credentialTypes = [
  10. {
  11. id: 'sdcpp',
  12. label: 'SD.cpp Server',
  13. baseType: 'basic',
  14. description: 'The username and password you sign in to sdcpp-restapi with. The node exchanges them for a token before every call',
  15. usernameLabel: 'Username',
  16. passwordLabel: 'Password'
  17. }
  18. ];
  19. const configSchema = {
  20. type: 'object',
  21. uiGroups: [
  22. { title: 'Server', fields: ['serverUrl', 'credentialId'] },
  23. { title: 'Model', fields: ['modelName', 'timeout'] }
  24. ],
  25. properties: {
  26. serverUrl: {
  27. type: 'string', title: 'Server URL',
  28. description: 'Base address of the sdcpp-restapi server',
  29. default: 'http://localhost:8077'
  30. },
  31. credentialId: {
  32. type: 'string', title: 'Credential',
  33. description: 'A basic credential holding the sdcpp-restapi username and password',
  34. dynamicOptions: { source: 'credentials', filter: { type: ['sdcpp', 'basic'] } }
  35. },
  36. modelName: {
  37. type: 'string', title: 'Model',
  38. description: 'Which model to look up. Leave empty to use whatever the server currently has loaded',
  39. dynamicOptions: {
  40. source: 'node',
  41. node: 'sdcpp-model',
  42. config: { listOnly: true },
  43. itemsPath: 'models',
  44. valueKey: 'name',
  45. labelKey: 'name',
  46. needs: ['serverUrl', 'credentialId']
  47. }
  48. },
  49. timeout: {
  50. type: 'number', title: 'Timeout (ms)',
  51. description: 'How long to wait for the server to answer',
  52. default: 30000
  53. }
  54. },
  55. required: ['credentialId']
  56. };
  57. const inputSchema = { type: 'object', properties: { data: { type: 'any' } } };
  58. const outputSchema = {
  59. type: 'object',
  60. properties: {
  61. found: { type: 'boolean', description: 'False when the server publishes no preset for this model' },
  62. architecture: { type: 'string', description: 'Architecture id, such as Z-Image or Flux Schnell' },
  63. architectureName: { type: 'string', description: 'Its display name' },
  64. modelName: { type: 'string', description: 'The model this describes' },
  65. description: { type: 'string', description: 'What the server says about this architecture' },
  66. imageEditMode: { type: 'string', description: 'How this architecture takes an image to edit, when it can' },
  67. requiredComponents: { type: 'object', description: 'VAE, LLM and so on that this architecture needs loaded' },
  68. defaults: { type: 'object', description: 'The generation defaults, named the way the generation nodes name their settings' },
  69. preset: { type: 'object', description: 'The preset exactly as the server describes it' }
  70. }
  71. };
  72. function normalizeServer(url) {
  73. const value = String(url || '').trim();
  74. if (!value) {
  75. throw new Error('SD.cpp: a server URL is required, such as http://localhost:8077');
  76. }
  77. return value.replace(/\/+$/, '');
  78. }
  79. function readCredential(credentialId) {
  80. const auth = smartbotic.credentials.get(credentialId);
  81. if (!auth || auth.success !== true) {
  82. throw new Error('SD.cpp: could not read the credential: ' +
  83. ((auth && auth.error) || 'unknown error'));
  84. }
  85. const value = auth.headerValue || '';
  86. if (value.indexOf('Basic ') !== 0) {
  87. throw new Error('SD.cpp: the credential must be a basic one, holding the sdcpp-restapi ' +
  88. 'username and password');
  89. }
  90. const decoded = smartbotic.utils.base64Decode(value.substring(6));
  91. const separator = decoded.indexOf(':');
  92. if (separator < 1) {
  93. throw new Error('SD.cpp: the credential is malformed, expected a username and a password');
  94. }
  95. return {
  96. username: decoded.substring(0, separator),
  97. password: decoded.substring(separator + 1)
  98. };
  99. }
  100. function call(options) {
  101. const response = smartbotic.http.request(options);
  102. let body = response.data;
  103. if (typeof body === 'string' && body.length > 0) {
  104. try {
  105. body = JSON.parse(body);
  106. } catch (e) {
  107. const snippet = body.substring(0, 200).replace(/\s+/g, ' ');
  108. throw new Error('SD.cpp: ' + options.what + ' returned HTTP ' + response.status +
  109. ' with a body that is not JSON: ' + snippet);
  110. }
  111. }
  112. if (response.status < 200 || response.status >= 300) {
  113. const detail = (body && (body.message || body.error)) || ('HTTP ' + response.status);
  114. throw new Error('SD.cpp: ' + options.what + ' failed: ' + detail);
  115. }
  116. return body || {};
  117. }
  118. function login(server, credential, timeout) {
  119. const session = call({
  120. method: 'POST',
  121. url: server + '/auth/login',
  122. headers: { 'Content-Type': 'application/json' },
  123. body: JSON.stringify({ username: credential.username, password: credential.password }),
  124. timeout: timeout,
  125. what: 'signing in'
  126. });
  127. if (!session.token) {
  128. throw new Error('SD.cpp: the server accepted the login but returned no token');
  129. }
  130. return session.token;
  131. }
  132. // The server names preset fields the way the generation endpoints do -
  133. // cfg_scale, cache_mode - while the nodes name their settings the way the rest
  134. // of the editor does. One rule converts between them, so a preset that gains a
  135. // field arrives without this node being touched.
  136. function toSetting(name) {
  137. const parts = String(name).split('_');
  138. let out = parts[0];
  139. for (let i = 1; i < parts.length; i++) {
  140. out += parts[i].charAt(0).toUpperCase() + parts[i].slice(1);
  141. }
  142. return out;
  143. }
  144. // Presets are keyed by their id, but a model can be known by a name or an
  145. // alias - "ZImage" and "Z Image" both mean Z-Image - so all three are tried.
  146. function findPreset(architectures, wanted) {
  147. const target = String(wanted || '').trim().toLowerCase();
  148. if (!target) { return null; }
  149. const keys = Object.keys(architectures || {});
  150. for (let i = 0; i < keys.length; i++) {
  151. const preset = architectures[keys[i]] || {};
  152. const names = [keys[i], preset.id, preset.name];
  153. const aliases = preset.aliases;
  154. if (aliases && typeof aliases.length === 'number') {
  155. for (let j = 0; j < aliases.length; j++) { names.push(aliases[j]); }
  156. }
  157. for (let j = 0; j < names.length; j++) {
  158. if (String(names[j] || '').trim().toLowerCase() === target) {
  159. return preset;
  160. }
  161. }
  162. }
  163. return null;
  164. }
  165. async function execute(config, input, context) {
  166. const server = normalizeServer(config.serverUrl);
  167. const timeout = config.timeout || 30000;
  168. const credential = readCredential(config.credentialId);
  169. const token = login(server, credential, timeout);
  170. const authHeader = { 'Authorization': 'Bearer ' + token };
  171. let modelName = String(config.modelName || '').trim();
  172. let architecture = '';
  173. if (!modelName) {
  174. // /health needs no token, and for a model that is already loaded it
  175. // names the architecture outright - which detection does not always
  176. // manage from a filename alone.
  177. const health = call({
  178. method: 'GET', url: server + '/health', timeout: timeout,
  179. what: 'reading server health'
  180. });
  181. modelName = String(health.model_name || health.loading_model_name || '').trim();
  182. architecture = String(health.model_architecture || '');
  183. }
  184. if (!modelName && !architecture) {
  185. throw new Error('SD.cpp: no model given and the server has none loaded, so there is no ' +
  186. 'architecture to look up');
  187. }
  188. if (!architecture && modelName) {
  189. const detection = call({
  190. method: 'GET',
  191. url: server + '/architectures/detect?model=' + encodeURIComponent(modelName),
  192. headers: authHeader,
  193. timeout: timeout,
  194. what: 'detecting the architecture of "' + modelName + '"'
  195. });
  196. const detected = detection.architecture || {};
  197. architecture = String(detected.id || detected.name || '');
  198. }
  199. const listing = call({
  200. method: 'GET', url: server + '/architectures', headers: authHeader,
  201. timeout: timeout, what: 'reading the architecture presets'
  202. });
  203. const preset = findPreset(listing.architectures, architecture) || {};
  204. // The preset's generation defaults, under the names the generation nodes use
  205. // for them. This is what the Fill-in button on those nodes writes into the
  206. // form.
  207. const defaults = {};
  208. const raw = preset.generationDefaults || {};
  209. const keys = Object.keys(raw);
  210. for (let i = 0; i < keys.length; i++) {
  211. defaults[toSetting(keys[i])] = raw[keys[i]];
  212. }
  213. if (!preset.id) {
  214. smartbotic.log.warn('SD.cpp: no preset for "' + (architecture || modelName) +
  215. '" - nothing to fill in from');
  216. } else {
  217. smartbotic.log.info('SD.cpp: ' + modelName + ' is ' + preset.id + ', ' +
  218. keys.length + ' generation default(s)');
  219. }
  220. return {
  221. found: !!preset.id,
  222. architecture: String(preset.id || architecture || ''),
  223. architectureName: String(preset.name || ''),
  224. modelName: modelName,
  225. description: String(preset.description || ''),
  226. imageEditMode: String(preset.imageEditMode || ''),
  227. requiredComponents: preset.requiredComponents || {},
  228. defaults: defaults,
  229. preset: preset
  230. };
  231. }
  232. module.exports = { credentialTypes, configSchema, inputSchema, outputSchema, execute };