videogen/apps/web/lib/mcp/html-reel.ts
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

60 lines
2.3 KiB
TypeScript

/**
* HTML an agent wrote, turned into the subject and narration create_reel
* already renders. Tags are not executed and nothing is fetched. Text inside
* script, style, or similar tags is dropped.
*/
export const HTML_MAX = 30_000;
export const SUBJECT_MAX = 500;
export const SCRIPT_MAX = 5_000;
export type ReelCopy = { readonly subject: string; readonly script: string };
export type ReelError = { readonly error: string };
const DROPPED = /<(script|style|noscript|iframe|svg|object|embed|template)\b[^>]*>[\s\S]*?<\/\1>/gi;
const UNCLOSED = /<(script|style|noscript|iframe|svg|object|embed|template)\b[\s\S]*$/i;
const BLOCK = /<(title|h1|h2|h3|p|li)\b[^>]*>([\s\S]*?)<\/\1>/gi;
function stripHidden(html: string): string {
let out = html.replace(/<!--[\s\S]*?-->/g, "");
let prev = "";
while (out !== prev) {
prev = out;
out = out.replace(DROPPED, "");
}
return out.replace(UNCLOSED, "");
}
function decode(text: string): string {
return text
.replace(/&nbsp;/gi, " ")
.replace(/&lt;/gi, "<")
.replace(/&gt;/gi, ">")
.replace(/&quot;/gi, '"')
.replace(/&#39;|&apos;/gi, "'")
.replace(/&amp;/gi, "&");
}
function visible(inner: string): string {
return decode(inner.replace(/<[^>]+>/g, " ")).replace(/\s+/g, " ").trim();
}
/** The reel copy, or an error when the HTML has nothing to say. */
export function htmlToReel(html: string): ReelCopy | ReelError {
if (typeof html !== "string" || html.trim() === "") return { error: "html is required" };
if (html.length > HTML_MAX) return { error: `html must be at most ${HTML_MAX} characters` };
const blocks: { tag: string; text: string }[] = [];
for (const match of stripHidden(html).matchAll(BLOCK)) {
const text = visible(match[2] ?? "");
if (text) blocks.push({ tag: (match[1] ?? "").toLowerCase(), text });
}
const heading = blocks.find((block) => block.tag === "h1") ?? blocks.find((block) => block.tag === "title");
const spoken = blocks.filter((block) => block.tag !== "title");
const subjectSource = heading?.text ?? spoken[0]?.text ?? "";
if (!subjectSource) return { error: "That HTML has no heading or paragraph to read." };
const subject = subjectSource.slice(0, SUBJECT_MAX);
const body = spoken.map((block) => block.text).join("\n\n");
const script = (body || subject).slice(0, SCRIPT_MAX).trim();
return { subject, script };
}