File size: 2,220 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextResponse } from "next/server";
import { z } from "zod";
import { requireCliToolsAuth } from "@/lib/api/requireCliToolsAuth";
import { generateConfig, generateAllConfigs } from "@/lib/cli-helper/config-generator";

const generateConfigSchema = z.object({
  toolId: z.string().min(1),
  baseUrl: z.string().optional(),
  apiKey: z.string().min(1),
  model: z.string().optional(),
});

// GET /api/cli-tools/config - List generated configs for all tools
export async function GET(request: Request) {
  const authError = await requireCliToolsAuth(request);
  if (authError) return authError;

  const { searchParams } = new URL(request.url);
  const baseUrl = searchParams.get("baseUrl") || "http://localhost:20128/v1";
  const apiKey = searchParams.get("apiKey") || "";

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

  try {
    const results = await generateAllConfigs({ baseUrl, apiKey });
    return NextResponse.json({ configs: results });
  } catch (error) {
    console.log("Error generating configs:", error);
    return NextResponse.json({ error: "Failed to generate configs" }, { status: 500 });
  }
}

// POST /api/cli-tools/config - Generate config for a specific tool
export async function POST(request: Request) {
  const authError = await requireCliToolsAuth(request);
  if (authError) return authError;

  try {
    const parsed = generateConfigSchema.safeParse(await request.json());
    if (!parsed.success) {
      return NextResponse.json(
        { error: parsed.error.issues[0]?.message ?? "Invalid request" },
        { status: 400 }
      );
    }
    const { toolId, baseUrl, apiKey, model } = parsed.data;

    const result = await generateConfig(toolId, {
      baseUrl: baseUrl || "http://localhost:20128/v1",
      apiKey,
      model,
    });

    if (!result.success) {
      return NextResponse.json({ error: result.error }, { status: 400 });
    }

    return NextResponse.json({
      configPath: result.configPath,
      content: result.content,
    });
  } catch (error) {
    console.log("Error generating config:", error);
    return NextResponse.json({ error: "Failed to generate config" }, { status: 500 });
  }
}