Spaces:
Sleeping
Sleeping
File size: 1,891 Bytes
a061734 4b80343 c75dfef 4b80343 c75dfef 4b80343 a061734 4b80343 a061734 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /**
* Chat Route
*
* POST /api/chat
* Proxy to /api/ai/chat for convenience
*/
import { NextRequest, NextResponse } from "next/server";
import { chat } from "@/lib/ai-client";
import { getCurrentUser } from "@/lib/auth";
export async function POST(request: NextRequest) {
try {
const user = await getCurrentUser(request);
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const body = await request.json();
const { message, sessionId, history, context } = body;
if (!message) {
return NextResponse.json({ error: "Message is required" }, { status: 400 });
}
const result = await chat(message, history, {
...context,
sessionId,
userId: user.id,
username: user.username,
role: user.role,
});
if (!result.success) {
console.error("AI Engine error:", result.error);
// Return 503 with helpful error message
return NextResponse.json({
error: "AI service unavailable",
detail: result.error,
help: "The AI engine service is not running. Please ensure the AI engine is deployed and the AI_ENGINE_URL environment variable is correctly configured."
}, { status: 503 });
}
return NextResponse.json(result.data);
} catch (error) {
console.error("Chat error:", error);
console.error("Error details:", {
message: error instanceof Error ? error.message : "Unknown error",
stack: error instanceof Error ? error.stack : undefined
});
return NextResponse.json({
error: "Internal server error",
detail: error instanceof Error ? error.message : "Unknown error"
}, { status: 500 });
}
}
|