Spaces:
Running
Running
Render workspace images in markdown
Browse filesFiles changed (3)
13 unmodified lines141516171819179 unmodified lines199200201202203204467 unmodified lines67267367467567667713 unmodified linesimport zipfilefrom pathlib import Pathfrom typing import Anyfrom uuid import uuid4from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect179 unmodified linesreturn workspace_rootdef _release_managed_run(bridge: FrontendRunBridge) -> None:_unregister_download_workspace(bridge.download_token)if bridge.managed_run_root:467 unmodified lines)@app.websocket("/ws")async def websocket_endpoint(websocket: WebSocket) -> None:await websocket.accept()13 unmodified lines14151617181920179 unmodified lines200201202203204205206207208209210211212213214215216217218219220221222223224225226227467 unmodified lines69569669769869970070170270370470570613 unmodified linesimport zipfilefrom pathlib import Pathfrom typing import Anyfrom urllib.parse import unquotefrom uuid import uuid4from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect179 unmodified linesreturn workspace_rootdef _resolve_workspace_file_path(workspace_root: Path, raw_path: str) -> Path:text = str(raw_path or "").strip()if text.startswith("file://"):text = text[7:]text = unquote(text)if not text:raise HTTPException(status_code=400, detail="workspace file path is required")candidate = Path(text)if not candidate.is_absolute():candidate = workspace_root / textresolved = candidate.resolve()try:resolved.relative_to(workspace_root.resolve())except ValueError as exc:raise HTTPException(status_code=403, detail="workspace file path is outside the workspace") from excif not resolved.is_file():raise HTTPException(status_code=404, detail="workspace file does not exist")if resolved.suffix.lower() not in {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp"}:raise HTTPException(status_code=415, detail="only workspace image files can be displayed inline")return resolveddef _release_managed_run(bridge: FrontendRunBridge) -> None:_unregister_download_workspace(bridge.download_token)if bridge.managed_run_root:467 unmodified lines)@app.get("/api/workspace-file")def workspace_file(token: str, path: str) -> FileResponse:workspace_root = _download_workspace_for_token(token)return FileResponse(_resolve_workspace_file_path(workspace_root, path))@app.websocket("/ws")async def websocket_endpoint(websocket: WebSocket) -> None:await websocket.accept()
601 unmodified lines602603604605606607601 unmodified linestext-underline-offset: 3px;}.markdown-body table {width: 100%;border-collapse: collapse;601 unmodified lines602603604605606607608609610611612613614615601 unmodified linestext-underline-offset: 3px;}.markdown-body img {display: block;max-width: 100%;height: auto;margin: 0.65rem 0;border-radius: 14px;}.markdown-body table {width: 100%;border-collapse: collapse;
128 unmodified lines12913013113213313444 unmodified lines17918018118218318421 unmodified lines206207208209210211395 unmodified lines6076086096106116125 unmodified lines6186196206216226237 unmodified lines631632633634635636128 unmodified linesvar autoFollowTimeline = true;var conversationStarted = false;var downloadToken = "";var downloadingWorkspace = false;var images = [];var COLLAPSED_STEP_HEIGHT = 220;44 unmodified linesreturn restored;}function renderMathInMarkdown(container) {if (!window.renderMathInElement) return;container.querySelectorAll(".markdown-body").forEach(function (body) {21 unmodified linestry {var protectedMath = protectMathSegments(text);var rawHtml = window.marked.parse(protectedMath.text, { gfm: true, breaks: false, async: false });var safeHtml = window.DOMPurify.sanitize(rawHtml, { USE_PROFILES: { html: true } });safeHtml = restoreMathSegments(safeHtml, protectedMath.segments);return '<div class="markdown-body">' + safeHtml + "</div>";395 unmodified linessetStatus("Connected", "idle");};ws.onclose = function () {setDownloadToken("");updateWorkspaceHint(false);clearAskRequest();5 unmodified linesif (message.type === "ready") {setStatus("Connected", "idle");} else if (message.type === "conversation_reset") {setDownloadToken("");updateWorkspaceHint(false);if (keepSubmittedMessageOnReset) {7 unmodified lines} else if (message.type === "uploaded_images") {addEvent("runtime", "Uploaded images saved", "<pre>" + escapeHtml((message.paths || []).join("\n")) + "</pre>", []);} else if (message.type === "run_started") {setDownloadToken(message.download_token || "");updateWorkspaceHint(Boolean(message.download_token));setRunning(true, "Running");128 unmodified lines12913013113213313413544 unmodified lines18018118218318418518618718818919019119219319419519619719819920020120221 unmodified lines224225226227228229230395 unmodified lines6266276286296306316325 unmodified lines6386396406416426436447 unmodified lines652653654655656657658128 unmodified linesvar autoFollowTimeline = true;var conversationStarted = false;var downloadToken = "";var fileToken = "";var downloadingWorkspace = false;var images = [];var COLLAPSED_STEP_HEIGHT = 220;44 unmodified linesreturn restored;}function isRemoteOrInlineImageSrc(src) {return /^(https?:|data:|blob:|#|\/api\/|\/static\/)/i.test(String(src || "").trim());}function rewriteWorkspaceImageSources(html) {var token = fileToken || downloadToken;if (!token) return html;var template = document.createElement("template");template.innerHTML = html;template.content.querySelectorAll("img").forEach(function (img) {var src = img.getAttribute("src") || "";if (!src || isRemoteOrInlineImageSrc(src)) return;img.setAttribute("src", "/api/workspace-file?token=" + encodeURIComponent(token) + "&path=" + encodeURIComponent(src));});return template.innerHTML;}function renderMathInMarkdown(container) {if (!window.renderMathInElement) return;container.querySelectorAll(".markdown-body").forEach(function (body) {21 unmodified linestry {var protectedMath = protectMathSegments(text);var rawHtml = window.marked.parse(protectedMath.text, { gfm: true, breaks: false, async: false });rawHtml = rewriteWorkspaceImageSources(rawHtml);var safeHtml = window.DOMPurify.sanitize(rawHtml, { USE_PROFILES: { html: true } });safeHtml = restoreMathSegments(safeHtml, protectedMath.segments);return '<div class="markdown-body">' + safeHtml + "</div>";395 unmodified linessetStatus("Connected", "idle");};ws.onclose = function () {fileToken = "";setDownloadToken("");updateWorkspaceHint(false);clearAskRequest();5 unmodified linesif (message.type === "ready") {setStatus("Connected", "idle");} else if (message.type === "conversation_reset") {fileToken = "";setDownloadToken("");updateWorkspaceHint(false);if (keepSubmittedMessageOnReset) {7 unmodified lines} else if (message.type === "uploaded_images") {addEvent("runtime", "Uploaded images saved", "<pre>" + escapeHtml((message.paths || []).join("\n")) + "</pre>", []);} else if (message.type === "run_started") {fileToken = message.download_token || "";setDownloadToken(message.download_token || "");updateWorkspaceHint(Boolean(message.download_token));setRunning(true, "Running");
frontend/local_server.py+29
frontend/static/app.css+8
frontend/static/app.js+22