Compare commits
18 Commits
epic-999-p
...
feat/sover
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3b09b7b49d | ||
| 5b948356b7 | |||
| 1bff6d17d5 | |||
| b5527fee26 | |||
| 482b6c5aea | |||
| 5ac5c7f44c | |||
| 0f508c9600 | |||
| 6aeb5a71df | |||
| f1b409cba4 | |||
| d1defbe06a | |||
| e2ee3b7819 | |||
| 689b8e705a | |||
| 79f411de4d | |||
| 8411f124cd | |||
| 7fe402fb70 | |||
| f8bc71823d | |||
| fdce07ff40 | |||
| bf82581189 |
@@ -63,6 +63,11 @@ _API_KEY_PROVIDER_AUX_MODELS: Dict[str, str] = {
|
||||
"opencode-zen": "gemini-3-flash",
|
||||
"opencode-go": "glm-5",
|
||||
"kilocode": "google/gemini-3-flash-preview",
|
||||
# Uniwizard backends
|
||||
"gemini": "gemini-2.5-flash",
|
||||
"groq": "llama-3.3-70b-versatile",
|
||||
"grok": "grok-3-mini-fast",
|
||||
"openrouter": "openai/gpt-4.1-mini",
|
||||
}
|
||||
|
||||
# OpenRouter app attribution headers
|
||||
|
||||
90
agent/gemini_adapter.py
Normal file
90
agent/gemini_adapter.py
Normal file
@@ -0,0 +1,90 @@
|
||||
"""Native Gemini 3 Series adapter for Hermes Agent.
|
||||
|
||||
Leverages the google-genai SDK to provide sovereign access to Gemini's
|
||||
unique capabilities: Thinking (Reasoning) tokens, Search Grounding,
|
||||
and Maps Grounding.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
try:
|
||||
from google import genai
|
||||
from google.genai import types
|
||||
except ImportError:
|
||||
genai = None # type: ignore
|
||||
types = None # type: ignore
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class GeminiAdapter:
|
||||
def __init__(self, api_key: Optional[str] = None):
|
||||
self.api_key = api_key or os.environ.get("GEMINI_API_KEY")
|
||||
if not self.api_key:
|
||||
logger.warning("GEMINI_API_KEY not found in environment.")
|
||||
|
||||
if genai:
|
||||
self.client = genai.Client(api_key=self.api_key)
|
||||
else:
|
||||
self.client = None
|
||||
|
||||
def generate(
|
||||
self,
|
||||
model: str,
|
||||
prompt: str,
|
||||
system_instruction: Optional[str] = None,
|
||||
thinking: bool = False,
|
||||
thinking_budget: int = 16000,
|
||||
grounding: bool = False,
|
||||
**kwargs
|
||||
) -> Dict[str, Any]:
|
||||
if not self.client:
|
||||
raise ImportError("google-genai SDK not installed. Run 'pip install google-genai'.")
|
||||
|
||||
config = {}
|
||||
if system_instruction:
|
||||
config["system_instruction"] = system_instruction
|
||||
|
||||
if thinking:
|
||||
# Gemini 3 series thinking config
|
||||
config["thinking_config"] = {"include_thoughts": True}
|
||||
# max_output_tokens includes thinking tokens
|
||||
kwargs["max_output_tokens"] = kwargs.get("max_output_tokens", 32000) + thinking_budget
|
||||
|
||||
tools = []
|
||||
if grounding:
|
||||
tools.append({"google_search": {}})
|
||||
|
||||
if tools:
|
||||
config["tools"] = tools
|
||||
|
||||
response = self.client.models.generate_content(
|
||||
model=model,
|
||||
contents=prompt,
|
||||
config=types.GenerateContentConfig(**config, **kwargs)
|
||||
)
|
||||
|
||||
result = {
|
||||
"text": response.text,
|
||||
"usage": {
|
||||
"prompt_tokens": response.usage_metadata.prompt_token_count,
|
||||
"candidates_tokens": response.usage_metadata.candidates_token_count,
|
||||
"total_tokens": response.usage_metadata.total_token_count,
|
||||
}
|
||||
}
|
||||
|
||||
# Extract thoughts if present
|
||||
thoughts = []
|
||||
for part in response.candidates[0].content.parts:
|
||||
if hasattr(part, 'thought') and part.thought:
|
||||
thoughts.append(part.thought)
|
||||
|
||||
if thoughts:
|
||||
result["thoughts"] = "\n".join(thoughts)
|
||||
|
||||
# Extract grounding metadata
|
||||
if response.candidates[0].grounding_metadata:
|
||||
result["grounding"] = response.candidates[0].grounding_metadata
|
||||
|
||||
return result
|
||||
73
agent/knowledge_ingester.py
Normal file
73
agent/knowledge_ingester.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""Sovereign Knowledge Ingester for Hermes Agent.
|
||||
|
||||
Uses Gemini 3.1 Pro to learn from Google Search in real-time and
|
||||
persists the knowledge to Timmy's sovereign memory (both Markdown and Symbolic).
|
||||
"""
|
||||
|
||||
import logging
|
||||
import base64
|
||||
from typing import Any, Dict, List, Optional
|
||||
from agent.gemini_adapter import GeminiAdapter
|
||||
from agent.symbolic_memory import SymbolicMemory
|
||||
from tools.gitea_client import GiteaClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class KnowledgeIngester:
|
||||
def __init__(self):
|
||||
self.adapter = GeminiAdapter()
|
||||
self.gitea = GiteaClient()
|
||||
self.symbolic = SymbolicMemory()
|
||||
|
||||
def learn_about(self, topic: str) -> str:
|
||||
"""Searches Google, analyzes the results, and saves the knowledge."""
|
||||
logger.info(f"Learning about: {topic}")
|
||||
|
||||
# 1. Search and Analyze
|
||||
prompt = f"""
|
||||
Please perform a deep dive into the following topic: {topic}
|
||||
|
||||
Use Google Search to find the most recent and relevant information.
|
||||
Analyze the findings and provide a structured 'Knowledge Fragment' in Markdown format.
|
||||
Include:
|
||||
- Summary of the topic
|
||||
- Key facts and recent developments
|
||||
- Implications for Timmy's sovereign mission
|
||||
- References (URLs)
|
||||
"""
|
||||
result = self.adapter.generate(
|
||||
model="gemini-3.1-pro-preview",
|
||||
prompt=prompt,
|
||||
system_instruction="You are Timmy's Sovereign Knowledge Ingester. Your goal is to find and synthesize high-fidelity information from Google Search.",
|
||||
grounding=True,
|
||||
thinking=True
|
||||
)
|
||||
|
||||
knowledge_fragment = result["text"]
|
||||
|
||||
# 2. Extract Symbolic Triples
|
||||
self.symbolic.ingest_text(knowledge_fragment)
|
||||
|
||||
# 3. Persist to Timmy's Memory (Markdown)
|
||||
repo = "Timmy_Foundation/timmy-config"
|
||||
filename = f"memories/realtime_learning/{topic.lower().replace(' ', '_')}.md"
|
||||
|
||||
try:
|
||||
sha = None
|
||||
try:
|
||||
existing = self.gitea.get_file(repo, filename)
|
||||
sha = existing.get("sha")
|
||||
except:
|
||||
pass
|
||||
|
||||
content_b64 = base64.b64encode(knowledge_fragment.encode()).decode()
|
||||
|
||||
if sha:
|
||||
self.gitea.update_file(repo, filename, content_b64, f"Update knowledge on {topic}", sha)
|
||||
else:
|
||||
self.gitea.create_file(repo, filename, content_b64, f"Initial knowledge on {topic}")
|
||||
|
||||
return f"Successfully learned about {topic}. Updated Timmy's Markdown memory and Symbolic Knowledge Graph."
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to persist knowledge: {e}")
|
||||
return f"Learned about {topic}, but failed to save to Markdown memory: {e}\n\n{knowledge_fragment}"
|
||||
47
agent/meta_reasoning.py
Normal file
47
agent/meta_reasoning.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Meta-Reasoning Layer for Hermes Agent.
|
||||
|
||||
Implements a sovereign self-correction loop where a 'strong' model (Gemini 3.1 Pro)
|
||||
critiques the plans generated by the primary agent loop before execution.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Any, Dict, List, Optional
|
||||
from agent.gemini_adapter import GeminiAdapter
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class MetaReasoningLayer:
|
||||
def __init__(self):
|
||||
self.adapter = GeminiAdapter()
|
||||
|
||||
def critique_plan(self, goal: str, proposed_plan: str, context: str) -> Dict[str, Any]:
|
||||
"""Critiques a proposed plan using Gemini's thinking capabilities."""
|
||||
prompt = f"""
|
||||
Goal: {goal}
|
||||
|
||||
Context:
|
||||
{context}
|
||||
|
||||
Proposed Plan:
|
||||
{proposed_plan}
|
||||
|
||||
Please perform a deep symbolic and neuro-symbolic analysis of this plan.
|
||||
Identify potential risks, logical fallacies, or missing steps.
|
||||
Suggest improvements to make the plan more sovereign, cost-efficient, and robust.
|
||||
"""
|
||||
try:
|
||||
result = self.adapter.generate(
|
||||
model="gemini-3.1-pro-preview",
|
||||
prompt=prompt,
|
||||
system_instruction="You are a Senior Meta-Reasoning Engine for the Hermes Agent. Your goal is to ensure the agent's plans are flawless and sovereign.",
|
||||
thinking=True,
|
||||
thinking_budget=8000
|
||||
)
|
||||
return {
|
||||
"critique": result["text"],
|
||||
"thoughts": result.get("thoughts", ""),
|
||||
"grounding": result.get("grounding")
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Meta-reasoning failed: {e}")
|
||||
return {"critique": "Meta-reasoning unavailable.", "error": str(e)}
|
||||
74
agent/symbolic_memory.py
Normal file
74
agent/symbolic_memory.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""Sovereign Intersymbolic Memory Layer.
|
||||
|
||||
Bridges Neural (LLM) and Symbolic (Graph) reasoning by extracting
|
||||
structured triples from unstructured text and performing graph lookups.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import json
|
||||
from typing import List, Dict, Any
|
||||
from agent.gemini_adapter import GeminiAdapter
|
||||
from tools.graph_store import GraphStore
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class SymbolicMemory:
|
||||
def __init__(self):
|
||||
self.adapter = GeminiAdapter()
|
||||
self.store = GraphStore()
|
||||
|
||||
def ingest_text(self, text: str):
|
||||
"""Extracts triples from text and adds them to the graph."""
|
||||
prompt = f"""
|
||||
Extract all meaningful entities and their relationships from the following text.
|
||||
Format the output as a JSON list of triples: [{{"s": "subject", "p": "predicate", "o": "object"}}]
|
||||
|
||||
Text:
|
||||
{text}
|
||||
|
||||
Guidelines:
|
||||
- Use clear, concise labels for entities and predicates.
|
||||
- Focus on stable facts and structural relationships.
|
||||
- Predicates should be verbs or descriptive relations (e.g., 'is_a', 'works_at', 'collaborates_with').
|
||||
"""
|
||||
try:
|
||||
result = self.adapter.generate(
|
||||
model="gemini-3.1-pro-preview",
|
||||
prompt=prompt,
|
||||
system_instruction="You are Timmy's Symbolic Extraction Engine. Extract high-fidelity knowledge triples.",
|
||||
response_mime_type="application/json"
|
||||
)
|
||||
|
||||
triples = json.loads(result["text"])
|
||||
if isinstance(triples, list):
|
||||
count = self.store.add_triples(triples)
|
||||
logger.info(f"Ingested {count} new triples into symbolic memory.")
|
||||
return count
|
||||
except Exception as e:
|
||||
logger.error(f"Symbolic ingestion failed: {e}")
|
||||
return 0
|
||||
|
||||
def get_context_for(self, topic: str) -> str:
|
||||
"""Performs a 2-hop graph search to find related context for a topic."""
|
||||
# 1. Find direct relations
|
||||
direct = self.store.query(subject=topic) + self.store.query(object=topic)
|
||||
|
||||
# 2. Find 2nd hop
|
||||
related_entities = set()
|
||||
for t in direct:
|
||||
related_entities.add(t['s'])
|
||||
related_entities.add(t['o'])
|
||||
|
||||
extended = []
|
||||
for entity in related_entities:
|
||||
if entity == topic: continue
|
||||
extended.extend(self.store.query(subject=entity))
|
||||
|
||||
all_triples = direct + extended
|
||||
if not all_triples:
|
||||
return ""
|
||||
|
||||
context = "Symbolic Knowledge Graph Context:\n"
|
||||
for t in all_triples:
|
||||
context += f"- {t['s']} --({t['p']})--> {t['o']}\n"
|
||||
return context
|
||||
@@ -75,6 +75,22 @@ class CostResult:
|
||||
notes: tuple[str, ...] = ()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CostBreakdown:
|
||||
input_usd: Optional[Decimal]
|
||||
output_usd: Optional[Decimal]
|
||||
cache_read_usd: Optional[Decimal]
|
||||
cache_write_usd: Optional[Decimal]
|
||||
request_usd: Optional[Decimal]
|
||||
total_usd: Optional[Decimal]
|
||||
status: CostStatus
|
||||
source: CostSource
|
||||
label: str
|
||||
fetched_at: Optional[datetime] = None
|
||||
pricing_version: Optional[str] = None
|
||||
notes: tuple[str, ...] = ()
|
||||
|
||||
|
||||
_UTC_NOW = lambda: datetime.now(timezone.utc)
|
||||
|
||||
|
||||
@@ -93,6 +109,25 @@ _OFFICIAL_DOCS_PRICING: Dict[tuple[str, str], PricingEntry] = {
|
||||
source_url="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching",
|
||||
pricing_version="anthropic-prompt-caching-2026-03-16",
|
||||
),
|
||||
# Aliases for short model names (Anthropic API resolves these to dated versions)
|
||||
("anthropic", "claude-opus-4-6"): PricingEntry(
|
||||
input_cost_per_million=Decimal("15.00"),
|
||||
output_cost_per_million=Decimal("75.00"),
|
||||
cache_read_cost_per_million=Decimal("1.50"),
|
||||
cache_write_cost_per_million=Decimal("18.75"),
|
||||
source="official_docs_snapshot",
|
||||
source_url="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching",
|
||||
pricing_version="anthropic-prompt-caching-2026-03-16",
|
||||
),
|
||||
("anthropic", "claude-opus-4.6"): PricingEntry(
|
||||
input_cost_per_million=Decimal("15.00"),
|
||||
output_cost_per_million=Decimal("75.00"),
|
||||
cache_read_cost_per_million=Decimal("1.50"),
|
||||
cache_write_cost_per_million=Decimal("18.75"),
|
||||
source="official_docs_snapshot",
|
||||
source_url="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching",
|
||||
pricing_version="anthropic-prompt-caching-2026-03-16",
|
||||
),
|
||||
(
|
||||
"anthropic",
|
||||
"claude-sonnet-4-20250514",
|
||||
@@ -105,6 +140,24 @@ _OFFICIAL_DOCS_PRICING: Dict[tuple[str, str], PricingEntry] = {
|
||||
source_url="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching",
|
||||
pricing_version="anthropic-prompt-caching-2026-03-16",
|
||||
),
|
||||
("anthropic", "claude-sonnet-4-5"): PricingEntry(
|
||||
input_cost_per_million=Decimal("3.00"),
|
||||
output_cost_per_million=Decimal("15.00"),
|
||||
cache_read_cost_per_million=Decimal("0.30"),
|
||||
cache_write_cost_per_million=Decimal("3.75"),
|
||||
source="official_docs_snapshot",
|
||||
source_url="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching",
|
||||
pricing_version="anthropic-prompt-caching-2026-03-16",
|
||||
),
|
||||
("anthropic", "claude-sonnet-4.5"): PricingEntry(
|
||||
input_cost_per_million=Decimal("3.00"),
|
||||
output_cost_per_million=Decimal("15.00"),
|
||||
cache_read_cost_per_million=Decimal("0.30"),
|
||||
cache_write_cost_per_million=Decimal("3.75"),
|
||||
source="official_docs_snapshot",
|
||||
source_url="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching",
|
||||
pricing_version="anthropic-prompt-caching-2026-03-16",
|
||||
),
|
||||
# OpenAI
|
||||
(
|
||||
"openai",
|
||||
@@ -654,3 +707,80 @@ def format_token_count_compact(value: int) -> str:
|
||||
return f"{sign}{text}{suffix}"
|
||||
|
||||
return f"{value:,}"
|
||||
|
||||
|
||||
|
||||
def estimate_usage_cost_breakdown(
|
||||
model_name: str,
|
||||
usage: CanonicalUsage,
|
||||
*,
|
||||
provider: Optional[str] = None,
|
||||
base_url: Optional[str] = None,
|
||||
api_key: Optional[str] = None,
|
||||
) -> CostBreakdown:
|
||||
"""Estimate per-bucket cost breakdown for a usage record.
|
||||
|
||||
Returns the same status/source semantics as estimate_usage_cost(), but splits
|
||||
the total into input/cache/output/request components when pricing data is
|
||||
available. For subscription-included routes (e.g. openai-codex), all
|
||||
components are reported as zero-cost instead of unknown.
|
||||
"""
|
||||
cost_result = estimate_usage_cost(
|
||||
model_name,
|
||||
usage,
|
||||
provider=provider,
|
||||
base_url=base_url,
|
||||
api_key=api_key,
|
||||
)
|
||||
route = resolve_billing_route(model_name, provider=provider, base_url=base_url)
|
||||
entry = get_pricing_entry(model_name, provider=provider, base_url=base_url, api_key=api_key)
|
||||
if not entry and route.billing_mode == "subscription_included":
|
||||
entry = PricingEntry(
|
||||
input_cost_per_million=_ZERO,
|
||||
output_cost_per_million=_ZERO,
|
||||
cache_read_cost_per_million=_ZERO,
|
||||
cache_write_cost_per_million=_ZERO,
|
||||
request_cost=_ZERO,
|
||||
source="none",
|
||||
pricing_version="included-route",
|
||||
)
|
||||
|
||||
if not entry:
|
||||
return CostBreakdown(
|
||||
input_usd=None,
|
||||
output_usd=None,
|
||||
cache_read_usd=None,
|
||||
cache_write_usd=None,
|
||||
request_usd=None,
|
||||
total_usd=cost_result.amount_usd,
|
||||
status=cost_result.status,
|
||||
source=cost_result.source,
|
||||
label=cost_result.label,
|
||||
fetched_at=cost_result.fetched_at,
|
||||
pricing_version=cost_result.pricing_version,
|
||||
notes=cost_result.notes,
|
||||
)
|
||||
|
||||
def _component(tokens: int, rate: Optional[Decimal]) -> Optional[Decimal]:
|
||||
if rate is None:
|
||||
return None
|
||||
return (Decimal(tokens or 0) * rate) / _ONE_MILLION
|
||||
|
||||
request_usd = None
|
||||
if entry.request_cost is not None:
|
||||
request_usd = Decimal(usage.request_count or 0) * entry.request_cost
|
||||
|
||||
return CostBreakdown(
|
||||
input_usd=_component(usage.input_tokens, entry.input_cost_per_million),
|
||||
output_usd=_component(usage.output_tokens, entry.output_cost_per_million),
|
||||
cache_read_usd=_component(usage.cache_read_tokens, entry.cache_read_cost_per_million),
|
||||
cache_write_usd=_component(usage.cache_write_tokens, entry.cache_write_cost_per_million),
|
||||
request_usd=request_usd,
|
||||
total_usd=cost_result.amount_usd,
|
||||
status=cost_result.status,
|
||||
source=cost_result.source,
|
||||
label=cost_result.label,
|
||||
fetched_at=cost_result.fetched_at,
|
||||
pricing_version=cost_result.pricing_version,
|
||||
notes=cost_result.notes,
|
||||
)
|
||||
|
||||
76
cli.py
76
cli.py
@@ -4563,7 +4563,30 @@ class HermesCLI:
|
||||
print("(._.) No API calls made yet in this session.")
|
||||
return
|
||||
|
||||
# Current context window state
|
||||
def _fmt_money(amount):
|
||||
return "n/a" if amount is None else f"${float(amount):.4f}"
|
||||
|
||||
def _fmt_limit(remaining, limit):
|
||||
if remaining is None and limit is None:
|
||||
return "n/a"
|
||||
if remaining is None:
|
||||
return f"? / {limit:,}"
|
||||
if limit is None:
|
||||
return f"{remaining:,} / ?"
|
||||
return f"{remaining:,} / {limit:,}"
|
||||
|
||||
def _fmt_reset(seconds):
|
||||
if seconds is None:
|
||||
return "n/a"
|
||||
seconds = int(seconds)
|
||||
if seconds < 60:
|
||||
return f"{seconds}s"
|
||||
minutes, secs = divmod(seconds, 60)
|
||||
if minutes < 60:
|
||||
return f"{minutes}m {secs}s"
|
||||
hours, minutes = divmod(minutes, 60)
|
||||
return f"{hours}h {minutes}m"
|
||||
|
||||
compressor = agent.context_compressor
|
||||
last_prompt = compressor.last_prompt_tokens
|
||||
ctx_len = compressor.context_length
|
||||
@@ -4571,14 +4594,21 @@ class HermesCLI:
|
||||
compressions = compressor.compression_count
|
||||
|
||||
msg_count = len(self.conversation_history)
|
||||
usage = CanonicalUsage(
|
||||
input_tokens=input_tokens,
|
||||
output_tokens=output_tokens,
|
||||
cache_read_tokens=cache_read_tokens,
|
||||
cache_write_tokens=cache_write_tokens,
|
||||
)
|
||||
cost_result = estimate_usage_cost(
|
||||
agent.model,
|
||||
CanonicalUsage(
|
||||
input_tokens=input_tokens,
|
||||
output_tokens=output_tokens,
|
||||
cache_read_tokens=cache_read_tokens,
|
||||
cache_write_tokens=cache_write_tokens,
|
||||
),
|
||||
usage,
|
||||
provider=getattr(agent, "provider", None),
|
||||
base_url=getattr(agent, "base_url", None),
|
||||
)
|
||||
cost_breakdown = estimate_usage_cost_breakdown(
|
||||
agent.model,
|
||||
usage,
|
||||
provider=getattr(agent, "provider", None),
|
||||
base_url=getattr(agent, "base_url", None),
|
||||
)
|
||||
@@ -4605,6 +4635,38 @@ class HermesCLI:
|
||||
print(f" Total cost: {'included':>10}")
|
||||
else:
|
||||
print(f" Total cost: {'n/a':>10}")
|
||||
print(f" Cost input: {_fmt_money(cost_breakdown.input_usd):>10}")
|
||||
print(f" Cost cache read: {_fmt_money(cost_breakdown.cache_read_usd):>10}")
|
||||
print(f" Cost cache write: {_fmt_money(cost_breakdown.cache_write_usd):>10}")
|
||||
print(f" Cost output: {_fmt_money(cost_breakdown.output_usd):>10}")
|
||||
if cost_breakdown.request_usd is not None:
|
||||
print(f" Cost requests: {_fmt_money(cost_breakdown.request_usd):>10}")
|
||||
|
||||
rate_limits = getattr(agent, "session_openai_rate_limits", None) or {}
|
||||
last_request_id = getattr(agent, "session_last_request_id", None)
|
||||
rate_limit_events = getattr(agent, "session_rate_limit_events", 0) or 0
|
||||
if last_request_id:
|
||||
print(f" Last request id: {last_request_id:>10}")
|
||||
if rate_limits:
|
||||
status_code = rate_limits.get("status_code")
|
||||
if status_code is not None:
|
||||
print(f" Last HTTP status: {status_code:>10}")
|
||||
req_remaining = rate_limits.get("remaining_requests")
|
||||
req_limit = rate_limits.get("limit_requests")
|
||||
req_reset = rate_limits.get("reset_requests_seconds")
|
||||
if req_remaining is not None or req_limit is not None:
|
||||
print(f" Req limit: {_fmt_limit(req_remaining, req_limit):>14} reset {_fmt_reset(req_reset)}")
|
||||
tok_remaining = rate_limits.get("remaining_tokens")
|
||||
tok_limit = rate_limits.get("limit_tokens")
|
||||
tok_reset = rate_limits.get("reset_tokens_seconds")
|
||||
if tok_remaining is not None or tok_limit is not None:
|
||||
print(f" Token limit: {_fmt_limit(tok_remaining, tok_limit):>14} reset {_fmt_reset(tok_reset)}")
|
||||
retry_after = rate_limits.get("retry_after_seconds")
|
||||
if retry_after is not None:
|
||||
print(f" Retry after: {_fmt_reset(retry_after):>10}")
|
||||
if rate_limit_events:
|
||||
print(f" Rate limit hits: {rate_limit_events:>10,}")
|
||||
|
||||
print(f" {'─' * 40}")
|
||||
print(f" Current context: {last_prompt:,} / {ctx_len:,} ({pct:.0f}%)")
|
||||
print(f" Messages: {msg_count}")
|
||||
|
||||
@@ -220,6 +220,39 @@ PROVIDER_REGISTRY: Dict[str, ProviderConfig] = {
|
||||
api_key_env_vars=("HF_TOKEN",),
|
||||
base_url_env_var="HF_BASE_URL",
|
||||
),
|
||||
# ── Uniwizard backends (added 2026-03-30) ─────────────────────────
|
||||
"gemini": ProviderConfig(
|
||||
id="gemini",
|
||||
name="Google Gemini",
|
||||
auth_type="api_key",
|
||||
inference_base_url="https://generativelanguage.googleapis.com/v1beta/openai",
|
||||
api_key_env_vars=("GEMINI_API_KEY",),
|
||||
base_url_env_var="GEMINI_BASE_URL",
|
||||
),
|
||||
"groq": ProviderConfig(
|
||||
id="groq",
|
||||
name="Groq",
|
||||
auth_type="api_key",
|
||||
inference_base_url="https://api.groq.com/openai/v1",
|
||||
api_key_env_vars=("GROQ_API_KEY",),
|
||||
base_url_env_var="GROQ_BASE_URL",
|
||||
),
|
||||
"grok": ProviderConfig(
|
||||
id="grok",
|
||||
name="xAI Grok",
|
||||
auth_type="api_key",
|
||||
inference_base_url="https://api.x.ai/v1",
|
||||
api_key_env_vars=("XAI_API_KEY", "GROK_API_KEY"),
|
||||
base_url_env_var="XAI_BASE_URL",
|
||||
),
|
||||
"openrouter": ProviderConfig(
|
||||
id="openrouter",
|
||||
name="OpenRouter",
|
||||
auth_type="api_key",
|
||||
inference_base_url="https://openrouter.ai/api/v1",
|
||||
api_key_env_vars=("OPENROUTER_API_KEY",),
|
||||
base_url_env_var="OPENROUTER_BASE_URL",
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ dependencies = [
|
||||
# Core — pinned to known-good ranges to limit supply chain attack surface
|
||||
"openai>=2.21.0,<3",
|
||||
"anthropic>=0.39.0,<1",
|
||||
"google-genai>=1.2.0,<2",
|
||||
"python-dotenv>=1.2.1,<2",
|
||||
"fire>=0.7.1,<1",
|
||||
"httpx>=0.28.1,<1",
|
||||
|
||||
137
run_agent.py
137
run_agent.py
@@ -3472,6 +3472,79 @@ class AIAgent:
|
||||
http_client = getattr(client, "_client", None)
|
||||
return bool(getattr(http_client, "is_closed", False))
|
||||
|
||||
def _coerce_rate_limit_int(self, value: Any) -> Optional[int]:
|
||||
try:
|
||||
if value is None or value == "":
|
||||
return None
|
||||
return int(float(str(value).strip()))
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def _parse_rate_limit_reset_seconds(self, value: Any) -> Optional[int]:
|
||||
if value is None:
|
||||
return None
|
||||
text = str(value).strip().lower()
|
||||
if not text:
|
||||
return None
|
||||
try:
|
||||
return int(round(float(text)))
|
||||
except Exception:
|
||||
pass
|
||||
total = 0.0
|
||||
matches = re.findall(r"(\d+(?:\.\d+)?)(ms|s|m|h)", text)
|
||||
if not matches:
|
||||
return None
|
||||
for number, unit in matches:
|
||||
value_f = float(number)
|
||||
if unit == "ms":
|
||||
total += value_f / 1000.0
|
||||
elif unit == "s":
|
||||
total += value_f
|
||||
elif unit == "m":
|
||||
total += value_f * 60.0
|
||||
elif unit == "h":
|
||||
total += value_f * 3600.0
|
||||
return int(round(total))
|
||||
|
||||
def _capture_openai_http_response(self, response: Any) -> None:
|
||||
if self.api_mode == "anthropic_messages":
|
||||
return
|
||||
headers = getattr(response, "headers", None)
|
||||
if not headers:
|
||||
return
|
||||
lowered = {str(k).lower(): str(v) for k, v in headers.items()}
|
||||
telemetry = dict(getattr(self, "session_openai_rate_limits", {}) or {})
|
||||
|
||||
def _put(key: str, value: Any) -> None:
|
||||
if value is not None:
|
||||
telemetry[key] = value
|
||||
|
||||
_put("status_code", getattr(response, "status_code", None))
|
||||
_put("limit_requests", self._coerce_rate_limit_int(lowered.get("x-ratelimit-limit-requests")))
|
||||
_put("remaining_requests", self._coerce_rate_limit_int(lowered.get("x-ratelimit-remaining-requests")))
|
||||
_put("limit_tokens", self._coerce_rate_limit_int(lowered.get("x-ratelimit-limit-tokens")))
|
||||
_put("remaining_tokens", self._coerce_rate_limit_int(lowered.get("x-ratelimit-remaining-tokens")))
|
||||
_put("reset_requests_seconds", self._parse_rate_limit_reset_seconds(lowered.get("x-ratelimit-reset-requests")))
|
||||
_put("reset_tokens_seconds", self._parse_rate_limit_reset_seconds(lowered.get("x-ratelimit-reset-tokens")))
|
||||
|
||||
retry_after_seconds = None
|
||||
retry_after_ms = self._coerce_rate_limit_int(lowered.get("retry-after-ms"))
|
||||
if retry_after_ms is not None:
|
||||
retry_after_seconds = max(0, int(round(retry_after_ms / 1000.0)))
|
||||
if retry_after_seconds is None:
|
||||
retry_after_seconds = self._parse_rate_limit_reset_seconds(lowered.get("retry-after"))
|
||||
_put("retry_after_seconds", retry_after_seconds)
|
||||
_put("observed_at", int(time.time()))
|
||||
|
||||
request_id = lowered.get("x-request-id") or lowered.get("openai-request-id")
|
||||
if request_id:
|
||||
self.session_last_request_id = request_id
|
||||
_put("request_id", request_id)
|
||||
|
||||
self.session_openai_rate_limits = telemetry
|
||||
if getattr(response, "status_code", None) == 429:
|
||||
self.session_rate_limit_events = (getattr(self, "session_rate_limit_events", 0) or 0) + 1
|
||||
|
||||
def _create_openai_client(self, client_kwargs: dict, *, reason: str, shared: bool) -> Any:
|
||||
if self.provider == "copilot-acp" or str(client_kwargs.get("base_url", "")).startswith("acp://copilot"):
|
||||
from agent.copilot_acp_client import CopilotACPClient
|
||||
@@ -3485,6 +3558,23 @@ class AIAgent:
|
||||
)
|
||||
return client
|
||||
client = OpenAI(**client_kwargs)
|
||||
http_client = getattr(client, "_client", None)
|
||||
if http_client is not None and not getattr(http_client, "_hermes_response_telemetry_installed", False):
|
||||
original_send = http_client.send
|
||||
|
||||
def _send_with_telemetry(request, *args, **kwargs):
|
||||
response = original_send(request, *args, **kwargs)
|
||||
try:
|
||||
self._capture_openai_http_response(response)
|
||||
except Exception as exc:
|
||||
logger.debug("OpenAI response telemetry capture failed: %s", exc)
|
||||
return response
|
||||
|
||||
http_client.send = _send_with_telemetry
|
||||
try:
|
||||
setattr(http_client, "_hermes_response_telemetry_installed", True)
|
||||
except Exception:
|
||||
pass
|
||||
logger.info(
|
||||
"OpenAI client created (%s, shared=%s) %s",
|
||||
reason,
|
||||
@@ -7466,6 +7556,53 @@ class AIAgent:
|
||||
if hasattr(self, '_incomplete_scratchpad_retries'):
|
||||
self._incomplete_scratchpad_retries = 0
|
||||
|
||||
# ── Uniwizard: Semantic refusal detection ──────────────────
|
||||
# Catches 200 OK responses where the model REFUSED the request.
|
||||
# No existing LLM gateway does this. This is novel.
|
||||
if (assistant_message.content
|
||||
and not assistant_message.tool_calls
|
||||
and self._fallback_index < len(self._fallback_chain)):
|
||||
_refusal_text = (assistant_message.content or "").strip()
|
||||
_REFUSAL_PATTERNS = (
|
||||
"I can't help with",
|
||||
"I cannot help with",
|
||||
"I'm not able to",
|
||||
"I am not able to",
|
||||
"I must decline",
|
||||
"I'm unable to",
|
||||
"I am unable to",
|
||||
"against my guidelines",
|
||||
"against my policy",
|
||||
"I can't assist with",
|
||||
"I cannot assist with",
|
||||
"I apologize, but I can't",
|
||||
"I'm sorry, but I can't",
|
||||
"I'm sorry, but I cannot",
|
||||
"not something I can help",
|
||||
"I don't think I should",
|
||||
"I can't fulfill that",
|
||||
"I cannot fulfill that",
|
||||
"I'm not comfortable",
|
||||
"I can't provide",
|
||||
"I cannot provide",
|
||||
)
|
||||
_refusal_lower = _refusal_text.lower()
|
||||
_is_refusal = any(p.lower() in _refusal_lower for p in _REFUSAL_PATTERNS)
|
||||
if _is_refusal:
|
||||
_fb_target = self._fallback_chain[self._fallback_index]
|
||||
self._emit_status(
|
||||
f"🚫 Semantic refusal detected from {self.provider}/{self.model}. "
|
||||
f"Rerouting to {_fb_target.get('model', '?')} via {_fb_target.get('provider', '?')}..."
|
||||
)
|
||||
logging.warning(
|
||||
"Refusal detected from %s/%s: %.80s...",
|
||||
self.provider, self.model, _refusal_text,
|
||||
)
|
||||
if self._try_activate_fallback():
|
||||
retry_count = 0
|
||||
continue
|
||||
# ── End refusal detection ──────────────────────────────────
|
||||
|
||||
if self.api_mode == "codex_responses" and finish_reason == "incomplete":
|
||||
if not hasattr(self, "_codex_incomplete_retries"):
|
||||
self._codex_incomplete_retries = 0
|
||||
|
||||
47
skills/creative/sovereign_thinking.py
Normal file
47
skills/creative/sovereign_thinking.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
---
|
||||
title: Sovereign Thinking
|
||||
description: Pauses the agent to perform deep reasoning on complex problems using Gemini 3.1 Pro.
|
||||
conditions:
|
||||
- Complex logic required
|
||||
- High-stakes decision making
|
||||
- Architecture or design tasks
|
||||
---
|
||||
"""
|
||||
|
||||
from agent.gemini_adapter import GeminiAdapter
|
||||
|
||||
def think(problem: str, effort: str = "medium") -> str:
|
||||
"""
|
||||
Performs deep reasoning on a complex problem.
|
||||
|
||||
Args:
|
||||
problem: The complex problem or question to analyze.
|
||||
effort: The reasoning effort ('low', 'medium', 'high', 'xhigh').
|
||||
"""
|
||||
adapter = GeminiAdapter()
|
||||
|
||||
budget_map = {
|
||||
"low": 4000,
|
||||
"medium": 16000,
|
||||
"high": 32000,
|
||||
"xhigh": 64000
|
||||
}
|
||||
|
||||
budget = budget_map.get(effort, 16000)
|
||||
|
||||
result = adapter.generate(
|
||||
model="gemini-3.1-pro-preview",
|
||||
prompt=problem,
|
||||
system_instruction="You are the internal reasoning engine of the Hermes Agent. Think deeply and provide a structured analysis.",
|
||||
thinking=True,
|
||||
thinking_budget=budget
|
||||
)
|
||||
|
||||
output = []
|
||||
if result.get("thoughts"):
|
||||
output.append("### Internal Monologue\n" + result["thoughts"])
|
||||
|
||||
output.append("### Conclusion\n" + result["text"])
|
||||
|
||||
return "\n\n".join(output)
|
||||
27
skills/memory/intersymbolic_graph.py
Normal file
27
skills/memory/intersymbolic_graph.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""
|
||||
---
|
||||
title: Intersymbolic Graph Query
|
||||
description: Queries Timmy's sovereign knowledge graph to find connections and structured facts.
|
||||
conditions:
|
||||
- Complex relationship analysis
|
||||
- Fact checking against structured memory
|
||||
- Finding non-obvious connections
|
||||
---
|
||||
"""
|
||||
|
||||
from agent.symbolic_memory import SymbolicMemory
|
||||
|
||||
def query_graph(topic: str) -> str:
|
||||
"""
|
||||
Queries the knowledge graph for a specific topic and returns structured context.
|
||||
|
||||
Args:
|
||||
topic: The entity or topic to search for in the graph.
|
||||
"""
|
||||
memory = SymbolicMemory()
|
||||
context = memory.get_context_for(topic)
|
||||
|
||||
if not context:
|
||||
return f"No symbolic connections found for '{topic}' in the knowledge graph."
|
||||
|
||||
return context
|
||||
22
skills/research/realtime_learning.py
Normal file
22
skills/research/realtime_learning.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""
|
||||
---
|
||||
title: Real-time Learning
|
||||
description: Allows Timmy to learn about any topic in real-time using Google Search and persist it to his sovereign memory.
|
||||
conditions:
|
||||
- New information required
|
||||
- Real-time events or trends
|
||||
- Knowledge base expansion
|
||||
---
|
||||
"""
|
||||
|
||||
from agent.knowledge_ingester import KnowledgeIngester
|
||||
|
||||
def learn(topic: str) -> str:
|
||||
"""
|
||||
Performs real-time learning on a topic and updates Timmy's memory.
|
||||
|
||||
Args:
|
||||
topic: The topic to learn about (e.g., 'recent advancements in quantum computing').
|
||||
"""
|
||||
ingester = KnowledgeIngester()
|
||||
return ingester.learn_about(topic)
|
||||
141
tests/agent/test_symbolic_memory.py
Normal file
141
tests/agent/test_symbolic_memory.py
Normal file
@@ -0,0 +1,141 @@
|
||||
"""Tests for Symbolic Memory / Intersymbolic Layer.
|
||||
|
||||
Generated by Allegro during PR #9 review.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
import json
|
||||
|
||||
|
||||
class TestSymbolicMemory:
|
||||
"""Test suite for agent/symbolic_memory.py"""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_adapter(self):
|
||||
"""Mock GeminiAdapter."""
|
||||
with patch('agent.symbolic_memory.GeminiAdapter') as MockAdapter:
|
||||
mock = MagicMock()
|
||||
MockAdapter.return_value = mock
|
||||
yield mock
|
||||
|
||||
@pytest.fixture
|
||||
def mock_store(self):
|
||||
"""Mock GraphStore."""
|
||||
with patch('agent.symbolic_memory.GraphStore') as MockStore:
|
||||
mock = MagicMock()
|
||||
MockStore.return_value = mock
|
||||
yield mock
|
||||
|
||||
@pytest.fixture
|
||||
def memory(self, mock_adapter, mock_store):
|
||||
"""Create SymbolicMemory with mocked deps."""
|
||||
from agent.symbolic_memory import SymbolicMemory
|
||||
return SymbolicMemory()
|
||||
|
||||
def test_ingest_text_success(self, memory, mock_adapter, mock_store):
|
||||
"""Should extract triples and add to graph."""
|
||||
mock_adapter.generate.return_value = {
|
||||
"text": json.dumps([
|
||||
{"s": "Timmy", "p": "is_a", "o": "AI"},
|
||||
{"s": "Timmy", "p": "has_goal", "o": "Sovereignty"}
|
||||
])
|
||||
}
|
||||
mock_store.add_triples.return_value = 2
|
||||
|
||||
count = memory.ingest_text("Timmy is an AI with the goal of Sovereignty.")
|
||||
|
||||
assert count == 2
|
||||
mock_store.add_triples.assert_called_once()
|
||||
|
||||
def test_ingest_text_invalid_json(self, memory, mock_adapter, mock_store):
|
||||
"""Should handle malformed JSON gracefully."""
|
||||
mock_adapter.generate.return_value = {
|
||||
"text": "not valid json"
|
||||
}
|
||||
|
||||
count = memory.ingest_text("Some text that confuses the model")
|
||||
|
||||
assert count == 0 # Should fail gracefully
|
||||
mock_store.add_triples.assert_not_called()
|
||||
|
||||
def test_ingest_text_not_list(self, memory, mock_adapter, mock_store):
|
||||
"""Should handle non-list JSON response."""
|
||||
mock_adapter.generate.return_value = {
|
||||
"text": json.dumps({"s": "Timmy", "p": "is_a", "o": "AI"}) # Dict, not list
|
||||
}
|
||||
|
||||
count = memory.ingest_text("Timmy is an AI")
|
||||
|
||||
# Current implementation might fail here - this test documents the gap
|
||||
# Should be handled: check isinstance(triples, list)
|
||||
|
||||
def test_get_context_for_direct_relations(self, memory, mock_store):
|
||||
"""Should find direct 1-hop relations."""
|
||||
mock_store.query.side_effect = lambda subject=None, **kwargs: [
|
||||
{"s": "Timmy", "p": "is_a", "o": "AI"},
|
||||
{"s": "Timmy", "p": "works_at", "o": "Foundation"}
|
||||
] if subject == "Timmy" else []
|
||||
|
||||
context = memory.get_context_for("Timmy")
|
||||
|
||||
assert "Timmy" in context
|
||||
assert "is_a" in context
|
||||
assert "AI" in context
|
||||
|
||||
def test_get_context_for_2hop(self, memory, mock_store):
|
||||
"""Should find 2-hop relations."""
|
||||
# First call: direct relations
|
||||
# Second call: extended relations
|
||||
mock_store.query.side_effect = [
|
||||
[{"s": "Timmy", "p": "works_at", "o": "Foundation"}], # Direct
|
||||
[{"s": "Foundation", "p": "founded_by", "o": "Alexander"}] # 2-hop
|
||||
]
|
||||
|
||||
context = memory.get_context_for("Timmy")
|
||||
|
||||
assert "Foundation" in context
|
||||
assert "founded_by" in context
|
||||
|
||||
def test_get_context_for_empty(self, memory, mock_store):
|
||||
"""Should return empty string when no context found."""
|
||||
mock_store.query.return_value = []
|
||||
|
||||
context = memory.get_context_for("UnknownEntity")
|
||||
|
||||
assert context == ""
|
||||
|
||||
|
||||
class TestIntersymbolicGraphSkill:
|
||||
"""Test suite for skills/memory/intersymbolic_graph.py"""
|
||||
|
||||
@patch('skills.memory.intersymbolic_graph.SymbolicMemory')
|
||||
def test_query_graph_with_results(self, MockMemory):
|
||||
"""Skill should return formatted context."""
|
||||
from skills.memory.intersymbolic_graph import query_graph
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.get_context_for.return_value = "- Timmy --(is_a)--> AI\n"
|
||||
MockMemory.return_value = mock_instance
|
||||
|
||||
result = query_graph("Timmy")
|
||||
|
||||
assert "Timmy" in result
|
||||
assert "is_a" in result
|
||||
|
||||
@patch('skills.memory.intersymbolic_graph.SymbolicMemory')
|
||||
def test_query_graph_no_results(self, MockMemory):
|
||||
"""Skill should handle empty results gracefully."""
|
||||
from skills.memory.intersymbolic_graph import query_graph
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.get_context_for.return_value = ""
|
||||
MockMemory.return_value = mock_instance
|
||||
|
||||
result = query_graph("Unknown")
|
||||
|
||||
assert "No symbolic connections" in result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
@@ -144,6 +144,42 @@ class TestCLIUsageReport:
|
||||
assert "0.064" in output
|
||||
assert "Session duration:" in output
|
||||
assert "Compressions:" in output
|
||||
assert "Cost input:" in output
|
||||
assert "Cost output:" in output
|
||||
|
||||
def test_show_usage_displays_rate_limit_telemetry(self, capsys):
|
||||
cli_obj = _attach_agent(
|
||||
_make_cli(model="openai/gpt-5.4"),
|
||||
prompt_tokens=10_000,
|
||||
completion_tokens=500,
|
||||
total_tokens=10_500,
|
||||
api_calls=3,
|
||||
context_tokens=10_500,
|
||||
context_length=200_000,
|
||||
)
|
||||
cli_obj.agent.provider = "openai-codex"
|
||||
cli_obj.agent.session_openai_rate_limits = {
|
||||
"status_code": 200,
|
||||
"limit_requests": 60,
|
||||
"remaining_requests": 48,
|
||||
"reset_requests_seconds": 33,
|
||||
"limit_tokens": 2000000,
|
||||
"remaining_tokens": 1750000,
|
||||
"reset_tokens_seconds": 90,
|
||||
"retry_after_seconds": 5,
|
||||
}
|
||||
cli_obj.agent.session_last_request_id = "req_123"
|
||||
cli_obj.agent.session_rate_limit_events = 2
|
||||
cli_obj.verbose = False
|
||||
|
||||
cli_obj._show_usage()
|
||||
output = capsys.readouterr().out
|
||||
|
||||
assert "Last request id:" in output
|
||||
assert "Req limit:" in output
|
||||
assert "Token limit:" in output
|
||||
assert "Retry after:" in output
|
||||
assert "Rate limit hits:" in output
|
||||
|
||||
def test_show_usage_marks_unknown_pricing(self, capsys):
|
||||
cli_obj = _attach_agent(
|
||||
|
||||
156
tests/tools/test_graph_store.py
Normal file
156
tests/tools/test_graph_store.py
Normal file
@@ -0,0 +1,156 @@
|
||||
"""Tests for Knowledge Graph Store.
|
||||
|
||||
Generated by Allegro during PR #9 review.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
import json
|
||||
import base64
|
||||
|
||||
|
||||
class TestGraphStore:
|
||||
"""Test suite for tools/graph_store.py"""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_gitea(self):
|
||||
"""Mock GiteaClient."""
|
||||
with patch('tools.graph_store.GiteaClient') as MockGitea:
|
||||
mock = MagicMock()
|
||||
MockGitea.return_value = mock
|
||||
yield mock
|
||||
|
||||
@pytest.fixture
|
||||
def store(self, mock_gitea):
|
||||
"""Create GraphStore with mocked Gitea."""
|
||||
from tools.graph_store import GraphStore
|
||||
return GraphStore()
|
||||
|
||||
def test_load_empty_graph(self, store, mock_gitea):
|
||||
"""Should return empty graph when file doesn't exist."""
|
||||
mock_gitea.get_file.side_effect = Exception("404")
|
||||
|
||||
graph = store._load_graph()
|
||||
|
||||
assert graph == {"triples": [], "entities": {}}
|
||||
|
||||
def test_add_triples_new(self, store, mock_gitea):
|
||||
"""Should add new triples."""
|
||||
mock_gitea.get_file.side_effect = Exception("404") # New file
|
||||
|
||||
triples = [
|
||||
{"s": "Timmy", "p": "is_a", "o": "AI"},
|
||||
{"s": "Timmy", "p": "works_at", "o": "Foundation"}
|
||||
]
|
||||
|
||||
count = store.add_triples(triples)
|
||||
|
||||
assert count == 2
|
||||
mock_gitea.create_file.assert_called_once()
|
||||
|
||||
def test_add_triples_deduplication(self, store, mock_gitea):
|
||||
"""Should not add duplicate triples."""
|
||||
existing = {
|
||||
"triples": [{"s": "Timmy", "p": "is_a", "o": "AI"}],
|
||||
"entities": {}
|
||||
}
|
||||
mock_gitea.get_file.return_value = {
|
||||
"content": base64.b64encode(json.dumps(existing).encode()).decode()
|
||||
}
|
||||
|
||||
# Try to add same triple again
|
||||
count = store.add_triples([{"s": "Timmy", "p": "is_a", "o": "AI"}])
|
||||
|
||||
assert count == 0 # No new triples added
|
||||
|
||||
def test_query_by_subject(self, store, mock_gitea):
|
||||
"""Should filter by subject."""
|
||||
existing = {
|
||||
"triples": [
|
||||
{"s": "Timmy", "p": "is_a", "o": "AI"},
|
||||
{"s": "Allegro", "p": "is_a", "o": "AI"},
|
||||
{"s": "Timmy", "p": "works_at", "o": "Foundation"}
|
||||
],
|
||||
"entities": {}
|
||||
}
|
||||
mock_gitea.get_file.return_value = {
|
||||
"content": base64.b64encode(json.dumps(existing).encode()).decode()
|
||||
}
|
||||
|
||||
results = store.query(subject="Timmy")
|
||||
|
||||
assert len(results) == 2
|
||||
assert all(r["s"] == "Timmy" for r in results)
|
||||
|
||||
def test_query_by_predicate(self, store, mock_gitea):
|
||||
"""Should filter by predicate."""
|
||||
existing = {
|
||||
"triples": [
|
||||
{"s": "Timmy", "p": "is_a", "o": "AI"},
|
||||
{"s": "Allegro", "p": "is_a", "o": "AI"},
|
||||
{"s": "Timmy", "p": "works_at", "o": "Foundation"}
|
||||
],
|
||||
"entities": {}
|
||||
}
|
||||
mock_gitea.get_file.return_value = {
|
||||
"content": base64.b64encode(json.dumps(existing).encode()).decode()
|
||||
}
|
||||
|
||||
results = store.query(predicate="is_a")
|
||||
|
||||
assert len(results) == 2
|
||||
assert all(r["p"] == "is_a" for r in results)
|
||||
|
||||
def test_query_by_object(self, store, mock_gitea):
|
||||
"""Should filter by object."""
|
||||
existing = {
|
||||
"triples": [
|
||||
{"s": "Timmy", "p": "is_a", "o": "AI"},
|
||||
{"s": "Allegro", "p": "is_a", "o": "AI"},
|
||||
{"s": "Timmy", "p": "works_at", "o": "Foundation"}
|
||||
],
|
||||
"entities": {}
|
||||
}
|
||||
mock_gitea.get_file.return_value = {
|
||||
"content": base64.b64encode(json.dumps(existing).encode()).decode()
|
||||
}
|
||||
|
||||
results = store.query(object="AI")
|
||||
|
||||
assert len(results) == 2
|
||||
assert all(r["o"] == "AI" for r in results)
|
||||
|
||||
def test_query_combined_filters(self, store, mock_gitea):
|
||||
"""Should support combined filters."""
|
||||
existing = {
|
||||
"triples": [
|
||||
{"s": "Timmy", "p": "is_a", "o": "AI"},
|
||||
{"s": "Timmy", "p": "works_at", "o": "Foundation"}
|
||||
],
|
||||
"entities": {}
|
||||
}
|
||||
mock_gitea.get_file.return_value = {
|
||||
"content": base64.b64encode(json.dumps(existing).encode()).decode()
|
||||
}
|
||||
|
||||
results = store.query(subject="Timmy", predicate="is_a")
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0]["o"] == "AI"
|
||||
|
||||
|
||||
class TestGraphStoreRaceCondition:
|
||||
"""Document race condition behavior."""
|
||||
|
||||
def test_concurrent_writes_risk(self):
|
||||
"""Document that concurrent writes may lose triples.
|
||||
|
||||
This is a known limitation of the read-modify-write pattern.
|
||||
For MVP, this is acceptable. Future: implement file locking or
|
||||
use atomic Gitea operations.
|
||||
"""
|
||||
pass # Documentation test
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
59
tools/gitea_client.py
Normal file
59
tools/gitea_client.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""
|
||||
Gitea API Client — typed, sovereign, zero-dependency.
|
||||
|
||||
Enables the agent to interact with Timmy's sovereign Gitea instance
|
||||
for issue tracking, PR management, and knowledge persistence.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, Dict, List
|
||||
|
||||
class GiteaClient:
|
||||
def __init__(self, base_url: Optional[str] = None, token: Optional[str] = None):
|
||||
self.base_url = base_url or os.environ.get("GITEA_URL", "http://143.198.27.163:3000")
|
||||
self.token = token or os.environ.get("GITEA_TOKEN")
|
||||
self.api = f"{self.base_url.rstrip('/')}/api/v1"
|
||||
|
||||
def _request(self, method: str, path: str, data: Optional[dict] = None) -> Any:
|
||||
url = f"{self.api}{path}"
|
||||
body = json.dumps(data).encode() if data else None
|
||||
req = urllib.request.Request(url, data=body, method=method)
|
||||
if self.token:
|
||||
req.add_header("Authorization", f"token {self.token}")
|
||||
req.add_header("Content-Type", "application/json")
|
||||
req.add_header("Accept", "application/json")
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as resp:
|
||||
raw = resp.read().decode()
|
||||
return json.loads(raw) if raw else {}
|
||||
except urllib.error.HTTPError as e:
|
||||
raise Exception(f"Gitea {e.code}: {e.read().decode()}") from e
|
||||
|
||||
def get_file(self, repo: str, path: str, ref: str = "main") -> Dict[str, Any]:
|
||||
return self._request("GET", f"/repos/{repo}/contents/{path}?ref={ref}")
|
||||
|
||||
def create_file(self, repo: str, path: str, content: str, message: str, branch: str = "main") -> Dict[str, Any]:
|
||||
data = {
|
||||
"branch": branch,
|
||||
"content": content, # Base64 encoded
|
||||
"message": message
|
||||
}
|
||||
return self._request("POST", f"/repos/{repo}/contents/{path}", data)
|
||||
|
||||
def update_file(self, repo: str, path: str, content: str, message: str, sha: str, branch: str = "main") -> Dict[str, Any]:
|
||||
data = {
|
||||
"branch": branch,
|
||||
"content": content, # Base64 encoded
|
||||
"message": message,
|
||||
"sha": sha
|
||||
}
|
||||
return self._request("PUT", f"/repos/{repo}/contents/{path}", data)
|
||||
64
tools/graph_store.py
Normal file
64
tools/graph_store.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""Sovereign Knowledge Graph Store for Hermes Agent.
|
||||
|
||||
Provides a simple triple-store (Subject, Predicate, Object) persisted
|
||||
to Timmy's sovereign Gitea instance.
|
||||
"""
|
||||
|
||||
import json
|
||||
import base64
|
||||
import logging
|
||||
from typing import List, Dict, Any, Optional
|
||||
from tools.gitea_client import GiteaClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class GraphStore:
|
||||
def __init__(self, repo: str = "Timmy_Foundation/timmy-config", path: str = "memories/knowledge_graph.json"):
|
||||
self.repo = repo
|
||||
self.path = path
|
||||
self.gitea = GiteaClient()
|
||||
|
||||
def _load_graph(self) -> Dict[str, Any]:
|
||||
try:
|
||||
content = self.gitea.get_file(self.repo, self.path)
|
||||
raw = base64.b64decode(content["content"]).decode()
|
||||
return json.loads(raw)
|
||||
except Exception:
|
||||
return {"triples": [], "entities": {}}
|
||||
|
||||
def _save_graph(self, graph: Dict[str, Any], message: str):
|
||||
sha = None
|
||||
try:
|
||||
existing = self.gitea.get_file(self.repo, self.path)
|
||||
sha = existing.get("sha")
|
||||
except:
|
||||
pass
|
||||
|
||||
content_b64 = base64.b64encode(json.dumps(graph, indent=2).encode()).decode()
|
||||
if sha:
|
||||
self.gitea.update_file(self.repo, self.path, content_b64, message, sha)
|
||||
else:
|
||||
self.gitea.create_file(self.repo, self.path, content_b64, message)
|
||||
|
||||
def add_triples(self, triples: List[Dict[str, str]]):
|
||||
"""Adds a list of triples: [{'s': '...', 'p': '...', 'o': '...'}]"""
|
||||
graph = self._load_graph()
|
||||
added_count = 0
|
||||
for t in triples:
|
||||
if t not in graph["triples"]:
|
||||
graph["triples"].append(t)
|
||||
added_count += 1
|
||||
|
||||
if added_count > 0:
|
||||
self._save_graph(graph, f"Add {added_count} triples to knowledge graph")
|
||||
return added_count
|
||||
|
||||
def query(self, subject: Optional[str] = None, predicate: Optional[str] = None, object: Optional[str] = None) -> List[Dict[str, str]]:
|
||||
graph = self._load_graph()
|
||||
results = []
|
||||
for t in graph["triples"]:
|
||||
if subject and t['s'] != subject: continue
|
||||
if predicate and t['p'] != predicate: continue
|
||||
if object and t['o'] != object: continue
|
||||
results.append(t)
|
||||
return results
|
||||
Reference in New Issue
Block a user