Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): share the watch-tool preamble and markdown component map

David Le9bd239f48fa7
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsx
index 5ff6b018..8a95b649 100644
--- a/site/src/cut/components/AiPanel.tsx
+++ b/site/src/cut/components/AiPanel.tsx
@@ -24,6 +24,7 @@ import {
X,
} from "lucide-react";
import Markdown from "react-markdown";
+import { baseMarkdownComponents } from "./markdownComponents";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
@@ -780,14 +781,10 @@ const MessageView = memo(function MessageView({ message }: { message: UIMessage
<div key={i} className="ai-md max-w-full text-[12.5px] leading-relaxed">
<Markdown
components={{
- p: (p) => <p className="mb-1.5 last:mb-0" {...p} />,
- ul: (p) => <ul className="mb-1.5 list-disc pl-4 last:mb-0" {...p} />,
- ol: (p) => <ol className="mb-1.5 list-decimal pl-4 last:mb-0" {...p} />,
- li: (p) => <li className="mb-0.5" {...p} />,
+ ...baseMarkdownComponents,
code: (p) => (
<code className="rounded bg-muted px-1 py-px font-mono text-[11px]" {...p} />
),
- a: (p) => <a className="text-[#0a84ff] underline" target="_blank" {...p} />,
}}
>
{part.text}
diff --git a/site/src/cut/components/DocText.tsx b/site/src/cut/components/DocText.tsx
index 7c1d8328..7e1c065e 100644
--- a/site/src/cut/components/DocText.tsx
+++ b/site/src/cut/components/DocText.tsx
@@ -3,6 +3,7 @@
import { useEffect, useState } from "react";
import Markdown from "react-markdown";
import { cn } from "@/lib/utils";
+import { baseMarkdownComponents } from "./markdownComponents";
// Rendering for text assets (a dropped script, notes, a CSV, an assistant
// deliverable): fetch the file behind the ref URL and render it by extension —
@@ -159,18 +160,14 @@ export function DocText({
<div className={cn("doc-md", className)}>
<Markdown
components={{
+ ...baseMarkdownComponents,
h1: (p) => <h1 className="mt-3 mb-1.5 text-[1.25em] font-semibold first:mt-0" {...p} />,
h2: (p) => <h2 className="mt-3 mb-1.5 text-[1.1em] font-semibold first:mt-0" {...p} />,
h3: (p) => <h3 className="mt-2 mb-1 font-semibold first:mt-0" {...p} />,
- p: (p) => <p className="mb-1.5 last:mb-0" {...p} />,
- ul: (p) => <ul className="mb-1.5 list-disc pl-4 last:mb-0" {...p} />,
- ol: (p) => <ol className="mb-1.5 list-decimal pl-4 last:mb-0" {...p} />,
- li: (p) => <li className="mb-0.5" {...p} />,
code: (p) => <code className="rounded bg-muted px-1 py-px font-mono text-[0.9em]" {...p} />,
pre: (p) => (
<pre className="mb-1.5 overflow-x-auto rounded-md bg-muted/70 p-2 font-mono text-[0.9em] last:mb-0" {...p} />
),
- a: (p) => <a className="text-[#0a84ff] underline" target="_blank" {...p} />,
blockquote: (p) => (
<blockquote className="mb-1.5 border-l-2 border-border pl-2 text-muted-foreground" {...p} />
),
diff --git a/site/src/cut/components/markdownComponents.tsx b/site/src/cut/components/markdownComponents.tsx
new file mode 100644
index 00000000..d4c54626
--- /dev/null
+++ b/site/src/cut/components/markdownComponents.tsx
@@ -0,0 +1,13 @@
+import type { Components } from "react-markdown";
+
+// Markdown element styling shared by the two surfaces that render it: the chat
+// messages (AiPanel) and the document view (DocText). Both want the same list,
+// paragraph, and link treatment; each spreads this base and overrides `code`
+// for its own type scale (DocText also adds headings, pre, and blockquote).
+export const baseMarkdownComponents: Components = {
+ p: (p) => <p className="mb-1.5 last:mb-0" {...p} />,
+ ul: (p) => <ul className="mb-1.5 list-disc pl-4 last:mb-0" {...p} />,
+ ol: (p) => <ol className="mb-1.5 list-decimal pl-4 last:mb-0" {...p} />,
+ li: (p) => <li className="mb-0.5" {...p} />,
+ a: (p) => <a className="text-[#0a84ff] underline" target="_blank" {...p} />,
+};
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index d94e3f1b..b158824d 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -69,9 +69,7 @@ export async function runAiTool(
}
case "watch_video": {
- const projectId = s.projectId;
- if (!projectId) throw new ToolError("No project open.");
- const { asset, clip } = resolveWatchTarget(s, input);
+ const { projectId, asset, clip, speed, from, to } = resolveWatchRange(s, input);
if (asset.type === "audio")
throw new ToolError(`"${asset.name}" is audio — detect_silence and subtitles_generate are the listening tools.`);
interface WatchBody {
@@ -97,19 +95,6 @@ export async function runAiTool(
note: "A still image — one frame, no time axis.",
};
}
- const speed = clip?.speed && clip.speed > 0 ? clip.speed : 1;
- const dur = asset.duration > 0 ? asset.duration : Infinity;
- const from = clamp(isNum(input.from) ? input.from : clip ? clip.in : 0, 0, dur);
- // An unknown duration leaves `to` for the engine to probe.
- const to = isNum(input.to)
- ? clamp(input.to, 0, dur)
- : clip
- ? clip.out
- : Number.isFinite(dur)
- ? dur
- : undefined;
- if (to !== undefined && to <= from)
- throw new ToolError("from/to describe an empty range of the source.");
const res = await apiFetch(`/api/cut/projects/${projectId}/watch`, {
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -163,23 +148,8 @@ export async function runAiTool(
}
case "detect_silence": {
- const projectId = s.projectId;
- if (!projectId) throw new ToolError("No project open.");
- const { asset, clip } = resolveWatchTarget(s, input);
+ const { projectId, asset, clip, speed, from, to } = resolveWatchRange(s, input);
if (asset.type === "image") throw new ToolError(`"${asset.name}" is an image — it has no audio.`);
- const speed = clip?.speed && clip.speed > 0 ? clip.speed : 1;
- const dur = asset.duration > 0 ? asset.duration : Infinity;
- const from = clamp(isNum(input.from) ? input.from : clip ? clip.in : 0, 0, dur);
- // An unknown duration leaves `to` for the engine to probe.
- const to = isNum(input.to)
- ? clamp(input.to, 0, dur)
- : clip
- ? clip.out
- : Number.isFinite(dur)
- ? dur
- : undefined;
- if (to !== undefined && to <= from)
- throw new ToolError("from/to describe an empty range of the source.");
const res = await apiFetch(`/api/cut/projects/${projectId}/silence`, {
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -1153,6 +1123,39 @@ function resolveWatchTarget(
throw new ToolError("Pass clip_id or asset_id — see videoTrack and media in the editor state.");
}
+/** The shared preamble for the source-reading tools (watch_video, detect_silence):
+ * require an open project, resolve the target asset/clip, and derive the source
+ * range to scan. Each tool then does its own media-type check and API call. */
+function resolveWatchRange(
+ s: ReturnType<typeof useEditor.getState>,
+ input: Record<string, unknown>,
+): {
+ projectId: string;
+ asset: MediaAsset;
+ clip: { id: string; start: number; in: number; out: number; speed?: number } | null;
+ speed: number;
+ from: number;
+ to: number | undefined;
+} {
+ const projectId = s.projectId;
+ if (!projectId) throw new ToolError("No project open.");
+ const { asset, clip } = resolveWatchTarget(s, input);
+ const speed = clip?.speed && clip.speed > 0 ? clip.speed : 1;
+ const dur = asset.duration > 0 ? asset.duration : Infinity;
+ const from = clamp(isNum(input.from) ? input.from : clip ? clip.in : 0, 0, dur);
+ // An unknown duration leaves `to` for the engine to probe.
+ const to = isNum(input.to)
+ ? clamp(input.to, 0, dur)
+ : clip
+ ? clip.out
+ : Number.isFinite(dur)
+ ? dur
+ : undefined;
+ if (to !== undefined && to <= from)
+ throw new ToolError("from/to describe an empty range of the source.");
+ return { projectId, asset, clip, speed, from, to };
+}
+
function requireItem<T extends { id: string }>(pool: T[], id: unknown, label: string): T {
const item = pool.find((x) => x.id === String(id ?? ""));
if (!item) throw new ToolError(`No ${label} with id ${String(id)}. Call get_state for current ids.`);