File size: 1,653 Bytes
fa6e2ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from src.brain.pattern_library import PatternLibrary

class CodeGenerator:
    TEMPLATES = {
        "class": 'class {name}:\n    def __init__(self):\n        pass\n\n    def run(self):\n        pass\n',
        "function": 'def {name}({args}):\n    """{doc}"""\n    pass\n',
        "test": 'import pytest\n\ndef test_{name}():\n    # Arrange\n    # Act\n    # Assert\n    assert True\n',
        "module": '"""\n{name} — Sovereign module\n"""\n__version__ = "0.1.0"\n',
    }

    # Tuned threshold based on HDC bundle dilution characteristics
    SIMILARITY_THRESHOLD = 0.05

    def __init__(self):
        self.library = PatternLibrary()

    def generate(self, intent, context=None):
        context = context or {}
        similar = self.library.retrieve(intent, top_k=1)
        if similar and similar[0][0] > self.SIMILARITY_THRESHOLD:
            sim = similar[0][0]
            meta = similar[0][1]
            print(f"[GENERATOR] Pattern retrieved (sim={sim:.4f}): {meta['intent']}")
            return meta["code"]
        # Template fallback
        name = context.get("name", intent.split()[-1] if intent.split() else "generated")
        if "test" in intent.lower():
            return self.TEMPLATES["test"].format(name=name)
        elif "class" in intent.lower():
            return self.TEMPLATES["class"].format(name=name)
        elif "function" in intent.lower():
            return self.TEMPLATES["function"].format(
                name=name, args="", doc=intent)
        return self.TEMPLATES["module"].format(name=name)

    def learn(self, intent, code, file_path=None):
        self.library.store(intent, code, file_path)