gaurv007 commited on
Commit
fdc2957
·
verified ·
1 Parent(s): 761e065

fix: web/app/api/redline/route.ts

Browse files
Files changed (1) hide show
  1. web/app/api/redline/route.ts +97 -11
web/app/api/redline/route.ts CHANGED
@@ -1,9 +1,25 @@
1
  import { NextRequest, NextResponse } from "next/server";
 
2
 
3
- const API_URL = process.env.CLAUSEGUARD_API_URL || "https://gaurv007-clauseguard-api.hf.space";
 
 
 
 
 
 
 
 
4
 
5
  export async function POST(req: NextRequest) {
6
  try {
 
 
 
 
 
 
 
7
  const body = await req.json();
8
  const { session_id, text, use_llm } = body;
9
 
@@ -14,19 +30,89 @@ export async function POST(req: NextRequest) {
14
  );
15
  }
16
 
17
- const response = await fetch(`${API_URL}/api/redline`, {
18
- method: "POST",
19
- headers: { "Content-Type": "application/json" },
20
- body: JSON.stringify({ session_id, text, use_llm: use_llm ?? true }),
21
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- if (!response.ok) {
24
- const err = await response.text().catch(() => "");
25
- throw new Error(err || `Backend error: ${response.status}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  }
27
 
28
- const result = await response.json();
29
- return NextResponse.json(result);
 
 
 
30
  } catch (error: any) {
31
  console.error("Redline error:", error.message);
32
  return NextResponse.json(
 
1
  import { NextRequest, NextResponse } from "next/server";
2
+ import { createClient } from "@/lib/supabase/server";
3
 
4
+ /**
5
+ * FIX v4.3: Redline route now works through the Gradio Space directly.
6
+ * The old code pointed to a non-existent FastAPI Space (gaurv007-clauseguard-api.hf.space).
7
+ * Since redlining is already part of the analyze pipeline (returned in analysis results),
8
+ * this endpoint is primarily for re-running redlines on existing text.
9
+ */
10
+
11
+ const GRADIO_URL = process.env.CLAUSEGUARD_GRADIO_URL || "https://gaurv007-clauseguard.hf.space";
12
+ const API_URL = process.env.CLAUSEGUARD_API_URL || "";
13
 
14
  export async function POST(req: NextRequest) {
15
  try {
16
+ const supabase = await createClient();
17
+ const { data: { user } } = await supabase.auth.getUser();
18
+
19
+ if (!user) {
20
+ return NextResponse.json({ error: "Unauthorized. Please log in." }, { status: 401 });
21
+ }
22
+
23
  const body = await req.json();
24
  const { session_id, text, use_llm } = body;
25
 
 
30
  );
31
  }
32
 
33
+ // Try FastAPI backend first (if configured and available)
34
+ if (API_URL) {
35
+ try {
36
+ const response = await fetch(`${API_URL}/api/redline`, {
37
+ method: "POST",
38
+ headers: { "Content-Type": "application/json" },
39
+ body: JSON.stringify({ session_id, text, use_llm: use_llm ?? true }),
40
+ });
41
+
42
+ if (response.ok) {
43
+ const result = await response.json();
44
+ return NextResponse.json(result);
45
+ }
46
+ } catch {
47
+ // Fall through to Gradio approach
48
+ }
49
+ }
50
+
51
+ // Fallback: If text is provided, run full analysis via Gradio (includes redlines)
52
+ if (text) {
53
+ if (text.trim().length < 50) {
54
+ return NextResponse.json({ error: "Text too short (min 50 chars)" }, { status: 400 });
55
+ }
56
+
57
+ const submitRes = await fetch(`${GRADIO_URL}/gradio_api/call/analyze`, {
58
+ method: "POST",
59
+ headers: { "Content-Type": "application/json" },
60
+ body: JSON.stringify({ data: [text] }),
61
+ });
62
 
63
+ if (!submitRes.ok) {
64
+ throw new Error(`Gradio submit failed: ${submitRes.status}`);
65
+ }
66
+
67
+ const { event_id } = await submitRes.json();
68
+ if (!event_id) throw new Error("No event_id from Gradio");
69
+
70
+ let resultText = "";
71
+ let attempts = 0;
72
+ while (attempts < 90) {
73
+ const resultRes = await fetch(
74
+ `${GRADIO_URL}/gradio_api/call/analyze/${event_id}`,
75
+ { headers: { Accept: "text/event-stream" } }
76
+ );
77
+ resultText = await resultRes.text();
78
+ if (resultText.includes("event: complete")) break;
79
+ if (resultText.includes("event: error")) throw new Error("Redline analysis failed");
80
+ await new Promise(r => setTimeout(r, 1000));
81
+ attempts++;
82
+ }
83
+
84
+ if (!resultText.includes("event: complete")) {
85
+ throw new Error("Analysis timed out");
86
+ }
87
+
88
+ // Parse the result to extract redlines from the JSON report
89
+ const completeIdx = resultText.indexOf("event: complete");
90
+ const dataIdx = resultText.indexOf("data: ", completeIdx);
91
+ if (dataIdx === -1) throw new Error("No data in response");
92
+
93
+ const dataStr = resultText.substring(dataIdx + 6).trim();
94
+ const gradioData = JSON.parse(dataStr);
95
+
96
+ // Download JSON report file
97
+ const jsonFileObj = gradioData[8];
98
+ if (jsonFileObj?.url) {
99
+ const jsonRes = await fetch(jsonFileObj.url);
100
+ if (jsonRes.ok) {
101
+ const analysisData = await jsonRes.json();
102
+ if (analysisData.redlines) {
103
+ return NextResponse.json({ redlines: analysisData.redlines, count: analysisData.redlines.length });
104
+ }
105
+ }
106
+ }
107
+
108
+ return NextResponse.json({ redlines: [], count: 0 });
109
  }
110
 
111
+ // No FastAPI backend and only session_id provided (can't access Gradio sessions)
112
+ return NextResponse.json({
113
+ error: "Redline by session_id requires the FastAPI backend. Provide contract text instead, or use the analysis results which already include redline suggestions.",
114
+ }, { status: 400 });
115
+
116
  } catch (error: any) {
117
  console.error("Redline error:", error.message);
118
  return NextResponse.json(