File size: 1,305 Bytes
eddfa38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import time

class ConversationalMemory:
    """
    Law XII Component: Topological Memory
    Stores conversational history as a sequence of coordinates in Fiber 2.
    Allows for temporal context retrieval.
    """
    def __init__(self, m=256, k=4):
        self.m = m
        self.k = k
        self.history = [] # List of entries

    def _get_coord(self, text, fiber=2):
        h = hashlib.sha256(text.encode()).digest()
        coords = [h[i % len(h)] % self.m for i in range(self.k - 1)]
        w = (fiber - sum(coords)) % self.m
        return tuple(coords + [w])

    def record_turn(self, speaker, text):
        coord = self._get_coord(text)
        entry = {
            "timestamp": time.time(),
            "speaker": speaker,
            "text": text,
            "coord": coord
        }
        self.history.append(entry)
        print(f"      [MEMORY]: Recorded {speaker} turn @ {coord}")
        return coord

    def get_recent_context(self, depth=5):
        return "\n".join([f"{h['speaker']}: {h['text']}" for h in self.history[-depth:]])

if __name__ == "__main__":
    mem = ConversationalMemory()
    mem.record_turn("User", "Hello TGI.")
    mem.record_turn("TGI", "Greetings. I am operational.")
    print(f"\nContext Retrieval:\n{mem.get_recent_context()}")