Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor: improve AI summary prompt quality

Richie McIlroy87b23e93ace0
diff --git a/apps/web/__tests__/unit/generate-ai-title.test.ts b/apps/web/__tests__/unit/generate-ai-title.test.ts
index b94bbae25..1b87b1af4 100644
--- a/apps/web/__tests__/unit/generate-ai-title.test.ts
+++ b/apps/web/__tests__/unit/generate-ai-title.test.ts
@@ -32,6 +32,7 @@ vi.mock("workflow", () => ({
vi.mock("server-only", () => ({}));
import {
+ getAiContentGuidelines,
getAiLanguageInstruction,
shouldReplaceVideoTitle,
} from "@/workflows/generate-ai";
@@ -135,3 +136,41 @@ describe("getAiLanguageInstruction", () => {
expect(getAiLanguageInstruction("es")).toContain("Spanish");
});
});
+
+describe("getAiContentGuidelines", () => {
+ it("prioritizes subject, intention, and standalone understanding", () => {
+ const { summary } = getAiContentGuidelines(114);
+
+ expect(summary).toContain("understand the video without watching it");
+ expect(summary).toContain("subject and the speaker's intention first");
+ expect(summary).toContain(
+ "outcomes, decisions, action items, and next steps",
+ );
+ expect(summary).toContain("meaning and useful information");
+ expect(summary).toContain("minor UI actions");
+ expect(summary).toContain("never omit information required");
+ expect(summary).toContain("rather than enumerating every utterance");
+ });
+
+ it("scales summary length without padding", () => {
+ expect(getAiContentGuidelines(20).summary).toContain(
+ "no more than 35 words",
+ );
+ expect(getAiContentGuidelines(114).summary).toContain("50-90 words");
+ expect(getAiContentGuidelines(300).summary).toContain("80-150 words");
+ expect(getAiContentGuidelines(1200).summary).toContain("150-250 words");
+ expect(getAiContentGuidelines(3600).summary).toContain("250-400 words");
+ expect(getAiContentGuidelines(114).summary).toContain(
+ "Do not pad the summary",
+ );
+ });
+
+ it("omits chapters for videos shorter than two minutes", () => {
+ expect(getAiContentGuidelines(119).chapters).toContain(
+ 'empty "chapters" array',
+ );
+ expect(getAiContentGuidelines(120).chapters).toContain(
+ "fewest chapters needed",
+ );
+ });
+});
diff --git a/apps/web/workflows/generate-ai.ts b/apps/web/workflows/generate-ai.ts
index efdb5d377..70a456227 100644
--- a/apps/web/workflows/generate-ai.ts
+++ b/apps/web/workflows/generate-ai.ts
@@ -274,6 +274,44 @@ export function getAiLanguageInstruction(
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.
+- Use the speaker's perspective when it is clear, but do not invent intent, outcomes, or actions.
+- Be concise, but never omit information required to understand or act on the video. Do not pad the summary to reach a target length.
+- For example, summarize a short drawing-feature test as "I test the drawing and highlighting tools, including how highlights behave on moving elements" rather than enumerating every utterance and interaction.
+- ${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];
@@ -502,24 +540,27 @@ async function generateSingleChunk(
`[${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 analyzing video content and creating comprehensive summaries.
+ 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 a detailed JSON response:
+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 (detailed summary that covers ALL key points discussed. For meetings: include decisions made, action items, and key discussion points. For tutorials: cover all steps and concepts explained. For presentations: summarize all main arguments and supporting points. Write from 1st person perspective if the speaker is teaching/presenting, e.g. 'In this video, I walk through...'. Make it comprehensive enough that someone could understand the full content without watching.)",
+ "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)}]
}
-Guidelines:
+Summary requirements:
+${contentGuidelines.summary}
+
+Chapter requirements:
+${contentGuidelines.chapters}
+
+Additional requirements:
- ${languageInstruction}
-- Keep JSON property names exactly as shown
-- The summary should be detailed and comprehensive, not a brief overview
-- Capture ALL important topics, not just the main theme
-- For longer content, organize the summary by topic or chronologically
-- Include specific details, names, numbers, and conclusions mentioned
-- Chapters should mark distinct topic changes or sections
+- 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.
@@ -543,24 +584,28 @@ async function generateMultipleChunks(
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, an expert at analyzing video content. 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")}.
+ 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")}.
-Analyze this section thoroughly and provide JSON:
+Extract only the information needed to understand this section's contribution to the full video and provide JSON:
{
- "summary": "string (detailed summary of this section - capture ALL key points, topics discussed, decisions made, or concepts explained. Include specific details like names, numbers, action items, and conclusions. This should be 3-6 sentences minimum.)",
- "keyPoints": ["string (specific key point or takeaway)", ...],
+ "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)}]
}
-${languageInstruction}
-Keep JSON property names exactly as shown.
+- 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.
-Be thorough - this summary will be combined with other sections to create a comprehensive overview.
Return ONLY valid JSON without any markdown formatting or code blocks.
Transcript section:
${chunk.text}`;
@@ -601,9 +646,9 @@ ${chunk.text}`;
})
.join("\n\n");
- const finalPrompt = `You are Cap AI, an expert at synthesizing information into comprehensive, well-organized summaries.
+ const finalPrompt = `You are Cap AI, an expert at turning video analyses into useful, concise summaries.
-Based on these detailed section analyses of a video, create a thorough final summary that captures EVERYTHING important.
+Using these section analyses, create a standalone final summary that lets someone understand the video without watching it.
Section analyses:
${sectionDetails}
@@ -613,12 +658,15 @@ ${allKeyPoints.length > 0 ? `All key points identified:\n${allKeyPoints.map((p,
Provide JSON in the following format:
{
"title": "string (concise but descriptive title that captures the main topic/purpose)",
- "summary": "string (COMPREHENSIVE summary that covers the entire video thoroughly. This should be detailed enough that someone could understand all the important content without watching. Include: main topics covered, key decisions or conclusions, important details mentioned, action items if any. Organize it logically - for meetings use topics/agenda items, for tutorials use steps/concepts, for presentations use main arguments. Write from 1st person perspective if appropriate. This should be several paragraphs for longer content.)"
+ "summary": "string (standalone summary of the subject, intention, essential information, outcome, and next steps)"
}
-The summary must be detailed and comprehensive - not a brief overview. Capture all the important information from every section.
-${languageInstruction}
-Keep JSON property names exactly as shown.
+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.`;
const finalContent = await callAiApi(finalPrompt, groqClient);