Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 27 additions & 53 deletions app/api/generate-thumbnail/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { NextRequest, NextResponse } from "next/server";
import {
generateThumbnail,
ThumbnailGenerationOptions,
<<<<<<< HEAD
} from "@/lib/ai/huggingface";
=======
} from "@/lib/ai";
>>>>>>> cde6b69 (feat: add dashboard navigation and search functionality)
import { generateThumbnail, ThumbnailGenerationOptions } from "@/lib/ai";

// Enhanced error response helper
function createErrorResponse(message: string, status: number = 500, type?: string) {
function createErrorResponse(
message: string,
status: number = 500,
type?: string
) {
return NextResponse.json(
{
error: message,
{
error: message,
type: type || "api",
success: false,
timestamp: new Date().toISOString(),
Expand All @@ -29,13 +26,9 @@ export async function POST(request: NextRequest) {
style = "tech",
model = "sdxl",
quality = "balanced",
<<<<<<< HEAD
userId,
=======
provider = "huggingface",
userId,
refinementPrompt,
>>>>>>> cde6b69 (feat: add dashboard navigation and search functionality)
} = body;

// Enhanced input validation
Expand Down Expand Up @@ -63,16 +56,6 @@ export async function POST(request: NextRequest) {
);
}

<<<<<<< HEAD
// Validate model and quality
const validModels = ["sdxl", "flux", "realistic"];
const validQualities = ["fast", "balanced", "high"];
const validStyles = ["tech", "gaming", "tutorial", "lifestyle"];

if (!validModels.includes(model)) {
return createErrorResponse(
"Invalid AI model selected. Please choose a valid model.",
=======
// Validate parameters
const validProviders = ["huggingface", "stability", "fal"];
const validQualities = ["fast", "balanced", "high"];
Expand All @@ -81,7 +64,6 @@ export async function POST(request: NextRequest) {
if (!validProviders.includes(provider)) {
return createErrorResponse(
"Invalid AI provider selected. Please choose a valid provider.",
>>>>>>> cde6b69 (feat: add dashboard navigation and search functionality)
400,
"validation"
);
Expand All @@ -103,35 +85,28 @@ export async function POST(request: NextRequest) {
);
}

<<<<<<< HEAD
// Check for API key
if (!process.env.HUGGINGFACE_API_KEY) {
return createErrorResponse(
"AI service is not configured. Please contact support.",
=======
// Check for API key based on provider
let apiKeyMissing = false;
let apiKeyName = "";

switch (provider) {
case 'huggingface':
case "huggingface":
apiKeyMissing = !process.env.HUGGINGFACE_API_KEY;
apiKeyName = "HUGGINGFACE_API_KEY";
break;
case 'stability':
case "stability":
apiKeyMissing = !process.env.STABILITY_API_KEY;
apiKeyName = "STABILITY_API_KEY";
break;
case 'fal':
case "fal":
apiKeyMissing = !process.env.FAL_KEY;
apiKeyName = "FAL_KEY";
break;
}

if (apiKeyMissing) {
return createErrorResponse(
`AI service is not configured (${apiKeyName} missing). Please contact support.`,
>>>>>>> cde6b69 (feat: add dashboard navigation and search functionality)
500,
"api"
);
Expand All @@ -143,27 +118,19 @@ export async function POST(request: NextRequest) {
style,
model,
quality,
<<<<<<< HEAD
userId,
=======
provider,
userId,
refinementPrompt: refinementPrompt?.trim(),
>>>>>>> cde6b69 (feat: add dashboard navigation and search functionality)
};

console.log("Generating thumbnail with options:", {
prompt: prompt.substring(0, 50) + "...",
style,
model,
quality,
<<<<<<< HEAD
userId: userId ? "***" : "none",
=======
provider,
userId: userId ? "***" : "none",
refinement: refinementPrompt ? "yes" : "no",
>>>>>>> cde6b69 (feat: add dashboard navigation and search functionality)
});

const result = await generateThumbnail(options);
Expand All @@ -179,10 +146,7 @@ export async function POST(request: NextRequest) {
prompt: result.prompt,
style: result.style,
model: result.model,
<<<<<<< HEAD
=======
provider: result.provider,
>>>>>>> cde6b69 (feat: add dashboard navigation and search functionality)
parameters: result.parameters,
timestamp: new Date().toISOString(),
});
Expand All @@ -192,7 +156,7 @@ export async function POST(request: NextRequest) {
// Enhanced error categorization
if (error instanceof Error) {
const errorMessage = error.message.toLowerCase();

// Network/connection errors
if (errorMessage.includes("fetch") || errorMessage.includes("network")) {
return createErrorResponse(
Expand All @@ -203,7 +167,11 @@ export async function POST(request: NextRequest) {
}

// Rate limiting errors
if (errorMessage.includes("rate") || errorMessage.includes("limit") || errorMessage.includes("quota")) {
if (
errorMessage.includes("rate") ||
errorMessage.includes("limit") ||
errorMessage.includes("quota")
) {
return createErrorResponse(
"Too many requests. Please wait a moment before trying again.",
429,
Expand All @@ -221,7 +189,10 @@ export async function POST(request: NextRequest) {
}

// Authentication errors
if (errorMessage.includes("unauthorized") || errorMessage.includes("forbidden")) {
if (
errorMessage.includes("unauthorized") ||
errorMessage.includes("forbidden")
) {
return createErrorResponse(
"AI service authentication failed. Please contact support.",
401,
Expand All @@ -230,7 +201,10 @@ export async function POST(request: NextRequest) {
}

// Timeout errors
if (errorMessage.includes("timeout") || errorMessage.includes("aborted")) {
if (
errorMessage.includes("timeout") ||
errorMessage.includes("aborted")
) {
return createErrorResponse(
"Request timed out. Please try again with a shorter description.",
408,
Expand Down
57 changes: 33 additions & 24 deletions app/api/test-ai/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,56 @@ import { testConnection, testThumbnailGeneration } from "@/lib/ai/huggingface";
export async function POST(request: NextRequest) {
try {
console.log("🔍 Starting AI service test...");

console.log("Request body:", await request.text());

// Check if API key is available
if (!process.env.HUGGINGFACE_API_KEY) {
return NextResponse.json({
success: false,
error: "HUGGINGFACE_API_KEY environment variable is not set",
type: "configuration",
}, { status: 500 });
return NextResponse.json(
{
success: false,
error: "HUGGINGFACE_API_KEY environment variable is not set",
type: "configuration",
},
{ status: 500 }
);
}

// Test basic connection
console.log("Testing basic connection...");
const connectionTest = await testConnection();

if (!connectionTest) {
return NextResponse.json({
success: false,
error: "HuggingFace API connection failed",
type: "connection",
}, { status: 503 });
return NextResponse.json(
{
success: false,
error: "HuggingFace API connection failed",
type: "connection",
},
{ status: 503 }
);
}

// Test thumbnail generation
console.log("Testing thumbnail generation...");
await testThumbnailGeneration();

return NextResponse.json({
success: true,
message: "AI service is working correctly",
timestamp: new Date().toISOString(),
});

} catch (error) {
console.error("❌ AI service test failed:", error);

return NextResponse.json({
success: false,
error: error instanceof Error ? error.message : "Unknown error",
type: "test_failed",
timestamp: new Date().toISOString(),
}, { status: 500 });

return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : "Unknown error",
type: "test_failed",
timestamp: new Date().toISOString(),
},
{ status: 500 }
);
}
}

Expand All @@ -53,4 +62,4 @@ export async function GET() {
message: "AI Test API",
usage: "Send a POST request to test the AI service",
});
}
}
Loading