mirror of
https://github.com/SkalaraAI/beta.git
synced 2025-04-09 15:00:20 -04:00
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
|
|
import { cookies } from "next/headers";
|
|
import { NextResponse, NextRequest } from "next/server";
|
|
import { Database } from "@/types/supabase";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
|
|
import { SupabaseVectorStore } from "langchain/vectorstores/supabase";
|
|
|
|
async function getSession(supabase: any) {
|
|
const {
|
|
data: { session },
|
|
} = await supabase.auth.getSession();
|
|
return session;
|
|
}
|
|
|
|
export async function POST(
|
|
req: NextRequest,
|
|
{
|
|
params: { workspaceID, projectID },
|
|
}: { params: { workspaceID: string; projectID: string } }
|
|
) {
|
|
try {
|
|
const supabase = createRouteHandlerClient<Database>({ cookies });
|
|
const session = await getSession(supabase);
|
|
|
|
if (!session) return NextResponse.redirect("/auth");
|
|
|
|
const formData = await req.json();
|
|
|
|
const name = String(formData.name);
|
|
const description = String(formData.description);
|
|
const featureID =
|
|
formData.featureID != undefined ? String(formData.featureID) : null;
|
|
|
|
const task = await prisma.task.create({
|
|
data: {
|
|
name,
|
|
description,
|
|
project_id: BigInt(projectID),
|
|
feature_id: featureID != null ? BigInt(featureID) : null,
|
|
},
|
|
});
|
|
|
|
await prisma.profile_task.create({
|
|
data: { profile_id: session.user.id, task_id: task.id },
|
|
});
|
|
|
|
const embeddings = new OpenAIEmbeddings({
|
|
openAIApiKey: process.env.OPENAI_API_KEY!,
|
|
batchSize: 512,
|
|
});
|
|
|
|
const task_prompt = `Task Name: ${task.name}\nTask Description: ${task.description}\nTask Feature: ${task.feature_id}\nTask Project: ${task.project_id}`;
|
|
const embedding = await embeddings.embedQuery(task_prompt);
|
|
|
|
const { data, error } = await supabase.from("documents").insert({
|
|
content: task_prompt,
|
|
metadata: {
|
|
workspace_id: workspaceID,
|
|
project_id: projectID,
|
|
feature_id: featureID,
|
|
},
|
|
embedding: JSON.stringify(embedding),
|
|
});
|
|
|
|
if (error) {
|
|
console.log(error);
|
|
return NextResponse.json({ error: error }, { status: 500 });
|
|
}
|
|
|
|
console.log(data);
|
|
|
|
const res = {
|
|
...task,
|
|
id: String(task.id),
|
|
project_id: String(task.project_id),
|
|
feature_id: task.feature_id ? String(task.feature_id) : null,
|
|
};
|
|
|
|
return NextResponse.json({ project: res }, { status: 200 });
|
|
} catch (err) {
|
|
console.log(err);
|
|
return NextResponse.json({ error: err }, { status: 500 });
|
|
}
|
|
}
|