File size: 1,930 Bytes
333c51a
 
 
 
 
 
 
5cff373
333c51a
5cff373
 
333c51a
 
5cff373
 
 
 
 
 
 
333c51a
5cff373
 
 
 
 
333c51a
5cff373
333c51a
5cff373
 
 
333c51a
5cff373
 
333c51a
 
5cff373
 
 
333c51a
 
 
 
 
 
 
5cff373
333c51a
 
5cff373
 
 
 
333c51a
5cff373
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
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import ZAI from "z-ai-web-dev-sdk";

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { code, repoId, projectId } = body;

    if (!code) {
      return NextResponse.json({ success: false, error: "Codigo requerido" }, { status: 400 });
    }

    const zai = await ZAI.create();
    const completion = await zai.chat.completions.create({
      messages: [
        { role: "system", content: "Eres un analista de codigo. Responde en JSON con {summary, issues:[], suggestions:[]}" },
        { role: "user", content: "Analiza: " + code.substring(0, 3000) }
      ]
    });

    let analysis: { summary?: string; issues?: string[]; suggestions?: string[] } = {};
    try {
      const match = completion.choices[0]?.message?.content?.match(/\{[\s\S]*\}/);
      if (match) analysis = JSON.parse(match[0]);
    } catch {}

    const result = await db.analysis.create({
      data: {
        type: "code",
        result: JSON.stringify(analysis),
        summary: analysis.summary || "Analisis completado",
        repoId: repoId || null,
        projectId: projectId || null
      }
    });

    return NextResponse.json({ success: true, analysis: result });
  } catch {
    return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
  }
}

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url);
    const projectId = searchParams.get("projectId");
    const where: Record<string, string | null> = {};
    if (projectId) where.projectId = projectId;

    const analyses = await db.analysis.findMany({ where, orderBy: { createdAt: "desc" }, take: 20 });
    return NextResponse.json({ success: true, analyses });
  } catch {
    return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
  }
}