Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.
Python ai-toolsauto-captionscaptionscliffmpegmcpmcp-serveropusclip-alternativepodcast-clipspodcastsshortform-videovertical-videowhisper
new: rewritten ui state listener / fixes
Nika Siradze14ff452e2b72
diff --git a/package-lock.json b/package-lock.jsonindex cd41aa2..bc4e8c5 100644--- a/package-lock.json+++ b/package-lock.json@@ -7,6 +7,7 @@"": {"name": "podcli","version": "1.0.0",+ "license": "MIT","dependencies": {"@modelcontextprotocol/sdk": "^1.12.1","dotenv": "^16.4.7",@@ -16,6 +17,9 @@"winston": "^3.17.0","zod": "^3.24.2"},+ "bin": {+ "podcli": "podcli"+ },"devDependencies": {"@types/express": "^5.0.0","@types/multer": "^1.4.12",@@ -23,6 +27,9 @@"@types/uuid": "^10.0.0","tsx": "^4.19.0","typescript": "^5.7.0"+ },+ "engines": {+ "node": ">=18.0.0"}},"node_modules/@colors/colors": {diff --git a/src/server.ts b/src/server.tsindex b2baaad..10cd5b6 100644--- a/src/server.ts+++ b/src/server.ts@@ -15,12 +15,13 @@ const kb = new KnowledgeBase();const assets = new AssetManager();const history = new ClipsHistory();-/** Prepend knowledge base context to a tool result if available. */+/** Prepend knowledge base file listing to a tool result. */async function withKnowledge(result: string): Promise<string> {try {- const context = await kb.readAll();- if (!context) return result;- return `[Knowledge Base]\n${context}\n\n---\n\n${result}`;+ const files = await kb.listFiles();+ if (!files.length) return result;+ const listing = files.map((f) => f.filename).join(", ");+ return `[Knowledge Base: ${listing} — use knowledge_base tool to read specific files]\n\n${result}`;} catch {return result;}@@ -95,6 +96,16 @@ export function createServer(): McpServer {async ({ suggestions }) => {try {const result = await handleSuggestClips({ suggestions });++ // Push suggestions to Web UI (silent fail if UI isn't running)+ try {+ await fetch("http://localhost:3847/api/ui-state", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ suggestions, phase: "review" }),+ });+ } catch {}+// Include clip history so Claude avoids duplicatesconst existing = await history.list(20);let text = await withKnowledge(result);@@ -245,29 +256,78 @@ export function createServer(): McpServer {},async (params) => {try {- const result = await handleBatchClips(params);- const parsed = JSON.parse(result);+ // Try routing through web server for real-time UI progress+ let usedWebServer = false;+ try {+ const webRes = await fetch("http://localhost:3847/api/mcp/export", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ video_path: params.video_path,+ clips: params.clips,+ transcript_words: params.transcript_words,+ }),+ });+ if (webRes.ok) {+ const webData = await webRes.json() as any;+ const jobId = webData.job_id;- // Record each successful clip- if (parsed.results) {- for (const r of parsed.results as any[]) {- if (r.status === "success" && r.output_path) {- await history.record({- source_video: params.video_path,- start_second: r.start_second || 0,- end_second: r.end_second || 0,- caption_style: r.caption_style || "hormozi",- crop_strategy: r.crop_strategy || "center",- title: r.title || "clip",- output_path: r.output_path,- file_size_mb: r.file_size_mb || 0,- duration: r.duration || 0,- });+ // Poll the job until completion+ let jobResult: any = null;+ while (true) {+ await new Promise((r) => setTimeout(r, 2000));+ const pollRes = await fetch(`http://localhost:3847/api/job/${jobId}`);+ if (!pollRes.ok) break;+ const job = await pollRes.json() as any;+ if (job.status === "done") {+ jobResult = job.result;+ break;+ }+ if (job.status === "error") {+ throw new Error(job.error || "Job failed");+ }}++ if (jobResult) {+ usedWebServer = true;+ return { content: [{ type: "text" as const, text: JSON.stringify(jobResult, null, 2) }] };+ }+ }+ } catch (webErr: unknown) {+ // Web server not running or errored — fall back to direct execution+ const webMsg = webErr instanceof Error ? webErr.message : String(webErr);+ if (!webMsg.includes("ECONNREFUSED") && !webMsg.includes("fetch failed")) {+ // Unexpected error from web server, still try fallback}}- return { content: [{ type: "text" as const, text: result }] };+ if (!usedWebServer) {+ const result = await handleBatchClips(params);+ const parsed = JSON.parse(result);++ // Record each successful clip+ if (parsed.results) {+ for (const r of parsed.results as any[]) {+ if (r.status === "success" && r.output_path) {+ await history.record({+ source_video: params.video_path,+ start_second: r.start_second || 0,+ end_second: r.end_second || 0,+ caption_style: r.caption_style || "hormozi",+ crop_strategy: r.crop_strategy || "center",+ title: r.title || "clip",+ output_path: r.output_path,+ file_size_mb: r.file_size_mb || 0,+ duration: r.duration || 0,+ });+ }+ }+ }++ return { content: [{ type: "text" as const, text: result }] };+ }++ return { content: [{ type: "text" as const, text: "Export completed via web server." }] };} catch (err: unknown) {const msg = err instanceof Error ? err.message : String(err);return {@@ -291,14 +351,23 @@ export function createServer(): McpServer {},async ({ action, filename, content }) => {try {- if (action === "read_all") {- const text = await kb.readAll();- return { content: [{ type: "text" as const, text: text || "Knowledge base is empty. Add .md files to .podcli/knowledge/ in the project directory." }] };- }- if (action === "list") {+ if (action === "read_all" || action === "list") {const files = await kb.listFiles();- const text = files.map((f) => `- ${f.filename} (updated ${f.updatedAt})`).join("\n");- return { content: [{ type: "text" as const, text: text || "No knowledge files found." }] };+ if (files.length === 0) {+ return { content: [{ type: "text" as const, text: "Knowledge base is empty. Add .md files to .podcli/knowledge/ in the project directory." }] };+ }+ const summaries: string[] = [];+ for (const f of files) {+ let preview = "";+ try {+ const content = await kb.readFile(f.filename);+ // First non-empty, non-heading line as preview+ const lines = content.split("\n").filter((l: string) => l.trim() && !l.startsWith("#"));+ preview = lines[0]?.trim().slice(0, 100) || "";+ } catch {}+ summaries.push(`- ${f.filename} (updated ${f.updatedAt})${preview ? `\n ${preview}` : ""}`);+ }+ return { content: [{ type: "text" as const, text: `Knowledge base (${files.length} files) — use read action with filename to get full content:\n${summaries.join("\n")}` }] };}if (action === "read" && filename) {const text = await kb.readFile(filename);@@ -405,5 +474,71 @@ export function createServer(): McpServer {});+ // =============================================+ // Tool: get_ui_state+ // =============================================+ server.tool(+ "get_ui_state",+ "Read the current state of the podcli Web UI — selected clips, video path, settings, transcript, and phase. Always returns the full transcript segments for clip analysis.",+ {},+ async () => {+ try {+ const res = await fetch("http://localhost:3847/api/ui-state");+ if (!res.ok) throw new Error(`HTTP ${res.status}`);+ const state = await res.json() as any;++ const lines: string[] = [];+ lines.push(`Phase: ${state.phase}`);+ lines.push(`Video: ${state.videoPath || state.filePath || "(none)"}`);+ lines.push(`Settings: caption=${state.settings?.captionStyle}, crop=${state.settings?.cropStrategy}, logo=${state.settings?.logoPath || "none"}`);+ lines.push(`Transcript: ${state.transcriptWordCount} words`);+ lines.push(`Clips: ${state.selectedClips?.length || 0} selected (${state.totalSuggestions || 0} total, ${state.deselectedCount || 0} deselected)`);++ if (state.selectedClips?.length) {+ lines.push("");+ lines.push("Selected clips:");+ for (const clip of state.selectedClips) {+ const title = (clip as any).title || "untitled";+ const start = (clip as any).start_second ?? "?";+ const end = (clip as any).end_second ?? "?";+ lines.push(` - "${title}" (${start}s – ${end}s)`);+ }+ }++ if (state.transcript) {+ const segments = state.transcript.segments || [];+ if (segments.length) {+ lines.push("");+ lines.push("=== TRANSCRIPT ===");+ for (const seg of segments) {+ const speaker = seg.speaker || "?";+ const start = Math.floor(seg.start || 0);+ const end = Math.floor(seg.end || 0);+ const text = seg.text || "";+ lines.push(`[${start}s-${end}s] ${speaker}: ${text}`);+ }+ }+ } else if (state.rawTranscriptText) {+ lines.push("");+ lines.push("=== RAW TRANSCRIPT (not yet parsed — use this to analyze and suggest clips) ===");+ lines.push(state.rawTranscriptText);+ }++ return { content: [{ type: "text" as const, text: lines.join("\n") }] };+ } catch (err: unknown) {+ const msg = err instanceof Error ? err.message : String(err);+ if (msg.includes("ECONNREFUSED") || msg.includes("fetch failed")) {+ return {+ content: [{ type: "text" as const, text: "Web UI is not running. Start with: npm run ui" }],+ };+ }+ return {+ content: [{ type: "text" as const, text: `Error reading UI state: ${msg}` }],+ isError: true,+ };+ }+ }+ );+return server;}diff --git a/src/ui/public/index.html b/src/ui/public/index.htmlindex be09647..cffa1c9 100644--- a/src/ui/public/index.html+++ b/src/ui/public/index.html@@ -373,6 +373,27 @@return selected;}+ // --- MCP ↔ UI Bridge: SSE hook ---+ function useSSE() {+ const [lastEvent, setLastEvent] = useState(null);+ const [connected, setConnected] = useState(false);+ useEffect(() => {+ const es = new EventSource('/api/events');+ const handle = (type) => (e) => {+ try { setLastEvent({ type, data: JSON.parse(e.data), ts: Date.now() }); } catch {}+ };+ es.addEventListener('state', (e) => { setConnected(true); handle('state')(e); });+ es.addEventListener('state-sync', handle('state-sync'));+ es.addEventListener('export-started', handle('export-started'));+ es.addEventListener('job-update', handle('job-update'));+ es.addEventListener('job-complete', handle('job-complete'));+ es.addEventListener('job-error', handle('job-error'));+ es.onerror = () => {};+ return () => es.close();+ }, []);+ return { lastEvent, connected };+ }+const CheckIcon = () => (<svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M2.5 6L5 8.5L9.5 3.5" stroke="#fff" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>);const CheckSmall = ({ color = 'var(--green)' }) => (<svg width="10" height="10" viewBox="0 0 12 12" fill="none"><path d="M2.5 6L5 8.5L9.5 3.5" stroke={color} strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"/></svg>);@@ -457,7 +478,6 @@const [cropStrategy, setCropStrategy] = useState('center');const [logoPath, setLogoPath] = useState('');const logoRef = useRef();- const [videoDragOver, setVideoDragOver] = useState(false);const [transcriptDragOver, setTranscriptDragOver] = useState(false);const [transcriptFileName, setTranscriptFileName] = useState('');@@ -478,14 +498,77 @@const [retryJobId, setRetryJobId] = useState(null);const retryStream = useJob(retryJobId);- const [videoUploading, setVideoUploading] = useState(false);- const [uploadProgress, setUploadProgress] = useState(0);const [logoUploading, setLogoUploading] = useState(false);- const [uploadingFileName, setUploadingFileName] = useState('');+ const [browsing, setBrowsing] = useState(false);const [encoderInfo, setEncoderInfo] = useState(null);useEffect(() => { api('/encoder-info').then(d => { if(d.best) setEncoderInfo(d); }).catch(()=>{}); }, []);+ // --- MCP ↔ UI Bridge: connect SSE + sync state ---+ const { lastEvent: sseEvent, connected: mcpConnected } = useSSE();++ // Sync UI state to server on changes (fire-and-forget)+ const prevSyncRef = useRef('');+ useEffect(() => {+ const state = {+ _source: 'ui',+ videoPath,+ filePath: file?.file_path || '',+ suggestions,+ deselectedIndices: Array.from(deselected),+ settings: { captionStyle, cropStrategy, logoPath },+ phase,+ };+ const key = JSON.stringify(state);+ if (key === prevSyncRef.current) return;+ prevSyncRef.current = key;+ fetch('/api/ui-state', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: key }).catch(() => {});+ }, [videoPath, file, suggestions, deselected, captionStyle, cropStrategy, logoPath, phase]);++ // Sync transcript separately (large payload)+ const prevTranscriptRef = useRef(null);+ useEffect(() => {+ if (!transcript || transcript === prevTranscriptRef.current) return;+ prevTranscriptRef.current = transcript;+ fetch('/api/ui-state', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ _source: 'ui', transcript }) }).catch(() => {});+ }, [transcript]);++ // Sync raw transcript text so MCP can read it before pipeline runs+ const prevRawRef = useRef('');+ useEffect(() => {+ if (transcriptText === prevRawRef.current) return;+ prevRawRef.current = transcriptText;+ if (transcriptText.trim()) {+ fetch('/api/ui-state', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ _source: 'ui', rawTranscriptText: transcriptText }) }).catch(() => {});+ }+ }, [transcriptText]);++ // React to SSE events from MCP+ const lastHandledTsRef = useRef(0);+ useEffect(() => {+ if (!sseEvent || sseEvent.ts === lastHandledTsRef.current) return;+ lastHandledTsRef.current = sseEvent.ts;++ if (sseEvent.type === 'state-sync') {+ const d = sseEvent.data;+ if (d.suggestions) { setSuggestions(d.suggestions); setDeselected(new Set()); }+ if (d.phase) setPhase(d.phase);+ if (d.videoPath !== undefined) setVideoPath(d.videoPath);+ if (d.transcript) setTranscript(d.transcript);+ if (d.settings) {+ if (d.settings.captionStyle) setCaptionStyle(d.settings.captionStyle);+ if (d.settings.cropStrategy) setCropStrategy(d.settings.cropStrategy);+ if (d.settings.logoPath !== undefined) setLogoPath(d.settings.logoPath);+ }+ } else if (sseEvent.type === 'export-started') {+ setBatchJobId(sseEvent.data.jobId);+ setPhase('exporting');+ } else if (sseEvent.type === 'job-complete' && phase === 'exporting') {+ setPhase('done');+ setResults(sseEvent.data.result?.results || []);+ }+ }, [sseEvent]);+// Preview panel stateconst videoRef = useRef();const [activeClipIdx, setActiveClipIdx] = useState(null);@@ -533,14 +616,14 @@setActiveClipIdx(null);};- // Upload- const doVideoUpload = useCallback(async (f) => {- setVideoUploading(true); setUploadProgress(0); setUploadingFileName(f.name);+ // Native file browse+ const doBrowse = useCallback(async () => {+ setBrowsing(true);try {- const d = await uploadFile(f, (pct) => setUploadProgress(pct));+ const d = await api('/browse-file');if (d.file_path) setVideoPath(d.file_path);- } catch(e) { setError('Upload failed: ' + e.message); }- finally { setVideoUploading(false); setUploadProgress(0); setUploadingFileName(''); }+ } catch(e) { setError('Browse failed: ' + e.message); }+ finally { setBrowsing(false); }}, []);// Pipeline@@ -655,8 +738,7 @@return 'pending';};- const handleVideoDrop = async (e) => { e.preventDefault(); setVideoDragOver(false); const files = e.dataTransfer?.files; if(files?.length) doVideoUpload(files[0]); };- const handleVideoFileSelect = async (e) => { const f = e.target.files?.[0]; if(f) doVideoUpload(f); };+ const handleVideoDrop = (e) => { e.preventDefault(); };const handleTranscriptDrop = (e) => { e.preventDefault(); setTranscriptDragOver(false); const files = e.dataTransfer?.files; if(!files?.length) return; const f=files[0]; setTranscriptFileName(f.name); const reader=new FileReader(); reader.onload=(ev)=>setTranscriptText(ev.target.result); reader.readAsText(f); };const handleTranscriptFileSelect = (e) => { const f=e.target.files?.[0]; if(!f) return; setTranscriptFileName(f.name); const reader=new FileReader(); reader.onload=(ev)=>setTranscriptText(ev.target.result); reader.readAsText(f); };const preventDef = (e) => e.preventDefault();@@ -669,6 +751,11 @@<div style={{ display:'flex', justifyContent:'space-between', alignItems:'center' }}><div className="logo">pod<span>cli</span></div><div style={{ display:'flex', alignItems:'center', gap: 8 }}>+ {mcpConnected && (+ <span className="pill" style={{ fontSize:10, letterSpacing:'0.5px', background:'var(--green-subtle)', color:'var(--green)', border:'1px solid var(--green-border)' }}>+ MCP linked+ </span>+ )}{encoderInfo && (<span className="pill pill-blue" style={{ fontSize:10, letterSpacing:'0.5px' }}>{encoderInfo.best === 'libx264' ? 'CPU' : encoderInfo.best.replace('h264_','').toUpperCase()}@@ -688,27 +775,23 @@{/* Video */}<div className="section"><div className="section-label">Video</div>- {videoUploading && (- <div className="drop-zone uploading fade-in">- <div style={{ display:'flex', alignItems:'center', justifyContent:'center', gap: 10 }}>- <div className="spinner sm" />- <div>- <div style={{ fontSize: 13, fontWeight: 600 }}>Uploading{'\u2026'} {uploadProgress}%</div>- <div style={{ fontSize: 11, color: 'var(--text2)', marginTop: 2 }}>{uploadingFileName}</div>+ {!videoPath && (+ <div className="drop-zone" style={{cursor:'pointer'}} onClick={browsing||isProcessing ? undefined : doBrowse}+ onDragOver={preventDef} onDrop={handleVideoDrop}>+ {browsing ? (+ <div style={{display:'flex',alignItems:'center',gap:10}}>+ <div className="spinner sm" />+ <div style={{fontSize:13,fontWeight:600}}>Waiting for file selection{'\u2026'}</div></div>- </div>- <div className="upload-bar" style={{ width: `${uploadProgress}%` }} />- </div>- )}- {!videoUploading && !videoPath && (- <div className={`drop-zone ${videoDragOver?'drag-over':''}`}- onDragOver={e=>{preventDef(e);setVideoDragOver(true);}} onDragLeave={()=>setVideoDragOver(false)} onDrop={handleVideoDrop}>- <div className="icon">{'\uD83C\uDFAC'}</div>- <div className="label">Drop your video here or <strong>browse</strong></div>- <input type="file" accept="video/*" onChange={handleVideoFileSelect} disabled={isProcessing} />+ ) : (+ <>+ <div className="icon">{'\uD83C\uDFAC'}</div>+ <div className="label"><strong>Browse</strong> to select a video file</div>+ </>+ )}</div>)}- {!videoUploading && videoPath && (+ {videoPath && (<div className="file-badge fade-in"><div className="dot" /><div className="name">{videoPath.split('/').pop()}</div>@@ -717,7 +800,7 @@)}<input type="text" placeholder="Or paste a local path: /Users/you/episode.mp4"value={videoPath} onChange={e=>setVideoPath(e.target.value)}- disabled={isProcessing||videoUploading}+ disabled={isProcessing||browsing}style={{ marginTop:8, fontSize:12, padding:'9px 13px', background:'var(--bg)', borderColor: videoPath?'var(--green-border)':'var(--border)' }} /></div>@@ -811,9 +894,16 @@{/* Generate */}{phase === 'idle' && (- <button className="btn btn-go" disabled={!videoPath.trim()||(transcriptMode==='import'&&!transcriptText.trim())||videoUploading} onClick={startPipeline}>- Generate Clips- </button>+ <div>+ <button className="btn btn-go" disabled={!videoPath.trim()||(transcriptMode==='import'&&!transcriptText.trim())||browsing} onClick={startPipeline}>+ Generate Clips+ </button>+ {mcpConnected && videoPath && transcriptText.trim() && (+ <div className="fade-in" style={{ textAlign:'center', marginTop:10, fontSize:12, color:'var(--text3)' }}>+ or ask Claude to find the best moments — your video and transcript are synced+ </div>+ )}+ </div>)}{/* Whisper progress */}diff --git a/src/ui/web-server.ts b/src/ui/web-server.tsindex 0de656f..e0e9212 100644--- a/src/ui/web-server.ts+++ b/src/ui/web-server.ts@@ -14,6 +14,7 @@ import multer from "multer";import { createReadStream, existsSync, statSync } from "fs";import { mkdir, readdir, unlink } from "fs/promises";import { join, dirname, basename, extname } from "path";+import { execSync } from "child_process";import { fileURLToPath } from "url";import { v4 as uuidv4 } from "uuid";@@ -54,6 +55,50 @@ const jobs = new Map<string, JobState>();// Store the latest transcript per uploaded file for the sessionconst sessionTranscripts = new Map<string, Record<string, unknown>>();+// --- MCP ↔ UI Bridge State ---+interface UIState {+ videoPath: string;+ filePath: string;+ transcript: Record<string, unknown> | null;+ rawTranscriptText: string;+ suggestions: Array<Record<string, unknown>>;+ deselectedIndices: number[];+ settings: {+ captionStyle: string;+ cropStrategy: string;+ logoPath: string;+ };+ phase: string;+ lastUpdated: number;+}++const uiState: UIState = {+ videoPath: "",+ filePath: "",+ transcript: null,+ rawTranscriptText: "",+ suggestions: [],+ deselectedIndices: [],+ settings: { captionStyle: "branded", cropStrategy: "center", logoPath: "" },+ phase: "idle",+ lastUpdated: 0,+};++// SSE clients for the global event bus+import type { Response } from "express";+const sseClients: Response[] = [];++function broadcastSSE(event: string, data: unknown) {+ const payload = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;+ for (let i = sseClients.length - 1; i >= 0; i--) {+ try {+ sseClients[i].write(payload);+ } catch {+ sseClients.splice(i, 1);+ }+ }+}+// --- Middleware ---app.use(express.json({ limit: "50mb" }));@@ -119,6 +164,40 @@ app.post("/api/select-file", (req, res) => {});});+/**+ * GET /api/browse-file — Open native OS file dialog and return the selected path+ */+app.get("/api/browse-file", (_req, res) => {+ try {+ let filePath: string;+ if (process.platform === "darwin") {+ const script = `osascript -e 'POSIX path of (choose file of type {"mp4","mov","mkv","webm","mp3","wav","m4a"})'`;+ filePath = execSync(script, { encoding: "utf-8", timeout: 120_000 }).trim();+ } else {+ // Linux fallback+ filePath = execSync(+ `zenity --file-selection --file-filter="Media files|*.mp4 *.mov *.mkv *.webm *.mp3 *.wav *.m4a"`,+ { encoding: "utf-8", timeout: 120_000 }+ ).trim();+ }++ if (!filePath || !existsSync(filePath)) {+ res.json({ error: "cancelled" });+ return;+ }++ const stat = statSync(filePath);+ res.json({+ file_path: filePath,+ filename: basename(filePath),+ size_mb: Math.round((stat.size / (1024 * 1024)) * 100) / 100,+ });+ } catch {+ // User cancelled the dialog (non-zero exit) or command not found+ res.json({ error: "cancelled" });+ }+});+/*** POST /api/import-transcript — Import an existing transcript (skip Whisper)* Accepts: { file_path, transcript } where transcript is an object with:@@ -328,6 +407,7 @@ app.post("/api/create-clip", async (req, res) => {job.progress = 100;job.message = "Clip created!";job.result = result.data;+ broadcastSSE("job-complete", { jobId, result: result.data });// Record to historytry {const d = result.data as any;@@ -346,6 +426,7 @@ app.post("/api/create-clip", async (req, res) => {job.status = "error";job.error = err.message;job.message = `Error: ${err.message}`;+ broadcastSSE("job-error", { jobId, error: err.message });});});@@ -389,6 +470,7 @@ app.post("/api/batch-clips", async (req, res) => {job.progress = 100;job.message = "Batch complete!";job.result = result.data;+ broadcastSSE("job-complete", { jobId, result: result.data });// Record successful clips to historytry {const d = result.data as any;@@ -415,6 +497,7 @@ app.post("/api/batch-clips", async (req, res) => {job.status = "error";job.error = err.message;job.message = `Error: ${err.message}`;+ broadcastSSE("job-error", { jobId, error: err.message });});});@@ -783,6 +866,199 @@ app.delete("/api/knowledge/:filename", async (req, res) => {}});+// --- MCP ↔ UI Bridge Endpoints ---++/**+ * GET /api/events — Global SSE channel for real-time MCP→UI events+ */+app.get("/api/events", (_req, res) => {+ res.writeHead(200, {+ "Content-Type": "text/event-stream",+ "Cache-Control": "no-cache",+ Connection: "keep-alive",+ });++ // Send current state on connect+ res.write(`event: state\ndata: ${JSON.stringify(uiState)}\n\n`);++ sseClients.push(res);++ // Heartbeat every 15s+ const heartbeat = setInterval(() => {+ try {+ res.write(`: heartbeat\n\n`);+ } catch {+ clearInterval(heartbeat);+ }+ }, 15_000);++ _req.on("close", () => {+ clearInterval(heartbeat);+ const idx = sseClients.indexOf(res);+ if (idx !== -1) sseClients.splice(idx, 1);+ });+});++/**+ * GET /api/ui-state — MCP reads current UI state+ */+app.get("/api/ui-state", (_req, res) => {+ const selected = uiState.suggestions.filter(+ (_: unknown, i: number) => !uiState.deselectedIndices.includes(i)+ );+ res.json({+ videoPath: uiState.videoPath,+ filePath: uiState.filePath,+ phase: uiState.phase,+ settings: uiState.settings,+ selectedClips: selected,+ totalSuggestions: uiState.suggestions.length,+ deselectedCount: uiState.deselectedIndices.length,+ transcriptWordCount: Array.isArray((uiState.transcript as any)?.words)+ ? (uiState.transcript as any).words.length+ : 0,+ transcript: uiState.transcript,+ rawTranscriptText: uiState.rawTranscriptText,+ lastUpdated: uiState.lastUpdated,+ });+});++/**+ * POST /api/ui-state — UI syncs state changes to server+ */+app.post("/api/ui-state", (req, res) => {+ const body = req.body;+ // Track which fields changed for targeted SSE broadcasts+ const source = body._source || "mcp"; // UI sends _source:'ui'++ if (body.videoPath !== undefined) uiState.videoPath = body.videoPath;+ if (body.filePath !== undefined) uiState.filePath = body.filePath;+ if (body.transcript !== undefined) uiState.transcript = body.transcript;+ if (body.rawTranscriptText !== undefined) uiState.rawTranscriptText = body.rawTranscriptText;+ if (body.suggestions !== undefined) uiState.suggestions = body.suggestions;+ if (body.deselectedIndices !== undefined) uiState.deselectedIndices = body.deselectedIndices;+ if (body.phase !== undefined) uiState.phase = body.phase;+ if (body.settings) {+ if (body.settings.captionStyle !== undefined) uiState.settings.captionStyle = body.settings.captionStyle;+ if (body.settings.cropStrategy !== undefined) uiState.settings.cropStrategy = body.settings.cropStrategy;+ if (body.settings.logoPath !== undefined) uiState.settings.logoPath = body.settings.logoPath;+ }+ uiState.lastUpdated = Date.now();++ // Broadcast to UI when changes come from MCP (not from UI itself)+ if (source !== "ui") {+ broadcastSSE("state-sync", {+ ...(body.videoPath !== undefined && { videoPath: body.videoPath }),+ ...(body.filePath !== undefined && { filePath: body.filePath }),+ ...(body.suggestions !== undefined && { suggestions: body.suggestions }),+ ...(body.phase !== undefined && { phase: body.phase }),+ ...(body.transcript !== undefined && { transcript: body.transcript }),+ ...(body.settings && { settings: body.settings }),+ });+ }++ res.json({ ok: true });+});++/**+ * POST /api/mcp/export — MCP triggers export using current UI state+ */+app.post("/api/mcp/export", async (req, res) => {+ // Use UI state if no explicit params provided+ const videoPath = req.body.video_path || uiState.filePath || uiState.videoPath;+ const clips = req.body.clips || uiState.suggestions.filter(+ (_: unknown, i: number) => !uiState.deselectedIndices.includes(i)+ );+ const transcriptWords = req.body.transcript_words ||+ (Array.isArray((uiState.transcript as any)?.words) ? (uiState.transcript as any).words : []);+ const logoPath = req.body.logo_path || uiState.settings.logoPath || null;+ const captionStyle = req.body.caption_style || uiState.settings.captionStyle || "branded";+ const cropStrategy = req.body.crop_strategy || uiState.settings.cropStrategy || "center";++ if (!videoPath || !existsSync(videoPath)) {+ res.status(400).json({ error: "Video file not found" });+ return;+ }+ if (!clips.length) {+ res.status(400).json({ error: "No clips to export" });+ return;+ }++ await fileManager.ensureDirectories();++ // Apply style settings to clips that don't have their own+ const styledClips = clips.map((c: any) => ({+ start_second: c.start_second,+ end_second: c.end_second,+ title: (c.title || "clip").slice(0, 40),+ caption_style: c.caption_style || captionStyle,+ crop_strategy: c.crop_strategy || cropStrategy,+ }));++ const jobId = uuidv4();+ const job: JobState = {+ id: jobId,+ type: "batch_clips",+ status: "running",+ progress: 0,+ message: "Starting MCP export...",+ createdAt: Date.now(),+ };+ jobs.set(jobId, job);++ // Broadcast to UI so it can track progress+ broadcastSSE("export-started", { jobId, clipCount: styledClips.length });+ uiState.phase = "exporting";+ uiState.lastUpdated = Date.now();++ res.json({ job_id: jobId, status: "running", clipCount: styledClips.length });++ executor+ .execute(+ "batch_clips",+ { video_path: videoPath, clips: styledClips, transcript_words: transcriptWords, output_dir: paths.output, logo_path: logoPath },+ (event) => {+ job.progress = event.percent;+ job.message = event.message;+ broadcastSSE("job-update", { jobId, progress: event.percent, message: event.message });+ }+ )+ .then(async (result) => {+ job.status = "done";+ job.progress = 100;+ job.message = "Export complete!";+ job.result = result.data;+ // Record clips to history+ try {+ const d = result.data as any;+ if (d?.results) {+ for (const r of d.results) {+ if (r.status === "success" && r.output_path) {+ await clipsHistory.record({+ source_video: videoPath,+ start_second: r.start_second || 0,+ end_second: r.end_second || 0,+ caption_style: r.caption_style || captionStyle,+ crop_strategy: r.crop_strategy || cropStrategy,+ title: r.title || "clip",+ output_path: r.output_path,+ file_size_mb: r.file_size_mb || 0,+ duration: r.duration || 0,+ });+ }+ }+ }+ } catch {}+ broadcastSSE("job-complete", { jobId, result: result.data });+ })+ .catch((err) => {+ job.status = "error";+ job.error = err.message;+ job.message = `Error: ${err.message}`;+ broadcastSSE("job-error", { jobId, error: err.message });+ });+});+// --- Start ---async function main() {await fileManager.ensureDirectories();