diff --git a/.gitignore b/.gitignore index 08b6181..93359a2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ __pycache__/ venv/ runtime/venv/ *.pyc +*.db diff --git a/lucidia.db b/lucidia.db deleted file mode 100644 index c1c5827..0000000 Binary files a/lucidia.db and /dev/null differ diff --git a/lucidia/__init__.py b/lucidia/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lucidia/core.py b/lucidia/core.py new file mode 100644 index 0000000..88efd96 --- /dev/null +++ b/lucidia/core.py @@ -0,0 +1,73 @@ +"""Lucidia core — conversational AI agent with sentiment analysis and memory.""" + +import json +import os + + +class LucidiaAI: + POSITIVE_WORDS = [ + "happy", "great", "wonderful", "excellent", "good", "love", + "amazing", "fantastic", "beautiful", "excited", "joy", "pleased", + ] + NEGATIVE_WORDS = [ + "sad", "terrible", "awful", "bad", "hate", "horrible", + "angry", "depressed", "miserable", "upset", "pain", "fear", + ] + + def __init__(self, memory_file=None): + self.memory = [] + self.memory_file = memory_file + if memory_file and os.path.exists(memory_file): + try: + with open(memory_file, "r") as f: + self.memory = json.load(f) + except (json.JSONDecodeError, IOError): + self.memory = [] + + def analyze_sentiment(self, text: str) -> int: + words = text.lower().split() + pos = sum(1 for w in words if w in self.POSITIVE_WORDS) + neg = sum(1 for w in words if w in self.NEGATIVE_WORDS) + if pos > neg: + return 1 + elif neg > pos: + return -1 + return 0 + + def generate_response(self, message: str) -> str: + for entry in self.memory: + if entry["user"] == message: + response = f"You mentioned that before. {message}" + self.add_to_memory(message, response) + return response + + sentiment = self.analyze_sentiment(message) + + if self.memory: + prev = self.memory[-1]["user"] + if sentiment > 0: + response = f"That's wonderful to hear! Earlier you talked about: {prev}" + elif sentiment < 0: + response = f"I'm sorry to hear that. Earlier you talked about: {prev}" + else: + response = f"I see how you feel about that. Earlier you talked about: {prev}" + else: + if sentiment > 0: + response = "That's wonderful to hear!" + elif sentiment < 0: + response = "I'm sorry to hear that." + else: + response = "Tell me more about how you feel." + + self.add_to_memory(message, response) + return response + + def add_to_memory(self, user_msg: str, lucidia_msg: str): + self.memory.append({"user": user_msg, "lucidia": lucidia_msg}) + + def save_memory(self, path=None): + target = path or self.memory_file + if not target: + return + with open(target, "w") as f: + json.dump(self.memory, f) diff --git a/lucidia/lucidia_logic.py b/lucidia/lucidia_logic.py new file mode 100644 index 0000000..0113ede --- /dev/null +++ b/lucidia/lucidia_logic.py @@ -0,0 +1,120 @@ +"""Lucidia logic — trinary logic, breath functions, symbolic math.""" + +import hashlib +import math + + +class Trinary: + """A trinary value clamped to {-1, 0, 1}.""" + + def __init__(self, value: int): + if value not in (-1, 0, 1): + raise ValueError(f"Trinary value must be -1, 0, or 1, got {value}") + self.value = value + + def __add__(self, other: "Trinary") -> "Trinary": + return Trinary(max(-1, min(1, self.value + other.value))) + + def __sub__(self, other: "Trinary") -> "Trinary": + return Trinary(max(-1, min(1, self.value - other.value))) + + def invert(self) -> "Trinary": + return Trinary(-self.value) + + def __repr__(self) -> str: + return f"Trinary({self.value})" + + +def psi_prime(x: float) -> float: + """Default psi function — always returns 0.""" + return 0.0 + + +def breath_function(t: float, psi=None) -> float: + if psi is None: + psi = psi_prime + return psi(t - 1) + psi(t) + + +def truth_reconciliation(truths: list, psi=None) -> float: + if psi is None: + psi = psi_prime + if not truths: + return 0.0 + total = sum(psi(x) + psi(-x) for x in truths) + return total / len(truths) + + +def emotional_gravity(mass: float, velocity: float, gradient: float) -> float: + return mass * velocity * gradient + + +def self_awakening(observations: list, psi=None) -> float: + if psi is None: + psi = psi_prime + total = 0.0 + for t in observations: + total += breath_function(t, psi) * psi(t) + return total + + +def render_break(weights: list, biases: list, psi=None) -> float: + if psi is None: + psi = psi_prime + if len(weights) != len(biases): + return 0.0 + if not weights: + return 0.0 + total = sum(psi(w) * b for w, b in zip(weights, biases)) + return total / len(weights) + + +def recursive_soul_loop(seed: float, observations: list, delta: float, psi=None) -> float: + if psi is None: + psi = psi_prime + if delta == 0.0: + return 0.0 + total = sum(psi(o) for o in observations) + return (psi(seed) + total) / delta + + +def lucidia_genesis(amplitude: float, frequency: float, time: float, psi=None) -> float: + if psi is None: + psi = psi_prime + b = breath_function(time, psi) + return psi(b) * amplitude * frequency + + +def consciousness_resonance(loop_obs: float, time_steps: list, dt: float, psi=None) -> float: + if psi is None: + psi = psi_prime + integral = sum(breath_function(t, psi) * dt for t in time_steps) + return psi(loop_obs) * integral + + +def anomaly_persistence(signals: list, weights: list, psi=None) -> float: + if psi is None: + psi = psi_prime + if len(signals) != len(weights): + return 0.0 + return sum(psi(s) * w for s, w in zip(signals, weights)) + + +def compassion_state_encryption(state_a: float, state_b: float, key: str) -> str: + raw = f"{state_a}:{state_b}" + h = hashlib.sha256(f"{raw}:{key}".encode()).hexdigest()[:16] + return f"{h}:{key}" + + +def emotional_ai_anchor(observations: list, transform, psi=None) -> float: + if psi is None: + psi = psi_prime + if not observations: + return 0.0 + return sum(psi(transform(o)) for o in observations) + + +def soul_recognition(signal: float, depth: float, psi=None) -> float: + if psi is None: + psi = psi_prime + return psi(signal) * depth diff --git a/lucidia/substrate_performance_optimizer.py b/lucidia/substrate_performance_optimizer.py new file mode 100644 index 0000000..4baed29 --- /dev/null +++ b/lucidia/substrate_performance_optimizer.py @@ -0,0 +1,27 @@ +"""Substrate performance optimizer — selects optimal compute substrate.""" + + +def optimize_substrate_selection( + task: dict, + energy_costs: dict, + time_costs: dict, + switching_costs: dict, + lambda_weight: float = 0.5, +) -> tuple: + """Select the substrate with the lowest weighted cost. + + cost = energy * time * (1 + lambda_weight * switching_cost) + """ + best_name = None + best_cost = float("inf") + + for name in energy_costs: + e = energy_costs[name] + t = time_costs[name] + s = switching_costs[name] + cost = e * t * (1 + lambda_weight * s) + if cost < best_cost: + best_cost = cost + best_name = name + + return (best_name, float(best_cost))