Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

26.2 KB
Raw Blame
import { db } from "@cap/database";
import { organizations, videos } from "@cap/database/schema";
import type { VideoMetadata } from "@cap/database/types";
import { Storage } from "@cap/web-backend/src/Storage/index";
import {
	AI_GENERATION_LANGUAGE_AUTO,
	type AiGenerationLanguage,
	getAiGenerationLanguageName,
	parseAiGenerationLanguage,
	type Video,
} from "@cap/web-domain";
import { generateText } from "ai";
import { and, eq, sql } from "drizzle-orm";
import { Effect, Option } from "effect";
import { FatalError } from "workflow";
import { isAiConfigured } from "@/lib/ai/provider";
import { AiUnavailableError, runWithAiProviders } from "@/lib/ai/run";
import { setGeneratedAiContent } from "@/lib/ai-content-metadata";
import { enqueueVideoStorageNameSync } from "@/lib/sync-video-storage-names";
import { decodeStorageVideo } from "@/lib/video-storage";
import { runWorkflowPromise } from "@/lib/workflow-runtime";

interface GenerateAiWorkflowPayload {
	videoId: string;
	userId: string;
}

interface VideoData {
	video: typeof videos.$inferSelect;
	metadata: VideoMetadata;
	aiGenerationLanguage: AiGenerationLanguage;
}

interface VttSegment {
	start: number;
	text: string;
}

interface TranscriptData {
	segments: VttSegment[];
	text: string;
}

interface AiResult {
	title?: string;
	summary?: string;
	chapters?: { title: string; start: number }[];
}

const getAffectedRows = (result: unknown) => {
	if (Array.isArray(result)) {
		return (
			(result[0] as { affectedRows?: number } | undefined)?.affectedRows ?? 0
		);
	}

	return (result as { affectedRows?: number } | undefined)?.affectedRows ?? 0;
};

const MAX_CHARS_PER_CHUNK = 24000;
const LEGACY_AI_TITLE_FALLBACK = "Generated Title";
const LEGACY_AI_SUMMARY_FALLBACK =
	"The AI was unable to generate a proper summary for this content.";
const GENERATED_TITLE_PATTERN =
	/^(Cap (Recording|Upload) - .+|Cap \d{4}-\d{2}-\d{2} at \d{2}[.:]\d{2}[.:]\d{2}|Untitled|\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|.+ \((Display|Window|Area|Camera)\) \d{4}-\d{2}-\d{2} \d{2}:\d{2} [AP]M)$/;

export function shouldReplaceVideoTitle({
	currentTitle,
	previousAiTitle,
	nextAiTitle,
	sourceName,
	titleManuallyEdited,
}: {
	currentTitle: string | null;
	previousAiTitle?: string | null;
	nextAiTitle?: string | null;
	sourceName?: string | null;
	titleManuallyEdited?: boolean | null;
}) {
	const nextTitle = nextAiTitle?.trim();
	if (!nextTitle) return false;
	if (titleManuallyEdited) return false;

	const title = currentTitle?.trim();
	if (!title) return true;
	if (previousAiTitle?.trim() && title === previousAiTitle.trim()) return true;
	if (sourceName?.trim() && title === sourceName.trim()) return true;
	if (title === LEGACY_AI_TITLE_FALLBACK) return true;
	return GENERATED_TITLE_PATTERN.test(title);
}

export async function generateAiWorkflow(payload: GenerateAiWorkflowPayload) {
	"use workflow";

	const { videoId, userId } = payload;

	let videoData: VideoData;
	try {
		videoData = await validateAndSetProcessing(videoId);
	} catch (error) {
		await markError(videoId);
		throw error;
	}

	try {
		const transcript = await fetchTranscript(videoId, userId, videoData.video);

		if (!transcript) {
			await markSkipped(videoId);
			return {
				success: true,
				message: "Transcript empty or too short - skipped",
			};
		}

		const result = await generateWithAi(
			transcript,
			videoData.aiGenerationLanguage,
		);

		await saveResults(videoId, videoData, result);
	} catch (error) {
		await markError(videoId);
		throw error;
	}

	return { success: true, message: "AI generation completed successfully" };
}

