"use strict" // Opt in to a restricted variant of JavaScript const MAX_INIT_IMAGE_DIMENSION = 768 const MIN_GPUS_TO_SHOW_SELECTION = 2 const IMAGE_REGEX = new RegExp('data:image/[A-Za-z]+;base64') const htmlTaskMap = new WeakMap() let promptField = document.querySelector('#prompt') let promptsFromFileSelector = document.querySelector('#prompt_from_file') let promptsFromFileBtn = document.querySelector('#promptsFromFileBtn') let negativePromptField = document.querySelector('#negative_prompt') let numOutputsTotalField = document.querySelector('#num_outputs_total') let numOutputsParallelField = document.querySelector('#num_outputs_parallel') let numInferenceStepsField = document.querySelector('#num_inference_steps') let guidanceScaleSlider = document.querySelector('#guidance_scale_slider') let guidanceScaleField = document.querySelector('#guidance_scale') let outputQualitySlider = document.querySelector('#output_quality_slider') let outputQualityField = document.querySelector('#output_quality') let outputQualityRow = document.querySelector('#output_quality_row') let randomSeedField = document.querySelector("#random_seed") let seedField = document.querySelector('#seed') let widthField = document.querySelector('#width') let heightField = document.querySelector('#height') let initImageSelector = document.querySelector("#init_image") let initImagePreview = document.querySelector("#init_image_preview") let initImageSizeBox = document.querySelector("#init_image_size_box") let maskImageSelector = document.querySelector("#mask") let maskImagePreview = document.querySelector("#mask_preview") let applyColorCorrectionField = document.querySelector('#apply_color_correction') let colorCorrectionSetting = document.querySelector('#apply_color_correction_setting') let promptStrengthSlider = document.querySelector('#prompt_strength_slider') let promptStrengthField = document.querySelector('#prompt_strength') let samplerField = document.querySelector('#sampler_name') let samplerSelectionContainer = document.querySelector("#samplerSelection") let useFaceCorrectionField = document.querySelector("#use_face_correction") let gfpganModelField = document.querySelector("#gfpgan_model") let useUpscalingField = document.querySelector("#use_upscale") let upscaleModelField = document.querySelector("#upscale_model") let upscaleAmountField = document.querySelector("#upscale_amount") let stableDiffusionModelField = document.querySelector('#stable_diffusion_model') let vaeModelField = document.querySelector('#vae_model') let hypernetworkModelField = document.querySelector('#hypernetwork_model') let hypernetworkStrengthSlider = document.querySelector('#hypernetwork_strength_slider') let hypernetworkStrengthField = document.querySelector('#hypernetwork_strength') let outputFormatField = document.querySelector('#output_format') let showOnlyFilteredImageField = document.querySelector("#show_only_filtered_image") let updateBranchLabel = document.querySelector("#updateBranchLabel") let streamImageProgressField = document.querySelector("#stream_image_progress") let makeImageBtn = document.querySelector('#makeImage') let stopImageBtn = document.querySelector('#stopImage') let pauseBtn = document.querySelector('#pause') let resumeBtn = document.querySelector('#resume') let renderButtons = document.querySelector('#render-buttons') let imagesContainer = document.querySelector('#current-images') let initImagePreviewContainer = document.querySelector('#init_image_preview_container') let initImageClearBtn = document.querySelector('.init_image_clear') let promptStrengthContainer = document.querySelector('#prompt_strength_container') let initialText = document.querySelector("#initial-text") let previewTools = document.querySelector("#preview-tools") let clearAllPreviewsBtn = document.querySelector("#clear-all-previews") let maskSetting = document.querySelector('#enable_mask') const processOrder = document.querySelector('#process_order_toggle') let imagePreview = document.querySelector("#preview") let imagePreviewContent = document.querySelector("#preview-content") imagePreview.addEventListener('drop', function(ev) { const data = ev.dataTransfer?.getData("text/plain"); if (!data) { return } const movedTask = document.getElementById(data) if (!movedTask) { return } ev.preventDefault() let moveTarget = ev.target while (moveTarget && typeof moveTarget === 'object' && moveTarget.parentNode !== imagePreview) { moveTarget = moveTarget.parentNode } if (moveTarget === initialText || moveTarget === previewTools) { moveTarget = null } if (moveTarget === movedTask) { return } if (moveTarget) { const childs = Array.from(imagePreview.children) if (moveTarget.nextSibling && childs.indexOf(movedTask) < childs.indexOf(moveTarget)) { // Move after the target if lower than current position. moveTarget = moveTarget.nextSibling } } const newNode = imagePreview.insertBefore(movedTask, moveTarget || previewTools.nextSibling) if (newNode === movedTask) { return } imagePreview.removeChild(movedTask) const task = htmlTaskMap.get(movedTask) if (task) { htmlTaskMap.delete(movedTask) } if (task) { htmlTaskMap.set(newNode, task) } }) let showConfigToggle = document.querySelector('#configToggleBtn') // let configBox = document.querySelector('#config') // let outputMsg = document.querySelector('#outputMsg') let soundToggle = document.querySelector('#sound_toggle') let serverStatusColor = document.querySelector('#server-status-color') let serverStatusMsg = document.querySelector('#server-status-msg') function getLocalStorageBoolItem(key, fallback) { let item = localStorage.getItem(key) if (item === null) { return fallback } return (item === 'true' ? true : false) } function handleBoolSettingChange(key) { return function(e) { localStorage.setItem(key, e.target.checked.toString()) } } function handleStringSettingChange(key) { return function(e) { localStorage.setItem(key, e.target.value.toString()) } } function isSoundEnabled() { return getSetting("sound_toggle") } function getSavedDiskPath() { return getSetting("diskPath") } function setStatus(statusType, msg, msgType) { } function setServerStatus(event) { switch(event.type) { case 'online': serverStatusColor.style.color = 'green' serverStatusMsg.style.color = 'green' serverStatusMsg.innerText = 'Stable Diffusion is ' + event.message break case 'busy': serverStatusColor.style.color = 'rgb(200, 139, 0)' serverStatusMsg.style.color = 'rgb(200, 139, 0)' serverStatusMsg.innerText = 'Stable Diffusion is ' + event.message break case 'error': serverStatusColor.style.color = 'red' serverStatusMsg.style.color = 'red' serverStatusMsg.innerText = 'Stable Diffusion has stopped' break } if (SD.serverState.devices) { setDeviceInfo(SD.serverState.devices) } } // shiftOrConfirm(e, prompt, fn) // e : MouseEvent // prompt : Text to be shown as prompt. Should be a question to which "yes" is a good answer. // fn : function to be called if the user confirms the dialog or has the shift key pressed // // If the user had the shift key pressed while clicking, the function fn will be executed. // If the setting "confirm_dangerous_actions" in the system settings is disabled, the function // fn will be executed. // Otherwise, a confirmation dialog is shown. If the user confirms, the function fn will also // be executed. function shiftOrConfirm(e, prompt, fn) { e.stopPropagation() if (e.shiftKey || !confirmDangerousActionsField.checked) { fn(e) } else { $.confirm({ theme: 'modern', title: prompt, useBootstrap: false, animateFromElement: false, content: 'Tip: To skip this dialog, use shift-click or disable the "Confirm dangerous actions" setting in the Settings tab.', buttons: { yes: () => { fn(e) }, cancel: () => {} } }); } } function logMsg(msg, level, outputMsg) { if (outputMsg.hasChildNodes()) { outputMsg.appendChild(document.createElement('br')) } if (level === 'error') { outputMsg.innerHTML += 'Error: ' + msg + '' } else if (level === 'warn') { outputMsg.innerHTML += 'Warning: ' + msg + '' } else { outputMsg.innerText += msg } console.log(level, msg) } function logError(msg, res, outputMsg) { logMsg(msg, 'error', outputMsg) console.log('request error', res) setStatus('request', 'error', 'error') } function playSound() { const audio = new Audio('/media/ding.mp3') audio.volume = 0.2 var promise = audio.play() if (promise !== undefined) { promise.then(_ => {}).catch(error => { console.warn("browser blocked autoplay") }) } } function showImages(reqBody, res, outputContainer, livePreview) { let imageItemElements = outputContainer.querySelectorAll('.imgItem') if(typeof res != 'object') return res.output.reverse() res.output.forEach((result, index) => { const imageData = result?.data || result?.path + '?t=' + Date.now(), imageSeed = result?.seed, imagePrompt = reqBody.prompt, imageInferenceSteps = reqBody.num_inference_steps, imageGuidanceScale = reqBody.guidance_scale, imageWidth = reqBody.width, imageHeight = reqBody.height; if (!imageData.includes('/')) { // res contained no data for the image, stop execution setStatus('request', 'invalid image', 'error') return } let imageItemElem = (index < imageItemElements.length ? imageItemElements[index] : null) if(!imageItemElem) { imageItemElem = document.createElement('div') imageItemElem.className = 'imgItem' imageItemElem.innerHTML = `
' if (this.exception) { msg += `Error: ${this.exception.message}' logError(msg, event, outputMsg) } else { let msg = `Unexpected Read Error:
` } try { // 'Response': body stream already read msg += 'Read: ' + await event.response.text() } catch(e) { msg += 'Unexpected end of stream. ' } const bufferString = event.reader.bufferedString if (bufferString) { msg += 'Buffered data: ' + bufferString } msg += '
Error:${this.exception}` logError(msg, event, outputMsg) } break } } if ('update' in event) { const stepUpdate = event.update if (!('step' in stepUpdate)) { return } // task.instances can be a mix of different tasks with uneven number of steps (Render Vs Filter Tasks) const overallStepCount = task.instances.reduce( (sum, instance) => sum + (instance.isPending ? Math.max(0, instance.step || stepUpdate.step) / (instance.total_steps || stepUpdate.total_steps) : 1), 0 // Initial value ) * stepUpdate.total_steps // Scale to current number of steps. const totalSteps = task.instances.reduce( (sum, instance) => sum + (instance.total_steps || stepUpdate.total_steps), stepUpdate.total_steps * (batchCount - task.batchesDone) // Initial value at (unstarted task count * Nbr of steps) ) const percent = Math.min(100, 100 * (overallStepCount / totalSteps)).toFixed(0) const timeTaken = stepUpdate.step_time // sec const stepsRemaining = Math.max(0, totalSteps - overallStepCount) const timeRemaining = (timeTaken < 0 ? '' : millisecondsToStr(stepsRemaining * timeTaken * 1000)) outputMsg.innerHTML = `Batch ${task.batchesDone} of ${batchCount}. Generating image(s): ${percent}%. Time remaining (approx): ${timeRemaining}` outputMsg.style.display = 'block' progressBarInner.style.width = `${percent}%` if (stepUpdate.output) { showImages(reqBody, stepUpdate, outputContainer, true) } } } } function abortTask(task) { if (!task.isProcessing) { return false } task.isProcessing = false task.progressBar.classList.remove("active") task['taskStatusLabel'].style.display = 'none' task['stopTask'].innerHTML = ' Remove' if (!task.instances?.some((r) => r.isPending)) { return } task.instances.forEach((instance) => { try { instance.abort() } catch (e) { console.error(e) } }) } function onTaskErrorHandler(task, reqBody, instance, reason) { if (!task.isProcessing) { return } console.log('Render request %o, Instance: %o, Error: %s', reqBody, instance, reason) abortTask(task) const outputMsg = task['outputMsg'] logError('Stable Diffusion had an error. Please check the logs in the command-line window.
EventInfo: ${JSON.stringify(event, undefined, 4)}
' + reason.stack + '', task, outputMsg) setStatus('request', 'error', 'error') } function onTaskCompleted(task, reqBody, instance, outputContainer, stepUpdate) { if (typeof stepUpdate === 'object') { if (stepUpdate.status === 'succeeded') { showImages(reqBody, stepUpdate, outputContainer, false) } else { task.isProcessing = false const outputMsg = task['outputMsg'] let msg = '' if ('detail' in stepUpdate && typeof stepUpdate.detail === 'string' && stepUpdate.detail.length > 0) { msg = stepUpdate.detail if (msg.toLowerCase().includes('out of memory')) { msg += `
StepUpdate: ${JSON.stringify(stepUpdate, undefined, 4)}` } logError(msg, stepUpdate, outputMsg) } } if (task.isProcessing && task.batchesDone < task.batchCount) { task['taskStatusLabel'].innerText = "Pending" task['taskStatusLabel'].classList.add('waitingTaskLabel') task['taskStatusLabel'].classList.remove('activeTaskLabel') return } if ('instances' in task && task.instances.some((ins) => ins != instance && ins.isPending)) { return } task.isProcessing = false task['stopTask'].innerHTML = ' Remove' task['taskStatusLabel'].style.display = 'none' let time = millisecondsToStr( Date.now() - task.startTime ) if (task.batchesDone == task.batchCount) { if (!task.outputMsg.innerText.toLowerCase().includes('error')) { task.outputMsg.innerText = `Processed ${task.numOutputsTotal} images in ${time}` } task.progressBar.style.height = "0px" task.progressBar.style.border = "0px solid var(--background-color3)" task.progressBar.classList.remove("active") setStatus('request', 'done', 'success') } else { task.outputMsg.innerText += `. Task ended after ${time}` } if (randomSeedField.checked) { seedField.value = task.seed } if (SD.activeTasks.size > 0) { return } const uncompletedTasks = getUncompletedTaskEntries() if (uncompletedTasks && uncompletedTasks.length > 0) { return } if (pauseClient) { resumeBtn.click() } renderButtons.style.display = 'none' renameMakeImageButton() if (isSoundEnabled()) { playSound() } } async function onTaskStart(task) { if (!task.isProcessing || task.batchesDone >= task.batchCount) { return } if (typeof task.startTime !== 'number') { task.startTime = Date.now() } if (!('instances' in task)) { task['instances'] = [] } task['stopTask'].innerHTML = ' Stop' task['taskStatusLabel'].innerText = "Starting" task['taskStatusLabel'].classList.add('waitingTaskLabel') let newTaskReqBody = task.reqBody if (task.batchCount > 1) { // Each output render batch needs it's own task reqBody instance to avoid altering the other runs after they are completed. newTaskReqBody = Object.assign({}, task.reqBody) if (task.batchesDone == task.batchCount-1) { // Last batch of the task // If the number of parallel jobs is no factor of the total number of images, the last batch must create less than "parallel jobs count" images // E.g. with numOutputsTotal = 6 and num_outputs = 5, the last batch shall only generate 1 image. newTaskReqBody.num_outputs = task.numOutputsTotal - task.reqBody.num_outputs * (task.batchCount-1) } } const startSeed = task.seed || newTaskReqBody.seed const genSeeds = Boolean(typeof newTaskReqBody.seed !== 'number' || (newTaskReqBody.seed === task.seed && task.numOutputsTotal > 1)) if (genSeeds) { newTaskReqBody.seed = parseInt(startSeed) + (task.batchesDone * task.reqBody.num_outputs) } // Update the seed *before* starting the processing so it's retained if user stops the task if (randomSeedField.checked) { seedField.value = task.seed } const outputContainer = document.createElement('div') outputContainer.className = 'img-batch' task.outputContainer.insertBefore(outputContainer, task.outputContainer.firstChild) const eventInfo = {reqBody:newTaskReqBody} const callbacksPromises = PLUGINS['TASK_CREATE'].map((hook) => { if (typeof hook !== 'function') { console.error('The provided TASK_CREATE hook is not a function. Hook: %o', hook) return Promise.reject(new Error('hook is not a function.')) } try { return Promise.resolve(hook.call(task, eventInfo)) } catch (err) { console.error(err) return Promise.reject(err) } }) await Promise.allSettled(callbacksPromises) let instance = eventInfo.instance if (!instance) { const factory = PLUGINS.OUTPUTS_FORMATS.get(eventInfo.reqBody?.output_format || newTaskReqBody.output_format) if (factory) { instance = await Promise.resolve(factory(eventInfo.reqBody || newTaskReqBody)) } if (!instance) { console.error(`${factory ? "Factory " + String(factory) : 'No factory defined'} for output format ${eventInfo.reqBody?.output_format || newTaskReqBody.output_format}. Instance is ${instance || 'undefined'}. Using default renderer.`) instance = new SD.RenderTask(eventInfo.reqBody || newTaskReqBody) } } task['instances'].push(instance) task.batchesDone++ instance.enqueue(getTaskUpdater(task, newTaskReqBody, outputContainer)).then( (renderResult) => { onTaskCompleted(task, newTaskReqBody, instance, outputContainer, renderResult) }, (reason) => { onTaskErrorHandler(task, newTaskReqBody, instance, reason) } ) setStatus('request', 'fetching..') renderButtons.style.display = 'flex' renameMakeImageButton() previewTools.style.display = 'block' } /* Hover effect for the init image in the task list */ function createInitImageHover(taskEntry) { var $tooltip = $( taskEntry.querySelector('.task-fs-initimage') ) var img = document.createElement('img') img.src = taskEntry.querySelector('div.task-initimg > img').src $tooltip.append(img) $tooltip.append(``) $tooltip.find('button').on('click', (e) => { e.stopPropagation() onUseAsInputClick(null,img) }) } let startX, startY; function onTaskEntryDragOver(event) { imagePreview.querySelectorAll(".imageTaskContainer").forEach(itc => { if(itc != event.target.closest(".imageTaskContainer")){ itc.classList.remove('dropTargetBefore','dropTargetAfter'); } }); if(event.target.closest(".imageTaskContainer")){ if(startX && startY){ if(event.target.closest(".imageTaskContainer").offsetTop > startY){ event.target.closest(".imageTaskContainer").classList.add('dropTargetAfter'); }else if(event.target.closest(".imageTaskContainer").offsetTop < startY){ event.target.closest(".imageTaskContainer").classList.add('dropTargetBefore'); }else if (event.target.closest(".imageTaskContainer").offsetLeft > startX){ event.target.closest(".imageTaskContainer").classList.add('dropTargetAfter'); }else if (event.target.closest(".imageTaskContainer").offsetLeft < startX){ event.target.closest(".imageTaskContainer").classList.add('dropTargetBefore'); } } } } function createTask(task) { let taskConfig = '' if (task.reqBody.init_image !== undefined) { let h = 80 let w = task.reqBody.width * h / task.reqBody.height >>0 taskConfig += `