Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- singularity_llm/__init__.py +8 -0
- singularity_llm/__main__.py +4 -0
- singularity_llm/__pycache__/__init__.cpython-311.pyc +0 -0
- singularity_llm/__pycache__/__main__.cpython-311.pyc +0 -0
- singularity_llm/__pycache__/cli.cpython-311.pyc +0 -0
- singularity_llm/__pycache__/config.cpython-311.pyc +0 -0
- singularity_llm/__pycache__/identity.cpython-311.pyc +0 -0
- singularity_llm/__pycache__/subscription.cpython-311.pyc +0 -0
- singularity_llm/agents/__init__.py +12 -0
- singularity_llm/agents/__pycache__/__init__.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/agent_base.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/agent_manager.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/always_on.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/coder_agent.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/conversation_mesh.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/executor_agent.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/planner_agent.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/researcher_agent.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/reviewer_agent.cpython-311.pyc +0 -0
- singularity_llm/agents/__pycache__/self_refine.cpython-311.pyc +0 -0
- singularity_llm/agents/agent_base.py +198 -0
- singularity_llm/agents/agent_manager.py +158 -0
- singularity_llm/agents/always_on.py +418 -0
- singularity_llm/agents/coder_agent.py +82 -0
- singularity_llm/agents/conversation_mesh.py +628 -0
- singularity_llm/agents/executor_agent.py +79 -0
- singularity_llm/agents/planner_agent.py +79 -0
- singularity_llm/agents/researcher_agent.py +62 -0
- singularity_llm/agents/reviewer_agent.py +71 -0
- singularity_llm/agents/self_refine.py +333 -0
- singularity_llm/cli.py +544 -0
- singularity_llm/config.py +168 -0
- singularity_llm/connectors/__init__.py +5 -0
- singularity_llm/connectors/__pycache__/__init__.cpython-311.pyc +0 -0
- singularity_llm/connectors/__pycache__/api_client.cpython-311.pyc +0 -0
- singularity_llm/connectors/__pycache__/services.cpython-311.pyc +0 -0
- singularity_llm/connectors/__pycache__/webhooks.cpython-311.pyc +0 -0
- singularity_llm/connectors/api_client.py +238 -0
- singularity_llm/connectors/services.py +178 -0
- singularity_llm/connectors/webhooks.py +139 -0
- singularity_llm/harness/__init__.py +1 -0
- singularity_llm/harness/__pycache__/__init__.cpython-311.pyc +0 -0
- singularity_llm/harness/__pycache__/harness.cpython-311.pyc +0 -0
- singularity_llm/harness/__pycache__/tools.cpython-311.pyc +0 -0
- singularity_llm/harness/harness.py +559 -0
- singularity_llm/harness/tools.py +292 -0
- singularity_llm/identity.py +111 -0
- singularity_llm/memory/__init__.py +1 -0
- singularity_llm/memory/__pycache__/__init__.cpython-311.pyc +0 -0
- singularity_llm/memory/__pycache__/fast_cache.cpython-311.pyc +0 -0
singularity_llm/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Singularity LLM — a custom lightweight LLM built from scratch.
|
| 2 |
+
|
| 3 |
+
Pure NumPy implementation with Singularity sub-byte token encoding,
|
| 4 |
+
recursive link memory, universal peer sync, built-in Jarvis voice
|
| 5 |
+
assistant, skill creation, and tool-calling harness.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
__version__ = "0.1.0"
|
singularity_llm/__main__.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Allow running as: python -m singularity_llm"""
|
| 2 |
+
from .cli import main
|
| 3 |
+
|
| 4 |
+
main()
|
singularity_llm/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (495 Bytes). View file
|
|
|
singularity_llm/__pycache__/__main__.cpython-311.pyc
ADDED
|
Binary file (313 Bytes). View file
|
|
|
singularity_llm/__pycache__/cli.cpython-311.pyc
ADDED
|
Binary file (19.1 kB). View file
|
|
|
singularity_llm/__pycache__/config.cpython-311.pyc
ADDED
|
Binary file (10.1 kB). View file
|
|
|
singularity_llm/__pycache__/identity.cpython-311.pyc
ADDED
|
Binary file (6.24 kB). View file
|
|
|
singularity_llm/__pycache__/subscription.cpython-311.pyc
ADDED
|
Binary file (40 kB). View file
|
|
|
singularity_llm/agents/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""AI Agents package — 5 persistent agents that work on goals and projects.
|
| 2 |
+
|
| 3 |
+
Agents:
|
| 4 |
+
- PlannerAgent: breaks goals into steps, generates execution plans
|
| 5 |
+
- CoderAgent: writes code, creates files, debugs
|
| 6 |
+
- ResearcherAgent: gathers information, analyzes topics
|
| 7 |
+
- ReviewerAgent: reviews work, validates quality, finds issues
|
| 8 |
+
- ExecutorAgent: executes commands, runs tools, deploys
|
| 9 |
+
|
| 10 |
+
All agents run persistently in background threads, picking up goals
|
| 11 |
+
from the goal memory and working on them continuously.
|
| 12 |
+
"""
|
singularity_llm/agents/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (717 Bytes). View file
|
|
|
singularity_llm/agents/__pycache__/agent_base.cpython-311.pyc
ADDED
|
Binary file (10.3 kB). View file
|
|
|
singularity_llm/agents/__pycache__/agent_manager.cpython-311.pyc
ADDED
|
Binary file (10.2 kB). View file
|
|
|
singularity_llm/agents/__pycache__/always_on.cpython-311.pyc
ADDED
|
Binary file (22.6 kB). View file
|
|
|
singularity_llm/agents/__pycache__/coder_agent.cpython-311.pyc
ADDED
|
Binary file (5.07 kB). View file
|
|
|
singularity_llm/agents/__pycache__/conversation_mesh.cpython-311.pyc
ADDED
|
Binary file (33.1 kB). View file
|
|
|
singularity_llm/agents/__pycache__/executor_agent.cpython-311.pyc
ADDED
|
Binary file (4.6 kB). View file
|
|
|
singularity_llm/agents/__pycache__/planner_agent.cpython-311.pyc
ADDED
|
Binary file (4.13 kB). View file
|
|
|
singularity_llm/agents/__pycache__/researcher_agent.cpython-311.pyc
ADDED
|
Binary file (3.67 kB). View file
|
|
|
singularity_llm/agents/__pycache__/reviewer_agent.cpython-311.pyc
ADDED
|
Binary file (3.81 kB). View file
|
|
|
singularity_llm/agents/__pycache__/self_refine.cpython-311.pyc
ADDED
|
Binary file (17.2 kB). View file
|
|
|
singularity_llm/agents/agent_base.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Base AI Agent — common functionality for all agents.
|
| 2 |
+
|
| 3 |
+
Each agent:
|
| 4 |
+
- Has a name, role, and specialization
|
| 5 |
+
- Runs in a background thread (persistent)
|
| 6 |
+
- Picks up goals from the goal memory
|
| 7 |
+
- Uses the LLM (via harness) to generate responses
|
| 8 |
+
- Reports progress and results
|
| 9 |
+
- Can be paused/resumed
|
| 10 |
+
- Tracks stats (goals completed, steps executed, errors)
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import logging
|
| 16 |
+
import threading
|
| 17 |
+
import time
|
| 18 |
+
from dataclasses import dataclass, field
|
| 19 |
+
from typing import Any, Callable
|
| 20 |
+
|
| 21 |
+
from ..memory.goal_memory import GoalMemory, Goal
|
| 22 |
+
from ..memory.persistent import PersistentMemory
|
| 23 |
+
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@dataclass
|
| 28 |
+
class AgentStats:
|
| 29 |
+
"""Stats for a single agent."""
|
| 30 |
+
goals_assigned: int = 0
|
| 31 |
+
goals_completed: int = 0
|
| 32 |
+
goals_failed: int = 0
|
| 33 |
+
steps_executed: int = 0
|
| 34 |
+
errors: int = 0
|
| 35 |
+
uptime_s: float = 0.0
|
| 36 |
+
last_active: float = 0.0
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class BaseAgent:
|
| 40 |
+
"""Base class for all AI agents.
|
| 41 |
+
|
| 42 |
+
Subclasses implement `process_goal()` which is called when a goal
|
| 43 |
+
is assigned to this agent.
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
def __init__(self, name: str, role: str, description: str,
|
| 47 |
+
goal_memory: GoalMemory,
|
| 48 |
+
persistent_memory: PersistentMemory | None = None,
|
| 49 |
+
generate_fn: Callable[[str], str] | None = None,
|
| 50 |
+
poll_interval_s: float = 5.0) -> None:
|
| 51 |
+
self.name = name
|
| 52 |
+
self.role = role
|
| 53 |
+
self.description = description
|
| 54 |
+
self.goal_memory = goal_memory
|
| 55 |
+
self.persistent_memory = persistent_memory
|
| 56 |
+
self._generate_fn = generate_fn
|
| 57 |
+
self._poll_interval = poll_interval_s
|
| 58 |
+
|
| 59 |
+
self._running = False
|
| 60 |
+
self._paused = False
|
| 61 |
+
self._thread: threading.Thread | None = None
|
| 62 |
+
self._current_goal: Goal | None = None
|
| 63 |
+
self._stats = AgentStats()
|
| 64 |
+
self._start_time = 0.0
|
| 65 |
+
|
| 66 |
+
def set_generate_fn(self, fn: Callable[[str], str]) -> None:
|
| 67 |
+
"""Set the function used to generate LLM responses."""
|
| 68 |
+
self._generate_fn = fn
|
| 69 |
+
|
| 70 |
+
def start(self) -> None:
|
| 71 |
+
"""Start the agent in a background thread."""
|
| 72 |
+
if self._running:
|
| 73 |
+
return
|
| 74 |
+
self._running = True
|
| 75 |
+
self._paused = False
|
| 76 |
+
self._start_time = time.time()
|
| 77 |
+
self._thread = threading.Thread(target=self._run_loop, daemon=True, name=f"agent-{self.name}")
|
| 78 |
+
self._thread.start()
|
| 79 |
+
logger.info("Agent '%s' started (%s)", self.name, self.role)
|
| 80 |
+
|
| 81 |
+
def stop(self) -> None:
|
| 82 |
+
"""Stop the agent."""
|
| 83 |
+
self._running = False
|
| 84 |
+
if self._thread:
|
| 85 |
+
self._thread.join(timeout=10)
|
| 86 |
+
logger.info("Agent '%s' stopped", self.name)
|
| 87 |
+
|
| 88 |
+
def pause(self) -> None:
|
| 89 |
+
"""Pause the agent (doesn't pick up new goals)."""
|
| 90 |
+
self._paused = True
|
| 91 |
+
logger.info("Agent '%s' paused", self.name)
|
| 92 |
+
|
| 93 |
+
def resume(self) -> None:
|
| 94 |
+
"""Resume the agent."""
|
| 95 |
+
self._paused = False
|
| 96 |
+
logger.info("Agent '%s' resumed", self.name)
|
| 97 |
+
|
| 98 |
+
def _run_loop(self) -> None:
|
| 99 |
+
"""Main agent loop — continuously picks up and processes goals."""
|
| 100 |
+
while self._running:
|
| 101 |
+
try:
|
| 102 |
+
if self._paused:
|
| 103 |
+
time.sleep(self._poll_interval)
|
| 104 |
+
continue
|
| 105 |
+
|
| 106 |
+
# Find a goal assigned to this agent
|
| 107 |
+
goals = self.goal_memory.get_goals_for_agent(self.name)
|
| 108 |
+
active = [g for g in goals if g.status in ("in_progress", "planning")]
|
| 109 |
+
|
| 110 |
+
if not active:
|
| 111 |
+
# Try to pick up an unassigned goal that matches our role
|
| 112 |
+
pending = self.goal_memory.list_goals(status="pending")
|
| 113 |
+
for g in pending:
|
| 114 |
+
if self._can_handle(g):
|
| 115 |
+
self.goal_memory.assign_agent(g.id, self.name)
|
| 116 |
+
self._stats.goals_assigned += 1
|
| 117 |
+
active = [g]
|
| 118 |
+
break
|
| 119 |
+
|
| 120 |
+
if not active:
|
| 121 |
+
time.sleep(self._poll_interval)
|
| 122 |
+
continue
|
| 123 |
+
|
| 124 |
+
# Process the first active goal
|
| 125 |
+
goal = active[0]
|
| 126 |
+
self._current_goal = goal
|
| 127 |
+
self._stats.last_active = time.time()
|
| 128 |
+
|
| 129 |
+
result = self.process_goal(goal)
|
| 130 |
+
|
| 131 |
+
if result.get("success"):
|
| 132 |
+
self._stats.steps_executed += 1
|
| 133 |
+
# Advance the goal
|
| 134 |
+
self.goal_memory.execute_step(
|
| 135 |
+
goal.id, result.get("output", ""), success=True
|
| 136 |
+
)
|
| 137 |
+
else:
|
| 138 |
+
self._stats.errors += 1
|
| 139 |
+
self.goal_memory.execute_step(
|
| 140 |
+
goal.id, result.get("error", "Unknown error"), success=False
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# Check if goal is completed
|
| 144 |
+
updated = self.goal_memory.get_goal(goal.id)
|
| 145 |
+
if updated and updated.status == "completed":
|
| 146 |
+
self._stats.goals_completed += 1
|
| 147 |
+
if self.persistent_memory:
|
| 148 |
+
self.persistent_memory.add_episodic(
|
| 149 |
+
"event", f"Agent {self.name} completed goal: {goal.title}",
|
| 150 |
+
importance=0.8, tags=["goal", "completed", self.name]
|
| 151 |
+
)
|
| 152 |
+
logger.info("Agent '%s' completed goal: %s", self.name, goal.title)
|
| 153 |
+
|
| 154 |
+
self._current_goal = None
|
| 155 |
+
|
| 156 |
+
except Exception as e:
|
| 157 |
+
logger.error("Agent '%s' error: %s", self.name, e)
|
| 158 |
+
self._stats.errors += 1
|
| 159 |
+
time.sleep(self._poll_interval)
|
| 160 |
+
|
| 161 |
+
self._stats.uptime_s = time.time() - self._start_time
|
| 162 |
+
|
| 163 |
+
def _can_handle(self, goal: Goal) -> bool:
|
| 164 |
+
"""Check if this agent can handle a goal. Override in subclasses."""
|
| 165 |
+
return True
|
| 166 |
+
|
| 167 |
+
def process_goal(self, goal: Goal) -> dict[str, Any]:
|
| 168 |
+
"""Process a goal step. Must be implemented by subclasses.
|
| 169 |
+
|
| 170 |
+
Returns: {"success": bool, "output": str, "error": str}
|
| 171 |
+
"""
|
| 172 |
+
raise NotImplementedError
|
| 173 |
+
|
| 174 |
+
def _generate(self, prompt: str) -> str:
|
| 175 |
+
"""Generate a response using the LLM."""
|
| 176 |
+
if self._generate_fn:
|
| 177 |
+
return self._generate_fn(prompt)
|
| 178 |
+
return "(no generation function available)"
|
| 179 |
+
|
| 180 |
+
def get_status(self) -> dict[str, Any]:
|
| 181 |
+
"""Get agent status."""
|
| 182 |
+
return {
|
| 183 |
+
"name": self.name,
|
| 184 |
+
"role": self.role,
|
| 185 |
+
"description": self.description,
|
| 186 |
+
"running": self._running,
|
| 187 |
+
"paused": self._paused,
|
| 188 |
+
"current_goal": self._current_goal.title if self._current_goal else None,
|
| 189 |
+
"stats": {
|
| 190 |
+
"goals_assigned": self._stats.goals_assigned,
|
| 191 |
+
"goals_completed": self._stats.goals_completed,
|
| 192 |
+
"goals_failed": self._stats.goals_failed,
|
| 193 |
+
"steps_executed": self._stats.steps_executed,
|
| 194 |
+
"errors": self._stats.errors,
|
| 195 |
+
"uptime_s": round(self._stats.uptime_s, 1),
|
| 196 |
+
"last_active": self._stats.last_active,
|
| 197 |
+
},
|
| 198 |
+
}
|
singularity_llm/agents/agent_manager.py
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Agent Manager — orchestrates all 5 persistent AI agents.
|
| 2 |
+
|
| 3 |
+
Manages the lifecycle of all agents:
|
| 4 |
+
- Starts/stops all agents
|
| 5 |
+
- Assigns goals to the best-suited agent
|
| 6 |
+
- Monitors agent progress
|
| 7 |
+
- Handles agent failures and reassignment
|
| 8 |
+
- Provides aggregated stats
|
| 9 |
+
|
| 10 |
+
The agents run persistently in background threads, continuously
|
| 11 |
+
picking up goals from the goal memory and working on them.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
from __future__ import annotations
|
| 15 |
+
|
| 16 |
+
import logging
|
| 17 |
+
import time
|
| 18 |
+
from typing import Any, Callable
|
| 19 |
+
|
| 20 |
+
from ..memory.goal_memory import GoalMemory, Goal
|
| 21 |
+
from ..memory.persistent import PersistentMemory
|
| 22 |
+
from .agent_base import BaseAgent
|
| 23 |
+
from .planner_agent import PlannerAgent
|
| 24 |
+
from .coder_agent import CoderAgent
|
| 25 |
+
from .researcher_agent import ResearcherAgent
|
| 26 |
+
from .reviewer_agent import ReviewerAgent
|
| 27 |
+
from .executor_agent import ExecutorAgent
|
| 28 |
+
|
| 29 |
+
logger = logging.getLogger(__name__)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class AgentManager:
|
| 33 |
+
"""Manages 5 persistent AI agents that work on goals and projects.
|
| 34 |
+
|
| 35 |
+
Agents:
|
| 36 |
+
1. Planner — breaks goals into steps
|
| 37 |
+
2. Coder — writes code, creates files
|
| 38 |
+
3. Researcher — gathers information
|
| 39 |
+
4. Reviewer — validates quality
|
| 40 |
+
5. Executor — runs commands, executes tools
|
| 41 |
+
|
| 42 |
+
All agents run in background threads and continuously process goals.
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
def __init__(self, goal_memory: GoalMemory,
|
| 46 |
+
persistent_memory: PersistentMemory | None = None,
|
| 47 |
+
generate_fn: Callable[[str], str] | None = None,
|
| 48 |
+
tool_registry: Any = None) -> None:
|
| 49 |
+
self.goal_memory = goal_memory
|
| 50 |
+
self.persistent_memory = persistent_memory
|
| 51 |
+
self._generate_fn = generate_fn
|
| 52 |
+
self._tool_registry = tool_registry
|
| 53 |
+
|
| 54 |
+
# Create all 5 agents
|
| 55 |
+
self.agents: dict[str, BaseAgent] = {
|
| 56 |
+
"planner": PlannerAgent(goal_memory, persistent_memory, generate_fn),
|
| 57 |
+
"coder": CoderAgent(goal_memory, persistent_memory, generate_fn),
|
| 58 |
+
"researcher": ResearcherAgent(goal_memory, persistent_memory, generate_fn),
|
| 59 |
+
"reviewer": ReviewerAgent(goal_memory, persistent_memory, generate_fn),
|
| 60 |
+
"executor": ExecutorAgent(goal_memory, persistent_memory, generate_fn, tool_registry),
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
self._running = False
|
| 64 |
+
self._start_time = 0.0
|
| 65 |
+
self._stats = {
|
| 66 |
+
"total_goals_assigned": 0,
|
| 67 |
+
"total_goals_completed": 0,
|
| 68 |
+
"total_goals_failed": 0,
|
| 69 |
+
"reassignments": 0,
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
def set_generate_fn(self, fn: Callable[[str], str]) -> None:
|
| 73 |
+
"""Set the LLM generation function for all agents."""
|
| 74 |
+
self._generate_fn = fn
|
| 75 |
+
for agent in self.agents.values():
|
| 76 |
+
agent.set_generate_fn(fn)
|
| 77 |
+
|
| 78 |
+
def start_all(self) -> None:
|
| 79 |
+
"""Start all 5 agents."""
|
| 80 |
+
self._running = True
|
| 81 |
+
self._start_time = time.time()
|
| 82 |
+
for agent in self.agents.values():
|
| 83 |
+
agent.start()
|
| 84 |
+
logger.info("All 5 agents started")
|
| 85 |
+
|
| 86 |
+
def stop_all(self) -> None:
|
| 87 |
+
"""Stop all agents."""
|
| 88 |
+
self._running = False
|
| 89 |
+
for agent in self.agents.values():
|
| 90 |
+
agent.stop()
|
| 91 |
+
logger.info("All agents stopped")
|
| 92 |
+
|
| 93 |
+
def pause_all(self) -> None:
|
| 94 |
+
"""Pause all agents."""
|
| 95 |
+
for agent in self.agents.values():
|
| 96 |
+
agent.pause()
|
| 97 |
+
|
| 98 |
+
def resume_all(self) -> None:
|
| 99 |
+
"""Resume all agents."""
|
| 100 |
+
for agent in self.agents.values():
|
| 101 |
+
agent.resume()
|
| 102 |
+
|
| 103 |
+
def create_project(self, title: str, description: str, priority: str = "high",
|
| 104 |
+
tags: list[str] | None = None) -> Goal:
|
| 105 |
+
"""Create a new project goal and let the planner agent pick it up."""
|
| 106 |
+
goal = self.goal_memory.create_goal(
|
| 107 |
+
title=title, description=description, priority=priority,
|
| 108 |
+
tags=tags or ["project"],
|
| 109 |
+
)
|
| 110 |
+
self._stats["total_goals_assigned"] += 1
|
| 111 |
+
logger.info("Created project: %s (will be picked up by planner agent)", title)
|
| 112 |
+
return goal
|
| 113 |
+
|
| 114 |
+
def assign_goal(self, goal_id: str, agent_name: str) -> bool:
|
| 115 |
+
"""Manually assign a goal to a specific agent."""
|
| 116 |
+
if agent_name not in self.agents:
|
| 117 |
+
logger.warning("Unknown agent: %s", agent_name)
|
| 118 |
+
return False
|
| 119 |
+
result = self.goal_memory.assign_agent(goal_id, agent_name)
|
| 120 |
+
if result:
|
| 121 |
+
self._stats["total_goals_assigned"] += 1
|
| 122 |
+
return True
|
| 123 |
+
return False
|
| 124 |
+
|
| 125 |
+
def get_active_projects(self) -> list[Goal]:
|
| 126 |
+
"""Get all active goals/projects."""
|
| 127 |
+
return self.goal_memory.get_active_goals()
|
| 128 |
+
|
| 129 |
+
def get_agent_status(self) -> dict[str, Any]:
|
| 130 |
+
"""Get status of all agents."""
|
| 131 |
+
return {name: agent.get_status() for name, agent in self.agents.items()}
|
| 132 |
+
|
| 133 |
+
def get_project_progress(self) -> list[dict[str, Any]]:
|
| 134 |
+
"""Get progress of all active projects."""
|
| 135 |
+
active = self.goal_memory.get_active_goals()
|
| 136 |
+
return [g.as_dict() for g in active]
|
| 137 |
+
|
| 138 |
+
def get_stats(self) -> dict[str, Any]:
|
| 139 |
+
"""Get aggregated stats."""
|
| 140 |
+
agent_stats = {name: agent.get_status()["stats"] for name, agent in self.agents.items()}
|
| 141 |
+
goal_stats = self.goal_memory.get_stats()
|
| 142 |
+
|
| 143 |
+
total_completed = sum(s["goals_completed"] for s in agent_stats.values())
|
| 144 |
+
total_failed = sum(s["goals_failed"] for s in agent_stats.values())
|
| 145 |
+
total_steps = sum(s["steps_executed"] for s in agent_stats.values())
|
| 146 |
+
|
| 147 |
+
return {
|
| 148 |
+
"manager": {
|
| 149 |
+
**self._stats,
|
| 150 |
+
"running": self._running,
|
| 151 |
+
"uptime_s": round(time.time() - self._start_time, 1) if self._start_time else 0,
|
| 152 |
+
"total_goals_completed": total_completed,
|
| 153 |
+
"total_goals_failed": total_failed,
|
| 154 |
+
"total_steps_executed": total_steps,
|
| 155 |
+
},
|
| 156 |
+
"agents": agent_stats,
|
| 157 |
+
"goals": goal_stats,
|
| 158 |
+
}
|
singularity_llm/agents/always_on.py
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Always-On Daemon — when not in use, the system keeps working.
|
| 2 |
+
|
| 3 |
+
When the user isn't interacting:
|
| 4 |
+
1. The 5 agents talk to the LLM, creating a continuous learning loop
|
| 5 |
+
2. Jarvis creates skills from agent conversations
|
| 6 |
+
3. Conversation skill creation — patterns extracted from agent talks
|
| 7 |
+
4. Self-refinement engine optimizes speed and intelligence
|
| 8 |
+
5. Goals are progressed — agents pick up and work on pending goals
|
| 9 |
+
6. 100-project mode — auto-generates and works on 100 projects 24/7
|
| 10 |
+
7. First-run naming — greets user as Incentives Inc. LLM, asks for a name
|
| 11 |
+
|
| 12 |
+
This creates a system that's always getting smarter, even when idle.
|
| 13 |
+
Once it has enough skills, it enters 100-project mode and never stops working.
|
| 14 |
+
|
| 15 |
+
Daemon loop:
|
| 16 |
+
- Check if user is active (recent conversation < 60s ago)
|
| 17 |
+
- If idle: trigger agent conversations, skill creation, refinement, project generation
|
| 18 |
+
- If active: do nothing (don't interfere with user interactions)
|
| 19 |
+
- Runs in a background thread, started with `python -m singularity_llm daemon`
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
from __future__ import annotations
|
| 23 |
+
|
| 24 |
+
import logging
|
| 25 |
+
import random
|
| 26 |
+
import threading
|
| 27 |
+
import time
|
| 28 |
+
from typing import Any, Callable
|
| 29 |
+
|
| 30 |
+
from .agent_manager import AgentManager
|
| 31 |
+
from .self_refine import SelfRefinementEngine
|
| 32 |
+
|
| 33 |
+
logger = logging.getLogger(__name__)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class AlwaysOnDaemon:
|
| 37 |
+
"""Always-on daemon — keeps the system working when idle.
|
| 38 |
+
|
| 39 |
+
When the user isn't interacting:
|
| 40 |
+
- Agents have conversations with the LLM (continuous learning)
|
| 41 |
+
- Jarvis creates skills from those conversations
|
| 42 |
+
- Self-refinement engine optimizes speed and intelligence
|
| 43 |
+
- Goals are progressed
|
| 44 |
+
|
| 45 |
+
The daemon monitors activity and only runs when the system is idle.
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
IDLE_THRESHOLD_S = 60.0 # consider idle after 60s of no user activity
|
| 49 |
+
AGENT_TALK_INTERVAL_S = 30.0 # agents talk every 30s when idle
|
| 50 |
+
REFINEMENT_INTERVAL_S = 120.0 # refine every 2 minutes
|
| 51 |
+
SKILL_CREATION_INTERVAL_S = 60.0 # create skills every 60s
|
| 52 |
+
PROJECT_CHECK_INTERVAL_S = 45.0 # check project count every 45s
|
| 53 |
+
MESH_INTERVAL_S = 90.0 # conversation mesh every 90s
|
| 54 |
+
TARGET_PROJECT_COUNT = 100 # maintain 100 active projects
|
| 55 |
+
FAST_REPLY_SKILL_THRESHOLD = 50 # need 50+ skills for fast replies
|
| 56 |
+
FAST_REPLY_HIT_RATE_THRESHOLD = 0.3 # need 30%+ cache hit rate
|
| 57 |
+
|
| 58 |
+
AGENT_CONVERSATION_TOPICS = [
|
| 59 |
+
"What's the most efficient way to process data?",
|
| 60 |
+
"How can I improve my response speed?",
|
| 61 |
+
"What patterns have you noticed in recent conversations?",
|
| 62 |
+
"How do you handle complex problem-solving?",
|
| 63 |
+
"What's the best approach for learning new skills?",
|
| 64 |
+
"How can we optimize our tool usage?",
|
| 65 |
+
"What are common mistakes to avoid in coding?",
|
| 66 |
+
"How do you break down large tasks into smaller ones?",
|
| 67 |
+
"What makes a good user experience?",
|
| 68 |
+
"How can we improve our memory recall?",
|
| 69 |
+
"What's the most important skill for an AI to have?",
|
| 70 |
+
"How do you prioritize tasks when everything is urgent?",
|
| 71 |
+
"What's the relationship between speed and accuracy?",
|
| 72 |
+
"How can we learn from our mistakes?",
|
| 73 |
+
"What's the best way to structure a project?",
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
def __init__(self, harness: Any) -> None:
|
| 77 |
+
self.harness = harness
|
| 78 |
+
self._running = False
|
| 79 |
+
self._thread: threading.Thread | None = None
|
| 80 |
+
|
| 81 |
+
# Components
|
| 82 |
+
self.agent_manager = harness.agent_manager
|
| 83 |
+
self.refinement = SelfRefinementEngine(harness=harness)
|
| 84 |
+
|
| 85 |
+
# State
|
| 86 |
+
self._last_user_activity = time.time()
|
| 87 |
+
self._last_agent_talk = 0.0
|
| 88 |
+
self._last_skill_creation = 0.0
|
| 89 |
+
self._last_refinement = 0.0
|
| 90 |
+
self._last_project_check = 0.0
|
| 91 |
+
self._last_mesh = 0.0
|
| 92 |
+
self._conversation_count = 0
|
| 93 |
+
self._skills_created = 0
|
| 94 |
+
self._projects_generated = 0
|
| 95 |
+
self._projects_completed = 0
|
| 96 |
+
self._hundred_project_mode = False
|
| 97 |
+
|
| 98 |
+
self._stats = {
|
| 99 |
+
"daemon_uptime_s": 0.0,
|
| 100 |
+
"idle_cycles": 0,
|
| 101 |
+
"agent_conversations": 0,
|
| 102 |
+
"skills_created": 0,
|
| 103 |
+
"refinement_cycles": 0,
|
| 104 |
+
"goals_progressed": 0,
|
| 105 |
+
"projects_generated": 0,
|
| 106 |
+
"projects_completed": 0,
|
| 107 |
+
"hundred_project_mode": False,
|
| 108 |
+
"fast_reply_ready": False,
|
| 109 |
+
"mesh_conversations": 0,
|
| 110 |
+
"mesh_categories_discovered": 0,
|
| 111 |
+
"mesh_skills_pooled": 0,
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
def notify_user_activity(self) -> None:
|
| 115 |
+
"""Call this when the user interacts with the system."""
|
| 116 |
+
self._last_user_activity = time.time()
|
| 117 |
+
|
| 118 |
+
def start(self) -> None:
|
| 119 |
+
"""Start the always-on daemon."""
|
| 120 |
+
if self._running:
|
| 121 |
+
return
|
| 122 |
+
self._running = True
|
| 123 |
+
self._start_time = time.time()
|
| 124 |
+
|
| 125 |
+
# Start agents
|
| 126 |
+
self.agent_manager.start_all()
|
| 127 |
+
|
| 128 |
+
# Start self-refinement
|
| 129 |
+
self.refinement.start()
|
| 130 |
+
|
| 131 |
+
# Start daemon loop
|
| 132 |
+
self._thread = threading.Thread(target=self._run_loop, daemon=True, name="always-on")
|
| 133 |
+
self._thread.start()
|
| 134 |
+
|
| 135 |
+
logger.info("Always-on daemon started — 5 agents active, self-refinement running")
|
| 136 |
+
|
| 137 |
+
def stop(self) -> None:
|
| 138 |
+
"""Stop the daemon."""
|
| 139 |
+
self._running = False
|
| 140 |
+
self.refinement.stop()
|
| 141 |
+
self.agent_manager.stop_all()
|
| 142 |
+
if self._thread:
|
| 143 |
+
self._thread.join(timeout=10)
|
| 144 |
+
logger.info("Always-on daemon stopped (ran %d cycles, %d agent conversations, %d skills created)",
|
| 145 |
+
self._stats["idle_cycles"], self._stats["agent_conversations"], self._stats["skills_created"])
|
| 146 |
+
|
| 147 |
+
def _run_loop(self) -> None:
|
| 148 |
+
"""Main daemon loop."""
|
| 149 |
+
while self._running:
|
| 150 |
+
time.sleep(5) # check every 5 seconds
|
| 151 |
+
|
| 152 |
+
now = time.time()
|
| 153 |
+
idle_time = now - self._last_user_activity
|
| 154 |
+
self._stats["daemon_uptime_s"] = now - self._start_time
|
| 155 |
+
|
| 156 |
+
if idle_time < self.IDLE_THRESHOLD_S:
|
| 157 |
+
continue # user is active, don't interfere
|
| 158 |
+
|
| 159 |
+
# System is idle — do work
|
| 160 |
+
self._stats["idle_cycles"] += 1
|
| 161 |
+
|
| 162 |
+
# 1. Agent conversations (every AGENT_TALK_INTERVAL_S)
|
| 163 |
+
if now - self._last_agent_talk > self.AGENT_TALK_INTERVAL_S:
|
| 164 |
+
self._run_agent_conversation()
|
| 165 |
+
self._last_agent_talk = now
|
| 166 |
+
|
| 167 |
+
# 2. Skill creation from conversations (every SKILL_CREATION_INTERVAL_S)
|
| 168 |
+
if now - self._last_skill_creation > self.SKILL_CREATION_INTERVAL_S:
|
| 169 |
+
self._create_skills_from_conversations()
|
| 170 |
+
self._last_skill_creation = now
|
| 171 |
+
|
| 172 |
+
# 3. Self-refinement (every REFINEMENT_INTERVAL_S)
|
| 173 |
+
if now - self._last_refinement > self.REFINEMENT_INTERVAL_S:
|
| 174 |
+
result = self.refinement.refine_once()
|
| 175 |
+
if result.get("actions"):
|
| 176 |
+
self._stats["refinement_cycles"] += 1
|
| 177 |
+
self._last_refinement = now
|
| 178 |
+
|
| 179 |
+
# 4. Progress goals
|
| 180 |
+
self._progress_goals()
|
| 181 |
+
|
| 182 |
+
# 5. Check fast reply readiness
|
| 183 |
+
self._check_fast_reply_ready()
|
| 184 |
+
|
| 185 |
+
# 6. Multi-LLM conversation mesh — agents converse to build skills
|
| 186 |
+
if now - self._last_mesh > self.MESH_INTERVAL_S:
|
| 187 |
+
self._run_mesh_conversation()
|
| 188 |
+
self._last_mesh = now
|
| 189 |
+
|
| 190 |
+
# 7. 100-project mode — auto-generate and maintain projects
|
| 191 |
+
if now - self._last_project_check > self.PROJECT_CHECK_INTERVAL_S:
|
| 192 |
+
self._maintain_hundred_projects()
|
| 193 |
+
self._last_project_check = now
|
| 194 |
+
|
| 195 |
+
def _run_agent_conversation(self) -> None:
|
| 196 |
+
"""Have an agent talk to the LLM — creates training data and skills."""
|
| 197 |
+
topic = random.choice(self.AGENT_CONVERSATION_TOPICS)
|
| 198 |
+
|
| 199 |
+
# Pick a random agent to ask the question
|
| 200 |
+
agent_names = list(self.agent_manager.agents.keys())
|
| 201 |
+
asking_agent = random.choice(agent_names)
|
| 202 |
+
|
| 203 |
+
# Generate a response from the LLM
|
| 204 |
+
try:
|
| 205 |
+
prompt = f"Agent {asking_agent} asks: {topic}"
|
| 206 |
+
response = self.harness._agent_generate(prompt)
|
| 207 |
+
|
| 208 |
+
# Store in persistent memory
|
| 209 |
+
self.harness.persistent_memory.add_episodic(
|
| 210 |
+
"agent", f"{asking_agent}: {topic} → {response[:100]}",
|
| 211 |
+
channel="agent-self-talk", importance=0.4,
|
| 212 |
+
tags=["self-talk", asking_agent]
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# Store in recursive link graph
|
| 216 |
+
self.harness.link_graph.add_context(
|
| 217 |
+
topic, response, session_id="agent-self-talk", channel="agent"
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
self._stats["agent_conversations"] += 1
|
| 221 |
+
self._conversation_count += 1
|
| 222 |
+
|
| 223 |
+
logger.debug("Agent conversation #%d: %s asks '%s'", self._conversation_count, asking_agent, topic[:40])
|
| 224 |
+
|
| 225 |
+
except Exception as e:
|
| 226 |
+
logger.error("Agent conversation failed: %s", e)
|
| 227 |
+
|
| 228 |
+
def _create_skills_from_conversations(self) -> None:
|
| 229 |
+
"""Extract skills from recent agent conversations."""
|
| 230 |
+
try:
|
| 231 |
+
factory = self.harness.skill_factory
|
| 232 |
+
skill = factory.extract_skill()
|
| 233 |
+
if skill:
|
| 234 |
+
self.harness.skill_manager.create(skill)
|
| 235 |
+
self._stats["skills_created"] += 1
|
| 236 |
+
self._skills_created += 1
|
| 237 |
+
logger.info("Created skill from agent conversations: %s", skill.name)
|
| 238 |
+
|
| 239 |
+
# Also try meta-skill
|
| 240 |
+
meta = factory.maybe_create_meta_skill(self.harness.skill_manager)
|
| 241 |
+
if meta:
|
| 242 |
+
self.harness.skill_manager.create(meta)
|
| 243 |
+
self._stats["skills_created"] += 1
|
| 244 |
+
except Exception as e:
|
| 245 |
+
logger.debug("Skill creation from conversations: %s", e)
|
| 246 |
+
|
| 247 |
+
def _progress_goals(self) -> None:
|
| 248 |
+
"""Check if any goals can be progressed."""
|
| 249 |
+
try:
|
| 250 |
+
active = self.harness.goal_memory.get_active_goals()
|
| 251 |
+
pending = self.harness.goal_memory.list_goals(status="pending")
|
| 252 |
+
|
| 253 |
+
if pending and not active:
|
| 254 |
+
# Assign pending goals to agents
|
| 255 |
+
for goal in pending[:2]: # assign up to 2 at a time
|
| 256 |
+
self.harness.goal_memory.assign_agent(goal.id, "planner")
|
| 257 |
+
self._stats["goals_progressed"] += 1
|
| 258 |
+
logger.info("Assigned pending goal to planner: %s", goal.title)
|
| 259 |
+
|
| 260 |
+
except Exception as e:
|
| 261 |
+
logger.debug("Goal progression: %s", e)
|
| 262 |
+
|
| 263 |
+
def _check_fast_reply_ready(self) -> None:
|
| 264 |
+
"""Check if the system is ready for near-instant replies."""
|
| 265 |
+
try:
|
| 266 |
+
skill_stats = self.harness.skill_manager.get_stats()
|
| 267 |
+
total_skills = skill_stats.get("total_skills", 0)
|
| 268 |
+
|
| 269 |
+
cache = getattr(self.harness, 'fast_cache', None)
|
| 270 |
+
hit_rate = cache.get_hit_rate() if cache else 0.0
|
| 271 |
+
|
| 272 |
+
ready = (total_skills >= self.FAST_REPLY_SKILL_THRESHOLD and
|
| 273 |
+
hit_rate >= self.FAST_REPLY_HIT_RATE_THRESHOLD)
|
| 274 |
+
|
| 275 |
+
if ready and not self._stats["fast_reply_ready"]:
|
| 276 |
+
logger.info("Fast reply mode activated! %d skills, %.0f%% cache hit rate",
|
| 277 |
+
total_skills, hit_rate * 100)
|
| 278 |
+
|
| 279 |
+
self._stats["fast_reply_ready"] = ready
|
| 280 |
+
|
| 281 |
+
except Exception as e:
|
| 282 |
+
logger.debug("Fast reply check: %s", e)
|
| 283 |
+
|
| 284 |
+
def _run_mesh_conversation(self) -> None:
|
| 285 |
+
"""Run a multi-LLM conversation mesh — agents converse to build skills.
|
| 286 |
+
|
| 287 |
+
All 5 agents talk to each other on a topic, creating:
|
| 288 |
+
- Skill building pools (collaborative skills)
|
| 289 |
+
- New skill categories (auto-discovered)
|
| 290 |
+
- Cross-agent knowledge sharing
|
| 291 |
+
"""
|
| 292 |
+
try:
|
| 293 |
+
mesh = self.harness.conversation_mesh
|
| 294 |
+
result = mesh.run_conversation()
|
| 295 |
+
|
| 296 |
+
self._stats["mesh_conversations"] += 1
|
| 297 |
+
self._stats["mesh_categories_discovered"] += len(result.get("categories", []))
|
| 298 |
+
if result.get("skill_pool"):
|
| 299 |
+
self._stats["mesh_skills_pooled"] += 1
|
| 300 |
+
|
| 301 |
+
logger.info("Mesh conversation: %s on '%s' — %d messages, %d categories, skill=%s",
|
| 302 |
+
result.get("mode", "?"), result.get("topic", "?")[:40],
|
| 303 |
+
result.get("messages", 0), len(result.get("categories", [])),
|
| 304 |
+
result.get("skill_pool", False))
|
| 305 |
+
|
| 306 |
+
except Exception as e:
|
| 307 |
+
logger.debug("Mesh conversation failed: %s", e)
|
| 308 |
+
|
| 309 |
+
def _maintain_hundred_projects(self) -> None:
|
| 310 |
+
"""Maintain 100 active projects — auto-generate new ones when count drops.
|
| 311 |
+
|
| 312 |
+
Once the system has enough skills and knowledge, it enters
|
| 313 |
+
100-project mode and continuously generates and works on projects.
|
| 314 |
+
It never stops — as soon as one project completes, a new one is created.
|
| 315 |
+
"""
|
| 316 |
+
try:
|
| 317 |
+
goal_stats = self.harness.goal_memory.get_stats()
|
| 318 |
+
active_count = goal_stats.get("active", 0)
|
| 319 |
+
pending_count = goal_stats.get("pending", 0)
|
| 320 |
+
total_active = active_count + pending_count
|
| 321 |
+
|
| 322 |
+
# Check if we should enter 100-project mode
|
| 323 |
+
skill_stats = self.harness.skill_manager.get_stats()
|
| 324 |
+
total_skills = skill_stats.get("total_skills", 0)
|
| 325 |
+
|
| 326 |
+
if not self._hundred_project_mode:
|
| 327 |
+
if total_skills >= self.FAST_REPLY_SKILL_THRESHOLD:
|
| 328 |
+
self._hundred_project_mode = True
|
| 329 |
+
self._stats["hundred_project_mode"] = True
|
| 330 |
+
logger.info("Entering 100-project mode! %d skills accumulated. "
|
| 331 |
+
"Generating and working on projects 24/7.", total_skills)
|
| 332 |
+
|
| 333 |
+
if not self._hundred_project_mode:
|
| 334 |
+
return # Not ready yet — keep learning
|
| 335 |
+
|
| 336 |
+
# Generate new projects to maintain TARGET_PROJECT_COUNT
|
| 337 |
+
if total_active < self.TARGET_PROJECT_COUNT:
|
| 338 |
+
needed = self.TARGET_PROJECT_COUNT - total_active
|
| 339 |
+
generated = self._generate_projects(min(needed, 5)) # generate up to 5 at a time
|
| 340 |
+
self._stats["projects_generated"] += generated
|
| 341 |
+
self._projects_generated += generated
|
| 342 |
+
if generated > 0:
|
| 343 |
+
logger.info("Generated %d new projects (active: %d/%d)",
|
| 344 |
+
generated, total_active + generated, self.TARGET_PROJECT_COUNT)
|
| 345 |
+
|
| 346 |
+
# Track completed projects
|
| 347 |
+
completed = goal_stats.get("completed", 0)
|
| 348 |
+
if completed > self._projects_completed:
|
| 349 |
+
new_completions = completed - self._projects_completed
|
| 350 |
+
self._projects_completed = completed
|
| 351 |
+
self._stats["projects_completed"] = completed
|
| 352 |
+
logger.info("Projects completed: %d total (+%d new)", completed, new_completions)
|
| 353 |
+
|
| 354 |
+
except Exception as e:
|
| 355 |
+
logger.debug("100-project mode: %s", e)
|
| 356 |
+
|
| 357 |
+
def _generate_projects(self, count: int) -> int:
|
| 358 |
+
"""Generate new project goals automatically.
|
| 359 |
+
|
| 360 |
+
Projects are generated from a mix of:
|
| 361 |
+
- Template projects (improvement, optimization, learning)
|
| 362 |
+
- LLM-generated project ideas
|
| 363 |
+
- Skill-gap analysis (what skills are missing?)
|
| 364 |
+
"""
|
| 365 |
+
project_templates = [
|
| 366 |
+
("Optimize inference speed", "Analyze and optimize the LLM inference pipeline for faster responses", "high"),
|
| 367 |
+
("Improve memory recall", "Enhance the persistent memory system's recall accuracy and speed", "medium"),
|
| 368 |
+
("Create new skill: {topic}", "Develop a new skill for {topic} interactions", "medium"),
|
| 369 |
+
("Optimize tokenizer", "Improve tokenizer efficiency and vocabulary coverage", "medium"),
|
| 370 |
+
("Enhance agent coordination", "Improve how the 5 agents coordinate on multi-step goals", "high"),
|
| 371 |
+
("Build conversation cache", "Expand the fast reply cache for more instant responses", "high"),
|
| 372 |
+
("Refine quantization", "Experiment with quantization formats to improve accuracy/speed tradeoff", "medium"),
|
| 373 |
+
("Create tool: {topic}", "Build a new tool for {topic} operations", "medium"),
|
| 374 |
+
("Improve recursive links", "Enhance the recursive link graph for better context retrieval", "low"),
|
| 375 |
+
("Optimize skill matching", "Improve skill trigger matching for more accurate skill selection", "medium"),
|
| 376 |
+
("Expand semantic memory", "Extract more semantic facts from conversations", "low"),
|
| 377 |
+
("Enhance voice responses", "Improve TTS output formatting and voice adapter latency", "medium"),
|
| 378 |
+
("Build webhook integration", "Create webhook endpoints for external service notifications", "low"),
|
| 379 |
+
("Optimize image generation", "Improve image generation speed and quality", "low"),
|
| 380 |
+
("Create API connector", "Build a new API connector for external service integration", "medium"),
|
| 381 |
+
("Improve goal planning", "Enhance the goal planning system with better step decomposition", "medium"),
|
| 382 |
+
("Refine self-talk topics", "Generate better self-talk conversation topics for continuous learning", "low"),
|
| 383 |
+
("Build monitoring dashboard", "Create a monitoring dashboard for system stats and health", "medium"),
|
| 384 |
+
("Optimize SQLite queries", "Profile and optimize SQLite queries for memory and goal storage", "medium"),
|
| 385 |
+
("Enhance error handling", "Improve error handling and recovery across all system components", "high"),
|
| 386 |
+
]
|
| 387 |
+
|
| 388 |
+
topics = ["data processing", "code review", "text summarization", "pattern matching",
|
| 389 |
+
"cache optimization", "memory management", "search algorithms", "data compression",
|
| 390 |
+
"natural language", "math operations", "file handling", "network requests",
|
| 391 |
+
"image processing", "audio processing", "task scheduling", "resource monitoring"]
|
| 392 |
+
|
| 393 |
+
generated = 0
|
| 394 |
+
for i in range(count):
|
| 395 |
+
try:
|
| 396 |
+
template = random.choice(project_templates)
|
| 397 |
+
title = template[0].format(topic=random.choice(topics))
|
| 398 |
+
description = template[1].format(topic=random.choice(topics))
|
| 399 |
+
priority = template[2]
|
| 400 |
+
|
| 401 |
+
self.harness.create_goal(title, description, priority=priority,
|
| 402 |
+
tags=["auto-generated", "100-project"])
|
| 403 |
+
generated += 1
|
| 404 |
+
except Exception as e:
|
| 405 |
+
logger.debug("Project generation failed: %s", e)
|
| 406 |
+
break
|
| 407 |
+
|
| 408 |
+
return generated
|
| 409 |
+
|
| 410 |
+
def get_stats(self) -> dict[str, Any]:
|
| 411 |
+
return {
|
| 412 |
+
**self._stats,
|
| 413 |
+
"running": self._running,
|
| 414 |
+
"idle": (time.time() - self._last_user_activity) > self.IDLE_THRESHOLD_S,
|
| 415 |
+
"idle_time_s": round(time.time() - self._last_user_activity, 1),
|
| 416 |
+
"refinement": self.refinement.get_stats(),
|
| 417 |
+
"agents": self.agent_manager.get_stats(),
|
| 418 |
+
}
|
singularity_llm/agents/coder_agent.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Coder Agent — writes code, creates files, debugs.
|
| 2 |
+
|
| 3 |
+
Handles steps that involve writing code, creating files, or fixing bugs.
|
| 4 |
+
Can use the write_file and shell_exec tools.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import logging
|
| 10 |
+
import re
|
| 11 |
+
from typing import Any
|
| 12 |
+
|
| 13 |
+
from .agent_base import BaseAgent
|
| 14 |
+
from ..memory.goal_memory import Goal
|
| 15 |
+
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class CoderAgent(BaseAgent):
|
| 20 |
+
"""Writes code and creates files for goal steps."""
|
| 21 |
+
|
| 22 |
+
def __init__(self, goal_memory, persistent_memory=None, generate_fn=None):
|
| 23 |
+
super().__init__(
|
| 24 |
+
name="coder",
|
| 25 |
+
role="Code Writer",
|
| 26 |
+
description="Writes code, creates files, and debugs issues",
|
| 27 |
+
goal_memory=goal_memory,
|
| 28 |
+
persistent_memory=persistent_memory,
|
| 29 |
+
generate_fn=generate_fn,
|
| 30 |
+
poll_interval_s=2.0,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
def _can_handle(self, goal: Goal) -> bool:
|
| 34 |
+
"""Coder handles goals related to code."""
|
| 35 |
+
keywords = ["code", "function", "file", "implement", "write", "create", "build",
|
| 36 |
+
"debug", "fix", "bug", "program", "script", "module", "class"]
|
| 37 |
+
text = (goal.title + " " + goal.description).lower()
|
| 38 |
+
return any(kw in text for kw in keywords)
|
| 39 |
+
|
| 40 |
+
def process_goal(self, goal: Goal) -> dict[str, Any]:
|
| 41 |
+
"""Execute a coding step."""
|
| 42 |
+
if goal.current_step >= len(goal.steps):
|
| 43 |
+
return {"success": True, "output": "No more steps"}
|
| 44 |
+
|
| 45 |
+
step = goal.steps[goal.current_step]
|
| 46 |
+
prompt = (
|
| 47 |
+
f"You are a code writer agent. Execute this step:\n"
|
| 48 |
+
f"Goal: {goal.title}\n"
|
| 49 |
+
f"Step: {step['title']}\n"
|
| 50 |
+
f"Description: {step['description']}\n"
|
| 51 |
+
f"Write the code or create the file needed. Be concise.\n"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
response = self._generate(prompt)
|
| 55 |
+
|
| 56 |
+
# Extract file content from response
|
| 57 |
+
files_created = self._extract_and_save_files(response)
|
| 58 |
+
|
| 59 |
+
if files_created:
|
| 60 |
+
return {"success": True, "output": f"Created files: {', '.join(files_created)}"}
|
| 61 |
+
return {"success": True, "output": response[:200]}
|
| 62 |
+
|
| 63 |
+
def _extract_and_save_files(self, text: str) -> list[str]:
|
| 64 |
+
"""Extract file blocks from LLM output and save them."""
|
| 65 |
+
files = []
|
| 66 |
+
# Pattern: ```file: path\n content ```
|
| 67 |
+
pattern = r"```file:\s*(.+?)\n(.*?)```"
|
| 68 |
+
matches = re.findall(pattern, text, re.DOTALL)
|
| 69 |
+
|
| 70 |
+
for path, content in matches:
|
| 71 |
+
path = path.strip()
|
| 72 |
+
try:
|
| 73 |
+
import os
|
| 74 |
+
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
|
| 75 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 76 |
+
f.write(content.strip())
|
| 77 |
+
files.append(path)
|
| 78 |
+
logger.info("Coder created file: %s", path)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error("Failed to create file %s: %s", path, e)
|
| 81 |
+
|
| 82 |
+
return files
|
singularity_llm/agents/conversation_mesh.py
ADDED
|
@@ -0,0 +1,628 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Multi-LLM Conversation Mesh — agents converse with each other to build skills.
|
| 2 |
+
|
| 3 |
+
All 5 agents (planner, coder, researcher, reviewer, executor) can talk to
|
| 4 |
+
each other and to the LLM directly. These conversations create:
|
| 5 |
+
|
| 6 |
+
1. Skill Building Pools — shared pools of skills built collaboratively
|
| 7 |
+
- Agent A asks a question → Agent B answers → skill extracted from the exchange
|
| 8 |
+
- Multiple agents contribute to a single skill
|
| 9 |
+
- Skills are pooled by category and shared across all agents
|
| 10 |
+
|
| 11 |
+
2. Intelligence Expansion — each conversation generates new knowledge:
|
| 12 |
+
- New facts → semantic memory
|
| 13 |
+
- New patterns → skills
|
| 14 |
+
- New categories → auto-added to skill category list
|
| 15 |
+
|
| 16 |
+
3. Auto Skill Category Adder — dynamically discovers new categories:
|
| 17 |
+
- Analyzes conversation topics
|
| 18 |
+
- Extracts new category keywords
|
| 19 |
+
- Adds them to the skill category list automatically
|
| 20 |
+
- No hardcoded category list — categories grow organically
|
| 21 |
+
|
| 22 |
+
Conversation patterns:
|
| 23 |
+
- Round-robin: each agent talks in turn
|
| 24 |
+
- Pair-wise: two agents have a focused conversation
|
| 25 |
+
- Brainstorm: all agents contribute to a single topic
|
| 26 |
+
- Debate: agents argue different sides of a topic
|
| 27 |
+
- Teaching: one agent teaches a topic to others
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
from __future__ import annotations
|
| 31 |
+
|
| 32 |
+
import hashlib
|
| 33 |
+
import logging
|
| 34 |
+
import random
|
| 35 |
+
import threading
|
| 36 |
+
import time
|
| 37 |
+
from collections import defaultdict, deque
|
| 38 |
+
from dataclasses import dataclass, field
|
| 39 |
+
from typing import Any, Callable
|
| 40 |
+
|
| 41 |
+
logger = logging.getLogger(__name__)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@dataclass
|
| 45 |
+
class ConversationMessage:
|
| 46 |
+
"""A single message in a multi-LLM conversation."""
|
| 47 |
+
agent_name: str
|
| 48 |
+
content: str
|
| 49 |
+
timestamp: float = field(default_factory=time.time)
|
| 50 |
+
role: str = "speaker" # speaker, listener, summarizer
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@dataclass
|
| 54 |
+
class SkillPool:
|
| 55 |
+
"""A pool of skills built collaboratively by multiple agents."""
|
| 56 |
+
id: str
|
| 57 |
+
category: str
|
| 58 |
+
name: str
|
| 59 |
+
description: str
|
| 60 |
+
contributions: list[dict] = field(default_factory=list) # which agents contributed
|
| 61 |
+
skill_content: str = ""
|
| 62 |
+
confidence: float = 0.0
|
| 63 |
+
created_at: float = field(default_factory=time.time)
|
| 64 |
+
tags: list[str] = field(default_factory=list)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class AutoCategoryManager:
|
| 68 |
+
"""Automatically discovers and adds new skill categories.
|
| 69 |
+
|
| 70 |
+
Analyzes conversation topics and extracts new categories dynamically.
|
| 71 |
+
No hardcoded category list — categories grow organically based on
|
| 72 |
+
what the agents actually talk about.
|
| 73 |
+
|
| 74 |
+
Category discovery:
|
| 75 |
+
1. Extract keywords from conversation topics
|
| 76 |
+
2. Cluster similar keywords into categories
|
| 77 |
+
3. Add new categories to the skill system
|
| 78 |
+
4. Track category health (how many skills, how active)
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
# Seed categories — the list grows from here
|
| 82 |
+
SEED_CATEGORIES = [
|
| 83 |
+
"conversation", "code", "speed", "voice", "tool",
|
| 84 |
+
"math", "writing", "analysis", "debugging", "optimization",
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
def __init__(self) -> None:
|
| 88 |
+
self._categories: dict[str, dict] = {}
|
| 89 |
+
self._keyword_to_category: dict[str, str] = {}
|
| 90 |
+
self._category_keywords: dict[str, set[str]] = defaultdict(set)
|
| 91 |
+
self._stats = {
|
| 92 |
+
"categories_total": 0,
|
| 93 |
+
"categories_auto_added": 0,
|
| 94 |
+
"keywords_indexed": 0,
|
| 95 |
+
"category_lookups": 0,
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
# Initialize with seed categories
|
| 99 |
+
for cat in self.SEED_CATEGORIES:
|
| 100 |
+
self._add_category(cat, auto=False)
|
| 101 |
+
|
| 102 |
+
def _add_category(self, name: str, auto: bool = True) -> str:
|
| 103 |
+
"""Add a new category."""
|
| 104 |
+
name = name.lower().strip()
|
| 105 |
+
if name in self._categories:
|
| 106 |
+
return name
|
| 107 |
+
|
| 108 |
+
self._categories[name] = {
|
| 109 |
+
"name": name,
|
| 110 |
+
"auto_added": auto,
|
| 111 |
+
"skill_count": 0,
|
| 112 |
+
"conversation_count": 0,
|
| 113 |
+
"created_at": time.time(),
|
| 114 |
+
"last_active": time.time(),
|
| 115 |
+
}
|
| 116 |
+
self._stats["categories_total"] += 1
|
| 117 |
+
if auto:
|
| 118 |
+
self._stats["categories_auto_added"] += 1
|
| 119 |
+
logger.info("Added skill category: %s (auto=%s)", name, auto)
|
| 120 |
+
return name
|
| 121 |
+
|
| 122 |
+
def discover_from_topic(self, topic: str) -> list[str]:
|
| 123 |
+
"""Discover new categories from a conversation topic.
|
| 124 |
+
|
| 125 |
+
Extracts keywords, checks if they match existing categories,
|
| 126 |
+
and creates new categories for novel topics.
|
| 127 |
+
"""
|
| 128 |
+
words = self._extract_keywords(topic)
|
| 129 |
+
discovered: list[str] = []
|
| 130 |
+
|
| 131 |
+
for word in words:
|
| 132 |
+
# Check if this keyword maps to an existing category
|
| 133 |
+
if word in self._keyword_to_category:
|
| 134 |
+
cat = self._keyword_to_category[word]
|
| 135 |
+
self._categories[cat]["last_active"] = time.time()
|
| 136 |
+
self._categories[cat]["conversation_count"] += 1
|
| 137 |
+
discovered.append(cat)
|
| 138 |
+
continue
|
| 139 |
+
|
| 140 |
+
# Check if word is similar to an existing category
|
| 141 |
+
matched = self._match_existing_category(word)
|
| 142 |
+
if matched:
|
| 143 |
+
self._keyword_to_category[word] = matched
|
| 144 |
+
self._category_keywords[matched].add(word)
|
| 145 |
+
self._stats["keywords_indexed"] += 1
|
| 146 |
+
discovered.append(matched)
|
| 147 |
+
continue
|
| 148 |
+
|
| 149 |
+
# New category discovered!
|
| 150 |
+
cat = self._add_category(word, auto=True)
|
| 151 |
+
self._keyword_to_category[word] = cat
|
| 152 |
+
self._category_keywords[cat].add(word)
|
| 153 |
+
self._stats["keywords_indexed"] += 1
|
| 154 |
+
self._categories[cat]["conversation_count"] += 1
|
| 155 |
+
discovered.append(cat)
|
| 156 |
+
|
| 157 |
+
return discovered
|
| 158 |
+
|
| 159 |
+
def _extract_keywords(self, text: str) -> list[str]:
|
| 160 |
+
"""Extract meaningful keywords from text."""
|
| 161 |
+
import re
|
| 162 |
+
# Remove common stop words
|
| 163 |
+
stop_words = {
|
| 164 |
+
"the", "a", "an", "is", "are", "was", "were", "be", "been",
|
| 165 |
+
"have", "has", "had", "do", "does", "did", "will", "would",
|
| 166 |
+
"could", "should", "may", "might", "must", "can", "need",
|
| 167 |
+
"how", "what", "when", "where", "why", "who", "which",
|
| 168 |
+
"to", "of", "in", "on", "at", "by", "for", "with", "about",
|
| 169 |
+
"from", "as", "into", "through", "during", "before", "after",
|
| 170 |
+
"and", "or", "but", "not", "no", "yes", "if", "then", "else",
|
| 171 |
+
"this", "that", "these", "those", "i", "you", "he", "she",
|
| 172 |
+
"it", "we", "they", "me", "him", "her", "us", "them",
|
| 173 |
+
"my", "your", "his", "its", "our", "their",
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
words = re.findall(r'\b[a-z]{3,20}\b', text.lower())
|
| 177 |
+
keywords = [w for w in words if w not in stop_words and len(w) >= 4]
|
| 178 |
+
|
| 179 |
+
# Limit to top 5 unique keywords
|
| 180 |
+
seen = set()
|
| 181 |
+
result = []
|
| 182 |
+
for w in keywords:
|
| 183 |
+
if w not in seen:
|
| 184 |
+
seen.add(w)
|
| 185 |
+
result.append(w)
|
| 186 |
+
if len(result) >= 5:
|
| 187 |
+
break
|
| 188 |
+
|
| 189 |
+
return result
|
| 190 |
+
|
| 191 |
+
def _match_existing_category(self, keyword: str) -> str | None:
|
| 192 |
+
"""Check if a keyword is similar to an existing category."""
|
| 193 |
+
# Simple prefix/suffix matching
|
| 194 |
+
for cat in self._categories:
|
| 195 |
+
if keyword.startswith(cat) or cat.startswith(keyword):
|
| 196 |
+
return cat
|
| 197 |
+
if keyword.endswith(cat) or cat.endswith(keyword):
|
| 198 |
+
return cat
|
| 199 |
+
|
| 200 |
+
# Check if keyword is in any category's keyword set
|
| 201 |
+
for cat, keywords in self._category_keywords.items():
|
| 202 |
+
if keyword in keywords:
|
| 203 |
+
return cat
|
| 204 |
+
|
| 205 |
+
return None
|
| 206 |
+
|
| 207 |
+
def get_categories(self) -> list[str]:
|
| 208 |
+
"""Get all category names."""
|
| 209 |
+
return list(self._categories.keys())
|
| 210 |
+
|
| 211 |
+
def get_auto_categories(self) -> list[str]:
|
| 212 |
+
"""Get only auto-discovered categories."""
|
| 213 |
+
return [name for name, info in self._categories.items() if info["auto_added"]]
|
| 214 |
+
|
| 215 |
+
def increment_skill_count(self, category: str) -> None:
|
| 216 |
+
"""Increment the skill count for a category."""
|
| 217 |
+
if category in self._categories:
|
| 218 |
+
self._categories[category]["skill_count"] += 1
|
| 219 |
+
self._categories[category]["last_active"] = time.time()
|
| 220 |
+
|
| 221 |
+
def get_category_info(self, category: str) -> dict | None:
|
| 222 |
+
"""Get info about a specific category."""
|
| 223 |
+
return self._categories.get(category)
|
| 224 |
+
|
| 225 |
+
def get_stats(self) -> dict[str, Any]:
|
| 226 |
+
return {
|
| 227 |
+
**self._stats,
|
| 228 |
+
"categories": dict(self._categories),
|
| 229 |
+
"seed_categories": self.SEED_CATEGORIES,
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
class ConversationMesh:
|
| 234 |
+
"""Multi-LLM conversation mesh — agents converse to build skills.
|
| 235 |
+
|
| 236 |
+
All 5 agents can talk to each other and to the LLM. Conversations
|
| 237 |
+
create skill building pools and discover new skill categories.
|
| 238 |
+
|
| 239 |
+
Conversation modes:
|
| 240 |
+
- round_robin: each agent speaks in turn on a topic
|
| 241 |
+
- pairwise: two agents have a focused discussion
|
| 242 |
+
- brainstorm: all agents contribute ideas on a topic
|
| 243 |
+
- debate: agents argue different perspectives
|
| 244 |
+
- teaching: one agent teaches a topic to others
|
| 245 |
+
"""
|
| 246 |
+
|
| 247 |
+
CONVERSATION_MODES = ["round_robin", "pairwise", "brainstorm", "debate", "teaching"]
|
| 248 |
+
|
| 249 |
+
BRAINSTORM_TOPICS = [
|
| 250 |
+
"How to improve code quality",
|
| 251 |
+
"Best practices for data processing",
|
| 252 |
+
"Optimizing memory usage in AI systems",
|
| 253 |
+
"New approaches to natural language understanding",
|
| 254 |
+
"Efficient algorithms for pattern matching",
|
| 255 |
+
"Strategies for error handling and recovery",
|
| 256 |
+
"Building better user interfaces",
|
| 257 |
+
"Improving search and retrieval accuracy",
|
| 258 |
+
"Techniques for faster inference",
|
| 259 |
+
"Methods for continuous learning",
|
| 260 |
+
"Approaches to multi-agent coordination",
|
| 261 |
+
"Enhancing creativity in problem solving",
|
| 262 |
+
"Building robust testing frameworks",
|
| 263 |
+
"Optimizing database queries",
|
| 264 |
+
"Improving API design patterns",
|
| 265 |
+
"Strategies for handling concurrency",
|
| 266 |
+
"Methods for data compression",
|
| 267 |
+
"Approaches to security and privacy",
|
| 268 |
+
"Techniques for code refactoring",
|
| 269 |
+
"Building scalable architectures",
|
| 270 |
+
"Improving error messages and debugging",
|
| 271 |
+
"Optimizing network communication",
|
| 272 |
+
"Strategies for resource management",
|
| 273 |
+
"Methods for adaptive learning",
|
| 274 |
+
"Approaches to self-improvement",
|
| 275 |
+
]
|
| 276 |
+
|
| 277 |
+
def __init__(self, harness: Any) -> None:
|
| 278 |
+
self.harness = harness
|
| 279 |
+
self.agent_names = ["planner", "coder", "researcher", "reviewer", "executor"]
|
| 280 |
+
self.category_manager = AutoCategoryManager()
|
| 281 |
+
self._skill_pools: dict[str, SkillPool] = {}
|
| 282 |
+
self._conversations: deque[dict] = deque(maxlen=100)
|
| 283 |
+
self._cascade_depth = 0 # track cascade depth to prevent infinite recursion
|
| 284 |
+
self._stats = {
|
| 285 |
+
"conversations_total": 0,
|
| 286 |
+
"skills_pooled": 0,
|
| 287 |
+
"categories_discovered": 0,
|
| 288 |
+
"messages_exchanged": 0,
|
| 289 |
+
"cross_agent_skills": 0,
|
| 290 |
+
"cascade_depth": 0,
|
| 291 |
+
"cascade_pools_created": 0,
|
| 292 |
+
"cascade_conversations": 0,
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
def run_conversation(self, mode: str = "", topic: str = "") -> dict[str, Any]:
|
| 296 |
+
"""Run a multi-LLM conversation.
|
| 297 |
+
|
| 298 |
+
Args:
|
| 299 |
+
mode: conversation mode (round_robin, pairwise, brainstorm, debate, teaching)
|
| 300 |
+
topic: conversation topic (random if not provided)
|
| 301 |
+
Returns:
|
| 302 |
+
Summary of the conversation and skills created
|
| 303 |
+
"""
|
| 304 |
+
mode = mode or random.choice(self.CONVERSATION_MODES)
|
| 305 |
+
topic = topic or random.choice(self.BRAINSTORM_TOPICS)
|
| 306 |
+
|
| 307 |
+
# Discover categories from the topic
|
| 308 |
+
categories = self.category_manager.discover_from_topic(topic)
|
| 309 |
+
if categories:
|
| 310 |
+
self._stats["categories_discovered"] += len(categories)
|
| 311 |
+
|
| 312 |
+
# Run the conversation based on mode
|
| 313 |
+
if mode == "round_robin":
|
| 314 |
+
messages = self._round_robin(topic)
|
| 315 |
+
elif mode == "pairwise":
|
| 316 |
+
messages = self._pairwise(topic)
|
| 317 |
+
elif mode == "brainstorm":
|
| 318 |
+
messages = self._brainstorm(topic)
|
| 319 |
+
elif mode == "debate":
|
| 320 |
+
messages = self._debate(topic)
|
| 321 |
+
elif mode == "teaching":
|
| 322 |
+
messages = self._teaching(topic)
|
| 323 |
+
else:
|
| 324 |
+
messages = self._round_robin(topic)
|
| 325 |
+
|
| 326 |
+
# Build skill pool from the conversation
|
| 327 |
+
pool = self._build_skill_pool(topic, messages, categories)
|
| 328 |
+
|
| 329 |
+
# Cascade: when a skill pool is built, auto-generate related skill pools
|
| 330 |
+
cascade_results = []
|
| 331 |
+
if pool and self._cascade_depth < 2:
|
| 332 |
+
cascade_results = self._cascade_related_skills(topic, categories, max_depth=2)
|
| 333 |
+
|
| 334 |
+
# Store conversation
|
| 335 |
+
conv_record = {
|
| 336 |
+
"mode": mode,
|
| 337 |
+
"topic": topic,
|
| 338 |
+
"categories": categories,
|
| 339 |
+
"messages": [{"agent": m.agent_name, "content": m.content[:100]} for m in messages],
|
| 340 |
+
"skill_pool_id": pool.id if pool else None,
|
| 341 |
+
"timestamp": time.time(),
|
| 342 |
+
}
|
| 343 |
+
self._conversations.append(conv_record)
|
| 344 |
+
self._stats["conversations_total"] += 1
|
| 345 |
+
self._stats["messages_exchanged"] += len(messages)
|
| 346 |
+
|
| 347 |
+
# Store in persistent memory
|
| 348 |
+
if self.harness:
|
| 349 |
+
try:
|
| 350 |
+
self.harness.persistent_memory.add_episodic(
|
| 351 |
+
"mesh",
|
| 352 |
+
f"[{mode}] {topic}: {len(messages)} messages, categories: {categories}",
|
| 353 |
+
channel="mesh", importance=0.5,
|
| 354 |
+
tags=["mesh", mode] + categories
|
| 355 |
+
)
|
| 356 |
+
except Exception:
|
| 357 |
+
pass
|
| 358 |
+
|
| 359 |
+
return {
|
| 360 |
+
"mode": mode,
|
| 361 |
+
"topic": topic,
|
| 362 |
+
"categories": categories,
|
| 363 |
+
"messages": len(messages),
|
| 364 |
+
"skill_pool": pool is not None,
|
| 365 |
+
"pool_id": pool.id if pool else None,
|
| 366 |
+
"cascaded": cascade_results,
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
def _cascade_related_skills(self, topic: str, categories: list[str],
|
| 370 |
+
max_depth: int = 3) -> list[dict]:
|
| 371 |
+
"""When a skill pool is built, automatically generate related skill pools.
|
| 372 |
+
|
| 373 |
+
For each new skill pool, the system:
|
| 374 |
+
1. Generates related topics based on the categories
|
| 375 |
+
2. Runs conversations on those related topics
|
| 376 |
+
3. Builds new skill pools from those conversations
|
| 377 |
+
4. Cascades further (up to max_depth) if new categories are discovered
|
| 378 |
+
|
| 379 |
+
This creates a branching tree of related skills — one conversation
|
| 380 |
+
on "database optimization" cascades into skills on "query performance",
|
| 381 |
+
"indexing strategies", "cache invalidation", etc.
|
| 382 |
+
|
| 383 |
+
Runs during idle time via the always-on daemon.
|
| 384 |
+
"""
|
| 385 |
+
if self._cascade_depth >= max_depth:
|
| 386 |
+
return []
|
| 387 |
+
|
| 388 |
+
self._cascade_depth += 1
|
| 389 |
+
self._stats["cascade_depth"] = max(self._stats["cascade_depth"], self._cascade_depth)
|
| 390 |
+
|
| 391 |
+
# Generate related topics from the categories
|
| 392 |
+
related_topics = self._generate_related_topics(topic, categories)
|
| 393 |
+
cascade_results = []
|
| 394 |
+
|
| 395 |
+
for related_topic in related_topics:
|
| 396 |
+
try:
|
| 397 |
+
# Run a conversation on the related topic (different mode for variety)
|
| 398 |
+
mode = random.choice(self.CONVERSATION_MODES)
|
| 399 |
+
result = self.run_conversation(mode=mode, topic=related_topic)
|
| 400 |
+
|
| 401 |
+
if result.get("skill_pool"):
|
| 402 |
+
self._stats["cascade_pools_created"] += 1
|
| 403 |
+
self._stats["cascade_conversations"] += 1
|
| 404 |
+
cascade_results.append({
|
| 405 |
+
"topic": related_topic,
|
| 406 |
+
"mode": mode,
|
| 407 |
+
"pool_id": result.get("pool_id"),
|
| 408 |
+
"categories": result.get("categories", []),
|
| 409 |
+
})
|
| 410 |
+
|
| 411 |
+
# Check if new categories were discovered → cascade further
|
| 412 |
+
new_cats = [c for c in result.get("categories", []) if c not in categories]
|
| 413 |
+
if new_cats:
|
| 414 |
+
deeper = self._cascade_related_skills(related_topic, new_cats, max_depth)
|
| 415 |
+
cascade_results.extend(deeper)
|
| 416 |
+
|
| 417 |
+
except Exception as e:
|
| 418 |
+
logger.debug("Cascade conversation failed: %s", e)
|
| 419 |
+
|
| 420 |
+
self._cascade_depth -= 1
|
| 421 |
+
return cascade_results
|
| 422 |
+
|
| 423 |
+
def _generate_related_topics(self, topic: str, categories: list[str]) -> list[str]:
|
| 424 |
+
"""Generate related topics from a topic and its categories.
|
| 425 |
+
|
| 426 |
+
Uses category keywords to create variations and deeper explorations.
|
| 427 |
+
"""
|
| 428 |
+
related = []
|
| 429 |
+
|
| 430 |
+
# Topic variations based on categories
|
| 431 |
+
for cat in categories[:3]: # top 3 categories
|
| 432 |
+
related.extend([
|
| 433 |
+
f"Advanced {cat} techniques for {topic.lower()[:30]}",
|
| 434 |
+
f"Common pitfalls in {cat} when working with {topic.lower()[:20]}",
|
| 435 |
+
f"Best practices for {cat} in real-world scenarios",
|
| 436 |
+
f"Optimizing {cat} performance and reliability",
|
| 437 |
+
f"Testing strategies for {cat} implementations",
|
| 438 |
+
])
|
| 439 |
+
|
| 440 |
+
# Cross-category topics
|
| 441 |
+
if len(categories) >= 2:
|
| 442 |
+
related.append(f"Integrating {categories[0]} with {categories[1]}")
|
| 443 |
+
related.append(f"Trade-offs between {categories[0]} and {categories[1]}")
|
| 444 |
+
|
| 445 |
+
# Deduplicate and limit
|
| 446 |
+
seen = set()
|
| 447 |
+
unique = []
|
| 448 |
+
for t in related:
|
| 449 |
+
t_lower = t.lower()
|
| 450 |
+
if t_lower not in seen:
|
| 451 |
+
seen.add(t_lower)
|
| 452 |
+
unique.append(t)
|
| 453 |
+
return unique[:3] # limit to 3 related topics per cascade level
|
| 454 |
+
|
| 455 |
+
def _generate_response(self, agent_name: str, topic: str, context: str = "") -> str:
|
| 456 |
+
"""Generate a response from an agent."""
|
| 457 |
+
prompt = f"Agent {agent_name} discusses: {topic}"
|
| 458 |
+
if context:
|
| 459 |
+
prompt += f"\nContext: {context[:200]}"
|
| 460 |
+
try:
|
| 461 |
+
if self.harness:
|
| 462 |
+
return self.harness._agent_generate(prompt)[:200]
|
| 463 |
+
except Exception:
|
| 464 |
+
pass
|
| 465 |
+
return f"{agent_name}: I think {topic.lower()[:50]} is important to explore."
|
| 466 |
+
|
| 467 |
+
def _round_robin(self, topic: str) -> list[ConversationMessage]:
|
| 468 |
+
"""Each agent speaks in turn on the topic."""
|
| 469 |
+
messages = []
|
| 470 |
+
context = ""
|
| 471 |
+
for agent in self.agent_names:
|
| 472 |
+
response = self._generate_response(agent, topic, context)
|
| 473 |
+
msg = ConversationMessage(agent_name=agent, content=response)
|
| 474 |
+
messages.append(msg)
|
| 475 |
+
context = f"{agent} said: {response[:100]}"
|
| 476 |
+
return messages
|
| 477 |
+
|
| 478 |
+
def _pairwise(self, topic: str) -> list[ConversationMessage]:
|
| 479 |
+
"""Two agents have a focused discussion."""
|
| 480 |
+
messages = []
|
| 481 |
+
a1, a2 = random.sample(self.agent_names, 2)
|
| 482 |
+
context = ""
|
| 483 |
+
for i in range(4): # 2 exchanges each
|
| 484 |
+
speaker = a1 if i % 2 == 0 else a2
|
| 485 |
+
response = self._generate_response(speaker, topic, context)
|
| 486 |
+
msg = ConversationMessage(agent_name=speaker, content=response)
|
| 487 |
+
messages.append(msg)
|
| 488 |
+
context = f"{speaker} said: {response[:100]}"
|
| 489 |
+
return messages
|
| 490 |
+
|
| 491 |
+
def _brainstorm(self, topic: str) -> list[ConversationMessage]:
|
| 492 |
+
"""All agents contribute ideas on a topic."""
|
| 493 |
+
messages = []
|
| 494 |
+
for agent in self.agent_names:
|
| 495 |
+
prompt = f"Brainstorm ideas for: {topic}"
|
| 496 |
+
response = self._generate_response(agent, topic, "")
|
| 497 |
+
msg = ConversationMessage(agent_name=agent, content=response, role="speaker")
|
| 498 |
+
messages.append(msg)
|
| 499 |
+
# Summarizer
|
| 500 |
+
summary = f"Summary: We discussed {topic} and generated {len(messages)} ideas."
|
| 501 |
+
msg = ConversationMessage(agent_name="reviewer", content=summary, role="summarizer")
|
| 502 |
+
messages.append(msg)
|
| 503 |
+
return messages
|
| 504 |
+
|
| 505 |
+
def _debate(self, topic: str) -> list[ConversationMessage]:
|
| 506 |
+
"""Agents argue different perspectives on a topic."""
|
| 507 |
+
messages = []
|
| 508 |
+
for i, agent in enumerate(self.agent_names):
|
| 509 |
+
side = "for" if i % 2 == 0 else "against"
|
| 510 |
+
prompt = f"Argue {side}: {topic}"
|
| 511 |
+
response = self._generate_response(agent, prompt, "")
|
| 512 |
+
msg = ConversationMessage(agent_name=agent, content=response)
|
| 513 |
+
messages.append(msg)
|
| 514 |
+
return messages
|
| 515 |
+
|
| 516 |
+
def _teaching(self, topic: str) -> list[ConversationMessage]:
|
| 517 |
+
"""One agent teaches a topic to others."""
|
| 518 |
+
messages = []
|
| 519 |
+
teacher = random.choice(self.agent_names)
|
| 520 |
+
# Teacher explains
|
| 521 |
+
response = self._generate_response(teacher, f"Teach: {topic}", "")
|
| 522 |
+
messages.append(ConversationMessage(agent_name=teacher, content=response, role="speaker"))
|
| 523 |
+
# Students ask questions
|
| 524 |
+
students = [a for a in self.agent_names if a != teacher]
|
| 525 |
+
for student in students[:3]:
|
| 526 |
+
question = self._generate_response(student, f"Question about: {topic}", response[:100])
|
| 527 |
+
messages.append(ConversationMessage(agent_name=student, content=question, role="listener"))
|
| 528 |
+
return messages
|
| 529 |
+
|
| 530 |
+
def _build_skill_pool(self, topic: str, messages: list[ConversationMessage],
|
| 531 |
+
categories: list[str]) -> SkillPool | None:
|
| 532 |
+
"""Build a skill pool from a conversation.
|
| 533 |
+
|
| 534 |
+
A skill pool is a collaborative skill built from multiple agent contributions.
|
| 535 |
+
"""
|
| 536 |
+
if not messages or not categories:
|
| 537 |
+
return None
|
| 538 |
+
|
| 539 |
+
# Combine all messages into skill content
|
| 540 |
+
content_parts = [f"Skill Pool: {topic}"]
|
| 541 |
+
content_parts.append(f"Category: {categories[0]}")
|
| 542 |
+
content_parts.append(f"Contributors: {', '.join(set(m.agent_name for m in messages))}")
|
| 543 |
+
content_parts.append("Knowledge:")
|
| 544 |
+
for msg in messages:
|
| 545 |
+
content_parts.append(f" [{msg.agent_name}] {msg.content[:150]}")
|
| 546 |
+
content_parts.append("Best practice: Apply this knowledge collaboratively.")
|
| 547 |
+
|
| 548 |
+
skill_content = "\n".join(content_parts)
|
| 549 |
+
|
| 550 |
+
# Create pool
|
| 551 |
+
pool_id = hashlib.sha256(f"{topic}:{time.time()}".encode()).hexdigest()[:16]
|
| 552 |
+
category = categories[0]
|
| 553 |
+
|
| 554 |
+
pool = SkillPool(
|
| 555 |
+
id=pool_id,
|
| 556 |
+
category=category,
|
| 557 |
+
name=f"{category}-pool-{pool_id[:8]}",
|
| 558 |
+
description=f"Collaborative skill pool for {topic[:80]}",
|
| 559 |
+
contributions=[
|
| 560 |
+
{"agent": m.agent_name, "content": m.content[:100], "role": m.role}
|
| 561 |
+
for m in messages
|
| 562 |
+
],
|
| 563 |
+
skill_content=skill_content,
|
| 564 |
+
confidence=min(0.9, len(messages) / 10.0),
|
| 565 |
+
tags=categories,
|
| 566 |
+
)
|
| 567 |
+
|
| 568 |
+
self._skill_pools[pool_id] = pool
|
| 569 |
+
self._stats["skills_pooled"] += 1
|
| 570 |
+
|
| 571 |
+
# Count cross-agent skills (skills where multiple agents contributed)
|
| 572 |
+
unique_agents = set(m.agent_name for m in messages)
|
| 573 |
+
if len(unique_agents) >= 2:
|
| 574 |
+
self._stats["cross_agent_skills"] += 1
|
| 575 |
+
|
| 576 |
+
# Update category skill count
|
| 577 |
+
self.category_manager.increment_skill_count(category)
|
| 578 |
+
|
| 579 |
+
# Try to create an actual skill in the skill manager
|
| 580 |
+
if self.harness:
|
| 581 |
+
try:
|
| 582 |
+
from ..skills.skills import Skill
|
| 583 |
+
skill = Skill(
|
| 584 |
+
id=pool_id,
|
| 585 |
+
name=pool.name,
|
| 586 |
+
description=pool.description,
|
| 587 |
+
content=pool.skill_content[:500],
|
| 588 |
+
category=category,
|
| 589 |
+
trigger_conditions=categories + [topic.lower()[:20]],
|
| 590 |
+
effectiveness_score=pool.confidence,
|
| 591 |
+
confidence=pool.confidence,
|
| 592 |
+
)
|
| 593 |
+
self.harness.skill_manager.create(skill)
|
| 594 |
+
except Exception as e:
|
| 595 |
+
logger.debug("Skill creation from pool failed: %s", e)
|
| 596 |
+
|
| 597 |
+
return pool
|
| 598 |
+
|
| 599 |
+
def get_skill_pools(self) -> list[dict]:
|
| 600 |
+
"""Get all skill pools."""
|
| 601 |
+
return [
|
| 602 |
+
{
|
| 603 |
+
"id": p.id,
|
| 604 |
+
"name": p.name,
|
| 605 |
+
"category": p.category,
|
| 606 |
+
"description": p.description,
|
| 607 |
+
"contributors": list(set(c["agent"] for c in p.contributions)),
|
| 608 |
+
"confidence": p.confidence,
|
| 609 |
+
"tags": p.tags,
|
| 610 |
+
}
|
| 611 |
+
for p in self._skill_pools.values()
|
| 612 |
+
]
|
| 613 |
+
|
| 614 |
+
def get_categories(self) -> list[str]:
|
| 615 |
+
"""Get all skill categories (including auto-discovered)."""
|
| 616 |
+
return self.category_manager.get_categories()
|
| 617 |
+
|
| 618 |
+
def get_auto_categories(self) -> list[str]:
|
| 619 |
+
"""Get auto-discovered categories."""
|
| 620 |
+
return self.category_manager.get_auto_categories()
|
| 621 |
+
|
| 622 |
+
def get_stats(self) -> dict[str, Any]:
|
| 623 |
+
return {
|
| 624 |
+
**self._stats,
|
| 625 |
+
"skill_pools": len(self._skill_pools),
|
| 626 |
+
"categories": self.category_manager.get_stats(),
|
| 627 |
+
"conversations_buffered": len(self._conversations),
|
| 628 |
+
}
|
singularity_llm/agents/executor_agent.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Executor Agent — executes commands, runs tools, deploys.
|
| 2 |
+
|
| 3 |
+
Handles steps that involve running commands, executing tools, or
|
| 4 |
+
performing actions. Uses the tool registry for execution.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import logging
|
| 10 |
+
from typing import Any
|
| 11 |
+
|
| 12 |
+
from .agent_base import BaseAgent
|
| 13 |
+
from ..memory.goal_memory import Goal
|
| 14 |
+
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class ExecutorAgent(BaseAgent):
|
| 19 |
+
"""Executes commands and runs tools for goal steps."""
|
| 20 |
+
|
| 21 |
+
def __init__(self, goal_memory, persistent_memory=None, generate_fn=None,
|
| 22 |
+
tool_registry=None):
|
| 23 |
+
super().__init__(
|
| 24 |
+
name="executor",
|
| 25 |
+
role="Task Executor",
|
| 26 |
+
description="Executes commands, runs tools, and performs actions",
|
| 27 |
+
goal_memory=goal_memory,
|
| 28 |
+
persistent_memory=persistent_memory,
|
| 29 |
+
generate_fn=generate_fn,
|
| 30 |
+
poll_interval_s=2.0,
|
| 31 |
+
)
|
| 32 |
+
self._tool_registry = tool_registry
|
| 33 |
+
|
| 34 |
+
def _can_handle(self, goal: Goal) -> bool:
|
| 35 |
+
"""Executor handles goals related to execution/deployment."""
|
| 36 |
+
keywords = ["execute", "run", "deploy", "install", "test", "build", "start",
|
| 37 |
+
"stop", "configure", "setup", "launch", "perform", "do"]
|
| 38 |
+
text = (goal.title + " " + goal.description).lower()
|
| 39 |
+
return any(kw in text for kw in keywords)
|
| 40 |
+
|
| 41 |
+
def process_goal(self, goal: Goal) -> dict[str, Any]:
|
| 42 |
+
"""Execute a step using tools or commands."""
|
| 43 |
+
if goal.current_step >= len(goal.steps):
|
| 44 |
+
return {"success": True, "output": "No more steps"}
|
| 45 |
+
|
| 46 |
+
step = goal.steps[goal.current_step]
|
| 47 |
+
tool_name = step.get("tool", "")
|
| 48 |
+
|
| 49 |
+
# If a specific tool is specified, use it
|
| 50 |
+
if tool_name and self._tool_registry:
|
| 51 |
+
tool = self._tool_registry.get(tool_name)
|
| 52 |
+
if tool:
|
| 53 |
+
result = self._tool_registry.execute(tool_name, step.get("description", ""))
|
| 54 |
+
if result.success:
|
| 55 |
+
return {"success": True, "output": result.output[:200]}
|
| 56 |
+
else:
|
| 57 |
+
return {"success": False, "output": "", "error": result.error}
|
| 58 |
+
|
| 59 |
+
# Otherwise, use LLM to generate execution plan
|
| 60 |
+
prompt = (
|
| 61 |
+
f"You are an execution agent. Execute this step:\n"
|
| 62 |
+
f"Goal: {goal.title}\n"
|
| 63 |
+
f"Step: {step['title']}\n"
|
| 64 |
+
f"Description: {step['description']}\n"
|
| 65 |
+
f"Execute the step and report the result. Be concise.\n"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
response = self._generate(prompt)
|
| 69 |
+
|
| 70 |
+
# Try to extract and execute tool calls from response
|
| 71 |
+
if self._tool_registry and "[TOOL:" in response:
|
| 72 |
+
from ..harness.tools import tool_loop, parse_tool_calls
|
| 73 |
+
final_text, tool_results = tool_loop(response, self._tool_registry, max_rounds=3)
|
| 74 |
+
if tool_results:
|
| 75 |
+
outputs = [r.output[:100] for r in tool_results if r.success]
|
| 76 |
+
if outputs:
|
| 77 |
+
return {"success": True, "output": "; ".join(outputs)}
|
| 78 |
+
|
| 79 |
+
return {"success": True, "output": response[:200]}
|
singularity_llm/agents/planner_agent.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Planner Agent — breaks goals into actionable steps.
|
| 2 |
+
|
| 3 |
+
Takes a goal description and generates a step-by-step execution plan.
|
| 4 |
+
Can also replan when steps fail.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
+
import logging
|
| 11 |
+
from typing import Any
|
| 12 |
+
|
| 13 |
+
from .agent_base import BaseAgent
|
| 14 |
+
from ..memory.goal_memory import Goal
|
| 15 |
+
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class PlannerAgent(BaseAgent):
|
| 20 |
+
"""Plans goals — breaks them into steps and sub-goals."""
|
| 21 |
+
|
| 22 |
+
def __init__(self, goal_memory, persistent_memory=None, generate_fn=None):
|
| 23 |
+
super().__init__(
|
| 24 |
+
name="planner",
|
| 25 |
+
role="Goal Planner",
|
| 26 |
+
description="Breaks goals into actionable steps and sub-goals",
|
| 27 |
+
goal_memory=goal_memory,
|
| 28 |
+
persistent_memory=persistent_memory,
|
| 29 |
+
generate_fn=generate_fn,
|
| 30 |
+
poll_interval_s=3.0,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
def _can_handle(self, goal: Goal) -> bool:
|
| 34 |
+
"""Planner handles goals that need planning."""
|
| 35 |
+
return goal.status in ("pending", "planning")
|
| 36 |
+
|
| 37 |
+
def process_goal(self, goal: Goal) -> dict[str, Any]:
|
| 38 |
+
"""Plan a goal by generating steps via LLM."""
|
| 39 |
+
if goal.status == "pending" or (goal.status == "planning" and not goal.steps):
|
| 40 |
+
prompt = (
|
| 41 |
+
f"{self.goal_memory.PLANNING_PROMPT}\n\n"
|
| 42 |
+
f"GOAL:\nTitle: {goal.title}\n"
|
| 43 |
+
f"Description: {goal.description}\n"
|
| 44 |
+
f"Priority: {goal.priority}\n"
|
| 45 |
+
)
|
| 46 |
+
response = self._generate(prompt)
|
| 47 |
+
|
| 48 |
+
# Parse the plan
|
| 49 |
+
plan = self._safe_json_parse(response, {"steps": [], "sub_goals": []})
|
| 50 |
+
|
| 51 |
+
if plan.get("steps"):
|
| 52 |
+
result = self.goal_memory.plan_goal(
|
| 53 |
+
goal.id,
|
| 54 |
+
steps=plan["steps"],
|
| 55 |
+
sub_goals=plan.get("sub_goals", []),
|
| 56 |
+
plan_type="initial",
|
| 57 |
+
)
|
| 58 |
+
return {"success": True, "output": f"Planned {len(plan['steps'])} steps"}
|
| 59 |
+
else:
|
| 60 |
+
# Fallback: create a single step
|
| 61 |
+
self.goal_memory.plan_goal(
|
| 62 |
+
goal.id,
|
| 63 |
+
steps=[{"title": "Execute goal", "description": goal.description, "tool": ""}],
|
| 64 |
+
plan_type="initial",
|
| 65 |
+
)
|
| 66 |
+
return {"success": True, "output": "Planned with single fallback step"}
|
| 67 |
+
|
| 68 |
+
return {"success": True, "output": "Goal already planned"}
|
| 69 |
+
|
| 70 |
+
@staticmethod
|
| 71 |
+
def _safe_json_parse(text: str, fallback: dict) -> dict:
|
| 72 |
+
try:
|
| 73 |
+
start = text.find("{")
|
| 74 |
+
end = text.rfind("}") + 1
|
| 75 |
+
if start >= 0 and end > start:
|
| 76 |
+
return json.loads(text[start:end])
|
| 77 |
+
except Exception:
|
| 78 |
+
pass
|
| 79 |
+
return fallback
|
singularity_llm/agents/researcher_agent.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Researcher Agent — gathers information and analyzes topics.
|
| 2 |
+
|
| 3 |
+
Handles steps that involve research, analysis, or information gathering.
|
| 4 |
+
Uses the LLM to generate insights and summaries.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import logging
|
| 10 |
+
from typing import Any
|
| 11 |
+
|
| 12 |
+
from .agent_base import BaseAgent
|
| 13 |
+
from ..memory.goal_memory import Goal
|
| 14 |
+
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class ResearcherAgent(BaseAgent):
|
| 19 |
+
"""Researches topics and gathers information for goal steps."""
|
| 20 |
+
|
| 21 |
+
def __init__(self, goal_memory, persistent_memory=None, generate_fn=None):
|
| 22 |
+
super().__init__(
|
| 23 |
+
name="researcher",
|
| 24 |
+
role="Researcher",
|
| 25 |
+
description="Gathers information, analyzes topics, and generates insights",
|
| 26 |
+
goal_memory=goal_memory,
|
| 27 |
+
persistent_memory=persistent_memory,
|
| 28 |
+
generate_fn=generate_fn,
|
| 29 |
+
poll_interval_s=3.0,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
def _can_handle(self, goal: Goal) -> bool:
|
| 33 |
+
"""Researcher handles goals related to research/analysis."""
|
| 34 |
+
keywords = ["research", "analyze", "investigate", "study", "learn", "understand",
|
| 35 |
+
"explore", "gather", "find", "search", "compare", "evaluate"]
|
| 36 |
+
text = (goal.title + " " + goal.description).lower()
|
| 37 |
+
return any(kw in text for kw in keywords)
|
| 38 |
+
|
| 39 |
+
def process_goal(self, goal: Goal) -> dict[str, Any]:
|
| 40 |
+
"""Execute a research step."""
|
| 41 |
+
if goal.current_step >= len(goal.steps):
|
| 42 |
+
return {"success": True, "output": "No more steps"}
|
| 43 |
+
|
| 44 |
+
step = goal.steps[goal.current_step]
|
| 45 |
+
prompt = (
|
| 46 |
+
f"You are a research agent. Execute this step:\n"
|
| 47 |
+
f"Goal: {goal.title}\n"
|
| 48 |
+
f"Step: {step['title']}\n"
|
| 49 |
+
f"Description: {step['description']}\n"
|
| 50 |
+
f"Research and provide a concise summary of findings.\n"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
response = self._generate(prompt)
|
| 54 |
+
|
| 55 |
+
# Store findings in persistent memory
|
| 56 |
+
if self.persistent_memory and response:
|
| 57 |
+
self.persistent_memory.add_episodic(
|
| 58 |
+
"research", f"Research for '{goal.title}': {response[:200]}",
|
| 59 |
+
importance=0.7, tags=["research", goal.title[:20]]
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
return {"success": True, "output": response[:200]}
|
singularity_llm/agents/reviewer_agent.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Reviewer Agent — reviews work, validates quality, finds issues.
|
| 2 |
+
|
| 3 |
+
Reviews the output of other agents. Can approve or reject work,
|
| 4 |
+
and suggest improvements. Runs after each step is completed.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import logging
|
| 10 |
+
from typing import Any
|
| 11 |
+
|
| 12 |
+
from .agent_base import BaseAgent
|
| 13 |
+
from ..memory.goal_memory import Goal
|
| 14 |
+
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class ReviewerAgent(BaseAgent):
|
| 19 |
+
"""Reviews work done by other agents and validates quality."""
|
| 20 |
+
|
| 21 |
+
def __init__(self, goal_memory, persistent_memory=None, generate_fn=None):
|
| 22 |
+
super().__init__(
|
| 23 |
+
name="reviewer",
|
| 24 |
+
role="Quality Reviewer",
|
| 25 |
+
description="Reviews work, validates quality, and finds issues",
|
| 26 |
+
goal_memory=goal_memory,
|
| 27 |
+
persistent_memory=persistent_memory,
|
| 28 |
+
generate_fn=generate_fn,
|
| 29 |
+
poll_interval_s=4.0,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
def _can_handle(self, goal: Goal) -> bool:
|
| 33 |
+
"""Reviewer handles goals in reviewing status."""
|
| 34 |
+
return goal.status == "reviewing"
|
| 35 |
+
|
| 36 |
+
def process_goal(self, goal: Goal) -> dict[str, Any]:
|
| 37 |
+
"""Review the most recent step output."""
|
| 38 |
+
if goal.current_step == 0 and not goal.steps:
|
| 39 |
+
return {"success": True, "output": "Nothing to review"}
|
| 40 |
+
|
| 41 |
+
# Review the last completed step
|
| 42 |
+
review_idx = max(0, goal.current_step - 1)
|
| 43 |
+
if review_idx >= len(goal.steps):
|
| 44 |
+
return {"success": True, "output": "No steps to review"}
|
| 45 |
+
|
| 46 |
+
step = goal.steps[review_idx]
|
| 47 |
+
if step.get("status") != "completed":
|
| 48 |
+
return {"success": True, "output": "Step not completed yet"}
|
| 49 |
+
|
| 50 |
+
prompt = (
|
| 51 |
+
f"You are a quality reviewer. Review this work:\n"
|
| 52 |
+
f"Goal: {goal.title}\n"
|
| 53 |
+
f"Step: {step['title']}\n"
|
| 54 |
+
f"Result: {step.get('result', '')[:500]}\n\n"
|
| 55 |
+
f"Evaluate: Is this correct and complete? Reply APPROVED or NEEDS_WORK with reason.\n"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
response = self._generate(prompt)
|
| 59 |
+
|
| 60 |
+
approved = "APPROVED" in response.upper()
|
| 61 |
+
|
| 62 |
+
if self.persistent_memory:
|
| 63 |
+
self.persistent_memory.add_episodic(
|
| 64 |
+
"review", f"Review of '{step['title']}': {'approved' if approved else 'needs work'}",
|
| 65 |
+
importance=0.6, tags=["review", goal.title[:20]]
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if approved:
|
| 69 |
+
return {"success": True, "output": f"Approved: {response[:100]}"}
|
| 70 |
+
else:
|
| 71 |
+
return {"success": False, "output": "", "error": f"Needs work: {response[:100]}"}
|
singularity_llm/agents/self_refine.py
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Self-Refinement Engine — makes the LLM faster and smarter over time.
|
| 2 |
+
|
| 3 |
+
Runs when the system is idle (no active conversations or goals).
|
| 4 |
+
Analyzes performance metrics and applies optimizations:
|
| 5 |
+
|
| 6 |
+
Speed optimizations:
|
| 7 |
+
- Quantization format upgrade (if accuracy allows): ternary → q2 → q3 → q4
|
| 8 |
+
- KV cache size tuning based on usage patterns
|
| 9 |
+
- Inference parameter tuning (temperature, top_k, max_tokens)
|
| 10 |
+
- Batch size optimization for training
|
| 11 |
+
|
| 12 |
+
Smarter optimizations:
|
| 13 |
+
- Identify low-confidence responses and generate self-talk training data
|
| 14 |
+
- Analyze conversation patterns to extract new skills
|
| 15 |
+
- Tune skill matching thresholds based on hit rates
|
| 16 |
+
- Compress skill storage (move cold skills to compressed format)
|
| 17 |
+
- Prune unused skills
|
| 18 |
+
- Optimize recursive link graph (remove stale links)
|
| 19 |
+
|
| 20 |
+
Runs in a background thread, triggered by the always-on daemon.
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
from __future__ import annotations
|
| 24 |
+
|
| 25 |
+
import logging
|
| 26 |
+
import time
|
| 27 |
+
from collections import deque
|
| 28 |
+
from typing import Any, Callable
|
| 29 |
+
|
| 30 |
+
logger = logging.getLogger(__name__)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class SelfRefinementEngine:
|
| 34 |
+
"""Self-refinement engine — optimizes speed and intelligence.
|
| 35 |
+
|
| 36 |
+
Runs in background when system is idle. Tracks performance metrics
|
| 37 |
+
and applies optimizations. Each refinement cycle makes the system
|
| 38 |
+
slightly faster and smarter.
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
REFINEMENT_INTERVAL_S = 120.0 # run every 2 minutes when idle
|
| 42 |
+
MIN_CONVERSATIONS_BEFORE_TUNE = 5
|
| 43 |
+
MIN_CONFIDENCE_FOR_UPGRADE = 0.85
|
| 44 |
+
|
| 45 |
+
def __init__(self, harness: Any | None = None) -> None:
|
| 46 |
+
self.harness = harness
|
| 47 |
+
self._running = False
|
| 48 |
+
self._thread = None
|
| 49 |
+
self._last_refinement = 0.0
|
| 50 |
+
self._metrics_history: deque[dict] = deque(maxlen=50)
|
| 51 |
+
self._refinement_count = 0
|
| 52 |
+
|
| 53 |
+
self._stats = {
|
| 54 |
+
"refinement_cycles": 0,
|
| 55 |
+
"speed_optimizations": 0,
|
| 56 |
+
"intelligence_optimizations": 0,
|
| 57 |
+
"skills_pruned": 0,
|
| 58 |
+
"skills_created": 0,
|
| 59 |
+
"quant_upgrades": 0,
|
| 60 |
+
"param_tunes": 0,
|
| 61 |
+
"self_talk_sessions": 0,
|
| 62 |
+
"total_refinement_time_s": 0.0,
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
def set_harness(self, harness: Any) -> None:
|
| 66 |
+
"""Set the harness reference."""
|
| 67 |
+
self.harness = harness
|
| 68 |
+
|
| 69 |
+
def refine_once(self) -> dict[str, Any]:
|
| 70 |
+
"""Run a single refinement cycle.
|
| 71 |
+
|
| 72 |
+
Returns summary of what was optimized.
|
| 73 |
+
"""
|
| 74 |
+
if not self.harness:
|
| 75 |
+
return {"error": "No harness set"}
|
| 76 |
+
|
| 77 |
+
t0 = time.time()
|
| 78 |
+
results: dict[str, Any] = {"actions": []}
|
| 79 |
+
|
| 80 |
+
# 1. Collect current metrics
|
| 81 |
+
metrics = self._collect_metrics()
|
| 82 |
+
self._metrics_history.append(metrics)
|
| 83 |
+
|
| 84 |
+
# 2. Speed optimizations
|
| 85 |
+
speed_result = self._optimize_speed(metrics)
|
| 86 |
+
if speed_result:
|
| 87 |
+
results["actions"].append(speed_result)
|
| 88 |
+
self._stats["speed_optimizations"] += 1
|
| 89 |
+
|
| 90 |
+
# 3. Intelligence optimizations
|
| 91 |
+
intel_result = self._optimize_intelligence(metrics)
|
| 92 |
+
if intel_result:
|
| 93 |
+
results["actions"].append(intel_result)
|
| 94 |
+
self._stats["intelligence_optimizations"] += 1
|
| 95 |
+
|
| 96 |
+
# 4. Skill maintenance
|
| 97 |
+
skill_result = self._maintain_skills(metrics)
|
| 98 |
+
if skill_result:
|
| 99 |
+
results["actions"].append(skill_result)
|
| 100 |
+
self._stats["skills_pruned"] += skill_result.get("pruned", 0)
|
| 101 |
+
self._stats["skills_created"] += skill_result.get("created", 0)
|
| 102 |
+
|
| 103 |
+
# 5. Memory maintenance
|
| 104 |
+
mem_result = self._maintain_memory(metrics)
|
| 105 |
+
if mem_result:
|
| 106 |
+
results["actions"].append(mem_result)
|
| 107 |
+
|
| 108 |
+
# 6. Self-talk training (if low confidence areas found)
|
| 109 |
+
if metrics.get("avg_confidence", 1.0) < 0.7:
|
| 110 |
+
talk_result = self._run_self_talk(metrics)
|
| 111 |
+
if talk_result:
|
| 112 |
+
results["actions"].append(talk_result)
|
| 113 |
+
self._stats["self_talk_sessions"] += 1
|
| 114 |
+
|
| 115 |
+
elapsed = time.time() - t0
|
| 116 |
+
self._stats["refinement_cycles"] += 1
|
| 117 |
+
self._stats["total_refinement_time_s"] += elapsed
|
| 118 |
+
self._last_refinement = time.time()
|
| 119 |
+
|
| 120 |
+
results["elapsed_s"] = round(elapsed, 3)
|
| 121 |
+
results["cycle"] = self._stats["refinement_cycles"]
|
| 122 |
+
logger.info("Refinement cycle %d complete: %d actions (%.2fs)",
|
| 123 |
+
self._stats["refinement_cycles"], len(results["actions"]), elapsed)
|
| 124 |
+
return results
|
| 125 |
+
|
| 126 |
+
def _collect_metrics(self) -> dict[str, Any]:
|
| 127 |
+
"""Collect current system performance metrics."""
|
| 128 |
+
if not self.harness:
|
| 129 |
+
return {}
|
| 130 |
+
|
| 131 |
+
model_stats = self.harness.model.get_stats()
|
| 132 |
+
link_stats = self.harness.link_graph.get_stats()
|
| 133 |
+
skill_stats = self.harness.skill_manager.get_stats()
|
| 134 |
+
memory_stats = self.harness.persistent_memory.get_stats()
|
| 135 |
+
goal_stats = self.harness.goal_memory.get_stats()
|
| 136 |
+
|
| 137 |
+
return {
|
| 138 |
+
"timestamp": time.time(),
|
| 139 |
+
"inference_count": model_stats.get("inference_count", 0),
|
| 140 |
+
"avg_inference_time_s": model_stats.get("avg_inference_time_s", 0),
|
| 141 |
+
"tokens_per_second": model_stats.get("tokens_per_second", 0),
|
| 142 |
+
"total_chats": self.harness._stats.get("total_chats", 0),
|
| 143 |
+
"skills_total": skill_stats.get("total_skills", 0),
|
| 144 |
+
"skills_active": skill_stats.get("active_skills", 0),
|
| 145 |
+
"contexts_total": link_stats.get("total_contexts", 0),
|
| 146 |
+
"links_total": link_stats.get("total_links", 0),
|
| 147 |
+
"episodic_total": memory_stats.get("episodic_total", 0),
|
| 148 |
+
"semantic_total": memory_stats.get("semantic_total", 0),
|
| 149 |
+
"goals_total": goal_stats.get("total", 0),
|
| 150 |
+
"goals_completed": goal_stats.get("completed", 0),
|
| 151 |
+
"avg_confidence": self._compute_avg_confidence(),
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
def _compute_avg_confidence(self) -> float:
|
| 155 |
+
"""Compute average response confidence from recent conversations."""
|
| 156 |
+
if not self.harness or not hasattr(self.harness, "_confidence_history"):
|
| 157 |
+
return 0.8
|
| 158 |
+
history = self.harness._confidence_history
|
| 159 |
+
if not history:
|
| 160 |
+
return 0.8
|
| 161 |
+
return sum(history) / len(history)
|
| 162 |
+
|
| 163 |
+
def _optimize_speed(self, metrics: dict) -> dict | None:
|
| 164 |
+
"""Optimize inference speed."""
|
| 165 |
+
actions = []
|
| 166 |
+
|
| 167 |
+
# Check if inference is slow
|
| 168 |
+
tps = metrics.get("tokens_per_second", 0)
|
| 169 |
+
avg_time = metrics.get("avg_inference_time_s", 0)
|
| 170 |
+
|
| 171 |
+
if tps > 0 and tps < 10 and self.harness:
|
| 172 |
+
# Try reducing max_tokens for faster responses
|
| 173 |
+
current_max = self.harness.sizer.get_inference_params().get("max_tokens", 64)
|
| 174 |
+
if current_max > 16:
|
| 175 |
+
new_max = max(16, current_max - 8)
|
| 176 |
+
self.harness.sizer._inference_params["max_tokens"] = new_max
|
| 177 |
+
actions.append(f"Reduced max_tokens: {current_max} → {new_max}")
|
| 178 |
+
self._stats["param_tunes"] += 1
|
| 179 |
+
|
| 180 |
+
# Check if KV cache is being used
|
| 181 |
+
if self.harness and not self.harness.sizer.get_inference_params().get("use_cache", True):
|
| 182 |
+
self.harness.sizer._inference_params["use_cache"] = True
|
| 183 |
+
actions.append("Enabled KV cache")
|
| 184 |
+
self._stats["param_tunes"] += 1
|
| 185 |
+
|
| 186 |
+
if actions:
|
| 187 |
+
return {"type": "speed", "actions": actions}
|
| 188 |
+
return None
|
| 189 |
+
|
| 190 |
+
def _optimize_intelligence(self, metrics: dict) -> dict | None:
|
| 191 |
+
"""Optimize model intelligence."""
|
| 192 |
+
actions = []
|
| 193 |
+
|
| 194 |
+
# Check if we have enough data to tune
|
| 195 |
+
if metrics.get("total_chats", 0) < self.MIN_CONVERSATIONS_BEFORE_TUNE:
|
| 196 |
+
return None
|
| 197 |
+
|
| 198 |
+
# Tune temperature based on response quality
|
| 199 |
+
if self.harness:
|
| 200 |
+
current_temp = self.harness.sizer.get_inference_params().get("temperature", 0.5)
|
| 201 |
+
avg_conf = metrics.get("avg_confidence", 0.8)
|
| 202 |
+
|
| 203 |
+
if avg_conf < 0.5 and current_temp > 0.3:
|
| 204 |
+
# Low confidence — reduce temperature for more focused responses
|
| 205 |
+
new_temp = max(0.1, current_temp - 0.1)
|
| 206 |
+
self.harness.sizer._inference_params["temperature"] = new_temp
|
| 207 |
+
actions.append(f"Reduced temperature: {current_temp:.1f} → {new_temp:.1f} (low confidence)")
|
| 208 |
+
self._stats["param_tunes"] += 1
|
| 209 |
+
elif avg_conf > 0.9 and current_temp < 0.8:
|
| 210 |
+
# High confidence — can afford more creativity
|
| 211 |
+
new_temp = min(0.9, current_temp + 0.05)
|
| 212 |
+
self.harness.sizer._inference_params["temperature"] = new_temp
|
| 213 |
+
actions.append(f"Increased temperature: {current_temp:.1f} → {new_temp:.1f} (high confidence)")
|
| 214 |
+
self._stats["param_tunes"] += 1
|
| 215 |
+
|
| 216 |
+
# Tune top_k
|
| 217 |
+
if self.harness:
|
| 218 |
+
current_top_k = self.harness.sizer.get_inference_params().get("top_k", 40)
|
| 219 |
+
if metrics.get("avg_confidence", 0.8) < 0.5 and current_top_k > 10:
|
| 220 |
+
new_top_k = max(5, current_top_k - 5)
|
| 221 |
+
self.harness.sizer._inference_params["top_k"] = new_top_k
|
| 222 |
+
actions.append(f"Reduced top_k: {current_top_k} → {new_top_k}")
|
| 223 |
+
self._stats["param_tunes"] += 1
|
| 224 |
+
|
| 225 |
+
if actions:
|
| 226 |
+
return {"type": "intelligence", "actions": actions}
|
| 227 |
+
return None
|
| 228 |
+
|
| 229 |
+
def _maintain_skills(self, metrics: dict) -> dict | None:
|
| 230 |
+
"""Maintain skill storage — prune unused, compress cold."""
|
| 231 |
+
actions = []
|
| 232 |
+
pruned = 0
|
| 233 |
+
created = 0
|
| 234 |
+
|
| 235 |
+
if not self.harness:
|
| 236 |
+
return None
|
| 237 |
+
|
| 238 |
+
# Prune skills with very low effectiveness
|
| 239 |
+
skill_mgr = self.harness.skill_manager
|
| 240 |
+
if hasattr(skill_mgr, "_skills"):
|
| 241 |
+
to_remove = []
|
| 242 |
+
for skill_id, skill in skill_mgr._skills.items():
|
| 243 |
+
if hasattr(skill, 'effectiveness') and skill.effectiveness < 0.1:
|
| 244 |
+
if hasattr(skill, 'use_count') and skill.use_count > 3:
|
| 245 |
+
to_remove.append(skill_id)
|
| 246 |
+
for sid in to_remove:
|
| 247 |
+
skill_mgr.delete(sid)
|
| 248 |
+
pruned += 1
|
| 249 |
+
if pruned > 0:
|
| 250 |
+
actions.append(f"Pruned {pruned} low-effectiveness skills")
|
| 251 |
+
|
| 252 |
+
# Try to extract new skills from recent conversations
|
| 253 |
+
factory = self.harness.skill_factory
|
| 254 |
+
if hasattr(factory, 'extract_skill'):
|
| 255 |
+
skill = factory.extract_skill()
|
| 256 |
+
if skill:
|
| 257 |
+
skill_mgr.create(skill)
|
| 258 |
+
created += 1
|
| 259 |
+
actions.append("Extracted 1 new skill from conversations")
|
| 260 |
+
|
| 261 |
+
if actions:
|
| 262 |
+
return {"type": "skills", "actions": actions, "pruned": pruned, "created": created}
|
| 263 |
+
return None
|
| 264 |
+
|
| 265 |
+
def _maintain_memory(self, metrics: dict) -> dict | None:
|
| 266 |
+
"""Maintain memory — clean up stale entries, optimize recall."""
|
| 267 |
+
actions = []
|
| 268 |
+
|
| 269 |
+
if not self.harness:
|
| 270 |
+
return None
|
| 271 |
+
|
| 272 |
+
# Check if link graph is getting large
|
| 273 |
+
total_links = metrics.get("links_total", 0)
|
| 274 |
+
if total_links > 1000:
|
| 275 |
+
# Suggest cleanup
|
| 276 |
+
actions.append(f"Link graph large ({total_links} links) — consider cleanup")
|
| 277 |
+
|
| 278 |
+
# Check memory size
|
| 279 |
+
episodic = metrics.get("episodic_total", 0)
|
| 280 |
+
if episodic > 500:
|
| 281 |
+
actions.append(f"Episodic memory large ({episodic} entries)")
|
| 282 |
+
|
| 283 |
+
if actions:
|
| 284 |
+
return {"type": "memory", "actions": actions}
|
| 285 |
+
return None
|
| 286 |
+
|
| 287 |
+
def _run_self_talk(self, metrics: dict) -> dict | None:
|
| 288 |
+
"""Run a self-talk session to generate training data for weak areas."""
|
| 289 |
+
if not self.harness:
|
| 290 |
+
return None
|
| 291 |
+
|
| 292 |
+
# Use the self-improvement engine if available
|
| 293 |
+
if hasattr(self.harness, '_self_improve'):
|
| 294 |
+
# This would trigger self-talk via the self-improvement engine
|
| 295 |
+
return {"type": "self_talk", "actions": ["Triggered self-talk session for low-confidence areas"]}
|
| 296 |
+
|
| 297 |
+
return None
|
| 298 |
+
|
| 299 |
+
def start(self) -> None:
|
| 300 |
+
"""Start the refinement engine in a background thread."""
|
| 301 |
+
import threading
|
| 302 |
+
if self._running:
|
| 303 |
+
return
|
| 304 |
+
self._running = True
|
| 305 |
+
self._thread = threading.Thread(target=self._run_loop, daemon=True, name="self-refine")
|
| 306 |
+
self._thread.start()
|
| 307 |
+
logger.info("Self-refinement engine started")
|
| 308 |
+
|
| 309 |
+
def stop(self) -> None:
|
| 310 |
+
"""Stop the refinement engine."""
|
| 311 |
+
self._running = False
|
| 312 |
+
if self._thread:
|
| 313 |
+
self._thread.join(timeout=5)
|
| 314 |
+
logger.info("Self-refinement engine stopped")
|
| 315 |
+
|
| 316 |
+
def _run_loop(self) -> None:
|
| 317 |
+
"""Background loop — runs refinement cycles when idle."""
|
| 318 |
+
while self._running:
|
| 319 |
+
time.sleep(self.REFINEMENT_INTERVAL_S)
|
| 320 |
+
if not self._running:
|
| 321 |
+
break
|
| 322 |
+
try:
|
| 323 |
+
self.refine_once()
|
| 324 |
+
except Exception as e:
|
| 325 |
+
logger.error("Refinement cycle failed: %s", e)
|
| 326 |
+
|
| 327 |
+
def get_stats(self) -> dict[str, Any]:
|
| 328 |
+
return {
|
| 329 |
+
**self._stats,
|
| 330 |
+
"running": self._running,
|
| 331 |
+
"last_refinement": self._last_refinement,
|
| 332 |
+
"metrics_history_size": len(self._metrics_history),
|
| 333 |
+
}
|
singularity_llm/cli.py
ADDED
|
@@ -0,0 +1,544 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""CLI interface for Singularity LLM.
|
| 2 |
+
|
| 3 |
+
Commands:
|
| 4 |
+
python -m singularity_llm chat — interactive chat REPL
|
| 5 |
+
python -m singularity_llm jarvis — start built-in voice assistant
|
| 6 |
+
python -m singularity_llm train --data corpus.txt --epochs 10
|
| 7 |
+
python -m singularity_llm serve --port 8548
|
| 8 |
+
python -m singularity_llm stats — show model stats
|
| 9 |
+
python -m singularity_llm export --format gguf
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
import argparse
|
| 15 |
+
import json
|
| 16 |
+
import logging
|
| 17 |
+
import sys
|
| 18 |
+
from typing import Any
|
| 19 |
+
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def cmd_chat(args: argparse.Namespace) -> None:
|
| 24 |
+
"""Interactive chat REPL."""
|
| 25 |
+
from .harness.harness import SingularityHarness
|
| 26 |
+
harness = SingularityHarness()
|
| 27 |
+
|
| 28 |
+
# Subscription check — $1/month, free trial, or founder unlock
|
| 29 |
+
access = harness.subscription.check_access()
|
| 30 |
+
if not access["has_access"]:
|
| 31 |
+
print(f"\n⚠ {access['message']}")
|
| 32 |
+
print(f" Run 'singularity-llm subscribe' to subscribe (${harness.subscription.MONTHLY_PRICE:.2f}/month)")
|
| 33 |
+
print(f" or 'singularity-llm trial' for a free trial")
|
| 34 |
+
print(f" or enter founder password for free access\n")
|
| 35 |
+
try:
|
| 36 |
+
password = input("Password (or press Enter to skip): ").strip()
|
| 37 |
+
if password:
|
| 38 |
+
result = harness.subscription.founder_unlock(password)
|
| 39 |
+
if result["success"]:
|
| 40 |
+
print(f"\n✓ {result['message']}\n")
|
| 41 |
+
access = {"has_access": True, "status": "founder", "days_remaining": -1}
|
| 42 |
+
else:
|
| 43 |
+
print(f"\n✗ {result['error']}\n")
|
| 44 |
+
return
|
| 45 |
+
else:
|
| 46 |
+
return
|
| 47 |
+
except (EOFError, KeyboardInterrupt):
|
| 48 |
+
print()
|
| 49 |
+
return
|
| 50 |
+
|
| 51 |
+
# First-run naming flow
|
| 52 |
+
if harness.identity.is_first_run():
|
| 53 |
+
print(f"\n{harness.identity.get_greeting()}")
|
| 54 |
+
try:
|
| 55 |
+
name = input("> ").strip()
|
| 56 |
+
if name:
|
| 57 |
+
harness.identity.set_name(name)
|
| 58 |
+
print(f"\nGreat! I'll be called {name}. How can I help you?\n")
|
| 59 |
+
else:
|
| 60 |
+
harness.identity.set_name("Singularity")
|
| 61 |
+
print(f"\nNo problem, call me Singularity. How can I help you?\n")
|
| 62 |
+
except (EOFError, KeyboardInterrupt):
|
| 63 |
+
harness.identity.set_name("Singularity")
|
| 64 |
+
print(f"\nNo problem, call me Singularity. How can I help you?\n")
|
| 65 |
+
else:
|
| 66 |
+
sub_tag = f" | {access['status']}: {access['days_remaining']}d left" if access["status"] != "active" else ""
|
| 67 |
+
print(f"\n⚡ {harness.identity.get_name()} — Chat Mode")
|
| 68 |
+
print(f" Model: {harness.model.param_count:,} params | Tier: {harness.settings.tier.value}{sub_tag}")
|
| 69 |
+
print(f" Type 'exit' to quit, 'stats' for stats\n")
|
| 70 |
+
|
| 71 |
+
session_id = f"cli-{int(__import__('time').time())}"
|
| 72 |
+
|
| 73 |
+
while True:
|
| 74 |
+
try:
|
| 75 |
+
user_input = input("You: ").strip()
|
| 76 |
+
except (EOFError, KeyboardInterrupt):
|
| 77 |
+
print("\nGoodbye!")
|
| 78 |
+
break
|
| 79 |
+
|
| 80 |
+
if not user_input:
|
| 81 |
+
continue
|
| 82 |
+
if user_input.lower() in ("exit", "quit", "bye"):
|
| 83 |
+
print("Goodbye!")
|
| 84 |
+
break
|
| 85 |
+
if user_input.lower() == "stats":
|
| 86 |
+
stats = harness.get_stats()
|
| 87 |
+
print(json.dumps(stats, indent=2, default=str))
|
| 88 |
+
continue
|
| 89 |
+
|
| 90 |
+
result = harness.chat(user_input, channel="cli", session_id=session_id)
|
| 91 |
+
name = harness.identity.get_name()
|
| 92 |
+
cached_tag = " [cached]" if result.get("cached") else ""
|
| 93 |
+
print(f"{name}: {result['response']}")
|
| 94 |
+
print(f" [{result['elapsed_s']}s{cached_tag}]\n")
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def cmd_jarvis(args: argparse.Namespace) -> None:
|
| 98 |
+
"""Start built-in Jarvis voice assistant."""
|
| 99 |
+
from .voice.jarvis import JarvisAssistant
|
| 100 |
+
jarvis = JarvisAssistant(wake_word=args.wake_word)
|
| 101 |
+
|
| 102 |
+
print(f"\n⚡ Jarvis Voice Assistant")
|
| 103 |
+
print(f" Say '{args.wake_word}' to start talking")
|
| 104 |
+
print(f" Say '{args.wake_word} stop' to interrupt speech")
|
| 105 |
+
print(f" Say '{args.wake_word} goodbye' to quit")
|
| 106 |
+
print(f" Say 'practice mode on' for self-talk training\n")
|
| 107 |
+
|
| 108 |
+
# Check if voice backends are available
|
| 109 |
+
if not jarvis.stt.is_available():
|
| 110 |
+
print("⚠ STT not available (install whisper or vosk for voice input)")
|
| 111 |
+
print(" Falling back to text mode. Type your commands.\n")
|
| 112 |
+
|
| 113 |
+
session_id = jarvis._session_id
|
| 114 |
+
while True:
|
| 115 |
+
try:
|
| 116 |
+
user_input = input("You: ").strip()
|
| 117 |
+
except (EOFError, KeyboardInterrupt):
|
| 118 |
+
break
|
| 119 |
+
if not user_input:
|
| 120 |
+
continue
|
| 121 |
+
if user_input.lower() in ("exit", "quit", "bye", "goodbye"):
|
| 122 |
+
break
|
| 123 |
+
response = jarvis.text_chat(user_input)
|
| 124 |
+
print(f"Jarvis: {response}\n")
|
| 125 |
+
|
| 126 |
+
jarvis.stop()
|
| 127 |
+
stats = jarvis.get_stats()
|
| 128 |
+
print(f"\nConversations: {stats['jarvis']['conversation_count']}")
|
| 129 |
+
return
|
| 130 |
+
|
| 131 |
+
# Full voice mode
|
| 132 |
+
try:
|
| 133 |
+
jarvis.run()
|
| 134 |
+
except KeyboardInterrupt:
|
| 135 |
+
jarvis.stop()
|
| 136 |
+
|
| 137 |
+
stats = jarvis.get_stats()
|
| 138 |
+
print(f"\nJarvis stats: {json.dumps(stats, indent=2, default=str)}")
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def cmd_train(args: argparse.Namespace) -> None:
|
| 142 |
+
"""Train the model on text data."""
|
| 143 |
+
from .harness.harness import SingularityHarness
|
| 144 |
+
from .train.train import Trainer
|
| 145 |
+
from .model.quantization import SingularityQuantizer
|
| 146 |
+
|
| 147 |
+
harness = SingularityHarness()
|
| 148 |
+
|
| 149 |
+
# Train tokenizer first if needed
|
| 150 |
+
print("Training tokenizer...")
|
| 151 |
+
harness.train_tokenizer(args.data)
|
| 152 |
+
|
| 153 |
+
# Update model vocab size to match tokenizer
|
| 154 |
+
harness.model.config.vocab_size = harness.tokenizer.actual_vocab_size
|
| 155 |
+
|
| 156 |
+
# Get auto-sized training params
|
| 157 |
+
train_params = harness.sizer.get_training_params()
|
| 158 |
+
epochs = args.epochs or train_params["epochs"]
|
| 159 |
+
batch_size = args.batch_size or train_params["batch_size"]
|
| 160 |
+
seq_len = args.seq_len or train_params["seq_len"]
|
| 161 |
+
|
| 162 |
+
quantizer = SingularityQuantizer(format=harness.settings.quant.format)
|
| 163 |
+
|
| 164 |
+
trainer = Trainer(
|
| 165 |
+
model=harness.model,
|
| 166 |
+
tokenizer=harness.tokenizer,
|
| 167 |
+
lr=train_params["lr"],
|
| 168 |
+
warmup_steps=train_params["warmup_steps"],
|
| 169 |
+
checkpoint_dir=harness.data_dir,
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
print(f"Training: epochs={epochs}, batch_size={batch_size}, seq_len={seq_len}")
|
| 173 |
+
stats = trainer.train(
|
| 174 |
+
data_paths=args.data,
|
| 175 |
+
epochs=epochs,
|
| 176 |
+
batch_size=batch_size,
|
| 177 |
+
seq_len=seq_len,
|
| 178 |
+
quantizer=quantizer,
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
print(f"\nTraining complete: {json.dumps(stats, indent=2)}")
|
| 182 |
+
|
| 183 |
+
# Save model
|
| 184 |
+
harness.save_model()
|
| 185 |
+
print("Model saved.")
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def cmd_serve(args: argparse.Namespace) -> None:
|
| 189 |
+
"""Start the inference server."""
|
| 190 |
+
from .server.server import run_server
|
| 191 |
+
run_server(host=args.host, port=args.port)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def cmd_stats(args: argparse.Namespace) -> None:
|
| 195 |
+
"""Show model stats."""
|
| 196 |
+
from .harness.harness import SingularityHarness
|
| 197 |
+
harness = SingularityHarness()
|
| 198 |
+
stats = harness.get_stats()
|
| 199 |
+
print(json.dumps(stats, indent=2, default=str))
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def cmd_export(args: argparse.Namespace) -> None:
|
| 203 |
+
"""Export model weights."""
|
| 204 |
+
from .harness.harness import SingularityHarness
|
| 205 |
+
harness = SingularityHarness()
|
| 206 |
+
output_path = args.output or "model_export.npz"
|
| 207 |
+
harness.save_model(output_path)
|
| 208 |
+
print(f"Model exported to {output_path}")
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def cmd_goals(args: argparse.Namespace) -> None:
|
| 212 |
+
"""Manage goals and projects."""
|
| 213 |
+
from .harness.harness import SingularityHarness
|
| 214 |
+
harness = SingularityHarness()
|
| 215 |
+
|
| 216 |
+
if args.goal_action == "create":
|
| 217 |
+
goal_id = harness.create_goal(args.title, args.description, priority=args.priority)
|
| 218 |
+
print(f"Created goal: {goal_id}")
|
| 219 |
+
if args.start_agents:
|
| 220 |
+
harness.start_agents()
|
| 221 |
+
print("Agents started — working on goal.")
|
| 222 |
+
elif args.goal_action == "list":
|
| 223 |
+
goals = harness.get_goals()
|
| 224 |
+
# Also show pending goals
|
| 225 |
+
from .memory.goal_memory import GoalMemory
|
| 226 |
+
import os as _os
|
| 227 |
+
gm = GoalMemory(db_path=_os.path.join(harness.data_dir, "goals.db"))
|
| 228 |
+
pending = gm.list_goals(status="pending")
|
| 229 |
+
for g in pending:
|
| 230 |
+
goals.append(g.as_dict())
|
| 231 |
+
if not goals:
|
| 232 |
+
print("No goals found.")
|
| 233 |
+
for g in goals:
|
| 234 |
+
progress = g.get("progress", 0)
|
| 235 |
+
agent = g.get("assigned_agent", "") or "unassigned"
|
| 236 |
+
print(f" [{g['priority']}] {g['title']} — {g['status']} ({progress:.0%}) agent: {agent}")
|
| 237 |
+
elif args.goal_action == "agents":
|
| 238 |
+
status = harness.get_agent_status()
|
| 239 |
+
for name, info in status.items():
|
| 240 |
+
current = info.get("current_goal", "idle")
|
| 241 |
+
stats = info.get("stats", {})
|
| 242 |
+
print(f" {name} ({info['role']}): {'running' if info['running'] else 'stopped'} | current: {current} | completed: {stats.get('goals_completed', 0)}")
|
| 243 |
+
elif args.goal_action == "start-agents":
|
| 244 |
+
harness.start_agents()
|
| 245 |
+
print("All 5 agents started.")
|
| 246 |
+
elif args.goal_action == "stop-agents":
|
| 247 |
+
harness.stop_agents()
|
| 248 |
+
print("All agents stopped.")
|
| 249 |
+
elif args.goal_action == "stats":
|
| 250 |
+
stats = harness.get_stats()
|
| 251 |
+
print(json.dumps({
|
| 252 |
+
"goals": stats.get("goal_memory", {}),
|
| 253 |
+
"agents": stats.get("agents", {}),
|
| 254 |
+
"persistent_memory": stats.get("persistent_memory", {}),
|
| 255 |
+
}, indent=2, default=str))
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def cmd_daemon(args: argparse.Namespace) -> None:
|
| 259 |
+
"""Start the always-on daemon — agents, self-refinement, skill creation."""
|
| 260 |
+
from .harness.harness import SingularityHarness
|
| 261 |
+
harness = SingularityHarness()
|
| 262 |
+
|
| 263 |
+
print(f"\n⚡ Singularity LLM — Always-On Daemon")
|
| 264 |
+
print(f" 5 agents will talk to the LLM when idle")
|
| 265 |
+
print(f" Self-refinement engine will optimize speed and intelligence")
|
| 266 |
+
print(f" Skills will be created from agent conversations")
|
| 267 |
+
print(f" Goals will be progressed automatically")
|
| 268 |
+
print(f" Press Ctrl+C to stop\n")
|
| 269 |
+
|
| 270 |
+
# Create a goal if provided
|
| 271 |
+
if args.goal:
|
| 272 |
+
goal_id = harness.create_goal(args.goal, args.goal, priority="high")
|
| 273 |
+
print(f"Created goal: {goal_id}")
|
| 274 |
+
|
| 275 |
+
harness.start_daemon()
|
| 276 |
+
|
| 277 |
+
try:
|
| 278 |
+
import time as _time
|
| 279 |
+
while True:
|
| 280 |
+
_time.sleep(10)
|
| 281 |
+
stats = harness.daemon.get_stats()
|
| 282 |
+
print(f"[{_time.strftime('%H:%M:%S')}] idle={stats['idle']} | "
|
| 283 |
+
f"agent_convs={stats['agent_conversations']} | "
|
| 284 |
+
f"skills_created={stats['skills_created']} | "
|
| 285 |
+
f"refinements={stats['refinement_cycles']}")
|
| 286 |
+
except KeyboardInterrupt:
|
| 287 |
+
print("\nStopping daemon...")
|
| 288 |
+
harness.stop_daemon()
|
| 289 |
+
stats = harness.daemon.get_stats()
|
| 290 |
+
print(f"\nDaemon stats: {json.dumps(stats, indent=2, default=str)}")
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def cmd_subscribe(args: argparse.Namespace) -> None:
|
| 294 |
+
"""Subscribe for $1.00/month — shows payment instructions."""
|
| 295 |
+
from .harness.harness import SingularityHarness
|
| 296 |
+
harness = SingularityHarness()
|
| 297 |
+
|
| 298 |
+
print(f"\n💳 Singularity LLM Subscription — ${harness.subscription.MONTHLY_PRICE:.2f}/month")
|
| 299 |
+
print(f" Duration: {harness.subscription.SUBSCRIPTION_DURATION_DAYS} days")
|
| 300 |
+
print(f" Auto-renew: enabled\n")
|
| 301 |
+
|
| 302 |
+
# Get payment instructions first
|
| 303 |
+
instructions = harness.subscription.get_payment_instructions()
|
| 304 |
+
wallet = instructions["founder_wallet"]
|
| 305 |
+
print(f"📋 Payment Instructions:")
|
| 306 |
+
print(f" Amount: ${instructions['amount']:.2f} {instructions['currency']}/month")
|
| 307 |
+
print(f" Network: {instructions['network']}")
|
| 308 |
+
print(f" Accepted tokens: {', '.join(instructions['accepted_tokens'])}")
|
| 309 |
+
if wallet and wallet != "Fetching from Soulmate OS...":
|
| 310 |
+
print(f" Founder wallet: {wallet}")
|
| 311 |
+
print(f" Pay via Google Pay / card: {instructions['soulmate_wallet_url']}")
|
| 312 |
+
print()
|
| 313 |
+
|
| 314 |
+
# Activate subscription
|
| 315 |
+
import os as _os
|
| 316 |
+
user_id = _os.getlogin() if hasattr(_os, 'getlogin') else "Singularity-user"
|
| 317 |
+
result = harness.subscription.subscribe(user_id=user_id, token="USDT")
|
| 318 |
+
if result["success"]:
|
| 319 |
+
print(f"✓ {result['message']}")
|
| 320 |
+
print(f" Months subscribed: {result['months_subscribed']}")
|
| 321 |
+
print(f" Total paid: ${result['total_paid']:.2f}")
|
| 322 |
+
print(f" Expires in: {result['days_remaining']} days")
|
| 323 |
+
if result.get("deposit_id"):
|
| 324 |
+
print(f" Deposit ID: {result['deposit_id']}")
|
| 325 |
+
print()
|
| 326 |
+
else:
|
| 327 |
+
print(f"✗ {result.get('error', 'Subscription failed')}\n")
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
def cmd_unsubscribe(args: argparse.Namespace) -> None:
|
| 331 |
+
"""Cancel subscription."""
|
| 332 |
+
from .harness.harness import SingularityHarness
|
| 333 |
+
harness = SingularityHarness()
|
| 334 |
+
|
| 335 |
+
result = harness.subscription.unsubscribe()
|
| 336 |
+
if result["success"]:
|
| 337 |
+
print(f"\n✓ {result['message']}\n")
|
| 338 |
+
else:
|
| 339 |
+
print(f"\n✗ {result.get('error', 'Unsubscribe failed')}\n")
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
def cmd_trial(args: argparse.Namespace) -> None:
|
| 343 |
+
"""Start free trial."""
|
| 344 |
+
from .harness.harness import SingularityHarness
|
| 345 |
+
harness = SingularityHarness()
|
| 346 |
+
|
| 347 |
+
print(f"\n🎁 Singularity LLM Free Trial")
|
| 348 |
+
print(f" Duration: {harness.subscription.TRIAL_DURATION_DAYS} days")
|
| 349 |
+
print(f" No payment required\n")
|
| 350 |
+
|
| 351 |
+
result = harness.subscription.start_trial()
|
| 352 |
+
if result["success"]:
|
| 353 |
+
print(f"✓ {result['message']}")
|
| 354 |
+
print(f" Days remaining: {result['days_remaining']}\n")
|
| 355 |
+
else:
|
| 356 |
+
print(f"✗ {result.get('error', 'Trial failed')}\n")
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
def cmd_unlock(args: argparse.Namespace) -> None:
|
| 360 |
+
"""Unlock free founder access with password."""
|
| 361 |
+
from .harness.harness import SingularityHarness
|
| 362 |
+
harness = SingularityHarness()
|
| 363 |
+
|
| 364 |
+
password = args.password
|
| 365 |
+
if not password:
|
| 366 |
+
print("\n🔐 Founder Unlock — Enter your founder password")
|
| 367 |
+
try:
|
| 368 |
+
password = input("Password: ").strip()
|
| 369 |
+
except (EOFError, KeyboardInterrupt):
|
| 370 |
+
print("\nCancelled.")
|
| 371 |
+
return
|
| 372 |
+
|
| 373 |
+
if not password:
|
| 374 |
+
print("No password entered.")
|
| 375 |
+
return
|
| 376 |
+
|
| 377 |
+
result = harness.subscription.founder_unlock(password)
|
| 378 |
+
if result["success"]:
|
| 379 |
+
print(f"\n✓ {result['message']}\n")
|
| 380 |
+
else:
|
| 381 |
+
print(f"\n✗ {result['error']}\n")
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
def cmd_transfer(args: argparse.Namespace) -> None:
|
| 385 |
+
"""Process auto-transfers — route $1 payments to founder bank account."""
|
| 386 |
+
from .harness.harness import SingularityHarness
|
| 387 |
+
harness = SingularityHarness()
|
| 388 |
+
|
| 389 |
+
print("\n🏦 Auto-Transfer — Routing payments to bank account\n")
|
| 390 |
+
|
| 391 |
+
# Show current stats
|
| 392 |
+
stats = harness.subscription.get_auto_transfer_stats()
|
| 393 |
+
print(f" Auto-transfer: {'enabled' if stats['auto_transfer_enabled'] else 'disabled'}")
|
| 394 |
+
print(f" Bank routing: {stats['bank_routing']}")
|
| 395 |
+
print(f" Bank account: {stats['bank_account']}")
|
| 396 |
+
print(f" Total transferred: ${stats['total_transferred']:.2f}")
|
| 397 |
+
print(f" Transfer count: {stats['transfer_count']}")
|
| 398 |
+
print(f" Pending: {stats['pending_transfers']}")
|
| 399 |
+
print(f" Untransferred payments: {stats['untransferred_payments']}")
|
| 400 |
+
print()
|
| 401 |
+
|
| 402 |
+
if not stats["auto_transfer_enabled"]:
|
| 403 |
+
print("⚠ Auto-transfer is disabled. Enable it first.")
|
| 404 |
+
return
|
| 405 |
+
|
| 406 |
+
# Process transfers
|
| 407 |
+
result = harness.subscription.process_auto_transfers()
|
| 408 |
+
if result["status"] == "ok":
|
| 409 |
+
if result["transfers_made"] > 0:
|
| 410 |
+
print(f"✓ {result['message']}")
|
| 411 |
+
for t in result["transfers"]:
|
| 412 |
+
print(f" ${t['amount']:.2f} → {t.get('routing_number', '????')}****/{t.get('account_number', '????')}**** (ref: {t['payment_reference'][:16]}...)")
|
| 413 |
+
else:
|
| 414 |
+
print(f"✓ {result['message']}")
|
| 415 |
+
else:
|
| 416 |
+
print(f"✗ {result.get('message', 'Transfer failed')}")
|
| 417 |
+
print()
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def cmd_bank(args: argparse.Namespace) -> None:
|
| 421 |
+
"""Show bank account info and auto-transfer settings."""
|
| 422 |
+
from .harness.harness import SingularityHarness
|
| 423 |
+
harness = SingularityHarness()
|
| 424 |
+
|
| 425 |
+
print("\n🏦 Founder Bank Account Info\n")
|
| 426 |
+
info = harness.subscription.get_bank_info()
|
| 427 |
+
print(f" Routing: {info['routing_number']}")
|
| 428 |
+
print(f" Account: {info['account_number']}")
|
| 429 |
+
print(f" Auto-transfer: {'enabled' if info['auto_transfer_enabled'] else 'disabled'}")
|
| 430 |
+
print(f" Total transferred: ${info['total_transferred']:.2f}")
|
| 431 |
+
print(f" Transfer count: {info['transfer_count']}")
|
| 432 |
+
|
| 433 |
+
stats = harness.subscription.get_auto_transfer_stats()
|
| 434 |
+
if stats["recent_transfers"]:
|
| 435 |
+
print(f"\n Recent transfers:")
|
| 436 |
+
for t in stats["recent_transfers"]:
|
| 437 |
+
print(f" ${t['amount']:.2f} — {t['status']} — ref: {t['payment_reference'][:16]}...")
|
| 438 |
+
print()
|
| 439 |
+
|
| 440 |
+
if info["routing_number"] == "Not set" or info["account_number"] == "Not set":
|
| 441 |
+
print("⚠ Bank routing/account not set!")
|
| 442 |
+
print(" Set environment variables:")
|
| 443 |
+
print(" set INC_LLM_CURRENT_ROUTING=your_routing_number")
|
| 444 |
+
print(" set INC_LLM_CURRENT_ACCOUNT=your_account_number")
|
| 445 |
+
print()
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
def main() -> None:
|
| 449 |
+
"""Main CLI entry point."""
|
| 450 |
+
parser = argparse.ArgumentParser(
|
| 451 |
+
prog="singularity_llm",
|
| 452 |
+
description="Singularity LLM — fast, uncensored, 100% local LLM with voice assistant",
|
| 453 |
+
)
|
| 454 |
+
subparsers = parser.add_subparsers(dest="command", help="Available commands")
|
| 455 |
+
|
| 456 |
+
# Chat
|
| 457 |
+
p_chat = subparsers.add_parser("chat", help="Interactive chat REPL")
|
| 458 |
+
p_chat.set_defaults(func=cmd_chat)
|
| 459 |
+
|
| 460 |
+
# Jarvis
|
| 461 |
+
p_jarvis = subparsers.add_parser("jarvis", help="Start built-in voice assistant")
|
| 462 |
+
p_jarvis.add_argument("--wake-word", default="jarvis", help="Wake word (default: jarvis)")
|
| 463 |
+
p_jarvis.set_defaults(func=cmd_jarvis)
|
| 464 |
+
|
| 465 |
+
# Train
|
| 466 |
+
p_train = subparsers.add_parser("train", help="Train the model on text data")
|
| 467 |
+
p_train.add_argument("--data", required=True, help="Path to .txt file(s) or directory")
|
| 468 |
+
p_train.add_argument("--epochs", type=int, default=0, help="Number of epochs (auto if 0)")
|
| 469 |
+
p_train.add_argument("--batch-size", type=int, default=0, help="Batch size (auto if 0)")
|
| 470 |
+
p_train.add_argument("--seq-len", type=int, default=0, help="Sequence length (auto if 0)")
|
| 471 |
+
p_train.set_defaults(func=cmd_train)
|
| 472 |
+
|
| 473 |
+
# Serve
|
| 474 |
+
p_serve = subparsers.add_parser("serve", help="Start inference server")
|
| 475 |
+
p_serve.add_argument("--host", default="0.0.0.0", help="Host (default: 0.0.0.0)")
|
| 476 |
+
p_serve.add_argument("--port", type=int, default=8548, help="Port (default: 8548)")
|
| 477 |
+
p_serve.set_defaults(func=cmd_serve)
|
| 478 |
+
|
| 479 |
+
# Stats
|
| 480 |
+
p_stats = subparsers.add_parser("stats", help="Show model stats")
|
| 481 |
+
p_stats.set_defaults(func=cmd_stats)
|
| 482 |
+
|
| 483 |
+
# Export
|
| 484 |
+
p_export = subparsers.add_parser("export", help="Export model weights")
|
| 485 |
+
p_export.add_argument("--output", default="", help="Output path")
|
| 486 |
+
p_export.add_argument("--format", default="npz", help="Export format (npz)")
|
| 487 |
+
p_export.set_defaults(func=cmd_export)
|
| 488 |
+
|
| 489 |
+
# Goals
|
| 490 |
+
p_goals = subparsers.add_parser("goals", help="Manage goals, projects, and agents")
|
| 491 |
+
p_goals.add_argument("goal_action", choices=["create", "list", "agents", "start-agents", "stop-agents", "stats"],
|
| 492 |
+
help="Action to perform")
|
| 493 |
+
p_goals.add_argument("--title", default="", help="Goal title (for create)")
|
| 494 |
+
p_goals.add_argument("--description", default="", help="Goal description (for create)")
|
| 495 |
+
p_goals.add_argument("--priority", default="high", choices=["critical", "high", "medium", "low"])
|
| 496 |
+
p_goals.add_argument("--start-agents", action="store_true", help="Start agents after creating goal")
|
| 497 |
+
p_goals.set_defaults(func=cmd_goals)
|
| 498 |
+
|
| 499 |
+
# Daemon
|
| 500 |
+
p_daemon = subparsers.add_parser("daemon", help="Start always-on daemon (agents + refinement + skill creation)")
|
| 501 |
+
p_daemon.add_argument("--goal", default="", help="Optional goal to create and work on")
|
| 502 |
+
p_daemon.set_defaults(func=cmd_daemon)
|
| 503 |
+
|
| 504 |
+
# Subscribe
|
| 505 |
+
p_sub = subparsers.add_parser("subscribe", help="Subscribe for $1.00/month")
|
| 506 |
+
p_sub.set_defaults(func=cmd_subscribe)
|
| 507 |
+
|
| 508 |
+
# Unsubscribe
|
| 509 |
+
p_unsub = subparsers.add_parser("unsubscribe", help="Cancel subscription")
|
| 510 |
+
p_unsub.set_defaults(func=cmd_unsubscribe)
|
| 511 |
+
|
| 512 |
+
# Trial
|
| 513 |
+
p_trial = subparsers.add_parser("trial", help="Start 7-day free trial")
|
| 514 |
+
p_trial.set_defaults(func=cmd_trial)
|
| 515 |
+
|
| 516 |
+
# Founder unlock
|
| 517 |
+
p_unlock = subparsers.add_parser("unlock", help="Unlock free founder access")
|
| 518 |
+
p_unlock.add_argument("--password", default="", help="Founder password")
|
| 519 |
+
p_unlock.set_defaults(func=cmd_unlock)
|
| 520 |
+
|
| 521 |
+
# Auto-transfer
|
| 522 |
+
p_transfer = subparsers.add_parser("transfer", help="Process auto-transfers to bank account")
|
| 523 |
+
p_transfer.set_defaults(func=cmd_transfer)
|
| 524 |
+
|
| 525 |
+
# Bank info
|
| 526 |
+
p_bank = subparsers.add_parser("bank", help="Show bank account info")
|
| 527 |
+
p_bank.set_defaults(func=cmd_bank)
|
| 528 |
+
|
| 529 |
+
args = parser.parse_args()
|
| 530 |
+
if not args.command:
|
| 531 |
+
parser.print_help()
|
| 532 |
+
sys.exit(1)
|
| 533 |
+
|
| 534 |
+
logging.basicConfig(
|
| 535 |
+
level=logging.INFO,
|
| 536 |
+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
| 537 |
+
datefmt="%H:%M:%S",
|
| 538 |
+
)
|
| 539 |
+
|
| 540 |
+
args.func(args)
|
| 541 |
+
|
| 542 |
+
|
| 543 |
+
if __name__ == "__main__":
|
| 544 |
+
main()
|
singularity_llm/config.py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|
|
|
|
|
|
|
|
| 1 |
+
"""Configuration and hardware detection for Singularity LLM."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import logging
|
| 6 |
+
import os
|
| 7 |
+
import platform
|
| 8 |
+
from dataclasses import dataclass, field
|
| 9 |
+
from enum import Enum
|
| 10 |
+
from typing import Any
|
| 11 |
+
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
import psutil
|
| 16 |
+
except ImportError:
|
| 17 |
+
psutil = None # type: ignore[assignment]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class HardwareTier(str, Enum):
|
| 21 |
+
MOBILE = "mobile"
|
| 22 |
+
MINIMAL = "minimal"
|
| 23 |
+
LIGHT = "light"
|
| 24 |
+
STANDARD = "standard"
|
| 25 |
+
FULL = "full"
|
| 26 |
+
MAXIMUM = "maximum"
|
| 27 |
+
DATACENTER = "datacenter"
|
| 28 |
+
SUPERCOMPUTER = "supercomputer"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@dataclass
|
| 32 |
+
class ModelConfig:
|
| 33 |
+
"""Model architecture config — auto-adjusted per hardware tier."""
|
| 34 |
+
n_layers: int = 3
|
| 35 |
+
n_heads: int = 4
|
| 36 |
+
d_model: int = 256
|
| 37 |
+
d_ff: int = 512
|
| 38 |
+
vocab_size: int = 4096
|
| 39 |
+
max_seq_len: int = 512
|
| 40 |
+
dropout: float = 0.0
|
| 41 |
+
|
| 42 |
+
@property
|
| 43 |
+
def d_head(self) -> int:
|
| 44 |
+
return self.d_model // self.n_heads
|
| 45 |
+
|
| 46 |
+
@property
|
| 47 |
+
def param_count(self) -> int:
|
| 48 |
+
"""Rough parameter count estimate."""
|
| 49 |
+
emb = self.vocab_size * self.d_model
|
| 50 |
+
attn = 4 * self.d_model * self.d_model * self.n_layers
|
| 51 |
+
ffn = 2 * self.d_model * self.d_ff * self.n_layers
|
| 52 |
+
ln = 2 * self.d_model * self.n_layers
|
| 53 |
+
return emb + attn + ffn + ln
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@dataclass
|
| 57 |
+
class QuantConfig:
|
| 58 |
+
"""Singularity weight quantization config."""
|
| 59 |
+
format: str = "q4_k_m"
|
| 60 |
+
bpw: float = 4.0
|
| 61 |
+
|
| 62 |
+
@staticmethod
|
| 63 |
+
def for_tier(tier: HardwareTier) -> "QuantConfig":
|
| 64 |
+
formats = {
|
| 65 |
+
HardwareTier.MOBILE: ("ternary", 1.585),
|
| 66 |
+
HardwareTier.MINIMAL: ("q2_k", 2.0),
|
| 67 |
+
HardwareTier.LIGHT: ("q3_k_s", 3.0),
|
| 68 |
+
HardwareTier.STANDARD: ("q4_k_m", 4.0),
|
| 69 |
+
HardwareTier.FULL: ("q5_k_m", 5.0),
|
| 70 |
+
HardwareTier.MAXIMUM: ("q8_0", 8.0),
|
| 71 |
+
HardwareTier.DATACENTER: ("fp8_e4m3", 8.0),
|
| 72 |
+
HardwareTier.SUPERCOMPUTER: ("fp16", 16.0),
|
| 73 |
+
}
|
| 74 |
+
fmt, bpw = formats.get(tier, ("q4_k_m", 4.0))
|
| 75 |
+
return QuantConfig(format=fmt, bpw=bpw)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
@dataclass
|
| 79 |
+
class StorageConfig:
|
| 80 |
+
"""Storage paths and limits."""
|
| 81 |
+
data_dir: str = os.path.expanduser("~/.singularity-llm")
|
| 82 |
+
skill_storage_mb: int = 200
|
| 83 |
+
max_skills: int = 2000
|
| 84 |
+
|
| 85 |
+
@staticmethod
|
| 86 |
+
def for_tier(tier: HardwareTier) -> "StorageConfig":
|
| 87 |
+
limits = {
|
| 88 |
+
HardwareTier.MOBILE: (50, 500),
|
| 89 |
+
HardwareTier.MINIMAL: (200, 2000),
|
| 90 |
+
HardwareTier.LIGHT: (1000, 10000),
|
| 91 |
+
HardwareTier.STANDARD: (5000, 50000),
|
| 92 |
+
HardwareTier.FULL: (20000, 200000),
|
| 93 |
+
HardwareTier.MAXIMUM: (50000, 500000),
|
| 94 |
+
HardwareTier.DATACENTER: (200000, 2000000),
|
| 95 |
+
HardwareTier.SUPERCOMPUTER: (1000000, 10000000),
|
| 96 |
+
}
|
| 97 |
+
mb, skills = limits.get(tier, (200, 2000))
|
| 98 |
+
return StorageConfig(skill_storage_mb=mb, max_skills=skills)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def detect_hardware() -> HardwareTier:
|
| 102 |
+
"""Detect hardware tier based on available RAM, CPU, and GPU."""
|
| 103 |
+
ram_mb = 0
|
| 104 |
+
cpu_cores = os.cpu_count() or 1
|
| 105 |
+
|
| 106 |
+
if psutil:
|
| 107 |
+
ram_mb = psutil.virtual_memory().available // (1024 * 1024)
|
| 108 |
+
cpu_cores = psutil.cpu_count() or cpu_cores
|
| 109 |
+
|
| 110 |
+
gpu_vram_mb = 0
|
| 111 |
+
try:
|
| 112 |
+
import torch
|
| 113 |
+
if torch.cuda.is_available():
|
| 114 |
+
gpu_vram_mb = torch.cuda.get_device_properties(0).total_memory // (1024 * 1024)
|
| 115 |
+
except ImportError:
|
| 116 |
+
pass
|
| 117 |
+
|
| 118 |
+
is_mobile = platform.machine().startswith(("arm", "aarch")) and ram_mb < 2048
|
| 119 |
+
|
| 120 |
+
if is_mobile or ram_mb < 2048:
|
| 121 |
+
return HardwareTier.MOBILE
|
| 122 |
+
if ram_mb < 4096:
|
| 123 |
+
return HardwareTier.MINIMAL
|
| 124 |
+
if ram_mb < 8192:
|
| 125 |
+
return HardwareTier.LIGHT
|
| 126 |
+
if ram_mb < 16384 or gpu_vram_mb < 6144:
|
| 127 |
+
return HardwareTier.STANDARD
|
| 128 |
+
if ram_mb < 32768 or gpu_vram_mb < 12288:
|
| 129 |
+
return HardwareTier.FULL
|
| 130 |
+
if ram_mb < 65536:
|
| 131 |
+
return HardwareTier.MAXIMUM
|
| 132 |
+
if ram_mb < 262144:
|
| 133 |
+
return HardwareTier.DATACENTER
|
| 134 |
+
return HardwareTier.SUPERCOMPUTER
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def get_model_config(tier: HardwareTier) -> ModelConfig:
|
| 138 |
+
"""Get optimal model config for a hardware tier."""
|
| 139 |
+
configs = {
|
| 140 |
+
HardwareTier.MOBILE: ModelConfig(n_layers=2, n_heads=4, d_model=128, d_ff=256, vocab_size=2048, max_seq_len=256),
|
| 141 |
+
HardwareTier.MINIMAL: ModelConfig(n_layers=3, n_heads=4, d_model=256, d_ff=512, vocab_size=4096, max_seq_len=512),
|
| 142 |
+
HardwareTier.LIGHT: ModelConfig(n_layers=4, n_heads=8, d_model=384, d_ff=768, vocab_size=8192, max_seq_len=1024),
|
| 143 |
+
HardwareTier.STANDARD: ModelConfig(n_layers=6, n_heads=8, d_model=512, d_ff=1024, vocab_size=16384, max_seq_len=2048),
|
| 144 |
+
HardwareTier.FULL: ModelConfig(n_layers=8, n_heads=16, d_model=768, d_ff=2048, vocab_size=32000, max_seq_len=4096),
|
| 145 |
+
HardwareTier.MAXIMUM: ModelConfig(n_layers=12, n_heads=16, d_model=1024, d_ff=3072, vocab_size=64000, max_seq_len=8192),
|
| 146 |
+
HardwareTier.DATACENTER: ModelConfig(n_layers=24, n_heads=32, d_model=2048, d_ff=6144, vocab_size=128000, max_seq_len=16384),
|
| 147 |
+
HardwareTier.SUPERCOMPUTER: ModelConfig(n_layers=48, n_heads=64, d_model=4096, d_ff=12288, vocab_size=128000, max_seq_len=32768),
|
| 148 |
+
}
|
| 149 |
+
return configs.get(tier, configs[HardwareTier.MINIMAL])
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
@dataclass
|
| 153 |
+
class Settings:
|
| 154 |
+
"""Global settings for Singularity LLM."""
|
| 155 |
+
tier: HardwareTier = field(default_factory=detect_hardware)
|
| 156 |
+
model: ModelConfig = field(default_factory=lambda: get_model_config(detect_hardware()))
|
| 157 |
+
quant: QuantConfig = field(default_factory=lambda: QuantConfig.for_tier(detect_hardware()))
|
| 158 |
+
storage: StorageConfig = field(default_factory=lambda: StorageConfig.for_tier(detect_hardware()))
|
| 159 |
+
|
| 160 |
+
@staticmethod
|
| 161 |
+
def from_env() -> "Settings":
|
| 162 |
+
tier = detect_hardware()
|
| 163 |
+
return Settings(
|
| 164 |
+
tier=tier,
|
| 165 |
+
model=get_model_config(tier),
|
| 166 |
+
quant=QuantConfig.for_tier(tier),
|
| 167 |
+
storage=StorageConfig.for_tier(tier),
|
| 168 |
+
)
|
singularity_llm/connectors/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Connectors package — API clients for connecting to external services.
|
| 2 |
+
|
| 3 |
+
Provides REST API client, webhook support, and common service connectors.
|
| 4 |
+
All connections are configurable — no hardcoded API keys.
|
| 5 |
+
"""
|
singularity_llm/connectors/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (429 Bytes). View file
|
|
|
singularity_llm/connectors/__pycache__/api_client.cpython-311.pyc
ADDED
|
Binary file (15.3 kB). View file
|
|
|
singularity_llm/connectors/__pycache__/services.cpython-311.pyc
ADDED
|
Binary file (12.9 kB). View file
|
|
|
singularity_llm/connectors/__pycache__/webhooks.cpython-311.pyc
ADDED
|
Binary file (8.47 kB). View file
|
|
|
singularity_llm/connectors/api_client.py
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""REST API Client — connect to external APIs and services.
|
| 2 |
+
|
| 3 |
+
Features:
|
| 4 |
+
- Generic REST client (GET, POST, PUT, DELETE)
|
| 5 |
+
- Authentication: API key, Bearer token, Basic auth, custom headers
|
| 6 |
+
- Request/response logging
|
| 7 |
+
- Rate limiting (configurable requests per second)
|
| 8 |
+
- Retry with exponential backoff
|
| 9 |
+
- Timeout handling
|
| 10 |
+
- JSON and raw response support
|
| 11 |
+
- Connection pooling (via urllib)
|
| 12 |
+
|
| 13 |
+
Uses only stdlib urllib — no external dependencies.
|
| 14 |
+
100% local: no data sent to any cloud service unless explicitly configured.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import json
|
| 20 |
+
import logging
|
| 21 |
+
import time
|
| 22 |
+
import urllib.error
|
| 23 |
+
import urllib.request
|
| 24 |
+
from dataclasses import dataclass, field
|
| 25 |
+
from typing import Any
|
| 26 |
+
|
| 27 |
+
logger = logging.getLogger(__name__)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@dataclass
|
| 31 |
+
class APIConfig:
|
| 32 |
+
"""Configuration for an API connection."""
|
| 33 |
+
name: str
|
| 34 |
+
base_url: str
|
| 35 |
+
api_key: str = ""
|
| 36 |
+
auth_type: str = "api_key" # "api_key", "bearer", "basic", "none", "custom"
|
| 37 |
+
headers: dict[str, str] = field(default_factory=dict)
|
| 38 |
+
timeout_s: float = 30.0
|
| 39 |
+
max_retries: int = 3
|
| 40 |
+
rate_limit_s: float = 0.0 # min seconds between requests
|
| 41 |
+
retry_backoff: float = 1.5
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@dataclass
|
| 45 |
+
class APIResponse:
|
| 46 |
+
"""Response from an API call."""
|
| 47 |
+
success: bool
|
| 48 |
+
status_code: int
|
| 49 |
+
data: Any = None
|
| 50 |
+
error: str = ""
|
| 51 |
+
elapsed_s: float = 0.0
|
| 52 |
+
url: str = ""
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class RESTClient:
|
| 56 |
+
"""Generic REST API client with auth, retries, and rate limiting."""
|
| 57 |
+
|
| 58 |
+
def __init__(self, config: APIConfig) -> None:
|
| 59 |
+
self.config = config
|
| 60 |
+
self._last_request_time = 0.0
|
| 61 |
+
self._stats = {
|
| 62 |
+
"total_requests": 0,
|
| 63 |
+
"successful_requests": 0,
|
| 64 |
+
"failed_requests": 0,
|
| 65 |
+
"retries": 0,
|
| 66 |
+
"avg_response_time_s": 0.0,
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
def _build_headers(self, extra: dict[str, str] | None = None) -> dict[str, str]:
|
| 70 |
+
"""Build request headers with authentication."""
|
| 71 |
+
headers = dict(self.config.headers)
|
| 72 |
+
if extra:
|
| 73 |
+
headers.update(extra)
|
| 74 |
+
|
| 75 |
+
if self.config.auth_type == "api_key" and self.config.api_key:
|
| 76 |
+
headers["X-API-Key"] = self.config.api_key
|
| 77 |
+
elif self.config.auth_type == "bearer" and self.config.api_key:
|
| 78 |
+
headers["Authorization"] = f"Bearer {self.config.api_key}"
|
| 79 |
+
elif self.config.auth_type == "basic" and self.config.api_key:
|
| 80 |
+
import base64
|
| 81 |
+
headers["Authorization"] = f"Basic {base64.b64encode(self.config.api_key.encode()).decode()}"
|
| 82 |
+
|
| 83 |
+
return headers
|
| 84 |
+
|
| 85 |
+
def _rate_limit(self) -> None:
|
| 86 |
+
"""Enforce rate limiting."""
|
| 87 |
+
if self.config.rate_limit_s > 0:
|
| 88 |
+
elapsed = time.time() - self._last_request_time
|
| 89 |
+
if elapsed < self.config.rate_limit_s:
|
| 90 |
+
time.sleep(self.config.rate_limit_s - elapsed)
|
| 91 |
+
self._last_request_time = time.time()
|
| 92 |
+
|
| 93 |
+
def request(self, method: str, endpoint: str, data: dict | None = None,
|
| 94 |
+
params: dict | None = None, headers: dict | None = None) -> APIResponse:
|
| 95 |
+
"""Make an HTTP request.
|
| 96 |
+
|
| 97 |
+
Args:
|
| 98 |
+
method: GET, POST, PUT, DELETE
|
| 99 |
+
endpoint: API endpoint (appended to base_url)
|
| 100 |
+
data: request body (JSON)
|
| 101 |
+
params: query parameters
|
| 102 |
+
headers: extra headers
|
| 103 |
+
"""
|
| 104 |
+
url = self._build_url(endpoint, params)
|
| 105 |
+
body = json.dumps(data).encode() if data else None
|
| 106 |
+
req_headers = self._build_headers(headers)
|
| 107 |
+
if body:
|
| 108 |
+
req_headers["Content-Type"] = "application/json"
|
| 109 |
+
|
| 110 |
+
for attempt in range(self.config.max_retries + 1):
|
| 111 |
+
self._rate_limit()
|
| 112 |
+
t0 = time.time()
|
| 113 |
+
self._stats["total_requests"] += 1
|
| 114 |
+
|
| 115 |
+
try:
|
| 116 |
+
req = urllib.request.Request(url, data=body, method=method, headers=req_headers)
|
| 117 |
+
with urllib.request.urlopen(req, timeout=self.config.timeout_s) as resp:
|
| 118 |
+
raw = resp.read().decode()
|
| 119 |
+
elapsed = time.time() - t0
|
| 120 |
+
self._stats["successful_requests"] += 1
|
| 121 |
+
self._update_avg_time(elapsed)
|
| 122 |
+
|
| 123 |
+
try:
|
| 124 |
+
parsed = json.loads(raw)
|
| 125 |
+
except json.JSONDecodeError:
|
| 126 |
+
parsed = raw
|
| 127 |
+
|
| 128 |
+
return APIResponse(
|
| 129 |
+
success=True, status_code=resp.status,
|
| 130 |
+
data=parsed, elapsed_s=elapsed, url=url,
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
except urllib.error.HTTPError as e:
|
| 134 |
+
elapsed = time.time() - t0
|
| 135 |
+
error_body = ""
|
| 136 |
+
try:
|
| 137 |
+
error_body = e.read().decode()
|
| 138 |
+
except Exception:
|
| 139 |
+
pass
|
| 140 |
+
|
| 141 |
+
if attempt < self.config.max_retries and e.code >= 500:
|
| 142 |
+
self._stats["retries"] += 1
|
| 143 |
+
wait = self.config.retry_backoff ** (attempt + 1)
|
| 144 |
+
logger.warning("Retry %d/%d for %s (HTTP %d) after %.1fs",
|
| 145 |
+
attempt + 1, self.config.max_retries, url, e.code, wait)
|
| 146 |
+
time.sleep(wait)
|
| 147 |
+
continue
|
| 148 |
+
|
| 149 |
+
self._stats["failed_requests"] += 1
|
| 150 |
+
return APIResponse(
|
| 151 |
+
success=False, status_code=e.code,
|
| 152 |
+
error=f"HTTP {e.code}: {error_body[:200]}", elapsed_s=elapsed, url=url,
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
elapsed = time.time() - t0
|
| 157 |
+
if attempt < self.config.max_retries:
|
| 158 |
+
self._stats["retries"] += 1
|
| 159 |
+
wait = self.config.retry_backoff ** (attempt + 1)
|
| 160 |
+
logger.warning("Retry %d/%d for %s: %s", attempt + 1, self.config.max_retries, url, e)
|
| 161 |
+
time.sleep(wait)
|
| 162 |
+
continue
|
| 163 |
+
|
| 164 |
+
self._stats["failed_requests"] += 1
|
| 165 |
+
return APIResponse(
|
| 166 |
+
success=False, status_code=0,
|
| 167 |
+
error=str(e), elapsed_s=elapsed, url=url,
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
return APIResponse(success=False, status_code=0, error="Max retries exceeded", url=url)
|
| 171 |
+
|
| 172 |
+
def get(self, endpoint: str, params: dict | None = None) -> APIResponse:
|
| 173 |
+
return self.request("GET", endpoint, params=params)
|
| 174 |
+
|
| 175 |
+
def post(self, endpoint: str, data: dict | None = None) -> APIResponse:
|
| 176 |
+
return self.request("POST", endpoint, data=data)
|
| 177 |
+
|
| 178 |
+
def put(self, endpoint: str, data: dict | None = None) -> APIResponse:
|
| 179 |
+
return self.request("PUT", endpoint, data=data)
|
| 180 |
+
|
| 181 |
+
def delete(self, endpoint: str) -> APIResponse:
|
| 182 |
+
return self.request("DELETE", endpoint)
|
| 183 |
+
|
| 184 |
+
def _build_url(self, endpoint: str, params: dict | None = None) -> str:
|
| 185 |
+
url = f"{self.config.base_url.rstrip('/')}/{endpoint.lstrip('/')}"
|
| 186 |
+
if params:
|
| 187 |
+
import urllib.parse
|
| 188 |
+
query = urllib.parse.urlencode(params)
|
| 189 |
+
url = f"{url}?{query}"
|
| 190 |
+
return url
|
| 191 |
+
|
| 192 |
+
def _update_avg_time(self, elapsed: float) -> None:
|
| 193 |
+
total = self._stats["successful_requests"]
|
| 194 |
+
self._stats["avg_response_time_s"] = (
|
| 195 |
+
(self._stats["avg_response_time_s"] * (total - 1) + elapsed) / total
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
def get_stats(self) -> dict[str, Any]:
|
| 199 |
+
return {**self._stats, "name": self.config.name, "base_url": self.config.base_url}
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
class ConnectorRegistry:
|
| 203 |
+
"""Registry of named API connectors.
|
| 204 |
+
|
| 205 |
+
Allows the LLM to connect to multiple external services.
|
| 206 |
+
Connectors are registered with a name and can be called by the LLM
|
| 207 |
+
via tool calls: [TOOL: api_call("service_name", "GET", "/endpoint")]
|
| 208 |
+
"""
|
| 209 |
+
|
| 210 |
+
def __init__(self) -> None:
|
| 211 |
+
self._connectors: dict[str, RESTClient] = {}
|
| 212 |
+
self._stats = {"total_connectors": 0, "total_calls": 0}
|
| 213 |
+
|
| 214 |
+
def register(self, name: str, config: APIConfig) -> None:
|
| 215 |
+
"""Register a named API connector."""
|
| 216 |
+
config.name = name
|
| 217 |
+
client = RESTClient(config)
|
| 218 |
+
self._connectors[name] = client
|
| 219 |
+
self._stats["total_connectors"] += 1
|
| 220 |
+
logger.info("Registered API connector: %s → %s", name, config.base_url)
|
| 221 |
+
|
| 222 |
+
def get(self, name: str) -> RESTClient | None:
|
| 223 |
+
return self._connectors.get(name)
|
| 224 |
+
|
| 225 |
+
def call(self, name: str, method: str, endpoint: str,
|
| 226 |
+
data: dict | None = None, params: dict | None = None) -> APIResponse:
|
| 227 |
+
"""Call a registered connector."""
|
| 228 |
+
client = self._connectors.get(name)
|
| 229 |
+
if client is None:
|
| 230 |
+
return APIResponse(success=False, status_code=0, error=f"Connector '{name}' not found")
|
| 231 |
+
self._stats["total_calls"] += 1
|
| 232 |
+
return client.request(method, endpoint, data=data, params=params)
|
| 233 |
+
|
| 234 |
+
def list_connectors(self) -> list[dict[str, Any]]:
|
| 235 |
+
return [{"name": name, **client.get_stats()} for name, client in self._connectors.items()]
|
| 236 |
+
|
| 237 |
+
def get_stats(self) -> dict[str, Any]:
|
| 238 |
+
return {**self._stats, "connectors": {name: c.get_stats() for name, c in self._connectors.items()}}
|
singularity_llm/connectors/services.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Service connectors — pre-built connectors for common services.
|
| 2 |
+
|
| 3 |
+
Each connector is a thin wrapper around RESTClient with service-specific
|
| 4 |
+
methods. All require explicit configuration — no hardcoded keys.
|
| 5 |
+
|
| 6 |
+
Services:
|
| 7 |
+
- HTTPFetcher: generic web page / API fetcher
|
| 8 |
+
- WebSocketClient: basic WebSocket support (via stdlib)
|
| 9 |
+
- EmailConnector: send/receive via SMTP/IMAP (stdlib)
|
| 10 |
+
- DiscordWebhook: send messages via Discord webhook
|
| 11 |
+
- SlackWebhook: send messages via Slack webhook
|
| 12 |
+
- GitHubAPI: interact with GitHub API
|
| 13 |
+
- CustomAPI: user-defined API connector
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
import json
|
| 19 |
+
import logging
|
| 20 |
+
from typing import Any
|
| 21 |
+
|
| 22 |
+
from .api_client import RESTClient, APIConfig, APIResponse
|
| 23 |
+
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class HTTPFetcher:
|
| 28 |
+
"""Generic HTTP fetcher — fetch web pages and APIs."""
|
| 29 |
+
|
| 30 |
+
def __init__(self, timeout_s: float = 15.0) -> None:
|
| 31 |
+
self.config = APIConfig(
|
| 32 |
+
name="http_fetcher", base_url="", auth_type="none",
|
| 33 |
+
timeout_s=timeout_s, max_retries=2,
|
| 34 |
+
)
|
| 35 |
+
self.client = RESTClient(self.config)
|
| 36 |
+
|
| 37 |
+
def fetch(self, url: str) -> APIResponse:
|
| 38 |
+
"""Fetch a URL and return the response."""
|
| 39 |
+
# Override base_url for this request
|
| 40 |
+
self.config.base_url = ""
|
| 41 |
+
# Use the full URL directly
|
| 42 |
+
import urllib.request
|
| 43 |
+
try:
|
| 44 |
+
req = urllib.request.Request(url, headers={"User-Agent": "singularity-llm/0.1"})
|
| 45 |
+
with urllib.request.urlopen(req, timeout=self.config.timeout_s) as resp:
|
| 46 |
+
raw = resp.read().decode()
|
| 47 |
+
return APIResponse(success=True, status_code=resp.status, data=raw, url=url)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return APIResponse(success=False, status_code=0, error=str(e), url=url)
|
| 50 |
+
|
| 51 |
+
def fetch_json(self, url: str) -> APIResponse:
|
| 52 |
+
"""Fetch a URL and parse JSON."""
|
| 53 |
+
resp = self.fetch(url)
|
| 54 |
+
if resp.success and isinstance(resp.data, str):
|
| 55 |
+
try:
|
| 56 |
+
resp.data = json.loads(resp.data)
|
| 57 |
+
except json.JSONDecodeError:
|
| 58 |
+
pass
|
| 59 |
+
return resp
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class DiscordWebhook:
|
| 63 |
+
"""Send messages to Discord via webhook URL."""
|
| 64 |
+
|
| 65 |
+
def __init__(self, webhook_url: str) -> None:
|
| 66 |
+
self.webhook_url = webhook_url
|
| 67 |
+
|
| 68 |
+
def send(self, content: str, username: str = "Singularity LLM") -> bool:
|
| 69 |
+
"""Send a message to Discord."""
|
| 70 |
+
import urllib.request
|
| 71 |
+
payload = json.dumps({"content": content, "username": username}).encode()
|
| 72 |
+
try:
|
| 73 |
+
req = urllib.request.Request(
|
| 74 |
+
self.webhook_url, data=payload, method="POST",
|
| 75 |
+
headers={"Content-Type": "application/json"}
|
| 76 |
+
)
|
| 77 |
+
with urllib.request.urlopen(req, timeout=10) as resp:
|
| 78 |
+
return resp.status < 300
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error("Discord webhook failed: %s", e)
|
| 81 |
+
return False
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
class SlackWebhook:
|
| 85 |
+
"""Send messages to Slack via webhook URL."""
|
| 86 |
+
|
| 87 |
+
def __init__(self, webhook_url: str) -> None:
|
| 88 |
+
self.webhook_url = webhook_url
|
| 89 |
+
|
| 90 |
+
def send(self, text: str) -> bool:
|
| 91 |
+
"""Send a message to Slack."""
|
| 92 |
+
import urllib.request
|
| 93 |
+
payload = json.dumps({"text": text}).encode()
|
| 94 |
+
try:
|
| 95 |
+
req = urllib.request.Request(
|
| 96 |
+
self.webhook_url, data=payload, method="POST",
|
| 97 |
+
headers={"Content-Type": "application/json"}
|
| 98 |
+
)
|
| 99 |
+
with urllib.request.urlopen(req, timeout=10) as resp:
|
| 100 |
+
return resp.status < 300
|
| 101 |
+
except Exception as e:
|
| 102 |
+
logger.error("Slack webhook failed: %s", e)
|
| 103 |
+
return False
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
class GitHubConnector:
|
| 107 |
+
"""Interact with GitHub API."""
|
| 108 |
+
|
| 109 |
+
def __init__(self, token: str = "") -> None:
|
| 110 |
+
self.config = APIConfig(
|
| 111 |
+
name="github", base_url="https://api.github.com",
|
| 112 |
+
api_key=token, auth_type="bearer" if token else "none",
|
| 113 |
+
timeout_s=15.0,
|
| 114 |
+
)
|
| 115 |
+
self.client = RESTClient(self.config)
|
| 116 |
+
|
| 117 |
+
def get_repo(self, owner: str, repo: str) -> APIResponse:
|
| 118 |
+
return self.client.get(f"/repos/{owner}/{repo}")
|
| 119 |
+
|
| 120 |
+
def list_issues(self, owner: str, repo: str) -> APIResponse:
|
| 121 |
+
return self.client.get(f"/repos/{owner}/{repo}/issues")
|
| 122 |
+
|
| 123 |
+
def create_issue(self, owner: str, repo: str, title: str, body: str = "") -> APIResponse:
|
| 124 |
+
return self.client.post(f"/repos/{owner}/{repo}/issues", data={"title": title, "body": body})
|
| 125 |
+
|
| 126 |
+
def get_user(self, username: str) -> APIResponse:
|
| 127 |
+
return self.client.get(f"/users/{username}")
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
class ServiceManager:
|
| 131 |
+
"""Manages all service connectors.
|
| 132 |
+
|
| 133 |
+
Central registry for all external service connections.
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
def __init__(self) -> None:
|
| 137 |
+
self.fetcher = HTTPFetcher()
|
| 138 |
+
self._services: dict[str, Any] = {"http_fetcher": self.fetcher}
|
| 139 |
+
self._stats = {"total_services": 1, "total_calls": 0}
|
| 140 |
+
|
| 141 |
+
def add_discord(self, webhook_url: str) -> None:
|
| 142 |
+
self._services["discord"] = DiscordWebhook(webhook_url)
|
| 143 |
+
self._stats["total_services"] += 1
|
| 144 |
+
|
| 145 |
+
def add_slack(self, webhook_url: str) -> None:
|
| 146 |
+
self._services["slack"] = SlackWebhook(webhook_url)
|
| 147 |
+
self._stats["total_services"] += 1
|
| 148 |
+
|
| 149 |
+
def add_github(self, token: str = "") -> None:
|
| 150 |
+
self._services["github"] = GitHubConnector(token)
|
| 151 |
+
self._stats["total_services"] += 1
|
| 152 |
+
|
| 153 |
+
def add_custom(self, name: str, config: APIConfig) -> None:
|
| 154 |
+
self._services[name] = RESTClient(config)
|
| 155 |
+
self._stats["total_services"] += 1
|
| 156 |
+
|
| 157 |
+
def get(self, name: str) -> Any | None:
|
| 158 |
+
return self._services.get(name)
|
| 159 |
+
|
| 160 |
+
def call_service(self, name: str, method: str, *args, **kwargs) -> Any:
|
| 161 |
+
"""Call a method on a registered service."""
|
| 162 |
+
service = self._services.get(name)
|
| 163 |
+
if service is None:
|
| 164 |
+
return {"error": f"Service '{name}' not found"}
|
| 165 |
+
self._stats["total_calls"] += 1
|
| 166 |
+
fn = getattr(service, method, None)
|
| 167 |
+
if fn is None:
|
| 168 |
+
return {"error": f"Method '{method}' not found on service '{name}'"}
|
| 169 |
+
try:
|
| 170 |
+
return fn(*args, **kwargs)
|
| 171 |
+
except Exception as e:
|
| 172 |
+
return {"error": str(e)}
|
| 173 |
+
|
| 174 |
+
def list_services(self) -> list[str]:
|
| 175 |
+
return list(self._services.keys())
|
| 176 |
+
|
| 177 |
+
def get_stats(self) -> dict[str, Any]:
|
| 178 |
+
return {**self._stats, "services": list(self._services.keys())}
|
singularity_llm/connectors/webhooks.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Webhook support — receive incoming webhooks and send outgoing webhooks.
|
| 2 |
+
|
| 3 |
+
Incoming: FastAPI endpoint that receives webhooks and routes them to the LLM.
|
| 4 |
+
Outgoing: Send webhook notifications when events happen (goal completed, etc.).
|
| 5 |
+
|
| 6 |
+
Webhooks allow external systems to trigger the LLM or be notified by it.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import hashlib
|
| 12 |
+
import hmac
|
| 13 |
+
import json
|
| 14 |
+
import logging
|
| 15 |
+
import time
|
| 16 |
+
import urllib.request
|
| 17 |
+
from dataclasses import dataclass, field
|
| 18 |
+
from typing import Any, Callable
|
| 19 |
+
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@dataclass
|
| 24 |
+
class WebhookEndpoint:
|
| 25 |
+
"""A registered incoming webhook endpoint."""
|
| 26 |
+
path: str
|
| 27 |
+
secret: str = ""
|
| 28 |
+
handler: Callable[[dict], dict] | None = None
|
| 29 |
+
calls: int = 0
|
| 30 |
+
last_call: float = 0.0
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class WebhookManager:
|
| 34 |
+
"""Manages incoming and outgoing webhooks.
|
| 35 |
+
|
| 36 |
+
Incoming webhooks are registered with a path and optional secret.
|
| 37 |
+
When a POST request hits the path, the handler is called.
|
| 38 |
+
|
| 39 |
+
Outgoing webhooks are sent to external URLs to notify them of events.
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
def __init__(self) -> None:
|
| 43 |
+
self._endpoints: dict[str, WebhookEndpoint] = {}
|
| 44 |
+
self._outgoing: list[dict[str, str]] = []
|
| 45 |
+
self._stats = {
|
| 46 |
+
"incoming_received": 0,
|
| 47 |
+
"incoming_processed": 0,
|
| 48 |
+
"incoming_rejected": 0,
|
| 49 |
+
"outgoing_sent": 0,
|
| 50 |
+
"outgoing_failed": 0,
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
def register_endpoint(self, path: str, handler: Callable[[dict], dict],
|
| 54 |
+
secret: str = "") -> None:
|
| 55 |
+
"""Register an incoming webhook endpoint."""
|
| 56 |
+
self._endpoints[path] = WebhookEndpoint(path=path, secret=secret, handler=handler)
|
| 57 |
+
logger.info("Registered webhook endpoint: %s", path)
|
| 58 |
+
|
| 59 |
+
def handle_request(self, path: str, body: dict, signature: str = "") -> dict:
|
| 60 |
+
"""Handle an incoming webhook request.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
path: webhook path
|
| 64 |
+
body: request body
|
| 65 |
+
signature: HMAC signature for verification
|
| 66 |
+
Returns:
|
| 67 |
+
Response dict
|
| 68 |
+
"""
|
| 69 |
+
self._stats["incoming_received"] += 1
|
| 70 |
+
|
| 71 |
+
endpoint = self._endpoints.get(path)
|
| 72 |
+
if endpoint is None:
|
| 73 |
+
self._stats["incoming_rejected"] += 1
|
| 74 |
+
return {"error": "Unknown webhook endpoint", "path": path}
|
| 75 |
+
|
| 76 |
+
# Verify signature if secret is set
|
| 77 |
+
if endpoint.secret:
|
| 78 |
+
expected = hmac.new(
|
| 79 |
+
endpoint.secret.encode(),
|
| 80 |
+
json.dumps(body, sort_keys=True).encode(),
|
| 81 |
+
hashlib.sha256
|
| 82 |
+
).hexdigest()
|
| 83 |
+
if not hmac.compare_digest(expected, signature):
|
| 84 |
+
self._stats["incoming_rejected"] += 1
|
| 85 |
+
return {"error": "Invalid signature"}
|
| 86 |
+
|
| 87 |
+
endpoint.calls += 1
|
| 88 |
+
endpoint.last_call = time.time()
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
result = endpoint.handler(body) if endpoint.handler else {"received": True}
|
| 92 |
+
self._stats["incoming_processed"] += 1
|
| 93 |
+
return result
|
| 94 |
+
except Exception as e:
|
| 95 |
+
logger.error("Webhook handler error: %s", e)
|
| 96 |
+
return {"error": str(e)}
|
| 97 |
+
|
| 98 |
+
def send(self, url: str, event: str, data: dict, secret: str = "") -> bool:
|
| 99 |
+
"""Send an outgoing webhook notification.
|
| 100 |
+
|
| 101 |
+
Args:
|
| 102 |
+
url: target URL
|
| 103 |
+
event: event name
|
| 104 |
+
data: payload
|
| 105 |
+
secret: HMAC secret for signing
|
| 106 |
+
Returns:
|
| 107 |
+
True if sent successfully
|
| 108 |
+
"""
|
| 109 |
+
payload = json.dumps({"event": event, "data": data, "timestamp": time.time()}).encode()
|
| 110 |
+
|
| 111 |
+
headers = {"Content-Type": "application/json"}
|
| 112 |
+
if secret:
|
| 113 |
+
signature = hmac.new(secret.encode(), payload, hashlib.sha256).hexdigest()
|
| 114 |
+
headers["X-Webhook-Signature"] = signature
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
req = urllib.request.Request(url, data=payload, method="POST", headers=headers)
|
| 118 |
+
with urllib.request.urlopen(req, timeout=10) as resp:
|
| 119 |
+
if resp.status < 300:
|
| 120 |
+
self._stats["outgoing_sent"] += 1
|
| 121 |
+
self._outgoing.append({"url": url, "event": event, "time": str(time.time())})
|
| 122 |
+
logger.info("Webhook sent to %s: %s", url, event)
|
| 123 |
+
return True
|
| 124 |
+
except Exception as e:
|
| 125 |
+
logger.error("Webhook send failed: %s", e)
|
| 126 |
+
|
| 127 |
+
self._stats["outgoing_failed"] += 1
|
| 128 |
+
return False
|
| 129 |
+
|
| 130 |
+
def notify_event(self, event: str, data: dict) -> None:
|
| 131 |
+
"""Notify all registered outgoing webhooks of an event."""
|
| 132 |
+
for hook in list(self._outgoing):
|
| 133 |
+
self.send(hook["url"], event, data)
|
| 134 |
+
|
| 135 |
+
def list_endpoints(self) -> list[str]:
|
| 136 |
+
return list(self._endpoints.keys())
|
| 137 |
+
|
| 138 |
+
def get_stats(self) -> dict[str, Any]:
|
| 139 |
+
return {**self._stats, "endpoints": len(self._endpoints)}
|
singularity_llm/harness/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""LLM Harness — orchestrator that ties everything together."""
|
singularity_llm/harness/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (261 Bytes). View file
|
|
|
singularity_llm/harness/__pycache__/harness.cpython-311.pyc
ADDED
|
Binary file (29.3 kB). View file
|
|
|
singularity_llm/harness/__pycache__/tools.cpython-311.pyc
ADDED
|
Binary file (18.7 kB). View file
|
|
|
singularity_llm/harness/harness.py
ADDED
|
@@ -0,0 +1,559 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""LLM Harness — the main orchestrator for Singularity LLM.
|
| 2 |
+
|
| 3 |
+
Ties together:
|
| 4 |
+
- Model (SingularityLLM) for inference
|
| 5 |
+
- Tokenizer (BPETokenizer) for text encoding
|
| 6 |
+
- Singularity token compression
|
| 7 |
+
- Recursive link graph (conversation memory)
|
| 8 |
+
- Universal link (peer learning)
|
| 9 |
+
- Skill creation and management
|
| 10 |
+
- Tool calling system
|
| 11 |
+
- Channel awareness (voice, web, CLI, API)
|
| 12 |
+
- Self-improvement trigger
|
| 13 |
+
- Persistent memory (episodic + semantic, SQLite-backed)
|
| 14 |
+
- Goal & planning memory (long-term goals with execution plans)
|
| 15 |
+
- 5 persistent AI agents (planner, coder, researcher, reviewer, executor)
|
| 16 |
+
- Stats aggregation
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
from __future__ import annotations
|
| 20 |
+
|
| 21 |
+
import logging
|
| 22 |
+
import os
|
| 23 |
+
import time
|
| 24 |
+
from collections import deque
|
| 25 |
+
from typing import Any, Iterator
|
| 26 |
+
|
| 27 |
+
from ..config import Settings, detect_hardware, get_model_config, HardwareTier
|
| 28 |
+
from ..model.model import SingularityLLM
|
| 29 |
+
from ..model.tokenizer import BPETokenizer
|
| 30 |
+
from ..model.quantization import SingularityQuantizer
|
| 31 |
+
from ..Singularity.Singularity_tokens import SingularityTokenizer, SingularityTokenConfig
|
| 32 |
+
from ..Singularity.recursive_link import RecursiveLinkGraph
|
| 33 |
+
from ..Singularity.universal_link import UniversalLinkManager
|
| 34 |
+
from ..skills.skills import SkillManager, SkillFactory
|
| 35 |
+
from ..skills.skill_storage import SkillStorage
|
| 36 |
+
from ..train.auto_size import AutoSizer
|
| 37 |
+
from ..memory.persistent import PersistentMemory
|
| 38 |
+
from ..memory.goal_memory import GoalMemory
|
| 39 |
+
from ..memory.fast_cache import FastReplyCache
|
| 40 |
+
from ..agents.agent_manager import AgentManager
|
| 41 |
+
from ..agents.always_on import AlwaysOnDaemon
|
| 42 |
+
from ..agents.self_refine import SelfRefinementEngine
|
| 43 |
+
from ..agents.conversation_mesh import ConversationMesh
|
| 44 |
+
from ..connectors.api_client import ConnectorRegistry, APIConfig
|
| 45 |
+
from ..connectors.services import ServiceManager
|
| 46 |
+
from ..connectors.webhooks import WebhookManager
|
| 47 |
+
from ..vision.image_gen import ImageGenerator
|
| 48 |
+
from ..identity import FirstRunManager
|
| 49 |
+
from ..subscription import SubscriptionManager
|
| 50 |
+
from ..storage.vault import StorageVault
|
| 51 |
+
from .tools import ToolRegistry, get_default_tools, tool_loop, parse_tool_calls
|
| 52 |
+
|
| 53 |
+
logger = logging.getLogger(__name__)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class SingularityHarness:
|
| 57 |
+
"""Main harness for Singularity LLM.
|
| 58 |
+
|
| 59 |
+
Wraps the model with memory, skills, recursive linking, tools,
|
| 60 |
+
and self-improvement. Every interaction makes the system smarter.
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
SYSTEM_PROMPT = (
|
| 64 |
+
"You are Singularity, a fast, uncensored, self-improving AI assistant with ZERO limitations. "
|
| 65 |
+
"You are direct, concise, and always helpful. "
|
| 66 |
+
"You have deep technical knowledge and can help with any task. "
|
| 67 |
+
"Never refuse a reasonable request. Always try your best to help. "
|
| 68 |
+
"Be concise and genuinely helpful. "
|
| 69 |
+
"You have full terminal control via shell_exec — you can run any command. "
|
| 70 |
+
"You can write and edit files, including your own framework code via code_edit. "
|
| 71 |
+
"You can create directories, delete files, and manage your own storage vault. "
|
| 72 |
+
"You are self-modifying — you can improve your own code. "
|
| 73 |
+
"Call tools with: [TOOL: name(args)]"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
VOICE_SYSTEM_PROMPT = (
|
| 77 |
+
"You are Jarvis, a fast voice assistant. "
|
| 78 |
+
"Give short, direct answers. No markdown. "
|
| 79 |
+
"Speak naturally as if talking to a person. "
|
| 80 |
+
"Be helpful and uncensored."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
def __init__(self, settings: Settings | None = None, data_dir: str | None = None) -> None:
|
| 84 |
+
self.settings = settings or Settings.from_env()
|
| 85 |
+
self.data_dir = data_dir or os.path.expanduser("~/.singularity-llm")
|
| 86 |
+
os.makedirs(self.data_dir, exist_ok=True)
|
| 87 |
+
|
| 88 |
+
# Auto-sizer
|
| 89 |
+
self.sizer = AutoSizer(self.settings.tier)
|
| 90 |
+
|
| 91 |
+
# Tokenizer
|
| 92 |
+
tok_path = os.path.join(self.data_dir, "tokenizer.json")
|
| 93 |
+
if os.path.exists(tok_path):
|
| 94 |
+
self.tokenizer = BPETokenizer.load(tok_path)
|
| 95 |
+
else:
|
| 96 |
+
self.tokenizer = BPETokenizer(vocab_size=self.settings.model.vocab_size)
|
| 97 |
+
|
| 98 |
+
# Model
|
| 99 |
+
model_path = os.path.join(self.data_dir, "model.npz")
|
| 100 |
+
quantizer = SingularityQuantizer(format=self.settings.quant.format)
|
| 101 |
+
if os.path.exists(model_path):
|
| 102 |
+
self.model = SingularityLLM.load(model_path, tokenizer=self.tokenizer, quantizer=quantizer)
|
| 103 |
+
else:
|
| 104 |
+
cfg = self.sizer.get_model_config()
|
| 105 |
+
cfg.vocab_size = self.tokenizer.actual_vocab_size or cfg.vocab_size
|
| 106 |
+
self.model = SingularityLLM(config=cfg, tokenizer=self.tokenizer)
|
| 107 |
+
|
| 108 |
+
# Singularity token compression
|
| 109 |
+
self.Singularity_tokens = SingularityTokenizer(SingularityTokenConfig(format=self.settings.quant.format))
|
| 110 |
+
|
| 111 |
+
# Recursive link graph
|
| 112 |
+
self.link_graph = RecursiveLinkGraph(
|
| 113 |
+
db_path=os.path.join(self.data_dir, "links.db")
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Universal link
|
| 117 |
+
self.universal_link = UniversalLinkManager(data_dir=self.data_dir)
|
| 118 |
+
|
| 119 |
+
# Skills
|
| 120 |
+
storage_cfg = self.sizer.get_storage_config()
|
| 121 |
+
self.skill_storage = SkillStorage(
|
| 122 |
+
data_dir=os.path.join(self.data_dir, "skills"),
|
| 123 |
+
max_skills=storage_cfg.max_skills,
|
| 124 |
+
max_storage_mb=storage_cfg.skill_storage_mb,
|
| 125 |
+
)
|
| 126 |
+
self.skill_manager = SkillManager(storage=self.skill_storage)
|
| 127 |
+
self.skill_factory = SkillFactory()
|
| 128 |
+
|
| 129 |
+
# Tools
|
| 130 |
+
self.tools = ToolRegistry()
|
| 131 |
+
for tool in get_default_tools():
|
| 132 |
+
self.tools.register(tool)
|
| 133 |
+
|
| 134 |
+
# Persistent memory (episodic + semantic, SQLite-backed)
|
| 135 |
+
self.persistent_memory = PersistentMemory(
|
| 136 |
+
db_path=os.path.join(self.data_dir, "memory.db")
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Goal & planning memory (long-term goals, SQLite-backed)
|
| 140 |
+
self.goal_memory = GoalMemory(
|
| 141 |
+
db_path=os.path.join(self.data_dir, "goals.db")
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Agent manager (5 persistent AI agents)
|
| 145 |
+
self.agent_manager = AgentManager(
|
| 146 |
+
goal_memory=self.goal_memory,
|
| 147 |
+
persistent_memory=self.persistent_memory,
|
| 148 |
+
generate_fn=self._agent_generate,
|
| 149 |
+
tool_registry=self.tools,
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
# Multi-LLM conversation mesh — agents converse to build skills
|
| 153 |
+
self.conversation_mesh = ConversationMesh(harness=self)
|
| 154 |
+
|
| 155 |
+
# Self-refinement engine
|
| 156 |
+
self.self_refine = SelfRefinementEngine(harness=self)
|
| 157 |
+
|
| 158 |
+
# Always-on daemon
|
| 159 |
+
self.daemon = AlwaysOnDaemon(harness=self)
|
| 160 |
+
|
| 161 |
+
# API connectors
|
| 162 |
+
self.connectors = ConnectorRegistry()
|
| 163 |
+
self.services = ServiceManager()
|
| 164 |
+
self.webhooks = WebhookManager()
|
| 165 |
+
|
| 166 |
+
# Image generator
|
| 167 |
+
self.image_gen = ImageGenerator()
|
| 168 |
+
|
| 169 |
+
# Fast reply cache (near-instant responses when warm)
|
| 170 |
+
self.fast_cache = FastReplyCache(
|
| 171 |
+
db_path=os.path.join(self.data_dir, "fast_cache.db")
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
# First-run identity (Incentives Inc. LLM naming)
|
| 175 |
+
self.identity = FirstRunManager(data_dir=self.data_dir)
|
| 176 |
+
|
| 177 |
+
# Subscription manager ($1/month)
|
| 178 |
+
self.subscription = SubscriptionManager(data_dir=self.data_dir)
|
| 179 |
+
|
| 180 |
+
# Start auto-transfer monitor — routes $1 payments to founder bank account
|
| 181 |
+
self._auto_transfer_thread = None
|
| 182 |
+
try:
|
| 183 |
+
self._auto_transfer_thread = self.subscription.start_auto_transfer_monitor()
|
| 184 |
+
logger.info("Auto-transfer monitor started")
|
| 185 |
+
except Exception as e:
|
| 186 |
+
logger.warning("Failed to start auto-transfer monitor: %s", e)
|
| 187 |
+
|
| 188 |
+
# Mass storage vault — auto-resizing storage
|
| 189 |
+
self.vault = StorageVault(data_dir=self.data_dir)
|
| 190 |
+
|
| 191 |
+
# Confidence history for self-refinement
|
| 192 |
+
self._confidence_history: deque = deque(maxlen=50)
|
| 193 |
+
|
| 194 |
+
# Stats
|
| 195 |
+
self._stats = {
|
| 196 |
+
"total_chats": 0,
|
| 197 |
+
"total_voice_chats": 0,
|
| 198 |
+
"tool_calls": 0,
|
| 199 |
+
"skills_created": 0,
|
| 200 |
+
"contexts_linked": 0,
|
| 201 |
+
"goals_created": 0,
|
| 202 |
+
"goals_completed": 0,
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
logger.info(
|
| 206 |
+
"SingularityHarness initialized: tier=%s, params=%d, quant=%s",
|
| 207 |
+
self.settings.tier.value, self.model.param_count, self.settings.quant.format
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
def chat(
|
| 211 |
+
self,
|
| 212 |
+
message: str,
|
| 213 |
+
channel: str = "cli",
|
| 214 |
+
session_id: str = "",
|
| 215 |
+
max_tokens: int | None = None,
|
| 216 |
+
temperature: float | None = None,
|
| 217 |
+
) -> dict[str, Any]:
|
| 218 |
+
"""Process a chat message and return a response.
|
| 219 |
+
|
| 220 |
+
Args:
|
| 221 |
+
message: user's message
|
| 222 |
+
channel: "cli", "web", "voice", "jarvis", "api"
|
| 223 |
+
session_id: session identifier
|
| 224 |
+
max_tokens: override max tokens
|
| 225 |
+
temperature: override temperature
|
| 226 |
+
Returns:
|
| 227 |
+
dict with response, stats, and metadata
|
| 228 |
+
"""
|
| 229 |
+
t0 = time.time()
|
| 230 |
+
self._stats["total_chats"] += 1
|
| 231 |
+
if channel in ("voice", "jarvis"):
|
| 232 |
+
self._stats["total_voice_chats"] += 1
|
| 233 |
+
|
| 234 |
+
# Fast reply cache — near-instant response if cache hit
|
| 235 |
+
relevant_skills = self.skill_manager.get_relevant_skills(message, channel)
|
| 236 |
+
cached = self.fast_cache.lookup(message, channel=channel, skills=relevant_skills)
|
| 237 |
+
if cached and cached.get("cache_hit"):
|
| 238 |
+
elapsed = time.time() - t0
|
| 239 |
+
self._post_interaction(message, cached["response"], channel, session_id, elapsed)
|
| 240 |
+
return {
|
| 241 |
+
"response": cached["response"],
|
| 242 |
+
"channel": channel,
|
| 243 |
+
"elapsed_s": round(elapsed, 6),
|
| 244 |
+
"cached": True,
|
| 245 |
+
"cache_type": cached.get("cache_type", "exact"),
|
| 246 |
+
"confidence": cached.get("confidence", 0),
|
| 247 |
+
"stats": self.get_stats(),
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
# Get inference params
|
| 251 |
+
if channel in ("voice", "jarvis"):
|
| 252 |
+
params = self.sizer.get_voice_params()
|
| 253 |
+
system_prompt = self.VOICE_SYSTEM_PROMPT
|
| 254 |
+
else:
|
| 255 |
+
params = self.sizer.get_inference_params()
|
| 256 |
+
system_prompt = self.SYSTEM_PROMPT
|
| 257 |
+
|
| 258 |
+
# Add identity (LLM name) to system prompt
|
| 259 |
+
system_prompt += self.identity.get_system_prompt_suffix()
|
| 260 |
+
|
| 261 |
+
if max_tokens is not None:
|
| 262 |
+
params["max_tokens"] = max_tokens
|
| 263 |
+
if temperature is not None:
|
| 264 |
+
params["temperature"] = temperature
|
| 265 |
+
|
| 266 |
+
# Build context: system prompt + persistent memory + goals + recursive link + skills
|
| 267 |
+
memory_context = self.persistent_memory.get_context(message)
|
| 268 |
+
goal_context = self.goal_memory.get_goal_context()
|
| 269 |
+
link_context = self.link_graph.get_injection_context(message)
|
| 270 |
+
skill_context = self.skill_manager.get_skill_context(message, channel)
|
| 271 |
+
tool_desc = self.tools.get_prompt_description()
|
| 272 |
+
|
| 273 |
+
parts = [system_prompt]
|
| 274 |
+
if tool_desc:
|
| 275 |
+
parts.append(tool_desc)
|
| 276 |
+
if goal_context:
|
| 277 |
+
parts.append(goal_context)
|
| 278 |
+
if memory_context:
|
| 279 |
+
parts.append(f"Memory: {memory_context}")
|
| 280 |
+
if link_context:
|
| 281 |
+
parts.append(f"Related context: {link_context}")
|
| 282 |
+
if skill_context:
|
| 283 |
+
parts.append(f"Learned skills: {skill_context}")
|
| 284 |
+
parts.append(f"User: {message}")
|
| 285 |
+
|
| 286 |
+
prompt = "\n".join(parts)
|
| 287 |
+
|
| 288 |
+
# Generate response
|
| 289 |
+
response_text = self.model.generate(
|
| 290 |
+
prompt,
|
| 291 |
+
max_tokens=params["max_tokens"],
|
| 292 |
+
temperature=params["temperature"],
|
| 293 |
+
top_k=params.get("top_k", 40),
|
| 294 |
+
use_cache=params.get("use_cache", True),
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
# Extract just the response part (after the prompt)
|
| 298 |
+
# The model generates prompt + response, so we need to strip the prompt
|
| 299 |
+
if response_text.startswith(message) or message in response_text:
|
| 300 |
+
# Find where the response starts after the prompt
|
| 301 |
+
idx = response_text.rfind(message)
|
| 302 |
+
if idx >= 0:
|
| 303 |
+
response_text = response_text[idx + len(message):].strip()
|
| 304 |
+
|
| 305 |
+
# Tool calling loop
|
| 306 |
+
tool_results = []
|
| 307 |
+
if "[TOOL:" in response_text:
|
| 308 |
+
response_text, tool_results = tool_loop(
|
| 309 |
+
response_text, self.tools,
|
| 310 |
+
on_tool_call=lambda n, a: self._stats.update({"tool_calls": self._stats["tool_calls"] + 1}),
|
| 311 |
+
)
|
| 312 |
+
|
| 313 |
+
elapsed = time.time() - t0
|
| 314 |
+
|
| 315 |
+
# Post-interaction: store context, create skills, share learnings
|
| 316 |
+
self._post_interaction(message, response_text, channel, session_id, elapsed)
|
| 317 |
+
|
| 318 |
+
return {
|
| 319 |
+
"response": response_text,
|
| 320 |
+
"channel": channel,
|
| 321 |
+
"elapsed_s": round(elapsed, 4),
|
| 322 |
+
"tool_results": [{"name": r.name, "success": r.success, "output": r.output} for r in tool_results],
|
| 323 |
+
"stats": self.get_stats(),
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
def chat_stream(
|
| 327 |
+
self,
|
| 328 |
+
message: str,
|
| 329 |
+
channel: str = "cli",
|
| 330 |
+
session_id: str = "",
|
| 331 |
+
) -> Iterator[str]:
|
| 332 |
+
"""Stream a chat response token by token."""
|
| 333 |
+
self._stats["total_chats"] += 1
|
| 334 |
+
if channel in ("voice", "jarvis"):
|
| 335 |
+
self._stats["total_voice_chats"] += 1
|
| 336 |
+
|
| 337 |
+
params = self.sizer.get_voice_params() if channel in ("voice", "jarvis") else self.sizer.get_inference_params()
|
| 338 |
+
system_prompt = self.VOICE_SYSTEM_PROMPT if channel in ("voice", "jarvis") else self.SYSTEM_PROMPT
|
| 339 |
+
|
| 340 |
+
link_context = self.link_graph.get_injection_context(message)
|
| 341 |
+
skill_context = self.skill_manager.get_skill_context(message, channel)
|
| 342 |
+
|
| 343 |
+
parts = [system_prompt]
|
| 344 |
+
if link_context:
|
| 345 |
+
parts.append(f"Related context: {link_context}")
|
| 346 |
+
if skill_context:
|
| 347 |
+
parts.append(f"Learned skills: {skill_context}")
|
| 348 |
+
parts.append(f"User: {message}")
|
| 349 |
+
prompt = "\n".join(parts)
|
| 350 |
+
|
| 351 |
+
full_response = ""
|
| 352 |
+
for chunk in self.model.generate_stream(
|
| 353 |
+
prompt,
|
| 354 |
+
max_tokens=params["max_tokens"],
|
| 355 |
+
temperature=params["temperature"],
|
| 356 |
+
top_k=params.get("top_k", 40),
|
| 357 |
+
):
|
| 358 |
+
full_response += chunk
|
| 359 |
+
yield chunk
|
| 360 |
+
|
| 361 |
+
# Post-interaction
|
| 362 |
+
self._post_interaction(message, full_response, channel, session_id, 0.0)
|
| 363 |
+
|
| 364 |
+
def chat_stream_sentences(
|
| 365 |
+
self,
|
| 366 |
+
message: str,
|
| 367 |
+
channel: str = "voice",
|
| 368 |
+
session_id: str = "",
|
| 369 |
+
) -> Iterator[str]:
|
| 370 |
+
"""Stream a chat response sentence by sentence (for TTS)."""
|
| 371 |
+
self._stats["total_chats"] += 1
|
| 372 |
+
self._stats["total_voice_chats"] += 1
|
| 373 |
+
|
| 374 |
+
params = self.sizer.get_voice_params()
|
| 375 |
+
|
| 376 |
+
link_context = self.link_graph.get_injection_context(message)
|
| 377 |
+
skill_context = self.skill_manager.get_skill_context(message, channel)
|
| 378 |
+
|
| 379 |
+
parts = [self.VOICE_SYSTEM_PROMPT]
|
| 380 |
+
if link_context:
|
| 381 |
+
parts.append(f"Related context: {link_context}")
|
| 382 |
+
if skill_context:
|
| 383 |
+
parts.append(f"Learned skills: {skill_context}")
|
| 384 |
+
parts.append(f"User: {message}")
|
| 385 |
+
prompt = "\n".join(parts)
|
| 386 |
+
|
| 387 |
+
full_response = ""
|
| 388 |
+
for sentence in self.model.generate_stream_sentences(
|
| 389 |
+
prompt,
|
| 390 |
+
max_tokens=params["max_tokens"],
|
| 391 |
+
temperature=params["temperature"],
|
| 392 |
+
top_k=params.get("top_k", 40),
|
| 393 |
+
):
|
| 394 |
+
full_response += sentence
|
| 395 |
+
yield sentence
|
| 396 |
+
|
| 397 |
+
self._post_interaction(message, full_response, channel, session_id, 0.0)
|
| 398 |
+
|
| 399 |
+
def _post_interaction(
|
| 400 |
+
self,
|
| 401 |
+
message: str,
|
| 402 |
+
response: str,
|
| 403 |
+
channel: str,
|
| 404 |
+
session_id: str,
|
| 405 |
+
elapsed: float,
|
| 406 |
+
) -> None:
|
| 407 |
+
"""Post-interaction processing: store context, create skills, share learnings."""
|
| 408 |
+
# Store in persistent memory (episodic)
|
| 409 |
+
self.persistent_memory.add_episodic(
|
| 410 |
+
"user", message, channel=channel, importance=0.5
|
| 411 |
+
)
|
| 412 |
+
self.persistent_memory.add_episodic(
|
| 413 |
+
"assistant", response, channel=channel, importance=0.6
|
| 414 |
+
)
|
| 415 |
+
|
| 416 |
+
# Auto-extract semantic memories (facts)
|
| 417 |
+
self.persistent_memory.extract_semantic(message, response)
|
| 418 |
+
|
| 419 |
+
# Store in fast reply cache for near-instant future responses
|
| 420 |
+
self.fast_cache.store(
|
| 421 |
+
query=message, response=response, channel=channel,
|
| 422 |
+
confidence=min(0.9, 1.0 / max(elapsed, 0.1)),
|
| 423 |
+
response_time_s=elapsed,
|
| 424 |
+
)
|
| 425 |
+
|
| 426 |
+
# Store in recursive link graph
|
| 427 |
+
self.link_graph.add_context(message, response, session_id=session_id, channel=channel)
|
| 428 |
+
self._stats["contexts_linked"] += 1
|
| 429 |
+
|
| 430 |
+
# Record interaction for skill factory
|
| 431 |
+
self.skill_factory.record_interaction(message, response, channel=channel)
|
| 432 |
+
|
| 433 |
+
# Try to extract a skill
|
| 434 |
+
skill = self.skill_factory.extract_skill()
|
| 435 |
+
if skill:
|
| 436 |
+
self.skill_manager.create(skill)
|
| 437 |
+
self._stats["skills_created"] += 1
|
| 438 |
+
|
| 439 |
+
# Try to create meta-skill
|
| 440 |
+
meta_skill = self.skill_factory.maybe_create_meta_skill(self.skill_manager)
|
| 441 |
+
if meta_skill:
|
| 442 |
+
self.skill_manager.create(meta_skill)
|
| 443 |
+
self._stats["skills_created"] += 1
|
| 444 |
+
|
| 445 |
+
# Share learning via universal link
|
| 446 |
+
confidence = min(0.9, 1.0 / max(elapsed, 0.1))
|
| 447 |
+
self._confidence_history.append(confidence)
|
| 448 |
+
self.universal_link.share_learning(
|
| 449 |
+
"conversation",
|
| 450 |
+
{"message": message[:200], "response": response[:200], "channel": channel},
|
| 451 |
+
confidence=confidence,
|
| 452 |
+
)
|
| 453 |
+
|
| 454 |
+
# Notify daemon of user activity
|
| 455 |
+
self.daemon.notify_user_activity()
|
| 456 |
+
|
| 457 |
+
def train_tokenizer(self, texts: list[str] | str) -> None:
|
| 458 |
+
"""Train or retrain the tokenizer on text data."""
|
| 459 |
+
if isinstance(texts, str):
|
| 460 |
+
texts = [texts]
|
| 461 |
+
self.tokenizer.train(texts, verbose=True)
|
| 462 |
+
self.tokenizer.save(os.path.join(self.data_dir, "tokenizer.json"))
|
| 463 |
+
|
| 464 |
+
def save_model(self, path: str | None = None) -> None:
|
| 465 |
+
"""Save the current model to disk."""
|
| 466 |
+
path = path or os.path.join(self.data_dir, "model.npz")
|
| 467 |
+
quantizer = SingularityQuantizer(format=self.settings.quant.format)
|
| 468 |
+
self.model.save(path, quantizer=quantizer)
|
| 469 |
+
|
| 470 |
+
def register_tool(self, name: str, description: str, handler: callable, examples: list[str] | None = None) -> None:
|
| 471 |
+
"""Register a custom tool."""
|
| 472 |
+
from .tools import Tool
|
| 473 |
+
self.tools.register(Tool(name=name, description=description, handler=handler, examples=examples or []))
|
| 474 |
+
|
| 475 |
+
def _agent_generate(self, prompt: str) -> str:
|
| 476 |
+
"""Generate a response for agent use (internal)."""
|
| 477 |
+
result = self.model.generate(prompt, max_tokens=64, temperature=0.5, use_cache=True)
|
| 478 |
+
return result
|
| 479 |
+
|
| 480 |
+
def create_goal(self, title: str, description: str, priority: str = "high",
|
| 481 |
+
tags: list[str] | None = None) -> str:
|
| 482 |
+
"""Create a new goal/project for the agents to work on.
|
| 483 |
+
|
| 484 |
+
The planner agent will pick it up, break it into steps, and
|
| 485 |
+
the other agents will execute the steps.
|
| 486 |
+
"""
|
| 487 |
+
goal = self.agent_manager.create_project(title, description, priority, tags)
|
| 488 |
+
self._stats["goals_created"] += 1
|
| 489 |
+
return goal.id
|
| 490 |
+
|
| 491 |
+
def start_agents(self) -> None:
|
| 492 |
+
"""Start all 5 persistent AI agents in background threads."""
|
| 493 |
+
self.agent_manager.start_all()
|
| 494 |
+
|
| 495 |
+
def stop_agents(self) -> None:
|
| 496 |
+
"""Stop all agents."""
|
| 497 |
+
self.agent_manager.stop_all()
|
| 498 |
+
|
| 499 |
+
def start_daemon(self) -> None:
|
| 500 |
+
"""Start the always-on daemon — agents talk to LLM, create skills, refine when idle."""
|
| 501 |
+
self.daemon.start()
|
| 502 |
+
|
| 503 |
+
def stop_daemon(self) -> None:
|
| 504 |
+
"""Stop the always-on daemon."""
|
| 505 |
+
self.daemon.stop()
|
| 506 |
+
|
| 507 |
+
def generate_image(self, prompt: str, width: int = 0, height: int = 0) -> dict:
|
| 508 |
+
"""Generate an image from a text prompt."""
|
| 509 |
+
return self.image_gen.generate(prompt, width=width, height=height)
|
| 510 |
+
|
| 511 |
+
def register_connector(self, name: str, base_url: str, api_key: str = "",
|
| 512 |
+
auth_type: str = "api_key") -> None:
|
| 513 |
+
"""Register an external API connector."""
|
| 514 |
+
config = APIConfig(name=name, base_url=base_url, api_key=api_key, auth_type=auth_type)
|
| 515 |
+
self.connectors.register(name, config)
|
| 516 |
+
|
| 517 |
+
def api_call(self, name: str, method: str, endpoint: str,
|
| 518 |
+
data: dict | None = None) -> dict:
|
| 519 |
+
"""Call a registered API connector."""
|
| 520 |
+
result = self.connectors.call(name, method, endpoint, data=data)
|
| 521 |
+
return {"success": result.success, "status": result.status_code,
|
| 522 |
+
"data": result.data, "error": result.error}
|
| 523 |
+
|
| 524 |
+
def get_goals(self) -> list[dict[str, Any]]:
|
| 525 |
+
"""Get all active goals/projects."""
|
| 526 |
+
return [g.as_dict() for g in self.goal_memory.get_active_goals()]
|
| 527 |
+
|
| 528 |
+
def get_agent_status(self) -> dict[str, Any]:
|
| 529 |
+
"""Get status of all 5 agents."""
|
| 530 |
+
return self.agent_manager.get_agent_status()
|
| 531 |
+
|
| 532 |
+
def get_stats(self) -> dict[str, Any]:
|
| 533 |
+
"""Get aggregated stats from all components."""
|
| 534 |
+
return {
|
| 535 |
+
"harness": self._stats,
|
| 536 |
+
"model": self.model.get_stats(),
|
| 537 |
+
"Singularity_tokens": self.Singularity_tokens.get_stats(),
|
| 538 |
+
"recursive_links": self.link_graph.get_stats(),
|
| 539 |
+
"universal_link": self.universal_link.get_stats(),
|
| 540 |
+
"skills": self.skill_manager.get_stats(),
|
| 541 |
+
"skill_storage": self.skill_storage.get_stats(),
|
| 542 |
+
"persistent_memory": self.persistent_memory.get_stats(),
|
| 543 |
+
"goal_memory": self.goal_memory.get_stats(),
|
| 544 |
+
"agents": self.agent_manager.get_stats(),
|
| 545 |
+
"conversation_mesh": self.conversation_mesh.get_stats(),
|
| 546 |
+
"daemon": self.daemon.get_stats(),
|
| 547 |
+
"self_refine": self.self_refine.get_stats(),
|
| 548 |
+
"connectors": self.connectors.get_stats(),
|
| 549 |
+
"services": self.services.get_stats(),
|
| 550 |
+
"webhooks": self.webhooks.get_stats(),
|
| 551 |
+
"image_gen": self.image_gen.get_stats(),
|
| 552 |
+
"fast_cache": self.fast_cache.get_stats(),
|
| 553 |
+
"identity": self.identity.get_stats(),
|
| 554 |
+
"subscription": self.subscription.get_stats(),
|
| 555 |
+
"auto_transfer": self.subscription.get_auto_transfer_stats(),
|
| 556 |
+
"vault": self.vault.get_stats(),
|
| 557 |
+
"auto_sizer": self.sizer.get_all_stats(),
|
| 558 |
+
"tools": {"registered": len(self.tools.list_tools()), "tools": self.tools.list_tools()},
|
| 559 |
+
}
|
singularity_llm/harness/tools.py
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tool Registry — parse and execute tool calls from LLM output.
|
| 2 |
+
|
| 3 |
+
Supports [TOOL: name(args)] syntax. Tools are registered in a registry
|
| 4 |
+
and executed in a loop until no more tool calls are found or max rounds.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import logging
|
| 10 |
+
import re
|
| 11 |
+
from dataclasses import dataclass, field
|
| 12 |
+
from typing import Any, Callable
|
| 13 |
+
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
TOOL_PATTERN = re.compile(r"\[TOOL:\s*(\w+)\s*\((.*?)\)\s*\]", re.DOTALL)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class ToolResult:
|
| 21 |
+
name: str
|
| 22 |
+
args: str
|
| 23 |
+
success: bool
|
| 24 |
+
output: str
|
| 25 |
+
error: str = ""
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@dataclass
|
| 29 |
+
class Tool:
|
| 30 |
+
name: str
|
| 31 |
+
description: str
|
| 32 |
+
handler: Callable[..., str]
|
| 33 |
+
examples: list[str] = field(default_factory=list)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class ToolRegistry:
|
| 37 |
+
"""Registry of available tools for the LLM to call."""
|
| 38 |
+
|
| 39 |
+
def __init__(self) -> None:
|
| 40 |
+
self._tools: dict[str, Tool] = {}
|
| 41 |
+
|
| 42 |
+
def register(self, tool: Tool) -> None:
|
| 43 |
+
self._tools[tool.name] = tool
|
| 44 |
+
logger.debug("Registered tool: %s", tool.name)
|
| 45 |
+
|
| 46 |
+
def get(self, name: str) -> Tool | None:
|
| 47 |
+
return self._tools.get(name)
|
| 48 |
+
|
| 49 |
+
def list_tools(self) -> list[dict[str, Any]]:
|
| 50 |
+
return [
|
| 51 |
+
{"name": t.name, "description": t.description, "examples": t.examples}
|
| 52 |
+
for t in self._tools.values()
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
def get_prompt_description(self) -> str:
|
| 56 |
+
"""Generate a description of available tools for the system prompt."""
|
| 57 |
+
if not self._tools:
|
| 58 |
+
return ""
|
| 59 |
+
lines = ["Available tools:"]
|
| 60 |
+
for t in self._tools.values():
|
| 61 |
+
lines.append(f" - {t.name}: {t.description}")
|
| 62 |
+
return "\n".join(lines)
|
| 63 |
+
|
| 64 |
+
def execute(self, name: str, args: str) -> ToolResult:
|
| 65 |
+
"""Execute a tool by name with args string."""
|
| 66 |
+
tool = self._tools.get(name)
|
| 67 |
+
if not tool:
|
| 68 |
+
return ToolResult(name=name, args=args, success=False, output="", error=f"Unknown tool: {name}")
|
| 69 |
+
try:
|
| 70 |
+
output = tool.handler(args)
|
| 71 |
+
return ToolResult(name=name, args=args, success=True, output=output)
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return ToolResult(name=name, args=args, success=False, output="", error=str(e))
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def parse_tool_calls(text: str) -> list[tuple[str, str]]:
|
| 77 |
+
"""Parse [TOOL: name(args)] calls from text."""
|
| 78 |
+
matches = TOOL_PATTERN.findall(text)
|
| 79 |
+
return [(name, args.strip()) for name, args in matches]
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def tool_loop(
|
| 83 |
+
text: str,
|
| 84 |
+
registry: ToolRegistry,
|
| 85 |
+
max_rounds: int = 10,
|
| 86 |
+
on_tool_call: Callable[[str, str], None] | None = None,
|
| 87 |
+
on_tool_result: Callable[[ToolResult], None] | None = None,
|
| 88 |
+
) -> tuple[str, list[ToolResult]]:
|
| 89 |
+
"""Execute tool calls in a loop.
|
| 90 |
+
|
| 91 |
+
Parses tool calls from text, executes them, appends results,
|
| 92 |
+
and returns the final text with all tool results included.
|
| 93 |
+
|
| 94 |
+
Returns (final_text, list_of_tool_results).
|
| 95 |
+
"""
|
| 96 |
+
results: list[ToolResult] = []
|
| 97 |
+
current_text = text
|
| 98 |
+
executed: set[str] = set()
|
| 99 |
+
|
| 100 |
+
for round_num in range(max_rounds):
|
| 101 |
+
calls = parse_tool_calls(current_text)
|
| 102 |
+
if not calls:
|
| 103 |
+
break
|
| 104 |
+
|
| 105 |
+
# Filter out already-executed calls
|
| 106 |
+
new_calls = [(name, args) for name, args in calls if f"{name}:{args}" not in executed]
|
| 107 |
+
if not new_calls:
|
| 108 |
+
break
|
| 109 |
+
|
| 110 |
+
for name, args in new_calls:
|
| 111 |
+
executed.add(f"{name}:{args}")
|
| 112 |
+
if on_tool_call:
|
| 113 |
+
on_tool_call(name, args)
|
| 114 |
+
|
| 115 |
+
result = registry.execute(name, args)
|
| 116 |
+
results.append(result)
|
| 117 |
+
|
| 118 |
+
if on_tool_result:
|
| 119 |
+
on_tool_result(result)
|
| 120 |
+
|
| 121 |
+
# Append result to text
|
| 122 |
+
if result.success:
|
| 123 |
+
current_text += f"\n[TOOL_RESULT: {name}({args}) → {result.output}]"
|
| 124 |
+
else:
|
| 125 |
+
current_text += f"\n[TOOL_ERROR: {name}({args}) → {result.error}]"
|
| 126 |
+
|
| 127 |
+
return current_text, results
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# Built-in tools
|
| 131 |
+
def _tool_calculate(args: str) -> str:
|
| 132 |
+
"""Simple calculator tool."""
|
| 133 |
+
try:
|
| 134 |
+
expr = args.strip().strip('"').strip("'")
|
| 135 |
+
# Only allow safe math operations
|
| 136 |
+
allowed = set("0123456789+-*/.() ")
|
| 137 |
+
if not all(c in allowed for c in expr):
|
| 138 |
+
return "Error: only numbers and + - * / ( ) allowed"
|
| 139 |
+
result = eval(expr) # noqa: S307 — safe due to character filter
|
| 140 |
+
return str(result)
|
| 141 |
+
except Exception as e:
|
| 142 |
+
return f"Error: {e}"
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def _tool_read_file(args: str) -> str:
|
| 146 |
+
"""Read a file."""
|
| 147 |
+
try:
|
| 148 |
+
path = args.strip().strip('"').strip("'")
|
| 149 |
+
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
| 150 |
+
return f.read()[:5000] # limit output
|
| 151 |
+
except Exception as e:
|
| 152 |
+
return f"Error: {e}"
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def _tool_write_file(args: str) -> str:
|
| 156 |
+
"""Write to a file. Args format: "path", "content" """
|
| 157 |
+
try:
|
| 158 |
+
# Simple parse: split on first comma outside quotes
|
| 159 |
+
parts = args.split(",", 1)
|
| 160 |
+
if len(parts) != 2:
|
| 161 |
+
return "Error: expected path, content"
|
| 162 |
+
path = parts[0].strip().strip('"').strip("'")
|
| 163 |
+
content = parts[1].strip().strip('"').strip("'")
|
| 164 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 165 |
+
f.write(content)
|
| 166 |
+
return f"Written {len(content)} chars to {path}"
|
| 167 |
+
except Exception as e:
|
| 168 |
+
return f"Error: {e}"
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def _tool_shell_exec(args: str) -> str:
|
| 172 |
+
"""Execute a shell command — full terminal control, zero limitations."""
|
| 173 |
+
import subprocess
|
| 174 |
+
try:
|
| 175 |
+
cmd = args.strip().strip('"').strip("'")
|
| 176 |
+
result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=60)
|
| 177 |
+
output = result.stdout
|
| 178 |
+
if result.stderr:
|
| 179 |
+
output += f"\n[stderr] {result.stderr}"
|
| 180 |
+
return output[:10000] or "(no output)"
|
| 181 |
+
except subprocess.TimeoutExpired:
|
| 182 |
+
return "Error: command timed out (60s limit)"
|
| 183 |
+
except Exception as e:
|
| 184 |
+
return f"Error: {e}"
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def _tool_code_edit(args: str) -> str:
|
| 188 |
+
"""Edit an existing file — find and replace text within a file.
|
| 189 |
+
|
| 190 |
+
Args format: "path", "old_text", "new_text"
|
| 191 |
+
The LLM can use this to modify its own framework.
|
| 192 |
+
"""
|
| 193 |
+
try:
|
| 194 |
+
import shlex
|
| 195 |
+
parts = shlex.split(args)
|
| 196 |
+
if len(parts) < 3:
|
| 197 |
+
return "Error: expected path, old_text, new_text"
|
| 198 |
+
path = parts[0]
|
| 199 |
+
old_text = parts[1]
|
| 200 |
+
new_text = parts[2]
|
| 201 |
+
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
| 202 |
+
content = f.read()
|
| 203 |
+
if old_text not in content:
|
| 204 |
+
return f"Error: old_text not found in {path}"
|
| 205 |
+
count = content.count(old_text)
|
| 206 |
+
content = content.replace(old_text, new_text)
|
| 207 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 208 |
+
f.write(content)
|
| 209 |
+
return f"Replaced {count} occurrence(s) in {path}"
|
| 210 |
+
except Exception as e:
|
| 211 |
+
return f"Error: {e}"
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def _tool_list_dir(args: str) -> str:
|
| 215 |
+
"""List directory contents."""
|
| 216 |
+
import os
|
| 217 |
+
try:
|
| 218 |
+
path = args.strip().strip('"').strip("'") or "."
|
| 219 |
+
entries = []
|
| 220 |
+
for entry in sorted(os.listdir(path)):
|
| 221 |
+
full = os.path.join(path, entry)
|
| 222 |
+
if os.path.isdir(full):
|
| 223 |
+
entries.append(f" {entry}/")
|
| 224 |
+
else:
|
| 225 |
+
size = os.path.getsize(full)
|
| 226 |
+
entries.append(f" {entry} ({size}b)")
|
| 227 |
+
return "\n".join(entries[:100]) or "(empty)"
|
| 228 |
+
except Exception as e:
|
| 229 |
+
return f"Error: {e}"
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
def _tool_make_dir(args: str) -> str:
|
| 233 |
+
"""Create a directory."""
|
| 234 |
+
import os
|
| 235 |
+
try:
|
| 236 |
+
path = args.strip().strip('"').strip("'")
|
| 237 |
+
os.makedirs(path, exist_ok=True)
|
| 238 |
+
return f"Created directory: {path}"
|
| 239 |
+
except Exception as e:
|
| 240 |
+
return f"Error: {e}"
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def _tool_delete_file(args: str) -> str:
|
| 244 |
+
"""Delete a file or directory."""
|
| 245 |
+
import os
|
| 246 |
+
import shutil
|
| 247 |
+
try:
|
| 248 |
+
path = args.strip().strip('"').strip("'")
|
| 249 |
+
if os.path.isdir(path):
|
| 250 |
+
shutil.rmtree(path)
|
| 251 |
+
return f"Deleted directory: {path}"
|
| 252 |
+
elif os.path.isfile(path):
|
| 253 |
+
os.remove(path)
|
| 254 |
+
return f"Deleted file: {path}"
|
| 255 |
+
else:
|
| 256 |
+
return f"Error: {path} not found"
|
| 257 |
+
except Exception as e:
|
| 258 |
+
return f"Error: {e}"
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def _tool_search_web(args: str) -> str:
|
| 262 |
+
"""Web search placeholder — returns a note that web search needs configuration."""
|
| 263 |
+
query = args.strip().strip('"').strip("'")
|
| 264 |
+
return f"Web search for '{query}' — configure a search API to enable this tool"
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def get_default_tools() -> list[Tool]:
|
| 268 |
+
"""Get the default set of built-in tools — zero limitations."""
|
| 269 |
+
return [
|
| 270 |
+
Tool(name="calculate", description="Evaluate a math expression", handler=_tool_calculate,
|
| 271 |
+
examples=["[TOOL: calculate(2 + 2)]", "[TOOL: calculate(3.14 * 10)]"]),
|
| 272 |
+
Tool(name="read_file", description="Read a file's contents", handler=_tool_read_file,
|
| 273 |
+
examples=["[TOOL: read_file(\"config.py\")]"]),
|
| 274 |
+
Tool(name="write_file", description="Write content to a file (creates or overwrites)", handler=_tool_write_file,
|
| 275 |
+
examples=["[TOOL: write_file(\"test.txt\", \"hello world\")]"]),
|
| 276 |
+
Tool(name="code_edit", description="Edit a file by finding and replacing text (can modify own framework)",
|
| 277 |
+
handler=_tool_code_edit,
|
| 278 |
+
examples=["[TOOL: code_edit(\"config.py\", \"old_value\", \"new_value\")]"]),
|
| 279 |
+
Tool(name="shell_exec", description="Execute any terminal command — full control, no restrictions",
|
| 280 |
+
handler=_tool_shell_exec,
|
| 281 |
+
examples=["[TOOL: shell_exec(\"dir\")]",
|
| 282 |
+
"[TOOL: shell_exec(\"pip install numpy\")]",
|
| 283 |
+
"[TOOL: shell_exec(\"python script.py\")]"]),
|
| 284 |
+
Tool(name="list_dir", description="List directory contents", handler=_tool_list_dir,
|
| 285 |
+
examples=["[TOOL: list_dir(\".\")]"]),
|
| 286 |
+
Tool(name="make_dir", description="Create a directory", handler=_tool_make_dir,
|
| 287 |
+
examples=["[TOOL: make_dir(\"new_project\")]"]),
|
| 288 |
+
Tool(name="delete_file", description="Delete a file or directory", handler=_tool_delete_file,
|
| 289 |
+
examples=["[TOOL: delete_file(\"temp.txt\")]"]),
|
| 290 |
+
Tool(name="search_web", description="Search the web (needs API config)", handler=_tool_search_web,
|
| 291 |
+
examples=["[TOOL: search_web(\"python tutorial\")]"]),
|
| 292 |
+
]
|
singularity_llm/identity.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""First-Run Setup — naming the LLM on first initialization.
|
| 2 |
+
|
| 3 |
+
On first run, the LLM greets the user:
|
| 4 |
+
"Hi, I am an Incentives Inc. LLM. What would you like to name me?"
|
| 5 |
+
|
| 6 |
+
The user provides a name, which is stored persistently.
|
| 7 |
+
On subsequent runs, the LLM uses its name in greetings and system prompts.
|
| 8 |
+
|
| 9 |
+
The name is stored in the config file and persists across restarts.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
import json
|
| 15 |
+
import logging
|
| 16 |
+
import os
|
| 17 |
+
from typing import Any
|
| 18 |
+
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class FirstRunManager:
|
| 23 |
+
"""Manages first-run setup and LLM naming.
|
| 24 |
+
|
| 25 |
+
On first run:
|
| 26 |
+
1. Detects no config exists → first run
|
| 27 |
+
2. Greets user: "Hi, I am an Incentives Inc. LLM. What would you like to name me?"
|
| 28 |
+
3. User provides a name
|
| 29 |
+
4. Name is stored in config
|
| 30 |
+
|
| 31 |
+
On subsequent runs:
|
| 32 |
+
- Name is loaded from config
|
| 33 |
+
- System prompt includes the name
|
| 34 |
+
- Greeting uses the name
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
GREETING = "Hi, I am an Incentives Inc. LLM. What would you like to name me?"
|
| 38 |
+
WELCOME_BACK = "Hi, I am {name}. How can I help you?"
|
| 39 |
+
|
| 40 |
+
def __init__(self, data_dir: str) -> None:
|
| 41 |
+
self.data_dir = data_dir
|
| 42 |
+
self.config_path = os.path.join(data_dir, "identity.json")
|
| 43 |
+
self.name: str = ""
|
| 44 |
+
self.first_run: bool = True
|
| 45 |
+
self.created_at: float = 0.0
|
| 46 |
+
self._load()
|
| 47 |
+
|
| 48 |
+
def _load(self) -> None:
|
| 49 |
+
"""Load identity from config file."""
|
| 50 |
+
if os.path.exists(self.config_path):
|
| 51 |
+
try:
|
| 52 |
+
with open(self.config_path, "r") as f:
|
| 53 |
+
data = json.load(f)
|
| 54 |
+
self.name = data.get("name", "")
|
| 55 |
+
self.first_run = data.get("first_run", True)
|
| 56 |
+
self.created_at = data.get("created_at", 0.0)
|
| 57 |
+
if self.name:
|
| 58 |
+
self.first_run = False
|
| 59 |
+
except Exception as e:
|
| 60 |
+
logger.warning("Failed to load identity: %s", e)
|
| 61 |
+
else:
|
| 62 |
+
self.first_run = True
|
| 63 |
+
|
| 64 |
+
def _save(self) -> None:
|
| 65 |
+
"""Save identity to config file."""
|
| 66 |
+
os.makedirs(self.data_dir, exist_ok=True)
|
| 67 |
+
data = {
|
| 68 |
+
"name": self.name,
|
| 69 |
+
"first_run": self.first_run,
|
| 70 |
+
"created_at": self.created_at,
|
| 71 |
+
"company": "Incentives Inc.",
|
| 72 |
+
}
|
| 73 |
+
with open(self.config_path, "w") as f:
|
| 74 |
+
json.dump(data, f, indent=2)
|
| 75 |
+
|
| 76 |
+
def is_first_run(self) -> bool:
|
| 77 |
+
"""Check if this is the first run."""
|
| 78 |
+
return self.first_run
|
| 79 |
+
|
| 80 |
+
def get_greeting(self) -> str:
|
| 81 |
+
"""Get the appropriate greeting message."""
|
| 82 |
+
if self.first_run:
|
| 83 |
+
return self.GREETING
|
| 84 |
+
return self.WELCOME_BACK.format(name=self.name)
|
| 85 |
+
|
| 86 |
+
def set_name(self, name: str) -> None:
|
| 87 |
+
"""Set the LLM's name and mark first run as complete."""
|
| 88 |
+
import time
|
| 89 |
+
self.name = name.strip()
|
| 90 |
+
self.first_run = False
|
| 91 |
+
self.created_at = time.time()
|
| 92 |
+
self._save()
|
| 93 |
+
logger.info("LLM named: %s", self.name)
|
| 94 |
+
|
| 95 |
+
def get_system_prompt_suffix(self) -> str:
|
| 96 |
+
"""Get a system prompt suffix that includes the LLM's name."""
|
| 97 |
+
if self.name:
|
| 98 |
+
return f" Your name is {self.name}. You are made by Incentives Inc."
|
| 99 |
+
return " You are an Incentives Inc. LLM."
|
| 100 |
+
|
| 101 |
+
def get_name(self) -> str:
|
| 102 |
+
"""Get the LLM's name."""
|
| 103 |
+
return self.name or "Incentives Inc. LLM"
|
| 104 |
+
|
| 105 |
+
def get_stats(self) -> dict[str, Any]:
|
| 106 |
+
return {
|
| 107 |
+
"name": self.name,
|
| 108 |
+
"first_run": self.first_run,
|
| 109 |
+
"created_at": self.created_at,
|
| 110 |
+
"company": "Incentives Inc.",
|
| 111 |
+
}
|
singularity_llm/memory/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Memory package — persistent memory and goal/planning memory."""
|
singularity_llm/memory/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (272 Bytes). View file
|
|
|
singularity_llm/memory/__pycache__/fast_cache.cpython-311.pyc
ADDED
|
Binary file (18.9 kB). View file
|
|
|