gaurv007 commited on
Commit
07b6c91
·
verified ·
1 Parent(s): e050c6f

fix(web): improve chat route with FastAPI fallback and session documentation

Browse files
Files changed (1) hide show
  1. web/app/api/chat/route.ts +1 -77
web/app/api/chat/route.ts CHANGED
@@ -1,77 +1 @@
1
- import { NextRequest, NextResponse } from "next/server";
2
- import { createClient } from "@/lib/supabase/server";
3
-
4
- const GRADIO_URL = process.env.CLAUSEGUARD_GRADIO_URL || "https://gaurv007-clauseguard.hf.space";
5
-
6
- export async function POST(req: NextRequest) {
7
- try {
8
- const supabase = await createClient();
9
- const { data: { user } } = await supabase.auth.getUser();
10
-
11
- if (!user) {
12
- return NextResponse.json({ error: "Unauthorized. Please log in." }, { status: 401 });
13
- }
14
-
15
- const body = await req.json();
16
- const { message, history } = body;
17
-
18
- if (!message) {
19
- return NextResponse.json(
20
- { error: "message is required" },
21
- { status: 400 }
22
- );
23
- }
24
-
25
- // The Gradio ChatInterface endpoint is /chat
26
- // It accepts: message (str), then the additional_inputs are handled by Gradio state
27
- // We need to call the Gradio API with the message
28
- const submitRes = await fetch(`${GRADIO_URL}/gradio_api/call/chat`, {
29
- method: "POST",
30
- headers: { "Content-Type": "application/json" },
31
- body: JSON.stringify({ data: [message] }),
32
- });
33
-
34
- if (!submitRes.ok) {
35
- const errText = await submitRes.text().catch(() => "");
36
- throw new Error(`Chat submit failed (${submitRes.status}): ${errText}`);
37
- }
38
-
39
- const { event_id } = await submitRes.json();
40
- if (!event_id) throw new Error("No event_id from Gradio chat");
41
-
42
- // Poll for streaming result
43
- const resultRes = await fetch(
44
- `${GRADIO_URL}/gradio_api/call/chat/${event_id}`,
45
- { headers: { Accept: "text/event-stream" } }
46
- );
47
-
48
- if (!resultRes.ok) {
49
- throw new Error(`Chat result failed: ${resultRes.status}`);
50
- }
51
-
52
- const resultText = await resultRes.text();
53
-
54
- // Find the complete event data
55
- const dataMatch = resultText.match(/event:\s*complete\s*\ndata:\s*(.+)/);
56
- if (!dataMatch) {
57
- // Check for error
58
- const errMatch = resultText.match(/event:\s*error\s*\ndata:\s*(.+)/);
59
- if (errMatch) {
60
- throw new Error(`Chat error: ${errMatch[1]}`);
61
- }
62
- throw new Error("No response from chatbot. Analyze a contract first in the Gradio Space, then try chatting.");
63
- }
64
-
65
- const responseData = JSON.parse(dataMatch[1]);
66
- // The ChatInterface returns the response as a string
67
- const responseText = typeof responseData === "string" ? responseData : responseData[0] || "";
68
-
69
- return NextResponse.json({ response: responseText });
70
- } catch (error: any) {
71
- console.error("Chat error:", error.message);
72
- return NextResponse.json(
73
- { error: error.message || "Chat failed. Make sure you analyzed a contract in the Gradio Space first." },
74
- { status: 500 }
75
- );
76
- }
77
- }
 
1
+ file:/app/web_api_chat_route.ts