Spaces:
No application file
No application file
Download web/app.js from Pankaj10346/Openenv-Hackathon: direct link, hf CLI and curl.
- Browser
- Download file 10.7 kB
-
https://huggingface.co/spaces/Pankaj10346/Openenv-Hackathon/resolve/main/web/app.js
- Command line
-
hf download hf://spaces/Pankaj10346/Openenv-Hackathon/web/app.js
-
curl -L -o app.js https://huggingface.co/spaces/Pankaj10346/Openenv-Hackathon/resolve/main/web/app.js
10.7 kB
| /** | |
| * OpenEnv WebSocket client for legal_contract_env (simulation mode). | |
| */ | |
| (function () { | |
| "use strict"; | |
| const $ = (id) => document.getElementById(id); | |
| const els = { | |
| wsUrl: $("ws-url"), | |
| btnConnect: $("btn-connect"), | |
| btnDisconnect: $("btn-disconnect"), | |
| connStatus: $("conn-status"), | |
| taskSelect: $("task-select"), | |
| btnReset: $("btn-reset"), | |
| btnState: $("btn-state"), | |
| btnFinish: $("btn-finish"), | |
| documentText: $("document-text"), | |
| standardRules: $("standard-rules"), | |
| taskBanner: $("task-banner"), | |
| clausesRoot: $("clauses-root"), | |
| log: $("log"), | |
| mStep: $("m-step"), | |
| mReward: $("m-reward"), | |
| mReturn: $("m-return"), | |
| mGrader: $("m-grader"), | |
| }; | |
| let ws = null; | |
| let done = false; | |
| /** | |
| * 0.0.0.0 is valid for servers to bind but browsers cannot open WebSockets to it. | |
| * If the page was opened as http://0.0.0.0:8001/, location.host breaks WS — use 127.0.0.1. | |
| */ | |
| function normalizeWsTarget(url) { | |
| if (!url) return url; | |
| return url.replace(/\b0\.0\.0\.0\b/g, "127.0.0.1"); | |
| } | |
| function defaultWsBase() { | |
| const proto = location.protocol === "https:" ? "wss:" : "ws:"; | |
| const host = normalizeWsTarget(location.host); | |
| return `${proto}//${host}`; | |
| } | |
| function buildWsUrl() { | |
| let base = (els.wsUrl.value || "").trim(); | |
| if (!base) base = defaultWsBase(); | |
| base = base.replace(/\/$/, ""); | |
| if (!/^wss?:\/\//i.test(base)) { | |
| const p = location.protocol === "https:" ? "wss:" : "ws:"; | |
| base = `${p}//${base.replace(/^\/\//, "")}`; | |
| } | |
| base = normalizeWsTarget(base); | |
| if (/\/ws$/i.test(base)) return base; | |
| return `${base}/ws`; | |
| } | |
| function setConnected(connected) { | |
| els.btnConnect.disabled = connected; | |
| els.btnDisconnect.disabled = !connected; | |
| els.btnReset.disabled = !connected; | |
| els.btnState.disabled = !connected; | |
| els.btnFinish.disabled = !connected || done; | |
| els.connStatus.textContent = connected ? "Connected" : "Disconnected"; | |
| els.connStatus.className = "pill " + (connected ? "ok" : "idle"); | |
| } | |
| function logLine(msg, kind) { | |
| const li = document.createElement("li"); | |
| li.textContent = `[${new Date().toLocaleTimeString()}] ${msg}`; | |
| if (kind) li.className = `log-${kind}`; | |
| els.log.appendChild(li); | |
| els.log.scrollTop = els.log.scrollHeight; | |
| } | |
| function empty(el) { | |
| while (el.firstChild) el.removeChild(el.firstChild); | |
| } | |
| function renderRules(rules) { | |
| empty(els.standardRules); | |
| if (!rules || typeof rules !== "object") return; | |
| for (const [k, v] of Object.entries(rules)) { | |
| const dt = document.createElement("dt"); | |
| dt.textContent = k; | |
| const dd = document.createElement("dd"); | |
| dd.textContent = v; | |
| els.standardRules.appendChild(dt); | |
| els.standardRules.appendChild(dd); | |
| } | |
| } | |
| function actionPayload(extra) { | |
| return Object.assign({ metadata: {} }, extra); | |
| } | |
| function sendStep(data) { | |
| if (!ws || ws.readyState !== WebSocket.OPEN) { | |
| logLine("Not connected — click Connect, then Reset.", "err"); | |
| return; | |
| } | |
| const msg = { type: "step", data }; | |
| ws.send(JSON.stringify(msg)); | |
| logLine(`→ step ${JSON.stringify(data)}`, "ok"); | |
| } | |
| function renderClauses(obs) { | |
| empty(els.clausesRoot); | |
| const task = obs.task || "classify"; | |
| const clauses = obs.clauses || []; | |
| clauses.forEach((text, idx) => { | |
| const card = document.createElement("article"); | |
| card.className = "clause-card"; | |
| card.dataset.index = String(idx); | |
| const head = document.createElement("div"); | |
| head.className = "clause-head"; | |
| const idxEl = document.createElement("span"); | |
| idxEl.className = "clause-idx"; | |
| idxEl.textContent = `Clause ${idx}`; | |
| head.appendChild(idxEl); | |
| card.appendChild(head); | |
| const p = document.createElement("p"); | |
| p.className = "clause-text"; | |
| p.textContent = text; | |
| card.appendChild(p); | |
| const actions = document.createElement("div"); | |
| actions.className = "clause-actions"; | |
| if (task === "classify") { | |
| const sel = document.createElement("select"); | |
| sel.setAttribute("aria-label", `Type for clause ${idx}`); | |
| ["liability", "termination", "auto_renewal"].forEach((v) => { | |
| const o = document.createElement("option"); | |
| o.value = v; | |
| o.textContent = v.replace("_", " "); | |
| sel.appendChild(o); | |
| }); | |
| const btn = document.createElement("button"); | |
| btn.type = "button"; | |
| btn.className = "btn primary btn-sm"; | |
| btn.textContent = "Classify"; | |
| btn.addEventListener("click", () => { | |
| if (done) return; | |
| sendStep( | |
| actionPayload({ | |
| action_type: "classify_clause", | |
| clause_index: idx, | |
| clause_type: sel.value, | |
| }) | |
| ); | |
| }); | |
| actions.appendChild(sel); | |
| actions.appendChild(btn); | |
| } else if (task === "risk") { | |
| const wrap = document.createElement("div"); | |
| wrap.className = "risk-toggle"; | |
| const y = document.createElement("input"); | |
| y.type = "radio"; | |
| y.name = `risk-${idx}`; | |
| y.value = "yes"; | |
| y.id = `risk-y-${idx}`; | |
| const n = document.createElement("input"); | |
| n.type = "radio"; | |
| n.name = `risk-${idx}`; | |
| n.value = "no"; | |
| n.id = `risk-n-${idx}`; | |
| y.checked = true; | |
| wrap.appendChild(y); | |
| wrap.appendChild( | |
| Object.assign(document.createElement("label"), { | |
| htmlFor: `risk-y-${idx}`, | |
| textContent: "Risky", | |
| }) | |
| ); | |
| wrap.appendChild(n); | |
| wrap.appendChild( | |
| Object.assign(document.createElement("label"), { | |
| htmlFor: `risk-n-${idx}`, | |
| textContent: "Not risky", | |
| }) | |
| ); | |
| const btn = document.createElement("button"); | |
| btn.type = "button"; | |
| btn.className = "btn primary btn-sm"; | |
| btn.textContent = "Submit"; | |
| btn.addEventListener("click", () => { | |
| if (done) return; | |
| sendStep( | |
| actionPayload({ | |
| action_type: "flag_risk", | |
| clause_index: idx, | |
| risk_flag: y.checked, | |
| }) | |
| ); | |
| }); | |
| actions.appendChild(wrap); | |
| actions.appendChild(btn); | |
| } else { | |
| const ta = document.createElement("textarea"); | |
| ta.placeholder = "Safer replacement clause…"; | |
| ta.setAttribute("aria-label", `Fix for clause ${idx}`); | |
| const btn = document.createElement("button"); | |
| btn.type = "button"; | |
| btn.className = "btn primary btn-sm"; | |
| btn.textContent = "Suggest fix"; | |
| btn.addEventListener("click", () => { | |
| if (done) return; | |
| const suggestion = ta.value.trim(); | |
| if (!suggestion) { | |
| logLine("Suggestion empty", "err"); | |
| return; | |
| } | |
| sendStep( | |
| actionPayload({ | |
| action_type: "suggest_fix", | |
| clause_index: idx, | |
| suggestion, | |
| }) | |
| ); | |
| }); | |
| actions.appendChild(ta); | |
| actions.appendChild(btn); | |
| } | |
| card.appendChild(actions); | |
| els.clausesRoot.appendChild(card); | |
| }); | |
| } | |
| function applyObservation(payload) { | |
| const data = payload.data || payload; | |
| const obs = data.observation; | |
| const reward = data.reward; | |
| const isDone = Boolean(data.done); | |
| if (!obs) { | |
| logLine("Missing observation in response", "err"); | |
| return; | |
| } | |
| done = isDone; | |
| els.btnFinish.disabled = !ws || ws.readyState !== WebSocket.OPEN || done; | |
| els.documentText.textContent = obs.document_text || "—"; | |
| renderRules(obs.standard_rules); | |
| els.taskBanner.textContent = `Task: ${obs.task || "—"} · ${obs.message || ""}`; | |
| els.mStep.textContent = obs.step_count != null ? String(obs.step_count) : "—"; | |
| els.mReward.textContent = | |
| reward != null && reward !== undefined ? String(reward) : "—"; | |
| els.mReturn.textContent = | |
| obs.episode_return != null ? String(obs.episode_return) : "—"; | |
| els.mGrader.textContent = | |
| obs.grader_score != null ? obs.grader_score.toFixed(3) : "—"; | |
| renderClauses(obs); | |
| logLine( | |
| `← reward=${reward} done=${isDone} ${obs.message || ""}`, | |
| isDone ? "ok" : "" | |
| ); | |
| } | |
| function handleMessage(raw) { | |
| let msg; | |
| try { | |
| msg = JSON.parse(raw); | |
| } catch (e) { | |
| logLine("Invalid JSON from server", "err"); | |
| return; | |
| } | |
| if (msg.type === "error") { | |
| const d = msg.data || {}; | |
| logLine(`Error: ${d.message || JSON.stringify(d)}`, "err"); | |
| return; | |
| } | |
| if (msg.type === "observation") { | |
| applyObservation(msg); | |
| return; | |
| } | |
| if (msg.type === "state") { | |
| logLine(`State: ${JSON.stringify(msg.data)}`, "ok"); | |
| return; | |
| } | |
| logLine(`? ${raw.slice(0, 200)}`, ""); | |
| } | |
| function connect() { | |
| if (ws && ws.readyState === WebSocket.OPEN) return; | |
| const url = buildWsUrl(); | |
| logLine(`Connecting ${url}…`, "ok"); | |
| ws = new WebSocket(url); | |
| ws.onopen = () => { | |
| setConnected(true); | |
| done = false; | |
| logLine("WebSocket open", "ok"); | |
| }; | |
| ws.onclose = () => { | |
| setConnected(false); | |
| logLine("WebSocket closed", "err"); | |
| }; | |
| ws.onerror = () => { | |
| logLine("WebSocket error", "err"); | |
| els.connStatus.textContent = "Error"; | |
| els.connStatus.className = "pill err"; | |
| }; | |
| ws.onmessage = (ev) => handleMessage(ev.data); | |
| } | |
| function disconnect() { | |
| if (ws) { | |
| try { | |
| ws.send(JSON.stringify({ type: "close" })); | |
| } catch (_) {} | |
| ws.close(); | |
| ws = null; | |
| } | |
| setConnected(false); | |
| } | |
| els.btnConnect.addEventListener("click", connect); | |
| els.btnDisconnect.addEventListener("click", disconnect); | |
| els.btnReset.addEventListener("click", () => { | |
| if (!ws || ws.readyState !== WebSocket.OPEN) return; | |
| const task = els.taskSelect.value; | |
| done = false; | |
| els.btnFinish.disabled = false; | |
| ws.send(JSON.stringify({ type: "reset", data: { task } })); | |
| logLine(`→ reset task=${task}`, "ok"); | |
| }); | |
| els.btnState.addEventListener("click", () => { | |
| if (!ws || ws.readyState !== WebSocket.OPEN) return; | |
| ws.send(JSON.stringify({ type: "state" })); | |
| logLine("→ state", "ok"); | |
| }); | |
| els.btnFinish.addEventListener("click", () => { | |
| if (done) return; | |
| sendStep(actionPayload({ action_type: "finish" })); | |
| }); | |
| if (!els.wsUrl.value) { | |
| els.wsUrl.placeholder = defaultWsBase().replace(/^wss?:\/\//, ""); | |
| } | |
| if (location.protocol.startsWith("http")) { | |
| els.wsUrl.value = defaultWsBase(); | |
| } | |
| })(); | |