🎥 Make videos programmatically with React
Add silent retry mode and track previously used skills
ASchwad8d3bc27e6f1a
diff --git a/packages/template-prompt-to-motion-graphics/src/app/api/generate/route.ts b/packages/template-prompt-to-motion-graphics/src/app/api/generate/route.tsindex 55c95b170c..e4fc9a853b 100644--- a/packages/template-prompt-to-motion-graphics/src/app/api/generate/route.ts+++ b/packages/template-prompt-to-motion-graphics/src/app/api/generate/route.ts@@ -263,6 +263,8 @@ interface GenerateRequest {hasManualEdits?: boolean;/** Error correction context for self-healing loops */errorCorrection?: ErrorCorrectionContext;+ /** Skills already used in this conversation (to avoid redundant skill content) */+ previouslyUsedSkills?: string[];}interface GenerateResponse {@@ -285,6 +287,7 @@ export async function POST(req: Request) {isFollowUp = false,hasManualEdits = false,errorCorrection,+ previouslyUsedSkills = [],}: GenerateRequest = await req.json();const apiKey = process.env.OPENAI_API_KEY;@@ -350,8 +353,19 @@ export async function POST(req: Request) {console.error("Skill detection error:", skillError);}- // Load skill-specific content and enhance the system prompt- const skillContent = getCombinedSkillContent(detectedSkills);+ // Filter out skills that were already used in the conversation to avoid redundant context+ const newSkills = detectedSkills.filter(+ (skill) => !previouslyUsedSkills.includes(skill),+ );+ if (previouslyUsedSkills.length > 0 && newSkills.length < detectedSkills.length) {+ console.log(+ `Skipping ${detectedSkills.length - newSkills.length} previously used skills:`,+ detectedSkills.filter((s) => previouslyUsedSkills.includes(s)),+ );+ }++ // Load skill-specific content only for NEW skills (previously used skills are already in context)+ const skillContent = getCombinedSkillContent(newSkills as SkillName[]);const enhancedSystemPrompt = skillContent? `${SYSTEM_PROMPT}\n\n## SKILL-SPECIFIC GUIDANCE\n${skillContent}`: SYSTEM_PROMPT;diff --git a/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx b/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsxindex 8bdc21b619..4f759aa0a0 100644--- a/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx+++ b/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx@@ -60,6 +60,7 @@ function GeneratePageContent() {addErrorMessage,markManualEdit,getRecentContext,+ getPreviouslyUsedSkills,isFirstGeneration,} = useConversationState();@@ -89,7 +90,8 @@ function GeneratePageContent() {setErrorCorrection(context);setPrompt(correctionPrompt);setTimeout(() => {- chatHistoryRef.current?.triggerGeneration();+ // Use silent mode to avoid showing retry as a user message+ chatHistoryRef.current?.triggerGeneration({ silent: true });}, 100);}, []),onAddErrorMessage: addErrorMessage,@@ -213,6 +215,7 @@ function GeneratePageContent() {onPromptChange={setPrompt}currentCode={code}conversationHistory={getRecentContext(3)}+ previouslyUsedSkills={getPreviouslyUsedSkills()}isFollowUp={!isFirstGeneration}onMessageSent={handleMessageSent}onGenerationComplete={handleGenerationComplete}diff --git a/packages/template-prompt-to-motion-graphics/src/components/ChatHistory/ChatHistory.tsx b/packages/template-prompt-to-motion-graphics/src/components/ChatHistory/ChatHistory.tsxindex 9614bea256..88bd79f547 100644--- a/packages/template-prompt-to-motion-graphics/src/components/ChatHistory/ChatHistory.tsx+++ b/packages/template-prompt-to-motion-graphics/src/components/ChatHistory/ChatHistory.tsx@@ -44,7 +44,7 @@ import {} from "@/helpers/sanitize-response";export interface ChatHistoryRef {- triggerGeneration: () => void;+ triggerGeneration: (options?: { silent?: boolean }) => void;}interface ChatHistoryProps {@@ -61,6 +61,8 @@ interface ChatHistoryProps {onPromptChange?: (prompt: string) => void;currentCode?: string;conversationHistory?: ConversationContextMessage[];+ /** Skills already used in this conversation (to avoid redundant skill content) */+ previouslyUsedSkills?: string[];isFollowUp?: boolean;onMessageSent?: (prompt: string) => void;onGenerationComplete?: (@@ -91,6 +93,7 @@ export const ChatHistory = forwardRef<ChatHistoryRef, ChatHistoryProps>(onPromptChange,currentCode,conversationHistory = [],+ previouslyUsedSkills = [],isFollowUp = false,onMessageSent,onGenerationComplete,@@ -118,13 +121,16 @@ export const ChatHistory = forwardRef<ChatHistoryRef, ChatHistoryProps>(triggerGeneration: runGeneration,}));- const runGeneration = async () => {+ const runGeneration = async (options?: { silent?: boolean }) => {if (!prompt.trim() || isLoading) return;setIsLoading(true);onStreamingChange?.(true);onStreamPhaseChange?.("reasoning");- onMessageSent?.(prompt);+ // Only add user message if not a silent retry+ if (!options?.silent) {+ onMessageSent?.(prompt);+ }setPrompt(""); // Clear input immediately after sendingtry {@@ -136,6 +142,7 @@ export const ChatHistory = forwardRef<ChatHistoryRef, ChatHistoryProps>(model,currentCode: isFollowUp ? currentCode : undefined,conversationHistory: isFollowUp ? conversationHistory : [],+ previouslyUsedSkills: isFollowUp ? previouslyUsedSkills : [],isFollowUp,hasManualEdits,errorCorrection,diff --git a/packages/template-prompt-to-motion-graphics/src/hooks/useAutoCorrection.ts b/packages/template-prompt-to-motion-graphics/src/hooks/useAutoCorrection.tsindex 43b579ca97..8071f87022 100644--- a/packages/template-prompt-to-motion-graphics/src/hooks/useAutoCorrection.ts+++ b/packages/template-prompt-to-motion-graphics/src/hooks/useAutoCorrection.ts@@ -115,7 +115,7 @@ export function useAutoCorrection({);onClearGenerationError();- onTriggerCorrection("Please try again", {+ onTriggerCorrection("Retry the previous request", {error: generationError.message,attemptNumber: nextAttempt,maxAttempts,diff --git a/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts b/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.tsindex a6da496a3f..87e8f71962 100644--- a/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts+++ b/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts@@ -105,6 +105,17 @@ export function useConversationState() {[state.messages],);+ // Get all skills that have been used in the conversation (to avoid redundant skill content)+ const getPreviouslyUsedSkills = useCallback((): string[] => {+ const allSkills = new Set<string>();+ state.messages.forEach((m) => {+ if (m.role === "assistant" && m.metadata?.skills) {+ m.metadata.skills.forEach((skill) => allSkills.add(skill));+ }+ });+ return Array.from(allSkills);+ }, [state.messages]);+return {...state,addUserMessage,@@ -113,6 +124,7 @@ export function useConversationState() {markManualEdit,clearConversation,getRecentContext,+ getPreviouslyUsedSkills,isFirstGeneration: state.messages.length === 0,};}