diff --git a/app/api/add-page/route.ts b/app/api/add-page/route.ts index 08508ed..f79a6d9 100644 --- a/app/api/add-page/route.ts +++ b/app/api/add-page/route.ts @@ -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) { return NextResponse.json( @@ -59,7 +64,7 @@ export async function POST(request: NextRequest) { } // Apply rate limiting for free tier - const hasApiKey = request.headers.get('x-api-key'); + const hasApiKey = request.headers.get("x-api-key"); if (!hasApiKey) { const { success, reset } = await freeTierRateLimit.limit(userId); if (!success) { @@ -90,7 +95,7 @@ export async function POST(request: NextRequest) { 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) { return NextResponse.json({ error: "Page not found" }, { status: 404 }); } @@ -115,20 +120,14 @@ export async function POST(request: NextRequest) { // Get previous page image for style consistency (unless it's page 1) if (pageNumber > 1) { - if (isRedraw) { - // For redraw, get all pages and find the previous page's image - const storyData = await getStoryWithPagesBySlug(storyId); - if (storyData) { - const previousPage = storyData.pages.find(p => p.pageNumber === pageNumber - 1); - if (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); + // Always use the previous page's image, regardless of new page or redraw + const storyData = await getStoryWithPagesBySlug(storyId); + if (storyData) { + const previousPage = storyData.pages.find( + (p) => p.pageNumber === pageNumber - 1 + ); + if (previousPage?.generatedImageUrl) { + referenceImages.push(previousPage.generatedImageUrl); } } } @@ -138,9 +137,14 @@ export async function POST(request: NextRequest) { referenceImages.push(...characterImages); // 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, - characterImages: p.characterImageUrls, })); const fullPrompt = buildComicPrompt({ @@ -151,7 +155,9 @@ export async function POST(request: NextRequest) { 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; try { @@ -160,8 +166,8 @@ export async function POST(request: NextRequest) { prompt: fullPrompt, width: dimensions.width, height: dimensions.height, - temperature: 0.1, - reference_images: referenceImages.length > 0 ? referenceImages : undefined, + reference_images: + referenceImages.length > 0 ? referenceImages : undefined, }); } catch (error) { console.error("Together AI API error:", error); @@ -225,4 +231,4 @@ export async function POST(request: NextRequest) { { status: 500 } ); } -} \ No newline at end of file +} diff --git a/app/api/stories/[storySlug]/route.ts b/app/api/stories/[storySlug]/route.ts index 63e633e..7af645c 100644 --- a/app/api/stories/[storySlug]/route.ts +++ b/app/api/stories/[storySlug]/route.ts @@ -13,10 +13,6 @@ export async function GET( const authResult = await auth(); 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; // Special case: if slug is "all", return user's stories for debugging @@ -27,10 +23,17 @@ export async function GET( { 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({ 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); if (!result) { - return NextResponse.json( - { error: "Story not found" }, - { status: 404 } - ); + return NextResponse.json({ error: "Story not found" }, { status: 404 }); } // Check if the story belongs to the authenticated user @@ -66,4 +66,4 @@ export async function GET( { status: 500 } ); } -} \ No newline at end of file +} diff --git a/lib/prompt.ts b/lib/prompt.ts index a37015a..73ce4dc 100644 --- a/lib/prompt.ts +++ b/lib/prompt.ts @@ -17,7 +17,6 @@ export function buildComicPrompt({ isAddPage?: boolean; previousPages?: Array<{ prompt: string; - characterImages: string[]; }>; }): string { const styleInfo = COMIC_STYLES.find((s) => s.id === style); @@ -31,7 +30,7 @@ export function buildComicPrompt({ if (isAddPage && previousPages.length > 0) { const storyHistory = previousPages .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`; } @@ -96,4 +95,4 @@ COMPOSITION: - Detailed backgrounds matching the scene and mood`; return `${systemPrompt}\n\nSTORY:\n${prompt}`; -} \ No newline at end of file +}