🎥 Make videos programmatically with React
Include failed edit details in error correction context
ASchwad63b895532d86
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 c6fe8af25a..ca37f289ad 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@@ -198,6 +198,7 @@ function applyEdits(result: string;error?: string;enrichedEdits?: EditOperation[];+ failedEdit?: EditOperation;} {let result = code;const enrichedEdits: EditOperation[] = [];@@ -212,6 +213,7 @@ function applyEdits(success: false,result: code,error: `Edit ${i + 1} failed: Could not find the specified text`,+ failedEdit: edit,};}@@ -222,6 +224,7 @@ function applyEdits(success: false,result: code,error: `Edit ${i + 1} failed: Found ${matches} matches. The edit target is ambiguous.`,+ failedEdit: edit,};}@@ -252,6 +255,11 @@ interface ErrorCorrectionContext {error: string;attemptNumber: number;maxAttempts: number;+ failedEdit?: {+ description: string;+ old_string: string;+ new_string: string;+ };}interface GenerateRequest {@@ -391,7 +399,32 @@ export async function POST(req: Request) {// Error correction context for self-healinglet errorCorrectionNotice = "";if (errorCorrection) {- errorCorrectionNotice = `+ const failedEditInfo = errorCorrection.failedEdit+ ? `++The previous edit attempt failed. Here's what was tried:+- Description: ${errorCorrection.failedEdit.description}+- Tried to find: \`${errorCorrection.failedEdit.old_string}\`+- Wanted to replace with: \`${errorCorrection.failedEdit.new_string}\`++The old_string was either not found or matched multiple locations. You MUST include more surrounding context to make the match unique.`+ : "";++ const isEditFailure = errorCorrection.error.includes("Edit") && errorCorrection.error.includes("failed");++ if (isEditFailure) {+ errorCorrectionNotice = `++## EDIT FAILED (ATTEMPT ${errorCorrection.attemptNumber}/${errorCorrection.maxAttempts})+${errorCorrection.error}+${failedEditInfo}++CRITICAL: Your previous edit target was ambiguous or not found. To fix this:+1. Include MORE surrounding code context in old_string to make it unique+2. Make sure old_string matches the code EXACTLY (including whitespace)+3. If the code structure changed, look at the current code carefully`;+ } else {+ errorCorrectionNotice = `## COMPILATION ERROR (ATTEMPT ${errorCorrection.attemptNumber}/${errorCorrection.maxAttempts})The previous code failed to compile with this error:@@ -406,6 +439,7 @@ CRITICAL: Fix this compilation error. Common issues include:- TypeScript type errorsFocus ONLY on fixing the error. Do not make other changes.`;+ }}const editPrompt = `## CURRENT CODE:@@ -446,11 +480,12 @@ Analyze the request and decide: use targeted edits (type: "edit") for small chan// Apply the edits to the current codeconst result = applyEdits(currentCode, response.edits);if (!result.success) {- // If edits fail, return error+ // If edits fail, return error with the failed edit detailsreturn new Response(JSON.stringify({error: result.error,type: "edit_failed",+ failedEdit: result.failedEdit,}),{ status: 400, headers: { "Content-Type": "application/json" } });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 be0f38ce15..1335bc1fce 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@@ -16,6 +16,7 @@ import { useAutoCorrection } from "../../hooks/useAutoCorrection";import type {AssistantMetadata,ErrorCorrectionContext,+ EditOperation,} from "../../types/conversation";const MAX_CORRECTION_ATTEMPTS = 3;@@ -42,6 +43,7 @@ function GeneratePageContent() {const [generationError, setGenerationError] = useState<{message: string;type: GenerationErrorType;+ failedEdit?: EditOperation;} | null>(null);// Self-correction state@@ -187,8 +189,8 @@ function GeneratePageContent() {}, []);const handleError = useCallback(- (message: string, type: GenerationErrorType) => {- setGenerationError({ message, type });+ (message: string, type: GenerationErrorType, failedEdit?: EditOperation) => {+ setGenerationError({ message, type, failedEdit });},[],);diff --git a/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatSidebar.tsx b/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatSidebar.tsxindex b76d151d37..233279273c 100644--- a/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatSidebar.tsx+++ b/packages/template-prompt-to-motion-graphics/src/components/ChatSidebar/ChatSidebar.tsx@@ -28,6 +28,7 @@ import type {ConversationContextMessage,AssistantMetadata,ErrorCorrectionContext,+ EditOperation,} from "@/types/conversation";import {type StreamPhase,@@ -55,7 +56,7 @@ interface ChatSidebarProps {onCodeGenerated?: (code: string) => void;onStreamingChange?: (isStreaming: boolean) => void;onStreamPhaseChange?: (phase: StreamPhase) => void;- onError?: (error: string, type: GenerationErrorType) => void;+ onError?: (error: string, type: GenerationErrorType, failedEdit?: EditOperation) => void;prompt: string;onPromptChange: (prompt: string) => void;currentCode?: string;diff --git a/packages/template-prompt-to-motion-graphics/src/hooks/useAutoCorrection.ts b/packages/template-prompt-to-motion-graphics/src/hooks/useAutoCorrection.tsindex 8071f87022..03dbabad4c 100644--- a/packages/template-prompt-to-motion-graphics/src/hooks/useAutoCorrection.ts+++ b/packages/template-prompt-to-motion-graphics/src/hooks/useAutoCorrection.ts@@ -1,12 +1,12 @@import { useEffect, useRef, useCallback } from "react";-import type { ErrorCorrectionContext } from "@/types/conversation";+import type { ErrorCorrectionContext, EditOperation } from "@/types/conversation";interface AutoCorrectionConfig {maxAttempts: number;/** Compilation error from useAnimationState */compilationError: string | null;/** Generation/API error */- generationError: { message: string; type: string } | null;+ generationError: { message: string; type: string; failedEdit?: EditOperation } | null;/** Whether code is currently being generated */isStreaming: boolean;/** Whether code is currently being compiled */@@ -19,7 +19,7 @@ interface AutoCorrectionConfig {errorCorrection: ErrorCorrectionContext | null;/** Callbacks */onTriggerCorrection: (prompt: string, errorContext: ErrorCorrectionContext) => void;- onAddErrorMessage: (message: string, type: "edit_failed" | "api" | "validation") => void;+ onAddErrorMessage: (message: string, type: "edit_failed" | "api" | "validation", failedEdit?: EditOperation) => void;onClearGenerationError: () => void;onClearErrorCorrection: () => void;}@@ -119,6 +119,7 @@ export function useAutoCorrection({error: generationError.message,attemptNumber: nextAttempt,maxAttempts,+ failedEdit: generationError.failedEdit,});}}, [diff --git a/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts b/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.tsindex 6e61c4befd..c4ec2e5c46 100644--- a/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts+++ b/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts@@ -4,6 +4,7 @@ import type {ConversationState,ConversationContextMessage,AssistantMetadata,+ EditOperation,} from "@/types/conversation";export function useConversationState() {@@ -54,13 +55,18 @@ export function useConversationState() {);const addErrorMessage = useCallback(- (content: string, errorType: "edit_failed" | "api" | "validation") => {+ (+ content: string,+ errorType: "edit_failed" | "api" | "validation",+ failedEdit?: EditOperation,+ ) => {const message: ConversationMessage = {id: `error-${Date.now()}`,role: "error",content,timestamp: Date.now(),errorType,+ failedEdit,};setState((prev) => ({...prev,diff --git a/packages/template-prompt-to-motion-graphics/src/hooks/useGenerationApi.ts b/packages/template-prompt-to-motion-graphics/src/hooks/useGenerationApi.tsindex 9175164a3b..c58e04403b 100644--- a/packages/template-prompt-to-motion-graphics/src/hooks/useGenerationApi.ts+++ b/packages/template-prompt-to-motion-graphics/src/hooks/useGenerationApi.ts@@ -11,11 +11,17 @@ import {stripMarkdownFences,} from "@/helpers/sanitize-response";+interface FailedEditInfo {+ description: string;+ old_string: string;+ new_string: string;+}+interface GenerationCallbacks {onCodeGenerated?: (code: string) => void;onStreamingChange?: (isStreaming: boolean) => void;onStreamPhaseChange?: (phase: StreamPhase) => void;- onError?: (error: string, type: GenerationErrorType) => void;+ onError?: (error: string, type: GenerationErrorType, failedEdit?: FailedEditInfo) => void;onMessageSent?: (prompt: string) => void;onGenerationComplete?: (code: string,@@ -25,6 +31,7 @@ interface GenerationCallbacks {onErrorMessage?: (message: string,errorType: "edit_failed" | "api" | "validation",+ failedEdit?: FailedEditInfo,) => void;onPendingMessage?: (skills?: string[]) => void;onClearPendingMessage?: () => void;@@ -114,8 +121,8 @@ export function useGenerationApi(): UseGenerationApiReturn {const errorMessage =errorData.error || `API error: ${response.status}`;if (errorData.type === "edit_failed") {- onError?.(errorMessage, "validation");- onErrorMessage?.(errorMessage, "edit_failed");+ onError?.(errorMessage, "validation", errorData.failedEdit);+ onErrorMessage?.(errorMessage, "edit_failed", errorData.failedEdit);return;}if (errorData.type === "validation") {diff --git a/packages/template-prompt-to-motion-graphics/src/types/conversation.ts b/packages/template-prompt-to-motion-graphics/src/types/conversation.tsindex a49ca909ab..8dfc3cc421 100644--- a/packages/template-prompt-to-motion-graphics/src/types/conversation.ts+++ b/packages/template-prompt-to-motion-graphics/src/types/conversation.ts@@ -27,6 +27,8 @@ export interface ConversationMessage {metadata?: AssistantMetadata;/** For error messages, store the error type */errorType?: "edit_failed" | "api" | "validation";+ /** For edit_failed errors, store the failed edit operation */+ failedEdit?: EditOperation;}export interface ConversationState {@@ -54,4 +56,6 @@ export interface ErrorCorrectionContext {attemptNumber: number;/** Maximum correction attempts allowed */maxAttempts: number;+ /** The edit operation that failed (for edit_failed errors) */+ failedEdit?: EditOperation;}