async function validateAndSetProcessing(videoId: string): Promise<VideoData> {
	"use step";

	if (!isAiConfigured()) {
		throw new FatalError("No AI provider configured");
	}

	const query = await db()
		.select({ video: videos, orgSettings: organizations.settings })
		.from(videos)
		.leftJoin(organizations, eq(videos.orgId, organizations.id))
		.where(eq(videos.id, videoId as Video.VideoId));

	if (query.length === 0 || !query[0]?.video) {
		throw new FatalError("Video does not exist");
	}

	const { video } = query[0];
	const metadata = (video.metadata as VideoMetadata) || {};

	if (video.transcriptionStatus !== "COMPLETE") {
		throw new FatalError("Transcription not complete");
	}

	if (
		metadata.summary &&
		metadata.summary !== LEGACY_AI_SUMMARY_FALLBACK &&
		metadata.chapters
	) {
		throw new FatalError("AI metadata already generated");
	}

	let processingMetadata = sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.aiGenerationStatus', 'PROCESSING')`;
	for (const [metadataPath, fallback] of [
		["$.aiTitle", LEGACY_AI_TITLE_FALLBACK],
		["$.summary", LEGACY_AI_SUMMARY_FALLBACK],
	] as const) {
		processingMetadata = sql`IF(JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, ${metadataPath})) = ${fallback} AND (${metadataPath} <> '$.summary' OR COALESCE(JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.summaryManuallyEdited')), 'false') <> 'true'), JSON_REMOVE(${processingMetadata}, ${metadataPath}), ${processingMetadata})`;
	}

	await db()
		.update(videos)
		.set({
			metadata: processingMetadata,
		})
		.where(eq(videos.id, videoId as Video.VideoId));

	return {
		video,
		metadata,
		aiGenerationLanguage: parseAiGenerationLanguage(
			query[0]?.orgSettings?.aiGenerationLanguage,
		),
	};
}

async function fetchTranscript(
	videoId: string,
	userId: string,
	video: typeof videos.$inferSelect,
): Promise<TranscriptData | null> {
	"use step";

	const vtt = await Effect.gen(function* () {
		const [bucket] = yield* Storage.getAccessForVideo(
			decodeStorageVideo(video),
		);
		return yield* bucket.getObject(`${userId}/${videoId}/transcription.vtt`);
	}).pipe(runWorkflowPromise);

	if (Option.isNone(vtt)) {
		return null;
	}

	const segments = parseVttWithTimestamps(vtt.value);
	const text = segments
		.map((s) => s.text)
		.join(" ")
		.trim();

	if (text.length < 10) {
		return null;
	}

	return { segments, text };
}

async function markError(videoId: string): Promise<void> {
	"use step";

	await db()
		.update(videos)
		.set({
			metadata: sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.aiGenerationStatus', 'ERROR')`,
		})
		.where(
			and(
				eq(videos.id, videoId as Video.VideoId),
				sql`NOT (
					COALESCE(JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.aiGenerationStatus')), '') = 'COMPLETE'
					AND JSON_EXTRACT(${videos.metadata}, '$.summary') IS NOT NULL
					AND JSON_EXTRACT(${videos.metadata}, '$.chapters') IS NOT NULL
				)`,
			),
		);
}

async function markSkipped(videoId: string): Promise<void> {
	"use step";

	await db()
		.update(videos)
		.set({
			metadata: sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.aiGenerationStatus', 'SKIPPED')`,
		})
		.where(eq(videos.id, videoId as Video.VideoId));
}

async function generateWithAi(
	transcript: TranscriptData,
	language: AiGenerationLanguage,
): Promise<AiResult> {
	"use step";

	const chunks = chunkTranscriptWithTimestamps(transcript.segments);

	const videoDuration = getVideoDuration(transcript.segments);
	const languageInstruction = getAiLanguageInstruction(language);

	let result: AiResult;
	if (chunks.length === 1) {
		result = await generateSingleChunk(
			transcript.segments,
			videoDuration,
			languageInstruction,
		);
	} else {
		result = await generateMultipleChunks(
			chunks,
			videoDuration,
			languageInstruction,
		);
	}

	if (result.chapters) {
		result.chapters = clampChapters(result.chapters, videoDuration);
	}

	return result;
}

