import { app } from "../../scripts/app.js" ;
import { api } from "../../scripts/api.js" ;
export function show _message ( msg ) {
app . ui . dialog . show ( msg ) ;
app . ui . dialog . element . style . zIndex = 10010 ;
}
export async function sleep ( ms ) {
return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
}
export function rebootAPI ( ) {
if ( confirm ( "Are you sure you'd like to reboot the server?" ) ) {
try {
api . fetchApi ( "/manager/reboot" ) ;
}
catch ( exception ) {
}
return true ;
}
return false ;
}
export async function install _checked _custom _node ( grid _rows , target _i , caller , mode ) {
if ( caller ) {
let failed = '' ;
caller . disableButtons ( ) ;
for ( let i in grid _rows ) {
if ( ! grid _rows [ i ] . checkbox . checked && i != target _i )
continue ;
var target ;
if ( grid _rows [ i ] . data . custom _node ) {
target = grid _rows [ i ] . data . custom _node ;
}
else {
target = grid _rows [ i ] . data ;
}
caller . startInstall ( target ) ;
try {
const response = await api . fetchApi ( ` /customnode/ ${ mode } ` , {
method : 'POST' ,
headers : { 'Content-Type' : 'application/json' } ,
body : JSON . stringify ( target )
} ) ;
if ( response . status == 403 ) {
show _message ( 'This action is not allowed with this security level configuration.' ) ;
caller . updateMessage ( '' ) ;
await caller . invalidateControl ( ) ;
return ;
}
if ( response . status == 404 ) {
show _message ( 'With the current security level configuration, only custom nodes from the <B>"default channel"</B> can be installed.' ) ;
caller . updateMessage ( '' ) ;
await caller . invalidateControl ( ) ;
return ;
}
if ( response . status == 400 ) {
show _message ( ` ${ mode } failed: ${ target . title } ` ) ;
continue ;
}
const status = await response . json ( ) ;
app . ui . dialog . close ( ) ;
target . installed = 'True' ;
continue ;
}
catch ( exception ) {
failed += ` <BR> ${ target . title } ` ;
}
}
if ( failed != '' ) {
show _message ( ` ${ mode } failed: ${ failed } ` ) ;
}
await caller . invalidateControl ( ) ;
caller . updateMessage ( "<BR>To apply the installed/updated/disabled/enabled custom node, please <button id='cm-reboot-button1' class='cm-small-button'>RESTART</button> ComfyUI. And refresh browser." , 'cm-reboot-button1' ) ;
}
} ;
export var manager _instance = null ;
export function setManagerInstance ( obj ) {
manager _instance = obj ;
}
function isValidURL ( url ) {
if ( url . includes ( '&' ) )
return false ;
const pattern = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/ ;
return pattern . test ( url ) ;
}
export async function install _pip ( packages ) {
if ( packages . includes ( '&' ) )
app . ui . dialog . show ( ` Invalid PIP package enumeration: ' ${ packages } ' ` ) ;
const res = await api . fetchApi ( "/customnode/install/pip" , {
method : "POST" ,
body : packages ,
} ) ;
if ( res . status == 403 ) {
show _message ( 'This action is not allowed with this security level configuration.' ) ;
return ;
}
if ( res . status == 200 ) {
show _message ( ` PIP package installation is processed.<br>To apply the pip packages, please click the <button id='cm-reboot-button3'><font size='3px'>RESTART</font></button> button in ComfyUI. ` ) ;
const rebootButton = document . getElementById ( 'cm-reboot-button3' ) ;
const self = this ;
rebootButton . addEventListener ( "click" , rebootAPI ) ;
}
else {
show _message ( ` Failed to install ' ${ packages } '<BR>See terminal log. ` ) ;
}
}
export async function install _via _git _url ( url , manager _dialog ) {
if ( ! url ) {
return ;
}
if ( ! isValidURL ( url ) ) {
show _message ( ` Invalid Git url ' ${ url } ' ` ) ;
return ;
}
show _message ( ` Wait...<BR><BR>Installing ' ${ url } ' ` ) ;
const res = await api . fetchApi ( "/customnode/install/git_url" , {
method : "POST" ,
body : url ,
} ) ;
if ( res . status == 403 ) {
show _message ( 'This action is not allowed with this security level configuration.' ) ;
return ;
}
if ( res . status == 200 ) {
show _message ( ` ' ${ url } ' is installed<BR>To apply the installed custom node, please <button id='cm-reboot-button4'><font size='3px'>RESTART</font></button> ComfyUI. ` ) ;
const rebootButton = document . getElementById ( 'cm-reboot-button4' ) ;
const self = this ;
rebootButton . addEventListener ( "click" ,
function ( ) {
if ( rebootAPI ( ) ) {
manager _dialog . close ( ) ;
}
} ) ;
}
else {
show _message ( ` Failed to install ' ${ url } '<BR>See terminal log. ` ) ;
}
}
export async function free _models ( ) {
let res = await api . fetchApi ( ` /free ` , {
method : 'POST' ,
headers : { 'Content-Type' : 'application/json' } ,
body : '{"unload_models": true}'
} ) ;
if ( res . status == 200 ) {
show _message ( 'Models have been unloaded.' )
}
else {
show _message ( 'Unloading of models failed.<BR><BR>Installed ComfyUI may be an outdated version.' )
}
}