Open source CapCut alternative.
TypeScript
feat(site): template replication fixes for the chat, effects and preview [rebuild]
David Le20cb2d1e87bd
diff --git a/site/packages/effects-kit/src/effects.ts b/site/packages/effects-kit/src/effects.tsindex dd201b4b..88f431d1 100644--- a/site/packages/effects-kit/src/effects.ts+++ b/site/packages/effects-kit/src/effects.ts@@ -125,8 +125,11 @@ const flashOn = (tLocal: number, rate: number, rhythm?: FlashRhythm) => {/** How a light leak plays: `drift` glows and wanders for as long as it is* up; `burn` is the film burn, played once across the element's length;* `scorch` flashes the picture through hot yellow, orange and red tones,- * a new one each frame. */-export const LEAK_COURSES = ["drift", "burn", "scorch"] as const;+ * a new one each frame; `film` is the film flash over a cut, a warm wash+ * with the strip's sprocket holes blazing along the top, played once across+ * the element; `char` is the burn's climb in dark stock — a hot orange front+ * cooling through ember to black — played once across the element. */+export const LEAK_COURSES = ["drift", "burn", "scorch", "film", "char"] as const;export type LeakCourse = (typeof LEAK_COURSES)[number];/** The kinds a glitch breaks the picture with. The random schedule deals the@@ -137,14 +140,14 @@ export type GlitchKind = (typeof GLITCH_KINDS)[number];/** The recipes the renderers draw: every picture effect, the burn a light* leak plays on its burn course, and a glitch pinned to one kind. */-type RecipeId = VisualEffectId | "burn" | "scorch" | `glitch:${GlitchKind}`;+type RecipeId = VisualEffectId | "burn" | "scorch" | "film" | "char" | `glitch:${GlitchKind}`;/** The recipe an effect element renders with. Every renderer resolves the* element through here, so a light leak on its burn course draws the burn* recipe, and a pinned glitch its kind, in the preview, the in-tab export and* the ffmpeg graph alike. */export const effectRecipe = (e: { effect: string; leak?: LeakCourse; glitch?: GlitchKind }): string =>- e.effect === "lightleak" && (e.leak === "burn" || e.leak === "scorch")+ e.effect === "lightleak" && (e.leak === "burn" || e.leak === "scorch" || e.leak === "film" || e.leak === "char")? e.leak: e.effect === "glitch" && e.glitch && (GLITCH_KINDS as readonly string[]).includes(e.glitch)? `glitch:${e.glitch}`@@ -350,6 +353,9 @@ export interface BurnStreak {p: number;w: number;alpha: number;+ /** The band's color when the burn's stock has its own (the char's light+ * line); the burn's flame color otherwise. */+ rgb?: [number, number, number];}/** The burn at one moment: its vertical profile, laid over the picture at@@ -368,6 +374,30 @@ const BURN_ORANGE: [number, number, number] = [247, 168, 58];const BURN_EDGE: [number, number, number] = [255, 106, 26];export const BURN_FLAME: [number, number, number] = [232, 120, 42];+/** A burn's stock: the body's three phases (`flare` while it climbs, `yellow`+ * as it cools, `orange` where it settles), the front's hot edge and the+ * flame band's color. */+export interface BurnPalette {+ flare: [number, number, number];+ yellow: [number, number, number];+ orange: [number, number, number];+ edge: [number, number, number];+ flame: [number, number, number];+}++const BURN_PALETTE: BurnPalette = { flare: BURN_FLARE, yellow: BURN_YELLOW, orange: BURN_ORANGE, edge: BURN_EDGE, flame: BURN_FLAME };++/** The char's dark stock: the body climbs hot orange, cools through ember to+ * char black, and a thin bright light line crosses it — film burning out on+ * black rather than flaring white. */+export const CHAR_PALETTE: BurnPalette = {+ flare: [240, 118, 30],+ yellow: [120, 38, 12],+ orange: [10, 7, 6],+ edge: [255, 112, 28],+ flame: [255, 176, 88],+};+/** How far the burned body runs under the front, how wide the hot edge is,* and how far its glow reaches above it — frame fractions. */const BURN_BODY = 0.08;@@ -393,10 +423,10 @@ const mixRgb = (a: [number, number, number], b: [number, number, number], p: num/** The burn's body color `u` of the way through: flare, then yellow, then* orange. */-function burnCore(u: number): [number, number, number] {- if (u < 0.4) return BURN_FLARE;- if (u < 0.6) return mixRgb(BURN_FLARE, BURN_YELLOW, smooth((u - 0.4) / 0.2));- return mixRgb(BURN_YELLOW, BURN_ORANGE, smooth((u - 0.6) / 0.25));+function burnCore(u: number, pal: BurnPalette): [number, number, number] {+ if (u < 0.4) return pal.flare;+ if (u < 0.6) return mixRgb(pal.flare, pal.yellow, smooth((u - 0.4) / 0.2));+ return mixRgb(pal.yellow, pal.orange, smooth((u - 0.6) / 0.25));}/**@@ -421,16 +451,16 @@ function clipStops(stops: BurnStop[]): BurnStop[] {}/** The burn `tLocal` seconds into an element `dur` long, at amount `k`. */-export function burnAt(tLocal: number, dur: number, k: number): BurnPaint {+export function burnAt(tLocal: number, dur: number, k: number, pal: BurnPalette = BURN_PALETTE): BurnPaint {const u = Math.max(0, Math.min(1, tLocal / Math.max(dur, 1e-3)));// The front climbs from the bottom edge until its glow has left the top.const h = (1 + BURN_GLOW + BURN_EDGE_W) * smooth(u / BURN_RISE);- const core = burnCore(u);+ const core = burnCore(u, pal);const stops = clipStops([{ at: h - BURN_BODY, rgb: core, a: 1 },- { at: h - BURN_EDGE_W, rgb: BURN_EDGE, a: 1 },- { at: h + BURN_GLOW, rgb: BURN_EDGE, a: 0 },+ { at: h - BURN_EDGE_W, rgb: pal.edge, a: 1 },+ { at: h + BURN_GLOW, rgb: pal.edge, a: 0 },]);// Opaque at full amount; the release hands the picture back.@@ -440,7 +470,8 @@ export function burnAt(tLocal: number, dur: number, k: number): BurnPaint {// The flame band crosses the frame while the burn flares.const B = BURN_STREAK;const f = (u - B.from) / B.span;- const streak = f > 0 && f < 1 ? { angle: B.angle, p: B.p0 - B.travel * f, w: B.w, alpha: B.alpha * Math.sin(Math.PI * f) } : undefined;+ const flame = pal === BURN_PALETTE ? {} : { rgb: pal.flame };+ const streak = f > 0 && f < 1 ? { angle: B.angle, p: B.p0 - B.travel * f, w: B.w, alpha: B.alpha * Math.sin(Math.PI * f), ...flame } : undefined;return streak ? { stops, alpha, streak } : { stops, alpha };}@@ -454,9 +485,9 @@ export const burnGradient = (b: BurnPaint) =>/** The CSS background of the flame band, at the band's own alpha. */export const burnStreakGradient = (s: BurnStreak) =>- `linear-gradient(${s.angle}deg, ${rgba(BURN_FLAME, 0)} ${((s.p - 2 * s.w) * 100).toFixed(1)}%, ` +- `${rgba(BURN_FLAME, 1)} ${(s.p * 100).toFixed(1)}%, ` +- `${rgba(BURN_FLAME, 0)} ${((s.p + 2 * s.w) * 100).toFixed(1)}%)`;+ `linear-gradient(${s.angle}deg, ${rgba(s.rgb ?? BURN_FLAME, 0)} ${((s.p - 2 * s.w) * 100).toFixed(1)}%, ` ++ `${rgba(s.rgb ?? BURN_FLAME, 1)} ${(s.p * 100).toFixed(1)}%, ` ++ `${rgba(s.rgb ?? BURN_FLAME, 0)} ${((s.p + 2 * s.w) * 100).toFixed(1)}%)`;/** The ellipse a wash covers, in frame fractions: center (x, y), radii (rx,* ry). The wash holds its full strength out to `inner` of the way to the rim@@ -682,16 +713,21 @@ function glitchRunAt(step: number, k: number): { kind: GlitchKind; len: number }return { kind: run.kind, len: run.min + Math.floor(stepHash(step, 6) * (run.max - run.min + 1)) };}+/** The amount a pinned glitch pulls its full travel at; the travel scales+ * with amount from there, so 0.15 is a faint fringe and 1 twice the pull. */+const PINNED_GLITCH_AMOUNT = 0.5;+/** What the glitch does on step `step` at amount `k`; null = a clean step.* The latest run to start plays until it ends, then the picture is clean* until the next one. A pinned glitch hits with its kind on every step, the- * row, shift and corner dealt fresh each step. */+ * row, shift and corner dealt fresh each step; with no clean steps to thin+ * out, its amount sets how far each hit pulls instead. */export function glitchHitAt(step: number, k: number, pin?: GlitchKind): GlitchHit | null {if (pin) {return {kind: pin,row: 0.15 + 0.7 * stepHash(step, 3),- shift: (stepHash(step, 4) < 0.5 ? -1 : 1) * (0.06 + 0.1 * stepHash(step, 5)),+ shift: (stepHash(step, 4) < 0.5 ? -1 : 1) * (0.06 + 0.1 * stepHash(step, 5)) * (k / PINNED_GLITCH_AMOUNT),focus: { x: 0.15 + 0.7 * stepHash(step, 7), y: 0.15 + 0.7 * stepHash(step, 8) },};}@@ -839,8 +875,12 @@ export function effectPreviewState(}case "burn":return { cssFilter: "", burn: burnAt(tLocal, dur ?? BURN_SECONDS, k) };+ case "char":+ return { cssFilter: "", burn: burnAt(tLocal, dur ?? BURN_SECONDS, k, CHAR_PALETTE) };case "scorch":return scorchState(tLocal, k);+ case "film":+ return filmState(tLocal, dur ?? FILM_SECONDS, k);case "shake": {const amp = SHAKE_AMP * k; // design pxreturn {@@ -1171,9 +1211,10 @@ export function applyEffectToCanvas(const [x1, y1] = pt(hi);const band = ctx.createLinearGradient(x0, y0, x1, y1);const at = (f: number) => (f - lo) / (hi - lo);- band.addColorStop(at(s.p - 2 * s.w), rgba(BURN_FLAME, 0));- band.addColorStop(at(s.p), rgba(BURN_FLAME, 1));- band.addColorStop(at(s.p + 2 * s.w), rgba(BURN_FLAME, 0));+ const flame = s.rgb ?? BURN_FLAME;+ band.addColorStop(at(s.p - 2 * s.w), rgba(flame, 0));+ band.addColorStop(at(s.p), rgba(flame, 1));+ band.addColorStop(at(s.p + 2 * s.w), rgba(flame, 0));over("source-over", s.alpha * b.alpha, (c) => {c.fillStyle = band;c.fillRect(0, 0, W, H);@@ -1530,8 +1571,12 @@ export function effectFilterLines(}case "burn":return burnFilterLines(inLabel, outLabel, k, start, end, chroma);+ case "char":+ return burnFilterLines(inLabel, outLabel, k, start, end, chroma, CHAR_PALETTE);case "scorch":return scorchFilterLines(inLabel, outLabel, k, start, end, tag, chroma);+ case "film":+ return filmFilterLines(inLabel, outLabel, k, start, end, tag, chroma);case "shake": {// The shaken copy renders on its own branch (overscaled, then cropped// with time-jittered offsets) and replaces the frame only inside the@@ -1750,17 +1795,95 @@ function scorchFilterLines(];}-function burnFilterLines(+/**+ * The film flash, a light leak's fourth course: the flash a film-strip+ * transition throws over a cut. The picture washes warm orange, a brighter+ * band glows across the lower middle, and the strip's sprocket holes blaze+ * cream along the top edge — rising, holding through the middle of the+ * element and handing the picture back by its end. A 3–5 frame element over+ * a cut is the template look. Every renderer lays the same screen washes,+ * so the preview, the in-tab export and the ffmpeg geq agree.+ */++/** Length the flash plays over when nothing gives it one: four frames. */+export const FILM_SECONDS = 4 / 30;++/** The warm wash over the whole frame, the glow band, and the holes: a+ * screen color, a peak alpha, and a patch (none = the whole frame). */+const FILM_WASHES: { color: [number, number, number]; alpha: number; area?: WashArea }[] = [+ { color: [240, 148, 58], alpha: 0.7 },+ { color: [255, 192, 112], alpha: 0.5, area: { x: 0.5, y: 0.62, rx: 0.9, ry: 0.22, inner: 0.2 } },+ ...[0.17, 0.5, 0.83].map((x) => ({+ color: [255, 246, 224] as [number, number, number],+ alpha: 1,+ area: { x, y: 0.05, rx: 0.09, ry: 0.09, inner: 0.6 },+ })),+];++/** Share of the element the flash spends rising, and again falling. */+const FILM_RAMP = 0.3;++/** The flash's strength `u` of the way through: up over the first ramp,+ * full through the middle, down over the last, e.g. 0.15 → 0.5. */+const filmGain = (u: number) => Math.max(0, Math.min(1, (1 - Math.abs(2 * u - 1)) / (2 * FILM_RAMP)));++function filmState(tLocal: number, dur: number, k: number): EffectPreviewState {+ const g = filmGain(dur > 0 ? tLocal / dur : 1) * k;+ return {+ cssFilter: "",+ washes: FILM_WASHES.map((w) => ({ color: hex(w.color), alpha: w.alpha * g, mode: "screen", area: w.area })),+ };+}++/** The flash in ffmpeg: one geq over the window screens each wash in the+ * canvas's order, its alpha the same gain of the frame's own time. */+function filmFilterLines(inLabel: string,outLabel: string,k: number,start: number,end: number,+ tag: string,chroma: ChainChroma+): string[] {+ if (end <= start) {+ return [`[${inLabel}]null[${outLabel}]`];+ }+ const max = chroma.depth === 10 ? 1023 : 255;+ const dur = end - start;+ // Register 1: the gain; register 2: the channel, 0..1, washed in turn.+ const gain = `st(1,${fmt(k)}*clip((1-abs(2*(T-${fmt(start)})/${fmt(dur)}-1))/${fmt(2 * FILM_RAMP)},0,1))`;+ const plane = (c: number) => {+ const washes = FILM_WASHES.map((w) => {+ const cover = w.area+ ? `*clip((1-hypot(((X+0.5)/W-${fmt(w.area.x)})/${fmt(w.area.rx)},((Y+0.5)/H-${fmt(w.area.y)})/${fmt(w.area.ry)}))/${fmt(1 - w.area.inner)},0,1)`+ : "";+ const b = fmt(w.color[c] / 255);+ return `st(2,ld(2)+${fmt(w.alpha)}*ld(1)${cover}*(${b}-ld(2)*${b}))`;+ });+ return `${gain};st(2,${"rgb"[c]}(X,Y)/${max});${washes.join(";")};${max}*ld(2)`;+ };+ const all = `enable='gte(t,${(start - STEP_SLACK).toFixed(5)})*lt(t,${(end - STEP_SLACK).toFixed(5)})'`;+ const rgb = chroma.depth === 10 ? "gbrp10le" : "gbrp";+ return [+ `[${inLabel}]split[flb${tag}][fls${tag}]`,+ `[fls${tag}]format=${rgb},geq=r='${plane(0)}':g='${plane(1)}':b='${plane(2)}':${all},format=${chroma.pixFmt}[flc${tag}]`,+ `[flb${tag}][flc${tag}]overlay=0:0:${all}:eof_action=pass${chroma.overlay}[${outLabel}]`,+ ];+}++function burnFilterLines(+ inLabel: string,+ outLabel: string,+ k: number,+ start: number,+ end: number,+ chroma: ChainChroma,+ pal: BurnPalette = BURN_PALETTE): string[] {const dur = Math.max(end - start, 1e-3);const smooth = (x: string) => `(st(1,clip(${x},0,1));ld(1)*ld(1)*(3-2*ld(1)))`;- const [flare, yellow, orange, edge, flame] = [BURN_FLARE, BURN_YELLOW, BURN_ORANGE, BURN_EDGE, BURN_FLAME].map((c) =>+ const [flare, yellow, orange, edge, flame] = [pal.flare, pal.yellow, pal.orange, pal.edge, pal.flame].map((c) =>yuvCodes(c, chroma.depth).map(Number));const S = BURN_STREAK;diff --git a/site/packages/effects-kit/src/glitchRgb.test.ts b/site/packages/effects-kit/src/glitchRgb.test.tsindex 4907399c..1fc982a1 100644--- a/site/packages/effects-kit/src/glitchRgb.test.ts+++ b/site/packages/effects-kit/src/glitchRgb.test.ts@@ -50,6 +50,21 @@ describe("a pinned glitch", () => {expect(effectRecipe({ effect: "flash", glitch: "rgb" })).toBe("flash");});+ test("its amount sets how far it pulls: the default 0.5 keeps the travel, lower pulls less, in every renderer", () => {+ for (const n of [0, 7, 19]) {+ const at = (k: number) => glitchHitAt(n, k, "rgb")!.shift;+ expect(at(0.15)).toBeCloseTo(at(0.5) * 0.3, 6);+ expect(Math.abs(at(0.5))).toBeGreaterThanOrEqual(0.06);+ }+ const dy = (k: number) => Math.abs(effectPreviewState("glitch:rgb", k, 0).rgb!.dy);+ expect(dy(0.15)).toBeCloseTo(dy(0.5) * 0.3, 6);+ // The ffmpeg graph moves the channels by the same whole pixels.+ const rv = (k: number) =>+ Math.abs(Number(/rgbashift=rh=-?\d+:rv=(-?\d+)/.exec(effectFilterLines("in", "out", "glitch:rgb", k, 0, 0.034, 1920, 1080, "t")!.join(";"))![1]));+ expect(rv(0.15)).toBe(Math.round(dy(0.15)));+ expect(rv(0.5)).toBe(Math.round(dy(0.5)));+ });+test("it hits on every step, where the random mix leaves clean ones", () => {const steps = [...Array(60).keys()];expect(steps.every((n) => glitchHitAt(n, 1, "rgb")?.kind === "rgb")).toBe(true);diff --git a/site/packages/effects-kit/src/gradeMatch.test.ts b/site/packages/effects-kit/src/gradeMatch.test.tsindex ed87446f..8056e326 100644--- a/site/packages/effects-kit/src/gradeMatch.test.ts+++ b/site/packages/effects-kit/src/gradeMatch.test.ts@@ -1,6 +1,6 @@import { describe, expect, test } from "bun:test";import { createGradeTransform } from "./gradeMath";-import { autoGradeFromImageData, colorStatsFromImageData, matchGrade } from "./gradeMatch";+import { autoGradeFromImageData, colorStatsFromImageData, matchGrade, type ColorStats } from "./gradeMatch";/** A synthetic frame with tonal and color variety. */function testFrame(): Uint8ClampedArray {@@ -81,6 +81,56 @@ describe("matchGrade", () => {}});+ test("a reference of other content never posterizes: every curve segment keeps a bounded slope", () => {+ // A shadow on a blue-and-orange wall matched to a shadow on beige+ // pavement: the green quantiles jump 54 -> 164 across eight code values,+ // which as a curve tears the wall into flat bands of false colour.+ const stats = (r: number[], g: number[], b: number[]): ColorStats => ({+ r,+ g,+ b,+ luma: g,+ meanR: r[3],+ meanG: g[3],+ meanB: b[3],+ meanSat: 0.3,+ warmth: 1,+ });+ const source = stats([50, 59, 126, 153, 227, 230, 232], [42, 52, 113, 147, 155, 160, 171], [23, 48, 60, 74, 132, 149, 161]);+ const reference = stats([33, 37, 40, 48, 203, 209, 213], [39, 45, 47, 54, 164, 173, 178], [40, 45, 48, 55, 135, 144, 150]);+ const grade = matchGrade(source, reference)!;+ for (const ch of ["r", "g", "b"] as const) {+ const pts = grade.curves?.[ch];+ expect(pts).toBeDefined();+ for (let i = 1; i < pts!.length; i++) {+ const slope = (pts![i][1] - pts![i - 1][1]) / (pts![i][0] - pts![i - 1][0]);+ // Rounding to whole code values moves a short segment's slope a little.+ const slack = 1.5 / (pts![i][0] - pts![i - 1][0]);+ expect(slope).toBeLessThanOrEqual(3 + slack);+ if (pts![i][1] < 255 && pts![i - 1][1] > 0) expect(slope).toBeGreaterThanOrEqual(1 / 3 - slack);+ }+ }+ // The middle of the range still lands where the reference puts it.+ const mid = grade.curves!.g!.find(([x]) => x === 147)!;+ expect(mid[1]).toBe(54);+ });++ test("strength scales the match: half strength moves every curve point half as far", () => {+ const src = testFrame();+ const ref = graded(src, (v, c) => (c === 0 ? v * 1.3 : c === 2 ? v * 0.7 : v));+ const srcStats = colorStatsFromImageData(src)!;+ const refStats = colorStatsFromImageData(ref)!;+ const full = matchGrade(srcStats, refStats)!;+ const half = matchGrade(srcStats, refStats, 0.5)!;+ for (const ch of ["r", "b"] as const) {+ const f = full.curves![ch]!;+ const h = half.curves![ch]!;+ expect(h.map(([x]) => x)).toEqual(f.map(([x]) => x));+ for (let i = 0; i < f.length; i++) expect(Math.abs(h[i][1] - (f[i][0] + f[i][1]) / 2)).toBeLessThanOrEqual(0.5);+ }+ expect(matchGrade(srcStats, refStats, 0)).toBeUndefined();+ });+test("stats expose warmth and saturation for the assistant", () => {const stats = colorStatsFromImageData(testFrame())!;expect(stats.meanSat).toBeGreaterThan(0);diff --git a/site/packages/effects-kit/src/gradeMatch.ts b/site/packages/effects-kit/src/gradeMatch.tsindex 306a5714..6435584f 100644--- a/site/packages/effects-kit/src/gradeMatch.ts+++ b/site/packages/effects-kit/src/gradeMatch.ts@@ -184,13 +184,24 @@ export function autoGradeFromImageData(data: Uint8ClampedArray): ColorGrade | un/* Reference matching *//* ------------------------------------------------------------------ */+/** The steepest a matched curve may climb, and (inverted) the flattest, per+ * segment. Quantiles of a reference with other content — a beige pavement+ * for a painted wall — can jump a hundred code values across a few, and a+ * curve that steep tears smooth gradients into flat bands of false colour;+ * one that flat crushes a channel while its neighbours keep climbing, so the+ * channels cross and highlights change hue. */+export const MATCH_SLOPE_MAX = 2.5;+function quantileCurve(src: number[], ref: number[]): CurvePoint[] | undefined {const pts: CurvePoint[] = [];+ const median = STAT_QUANTILES.indexOf(0.5);+ let anchor = 0;let lastX = -1;for (let i = 0; i < src.length; i++) {const x = Math.round(src[i]);const y = Math.round(ref[i]);if (x <= lastX) continue;+ if (i <= median) anchor = pts.length;pts.push([x, y]);lastX = x;}@@ -198,6 +209,19 @@ function quantileCurve(src: number[], ref: number[]): CurvePoint[] | undefined {for (let i = 1; i < pts.length; i++) {if (pts[i][1] < pts[i - 1][1]) pts[i][1] = pts[i - 1][1];}+ // The median lands where the reference puts it; outward from it each+ // segment's slope stays inside [1/MATCH_SLOPE_MAX, MATCH_SLOPE_MAX].+ const clampY = (y: number) => Math.max(0, Math.min(255, Math.round(y)));+ for (let i = anchor + 1; i < pts.length; i++) {+ const dx = pts[i][0] - pts[i - 1][0];+ const y = pts[i - 1][1];+ pts[i][1] = clampY(Math.min(y + dx * MATCH_SLOPE_MAX, Math.max(y + dx / MATCH_SLOPE_MAX, pts[i][1])));+ }+ for (let i = anchor - 1; i >= 0; i--) {+ const dx = pts[i + 1][0] - pts[i][0];+ const y = pts[i + 1][1];+ pts[i][1] = clampY(Math.max(y - dx * MATCH_SLOPE_MAX, Math.min(y - dx / MATCH_SLOPE_MAX, pts[i][1])));+ }if (pts.length < 2 || pts.every(([x, y]) => Math.abs(x - y) <= 2)) return undefined;// The spline holds flat outside its end points, so a curve that stops at the// p2/p98 probes would crush everything darker and brighter into those two@@ -218,16 +242,29 @@ function quantileCurve(src: number[], ref: number[]): CurvePoint[] | undefined {return pts;}+/** Move each curve point `strength` of the way from identity to where the+ * match put it. */+function scaleCurve(pts: CurvePoint[] | undefined, strength: number): CurvePoint[] | undefined {+ if (!pts || strength >= 1) return pts;+ const out = pts.map(([x, y]) => [x, Math.round(x + (y - x) * strength)] as CurvePoint);+ return out.every(([x, y]) => Math.abs(x - y) <= 2) ? undefined : out;+}+/*** Compute the grade that moves `source` toward `reference`: per-channel* quantile curves, or a saturation delta when the tones already agree.- * Returns undefined when the frames already agree.+ * `strength` (0..1) takes the match part of the way — for a reference whose+ * content differs from the clip's (a bright window for a dark room), where+ * the full match lifts or crushes what the reference never had. Returns+ * undefined when the frames already agree.*/-export function matchGrade(source: ColorStats, reference: ColorStats): ColorGrade | undefined {+export function matchGrade(source: ColorStats, reference: ColorStats, strength = 1): ColorGrade | undefined {+ const k = Math.max(0, Math.min(1, strength));+ if (k === 0) return undefined;const grade: ColorGrade = {};- const r = quantileCurve(source.r, reference.r);- const g = quantileCurve(source.g, reference.g);- const b = quantileCurve(source.b, reference.b);+ const r = scaleCurve(quantileCurve(source.r, reference.r), k);+ const g = scaleCurve(quantileCurve(source.g, reference.g), k);+ const b = scaleCurve(quantileCurve(source.b, reference.b), k);if (r || g || b) {grade.curves = {};if (r) grade.curves.r = r;@@ -238,7 +275,7 @@ export function matchGrade(source: ColorStats, reference: ColorStats): ColorGrad// channels, so the saturation term is for frames that agree in tone and// differ in chroma alone.if (!grade.curves && source.meanSat > 0.01) {- const delta = Math.round((reference.meanSat / source.meanSat - 1) * GRADE_MAX);+ const delta = Math.round((reference.meanSat / source.meanSat - 1) * GRADE_MAX * k);if (Math.abs(delta) >= 2) {grade.saturation = Math.max(-GRADE_MAX, Math.min(GRADE_MAX, delta));}diff --git a/site/packages/effects-kit/src/leakChar.test.ts b/site/packages/effects-kit/src/leakChar.test.tsnew file mode 100644index 00000000..fcbc1f0e--- /dev/null+++ b/site/packages/effects-kit/src/leakChar.test.ts@@ -0,0 +1,91 @@+import { describe, expect, test } from "bun:test";+import { spawnSync } from "node:child_process";+import { Canvas } from "skia-canvas";+import { TEST_FFMPEG } from "./testing/ffmpeg";+import { applyEffectToCanvas, burnAt, effectFilterLines, effectPreviewState, effectRecipe } from "./effects";++/**+ * The char, a light leak's fifth course: the film burn's climb in dark+ * stock — a hot orange front rises from the bottom edge, cools through ember+ * to char black and hands the picture back. The handover out of a black+ * card in a recap opener. The canvas pass and the ffmpeg graph draw the same+ * frames.+ */++const W = 36;+const H = 64;+const FRAMES = 8;+const DUR = FRAMES / 30;++function white(): HTMLCanvasElement {+ const c = new Canvas(W, H) as unknown as HTMLCanvasElement;+ const ctx = c.getContext("2d")!;+ ctx.fillStyle = "#ffffff";+ ctx.fillRect(0, 0, W, H);+ return c;+}++function canvasFrame(t: number): Uint8ClampedArray {+ const c = white();+ applyEffectToCanvas(c, white(), "char", 1, t, () => null, undefined, undefined, DUR);+ return c.getContext("2d")!.getImageData(0, 0, W, H).data;+}++const rgbAt = (d: Uint8ClampedArray, x: number, y: number) => Array.from(d.subarray((y * W + x) * 4, (y * W + x) * 4 + 3));++describe("the char", () => {+ test("a light leak on its char course renders the char", () => {+ expect(effectRecipe({ effect: "lightleak", leak: "char" })).toBe("char");+ });++ test("it climbs as the burn does, and plays across the element's own length", () => {+ const char = effectPreviewState("char", 1, 0.1, undefined, undefined, 1).burn!;+ expect(char.stops.map((s) => [s.at, s.a])).toEqual(burnAt(0.1, 1, 1).stops.map((s) => [s.at, s.a]));+ });++ test("early on a hot orange front has caught the bottom; the top still shows the picture", () => {+ const d = canvasFrame(DUR * 0.12);+ const [r, g, b] = rgbAt(d, W / 2, H - 2);+ expect(r).toBeGreaterThan(200);+ expect(r - b).toBeGreaterThan(150);+ expect(g).toBeLessThan(170);+ expect(rgbAt(d, W / 2, 1)).toEqual([255, 255, 255]);+ });++ test("it cools to char black, never a pale flare, and the picture comes back at the end", () => {+ for (let i = 0; i <= 20; i++) {+ const paint = effectPreviewState("char", 1, (i / 20) * DUR, undefined, undefined, DUR).burn!;+ for (const s of paint.stops) expect(s.rgb[2]).toBeLessThan(60);+ }+ const late = canvasFrame(DUR * 0.8);+ expect(Math.max(...rgbAt(late, W / 2, H / 2))).toBeLessThan(50);+ expect(effectPreviewState("char", 1, DUR, undefined, undefined, DUR).burn!.alpha).toBe(0);+ });++ test("the ffmpeg recipe draws the same frames", () => {+ const chroma = { pixFmt: "yuv444p", overlay: ":format=yuv444" };+ const lines = effectFilterLines("in", "out", "char", 1, 0, DUR, W, H, "t", undefined, undefined, chroma)!;+ const graph = `color=c=white:s=${W}x${H}:r=30:d=${DUR},format=yuv444p[in];${lines.join(";")}`;+ const run = spawnSync(TEST_FFMPEG, ["-v", "error", "-filter_complex", graph, "-map", "[out]", "-pix_fmt", "rgb24", "-f", "rawvideo", "-"]);+ expect(run.stderr.toString()).toBe("");+ expect(run.status).toBe(0);+ for (let i = 0; i < FRAMES; i++) {+ const ref = canvasFrame(i / 30);+ let sum = 0;+ let n = 0;+ for (let y = 1; y < H; y += 3) {+ for (let x = 1; x < W; x += 3) {+ const at = (i * W * H + y * W + x) * 3;+ const ours = Array.from(run.stdout.subarray(at, at + 3));+ rgbAt(ref, x, y).forEach((v, c) => {+ sum += Math.abs(ours[c] - v);+ n++;+ });+ }+ }+ // The export blends the burn in YUV and the canvas in RGB, so the+ // front's glow over a white picture lands a few codes apart.+ expect(sum / n).toBeLessThan(10);+ }+ });+});diff --git a/site/packages/effects-kit/src/leakFilm.test.ts b/site/packages/effects-kit/src/leakFilm.test.tsnew file mode 100644index 00000000..a062e740--- /dev/null+++ b/site/packages/effects-kit/src/leakFilm.test.ts@@ -0,0 +1,84 @@+import { describe, expect, test } from "bun:test";+import { spawnSync } from "node:child_process";+import { Canvas } from "skia-canvas";+import { TEST_FFMPEG } from "./testing/ffmpeg";+import { applyEffectToCanvas, effectFilterLines, effectPreviewState, effectRecipe } from "./effects";++/**+ * The film flash, a light leak's fourth course: for a few frames over a cut+ * the picture washes warm orange and the film strip's sprocket holes blaze+ * along the top edge, then everything hands back. The canvas pass and the+ * ffmpeg graph draw the same washes on the same frames.+ */++const S = 64;+const FRAMES = 6;+const DUR = FRAMES / 30;++function black(): HTMLCanvasElement {+ const c = new Canvas(S, S) as unknown as HTMLCanvasElement;+ const ctx = c.getContext("2d")!;+ ctx.fillStyle = "#000000";+ ctx.fillRect(0, 0, S, S);+ return c;+}++function canvasFrame(t: number): Uint8ClampedArray {+ const c = black();+ applyEffectToCanvas(c, black(), "film", 1, t, () => null, undefined, undefined, DUR);+ return c.getContext("2d")!.getImageData(0, 0, S, S).data;+}++const rgbAt = (d: Uint8ClampedArray, x: number, y: number) => Array.from(d.subarray((y * S + x) * 4, (y * S + x) * 4 + 3));++describe("the film flash", () => {+ test("a light leak on its film course renders the film flash", () => {+ expect(effectRecipe({ effect: "lightleak", leak: "film" })).toBe("film");+ });++ test("it peaks mid-element and leaves the picture alone at its edges", () => {+ const edge = effectPreviewState("film", 1, 0, undefined, undefined, DUR);+ const mid = effectPreviewState("film", 1, DUR / 2, undefined, undefined, DUR);+ expect(Math.max(0, ...(edge.washes ?? []).map((w) => w.alpha))).toBeLessThan(0.05);+ expect(Math.max(...(mid.washes ?? []).map((w) => w.alpha))).toBeGreaterThan(0.8);+ });++ test("sprocket holes blaze along the top over a warm wash", () => {+ const d = canvasFrame(DUR / 2);+ const holes = effectPreviewState("film", 1, DUR / 2, undefined, undefined, DUR).washes!.filter((w) => w.area && w.area.y < 0.15);+ expect(holes.length).toBeGreaterThanOrEqual(3);+ for (const h of holes) {+ const [r, g, b] = rgbAt(d, Math.floor(h.area!.x * S), Math.floor(h.area!.y * S));+ expect(Math.min(r, g, b)).toBeGreaterThan(200);+ }+ // Between the holes and down the frame: warm, not white.+ const [r, g, b] = rgbAt(d, S / 2, Math.floor(S * 0.6));+ expect(r).toBeGreaterThan(110);+ expect(r - b).toBeGreaterThan(60);+ expect(b).toBeLessThan(150);+ });++ test("the ffmpeg recipe draws the same frames", () => {+ const chroma = { pixFmt: "yuv444p", overlay: ":format=yuv444" };+ const lines = effectFilterLines("in", "out", "film", 1, 0, DUR, S, S, "t", undefined, undefined, chroma)!;+ expect(lines).not.toBeNull();+ const graph = `color=c=black:s=${S}x${S}:r=30:d=${DUR},format=yuv444p[in];${lines.join(";")}`;+ const run = spawnSync(TEST_FFMPEG, ["-v", "error", "-filter_complex", graph, "-map", "[out]", "-pix_fmt", "rgb24", "-f", "rawvideo", "-"]);+ expect(run.stderr.toString()).toBe("");+ expect(run.status).toBe(0);+ let checked = 0;+ for (let i = 0; i < FRAMES; i++) {+ const ref = canvasFrame(i / 30);+ const base = i * S * S * 3;+ for (let y = 1; y < S; y += 4) {+ for (let x = 1; x < S; x += 4) {+ const at = base + (y * S + x) * 3;+ const ours = Array.from(run.stdout.subarray(at, at + 3));+ rgbAt(ref, x, y).forEach((v, c) => expect(Math.abs(ours[c] - v)).toBeLessThanOrEqual(12));+ checked++;+ }+ }+ }+ expect(checked).toBe(FRAMES * 256);+ });+});diff --git a/site/src/cut/components/AiPanel.tools.ts b/site/src/cut/components/AiPanel.tools.tsindex 1f75e990..c40c8292 100644--- a/site/src/cut/components/AiPanel.tools.ts+++ b/site/src/cut/components/AiPanel.tools.ts@@ -10,6 +10,8 @@import { bool, num, obj, str, type AiToolDef } from "@/cut/lib/aiToolDef";import { WATCH_DETAIL_NOTES, WATCH_DETAILS } from "@/cut/lib/types";import { FACT_CLAIM_MAX_CHARS, FACT_CLAIMS_MAX } from "@/lib/inference/factCheck";+import { URL_BATCH_MAX } from "@/cut/lib/urlBatch";+import { FIND_MEDIA_DEFAULT, FIND_MEDIA_MAX, FIND_MEDIA_SNAPSHOT_CAP } from "@/cut/lib/findMedia";export const AI_PANEL_TOOLS = [{@@ -24,6 +26,17 @@ export const AI_PANEL_TOOLS = ["Read a project asset's file name, kind, readable fileSize (KB, MB, GB), exact sizeBytes, duration in seconds, and pixel dimensions. Present fileSize in replies; include exact bytes only when asked. Use this for file-size questions when sizeBytes is absent from the media snapshot. Reads file metadata across browser, Mac, and cloud storage.",inputSchema: obj({ asset_id: str("Project asset id from media or an import result") }, ["asset_id"]),},+ {+ name: "find_media",+ description:+ `Find project media by name: every asset whose name or file name contains query (any case), optionally of one kind, with its id. The context snapshot lists at most ${FIND_MEDIA_SNAPSHOT_CAP} media items (mediaTruncated says when it cut the rest), so use this to reach the others — say the reference images a person just uploaded to a long project. Pages: pass the answer's next as offset for more.`,+ inputSchema: obj({+ query: str("Text the name or file name contains (omit for all)"),+ type: { type: "string", enum: ["video", "image", "audio"], description: "Only this kind of media" },+ limit: num(`How many to return, 1..${FIND_MEDIA_MAX} (default ${FIND_MEDIA_DEFAULT})`),+ offset: num("Skip this many matches (the previous answer's next)"),+ }, []),+ },{name: "watch_video",description:@@ -175,11 +188,17 @@ export const AI_PANEL_TOOLS = [inputSchema: obj({url: str("The page or media URL to download"),+ urls: {+ type: "array",+ items: { type: "string" },+ maxItems: URL_BATCH_MAX,+ description: `Several direct media links at once (up to ${URL_BATCH_MAX}), instead of url — a section's stock footage, say. They download two at a time and come back as results in the order given, each with its assets or its own error, so one dead link never fails the rest. A call answers within about 90s: a link still downloading then comes back pending (it lands in the project on its own — don't import it again; it shows up in media) and links it never reached come back in notStarted — call again with exactly those. Pass this rather than calling import_url once per link`,+ },audio_only: bool("Import only the source's audio track, extracted to an audio file (default false)"),},- ["url"]+ []),},{diff --git a/site/src/cut/components/ColorPanel.tsx b/site/src/cut/components/ColorPanel.tsxindex cb091c56..16e820e5 100644--- a/site/src/cut/components/ColorPanel.tsx+++ b/site/src/cut/components/ColorPanel.tsx@@ -203,17 +203,19 @@ function SourceColorRow({ clip }: { clip: VideoClip }) {);}-/** Where a ProRes master's preview is coming from: the proxy being made, or+/** Where a proxied video's preview is coming from: the proxy being made, or* the master itself when no proxy could be made here. Silent once the proxy- * is in place, and for every other kind of file. */+ * is in place, and for every file no proxy was asked for. */function ProxyLine({ asset }: { asset: MediaAsset }) {const job = useProxyJobs((s) => s.jobs[asset.id]);- if (!job || !needsProxy(asset)) return null;+ // A job only exists for a file that needed a proxy where it was asked.+ if (!job || !needsProxy(asset, true)) return null;+ const master = asset.color?.codec?.startsWith("ap") ? "the ProRes master" : "the original file";const text =job.kind === "making"? `Preview proxy: ${Math.round(job.progress * 100)}%`: job.kind === "none"- ? "No preview proxy on this browser. The preview plays the ProRes master."+ ? `No preview proxy on this browser. The preview plays ${master}.`: `Preview proxy failed: ${job.error}`;return <p className="pb-2 text-[11px] leading-snug text-muted-foreground">{text}</p>;}diff --git a/site/src/cut/components/EffectsPanel.tools.ts b/site/src/cut/components/EffectsPanel.tools.tsindex 20acfd99..ae6d0318 100644--- a/site/src/cut/components/EffectsPanel.tools.ts+++ b/site/src/cut/components/EffectsPanel.tools.ts@@ -15,11 +15,38 @@ export const EFFECT_TREATMENTS = EFFECT_IDS.filter((id) => id !== "zoom" && !(LOOK_EFFECTS as string[]).includes(id));+/** An effect's own settings, each for one kind of effect: a light leak's+ * course, a glitch's pinned kind, a flash's tone and strobe. add_effect+ * places an effect with them and update_overlay changes them in place. */+export const EFFECT_SETTING_ARGS = {+ leak: {+ type: "string",+ enum: [...LEAK_COURSES],+ description: 'lightleak only: "drift" glows and wanders for the whole element (default), "burn" is the film burn played once across it, "scorch" flashes hot burn tones through the picture frame by frame, "film" is the film-strip flash over a cut (warm wash, sprocket holes along the top) played once across it, "char" is the burn climb in dark stock (orange front, ember, black, light line) played once across it',+ },+ glitch: {+ type: "string",+ enum: [...GLITCH_KINDS],+ description: 'glitch only: pin every frame to one kind; "rgb" is the channel split. Omit for the random mix. A pinned rgb or split hits every frame, so amount sets how far it pulls: 0.5 is the full split, 0.15 a faint fringe',+ },+ tone: {+ type: "string",+ enum: [...FLASH_TONES],+ description: 'flash only: "white" pops the picture bright (default), "black" dips it dark',+ },+ rate: num(`flash only: strobe pulses a second, ${FLASH_RATE_MIN}..${FLASH_RATE_MAX}; each pulse holds the tone for its first half. Omit for one pop at the start`),+ rhythm: {+ type: "string",+ enum: [...FLASH_RHYTHMS],+ description: 'flash with a rate only: "strobe" fires every pulse at full strength (default), "flicker" deals each pulse its own strength and skips some',+ },+};+export const EFFECTS_TOOLS = [{name: "add_effect",description:- `Add a time-ranged effect element over the cut: a zoom into part of the frame, a picture treatment (${EFFECT_TREATMENTS.join(", ")}), a graded look (${LOOK_EFFECTS.join(", ")}), or an audio treatment (${AUDIO_EFFECT_IDS.join(", ")}) over the sound. A picture effect filters everything visible under its window (start..end); an audio effect treats everything audible under it — clip sound, upper tracks and soundtrack together. They stack. glitch is clean footage broken by runs of corrupted frames — dark pixel-stretch smears, the picture doubled sideways over an orange burn, teal close-ups, blown-out flashes — the glitch ending of a teaser, where amount is how often it hits. A glitch pinned to one kind (glitch "smear", "split", "tint", "blowout" or "rgb") hits with it on every frame; "rgb" pulls the color channels apart, red down and blue up — a 2–3 frame rgb glitch is the RGB-split hit of a collapsing title or HUD. lightleak drifts a warm bloom and light streaks over the picture for as long as it is up; with leak "burn" it is a film burn played once across the element's own length: it climbs from the bottom edge behind a hot orange front, flares near white, settles orange and hands the picture back in its last tenth — the burn-out of a teaser, a 0.4–0.6s element over the end of the last shot, amount 1 for a fully opaque burn. With leak "scorch" it flashes film burn through the picture: every frame maps the picture onto a saturated hot tone by brightness — yellow, orange, or red over pink — across the whole frame or a soft-edged patch of it, a new tone and patch each frame; short 2–12 frame scorches over the last shots, amount 1, are the burn-through flicker of a teaser's ending. flash pops the picture white at the element's start and decays in about 0.4s; tone "black" dips it dark instead, and a strobe rate holds the tone on and off that many times a second for the whole element — a black strobe at 5–6 with amount 0.9–1 is the dark beat-flicker over a teaser's last shot. rhythm "flicker" deals each pulse its own strength and leaves some dark: an uneven exposure flicker, a white flicker at rate 12–15 and amount 0.15–0.25. To flicker one shot inside its mask, give the clip the flash through set_clip_effects. Tasteful default: one effect at a time, amount under ~0.5.`,+ `Add a time-ranged effect element over the cut: a zoom into part of the frame, a picture treatment (${EFFECT_TREATMENTS.join(", ")}), a graded look (${LOOK_EFFECTS.join(", ")}), or an audio treatment (${AUDIO_EFFECT_IDS.join(", ")}) over the sound. A picture effect filters everything visible under its window (start..end); an audio effect treats everything audible under it — clip sound, upper tracks and soundtrack together. They stack. glitch is clean footage broken by runs of corrupted frames — dark pixel-stretch smears, the picture doubled sideways over an orange burn, teal close-ups, blown-out flashes — the glitch ending of a teaser, where amount is how often it hits. A glitch pinned to one kind (glitch "smear", "split", "tint", "blowout" or "rgb") hits with it on every frame; "rgb" pulls the color channels apart, red down and blue up — a 2–3 frame rgb glitch is the RGB-split hit of a collapsing title or HUD. lightleak drifts a warm bloom and light streaks over the picture for as long as it is up; with leak "burn" it is a film burn played once across the element's own length: it climbs from the bottom edge behind a hot orange front, flares near white, settles orange and hands the picture back in its last tenth — the burn-out of a teaser, a 0.4–0.6s element over the end of the last shot, amount 1 for a fully opaque burn. With leak "scorch" it flashes film burn through the picture: every frame maps the picture onto a saturated hot tone by brightness — yellow, orange, or red over pink — across the whole frame or a soft-edged patch of it, a new tone and patch each frame; short 2–12 frame scorches over the last shots, amount 1, are the burn-through flicker of a teaser's ending. With leak "film" it is the film-strip flash over a cut, played once across the element: the picture washes warm orange, a brighter band glows across the lower middle and the strip's sprocket holes blaze cream along the top edge, full through the middle of the element and gone by its ends — a 3–5 frame element centered on a cut, amount 1, is the film-strip light-leak transition of a recap montage (one per section change). With leak "char" it is the burn's climb in dark stock: a hot orange front rises from the bottom edge, the frame cools through ember to black behind it, a thin bright light line crosses, and the picture comes back in the last tenth — never a pale flare; an 8–10 frame element, amount 1, over a black card's end and the next shot's first frames is the dark film-burn handover of a recap opener. flash pops the picture white at the element's start and decays in about 0.4s; tone "black" dips it dark instead, and a strobe rate holds the tone on and off that many times a second for the whole element — a black strobe at 5–6 with amount 0.9–1 is the dark beat-flicker over a teaser's last shot. rhythm "flicker" deals each pulse its own strength and leaves some dark: an uneven exposure flicker, a white flicker at rate 12–15 and amount 0.15–0.25. To flicker one shot inside its mask, give the clip the flash through set_clip_effects. Tasteful default: one effect at a time, amount under ~0.5.`,inputSchema: obj({effect: { type: "string", enum: [...ALL_EFFECT_IDS], description: "Effect id" },start: num("Start time s (default: playhead)"),@@ -27,27 +54,7 @@ export const EFFECTS_TOOLS = [amount: num("Strength 0.05..1 (default 0.5). For zoom it is the depth: 0.25 shallow, 0.5 moderate, 1 deep; for an audio effect it is how far the treatment goes"),focus_x: num("Zoom only: the x of the point to zoom into, 0..1 (default 0.5)"),focus_y: num("Zoom only: the y of the point to zoom into, 0..1 (default 0.5)"),- leak: {- type: "string",- enum: [...LEAK_COURSES],- description: 'lightleak only: "drift" glows and wanders for the whole element (default), "burn" is the film burn played once across it, "scorch" flashes hot burn tones through the picture frame by frame',- },- glitch: {- type: "string",- enum: [...GLITCH_KINDS],- description: 'glitch only: pin every frame to one kind; "rgb" is the channel split. Omit for the random mix',- },- tone: {- type: "string",- enum: [...FLASH_TONES],- description: 'flash only: "white" pops the picture bright (default), "black" dips it dark',- },- rate: num(`flash only: strobe pulses a second, ${FLASH_RATE_MIN}..${FLASH_RATE_MAX}; each pulse holds the tone for its first half. Omit for one pop at the start`),- rhythm: {- type: "string",- enum: [...FLASH_RHYTHMS],- description: 'flash with a rate only: "strobe" fires every pulse at full strength (default), "flicker" deals each pulse its own strength and skips some',- },+ ...EFFECT_SETTING_ARGS,lane: num("Element row (0 = the front row, drawn over every higher row). Elements on one row never overlap — a title over a shape needs a lower row than the shape."),}, ["effect"]),},diff --git a/site/src/cut/components/Inspector.tools.ts b/site/src/cut/components/Inspector.tools.tsindex 8825708f..b276ccfd 100644--- a/site/src/cut/components/Inspector.tools.ts+++ b/site/src/cut/components/Inspector.tools.ts@@ -52,6 +52,7 @@ import {STRIPE_LINE_MAX,STRIPE_LINE_MIN,} from "@donkeycut/effects-kit";+import { EFFECT_SETTING_ARGS } from "@/cut/components/EffectsPanel.tools";import { bool, ids, num, obj, str, type AiToolDef } from "@/cut/lib/aiToolDef";import { builtinLutCatalogText } from "@/cut/lib/builtinLuts";import { SPLIT_EDIT_MAX_S } from "@/cut/lib/soundSource";@@ -175,6 +176,8 @@ export const INSPECTOR_TOOLS = [motion_blur: bool("Streak the element along its own on-screen movement while it moves (true starts at the default shutter); still frames stay sharp"),shutter: num(`Motion blur shutter in 30fps frames, ${SHUTTER_MIN}..${SHUTTER_MAX}: 0.5 is the usual half-open shutter, and past 1 is a stylized long exposure (2–3 smears a whip that settles in a few frames); setting it switches motion blur on`),hidden: bool("Hide the element without deleting it"),+ amount: num("Effects: strength 0.05..1 (see add_effect)"),+ ...EFFECT_SETTING_ARGS,lane: num("Move the element to another row (0 = the front row, drawn over every higher row). Elements on one row never overlap — a title over a shape needs a lower row than the shape."),}, []),},@@ -596,7 +599,10 @@ export const INSPECTOR_TOOLS = [inputSchema: obj({clipId: str("Video clip to grade"),ref_asset_id: str("Reference image asset id"),- ref_clip_id: str("Reference video clip id (its current frame's base rendering)"),+ strength: num(+ "0..1, default 1: how far to take the match. Lower it when the reference shows other content than the clip (a bright window for a dark room, a black foreground the clip lacks) and the full match lifts or crushes tones the reference never had"+ ),+ ref_clip_id: str("Reference video clip id (its base rendering: the frame on screen, or one decoded mid-trim when it is off screen)"),}, ["clipId"]),},] as const satisfies readonly AiToolDef[];diff --git a/site/src/cut/components/Inspector.tsx b/site/src/cut/components/Inspector.tsxindex b67a049b..ee156635 100644--- a/site/src/cut/components/Inspector.tsx+++ b/site/src/cut/components/Inspector.tsx@@ -5034,10 +5034,11 @@ function GlitchKindRow({ overlay: o }: { overlay: EffectOverlay }) {}/** What each light leak course is called in the panel. */-const LEAK_LABELS: Record<LeakCourse, string> = { drift: "Drift", burn: "Burn", scorch: "Scorch" };+const LEAK_LABELS: Record<LeakCourse, string> = { drift: "Drift", burn: "Burn", scorch: "Scorch", film: "Film", char: "Char" };/** A light leak's course: a drifting glow for the whole element, the film- * burn played once across it, or the scorch flashing hot tones through it. */+ * burn played once across it, the scorch flashing hot tones through it, the+ * film-strip flash over a cut, or the burn's dark climb. */function LeakCourseRow({ overlay: o }: { overlay: EffectOverlay }) {const leak = o.leak ?? "drift";return (diff --git a/site/src/cut/components/SidePanel.tools.ts b/site/src/cut/components/SidePanel.tools.tsindex 8d9d9be5..086c6a3e 100644--- a/site/src/cut/components/SidePanel.tools.ts+++ b/site/src/cut/components/SidePanel.tools.ts@@ -68,8 +68,12 @@ export const SIDE_PANEL_TOOLS = [{name: "delete_asset",description:- "Remove a project asset and every timeline clip that uses it — the Media panel's trash. Removed media does not come back with undo, so call it only when the user explicitly asked to remove that media, and report how many clips went with it.",- inputSchema: obj({ asset_id: str("Project asset id from `media`") }, ["asset_id"]),+ "Remove project assets and every timeline clip that uses them — the Media panel's trash. Pass asset_id for one, asset_ids for several in one call, or unused:true to clear every asset no timeline uses, open or parked (leftover imports, replaced footage, reference images) without touching placed media. Removed media does not come back with undo, so call it only when the user explicitly asked to remove that media, and report how many assets and clips went.",+ inputSchema: obj({+ asset_id: str("Project asset id from `media`"),+ asset_ids: { type: "array", items: { type: "string" }, description: "Several project asset ids, removed in one call" },+ unused: { type: "boolean", description: "true removes every asset that no timeline (open or parked) uses" },+ }),},{name: "set_publish",@@ -82,6 +86,17 @@ export const SIDE_PANEL_TOOLS = [handle: str("Creator handle without @"),}),},+ {+ name: "set_project_notes",+ description:+ "Write this project's own notes — the Details tab's date, links and text, saved with the project for anyone who opens it. Put a cut's sources here: the original video a template copies, the post it was published as, a reference. note_save writes the account's Notes instead, which travel with no project. add_links appends (skipping links already there); links replaces the whole list.",+ inputSchema: obj({+ add_links: { type: "array", items: { type: "string" }, description: "Links to append, kept in order, duplicates skipped" },+ links: { type: "array", items: { type: "string" }, description: "The whole link list, replacing what is there ([] clears it)" },+ text: str("Free text, replacing what is there"),+ published_at: str("Published date, yyyy-mm-dd ('' clears it)"),+ }),+ },{name: "set_background",description:diff --git a/site/src/cut/components/TransitionsPanel.tools.ts b/site/src/cut/components/TransitionsPanel.tools.tsindex 34496629..f639d810 100644--- a/site/src/cut/components/TransitionsPanel.tools.ts+++ b/site/src/cut/components/TransitionsPanel.tools.ts@@ -9,7 +9,7 @@import { bool, ids, num, obj, str, type AiToolDef } from "@/cut/lib/aiToolDef";import { ANIM_STYLE_IDS, TRANSITION_STYLE_IDS } from "@/cut/lib/types";-import { FEATHER_STYLES, TRANSITION_FEATHER_MAX } from "@/cut/lib/transitionShape";+import { BAND_STYLES, FEATHER_STYLES, TRANSITION_BAND_MAX, TRANSITION_FEATHER_MAX } from "@/cut/lib/transitionShape";export const TRANSITIONS_TOOLS = [{@@ -28,6 +28,9 @@ export const TRANSITIONS_TOOLS = [feather: num(`Softness of the reveal edge for ${FEATHER_STYLES.join(", ")}: a share of the frame the edge crosses, 0..${TRANSITION_FEATHER_MAX} (0 = hard edge, 0.15 = a soft blurred band); other styles ignore it`),+ band: num(+ `A solid white panel running just ahead of the reveal edge of ${BAND_STYLES.join(", ")}, as a share of the frame the edge crosses, 0..${TRANSITION_BAND_MAX} (0 = none): the outgoing shot is wiped first by white, then by the incoming one, and the panel leaves past the far edge. A 0.1s wiperight with band 0.5 is the white-panel slide of a recap montage. Draws a hard edge (overrides feather)`+ ),}, ["seconds"]),},{diff --git a/site/src/cut/components/TransitionsPanel.tsx b/site/src/cut/components/TransitionsPanel.tsxindex a3f983e0..adbbf0ec 100644--- a/site/src/cut/components/TransitionsPanel.tsx+++ b/site/src/cut/components/TransitionsPanel.tsx@@ -23,7 +23,14 @@ import {type TransitionStyle,type VideoClip,} from "@/cut/lib/types";-import { CLOCK_SWEEP, shapeEase, takesFeather, TRANSITION_FEATHER_MAX } from "@/cut/lib/transitionShape";+import {+ CLOCK_SWEEP,+ shapeEase,+ takesBand,+ takesFeather,+ TRANSITION_BAND_MAX,+ TRANSITION_FEATHER_MAX,+} from "@/cut/lib/transitionShape";import { Row, useSliderCheckpoint } from "@/cut/components/panelBits";import { parseNumberInput } from "@/cut/components/ScrubValue";import { ValueSlider } from "@/cut/components/ValueSlider";@@ -69,6 +76,7 @@ export function TransitionsPanel() {{/* One key handler over every group, so arrows walk the whole listacross section boundaries. */}{live && takesFeather(live.style) && <SoftnessRow bar={live} />}+ {live && takesBand(live.style) && <BandRow bar={live} />}<div onKeyDown={pickGridNav}>{TRANSITION_STYLE_GROUPS.map((g) => (<section key={g.label} className="mb-3 flex flex-col gap-1.5">@@ -124,6 +132,44 @@ function SoftnessRow({ bar }: { bar: TimelineTransition }) {);}+/** How wide the white panel running ahead of the selected wipe's edge is:+ * 0 is none, the top of the range half the frame. A drag is one undo step. */+function BandRow({ bar }: { bar: TimelineTransition }) {+ const ck = useSliderCheckpoint();+ const write = (v: number) => {+ ck.begin();+ useEditor.getState().updateTransitionTransient(bar.id, { band: v > 0 ? v : undefined });+ };+ return (+ <div className="mb-3">+ <Row label="White band">+ <ValueSlider+ label="White band width"+ sliderClassName="data-horizontal:w-24"+ valueClassName="w-9 text-muted-foreground"+ value={bar.band ?? 0}+ min={0}+ max={TRANSITION_BAND_MAX}+ step={0.01}+ format={(v) => (v ? `${Math.round(v * 100)}%` : "None")}+ parse={(raw) => {+ if (raw.trim().toLowerCase() === "none") {+ return 0;+ }+ const n = parseNumberInput(raw.replace(/%$/, ""));+ return n === null ? null : n / 100;+ }}+ onDraft={write}+ onCommit={(v) => {+ write(v);+ ck.end();+ }}+ />+ </Row>+ </div>+ );+}+/** The frame an asset shows at `srcT` seconds into its source: the nearest* filmstrip thumb for video, the picture itself for an image. */function frameAt(asset: MediaAsset, srcT: number): string | null {diff --git a/site/src/cut/lib/aiContext.test.ts b/site/src/cut/lib/aiContext.test.tsindex c994bc21..8c1ecc22 100644--- a/site/src/cut/lib/aiContext.test.ts+++ b/site/src/cut/lib/aiContext.test.ts@@ -33,6 +33,18 @@ describe("describeDoc", () => {expect(read.project).toMatchObject({ id: "rich", name: "Rich edit", aspect: "16:9", background: "#102030" });});+ test("an effect element reads with its own settings, a light leak's course among them", () => {+ const doc = richDoc();+ const base = { start: 1, end: 1.2, x: 0.5, y: 0.5 };+ doc.overlays.push(+ { ...base, id: "fx-leak", kind: "effect", effect: "lightleak", amount: 1, leak: "char" },+ { ...base, id: "fx-drift", kind: "effect", effect: "lightleak" },+ );+ const read = describeDoc(doc).overlays as Record<string, unknown>[];+ expect(read.find((o) => o.id === "fx-leak")).toMatchObject({ effect: "lightleak", leak: "char" });+ expect("leak" in read.find((o) => o.id === "fx-drift")!).toBe(false);+ });+test("carries no live-only fields", () => {const read = describeDoc(richDoc()) as Record<string, unknown>;for (const key of ["playhead", "selection", "renders", "view", "fonts", "playing"]) {diff --git a/site/src/cut/lib/aiContext.ts b/site/src/cut/lib/aiContext.tsindex 7e98b656..71de6ace 100644--- a/site/src/cut/lib/aiContext.ts+++ b/site/src/cut/lib/aiContext.ts@@ -2,6 +2,7 @@import { formatBytes } from "@/lib/bytes";import { isLinkedAssetType, isTimelineId, sanitizeTimelines, timelineAspect, TIMELINE_IDS, TIMELINE_LABELS, type TimelineBody, type TimelineId } from "./types";+import { FIND_MEDIA_SNAPSHOT_CAP } from "./findMedia";import { GUIDE_PRESETS, guideFits, guideGeometry, safeAreaOf, sanitizeGuideLines, sanitizeGuides, type GuideId, type GuideLines } from "./guides";import { hasOverlayAnim, retimeOf, speedCurveOf, WHEEL_LABELS, WHEEL_ZONES, type ClipSound, type OutputSpace, type SpeedNode } from "@donkeycut/effects-kit";import { chatOwner } from "./chatAssets";@@ -48,7 +49,12 @@ function transitionToNext(sp: ClipSpan, index: number, spans: ClipSpan[]) {const seconds = sp.clip.transition ?? 0;if (seconds <= 0 || index >= spans.length - 1) return null;const style = sp.clip.transitionStyle ?? "crossfade";- return { style, seconds: r(sp.transitionOut), ...(sp.clip.transitionFeather ? { feather: sp.clip.transitionFeather } : {}) };+ return {+ style,+ seconds: r(sp.transitionOut),+ ...(sp.clip.transitionFeather ? { feather: sp.clip.transitionFeather } : {}),+ ...(sp.clip.transitionBand ? { band: sp.clip.transitionBand } : {}),+ };}/** The clip's own effects — entrance/exit animations, box styling, and its@@ -328,6 +334,8 @@ export function buildAiContext(opts?: { fullCues?: boolean; chatId?: string | nuoverlays: core.overlays,subtitles: core.subtitles,publish: s.publish,+ // The Details tab's notes: where the cut keeps its source links.+ ...(s.notes.text || s.notes.publishedAt || s.notes.links.some(Boolean) ? { notes: s.notes } : {}),view: {pxPerSec: r(s.pxPerSec),timelineH: s.timelineH,@@ -433,10 +441,11 @@ function describeState(// The cap trims the tail of a huge media list, and a source someone has// watched and written up stays regardless of where it sits: the record is// what later decisions are made from.+ const mediaCap = opts.fullCues ? Infinity : FIND_MEDIA_SNAPSHOT_CAP;const shownAssets =- visibleAssets.length <= cueCap+ visibleAssets.length <= mediaCap? visibleAssets- : visibleAssets.filter((a, i) => i < cueCap || (a.watch?.notes?.length ?? 0) > 0);+ : visibleAssets.filter((a, i) => i < mediaCap || (a.watch?.notes?.length ?? 0) > 0);const spans = getClipSpans(s.clips, s.assets);const duration = totalDuration(s.clips);const assetById = new Map(s.assets.map((a) => [a.id, a]));@@ -672,6 +681,7 @@ function describeState(seconds: r(t.seconds),style: t.style,...(t.feather ? { feather: t.feather } : {}),+ ...(t.band ? { band: t.band } : {}),...(t.hidden ? { hidden: true } : {}),...(plays.length > 0? { plays: plays.map((p) => ({ at: p.kind, clipId: p.clipId })) }@@ -894,6 +904,7 @@ function describeOverlay(o: Overlay) {...(o.effect === "zoom"? { focus: { x: r(o.focus?.x ?? 0.5), y: r(o.focus?.y ?? 0.5) } }: {}),+ ...(o.effect === "lightleak" && o.leak ? { leak: o.leak } : {}),...(o.effect === "glitch" && o.glitch ? { glitch: o.glitch } : {}),...(o.effect === "flash" && o.tone ? { tone: o.tone } : {}),...(o.effect === "flash" && o.rate ? { rate: r(o.rate), rhythm: o.rhythm ?? "strobe" } : {}),diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.tsindex cda7fd6a..b8c9f777 100644--- a/site/src/cut/lib/aiTools.ts+++ b/site/src/cut/lib/aiTools.ts@@ -223,8 +223,8 @@ import { isGradePresetTemplate, listSavedGrades, saveGradePreset } from "./gradeimport { isStylePresetTemplate } from "./stylePresets";import { findLibraryAsset, listLibrary } from "./libraryList";import { loadLibraryLut, lutIdOf, lutLabel } from "./linkedLibrary";-import { sampleClipBaseFrameData, sourceProfileOf, toBaseRendering } from "./baseFrame";-import { applyOverlayPatchSettled, assetClipUses, clipLen, track0Clips, laneGapAt, getClipSpans, overlayLaneOrder, overlayLayers, parkedTransitions, projectDuration, resolveTransitions, totalDuration, useEditor } from "./store";+import { baseFrameAt, sampleClipBaseFrameData, sourceProfileOf, toBaseRendering } from "./baseFrame";+import { applyOverlayPatchSettled, assetClipUses, assetIdsOnAnyTimeline, clipLen, track0Clips, laneGapAt, getClipSpans, overlayLaneOrder, overlayLayers, parkedTransitions, projectDuration, resolveTransitions, totalDuration, useEditor } from "./store";import { playheadAt } from "./playhead";import { mixSpecFor, renderProjectFrame, renderProjectFrames } from "./exportRender";import { renderMix } from "./audioMix";@@ -319,7 +319,9 @@ import {type AudioClip,type ClipShadow,type ColorGrade,+ type EffectOverlay,type FontId,+ isLinkedAssetType,type MediaAsset,type Selection,type ShapeKind,@@ -332,7 +334,9 @@ import {WATCH_DETAILS,type WatchDetail,} from "./types";-import { clampFeather } from "./transitionShape";+import { BAND_STYLES, clampBand, clampFeather, takesBand } from "./transitionShape";+import { importEach } from "./urlBatch";+import { findMedia } from "./findMedia";import { soundSourceOf, SPLIT_EDIT_MAX_S } from "./soundSource";import { bindRecording, unbindRecording } from "./soundBind";@@ -436,17 +440,30 @@ const GRADE_KEYS = [* rendering (through the source conversion, before any grade — what Auto* reads), or `graded` through the clip's LUT, preset and adjustments too, or* an image asset's pixels. */+/** A clip's frame decoded from its file at the middle of its trim, for a clip+ * the preview has no frame of — off screen, or not decoded yet. The decode+ * reads the base rendering, as the on-screen sample does. */+async function offscreenClipFrameData(+ s: ReturnType<typeof useEditor.getState>,+ clip: VideoClip,+ w: number,+ h: number+): Promise<Uint8ClampedArray | null> {+ const asset = s.assets.find((a) => a.id === clip.assetId);+ if (!asset?.url) return null;+ const frame = await baseFrameAt(asset.url, (clip.in + clip.out) / 2, { width: w, height: h, fit: "fill" });+ const ctx = frame?.canvas.getContext("2d") as CanvasRenderingContext2D | null | undefined;+ return ctx ? ctx.getImageData(0, 0, w, h).data : null;+}+async function colorStatsForRef(s: ReturnType<typeof useEditor.getState>,ref: { clipId?: string; assetId?: string; graded?: boolean }): Promise<ColorStats> {if (ref.clipId) {const clip = requireItem(s.clips, ref.clipId, "video clip");- const data = sampleClipBaseFrameData(clip.id, 192, 108);- if (!data)- throw new ToolError(- "No decoded frame for that clip yet — seek into it so it is on screen, then retry."- );+ const data = sampleClipBaseFrameData(clip.id, 192, 108) ?? (await offscreenClipFrameData(s, clip, 192, 108));+ if (!data) throw new ToolError("Could not decode a frame of that clip's file.");if (ref.graded && clip.grade) {// The same recipe the Color panel's tiles draw with: the base rendering// through the clip's LUT, preset and adjustments.@@ -634,32 +651,8 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {},});}- // A light leak's course: drift is the default and stores as absence.- if (effect === "lightleak" && input.leak !== undefined) {- if (!(LEAK_COURSES as readonly unknown[]).includes(input.leak))- throw new ToolError(`leak is one of ${LEAK_COURSES.join(", ")}.`);- useEditor.getState().updateOverlay(sel.id, { leak: input.leak === "drift" ? undefined : (input.leak as LeakCourse) });- }- // A glitch pinned to one kind; the random mix stores as absence.- if (effect === "glitch" && input.glitch !== undefined) {- if (!(GLITCH_KINDS as readonly unknown[]).includes(input.glitch)) {- throw new ToolError(`glitch is one of ${GLITCH_KINDS.join(", ")}.`);- }- useEditor.getState().updateOverlay(sel.id, { glitch: input.glitch as GlitchKind });- }- // A flash's tone and strobe: white and a single pop store as absence.- if (effect === "flash" && input.tone !== undefined) {- if (!(FLASH_TONES as readonly unknown[]).includes(input.tone)) {- throw new ToolError(`tone is one of ${FLASH_TONES.join(", ")}.`);- }- useEditor.getState().updateOverlay(sel.id, { tone: input.tone === "black" ? "black" : undefined });- }- if (effect === "flash" && isNum(input.rate)) {- useEditor.getState().updateOverlay(sel.id, { rate: input.rate > 0 ? clamp(input.rate, FLASH_RATE_MIN, FLASH_RATE_MAX) : undefined });- }- if (effect === "flash" && input.rhythm !== undefined) {- useEditor.getState().updateOverlay(sel.id, { rhythm: flashRhythm(input.rhythm) });- }+ const settings = effectSettings(effect as EffectId, input);+ if (Object.keys(settings).length) useEditor.getState().updateOverlay(sel.id, settings);const o = useEditor.getState().overlays.find((x) => x.id === sel.id)!;return { id: o.id, effect, start: round2(o.start), end: round2(o.end) };},@@ -780,6 +773,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {if (!Array.isArray(raw)) throw new ToolError("keys must be a list.");const dur = Math.max(0.1, o.end - o.start);let kf = o.kf;+ const sent = new Set<number>();if (raw.length === 0) {s.clearOverlayKeys(o.id);kf = undefined;@@ -789,6 +783,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {s.pushHistory();for (const k of raw as Record<string, unknown>[]) {if (!isNum(k.t)) throw new ToolError("Every key needs a time `t`.");+ sent.add(round2(clamp(k.t, 0, dur)));s.setOverlayKey(o.id,clamp(k.t, 0, dur),@@ -810,7 +805,16 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {}kf = useEditor.getState().overlays.find((x) => x.id === o.id)?.kf;}- return { id: o.id, keys: (kf ?? []).map((k) => ({ ...k, t: round2(k.t) })) };+ // A merge keeps keys this call never named; say which, so a key meant to+ // go is not sent around again and again.+ const kept = raw.length ? (kf ?? []).map((k) => round2(k.t)).filter((t) => !sent.has(t)) : [];+ return {+ id: o.id,+ keys: (kf ?? []).map((k) => ({ ...k, t: round2(k.t) })),+ ...(kept.length+ ? { kept, note: `Keys at ${kept.join(", ")}s were already on the track and stay; to drop them, clear the track with an empty keys list, then send the full list.` }+ : {}),+ };},set_group_camera: (s, input) => {@@ -1044,12 +1048,18 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const bar = after.transitions.find((t) =>(roles.get(t.id) ?? []).some((r) => r.kind !== "in" && r.clipId === clip.id));- // The softness lands on the bar inside the same undo step.+ // The softness and the band land on the bar inside the same undo step.if (bar && isNum(input.feather)) {const feather = clampFeather(input.feather);after.updateTransitionTransient(bar.id, { feather: feather > 0 ? feather : undefined });after = useEditor.getState();}+ if (bar && isNum(input.band)) {+ if (!takesBand(bar.style)) throw new ToolError(`band is for the wipes only (${BAND_STYLES.join(", ")}).`);+ const band = clampBand(input.band);+ after.updateTransitionTransient(bar.id, { band: band > 0 ? band : undefined });+ after = useEditor.getState();+ }const next = after.clips.find((c) => c.id === clip.id)!;return {id: next.id,@@ -1057,6 +1067,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {transition: next.transition ?? 0,style: next.transitionStyle ?? "crossfade",...(next.transitionFeather ? { feather: next.transitionFeather } : {}),+ ...(next.transitionBand ? { band: next.transitionBand } : {}),};},@@ -1114,6 +1125,33 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {};},+ find_media: (s, input) => {+ // The same media the snapshot would show this chat, past its cap.+ const placed = placedAssetIds(s);+ const chatId = chatOwner();+ const visible = s.assets.filter((a) => !isLinkedAssetType(a.type) && !hiddenFromChat(a, chatId, placed));+ const type = input.type === "video" || input.type === "image" || input.type === "audio" ? input.type : undefined;+ const found = findMedia(visible, {+ query: typeof input.query === "string" ? input.query : undefined,+ type,+ limit: isNum(input.limit) ? input.limit : undefined,+ offset: isNum(input.offset) ? input.offset : undefined,+ });+ return {+ total: found.total,+ assets: found.page.map((a) => ({+ id: a.id,+ name: a.name,+ type: a.type,+ fileName: a.fileName,+ duration: round2(a.duration),+ ...(a.width !== undefined ? { width: a.width, height: a.height } : {}),+ ...(a.folderId != null ? { folderId: a.folderId } : {}),+ })),+ ...(found.next !== undefined ? { next: found.next } : {}),+ };+ },+render_preview: async (s, _input, operation) => {if (!s.projectId || !s.loaded) throw new ToolError("No project open.");return submitPreviewSnapshot(captureRenderSnapshot(operation ?? projectOperation(s.projectId), renderDoc(s)));@@ -2992,7 +3030,15 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const targets = (ids ?? [String(input.id)]).map((id) => requireItem(s.overlays, id, "overlay element"));// One undo step for the whole write, however many elements it lands on.s.pushHistory();- for (const o of targets) applyOverlayPatchSettled(o.id, overlayPatch(input, o.kind ?? "text", o));+ for (const o of targets) {+ applyOverlayPatchSettled(o.id, overlayPatch(input, o.kind ?? "text", o));+ // An effect's own settings — its course, kind, tone, strobe — change+ // in place, so the element keeps its id, lane and timing.+ if (isEffectOverlay(o)) {+ const settings = effectSettings(o.effect, input);+ if (Object.keys(settings).length) useEditor.getState().updateOverlay(o.id, settings);+ }+ }const report = (id: string) => {const next = useEditor.getState().overlays.find((x) => x.id === id)!;return {@@ -3971,17 +4017,11 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {import_url: async (s, input) => {const projectId = s.projectId;if (!projectId) throw new ToolError("No project open.");- const url = String(input.url ?? "").trim();- if (!url) throw new ToolError("A URL is required.");// Captured before the download: the media files under the chat that// asked, even if the user switches threads while it downloads.const chatId = chatOwner();- const { assets, text } = await importUrlMedia(projectId, url, {- audio: input.audio_only === true,- });- for (const asset of assets) tagChatAsset(asset.id, chatId);- return {- assets: assets.map((asset) => ({+ const describe = (assets: MediaAsset[]) =>+ assets.map((asset) => ({assetId: asset.id,name: asset.name,kind: asset.type,@@ -3990,7 +4030,38 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {width: asset.width,height: asset.height,duration: round2(asset.type === "image" ? IMAGE_CLIP_SECONDS : asset.duration),- })),+ }));+ // A list downloads a few at a time; each URL answers on its own row.+ if (Array.isArray(input.urls) && input.urls.length > 0) {+ const rows = await importEach(input.urls.map(String), async (u) => {+ const { assets } = await importUrlMedia(projectId, u, { audio: input.audio_only === true });+ for (const asset of assets) tagChatAsset(asset.id, chatId);+ return describe(assets);+ }).catch((e) => {+ throw new ToolError(e instanceof Error ? e.message : String(e));+ });+ const results = rows.filter((r) => !("notStarted" in r)).map((r) => ("value" in r ? { url: r.url, assets: r.value } : r));+ const notStarted = rows.filter((r) => "notStarted" in r).map((r) => r.url);+ const pending = rows.some((r) => "pending" in r);+ return {+ results,+ ...(notStarted.length ? { notStarted } : {}),+ // A long list answers in parts, inside the tool call's time.+ ...(pending || notStarted.length+ ? {+ note: `${pending ? "Pending links are still downloading and land in the project on their own; don't import them again. " : ""}${notStarted.length ? "Call import_url again with the notStarted urls." : ""}`.trim(),+ }+ : {}),+ };+ }+ const url = String(input.url ?? "").trim();+ if (!url) throw new ToolError("A URL is required.");+ const { assets, text } = await importUrlMedia(projectId, url, {+ audio: input.audio_only === true,+ });+ for (const asset of assets) tagChatAsset(asset.id, chatId);+ return {+ assets: describe(assets),...(text ? { sourceText: text } : {}),};},@@ -4608,6 +4679,25 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {},delete_asset: (s, input) => {+ // The sweep: every asset no timeline uses, open or parked.+ if (input.unused === true || Array.isArray(input.asset_ids)) {+ let gone: MediaAsset[];+ if (input.unused === true) {+ const used = assetIdsOnAnyTimeline(s);+ gone = s.assets.filter((a) => !used.has(a.id));+ } else {+ const want = (input.asset_ids as unknown[]).filter((v): v is string => typeof v === "string");+ gone = want.map((id) => requireItem(s.assets, id, "project asset"));+ }+ const uses = assetClipUses(s, new Set(gone.map((a) => a.id)));+ for (const a of gone) useEditor.getState().removeAsset(a.id);+ return {+ removed: gone.length,+ names: gone.slice(0, 30).map((a) => a.name),+ ...(gone.length > 30 ? { namesTruncated: true } : {}),+ clipsRemoved: uses,+ };+ }const asset = requireItem(s.assets, input.asset_id, "project asset");const uses = assetClipUses(s, new Set([asset.id]));s.removeAsset(asset.id);@@ -4952,6 +5042,28 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return patch;},+ set_project_notes: (s, input) => {+ const strings = (v: unknown, k: string) => {+ if (!Array.isArray(v) || !v.every((x) => typeof x === "string")) throw new ToolError(`${k} must be a list of strings.`);+ return (v as string[]).map((x) => x.trim()).filter(Boolean);+ };+ const patch: Partial<{ text: string; publishedAt: string; links: string[] }> = {};+ let links = input.links !== undefined ? strings(input.links, "links") : undefined;+ if (input.add_links !== undefined) {+ const base = links ?? s.notes.links.filter(Boolean);+ links = [...base, ...strings(input.add_links, "add_links").filter((l) => !base.includes(l))];+ }+ if (links) patch.links = links;+ if (typeof input.text === "string") patch.text = input.text;+ if (typeof input.published_at === "string") {+ if (input.published_at && !/^\d{4}-\d{2}-\d{2}$/.test(input.published_at)) throw new ToolError("published_at is a date as yyyy-mm-dd.");+ patch.publishedAt = input.published_at;+ }+ if (Object.keys(patch).length === 0) throw new ToolError("Nothing to change.");+ s.setNotes(patch);+ return useEditor.getState().notes;+ },+switch_timeline: async (_s, input) => {if (!isTimelineId(input.timeline))throw new ToolError(`timeline must be one of ${TIMELINE_IDS.join(", ")}.`);@@ -5087,17 +5199,14 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {};},- set_color_grade: (s, input) => {+ set_color_grade: async (s, input) => {const clip = requireItem(s.clips, input.clipId, "video clip");// reset and auto rebuild the manual layer; the preset and LUT stay.let grade: ColorGrade =input.reset === true ? { preset: clip.grade?.preset, lut: clip.grade?.lut } : { ...clip.grade };if (input.auto === true) {- const data = sampleClipBaseFrameData(clip.id);- if (!data)- throw new ToolError(- "No decoded frame for that clip yet — seek into it so it is on screen, then retry."- );+ const data = sampleClipBaseFrameData(clip.id) ?? (await offscreenClipFrameData(s, clip, 96, 54));+ if (!data) throw new ToolError("Could not decode a frame of that clip's file.");grade = { ...autoGradeFromImageData(data), preset: clip.grade?.preset, lut: clip.grade?.lut };}for (const k of GRADE_KEYS) if (isNum(input[k])) grade[k] = input[k];@@ -5301,7 +5410,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {clipId: typeof input.ref_clip_id === "string" ? input.ref_clip_id : undefined,assetId: typeof input.ref_asset_id === "string" ? input.ref_asset_id : undefined,});- const grade = matchGrade(source, reference);+ const grade = matchGrade(source, reference, isNum(input.strength) ? input.strength : 1);// The frames already agree: the clip's own grade stays as it is rather// than being wiped by a match that has nothing to say.if (!grade)@@ -6841,6 +6950,41 @@ function patternInput(return { value: spec };}+/**+ * The settings an effect of this kind takes from a tool call: a light leak's+ * course, a glitch's pinned kind, a flash's tone, strobe rate and rhythm.+ * Only the keys the call names come back, each as the value to store+ * (undefined clears it back to the default); a setting the effect doesn't+ * take is skipped. Shared by add_effect and update_overlay.+ */+function effectSettings(effect: EffectId, input: Record<string, unknown>): Partial<EffectOverlay> {+ const patch: Partial<EffectOverlay> = {};+ // A light leak's course: drift is the default and stores as absence.+ if (effect === "lightleak" && input.leak !== undefined) {+ if (!(LEAK_COURSES as readonly unknown[]).includes(input.leak))+ throw new ToolError(`leak is one of ${LEAK_COURSES.join(", ")}.`);+ patch.leak = input.leak === "drift" ? undefined : (input.leak as LeakCourse);+ }+ // A glitch pinned to one kind; the random mix stores as absence.+ if (effect === "glitch" && input.glitch !== undefined) {+ if (!(GLITCH_KINDS as readonly unknown[]).includes(input.glitch)) {+ throw new ToolError(`glitch is one of ${GLITCH_KINDS.join(", ")}.`);+ }+ patch.glitch = input.glitch as GlitchKind;+ }+ if (effect !== "flash") return patch;+ // A flash's tone and strobe: white and a single pop store as absence.+ if (input.tone !== undefined) {+ if (!(FLASH_TONES as readonly unknown[]).includes(input.tone)) {+ throw new ToolError(`tone is one of ${FLASH_TONES.join(", ")}.`);+ }+ patch.tone = input.tone === "black" ? "black" : undefined;+ }+ if (isNum(input.rate)) patch.rate = input.rate > 0 ? clamp(input.rate, FLASH_RATE_MIN, FLASH_RATE_MAX) : undefined;+ if (input.rhythm !== undefined) patch.rhythm = flashRhythm(input.rhythm);+ return patch;+}+function overlayPatch(input: Record<string, unknown>,kind: "text" | "shape" | "sticker" | "effect",diff --git a/site/src/cut/lib/colorOffscreen.test.ts b/site/src/cut/lib/colorOffscreen.test.tsnew file mode 100644index 00000000..a4993d7b--- /dev/null+++ b/site/src/cut/lib/colorOffscreen.test.ts@@ -0,0 +1,86 @@+import { beforeEach, expect, test } from "bun:test";+import { stubModule } from "@/lib/testing/stubModule";++/** A clip's colour is read even when it isn't on screen: the frame is decoded+ * from its file at the middle of its trim, so grading a whole timeline never+ * waits on the playhead visiting each clip. */++const decoded: { url: string; time: number; width?: number; height?: number }[] = [];++// Nothing is on screen; the file decode answers a flat dark frame.+await stubModule<typeof import("./baseFrame")>("./baseFrame", import.meta.url, {+ sampleClipBaseFrameData: () => null,+ baseFrameAt: async (url, time, size) => {+ decoded.push({ url, time, width: size?.width, height: size?.height });+ const w = size?.width ?? 16;+ const h = size?.height ?? 9;+ // Each file is two greys 40 apart, pixel by pixel: "blob:one" dark+ // (20 and 60), any other mid (100 and 140).+ const base = url === "blob:one" ? 20 : 100;+ const data = new Uint8ClampedArray(w * h * 4);+ for (let j = 0; j < w * h; j++) data.fill(base + (j % 2) * 40, j * 4, j * 4 + 3), (data[j * 4 + 3] = 255);+ const canvas = {+ width: w,+ height: h,+ getContext: () => ({ getImageData: () => ({ data, width: w, height: h }) }),+ };+ return { canvas, timestamp: time, duration: 1 / 30 } as never;+ },+});++const { runAiTool } = await import("./aiTools");+const { useEditor } = await import("./store");++beforeEach(async () => {+ decoded.length = 0;+ useEditor.setState({+ projectId: "p1",+ clips: [],+ overlays: [],+ transitions: [],+ audioClips: [],+ assets: [{ id: "a1", fileName: "one.mp4", name: "One", type: "video", duration: 10, url: "blob:one" }],+ });+ await runAiTool("add_clip", { asset_id: "a1" });+ const [clip] = useEditor.getState().clips;+ useEditor.getState().updateClip(clip.id, { in: 2, out: 6 });+});++test("read_color_stats decodes an off-screen clip's frame from its file, mid-trim", async () => {+ const [clip] = useEditor.getState().clips;+ const stats = (await runAiTool("read_color_stats", { clipId: clip.id })) as { meanR: number };+ expect(stats.meanR).toBe(40);+ expect(decoded).toHaveLength(1);+ expect(decoded[0]).toMatchObject({ url: "blob:one", time: 4 });+});++test("match_color_grade grades an off-screen clip instead of asking for a seek", async () => {+ const [clip] = useEditor.getState().clips;+ // The clip is its own reference: nothing to change, but no seek error.+ const out = (await runAiTool("match_color_grade", { clipId: clip.id, ref_clip_id: clip.id })) as { id: string };+ expect(out.id).toBe(clip.id);+ expect(decoded.length).toBeGreaterThanOrEqual(1);+});++test("set_color_grade auto fits a grade off an off-screen clip", async () => {+ const [clip] = useEditor.getState().clips;+ await runAiTool("set_color_grade", { clipId: clip.id, auto: true });+ expect(decoded[0]).toMatchObject({ time: 4 });+ expect(useEditor.getState().clips[0].grade?.exposure).toBeGreaterThan(0);+});++test("match_color_grade strength takes the match part of the way", async () => {+ useEditor.setState({+ assets: [...useEditor.getState().assets, { id: "a2", fileName: "two.mp4", name: "Two", type: "video", duration: 10, url: "blob:two" }],+ });+ await runAiTool("add_clip", { asset_id: "a2" });+ const [dark, mid] = useEditor.getState().clips;+ const curveAt20 = () => {+ const g = useEditor.getState().clips.find((c) => c.id === dark.id)!.grade;+ return g?.curves?.g?.find(([x]) => x === 20)?.[1];+ };+ await runAiTool("match_color_grade", { clipId: dark.id, ref_clip_id: mid.id });+ expect(curveAt20()).toBe(100);+ await runAiTool("match_color_grade", { clipId: dark.id, ref_clip_id: mid.id, strength: 0.5 });+ expect(curveAt20()).toBe(60);+});diff --git a/site/src/cut/lib/composite.ts b/site/src/cut/lib/composite.tsindex 88cf955e..c680503e 100644--- a/site/src/cut/lib/composite.ts+++ b/site/src/cut/lib/composite.ts@@ -25,7 +25,18 @@ import { applyDetailGpu } from "./detailGpu";import { applyLutGpu } from "./gradeGpu";import { clipRecipe, peekClipLut, requestClipLut, type ClipLut, type RecipeSource } from "./lutBuild";import { createRasterCanvas } from "./raster";-import { CLOCK_SWEEP, clampFeather, featherAlpha, shapeEase, SLICE_TAPS, sliceStreak, takesFeather } from "./transitionShape";+import {+ bandEdge,+ CLOCK_SWEEP,+ clampBand,+ clampFeather,+ featherAlpha,+ shapeEase,+ SLICE_TAPS,+ sliceStreak,+ takesBand,+ takesFeather,+} from "./transitionShape";import { clipCovers, clipFrameAt, clipPosed, clipZoom, contentRect, DEFAULT_BACKGROUND, isFullRect, rectOf, shadowInk, TRANSITION_BLUR } from "./types";import type { ClipShadow, FrameRect, TransitionStyle, VideoClip } from "./types";@@ -1386,8 +1397,11 @@ export class FrameCompositor {}// A softened edge: the incoming shot fades in across a ramp along the// edge, the same profile the ffmpeg expression computes per pixel.+ // A white band ahead of a wipe's edge draws a hard edge, so it wins+ // over a feather.+ const band = takesBand(style) ? clampBand(d.masterClip.transitionBand) : 0;const feather = clampFeather(d.masterClip.transitionFeather);- if (feather > 0 && takesFeather(style)) {+ if (feather > 0 && takesFeather(style) && band === 0) {drawMaster();this.drawFeathered(style, p, feather, () => drawInc(1));return;@@ -1437,20 +1451,32 @@ export class FrameCompositor {case "wiperight":case "wipeup":case "wipedown": {- // Hard reveal edge traveling in the named direction.- const r =+ // Hard reveal edge traveling in the named direction; a band runs a+ // solid white panel just ahead of it (`bandEdge`).+ const e = bandEdge(p, band);+ const [r, b] =style === "wipeleft"- ? { x: (1 - p) * W, y: 0, w: p * W, h: H }+ ? [{ x: (1 - e) * W, y: 0, w: e * W, h: H }, { x: (1 - e - band) * W, y: 0, w: band * W, h: H }]: style === "wiperight"- ? { x: 0, y: 0, w: p * W, h: H }+ ? [{ x: 0, y: 0, w: e * W, h: H }, { x: e * W, y: 0, w: band * W, h: H }]: style === "wipeup"- ? { x: 0, y: (1 - p) * H, w: W, h: p * H }- : { x: 0, y: 0, w: W, h: p * H };+ ? [{ x: 0, y: (1 - e) * H, w: W, h: e * H }, { x: 0, y: (1 - e - band) * H, w: W, h: band * H }]+ : [{ x: 0, y: 0, w: W, h: e * H }, { x: 0, y: e * H, w: W, h: band * H }];drawMaster();- clipped(- () => ctx.rect(r.x, r.y, r.w, r.h),- () => drawInc(1)- );+ if (e > 0) {+ clipped(+ () => ctx.rect(r.x, r.y, r.w, r.h),+ () => drawInc(1)+ );+ }+ if (band > 0) {+ ctx.save();+ ctx.globalAlpha = 1;+ ctx.globalCompositeOperation = "source-over";+ ctx.fillStyle = "#ffffff";+ ctx.fillRect(b.x, b.y, b.w, b.h);+ ctx.restore();+ }break;}case "circleopen": {diff --git a/site/src/cut/lib/deleteAssetTool.test.ts b/site/src/cut/lib/deleteAssetTool.test.tsnew file mode 100644index 00000000..69cd02b5--- /dev/null+++ b/site/src/cut/lib/deleteAssetTool.test.ts@@ -0,0 +1,62 @@+import { beforeEach, describe, expect, test } from "bun:test";+import { runAiTool } from "./aiTools";+import { useEditor } from "./store";+import { emptySubtitles, type MediaAsset } from "./types";+import { SIDE_PANEL_TOOLS } from "../components/SidePanel.tools";++/** Clearing a project's leftover media in one call: the unused sweep takes+ * everything no timeline uses, open or parked, and a list takes several ids+ * at once — where one id per call meant a call per file and ids the chat+ * could not tell apart from placed media. */++const asset = (id: string): MediaAsset => ({ id, fileName: `${id}.mp4`, name: id, type: "video", duration: 5, url: "" });++beforeEach(async () => {+ useEditor.setState({+ projectId: null,+ aspect: "16:9",+ assets: ["placed", "parked", "spare1", "spare2", "spare3"].map(asset),+ clips: [],+ audioClips: [],+ overlays: [],+ transitions: [],+ subtitles: emptySubtitles(),+ background: "#000000",+ timelines: {},+ });+ await runAiTool("add_clip", { asset_id: "placed" });+ // A clip on the closed Second timeline still uses its asset.+ const s = useEditor.getState();+ useEditor.setState({+ timelines: { second: { clips: [{ ...s.clips[0], id: "c2", assetId: "parked" }], audioClips: [], overlays: [] } as never },+ });+});++const ids = () => useEditor.getState().assets.map((a) => a.id);++describe("delete_asset", () => {+ test("unused:true removes every asset no timeline uses, open or parked", async () => {+ const out = (await runAiTool("delete_asset", { unused: true })) as { removed: number; clipsRemoved: number };+ expect(out).toMatchObject({ removed: 3, clipsRemoved: 0 });+ expect(ids()).toEqual(["placed", "parked"]);+ });++ test("asset_ids removes several at once and counts the clips that went with them", async () => {+ const out = (await runAiTool("delete_asset", { asset_ids: ["spare1", "placed"] })) as { removed: number; clipsRemoved: number };+ expect(out).toMatchObject({ removed: 2, clipsRemoved: 1 });+ expect(ids()).toEqual(["parked", "spare2", "spare3"]);+ expect(useEditor.getState().clips).toHaveLength(0);+ });++ test("one asset_id still works as before", async () => {+ const out = (await runAiTool("delete_asset", { asset_id: "spare2" })) as { removed: string };+ expect(out.removed).toBe("spare2");+ expect(ids()).not.toContain("spare2");+ });++ test("the schema offers the list and the sweep", () => {+ const def = SIDE_PANEL_TOOLS.find((t) => t.name === "delete_asset")!;+ const props = def.inputSchema.properties as Record<string, unknown>;+ for (const k of ["asset_id", "asset_ids", "unused"]) expect(Object.keys(props)).toContain(k);+ });+});diff --git a/site/src/cut/lib/effectUpdateTool.test.ts b/site/src/cut/lib/effectUpdateTool.test.tsnew file mode 100644index 00000000..2120ef80--- /dev/null+++ b/site/src/cut/lib/effectUpdateTool.test.ts@@ -0,0 +1,63 @@+import { beforeEach, describe, expect, test } from "bun:test";+import { runAiTool } from "./aiTools";+import { useEditor } from "./store";+import { emptySubtitles } from "./types";+import { INSPECTOR_TOOLS } from "../components/Inspector.tools";++/** An effect element's own settings changed in place through update_overlay,+ * so its id, lane and timing survive the edit. */++const reset = () =>+ useEditor.setState({+ projectId: "p1",+ aspect: "16:9",+ assets: [],+ clips: [],+ overlays: [],+ subtitles: emptySubtitles(),+ background: "#000000",+ });++const overlay = (id: string) => useEditor.getState().overlays.find((o) => o.id === id);++async function errorOf(run: () => Promise<unknown>): Promise<string> {+ try {+ await run();+ } catch (err) {+ return err instanceof Error ? err.message : String(err);+ }+ return "";+}++describe("update_overlay on an effect", () => {+ beforeEach(reset);++ test("a light leak's amount and course change in place", async () => {+ const { id } = (await runAiTool("add_effect", { effect: "lightleak", start: 1, end: 1.5, leak: "burn", amount: 0.8 })) as { id: string };+ await runAiTool("update_overlay", { id, amount: 1, leak: "char" });+ expect(overlay(id)).toMatchObject({ effect: "lightleak", leak: "char", amount: 1, start: 1, end: 1.5 });+ await runAiTool("update_overlay", { id, leak: "drift" });+ expect((overlay(id) as { leak?: string }).leak).toBeUndefined();+ expect(await errorOf(() => runAiTool("update_overlay", { id, leak: "flare" }))).toContain("leak is one of");+ });++ test("a flash's tone, strobe and rhythm, and a glitch's kind, change in place", async () => {+ const { id: flash } = (await runAiTool("add_effect", { effect: "flash", start: 2, end: 3 })) as { id: string };+ await runAiTool("update_overlay", { id: flash, tone: "black", rate: 6, rhythm: "flicker" });+ expect(overlay(flash)).toMatchObject({ tone: "black", rate: 6, rhythm: "flicker" });+ const { id: glitch } = (await runAiTool("add_effect", { effect: "glitch", start: 0, end: 1 })) as { id: string };+ await runAiTool("update_overlay", { id: glitch, glitch: "rgb" });+ expect(overlay(glitch)).toMatchObject({ glitch: "rgb" });+ });++ test("a setting the effect doesn't take is skipped, like any field a kind lacks", async () => {+ const { id } = (await runAiTool("add_effect", { effect: "flash", start: 2, end: 3 })) as { id: string };+ await runAiTool("update_overlay", { id, leak: "burn" });+ expect((overlay(id) as { leak?: string }).leak).toBeUndefined();+ });++ test("the tool's schema offers the effect settings", () => {+ const props = INSPECTOR_TOOLS.find((t) => t.name === "update_overlay")!.inputSchema.properties as Record<string, unknown>;+ for (const k of ["amount", "leak", "glitch", "tone", "rate", "rhythm"]) expect(Object.keys(props)).toContain(k);+ });+});diff --git a/site/src/cut/lib/exportClient.ts b/site/src/cut/lib/exportClient.tsindex 9f845732..a7aabec1 100644--- a/site/src/cut/lib/exportClient.ts+++ b/site/src/cut/lib/exportClient.ts@@ -983,6 +983,7 @@ export async function buildExportPayload(// the id.transitionStyle: sp.clip.transitionStyle,transitionFeather: sp.clip.transitionFeather,+ transitionBand: sp.clip.transitionBand,animIn: sp.clip.animIn,animOut: sp.clip.animOut,look: sp.clip.look,diff --git a/site/src/cut/lib/findMedia.ts b/site/src/cut/lib/findMedia.tsnew file mode 100644index 00000000..a4592bce--- /dev/null+++ b/site/src/cut/lib/findMedia.ts@@ -0,0 +1,32 @@+/**+ * Finding project media by name. The chat's context snapshot lists only the+ * first `FIND_MEDIA_SNAPSHOT_CAP` media items, so a long project's later+ * imports — a batch of reference images uploaded last — are reached by name+ * through find_media instead.+ */++/** How many media items the context snapshot lists before it trims. */+export const FIND_MEDIA_SNAPSHOT_CAP = 60;++/** How many matches find_media answers with by default, and at most. */+export const FIND_MEDIA_DEFAULT = 50;+export const FIND_MEDIA_MAX = 200;++export type MediaKind = "video" | "image" | "audio";++/** The page of `assets` whose name or file name contains `query` (any case)+ * and, when given, is of `type`; `next` is the offset of the page after,+ * absent on the last. */+export function findMedia<A extends { name: string; fileName: string; type: string }>(+ assets: A[],+ opts: { query?: string; type?: MediaKind; limit?: number; offset?: number }+): { total: number; page: A[]; next?: number } {+ const q = (opts.query ?? "").trim().toLowerCase();+ const hits = assets.filter(+ (a) => (!opts.type || a.type === opts.type) && (!q || a.name.toLowerCase().includes(q) || a.fileName.toLowerCase().includes(q))+ );+ const limit = Math.max(1, Math.min(FIND_MEDIA_MAX, Math.round(opts.limit ?? FIND_MEDIA_DEFAULT)));+ const offset = Math.max(0, Math.round(opts.offset ?? 0));+ const page = hits.slice(offset, offset + limit);+ return offset + limit < hits.length ? { total: hits.length, page, next: offset + limit } : { total: hits.length, page };+}diff --git a/site/src/cut/lib/findMediaTool.test.ts b/site/src/cut/lib/findMediaTool.test.tsnew file mode 100644index 00000000..484d9e40--- /dev/null+++ b/site/src/cut/lib/findMediaTool.test.ts@@ -0,0 +1,71 @@+import { beforeEach, describe, expect, test } from "bun:test";+import { buildAiContext } from "./aiContext";+import { runAiTool } from "./aiTools";+import { useEditor } from "./store";+import { emptySubtitles, type MediaAsset } from "./types";+import { AI_PANEL_TOOLS } from "../components/AiPanel.tools";++/** Media past the context snapshot's cap is found by name through+ * find_media, so a long project's last imports still have ids to act on. */++const image = (i: number): MediaAsset => ({+ id: `img${i}`,+ fileName: `gref_s${String(i).padStart(2, "0")}.jpg`,+ name: `gref_s${String(i).padStart(2, "0")}.jpg`,+ type: "image",+ duration: 0,+ url: "",+});+const clipFile = (i: number): MediaAsset => ({ id: `v${i}`, fileName: `clip${i}.mp4`, name: `clip${i}`, type: "video", duration: 5, url: "" });++beforeEach(() => {+ useEditor.setState({+ projectId: "p1",+ aspect: "16:9",+ assets: [+ ...Array.from({ length: 70 }, (_, i) => clipFile(i)),+ ...Array.from({ length: 69 }, (_, i) => image(i + 1)),+ { ...image(99), id: "other", name: "gref_private.jpg", origin: "chat", chatId: "someone-else" },+ ],+ clips: [],+ audioClips: [],+ overlays: [],+ subtitles: emptySubtitles(),+ background: "#000000",+ });+});++type Found = { total: number; assets: { id: string; name: string; type: string }[]; next?: number };++describe("find_media", () => {+ test("the snapshot trims a long media list, and says so", () => {+ const ctx = buildAiContext({ chatId: null });+ expect(ctx.mediaTruncated).toBe(true);+ expect(ctx.media.some((m) => m.name === "gref_s60.jpg")).toBe(false);+ });++ test("finds media past the cap by name, case-insensitively", async () => {+ const out = (await runAiTool("find_media", { query: "GREF_S6" })) as Found;+ expect(out.assets.map((a) => a.name)).toEqual(Array.from({ length: 10 }, (_, i) => `gref_s6${i}.jpg`));+ expect(out.assets[0]).toMatchObject({ id: "img60", type: "image" });+ });++ test("filters by kind and pages through a long answer", async () => {+ const first = (await runAiTool("find_media", { type: "image", limit: 50 })) as Found;+ expect(first.total).toBe(69);+ expect(first.assets).toHaveLength(50);+ expect(first.next).toBe(50);+ const rest = (await runAiTool("find_media", { type: "image", limit: 50, offset: first.next })) as Found;+ expect(rest.assets).toHaveLength(19);+ expect(rest.next).toBeUndefined();+ });++ test("another chat's unplaced media stays hidden, as it does in the snapshot", async () => {+ const out = (await runAiTool("find_media", { query: "private" })) as Found;+ expect(out.total).toBe(0);+ });++ test("the chat is offered the tool", () => {+ expect(AI_PANEL_TOOLS.some((t) => t.name === "find_media")).toBe(true);+ });+});diff --git a/site/src/cut/lib/importQueue.ts b/site/src/cut/lib/importQueue.tsindex 2c94b750..7101476e 100644--- a/site/src/cut/lib/importQueue.ts+++ b/site/src/cut/lib/importQueue.ts@@ -213,12 +213,12 @@ async function run(job: Job) {upload: undefined,...(asset.type === "image" ? { thumbs: [url] } : {}),});- // A ProRes master's preview proxy is built once the bytes are at rest in- // the project's own storage, which is now.+ // A ProRes master's preview proxy, or a heavy cloud video's, is built+ // once the bytes are at rest in the project's own storage, which is now.const landed = useEditor.getState().assets.find((a) => a.id === asset.id);if (landed) {void import("./mediaProxy")- .then((m) => (m.needsProxy(landed) ? m.ensureProxy(job.projectId, landed, job.backend) : undefined))+ .then((m) => (m.needsProxy(landed, job.backend.kind === "cloud") ? m.ensureProxy(job.projectId, landed, job.backend) : undefined)).catch(() => {});}// Decoders and voices carry across the URL change on what they alreadydiff --git a/site/src/cut/lib/keyframeKept.test.ts b/site/src/cut/lib/keyframeKept.test.tsnew file mode 100644index 00000000..ec34ecce--- /dev/null+++ b/site/src/cut/lib/keyframeKept.test.ts@@ -0,0 +1,32 @@+import { beforeEach, expect, test } from "bun:test";+import { runAiTool } from "./aiTools";+import { useEditor } from "./store";+import type { TextOverlay } from "./types";++/** A merge that leaves keys the call never named says so: the chat sent the+ * same three keys twenty times, each time getting back a fourth it meant to+ * drop, because nothing in the result told it the old key had stayed. */++const t1: TextOverlay = { id: "t1", text: "2025", start: 0, end: 2, x: 0.5, y: 0.5, size: 88, font: "sf", color: "#fff" } as TextOverlay;++beforeEach(async () => {+ useEditor.setState({ overlays: [t1] });+ await runAiTool("set_overlay_keyframes", { id: "t1", keys: [{ t: 0, y: 0.49 }, { t: 1, y: 0.53 }, { t: 1.5, y: 0.49 }] });+});++test("keys the call left in place come back named, with the way to drop them", async () => {+ const out = (await runAiTool("set_overlay_keyframes", { id: "t1", keys: [{ t: 0, y: 0.56 }, { t: 1.5, y: 0.56 }] })) as {+ kept?: number[];+ note?: string;+ };+ expect(out.kept).toEqual([1]);+ expect(out.note).toContain("empty keys list");+});++test("a call that names every key, or clears the track, reports nothing kept", async () => {+ const all = (await runAiTool("set_overlay_keyframes", { id: "t1", keys: [{ t: 0, y: 0.56 }, { t: 1, y: 0.56 }, { t: 1.5, y: 0.56 }] })) as { kept?: number[] };+ expect(all.kept).toBeUndefined();+ const cleared = (await runAiTool("set_overlay_keyframes", { id: "t1", keys: [] })) as { kept?: number[]; keys: unknown[] };+ expect(cleared.kept).toBeUndefined();+ expect(cleared.keys).toHaveLength(0);+});diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.tsindex 4ccbaa3d..e1b06beb 100644--- a/site/src/cut/lib/media.ts+++ b/site/src/cut/lib/media.ts@@ -2136,9 +2136,10 @@ export async function enrichAsset(asset: MediaAsset, src = asset.url) {}}// A ProRes master previews through the WASM decoder until its proxy- // lands; the proxy is built behind the editor, once the bytes are at- // rest in the project's own storage.- if (openProject && !asset.upload && needsProxy(asset)) void ensureProxy(openProject, asset);+ // lands, and a heavy video streamed from the cloud stalls at its cuts;+ // the proxy is built behind the editor, once the bytes are at rest in+ // the project's own storage.+ if (openProject && !asset.upload && needsProxy(asset, getBackend().kind === "cloud")) void ensureProxy(openProject, asset);// Footage stored without a length — a shelf row that was never measured// — reads its own before anything is swept from it: a zero-length sweep// has no tiles to place and no buckets to fill, and the clips cut fromdiff --git a/site/src/cut/lib/mediaProxy.test.ts b/site/src/cut/lib/mediaProxy.test.tsindex 6ecbf1b9..5a952616 100644--- a/site/src/cut/lib/mediaProxy.test.ts+++ b/site/src/cut/lib/mediaProxy.test.ts@@ -17,7 +17,7 @@ await stubModule<typeof import("./mediaRead")>("./mediaRead", import.meta.url, {return { hasVideo: true };}) as never,});-const { ensureProxy, needsProxy, proxyUnreadable, useProxyJobs } = await import("./mediaProxy");+const { ensureProxy, HEAVY_PROXY_MAX_HEIGHT, needsProxy, proxyUnreadable, useProxyJobs } = await import("./mediaProxy");const { useEditor } = await import("./store");const prores = (id: string, extra: Partial<MediaAsset> = {}): MediaAsset => ({@@ -31,6 +31,27 @@ const prores = (id: string, extra: Partial<MediaAsset> = {}): MediaAsset => ({...extra,});+/** A Pexels-style H.264 master: 4K at about 20 Mbps unless told otherwise. */+const h264 = (id: string, extra: Partial<MediaAsset> = {}): MediaAsset => ({+ id,+ fileName: `${id}.mp4`,+ name: `${id}.mp4`,+ type: "video",+ duration: 10,+ width: 3840,+ height: 2160,+ sizeBytes: 25_000_000,+ url: `https://example.test/${id}.mp4`,+ color: { matrix: "bt709", fullRange: false, bitDepth: 8, detected: "rec709", codec: "avc1" },+ ...extra,+});++/** What the browser answers when asked whether it decodes the proxy's HEVC. */+let hevc = true;+(globalThis as { VideoDecoder?: unknown }).VideoDecoder = {+ isConfigSupported: async () => ({ supported: hevc }),+};+function backend(kind: CutBackend["kind"], answer: (path: string, body: unknown) => Response) {const calls: { path: string; body: unknown }[] = [];const fetch = mock(async (path: string, init?: RequestInit) => {@@ -54,6 +75,23 @@ describe("needsProxy", () => {expect(needsProxy(prores("a", { color: undefined }))).toBe(false);expect(needsProxy(prores("a", { type: "audio" }))).toBe(false);});++ // A fast cut streamed from the cloud opens a new file at every cut, and a+ // 4K or high-bitrate master takes longer to reach its frame than the shot+ // lasts. Its preview reads a lighter copy; a file on this machine, read+ // off its own disk, does not need one.+ test("a heavy streamed video needs one: over 1080p, or over the bitrate line", () => {+ expect(needsProxy(h264("uhd"), true)).toBe(true);+ expect(needsProxy(h264("qhd", { width: 2560, height: 1440, sizeBytes: 5_000_000 }), true)).toBe(true);+ expect(needsProxy(h264("dense", { width: 1920, height: 1080, sizeBytes: 22_000_000 }), true)).toBe(true);+ });++ test("a light streamed video, or any video read off this machine, does not", () => {+ expect(needsProxy(h264("hd", { width: 1920, height: 1080, sizeBytes: 7_000_000 }), true)).toBe(false);+ expect(needsProxy(h264("wide", { width: 2048, height: 1080, sizeBytes: 6_000_000 }), true)).toBe(false);+ expect(needsProxy(h264("uhd"))).toBe(false);+ expect(needsProxy(h264("uhd", { proxy: { fileName: "uhd.proxy.mp4", sizeBytes: 1 } }), true)).toBe(false);+ });});describe("ensureProxy", () => {@@ -94,6 +132,35 @@ describe("ensureProxy", () => {expect(useEditor.getState().assets[0].proxy).toEqual({ fileName: "a.proxy.mp4", sizeBytes: 9 });});+ test("a heavy cloud video asks the worker for a copy no taller than 1080p", async () => {+ hevc = true;+ const asset = h264("a");+ useEditor.setState({ projectId: "p", assets: [asset] });+ const b = backend("cloud", () => Response.json({ fileName: "a.proxy.mp4", sizeBytes: 9 }));+ await ensureProxy("p", asset, b.api);+ expect(b.calls[0]).toEqual({ path: "/api/cut/projects/p/proxy", body: { file: "a.mp4", maxHeight: HEAVY_PROXY_MAX_HEIGHT } });+ expect(HEAVY_PROXY_MAX_HEIGHT).toBeLessThanOrEqual(1080);+ expect(useEditor.getState().assets[0].proxy).toEqual({ fileName: "a.proxy.mp4", sizeBytes: 9 });+ });++ test("a browser that cannot play the proxy's HEVC keeps the master and builds nothing", async () => {+ hevc = false;+ const asset = h264("a");+ useEditor.setState({ projectId: "p", assets: [asset] });+ const b = backend("cloud", () => Response.json({ fileName: "a.proxy.mp4", sizeBytes: 9 }));+ await ensureProxy("p", asset, b.api);+ hevc = true;+ expect(b.calls).toHaveLength(0);+ expect(useEditor.getState().assets[0].proxy).toBeUndefined();+ });++ test("a heavy video in a Mac project is left alone", async () => {+ features = new Set(["media.proxy"]);+ const b = backend("local", () => Response.json({}));+ await ensureProxy("p", h264("a"), b.api);+ expect(b.calls).toHaveLength(0);+ });+test("a failure leaves the asset on the master and says why", async () => {features = new Set(["media.proxy"]);const asset = prores("a");@@ -129,7 +196,6 @@ describe("proxyUnreadable", () => {const b = backend("local", () => Response.json({ fileName: "gone.proxy.mp4", sizeBytes: 44 }));const healed = proxyUnreadable(asset, b.api);// The master plays from the moment the reader gives up.- expect(useEditor.getState().assets[0].proxy).toBeUndefined();expect(useEditor.getState().assets[0].proxyUrl).toBeUndefined();await healed;expect(b.calls[0].path).toBe("/api/cut/projects/p/proxy");@@ -159,6 +225,24 @@ describe("proxyUnreadable", () => {expect(useEditor.getState().assets[0].proxy).toBeUndefined();});+ test("a browser that cannot decode the proxy plays the master and leaves the doc's proxy alone", async () => {+ // Firefox has no HEVC: the proxy is fine, only this page cannot play it.+ // Dropping it from the doc would take it from every other viewer too.+ hevc = false;+ const asset = broken("nohevc");+ useEditor.setState({ projectId: "p", assets: [asset] });+ const b = backend("cloud", () => Response.json({}));+ await proxyUnreadable(asset, b.api);+ expect(useEditor.getState().assets[0].proxy).toEqual(asset.proxy!);+ expect(useEditor.getState().assets[0].proxyUrl).toBeUndefined();+ expect(b.calls).toHaveLength(0);+ // A link refresh re-mints the master's address but keeps the proxy off.+ useEditor.getState().applyMediaUrls(new Map([["nohevc.MOV", "https://x/m"], ["nohevc.proxy.mp4", "https://x/p"]]));+ expect(useEditor.getState().assets[0].url).toBe("https://x/m");+ expect(useEditor.getState().assets[0].proxyUrl).toBeUndefined();+ hevc = true;+ });+test("a shared view drops the proxy for itself and builds nothing", async () => {const asset = broken("viewer");useEditor.setState({ projectId: "p", assets: [asset], readOnly: true });diff --git a/site/src/cut/lib/mediaProxy.ts b/site/src/cut/lib/mediaProxy.tsindex 201cb3b8..a2eda365 100644--- a/site/src/cut/lib/mediaProxy.ts+++ b/site/src/cut/lib/mediaProxy.ts@@ -1,7 +1,8 @@"use client";/**- * The preview proxy of a ProRes master.+ * The preview proxy of a ProRes master, or of a heavy video streamed from the+ * cloud.** A ProRes file has no hardware decoder in any browser. The master stays in* the project — an export reads it — and beside it lands a 10-bit HEVC (or@@ -10,6 +11,12 @@* preview decodes the master through the WASM decoder: slower, and the* same picture.*+ * A cloud project streams its media, and a fast cut opens a new file at every+ * cut: a 4K or high-bitrate master takes longer to reach its frame than a+ * half-second shot lasts, so the preview stalls at each one. Such a video+ * gets a proxy too, no taller than 1080p, built by the worker. A project on+ * this machine reads its files off its own disk and keeps its masters.+ ** Who builds it follows the residency: the engine for a project on this Mac,* the worker for a cloud project, and for a browser project the page itself* when it has a 10-bit encoder, else the Mac's engine when the app is there@@ -29,13 +36,51 @@ import { uploadProjectMediaTo } from "./media";import { frameSinkIsCustom, probeMediaFile } from "./mediaRead";import { forgetLocalMediaUrl, storedMediaUrl } from "./mediaSync";import { encodeProxyInPage, proxyNameFor, proxyOnTheMac, type Landed } from "./proxyEncode";-import { useEditor } from "./store";+import { releaseProxyHold, useEditor } from "./store";import type { MediaAsset, StoredAsset } from "./types";-/** A video whose master needs a proxy and has none: ProRes (a four-character- * code starting `ap`), read off the header at import. */-export function needsProxy(asset: Pick<StoredAsset, "type" | "color" | "proxy" | "block">): boolean {- return asset.type === "video" && !asset.proxy && !asset.block && !!asset.color?.codec?.startsWith("ap");+/** Past this many pixels a frame is heavier than 1080p (a 2048×1080 DCI+ * frame is not). */+export const HEAVY_PIXELS = 1920 * 1080 * 1.1;+/** Past this average bitrate a master streams slower than a fast cut plays. */+export const HEAVY_BITS_PER_SECOND = 12_000_000;+/** The tallest proxy a heavy video gets: the stage is never larger. */+export const HEAVY_PROXY_MAX_HEIGHT = 1080;++type ProxyFacts = Pick<StoredAsset, "type" | "color" | "proxy" | "block" | "width" | "height" | "sizeBytes" | "duration">;++/** ProRes, by the four-character code read off the header at import. */+const isProres = (asset: Pick<StoredAsset, "color">) => !!asset.color?.codec?.startsWith("ap");++/** A video too large or too dense to stream at a fast cut's pace. */+export function isHeavyVideo(asset: ProxyFacts): boolean {+ if (asset.type !== "video") return false;+ if ((asset.width ?? 0) * (asset.height ?? 0) > HEAVY_PIXELS) return true;+ return !!asset.sizeBytes && asset.duration > 0 && (asset.sizeBytes * 8) / asset.duration > HEAVY_BITS_PER_SECOND;+}++/** A video whose master needs a proxy and has none: a ProRes master, or a+ * heavy video when the project streams its media (`streamed`, a cloud+ * project). */+export function needsProxy(asset: ProxyFacts, streamed = false): boolean {+ if (asset.type !== "video" || asset.proxy || asset.block) return false;+ return isProres(asset) || (streamed && isHeavyVideo(asset));+}++/** The codec string of the worker's proxy: HEVC Main10. */+const PROXY_DECODE_CODEC = "hvc1.2.4.L123.B0";++/** Whether this browser decodes the proxy a heavy video would get. A heavy+ * master plays here already, only slowly, so where HEVC does not decode it+ * stays on the master rather than trading it for a file that cannot play. */+async function proxyDecodes(): Promise<boolean> {+ const decoder = (globalThis as { VideoDecoder?: { isConfigSupported?: (c: VideoDecoderConfig) => Promise<{ supported?: boolean }> } }).VideoDecoder;+ if (!decoder?.isConfigSupported) return false;+ try {+ return !!(await decoder.isConfigSupported({ codec: PROXY_DECODE_CODEC, codedWidth: 1920, codedHeight: 1080 })).supported;+ } catch {+ return false;+ }}/** What the panel shows about an asset's proxy. */@@ -79,8 +124,12 @@ export async function ensureProxy(// chat tool and reads the master; the page queues the proxy when the// project is next opened.if (frameSinkIsCustom()) return;- if (!needsProxy(asset) || asset.upload || inFlight.has(asset.id)) return;+ if (!needsProxy(asset, backend.kind === "cloud") || asset.upload || inFlight.has(asset.id)) return;inFlight.add(asset.id);+ if (!isProres(asset) && !(await proxyDecodes())) {+ inFlight.delete(asset.id);+ return;+ }const jobs = useProxyJobs.getState();const progress = (p: number) => jobs.set(asset.id, { kind: "making", progress: Math.max(0, Math.min(1, p)) });progress(0);@@ -113,7 +162,14 @@ export async function proxyUnreadable(asset: MediaAsset, backend: CutBackend = gconst live = s.assets.find((a) => a.id === asset.id);// Dropped or replaced since the reader tried it.if (!live?.proxy || live.proxyUrl !== asset.proxyUrl) return;+ // The master plays from this moment; whether the proxy leaves the doc+ // waits on why it failed.+ s.holdProxyOff(asset.id);+ // A page with no HEVC decoder cannot play the worker's proxy, however+ // sound the file: the proxy stays on the doc for the viewers who can.+ if (!(await proxyDecodes())) return;const broken = live.proxy;+ releaseProxyHold(asset.id);s.dropAssetProxy(asset.id);const projectId = s.projectId;if (!projectId || s.readOnly || frameSinkIsCustom()) return;@@ -139,7 +195,8 @@ export async function proxyUnreadable(asset: MediaAsset, backend: CutBackend = gexport function ensureProjectProxies(): void {const s = useEditor.getState();if (!s.projectId || s.readOnly) return;- for (const asset of s.assets) if (needsProxy(asset)) void ensureProxy(s.projectId, asset);+ const streamed = getBackend().kind === "cloud";+ for (const asset of s.assets) if (needsProxy(asset, streamed)) void ensureProxy(s.projectId, asset);}async function buildProxy(@@ -163,10 +220,13 @@ async function buildProxy(return { fileName: body.fileName, sizeBytes: body.sizeBytes };}if (backend.kind === "cloud") {+ // A heavy video's proxy is there to stream light; a ProRes master's+ // keeps the account's size.+ const light = isProres(asset) ? {} : { maxHeight: Math.min(proxyMaxHeight, HEAVY_PROXY_MAX_HEIGHT) };const res = await backend.fetch(`/api/cut/projects/${projectId}/proxy`, {method: "POST",headers: { "Content-Type": "application/json" },- body: JSON.stringify({ file: asset.fileName }),+ body: JSON.stringify({ file: asset.fileName, ...light }),});const started = (await res.json().catch(() => ({}))) as Partial<Landed> & { jobId?: string; error?: string };if (!res.ok) throw new Error(started.error ?? "Could not make the preview proxy.");diff --git a/site/src/cut/lib/projectNotesTool.test.ts b/site/src/cut/lib/projectNotesTool.test.tsnew file mode 100644index 00000000..83b0b8c7--- /dev/null+++ b/site/src/cut/lib/projectNotesTool.test.ts@@ -0,0 +1,34 @@+import { beforeEach, expect, test } from "bun:test";+import { buildAiContext } from "./aiContext";+import { runAiTool } from "./aiTools";+import { useEditor } from "./store";+import { SIDE_PANEL_TOOLS } from "../components/SidePanel.tools";++/** The project's own notes — the Details tab's date, links and text — are+ * where a cut keeps its source. The chat had no way to write them, so a+ * template's original link went into the account's Notes instead, where+ * nobody opening the project would see it. */++beforeEach(() => useEditor.setState({ notes: { text: "", publishedAt: "", links: [] } }));++test("set_project_notes adds a link without dropping the ones already there", async () => {+ useEditor.getState().setNotes({ links: ["https://a.test/1"] });+ const out = (await runAiTool("set_project_notes", { add_links: ["https://www.instagram.com/reel/X/", "https://a.test/1"] })) as {+ links: string[];+ };+ expect(out.links).toEqual(["https://a.test/1", "https://www.instagram.com/reel/X/"]);+ expect(useEditor.getState().notes.links).toEqual(out.links);+});++test("text, date and a whole link list can be set; a bad date is refused", async () => {+ await runAiTool("set_project_notes", { text: "Replica of a recap reel", published_at: "2026-10-13", links: ["https://b.test"] });+ expect(useEditor.getState().notes).toEqual({ text: "Replica of a recap reel", publishedAt: "2026-10-13", links: ["https://b.test"] });+ await expect(runAiTool("set_project_notes", { published_at: "13/10/2026" })).rejects.toThrow();+ await expect(runAiTool("set_project_notes", {})).rejects.toThrow();+});++test("the chat sees the notes and the tool", () => {+ useEditor.getState().setNotes({ links: ["https://c.test"] });+ expect(JSON.stringify(buildAiContext())).toContain("https://c.test");+ expect(SIDE_PANEL_TOOLS.some((t) => t.name === "set_project_notes")).toBe(true);+});diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.tsindex 06b96ff3..2da98220 100644--- a/site/src/cut/lib/store.ts+++ b/site/src/cut/lib/store.ts@@ -97,7 +97,7 @@ import { laneCues, subtitleLaneCount, trackLocale } from "./subtitles";import { cueEmphasis, mergeEmphasis, remapEmphasis, splitEmphasis, toggleEmphasis, withEmphasis } from "./captionEmphasis";import { clipboardItemAssetIds, clipboardItemFor, listedAssetIds, type TimelineClipboardItem } from "./itemKinds";import { ANIM_STYLE_IDS, animStyleOfTransition, assetIsSilent, CLIP_MIN_SECONDS, clipPoseAt, DEFAULT_BACKGROUND, emptySubtitles, frameOf, IMAGE_CLIP_SECONDS, isAudioTransition, fontAssetId, isEffectOverlay, isStickerOverlay, MAX_SUBTITLE_LANES, mediaUrl, migrateBehindSubject, migrateLegacyTransitions, normalizeAspect, overlayAnimStyle, projectBackground, SPEED_FLOOR, SPEED_MIN, stampOverlayKinds, stripDefaultOverlayKinds, isTimelineId, sanitizeTimelines, timelineAspect, TRANSITION_MAX, TRANSITION_STYLE_IDS, transitionBarAt, transitionBarStart, transitionStyleOfAnim, type TransitionBoundaryKind } from "./types";-import { clampFeather, takesFeather } from "./transitionShape";+import { clampBand, clampFeather, takesBand, takesFeather } from "./transitionShape";import { liftMoveTracks } from "./textMotion";import { OWN_SOUND, soundRoom, soundSourceOf, soundWindow, SPLIT_EDIT_MAX_S } from "./soundSource";import { cutSound } from "./soundSettings";@@ -605,6 +605,9 @@ export interface EditorState {/** Take a proxy that would not open off its asset, so every reader falls* back to the master. A read-only view drops it for this session only. */dropAssetProxy: (id: string) => void;+ /** This page cannot decode the asset's proxy: play the master here and+ * keep the proxy on the doc for the viewers who can. */+ holdProxyOff: (id: string) => void;/** Give a video asset stored without a length the duration and size a probe* read off its file, and open every zero-length clip cut from it to the* footage — up to the next clip on its track, so nothing overlaps. A doc@@ -1505,6 +1508,12 @@ export const DOC_KEYS = ["renders",] as const;let hydrating = false;+/** Assets whose proxy this page cannot decode (`holdProxyOff`): their links+ * are minted without one, while the doc keeps it. */+const proxyOffHere = new Set<string>();+const proxyOn = (a: { id: string; proxy?: unknown }) => !!a.proxy && !proxyOffHere.has(a.id);+/** A held proxy is going to be dropped or replaced: links mint it again. */+export const releaseProxyHold = (id: string) => void proxyOffHere.delete(id);/*** The invariants every write to the store passes through, wherever it comes@@ -2089,7 +2098,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {stale.map((a) => ({...a,url: held.get(a.fileName) ?? links?.urls.get(a.fileName) ?? mediaUrl(id, a.fileName),- ...(a.proxy ? { proxyUrl: links?.urls.get(a.proxy.fileName) ?? mediaUrl(id, a.proxy.fileName) } : {}),+ ...(a.proxy && proxyOn(a) ? { proxyUrl: links?.urls.get(a.proxy.fileName) ?? mediaUrl(id, a.proxy.fileName) } : {}),})),ui);@@ -2144,7 +2153,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {...(kept?.sceneCuts !== undefined ? { sceneCuts: kept.sceneCuts } : {}),...(kept?.peaks !== undefined ? { peaks: kept.peaks } : {}),url: mediaUrl(id, a.fileName),- ...(a.proxy ? { proxyUrl: mediaUrl(id, a.proxy.fileName) } : {}),+ ...(a.proxy && proxyOn(a) ? { proxyUrl: mediaUrl(id, a.proxy.fileName) } : {}),};});// Cloud and shared media ride signed R2 URLs, batch-minted once per@@ -2172,7 +2181,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {if (!current()) return;for (const a of assets) {a.url = held.get(a.fileName) ?? signed.urls.get(a.fileName) ?? a.url;- if (a.proxy) a.proxyUrl = signed.urls.get(a.proxy.fileName) ?? a.proxyUrl;+ if (a.proxy && proxyOn(a)) a.proxyUrl = signed.urls.get(a.proxy.fileName) ?? a.proxyUrl;}markSignedBatch(id, signed.expiresAt);// A browser project's "mint" hands back session blob URLs, dead on@@ -2694,6 +2703,26 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {}},+ holdProxyOff: (id) => {+ proxyOffHere.add(id);+ const target = get().assets.find((a) => a.id === id);+ if (!target?.proxyUrl) return;+ // Only the page's address goes; `proxy` stays, so the doc saves it.+ hydrating = true;+ try {+ set((s) => ({+ assets: s.assets.map((a) => {+ if (a.id !== id) return a;+ const rest = { ...a };+ delete rest.proxyUrl;+ return rest;+ }),+ }));+ } finally {+ hydrating = false;+ }+ },+renameAsset: (id, name) =>get().updateAsset(id, {name: freeItemName(name, get().assets.filter((a) => a.id !== id).map((a) => a.name)),@@ -2775,7 +2804,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {// links that expire.if (a.upload || resolveRegisteredBlob(a.url)) return a;const url = urls.get(a.fileName) ?? a.url;- const proxyUrl = a.proxy ? (urls.get(a.proxy.fileName) ?? a.proxyUrl) : a.proxyUrl;+ const proxyUrl = a.proxy && proxyOn(a) ? (urls.get(a.proxy.fileName) ?? a.proxyUrl) : a.proxyUrl;if (url === a.url && proxyUrl === a.proxyUrl) return a;// A still's filmstrip is its source URL, so it moves with it.const thumbs =@@ -5744,6 +5773,7 @@ export function deriveTransitionFields(// Only a style with a reveal edge has anything to soften.const transitionFeather =cut && takesFeather(cut.style) && clampFeather(cut.feather) > 0 ? clampFeather(cut.feather) : undefined;+ const transitionBand = cut && takesBand(cut.style) && clampBand(cut.band) > 0 ? clampBand(cut.band) : undefined;const animOf = (t: TimelineTransition | undefined): ClipAnim | undefined => {if (!t) return undefined;// Only a style with an edge ramp reaches an open edge — a sound@@ -5762,12 +5792,13 @@ export function deriveTransitionFields(c.transition === transition &&c.transitionStyle === transitionStyle &&c.transitionFeather === transitionFeather &&+ c.transitionBand === transitionBand &&sameAnim(c.animIn, animIn) &&sameAnim(c.animOut, animOut))return c;changed = true;- return { ...c, transition, transitionStyle, transitionFeather, animIn, animOut };+ return { ...c, transition, transitionStyle, transitionFeather, transitionBand, animIn, animOut };});return changed ? next : clips;}@@ -5786,6 +5817,7 @@ function sanitizeTransitions(raw: TimelineTransition[] | undefined): TimelineTraseconds: clampBarSeconds(t.seconds),style: TRANSITION_STYLE_IDS.includes(t.style) ? t.style : "crossfade",...(clampFeather(t.feather) > 0 ? { feather: clampFeather(t.feather) } : {}),+ ...(clampBand(t.band) > 0 ? { band: clampBand(t.band) } : {}),...(t.hidden ? { hidden: true as const } : {}),}));return bars.filter(diff --git a/site/src/cut/lib/transitionBand.test.ts b/site/src/cut/lib/transitionBand.test.tsnew file mode 100644index 00000000..de5930d9--- /dev/null+++ b/site/src/cut/lib/transitionBand.test.ts@@ -0,0 +1,135 @@+import { beforeAll, describe, expect, test } from "bun:test";+import { spawnSync } from "node:child_process";+import { existsSync } from "node:fs";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { Canvas } from "skia-canvas";+import { FrameCompositor, type Frame } from "./composite";+import { setRasterFactory, type RasterSurface } from "./raster";+import { bandEdge, clampBand, takesBand } from "./transitionShape";+import { xfadeTransition } from "../server/transitionExpr";+import type { TransitionStyle, VideoClip } from "./types";++/**+ * A wipe with a band: a solid white panel runs just ahead of the reveal+ * edge, so the outgoing shot is wiped first by white and then by the+ * incoming shot — the white-panel slide of a recap montage. A gray shot+ * hands over to a black one, compared on the compositor's canvas and in+ * the ffmpeg export's xfade expression.+ */++const W = 160;+const H = 90;+const FPS = 30;+const SECONDS = 0.5;+const FRAMES = Math.round(SECONDS * FPS);+const BUNDLED_FFMPEG = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../vendor/donkey-tools/ffmpeg");+const FFMPEG = existsSync(BUNDLED_FFMPEG) ? BUNDLED_FFMPEG : "ffmpeg";++beforeAll(() => {+ setRasterFactory({+ createCanvas: (w, h) => new Canvas(w, h) as unknown as RasterSurface,+ decodeImage: async () => null,+ canvasToBlob: async () => new Blob(),+ snapshot: async (canvas) => canvas as unknown as ImageBitmap,+ });+});++function solid(color: string): Frame {+ const c = new Canvas(W, H);+ const ctx = c.getContext("2d");+ ctx.fillStyle = color;+ ctx.fillRect(0, 0, W, H);+ return { kind: "ready", image: c as unknown as CanvasImageSource, width: W, height: H };+}++const clipOf = (over: Partial<VideoClip> = {}): VideoClip => ({+ id: "c1",+ assetId: "a1",+ track: 0,+ start: 0,+ in: 0,+ out: 4,+ muted: false,+ transition: SECONDS,+ ...over,+});++function canvasFrame(style: TransitionStyle, p: number, band: number): Uint8Array {+ const canvas = new Canvas(W, H);+ const comp = new FrameCompositor(canvas as unknown as HTMLCanvasElement);+ comp.drawCrossJoin(+ style,+ p,+ {+ masterFrame: solid("#808080"),+ masterClip: clipOf({ transitionStyle: style, transitionBand: band }),+ masterAlpha: 1,+ masterZoom: 1,+ masterFx: { dx: 0, dy: 0 },+ incFrame: solid("#000000"),+ incClip: clipOf({ id: "c2" }),+ incAlpha: p,+ incZoom: 1,+ },+ 0+ );+ const rgba = canvas.getContext("2d").getImageData(0, 0, W, H).data;+ const out = new Uint8Array(W * H);+ for (let i = 0; i < W * H; i++) out[i] = rgba[i * 4];+ return out;+}++function ffmpegFrames(style: TransitionStyle, band: number): Uint8Array[] {+ const opt = xfadeTransition(style, undefined, SECONDS, W, H, band);+ const graph =+ `color=c=0x808080:s=${W}x${H}:r=${FPS}:d=${SECONDS},format=yuv420p[a];` ++ `color=c=black:s=${W}x${H}:r=${FPS}:d=${SECONDS * 2},format=yuv420p[b];` ++ `[a][b]xfade=${opt}:duration=${SECONDS}:offset=0,format=gray[out]`;+ const run = spawnSync(FFMPEG, ["-v", "error", "-filter_complex", graph, "-map", "[out]", "-frames:v", String(FRAMES), "-f", "rawvideo", "-"]);+ expect(run.stderr.toString()).toBe("");+ expect(run.status).toBe(0);+ // The band is the limited-range white code, which this raw gray readout+ // leaves at 235 where the canvas reads 255.+ return [...Array(FRAMES).keys()].map((n) =>+ new Uint8Array(run.stdout.subarray(n * W * H, (n + 1) * W * H)).map((v) => (v >= 230 ? 255 : v))+ );+}++const meanDiff = (a: Uint8Array, b: Uint8Array) => a.reduce((s, v, i) => s + Math.abs(v - b[i]), 0) / a.length;+const at = (f: Uint8Array, x: number, y: number) => f[Math.round(y) * W + Math.round(x)];++describe("a wipe's white band", () => {+ test("wipes take a band, other styles don't, and it is clamped", () => {+ expect(takesBand("wiperight")).toBe(true);+ expect(takesBand("crossfade")).toBe(false);+ expect(clampBand(2)).toBe(0.5);+ expect(clampBand(-1)).toBe(0);+ });++ test("the band enters off-frame, crosses, and leaves off-frame", () => {+ expect(bandEdge(0, 0.25)).toBeCloseTo(-0.25, 6);+ expect(bandEdge(1, 0.25)).toBeCloseTo(1, 6);+ expect(bandEdge(0.5, 0.25)).toBeCloseTo(0.375, 6);+ });++ test("mid-wipe: incoming behind the edge, white ahead of it, outgoing past the band", () => {+ const f = canvasFrame("wiperight", 0.5, 0.25);+ expect(at(f, 0.2 * W, H / 2)).toBeLessThan(10);+ expect(at(f, 0.5 * W, H / 2)).toBeGreaterThan(245);+ expect(Math.abs(at(f, 0.8 * W, H / 2) - 128)).toBeLessThan(6);+ const up = canvasFrame("wipeup", 0.5, 0.25);+ expect(at(up, W / 2, 0.8 * H)).toBeLessThan(10);+ expect(at(up, W / 2, 0.5 * H)).toBeGreaterThan(245);+ expect(Math.abs(at(up, W / 2, 0.2 * H) - 128)).toBeLessThan(6);+ });++ for (const style of ["wiperight", "wipeleft", "wipedown"] as TransitionStyle[]) {+ test(`${style} with a band renders the same frames in ffmpeg`, () => {+ const frames = ffmpegFrames(style, 0.3);+ for (let n = 1; n < FRAMES; n += 2) {+ expect(meanDiff(canvasFrame(style, n / FRAMES, 0.3), frames[n])).toBeLessThan(6);+ }+ });+ }+});diff --git a/site/src/cut/lib/transitionShape.ts b/site/src/cut/lib/transitionShape.tsindex d03c26f1..77b63e21 100644--- a/site/src/cut/lib/transitionShape.ts+++ b/site/src/cut/lib/transitionShape.ts@@ -38,6 +38,28 @@ export const SLICE_TAPS = 5;/** The clock wipe's sweep from the top edge down to the left one. */export const CLOCK_SWEEP = Math.PI / 2;+/** The wipes that can run a solid white band just ahead of their edge. */+export const BAND_STYLES: TransitionStyle[] = ["wipeleft", "wiperight", "wipeup", "wipedown"];++/** The widest band offered: half the distance the edge crosses. */+export const TRANSITION_BAND_MAX = 0.5;++/** Whether `style` takes a white band. */+export const takesBand = (style: TransitionStyle | undefined): boolean => !!style && BAND_STYLES.includes(style);++/** A clip's stored band brought into range; 0 = no band. */+export const clampBand = (v: number | undefined): number =>+ Math.min(TRANSITION_BAND_MAX, Math.max(0, Number.isFinite(v) ? (v as number) : 0));++/**+ * Where a banded wipe's reveal edge stands at progress `p`, as a share of+ * the distance it crosses. The edge travels the frame plus the band, so the+ * band enters from off-frame and leaves past the far edge: e.g. a 0.25 band+ * stands at -0.25 at the start, 0.375 halfway and 1 at the end. With no+ * band it is plain progress.+ */+export const bandEdge = (p: number, band: number): number => Math.min(1, Math.max(0, p)) * (1 + band) - band;+/** Whether `style` takes a soft edge. */export const takesFeather = (style: TransitionStyle | undefined): boolean =>!!style && FEATHER_STYLES.includes(style);diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.tsindex 51e0a14c..769d1428 100644--- a/site/src/cut/lib/types.ts+++ b/site/src/cut/lib/types.ts@@ -287,7 +287,8 @@ export interface StoredAsset {/** The person's override of what this source's code values mean. */colorProfile?: SourceProfile;/** A preview proxy of a master the browser cannot play at speed (a ProRes- * master, an HDR file on a browser that tone-maps): a bt709-tagged HEVC+ * master, an HDR file on a browser that tone-maps, a 4K or high-bitrate+ * video streamed from the cloud): a bt709-tagged HEVC* file beside the master in the project's media. The preview reads it; an* export reads the master. */proxy?: { fileName: string; sizeBytes: number };@@ -694,6 +695,9 @@ export interface VideoClip {/** Softness of that transition's reveal edge (the bar's `feather`);* absent = a hard edge. */transitionFeather?: number;+ /** Width of the white band running ahead of that wipe's edge (the bar's+ * `band`); absent = none. */+ transitionBand?: number;/** Entrance animation on this clip's own head (absent = none). Unlike a* transition it belongs to one clip and never moves its neighbors. */animIn?: ClipAnim;@@ -825,6 +829,10 @@ export interface TimelineTransition {* (0..TRANSITION_FEATHER_MAX); absent = a hard edge. Wipes, circles and* splits take it. */feather?: number;+ /** A solid white panel running just ahead of a wipe's edge, as a share of+ * the frame the edge crosses (0..TRANSITION_BAND_MAX); absent = none.+ * Wipes take it; it draws a hard edge, so it overrides a feather. */+ band?: number;/** Hidden bars stay on the row (grayed) and keep the boundary they line up* with, so nothing else claims it, but play nothing — the cut renders hard* in the preview and in every export. */diff --git a/site/src/cut/lib/urlBatch.test.ts b/site/src/cut/lib/urlBatch.test.tsnew file mode 100644index 00000000..d4913732--- /dev/null+++ b/site/src/cut/lib/urlBatch.test.ts@@ -0,0 +1,81 @@+import { describe, expect, test } from "bun:test";+import { importEach, URL_BATCH_MAX, URL_DOWNLOADS_MAX, URL_BUSY_MESSAGE } from "./urlBatch";++describe("importEach", () => {+ test("answers in the order asked, a few at a time", async () => {+ let live = 0;+ let peak = 0;+ const urls = ["a", "b", "c", "d", "e", "f", "g"];+ const out = await importEach(urls, async (u) => {+ live++;+ peak = Math.max(peak, live);+ await new Promise((r) => setTimeout(r, u === "a" ? 30 : 5));+ live--;+ return u.toUpperCase();+ });+ expect(out.map((r) => r.url)).toEqual(urls);+ expect(out.map((r) => ("value" in r ? r.value : null))).toEqual(urls.map((u) => u.toUpperCase()));+ expect(peak).toBeGreaterThan(1);+ // Never more than the local server lets download at once, or the extra+ // lanes are turned away as busy.+ expect(peak).toBeLessThanOrEqual(URL_DOWNLOADS_MAX);+ });++ test("one failure is reported on its own row and the rest still land", async () => {+ const out = await importEach(["ok1", "bad", "ok2"], async (u) => {+ if (u === "bad") throw new Error("404 from host");+ return u;+ });+ expect(out[0]).toEqual({ url: "ok1", value: "ok1" });+ expect(out[1]).toEqual({ url: "bad", error: "404 from host" });+ expect(out[2]).toEqual({ url: "ok2", value: "ok2" });+ });++ test("blank and repeated urls are dropped, and the batch is capped", async () => {+ const out = await importEach([" a ", "", "a", "b"], async (u) => u);+ expect(out.map((r) => r.url)).toEqual(["a", "b"]);+ const many = Array.from({ length: URL_BATCH_MAX + 5 }, (_, i) => `u${i}`);+ await expect(importEach(many, async (u) => u)).rejects.toThrow(String(URL_BATCH_MAX));+ });++ test("a link turned away as busy waits its turn and lands", async () => {+ let tries = 0;+ const out = await importEach(+ ["a", "b"],+ async (u) => {+ if (u === "a" && tries++ < 2) throw new Error(URL_BUSY_MESSAGE);+ return u;+ },+ { busyWaitMs: 5 }+ );+ expect(out).toEqual([{ url: "a", value: "a" }, { url: "b", value: "b" }]);+ expect(tries).toBe(3);+ });++ test("a batch answers within its budget: what landed, what is still downloading, what never started", async () => {+ const t0 = Date.now();+ const out = await importEach(+ ["a", "b", "c", "d", "e", "f"],+ async (u) => {+ await new Promise((r) => setTimeout(r, 60));+ return u;+ },+ { budgetMs: 90 }+ );+ expect(Date.now() - t0).toBeLessThan(140);+ expect(out.slice(0, 2)).toEqual([{ url: "a", value: "a" }, { url: "b", value: "b" }]);+ expect(out.slice(2, 4)).toEqual([{ url: "c", pending: true }, { url: "d", pending: true }]);+ expect(out.slice(4)).toEqual([{ url: "e", notStarted: true }, { url: "f", notStarted: true }]);+ });++ test("a link still waiting for a slot when the budget runs out is reported as never started", async () => {+ const out = await importEach(+ ["a"],+ async () => {+ throw new Error(URL_BUSY_MESSAGE);+ },+ { busyWaitMs: 20, budgetMs: 50 }+ );+ expect(out).toEqual([{ url: "a", notStarted: true }]);+ });+});diff --git a/site/src/cut/lib/urlBatch.ts b/site/src/cut/lib/urlBatch.tsnew file mode 100644index 00000000..7bafdf8a--- /dev/null+++ b/site/src/cut/lib/urlBatch.ts@@ -0,0 +1,85 @@+/**+ * Importing a list of URLs at once — the footage of a whole section of a+ * template, say — a few downloads at a time, each answer on its own row in+ * the order asked so a failed link never sinks the rest.+ */++/** The most URLs one batch takes. */+export const URL_BATCH_MAX = 24;++/** How many downloads the local server runs at once (`urlImport.ts` turns+ * the next one away as busy), so a batch never runs more lanes than this. */+export const URL_DOWNLOADS_MAX = 2;++/** What the local server answers when every download slot is taken. */+export const URL_BUSY_MESSAGE = "Too many downloads in progress. Try again in a moment.";++/** How long a batch works before it answers: inside the chat bridge's two+ * minutes a tool call gets (`server/ai/bridge.ts`), with room to describe+ * what landed. A long list answers in parts — the chat calls again with what+ * never started. */+export const URL_BATCH_BUDGET_MS = 90_000;++/** How long a link turned away as busy waits before it asks again: a slot+ * frees when any download ends. */+const BUSY_WAIT_MS = 1500;++/** One URL's answer: what it brought in, why it failed, still downloading+ * when the batch answered (it lands in the project on its own), or never+ * started (ask for it again). */+export type UrlRow<T> =+ | { url: string; value: T }+ | { url: string; error: string }+ | { url: string; pending: true }+ | { url: string; notStarted: true };++const messageOf = (e: unknown) => (e instanceof Error ? e.message : String(e));++/** Thrown inside a lane when the budget runs out while a link waits for a+ * slot: it never started. */+const OUT_OF_TIME = Symbol("out of time");++export async function importEach<T>(+ urls: string[],+ importOne: (url: string) => Promise<T>,+ opts: { busyWaitMs?: number; budgetMs?: number } = {}+): Promise<UrlRow<T>[]> {+ const list = [...new Set(urls.map((u) => u.trim()).filter(Boolean))];+ if (list.length > URL_BATCH_MAX) throw new Error(`At most ${URL_BATCH_MAX} URLs per call.`);+ const wait = opts.busyWaitMs ?? BUSY_WAIT_MS;+ const deadline = Date.now() + (opts.budgetMs ?? URL_BATCH_BUDGET_MS);+ const rows: (UrlRow<T> | undefined)[] = new Array(list.length);+ // A link turned away as busy (a download started elsewhere holds a slot)+ // waits and asks again rather than failing, until the budget runs out.+ const attempt = async (url: string): Promise<T> => {+ for (;;) {+ try {+ return await importOne(url);+ } catch (e) {+ if (messageOf(e) !== URL_BUSY_MESSAGE) throw e;+ if (Date.now() + wait >= deadline) throw OUT_OF_TIME;+ await new Promise((r) => setTimeout(r, wait));+ }+ }+ };+ let next = 0;+ const lane = async () => {+ while (next < list.length && Date.now() < deadline) {+ const i = next++;+ const url = list[i];+ try {+ rows[i] = { url, value: await attempt(url) };+ } catch (e) {+ rows[i] = e === OUT_OF_TIME ? { url, notStarted: true } : { url, error: messageOf(e) };+ }+ }+ };+ const lanes = Promise.all(Array.from({ length: Math.min(URL_DOWNLOADS_MAX, list.length) }, lane));+ let timer: ReturnType<typeof setTimeout> | undefined;+ const outOfTime = new Promise<void>((r) => (timer = setTimeout(r, Math.max(0, deadline - Date.now()))));+ await Promise.race([lanes, outOfTime]);+ clearTimeout(timer);+ // A link a lane took up is still downloading; one no lane reached never+ // started.+ return list.map((url, i) => rows[i] ?? (i < next ? { url, pending: true as const } : { url, notStarted: true as const }));+}diff --git a/site/src/cut/server/cloud/jobs.ts b/site/src/cut/server/cloud/jobs.tsindex 396ba4ff..32c681d8 100644--- a/site/src/cut/server/cloud/jobs.ts+++ b/site/src/cut/server/cloud/jobs.ts@@ -927,14 +927,16 @@ export const jobsCloud = {}},- /** Queue a preview proxy of a ProRes master: the worker builds it and+ /** Queue a preview proxy of a ProRes master, or of a heavy video at the+ * smaller `maxHeight` it asks for: the worker builds it and* registers it beside the master as a quota-exempt object. A proxy already* built answers at once, and a job already on its way answers with that* job, so a project copied to the cloud and then opened queues one. */async proxy(userId: string, projectId: string, req: Request) {try {- const { file } = (await req.json()) as { file?: string };+ const { file, maxHeight: asked } = (await req.json()) as { file?: string; maxHeight?: unknown };if (!file) return err("file is required.", 400);+ const maxHeight = typeof asked === "number" && Number.isFinite(asked) ? Math.round(Math.min(4320, Math.max(360, asked))) : undefined;if (!(await getProject(userId, projectId))) return err("Project not found.", 404);const source = await prisma.cutMediaObject.findFirst({where: { userId, projectId, fileName: file, kind: "media", uploadState: "complete" },@@ -958,7 +960,7 @@ export const jobsCloud = {const capped = await renderJobCheck(userId);if (capped) return capped;const row = await prisma.cutRenderJob.create({- data: { userId, projectId, kind: "proxy", spec: { file } as unknown as Prisma.InputJsonValue },+ data: { userId, projectId, kind: "proxy", spec: { file, ...(maxHeight ? { maxHeight } : {}) } as unknown as Prisma.InputJsonValue },});wakeRenderWorker();return Response.json({ jobId: row.id });diff --git a/site/src/cut/server/exportPipeline.ts b/site/src/cut/server/exportPipeline.tsindex b401acd2..021bcba6 100644--- a/site/src/cut/server/exportPipeline.ts+++ b/site/src/cut/server/exportPipeline.ts@@ -183,6 +183,9 @@ export interface ExportSpec {/** Softness of that transition's reveal edge (lib/transitionShape.ts);* absent = hard. */transitionFeather?: number;+ /** Width of the white band ahead of that wipe's edge+ * (lib/transitionShape.ts); absent = none. */+ transitionBand?: number;/** This clip's own entrance/exit animation, baked into the segment's* head/tail window: fade (audio follows), zoom, pop, or a slide* against black. Unknown styles render as a fade. */@@ -2908,7 +2911,7 @@ async function renderPass(// The style id resolves through the allowlist map; anything unknown// (or an old spec without a style) renders as a plain fade. The shaped// and softened styles come back as xfade expressions.- const kind = xfadeTransition(prev.transitionStyle, prev.transitionFeather, d, W, H);+ const kind = xfadeTransition(prev.transitionStyle, prev.transitionFeather, d, W, H, prev.transitionBand, depth);filters.push(`[${segLabel[j]}]tpad=start_duration=${num(d)}:start_mode=clone[vh${j}]`);filters.push(`[${vAcc}][vh${j}]xfade=${kind}:duration=${num(d)}:offset=${num(offset)}[vx${j}]`);filters.push(`[${aAcc}]afade=t=out:st=${num(offset)}:d=${num(d)}[ah${j}]`);diff --git a/site/src/cut/server/proxy.test.ts b/site/src/cut/server/proxy.test.tsindex f58a8374..8c9bcb81 100644--- a/site/src/cut/server/proxy.test.ts+++ b/site/src/cut/server/proxy.test.ts@@ -61,6 +61,9 @@ describe("proxy", () => {expect(args[args.indexOf("-profile:v") + 1]).toBe("main10");expect(args[args.indexOf("-c:a") + 1]).toBe("aac");expect(args[args.indexOf("-movflags") + 1]).toBe("+faststart");+ // A keyframe every second: the preview seeks into the proxy at every+ // cut, and a seek decodes from the keyframe before it.+ expect(args[args.indexOf("-force_key_frames") + 1]).toBe("expr:gte(t,n_forced*1)");const vt = proxyFfmpegArgs("in.mov", "out.mp4", {encoder: "hevc_videotoolbox",diff --git a/site/src/cut/server/proxy.ts b/site/src/cut/server/proxy.tsindex 275c2f2f..5b443cac 100644--- a/site/src/cut/server/proxy.ts+++ b/site/src/cut/server/proxy.ts@@ -5,9 +5,10 @@ import { assertGraphSafe } from "./filterGraph";import { videoDimensions } from "./util";/**- * The preview proxy of a ProRes master: a 10-bit HEVC file the browser's- * hardware decoder plays at speed, kept beside the master in the project's- * media. The master stays what an export reads.+ * The preview proxy of a ProRes master, or of a heavy video a cloud project+ * streams: a 10-bit HEVC file the browser's hardware decoder plays at speed,+ * kept beside the master in the project's media. The master stays what an+ * export reads.** The proxy carries the master's code values through the Rec.709 matrix: the* picture is taken out of the master's own matrix (BT.2020 non-constant on an@@ -93,6 +94,9 @@ export function proxyFfmpegArgs(src: string, out: string, opts: ProxyArgs): stri"-colorspace", "bt709","-color_range", "tv",...video,+ // A keyframe every second of the master's time: the preview seeks into+ // the proxy at every cut, and a seek decodes from the keyframe before it.+ "-force_key_frames", "expr:gte(t,n_forced*1)","-tag:v", "hvc1",...(source.hasAudio ? ["-c:a", "aac", "-b:a", "192k"] : []),"-movflags", "+faststart",diff --git a/site/src/cut/server/transitionExpr.ts b/site/src/cut/server/transitionExpr.tsindex 8d5c555d..dcd7e3bb 100644--- a/site/src/cut/server/transitionExpr.ts+++ b/site/src/cut/server/transitionExpr.ts@@ -13,6 +13,7 @@import { fexpr } from "./filterGraph";import {+ clampBand,CLOCK_SWEEP,clampFeather,FEATHER_STYLES,@@ -23,6 +24,7 @@ import {SLICE_SHUTTER,SLICE_STYLES,SLICE_TAPS,+ takesBand,takesFeather,} from "../lib/transitionShape";import { TRANSITION_XFADE, type TransitionStyle } from "../lib/types";@@ -122,6 +124,25 @@ function clockExpr(W: number, H: number): string {return `if(lte(atan2(Y/H*${n(H)},X/W*${n(W)}),${CLOCK_EASE}*${n(CLOCK_SWEEP)}),B,A)`;}+/**+ * A banded wipe: the incoming picture behind the edge, a solid white panel+ * `band` of the crossed distance just ahead of it, the outgoing picture+ * past that — the edge standing where `bandEdge` puts it. White is the+ * limited-range code at the chain's depth; an alpha plane keeps the+ * outgoing picture's.+ */+function bandExpr(style: TransitionStyle, band: number, depth: 8 | 10): string {+ const b = n(band);+ const e = `(${PROG}*(1+${b})-${b})`;+ const s = depth === 10 ? 1023 / 255 : 1;+ const white = `if(eq(PLANE,0),${n(235 * s)},if(eq(PLANE,3),A,${n(128 * s)}))`;+ // `d` is how far the pixel sits behind the edge's start, as a share of the+ // distance crossed: under `e` it is revealed, under `e + band` it is white.+ const d =+ style === "wipeleft" ? "((W-X-0.5)/W)" : style === "wiperight" ? "((X+0.5)/W)" : style === "wipeup" ? "((H-Y-0.5)/H)" : "((Y+0.5)/H)";+ return `if(lt(${d},${e}),B,if(lt(${d},${e}+${b}),${white},A))`;+}+/*** The option string for one xfade join of a W×H frame lasting `seconds`:* `transition=<name>` for a built-in, or `transition=custom:expr='…'`.@@ -132,10 +153,15 @@ export function xfadeTransition(feather: number | undefined,seconds: number,W: number,- H: number+ H: number,+ band?: number,+ depth: 8 | 10 = 8): string {const known = style as TransitionStyle;const kind = TRANSITION_XFADE[known] ?? "fade";+ if (takesBand(known) && clampBand(band) > 0) {+ return `transition=custom:expr=${fexpr(bandExpr(known, clampBand(band), depth))}`;+ }if (SLICE_STYLES.includes(known)) {return `transition=custom:expr=${fexpr(sliceExpr(known, seconds))}`;}diff --git a/site/src/cut/server/urlImport.ts b/site/src/cut/server/urlImport.tsindex 823a623b..ad71dd9e 100644--- a/site/src/cut/server/urlImport.ts+++ b/site/src/cut/server/urlImport.ts@@ -5,6 +5,7 @@ import { addDownloaded, type LibraryAsset, type LibrarySource } from "./library"import { assertLocalRuntime } from "./local-only";import { mediaDir, mediaPath as projectMediaPath, readProject } from "./projects";import { download, type Downloaded, type DownloadOptions } from "./urlDownload";+import { URL_BUSY_MESSAGE, URL_DOWNLOADS_MAX } from "../lib/urlBatch";import { uniqueName } from "./util";// Import a URL (TikTok, YouTube, Instagram, an X post, a page, …) into the@@ -12,8 +13,9 @@ import { uniqueName } from "./util";// resolved from the widened PATH — tool-path.ts — exactly like ffmpeg); this// module owns the local landing spots and the concurrency guard.-// A small ceiling so a burst of pastes can't spawn unbounded downloads.-const MAX_ACTIVE = 2;+// A small ceiling so a burst of pastes can't spawn unbounded downloads; the+// chat's URL batches run that many lanes and wait out a busy answer.+const MAX_ACTIVE = URL_DOWNLOADS_MAX;let active = 0;/** Run one guarded download and hand the files to `consume` before the temp@@ -29,7 +31,7 @@ async function withDownload<T>(assertLocalRuntime();if (!/^https?:\/\//i.test(url.trim())) throw new Error("Enter a valid http(s) URL.");if (active >= MAX_ACTIVE) {- throw new Error("Too many downloads in progress. Try again in a moment.");+ throw new Error(URL_BUSY_MESSAGE);}active++;try {diff --git a/site/src/cut/worker/proxyJob.test.ts b/site/src/cut/worker/proxyJob.test.tsindex cd3fb8d2..4b040866 100644--- a/site/src/cut/worker/proxyJob.test.ts+++ b/site/src/cut/worker/proxyJob.test.ts@@ -3,7 +3,7 @@ import { writeFile } from "node:fs/promises";import type { prisma } from "@/lib/prisma";import { runProxyJob, type ProxyJobIO } from "./proxyJob";-function harness(taken: string[] = []) {+function harness(taken: string[] = [], spec: Record<string, unknown> = { file: "clip.mov" }) {const findMany = mock(async (_args: unknown) => { void _args; return taken.map((fileName) => ({ fileName })); });const io = {db: { cutMediaObject: { findMany } } as unknown as typeof prisma,@@ -20,13 +20,22 @@ function harness(taken: string[] = []) {duration: mock(async () => 2),settings: mock(async () => ({ proxyMaxHeight: 1080, proxyCrf: 20 })),} satisfies ProxyJobIO;- const job = { id: "proxy-1", userId: "owner", projectId: "project", kind: "proxy", outName: null, spec: { file: "clip.mov" } };+ const job = { id: "proxy-1", userId: "owner", projectId: "project", kind: "proxy", outName: null, spec };const handle = { tmpDir: "", outPath: "", progress: 0, log: [] as string[] };const run = (canceled = () => false) => runProxyJob(job, handle, canceled, io);return { run, io, handle, findMany };}describe("proxy worker", () => {+ test("a job that asks for a smaller copy gets it, never one past the account's size", async () => {+ const small = harness([], { file: "clip.mov", maxHeight: 720 });+ await small.run();+ expect(small.io.make.mock.calls[0][3]).toMatchObject({ maxHeight: 720 });+ const big = harness([], { file: "clip.mov", maxHeight: 4320 });+ await big.run();+ expect(big.io.make.mock.calls[0][3]).toMatchObject({ maxHeight: 1080 });+ });+test("builds the proxy at the account's size and quality and registers it exempt beside the master", async () => {const h = harness();expect(await h.run()).toEqual({ fileName: "clip.proxy.mp4", sizeBytes: 5, width: 64, height: 64 });diff --git a/site/src/cut/worker/proxyJob.ts b/site/src/cut/worker/proxyJob.tsindex 900ed467..7362decd 100644--- a/site/src/cut/worker/proxyJob.ts+++ b/site/src/cut/worker/proxyJob.ts@@ -46,7 +46,7 @@ const defaultIO: ProxyJobIO = {};/**- * Run one proxy job: pull the ProRes master out of R2, build its preview+ * Run one proxy job: pull the master (ProRes, or a heavy video) out of R2, build its preview* proxy with the same code the engine runs on a Mac, and land it in the* project's media prefix as a quota-exempt object (kind "proxy") under a* name deduped against the project's files. The master is left alone; the@@ -62,7 +62,7 @@ export async function runProxyJob(if ((job.spec as { target?: string } | null)?.target === "library") {return runLibraryProxyJob(job, handle, isCanceled);}- const { file } = (job.spec ?? {}) as { file?: string };+ const { file, maxHeight } = (job.spec ?? {}) as { file?: string; maxHeight?: number };if (!file) throw new Error("Proxy job has no file.");const projectId = job.projectId;if (!projectId) throw new Error("Proxy job has no project.");@@ -84,7 +84,9 @@ export async function runProxyJob(const [total, settings] = await Promise.all([io.duration(src), io.settings()]);const outcome = await io.make(handle, src, out, {- maxHeight: settings.proxyMaxHeight,+ // A heavy video's proxy asks for a smaller copy; none is larger than+ // the account allows.+ maxHeight: maxHeight && maxHeight > 0 ? Math.min(settings.proxyMaxHeight, maxHeight) : settings.proxyMaxHeight,crf: settings.proxyCrf,onProgress: (seconds) => {if (total > 0) handle.progress = Math.min(0.99, seconds / total);