export function getAiLanguageInstruction(
	language: AiGenerationLanguage,
): string {
	if (language === AI_GENERATION_LANGUAGE_AUTO) {
		return "Write the title, summary, chapter titles, section summaries, and key points in the same language as the transcript.";
	}

	return `Write the title, summary, chapter titles, section summaries, and key points in ${getAiGenerationLanguageName(language)}.`;
}

export function getAiContentGuidelines(videoDuration: number): {
	summary: string;
	chapters: string;
} {
	let lengthInstruction: string;
	if (videoDuration < 60) {
		lengthInstruction = "Use no more than 35 words and one or two sentences.";
	} else if (videoDuration < 180) {
		lengthInstruction =
			"Aim for 50-90 words in one concise paragraph, but use fewer when that fully communicates the video.";
	} else if (videoDuration < 600) {
		lengthInstruction =
			"Aim for 80-150 words in one concise paragraph, but use fewer when that fully communicates the video.";
	} else if (videoDuration < 1800) {
		lengthInstruction =
			"Aim for 150-250 words. Use short paragraphs or Markdown bullets only when they materially improve clarity.";
	} else {
		lengthInstruction =
			"Aim for 250-400 words. Exceed 400 only when necessary to preserve important decisions, responsibilities, or next steps.";
	}

	return {
		summary: `- Write a standalone summary that lets someone understand the video without watching it.
- State the subject and the speaker's intention first: what the video is about and why it was recorded. If the intention is not explicit, describe only what the transcript supports.
- Then include only the essential explanation, outcomes, decisions, action items, and next steps needed to understand or act on the video.
- Prioritize meaning and useful information over chronological retelling.
- Omit filler, greetings, reactions, apologies, repetition, incidental conversation, minor UI actions, and timestamps unless a timestamp is essential to the viewer.
- Write from the primary speaker's point of view. For a single-person video, use "I" and "my". Use "we" only when the speaker clearly represents a team or several participants share the discussion.
- Never describe the primary voice as "the speaker", "the presenter", "the user", "they", or any similar detached label.
- If multiple speakers need to be distinguished, use names only when the transcript identifies them unambiguously. Otherwise summarize the discussion directly without inventing names or identities.
- Be concise, but never omit information required to understand or act on the video. Do not pad the summary to reach a target length.
- Convert detached narration into first person. For example, write "I review the proposal" instead of "The speaker reviews the proposal". Do not introduce names, projects, or personal details that are not present in the transcript.
- ${lengthInstruction}`,
		chapters:
			videoDuration < 120
				? 'Return an empty "chapters" array because videos shorter than two minutes do not need chapters.'
				: "Create the fewest chapters needed to identify meaningful topic or phase changes. Do not create chapters for filler, minor UI actions, or every transcript segment.",
	};
}

function getVideoDuration(segments: VttSegment[]): number {
	if (segments.length === 0) return 0;
	const lastSegment = segments[segments.length - 1];
	return lastSegment ? lastSegment.start + 3 : 0;
}

function clampChapters(
	chapters: { title: string; start: number }[],
	videoDuration: number,
): { title: string; start: number }[] {
	const filtered = chapters.filter((ch) => ch.start < videoDuration);

	if (filtered.length === 0 && chapters.length > 0) {
		const first = chapters[0];
		return first ? [{ title: first.title, start: 0 }] : [];
	}

	const minGap = Math.max(5, Math.floor(videoDuration / 10));
	const deduped: { title: string; start: number }[] = [];
	for (const chapter of filtered) {
		const last = deduped[deduped.length - 1];
		if (!last || Math.abs(chapter.start - last.start) >= minGap) {
			deduped.push(chapter);
		}
	}

	return deduped;
}

