File size: 961 Bytes
0fcfe1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Reflection layer producing why-chains."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Iterable, List


@dataclass
class ReflectionInsight:
    """Structured representation of a reflection cycle."""

    summary: str
    chain: List[str]


class ReflectionEngine:
    """Produces recursive why-chains to explain a decision."""

    def __init__(self, *, depth: int = 3) -> None:
        self.depth = max(1, depth)

    def reflect(self, prompt: str, *, seeds: Iterable[str] | None = None) -> ReflectionInsight:
        chain = list(seeds or [])
        current_reason = prompt.strip()
        for _ in range(self.depth):
            chain.append(current_reason)
            current_reason = f"Because {current_reason.lower()}"
        summary = chain[-1] if chain else "No reflection available."
        return ReflectionInsight(summary=summary, chain=chain)


__all__ = ["ReflectionEngine", "ReflectionInsight"]