Improve chat UX: thinking indicator, pending message, 5min timeout
This commit is contained in:
@@ -6,17 +6,23 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
|
||||
const { id } = await params;
|
||||
const body = await req.json();
|
||||
try {
|
||||
// Agent chat can take a while — 5 minute timeout
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 300_000);
|
||||
const res = await fetch(BACKEND + "/api/sessions/" + id + "/chat", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
const data = await res.text();
|
||||
return new NextResponse(data, {
|
||||
status: res.status,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
} catch {
|
||||
return NextResponse.json({ error: "backend unreachable" }, { status: 502 });
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : "backend unreachable";
|
||||
return NextResponse.json({ error: msg }, { status: 502 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user