Files
Timmy-time-dashboard/src/timmy/backends.py

468 lines
16 KiB
Python
Raw Normal View History

"""LLM backends — Grok (xAI) and Claude (Anthropic).
Provides drop-in replacements for the Agno Agent that expose the same
run(message, stream) RunResult interface used by the dashboard and the
print_response(message, stream) interface used by the CLI.
Backends:
- GrokBackend: xAI Grok API via OpenAI-compatible SDK (opt-in premium)
- ClaudeBackend: Anthropic Claude API lightweight cloud fallback
No cloud by default. No telemetry. Sats are sovereignty, boss.
"""
import logging
import platform
import time
ruff (#169) * polish: streamline nav, extract inline styles, improve tablet UX - Restructure desktop nav from 8+ flat links + overflow dropdown into 5 grouped dropdowns (Core, Agents, Intel, System, More) matching the mobile menu structure to reduce decision fatigue - Extract all inline styles from mission_control.html and base.html notification elements into mission-control.css with semantic classes - Replace JS-built innerHTML with secure DOM construction in notification loader and chat history - Add CONNECTING state to connection indicator (amber) instead of showing OFFLINE before WebSocket connects - Add tablet breakpoint (1024px) with larger touch targets for Apple Pencil / stylus use and safe-area padding for iPad toolbar - Add active-link highlighting in desktop dropdown menus - Rename "Mission Control" page title to "System Overview" to disambiguate from the chat home page - Add "Home — Timmy Time" page title to index.html https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * fix(security): move auth-gate credentials to environment variables Hardcoded username, password, and HMAC secret in auth-gate.py replaced with os.environ lookups. Startup now refuses to run if any variable is unset. Added AUTH_GATE_SECRET/USER/PASS to .env.example. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * refactor(tooling): migrate from black+isort+bandit to ruff Replace three separate linting/formatting tools with a single ruff invocation. Updates tox.ini (lint, format, pre-push, pre-commit envs), .pre-commit-config.yaml, and CI workflow. Fixes all ruff errors including unused imports, missing raise-from, and undefined names. Ruff config maps existing bandit skips to equivalent S-rules. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h --------- Co-authored-by: Claude <noreply@anthropic.com>
2026-03-11 12:23:35 -04:00
from dataclasses import dataclass
from timmy.prompts import get_system_prompt
logger = logging.getLogger(__name__)
feat: quality analysis — bug fixes, mobile tests, HITL checklist Senior architect review findings + remediations: BUG FIX — critical interface mismatch - TimmyAirLLMAgent only exposed print_response(); dashboard route calls agent.run() → AttributeError when AirLLM backend is selected. Added run() → RunResult(content) as primary inference entry point; print_response() now delegates to run() so both call sites share one inference path. - Added RunResult dataclass for Agno-compatible structured return. BUG FIX — hardcoded model name in health status partial - health_status.html rendered literal "llama3.2" regardless of OLLAMA_MODEL env var. Route now passes settings.ollama_model to the template context; partial renders {{ model }} instead. FEATURE — /mobile-test HITL checklist page - 22 human-executable test scenarios across: Layout, Touch & Input, Chat behaviour, Health, Scroll, Notch/Home Bar, Live UI. - Pass/Fail/Skip buttons with sessionStorage state persistence. - Live progress bar + final score summary. - TEST link added to Mission Control header for quick access on phone. TEST — 32 new automated mobile quality tests (M1xx–M6xx) - M1xx: viewport/meta tags (8 tests) - M2xx: touch target sizing — 44 px min-height, manipulation (4 tests) - M3xx: iOS zoom prevention, autocapitalize, enterkeyhint (5 tests) - M4xx: HTMX robustness — hx-sync drop, disabled-elt, polling (5 tests) - M5xx: safe-area insets, overscroll, dvh units (5 tests) - M6xx: AirLLM interface contract — run(), RunResult, delegation (5 tests) Total test count: 61 → 93 (all passing). https://claude.ai/code/session_01RBuRCBXZNkAQQXXGiJNDmt
2026-02-21 17:21:47 +00:00
@dataclass
class RunResult:
"""Minimal Agno-compatible run result — carries the model's response text."""
feat: quality analysis — bug fixes, mobile tests, HITL checklist Senior architect review findings + remediations: BUG FIX — critical interface mismatch - TimmyAirLLMAgent only exposed print_response(); dashboard route calls agent.run() → AttributeError when AirLLM backend is selected. Added run() → RunResult(content) as primary inference entry point; print_response() now delegates to run() so both call sites share one inference path. - Added RunResult dataclass for Agno-compatible structured return. BUG FIX — hardcoded model name in health status partial - health_status.html rendered literal "llama3.2" regardless of OLLAMA_MODEL env var. Route now passes settings.ollama_model to the template context; partial renders {{ model }} instead. FEATURE — /mobile-test HITL checklist page - 22 human-executable test scenarios across: Layout, Touch & Input, Chat behaviour, Health, Scroll, Notch/Home Bar, Live UI. - Pass/Fail/Skip buttons with sessionStorage state persistence. - Live progress bar + final score summary. - TEST link added to Mission Control header for quick access on phone. TEST — 32 new automated mobile quality tests (M1xx–M6xx) - M1xx: viewport/meta tags (8 tests) - M2xx: touch target sizing — 44 px min-height, manipulation (4 tests) - M3xx: iOS zoom prevention, autocapitalize, enterkeyhint (5 tests) - M4xx: HTMX robustness — hx-sync drop, disabled-elt, polling (5 tests) - M5xx: safe-area insets, overscroll, dvh units (5 tests) - M6xx: AirLLM interface contract — run(), RunResult, delegation (5 tests) Total test count: 61 → 93 (all passing). https://claude.ai/code/session_01RBuRCBXZNkAQQXXGiJNDmt
2026-02-21 17:21:47 +00:00
content: str
confidence: float | None = None
feat: quality analysis — bug fixes, mobile tests, HITL checklist Senior architect review findings + remediations: BUG FIX — critical interface mismatch - TimmyAirLLMAgent only exposed print_response(); dashboard route calls agent.run() → AttributeError when AirLLM backend is selected. Added run() → RunResult(content) as primary inference entry point; print_response() now delegates to run() so both call sites share one inference path. - Added RunResult dataclass for Agno-compatible structured return. BUG FIX — hardcoded model name in health status partial - health_status.html rendered literal "llama3.2" regardless of OLLAMA_MODEL env var. Route now passes settings.ollama_model to the template context; partial renders {{ model }} instead. FEATURE — /mobile-test HITL checklist page - 22 human-executable test scenarios across: Layout, Touch & Input, Chat behaviour, Health, Scroll, Notch/Home Bar, Live UI. - Pass/Fail/Skip buttons with sessionStorage state persistence. - Live progress bar + final score summary. - TEST link added to Mission Control header for quick access on phone. TEST — 32 new automated mobile quality tests (M1xx–M6xx) - M1xx: viewport/meta tags (8 tests) - M2xx: touch target sizing — 44 px min-height, manipulation (4 tests) - M3xx: iOS zoom prevention, autocapitalize, enterkeyhint (5 tests) - M4xx: HTMX robustness — hx-sync drop, disabled-elt, polling (5 tests) - M5xx: safe-area insets, overscroll, dvh units (5 tests) - M6xx: AirLLM interface contract — run(), RunResult, delegation (5 tests) Total test count: 61 → 93 (all passing). https://claude.ai/code/session_01RBuRCBXZNkAQQXXGiJNDmt
2026-02-21 17:21:47 +00:00
def is_apple_silicon() -> bool:
"""Return True when running on an M-series Mac (arm64 Darwin)."""
return platform.system() == "Darwin" and platform.machine() == "arm64"
# ── Grok (xAI) Backend ─────────────────────────────────────────────────────
# Premium cloud augmentation — opt-in only, never the default path.
# Available Grok models (configurable via GROK_DEFAULT_MODEL)
GROK_MODELS: dict[str, str] = {
"grok-3-fast": "grok-3-fast",
"grok-3": "grok-3",
"grok-3-mini": "grok-3-mini",
"grok-3-mini-fast": "grok-3-mini-fast",
}
@dataclass
class GrokUsageStats:
"""Tracks Grok API usage for cost monitoring and Spark logging."""
total_requests: int = 0
total_prompt_tokens: int = 0
total_completion_tokens: int = 0
total_latency_ms: float = 0.0
errors: int = 0
ruff (#169) * polish: streamline nav, extract inline styles, improve tablet UX - Restructure desktop nav from 8+ flat links + overflow dropdown into 5 grouped dropdowns (Core, Agents, Intel, System, More) matching the mobile menu structure to reduce decision fatigue - Extract all inline styles from mission_control.html and base.html notification elements into mission-control.css with semantic classes - Replace JS-built innerHTML with secure DOM construction in notification loader and chat history - Add CONNECTING state to connection indicator (amber) instead of showing OFFLINE before WebSocket connects - Add tablet breakpoint (1024px) with larger touch targets for Apple Pencil / stylus use and safe-area padding for iPad toolbar - Add active-link highlighting in desktop dropdown menus - Rename "Mission Control" page title to "System Overview" to disambiguate from the chat home page - Add "Home — Timmy Time" page title to index.html https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * fix(security): move auth-gate credentials to environment variables Hardcoded username, password, and HMAC secret in auth-gate.py replaced with os.environ lookups. Startup now refuses to run if any variable is unset. Added AUTH_GATE_SECRET/USER/PASS to .env.example. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * refactor(tooling): migrate from black+isort+bandit to ruff Replace three separate linting/formatting tools with a single ruff invocation. Updates tox.ini (lint, format, pre-push, pre-commit envs), .pre-commit-config.yaml, and CI workflow. Fixes all ruff errors including unused imports, missing raise-from, and undefined names. Ruff config maps existing bandit skips to equivalent S-rules. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h --------- Co-authored-by: Claude <noreply@anthropic.com>
2026-03-11 12:23:35 -04:00
last_request_at: float | None = None
@property
def estimated_cost_sats(self) -> int:
"""Rough cost estimate in sats based on token usage."""
# ~$5/1M input tokens, ~$15/1M output tokens for Grok
# At ~$100k/BTC, 1 sat ≈ $0.001
input_cost = (self.total_prompt_tokens / 1_000_000) * 5
output_cost = (self.total_completion_tokens / 1_000_000) * 15
total_usd = input_cost + output_cost
return int(total_usd / 0.001) # Convert to sats
class GrokBackend:
"""xAI Grok backend — premium cloud augmentation for frontier reasoning.
Uses the OpenAI-compatible SDK to connect to xAI's API.
Only activated when GROK_ENABLED=true and XAI_API_KEY is set.
Exposes the same interface as Agno Agent:
run(message, stream) RunResult [dashboard]
print_response(message, stream) None [CLI]
health_check() dict [monitoring]
"""
def __init__(
self,
ruff (#169) * polish: streamline nav, extract inline styles, improve tablet UX - Restructure desktop nav from 8+ flat links + overflow dropdown into 5 grouped dropdowns (Core, Agents, Intel, System, More) matching the mobile menu structure to reduce decision fatigue - Extract all inline styles from mission_control.html and base.html notification elements into mission-control.css with semantic classes - Replace JS-built innerHTML with secure DOM construction in notification loader and chat history - Add CONNECTING state to connection indicator (amber) instead of showing OFFLINE before WebSocket connects - Add tablet breakpoint (1024px) with larger touch targets for Apple Pencil / stylus use and safe-area padding for iPad toolbar - Add active-link highlighting in desktop dropdown menus - Rename "Mission Control" page title to "System Overview" to disambiguate from the chat home page - Add "Home — Timmy Time" page title to index.html https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * fix(security): move auth-gate credentials to environment variables Hardcoded username, password, and HMAC secret in auth-gate.py replaced with os.environ lookups. Startup now refuses to run if any variable is unset. Added AUTH_GATE_SECRET/USER/PASS to .env.example. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * refactor(tooling): migrate from black+isort+bandit to ruff Replace three separate linting/formatting tools with a single ruff invocation. Updates tox.ini (lint, format, pre-push, pre-commit envs), .pre-commit-config.yaml, and CI workflow. Fixes all ruff errors including unused imports, missing raise-from, and undefined names. Ruff config maps existing bandit skips to equivalent S-rules. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h --------- Co-authored-by: Claude <noreply@anthropic.com>
2026-03-11 12:23:35 -04:00
api_key: str | None = None,
model: str | None = None,
) -> None:
from config import settings
self._api_key = api_key if api_key is not None else settings.xai_api_key
self._model = model or settings.grok_default_model
self._history: list[dict[str, str]] = []
self.stats = GrokUsageStats()
if not self._api_key:
logger.warning(
ruff (#169) * polish: streamline nav, extract inline styles, improve tablet UX - Restructure desktop nav from 8+ flat links + overflow dropdown into 5 grouped dropdowns (Core, Agents, Intel, System, More) matching the mobile menu structure to reduce decision fatigue - Extract all inline styles from mission_control.html and base.html notification elements into mission-control.css with semantic classes - Replace JS-built innerHTML with secure DOM construction in notification loader and chat history - Add CONNECTING state to connection indicator (amber) instead of showing OFFLINE before WebSocket connects - Add tablet breakpoint (1024px) with larger touch targets for Apple Pencil / stylus use and safe-area padding for iPad toolbar - Add active-link highlighting in desktop dropdown menus - Rename "Mission Control" page title to "System Overview" to disambiguate from the chat home page - Add "Home — Timmy Time" page title to index.html https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * fix(security): move auth-gate credentials to environment variables Hardcoded username, password, and HMAC secret in auth-gate.py replaced with os.environ lookups. Startup now refuses to run if any variable is unset. Added AUTH_GATE_SECRET/USER/PASS to .env.example. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * refactor(tooling): migrate from black+isort+bandit to ruff Replace three separate linting/formatting tools with a single ruff invocation. Updates tox.ini (lint, format, pre-push, pre-commit envs), .pre-commit-config.yaml, and CI workflow. Fixes all ruff errors including unused imports, missing raise-from, and undefined names. Ruff config maps existing bandit skips to equivalent S-rules. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h --------- Co-authored-by: Claude <noreply@anthropic.com>
2026-03-11 12:23:35 -04:00
"GrokBackend created without XAI_API_KEY — calls will fail until key is configured"
)
def _get_client(self):
"""Create OpenAI client configured for xAI endpoint."""
import httpx
from openai import OpenAI
from config import settings
return OpenAI(
api_key=self._api_key,
base_url=settings.xai_base_url,
timeout=httpx.Timeout(300.0),
)
async def _get_async_client(self):
"""Create async OpenAI client configured for xAI endpoint."""
import httpx
from openai import AsyncOpenAI
from config import settings
return AsyncOpenAI(
api_key=self._api_key,
base_url=settings.xai_base_url,
timeout=httpx.Timeout(300.0),
)
# ── Public interface (mirrors Agno Agent) ─────────────────────────────
def run(self, message: str, *, stream: bool = False) -> RunResult:
"""Synchronous inference via Grok API.
Args:
message: User prompt
stream: Accepted for API compat; Grok returns full response
Returns:
RunResult with response content
"""
if not self._api_key:
return RunResult(content="Grok is not configured. Set XAI_API_KEY to enable.")
start = time.time()
messages = self._build_messages(message)
try:
client = self._get_client()
response = client.chat.completions.create(
model=self._model,
messages=messages,
temperature=0.7,
)
content = response.choices[0].message.content or ""
latency_ms = (time.time() - start) * 1000
# Track usage
self.stats.total_requests += 1
self.stats.total_latency_ms += latency_ms
self.stats.last_request_at = time.time()
if response.usage:
self.stats.total_prompt_tokens += response.usage.prompt_tokens
self.stats.total_completion_tokens += response.usage.completion_tokens
# Update conversation history
self._history.append({"role": "user", "content": message})
self._history.append({"role": "assistant", "content": content})
# Keep last 10 turns
if len(self._history) > 20:
self._history = self._history[-20:]
logger.info(
"Grok response: %d tokens in %.0fms (model=%s)",
response.usage.completion_tokens if response.usage else 0,
latency_ms,
self._model,
)
return RunResult(content=content)
except Exception as exc:
self.stats.errors += 1
logger.error("Grok API error: %s", exc)
return RunResult(content=f"Grok temporarily unavailable: {exc}")
async def arun(self, message: str) -> RunResult:
"""Async inference via Grok API — used by cascade router and tools."""
if not self._api_key:
return RunResult(content="Grok is not configured. Set XAI_API_KEY to enable.")
start = time.time()
messages = self._build_messages(message)
try:
client = await self._get_async_client()
response = await client.chat.completions.create(
model=self._model,
messages=messages,
temperature=0.7,
)
content = response.choices[0].message.content or ""
latency_ms = (time.time() - start) * 1000
# Track usage
self.stats.total_requests += 1
self.stats.total_latency_ms += latency_ms
self.stats.last_request_at = time.time()
if response.usage:
self.stats.total_prompt_tokens += response.usage.prompt_tokens
self.stats.total_completion_tokens += response.usage.completion_tokens
# Update conversation history
self._history.append({"role": "user", "content": message})
self._history.append({"role": "assistant", "content": content})
if len(self._history) > 20:
self._history = self._history[-20:]
logger.info(
"Grok async response: %d tokens in %.0fms (model=%s)",
response.usage.completion_tokens if response.usage else 0,
latency_ms,
self._model,
)
return RunResult(content=content)
except Exception as exc:
self.stats.errors += 1
logger.error("Grok async API error: %s", exc)
return RunResult(content=f"Grok temporarily unavailable: {exc}")
def print_response(self, message: str, *, stream: bool = True) -> None:
"""Run inference and render the response to stdout (CLI interface)."""
result = self.run(message, stream=stream)
try:
from rich.console import Console
from rich.markdown import Markdown
Console().print(Markdown(result.content))
except ImportError:
print(result.content)
def health_check(self) -> dict:
"""Check Grok API connectivity and return status."""
if not self._api_key:
return {
"ok": False,
"error": "XAI_API_KEY not configured",
"backend": "grok",
"model": self._model,
}
try:
client = self._get_client()
# Lightweight check — list models
client.models.list()
return {
"ok": True,
"error": None,
"backend": "grok",
"model": self._model,
"stats": {
"total_requests": self.stats.total_requests,
"estimated_cost_sats": self.stats.estimated_cost_sats,
},
}
except Exception as exc:
logger.exception("Grok health check failed")
return {
"ok": False,
"error": str(exc),
"backend": "grok",
"model": self._model,
}
@property
def estimated_cost(self) -> int:
"""Return estimated cost in sats for all requests so far."""
return self.stats.estimated_cost_sats
# ── Private helpers ───────────────────────────────────────────────────
def _build_messages(self, message: str) -> list[dict[str, str]]:
"""Build the messages array for the API call."""
messages = [
{"role": "system", "content": get_system_prompt(tools_enabled=True, session_id="grok")}
]
# Include conversation history for context
messages.extend(self._history[-10:])
messages.append({"role": "user", "content": message})
return messages
# ── Module-level Grok singleton ─────────────────────────────────────────────
ruff (#169) * polish: streamline nav, extract inline styles, improve tablet UX - Restructure desktop nav from 8+ flat links + overflow dropdown into 5 grouped dropdowns (Core, Agents, Intel, System, More) matching the mobile menu structure to reduce decision fatigue - Extract all inline styles from mission_control.html and base.html notification elements into mission-control.css with semantic classes - Replace JS-built innerHTML with secure DOM construction in notification loader and chat history - Add CONNECTING state to connection indicator (amber) instead of showing OFFLINE before WebSocket connects - Add tablet breakpoint (1024px) with larger touch targets for Apple Pencil / stylus use and safe-area padding for iPad toolbar - Add active-link highlighting in desktop dropdown menus - Rename "Mission Control" page title to "System Overview" to disambiguate from the chat home page - Add "Home — Timmy Time" page title to index.html https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * fix(security): move auth-gate credentials to environment variables Hardcoded username, password, and HMAC secret in auth-gate.py replaced with os.environ lookups. Startup now refuses to run if any variable is unset. Added AUTH_GATE_SECRET/USER/PASS to .env.example. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * refactor(tooling): migrate from black+isort+bandit to ruff Replace three separate linting/formatting tools with a single ruff invocation. Updates tox.ini (lint, format, pre-push, pre-commit envs), .pre-commit-config.yaml, and CI workflow. Fixes all ruff errors including unused imports, missing raise-from, and undefined names. Ruff config maps existing bandit skips to equivalent S-rules. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h --------- Co-authored-by: Claude <noreply@anthropic.com>
2026-03-11 12:23:35 -04:00
_grok_backend: GrokBackend | None = None
def get_grok_backend() -> GrokBackend:
"""Get or create the Grok backend singleton."""
global _grok_backend
if _grok_backend is None:
_grok_backend = GrokBackend()
return _grok_backend
def grok_available() -> bool:
"""Return True when Grok is enabled and API key is configured."""
try:
from config import settings
return settings.grok_enabled and bool(settings.xai_api_key)
except Exception as exc:
logger.warning("Backend check failed (grok_available): %s", exc)
return False
# ── Claude (Anthropic) Backend ─────────────────────────────────────────────
# Lightweight cloud fallback — used when Ollama is offline and the user
# has set ANTHROPIC_API_KEY. Follows the same sovereign-first philosophy:
# never the default, only activated explicitly or as a last-resort fallback.
CLAUDE_MODELS: dict[str, str] = {
"haiku": "claude-haiku-4-5-20251001",
"sonnet": "claude-sonnet-4-20250514",
"opus": "claude-opus-4-20250514",
}
class ClaudeBackend:
"""Anthropic Claude backend — cloud fallback when local models are offline.
Uses the official Anthropic SDK. Same interface as GrokBackend:
run(message, stream) RunResult [dashboard]
print_response(message, stream) None [CLI]
health_check() dict [monitoring]
"""
def __init__(
self,
ruff (#169) * polish: streamline nav, extract inline styles, improve tablet UX - Restructure desktop nav from 8+ flat links + overflow dropdown into 5 grouped dropdowns (Core, Agents, Intel, System, More) matching the mobile menu structure to reduce decision fatigue - Extract all inline styles from mission_control.html and base.html notification elements into mission-control.css with semantic classes - Replace JS-built innerHTML with secure DOM construction in notification loader and chat history - Add CONNECTING state to connection indicator (amber) instead of showing OFFLINE before WebSocket connects - Add tablet breakpoint (1024px) with larger touch targets for Apple Pencil / stylus use and safe-area padding for iPad toolbar - Add active-link highlighting in desktop dropdown menus - Rename "Mission Control" page title to "System Overview" to disambiguate from the chat home page - Add "Home — Timmy Time" page title to index.html https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * fix(security): move auth-gate credentials to environment variables Hardcoded username, password, and HMAC secret in auth-gate.py replaced with os.environ lookups. Startup now refuses to run if any variable is unset. Added AUTH_GATE_SECRET/USER/PASS to .env.example. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * refactor(tooling): migrate from black+isort+bandit to ruff Replace three separate linting/formatting tools with a single ruff invocation. Updates tox.ini (lint, format, pre-push, pre-commit envs), .pre-commit-config.yaml, and CI workflow. Fixes all ruff errors including unused imports, missing raise-from, and undefined names. Ruff config maps existing bandit skips to equivalent S-rules. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h --------- Co-authored-by: Claude <noreply@anthropic.com>
2026-03-11 12:23:35 -04:00
api_key: str | None = None,
model: str | None = None,
) -> None:
from config import settings
self._api_key = api_key or settings.anthropic_api_key
raw_model = model or settings.claude_model
# Allow short names like "haiku" / "sonnet" / "opus"
self._model = CLAUDE_MODELS.get(raw_model, raw_model)
self._history: list[dict[str, str]] = []
if not self._api_key:
logger.warning(
"ClaudeBackend created without ANTHROPIC_API_KEY — "
"calls will fail until key is configured"
)
def _get_client(self):
"""Create Anthropic client."""
import anthropic
return anthropic.Anthropic(api_key=self._api_key)
# ── Public interface (mirrors Agno Agent) ─────────────────────────────
def run(self, message: str, *, stream: bool = False, **kwargs) -> RunResult:
"""Synchronous inference via Claude API."""
if not self._api_key:
return RunResult(content="Claude is not configured. Set ANTHROPIC_API_KEY to enable.")
start = time.time()
messages = self._build_messages(message)
try:
client = self._get_client()
response = client.messages.create(
model=self._model,
max_tokens=1024,
system=get_system_prompt(tools_enabled=True, session_id="claude"),
messages=messages,
)
content = response.content[0].text if response.content else ""
latency_ms = (time.time() - start) * 1000
# Update conversation history
self._history.append({"role": "user", "content": message})
self._history.append({"role": "assistant", "content": content})
if len(self._history) > 20:
self._history = self._history[-20:]
logger.info(
"Claude response: %d chars in %.0fms (model=%s)",
len(content),
latency_ms,
self._model,
)
return RunResult(content=content)
except Exception as exc:
logger.error("Claude API error: %s", exc)
return RunResult(content=f"Claude temporarily unavailable: {exc}")
def print_response(self, message: str, *, stream: bool = True) -> None:
"""Run inference and render the response to stdout (CLI interface)."""
result = self.run(message, stream=stream)
try:
from rich.console import Console
from rich.markdown import Markdown
Console().print(Markdown(result.content))
except ImportError:
print(result.content)
def health_check(self) -> dict:
"""Check Claude API connectivity."""
if not self._api_key:
return {
"ok": False,
"error": "ANTHROPIC_API_KEY not configured",
"backend": "claude",
"model": self._model,
}
try:
client = self._get_client()
# Lightweight ping — tiny completion
client.messages.create(
model=self._model,
max_tokens=4,
messages=[{"role": "user", "content": "ping"}],
)
return {"ok": True, "error": None, "backend": "claude", "model": self._model}
except Exception as exc:
logger.exception("Claude health check failed")
return {"ok": False, "error": str(exc), "backend": "claude", "model": self._model}
# ── Private helpers ───────────────────────────────────────────────────
def _build_messages(self, message: str) -> list[dict[str, str]]:
"""Build the messages array for the API call."""
messages = list(self._history[-10:])
messages.append({"role": "user", "content": message})
return messages
# ── Module-level Claude singleton ──────────────────────────────────────────
ruff (#169) * polish: streamline nav, extract inline styles, improve tablet UX - Restructure desktop nav from 8+ flat links + overflow dropdown into 5 grouped dropdowns (Core, Agents, Intel, System, More) matching the mobile menu structure to reduce decision fatigue - Extract all inline styles from mission_control.html and base.html notification elements into mission-control.css with semantic classes - Replace JS-built innerHTML with secure DOM construction in notification loader and chat history - Add CONNECTING state to connection indicator (amber) instead of showing OFFLINE before WebSocket connects - Add tablet breakpoint (1024px) with larger touch targets for Apple Pencil / stylus use and safe-area padding for iPad toolbar - Add active-link highlighting in desktop dropdown menus - Rename "Mission Control" page title to "System Overview" to disambiguate from the chat home page - Add "Home — Timmy Time" page title to index.html https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * fix(security): move auth-gate credentials to environment variables Hardcoded username, password, and HMAC secret in auth-gate.py replaced with os.environ lookups. Startup now refuses to run if any variable is unset. Added AUTH_GATE_SECRET/USER/PASS to .env.example. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h * refactor(tooling): migrate from black+isort+bandit to ruff Replace three separate linting/formatting tools with a single ruff invocation. Updates tox.ini (lint, format, pre-push, pre-commit envs), .pre-commit-config.yaml, and CI workflow. Fixes all ruff errors including unused imports, missing raise-from, and undefined names. Ruff config maps existing bandit skips to equivalent S-rules. https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h --------- Co-authored-by: Claude <noreply@anthropic.com>
2026-03-11 12:23:35 -04:00
_claude_backend: ClaudeBackend | None = None
def get_claude_backend() -> ClaudeBackend:
"""Get or create the Claude backend singleton."""
global _claude_backend
if _claude_backend is None:
_claude_backend = ClaudeBackend()
return _claude_backend
def claude_available() -> bool:
"""Return True when Anthropic API key is configured."""
try:
from config import settings
return bool(settings.anthropic_api_key)
except Exception as exc:
logger.warning("Backend check failed (claude_available): %s", exc)
return False