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(studio): browse project results through distribution

kashorin03bc2e866497
diff --git a/packages/studio/src/build-library.ts b/packages/studio/src/build-library.ts
index 6480cf75..a9f15fc9 100644
--- a/packages/studio/src/build-library.ts
+++ b/packages/studio/src/build-library.ts
@@ -1,9 +1,8 @@
-import { isAbsolute, join, relative, resolve, sep } from "node:path";
+import { isAbsolute, relative, resolve, sep } from "node:path";
import { buildIdCreatedAt } from "@hypit/protocol";
import type { BuildView, NodeRuntimeHost } from "@hypit/runtime-host-node";
import type { StoredValue, TypeRef } from "@hypit/protocol";
-import { FileBuildResultRepository } from "@hypit/build-result";
import type {
BuildResultFileRange,
BuildResultFileRef,
@@ -23,7 +22,7 @@ type RuntimeControl = Awaited<ReturnType<NodeRuntimeHost["openControl"]>>;
export type StudioBuildLibrary = {
readonly profile?: string;
readonly runtime?: Pick<RuntimeControl, "activity">;
- readonly library: () => Promise<StudioLibraryView>;
+ readonly library: (before?: string) => Promise<StudioLibraryView>;
readonly resolveHistoricalOutput: (
build: string,
output: string,
@@ -183,16 +182,24 @@ export async function readStudioLibrary(input: {
readonly workspaceRoot: string;
readonly runtime?: Pick<RuntimeControl, "activity">;
readonly results: BuildResultRepository;
+ readonly before?: string;
}): Promise<StudioLibraryView> {
- const manifests = (await input.results.browse({ limit: 100 })).results.filter((manifest) =>
+ const page = await input.results.browse({
+ limit: 25,
+ ...(input.before === undefined ? {} : { before: input.before }),
+ });
+ const manifests = page.results.filter((manifest) =>
isWithin(input.workspaceRoot, manifest.run?.path ?? manifest.source.path)
|| isWithin(input.workspaceRoot, manifest.source.path));
const resultsByBuild = new Map(manifests.map((manifest) => [manifest.id, manifest]));
- const active = input.runtime === undefined ? [] : (await input.runtime.activity()).builds;
+ const active = input.runtime === undefined || input.before !== undefined
+ ? []
+ : (await input.runtime.activity()).builds;
const views = active.filter((view) => buildBelongsTo(input.workspaceRoot, view));
return {
environment: resolve(input.workspaceRoot),
...(input.profile === undefined ? {} : { runtime: resolve(input.profile) }),
+ ...(page.next === undefined ? {} : { next: page.next }),
tasks: [
...views.map((view) => taskView(
input.workspaceRoot,
@@ -213,7 +220,7 @@ export async function openStudioBuildLibrary(
packageRoot: string,
workspaceRoot: string,
distributionPackageRoot?: string,
-): Promise<StudioBuildLibrary | undefined> {
+): Promise<StudioBuildLibrary> {
const resolvedProfile = profile === undefined ? undefined : resolve(profile);
const host = resolvedProfile === undefined
? undefined
@@ -222,23 +229,27 @@ export async function openStudioBuildLibrary(
...(distributionPackageRoot === undefined ? {} : { distributionPackageRoot }),
});
const runtime = await host?.openControl({ readOnly: true });
- const defaultResultRoot = join(workspaceRoot, ".hypit", "results");
- const openedResults = videoCliDistribution.openProjectResults === undefined
- ? undefined
- : await videoCliDistribution.openProjectResults(workspaceRoot, {
- packageRoot,
- ...(distributionPackageRoot === undefined ? {} : { distributionPackageRoot }),
- });
- const results = openedResults?.repository ?? new FileBuildResultRepository(defaultResultRoot);
+ let openedResults: Awaited<ReturnType<typeof videoCliDistribution.openProjectResults>>;
+ try {
+ openedResults = await videoCliDistribution.openProjectResults(workspaceRoot, {
+ packageRoot,
+ ...(distributionPackageRoot === undefined ? {} : { distributionPackageRoot }),
+ });
+ } catch (error) {
+ await runtime?.close();
+ throw error;
+ }
+ const results = openedResults.repository;
return {
...(resolvedProfile === undefined ? {} : { profile: resolvedProfile }),
...(runtime === undefined ? {} : { runtime }),
- async library() {
+ async library(before) {
return await readStudioLibrary({
...(resolvedProfile === undefined ? {} : { profile: resolvedProfile }),
workspaceRoot,
...(runtime === undefined ? {} : { runtime }),
results,
+ ...(before === undefined ? {} : { before }),
});
},
async resolveHistoricalOutput(build, output) {
@@ -256,7 +267,7 @@ export async function openStudioBuildLibrary(
};
},
async close() {
- await openedResults?.close();
+ await openedResults.close();
await runtime?.close();
},
};
diff --git a/packages/studio/src/server.ts b/packages/studio/src/server.ts
index fe05ae95..e072a849 100644
--- a/packages/studio/src/server.ts
+++ b/packages/studio/src/server.ts
@@ -88,20 +88,17 @@ export function studioPlugin(options: StudioPluginOptions): Plugin {
let publishing = 0;
let requestedRevision = 0;
let currentSource = options.source;
- let library: StudioLibraryView | undefined;
let allowedSourceFiles = new Set<string>();
const watched = new Map<string, FSWatcher>();
const watchedFiles = new Set<string>();
const storyboards = new Map<string, Promise<StudioStoryboard>>();
- const readLibrary = async (): Promise<StudioLibraryView> => {
- const next = await options.buildLibrary?.library() ?? {
+ const readLibrary = async (before?: string): Promise<StudioLibraryView> => {
+ return await options.buildLibrary?.library(before) ?? {
environment: options.workspaceRoot,
tasks: [],
artifacts: [],
};
- library = next;
- return next;
};
const watchSource = (path: string): void => {
@@ -578,7 +575,8 @@ export function studioPlugin(options: StudioPluginOptions): Plugin {
return;
}
if (url.pathname === "/__studio/library") {
- void readLibrary().then(
+ const before = url.searchParams.get("before") ?? undefined;
+ void readLibrary(before).then(
(view) => json(response, 200, view),
(error) => json(response, 500, { error: error instanceof Error ? error.message : String(error) }),
);
diff --git a/packages/studio/src/shared.ts b/packages/studio/src/shared.ts
index a484714f..4df1c9df 100644
--- a/packages/studio/src/shared.ts
+++ b/packages/studio/src/shared.ts
@@ -228,6 +228,8 @@ export type StudioArtifactView = {
export type StudioLibraryView = {
readonly environment: string;
readonly runtime?: string;
+ /** Cursor for the next older page of immutable Build Results. */
+ readonly next?: string;
readonly tasks: readonly StudioTaskView[];
readonly artifacts: readonly StudioArtifactView[];
};
diff --git a/packages/studio/src/style.css b/packages/studio/src/style.css
index ef167de8..16cdba00 100644
--- a/packages/studio/src/style.css
+++ b/packages/studio/src/style.css
@@ -441,6 +441,18 @@ button:focus-visible, textarea:focus-visible {
.library-empty-icon .icon { width: 21px; height: 21px; }
.library-empty strong { color: #aeb2ba; font-size: 11px; font-weight: 600; }
.library-empty p { max-width: 260px; margin: 6px 0 0; color: #666b74; font-size: 9px; line-height: 1.55; }
+.library-older {
+ grid-column: 1 / -1;
+ width: 100%;
+ min-height: 32px;
+ margin-top: 3px;
+ border: 1px solid #292c31;
+ border-radius: 6px;
+ background: #191a1e;
+ color: #858a93;
+ font-size: 9px;
+}
+.library-older:hover { border-color: #3a3d44; background: #202227; color: #d1d4da; }
.code { --code-inline-end: 12px; flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.code-toolbar { min-width: 0; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
diff --git a/packages/studio/src/ui/library.ts b/packages/studio/src/ui/library.ts
index 9a197d1b..e5c71b27 100644
--- a/packages/studio/src/ui/library.ts
+++ b/packages/studio/src/ui/library.ts
@@ -147,6 +147,15 @@ function artifactCard(artifact: StudioArtifactView): HTMLElement {
return link;
}
+function olderButton(load: () => void): HTMLButtonElement {
+ const button = document.createElement("button");
+ button.type = "button";
+ button.className = "library-older";
+ button.textContent = "Load older";
+ button.addEventListener("click", load);
+ return button;
+}
+
export function createLibraryPane(code: CodePane): LibraryPane {
const element = document.createElement("section");
element.className = "library";
@@ -202,6 +211,7 @@ export function createLibraryPane(code: CodePane): LibraryPane {
let library: StudioLibraryView | undefined;
let artifactFilter = "all";
let refreshing = false;
+ let expanded = false;
const renderSources = (): void => {
if (snapshot === undefined) return;
@@ -240,15 +250,16 @@ export function createLibraryPane(code: CodePane): LibraryPane {
context.querySelector("strong")!.textContent = runtime === undefined ? "not selected" : leaf(library!.environment);
context.title = runtime === undefined ? "No Runtime selected for this environment" : `${library!.environment}\n${runtime}`;
runtimeContext.append(context);
+ const entries: HTMLElement[] = tasks.map(taskCard);
if (tasks.length === 0) {
- taskList.replaceChildren(emptyState("tasks",
+ entries.push(emptyState("tasks",
runtime === undefined ? "No Runtime selected" : "No builds yet",
runtime === undefined
? "Select one with hypit runtime use, then reopen Studio."
: "Builds created from this environment will appear here."));
- return;
}
- taskList.replaceChildren(...tasks.map(taskCard));
+ if (library?.next !== undefined) entries.push(olderButton(() => void loadOlder()));
+ taskList.replaceChildren(...entries);
};
const renderArtifacts = (): void => {
@@ -271,13 +282,14 @@ export function createLibraryPane(code: CodePane): LibraryPane {
const shown = artifactFilter === "all"
? artifacts
: artifacts.filter((artifact) => artifact.mediaType.startsWith(`${artifactFilter}/`));
+ const entries: HTMLElement[] = shown.map(artifactCard);
if (shown.length === 0) {
- artifactGrid.replaceChildren(emptyState("results",
+ entries.push(emptyState("results",
artifacts.length === 0 ? "No accepted artifacts" : `No ${artifactFilter} artifacts`,
"Public files appear here when a Build Result contains them."));
- return;
}
- artifactGrid.replaceChildren(...shown.map(artifactCard));
+ if (library?.next !== undefined) entries.push(olderButton(() => void loadOlder()));
+ artifactGrid.replaceChildren(...entries);
};
const switchTo = (next: LibrarySection): void => {
@@ -297,15 +309,41 @@ export function createLibraryPane(code: CodePane): LibraryPane {
tab.addEventListener("click", () => switchTo(tab.dataset.libraryTab as LibrarySection));
}
- const refresh = async (): Promise<void> => {
+ const mergeLibrary = (value: StudioLibraryView, append: boolean): void => {
+ if (library === undefined || (!append && !expanded)) {
+ library = value;
+ return;
+ }
+ const tasks = new Map(library.tasks.map((task) => [task.id, task]));
+ const artifacts = new Map(library.artifacts.map((artifact) => [artifact.id, artifact]));
+ for (const task of value.tasks) tasks.set(task.id, task);
+ for (const artifact of value.artifacts) artifacts.set(artifact.id, artifact);
+ library = {
+ environment: value.environment,
+ ...(value.runtime === undefined ? {} : { runtime: value.runtime }),
+ ...((append || !expanded ? value.next : library.next) === undefined
+ ? {}
+ : { next: append || !expanded ? value.next : library.next }),
+ tasks: [...tasks.values()].sort((left, right) => right.createdAt - left.createdAt || left.id.localeCompare(right.id)),
+ artifacts: [...artifacts.values()].sort((left, right) =>
+ Number(right.highlighted) - Number(left.highlighted)
+ || right.createdAt - left.createdAt
+ || left.output.localeCompare(right.output)
+ || left.valuePath.localeCompare(right.valuePath)),
+ };
+ };
+
+ const refresh = async (before?: string): Promise<void> => {
if (refreshing) return;
refreshing = true;
element.classList.add("is-refreshing");
try {
- const response = await fetch("/__studio/library");
+ const query = before === undefined ? "" : `?${new URLSearchParams({ before }).toString()}`;
+ const response = await fetch(`/__studio/library${query}`);
const value = await response.json() as StudioLibraryView | { readonly error: string };
if (!response.ok || "error" in value) throw new Error("error" in value ? value.error : "Library unavailable");
- library = value;
+ mergeLibrary(value, before !== undefined);
+ if (before !== undefined) expanded = true;
renderTasks();
renderArtifacts();
} catch (error) {
@@ -318,6 +356,10 @@ export function createLibraryPane(code: CodePane): LibraryPane {
}
};
+ const loadOlder = async (): Promise<void> => {
+ if (library?.next !== undefined) await refresh(library.next);
+ };
+
for (const button of Array.from(element.querySelectorAll<HTMLButtonElement>("[data-library-refresh]"))) {
button.addEventListener("click", () => void refresh());
}
diff --git a/packages/studio/test/build-library.test.ts b/packages/studio/test/build-library.test.ts
index fa791206..423aff8b 100644
--- a/packages/studio/test/build-library.test.ts
+++ b/packages/studio/test/build-library.test.ts
@@ -50,7 +50,10 @@ test("Studio library joins this environment's Builds with project Build Result f
async removeIncomplete() {},
async updatePresentation() { throw new Error("read-only fixture"); },
async read(build) { return manifests.find((item) => item.id === build); },
- async browse() { return { results: manifests }; },
+ async browse(request) {
+ assert.deepEqual(request, { limit: 25 });
+ return { results: manifests, next: relevant.id };
+ },
async describeOutput(build, output) {
const manifest = manifests.find((item) => item.id === build);
const value = manifest?.outputs[output as "final.video"];
@@ -79,6 +82,7 @@ test("Studio library joins this environment's Builds with project Build Result f
results,
});
+ assert.equal(view.next, relevant.id);
assert.deepEqual(view.tasks.map((task) => ({
id: task.id,
status: task.status,