import time from typing import List, Dict from reasoning.sectors import SECTOR_BEHAVIOR from reasoning.templates import TEMPLATES class ReasoningPlanner: def __init__(self, max_loops: int = 3): self.max_loops = max_loops def _classify_sector(self, intent: str, context: List[str]) -> str: """ Basic sector detection based on intent or keywords in context. Returns the sector string. """ for sector_name, keywords in SECTOR_BEHAVIOR.items(): if any(word.lower() in " ".join(context + [intent]).lower() for word in keywords): return sector_name return "general" def _select_template(self, sector: str, intent: str, emotion: str) -> str: """ Choose an appropriate template phrase for the response based on sector, intent type, and user emotion. """ # Default fallbacks intent_templates = TEMPLATES.get(intent, TEMPLATES.get("statement")) template_phrase = intent_templates.get(emotion, intent_templates.get("neutral")) return template_phrase def _analyze_turn( self, intent: str, emotion: str, context: List[str], strategy: str, persona: str ) -> Dict: sector = self._classify_sector(intent, context) template_phrase = self._select_template(sector, intent, emotion) return { "intent": intent, "emotion": emotion, "strategy": strategy, "persona": persona, "context_summary": context[-3:], "sector": sector, "template": template_phrase, "needs_clarification": intent == "question" and len(context) < 2 } def _refine_plan(self, plan: Dict, loop_id: int) -> Dict: plan["confidence"] = min(0.9, 0.4 + 0.2 * (loop_id + 1)) plan["reasoning_depth"] = loop_id + 1 plan["timestamp"] = time.time() if plan["strategy"] == "cautious": plan["confidence"] = min(plan["confidence"], 0.7) elif plan["strategy"] == "bold": plan["confidence"] = min(1.0, plan["confidence"] + 0.1) return plan def plan( self, intent: str, emotion: str, context: List[str], strategy: str = "normal", persona: str = "assistant" ) -> Dict: plan = self._analyze_turn(intent, emotion, context, strategy, persona) for i in range(self.max_loops): plan = self._refine_plan(plan, i) if plan["confidence"] > 0.85: break return plan