Spaces:
Sleeping
Sleeping
File size: 1,859 Bytes
0d6660e 263efa8 0d6660e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | const BACKEND_URL = (process.env.NEXT_PUBLIC_BACKEND_URL || "http://localhost:8000").replace(/\/$/, "")
function apiUrl(path: string) {
return `${BACKEND_URL}${path}`
}
export async function wakeBackend() {
const response = await fetch(apiUrl("/wake"))
return response.json()
}
export async function getAvailableModels() {
const response = await fetch(apiUrl("/available-models"))
return response.json()
}
export async function startSimulation(modelNames: string[]) {
const response = await fetch(apiUrl("/start-simulation"), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model_names: modelNames,
scenario: "fire",
map_width: 1200,
map_height: 800,
}),
})
if (!response.ok) throw new Error("Failed to start simulation")
return response.json()
}
export async function placeVolcano(simulationId: string, x: number, y: number) {
const response = await fetch(apiUrl("/place-fire"), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ simulation_id: simulationId, x, y }),
})
if (!response.ok) {
const details = await response.text()
throw new Error(`Failed to place fire (${response.status}): ${details}`)
}
return response.json()
}
export function createSimulationSocket(
simulationId: string,
onMessage: (msg: any) => void,
onError: () => void
) {
const wsBase = BACKEND_URL.replace(/^http:/, "ws:").replace(/^https:/, "wss:")
const wsUrl = `${wsBase}/ws/${simulationId}`
const ws = new WebSocket(wsUrl)
ws.onmessage = (event) => {
try {
const msg = JSON.parse(event.data)
onMessage(msg)
} catch (e) {
console.error("Failed to parse message:", e)
}
}
ws.onerror = () => onError()
ws.onclose = () => onError()
return ws
}
|