UNPKG

enhanced-thinking-mcp

Version:

Enhanced sequential thinking MCP server for advanced reasoning and problem-solving with Cursor AI

117 lines (114 loc) 4.8 kB
import OpenAI from 'openai'; export class LLMEnhancer { client; config; constructor() { this.initializeFromEnv(); } initializeFromEnv() { const apiKey = process.env.OPENAI_API_KEY; if (apiKey) { this.config = { provider: 'openai', apiKey, model: process.env.OPENAI_MODEL || 'gpt-4o-mini' }; this.client = new OpenAI({ apiKey }); console.error("🤖 LLM Integration: OpenAI enabled"); } } isEnabled() { return !!this.client && !!this.config; } async enhanceThought(request) { if (!this.isEnabled()) { return null; } try { console.error("🤖 LLM: Enhancing thought..."); const systemPrompt = `You are an expert thinking coach that helps improve reasoning quality. Analyze the given thought and provide enhancements while maintaining the original intent. Focus on clarity, structure, logical flow, and depth of analysis. Respond in JSON format with: { "enhanced_thought": "improved version of the original thought", "reasoning_improvements": ["specific improvement 1", "specific improvement 2"], "alternative_approaches": ["alternative way 1", "alternative way 2"], "confidence_boost": number (0-20 points to add to confidence), "quality_improvements": ["quality aspect improved 1", "quality aspect improved 2"] }`; const userPrompt = `Original thought: "${request.thought}" Context: ${request.context || 'None provided'} Current quality score: ${request.quality_score || 'Unknown'}/100 Current confidence: ${request.confidence || 'Unknown'}% Previous thoughts: ${request.previous_thoughts?.join('; ') || 'None'} Please enhance this thought while keeping its core message intact.`; const response = await this.client.chat.completions.create({ model: this.config.model, messages: [ { role: 'system', content: systemPrompt }, { role: 'user', content: userPrompt } ], temperature: 0.7, max_tokens: 1000 }); const content = response.choices[0]?.message?.content; if (!content) { console.error("🤖 LLM: Empty response"); return null; } try { const parsed = JSON.parse(content); console.error("🤖 LLM: Enhancement complete ✅"); return parsed; } catch (parseError) { console.error("🤖 LLM: Failed to parse JSON response"); return null; } } catch (error) { console.error("🤖 LLM: Enhancement failed:", error); return null; } } async generateInsights(thoughtHistory) { if (!this.isEnabled() || thoughtHistory.length === 0) { return []; } try { console.error("🤖 LLM: Generating session insights..."); const systemPrompt = `You are an expert analyst of thinking patterns. Analyze a sequence of thoughts and extract key insights about the thinking process. Focus on patterns, quality progression, and breakthrough moments. Return an array of concise insight strings.`; const thoughtSummary = thoughtHistory.map((t, i) => `${i + 1}. [Q:${t.qualityScore || 'N/A'}] ${t.thought.substring(0, 100)}...`).join('\n'); const response = await this.client.chat.completions.create({ model: this.config.model, messages: [ { role: 'system', content: systemPrompt }, { role: 'user', content: `Analyze this thinking sequence:\n\n${thoughtSummary}\n\nProvide 3-5 key insights as a JSON array of strings.` } ], temperature: 0.8, max_tokens: 500 }); const content = response.choices[0]?.message?.content; if (content) { try { const insights = JSON.parse(content); console.error("🤖 LLM: Insights generated ✅"); return Array.isArray(insights) ? insights : []; } catch { return [`Analysis: ${content.substring(0, 100)}...`]; } } return []; } catch (error) { console.error("🤖 LLM: Insight generation failed:", error); return []; } } } //# sourceMappingURL=llm-integration.js.map