-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
256 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Sotopia Minimal</title> | ||
<style> | ||
body { | ||
padding: 20px; | ||
} | ||
pre { | ||
background: #f5f5f5; | ||
padding: 10px; | ||
} | ||
.error { | ||
color: red; | ||
} | ||
button { | ||
margin-right: 10px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div> | ||
<h3>Scenario (select one):</h3> | ||
<select id="scenarioSelect"> | ||
<option value="">Select scenario...</option> | ||
</select> | ||
</div> | ||
|
||
<div> | ||
<h3>Agents (select exactly 2):</h3> | ||
<div id="agentsList"></div> | ||
</div> | ||
|
||
<div> | ||
<button id="startBtn" disabled>Start</button> | ||
<button id="stopBtn" disabled>Stop</button> | ||
</div> | ||
|
||
<h3>Selected Data:</h3> | ||
<pre id="selectedData"></pre> | ||
|
||
<h3>Simulation Messages:</h3> | ||
<pre id="messages"></pre> | ||
|
||
<script> | ||
const scenarioSelect = document.getElementById("scenarioSelect"); | ||
const agentsList = document.getElementById("agentsList"); | ||
const startBtn = document.getElementById("startBtn"); | ||
const stopBtn = document.getElementById("stopBtn"); | ||
const selectedData = document.getElementById("selectedData"); | ||
const messages = document.getElementById("messages"); | ||
|
||
let scenarios = []; | ||
let agents = []; | ||
let selectedAgents = []; | ||
let activeSocket = null; | ||
|
||
function updateSelectedDisplay() { | ||
const selectedScenario = scenarios.find( | ||
(s) => s.pk === scenarioSelect.value, | ||
); | ||
const selectedAgentData = agents.filter((a) => | ||
selectedAgents.includes(a.pk), | ||
); | ||
|
||
selectedData.textContent = JSON.stringify( | ||
{ | ||
selected_scenario: selectedScenario, | ||
selected_agents: selectedAgentData, | ||
}, | ||
null, | ||
2, | ||
); | ||
|
||
startBtn.disabled = !selectedScenario || selectedAgents.length !== 2; | ||
} | ||
|
||
async function cleanupWebSocket() { | ||
if (activeSocket) { | ||
activeSocket.close(); | ||
activeSocket = null; | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
} | ||
} | ||
|
||
async function initialize() { | ||
try { | ||
const [scenariosRes, agentsRes] = await Promise.all([ | ||
fetch("http://127.0.0.1:8800/scenarios"), | ||
fetch("http://127.0.0.1:8800/agents"), | ||
]); | ||
|
||
scenarios = await scenariosRes.json(); | ||
agents = await agentsRes.json(); | ||
|
||
scenarioSelect.innerHTML = ` | ||
<option value="">Select scenario...</option> | ||
${scenarios | ||
.map((s) => `<option value="${s.pk}">${s.pk}</option>`) | ||
.join("")} | ||
`; | ||
|
||
agentsList.innerHTML = agents | ||
.map( | ||
(a) => ` | ||
<label style="display: block; margin: 5px;"> | ||
<input type="checkbox" value="${a.pk}"> | ||
${a.pk} | ||
</label> | ||
`, | ||
) | ||
.join(""); | ||
|
||
agentsList | ||
.querySelectorAll('input[type="checkbox"]') | ||
.forEach((checkbox) => { | ||
checkbox.addEventListener("change", (e) => { | ||
if (e.target.checked) { | ||
if (selectedAgents.length < 2) { | ||
selectedAgents.push(e.target.value); | ||
} else { | ||
e.target.checked = false; | ||
alert("You can only select 2 agents"); | ||
} | ||
} else { | ||
selectedAgents = selectedAgents.filter( | ||
(id) => id !== e.target.value, | ||
); | ||
} | ||
updateSelectedDisplay(); | ||
}); | ||
}); | ||
} catch (error) { | ||
selectedData.textContent = "Error loading data: " + error.message; | ||
} | ||
} | ||
|
||
scenarioSelect.addEventListener("change", updateSelectedDisplay); | ||
|
||
stopBtn.addEventListener("click", async () => { | ||
if (!activeSocket || activeSocket.readyState !== WebSocket.OPEN) return; | ||
|
||
stopBtn.disabled = true; | ||
const stopMessage = { | ||
type: "FINISH_SIM", | ||
data: "", | ||
}; | ||
activeSocket.send(JSON.stringify(stopMessage)); | ||
messages.textContent += `Sent: ${JSON.stringify( | ||
stopMessage, | ||
null, | ||
2, | ||
)}\n\n`; | ||
|
||
startBtn.disabled = false; | ||
}); | ||
|
||
startBtn.addEventListener("click", async () => { | ||
// await cleanupWebSocket(); | ||
|
||
messages.textContent = "Starting simulation...\n"; | ||
|
||
activeSocket = new WebSocket( | ||
"ws://127.0.0.1:8800/ws/simulation?token=demo-token", | ||
); | ||
startBtn.disabled = true; | ||
|
||
activeSocket.onopen = () => { | ||
stopBtn.disabled = false; | ||
const startMessage = { | ||
type: "START_SIM", | ||
data: { | ||
env_id: scenarioSelect.value, | ||
agent_ids: selectedAgents, | ||
}, | ||
}; | ||
activeSocket.send(JSON.stringify(startMessage)); | ||
messages.textContent += `Sent: ${JSON.stringify( | ||
startMessage, | ||
null, | ||
2, | ||
)}\n\n`; | ||
}; | ||
|
||
activeSocket.onmessage = (event) => { | ||
const data = JSON.parse(event.data); | ||
messages.textContent += JSON.stringify(data, null, 2) + "\n\n"; | ||
messages.scrollTop = messages.scrollHeight; | ||
|
||
if (data.type === "END_SIM" || data.type === "ERROR") { | ||
stopBtn.disabled = true; | ||
startBtn.disabled = false; | ||
// cleanupWebSocket(); | ||
} | ||
}; | ||
|
||
activeSocket.onerror = async (error) => { | ||
messages.textContent += "Error: " + error + "\n"; | ||
stopBtn.disabled = true; | ||
startBtn.disabled = false; | ||
// await cleanupWebSocket(); | ||
}; | ||
|
||
activeSocket.onclose = async () => { | ||
stopBtn.disabled = true; | ||
startBtn.disabled = false; | ||
// await cleanupWebSocket(); | ||
}; | ||
}); | ||
|
||
initialize(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters