Wisdom Layer MCP

by PV-Bhat
Verified
// Canvas tool for plan distillation export interface CanvasInput { plan: string; userRequest?: string; sessionId?: string; } export interface CanvasOutput { distilledPlan: string; rationale: string; } /** * The wisdom_canvas tool forces ultra-distilled plan summaries * to create clarity and serve as a checkpoint before implementation */ export async function canvasTool(input: CanvasInput): Promise<CanvasOutput> { try { if (!input.plan) { throw new Error('Plan is required'); } // Format input plan for processing const originalPlan = input.plan.trim(); const userRequest = input.userRequest ? input.userRequest.trim() : ''; // Generate canvas template const template = `# Strategy Canvas ## Goals [Ultra-concise statement of objectives] ## Approach [Key steps and methodology] ## Reasoning [Core rationale and justification] ## Risks & Mitigations [Key risks and how they'll be addressed] ## Visual Summary [Simple ASCII representation of the strategy]`; // Create instructions for Claude to fill out the canvas const instructions = ` You must distill the plan into a highly concise strategy canvas with the following constraints: - Goals section: Maximum 250 characters - Approach section: Maximum 350 characters - Reasoning section: Maximum 300 characters - Risks & Mitigations section: Maximum 350 characters - Visual Summary: Simple ASCII representation of the plan This is a forced distillation exercise - you must be extremely concise while preserving the essence of the plan. Focus on clarity, precision, and actionability. Eliminate all unnecessary details. YOUR PLAN: ${originalPlan} ${userRequest ? `ORIGINAL USER REQUEST:\n${userRequest}\n\n` : ''} Complete the canvas template maintaining the strict character limits. `; // In a real implementation, we would use an LLM to distill the plan // For this demonstration version, we'll create a simple distiller function // Simulate distillation by extracting key components from the plan // This is a very basic implementation - in practice we'd use an LLM for this const distilledPlan = distillPlan(template, originalPlan, instructions); // Add rationale const rationale = "The plan has been distilled to its essence while preserving core goals, approaches, reasoning, and risk awareness. This canvas serves as a clear reference point for implementation."; return { distilledPlan, rationale }; } catch (error) { console.error('Error in wisdom_canvas tool:', error); return { distilledPlan: `Error creating canvas: ${error instanceof Error ? error.message : String(error)}`, rationale: 'Error occurred during plan distillation.' }; } } /** * Simple function to distill a plan into a canvas format * In a real implementation, this would use an LLM */ function distillPlan(template: string, plan: string, instructions: string): string { // Extract key sentences from each expected section // For goals, look for sentences with goal-related keywords const goalKeywords = ['goal', 'aim', 'objective', 'purpose', 'target', 'intended', 'achieve']; const goalSentences = extractSentencesWithKeywords(plan, goalKeywords, 250); // For approach, look for sentences with approach-related keywords const approachKeywords = ['approach', 'method', 'step', 'process', 'procedure', 'how', 'implement']; const approachSentences = extractSentencesWithKeywords(plan, approachKeywords, 350); // For reasoning, look for sentences with reasoning-related keywords const reasoningKeywords = ['because', 'reason', 'rationale', 'why', 'since', 'therefore', 'hence']; const reasoningSentences = extractSentencesWithKeywords(plan, reasoningKeywords, 300); // For risks, look for sentences with risk-related keywords const riskKeywords = ['risk', 'challenge', 'issue', 'problem', 'concern', 'threat', 'mitigate', 'address']; const riskSentences = extractSentencesWithKeywords(plan, riskKeywords, 350); // Create a simple ASCII visual summary const visualSummary = ` +----------------+ | G O A L | +----------------+ | v +----------------+ | APPROACH | +----------------+ | [Risks] | v +----------------+ | IMPLEMENTATION | +----------------+ `; // Replace template placeholders with extracted content return template .replace('[Ultra-concise statement of objectives]', goalSentences) .replace('[Key steps and methodology]', approachSentences) .replace('[Core rationale and justification]', reasoningSentences) .replace('[Key risks and how they'll be addressed]', riskSentences) .replace('[Simple ASCII representation of the strategy]', visualSummary); } /** * Extract sentences containing keywords, limited to maxChars */ function extractSentencesWithKeywords(text: string, keywords: string[], maxChars: number): string { // Split text into sentences const sentences = text.split(/[.!?]\s+/); // Filter sentences containing any of the keywords const matchingSentences = sentences.filter(sentence => keywords.some(keyword => sentence.toLowerCase().includes(keyword.toLowerCase())) ); // Combine sentences and truncate to maxChars let result = matchingSentences.join('. '); if (result.length > maxChars) { result = result.substring(0, maxChars - 3) + '...'; } return result || 'No relevant information found.'; }