2024-02-01 20:24:42 +01:00
|
|
|
const prisma = require("../utils/prisma");
|
|
|
|
|
|
|
|
const EmbedChats = {
|
2024-02-02 00:08:30 +01:00
|
|
|
new: async function ({
|
|
|
|
embedId,
|
|
|
|
prompt,
|
|
|
|
response = {},
|
|
|
|
connection_information = {},
|
|
|
|
sessionId,
|
|
|
|
}) {
|
2024-02-01 20:24:42 +01:00
|
|
|
try {
|
|
|
|
const chat = await prisma.embed_chats.create({
|
|
|
|
data: {
|
|
|
|
prompt,
|
|
|
|
embed_id: Number(embedId),
|
|
|
|
response: JSON.stringify(response),
|
2024-02-02 00:08:30 +01:00
|
|
|
connection_information: JSON.stringify(connection_information),
|
2024-02-01 20:24:42 +01:00
|
|
|
session_id: sessionId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return { chat, message: null };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return { chat: null, message: error.message };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
forEmbedByUser: async function (
|
|
|
|
embedId = null,
|
|
|
|
sessionId = null,
|
|
|
|
limit = null,
|
|
|
|
orderBy = null
|
|
|
|
) {
|
|
|
|
if (!embedId || !sessionId) return [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
const chats = await prisma.embed_chats.findMany({
|
|
|
|
where: {
|
|
|
|
embed_id: embedId,
|
|
|
|
session_id: sessionId,
|
2024-02-02 01:20:44 +01:00
|
|
|
include: true,
|
2024-02-01 20:24:42 +01:00
|
|
|
},
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
|
|
|
...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
|
|
|
|
});
|
|
|
|
return chats;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-02-02 01:20:44 +01:00
|
|
|
markHistoryInvalid: async function (embedId = null, sessionId = null) {
|
|
|
|
if (!embedId || !sessionId) return [];
|
2024-02-01 20:24:42 +01:00
|
|
|
|
2024-02-02 01:20:44 +01:00
|
|
|
try {
|
|
|
|
await prisma.embed_chats.updateMany({
|
|
|
|
where: {
|
|
|
|
embed_id: embedId,
|
|
|
|
session_id: sessionId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
include: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
}
|
|
|
|
},
|
2024-02-01 20:24:42 +01:00
|
|
|
|
|
|
|
get: async function (clause = {}, limit = null, orderBy = null) {
|
|
|
|
try {
|
|
|
|
const chat = await prisma.embed_chats.findFirst({
|
|
|
|
where: clause,
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
|
|
|
...(orderBy !== null ? { orderBy } : {}),
|
|
|
|
});
|
|
|
|
return chat || null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
delete: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
await prisma.embed_chats.deleteMany({
|
|
|
|
where: clause,
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
where: async function (
|
|
|
|
clause = {},
|
|
|
|
limit = null,
|
|
|
|
orderBy = null,
|
|
|
|
offset = null
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const chats = await prisma.embed_chats.findMany({
|
|
|
|
where: clause,
|
|
|
|
...(limit !== null ? { take: limit } : {}),
|
|
|
|
...(offset !== null ? { skip: offset } : {}),
|
|
|
|
...(orderBy !== null ? { orderBy } : {}),
|
|
|
|
});
|
|
|
|
return chats;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-02-02 20:41:04 +01:00
|
|
|
whereWithEmbedAndWorkspace: async function (
|
2024-02-02 20:12:06 +01:00
|
|
|
clause = {},
|
|
|
|
limit = null,
|
|
|
|
orderBy = null,
|
|
|
|
offset = null
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const chats = await prisma.embed_chats.findMany({
|
|
|
|
where: clause,
|
2024-02-02 20:41:04 +01:00
|
|
|
include: {
|
|
|
|
embed_config: {
|
|
|
|
select: {
|
|
|
|
workspace: {
|
|
|
|
select: {
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-02 20:12:06 +01:00
|
|
|
...(limit !== null ? { take: limit } : {}),
|
|
|
|
...(offset !== null ? { skip: offset } : {}),
|
|
|
|
...(orderBy !== null ? { orderBy } : {}),
|
|
|
|
});
|
|
|
|
return chats;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-02-01 20:24:42 +01:00
|
|
|
count: async function (clause = {}) {
|
|
|
|
try {
|
|
|
|
const count = await prisma.embed_chats.count({
|
|
|
|
where: clause,
|
|
|
|
});
|
|
|
|
return count;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { EmbedChats };
|