/**
* Additional methods for the thinking engine
*/
/**
* Parse persona response into structured format
*/
export function parsePersonaResponse(personaName, response, focus) {
const result = {
persona: personaName,
focus: focus,
rawResponse: response,
keyInsights: [],
recommendations: [],
concerns: [],
metrics: [],
timestamp: new Date().toISOString()
};
try {
const lines = response.split('\n').filter(line => line.trim());
let currentSection = null;
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.match(/^1\.|key insights?/i)) {
currentSection = 'insights';
continue;
} else if (trimmed.match(/^2\.|strategic recommendations?|recommendations?/i)) {
currentSection = 'recommendations';
continue;
} else if (trimmed.match(/^3\.|concerns?|limitations?/i)) {
currentSection = 'concerns';
continue;
} else if (trimmed.match(/^4\.|metrics?|indicators?/i)) {
currentSection = 'metrics';
continue;
}
if (trimmed.startsWith('-') || trimmed.startsWith('•') || trimmed.match(/^\d+\./)) {
const content = trimmed.replace(/^[-•]\s*|^\d+\.\s*/, '').trim();
if (content) {
switch (currentSection) {
case 'insights':
result.keyInsights.push(content);
break;
case 'recommendations':
result.recommendations.push(content);
break;
case 'concerns':
result.concerns.push(content);
break;
case 'metrics':
result.metrics.push(content);
break;
}
}
}
}
// Fallback: extract any bullet points if structured parsing failed
if (result.keyInsights.length === 0 && result.recommendations.length === 0) {
const bulletPoints = response.match(/[-•]\s*([^\n]+)/g) || [];
bulletPoints.forEach((point, index) => {
const content = point.replace(/^[-•]\s*/, '').trim();
if (index < 3) {
result.keyInsights.push(content);
} else {
result.recommendations.push(content);
}
});
}
} catch (error) {
console.error('Error parsing persona response:', error);
result.keyInsights = [response.substring(0, 200) + '...'];
}
return result;
}
/**
* Identify contradictions between persona analyses
*/
export function identifyContradictions(personaResults) {
const contradictions = [];
for (let i = 0; i < personaResults.length; i++) {
for (let j = i + 1; j < personaResults.length; j++) {
const persona1 = personaResults[i];
const persona2 = personaResults[j];
const contradiction = findContradictionBetween(persona1, persona2);
if (contradiction) {
contradictions.push(contradiction);
}
}
}
return contradictions;
}
/**
* Find specific contradictions between two personas
*/
function findContradictionBetween(persona1, persona2) {
const contradictionKeywords = {
risk: ['safe', 'secure', 'low-risk', 'guaranteed'],
opportunity: ['threat', 'danger', 'risk', 'problem'],
fast: ['slow', 'gradual', 'long-term', 'patient'],
expensive: ['cheap', 'cost-effective', 'affordable', 'budget'],
simple: ['complex', 'complicated', 'sophisticated', 'advanced'],
conservative: ['aggressive', 'bold', 'radical', 'disruptive']
};
const allText1 = [...persona1.keyInsights, ...persona1.recommendations].join(' ').toLowerCase();
const allText2 = [...persona2.keyInsights, ...persona2.recommendations].join(' ').toLowerCase();
for (const [concept, opposites] of Object.entries(contradictionKeywords)) {
if (allText1.includes(concept) && opposites.some(opp => allText2.includes(opp))) {
return {
type: 'conceptual',
concept: concept,
persona1: persona1.persona,
persona2: persona2.persona,
description: `${persona1.persona} emphasizes ${concept} while ${persona2.persona} suggests ${opposites.find(opp => allText2.includes(opp))}`
};
}
}
return null;
}