Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

refactor: one layout for a HyperFrames project, whatever renders it

kashorin295488a8e508
diff --git a/packages/hyperframes/src/index.ts b/packages/hyperframes/src/index.ts
index a64beb0e..190ea9a8 100644
--- a/packages/hyperframes/src/index.ts
+++ b/packages/hyperframes/src/index.ts
@@ -19,3 +19,5 @@ export {
hyperframesTypes,
} from "./manifest.js";
export type * from "./types.js";
+export { stageHyperframesProject } from "./project.js";
+export type { HyperframesArtifactReader } from "./project.js";
diff --git a/packages/hyperframes/src/project.ts b/packages/hyperframes/src/project.ts
new file mode 100644
index 00000000..54cf419d
--- /dev/null
+++ b/packages/hyperframes/src/project.ts
@@ -0,0 +1,67 @@
+import { mkdir, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+
+import type { BlobRef } from "@narratage/protocol";
+
+import { assertHyperframesDocument, materializeHyperframesHtml } from "./document.js";
+import type { HyperframesDocument } from "./types.js";
+
+/** Reads one content-addressed dependency. Whose store it comes from is the caller's business. */
+export type HyperframesArtifactReader = (artifact: BlobRef) => Promise<Uint8Array>;
+
+const EXTENSIONS: Readonly<Record<string, string>> = {
+ "image/png": ".png",
+ "image/jpeg": ".jpg",
+ "image/webp": ".webp",
+ "video/mp4": ".mp4",
+ "video/webm": ".webm",
+ "font/woff2": ".woff2",
+ "font/woff": ".woff",
+ "font/ttf": ".ttf",
+ "font/otf": ".otf",
+};
+
+function extension(mediaType: string): string {
+ const found = EXTENSIONS[mediaType];
+ if (found === undefined) throw new Error(`HyperFrames does not support Artifact media type ${mediaType}`);
+ return found;
+}
+
+/**
+ * Lay one document out as a directory HyperFrames can render.
+ *
+ * The same layout serves a local render and a distributed one: the local
+ * Provider points the CLI at it, and the AWS Provider tars it into a site. If
+ * the two laid it out differently, one Need would name two different projects
+ * and the content-addressed site id would stop meaning anything.
+ *
+ * Dependencies are named by digest, so a directory built twice from one
+ * document is byte-identical and an unchanged tree re-uploads as a no-op.
+ */
+export async function stageHyperframesProject(options: {
+ readonly document: HyperframesDocument;
+ readonly directory: string;
+ readonly read: HyperframesArtifactReader;
+}): Promise<{ readonly html: string }> {
+ const { document, directory, read } = options;
+ assertHyperframesDocument(document);
+ const artifactDirectory = join(directory, "artifacts");
+ await mkdir(artifactDirectory, { recursive: true });
+ const paths = new Map<string, string>();
+ await Promise.all(document.artifacts.map(async (artifact) => {
+ const name = `${artifact.digest.slice("sha256:".length)}${extension(artifact.mediaType)}`;
+ const bytes = await read(artifact);
+ if (bytes.byteLength !== artifact.size) {
+ throw new Error(`HyperFrames Artifact ${artifact.digest} size differs`);
+ }
+ await writeFile(join(artifactDirectory, name), bytes);
+ paths.set(artifact.digest, `./artifacts/${name}`);
+ }));
+ const html = materializeHyperframesHtml(document, (artifact) => {
+ const path = paths.get(artifact.digest);
+ if (path === undefined) throw new Error(`HyperFrames Artifact ${artifact.digest} was not staged`);
+ return path;
+ });
+ await writeFile(join(directory, "index.html"), html, "utf8");
+ return { html };
+}
diff --git a/packages/provider-hyperframes-local/src/provider.ts b/packages/provider-hyperframes-local/src/provider.ts
index a63d6858..42959609 100644
--- a/packages/provider-hyperframes-local/src/provider.ts
+++ b/packages/provider-hyperframes-local/src/provider.ts
@@ -1,12 +1,12 @@
import { spawn } from "node:child_process";
import { createRequire } from "node:module";
-import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
+import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { mediaTypes, sealRenderedVisual } from "@narratage/media";
import type { RenderedVisual } from "@narratage/media";
import type { EndpointInvocationContext, EndpointFulfillment } from "@narratage/endpoint-kit";
-import { assertHyperframesDocument, materializeHyperframesHtml } from "@narratage/hyperframes";
+import { assertHyperframesDocument, stageHyperframesProject } from "@narratage/hyperframes";
import type { HyperframesDocument } from "@narratage/hyperframes";
import { renderHyperframesCapabilities } from "@narratage/render-hyperframes";
import { canonicalize, digestOf } from "@narratage/protocol";
@@ -133,26 +133,6 @@ async function runProcess(args: {
});
}
-function extension(mediaType: string): string {
- const known: Readonly<Record<string, string>> = {
- "image/png": ".png",
- "image/jpeg": ".jpg",
- "image/webp": ".webp",
- "image/gif": ".gif",
- "image/svg+xml": ".svg",
- "video/mp4": ".mp4",
- "video/webm": ".webm",
- "video/quicktime": ".mov",
- "font/woff2": ".woff2",
- "font/woff": ".woff",
- "font/otf": ".otf",
- "font/ttf": ".ttf",
- };
- const result = known[mediaType];
- assert(result !== undefined, `HyperFrames does not support Artifact media type ${mediaType}`);
- return result;
-}
-
async function artifactBytes(context: EndpointInvocationContext, artifact: BlobRef): Promise<Uint8Array> {
const bytes = await context.artifacts.get(artifact.digest);
assert(bytes !== undefined, `HyperFrames Artifact ${artifact.digest} is unavailable`);
@@ -290,21 +270,11 @@ export function createLocalHyperframesProvider(config: CreateLocalHyperframesPro
const document = visualRequest(context.need.constraints);
const work = await mkdtemp(join(tmpdir(), "svml-hyperframes-local-"));
try {
- const artifactDir = join(work, "artifacts");
- await mkdir(artifactDir);
- const paths = new Map<string, string>();
- await Promise.all(document.artifacts.map(async (artifact) => {
- const name = `${artifact.digest.slice("sha256:".length)}${extension(artifact.mediaType)}`;
- const path = join(artifactDir, name);
- await writeFile(path, await artifactBytes(context, artifact));
- paths.set(artifact.digest, `./artifacts/${name}`);
- }));
- const html = materializeHyperframesHtml(document, (artifact) => {
- const path = paths.get(artifact.digest);
- assert(path !== undefined, `HyperFrames Artifact ${artifact.digest} was not staged`);
- return path;
+ await stageHyperframesProject({
+ document,
+ directory: work,
+ read: (artifact) => artifactBytes(context, artifact),
});
- await writeFile(join(work, "index.html"), html, "utf8");
const output = join(work, "visual.mp4");
const fps = document.frameRate.denominator === 1
? String(document.frameRate.numerator)