Update app/api/ai/route.ts
Browse files3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243441 unmodified line46474849505152535455565758593 unmodified linesexport async function POST(req: Request) {const body = await req.json();// --- IMAGE GENERATION (LOCAL GPU) ---if (body.type === 'IMAGE') {try {console.log("Calling Local Python GPU for Image...");const res = await fetch("http://127.0.0.1:5000/generate-image", {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({ prompt: body.prompt })});const data = await res.json();// Save to DBdb.prepare('UPDATE characters SET image_url = ? WHERE user_id = ?').run(data.url, body.userId);return NextResponse.json({ success: true, url: data.url });} catch (e) {console.error("Local GPU Error:", e);return NextResponse.json({ error: "GPU Failed" }, { status: 500 });}}// --- CHAT (LOCAL GPU) ---if (body.type === 'CHAT') {try {const { message } = body;const res = await fetch("http://127.0.0.1:5000/chat", {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({ message })});const data = await res.json();// Return stream-like format for frontend compatibilityconst encoder = new TextEncoder();const customStream = new ReadableStream({async start(controller) {1 unmodified linecontroller.close();},});return new Response(customStream, { headers: { 'Content-Type': 'text/plain' } });} catch (e) {console.error("Local GPU Chat Error:", e);return NextResponse.json({ error: "AI Sleepy" }, { status: 500 });}}return NextResponse.json({ error: "Invalid Type" });}3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738391 unmodified line414243444546474849503 unmodified linesexport async function POST(req: Request) {const body = await req.json();// IMAGE GENERATION (Talks to Local GPU)if (body.type === 'IMAGE') {try {// Note: 127.0.0.1:5000 is the Python script running in the backgroundconst res = await fetch("http://127.0.0.1:5000/generate-image", {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({ prompt: body.prompt })});const data = await res.json();// Save to DBdb.prepare('UPDATE characters SET image_url = ? WHERE user_id = ?').run(data.url, body.userId);return NextResponse.json({ success: true, url: data.url });} catch (e) {return NextResponse.json({ error: "GPU Failed (Is it running?)" }, { status: 500 });}}// CHAT (Talks to Local GPU)if (body.type === 'CHAT') {try {const res = await fetch("http://127.0.0.1:5000/chat", {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({ message: body.message })});const data = await res.json();// Stream wrapperconst encoder = new TextEncoder();const customStream = new ReadableStream({async start(controller) {1 unmodified linecontroller.close();},});return new Response(customStream, { headers: { 'Content-Type': 'text/plain' } });} catch (e) {return NextResponse.json({ error: "AI Sleepy" }, { status: 500 });}}return NextResponse.json({ error: "Invalid Type" });}
app/api/ai/route.ts+6-15