wip export all embed and workspace chats

This commit is contained in:
shatfield4 2024-09-18 18:35:26 -07:00
parent b25298c04a
commit f4edd74e64
2 changed files with 65 additions and 10 deletions

View File

@ -53,6 +53,7 @@ const {
const { SlashCommandPresets } = require("../models/slashCommandsPresets");
const { EncryptionManager } = require("../utils/EncryptionManager");
const { BrowserExtensionApiKey } = require("../models/browserExtensionApiKey");
const { SystemChats } = require("../models/systemChats");
function systemEndpoints(app) {
if (!app) return;
@ -970,16 +971,8 @@ function systemEndpoints(app) {
async (request, response) => {
try {
const { offset = 0, limit = 20 } = reqBody(request);
const chats = await WorkspaceChats.whereWithData(
{},
limit,
offset * limit,
{ id: "desc" }
);
const totalChats = await WorkspaceChats.count();
const hasPages = totalChats > (offset + 1) * limit;
response.status(200).json({ chats: chats, hasPages, totalChats });
const { chats, hasPages, totalChats } = await SystemChats.getChats(offset, limit);
response.status(200).json({ chats, hasPages, totalChats });
} catch (e) {
console.error(e);
response.sendStatus(500).end();

View File

@ -0,0 +1,62 @@
const prisma = require("../utils/prisma");
const SystemChats = {
getChats: async function (offset = 0, limit = 20) {
try {
const result = await prisma.$queryRaw`
SELECT * FROM (
SELECT
id,
workspaceId,
prompt,
response,
createdAt,
'workspace' as type,
(SELECT name FROM workspaces WHERE id = workspaceId) as workspace_name,
(SELECT username FROM users WHERE id = user_id) as sent_by
FROM workspace_chats
UNION ALL
SELECT
id,
embed_id as workspaceId,
prompt,
response,
createdAt,
'embed' as type,
(SELECT name FROM workspaces WHERE id = (SELECT workspace_id FROM embed_configs WHERE id = embed_id)) as workspace_name,
NULL as sent_by
FROM embed_chats
) AS combined_chats
ORDER BY createdAt DESC
LIMIT ${limit}
OFFSET ${offset * limit}
`;
const totalChats = await prisma.$queryRaw`
SELECT
(SELECT COUNT(*) FROM workspace_chats) +
(SELECT COUNT(*) FROM embed_chats) as total
`;
const hasPages = Number(totalChats[0].total) > (offset + 1) * limit;
const serializedResult = result.map(chat => ({
...chat,
id: Number(chat.id),
workspaceId: Number(chat.workspaceId),
createdAt: chat.createdAt.toISOString(),
}));
return {
chats: serializedResult,
hasPages,
totalChats: Number(totalChats[0].total),
};
} catch (error) {
console.error(error.message);
return { chats: [], hasPages: false, totalChats: 0 };
}
},
};
module.exports = { SystemChats };