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html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>SimpleTool Pong - Real-time AI Demo</title>
|
| 7 |
+
<style>
|
| 8 |
+
*{margin:0;padding:0;box-sizing:border-box}
|
| 9 |
+
body{font-family:'Segoe UI',system-ui,sans-serif;background:linear-gradient(135deg,#0a0a0a,#1a1a2e);min-height:100vh;color:#fff;display:flex;flex-direction:column;align-items:center;padding:20px}
|
| 10 |
+
h1{font-size:2rem;margin-bottom:5px;background:linear-gradient(90deg,#00d4ff,#7b2cbf);-webkit-background-clip:text;-webkit-text-fill-color:transparent}
|
| 11 |
+
.subtitle{color:#666;font-size:.85rem;margin-bottom:20px}
|
| 12 |
+
.game-wrap{display:flex;gap:20px;flex-wrap:wrap;justify-content:center}
|
| 13 |
+
#canvas{background:#000;border-radius:8px;box-shadow:0 0 60px rgba(0,212,255,.3);border:2px solid #333}
|
| 14 |
+
.panel{background:rgba(255,255,255,.03);border-radius:12px;padding:16px;width:320px;border:1px solid #222}
|
| 15 |
+
.panel h3{font-size:.85rem;color:#00d4ff;margin-bottom:10px;text-transform:uppercase;letter-spacing:1px}
|
| 16 |
+
.score-board{display:flex;justify-content:space-around;margin-bottom:20px;background:#000;border-radius:8px;padding:15px}
|
| 17 |
+
.score-item{text-align:center}
|
| 18 |
+
.score-label{font-size:.7rem;color:#666;margin-bottom:5px}
|
| 19 |
+
.score-value{font-size:2.5rem;font-weight:bold}
|
| 20 |
+
.ai-score{color:#00d4ff}
|
| 21 |
+
.human-score{color:#ff6b6b}
|
| 22 |
+
.stats{display:grid;grid-template-columns:1fr 1fr;gap:8px;margin-bottom:15px}
|
| 23 |
+
.stat{background:#000;padding:12px;border-radius:6px;text-align:center}
|
| 24 |
+
.stat-val{font-size:1.3rem;font-weight:bold;color:#0f0}
|
| 25 |
+
.stat-label{font-size:.65rem;color:#555;margin-top:2px}
|
| 26 |
+
.info-box{font-family:Consolas,monospace;font-size:.7rem;background:#000;padding:10px;border-radius:6px;max-height:100px;overflow-y:auto;margin-bottom:12px;border:1px solid #222;color:#0f0;word-break:break-all}
|
| 27 |
+
.controls{margin-top:15px;display:flex;gap:10px}
|
| 28 |
+
.btn{flex:1;padding:12px;border:none;border-radius:6px;cursor:pointer;font-weight:600;transition:all .2s}
|
| 29 |
+
.btn-start{background:linear-gradient(90deg,#00d4ff,#7b2cbf);color:#fff}
|
| 30 |
+
.btn-reset{background:#222;color:#888;border:1px solid #444}
|
| 31 |
+
.btn:hover{transform:translateY(-2px)}
|
| 32 |
+
.status{text-align:center;padding:8px;border-radius:4px;font-size:.8rem;margin-bottom:10px}
|
| 33 |
+
.status.ok{background:rgba(0,255,0,.1);color:#0f0}
|
| 34 |
+
.status.err{background:rgba(255,0,0,.1);color:#f44}
|
| 35 |
+
.status.wait{background:rgba(255,255,0,.1);color:#ff0}
|
| 36 |
+
.server-input{display:flex;gap:8px;margin-bottom:15px}
|
| 37 |
+
.server-input input{flex:1;padding:8px;background:#111;border:1px solid #333;color:#fff;border-radius:4px;font-size:.75rem}
|
| 38 |
+
.legend{font-size:.7rem;color:#444;text-align:center;margin-top:10px}
|
| 39 |
+
.decision{background:#111;padding:8px;border-radius:4px;margin-bottom:10px;font-size:.8rem;border-left:3px solid #00d4ff}
|
| 40 |
+
</style>
|
| 41 |
+
</head>
|
| 42 |
+
<body>
|
| 43 |
+
<h1>🏓 SimpleTool Pong</h1>
|
| 44 |
+
<p class="subtitle">Real-time AI vs Human • Multi-Head Parallel Decoding</p>
|
| 45 |
+
<div class="game-wrap">
|
| 46 |
+
<div>
|
| 47 |
+
<canvas id="canvas" width="600" height="400"></canvas>
|
| 48 |
+
<p class="legend">⬆⬇ Arrow Keys or W/S to move your paddle (right side)</p>
|
| 49 |
+
</div>
|
| 50 |
+
<div class="panel">
|
| 51 |
+
<div class="score-board">
|
| 52 |
+
<div class="score-item"><div class="score-label">🤖 SimpleTool AI</div><div class="score-value ai-score" id="ai-score">0</div></div>
|
| 53 |
+
<div class="score-item"><div class="score-label">👤 Human</div><div class="score-value human-score" id="human-score">0</div></div>
|
| 54 |
+
</div>
|
| 55 |
+
<div class="server-input">
|
| 56 |
+
<input id="server-url" value="http://localhost:8899" placeholder="SimpleTool Server URL">
|
| 57 |
+
<button class="btn btn-reset" onclick="checkServer()" style="flex:0;padding:8px 12px">Test</button>
|
| 58 |
+
</div>
|
| 59 |
+
<div class="status wait" id="status">⏳ Click START to begin</div>
|
| 60 |
+
<div class="stats">
|
| 61 |
+
<div class="stat"><div class="stat-val" id="latency">--</div><div class="stat-label">Latency (ms)</div></div>
|
| 62 |
+
<div class="stat"><div class="stat-val" id="fps">60</div><div class="stat-label">Game FPS</div></div>
|
| 63 |
+
<div class="stat"><div class="stat-val" id="calls">0</div><div class="stat-label">API Calls</div></div>
|
| 64 |
+
<div class="stat"><div class="stat-val" id="avg-lat">--</div><div class="stat-label">Avg Latency</div></div>
|
| 65 |
+
</div>
|
| 66 |
+
<h3>🎯 AI Decision</h3>
|
| 67 |
+
<div class="decision" id="decision-box">Waiting...</div>
|
| 68 |
+
<h3>📡 Environment</h3>
|
| 69 |
+
<div class="info-box" id="env-box">Waiting...</div>
|
| 70 |
+
<h3>📜 History (last 6)</h3>
|
| 71 |
+
<div class="info-box" id="hist-box">[]</div>
|
| 72 |
+
<h3>📤 Raw Response</h3>
|
| 73 |
+
<div class="info-box" id="heads-box" style="max-height:120px">Waiting...</div>
|
| 74 |
+
<div class="controls">
|
| 75 |
+
<button class="btn btn-start" id="start-btn">▶ START</button>
|
| 76 |
+
<button class="btn btn-reset" id="reset-btn">↺ RESET</button>
|
| 77 |
+
</div>
|
| 78 |
+
</div>
|
| 79 |
+
</div>
|
| 80 |
+
<script>
|
| 81 |
+
const $ = id => document.getElementById(id);
|
| 82 |
+
const canvas = $('canvas'), ctx = canvas.getContext('2d');
|
| 83 |
+
const W = 600, H = 400;
|
| 84 |
+
const log = (msg, type='info') => console.log(`[Pong][${type}] ${msg}`);
|
| 85 |
+
|
| 86 |
+
let running = false, aiScore = 0, humanScore = 0;
|
| 87 |
+
let ball = {x: W/2, y: H/2, vx: 5, vy: 3, r: 8};
|
| 88 |
+
let aiPaddle = {x: 20, y: H/2, w: 10, h: 70, speed: 6};
|
| 89 |
+
let humanPaddle = {x: W-30, y: H/2, w: 10, h: 70, speed: 8};
|
| 90 |
+
let keys = {up: false, down: false};
|
| 91 |
+
let history = [], apiCalls = 0, totalLatency = 0, latencies = [];
|
| 92 |
+
let lastAiMove = 'stay', aiPending = false, aiInterval = null;
|
| 93 |
+
|
| 94 |
+
const TOOLS = [{
|
| 95 |
+
type: "function",
|
| 96 |
+
function: {
|
| 97 |
+
name: "move",
|
| 98 |
+
description: "Move paddle vertically to intercept ball",
|
| 99 |
+
parameters: {
|
| 100 |
+
type: "object",
|
| 101 |
+
properties: {
|
| 102 |
+
direction: {type: "string", enum: ["up", "down", "stay"], description: "up=decrease Y, down=increase Y, stay=hold position"}
|
| 103 |
+
},
|
| 104 |
+
required: ["direction"]
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
}];
|
| 108 |
+
|
| 109 |
+
document.addEventListener('keydown', e => {
|
| 110 |
+
if (e.key === 'ArrowUp' || e.key === 'w' || e.key === 'W') keys.up = true;
|
| 111 |
+
if (e.key === 'ArrowDown' || e.key === 's' || e.key === 'S') keys.down = true;
|
| 112 |
+
});
|
| 113 |
+
document.addEventListener('keyup', e => {
|
| 114 |
+
if (e.key === 'ArrowUp' || e.key === 'w' || e.key === 'W') keys.up = false;
|
| 115 |
+
if (e.key === 'ArrowDown' || e.key === 's' || e.key === 'S') keys.down = false;
|
| 116 |
+
});
|
| 117 |
+
|
| 118 |
+
function buildEnv() {
|
| 119 |
+
const ballY = Math.round(ball.y), paddleY = Math.round(aiPaddle.y);
|
| 120 |
+
const diff = ballY - paddleY;
|
| 121 |
+
return [
|
| 122 |
+
`ball_y=${ballY}`,
|
| 123 |
+
`paddle_y=${paddleY}`,
|
| 124 |
+
`diff=${diff}`,
|
| 125 |
+
`ball_moving_${ball.vx < 0 ? 'towards' : 'away'}`,
|
| 126 |
+
`ball_vy=${ball.vy > 0 ? 'down' : 'up'}`
|
| 127 |
+
];
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
function buildQuery() {
|
| 131 |
+
const ballY = Math.round(ball.y), paddleY = Math.round(aiPaddle.y);
|
| 132 |
+
const diff = ballY - paddleY;
|
| 133 |
+
const approaching = ball.vx < 0;
|
| 134 |
+
let instruction;
|
| 135 |
+
if (!approaching) {
|
| 136 |
+
instruction = 'Ball moving away. Stay or prepare.';
|
| 137 |
+
} else if (diff > 20) {
|
| 138 |
+
instruction = `Ball is ${diff}px BELOW paddle. Move DOWN.`;
|
| 139 |
+
} else if (diff < -20) {
|
| 140 |
+
instruction = `Ball is ${-diff}px ABOVE paddle. Move UP.`;
|
| 141 |
+
} else {
|
| 142 |
+
instruction = 'Ball aligned. STAY.';
|
| 143 |
+
}
|
| 144 |
+
return `Pong AI. ${instruction} Choose: up/down/stay`;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
function updateUI() {
|
| 148 |
+
$('env-box').textContent = buildEnv().join(' | ');
|
| 149 |
+
$('hist-box').textContent = `[${history.slice(-6).join(', ')}]`;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
async function checkServer() {
|
| 153 |
+
const url = $('server-url').value.replace(/\/$/, '');
|
| 154 |
+
try {
|
| 155 |
+
const r = await fetch(`${url}/health`, {signal: AbortSignal.timeout(3000)});
|
| 156 |
+
const d = await r.json();
|
| 157 |
+
if (d.loaded || d.status === 'ok') {
|
| 158 |
+
$('status').className = 'status ok';
|
| 159 |
+
$('status').textContent = '🟢 Connected';
|
| 160 |
+
log('Server connected', 'success');
|
| 161 |
+
return true;
|
| 162 |
+
}
|
| 163 |
+
} catch(e) { log(`Connection failed: ${e.message}`, 'error'); }
|
| 164 |
+
$('status').className = 'status err';
|
| 165 |
+
$('status').textContent = '🔴 Connection failed';
|
| 166 |
+
return false;
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
async function callAI() {
|
| 170 |
+
if (aiPending || !running) return;
|
| 171 |
+
aiPending = true;
|
| 172 |
+
const url = $('server-url').value.replace(/\/$/, '');
|
| 173 |
+
const env = buildEnv(), query = buildQuery();
|
| 174 |
+
log(`Query: ${query}`);
|
| 175 |
+
try {
|
| 176 |
+
const t0 = performance.now();
|
| 177 |
+
const r = await fetch(`${url}/v1/function_call`, {
|
| 178 |
+
method: 'POST',
|
| 179 |
+
headers: {'Content-Type': 'application/json'},
|
| 180 |
+
body: JSON.stringify({
|
| 181 |
+
messages: [{role: 'user', content: query}],
|
| 182 |
+
tools: TOOLS,
|
| 183 |
+
environment: env,
|
| 184 |
+
history: history.slice(-6)
|
| 185 |
+
})
|
| 186 |
+
});
|
| 187 |
+
const d = await r.json();
|
| 188 |
+
const lat = performance.now() - t0;
|
| 189 |
+
apiCalls++; totalLatency += lat; latencies.push(lat);
|
| 190 |
+
if (latencies.length > 50) latencies.shift();
|
| 191 |
+
$('latency').textContent = lat.toFixed(0);
|
| 192 |
+
$('calls').textContent = apiCalls;
|
| 193 |
+
$('avg-lat').textContent = (totalLatency/apiCalls).toFixed(0);
|
| 194 |
+
$('status').className = 'status ok';
|
| 195 |
+
$('status').textContent = '🟢 Active';
|
| 196 |
+
|
| 197 |
+
// Display raw response
|
| 198 |
+
$('heads-box').innerHTML = `function: ${d.function || '-'}<br>args: ${JSON.stringify(d.args)}<br>heads: ${JSON.stringify(d.heads)}`;
|
| 199 |
+
|
| 200 |
+
// Safe extraction with type coercion
|
| 201 |
+
let direction = 'stay';
|
| 202 |
+
if (d.success && d.args && d.args.direction !== undefined) {
|
| 203 |
+
direction = String(d.args.direction).toLowerCase().trim();
|
| 204 |
+
} else if (d.heads && d.heads.arg1) {
|
| 205 |
+
direction = String(d.heads.arg1).toLowerCase().trim();
|
| 206 |
+
}
|
| 207 |
+
if (!['up', 'down', 'stay'].includes(direction)) {
|
| 208 |
+
log(`Invalid direction "${direction}", fallback to stay`, 'warn');
|
| 209 |
+
direction = 'stay';
|
| 210 |
+
}
|
| 211 |
+
lastAiMove = direction;
|
| 212 |
+
history.push(direction);
|
| 213 |
+
if (history.length > 20) history.shift();
|
| 214 |
+
const emoji = direction === 'up' ? '⬆️' : direction === 'down' ? '⬇️' : '⏸️';
|
| 215 |
+
$('decision-box').innerHTML = `${emoji} <strong>${direction.toUpperCase()}</strong> (${lat.toFixed(0)}ms)`;
|
| 216 |
+
log(`Decision: ${direction} in ${lat.toFixed(0)}ms`, 'success');
|
| 217 |
+
} catch(e) {
|
| 218 |
+
log(`API Error: ${e.message}`, 'error');
|
| 219 |
+
$('status').className = 'status err';
|
| 220 |
+
$('status').textContent = '🔴 Error - Fallback';
|
| 221 |
+
// Fallback
|
| 222 |
+
if (ball.vx < 0) {
|
| 223 |
+
const diff = ball.y - aiPaddle.y;
|
| 224 |
+
lastAiMove = diff > 15 ? 'down' : diff < -15 ? 'up' : 'stay';
|
| 225 |
+
} else lastAiMove = 'stay';
|
| 226 |
+
history.push(`fb:${lastAiMove}`);
|
| 227 |
+
}
|
| 228 |
+
aiPending = false;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
function applyAI() {
|
| 232 |
+
if (lastAiMove === 'up') aiPaddle.y -= aiPaddle.speed;
|
| 233 |
+
else if (lastAiMove === 'down') aiPaddle.y += aiPaddle.speed;
|
| 234 |
+
aiPaddle.y = Math.max(aiPaddle.h/2, Math.min(H - aiPaddle.h/2, aiPaddle.y));
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
function update() {
|
| 238 |
+
if (keys.up) humanPaddle.y -= humanPaddle.speed;
|
| 239 |
+
if (keys.down) humanPaddle.y += humanPaddle.speed;
|
| 240 |
+
humanPaddle.y = Math.max(humanPaddle.h/2, Math.min(H - humanPaddle.h/2, humanPaddle.y));
|
| 241 |
+
applyAI();
|
| 242 |
+
ball.x += ball.vx; ball.y += ball.vy;
|
| 243 |
+
if (ball.y - ball.r < 0 || ball.y + ball.r > H) {
|
| 244 |
+
ball.vy *= -1;
|
| 245 |
+
ball.y = ball.y < ball.r ? ball.r : H - ball.r;
|
| 246 |
+
}
|
| 247 |
+
if (ball.x - ball.r < aiPaddle.x + aiPaddle.w && ball.y > aiPaddle.y - aiPaddle.h/2 && ball.y < aiPaddle.y + aiPaddle.h/2 && ball.vx < 0) {
|
| 248 |
+
ball.vx = Math.abs(ball.vx) * 1.05;
|
| 249 |
+
ball.vy += (ball.y - aiPaddle.y) * 0.1;
|
| 250 |
+
ball.x = aiPaddle.x + aiPaddle.w + ball.r;
|
| 251 |
+
}
|
| 252 |
+
if (ball.x + ball.r > humanPaddle.x && ball.y > humanPaddle.y - humanPaddle.h/2 && ball.y < humanPaddle.y + humanPaddle.h/2 && ball.vx > 0) {
|
| 253 |
+
ball.vx = -Math.abs(ball.vx) * 1.05;
|
| 254 |
+
ball.vy += (ball.y - humanPaddle.y) * 0.1;
|
| 255 |
+
ball.x = humanPaddle.x - ball.r;
|
| 256 |
+
}
|
| 257 |
+
ball.vx = Math.max(-12, Math.min(12, ball.vx));
|
| 258 |
+
ball.vy = Math.max(-8, Math.min(8, ball.vy));
|
| 259 |
+
if (ball.x < 0) { humanScore++; $('human-score').textContent = humanScore; resetBall(1); }
|
| 260 |
+
if (ball.x > W) { aiScore++; $('ai-score').textContent = aiScore; resetBall(-1); }
|
| 261 |
+
updateUI();
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
function resetBall(dir) {
|
| 265 |
+
ball.x = W/2; ball.y = H/2;
|
| 266 |
+
ball.vx = 5 * dir;
|
| 267 |
+
ball.vy = (Math.random() - 0.5) * 6;
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
function draw() {
|
| 271 |
+
ctx.fillStyle = '#000'; ctx.fillRect(0, 0, W, H);
|
| 272 |
+
ctx.setLineDash([10, 10]); ctx.strokeStyle = '#333'; ctx.lineWidth = 2;
|
| 273 |
+
ctx.beginPath(); ctx.moveTo(W/2, 0); ctx.lineTo(W/2, H); ctx.stroke(); ctx.setLineDash([]);
|
| 274 |
+
ctx.fillStyle = '#00d4ff'; ctx.shadowColor = '#00d4ff'; ctx.shadowBlur = 20;
|
| 275 |
+
ctx.fillRect(aiPaddle.x, aiPaddle.y - aiPaddle.h/2, aiPaddle.w, aiPaddle.h);
|
| 276 |
+
ctx.fillStyle = '#ff6b6b'; ctx.shadowColor = '#ff6b6b';
|
| 277 |
+
ctx.fillRect(humanPaddle.x, humanPaddle.y - humanPaddle.h/2, humanPaddle.w, humanPaddle.h);
|
| 278 |
+
ctx.fillStyle = '#fff'; ctx.shadowColor = '#fff'; ctx.shadowBlur = 15;
|
| 279 |
+
ctx.beginPath(); ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI * 2); ctx.fill();
|
| 280 |
+
ctx.shadowBlur = 0;
|
| 281 |
+
ctx.font = '14px monospace'; ctx.fillStyle = '#00d4ff'; ctx.fillText('AI', 50, 30);
|
| 282 |
+
ctx.fillStyle = '#ff6b6b'; ctx.fillText('YOU', W - 70, 30);
|
| 283 |
+
const arrow = lastAiMove === 'up' ? '↑' : lastAiMove === 'down' ? '↓' : '•';
|
| 284 |
+
ctx.fillStyle = '#00d4ff'; ctx.font = '12px monospace'; ctx.fillText(arrow, aiPaddle.x + 25, aiPaddle.y + 4);
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
function gameLoop() {
|
| 288 |
+
if (!running) return;
|
| 289 |
+
update(); draw();
|
| 290 |
+
requestAnimationFrame(gameLoop);
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
$('start-btn').onclick = async () => {
|
| 294 |
+
if (!running) {
|
| 295 |
+
await checkServer();
|
| 296 |
+
running = true;
|
| 297 |
+
$('start-btn').textContent = '⏸ PAUSE';
|
| 298 |
+
log('Game started');
|
| 299 |
+
gameLoop();
|
| 300 |
+
aiInterval = setInterval(callAI, 100);
|
| 301 |
+
} else {
|
| 302 |
+
running = false;
|
| 303 |
+
$('start-btn').textContent = '▶ START';
|
| 304 |
+
clearInterval(aiInterval);
|
| 305 |
+
log('Game paused');
|
| 306 |
+
}
|
| 307 |
+
};
|
| 308 |
+
|
| 309 |
+
$('reset-btn').onclick = () => {
|
| 310 |
+
running = false; clearInterval(aiInterval);
|
| 311 |
+
aiScore = humanScore = apiCalls = totalLatency = 0;
|
| 312 |
+
history = []; latencies = []; lastAiMove = 'stay';
|
| 313 |
+
$('ai-score').textContent = '0'; $('human-score').textContent = '0';
|
| 314 |
+
$('latency').textContent = '--'; $('calls').textContent = '0'; $('avg-lat').textContent = '--';
|
| 315 |
+
$('decision-box').textContent = 'Waiting...'; $('start-btn').textContent = '▶ START';
|
| 316 |
+
resetBall(Math.random() > 0.5 ? 1 : -1);
|
| 317 |
+
aiPaddle.y = humanPaddle.y = H/2;
|
| 318 |
+
draw(); log('Game reset');
|
| 319 |
+
};
|
| 320 |
+
|
| 321 |
+
draw(); checkServer();
|
| 322 |
+
</script>
|
| 323 |
+
</body>
|
| 324 |
+
</html>
|