mirror of
https://github.com/SkalaraAI/beta.git
synced 2025-04-09 15:00:20 -04:00
92 lines
2.5 KiB
TypeScript
92 lines
2.5 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
|
|
import { cookies } from "next/headers";
|
|
import { Database } from "@/types/supabase";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
import { completion } from "zod-gpt";
|
|
import * as z from "zod";
|
|
import { OpenAIChatApi } from "llm-api";
|
|
|
|
import { generateTasks } from "@/lib/prompts";
|
|
|
|
const MIN_TASKS_PER_FEATURE = 6;
|
|
const MAX_TASKS_PER_FEATURE = 12;
|
|
const OPENAI_MODEL = "gpt-3.5-turbo-16k";
|
|
|
|
const openai = new OpenAIChatApi(
|
|
{
|
|
apiKey: process.env.OPENAI_API_KEY!,
|
|
},
|
|
{
|
|
model: OPENAI_MODEL,
|
|
}
|
|
);
|
|
|
|
async function getSession(supabase: any) {
|
|
const {
|
|
data: { session },
|
|
} = await supabase.auth.getSession();
|
|
return session;
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const supabase = createRouteHandlerClient<Database>({ cookies });
|
|
const session = await getSession(supabase);
|
|
|
|
if (!session) return NextResponse.redirect("/auth");
|
|
|
|
try {
|
|
const formData = await req.json();
|
|
const project_name = String(formData.project_name);
|
|
const project_description = String(formData.project_description);
|
|
const project_stack = formData.tech_stack;
|
|
const related_features = formData.related_features;
|
|
const feature = formData.feature;
|
|
|
|
console.log("GEN TASKS DATA ===>", formData);
|
|
|
|
const task_gen_prompt = generateTasks(
|
|
project_name,
|
|
project_description,
|
|
project_stack,
|
|
related_features,
|
|
feature
|
|
);
|
|
|
|
const res = await completion(openai, task_gen_prompt, {
|
|
schema: z.object({
|
|
tasks: z
|
|
.array(
|
|
z.object({
|
|
name: z.string().describe("The task name"),
|
|
description: z.string().describe("The task description"),
|
|
priority: z
|
|
.enum(["low", "medium", "high"])
|
|
.describe("The task priority"),
|
|
order: z
|
|
.number()
|
|
.describe("The order in which the task should be implemented"),
|
|
})
|
|
)
|
|
.min(MIN_TASKS_PER_FEATURE)
|
|
.max(MAX_TASKS_PER_FEATURE),
|
|
}),
|
|
});
|
|
|
|
const tasksWithUUID = res.data.tasks.map((task) => ({
|
|
...task,
|
|
uid: uuidv4(),
|
|
feature_id: feature.id,
|
|
}));
|
|
|
|
console.log("TASKS RAW RES ===>", tasksWithUUID);
|
|
|
|
return NextResponse.json({ tasks: tasksWithUUID }, { status: 200 });
|
|
} catch (err) {
|
|
console.error(err);
|
|
return NextResponse.json({ error: err }, { status: 500 });
|
|
}
|
|
}
|