merlyn/server/__tests__/utils/agents/defaults.test.js
Sean Hatfield f5f8fb1edb
Agent workspace system prompt with variable expansion (#4526)
* agent workspace system prompt with variable expansion

* cleanup

---------

Co-authored-by: timothycarambat <rambat1010@gmail.com>
2025-10-15 13:32:50 -07:00

132 lines
4.2 KiB
JavaScript

// Set required env vars before requiring modules
process.env.STORAGE_DIR = __dirname;
process.env.NODE_ENV = "test";
const { SystemPromptVariables } = require("../../../models/systemPromptVariables");
const Provider = require("../../../utils/agents/aibitat/providers/ai-provider");
jest.mock("../../../models/systemPromptVariables");
jest.mock("../../../models/systemSettings");
jest.mock("../../../utils/agents/imported", () => ({
activeImportedPlugins: jest.fn().mockReturnValue([]),
}));
jest.mock("../../../utils/agentFlows", () => ({
AgentFlows: {
activeFlowPlugins: jest.fn().mockReturnValue([]),
},
}));
jest.mock("../../../utils/MCP", () => {
return jest.fn().mockImplementation(() => ({
activeMCPServers: jest.fn().mockResolvedValue([]),
}));
});
const { WORKSPACE_AGENT } = require("../../../utils/agents/defaults");
describe("WORKSPACE_AGENT.getDefinition", () => {
beforeEach(() => {
jest.clearAllMocks();
// Mock SystemSettings to return empty arrays for agent skills
const { SystemSettings } = require("../../../models/systemSettings");
SystemSettings.getValueOrFallback = jest.fn().mockResolvedValue("[]");
});
it("should use provider default system prompt when workspace has no openAiPrompt", async () => {
const workspace = {
id: 1,
name: "Test Workspace",
openAiPrompt: null,
};
const user = { id: 1 };
const provider = "openai";
const expectedPrompt = await Provider.systemPrompt({ provider, workspace, user });
const definition = await WORKSPACE_AGENT.getDefinition(
provider,
workspace,
user
);
expect(definition.role).toBe(expectedPrompt);
expect(SystemPromptVariables.expandSystemPromptVariables).not.toHaveBeenCalled();
});
it("should use workspace system prompt with variable expansion when openAiPrompt exists", async () => {
const workspace = {
id: 1,
name: "Test Workspace",
openAiPrompt: "You are a helpful assistant for {workspace.name}. The current user is {user.name}.",
};
const user = { id: 1 };
const provider = "openai";
const expandedPrompt = "You are a helpful assistant for Test Workspace. The current user is John Doe.";
SystemPromptVariables.expandSystemPromptVariables.mockResolvedValue(expandedPrompt);
const definition = await WORKSPACE_AGENT.getDefinition(
provider,
workspace,
user
);
expect(SystemPromptVariables.expandSystemPromptVariables).toHaveBeenCalledWith(
workspace.openAiPrompt,
user.id,
workspace.id
);
expect(definition.role).toBe(expandedPrompt);
});
it("should handle workspace system prompt without user context", async () => {
const workspace = {
id: 1,
name: "Test Workspace",
openAiPrompt: "You are a helpful assistant. Today is {date}.",
};
const user = null;
const provider = "lmstudio";
const expandedPrompt = "You are a helpful assistant. Today is January 1, 2024.";
SystemPromptVariables.expandSystemPromptVariables.mockResolvedValue(expandedPrompt);
const definition = await WORKSPACE_AGENT.getDefinition(
provider,
workspace,
user
);
expect(SystemPromptVariables.expandSystemPromptVariables).toHaveBeenCalledWith(
workspace.openAiPrompt,
null,
workspace.id
);
expect(definition.role).toBe(expandedPrompt);
});
it("should return functions array in definition", async () => {
const workspace = { id: 1, openAiPrompt: null };
const provider = "openai";
const definition = await WORKSPACE_AGENT.getDefinition(
provider,
workspace,
null
);
expect(definition).toHaveProperty("functions");
expect(Array.isArray(definition.functions)).toBe(true);
});
it("should use LMStudio specific prompt when workspace has no openAiPrompt", async () => {
const workspace = { id: 1, openAiPrompt: null };
const user = null;
const provider = "lmstudio";
const definition = await WORKSPACE_AGENT.getDefinition(
provider,
workspace,
null
);
expect(definition.role).toBe(await Provider.systemPrompt({ provider, workspace, user }));
expect(definition.role).toContain("helpful ai assistant");
});
});