async function saveResults(
	videoId: string,
	videoData: VideoData,
	result: AiResult,
): Promise<void> {
	"use step";

	const { video, metadata } = videoData;
	const generatedTitle = result.title?.trim();
	const currentVideo = await getCurrentVideo(videoId);
	const currentMetadata = currentVideo
		? (currentVideo.metadata as VideoMetadata) || {}
		: metadata;
	const currentTitle = currentVideo?.name ?? video.name;

	let metadataUpdate = sql`COALESCE(${videos.metadata}, JSON_OBJECT())`;
	if (generatedTitle) {
		metadataUpdate = sql`JSON_SET(${metadataUpdate}, '$.aiTitle', ${generatedTitle})`;
	}
	if (result.summary) {
		metadataUpdate = setGeneratedAiContent(
			metadataUpdate,
			"summary",
			result.summary,
		);
	}
	if (result.chapters) {
		metadataUpdate = setGeneratedAiContent(
			metadataUpdate,
			"chapters",
			result.chapters,
		);
	}
	metadataUpdate = sql`JSON_SET(${metadataUpdate}, '$.aiGenerationStatus', 'COMPLETE')`;

	await db()
		.update(videos)
		.set({ metadata: metadataUpdate })
		.where(eq(videos.id, videoId as Video.VideoId));

	if (
		generatedTitle &&
		shouldReplaceVideoTitle({
			currentTitle,
			previousAiTitle: currentMetadata.aiTitle,
			nextAiTitle: generatedTitle,
			sourceName: currentMetadata.sourceName,
			titleManuallyEdited: currentMetadata.titleManuallyEdited,
		})
	) {
		const titleUpdate = await db()
			.update(videos)
			.set({ name: generatedTitle })
			.where(
				and(
					eq(videos.id, videoId as Video.VideoId),
					eq(videos.name, currentTitle),
					sql`COALESCE(JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.titleManuallyEdited')), 'false') <> 'true'`,
				),
			);
		if (getAffectedRows(titleUpdate) > 0) {
			await enqueueVideoStorageNameSync(videoId as Video.VideoId);
		}
	}
}

async function getCurrentVideo(
	videoId: string,
): Promise<typeof videos.$inferSelect | null> {
	const [currentVideo] = await db()
		.select()
		.from(videos)
		.where(eq(videos.id, videoId as Video.VideoId));

	return currentVideo ?? null;
}

function parseVttWithTimestamps(vttContent: string): VttSegment[] {
	const lines = vttContent.split("\n");
	const segments: VttSegment[] = [];
	let currentStart = 0;

	for (let i = 0; i < lines.length; i++) {
		const line = lines[i]?.trim() ?? "";
		if (line.includes("-->")) {
			const timeMatch = line.match(/(\d{2}):(\d{2}):(\d{2})[.,](\d{3})/);
			if (timeMatch) {
				currentStart =
					parseInt(timeMatch[1] ?? "0", 10) * 3600 +
					parseInt(timeMatch[2] ?? "0", 10) * 60 +
					parseInt(timeMatch[3] ?? "0", 10);
			}
		} else if (
			line &&
			line !== "WEBVTT" &&
			!/^\d+$/.test(line) &&
			!line.includes("-->")
		) {
			segments.push({ start: currentStart, text: line });
		}
	}

	return segments;
}

function chunkTranscriptWithTimestamps(
	segments: VttSegment[],
): { text: string; startTime: number; endTime: number }[] {
	const chunks: { text: string; startTime: number; endTime: number }[] = [];
	let currentChunk: VttSegment[] = [];
	let currentLength = 0;

	for (const segment of segments) {
		if (
			currentLength + segment.text.length > MAX_CHARS_PER_CHUNK &&
			currentChunk.length > 0
		) {
			chunks.push({
				text: currentChunk.map((s) => s.text).join(" "),
				startTime: currentChunk[0]?.start ?? 0,
				endTime: currentChunk[currentChunk.length - 1]?.start ?? 0,
			});
			currentChunk = [];
			currentLength = 0;
		}
		currentChunk.push(segment);
		currentLength += segment.text.length + 1;
	}

	if (currentChunk.length > 0) {
		chunks.push({
			text: currentChunk.map((s) => s.text).join(" "),
			startTime: currentChunk[0]?.start ?? 0,
			endTime: currentChunk[currentChunk.length - 1]?.start ?? 0,
		});
	}

	return chunks;
}

