Restore Markdown preview and embed report assets
Browse filesFiles changed (2)
1 unmodified line23456732 unmodified lines404142434445464748495051525354555644 unmodified lines1011021031041051061071082 unmodified lines11111211311411511630 unmodified lines14714814915015115215326 unmodified lines18018118218318418518618718818912 unmodified lines20220320420520620720818 unmodified lines2272282292302312321 unmodified linefrom __future__ import annotationsimport jsonimport posixpathimport re32 unmodified linesr"""(?:src|href)=["'](?P<value>[^"'#?]+)(?:[?#][^"']*)?["']""",re.I,)MAX_PRESENTATION_BYTES = 25 * 1024 * 1024SAFE_ASSET_SUFFIXES = {".avif",".csv",".gif",".jpeg",".jpg",".json",".png",".svg",".webp",}44 unmodified lineshtml = local.read_text()assets: list[tuple[bytes, str]] = []if source_path == attempt_path:assets = _load_presentation_assets(api,bucket_id,job.artifact_id,2 unmodified lineslocal_root,auth.token,)css = _birch_css_path(home).read_text()if MARKER in html:finalized = _ensure_page_shell(_inject_birch_css(html, css))30 unmodified lineshtml: str,local_root: Path,token: str,) -> list[tuple[bytes, str]]:manifest_remote = f"{workspace}/{attempt_root}/manifest.json"manifest_local = local_root / "manifest.json"api.download_bucket_files(26 unmodified lines)uploads: list[tuple[bytes, str]] = []total = 0for index, path in enumerate(asset_paths):suffix = _path_suffix(path)if suffix not in SAFE_ASSET_SUFFIXES:raise ValueError(f"Unsupported presentation asset type: {path}")local = local_root / f"asset-{index}{suffix}"api.download_bucket_files(12 unmodified lines)relative = posixpath.relpath(path, asset_prefix)uploads.append((payload, f"{workspace}/output/assets/{relative}"))return uploadsdef _path_suffix(path: str) -> str:18 unmodified linesreturn referencesdef read_bucket_markdown(job: ResearchJob,auth: AgentAuth | None,1 unmodified line234567832 unmodified lines41424344454647484950515253545556575859606144 unmodified lines1061071081091101111121131142 unmodified lines11711811912012112212330 unmodified lines15415515615715815916026 unmodified lines18718818919019119219319419519619719812 unmodified lines21121221321421521621721818 unmodified lines2372382392402412422432442452462472482492502512522532542552562572582592602612622632642651 unmodified linefrom __future__ import annotationsimport base64import jsonimport posixpathimport re32 unmodified linesr"""(?:src|href)=["'](?P<value>[^"'#?]+)(?:[?#][^"']*)?["']""",re.I,)LOCAL_ASSET_SRC_RE = re.compile(r"""(?P<prefix>\bsrc\s*=\s*)(?P<quote>["'])(?P<value>assets/[^"'#?]+)(?P=quote)""",re.I,)MAX_PRESENTATION_BYTES = 25 * 1024 * 1024SAFE_ASSET_MEDIA_TYPES = {".avif": "image/avif",".csv": "text/csv",".gif": "image/gif",".jpeg": "image/jpeg",".jpg": "image/jpeg",".json": "application/json",".png": "image/png",".svg": "image/svg+xml",".webp": "image/webp",}44 unmodified lineshtml = local.read_text()assets: list[tuple[bytes, str]] = []embedded_assets: dict[str, tuple[str, bytes]] = {}if source_path == attempt_path:assets, embedded_assets = _load_presentation_assets(api,bucket_id,job.artifact_id,2 unmodified lineslocal_root,auth.token,)html = _embed_presentation_assets(html, embedded_assets)css = _birch_css_path(home).read_text()if MARKER in html:finalized = _ensure_page_shell(_inject_birch_css(html, css))30 unmodified lineshtml: str,local_root: Path,token: str,) -> tuple[list[tuple[bytes, str]], dict[str, tuple[str, bytes]]]:manifest_remote = f"{workspace}/{attempt_root}/manifest.json"manifest_local = local_root / "manifest.json"api.download_bucket_files(26 unmodified lines)uploads: list[tuple[bytes, str]] = []embeds: dict[str, tuple[str, bytes]] = {}total = 0for index, path in enumerate(asset_paths):suffix = _path_suffix(path)media_type = SAFE_ASSET_MEDIA_TYPES.get(suffix)if media_type is None:raise ValueError(f"Unsupported presentation asset type: {path}")local = local_root / f"asset-{index}{suffix}"api.download_bucket_files(12 unmodified lines)relative = posixpath.relpath(path, asset_prefix)uploads.append((payload, f"{workspace}/output/assets/{relative}"))embeds[f"assets/{relative}"] = (media_type, payload)return uploads, embedsdef _path_suffix(path: str) -> str:18 unmodified linesreturn referencesdef _embed_presentation_assets(html: str,assets: dict[str, tuple[str, bytes]],) -> str:"""Embed declared local image sources while retaining published asset files."""def replace(match: re.Match[str]) -> str:value = posixpath.normpath(match.group("value"))asset = assets.get(value)if asset is None:return match.group(0)media_type, payload = assetif not media_type.startswith("image/"):return match.group(0)encoded = base64.b64encode(payload).decode("ascii")return (f"{match.group('prefix')}{match.group('quote')}"f"data:{media_type};base64,{encoded}{match.group('quote')}")return LOCAL_ASSET_SRC_RE.sub(replace, html)def read_bucket_markdown(job: ResearchJob,auth: AgentAuth | None,
234 unmodified lines235236237238239240579 unmodified lines820821822823824825234 unmodified linesmargin-top: 14px;color: var(--dispatch-accent);}.dispatch-log {padding: 12px 16px;border: 1px solid var(--border);579 unmodified lines),onClick=OpenLink(STATE.job.html_report_url),)with If("{{ job.done && job.activity_roll.length > 0 }}"):with Div(css_class="dispatch-log dispatch-log-body"):234 unmodified lines235236237238239240241242243244245246247248249250251252253254255256257258259579 unmodified lines839840841842843844845846847848849850851852853854855856857234 unmodified linesmargin-top: 14px;color: var(--dispatch-accent);}.dispatch-markdown-report {margin-top: 16px;padding: 22px;border: 1px solid var(--border);border-radius: var(--radius);background: var(--card);}.dispatch-markdown-body {margin-top: 14px;color: var(--foreground);font-size: 14px;line-height: 1.65;}.dispatch-markdown-body > :first-child {margin-top: 0;}.dispatch-markdown-body > :last-child {margin-bottom: 0;}.dispatch-log {padding: 12px 16px;border: 1px solid var(--border);579 unmodified lines),onClick=OpenLink(STATE.job.html_report_url),)with If((STATE.job.status == "completed")& STATE.job.markdown_report):with Div(css_class="dispatch-markdown-report"):Text("Markdown report",css_class="dispatch-section-label",)Markdown(STATE.job.markdown_report,css_class="dispatch-markdown-body",)with If("{{ job.done && job.activity_roll.length > 0 }}"):with Div(css_class="dispatch-log dispatch-log-body"):
research/app_artifacts.py+47-14
research/app_ui.py+32