File size: 3,688 Bytes
f782685
 
 
 
bf51166
f782685
bf51166
 
 
 
 
f782685
bf51166
 
 
 
 
f782685
bf51166
 
f782685
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf51166
f782685
 
 
 
 
 
 
 
 
 
 
 
bf51166
 
 
 
 
 
 
f782685
bf51166
f782685
 
 
bf51166
 
 
 
 
 
 
 
f782685
 
bf51166
f782685
bf51166
 
f782685
 
 
bf51166
 
 
 
f782685
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { NextRequest, NextResponse } from "next/server";
import { createClient } from "@/lib/supabase/server";

/**
 * FIX v4.3: Chat route completely rewritten.
 *
 * ARCHITECTURE:
 * The Gradio ChatInterface uses gr.State for RAG embeddings — these are
 * per-browser-session and NOT accessible via the Gradio REST API. Every API
 * call creates a new session with empty state, so chat via Gradio API will
 * NEVER have contract context.
 *
 * The correct approach:
 * 1. PRIMARY: Use the FastAPI backend (/api/chat) which manages RAG sessions
 *    with proper TTL-based expiry. The session_id comes from /api/analyze.
 * 2. FALLBACK: If FastAPI is unavailable, return a clear error directing
 *    the user to use the Gradio Space directly.
 *
 * The old code tried to call a non-existent Gradio "chat" endpoint which
 * always failed. Removed the broken Gradio fallback entirely.
 */
export async function POST(req: NextRequest) {
  try {
    const supabase = await createClient();
    const { data: { user } } = await supabase.auth.getUser();

    if (!user) {
      return NextResponse.json({ error: "Unauthorized. Please log in." }, { status: 401 });
    }

    const body = await req.json();
    const { message, history, session_id } = body;

    if (!message) {
      return NextResponse.json(
        { error: "message is required" },
        { status: 400 }
      );
    }

    if (message.length > 2000) {
      return NextResponse.json(
        { error: "Message too long (max 2000 characters)" },
        { status: 400 }
      );
    }

    // Try the FastAPI backend (it has proper RAG session management)
    const apiUrl = process.env.CLAUSEGUARD_API_URL || "";
    if (apiUrl && session_id) {
      try {
        const apiRes = await fetch(`${apiUrl}/api/chat`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ message, session_id, history: history || [] }),
        });
        if (apiRes.ok) {
          const data = await apiRes.json();
          return NextResponse.json({ response: data.response });
        }
        // If 404, session expired
        if (apiRes.status === 404) {
          return NextResponse.json({
            response: "⚠️ Your chat session has expired (sessions last 1 hour). " +
              "Please analyze the contract again to start a new chat session."
          });
        }
      } catch {
        // FastAPI backend unreachable — fall through to error message
      }
    }

    // No FastAPI backend available or no session_id
    // FIX v4.3: Return a clear, helpful message instead of trying a broken Gradio endpoint
    if (!apiUrl) {
      return NextResponse.json({
        response: "⚠️ Contract Q&A chat requires the FastAPI backend which is not currently deployed. " +
          "You can use the chat feature directly in the [Gradio Space](https://gaurv007-clauseguard.hf.space) " +
          "— analyze a contract there, then switch to the Q&A tab."
      });
    }

    if (!session_id) {
      return NextResponse.json({
        response: "⚠️ No active chat session. Please analyze a contract first — " +
          "the chat session is created when you run analysis."
      });
    }

    return NextResponse.json({
      response: "⚠️ Chat service is temporarily unavailable. Please try again, or use the " +
        "[Gradio Space](https://gaurv007-clauseguard.hf.space) directly."
    });

  } catch (error: any) {
    console.error("Chat error:", error.message);
    return NextResponse.json(
      { error: error.message || "Chat failed. Make sure you analyzed a contract first." },
      { status: 500 }
    );
  }
}