Add reference images from previous page and story characters for consist

This commit is contained in:
Riccardo Giorato
2025-12-26 18:08:21 +01:00
parent 7fd5367577
commit 85f18f0ed1
6 changed files with 278 additions and 219 deletions
+22 -3
View File
@@ -7,6 +7,8 @@ import {
createPage,
getNextPageNumber,
getStoryById,
getLastPageImage,
getStoryCharacterImages,
} from "@/lib/db-actions";
import { freeTierRateLimit } from "@/lib/rate-limit";
import { COMIC_STYLES } from "@/lib/constants";
@@ -89,9 +91,11 @@ export async function POST(request: NextRequest) {
let page;
let story;
let referenceImages: string[] = [];
if (storyId) {
const story = await getStoryById(storyId);
// Continuation: get previous page image and story character images
story = await getStoryById(storyId);
if (!story) {
return NextResponse.json({ error: "Story not found" }, { status: 404 });
}
@@ -103,7 +107,20 @@ export async function POST(request: NextRequest) {
prompt,
characterImageUrls: characterImages,
});
// Get previous page image for style consistency (unless it's page 1)
if (nextPageNumber > 1) {
const lastPageImage = await getLastPageImage(storyId);
if (lastPageImage) {
referenceImages.push(lastPageImage);
}
}
// Get story character images (up to 2 most recent)
const storyCharacterImages = await getStoryCharacterImages(storyId);
referenceImages.push(...storyCharacterImages.slice(-2)); // Take last 2
} else {
// New story: no previous page reference
story = await createStory({
title: prompt.length > 50 ? prompt.substring(0, 50) + "..." : prompt,
description: undefined,
@@ -119,6 +136,9 @@ export async function POST(request: NextRequest) {
});
}
// Add current character images to references
referenceImages.push(...characterImages);
const dimensions = FIXED_DIMENSIONS;
const fullPrompt = buildComicPrompt({
@@ -139,8 +159,7 @@ export async function POST(request: NextRequest) {
width: dimensions.width,
height: dimensions.height,
temperature: 0.1, // Lower temperature for more consistent face matching
reference_images:
characterImages.length > 0 ? characterImages : undefined,
reference_images: referenceImages.length > 0 ? referenceImages : undefined,
});
} catch (error) {
console.error("Together AI API error:", error);