Skip to content

The VS Code of video editing, built for agents.

TypeScript

Integrate source error handling and enhance UI feedback

konstantin-paulus620dd7600c81
diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.ts
index 29a2b2fd..e03d7cc6 100644
--- a/apps/web/src/engine/create-engine.ts
+++ b/apps/web/src/engine/create-engine.ts
@@ -9,6 +9,7 @@ import { AssetSelection, Hud, Keys, Pointer, PointerEvents, ProjectConfig, SnapL
import { inputSystem } from './input/input-system';
import { timelineSystem, TimelineSurface } from './timeline';
import { shortcutSystem } from './input/shortcuts';
+import { sourceErrorSystem } from './source-errors';
import type { RuntimeWorld } from '@diffusionstudio/runtime';
import type { CanvasPointerEvent, PointerEventType } from '@diffusionstudio/runtime';
@@ -199,6 +200,7 @@ class Engine {
inputSystem(this.world);
shortcutSystem(this.world);
assetSystem(this.world);
+ sourceErrorSystem(this.world);
playbackSystem(this.world);
motionSystem(this.world);
transformSystem(this.world);
diff --git a/apps/web/src/engine/hud/hud-system.ts b/apps/web/src/engine/hud/hud-system.ts
index fce304bb..559d0207 100644
--- a/apps/web/src/engine/hud/hud-system.ts
+++ b/apps/web/src/engine/hud/hud-system.ts
@@ -19,7 +19,7 @@ import { Not, Or } from 'koota';
import {
Active, Computed, Culled, FrameRate, Generating, Geometry, Group, Hidden,
HitRegions, Hovering, Name, Playback, RenderSurface, Root, Selected,
- Sequential, ChildOf,
+ Sequential, SourceError, ChildOf,
entityQuad, entityWorldMat, getMaskSelection, getSelectionMask, invert2D,
multiply2D, rectToQuad, rotate2D, scale2D, store, transformPoint,
translate2D,
@@ -38,6 +38,7 @@ import type { Mat2D } from '@diffusionstudio/runtime';
const ACCENT = '#008CFF';
const SNAP_COLOR = '#F43535';
+const ERROR_COLOR = '#FF8A8A';
const HEADER_FONT = '350 11px Inter, sans-serif';
const HEADER_HEIGHT = 22;
const ACTIVE_BADGE_WIDTH = 60;
@@ -53,7 +54,7 @@ export function hudSystem(world: World): void {
drawSnapLines(world, ctx, resolution);
drawHoverOutlines(world, ctx, resolution);
- for (const entity of world.query(Or(Name, Generating), ChildOf(world.get(Root)!), Not(Culled), Not(Hidden))) {
+ for (const entity of world.query(Or(Name, Generating, SourceError), ChildOf(world.get(Root)!), Not(Culled), Not(Hidden))) {
drawHeader(world, ctx, entity, resolution);
}
@@ -118,15 +119,16 @@ function drawHoverOutlines(world: World, ctx: Ctx2D, resolution: number): void {
* It sits in the node's own rotation, one line above its top edge.
*/
function drawHeader(world: World, ctx: Ctx2D, entity: Entity, resolution: number): void {
+ const failure = entity.get(SourceError)?.value;
const generating = entity.has(Generating);
- const label = generating ? 'Generating...' : entity.get(Name)?.value;
+ const label = failure || (generating ? 'Generating...' : entity.get(Name)?.value);
if (!label) return;
const header = getHeaderLayout(world, entity, resolution);
const playback = entity.get(Playback);
const selected = entity.has(Selected);
const active = entity.has(Active);
- const foreground = selected ? '#cce8ff' : 'rgba(242, 242, 242, 0.64)';
+ const foreground = failure ? ERROR_COLOR : selected ? '#cce8ff' : 'rgba(242, 242, 242, 0.64)';
const regions = world.get(HitRegions)!.list;
ctx.setTransform(header.mat.a, header.mat.b, header.mat.c, header.mat.d, header.mat.e, header.mat.f);
diff --git a/apps/web/src/engine/source-errors.ts b/apps/web/src/engine/source-errors.ts
new file mode 100644
index 00000000..75b13a08
--- /dev/null
+++ b/apps/web/src/engine/source-errors.ts
@@ -0,0 +1,47 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * A failed generation, written back to the JSX that asked for it. The runtime
+ * leaves the reason on the entity (`SourceError`); this is what turns it into
+ * the `error` prop of the element, so the failure lives where the rest of the
+ * document lives — in the file — rather than only in the session that saw it.
+ *
+ * That is what keeps a generation from being run again: the project remounts
+ * on every edit and is compiled afresh on every reload, and each time the
+ * element carries the answer its declaration already got (see the asset
+ * system, which leaves such an element alone). Taking the prop off the
+ * element — in the file, like any other authored change — is what asks for
+ * the run again; nothing else in the editor does it silently.
+ *
+ * Only generations are written: a load that failed is cheap to try again, and
+ * an asset that has since been put back should simply load.
+ */
+
+import { SourceError } from '@diffusionstudio/runtime';
+import { authoredElement } from '@diffusionstudio/reconciler';
+
+import { getDocumentEditor, isLooped } from './editor';
+
+import type { World } from 'koota';
+
+export function sourceErrorSystem(world: World): void {
+ for (const entity of world.query(SourceError)) {
+ const failure = entity.get(SourceError)!;
+ if (!failure.generated) continue;
+
+ const authored = authoredElement(entity);
+ // Nothing to write it to (an entity no element produced), or nothing
+ // new to say.
+ if (!authored || authored.props.error === failure.value) continue;
+
+ // One element renders every iteration of a loop, so writing a prop to
+ // one of them means unrolling the loop into the file. That is a change
+ // to the project worth making when the user asks for one, and not
+ // worth making behind their back over a generation that failed.
+ if (isLooped(entity)) continue;
+
+ getDocumentEditor(world).editProperty(entity, 'error', failure.value);
+ }
+}
diff --git a/apps/web/src/engine/timeline/constants.ts b/apps/web/src/engine/timeline/constants.ts
index 316338d8..01f9b128 100644
--- a/apps/web/src/engine/timeline/constants.ts
+++ b/apps/web/src/engine/timeline/constants.ts
@@ -127,5 +127,9 @@ export const COLORS = {
background: '#2B525F',
foreground: '#E7F4F9',
},
+ failed: {
+ background: '#2E1D1D',
+ foreground: '#FF8A8A',
+ },
},
} as const;
diff --git a/apps/web/src/engine/timeline/render/clip.ts b/apps/web/src/engine/timeline/render/clip.ts
index 72dd8ab6..a1098769 100644
--- a/apps/web/src/engine/timeline/render/clip.ts
+++ b/apps/web/src/engine/timeline/render/clip.ts
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { Caption, CaptionType, Chars, ClipDragOrigin, Computed, Generating, Hidden, Name, Selected, TrimDragOrigin, fitsChildren, getGeneratingColor, isCaption, isGroup, isText, store } from '@diffusionstudio/runtime';
+import { Caption, CaptionType, Chars, ClipDragOrigin, Computed, Generating, Hidden, Name, Selected, SourceError, TrimDragOrigin, fitsChildren, getGeneratingColor, isCaption, isGroup, isText, store } from '@diffusionstudio/runtime';
import { getDocumentEditor } from '../../editor';
import {
@@ -56,6 +56,7 @@ export function renderClip(
pointer.scope(String(entity.id()));
const asset = getClipAsset(world, entity);
+ const failed = entity.has(SourceError);
const style = getClipStyle(entity, asset);
handleBody(world, surface, entity, left, width, row, resolution);
@@ -75,7 +76,7 @@ export function renderClip(
ctx.fill();
ctx.restore();
- if (!generating) {
+ if (!generating && !failed) {
renderContent(world, scene, surface, entity, asset, style, row);
}
@@ -264,8 +265,12 @@ function renderLabel(
// A caption's row is mostly its words; below a certain height the label
// would be all there is room for, so the words win. One still generating
- // has no words yet, so there is nothing for the label to crowd out.
- if (isCaption(entity) && !entity.has(Generating) && row.height < CLIP_BREAKPOINTS.sm) return;
+ // (or one that failed) has no words yet, so there is nothing for the label
+ // to crowd out.
+ if (
+ isCaption(entity) && !entity.has(Generating) && !entity.has(SourceError)
+ && row.height < CLIP_BREAKPOINTS.sm
+ ) return;
const label = clipLabel(entity);
if (!label) return;
@@ -310,6 +315,9 @@ const CAPTION_PRESETS: Record<CaptionType, string> = {
* naming the thing it is drawing is an edit nobody asked for.
*/
function clipLabel(entity: Entity): string {
+ const failure = entity.get(SourceError)?.value;
+ if (failure) return failure;
+
if (entity.has(Generating)) return 'Generating...';
const name = entity.get(Name)?.value;
diff --git a/apps/web/src/engine/timeline/style.ts b/apps/web/src/engine/timeline/style.ts
index e16a5bde..69310281 100644
--- a/apps/web/src/engine/timeline/style.ts
+++ b/apps/web/src/engine/timeline/style.ts
@@ -2,14 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-/**
- * What colour a clip is. A clip is coloured by what it *is* rather than by
- * what it holds, so the tests run from the most specific kind outward — a
- * caption is text geometry too, and would read as plain text if text were
- * asked about first.
- */
-
import {
+ AssetId,
findGeometryAsset,
hasHtmlPaint,
hasSurfacePaint,
@@ -19,6 +13,7 @@ import {
isMask,
isScene,
isText,
+ SourceError,
} from '@diffusionstudio/runtime';
import { COLORS } from './constants';
@@ -34,6 +29,7 @@ export type ClipStyle = {
};
export function getClipStyle(entity: Entity, asset: Asset | null): ClipStyle {
+ if (entity.has(SourceError) && !entity.has(AssetId)) return COLORS.clip.failed;
if (isCaption(entity)) return COLORS.clip.caption;
if (isText(entity)) return COLORS.clip.text;
if (isScene(entity)) return COLORS.clip.scene;
diff --git a/apps/web/src/utils/gen-ai.ts b/apps/web/src/utils/gen-ai.ts
index cac4b800..8e234d41 100644
--- a/apps/web/src/utils/gen-ai.ts
+++ b/apps/web/src/utils/gen-ai.ts
@@ -2,14 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-// The app's GenAi: `generate.*` declarations made real against this
-// project's library and the dapi backend. Attached to the world as the `Ai`
-// trait, which is what the runtime's asset system (and `ai.generate.image`
-// callers) resolve through. A declaration resolves to an asset by content —
-// the same spec is the same asset, in this session and the next, since the
-// finished file lands in the library under `generated/` with the spec's key
-// in the manifest.
-
import { getAssetSpec, isAssetRef, parseSource } from "@diffusionstudio/jsx";
import {
Ai, AssetId, Audio, GenAi, getAssetFile, getEntityTree, Hidden, Muted,
@@ -27,6 +19,7 @@ import { assert } from "@/utils";
import { uploadBlob } from "@/components/engine";
import { track } from "@/lib/analytics";
import { trpc } from "@/lib/trpc";
+import { toast } from "somoto";
import type { AspectRatio, AssetInput, AssetRef, AssetSpecInput } from "@diffusionstudio/jsx";
import type { Asset, AssetLibrary } from "@diffusionstudio/assets";
@@ -35,6 +28,15 @@ import type { Entity, World } from "koota";
// The UI offers a fixed set of voices on a fixed model (see prompt-input.tsx).
const VOICE_MODEL = "elevenlabs-v3";
+/** What a failure is called where the user reads about it. */
+const FAILURE_TITLES: Record<AssetSpecInput["type"] | "transcript", string> = {
+ image: "Image generation failed",
+ video: "Video generation failed",
+ voice: "Voice generation failed",
+ audio: "Audio generation failed",
+ transcript: "Caption generation failed",
+};
+
/**
* A spec with defaults applied and every `AssetInput` reduced to an asset id.
* Field order is fixed, so `JSON.stringify` of it is a stable `generationKey`.
@@ -59,8 +61,9 @@ export class EditorGenAi extends GenAi {
/**
* Declarations already resolved, keyed by ref identity — a ref consumed by
- * several elements resolves (and validates) once. A failed one is
- * forgotten, so a remount tries again.
+ * several elements resolves (and validates) once. A failed one is forgotten:
+ * from there on the element that asked is what carries the failure, in its
+ * `error` prop, and that is what keeps the generation from running again.
*/
private readonly memo = new Map<AssetRef, Promise<Asset>>();
/** In-flight generations and transcriptions keyed by `generationKey`. */
@@ -105,6 +108,8 @@ export class EditorGenAi extends GenAi {
this.inflight.set(key, promise);
try {
return await promise;
+ } catch (error) {
+ throw reportFailure(error, FAILURE_TITLES.transcript);
} finally {
this.inflight.delete(key);
}
@@ -158,21 +163,27 @@ export class EditorGenAi extends GenAi {
}
private async generateFromRef(ref: AssetRef): Promise<Asset> {
- const resolved = await this.resolveSpec(getAssetSpec(ref));
- const generationKey = JSON.stringify(resolved);
+ const spec = getAssetSpec(ref);
- const cached = this.library.list().find((asset) => asset.generation?.key === generationKey);
- if (cached) return cached;
+ try {
+ const resolved = await this.resolveSpec(spec);
+ const generationKey = JSON.stringify(resolved);
- const running = this.inflight.get(generationKey);
- if (running) return await running;
+ const cached = this.library.list().find((asset) => asset.generation?.key === generationKey);
+ if (cached) return cached;
- const promise = this.runGeneration(resolved, generationKey);
- this.inflight.set(generationKey, promise);
- try {
- return await promise;
- } finally {
- this.inflight.delete(generationKey);
+ const running = this.inflight.get(generationKey);
+ if (running) return await running;
+
+ const promise = this.runGeneration(resolved, generationKey);
+ this.inflight.set(generationKey, promise);
+ try {
+ return await promise;
+ } finally {
+ this.inflight.delete(generationKey);
+ }
+ } catch (error) {
+ throw reportFailure(error, FAILURE_TITLES[spec.type]);
}
}
@@ -332,6 +343,26 @@ export class EditorGenAi extends GenAi {
}
}
+/**
+ * Says what a generation failed with, and hands the error on. The message is
+ * what the element ends up carrying too (the runtime's `SourceError`, which
+ * the editor writes into its `error` prop), so the sentence in the file, the
+ * one on the canvas and the one in the toast are the same. The toast is keyed
+ * by it: variants that failed the same way are one thing gone wrong rather
+ * than four, and a render that ran into the same wall does not stack up.
+ */
+function reportFailure(error: unknown, title: string): Error {
+ const failure = error instanceof Error ? error : new Error(String(error));
+
+ console.error(`[gen-ai] ${title}:`, failure);
+ toast.error(title, {
+ id: `gen-ai:${title}:${failure.message}`,
+ description: failure.message,
+ });
+
+ return failure;
+}
+
/**
* The transcript cache key: scene id + seed. The scene's durable name is the
* id in its source stamp (`<file>:<id>`, stamped once by the compiler — the
diff --git a/packages/jsx/src/types.ts b/packages/jsx/src/types.ts
index 695bbeba..50257f1c 100644
--- a/packages/jsx/src/types.ts
+++ b/packages/jsx/src/types.ts
@@ -283,6 +283,16 @@ type MediaProps = {
* transcribing the scene; `generate.*` is not accepted there.
*/
src: string | AssetRef;
+ /**
+ * Why this element's source never became an asset. Editor state carried by
+ * the source like `selected`, and the answer to the `src` it was written
+ * for: an element holding one is not resolved again, so a generation the
+ * model refused is not run — or paid for — a second time by every reopen of
+ * the project. The editor writes it when a generation or a transcription
+ * fails; taking it off the element is what asks for the run again, and
+ * nothing else does — not another take, not another prompt.
+ */
+ error?: string;
};
type FitProps = {
diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.ts
index 649e88a3..7ba1779e 100644
--- a/packages/reconciler/src/document.ts
+++ b/packages/reconciler/src/document.ts
@@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { Active, AdjustmentLayer, Animation, AnimationPhase, AnimationType, appendChild, AssetId, Audio, Background, bindAsset, BlendMode, BlendModeType, Blur, Caption, CaptionAlign, CaptionType, Chars, ClipHeight, ClipsContent, CornerRadius, createEntity, DEFAULT_BACKGROUND, Color, ColorStop, Effect, EffectType, End, Expanded, FontStyle, FrameRate, Generating, GenerationRequest, Loop, LoadRequest, Geometry, GeometryType, getEntityTree, getParentEntity, getParentNode, Hidden, IsMask, isText, ItemIndex, Keyframe, KeyframeTrack, MixedCornerRadius, Muted, Name, Offset, Opacity, Paint, PaintType, parseColor, PendingSource, Playback, PlaybackRate, Position, removeChild, resizeEntity, Scale, ScaleMode, ScaleModeType, secondsToFrames, getAsset, getEntityChildren, Group, Sequential, Shader, Size, Stage, Root, Rotation, Scene, Selected, Shadow, Source, SourceIn, SourceOut, setCameraMatrix, Start, Stroke, StrokeCap, StrokeJoin, StrokeStyle, SurfaceHost, SurfaceHostHandle, TextAlign, TextBaseline, TextCase, TextRange, TextStyle, TranscriptionRequest, Transition, TransitionType, UniformScale, Volume } from '@diffusionstudio/runtime';
+import { Active, AdjustmentLayer, Animation, AnimationPhase, AnimationType, appendChild, AssetId, Audio, Background, bindAsset, BlendMode, BlendModeType, Blur, Caption, CaptionAlign, CaptionType, Chars, ClipHeight, ClipsContent, CornerRadius, createEntity, DEFAULT_BACKGROUND, Color, ColorStop, Effect, EffectType, End, Expanded, FontStyle, FrameRate, Generating, GenerationRequest, Loop, LoadRequest, Geometry, GeometryType, getEntityTree, getParentEntity, getParentNode, Hidden, IsMask, isText, ItemIndex, Keyframe, KeyframeTrack, MixedCornerRadius, Muted, Name, Offset, Opacity, Paint, PaintType, parseColor, PendingSource, Playback, PlaybackRate, Position, removeChild, resizeEntity, Scale, ScaleMode, ScaleModeType, secondsToFrames, getAsset, getEntityChildren, Group, Sequential, Shader, Size, Stage, Root, Rotation, Scene, Selected, Shadow, Source, SourceError, SourceIn, SourceOut, setCameraMatrix, Start, Stroke, StrokeCap, StrokeJoin, StrokeStyle, SurfaceHost, SurfaceHostHandle, TextAlign, TextBaseline, TextCase, TextRange, TextStyle, TranscriptionRequest, Transition, TransitionType, UniformScale, Volume } from '@diffusionstudio/runtime';
import { LOOP_ATTR, parseTime, SOURCE_ATTR } from '@diffusionstudio/jsx';
import type { CameraMatrix, PropertyPath } from '@diffusionstudio/runtime';
@@ -1017,6 +1017,18 @@ export class RuntimeDocument implements ProjectDocument<HostNode> {
entity.set(LoadRequest, { value });
return;
}
+ case 'error': {
+ const message = typeof value === 'string' && value !== '' ? value : undefined;
+
+ if (message === undefined) {
+ entity.remove(SourceError);
+ return;
+ }
+
+ entity.add(SourceError);
+ entity.set(SourceError, { value: message, generated: true });
+ return;
+ }
case 'objectFit': {
const mode = typeof value === 'string' ? SCALE_MODES[value] : undefined;
if (mode === undefined) {
@@ -1163,8 +1175,7 @@ export class RuntimeDocument implements ProjectDocument<HostNode> {
if (entity.get(Authored)?.props.src !== undefined) return;
if (entity.has(LoadRequest) || entity.has(GenerationRequest)) return;
- // A resolution running for another seed must not bind late,
- // nor leave the wait it was marked with behind.
+ // A resolution running for another seed must not bind late
entity.remove(PendingSource, Generating);
entity.add(TranscriptionRequest);
entity.set(TranscriptionRequest, { seed: toNumber(value) ?? 0 });
diff --git a/packages/runtime/src/systems/assets.ts b/packages/runtime/src/systems/assets.ts
index a975699c..ea68dd7b 100644
--- a/packages/runtime/src/systems/assets.ts
+++ b/packages/runtime/src/systems/assets.ts
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { Ai, FramePromises, Generating, GenerationRequest, Library, LoadRequest, PendingSource, TranscriptionRequest } from '../traits';
+import { Ai, FramePromises, Generating, GenerationRequest, Library, LoadRequest, PendingSource, SourceError, TranscriptionRequest } from '../traits';
import { bindAsset } from '../actions/assets';
import { getEntityTree, getSceneAncestor } from '../queries/hierarchy';
@@ -24,7 +24,7 @@ export function assetSystem(world: World): void {
for (const entity of world.query(GenerationRequest)) {
const ref = entity.get(GenerationRequest)!.ref;
entity.remove(GenerationRequest);
- if (ref === null) continue;
+ if (ref === null || entity.has(SourceError)) continue;
resolve(world, entity, ref, ai.resolve(ref), true);
}
@@ -34,6 +34,7 @@ export function assetSystem(world: World): void {
const seed = entity.get(TranscriptionRequest)!.seed;
entity.remove(TranscriptionRequest);
+ if (entity.has(SourceError)) continue;
resolve(world, entity, `transcript:${scene.id()}:${seed}`, ai.transcribe(world, scene, seed), true);
}
}
@@ -60,6 +61,7 @@ function hasPendingSources(world: World, scene: Entity, except: Entity): boolean
* one that outlives its element (or its src) is dropped.
*/
function resolve(world: World, entity: Entity, value: unknown, promise: Promise<Asset>, generating = false): void {
+ entity.remove(SourceError);
entity.add(PendingSource);
entity.set(PendingSource, { value });
if (generating) entity.add(Generating);
@@ -73,6 +75,8 @@ function resolve(world: World, entity: Entity, value: unknown, promise: Promise<
(error: unknown) => {
if (!current(entity, value)) return;
entity.remove(PendingSource, Generating);
+ entity.add(SourceError);
+ entity.set(SourceError, { value: errorMessage(error), generated: generating });
console.error('[runtime] could not resolve src:', error);
},
);
@@ -80,6 +84,12 @@ function resolve(world: World, entity: Entity, value: unknown, promise: Promise<
world.get(FramePromises)?.list?.push(done);
}
+/** What a rejection says, for an entity to carry and the host to show. */
+function errorMessage(error: unknown): string {
+ const message = error instanceof Error ? error.message : String(error);
+ return message.trim() || 'Something went wrong';
+}
+
function current(entity: Entity, value: unknown): boolean {
return entity.isAlive() && entity.get(PendingSource)?.value === value;
}
diff --git a/packages/runtime/src/systems/render.ts b/packages/runtime/src/systems/render.ts
index fb71b950..1180549a 100644
--- a/packages/runtime/src/systems/render.ts
+++ b/packages/runtime/src/systems/render.ts
@@ -16,8 +16,9 @@ import {
TransitionType,
} from '../constants';
import {
- ChildOf, Hidden, Culled, Generating, Interactive, IsMask,
+ AssetId, ChildOf, Hidden, Culled, Generating, Interactive, IsMask,
ClipsContent, Geometry, Group, Paint, Color, Caption, ScaleMode, Shader,
+ SourceError,
BlendMode, Effect, Transition, MixedCornerRadius,
LocalTransform, WorldTransform, Computed, Cache,
HtmlHostHandle, SurfaceHostHandle,
@@ -28,7 +29,7 @@ import {
import { getParentNode } from '../queries/hierarchy';
import { getViewMatrix } from '../queries/camera';
import { colorToHex } from '../utils/color';
-import { getGeneratingColor } from '../utils/generating';
+import { FAILED_COLOR, getGeneratingColor } from '../utils/generating';
import { applyStrokeStyle } from '../utils/stroke';
import { renderText } from '../utils/text';
import { getTransitionWindow } from '../utils/transition';
@@ -551,12 +552,15 @@ function renderStrokes(world: World, entity: Entity): void {
}
}
-/** The pulse a node waiting on a generation is filled with (see `../utils/generating`). */
+/**
+ * The pulse a node waiting on a generation is filled with
+ */
function renderGenerating(world: World, entity: Entity): void {
- if (!entity.has(Generating)) return;
+ const failed = entity.has(SourceError) && !entity.has(AssetId);
+ if (!failed && !entity.has(Generating)) return;
const ctx = getCtx(world);
- ctx.fillStyle = getGeneratingColor(world);
+ ctx.fillStyle = failed ? FAILED_COLOR : getGeneratingColor(world);
ctx.fill();
}
diff --git a/packages/runtime/src/traits/node.ts b/packages/runtime/src/traits/node.ts
index a40395b3..408263ca 100644
--- a/packages/runtime/src/traits/node.ts
+++ b/packages/runtime/src/traits/node.ts
@@ -96,6 +96,19 @@ export const TranscriptionRequest = trait({ seed: 0 });
// (or none) is dropped. The document clears it whenever a new src arrives.
export const PendingSource = trait(() => ({ value: undefined as unknown }));
+// Why the entity's src never became an asset: the message of the rejection
+// the asset system saw. It stands until the element is authored without it
+// (the `error` prop) or a resolution for it starts — which, for a generation,
+// only happens once that prop is gone.
+//
+// `generated` tells a failed generation from a failed load, which are worth
+// different things: a load is cheap and idempotent, so it is simply tried
+// again next render, while a generation is neither. An element carrying one
+// of those is not resolved again (see the asset system), and the host is
+// expected to write it back to the source the element came from — which is
+// what makes the failure outlive the session that saw it.
+export const SourceError = trait({ value: '', generated: false });
+
// Sibling order under a ChildOf parent.
export const ItemIndex = trait({ value: 0 });
diff --git a/packages/runtime/src/utils/generating.ts b/packages/runtime/src/utils/generating.ts
index 044e4fec..d6a530eb 100644
--- a/packages/runtime/src/utils/generating.ts
+++ b/packages/runtime/src/utils/generating.ts
@@ -3,7 +3,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
- * Pulse animation for entities with the Generating tag. Everything that draws
+ * Pulse animation for entities with the Generating tag, and the still fill
+ * that takes its place when the wait ends in an error. Everything that draws
* one reads it from here — the stage and the timeline both — so a node
* waiting on a generation pulses in step wherever it is shown.
*
@@ -48,6 +49,13 @@ function generatingFactor(world: World): number {
return cycleT >= GEN_DURATION ? 1 - eased : eased;
}
+/**
+ * The fill a node whose source failed is left with: the pulse gone still and
+ * turned toward red, so a node that will never fill itself in is not mistaken
+ * for one that still might.
+ */
+export const FAILED_COLOR = '#2e1d1d';
+
/** The fill a node in generation is painted with this frame. */
export function getGeneratingColor(world: World): string {
const factor = generatingFactor(world);