anything-llm/server/models/workspacesSuggestedMessages.js
Sean Hatfield 608f28d745
[FEAT] create custom prompt suggestions per workspace (#664)
* create custom suggested chat messages per workspace

* update how suggestedChats are passed to chat window

* update mobile styles

* update edit change handler

---------

Co-authored-by: timothycarambat <rambat1010@gmail.com>
2024-02-06 11:24:33 -08:00

84 lines
2.2 KiB
JavaScript

const prisma = require("../utils/prisma");
const WorkspaceSuggestedMessages = {
get: async function (clause = {}) {
try {
const message = await prisma.workspace_suggested_messages.findFirst({
where: clause,
});
return message || null;
} catch (error) {
console.error(error.message);
return null;
}
},
where: async function (clause = {}, limit) {
try {
const messages = await prisma.workspace_suggested_messages.findMany({
where: clause,
take: limit || undefined,
});
return messages;
} catch (error) {
console.error(error.message);
return [];
}
},
saveAll: async function (messages, workspaceSlug) {
try {
const workspace = await prisma.workspaces.findUnique({
where: { slug: workspaceSlug },
});
if (!workspace) throw new Error("Workspace not found");
// Delete all existing messages for the workspace
await prisma.workspace_suggested_messages.deleteMany({
where: { workspaceId: workspace.id },
});
// Create new messages
// We create each message individually because prisma
// with sqlite does not support createMany()
for (const message of messages) {
await prisma.workspace_suggested_messages.create({
data: {
workspaceId: workspace.id,
heading: message.heading,
message: message.message,
},
});
}
} catch (error) {
console.error("Failed to save all messages", error.message);
}
},
getMessages: async function (workspaceSlug) {
try {
const workspace = await prisma.workspaces.findUnique({
where: { slug: workspaceSlug },
});
if (!workspace) throw new Error("Workspace not found");
const messages = await prisma.workspace_suggested_messages.findMany({
where: { workspaceId: workspace.id },
orderBy: { createdAt: "asc" },
});
return messages.map((msg) => ({
heading: msg.heading,
message: msg.message,
}));
} catch (error) {
console.error("Failed to get all messages", error.message);
return [];
}
},
};
module.exports.WorkspaceSuggestedMessages = WorkspaceSuggestedMessages;