Skip to content

🎥 Make videos programmatically with React

fix(tp2mg): preserve attached images when retrying after error

ASchwad4acc3e53de80
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.tsx
index ada67fe5a5..02b8bb032f 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
@@ -63,6 +63,7 @@ function GeneratePageContent() {
markManualEdit,
getFullContext,
getPreviouslyUsedSkills,
+ getLastUserAttachedImages,
setPendingMessage,
clearPendingMessage,
isFirstGeneration,
@@ -99,11 +100,14 @@ function GeneratePageContent() {
onTriggerCorrection: useCallback((correctionPrompt: string, context: ErrorCorrectionContext) => {
setErrorCorrection(context);
setPrompt(correctionPrompt);
+ // Get attached images from the last user message to include in retry
+ const lastImages = getLastUserAttachedImages();
setTimeout(() => {
// Use silent mode to avoid showing retry as a user message
- chatSidebarRef.current?.triggerGeneration({ silent: true });
+ // Include images from the last user message so image-based requests can be retried
+ chatSidebarRef.current?.triggerGeneration({ silent: true, attachedImages: lastImages });
}, 100);
- }, []),
+ }, [getLastUserAttachedImages]),
onAddErrorMessage: addErrorMessage,
onClearGenerationError: useCallback(() => setGenerationError(null), []),
onClearErrorCorrection: useCallback(() => setErrorCorrection(null), []),
diff --git a/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts b/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts
index 94339acbf0..e71304dd24 100644
--- a/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts
+++ b/packages/template-prompt-to-motion-graphics/src/hooks/useConversationState.ts
@@ -141,6 +141,17 @@ export function useConversationState() {
return Array.from(allSkills);
}, [state.messages]);
+ // Get attached images from the last user message (for retry scenarios)
+ const getLastUserAttachedImages = useCallback((): string[] | undefined => {
+ for (let i = state.messages.length - 1; i >= 0; i--) {
+ const msg = state.messages[i];
+ if (msg.role === "user" && msg.attachedImages && msg.attachedImages.length > 0) {
+ return msg.attachedImages;
+ }
+ }
+ return undefined;
+ }, [state.messages]);
+
return {
...state,
addUserMessage,
@@ -150,6 +161,7 @@ export function useConversationState() {
clearConversation,
getFullContext,
getPreviouslyUsedSkills,
+ getLastUserAttachedImages,
setPendingMessage,
clearPendingMessage,
isFirstGeneration: state.messages.length === 0,