Open source CapCut alternative.
TypeScript
fix(cut): read times back to the millisecond and frame boxes to 1/10,000
David Le2d5454444fc1
diff --git a/site/src/cut/lib/aiContext.ts b/site/src/cut/lib/aiContext.tsindex 4767b63c..260d265e 100644--- a/site/src/cut/lib/aiContext.ts+++ b/site/src/cut/lib/aiContext.ts@@ -44,6 +44,9 @@ const r = (n: number) => Math.round(n * 100) / 100;/** Element times to the millisecond: a 60 fps frame is 0.0167 s, which two* decimals blur. editor_state and the tool results both read times through it. */export const roundT = (n: number) => Math.round(n * 1000) / 1000;+/** Frame fractions to the 1/10,000: a pixel of a 1080 frame is 0.0009, which+ * two decimals blur by up to five pixels. */+const rF = (n: number) => Math.round(n * 10000) / 10000;/** The transition this clip applies into the next one, clamped to the live* overlap the same way the preview and export are. Null when there's no@@ -54,7 +57,7 @@ function transitionToNext(sp: ClipSpan, index: number, spans: ClipSpan[]) {const style = sp.clip.transitionStyle ?? "crossfade";return {style,- seconds: r(sp.transitionOut),+ seconds: roundT(sp.transitionOut),...(sp.clip.transitionFeather ? { feather: sp.clip.transitionFeather } : {}),...(sp.clip.transitionBand ? { band: sp.clip.transitionBand } : {}),};@@ -92,9 +95,9 @@ function clipEffects(clip: VideoClip, live: boolean, frame?: CardFrame) {}: {};return {- ...(clip.animIn ? { animIn: { style: clip.animIn.style, seconds: r(clip.animIn.seconds) } } : {}),+ ...(clip.animIn ? { animIn: { style: clip.animIn.style, seconds: roundT(clip.animIn.seconds) } } : {}),...(clip.animOut- ? { animOut: { style: clip.animOut.style, seconds: r(clip.animOut.seconds) } }+ ? { animOut: { style: clip.animOut.style, seconds: roundT(clip.animOut.seconds) } }: {}),...color,...(clip.effects?.length ? { effects: clip.effects } : {}),@@ -208,13 +211,13 @@ function noteFields(if (!notes || notes.length === 0) return {};const size = notes.reduce((sum, n) => sum + n.text.length, 0);if (full || size <= NOTE_SNAPSHOT_BUDGET)- return { observed: notes.map((n) => ({ from: r(n.from), to: r(n.to), text: n.text })) };+ return { observed: notes.map((n) => ({ from: roundT(n.from), to: roundT(n.to), text: n.text })) };const share = Math.max(120, Math.floor(NOTE_SNAPSHOT_BUDGET / notes.length));let trimmed = false;const observed = notes.map((n) => {- if (n.text.length <= share) return { from: r(n.from), to: r(n.to), text: n.text };+ if (n.text.length <= share) return { from: roundT(n.from), to: roundT(n.to), text: n.text };trimmed = true;- return { from: r(n.from), to: r(n.to), text: `${n.text.slice(0, share).trimEnd()}…` };+ return { from: roundT(n.from), to: roundT(n.to), text: `${n.text.slice(0, share).trimEnd()}…` };});return { observed, ...(trimmed ? { observedText: "shortened here, whole in get_state" } : {}) };}@@ -253,8 +256,8 @@ export function buildAiContext(opts?: { fullCues?: boolean; chatId?: string | nukind,id,asset: sp.asset.name,- start: r(sp.start),- len: r(sp.len),+ start: roundT(sp.start),+ len: roundT(sp.len),muted: sp.clip.muted,...(sp.clip.hidden ? { hidden: true } : {}),...describeRate(sp.clip),@@ -272,7 +275,7 @@ export function buildAiContext(opts?: { fullCues?: boolean; chatId?: string | nuif (kind === "cue") {const c = s.subtitles.cues.find((x) => x.id === id);return c- ? { kind, id, text: c.text, start: r(c.start), end: r(c.end), track: c.lane ?? 0, ...cueExtras(c) }+ ? { kind, id, text: c.text, start: roundT(c.start), end: roundT(c.end), track: c.lane ?? 0, ...cueExtras(c) }: { kind, id };}// The selection's kind stays "overlay" (what select/delete take); the@@ -285,8 +288,8 @@ export function buildAiContext(opts?: { fullCues?: boolean; chatId?: string | nureturn {project: core.project,- playhead: r(playheadAt()),- skimmer: skimAt() === null ? null : r(skimAt()!),+ playhead: roundT(playheadAt()),+ skimmer: skimAt() === null ? null : roundT(skimAt()!),playing: s.playing,selection,// Every font a title or caption can be set in beyond the built-in list:@@ -469,7 +472,7 @@ function describeState(project: {id: s.projectId,name: s.projectName,- duration: r(duration),+ duration: roundT(duration),// The open timeline is what everything else here describes; the others// are parked until switch_timeline opens one.timeline: s.timeline,@@ -479,7 +482,7 @@ function describeState(id,label: TIMELINE_LABELS[id],...(id === s.timeline ? { open: true } : {}),- duration: r(body ? projectDuration(body) : 0),+ duration: roundT(body ? projectDuration(body) : 0),clips: body?.clips.length ?? 0,aspect: timelineAspect(s, id),};@@ -499,10 +502,10 @@ function describeState(if (!safeArea) return [];const keepOut = guideGeometry([p.id], s.aspect).boxes.map((b) => ({label: b.label,- x: r(b.x),- y: r(b.y),- w: r(b.w),- h: r(b.h),+ x: rF(b.x),+ y: rF(b.y),+ w: rF(b.w),+ h: rF(b.h),}));return [[p.id, { safeArea, ...(keepOut.length ? { keepOut } : {}) }]];})@@ -533,7 +536,7 @@ function describeState(...(a.sizeBytes !== undefined ? { sizeBytes: a.sizeBytes, fileSize: formatBytes(a.sizeBytes) } : {}),...(a.width !== undefined ? { width: a.width } : {}),...(a.height !== undefined ? { height: a.height } : {}),- duration: r(a.duration),+ duration: roundT(a.duration),...(a.origin ? { origin: a.origin } : {}),...(a.folderId != null ? { folderId: a.folderId } : {}),// The recording bound to this video (sync_audio): every clip of it@@ -564,7 +567,7 @@ function describeState(// footage; only sheets returned in this chat are. `watching` marks the// sweep still filling the rest; the spans grow as segments land....(a.watch && a.watch.ranges.length > 0- ? { mapped: a.watch.ranges.map((rg) => ({ from: r(rg.from), to: r(rg.to) })) }+ ? { mapped: a.watch.ranges.map((rg) => ({ from: roundT(rg.from), to: roundT(rg.to) })) }: {}),...(live && watchSweepActive(a.id) ? { watching: true } : {}),// The written record of this source — what someone looked at these@@ -583,8 +586,8 @@ function describeState(...(opts.fullCues && a.speech.segments.length > 0? {transcript: a.speech.segments.map((sg) => ({- start: r(sg.start),- end: r(sg.end),+ start: roundT(sg.start),+ end: roundT(sg.end),text: sg.text,})),}@@ -600,12 +603,12 @@ function describeState(...(sp.clip.groupId ? { groupId: sp.clip.groupId } : {}),asset: sp.asset.name,...(sp.clip.name ? { name: sp.clip.name } : {}),- start: r(sp.start),- len: r(sp.len),- in: r(sp.clip.in),- out: r(sp.clip.out),+ start: roundT(sp.start),+ len: roundT(sp.len),+ in: roundT(sp.clip.in),+ out: roundT(sp.clip.out),// A still has no source length; report its placed length instead of 0.- sourceDuration: r(sp.asset.type === "image" ? sp.len : sp.asset.duration),+ sourceDuration: roundT(sp.asset.type === "image" ? sp.len : sp.asset.duration),// A blocked-out shot holds a slot and draws a flat colour — there is no// picture here yet, and its label lives on the chip. Without this the// label reads as a clip's name and a shell of a cut looks finished.@@ -628,7 +631,7 @@ function describeState(// Track 0 is free-positioned: empty stretches play black....(() => {const prevEnd = index === 0 ? 0 : spans[index - 1].start + spans[index - 1].len;- return sp.start - prevEnd > 0.005 ? { gapBefore: r(sp.start - prevEnd) } : {};+ return sp.start - prevEnd > 0.005 ? { gapBefore: roundT(sp.start - prevEnd) } : {};})(),...(() => {const t = transitionToNext(sp, index, spans);@@ -648,7 +651,7 @@ function describeState(...(sp.clip.grade ? { colorGrade: sp.clip.grade } : {}),...(sp.clip.mask ? { mask: sp.clip.mask } : {}),...(sp.clip.kf?.length- ? { keyframes: sp.clip.kf.map((k) => ({ ...k, t: roundT(k.t), x: r(k.x), y: r(k.y) })) }+ ? { keyframes: sp.clip.kf.map((k) => ({ ...k, t: roundT(k.t), x: rF(k.x), y: rF(k.y) })) }: {}),...(sp.clip.motionBlur ? { motionBlur: r(sp.clip.motionBlur) } : {}),...clipEffects(sp.clip, live, { aspect: s.aspect, assetById }),@@ -684,8 +687,8 @@ function describeState(return {id: t.id,...(t.groupId ? { groupId: t.groupId } : {}),- start: r(t.start),- seconds: r(t.seconds),+ start: roundT(t.start),+ seconds: roundT(t.seconds),style: t.style,...(t.feather ? { feather: t.feather } : {}),...(t.band ? { band: t.band } : {}),@@ -708,8 +711,8 @@ function describeState(? {groupCameras: cams.map((c) => ({groupId: c.groupId,- start: r(c.start),- keys: c.keys.map((k) => ({ ...k, t: roundT(k.t), x: r(k.x), y: r(k.y), scale: r(k.scale), rotation: r(k.rotation) })),+ start: roundT(c.start),+ keys: c.keys.map((k) => ({ ...k, t: roundT(k.t), x: rF(k.x), y: rF(k.y), scale: r(k.scale), rotation: r(k.rotation) })),...(c.motionBlur ? { motionBlur: r(c.motionBlur) } : {}),})),}@@ -740,7 +743,7 @@ function describeState(...(s.subtitles.font ? { font: s.subtitles.font } : {}),...(s.subtitles.size !== undefined ? { size: s.subtitles.size } : {}),...(s.subtitles.x !== undefined || s.subtitles.y !== undefined- ? { x: r(s.subtitles.x ?? 0.5), y: r(s.subtitles.y ?? 0.5) }+ ? { x: rF(s.subtitles.x ?? 0.5), y: rF(s.subtitles.y ?? 0.5) }: {}),...(s.subtitles.wordHighlight ? { wordHighlight: true } : {}),...(s.subtitles.accentMode ? { accentMode: s.subtitles.accentMode } : {}),@@ -758,8 +761,8 @@ function describeState(cues: s.subtitles.cues.slice(0, cueCap).map((c) => ({id: c.id,...(c.groupId ? { groupId: c.groupId } : {}),- start: r(c.start),- end: r(c.end),+ start: roundT(c.start),+ end: roundT(c.end),text: c.text,...(subtitleTracks > 1 ? { track: c.lane ?? 0 } : {}),...cueExtras(c),@@ -776,7 +779,7 @@ function cueExtras(c: SubtitleCue) {const emphasis = emphasizedWords(c);return {...(emphasis.length > 0 ? { emphasis } : {}),- ...(c.x !== undefined && c.y !== undefined ? { x: r(c.x), y: r(c.y) } : {}),+ ...(c.x !== undefined && c.y !== undefined ? { x: rF(c.x), y: rF(c.y) } : {}),};}@@ -804,10 +807,10 @@ function describeAudio(...(a.groupId ? { groupId: a.groupId } : {}),asset: assets.get(a.assetId)?.name ?? a.assetId,...(a.name ? { name: a.name } : {}),- start: r(a.start),- len: r(rt.len),- in: r(a.in),- out: r(a.out),+ start: roundT(a.start),+ len: roundT(rt.len),+ in: roundT(a.in),+ out: roundT(a.out),volume: r(a.volume),fadeIn: r(a.fadeIn ?? 0),fadeOut: r(a.fadeOut ?? 0),@@ -832,10 +835,10 @@ function describeOverlayClip(c: VideoClip, assets: Map<string, { name: string }>asset: assets.get(c.assetId)?.name ?? c.assetId,...(c.name ? { name: c.name } : {}),track: c.track,- start: r(c.start),- len: r(retimeOf(c).len),- in: r(c.in),- out: r(c.out),+ start: roundT(c.start),+ len: roundT(retimeOf(c).len),+ in: roundT(c.in),+ out: roundT(c.out),muted: c.muted,...splitEditOf(c),...(c.hidden ? { hidden: true } : {}),@@ -843,7 +846,7 @@ function describeOverlayClip(c: VideoClip, assets: Map<string, { name: string }>// The frame region this layer occupies: Full covers the frame; Top/Bottom/// Left/Right split it; PiP floats inside it.layout: regionLabel(rect),- region: { x: r(rect.x), y: r(rect.y), w: r(rect.w), h: r(rect.h) },+ region: { x: rF(rect.x), y: rF(rect.y), w: rF(rect.w), h: rF(rect.h) },fit: c.fit ?? "fit",...(clipZoom(c) > 1? { zoom: r(clipZoom(c)), panX: r(c.panX ?? 0), panY: r(c.panY ?? 0) }@@ -859,7 +862,7 @@ function describeOverlayClip(c: VideoClip, assets: Map<string, { name: string }>...(c.mask ? { mask: c.mask } : {}),...(c.boxStyle ? { boxStyle: c.boxStyle } : {}),...(c.kf?.length- ? { keyframes: c.kf.map((k) => ({ ...k, t: roundT(k.t), x: r(k.x), y: r(k.y) })) }+ ? { keyframes: c.kf.map((k) => ({ ...k, t: roundT(k.t), x: rF(k.x), y: rF(k.y) })) }: {}),...(c.motionBlur ? { motionBlur: r(c.motionBlur) } : {}),};@@ -872,15 +875,15 @@ function describeOverlay(o: Overlay) {name: overlayName(o),start: roundT(o.start),end: roundT(o.end),- x: r(o.x),- y: r(o.y),+ x: rF(o.x),+ y: rF(o.y),...(o.rotation ? { rotation: o.rotation } : {}),...(o.opacity !== undefined ? { opacity: r(o.opacity) } : {}),...(o.blur ? { blur: r(o.blur) } : {}),...(o.motionBlur ? { motionBlur: r(o.motionBlur) } : {}),...(hasOverlayAnim(o.anim) ? { anim: o.anim } : {}),...(o.kf?.length- ? { keyframes: o.kf.map((k) => ({ ...k, t: roundT(k.t), x: r(k.x), y: r(k.y) })) }+ ? { keyframes: o.kf.map((k) => ({ ...k, t: roundT(k.t), x: rF(k.x), y: rF(k.y) })) }: {}),...(o.mask ? { mask: o.mask } : {}),...(o.lane ? { lane: o.lane } : {}),@@ -890,8 +893,8 @@ function describeOverlay(o: Overlay) {return {...base,shape: o.shape,- w: r(o.w),- h: r(o.h),+ w: rF(o.w),+ h: rF(o.h),fill: o.fill,...(o.fillOpacity !== undefined ? { fillOpacity: r(o.fillOpacity) } : {}),...(o.radius ? { radius: o.radius } : {}),@@ -901,7 +904,7 @@ function describeOverlay(o: Overlay) {};}if (o.kind === "sticker") {- return { ...base, assetId: o.assetId, w: r(o.w), ...(o.h ? { h: r(o.h) } : {}) };+ return { ...base, assetId: o.assetId, w: rF(o.w), ...(o.h ? { h: rF(o.h) } : {}) };}if (o.kind === "effect") {return {diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.tsindex 10c35a17..44f0135b 100644--- a/site/src/cut/lib/aiTools.ts+++ b/site/src/cut/lib/aiTools.ts@@ -377,14 +377,14 @@ function tracksAfter() {const cur = useEditor.getState();const row = track0Clips(cur.clips).map((c) => ({id: c.id,- start: round2(c.start),- len: round2(clipLen(c)),+ start: roundT(c.start),+ len: roundT(clipLen(c)),}));const lanes = cur.audioClips.map((a) => ({id: a.id,lane: a.lane ?? 0,- start: round2(a.start),- len: round2(a.out - a.in),+ start: roundT(a.start),+ len: roundT(a.out - a.in),}));const parked = parkedTransitions(cur.clips, cur.transitions);return {@@ -396,8 +396,8 @@ function tracksAfter() {? {parkedTransitions: parked.map((t) => ({id: t.id,- start: round2(t.start),- seconds: round2(t.seconds),+ start: roundT(t.start),+ seconds: roundT(t.seconds),style: t.style,})),parkedTransitionsNote:@@ -912,7 +912,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const next = groupCameraOf(useEditor.getState().overlays, groupId)!;return {groupId,- start: round2(next.start),+ start: roundT(next.start),keys: next.keys.map((k) => ({ ...k, t: roundT(k.t), x: round2(k.x), y: round2(k.y), scale: round2(k.scale) })),motionBlur: next.motionBlur ?? null,};@@ -1193,7 +1193,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const sizeBytes = asset.sizeBytes ?? await readMediaFileSize(asset.url);return {id: asset.id, name: asset.name, fileName: asset.fileName, kind: asset.type,- sizeBytes, fileSize: formatBytes(sizeBytes), duration: round2(asset.duration), width: asset.width, height: asset.height,+ sizeBytes, fileSize: formatBytes(sizeBytes), duration: roundT(asset.duration), width: asset.width, height: asset.height,};},@@ -1222,7 +1222,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {name: a.name,type: a.type,fileName: a.fileName,- duration: round2(a.duration),+ duration: roundT(a.duration),...(a.width !== undefined ? { width: a.width, height: a.height } : {}),...(a.folderId != null ? { folderId: a.folderId } : {}),})),@@ -1348,7 +1348,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {async (canvas, at) => {const i = pairs.length;pairs.push({- t: round2(at),+ t: roundT(at),sourceAt: round2(wanted[i].sourceAt),source: refs.get(i)!,cut: await rasterCanvasToDataUrl(canvas, "image/jpeg", 0.82),@@ -1448,7 +1448,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {// A source with more to see owes a reading of what just went past:// the next watch of it asks for this span's notes first.if (body.coveredTo > from && asset.duration - body.coveredTo > 0.5)- unwritten.set(asset.id, { from: round2(from), to: round2(body.coveredTo) });+ unwritten.set(asset.id, { from: roundT(from), to: roundT(body.coveredTo) });else unwritten.delete(asset.id);// A first look queues the quiet background sweep of the rest of the// source — metadata only, merged in as segments land.@@ -1483,7 +1483,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {}// Each cell's time plus why the selector kept it — "global" a hard cut,// "action" local motion, "settled" new settled detail (text, ink, UI).- const cellInfo = body.frames.map((f) => ({ t: round2(f.t), via: f.via }));+ const cellInfo = body.frames.map((f) => ({ t: roundT(f.t), via: f.via }));// Where a card could sit over what was just watched: the quiet parts of// the picture, inside whatever the project's guides keep clear. The// watch already measured the busy ones to choose these frames.@@ -1511,7 +1511,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {// The span this pass covered and how closely it looked at it. A// stretch seen only at "scan" has been surveyed, not read.detail,- from: round2(from),+ from: roundT(from),// Seconds no pass has read at a detail that carries type, and where// the next close pass would start.unreadSeconds: unread,@@ -1531,8 +1531,8 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {// the source nothing has been written about yet.recorded: notesIn((liveAsset ?? asset).watch, from, body.coveredTo),unnoted: unnotedSpans((liveAsset ?? asset).watch, asset.duration).map((g) => ({- from: round2(g.from),- to: round2(g.to),+ from: roundT(g.from),+ to: roundT(g.to),})),source: sourceRef(asset),...(reference@@ -1542,23 +1542,23 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {? {clip: {id: clip.id,- timelineStart: round2(clip.start),- in: round2(clip.in),- out: round2(clip.out),+ timelineStart: roundT(clip.start),+ in: roundT(clip.in),+ out: roundT(clip.out),speed: round2(speed),...(clip.reverse ? { reverse: true } : {}),...(clip.smoothSlow ? { smooth: true } : {}),...(speedCurveOf(clip)? {speedCurve: speedCurveOf(clip)!.map(([at, rate]) => [round2(at), round2(rate)]),- note: `this clip has a speed curve (average ${round2(speed)}×${clip.reverse ? ", played backward" : ""}): source_t maps to the timeline through the curve, with the clip occupying [${round2(clip.start)}, ${round2(clip.start + retimeOf(clip).len)}]; work in source seconds here`,+ note: `this clip has a speed curve (average ${round2(speed)}×${clip.reverse ? ", played backward" : ""}): source_t maps to the timeline through the curve, with the clip occupying [${roundT(clip.start)}, ${round2(clip.start + retimeOf(clip).len)}]; work in source seconds here`,}: clip.reverse? {- note: `this clip plays backward: timeline_t = ${round2(clip.start)} + (${round2(clip.out)} - source_t) / ${round2(speed)}, for source_t in [${round2(clip.in)}, ${round2(clip.out)}]`,+ note: `this clip plays backward: timeline_t = ${roundT(clip.start)} + (${roundT(clip.out)} - source_t) / ${round2(speed)}, for source_t in [${roundT(clip.in)}, ${roundT(clip.out)}]`,}: {- note: `timeline_t = ${round2(clip.start)} + (source_t - ${round2(clip.in)}) / ${round2(speed)}, for source_t in [${round2(clip.in)}, ${round2(clip.out)}]`,+ note: `timeline_t = ${roundT(clip.start)} + (source_t - ${roundT(clip.in)}) / ${round2(speed)}, for source_t in [${roundT(clip.in)}, ${roundT(clip.out)}]`,}),},}@@ -1608,11 +1608,11 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return {source: sourceRef(asset),notes: (cur?.watch?.notes ?? []).map((n) => ({- from: round2(n.from),- to: round2(n.to),+ from: roundT(n.from),+ to: roundT(n.to),text: n.text,})),- unnoted: unnoted.map((g) => ({ from: round2(g.from), to: round2(g.to) })),+ unnoted: unnoted.map((g) => ({ from: roundT(g.from), to: roundT(g.to) })),note:"Written onto the source: it saves with the project, rides the editor state from here on, and outlives this conversation's images." +(unnoted.length > 0@@ -1635,12 +1635,12 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {// Pre-map each silence's overlap with the clip's trimmed range onto the// timeline so the model cuts on ready numbers.const toTimeline = (t: number) =>- round2(clip!.start + retimeOf(clip!).tAt(clamp(t, clip!.in, clip!.out)));+ roundT(clip!.start + retimeOf(clip!).tAt(clamp(t, clip!.in, clip!.out)));return {silences: silences.map((x) => ({- start: round2(x.start),- end: round2(x.end),- duration: round2(x.duration),+ start: roundT(x.start),+ end: roundT(x.end),+ duration: roundT(x.duration),...(clip && x.end > clip.in && x.start < clip.out? { timeline: { start: toTimeline(x.start), end: toTimeline(x.end) } }: {}),@@ -1650,9 +1650,9 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {? {clip: {id: clip.id,- timelineStart: round2(clip.start),- in: round2(clip.in),- out: round2(clip.out),+ timelineStart: roundT(clip.start),+ in: roundT(clip.in),+ out: roundT(clip.out),speed: round2(speed),},}@@ -1722,7 +1722,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {throw new ToolError(`Filler detection is unavailable: ${e instanceof Error ? e.message : String(e)}`);}const floor = cutJudge().fillerCut;- const toSource = clip ? (t: number) => round2(retimeOf(clip).srcAt(clamp(t - clip.start, 0, Infinity))) : null;+ const toSource = clip ? (t: number) => roundT(retimeOf(clip).srcAt(clamp(t - clip.start, 0, Infinity))) : null;// Neighbouring filler words are one span: "you know" is a single cut,// and every span the caller gets is a cut it can make in one call.const kept = words.filter((w) => (answers[w.key]?.noul ?? 0) >= floor);@@ -1747,8 +1747,8 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const fillers = spans.map((sp) => ({cue_id: sp.cueId,text: sp.words.join(" "),- start: round2(sp.t0),- end: round2(sp.t1),+ start: roundT(sp.t0),+ end: roundT(sp.t1),...(sp.estimated ? { estimated: true } : {}),...(toSource ? { source: { start: toSource(sp.t0), end: toSource(sp.t1) } } : {}),}));@@ -1756,7 +1756,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {fillers,count: fillers.length,track: lane,- ...(clip ? { clip: { id: clip.id, timelineStart: round2(clip.start), in: round2(clip.in), out: round2(clip.out) } } : {}),+ ...(clip ? { clip: { id: clip.id, timelineStart: roundT(clip.start), in: roundT(clip.in), out: roundT(clip.out) } } : {}),...(fillers.some((f) => f.estimated)? { note: "Words marked estimated sit in cues with no word timings; their spans split the cue evenly, so confirm them with listen_audio before a tight cut." }: {}),@@ -1780,7 +1780,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {throw new ToolError(e instanceof Error ? e.message : "Could not read the audio.");});const beats = grid.beats.filter((b) => b >= from && (to === undefined || b <= to));- const toTimeline = (t: number) => round2(clip!.start + retimeOf(clip!).tAt(t));+ const toTimeline = (t: number) => roundT(clip!.start + retimeOf(clip!).tAt(t));return {bpm: grid.bpm,beats: beats.map(round2),@@ -1789,9 +1789,9 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {timelineBeats: grid.beats.filter((b) => b >= clip.in && b <= clip.out).map(toTimeline),clip: {id: clip.id,- timelineStart: round2(clip.start),- in: round2(clip.in),- out: round2(clip.out),+ timelineStart: roundT(clip.start),+ in: roundT(clip.in),+ out: roundT(clip.out),speed: round2(speed),},}@@ -1928,9 +1928,9 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return {name: asset.name,source: sourceRef(asset),- duration: round2(asset.duration),+ duration: roundT(asset.duration),...(clip ? { clipId: clip.id } : {}),- from: round2(heard.from),+ from: roundT(heard.from),...(soundSourceOf(asset, s.assets).bound? { soundFrom: soundSourceOf(asset, s.assets).asset.name }: {}),@@ -2085,7 +2085,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const at = clip.start + delta;return {id: clip.id,- start: round2(at),+ start: roundT(at),...(Math.abs(at - Math.max(0, input.start)) > 0.005? { note: "That spot was taken — slid right to the next free one." }: {}),@@ -2136,8 +2136,8 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return {id: c.id,track: c.track,- start: round2(c.start),- len: round2(clipLen(c)),+ start: roundT(c.start),+ len: roundT(clipLen(c)),layout: regionLabel(rectOf(c)),};},@@ -2199,7 +2199,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return {id: next.id,track: next.track,- start: round2(next.start),+ start: roundT(next.start),layout: regionLabel(rectOf(next)),fit: next.fit ?? "fit",...(clipZoom(next) > 1 ? { zoom: clipZoom(next) } : {}),@@ -2250,7 +2250,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const settings = cutClip();return {source: sourceRef(asset),- searched: { from: round2(from), to: round2(to ?? asset.duration) },+ searched: { from: roundT(from), to: roundT(to ?? asset.duration) },clips: clips.map((c, i) => ({n: i + 1,rank: c.rank,@@ -2416,8 +2416,8 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {clipId: clip.id,...(plan? {- in: { from: round2(clip.in), to: round2(plan.in) },- out: { from: round2(clip.out), to: round2(plan.out) },+ in: { from: roundT(clip.in), to: roundT(plan.in) },+ out: { from: roundT(clip.out), to: roundT(plan.out) },}: {}),...(notes@@ -3190,7 +3190,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {id: a.id,...patch,lane: landed.lane ?? 0,- ...("start" in patch ? { start: round2(landed.start) } : {}),+ ...("start" in patch ? { start: roundT(landed.start) } : {}),...audioPackHint(a.id),};},@@ -3496,9 +3496,9 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {kind: "clip",sourceId: asset.id,name: asset.name,- start: round2(next.start),- lenBefore: round2(before),- lenAfter: round2(clipLen(next)),+ start: roundT(next.start),+ lenBefore: roundT(before),+ lenAfter: roundT(clipLen(next)),...tracksAfter(),};}@@ -3515,9 +3515,9 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {kind: "audio",sourceId: asset.id,name: asset.name,- start: round2(next.start),- lenBefore: round2(before),- lenAfter: round2(next.out - next.in),+ start: roundT(next.start),+ lenBefore: roundT(before),+ lenAfter: roundT(next.out - next.in),};}const overlay = s.overlays.find((o) => o.id === id);@@ -4025,7 +4025,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {kind,category: v.category,aspect: v.aspect,- duration: round2(v.duration),+ duration: roundT(v.duration),...(v.persona ? { persona: v.persona } : {}),prompt: v.prompt,tags: v.tags,@@ -4049,7 +4049,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {id: x.id,kind: "sound",category: x.category,- duration: round2(x.duration),+ duration: roundT(x.duration),prompt: x.prompt,tags: x.tags,});@@ -4113,7 +4113,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {assetId: asset.id,name: asset.name,kind: vid ? "video" : sfx ? "sound" : "image",- duration: round2(vid || sfx ? asset.duration : IMAGE_CLIP_SECONDS),+ duration: roundT(vid || sfx ? asset.duration : IMAGE_CLIP_SECONDS),addedToTimeline: addToTimeline,clipId,fromRef: { scope: "stock", id },@@ -4135,7 +4135,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {sizeBytes: asset.sizeBytes,width: asset.width,height: asset.height,- duration: round2(asset.type === "image" ? IMAGE_CLIP_SECONDS : asset.duration),+ duration: roundT(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) {@@ -4307,7 +4307,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {kind: l.asset.type,...(l.reused ? { reused: true } : {}),})),- adjustments: adjustments.map((a) => ({ ...a, from: round2(a.from), to: round2(a.to) })),+ adjustments: adjustments.map((a) => ({ ...a, from: roundT(a.from), to: roundT(a.to) })),skipped,frame: frame && !!t.project,captionLook: captionLook && !!captions && Object.keys(captions).length > 0,@@ -4347,7 +4347,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {sourceId: l.sourceId,name: l.asset.name,kind: l.asset.type,- duration: round2(l.asset.type === "image" ? IMAGE_CLIP_SECONDS : l.asset.duration),+ duration: roundT(l.asset.type === "image" ? IMAGE_CLIP_SECONDS : l.asset.duration),...(l.reused ? { reused: true } : {}),})),note: "The copies preview on cards in this chat; add_clip places one when the user asks for it in the cut. A source this project already held a copy of comes back as that copy, marked reused.",@@ -4470,7 +4470,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {void enrichAsset(asset);tagChatAsset(asset.id, chatId);const place = input.add_to_timeline === true || isNum(input.start) || isNum(input.index);- return { assetId: asset.id, name: asset.name, kind: asset.type, duration: round2(asset.duration),+ return { assetId: asset.id, name: asset.name, kind: asset.type, duration: roundT(asset.duration),...(place ? { addedToTimeline: true, clip: placeAssetOnTimeline(asset, input) } : { addedToTimeline: false }) };}const found = findLibraryAsset((await fetchLibrary()).assets, String(input.id ?? ""));@@ -4501,7 +4501,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {assetId: asset.id,name: asset.name,kind: asset.type,- duration: round2(asset.type === "image" ? IMAGE_CLIP_SECONDS : asset.duration),+ duration: roundT(asset.type === "image" ? IMAGE_CLIP_SECONDS : asset.duration),...(place? { addedToTimeline: true, clip: placeAssetOnTimeline(asset, input) }: { addedToTimeline: false }),@@ -4531,7 +4531,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {}return {added: t.name,- duration: round2(t.duration),+ duration: roundT(t.duration),parts: t.layers.length + t.audio.length + t.texts.length + t.cues.length,};},@@ -4556,7 +4556,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const built = useEditor.getState().selectionTemplate();if (!built) throw new ToolError("Could not build a template from those items.");const saved = useEditor.getState().addTemplate({ ...built, name });- return { id: saved.id, name: saved.name, duration: round2(saved.duration) };+ return { id: saved.id, name: saved.name, duration: roundT(saved.duration) };},library_organize: async (_s, input) => {@@ -4771,7 +4771,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {// False means the packets were copied — same picture, new container.reencoded: out.reencoded,...(out.unchanged ? { unchanged: true } : {}),- duration: round2(out.duration),+ duration: roundT(out.duration),...(out.width !== undefined ? { width: out.width, height: out.height } : {}),// The file stayed where it already lived: a chat attachment on its// card in this thread, an import in the Media panel.@@ -4848,7 +4848,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {count: made.length,cues: made.slice(0, 60)- .map((q) => ({ id: q.id, start: round2(q.start), end: round2(q.end), text: q.text })),+ .map((q) => ({ id: q.id, start: roundT(q.start), end: roundT(q.end), text: q.text })),...(made.length > 60 ? { cuesTruncated: true } : {}),note:cur.subtitleStatus === "empty"@@ -5031,7 +5031,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return {assetId: asset.id,name: asset.name,- duration: round2(asset.duration),+ duration: roundT(asset.duration),addedToTimeline: false,note: "The music finished and was saved to the project it was generated for; the user has since switched projects, so it isn't on this one.",};@@ -5043,7 +5043,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {return {assetId: asset.id,name: asset.name,- duration: round2(asset.duration),+ duration: roundT(asset.duration),addedToTimeline: false,note: "The music previews in this chat — the user can play it and drag it onto the soundtrack; pass add_to_timeline or start when they ask for it in the cut.",};@@ -5066,8 +5066,8 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {assetId: asset.id,name: asset.name,audioClipId: clipId,- start: round2(placedStart),- duration: round2(asset.duration),+ start: roundT(placedStart),+ duration: roundT(asset.duration),volume,addedToTimeline: true,};@@ -5094,8 +5094,8 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {name: out.asset.name,audioClipId: sel?.kind === "audio" ? sel.id : null,voice,- start: round2(out.start),- duration: round2(out.asset.duration),+ start: roundT(out.start),+ duration: roundT(out.asset.duration),duck: out.duck,lines: out.lines,};@@ -5198,7 +5198,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {const s = useEditor.getState();return {timeline: s.timeline,- duration: round2(projectDuration(s)),+ duration: roundT(projectDuration(s)),clips: s.clips.length,audioClips: s.audioClips.length,overlays: s.overlays.length,@@ -5271,8 +5271,8 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {speed: next.speed ?? 1,...(next.reverse ? { reverse: true } : {}),...(next.smoothSlow ? { smooth: true } : {}),- lenBefore: round2(before),- lenAfter: round2(clipLen(next)),+ lenBefore: roundT(before),+ lenAfter: roundT(clipLen(next)),...tracksAfter(),};},@@ -5318,9 +5318,9 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {id: next.id,speed: round2(retimeOf(next).rate),...(next.smoothSlow ? { smooth: true } : {}),- nodes: curve ? curve.map(([at, speed]) => ({ at: round2(at), speed: round2(speed) })) : [],- lenBefore: round2(before),- lenAfter: round2(clipLen(next)),+ nodes: curve ? curve.map(([at, speed]) => ({ at: roundT(at), speed: round2(speed) })) : [],+ lenBefore: roundT(before),+ lenAfter: roundT(clipLen(next)),...tracksAfter(),};},@@ -5849,7 +5849,7 @@ async function synthesizeVoiceover(assetId: asset.id,name: asset.name,voice,- duration: round2(asset.duration),+ duration: roundT(asset.duration),addedToTimeline: false,note: "The voiceover previews in this chat — the user can play it and drag it onto the soundtrack; pass add_to_timeline or start when they ask for it in the cut.",};@@ -5864,8 +5864,8 @@ async function synthesizeVoiceover(name: asset.name,audioClipId: sel?.kind === "audio" ? sel.id : null,voice,- start: round2(at),- duration: round2(asset.duration),+ start: roundT(at),+ duration: roundT(asset.duration),duck: duck < 1 ? duck : null,addedToTimeline: true,};@@ -5960,8 +5960,8 @@ function textSequenceLines(const lane = isNum(input.from_captions) ? Math.max(0, Math.round(input.from_captions)) : 0;return laneCues(s.subtitles, lane).map((c) => ({text: c.text,- start: round2(c.start),- end: round2(c.end),+ start: roundT(c.start),+ end: roundT(c.end),}));}const raw = Array.isArray(input.lines) ? input.lines : [];@@ -5999,7 +5999,7 @@ function textSequenceLines(// decided — a following line that starts earlier cannot invert it.const end = Math.max(Number.isNaN(l.end) ? fallbackEnd : l.end, start + 0.2);cursor = end;- return { ...l, start: round2(start), end: round2(end) } as ComposeLine;+ return { ...l, start: roundT(start), end: roundT(end) } as ComposeLine;});}@@ -6029,7 +6029,7 @@ function notesIn(): { from: number; to: number; text: string }[] {return (watch?.notes ?? []).filter((n) => n.to > from && n.from < to)- .map((n) => ({ from: round2(n.from), to: round2(n.to), text: n.text }));+ .map((n) => ({ from: roundT(n.from), to: roundT(n.to), text: n.text }));}/** A clip volume as decibels of gain; silence reads as the quiet floor. */@@ -6059,7 +6059,7 @@ async function measureMix(s: ReturnType<typeof useEditor.getState>) {});if (!whole) return { silent: true, note: "Nothing in the cut makes a sound." };const channels = Array.from({ length: whole.numberOfChannels }, (_, c) => whole.getChannelData(c));- return { ...roundedLoudness(await measureLoudnessSliced(channels, whole.sampleRate)), seconds: round2(whole.duration) };+ return { ...roundedLoudness(await measureLoudnessSliced(channels, whole.sampleRate)), seconds: roundT(whole.duration) };}function resolveWatchTarget(@@ -6137,7 +6137,7 @@ function sourceRef(asset: MediaAsset) {return {assetId: asset.id,name: asset.name,- duration: round2(asset.duration),+ duration: roundT(asset.duration),type: asset.type,sound: soundOf(asset),};@@ -6390,7 +6390,7 @@ async function listenSpan(for (let attempt = 0; attempt < 3 && span > 0.5; attempt++) {const blob = await audioSpanBlob(projectId, heard.asset, from, round2(from + span));const inline = await blobToInlineAudio(blob);- if (inline) return { inline, from: round2(heard.back(from)), to: round2(heard.back(from + span)) };+ if (inline) return { inline, from: roundT(heard.back(from)), to: roundT(heard.back(from + span)) };const perSecond = blob.size / span;// Leave room for the base64 inflation the cap already accounts for.const fits = (MAX_AUDIO_BYTES * 0.9) / perSecond;@@ -6405,7 +6405,7 @@ async function listenSpan(* measured first and the measurement goes in the description. */function sweepCandidates(s: Editor, kind: ItemKind): SweepCandidate[] {const assetName = (id: string) => s.assets.find((a) => a.id === id)?.name ?? "media";- const span = (start: number, len: number) => `starts ${round2(start)}s, ${round2(len)}s long`;+ const span = (start: number, len: number) => `starts ${roundT(start)}s, ${roundT(len)}s long`;if (kind === "clip")return s.clips.map((c) => ({id: c.id,@@ -6441,7 +6441,7 @@ function sweepCandidates(s: Editor, kind: ItemKind): SweepCandidate[] {id: o.id,label: [isTextOverlay(o) ? `text "${o.text}"` : `${o.kind ?? "element"} "${o.name ?? o.kind ?? "element"}"`,- `${round2(o.start)}s to ${round2(o.end)}s`,+ `${roundT(o.start)}s to ${roundT(o.end)}s`,o.hidden ? "hidden" : "",].filter(Boolean)@@ -6450,11 +6450,11 @@ function sweepCandidates(s: Editor, kind: ItemKind): SweepCandidate[] {if (kind === "cue")return s.subtitles.cues.map((c) => ({id: c.id,- label: `caption "${c.text}", ${round2(c.start)}s to ${round2(c.end)}s`,+ label: `caption "${c.text}", ${roundT(c.start)}s to ${roundT(c.end)}s`,}));return s.transitions.map((t) => ({id: t.id,- label: `${t.style} transition, ${round2(t.seconds)}s at ${round2(t.start)}s${t.hidden ? ", switched off" : ""}`,+ label: `${t.style} transition, ${roundT(t.seconds)}s at ${roundT(t.start)}s${t.hidden ? ", switched off" : ""}`,}));}@@ -6834,8 +6834,8 @@ function placeBlocks(s: Editor, raw: unknown[], input: Record<string, unknown>)made.push({clipId: placed.id,assetId: asset.id,- start: round2(placed.start),- seconds: round2(shot.seconds),+ start: roundT(placed.start),+ seconds: roundT(shot.seconds),label: asset.block?.label ?? "",});}@@ -6887,7 +6887,7 @@ function placeAssetSpans(}if (!clipId) throw new ToolError("Could not create the clip.");useEditor.getState().setClipTrim(clipId, cut.from, cut.to);- laid.push({ id: clipId, in: round2(cut.from), out: round2(cut.to) });+ laid.push({ id: clipId, in: roundT(cut.from), out: roundT(cut.to) });}} finally {useEditor.getState().endHistoryBatch();@@ -6900,11 +6900,11 @@ function placeAssetSpans(id: c.id,in: c.in,out: c.out,- start: round2(live?.start ?? 0),- len: round2(c.out - c.in),+ start: roundT(live?.start ?? 0),+ len: roundT(c.out - c.in),};}),- seconds: round2(laid.reduce((n, c) => n + c.out - c.in, 0)),+ seconds: roundT(laid.reduce((n, c) => n + c.out - c.in, 0)),};}@@ -6927,7 +6927,7 @@ function placeAssetOnTimeline(asset: MediaAsset, input: Record<string, unknown>)? useEditor.getState().audioClips.find((x) => x.id === sel.id): undefined;if (!c) throw new ToolError("Could not create the soundtrack clip.");- return { id: c.id, kind: asset.type, lane: c.lane ?? 0, start: round2(c.start), len: round2(c.out - c.in), ...audioPackHint(c.id) };+ return { id: c.id, kind: asset.type, lane: c.lane ?? 0, start: roundT(c.start), len: roundT(c.out - c.in), ...audioPackHint(c.id) };}let clipId: string | null;if (isNum(input.index)) {@@ -6943,8 +6943,8 @@ function placeAssetOnTimeline(asset: MediaAsset, input: Record<string, unknown>)id: c.id,kind: asset.type,index: track0Clips(useEditor.getState().clips).findIndex((x) => x.id === c.id),- start: round2(c.start),- len: round2(c.out - c.in),+ start: roundT(c.start),+ len: roundT(c.out - c.in),};}diff --git a/site/src/cut/lib/audioLaneTools.test.ts b/site/src/cut/lib/audioLaneTools.test.tsindex 583bec1c..ef9e439e 100644--- a/site/src/cut/lib/audioLaneTools.test.ts+++ b/site/src/cut/lib/audioLaneTools.test.ts@@ -60,3 +60,14 @@ test("a sound put on a higher lane than it needs is told which lane is free", asconst packed = (await runAiTool("update_audio", { id, lane: 0 })) as { freeLane?: number };expect(packed.freeLane).toBeUndefined();});++// A sound timed to a frame (a click under a key press, music under a screen+// recording's clock) is set to the millisecond, and the chat confirms the+// placement from what the tool reports back.+test("a placed and moved sound reports its times to the millisecond", async () => {+ const placed = (await runAiTool("add_clip", { asset_id: "music", start: 0.617 })) as { start: number };+ expect(placed.start).toBe(0.617);+ const id = useEditor.getState().audioClips[0].id;+ const moved = (await runAiTool("update_audio", { id, start: 0.017, in: 0.5723 })) as { start: number; in: number };+ expect([moved.start, moved.in]).toEqual([0.017, 0.5723]);+});diff --git a/site/src/cut/lib/overlayRegionTool.test.ts b/site/src/cut/lib/overlayRegionTool.test.tsindex 07d404f5..5ebf5c10 100644--- a/site/src/cut/lib/overlayRegionTool.test.ts+++ b/site/src/cut/lib/overlayRegionTool.test.ts@@ -1,6 +1,7 @@import { beforeEach, expect, test } from "bun:test";import { TIMELINE_TOOLS } from "@/cut/components/Timeline.tools";import { runAiTool } from "./aiTools";+import { buildAiContext } from "./aiContext";import { useEditor } from "./store";import { REGION_MAX_SCALE, emptySubtitles } from "./types";@@ -49,3 +50,11 @@ test("the region schema says it may pass the frame edges", () => {expect(region.properties.x.description).toContain("past");expect(region.properties.w.description).toContain(String(REGION_MAX_SCALE));});++// The box is measured to a pixel of a 1080 frame; editor_state reads it back+// at that precision so the chat can confirm what it set.+test("editor_state reads a layer's region back at the precision it was set", async () => {+ await runAiTool("update_overlay_video", { id: overlay().id, region: { x: -0.0126, y: 0.2877, w: 1.0131, h: 0.3771 } });+ const layer = JSON.stringify(buildAiContext({ chatId: null }));+ expect(layer).toContain('"region":{"x":-0.0126,"y":0.2877,"w":1.0131,"h":0.3771}');+});