File size: 1,261 Bytes
38bc0dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d4f60b
 
 
38bc0dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export interface ScoreResult {
  error?: boolean;
  message?: string;
  risk_score: number;
  risk_level: "Low" | "Medium" | "High";
  confidence: number;
  top_risk_factors: string[];
}

export interface RefactorResult {
  error?: boolean;
  message?: string;
  initial_code: string;
  final_code: string;
  final_score: number | null;
  iterations_run: number;
}

const API_BASE_URL =
  import.meta.env.VITE_API_BASE_URL ||
  "https://usman-ai-dev-ai-code-maintainability-engine.hf.space";

async function post<T>(path: string, body: unknown): Promise<T> {
  const fullPath = `${API_BASE_URL}${path}`;
  const res = await fetch(fullPath, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });
  if (!res.ok) {
    throw new Error(`Request to ${fullPath} failed with status ${res.status}`);
  }
  return res.json() as Promise<T>;
}

export function scoreCode(code: string): Promise<ScoreResult> {
  return post<ScoreResult>("/api/score", { code });
}

export function refactorCode(
  code: string,
  targetScore = 20.0,
  maxIterations = 3
): Promise<RefactorResult> {
  return post<RefactorResult>("/api/refactor", {
    code,
    target_score: targetScore,
    max_iterations: maxIterations,
  });
}