Layout/style tweaks, remove text file prompts, integrate system prompt into database/settings

This commit is contained in:
2025-03-24 12:26:21 -04:00
parent dd4b3f7145
commit 228ae8b2a9
7 changed files with 232 additions and 315 deletions

View File

@@ -106,6 +106,33 @@ router.get('/type/general', async (req, res) => {
}
});
// Get system prompt
router.get('/type/system', async (req, res) => {
try {
const pool = req.app.locals.pool;
if (!pool) {
throw new Error('Database pool not initialized');
}
const result = await pool.query(`
SELECT * FROM ai_prompts
WHERE prompt_type = 'system'
`);
if (result.rows.length === 0) {
return res.status(404).json({ error: 'System AI prompt not found' });
}
res.json(result.rows[0]);
} catch (error) {
console.error('Error fetching system AI prompt:', error);
res.status(500).json({
error: 'Failed to fetch system AI prompt',
details: error instanceof Error ? error.message : 'Unknown error'
});
}
});
// Create new AI prompt
router.post('/', async (req, res) => {
try {
@@ -121,8 +148,8 @@ router.post('/', async (req, res) => {
}
// Validate prompt type
if (!['general', 'company_specific'].includes(prompt_type)) {
return res.status(400).json({ error: 'Prompt type must be either "general" or "company_specific"' });
if (!['general', 'company_specific', 'system'].includes(prompt_type)) {
return res.status(400).json({ error: 'Prompt type must be either "general", "company_specific", or "system"' });
}
// Validate company is provided for company-specific prompts
@@ -130,6 +157,11 @@ router.post('/', async (req, res) => {
return res.status(400).json({ error: 'Company is required for company-specific prompts' });
}
// Validate company is not provided for general or system prompts
if ((prompt_type === 'general' || prompt_type === 'system') && company) {
return res.status(400).json({ error: 'Company should not be provided for general or system prompts' });
}
const pool = req.app.locals.pool;
if (!pool) {
throw new Error('Database pool not initialized');
@@ -164,6 +196,11 @@ router.post('/', async (req, res) => {
error: 'A general prompt already exists',
details: error.message
});
} else if (error.message.includes('idx_unique_system_prompt')) {
return res.status(409).json({
error: 'A system prompt already exists',
details: error.message
});
}
}
@@ -190,14 +227,19 @@ router.put('/:id', async (req, res) => {
}
// Validate prompt type
if (!['general', 'company_specific'].includes(prompt_type)) {
return res.status(400).json({ error: 'Prompt type must be either "general" or "company_specific"' });
if (!['general', 'company_specific', 'system'].includes(prompt_type)) {
return res.status(400).json({ error: 'Prompt type must be either "general", "company_specific", or "system"' });
}
// Validate company is provided for company-specific prompts
if (prompt_type === 'company_specific' && !company) {
return res.status(400).json({ error: 'Company is required for company-specific prompts' });
}
// Validate company is not provided for general or system prompts
if ((prompt_type === 'general' || prompt_type === 'system') && company) {
return res.status(400).json({ error: 'Company should not be provided for general or system prompts' });
}
const pool = req.app.locals.pool;
if (!pool) {
@@ -241,6 +283,11 @@ router.put('/:id', async (req, res) => {
error: 'A general prompt already exists',
details: error.message
});
} else if (error.message.includes('idx_unique_system_prompt')) {
return res.status(409).json({
error: 'A system prompt already exists',
details: error.message
});
}
}