File size: 1,419 Bytes
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
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 { prompt, type } = body;

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

    const zai = await ZAI.create();
    const completion = await zai.chat.completions.create({
      messages: [
        { role: "system", content: "Eres Sofia, un asistente de desarrollo." },
        { role: "user", content: prompt }
      ]
    });

    const output = completion.choices[0]?.message?.content || "";

    const task = await db.agentTask.create({
      data: { type: type || "chat", status: "completed", input: prompt, output, completedAt: new Date() }
    });

    return NextResponse.json({ success: true, result: output, task });
  } catch (error: unknown) {
    const message = error instanceof Error ? error.message : "Error desconocido";
    return NextResponse.json({ success: false, error: message }, { status: 500 });
  }
}

export async function GET() {
  try {
    const tasks = await db.agentTask.findMany({ orderBy: { createdAt: "desc" }, take: 20 });
    return NextResponse.json({ success: true, tasks });
  } catch {
    return NextResponse.json({ success: false, error: "Error" }, { status: 500 });
  }
}