"""Inject the published-app runtime API into served HTML. A published app talks only to its own origin. This shim wires the two capabilities the edge hosts cheaply, same-origin, so an app's JS can use them without knowing about Tigris, the cloud, or budgets: window.OUTPUT_COMPUTE(input) -> runs the app's sandboxed backend.py (/__compute), resolves to its `result` dict. window.OUTPUT_LLM(body) -> a metered Claude call (/__llm), resolves to the raw fetch Response (stream it, or await .json()). The shape matches the desktop's _build_data_injection so an app written once works in the App Builder preview AND when published. We do NOT precompute results here (a published page has many visitors with different inputs); the app drives compute itself via OUTPUT_COMPUTE.""" from __future__ import annotations _RUNTIME_SHIM = """""" def inject_runtime(html: bytes) -> bytes: """Insert the runtime shim into an HTML document (before , else before , else prepend). Returns bytes so callers can serve it directly.""" try: text = html.decode("utf-8") except UnicodeDecodeError: return html # not text we can safely rewrite; serve as-is if "" in text: out = text.replace("", _RUNTIME_SHIM + "\n", 1) elif "