Spaces:
Sleeping
Sleeping
File size: 9,842 Bytes
734b153 8114240 734b153 8114240 734b153 8114240 734b153 | 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Agent Language Environment</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: #f5f5f7;
color: #1a1a1a;
min-height: 100vh;
padding: 24px;
}
.layout {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 16px;
max-width: 1200px;
margin: 0 auto;
}
.left-col, .right-col {
display: flex;
flex-direction: column;
gap: 16px;
}
.card {
background: #fff;
border: 1px solid #e0e0e0;
border-radius: 8px;
padding: 20px;
}
.card h2 {
font-size: 1rem;
font-weight: 700;
margin-bottom: 12px;
}
label {
display: block;
font-size: 0.875rem;
margin-bottom: 6px;
}
label .required {
color: #e53e3e;
}
textarea {
width: 100%;
min-height: 90px;
padding: 10px 12px;
border: 2px solid #4a90d9;
border-radius: 6px;
font-size: 0.875rem;
font-family: inherit;
resize: vertical;
outline: none;
color: #555;
}
textarea:focus {
border-color: #2563eb;
}
.field-hint {
font-size: 0.75rem;
color: #888;
margin-top: 4px;
font-style: italic;
}
.btn {
display: inline-block;
padding: 8px 20px;
border: none;
border-radius: 6px;
font-size: 0.875rem;
font-weight: 500;
cursor: pointer;
transition: opacity 0.15s;
}
.btn:hover { opacity: 0.85; }
.btn:active { opacity: 0.7; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: #4a7fd4; color: #fff; }
.btn-secondary { background: #5a6472; color: #fff; }
.btn-row {
display: flex;
gap: 10px;
}
.state-card .state-row {
font-size: 0.875rem;
color: #555;
margin-bottom: 6px;
}
.state-card .state-row strong {
color: #1a1a1a;
}
.obs-pre {
background: #f7f8fa;
border-radius: 4px;
padding: 12px;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
font-size: 0.8rem;
white-space: pre;
overflow-x: auto;
line-height: 1.6;
}
.history-empty {
font-size: 0.875rem;
color: #555;
}
.history-list {
display: flex;
flex-direction: column;
gap: 10px;
max-height: 400px;
overflow-y: auto;
}
.history-item {
background: #f7f8fa;
border-radius: 6px;
padding: 10px 12px;
font-size: 0.8rem;
}
.history-item .hi-step {
font-weight: 600;
margin-bottom: 4px;
color: #333;
}
.history-item .hi-action {
color: #555;
margin-bottom: 2px;
}
.history-item .hi-reward {
color: #2563eb;
font-size: 0.75rem;
}
.error-msg {
color: #c53030;
font-size: 0.8rem;
margin-top: 8px;
}
</style>
</head>
<body>
<div class="layout">
<!-- LEFT COLUMN -->
<div class="left-col">
<!-- Take Action Card -->
<div class="card">
<h2>Take Action</h2>
<label>Language Specification <span class="required">*</span>:</label>
<textarea id="messageInput" placeholder="Enter language specification..."></textarea>
<div class="field-hint">Language Specification</div>
<div id="stepError" class="error-msg" style="display:none;"></div>
<div style="margin-top:12px;">
<button class="btn btn-primary" id="stepBtn" onclick="doStep()">Step</button>
</div>
</div>
<!-- Env Controls -->
<div class="btn-row">
<button class="btn btn-secondary" id="resetBtn" onclick="doReset()">Reset Environment</button>
<button class="btn btn-secondary" id="stateBtn" onclick="doGetState()">Get State</button>
</div>
<!-- Current State Card -->
<div class="card state-card">
<h2>Current State</h2>
<div class="state-row">Status: <strong id="stateStatus">β</strong></div>
<div class="state-row">Episode ID: <strong id="stateEpisodeId">β</strong></div>
<div class="state-row">Step Count: <strong id="stateStepCount">β</strong></div>
<div id="stateError" class="error-msg" style="display:none;"></div>
</div>
</div>
<!-- RIGHT COLUMN -->
<div class="right-col">
<!-- Current Observation Card -->
<div class="card">
<h2>Current Observation</h2>
<div class="obs-pre" id="obsDisplay">β</div>
</div>
<!-- Action History Card -->
<div class="card">
<h2>Action History</h2>
<div id="historyContainer">
<div class="history-empty" id="historyEmpty">No actions taken yet</div>
<div class="history-list" id="historyList" style="display:none;"></div>
</div>
</div>
</div>
</div>
<script>
const BASE_URL = 'http://localhost:8000';
const actionHistory = [];
function setButtons(disabled) {
['stepBtn', 'resetBtn', 'stateBtn'].forEach(id => {
document.getElementById(id).disabled = disabled;
});
}
function showError(elemId, msg) {
const el = document.getElementById(elemId);
el.textContent = msg;
el.style.display = 'block';
}
function clearError(elemId) {
const el = document.getElementById(elemId);
el.textContent = '';
el.style.display = 'none';
}
function updateStateDisplay(data) {
document.getElementById('stateStatus').textContent = data.status ?? 'β';
document.getElementById('stateEpisodeId').textContent = data.episode_id ?? 'β';
document.getElementById('stateStepCount').textContent = data.step_count ?? 'β';
}
function updateObsDisplay(obs) {
// Show obs as pretty JSON, excluding internal fields if desired
const display = {};
for (const [k, v] of Object.entries(obs)) {
if (!['done', 'reward', 'metadata'].includes(k)) {
display[k] = v;
}
}
document.getElementById('obsDisplay').textContent = JSON.stringify(display, null, 2);
}
function addToHistory(stepNum, message, reward) {
actionHistory.push({ stepNum, message, reward });
renderHistory();
}
function renderHistory() {
const empty = document.getElementById('historyEmpty');
const list = document.getElementById('historyList');
if (actionHistory.length === 0) {
empty.style.display = '';
list.style.display = 'none';
return;
}
empty.style.display = 'none';
list.style.display = 'flex';
list.innerHTML = actionHistory.slice().reverse().map(item => `
<div class="history-item">
<div class="hi-step">Step ${item.stepNum}</div>
<div class="hi-action">Spec: "${item.message}"</div>
<div class="hi-reward">Reward: ${item.reward.toFixed(2)}</div>
</div>
`).join('');
}
async function doReset() {
clearError('stateError');
setButtons(true);
try {
const res = await fetch(`${BASE_URL}/reset`, { method: 'POST' });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
// data contains observation + state
if (data.observation) updateObsDisplay(data.observation);
if (data.state) updateStateDisplay({ ...data.state, status: 'Reset' });
// Clear history on reset
actionHistory.length = 0;
renderHistory();
} catch (e) {
showError('stateError', `Reset failed: ${e.message}`);
} finally {
setButtons(false);
}
}
async function doGetState() {
clearError('stateError');
setButtons(true);
try {
const res = await fetch(`${BASE_URL}/state`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
updateStateDisplay(data);
} catch (e) {
showError('stateError', `Get state failed: ${e.message}`);
} finally {
setButtons(false);
}
}
async function doStep() {
clearError('stepError');
const message = document.getElementById('messageInput').value.trim();
if (!message) {
showError('stepError', 'Language Specification is required.');
return;
}
setButtons(true);
try {
const res = await fetch(`${BASE_URL}/step`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: { language_specification: message } }),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
const obs = data.observation ?? data;
updateObsDisplay(obs);
if (data.state) {
updateStateDisplay({ ...data.state, status: 'Running' });
addToHistory(data.state.step_count, message, data.reward ?? 0);
} else {
addToHistory(actionHistory.length + 1, message, data.reward ?? 0);
}
document.getElementById('messageInput').value = '';
} catch (e) {
showError('stepError', `Step failed: ${e.message}`);
} finally {
setButtons(false);
}
}
// Load initial state on page load
(async () => {
try {
const res = await fetch(`${BASE_URL}/state`);
if (res.ok) {
const data = await res.json();
updateStateDisplay(data);
}
} catch (_) {}
})();
</script>
</body>
</html>
|