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
+38 -5
View File
@@ -82,13 +82,46 @@ export async function getStoryWithPagesBySlug(slug: string): Promise<{ story: St
}
export async function getStoryCharacterImages(storyId: string): Promise<string[]> {
const storyPages = await db.select({ characterImageUrls: pages.characterImageUrls })
const storyPages = await db.select({
characterImageUrls: pages.characterImageUrls,
pageNumber: pages.pageNumber
})
.from(pages)
.where(eq(pages.storyId, storyId));
.where(eq(pages.storyId, storyId))
.orderBy(pages.pageNumber);
// Flatten all character URLs from all pages and remove duplicates
const allUrls = storyPages.flatMap(page => page.characterImageUrls);
return [...new Set(allUrls)]; // Remove duplicates
// Flatten all character URLs from all pages, keeping order by page number
const allUrls: string[] = [];
const seenUrls = new Set<string>();
for (const page of storyPages) {
for (const url of page.characterImageUrls) {
if (!seenUrls.has(url)) {
seenUrls.add(url);
allUrls.push(url);
}
}
}
return allUrls;
}
export async function getLastPageImage(storyId: string): Promise<string | null> {
const allPages = await db.select({ generatedImageUrl: pages.generatedImageUrl, pageNumber: pages.pageNumber })
.from(pages)
.where(eq(pages.storyId, storyId))
.orderBy(pages.pageNumber);
if (allPages.length === 0) return null;
// Find the last page that has a generated image
for (let i = allPages.length - 1; i >= 0; i--) {
if (allPages[i].generatedImageUrl) {
return allPages[i].generatedImageUrl;
}
}
return null;
}
export async function getNextPageNumber(storyId: string): Promise<number> {