import { app } from "../../../scripts/app.js"; import { api } from "../../scripts/api.js"; class KayPromptExecution { constructor(id) { this.promptApi = null; this.executedNodeIds = []; this.totalNodes = 0; this.currentlyExecuting = null; this.errorDetails = null; this.id = id; } setPrompt(prompt) { this.promptApi = prompt.output; this.totalNodes = Object.keys(this.promptApi).length; } getApiNode(nodeId) { return this.promptApi?.[String(nodeId)] || null; } getNodeLabel(nodeId) { const apiNode = this.getApiNode(nodeId); let label = apiNode?._meta?.title || apiNode?.class_type || undefined; if (!label) { const graphNode = window?.app?.graph?.getNodeById(Number(nodeId)); label = graphNode?.title || graphNode?.type || undefined; } return label || "Unknown"; } executing(nodeId, step, maxSteps) { if (nodeId == null) { this.currentlyExecuting = null; return; } if (this.currentlyExecuting?.nodeId !== nodeId) { if (this.currentlyExecuting != null && !this.executedNodeIds.includes(this.currentlyExecuting.nodeId)) { this.executedNodeIds.push(this.currentlyExecuting.nodeId); } this.currentlyExecuting = { nodeId, nodeLabel: this.getNodeLabel(nodeId), pass: 0 }; const apiNode = this.getApiNode(nodeId); if (apiNode?.class_type === "UltimateSDUpscale") { this.currentlyExecuting.pass--; this.currentlyExecuting.maxPasses = -1; } else if (apiNode?.class_type === "IterativeImageUpscale") { this.currentlyExecuting.maxPasses = apiNode?.inputs["steps"] ?? -1; } } if (step != null && maxSteps != null) { if (!this.currentlyExecuting.step || step < this.currentlyExecuting.step) { this.currentlyExecuting.pass++; } this.currentlyExecuting.step = step; this.currentlyExecuting.maxSteps = maxSteps; } } error(details) { this.errorDetails = details; } } const KayResourceMonitor = { toolbar: null, chartCanvas: null, dataDisplay: null, menuButton: null, workflowProgressEl: null, dataHistory: { cpu: [], ram: [], gpu: [], gpuMem: [], gpuTemp: [] }, displayHistory: { cpu: [], ram: [], gpu: [], gpuMem: [], gpuTemp: [] }, current: { cpu: 0, ram: 0, gpu: 0, gpuMem: 0, gpuTemp: 0, ramUsed: 0, ramTotal: 0, gpuMemUsed: 0, gpuMemTotal: 0 }, target: { cpu: 0, ram: 0, gpu: 0, gpuMem: 0, gpuTemp: 0, ramTotal: 0, ramUsed: 0, gpuMemTotal: 0, gpuMemUsed: 0, cpuName: "Unknown", gpuName: "Unknown" }, workflowProgress: { percentage: 0, nodePercentage: 0, currentNodePercentage: 0, startTime: null, totalDuration: null, currentNode: "Idle", currentNodeLabel: "Idle", easeOutStartTime: null, isInterrupted: false, isProgressUnknown: false, queueCount: 0, completionTime: null, resetAnimationStartTime: null, }, currentWorkflow: { percentage: 0 }, promptsMap: new Map(), currentExecution: null, lastQueueRemaining: 0, cpuCores: 4, dragState: { isDragging: false, offsetX: 0, offsetY: 0 }, resizeState: { isResizing: false, startX: 0, startY: 0, startWidth: 0, startHeight: 0 }, position: { left: 0, top: 0 }, size: { width: 250, height: 219 }, lastUpdate: 0, lastCurveUpdate: 0, lastDisplayUpdate: 0, maxPoints: 120, curveInterval: 0, displayInterval: 0, smoothFactor: 0.1, easeOutDuration: 500, minWidth: 150, minHeight: 90, maxHeight: 219, edgeThreshold: 10, resizeHandleSize: 10, colors: { cpu: 'rgba(253, 170, 4, 0.86)', ram: 'rgba(10, 124, 254, 0.86)', gpu: 'rgba(255, 38, 0, 0.86)', gpuMem: 'rgba(97, 208, 12, 0.86)', gpuTemp: 'rgba(167, 43, 255, 0.86)', header: 'rgba(128, 128, 128, 0.5)', workflow: 'rgba(208, 255, 0, 0.86)', }, styles: { toolbar: `position: fixed; display: flex; flex-direction: column; color: rgba(186, 186, 186, 0.8); padding: 10px; border-radius: 5px; z-index: 10000; user-select: none; pointer-events: none; max-height: 219px;`, header: `margin-bottom: 5px; font-weight: bold; font-size: 10px;`, headerText: `cursor: grab; pointer-events: auto; display: inline-block;`, canvas: `margin-top: 5px; margin-bottom: 10px; width: 100%; height: 100px; pointer-events: none;`, display: `display: flex; flex-direction: column; font-size: 10px; flex-grow: 1; overflow: hidden; pointer-events: none;`, resizeHandle: `position: absolute; bottom: 0; right: 0; width: 8px; height: 8px; border-bottom: 1px solid; border-right: 1px solid; cursor: se-resize; pointer-events: auto;`, dot: (color) => `display: inline-block; width: 3px; height: 3px; border-radius: 50%; background: ${color}; margin-right: 5px; flex-shrink: 0; transition: background 0.5s ease;`, bar: (width) => `display: inline-block; width: ${width}px; height: 3px; background: rgba(200, 200, 200, 0.3); margin-right: 5px; position: relative; flex-shrink: 0;`, fill: (color, percent) => `position: absolute; top: 0; left: 0; width: ${percent}%; height: 100%; background: ${color};`, row: `display: flex; align-items: center; height: 11px; line-height: 11px; white-space: nowrap;`, text: `overflow: hidden; text-overflow: ellipsis; font-size: 10px;` }, isVisible: true, animationFrameId: null, isEnabled: true, isInitialized: false, edgeFadeEnabled: true, // 硬编码菜单边界 menuOffsets: { left: 0, // 左侧菜单宽度 top: 0 // 顶部菜单高度 }, easeOutQuad(t) { return t * (2 - t); }, async init() { if (this.isInitialized) return; this.isInitialized = true; this.currentWorkflow.percentage = 0; this.workflowProgress.currentNodePercentage = 0; this.isEnabled = app.ui.settings.getSettingValue("KayTool.EnableResourceMonitor"); this.edgeFadeEnabled = app.ui.settings.getSettingValue("KayTool.ResourceMonitorEdgeFade"); this.loadVisibility(); this.startMonitoring(); this.bindEvents(); this.setupUI(); this.setupWorkflowListener(); this.setupWebSocketListener(); if (this.isEnabled && this.isVisible) { this.startAnimation(); } }, async loadSettings() { this.isEnabled = app.ui.settings.getSettingValue("KayTool.EnableResourceMonitor"); this.edgeFadeEnabled = app.ui.settings.getSettingValue("KayTool.ResourceMonitorEdgeFade"); }, loadVisibility() { const visible = localStorage.getItem('KayResourceMonitorVisible'); this.isVisible = visible === null ? true : visible === 'true'; }, saveVisibility() { localStorage.setItem('KayResourceMonitorVisible', this.isVisible.toString()); }, setupUI() { this.toolbar = document.createElement('div'); this.toolbar.id = 'kay-resource-monitor'; const opacity = app.ui.settings.getSettingValue("KayTool.ResourceMonitorBackgroundOpacity") / 100; Object.assign(this.toolbar.style, { cssText: `${this.styles.toolbar} width: ${this.size.width}px; height: ${this.size.height}px; left: ${this.position.left}px; top: ${this.position.top}px; display: ${this.isVisible && this.isEnabled ? 'flex' : 'none'}; background: rgba(0, 0, 0, ${opacity});` }); const header = document.createElement('div'); Object.assign(header.style, { cssText: this.styles.header }); const headerText = document.createElement('span'); Object.assign(headerText.style, { cssText: this.styles.headerText }); headerText.textContent = "𝙆 Resource Monitor"; header.appendChild(headerText); this.toolbar.appendChild(header); this.workflowProgressEl = document.createElement('div'); Object.assign(this.workflowProgressEl.style, { cssText: this.styles.row }); this.updateWorkflowProgress(); this.toolbar.appendChild(this.workflowProgressEl); this.chartCanvas = document.createElement('canvas'); Object.assign(this.chartCanvas.style, { cssText: this.styles.canvas }); this.chartCanvas.width = this.size.width - 20; this.chartCanvas.height = 100; this.toolbar.appendChild(this.chartCanvas); this.dataDisplay = document.createElement('div'); Object.assign(this.dataDisplay.style, { cssText: this.styles.display }); this.toolbar.appendChild(this.dataDisplay); this.resizeHandle = document.createElement('div'); Object.assign(this.resizeHandle.style, { cssText: `${this.styles.resizeHandle} border-color: ${this.colors.header};` }); this.toolbar.appendChild(this.resizeHandle); document.body.appendChild(this.toolbar); this.loadPosition(); this.loadSize(); this.onResize(); this.bindToolbarEvents(); if (this.isEnabled && this.isVisible) { this.startAnimation(); } }, setupWebSocketListener() { api.addEventListener("kaytool.resources", (event) => { const data = event.detail; this.updateTarget(data); this.updateDisplay(); this.drawCurves(); }); }, setupWorkflowListener() { const that = this; const queuePrompt = api.queuePrompt; api.queuePrompt = async function (num, prompt) { let response; try { response = await queuePrompt.apply(api, [...arguments]); } catch (e) { const promptExecution = that.getOrMakePrompt("error"); promptExecution.error({ exception_type: "Unknown." }); throw e; } const promptExecution = that.getOrMakePrompt(response.prompt_id); promptExecution.setPrompt(prompt); that.promptsMap.set(response.prompt_id, promptExecution); return response; }; api.addEventListener("status", (e) => { if (!e.detail?.exec_info) return; this.lastQueueRemaining = e.detail.exec_info.queue_remaining; this.dispatchProgressUpdate(); }); api.addEventListener("execution_start", (e) => { const prompt = this.getOrMakePrompt(e.detail.prompt_id); this.currentExecution = prompt; this.workflowProgress.startTime = performance.now(); this.workflowProgress.totalDuration = null; this.workflowProgress.percentage = 0; this.workflowProgress.nodePercentage = 0; this.workflowProgress.currentNodePercentage = 0; this.currentWorkflow.percentage = 0; this.workflowProgress.easeOutStartTime = null; this.workflowProgress.isInterrupted = false; this.workflowProgress.isProgressUnknown = false; this.dispatchProgressUpdate(); }); api.addEventListener("executing", (e) => { if (!this.currentExecution) { this.currentExecution = this.getOrMakePrompt("unknown"); } this.currentExecution.executing(e.detail); if (e.detail == null && !this.workflowProgress.isInterrupted) { if (this.workflowProgress.startTime) { this.workflowProgress.totalDuration = (performance.now() - this.workflowProgress.startTime) / 1000; } this.workflowProgress.easeOutStartTime = performance.now(); this.currentExecution = null; } this.dispatchProgressUpdate(); }); api.addEventListener("progress", (e) => { if (!this.currentExecution) { this.currentExecution = this.getOrMakePrompt(e.detail.prompt_id); } this.currentExecution.executing(e.detail.node, e.detail.value, e.detail.max); this.dispatchProgressUpdate(); }); api.addEventListener("execution_cached", (e) => { if (!this.currentExecution) { this.currentExecution = this.getOrMakePrompt(e.detail.prompt_id); } for (const cached of e.detail.nodes) { if (!this.currentExecution.executedNodeIds.includes(cached)) { this.currentExecution.executedNodeIds.push(cached); } } this.dispatchProgressUpdate(); }); api.addEventListener("execution_error", (e) => { if (!this.currentExecution) { this.currentExecution = this.getOrMakePrompt(e.detail.prompt_id); } this.currentExecution?.error(e.detail); this.dispatchProgressUpdate(); }); api.addEventListener("execution_interrupted", (e) => { if (this.currentExecution) { if (this.workflowProgress.startTime) { this.workflowProgress.totalDuration = (performance.now() - this.workflowProgress.startTime) / 1000; } this.workflowProgress.easeOutStartTime = performance.now(); this.workflowProgress.isInterrupted = true; this.currentExecution = null; this.dispatchProgressUpdate(); } }); }, getOrMakePrompt(id) { let prompt = this.promptsMap.get(id); if (!prompt) { prompt = new KayPromptExecution(id); this.promptsMap.set(id, prompt); } return prompt; }, dispatchProgressUpdate() { const prompt = this.currentExecution; this.workflowProgress.queueCount = this.lastQueueRemaining; if (prompt?.currentlyExecuting) { if (!prompt.promptApi || prompt.totalNodes <= 0) { this.workflowProgress.isProgressUnknown = true; this.workflowProgress.percentage = 0; this.workflowProgress.currentNodeLabel = prompt.currentlyExecuting.nodeLabel || "Unknown"; } else { this.workflowProgress.isProgressUnknown = false; const executed = prompt.executedNodeIds.length; const total = prompt.totalNodes || 1; const basePercentage = (executed / total) * 100; const current = prompt.currentlyExecuting; let nodeContribution = 0; this.workflowProgress.currentNodeLabel = current.nodeLabel || "Unknown"; if (current.step != null && current.maxSteps) { this.workflowProgress.nodePercentage = (current.step / current.maxSteps) * 100; nodeContribution = (1 / total) * (current.step / current.maxSteps) * 100; } else { this.workflowProgress.nodePercentage = 0; } this.workflowProgress.percentage = Math.min(basePercentage + nodeContribution, 100); } } else if (this.workflowProgress.totalDuration !== null) { this.workflowProgress.isProgressUnknown = false; if (this.workflowProgress.isInterrupted) { this.workflowProgress.currentNode = `${this.workflowProgress.totalDuration.toFixed(1)}s (Interrupted)`; this.workflowProgress.currentNodeLabel = "Interrupted"; } else { this.workflowProgress.percentage = 100; this.workflowProgress.currentNode = `${this.workflowProgress.totalDuration.toFixed(1)}s`; this.workflowProgress.currentNodeLabel = "Completed"; } this.workflowProgress.nodePercentage = 0; this.workflowProgress.completionTime = performance.now(); // 新增:设置完成时间 } else { this.workflowProgress.isProgressUnknown = false; this.workflowProgress.percentage = 0; this.workflowProgress.nodePercentage = 0; this.workflowProgress.currentNode = "Idle"; this.workflowProgress.currentNodeLabel = "Idle"; this.workflowProgress.completionTime = null; // 新增:闲置时清空完成时间 } }, updateWorkflowProgress() { if (!this.workflowProgressEl) return; const { percentage, currentNode, isProgressUnknown, queueCount } = this.workflowProgress; const { styles, colors } = this; const containerWidth = this.toolbar.clientWidth - 20; const barWidth = Math.min(200, Math.max(50, (containerWidth - 15) * 0.18)); let displayPercentage, barPercentage, displayText; if (currentNode === "Idle") { displayText = "Workflow: Idle"; barPercentage = 0; } else if (this.workflowProgress.resetAnimationStartTime !== null) { // 重置动画期间,使用当前动画的百分比 displayPercentage = `${Math.min(Math.max(this.currentWorkflow.percentage, 0), 100).toFixed(1)}%`; barPercentage = Math.min(Math.max(this.currentWorkflow.percentage, 0), 100); displayText = `Workflow: ${displayPercentage} - ${currentNode}`; } else { displayPercentage = isProgressUnknown ? "Loading" : `${Math.min(Math.max(this.currentWorkflow.percentage, 0), 100).toFixed(1)}%`; barPercentage = isProgressUnknown ? 0 : Math.min(Math.max(this.currentWorkflow.percentage, 0), 100); if (queueCount > 1) { displayPercentage += ` (${queueCount})`; } displayText = `Workflow: ${displayPercentage} - ${currentNode}`; } const dotColor = this.currentExecution?.currentlyExecuting ? 'rgba(208, 255, 0, 0.86)' : 'rgba(200, 200, 200, 0.3)'; if (!this.workflowProgressEl.querySelector('.workflow-dot')) { this.workflowProgressEl.innerHTML = ` ${displayText} `; } else { const dotElement = this.workflowProgressEl.querySelector('.workflow-dot'); if (dotElement) dotElement.style.background = dotColor; const barElement = this.workflowProgressEl.querySelector('.workflow-bar'); if (barElement) { barElement.style.width = `${barWidth}px`; const barFill = barElement.querySelector('span'); if (barFill) barFill.style.width = `${barPercentage}%`; } const textElement = this.workflowProgressEl.querySelector('span:nth-child(3)'); if (textElement) textElement.textContent = displayText; } }, show() { this.isVisible = true; if (this.toolbar && this.isEnabled) { this.toolbar.style.display = 'flex'; this.refreshCanvas(); this.startAnimation(); } this.saveVisibility(); }, hide() { this.isVisible = false; if (this.toolbar) { this.toolbar.style.display = 'none'; this.stopAnimation(); } this.saveVisibility(); }, updateEnabledState(enabled) { this.isEnabled = enabled; if (this.menuButton) this.menuButton.style.display = enabled ? '' : 'none'; if (this.toolbar) { this.toolbar.style.display = enabled && this.isVisible ? 'flex' : 'none'; if (enabled && this.isVisible) { this.refreshCanvas(); this.startAnimation(); } else { this.stopAnimation(); } } }, refreshCanvas() { if (this.chartCanvas) { const headerHeight = this.toolbar.firstChild.getBoundingClientRect().height + 10; const workflowHeight = this.workflowProgressEl.clientHeight; const dataHeight = this.dataDisplay.clientHeight; this.chartCanvas.width = this.toolbar.clientWidth - 20; this.chartCanvas.height = this.toolbar.clientHeight - headerHeight - workflowHeight - dataHeight - 10; } }, updateTarget(data) { this.target.cpu = data.cpu_percent || 0; this.cpuCores = data.cpu_count || 4; this.target.ram = data.ram_percent || 0; this.target.ramTotal = data.ram_total || 0; this.target.ramUsed = data.ram_used || 0; this.target.cpuName = data.cpu_name || "Unknown"; this.dataHistory.cpu.push(this.target.cpu); this.dataHistory.ram.push(this.target.ram); if (data.gpu && typeof data.gpu !== 'string' && data.gpu.length > 0) { this.target.gpu = data.gpu[0].load || 0; this.target.gpuMem = data.gpu[0].memory_percent || 0; this.target.gpuMemTotal = data.gpu[0].memory_total || 0; this.target.gpuMemUsed = data.gpu[0].memory_used || 0; this.target.gpuTemp = data.gpu[0].temperature || 0; this.target.gpuName = data.gpu[0].name || "Unknown"; this.dataHistory.gpu.push(this.target.gpu); this.dataHistory.gpuMem.push(this.target.gpuMem); this.dataHistory.gpuTemp.push(this.target.gpuTemp); } else { this.target.gpu = 0; this.target.gpuMem = 0; this.target.gpuMemTotal = 0; this.target.gpuMemUsed = 0; this.target.gpuTemp = 0; this.target.gpuName = "Unknown"; this.dataHistory.gpu.push(0); this.dataHistory.gpuMem.push(0); this.dataHistory.gpuTemp.push(0); } if (this.dataHistory.cpu.length > this.maxPoints) { this.dataHistory.cpu.shift(); this.dataHistory.ram.shift(); this.dataHistory.gpu.shift(); this.dataHistory.gpuMem.shift(); this.dataHistory.gpuTemp.shift(); } }, updateDisplay() { const { cpu, ram, gpu, gpuMem, gpuTemp, ramUsed, ramTotal, gpuMemUsed, gpuMemTotal } = this.current; const { styles, colors } = this; this.dataDisplay.innerHTML = [ this.renderRow('CPU', colors.cpu, cpu, `${cpu.toFixed(1)}% (${this.cpuCores} cores) - ${this.target.cpuName}`), this.renderRow('RAM', colors.ram, ram, `${ramUsed.toFixed(1)}/${ramTotal.toFixed(1)}GB (${ram.toFixed(1)}%)`), this.target.gpu || this.target.gpuMem ? [ this.renderRow('GPU', colors.gpu, gpu, `${gpu.toFixed(1)}% - ${this.target.gpuName}`), this.renderRow('VRAM', colors.gpuMem, gpuMem, `${gpuMemUsed.toFixed(1)}/${gpuMemTotal.toFixed(1)}GB (${gpuMem.toFixed(1)}%)`), this.renderRow('TEMP', colors.gpuTemp, gpuTemp, `${gpuTemp.toFixed(1)}°C (GPU)`) ].join('') : [ this.renderRow('GPU', colors.gpu, 0, 'N/A'), this.renderRow('VRAM', colors.gpuMem, 0, 'N/A'), this.renderRow('TEMP', colors.gpuTemp, 0, 'N/A (GPU)') ].join('') ].join(''); }, renderRow(label, color, value, text) { const { styles } = this; const containerWidth = this.toolbar.clientWidth - 20; const minBarWidth = 50; const maxBarWidth = 200; const fixedSpacing = 13; const availableWidth = containerWidth - fixedSpacing; const barWidth = Math.min(maxBarWidth, Math.max(minBarWidth, availableWidth * 0.18)); const textAvailableWidth = containerWidth - fixedSpacing - barWidth; const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); ctx.font = '10px sans-serif'; const textWidth = ctx.measureText(`${label}: ${text}`).width; let displayText = `${label}: ${text}`; if (textWidth > textAvailableWidth) { const ellipsisWidth = ctx.measureText('...').width; let truncatedText = `${label}: `; let remainingWidth = textAvailableWidth - ctx.measureText(truncatedText).width - ellipsisWidth; for (let i = 0; i < text.length; i++) { const charWidth = ctx.measureText(text[i]).width; if (remainingWidth < charWidth) { truncatedText += '...'; break; } truncatedText += text[i]; remainingWidth -= charWidth; } displayText = truncatedText; } return `