Skip to content

Open source CapCut alternative.

TypeScript

feat(site): reference timeline audio clips by @sN and pull them into chat [rebuild] (#10)

David Le1bfa6ac46e80
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index 7ad0da47..f87a1d16 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -22,7 +22,7 @@ import {
hasLibraryDrag,
hasTemplateDrag,
} from "@/cut/lib/assetDrag";
-import { draggingRef, hasRefDrag, type AssetRef } from "@/cut/lib/assetRef";
+import { audioClipRefs, draggingRef, hasRefDrag, type AssetRef } from "@/cut/lib/assetRef";
import {
addProjectTemplateToTimeline,
addTemplateToProject,
@@ -325,6 +325,16 @@ export function Timeline() {
const rowOf = new Map(used.map((l, i) => [l, i]));
return { used, rowOf, count: used.length };
}, [audioClips]);
+ // Chat mention token per audio clip ("@s1"), keyed by clip id — the same
+ // handles the chat resolves against, so the token shown on hover is exactly
+ // what pulls this sound into a message.
+ const audioMentions = useMemo(() => {
+ const map = new Map<string, string>();
+ for (const ref of audioClipRefs(audioClips, assets)) {
+ if (ref.handle) map.set(ref.id, `@${ref.handle}`);
+ }
+ return map;
+ }, [audioClips, assets]);
// The home track of an in-flight upper-layer drag, so that row can render
// the landing slot while the clip stays on its own track.
@@ -1132,6 +1142,7 @@ export function Timeline() {
key={a.id}
clip={a}
asset={assets.find((x) => x.id === a.assetId)}
+ mention={audioMentions.get(a.id)}
pps={pps}
top={homeRow * AUDIO_H}
homeRow={homeRow}
@@ -1783,6 +1794,7 @@ function MuteChip({
function AudioView({
clip,
asset,
+ mention,
pps,
top,
homeRow,
@@ -1795,6 +1807,9 @@ function AudioView({
}: {
clip: AudioClip;
asset: MediaAsset | undefined;
+ /** The clip's chat mention token ("@s1"), shown on hover so the user can
+ * point the assistant at this exact sound. Absent when its asset is gone. */
+ mention: string | undefined;
pps: number;
/** Home-row top in px; while carried the ghost adds the pointer's offset. */
top: number;
@@ -1873,9 +1888,20 @@ function AudioView({
style={{ width: Math.min(w, (clip.fadeOut ?? 0) * pps) }}
/>
)}
- <span className="pointer-events-none absolute top-[3px] left-2 text-[9.5px] whitespace-nowrap text-white/90 [text-shadow:0_1px_2px_rgba(0,0,0,0.35)]">
+ <span
+ className={cn(
+ "pointer-events-none absolute top-[3px] left-2 text-[9.5px] whitespace-nowrap text-white/90 transition-opacity [text-shadow:0_1px_2px_rgba(0,0,0,0.35)]",
+ // On hover the mention chip takes the corner, so step the name aside.
+ mention && !drag && "group-hover:opacity-0"
+ )}
+ >
{asset.name}
</span>
+ {mention && !drag && (
+ <span className="tl-mention-chip pointer-events-none absolute top-1 left-1 z-2 rounded-[5px] bg-black/65 px-1.5 py-px font-mono text-[10px] text-white opacity-0 transition-opacity group-hover:opacity-100">
+ {mention}
+ </span>
+ )}
<ClipMenu asset={asset} />
<MuteChip
muted={!!clip.hidden}
diff --git a/site/src/cut/lib/assetRef.ts b/site/src/cut/lib/assetRef.ts
index c4adcfb5..71af1c8c 100644
--- a/site/src/cut/lib/assetRef.ts
+++ b/site/src/cut/lib/assetRef.ts
@@ -6,8 +6,8 @@ import { fetchLibrary, libraryMediaUrl, type LibraryAsset } from "./library";
import { stockTitle, type StockImage, type StockMusic, type StockVideo } from "./stock";
import { STOCK_IMAGES } from "./stockManifest";
import { STOCK_VIDEOS } from "./stockVideoManifest";
-import { getClipSpans, useEditor } from "./store";
-import type { MediaAsset } from "./types";
+import { clipLen, getClipSpans, useEditor } from "./store";
+import type { AudioClip, MediaAsset } from "./types";
// One reference model for every piece of media in the app. A ref names an
// asset wherever it lives — the open project, the shared library, the bundled
@@ -27,9 +27,9 @@ import type { MediaAsset } from "./types";
/** "file" marks a transient ref made from a desktop drop (a text file riding a
* composer) — it lives only in the composer's state, never in a catalog.
- * "clip" names a clip on the timeline's video track (id = the clip id, url =
- * its source media), so chat can talk about segments of the cut, not just
- * assets. */
+ * "clip" names a clip on the timeline — a video-track clip or a soundtrack
+ * clip (id = the clip id, url = its source media), so chat can talk about
+ * segments of the cut, not just assets. */
export type AssetRefScope = "project" | "library" | "stock" | "file" | "clip";
export type AssetRefKind = "video" | "audio" | "image" | "text";
@@ -359,15 +359,43 @@ export function clipRefs(
}));
}
+/** The soundtrack's audio clips as refs — `s1`, `s2`… in start-time order — so
+ * a prompt can point at a sound on the timeline ("@s1 is too loud"). Each ref
+ * carries its source audio's URL and `kind: "audio"`, so using the token in
+ * chat pulls the actual sound in. Handles are derived from the current order,
+ * never stored; the timeline shows each clip's token on hover. A clip whose
+ * asset has gone missing is dropped. */
+export function audioClipRefs(audioClips: AudioClip[], assets: MediaAsset[]): AssetRef[] {
+ return [...audioClips]
+ .sort((a, b) => a.start - b.start || (a.lane ?? 0) - (b.lane ?? 0))
+ .flatMap((clip, i) => {
+ const asset = assets.find((x) => x.id === clip.assetId);
+ if (!asset) return [];
+ return [
+ {
+ scope: "clip",
+ id: clip.id,
+ name: `sound ${i + 1}`,
+ kind: "audio",
+ url: asset.url,
+ duration: clipLen(clip),
+ handle: `s${i + 1}`,
+ } satisfies AssetRef,
+ ];
+ });
+}
+
/** Everything referenceable right now, in resolution order: the timeline's
- * clips first (what's in front of the user — a bare `@` leads with the cut),
- * then the open project's media, the shared library, and the stock catalog.
+ * clips first — video-track clips, then soundtrack clips (what's in front of
+ * the user — a bare `@` leads with the cut), then the open project's media,
+ * the shared library, and the stock catalog.
* Names are unique within the list (first scope wins) so a mention resolves
* to one asset. Library items mention by name; stock ids are already short
* (`@nature-dunes`). */
export function useRefCandidates(): AssetRef[] {
const assets = useEditor((s) => s.assets);
const clips = useEditor((s) => s.clips);
+ const audioClips = useEditor((s) => s.audioClips);
const [lib, setLib] = useState<LibraryAsset[]>(libraryCache ?? []);
useEffect(() => {
@@ -387,6 +415,7 @@ export function useRefCandidates(): AssetRef[] {
const out: AssetRef[] = [];
for (const ref of [
...clipRefs(clips, assets),
+ ...audioClipRefs(audioClips, assets),
...project,
...lib.map(refFromLibrary),
...STOCK_IMAGES.map(refFromStock),
@@ -398,7 +427,7 @@ export function useRefCandidates(): AssetRef[] {
out.push(ref);
}
return out;
- }, [assets, clips, lib]);
+ }, [assets, clips, audioClips, lib]);
}
/** The prompt token for an asset name: `@name`, quoted when it has spaces. */
diff --git a/site/src/cut/server/ai/catalog.ts b/site/src/cut/server/ai/catalog.ts
index 5a1e5b31..9ae6a8f1 100644
--- a/site/src/cut/server/ai/catalog.ts
+++ b/site/src/cut/server/ai/catalog.ts
@@ -810,5 +810,5 @@ export function attachedAssetsBlock(refs: unknown[]): string {
const clipHint = refs.some((r) => (r as { scope?: string }).scope === "clip")
? '\nA "clip" attachment means the user is pointing at that segment of the cut: diagnose with read-only looks (watch_video, listen_audio — never subtitles_generate, which writes captions), then revise the clip in place — regenerate_shot for its sceneShot, edit tools otherwise — without deleting it.'
: "";
- return `\n\n<attached_assets>\nThe user attached these assets to this message; their text may cite one by @handle or @name. Assets with scope "project" are in the open project (ids usable with the editor tools); "clip" assets are clips on the timeline's video track — the id matches videoTrack in editor_state (a scene-run clip carries its sceneShot number there); "library" and "stock" assets live outside the project until imported; "file" assets came straight from the user's computer and exist only on this message:\n${JSON.stringify(refs)}${clipHint}\n</attached_assets>`;
+ return `\n\n<attached_assets>\nThe user attached these assets to this message; their text may cite one by @handle or @name. Assets with scope "project" are in the open project (ids usable with the editor tools); "clip" assets are clips on the timeline — a video clip's id matches videoTrack in editor_state (a scene-run clip carries its sceneShot number there), an audio clip's (kind "audio") id matches soundtrack; "library" and "stock" assets live outside the project until imported; "file" assets came straight from the user's computer and exist only on this message:\n${JSON.stringify(refs)}${clipHint}\n</attached_assets>`;
}