/**
* Pulls the actual HTML document out of an agent's possibly chatty response.
* Agents sometimes wrap output in ```html ... ``` fences or prepend explanation.
*/
export function extractHtml(streamed: string): string {
if (!streamed) return "";
// 1. Strip leading ```html fence (and trailing ```)
const fence = streamed.match(/```(?:html|HTML)?\s*([\s\S]*?)```/);
if (fence) {
const inner = fence[1].trim();
if (inner.startsWith("<")) return inner;
}
// 2. Find
const doctypeStart = streamed.search(/");
if (closeIdx !== -1) {
return streamed.slice(doctypeStart, closeIdx + "