anything-llm/server/models/slashCommandsPresets.js
Sean Hatfield 05c530221b
Community hub integration ()
* wip hub connection page fe + backend

* lint

* implement backend for local hub items + placeholder endpoints to fetch hub app data

* fix hebrew translations

* revamp community integration flow

* change sidebar

* Auto import if id in URL param
remove preview in card screen and instead go to import flow

* get user's items + team items from hub + ui improvements to hub settings

* lint

* fix merge conflict

* refresh hook for community items

* add fallback for user items

* Disable bundle items by default on all instances

* remove translations (will complete later)

* loading skeleton

* Make community hub endpoints admin only
show visibility on items
combine import/apply for items to they are event logged for review

* improve middleware and import flow

* community hub ui updates

* Adjust importing process

* community hub to dev

* Add webscraper preload into imported plugins

* add runtime property to plugins

* Fix button status on imported skill change
show alert on skill change
Update markdown type and theme on import of agent skill

* update documentaion paths

* remove unused import

* linting

* review loading state

---------

Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
2024-11-26 09:59:43 -08:00

117 lines
3.5 KiB
JavaScript

const { v4 } = require("uuid");
const prisma = require("../utils/prisma");
const CMD_REGEX = new RegExp(/[^a-zA-Z0-9_-]/g);
const SlashCommandPresets = {
formatCommand: function (command = "") {
if (!command || command.length < 2) return `/${v4().split("-")[0]}`;
let adjustedCmd = command.toLowerCase(); // force lowercase
if (!adjustedCmd.startsWith("/")) adjustedCmd = `/${adjustedCmd}`; // Fix if no preceding / is found.
return `/${adjustedCmd.slice(1).toLowerCase().replace(CMD_REGEX, "-")}`; // replace any invalid chars with '-'
},
get: async function (clause = {}) {
try {
const preset = await prisma.slash_command_presets.findFirst({
where: clause,
});
return preset || null;
} catch (error) {
console.error(error.message);
return null;
}
},
where: async function (clause = {}, limit) {
try {
const presets = await prisma.slash_command_presets.findMany({
where: clause,
take: limit || undefined,
});
return presets;
} catch (error) {
console.error(error.message);
return [];
}
},
// Command + userId must be unique combination.
create: async function (userId = null, presetData = {}) {
try {
const existingPreset = await this.get({
userId: userId ? Number(userId) : null,
command: String(presetData.command),
});
if (existingPreset) {
console.log(
"SlashCommandPresets.create - preset already exists - will not create"
);
return existingPreset;
}
const preset = await prisma.slash_command_presets.create({
data: {
...presetData,
// This field (uid) is either the user_id or 0 (for non-multi-user mode).
// the UID field enforces the @@unique(userId, command) constraint since
// the real relational field (userId) cannot be non-null so this 'dummy' field gives us something
// to constrain against within the context of prisma and sqlite that works.
uid: userId ? Number(userId) : 0,
userId: userId ? Number(userId) : null,
},
});
return preset;
} catch (error) {
console.error("Failed to create preset", error.message);
return null;
}
},
getUserPresets: async function (userId = null) {
try {
return (
await prisma.slash_command_presets.findMany({
where: { userId: !!userId ? Number(userId) : null },
orderBy: { createdAt: "asc" },
})
)?.map((preset) => ({
id: preset.id,
command: preset.command,
prompt: preset.prompt,
description: preset.description,
}));
} catch (error) {
console.error("Failed to get user presets", error.message);
return [];
}
},
update: async function (presetId = null, presetData = {}) {
try {
const preset = await prisma.slash_command_presets.update({
where: { id: Number(presetId) },
data: presetData,
});
return preset;
} catch (error) {
console.error("Failed to update preset", error.message);
return null;
}
},
delete: async function (presetId = null) {
try {
await prisma.slash_command_presets.delete({
where: { id: Number(presetId) },
});
return true;
} catch (error) {
console.error("Failed to delete preset", error.message);
return false;
}
},
};
module.exports.SlashCommandPresets = SlashCommandPresets;