@simonecoelhosfo/optimizely-mcp-server
Version:
Optimizely MCP Server for AI assistants with integrated CLI tools
115 lines (103 loc) โข 3.77 kB
JavaScript
// Debug the condition parser with your exact input
function tryBuildConditionsFromDescription(description) {
if (!description) return null;
const desc = description.toLowerCase();
console.log('Input description:', desc);
// Pattern matching for common targeting scenarios
const patterns = [
// Custom attribute patterns
{
name: 'Quoted attribute pattern',
regex: /(\w+)\s+attribute\s+equals?\s+["']([^"']+)["']/i,
builder: (matches) => {
const attributeName = matches[1].toLowerCase();
const value = matches[2];
return `["and", {"type": "custom_attribute", "name": "${attributeName}", "value": "${value}"}]`;
}
},
{
name: 'Unquoted attribute pattern',
regex: /(\w+)\s+attribute\s+equals?\s+([^'"]*(?:\s+[^'"]*)*)/i,
builder: (matches) => {
const attributeName = matches[1].toLowerCase();
const value = matches[2].trim();
return `["and", {"type": "custom_attribute", "name": "${attributeName}", "value": "${value}"}]`;
}
},
{
name: 'Where clause pattern',
regex: /where\s+(\w+)\s*=\s*["']?([^"'\s]+)["']?/i,
builder: (matches) => {
const attributeName = matches[1].toLowerCase();
const value = matches[2];
return `["and", {"type": "custom_attribute", "name": "${attributeName}", "value": "${value}"}]`;
}
},
{
name: 'Users in location pattern',
regex: /users?\s+in\s+(\w+)/i,
builder: (matches) => {
const location = matches[1];
return `["and", {"type": "custom_attribute", "name": "region", "value": "${location}"}]`;
}
},
{
name: 'Based on attribute pattern',
regex: /based\s+on\s+(\w+)\s+attribute/i,
builder: (matches) => {
const attributeName = matches[1].toLowerCase();
// Try to extract value from context
const valueMatch = desc.match(new RegExp(`${attributeName}[^"']*["']?([^"'\\s]+)["']?`, 'i'));
if (valueMatch) {
return `["and", {"type": "custom_attribute", "name": "${attributeName}", "value": "${valueMatch[1]}"}]`;
}
return null;
}
}
];
// Try each pattern
for (const pattern of patterns) {
console.log(`\nTrying pattern: ${pattern.name}`);
console.log(`Regex: ${pattern.regex}`);
const matches = desc.match(pattern.regex);
if (matches) {
console.log('โ
MATCH FOUND!');
console.log('Matches:', matches);
try {
const result = pattern.builder(matches);
console.log('Generated condition:', result);
return result;
} catch (error) {
console.log('โ Builder failed:', error.message);
continue;
}
} else {
console.log('โ No match');
}
}
return null;
}
// Test with your exact inputs
console.log('๐ DEBUGGING CONDITION PARSER');
console.log('============================\n');
const testCases = [
'Audience targeting users in Spain based on Region attribute',
'Users where Region attribute equals Spain',
'Region attribute equals Spain',
'where Region = Spain',
'users in Spain'
];
testCases.forEach((description, index) => {
console.log(`\n๐งช Test ${index + 1}: "${description}"`);
console.log('=' + '='.repeat(50));
const result = tryBuildConditionsFromDescription(description);
if (result) {
console.log('\n๐ SUCCESS!');
console.log('Generated:', result);
} else {
console.log('\nโ FAILED - No conditions generated');
}
});
console.log('\n๐ฏ CONCLUSION:');
console.log('If any test succeeded, the logic works but the pattern might not match your exact description.');
console.log('If all tests failed, there\'s a bug in the condition builder.');