This commit is contained in:
timothycarambat
2025-09-17 16:55:28 -07:00
parent 95557ee16f
commit 554a042f96
3 changed files with 113 additions and 25 deletions

View File

@@ -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 (
<>
<tr

View File

@@ -13,7 +13,7 @@ const moment = require("moment");
*/
const SystemPromptVariables = {
VALID_TYPES: ["user", "system", "static"],
VALID_TYPES: ["user", "system", "static", "workspace"],
DEFAULT_VARIABLES: [
{
key: "time",
@@ -36,6 +36,16 @@ const SystemPromptVariables = {
type: "system",
multiUserRequired: false,
},
{
key: "user.id",
value: async (userId = null) => {
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<string>}
*/
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;

View File

@@ -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
);
}