diff --git a/eval-clean.png b/eval-clean.png new file mode 100644 index 0000000..6ba7514 Binary files /dev/null and b/eval-clean.png differ diff --git a/eval-pill.png b/eval-pill.png new file mode 100644 index 0000000..6fc82b8 Binary files /dev/null and b/eval-pill.png differ diff --git a/eval-pill2.png b/eval-pill2.png new file mode 100644 index 0000000..a62a56f Binary files /dev/null and b/eval-pill2.png differ diff --git a/eval-v2.png b/eval-v2.png new file mode 100644 index 0000000..0164df2 Binary files /dev/null and b/eval-v2.png differ diff --git a/public/app.js b/public/app.js index a0949cf..d8f4c09 100644 --- a/public/app.js +++ b/public/app.js @@ -9,6 +9,7 @@ const STATE = { settings: null, bindings: null, // {white: {agent, model, ...}, black: {...}} viewPly: null, // null = live; otherwise integer ply (1-based) + evalByFen: new Map(), // fen → eval payload (cp, mate, best_uci, pv) — from white's POV }; window._STATE = STATE; // for debugging @@ -20,6 +21,27 @@ const cg = Chessground($("board"), { drawable: { enabled: true, visible: true }, }); +// ---------- responsive board sizing ---------- +function fitBoard() { + const row = $("board-row"); + const evalCol = $("eval-col"); + const board = $("board"); + if (!row || !board || !evalCol) return; + // 10px = gap between eval-col and board in #board-row (CSS). + const w = row.clientWidth - evalCol.offsetWidth - 10; + const h = row.clientHeight; + const side = Math.max(120, Math.floor(Math.min(w, h))); + board.style.width = side + "px"; + board.style.height = side + "px"; + evalCol.style.height = side + "px"; + // Let chessground recompute internal layout. + if (cg && cg.redrawAll) cg.redrawAll(); +} +const _ro = new ResizeObserver(fitBoard); +_ro.observe($("board-row")); +window.addEventListener("resize", fitBoard); +requestAnimationFrame(fitBoard); + // ---------- agent labels ---------- function agentOfSide(side) { return STATE.bindings?.[side]?.agent || "?"; @@ -31,27 +53,34 @@ function agentDisplay(side) { } // ---------- rendering ---------- +function currentFen() { + if (!STATE.game) return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + const moves = (STATE.game.history || []).filter(h => h.san); + if (STATE.viewPly === null) return STATE.game.fen; + const c = new Chess(); + for (let i = 0; i < STATE.viewPly; i++) c.move(moves[i].san); + return c.fen(); +} + function renderBoard() { if (!STATE.game) return; - let fen = STATE.game.fen; - let lastMove = undefined; const moves = (STATE.game.history || []).filter(h => h.san); - if (STATE.viewPly !== null && STATE.viewPly >= 0) { - const c = new Chess(); - for (let i = 0; i < STATE.viewPly; i++) c.move(moves[i].san); - fen = c.fen(); - if (STATE.viewPly > 0) { - const m = moves[STATE.viewPly - 1]; - const uci = m.uci || ""; - if (uci.length >= 4) lastMove = [uci.slice(0, 2), uci.slice(2, 4)]; - } - } else if (moves.length) { - const m = moves[moves.length - 1]; - const uci = m.uci || ""; + const fen = currentFen(); + let lastMove = undefined; + const idx = STATE.viewPly === null ? moves.length - 1 : STATE.viewPly - 1; + if (idx >= 0 && moves[idx]) { + const uci = moves[idx].uci || ""; if (uci.length >= 4) lastMove = [uci.slice(0, 2), uci.slice(2, 4)]; } - cg.set({ fen, lastMove }); + // Pass shapes via set() to atomically replace last move + arrow. + // Arrow comes from stored eval for this FEN, if any. + const e = STATE.evalByFen.get(fen); + const shapes = (e && e.best_uci && e.best_uci.length >= 4) + ? [{ orig: e.best_uci.slice(0, 2), dest: e.best_uci.slice(2, 4), brush: "green" }] + : []; + cg.set({ fen, lastMove, drawable: { autoShapes: shapes } }); $("live-btn").hidden = STATE.viewPly === null; + applyEvalForFen(fen); } function renderChat() { @@ -60,13 +89,15 @@ function renderChat() { const moves = (STATE.game?.history || []).filter(h => h.san); const counter = $("chat-counter"); counter.textContent = moves.length ? `${moves.length} moves` : ""; + const activePly = STATE.viewPly === null ? moves.length : STATE.viewPly; + let activeDiv = null; moves.forEach((h, i) => { const ply = i + 1; - const side = h.side; // "white" | "black" + const side = h.side; const div = document.createElement("div"); div.className = `bubble bubble-${side}`; - if (STATE.viewPly === ply) div.classList.add("active"); + if (ply === activePly) { div.classList.add("active"); activeDiv = div; } const moveNum = Math.floor(i / 2) + 1; const moveLabel = side === "white" ? `${moveNum}.` : `${moveNum}...`; const meta = document.createElement("div"); @@ -86,14 +117,21 @@ function renderChat() { list.appendChild(div); }); - // auto-scroll to bottom if at live - if (STATE.viewPly === null) list.scrollTop = list.scrollHeight; + // Scroll the active bubble into view (smooth at live tail, instant elsewhere). + if (activeDiv) { + requestAnimationFrame(() => activeDiv.scrollIntoView({ + behavior: "smooth", block: "nearest", + })); + } } function renderHistory() { const list = $("history-list"); list.innerHTML = ""; const moves = (STATE.game?.history || []).filter(h => h.san); + const activePly = STATE.viewPly === null ? moves.length : STATE.viewPly; + let activeLi = null; + let row = 0; for (let i = 0; i < moves.length; i += 2) { row++; @@ -104,18 +142,23 @@ function renderHistory() { const w = document.createElement("li"); w.textContent = moves[i]?.san || ""; - if (STATE.viewPly === i + 1) w.classList.add("active"); + if (i + 1 === activePly) { w.classList.add("active"); activeLi = w; } w.addEventListener("click", () => setView(i + 1)); list.appendChild(w); const b = document.createElement("li"); b.textContent = moves[i + 1]?.san || ""; if (moves[i + 1]) { - if (STATE.viewPly === i + 2) b.classList.add("active"); + if (i + 2 === activePly) { b.classList.add("active"); activeLi = b; } b.addEventListener("click", () => setView(i + 2)); } list.appendChild(b); } + if (activeLi) { + requestAnimationFrame(() => activeLi.scrollIntoView({ + behavior: "smooth", block: "nearest", + })); + } } function renderStatus() { @@ -129,32 +172,53 @@ function renderStatus() { } } -function applyEval(e) { - const cp = e.cp, mate = e.mate; - const col = $("eval-col"), bar = $("eval-fill"), txt = $("eval-text"); - // pct = % of the bar occupied by WHITE (bottom-anchored). - // 50 = even; >50 = white ahead; <50 = black ahead. +function storeEval(e) { + if (!e || !e.fen) return; + // Normalize cp/mate to WHITE's POV. Stockfish reports from side-to-move. + // If FEN says black to move, flip the sign. + const sideToMove = e.fen.split(" ")[1] || "w"; + const flip = sideToMove === "b"; + const norm = { + cp: e.cp == null ? null : (flip ? -e.cp : e.cp), + mate: e.mate == null ? null : (flip ? -e.mate : e.mate), + best_uci: e.best_uci || null, + pv: e.pv || [], + fen: e.fen, + }; + STATE.evalByFen.set(e.fen, norm); +} + +function applyEvalForFen(fen) { + const e = STATE.evalByFen.get(fen); + const col = $("eval-col"), bar = $("eval-fill"), txt = $("eval-text"), pv = $("sf-pv"); + if (!e) { + txt.textContent = "—"; + pv.textContent = ""; + bar.style.height = "50%"; + col.classList.remove("advantage-black"); + return; + } let pct = 50; - if (mate != null) { - pct = mate > 0 ? 100 : 0; - txt.textContent = (mate > 0 ? "+M" : "-M") + Math.abs(mate); - } else if (cp != null) { - // map cp to percentage: ±800cp = full bar. Capped. - pct = Math.max(2, Math.min(98, 50 + cp / 16)); - const v = (cp / 100); + if (e.mate != null) { + pct = e.mate > 0 ? 100 : 0; + txt.textContent = (e.mate > 0 ? "+M" : "-M") + Math.abs(e.mate); + } else if (e.cp != null) { + pct = Math.max(2, Math.min(98, 50 + e.cp / 16)); + const v = (e.cp / 100); txt.textContent = (v > 0 ? "+" : "") + v.toFixed(2); } else { txt.textContent = "—"; } bar.style.height = `${pct}%`; col.classList.toggle("advantage-black", pct < 50); - if (e.best_uci && e.best_uci.length >= 4) { - const from = e.best_uci.slice(0, 2), to = e.best_uci.slice(2, 4); - cg.setShapes([{ orig: from, dest: to, brush: "green" }]); - } - if (e.pv && e.pv.length) { - $("sf-pv").textContent = "pv: " + e.pv.slice(0, 6).join(" "); - } + col.classList.toggle("advantage-white", pct > 50); + pv.textContent = e.pv && e.pv.length ? "pv: " + e.pv.slice(0, 6).join(" ") : ""; +} + +function applyEval(e) { + storeEval(e); + // If we're currently looking at this fen, refresh the visuals. + if (currentFen() === e.fen) renderBoard(); } function setView(ply) { @@ -167,20 +231,15 @@ function goLive() { renderBoard(); renderChat(); renderHistory(); } -function log(line) { - const t = $("log-text"); - t.textContent += line + "\n"; - // trim to last 200 lines - const lines = t.textContent.split("\n"); - if (lines.length > 200) t.textContent = lines.slice(-200).join("\n"); - t.scrollTop = t.scrollHeight; -} +function log(_line) { /* log strip removed; keep no-op to preserve callsites */ } function applySnapshot(s) { STATE.game = s.game; STATE.settings = s.settings; STATE.bindings = s.bindings; STATE.viewPly = null; + STATE.evalByFen.clear(); + for (const e of (s.evals || [])) storeEval(e); renderBoard(); renderChat(); renderHistory(); renderStatus(); } diff --git a/public/index.html b/public/index.html index cb123ec..3955cc5 100644 --- a/public/index.html +++ b/public/index.html @@ -10,21 +10,14 @@
- - -
-
-
0.00
-
-
-
+
+
+
+
0.00
+
+
+
+
+ moves +
    +
    -
    diff --git a/public/style.css b/public/style.css index 4ccac49..cac260e 100644 --- a/public/style.css +++ b/public/style.css @@ -7,77 +7,67 @@ body { font-size: 13px; } +/* ============ layout: 2 columns ============ */ #layout { display: grid; - grid-template-columns: minmax(280px, 1.1fr) 28px minmax(420px, 700px) minmax(180px, 240px); - grid-template-rows: 1fr 110px; - grid-template-areas: - "chat eval board moves" - "log log log log"; + grid-template-columns: minmax(440px, min(900px, 60vw)) minmax(380px, 1fr); + grid-template-rows: 1fr; + grid-template-areas: "board chat"; height: 100vh; - gap: 8px; - padding: 8px; + width: 100vw; + gap: 20px; + padding: 14px; + overflow: hidden; } -#chat-col { grid-area: chat; min-width: 0; min-height: 0; } -#eval-col { grid-area: eval; min-width: 0; min-height: 0; } -#board-col { grid-area: board; min-width: 0; min-height: 0; } -#moves-col { grid-area: moves; min-width: 0; min-height: 0; } -#log-strip { grid-area: log; min-width: 0; min-height: 0; } +#board-col { grid-area: board; min-width: 0; min-height: 0; overflow: hidden; display: flex; flex-direction: column; gap: 10px; } +#chat-col { grid-area: chat; min-width: 0; min-height: 0; overflow: hidden; } -/* ----- columns shared header ----- */ -.col-header { - display: flex; justify-content: space-between; align-items: center; - padding: 4px 8px; opacity: 0.6; font-size: 11px; text-transform: uppercase; - letter-spacing: 0.08em; border-bottom: 1px solid #2a2a30; +/* ============ board area ============ */ +#board-row { + display: flex; + align-items: center; + justify-content: flex-start; + gap: 12px; + flex: 1 1 auto; + min-height: 0; + min-width: 0; } +#board { + /* size set imperatively by fitBoard() */ + background: #444; + border-radius: 4px; + overflow: hidden; + flex: 0 0 auto; +} +#board cg-container { width: 100% !important; height: 100% !important; } -/* ----- chat (left) ----- */ -#chat-col { display: flex; flex-direction: column; background: #15151a; border-radius: 6px; overflow: hidden; } -#chat-list { - flex: 1; overflow-y: auto; padding: 8px; - display: flex; flex-direction: column; gap: 6px; +#board-footer { + display: flex; gap: 10px; align-items: center; + padding: 4px 2px; + font-size: 12.5px; } -.bubble { - padding: 6px 9px 8px; - border-radius: 8px; - border: 1px solid transparent; - cursor: pointer; - line-height: 1.35; - word-wrap: break-word; - transition: outline-color 0.1s ease; - outline: 1px solid transparent; - outline-offset: 2px; -} -.bubble:hover { outline-color: #777; } -.bubble.active { outline-color: #c0a060; } -.bubble-white { background: #f0eee6; color: #16161a; align-self: flex-start; max-width: 92%; } -.bubble-black { background: #1f1e22; color: #e7e5e4; align-self: flex-end; max-width: 92%; border-color: #2a2a30; } -.bubble-meta { - font-size: 10px; opacity: 0.65; margin-bottom: 3px; - display: flex; justify-content: space-between; gap: 6px; align-items: center; -} -.bubble-san { font-weight: 700; opacity: 0.95; } -.bubble-body { font-size: 12.5px; } -.bubble-empty { font-style: italic; opacity: 0.55; } +#status-line { flex: 0 0 auto; opacity: 0.85; } +#sf-pv { flex: 1; opacity: 0.5; font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } -/* ----- eval bar (lichess-style two-bar) ----- */ +/* ============ eval bar ============ */ #eval-col { position: relative; - background: #2a2a30; /* the BLACK half — sits at the top */ + width: 36px; + flex: 0 0 36px; + background: #2a2a30; /* BLACK half on top */ border-radius: 4px; overflow: hidden; box-shadow: inset 0 0 0 1px #3a3a40; } -/* center reference line (50/50) */ -#eval-col::after { +#eval-col::after { /* 50/50 reference line */ content: ""; position: absolute; left: 0; right: 0; top: 50%; height: 1px; background: rgba(255,255,255,0.12); pointer-events: none; z-index: 2; } -#eval-fill { /* the WHITE half — anchored at bottom */ +#eval-fill { /* WHITE half from bottom */ position: absolute; left: 0; right: 0; bottom: 0; height: 50%; @@ -86,59 +76,86 @@ body { z-index: 1; } #eval-text { - position: absolute; left: 0; right: 0; bottom: 4px; - text-align: center; font-size: 10px; - color: #18181c; mix-blend-mode: difference; z-index: 3; - font-weight: 700; letter-spacing: 0.04em; + position: absolute; + left: 0; right: 0; + font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; + font-size: 10.5px; + font-weight: 700; + letter-spacing: -0.03em; + text-align: center; + font-variant-numeric: tabular-nums; + pointer-events: none; z-index: 3; + transition: color 0.2s, top 0.4s, bottom 0.4s; + /* default — slight white or even: text near bottom on white half, dark */ + bottom: 4px; top: auto; + color: #1a1a1a; } #eval-col.advantage-black #eval-text { - bottom: auto; top: 4px; /* flip text to top when black is ahead */ + bottom: auto; top: 4px; + color: #e7e5e4; } -/* ----- board ----- */ -#board-col { - display: flex; flex-direction: column; align-items: center; gap: 8px; - min-width: 0; min-height: 0; -} -#board { - width: min(100%, calc(100vh - 220px)); - aspect-ratio: 1 / 1; - max-width: 100%; - background: #444; - border-radius: 4px; +/* ============ floating moves panel (below board) ============ */ +#moves-floating { + display: flex; align-items: flex-start; gap: 8px; + background: #15151a; + border: 1px solid #2a2a30; + border-radius: 8px; + padding: 6px 10px; + max-height: 84px; overflow: hidden; + box-shadow: 0 2px 8px rgba(0,0,0,0.3); } -#board cg-container { width: 100% !important; height: 100% !important; } -#board-footer { - display: flex; gap: 8px; align-items: center; width: 100%; padding: 0 4px; - font-size: 12px; +.moves-label { + font-size: 10px; opacity: 0.45; text-transform: uppercase; + letter-spacing: 0.1em; padding-top: 2px; flex: 0 0 auto; } -#status-line { flex: 0 0 auto; opacity: 0.85; } -#sf-pv { flex: 1; opacity: 0.55; font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } - -/* ----- moves history ----- */ -#moves-col { display: flex; flex-direction: column; background: #15151a; border-radius: 6px; overflow: hidden; } #history-list { - flex: 1; overflow-y: auto; padding: 4px 8px; margin: 0; - list-style: none; - display: grid; grid-template-columns: 28px 1fr 1fr; row-gap: 1px; - align-content: start; + list-style: none; padding: 0; margin: 0; + display: flex; flex-wrap: wrap; gap: 2px 8px; + font-size: 12.5px; overflow-y: auto; flex: 1; + font-variant-numeric: tabular-nums; } -#history-list li { padding: 2px 6px; border-radius: 3px; cursor: pointer; font-size: 12.5px; } -#history-list li.ply-num { opacity: 0.4; cursor: default; } +#history-list li { padding: 1px 4px; border-radius: 3px; cursor: pointer; } +#history-list li.ply-num { opacity: 0.4; cursor: default; padding-right: 2px; } #history-list li:not(.ply-num):hover { background: #25252b; } #history-list li.active { background: #3d342a; color: #f0d090; } -/* ----- log strip ----- */ -#log-strip { - background: #0c0c0e; border: 1px solid #1f1f24; border-radius: 6px; - overflow: auto; padding: 4px 8px; +/* ============ chat panel (right, full height) ============ */ +#chat-col { display: flex; flex-direction: column; background: #15151a; border-radius: 8px; overflow: hidden; border: 1px solid #2a2a30; } +.col-header { + display: flex; justify-content: space-between; align-items: center; + padding: 6px 10px; opacity: 0.6; font-size: 10px; text-transform: uppercase; + letter-spacing: 0.1em; border-bottom: 1px solid #2a2a30; } -#log-text { - font-size: 11px; margin: 0; color: #888; white-space: pre-wrap; +#chat-list { + flex: 1; overflow-y: auto; padding: 10px; + display: flex; flex-direction: column; gap: 8px; } +.bubble { + padding: 7px 10px 9px; + border-radius: 9px; + border: 1px solid transparent; + cursor: pointer; + line-height: 1.4; + word-wrap: break-word; + transition: outline-color 0.1s ease; + outline: 1px solid transparent; + outline-offset: 2px; +} +.bubble:hover { outline-color: #555; } +.bubble.active { outline-color: #c0a060; } +.bubble-white { background: #f0eee6; color: #16161a; align-self: flex-start; max-width: 92%; } +.bubble-black { background: #1f1e22; color: #e7e5e4; align-self: flex-end; max-width: 92%; border-color: #2a2a30; } +.bubble-meta { + font-size: 10px; opacity: 0.65; margin-bottom: 3px; + display: flex; justify-content: space-between; gap: 6px; align-items: center; +} +.bubble-san { font-weight: 700; opacity: 0.95; } +.bubble-body { font-size: 13px; } +.bubble-empty { font-style: italic; opacity: 0.5; } -/* ----- buttons + dialog ----- */ +/* ============ buttons + dialog ============ */ button { background: #25252b; color: #e7e5e4; border: 1px solid #34343a; padding: 4px 10px; cursor: pointer; border-radius: 4px; font-size: 12px; diff --git a/src/ai_chess/app.py b/src/ai_chess/app.py index 3581028..3c1eb59 100644 --- a/src/ai_chess/app.py +++ b/src/ai_chess/app.py @@ -54,6 +54,9 @@ def build_app(state: state_mod.AppState) -> FastAPI: try: while True: ev = await q.get() + if ev.get("type") == "new_game": + state.eval_cache.clear() + continue if ev.get("type") != "move" or not ev.get("fen"): continue if not eng_ok: @@ -62,9 +65,10 @@ def build_app(state: state_mod.AppState) -> FastAPI: res = await eng.analyze( ev["fen"], depth=state.settings.stockfish_depth, ) - await state.events.publish( - {"type": "eval", **res, "fen": ev["fen"]}, - ) + payload = {"type": "eval", **res, "fen": ev["fen"]} + # Cache so future WS clients can backfill on connect. + state.eval_cache[ev["fen"]] = {**res, "fen": ev["fen"]} + await state.events.publish(payload) except Exception as e: print(f"[eval_bridge] analyze failed: {e}", flush=True) finally: @@ -88,13 +92,16 @@ def build_app(state: state_mod.AppState) -> FastAPI: @app.get("/api/state") async def get_state() -> dict[str, Any]: + evals = list(state.eval_cache.values()) if state.game is None: return {"game": None, "settings": state.settings.model_dump(), - "bindings": {s.value: b.to_dict() for s, b in state.bindings.items()}} + "bindings": {s.value: b.to_dict() for s, b in state.bindings.items()}, + "evals": evals} return { "game": state.game.get_state(), "settings": state.settings.model_dump(), "bindings": {s.value: b.to_dict() for s, b in state.bindings.items()}, + "evals": evals, } @app.get("/api/games") diff --git a/src/ai_chess/state.py b/src/ai_chess/state.py index 86b42cb..e51d2f4 100644 --- a/src/ai_chess/state.py +++ b/src/ai_chess/state.py @@ -41,6 +41,8 @@ class AppState: self.bindings: dict[Side, AgentBinding] = {} self.first_move_sent: dict[Side, bool] = {Side.WHITE: False, Side.BLACK: False} self.nudge_count: dict[Side, int] = {Side.WHITE: 0, Side.BLACK: 0} + # fen → {cp, mate, best_uci, pv, fen} — populated by eval_bridge per move + self.eval_cache: dict[str, dict] = {} state = AppState() diff --git a/src/ai_chess/tmux.py b/src/ai_chess/tmux.py index 8b36304..9ca34fb 100644 --- a/src/ai_chess/tmux.py +++ b/src/ai_chess/tmux.py @@ -88,9 +88,19 @@ class TmuxSession: except OSError: pass - async def submit(self, pane: int, text: str) -> None: - """Paste text and press Enter to submit.""" + async def submit(self, pane: int, text: str, *, settle_ms: int = 600) -> None: + """Paste text and press Enter to submit. + + `settle_ms` is a short wait between paste-buffer and Enter so that the + target TUI's paste-burst detector finishes collecting the pasted block + before we send the submit keystroke. codex shows the paste as + '[Pasted Content N chars]' and only treats a *subsequent* Enter as + submit; without the delay the Enter gets folded into the paste burst + and never submits. + """ await self.paste_text(pane, text) + if settle_ms > 0: + await asyncio.sleep(settle_ms / 1000) await self.send_keys(pane, ["Enter"]) async def capture_pane(self, pane: int, *, escape: bool = True, history: int = 0) -> str: diff --git a/ui-final.png b/ui-final.png new file mode 100644 index 0000000..f72d9ae Binary files /dev/null and b/ui-final.png differ diff --git a/ui-v4.png b/ui-v4.png new file mode 100644 index 0000000..3b611ac Binary files /dev/null and b/ui-v4.png differ diff --git a/ui-v5.png b/ui-v5.png new file mode 100644 index 0000000..1e16f23 Binary files /dev/null and b/ui-v5.png differ diff --git a/ui-v6.png b/ui-v6.png new file mode 100644 index 0000000..50d43ff Binary files /dev/null and b/ui-v6.png differ