| let currentTokens = []; |
| let streamBuffer = ""; |
|
|
| export function appendToken(token) { |
| streamBuffer += token; |
| currentTokens.push(token); |
|
|
| const display = document.getElementById("stream-display"); |
| if (display) { |
| display.textContent = cleanVisibleStream(streamBuffer); |
| display.scrollTop = display.scrollHeight; |
| } |
| } |
|
|
| export function setStatus(text, type = "neutral") { |
| const el = document.getElementById("status-text"); |
| const phase = document.getElementById("stream-phase"); |
|
|
| if (el) { |
| el.textContent = text; |
| el.className = `status-${type}`; |
| } |
|
|
| if (phase) { |
| phase.textContent = text; |
| phase.className = `status-${type}`; |
| } |
| } |
|
|
| export function setVerifierStatus(verdict) { |
| const el = document.getElementById("verifier-status"); |
| if (!el) return; |
|
|
| const labels = { |
| IDLE: "Verifier idle", |
| PASS: "Verified", |
| FIX: "Fixed", |
| REWRITE: "Rewritten", |
| ERROR: "Verifier error", |
| CHECKING: "Verifying...", |
| }; |
| el.textContent = labels[verdict] || ""; |
| el.className = `verdict-${String(verdict || "idle").toLowerCase()}`; |
| } |
|
|
| export async function rollbackAndReplace(correctedCode, reason, verdict = "FIX") { |
| const display = document.getElementById("stream-display"); |
| if (!display) return; |
|
|
| display.classList.add("rollback-flash"); |
| setVerifierStatus(verdict); |
| setStatus(`Verifier corrected: ${reason}`, "warning"); |
|
|
| await sleep(450); |
| display.classList.remove("rollback-flash"); |
| display.textContent = ""; |
| streamBuffer = correctedCode; |
| currentTokens = []; |
|
|
| for (let i = 0; i < correctedCode.length; i += 1) { |
| display.textContent += correctedCode[i]; |
| if (i % 5 === 0) await sleep(8); |
| } |
|
|
| setVerifierStatus("PASS"); |
| setStatus("Corrected block verified", "success"); |
| } |
|
|
| export function getCurrentCode() { |
| return cleanVisibleStream(streamBuffer); |
| } |
|
|
| export function setCode(code) { |
| streamBuffer = code; |
| currentTokens = []; |
|
|
| const display = document.getElementById("stream-display"); |
| if (display) { |
| display.textContent = code; |
| display.scrollTop = display.scrollHeight; |
| } |
| } |
|
|
| export function reset() { |
| streamBuffer = ""; |
| currentTokens = []; |
|
|
| const display = document.getElementById("stream-display"); |
| if (display) display.textContent = ""; |
|
|
| const tokenCount = document.getElementById("token-count"); |
| if (tokenCount) tokenCount.textContent = "0 tok/s"; |
| } |
|
|
| function sleep(ms) { |
| return new Promise((resolve) => setTimeout(resolve, ms)); |
| } |
|
|
| function cleanVisibleStream(code) { |
| if (typeof window.stripMarkdownCodeFence === "function") { |
| return window.stripMarkdownCodeFence(code); |
| } |
| return code; |
| } |
|
|
| Object.assign(window, { |
| appendToken, |
| setStatus, |
| setVerifierStatus, |
| rollbackAndReplace, |
| getCurrentCode, |
| setCode, |
| reset, |
| }); |
|
|