Refactor add-page API to use consistent previous page image logic and im
This commit is contained in:
+24
-18
@@ -36,7 +36,12 @@ export async function POST(request: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { storyId, pageId, prompt, characterImages = [] } = await request.json();
|
const {
|
||||||
|
storyId,
|
||||||
|
pageId,
|
||||||
|
prompt,
|
||||||
|
characterImages = [],
|
||||||
|
} = await request.json();
|
||||||
|
|
||||||
if (!storyId || !prompt) {
|
if (!storyId || !prompt) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@@ -59,7 +64,7 @@ export async function POST(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply rate limiting for free tier
|
// Apply rate limiting for free tier
|
||||||
const hasApiKey = request.headers.get('x-api-key');
|
const hasApiKey = request.headers.get("x-api-key");
|
||||||
if (!hasApiKey) {
|
if (!hasApiKey) {
|
||||||
const { success, reset } = await freeTierRateLimit.limit(userId);
|
const { success, reset } = await freeTierRateLimit.limit(userId);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
@@ -90,7 +95,7 @@ export async function POST(request: NextRequest) {
|
|||||||
return NextResponse.json({ error: "Story not found" }, { status: 404 });
|
return NextResponse.json({ error: "Story not found" }, { status: 404 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingPage = storyData.pages.find(p => p.id === pageId);
|
const existingPage = storyData.pages.find((p) => p.id === pageId);
|
||||||
if (!existingPage) {
|
if (!existingPage) {
|
||||||
return NextResponse.json({ error: "Page not found" }, { status: 404 });
|
return NextResponse.json({ error: "Page not found" }, { status: 404 });
|
||||||
}
|
}
|
||||||
@@ -115,22 +120,16 @@ export async function POST(request: NextRequest) {
|
|||||||
|
|
||||||
// Get previous page image for style consistency (unless it's page 1)
|
// Get previous page image for style consistency (unless it's page 1)
|
||||||
if (pageNumber > 1) {
|
if (pageNumber > 1) {
|
||||||
if (isRedraw) {
|
// Always use the previous page's image, regardless of new page or redraw
|
||||||
// For redraw, get all pages and find the previous page's image
|
|
||||||
const storyData = await getStoryWithPagesBySlug(storyId);
|
const storyData = await getStoryWithPagesBySlug(storyId);
|
||||||
if (storyData) {
|
if (storyData) {
|
||||||
const previousPage = storyData.pages.find(p => p.pageNumber === pageNumber - 1);
|
const previousPage = storyData.pages.find(
|
||||||
|
(p) => p.pageNumber === pageNumber - 1
|
||||||
|
);
|
||||||
if (previousPage?.generatedImageUrl) {
|
if (previousPage?.generatedImageUrl) {
|
||||||
referenceImages.push(previousPage.generatedImageUrl);
|
referenceImages.push(previousPage.generatedImageUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// For new page, use the last page image
|
|
||||||
const lastPageImage = await getLastPageImage(story.id);
|
|
||||||
if (lastPageImage) {
|
|
||||||
referenceImages.push(lastPageImage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use only the character images sent from the frontend (user's selection)
|
// Use only the character images sent from the frontend (user's selection)
|
||||||
@@ -138,9 +137,14 @@ export async function POST(request: NextRequest) {
|
|||||||
referenceImages.push(...characterImages);
|
referenceImages.push(...characterImages);
|
||||||
|
|
||||||
// Build the prompt with continuation context
|
// Build the prompt with continuation context
|
||||||
const previousPages = pages.map(p => ({
|
// For redraw, only include pages up to the current page being redrawn
|
||||||
|
// For new page, include all existing pages
|
||||||
|
const relevantPages = isRedraw
|
||||||
|
? pages.filter((p) => p.pageNumber < pageNumber)
|
||||||
|
: pages;
|
||||||
|
|
||||||
|
const previousPages = relevantPages.map((p) => ({
|
||||||
prompt: p.prompt,
|
prompt: p.prompt,
|
||||||
characterImages: p.characterImageUrls,
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const fullPrompt = buildComicPrompt({
|
const fullPrompt = buildComicPrompt({
|
||||||
@@ -151,7 +155,9 @@ export async function POST(request: NextRequest) {
|
|||||||
previousPages,
|
previousPages,
|
||||||
});
|
});
|
||||||
|
|
||||||
const client = new Together({ apiKey: process.env.TOGETHER_API_KEY_DEFAULT });
|
const client = new Together({
|
||||||
|
apiKey: process.env.TOGETHER_API_KEY_DEFAULT,
|
||||||
|
});
|
||||||
|
|
||||||
let response;
|
let response;
|
||||||
try {
|
try {
|
||||||
@@ -160,8 +166,8 @@ export async function POST(request: NextRequest) {
|
|||||||
prompt: fullPrompt,
|
prompt: fullPrompt,
|
||||||
width: dimensions.width,
|
width: dimensions.width,
|
||||||
height: dimensions.height,
|
height: dimensions.height,
|
||||||
temperature: 0.1,
|
reference_images:
|
||||||
reference_images: referenceImages.length > 0 ? referenceImages : undefined,
|
referenceImages.length > 0 ? referenceImages : undefined,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Together AI API error:", error);
|
console.error("Together AI API error:", error);
|
||||||
|
|||||||
@@ -13,10 +13,6 @@ export async function GET(
|
|||||||
const authResult = await auth();
|
const authResult = await auth();
|
||||||
const { userId } = authResult;
|
const { userId } = authResult;
|
||||||
|
|
||||||
console.log('API: auth result:', authResult);
|
|
||||||
console.log('API: userId type:', typeof userId, 'value:', userId);
|
|
||||||
console.log('API: timestamp:', new Date().toISOString());
|
|
||||||
|
|
||||||
const { storySlug: slug } = await params;
|
const { storySlug: slug } = await params;
|
||||||
|
|
||||||
// Special case: if slug is "all", return user's stories for debugging
|
// Special case: if slug is "all", return user's stories for debugging
|
||||||
@@ -27,10 +23,17 @@ export async function GET(
|
|||||||
{ status: 401 }
|
{ status: 401 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const userStories = await db.select().from(stories).where(eq(stories.userId, userId));
|
const userStories = await db
|
||||||
|
.select()
|
||||||
|
.from(stories)
|
||||||
|
.where(eq(stories.userId, userId));
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
message: "User stories",
|
message: "User stories",
|
||||||
stories: userStories.map(s => ({ id: s.id, slug: s.slug, title: s.title }))
|
stories: userStories.map((s) => ({
|
||||||
|
id: s.id,
|
||||||
|
slug: s.slug,
|
||||||
|
title: s.title,
|
||||||
|
})),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,10 +47,7 @@ export async function GET(
|
|||||||
const result = await getStoryWithPagesBySlug(slug);
|
const result = await getStoryWithPagesBySlug(slug);
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return NextResponse.json(
|
return NextResponse.json({ error: "Story not found" }, { status: 404 });
|
||||||
{ error: "Story not found" },
|
|
||||||
{ status: 404 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the story belongs to the authenticated user
|
// Check if the story belongs to the authenticated user
|
||||||
|
|||||||
+1
-2
@@ -17,7 +17,6 @@ export function buildComicPrompt({
|
|||||||
isAddPage?: boolean;
|
isAddPage?: boolean;
|
||||||
previousPages?: Array<{
|
previousPages?: Array<{
|
||||||
prompt: string;
|
prompt: string;
|
||||||
characterImages: string[];
|
|
||||||
}>;
|
}>;
|
||||||
}): string {
|
}): string {
|
||||||
const styleInfo = COMIC_STYLES.find((s) => s.id === style);
|
const styleInfo = COMIC_STYLES.find((s) => s.id === style);
|
||||||
@@ -31,7 +30,7 @@ export function buildComicPrompt({
|
|||||||
if (isAddPage && previousPages.length > 0) {
|
if (isAddPage && previousPages.length > 0) {
|
||||||
const storyHistory = previousPages
|
const storyHistory = previousPages
|
||||||
.map((page, index) => `Page ${index + 1}: ${page.prompt}`)
|
.map((page, index) => `Page ${index + 1}: ${page.prompt}`)
|
||||||
.join('\n');
|
.join("\n");
|
||||||
|
|
||||||
continuationContext = `\nSTORY CONTINUATION CONTEXT:\nThis is a continuation of an existing comic story. Here are the previous pages:\n${storyHistory}\n\nThe new page should naturally continue this story. Maintain the same characters, setting, and narrative style. Reference previous events and build upon them.\n`;
|
continuationContext = `\nSTORY CONTINUATION CONTEXT:\nThis is a continuation of an existing comic story. Here are the previous pages:\n${storyHistory}\n\nThe new page should naturally continue this story. Maintain the same characters, setting, and narrative style. Reference previous events and build upon them.\n`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user