File size: 4,009 Bytes
93d3b6e
c1e438c
 
8372ed0
 
c1e438c
93d3b6e
c1e438c
93d3b6e
 
 
 
 
 
c1e438c
93d3b6e
 
 
2048c0e
c1e438c
2048c0e
2f18236
93d3b6e
 
 
 
 
 
2f18236
93d3b6e
2048c0e
93d3b6e
2048c0e
2f18236
 
2048c0e
2f18236
93d3b6e
 
 
 
 
 
2f18236
2048c0e
93d3b6e
2f18236
c1e438c
93d3b6e
 
 
 
 
 
c1e438c
93d3b6e
 
 
 
 
2f18236
 
 
c1e438c
 
 
 
 
2048c0e
2f18236
 
c1e438c
 
 
 
93d3b6e
 
 
 
 
 
 
c1e438c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d3b6e
 
 
 
 
 
 
 
 
 
c1e438c
 
 
 
 
 
 
93d3b6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# reasoning/planner.py
import time
from typing import List, Dict
from reasoning.sectors import SECTOR_BEHAVIOR
from reasoning.templates import TEMPLATES


class ReasoningPlanner:
    """
    Handles the cognitive planning of responses by analyzing intent,
    emotion, context, strategy, and persona. Produces structured
    plans with confidence scores and reasoning depth.
    """

    def __init__(self, max_loops: int = 3):
        """
        :param max_loops: Max refinement loops to improve plan confidence
        """
        self.max_loops = max_loops

    def _classify_sector(self, intent: str, context: List[str]) -> str:
        """
        Detects which sector (domain) the conversation belongs to
        based on intent and context keywords.

        :param intent: The detected intent of user input
        :param context: Recent conversation history
        :return: Sector name string
        """
        combined_text = " ".join(context + [intent]).lower()
        for sector_name, keywords in SECTOR_BEHAVIOR.items():
            if any(word.lower() in combined_text for word in keywords):
                return sector_name
        return "general"

    def _select_template(self, sector: str, intent: str, emotion: str) -> str:
        """
        Select a response template based on sector, intent, and emotion.

        :param sector: Classified sector
        :param intent: User intent
        :param emotion: Detected emotion
        :return: Template phrase string
        """
        intent_templates = TEMPLATES.get(intent, TEMPLATES.get("statement"))
        return intent_templates.get(emotion, intent_templates.get("neutral"))

    def _analyze_turn(
        self,
        intent: str,
        emotion: str,
        context: List[str],
        strategy: str,
        persona: str
    ) -> Dict:
        """
        Analyze a single conversation turn and return a preliminary plan.

        :return: Plan dict with sector, template, persona, strategy, and context summary
        """
        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:
        """
        Refine the plan iteratively, adjusting confidence and reasoning depth.

        :param plan: Current plan dictionary
        :param loop_id: Current iteration index
        :return: Updated plan dictionary
        """
        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:
        """
        Generate a final reasoning plan after iterative refinement.

        :param intent: Detected user intent
        :param emotion: Detected user emotion
        :param context: Recent conversation history
        :param strategy: Planning strategy ("normal", "cautious", "bold")
        :param persona: Persona to simulate
        :return: Final plan dictionary
        """
        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