/**
* Core thinking engine with multi-persona contradiction analysis
*/
export class ThinkingEngine {
constructor(pollinationsAPI, webSearchAPI = null) {
this.pollinationsAPI = pollinationsAPI;
this.webSearchAPI = webSearchAPI;
}
/**
* Thinking personas for multi-perspective analysis
*/
getThinkingPersonas() {
return [
{
name: 'Strategic Optimist',
prompt: 'You are a strategic optimist who sees opportunities and potential in every situation. Focus on possibilities, growth potential, and positive outcomes. Be enthusiastic but grounded in realistic assessment of opportunities.',
focus: 'opportunities, growth, positive outcomes'
},
{
name: 'Critical Skeptic',
prompt: 'You are a critical skeptic who identifies risks, challenges, and potential failures. Question assumptions, point out weaknesses, and highlight what could go wrong. Be thorough in risk assessment but constructive in criticism.',
focus: 'risks, challenges, potential failures'
},
{
name: 'Practical Implementer',
prompt: 'You are a practical implementer focused on execution, resources, and real-world constraints. Consider feasibility, timeline, budget, and operational requirements. Think about how things actually get done.',
focus: 'execution, resources, feasibility'
},
{
name: 'Creative Disruptor',
prompt: 'You are a creative disruptor who thinks outside conventional boundaries. Challenge traditional approaches, suggest innovative alternatives, and explore unconventional solutions. Push beyond obvious answers.',
focus: 'innovation, alternatives, unconventional solutions'
}
];
}
/**
* Execute multi-persona thinking process
*/
async executeThinking(text, cycles = 3, enableSearch = false) {
const personas = this.getThinkingPersonas();
const thinkingResults = {
topic: text,
cycles: cycles,
searchEnabled: enableSearch,
searchResults: null,
personaAnalysis: [],
contradictions: [],
synthesis: null,
timestamp: new Date().toISOString()
};
// Optional web search for context
if (enableSearch && this.webSearchAPI) {
try {
thinkingResults.searchResults = await this.webSearchAPI(text);
} catch (error) {
console.warn('Web search failed:', error.message);
}
}
// Execute thinking cycles with each persona
for (let cycle = 0; cycle < cycles; cycle++) {
const cycleResults = {
cycle: cycle + 1,
personas: []
};
for (const persona of personas) {
const personaResult = await this.executePersonaThinking(
text,
persona,
thinkingResults.searchResults,
thinkingResults.personaAnalysis
);
cycleResults.personas.push(personaResult);
}
thinkingResults.personaAnalysis.push(cycleResults);
// Identify contradictions after each cycle
const contradictions = this.identifyContradictions(cycleResults.personas);
thinkingResults.contradictions.push({
cycle: cycle + 1,
contradictions: contradictions
});
}
// Generate final synthesis
thinkingResults.synthesis = await this.generateSynthesis(
text,
thinkingResults.personaAnalysis,
thinkingResults.contradictions
);
return thinkingResults;
}
/**
* Execute thinking for a specific persona
*/
async executePersonaThinking(topic, persona, searchResults = null, previousAnalysis = []) {
let prompt = `${persona.prompt}\n\n`;
prompt += `Topic to analyze: "${topic}"\n\n`;
if (searchResults && searchResults.length > 0) {
prompt += `Relevant web search context:\n`;
searchResults.slice(0, 3).forEach((result, index) => {
prompt += `${index + 1}. ${result.title}: ${result.snippet}\n`;
});
prompt += `\n`;
}
if (previousAnalysis.length > 0) {
prompt += `Previous analysis from other perspectives:\n`;
const lastCycle = previousAnalysis[previousAnalysis.length - 1];
lastCycle.personas.forEach(p => {
prompt += `- ${p.persona}: ${p.keyInsights.slice(0, 2).join(', ')}\n`;
});
prompt += `\n`;
}
prompt += `Focus specifically on: ${persona.focus}\n\n`;
prompt += `Provide your strategic analysis in the following format:\n`;
prompt += `1. Key Insights (3-5 bullet points)\n`;
prompt += `2. Strategic Recommendations (2-3 specific actions)\n`;
prompt += `3. Potential Concerns or Limitations\n`;
prompt += `4. Success Metrics or Indicators\n\n`;
prompt += `Be thorough but concise. Focus on actionable strategic thinking.`;
try {
const response = await this.pollinationsAPI(prompt);
return this.parsePersonaResponse(persona.name, response, persona.focus);
} catch (error) {
return {
persona: persona.name,
focus: persona.focus,
error: error.message,
keyInsights: [],
recommendations: [],
concerns: [],
metrics: []
};
}
}