const DAEMON_URL = 'http://127.0.0.1:12345'; export async function fetchInstances() { const res = await fetch(`${DAEMON_URL}/instances/list`); if (!res.ok) { throw new Error(`Daemon returned status code: ${res.status}`); } return await res.json(); } export async function toggleInstanceStatus(name, currentStatus) { const action = currentStatus === 'RUNNING' ? 'stop' : 'start'; const res = await fetch(`${DAEMON_URL}/instances/${action}?name=${encodeURIComponent(name)}`, { method: 'POST' }); if (!res.ok) { const errorText = await res.text(); throw new Error(errorText || `Action '${action}' failed`); } return true; } export async function sendServerCommand(name, commandString) { const res = await fetch(`${DAEMON_URL}/instances/command?name=${encodeURIComponent(name)}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ command: commandString }) }); if (!res.ok) { const errorText = await res.text(); throw new Error(errorText || "Failed to route command to target instance"); } return await res.text(); } export async function fetchServerLogs(name) { const res = await fetch(`${DAEMON_URL}/instances/logs?name=${encodeURIComponent(name)}`); if (!res.ok) { throw new Error("Failed to sync engine logs"); } return await res.json(); }