'
async function startOmnitoolServer()
{
if (global.ALREADY_STARTING) return;
global.ALREADY_STARTING = true;
console.log('Starting Omnitool Server...');
return new Promise((resolve, reject) =>
{
const omnitoolStartProcess = spawn(OMNITOOL_INSTALL_SCRIPT);
omnitoolStartProcess.stdout.on('data', (data) =>
{
if (global.logs.length >= MAX_LOG_SIZE) {
global.logs.shift(); // Remove the oldest log entry
}
global.logs.push(data.toString());
console.log(`[log] ${data}`);
if (!global.OMNITOOL_RUNNING)
{
if (data.toString().includes(`Server has started and is ready to accept connections`))
{
console.log('Omnitool server started successfully');
setOmnitoolRunning(true);
}
}
});
omnitoolStartProcess.stderr.on('data', (data) =>
{
if (global.error_logs.length >= MAX_LOG_SIZE) {
global.error_logs.shift(); // Remove the oldest log entry
}
console.error(`[stderr] ${data}`);
global.error_logs.push(data.toString());
});
omnitoolStartProcess.on('close', (code) =>
{
const message = `Omnitool server process exited with code: ${code}`;
console.log(message);
global.logs.push(message);
global.ALREADY_STARTING = false;
if (code === 0)
{
//@ts-ignore
resolve();
} else
{
reject(`Omnitool server did not start properly.`);
}
});
});
}
function setOmnitoolRunning(set_running)
{
if (set_running === true)
{
if (global.OMNITOOL_READY === false)
{
global.OMNITOOL_READY = true;
console.log('Omnitool server is READY! ');
setTimeout(() =>
{
console.log('Omnitool server is RUNNING! ');
global.OMNITOOL_RUNNING = true;
}, DELAY_OMNITOOL_SET_TO_RUNNING); // Delay by 2 second
// start mirroring to /data when the server is running
// startMirrorToDataDir(); #DISABLED!
}
}
else
{
global.OMNITOOL_READY = false;
global.OMNITOOL_RUNNING = false;
}
}
// Function to check the status of the external service
async function checkOmnitoolStatus()
{
try
{
//@ts-ignore
const response = await axios.get('http://127.0.0.1:1688/api/v1/mercenaries/ping');
if (response.data && response.data.ping === 'pong' && Object.keys(response.data.payload).length === 0)
{
setOmnitoolRunning(true);
}
else
{
setOmnitoolRunning(false);
}
} catch (error)
{
setOmnitoolRunning(false);
}
}
async function handleGetStartOmnitoolServer(req, res)
{
console.log(`Omnitool Server:ALREADY_STARTING = ${global.ALREADY_STARTING}`);
if (global.ALREADY_STARTING)
{
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end("Omnitool server already starting");
return;
}
try
{
await startOmnitoolServer();
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end("Omnitool server started successfully");
}
catch (error)
{
console.error(error);
global.ALREADY_STARTING = false;
res.writeHead(500, { 'Content-Type': 'text/html' });
res.end(`Error starting Omnitool server: ${error}`);
}
}
async function handleGetOmnitoolLogs(req, res)
{
res.writeHead(200, { 'Content-Type': 'application/json' });
const reply = { logs: global.logs, error_logs: global.error_logs, ready: global.OMNITOOL_READY };
res.end(JSON.stringify(reply));
return;
}
async function handleGetBurstIframe(req, res)
{
req.session.isVisited = true;
const reply = { burst_iframe: true };
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(reply));
return;
}
async function proxyRequest(req, res)
{
console.log('Proxying request');
if (global.PROXY_STARTED) return;
global.PROXY_STARTED = true;
// Proxy logic...
const options = { hostname: CONTAINER_HOST, port: CONTAINER_PORT_OMNITOOL, path: req.url, method: req.method, headers: req.headers, };
const proxy = http.request(options, (proxyRes) =>
{
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res, { end: true });
});
req.pipe(proxy, { end: true });
return;
}
async function handleGetRoot(req, res)
{
setGlobals(req);
if (req.query.isVisited) req.session.isVisited = true;
console.log(`req.session.isVisited = ${req.session.isVisited}`);
console.log(`global.OMNITOOL_RUNNING = ${global.OMNITOOL_RUNNING}`);
if (!req.session.isVisited || !global.OMNITOOL_RUNNING)
{
await checkOmnitoolStatus();
}
if (global.CONNECTED_TO_MASTER && !req.session.roayl_override)
{
let page_message ="";
page_message += '
This is the REFERENCE Omnitool Space on Huggingface.
';
page_message += '';
page_message += '
To access Omnitool, you must first DUPLICATE this SPACE to make it your own.
';
page_message += '
Duplicating the Space will help secure your keys, recipes and outputs.
';
page_message += '
Please set your Space as PRIVATE!
';
page_message += '
DUPLICATING and using your own SPACE is FREE if you use the lowest tier of CPU. No GPU is required.
';
page_message += `
DUPLICATING this space is done by pressing this button: DUPLICATE SPACE
`;
page_message += "
However, you can try using the 20 Gig Storage option (~ 5 USD/month) to persist keys, recipes and outputs between server restarts. This is still a BETA feature!
";
page_message += '';
page_message += '
Some users have reported getting a 404 error when pressing the yellow START button after duplicating the space. If that happens to you follow the following instructions
';
page_message += '
- go to settings and set the space to PUBLIC
';
page_message += '
- confirm that the START button now works
';
page_message += '
- go to settings and set the Space back to PRIVATE