51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
const CRAWL_SERVICE_URL = 'http://127.0.0.1:8000';
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const body = await req.json();
|
|
const { message, history } = body;
|
|
|
|
console.log(`[Grok API] Incoming body:`, JSON.stringify(body, null, 2));
|
|
|
|
const proxyPayload = {
|
|
message,
|
|
history,
|
|
cookies: body.cookies
|
|
};
|
|
console.log(`[Grok API] Proxy payload:`, JSON.stringify(proxyPayload, null, 2));
|
|
|
|
const response = await fetch(`${CRAWL_SERVICE_URL}/grok/chat`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(proxyPayload),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
console.error(`[Grok API] Service error: ${response.status} ${errorText}`);
|
|
try {
|
|
const errorJson = JSON.parse(errorText);
|
|
return NextResponse.json(errorJson, { status: response.status });
|
|
} catch {
|
|
return NextResponse.json(
|
|
{ error: `Service error: ${response.status} - ${errorText}` },
|
|
{ status: response.status }
|
|
);
|
|
}
|
|
}
|
|
|
|
const data = await response.json();
|
|
return NextResponse.json(data);
|
|
|
|
} catch (error: any) {
|
|
console.error('[Grok API] Proxy error:', error);
|
|
return NextResponse.json(
|
|
{ error: error.message || 'Internal Server Error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|