Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/backend/creationJobs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { creationClient } from "./creationClient.ts";
import { creationClient, currentAsset } from "./creationClient.ts";
import type { Asset, ProjectSnapshot } from "../contracts/generated.ts";
import type { GenerationJob } from "../canvas/generationHistory.ts";
import {
Expand Down Expand Up @@ -38,6 +38,14 @@ export function creationJobs(
item.binding.role === "material" &&
item.binding.objectRef.assetId === asset.id,
);
const origin = snapshot.canvasItems.find((item) => item.id === job.originItemId);
const delivered =
job.status === "succeeded" &&
asset &&
origin &&
currentAsset(snapshot, origin)?.id === asset.id
? asset.id
: undefined;
const phase = generationPhase(job);
const failureMessage = job.errorDetails
? generationFailureMessage(job.errorDetails)
Expand Down Expand Up @@ -65,6 +73,7 @@ export function creationJobs(
}
: undefined,
activityPhase: phase ?? undefined,
...(delivered ? { deliveredAssetId: delivered } : {}),
preview:
preview ??
(asset ? { assetId: asset.id, state: "pending" } : undefined),
Expand Down
13 changes: 12 additions & 1 deletion src/canvas/generationActivity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import type { BackendTask } from "../backend/generation.ts";
import type { GenerationJob } from "./generationHistory.ts";
import type { CanvasNode } from "./model.ts";
import { expectedDuration } from "./generationDurations.ts";

/** Display ownership may differ from the source retained by task/history APIs. */
export function jobActivities(
jobs: readonly GenerationJob[],
documentId: string,
nodes: readonly CanvasNode[] = [],
): ActiveGeneration[] {
const displayed = new Map(nodes.map((node) => [node.id, node.data.backendAssetId]));
return jobs.flatMap((job) => {
const finishing = Boolean(
// A regenerated node keeps its old media until the new result has loaded;
// stay busy until then rather than briefly revealing the old image.
const delivering = Boolean(
job.status === "succeeded" &&
job.deliveredAssetId &&
displayed.has(job.nodeId) &&
displayed.get(job.nodeId) !== job.deliveredAssetId,
);
const finishing = delivering || Boolean(
job.toolTask &&
job.status === "succeeded" &&
job.toolTask.placement === "pending",
Expand Down
5 changes: 5 additions & 0 deletions src/canvas/generationHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export interface GenerationJob {
settings: GenerationSettings;
};
outputs?: ServiceResult[];
/**
* A succeeded generation's result asset once the service has made it the
* origin node's current content; the canvas may still be loading it.
*/
deliveredAssetId?: string;
/** Rebuildable unified history preview; independent of execution status. */
preview?: { assetId: string; state: "pending" | "ready" | "unavailable" };
/** The AI tool request behind a tool task, so a reopened canvas can follow it. */
Expand Down
3 changes: 2 additions & 1 deletion src/canvas/workspace/useWorkspaceRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export function useWorkspaceRuntime(): WorkspaceRuntime {
...jobActivities(
jobs.filter((job) => !job.tool),
doc.id,
doc.nodes,
),
...jobActivities(
jobs.filter((job) => job.tool),
Expand All @@ -177,7 +178,7 @@ export function useWorkspaceRuntime(): WorkspaceRuntime {
],
doc.id,
),
[activeGenerations, doc.id, jobs],
[activeGenerations, doc.id, doc.nodes, jobs],
);
const clipboard = useRef<CanvasFragment | null>(null);
const clipboardMedia = useRef<ClipboardMedia | null>(null);
Expand Down
23 changes: 23 additions & 0 deletions tests/canvasPendingResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,26 @@ test("history polling and reloads learn each successful duration sample only onc
else Reflect.deleteProperty(globalThis, "localStorage");
}
});

test("a regenerated node stays busy until its delivered result is displayed", () => {
const node = createNode("image");
node.data.backendAssetId = "old-asset";
const job = {
id: "job",
nodeId: node.id,
title: "regenerate",
status: "succeeded" as const,
time: "",
outputKind: "image" as const,
deliveredAssetId: "new-asset",
};
const [activity] = jobActivities([job], "doc", [node]);
assert.equal(activity?.phase, "finishing");
assert.equal(activity?.nodeId, node.id);
// Once the canvas shows the new asset, or the node is gone, nothing is pending.
const shown = { ...node, data: { ...node.data, backendAssetId: "new-asset" } };
assert.deepEqual(jobActivities([job], "doc", [shown]), []);
assert.deepEqual(jobActivities([job], "doc", []), []);
// A succeeded job whose result is not the node's current content stays idle.
assert.deepEqual(jobActivities([{ ...job, deliveredAssetId: undefined }], "doc", [node]), []);
});
20 changes: 20 additions & 0 deletions tests/creationJobs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,23 @@ test("video history shows measured duration separately before the preview loads"
const [withoutResult] = creationJobs({ ...completed, results: [] });
assert.deepEqual(withoutResult?.videoDuration, { requestedSeconds: 4, resultSeconds: undefined });
});

test("a succeeded job names the result its origin node now shows", () => {
const { snapshot } = fixture();
const job = { ...snapshot.jobs[0]!, originItemId: "00000000-0000-0000-0000-000000000033" };
const delivered = (value: ProjectSnapshot) =>
creationJobs({ ...value, jobs: [job] })[0]!.deliveredAssetId;
assert.equal(delivered(snapshot), "00000000-0000-0000-0000-00000000000f");
// Another current result, or a running job, has nothing delivered yet.
assert.equal(
delivered({
...snapshot,
generationSpecs: snapshot.generationSpecs.map((spec) => ({ ...spec, currentResultId: null })),
}),
undefined,
);
assert.equal(
creationJobs({ ...snapshot, jobs: [{ ...job, status: "running" as const }] })[0]!.deliveredAssetId,
undefined,
);
});
Loading