class InvalidAiOutputError extends Error {
	constructor(message: string, options?: { cause?: unknown }) {
		super(message, options);
		this.name = "InvalidAiOutputError";
	}
}

/**
 * True when the whole provider chain was exhausted and the terminal failure
 * was a fulfilled-but-unusable response rather than a request-level failure.
 */
function failedOnInvalidOutput(error: unknown): boolean {
	return (
		error instanceof AiUnavailableError &&
		error.cause instanceof InvalidAiOutputError
	);
}

export async function callAiApi<T>(
	prompt: string,
	parse: (text: string) => T,
): Promise<T> {
	return runWithAiProviders("generation", async (selection) => {
		const result = await generateText({
			model: selection.model({ jsonRepair: true }),
			prompt,
			maxOutputTokens: selection.defaultMaxOutputTokens,
		});
		// Parse inside the provider loop so an empty, malformed, or truncated
		// fulfilled response falls through to the next provider too.
		try {
			return parse(result.text);
		} catch (error) {
			throw new InvalidAiOutputError(
				error instanceof Error ? error.message : String(error),
				{ cause: error },
			);
		}
	});
}

function cleanJsonResponse(content: string): string {
	if (content.includes("```json")) {
		return content.replace(/```json\s*/g, "").replace(/```\s*/g, "");
	}
	if (content.includes("```")) {
		return content.replace(/```\s*/g, "");
	}
	return content;
}

function extractJsonObject(content: string): string {
	const cleanedContent = cleanJsonResponse(content).trim();
	const start = cleanedContent.indexOf("{");
	if (start < 0) {
		throw new Error("AI response did not contain a JSON object");
	}

	let depth = 0;
	let inString = false;
	let escaped = false;
	for (let i = start; i < cleanedContent.length; i++) {
		const character = cleanedContent[i];
		if (inString) {
			if (escaped) {
				escaped = false;
			} else if (character === "\\") {
				escaped = true;
			} else if (character === '"') {
				inString = false;
			}
			continue;
		}

		if (character === '"') {
			inString = true;
		} else if (character === "{") {
			depth += 1;
		} else if (character === "}") {
			depth -= 1;
			if (depth === 0) {
				return cleanedContent.slice(start, i + 1);
			}
		}
	}

	throw new Error("AI response contained an incomplete JSON object");
}

async function generateSingleChunk(
	segments: VttSegment[],
	videoDuration: number,
	languageInstruction: string,
): Promise<AiResult> {
	const transcriptWithTimestamps = segments
		.map(
			(s) =>
				`[${Math.floor(s.start / 60)}:${String(s.start % 60).padStart(2, "0")}] ${s.text}`,
		)
		.join("\n");
	const contentGuidelines = getAiContentGuidelines(videoDuration);

	const prompt = `You are Cap AI, an expert at turning video transcripts into useful, concise summaries.

The video is ${videoDuration} seconds long (${Math.floor(videoDuration / 60)}:${String(Math.floor(videoDuration % 60)).padStart(2, "0")} total). Analyze this timestamped transcript and provide JSON:
{
  "title": "string (concise but descriptive title that captures the main topic)",
  "summary": "string (standalone summary of the subject, intention, essential information, outcome, and next steps)",
  "chapters": [{"title": "string (descriptive chapter title)", "start": number (seconds from start)}]
}

Summary requirements:
${contentGuidelines.summary}

Chapter requirements:
${contentGuidelines.chapters}

Additional requirements:
- ${languageInstruction}
- Keep JSON property names exactly as shown.
- Include specific names, numbers, decisions, and conclusions only when they help someone understand or act on the video.
- IMPORTANT: All chapter "start" values MUST be between 0 and ${videoDuration} seconds. Use the timestamps from the transcript to determine accurate chapter start times.

Return ONLY valid JSON without any markdown formatting or code blocks.
Transcript:
${transcriptWithTimestamps}`;

	return callAiApi(prompt, parseAiResponse);
}

async function generateMultipleChunks(
	chunks: { text: string; startTime: number; endTime: number }[],
	videoDuration: number,
	languageInstruction: string,
): Promise<AiResult> {
	const chunkSummaries: {
		summary: string;
		keyPoints: string[];
		chapters: { title: string; start: number }[];
		startTime: number;
		endTime: number;
	}[] = [];
	const contentGuidelines = getAiContentGuidelines(videoDuration);

	for (let i = 0; i < chunks.length; i++) {
		const chunk = chunks[i];
		if (!chunk) continue;

		const chunkPrompt = `You are Cap AI, analyzing one section of a video for a later final summary. This is section ${i + 1} of ${chunks.length} from a video that is ${videoDuration} seconds long (${Math.floor(videoDuration / 60)}:${String(Math.floor(videoDuration % 60)).padStart(2, "0")} total). This section covers timestamp ${Math.floor(chunk.startTime / 60)}:${String(chunk.startTime % 60).padStart(2, "0")} to ${Math.floor(chunk.endTime / 60)}:${String(chunk.endTime % 60).padStart(2, "0")}.

Extract only the information needed to understand this section's contribution to the full video and provide JSON:
{
  "summary": "string (concise factual notes about the subject, intention, essential explanation, outcomes, decisions, or next steps in this section)",
  "keyPoints": ["string (essential key point or takeaway, or an empty array when there is none)", ...],
  "chapters": [{"title": "string (descriptive title for this topic/section)", "start": number (seconds from video start)}]
}

- Preserve specific names, numbers, decisions, responsibilities, and conclusions that matter to the final summary.
- Omit filler, greetings, reactions, apologies, repetition, incidental conversation, and minor UI actions.
- Do not narrate the transcript chronologically or pad the section analysis.
- ${contentGuidelines.chapters}
- ${languageInstruction}
- Keep JSON property names exactly as shown.
IMPORTANT: All chapter "start" values MUST be between ${chunk.startTime} and ${chunk.endTime} seconds. The total video is only ${videoDuration} seconds long.
Return ONLY valid JSON without any markdown formatting or code blocks.
Transcript section:
${chunk.text}`;

		try {
			const parsed = await callAiApi(chunkPrompt, parseChunkAnalysis);
			chunkSummaries.push({
				...parsed,
				startTime: chunk.startTime,
				endTime: chunk.endTime,
			});
		} catch (error) {
			// A chunk is skipped only when every provider returned unusable
			// JSON; a request-level chain failure still fails the workflow.
			if (!failedOnInvalidOutput(error)) throw error;
		}
	}

	const allChapters: { title: string; start: number }[] = [];
	const sortedChapters = chunkSummaries
		.flatMap((c) => c.chapters)
		.sort((a, b) => a.start - b.start);
	const minGap = Math.max(5, Math.floor(videoDuration / 10));
	for (const chapter of sortedChapters) {
		const lastChapter = allChapters[allChapters.length - 1];
		if (!lastChapter || Math.abs(chapter.start - lastChapter.start) >= minGap) {
			allChapters.push(chapter);
		}
	}

	const allKeyPoints = chunkSummaries.flatMap((c) => c.keyPoints);

	const sectionDetails = chunkSummaries
		.map((c, i) => {
			const timeRange = `${Math.floor(c.startTime / 60)}:${String(c.startTime % 60).padStart(2, "0")} - ${Math.floor(c.endTime / 60)}:${String(c.endTime % 60).padStart(2, "0")}`;
			const keyPointsList =
				c.keyPoints.length > 0 ? `\nKey points: ${c.keyPoints.join("; ")}` : "";
			return `Section ${i + 1} (${timeRange}):\n${c.summary}${keyPointsList}`;
		})
		.join("\n\n");

	const finalPrompt = `You are Cap AI, an expert at turning video analyses into useful, concise summaries.

Using these section analyses, create a standalone final summary that lets someone understand the video without watching it.

Section analyses:
${sectionDetails}

${allKeyPoints.length > 0 ? `All key points identified:\n${allKeyPoints.map((p, i) => `${i + 1}. ${p}`).join("\n")}\n` : ""}

Provide JSON in the following format:
{
  "title": "string (concise but descriptive title that captures the main topic/purpose)",
  "summary": "string (standalone summary of the subject, intention, essential information, outcome, and next steps)"
}

Summary requirements:
${contentGuidelines.summary}

Additional requirements:
- ${languageInstruction}
- Keep JSON property names exactly as shown.
Return ONLY valid JSON without any markdown formatting or code blocks.`;

	try {
		const parsed = await callAiApi(finalPrompt, parseFinalSummary);
		return {
			title: parsed.title,
			summary: parsed.summary,
			chapters: allChapters,
		};
	} catch (error) {
		if (!failedOnInvalidOutput(error)) throw error;
		const fallbackSummary = chunkSummaries
			.map((c, i) => `**Part ${i + 1}:** ${c.summary}`)
			.join("\n\n");
		const keyPointsSummary =
			allKeyPoints.length > 0
				? `\n\n**Key Points:**\n${allKeyPoints.map((p) => `- ${p}`).join("\n")}`
				: "";
		return {
			title: "Video Summary",
			summary: fallbackSummary + keyPointsSummary,
			chapters: allChapters,
		};
	}
}

// Like parseAiResponse, these throw on missing or empty required fields —
// inside the provider loop that sends the attempt to the next provider
// instead of completing the workflow with an empty analysis.
export function parseChunkAnalysis(content: string): {
	summary: string;
	keyPoints: string[];
	chapters: { title: string; start: number }[];
} {
	const parsed = JSON.parse(extractJsonObject(content)) as {
		summary?: unknown;
		keyPoints?: unknown;
		chapters?: unknown;
	};
	if (typeof parsed.summary !== "string" || !parsed.summary.trim()) {
		throw new Error("AI response did not contain a valid section summary");
	}
	return {
		summary: parsed.summary,
		keyPoints: Array.isArray(parsed.keyPoints)
			? parsed.keyPoints.filter(
					(keyPoint): keyPoint is string => typeof keyPoint === "string",
				)
			: [],
		chapters: Array.isArray(parsed.chapters)
			? parsed.chapters.filter(
					(chapter): chapter is { title: string; start: number } =>
						typeof chapter === "object" &&
						chapter !== null &&
						typeof chapter.start === "number" &&
						chapter.start >= 0 &&
						typeof chapter.title === "string" &&
						chapter.title.trim().length > 0,
				)
			: [],
	};
}

export function parseFinalSummary(content: string): {
	title: string;
	summary: string;
} {
	const parsed = JSON.parse(extractJsonObject(content)) as {
		title?: unknown;
		summary?: unknown;
	};
	if (typeof parsed.title !== "string" || !parsed.title.trim()) {
		throw new Error("AI response did not contain a valid title");
	}
	if (typeof parsed.summary !== "string" || !parsed.summary.trim()) {
		throw new Error("AI response did not contain a valid summary");
	}
	return { title: parsed.title, summary: parsed.summary };
}

export function parseAiResponse(content: string): AiResult {
	const data = JSON.parse(extractJsonObject(content)) as {
		title?: unknown;
		summary?: unknown;
		chapters?: unknown;
	};
	if (typeof data.title !== "string" || !data.title.trim()) {
		throw new Error("AI response did not contain a valid title");
	}
	if (typeof data.summary !== "string" || !data.summary.trim()) {
		throw new Error("AI response did not contain a valid summary");
	}

	const chapters = Array.isArray(data.chapters)
		? data.chapters
				.filter(
					(ch): ch is { start: number; title: string } =>
						typeof ch === "object" &&
						ch !== null &&
						typeof ch.start === "number" &&
						ch.start >= 0 &&
						typeof ch.title === "string" &&
						ch.title.trim().length > 0,
				)
				.sort((a, b) => a.start - b.start)
		: [];

	return {
		title: data.title.trim(),
		summary: data.summary.trim(),
		chapters,
	};
}