Add Groq as AI provider + new inline AI tasks, extend database to support more prompt types

This commit is contained in:
2026-01-20 10:04:01 -05:00
parent 7218e7cc3f
commit 167c13c572
24 changed files with 3521 additions and 315 deletions

View File

@@ -347,34 +347,34 @@ async function generateDebugResponse(productsToUse, res) {
throw new Error("Database connection not available");
}
// First, fetch the system prompt using the consolidated endpoint approach
// First, fetch the system prompt for bulk validation
const systemPromptResult = await pool.query(`
SELECT * FROM ai_prompts
WHERE prompt_type = 'system'
SELECT * FROM ai_prompts
WHERE prompt_type = 'bulk_validation_system' AND company IS NULL
`);
if (systemPromptResult.rows.length === 0) {
console.error("❌ No system prompt found in database");
throw new Error("No system prompt found in database");
console.error("❌ No bulk_validation_system prompt found in database");
throw new Error("Missing required AI prompt: bulk_validation_system. Please add it in Settings > AI Validation Prompts.");
}
const systemPrompt = systemPromptResult.rows[0];
console.log("📝 Loaded system prompt from database, ID:", systemPrompt.id);
console.log("📝 Loaded bulk_validation_system prompt from database, ID:", systemPrompt.id);
// Then, fetch the general prompt using the consolidated endpoint approach
// Then, fetch the general prompt for bulk validation
const generalPromptResult = await pool.query(`
SELECT * FROM ai_prompts
WHERE prompt_type = 'general'
SELECT * FROM ai_prompts
WHERE prompt_type = 'bulk_validation_general' AND company IS NULL
`);
if (generalPromptResult.rows.length === 0) {
console.error("❌ No general prompt found in database");
throw new Error("No general prompt found in database");
console.error("❌ No bulk_validation_general prompt found in database");
throw new Error("Missing required AI prompt: bulk_validation_general. Please add it in Settings > AI Validation Prompts.");
}
// Get the general prompt text and info
const generalPrompt = generalPromptResult.rows[0];
console.log("📝 Loaded general prompt from database, ID:", generalPrompt.id);
console.log("📝 Loaded bulk_validation_general prompt from database, ID:", generalPrompt.id);
// Fetch company-specific prompts if we have products to validate
let companyPrompts = [];
@@ -389,16 +389,16 @@ async function generateDebugResponse(productsToUse, res) {
if (companyIds.size > 0) {
console.log(`🔍 Found ${companyIds.size} unique companies in products:`, Array.from(companyIds));
// Fetch company-specific prompts
// Fetch company-specific prompts for bulk validation
const companyPromptsResult = await pool.query(`
SELECT * FROM ai_prompts
WHERE prompt_type = 'company_specific'
SELECT * FROM ai_prompts
WHERE prompt_type = 'bulk_validation_company_specific'
AND company = ANY($1)
`, [Array.from(companyIds)]);
companyPrompts = companyPromptsResult.rows;
console.log(`📝 Loaded ${companyPrompts.length} company-specific prompts`);
console.log(`📝 Loaded ${companyPrompts.length} bulk_validation_company_specific prompts`);
}
}
@@ -688,34 +688,34 @@ async function loadPrompt(connection, productsToValidate = null, appPool = null)
throw new Error("Database connection not available");
}
// Fetch the system prompt using the consolidated endpoint approach
// Fetch the system prompt for bulk validation
const systemPromptResult = await pool.query(`
SELECT * FROM ai_prompts
WHERE prompt_type = 'system'
SELECT * FROM ai_prompts
WHERE prompt_type = 'bulk_validation_system' AND company IS NULL
`);
if (systemPromptResult.rows.length === 0) {
console.error("❌ No system prompt found in database");
throw new Error("No system prompt found in database");
console.error("❌ No bulk_validation_system prompt found in database");
throw new Error("Missing required AI prompt: bulk_validation_system. Please add it in Settings > AI Validation Prompts.");
}
const systemInstructions = systemPromptResult.rows[0].prompt_text;
console.log("📝 Loaded system prompt from database");
console.log("📝 Loaded bulk_validation_system prompt from database");
// Fetch the general prompt using the consolidated endpoint approach
// Fetch the general prompt for bulk validation
const generalPromptResult = await pool.query(`
SELECT * FROM ai_prompts
WHERE prompt_type = 'general'
SELECT * FROM ai_prompts
WHERE prompt_type = 'bulk_validation_general' AND company IS NULL
`);
if (generalPromptResult.rows.length === 0) {
console.error("❌ No general prompt found in database");
throw new Error("No general prompt found in database");
console.error("❌ No bulk_validation_general prompt found in database");
throw new Error("Missing required AI prompt: bulk_validation_general. Please add it in Settings > AI Validation Prompts.");
}
// Get the general prompt text
const basePrompt = generalPromptResult.rows[0].prompt_text;
console.log("📝 Loaded general prompt from database");
console.log("📝 Loaded bulk_validation_general prompt from database");
// Fetch company-specific prompts if we have products to validate
let companyPrompts = [];
@@ -730,16 +730,16 @@ async function loadPrompt(connection, productsToValidate = null, appPool = null)
if (companyIds.size > 0) {
console.log(`🔍 Found ${companyIds.size} unique companies in products:`, Array.from(companyIds));
// Fetch company-specific prompts
// Fetch company-specific prompts for bulk validation
const companyPromptsResult = await pool.query(`
SELECT * FROM ai_prompts
WHERE prompt_type = 'company_specific'
SELECT * FROM ai_prompts
WHERE prompt_type = 'bulk_validation_company_specific'
AND company = ANY($1)
`, [Array.from(companyIds)]);
companyPrompts = companyPromptsResult.rows;
console.log(`📝 Loaded ${companyPrompts.length} company-specific prompts`);
console.log(`📝 Loaded ${companyPrompts.length} bulk_validation_company_specific prompts`);
}
}
@@ -1186,14 +1186,14 @@ router.post("/validate", async (req, res) => {
if (!pool) {
console.warn("⚠️ Local database pool not available for prompt sources");
} else {
// Get system prompt
// Get system prompt for bulk validation
const systemPromptResult = await pool.query(`
SELECT * FROM ai_prompts WHERE prompt_type = 'system'
SELECT * FROM ai_prompts WHERE prompt_type = 'bulk_validation_system' AND company IS NULL
`);
// Get general prompt
// Get general prompt for bulk validation
const generalPromptResult = await pool.query(`
SELECT * FROM ai_prompts WHERE prompt_type = 'general'
SELECT * FROM ai_prompts WHERE prompt_type = 'bulk_validation_general' AND company IS NULL
`);
// Extract unique company IDs from products
@@ -1206,10 +1206,10 @@ router.post("/validate", async (req, res) => {
let companyPrompts = [];
if (companyIds.size > 0) {
// Fetch company-specific prompts
// Fetch company-specific prompts for bulk validation
const companyPromptsResult = await pool.query(`
SELECT * FROM ai_prompts
WHERE prompt_type = 'company_specific'
WHERE prompt_type = 'bulk_validation_company_specific'
AND company = ANY($1)
`, [Array.from(companyIds)]);