sdcpp-txt2img.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. * @node sdcpp-txt2img
  3. * @name SD.cpp Text to Image
  4. * @category sdcpp
  5. * @version 1.0.0
  6. * @description Queue an image generation from a prompt
  7. * @icon image
  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": "Core",
  39. "fields": [
  40. "prompt",
  41. "negativePrompt",
  42. "width",
  43. "height",
  44. "steps",
  45. "cfgScale",
  46. "seed",
  47. "sampler",
  48. "scheduler",
  49. "batchCount"
  50. ]
  51. },
  52. {
  53. "title": "Cache Acceleration",
  54. "fields": [
  55. "cacheMode",
  56. "easycacheThreshold",
  57. "easycacheStart",
  58. "easycacheEnd",
  59. "spectrumW",
  60. "spectrumM",
  61. "spectrumLam",
  62. "spectrumWindowSize",
  63. "spectrumFlexWindow",
  64. "spectrumWarmupSteps",
  65. "spectrumStopPercent"
  66. ]
  67. },
  68. {
  69. "title": "ControlNet",
  70. "fields": [
  71. "controlImageBase64",
  72. "controlStrength"
  73. ]
  74. },
  75. {
  76. "title": "Prompt Expansion",
  77. "fields": [
  78. "expandPrompt"
  79. ]
  80. },
  81. {
  82. "title": "Guidance",
  83. "fields": [
  84. "distilledGuidance",
  85. "eta",
  86. "shiftedTimestep",
  87. "flowShift",
  88. "clipSkip"
  89. ]
  90. },
  91. {
  92. "title": "Image Input (img2img / image-edit)",
  93. "fields": [
  94. "refImages",
  95. "refImageArgs"
  96. ]
  97. },
  98. {
  99. "title": "PhotoMaker",
  100. "fields": [
  101. "pmIdImages",
  102. "pmIdEmbedPath",
  103. "pmStyleStrength"
  104. ]
  105. },
  106. {
  107. "title": "Skip Layer Guidance (SLG)",
  108. "fields": [
  109. "slgScale",
  110. "skipLayers",
  111. "slgStart",
  112. "slgEnd",
  113. "customSigmas"
  114. ]
  115. },
  116. {
  117. "title": "Upscale After Generation",
  118. "fields": [
  119. "upscale",
  120. "upscaleRepeats",
  121. "upscaleAutoUnload"
  122. ]
  123. },
  124. {
  125. "title": "VAE Tiling (per-generation)",
  126. "fields": [
  127. "vaeTiling",
  128. "vaeTileSizeX",
  129. "vaeTileSizeY",
  130. "vaeTileOverlap"
  131. ]
  132. },
  133. {
  134. "title": "Job",
  135. "fields": [
  136. "title",
  137. "extraOptions",
  138. "timeout"
  139. ]
  140. }
  141. ],
  142. prefill: {
  143. "label": "Take the architecture defaults",
  144. "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",
  145. "node": "sdcpp-architecture",
  146. "needs": [
  147. "serverUrl",
  148. "credentialId"
  149. ],
  150. "map": {
  151. "defaults.width": "width",
  152. "defaults.height": "height",
  153. "defaults.steps": "steps",
  154. "defaults.cfgScale": "cfgScale",
  155. "defaults.sampler": "sampler",
  156. "defaults.scheduler": "scheduler",
  157. "defaults.cacheMode": "cacheMode",
  158. "defaults.distilledGuidance": "distilledGuidance",
  159. "defaults.flowShift": "flowShift",
  160. "defaults.negativePrompt": "negativePrompt"
  161. }
  162. },
  163. properties: {
  164. serverUrl: {
  165. type: 'string', title: 'Server URL',
  166. description: 'Base address of the sdcpp-restapi server',
  167. default: 'http://localhost:8077'
  168. },
  169. credentialId: {
  170. type: 'string', title: 'Credential',
  171. description: 'A basic credential holding the sdcpp-restapi username and password',
  172. dynamicOptions: { source: 'credentials', filter: { type: ['sdcpp', 'basic'] } }
  173. },
  174. batchCount: { title: "Batch Count", description: "Number of independent images to produce in this single job. Each image gets a different seed (`seed`, `seed+1`, …). Total VRAM doesn't grow with batch_count — sd.cpp serializes. Recommended: 1 for interactive, higher when you want a grid of variations from one prompt. Leave empty for the architecture default.", type: "number" },
  175. cacheMode: { title: "Cache Mode", description: "DiT-model intermediate caching strategy. Skips redundant computation across consecutive sampler steps when the model's intermediate state hasn't changed enough to matter. `easycache` is the simplest; `spectrum` is the newest, generally best for Flux/SD3/Z-Image. Recommended: spectrum for Flux/SD3/Z-Image when generation is too slow. Off for SD1.5/SDXL (UNet is too small for caching to win). Leave empty for the architecture default.", type: "string", enum: ["", "easycache", "spectrum"], enumLabels: ["(architecture default)", "EasyCache — single threshold, simple", "Spectrum — frequency-domain analysis (best quality/speed tradeoff)"], default: "" },
  176. cfgScale: { title: "CFG Scale", description: "Classifier-Free Guidance scale. Strength of pushing the cond toward the prompt vs the uncond. Higher = follows prompt more aggressively but can over-saturate. Set to 1.0 to disable CFG (skips the uncond pass — twice as fast). Recommended: SD1.5: 7. SDXL: 4–8. Flux Dev: 1 (CFG bypassed in flow models). Z-Image: 1. Schnell/Turbo: 1. Leave empty for the architecture default.", type: "number" },
  177. clipSkip: { title: "CLIP Skip", description: "Skip the last N layers of CLIP when encoding the prompt. -1 = use the model's recommended default. 2 is the classic anime-model setting. Recommended: -1 (auto). Set to 2 for anime/cartoon SD1.5 fine-tunes. Leave empty for the architecture default.", type: "number" },
  178. controlImageBase64: { title: "ControlNet Image (base64)", description: "Base64-encoded pre-processed control image. Must match the ControlNet model loaded — Canny edges for canny model, depth map for depth model, etc. sd.cpp does not pre-process; you do that client-side. Recommended: Required when using ControlNet. Leave empty for the architecture default.", type: "string" },
  179. controlStrength: { title: "ControlNet Strength", description: "How much the ControlNet influences the diffusion process (0.0–1.0). Lower = looser following, higher = tighter following at cost of overall coherence. Recommended: 0.6–0.9 for most uses. Lower for creative prompts where control should be a hint, not a constraint. Leave empty for the architecture default.", type: "number" },
  180. customSigmas: { title: "Custom Sigma Schedule", description: "Replace the scheduler's noise sigma sequence with a hand-tuned one. Bypasses `scheduler`. Empty array = use the chosen scheduler. Recommended: Empty unless you're hand-tuning per-step noise levels — niche. Leave empty for the architecture default.", type: "array", items: {"type": "number"} },
  181. distilledGuidance: { title: "Distilled Guidance", description: "Distilled-CFG scale for models that bake the CFG behavior into the diffusion model itself (Flux). Replaces the runtime CFG split. Recommended: Flux Dev: 3.5 (sd.cpp default). Other architectures: ignored. Leave empty for the architecture default.", type: "number" },
  182. easycacheEnd: { title: "EasyCache End (% of steps)", description: "Fraction of steps after which EasyCache turns off (last few steps fully recomputed for sharpness). Recommended: 0.95. Leave empty for the architecture default.", type: "number" },
  183. easycacheStart: { title: "EasyCache Start (% of steps)", description: "Fraction of steps before EasyCache becomes active. Small to skip the early high-noise steps where caching hurts. Recommended: 0.15. Leave empty for the architecture default.", type: "number" },
  184. easycacheThreshold: { title: "EasyCache Threshold", description: "Reuse threshold for EasyCache. Higher = more aggressive reuse (faster but more quality loss). Range typically 0.05–0.4. Recommended: 0.2 starting point. Tune up for more speed, down for more quality. Leave empty for the architecture default.", type: "number" },
  185. eta: { title: "Eta", description: "Stochasticity parameter for DDIM-family samplers. 0 = deterministic, 1 = max stochasticity (matches DDPM noise schedule). Most other samplers ignore this. Recommended: 0 (deterministic, reproducible). Raise only with DDIM-trailing for some variation. Leave empty for the architecture default.", type: "number" },
  186. expandPrompt: { title: "Expand Prompt Template", description: "If true, parse `prompt` for a1111-style dynamic-prompts syntax (`{a|b|c}`, `{N$$a|b|c}`) and create one queue item per variation. The response then carries `group_id` + `variation_count` + `job_ids[]` instead of a single `job_id`. Hard cap: 200 variations per request. Recommended: Enable when your prompt actually contains `{…}` syntax. Leave empty for the architecture default.", type: "boolean" },
  187. flowShift: { title: "Flow Shift", description: "Per-generation flow-matching shift parameter for Flux / SD3 / Z-Image models. Higher values shift sampling toward larger noise levels longer. Per-call override; the load-time `flow_shift` is the default if this isn't set. Recommended: Flux Dev: 1.0. Z-Image: 3.0. SD3 Medium: 3.0. Leave empty for the architecture default.", type: "number" },
  188. height: { title: "Height (px)", description: "Output image height in pixels. Same divisibility constraint as `width`. Recommended: Match training resolution. For non-square: total pixel count close to native is more important than aspect ratio. Leave empty for the architecture default.", type: "number" },
  189. negativePrompt: { title: "Negative Prompt", description: "What to push the model away from. Has effect only when `cfg_scale > 1` (CFG is what compares cond vs uncond). Ignored on architectures that don't use CFG (most flow-matching models default to `cfg_scale = 1`). Recommended: Optional. Empty is a fine default for Flux / SD3 / Z-Image. Leave empty for the architecture default.", type: "string", format: "textarea" },
  190. pmIdEmbedPath: { title: "PhotoMaker ID Embed Path", description: "Server-side path to a pre-computed PhotoMaker ID embedding. Skips per-generation ID encoding. Recommended: Empty unless you've pre-computed an embedding for repeated use. Leave empty for the architecture default.", type: "string" },
  191. pmIdImages: { title: "PhotoMaker ID Images (base64)", description: "Reference images for PhotoMaker identity preservation. Multiple faces of the same person. Recommended: 3–5 images of the same person from different angles when using PhotoMaker. Leave empty for the architecture default.", type: "array", items: {"type": "string"} },
  192. pmStyleStrength: { title: "PhotoMaker Style Strength", description: "PhotoMaker style-vs-identity tradeoff. Higher = more style influence, less identity preservation. Recommended: 20 (sd.cpp default). Drop to 10 for stronger identity preservation. Leave empty for the architecture default.", type: "number" },
  193. prompt: { title: "Prompt", description: "Text prompt fed to the model's text encoder (CLIP / T5 / LLM). Supports inline `<lora:name:weight>` tags — these are auto-extracted into the LoRA list before being sent to the encoder. Supports a1111-style dynamic-prompts (`{a|b|c}`) when `expand_prompt: true` is set. Recommended: Required. Leave empty for the architecture default.", type: "string", format: "textarea" },
  194. refImageArgs: { title: "Reference Image Args", description: "Comma-separated k=v flags controlling reference-image preprocessing (e.g. `resize_before_vae=0,ref_index_mode=increase`). Replaces the previous auto_resize_ref_image / increase_ref_index bools. See sd.cpp docs. Recommended: Leave empty unless you need to override defaults. Leave empty for the architecture default.", type: "string" },
  195. refImages: { title: "Reference Images (base64)", description: "Base64-encoded reference images for Flux Kontext / image-edit. Each image gets encoded into the conditioner alongside the text prompt. Recommended: Use for Flux Kontext or models that accept reference images. Leave empty for the architecture default.", type: "array", items: {"type": "string"} },
  196. sampler: { title: "Sampler", description: "Sampling algorithm. Different samplers can produce different images at the same seed; quality and speed differ too. Recommended: euler_a (general), dpmpp2m (SD1.5/SDXL), euler (Flux/SD3/Z-Image), lcm (LCM models). Leave empty for the architecture default.", type: "string", enum: ["", "ddim_trailing", "dpm2", "dpmpp2m", "dpmpp2mv2", "dpmpp2s_a", "er_sde", "euler", "euler_a", "heun", "ipndm", "ipndm_v", "lcm", "res_2s", "res_multistep", "tcd"], enumLabels: ["(architecture default)", "DDIM trailing — required for some fine-tunes", "DPM2 — 2nd-order, balanced", "DPM++ 2M — fast, good quality", "DPM++ 2M v2 — improved schedule", "DPM++ 2S ancestral — popular for SDXL", "ER SDE — SDE sampler (added recently)", "Euler — simple, fast, deterministic", "Euler ancestral — adds noise each step (less reproducible, more variet", "Heun — 2nd-order, slower, sometimes higher quality", "IPNDM", "IPNDM-V", "LCM — for LCM-finetuned models (4–8 step generation)", "RES 2S", "RES multistep — flow-model variant", "TCD — for TCD-finetuned models"], default: "" },
  197. scheduler: { title: "Scheduler", description: "Determines the noise schedule (the timesteps the sampler walks through). Pairs with the sampler — some combinations (e.g. karras+dpmpp2m) are well-tested, others may be off. Recommended: discrete or karras for SD1.5/SDXL; simple for Flux/SD3; smoothstep for Z-Image. Leave empty for the architecture default.", type: "string", enum: ["", "ays", "bong_tangent", "discrete", "exponential", "gits", "karras", "kl_optimal", "lcm", "sgm_uniform", "simple", "smoothstep"], enumLabels: ["(architecture default)", "Align Your Steps (AYS) — auto-tuned", "Bong Tangent", "Discrete — uniform across model timesteps (default)", "Exponential", "GITS", "Karras — concentrates more steps near the end, common for SDXL", "KL optimal", "LCM — for LCM samplers", "SGM uniform — for SGM-trained models", "Simple — used by Flux / SD3 flow models", "Smoothstep — Z-Image's recommended scheduler"], default: "" },
  198. seed: { title: "Seed", description: "RNG seed for the initial noise tensor (and stochastic samplers). -1 = pick a random one each generation. Same seed + same prompt + same model = same image. Recommended: -1 for variety. Pin a specific number for A/B comparing prompt or sampler changes. Leave empty for the architecture default.", type: "number" },
  199. shiftedTimestep: { title: "Shifted Timestep", description: "Start the sampling schedule from a non-final timestep (used by NitroFusion and similar fast-sampling fine-tunes). 0 = standard schedule. 250–500 = NitroFusion's range. Recommended: 0 unless you're explicitly running a NitroFusion-style fine-tune. Leave empty for the architecture default.", type: "number" },
  200. skipLayers: { title: "SLG Skip Layers", description: "Which transformer layers to skip during the SLG unconditional pass. SD3.5 Medium uses [7, 8, 9]. Recommended: [7,8,9] for SD3.5 Medium. Other models: leave default, ignored when slg_scale=0. Leave empty for the architecture default.", type: "array", items: {"type": "number"} },
  201. slgEnd: { title: "SLG End (% of steps)", description: "Fraction of the sampler schedule at which SLG turns off. Recommended: 0.2 (early-cycle only — late-cycle SLG hurts quality). Leave empty for the architecture default.", type: "number" },
  202. slgScale: { title: "SLG Scale", description: "Skip Layer Guidance scale. Selectively zeros out a few diffusion layers when computing the unconditional pass — improves anatomy / coherence on SD3.5-medium and similar. 0 = disabled. Recommended: 0 for most models. 2.5 for SD3.5 Medium with the recommended skip layers. Leave empty for the architecture default.", type: "number" },
  203. slgStart: { title: "SLG Start (% of steps)", description: "Fraction of the sampler schedule (0.0–1.0) at which SLG kicks in. Recommended: 0.01 (almost from the start). Leave empty for the architecture default.", type: "number" },
  204. spectrumFlexWindow: { title: "Spectrum Cache: flex window", description: "Spectrum-cache flexibility window (0.0–1.0). Recommended: 0.5. Leave empty for the architecture default.", type: "number" },
  205. spectrumLam: { title: "Spectrum Cache: λ", description: "Spectrum-cache regularization λ. Recommended: 0.5. Leave empty for the architecture default.", type: "number" },
  206. spectrumM: { title: "Spectrum Cache: m", description: "Spectrum-cache moving-average length. Recommended: 5. Leave empty for the architecture default.", type: "number" },
  207. spectrumStopPercent: { title: "Spectrum Cache: stop percent", description: "Fraction of steps after which spectrum cache disengages (last steps recomputed). Recommended: 0.8. Leave empty for the architecture default.", type: "number" },
  208. spectrumW: { title: "Spectrum Cache: w", description: "Spectrum-cache `w` weight (frequency cutoff). Higher = retains more spectrum, less speedup. Recommended: 0.5 default. See sd.cpp PR #1322 for tuning. Leave empty for the architecture default.", type: "number" },
  209. spectrumWarmupSteps: { title: "Spectrum Cache: warmup steps", description: "Steps at the start of sampling before spectrum cache becomes active. Recommended: 2. Leave empty for the architecture default.", type: "number" },
  210. spectrumWindowSize: { title: "Spectrum Cache: window size", description: "Spectrum-cache analysis window size in steps. Recommended: 3. Leave empty for the architecture default.", type: "number" },
  211. steps: { title: "Sampling Steps", description: "Number of denoising steps the sampler runs. More steps = closer to the model's converged output, with diminishing returns. Distilled models (Flux Schnell, SDXL Turbo, Z-Image Turbo) need only 4–8. Recommended: 20–30 for SD1.5/SDXL, 20 for Flux Dev, 4–8 for *-Turbo or Schnell variants. Leave empty for the architecture default.", type: "number" },
  212. upscale: { title: "Upscale After Generate", description: "Run the loaded ESRGAN upscaler on the output image after generation. Requires an upscaler to be loaded via POST /upscaler/load. Recommended: On for one-shot 'generate then upscale' workflows. Leave empty for the architecture default.", type: "boolean" },
  213. upscaleAutoUnload: { title: "Auto-unload Upscaler", description: "Free upscaler VRAM right after the upscale step. Useful when you generated with `upscale: true` and want VRAM back for other work. Recommended: On. Leave empty for the architecture default.", type: "boolean" },
  214. upscaleRepeats: { title: "Upscale Repeats", description: "Number of post-generation auto-upscale passes (each with the upscaler's native factor — 4× ESRGAN × 2 passes = 16×). DISTINCT from the `/upscale` endpoint's own `repeats` field, which controls passes inside a single upscale job. `upscale_repeats` only chains additional /upscale calls after txt2img / img2img finish. Recommended: 1. Two passes amplify artifacts, but produces large outputs from small inputs. Leave empty for the architecture default.", type: "number" },
  215. vaeTileOverlap: { title: "VAE Tile Overlap", description: "Overlap fraction between VAE tiles for seam blending. Recommended: 0.5. Leave empty for the architecture default.", type: "number" },
  216. vaeTileSizeX: { title: "VAE Tile Width", description: "Width of VAE tiles when tiling is on. 0 = use load-time default. Recommended: 0. Leave empty for the architecture default.", type: "number" },
  217. vaeTileSizeY: { title: "VAE Tile Height", description: "Height of VAE tiles. 0 = use load-time default. Recommended: 0. Leave empty for the architecture default.", type: "number" },
  218. vaeTiling: { title: "VAE Tiling", description: "Per-generation override of the model-load `vae_tiling`. Process VAE encode/decode in tiles to reduce peak VRAM. Recommended: Enable for ≥2048 px outputs. Otherwise leave to the load-time default. Leave empty for the architecture default.", type: "boolean" },
  219. width: { title: "Width (px)", description: "Output image width in pixels. Must be divisible by the model's patch size (typically 8 or 16). Architectures have native resolutions they were trained at — going far off them can degrade quality. Recommended: Match the architecture's training resolution: SD1.5=512, SDXL=1024, Flux/SD3/Z-Image=1024, Wan video=832. Leave empty for the architecture default.", type: "number" },
  220. title: { type: "string", title: "Job Title", description: "Optional label stored with the job, useful for finding it again in the queue" },
  221. 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" },
  222. 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 }
  223. },
  224. required: ['credentialId']
  225. };
  226. const inputSchema = { type: 'object', properties: { data: { type: 'any' } } };
  227. const outputSchema = {
  228. type: 'object',
  229. properties: {
  230. jobId: { type: 'string', description: 'Id of the queued job, to pass to SD.cpp Wait For Job' },
  231. status: { type: 'string', description: 'Queue status when the job was accepted, normally pending' },
  232. position: { type: 'number', description: 'Place in the queue' },
  233. request: { type: 'object', description: 'The body actually sent, useful for seeing which defaults were left to the server' }
  234. }
  235. };
  236. function normalizeServer(url) {
  237. const value = String(url || '').trim();
  238. if (!value) {
  239. throw new Error('SD.cpp: a server URL is required, such as http://localhost:8077');
  240. }
  241. return value.replace(/\/+$/, '');
  242. }
  243. function readCredential(credentialId) {
  244. const auth = smartbotic.credentials.get(credentialId);
  245. if (!auth || auth.success !== true) {
  246. throw new Error('SD.cpp: could not read the credential: ' +
  247. ((auth && auth.error) || 'unknown error'));
  248. }
  249. const value = auth.headerValue || '';
  250. if (value.indexOf('Basic ') !== 0) {
  251. throw new Error('SD.cpp: the credential must be a basic one, holding the sdcpp-restapi ' +
  252. 'username and password');
  253. }
  254. const decoded = smartbotic.utils.base64Decode(value.substring(6));
  255. const separator = decoded.indexOf(':');
  256. if (separator < 1) {
  257. throw new Error('SD.cpp: the credential is malformed, expected a username and a password');
  258. }
  259. return {
  260. username: decoded.substring(0, separator),
  261. password: decoded.substring(separator + 1)
  262. };
  263. }
  264. function call(options) {
  265. const response = smartbotic.http.request(options);
  266. let body = response.data;
  267. if (typeof body === 'string' && body.length > 0) {
  268. try {
  269. body = JSON.parse(body);
  270. } catch (e) {
  271. const snippet = body.substring(0, 200).replace(/\s+/g, ' ');
  272. throw new Error('SD.cpp: ' + options.what + ' returned HTTP ' + response.status +
  273. ' with a body that is not JSON: ' + snippet);
  274. }
  275. }
  276. if (response.status < 200 || response.status >= 300) {
  277. const detail = (body && (body.message || body.error)) || ('HTTP ' + response.status);
  278. throw new Error('SD.cpp: ' + options.what + ' failed: ' + detail);
  279. }
  280. return body || {};
  281. }
  282. function login(server, credential, timeout) {
  283. const session = call({
  284. method: 'POST',
  285. url: server + '/auth/login',
  286. headers: { 'Content-Type': 'application/json' },
  287. body: JSON.stringify({
  288. username: credential.username,
  289. password: credential.password
  290. }),
  291. timeout: timeout,
  292. what: 'signing in'
  293. });
  294. if (!session.token) {
  295. throw new Error('SD.cpp: the server accepted the login but returned no token');
  296. }
  297. return session.token;
  298. }
  299. // /health is unauthenticated, and it is the only way to find out what is
  300. // already loaded without asking for a token first.
  301. function readHealth(server, timeout) {
  302. return call({
  303. method: 'GET',
  304. url: server + '/health',
  305. timeout: timeout,
  306. what: 'reading server health'
  307. });
  308. }
  309. function putIfSet(target, key, value) {
  310. if (value === undefined || value === null || value === '') {
  311. return;
  312. }
  313. target[key] = value;
  314. }
  315. // Every generation field the server documents, and the setting it comes
  316. // from. Generated alongside the schema above so the two cannot drift.
  317. const GENERATION_OPTIONS = [
  318. { setting: "batchCount", server: "batch_count" },
  319. { setting: "cacheMode", server: "cache_mode" },
  320. { setting: "cfgScale", server: "cfg_scale" },
  321. { setting: "clipSkip", server: "clip_skip" },
  322. { setting: "controlImageBase64", server: "control_image_base64" },
  323. { setting: "controlStrength", server: "control_strength" },
  324. { setting: "customSigmas", server: "custom_sigmas" },
  325. { setting: "distilledGuidance", server: "distilled_guidance" },
  326. { setting: "easycacheEnd", server: "easycache_end" },
  327. { setting: "easycacheStart", server: "easycache_start" },
  328. { setting: "easycacheThreshold", server: "easycache_threshold" },
  329. { setting: "eta", server: "eta" },
  330. { setting: "expandPrompt", server: "expand_prompt" },
  331. { setting: "flowShift", server: "flow_shift" },
  332. { setting: "height", server: "height" },
  333. { setting: "negativePrompt", server: "negative_prompt" },
  334. { setting: "pmIdEmbedPath", server: "pm_id_embed_path" },
  335. { setting: "pmIdImages", server: "pm_id_images" },
  336. { setting: "pmStyleStrength", server: "pm_style_strength" },
  337. { setting: "prompt", server: "prompt" },
  338. { setting: "refImageArgs", server: "ref_image_args" },
  339. { setting: "refImages", server: "ref_images" },
  340. { setting: "sampler", server: "sampler" },
  341. { setting: "scheduler", server: "scheduler" },
  342. { setting: "seed", server: "seed" },
  343. { setting: "shiftedTimestep", server: "shifted_timestep" },
  344. { setting: "skipLayers", server: "skip_layers" },
  345. { setting: "slgEnd", server: "slg_end" },
  346. { setting: "slgScale", server: "slg_scale" },
  347. { setting: "slgStart", server: "slg_start" },
  348. { setting: "spectrumFlexWindow", server: "spectrum_flex_window" },
  349. { setting: "spectrumLam", server: "spectrum_lam" },
  350. { setting: "spectrumM", server: "spectrum_m" },
  351. { setting: "spectrumStopPercent", server: "spectrum_stop_percent" },
  352. { setting: "spectrumW", server: "spectrum_w" },
  353. { setting: "spectrumWarmupSteps", server: "spectrum_warmup_steps" },
  354. { setting: "spectrumWindowSize", server: "spectrum_window_size" },
  355. { setting: "steps", server: "steps" },
  356. { setting: "upscale", server: "upscale" },
  357. { setting: "upscaleAutoUnload", server: "upscale_auto_unload" },
  358. { setting: "upscaleRepeats", server: "upscale_repeats" },
  359. { setting: "vaeTileOverlap", server: "vae_tile_overlap" },
  360. { setting: "vaeTileSizeX", server: "vae_tile_size_x" },
  361. { setting: "vaeTileSizeY", server: "vae_tile_size_y" },
  362. { setting: "vaeTiling", server: "vae_tiling" },
  363. { setting: "width", server: "width" }
  364. ];
  365. async function execute(config, input, context) {
  366. const server = normalizeServer(config.serverUrl);
  367. const timeout = config.timeout || 30000;
  368. const credential = readCredential(config.credentialId);
  369. const token = login(server, credential, timeout);
  370. const body = {};
  371. // Built from the table above rather than field by field, so a setting the
  372. // server documents cannot be quietly missing from the request. A setting
  373. // left empty is left out of the body entirely: the server fills an absent
  374. // field from the loaded model's architecture preset, and sending an empty
  375. // box as 0 would override that preset with nonsense. The test is emptiness,
  376. // never truthiness - seed 0 and clip_skip 0 are legitimate values.
  377. for (let i = 0; i < GENERATION_OPTIONS.length; i++) {
  378. const option = GENERATION_OPTIONS[i];
  379. const value = config[option.setting];
  380. if (Array.isArray(value)) {
  381. if (value.length > 0) {
  382. body[option.server] = value;
  383. }
  384. } else {
  385. putIfSet(body, option.server, value);
  386. }
  387. }
  388. putIfSet(body, 'title', config.title);
  389. if (!body.prompt) {
  390. throw new Error('SD.cpp: txt2img needs a prompt');
  391. }
  392. // Anything else the API accepts, passed through, so a new server field does
  393. // not need a node change to be reachable.
  394. const extra = config.extraOptions;
  395. if (extra && typeof extra === 'object') {
  396. const keys = Object.keys(extra);
  397. for (let i = 0; i < keys.length; i++) {
  398. putIfSet(body, keys[i], extra[keys[i]]);
  399. }
  400. }
  401. const queued = call({
  402. method: 'POST',
  403. url: server + '/txt2img',
  404. headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  405. body: JSON.stringify(body),
  406. timeout: timeout,
  407. what: 'queueing the txt2img job'
  408. });
  409. if (!queued.job_id) {
  410. throw new Error('SD.cpp: the job was accepted but no job id came back');
  411. }
  412. smartbotic.log.info('SD.cpp: queued txt2img job ' + queued.job_id);
  413. return {
  414. jobId: queued.job_id,
  415. status: queued.status || 'pending',
  416. position: queued.position !== undefined ? queued.position : -1,
  417. request: body
  418. };
  419. }
  420. module.exports = { configSchema, inputSchema, outputSchema, execute };