This commit adds support for reusing existing character images from exis

This commit is contained in:
Riccardo Giorato
2025-12-23 13:43:35 +01:00
parent fbcac53de1
commit ff25be7c85
17 changed files with 895 additions and 155 deletions
+57 -2
View File
@@ -1,5 +1,6 @@
import { type NextRequest, NextResponse } from "next/server";
import Together from "together-ai";
import { updatePage, createStory, createPage, getNextPageNumber } from "@/lib/db-actions";
const FIXED_DIMENSIONS = { width: 864, height: 1184 };
@@ -91,6 +92,7 @@ const STYLE_DESCRIPTIONS: Record<string, string> = {
export async function POST(request: NextRequest) {
try {
const {
storyId,
prompt,
apiKey,
style = "noir",
@@ -99,7 +101,7 @@ export async function POST(request: NextRequest) {
previousContext = "",
} = await request.json();
console.log("Received character image URLs:", characterImages);
console.log("Received request:", { storyId, prompt: prompt?.substring(0, 50), characterImagesCount: characterImages.length });
if (!prompt || !apiKey) {
return NextResponse.json(
@@ -108,6 +110,41 @@ export async function POST(request: NextRequest) {
);
}
let page;
let story;
if (storyId) {
// Create next page for existing story
console.log("Creating page for existing story:", storyId);
const nextPageNumber = await getNextPageNumber(storyId);
page = await createPage({
storyId,
pageNumber: nextPageNumber,
prompt,
characterImageUrls: characterImages,
style,
});
console.log("Page created:", page.id);
} else {
// Create new story and first page
console.log("Creating new story");
story = await createStory({
title: prompt.length > 50 ? prompt.substring(0, 50) + "..." : prompt,
description: undefined,
userId: undefined,
});
console.log("Story created:", story.id);
page = await createPage({
storyId: story.id,
pageNumber: 1,
prompt,
characterImageUrls: characterImages,
style,
});
console.log("First page created:", page.id);
}
const dimensions = FIXED_DIMENSIONS;
const styleDesc = STYLE_DESCRIPTIONS[style] || STYLE_DESCRIPTIONS.noir;
@@ -221,7 +258,25 @@ COMPOSITION:
);
}
return NextResponse.json({ imageUrl: response.data[0].url });
const imageUrl = response.data[0].url;
// Update page in database
try {
await updatePage(page.id, imageUrl);
console.log("Page updated with image:", page.id);
} catch (dbError) {
console.error("Error updating page in database:", dbError);
return NextResponse.json(
{ error: "Failed to save generated image" },
{ status: 500 }
);
}
const responseData = storyId
? { imageUrl, pageId: page.id, pageNumber: page.pageNumber }
: { imageUrl, storyId: story!.id, storySlug: story!.slug, pageId: page.id, pageNumber: page.pageNumber };
return NextResponse.json(responseData);
} catch (error) {
console.error("Error in generate-comic API:", error);
return NextResponse.json(
+48
View File
@@ -0,0 +1,48 @@
import { type NextRequest, NextResponse } from "next/server";
import { getStoryWithPagesBySlug } from "@/lib/db-actions";
import { db } from "@/lib/db";
import { stories } from "@/lib/schema";
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ storySlug: string }> }
) {
try {
const { storySlug: slug } = await params;
console.log("API: Fetching story with slug:", slug);
// Special case: if slug is "all", return all stories for debugging
if (slug === "all") {
const allStories = await db.select().from(stories);
return NextResponse.json({
message: "All stories",
stories: allStories.map(s => ({ id: s.id, slug: s.slug, title: s.title }))
});
}
if (!slug) {
return NextResponse.json(
{ error: "Story slug is required" },
{ status: 400 }
);
}
const result = await getStoryWithPagesBySlug(slug);
console.log("API: Result found:", !!result);
if (!result) {
return NextResponse.json(
{ error: "Story not found" },
{ status: 404 }
);
}
return NextResponse.json(result);
} catch (error) {
console.error("Error fetching story:", error);
return NextResponse.json(
{ error: "Failed to fetch story" },
{ status: 500 }
);
}
}