This commit is contained in:
Emily
2024-10-09 15:30:59 +02:00
parent 126296d28f
commit e953af2c1b
27 changed files with 271 additions and 209 deletions

View File

@@ -3,13 +3,11 @@ import { AiChatModel } from "@schema/ai/AiChatSchema";
export default defineEventHandler(async event => {
const project_id = getRequestProjectId(event);
if (!project_id) return;
const user = getRequestUser(event);
const project = await getUserProjectFromId(project_id, user);
if (!project) return;
const data = await getRequestData(event);
if (!data) return;
const { project_id } = data;
if (!event.context.params) return;
const chat_id = event.context.params['chat_id'];

View File

@@ -4,13 +4,10 @@ import type OpenAI from "openai";
import { getChartsInMessage } from "~/server/services/AiService";
export default defineEventHandler(async event => {
const data = await getRequestData(event);
if (!data) return;
const project_id = getRequestProjectId(event);
if (!project_id) return;
const user = getRequestUser(event);
const project = await getUserProjectFromId(project_id, user);
if (!project) return;
const { project_id } = data;
if (!event.context.params) return;
const chat_id = event.context.params['chat_id'];

View File

@@ -1,28 +0,0 @@
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
import { sendMessageOnChat } from "~/server/services/AiService";
import { getAiChatRemainings } from "./chats_remaining";
export default defineEventHandler(async event => {
const project_id = getRequestProjectId(event);
if (!project_id) return;
const user = getRequestUser(event);
const project = await getUserProjectFromId(project_id, user, false);
if (!project) return;
// if (!user?.logged) return;
// if (!user.user.roles.includes('ADMIN')) return;
const { text, chat_id } = await readBody(event);
if (!text) return setResponseStatus(event, 400, 'text parameter missing');
const chatsRemaining = await getAiChatRemainings(project_id);
if (chatsRemaining <= 0) return setResponseStatus(event, 400, 'CHAT_LIMIT_REACHED');
const response = await sendMessageOnChat(text, project._id.toString(), chat_id);
return response;
});

View File

@@ -3,15 +3,14 @@ import { AiChatModel } from "@schema/ai/AiChatSchema";
export default defineEventHandler(async event => {
const project_id = getRequestProjectId(event);
if (!project_id) return;
const user = getRequestUser(event);
const project = await getUserProjectFromId(project_id, user);
if (!project) return;
const data = await getRequestData(event);
if (!data) return;
const { project_id } = data;
const chatList = await AiChatModel.find({ project_id }, { _id: 1, title: 1 }, { sort: { updated_at: 1 } });
return chatList.map(e => e.toJSON());
});

View File

@@ -1,5 +1,4 @@
import { ProjectLimitModel } from "@schema/ProjectsLimits";
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
export async function getAiChatRemainings(project_id: string) {
const limits = await ProjectLimitModel.findOne({ project_id })
@@ -11,13 +10,11 @@ export async function getAiChatRemainings(project_id: string) {
}
export default defineEventHandler(async event => {
const project_id = getRequestProjectId(event);
if (!project_id) return;
const data = await getRequestData(event);
if (!data) return;
const user = getRequestUser(event);
const project = await getUserProjectFromId(project_id, user);
if (!project) return;
const { pid } = data;
const chatsRemaining = await getAiChatRemainings(project_id);
const chatsRemaining = await getAiChatRemainings(pid);
return chatsRemaining;
});

View File

@@ -0,0 +1,21 @@
import { sendMessageOnChat } from "~/server/services/AiService";
import { getAiChatRemainings } from "./chats_remaining";
export default defineEventHandler(async event => {
const data = await getRequestData(event);
if (!data) return;
const { pid } = data;
const { text, chat_id } = await readBody(event);
if (!text) return setResponseStatus(event, 400, 'text parameter missing');
const chatsRemaining = await getAiChatRemainings(pid);
if (chatsRemaining <= 0) return setResponseStatus(event, 400, 'CHAT_LIMIT_REACHED');
const response = await sendMessageOnChat(text, pid, chat_id);
return response;
});

View File

@@ -5,7 +5,7 @@ import { Redis } from "~/server/services/CacheService";
export default defineEventHandler(async event => {
const data = await getRequestData(event, { requireSchema: false });
const data = await getRequestData(event);
if (!data) return;
const { project_id } = data;

View File

@@ -15,12 +15,10 @@ export type SecutityReport = (TSecurityDomainEntry | TSecurityVisitEntry | TSecu
export default defineEventHandler(async event => {
const project_id = getHeader(event, 'x-pid');
if (!project_id) return;
const data = await getRequestData(event, { requireSchema: false });
if (!data) return;
const user = getRequestUser(event);
const project = await getUserProjectFromId(project_id, user);
if (!project) return;
const { project_id } = data;
const visits = await AnomalyVisitModel.find({ project_id }, { _id: 0, project_id: 0 });
const events = await AnomalyEventsModel.find({ project_id }, { _id: 0, project_id: 0 });