diff --git a/frontend/src/pages/Admin/SystemPromptVariables/VariableRow/index.jsx b/frontend/src/pages/Admin/SystemPromptVariables/VariableRow/index.jsx index ac4c3ea81..7970535f4 100644 --- a/frontend/src/pages/Admin/SystemPromptVariables/VariableRow/index.jsx +++ b/frontend/src/pages/Admin/SystemPromptVariables/VariableRow/index.jsx @@ -8,6 +8,36 @@ import { titleCase } from "text-case"; import truncate from "truncate"; import { Trash } from "@phosphor-icons/react"; +function getTypeColorTheme(type) { + switch (type) { + case "system": + return { + bg: "bg-blue-600/20", + text: "text-blue-400 light:text-blue-800", + }; + case "user": + return { + bg: "bg-cyan-600/20", + text: "text-cyan-400 light:text-cyan-800", + }; + case "workspace": + return { + bg: "bg-orange-600/20", + text: "text-orange-400 light:text-orange-800", + }; + case "dynamic": + return { + bg: "bg-green-600/20", + text: "text-green-400 light:text-green-800", + }; + default: + return { + bg: "bg-yellow-600/20", + text: "text-yellow-400 light:text-yellow-800", + }; + } +} + /** * A row component for displaying a system prompt variable * @param {{id: number|null, key: string, value: string, description: string, type: string}} variable - The system prompt variable to display @@ -16,6 +46,7 @@ import { Trash } from "@phosphor-icons/react"; */ export default function VariableRow({ variable, onRefresh }) { const rowRef = useRef(null); + const colorTheme = getTypeColorTheme(variable.type); const { isOpen, openModal, closeModal } = useModal(); const handleDelete = async () => { @@ -38,28 +69,6 @@ export default function VariableRow({ variable, onRefresh }) { } }; - const getTypeColorTheme = (type) => { - switch (type) { - case "system": - return { - bg: "bg-blue-600/20", - text: "text-blue-400 light:text-blue-800", - }; - case "dynamic": - return { - bg: "bg-green-600/20", - text: "text-green-400 light:text-green-800", - }; - default: - return { - bg: "bg-yellow-600/20", - text: "text-yellow-400 light:text-yellow-800", - }; - } - }; - - const colorTheme = getTypeColorTheme(variable.type); - return ( <> { + if (!userId) return "[User id]"; + return Number(userId); + }, + description: "Current user's id", + type: "user", + multiUserRequired: true, + }, { key: "user.name", value: async (userId = null) => { @@ -74,6 +84,43 @@ const SystemPromptVariables = { type: "user", multiUserRequired: true, }, + { + key: "workspace.id", + value: async (workspaceId = null) => { + if (!workspaceId) return "[Workspace id]"; + try { + const workspace = await prisma.workspaces.findUnique({ + where: { id: Number(workspaceId) }, + }); + return workspace?.id || "[Workspace id is empty]"; + } catch (error) { + console.error("Error fetching workspace id:", error); + return "[Workspace id is empty]"; + } + }, + description: "Current workspace's id", + type: "workspace", + multiUserRequired: true, + }, + { + key: "workspace.name", + value: async (workspaceId = null) => { + if (!workspaceId) return "[Workspace name]"; + try { + const workspace = await prisma.workspaces.findUnique({ + where: { id: Number(workspaceId) }, + select: { name: true }, + }); + return workspace?.name || "[Workspace name is empty]"; + } catch (error) { + console.error("Error fetching workspace name:", error); + return "[Workspace name is empty]"; + } + }, + description: "Current workspace's name", + type: "workspace", + multiUserRequired: true, + }, ], /** @@ -186,9 +233,14 @@ const SystemPromptVariables = { * Injects variables into a string based on the user ID (if provided) and the variables available * @param {string} str - the input string to expand variables into * @param {number|null} userId - the user ID to use for dynamic variables + * @param {number|null} workspaceId - the workspace ID to use for dynamic variables * @returns {Promise} */ - expandSystemPromptVariables: async function (str, userId = null) { + expandSystemPromptVariables: async function ( + str, + userId = null, + workspaceId = null + ) { if (!str) return str; try { @@ -226,6 +278,32 @@ const SystemPromptVariables = { continue; } + if (key.startsWith("workspace.")) { + const workspaceProp = key.split(".")[1]; + const variable = allVariables.find((v) => v.key === key); + + if (variable && typeof variable.value === "function") { + if (variable.value.constructor.name === "AsyncFunction") { + try { + const value = await variable.value(workspaceId); + result = result.replace(match, value); + } catch (error) { + console.error( + `Error processing workspace variable ${key}:`, + error + ); + result = result.replace(match, `[Workspace ${workspaceProp}]`); + } + } else { + const value = variable.value(); + result = result.replace(match, value); + } + } else { + result = result.replace(match, `[Workspace ${workspaceProp}]`); + } + continue; + } + // Handle regular variables (static types) const variable = allVariables.find((v) => v.key === key); if (!variable) continue; diff --git a/server/utils/chats/index.js b/server/utils/chats/index.js index 658302f7b..aaf46bc26 100644 --- a/server/utils/chats/index.js +++ b/server/utils/chats/index.js @@ -94,7 +94,8 @@ async function chatPrompt(workspace, user = null) { "Given the following conversation, relevant context, and a follow up question, reply with an answer to the current question the user is asking. Return only your response to the question given the above information following the users instructions as needed."; return await SystemPromptVariables.expandSystemPromptVariables( basePrompt, - user?.id + user?.id, + workspace?.id ); }