128 lines
3.6 KiB
JavaScript
128 lines
3.6 KiB
JavaScript
/**
|
|
* Sanity Check Prompts
|
|
*
|
|
* Functions for building and parsing batch product consistency validation prompts.
|
|
* System and general prompts are loaded from the database.
|
|
*/
|
|
|
|
/**
|
|
* Build the user prompt for sanity check
|
|
* Combines database prompts with product data
|
|
*
|
|
* @param {Object[]} products - Array of product data (limited fields for context)
|
|
* @param {Object} prompts - Prompts loaded from database
|
|
* @param {string} prompts.general - General sanity check rules
|
|
* @returns {string} Complete user prompt
|
|
*/
|
|
function buildSanityCheckUserPrompt(products, prompts) {
|
|
// Build a simplified product list for the prompt
|
|
const productSummaries = products.map((p, index) => ({
|
|
index,
|
|
name: p.name,
|
|
supplier: p.supplier_name || p.supplier,
|
|
company: p.company_name || p.company,
|
|
supplier_no: p.supplier_no,
|
|
msrp: p.msrp,
|
|
cost_each: p.cost_each,
|
|
qty_per_unit: p.qty_per_unit,
|
|
case_qty: p.case_qty,
|
|
tax_cat: p.tax_cat_name || p.tax_cat,
|
|
size_cat: p.size_cat_name || p.size_cat,
|
|
themes: p.theme_names || p.themes,
|
|
weight: p.weight,
|
|
length: p.length,
|
|
width: p.width,
|
|
height: p.height
|
|
}));
|
|
|
|
const parts = [];
|
|
|
|
// Add general prompt/rules if provided
|
|
if (prompts.general) {
|
|
parts.push(prompts.general);
|
|
parts.push(''); // Empty line for separation
|
|
}
|
|
|
|
// Add products to review
|
|
parts.push(`PRODUCTS TO REVIEW (${products.length} items):`);
|
|
parts.push(JSON.stringify(productSummaries, null, 2));
|
|
|
|
// Add response format
|
|
parts.push('');
|
|
parts.push('RESPOND WITH JSON:');
|
|
parts.push(JSON.stringify({
|
|
issues: [
|
|
{
|
|
productIndex: 0,
|
|
field: 'msrp',
|
|
issue: 'Description of the issue found',
|
|
suggestion: 'Suggested fix or verification (optional)'
|
|
}
|
|
],
|
|
summary: '1-2 sentences summarizing the batch quality'
|
|
}, null, 2));
|
|
|
|
parts.push('');
|
|
parts.push('If no issues are found, return empty issues array with positive summary.');
|
|
|
|
return parts.join('\n');
|
|
}
|
|
|
|
/**
|
|
* Parse the AI response for sanity check
|
|
*
|
|
* @param {Object|null} parsed - Parsed JSON from AI
|
|
* @param {string} content - Raw response content
|
|
* @returns {Object}
|
|
*/
|
|
function parseSanityCheckResponse(parsed, content) {
|
|
// If we got valid parsed JSON, use it
|
|
if (parsed && Array.isArray(parsed.issues)) {
|
|
return {
|
|
issues: parsed.issues.map(issue => ({
|
|
productIndex: issue.productIndex ?? issue.index ?? 0,
|
|
field: issue.field || 'unknown',
|
|
issue: issue.issue || issue.message || '',
|
|
suggestion: issue.suggestion || null
|
|
})),
|
|
summary: parsed.summary || 'Review complete'
|
|
};
|
|
}
|
|
|
|
// Try to extract from content if parsing failed
|
|
try {
|
|
// Try to find issues array
|
|
const issuesMatch = content.match(/"issues"\s*:\s*\[([\s\S]*?)\]/);
|
|
let issues = [];
|
|
|
|
if (issuesMatch) {
|
|
// Try to parse the array content
|
|
try {
|
|
const arrayContent = `[${issuesMatch[1]}]`;
|
|
const parsedIssues = JSON.parse(arrayContent);
|
|
issues = parsedIssues.map(issue => ({
|
|
productIndex: issue.productIndex ?? issue.index ?? 0,
|
|
field: issue.field || 'unknown',
|
|
issue: issue.issue || issue.message || '',
|
|
suggestion: issue.suggestion || null
|
|
}));
|
|
} catch {
|
|
// Couldn't parse the array
|
|
}
|
|
}
|
|
|
|
// Try to find summary
|
|
const summaryMatch = content.match(/"summary"\s*:\s*"([^"]+)"/);
|
|
const summary = summaryMatch ? summaryMatch[1] : 'Review complete';
|
|
|
|
return { issues, summary };
|
|
} catch {
|
|
return { issues: [], summary: 'Could not parse review results' };
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
buildSanityCheckUserPrompt,
|
|
parseSanityCheckResponse
|
|
};
|