Spaces:
Paused
Paused
Upload 1579 files
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .dockerignore +15 -0
- .env.example +378 -0
- .envrc +1 -0
- .gitattributes +39 -35
- .gitignore +64 -0
- .gitmodules +3 -0
- .plans/openai-api-server.md +291 -0
- .plans/streaming-support.md +705 -0
- AGENTS.md +470 -0
- CONTRIBUTING.md +660 -0
- Dockerfile +29 -0
- LICENSE +21 -0
- MANIFEST.in +4 -0
- README.md +174 -7
- RELEASE_v0.2.0.md +383 -0
- RELEASE_v0.3.0.md +377 -0
- RELEASE_v0.4.0.md +400 -0
- RELEASE_v0.5.0.md +348 -0
- RELEASE_v0.6.0.md +249 -0
- RELEASE_v0.7.0.md +290 -0
- RELEASE_v0.8.0.md +346 -0
- acp_adapter/__init__.py +1 -0
- acp_adapter/__main__.py +5 -0
- acp_adapter/auth.py +24 -0
- acp_adapter/entry.py +85 -0
- acp_adapter/events.py +175 -0
- acp_adapter/permissions.py +77 -0
- acp_adapter/server.py +728 -0
- acp_adapter/session.py +475 -0
- acp_adapter/tools.py +214 -0
- acp_registry/agent.json +12 -0
- acp_registry/icon.svg +25 -0
- agent/__init__.py +6 -0
- agent/anthropic_adapter.py +1410 -0
- agent/auxiliary_client.py +0 -0
- agent/context_compressor.py +809 -0
- agent/context_engine.py +184 -0
- agent/context_references.py +520 -0
- agent/copilot_acp_client.py +570 -0
- agent/credential_pool.py +1319 -0
- agent/display.py +1043 -0
- agent/error_classifier.py +809 -0
- agent/insights.py +790 -0
- agent/manual_compression_feedback.py +49 -0
- agent/memory_manager.py +362 -0
- agent/memory_provider.py +231 -0
- agent/model_metadata.py +1085 -0
- agent/models_dev.py +678 -0
- agent/prompt_builder.py +987 -0
- agent/prompt_caching.py +72 -0
.dockerignore
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hermes Agent Environment Configuration
|
| 2 |
+
# Copy this file to .env and fill in your API keys
|
| 3 |
+
|
| 4 |
+
# =============================================================================
|
| 5 |
+
# LLM PROVIDER (OpenRouter)
|
| 6 |
+
# =============================================================================
|
| 7 |
+
# OpenRouter provides access to many models through one API
|
| 8 |
+
# All LLM calls go through OpenRouter - no direct provider keys needed
|
| 9 |
+
# Get your key at: https://openrouter.ai/keys
|
| 10 |
+
# OPENROUTER_API_KEY=
|
| 11 |
+
|
| 12 |
+
# Default model is configured in ~/.hermes/config.yaml (model.default).
|
| 13 |
+
# Use 'hermes model' or 'hermes setup' to change it.
|
| 14 |
+
# LLM_MODEL is no longer read from .env — this line is kept for reference only.
|
| 15 |
+
# LLM_MODEL=anthropic/claude-opus-4.6
|
| 16 |
+
|
| 17 |
+
# =============================================================================
|
| 18 |
+
# LLM PROVIDER (Google AI Studio / Gemini)
|
| 19 |
+
# =============================================================================
|
| 20 |
+
# Native Gemini API via Google's OpenAI-compatible endpoint.
|
| 21 |
+
# Get your key at: https://aistudio.google.com/app/apikey
|
| 22 |
+
# GOOGLE_API_KEY=your_google_ai_studio_key_here
|
| 23 |
+
# GEMINI_API_KEY=your_gemini_key_here # alias for GOOGLE_API_KEY
|
| 24 |
+
# Optional base URL override (default: Google's OpenAI-compatible endpoint)
|
| 25 |
+
# GEMINI_BASE_URL=https://generativelanguage.googleapis.com/v1beta/openai
|
| 26 |
+
|
| 27 |
+
# =============================================================================
|
| 28 |
+
# LLM PROVIDER (z.ai / GLM)
|
| 29 |
+
# =============================================================================
|
| 30 |
+
# z.ai provides access to ZhipuAI GLM models (GLM-4-Plus, etc.)
|
| 31 |
+
# Get your key at: https://z.ai or https://open.bigmodel.cn
|
| 32 |
+
# GLM_API_KEY=
|
| 33 |
+
# GLM_BASE_URL=https://api.z.ai/api/paas/v4 # Override default base URL
|
| 34 |
+
|
| 35 |
+
# =============================================================================
|
| 36 |
+
# LLM PROVIDER (Kimi / Moonshot)
|
| 37 |
+
# =============================================================================
|
| 38 |
+
# Kimi Code provides access to Moonshot AI coding models (kimi-k2.5, etc.)
|
| 39 |
+
# Get your key at: https://platform.kimi.ai (Kimi Code console)
|
| 40 |
+
# Keys prefixed sk-kimi- use the Kimi Code API (api.kimi.com) by default.
|
| 41 |
+
# Legacy keys from platform.moonshot.ai need KIMI_BASE_URL override below.
|
| 42 |
+
# KIMI_API_KEY=
|
| 43 |
+
# KIMI_BASE_URL=https://api.kimi.com/coding/v1 # Default for sk-kimi- keys
|
| 44 |
+
# KIMI_BASE_URL=https://api.moonshot.ai/v1 # For legacy Moonshot keys
|
| 45 |
+
# KIMI_BASE_URL=https://api.moonshot.cn/v1 # For Moonshot China keys
|
| 46 |
+
|
| 47 |
+
# =============================================================================
|
| 48 |
+
# LLM PROVIDER (MiniMax)
|
| 49 |
+
# =============================================================================
|
| 50 |
+
# MiniMax provides access to MiniMax models (global endpoint)
|
| 51 |
+
# Get your key at: https://www.minimax.io
|
| 52 |
+
# MINIMAX_API_KEY=
|
| 53 |
+
# MINIMAX_BASE_URL=https://api.minimax.io/v1 # Override default base URL
|
| 54 |
+
|
| 55 |
+
# MiniMax China endpoint (for users in mainland China)
|
| 56 |
+
# MINIMAX_CN_API_KEY=
|
| 57 |
+
# MINIMAX_CN_BASE_URL=https://api.minimaxi.com/v1 # Override default base URL
|
| 58 |
+
|
| 59 |
+
# =============================================================================
|
| 60 |
+
# LLM PROVIDER (OpenCode Zen)
|
| 61 |
+
# =============================================================================
|
| 62 |
+
# OpenCode Zen provides curated, tested models (GPT, Claude, Gemini, MiniMax, GLM, Kimi)
|
| 63 |
+
# Pay-as-you-go pricing. Get your key at: https://opencode.ai/auth
|
| 64 |
+
# OPENCODE_ZEN_API_KEY=
|
| 65 |
+
# OPENCODE_ZEN_BASE_URL=https://opencode.ai/zen/v1 # Override default base URL
|
| 66 |
+
|
| 67 |
+
# =============================================================================
|
| 68 |
+
# LLM PROVIDER (OpenCode Go)
|
| 69 |
+
# =============================================================================
|
| 70 |
+
# OpenCode Go provides access to open models (GLM-5, Kimi K2.5, MiniMax M2.5)
|
| 71 |
+
# $10/month subscription. Get your key at: https://opencode.ai/auth
|
| 72 |
+
# OPENCODE_GO_API_KEY=
|
| 73 |
+
|
| 74 |
+
# =============================================================================
|
| 75 |
+
# LLM PROVIDER (Hugging Face Inference Providers)
|
| 76 |
+
# =============================================================================
|
| 77 |
+
# Hugging Face routes to 20+ open models via unified OpenAI-compatible endpoint.
|
| 78 |
+
# Free tier included ($0.10/month), no markup on provider rates.
|
| 79 |
+
# Get your token at: https://huggingface.co/settings/tokens
|
| 80 |
+
# Required permission: "Make calls to Inference Providers"
|
| 81 |
+
# HF_TOKEN=
|
| 82 |
+
# OPENCODE_GO_BASE_URL=https://opencode.ai/zen/go/v1 # Override default base URL
|
| 83 |
+
|
| 84 |
+
# =============================================================================
|
| 85 |
+
# LLM PROVIDER (Qwen OAuth)
|
| 86 |
+
# =============================================================================
|
| 87 |
+
# Qwen OAuth reuses your local Qwen CLI login (qwen auth qwen-oauth).
|
| 88 |
+
# No API key needed — credentials come from ~/.qwen/oauth_creds.json.
|
| 89 |
+
# Optional base URL override:
|
| 90 |
+
# HERMES_QWEN_BASE_URL=https://portal.qwen.ai/v1
|
| 91 |
+
|
| 92 |
+
# =============================================================================
|
| 93 |
+
# LLM PROVIDER (Xiaomi MiMo)
|
| 94 |
+
# =============================================================================
|
| 95 |
+
# Xiaomi MiMo models (mimo-v2-pro, mimo-v2-omni, mimo-v2-flash).
|
| 96 |
+
# Get your key at: https://platform.xiaomimimo.com
|
| 97 |
+
# XIAOMI_API_KEY=your_key_here
|
| 98 |
+
# Optional base URL override:
|
| 99 |
+
# XIAOMI_BASE_URL=https://api.xiaomimimo.com/v1
|
| 100 |
+
|
| 101 |
+
# =============================================================================
|
| 102 |
+
# TOOL API KEYS
|
| 103 |
+
# =============================================================================
|
| 104 |
+
|
| 105 |
+
# Exa API Key - AI-native web search and contents
|
| 106 |
+
# Get at: https://exa.ai
|
| 107 |
+
# EXA_API_KEY=
|
| 108 |
+
|
| 109 |
+
# Parallel API Key - AI-native web search and extract
|
| 110 |
+
# Get at: https://parallel.ai
|
| 111 |
+
# PARALLEL_API_KEY=
|
| 112 |
+
|
| 113 |
+
# Firecrawl API Key - Web search, extract, and crawl
|
| 114 |
+
# Get at: https://firecrawl.dev/
|
| 115 |
+
# FIRECRAWL_API_KEY=
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# FAL.ai API Key - Image generation
|
| 119 |
+
# Get at: https://fal.ai/
|
| 120 |
+
# FAL_KEY=
|
| 121 |
+
|
| 122 |
+
# Honcho - Cross-session AI-native user modeling (optional)
|
| 123 |
+
# Builds a persistent understanding of the user across sessions and tools.
|
| 124 |
+
# Get at: https://app.honcho.dev
|
| 125 |
+
# Also requires ~/.honcho/config.json with enabled=true (see README).
|
| 126 |
+
# HONCHO_API_KEY=
|
| 127 |
+
|
| 128 |
+
# =============================================================================
|
| 129 |
+
# TERMINAL TOOL CONFIGURATION
|
| 130 |
+
# =============================================================================
|
| 131 |
+
# Backend type: "local", "singularity", "docker", "modal", or "ssh"
|
| 132 |
+
# Terminal backend is configured in ~/.hermes/config.yaml (terminal.backend).
|
| 133 |
+
# Use 'hermes setup' or 'hermes config set terminal.backend docker' to change.
|
| 134 |
+
# Supported: local, docker, singularity, modal, ssh
|
| 135 |
+
#
|
| 136 |
+
# Only override here if you need to force a backend without touching config.yaml:
|
| 137 |
+
# TERMINAL_ENV=local
|
| 138 |
+
|
| 139 |
+
# Container images (for singularity/docker/modal backends)
|
| 140 |
+
# TERMINAL_DOCKER_IMAGE=nikolaik/python-nodejs:python3.11-nodejs20
|
| 141 |
+
# TERMINAL_SINGULARITY_IMAGE=docker://nikolaik/python-nodejs:python3.11-nodejs20
|
| 142 |
+
TERMINAL_MODAL_IMAGE=nikolaik/python-nodejs:python3.11-nodejs20
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# Working directory for terminal commands
|
| 146 |
+
# For local backend: "." means current directory (resolved automatically)
|
| 147 |
+
# For remote backends (ssh/docker/modal/singularity): use an absolute path
|
| 148 |
+
# INSIDE the target environment, or leave unset for the backend's default
|
| 149 |
+
# (/root for modal, / for docker, ~ for ssh). Do NOT use a host-local path.
|
| 150 |
+
# Usually managed by config.yaml (terminal.cwd) — uncomment to override
|
| 151 |
+
# TERMINAL_CWD=.
|
| 152 |
+
|
| 153 |
+
# Default command timeout in seconds
|
| 154 |
+
TERMINAL_TIMEOUT=60
|
| 155 |
+
|
| 156 |
+
# Cleanup inactive environments after this many seconds
|
| 157 |
+
TERMINAL_LIFETIME_SECONDS=300
|
| 158 |
+
|
| 159 |
+
# =============================================================================
|
| 160 |
+
# SSH REMOTE EXECUTION (for TERMINAL_ENV=ssh)
|
| 161 |
+
# =============================================================================
|
| 162 |
+
# Run terminal commands on a remote server via SSH.
|
| 163 |
+
# Agent code stays on your machine, commands execute remotely.
|
| 164 |
+
#
|
| 165 |
+
# SECURITY BENEFITS:
|
| 166 |
+
# - Agent cannot read your .env file (API keys protected)
|
| 167 |
+
# - Agent cannot modify its own code
|
| 168 |
+
# - Remote server acts as isolated sandbox
|
| 169 |
+
# - Can safely configure passwordless sudo on remote
|
| 170 |
+
#
|
| 171 |
+
# TERMINAL_SSH_HOST=192.168.1.100
|
| 172 |
+
# TERMINAL_SSH_USER=agent
|
| 173 |
+
# TERMINAL_SSH_PORT=22
|
| 174 |
+
# TERMINAL_SSH_KEY=~/.ssh/id_rsa
|
| 175 |
+
|
| 176 |
+
# =============================================================================
|
| 177 |
+
# SUDO SUPPORT (works with ALL terminal backends)
|
| 178 |
+
# =============================================================================
|
| 179 |
+
# If set, enables sudo commands by piping password via `sudo -S`.
|
| 180 |
+
# Works with: local, docker, singularity, modal, and ssh backends.
|
| 181 |
+
#
|
| 182 |
+
# SECURITY WARNING: Password stored in plaintext. Only use on trusted machines.
|
| 183 |
+
#
|
| 184 |
+
# ALTERNATIVES:
|
| 185 |
+
# - For SSH backend: Configure passwordless sudo on the remote server
|
| 186 |
+
# - For containers: Run as root inside the container (no sudo needed)
|
| 187 |
+
# - For local: Configure /etc/sudoers for specific commands
|
| 188 |
+
# - For CLI: Leave unset - you'll be prompted interactively with 45s timeout
|
| 189 |
+
#
|
| 190 |
+
# SUDO_PASSWORD=your_password_here
|
| 191 |
+
|
| 192 |
+
# =============================================================================
|
| 193 |
+
# MODAL CLOUD BACKEND (Optional - for TERMINAL_ENV=modal)
|
| 194 |
+
# =============================================================================
|
| 195 |
+
# Modal uses CLI authentication, not environment variables.
|
| 196 |
+
# Run: pip install modal && modal setup
|
| 197 |
+
# This will authenticate via browser and store credentials locally.
|
| 198 |
+
# No API key needed in .env - Modal handles auth automatically.
|
| 199 |
+
|
| 200 |
+
# =============================================================================
|
| 201 |
+
# BROWSER TOOL CONFIGURATION (agent-browser + Browserbase)
|
| 202 |
+
# =============================================================================
|
| 203 |
+
# Browser automation requires Browserbase cloud service for remote browser execution.
|
| 204 |
+
# This allows the agent to navigate websites, fill forms, and extract information.
|
| 205 |
+
#
|
| 206 |
+
# STEALTH MODES:
|
| 207 |
+
# - Basic Stealth: ALWAYS active (random fingerprints, auto CAPTCHA solving)
|
| 208 |
+
# - Advanced Stealth: Requires BROWSERBASE_ADVANCED_STEALTH=true (Scale Plan only)
|
| 209 |
+
|
| 210 |
+
# Browserbase API Key - Cloud browser execution
|
| 211 |
+
# Get at: https://browserbase.com/
|
| 212 |
+
# BROWSERBASE_API_KEY=
|
| 213 |
+
|
| 214 |
+
# Browserbase Project ID - From your Browserbase dashboard
|
| 215 |
+
# BROWSERBASE_PROJECT_ID=
|
| 216 |
+
|
| 217 |
+
# Enable residential proxies for better CAPTCHA solving (default: true)
|
| 218 |
+
# Routes traffic through residential IPs, significantly improves success rate
|
| 219 |
+
BROWSERBASE_PROXIES=true
|
| 220 |
+
|
| 221 |
+
# Enable advanced stealth mode (default: false, requires Scale Plan)
|
| 222 |
+
# Uses custom Chromium build to avoid bot detection altogether
|
| 223 |
+
BROWSERBASE_ADVANCED_STEALTH=false
|
| 224 |
+
|
| 225 |
+
# Browser session timeout in seconds (default: 300)
|
| 226 |
+
# Sessions are cleaned up after this duration of inactivity
|
| 227 |
+
BROWSER_SESSION_TIMEOUT=300
|
| 228 |
+
|
| 229 |
+
# Browser inactivity timeout - auto-cleanup inactive sessions (default: 120 = 2 min)
|
| 230 |
+
# Browser sessions are automatically closed after this period of no activity
|
| 231 |
+
BROWSER_INACTIVITY_TIMEOUT=120
|
| 232 |
+
|
| 233 |
+
# =============================================================================
|
| 234 |
+
# SESSION LOGGING
|
| 235 |
+
# =============================================================================
|
| 236 |
+
# Session trajectories are automatically saved to logs/ directory
|
| 237 |
+
# Format: logs/session_YYYYMMDD_HHMMSS_UUID.json
|
| 238 |
+
# Contains full conversation history in trajectory format for debugging/replay
|
| 239 |
+
|
| 240 |
+
# =============================================================================
|
| 241 |
+
# VOICE TRANSCRIPTION & OPENAI TTS
|
| 242 |
+
# =============================================================================
|
| 243 |
+
# Required for voice message transcription (Whisper) and OpenAI TTS voices.
|
| 244 |
+
# Uses OpenAI's API directly (not via OpenRouter).
|
| 245 |
+
# Named VOICE_TOOLS_OPENAI_KEY to avoid interference with OpenRouter.
|
| 246 |
+
# Get at: https://platform.openai.com/api-keys
|
| 247 |
+
# VOICE_TOOLS_OPENAI_KEY=
|
| 248 |
+
|
| 249 |
+
# =============================================================================
|
| 250 |
+
# SLACK INTEGRATION
|
| 251 |
+
# =============================================================================
|
| 252 |
+
# Slack Bot Token - From Slack App settings (OAuth & Permissions)
|
| 253 |
+
# Get at: https://api.slack.com/apps
|
| 254 |
+
# SLACK_BOT_TOKEN=xoxb-...
|
| 255 |
+
|
| 256 |
+
# Slack App Token - For Socket Mode (App-Level Tokens in Slack App settings)
|
| 257 |
+
# SLACK_APP_TOKEN=xapp-...
|
| 258 |
+
|
| 259 |
+
# Slack allowed users (comma-separated Slack user IDs)
|
| 260 |
+
# SLACK_ALLOWED_USERS=
|
| 261 |
+
|
| 262 |
+
# =============================================================================
|
| 263 |
+
# TELEGRAM INTEGRATION
|
| 264 |
+
# =============================================================================
|
| 265 |
+
# Telegram Bot Token - From @BotFather (https://t.me/BotFather)
|
| 266 |
+
# TELEGRAM_BOT_TOKEN=
|
| 267 |
+
# TELEGRAM_ALLOWED_USERS= # Comma-separated user IDs
|
| 268 |
+
# TELEGRAM_HOME_CHANNEL= # Default chat for cron delivery
|
| 269 |
+
# TELEGRAM_HOME_CHANNEL_NAME= # Display name for home channel
|
| 270 |
+
|
| 271 |
+
# Webhook mode (optional — for cloud deployments like Fly.io/Railway)
|
| 272 |
+
# Default is long polling. Setting TELEGRAM_WEBHOOK_URL switches to webhook mode.
|
| 273 |
+
# TELEGRAM_WEBHOOK_URL=https://my-app.fly.dev/telegram
|
| 274 |
+
# TELEGRAM_WEBHOOK_PORT=8443
|
| 275 |
+
# TELEGRAM_WEBHOOK_SECRET= # Recommended for production
|
| 276 |
+
|
| 277 |
+
# WhatsApp (built-in Baileys bridge — run `hermes whatsapp` to pair)
|
| 278 |
+
# WHATSAPP_ENABLED=false
|
| 279 |
+
# WHATSAPP_ALLOWED_USERS=15551234567
|
| 280 |
+
|
| 281 |
+
# Email (IMAP/SMTP — send and receive emails as Hermes)
|
| 282 |
+
# For Gmail: enable 2FA → create App Password at https://myaccount.google.com/apppasswords
|
| 283 |
+
# EMAIL_ADDRESS=hermes@gmail.com
|
| 284 |
+
# EMAIL_PASSWORD=xxxx xxxx xxxx xxxx
|
| 285 |
+
# EMAIL_IMAP_HOST=imap.gmail.com
|
| 286 |
+
# EMAIL_IMAP_PORT=993
|
| 287 |
+
# EMAIL_SMTP_HOST=smtp.gmail.com
|
| 288 |
+
# EMAIL_SMTP_PORT=587
|
| 289 |
+
# EMAIL_POLL_INTERVAL=15
|
| 290 |
+
# EMAIL_ALLOWED_USERS=your@email.com
|
| 291 |
+
# EMAIL_HOME_ADDRESS=your@email.com
|
| 292 |
+
|
| 293 |
+
# Gateway-wide: allow ALL users without an allowlist (default: false = deny)
|
| 294 |
+
# Only set to true if you intentionally want open access.
|
| 295 |
+
# GATEWAY_ALLOW_ALL_USERS=false
|
| 296 |
+
|
| 297 |
+
# =============================================================================
|
| 298 |
+
# RESPONSE PACING
|
| 299 |
+
# =============================================================================
|
| 300 |
+
# Human-like delays between message chunks on messaging platforms.
|
| 301 |
+
# Makes the bot feel less robotic.
|
| 302 |
+
# HERMES_HUMAN_DELAY_MODE=off # off | natural | custom
|
| 303 |
+
# HERMES_HUMAN_DELAY_MIN_MS=800 # Min delay in ms (custom mode)
|
| 304 |
+
# HERMES_HUMAN_DELAY_MAX_MS=2500 # Max delay in ms (custom mode)
|
| 305 |
+
|
| 306 |
+
# =============================================================================
|
| 307 |
+
# DEBUG OPTIONS
|
| 308 |
+
# =============================================================================
|
| 309 |
+
WEB_TOOLS_DEBUG=false
|
| 310 |
+
VISION_TOOLS_DEBUG=false
|
| 311 |
+
MOA_TOOLS_DEBUG=false
|
| 312 |
+
IMAGE_TOOLS_DEBUG=false
|
| 313 |
+
|
| 314 |
+
# =============================================================================
|
| 315 |
+
# CONTEXT COMPRESSION (Auto-shrinks long conversations)
|
| 316 |
+
# =============================================================================
|
| 317 |
+
# When conversation approaches model's context limit, middle turns are
|
| 318 |
+
# automatically summarized to free up space.
|
| 319 |
+
#
|
| 320 |
+
# Context compression is configured in ~/.hermes/config.yaml under compression:
|
| 321 |
+
# CONTEXT_COMPRESSION_ENABLED=true # Enable auto-compression (default: true)
|
| 322 |
+
# CONTEXT_COMPRESSION_THRESHOLD=0.85 # Compress at 85% of context limit
|
| 323 |
+
# Model is set via compression.summary_model in config.yaml (default: google/gemini-3-flash-preview)
|
| 324 |
+
|
| 325 |
+
# =============================================================================
|
| 326 |
+
# RL TRAINING (Tinker + Atropos)
|
| 327 |
+
# =============================================================================
|
| 328 |
+
# Run reinforcement learning training on language models using the Tinker API.
|
| 329 |
+
# Requires the rl-server to be running (from tinker-atropos package).
|
| 330 |
+
|
| 331 |
+
# Tinker API Key - RL training service
|
| 332 |
+
# Get at: https://tinker-console.thinkingmachines.ai/keys
|
| 333 |
+
# TINKER_API_KEY=
|
| 334 |
+
|
| 335 |
+
# Weights & Biases API Key - Experiment tracking and metrics
|
| 336 |
+
# Get at: https://wandb.ai/authorize
|
| 337 |
+
# WANDB_API_KEY=
|
| 338 |
+
|
| 339 |
+
# RL API Server URL (default: http://localhost:8080)
|
| 340 |
+
# Change if running the rl-server on a different host/port
|
| 341 |
+
# RL_API_URL=http://localhost:8080
|
| 342 |
+
|
| 343 |
+
# =============================================================================
|
| 344 |
+
# SKILLS HUB (GitHub integration for skill search/install/publish)
|
| 345 |
+
# =============================================================================
|
| 346 |
+
|
| 347 |
+
# GitHub Personal Access Token — for higher API rate limits on skill search/install
|
| 348 |
+
# Get at: https://github.com/settings/tokens (Fine-grained recommended)
|
| 349 |
+
# GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxx
|
| 350 |
+
|
| 351 |
+
# GitHub App credentials (optional — for bot identity on PRs)
|
| 352 |
+
# GITHUB_APP_ID=
|
| 353 |
+
# GITHUB_APP_PRIVATE_KEY_PATH=
|
| 354 |
+
# GITHUB_APP_INSTALLATION_ID=
|
| 355 |
+
|
| 356 |
+
# Groq API key (free tier — used for Whisper STT in voice mode)
|
| 357 |
+
# GROQ_API_KEY=
|
| 358 |
+
|
| 359 |
+
# =============================================================================
|
| 360 |
+
# STT PROVIDER SELECTION
|
| 361 |
+
# =============================================================================
|
| 362 |
+
# Default STT provider is "local" (faster-whisper) — runs on your machine, no API key needed.
|
| 363 |
+
# Install with: pip install faster-whisper
|
| 364 |
+
# Model downloads automatically on first use (~150 MB for "base").
|
| 365 |
+
# To use cloud providers instead, set GROQ_API_KEY or VOICE_TOOLS_OPENAI_KEY above.
|
| 366 |
+
# Provider priority: local > groq > openai
|
| 367 |
+
# Configure in config.yaml: stt.provider: local | groq | openai
|
| 368 |
+
|
| 369 |
+
# =============================================================================
|
| 370 |
+
# STT ADVANCED OVERRIDES (optional)
|
| 371 |
+
# =============================================================================
|
| 372 |
+
# Override default STT models per provider (normally set via stt.model in config.yaml)
|
| 373 |
+
# STT_GROQ_MODEL=whisper-large-v3-turbo
|
| 374 |
+
# STT_OPENAI_MODEL=whisper-1
|
| 375 |
+
|
| 376 |
+
# Override STT provider endpoints (for proxies or self-hosted instances)
|
| 377 |
+
# GROQ_BASE_URL=https://api.groq.com/openai/v1
|
| 378 |
+
# STT_OPENAI_BASE_URL=https://api.openai.com/v1
|
.envrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
use flake
|
.gitattributes
CHANGED
|
@@ -1,35 +1,39 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.pdf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
*.wav filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
*.mp3 filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/venv/
|
| 2 |
+
/_pycache/
|
| 3 |
+
*.pyc*
|
| 4 |
+
__pycache__/
|
| 5 |
+
.venv/
|
| 6 |
+
.vscode/
|
| 7 |
+
.env
|
| 8 |
+
.env.local
|
| 9 |
+
.env.development.local
|
| 10 |
+
.env.test.local
|
| 11 |
+
.env.production.local
|
| 12 |
+
.env.development
|
| 13 |
+
.env.test
|
| 14 |
+
export*
|
| 15 |
+
__pycache__/model_tools.cpython-310.pyc
|
| 16 |
+
__pycache__/web_tools.cpython-310.pyc
|
| 17 |
+
logs/
|
| 18 |
+
data/
|
| 19 |
+
.pytest_cache/
|
| 20 |
+
tmp/
|
| 21 |
+
temp_vision_images/
|
| 22 |
+
hermes-*/*
|
| 23 |
+
examples/
|
| 24 |
+
tests/quick_test_dataset.jsonl
|
| 25 |
+
tests/sample_dataset.jsonl
|
| 26 |
+
run_datagen_kimik2-thinking.sh
|
| 27 |
+
run_datagen_megascience_glm4-6.sh
|
| 28 |
+
run_datagen_sonnet.sh
|
| 29 |
+
source-data/*
|
| 30 |
+
run_datagen_megascience_glm4-6.sh
|
| 31 |
+
data/*
|
| 32 |
+
node_modules/
|
| 33 |
+
browser-use/
|
| 34 |
+
agent-browser/
|
| 35 |
+
# Private keys
|
| 36 |
+
*.ppk
|
| 37 |
+
*.pem
|
| 38 |
+
privvy*
|
| 39 |
+
images/
|
| 40 |
+
__pycache__/
|
| 41 |
+
hermes_agent.egg-info/
|
| 42 |
+
wandb/
|
| 43 |
+
testlogs
|
| 44 |
+
|
| 45 |
+
# CLI config (may contain sensitive SSH paths)
|
| 46 |
+
cli-config.yaml
|
| 47 |
+
|
| 48 |
+
# Skills Hub state (lives in ~/.hermes/skills/.hub/ at runtime, but just in case)
|
| 49 |
+
skills/.hub/
|
| 50 |
+
ignored/
|
| 51 |
+
.worktrees/
|
| 52 |
+
environments/benchmarks/evals/
|
| 53 |
+
|
| 54 |
+
# Release script temp files
|
| 55 |
+
.release_notes.md
|
| 56 |
+
mini-swe-agent/
|
| 57 |
+
|
| 58 |
+
# Nix
|
| 59 |
+
.direnv/
|
| 60 |
+
result
|
| 61 |
+
"# Binary files"
|
| 62 |
+
"*.png"
|
| 63 |
+
"*.pdf"
|
| 64 |
+
"*.wav"
|
.gitmodules
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OpenAI-Compatible API Server for Hermes Agent
|
| 2 |
+
|
| 3 |
+
## Motivation
|
| 4 |
+
|
| 5 |
+
Every major chat frontend (Open WebUI 126k★, LobeChat 73k★, LibreChat 34k★,
|
| 6 |
+
AnythingLLM 56k★, NextChat 87k★, ChatBox 39k★, Jan 26k★, HF Chat-UI 8k★,
|
| 7 |
+
big-AGI 7k★) connects to backends via the OpenAI-compatible REST API with
|
| 8 |
+
SSE streaming. By exposing this endpoint, hermes-agent becomes instantly
|
| 9 |
+
usable as a backend for all of them — no custom adapters needed.
|
| 10 |
+
|
| 11 |
+
## What It Enables
|
| 12 |
+
|
| 13 |
+
```
|
| 14 |
+
┌──────────────────┐
|
| 15 |
+
│ Open WebUI │──┐
|
| 16 |
+
│ LobeChat │ │ POST /v1/chat/completions
|
| 17 |
+
│ LibreChat │ ├──► Authorization: Bearer <key> ┌─────────────────┐
|
| 18 |
+
│ AnythingLLM │ │ {"messages": [...]} │ hermes-agent │
|
| 19 |
+
│ NextChat │ │ │ gateway │
|
| 20 |
+
│ Any OAI client │──┘ ◄── SSE streaming response │ (API server) │
|
| 21 |
+
└──────────────────┘ └─────────────────┘
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
A user would:
|
| 25 |
+
1. Set `API_SERVER_ENABLED=true` in `~/.hermes/.env`
|
| 26 |
+
2. Run `hermes gateway` (API server starts alongside Telegram/Discord/etc.)
|
| 27 |
+
3. Point Open WebUI (or any frontend) at `http://localhost:8642/v1`
|
| 28 |
+
4. Chat with hermes-agent through any OpenAI-compatible UI
|
| 29 |
+
|
| 30 |
+
## Endpoints
|
| 31 |
+
|
| 32 |
+
| Method | Path | Purpose |
|
| 33 |
+
|--------|------|---------|
|
| 34 |
+
| POST | `/v1/chat/completions` | Chat with the agent (streaming + non-streaming) |
|
| 35 |
+
| GET | `/v1/models` | List available "models" (returns hermes-agent as a model) |
|
| 36 |
+
| GET | `/health` | Health check |
|
| 37 |
+
|
| 38 |
+
## Architecture
|
| 39 |
+
|
| 40 |
+
### Option A: Gateway Platform Adapter (recommended)
|
| 41 |
+
|
| 42 |
+
Create `gateway/platforms/api_server.py` as a new platform adapter that
|
| 43 |
+
extends `BasePlatformAdapter`. This is the cleanest approach because:
|
| 44 |
+
|
| 45 |
+
- Reuses all gateway infrastructure (session management, auth, context building)
|
| 46 |
+
- Runs in the same async loop as other adapters
|
| 47 |
+
- Gets message handling, interrupt support, and session persistence for free
|
| 48 |
+
- Follows the established pattern (like Telegram, Discord, etc.)
|
| 49 |
+
- Uses `aiohttp.web` (already a dependency) for the HTTP server
|
| 50 |
+
|
| 51 |
+
The adapter would start an `aiohttp.web.Application` server in `connect()`
|
| 52 |
+
and route incoming HTTP requests through the standard `handle_message()` pipeline.
|
| 53 |
+
|
| 54 |
+
### Option B: Standalone Component
|
| 55 |
+
|
| 56 |
+
A separate HTTP server class in `gateway/api_server.py` that creates its own
|
| 57 |
+
AIAgent instances directly. Simpler but duplicates session/auth logic.
|
| 58 |
+
|
| 59 |
+
**Recommendation: Option A** — fits the existing architecture, less code to
|
| 60 |
+
maintain, gets all gateway features for free.
|
| 61 |
+
|
| 62 |
+
## Request/Response Format
|
| 63 |
+
|
| 64 |
+
### Chat Completions (non-streaming)
|
| 65 |
+
|
| 66 |
+
```
|
| 67 |
+
POST /v1/chat/completions
|
| 68 |
+
Authorization: Bearer hermes-api-key-here
|
| 69 |
+
Content-Type: application/json
|
| 70 |
+
|
| 71 |
+
{
|
| 72 |
+
"model": "hermes-agent",
|
| 73 |
+
"messages": [
|
| 74 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 75 |
+
{"role": "user", "content": "What files are in the current directory?"}
|
| 76 |
+
],
|
| 77 |
+
"stream": false,
|
| 78 |
+
"temperature": 0.7
|
| 79 |
+
}
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
Response:
|
| 83 |
+
```json
|
| 84 |
+
{
|
| 85 |
+
"id": "chatcmpl-abc123",
|
| 86 |
+
"object": "chat.completion",
|
| 87 |
+
"created": 1710000000,
|
| 88 |
+
"model": "hermes-agent",
|
| 89 |
+
"choices": [{
|
| 90 |
+
"index": 0,
|
| 91 |
+
"message": {
|
| 92 |
+
"role": "assistant",
|
| 93 |
+
"content": "Here are the files in the current directory:\n..."
|
| 94 |
+
},
|
| 95 |
+
"finish_reason": "stop"
|
| 96 |
+
}],
|
| 97 |
+
"usage": {
|
| 98 |
+
"prompt_tokens": 50,
|
| 99 |
+
"completion_tokens": 200,
|
| 100 |
+
"total_tokens": 250
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
### Chat Completions (streaming)
|
| 106 |
+
|
| 107 |
+
Same request with `"stream": true`. Response is SSE:
|
| 108 |
+
|
| 109 |
+
```
|
| 110 |
+
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
|
| 111 |
+
|
| 112 |
+
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Here "},"finish_reason":null}]}
|
| 113 |
+
|
| 114 |
+
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"are "},"finish_reason":null}]}
|
| 115 |
+
|
| 116 |
+
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
|
| 117 |
+
|
| 118 |
+
data: [DONE]
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
### Models List
|
| 122 |
+
|
| 123 |
+
```
|
| 124 |
+
GET /v1/models
|
| 125 |
+
Authorization: Bearer hermes-api-key-here
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
Response:
|
| 129 |
+
```json
|
| 130 |
+
{
|
| 131 |
+
"object": "list",
|
| 132 |
+
"data": [{
|
| 133 |
+
"id": "hermes-agent",
|
| 134 |
+
"object": "model",
|
| 135 |
+
"created": 1710000000,
|
| 136 |
+
"owned_by": "hermes-agent"
|
| 137 |
+
}]
|
| 138 |
+
}
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
## Key Design Decisions
|
| 142 |
+
|
| 143 |
+
### 1. Session Management
|
| 144 |
+
|
| 145 |
+
The OpenAI API is stateless — each request includes the full conversation.
|
| 146 |
+
But hermes-agent sessions have persistent state (memory, skills, tool context).
|
| 147 |
+
|
| 148 |
+
**Approach: Hybrid**
|
| 149 |
+
- Default: Stateless. Each request is independent. The `messages` array IS
|
| 150 |
+
the conversation. No session persistence between requests.
|
| 151 |
+
- Opt-in persistent sessions via `X-Session-ID` header. When provided, the
|
| 152 |
+
server maintains session state across requests (conversation history,
|
| 153 |
+
memory context, tool state). This enables richer agent behavior.
|
| 154 |
+
- The session ID also enables interrupt support — a subsequent request with
|
| 155 |
+
the same session ID while one is running triggers an interrupt.
|
| 156 |
+
|
| 157 |
+
### 2. Streaming
|
| 158 |
+
|
| 159 |
+
The agent's `run_conversation()` is synchronous and returns the full response.
|
| 160 |
+
For real SSE streaming, we need to emit chunks as they're generated.
|
| 161 |
+
|
| 162 |
+
**Phase 1 (MVP):** Run agent in a thread, return the complete response as
|
| 163 |
+
a single SSE chunk + `[DONE]`. This works with all frontends — they just see
|
| 164 |
+
a fast single-chunk response. Not true streaming but functional.
|
| 165 |
+
|
| 166 |
+
**Phase 2:** Add a response callback to AIAgent that emits text chunks as the
|
| 167 |
+
LLM generates them. The API server captures these via a queue and streams them
|
| 168 |
+
as SSE events. This gives real token-by-token streaming.
|
| 169 |
+
|
| 170 |
+
**Phase 3:** Stream tool execution progress too — emit tool call/result events
|
| 171 |
+
as the agent works, giving frontends visibility into what the agent is doing.
|
| 172 |
+
|
| 173 |
+
### 3. Tool Transparency
|
| 174 |
+
|
| 175 |
+
Two modes:
|
| 176 |
+
- **Opaque (default):** Frontends see only the final response. Tool calls
|
| 177 |
+
happen server-side and are invisible. Best for general-purpose UIs.
|
| 178 |
+
- **Transparent (opt-in via header):** Tool calls are emitted as OpenAI-format
|
| 179 |
+
tool_call/tool_result messages in the stream. Useful for agent-aware frontends.
|
| 180 |
+
|
| 181 |
+
### 4. Authentication
|
| 182 |
+
|
| 183 |
+
- Bearer token via `Authorization: Bearer <key>` header
|
| 184 |
+
- Token configured via `API_SERVER_KEY` env var
|
| 185 |
+
- Optional: allow unauthenticated local-only access (127.0.0.1 bind)
|
| 186 |
+
- Follows the same pattern as other platform adapters
|
| 187 |
+
|
| 188 |
+
### 5. Model Mapping
|
| 189 |
+
|
| 190 |
+
Frontends send `"model": "hermes-agent"` (or whatever). The actual LLM model
|
| 191 |
+
used is configured server-side in config.yaml. The API server maps any
|
| 192 |
+
requested model name to the configured hermes-agent model.
|
| 193 |
+
|
| 194 |
+
Optionally, allow model passthrough: if the frontend sends
|
| 195 |
+
`"model": "anthropic/claude-sonnet-4"`, the agent uses that model. Controlled
|
| 196 |
+
by a config flag.
|
| 197 |
+
|
| 198 |
+
## Configuration
|
| 199 |
+
|
| 200 |
+
```yaml
|
| 201 |
+
# In config.yaml
|
| 202 |
+
api_server:
|
| 203 |
+
enabled: true
|
| 204 |
+
port: 8642
|
| 205 |
+
host: "127.0.0.1" # localhost only by default
|
| 206 |
+
key: "your-secret-key" # or via API_SERVER_KEY env var
|
| 207 |
+
allow_model_override: false # let clients choose the model
|
| 208 |
+
max_concurrent: 5 # max simultaneous requests
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
Environment variables:
|
| 212 |
+
```bash
|
| 213 |
+
API_SERVER_ENABLED=true
|
| 214 |
+
API_SERVER_PORT=8642
|
| 215 |
+
API_SERVER_HOST=127.0.0.1
|
| 216 |
+
API_SERVER_KEY=your-secret-key
|
| 217 |
+
```
|
| 218 |
+
|
| 219 |
+
## Implementation Plan
|
| 220 |
+
|
| 221 |
+
### Phase 1: MVP (non-streaming) — PR
|
| 222 |
+
|
| 223 |
+
1. `gateway/platforms/api_server.py` — new adapter
|
| 224 |
+
- aiohttp.web server with endpoints:
|
| 225 |
+
- `POST /v1/chat/completions` — Chat Completions API (universal compat)
|
| 226 |
+
- `POST /v1/responses` — Responses API (server-side state, tool preservation)
|
| 227 |
+
- `GET /v1/models` — list available models
|
| 228 |
+
- `GET /health` — health check
|
| 229 |
+
- Bearer token auth middleware
|
| 230 |
+
- Non-streaming responses (run agent, return full result)
|
| 231 |
+
- Chat Completions: stateless, messages array is the conversation
|
| 232 |
+
- Responses API: server-side conversation storage via previous_response_id
|
| 233 |
+
- Store full internal conversation (including tool calls) keyed by response ID
|
| 234 |
+
- On subsequent requests, reconstruct full context from stored chain
|
| 235 |
+
- Frontend system prompt layered on top of hermes-agent's core prompt
|
| 236 |
+
|
| 237 |
+
2. `gateway/config.py` — add `Platform.API_SERVER` enum + config
|
| 238 |
+
|
| 239 |
+
3. `gateway/run.py` — register adapter in `_create_adapter()`
|
| 240 |
+
|
| 241 |
+
4. Tests in `tests/gateway/test_api_server.py`
|
| 242 |
+
|
| 243 |
+
### Phase 2: SSE Streaming
|
| 244 |
+
|
| 245 |
+
1. Add response streaming to both endpoints
|
| 246 |
+
- Chat Completions: `choices[0].delta.content` SSE format
|
| 247 |
+
- Responses API: semantic events (response.output_text.delta, etc.)
|
| 248 |
+
- Run agent in thread, collect output via callback queue
|
| 249 |
+
- Handle client disconnect (cancel agent)
|
| 250 |
+
|
| 251 |
+
2. Add `stream_callback` parameter to `AIAgent.run_conversation()`
|
| 252 |
+
|
| 253 |
+
### Phase 3: Enhanced Features
|
| 254 |
+
|
| 255 |
+
1. Tool call transparency mode (opt-in)
|
| 256 |
+
2. Model passthrough/override
|
| 257 |
+
3. Concurrent request limiting
|
| 258 |
+
4. Usage tracking / rate limiting
|
| 259 |
+
5. CORS headers for browser-based frontends
|
| 260 |
+
6. GET /v1/responses/{id} — retrieve stored response
|
| 261 |
+
7. DELETE /v1/responses/{id} — delete stored response
|
| 262 |
+
|
| 263 |
+
## Files Changed
|
| 264 |
+
|
| 265 |
+
| File | Change |
|
| 266 |
+
|------|--------|
|
| 267 |
+
| `gateway/platforms/api_server.py` | NEW — main adapter (~300 lines) |
|
| 268 |
+
| `gateway/config.py` | Add Platform.API_SERVER + config (~20 lines) |
|
| 269 |
+
| `gateway/run.py` | Register adapter in _create_adapter() (~10 lines) |
|
| 270 |
+
| `tests/gateway/test_api_server.py` | NEW — tests (~200 lines) |
|
| 271 |
+
| `cli-config.yaml.example` | Add api_server section |
|
| 272 |
+
| `README.md` | Mention API server in platform list |
|
| 273 |
+
|
| 274 |
+
## Compatibility Matrix
|
| 275 |
+
|
| 276 |
+
Once implemented, hermes-agent works as a drop-in backend for:
|
| 277 |
+
|
| 278 |
+
| Frontend | Stars | How to Connect |
|
| 279 |
+
|----------|-------|---------------|
|
| 280 |
+
| Open WebUI | 126k | Settings → Connections → Add OpenAI API, URL: `http://localhost:8642/v1` |
|
| 281 |
+
| NextChat | 87k | BASE_URL env var |
|
| 282 |
+
| LobeChat | 73k | Custom provider endpoint |
|
| 283 |
+
| AnythingLLM | 56k | LLM Provider → Generic OpenAI |
|
| 284 |
+
| Oobabooga | 42k | Already a backend, not a frontend |
|
| 285 |
+
| ChatBox | 39k | API Host setting |
|
| 286 |
+
| LibreChat | 34k | librechat.yaml custom endpoint |
|
| 287 |
+
| Chatbot UI | 29k | Custom API endpoint |
|
| 288 |
+
| Jan | 26k | Remote model config |
|
| 289 |
+
| AionUI | 18k | Custom API endpoint |
|
| 290 |
+
| HF Chat-UI | 8k | OPENAI_BASE_URL env var |
|
| 291 |
+
| big-AGI | 7k | Custom endpoint |
|
.plans/streaming-support.md
ADDED
|
@@ -0,0 +1,705 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Streaming LLM Response Support for Hermes Agent
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
|
| 5 |
+
Add token-by-token streaming of LLM responses across all platforms. When enabled,
|
| 6 |
+
users see the response typing out live instead of waiting for the full generation.
|
| 7 |
+
Streaming is opt-in via config, defaults to off, and all existing non-streaming
|
| 8 |
+
code paths remain intact as the default.
|
| 9 |
+
|
| 10 |
+
## Design Principles
|
| 11 |
+
|
| 12 |
+
1. **Feature-flagged**: `streaming.enabled: true` in config.yaml. Off by default.
|
| 13 |
+
When off, all existing code paths are unchanged — zero risk to current behavior.
|
| 14 |
+
2. **Callback-based**: A simple `stream_callback(text_delta: str)` function injected
|
| 15 |
+
into AIAgent. The agent doesn't know or care what the consumer does with tokens.
|
| 16 |
+
3. **Graceful degradation**: If the provider doesn't support streaming, or streaming
|
| 17 |
+
fails for any reason, silently fall back to the non-streaming path.
|
| 18 |
+
4. **Platform-agnostic core**: The streaming mechanism in AIAgent works the same
|
| 19 |
+
regardless of whether the consumer is CLI, Telegram, Discord, or the API server.
|
| 20 |
+
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
## Architecture
|
| 24 |
+
|
| 25 |
+
```
|
| 26 |
+
stream_callback(delta)
|
| 27 |
+
│
|
| 28 |
+
┌─────────────┐ ┌─────────────▼──────────────┐
|
| 29 |
+
│ LLM API │ │ queue.Queue() │
|
| 30 |
+
│ (stream) │───►│ thread-safe bridge between │
|
| 31 |
+
│ │ │ agent thread & consumer │
|
| 32 |
+
└─────────────┘ └─────────────┬──────────────┘
|
| 33 |
+
│
|
| 34 |
+
┌──────────────┼──────────────┐
|
| 35 |
+
│ │ │
|
| 36 |
+
┌─────▼─────┐ ┌─────▼─────┐ ┌─────▼─────┐
|
| 37 |
+
│ CLI │ │ Gateway │ │ API Server│
|
| 38 |
+
│ print to │ │ edit msg │ │ SSE event │
|
| 39 |
+
│ terminal │ │ on Tg/Dc │ │ to client │
|
| 40 |
+
└───────────┘ └───────────┘ └───────────┘
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
The agent runs in a thread. The callback puts tokens into a thread-safe queue.
|
| 44 |
+
Each consumer reads the queue in its own context (async task, main thread, etc.).
|
| 45 |
+
|
| 46 |
+
---
|
| 47 |
+
|
| 48 |
+
## Configuration
|
| 49 |
+
|
| 50 |
+
### config.yaml
|
| 51 |
+
|
| 52 |
+
```yaml
|
| 53 |
+
streaming:
|
| 54 |
+
enabled: false # Master switch. Default off.
|
| 55 |
+
# Per-platform overrides (optional):
|
| 56 |
+
# cli: true # Override for CLI only
|
| 57 |
+
# telegram: true # Override for Telegram only
|
| 58 |
+
# discord: false # Keep Discord non-streaming
|
| 59 |
+
# api_server: true # Override for API server
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
### Environment variables
|
| 63 |
+
|
| 64 |
+
```
|
| 65 |
+
HERMES_STREAMING_ENABLED=true # Master switch via env
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
### How the flag is read
|
| 69 |
+
|
| 70 |
+
- **CLI**: `load_cli_config()` reads `streaming.enabled`, sets env var. AIAgent
|
| 71 |
+
checks at init time.
|
| 72 |
+
- **Gateway**: `_run_agent()` reads config, decides whether to pass
|
| 73 |
+
`stream_callback` to the AIAgent constructor.
|
| 74 |
+
- **API server**: For Chat Completions `stream=true` requests, always uses streaming
|
| 75 |
+
regardless of config (the client is explicitly requesting it). For non-stream
|
| 76 |
+
requests, uses config.
|
| 77 |
+
|
| 78 |
+
### Precedence
|
| 79 |
+
|
| 80 |
+
1. API server: client's `stream` field overrides everything
|
| 81 |
+
2. Per-platform config override (e.g., `streaming.telegram: true`)
|
| 82 |
+
3. Master `streaming.enabled` flag
|
| 83 |
+
4. Default: off
|
| 84 |
+
|
| 85 |
+
---
|
| 86 |
+
|
| 87 |
+
## Implementation Plan
|
| 88 |
+
|
| 89 |
+
### Phase 1: Core streaming infrastructure in AIAgent
|
| 90 |
+
|
| 91 |
+
**File: run_agent.py**
|
| 92 |
+
|
| 93 |
+
#### 1a. Add stream_callback parameter to __init__ (~5 lines)
|
| 94 |
+
|
| 95 |
+
```python
|
| 96 |
+
def __init__(self, ..., stream_callback: callable = None, ...):
|
| 97 |
+
self.stream_callback = stream_callback
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
No other init changes. The callback is optional — when None, everything
|
| 101 |
+
works exactly as before.
|
| 102 |
+
|
| 103 |
+
#### 1b. Add _run_streaming_chat_completion() method (~65 lines)
|
| 104 |
+
|
| 105 |
+
New method for Chat Completions API streaming:
|
| 106 |
+
|
| 107 |
+
```python
|
| 108 |
+
def _run_streaming_chat_completion(self, api_kwargs: dict):
|
| 109 |
+
"""Stream a chat completion, emitting text tokens via stream_callback.
|
| 110 |
+
|
| 111 |
+
Returns a fake response object compatible with the non-streaming code path.
|
| 112 |
+
Falls back to non-streaming on any error.
|
| 113 |
+
"""
|
| 114 |
+
stream_kwargs = dict(api_kwargs)
|
| 115 |
+
stream_kwargs["stream"] = True
|
| 116 |
+
stream_kwargs["stream_options"] = {"include_usage": True}
|
| 117 |
+
|
| 118 |
+
accumulated_content = []
|
| 119 |
+
accumulated_tool_calls = {} # index -> {id, name, arguments}
|
| 120 |
+
final_usage = None
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
stream = self.client.chat.completions.create(**stream_kwargs)
|
| 124 |
+
|
| 125 |
+
for chunk in stream:
|
| 126 |
+
if not chunk.choices:
|
| 127 |
+
# Usage-only chunk (final)
|
| 128 |
+
if chunk.usage:
|
| 129 |
+
final_usage = chunk.usage
|
| 130 |
+
continue
|
| 131 |
+
|
| 132 |
+
delta = chunk.choices[0].delta
|
| 133 |
+
|
| 134 |
+
# Text content — emit via callback
|
| 135 |
+
if delta.content:
|
| 136 |
+
accumulated_content.append(delta.content)
|
| 137 |
+
if self.stream_callback:
|
| 138 |
+
try:
|
| 139 |
+
self.stream_callback(delta.content)
|
| 140 |
+
except Exception:
|
| 141 |
+
pass
|
| 142 |
+
|
| 143 |
+
# Tool call deltas — accumulate silently
|
| 144 |
+
if delta.tool_calls:
|
| 145 |
+
for tc_delta in delta.tool_calls:
|
| 146 |
+
idx = tc_delta.index
|
| 147 |
+
if idx not in accumulated_tool_calls:
|
| 148 |
+
accumulated_tool_calls[idx] = {
|
| 149 |
+
"id": tc_delta.id or "",
|
| 150 |
+
"name": "", "arguments": ""
|
| 151 |
+
}
|
| 152 |
+
if tc_delta.function:
|
| 153 |
+
if tc_delta.function.name:
|
| 154 |
+
accumulated_tool_calls[idx]["name"] = tc_delta.function.name
|
| 155 |
+
if tc_delta.function.arguments:
|
| 156 |
+
accumulated_tool_calls[idx]["arguments"] += tc_delta.function.arguments
|
| 157 |
+
|
| 158 |
+
# Build fake response compatible with existing code
|
| 159 |
+
tool_calls = []
|
| 160 |
+
for idx in sorted(accumulated_tool_calls):
|
| 161 |
+
tc = accumulated_tool_calls[idx]
|
| 162 |
+
if tc["name"]:
|
| 163 |
+
tool_calls.append(SimpleNamespace(
|
| 164 |
+
id=tc["id"], type="function",
|
| 165 |
+
function=SimpleNamespace(name=tc["name"], arguments=tc["arguments"]),
|
| 166 |
+
))
|
| 167 |
+
|
| 168 |
+
return SimpleNamespace(
|
| 169 |
+
choices=[SimpleNamespace(
|
| 170 |
+
message=SimpleNamespace(
|
| 171 |
+
content="".join(accumulated_content) or "",
|
| 172 |
+
tool_calls=tool_calls or None,
|
| 173 |
+
role="assistant",
|
| 174 |
+
),
|
| 175 |
+
finish_reason="tool_calls" if tool_calls else "stop",
|
| 176 |
+
)],
|
| 177 |
+
usage=final_usage,
|
| 178 |
+
model=self.model,
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
except Exception as e:
|
| 182 |
+
logger.debug("Streaming failed, falling back to non-streaming: %s", e)
|
| 183 |
+
return self.client.chat.completions.create(**api_kwargs)
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
#### 1c. Modify _run_codex_stream() for Responses API (~10 lines)
|
| 187 |
+
|
| 188 |
+
The method already iterates the stream. Add callback emission:
|
| 189 |
+
|
| 190 |
+
```python
|
| 191 |
+
def _run_codex_stream(self, api_kwargs: dict):
|
| 192 |
+
with self.client.responses.stream(**api_kwargs) as stream:
|
| 193 |
+
for event in stream:
|
| 194 |
+
# Emit text deltas if streaming callback is set
|
| 195 |
+
if self.stream_callback and hasattr(event, 'type'):
|
| 196 |
+
if event.type == 'response.output_text.delta':
|
| 197 |
+
try:
|
| 198 |
+
self.stream_callback(event.delta)
|
| 199 |
+
except Exception:
|
| 200 |
+
pass
|
| 201 |
+
return stream.get_final_response()
|
| 202 |
+
```
|
| 203 |
+
|
| 204 |
+
#### 1d. Modify _interruptible_api_call() (~5 lines)
|
| 205 |
+
|
| 206 |
+
Add the streaming branch:
|
| 207 |
+
|
| 208 |
+
```python
|
| 209 |
+
def _call():
|
| 210 |
+
try:
|
| 211 |
+
if self.api_mode == "codex_responses":
|
| 212 |
+
result["response"] = self._run_codex_stream(api_kwargs)
|
| 213 |
+
elif self.stream_callback is not None:
|
| 214 |
+
result["response"] = self._run_streaming_chat_completion(api_kwargs)
|
| 215 |
+
else:
|
| 216 |
+
result["response"] = self.client.chat.completions.create(**api_kwargs)
|
| 217 |
+
except Exception as e:
|
| 218 |
+
result["error"] = e
|
| 219 |
+
```
|
| 220 |
+
|
| 221 |
+
#### 1e. Signal end-of-stream to consumers (~5 lines)
|
| 222 |
+
|
| 223 |
+
After the API call returns, signal the callback that streaming is done
|
| 224 |
+
so consumers can finalize (remove cursor, close SSE, etc.):
|
| 225 |
+
|
| 226 |
+
```python
|
| 227 |
+
# In run_conversation(), after _interruptible_api_call returns:
|
| 228 |
+
if self.stream_callback:
|
| 229 |
+
try:
|
| 230 |
+
self.stream_callback(None) # None = end of stream signal
|
| 231 |
+
except Exception:
|
| 232 |
+
pass
|
| 233 |
+
```
|
| 234 |
+
|
| 235 |
+
Consumers check: `if delta is None: finalize()`
|
| 236 |
+
|
| 237 |
+
**Tests for Phase 1:** (~150 lines)
|
| 238 |
+
- Test _run_streaming_chat_completion with mocked stream
|
| 239 |
+
- Test fallback to non-streaming on error
|
| 240 |
+
- Test tool_call accumulation during streaming
|
| 241 |
+
- Test stream_callback receives correct deltas
|
| 242 |
+
- Test None signal at end of stream
|
| 243 |
+
- Test streaming disabled when callback is None
|
| 244 |
+
|
| 245 |
+
---
|
| 246 |
+
|
| 247 |
+
### Phase 2: Gateway consumers (Telegram, Discord, etc.)
|
| 248 |
+
|
| 249 |
+
**File: gateway/run.py**
|
| 250 |
+
|
| 251 |
+
#### 2a. Read streaming config (~15 lines)
|
| 252 |
+
|
| 253 |
+
In `_run_agent()`, before creating the AIAgent:
|
| 254 |
+
|
| 255 |
+
```python
|
| 256 |
+
# Read streaming config
|
| 257 |
+
_streaming_enabled = False
|
| 258 |
+
try:
|
| 259 |
+
# Check per-platform override first
|
| 260 |
+
platform_key = source.platform.value if source.platform else ""
|
| 261 |
+
_stream_cfg = {} # loaded from config.yaml streaming section
|
| 262 |
+
if _stream_cfg.get(platform_key) is not None:
|
| 263 |
+
_streaming_enabled = bool(_stream_cfg[platform_key])
|
| 264 |
+
else:
|
| 265 |
+
_streaming_enabled = bool(_stream_cfg.get("enabled", False))
|
| 266 |
+
except Exception:
|
| 267 |
+
pass
|
| 268 |
+
# Env var override
|
| 269 |
+
if os.getenv("HERMES_STREAMING_ENABLED", "").lower() in ("true", "1", "yes"):
|
| 270 |
+
_streaming_enabled = True
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
#### 2b. Set up queue + callback (~15 lines)
|
| 274 |
+
|
| 275 |
+
```python
|
| 276 |
+
_stream_q = None
|
| 277 |
+
_stream_done = None
|
| 278 |
+
_stream_msg_id = [None] # mutable ref for the async task
|
| 279 |
+
|
| 280 |
+
if _streaming_enabled:
|
| 281 |
+
import queue as _q
|
| 282 |
+
_stream_q = _q.Queue()
|
| 283 |
+
_stream_done = threading.Event()
|
| 284 |
+
|
| 285 |
+
def _on_token(delta):
|
| 286 |
+
if delta is None:
|
| 287 |
+
_stream_done.set()
|
| 288 |
+
else:
|
| 289 |
+
_stream_q.put(delta)
|
| 290 |
+
```
|
| 291 |
+
|
| 292 |
+
Pass `stream_callback=_on_token` to the AIAgent constructor.
|
| 293 |
+
|
| 294 |
+
#### 2c. Telegram/Discord stream preview task (~50 lines)
|
| 295 |
+
|
| 296 |
+
```python
|
| 297 |
+
async def stream_preview():
|
| 298 |
+
"""Progressively edit a message with streaming tokens."""
|
| 299 |
+
if not _stream_q:
|
| 300 |
+
return
|
| 301 |
+
adapter = self.adapters.get(source.platform)
|
| 302 |
+
if not adapter:
|
| 303 |
+
return
|
| 304 |
+
|
| 305 |
+
accumulated = []
|
| 306 |
+
token_count = 0
|
| 307 |
+
last_edit = 0.0
|
| 308 |
+
MIN_TOKENS = 20 # Don't show until enough context
|
| 309 |
+
EDIT_INTERVAL = 1.5 # Respect Telegram rate limits
|
| 310 |
+
|
| 311 |
+
try:
|
| 312 |
+
while not _stream_done.is_set():
|
| 313 |
+
try:
|
| 314 |
+
chunk = _stream_q.get(timeout=0.1)
|
| 315 |
+
accumulated.append(chunk)
|
| 316 |
+
token_count += 1
|
| 317 |
+
except queue.Empty:
|
| 318 |
+
continue
|
| 319 |
+
|
| 320 |
+
now = time.monotonic()
|
| 321 |
+
if token_count >= MIN_TOKENS and (now - last_edit) >= EDIT_INTERVAL:
|
| 322 |
+
preview = "".join(accumulated) + " ▌"
|
| 323 |
+
if _stream_msg_id[0] is None:
|
| 324 |
+
r = await adapter.send(
|
| 325 |
+
chat_id=source.chat_id,
|
| 326 |
+
content=preview,
|
| 327 |
+
metadata=_thread_metadata,
|
| 328 |
+
)
|
| 329 |
+
if r.success and r.message_id:
|
| 330 |
+
_stream_msg_id[0] = r.message_id
|
| 331 |
+
else:
|
| 332 |
+
await adapter.edit_message(
|
| 333 |
+
chat_id=source.chat_id,
|
| 334 |
+
message_id=_stream_msg_id[0],
|
| 335 |
+
content=preview,
|
| 336 |
+
)
|
| 337 |
+
last_edit = now
|
| 338 |
+
|
| 339 |
+
# Drain remaining tokens
|
| 340 |
+
while not _stream_q.empty():
|
| 341 |
+
accumulated.append(_stream_q.get_nowait())
|
| 342 |
+
|
| 343 |
+
# Final edit — remove cursor, show complete text
|
| 344 |
+
if _stream_msg_id[0] and accumulated:
|
| 345 |
+
await adapter.edit_message(
|
| 346 |
+
chat_id=source.chat_id,
|
| 347 |
+
message_id=_stream_msg_id[0],
|
| 348 |
+
content="".join(accumulated),
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
except asyncio.CancelledError:
|
| 352 |
+
# Clean up on cancel
|
| 353 |
+
if _stream_msg_id[0] and accumulated:
|
| 354 |
+
try:
|
| 355 |
+
await adapter.edit_message(
|
| 356 |
+
chat_id=source.chat_id,
|
| 357 |
+
message_id=_stream_msg_id[0],
|
| 358 |
+
content="".join(accumulated),
|
| 359 |
+
)
|
| 360 |
+
except Exception:
|
| 361 |
+
pass
|
| 362 |
+
except Exception as e:
|
| 363 |
+
logger.debug("stream_preview error: %s", e)
|
| 364 |
+
```
|
| 365 |
+
|
| 366 |
+
#### 2d. Skip final send if already streamed (~10 lines)
|
| 367 |
+
|
| 368 |
+
In `_process_message_background()` (base.py), after getting the response,
|
| 369 |
+
if streaming was active and `_stream_msg_id[0]` is set, the final response
|
| 370 |
+
was already delivered via progressive edits. Skip the normal `self.send()`
|
| 371 |
+
call to avoid duplicating the message.
|
| 372 |
+
|
| 373 |
+
This is the most delicate integration point — we need to communicate from
|
| 374 |
+
the gateway's `_run_agent` back to the base adapter's response sender that
|
| 375 |
+
the response was already delivered. Options:
|
| 376 |
+
|
| 377 |
+
- **Option A**: Return a special marker in the result dict:
|
| 378 |
+
`result["_streamed_msg_id"] = _stream_msg_id[0]`
|
| 379 |
+
The base adapter checks this and skips `send()`.
|
| 380 |
+
|
| 381 |
+
- **Option B**: Edit the already-sent message with the final response
|
| 382 |
+
(which may differ slightly from accumulated tokens due to think-block
|
| 383 |
+
stripping, etc.) and don't send a new one.
|
| 384 |
+
|
| 385 |
+
- **Option C**: The stream preview task handles the FULL final response
|
| 386 |
+
(including any post-processing), and the handler returns None to skip
|
| 387 |
+
the normal send path.
|
| 388 |
+
|
| 389 |
+
Recommended: **Option A** — cleanest separation. The result dict already
|
| 390 |
+
carries metadata; adding one more field is low-risk.
|
| 391 |
+
|
| 392 |
+
**Platform-specific considerations:**
|
| 393 |
+
|
| 394 |
+
| Platform | Edit support | Rate limits | Streaming approach |
|
| 395 |
+
|----------|-------------|-------------|-------------------|
|
| 396 |
+
| Telegram | ✅ edit_message_text | ~20 edits/min | Edit every 1.5s |
|
| 397 |
+
| Discord | ✅ message.edit | 5 edits/5s per message | Edit every 1.2s |
|
| 398 |
+
| Slack | ✅ chat.update | Tier 3 (~50/min) | Edit every 1.5s |
|
| 399 |
+
| WhatsApp | ❌ no edit support | N/A | Skip streaming, use normal path |
|
| 400 |
+
| HomeAssistant | ❌ no edit | N/A | Skip streaming |
|
| 401 |
+
| API Server | ✅ SSE native | No limit | Real SSE events |
|
| 402 |
+
|
| 403 |
+
WhatsApp and HomeAssistant fall back to non-streaming automatically because
|
| 404 |
+
they don't support message editing.
|
| 405 |
+
|
| 406 |
+
**Tests for Phase 2:** (~100 lines)
|
| 407 |
+
- Test stream_preview sends/edits correctly
|
| 408 |
+
- Test skip-final-send when streaming delivered
|
| 409 |
+
- Test WhatsApp/HA graceful fallback
|
| 410 |
+
- Test streaming disabled per-platform config
|
| 411 |
+
- Test thread_id metadata forwarded in stream messages
|
| 412 |
+
|
| 413 |
+
---
|
| 414 |
+
|
| 415 |
+
### Phase 3: CLI streaming
|
| 416 |
+
|
| 417 |
+
**File: cli.py**
|
| 418 |
+
|
| 419 |
+
#### 3a. Set up callback in the CLI chat loop (~20 lines)
|
| 420 |
+
|
| 421 |
+
In `_chat_once()` or wherever the agent is invoked:
|
| 422 |
+
|
| 423 |
+
```python
|
| 424 |
+
if streaming_enabled:
|
| 425 |
+
_stream_q = queue.Queue()
|
| 426 |
+
_stream_done = threading.Event()
|
| 427 |
+
|
| 428 |
+
def _cli_stream_callback(delta):
|
| 429 |
+
if delta is None:
|
| 430 |
+
_stream_done.set()
|
| 431 |
+
else:
|
| 432 |
+
_stream_q.put(delta)
|
| 433 |
+
|
| 434 |
+
agent.stream_callback = _cli_stream_callback
|
| 435 |
+
```
|
| 436 |
+
|
| 437 |
+
#### 3b. Token display thread/task (~30 lines)
|
| 438 |
+
|
| 439 |
+
Start a thread that reads the queue and prints tokens:
|
| 440 |
+
|
| 441 |
+
```python
|
| 442 |
+
def _stream_display():
|
| 443 |
+
"""Print tokens to terminal as they arrive."""
|
| 444 |
+
first_token = True
|
| 445 |
+
while not _stream_done.is_set():
|
| 446 |
+
try:
|
| 447 |
+
delta = _stream_q.get(timeout=0.1)
|
| 448 |
+
except queue.Empty:
|
| 449 |
+
continue
|
| 450 |
+
if first_token:
|
| 451 |
+
# Print response box top border
|
| 452 |
+
_cprint(f"\n{top}")
|
| 453 |
+
first_token = False
|
| 454 |
+
sys.stdout.write(delta)
|
| 455 |
+
sys.stdout.flush()
|
| 456 |
+
# Drain remaining
|
| 457 |
+
while not _stream_q.empty():
|
| 458 |
+
sys.stdout.write(_stream_q.get_nowait())
|
| 459 |
+
sys.stdout.flush()
|
| 460 |
+
# Print bottom border
|
| 461 |
+
_cprint(f"\n\n{bot}")
|
| 462 |
+
```
|
| 463 |
+
|
| 464 |
+
**Integration challenge: prompt_toolkit**
|
| 465 |
+
|
| 466 |
+
The CLI uses prompt_toolkit which controls the terminal. Writing directly
|
| 467 |
+
to stdout while prompt_toolkit is active can cause display corruption.
|
| 468 |
+
The existing KawaiiSpinner already solves this by using prompt_toolkit's
|
| 469 |
+
`patch_stdout` context. The streaming display would need to do the same.
|
| 470 |
+
|
| 471 |
+
Alternative: use `_cprint()` for each token chunk (routes through
|
| 472 |
+
prompt_toolkit's renderer). But this might be slow for individual tokens.
|
| 473 |
+
|
| 474 |
+
Recommended approach: accumulate tokens in small batches (e.g., every 50ms)
|
| 475 |
+
and `_cprint()` the batch. This balances display responsiveness with
|
| 476 |
+
prompt_toolkit compatibility.
|
| 477 |
+
|
| 478 |
+
**Tests for Phase 3:** (~50 lines)
|
| 479 |
+
- Test CLI streaming callback setup
|
| 480 |
+
- Test response box borders with streaming
|
| 481 |
+
- Test fallback when streaming disabled
|
| 482 |
+
|
| 483 |
+
---
|
| 484 |
+
|
| 485 |
+
### Phase 4: API Server real streaming
|
| 486 |
+
|
| 487 |
+
**File: gateway/platforms/api_server.py**
|
| 488 |
+
|
| 489 |
+
Replace the pseudo-streaming `_write_sse_chat_completion()` with real
|
| 490 |
+
token-by-token SSE when the agent supports it.
|
| 491 |
+
|
| 492 |
+
#### 4a. Wire streaming callback for stream=true requests (~20 lines)
|
| 493 |
+
|
| 494 |
+
```python
|
| 495 |
+
if stream:
|
| 496 |
+
_stream_q = queue.Queue()
|
| 497 |
+
|
| 498 |
+
def _api_stream_callback(delta):
|
| 499 |
+
_stream_q.put(delta) # None = done
|
| 500 |
+
|
| 501 |
+
# Pass callback to _run_agent
|
| 502 |
+
result, usage = await self._run_agent(
|
| 503 |
+
..., stream_callback=_api_stream_callback,
|
| 504 |
+
)
|
| 505 |
+
```
|
| 506 |
+
|
| 507 |
+
#### 4b. Real SSE writer (~40 lines)
|
| 508 |
+
|
| 509 |
+
```python
|
| 510 |
+
async def _write_real_sse(self, request, completion_id, model, stream_q):
|
| 511 |
+
response = web.StreamResponse(
|
| 512 |
+
headers={"Content-Type": "text/event-stream", "Cache-Control": "no-cache"},
|
| 513 |
+
)
|
| 514 |
+
await response.prepare(request)
|
| 515 |
+
|
| 516 |
+
# Role chunk
|
| 517 |
+
await response.write(...)
|
| 518 |
+
|
| 519 |
+
# Stream content chunks as they arrive
|
| 520 |
+
while True:
|
| 521 |
+
try:
|
| 522 |
+
delta = await asyncio.get_event_loop().run_in_executor(
|
| 523 |
+
None, lambda: stream_q.get(timeout=0.1)
|
| 524 |
+
)
|
| 525 |
+
except queue.Empty:
|
| 526 |
+
continue
|
| 527 |
+
|
| 528 |
+
if delta is None: # End of stream
|
| 529 |
+
break
|
| 530 |
+
|
| 531 |
+
chunk = {"id": completion_id, "object": "chat.completion.chunk", ...
|
| 532 |
+
"choices": [{"delta": {"content": delta}, ...}]}
|
| 533 |
+
await response.write(f"data: {json.dumps(chunk)}\n\n".encode())
|
| 534 |
+
|
| 535 |
+
# Finish + [DONE]
|
| 536 |
+
await response.write(...)
|
| 537 |
+
await response.write(b"data: [DONE]\n\n")
|
| 538 |
+
return response
|
| 539 |
+
```
|
| 540 |
+
|
| 541 |
+
**Challenge: concurrent execution**
|
| 542 |
+
|
| 543 |
+
The agent runs in a thread executor. SSE writing happens in the async event
|
| 544 |
+
loop. The queue bridges them. But `_run_agent()` currently awaits the full
|
| 545 |
+
result before returning. For real streaming, we need to start the agent in
|
| 546 |
+
the background and stream tokens while it runs:
|
| 547 |
+
|
| 548 |
+
```python
|
| 549 |
+
# Start agent in background
|
| 550 |
+
agent_task = asyncio.create_task(self._run_agent_async(...))
|
| 551 |
+
|
| 552 |
+
# Stream tokens while agent runs
|
| 553 |
+
await self._write_real_sse(request, ..., stream_q)
|
| 554 |
+
|
| 555 |
+
# Agent is done by now (stream_q received None)
|
| 556 |
+
result, usage = await agent_task
|
| 557 |
+
```
|
| 558 |
+
|
| 559 |
+
This requires splitting `_run_agent` into an async version that doesn't
|
| 560 |
+
block waiting for the result, or running it in a separate task.
|
| 561 |
+
|
| 562 |
+
**Responses API SSE format:**
|
| 563 |
+
|
| 564 |
+
For `/v1/responses` with `stream=true`, the SSE events are different:
|
| 565 |
+
|
| 566 |
+
```
|
| 567 |
+
event: response.output_text.delta
|
| 568 |
+
data: {"type":"response.output_text.delta","delta":"Hello"}
|
| 569 |
+
|
| 570 |
+
event: response.completed
|
| 571 |
+
data: {"type":"response.completed","response":{...}}
|
| 572 |
+
```
|
| 573 |
+
|
| 574 |
+
This needs a separate SSE writer that emits Responses API format events.
|
| 575 |
+
|
| 576 |
+
**Tests for Phase 4:** (~80 lines)
|
| 577 |
+
- Test real SSE streaming with mocked agent
|
| 578 |
+
- Test SSE event format (Chat Completions vs Responses)
|
| 579 |
+
- Test client disconnect during streaming
|
| 580 |
+
- Test fallback to pseudo-streaming when callback not available
|
| 581 |
+
|
| 582 |
+
---
|
| 583 |
+
|
| 584 |
+
## Integration Issues & Edge Cases
|
| 585 |
+
|
| 586 |
+
### 1. Tool calls during streaming
|
| 587 |
+
|
| 588 |
+
When the model returns tool calls instead of text, no text tokens are emitted.
|
| 589 |
+
The stream_callback is simply never called with text. After tools execute, the
|
| 590 |
+
next API call may produce the final text response — streaming picks up again.
|
| 591 |
+
|
| 592 |
+
The stream preview task needs to handle this: if no tokens arrive during a
|
| 593 |
+
tool-call round, don't send/edit any message. The tool progress messages
|
| 594 |
+
continue working as before.
|
| 595 |
+
|
| 596 |
+
### 2. Duplicate messages
|
| 597 |
+
|
| 598 |
+
The biggest risk: the agent sends the final response normally (via the
|
| 599 |
+
existing send path) AND the stream preview already showed it. The user
|
| 600 |
+
sees the response twice.
|
| 601 |
+
|
| 602 |
+
Prevention: when streaming is active and tokens were delivered, the final
|
| 603 |
+
response send must be suppressed. The `result["_streamed_msg_id"]` marker
|
| 604 |
+
tells the base adapter to skip its normal send.
|
| 605 |
+
|
| 606 |
+
### 3. Response post-processing
|
| 607 |
+
|
| 608 |
+
The final response may differ from the accumulated streamed tokens:
|
| 609 |
+
- Think block stripping (`<think>...</think>` removed)
|
| 610 |
+
- Trailing whitespace cleanup
|
| 611 |
+
- Tool result media tag appending
|
| 612 |
+
|
| 613 |
+
The stream preview shows raw tokens. The final edit should use the
|
| 614 |
+
post-processed version. This means the final edit (removing the cursor)
|
| 615 |
+
should use the post-processed `final_response`, not just the accumulated
|
| 616 |
+
stream text.
|
| 617 |
+
|
| 618 |
+
### 4. Context compression during streaming
|
| 619 |
+
|
| 620 |
+
If the agent triggers context compression mid-conversation, the streaming
|
| 621 |
+
tokens from BEFORE compression are from a different context than those
|
| 622 |
+
after. This isn't a problem in practice — compression happens between
|
| 623 |
+
API calls, not during streaming.
|
| 624 |
+
|
| 625 |
+
### 5. Interrupt during streaming
|
| 626 |
+
|
| 627 |
+
User sends a new message while streaming → interrupt. The stream is killed
|
| 628 |
+
(HTTP connection closed), accumulated tokens are shown as-is (no cursor),
|
| 629 |
+
and the interrupt message is processed normally. This is already handled by
|
| 630 |
+
`_interruptible_api_call` closing the client.
|
| 631 |
+
|
| 632 |
+
### 6. Multi-model / fallback
|
| 633 |
+
|
| 634 |
+
If the primary model fails and the agent falls back to a different model,
|
| 635 |
+
streaming state resets. The fallback call may or may not support streaming.
|
| 636 |
+
The graceful fallback in `_run_streaming_chat_completion` handles this.
|
| 637 |
+
|
| 638 |
+
### 7. Rate limiting on edits
|
| 639 |
+
|
| 640 |
+
Telegram: ~20 edits/minute (~1 every 3 seconds to be safe)
|
| 641 |
+
Discord: 5 edits per 5 seconds per message
|
| 642 |
+
Slack: ~50 API calls/minute
|
| 643 |
+
|
| 644 |
+
The 1.5s edit interval is conservative enough for all platforms. If we get
|
| 645 |
+
429 rate limit errors on edits, just skip that edit cycle and try next time.
|
| 646 |
+
|
| 647 |
+
---
|
| 648 |
+
|
| 649 |
+
## Files Changed Summary
|
| 650 |
+
|
| 651 |
+
| File | Phase | Changes |
|
| 652 |
+
|------|-------|---------|
|
| 653 |
+
| `run_agent.py` | 1 | +stream_callback param, +_run_streaming_chat_completion(), modify _run_codex_stream(), modify _interruptible_api_call() |
|
| 654 |
+
| `gateway/run.py` | 2 | +streaming config reader, +queue/callback setup, +stream_preview task, +skip-final-send logic |
|
| 655 |
+
| `gateway/platforms/base.py` | 2 | +check for _streamed_msg_id in response handler |
|
| 656 |
+
| `cli.py` | 3 | +streaming setup, +token display, +response box integration |
|
| 657 |
+
| `gateway/platforms/api_server.py` | 4 | +real SSE writer, +streaming callback wiring |
|
| 658 |
+
| `hermes_cli/config.py` | 1 | +streaming config defaults |
|
| 659 |
+
| `cli-config.yaml.example` | 1 | +streaming section |
|
| 660 |
+
| `tests/test_streaming.py` | 1-4 | NEW — ~380 lines of tests |
|
| 661 |
+
|
| 662 |
+
**Total new code**: ~500 lines across all phases
|
| 663 |
+
**Total test code**: ~380 lines
|
| 664 |
+
|
| 665 |
+
---
|
| 666 |
+
|
| 667 |
+
## Rollout Plan
|
| 668 |
+
|
| 669 |
+
1. **Phase 1** (core): Merge to main. Streaming disabled by default.
|
| 670 |
+
Zero impact on existing behavior. Can be tested with env var.
|
| 671 |
+
|
| 672 |
+
2. **Phase 2** (gateway): Merge to main. Test on Telegram manually.
|
| 673 |
+
Enable per-platform: `streaming.telegram: true` in config.
|
| 674 |
+
|
| 675 |
+
3. **Phase 3** (CLI): Merge to main. Test in terminal.
|
| 676 |
+
Enable: `streaming.cli: true` or `streaming.enabled: true`.
|
| 677 |
+
|
| 678 |
+
4. **Phase 4** (API server): Merge to main. Test with Open WebUI.
|
| 679 |
+
Auto-enabled when client sends `stream: true`.
|
| 680 |
+
|
| 681 |
+
Each phase is independently mergeable and testable. Streaming stays
|
| 682 |
+
off by default throughout. Once all phases are stable, consider
|
| 683 |
+
changing the default to enabled.
|
| 684 |
+
|
| 685 |
+
---
|
| 686 |
+
|
| 687 |
+
## Config Reference (final state)
|
| 688 |
+
|
| 689 |
+
```yaml
|
| 690 |
+
# config.yaml
|
| 691 |
+
streaming:
|
| 692 |
+
enabled: false # Master switch (default: off)
|
| 693 |
+
cli: true # Per-platform override
|
| 694 |
+
telegram: true
|
| 695 |
+
discord: true
|
| 696 |
+
slack: true
|
| 697 |
+
api_server: true # API server always streams when client requests it
|
| 698 |
+
edit_interval: 1.5 # Seconds between message edits (default: 1.5)
|
| 699 |
+
min_tokens: 20 # Tokens before first display (default: 20)
|
| 700 |
+
```
|
| 701 |
+
|
| 702 |
+
```bash
|
| 703 |
+
# Environment variable override
|
| 704 |
+
HERMES_STREAMING_ENABLED=true
|
| 705 |
+
```
|
AGENTS.md
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hermes Agent - Development Guide
|
| 2 |
+
|
| 3 |
+
Instructions for AI coding assistants and developers working on the hermes-agent codebase.
|
| 4 |
+
|
| 5 |
+
## Development Environment
|
| 6 |
+
|
| 7 |
+
```bash
|
| 8 |
+
source venv/bin/activate # ALWAYS activate before running Python
|
| 9 |
+
```
|
| 10 |
+
|
| 11 |
+
## Project Structure
|
| 12 |
+
|
| 13 |
+
```
|
| 14 |
+
hermes-agent/
|
| 15 |
+
├── run_agent.py # AIAgent class — core conversation loop
|
| 16 |
+
├── model_tools.py # Tool orchestration, _discover_tools(), handle_function_call()
|
| 17 |
+
├── toolsets.py # Toolset definitions, _HERMES_CORE_TOOLS list
|
| 18 |
+
├── cli.py # HermesCLI class — interactive CLI orchestrator
|
| 19 |
+
├── hermes_state.py # SessionDB — SQLite session store (FTS5 search)
|
| 20 |
+
├── agent/ # Agent internals
|
| 21 |
+
│ ├── prompt_builder.py # System prompt assembly
|
| 22 |
+
│ ├── context_compressor.py # Auto context compression
|
| 23 |
+
│ ├── prompt_caching.py # Anthropic prompt caching
|
| 24 |
+
│ ├── auxiliary_client.py # Auxiliary LLM client (vision, summarization)
|
| 25 |
+
│ ├── model_metadata.py # Model context lengths, token estimation
|
| 26 |
+
│ ├── models_dev.py # models.dev registry integration (provider-aware context)
|
| 27 |
+
│ ├── display.py # KawaiiSpinner, tool preview formatting
|
| 28 |
+
│ ├── skill_commands.py # Skill slash commands (shared CLI/gateway)
|
| 29 |
+
│ └── trajectory.py # Trajectory saving helpers
|
| 30 |
+
├── hermes_cli/ # CLI subcommands and setup
|
| 31 |
+
│ ├── main.py # Entry point — all `hermes` subcommands
|
| 32 |
+
│ ├── config.py # DEFAULT_CONFIG, OPTIONAL_ENV_VARS, migration
|
| 33 |
+
│ ├── commands.py # Slash command definitions + SlashCommandCompleter
|
| 34 |
+
│ ├── callbacks.py # Terminal callbacks (clarify, sudo, approval)
|
| 35 |
+
│ ├── setup.py # Interactive setup wizard
|
| 36 |
+
│ ├── skin_engine.py # Skin/theme engine — CLI visual customization
|
| 37 |
+
│ ├── skills_config.py # `hermes skills` — enable/disable skills per platform
|
| 38 |
+
│ ├── tools_config.py # `hermes tools` — enable/disable tools per platform
|
| 39 |
+
│ ├── skills_hub.py # `/skills` slash command (search, browse, install)
|
| 40 |
+
│ ├── models.py # Model catalog, provider model lists
|
| 41 |
+
│ ├── model_switch.py # Shared /model switch pipeline (CLI + gateway)
|
| 42 |
+
│ └── auth.py # Provider credential resolution
|
| 43 |
+
├── tools/ # Tool implementations (one file per tool)
|
| 44 |
+
│ ├── registry.py # Central tool registry (schemas, handlers, dispatch)
|
| 45 |
+
│ ├── approval.py # Dangerous command detection
|
| 46 |
+
│ ├── terminal_tool.py # Terminal orchestration
|
| 47 |
+
│ ├── process_registry.py # Background process management
|
| 48 |
+
│ ├── file_tools.py # File read/write/search/patch
|
| 49 |
+
│ ├── web_tools.py # Web search/extract (Parallel + Firecrawl)
|
| 50 |
+
│ ├── browser_tool.py # Browserbase browser automation
|
| 51 |
+
│ ├── code_execution_tool.py # execute_code sandbox
|
| 52 |
+
│ ├── delegate_tool.py # Subagent delegation
|
| 53 |
+
│ ├── mcp_tool.py # MCP client (~1050 lines)
|
| 54 |
+
│ └── environments/ # Terminal backends (local, docker, ssh, modal, daytona, singularity)
|
| 55 |
+
├── gateway/ # Messaging platform gateway
|
| 56 |
+
│ ├── run.py # Main loop, slash commands, message dispatch
|
| 57 |
+
│ ├── session.py # SessionStore — conversation persistence
|
| 58 |
+
│ └── platforms/ # Adapters: telegram, discord, slack, whatsapp, homeassistant, signal
|
| 59 |
+
├── acp_adapter/ # ACP server (VS Code / Zed / JetBrains integration)
|
| 60 |
+
├── cron/ # Scheduler (jobs.py, scheduler.py)
|
| 61 |
+
├── environments/ # RL training environments (Atropos)
|
| 62 |
+
├── tests/ # Pytest suite (~3000 tests)
|
| 63 |
+
└── batch_runner.py # Parallel batch processing
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
**User config:** `~/.hermes/config.yaml` (settings), `~/.hermes/.env` (API keys)
|
| 67 |
+
|
| 68 |
+
## File Dependency Chain
|
| 69 |
+
|
| 70 |
+
```
|
| 71 |
+
tools/registry.py (no deps — imported by all tool files)
|
| 72 |
+
↑
|
| 73 |
+
tools/*.py (each calls registry.register() at import time)
|
| 74 |
+
↑
|
| 75 |
+
model_tools.py (imports tools/registry + triggers tool discovery)
|
| 76 |
+
↑
|
| 77 |
+
run_agent.py, cli.py, batch_runner.py, environments/
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
---
|
| 81 |
+
|
| 82 |
+
## AIAgent Class (run_agent.py)
|
| 83 |
+
|
| 84 |
+
```python
|
| 85 |
+
class AIAgent:
|
| 86 |
+
def __init__(self,
|
| 87 |
+
model: str = "anthropic/claude-opus-4.6",
|
| 88 |
+
max_iterations: int = 90,
|
| 89 |
+
enabled_toolsets: list = None,
|
| 90 |
+
disabled_toolsets: list = None,
|
| 91 |
+
quiet_mode: bool = False,
|
| 92 |
+
save_trajectories: bool = False,
|
| 93 |
+
platform: str = None, # "cli", "telegram", etc.
|
| 94 |
+
session_id: str = None,
|
| 95 |
+
skip_context_files: bool = False,
|
| 96 |
+
skip_memory: bool = False,
|
| 97 |
+
# ... plus provider, api_mode, callbacks, routing params
|
| 98 |
+
): ...
|
| 99 |
+
|
| 100 |
+
def chat(self, message: str) -> str:
|
| 101 |
+
"""Simple interface — returns final response string."""
|
| 102 |
+
|
| 103 |
+
def run_conversation(self, user_message: str, system_message: str = None,
|
| 104 |
+
conversation_history: list = None, task_id: str = None) -> dict:
|
| 105 |
+
"""Full interface — returns dict with final_response + messages."""
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
### Agent Loop
|
| 109 |
+
|
| 110 |
+
The core loop is inside `run_conversation()` — entirely synchronous:
|
| 111 |
+
|
| 112 |
+
```python
|
| 113 |
+
while api_call_count < self.max_iterations and self.iteration_budget.remaining > 0:
|
| 114 |
+
response = client.chat.completions.create(model=model, messages=messages, tools=tool_schemas)
|
| 115 |
+
if response.tool_calls:
|
| 116 |
+
for tool_call in response.tool_calls:
|
| 117 |
+
result = handle_function_call(tool_call.name, tool_call.args, task_id)
|
| 118 |
+
messages.append(tool_result_message(result))
|
| 119 |
+
api_call_count += 1
|
| 120 |
+
else:
|
| 121 |
+
return response.content
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
Messages follow OpenAI format: `{"role": "system/user/assistant/tool", ...}`. Reasoning content is stored in `assistant_msg["reasoning"]`.
|
| 125 |
+
|
| 126 |
+
---
|
| 127 |
+
|
| 128 |
+
## CLI Architecture (cli.py)
|
| 129 |
+
|
| 130 |
+
- **Rich** for banner/panels, **prompt_toolkit** for input with autocomplete
|
| 131 |
+
- **KawaiiSpinner** (`agent/display.py`) — animated faces during API calls, `┊` activity feed for tool results
|
| 132 |
+
- `load_cli_config()` in cli.py merges hardcoded defaults + user config YAML
|
| 133 |
+
- **Skin engine** (`hermes_cli/skin_engine.py`) — data-driven CLI theming; initialized from `display.skin` config key at startup; skins customize banner colors, spinner faces/verbs/wings, tool prefix, response box, branding text
|
| 134 |
+
- `process_command()` is a method on `HermesCLI` — dispatches on canonical command name resolved via `resolve_command()` from the central registry
|
| 135 |
+
- Skill slash commands: `agent/skill_commands.py` scans `~/.hermes/skills/`, injects as **user message** (not system prompt) to preserve prompt caching
|
| 136 |
+
|
| 137 |
+
### Slash Command Registry (`hermes_cli/commands.py`)
|
| 138 |
+
|
| 139 |
+
All slash commands are defined in a central `COMMAND_REGISTRY` list of `CommandDef` objects. Every downstream consumer derives from this registry automatically:
|
| 140 |
+
|
| 141 |
+
- **CLI** — `process_command()` resolves aliases via `resolve_command()`, dispatches on canonical name
|
| 142 |
+
- **Gateway** — `GATEWAY_KNOWN_COMMANDS` frozenset for hook emission, `resolve_command()` for dispatch
|
| 143 |
+
- **Gateway help** — `gateway_help_lines()` generates `/help` output
|
| 144 |
+
- **Telegram** — `telegram_bot_commands()` generates the BotCommand menu
|
| 145 |
+
- **Slack** — `slack_subcommand_map()` generates `/hermes` subcommand routing
|
| 146 |
+
- **Autocomplete** — `COMMANDS` flat dict feeds `SlashCommandCompleter`
|
| 147 |
+
- **CLI help** — `COMMANDS_BY_CATEGORY` dict feeds `show_help()`
|
| 148 |
+
|
| 149 |
+
### Adding a Slash Command
|
| 150 |
+
|
| 151 |
+
1. Add a `CommandDef` entry to `COMMAND_REGISTRY` in `hermes_cli/commands.py`:
|
| 152 |
+
```python
|
| 153 |
+
CommandDef("mycommand", "Description of what it does", "Session",
|
| 154 |
+
aliases=("mc",), args_hint="[arg]"),
|
| 155 |
+
```
|
| 156 |
+
2. Add handler in `HermesCLI.process_command()` in `cli.py`:
|
| 157 |
+
```python
|
| 158 |
+
elif canonical == "mycommand":
|
| 159 |
+
self._handle_mycommand(cmd_original)
|
| 160 |
+
```
|
| 161 |
+
3. If the command is available in the gateway, add a handler in `gateway/run.py`:
|
| 162 |
+
```python
|
| 163 |
+
if canonical == "mycommand":
|
| 164 |
+
return await self._handle_mycommand(event)
|
| 165 |
+
```
|
| 166 |
+
4. For persistent settings, use `save_config_value()` in `cli.py`
|
| 167 |
+
|
| 168 |
+
**CommandDef fields:**
|
| 169 |
+
- `name` — canonical name without slash (e.g. `"background"`)
|
| 170 |
+
- `description` — human-readable description
|
| 171 |
+
- `category` — one of `"Session"`, `"Configuration"`, `"Tools & Skills"`, `"Info"`, `"Exit"`
|
| 172 |
+
- `aliases` — tuple of alternative names (e.g. `("bg",)`)
|
| 173 |
+
- `args_hint` — argument placeholder shown in help (e.g. `"<prompt>"`, `"[name]"`)
|
| 174 |
+
- `cli_only` — only available in the interactive CLI
|
| 175 |
+
- `gateway_only` — only available in messaging platforms
|
| 176 |
+
- `gateway_config_gate` — config dotpath (e.g. `"display.tool_progress_command"`); when set on a `cli_only` command, the command becomes available in the gateway if the config value is truthy. `GATEWAY_KNOWN_COMMANDS` always includes config-gated commands so the gateway can dispatch them; help/menus only show them when the gate is open.
|
| 177 |
+
|
| 178 |
+
**Adding an alias** requires only adding it to the `aliases` tuple on the existing `CommandDef`. No other file changes needed — dispatch, help text, Telegram menu, Slack mapping, and autocomplete all update automatically.
|
| 179 |
+
|
| 180 |
+
---
|
| 181 |
+
|
| 182 |
+
## Adding New Tools
|
| 183 |
+
|
| 184 |
+
Requires changes in **3 files**:
|
| 185 |
+
|
| 186 |
+
**1. Create `tools/your_tool.py`:**
|
| 187 |
+
```python
|
| 188 |
+
import json, os
|
| 189 |
+
from tools.registry import registry
|
| 190 |
+
|
| 191 |
+
def check_requirements() -> bool:
|
| 192 |
+
return bool(os.getenv("EXAMPLE_API_KEY"))
|
| 193 |
+
|
| 194 |
+
def example_tool(param: str, task_id: str = None) -> str:
|
| 195 |
+
return json.dumps({"success": True, "data": "..."})
|
| 196 |
+
|
| 197 |
+
registry.register(
|
| 198 |
+
name="example_tool",
|
| 199 |
+
toolset="example",
|
| 200 |
+
schema={"name": "example_tool", "description": "...", "parameters": {...}},
|
| 201 |
+
handler=lambda args, **kw: example_tool(param=args.get("param", ""), task_id=kw.get("task_id")),
|
| 202 |
+
check_fn=check_requirements,
|
| 203 |
+
requires_env=["EXAMPLE_API_KEY"],
|
| 204 |
+
)
|
| 205 |
+
```
|
| 206 |
+
|
| 207 |
+
**2. Add import** in `model_tools.py` `_discover_tools()` list.
|
| 208 |
+
|
| 209 |
+
**3. Add to `toolsets.py`** — either `_HERMES_CORE_TOOLS` (all platforms) or a new toolset.
|
| 210 |
+
|
| 211 |
+
The registry handles schema collection, dispatch, availability checking, and error wrapping. All handlers MUST return a JSON string.
|
| 212 |
+
|
| 213 |
+
**Path references in tool schemas**: If the schema description mentions file paths (e.g. default output directories), use `display_hermes_home()` to make them profile-aware. The schema is generated at import time, which is after `_apply_profile_override()` sets `HERMES_HOME`.
|
| 214 |
+
|
| 215 |
+
**State files**: If a tool stores persistent state (caches, logs, checkpoints), use `get_hermes_home()` for the base directory — never `Path.home() / ".hermes"`. This ensures each profile gets its own state.
|
| 216 |
+
|
| 217 |
+
**Agent-level tools** (todo, memory): intercepted by `run_agent.py` before `handle_function_call()`. See `todo_tool.py` for the pattern.
|
| 218 |
+
|
| 219 |
+
---
|
| 220 |
+
|
| 221 |
+
## Adding Configuration
|
| 222 |
+
|
| 223 |
+
### config.yaml options:
|
| 224 |
+
1. Add to `DEFAULT_CONFIG` in `hermes_cli/config.py`
|
| 225 |
+
2. Bump `_config_version` (currently 5) to trigger migration for existing users
|
| 226 |
+
|
| 227 |
+
### .env variables:
|
| 228 |
+
1. Add to `OPTIONAL_ENV_VARS` in `hermes_cli/config.py` with metadata:
|
| 229 |
+
```python
|
| 230 |
+
"NEW_API_KEY": {
|
| 231 |
+
"description": "What it's for",
|
| 232 |
+
"prompt": "Display name",
|
| 233 |
+
"url": "https://...",
|
| 234 |
+
"password": True,
|
| 235 |
+
"category": "tool", # provider, tool, messaging, setting
|
| 236 |
+
},
|
| 237 |
+
```
|
| 238 |
+
|
| 239 |
+
### Config loaders (two separate systems):
|
| 240 |
+
|
| 241 |
+
| Loader | Used by | Location |
|
| 242 |
+
|--------|---------|----------|
|
| 243 |
+
| `load_cli_config()` | CLI mode | `cli.py` |
|
| 244 |
+
| `load_config()` | `hermes tools`, `hermes setup` | `hermes_cli/config.py` |
|
| 245 |
+
| Direct YAML load | Gateway | `gateway/run.py` |
|
| 246 |
+
|
| 247 |
+
---
|
| 248 |
+
|
| 249 |
+
## Skin/Theme System
|
| 250 |
+
|
| 251 |
+
The skin engine (`hermes_cli/skin_engine.py`) provides data-driven CLI visual customization. Skins are **pure data** — no code changes needed to add a new skin.
|
| 252 |
+
|
| 253 |
+
### Architecture
|
| 254 |
+
|
| 255 |
+
```
|
| 256 |
+
hermes_cli/skin_engine.py # SkinConfig dataclass, built-in skins, YAML loader
|
| 257 |
+
~/.hermes/skins/*.yaml # User-installed custom skins (drop-in)
|
| 258 |
+
```
|
| 259 |
+
|
| 260 |
+
- `init_skin_from_config()` — called at CLI startup, reads `display.skin` from config
|
| 261 |
+
- `get_active_skin()` — returns cached `SkinConfig` for the current skin
|
| 262 |
+
- `set_active_skin(name)` — switches skin at runtime (used by `/skin` command)
|
| 263 |
+
- `load_skin(name)` — loads from user skins first, then built-ins, then falls back to default
|
| 264 |
+
- Missing skin values inherit from the `default` skin automatically
|
| 265 |
+
|
| 266 |
+
### What skins customize
|
| 267 |
+
|
| 268 |
+
| Element | Skin Key | Used By |
|
| 269 |
+
|---------|----------|---------|
|
| 270 |
+
| Banner panel border | `colors.banner_border` | `banner.py` |
|
| 271 |
+
| Banner panel title | `colors.banner_title` | `banner.py` |
|
| 272 |
+
| Banner section headers | `colors.banner_accent` | `banner.py` |
|
| 273 |
+
| Banner dim text | `colors.banner_dim` | `banner.py` |
|
| 274 |
+
| Banner body text | `colors.banner_text` | `banner.py` |
|
| 275 |
+
| Response box border | `colors.response_border` | `cli.py` |
|
| 276 |
+
| Spinner faces (waiting) | `spinner.waiting_faces` | `display.py` |
|
| 277 |
+
| Spinner faces (thinking) | `spinner.thinking_faces` | `display.py` |
|
| 278 |
+
| Spinner verbs | `spinner.thinking_verbs` | `display.py` |
|
| 279 |
+
| Spinner wings (optional) | `spinner.wings` | `display.py` |
|
| 280 |
+
| Tool output prefix | `tool_prefix` | `display.py` |
|
| 281 |
+
| Per-tool emojis | `tool_emojis` | `display.py` → `get_tool_emoji()` |
|
| 282 |
+
| Agent name | `branding.agent_name` | `banner.py`, `cli.py` |
|
| 283 |
+
| Welcome message | `branding.welcome` | `cli.py` |
|
| 284 |
+
| Response box label | `branding.response_label` | `cli.py` |
|
| 285 |
+
| Prompt symbol | `branding.prompt_symbol` | `cli.py` |
|
| 286 |
+
|
| 287 |
+
### Built-in skins
|
| 288 |
+
|
| 289 |
+
- `default` — Classic Hermes gold/kawaii (the current look)
|
| 290 |
+
- `ares` — Crimson/bronze war-god theme with custom spinner wings
|
| 291 |
+
- `mono` — Clean grayscale monochrome
|
| 292 |
+
- `slate` — Cool blue developer-focused theme
|
| 293 |
+
|
| 294 |
+
### Adding a built-in skin
|
| 295 |
+
|
| 296 |
+
Add to `_BUILTIN_SKINS` dict in `hermes_cli/skin_engine.py`:
|
| 297 |
+
|
| 298 |
+
```python
|
| 299 |
+
"mytheme": {
|
| 300 |
+
"name": "mytheme",
|
| 301 |
+
"description": "Short description",
|
| 302 |
+
"colors": { ... },
|
| 303 |
+
"spinner": { ... },
|
| 304 |
+
"branding": { ... },
|
| 305 |
+
"tool_prefix": "┊",
|
| 306 |
+
},
|
| 307 |
+
```
|
| 308 |
+
|
| 309 |
+
### User skins (YAML)
|
| 310 |
+
|
| 311 |
+
Users create `~/.hermes/skins/<name>.yaml`:
|
| 312 |
+
|
| 313 |
+
```yaml
|
| 314 |
+
name: cyberpunk
|
| 315 |
+
description: Neon-soaked terminal theme
|
| 316 |
+
|
| 317 |
+
colors:
|
| 318 |
+
banner_border: "#FF00FF"
|
| 319 |
+
banner_title: "#00FFFF"
|
| 320 |
+
banner_accent: "#FF1493"
|
| 321 |
+
|
| 322 |
+
spinner:
|
| 323 |
+
thinking_verbs: ["jacking in", "decrypting", "uploading"]
|
| 324 |
+
wings:
|
| 325 |
+
- ["⟨⚡", "⚡⟩"]
|
| 326 |
+
|
| 327 |
+
branding:
|
| 328 |
+
agent_name: "Cyber Agent"
|
| 329 |
+
response_label: " ⚡ Cyber "
|
| 330 |
+
|
| 331 |
+
tool_prefix: "▏"
|
| 332 |
+
```
|
| 333 |
+
|
| 334 |
+
Activate with `/skin cyberpunk` or `display.skin: cyberpunk` in config.yaml.
|
| 335 |
+
|
| 336 |
+
---
|
| 337 |
+
|
| 338 |
+
## Important Policies
|
| 339 |
+
### Prompt Caching Must Not Break
|
| 340 |
+
|
| 341 |
+
Hermes-Agent ensures caching remains valid throughout a conversation. **Do NOT implement changes that would:**
|
| 342 |
+
- Alter past context mid-conversation
|
| 343 |
+
- Change toolsets mid-conversation
|
| 344 |
+
- Reload memories or rebuild system prompts mid-conversation
|
| 345 |
+
|
| 346 |
+
Cache-breaking forces dramatically higher costs. The ONLY time we alter context is during context compression.
|
| 347 |
+
|
| 348 |
+
### Working Directory Behavior
|
| 349 |
+
- **CLI**: Uses current directory (`.` → `os.getcwd()`)
|
| 350 |
+
- **Messaging**: Uses `MESSAGING_CWD` env var (default: home directory)
|
| 351 |
+
|
| 352 |
+
### Background Process Notifications (Gateway)
|
| 353 |
+
|
| 354 |
+
When `terminal(background=true, notify_on_complete=true)` is used, the gateway runs a watcher that
|
| 355 |
+
detects process completion and triggers a new agent turn. Control verbosity of background process
|
| 356 |
+
messages with `display.background_process_notifications`
|
| 357 |
+
in config.yaml (or `HERMES_BACKGROUND_NOTIFICATIONS` env var):
|
| 358 |
+
|
| 359 |
+
- `all` — running-output updates + final message (default)
|
| 360 |
+
- `result` — only the final completion message
|
| 361 |
+
- `error` — only the final message when exit code != 0
|
| 362 |
+
- `off` — no watcher messages at all
|
| 363 |
+
|
| 364 |
+
---
|
| 365 |
+
|
| 366 |
+
## Profiles: Multi-Instance Support
|
| 367 |
+
|
| 368 |
+
Hermes supports **profiles** — multiple fully isolated instances, each with its own
|
| 369 |
+
`HERMES_HOME` directory (config, API keys, memory, sessions, skills, gateway, etc.).
|
| 370 |
+
|
| 371 |
+
The core mechanism: `_apply_profile_override()` in `hermes_cli/main.py` sets
|
| 372 |
+
`HERMES_HOME` before any module imports. All 119+ references to `get_hermes_home()`
|
| 373 |
+
automatically scope to the active profile.
|
| 374 |
+
|
| 375 |
+
### Rules for profile-safe code
|
| 376 |
+
|
| 377 |
+
1. **Use `get_hermes_home()` for all HERMES_HOME paths.** Import from `hermes_constants`.
|
| 378 |
+
NEVER hardcode `~/.hermes` or `Path.home() / ".hermes"` in code that reads/writes state.
|
| 379 |
+
```python
|
| 380 |
+
# GOOD
|
| 381 |
+
from hermes_constants import get_hermes_home
|
| 382 |
+
config_path = get_hermes_home() / "config.yaml"
|
| 383 |
+
|
| 384 |
+
# BAD — breaks profiles
|
| 385 |
+
config_path = Path.home() / ".hermes" / "config.yaml"
|
| 386 |
+
```
|
| 387 |
+
|
| 388 |
+
2. **Use `display_hermes_home()` for user-facing messages.** Import from `hermes_constants`.
|
| 389 |
+
This returns `~/.hermes` for default or `~/.hermes/profiles/<name>` for profiles.
|
| 390 |
+
```python
|
| 391 |
+
# GOOD
|
| 392 |
+
from hermes_constants import display_hermes_home
|
| 393 |
+
print(f"Config saved to {display_hermes_home()}/config.yaml")
|
| 394 |
+
|
| 395 |
+
# BAD — shows wrong path for profiles
|
| 396 |
+
print("Config saved to ~/.hermes/config.yaml")
|
| 397 |
+
```
|
| 398 |
+
|
| 399 |
+
3. **Module-level constants are fine** — they cache `get_hermes_home()` at import time,
|
| 400 |
+
which is AFTER `_apply_profile_override()` sets the env var. Just use `get_hermes_home()`,
|
| 401 |
+
not `Path.home() / ".hermes"`.
|
| 402 |
+
|
| 403 |
+
4. **Tests that mock `Path.home()` must also set `HERMES_HOME`** — since code now uses
|
| 404 |
+
`get_hermes_home()` (reads env var), not `Path.home() / ".hermes"`:
|
| 405 |
+
```python
|
| 406 |
+
with patch.object(Path, "home", return_value=tmp_path), \
|
| 407 |
+
patch.dict(os.environ, {"HERMES_HOME": str(tmp_path / ".hermes")}):
|
| 408 |
+
...
|
| 409 |
+
```
|
| 410 |
+
|
| 411 |
+
5. **Gateway platform adapters should use token locks** — if the adapter connects with
|
| 412 |
+
a unique credential (bot token, API key), call `acquire_scoped_lock()` from
|
| 413 |
+
`gateway.status` in the `connect()`/`start()` method and `release_scoped_lock()` in
|
| 414 |
+
`disconnect()`/`stop()`. This prevents two profiles from using the same credential.
|
| 415 |
+
See `gateway/platforms/telegram.py` for the canonical pattern.
|
| 416 |
+
|
| 417 |
+
6. **Profile operations are HOME-anchored, not HERMES_HOME-anchored** — `_get_profiles_root()`
|
| 418 |
+
returns `Path.home() / ".hermes" / "profiles"`, NOT `get_hermes_home() / "profiles"`.
|
| 419 |
+
This is intentional — it lets `hermes -p coder profile list` see all profiles regardless
|
| 420 |
+
of which one is active.
|
| 421 |
+
|
| 422 |
+
## Known Pitfalls
|
| 423 |
+
|
| 424 |
+
### DO NOT hardcode `~/.hermes` paths
|
| 425 |
+
Use `get_hermes_home()` from `hermes_constants` for code paths. Use `display_hermes_home()`
|
| 426 |
+
for user-facing print/log messages. Hardcoding `~/.hermes` breaks profiles — each profile
|
| 427 |
+
has its own `HERMES_HOME` directory. This was the source of 5 bugs fixed in PR #3575.
|
| 428 |
+
|
| 429 |
+
### DO NOT use `simple_term_menu` for interactive menus
|
| 430 |
+
Rendering bugs in tmux/iTerm2 — ghosting on scroll. Use `curses` (stdlib) instead. See `hermes_cli/tools_config.py` for the pattern.
|
| 431 |
+
|
| 432 |
+
### DO NOT use `\033[K` (ANSI erase-to-EOL) in spinner/display code
|
| 433 |
+
Leaks as literal `?[K` text under `prompt_toolkit`'s `patch_stdout`. Use space-padding: `f"\r{line}{' ' * pad}"`.
|
| 434 |
+
|
| 435 |
+
### `_last_resolved_tool_names` is a process-global in `model_tools.py`
|
| 436 |
+
`_run_single_child()` in `delegate_tool.py` saves and restores this global around subagent execution. If you add new code that reads this global, be aware it may be temporarily stale during child agent runs.
|
| 437 |
+
|
| 438 |
+
### DO NOT hardcode cross-tool references in schema descriptions
|
| 439 |
+
Tool schema descriptions must not mention tools from other toolsets by name (e.g., `browser_navigate` saying "prefer web_search"). Those tools may be unavailable (missing API keys, disabled toolset), causing the model to hallucinate calls to non-existent tools. If a cross-reference is needed, add it dynamically in `get_tool_definitions()` in `model_tools.py` — see the `browser_navigate` / `execute_code` post-processing blocks for the pattern.
|
| 440 |
+
|
| 441 |
+
### Tests must not write to `~/.hermes/`
|
| 442 |
+
The `_isolate_hermes_home` autouse fixture in `tests/conftest.py` redirects `HERMES_HOME` to a temp dir. Never hardcode `~/.hermes/` paths in tests.
|
| 443 |
+
|
| 444 |
+
**Profile tests**: When testing profile features, also mock `Path.home()` so that
|
| 445 |
+
`_get_profiles_root()` and `_get_default_hermes_home()` resolve within the temp dir.
|
| 446 |
+
Use the pattern from `tests/hermes_cli/test_profiles.py`:
|
| 447 |
+
```python
|
| 448 |
+
@pytest.fixture
|
| 449 |
+
def profile_env(tmp_path, monkeypatch):
|
| 450 |
+
home = tmp_path / ".hermes"
|
| 451 |
+
home.mkdir()
|
| 452 |
+
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
| 453 |
+
monkeypatch.setenv("HERMES_HOME", str(home))
|
| 454 |
+
return home
|
| 455 |
+
```
|
| 456 |
+
|
| 457 |
+
---
|
| 458 |
+
|
| 459 |
+
## Testing
|
| 460 |
+
|
| 461 |
+
```bash
|
| 462 |
+
source venv/bin/activate
|
| 463 |
+
python -m pytest tests/ -q # Full suite (~3000 tests, ~3 min)
|
| 464 |
+
python -m pytest tests/test_model_tools.py -q # Toolset resolution
|
| 465 |
+
python -m pytest tests/test_cli_init.py -q # CLI config loading
|
| 466 |
+
python -m pytest tests/gateway/ -q # Gateway tests
|
| 467 |
+
python -m pytest tests/tools/ -q # Tool-level tests
|
| 468 |
+
```
|
| 469 |
+
|
| 470 |
+
Always run the full suite before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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Contributing to Hermes Agent
|
| 2 |
+
|
| 3 |
+
Thank you for contributing to Hermes Agent! This guide covers everything you need: setting up your dev environment, understanding the architecture, deciding what to build, and getting your PR merged.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## Contribution Priorities
|
| 8 |
+
|
| 9 |
+
We value contributions in this order:
|
| 10 |
+
|
| 11 |
+
1. **Bug fixes** — crashes, incorrect behavior, data loss. Always top priority.
|
| 12 |
+
2. **Cross-platform compatibility** — Windows, macOS, different Linux distros, different terminal emulators. We want Hermes to work everywhere.
|
| 13 |
+
3. **Security hardening** — shell injection, prompt injection, path traversal, privilege escalation. See [Security](#security-considerations).
|
| 14 |
+
4. **Performance and robustness** — retry logic, error handling, graceful degradation.
|
| 15 |
+
5. **New skills** — but only broadly useful ones. See [Should it be a Skill or a Tool?](#should-it-be-a-skill-or-a-tool)
|
| 16 |
+
6. **New tools** — rarely needed. Most capabilities should be skills. See below.
|
| 17 |
+
7. **Documentation** — fixes, clarifications, new examples.
|
| 18 |
+
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
## Should it be a Skill or a Tool?
|
| 22 |
+
|
| 23 |
+
This is the most common question for new contributors. The answer is almost always **skill**.
|
| 24 |
+
|
| 25 |
+
### Make it a Skill when:
|
| 26 |
+
|
| 27 |
+
- The capability can be expressed as instructions + shell commands + existing tools
|
| 28 |
+
- It wraps an external CLI or API that the agent can call via `terminal` or `web_extract`
|
| 29 |
+
- It doesn't need custom Python integration or API key management baked into the agent
|
| 30 |
+
- Examples: arXiv search, git workflows, Docker management, PDF processing, email via CLI tools
|
| 31 |
+
|
| 32 |
+
### Make it a Tool when:
|
| 33 |
+
|
| 34 |
+
- It requires end-to-end integration with API keys, auth flows, or multi-component configuration managed by the agent harness
|
| 35 |
+
- It needs custom processing logic that must execute precisely every time (not "best effort" from LLM interpretation)
|
| 36 |
+
- It handles binary data, streaming, or real-time events that can't go through the terminal
|
| 37 |
+
- Examples: browser automation (Browserbase session management), TTS (audio encoding + platform delivery), vision analysis (base64 image handling)
|
| 38 |
+
|
| 39 |
+
### Should the Skill be bundled?
|
| 40 |
+
|
| 41 |
+
Bundled skills (in `skills/`) ship with every Hermes install. They should be **broadly useful to most users**:
|
| 42 |
+
|
| 43 |
+
- Document handling, web research, common dev workflows, system administration
|
| 44 |
+
- Used regularly by a wide range of people
|
| 45 |
+
|
| 46 |
+
If your skill is official and useful but not universally needed (e.g., a paid service integration, a heavyweight dependency), put it in **`optional-skills/`** — it ships with the repo but isn't activated by default. Users can discover it via `hermes skills browse` (labeled "official") and install it with `hermes skills install` (no third-party warning, builtin trust).
|
| 47 |
+
|
| 48 |
+
If your skill is specialized, community-contributed, or niche, it's better suited for a **Skills Hub** — upload it to a skills registry and share it in the [Nous Research Discord](https://discord.gg/NousResearch). Users can install it with `hermes skills install`.
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## Development Setup
|
| 53 |
+
|
| 54 |
+
### Prerequisites
|
| 55 |
+
|
| 56 |
+
| Requirement | Notes |
|
| 57 |
+
|-------------|-------|
|
| 58 |
+
| **Git** | With `--recurse-submodules` support |
|
| 59 |
+
| **Python 3.11+** | uv will install it if missing |
|
| 60 |
+
| **uv** | Fast Python package manager ([install](https://docs.astral.sh/uv/)) |
|
| 61 |
+
| **Node.js 18+** | Optional — needed for browser tools and WhatsApp bridge |
|
| 62 |
+
|
| 63 |
+
### Clone and install
|
| 64 |
+
|
| 65 |
+
```bash
|
| 66 |
+
git clone --recurse-submodules https://github.com/NousResearch/hermes-agent.git
|
| 67 |
+
cd hermes-agent
|
| 68 |
+
|
| 69 |
+
# Create venv with Python 3.11
|
| 70 |
+
uv venv venv --python 3.11
|
| 71 |
+
export VIRTUAL_ENV="$(pwd)/venv"
|
| 72 |
+
|
| 73 |
+
# Install with all extras (messaging, cron, CLI menus, dev tools)
|
| 74 |
+
uv pip install -e ".[all,dev]"
|
| 75 |
+
|
| 76 |
+
# Optional: RL training submodule
|
| 77 |
+
# git submodule update --init tinker-atropos && uv pip install -e "./tinker-atropos"
|
| 78 |
+
|
| 79 |
+
# Optional: browser tools
|
| 80 |
+
npm install
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
### Configure for development
|
| 84 |
+
|
| 85 |
+
```bash
|
| 86 |
+
mkdir -p ~/.hermes/{cron,sessions,logs,memories,skills}
|
| 87 |
+
cp cli-config.yaml.example ~/.hermes/config.yaml
|
| 88 |
+
touch ~/.hermes/.env
|
| 89 |
+
|
| 90 |
+
# Add at minimum an LLM provider key:
|
| 91 |
+
echo 'OPENROUTER_API_KEY=sk-or-v1-your-key' >> ~/.hermes/.env
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
### Run
|
| 95 |
+
|
| 96 |
+
```bash
|
| 97 |
+
# Symlink for global access
|
| 98 |
+
mkdir -p ~/.local/bin
|
| 99 |
+
ln -sf "$(pwd)/venv/bin/hermes" ~/.local/bin/hermes
|
| 100 |
+
|
| 101 |
+
# Verify
|
| 102 |
+
hermes doctor
|
| 103 |
+
hermes chat -q "Hello"
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
### Run tests
|
| 107 |
+
|
| 108 |
+
```bash
|
| 109 |
+
pytest tests/ -v
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
---
|
| 113 |
+
|
| 114 |
+
## Project Structure
|
| 115 |
+
|
| 116 |
+
```
|
| 117 |
+
hermes-agent/
|
| 118 |
+
├── run_agent.py # AIAgent class — core conversation loop, tool dispatch, session persistence
|
| 119 |
+
├── cli.py # HermesCLI class — interactive TUI, prompt_toolkit integration
|
| 120 |
+
├── model_tools.py # Tool orchestration (thin layer over tools/registry.py)
|
| 121 |
+
├── toolsets.py # Tool groupings and presets (hermes-cli, hermes-telegram, etc.)
|
| 122 |
+
├── hermes_state.py # SQLite session database with FTS5 full-text search, session titles
|
| 123 |
+
├── batch_runner.py # Parallel batch processing for trajectory generation
|
| 124 |
+
│
|
| 125 |
+
├── agent/ # Agent internals (extracted modules)
|
| 126 |
+
│ ├── prompt_builder.py # System prompt assembly (identity, skills, context files, memory)
|
| 127 |
+
│ ├── context_compressor.py # Auto-summarization when approaching context limits
|
| 128 |
+
│ ├── auxiliary_client.py # Resolves auxiliary OpenAI clients (summarization, vision)
|
| 129 |
+
│ ├── display.py # KawaiiSpinner, tool progress formatting
|
| 130 |
+
│ ├── model_metadata.py # Model context lengths, token estimation
|
| 131 |
+
│ └── trajectory.py # Trajectory saving helpers
|
| 132 |
+
│
|
| 133 |
+
├── hermes_cli/ # CLI command implementations
|
| 134 |
+
│ ├── main.py # Entry point, argument parsing, command dispatch
|
| 135 |
+
│ ├── config.py # Config management, migration, env var definitions
|
| 136 |
+
│ ├── setup.py # Interactive setup wizard
|
| 137 |
+
│ ├── auth.py # Provider resolution, OAuth, Nous Portal
|
| 138 |
+
│ ├── models.py # OpenRouter model selection lists
|
| 139 |
+
│ ├── banner.py # Welcome banner, ASCII art
|
| 140 |
+
│ ├── commands.py # Central slash command registry (CommandDef), autocomplete, gateway helpers
|
| 141 |
+
│ ├── callbacks.py # Interactive callbacks (clarify, sudo, approval)
|
| 142 |
+
│ ├── doctor.py # Diagnostics
|
| 143 |
+
│ ├── skills_hub.py # Skills Hub CLI + /skills slash command
|
| 144 |
+
│ └── skin_engine.py # Skin/theme engine — data-driven CLI visual customization
|
| 145 |
+
│
|
| 146 |
+
├── tools/ # Tool implementations (self-registering)
|
| 147 |
+
│ ├── registry.py # Central tool registry (schemas, handlers, dispatch)
|
| 148 |
+
│ ├── approval.py # Dangerous command detection + per-session approval
|
| 149 |
+
│ ├── terminal_tool.py # Terminal orchestration (sudo, env lifecycle, backends)
|
| 150 |
+
│ ├── file_operations.py # read_file, write_file, search, patch, etc.
|
| 151 |
+
│ ├── web_tools.py # web_search, web_extract (Parallel/Firecrawl + Gemini summarization)
|
| 152 |
+
│ ├── vision_tools.py # Image analysis via multimodal models
|
| 153 |
+
│ ├── delegate_tool.py # Subagent spawning and parallel task execution
|
| 154 |
+
│ ├── code_execution_tool.py # Sandboxed Python with RPC tool access
|
| 155 |
+
│ ├── session_search_tool.py # Search past conversations with FTS5 + summarization
|
| 156 |
+
│ ├── cronjob_tools.py # Scheduled task management
|
| 157 |
+
│ ├── skill_tools.py # Skill search, load, manage
|
| 158 |
+
│ └── environments/ # Terminal execution backends
|
| 159 |
+
│ ├── base.py # BaseEnvironment ABC
|
| 160 |
+
│ ├── local.py, docker.py, ssh.py, singularity.py, modal.py, daytona.py
|
| 161 |
+
│
|
| 162 |
+
├── gateway/ # Messaging gateway
|
| 163 |
+
│ ├── run.py # GatewayRunner — platform lifecycle, message routing, cron
|
| 164 |
+
│ ├── config.py # Platform configuration resolution
|
| 165 |
+
│ ├── session.py # Session store, context prompts, reset policies
|
| 166 |
+
│ └── platforms/ # Platform adapters
|
| 167 |
+
│ ├── telegram.py, discord_adapter.py, slack.py, whatsapp.py
|
| 168 |
+
│
|
| 169 |
+
├── scripts/ # Installer and bridge scripts
|
| 170 |
+
│ ├── install.sh # Linux/macOS installer
|
| 171 |
+
│ ├── install.ps1 # Windows PowerShell installer
|
| 172 |
+
│ └── whatsapp-bridge/ # Node.js WhatsApp bridge (Baileys)
|
| 173 |
+
│
|
| 174 |
+
├── skills/ # Bundled skills (copied to ~/.hermes/skills/ on install)
|
| 175 |
+
├── optional-skills/ # Official optional skills (discoverable via hub, not activated by default)
|
| 176 |
+
├── environments/ # RL training environments (Atropos integration)
|
| 177 |
+
├── tests/ # Test suite
|
| 178 |
+
├── website/ # Documentation site (hermes-agent.nousresearch.com)
|
| 179 |
+
│
|
| 180 |
+
├── cli-config.yaml.example # Example configuration (copied to ~/.hermes/config.yaml)
|
| 181 |
+
└── AGENTS.md # Development guide for AI coding assistants
|
| 182 |
+
```
|
| 183 |
+
|
| 184 |
+
### User configuration (stored in `~/.hermes/`)
|
| 185 |
+
|
| 186 |
+
| Path | Purpose |
|
| 187 |
+
|------|---------|
|
| 188 |
+
| `~/.hermes/config.yaml` | Settings (model, terminal, toolsets, compression, etc.) |
|
| 189 |
+
| `~/.hermes/.env` | API keys and secrets |
|
| 190 |
+
| `~/.hermes/auth.json` | OAuth credentials (Nous Portal) |
|
| 191 |
+
| `~/.hermes/skills/` | All active skills (bundled + hub-installed + agent-created) |
|
| 192 |
+
| `~/.hermes/memories/` | Persistent memory (MEMORY.md, USER.md) |
|
| 193 |
+
| `~/.hermes/state.db` | SQLite session database |
|
| 194 |
+
| `~/.hermes/sessions/` | JSON session logs |
|
| 195 |
+
| `~/.hermes/cron/` | Scheduled job data |
|
| 196 |
+
| `~/.hermes/whatsapp/session/` | WhatsApp bridge credentials |
|
| 197 |
+
|
| 198 |
+
---
|
| 199 |
+
|
| 200 |
+
## Architecture Overview
|
| 201 |
+
|
| 202 |
+
### Core Loop
|
| 203 |
+
|
| 204 |
+
```
|
| 205 |
+
User message → AIAgent._run_agent_loop()
|
| 206 |
+
├── Build system prompt (prompt_builder.py)
|
| 207 |
+
├── Build API kwargs (model, messages, tools, reasoning config)
|
| 208 |
+
├── Call LLM (OpenAI-compatible API)
|
| 209 |
+
├── If tool_calls in response:
|
| 210 |
+
│ ├── Execute each tool via registry dispatch
|
| 211 |
+
│ ├── Add tool results to conversation
|
| 212 |
+
│ └── Loop back to LLM call
|
| 213 |
+
├── If text response:
|
| 214 |
+
│ ├── Persist session to DB
|
| 215 |
+
│ └── Return final_response
|
| 216 |
+
└── Context compression if approaching token limit
|
| 217 |
+
```
|
| 218 |
+
|
| 219 |
+
### Key Design Patterns
|
| 220 |
+
|
| 221 |
+
- **Self-registering tools**: Each tool file calls `registry.register()` at import time. `model_tools.py` triggers discovery by importing all tool modules.
|
| 222 |
+
- **Toolset grouping**: Tools are grouped into toolsets (`web`, `terminal`, `file`, `browser`, etc.) that can be enabled/disabled per platform.
|
| 223 |
+
- **Session persistence**: All conversations are stored in SQLite (`hermes_state.py`) with full-text search and unique session titles. JSON logs go to `~/.hermes/sessions/`.
|
| 224 |
+
- **Ephemeral injection**: System prompts and prefill messages are injected at API call time, never persisted to the database or logs.
|
| 225 |
+
- **Provider abstraction**: The agent works with any OpenAI-compatible API. Provider resolution happens at init time (Nous Portal OAuth, OpenRouter API key, or custom endpoint).
|
| 226 |
+
- **Provider routing**: When using OpenRouter, `provider_routing` in config.yaml controls provider selection (sort by throughput/latency/price, allow/ignore specific providers, data retention policies). These are injected as `extra_body.provider` in API requests.
|
| 227 |
+
|
| 228 |
+
---
|
| 229 |
+
|
| 230 |
+
## Code Style
|
| 231 |
+
|
| 232 |
+
- **PEP 8** with practical exceptions (we don't enforce strict line length)
|
| 233 |
+
- **Comments**: Only when explaining non-obvious intent, trade-offs, or API quirks. Don't narrate what the code does — `# increment counter` adds nothing
|
| 234 |
+
- **Error handling**: Catch specific exceptions. Log with `logger.warning()`/`logger.error()` — use `exc_info=True` for unexpected errors so stack traces appear in logs
|
| 235 |
+
- **Cross-platform**: Never assume Unix. See [Cross-Platform Compatibility](#cross-platform-compatibility)
|
| 236 |
+
|
| 237 |
+
---
|
| 238 |
+
|
| 239 |
+
## Adding a New Tool
|
| 240 |
+
|
| 241 |
+
Before writing a tool, ask: [should this be a skill instead?](#should-it-be-a-skill-or-a-tool)
|
| 242 |
+
|
| 243 |
+
Tools self-register with the central registry. Each tool file co-locates its schema, handler, and registration:
|
| 244 |
+
|
| 245 |
+
```python
|
| 246 |
+
"""my_tool — Brief description of what this tool does."""
|
| 247 |
+
|
| 248 |
+
import json
|
| 249 |
+
from tools.registry import registry
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def my_tool(param1: str, param2: int = 10, **kwargs) -> str:
|
| 253 |
+
"""Handler. Returns a string result (often JSON)."""
|
| 254 |
+
result = do_work(param1, param2)
|
| 255 |
+
return json.dumps(result)
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
MY_TOOL_SCHEMA = {
|
| 259 |
+
"type": "function",
|
| 260 |
+
"function": {
|
| 261 |
+
"name": "my_tool",
|
| 262 |
+
"description": "What this tool does and when the agent should use it.",
|
| 263 |
+
"parameters": {
|
| 264 |
+
"type": "object",
|
| 265 |
+
"properties": {
|
| 266 |
+
"param1": {"type": "string", "description": "What param1 is"},
|
| 267 |
+
"param2": {"type": "integer", "description": "What param2 is", "default": 10},
|
| 268 |
+
},
|
| 269 |
+
"required": ["param1"],
|
| 270 |
+
},
|
| 271 |
+
},
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def _check_requirements() -> bool:
|
| 276 |
+
"""Return True if this tool's dependencies are available."""
|
| 277 |
+
return True
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
registry.register(
|
| 281 |
+
name="my_tool",
|
| 282 |
+
toolset="my_toolset",
|
| 283 |
+
schema=MY_TOOL_SCHEMA,
|
| 284 |
+
handler=lambda args, **kw: my_tool(**args, **kw),
|
| 285 |
+
check_fn=_check_requirements,
|
| 286 |
+
)
|
| 287 |
+
```
|
| 288 |
+
|
| 289 |
+
Then add the import to `model_tools.py` in the `_modules` list:
|
| 290 |
+
|
| 291 |
+
```python
|
| 292 |
+
_modules = [
|
| 293 |
+
# ... existing modules ...
|
| 294 |
+
"tools.my_tool",
|
| 295 |
+
]
|
| 296 |
+
```
|
| 297 |
+
|
| 298 |
+
If it's a new toolset, add it to `toolsets.py` and to the relevant platform presets.
|
| 299 |
+
|
| 300 |
+
---
|
| 301 |
+
|
| 302 |
+
## Adding a Skill
|
| 303 |
+
|
| 304 |
+
Bundled skills live in `skills/` organized by category. Official optional skills use the same structure in `optional-skills/`:
|
| 305 |
+
|
| 306 |
+
```
|
| 307 |
+
skills/
|
| 308 |
+
├── research/
|
| 309 |
+
│ └── arxiv/
|
| 310 |
+
│ ├── SKILL.md # Required: main instructions
|
| 311 |
+
│ └── scripts/ # Optional: helper scripts
|
| 312 |
+
│ └── search_arxiv.py
|
| 313 |
+
├── productivity/
|
| 314 |
+
│ └── ocr-and-documents/
|
| 315 |
+
│ ├── SKILL.md
|
| 316 |
+
│ ├── scripts/
|
| 317 |
+
│ └── references/
|
| 318 |
+
└── ...
|
| 319 |
+
```
|
| 320 |
+
|
| 321 |
+
### SKILL.md format
|
| 322 |
+
|
| 323 |
+
```markdown
|
| 324 |
+
---
|
| 325 |
+
name: my-skill
|
| 326 |
+
description: Brief description (shown in skill search results)
|
| 327 |
+
version: 1.0.0
|
| 328 |
+
author: Your Name
|
| 329 |
+
license: MIT
|
| 330 |
+
platforms: [macos, linux] # Optional — restrict to specific OS platforms
|
| 331 |
+
# Valid: macos, linux, windows
|
| 332 |
+
# Omit to load on all platforms (default)
|
| 333 |
+
required_environment_variables: # Optional — secure setup-on-load metadata
|
| 334 |
+
- name: MY_API_KEY
|
| 335 |
+
prompt: API key
|
| 336 |
+
help: Where to get it
|
| 337 |
+
required_for: full functionality
|
| 338 |
+
prerequisites: # Optional legacy runtime requirements
|
| 339 |
+
env_vars: [MY_API_KEY] # Backward-compatible alias for required env vars
|
| 340 |
+
commands: [curl, jq] # Advisory only; does not hide the skill
|
| 341 |
+
metadata:
|
| 342 |
+
hermes:
|
| 343 |
+
tags: [Category, Subcategory, Keywords]
|
| 344 |
+
related_skills: [other-skill-name]
|
| 345 |
+
fallback_for_toolsets: [web] # Optional — show only when toolset is unavailable
|
| 346 |
+
requires_toolsets: [terminal] # Optional — show only when toolset is available
|
| 347 |
+
---
|
| 348 |
+
|
| 349 |
+
# Skill Title
|
| 350 |
+
|
| 351 |
+
Brief intro.
|
| 352 |
+
|
| 353 |
+
## When to Use
|
| 354 |
+
Trigger conditions — when should the agent load this skill?
|
| 355 |
+
|
| 356 |
+
## Quick Reference
|
| 357 |
+
Table of common commands or API calls.
|
| 358 |
+
|
| 359 |
+
## Procedure
|
| 360 |
+
Step-by-step instructions the agent follows.
|
| 361 |
+
|
| 362 |
+
## Pitfalls
|
| 363 |
+
Known failure modes and how to handle them.
|
| 364 |
+
|
| 365 |
+
## Verification
|
| 366 |
+
How the agent confirms it worked.
|
| 367 |
+
```
|
| 368 |
+
|
| 369 |
+
### Platform-specific skills
|
| 370 |
+
|
| 371 |
+
Skills can declare which OS platforms they support via the `platforms` frontmatter field. Skills with this field are automatically hidden from the system prompt, `skills_list()`, and slash commands on incompatible platforms.
|
| 372 |
+
|
| 373 |
+
```yaml
|
| 374 |
+
platforms: [macos] # macOS only (e.g., iMessage, Apple Reminders)
|
| 375 |
+
platforms: [macos, linux] # macOS and Linux
|
| 376 |
+
platforms: [windows] # Windows only
|
| 377 |
+
```
|
| 378 |
+
|
| 379 |
+
If the field is omitted or empty, the skill loads on all platforms (backward compatible). See `skills/apple/` for examples of macOS-only skills.
|
| 380 |
+
|
| 381 |
+
### Conditional skill activation
|
| 382 |
+
|
| 383 |
+
Skills can declare conditions that control when they appear in the system prompt, based on which tools and toolsets are available in the current session. This is primarily used for **fallback skills** — alternatives that should only be shown when a primary tool is unavailable.
|
| 384 |
+
|
| 385 |
+
Four fields are supported under `metadata.hermes`:
|
| 386 |
+
|
| 387 |
+
```yaml
|
| 388 |
+
metadata:
|
| 389 |
+
hermes:
|
| 390 |
+
fallback_for_toolsets: [web] # Show ONLY when these toolsets are unavailable
|
| 391 |
+
requires_toolsets: [terminal] # Show ONLY when these toolsets are available
|
| 392 |
+
fallback_for_tools: [web_search] # Show ONLY when these specific tools are unavailable
|
| 393 |
+
requires_tools: [terminal] # Show ONLY when these specific tools are available
|
| 394 |
+
```
|
| 395 |
+
|
| 396 |
+
**Semantics:**
|
| 397 |
+
- `fallback_for_*`: The skill is a backup. It is **hidden** when the listed tools/toolsets are available, and **shown** when they are unavailable. Use this for free alternatives to premium tools.
|
| 398 |
+
- `requires_*`: The skill needs certain tools to function. It is **hidden** when the listed tools/toolsets are unavailable. Use this for skills that depend on specific capabilities (e.g., a skill that only makes sense with terminal access).
|
| 399 |
+
- If both are specified, both conditions must be satisfied for the skill to appear.
|
| 400 |
+
- If neither is specified, the skill is always shown (backward compatible).
|
| 401 |
+
|
| 402 |
+
**Examples:**
|
| 403 |
+
|
| 404 |
+
```yaml
|
| 405 |
+
# DuckDuckGo search — shown when Firecrawl (web toolset) is unavailable
|
| 406 |
+
metadata:
|
| 407 |
+
hermes:
|
| 408 |
+
fallback_for_toolsets: [web]
|
| 409 |
+
|
| 410 |
+
# Smart home skill — only useful when terminal is available
|
| 411 |
+
metadata:
|
| 412 |
+
hermes:
|
| 413 |
+
requires_toolsets: [terminal]
|
| 414 |
+
|
| 415 |
+
# Local browser fallback — shown when Browserbase is unavailable
|
| 416 |
+
metadata:
|
| 417 |
+
hermes:
|
| 418 |
+
fallback_for_toolsets: [browser]
|
| 419 |
+
```
|
| 420 |
+
|
| 421 |
+
The filtering happens at prompt build time in `agent/prompt_builder.py`. The `build_skills_system_prompt()` function receives the set of available tools and toolsets from the agent and uses `_skill_should_show()` to evaluate each skill's conditions.
|
| 422 |
+
|
| 423 |
+
### Skill setup metadata
|
| 424 |
+
|
| 425 |
+
Skills can declare secure setup-on-load metadata via the `required_environment_variables` frontmatter field. Missing values do not hide the skill from discovery; they trigger a CLI-only secure prompt when the skill is actually loaded.
|
| 426 |
+
|
| 427 |
+
```yaml
|
| 428 |
+
required_environment_variables:
|
| 429 |
+
- name: TENOR_API_KEY
|
| 430 |
+
prompt: Tenor API key
|
| 431 |
+
help: Get a key from https://developers.google.com/tenor
|
| 432 |
+
required_for: full functionality
|
| 433 |
+
```
|
| 434 |
+
|
| 435 |
+
The user may skip setup and keep loading the skill. Hermes only exposes metadata (`stored_as`, `skipped`, `validated`) to the model — never the secret value.
|
| 436 |
+
|
| 437 |
+
Legacy `prerequisites.env_vars` remains supported and is normalized into the new representation.
|
| 438 |
+
|
| 439 |
+
```yaml
|
| 440 |
+
prerequisites:
|
| 441 |
+
env_vars: [TENOR_API_KEY] # Legacy alias for required_environment_variables
|
| 442 |
+
commands: [curl, jq] # Advisory CLI checks
|
| 443 |
+
```
|
| 444 |
+
|
| 445 |
+
Gateway and messaging sessions never collect secrets in-band; they instruct the user to run `hermes setup` or update `~/.hermes/.env` locally.
|
| 446 |
+
|
| 447 |
+
**When to declare required environment variables:**
|
| 448 |
+
- The skill uses an API key or token that should be collected securely at load time
|
| 449 |
+
- The skill can still be useful if the user skips setup, but may degrade gracefully
|
| 450 |
+
|
| 451 |
+
**When to declare command prerequisites:**
|
| 452 |
+
- The skill relies on a CLI tool that may not be installed (e.g., `himalaya`, `openhue`, `ddgs`)
|
| 453 |
+
- Treat command checks as guidance, not discovery-time hiding
|
| 454 |
+
|
| 455 |
+
See `skills/gifs/gif-search/` and `skills/email/himalaya/` for examples.
|
| 456 |
+
|
| 457 |
+
### Skill guidelines
|
| 458 |
+
|
| 459 |
+
- **No external dependencies unless absolutely necessary.** Prefer stdlib Python, curl, and existing Hermes tools (`web_extract`, `terminal`, `read_file`).
|
| 460 |
+
- **Progressive disclosure.** Put the most common workflow first. Edge cases and advanced usage go at the bottom.
|
| 461 |
+
- **Include helper scripts** for XML/JSON parsing or complex logic — don't expect the LLM to write parsers inline every time.
|
| 462 |
+
- **Test it.** Run `hermes --toolsets skills -q "Use the X skill to do Y"` and verify the agent follows the instructions correctly.
|
| 463 |
+
|
| 464 |
+
---
|
| 465 |
+
|
| 466 |
+
## Adding a Skin / Theme
|
| 467 |
+
|
| 468 |
+
Hermes uses a data-driven skin system — no code changes needed to add a new skin.
|
| 469 |
+
|
| 470 |
+
**Option A: User skin (YAML file)**
|
| 471 |
+
|
| 472 |
+
Create `~/.hermes/skins/<name>.yaml`:
|
| 473 |
+
|
| 474 |
+
```yaml
|
| 475 |
+
name: mytheme
|
| 476 |
+
description: Short description of the theme
|
| 477 |
+
|
| 478 |
+
colors:
|
| 479 |
+
banner_border: "#HEX" # Panel border color
|
| 480 |
+
banner_title: "#HEX" # Panel title color
|
| 481 |
+
banner_accent: "#HEX" # Section header color
|
| 482 |
+
banner_dim: "#HEX" # Muted/dim text color
|
| 483 |
+
banner_text: "#HEX" # Body text color
|
| 484 |
+
response_border: "#HEX" # Response box border
|
| 485 |
+
|
| 486 |
+
spinner:
|
| 487 |
+
waiting_faces: ["(⚔)", "(⛨)"]
|
| 488 |
+
thinking_faces: ["(⚔)", "(⌁)"]
|
| 489 |
+
thinking_verbs: ["forging", "plotting"]
|
| 490 |
+
wings: # Optional left/right decorations
|
| 491 |
+
- ["⟪⚔", "⚔⟫"]
|
| 492 |
+
|
| 493 |
+
branding:
|
| 494 |
+
agent_name: "My Agent"
|
| 495 |
+
welcome: "Welcome message"
|
| 496 |
+
response_label: " ⚔ Agent "
|
| 497 |
+
prompt_symbol: "⚔ ❯ "
|
| 498 |
+
|
| 499 |
+
tool_prefix: "╎" # Tool output line prefix
|
| 500 |
+
```
|
| 501 |
+
|
| 502 |
+
All fields are optional — missing values inherit from the default skin.
|
| 503 |
+
|
| 504 |
+
**Option B: Built-in skin**
|
| 505 |
+
|
| 506 |
+
Add to `_BUILTIN_SKINS` dict in `hermes_cli/skin_engine.py`. Use the same schema as above but as a Python dict. Built-in skins ship with the package and are always available.
|
| 507 |
+
|
| 508 |
+
**Activating:**
|
| 509 |
+
- CLI: `/skin mytheme` or set `display.skin: mytheme` in config.yaml
|
| 510 |
+
- Config: `display: { skin: mytheme }`
|
| 511 |
+
|
| 512 |
+
See `hermes_cli/skin_engine.py` for the full schema and existing skins as examples.
|
| 513 |
+
|
| 514 |
+
---
|
| 515 |
+
|
| 516 |
+
## Cross-Platform Compatibility
|
| 517 |
+
|
| 518 |
+
Hermes runs on Linux, macOS, and Windows. When writing code that touches the OS:
|
| 519 |
+
|
| 520 |
+
### Critical rules
|
| 521 |
+
|
| 522 |
+
1. **`termios` and `fcntl` are Unix-only.** Always catch both `ImportError` and `NotImplementedError`:
|
| 523 |
+
```python
|
| 524 |
+
try:
|
| 525 |
+
from simple_term_menu import TerminalMenu
|
| 526 |
+
menu = TerminalMenu(options)
|
| 527 |
+
idx = menu.show()
|
| 528 |
+
except (ImportError, NotImplementedError):
|
| 529 |
+
# Fallback: numbered menu for Windows
|
| 530 |
+
for i, opt in enumerate(options):
|
| 531 |
+
print(f" {i+1}. {opt}")
|
| 532 |
+
idx = int(input("Choice: ")) - 1
|
| 533 |
+
```
|
| 534 |
+
|
| 535 |
+
2. **File encoding.** Windows may save `.env` files in `cp1252`. Always handle encoding errors:
|
| 536 |
+
```python
|
| 537 |
+
try:
|
| 538 |
+
load_dotenv(env_path)
|
| 539 |
+
except UnicodeDecodeError:
|
| 540 |
+
load_dotenv(env_path, encoding="latin-1")
|
| 541 |
+
```
|
| 542 |
+
|
| 543 |
+
3. **Process management.** `os.setsid()`, `os.killpg()`, and signal handling differ on Windows. Use platform checks:
|
| 544 |
+
```python
|
| 545 |
+
import platform
|
| 546 |
+
if platform.system() != "Windows":
|
| 547 |
+
kwargs["preexec_fn"] = os.setsid
|
| 548 |
+
```
|
| 549 |
+
|
| 550 |
+
4. **Path separators.** Use `pathlib.Path` instead of string concatenation with `/`.
|
| 551 |
+
|
| 552 |
+
5. **Shell commands in installers.** If you change `scripts/install.sh`, check if the equivalent change is needed in `scripts/install.ps1`.
|
| 553 |
+
|
| 554 |
+
---
|
| 555 |
+
|
| 556 |
+
## Security Considerations
|
| 557 |
+
|
| 558 |
+
Hermes has terminal access. Security matters.
|
| 559 |
+
|
| 560 |
+
### Existing protections
|
| 561 |
+
|
| 562 |
+
| Layer | Implementation |
|
| 563 |
+
|-------|---------------|
|
| 564 |
+
| **Sudo password piping** | Uses `shlex.quote()` to prevent shell injection |
|
| 565 |
+
| **Dangerous command detection** | Regex patterns in `tools/approval.py` with user approval flow |
|
| 566 |
+
| **Cron prompt injection** | Scanner in `tools/cronjob_tools.py` blocks instruction-override patterns |
|
| 567 |
+
| **Write deny list** | Protected paths (`~/.ssh/authorized_keys`, `/etc/shadow`) resolved via `os.path.realpath()` to prevent symlink bypass |
|
| 568 |
+
| **Skills guard** | Security scanner for hub-installed skills (`tools/skills_guard.py`) |
|
| 569 |
+
| **Code execution sandbox** | `execute_code` child process runs with API keys stripped from environment |
|
| 570 |
+
| **Container hardening** | Docker: all capabilities dropped, no privilege escalation, PID limits, size-limited tmpfs |
|
| 571 |
+
|
| 572 |
+
### When contributing security-sensitive code
|
| 573 |
+
|
| 574 |
+
- **Always use `shlex.quote()`** when interpolating user input into shell commands
|
| 575 |
+
- **Resolve symlinks** with `os.path.realpath()` before path-based access control checks
|
| 576 |
+
- **Don't log secrets.** API keys, tokens, and passwords should never appear in log output
|
| 577 |
+
- **Catch broad exceptions** around tool execution so a single failure doesn't crash the agent loop
|
| 578 |
+
- **Test on all platforms** if your change touches file paths, process management, or shell commands
|
| 579 |
+
|
| 580 |
+
If your PR affects security, note it explicitly in the description.
|
| 581 |
+
|
| 582 |
+
---
|
| 583 |
+
|
| 584 |
+
## Pull Request Process
|
| 585 |
+
|
| 586 |
+
### Branch naming
|
| 587 |
+
|
| 588 |
+
```
|
| 589 |
+
fix/description # Bug fixes
|
| 590 |
+
feat/description # New features
|
| 591 |
+
docs/description # Documentation
|
| 592 |
+
test/description # Tests
|
| 593 |
+
refactor/description # Code restructuring
|
| 594 |
+
```
|
| 595 |
+
|
| 596 |
+
### Before submitting
|
| 597 |
+
|
| 598 |
+
1. **Run tests**: `pytest tests/ -v`
|
| 599 |
+
2. **Test manually**: Run `hermes` and exercise the code path you changed
|
| 600 |
+
3. **Check cross-platform impact**: If you touch file I/O, process management, or terminal handling, consider Windows and macOS
|
| 601 |
+
4. **Keep PRs focused**: One logical change per PR. Don't mix a bug fix with a refactor with a new feature.
|
| 602 |
+
|
| 603 |
+
### PR description
|
| 604 |
+
|
| 605 |
+
Include:
|
| 606 |
+
- **What** changed and **why**
|
| 607 |
+
- **How to test** it (reproduction steps for bugs, usage examples for features)
|
| 608 |
+
- **What platforms** you tested on
|
| 609 |
+
- Reference any related issues
|
| 610 |
+
|
| 611 |
+
### Commit messages
|
| 612 |
+
|
| 613 |
+
We use [Conventional Commits](https://www.conventionalcommits.org/):
|
| 614 |
+
|
| 615 |
+
```
|
| 616 |
+
<type>(<scope>): <description>
|
| 617 |
+
```
|
| 618 |
+
|
| 619 |
+
| Type | Use for |
|
| 620 |
+
|------|---------|
|
| 621 |
+
| `fix` | Bug fixes |
|
| 622 |
+
| `feat` | New features |
|
| 623 |
+
| `docs` | Documentation |
|
| 624 |
+
| `test` | Tests |
|
| 625 |
+
| `refactor` | Code restructuring (no behavior change) |
|
| 626 |
+
| `chore` | Build, CI, dependency updates |
|
| 627 |
+
|
| 628 |
+
Scopes: `cli`, `gateway`, `tools`, `skills`, `agent`, `install`, `whatsapp`, `security`, etc.
|
| 629 |
+
|
| 630 |
+
Examples:
|
| 631 |
+
```
|
| 632 |
+
fix(cli): prevent crash in save_config_value when model is a string
|
| 633 |
+
feat(gateway): add WhatsApp multi-user session isolation
|
| 634 |
+
fix(security): prevent shell injection in sudo password piping
|
| 635 |
+
test(tools): add unit tests for file_operations
|
| 636 |
+
```
|
| 637 |
+
|
| 638 |
+
---
|
| 639 |
+
|
| 640 |
+
## Reporting Issues
|
| 641 |
+
|
| 642 |
+
- Use [GitHub Issues](https://github.com/NousResearch/hermes-agent/issues)
|
| 643 |
+
- Include: OS, Python version, Hermes version (`hermes version`), full error traceback
|
| 644 |
+
- Include steps to reproduce
|
| 645 |
+
- Check existing issues before creating duplicates
|
| 646 |
+
- For security vulnerabilities, please report privately
|
| 647 |
+
|
| 648 |
+
---
|
| 649 |
+
|
| 650 |
+
## Community
|
| 651 |
+
|
| 652 |
+
- **Discord**: [discord.gg/NousResearch](https://discord.gg/NousResearch) — for questions, showcasing projects, and sharing skills
|
| 653 |
+
- **GitHub Discussions**: For design proposals and architecture discussions
|
| 654 |
+
- **Skills Hub**: Upload specialized skills to a registry and share them with the community
|
| 655 |
+
|
| 656 |
+
---
|
| 657 |
+
|
| 658 |
+
## License
|
| 659 |
+
|
| 660 |
+
By contributing, you agree that your contributions will be licensed under the [MIT License](LICENSE).
|
Dockerfile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM debian:13.4
|
| 2 |
+
|
| 3 |
+
# Disable Python stdout buffering to ensure logs are printed immediately
|
| 4 |
+
ENV PYTHONUNBUFFERED=1
|
| 5 |
+
|
| 6 |
+
# Install system dependencies in one layer, clear APT cache
|
| 7 |
+
RUN apt-get update && \
|
| 8 |
+
apt-get install -y --no-install-recommends \
|
| 9 |
+
build-essential nodejs npm python3 python3-pip ripgrep ffmpeg gcc python3-dev libffi-dev procps && \
|
| 10 |
+
rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
COPY . /opt/hermes
|
| 13 |
+
WORKDIR /opt/hermes
|
| 14 |
+
|
| 15 |
+
# Install Python and Node dependencies in one layer, no cache
|
| 16 |
+
RUN pip install --no-cache-dir uv --break-system-packages && \
|
| 17 |
+
uv pip install --system --break-system-packages --no-cache -e ".[all]" && \
|
| 18 |
+
npm install --prefer-offline --no-audit && \
|
| 19 |
+
npx playwright install --with-deps chromium --only-shell && \
|
| 20 |
+
cd /opt/hermes/scripts/whatsapp-bridge && \
|
| 21 |
+
npm install --prefer-offline --no-audit && \
|
| 22 |
+
npm cache clean --force
|
| 23 |
+
|
| 24 |
+
WORKDIR /opt/hermes
|
| 25 |
+
RUN chmod +x /opt/hermes/docker/entrypoint.sh
|
| 26 |
+
|
| 27 |
+
ENV HERMES_HOME=/opt/data
|
| 28 |
+
VOLUME [ "/opt/data" ]
|
| 29 |
+
ENTRYPOINT [ "/opt/hermes/docker/entrypoint.sh" ]
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 Nous Research
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
MANIFEST.in
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
graft skills
|
| 2 |
+
graft optional-skills
|
| 3 |
+
global-exclude __pycache__
|
| 4 |
+
global-exclude *.py[cod]
|
README.md
CHANGED
|
@@ -1,10 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
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com/docs/"><img src="https://img.shields.io/badge/Docs-hermes--agent.nousresearch.com-FFD700?style=for-the-badge" alt="Documentation"></a>
|
| 9 |
+
<a href="https://discord.gg/NousResearch"><img src="https://img.shields.io/badge/Discord-5865F2?style=for-the-badge&logo=discord&logoColor=white" alt="Discord"></a>
|
| 10 |
+
<a href="https://github.com/NousResearch/hermes-agent/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" alt="License: MIT"></a>
|
| 11 |
+
<a href="https://nousresearch.com"><img src="https://img.shields.io/badge/Built%20by-Nous%20Research-blueviolet?style=for-the-badge" alt="Built by Nous Research"></a>
|
| 12 |
+
</p>
|
| 13 |
+
|
| 14 |
+
**The self-improving AI agent built by [Nous Research](https://nousresearch.com).** It's the only agent with a built-in learning loop — it creates skills from experience, improves them during use, nudges itself to persist knowledge, searches its own past conversations, and builds a deepening model of who you are across sessions. Run it on a $5 VPS, a GPU cluster, or serverless infrastructure that costs nearly nothing when idle. It's not tied to your laptop — talk to it from Telegram while it works on a cloud VM.
|
| 15 |
+
|
| 16 |
+
Use any model you want — [Nous Portal](https://portal.nousresearch.com), [OpenRouter](https://openrouter.ai) (200+ models), [z.ai/GLM](https://z.ai), [Kimi/Moonshot](https://platform.moonshot.ai), [MiniMax](https://www.minimax.io), OpenAI, or your own endpoint. Switch with `hermes model` — no code changes, no lock-in.
|
| 17 |
+
|
| 18 |
+
<table>
|
| 19 |
+
<tr><td><b>A real terminal interface</b></td><td>Full TUI with multiline editing, slash-command autocomplete, conversation history, interrupt-and-redirect, and streaming tool output.</td></tr>
|
| 20 |
+
<tr><td><b>Lives where you do</b></td><td>Telegram, Discord, Slack, WhatsApp, Signal, and CLI — all from a single gateway process. Voice memo transcription, cross-platform conversation continuity.</td></tr>
|
| 21 |
+
<tr><td><b>A closed learning loop</b></td><td>Agent-curated memory with periodic nudges. Autonomous skill creation after complex tasks. Skills self-improve during use. FTS5 session search with LLM summarization for cross-session recall. <a href="https://github.com/plastic-labs/honcho">Honcho</a> dialectic user modeling. Compatible with the <a href="https://agentskills.io">agentskills.io</a> open standard.</td></tr>
|
| 22 |
+
<tr><td><b>Scheduled automations</b></td><td>Built-in cron scheduler with delivery to any platform. Daily reports, nightly backups, weekly audits — all in natural language, running unattended.</td></tr>
|
| 23 |
+
<tr><td><b>Delegates and parallelizes</b></td><td>Spawn isolated subagents for parallel workstreams. Write Python scripts that call tools via RPC, collapsing multi-step pipelines into zero-context-cost turns.</td></tr>
|
| 24 |
+
<tr><td><b>Runs anywhere, not just your laptop</b></td><td>Six terminal backends — local, Docker, SSH, Daytona, Singularity, and Modal. Daytona and Modal offer serverless persistence — your agent's environment hibernates when idle and wakes on demand, costing nearly nothing between sessions. Run it on a $5 VPS or a GPU cluster.</td></tr>
|
| 25 |
+
<tr><td><b>Research-ready</b></td><td>Batch trajectory generation, Atropos RL environments, trajectory compression for training the next generation of tool-calling models.</td></tr>
|
| 26 |
+
</table>
|
| 27 |
+
|
| 28 |
---
|
| 29 |
+
|
| 30 |
+
## Quick Install
|
| 31 |
+
|
| 32 |
+
```bash
|
| 33 |
+
curl -fsSL https://raw.githubusercontent.com/NousResearch/hermes-agent/main/scripts/install.sh | bash
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
Works on Linux, macOS, WSL2, and Android via Termux. The installer handles the platform-specific setup for you.
|
| 37 |
+
|
| 38 |
+
> **Android / Termux:** The tested manual path is documented in the [Termux guide](https://hermes-agent.nousresearch.com/docs/getting-started/termux). On Termux, Hermes installs a curated `.[termux]` extra because the full `.[all]` extra currently pulls Android-incompatible voice dependencies.
|
| 39 |
+
>
|
| 40 |
+
> **Windows:** Native Windows is not supported. Please install [WSL2](https://learn.microsoft.com/en-us/windows/wsl/install) and run the command above.
|
| 41 |
+
|
| 42 |
+
After installation:
|
| 43 |
+
|
| 44 |
+
```bash
|
| 45 |
+
source ~/.bashrc # reload shell (or: source ~/.zshrc)
|
| 46 |
+
hermes # start chatting!
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
---
|
| 50 |
|
| 51 |
+
## Getting Started
|
| 52 |
+
|
| 53 |
+
```bash
|
| 54 |
+
hermes # Interactive CLI — start a conversation
|
| 55 |
+
hermes model # Choose your LLM provider and model
|
| 56 |
+
hermes tools # Configure which tools are enabled
|
| 57 |
+
hermes config set # Set individual config values
|
| 58 |
+
hermes gateway # Start the messaging gateway (Telegram, Discord, etc.)
|
| 59 |
+
hermes setup # Run the full setup wizard (configures everything at once)
|
| 60 |
+
hermes claw migrate # Migrate from OpenClaw (if coming from OpenClaw)
|
| 61 |
+
hermes update # Update to the latest version
|
| 62 |
+
hermes doctor # Diagnose any issues
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
📖 **[Full documentation →](https://hermes-agent.nousresearch.com/docs/)**
|
| 66 |
+
|
| 67 |
+
## CLI vs Messaging Quick Reference
|
| 68 |
+
|
| 69 |
+
Hermes has two entry points: start the terminal UI with `hermes`, or run the gateway and talk to it from Telegram, Discord, Slack, WhatsApp, Signal, or Email. Once you're in a conversation, many slash commands are shared across both interfaces.
|
| 70 |
+
|
| 71 |
+
| Action | CLI | Messaging platforms |
|
| 72 |
+
|---------|-----|---------------------|
|
| 73 |
+
| Start chatting | `hermes` | Run `hermes gateway setup` + `hermes gateway start`, then send the bot a message |
|
| 74 |
+
| Start fresh conversation | `/new` or `/reset` | `/new` or `/reset` |
|
| 75 |
+
| Change model | `/model [provider:model]` | `/model [provider:model]` |
|
| 76 |
+
| Set a personality | `/personality [name]` | `/personality [name]` |
|
| 77 |
+
| Retry or undo the last turn | `/retry`, `/undo` | `/retry`, `/undo` |
|
| 78 |
+
| Compress context / check usage | `/compress`, `/usage`, `/insights [--days N]` | `/compress`, `/usage`, `/insights [days]` |
|
| 79 |
+
| Browse skills | `/skills` or `/<skill-name>` | `/skills` or `/<skill-name>` |
|
| 80 |
+
| Interrupt current work | `Ctrl+C` or send a new message | `/stop` or send a new message |
|
| 81 |
+
| Platform-specific status | `/platforms` | `/status`, `/sethome` |
|
| 82 |
+
|
| 83 |
+
For the full command lists, see the [CLI guide](https://hermes-agent.nousresearch.com/docs/user-guide/cli) and the [Messaging Gateway guide](https://hermes-agent.nousresearch.com/docs/user-guide/messaging).
|
| 84 |
+
|
| 85 |
+
---
|
| 86 |
+
|
| 87 |
+
## Documentation
|
| 88 |
+
|
| 89 |
+
All documentation lives at **[hermes-agent.nousresearch.com/docs](https://hermes-agent.nousresearch.com/docs/)**:
|
| 90 |
+
|
| 91 |
+
| Section | What's Covered |
|
| 92 |
+
|---------|---------------|
|
| 93 |
+
| [Quickstart](https://hermes-agent.nousresearch.com/docs/getting-started/quickstart) | Install → setup → first conversation in 2 minutes |
|
| 94 |
+
| [CLI Usage](https://hermes-agent.nousresearch.com/docs/user-guide/cli) | Commands, keybindings, personalities, sessions |
|
| 95 |
+
| [Configuration](https://hermes-agent.nousresearch.com/docs/user-guide/configuration) | Config file, providers, models, all options |
|
| 96 |
+
| [Messaging Gateway](https://hermes-agent.nousresearch.com/docs/user-guide/messaging) | Telegram, Discord, Slack, WhatsApp, Signal, Home Assistant |
|
| 97 |
+
| [Security](https://hermes-agent.nousresearch.com/docs/user-guide/security) | Command approval, DM pairing, container isolation |
|
| 98 |
+
| [Tools & Toolsets](https://hermes-agent.nousresearch.com/docs/user-guide/features/tools) | 40+ tools, toolset system, terminal backends |
|
| 99 |
+
| [Skills System](https://hermes-agent.nousresearch.com/docs/user-guide/features/skills) | Procedural memory, Skills Hub, creating skills |
|
| 100 |
+
| [Memory](https://hermes-agent.nousresearch.com/docs/user-guide/features/memory) | Persistent memory, user profiles, best practices |
|
| 101 |
+
| [MCP Integration](https://hermes-agent.nousresearch.com/docs/user-guide/features/mcp) | Connect any MCP server for extended capabilities |
|
| 102 |
+
| [Cron Scheduling](https://hermes-agent.nousresearch.com/docs/user-guide/features/cron) | Scheduled tasks with platform delivery |
|
| 103 |
+
| [Context Files](https://hermes-agent.nousresearch.com/docs/user-guide/features/context-files) | Project context that shapes every conversation |
|
| 104 |
+
| [Architecture](https://hermes-agent.nousresearch.com/docs/developer-guide/architecture) | Project structure, agent loop, key classes |
|
| 105 |
+
| [Contributing](https://hermes-agent.nousresearch.com/docs/developer-guide/contributing) | Development setup, PR process, code style |
|
| 106 |
+
| [CLI Reference](https://hermes-agent.nousresearch.com/docs/reference/cli-commands) | All commands and flags |
|
| 107 |
+
| [Environment Variables](https://hermes-agent.nousresearch.com/docs/reference/environment-variables) | Complete env var reference |
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
## Migrating from OpenClaw
|
| 112 |
+
|
| 113 |
+
If you're coming from OpenClaw, Hermes can automatically import your settings, memories, skills, and API keys.
|
| 114 |
+
|
| 115 |
+
**During first-time setup:** The setup wizard (`hermes setup`) automatically detects `~/.openclaw` and offers to migrate before configuration begins.
|
| 116 |
+
|
| 117 |
+
**Anytime after install:**
|
| 118 |
+
|
| 119 |
+
```bash
|
| 120 |
+
hermes claw migrate # Interactive migration (full preset)
|
| 121 |
+
hermes claw migrate --dry-run # Preview what would be migrated
|
| 122 |
+
hermes claw migrate --preset user-data # Migrate without secrets
|
| 123 |
+
hermes claw migrate --overwrite # Overwrite existing conflicts
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
What gets imported:
|
| 127 |
+
- **SOUL.md** — persona file
|
| 128 |
+
- **Memories** — MEMORY.md and USER.md entries
|
| 129 |
+
- **Skills** — user-created skills → `~/.hermes/skills/openclaw-imports/`
|
| 130 |
+
- **Command allowlist** — approval patterns
|
| 131 |
+
- **Messaging settings** — platform configs, allowed users, working directory
|
| 132 |
+
- **API keys** — allowlisted secrets (Telegram, OpenRouter, OpenAI, Anthropic, ElevenLabs)
|
| 133 |
+
- **TTS assets** — workspace audio files
|
| 134 |
+
- **Workspace instructions** — AGENTS.md (with `--workspace-target`)
|
| 135 |
+
|
| 136 |
+
See `hermes claw migrate --help` for all options, or use the `openclaw-migration` skill for an interactive agent-guided migration with dry-run previews.
|
| 137 |
+
|
| 138 |
+
---
|
| 139 |
+
|
| 140 |
+
## Contributing
|
| 141 |
+
|
| 142 |
+
We welcome contributions! See the [Contributing Guide](https://hermes-agent.nousresearch.com/docs/developer-guide/contributing) for development setup, code style, and PR process.
|
| 143 |
+
|
| 144 |
+
Quick start for contributors:
|
| 145 |
+
|
| 146 |
+
```bash
|
| 147 |
+
git clone https://github.com/NousResearch/hermes-agent.git
|
| 148 |
+
cd hermes-agent
|
| 149 |
+
curl -LsSf https://astral.sh/uv/install.sh | sh
|
| 150 |
+
uv venv venv --python 3.11
|
| 151 |
+
source venv/bin/activate
|
| 152 |
+
uv pip install -e ".[all,dev]"
|
| 153 |
+
python -m pytest tests/ -q
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
> **RL Training (optional):** To work on the RL/Tinker-Atropos integration:
|
| 157 |
+
> ```bash
|
| 158 |
+
> git submodule update --init tinker-atropos
|
| 159 |
+
> uv pip install -e "./tinker-atropos"
|
| 160 |
+
> ```
|
| 161 |
+
|
| 162 |
+
---
|
| 163 |
+
|
| 164 |
+
## Community
|
| 165 |
+
|
| 166 |
+
- 💬 [Discord](https://discord.gg/NousResearch)
|
| 167 |
+
- 📚 [Skills Hub](https://agentskills.io)
|
| 168 |
+
- 🐛 [Issues](https://github.com/NousResearch/hermes-agent/issues)
|
| 169 |
+
- 💡 [Discussions](https://github.com/NousResearch/hermes-agent/discussions)
|
| 170 |
+
|
| 171 |
+
---
|
| 172 |
+
|
| 173 |
+
## License
|
| 174 |
+
|
| 175 |
+
MIT — see [LICENSE](LICENSE).
|
| 176 |
+
|
| 177 |
+
Built by [Nous Research](https://nousresearch.com).
|
RELEASE_v0.2.0.md
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hermes Agent v0.2.0 (v2026.3.12)
|
| 2 |
+
|
| 3 |
+
**Release Date:** March 12, 2026
|
| 4 |
+
|
| 5 |
+
> First tagged release since v0.1.0 (the initial pre-public foundation). In just over two weeks, Hermes Agent went from a small internal project to a full-featured AI agent platform — thanks to an explosion of community contributions. This release covers **216 merged pull requests** from **63 contributors**, resolving **119 issues**.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## ✨ Highlights
|
| 10 |
+
|
| 11 |
+
- **Multi-Platform Messaging Gateway** — Telegram, Discord, Slack, WhatsApp, Signal, Email (IMAP/SMTP), and Home Assistant platforms with unified session management, media attachments, and per-platform tool configuration.
|
| 12 |
+
|
| 13 |
+
- **MCP (Model Context Protocol) Client** — Native MCP support with stdio and HTTP transports, reconnection, resource/prompt discovery, and sampling (server-initiated LLM requests). ([#291](https://github.com/NousResearch/hermes-agent/pull/291) — @0xbyt4, [#301](https://github.com/NousResearch/hermes-agent/pull/301), [#753](https://github.com/NousResearch/hermes-agent/pull/753))
|
| 14 |
+
|
| 15 |
+
- **Skills Ecosystem** — 70+ bundled and optional skills across 15+ categories with a Skills Hub for community discovery, per-platform enable/disable, conditional activation based on tool availability, and prerequisite validation. ([#743](https://github.com/NousResearch/hermes-agent/pull/743) — @teyrebaz33, [#785](https://github.com/NousResearch/hermes-agent/pull/785) — @teyrebaz33)
|
| 16 |
+
|
| 17 |
+
- **Centralized Provider Router** — Unified `call_llm()`/`async_call_llm()` API replaces scattered provider logic across vision, summarization, compression, and trajectory saving. All auxiliary consumers route through a single code path with automatic credential resolution. ([#1003](https://github.com/NousResearch/hermes-agent/pull/1003))
|
| 18 |
+
|
| 19 |
+
- **ACP Server** — VS Code, Zed, and JetBrains editor integration via the Agent Communication Protocol standard. ([#949](https://github.com/NousResearch/hermes-agent/pull/949))
|
| 20 |
+
|
| 21 |
+
- **CLI Skin/Theme Engine** — Data-driven visual customization: banners, spinners, colors, branding. 7 built-in skins + custom YAML skins.
|
| 22 |
+
|
| 23 |
+
- **Git Worktree Isolation** — `hermes -w` launches isolated agent sessions in git worktrees for safe parallel work on the same repo. ([#654](https://github.com/NousResearch/hermes-agent/pull/654))
|
| 24 |
+
|
| 25 |
+
- **Filesystem Checkpoints & Rollback** — Automatic snapshots before destructive operations with `/rollback` to restore. ([#824](https://github.com/NousResearch/hermes-agent/pull/824))
|
| 26 |
+
|
| 27 |
+
- **3,289 Tests** — From near-zero test coverage to a comprehensive test suite covering agent, gateway, tools, cron, and CLI.
|
| 28 |
+
|
| 29 |
+
---
|
| 30 |
+
|
| 31 |
+
## 🏗️ Core Agent & Architecture
|
| 32 |
+
|
| 33 |
+
### Provider & Model Support
|
| 34 |
+
- Centralized provider router with `resolve_provider_client()` + `call_llm()` API ([#1003](https://github.com/NousResearch/hermes-agent/pull/1003))
|
| 35 |
+
- Nous Portal as first-class provider in setup ([#644](https://github.com/NousResearch/hermes-agent/issues/644))
|
| 36 |
+
- OpenAI Codex (Responses API) with ChatGPT subscription support ([#43](https://github.com/NousResearch/hermes-agent/pull/43)) — @grp06
|
| 37 |
+
- Codex OAuth vision support + multimodal content adapter
|
| 38 |
+
- Validate `/model` against live API instead of hardcoded lists
|
| 39 |
+
- Self-hosted Firecrawl support ([#460](https://github.com/NousResearch/hermes-agent/pull/460)) — @caentzminger
|
| 40 |
+
- Kimi Code API support ([#635](https://github.com/NousResearch/hermes-agent/pull/635)) — @christomitov
|
| 41 |
+
- MiniMax model ID update ([#473](https://github.com/NousResearch/hermes-agent/pull/473)) — @tars90percent
|
| 42 |
+
- OpenRouter provider routing configuration (provider_preferences)
|
| 43 |
+
- Nous credential refresh on 401 errors ([#571](https://github.com/NousResearch/hermes-agent/pull/571), [#269](https://github.com/NousResearch/hermes-agent/pull/269)) — @rewbs
|
| 44 |
+
- z.ai/GLM, Kimi/Moonshot, MiniMax, Azure OpenAI as first-class providers
|
| 45 |
+
- Unified `/model` and `/provider` into single view
|
| 46 |
+
|
| 47 |
+
### Agent Loop & Conversation
|
| 48 |
+
- Simple fallback model for provider resilience ([#740](https://github.com/NousResearch/hermes-agent/pull/740))
|
| 49 |
+
- Shared iteration budget across parent + subagent delegation
|
| 50 |
+
- Iteration budget pressure via tool result injection
|
| 51 |
+
- Configurable subagent provider/model with full credential resolution
|
| 52 |
+
- Handle 413 payload-too-large via compression instead of aborting ([#153](https://github.com/NousResearch/hermes-agent/pull/153)) — @tekelala
|
| 53 |
+
- Retry with rebuilt payload after compression ([#616](https://github.com/NousResearch/hermes-agent/pull/616)) — @tripledoublev
|
| 54 |
+
- Auto-compress pathologically large gateway sessions ([#628](https://github.com/NousResearch/hermes-agent/issues/628))
|
| 55 |
+
- Tool call repair middleware — auto-lowercase and invalid tool handler
|
| 56 |
+
- Reasoning effort configuration and `/reasoning` command ([#921](https://github.com/NousResearch/hermes-agent/pull/921))
|
| 57 |
+
- Detect and block file re-read/search loops after context compression ([#705](https://github.com/NousResearch/hermes-agent/pull/705)) — @0xbyt4
|
| 58 |
+
|
| 59 |
+
### Session & Memory
|
| 60 |
+
- Session naming with unique titles, auto-lineage, rich listing, and resume by name ([#720](https://github.com/NousResearch/hermes-agent/pull/720))
|
| 61 |
+
- Interactive session browser with search filtering ([#733](https://github.com/NousResearch/hermes-agent/pull/733))
|
| 62 |
+
- Display previous messages when resuming a session ([#734](https://github.com/NousResearch/hermes-agent/pull/734))
|
| 63 |
+
- Honcho AI-native cross-session user modeling ([#38](https://github.com/NousResearch/hermes-agent/pull/38)) — @erosika
|
| 64 |
+
- Proactive async memory flush on session expiry
|
| 65 |
+
- Smart context length probing with persistent caching + banner display
|
| 66 |
+
- `/resume` command for switching to named sessions in gateway
|
| 67 |
+
- Session reset policy for messaging platforms
|
| 68 |
+
|
| 69 |
+
---
|
| 70 |
+
|
| 71 |
+
## 📱 Messaging Platforms (Gateway)
|
| 72 |
+
|
| 73 |
+
### Telegram
|
| 74 |
+
- Native file attachments: send_document + send_video
|
| 75 |
+
- Document file processing for PDF, text, and Office files — @tekelala
|
| 76 |
+
- Forum topic session isolation ([#766](https://github.com/NousResearch/hermes-agent/pull/766)) — @spanishflu-est1918
|
| 77 |
+
- Browser screenshot sharing via MEDIA: protocol ([#657](https://github.com/NousResearch/hermes-agent/pull/657))
|
| 78 |
+
- Location support for find-nearby skill
|
| 79 |
+
- TTS voice message accumulation fix ([#176](https://github.com/NousResearch/hermes-agent/pull/176)) — @Bartok9
|
| 80 |
+
- Improved error handling and logging ([#763](https://github.com/NousResearch/hermes-agent/pull/763)) — @aydnOktay
|
| 81 |
+
- Italic regex newline fix + 43 format tests ([#204](https://github.com/NousResearch/hermes-agent/pull/204)) — @0xbyt4
|
| 82 |
+
|
| 83 |
+
### Discord
|
| 84 |
+
- Channel topic included in session context ([#248](https://github.com/NousResearch/hermes-agent/pull/248)) — @Bartok9
|
| 85 |
+
- DISCORD_ALLOW_BOTS config for bot message filtering ([#758](https://github.com/NousResearch/hermes-agent/pull/758))
|
| 86 |
+
- Document and video support ([#784](https://github.com/NousResearch/hermes-agent/pull/784))
|
| 87 |
+
- Improved error handling and logging ([#761](https://github.com/NousResearch/hermes-agent/pull/761)) — @aydnOktay
|
| 88 |
+
|
| 89 |
+
### Slack
|
| 90 |
+
- App_mention 404 fix + document/video support ([#784](https://github.com/NousResearch/hermes-agent/pull/784))
|
| 91 |
+
- Structured logging replacing print statements — @aydnOktay
|
| 92 |
+
|
| 93 |
+
### WhatsApp
|
| 94 |
+
- Native media sending — images, videos, documents ([#292](https://github.com/NousResearch/hermes-agent/pull/292)) — @satelerd
|
| 95 |
+
- Multi-user session isolation ([#75](https://github.com/NousResearch/hermes-agent/pull/75)) — @satelerd
|
| 96 |
+
- Cross-platform port cleanup replacing Linux-only fuser ([#433](https://github.com/NousResearch/hermes-agent/pull/433)) — @Farukest
|
| 97 |
+
- DM interrupt key mismatch fix ([#350](https://github.com/NousResearch/hermes-agent/pull/350)) — @Farukest
|
| 98 |
+
|
| 99 |
+
### Signal
|
| 100 |
+
- Full Signal messenger gateway via signal-cli-rest-api ([#405](https://github.com/NousResearch/hermes-agent/issues/405))
|
| 101 |
+
- Media URL support in message events ([#871](https://github.com/NousResearch/hermes-agent/pull/871))
|
| 102 |
+
|
| 103 |
+
### Email (IMAP/SMTP)
|
| 104 |
+
- New email gateway platform — @0xbyt4
|
| 105 |
+
|
| 106 |
+
### Home Assistant
|
| 107 |
+
- REST tools + WebSocket gateway integration ([#184](https://github.com/NousResearch/hermes-agent/pull/184)) — @0xbyt4
|
| 108 |
+
- Service discovery and enhanced setup
|
| 109 |
+
- Toolset mapping fix ([#538](https://github.com/NousResearch/hermes-agent/pull/538)) — @Himess
|
| 110 |
+
|
| 111 |
+
### Gateway Core
|
| 112 |
+
- Expose subagent tool calls and thinking to users ([#186](https://github.com/NousResearch/hermes-agent/pull/186)) — @cutepawss
|
| 113 |
+
- Configurable background process watcher notifications ([#840](https://github.com/NousResearch/hermes-agent/pull/840))
|
| 114 |
+
- `edit_message()` for Telegram/Discord/Slack with fallback
|
| 115 |
+
- `/compress`, `/usage`, `/update` slash commands
|
| 116 |
+
- Eliminated 3x SQLite message duplication in gateway sessions ([#873](https://github.com/NousResearch/hermes-agent/pull/873))
|
| 117 |
+
- Stabilize system prompt across gateway turns for cache hits ([#754](https://github.com/NousResearch/hermes-agent/pull/754))
|
| 118 |
+
- MCP server shutdown on gateway exit ([#796](https://github.com/NousResearch/hermes-agent/pull/796)) — @0xbyt4
|
| 119 |
+
- Pass session_db to AIAgent, fixing session_search error ([#108](https://github.com/NousResearch/hermes-agent/pull/108)) — @Bartok9
|
| 120 |
+
- Persist transcript changes in /retry, /undo; fix /reset attribute ([#217](https://github.com/NousResearch/hermes-agent/pull/217)) — @Farukest
|
| 121 |
+
- UTF-8 encoding fix preventing Windows crashes ([#369](https://github.com/NousResearch/hermes-agent/pull/369)) — @ch3ronsa
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## 🖥️ CLI & User Experience
|
| 126 |
+
|
| 127 |
+
### Interactive CLI
|
| 128 |
+
- Data-driven skin/theme engine — 7 built-in skins (default, ares, mono, slate, poseidon, sisyphus, charizard) + custom YAML skins
|
| 129 |
+
- `/personality` command with custom personality + disable support ([#773](https://github.com/NousResearch/hermes-agent/pull/773)) — @teyrebaz33
|
| 130 |
+
- User-defined quick commands that bypass the agent loop ([#746](https://github.com/NousResearch/hermes-agent/pull/746)) — @teyrebaz33
|
| 131 |
+
- `/reasoning` command for effort level and display toggle ([#921](https://github.com/NousResearch/hermes-agent/pull/921))
|
| 132 |
+
- `/verbose` slash command to toggle debug at runtime ([#94](https://github.com/NousResearch/hermes-agent/pull/94)) — @cesareth
|
| 133 |
+
- `/insights` command — usage analytics, cost estimation & activity patterns ([#552](https://github.com/NousResearch/hermes-agent/pull/552))
|
| 134 |
+
- `/background` command for managing background processes
|
| 135 |
+
- `/help` formatting with command categories
|
| 136 |
+
- Bell-on-complete — terminal bell when agent finishes ([#738](https://github.com/NousResearch/hermes-agent/pull/738))
|
| 137 |
+
- Up/down arrow history navigation
|
| 138 |
+
- Clipboard image paste (Alt+V / Ctrl+V)
|
| 139 |
+
- Loading indicators for slow slash commands ([#882](https://github.com/NousResearch/hermes-agent/pull/882))
|
| 140 |
+
- Spinner flickering fix under patch_stdout ([#91](https://github.com/NousResearch/hermes-agent/pull/91)) — @0xbyt4
|
| 141 |
+
- `--quiet/-Q` flag for programmatic single-query mode
|
| 142 |
+
- `--fuck-it-ship-it` flag to bypass all approval prompts ([#724](https://github.com/NousResearch/hermes-agent/pull/724)) — @dmahan93
|
| 143 |
+
- Tools summary flag ([#767](https://github.com/NousResearch/hermes-agent/pull/767)) — @luisv-1
|
| 144 |
+
- Terminal blinking fix on SSH ([#284](https://github.com/NousResearch/hermes-agent/pull/284)) — @ygd58
|
| 145 |
+
- Multi-line paste detection fix ([#84](https://github.com/NousResearch/hermes-agent/pull/84)) — @0xbyt4
|
| 146 |
+
|
| 147 |
+
### Setup & Configuration
|
| 148 |
+
- Modular setup wizard with section subcommands and tool-first UX
|
| 149 |
+
- Container resource configuration prompts
|
| 150 |
+
- Backend validation for required binaries
|
| 151 |
+
- Config migration system (currently v7)
|
| 152 |
+
- API keys properly routed to .env instead of config.yaml ([#469](https://github.com/NousResearch/hermes-agent/pull/469)) — @ygd58
|
| 153 |
+
- Atomic write for .env to prevent API key loss on crash ([#954](https://github.com/NousResearch/hermes-agent/pull/954))
|
| 154 |
+
- `hermes tools` — per-platform tool enable/disable with curses UI
|
| 155 |
+
- `hermes doctor` for health checks across all configured providers
|
| 156 |
+
- `hermes update` with auto-restart for gateway service
|
| 157 |
+
- Show update-available notice in CLI banner
|
| 158 |
+
- Multiple named custom providers
|
| 159 |
+
- Shell config detection improvement for PATH setup ([#317](https://github.com/NousResearch/hermes-agent/pull/317)) — @mehmetkr-31
|
| 160 |
+
- Consistent HERMES_HOME and .env path resolution ([#51](https://github.com/NousResearch/hermes-agent/pull/51), [#48](https://github.com/NousResearch/hermes-agent/pull/48)) — @deankerr
|
| 161 |
+
- Docker backend fix on macOS + subagent auth for Nous Portal ([#46](https://github.com/NousResearch/hermes-agent/pull/46)) — @rsavitt
|
| 162 |
+
|
| 163 |
+
---
|
| 164 |
+
|
| 165 |
+
## 🔧 Tool System
|
| 166 |
+
|
| 167 |
+
### MCP (Model Context Protocol)
|
| 168 |
+
- Native MCP client with stdio + HTTP transports ([#291](https://github.com/NousResearch/hermes-agent/pull/291) — @0xbyt4, [#301](https://github.com/NousResearch/hermes-agent/pull/301))
|
| 169 |
+
- Sampling support — server-initiated LLM requests ([#753](https://github.com/NousResearch/hermes-agent/pull/753))
|
| 170 |
+
- Resource and prompt discovery
|
| 171 |
+
- Automatic reconnection and security hardening
|
| 172 |
+
- Banner integration, `/reload-mcp` command
|
| 173 |
+
- `hermes tools` UI integration
|
| 174 |
+
|
| 175 |
+
### Browser
|
| 176 |
+
- Local browser backend — zero-cost headless Chromium (no Browserbase needed)
|
| 177 |
+
- Console/errors tool, annotated screenshots, auto-recording, dogfood QA skill ([#745](https://github.com/NousResearch/hermes-agent/pull/745))
|
| 178 |
+
- Screenshot sharing via MEDIA: on all messaging platforms ([#657](https://github.com/NousResearch/hermes-agent/pull/657))
|
| 179 |
+
|
| 180 |
+
### Terminal & Execution
|
| 181 |
+
- `execute_code` sandbox with json_parse, shell_quote, retry helpers
|
| 182 |
+
- Docker: custom volume mounts ([#158](https://github.com/NousResearch/hermes-agent/pull/158)) — @Indelwin
|
| 183 |
+
- Daytona cloud sandbox backend ([#451](https://github.com/NousResearch/hermes-agent/pull/451)) — @rovle
|
| 184 |
+
- SSH backend fix ([#59](https://github.com/NousResearch/hermes-agent/pull/59)) — @deankerr
|
| 185 |
+
- Shell noise filtering and login shell execution for environment consistency
|
| 186 |
+
- Head+tail truncation for execute_code stdout overflow
|
| 187 |
+
- Configurable background process notification modes
|
| 188 |
+
|
| 189 |
+
### File Operations
|
| 190 |
+
- Filesystem checkpoints and `/rollback` command ([#824](https://github.com/NousResearch/hermes-agent/pull/824))
|
| 191 |
+
- Structured tool result hints (next-action guidance) for patch and search_files ([#722](https://github.com/NousResearch/hermes-agent/issues/722))
|
| 192 |
+
- Docker volumes passed to sandbox container config ([#687](https://github.com/NousResearch/hermes-agent/pull/687)) — @manuelschipper
|
| 193 |
+
|
| 194 |
+
---
|
| 195 |
+
|
| 196 |
+
## 🧩 Skills Ecosystem
|
| 197 |
+
|
| 198 |
+
### Skills System
|
| 199 |
+
- Per-platform skill enable/disable ([#743](https://github.com/NousResearch/hermes-agent/pull/743)) — @teyrebaz33
|
| 200 |
+
- Conditional skill activation based on tool availability ([#785](https://github.com/NousResearch/hermes-agent/pull/785)) — @teyrebaz33
|
| 201 |
+
- Skill prerequisites — hide skills with unmet dependencies ([#659](https://github.com/NousResearch/hermes-agent/pull/659)) — @kshitijk4poor
|
| 202 |
+
- Optional skills — shipped but not activated by default
|
| 203 |
+
- `hermes skills browse` — paginated hub browsing
|
| 204 |
+
- Skills sub-category organization
|
| 205 |
+
- Platform-conditional skill loading
|
| 206 |
+
- Atomic skill file writes ([#551](https://github.com/NousResearch/hermes-agent/pull/551)) — @aydnOktay
|
| 207 |
+
- Skills sync data loss prevention ([#563](https://github.com/NousResearch/hermes-agent/pull/563)) — @0xbyt4
|
| 208 |
+
- Dynamic skill slash commands for CLI and gateway
|
| 209 |
+
|
| 210 |
+
### New Skills (selected)
|
| 211 |
+
- **ASCII Art** — pyfiglet (571 fonts), cowsay, image-to-ascii ([#209](https://github.com/NousResearch/hermes-agent/pull/209)) — @0xbyt4
|
| 212 |
+
- **ASCII Video** — Full production pipeline ([#854](https://github.com/NousResearch/hermes-agent/pull/854)) — @SHL0MS
|
| 213 |
+
- **DuckDuckGo Search** — Firecrawl fallback ([#267](https://github.com/NousResearch/hermes-agent/pull/267)) — @gamedevCloudy; DDGS API expansion ([#598](https://github.com/NousResearch/hermes-agent/pull/598)) — @areu01or00
|
| 214 |
+
- **Solana Blockchain** — Wallet balances, USD pricing, token names ([#212](https://github.com/NousResearch/hermes-agent/pull/212)) — @gizdusum
|
| 215 |
+
- **AgentMail** — Agent-owned email inboxes ([#330](https://github.com/NousResearch/hermes-agent/pull/330)) — @teyrebaz33
|
| 216 |
+
- **Polymarket** — Prediction market data (read-only) ([#629](https://github.com/NousResearch/hermes-agent/pull/629))
|
| 217 |
+
- **OpenClaw Migration** — Official migration tool ([#570](https://github.com/NousResearch/hermes-agent/pull/570)) — @unmodeled-tyler
|
| 218 |
+
- **Domain Intelligence** — Passive recon: subdomains, SSL, WHOIS, DNS ([#136](https://github.com/NousResearch/hermes-agent/pull/136)) — @FurkanL0
|
| 219 |
+
- **Superpowers** — Software development skills ([#137](https://github.com/NousResearch/hermes-agent/pull/137)) — @kaos35
|
| 220 |
+
- **Hermes-Atropos** — RL environment development skill ([#815](https://github.com/NousResearch/hermes-agent/pull/815))
|
| 221 |
+
- Plus: arXiv search, OCR/documents, Excalidraw diagrams, YouTube transcripts, GIF search, Pokémon player, Minecraft modpack server, OpenHue (Philips Hue), Google Workspace, Notion, PowerPoint, Obsidian, find-nearby, and 40+ MLOps skills
|
| 222 |
+
|
| 223 |
+
---
|
| 224 |
+
|
| 225 |
+
## 🔒 Security & Reliability
|
| 226 |
+
|
| 227 |
+
### Security Hardening
|
| 228 |
+
- Path traversal fix in skill_view — prevented reading arbitrary files ([#220](https://github.com/NousResearch/hermes-agent/issues/220)) — @Farukest
|
| 229 |
+
- Shell injection prevention in sudo password piping ([#65](https://github.com/NousResearch/hermes-agent/pull/65)) — @leonsgithub
|
| 230 |
+
- Dangerous command detection: multiline bypass fix ([#233](https://github.com/NousResearch/hermes-agent/pull/233)) — @Farukest; tee/process substitution patterns ([#280](https://github.com/NousResearch/hermes-agent/pull/280)) — @dogiladeveloper
|
| 231 |
+
- Symlink boundary check fix in skills_guard ([#386](https://github.com/NousResearch/hermes-agent/pull/386)) — @Farukest
|
| 232 |
+
- Symlink bypass fix in write deny list on macOS ([#61](https://github.com/NousResearch/hermes-agent/pull/61)) — @0xbyt4
|
| 233 |
+
- Multi-word prompt injection bypass prevention ([#192](https://github.com/NousResearch/hermes-agent/pull/192)) — @0xbyt4
|
| 234 |
+
- Cron prompt injection scanner bypass fix ([#63](https://github.com/NousResearch/hermes-agent/pull/63)) — @0xbyt4
|
| 235 |
+
- Enforce 0600/0700 file permissions on sensitive files ([#757](https://github.com/NousResearch/hermes-agent/pull/757))
|
| 236 |
+
- .env file permissions restricted to owner-only ([#529](https://github.com/NousResearch/hermes-agent/pull/529)) — @Himess
|
| 237 |
+
- `--force` flag properly blocked from overriding dangerous verdicts ([#388](https://github.com/NousResearch/hermes-agent/pull/388)) — @Farukest
|
| 238 |
+
- FTS5 query sanitization + DB connection leak fix ([#565](https://github.com/NousResearch/hermes-agent/pull/565)) — @0xbyt4
|
| 239 |
+
- Expand secret redaction patterns + config toggle to disable
|
| 240 |
+
- In-memory permanent allowlist to prevent data leak ([#600](https://github.com/NousResearch/hermes-agent/pull/600)) — @alireza78a
|
| 241 |
+
|
| 242 |
+
### Atomic Writes (data loss prevention)
|
| 243 |
+
- sessions.json ([#611](https://github.com/NousResearch/hermes-agent/pull/611)) — @alireza78a
|
| 244 |
+
- Cron jobs ([#146](https://github.com/NousResearch/hermes-agent/pull/146)) — @alireza78a
|
| 245 |
+
- .env config ([#954](https://github.com/NousResearch/hermes-agent/pull/954))
|
| 246 |
+
- Process checkpoints ([#298](https://github.com/NousResearch/hermes-agent/pull/298)) — @aydnOktay
|
| 247 |
+
- Batch runner ([#297](https://github.com/NousResearch/hermes-agent/pull/297)) — @aydnOktay
|
| 248 |
+
- Skill files ([#551](https://github.com/NousResearch/hermes-agent/pull/551)) — @aydnOktay
|
| 249 |
+
|
| 250 |
+
### Reliability
|
| 251 |
+
- Guard all print() against OSError for systemd/headless environments ([#963](https://github.com/NousResearch/hermes-agent/pull/963))
|
| 252 |
+
- Reset all retry counters at start of run_conversation ([#607](https://github.com/NousResearch/hermes-agent/pull/607)) — @0xbyt4
|
| 253 |
+
- Return deny on approval callback timeout instead of None ([#603](https://github.com/NousResearch/hermes-agent/pull/603)) — @0xbyt4
|
| 254 |
+
- Fix None message content crashes across codebase ([#277](https://github.com/NousResearch/hermes-agent/pull/277))
|
| 255 |
+
- Fix context overrun crash with local LLM backends ([#403](https://github.com/NousResearch/hermes-agent/pull/403)) — @ch3ronsa
|
| 256 |
+
- Prevent `_flush_sentinel` from leaking to external APIs ([#227](https://github.com/NousResearch/hermes-agent/pull/227)) — @Farukest
|
| 257 |
+
- Prevent conversation_history mutation in callers ([#229](https://github.com/NousResearch/hermes-agent/pull/229)) — @Farukest
|
| 258 |
+
- Fix systemd restart loop ([#614](https://github.com/NousResearch/hermes-agent/pull/614)) — @voidborne-d
|
| 259 |
+
- Close file handles and sockets to prevent fd leaks ([#568](https://github.com/NousResearch/hermes-agent/pull/568) — @alireza78a, [#296](https://github.com/NousResearch/hermes-agent/pull/296) — @alireza78a, [#709](https://github.com/NousResearch/hermes-agent/pull/709) — @memosr)
|
| 260 |
+
- Prevent data loss in clipboard PNG conversion ([#602](https://github.com/NousResearch/hermes-agent/pull/602)) — @0xbyt4
|
| 261 |
+
- Eliminate shell noise from terminal output ([#293](https://github.com/NousResearch/hermes-agent/pull/293)) — @0xbyt4
|
| 262 |
+
- Timezone-aware now() for prompt, cron, and execute_code ([#309](https://github.com/NousResearch/hermes-agent/pull/309)) — @areu01or00
|
| 263 |
+
|
| 264 |
+
### Windows Compatibility
|
| 265 |
+
- Guard POSIX-only process functions ([#219](https://github.com/NousResearch/hermes-agent/pull/219)) — @Farukest
|
| 266 |
+
- Windows native support via Git Bash + ZIP-based update fallback
|
| 267 |
+
- pywinpty for PTY support ([#457](https://github.com/NousResearch/hermes-agent/pull/457)) — @shitcoinsherpa
|
| 268 |
+
- Explicit UTF-8 encoding on all config/data file I/O ([#458](https://github.com/NousResearch/hermes-agent/pull/458)) — @shitcoinsherpa
|
| 269 |
+
- Windows-compatible path handling ([#354](https://github.com/NousResearch/hermes-agent/pull/354), [#390](https://github.com/NousResearch/hermes-agent/pull/390)) — @Farukest
|
| 270 |
+
- Regex-based search output parsing for drive-letter paths ([#533](https://github.com/NousResearch/hermes-agent/pull/533)) — @Himess
|
| 271 |
+
- Auth store file lock for Windows ([#455](https://github.com/NousResearch/hermes-agent/pull/455)) — @shitcoinsherpa
|
| 272 |
+
|
| 273 |
+
---
|
| 274 |
+
|
| 275 |
+
## 🐛 Notable Bug Fixes
|
| 276 |
+
|
| 277 |
+
- Fix DeepSeek V3 tool call parser silently dropping multi-line JSON arguments ([#444](https://github.com/NousResearch/hermes-agent/pull/444)) — @PercyDikec
|
| 278 |
+
- Fix gateway transcript losing 1 message per turn due to offset mismatch ([#395](https://github.com/NousResearch/hermes-agent/pull/395)) — @PercyDikec
|
| 279 |
+
- Fix /retry command silently discarding the agent's final response ([#441](https://github.com/NousResearch/hermes-agent/pull/441)) — @PercyDikec
|
| 280 |
+
- Fix max-iterations retry returning empty string after think-block stripping ([#438](https://github.com/NousResearch/hermes-agent/pull/438)) — @PercyDikec
|
| 281 |
+
- Fix max-iterations retry using hardcoded max_tokens ([#436](https://github.com/NousResearch/hermes-agent/pull/436)) — @Farukest
|
| 282 |
+
- Fix Codex status dict key mismatch ([#448](https://github.com/NousResearch/hermes-agent/pull/448)) and visibility filter ([#446](https://github.com/NousResearch/hermes-agent/pull/446)) — @PercyDikec
|
| 283 |
+
- Strip \<think\> blocks from final user-facing responses ([#174](https://github.com/NousResearch/hermes-agent/pull/174)) — @Bartok9
|
| 284 |
+
- Fix \<think\> block regex stripping visible content when model discusses tags literally ([#786](https://github.com/NousResearch/hermes-agent/issues/786))
|
| 285 |
+
- Fix Mistral 422 errors from leftover finish_reason in assistant messages ([#253](https://github.com/NousResearch/hermes-agent/pull/253)) — @Sertug17
|
| 286 |
+
- Fix OPENROUTER_API_KEY resolution order across all code paths ([#295](https://github.com/NousResearch/hermes-agent/pull/295)) — @0xbyt4
|
| 287 |
+
- Fix OPENAI_BASE_URL API key priority ([#420](https://github.com/NousResearch/hermes-agent/pull/420)) — @manuelschipper
|
| 288 |
+
- Fix Anthropic "prompt is too long" 400 error not detected as context length error ([#813](https://github.com/NousResearch/hermes-agent/issues/813))
|
| 289 |
+
- Fix SQLite session transcript accumulating duplicate messages — 3-4x token inflation ([#860](https://github.com/NousResearch/hermes-agent/issues/860))
|
| 290 |
+
- Fix setup wizard skipping API key prompts on first install ([#748](https://github.com/NousResearch/hermes-agent/pull/748))
|
| 291 |
+
- Fix setup wizard showing OpenRouter model list for Nous Portal ([#575](https://github.com/NousResearch/hermes-agent/pull/575)) — @PercyDikec
|
| 292 |
+
- Fix provider selection not persisting when switching via hermes model ([#881](https://github.com/NousResearch/hermes-agent/pull/881))
|
| 293 |
+
- Fix Docker backend failing when docker not in PATH on macOS ([#889](https://github.com/NousResearch/hermes-agent/pull/889))
|
| 294 |
+
- Fix ClawHub Skills Hub adapter for API endpoint changes ([#286](https://github.com/NousResearch/hermes-agent/pull/286)) — @BP602
|
| 295 |
+
- Fix Honcho auto-enable when API key is present ([#243](https://github.com/NousResearch/hermes-agent/pull/243)) — @Bartok9
|
| 296 |
+
- Fix duplicate 'skills' subparser crash on Python 3.11+ ([#898](https://github.com/NousResearch/hermes-agent/issues/898))
|
| 297 |
+
- Fix memory tool entry parsing when content contains section sign ([#162](https://github.com/NousResearch/hermes-agent/pull/162)) — @aydnOktay
|
| 298 |
+
- Fix piped install silently aborting when interactive prompts fail ([#72](https://github.com/NousResearch/hermes-agent/pull/72)) — @cutepawss
|
| 299 |
+
- Fix false positives in recursive delete detection ([#68](https://github.com/NousResearch/hermes-agent/pull/68)) — @cutepawss
|
| 300 |
+
- Fix Ruff lint warnings across codebase ([#608](https://github.com/NousResearch/hermes-agent/pull/608)) — @JackTheGit
|
| 301 |
+
- Fix Anthropic native base URL fail-fast ([#173](https://github.com/NousResearch/hermes-agent/pull/173)) — @adavyas
|
| 302 |
+
- Fix install.sh creating ~/.hermes before moving Node.js directory ([#53](https://github.com/NousResearch/hermes-agent/pull/53)) — @JoshuaMart
|
| 303 |
+
- Fix SystemExit traceback during atexit cleanup on Ctrl+C ([#55](https://github.com/NousResearch/hermes-agent/pull/55)) — @bierlingm
|
| 304 |
+
- Restore missing MIT license file ([#620](https://github.com/NousResearch/hermes-agent/pull/620)) — @stablegenius49
|
| 305 |
+
|
| 306 |
+
---
|
| 307 |
+
|
| 308 |
+
## 🧪 Testing
|
| 309 |
+
|
| 310 |
+
- **3,289 tests** across agent, gateway, tools, cron, and CLI
|
| 311 |
+
- Parallelized test suite with pytest-xdist ([#802](https://github.com/NousResearch/hermes-agent/pull/802)) — @OutThisLife
|
| 312 |
+
- Unit tests batch 1: 8 core modules ([#60](https://github.com/NousResearch/hermes-agent/pull/60)) — @0xbyt4
|
| 313 |
+
- Unit tests batch 2: 8 more modules ([#62](https://github.com/NousResearch/hermes-agent/pull/62)) — @0xbyt4
|
| 314 |
+
- Unit tests batch 3: 8 untested modules ([#191](https://github.com/NousResearch/hermes-agent/pull/191)) — @0xbyt4
|
| 315 |
+
- Unit tests batch 4: 5 security/logic-critical modules ([#193](https://github.com/NousResearch/hermes-agent/pull/193)) — @0xbyt4
|
| 316 |
+
- AIAgent (run_agent.py) unit tests ([#67](https://github.com/NousResearch/hermes-agent/pull/67)) — @0xbyt4
|
| 317 |
+
- Trajectory compressor tests ([#203](https://github.com/NousResearch/hermes-agent/pull/203)) — @0xbyt4
|
| 318 |
+
- Clarify tool tests ([#121](https://github.com/NousResearch/hermes-agent/pull/121)) — @Bartok9
|
| 319 |
+
- Telegram format tests — 43 tests for italic/bold/code rendering ([#204](https://github.com/NousResearch/hermes-agent/pull/204)) — @0xbyt4
|
| 320 |
+
- Vision tools type hints + 42 tests ([#792](https://github.com/NousResearch/hermes-agent/pull/792))
|
| 321 |
+
- Compressor tool-call boundary regression tests ([#648](https://github.com/NousResearch/hermes-agent/pull/648)) — @intertwine
|
| 322 |
+
- Test structure reorganization ([#34](https://github.com/NousResearch/hermes-agent/pull/34)) — @0xbyt4
|
| 323 |
+
- Shell noise elimination + fix 36 test failures ([#293](https://github.com/NousResearch/hermes-agent/pull/293)) — @0xbyt4
|
| 324 |
+
|
| 325 |
+
---
|
| 326 |
+
|
| 327 |
+
## 🔬 RL & Evaluation Environments
|
| 328 |
+
|
| 329 |
+
- WebResearchEnv — Multi-step web research RL environment ([#434](https://github.com/NousResearch/hermes-agent/pull/434)) — @jackx707
|
| 330 |
+
- Modal sandbox concurrency limits to avoid deadlocks ([#621](https://github.com/NousResearch/hermes-agent/pull/621)) — @voteblake
|
| 331 |
+
- Hermes-atropos-environments bundled skill ([#815](https://github.com/NousResearch/hermes-agent/pull/815))
|
| 332 |
+
- Local vLLM instance support for evaluation — @dmahan93
|
| 333 |
+
- YC-Bench long-horizon agent benchmark environment
|
| 334 |
+
- OpenThoughts-TBLite evaluation environment and scripts
|
| 335 |
+
|
| 336 |
+
---
|
| 337 |
+
|
| 338 |
+
## 📚 Documentation
|
| 339 |
+
|
| 340 |
+
- Full documentation website (Docusaurus) with 37+ pages
|
| 341 |
+
- Comprehensive platform setup guides for Telegram, Discord, Slack, WhatsApp, Signal, Email
|
| 342 |
+
- AGENTS.md — development guide for AI coding assistants
|
| 343 |
+
- CONTRIBUTING.md ([#117](https://github.com/NousResearch/hermes-agent/pull/117)) — @Bartok9
|
| 344 |
+
- Slash commands reference ([#142](https://github.com/NousResearch/hermes-agent/pull/142)) — @Bartok9
|
| 345 |
+
- Comprehensive AGENTS.md accuracy audit ([#732](https://github.com/NousResearch/hermes-agent/pull/732))
|
| 346 |
+
- Skin/theme system documentation
|
| 347 |
+
- MCP documentation and examples
|
| 348 |
+
- Docs accuracy audit — 35+ corrections
|
| 349 |
+
- Documentation typo fixes ([#825](https://github.com/NousResearch/hermes-agent/pull/825), [#439](https://github.com/NousResearch/hermes-agent/pull/439)) — @JackTheGit
|
| 350 |
+
- CLI config precedence and terminology standardization ([#166](https://github.com/NousResearch/hermes-agent/pull/166), [#167](https://github.com/NousResearch/hermes-agent/pull/167), [#168](https://github.com/NousResearch/hermes-agent/pull/168)) — @Jr-kenny
|
| 351 |
+
- Telegram token regex documentation ([#713](https://github.com/NousResearch/hermes-agent/pull/713)) — @VolodymyrBg
|
| 352 |
+
|
| 353 |
+
---
|
| 354 |
+
|
| 355 |
+
## 👥 Contributors
|
| 356 |
+
|
| 357 |
+
Thank you to the 63 contributors who made this release possible! In just over two weeks, the Hermes Agent community came together to ship an extraordinary amount of work.
|
| 358 |
+
|
| 359 |
+
### Core
|
| 360 |
+
- **@teknium1** — 43 PRs: Project lead, core architecture, provider router, sessions, skills, CLI, documentation
|
| 361 |
+
|
| 362 |
+
### Top Community Contributors
|
| 363 |
+
- **@0xbyt4** — 40 PRs: MCP client, Home Assistant, security fixes (symlink, prompt injection, cron), extensive test coverage (6 batches), ascii-art skill, shell noise elimination, skills sync, Telegram formatting, and dozens more
|
| 364 |
+
- **@Farukest** — 16 PRs: Security hardening (path traversal, dangerous command detection, symlink boundary), Windows compatibility (POSIX guards, path handling), WhatsApp fixes, max-iterations retry, gateway fixes
|
| 365 |
+
- **@aydnOktay** — 11 PRs: Atomic writes (process checkpoints, batch runner, skill files), error handling improvements across Telegram, Discord, code execution, transcription, TTS, and skills
|
| 366 |
+
- **@Bartok9** — 9 PRs: CONTRIBUTING.md, slash commands reference, Discord channel topics, think-block stripping, TTS fix, Honcho fix, session count fix, clarify tests
|
| 367 |
+
- **@PercyDikec** — 7 PRs: DeepSeek V3 parser fix, /retry response discard, gateway transcript offset, Codex status/visibility, max-iterations retry, setup wizard fix
|
| 368 |
+
- **@teyrebaz33** — 5 PRs: Skills enable/disable system, quick commands, personality customization, conditional skill activation
|
| 369 |
+
- **@alireza78a** — 5 PRs: Atomic writes (cron, sessions), fd leak prevention, security allowlist, code execution socket cleanup
|
| 370 |
+
- **@shitcoinsherpa** — 3 PRs: Windows support (pywinpty, UTF-8 encoding, auth store lock)
|
| 371 |
+
- **@Himess** — 3 PRs: Cron/HomeAssistant/Daytona fix, Windows drive-letter parsing, .env permissions
|
| 372 |
+
- **@satelerd** — 2 PRs: WhatsApp native media, multi-user session isolation
|
| 373 |
+
- **@rovle** — 1 PR: Daytona cloud sandbox backend (4 commits)
|
| 374 |
+
- **@erosika** — 1 PR: Honcho AI-native memory integration
|
| 375 |
+
- **@dmahan93** — 1 PR: --fuck-it-ship-it flag + RL environment work
|
| 376 |
+
- **@SHL0MS** — 1 PR: ASCII video skill
|
| 377 |
+
|
| 378 |
+
### All Contributors
|
| 379 |
+
@0xbyt4, @BP602, @Bartok9, @Farukest, @FurkanL0, @Himess, @Indelwin, @JackTheGit, @JoshuaMart, @Jr-kenny, @OutThisLife, @PercyDikec, @SHL0MS, @Sertug17, @VencentSoliman, @VolodymyrBg, @adavyas, @alireza78a, @areu01or00, @aydnOktay, @batuhankocyigit, @bierlingm, @caentzminger, @cesareth, @ch3ronsa, @christomitov, @cutepawss, @deankerr, @dmahan93, @dogiladeveloper, @dragonkhoi, @erosika, @gamedevCloudy, @gizdusum, @grp06, @intertwine, @jackx707, @jdblackstar, @johnh4098, @kaos35, @kshitijk4poor, @leonsgithub, @luisv-1, @manuelschipper, @mehmetkr-31, @memosr, @PeterFile, @rewbs, @rovle, @rsavitt, @satelerd, @spanishflu-est1918, @stablegenius49, @tars90percent, @tekelala, @teknium1, @teyrebaz33, @tripledoublev, @unmodeled-tyler, @voidborne-d, @voteblake, @ygd58
|
| 380 |
+
|
| 381 |
+
---
|
| 382 |
+
|
| 383 |
+
**Full Changelog**: [v0.1.0...v2026.3.12](https://github.com/NousResearch/hermes-agent/compare/v0.1.0...v2026.3.12)
|
RELEASE_v0.3.0.md
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hermes Agent v0.3.0 (v2026.3.17)
|
| 2 |
+
|
| 3 |
+
**Release Date:** March 17, 2026
|
| 4 |
+
|
| 5 |
+
> The streaming, plugins, and provider release — unified real-time token delivery, first-class plugin architecture, rebuilt provider system with Vercel AI Gateway, native Anthropic provider, smart approvals, live Chrome CDP browser connect, ACP IDE integration, Honcho memory, voice mode, persistent shell, and 50+ bug fixes across every platform.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## ✨ Highlights
|
| 10 |
+
|
| 11 |
+
- **Unified Streaming Infrastructure** — Real-time token-by-token delivery in CLI and all gateway platforms. Responses stream as they're generated instead of arriving as a block. ([#1538](https://github.com/NousResearch/hermes-agent/pull/1538))
|
| 12 |
+
|
| 13 |
+
- **First-Class Plugin Architecture** — Drop Python files into `~/.hermes/plugins/` to extend Hermes with custom tools, commands, and hooks. No forking required. ([#1544](https://github.com/NousResearch/hermes-agent/pull/1544), [#1555](https://github.com/NousResearch/hermes-agent/pull/1555))
|
| 14 |
+
|
| 15 |
+
- **Native Anthropic Provider** — Direct Anthropic API calls with Claude Code credential auto-discovery, OAuth PKCE flows, and native prompt caching. No OpenRouter middleman needed. ([#1097](https://github.com/NousResearch/hermes-agent/pull/1097))
|
| 16 |
+
|
| 17 |
+
- **Smart Approvals + /stop Command** — Codex-inspired approval system that learns which commands are safe and remembers your preferences. `/stop` kills the current agent run immediately. ([#1543](https://github.com/NousResearch/hermes-agent/pull/1543))
|
| 18 |
+
|
| 19 |
+
- **Honcho Memory Integration** — Async memory writes, configurable recall modes, session title integration, and multi-user isolation in gateway mode. By @erosika. ([#736](https://github.com/NousResearch/hermes-agent/pull/736))
|
| 20 |
+
|
| 21 |
+
- **Voice Mode** — Push-to-talk in CLI, voice notes in Telegram/Discord, Discord voice channel support, and local Whisper transcription via faster-whisper. ([#1299](https://github.com/NousResearch/hermes-agent/pull/1299), [#1185](https://github.com/NousResearch/hermes-agent/pull/1185), [#1429](https://github.com/NousResearch/hermes-agent/pull/1429))
|
| 22 |
+
|
| 23 |
+
- **Concurrent Tool Execution** — Multiple independent tool calls now run in parallel via ThreadPoolExecutor, significantly reducing latency for multi-tool turns. ([#1152](https://github.com/NousResearch/hermes-agent/pull/1152))
|
| 24 |
+
|
| 25 |
+
- **PII Redaction** — When `privacy.redact_pii` is enabled, personally identifiable information is automatically scrubbed before sending context to LLM providers. ([#1542](https://github.com/NousResearch/hermes-agent/pull/1542))
|
| 26 |
+
|
| 27 |
+
- **`/browser connect` via CDP** — Attach browser tools to a live Chrome instance through Chrome DevTools Protocol. Debug, inspect, and interact with pages you already have open. ([#1549](https://github.com/NousResearch/hermes-agent/pull/1549))
|
| 28 |
+
|
| 29 |
+
- **Vercel AI Gateway Provider** — Route Hermes through Vercel's AI Gateway for access to their model catalog and infrastructure. ([#1628](https://github.com/NousResearch/hermes-agent/pull/1628))
|
| 30 |
+
|
| 31 |
+
- **Centralized Provider Router** — Rebuilt provider system with `call_llm` API, unified `/model` command, auto-detect provider on model switch, and direct endpoint overrides for auxiliary/delegation clients. ([#1003](https://github.com/NousResearch/hermes-agent/pull/1003), [#1506](https://github.com/NousResearch/hermes-agent/pull/1506), [#1375](https://github.com/NousResearch/hermes-agent/pull/1375))
|
| 32 |
+
|
| 33 |
+
- **ACP Server (IDE Integration)** — VS Code, Zed, and JetBrains can now connect to Hermes as an agent backend, with full slash command support. ([#1254](https://github.com/NousResearch/hermes-agent/pull/1254), [#1532](https://github.com/NousResearch/hermes-agent/pull/1532))
|
| 34 |
+
|
| 35 |
+
- **Persistent Shell Mode** — Local and SSH terminal backends can maintain shell state across tool calls — cd, env vars, and aliases persist. By @alt-glitch. ([#1067](https://github.com/NousResearch/hermes-agent/pull/1067), [#1483](https://github.com/NousResearch/hermes-agent/pull/1483))
|
| 36 |
+
|
| 37 |
+
- **Agentic On-Policy Distillation (OPD)** — New RL training environment for distilling agent policies, expanding the Atropos training ecosystem. ([#1149](https://github.com/NousResearch/hermes-agent/pull/1149))
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
## 🏗️ Core Agent & Architecture
|
| 42 |
+
|
| 43 |
+
### Provider & Model Support
|
| 44 |
+
- **Centralized provider router** with `call_llm` API and unified `/model` command — switch models and providers seamlessly ([#1003](https://github.com/NousResearch/hermes-agent/pull/1003))
|
| 45 |
+
- **Vercel AI Gateway** provider support ([#1628](https://github.com/NousResearch/hermes-agent/pull/1628))
|
| 46 |
+
- **Auto-detect provider** when switching models via `/model` ([#1506](https://github.com/NousResearch/hermes-agent/pull/1506))
|
| 47 |
+
- **Direct endpoint overrides** for auxiliary and delegation clients — point vision/subagent calls at specific endpoints ([#1375](https://github.com/NousResearch/hermes-agent/pull/1375))
|
| 48 |
+
- **Native Anthropic auxiliary vision** — use Claude's native vision API instead of routing through OpenAI-compatible endpoints ([#1377](https://github.com/NousResearch/hermes-agent/pull/1377))
|
| 49 |
+
- Anthropic OAuth flow improvements — auto-run `claude setup-token`, reauthentication, PKCE state persistence, identity fingerprinting ([#1132](https://github.com/NousResearch/hermes-agent/pull/1132), [#1360](https://github.com/NousResearch/hermes-agent/pull/1360), [#1396](https://github.com/NousResearch/hermes-agent/pull/1396), [#1597](https://github.com/NousResearch/hermes-agent/pull/1597))
|
| 50 |
+
- Fix adaptive thinking without `budget_tokens` for Claude 4.6 models — by @ASRagab ([#1128](https://github.com/NousResearch/hermes-agent/pull/1128))
|
| 51 |
+
- Fix Anthropic cache markers through adapter — by @brandtcormorant ([#1216](https://github.com/NousResearch/hermes-agent/pull/1216))
|
| 52 |
+
- Retry Anthropic 429/529 errors and surface details to users — by @0xbyt4 ([#1585](https://github.com/NousResearch/hermes-agent/pull/1585))
|
| 53 |
+
- Fix Anthropic adapter max_tokens, fallback crash, proxy base_url — by @0xbyt4 ([#1121](https://github.com/NousResearch/hermes-agent/pull/1121))
|
| 54 |
+
- Fix DeepSeek V3 parser dropping multiple parallel tool calls — by @mr-emmett-one ([#1365](https://github.com/NousResearch/hermes-agent/pull/1365), [#1300](https://github.com/NousResearch/hermes-agent/pull/1300))
|
| 55 |
+
- Accept unlisted models with warning instead of rejecting ([#1047](https://github.com/NousResearch/hermes-agent/pull/1047), [#1102](https://github.com/NousResearch/hermes-agent/pull/1102))
|
| 56 |
+
- Skip reasoning params for unsupported OpenRouter models ([#1485](https://github.com/NousResearch/hermes-agent/pull/1485))
|
| 57 |
+
- MiniMax Anthropic API compatibility fix ([#1623](https://github.com/NousResearch/hermes-agent/pull/1623))
|
| 58 |
+
- Custom endpoint `/models` verification and `/v1` base URL suggestion ([#1480](https://github.com/NousResearch/hermes-agent/pull/1480))
|
| 59 |
+
- Resolve delegation providers from `custom_providers` config ([#1328](https://github.com/NousResearch/hermes-agent/pull/1328))
|
| 60 |
+
- Kimi model additions and User-Agent fix ([#1039](https://github.com/NousResearch/hermes-agent/pull/1039))
|
| 61 |
+
- Strip `call_id`/`response_item_id` for Mistral compatibility ([#1058](https://github.com/NousResearch/hermes-agent/pull/1058))
|
| 62 |
+
|
| 63 |
+
### Agent Loop & Conversation
|
| 64 |
+
- **Anthropic Context Editing API** support ([#1147](https://github.com/NousResearch/hermes-agent/pull/1147))
|
| 65 |
+
- Improved context compaction handoff summaries — compressor now preserves more actionable state ([#1273](https://github.com/NousResearch/hermes-agent/pull/1273))
|
| 66 |
+
- Sync session_id after mid-run context compression ([#1160](https://github.com/NousResearch/hermes-agent/pull/1160))
|
| 67 |
+
- Session hygiene threshold tuned to 50% for more proactive compression ([#1096](https://github.com/NousResearch/hermes-agent/pull/1096), [#1161](https://github.com/NousResearch/hermes-agent/pull/1161))
|
| 68 |
+
- Include session ID in system prompt via `--pass-session-id` flag ([#1040](https://github.com/NousResearch/hermes-agent/pull/1040))
|
| 69 |
+
- Prevent closed OpenAI client reuse across retries ([#1391](https://github.com/NousResearch/hermes-agent/pull/1391))
|
| 70 |
+
- Sanitize chat payloads and provider precedence ([#1253](https://github.com/NousResearch/hermes-agent/pull/1253))
|
| 71 |
+
- Handle dict tool call arguments from Codex and local backends ([#1393](https://github.com/NousResearch/hermes-agent/pull/1393), [#1440](https://github.com/NousResearch/hermes-agent/pull/1440))
|
| 72 |
+
|
| 73 |
+
### Memory & Sessions
|
| 74 |
+
- **Improve memory prioritization** — user preferences and corrections weighted above procedural knowledge ([#1548](https://github.com/NousResearch/hermes-agent/pull/1548))
|
| 75 |
+
- Tighter memory and session recall guidance in system prompts ([#1329](https://github.com/NousResearch/hermes-agent/pull/1329))
|
| 76 |
+
- Persist CLI token counts to session DB for `/insights` ([#1498](https://github.com/NousResearch/hermes-agent/pull/1498))
|
| 77 |
+
- Keep Honcho recall out of the cached system prefix ([#1201](https://github.com/NousResearch/hermes-agent/pull/1201))
|
| 78 |
+
- Correct `seed_ai_identity` to use `session.add_messages()` ([#1475](https://github.com/NousResearch/hermes-agent/pull/1475))
|
| 79 |
+
- Isolate Honcho session routing for multi-user gateway ([#1500](https://github.com/NousResearch/hermes-agent/pull/1500))
|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
## 📱 Messaging Platforms (Gateway)
|
| 84 |
+
|
| 85 |
+
### Gateway Core
|
| 86 |
+
- **System gateway service mode** — run as a system-level systemd service, not just user-level ([#1371](https://github.com/NousResearch/hermes-agent/pull/1371))
|
| 87 |
+
- **Gateway install scope prompts** — choose user vs system scope during setup ([#1374](https://github.com/NousResearch/hermes-agent/pull/1374))
|
| 88 |
+
- **Reasoning hot reload** — change reasoning settings without restarting the gateway ([#1275](https://github.com/NousResearch/hermes-agent/pull/1275))
|
| 89 |
+
- Default group sessions to per-user isolation — no more shared state across users in group chats ([#1495](https://github.com/NousResearch/hermes-agent/pull/1495), [#1417](https://github.com/NousResearch/hermes-agent/pull/1417))
|
| 90 |
+
- Harden gateway restart recovery ([#1310](https://github.com/NousResearch/hermes-agent/pull/1310))
|
| 91 |
+
- Cancel active runs during shutdown ([#1427](https://github.com/NousResearch/hermes-agent/pull/1427))
|
| 92 |
+
- SSL certificate auto-detection for NixOS and non-standard systems ([#1494](https://github.com/NousResearch/hermes-agent/pull/1494))
|
| 93 |
+
- Auto-detect D-Bus session bus for `systemctl --user` on headless servers ([#1601](https://github.com/NousResearch/hermes-agent/pull/1601))
|
| 94 |
+
- Auto-enable systemd linger during gateway install on headless servers ([#1334](https://github.com/NousResearch/hermes-agent/pull/1334))
|
| 95 |
+
- Fall back to module entrypoint when `hermes` is not on PATH ([#1355](https://github.com/NousResearch/hermes-agent/pull/1355))
|
| 96 |
+
- Fix dual gateways on macOS launchd after `hermes update` ([#1567](https://github.com/NousResearch/hermes-agent/pull/1567))
|
| 97 |
+
- Remove recursive ExecStop from systemd units ([#1530](https://github.com/NousResearch/hermes-agent/pull/1530))
|
| 98 |
+
- Prevent logging handler accumulation in gateway mode ([#1251](https://github.com/NousResearch/hermes-agent/pull/1251))
|
| 99 |
+
- Restart on retryable startup failures — by @jplew ([#1517](https://github.com/NousResearch/hermes-agent/pull/1517))
|
| 100 |
+
- Backfill model on gateway sessions after agent runs ([#1306](https://github.com/NousResearch/hermes-agent/pull/1306))
|
| 101 |
+
- PID-based gateway kill and deferred config write ([#1499](https://github.com/NousResearch/hermes-agent/pull/1499))
|
| 102 |
+
|
| 103 |
+
### Telegram
|
| 104 |
+
- Buffer media groups to prevent self-interruption from photo bursts ([#1341](https://github.com/NousResearch/hermes-agent/pull/1341), [#1422](https://github.com/NousResearch/hermes-agent/pull/1422))
|
| 105 |
+
- Retry on transient TLS failures during connect and send ([#1535](https://github.com/NousResearch/hermes-agent/pull/1535))
|
| 106 |
+
- Harden polling conflict handling ([#1339](https://github.com/NousResearch/hermes-agent/pull/1339))
|
| 107 |
+
- Escape chunk indicators and inline code in MarkdownV2 ([#1478](https://github.com/NousResearch/hermes-agent/pull/1478), [#1626](https://github.com/NousResearch/hermes-agent/pull/1626))
|
| 108 |
+
- Check updater/app state before disconnect ([#1389](https://github.com/NousResearch/hermes-agent/pull/1389))
|
| 109 |
+
|
| 110 |
+
### Discord
|
| 111 |
+
- `/thread` command with `auto_thread` config and media metadata fixes ([#1178](https://github.com/NousResearch/hermes-agent/pull/1178))
|
| 112 |
+
- Auto-thread on @mention, skip mention text in bot threads ([#1438](https://github.com/NousResearch/hermes-agent/pull/1438))
|
| 113 |
+
- Retry without reply reference for system messages ([#1385](https://github.com/NousResearch/hermes-agent/pull/1385))
|
| 114 |
+
- Preserve native document and video attachment support ([#1392](https://github.com/NousResearch/hermes-agent/pull/1392))
|
| 115 |
+
- Defer discord adapter annotations to avoid optional import crashes ([#1314](https://github.com/NousResearch/hermes-agent/pull/1314))
|
| 116 |
+
|
| 117 |
+
### Slack
|
| 118 |
+
- Thread handling overhaul — progress messages, responses, and session isolation all respect threads ([#1103](https://github.com/NousResearch/hermes-agent/pull/1103))
|
| 119 |
+
- Formatting, reactions, user resolution, and command improvements ([#1106](https://github.com/NousResearch/hermes-agent/pull/1106))
|
| 120 |
+
- Fix MAX_MESSAGE_LENGTH 3900 → 39000 ([#1117](https://github.com/NousResearch/hermes-agent/pull/1117))
|
| 121 |
+
- File upload fallback preserves thread context — by @0xbyt4 ([#1122](https://github.com/NousResearch/hermes-agent/pull/1122))
|
| 122 |
+
- Improve setup guidance ([#1387](https://github.com/NousResearch/hermes-agent/pull/1387))
|
| 123 |
+
|
| 124 |
+
### Email
|
| 125 |
+
- Fix IMAP UID tracking and SMTP TLS verification ([#1305](https://github.com/NousResearch/hermes-agent/pull/1305))
|
| 126 |
+
- Add `skip_attachments` option via config.yaml ([#1536](https://github.com/NousResearch/hermes-agent/pull/1536))
|
| 127 |
+
|
| 128 |
+
### Home Assistant
|
| 129 |
+
- Event filtering closed by default ([#1169](https://github.com/NousResearch/hermes-agent/pull/1169))
|
| 130 |
+
|
| 131 |
+
---
|
| 132 |
+
|
| 133 |
+
## 🖥️ CLI & User Experience
|
| 134 |
+
|
| 135 |
+
### Interactive CLI
|
| 136 |
+
- **Persistent CLI status bar** — always-visible model, provider, and token counts ([#1522](https://github.com/NousResearch/hermes-agent/pull/1522))
|
| 137 |
+
- **File path autocomplete** in the input prompt ([#1545](https://github.com/NousResearch/hermes-agent/pull/1545))
|
| 138 |
+
- **`/plan` command** — generate implementation plans from specs ([#1372](https://github.com/NousResearch/hermes-agent/pull/1372), [#1381](https://github.com/NousResearch/hermes-agent/pull/1381))
|
| 139 |
+
- **Major `/rollback` improvements** — richer checkpoint history, clearer UX ([#1505](https://github.com/NousResearch/hermes-agent/pull/1505))
|
| 140 |
+
- **Preload CLI skills on launch** — skills are ready before the first prompt ([#1359](https://github.com/NousResearch/hermes-agent/pull/1359))
|
| 141 |
+
- **Centralized slash command registry** — all commands defined once, consumed everywhere ([#1603](https://github.com/NousResearch/hermes-agent/pull/1603))
|
| 142 |
+
- `/bg` alias for `/background` ([#1590](https://github.com/NousResearch/hermes-agent/pull/1590))
|
| 143 |
+
- Prefix matching for slash commands — `/mod` resolves to `/model` ([#1320](https://github.com/NousResearch/hermes-agent/pull/1320))
|
| 144 |
+
- `/new`, `/reset`, `/clear` now start genuinely fresh sessions ([#1237](https://github.com/NousResearch/hermes-agent/pull/1237))
|
| 145 |
+
- Accept session ID prefixes for session actions ([#1425](https://github.com/NousResearch/hermes-agent/pull/1425))
|
| 146 |
+
- TUI prompt and accent output now respect active skin ([#1282](https://github.com/NousResearch/hermes-agent/pull/1282))
|
| 147 |
+
- Centralize tool emoji metadata in registry + skin integration ([#1484](https://github.com/NousResearch/hermes-agent/pull/1484))
|
| 148 |
+
- "View full command" option added to dangerous command approval — by @teknium1 based on design by community ([#887](https://github.com/NousResearch/hermes-agent/pull/887))
|
| 149 |
+
- Non-blocking startup update check and banner deduplication ([#1386](https://github.com/NousResearch/hermes-agent/pull/1386))
|
| 150 |
+
- `/reasoning` command output ordering and inline think extraction fixes ([#1031](https://github.com/NousResearch/hermes-agent/pull/1031))
|
| 151 |
+
- Verbose mode shows full untruncated output ([#1472](https://github.com/NousResearch/hermes-agent/pull/1472))
|
| 152 |
+
- Fix `/status` to report live state and tokens ([#1476](https://github.com/NousResearch/hermes-agent/pull/1476))
|
| 153 |
+
- Seed a default global SOUL.md ([#1311](https://github.com/NousResearch/hermes-agent/pull/1311))
|
| 154 |
+
|
| 155 |
+
### Setup & Configuration
|
| 156 |
+
- **OpenClaw migration** during first-time setup — by @kshitijk4poor ([#981](https://github.com/NousResearch/hermes-agent/pull/981))
|
| 157 |
+
- `hermes claw migrate` command + migration docs ([#1059](https://github.com/NousResearch/hermes-agent/pull/1059))
|
| 158 |
+
- Smart vision setup that respects the user's chosen provider ([#1323](https://github.com/NousResearch/hermes-agent/pull/1323))
|
| 159 |
+
- Handle headless setup flows end-to-end ([#1274](https://github.com/NousResearch/hermes-agent/pull/1274))
|
| 160 |
+
- Prefer curses over `simple_term_menu` in setup.py ([#1487](https://github.com/NousResearch/hermes-agent/pull/1487))
|
| 161 |
+
- Show effective model and provider in `/status` ([#1284](https://github.com/NousResearch/hermes-agent/pull/1284))
|
| 162 |
+
- Config set examples use placeholder syntax ([#1322](https://github.com/NousResearch/hermes-agent/pull/1322))
|
| 163 |
+
- Reload .env over stale shell overrides ([#1434](https://github.com/NousResearch/hermes-agent/pull/1434))
|
| 164 |
+
- Fix is_coding_plan NameError crash — by @0xbyt4 ([#1123](https://github.com/NousResearch/hermes-agent/pull/1123))
|
| 165 |
+
- Add missing packages to setuptools config — by @alt-glitch ([#912](https://github.com/NousResearch/hermes-agent/pull/912))
|
| 166 |
+
- Installer: clarify why sudo is needed at every prompt ([#1602](https://github.com/NousResearch/hermes-agent/pull/1602))
|
| 167 |
+
|
| 168 |
+
---
|
| 169 |
+
|
| 170 |
+
## 🔧 Tool System
|
| 171 |
+
|
| 172 |
+
### Terminal & Execution
|
| 173 |
+
- **Persistent shell mode** for local and SSH backends — maintain shell state across tool calls — by @alt-glitch ([#1067](https://github.com/NousResearch/hermes-agent/pull/1067), [#1483](https://github.com/NousResearch/hermes-agent/pull/1483))
|
| 174 |
+
- **Tirith pre-exec command scanning** — security layer that analyzes commands before execution ([#1256](https://github.com/NousResearch/hermes-agent/pull/1256))
|
| 175 |
+
- Strip Hermes provider env vars from all subprocess environments ([#1157](https://github.com/NousResearch/hermes-agent/pull/1157), [#1172](https://github.com/NousResearch/hermes-agent/pull/1172), [#1399](https://github.com/NousResearch/hermes-agent/pull/1399), [#1419](https://github.com/NousResearch/hermes-agent/pull/1419)) — initial fix by @eren-karakus0
|
| 176 |
+
- SSH preflight check ([#1486](https://github.com/NousResearch/hermes-agent/pull/1486))
|
| 177 |
+
- Docker backend: make cwd workspace mount explicit opt-in ([#1534](https://github.com/NousResearch/hermes-agent/pull/1534))
|
| 178 |
+
- Add project root to PYTHONPATH in execute_code sandbox ([#1383](https://github.com/NousResearch/hermes-agent/pull/1383))
|
| 179 |
+
- Eliminate execute_code progress spam on gateway platforms ([#1098](https://github.com/NousResearch/hermes-agent/pull/1098))
|
| 180 |
+
- Clearer docker backend preflight errors ([#1276](https://github.com/NousResearch/hermes-agent/pull/1276))
|
| 181 |
+
|
| 182 |
+
### Browser
|
| 183 |
+
- **`/browser connect`** — attach browser tools to a live Chrome instance via CDP ([#1549](https://github.com/NousResearch/hermes-agent/pull/1549))
|
| 184 |
+
- Improve browser cleanup, local browser PATH setup, and screenshot recovery ([#1333](https://github.com/NousResearch/hermes-agent/pull/1333))
|
| 185 |
+
|
| 186 |
+
### MCP
|
| 187 |
+
- **Selective tool loading** with utility policies — filter which MCP tools are available ([#1302](https://github.com/NousResearch/hermes-agent/pull/1302))
|
| 188 |
+
- Auto-reload MCP tools when `mcp_servers` config changes without restart ([#1474](https://github.com/NousResearch/hermes-agent/pull/1474))
|
| 189 |
+
- Resolve npx stdio connection failures ([#1291](https://github.com/NousResearch/hermes-agent/pull/1291))
|
| 190 |
+
- Preserve MCP toolsets when saving platform tool config ([#1421](https://github.com/NousResearch/hermes-agent/pull/1421))
|
| 191 |
+
|
| 192 |
+
### Vision
|
| 193 |
+
- Unify vision backend gating ([#1367](https://github.com/NousResearch/hermes-agent/pull/1367))
|
| 194 |
+
- Surface actual error reason instead of generic message ([#1338](https://github.com/NousResearch/hermes-agent/pull/1338))
|
| 195 |
+
- Make Claude image handling work end-to-end ([#1408](https://github.com/NousResearch/hermes-agent/pull/1408))
|
| 196 |
+
|
| 197 |
+
### Cron
|
| 198 |
+
- **Compress cron management into one tool** — single `cronjob` tool replaces multiple commands ([#1343](https://github.com/NousResearch/hermes-agent/pull/1343))
|
| 199 |
+
- Suppress duplicate cron sends to auto-delivery targets ([#1357](https://github.com/NousResearch/hermes-agent/pull/1357))
|
| 200 |
+
- Persist cron sessions to SQLite ([#1255](https://github.com/NousResearch/hermes-agent/pull/1255))
|
| 201 |
+
- Per-job runtime overrides (provider, model, base_url) ([#1398](https://github.com/NousResearch/hermes-agent/pull/1398))
|
| 202 |
+
- Atomic write in `save_job_output` to prevent data loss on crash ([#1173](https://github.com/NousResearch/hermes-agent/pull/1173))
|
| 203 |
+
- Preserve thread context for `deliver=origin` ([#1437](https://github.com/NousResearch/hermes-agent/pull/1437))
|
| 204 |
+
|
| 205 |
+
### Patch Tool
|
| 206 |
+
- Avoid corrupting pipe chars in V4A patch apply ([#1286](https://github.com/NousResearch/hermes-agent/pull/1286))
|
| 207 |
+
- Permissive `block_anchor` thresholds and unicode normalization ([#1539](https://github.com/NousResearch/hermes-agent/pull/1539))
|
| 208 |
+
|
| 209 |
+
### Delegation
|
| 210 |
+
- Add observability metadata to subagent results (model, tokens, duration, tool trace) ([#1175](https://github.com/NousResearch/hermes-agent/pull/1175))
|
| 211 |
+
|
| 212 |
+
---
|
| 213 |
+
|
| 214 |
+
## 🧩 Skills Ecosystem
|
| 215 |
+
|
| 216 |
+
### Skills System
|
| 217 |
+
- **Integrate skills.sh** as a hub source alongside ClawHub ([#1303](https://github.com/NousResearch/hermes-agent/pull/1303))
|
| 218 |
+
- Secure skill env setup on load ([#1153](https://github.com/NousResearch/hermes-agent/pull/1153))
|
| 219 |
+
- Honor policy table for dangerous verdicts ([#1330](https://github.com/NousResearch/hermes-agent/pull/1330))
|
| 220 |
+
- Harden ClawHub skill search exact matches ([#1400](https://github.com/NousResearch/hermes-agent/pull/1400))
|
| 221 |
+
- Fix ClawHub skill install — use `/download` ZIP endpoint ([#1060](https://github.com/NousResearch/hermes-agent/pull/1060))
|
| 222 |
+
- Avoid mislabeling local skills as builtin — by @arceus77-7 ([#862](https://github.com/NousResearch/hermes-agent/pull/862))
|
| 223 |
+
|
| 224 |
+
### New Skills
|
| 225 |
+
- **Linear** project management ([#1230](https://github.com/NousResearch/hermes-agent/pull/1230))
|
| 226 |
+
- **X/Twitter** via x-cli ([#1285](https://github.com/NousResearch/hermes-agent/pull/1285))
|
| 227 |
+
- **Telephony** — Twilio, SMS, and AI calls ([#1289](https://github.com/NousResearch/hermes-agent/pull/1289))
|
| 228 |
+
- **1Password** — by @arceus77-7 ([#883](https://github.com/NousResearch/hermes-agent/pull/883), [#1179](https://github.com/NousResearch/hermes-agent/pull/1179))
|
| 229 |
+
- **NeuroSkill BCI** integration ([#1135](https://github.com/NousResearch/hermes-agent/pull/1135))
|
| 230 |
+
- **Blender MCP** for 3D modeling ([#1531](https://github.com/NousResearch/hermes-agent/pull/1531))
|
| 231 |
+
- **OSS Security Forensics** ([#1482](https://github.com/NousResearch/hermes-agent/pull/1482))
|
| 232 |
+
- **Parallel CLI** research skill ([#1301](https://github.com/NousResearch/hermes-agent/pull/1301))
|
| 233 |
+
- **OpenCode** CLI skill ([#1174](https://github.com/NousResearch/hermes-agent/pull/1174))
|
| 234 |
+
- **ASCII Video** skill refactored — by @SHL0MS ([#1213](https://github.com/NousResearch/hermes-agent/pull/1213), [#1598](https://github.com/NousResearch/hermes-agent/pull/1598))
|
| 235 |
+
|
| 236 |
+
---
|
| 237 |
+
|
| 238 |
+
## 🎙️ Voice Mode
|
| 239 |
+
|
| 240 |
+
- Voice mode foundation — push-to-talk CLI, Telegram/Discord voice notes ([#1299](https://github.com/NousResearch/hermes-agent/pull/1299))
|
| 241 |
+
- Free local Whisper transcription via faster-whisper ([#1185](https://github.com/NousResearch/hermes-agent/pull/1185))
|
| 242 |
+
- Discord voice channel reliability fixes ([#1429](https://github.com/NousResearch/hermes-agent/pull/1429))
|
| 243 |
+
- Restore local STT fallback for gateway voice notes ([#1490](https://github.com/NousResearch/hermes-agent/pull/1490))
|
| 244 |
+
- Honor `stt.enabled: false` across gateway transcription ([#1394](https://github.com/NousResearch/hermes-agent/pull/1394))
|
| 245 |
+
- Fix bogus incapability message on Telegram voice notes (Issue [#1033](https://github.com/NousResearch/hermes-agent/issues/1033))
|
| 246 |
+
|
| 247 |
+
---
|
| 248 |
+
|
| 249 |
+
## 🔌 ACP (IDE Integration)
|
| 250 |
+
|
| 251 |
+
- Restore ACP server implementation ([#1254](https://github.com/NousResearch/hermes-agent/pull/1254))
|
| 252 |
+
- Support slash commands in ACP adapter ([#1532](https://github.com/NousResearch/hermes-agent/pull/1532))
|
| 253 |
+
|
| 254 |
+
---
|
| 255 |
+
|
| 256 |
+
## 🧪 RL Training
|
| 257 |
+
|
| 258 |
+
- **Agentic On-Policy Distillation (OPD)** environment — new RL training environment for agent policy distillation ([#1149](https://github.com/NousResearch/hermes-agent/pull/1149))
|
| 259 |
+
- Make tinker-atropos RL training fully optional ([#1062](https://github.com/NousResearch/hermes-agent/pull/1062))
|
| 260 |
+
|
| 261 |
+
---
|
| 262 |
+
|
| 263 |
+
## 🔒 Security & Reliability
|
| 264 |
+
|
| 265 |
+
### Security Hardening
|
| 266 |
+
- **Tirith pre-exec command scanning** — static analysis of terminal commands before execution ([#1256](https://github.com/NousResearch/hermes-agent/pull/1256))
|
| 267 |
+
- **PII redaction** when `privacy.redact_pii` is enabled ([#1542](https://github.com/NousResearch/hermes-agent/pull/1542))
|
| 268 |
+
- Strip Hermes provider/gateway/tool env vars from all subprocess environments ([#1157](https://github.com/NousResearch/hermes-agent/pull/1157), [#1172](https://github.com/NousResearch/hermes-agent/pull/1172), [#1399](https://github.com/NousResearch/hermes-agent/pull/1399), [#1419](https://github.com/NousResearch/hermes-agent/pull/1419))
|
| 269 |
+
- Docker cwd workspace mount now explicit opt-in — never auto-mount host directories ([#1534](https://github.com/NousResearch/hermes-agent/pull/1534))
|
| 270 |
+
- Escape parens and braces in fork bomb regex pattern ([#1397](https://github.com/NousResearch/hermes-agent/pull/1397))
|
| 271 |
+
- Harden `.worktreeinclude` path containment ([#1388](https://github.com/NousResearch/hermes-agent/pull/1388))
|
| 272 |
+
- Use description as `pattern_key` to prevent approval collisions ([#1395](https://github.com/NousResearch/hermes-agent/pull/1395))
|
| 273 |
+
|
| 274 |
+
### Reliability
|
| 275 |
+
- Guard init-time stdio writes ([#1271](https://github.com/NousResearch/hermes-agent/pull/1271))
|
| 276 |
+
- Session log writes reuse shared atomic JSON helper ([#1280](https://github.com/NousResearch/hermes-agent/pull/1280))
|
| 277 |
+
- Atomic temp cleanup protected on interrupts ([#1401](https://github.com/NousResearch/hermes-agent/pull/1401))
|
| 278 |
+
|
| 279 |
+
---
|
| 280 |
+
|
| 281 |
+
## 🐛 Notable Bug Fixes
|
| 282 |
+
|
| 283 |
+
- **`/status` always showing 0 tokens** — now reports live state (Issue [#1465](https://github.com/NousResearch/hermes-agent/issues/1465), [#1476](https://github.com/NousResearch/hermes-agent/pull/1476))
|
| 284 |
+
- **Custom model endpoints not working** — restored config-saved endpoint resolution (Issue [#1460](https://github.com/NousResearch/hermes-agent/issues/1460), [#1373](https://github.com/NousResearch/hermes-agent/pull/1373))
|
| 285 |
+
- **MCP tools not visible until restart** — auto-reload on config change (Issue [#1036](https://github.com/NousResearch/hermes-agent/issues/1036), [#1474](https://github.com/NousResearch/hermes-agent/pull/1474))
|
| 286 |
+
- **`hermes tools` removing MCP tools** — preserve MCP toolsets when saving (Issue [#1247](https://github.com/NousResearch/hermes-agent/issues/1247), [#1421](https://github.com/NousResearch/hermes-agent/pull/1421))
|
| 287 |
+
- **Terminal subprocesses inheriting `OPENAI_BASE_URL`** breaking external tools (Issue [#1002](https://github.com/NousResearch/hermes-agent/issues/1002), [#1399](https://github.com/NousResearch/hermes-agent/pull/1399))
|
| 288 |
+
- **Background process lost on gateway restart** — improved recovery (Issue [#1144](https://github.com/NousResearch/hermes-agent/issues/1144))
|
| 289 |
+
- **Cron jobs not persisting state** — now stored in SQLite (Issue [#1416](https://github.com/NousResearch/hermes-agent/issues/1416), [#1255](https://github.com/NousResearch/hermes-agent/pull/1255))
|
| 290 |
+
- **Cronjob `deliver: origin` not preserving thread context** (Issue [#1219](https://github.com/NousResearch/hermes-agent/issues/1219), [#1437](https://github.com/NousResearch/hermes-agent/pull/1437))
|
| 291 |
+
- **Gateway systemd service failing to auto-restart** when browser processes orphaned (Issue [#1617](https://github.com/NousResearch/hermes-agent/issues/1617))
|
| 292 |
+
- **`/background` completion report cut off in Telegram** (Issue [#1443](https://github.com/NousResearch/hermes-agent/issues/1443))
|
| 293 |
+
- **Model switching not taking effect** (Issue [#1244](https://github.com/NousResearch/hermes-agent/issues/1244), [#1183](https://github.com/NousResearch/hermes-agent/pull/1183))
|
| 294 |
+
- **`hermes doctor` reporting cronjob as unavailable** (Issue [#878](https://github.com/NousResearch/hermes-agent/issues/878), [#1180](https://github.com/NousResearch/hermes-agent/pull/1180))
|
| 295 |
+
- **WhatsApp bridge messages not received** from mobile (Issue [#1142](https://github.com/NousResearch/hermes-agent/issues/1142))
|
| 296 |
+
- **Setup wizard hanging on headless SSH** (Issue [#905](https://github.com/NousResearch/hermes-agent/issues/905), [#1274](https://github.com/NousResearch/hermes-agent/pull/1274))
|
| 297 |
+
- **Log handler accumulation** degrading gateway performance (Issue [#990](https://github.com/NousResearch/hermes-agent/issues/990), [#1251](https://github.com/NousResearch/hermes-agent/pull/1251))
|
| 298 |
+
- **Gateway NULL model in DB** (Issue [#987](https://github.com/NousResearch/hermes-agent/issues/987), [#1306](https://github.com/NousResearch/hermes-agent/pull/1306))
|
| 299 |
+
- **Strict endpoints rejecting replayed tool_calls** (Issue [#893](https://github.com/NousResearch/hermes-agent/issues/893))
|
| 300 |
+
- **Remaining hardcoded `~/.hermes` paths** — all now respect `HERMES_HOME` (Issue [#892](https://github.com/NousResearch/hermes-agent/issues/892), [#1233](https://github.com/NousResearch/hermes-agent/pull/1233))
|
| 301 |
+
- **Delegate tool not working with custom inference providers** (Issue [#1011](https://github.com/NousResearch/hermes-agent/issues/1011), [#1328](https://github.com/NousResearch/hermes-agent/pull/1328))
|
| 302 |
+
- **Skills Guard blocking official skills** (Issue [#1006](https://github.com/NousResearch/hermes-agent/issues/1006), [#1330](https://github.com/NousResearch/hermes-agent/pull/1330))
|
| 303 |
+
- **Setup writing provider before model selection** (Issue [#1182](https://github.com/NousResearch/hermes-agent/issues/1182))
|
| 304 |
+
- **`GatewayConfig.get()` AttributeError** crashing all message handling (Issue [#1158](https://github.com/NousResearch/hermes-agent/issues/1158), [#1287](https://github.com/NousResearch/hermes-agent/pull/1287))
|
| 305 |
+
- **`/update` hard-failing with "command not found"** (Issue [#1049](https://github.com/NousResearch/hermes-agent/issues/1049))
|
| 306 |
+
- **Image analysis failing silently** (Issue [#1034](https://github.com/NousResearch/hermes-agent/issues/1034), [#1338](https://github.com/NousResearch/hermes-agent/pull/1338))
|
| 307 |
+
- **API `BadRequestError` from `'dict'` object has no attribute `'strip'`** (Issue [#1071](https://github.com/NousResearch/hermes-agent/issues/1071))
|
| 308 |
+
- **Slash commands requiring exact full name** — now uses prefix matching (Issue [#928](https://github.com/NousResearch/hermes-agent/issues/928), [#1320](https://github.com/NousResearch/hermes-agent/pull/1320))
|
| 309 |
+
- **Gateway stops responding when terminal is closed on headless** (Issue [#1005](https://github.com/NousResearch/hermes-agent/issues/1005))
|
| 310 |
+
|
| 311 |
+
---
|
| 312 |
+
|
| 313 |
+
## 🧪 Testing
|
| 314 |
+
|
| 315 |
+
- Cover empty cached Anthropic tool-call turns ([#1222](https://github.com/NousResearch/hermes-agent/pull/1222))
|
| 316 |
+
- Fix stale CI assumptions in parser and quick-command coverage ([#1236](https://github.com/NousResearch/hermes-agent/pull/1236))
|
| 317 |
+
- Fix gateway async tests without implicit event loop ([#1278](https://github.com/NousResearch/hermes-agent/pull/1278))
|
| 318 |
+
- Make gateway async tests xdist-safe ([#1281](https://github.com/NousResearch/hermes-agent/pull/1281))
|
| 319 |
+
- Cross-timezone naive timestamp regression for cron ([#1319](https://github.com/NousResearch/hermes-agent/pull/1319))
|
| 320 |
+
- Isolate codex provider tests from local env ([#1335](https://github.com/NousResearch/hermes-agent/pull/1335))
|
| 321 |
+
- Lock retry replacement semantics ([#1379](https://github.com/NousResearch/hermes-agent/pull/1379))
|
| 322 |
+
- Improve error logging in session search tool — by @aydnOktay ([#1533](https://github.com/NousResearch/hermes-agent/pull/1533))
|
| 323 |
+
|
| 324 |
+
---
|
| 325 |
+
|
| 326 |
+
## 📚 Documentation
|
| 327 |
+
|
| 328 |
+
- Comprehensive SOUL.md guide ([#1315](https://github.com/NousResearch/hermes-agent/pull/1315))
|
| 329 |
+
- Voice mode documentation ([#1316](https://github.com/NousResearch/hermes-agent/pull/1316), [#1362](https://github.com/NousResearch/hermes-agent/pull/1362))
|
| 330 |
+
- Provider contribution guide ([#1361](https://github.com/NousResearch/hermes-agent/pull/1361))
|
| 331 |
+
- ACP and internal systems implementation guides ([#1259](https://github.com/NousResearch/hermes-agent/pull/1259))
|
| 332 |
+
- Expand Docusaurus coverage across CLI, tools, skills, and skins ([#1232](https://github.com/NousResearch/hermes-agent/pull/1232))
|
| 333 |
+
- Terminal backend and Windows troubleshooting ([#1297](https://github.com/NousResearch/hermes-agent/pull/1297))
|
| 334 |
+
- Skills hub reference section ([#1317](https://github.com/NousResearch/hermes-agent/pull/1317))
|
| 335 |
+
- Checkpoint, /rollback, and git worktrees guide ([#1493](https://github.com/NousResearch/hermes-agent/pull/1493), [#1524](https://github.com/NousResearch/hermes-agent/pull/1524))
|
| 336 |
+
- CLI status bar and /usage reference ([#1523](https://github.com/NousResearch/hermes-agent/pull/1523))
|
| 337 |
+
- Fallback providers + /background command docs ([#1430](https://github.com/NousResearch/hermes-agent/pull/1430))
|
| 338 |
+
- Gateway service scopes docs ([#1378](https://github.com/NousResearch/hermes-agent/pull/1378))
|
| 339 |
+
- Slack thread reply behavior docs ([#1407](https://github.com/NousResearch/hermes-agent/pull/1407))
|
| 340 |
+
- Redesigned landing page with Nous blue palette — by @austinpickett ([#974](https://github.com/NousResearch/hermes-agent/pull/974))
|
| 341 |
+
- Fix several documentation typos — by @JackTheGit ([#953](https://github.com/NousResearch/hermes-agent/pull/953))
|
| 342 |
+
- Stabilize website diagrams ([#1405](https://github.com/NousResearch/hermes-agent/pull/1405))
|
| 343 |
+
- CLI vs messaging quick reference in README ([#1491](https://github.com/NousResearch/hermes-agent/pull/1491))
|
| 344 |
+
- Add search to Docusaurus ([#1053](https://github.com/NousResearch/hermes-agent/pull/1053))
|
| 345 |
+
- Home Assistant integration docs ([#1170](https://github.com/NousResearch/hermes-agent/pull/1170))
|
| 346 |
+
|
| 347 |
+
---
|
| 348 |
+
|
| 349 |
+
## 👥 Contributors
|
| 350 |
+
|
| 351 |
+
### Core
|
| 352 |
+
- **@teknium1** — 220+ PRs spanning every area of the codebase
|
| 353 |
+
|
| 354 |
+
### Top Community Contributors
|
| 355 |
+
|
| 356 |
+
- **@0xbyt4** (4 PRs) — Anthropic adapter fixes (max_tokens, fallback crash, 429/529 retry), Slack file upload thread context, setup NameError fix
|
| 357 |
+
- **@erosika** (1 PR) — Honcho memory integration: async writes, memory modes, session title integration
|
| 358 |
+
- **@SHL0MS** (2 PRs) — ASCII video skill design patterns and refactoring
|
| 359 |
+
- **@alt-glitch** (2 PRs) — Persistent shell mode for local/SSH backends, setuptools packaging fix
|
| 360 |
+
- **@arceus77-7** (2 PRs) — 1Password skill, fix skills list mislabeling
|
| 361 |
+
- **@kshitijk4poor** (1 PR) — OpenClaw migration during setup wizard
|
| 362 |
+
- **@ASRagab** (1 PR) — Fix adaptive thinking for Claude 4.6 models
|
| 363 |
+
- **@eren-karakus0** (1 PR) — Strip Hermes provider env vars from subprocess environment
|
| 364 |
+
- **@mr-emmett-one** (1 PR) — Fix DeepSeek V3 parser multi-tool call support
|
| 365 |
+
- **@jplew** (1 PR) — Gateway restart on retryable startup failures
|
| 366 |
+
- **@brandtcormorant** (1 PR) — Fix Anthropic cache control for empty text blocks
|
| 367 |
+
- **@aydnOktay** (1 PR) — Improve error logging in session search tool
|
| 368 |
+
- **@austinpickett** (1 PR) — Landing page redesign with Nous blue palette
|
| 369 |
+
- **@JackTheGit** (1 PR) — Documentation typo fixes
|
| 370 |
+
|
| 371 |
+
### All Contributors
|
| 372 |
+
|
| 373 |
+
@0xbyt4, @alt-glitch, @arceus77-7, @ASRagab, @austinpickett, @aydnOktay, @brandtcormorant, @eren-karakus0, @erosika, @JackTheGit, @jplew, @kshitijk4poor, @mr-emmett-one, @SHL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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hermes Agent v0.4.0 (v2026.3.23)
|
| 2 |
+
|
| 3 |
+
**Release Date:** March 23, 2026
|
| 4 |
+
|
| 5 |
+
> The platform expansion release — OpenAI-compatible API server, 6 new messaging adapters, 4 new inference providers, MCP server management with OAuth 2.1, @ context references, gateway prompt caching, streaming enabled by default, and a sweeping reliability pass with 200+ bug fixes.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## ✨ Highlights
|
| 10 |
+
|
| 11 |
+
- **OpenAI-compatible API server** — Expose Hermes as an `/v1/chat/completions` endpoint with a new `/api/jobs` REST API for cron job management, hardened with input limits, field whitelists, SQLite-backed response persistence, and CORS origin protection ([#1756](https://github.com/NousResearch/hermes-agent/pull/1756), [#2450](https://github.com/NousResearch/hermes-agent/pull/2450), [#2456](https://github.com/NousResearch/hermes-agent/pull/2456), [#2451](https://github.com/NousResearch/hermes-agent/pull/2451), [#2472](https://github.com/NousResearch/hermes-agent/pull/2472))
|
| 12 |
+
|
| 13 |
+
- **6 new messaging platform adapters** — Signal, DingTalk, SMS (Twilio), Mattermost, Matrix, and Webhook adapters join Telegram, Discord, and WhatsApp. Gateway auto-reconnects failed platforms with exponential backoff ([#2206](https://github.com/NousResearch/hermes-agent/pull/2206), [#1685](https://github.com/NousResearch/hermes-agent/pull/1685), [#1688](https://github.com/NousResearch/hermes-agent/pull/1688), [#1683](https://github.com/NousResearch/hermes-agent/pull/1683), [#2166](https://github.com/NousResearch/hermes-agent/pull/2166), [#2584](https://github.com/NousResearch/hermes-agent/pull/2584))
|
| 14 |
+
|
| 15 |
+
- **@ context references** — Claude Code-style `@file` and `@url` context injection with tab completions in the CLI ([#2343](https://github.com/NousResearch/hermes-agent/pull/2343), [#2482](https://github.com/NousResearch/hermes-agent/pull/2482))
|
| 16 |
+
|
| 17 |
+
- **4 new inference providers** — GitHub Copilot (OAuth + token validation), Alibaba Cloud / DashScope, Kilo Code, and OpenCode Zen/Go ([#1924](https://github.com/NousResearch/hermes-agent/pull/1924), [#1879](https://github.com/NousResearch/hermes-agent/pull/1879) by @mchzimm, [#1673](https://github.com/NousResearch/hermes-agent/pull/1673), [#1666](https://github.com/NousResearch/hermes-agent/pull/1666), [#1650](https://github.com/NousResearch/hermes-agent/pull/1650))
|
| 18 |
+
|
| 19 |
+
- **MCP server management CLI** — `hermes mcp` commands for installing, configuring, and authenticating MCP servers with full OAuth 2.1 PKCE flow ([#2465](https://github.com/NousResearch/hermes-agent/pull/2465))
|
| 20 |
+
|
| 21 |
+
- **Gateway prompt caching** — Cache AIAgent instances per session, preserving Anthropic prompt cache across turns for dramatic cost reduction on long conversations ([#2282](https://github.com/NousResearch/hermes-agent/pull/2282), [#2284](https://github.com/NousResearch/hermes-agent/pull/2284), [#2361](https://github.com/NousResearch/hermes-agent/pull/2361))
|
| 22 |
+
|
| 23 |
+
- **Context compression overhaul** — Structured summaries with iterative updates, token-budget tail protection, configurable summary endpoint, and fallback model support ([#2323](https://github.com/NousResearch/hermes-agent/pull/2323), [#1727](https://github.com/NousResearch/hermes-agent/pull/1727), [#2224](https://github.com/NousResearch/hermes-agent/pull/2224))
|
| 24 |
+
|
| 25 |
+
- **Streaming enabled by default** — CLI streaming on by default with proper spinner/tool progress display during streaming mode, plus extensive linebreak and concatenation fixes ([#2340](https://github.com/NousResearch/hermes-agent/pull/2340), [#2161](https://github.com/NousResearch/hermes-agent/pull/2161), [#2258](https://github.com/NousResearch/hermes-agent/pull/2258))
|
| 26 |
+
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
## 🖥️ CLI & User Experience
|
| 30 |
+
|
| 31 |
+
### New Commands & Interactions
|
| 32 |
+
- **@ context completions** — Tab-completable `@file`/`@url` references that inject file content or web pages into the conversation ([#2482](https://github.com/NousResearch/hermes-agent/pull/2482), [#2343](https://github.com/NousResearch/hermes-agent/pull/2343))
|
| 33 |
+
- **`/statusbar`** — Toggle a persistent config bar showing model + provider info in the prompt ([#2240](https://github.com/NousResearch/hermes-agent/pull/2240), [#1917](https://github.com/NousResearch/hermes-agent/pull/1917))
|
| 34 |
+
- **`/queue`** — Queue prompts for the agent without interrupting the current run ([#2191](https://github.com/NousResearch/hermes-agent/pull/2191), [#2469](https://github.com/NousResearch/hermes-agent/pull/2469))
|
| 35 |
+
- **`/permission`** — Switch approval mode dynamically during a session ([#2207](https://github.com/NousResearch/hermes-agent/pull/2207))
|
| 36 |
+
- **`/browser`** — Interactive browser sessions from the CLI ([#2273](https://github.com/NousResearch/hermes-agent/pull/2273), [#1814](https://github.com/NousResearch/hermes-agent/pull/1814))
|
| 37 |
+
- **`/cost`** — Live pricing and usage tracking in gateway mode ([#2180](https://github.com/NousResearch/hermes-agent/pull/2180))
|
| 38 |
+
- **`/approve` and `/deny`** — Replaced bare text approval in gateway with explicit commands ([#2002](https://github.com/NousResearch/hermes-agent/pull/2002))
|
| 39 |
+
|
| 40 |
+
### Streaming & Display
|
| 41 |
+
- Streaming enabled by default in CLI ([#2340](https://github.com/NousResearch/hermes-agent/pull/2340))
|
| 42 |
+
- Show spinners and tool progress during streaming mode ([#2161](https://github.com/NousResearch/hermes-agent/pull/2161))
|
| 43 |
+
- Show reasoning/thinking blocks when `show_reasoning` enabled ([#2118](https://github.com/NousResearch/hermes-agent/pull/2118))
|
| 44 |
+
- Context pressure warnings for CLI and gateway ([#2159](https://github.com/NousResearch/hermes-agent/pull/2159))
|
| 45 |
+
- Fix: streaming chunks concatenated without whitespace ([#2258](https://github.com/NousResearch/hermes-agent/pull/2258))
|
| 46 |
+
- Fix: iteration boundary linebreak prevents stream concatenation ([#2413](https://github.com/NousResearch/hermes-agent/pull/2413))
|
| 47 |
+
- Fix: defer streaming linebreak to prevent blank line stacking ([#2473](https://github.com/NousResearch/hermes-agent/pull/2473))
|
| 48 |
+
- Fix: suppress spinner animation in non-TTY environments ([#2216](https://github.com/NousResearch/hermes-agent/pull/2216))
|
| 49 |
+
- Fix: display provider and endpoint in API error messages ([#2266](https://github.com/NousResearch/hermes-agent/pull/2266))
|
| 50 |
+
- Fix: resolve garbled ANSI escape codes in status printouts ([#2448](https://github.com/NousResearch/hermes-agent/pull/2448))
|
| 51 |
+
- Fix: update gold ANSI color to true-color format ([#2246](https://github.com/NousResearch/hermes-agent/pull/2246))
|
| 52 |
+
- Fix: normalize toolset labels and use skin colors in banner ([#1912](https://github.com/NousResearch/hermes-agent/pull/1912))
|
| 53 |
+
|
| 54 |
+
### CLI Polish
|
| 55 |
+
- Fix: prevent 'Press ENTER to continue...' on exit ([#2555](https://github.com/NousResearch/hermes-agent/pull/2555))
|
| 56 |
+
- Fix: flush stdout during agent loop to prevent macOS display freeze ([#1654](https://github.com/NousResearch/hermes-agent/pull/1654))
|
| 57 |
+
- Fix: show human-readable error when `hermes setup` hits permissions error ([#2196](https://github.com/NousResearch/hermes-agent/pull/2196))
|
| 58 |
+
- Fix: `/stop` command crash + UnboundLocalError in streaming media delivery ([#2463](https://github.com/NousResearch/hermes-agent/pull/2463))
|
| 59 |
+
- Fix: allow custom/local endpoints without API key ([#2556](https://github.com/NousResearch/hermes-agent/pull/2556))
|
| 60 |
+
- Fix: Kitty keyboard protocol Shift+Enter for Ghostty/WezTerm (attempted + reverted due to prompt_toolkit crash) ([#2345](https://github.com/NousResearch/hermes-agent/pull/2345), [#2349](https://github.com/NousResearch/hermes-agent/pull/2349))
|
| 61 |
+
|
| 62 |
+
### Configuration
|
| 63 |
+
- **`${ENV_VAR}` substitution** in config.yaml ([#2684](https://github.com/NousResearch/hermes-agent/pull/2684))
|
| 64 |
+
- **Real-time config reload** — config.yaml changes apply without restart ([#2210](https://github.com/NousResearch/hermes-agent/pull/2210))
|
| 65 |
+
- **`custom_models.yaml`** for user-managed model additions ([#2214](https://github.com/NousResearch/hermes-agent/pull/2214))
|
| 66 |
+
- **Priority-based context file selection** + CLAUDE.md support ([#2301](https://github.com/NousResearch/hermes-agent/pull/2301))
|
| 67 |
+
- **Merge nested YAML sections** instead of replacing on config update ([#2213](https://github.com/NousResearch/hermes-agent/pull/2213))
|
| 68 |
+
- Fix: config.yaml provider key overrides env var silently ([#2272](https://github.com/NousResearch/hermes-agent/pull/2272))
|
| 69 |
+
- Fix: log warning instead of silently swallowing config.yaml errors ([#2683](https://github.com/NousResearch/hermes-agent/pull/2683))
|
| 70 |
+
- Fix: disabled toolsets re-enable themselves after `hermes tools` ([#2268](https://github.com/NousResearch/hermes-agent/pull/2268))
|
| 71 |
+
- Fix: platform default toolsets silently override tool deselection ([#2624](https://github.com/NousResearch/hermes-agent/pull/2624))
|
| 72 |
+
- Fix: honor bare YAML `approvals.mode: off` ([#2620](https://github.com/NousResearch/hermes-agent/pull/2620))
|
| 73 |
+
- Fix: `hermes update` use `.[all]` extras with fallback ([#1728](https://github.com/NousResearch/hermes-agent/pull/1728))
|
| 74 |
+
- Fix: `hermes update` prompt before resetting working tree on stash conflicts ([#2390](https://github.com/NousResearch/hermes-agent/pull/2390))
|
| 75 |
+
- Fix: use git pull --rebase in update/install to avoid divergent branch error ([#2274](https://github.com/NousResearch/hermes-agent/pull/2274))
|
| 76 |
+
- Fix: add zprofile fallback and create zshrc on fresh macOS installs ([#2320](https://github.com/NousResearch/hermes-agent/pull/2320))
|
| 77 |
+
- Fix: remove `ANTHROPIC_BASE_URL` env var to avoid collisions ([#1675](https://github.com/NousResearch/hermes-agent/pull/1675))
|
| 78 |
+
- Fix: don't ask IMAP password if already in keyring or env ([#2212](https://github.com/NousResearch/hermes-agent/pull/2212))
|
| 79 |
+
- Fix: OpenCode Zen/Go show OpenRouter models instead of their own ([#2277](https://github.com/NousResearch/hermes-agent/pull/2277))
|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
## 🏗️ Core Agent & Architecture
|
| 84 |
+
|
| 85 |
+
### New Providers
|
| 86 |
+
- **GitHub Copilot** — Full OAuth auth, API routing, token validation, and 400k context. ([#1924](https://github.com/NousResearch/hermes-agent/pull/1924), [#1896](https://github.com/NousResearch/hermes-agent/pull/1896), [#1879](https://github.com/NousResearch/hermes-agent/pull/1879) by @mchzimm, [#2507](https://github.com/NousResearch/hermes-agent/pull/2507))
|
| 87 |
+
- **Alibaba Cloud / DashScope** — Full integration with DashScope v1 runtime, model dot preservation, and 401 auth fixes ([#1673](https://github.com/NousResearch/hermes-agent/pull/1673), [#2332](https://github.com/NousResearch/hermes-agent/pull/2332), [#2459](https://github.com/NousResearch/hermes-agent/pull/2459))
|
| 88 |
+
- **Kilo Code** — First-class inference provider ([#1666](https://github.com/NousResearch/hermes-agent/pull/1666))
|
| 89 |
+
- **OpenCode Zen and OpenCode Go** — New provider backends ([#1650](https://github.com/NousResearch/hermes-agent/pull/1650), [#2393](https://github.com/NousResearch/hermes-agent/pull/2393) by @0xbyt4)
|
| 90 |
+
- **NeuTTS** — Local TTS provider backend with built-in setup flow, replacing the old optional skill ([#1657](https://github.com/NousResearch/hermes-agent/pull/1657), [#1664](https://github.com/NousResearch/hermes-agent/pull/1664))
|
| 91 |
+
|
| 92 |
+
### Provider Improvements
|
| 93 |
+
- **Eager fallback** to backup model on rate-limit errors ([#1730](https://github.com/NousResearch/hermes-agent/pull/1730))
|
| 94 |
+
- **Endpoint metadata** for custom model context and pricing; query local servers for actual context window size ([#1906](https://github.com/NousResearch/hermes-agent/pull/1906), [#2091](https://github.com/NousResearch/hermes-agent/pull/2091) by @dusterbloom)
|
| 95 |
+
- **Context length detection overhaul** — models.dev integration, provider-aware resolution, fuzzy matching for custom endpoints, `/v1/props` for llama.cpp ([#2158](https://github.com/NousResearch/hermes-agent/pull/2158), [#2051](https://github.com/NousResearch/hermes-agent/pull/2051), [#2403](https://github.com/NousResearch/hermes-agent/pull/2403))
|
| 96 |
+
- **Model catalog updates** — gpt-5.4-mini, gpt-5.4-nano, healer-alpha, haiku-4.5, minimax-m2.7, claude 4.6 at 1M context ([#1913](https://github.com/NousResearch/hermes-agent/pull/1913), [#1915](https://github.com/NousResearch/hermes-agent/pull/1915), [#1900](https://github.com/NousResearch/hermes-agent/pull/1900), [#2155](https://github.com/NousResearch/hermes-agent/pull/2155), [#2474](https://github.com/NousResearch/hermes-agent/pull/2474))
|
| 97 |
+
- **Custom endpoint improvements** — `model.base_url` in config.yaml, `api_mode` override for responses API, allow endpoints without API key, fail fast on missing keys ([#2330](https://github.com/NousResearch/hermes-agent/pull/2330), [#1651](https://github.com/NousResearch/hermes-agent/pull/1651), [#2556](https://github.com/NousResearch/hermes-agent/pull/2556), [#2445](https://github.com/NousResearch/hermes-agent/pull/2445), [#1994](https://github.com/NousResearch/hermes-agent/pull/1994), [#1998](https://github.com/NousResearch/hermes-agent/pull/1998))
|
| 98 |
+
- Inject model and provider into system prompt ([#1929](https://github.com/NousResearch/hermes-agent/pull/1929))
|
| 99 |
+
- Tie `api_mode` to provider config instead of env var ([#1656](https://github.com/NousResearch/hermes-agent/pull/1656))
|
| 100 |
+
- Fix: prevent Anthropic token leaking to third-party `anthropic_messages` providers ([#2389](https://github.com/NousResearch/hermes-agent/pull/2389))
|
| 101 |
+
- Fix: prevent Anthropic fallback from inheriting non-Anthropic `base_url` ([#2388](https://github.com/NousResearch/hermes-agent/pull/2388))
|
| 102 |
+
- Fix: `auxiliary_is_nous` flag never resets — leaked Nous tags to other providers ([#1713](https://github.com/NousResearch/hermes-agent/pull/1713))
|
| 103 |
+
- Fix: Anthropic `tool_choice 'none'` still allowed tool calls ([#1714](https://github.com/NousResearch/hermes-agent/pull/1714))
|
| 104 |
+
- Fix: Mistral parser nested JSON fallback extraction ([#2335](https://github.com/NousResearch/hermes-agent/pull/2335))
|
| 105 |
+
- Fix: MiniMax 401 auth resolved by defaulting to `anthropic_messages` ([#2103](https://github.com/NousResearch/hermes-agent/pull/2103))
|
| 106 |
+
- Fix: case-insensitive model family matching ([#2350](https://github.com/NousResearch/hermes-agent/pull/2350))
|
| 107 |
+
- Fix: ignore placeholder provider keys in activation checks ([#2358](https://github.com/NousResearch/hermes-agent/pull/2358))
|
| 108 |
+
- Fix: Preserve Ollama model:tag colons in context length detection ([#2149](https://github.com/NousResearch/hermes-agent/pull/2149))
|
| 109 |
+
- Fix: recognize Claude Code OAuth credentials in startup gate ([#1663](https://github.com/NousResearch/hermes-agent/pull/1663))
|
| 110 |
+
- Fix: detect Claude Code version dynamically for OAuth user-agent ([#1670](https://github.com/NousResearch/hermes-agent/pull/1670))
|
| 111 |
+
- Fix: OAuth flag stale after refresh/fallback ([#1890](https://github.com/NousResearch/hermes-agent/pull/1890))
|
| 112 |
+
- Fix: auxiliary client skips expired Codex JWT ([#2397](https://github.com/NousResearch/hermes-agent/pull/2397))
|
| 113 |
+
|
| 114 |
+
### Agent Loop
|
| 115 |
+
- **Gateway prompt caching** — Cache AIAgent per session, keep assistant turns, fix session restore ([#2282](https://github.com/NousResearch/hermes-agent/pull/2282), [#2284](https://github.com/NousResearch/hermes-agent/pull/2284), [#2361](https://github.com/NousResearch/hermes-agent/pull/2361))
|
| 116 |
+
- **Context compression overhaul** — Structured summaries, iterative updates, token-budget tail protection, configurable `summary_base_url` ([#2323](https://github.com/NousResearch/hermes-agent/pull/2323), [#1727](https://github.com/NousResearch/hermes-agent/pull/1727), [#2224](https://github.com/NousResearch/hermes-agent/pull/2224))
|
| 117 |
+
- **Pre-call sanitization and post-call tool guardrails** ([#1732](https://github.com/NousResearch/hermes-agent/pull/1732))
|
| 118 |
+
- **Auto-recover** from provider-rejected `tool_choice` by retrying without ([#2174](https://github.com/NousResearch/hermes-agent/pull/2174))
|
| 119 |
+
- **Background memory/skill review** replaces inline nudges ([#2235](https://github.com/NousResearch/hermes-agent/pull/2235))
|
| 120 |
+
- **SOUL.md as primary agent identity** instead of hardcoded default ([#1922](https://github.com/NousResearch/hermes-agent/pull/1922))
|
| 121 |
+
- Fix: prevent silent tool result loss during context compression ([#1993](https://github.com/NousResearch/hermes-agent/pull/1993))
|
| 122 |
+
- Fix: handle empty/null function arguments in tool call recovery ([#2163](https://github.com/NousResearch/hermes-agent/pull/2163))
|
| 123 |
+
- Fix: handle API refusal responses gracefully instead of crashing ([#2156](https://github.com/NousResearch/hermes-agent/pull/2156))
|
| 124 |
+
- Fix: prevent stuck agent loop on malformed tool calls ([#2114](https://github.com/NousResearch/hermes-agent/pull/2114))
|
| 125 |
+
- Fix: return JSON parse error to model instead of dispatching with empty args ([#2342](https://github.com/NousResearch/hermes-agent/pull/2342))
|
| 126 |
+
- Fix: consecutive assistant message merge drops content on mixed types ([#1703](https://github.com/NousResearch/hermes-agent/pull/1703))
|
| 127 |
+
- Fix: message role alternation violations in JSON recovery and error handler ([#1722](https://github.com/NousResearch/hermes-agent/pull/1722))
|
| 128 |
+
- Fix: `compression_attempts` resets each iteration — allowed unlimited compressions ([#1723](https://github.com/NousResearch/hermes-agent/pull/1723))
|
| 129 |
+
- Fix: `length_continue_retries` never resets — later truncations got fewer retries ([#1717](https://github.com/NousResearch/hermes-agent/pull/1717))
|
| 130 |
+
- Fix: compressor summary role violated consecutive-role constraint ([#1720](https://github.com/NousResearch/hermes-agent/pull/1720), [#1743](https://github.com/NousResearch/hermes-agent/pull/1743))
|
| 131 |
+
- Fix: remove hardcoded `gemini-3-flash-preview` as default summary model ([#2464](https://github.com/NousResearch/hermes-agent/pull/2464))
|
| 132 |
+
- Fix: correctly handle empty tool results ([#2201](https://github.com/NousResearch/hermes-agent/pull/2201))
|
| 133 |
+
- Fix: crash on None entry in `tool_calls` list ([#2209](https://github.com/NousResearch/hermes-agent/pull/2209) by @0xbyt4, [#2316](https://github.com/NousResearch/hermes-agent/pull/2316))
|
| 134 |
+
- Fix: per-thread persistent event loops in worker threads ([#2214](https://github.com/NousResearch/hermes-agent/pull/2214) by @jquesnelle)
|
| 135 |
+
- Fix: prevent 'event loop already running' when async tools run in parallel ([#2207](https://github.com/NousResearch/hermes-agent/pull/2207))
|
| 136 |
+
- Fix: strip ANSI at the source — clean terminal output before it reaches the model ([#2115](https://github.com/NousResearch/hermes-agent/pull/2115))
|
| 137 |
+
- Fix: skip top-level `cache_control` on role:tool for OpenRouter ([#2391](https://github.com/NousResearch/hermes-agent/pull/2391))
|
| 138 |
+
- Fix: delegate tool — save parent tool names before child construction mutates global ([#2083](https://github.com/NousResearch/hermes-agent/pull/2083) by @ygd58, [#1894](https://github.com/NousResearch/hermes-agent/pull/1894))
|
| 139 |
+
- Fix: only strip last assistant message if empty string ([#2326](https://github.com/NousResearch/hermes-agent/pull/2326))
|
| 140 |
+
|
| 141 |
+
### Session & Memory
|
| 142 |
+
- **Session search** and management slash commands ([#2198](https://github.com/NousResearch/hermes-agent/pull/2198))
|
| 143 |
+
- **Auto session titles** and `.hermes.md` project config ([#1712](https://github.com/NousResearch/hermes-agent/pull/1712))
|
| 144 |
+
- Fix: concurrent memory writes silently drop entries — added file locking ([#1726](https://github.com/NousResearch/hermes-agent/pull/1726))
|
| 145 |
+
- Fix: search all sources by default in `session_search` ([#1892](https://github.com/NousResearch/hermes-agent/pull/1892))
|
| 146 |
+
- Fix: handle hyphenated FTS5 queries and preserve quoted literals ([#1776](https://github.com/NousResearch/hermes-agent/pull/1776))
|
| 147 |
+
- Fix: skip corrupt lines in `load_transcript` instead of crashing ([#1744](https://github.com/NousResearch/hermes-agent/pull/1744))
|
| 148 |
+
- Fix: normalize session keys to prevent case-sensitive duplicates ([#2157](https://github.com/NousResearch/hermes-agent/pull/2157))
|
| 149 |
+
- Fix: prevent `session_search` crash when no sessions exist ([#2194](https://github.com/NousResearch/hermes-agent/pull/2194))
|
| 150 |
+
- Fix: reset token counters on new session for accurate usage display ([#2101](https://github.com/NousResearch/hermes-agent/pull/2101) by @InB4DevOps)
|
| 151 |
+
- Fix: prevent stale memory overwrites by flush agent ([#2687](https://github.com/NousResearch/hermes-agent/pull/2687))
|
| 152 |
+
- Fix: remove synthetic error message injection, fix session resume after repeated failures ([#2303](https://github.com/NousResearch/hermes-agent/pull/2303))
|
| 153 |
+
- Fix: quiet mode with `--resume` now passes conversation_history ([#2357](https://github.com/NousResearch/hermes-agent/pull/2357))
|
| 154 |
+
- Fix: unify resume logic in batch mode ([#2331](https://github.com/NousResearch/hermes-agent/pull/2331))
|
| 155 |
+
|
| 156 |
+
### Honcho Memory
|
| 157 |
+
- Honcho config fixes and @ context reference integration ([#2343](https://github.com/NousResearch/hermes-agent/pull/2343))
|
| 158 |
+
- Self-hosted / Docker configuration documentation ([#2475](https://github.com/NousResearch/hermes-agent/pull/2475))
|
| 159 |
+
|
| 160 |
+
---
|
| 161 |
+
|
| 162 |
+
## 📱 Messaging Platforms (Gateway)
|
| 163 |
+
|
| 164 |
+
### New Platform Adapters
|
| 165 |
+
- **Signal Messenger** — Full adapter with attachment handling, group message filtering, and Note to Self echo-back protection ([#2206](https://github.com/NousResearch/hermes-agent/pull/2206), [#2400](https://github.com/NousResearch/hermes-agent/pull/2400), [#2297](https://github.com/NousResearch/hermes-agent/pull/2297), [#2156](https://github.com/NousResearch/hermes-agent/pull/2156))
|
| 166 |
+
- **DingTalk** — Adapter with gateway wiring and setup docs ([#1685](https://github.com/NousResearch/hermes-agent/pull/1685), [#1690](https://github.com/NousResearch/hermes-agent/pull/1690), [#1692](https://github.com/NousResearch/hermes-agent/pull/1692))
|
| 167 |
+
- **SMS (Twilio)** ([#1688](https://github.com/NousResearch/hermes-agent/pull/1688))
|
| 168 |
+
- **Mattermost** — With @-mention-only channel filter ([#1683](https://github.com/NousResearch/hermes-agent/pull/1683), [#2443](https://github.com/NousResearch/hermes-agent/pull/2443))
|
| 169 |
+
- **Matrix** — With vision support and image caching ([#1683](https://github.com/NousResearch/hermes-agent/pull/1683), [#2520](https://github.com/NousResearch/hermes-agent/pull/2520))
|
| 170 |
+
- **Webhook** — Platform adapter for external event triggers ([#2166](https://github.com/NousResearch/hermes-agent/pull/2166))
|
| 171 |
+
- **OpenAI-compatible API server** — `/v1/chat/completions` endpoint with `/api/jobs` cron management ([#1756](https://github.com/NousResearch/hermes-agent/pull/1756), [#2450](https://github.com/NousResearch/hermes-agent/pull/2450), [#2456](https://github.com/NousResearch/hermes-agent/pull/2456))
|
| 172 |
+
|
| 173 |
+
### Telegram Improvements
|
| 174 |
+
- MarkdownV2 support — strikethrough, spoiler, blockquotes, escape parentheses/braces/backslashes/backticks ([#2199](https://github.com/NousResearch/hermes-agent/pull/2199), [#2200](https://github.com/NousResearch/hermes-agent/pull/2200) by @llbn, [#2386](https://github.com/NousResearch/hermes-agent/pull/2386))
|
| 175 |
+
- Auto-detect HTML tags and use `parse_mode=HTML` ([#1709](https://github.com/NousResearch/hermes-agent/pull/1709))
|
| 176 |
+
- Telegram group vision support + thread-based sessions ([#2153](https://github.com/NousResearch/hermes-agent/pull/2153))
|
| 177 |
+
- Auto-reconnect polling after network interruption ([#2517](https://github.com/NousResearch/hermes-agent/pull/2517))
|
| 178 |
+
- Aggregate split text messages before dispatching ([#1674](https://github.com/NousResearch/hermes-agent/pull/1674))
|
| 179 |
+
- Fix: streaming config bridge, not-modified, flood control ([#1782](https://github.com/NousResearch/hermes-agent/pull/1782), [#1783](https://github.com/NousResearch/hermes-agent/pull/1783))
|
| 180 |
+
- Fix: edited_message event crashes ([#2074](https://github.com/NousResearch/hermes-agent/pull/2074))
|
| 181 |
+
- Fix: retry 409 polling conflicts before giving up ([#2312](https://github.com/NousResearch/hermes-agent/pull/2312))
|
| 182 |
+
- Fix: topic delivery via `platform:chat_id:thread_id` format ([#2455](https://github.com/NousResearch/hermes-agent/pull/2455))
|
| 183 |
+
|
| 184 |
+
### Discord Improvements
|
| 185 |
+
- Document caching and text-file injection ([#2503](https://github.com/NousResearch/hermes-agent/pull/2503))
|
| 186 |
+
- Persistent typing indicator for DMs ([#2468](https://github.com/NousResearch/hermes-agent/pull/2468))
|
| 187 |
+
- Discord DM vision — inline images + attachment analysis ([#2186](https://github.com/NousResearch/hermes-agent/pull/2186))
|
| 188 |
+
- Persist thread participation across gateway restarts ([#1661](https://github.com/NousResearch/hermes-agent/pull/1661))
|
| 189 |
+
- Fix: gateway crash on non-ASCII guild names ([#2302](https://github.com/NousResearch/hermes-agent/pull/2302))
|
| 190 |
+
- Fix: thread permission errors ([#2073](https://github.com/NousResearch/hermes-agent/pull/2073))
|
| 191 |
+
- Fix: slash event routing in threads ([#2460](https://github.com/NousResearch/hermes-agent/pull/2460))
|
| 192 |
+
- Fix: remove bugged followup messages + `/ask` command ([#1836](https://github.com/NousResearch/hermes-agent/pull/1836))
|
| 193 |
+
- Fix: graceful WebSocket reconnection ([#2127](https://github.com/NousResearch/hermes-agent/pull/2127))
|
| 194 |
+
- Fix: voice channel TTS when streaming enabled ([#2322](https://github.com/NousResearch/hermes-agent/pull/2322))
|
| 195 |
+
|
| 196 |
+
### WhatsApp & Other Adapters
|
| 197 |
+
- WhatsApp: outbound `send_message` routing ([#1769](https://github.com/NousResearch/hermes-agent/pull/1769) by @sai-samarth), LID format self-chat ([#1667](https://github.com/NousResearch/hermes-agent/pull/1667)), `reply_prefix` config fix ([#1923](https://github.com/NousResearch/hermes-agent/pull/1923)), restart on bridge child exit ([#2334](https://github.com/NousResearch/hermes-agent/pull/2334)), image/bridge improvements ([#2181](https://github.com/NousResearch/hermes-agent/pull/2181))
|
| 198 |
+
- Matrix: correct `reply_to_message_id` parameter ([#1895](https://github.com/NousResearch/hermes-agent/pull/1895)), bare media types fix ([#1736](https://github.com/NousResearch/hermes-agent/pull/1736))
|
| 199 |
+
- Mattermost: MIME types for media attachments ([#2329](https://github.com/NousResearch/hermes-agent/pull/2329))
|
| 200 |
+
|
| 201 |
+
### Gateway Core
|
| 202 |
+
- **Auto-reconnect** failed platforms with exponential backoff ([#2584](https://github.com/NousResearch/hermes-agent/pull/2584))
|
| 203 |
+
- **Notify users when session auto-resets** ([#2519](https://github.com/NousResearch/hermes-agent/pull/2519))
|
| 204 |
+
- **Reply-to message context** for out-of-session replies ([#1662](https://github.com/NousResearch/hermes-agent/pull/1662))
|
| 205 |
+
- **Ignore unauthorized DMs** config option ([#1919](https://github.com/NousResearch/hermes-agent/pull/1919))
|
| 206 |
+
- Fix: `/reset` in thread-mode resets global session instead of thread ([#2254](https://github.com/NousResearch/hermes-agent/pull/2254))
|
| 207 |
+
- Fix: deliver MEDIA: files after streaming responses ([#2382](https://github.com/NousResearch/hermes-agent/pull/2382))
|
| 208 |
+
- Fix: cap interrupt recursion depth to prevent resource exhaustion ([#1659](https://github.com/NousResearch/hermes-agent/pull/1659))
|
| 209 |
+
- Fix: detect stopped processes and release stale locks on `--replace` ([#2406](https://github.com/NousResearch/hermes-agent/pull/2406), [#1908](https://github.com/NousResearch/hermes-agent/pull/1908))
|
| 210 |
+
- Fix: PID-based wait with force-kill for gateway restart ([#1902](https://github.com/NousResearch/hermes-agent/pull/1902))
|
| 211 |
+
- Fix: prevent `--replace` mode from killing the caller process ([#2185](https://github.com/NousResearch/hermes-agent/pull/2185))
|
| 212 |
+
- Fix: `/model` shows active fallback model instead of config default ([#1660](https://github.com/NousResearch/hermes-agent/pull/1660))
|
| 213 |
+
- Fix: `/title` command fails when session doesn't exist in SQLite yet ([#2379](https://github.com/NousResearch/hermes-agent/pull/2379) by @ten-jampa)
|
| 214 |
+
- Fix: process `/queue`'d messages after agent completion ([#2469](https://github.com/NousResearch/hermes-agent/pull/2469))
|
| 215 |
+
- Fix: strip orphaned `tool_results` + let `/reset` bypass running agent ([#2180](https://github.com/NousResearch/hermes-agent/pull/2180))
|
| 216 |
+
- Fix: prevent agents from starting gateway outside systemd management ([#2617](https://github.com/NousResearch/hermes-agent/pull/2617))
|
| 217 |
+
- Fix: prevent systemd restart storm on gateway connection failure ([#2327](https://github.com/NousResearch/hermes-agent/pull/2327))
|
| 218 |
+
- Fix: include resolved node path in systemd unit ([#1767](https://github.com/NousResearch/hermes-agent/pull/1767) by @sai-samarth)
|
| 219 |
+
- Fix: send error details to user in gateway outer exception handler ([#1966](https://github.com/NousResearch/hermes-agent/pull/1966))
|
| 220 |
+
- Fix: improve error handling for 429 usage limits and 500 context overflow ([#1839](https://github.com/NousResearch/hermes-agent/pull/1839))
|
| 221 |
+
- Fix: add all missing platform allowlist env vars to startup warning check ([#2628](https://github.com/NousResearch/hermes-agent/pull/2628))
|
| 222 |
+
- Fix: media delivery fails for file paths containing spaces ([#2621](https://github.com/NousResearch/hermes-agent/pull/2621))
|
| 223 |
+
- Fix: duplicate session-key collision in multi-platform gateway ([#2171](https://github.com/NousResearch/hermes-agent/pull/2171))
|
| 224 |
+
- Fix: Matrix and Mattermost never report as connected ([#1711](https://github.com/NousResearch/hermes-agent/pull/1711))
|
| 225 |
+
- Fix: PII redaction config never read — missing yaml import ([#1701](https://github.com/NousResearch/hermes-agent/pull/1701))
|
| 226 |
+
- Fix: NameError on skill slash commands ([#1697](https://github.com/NousResearch/hermes-agent/pull/1697))
|
| 227 |
+
- Fix: persist watcher metadata in checkpoint for crash recovery ([#1706](https://github.com/NousResearch/hermes-agent/pull/1706))
|
| 228 |
+
- Fix: pass `message_thread_id` in send_image_file, send_document, send_video ([#2339](https://github.com/NousResearch/hermes-agent/pull/2339))
|
| 229 |
+
- Fix: media-group aggregation on rapid successive photo messages ([#2160](https://github.com/NousResearch/hermes-agent/pull/2160))
|
| 230 |
+
|
| 231 |
+
---
|
| 232 |
+
|
| 233 |
+
## 🔧 Tool System
|
| 234 |
+
|
| 235 |
+
### MCP Enhancements
|
| 236 |
+
- **MCP server management CLI** + OAuth 2.1 PKCE auth ([#2465](https://github.com/NousResearch/hermes-agent/pull/2465))
|
| 237 |
+
- **Expose MCP servers as standalone toolsets** ([#1907](https://github.com/NousResearch/hermes-agent/pull/1907))
|
| 238 |
+
- **Interactive MCP tool configuration** in `hermes tools` ([#1694](https://github.com/NousResearch/hermes-agent/pull/1694))
|
| 239 |
+
- Fix: MCP-OAuth port mismatch, path traversal, and shared handler state ([#2552](https://github.com/NousResearch/hermes-agent/pull/2552))
|
| 240 |
+
- Fix: preserve MCP tool registrations across session resets ([#2124](https://github.com/NousResearch/hermes-agent/pull/2124))
|
| 241 |
+
- Fix: concurrent file access crash + duplicate MCP registration ([#2154](https://github.com/NousResearch/hermes-agent/pull/2154))
|
| 242 |
+
- Fix: normalise MCP schemas + expand session list columns ([#2102](https://github.com/NousResearch/hermes-agent/pull/2102))
|
| 243 |
+
- Fix: `tool_choice` `mcp_` prefix handling ([#1775](https://github.com/NousResearch/hermes-agent/pull/1775))
|
| 244 |
+
|
| 245 |
+
### Web Tool Backends
|
| 246 |
+
- **Tavily** as web search/extract/crawl backend ([#1731](https://github.com/NousResearch/hermes-agent/pull/1731))
|
| 247 |
+
- **Parallel** as alternative web search/extract backend ([#1696](https://github.com/NousResearch/hermes-agent/pull/1696))
|
| 248 |
+
- **Configurable web backend** — Firecrawl/BeautifulSoup/Playwright selection ([#2256](https://github.com/NousResearch/hermes-agent/pull/2256))
|
| 249 |
+
- Fix: whitespace-only env vars bypass web backend detection ([#2341](https://github.com/NousResearch/hermes-agent/pull/2341))
|
| 250 |
+
|
| 251 |
+
### New Tools
|
| 252 |
+
- **IMAP email** reading and sending ([#2173](https://github.com/NousResearch/hermes-agent/pull/2173))
|
| 253 |
+
- **STT (speech-to-text)** tool using Whisper API ([#2072](https://github.com/NousResearch/hermes-agent/pull/2072))
|
| 254 |
+
- **Route-aware pricing estimates** ([#1695](https://github.com/NousResearch/hermes-agent/pull/1695))
|
| 255 |
+
|
| 256 |
+
### Tool Improvements
|
| 257 |
+
- TTS: `base_url` support for OpenAI TTS provider ([#2064](https://github.com/NousResearch/hermes-agent/pull/2064) by @hanai)
|
| 258 |
+
- Vision: configurable timeout, tilde expansion in file paths, DM vision with multi-image and base64 fallback ([#2480](https://github.com/NousResearch/hermes-agent/pull/2480), [#2585](https://github.com/NousResearch/hermes-agent/pull/2585), [#2211](https://github.com/NousResearch/hermes-agent/pull/2211))
|
| 259 |
+
- Browser: race condition fix in session creation ([#1721](https://github.com/NousResearch/hermes-agent/pull/1721)), TypeError on unexpected LLM params ([#1735](https://github.com/NousResearch/hermes-agent/pull/1735))
|
| 260 |
+
- File tools: strip ANSI escape codes from write_file and patch content ([#2532](https://github.com/NousResearch/hermes-agent/pull/2532)), include pagination args in repeated search key ([#1824](https://github.com/NousResearch/hermes-agent/pull/1824) by @cutepawss), improve fuzzy matching accuracy + position calculation refactor ([#2096](https://github.com/NousResearch/hermes-agent/pull/2096), [#1681](https://github.com/NousResearch/hermes-agent/pull/1681))
|
| 261 |
+
- Code execution: resource leak and double socket close fix ([#2381](https://github.com/NousResearch/hermes-agent/pull/2381))
|
| 262 |
+
- Delegate: thread safety for concurrent subagent delegation ([#1672](https://github.com/NousResearch/hermes-agent/pull/1672)), preserve parent agent's tool list after delegation ([#1778](https://github.com/NousResearch/hermes-agent/pull/1778))
|
| 263 |
+
- Fix: make concurrent tool batching path-aware for file mutations ([#1914](https://github.com/NousResearch/hermes-agent/pull/1914))
|
| 264 |
+
- Fix: chunk long messages in `send_message_tool` before platform dispatch ([#1646](https://github.com/NousResearch/hermes-agent/pull/1646))
|
| 265 |
+
- Fix: add missing 'messaging' toolset ([#1718](https://github.com/NousResearch/hermes-agent/pull/1718))
|
| 266 |
+
- Fix: prevent unavailable tool names from leaking into model schemas ([#2072](https://github.com/NousResearch/hermes-agent/pull/2072))
|
| 267 |
+
- Fix: pass visited set by reference to prevent diamond dependency duplication ([#2311](https://github.com/NousResearch/hermes-agent/pull/2311))
|
| 268 |
+
- Fix: Daytona sandbox lookup migrated from `find_one` to `get/list` ([#2063](https://github.com/NousResearch/hermes-agent/pull/2063) by @rovle)
|
| 269 |
+
|
| 270 |
+
---
|
| 271 |
+
|
| 272 |
+
## 🧩 Skills Ecosystem
|
| 273 |
+
|
| 274 |
+
### Skills System Improvements
|
| 275 |
+
- **Agent-created skills** — Caution-level findings allowed, dangerous skills ask instead of block ([#1840](https://github.com/NousResearch/hermes-agent/pull/1840), [#2446](https://github.com/NousResearch/hermes-agent/pull/2446))
|
| 276 |
+
- **`--yes` flag** to bypass confirmation in `/skills install` and uninstall ([#1647](https://github.com/NousResearch/hermes-agent/pull/1647))
|
| 277 |
+
- **Disabled skills respected** across banner, system prompt, and slash commands ([#1897](https://github.com/NousResearch/hermes-agent/pull/1897))
|
| 278 |
+
- Fix: skills custom_tools import crash + sandbox file_tools integration ([#2239](https://github.com/NousResearch/hermes-agent/pull/2239))
|
| 279 |
+
- Fix: agent-created skills with pip requirements crash on install ([#2145](https://github.com/NousResearch/hermes-agent/pull/2145))
|
| 280 |
+
- Fix: race condition in `Skills.__init__` when `hub.yaml` missing ([#2242](https://github.com/NousResearch/hermes-agent/pull/2242))
|
| 281 |
+
- Fix: validate skill metadata before install and block duplicates ([#2241](https://github.com/NousResearch/hermes-agent/pull/2241))
|
| 282 |
+
- Fix: skills hub inspect/resolve — 4 bugs in inspect, redirects, discovery, tap list ([#2447](https://github.com/NousResearch/hermes-agent/pull/2447))
|
| 283 |
+
- Fix: agent-created skills keep working after session reset ([#2121](https://github.com/NousResearch/hermes-agent/pull/2121))
|
| 284 |
+
|
| 285 |
+
### New Skills
|
| 286 |
+
- **OCR-and-documents** — PDF/DOCX/XLS/PPTX/image OCR with optional GPU ([#2236](https://github.com/NousResearch/hermes-agent/pull/2236), [#2461](https://github.com/NousResearch/hermes-agent/pull/2461))
|
| 287 |
+
- **Huggingface-hub** bundled skill ([#1921](https://github.com/NousResearch/hermes-agent/pull/1921))
|
| 288 |
+
- **Sherlock OSINT** username search ([#1671](https://github.com/NousResearch/hermes-agent/pull/1671))
|
| 289 |
+
- **Meme-generation** — Image generator with Pillow ([#2344](https://github.com/NousResearch/hermes-agent/pull/2344))
|
| 290 |
+
- **Bioinformatics** gateway skill — index to 400+ bio skills ([#2387](https://github.com/NousResearch/hermes-agent/pull/2387))
|
| 291 |
+
- **Inference.sh** skill (terminal-based) ([#1686](https://github.com/NousResearch/hermes-agent/pull/1686))
|
| 292 |
+
- **Base blockchain** optional skill ([#1643](https://github.com/NousResearch/hermes-agent/pull/1643))
|
| 293 |
+
- **3D-model-viewer** optional skill ([#2226](https://github.com/NousResearch/hermes-agent/pull/2226))
|
| 294 |
+
- **FastMCP** optional skill ([#2113](https://github.com/NousResearch/hermes-agent/pull/2113))
|
| 295 |
+
- **Hermes-agent-setup** skill ([#1905](https://github.com/NousResearch/hermes-agent/pull/1905))
|
| 296 |
+
|
| 297 |
+
---
|
| 298 |
+
|
| 299 |
+
## 🔌 Plugin System Enhancements
|
| 300 |
+
|
| 301 |
+
- **TUI extension hooks** — Build custom CLIs on top of Hermes ([#2333](https://github.com/NousResearch/hermes-agent/pull/2333))
|
| 302 |
+
- **`hermes plugins install/remove/list`** commands ([#2337](https://github.com/NousResearch/hermes-agent/pull/2337))
|
| 303 |
+
- **Slash command registration** for plugins ([#2359](https://github.com/NousResearch/hermes-agent/pull/2359))
|
| 304 |
+
- **`session:end` lifecycle event** hook ([#1725](https://github.com/NousResearch/hermes-agent/pull/1725))
|
| 305 |
+
- Fix: require opt-in for project plugin discovery ([#2215](https://github.com/NousResearch/hermes-agent/pull/2215))
|
| 306 |
+
|
| 307 |
+
---
|
| 308 |
+
|
| 309 |
+
## 🔒 Security & Reliability
|
| 310 |
+
|
| 311 |
+
### Security
|
| 312 |
+
- **SSRF protection** for vision_tools and web_tools ([#2679](https://github.com/NousResearch/hermes-agent/pull/2679))
|
| 313 |
+
- **Shell injection prevention** in `_expand_path` via `~user` path suffix ([#2685](https://github.com/NousResearch/hermes-agent/pull/2685))
|
| 314 |
+
- **Block untrusted browser-origin** API server access ([#2451](https://github.com/NousResearch/hermes-agent/pull/2451))
|
| 315 |
+
- **Block sandbox backend creds** from subprocess env ([#1658](https://github.com/NousResearch/hermes-agent/pull/1658))
|
| 316 |
+
- **Block @ references** from reading secrets outside workspace ([#2601](https://github.com/NousResearch/hermes-agent/pull/2601) by @Gutslabs)
|
| 317 |
+
- **Malicious code pattern pre-exec scanner** for terminal_tool ([#2245](https://github.com/NousResearch/hermes-agent/pull/2245))
|
| 318 |
+
- **Harden terminal safety** and sandbox file writes ([#1653](https://github.com/NousResearch/hermes-agent/pull/1653))
|
| 319 |
+
- **PKCE verifier leak** fix + OAuth refresh Content-Type ([#1775](https://github.com/NousResearch/hermes-agent/pull/1775))
|
| 320 |
+
- **Eliminate SQL string formatting** in `execute()` calls ([#2061](https://github.com/NousResearch/hermes-agent/pull/2061) by @dusterbloom)
|
| 321 |
+
- **Harden jobs API** — input limits, field whitelist, startup check ([#2456](https://github.com/NousResearch/hermes-agent/pull/2456))
|
| 322 |
+
|
| 323 |
+
### Reliability
|
| 324 |
+
- Thread locks on 4 SessionDB methods ([#1704](https://github.com/NousResearch/hermes-agent/pull/1704))
|
| 325 |
+
- File locking for concurrent memory writes ([#1726](https://github.com/NousResearch/hermes-agent/pull/1726))
|
| 326 |
+
- Handle OpenRouter errors gracefully ([#2112](https://github.com/NousResearch/hermes-agent/pull/2112))
|
| 327 |
+
- Guard print() calls against OSError ([#1668](https://github.com/NousResearch/hermes-agent/pull/1668))
|
| 328 |
+
- Safely handle non-string inputs in redacting formatter ([#2392](https://github.com/NousResearch/hermes-agent/pull/2392), [#1700](https://github.com/NousResearch/hermes-agent/pull/1700))
|
| 329 |
+
- ACP: preserve session provider on model switch, persist sessions to disk ([#2380](https://github.com/NousResearch/hermes-agent/pull/2380), [#2071](https://github.com/NousResearch/hermes-agent/pull/2071))
|
| 330 |
+
- API server: persist ResponseStore to SQLite across restarts ([#2472](https://github.com/NousResearch/hermes-agent/pull/2472))
|
| 331 |
+
- Fix: `fetch_nous_models` always TypeError from positional args ([#1699](https://github.com/NousResearch/hermes-agent/pull/1699))
|
| 332 |
+
- Fix: resolve merge conflict markers in cli.py breaking startup ([#2347](https://github.com/NousResearch/hermes-agent/pull/2347))
|
| 333 |
+
- Fix: `minisweagent_path.py` missing from wheel ([#2098](https://github.com/NousResearch/hermes-agent/pull/2098) by @JiwaniZakir)
|
| 334 |
+
|
| 335 |
+
### Cron System
|
| 336 |
+
- **`[SILENT]` response** — cron agents can suppress delivery ([#1833](https://github.com/NousResearch/hermes-agent/pull/1833))
|
| 337 |
+
- **Scale missed-job grace window** with schedule frequency ([#2449](https://github.com/NousResearch/hermes-agent/pull/2449))
|
| 338 |
+
- **Recover recent one-shot jobs** ([#1918](https://github.com/NousResearch/hermes-agent/pull/1918))
|
| 339 |
+
- Fix: normalize `repeat<=0` to None — jobs deleted after first run when LLM passes -1 ([#2612](https://github.com/NousResearch/hermes-agent/pull/2612) by @Mibayy)
|
| 340 |
+
- Fix: Matrix added to scheduler delivery platform_map ([#2167](https://github.com/NousResearch/hermes-agent/pull/2167) by @buntingszn)
|
| 341 |
+
- Fix: naive ISO timestamps without timezone — jobs fire at wrong time ([#1729](https://github.com/NousResearch/hermes-agent/pull/1729))
|
| 342 |
+
- Fix: `get_due_jobs` reads `jobs.json` twice — race condition ([#1716](https://github.com/NousResearch/hermes-agent/pull/1716))
|
| 343 |
+
- Fix: silent jobs return empty response for delivery skip ([#2442](https://github.com/NousResearch/hermes-agent/pull/2442))
|
| 344 |
+
- Fix: stop injecting cron outputs into gateway session history ([#2313](https://github.com/NousResearch/hermes-agent/pull/2313))
|
| 345 |
+
- Fix: close abandoned coroutine when `asyncio.run()` raises RuntimeError ([#2317](https://github.com/NousResearch/hermes-agent/pull/2317))
|
| 346 |
+
|
| 347 |
+
---
|
| 348 |
+
|
| 349 |
+
## 🧪 Testing
|
| 350 |
+
|
| 351 |
+
- Resolve all consistently failing tests ([#2488](https://github.com/NousResearch/hermes-agent/pull/2488))
|
| 352 |
+
- Replace `FakePath` with `monkeypatch` for Python 3.12 compat ([#2444](https://github.com/NousResearch/hermes-agent/pull/2444))
|
| 353 |
+
- Align Hermes setup and full-suite expectations ([#1710](https://github.com/NousResearch/hermes-agent/pull/1710))
|
| 354 |
+
|
| 355 |
+
---
|
| 356 |
+
|
| 357 |
+
## 📚 Documentation
|
| 358 |
+
|
| 359 |
+
- Comprehensive docs update for recent features ([#1693](https://github.com/NousResearch/hermes-agent/pull/1693), [#2183](https://github.com/NousResearch/hermes-agent/pull/2183))
|
| 360 |
+
- Alibaba Cloud and DingTalk setup guides ([#1687](https://github.com/NousResearch/hermes-agent/pull/1687), [#1692](https://github.com/NousResearch/hermes-agent/pull/1692))
|
| 361 |
+
- Detailed skills documentation ([#2244](https://github.com/NousResearch/hermes-agent/pull/2244))
|
| 362 |
+
- Honcho self-hosted / Docker configuration ([#2475](https://github.com/NousResearch/hermes-agent/pull/2475))
|
| 363 |
+
- Context length detection FAQ and quickstart references ([#2179](https://github.com/NousResearch/hermes-agent/pull/2179))
|
| 364 |
+
- Fix docs inconsistencies across reference and user guides ([#1995](https://github.com/NousResearch/hermes-agent/pull/1995))
|
| 365 |
+
- Fix MCP install commands — use uv, not bare pip ([#1909](https://github.com/NousResearch/hermes-agent/pull/1909))
|
| 366 |
+
- Replace ASCII diagrams with Mermaid/lists ([#2402](https://github.com/NousResearch/hermes-agent/pull/2402))
|
| 367 |
+
- Gemini OAuth provider implementation plan ([#2467](https://github.com/NousResearch/hermes-agent/pull/2467))
|
| 368 |
+
- Discord Server Members Intent marked as required ([#2330](https://github.com/NousResearch/hermes-agent/pull/2330))
|
| 369 |
+
- Fix MDX build error in api-server.md ([#1787](https://github.com/NousResearch/hermes-agent/pull/1787))
|
| 370 |
+
- Align venv path to match installer ([#2114](https://github.com/NousResearch/hermes-agent/pull/2114))
|
| 371 |
+
- New skills added to hub index ([#2281](https://github.com/NousResearch/hermes-agent/pull/2281))
|
| 372 |
+
|
| 373 |
+
---
|
| 374 |
+
|
| 375 |
+
## 👥 Contributors
|
| 376 |
+
|
| 377 |
+
### Core
|
| 378 |
+
- **@teknium1** (Teknium) — 280 PRs
|
| 379 |
+
|
| 380 |
+
### Community Contributors
|
| 381 |
+
- **@mchzimm** (to_the_max) — GitHub Copilot provider integration ([#1879](https://github.com/NousResearch/hermes-agent/pull/1879))
|
| 382 |
+
- **@jquesnelle** (Jeffrey Quesnelle) — Per-thread persistent event loops fix ([#2214](https://github.com/NousResearch/hermes-agent/pull/2214))
|
| 383 |
+
- **@llbn** (lbn) — Telegram MarkdownV2 strikethrough, spoiler, blockquotes, and escape fixes ([#2199](https://github.com/NousResearch/hermes-agent/pull/2199), [#2200](https://github.com/NousResearch/hermes-agent/pull/2200))
|
| 384 |
+
- **@dusterbloom** — SQL injection prevention + local server context window querying ([#2061](https://github.com/NousResearch/hermes-agent/pull/2061), [#2091](https://github.com/NousResearch/hermes-agent/pull/2091))
|
| 385 |
+
- **@0xbyt4** — Anthropic tool_calls None guard + OpenCode-Go provider config fix ([#2209](https://github.com/NousResearch/hermes-agent/pull/2209), [#2393](https://github.com/NousResearch/hermes-agent/pull/2393))
|
| 386 |
+
- **@sai-samarth** (Saisamarth) — WhatsApp send_message routing + systemd node path ([#1769](https://github.com/NousResearch/hermes-agent/pull/1769), [#1767](https://github.com/NousResearch/hermes-agent/pull/1767))
|
| 387 |
+
- **@Gutslabs** (Guts) — Block @ references from reading secrets ([#2601](https://github.com/NousResearch/hermes-agent/pull/2601))
|
| 388 |
+
- **@Mibayy** (Mibay) — Cron job repeat normalization ([#2612](https://github.com/NousResearch/hermes-agent/pull/2612))
|
| 389 |
+
- **@ten-jampa** (Tenzin Jampa) — Gateway /title command fix ([#2379](https://github.com/NousResearch/hermes-agent/pull/2379))
|
| 390 |
+
- **@cutepawss** (lila) — File tools search pagination fix ([#1824](https://github.com/NousResearch/hermes-agent/pull/1824))
|
| 391 |
+
- **@hanai** (Hanai) — OpenAI TTS base_url support ([#2064](https://github.com/NousResearch/hermes-agent/pull/2064))
|
| 392 |
+
- **@rovle** (Lovre Pešut) — Daytona sandbox API migration ([#2063](https://github.com/NousResearch/hermes-agent/pull/2063))
|
| 393 |
+
- **@buntingszn** (bunting szn) — Matrix cron delivery support ([#2167](https://github.com/NousResearch/hermes-agent/pull/2167))
|
| 394 |
+
- **@InB4DevOps** — Token counter reset on new session ([#2101](https://github.com/NousResearch/hermes-agent/pull/2101))
|
| 395 |
+
- **@JiwaniZakir** (Zakir Jiwani) — Missing file in wheel fix ([#2098](https://github.com/NousResearch/hermes-agent/pull/2098))
|
| 396 |
+
- **@ygd58** (buray) — Delegate tool parent tool names fix ([#2083](https://github.com/NousResearch/hermes-agent/pull/2083))
|
| 397 |
+
|
| 398 |
+
---
|
| 399 |
+
|
| 400 |
+
**Full Changelog**: [v2026.3.17...v2026.3.23](https://github.com/NousResearch/hermes-agent/compare/v2026.3.17...v2026.3.23)
|
RELEASE_v0.5.0.md
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hermes Agent v0.5.0 (v2026.3.28)
|
| 2 |
+
|
| 3 |
+
**Release Date:** March 28, 2026
|
| 4 |
+
|
| 5 |
+
> The hardening release — Hugging Face provider, /model command overhaul, Telegram Private Chat Topics, native Modal SDK, plugin lifecycle hooks, tool-use enforcement for GPT models, Nix flake, 50+ security and reliability fixes, and a comprehensive supply chain audit.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## ✨ Highlights
|
| 10 |
+
|
| 11 |
+
- **Nous Portal now supports 400+ models** — The Nous Research inference portal has expanded dramatically, giving Hermes Agent users access to over 400 models through a single provider endpoint
|
| 12 |
+
|
| 13 |
+
- **Hugging Face as a first-class inference provider** — Full integration with HF Inference API including curated agentic model picker that maps to OpenRouter analogues, live `/models` endpoint probe, and setup wizard flow ([#3419](https://github.com/NousResearch/hermes-agent/pull/3419), [#3440](https://github.com/NousResearch/hermes-agent/pull/3440))
|
| 14 |
+
|
| 15 |
+
- **Telegram Private Chat Topics** — Project-based conversations with functional skill binding per topic, enabling isolated workflows within a single Telegram chat ([#3163](https://github.com/NousResearch/hermes-agent/pull/3163))
|
| 16 |
+
|
| 17 |
+
- **Native Modal SDK backend** — Replaced swe-rex dependency with native Modal SDK (`Sandbox.create.aio` + `exec.aio`), eliminating tunnels and simplifying the Modal terminal backend ([#3538](https://github.com/NousResearch/hermes-agent/pull/3538))
|
| 18 |
+
|
| 19 |
+
- **Plugin lifecycle hooks activated** — `pre_llm_call`, `post_llm_call`, `on_session_start`, and `on_session_end` hooks now fire in the agent loop and CLI/gateway, completing the plugin hook system ([#3542](https://github.com/NousResearch/hermes-agent/pull/3542))
|
| 20 |
+
|
| 21 |
+
- **Improved OpenAI Model Reliability** — Added `GPT_TOOL_USE_GUIDANCE` to prevent GPT models from describing intended actions instead of making tool calls, plus automatic stripping of stale budget warnings from conversation history that caused models to avoid tools across turns ([#3528](https://github.com/NousResearch/hermes-agent/pull/3528))
|
| 22 |
+
|
| 23 |
+
- **Nix flake** — Full uv2nix build, NixOS module with persistent container mode, auto-generated config keys from Python source, and suffix PATHs for agent-friendliness ([#20](https://github.com/NousResearch/hermes-agent/pull/20), [#3274](https://github.com/NousResearch/hermes-agent/pull/3274), [#3061](https://github.com/NousResearch/hermes-agent/pull/3061)) by @alt-glitch
|
| 24 |
+
|
| 25 |
+
- **Supply chain hardening** — Removed compromised `litellm` dependency, pinned all dependency version ranges, regenerated `uv.lock` with hashes, added CI workflow scanning PRs for supply chain attack patterns, and bumped deps to fix CVEs ([#2796](https://github.com/NousResearch/hermes-agent/pull/2796), [#2810](https://github.com/NousResearch/hermes-agent/pull/2810), [#2812](https://github.com/NousResearch/hermes-agent/pull/2812), [#2816](https://github.com/NousResearch/hermes-agent/pull/2816), [#3073](https://github.com/NousResearch/hermes-agent/pull/3073))
|
| 26 |
+
|
| 27 |
+
- **Anthropic output limits fix** — Replaced hardcoded 16K `max_tokens` with per-model native output limits (128K for Opus 4.6, 64K for Sonnet 4.6), fixing "Response truncated" and thinking-budget exhaustion on direct Anthropic API ([#3426](https://github.com/NousResearch/hermes-agent/pull/3426), [#3444](https://github.com/NousResearch/hermes-agent/pull/3444))
|
| 28 |
+
|
| 29 |
+
---
|
| 30 |
+
|
| 31 |
+
## 🏗️ Core Agent & Architecture
|
| 32 |
+
|
| 33 |
+
### New Provider: Hugging Face
|
| 34 |
+
- First-class Hugging Face Inference API integration with auth, setup wizard, and model picker ([#3419](https://github.com/NousResearch/hermes-agent/pull/3419))
|
| 35 |
+
- Curated model list mapping OpenRouter agentic defaults to HF equivalents — providers with 8+ curated models skip live `/models` probe for speed ([#3440](https://github.com/NousResearch/hermes-agent/pull/3440))
|
| 36 |
+
- Added glm-5-turbo to Z.AI provider model list ([#3095](https://github.com/NousResearch/hermes-agent/pull/3095))
|
| 37 |
+
|
| 38 |
+
### Provider & Model Improvements
|
| 39 |
+
- `/model` command overhaul — extracted shared `switch_model()` pipeline for CLI and gateway, custom endpoint support, provider-aware routing ([#2795](https://github.com/NousResearch/hermes-agent/pull/2795), [#2799](https://github.com/NousResearch/hermes-agent/pull/2799))
|
| 40 |
+
- Removed `/model` slash command from CLI and gateway in favor of `hermes model` subcommand ([#3080](https://github.com/NousResearch/hermes-agent/pull/3080))
|
| 41 |
+
- Preserve `custom` provider instead of silently remapping to `openrouter` ([#2792](https://github.com/NousResearch/hermes-agent/pull/2792))
|
| 42 |
+
- Read root-level `provider` and `base_url` from config.yaml into model config ([#3112](https://github.com/NousResearch/hermes-agent/pull/3112))
|
| 43 |
+
- Align Nous Portal model slugs with OpenRouter naming ([#3253](https://github.com/NousResearch/hermes-agent/pull/3253))
|
| 44 |
+
- Fix Alibaba provider default endpoint and model list ([#3484](https://github.com/NousResearch/hermes-agent/pull/3484))
|
| 45 |
+
- Allow MiniMax users to override `/v1` → `/anthropic` auto-correction ([#3553](https://github.com/NousResearch/hermes-agent/pull/3553))
|
| 46 |
+
- Migrate OAuth token refresh to `platform.claude.com` with fallback ([#3246](https://github.com/NousResearch/hermes-agent/pull/3246))
|
| 47 |
+
|
| 48 |
+
### Agent Loop & Conversation
|
| 49 |
+
- **Improved OpenAI model reliability** — `GPT_TOOL_USE_GUIDANCE` prevents GPT models from describing actions instead of calling tools + automatic budget warning stripping from history ([#3528](https://github.com/NousResearch/hermes-agent/pull/3528))
|
| 50 |
+
- **Surface lifecycle events** — All retry, fallback, and compression events now surface to the user as formatted messages ([#3153](https://github.com/NousResearch/hermes-agent/pull/3153))
|
| 51 |
+
- **Anthropic output limits** — Per-model native output limits instead of hardcoded 16K `max_tokens` ([#3426](https://github.com/NousResearch/hermes-agent/pull/3426))
|
| 52 |
+
- **Thinking-budget exhaustion detection** — Skip useless continuation retries when model uses all output tokens on reasoning ([#3444](https://github.com/NousResearch/hermes-agent/pull/3444))
|
| 53 |
+
- Always prefer streaming for API calls to prevent hung subagents ([#3120](https://github.com/NousResearch/hermes-agent/pull/3120))
|
| 54 |
+
- Restore safe non-streaming fallback after stream failures ([#3020](https://github.com/NousResearch/hermes-agent/pull/3020))
|
| 55 |
+
- Give subagents independent iteration budgets ([#3004](https://github.com/NousResearch/hermes-agent/pull/3004))
|
| 56 |
+
- Update `api_key` in `_try_activate_fallback` for subagent auth ([#3103](https://github.com/NousResearch/hermes-agent/pull/3103))
|
| 57 |
+
- Graceful return on max retries instead of crashing thread ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 58 |
+
- Count compression restarts toward retry limit ([#3070](https://github.com/NousResearch/hermes-agent/pull/3070))
|
| 59 |
+
- Include tool tokens in preflight estimate, guard context probe persistence ([#3164](https://github.com/NousResearch/hermes-agent/pull/3164))
|
| 60 |
+
- Update context compressor limits after fallback activation ([#3305](https://github.com/NousResearch/hermes-agent/pull/3305))
|
| 61 |
+
- Validate empty user messages to prevent Anthropic API 400 errors ([#3322](https://github.com/NousResearch/hermes-agent/pull/3322))
|
| 62 |
+
- GLM reasoning-only and max-length handling ([#3010](https://github.com/NousResearch/hermes-agent/pull/3010))
|
| 63 |
+
- Increase API timeout default from 900s to 1800s for slow-thinking models ([#3431](https://github.com/NousResearch/hermes-agent/pull/3431))
|
| 64 |
+
- Send `max_tokens` for Claude/OpenRouter + retry SSE connection errors ([#3497](https://github.com/NousResearch/hermes-agent/pull/3497))
|
| 65 |
+
- Prevent AsyncOpenAI/httpx cross-loop deadlock in gateway mode ([#2701](https://github.com/NousResearch/hermes-agent/pull/2701)) by @ctlst
|
| 66 |
+
|
| 67 |
+
### Streaming & Reasoning
|
| 68 |
+
- **Persist reasoning across gateway session turns** with new schema v6 columns (`reasoning`, `reasoning_details`, `codex_reasoning_items`) ([#2974](https://github.com/NousResearch/hermes-agent/pull/2974))
|
| 69 |
+
- Detect and kill stale SSE connections ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 70 |
+
- Fix stale stream detector race causing spurious `RemoteProtocolError` ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 71 |
+
- Skip duplicate callback for `<think>`-extracted reasoning during streaming ([#3116](https://github.com/NousResearch/hermes-agent/pull/3116))
|
| 72 |
+
- Preserve reasoning fields in `rewrite_transcript` ([#3311](https://github.com/NousResearch/hermes-agent/pull/3311))
|
| 73 |
+
- Preserve Gemini thought signatures in streamed tool calls ([#2997](https://github.com/NousResearch/hermes-agent/pull/2997))
|
| 74 |
+
- Ensure first delta is fired during reasoning updates ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 75 |
+
|
| 76 |
+
### Session & Memory
|
| 77 |
+
- **Session search recent sessions mode** — Omit query to browse recent sessions with titles, previews, and timestamps ([#2533](https://github.com/NousResearch/hermes-agent/pull/2533))
|
| 78 |
+
- **Session config surfacing** on `/new`, `/reset`, and auto-reset ([#3321](https://github.com/NousResearch/hermes-agent/pull/3321))
|
| 79 |
+
- **Third-party session isolation** — `--source` flag for isolating sessions by origin ([#3255](https://github.com/NousResearch/hermes-agent/pull/3255))
|
| 80 |
+
- Add `/resume` CLI handler, session log truncation guard, `reopen_session` API ([#3315](https://github.com/NousResearch/hermes-agent/pull/3315))
|
| 81 |
+
- Clear compressor summary and turn counter on `/clear` and `/new` ([#3102](https://github.com/NousResearch/hermes-agent/pull/3102))
|
| 82 |
+
- Surface silent SessionDB failures that cause session data loss ([#2999](https://github.com/NousResearch/hermes-agent/pull/2999))
|
| 83 |
+
- Session search fallback preview on summarization failure ([#3478](https://github.com/NousResearch/hermes-agent/pull/3478))
|
| 84 |
+
- Prevent stale memory overwrites by flush agent ([#2687](https://github.com/NousResearch/hermes-agent/pull/2687))
|
| 85 |
+
|
| 86 |
+
### Context Compression
|
| 87 |
+
- Replace dead `summary_target_tokens` with ratio-based scaling ([#2554](https://github.com/NousResearch/hermes-agent/pull/2554))
|
| 88 |
+
- Expose `compression.target_ratio`, `protect_last_n`, and `threshold` in `DEFAULT_CONFIG` ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 89 |
+
- Restore sane defaults and cap summary at 12K tokens ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 90 |
+
- Preserve transcript on `/compress` and hygiene compression ([#3556](https://github.com/NousResearch/hermes-agent/pull/3556))
|
| 91 |
+
- Update context pressure warnings and token estimates after compaction ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 92 |
+
|
| 93 |
+
### Architecture & Dependencies
|
| 94 |
+
- **Remove mini-swe-agent dependency** — Inline Docker and Modal backends directly ([#2804](https://github.com/NousResearch/hermes-agent/pull/2804))
|
| 95 |
+
- **Replace swe-rex with native Modal SDK** for Modal backend ([#3538](https://github.com/NousResearch/hermes-agent/pull/3538))
|
| 96 |
+
- **Plugin lifecycle hooks** — `pre_llm_call`, `post_llm_call`, `on_session_start`, `on_session_end` now fire in the agent loop ([#3542](https://github.com/NousResearch/hermes-agent/pull/3542))
|
| 97 |
+
- Fix plugin toolsets invisible in `hermes tools` and standalone processes ([#3457](https://github.com/NousResearch/hermes-agent/pull/3457))
|
| 98 |
+
- Consolidate `get_hermes_home()` and `parse_reasoning_effort()` ([#3062](https://github.com/NousResearch/hermes-agent/pull/3062))
|
| 99 |
+
- Remove unused Hermes-native PKCE OAuth flow ([#3107](https://github.com/NousResearch/hermes-agent/pull/3107))
|
| 100 |
+
- Remove ~100 unused imports across 55 files ([#3016](https://github.com/NousResearch/hermes-agent/pull/3016))
|
| 101 |
+
- Fix 154 f-strings, simplify getattr/URL patterns, remove dead code ([#3119](https://github.com/NousResearch/hermes-agent/pull/3119))
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
|
| 105 |
+
## 📱 Messaging Platforms (Gateway)
|
| 106 |
+
|
| 107 |
+
### Telegram
|
| 108 |
+
- **Private Chat Topics** — Project-based conversations with functional skill binding per topic, enabling isolated workflows within a single Telegram chat ([#3163](https://github.com/NousResearch/hermes-agent/pull/3163))
|
| 109 |
+
- **Auto-discover fallback IPs via DNS-over-HTTPS** when `api.telegram.org` is unreachable ([#3376](https://github.com/NousResearch/hermes-agent/pull/3376))
|
| 110 |
+
- **Configurable reply threading mode** ([#2907](https://github.com/NousResearch/hermes-agent/pull/2907))
|
| 111 |
+
- Fall back to no `thread_id` on "Message thread not found" BadRequest ([#3390](https://github.com/NousResearch/hermes-agent/pull/3390))
|
| 112 |
+
- Self-reschedule reconnect when `start_polling` fails after 502 ([#3268](https://github.com/NousResearch/hermes-agent/pull/3268))
|
| 113 |
+
|
| 114 |
+
### Discord
|
| 115 |
+
- Stop phantom typing indicator after agent turn completes ([#3003](https://github.com/NousResearch/hermes-agent/pull/3003))
|
| 116 |
+
|
| 117 |
+
### Slack
|
| 118 |
+
- Send tool call progress messages to correct Slack thread ([#3063](https://github.com/NousResearch/hermes-agent/pull/3063))
|
| 119 |
+
- Scope progress thread fallback to Slack only ([#3488](https://github.com/NousResearch/hermes-agent/pull/3488))
|
| 120 |
+
|
| 121 |
+
### WhatsApp
|
| 122 |
+
- Download documents, audio, and video media from messages ([#2978](https://github.com/NousResearch/hermes-agent/pull/2978))
|
| 123 |
+
|
| 124 |
+
### Matrix
|
| 125 |
+
- Add missing Matrix entry in `PLATFORMS` dict ([#3473](https://github.com/NousResearch/hermes-agent/pull/3473))
|
| 126 |
+
- Harden e2ee access-token handling ([#3562](https://github.com/NousResearch/hermes-agent/pull/3562))
|
| 127 |
+
- Add backoff for `SyncError` in sync loop ([#3280](https://github.com/NousResearch/hermes-agent/pull/3280))
|
| 128 |
+
|
| 129 |
+
### Signal
|
| 130 |
+
- Track SSE keepalive comments as connection activity ([#3316](https://github.com/NousResearch/hermes-agent/pull/3316))
|
| 131 |
+
|
| 132 |
+
### Email
|
| 133 |
+
- Prevent unbounded growth of `_seen_uids` in EmailAdapter ([#3490](https://github.com/NousResearch/hermes-agent/pull/3490))
|
| 134 |
+
|
| 135 |
+
### Gateway Core
|
| 136 |
+
- **Config-gated `/verbose` command** for messaging platforms — toggle tool output verbosity from chat ([#3262](https://github.com/NousResearch/hermes-agent/pull/3262))
|
| 137 |
+
- **Background review notifications** delivered to user chat ([#3293](https://github.com/NousResearch/hermes-agent/pull/3293))
|
| 138 |
+
- **Retry transient send failures** and notify user on exhaustion ([#3288](https://github.com/NousResearch/hermes-agent/pull/3288))
|
| 139 |
+
- Recover from hung agents — `/stop` hard-kills session lock ([#3104](https://github.com/NousResearch/hermes-agent/pull/3104))
|
| 140 |
+
- Thread-safe `SessionStore` — protect `_entries` with `threading.Lock` ([#3052](https://github.com/NousResearch/hermes-agent/pull/3052))
|
| 141 |
+
- Fix gateway token double-counting with cached agents — use absolute set instead of increment ([#3306](https://github.com/NousResearch/hermes-agent/pull/3306), [#3317](https://github.com/NousResearch/hermes-agent/pull/3317))
|
| 142 |
+
- Fingerprint full auth token in agent cache signature ([#3247](https://github.com/NousResearch/hermes-agent/pull/3247))
|
| 143 |
+
- Silence background agent terminal output ([#3297](https://github.com/NousResearch/hermes-agent/pull/3297))
|
| 144 |
+
- Include per-platform `ALLOW_ALL` and `SIGNAL_GROUP` in startup allowlist check ([#3313](https://github.com/NousResearch/hermes-agent/pull/3313))
|
| 145 |
+
- Include user-local bin paths in systemd unit PATH ([#3527](https://github.com/NousResearch/hermes-agent/pull/3527))
|
| 146 |
+
- Track background task references in `GatewayRunner` ([#3254](https://github.com/NousResearch/hermes-agent/pull/3254))
|
| 147 |
+
- Add request timeouts to HA, Email, Mattermost, SMS adapters ([#3258](https://github.com/NousResearch/hermes-agent/pull/3258))
|
| 148 |
+
- Add media download retry to Mattermost, Slack, and base cache ([#3323](https://github.com/NousResearch/hermes-agent/pull/3323))
|
| 149 |
+
- Detect virtualenv path instead of hardcoding `venv/` ([#2797](https://github.com/NousResearch/hermes-agent/pull/2797))
|
| 150 |
+
- Use `TERMINAL_CWD` for context file discovery, not process cwd ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 151 |
+
- Stop loading hermes repo AGENTS.md into gateway sessions (~10k wasted tokens) ([#2891](https://github.com/NousResearch/hermes-agent/pull/2891))
|
| 152 |
+
|
| 153 |
+
---
|
| 154 |
+
|
| 155 |
+
## 🖥️ CLI & User Experience
|
| 156 |
+
|
| 157 |
+
### Interactive CLI
|
| 158 |
+
- **Configurable busy input mode** + fix `/queue` always working ([#3298](https://github.com/NousResearch/hermes-agent/pull/3298))
|
| 159 |
+
- **Preserve user input on multiline paste** ([#3065](https://github.com/NousResearch/hermes-agent/pull/3065))
|
| 160 |
+
- **Tool generation callback** — streaming "preparing terminal…" updates during tool argument generation ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 161 |
+
- Show tool progress for substantive tools, not just "preparing" ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 162 |
+
- Buffer reasoning preview chunks and fix duplicate display ([#3013](https://github.com/NousResearch/hermes-agent/pull/3013))
|
| 163 |
+
- Prevent reasoning box from rendering 3x during tool-calling loops ([#3405](https://github.com/NousResearch/hermes-agent/pull/3405))
|
| 164 |
+
- Eliminate "Event loop is closed" / "Press ENTER to continue" during idle — three-layer fix with `neuter_async_httpx_del()`, custom exception handler, and stale client cleanup ([#3398](https://github.com/NousResearch/hermes-agent/pull/3398))
|
| 165 |
+
- Fix status bar shows 26K instead of 260K for token counts with trailing zeros ([#3024](https://github.com/NousResearch/hermes-agent/pull/3024))
|
| 166 |
+
- Fix status bar duplicates and degrades during long sessions ([#3291](https://github.com/NousResearch/hermes-agent/pull/3291))
|
| 167 |
+
- Refresh TUI before background task output to prevent status bar overlap ([#3048](https://github.com/NousResearch/hermes-agent/pull/3048))
|
| 168 |
+
- Suppress KawaiiSpinner animation under `patch_stdout` ([#2994](https://github.com/NousResearch/hermes-agent/pull/2994))
|
| 169 |
+
- Skip KawaiiSpinner when TUI handles tool progress ([#2973](https://github.com/NousResearch/hermes-agent/pull/2973))
|
| 170 |
+
- Guard `isatty()` against closed streams via `_is_tty` property ([#3056](https://github.com/NousResearch/hermes-agent/pull/3056))
|
| 171 |
+
- Ensure single closure of streaming boxes during tool generation ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 172 |
+
- Cap context pressure percentage at 100% in display ([#3480](https://github.com/NousResearch/hermes-agent/pull/3480))
|
| 173 |
+
- Clean up HTML error messages in CLI display ([#3069](https://github.com/NousResearch/hermes-agent/pull/3069))
|
| 174 |
+
- Show HTTP status code and 400 body in API error output ([#3096](https://github.com/NousResearch/hermes-agent/pull/3096))
|
| 175 |
+
- Extract useful info from HTML error pages, dump debug on max retries ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 176 |
+
- Prevent TypeError on startup when `base_url` is None ([#3068](https://github.com/NousResearch/hermes-agent/pull/3068))
|
| 177 |
+
- Prevent update crash in non-TTY environments ([#3094](https://github.com/NousResearch/hermes-agent/pull/3094))
|
| 178 |
+
- Handle EOFError in sessions delete/prune confirmation prompts ([#3101](https://github.com/NousResearch/hermes-agent/pull/3101))
|
| 179 |
+
- Catch KeyboardInterrupt during `flush_memories` on exit and in exit cleanup handlers ([#3025](https://github.com/NousResearch/hermes-agent/pull/3025), [#3257](https://github.com/NousResearch/hermes-agent/pull/3257))
|
| 180 |
+
- Guard `.strip()` against None values from YAML config ([#3552](https://github.com/NousResearch/hermes-agent/pull/3552))
|
| 181 |
+
- Guard `config.get()` against YAML null values to prevent AttributeError ([#3377](https://github.com/NousResearch/hermes-agent/pull/3377))
|
| 182 |
+
- Store asyncio task references to prevent GC mid-execution ([#3267](https://github.com/NousResearch/hermes-agent/pull/3267))
|
| 183 |
+
|
| 184 |
+
### Setup & Configuration
|
| 185 |
+
- Use explicit key mapping for returning-user menu dispatch instead of positional index ([#3083](https://github.com/NousResearch/hermes-agent/pull/3083))
|
| 186 |
+
- Use `sys.executable` for pip in update commands to fix PEP 668 ([#3099](https://github.com/NousResearch/hermes-agent/pull/3099))
|
| 187 |
+
- Harden `hermes update` against diverged history, non-main branches, and gateway edge cases ([#3492](https://github.com/NousResearch/hermes-agent/pull/3492))
|
| 188 |
+
- OpenClaw migration overwrites defaults and setup wizard skips imported sections — fixed ([#3282](https://github.com/NousResearch/hermes-agent/pull/3282))
|
| 189 |
+
- Stop recursive AGENTS.md walk, load top-level only ([#3110](https://github.com/NousResearch/hermes-agent/pull/3110))
|
| 190 |
+
- Add macOS Homebrew paths to browser and terminal PATH resolution ([#2713](https://github.com/NousResearch/hermes-agent/pull/2713))
|
| 191 |
+
- YAML boolean handling for `tool_progress` config ([#3300](https://github.com/NousResearch/hermes-agent/pull/3300))
|
| 192 |
+
- Reset default SOUL.md to baseline identity text ([#3159](https://github.com/NousResearch/hermes-agent/pull/3159))
|
| 193 |
+
- Reject relative cwd paths for container terminal backends ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 194 |
+
- Add explicit `hermes-api-server` toolset for API server platform ([#3304](https://github.com/NousResearch/hermes-agent/pull/3304))
|
| 195 |
+
- Reorder setup wizard providers — OpenRouter first ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 196 |
+
|
| 197 |
+
---
|
| 198 |
+
|
| 199 |
+
## 🔧 Tool System
|
| 200 |
+
|
| 201 |
+
### API Server
|
| 202 |
+
- **Idempotency-Key support**, body size limit, and OpenAI error envelope ([#2903](https://github.com/NousResearch/hermes-agent/pull/2903))
|
| 203 |
+
- Allow Idempotency-Key in CORS headers ([#3530](https://github.com/NousResearch/hermes-agent/pull/3530))
|
| 204 |
+
- Cancel orphaned agent + true interrupt on SSE disconnect ([#3427](https://github.com/NousResearch/hermes-agent/pull/3427))
|
| 205 |
+
- Fix streaming breaks when agent makes tool calls ([#2985](https://github.com/NousResearch/hermes-agent/pull/2985))
|
| 206 |
+
|
| 207 |
+
### Terminal & File Operations
|
| 208 |
+
- Handle addition-only hunks in V4A patch parser ([#3325](https://github.com/NousResearch/hermes-agent/pull/3325))
|
| 209 |
+
- Exponential backoff for persistent shell polling ([#2996](https://github.com/NousResearch/hermes-agent/pull/2996))
|
| 210 |
+
- Add timeout to subprocess calls in `context_references` ([#3469](https://github.com/NousResearch/hermes-agent/pull/3469))
|
| 211 |
+
|
| 212 |
+
### Browser & Vision
|
| 213 |
+
- Handle 402 insufficient credits error in vision tool ([#2802](https://github.com/NousResearch/hermes-agent/pull/2802))
|
| 214 |
+
- Fix `browser_vision` ignores `auxiliary.vision.timeout` config ([#2901](https://github.com/NousResearch/hermes-agent/pull/2901))
|
| 215 |
+
- Make browser command timeout configurable via config.yaml ([#2801](https://github.com/NousResearch/hermes-agent/pull/2801))
|
| 216 |
+
|
| 217 |
+
### MCP
|
| 218 |
+
- MCP toolset resolution for runtime and config ([#3252](https://github.com/NousResearch/hermes-agent/pull/3252))
|
| 219 |
+
- Add MCP tool name collision protection ([#3077](https://github.com/NousResearch/hermes-agent/pull/3077))
|
| 220 |
+
|
| 221 |
+
### Auxiliary LLM
|
| 222 |
+
- Guard aux LLM calls against None content + reasoning fallback + retry ([#3449](https://github.com/NousResearch/hermes-agent/pull/3449))
|
| 223 |
+
- Catch ImportError from `build_anthropic_client` in vision auto-detection ([#3312](https://github.com/NousResearch/hermes-agent/pull/3312))
|
| 224 |
+
|
| 225 |
+
### Other Tools
|
| 226 |
+
- Add request timeouts to `send_message_tool` HTTP calls ([#3162](https://github.com/NousResearch/hermes-agent/pull/3162)) by @memosr
|
| 227 |
+
- Auto-repair `jobs.json` with invalid control characters ([#3537](https://github.com/NousResearch/hermes-agent/pull/3537))
|
| 228 |
+
- Enable fine-grained tool streaming for Claude/OpenRouter ([#3497](https://github.com/NousResearch/hermes-agent/pull/3497))
|
| 229 |
+
|
| 230 |
+
---
|
| 231 |
+
|
| 232 |
+
## 🧩 Skills Ecosystem
|
| 233 |
+
|
| 234 |
+
### Skills System
|
| 235 |
+
- **Env var passthrough** for skills and user config — skills can declare environment variables to pass through ([#2807](https://github.com/NousResearch/hermes-agent/pull/2807))
|
| 236 |
+
- Cache skills prompt with shared `skill_utils` module for faster TTFT ([#3421](https://github.com/NousResearch/hermes-agent/pull/3421))
|
| 237 |
+
- Avoid redundant file re-read for skill conditions ([#2992](https://github.com/NousResearch/hermes-agent/pull/2992))
|
| 238 |
+
- Use Git Trees API to prevent silent subdirectory loss during install ([#2995](https://github.com/NousResearch/hermes-agent/pull/2995))
|
| 239 |
+
- Fix skills-sh install for deeply nested repo structures ([#2980](https://github.com/NousResearch/hermes-agent/pull/2980))
|
| 240 |
+
- Handle null metadata in skill frontmatter ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 241 |
+
- Preserve trust for skills-sh identifiers + reduce resolution churn ([#3251](https://github.com/NousResearch/hermes-agent/pull/3251))
|
| 242 |
+
- Agent-created skills were incorrectly treated as untrusted community content — fixed ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 243 |
+
|
| 244 |
+
### New Skills
|
| 245 |
+
- **G0DM0D3 godmode jailbreaking skill** + docs ([#3157](https://github.com/NousResearch/hermes-agent/pull/3157))
|
| 246 |
+
- **Docker management skill** added to optional-skills ([#3060](https://github.com/NousResearch/hermes-agent/pull/3060))
|
| 247 |
+
- **OpenClaw migration v2** — 17 new modules, terminal recap for migrating from OpenClaw to Hermes ([#2906](https://github.com/NousResearch/hermes-agent/pull/2906))
|
| 248 |
+
|
| 249 |
+
---
|
| 250 |
+
|
| 251 |
+
## 🔒 Security & Reliability
|
| 252 |
+
|
| 253 |
+
### Security Hardening
|
| 254 |
+
- **SSRF protection** added to `browser_navigate` ([#3058](https://github.com/NousResearch/hermes-agent/pull/3058))
|
| 255 |
+
- **SSRF protection** added to `vision_tools` and `web_tools` (hardened) ([#2679](https://github.com/NousResearch/hermes-agent/pull/2679))
|
| 256 |
+
- **Restrict subagent toolsets** to parent's enabled set ([#3269](https://github.com/NousResearch/hermes-agent/pull/3269))
|
| 257 |
+
- **Prevent zip-slip path traversal** in self-update ([#3250](https://github.com/NousResearch/hermes-agent/pull/3250))
|
| 258 |
+
- **Prevent shell injection** in `_expand_path` via `~user` path suffix ([#2685](https://github.com/NousResearch/hermes-agent/pull/2685))
|
| 259 |
+
- **Normalize input** before dangerous command detection ([#3260](https://github.com/NousResearch/hermes-agent/pull/3260))
|
| 260 |
+
- Make tirith block verdicts approvable instead of hard-blocking ([#3428](https://github.com/NousResearch/hermes-agent/pull/3428))
|
| 261 |
+
- Remove compromised `litellm`/`typer`/`platformdirs` from deps ([#2796](https://github.com/NousResearch/hermes-agent/pull/2796))
|
| 262 |
+
- Pin all dependency version ranges ([#2810](https://github.com/NousResearch/hermes-agent/pull/2810))
|
| 263 |
+
- Regenerate `uv.lock` with hashes, use lockfile in setup ([#2812](https://github.com/NousResearch/hermes-agent/pull/2812))
|
| 264 |
+
- Bump dependencies to fix CVEs + regenerate `uv.lock` ([#3073](https://github.com/NousResearch/hermes-agent/pull/3073))
|
| 265 |
+
- Supply chain audit CI workflow for PR scanning ([#2816](https://github.com/NousResearch/hermes-agent/pull/2816))
|
| 266 |
+
|
| 267 |
+
### Reliability
|
| 268 |
+
- **SQLite WAL write-lock contention** causing 15-20s TUI freeze — fixed ([#3385](https://github.com/NousResearch/hermes-agent/pull/3385))
|
| 269 |
+
- **SQLite concurrency hardening** + session transcript integrity ([#3249](https://github.com/NousResearch/hermes-agent/pull/3249))
|
| 270 |
+
- Prevent recurring cron job re-fire on gateway crash/restart loop ([#3396](https://github.com/NousResearch/hermes-agent/pull/3396))
|
| 271 |
+
- Mark cron session as ended after job completes ([#2998](https://github.com/NousResearch/hermes-agent/pull/2998))
|
| 272 |
+
|
| 273 |
+
---
|
| 274 |
+
|
| 275 |
+
## ⚡ Performance
|
| 276 |
+
|
| 277 |
+
- **TTFT startup optimizations** — salvaged easy-win startup improvements ([#3395](https://github.com/NousResearch/hermes-agent/pull/3395))
|
| 278 |
+
- Cache skills prompt with shared `skill_utils` module ([#3421](https://github.com/NousResearch/hermes-agent/pull/3421))
|
| 279 |
+
- Avoid redundant file re-read for skill conditions in prompt builder ([#2992](https://github.com/NousResearch/hermes-agent/pull/2992))
|
| 280 |
+
|
| 281 |
+
---
|
| 282 |
+
|
| 283 |
+
## 🐛 Notable Bug Fixes
|
| 284 |
+
|
| 285 |
+
- Fix gateway token double-counting with cached agents ([#3306](https://github.com/NousResearch/hermes-agent/pull/3306), [#3317](https://github.com/NousResearch/hermes-agent/pull/3317))
|
| 286 |
+
- Fix "Event loop is closed" / "Press ENTER to continue" during idle sessions ([#3398](https://github.com/NousResearch/hermes-agent/pull/3398))
|
| 287 |
+
- Fix reasoning box rendering 3x during tool-calling loops ([#3405](https://github.com/NousResearch/hermes-agent/pull/3405))
|
| 288 |
+
- Fix status bar shows 26K instead of 260K for token counts ([#3024](https://github.com/NousResearch/hermes-agent/pull/3024))
|
| 289 |
+
- Fix `/queue` always working regardless of config ([#3298](https://github.com/NousResearch/hermes-agent/pull/3298))
|
| 290 |
+
- Fix phantom Discord typing indicator after agent turn ([#3003](https://github.com/NousResearch/hermes-agent/pull/3003))
|
| 291 |
+
- Fix Slack progress messages appearing in wrong thread ([#3063](https://github.com/NousResearch/hermes-agent/pull/3063))
|
| 292 |
+
- Fix WhatsApp media downloads (documents, audio, video) ([#2978](https://github.com/NousResearch/hermes-agent/pull/2978))
|
| 293 |
+
- Fix Telegram "Message thread not found" killing progress messages ([#3390](https://github.com/NousResearch/hermes-agent/pull/3390))
|
| 294 |
+
- Fix OpenClaw migration overwriting defaults ([#3282](https://github.com/NousResearch/hermes-agent/pull/3282))
|
| 295 |
+
- Fix returning-user setup menu dispatching wrong section ([#3083](https://github.com/NousResearch/hermes-agent/pull/3083))
|
| 296 |
+
- Fix `hermes update` PEP 668 "externally-managed-environment" error ([#3099](https://github.com/NousResearch/hermes-agent/pull/3099))
|
| 297 |
+
- Fix subagents hitting `max_iterations` prematurely via shared budget ([#3004](https://github.com/NousResearch/hermes-agent/pull/3004))
|
| 298 |
+
- Fix YAML boolean handling for `tool_progress` config ([#3300](https://github.com/NousResearch/hermes-agent/pull/3300))
|
| 299 |
+
- Fix `config.get()` crashes on YAML null values ([#3377](https://github.com/NousResearch/hermes-agent/pull/3377))
|
| 300 |
+
- Fix `.strip()` crash on None values from YAML config ([#3552](https://github.com/NousResearch/hermes-agent/pull/3552))
|
| 301 |
+
- Fix hung agents on gateway — `/stop` now hard-kills session lock ([#3104](https://github.com/NousResearch/hermes-agent/pull/3104))
|
| 302 |
+
- Fix `_custom` provider silently remapped to `openrouter` ([#2792](https://github.com/NousResearch/hermes-agent/pull/2792))
|
| 303 |
+
- Fix Matrix missing from `PLATFORMS` dict ([#3473](https://github.com/NousResearch/hermes-agent/pull/3473))
|
| 304 |
+
- Fix Email adapter unbounded `_seen_uids` growth ([#3490](https://github.com/NousResearch/hermes-agent/pull/3490))
|
| 305 |
+
|
| 306 |
+
---
|
| 307 |
+
|
| 308 |
+
## 🧪 Testing
|
| 309 |
+
|
| 310 |
+
- Pin `agent-client-protocol` < 0.9 to handle breaking upstream release ([#3320](https://github.com/NousResearch/hermes-agent/pull/3320))
|
| 311 |
+
- Catch anthropic ImportError in vision auto-detection tests ([#3312](https://github.com/NousResearch/hermes-agent/pull/3312))
|
| 312 |
+
- Update retry-exhaust test for new graceful return behavior ([#3320](https://github.com/NousResearch/hermes-agent/pull/3320))
|
| 313 |
+
- Add regression tests for null metadata frontmatter ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 314 |
+
|
| 315 |
+
---
|
| 316 |
+
|
| 317 |
+
## 📚 Documentation
|
| 318 |
+
|
| 319 |
+
- Update all docs for `/model` command overhaul and custom provider support ([#2800](https://github.com/NousResearch/hermes-agent/pull/2800))
|
| 320 |
+
- Fix stale and incorrect documentation across 18 files ([#2805](https://github.com/NousResearch/hermes-agent/pull/2805))
|
| 321 |
+
- Document 9 previously undocumented features ([#2814](https://github.com/NousResearch/hermes-agent/pull/2814))
|
| 322 |
+
- Add missing skills, CLI commands, and messaging env vars to docs ([#2809](https://github.com/NousResearch/hermes-agent/pull/2809))
|
| 323 |
+
- Fix api-server response storage documentation — SQLite, not in-memory ([#2819](https://github.com/NousResearch/hermes-agent/pull/2819))
|
| 324 |
+
- Quote pip install extras to fix zsh glob errors ([#2815](https://github.com/NousResearch/hermes-agent/pull/2815))
|
| 325 |
+
- Unify hooks documentation — add plugin hooks to hooks page, add `session:end` event ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 326 |
+
- Clarify two-mode behavior in `session_search` schema description ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 327 |
+
- Fix Discord Public Bot setting for Discord-provided invite link ([#3519](https://github.com/NousResearch/hermes-agent/pull/3519)) by @mehmoodosman
|
| 328 |
+
- Revise v0.4.0 changelog — fix feature attribution, reorder sections ([untagged commit](https://github.com/NousResearch/hermes-agent))
|
| 329 |
+
|
| 330 |
+
---
|
| 331 |
+
|
| 332 |
+
## 👥 Contributors
|
| 333 |
+
|
| 334 |
+
### Core
|
| 335 |
+
- **@teknium1** — 157 PRs covering the full scope of this release
|
| 336 |
+
|
| 337 |
+
### Community Contributors
|
| 338 |
+
- **@alt-glitch** (Siddharth Balyan) — 2 PRs: Nix flake with uv2nix build, NixOS module, and persistent container mode ([#20](https://github.com/NousResearch/hermes-agent/pull/20)); auto-generated config keys and suffix PATHs for Nix builds ([#3061](https://github.com/NousResearch/hermes-agent/pull/3061), [#3274](https://github.com/NousResearch/hermes-agent/pull/3274))
|
| 339 |
+
- **@ctlst** — 1 PR: Prevent AsyncOpenAI/httpx cross-loop deadlock in gateway mode ([#2701](https://github.com/NousResearch/hermes-agent/pull/2701))
|
| 340 |
+
- **@memosr** (memosr.eth) — 1 PR: Add request timeouts to `send_message_tool` HTTP calls ([#3162](https://github.com/NousResearch/hermes-agent/pull/3162))
|
| 341 |
+
- **@mehmoodosman** (Osman Mehmood) — 1 PR: Fix Discord docs for Public Bot setting ([#3519](https://github.com/NousResearch/hermes-agent/pull/3519))
|
| 342 |
+
|
| 343 |
+
### All Contributors
|
| 344 |
+
@alt-glitch, @ctlst, @mehmoodosman, @memosr, @teknium1
|
| 345 |
+
|
| 346 |
+
---
|
| 347 |
+
|
| 348 |
+
**Full Changelog**: [v2026.3.23...v2026.3.28](https://github.com/NousResearch/hermes-agent/compare/v2026.3.23...v2026.3.28)
|
RELEASE_v0.6.0.md
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hermes Agent v0.6.0 (v2026.3.30)
|
| 2 |
+
|
| 3 |
+
**Release Date:** March 30, 2026
|
| 4 |
+
|
| 5 |
+
> The multi-instance release — Profiles for running isolated agent instances, MCP server mode, Docker container, fallback provider chains, two new messaging platforms (Feishu/Lark and WeCom), Telegram webhook mode, Slack multi-workspace OAuth, 95 PRs and 16 resolved issues in 2 days.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## ✨ Highlights
|
| 10 |
+
|
| 11 |
+
- **Profiles — Multi-Instance Hermes** — Run multiple isolated Hermes instances from the same installation. Each profile gets its own config, memory, sessions, skills, and gateway service. Create with `hermes profile create`, switch with `hermes -p <name>`, export/import for sharing. Full token-lock isolation prevents two profiles from using the same bot credential. ([#3681](https://github.com/NousResearch/hermes-agent/pull/3681))
|
| 12 |
+
|
| 13 |
+
- **MCP Server Mode** — Expose Hermes conversations and sessions to any MCP-compatible client (Claude Desktop, Cursor, VS Code, etc.) via `hermes mcp serve`. Browse conversations, read messages, search across sessions, and manage attachments — all through the Model Context Protocol. Supports both stdio and Streamable HTTP transports. ([#3795](https://github.com/NousResearch/hermes-agent/pull/3795))
|
| 14 |
+
|
| 15 |
+
- **Docker Container** — Official Dockerfile for running Hermes Agent in a container. Supports both CLI and gateway modes with volume-mounted config. ([#3668](https://github.com/NousResearch/hermes-agent/pull/3668), closes [#850](https://github.com/NousResearch/hermes-agent/issues/850))
|
| 16 |
+
|
| 17 |
+
- **Ordered Fallback Provider Chain** — Configure multiple inference providers with automatic failover. When your primary provider returns errors or is unreachable, Hermes automatically tries the next provider in the chain. Configure via `fallback_providers` in config.yaml. ([#3813](https://github.com/NousResearch/hermes-agent/pull/3813), closes [#1734](https://github.com/NousResearch/hermes-agent/issues/1734))
|
| 18 |
+
|
| 19 |
+
- **Feishu/Lark Platform Support** — Full gateway adapter for Feishu (飞书) and Lark with event subscriptions, message cards, group chat, image/file attachments, and interactive card callbacks. ([#3799](https://github.com/NousResearch/hermes-agent/pull/3799), [#3817](https://github.com/NousResearch/hermes-agent/pull/3817), closes [#1788](https://github.com/NousResearch/hermes-agent/issues/1788))
|
| 20 |
+
|
| 21 |
+
- **WeCom (Enterprise WeChat) Platform Support** — New gateway adapter for WeCom (企业微信) with text/image/voice messages, group chats, and callback verification. ([#3847](https://github.com/NousResearch/hermes-agent/pull/3847))
|
| 22 |
+
|
| 23 |
+
- **Slack Multi-Workspace OAuth** — Connect a single Hermes gateway to multiple Slack workspaces via OAuth token file. Each workspace gets its own bot token, resolved dynamically per incoming event. ([#3903](https://github.com/NousResearch/hermes-agent/pull/3903))
|
| 24 |
+
|
| 25 |
+
- **Telegram Webhook Mode & Group Controls** — Run the Telegram adapter in webhook mode as an alternative to polling — faster response times and better for production deployments behind a reverse proxy. New group mention gating controls when the bot responds: always, only when @mentioned, or via regex triggers. ([#3880](https://github.com/NousResearch/hermes-agent/pull/3880), [#3870](https://github.com/NousResearch/hermes-agent/pull/3870))
|
| 26 |
+
|
| 27 |
+
- **Exa Search Backend** — Add Exa as an alternative web search and content extraction backend alongside Firecrawl and DuckDuckGo. Set `EXA_API_KEY` and configure as preferred backend. ([#3648](https://github.com/NousResearch/hermes-agent/pull/3648))
|
| 28 |
+
|
| 29 |
+
- **Skills & Credentials on Remote Backends** — Mount skill directories and credential files into Modal and Docker containers, so remote terminal sessions have access to the same skills and secrets as local execution. ([#3890](https://github.com/NousResearch/hermes-agent/pull/3890), [#3671](https://github.com/NousResearch/hermes-agent/pull/3671), closes [#3665](https://github.com/NousResearch/hermes-agent/issues/3665), [#3433](https://github.com/NousResearch/hermes-agent/issues/3433))
|
| 30 |
+
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
## 🏗️ Core Agent & Architecture
|
| 34 |
+
|
| 35 |
+
### Provider & Model Support
|
| 36 |
+
- **Ordered fallback provider chain** — automatic failover across multiple configured providers ([#3813](https://github.com/NousResearch/hermes-agent/pull/3813))
|
| 37 |
+
- **Fix api_mode on provider switch** — switching providers via `hermes model` now correctly clears stale `api_mode` instead of hardcoding `chat_completions`, fixing 404s for providers with Anthropic-compatible endpoints ([#3726](https://github.com/NousResearch/hermes-agent/pull/3726), [#3857](https://github.com/NousResearch/hermes-agent/pull/3857), closes [#3685](https://github.com/NousResearch/hermes-agent/issues/3685))
|
| 38 |
+
- **Stop silent OpenRouter fallback** — when no provider is configured, Hermes now raises a clear error instead of silently routing to OpenRouter ([#3807](https://github.com/NousResearch/hermes-agent/pull/3807), [#3862](https://github.com/NousResearch/hermes-agent/pull/3862))
|
| 39 |
+
- **Gemini 3.1 preview models** — added to OpenRouter and Nous Portal catalogs ([#3803](https://github.com/NousResearch/hermes-agent/pull/3803), closes [#3753](https://github.com/NousResearch/hermes-agent/issues/3753))
|
| 40 |
+
- **Gemini direct API context length** — full context length resolution for direct Google AI endpoints ([#3876](https://github.com/NousResearch/hermes-agent/pull/3876))
|
| 41 |
+
- **gpt-5.4-mini** added to Codex fallback catalog ([#3855](https://github.com/NousResearch/hermes-agent/pull/3855))
|
| 42 |
+
- **Curated model lists preferred** over live API probe when the probe returns fewer models ([#3856](https://github.com/NousResearch/hermes-agent/pull/3856), [#3867](https://github.com/NousResearch/hermes-agent/pull/3867))
|
| 43 |
+
- **User-friendly 429 rate limit messages** with Retry-After countdown ([#3809](https://github.com/NousResearch/hermes-agent/pull/3809))
|
| 44 |
+
- **Auxiliary client placeholder key** for local servers without auth requirements ([#3842](https://github.com/NousResearch/hermes-agent/pull/3842))
|
| 45 |
+
- **INFO-level logging** for auxiliary provider resolution ([#3866](https://github.com/NousResearch/hermes-agent/pull/3866))
|
| 46 |
+
|
| 47 |
+
### Agent Loop & Conversation
|
| 48 |
+
- **Subagent status reporting** — reports `completed` status when summary exists instead of generic failure ([#3829](https://github.com/NousResearch/hermes-agent/pull/3829))
|
| 49 |
+
- **Session log file updated during compression** — prevents stale file references after context compression ([#3835](https://github.com/NousResearch/hermes-agent/pull/3835))
|
| 50 |
+
- **Omit empty tools param** — sends no `tools` parameter when empty instead of `None`, fixing compatibility with strict providers ([#3820](https://github.com/NousResearch/hermes-agent/pull/3820))
|
| 51 |
+
|
| 52 |
+
### Profiles & Multi-Instance
|
| 53 |
+
- **Profiles system** — `hermes profile create/list/switch/delete/export/import/rename`. Each profile gets isolated HERMES_HOME, gateway service, CLI wrapper. Token locks prevent credential collisions. Tab completion for profile names. ([#3681](https://github.com/NousResearch/hermes-agent/pull/3681))
|
| 54 |
+
- **Profile-aware display paths** — all user-facing `~/.hermes` paths replaced with `display_hermes_home()` to show the correct profile directory ([#3623](https://github.com/NousResearch/hermes-agent/pull/3623))
|
| 55 |
+
- **Lazy display_hermes_home imports** — prevents `ImportError` during `hermes update` when modules cache stale bytecode ([#3776](https://github.com/NousResearch/hermes-agent/pull/3776))
|
| 56 |
+
- **HERMES_HOME for protected paths** — `.env` write-deny path now respects HERMES_HOME instead of hardcoded `~/.hermes` ([#3840](https://github.com/NousResearch/hermes-agent/pull/3840))
|
| 57 |
+
|
| 58 |
+
---
|
| 59 |
+
|
| 60 |
+
## 📱 Messaging Platforms (Gateway)
|
| 61 |
+
|
| 62 |
+
### New Platforms
|
| 63 |
+
- **Feishu/Lark** — Full adapter with event subscriptions, message cards, group chat, image/file attachments, interactive card callbacks ([#3799](https://github.com/NousResearch/hermes-agent/pull/3799), [#3817](https://github.com/NousResearch/hermes-agent/pull/3817))
|
| 64 |
+
- **WeCom (Enterprise WeChat)** — Text/image/voice messages, group chats, callback verification ([#3847](https://github.com/NousResearch/hermes-agent/pull/3847))
|
| 65 |
+
|
| 66 |
+
### Telegram
|
| 67 |
+
- **Webhook mode** — run as webhook endpoint instead of polling for production deployments ([#3880](https://github.com/NousResearch/hermes-agent/pull/3880))
|
| 68 |
+
- **Group mention gating & regex triggers** — configurable bot response behavior in groups: always, @mention-only, or regex-matched ([#3870](https://github.com/NousResearch/hermes-agent/pull/3870))
|
| 69 |
+
- **Gracefully handle deleted reply targets** — no more crashes when the message being replied to was deleted ([#3858](https://github.com/NousResearch/hermes-agent/pull/3858), closes [#3229](https://github.com/NousResearch/hermes-agent/issues/3229))
|
| 70 |
+
|
| 71 |
+
### Discord
|
| 72 |
+
- **Message processing reactions** — adds a reaction emoji while processing and removes it when done, giving visual feedback in channels ([#3871](https://github.com/NousResearch/hermes-agent/pull/3871))
|
| 73 |
+
- **DISCORD_IGNORE_NO_MENTION** — skip messages that @mention other users/bots but not Hermes ([#3640](https://github.com/NousResearch/hermes-agent/pull/3640))
|
| 74 |
+
- **Clean up deferred "thinking..."** — properly removes the "thinking..." indicator after slash commands complete ([#3674](https://github.com/NousResearch/hermes-agent/pull/3674), closes [#3595](https://github.com/NousResearch/hermes-agent/issues/3595))
|
| 75 |
+
|
| 76 |
+
### Slack
|
| 77 |
+
- **Multi-workspace OAuth** — connect to multiple Slack workspaces from a single gateway via OAuth token file ([#3903](https://github.com/NousResearch/hermes-agent/pull/3903))
|
| 78 |
+
|
| 79 |
+
### WhatsApp
|
| 80 |
+
- **Persistent aiohttp session** — reuse HTTP sessions across requests instead of creating new ones per message ([#3818](https://github.com/NousResearch/hermes-agent/pull/3818))
|
| 81 |
+
- **LID↔phone alias resolution** — correctly match Linked ID and phone number formats in allowlists ([#3830](https://github.com/NousResearch/hermes-agent/pull/3830))
|
| 82 |
+
- **Skip reply prefix in bot mode** — cleaner message formatting when running as a WhatsApp bot ([#3931](https://github.com/NousResearch/hermes-agent/pull/3931))
|
| 83 |
+
|
| 84 |
+
### Matrix
|
| 85 |
+
- **Native voice messages via MSC3245** — send voice messages as proper Matrix voice events instead of file attachments ([#3877](https://github.com/NousResearch/hermes-agent/pull/3877))
|
| 86 |
+
|
| 87 |
+
### Mattermost
|
| 88 |
+
- **Configurable mention behavior** — respond to messages without requiring @mention ([#3664](https://github.com/NousResearch/hermes-agent/pull/3664))
|
| 89 |
+
|
| 90 |
+
### Signal
|
| 91 |
+
- **URL-encode phone numbers** and correct attachment RPC parameter — fixes delivery failures with certain phone number formats ([#3670](https://github.com/NousResearch/hermes-agent/pull/3670)) — @kshitijk4poor
|
| 92 |
+
|
| 93 |
+
### Email
|
| 94 |
+
- **Close SMTP/IMAP connections on failure** — prevents connection leaks during error scenarios ([#3804](https://github.com/NousResearch/hermes-agent/pull/3804))
|
| 95 |
+
|
| 96 |
+
### Gateway Core
|
| 97 |
+
- **Atomic config writes** — use atomic file writes for config.yaml to prevent data loss during crashes ([#3800](https://github.com/NousResearch/hermes-agent/pull/3800))
|
| 98 |
+
- **Home channel env overrides** — apply environment variable overrides for home channels consistently ([#3796](https://github.com/NousResearch/hermes-agent/pull/3796), [#3808](https://github.com/NousResearch/hermes-agent/pull/3808))
|
| 99 |
+
- **Replace print() with logger** — BasePlatformAdapter now uses proper logging instead of print statements ([#3669](https://github.com/NousResearch/hermes-agent/pull/3669))
|
| 100 |
+
- **Cron delivery labels** — resolve human-friendly delivery labels via channel directory ([#3860](https://github.com/NousResearch/hermes-agent/pull/3860), closes [#1945](https://github.com/NousResearch/hermes-agent/issues/1945))
|
| 101 |
+
- **Cron [SILENT] tightening** — prevent agents from prefixing reports with [SILENT] to suppress delivery ([#3901](https://github.com/NousResearch/hermes-agent/pull/3901))
|
| 102 |
+
- **Background task media delivery** and vision download timeout fixes ([#3919](https://github.com/NousResearch/hermes-agent/pull/3919))
|
| 103 |
+
- **Boot-md hook** — example built-in hook to run a BOOT.md file on gateway startup ([#3733](https://github.com/NousResearch/hermes-agent/pull/3733))
|
| 104 |
+
|
| 105 |
+
---
|
| 106 |
+
|
| 107 |
+
## 🖥️ CLI & User Experience
|
| 108 |
+
|
| 109 |
+
### Interactive CLI
|
| 110 |
+
- **Configurable tool preview length** — show full file paths by default instead of truncating at 40 chars ([#3841](https://github.com/NousResearch/hermes-agent/pull/3841))
|
| 111 |
+
- **Tool token context display** — `hermes tools` checklist now shows estimated token cost per toolset ([#3805](https://github.com/NousResearch/hermes-agent/pull/3805))
|
| 112 |
+
- **/bg spinner TUI fix** — route background task spinner through the TUI widget to prevent status bar collision ([#3643](https://github.com/NousResearch/hermes-agent/pull/3643))
|
| 113 |
+
- **Prevent status bar wrapping** into duplicate rows ([#3883](https://github.com/NousResearch/hermes-agent/pull/3883)) — @kshitijk4poor
|
| 114 |
+
- **Handle closed stdout ValueError** in safe print paths — fixes crashes when stdout is closed during gateway thread shutdown ([#3843](https://github.com/NousResearch/hermes-agent/pull/3843), closes [#3534](https://github.com/NousResearch/hermes-agent/issues/3534))
|
| 115 |
+
- **Remove input() from /tools disable** — eliminates freeze in terminal when disabling tools ([#3918](https://github.com/NousResearch/hermes-agent/pull/3918))
|
| 116 |
+
- **TTY guard for interactive CLI commands** — prevent CPU spin when launched without a terminal ([#3933](https://github.com/NousResearch/hermes-agent/pull/3933))
|
| 117 |
+
- **Argparse entrypoint** — use argparse in the top-level launcher for cleaner error handling ([#3874](https://github.com/NousResearch/hermes-agent/pull/3874))
|
| 118 |
+
- **Lazy-initialized tools show yellow** in banner instead of red, reducing false alarm about "missing" tools ([#3822](https://github.com/NousResearch/hermes-agent/pull/3822))
|
| 119 |
+
- **Honcho tools shown in banner** when configured ([#3810](https://github.com/NousResearch/hermes-agent/pull/3810))
|
| 120 |
+
|
| 121 |
+
### Setup & Configuration
|
| 122 |
+
- **Auto-install matrix-nio** during `hermes setup` when Matrix is selected ([#3802](https://github.com/NousResearch/hermes-agent/pull/3802), [#3873](https://github.com/NousResearch/hermes-agent/pull/3873))
|
| 123 |
+
- **Session export stdout support** — export sessions to stdout with `-` for piping ([#3641](https://github.com/NousResearch/hermes-agent/pull/3641), closes [#3609](https://github.com/NousResearch/hermes-agent/issues/3609))
|
| 124 |
+
- **Configurable approval timeouts** — set how long dangerous command approval prompts wait before auto-denying ([#3886](https://github.com/NousResearch/hermes-agent/pull/3886), closes [#3765](https://github.com/NousResearch/hermes-agent/issues/3765))
|
| 125 |
+
- **Clear __pycache__ during update** — prevents stale bytecode ImportError after `hermes update` ([#3819](https://github.com/NousResearch/hermes-agent/pull/3819))
|
| 126 |
+
|
| 127 |
+
---
|
| 128 |
+
|
| 129 |
+
## 🔧 Tool System
|
| 130 |
+
|
| 131 |
+
### MCP
|
| 132 |
+
- **MCP Server Mode** — `hermes mcp serve` exposes conversations, sessions, and attachments to MCP clients via stdio or Streamable HTTP ([#3795](https://github.com/NousResearch/hermes-agent/pull/3795))
|
| 133 |
+
- **Dynamic tool discovery** — respond to `notifications/tools/list_changed` events to pick up new tools from MCP servers without reconnecting ([#3812](https://github.com/NousResearch/hermes-agent/pull/3812))
|
| 134 |
+
- **Non-deprecated HTTP transport** — switched from `sse_client` to `streamable_http_client` ([#3646](https://github.com/NousResearch/hermes-agent/pull/3646))
|
| 135 |
+
|
| 136 |
+
### Web Tools
|
| 137 |
+
- **Exa search backend** — alternative to Firecrawl and DuckDuckGo for web search and extraction ([#3648](https://github.com/NousResearch/hermes-agent/pull/3648))
|
| 138 |
+
|
| 139 |
+
### Browser
|
| 140 |
+
- **Guard against None LLM responses** in browser snapshot and vision tools ([#3642](https://github.com/NousResearch/hermes-agent/pull/3642))
|
| 141 |
+
|
| 142 |
+
### Terminal & Remote Backends
|
| 143 |
+
- **Mount skill directories** into Modal and Docker containers ([#3890](https://github.com/NousResearch/hermes-agent/pull/3890))
|
| 144 |
+
- **Mount credential files** into remote backends with mtime+size caching ([#3671](https://github.com/NousResearch/hermes-agent/pull/3671))
|
| 145 |
+
- **Preserve partial output** when commands time out instead of losing everything ([#3868](https://github.com/NousResearch/hermes-agent/pull/3868))
|
| 146 |
+
- **Stop marking persisted env vars as missing** on remote backends ([#3650](https://github.com/NousResearch/hermes-agent/pull/3650))
|
| 147 |
+
|
| 148 |
+
### Audio
|
| 149 |
+
- **.aac format support** in transcription tool ([#3865](https://github.com/NousResearch/hermes-agent/pull/3865), closes [#1963](https://github.com/NousResearch/hermes-agent/issues/1963))
|
| 150 |
+
- **Audio download retry** — retry logic for `cache_audio_from_url` matching the existing image download pattern ([#3401](https://github.com/NousResearch/hermes-agent/pull/3401)) — @binhnt92
|
| 151 |
+
|
| 152 |
+
### Vision
|
| 153 |
+
- **Reject non-image files** and enforce website-only policy for vision analysis ([#3845](https://github.com/NousResearch/hermes-agent/pull/3845))
|
| 154 |
+
|
| 155 |
+
### Tool Schema
|
| 156 |
+
- **Ensure name field** always present in tool definitions, fixing `KeyError: 'name'` crashes ([#3811](https://github.com/NousResearch/hermes-agent/pull/3811), closes [#3729](https://github.com/NousResearch/hermes-agent/issues/3729))
|
| 157 |
+
|
| 158 |
+
### ACP (Editor Integration)
|
| 159 |
+
- **Complete session management surface** for VS Code/Zed/JetBrains clients — proper task lifecycle, cancel support, session persistence ([#3675](https://github.com/NousResearch/hermes-agent/pull/3675))
|
| 160 |
+
|
| 161 |
+
---
|
| 162 |
+
|
| 163 |
+
## 🧩 Skills & Plugins
|
| 164 |
+
|
| 165 |
+
### Skills System
|
| 166 |
+
- **External skill directories** — configure additional skill directories via `skills.external_dirs` in config.yaml ([#3678](https://github.com/NousResearch/hermes-agent/pull/3678))
|
| 167 |
+
- **Category path traversal blocked** — prevents `../` attacks in skill category names ([#3844](https://github.com/NousResearch/hermes-agent/pull/3844))
|
| 168 |
+
- **parallel-cli moved to optional-skills** — reduces default skill footprint ([#3673](https://github.com/NousResearch/hermes-agent/pull/3673)) — @kshitijk4poor
|
| 169 |
+
|
| 170 |
+
### New Skills
|
| 171 |
+
- **memento-flashcards** — spaced repetition flashcard system ([#3827](https://github.com/NousResearch/hermes-agent/pull/3827))
|
| 172 |
+
- **songwriting-and-ai-music** — songwriting craft and AI music generation prompts ([#3834](https://github.com/NousResearch/hermes-agent/pull/3834))
|
| 173 |
+
- **SiYuan Note** — integration with SiYuan note-taking app ([#3742](https://github.com/NousResearch/hermes-agent/pull/3742))
|
| 174 |
+
- **Scrapling** — web scraping skill using Scrapling library ([#3742](https://github.com/NousResearch/hermes-agent/pull/3742))
|
| 175 |
+
- **one-three-one-rule** — communication framework skill ([#3797](https://github.com/NousResearch/hermes-agent/pull/3797))
|
| 176 |
+
|
| 177 |
+
### Plugin System
|
| 178 |
+
- **Plugin enable/disable commands** — `hermes plugins enable/disable <name>` for managing plugin state without removing them ([#3747](https://github.com/NousResearch/hermes-agent/pull/3747))
|
| 179 |
+
- **Plugin message injection** — plugins can now inject messages into the conversation stream on behalf of the user via `ctx.inject_message()` ([#3778](https://github.com/NousResearch/hermes-agent/pull/3778)) — @winglian
|
| 180 |
+
- **Honcho self-hosted support** — allow local Honcho instances without requiring an API key ([#3644](https://github.com/NousResearch/hermes-agent/pull/3644))
|
| 181 |
+
|
| 182 |
+
---
|
| 183 |
+
|
| 184 |
+
## 🔒 Security & Reliability
|
| 185 |
+
|
| 186 |
+
### Security Hardening
|
| 187 |
+
- **Hardened dangerous command detection** — expanded pattern matching for risky shell commands and added file tool path guards for sensitive locations (`/etc/`, `/boot/`, docker.sock) ([#3872](https://github.com/NousResearch/hermes-agent/pull/3872))
|
| 188 |
+
- **Sensitive path write checks** in approval system — catch writes to system config files through file tools, not just terminal ([#3859](https://github.com/NousResearch/hermes-agent/pull/3859))
|
| 189 |
+
- **Secret redaction expansion** — now covers ElevenLabs, Tavily, and Exa API keys ([#3920](https://github.com/NousResearch/hermes-agent/pull/3920))
|
| 190 |
+
- **Vision file rejection** — reject non-image files passed to vision analysis to prevent information disclosure ([#3845](https://github.com/NousResearch/hermes-agent/pull/3845))
|
| 191 |
+
- **Category path traversal blocking** — prevent directory traversal in skill category names ([#3844](https://github.com/NousResearch/hermes-agent/pull/3844))
|
| 192 |
+
|
| 193 |
+
### Reliability
|
| 194 |
+
- **Atomic config.yaml writes** — prevent data loss during gateway crashes ([#3800](https://github.com/NousResearch/hermes-agent/pull/3800))
|
| 195 |
+
- **Clear __pycache__ on update** — prevent stale bytecode from causing ImportError after updates ([#3819](https://github.com/NousResearch/hermes-agent/pull/3819))
|
| 196 |
+
- **Lazy imports for update safety** — prevent ImportError chains during `hermes update` when modules reference new functions ([#3776](https://github.com/NousResearch/hermes-agent/pull/3776))
|
| 197 |
+
- **Restore terminalbench2 from patch corruption** — recovered file damaged by patch tool's secret redaction ([#3801](https://github.com/NousResearch/hermes-agent/pull/3801))
|
| 198 |
+
- **Terminal timeout preserves partial output** — no more lost command output on timeout ([#3868](https://github.com/NousResearch/hermes-agent/pull/3868))
|
| 199 |
+
|
| 200 |
+
---
|
| 201 |
+
|
| 202 |
+
## 🐛 Notable Bug Fixes
|
| 203 |
+
|
| 204 |
+
- **OpenClaw migration model config overwrite** — migration no longer overwrites model config dict with a string ([#3924](https://github.com/NousResearch/hermes-agent/pull/3924)) — @0xbyt4
|
| 205 |
+
- **OpenClaw migration expanded** — covers full data footprint including sessions, cron, memory ([#3869](https://github.com/NousResearch/hermes-agent/pull/3869))
|
| 206 |
+
- **Telegram deleted reply targets** — gracefully handle replies to deleted messages instead of crashing ([#3858](https://github.com/NousResearch/hermes-agent/pull/3858))
|
| 207 |
+
- **Discord "thinking..." persistence** — properly cleans up deferred response indicators ([#3674](https://github.com/NousResearch/hermes-agent/pull/3674))
|
| 208 |
+
- **WhatsApp LID↔phone aliases** — fixes allowlist matching failures with Linked ID format ([#3830](https://github.com/NousResearch/hermes-agent/pull/3830))
|
| 209 |
+
- **Signal URL-encoded phone numbers** — fixes delivery failures with certain formats ([#3670](https://github.com/NousResearch/hermes-agent/pull/3670))
|
| 210 |
+
- **Email connection leaks** — properly close SMTP/IMAP connections on error ([#3804](https://github.com/NousResearch/hermes-agent/pull/3804))
|
| 211 |
+
- **_safe_print ValueError** — no more gateway thread crashes on closed stdout ([#3843](https://github.com/NousResearch/hermes-agent/pull/3843))
|
| 212 |
+
- **Tool schema KeyError 'name'** — ensure name field always present in tool definitions ([#3811](https://github.com/NousResearch/hermes-agent/pull/3811))
|
| 213 |
+
- **api_mode stale on provider switch** — correctly clear when switching providers via `hermes model` ([#3857](https://github.com/NousResearch/hermes-agent/pull/3857))
|
| 214 |
+
|
| 215 |
+
---
|
| 216 |
+
|
| 217 |
+
## 🧪 Testing
|
| 218 |
+
|
| 219 |
+
- Resolved 10+ CI failures across hooks, tiktoken, plugins, and skill tests ([#3848](https://github.com/NousResearch/hermes-agent/pull/3848), [#3721](https://github.com/NousResearch/hermes-agent/pull/3721), [#3936](https://github.com/NousResearch/hermes-agent/pull/3936))
|
| 220 |
+
|
| 221 |
+
---
|
| 222 |
+
|
| 223 |
+
## 📚 Documentation
|
| 224 |
+
|
| 225 |
+
- **Comprehensive OpenClaw migration guide** — step-by-step guide for migrating from OpenClaw/Claw3D to Hermes Agent ([#3864](https://github.com/NousResearch/hermes-agent/pull/3864), [#3900](https://github.com/NousResearch/hermes-agent/pull/3900))
|
| 226 |
+
- **Credential file passthrough docs** — document how to forward credential files and env vars to remote backends ([#3677](https://github.com/NousResearch/hermes-agent/pull/3677))
|
| 227 |
+
- **DuckDuckGo requirements clarified** — note runtime dependency on duckduckgo-search package ([#3680](https://github.com/NousResearch/hermes-agent/pull/3680))
|
| 228 |
+
- **Skills catalog updated** — added red-teaming category and optional skills listing ([#3745](https://github.com/NousResearch/hermes-agent/pull/3745))
|
| 229 |
+
- **Feishu docs MDX fix** — escape angle-bracket URLs that break Docusaurus build ([#3902](https://github.com/NousResearch/hermes-agent/pull/3902))
|
| 230 |
+
|
| 231 |
+
---
|
| 232 |
+
|
| 233 |
+
## 👥 Contributors
|
| 234 |
+
|
| 235 |
+
### Core
|
| 236 |
+
- **@teknium1** — 90 PRs across all subsystems
|
| 237 |
+
|
| 238 |
+
### Community Contributors
|
| 239 |
+
- **@kshitijk4poor** — 3 PRs: Signal phone number fix ([#3670](https://github.com/NousResearch/hermes-agent/pull/3670)), parallel-cli to optional-skills ([#3673](https://github.com/NousResearch/hermes-agent/pull/3673)), status bar wrapping fix ([#3883](https://github.com/NousResearch/hermes-agent/pull/3883))
|
| 240 |
+
- **@winglian** — 1 PR: Plugin message injection interface ([#3778](https://github.com/NousResearch/hermes-agent/pull/3778))
|
| 241 |
+
- **@binhnt92** — 1 PR: Audio download retry logic ([#3401](https://github.com/NousResearch/hermes-agent/pull/3401))
|
| 242 |
+
- **@0xbyt4** — 1 PR: OpenClaw migration model config fix ([#3924](https://github.com/NousResearch/hermes-agent/pull/3924))
|
| 243 |
+
|
| 244 |
+
### Issues Resolved from Community
|
| 245 |
+
@Material-Scientist ([#850](https://github.com/NousResearch/hermes-agent/issues/850)), @hanxu98121 ([#1734](https://github.com/NousResearch/hermes-agent/issues/1734)), @penwyp ([#1788](https://github.com/NousResearch/hermes-agent/issues/1788)), @dan-and ([#1945](https://github.com/NousResearch/hermes-agent/issues/1945)), @AdrianScott ([#1963](https://github.com/NousResearch/hermes-agent/issues/1963)), @clawdbot47 ([#3229](https://github.com/NousResearch/hermes-agent/issues/3229)), @alanfwilliams ([#3404](https://github.com/NousResearch/hermes-agent/issues/3404)), @kentimsit ([#3433](https://github.com/NousResearch/hermes-agent/issues/3433)), @hayka-pacha ([#3534](https://github.com/NousResearch/hermes-agent/issues/3534)), @primmer ([#3595](https://github.com/NousResearch/hermes-agent/issues/3595)), @dagelf ([#3609](https://github.com/NousResearch/hermes-agent/issues/3609)), @HenkDz ([#3685](https://github.com/NousResearch/hermes-agent/issues/3685)), @tmdgusya ([#3729](https://github.com/NousResearch/hermes-agent/issues/3729)), @TypQxQ ([#3753](https://github.com/NousResearch/hermes-agent/issues/3753)), @acsezen ([#3765](https://github.com/NousResearch/hermes-agent/issues/3765))
|
| 246 |
+
|
| 247 |
+
---
|
| 248 |
+
|
| 249 |
+
**Full Changelog**: [v2026.3.28...v2026.3.30](https://github.com/NousResearch/hermes-agent/compare/v2026.3.28...v2026.3.30)
|
RELEASE_v0.7.0.md
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hermes Agent v0.7.0 (v2026.4.3)
|
| 2 |
+
|
| 3 |
+
**Release Date:** April 3, 2026
|
| 4 |
+
|
| 5 |
+
> The resilience release — pluggable memory providers, credential pool rotation, Camofox anti-detection browser, inline diff previews, gateway hardening across race conditions and approval routing, and deep security fixes across 168 PRs and 46 resolved issues.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## ✨ Highlights
|
| 10 |
+
|
| 11 |
+
- **Pluggable Memory Provider Interface** — Memory is now an extensible plugin system. Third-party memory backends (Honcho, vector stores, custom DBs) implement a simple provider ABC and register via the plugin system. Built-in memory is the default provider. Honcho integration restored to full parity as the reference plugin with profile-scoped host/peer resolution. ([#4623](https://github.com/NousResearch/hermes-agent/pull/4623), [#4616](https://github.com/NousResearch/hermes-agent/pull/4616), [#4355](https://github.com/NousResearch/hermes-agent/pull/4355))
|
| 12 |
+
|
| 13 |
+
- **Same-Provider Credential Pools** — Configure multiple API keys for the same provider with automatic rotation. Thread-safe `least_used` strategy distributes load across keys, and 401 failures trigger automatic rotation to the next credential. Set up via the setup wizard or `credential_pool` config. ([#4188](https://github.com/NousResearch/hermes-agent/pull/4188), [#4300](https://github.com/NousResearch/hermes-agent/pull/4300), [#4361](https://github.com/NousResearch/hermes-agent/pull/4361))
|
| 14 |
+
|
| 15 |
+
- **Camofox Anti-Detection Browser Backend** — New local browser backend using Camoufox for stealth browsing. Persistent sessions with VNC URL discovery for visual debugging, configurable SSRF bypass for local backends, auto-install via `hermes tools`. ([#4008](https://github.com/NousResearch/hermes-agent/pull/4008), [#4419](https://github.com/NousResearch/hermes-agent/pull/4419), [#4292](https://github.com/NousResearch/hermes-agent/pull/4292))
|
| 16 |
+
|
| 17 |
+
- **Inline Diff Previews** — File write and patch operations now show inline diffs in the tool activity feed, giving you visual confirmation of what changed before the agent moves on. ([#4411](https://github.com/NousResearch/hermes-agent/pull/4411), [#4423](https://github.com/NousResearch/hermes-agent/pull/4423))
|
| 18 |
+
|
| 19 |
+
- **API Server Session Continuity & Tool Streaming** — The API server (Open WebUI integration) now streams tool progress events in real-time and supports `X-Hermes-Session-Id` headers for persistent sessions across requests. Sessions persist to the shared SessionDB. ([#4092](https://github.com/NousResearch/hermes-agent/pull/4092), [#4478](https://github.com/NousResearch/hermes-agent/pull/4478), [#4802](https://github.com/NousResearch/hermes-agent/pull/4802))
|
| 20 |
+
|
| 21 |
+
- **ACP: Client-Provided MCP Servers** — Editor integrations (VS Code, Zed, JetBrains) can now register their own MCP servers, which Hermes picks up as additional agent tools. Your editor's MCP ecosystem flows directly into the agent. ([#4705](https://github.com/NousResearch/hermes-agent/pull/4705))
|
| 22 |
+
|
| 23 |
+
- **Gateway Hardening** — Major stability pass across race conditions, photo media delivery, flood control, stuck sessions, approval routing, and compression death spirals. The gateway is substantially more reliable in production. ([#4727](https://github.com/NousResearch/hermes-agent/pull/4727), [#4750](https://github.com/NousResearch/hermes-agent/pull/4750), [#4798](https://github.com/NousResearch/hermes-agent/pull/4798), [#4557](https://github.com/NousResearch/hermes-agent/pull/4557))
|
| 24 |
+
|
| 25 |
+
- **Security: Secret Exfiltration Blocking** — Browser URLs and LLM responses are now scanned for secret patterns, blocking exfiltration attempts via URL encoding, base64, or prompt injection. Credential directory protections expanded to `.docker`, `.azure`, `.config/gh`. Execute_code sandbox output is redacted. ([#4483](https://github.com/NousResearch/hermes-agent/pull/4483), [#4360](https://github.com/NousResearch/hermes-agent/pull/4360), [#4305](https://github.com/NousResearch/hermes-agent/pull/4305), [#4327](https://github.com/NousResearch/hermes-agent/pull/4327))
|
| 26 |
+
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
## 🏗️ Core Agent & Architecture
|
| 30 |
+
|
| 31 |
+
### Provider & Model Support
|
| 32 |
+
- **Same-provider credential pools** — configure multiple API keys with automatic `least_used` rotation and 401 failover ([#4188](https://github.com/NousResearch/hermes-agent/pull/4188), [#4300](https://github.com/NousResearch/hermes-agent/pull/4300))
|
| 33 |
+
- **Credential pool preserved through smart routing** — pool state survives fallback provider switches and defers eager fallback on 429 ([#4361](https://github.com/NousResearch/hermes-agent/pull/4361))
|
| 34 |
+
- **Per-turn primary runtime restoration** — after fallback provider use, the agent automatically restores the primary provider on the next turn with transport recovery ([#4624](https://github.com/NousResearch/hermes-agent/pull/4624))
|
| 35 |
+
- **`developer` role for GPT-5 and Codex models** — uses OpenAI's recommended system message role for newer models ([#4498](https://github.com/NousResearch/hermes-agent/pull/4498))
|
| 36 |
+
- **Google model operational guidance** — Gemini and Gemma models get provider-specific prompting guidance ([#4641](https://github.com/NousResearch/hermes-agent/pull/4641))
|
| 37 |
+
- **Anthropic long-context tier 429 handling** — automatically reduces context to 200k when hitting tier limits ([#4747](https://github.com/NousResearch/hermes-agent/pull/4747))
|
| 38 |
+
- **URL-based auth for third-party Anthropic endpoints** + CI test fixes ([#4148](https://github.com/NousResearch/hermes-agent/pull/4148))
|
| 39 |
+
- **Bearer auth for MiniMax Anthropic endpoints** ([#4028](https://github.com/NousResearch/hermes-agent/pull/4028))
|
| 40 |
+
- **Fireworks context length detection** ([#4158](https://github.com/NousResearch/hermes-agent/pull/4158))
|
| 41 |
+
- **Standard DashScope international endpoint** for Alibaba provider ([#4133](https://github.com/NousResearch/hermes-agent/pull/4133), closes [#3912](https://github.com/NousResearch/hermes-agent/issues/3912))
|
| 42 |
+
- **Custom providers context_length** honored in hygiene compression ([#4085](https://github.com/NousResearch/hermes-agent/pull/4085))
|
| 43 |
+
- **Non-sk-ant keys** treated as regular API keys, not OAuth tokens ([#4093](https://github.com/NousResearch/hermes-agent/pull/4093))
|
| 44 |
+
- **Claude-sonnet-4.6** added to OpenRouter and Nous model lists ([#4157](https://github.com/NousResearch/hermes-agent/pull/4157))
|
| 45 |
+
- **Qwen 3.6 Plus Preview** added to model lists ([#4376](https://github.com/NousResearch/hermes-agent/pull/4376))
|
| 46 |
+
- **MiniMax M2.7** added to hermes model picker and OpenCode ([#4208](https://github.com/NousResearch/hermes-agent/pull/4208))
|
| 47 |
+
- **Auto-detect models from server probe** in custom endpoint setup ([#4218](https://github.com/NousResearch/hermes-agent/pull/4218))
|
| 48 |
+
- **Config.yaml single source of truth** for endpoint URLs — no more env var vs config.yaml conflicts ([#4165](https://github.com/NousResearch/hermes-agent/pull/4165))
|
| 49 |
+
- **Setup wizard no longer overwrites** custom endpoint config ([#4180](https://github.com/NousResearch/hermes-agent/pull/4180), closes [#4172](https://github.com/NousResearch/hermes-agent/issues/4172))
|
| 50 |
+
- **Unified setup wizard provider selection** with `hermes model` — single code path for both flows ([#4200](https://github.com/NousResearch/hermes-agent/pull/4200))
|
| 51 |
+
- **Root-level provider config** no longer overrides `model.provider` ([#4329](https://github.com/NousResearch/hermes-agent/pull/4329))
|
| 52 |
+
- **Rate-limit pairing rejection messages** to prevent spam ([#4081](https://github.com/NousResearch/hermes-agent/pull/4081))
|
| 53 |
+
|
| 54 |
+
### Agent Loop & Conversation
|
| 55 |
+
- **Preserve Anthropic thinking block signatures** across tool-use turns ([#4626](https://github.com/NousResearch/hermes-agent/pull/4626))
|
| 56 |
+
- **Classify think-only empty responses** before retrying — prevents infinite retry loops on models that produce thinking blocks without content ([#4645](https://github.com/NousResearch/hermes-agent/pull/4645))
|
| 57 |
+
- **Prevent compression death spiral** from API disconnects — stops the loop where compression triggers, fails, compresses again ([#4750](https://github.com/NousResearch/hermes-agent/pull/4750), closes [#2153](https://github.com/NousResearch/hermes-agent/issues/2153))
|
| 58 |
+
- **Persist compressed context** to gateway session after mid-run compression ([#4095](https://github.com/NousResearch/hermes-agent/pull/4095))
|
| 59 |
+
- **Context-exceeded error messages** now include actionable guidance ([#4155](https://github.com/NousResearch/hermes-agent/pull/4155), closes [#4061](https://github.com/NousResearch/hermes-agent/issues/4061))
|
| 60 |
+
- **Strip orphaned think/reasoning tags** from user-facing responses ([#4311](https://github.com/NousResearch/hermes-agent/pull/4311), closes [#4285](https://github.com/NousResearch/hermes-agent/issues/4285))
|
| 61 |
+
- **Harden Codex responses preflight** and stream error handling ([#4313](https://github.com/NousResearch/hermes-agent/pull/4313))
|
| 62 |
+
- **Deterministic call_id fallbacks** instead of random UUIDs for prompt cache consistency ([#3991](https://github.com/NousResearch/hermes-agent/pull/3991))
|
| 63 |
+
- **Context pressure warning spam** prevented after compression ([#4012](https://github.com/NousResearch/hermes-agent/pull/4012))
|
| 64 |
+
- **AsyncOpenAI created lazily** in trajectory compressor to avoid closed event loop errors ([#4013](https://github.com/NousResearch/hermes-agent/pull/4013))
|
| 65 |
+
|
| 66 |
+
### Memory & Sessions
|
| 67 |
+
- **Pluggable memory provider interface** — ABC-based plugin system for custom memory backends with profile isolation ([#4623](https://github.com/NousResearch/hermes-agent/pull/4623))
|
| 68 |
+
- **Honcho full integration parity** restored as reference memory provider plugin ([#4355](https://github.com/NousResearch/hermes-agent/pull/4355)) — @erosika
|
| 69 |
+
- **Honcho profile-scoped** host and peer resolution ([#4616](https://github.com/NousResearch/hermes-agent/pull/4616))
|
| 70 |
+
- **Memory flush state persisted** to prevent redundant re-flushes on gateway restart ([#4481](https://github.com/NousResearch/hermes-agent/pull/4481))
|
| 71 |
+
- **Memory provider tools** routed through sequential execution path ([#4803](https://github.com/NousResearch/hermes-agent/pull/4803))
|
| 72 |
+
- **Honcho config** written to instance-local path for profile isolation ([#4037](https://github.com/NousResearch/hermes-agent/pull/4037))
|
| 73 |
+
- **API server sessions** persist to shared SessionDB ([#4802](https://github.com/NousResearch/hermes-agent/pull/4802))
|
| 74 |
+
- **Token usage persisted** for non-CLI sessions ([#4627](https://github.com/NousResearch/hermes-agent/pull/4627))
|
| 75 |
+
- **Quote dotted terms in FTS5 queries** — fixes session search for terms containing dots ([#4549](https://github.com/NousResearch/hermes-agent/pull/4549))
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
|
| 79 |
+
## 📱 Messaging Platforms (Gateway)
|
| 80 |
+
|
| 81 |
+
### Gateway Core
|
| 82 |
+
- **Race condition fixes** — photo media loss, flood control, stuck sessions, and STT config issues resolved in one hardening pass ([#4727](https://github.com/NousResearch/hermes-agent/pull/4727))
|
| 83 |
+
- **Approval routing through running-agent guard** — `/approve` and `/deny` now route correctly when the agent is blocked waiting for approval instead of being swallowed as interrupts ([#4798](https://github.com/NousResearch/hermes-agent/pull/4798), [#4557](https://github.com/NousResearch/hermes-agent/pull/4557), closes [#4542](https://github.com/NousResearch/hermes-agent/issues/4542))
|
| 84 |
+
- **Resume agent after /approve** — tool result is no longer lost when executing blocked commands ([#4418](https://github.com/NousResearch/hermes-agent/pull/4418))
|
| 85 |
+
- **DM thread sessions seeded** with parent transcript to preserve context ([#4559](https://github.com/NousResearch/hermes-agent/pull/4559))
|
| 86 |
+
- **Skill-aware slash commands** — gateway dynamically registers installed skills as slash commands with paginated `/commands` list and Telegram 100-command cap ([#3934](https://github.com/NousResearch/hermes-agent/pull/3934), [#4005](https://github.com/NousResearch/hermes-agent/pull/4005), [#4006](https://github.com/NousResearch/hermes-agent/pull/4006), [#4010](https://github.com/NousResearch/hermes-agent/pull/4010), [#4023](https://github.com/NousResearch/hermes-agent/pull/4023))
|
| 87 |
+
- **Per-platform disabled skills** respected in Telegram menu and gateway dispatch ([#4799](https://github.com/NousResearch/hermes-agent/pull/4799))
|
| 88 |
+
- **Remove user-facing compression warnings** — cleaner message flow ([#4139](https://github.com/NousResearch/hermes-agent/pull/4139))
|
| 89 |
+
- **`-v/-q` flags wired to stderr logging** for gateway service ([#4474](https://github.com/NousResearch/hermes-agent/pull/4474))
|
| 90 |
+
- **HERMES_HOME remapped** to target user in system service unit ([#4456](https://github.com/NousResearch/hermes-agent/pull/4456))
|
| 91 |
+
- **Honor default for invalid bool-like config values** ([#4029](https://github.com/NousResearch/hermes-agent/pull/4029))
|
| 92 |
+
- **setsid instead of systemd-run** for `/update` command to avoid systemd permission issues ([#4104](https://github.com/NousResearch/hermes-agent/pull/4104), closes [#4017](https://github.com/NousResearch/hermes-agent/issues/4017))
|
| 93 |
+
- **'Initializing agent...'** shown on first message for better UX ([#4086](https://github.com/NousResearch/hermes-agent/pull/4086))
|
| 94 |
+
- **Allow running gateway service as root** for LXC/container environments ([#4732](https://github.com/NousResearch/hermes-agent/pull/4732))
|
| 95 |
+
|
| 96 |
+
### Telegram
|
| 97 |
+
- **32-char limit on command names** with collision avoidance ([#4211](https://github.com/NousResearch/hermes-agent/pull/4211))
|
| 98 |
+
- **Priority order enforced** in menu — core > plugins > skills ([#4023](https://github.com/NousResearch/hermes-agent/pull/4023))
|
| 99 |
+
- **Capped at 50 commands** — API rejects above ~60 ([#4006](https://github.com/NousResearch/hermes-agent/pull/4006))
|
| 100 |
+
- **Skip empty/whitespace text** to prevent 400 errors ([#4388](https://github.com/NousResearch/hermes-agent/pull/4388))
|
| 101 |
+
- **E2E gateway tests** added ([#4497](https://github.com/NousResearch/hermes-agent/pull/4497)) — @pefontana
|
| 102 |
+
|
| 103 |
+
### Discord
|
| 104 |
+
- **Button-based approval UI** — register `/approve` and `/deny` slash commands with interactive button prompts ([#4800](https://github.com/NousResearch/hermes-agent/pull/4800))
|
| 105 |
+
- **Configurable reactions** — `discord.reactions` config option to disable message processing reactions ([#4199](https://github.com/NousResearch/hermes-agent/pull/4199))
|
| 106 |
+
- **Skip reactions and auto-threading** for unauthorized users ([#4387](https://github.com/NousResearch/hermes-agent/pull/4387))
|
| 107 |
+
|
| 108 |
+
### Slack
|
| 109 |
+
- **Reply in thread** — `slack.reply_in_thread` config option for threaded responses ([#4643](https://github.com/NousResearch/hermes-agent/pull/4643), closes [#2662](https://github.com/NousResearch/hermes-agent/issues/2662))
|
| 110 |
+
|
| 111 |
+
### WhatsApp
|
| 112 |
+
- **Enforce require_mention in group chats** ([#4730](https://github.com/NousResearch/hermes-agent/pull/4730))
|
| 113 |
+
|
| 114 |
+
### Webhook
|
| 115 |
+
- **Platform support fixes** — skip home channel prompt, disable tool progress for webhook adapters ([#4660](https://github.com/NousResearch/hermes-agent/pull/4660))
|
| 116 |
+
|
| 117 |
+
### Matrix
|
| 118 |
+
- **E2EE decryption hardening** — request missing keys, auto-trust devices, retry buffered events ([#4083](https://github.com/NousResearch/hermes-agent/pull/4083))
|
| 119 |
+
|
| 120 |
+
---
|
| 121 |
+
|
| 122 |
+
## 🖥️ CLI & User Experience
|
| 123 |
+
|
| 124 |
+
### New Slash Commands
|
| 125 |
+
- **`/yolo`** — toggle dangerous command approvals on/off for the session ([#3990](https://github.com/NousResearch/hermes-agent/pull/3990))
|
| 126 |
+
- **`/btw`** — ephemeral side questions that don't affect the main conversation context ([#4161](https://github.com/NousResearch/hermes-agent/pull/4161))
|
| 127 |
+
- **`/profile`** — show active profile info without leaving the chat session ([#4027](https://github.com/NousResearch/hermes-agent/pull/4027))
|
| 128 |
+
|
| 129 |
+
### Interactive CLI
|
| 130 |
+
- **Inline diff previews** for write and patch operations in the tool activity feed ([#4411](https://github.com/NousResearch/hermes-agent/pull/4411), [#4423](https://github.com/NousResearch/hermes-agent/pull/4423))
|
| 131 |
+
- **TUI pinned to bottom** on startup — no more large blank spaces between response and input ([#4412](https://github.com/NousResearch/hermes-agent/pull/4412), [#4359](https://github.com/NousResearch/hermes-agent/pull/4359), closes [#4398](https://github.com/NousResearch/hermes-agent/issues/4398), [#4421](https://github.com/NousResearch/hermes-agent/issues/4421))
|
| 132 |
+
- **`/history` and `/resume`** now surface recent sessions directly instead of requiring search ([#4728](https://github.com/NousResearch/hermes-agent/pull/4728))
|
| 133 |
+
- **Cache tokens shown** in `/insights` overview so total adds up ([#4428](https://github.com/NousResearch/hermes-agent/pull/4428))
|
| 134 |
+
- **`--max-turns` CLI flag** for `hermes chat` to limit agent iterations ([#4314](https://github.com/NousResearch/hermes-agent/pull/4314))
|
| 135 |
+
- **Detect dragged file paths** instead of treating them as slash commands ([#4533](https://github.com/NousResearch/hermes-agent/pull/4533)) — @rolme
|
| 136 |
+
- **Allow empty strings and falsy values** in `config set` ([#4310](https://github.com/NousResearch/hermes-agent/pull/4310), closes [#4277](https://github.com/NousResearch/hermes-agent/issues/4277))
|
| 137 |
+
- **Voice mode in WSL** when PulseAudio bridge is configured ([#4317](https://github.com/NousResearch/hermes-agent/pull/4317))
|
| 138 |
+
- **Respect `NO_COLOR` env var** and `TERM=dumb` for accessibility ([#4079](https://github.com/NousResearch/hermes-agent/pull/4079), closes [#4066](https://github.com/NousResearch/hermes-agent/issues/4066)) — @SHL0MS
|
| 139 |
+
- **Correct shell reload instruction** for macOS/zsh users ([#4025](https://github.com/NousResearch/hermes-agent/pull/4025))
|
| 140 |
+
- **Zero exit code** on successful quiet mode queries ([#4613](https://github.com/NousResearch/hermes-agent/pull/4613), closes [#4601](https://github.com/NousResearch/hermes-agent/issues/4601)) — @devorun
|
| 141 |
+
- **on_session_end hook fires** on interrupted exits ([#4159](https://github.com/NousResearch/hermes-agent/pull/4159))
|
| 142 |
+
- **Profile list display** reads `model.default` key correctly ([#4160](https://github.com/NousResearch/hermes-agent/pull/4160))
|
| 143 |
+
- **Browser and TTS** shown in reconfigure menu ([#4041](https://github.com/NousResearch/hermes-agent/pull/4041))
|
| 144 |
+
- **Web backend priority** detection simplified ([#4036](https://github.com/NousResearch/hermes-agent/pull/4036))
|
| 145 |
+
|
| 146 |
+
### Setup & Configuration
|
| 147 |
+
- **Allowed_users preserved** during setup and quiet unconfigured provider warnings ([#4551](https://github.com/NousResearch/hermes-agent/pull/4551)) — @kshitijk4poor
|
| 148 |
+
- **Save API key to model config** for custom endpoints ([#4202](https://github.com/NousResearch/hermes-agent/pull/4202), closes [#4182](https://github.com/NousResearch/hermes-agent/issues/4182))
|
| 149 |
+
- **Claude Code credentials gated** behind explicit Hermes config in wizard trigger ([#4210](https://github.com/NousResearch/hermes-agent/pull/4210))
|
| 150 |
+
- **Atomic writes in save_config_value** to prevent config loss on interrupt ([#4298](https://github.com/NousResearch/hermes-agent/pull/4298), [#4320](https://github.com/NousResearch/hermes-agent/pull/4320))
|
| 151 |
+
- **Scopes field written** to Claude Code credentials on token refresh ([#4126](https://github.com/NousResearch/hermes-agent/pull/4126))
|
| 152 |
+
|
| 153 |
+
### Update System
|
| 154 |
+
- **Fork detection and upstream sync** in `hermes update` ([#4744](https://github.com/NousResearch/hermes-agent/pull/4744))
|
| 155 |
+
- **Preserve working optional extras** when one extra fails during update ([#4550](https://github.com/NousResearch/hermes-agent/pull/4550))
|
| 156 |
+
- **Handle conflicted git index** during hermes update ([#4735](https://github.com/NousResearch/hermes-agent/pull/4735))
|
| 157 |
+
- **Avoid launchd restart race** on macOS ([#4736](https://github.com/NousResearch/hermes-agent/pull/4736))
|
| 158 |
+
- **Missing subprocess.run() timeouts** added to doctor and status commands ([#4009](https://github.com/NousResearch/hermes-agent/pull/4009))
|
| 159 |
+
|
| 160 |
+
---
|
| 161 |
+
|
| 162 |
+
## 🔧 Tool System
|
| 163 |
+
|
| 164 |
+
### Browser
|
| 165 |
+
- **Camofox anti-detection browser backend** — local stealth browsing with auto-install via `hermes tools` ([#4008](https://github.com/NousResearch/hermes-agent/pull/4008))
|
| 166 |
+
- **Persistent Camofox sessions** with VNC URL discovery for visual debugging ([#4419](https://github.com/NousResearch/hermes-agent/pull/4419))
|
| 167 |
+
- **Skip SSRF check for local backends** (Camofox, headless Chromium) ([#4292](https://github.com/NousResearch/hermes-agent/pull/4292))
|
| 168 |
+
- **Configurable SSRF check** via `browser.allow_private_urls` ([#4198](https://github.com/NousResearch/hermes-agent/pull/4198)) — @nils010485
|
| 169 |
+
- **CAMOFOX_PORT=9377** added to Docker commands ([#4340](https://github.com/NousResearch/hermes-agent/pull/4340))
|
| 170 |
+
|
| 171 |
+
### File Operations
|
| 172 |
+
- **Inline diff previews** on write and patch actions ([#4411](https://github.com/NousResearch/hermes-agent/pull/4411), [#4423](https://github.com/NousResearch/hermes-agent/pull/4423))
|
| 173 |
+
- **Stale file detection** on write and patch — warns when file was modified externally since last read ([#4345](https://github.com/NousResearch/hermes-agent/pull/4345))
|
| 174 |
+
- **Staleness timestamp refreshed** after writes ([#4390](https://github.com/NousResearch/hermes-agent/pull/4390))
|
| 175 |
+
- **Size guard, dedup, and device blocking** on read_file ([#4315](https://github.com/NousResearch/hermes-agent/pull/4315))
|
| 176 |
+
|
| 177 |
+
### MCP
|
| 178 |
+
- **Stability fix pack** — reload timeout, shutdown cleanup, event loop handler, OAuth non-blocking ([#4757](https://github.com/NousResearch/hermes-agent/pull/4757), closes [#4462](https://github.com/NousResearch/hermes-agent/issues/4462), [#2537](https://github.com/NousResearch/hermes-agent/issues/2537))
|
| 179 |
+
|
| 180 |
+
### ACP (Editor Integration)
|
| 181 |
+
- **Client-provided MCP servers** registered as agent tools — editors pass their MCP servers to Hermes ([#4705](https://github.com/NousResearch/hermes-agent/pull/4705))
|
| 182 |
+
|
| 183 |
+
### Skills System
|
| 184 |
+
- **Size limits for agent writes** and **fuzzy matching for skill patch** — prevents oversized skill writes and improves edit reliability ([#4414](https://github.com/NousResearch/hermes-agent/pull/4414))
|
| 185 |
+
- **Validate hub bundle paths** before install — blocks path traversal in skill bundles ([#3986](https://github.com/NousResearch/hermes-agent/pull/3986))
|
| 186 |
+
- **Unified hermes-agent and hermes-agent-setup** into single skill ([#4332](https://github.com/NousResearch/hermes-agent/pull/4332))
|
| 187 |
+
- **Skill metadata type check** in extract_skill_conditions ([#4479](https://github.com/NousResearch/hermes-agent/pull/4479))
|
| 188 |
+
|
| 189 |
+
### New/Updated Skills
|
| 190 |
+
- **research-paper-writing** — full end-to-end research pipeline (replaced ml-paper-writing) ([#4654](https://github.com/NousResearch/hermes-agent/pull/4654)) — @SHL0MS
|
| 191 |
+
- **ascii-video** — text readability techniques and external layout oracle ([#4054](https://github.com/NousResearch/hermes-agent/pull/4054)) — @SHL0MS
|
| 192 |
+
- **youtube-transcript** updated for youtube-transcript-api v1.x ([#4455](https://github.com/NousResearch/hermes-agent/pull/4455)) — @el-analista
|
| 193 |
+
- **Skills browse and search page** added to documentation site ([#4500](https://github.com/NousResearch/hermes-agent/pull/4500)) — @IAvecilla
|
| 194 |
+
|
| 195 |
+
---
|
| 196 |
+
|
| 197 |
+
## 🔒 Security & Reliability
|
| 198 |
+
|
| 199 |
+
### Security Hardening
|
| 200 |
+
- **Block secret exfiltration** via browser URLs and LLM responses — scans for secret patterns in URL encoding, base64, and prompt injection vectors ([#4483](https://github.com/NousResearch/hermes-agent/pull/4483))
|
| 201 |
+
- **Redact secrets from execute_code sandbox output** ([#4360](https://github.com/NousResearch/hermes-agent/pull/4360))
|
| 202 |
+
- **Protect `.docker`, `.azure`, `.config/gh` credential directories** from read/write via file tools and terminal ([#4305](https://github.com/NousResearch/hermes-agent/pull/4305), [#4327](https://github.com/NousResearch/hermes-agent/pull/4327)) — @memosr
|
| 203 |
+
- **GitHub OAuth token patterns** added to redaction + snapshot redact flag ([#4295](https://github.com/NousResearch/hermes-agent/pull/4295))
|
| 204 |
+
- **Reject private and loopback IPs** in Telegram DoH fallback ([#4129](https://github.com/NousResearch/hermes-agent/pull/4129))
|
| 205 |
+
- **Reject path traversal** in credential file registration ([#4316](https://github.com/NousResearch/hermes-agent/pull/4316))
|
| 206 |
+
- **Validate tar archive member paths** on profile import — blocks zip-slip attacks ([#4318](https://github.com/NousResearch/hermes-agent/pull/4318))
|
| 207 |
+
- **Exclude auth.json and .env** from profile exports ([#4475](https://github.com/NousResearch/hermes-agent/pull/4475))
|
| 208 |
+
|
| 209 |
+
### Reliability
|
| 210 |
+
- **Prevent compression death spiral** from API disconnects ([#4750](https://github.com/NousResearch/hermes-agent/pull/4750), closes [#2153](https://github.com/NousResearch/hermes-agent/issues/2153))
|
| 211 |
+
- **Handle `is_closed` as method** in OpenAI SDK — prevents false positive client closure detection ([#4416](https://github.com/NousResearch/hermes-agent/pull/4416), closes [#4377](https://github.com/NousResearch/hermes-agent/issues/4377))
|
| 212 |
+
- **Exclude matrix from [all] extras** — python-olm is upstream-broken, prevents install failures ([#4615](https://github.com/NousResearch/hermes-agent/pull/4615), closes [#4178](https://github.com/NousResearch/hermes-agent/issues/4178))
|
| 213 |
+
- **OpenCode model routing** repaired ([#4508](https://github.com/NousResearch/hermes-agent/pull/4508))
|
| 214 |
+
- **Docker container image** optimized ([#4034](https://github.com/NousResearch/hermes-agent/pull/4034)) — @bcross
|
| 215 |
+
|
| 216 |
+
### Windows & Cross-Platform
|
| 217 |
+
- **Voice mode in WSL** with PulseAudio bridge ([#4317](https://github.com/NousResearch/hermes-agent/pull/4317))
|
| 218 |
+
- **Homebrew packaging** preparation ([#4099](https://github.com/NousResearch/hermes-agent/pull/4099))
|
| 219 |
+
- **CI fork conditionals** to prevent workflow failures on forks ([#4107](https://github.com/NousResearch/hermes-agent/pull/4107))
|
| 220 |
+
|
| 221 |
+
---
|
| 222 |
+
|
| 223 |
+
## 🐛 Notable Bug Fixes
|
| 224 |
+
|
| 225 |
+
- **Gateway approval blocked agent thread** — approval now blocks the agent thread like CLI does, preventing tool result loss ([#4557](https://github.com/NousResearch/hermes-agent/pull/4557), closes [#4542](https://github.com/NousResearch/hermes-agent/issues/4542))
|
| 226 |
+
- **Compression death spiral** from API disconnects — detected and halted instead of looping ([#4750](https://github.com/NousResearch/hermes-agent/pull/4750), closes [#2153](https://github.com/NousResearch/hermes-agent/issues/2153))
|
| 227 |
+
- **Anthropic thinking blocks lost** across tool-use turns ([#4626](https://github.com/NousResearch/hermes-agent/pull/4626))
|
| 228 |
+
- **Profile model config ignored** with `-p` flag — model.model now promoted to model.default correctly ([#4160](https://github.com/NousResearch/hermes-agent/pull/4160), closes [#4486](https://github.com/NousResearch/hermes-agent/issues/4486))
|
| 229 |
+
- **CLI blank space** between response and input area ([#4412](https://github.com/NousResearch/hermes-agent/pull/4412), [#4359](https://github.com/NousResearch/hermes-agent/pull/4359), closes [#4398](https://github.com/NousResearch/hermes-agent/issues/4398))
|
| 230 |
+
- **Dragged file paths** treated as slash commands instead of file references ([#4533](https://github.com/NousResearch/hermes-agent/pull/4533)) — @rolme
|
| 231 |
+
- **Orphaned `</think>` tags** leaking into user-facing responses ([#4311](https://github.com/NousResearch/hermes-agent/pull/4311), closes [#4285](https://github.com/NousResearch/hermes-agent/issues/4285))
|
| 232 |
+
- **OpenAI SDK `is_closed`** is a method not property — false positive client closure ([#4416](https://github.com/NousResearch/hermes-agent/pull/4416), closes [#4377](https://github.com/NousResearch/hermes-agent/issues/4377))
|
| 233 |
+
- **MCP OAuth server** could block Hermes startup instead of degrading gracefully ([#4757](https://github.com/NousResearch/hermes-agent/pull/4757), closes [#4462](https://github.com/NousResearch/hermes-agent/issues/4462))
|
| 234 |
+
- **MCP event loop closed** on shutdown with HTTP servers ([#4757](https://github.com/NousResearch/hermes-agent/pull/4757), closes [#2537](https://github.com/NousResearch/hermes-agent/issues/2537))
|
| 235 |
+
- **Alibaba provider** hardcoded to wrong endpoint ([#4133](https://github.com/NousResearch/hermes-agent/pull/4133), closes [#3912](https://github.com/NousResearch/hermes-agent/issues/3912))
|
| 236 |
+
- **Slack reply_in_thread** missing config option ([#4643](https://github.com/NousResearch/hermes-agent/pull/4643), closes [#2662](https://github.com/NousResearch/hermes-agent/issues/2662))
|
| 237 |
+
- **Quiet mode exit code** — successful `-q` queries no longer exit nonzero ([#4613](https://github.com/NousResearch/hermes-agent/pull/4613), closes [#4601](https://github.com/NousResearch/hermes-agent/issues/4601))
|
| 238 |
+
- **Mobile sidebar** shows only close button due to backdrop-filter issue in docs site ([#4207](https://github.com/NousResearch/hermes-agent/pull/4207)) — @xsmyile
|
| 239 |
+
- **Config restore reverted** by stale-branch squash merge — `_config_version` fixed ([#4440](https://github.com/NousResearch/hermes-agent/pull/4440))
|
| 240 |
+
|
| 241 |
+
---
|
| 242 |
+
|
| 243 |
+
## 🧪 Testing
|
| 244 |
+
|
| 245 |
+
- **Telegram gateway E2E tests** — full integration test suite for the Telegram adapter ([#4497](https://github.com/NousResearch/hermes-agent/pull/4497)) — @pefontana
|
| 246 |
+
- **11 real test failures fixed** plus sys.modules cascade poisoner resolved ([#4570](https://github.com/NousResearch/hermes-agent/pull/4570))
|
| 247 |
+
- **7 CI failures resolved** across hooks, plugins, and skill tests ([#3936](https://github.com/NousResearch/hermes-agent/pull/3936))
|
| 248 |
+
- **Codex 401 refresh tests** updated for CI compatibility ([#4166](https://github.com/NousResearch/hermes-agent/pull/4166))
|
| 249 |
+
- **Stale OPENAI_BASE_URL test** fixed ([#4217](https://github.com/NousResearch/hermes-agent/pull/4217))
|
| 250 |
+
|
| 251 |
+
---
|
| 252 |
+
|
| 253 |
+
## 📚 Documentation
|
| 254 |
+
|
| 255 |
+
- **Comprehensive documentation audit** — 9 HIGH and 20+ MEDIUM gaps fixed across 21 files ([#4087](https://github.com/NousResearch/hermes-agent/pull/4087))
|
| 256 |
+
- **Site navigation restructured** — features and platforms promoted to top-level ([#4116](https://github.com/NousResearch/hermes-agent/pull/4116))
|
| 257 |
+
- **Tool progress streaming** documented for API server and Open WebUI ([#4138](https://github.com/NousResearch/hermes-agent/pull/4138))
|
| 258 |
+
- **Telegram webhook mode** documentation ([#4089](https://github.com/NousResearch/hermes-agent/pull/4089))
|
| 259 |
+
- **Local LLM provider guides** — comprehensive setup guides with context length warnings ([#4294](https://github.com/NousResearch/hermes-agent/pull/4294))
|
| 260 |
+
- **WhatsApp allowlist behavior** clarified with `WHATSAPP_ALLOW_ALL_USERS` documentation ([#4293](https://github.com/NousResearch/hermes-agent/pull/4293))
|
| 261 |
+
- **Slack configuration options** — new config section in Slack docs ([#4644](https://github.com/NousResearch/hermes-agent/pull/4644))
|
| 262 |
+
- **Terminal backends section** expanded + docs build fixes ([#4016](https://github.com/NousResearch/hermes-agent/pull/4016))
|
| 263 |
+
- **Adding-providers guide** updated for unified setup flow ([#4201](https://github.com/NousResearch/hermes-agent/pull/4201))
|
| 264 |
+
- **ACP Zed config** fixed ([#4743](https://github.com/NousResearch/hermes-agent/pull/4743))
|
| 265 |
+
- **Community FAQ** entries for common workflows and troubleshooting ([#4797](https://github.com/NousResearch/hermes-agent/pull/4797))
|
| 266 |
+
- **Skills browse and search page** on docs site ([#4500](https://github.com/NousResearch/hermes-agent/pull/4500)) — @IAvecilla
|
| 267 |
+
|
| 268 |
+
---
|
| 269 |
+
|
| 270 |
+
## 👥 Contributors
|
| 271 |
+
|
| 272 |
+
### Core
|
| 273 |
+
- **@teknium1** — 135 commits across all subsystems
|
| 274 |
+
|
| 275 |
+
### Top Community Contributors
|
| 276 |
+
- **@kshitijk4poor** — 13 commits: preserve allowed_users during setup ([#4551](https://github.com/NousResearch/hermes-agent/pull/4551)), and various fixes
|
| 277 |
+
- **@erosika** — 12 commits: Honcho full integration parity restored as memory provider plugin ([#4355](https://github.com/NousResearch/hermes-agent/pull/4355))
|
| 278 |
+
- **@pefontana** — 9 commits: Telegram gateway E2E test suite ([#4497](https://github.com/NousResearch/hermes-agent/pull/4497))
|
| 279 |
+
- **@bcross** — 5 commits: Docker container image optimization ([#4034](https://github.com/NousResearch/hermes-agent/pull/4034))
|
| 280 |
+
- **@SHL0MS** — 4 commits: NO_COLOR/TERM=dumb support ([#4079](https://github.com/NousResearch/hermes-agent/pull/4079)), ascii-video skill updates ([#4054](https://github.com/NousResearch/hermes-agent/pull/4054)), research-paper-writing skill ([#4654](https://github.com/NousResearch/hermes-agent/pull/4654))
|
| 281 |
+
|
| 282 |
+
### All Contributors
|
| 283 |
+
@0xbyt4, @arasovic, @Bartok9, @bcross, @binhnt92, @camden-lowrance, @curtitoo, @Dakota, @Dave Tist, @Dean Kerr, @devorun, @dieutx, @Dilee, @el-analista, @erosika, @Gutslabs, @IAvecilla, @Jack, @Johannnnn506, @kshitijk4poor, @Laura Batalha, @Leegenux, @Lume, @MacroAnarchy, @maymuneth, @memosr, @NexVeridian, @Nick, @nils010485, @pefontana, @Penov, @rolme, @SHL0MS, @txchen, @xsmyile
|
| 284 |
+
|
| 285 |
+
### Issues Resolved from Community
|
| 286 |
+
@acsezen ([#2537](https://github.com/NousResearch/hermes-agent/issues/2537)), @arasovic ([#4285](https://github.com/NousResearch/hermes-agent/issues/4285)), @camden-lowrance ([#4462](https://github.com/NousResearch/hermes-agent/issues/4462)), @devorun ([#4601](https://github.com/NousResearch/hermes-agent/issues/4601)), @eloklam ([#4486](https://github.com/NousResearch/hermes-agent/issues/4486)), @HenkDz ([#3719](https://github.com/NousResearch/hermes-agent/issues/3719)), @hypotyposis ([#2153](https://github.com/NousResearch/hermes-agent/issues/2153)), @kazamak ([#4178](https://github.com/NousResearch/hermes-agent/issues/4178)), @lstep ([#4366](https://github.com/NousResearch/hermes-agent/issues/4366)), @Mark-Lok ([#4542](https://github.com/NousResearch/hermes-agent/issues/4542)), @NoJster ([#4421](https://github.com/NousResearch/hermes-agent/issues/4421)), @patp ([#2662](https://github.com/NousResearch/hermes-agent/issues/2662)), @pr0n ([#4601](https://github.com/NousResearch/hermes-agent/issues/4601)), @saulmc ([#4377](https://github.com/NousResearch/hermes-agent/issues/4377)), @SHL0MS ([#4060](https://github.com/NousResearch/hermes-agent/issues/4060), [#4061](https://github.com/NousResearch/hermes-agent/issues/4061), [#4066](https://github.com/NousResearch/hermes-agent/issues/4066), [#4172](https://github.com/NousResearch/hermes-agent/issues/4172), [#4277](https://github.com/NousResearch/hermes-agent/issues/4277)), @Z-Mackintosh ([#4398](https://github.com/NousResearch/hermes-agent/issues/4398))
|
| 287 |
+
|
| 288 |
+
---
|
| 289 |
+
|
| 290 |
+
**Full Changelog**: [v2026.3.30...v2026.4.3](https://github.com/NousResearch/hermes-agent/compare/v2026.3.30...v2026.4.3)
|
RELEASE_v0.8.0.md
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hermes Agent v0.8.0 (v2026.4.8)
|
| 2 |
+
|
| 3 |
+
**Release Date:** April 8, 2026
|
| 4 |
+
|
| 5 |
+
> The intelligence release — background task auto-notifications, free MiMo v2 Pro on Nous Portal, live model switching across all platforms, self-optimized GPT/Codex guidance, native Google AI Studio, smart inactivity timeouts, approval buttons, MCP OAuth 2.1, and 209 merged PRs with 82 resolved issues.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## ✨ Highlights
|
| 10 |
+
|
| 11 |
+
- **Background Process Auto-Notifications (`notify_on_complete`)** — Background tasks can now automatically notify the agent when they finish. Start a long-running process (AI model training, test suites, deployments, builds) and the agent gets notified on completion — no polling needed. The agent can keep working on other things and pick up results when they land. ([#5779](https://github.com/NousResearch/hermes-agent/pull/5779))
|
| 12 |
+
|
| 13 |
+
- **Free Xiaomi MiMo v2 Pro on Nous Portal** — Nous Portal now supports the free-tier Xiaomi MiMo v2 Pro model for auxiliary tasks (compression, vision, summarization), with free-tier model gating and pricing display in model selection. ([#6018](https://github.com/NousResearch/hermes-agent/pull/6018), [#5880](https://github.com/NousResearch/hermes-agent/pull/5880))
|
| 14 |
+
|
| 15 |
+
- **Live Model Switching (`/model` Command)** — Switch models and providers mid-session from CLI, Telegram, Discord, Slack, or any gateway platform. Aggregator-aware resolution keeps you on OpenRouter/Nous when possible, with automatic cross-provider fallback when needed. Interactive model pickers on Telegram and Discord with inline buttons. ([#5181](https://github.com/NousResearch/hermes-agent/pull/5181), [#5742](https://github.com/NousResearch/hermes-agent/pull/5742))
|
| 16 |
+
|
| 17 |
+
- **Self-Optimized GPT/Codex Tool-Use Guidance** — The agent diagnosed and patched 5 failure modes in GPT and Codex tool calling through automated behavioral benchmarking, dramatically improving reliability on OpenAI models. Includes execution discipline guidance and thinking-only prefill continuation for structured reasoning. ([#6120](https://github.com/NousResearch/hermes-agent/pull/6120), [#5414](https://github.com/NousResearch/hermes-agent/pull/5414), [#5931](https://github.com/NousResearch/hermes-agent/pull/5931))
|
| 18 |
+
|
| 19 |
+
- **Google AI Studio (Gemini) Native Provider** — Direct access to Gemini models through Google's AI Studio API. Includes automatic models.dev registry integration for real-time context length detection across any provider. ([#5577](https://github.com/NousResearch/hermes-agent/pull/5577))
|
| 20 |
+
|
| 21 |
+
- **Inactivity-Based Agent Timeouts** — Gateway and cron timeouts now track actual tool activity instead of wall-clock time. Long-running tasks that are actively working will never be killed — only truly idle agents time out. ([#5389](https://github.com/NousResearch/hermes-agent/pull/5389), [#5440](https://github.com/NousResearch/hermes-agent/pull/5440))
|
| 22 |
+
|
| 23 |
+
- **Approval Buttons on Slack & Telegram** — Dangerous command approval via native platform buttons instead of typing `/approve`. Slack gets thread context preservation; Telegram gets emoji reactions for approval status. ([#5890](https://github.com/NousResearch/hermes-agent/pull/5890), [#5975](https://github.com/NousResearch/hermes-agent/pull/5975))
|
| 24 |
+
|
| 25 |
+
- **MCP OAuth 2.1 PKCE + OSV Malware Scanning** — Full standards-compliant OAuth for MCP server authentication, plus automatic malware scanning of MCP extension packages via the OSV vulnerability database. ([#5420](https://github.com/NousResearch/hermes-agent/pull/5420), [#5305](https://github.com/NousResearch/hermes-agent/pull/5305))
|
| 26 |
+
|
| 27 |
+
- **Centralized Logging & Config Validation** — Structured logging to `~/.hermes/logs/` (agent.log + errors.log) with the `hermes logs` command for tailing and filtering. Config structure validation catches malformed YAML at startup before it causes cryptic failures. ([#5430](https://github.com/NousResearch/hermes-agent/pull/5430), [#5426](https://github.com/NousResearch/hermes-agent/pull/5426))
|
| 28 |
+
|
| 29 |
+
- **Plugin System Expansion** — Plugins can now register CLI subcommands, receive request-scoped API hooks with correlation IDs, prompt for required env vars during install, and hook into session lifecycle events (finalize/reset). ([#5295](https://github.com/NousResearch/hermes-agent/pull/5295), [#5427](https://github.com/NousResearch/hermes-agent/pull/5427), [#5470](https://github.com/NousResearch/hermes-agent/pull/5470), [#6129](https://github.com/NousResearch/hermes-agent/pull/6129))
|
| 30 |
+
|
| 31 |
+
- **Matrix Tier 1 & Platform Hardening** — Matrix gets reactions, read receipts, rich formatting, and room management. Discord adds channel controls and ignored channels. Signal gets full MEDIA: tag delivery. Mattermost gets file attachments. Comprehensive reliability fixes across all platforms. ([#5275](https://github.com/NousResearch/hermes-agent/pull/5275), [#5975](https://github.com/NousResearch/hermes-agent/pull/5975), [#5602](https://github.com/NousResearch/hermes-agent/pull/5602))
|
| 32 |
+
|
| 33 |
+
- **Security Hardening Pass** — Consolidated SSRF protections, timing attack mitigations, tar traversal prevention, credential leakage guards, cron path traversal hardening, and cross-session isolation. Terminal workdir sanitization across all backends. ([#5944](https://github.com/NousResearch/hermes-agent/pull/5944), [#5613](https://github.com/NousResearch/hermes-agent/pull/5613), [#5629](https://github.com/NousResearch/hermes-agent/pull/5629))
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
## 🏗️ Core Agent & Architecture
|
| 38 |
+
|
| 39 |
+
### Provider & Model Support
|
| 40 |
+
- **Native Google AI Studio (Gemini) provider** with models.dev integration for automatic context length detection ([#5577](https://github.com/NousResearch/hermes-agent/pull/5577))
|
| 41 |
+
- **`/model` command — full provider+model system overhaul** — live switching across CLI and all gateway platforms with aggregator-aware resolution ([#5181](https://github.com/NousResearch/hermes-agent/pull/5181))
|
| 42 |
+
- **Interactive model picker for Telegram and Discord** — inline button-based model selection ([#5742](https://github.com/NousResearch/hermes-agent/pull/5742))
|
| 43 |
+
- **Nous Portal free-tier model gating** with pricing display in model selection ([#5880](https://github.com/NousResearch/hermes-agent/pull/5880))
|
| 44 |
+
- **Model pricing display** for OpenRouter and Nous Portal providers ([#5416](https://github.com/NousResearch/hermes-agent/pull/5416))
|
| 45 |
+
- **xAI (Grok) prompt caching** via `x-grok-conv-id` header ([#5604](https://github.com/NousResearch/hermes-agent/pull/5604))
|
| 46 |
+
- **Grok added to tool-use enforcement models** for direct xAI usage ([#5595](https://github.com/NousResearch/hermes-agent/pull/5595))
|
| 47 |
+
- **MiniMax TTS provider** (speech-2.8) ([#4963](https://github.com/NousResearch/hermes-agent/pull/4963))
|
| 48 |
+
- **Non-agentic model warning** — warns users when loading Hermes LLM models not designed for tool use ([#5378](https://github.com/NousResearch/hermes-agent/pull/5378))
|
| 49 |
+
- **Ollama Cloud auth, /model switch persistence**, and alias tab completion ([#5269](https://github.com/NousResearch/hermes-agent/pull/5269))
|
| 50 |
+
- **Preserve dots in OpenCode Go model names** (minimax-m2.7, glm-4.5, kimi-k2.5) ([#5597](https://github.com/NousResearch/hermes-agent/pull/5597))
|
| 51 |
+
- **MiniMax models 404 fix** — strip /v1 from Anthropic base URL for OpenCode Go ([#4918](https://github.com/NousResearch/hermes-agent/pull/4918))
|
| 52 |
+
- **Provider credential reset windows** honored in pooled failover ([#5188](https://github.com/NousResearch/hermes-agent/pull/5188))
|
| 53 |
+
- **OAuth token sync** between credential pool and credentials file ([#4981](https://github.com/NousResearch/hermes-agent/pull/4981))
|
| 54 |
+
- **Stale OAuth credentials** no longer block OpenRouter users on auto-detect ([#5746](https://github.com/NousResearch/hermes-agent/pull/5746))
|
| 55 |
+
- **Codex OAuth credential pool disconnect** + expired token import fix ([#5681](https://github.com/NousResearch/hermes-agent/pull/5681))
|
| 56 |
+
- **Codex pool entry sync** from `~/.codex/auth.json` on exhaustion — @GratefulDave ([#5610](https://github.com/NousResearch/hermes-agent/pull/5610))
|
| 57 |
+
- **Auxiliary client payment fallback** — retry with next provider on 402 ([#5599](https://github.com/NousResearch/hermes-agent/pull/5599))
|
| 58 |
+
- **Auxiliary client resolves named custom providers** and 'main' alias ([#5978](https://github.com/NousResearch/hermes-agent/pull/5978))
|
| 59 |
+
- **Use mimo-v2-pro** for non-vision auxiliary tasks on Nous free tier ([#6018](https://github.com/NousResearch/hermes-agent/pull/6018))
|
| 60 |
+
- **Vision auto-detection** tries main provider first ([#6041](https://github.com/NousResearch/hermes-agent/pull/6041))
|
| 61 |
+
- **Provider re-ordering and Quick Install** — @austinpickett ([#4664](https://github.com/NousResearch/hermes-agent/pull/4664))
|
| 62 |
+
- **Nous OAuth access_token** no longer used as inference API key — @SHL0MS ([#5564](https://github.com/NousResearch/hermes-agent/pull/5564))
|
| 63 |
+
- **HERMES_PORTAL_BASE_URL env var** respected during Nous login — @benbarclay ([#5745](https://github.com/NousResearch/hermes-agent/pull/5745))
|
| 64 |
+
- **Env var overrides** for Nous portal/inference URLs ([#5419](https://github.com/NousResearch/hermes-agent/pull/5419))
|
| 65 |
+
- **Z.AI endpoint auto-detect** via probe and cache ([#5763](https://github.com/NousResearch/hermes-agent/pull/5763))
|
| 66 |
+
- **MiniMax context lengths, model catalog, thinking guard, aux model, and config base_url** corrections ([#6082](https://github.com/NousResearch/hermes-agent/pull/6082))
|
| 67 |
+
- **Community provider/model resolution fixes** — salvaged 4 community PRs + MiniMax aux URL ([#5983](https://github.com/NousResearch/hermes-agent/pull/5983))
|
| 68 |
+
|
| 69 |
+
### Agent Loop & Conversation
|
| 70 |
+
- **Self-optimized GPT/Codex tool-use guidance** via automated behavioral benchmarking — agent self-diagnosed and patched 5 failure modes ([#6120](https://github.com/NousResearch/hermes-agent/pull/6120))
|
| 71 |
+
- **GPT/Codex execution discipline guidance** in system prompts ([#5414](https://github.com/NousResearch/hermes-agent/pull/5414))
|
| 72 |
+
- **Thinking-only prefill continuation** for structured reasoning responses ([#5931](https://github.com/NousResearch/hermes-agent/pull/5931))
|
| 73 |
+
- **Accept reasoning-only responses** without retries — set content to "(empty)" instead of infinite retry ([#5278](https://github.com/NousResearch/hermes-agent/pull/5278))
|
| 74 |
+
- **Jittered retry backoff** — exponential backoff with jitter for API retries ([#6048](https://github.com/NousResearch/hermes-agent/pull/6048))
|
| 75 |
+
- **Smart thinking block signature management** — preserve and manage Anthropic thinking signatures across turns ([#6112](https://github.com/NousResearch/hermes-agent/pull/6112))
|
| 76 |
+
- **Coerce tool call arguments** to match JSON Schema types — fixes models that send strings instead of numbers/booleans ([#5265](https://github.com/NousResearch/hermes-agent/pull/5265))
|
| 77 |
+
- **Save oversized tool results to file** instead of destructive truncation ([#5210](https://github.com/NousResearch/hermes-agent/pull/5210))
|
| 78 |
+
- **Sandbox-aware tool result persistence** ([#6085](https://github.com/NousResearch/hermes-agent/pull/6085))
|
| 79 |
+
- **Streaming fallback** improved after edit failures ([#6110](https://github.com/NousResearch/hermes-agent/pull/6110))
|
| 80 |
+
- **Codex empty-output gaps** covered in fallback + normalizer + auxiliary client ([#5724](https://github.com/NousResearch/hermes-agent/pull/5724), [#5730](https://github.com/NousResearch/hermes-agent/pull/5730), [#5734](https://github.com/NousResearch/hermes-agent/pull/5734))
|
| 81 |
+
- **Codex stream output backfill** from output_item.done events ([#5689](https://github.com/NousResearch/hermes-agent/pull/5689))
|
| 82 |
+
- **Stream consumer creates new message** after tool boundaries ([#5739](https://github.com/NousResearch/hermes-agent/pull/5739))
|
| 83 |
+
- **Codex validation aligned** with normalization for empty stream output ([#5940](https://github.com/NousResearch/hermes-agent/pull/5940))
|
| 84 |
+
- **Bridge tool-calls** in copilot-acp adapter ([#5460](https://github.com/NousResearch/hermes-agent/pull/5460))
|
| 85 |
+
- **Filter transcript-only roles** from chat-completions payload ([#4880](https://github.com/NousResearch/hermes-agent/pull/4880))
|
| 86 |
+
- **Context compaction failures fixed** on temperature-restricted models — @MadKangYu ([#5608](https://github.com/NousResearch/hermes-agent/pull/5608))
|
| 87 |
+
- **Sanitize tool_calls for all strict APIs** (Fireworks, Mistral, etc.) — @lumethegreat ([#5183](https://github.com/NousResearch/hermes-agent/pull/5183))
|
| 88 |
+
|
| 89 |
+
### Memory & Sessions
|
| 90 |
+
- **Supermemory memory provider** — new memory plugin with multi-container, search_mode, identity template, and env var override ([#5737](https://github.com/NousResearch/hermes-agent/pull/5737), [#5933](https://github.com/NousResearch/hermes-agent/pull/5933))
|
| 91 |
+
- **Shared thread sessions** by default — multi-user thread support across gateway platforms ([#5391](https://github.com/NousResearch/hermes-agent/pull/5391))
|
| 92 |
+
- **Subagent sessions linked to parent** and hidden from session list ([#5309](https://github.com/NousResearch/hermes-agent/pull/5309))
|
| 93 |
+
- **Profile-scoped memory isolation** and clone support ([#4845](https://github.com/NousResearch/hermes-agent/pull/4845))
|
| 94 |
+
- **Thread gateway user_id to memory plugins** for per-user scoping ([#5895](https://github.com/NousResearch/hermes-agent/pull/5895))
|
| 95 |
+
- **Honcho plugin drift overhaul** + plugin CLI registration system ([#5295](https://github.com/NousResearch/hermes-agent/pull/5295))
|
| 96 |
+
- **Honcho holographic prompt and trust score** rendering preserved ([#4872](https://github.com/NousResearch/hermes-agent/pull/4872))
|
| 97 |
+
- **Honcho doctor fix** — use recall_mode instead of memory_mode — @techguysimon ([#5645](https://github.com/NousResearch/hermes-agent/pull/5645))
|
| 98 |
+
- **RetainDB** — API routes, write queue, dialectic, agent model, file tools fixes ([#5461](https://github.com/NousResearch/hermes-agent/pull/5461))
|
| 99 |
+
- **Hindsight memory plugin overhaul** + memory setup wizard fixes ([#5094](https://github.com/NousResearch/hermes-agent/pull/5094))
|
| 100 |
+
- **mem0 API v2 compat**, prefetch context fencing, secret redaction ([#5423](https://github.com/NousResearch/hermes-agent/pull/5423))
|
| 101 |
+
- **mem0 env vars merged** with mem0.json instead of either/or ([#4939](https://github.com/NousResearch/hermes-agent/pull/4939))
|
| 102 |
+
- **Clean user message** used for all memory provider operations ([#4940](https://github.com/NousResearch/hermes-agent/pull/4940))
|
| 103 |
+
- **Silent memory flush failure** on /new and /resume fixed — @ryanautomated ([#5640](https://github.com/NousResearch/hermes-agent/pull/5640))
|
| 104 |
+
- **OpenViking atexit safety net** for session commit ([#5664](https://github.com/NousResearch/hermes-agent/pull/5664))
|
| 105 |
+
- **OpenViking tenant-scoping headers** for multi-tenant servers ([#4936](https://github.com/NousResearch/hermes-agent/pull/4936))
|
| 106 |
+
- **ByteRover brv query** runs synchronously before LLM call ([#4831](https://github.com/NousResearch/hermes-agent/pull/4831))
|
| 107 |
+
|
| 108 |
+
---
|
| 109 |
+
|
| 110 |
+
## 📱 Messaging Platforms (Gateway)
|
| 111 |
+
|
| 112 |
+
### Gateway Core
|
| 113 |
+
- **Inactivity-based agent timeout** — replaces wall-clock timeout with smart activity tracking; long-running active tasks never killed ([#5389](https://github.com/NousResearch/hermes-agent/pull/5389))
|
| 114 |
+
- **Approval buttons for Slack & Telegram** + Slack thread context preservation ([#5890](https://github.com/NousResearch/hermes-agent/pull/5890))
|
| 115 |
+
- **Live-stream /update output** + forward interactive prompts to user ([#5180](https://github.com/NousResearch/hermes-agent/pull/5180))
|
| 116 |
+
- **Infinite timeout support** + periodic notifications + actionable error messages ([#4959](https://github.com/NousResearch/hermes-agent/pull/4959))
|
| 117 |
+
- **Duplicate message prevention** — gateway dedup + partial stream guard ([#4878](https://github.com/NousResearch/hermes-agent/pull/4878))
|
| 118 |
+
- **Webhook delivery_info persistence** + full session id in /status ([#5942](https://github.com/NousResearch/hermes-agent/pull/5942))
|
| 119 |
+
- **Tool preview truncation** respects tool_preview_length in all/new progress modes ([#5937](https://github.com/NousResearch/hermes-agent/pull/5937))
|
| 120 |
+
- **Short preview truncation** restored for all/new tool progress modes ([#4935](https://github.com/NousResearch/hermes-agent/pull/4935))
|
| 121 |
+
- **Update-pending state** written atomically to prevent corruption ([#4923](https://github.com/NousResearch/hermes-agent/pull/4923))
|
| 122 |
+
- **Approval session key isolated** per turn ([#4884](https://github.com/NousResearch/hermes-agent/pull/4884))
|
| 123 |
+
- **Active-session guard bypass** for /approve, /deny, /stop, /new ([#4926](https://github.com/NousResearch/hermes-agent/pull/4926), [#5765](https://github.com/NousResearch/hermes-agent/pull/5765))
|
| 124 |
+
- **Typing indicator paused** during approval waits ([#5893](https://github.com/NousResearch/hermes-agent/pull/5893))
|
| 125 |
+
- **Caption check** uses exact line-by-line match instead of substring (all platforms) ([#5939](https://github.com/NousResearch/hermes-agent/pull/5939))
|
| 126 |
+
- **MEDIA: tags stripped** from streamed gateway messages ([#5152](https://github.com/NousResearch/hermes-agent/pull/5152))
|
| 127 |
+
- **MEDIA: tags extracted** from cron delivery before sending ([#5598](https://github.com/NousResearch/hermes-agent/pull/5598))
|
| 128 |
+
- **Profile-aware service units** + voice transcription cleanup ([#5972](https://github.com/NousResearch/hermes-agent/pull/5972))
|
| 129 |
+
- **Thread-safe PairingStore** with atomic writes — @CharlieKerfoot ([#5656](https://github.com/NousResearch/hermes-agent/pull/5656))
|
| 130 |
+
- **Sanitize media URLs** in base platform logs — @WAXLYY ([#5631](https://github.com/NousResearch/hermes-agent/pull/5631))
|
| 131 |
+
- **Reduce Telegram fallback IP activation log noise** — @MadKangYu ([#5615](https://github.com/NousResearch/hermes-agent/pull/5615))
|
| 132 |
+
- **Cron static method wrappers** to prevent self-binding ([#5299](https://github.com/NousResearch/hermes-agent/pull/5299))
|
| 133 |
+
- **Stale 'hermes login' replaced** with 'hermes auth' + credential removal re-seeding fix ([#5670](https://github.com/NousResearch/hermes-agent/pull/5670))
|
| 134 |
+
|
| 135 |
+
### Telegram
|
| 136 |
+
- **Group topics skill binding** for supergroup forum topics ([#4886](https://github.com/NousResearch/hermes-agent/pull/4886))
|
| 137 |
+
- **Emoji reactions** for approval status and notifications ([#5975](https://github.com/NousResearch/hermes-agent/pull/5975))
|
| 138 |
+
- **Duplicate message delivery prevented** on send timeout ([#5153](https://github.com/NousResearch/hermes-agent/pull/5153))
|
| 139 |
+
- **Command names sanitized** to strip invalid characters ([#5596](https://github.com/NousResearch/hermes-agent/pull/5596))
|
| 140 |
+
- **Per-platform disabled skills** respected in Telegram menu and gateway dispatch ([#4799](https://github.com/NousResearch/hermes-agent/pull/4799))
|
| 141 |
+
- **/approve and /deny** routed through running-agent guard ([#4798](https://github.com/NousResearch/hermes-agent/pull/4798))
|
| 142 |
+
|
| 143 |
+
### Discord
|
| 144 |
+
- **Channel controls** — ignored_channels and no_thread_channels config options ([#5975](https://github.com/NousResearch/hermes-agent/pull/5975))
|
| 145 |
+
- **Skills registered as native slash commands** via shared gateway logic ([#5603](https://github.com/NousResearch/hermes-agent/pull/5603))
|
| 146 |
+
- **/approve, /deny, /queue, /background, /btw** registered as native slash commands ([#4800](https://github.com/NousResearch/hermes-agent/pull/4800), [#5477](https://github.com/NousResearch/hermes-agent/pull/5477))
|
| 147 |
+
- **Unnecessary members intent** removed on startup + token lock leak fix ([#5302](https://github.com/NousResearch/hermes-agent/pull/5302))
|
| 148 |
+
|
| 149 |
+
### Slack
|
| 150 |
+
- **Thread engagement** — auto-respond in bot-started and mentioned threads ([#5897](https://github.com/NousResearch/hermes-agent/pull/5897))
|
| 151 |
+
- **mrkdwn in edit_message** + thread replies without @mentions ([#5733](https://github.com/NousResearch/hermes-agent/pull/5733))
|
| 152 |
+
|
| 153 |
+
### Matrix
|
| 154 |
+
- **Tier 1 feature parity** — reactions, read receipts, rich formatting, room management ([#5275](https://github.com/NousResearch/hermes-agent/pull/5275))
|
| 155 |
+
- **MATRIX_REQUIRE_MENTION and MATRIX_AUTO_THREAD** support ([#5106](https://github.com/NousResearch/hermes-agent/pull/5106))
|
| 156 |
+
- **Comprehensive reliability** — encrypted media, auth recovery, cron E2EE, Synapse compat ([#5271](https://github.com/NousResearch/hermes-agent/pull/5271))
|
| 157 |
+
- **CJK input, E2EE, and reconnect** fixes ([#5665](https://github.com/NousResearch/hermes-agent/pull/5665))
|
| 158 |
+
|
| 159 |
+
### Signal
|
| 160 |
+
- **Full MEDIA: tag delivery** — send_image_file, send_voice, and send_video implemented ([#5602](https://github.com/NousResearch/hermes-agent/pull/5602))
|
| 161 |
+
|
| 162 |
+
### Mattermost
|
| 163 |
+
- **File attachments** — set message type to DOCUMENT when post has file attachments — @nericervin ([#5609](https://github.com/NousResearch/hermes-agent/pull/5609))
|
| 164 |
+
|
| 165 |
+
### Feishu
|
| 166 |
+
- **Interactive card approval buttons** ([#6043](https://github.com/NousResearch/hermes-agent/pull/6043))
|
| 167 |
+
- **Reconnect and ACL** fixes ([#5665](https://github.com/NousResearch/hermes-agent/pull/5665))
|
| 168 |
+
|
| 169 |
+
### Webhooks
|
| 170 |
+
- **`{__raw__}` template token** and thread_id passthrough for forum topics ([#5662](https://github.com/NousResearch/hermes-agent/pull/5662))
|
| 171 |
+
|
| 172 |
+
---
|
| 173 |
+
|
| 174 |
+
## 🖥️ CLI & User Experience
|
| 175 |
+
|
| 176 |
+
### Interactive CLI
|
| 177 |
+
- **Defer response content** until reasoning block completes ([#5773](https://github.com/NousResearch/hermes-agent/pull/5773))
|
| 178 |
+
- **Ghost status-bar lines cleared** on terminal resize ([#4960](https://github.com/NousResearch/hermes-agent/pull/4960))
|
| 179 |
+
- **Normalise \r\n and \r line endings** in pasted text ([#4849](https://github.com/NousResearch/hermes-agent/pull/4849))
|
| 180 |
+
- **ChatConsole errors, curses scroll, skin-aware banner, git state** banner fixes ([#5974](https://github.com/NousResearch/hermes-agent/pull/5974))
|
| 181 |
+
- **Native Windows image paste** support ([#5917](https://github.com/NousResearch/hermes-agent/pull/5917))
|
| 182 |
+
- **--yolo and other flags** no longer silently dropped when placed before 'chat' subcommand ([#5145](https://github.com/NousResearch/hermes-agent/pull/5145))
|
| 183 |
+
|
| 184 |
+
### Setup & Configuration
|
| 185 |
+
- **Config structure validation** — detect malformed YAML at startup with actionable error messages ([#5426](https://github.com/NousResearch/hermes-agent/pull/5426))
|
| 186 |
+
- **Centralized logging** to `~/.hermes/logs/` — agent.log (INFO+), errors.log (WARNING+) with `hermes logs` command ([#5430](https://github.com/NousResearch/hermes-agent/pull/5430))
|
| 187 |
+
- **Docs links added** to setup wizard sections ([#5283](https://github.com/NousResearch/hermes-agent/pull/5283))
|
| 188 |
+
- **Doctor diagnostics** — sync provider checks, config migration, WAL and mem0 diagnostics ([#5077](https://github.com/NousResearch/hermes-agent/pull/5077))
|
| 189 |
+
- **Timeout debug logging** and user-facing diagnostics improved ([#5370](https://github.com/NousResearch/hermes-agent/pull/5370))
|
| 190 |
+
- **Reasoning effort unified** to config.yaml only ([#6118](https://github.com/NousResearch/hermes-agent/pull/6118))
|
| 191 |
+
- **Permanent command allowlist** loaded on startup ([#5076](https://github.com/NousResearch/hermes-agent/pull/5076))
|
| 192 |
+
- **`hermes auth remove`** now clears env-seeded credentials permanently ([#5285](https://github.com/NousResearch/hermes-agent/pull/5285))
|
| 193 |
+
- **Bundled skills synced to all profiles** during update ([#5795](https://github.com/NousResearch/hermes-agent/pull/5795))
|
| 194 |
+
- **`hermes update` no longer kills** freshly-restarted gateway service ([#5448](https://github.com/NousResearch/hermes-agent/pull/5448))
|
| 195 |
+
- **Subprocess.run() timeouts** added to all gateway CLI commands ([#5424](https://github.com/NousResearch/hermes-agent/pull/5424))
|
| 196 |
+
- **Actionable error message** when Codex refresh token is reused — @tymrtn ([#5612](https://github.com/NousResearch/hermes-agent/pull/5612))
|
| 197 |
+
- **Google-workspace skill scripts** can now run directly — @xinbenlv ([#5624](https://github.com/NousResearch/hermes-agent/pull/5624))
|
| 198 |
+
|
| 199 |
+
### Cron System
|
| 200 |
+
- **Inactivity-based cron timeout** — replaces wall-clock; active tasks run indefinitely ([#5440](https://github.com/NousResearch/hermes-agent/pull/5440))
|
| 201 |
+
- **Pre-run script injection** for data collection and change detection ([#5082](https://github.com/NousResearch/hermes-agent/pull/5082))
|
| 202 |
+
- **Delivery failure tracking** in job status ([#6042](https://github.com/NousResearch/hermes-agent/pull/6042))
|
| 203 |
+
- **Delivery guidance** in cron prompts — stops send_message thrashing ([#5444](https://github.com/NousResearch/hermes-agent/pull/5444))
|
| 204 |
+
- **MEDIA files delivered** as native platform attachments ([#5921](https://github.com/NousResearch/hermes-agent/pull/5921))
|
| 205 |
+
- **[SILENT] suppression** works anywhere in response — @auspic7 ([#5654](https://github.com/NousResearch/hermes-agent/pull/5654))
|
| 206 |
+
- **Cron path traversal** hardening ([#5147](https://github.com/NousResearch/hermes-agent/pull/5147))
|
| 207 |
+
|
| 208 |
+
---
|
| 209 |
+
|
| 210 |
+
## 🔧 Tool System
|
| 211 |
+
|
| 212 |
+
### Terminal & Execution
|
| 213 |
+
- **Execute_code on remote backends** — code execution now works on Docker, SSH, Modal, and other remote terminal backends ([#5088](https://github.com/NousResearch/hermes-agent/pull/5088))
|
| 214 |
+
- **Exit code context** for common CLI tools in terminal results — helps agent understand what went wrong ([#5144](https://github.com/NousResearch/hermes-agent/pull/5144))
|
| 215 |
+
- **Progressive subdirectory hint discovery** — agent learns project structure as it navigates ([#5291](https://github.com/NousResearch/hermes-agent/pull/5291))
|
| 216 |
+
- **notify_on_complete for background processes** — get notified when long-running tasks finish ([#5779](https://github.com/NousResearch/hermes-agent/pull/5779))
|
| 217 |
+
- **Docker env config** — explicit container environment variables via docker_env config ([#4738](https://github.com/NousResearch/hermes-agent/pull/4738))
|
| 218 |
+
- **Approval metadata included** in terminal tool results ([#5141](https://github.com/NousResearch/hermes-agent/pull/5141))
|
| 219 |
+
- **Workdir parameter sanitized** in terminal tool across all backends ([#5629](https://github.com/NousResearch/hermes-agent/pull/5629))
|
| 220 |
+
- **Detached process crash recovery** state corrected ([#6101](https://github.com/NousResearch/hermes-agent/pull/6101))
|
| 221 |
+
- **Agent-browser paths with spaces** preserved — @Vasanthdev2004 ([#6077](https://github.com/NousResearch/hermes-agent/pull/6077))
|
| 222 |
+
- **Portable base64 encoding** for image reading on macOS — @CharlieKerfoot ([#5657](https://github.com/NousResearch/hermes-agent/pull/5657))
|
| 223 |
+
|
| 224 |
+
### Browser
|
| 225 |
+
- **Switch managed browser provider** from Browserbase to Browser Use — @benbarclay ([#5750](https://github.com/NousResearch/hermes-agent/pull/5750))
|
| 226 |
+
- **Firecrawl cloud browser** provider — @alt-glitch ([#5628](https://github.com/NousResearch/hermes-agent/pull/5628))
|
| 227 |
+
- **JS evaluation** via browser_console expression parameter ([#5303](https://github.com/NousResearch/hermes-agent/pull/5303))
|
| 228 |
+
- **Windows browser** fixes ([#5665](https://github.com/NousResearch/hermes-agent/pull/5665))
|
| 229 |
+
|
| 230 |
+
### MCP
|
| 231 |
+
- **MCP OAuth 2.1 PKCE** — full standards-compliant OAuth client support ([#5420](https://github.com/NousResearch/hermes-agent/pull/5420))
|
| 232 |
+
- **OSV malware check** for MCP extension packages ([#5305](https://github.com/NousResearch/hermes-agent/pull/5305))
|
| 233 |
+
- **Prefer structuredContent over text** + no_mcp sentinel ([#5979](https://github.com/NousResearch/hermes-agent/pull/5979))
|
| 234 |
+
- **Unknown toolsets warning suppressed** for MCP server names ([#5279](https://github.com/NousResearch/hermes-agent/pull/5279))
|
| 235 |
+
|
| 236 |
+
### Web & Files
|
| 237 |
+
- **.zip document support** + auto-mount cache dirs into remote backends ([#4846](https://github.com/NousResearch/hermes-agent/pull/4846))
|
| 238 |
+
- **Redact query secrets** in send_message errors — @WAXLYY ([#5650](https://github.com/NousResearch/hermes-agent/pull/5650))
|
| 239 |
+
|
| 240 |
+
### Delegation
|
| 241 |
+
- **Credential pool sharing** + workspace path hints for subagents ([#5748](https://github.com/NousResearch/hermes-agent/pull/5748))
|
| 242 |
+
|
| 243 |
+
### ACP (VS Code / Zed / JetBrains)
|
| 244 |
+
- **Aggregate ACP improvements** — auth compat, protocol fixes, command ads, delegation, SSE events ([#5292](https://github.com/NousResearch/hermes-agent/pull/5292))
|
| 245 |
+
|
| 246 |
+
---
|
| 247 |
+
|
| 248 |
+
## 🧩 Skills Ecosystem
|
| 249 |
+
|
| 250 |
+
### Skills System
|
| 251 |
+
- **Skill config interface** — skills can declare required config.yaml settings, prompted during setup, injected at load time ([#5635](https://github.com/NousResearch/hermes-agent/pull/5635))
|
| 252 |
+
- **Plugin CLI registration system** — plugins register their own CLI subcommands without touching main.py ([#5295](https://github.com/NousResearch/hermes-agent/pull/5295))
|
| 253 |
+
- **Request-scoped API hooks** with tool call correlation IDs for plugins ([#5427](https://github.com/NousResearch/hermes-agent/pull/5427))
|
| 254 |
+
- **Session lifecycle hooks** — on_session_finalize and on_session_reset for CLI + gateway ([#6129](https://github.com/NousResearch/hermes-agent/pull/6129))
|
| 255 |
+
- **Prompt for required env vars** during plugin install — @kshitijk4poor ([#5470](https://github.com/NousResearch/hermes-agent/pull/5470))
|
| 256 |
+
- **Plugin name validation** — reject names that resolve to plugins root ([#5368](https://github.com/NousResearch/hermes-agent/pull/5368))
|
| 257 |
+
- **pre_llm_call plugin context** moved to user message to preserve prompt cache ([#5146](https://github.com/NousResearch/hermes-agent/pull/5146))
|
| 258 |
+
|
| 259 |
+
### New & Updated Skills
|
| 260 |
+
- **popular-web-designs** — 54 production website design systems ([#5194](https://github.com/NousResearch/hermes-agent/pull/5194))
|
| 261 |
+
- **p5js creative coding** — @SHL0MS ([#5600](https://github.com/NousResearch/hermes-agent/pull/5600))
|
| 262 |
+
- **manim-video** — mathematical and technical animations — @SHL0MS ([#4930](https://github.com/NousResearch/hermes-agent/pull/4930))
|
| 263 |
+
- **llm-wiki** — Karpathy's LLM Wiki skill ([#5635](https://github.com/NousResearch/hermes-agent/pull/5635))
|
| 264 |
+
- **gitnexus-explorer** — codebase indexing and knowledge serving ([#5208](https://github.com/NousResearch/hermes-agent/pull/5208))
|
| 265 |
+
- **research-paper-writing** — AI-Scientist & GPT-Researcher patterns — @SHL0MS ([#5421](https://github.com/NousResearch/hermes-agent/pull/5421))
|
| 266 |
+
- **blogwatcher** updated to JulienTant's fork ([#5759](https://github.com/NousResearch/hermes-agent/pull/5759))
|
| 267 |
+
- **claude-code skill** comprehensive rewrite v2.0 + v2.2 ([#5155](https://github.com/NousResearch/hermes-agent/pull/5155), [#5158](https://github.com/NousResearch/hermes-agent/pull/5158))
|
| 268 |
+
- **Code verification skills** consolidated into one ([#4854](https://github.com/NousResearch/hermes-agent/pull/4854))
|
| 269 |
+
- **Manim CE reference docs** expanded — geometry, animations, LaTeX — @leotrs ([#5791](https://github.com/NousResearch/hermes-agent/pull/5791))
|
| 270 |
+
- **Manim-video references** — design thinking, updaters, paper explainer, decorations, production quality — @SHL0MS ([#5588](https://github.com/NousResearch/hermes-agent/pull/5588), [#5408](https://github.com/NousResearch/hermes-agent/pull/5408))
|
| 271 |
+
|
| 272 |
+
---
|
| 273 |
+
|
| 274 |
+
## 🔒 Security & Reliability
|
| 275 |
+
|
| 276 |
+
### Security Hardening
|
| 277 |
+
- **Consolidated security** — SSRF protections, timing attack mitigations, tar traversal prevention, credential leakage guards ([#5944](https://github.com/NousResearch/hermes-agent/pull/5944))
|
| 278 |
+
- **Cross-session isolation** + cron path traversal hardening ([#5613](https://github.com/NousResearch/hermes-agent/pull/5613))
|
| 279 |
+
- **Workdir parameter sanitized** in terminal tool across all backends ([#5629](https://github.com/NousResearch/hermes-agent/pull/5629))
|
| 280 |
+
- **Approval 'once' session escalation** prevented + cron delivery platform validation ([#5280](https://github.com/NousResearch/hermes-agent/pull/5280))
|
| 281 |
+
- **Profile-scoped Google Workspace OAuth tokens** protected ([#4910](https://github.com/NousResearch/hermes-agent/pull/4910))
|
| 282 |
+
|
| 283 |
+
### Reliability
|
| 284 |
+
- **Aggressive worktree and branch cleanup** to prevent accumulation ([#6134](https://github.com/NousResearch/hermes-agent/pull/6134))
|
| 285 |
+
- **O(n²) catastrophic backtracking** in redact regex fixed — 100x improvement on large outputs ([#4962](https://github.com/NousResearch/hermes-agent/pull/4962))
|
| 286 |
+
- **Runtime stability fixes** across core, web, delegate, and browser tools ([#4843](https://github.com/NousResearch/hermes-agent/pull/4843))
|
| 287 |
+
- **API server streaming fix** + conversation history support ([#5977](https://github.com/NousResearch/hermes-agent/pull/5977))
|
| 288 |
+
- **OpenViking API endpoint paths** and response parsing corrected ([#5078](https://github.com/NousResearch/hermes-agent/pull/5078))
|
| 289 |
+
|
| 290 |
+
---
|
| 291 |
+
|
| 292 |
+
## 🐛 Notable Bug Fixes
|
| 293 |
+
|
| 294 |
+
- **9 community bugfixes salvaged** — gateway, cron, deps, macOS launchd in one batch ([#5288](https://github.com/NousResearch/hermes-agent/pull/5288))
|
| 295 |
+
- **Batch core bug fixes** — model config, session reset, alias fallback, launchctl, delegation, atomic writes ([#5630](https://github.com/NousResearch/hermes-agent/pull/5630))
|
| 296 |
+
- **Batch gateway/platform fixes** — matrix E2EE, CJK input, Windows browser, Feishu reconnect + ACL ([#5665](https://github.com/NousResearch/hermes-agent/pull/5665))
|
| 297 |
+
- **Stale test skips removed**, regex backtracking, file search bug, and test flakiness ([#4969](https://github.com/NousResearch/hermes-agent/pull/4969))
|
| 298 |
+
- **Nix flake** — read version, regen uv.lock, add hermes_logging — @alt-glitch ([#5651](https://github.com/NousResearch/hermes-agent/pull/5651))
|
| 299 |
+
- **Lowercase variable redaction** regression tests ([#5185](https://github.com/NousResearch/hermes-agent/pull/5185))
|
| 300 |
+
|
| 301 |
+
---
|
| 302 |
+
|
| 303 |
+
## 🧪 Testing
|
| 304 |
+
|
| 305 |
+
- **57 failing CI tests repaired** across 14 files ([#5823](https://github.com/NousResearch/hermes-agent/pull/5823))
|
| 306 |
+
- **Test suite re-architecture** + CI failure fixes — @alt-glitch ([#5946](https://github.com/NousResearch/hermes-agent/pull/5946))
|
| 307 |
+
- **Codebase-wide lint cleanup** — unused imports, dead code, and inefficient patterns ([#5821](https://github.com/NousResearch/hermes-agent/pull/5821))
|
| 308 |
+
- **browser_close tool removed** — auto-cleanup handles it ([#5792](https://github.com/NousResearch/hermes-agent/pull/5792))
|
| 309 |
+
|
| 310 |
+
---
|
| 311 |
+
|
| 312 |
+
## 📚 Documentation
|
| 313 |
+
|
| 314 |
+
- **Comprehensive documentation audit** — fix stale info, expand thin pages, add depth ([#5393](https://github.com/NousResearch/hermes-agent/pull/5393))
|
| 315 |
+
- **40+ discrepancies fixed** between documentation and codebase ([#5818](https://github.com/NousResearch/hermes-agent/pull/5818))
|
| 316 |
+
- **13 features documented** from last week's PRs ([#5815](https://github.com/NousResearch/hermes-agent/pull/5815))
|
| 317 |
+
- **Guides section overhaul** — fix existing + add 3 new tutorials ([#5735](https://github.com/NousResearch/hermes-agent/pull/5735))
|
| 318 |
+
- **Salvaged 4 docs PRs** — docker setup, post-update validation, local LLM guide, signal-cli install ([#5727](https://github.com/NousResearch/hermes-agent/pull/5727))
|
| 319 |
+
- **Discord configuration reference** ([#5386](https://github.com/NousResearch/hermes-agent/pull/5386))
|
| 320 |
+
- **Community FAQ entries** for common workflows and troubleshooting ([#4797](https://github.com/NousResearch/hermes-agent/pull/4797))
|
| 321 |
+
- **WSL2 networking guide** for local model servers ([#5616](https://github.com/NousResearch/hermes-agent/pull/5616))
|
| 322 |
+
- **Honcho CLI reference** + plugin CLI registration docs ([#5308](https://github.com/NousResearch/hermes-agent/pull/5308))
|
| 323 |
+
- **Obsidian Headless setup** for servers in llm-wiki ([#5660](https://github.com/NousResearch/hermes-agent/pull/5660))
|
| 324 |
+
- **Hermes Mod visual skin editor** added to skins page ([#6095](https://github.com/NousResearch/hermes-agent/pull/6095))
|
| 325 |
+
|
| 326 |
+
---
|
| 327 |
+
|
| 328 |
+
## 👥 Contributors
|
| 329 |
+
|
| 330 |
+
### Core
|
| 331 |
+
- **@teknium1** — 179 PRs
|
| 332 |
+
|
| 333 |
+
### Top Community Contributors
|
| 334 |
+
- **@SHL0MS** (7 PRs) — p5js creative coding skill, manim-video skill + 5 reference expansions, research-paper-writing, Nous OAuth fix, manim font fix
|
| 335 |
+
- **@alt-glitch** (3 PRs) — Firecrawl cloud browser provider, test re-architecture + CI fixes, Nix flake fixes
|
| 336 |
+
- **@benbarclay** (2 PRs) — Browser Use managed provider switch, Nous portal base URL fix
|
| 337 |
+
- **@CharlieKerfoot** (2 PRs) — macOS portable base64 encoding, thread-safe PairingStore
|
| 338 |
+
- **@WAXLYY** (2 PRs) — send_message secret redaction, gateway media URL sanitization
|
| 339 |
+
- **@MadKangYu** (2 PRs) — Telegram log noise reduction, context compaction fix for temperature-restricted models
|
| 340 |
+
|
| 341 |
+
### All Contributors
|
| 342 |
+
@alt-glitch, @austinpickett, @auspic7, @benbarclay, @CharlieKerfoot, @GratefulDave, @kshitijk4poor, @leotrs, @lumethegreat, @MadKangYu, @nericervin, @ryanautomated, @SHL0MS, @techguysimon, @tymrtn, @Vasanthdev2004, @WAXLYY, @xinbenlv
|
| 343 |
+
|
| 344 |
+
---
|
| 345 |
+
|
| 346 |
+
**Full Changelog**: [v2026.4.3...v2026.4.8](https://github.com/NousResearch/hermes-agent/compare/v2026.4.3...v2026.4.8)
|
acp_adapter/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""ACP (Agent Communication Protocol) adapter for hermes-agent."""
|
acp_adapter/__main__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Allow running the ACP adapter as ``python -m acp_adapter``."""
|
| 2 |
+
|
| 3 |
+
from .entry import main
|
| 4 |
+
|
| 5 |
+
main()
|
acp_adapter/auth.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ACP auth helpers — detect the currently configured Hermes provider."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import Optional
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def detect_provider() -> Optional[str]:
|
| 9 |
+
"""Resolve the active Hermes runtime provider, or None if unavailable."""
|
| 10 |
+
try:
|
| 11 |
+
from hermes_cli.runtime_provider import resolve_runtime_provider
|
| 12 |
+
runtime = resolve_runtime_provider()
|
| 13 |
+
api_key = runtime.get("api_key")
|
| 14 |
+
provider = runtime.get("provider")
|
| 15 |
+
if isinstance(api_key, str) and api_key.strip() and isinstance(provider, str) and provider.strip():
|
| 16 |
+
return provider.strip().lower()
|
| 17 |
+
except Exception:
|
| 18 |
+
return None
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def has_provider() -> bool:
|
| 23 |
+
"""Return True if Hermes can resolve any runtime provider credentials."""
|
| 24 |
+
return detect_provider() is not None
|
acp_adapter/entry.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""CLI entry point for the hermes-agent ACP adapter.
|
| 2 |
+
|
| 3 |
+
Loads environment variables from ``~/.hermes/.env``, configures logging
|
| 4 |
+
to write to stderr (so stdout is reserved for ACP JSON-RPC transport),
|
| 5 |
+
and starts the ACP agent server.
|
| 6 |
+
|
| 7 |
+
Usage::
|
| 8 |
+
|
| 9 |
+
python -m acp_adapter.entry
|
| 10 |
+
# or
|
| 11 |
+
hermes acp
|
| 12 |
+
# or
|
| 13 |
+
hermes-acp
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import asyncio
|
| 17 |
+
import logging
|
| 18 |
+
import sys
|
| 19 |
+
from pathlib import Path
|
| 20 |
+
from hermes_constants import get_hermes_home
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _setup_logging() -> None:
|
| 24 |
+
"""Route all logging to stderr so stdout stays clean for ACP stdio."""
|
| 25 |
+
handler = logging.StreamHandler(sys.stderr)
|
| 26 |
+
handler.setFormatter(
|
| 27 |
+
logging.Formatter(
|
| 28 |
+
"%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
| 29 |
+
datefmt="%Y-%m-%d %H:%M:%S",
|
| 30 |
+
)
|
| 31 |
+
)
|
| 32 |
+
root = logging.getLogger()
|
| 33 |
+
root.handlers.clear()
|
| 34 |
+
root.addHandler(handler)
|
| 35 |
+
root.setLevel(logging.INFO)
|
| 36 |
+
|
| 37 |
+
# Quiet down noisy libraries
|
| 38 |
+
logging.getLogger("httpx").setLevel(logging.WARNING)
|
| 39 |
+
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
| 40 |
+
logging.getLogger("openai").setLevel(logging.WARNING)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _load_env() -> None:
|
| 44 |
+
"""Load .env from HERMES_HOME (default ``~/.hermes``)."""
|
| 45 |
+
from hermes_cli.env_loader import load_hermes_dotenv
|
| 46 |
+
|
| 47 |
+
hermes_home = get_hermes_home()
|
| 48 |
+
loaded = load_hermes_dotenv(hermes_home=hermes_home)
|
| 49 |
+
if loaded:
|
| 50 |
+
for env_file in loaded:
|
| 51 |
+
logging.getLogger(__name__).info("Loaded env from %s", env_file)
|
| 52 |
+
else:
|
| 53 |
+
logging.getLogger(__name__).info(
|
| 54 |
+
"No .env found at %s, using system env", hermes_home / ".env"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def main() -> None:
|
| 59 |
+
"""Entry point: load env, configure logging, run the ACP agent."""
|
| 60 |
+
_setup_logging()
|
| 61 |
+
_load_env()
|
| 62 |
+
|
| 63 |
+
logger = logging.getLogger(__name__)
|
| 64 |
+
logger.info("Starting hermes-agent ACP adapter")
|
| 65 |
+
|
| 66 |
+
# Ensure the project root is on sys.path so ``from run_agent import AIAgent`` works
|
| 67 |
+
project_root = str(Path(__file__).resolve().parent.parent)
|
| 68 |
+
if project_root not in sys.path:
|
| 69 |
+
sys.path.insert(0, project_root)
|
| 70 |
+
|
| 71 |
+
import acp
|
| 72 |
+
from .server import HermesACPAgent
|
| 73 |
+
|
| 74 |
+
agent = HermesACPAgent()
|
| 75 |
+
try:
|
| 76 |
+
asyncio.run(acp.run_agent(agent, use_unstable_protocol=True))
|
| 77 |
+
except KeyboardInterrupt:
|
| 78 |
+
logger.info("Shutting down (KeyboardInterrupt)")
|
| 79 |
+
except Exception:
|
| 80 |
+
logger.exception("ACP agent crashed")
|
| 81 |
+
sys.exit(1)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
main()
|
acp_adapter/events.py
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Callback factories for bridging AIAgent events to ACP notifications.
|
| 2 |
+
|
| 3 |
+
Each factory returns a callable with the signature that AIAgent expects
|
| 4 |
+
for its callbacks. Internally, the callbacks push ACP session updates
|
| 5 |
+
to the client via ``conn.session_update()`` using
|
| 6 |
+
``asyncio.run_coroutine_threadsafe()`` (since AIAgent runs in a worker
|
| 7 |
+
thread while the event loop lives on the main thread).
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import asyncio
|
| 11 |
+
import json
|
| 12 |
+
import logging
|
| 13 |
+
from collections import deque
|
| 14 |
+
from typing import Any, Callable, Deque, Dict
|
| 15 |
+
|
| 16 |
+
import acp
|
| 17 |
+
|
| 18 |
+
from .tools import (
|
| 19 |
+
build_tool_complete,
|
| 20 |
+
build_tool_start,
|
| 21 |
+
make_tool_call_id,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _send_update(
|
| 28 |
+
conn: acp.Client,
|
| 29 |
+
session_id: str,
|
| 30 |
+
loop: asyncio.AbstractEventLoop,
|
| 31 |
+
update: Any,
|
| 32 |
+
) -> None:
|
| 33 |
+
"""Fire-and-forget an ACP session update from a worker thread."""
|
| 34 |
+
try:
|
| 35 |
+
future = asyncio.run_coroutine_threadsafe(
|
| 36 |
+
conn.session_update(session_id, update), loop
|
| 37 |
+
)
|
| 38 |
+
future.result(timeout=5)
|
| 39 |
+
except Exception:
|
| 40 |
+
logger.debug("Failed to send ACP update", exc_info=True)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# ------------------------------------------------------------------
|
| 44 |
+
# Tool progress callback
|
| 45 |
+
# ------------------------------------------------------------------
|
| 46 |
+
|
| 47 |
+
def make_tool_progress_cb(
|
| 48 |
+
conn: acp.Client,
|
| 49 |
+
session_id: str,
|
| 50 |
+
loop: asyncio.AbstractEventLoop,
|
| 51 |
+
tool_call_ids: Dict[str, Deque[str]],
|
| 52 |
+
) -> Callable:
|
| 53 |
+
"""Create a ``tool_progress_callback`` for AIAgent.
|
| 54 |
+
|
| 55 |
+
Signature expected by AIAgent::
|
| 56 |
+
|
| 57 |
+
tool_progress_callback(event_type: str, name: str, preview: str, args: dict, **kwargs)
|
| 58 |
+
|
| 59 |
+
Emits ``ToolCallStart`` for ``tool.started`` events and tracks IDs in a FIFO
|
| 60 |
+
queue per tool name so duplicate/parallel same-name calls still complete
|
| 61 |
+
against the correct ACP tool call. Other event types (``tool.completed``,
|
| 62 |
+
``reasoning.available``) are silently ignored.
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
def _tool_progress(event_type: str, name: str = None, preview: str = None, args: Any = None, **kwargs) -> None:
|
| 66 |
+
# Only emit ACP ToolCallStart for tool.started; ignore other event types
|
| 67 |
+
if event_type != "tool.started":
|
| 68 |
+
return
|
| 69 |
+
if isinstance(args, str):
|
| 70 |
+
try:
|
| 71 |
+
args = json.loads(args)
|
| 72 |
+
except (json.JSONDecodeError, TypeError):
|
| 73 |
+
args = {"raw": args}
|
| 74 |
+
if not isinstance(args, dict):
|
| 75 |
+
args = {}
|
| 76 |
+
|
| 77 |
+
tc_id = make_tool_call_id()
|
| 78 |
+
queue = tool_call_ids.get(name)
|
| 79 |
+
if queue is None:
|
| 80 |
+
queue = deque()
|
| 81 |
+
tool_call_ids[name] = queue
|
| 82 |
+
elif isinstance(queue, str):
|
| 83 |
+
queue = deque([queue])
|
| 84 |
+
tool_call_ids[name] = queue
|
| 85 |
+
queue.append(tc_id)
|
| 86 |
+
|
| 87 |
+
update = build_tool_start(tc_id, name, args)
|
| 88 |
+
_send_update(conn, session_id, loop, update)
|
| 89 |
+
|
| 90 |
+
return _tool_progress
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# ------------------------------------------------------------------
|
| 94 |
+
# Thinking callback
|
| 95 |
+
# ------------------------------------------------------------------
|
| 96 |
+
|
| 97 |
+
def make_thinking_cb(
|
| 98 |
+
conn: acp.Client,
|
| 99 |
+
session_id: str,
|
| 100 |
+
loop: asyncio.AbstractEventLoop,
|
| 101 |
+
) -> Callable:
|
| 102 |
+
"""Create a ``thinking_callback`` for AIAgent."""
|
| 103 |
+
|
| 104 |
+
def _thinking(text: str) -> None:
|
| 105 |
+
if not text:
|
| 106 |
+
return
|
| 107 |
+
update = acp.update_agent_thought_text(text)
|
| 108 |
+
_send_update(conn, session_id, loop, update)
|
| 109 |
+
|
| 110 |
+
return _thinking
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# ------------------------------------------------------------------
|
| 114 |
+
# Step callback
|
| 115 |
+
# ------------------------------------------------------------------
|
| 116 |
+
|
| 117 |
+
def make_step_cb(
|
| 118 |
+
conn: acp.Client,
|
| 119 |
+
session_id: str,
|
| 120 |
+
loop: asyncio.AbstractEventLoop,
|
| 121 |
+
tool_call_ids: Dict[str, Deque[str]],
|
| 122 |
+
) -> Callable:
|
| 123 |
+
"""Create a ``step_callback`` for AIAgent.
|
| 124 |
+
|
| 125 |
+
Signature expected by AIAgent::
|
| 126 |
+
|
| 127 |
+
step_callback(api_call_count: int, prev_tools: list)
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
def _step(api_call_count: int, prev_tools: Any = None) -> None:
|
| 131 |
+
if prev_tools and isinstance(prev_tools, list):
|
| 132 |
+
for tool_info in prev_tools:
|
| 133 |
+
tool_name = None
|
| 134 |
+
result = None
|
| 135 |
+
|
| 136 |
+
if isinstance(tool_info, dict):
|
| 137 |
+
tool_name = tool_info.get("name") or tool_info.get("function_name")
|
| 138 |
+
result = tool_info.get("result") or tool_info.get("output")
|
| 139 |
+
elif isinstance(tool_info, str):
|
| 140 |
+
tool_name = tool_info
|
| 141 |
+
|
| 142 |
+
queue = tool_call_ids.get(tool_name or "")
|
| 143 |
+
if isinstance(queue, str):
|
| 144 |
+
queue = deque([queue])
|
| 145 |
+
tool_call_ids[tool_name] = queue
|
| 146 |
+
if tool_name and queue:
|
| 147 |
+
tc_id = queue.popleft()
|
| 148 |
+
update = build_tool_complete(
|
| 149 |
+
tc_id, tool_name, result=str(result) if result is not None else None
|
| 150 |
+
)
|
| 151 |
+
_send_update(conn, session_id, loop, update)
|
| 152 |
+
if not queue:
|
| 153 |
+
tool_call_ids.pop(tool_name, None)
|
| 154 |
+
|
| 155 |
+
return _step
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# ------------------------------------------------------------------
|
| 159 |
+
# Agent message callback
|
| 160 |
+
# ------------------------------------------------------------------
|
| 161 |
+
|
| 162 |
+
def make_message_cb(
|
| 163 |
+
conn: acp.Client,
|
| 164 |
+
session_id: str,
|
| 165 |
+
loop: asyncio.AbstractEventLoop,
|
| 166 |
+
) -> Callable:
|
| 167 |
+
"""Create a callback that streams agent response text to the editor."""
|
| 168 |
+
|
| 169 |
+
def _message(text: str) -> None:
|
| 170 |
+
if not text:
|
| 171 |
+
return
|
| 172 |
+
update = acp.update_agent_message_text(text)
|
| 173 |
+
_send_update(conn, session_id, loop, update)
|
| 174 |
+
|
| 175 |
+
return _message
|
acp_adapter/permissions.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ACP permission bridging — maps ACP approval requests to hermes approval callbacks."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import asyncio
|
| 6 |
+
import logging
|
| 7 |
+
from concurrent.futures import TimeoutError as FutureTimeout
|
| 8 |
+
from typing import Callable
|
| 9 |
+
|
| 10 |
+
from acp.schema import (
|
| 11 |
+
AllowedOutcome,
|
| 12 |
+
PermissionOption,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
# Maps ACP PermissionOptionKind -> hermes approval result strings
|
| 18 |
+
_KIND_TO_HERMES = {
|
| 19 |
+
"allow_once": "once",
|
| 20 |
+
"allow_always": "always",
|
| 21 |
+
"reject_once": "deny",
|
| 22 |
+
"reject_always": "deny",
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def make_approval_callback(
|
| 27 |
+
request_permission_fn: Callable,
|
| 28 |
+
loop: asyncio.AbstractEventLoop,
|
| 29 |
+
session_id: str,
|
| 30 |
+
timeout: float = 60.0,
|
| 31 |
+
) -> Callable[[str, str], str]:
|
| 32 |
+
"""
|
| 33 |
+
Return a hermes-compatible ``approval_callback(command, description) -> str``
|
| 34 |
+
that bridges to the ACP client's ``request_permission`` call.
|
| 35 |
+
|
| 36 |
+
Args:
|
| 37 |
+
request_permission_fn: The ACP connection's ``request_permission`` coroutine.
|
| 38 |
+
loop: The event loop on which the ACP connection lives.
|
| 39 |
+
session_id: Current ACP session id.
|
| 40 |
+
timeout: Seconds to wait for a response before auto-denying.
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
def _callback(command: str, description: str) -> str:
|
| 44 |
+
options = [
|
| 45 |
+
PermissionOption(option_id="allow_once", kind="allow_once", name="Allow once"),
|
| 46 |
+
PermissionOption(option_id="allow_always", kind="allow_always", name="Allow always"),
|
| 47 |
+
PermissionOption(option_id="deny", kind="reject_once", name="Deny"),
|
| 48 |
+
]
|
| 49 |
+
import acp as _acp
|
| 50 |
+
|
| 51 |
+
tool_call = _acp.start_tool_call("perm-check", command, kind="execute")
|
| 52 |
+
|
| 53 |
+
coro = request_permission_fn(
|
| 54 |
+
session_id=session_id,
|
| 55 |
+
tool_call=tool_call,
|
| 56 |
+
options=options,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
future = asyncio.run_coroutine_threadsafe(coro, loop)
|
| 61 |
+
response = future.result(timeout=timeout)
|
| 62 |
+
except (FutureTimeout, Exception) as exc:
|
| 63 |
+
logger.warning("Permission request timed out or failed: %s", exc)
|
| 64 |
+
return "deny"
|
| 65 |
+
|
| 66 |
+
outcome = response.outcome
|
| 67 |
+
if isinstance(outcome, AllowedOutcome):
|
| 68 |
+
option_id = outcome.option_id
|
| 69 |
+
# Look up the kind from our options list
|
| 70 |
+
for opt in options:
|
| 71 |
+
if opt.option_id == option_id:
|
| 72 |
+
return _KIND_TO_HERMES.get(opt.kind, "deny")
|
| 73 |
+
return "once" # fallback for unknown option_id
|
| 74 |
+
else:
|
| 75 |
+
return "deny"
|
| 76 |
+
|
| 77 |
+
return _callback
|
acp_adapter/server.py
ADDED
|
@@ -0,0 +1,728 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ACP agent server — exposes Hermes Agent via the Agent Client Protocol."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import asyncio
|
| 6 |
+
import logging
|
| 7 |
+
from collections import defaultdict, deque
|
| 8 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
+
from typing import Any, Deque, Optional
|
| 10 |
+
|
| 11 |
+
import acp
|
| 12 |
+
from acp.schema import (
|
| 13 |
+
AgentCapabilities,
|
| 14 |
+
AuthenticateResponse,
|
| 15 |
+
AvailableCommand,
|
| 16 |
+
AvailableCommandsUpdate,
|
| 17 |
+
ClientCapabilities,
|
| 18 |
+
EmbeddedResourceContentBlock,
|
| 19 |
+
ForkSessionResponse,
|
| 20 |
+
ImageContentBlock,
|
| 21 |
+
AudioContentBlock,
|
| 22 |
+
Implementation,
|
| 23 |
+
InitializeResponse,
|
| 24 |
+
ListSessionsResponse,
|
| 25 |
+
LoadSessionResponse,
|
| 26 |
+
McpServerHttp,
|
| 27 |
+
McpServerSse,
|
| 28 |
+
McpServerStdio,
|
| 29 |
+
NewSessionResponse,
|
| 30 |
+
PromptResponse,
|
| 31 |
+
ResumeSessionResponse,
|
| 32 |
+
SetSessionConfigOptionResponse,
|
| 33 |
+
SetSessionModelResponse,
|
| 34 |
+
SetSessionModeResponse,
|
| 35 |
+
ResourceContentBlock,
|
| 36 |
+
SessionCapabilities,
|
| 37 |
+
SessionForkCapabilities,
|
| 38 |
+
SessionListCapabilities,
|
| 39 |
+
SessionResumeCapabilities,
|
| 40 |
+
SessionInfo,
|
| 41 |
+
TextContentBlock,
|
| 42 |
+
UnstructuredCommandInput,
|
| 43 |
+
Usage,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# AuthMethodAgent was renamed from AuthMethod in agent-client-protocol 0.9.0
|
| 47 |
+
try:
|
| 48 |
+
from acp.schema import AuthMethodAgent
|
| 49 |
+
except ImportError:
|
| 50 |
+
from acp.schema import AuthMethod as AuthMethodAgent # type: ignore[attr-defined]
|
| 51 |
+
|
| 52 |
+
from acp_adapter.auth import detect_provider, has_provider
|
| 53 |
+
from acp_adapter.events import (
|
| 54 |
+
make_message_cb,
|
| 55 |
+
make_step_cb,
|
| 56 |
+
make_thinking_cb,
|
| 57 |
+
make_tool_progress_cb,
|
| 58 |
+
)
|
| 59 |
+
from acp_adapter.permissions import make_approval_callback
|
| 60 |
+
from acp_adapter.session import SessionManager, SessionState
|
| 61 |
+
|
| 62 |
+
logger = logging.getLogger(__name__)
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
from hermes_cli import __version__ as HERMES_VERSION
|
| 66 |
+
except Exception:
|
| 67 |
+
HERMES_VERSION = "0.0.0"
|
| 68 |
+
|
| 69 |
+
# Thread pool for running AIAgent (synchronous) in parallel.
|
| 70 |
+
_executor = ThreadPoolExecutor(max_workers=4, thread_name_prefix="acp-agent")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def _extract_text(
|
| 74 |
+
prompt: list[
|
| 75 |
+
TextContentBlock
|
| 76 |
+
| ImageContentBlock
|
| 77 |
+
| AudioContentBlock
|
| 78 |
+
| ResourceContentBlock
|
| 79 |
+
| EmbeddedResourceContentBlock
|
| 80 |
+
],
|
| 81 |
+
) -> str:
|
| 82 |
+
"""Extract plain text from ACP content blocks."""
|
| 83 |
+
parts: list[str] = []
|
| 84 |
+
for block in prompt:
|
| 85 |
+
if isinstance(block, TextContentBlock):
|
| 86 |
+
parts.append(block.text)
|
| 87 |
+
elif hasattr(block, "text"):
|
| 88 |
+
parts.append(str(block.text))
|
| 89 |
+
# Non-text blocks are ignored for now.
|
| 90 |
+
return "\n".join(parts)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
class HermesACPAgent(acp.Agent):
|
| 94 |
+
"""ACP Agent implementation wrapping Hermes AIAgent."""
|
| 95 |
+
|
| 96 |
+
_SLASH_COMMANDS = {
|
| 97 |
+
"help": "Show available commands",
|
| 98 |
+
"model": "Show or change current model",
|
| 99 |
+
"tools": "List available tools",
|
| 100 |
+
"context": "Show conversation context info",
|
| 101 |
+
"reset": "Clear conversation history",
|
| 102 |
+
"compact": "Compress conversation context",
|
| 103 |
+
"version": "Show Hermes version",
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
_ADVERTISED_COMMANDS = (
|
| 107 |
+
{
|
| 108 |
+
"name": "help",
|
| 109 |
+
"description": "List available commands",
|
| 110 |
+
},
|
| 111 |
+
{
|
| 112 |
+
"name": "model",
|
| 113 |
+
"description": "Show current model and provider, or switch models",
|
| 114 |
+
"input_hint": "model name to switch to",
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"name": "tools",
|
| 118 |
+
"description": "List available tools with descriptions",
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"name": "context",
|
| 122 |
+
"description": "Show conversation message counts by role",
|
| 123 |
+
},
|
| 124 |
+
{
|
| 125 |
+
"name": "reset",
|
| 126 |
+
"description": "Clear conversation history",
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
"name": "compact",
|
| 130 |
+
"description": "Compress conversation context",
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"name": "version",
|
| 134 |
+
"description": "Show Hermes version",
|
| 135 |
+
},
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
def __init__(self, session_manager: SessionManager | None = None):
|
| 139 |
+
super().__init__()
|
| 140 |
+
self.session_manager = session_manager or SessionManager()
|
| 141 |
+
self._conn: Optional[acp.Client] = None
|
| 142 |
+
|
| 143 |
+
# ---- Connection lifecycle -----------------------------------------------
|
| 144 |
+
|
| 145 |
+
def on_connect(self, conn: acp.Client) -> None:
|
| 146 |
+
"""Store the client connection for sending session updates."""
|
| 147 |
+
self._conn = conn
|
| 148 |
+
logger.info("ACP client connected")
|
| 149 |
+
|
| 150 |
+
async def _register_session_mcp_servers(
|
| 151 |
+
self,
|
| 152 |
+
state: SessionState,
|
| 153 |
+
mcp_servers: list[McpServerStdio | McpServerHttp | McpServerSse] | None,
|
| 154 |
+
) -> None:
|
| 155 |
+
"""Register ACP-provided MCP servers and refresh the agent tool surface."""
|
| 156 |
+
if not mcp_servers:
|
| 157 |
+
return
|
| 158 |
+
|
| 159 |
+
try:
|
| 160 |
+
from tools.mcp_tool import register_mcp_servers
|
| 161 |
+
|
| 162 |
+
config_map: dict[str, dict] = {}
|
| 163 |
+
for server in mcp_servers:
|
| 164 |
+
name = server.name
|
| 165 |
+
if isinstance(server, McpServerStdio):
|
| 166 |
+
config = {
|
| 167 |
+
"command": server.command,
|
| 168 |
+
"args": list(server.args),
|
| 169 |
+
"env": {item.name: item.value for item in server.env},
|
| 170 |
+
}
|
| 171 |
+
else:
|
| 172 |
+
config = {
|
| 173 |
+
"url": server.url,
|
| 174 |
+
"headers": {item.name: item.value for item in server.headers},
|
| 175 |
+
}
|
| 176 |
+
config_map[name] = config
|
| 177 |
+
|
| 178 |
+
await asyncio.to_thread(register_mcp_servers, config_map)
|
| 179 |
+
except Exception:
|
| 180 |
+
logger.warning(
|
| 181 |
+
"Session %s: failed to register ACP MCP servers",
|
| 182 |
+
state.session_id,
|
| 183 |
+
exc_info=True,
|
| 184 |
+
)
|
| 185 |
+
return
|
| 186 |
+
|
| 187 |
+
try:
|
| 188 |
+
from model_tools import get_tool_definitions
|
| 189 |
+
|
| 190 |
+
enabled_toolsets = getattr(state.agent, "enabled_toolsets", None) or ["hermes-acp"]
|
| 191 |
+
disabled_toolsets = getattr(state.agent, "disabled_toolsets", None)
|
| 192 |
+
state.agent.tools = get_tool_definitions(
|
| 193 |
+
enabled_toolsets=enabled_toolsets,
|
| 194 |
+
disabled_toolsets=disabled_toolsets,
|
| 195 |
+
quiet_mode=True,
|
| 196 |
+
)
|
| 197 |
+
state.agent.valid_tool_names = {
|
| 198 |
+
tool["function"]["name"] for tool in state.agent.tools or []
|
| 199 |
+
}
|
| 200 |
+
invalidate = getattr(state.agent, "_invalidate_system_prompt", None)
|
| 201 |
+
if callable(invalidate):
|
| 202 |
+
invalidate()
|
| 203 |
+
logger.info(
|
| 204 |
+
"Session %s: refreshed tool surface after ACP MCP registration (%d tools)",
|
| 205 |
+
state.session_id,
|
| 206 |
+
len(state.agent.tools or []),
|
| 207 |
+
)
|
| 208 |
+
except Exception:
|
| 209 |
+
logger.warning(
|
| 210 |
+
"Session %s: failed to refresh tool surface after ACP MCP registration",
|
| 211 |
+
state.session_id,
|
| 212 |
+
exc_info=True,
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# ---- ACP lifecycle ------------------------------------------------------
|
| 216 |
+
|
| 217 |
+
async def initialize(
|
| 218 |
+
self,
|
| 219 |
+
protocol_version: int | None = None,
|
| 220 |
+
client_capabilities: ClientCapabilities | None = None,
|
| 221 |
+
client_info: Implementation | None = None,
|
| 222 |
+
**kwargs: Any,
|
| 223 |
+
) -> InitializeResponse:
|
| 224 |
+
resolved_protocol_version = (
|
| 225 |
+
protocol_version if isinstance(protocol_version, int) else acp.PROTOCOL_VERSION
|
| 226 |
+
)
|
| 227 |
+
provider = detect_provider()
|
| 228 |
+
auth_methods = None
|
| 229 |
+
if provider:
|
| 230 |
+
auth_methods = [
|
| 231 |
+
AuthMethodAgent(
|
| 232 |
+
id=provider,
|
| 233 |
+
name=f"{provider} runtime credentials",
|
| 234 |
+
description=f"Authenticate Hermes using the currently configured {provider} runtime credentials.",
|
| 235 |
+
)
|
| 236 |
+
]
|
| 237 |
+
|
| 238 |
+
client_name = client_info.name if client_info else "unknown"
|
| 239 |
+
logger.info(
|
| 240 |
+
"Initialize from %s (protocol v%s)",
|
| 241 |
+
client_name,
|
| 242 |
+
resolved_protocol_version,
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
return InitializeResponse(
|
| 246 |
+
protocol_version=acp.PROTOCOL_VERSION,
|
| 247 |
+
agent_info=Implementation(name="hermes-agent", version=HERMES_VERSION),
|
| 248 |
+
agent_capabilities=AgentCapabilities(
|
| 249 |
+
load_session=True,
|
| 250 |
+
session_capabilities=SessionCapabilities(
|
| 251 |
+
fork=SessionForkCapabilities(),
|
| 252 |
+
list=SessionListCapabilities(),
|
| 253 |
+
resume=SessionResumeCapabilities(),
|
| 254 |
+
),
|
| 255 |
+
),
|
| 256 |
+
auth_methods=auth_methods,
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
async def authenticate(self, method_id: str, **kwargs: Any) -> AuthenticateResponse | None:
|
| 260 |
+
if has_provider():
|
| 261 |
+
return AuthenticateResponse()
|
| 262 |
+
return None
|
| 263 |
+
|
| 264 |
+
# ---- Session management -------------------------------------------------
|
| 265 |
+
|
| 266 |
+
async def new_session(
|
| 267 |
+
self,
|
| 268 |
+
cwd: str,
|
| 269 |
+
mcp_servers: list | None = None,
|
| 270 |
+
**kwargs: Any,
|
| 271 |
+
) -> NewSessionResponse:
|
| 272 |
+
state = self.session_manager.create_session(cwd=cwd)
|
| 273 |
+
await self._register_session_mcp_servers(state, mcp_servers)
|
| 274 |
+
logger.info("New session %s (cwd=%s)", state.session_id, cwd)
|
| 275 |
+
self._schedule_available_commands_update(state.session_id)
|
| 276 |
+
return NewSessionResponse(session_id=state.session_id)
|
| 277 |
+
|
| 278 |
+
async def load_session(
|
| 279 |
+
self,
|
| 280 |
+
cwd: str,
|
| 281 |
+
session_id: str,
|
| 282 |
+
mcp_servers: list | None = None,
|
| 283 |
+
**kwargs: Any,
|
| 284 |
+
) -> LoadSessionResponse | None:
|
| 285 |
+
state = self.session_manager.update_cwd(session_id, cwd)
|
| 286 |
+
if state is None:
|
| 287 |
+
logger.warning("load_session: session %s not found", session_id)
|
| 288 |
+
return None
|
| 289 |
+
await self._register_session_mcp_servers(state, mcp_servers)
|
| 290 |
+
logger.info("Loaded session %s", session_id)
|
| 291 |
+
self._schedule_available_commands_update(session_id)
|
| 292 |
+
return LoadSessionResponse()
|
| 293 |
+
|
| 294 |
+
async def resume_session(
|
| 295 |
+
self,
|
| 296 |
+
cwd: str,
|
| 297 |
+
session_id: str,
|
| 298 |
+
mcp_servers: list | None = None,
|
| 299 |
+
**kwargs: Any,
|
| 300 |
+
) -> ResumeSessionResponse:
|
| 301 |
+
state = self.session_manager.update_cwd(session_id, cwd)
|
| 302 |
+
if state is None:
|
| 303 |
+
logger.warning("resume_session: session %s not found, creating new", session_id)
|
| 304 |
+
state = self.session_manager.create_session(cwd=cwd)
|
| 305 |
+
await self._register_session_mcp_servers(state, mcp_servers)
|
| 306 |
+
logger.info("Resumed session %s", state.session_id)
|
| 307 |
+
self._schedule_available_commands_update(state.session_id)
|
| 308 |
+
return ResumeSessionResponse()
|
| 309 |
+
|
| 310 |
+
async def cancel(self, session_id: str, **kwargs: Any) -> None:
|
| 311 |
+
state = self.session_manager.get_session(session_id)
|
| 312 |
+
if state and state.cancel_event:
|
| 313 |
+
state.cancel_event.set()
|
| 314 |
+
try:
|
| 315 |
+
if getattr(state, "agent", None) and hasattr(state.agent, "interrupt"):
|
| 316 |
+
state.agent.interrupt()
|
| 317 |
+
except Exception:
|
| 318 |
+
logger.debug("Failed to interrupt ACP session %s", session_id, exc_info=True)
|
| 319 |
+
logger.info("Cancelled session %s", session_id)
|
| 320 |
+
|
| 321 |
+
async def fork_session(
|
| 322 |
+
self,
|
| 323 |
+
cwd: str,
|
| 324 |
+
session_id: str,
|
| 325 |
+
mcp_servers: list | None = None,
|
| 326 |
+
**kwargs: Any,
|
| 327 |
+
) -> ForkSessionResponse:
|
| 328 |
+
state = self.session_manager.fork_session(session_id, cwd=cwd)
|
| 329 |
+
new_id = state.session_id if state else ""
|
| 330 |
+
if state is not None:
|
| 331 |
+
await self._register_session_mcp_servers(state, mcp_servers)
|
| 332 |
+
logger.info("Forked session %s -> %s", session_id, new_id)
|
| 333 |
+
if new_id:
|
| 334 |
+
self._schedule_available_commands_update(new_id)
|
| 335 |
+
return ForkSessionResponse(session_id=new_id)
|
| 336 |
+
|
| 337 |
+
async def list_sessions(
|
| 338 |
+
self,
|
| 339 |
+
cursor: str | None = None,
|
| 340 |
+
cwd: str | None = None,
|
| 341 |
+
**kwargs: Any,
|
| 342 |
+
) -> ListSessionsResponse:
|
| 343 |
+
infos = self.session_manager.list_sessions()
|
| 344 |
+
sessions = [
|
| 345 |
+
SessionInfo(session_id=s["session_id"], cwd=s["cwd"])
|
| 346 |
+
for s in infos
|
| 347 |
+
]
|
| 348 |
+
return ListSessionsResponse(sessions=sessions)
|
| 349 |
+
|
| 350 |
+
# ---- Prompt (core) ------------------------------------------------------
|
| 351 |
+
|
| 352 |
+
async def prompt(
|
| 353 |
+
self,
|
| 354 |
+
prompt: list[
|
| 355 |
+
TextContentBlock
|
| 356 |
+
| ImageContentBlock
|
| 357 |
+
| AudioContentBlock
|
| 358 |
+
| ResourceContentBlock
|
| 359 |
+
| EmbeddedResourceContentBlock
|
| 360 |
+
],
|
| 361 |
+
session_id: str,
|
| 362 |
+
**kwargs: Any,
|
| 363 |
+
) -> PromptResponse:
|
| 364 |
+
"""Run Hermes on the user's prompt and stream events back to the editor."""
|
| 365 |
+
state = self.session_manager.get_session(session_id)
|
| 366 |
+
if state is None:
|
| 367 |
+
logger.error("prompt: session %s not found", session_id)
|
| 368 |
+
return PromptResponse(stop_reason="refusal")
|
| 369 |
+
|
| 370 |
+
user_text = _extract_text(prompt).strip()
|
| 371 |
+
if not user_text:
|
| 372 |
+
return PromptResponse(stop_reason="end_turn")
|
| 373 |
+
|
| 374 |
+
# Intercept slash commands — handle locally without calling the LLM
|
| 375 |
+
if user_text.startswith("/"):
|
| 376 |
+
response_text = self._handle_slash_command(user_text, state)
|
| 377 |
+
if response_text is not None:
|
| 378 |
+
if self._conn:
|
| 379 |
+
update = acp.update_agent_message_text(response_text)
|
| 380 |
+
await self._conn.session_update(session_id, update)
|
| 381 |
+
return PromptResponse(stop_reason="end_turn")
|
| 382 |
+
|
| 383 |
+
logger.info("Prompt on session %s: %s", session_id, user_text[:100])
|
| 384 |
+
|
| 385 |
+
conn = self._conn
|
| 386 |
+
loop = asyncio.get_running_loop()
|
| 387 |
+
|
| 388 |
+
if state.cancel_event:
|
| 389 |
+
state.cancel_event.clear()
|
| 390 |
+
|
| 391 |
+
tool_call_ids: dict[str, Deque[str]] = defaultdict(deque)
|
| 392 |
+
previous_approval_cb = None
|
| 393 |
+
|
| 394 |
+
if conn:
|
| 395 |
+
tool_progress_cb = make_tool_progress_cb(conn, session_id, loop, tool_call_ids)
|
| 396 |
+
thinking_cb = make_thinking_cb(conn, session_id, loop)
|
| 397 |
+
step_cb = make_step_cb(conn, session_id, loop, tool_call_ids)
|
| 398 |
+
message_cb = make_message_cb(conn, session_id, loop)
|
| 399 |
+
approval_cb = make_approval_callback(conn.request_permission, loop, session_id)
|
| 400 |
+
else:
|
| 401 |
+
tool_progress_cb = None
|
| 402 |
+
thinking_cb = None
|
| 403 |
+
step_cb = None
|
| 404 |
+
message_cb = None
|
| 405 |
+
approval_cb = None
|
| 406 |
+
|
| 407 |
+
agent = state.agent
|
| 408 |
+
agent.tool_progress_callback = tool_progress_cb
|
| 409 |
+
agent.thinking_callback = thinking_cb
|
| 410 |
+
agent.step_callback = step_cb
|
| 411 |
+
agent.message_callback = message_cb
|
| 412 |
+
|
| 413 |
+
if approval_cb:
|
| 414 |
+
try:
|
| 415 |
+
from tools import terminal_tool as _terminal_tool
|
| 416 |
+
previous_approval_cb = getattr(_terminal_tool, "_approval_callback", None)
|
| 417 |
+
_terminal_tool.set_approval_callback(approval_cb)
|
| 418 |
+
except Exception:
|
| 419 |
+
logger.debug("Could not set ACP approval callback", exc_info=True)
|
| 420 |
+
|
| 421 |
+
def _run_agent() -> dict:
|
| 422 |
+
try:
|
| 423 |
+
result = agent.run_conversation(
|
| 424 |
+
user_message=user_text,
|
| 425 |
+
conversation_history=state.history,
|
| 426 |
+
task_id=session_id,
|
| 427 |
+
)
|
| 428 |
+
return result
|
| 429 |
+
except Exception as e:
|
| 430 |
+
logger.exception("Agent error in session %s", session_id)
|
| 431 |
+
return {"final_response": f"Error: {e}", "messages": state.history}
|
| 432 |
+
finally:
|
| 433 |
+
if approval_cb:
|
| 434 |
+
try:
|
| 435 |
+
from tools import terminal_tool as _terminal_tool
|
| 436 |
+
_terminal_tool.set_approval_callback(previous_approval_cb)
|
| 437 |
+
except Exception:
|
| 438 |
+
logger.debug("Could not restore approval callback", exc_info=True)
|
| 439 |
+
|
| 440 |
+
try:
|
| 441 |
+
result = await loop.run_in_executor(_executor, _run_agent)
|
| 442 |
+
except Exception:
|
| 443 |
+
logger.exception("Executor error for session %s", session_id)
|
| 444 |
+
return PromptResponse(stop_reason="end_turn")
|
| 445 |
+
|
| 446 |
+
if result.get("messages"):
|
| 447 |
+
state.history = result["messages"]
|
| 448 |
+
# Persist updated history so sessions survive process restarts.
|
| 449 |
+
self.session_manager.save_session(session_id)
|
| 450 |
+
|
| 451 |
+
final_response = result.get("final_response", "")
|
| 452 |
+
if final_response and conn:
|
| 453 |
+
update = acp.update_agent_message_text(final_response)
|
| 454 |
+
await conn.session_update(session_id, update)
|
| 455 |
+
|
| 456 |
+
usage = None
|
| 457 |
+
if any(result.get(key) is not None for key in ("prompt_tokens", "completion_tokens", "total_tokens")):
|
| 458 |
+
usage = Usage(
|
| 459 |
+
input_tokens=result.get("prompt_tokens", 0),
|
| 460 |
+
output_tokens=result.get("completion_tokens", 0),
|
| 461 |
+
total_tokens=result.get("total_tokens", 0),
|
| 462 |
+
thought_tokens=result.get("reasoning_tokens"),
|
| 463 |
+
cached_read_tokens=result.get("cache_read_tokens"),
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
stop_reason = "cancelled" if state.cancel_event and state.cancel_event.is_set() else "end_turn"
|
| 467 |
+
return PromptResponse(stop_reason=stop_reason, usage=usage)
|
| 468 |
+
|
| 469 |
+
# ---- Slash commands (headless) -------------------------------------------
|
| 470 |
+
|
| 471 |
+
@classmethod
|
| 472 |
+
def _available_commands(cls) -> list[AvailableCommand]:
|
| 473 |
+
commands: list[AvailableCommand] = []
|
| 474 |
+
for spec in cls._ADVERTISED_COMMANDS:
|
| 475 |
+
input_hint = spec.get("input_hint")
|
| 476 |
+
commands.append(
|
| 477 |
+
AvailableCommand(
|
| 478 |
+
name=spec["name"],
|
| 479 |
+
description=spec["description"],
|
| 480 |
+
input=UnstructuredCommandInput(hint=input_hint)
|
| 481 |
+
if input_hint
|
| 482 |
+
else None,
|
| 483 |
+
)
|
| 484 |
+
)
|
| 485 |
+
return commands
|
| 486 |
+
|
| 487 |
+
async def _send_available_commands_update(self, session_id: str) -> None:
|
| 488 |
+
"""Advertise supported slash commands to the connected ACP client."""
|
| 489 |
+
if not self._conn:
|
| 490 |
+
return
|
| 491 |
+
|
| 492 |
+
try:
|
| 493 |
+
await self._conn.session_update(
|
| 494 |
+
session_id=session_id,
|
| 495 |
+
update=AvailableCommandsUpdate(
|
| 496 |
+
sessionUpdate="available_commands_update",
|
| 497 |
+
availableCommands=self._available_commands(),
|
| 498 |
+
),
|
| 499 |
+
)
|
| 500 |
+
except Exception:
|
| 501 |
+
logger.warning(
|
| 502 |
+
"Failed to advertise ACP slash commands for session %s",
|
| 503 |
+
session_id,
|
| 504 |
+
exc_info=True,
|
| 505 |
+
)
|
| 506 |
+
|
| 507 |
+
def _schedule_available_commands_update(self, session_id: str) -> None:
|
| 508 |
+
"""Send the command advertisement after the session response is queued."""
|
| 509 |
+
if not self._conn:
|
| 510 |
+
return
|
| 511 |
+
loop = asyncio.get_running_loop()
|
| 512 |
+
loop.call_soon(
|
| 513 |
+
asyncio.create_task, self._send_available_commands_update(session_id)
|
| 514 |
+
)
|
| 515 |
+
|
| 516 |
+
def _handle_slash_command(self, text: str, state: SessionState) -> str | None:
|
| 517 |
+
"""Dispatch a slash command and return the response text.
|
| 518 |
+
|
| 519 |
+
Returns ``None`` for unrecognized commands so they fall through
|
| 520 |
+
to the LLM (the user may have typed ``/something`` as prose).
|
| 521 |
+
"""
|
| 522 |
+
parts = text.split(maxsplit=1)
|
| 523 |
+
cmd = parts[0].lstrip("/").lower()
|
| 524 |
+
args = parts[1].strip() if len(parts) > 1 else ""
|
| 525 |
+
|
| 526 |
+
handler = {
|
| 527 |
+
"help": self._cmd_help,
|
| 528 |
+
"model": self._cmd_model,
|
| 529 |
+
"tools": self._cmd_tools,
|
| 530 |
+
"context": self._cmd_context,
|
| 531 |
+
"reset": self._cmd_reset,
|
| 532 |
+
"compact": self._cmd_compact,
|
| 533 |
+
"version": self._cmd_version,
|
| 534 |
+
}.get(cmd)
|
| 535 |
+
|
| 536 |
+
if handler is None:
|
| 537 |
+
return None # not a known command — let the LLM handle it
|
| 538 |
+
|
| 539 |
+
try:
|
| 540 |
+
return handler(args, state)
|
| 541 |
+
except Exception as e:
|
| 542 |
+
logger.error("Slash command /%s error: %s", cmd, e, exc_info=True)
|
| 543 |
+
return f"Error executing /{cmd}: {e}"
|
| 544 |
+
|
| 545 |
+
def _cmd_help(self, args: str, state: SessionState) -> str:
|
| 546 |
+
lines = ["Available commands:", ""]
|
| 547 |
+
for cmd, desc in self._SLASH_COMMANDS.items():
|
| 548 |
+
lines.append(f" /{cmd:10s} {desc}")
|
| 549 |
+
lines.append("")
|
| 550 |
+
lines.append("Unrecognized /commands are sent to the model as normal messages.")
|
| 551 |
+
return "\n".join(lines)
|
| 552 |
+
|
| 553 |
+
def _cmd_model(self, args: str, state: SessionState) -> str:
|
| 554 |
+
if not args:
|
| 555 |
+
model = state.model or getattr(state.agent, "model", "unknown")
|
| 556 |
+
provider = getattr(state.agent, "provider", None) or "auto"
|
| 557 |
+
return f"Current model: {model}\nProvider: {provider}"
|
| 558 |
+
|
| 559 |
+
new_model = args.strip()
|
| 560 |
+
target_provider = None
|
| 561 |
+
current_provider = getattr(state.agent, "provider", None) or "openrouter"
|
| 562 |
+
|
| 563 |
+
# Auto-detect provider for the requested model
|
| 564 |
+
try:
|
| 565 |
+
from hermes_cli.models import parse_model_input, detect_provider_for_model
|
| 566 |
+
target_provider, new_model = parse_model_input(new_model, current_provider)
|
| 567 |
+
if target_provider == current_provider:
|
| 568 |
+
detected = detect_provider_for_model(new_model, current_provider)
|
| 569 |
+
if detected:
|
| 570 |
+
target_provider, new_model = detected
|
| 571 |
+
except Exception:
|
| 572 |
+
logger.debug("Provider detection failed, using model as-is", exc_info=True)
|
| 573 |
+
|
| 574 |
+
state.model = new_model
|
| 575 |
+
state.agent = self.session_manager._make_agent(
|
| 576 |
+
session_id=state.session_id,
|
| 577 |
+
cwd=state.cwd,
|
| 578 |
+
model=new_model,
|
| 579 |
+
requested_provider=target_provider or current_provider,
|
| 580 |
+
)
|
| 581 |
+
self.session_manager.save_session(state.session_id)
|
| 582 |
+
provider_label = getattr(state.agent, "provider", None) or target_provider or current_provider
|
| 583 |
+
logger.info("Session %s: model switched to %s", state.session_id, new_model)
|
| 584 |
+
return f"Model switched to: {new_model}\nProvider: {provider_label}"
|
| 585 |
+
|
| 586 |
+
def _cmd_tools(self, args: str, state: SessionState) -> str:
|
| 587 |
+
try:
|
| 588 |
+
from model_tools import get_tool_definitions
|
| 589 |
+
toolsets = getattr(state.agent, "enabled_toolsets", None) or ["hermes-acp"]
|
| 590 |
+
tools = get_tool_definitions(enabled_toolsets=toolsets, quiet_mode=True)
|
| 591 |
+
if not tools:
|
| 592 |
+
return "No tools available."
|
| 593 |
+
lines = [f"Available tools ({len(tools)}):"]
|
| 594 |
+
for t in tools:
|
| 595 |
+
name = t.get("function", {}).get("name", "?")
|
| 596 |
+
desc = t.get("function", {}).get("description", "")
|
| 597 |
+
# Truncate long descriptions
|
| 598 |
+
if len(desc) > 80:
|
| 599 |
+
desc = desc[:77] + "..."
|
| 600 |
+
lines.append(f" {name}: {desc}")
|
| 601 |
+
return "\n".join(lines)
|
| 602 |
+
except Exception as e:
|
| 603 |
+
return f"Could not list tools: {e}"
|
| 604 |
+
|
| 605 |
+
def _cmd_context(self, args: str, state: SessionState) -> str:
|
| 606 |
+
n_messages = len(state.history)
|
| 607 |
+
if n_messages == 0:
|
| 608 |
+
return "Conversation is empty (no messages yet)."
|
| 609 |
+
# Count by role
|
| 610 |
+
roles: dict[str, int] = {}
|
| 611 |
+
for msg in state.history:
|
| 612 |
+
role = msg.get("role", "unknown")
|
| 613 |
+
roles[role] = roles.get(role, 0) + 1
|
| 614 |
+
lines = [
|
| 615 |
+
f"Conversation: {n_messages} messages",
|
| 616 |
+
f" user: {roles.get('user', 0)}, assistant: {roles.get('assistant', 0)}, "
|
| 617 |
+
f"tool: {roles.get('tool', 0)}, system: {roles.get('system', 0)}",
|
| 618 |
+
]
|
| 619 |
+
model = state.model or getattr(state.agent, "model", "")
|
| 620 |
+
if model:
|
| 621 |
+
lines.append(f"Model: {model}")
|
| 622 |
+
return "\n".join(lines)
|
| 623 |
+
|
| 624 |
+
def _cmd_reset(self, args: str, state: SessionState) -> str:
|
| 625 |
+
state.history.clear()
|
| 626 |
+
self.session_manager.save_session(state.session_id)
|
| 627 |
+
return "Conversation history cleared."
|
| 628 |
+
|
| 629 |
+
def _cmd_compact(self, args: str, state: SessionState) -> str:
|
| 630 |
+
if not state.history:
|
| 631 |
+
return "Nothing to compress — conversation is empty."
|
| 632 |
+
try:
|
| 633 |
+
agent = state.agent
|
| 634 |
+
if not getattr(agent, "compression_enabled", True):
|
| 635 |
+
return "Context compression is disabled for this agent."
|
| 636 |
+
if not hasattr(agent, "_compress_context"):
|
| 637 |
+
return "Context compression not available for this agent."
|
| 638 |
+
|
| 639 |
+
from agent.model_metadata import estimate_messages_tokens_rough
|
| 640 |
+
|
| 641 |
+
original_count = len(state.history)
|
| 642 |
+
approx_tokens = estimate_messages_tokens_rough(state.history)
|
| 643 |
+
original_session_db = getattr(agent, "_session_db", None)
|
| 644 |
+
|
| 645 |
+
try:
|
| 646 |
+
# ACP sessions must keep a stable session id, so avoid the
|
| 647 |
+
# SQLite session-splitting side effect inside _compress_context.
|
| 648 |
+
agent._session_db = None
|
| 649 |
+
compressed, _ = agent._compress_context(
|
| 650 |
+
state.history,
|
| 651 |
+
getattr(agent, "_cached_system_prompt", "") or "",
|
| 652 |
+
approx_tokens=approx_tokens,
|
| 653 |
+
task_id=state.session_id,
|
| 654 |
+
)
|
| 655 |
+
finally:
|
| 656 |
+
agent._session_db = original_session_db
|
| 657 |
+
|
| 658 |
+
state.history = compressed
|
| 659 |
+
self.session_manager.save_session(state.session_id)
|
| 660 |
+
|
| 661 |
+
new_count = len(state.history)
|
| 662 |
+
new_tokens = estimate_messages_tokens_rough(state.history)
|
| 663 |
+
return (
|
| 664 |
+
f"Context compressed: {original_count} -> {new_count} messages\n"
|
| 665 |
+
f"~{approx_tokens:,} -> ~{new_tokens:,} tokens"
|
| 666 |
+
)
|
| 667 |
+
except Exception as e:
|
| 668 |
+
return f"Compression failed: {e}"
|
| 669 |
+
|
| 670 |
+
def _cmd_version(self, args: str, state: SessionState) -> str:
|
| 671 |
+
return f"Hermes Agent v{HERMES_VERSION}"
|
| 672 |
+
|
| 673 |
+
# ---- Model switching (ACP protocol method) -------------------------------
|
| 674 |
+
|
| 675 |
+
async def set_session_model(
|
| 676 |
+
self, model_id: str, session_id: str, **kwargs: Any
|
| 677 |
+
) -> SetSessionModelResponse | None:
|
| 678 |
+
"""Switch the model for a session (called by ACP protocol)."""
|
| 679 |
+
state = self.session_manager.get_session(session_id)
|
| 680 |
+
if state:
|
| 681 |
+
state.model = model_id
|
| 682 |
+
current_provider = getattr(state.agent, "provider", None)
|
| 683 |
+
current_base_url = getattr(state.agent, "base_url", None)
|
| 684 |
+
current_api_mode = getattr(state.agent, "api_mode", None)
|
| 685 |
+
state.agent = self.session_manager._make_agent(
|
| 686 |
+
session_id=session_id,
|
| 687 |
+
cwd=state.cwd,
|
| 688 |
+
model=model_id,
|
| 689 |
+
requested_provider=current_provider,
|
| 690 |
+
base_url=current_base_url,
|
| 691 |
+
api_mode=current_api_mode,
|
| 692 |
+
)
|
| 693 |
+
self.session_manager.save_session(session_id)
|
| 694 |
+
logger.info("Session %s: model switched to %s", session_id, model_id)
|
| 695 |
+
return SetSessionModelResponse()
|
| 696 |
+
logger.warning("Session %s: model switch requested for missing session", session_id)
|
| 697 |
+
return None
|
| 698 |
+
|
| 699 |
+
async def set_session_mode(
|
| 700 |
+
self, mode_id: str, session_id: str, **kwargs: Any
|
| 701 |
+
) -> SetSessionModeResponse | None:
|
| 702 |
+
"""Persist the editor-requested mode so ACP clients do not fail on mode switches."""
|
| 703 |
+
state = self.session_manager.get_session(session_id)
|
| 704 |
+
if state is None:
|
| 705 |
+
logger.warning("Session %s: mode switch requested for missing session", session_id)
|
| 706 |
+
return None
|
| 707 |
+
setattr(state, "mode", mode_id)
|
| 708 |
+
self.session_manager.save_session(session_id)
|
| 709 |
+
logger.info("Session %s: mode switched to %s", session_id, mode_id)
|
| 710 |
+
return SetSessionModeResponse()
|
| 711 |
+
|
| 712 |
+
async def set_config_option(
|
| 713 |
+
self, config_id: str, session_id: str, value: str, **kwargs: Any
|
| 714 |
+
) -> SetSessionConfigOptionResponse | None:
|
| 715 |
+
"""Accept ACP config option updates even when Hermes has no typed ACP config surface yet."""
|
| 716 |
+
state = self.session_manager.get_session(session_id)
|
| 717 |
+
if state is None:
|
| 718 |
+
logger.warning("Session %s: config update requested for missing session", session_id)
|
| 719 |
+
return None
|
| 720 |
+
|
| 721 |
+
options = getattr(state, "config_options", None)
|
| 722 |
+
if not isinstance(options, dict):
|
| 723 |
+
options = {}
|
| 724 |
+
options[str(config_id)] = value
|
| 725 |
+
setattr(state, "config_options", options)
|
| 726 |
+
self.session_manager.save_session(session_id)
|
| 727 |
+
logger.info("Session %s: config option %s updated", session_id, config_id)
|
| 728 |
+
return SetSessionConfigOptionResponse(config_options=[])
|
acp_adapter/session.py
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ACP session manager — maps ACP sessions to Hermes AIAgent instances.
|
| 2 |
+
|
| 3 |
+
Sessions are persisted to the shared SessionDB (``~/.hermes/state.db``) so they
|
| 4 |
+
survive process restarts and appear in ``session_search``. When the editor
|
| 5 |
+
reconnects after idle/restart, the ``load_session`` / ``resume_session`` calls
|
| 6 |
+
find the persisted session in the database and restore the full conversation
|
| 7 |
+
history.
|
| 8 |
+
"""
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
from hermes_constants import get_hermes_home
|
| 12 |
+
|
| 13 |
+
import copy
|
| 14 |
+
import json
|
| 15 |
+
import logging
|
| 16 |
+
import sys
|
| 17 |
+
import uuid
|
| 18 |
+
from dataclasses import dataclass, field
|
| 19 |
+
from threading import Lock
|
| 20 |
+
from typing import Any, Dict, List, Optional
|
| 21 |
+
|
| 22 |
+
logger = logging.getLogger(__name__)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _acp_stderr_print(*args, **kwargs) -> None:
|
| 26 |
+
"""Best-effort human-readable output sink for ACP stdio sessions.
|
| 27 |
+
|
| 28 |
+
ACP reserves stdout for JSON-RPC frames, so any incidental CLI/status output
|
| 29 |
+
from AIAgent must be redirected away from stdout. Route it to stderr instead.
|
| 30 |
+
"""
|
| 31 |
+
kwargs = dict(kwargs)
|
| 32 |
+
kwargs.setdefault("file", sys.stderr)
|
| 33 |
+
print(*args, **kwargs)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _register_task_cwd(task_id: str, cwd: str) -> None:
|
| 37 |
+
"""Bind a task/session id to the editor's working directory for tools."""
|
| 38 |
+
if not task_id:
|
| 39 |
+
return
|
| 40 |
+
try:
|
| 41 |
+
from tools.terminal_tool import register_task_env_overrides
|
| 42 |
+
register_task_env_overrides(task_id, {"cwd": cwd})
|
| 43 |
+
except Exception:
|
| 44 |
+
logger.debug("Failed to register ACP task cwd override", exc_info=True)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _clear_task_cwd(task_id: str) -> None:
|
| 48 |
+
"""Remove task-specific cwd overrides for an ACP session."""
|
| 49 |
+
if not task_id:
|
| 50 |
+
return
|
| 51 |
+
try:
|
| 52 |
+
from tools.terminal_tool import clear_task_env_overrides
|
| 53 |
+
clear_task_env_overrides(task_id)
|
| 54 |
+
except Exception:
|
| 55 |
+
logger.debug("Failed to clear ACP task cwd override", exc_info=True)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@dataclass
|
| 59 |
+
class SessionState:
|
| 60 |
+
"""Tracks per-session state for an ACP-managed Hermes agent."""
|
| 61 |
+
|
| 62 |
+
session_id: str
|
| 63 |
+
agent: Any # AIAgent instance
|
| 64 |
+
cwd: str = "."
|
| 65 |
+
model: str = ""
|
| 66 |
+
history: List[Dict[str, Any]] = field(default_factory=list)
|
| 67 |
+
cancel_event: Any = None # threading.Event
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class SessionManager:
|
| 71 |
+
"""Thread-safe manager for ACP sessions backed by Hermes AIAgent instances.
|
| 72 |
+
|
| 73 |
+
Sessions are held in-memory for fast access **and** persisted to the
|
| 74 |
+
shared SessionDB so they survive process restarts and are searchable
|
| 75 |
+
via ``session_search``.
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
def __init__(self, agent_factory=None, db=None):
|
| 79 |
+
"""
|
| 80 |
+
Args:
|
| 81 |
+
agent_factory: Optional callable that creates an AIAgent-like object.
|
| 82 |
+
Used by tests. When omitted, a real AIAgent is created
|
| 83 |
+
using the current Hermes runtime provider configuration.
|
| 84 |
+
db: Optional SessionDB instance. When omitted, the default
|
| 85 |
+
SessionDB (``~/.hermes/state.db``) is lazily created.
|
| 86 |
+
"""
|
| 87 |
+
self._sessions: Dict[str, SessionState] = {}
|
| 88 |
+
self._lock = Lock()
|
| 89 |
+
self._agent_factory = agent_factory
|
| 90 |
+
self._db_instance = db # None → lazy-init on first use
|
| 91 |
+
|
| 92 |
+
# ---- public API ---------------------------------------------------------
|
| 93 |
+
|
| 94 |
+
def create_session(self, cwd: str = ".") -> SessionState:
|
| 95 |
+
"""Create a new session with a unique ID and a fresh AIAgent."""
|
| 96 |
+
import threading
|
| 97 |
+
|
| 98 |
+
session_id = str(uuid.uuid4())
|
| 99 |
+
agent = self._make_agent(session_id=session_id, cwd=cwd)
|
| 100 |
+
state = SessionState(
|
| 101 |
+
session_id=session_id,
|
| 102 |
+
agent=agent,
|
| 103 |
+
cwd=cwd,
|
| 104 |
+
model=getattr(agent, "model", "") or "",
|
| 105 |
+
cancel_event=threading.Event(),
|
| 106 |
+
)
|
| 107 |
+
with self._lock:
|
| 108 |
+
self._sessions[session_id] = state
|
| 109 |
+
_register_task_cwd(session_id, cwd)
|
| 110 |
+
self._persist(state)
|
| 111 |
+
logger.info("Created ACP session %s (cwd=%s)", session_id, cwd)
|
| 112 |
+
return state
|
| 113 |
+
|
| 114 |
+
def get_session(self, session_id: str) -> Optional[SessionState]:
|
| 115 |
+
"""Return the session for *session_id*, or ``None``.
|
| 116 |
+
|
| 117 |
+
If the session is not in memory but exists in the database (e.g. after
|
| 118 |
+
a process restart), it is transparently restored.
|
| 119 |
+
"""
|
| 120 |
+
with self._lock:
|
| 121 |
+
state = self._sessions.get(session_id)
|
| 122 |
+
if state is not None:
|
| 123 |
+
return state
|
| 124 |
+
# Attempt to restore from database.
|
| 125 |
+
return self._restore(session_id)
|
| 126 |
+
|
| 127 |
+
def remove_session(self, session_id: str) -> bool:
|
| 128 |
+
"""Remove a session from memory and database. Returns True if it existed."""
|
| 129 |
+
with self._lock:
|
| 130 |
+
existed = self._sessions.pop(session_id, None) is not None
|
| 131 |
+
db_existed = self._delete_persisted(session_id)
|
| 132 |
+
if existed or db_existed:
|
| 133 |
+
_clear_task_cwd(session_id)
|
| 134 |
+
return existed or db_existed
|
| 135 |
+
|
| 136 |
+
def fork_session(self, session_id: str, cwd: str = ".") -> Optional[SessionState]:
|
| 137 |
+
"""Deep-copy a session's history into a new session."""
|
| 138 |
+
import threading
|
| 139 |
+
|
| 140 |
+
original = self.get_session(session_id) # checks DB too
|
| 141 |
+
if original is None:
|
| 142 |
+
return None
|
| 143 |
+
|
| 144 |
+
new_id = str(uuid.uuid4())
|
| 145 |
+
agent = self._make_agent(
|
| 146 |
+
session_id=new_id,
|
| 147 |
+
cwd=cwd,
|
| 148 |
+
model=original.model or None,
|
| 149 |
+
)
|
| 150 |
+
state = SessionState(
|
| 151 |
+
session_id=new_id,
|
| 152 |
+
agent=agent,
|
| 153 |
+
cwd=cwd,
|
| 154 |
+
model=getattr(agent, "model", original.model) or original.model,
|
| 155 |
+
history=copy.deepcopy(original.history),
|
| 156 |
+
cancel_event=threading.Event(),
|
| 157 |
+
)
|
| 158 |
+
with self._lock:
|
| 159 |
+
self._sessions[new_id] = state
|
| 160 |
+
_register_task_cwd(new_id, cwd)
|
| 161 |
+
self._persist(state)
|
| 162 |
+
logger.info("Forked ACP session %s -> %s", session_id, new_id)
|
| 163 |
+
return state
|
| 164 |
+
|
| 165 |
+
def list_sessions(self) -> List[Dict[str, Any]]:
|
| 166 |
+
"""Return lightweight info dicts for all sessions (memory + database)."""
|
| 167 |
+
# Collect in-memory sessions first.
|
| 168 |
+
with self._lock:
|
| 169 |
+
seen_ids = set(self._sessions.keys())
|
| 170 |
+
results = [
|
| 171 |
+
{
|
| 172 |
+
"session_id": s.session_id,
|
| 173 |
+
"cwd": s.cwd,
|
| 174 |
+
"model": s.model,
|
| 175 |
+
"history_len": len(s.history),
|
| 176 |
+
}
|
| 177 |
+
for s in self._sessions.values()
|
| 178 |
+
]
|
| 179 |
+
|
| 180 |
+
# Merge any persisted sessions not currently in memory.
|
| 181 |
+
db = self._get_db()
|
| 182 |
+
if db is not None:
|
| 183 |
+
try:
|
| 184 |
+
rows = db.search_sessions(source="acp", limit=1000)
|
| 185 |
+
for row in rows:
|
| 186 |
+
sid = row["id"]
|
| 187 |
+
if sid in seen_ids:
|
| 188 |
+
continue
|
| 189 |
+
# Extract cwd from model_config JSON.
|
| 190 |
+
cwd = "."
|
| 191 |
+
mc = row.get("model_config")
|
| 192 |
+
if mc:
|
| 193 |
+
try:
|
| 194 |
+
cwd = json.loads(mc).get("cwd", ".")
|
| 195 |
+
except (json.JSONDecodeError, TypeError):
|
| 196 |
+
pass
|
| 197 |
+
results.append({
|
| 198 |
+
"session_id": sid,
|
| 199 |
+
"cwd": cwd,
|
| 200 |
+
"model": row.get("model") or "",
|
| 201 |
+
"history_len": row.get("message_count") or 0,
|
| 202 |
+
})
|
| 203 |
+
except Exception:
|
| 204 |
+
logger.debug("Failed to list ACP sessions from DB", exc_info=True)
|
| 205 |
+
|
| 206 |
+
return results
|
| 207 |
+
|
| 208 |
+
def update_cwd(self, session_id: str, cwd: str) -> Optional[SessionState]:
|
| 209 |
+
"""Update the working directory for a session and its tool overrides."""
|
| 210 |
+
state = self.get_session(session_id) # checks DB too
|
| 211 |
+
if state is None:
|
| 212 |
+
return None
|
| 213 |
+
state.cwd = cwd
|
| 214 |
+
_register_task_cwd(session_id, cwd)
|
| 215 |
+
self._persist(state)
|
| 216 |
+
return state
|
| 217 |
+
|
| 218 |
+
def cleanup(self) -> None:
|
| 219 |
+
"""Remove all sessions (memory and database) and clear task-specific cwd overrides."""
|
| 220 |
+
with self._lock:
|
| 221 |
+
session_ids = list(self._sessions.keys())
|
| 222 |
+
self._sessions.clear()
|
| 223 |
+
for session_id in session_ids:
|
| 224 |
+
_clear_task_cwd(session_id)
|
| 225 |
+
self._delete_persisted(session_id)
|
| 226 |
+
# Also remove any DB-only ACP sessions not currently in memory.
|
| 227 |
+
db = self._get_db()
|
| 228 |
+
if db is not None:
|
| 229 |
+
try:
|
| 230 |
+
rows = db.search_sessions(source="acp", limit=10000)
|
| 231 |
+
for row in rows:
|
| 232 |
+
sid = row["id"]
|
| 233 |
+
_clear_task_cwd(sid)
|
| 234 |
+
db.delete_session(sid)
|
| 235 |
+
except Exception:
|
| 236 |
+
logger.debug("Failed to cleanup ACP sessions from DB", exc_info=True)
|
| 237 |
+
|
| 238 |
+
def save_session(self, session_id: str) -> None:
|
| 239 |
+
"""Persist the current state of a session to the database.
|
| 240 |
+
|
| 241 |
+
Called by the server after prompt completion, slash commands that
|
| 242 |
+
mutate history, and model switches.
|
| 243 |
+
"""
|
| 244 |
+
with self._lock:
|
| 245 |
+
state = self._sessions.get(session_id)
|
| 246 |
+
if state is not None:
|
| 247 |
+
self._persist(state)
|
| 248 |
+
|
| 249 |
+
# ---- persistence via SessionDB ------------------------------------------
|
| 250 |
+
|
| 251 |
+
def _get_db(self):
|
| 252 |
+
"""Lazily initialise and return the SessionDB instance.
|
| 253 |
+
|
| 254 |
+
Returns ``None`` if the DB is unavailable (e.g. import error in a
|
| 255 |
+
minimal test environment).
|
| 256 |
+
|
| 257 |
+
Note: we resolve ``HERMES_HOME`` dynamically rather than relying on
|
| 258 |
+
the module-level ``DEFAULT_DB_PATH`` constant, because that constant
|
| 259 |
+
is evaluated at import time and won't reflect env-var changes made
|
| 260 |
+
later (e.g. by the test fixture ``_isolate_hermes_home``).
|
| 261 |
+
"""
|
| 262 |
+
if self._db_instance is not None:
|
| 263 |
+
return self._db_instance
|
| 264 |
+
try:
|
| 265 |
+
from hermes_state import SessionDB
|
| 266 |
+
hermes_home = get_hermes_home()
|
| 267 |
+
self._db_instance = SessionDB(db_path=hermes_home / "state.db")
|
| 268 |
+
return self._db_instance
|
| 269 |
+
except Exception:
|
| 270 |
+
logger.debug("SessionDB unavailable for ACP persistence", exc_info=True)
|
| 271 |
+
return None
|
| 272 |
+
|
| 273 |
+
def _persist(self, state: SessionState) -> None:
|
| 274 |
+
"""Write session state to the database.
|
| 275 |
+
|
| 276 |
+
Creates the session record if it doesn't exist, then replaces all
|
| 277 |
+
stored messages with the current in-memory history.
|
| 278 |
+
"""
|
| 279 |
+
db = self._get_db()
|
| 280 |
+
if db is None:
|
| 281 |
+
return
|
| 282 |
+
|
| 283 |
+
# Ensure model is a plain string (not a MagicMock or other proxy).
|
| 284 |
+
model_str = str(state.model) if state.model else None
|
| 285 |
+
session_meta = {"cwd": state.cwd}
|
| 286 |
+
provider = getattr(state.agent, "provider", None)
|
| 287 |
+
base_url = getattr(state.agent, "base_url", None)
|
| 288 |
+
api_mode = getattr(state.agent, "api_mode", None)
|
| 289 |
+
if isinstance(provider, str) and provider.strip():
|
| 290 |
+
session_meta["provider"] = provider.strip()
|
| 291 |
+
if isinstance(base_url, str) and base_url.strip():
|
| 292 |
+
session_meta["base_url"] = base_url.strip()
|
| 293 |
+
if isinstance(api_mode, str) and api_mode.strip():
|
| 294 |
+
session_meta["api_mode"] = api_mode.strip()
|
| 295 |
+
cwd_json = json.dumps(session_meta)
|
| 296 |
+
|
| 297 |
+
try:
|
| 298 |
+
# Ensure the session record exists.
|
| 299 |
+
existing = db.get_session(state.session_id)
|
| 300 |
+
if existing is None:
|
| 301 |
+
db.create_session(
|
| 302 |
+
session_id=state.session_id,
|
| 303 |
+
source="acp",
|
| 304 |
+
model=model_str,
|
| 305 |
+
model_config={"cwd": state.cwd},
|
| 306 |
+
)
|
| 307 |
+
else:
|
| 308 |
+
# Update model_config (contains cwd) if changed.
|
| 309 |
+
try:
|
| 310 |
+
with db._lock:
|
| 311 |
+
db._conn.execute(
|
| 312 |
+
"UPDATE sessions SET model_config = ?, model = COALESCE(?, model) WHERE id = ?",
|
| 313 |
+
(cwd_json, model_str, state.session_id),
|
| 314 |
+
)
|
| 315 |
+
db._conn.commit()
|
| 316 |
+
except Exception:
|
| 317 |
+
logger.debug("Failed to update ACP session metadata", exc_info=True)
|
| 318 |
+
|
| 319 |
+
# Replace stored messages with current history.
|
| 320 |
+
db.clear_messages(state.session_id)
|
| 321 |
+
for msg in state.history:
|
| 322 |
+
db.append_message(
|
| 323 |
+
session_id=state.session_id,
|
| 324 |
+
role=msg.get("role", "user"),
|
| 325 |
+
content=msg.get("content"),
|
| 326 |
+
tool_name=msg.get("tool_name") or msg.get("name"),
|
| 327 |
+
tool_calls=msg.get("tool_calls"),
|
| 328 |
+
tool_call_id=msg.get("tool_call_id"),
|
| 329 |
+
)
|
| 330 |
+
except Exception:
|
| 331 |
+
logger.warning("Failed to persist ACP session %s", state.session_id, exc_info=True)
|
| 332 |
+
|
| 333 |
+
def _restore(self, session_id: str) -> Optional[SessionState]:
|
| 334 |
+
"""Load a session from the database into memory, recreating the AIAgent."""
|
| 335 |
+
import threading
|
| 336 |
+
|
| 337 |
+
db = self._get_db()
|
| 338 |
+
if db is None:
|
| 339 |
+
return None
|
| 340 |
+
|
| 341 |
+
try:
|
| 342 |
+
row = db.get_session(session_id)
|
| 343 |
+
except Exception:
|
| 344 |
+
logger.debug("Failed to query DB for ACP session %s", session_id, exc_info=True)
|
| 345 |
+
return None
|
| 346 |
+
|
| 347 |
+
if row is None:
|
| 348 |
+
return None
|
| 349 |
+
|
| 350 |
+
# Only restore ACP sessions.
|
| 351 |
+
if row.get("source") != "acp":
|
| 352 |
+
return None
|
| 353 |
+
|
| 354 |
+
# Extract cwd from model_config.
|
| 355 |
+
cwd = "."
|
| 356 |
+
requested_provider = row.get("billing_provider")
|
| 357 |
+
restored_base_url = row.get("billing_base_url")
|
| 358 |
+
restored_api_mode = None
|
| 359 |
+
mc = row.get("model_config")
|
| 360 |
+
if mc:
|
| 361 |
+
try:
|
| 362 |
+
meta = json.loads(mc)
|
| 363 |
+
if isinstance(meta, dict):
|
| 364 |
+
cwd = meta.get("cwd", ".")
|
| 365 |
+
requested_provider = meta.get("provider") or requested_provider
|
| 366 |
+
restored_base_url = meta.get("base_url") or restored_base_url
|
| 367 |
+
restored_api_mode = meta.get("api_mode") or restored_api_mode
|
| 368 |
+
except (json.JSONDecodeError, TypeError):
|
| 369 |
+
pass
|
| 370 |
+
|
| 371 |
+
model = row.get("model") or None
|
| 372 |
+
|
| 373 |
+
# Load conversation history.
|
| 374 |
+
try:
|
| 375 |
+
history = db.get_messages_as_conversation(session_id)
|
| 376 |
+
except Exception:
|
| 377 |
+
logger.warning("Failed to load messages for ACP session %s", session_id, exc_info=True)
|
| 378 |
+
history = []
|
| 379 |
+
|
| 380 |
+
try:
|
| 381 |
+
agent = self._make_agent(
|
| 382 |
+
session_id=session_id,
|
| 383 |
+
cwd=cwd,
|
| 384 |
+
model=model,
|
| 385 |
+
requested_provider=requested_provider,
|
| 386 |
+
base_url=restored_base_url,
|
| 387 |
+
api_mode=restored_api_mode,
|
| 388 |
+
)
|
| 389 |
+
except Exception:
|
| 390 |
+
logger.warning("Failed to recreate agent for ACP session %s", session_id, exc_info=True)
|
| 391 |
+
return None
|
| 392 |
+
|
| 393 |
+
state = SessionState(
|
| 394 |
+
session_id=session_id,
|
| 395 |
+
agent=agent,
|
| 396 |
+
cwd=cwd,
|
| 397 |
+
model=model or getattr(agent, "model", "") or "",
|
| 398 |
+
history=history,
|
| 399 |
+
cancel_event=threading.Event(),
|
| 400 |
+
)
|
| 401 |
+
with self._lock:
|
| 402 |
+
self._sessions[session_id] = state
|
| 403 |
+
_register_task_cwd(session_id, cwd)
|
| 404 |
+
logger.info("Restored ACP session %s from DB (%d messages)", session_id, len(history))
|
| 405 |
+
return state
|
| 406 |
+
|
| 407 |
+
def _delete_persisted(self, session_id: str) -> bool:
|
| 408 |
+
"""Delete a session from the database. Returns True if it existed."""
|
| 409 |
+
db = self._get_db()
|
| 410 |
+
if db is None:
|
| 411 |
+
return False
|
| 412 |
+
try:
|
| 413 |
+
return db.delete_session(session_id)
|
| 414 |
+
except Exception:
|
| 415 |
+
logger.debug("Failed to delete ACP session %s from DB", session_id, exc_info=True)
|
| 416 |
+
return False
|
| 417 |
+
|
| 418 |
+
# ---- internal -----------------------------------------------------------
|
| 419 |
+
|
| 420 |
+
def _make_agent(
|
| 421 |
+
self,
|
| 422 |
+
*,
|
| 423 |
+
session_id: str,
|
| 424 |
+
cwd: str,
|
| 425 |
+
model: str | None = None,
|
| 426 |
+
requested_provider: str | None = None,
|
| 427 |
+
base_url: str | None = None,
|
| 428 |
+
api_mode: str | None = None,
|
| 429 |
+
):
|
| 430 |
+
if self._agent_factory is not None:
|
| 431 |
+
return self._agent_factory()
|
| 432 |
+
|
| 433 |
+
from run_agent import AIAgent
|
| 434 |
+
from hermes_cli.config import load_config
|
| 435 |
+
from hermes_cli.runtime_provider import resolve_runtime_provider
|
| 436 |
+
|
| 437 |
+
config = load_config()
|
| 438 |
+
model_cfg = config.get("model")
|
| 439 |
+
default_model = ""
|
| 440 |
+
config_provider = None
|
| 441 |
+
if isinstance(model_cfg, dict):
|
| 442 |
+
default_model = str(model_cfg.get("default") or default_model)
|
| 443 |
+
config_provider = model_cfg.get("provider")
|
| 444 |
+
elif isinstance(model_cfg, str) and model_cfg.strip():
|
| 445 |
+
default_model = model_cfg.strip()
|
| 446 |
+
|
| 447 |
+
kwargs = {
|
| 448 |
+
"platform": "acp",
|
| 449 |
+
"enabled_toolsets": ["hermes-acp"],
|
| 450 |
+
"quiet_mode": True,
|
| 451 |
+
"session_id": session_id,
|
| 452 |
+
"model": model or default_model,
|
| 453 |
+
}
|
| 454 |
+
|
| 455 |
+
try:
|
| 456 |
+
runtime = resolve_runtime_provider(requested=requested_provider or config_provider)
|
| 457 |
+
kwargs.update(
|
| 458 |
+
{
|
| 459 |
+
"provider": runtime.get("provider"),
|
| 460 |
+
"api_mode": api_mode or runtime.get("api_mode"),
|
| 461 |
+
"base_url": base_url or runtime.get("base_url"),
|
| 462 |
+
"api_key": runtime.get("api_key"),
|
| 463 |
+
"command": runtime.get("command"),
|
| 464 |
+
"args": list(runtime.get("args") or []),
|
| 465 |
+
}
|
| 466 |
+
)
|
| 467 |
+
except Exception:
|
| 468 |
+
logger.debug("ACP session falling back to default provider resolution", exc_info=True)
|
| 469 |
+
|
| 470 |
+
_register_task_cwd(session_id, cwd)
|
| 471 |
+
agent = AIAgent(**kwargs)
|
| 472 |
+
# ACP stdio transport requires stdout to remain protocol-only JSON-RPC.
|
| 473 |
+
# Route any incidental human-readable agent output to stderr instead.
|
| 474 |
+
agent._print_fn = _acp_stderr_print
|
| 475 |
+
return agent
|
acp_adapter/tools.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ACP tool-call helpers for mapping hermes tools to ACP ToolKind and building content."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import uuid
|
| 6 |
+
from typing import Any, Dict, List, Optional
|
| 7 |
+
|
| 8 |
+
import acp
|
| 9 |
+
from acp.schema import (
|
| 10 |
+
ToolCallLocation,
|
| 11 |
+
ToolCallStart,
|
| 12 |
+
ToolCallProgress,
|
| 13 |
+
ToolKind,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# ---------------------------------------------------------------------------
|
| 17 |
+
# Map hermes tool names -> ACP ToolKind
|
| 18 |
+
# ---------------------------------------------------------------------------
|
| 19 |
+
|
| 20 |
+
TOOL_KIND_MAP: Dict[str, ToolKind] = {
|
| 21 |
+
# File operations
|
| 22 |
+
"read_file": "read",
|
| 23 |
+
"write_file": "edit",
|
| 24 |
+
"patch": "edit",
|
| 25 |
+
"search_files": "search",
|
| 26 |
+
# Terminal / execution
|
| 27 |
+
"terminal": "execute",
|
| 28 |
+
"process": "execute",
|
| 29 |
+
"execute_code": "execute",
|
| 30 |
+
# Web / fetch
|
| 31 |
+
"web_search": "fetch",
|
| 32 |
+
"web_extract": "fetch",
|
| 33 |
+
# Browser
|
| 34 |
+
"browser_navigate": "fetch",
|
| 35 |
+
"browser_click": "execute",
|
| 36 |
+
"browser_type": "execute",
|
| 37 |
+
"browser_snapshot": "read",
|
| 38 |
+
"browser_vision": "read",
|
| 39 |
+
"browser_scroll": "execute",
|
| 40 |
+
"browser_press": "execute",
|
| 41 |
+
"browser_back": "execute",
|
| 42 |
+
"browser_get_images": "read",
|
| 43 |
+
# Agent internals
|
| 44 |
+
"delegate_task": "execute",
|
| 45 |
+
"vision_analyze": "read",
|
| 46 |
+
"image_generate": "execute",
|
| 47 |
+
"text_to_speech": "execute",
|
| 48 |
+
# Thinking / meta
|
| 49 |
+
"_thinking": "think",
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def get_tool_kind(tool_name: str) -> ToolKind:
|
| 54 |
+
"""Return the ACP ToolKind for a hermes tool, defaulting to 'other'."""
|
| 55 |
+
return TOOL_KIND_MAP.get(tool_name, "other")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def make_tool_call_id() -> str:
|
| 59 |
+
"""Generate a unique tool call ID."""
|
| 60 |
+
return f"tc-{uuid.uuid4().hex[:12]}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def build_tool_title(tool_name: str, args: Dict[str, Any]) -> str:
|
| 64 |
+
"""Build a human-readable title for a tool call."""
|
| 65 |
+
if tool_name == "terminal":
|
| 66 |
+
cmd = args.get("command", "")
|
| 67 |
+
if len(cmd) > 80:
|
| 68 |
+
cmd = cmd[:77] + "..."
|
| 69 |
+
return f"terminal: {cmd}"
|
| 70 |
+
if tool_name == "read_file":
|
| 71 |
+
return f"read: {args.get('path', '?')}"
|
| 72 |
+
if tool_name == "write_file":
|
| 73 |
+
return f"write: {args.get('path', '?')}"
|
| 74 |
+
if tool_name == "patch":
|
| 75 |
+
mode = args.get("mode", "replace")
|
| 76 |
+
path = args.get("path", "?")
|
| 77 |
+
return f"patch ({mode}): {path}"
|
| 78 |
+
if tool_name == "search_files":
|
| 79 |
+
return f"search: {args.get('pattern', '?')}"
|
| 80 |
+
if tool_name == "web_search":
|
| 81 |
+
return f"web search: {args.get('query', '?')}"
|
| 82 |
+
if tool_name == "web_extract":
|
| 83 |
+
urls = args.get("urls", [])
|
| 84 |
+
if urls:
|
| 85 |
+
return f"extract: {urls[0]}" + (f" (+{len(urls)-1})" if len(urls) > 1 else "")
|
| 86 |
+
return "web extract"
|
| 87 |
+
if tool_name == "delegate_task":
|
| 88 |
+
goal = args.get("goal", "")
|
| 89 |
+
if goal and len(goal) > 60:
|
| 90 |
+
goal = goal[:57] + "..."
|
| 91 |
+
return f"delegate: {goal}" if goal else "delegate task"
|
| 92 |
+
if tool_name == "execute_code":
|
| 93 |
+
return "execute code"
|
| 94 |
+
if tool_name == "vision_analyze":
|
| 95 |
+
return f"analyze image: {args.get('question', '?')[:50]}"
|
| 96 |
+
return tool_name
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# ---------------------------------------------------------------------------
|
| 100 |
+
# Build ACP content objects for tool-call events
|
| 101 |
+
# ---------------------------------------------------------------------------
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def build_tool_start(
|
| 105 |
+
tool_call_id: str,
|
| 106 |
+
tool_name: str,
|
| 107 |
+
arguments: Dict[str, Any],
|
| 108 |
+
) -> ToolCallStart:
|
| 109 |
+
"""Create a ToolCallStart event for the given hermes tool invocation."""
|
| 110 |
+
kind = get_tool_kind(tool_name)
|
| 111 |
+
title = build_tool_title(tool_name, arguments)
|
| 112 |
+
locations = extract_locations(arguments)
|
| 113 |
+
|
| 114 |
+
if tool_name == "patch":
|
| 115 |
+
mode = arguments.get("mode", "replace")
|
| 116 |
+
if mode == "replace":
|
| 117 |
+
path = arguments.get("path", "")
|
| 118 |
+
old = arguments.get("old_string", "")
|
| 119 |
+
new = arguments.get("new_string", "")
|
| 120 |
+
content = [acp.tool_diff_content(path=path, new_text=new, old_text=old)]
|
| 121 |
+
else:
|
| 122 |
+
# Patch mode — show the patch content as text
|
| 123 |
+
patch_text = arguments.get("patch", "")
|
| 124 |
+
content = [acp.tool_content(acp.text_block(patch_text))]
|
| 125 |
+
return acp.start_tool_call(
|
| 126 |
+
tool_call_id, title, kind=kind, content=content, locations=locations,
|
| 127 |
+
raw_input=arguments,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
if tool_name == "write_file":
|
| 131 |
+
path = arguments.get("path", "")
|
| 132 |
+
file_content = arguments.get("content", "")
|
| 133 |
+
content = [acp.tool_diff_content(path=path, new_text=file_content)]
|
| 134 |
+
return acp.start_tool_call(
|
| 135 |
+
tool_call_id, title, kind=kind, content=content, locations=locations,
|
| 136 |
+
raw_input=arguments,
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
if tool_name == "terminal":
|
| 140 |
+
command = arguments.get("command", "")
|
| 141 |
+
content = [acp.tool_content(acp.text_block(f"$ {command}"))]
|
| 142 |
+
return acp.start_tool_call(
|
| 143 |
+
tool_call_id, title, kind=kind, content=content, locations=locations,
|
| 144 |
+
raw_input=arguments,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
if tool_name == "read_file":
|
| 148 |
+
path = arguments.get("path", "")
|
| 149 |
+
content = [acp.tool_content(acp.text_block(f"Reading {path}"))]
|
| 150 |
+
return acp.start_tool_call(
|
| 151 |
+
tool_call_id, title, kind=kind, content=content, locations=locations,
|
| 152 |
+
raw_input=arguments,
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
if tool_name == "search_files":
|
| 156 |
+
pattern = arguments.get("pattern", "")
|
| 157 |
+
target = arguments.get("target", "content")
|
| 158 |
+
content = [acp.tool_content(acp.text_block(f"Searching for '{pattern}' ({target})"))]
|
| 159 |
+
return acp.start_tool_call(
|
| 160 |
+
tool_call_id, title, kind=kind, content=content, locations=locations,
|
| 161 |
+
raw_input=arguments,
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# Generic fallback
|
| 165 |
+
import json
|
| 166 |
+
try:
|
| 167 |
+
args_text = json.dumps(arguments, indent=2, default=str)
|
| 168 |
+
except (TypeError, ValueError):
|
| 169 |
+
args_text = str(arguments)
|
| 170 |
+
content = [acp.tool_content(acp.text_block(args_text))]
|
| 171 |
+
return acp.start_tool_call(
|
| 172 |
+
tool_call_id, title, kind=kind, content=content, locations=locations,
|
| 173 |
+
raw_input=arguments,
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def build_tool_complete(
|
| 178 |
+
tool_call_id: str,
|
| 179 |
+
tool_name: str,
|
| 180 |
+
result: Optional[str] = None,
|
| 181 |
+
) -> ToolCallProgress:
|
| 182 |
+
"""Create a ToolCallUpdate (progress) event for a completed tool call."""
|
| 183 |
+
kind = get_tool_kind(tool_name)
|
| 184 |
+
|
| 185 |
+
# Truncate very large results for the UI
|
| 186 |
+
display_result = result or ""
|
| 187 |
+
if len(display_result) > 5000:
|
| 188 |
+
display_result = display_result[:4900] + f"\n... ({len(result)} chars total, truncated)"
|
| 189 |
+
|
| 190 |
+
content = [acp.tool_content(acp.text_block(display_result))]
|
| 191 |
+
return acp.update_tool_call(
|
| 192 |
+
tool_call_id,
|
| 193 |
+
kind=kind,
|
| 194 |
+
status="completed",
|
| 195 |
+
content=content,
|
| 196 |
+
raw_output=result,
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# ---------------------------------------------------------------------------
|
| 201 |
+
# Location extraction
|
| 202 |
+
# ---------------------------------------------------------------------------
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def extract_locations(
|
| 206 |
+
arguments: Dict[str, Any],
|
| 207 |
+
) -> List[ToolCallLocation]:
|
| 208 |
+
"""Extract file-system locations from tool arguments."""
|
| 209 |
+
locations: List[ToolCallLocation] = []
|
| 210 |
+
path = arguments.get("path")
|
| 211 |
+
if path:
|
| 212 |
+
line = arguments.get("offset") or arguments.get("line")
|
| 213 |
+
locations.append(ToolCallLocation(path=path, line=line))
|
| 214 |
+
return locations
|
acp_registry/agent.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"schema_version": 1,
|
| 3 |
+
"name": "hermes-agent",
|
| 4 |
+
"display_name": "Hermes Agent",
|
| 5 |
+
"description": "AI agent by Nous Research with 90+ tools, persistent memory, and multi-platform support",
|
| 6 |
+
"icon": "icon.svg",
|
| 7 |
+
"distribution": {
|
| 8 |
+
"type": "command",
|
| 9 |
+
"command": "hermes",
|
| 10 |
+
"args": ["acp"]
|
| 11 |
+
}
|
| 12 |
+
}
|
acp_registry/icon.svg
ADDED
|
|
agent/__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Agent internals -- extracted modules from run_agent.py.
|
| 2 |
+
|
| 3 |
+
These modules contain pure utility functions and self-contained classes
|
| 4 |
+
that were previously embedded in the 3,600-line run_agent.py. Extracting
|
| 5 |
+
them makes run_agent.py focused on the AIAgent orchestrator class.
|
| 6 |
+
"""
|
agent/anthropic_adapter.py
ADDED
|
@@ -0,0 +1,1410 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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thropic's Messages API. Follows the same pattern as the codex_responses
|
| 5 |
+
adapter — all provider-specific logic is isolated here.
|
| 6 |
+
|
| 7 |
+
Auth supports:
|
| 8 |
+
- Regular API keys (sk-ant-api*) → x-api-key header
|
| 9 |
+
- OAuth setup-tokens (sk-ant-oat*) → Bearer auth + beta header
|
| 10 |
+
- Claude Code credentials (~/.claude.json or ~/.claude/.credentials.json) → Bearer auth
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import copy
|
| 14 |
+
import json
|
| 15 |
+
import logging
|
| 16 |
+
import os
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
|
| 19 |
+
from hermes_constants import get_hermes_home
|
| 20 |
+
from types import SimpleNamespace
|
| 21 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
import anthropic as _anthropic_sdk
|
| 25 |
+
except ImportError:
|
| 26 |
+
_anthropic_sdk = None # type: ignore[assignment]
|
| 27 |
+
|
| 28 |
+
logger = logging.getLogger(__name__)
|
| 29 |
+
|
| 30 |
+
THINKING_BUDGET = {"xhigh": 32000, "high": 16000, "medium": 8000, "low": 4000}
|
| 31 |
+
ADAPTIVE_EFFORT_MAP = {
|
| 32 |
+
"xhigh": "max",
|
| 33 |
+
"high": "high",
|
| 34 |
+
"medium": "medium",
|
| 35 |
+
"low": "low",
|
| 36 |
+
"minimal": "low",
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# ── Max output token limits per Anthropic model ───────────────────────
|
| 40 |
+
# Source: Anthropic docs + Cline model catalog. Anthropic's API requires
|
| 41 |
+
# max_tokens as a mandatory field. Previously we hardcoded 16384, which
|
| 42 |
+
# starves thinking-enabled models (thinking tokens count toward the limit).
|
| 43 |
+
_ANTHROPIC_OUTPUT_LIMITS = {
|
| 44 |
+
# Claude 4.6
|
| 45 |
+
"claude-opus-4-6": 128_000,
|
| 46 |
+
"claude-sonnet-4-6": 64_000,
|
| 47 |
+
# Claude 4.5
|
| 48 |
+
"claude-opus-4-5": 64_000,
|
| 49 |
+
"claude-sonnet-4-5": 64_000,
|
| 50 |
+
"claude-haiku-4-5": 64_000,
|
| 51 |
+
# Claude 4
|
| 52 |
+
"claude-opus-4": 32_000,
|
| 53 |
+
"claude-sonnet-4": 64_000,
|
| 54 |
+
# Claude 3.7
|
| 55 |
+
"claude-3-7-sonnet": 128_000,
|
| 56 |
+
# Claude 3.5
|
| 57 |
+
"claude-3-5-sonnet": 8_192,
|
| 58 |
+
"claude-3-5-haiku": 8_192,
|
| 59 |
+
# Claude 3
|
| 60 |
+
"claude-3-opus": 4_096,
|
| 61 |
+
"claude-3-sonnet": 4_096,
|
| 62 |
+
"claude-3-haiku": 4_096,
|
| 63 |
+
# Third-party Anthropic-compatible providers
|
| 64 |
+
"minimax": 131_072,
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# For any model not in the table, assume the highest current limit.
|
| 68 |
+
# Future Anthropic models are unlikely to have *less* output capacity.
|
| 69 |
+
_ANTHROPIC_DEFAULT_OUTPUT_LIMIT = 128_000
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def _get_anthropic_max_output(model: str) -> int:
|
| 73 |
+
"""Look up the max output token limit for an Anthropic model.
|
| 74 |
+
|
| 75 |
+
Uses substring matching against _ANTHROPIC_OUTPUT_LIMITS so date-stamped
|
| 76 |
+
model IDs (claude-sonnet-4-5-20250929) and variant suffixes (:1m, :fast)
|
| 77 |
+
resolve correctly. Longest-prefix match wins to avoid e.g. "claude-3-5"
|
| 78 |
+
matching before "claude-3-5-sonnet".
|
| 79 |
+
|
| 80 |
+
Normalizes dots to hyphens so that model names like
|
| 81 |
+
``anthropic/claude-opus-4.6`` match the ``claude-opus-4-6`` table key.
|
| 82 |
+
"""
|
| 83 |
+
m = model.lower().replace(".", "-")
|
| 84 |
+
best_key = ""
|
| 85 |
+
best_val = _ANTHROPIC_DEFAULT_OUTPUT_LIMIT
|
| 86 |
+
for key, val in _ANTHROPIC_OUTPUT_LIMITS.items():
|
| 87 |
+
if key in m and len(key) > len(best_key):
|
| 88 |
+
best_key = key
|
| 89 |
+
best_val = val
|
| 90 |
+
return best_val
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def _supports_adaptive_thinking(model: str) -> bool:
|
| 94 |
+
"""Return True for Claude 4.6 models that support adaptive thinking."""
|
| 95 |
+
return any(v in model for v in ("4-6", "4.6"))
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# Beta headers for enhanced features (sent with ALL auth types)
|
| 99 |
+
_COMMON_BETAS = [
|
| 100 |
+
"interleaved-thinking-2025-05-14",
|
| 101 |
+
"fine-grained-tool-streaming-2025-05-14",
|
| 102 |
+
]
|
| 103 |
+
# MiniMax's Anthropic-compatible endpoints fail tool-use requests when
|
| 104 |
+
# the fine-grained tool streaming beta is present. Omit it so tool calls
|
| 105 |
+
# fall back to the provider's default response path.
|
| 106 |
+
_TOOL_STREAMING_BETA = "fine-grained-tool-streaming-2025-05-14"
|
| 107 |
+
|
| 108 |
+
# Fast mode beta — enables the ``speed: "fast"`` request parameter for
|
| 109 |
+
# significantly higher output token throughput on Opus 4.6 (~2.5x).
|
| 110 |
+
# See https://platform.claude.com/docs/en/build-with-claude/fast-mode
|
| 111 |
+
_FAST_MODE_BETA = "fast-mode-2026-02-01"
|
| 112 |
+
|
| 113 |
+
# Additional beta headers required for OAuth/subscription auth.
|
| 114 |
+
# Matches what Claude Code (and pi-ai / OpenCode) send.
|
| 115 |
+
_OAUTH_ONLY_BETAS = [
|
| 116 |
+
"claude-code-20250219",
|
| 117 |
+
"oauth-2025-04-20",
|
| 118 |
+
]
|
| 119 |
+
|
| 120 |
+
# Claude Code identity — required for OAuth requests to be routed correctly.
|
| 121 |
+
# Without these, Anthropic's infrastructure intermittently 500s OAuth traffic.
|
| 122 |
+
# The version must stay reasonably current — Anthropic rejects OAuth requests
|
| 123 |
+
# when the spoofed user-agent version is too far behind the actual release.
|
| 124 |
+
_CLAUDE_CODE_VERSION_FALLBACK = "2.1.74"
|
| 125 |
+
_claude_code_version_cache: Optional[str] = None
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def _detect_claude_code_version() -> str:
|
| 129 |
+
"""Detect the installed Claude Code version, fall back to a static constant.
|
| 130 |
+
|
| 131 |
+
Anthropic's OAuth infrastructure validates the user-agent version and may
|
| 132 |
+
reject requests with a version that's too old. Detecting dynamically means
|
| 133 |
+
users who keep Claude Code updated never hit stale-version 400s.
|
| 134 |
+
"""
|
| 135 |
+
import subprocess as _sp
|
| 136 |
+
|
| 137 |
+
for cmd in ("claude", "claude-code"):
|
| 138 |
+
try:
|
| 139 |
+
result = _sp.run(
|
| 140 |
+
[cmd, "--version"],
|
| 141 |
+
capture_output=True, text=True, timeout=5,
|
| 142 |
+
)
|
| 143 |
+
if result.returncode == 0 and result.stdout.strip():
|
| 144 |
+
# Output is like "2.1.74 (Claude Code)" or just "2.1.74"
|
| 145 |
+
version = result.stdout.strip().split()[0]
|
| 146 |
+
if version and version[0].isdigit():
|
| 147 |
+
return version
|
| 148 |
+
except Exception:
|
| 149 |
+
pass
|
| 150 |
+
return _CLAUDE_CODE_VERSION_FALLBACK
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
_CLAUDE_CODE_SYSTEM_PREFIX = "You are Claude Code, Anthropic's official CLI for Claude."
|
| 154 |
+
_MCP_TOOL_PREFIX = "mcp_"
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def _get_claude_code_version() -> str:
|
| 158 |
+
"""Lazily detect the installed Claude Code version when OAuth headers need it."""
|
| 159 |
+
global _claude_code_version_cache
|
| 160 |
+
if _claude_code_version_cache is None:
|
| 161 |
+
_claude_code_version_cache = _detect_claude_code_version()
|
| 162 |
+
return _claude_code_version_cache
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def _is_oauth_token(key: str) -> bool:
|
| 166 |
+
"""Check if the key is an Anthropic OAuth/setup token.
|
| 167 |
+
|
| 168 |
+
Positively identifies Anthropic OAuth tokens by their key format:
|
| 169 |
+
- ``sk-ant-`` prefix (but NOT ``sk-ant-api``) → setup tokens, managed keys
|
| 170 |
+
- ``eyJ`` prefix → JWTs from the Anthropic OAuth flow
|
| 171 |
+
|
| 172 |
+
Non-Anthropic keys (MiniMax, Alibaba, etc.) don't match either pattern
|
| 173 |
+
and correctly return False.
|
| 174 |
+
"""
|
| 175 |
+
if not key:
|
| 176 |
+
return False
|
| 177 |
+
# Regular Anthropic Console API keys — x-api-key auth, never OAuth
|
| 178 |
+
if key.startswith("sk-ant-api"):
|
| 179 |
+
return False
|
| 180 |
+
# Anthropic-issued tokens (setup-tokens sk-ant-oat-*, managed keys)
|
| 181 |
+
if key.startswith("sk-ant-"):
|
| 182 |
+
return True
|
| 183 |
+
# JWTs from Anthropic OAuth flow
|
| 184 |
+
if key.startswith("eyJ"):
|
| 185 |
+
return True
|
| 186 |
+
return False
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def _normalize_base_url_text(base_url) -> str:
|
| 190 |
+
"""Normalize SDK/base transport URL values to a plain string for inspection.
|
| 191 |
+
|
| 192 |
+
Some client objects expose ``base_url`` as an ``httpx.URL`` instead of a raw
|
| 193 |
+
string. Provider/auth detection should accept either shape.
|
| 194 |
+
"""
|
| 195 |
+
if not base_url:
|
| 196 |
+
return ""
|
| 197 |
+
return str(base_url).strip()
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def _is_third_party_anthropic_endpoint(base_url: str | None) -> bool:
|
| 201 |
+
"""Return True for non-Anthropic endpoints using the Anthropic Messages API.
|
| 202 |
+
|
| 203 |
+
Third-party proxies (Azure AI Foundry, AWS Bedrock, self-hosted) authenticate
|
| 204 |
+
with their own API keys via x-api-key, not Anthropic OAuth tokens. OAuth
|
| 205 |
+
detection should be skipped for these endpoints.
|
| 206 |
+
"""
|
| 207 |
+
normalized = _normalize_base_url_text(base_url)
|
| 208 |
+
if not normalized:
|
| 209 |
+
return False # No base_url = direct Anthropic API
|
| 210 |
+
normalized = normalized.rstrip("/").lower()
|
| 211 |
+
if "anthropic.com" in normalized:
|
| 212 |
+
return False # Direct Anthropic API — OAuth applies
|
| 213 |
+
return True # Any other endpoint is a third-party proxy
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def _requires_bearer_auth(base_url: str | None) -> bool:
|
| 217 |
+
"""Return True for Anthropic-compatible providers that require Bearer auth.
|
| 218 |
+
|
| 219 |
+
Some third-party /anthropic endpoints implement Anthropic's Messages API but
|
| 220 |
+
require Authorization: Bearer *** of Anthropic's native x-api-key header.
|
| 221 |
+
MiniMax's global and China Anthropic-compatible endpoints follow this pattern.
|
| 222 |
+
"""
|
| 223 |
+
normalized = _normalize_base_url_text(base_url)
|
| 224 |
+
if not normalized:
|
| 225 |
+
return False
|
| 226 |
+
normalized = normalized.rstrip("/").lower()
|
| 227 |
+
return normalized.startswith(("https://api.minimax.io/anthropic", "https://api.minimaxi.com/anthropic"))
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
def _common_betas_for_base_url(base_url: str | None) -> list[str]:
|
| 231 |
+
"""Return the beta headers that are safe for the configured endpoint.
|
| 232 |
+
|
| 233 |
+
MiniMax's Anthropic-compatible endpoints (Bearer-auth) reject requests
|
| 234 |
+
that include Anthropic's ``fine-grained-tool-streaming`` beta — every
|
| 235 |
+
tool-use message triggers a connection error. Strip that beta for
|
| 236 |
+
Bearer-auth endpoints while keeping all other betas intact.
|
| 237 |
+
"""
|
| 238 |
+
if _requires_bearer_auth(base_url):
|
| 239 |
+
return [b for b in _COMMON_BETAS if b != _TOOL_STREAMING_BETA]
|
| 240 |
+
return _COMMON_BETAS
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def build_anthropic_client(api_key: str, base_url: str = None):
|
| 244 |
+
"""Create an Anthropic client, auto-detecting setup-tokens vs API keys.
|
| 245 |
+
|
| 246 |
+
Returns an anthropic.Anthropic instance.
|
| 247 |
+
"""
|
| 248 |
+
if _anthropic_sdk is None:
|
| 249 |
+
raise ImportError(
|
| 250 |
+
"The 'anthropic' package is required for the Anthropic provider. "
|
| 251 |
+
"Install it with: pip install 'anthropic>=0.39.0'"
|
| 252 |
+
)
|
| 253 |
+
from httpx import Timeout
|
| 254 |
+
|
| 255 |
+
normalized_base_url = _normalize_base_url_text(base_url)
|
| 256 |
+
kwargs = {
|
| 257 |
+
"timeout": Timeout(timeout=900.0, connect=10.0),
|
| 258 |
+
}
|
| 259 |
+
if normalized_base_url:
|
| 260 |
+
kwargs["base_url"] = normalized_base_url
|
| 261 |
+
common_betas = _common_betas_for_base_url(normalized_base_url)
|
| 262 |
+
|
| 263 |
+
if _requires_bearer_auth(normalized_base_url):
|
| 264 |
+
# Some Anthropic-compatible providers (e.g. MiniMax) expect the API key in
|
| 265 |
+
# Authorization: Bearer even for regular API keys. Route those endpoints
|
| 266 |
+
# through auth_token so the SDK sends Bearer auth instead of x-api-key.
|
| 267 |
+
# Check this before OAuth token shape detection because MiniMax secrets do
|
| 268 |
+
# not use Anthropic's sk-ant-api prefix and would otherwise be misread as
|
| 269 |
+
# Anthropic OAuth/setup tokens.
|
| 270 |
+
kwargs["auth_token"] = api_key
|
| 271 |
+
if common_betas:
|
| 272 |
+
kwargs["default_headers"] = {"anthropic-beta": ",".join(common_betas)}
|
| 273 |
+
elif _is_third_party_anthropic_endpoint(base_url):
|
| 274 |
+
# Third-party proxies (Azure AI Foundry, AWS Bedrock, etc.) use their
|
| 275 |
+
# own API keys with x-api-key auth. Skip OAuth detection — their keys
|
| 276 |
+
# don't follow Anthropic's sk-ant-* prefix convention and would be
|
| 277 |
+
# misclassified as OAuth tokens.
|
| 278 |
+
kwargs["api_key"] = api_key
|
| 279 |
+
if common_betas:
|
| 280 |
+
kwargs["default_headers"] = {"anthropic-beta": ",".join(common_betas)}
|
| 281 |
+
elif _is_oauth_token(api_key):
|
| 282 |
+
# OAuth access token / setup-token → Bearer auth + Claude Code identity.
|
| 283 |
+
# Anthropic routes OAuth requests based on user-agent and headers;
|
| 284 |
+
# without Claude Code's fingerprint, requests get intermittent 500s.
|
| 285 |
+
all_betas = common_betas + _OAUTH_ONLY_BETAS
|
| 286 |
+
kwargs["auth_token"] = api_key
|
| 287 |
+
kwargs["default_headers"] = {
|
| 288 |
+
"anthropic-beta": ",".join(all_betas),
|
| 289 |
+
"user-agent": f"claude-cli/{_get_claude_code_version()} (external, cli)",
|
| 290 |
+
"x-app": "cli",
|
| 291 |
+
}
|
| 292 |
+
else:
|
| 293 |
+
# Regular API key → x-api-key header + common betas
|
| 294 |
+
kwargs["api_key"] = api_key
|
| 295 |
+
if common_betas:
|
| 296 |
+
kwargs["default_headers"] = {"anthropic-beta": ",".join(common_betas)}
|
| 297 |
+
|
| 298 |
+
return _anthropic_sdk.Anthropic(**kwargs)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def read_claude_code_credentials() -> Optional[Dict[str, Any]]:
|
| 302 |
+
"""Read refreshable Claude Code OAuth credentials from ~/.claude/.credentials.json.
|
| 303 |
+
|
| 304 |
+
This intentionally excludes ~/.claude.json primaryApiKey. Opencode's
|
| 305 |
+
subscription flow is OAuth/setup-token based with refreshable credentials,
|
| 306 |
+
and native direct Anthropic provider usage should follow that path rather
|
| 307 |
+
than auto-detecting Claude's first-party managed key.
|
| 308 |
+
|
| 309 |
+
Returns dict with {accessToken, refreshToken?, expiresAt?} or None.
|
| 310 |
+
"""
|
| 311 |
+
cred_path = Path.home() / ".claude" / ".credentials.json"
|
| 312 |
+
if cred_path.exists():
|
| 313 |
+
try:
|
| 314 |
+
data = json.loads(cred_path.read_text(encoding="utf-8"))
|
| 315 |
+
oauth_data = data.get("claudeAiOauth")
|
| 316 |
+
if oauth_data and isinstance(oauth_data, dict):
|
| 317 |
+
access_token = oauth_data.get("accessToken", "")
|
| 318 |
+
if access_token:
|
| 319 |
+
return {
|
| 320 |
+
"accessToken": access_token,
|
| 321 |
+
"refreshToken": oauth_data.get("refreshToken", ""),
|
| 322 |
+
"expiresAt": oauth_data.get("expiresAt", 0),
|
| 323 |
+
"source": "claude_code_credentials_file",
|
| 324 |
+
}
|
| 325 |
+
except (json.JSONDecodeError, OSError, IOError) as e:
|
| 326 |
+
logger.debug("Failed to read ~/.claude/.credentials.json: %s", e)
|
| 327 |
+
|
| 328 |
+
return None
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def read_claude_managed_key() -> Optional[str]:
|
| 332 |
+
"""Read Claude's native managed key from ~/.claude.json for diagnostics only."""
|
| 333 |
+
claude_json = Path.home() / ".claude.json"
|
| 334 |
+
if claude_json.exists():
|
| 335 |
+
try:
|
| 336 |
+
data = json.loads(claude_json.read_text(encoding="utf-8"))
|
| 337 |
+
primary_key = data.get("primaryApiKey", "")
|
| 338 |
+
if isinstance(primary_key, str) and primary_key.strip():
|
| 339 |
+
return primary_key.strip()
|
| 340 |
+
except (json.JSONDecodeError, OSError, IOError) as e:
|
| 341 |
+
logger.debug("Failed to read ~/.claude.json: %s", e)
|
| 342 |
+
return None
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
def is_claude_code_token_valid(creds: Dict[str, Any]) -> bool:
|
| 346 |
+
"""Check if Claude Code credentials have a non-expired access token."""
|
| 347 |
+
import time
|
| 348 |
+
|
| 349 |
+
expires_at = creds.get("expiresAt", 0)
|
| 350 |
+
if not expires_at:
|
| 351 |
+
# No expiry set (managed keys) — valid if token is present
|
| 352 |
+
return bool(creds.get("accessToken"))
|
| 353 |
+
|
| 354 |
+
# expiresAt is in milliseconds since epoch
|
| 355 |
+
now_ms = int(time.time() * 1000)
|
| 356 |
+
# Allow 60 seconds of buffer
|
| 357 |
+
return now_ms < (expires_at - 60_000)
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
def refresh_anthropic_oauth_pure(refresh_token: str, *, use_json: bool = False) -> Dict[str, Any]:
|
| 361 |
+
"""Refresh an Anthropic OAuth token without mutating local credential files."""
|
| 362 |
+
import time
|
| 363 |
+
import urllib.parse
|
| 364 |
+
import urllib.request
|
| 365 |
+
|
| 366 |
+
if not refresh_token:
|
| 367 |
+
raise ValueError("refresh_token is required")
|
| 368 |
+
|
| 369 |
+
client_id = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
|
| 370 |
+
if use_json:
|
| 371 |
+
data = json.dumps({
|
| 372 |
+
"grant_type": "refresh_token",
|
| 373 |
+
"refresh_token": refresh_token,
|
| 374 |
+
"client_id": client_id,
|
| 375 |
+
}).encode()
|
| 376 |
+
content_type = "application/json"
|
| 377 |
+
else:
|
| 378 |
+
data = urllib.parse.urlencode({
|
| 379 |
+
"grant_type": "refresh_token",
|
| 380 |
+
"refresh_token": refresh_token,
|
| 381 |
+
"client_id": client_id,
|
| 382 |
+
}).encode()
|
| 383 |
+
content_type = "application/x-www-form-urlencoded"
|
| 384 |
+
|
| 385 |
+
token_endpoints = [
|
| 386 |
+
"https://platform.claude.com/v1/oauth/token",
|
| 387 |
+
"https://console.anthropic.com/v1/oauth/token",
|
| 388 |
+
]
|
| 389 |
+
last_error = None
|
| 390 |
+
for endpoint in token_endpoints:
|
| 391 |
+
req = urllib.request.Request(
|
| 392 |
+
endpoint,
|
| 393 |
+
data=data,
|
| 394 |
+
headers={
|
| 395 |
+
"Content-Type": content_type,
|
| 396 |
+
"User-Agent": f"claude-cli/{_get_claude_code_version()} (external, cli)",
|
| 397 |
+
},
|
| 398 |
+
method="POST",
|
| 399 |
+
)
|
| 400 |
+
try:
|
| 401 |
+
with urllib.request.urlopen(req, timeout=10) as resp:
|
| 402 |
+
result = json.loads(resp.read().decode())
|
| 403 |
+
except Exception as exc:
|
| 404 |
+
last_error = exc
|
| 405 |
+
logger.debug("Anthropic token refresh failed at %s: %s", endpoint, exc)
|
| 406 |
+
continue
|
| 407 |
+
|
| 408 |
+
access_token = result.get("access_token", "")
|
| 409 |
+
if not access_token:
|
| 410 |
+
raise ValueError("Anthropic refresh response was missing access_token")
|
| 411 |
+
next_refresh = result.get("refresh_token", refresh_token)
|
| 412 |
+
expires_in = result.get("expires_in", 3600)
|
| 413 |
+
return {
|
| 414 |
+
"access_token": access_token,
|
| 415 |
+
"refresh_token": next_refresh,
|
| 416 |
+
"expires_at_ms": int(time.time() * 1000) + (expires_in * 1000),
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
if last_error is not None:
|
| 420 |
+
raise last_error
|
| 421 |
+
raise ValueError("Anthropic token refresh failed")
|
| 422 |
+
|
| 423 |
+
|
| 424 |
+
def _refresh_oauth_token(creds: Dict[str, Any]) -> Optional[str]:
|
| 425 |
+
"""Attempt to refresh an expired Claude Code OAuth token."""
|
| 426 |
+
refresh_token = creds.get("refreshToken", "")
|
| 427 |
+
if not refresh_token:
|
| 428 |
+
logger.debug("No refresh token available — cannot refresh")
|
| 429 |
+
return None
|
| 430 |
+
|
| 431 |
+
try:
|
| 432 |
+
refreshed = refresh_anthropic_oauth_pure(refresh_token, use_json=False)
|
| 433 |
+
_write_claude_code_credentials(
|
| 434 |
+
refreshed["access_token"],
|
| 435 |
+
refreshed["refresh_token"],
|
| 436 |
+
refreshed["expires_at_ms"],
|
| 437 |
+
)
|
| 438 |
+
logger.debug("Successfully refreshed Claude Code OAuth token")
|
| 439 |
+
return refreshed["access_token"]
|
| 440 |
+
except Exception as e:
|
| 441 |
+
logger.debug("Failed to refresh Claude Code token: %s", e)
|
| 442 |
+
return None
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
def _write_claude_code_credentials(
|
| 446 |
+
access_token: str,
|
| 447 |
+
refresh_token: str,
|
| 448 |
+
expires_at_ms: int,
|
| 449 |
+
*,
|
| 450 |
+
scopes: Optional[list] = None,
|
| 451 |
+
) -> None:
|
| 452 |
+
"""Write refreshed credentials back to ~/.claude/.credentials.json.
|
| 453 |
+
|
| 454 |
+
The optional *scopes* list (e.g. ``["user:inference", "user:profile", ...]``)
|
| 455 |
+
is persisted so that Claude Code's own auth check recognises the credential
|
| 456 |
+
as valid. Claude Code >=2.1.81 gates on the presence of ``"user:inference"``
|
| 457 |
+
in the stored scopes before it will use the token.
|
| 458 |
+
"""
|
| 459 |
+
cred_path = Path.home() / ".claude" / ".credentials.json"
|
| 460 |
+
try:
|
| 461 |
+
# Read existing file to preserve other fields
|
| 462 |
+
existing = {}
|
| 463 |
+
if cred_path.exists():
|
| 464 |
+
existing = json.loads(cred_path.read_text(encoding="utf-8"))
|
| 465 |
+
|
| 466 |
+
oauth_data: Dict[str, Any] = {
|
| 467 |
+
"accessToken": access_token,
|
| 468 |
+
"refreshToken": refresh_token,
|
| 469 |
+
"expiresAt": expires_at_ms,
|
| 470 |
+
}
|
| 471 |
+
if scopes is not None:
|
| 472 |
+
oauth_data["scopes"] = scopes
|
| 473 |
+
elif "claudeAiOauth" in existing and "scopes" in existing["claudeAiOauth"]:
|
| 474 |
+
# Preserve previously-stored scopes when the refresh response
|
| 475 |
+
# does not include a scope field.
|
| 476 |
+
oauth_data["scopes"] = existing["claudeAiOauth"]["scopes"]
|
| 477 |
+
|
| 478 |
+
existing["claudeAiOauth"] = oauth_data
|
| 479 |
+
|
| 480 |
+
cred_path.parent.mkdir(parents=True, exist_ok=True)
|
| 481 |
+
cred_path.write_text(json.dumps(existing, indent=2), encoding="utf-8")
|
| 482 |
+
# Restrict permissions (credentials file)
|
| 483 |
+
cred_path.chmod(0o600)
|
| 484 |
+
except (OSError, IOError) as e:
|
| 485 |
+
logger.debug("Failed to write refreshed credentials: %s", e)
|
| 486 |
+
|
| 487 |
+
|
| 488 |
+
def _resolve_claude_code_token_from_credentials(creds: Optional[Dict[str, Any]] = None) -> Optional[str]:
|
| 489 |
+
"""Resolve a token from Claude Code credential files, refreshing if needed."""
|
| 490 |
+
creds = creds or read_claude_code_credentials()
|
| 491 |
+
if creds and is_claude_code_token_valid(creds):
|
| 492 |
+
logger.debug("Using Claude Code credentials (auto-detected)")
|
| 493 |
+
return creds["accessToken"]
|
| 494 |
+
if creds:
|
| 495 |
+
logger.debug("Claude Code credentials expired — attempting refresh")
|
| 496 |
+
refreshed = _refresh_oauth_token(creds)
|
| 497 |
+
if refreshed:
|
| 498 |
+
return refreshed
|
| 499 |
+
logger.debug("Token refresh failed — re-run 'claude setup-token' to reauthenticate")
|
| 500 |
+
return None
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
def _prefer_refreshable_claude_code_token(env_token: str, creds: Optional[Dict[str, Any]]) -> Optional[str]:
|
| 504 |
+
"""Prefer Claude Code creds when a persisted env OAuth token would shadow refresh.
|
| 505 |
+
|
| 506 |
+
Hermes historically persisted setup tokens into ANTHROPIC_TOKEN. That makes
|
| 507 |
+
later refresh impossible because the static env token wins before we ever
|
| 508 |
+
inspect Claude Code's refreshable credential file. If we have a refreshable
|
| 509 |
+
Claude Code credential record, prefer it over the static env OAuth token.
|
| 510 |
+
"""
|
| 511 |
+
if not env_token or not _is_oauth_token(env_token) or not isinstance(creds, dict):
|
| 512 |
+
return None
|
| 513 |
+
if not creds.get("refreshToken"):
|
| 514 |
+
return None
|
| 515 |
+
|
| 516 |
+
resolved = _resolve_claude_code_token_from_credentials(creds)
|
| 517 |
+
if resolved and resolved != env_token:
|
| 518 |
+
logger.debug(
|
| 519 |
+
"Preferring Claude Code credential file over static env OAuth token so refresh can proceed"
|
| 520 |
+
)
|
| 521 |
+
return resolved
|
| 522 |
+
return None
|
| 523 |
+
|
| 524 |
+
|
| 525 |
+
def resolve_anthropic_token() -> Optional[str]:
|
| 526 |
+
"""Resolve an Anthropic token from all available sources.
|
| 527 |
+
|
| 528 |
+
Priority:
|
| 529 |
+
1. ANTHROPIC_TOKEN env var (OAuth/setup token saved by Hermes)
|
| 530 |
+
2. CLAUDE_CODE_OAUTH_TOKEN env var
|
| 531 |
+
3. Claude Code credentials (~/.claude.json or ~/.claude/.credentials.json)
|
| 532 |
+
— with automatic refresh if expired and a refresh token is available
|
| 533 |
+
4. ANTHROPIC_API_KEY env var (regular API key, or legacy fallback)
|
| 534 |
+
|
| 535 |
+
Returns the token string or None.
|
| 536 |
+
"""
|
| 537 |
+
creds = read_claude_code_credentials()
|
| 538 |
+
|
| 539 |
+
# 1. Hermes-managed OAuth/setup token env var
|
| 540 |
+
token = os.getenv("ANTHROPIC_TOKEN", "").strip()
|
| 541 |
+
if token:
|
| 542 |
+
preferred = _prefer_refreshable_claude_code_token(token, creds)
|
| 543 |
+
if preferred:
|
| 544 |
+
return preferred
|
| 545 |
+
return token
|
| 546 |
+
|
| 547 |
+
# 2. CLAUDE_CODE_OAUTH_TOKEN (used by Claude Code for setup-tokens)
|
| 548 |
+
cc_token = os.getenv("CLAUDE_CODE_OAUTH_TOKEN", "").strip()
|
| 549 |
+
if cc_token:
|
| 550 |
+
preferred = _prefer_refreshable_claude_code_token(cc_token, creds)
|
| 551 |
+
if preferred:
|
| 552 |
+
return preferred
|
| 553 |
+
return cc_token
|
| 554 |
+
|
| 555 |
+
# 3. Claude Code credential file
|
| 556 |
+
resolved_claude_token = _resolve_claude_code_token_from_credentials(creds)
|
| 557 |
+
if resolved_claude_token:
|
| 558 |
+
return resolved_claude_token
|
| 559 |
+
|
| 560 |
+
# 4. Regular API key, or a legacy OAuth token saved in ANTHROPIC_API_KEY.
|
| 561 |
+
# This remains as a compatibility fallback for pre-migration Hermes configs.
|
| 562 |
+
api_key = os.getenv("ANTHROPIC_API_KEY", "").strip()
|
| 563 |
+
if api_key:
|
| 564 |
+
return api_key
|
| 565 |
+
|
| 566 |
+
return None
|
| 567 |
+
|
| 568 |
+
|
| 569 |
+
def run_oauth_setup_token() -> Optional[str]:
|
| 570 |
+
"""Run 'claude setup-token' interactively and return the resulting token.
|
| 571 |
+
|
| 572 |
+
Checks multiple sources after the subprocess completes:
|
| 573 |
+
1. Claude Code credential files (may be written by the subprocess)
|
| 574 |
+
2. CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_TOKEN env vars
|
| 575 |
+
|
| 576 |
+
Returns the token string, or None if no credentials were obtained.
|
| 577 |
+
Raises FileNotFoundError if the 'claude' CLI is not installed.
|
| 578 |
+
"""
|
| 579 |
+
import shutil
|
| 580 |
+
import subprocess
|
| 581 |
+
|
| 582 |
+
claude_path = shutil.which("claude")
|
| 583 |
+
if not claude_path:
|
| 584 |
+
raise FileNotFoundError(
|
| 585 |
+
"The 'claude' CLI is not installed. "
|
| 586 |
+
"Install it with: npm install -g @anthropic-ai/claude-code"
|
| 587 |
+
)
|
| 588 |
+
|
| 589 |
+
# Run interactively — stdin/stdout/stderr inherited so user can interact
|
| 590 |
+
try:
|
| 591 |
+
subprocess.run([claude_path, "setup-token"])
|
| 592 |
+
except (KeyboardInterrupt, EOFError):
|
| 593 |
+
return None
|
| 594 |
+
|
| 595 |
+
# Check if credentials were saved to Claude Code's config files
|
| 596 |
+
creds = read_claude_code_credentials()
|
| 597 |
+
if creds and is_claude_code_token_valid(creds):
|
| 598 |
+
return creds["accessToken"]
|
| 599 |
+
|
| 600 |
+
# Check env vars that may have been set
|
| 601 |
+
for env_var in ("CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_TOKEN"):
|
| 602 |
+
val = os.getenv(env_var, "").strip()
|
| 603 |
+
if val:
|
| 604 |
+
return val
|
| 605 |
+
|
| 606 |
+
return None
|
| 607 |
+
|
| 608 |
+
|
| 609 |
+
# ── Hermes-native PKCE OAuth flow ────────────────────────────────────────
|
| 610 |
+
# Mirrors the flow used by Claude Code, pi-ai, and OpenCode.
|
| 611 |
+
# Stores credentials in ~/.hermes/.anthropic_oauth.json (our own file).
|
| 612 |
+
|
| 613 |
+
_OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
|
| 614 |
+
_OAUTH_TOKEN_URL = "https://console.anthropic.com/v1/oauth/token"
|
| 615 |
+
_OAUTH_REDIRECT_URI = "https://console.anthropic.com/oauth/code/callback"
|
| 616 |
+
_OAUTH_SCOPES = "org:create_api_key user:profile user:inference"
|
| 617 |
+
_HERMES_OAUTH_FILE = get_hermes_home() / ".anthropic_oauth.json"
|
| 618 |
+
|
| 619 |
+
|
| 620 |
+
def _generate_pkce() -> tuple:
|
| 621 |
+
"""Generate PKCE code_verifier and code_challenge (S256)."""
|
| 622 |
+
import base64
|
| 623 |
+
import hashlib
|
| 624 |
+
import secrets
|
| 625 |
+
|
| 626 |
+
verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode()
|
| 627 |
+
challenge = base64.urlsafe_b64encode(
|
| 628 |
+
hashlib.sha256(verifier.encode()).digest()
|
| 629 |
+
).rstrip(b"=").decode()
|
| 630 |
+
return verifier, challenge
|
| 631 |
+
|
| 632 |
+
|
| 633 |
+
def run_hermes_oauth_login_pure() -> Optional[Dict[str, Any]]:
|
| 634 |
+
"""Run Hermes-native OAuth PKCE flow and return credential state."""
|
| 635 |
+
import time
|
| 636 |
+
import webbrowser
|
| 637 |
+
|
| 638 |
+
verifier, challenge = _generate_pkce()
|
| 639 |
+
|
| 640 |
+
params = {
|
| 641 |
+
"code": "true",
|
| 642 |
+
"client_id": _OAUTH_CLIENT_ID,
|
| 643 |
+
"response_type": "code",
|
| 644 |
+
"redirect_uri": _OAUTH_REDIRECT_URI,
|
| 645 |
+
"scope": _OAUTH_SCOPES,
|
| 646 |
+
"code_challenge": challenge,
|
| 647 |
+
"code_challenge_method": "S256",
|
| 648 |
+
"state": verifier,
|
| 649 |
+
}
|
| 650 |
+
from urllib.parse import urlencode
|
| 651 |
+
|
| 652 |
+
auth_url = f"https://claude.ai/oauth/authorize?{urlencode(params)}"
|
| 653 |
+
|
| 654 |
+
print()
|
| 655 |
+
print("Authorize Hermes with your Claude Pro/Max subscription.")
|
| 656 |
+
print()
|
| 657 |
+
print("╭─ Claude Pro/Max Authorization ────────────────────╮")
|
| 658 |
+
print("│ │")
|
| 659 |
+
print("│ Open this link in your browser: │")
|
| 660 |
+
print("╰───────────────────────────────────────────────────╯")
|
| 661 |
+
print()
|
| 662 |
+
print(f" {auth_url}")
|
| 663 |
+
print()
|
| 664 |
+
|
| 665 |
+
try:
|
| 666 |
+
webbrowser.open(auth_url)
|
| 667 |
+
print(" (Browser opened automatically)")
|
| 668 |
+
except Exception:
|
| 669 |
+
pass
|
| 670 |
+
|
| 671 |
+
print()
|
| 672 |
+
print("After authorizing, you'll see a code. Paste it below.")
|
| 673 |
+
print()
|
| 674 |
+
try:
|
| 675 |
+
auth_code = input("Authorization code: ").strip()
|
| 676 |
+
except (KeyboardInterrupt, EOFError):
|
| 677 |
+
return None
|
| 678 |
+
|
| 679 |
+
if not auth_code:
|
| 680 |
+
print("No code entered.")
|
| 681 |
+
return None
|
| 682 |
+
|
| 683 |
+
splits = auth_code.split("#")
|
| 684 |
+
code = splits[0]
|
| 685 |
+
state = splits[1] if len(splits) > 1 else ""
|
| 686 |
+
|
| 687 |
+
try:
|
| 688 |
+
import urllib.request
|
| 689 |
+
|
| 690 |
+
exchange_data = json.dumps({
|
| 691 |
+
"grant_type": "authorization_code",
|
| 692 |
+
"client_id": _OAUTH_CLIENT_ID,
|
| 693 |
+
"code": code,
|
| 694 |
+
"state": state,
|
| 695 |
+
"redirect_uri": _OAUTH_REDIRECT_URI,
|
| 696 |
+
"code_verifier": verifier,
|
| 697 |
+
}).encode()
|
| 698 |
+
|
| 699 |
+
req = urllib.request.Request(
|
| 700 |
+
_OAUTH_TOKEN_URL,
|
| 701 |
+
data=exchange_data,
|
| 702 |
+
headers={
|
| 703 |
+
"Content-Type": "application/json",
|
| 704 |
+
"User-Agent": f"claude-cli/{_get_claude_code_version()} (external, cli)",
|
| 705 |
+
},
|
| 706 |
+
method="POST",
|
| 707 |
+
)
|
| 708 |
+
|
| 709 |
+
with urllib.request.urlopen(req, timeout=15) as resp:
|
| 710 |
+
result = json.loads(resp.read().decode())
|
| 711 |
+
except Exception as e:
|
| 712 |
+
print(f"Token exchange failed: {e}")
|
| 713 |
+
return None
|
| 714 |
+
|
| 715 |
+
access_token = result.get("access_token", "")
|
| 716 |
+
refresh_token = result.get("refresh_token", "")
|
| 717 |
+
expires_in = result.get("expires_in", 3600)
|
| 718 |
+
|
| 719 |
+
if not access_token:
|
| 720 |
+
print("No access token in response.")
|
| 721 |
+
return None
|
| 722 |
+
|
| 723 |
+
expires_at_ms = int(time.time() * 1000) + (expires_in * 1000)
|
| 724 |
+
return {
|
| 725 |
+
"access_token": access_token,
|
| 726 |
+
"refresh_token": refresh_token,
|
| 727 |
+
"expires_at_ms": expires_at_ms,
|
| 728 |
+
}
|
| 729 |
+
|
| 730 |
+
|
| 731 |
+
def read_hermes_oauth_credentials() -> Optional[Dict[str, Any]]:
|
| 732 |
+
"""Read Hermes-managed OAuth credentials from ~/.hermes/.anthropic_oauth.json."""
|
| 733 |
+
if _HERMES_OAUTH_FILE.exists():
|
| 734 |
+
try:
|
| 735 |
+
data = json.loads(_HERMES_OAUTH_FILE.read_text(encoding="utf-8"))
|
| 736 |
+
if data.get("accessToken"):
|
| 737 |
+
return data
|
| 738 |
+
except (json.JSONDecodeError, OSError, IOError) as e:
|
| 739 |
+
logger.debug("Failed to read Hermes OAuth credentials: %s", e)
|
| 740 |
+
return None
|
| 741 |
+
|
| 742 |
+
|
| 743 |
+
# ---------------------------------------------------------------------------
|
| 744 |
+
# Message / tool / response format conversion
|
| 745 |
+
# ---------------------------------------------------------------------------
|
| 746 |
+
|
| 747 |
+
|
| 748 |
+
def normalize_model_name(model: str, preserve_dots: bool = False) -> str:
|
| 749 |
+
"""Normalize a model name for the Anthropic API.
|
| 750 |
+
|
| 751 |
+
- Strips 'anthropic/' prefix (OpenRouter format, case-insensitive)
|
| 752 |
+
- Converts dots to hyphens in version numbers (OpenRouter uses dots,
|
| 753 |
+
Anthropic uses hyphens: claude-opus-4.6 → claude-opus-4-6), unless
|
| 754 |
+
preserve_dots is True (e.g. for Alibaba/DashScope: qwen3.5-plus).
|
| 755 |
+
"""
|
| 756 |
+
lower = model.lower()
|
| 757 |
+
if lower.startswith("anthropic/"):
|
| 758 |
+
model = model[len("anthropic/"):]
|
| 759 |
+
if not preserve_dots:
|
| 760 |
+
# OpenRouter uses dots for version separators (claude-opus-4.6),
|
| 761 |
+
# Anthropic uses hyphens (claude-opus-4-6). Convert dots to hyphens.
|
| 762 |
+
model = model.replace(".", "-")
|
| 763 |
+
return model
|
| 764 |
+
|
| 765 |
+
|
| 766 |
+
def _sanitize_tool_id(tool_id: str) -> str:
|
| 767 |
+
"""Sanitize a tool call ID for the Anthropic API.
|
| 768 |
+
|
| 769 |
+
Anthropic requires IDs matching [a-zA-Z0-9_-]. Replace invalid
|
| 770 |
+
characters with underscores and ensure non-empty.
|
| 771 |
+
"""
|
| 772 |
+
import re
|
| 773 |
+
if not tool_id:
|
| 774 |
+
return "tool_0"
|
| 775 |
+
sanitized = re.sub(r"[^a-zA-Z0-9_-]", "_", tool_id)
|
| 776 |
+
return sanitized or "tool_0"
|
| 777 |
+
|
| 778 |
+
|
| 779 |
+
def convert_tools_to_anthropic(tools: List[Dict]) -> List[Dict]:
|
| 780 |
+
"""Convert OpenAI tool definitions to Anthropic format."""
|
| 781 |
+
if not tools:
|
| 782 |
+
return []
|
| 783 |
+
result = []
|
| 784 |
+
for t in tools:
|
| 785 |
+
fn = t.get("function", {})
|
| 786 |
+
result.append({
|
| 787 |
+
"name": fn.get("name", ""),
|
| 788 |
+
"description": fn.get("description", ""),
|
| 789 |
+
"input_schema": fn.get("parameters", {"type": "object", "properties": {}}),
|
| 790 |
+
})
|
| 791 |
+
return result
|
| 792 |
+
|
| 793 |
+
|
| 794 |
+
def _image_source_from_openai_url(url: str) -> Dict[str, str]:
|
| 795 |
+
"""Convert an OpenAI-style image URL/data URL into Anthropic image source."""
|
| 796 |
+
url = str(url or "").strip()
|
| 797 |
+
if not url:
|
| 798 |
+
return {"type": "url", "url": ""}
|
| 799 |
+
|
| 800 |
+
if url.startswith("data:"):
|
| 801 |
+
header, _, data = url.partition(",")
|
| 802 |
+
media_type = "image/jpeg"
|
| 803 |
+
if header.startswith("data:"):
|
| 804 |
+
mime_part = header[len("data:"):].split(";", 1)[0].strip()
|
| 805 |
+
if mime_part.startswith("image/"):
|
| 806 |
+
media_type = mime_part
|
| 807 |
+
return {
|
| 808 |
+
"type": "base64",
|
| 809 |
+
"media_type": media_type,
|
| 810 |
+
"data": data,
|
| 811 |
+
}
|
| 812 |
+
|
| 813 |
+
return {"type": "url", "url": url}
|
| 814 |
+
|
| 815 |
+
|
| 816 |
+
def _convert_content_part_to_anthropic(part: Any) -> Optional[Dict[str, Any]]:
|
| 817 |
+
"""Convert a single OpenAI-style content part to Anthropic format."""
|
| 818 |
+
if part is None:
|
| 819 |
+
return None
|
| 820 |
+
if isinstance(part, str):
|
| 821 |
+
return {"type": "text", "text": part}
|
| 822 |
+
if not isinstance(part, dict):
|
| 823 |
+
return {"type": "text", "text": str(part)}
|
| 824 |
+
|
| 825 |
+
ptype = part.get("type")
|
| 826 |
+
|
| 827 |
+
if ptype == "input_text":
|
| 828 |
+
block: Dict[str, Any] = {"type": "text", "text": part.get("text", "")}
|
| 829 |
+
elif ptype in {"image_url", "input_image"}:
|
| 830 |
+
image_value = part.get("image_url", {})
|
| 831 |
+
url = image_value.get("url", "") if isinstance(image_value, dict) else str(image_value or "")
|
| 832 |
+
block = {"type": "image", "source": _image_source_from_openai_url(url)}
|
| 833 |
+
else:
|
| 834 |
+
block = dict(part)
|
| 835 |
+
|
| 836 |
+
if isinstance(part.get("cache_control"), dict) and "cache_control" not in block:
|
| 837 |
+
block["cache_control"] = dict(part["cache_control"])
|
| 838 |
+
return block
|
| 839 |
+
|
| 840 |
+
|
| 841 |
+
def _to_plain_data(value: Any, *, _depth: int = 0, _path: Optional[set] = None) -> Any:
|
| 842 |
+
"""Recursively convert SDK objects to plain Python data structures.
|
| 843 |
+
|
| 844 |
+
Guards against circular references (``_path`` tracks ``id()`` of objects
|
| 845 |
+
on the *current* recursion path) and runaway depth (capped at 20 levels).
|
| 846 |
+
Uses path-based tracking so shared (but non-cyclic) objects referenced by
|
| 847 |
+
multiple siblings are converted correctly rather than being stringified.
|
| 848 |
+
"""
|
| 849 |
+
_MAX_DEPTH = 20
|
| 850 |
+
if _depth > _MAX_DEPTH:
|
| 851 |
+
return str(value)
|
| 852 |
+
|
| 853 |
+
if _path is None:
|
| 854 |
+
_path = set()
|
| 855 |
+
|
| 856 |
+
obj_id = id(value)
|
| 857 |
+
if obj_id in _path:
|
| 858 |
+
return str(value)
|
| 859 |
+
|
| 860 |
+
if hasattr(value, "model_dump"):
|
| 861 |
+
_path.add(obj_id)
|
| 862 |
+
result = _to_plain_data(value.model_dump(), _depth=_depth + 1, _path=_path)
|
| 863 |
+
_path.discard(obj_id)
|
| 864 |
+
return result
|
| 865 |
+
if isinstance(value, dict):
|
| 866 |
+
_path.add(obj_id)
|
| 867 |
+
result = {k: _to_plain_data(v, _depth=_depth + 1, _path=_path) for k, v in value.items()}
|
| 868 |
+
_path.discard(obj_id)
|
| 869 |
+
return result
|
| 870 |
+
if isinstance(value, (list, tuple)):
|
| 871 |
+
_path.add(obj_id)
|
| 872 |
+
result = [_to_plain_data(v, _depth=_depth + 1, _path=_path) for v in value]
|
| 873 |
+
_path.discard(obj_id)
|
| 874 |
+
return result
|
| 875 |
+
if hasattr(value, "__dict__"):
|
| 876 |
+
_path.add(obj_id)
|
| 877 |
+
result = {
|
| 878 |
+
k: _to_plain_data(v, _depth=_depth + 1, _path=_path)
|
| 879 |
+
for k, v in vars(value).items()
|
| 880 |
+
if not k.startswith("_")
|
| 881 |
+
}
|
| 882 |
+
_path.discard(obj_id)
|
| 883 |
+
return result
|
| 884 |
+
return value
|
| 885 |
+
|
| 886 |
+
|
| 887 |
+
def _extract_preserved_thinking_blocks(message: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 888 |
+
"""Return Anthropic thinking blocks previously preserved on the message."""
|
| 889 |
+
raw_details = message.get("reasoning_details")
|
| 890 |
+
if not isinstance(raw_details, list):
|
| 891 |
+
return []
|
| 892 |
+
|
| 893 |
+
preserved: List[Dict[str, Any]] = []
|
| 894 |
+
for detail in raw_details:
|
| 895 |
+
if not isinstance(detail, dict):
|
| 896 |
+
continue
|
| 897 |
+
block_type = str(detail.get("type", "") or "").strip().lower()
|
| 898 |
+
if block_type not in {"thinking", "redacted_thinking"}:
|
| 899 |
+
continue
|
| 900 |
+
preserved.append(copy.deepcopy(detail))
|
| 901 |
+
return preserved
|
| 902 |
+
|
| 903 |
+
|
| 904 |
+
def _convert_content_to_anthropic(content: Any) -> Any:
|
| 905 |
+
"""Convert OpenAI-style multimodal content arrays to Anthropic blocks."""
|
| 906 |
+
if not isinstance(content, list):
|
| 907 |
+
return content
|
| 908 |
+
|
| 909 |
+
converted = []
|
| 910 |
+
for part in content:
|
| 911 |
+
block = _convert_content_part_to_anthropic(part)
|
| 912 |
+
if block is not None:
|
| 913 |
+
converted.append(block)
|
| 914 |
+
return converted
|
| 915 |
+
|
| 916 |
+
|
| 917 |
+
def convert_messages_to_anthropic(
|
| 918 |
+
messages: List[Dict],
|
| 919 |
+
base_url: str | None = None,
|
| 920 |
+
) -> Tuple[Optional[Any], List[Dict]]:
|
| 921 |
+
"""Convert OpenAI-format messages to Anthropic format.
|
| 922 |
+
|
| 923 |
+
Returns (system_prompt, anthropic_messages).
|
| 924 |
+
System messages are extracted since Anthropic takes them as a separate param.
|
| 925 |
+
system_prompt is a string or list of content blocks (when cache_control present).
|
| 926 |
+
|
| 927 |
+
When *base_url* is provided and points to a third-party Anthropic-compatible
|
| 928 |
+
endpoint, all thinking block signatures are stripped. Signatures are
|
| 929 |
+
Anthropic-proprietary — third-party endpoints cannot validate them and will
|
| 930 |
+
reject them with HTTP 400 "Invalid signature in thinking block".
|
| 931 |
+
"""
|
| 932 |
+
system = None
|
| 933 |
+
result = []
|
| 934 |
+
|
| 935 |
+
for m in messages:
|
| 936 |
+
role = m.get("role", "user")
|
| 937 |
+
content = m.get("content", "")
|
| 938 |
+
|
| 939 |
+
if role == "system":
|
| 940 |
+
if isinstance(content, list):
|
| 941 |
+
# Preserve cache_control markers on content blocks
|
| 942 |
+
has_cache = any(
|
| 943 |
+
p.get("cache_control") for p in content if isinstance(p, dict)
|
| 944 |
+
)
|
| 945 |
+
if has_cache:
|
| 946 |
+
system = [p for p in content if isinstance(p, dict)]
|
| 947 |
+
else:
|
| 948 |
+
system = "\n".join(
|
| 949 |
+
p["text"] for p in content if p.get("type") == "text"
|
| 950 |
+
)
|
| 951 |
+
else:
|
| 952 |
+
system = content
|
| 953 |
+
continue
|
| 954 |
+
|
| 955 |
+
if role == "assistant":
|
| 956 |
+
blocks = _extract_preserved_thinking_blocks(m)
|
| 957 |
+
if content:
|
| 958 |
+
if isinstance(content, list):
|
| 959 |
+
converted_content = _convert_content_to_anthropic(content)
|
| 960 |
+
if isinstance(converted_content, list):
|
| 961 |
+
blocks.extend(converted_content)
|
| 962 |
+
else:
|
| 963 |
+
blocks.append({"type": "text", "text": str(content)})
|
| 964 |
+
for tc in m.get("tool_calls", []):
|
| 965 |
+
if not tc or not isinstance(tc, dict):
|
| 966 |
+
continue
|
| 967 |
+
fn = tc.get("function", {})
|
| 968 |
+
args = fn.get("arguments", "{}")
|
| 969 |
+
try:
|
| 970 |
+
parsed_args = json.loads(args) if isinstance(args, str) else args
|
| 971 |
+
except (json.JSONDecodeError, ValueError):
|
| 972 |
+
parsed_args = {}
|
| 973 |
+
blocks.append({
|
| 974 |
+
"type": "tool_use",
|
| 975 |
+
"id": _sanitize_tool_id(tc.get("id", "")),
|
| 976 |
+
"name": fn.get("name", ""),
|
| 977 |
+
"input": parsed_args,
|
| 978 |
+
})
|
| 979 |
+
# Anthropic rejects empty assistant content
|
| 980 |
+
effective = blocks or content
|
| 981 |
+
if not effective or effective == "":
|
| 982 |
+
effective = [{"type": "text", "text": "(empty)"}]
|
| 983 |
+
result.append({"role": "assistant", "content": effective})
|
| 984 |
+
continue
|
| 985 |
+
|
| 986 |
+
if role == "tool":
|
| 987 |
+
# Sanitize tool_use_id and ensure non-empty content
|
| 988 |
+
result_content = content if isinstance(content, str) else json.dumps(content)
|
| 989 |
+
if not result_content:
|
| 990 |
+
result_content = "(no output)"
|
| 991 |
+
tool_result = {
|
| 992 |
+
"type": "tool_result",
|
| 993 |
+
"tool_use_id": _sanitize_tool_id(m.get("tool_call_id", "")),
|
| 994 |
+
"content": result_content,
|
| 995 |
+
}
|
| 996 |
+
if isinstance(m.get("cache_control"), dict):
|
| 997 |
+
tool_result["cache_control"] = dict(m["cache_control"])
|
| 998 |
+
# Merge consecutive tool results into one user message
|
| 999 |
+
if (
|
| 1000 |
+
result
|
| 1001 |
+
and result[-1]["role"] == "user"
|
| 1002 |
+
and isinstance(result[-1]["content"], list)
|
| 1003 |
+
and result[-1]["content"]
|
| 1004 |
+
and result[-1]["content"][0].get("type") == "tool_result"
|
| 1005 |
+
):
|
| 1006 |
+
result[-1]["content"].append(tool_result)
|
| 1007 |
+
else:
|
| 1008 |
+
result.append({"role": "user", "content": [tool_result]})
|
| 1009 |
+
continue
|
| 1010 |
+
|
| 1011 |
+
# Regular user message — validate non-empty content (Anthropic rejects empty)
|
| 1012 |
+
if isinstance(content, list):
|
| 1013 |
+
converted_blocks = _convert_content_to_anthropic(content)
|
| 1014 |
+
# Check if all text blocks are empty
|
| 1015 |
+
if not converted_blocks or all(
|
| 1016 |
+
b.get("text", "").strip() == ""
|
| 1017 |
+
for b in converted_blocks
|
| 1018 |
+
if isinstance(b, dict) and b.get("type") == "text"
|
| 1019 |
+
):
|
| 1020 |
+
converted_blocks = [{"type": "text", "text": "(empty message)"}]
|
| 1021 |
+
result.append({"role": "user", "content": converted_blocks})
|
| 1022 |
+
else:
|
| 1023 |
+
# Validate string content is non-empty
|
| 1024 |
+
if not content or (isinstance(content, str) and not content.strip()):
|
| 1025 |
+
content = "(empty message)"
|
| 1026 |
+
result.append({"role": "user", "content": content})
|
| 1027 |
+
|
| 1028 |
+
# Strip orphaned tool_use blocks (no matching tool_result follows)
|
| 1029 |
+
tool_result_ids = set()
|
| 1030 |
+
for m in result:
|
| 1031 |
+
if m["role"] == "user" and isinstance(m["content"], list):
|
| 1032 |
+
for block in m["content"]:
|
| 1033 |
+
if block.get("type") == "tool_result":
|
| 1034 |
+
tool_result_ids.add(block.get("tool_use_id"))
|
| 1035 |
+
for m in result:
|
| 1036 |
+
if m["role"] == "assistant" and isinstance(m["content"], list):
|
| 1037 |
+
m["content"] = [
|
| 1038 |
+
b
|
| 1039 |
+
for b in m["content"]
|
| 1040 |
+
if b.get("type") != "tool_use" or b.get("id") in tool_result_ids
|
| 1041 |
+
]
|
| 1042 |
+
if not m["content"]:
|
| 1043 |
+
m["content"] = [{"type": "text", "text": "(tool call removed)"}]
|
| 1044 |
+
|
| 1045 |
+
# Strip orphaned tool_result blocks (no matching tool_use precedes them).
|
| 1046 |
+
# This is the mirror of the above: context compression or session truncation
|
| 1047 |
+
# can remove an assistant message containing a tool_use while leaving the
|
| 1048 |
+
# subsequent tool_result intact. Anthropic rejects these with a 400.
|
| 1049 |
+
tool_use_ids = set()
|
| 1050 |
+
for m in result:
|
| 1051 |
+
if m["role"] == "assistant" and isinstance(m["content"], list):
|
| 1052 |
+
for block in m["content"]:
|
| 1053 |
+
if block.get("type") == "tool_use":
|
| 1054 |
+
tool_use_ids.add(block.get("id"))
|
| 1055 |
+
for m in result:
|
| 1056 |
+
if m["role"] == "user" and isinstance(m["content"], list):
|
| 1057 |
+
m["content"] = [
|
| 1058 |
+
b
|
| 1059 |
+
for b in m["content"]
|
| 1060 |
+
if b.get("type") != "tool_result" or b.get("tool_use_id") in tool_use_ids
|
| 1061 |
+
]
|
| 1062 |
+
if not m["content"]:
|
| 1063 |
+
m["content"] = [{"type": "text", "text": "(tool result removed)"}]
|
| 1064 |
+
|
| 1065 |
+
# Enforce strict role alternation (Anthropic rejects consecutive same-role messages)
|
| 1066 |
+
fixed = []
|
| 1067 |
+
for m in result:
|
| 1068 |
+
if fixed and fixed[-1]["role"] == m["role"]:
|
| 1069 |
+
if m["role"] == "user":
|
| 1070 |
+
# Merge consecutive user messages
|
| 1071 |
+
prev_content = fixed[-1]["content"]
|
| 1072 |
+
curr_content = m["content"]
|
| 1073 |
+
if isinstance(prev_content, str) and isinstance(curr_content, str):
|
| 1074 |
+
fixed[-1]["content"] = prev_content + "\n" + curr_content
|
| 1075 |
+
elif isinstance(prev_content, list) and isinstance(curr_content, list):
|
| 1076 |
+
fixed[-1]["content"] = prev_content + curr_content
|
| 1077 |
+
else:
|
| 1078 |
+
# Mixed types — wrap string in list
|
| 1079 |
+
if isinstance(prev_content, str):
|
| 1080 |
+
prev_content = [{"type": "text", "text": prev_content}]
|
| 1081 |
+
if isinstance(curr_content, str):
|
| 1082 |
+
curr_content = [{"type": "text", "text": curr_content}]
|
| 1083 |
+
fixed[-1]["content"] = prev_content + curr_content
|
| 1084 |
+
else:
|
| 1085 |
+
# Consecutive assistant messages — merge text content.
|
| 1086 |
+
# Drop thinking blocks from the *second* message: their
|
| 1087 |
+
# signature was computed against a different turn boundary
|
| 1088 |
+
# and becomes invalid once merged.
|
| 1089 |
+
if isinstance(m["content"], list):
|
| 1090 |
+
m["content"] = [
|
| 1091 |
+
b for b in m["content"]
|
| 1092 |
+
if not (isinstance(b, dict) and b.get("type") in ("thinking", "redacted_thinking"))
|
| 1093 |
+
]
|
| 1094 |
+
prev_blocks = fixed[-1]["content"]
|
| 1095 |
+
curr_blocks = m["content"]
|
| 1096 |
+
if isinstance(prev_blocks, list) and isinstance(curr_blocks, list):
|
| 1097 |
+
fixed[-1]["content"] = prev_blocks + curr_blocks
|
| 1098 |
+
elif isinstance(prev_blocks, str) and isinstance(curr_blocks, str):
|
| 1099 |
+
fixed[-1]["content"] = prev_blocks + "\n" + curr_blocks
|
| 1100 |
+
else:
|
| 1101 |
+
# Mixed types — normalize both to list and merge
|
| 1102 |
+
if isinstance(prev_blocks, str):
|
| 1103 |
+
prev_blocks = [{"type": "text", "text": prev_blocks}]
|
| 1104 |
+
if isinstance(curr_blocks, str):
|
| 1105 |
+
curr_blocks = [{"type": "text", "text": curr_blocks}]
|
| 1106 |
+
fixed[-1]["content"] = prev_blocks + curr_blocks
|
| 1107 |
+
else:
|
| 1108 |
+
fixed.append(m)
|
| 1109 |
+
result = fixed
|
| 1110 |
+
|
| 1111 |
+
# ── Thinking block signature management ──────────────────────────
|
| 1112 |
+
# Anthropic signs thinking blocks against the full turn content.
|
| 1113 |
+
# Any upstream mutation (context compression, session truncation,
|
| 1114 |
+
# orphan stripping, message merging) invalidates the signature,
|
| 1115 |
+
# causing HTTP 400 "Invalid signature in thinking block".
|
| 1116 |
+
#
|
| 1117 |
+
# Signatures are Anthropic-proprietary. Third-party endpoints
|
| 1118 |
+
# (MiniMax, Azure AI Foundry, self-hosted proxies) cannot validate
|
| 1119 |
+
# them and will reject them outright. When targeting a third-party
|
| 1120 |
+
# endpoint, strip ALL thinking/redacted_thinking blocks from every
|
| 1121 |
+
# assistant message — the third-party will generate its own
|
| 1122 |
+
# thinking blocks if it supports extended thinking.
|
| 1123 |
+
#
|
| 1124 |
+
# For direct Anthropic (strategy following clawdbot/OpenClaw):
|
| 1125 |
+
# 1. Strip thinking/redacted_thinking from all assistant messages
|
| 1126 |
+
# EXCEPT the last one — preserves reasoning continuity on the
|
| 1127 |
+
# current tool-use chain while avoiding stale signature errors.
|
| 1128 |
+
# 2. Downgrade unsigned thinking blocks (no signature) to text —
|
| 1129 |
+
# Anthropic can't validate them and will reject them.
|
| 1130 |
+
# 3. Strip cache_control from thinking/redacted_thinking blocks —
|
| 1131 |
+
# cache markers can interfere with signature validation.
|
| 1132 |
+
_THINKING_TYPES = frozenset(("thinking", "redacted_thinking"))
|
| 1133 |
+
_is_third_party = _is_third_party_anthropic_endpoint(base_url)
|
| 1134 |
+
|
| 1135 |
+
last_assistant_idx = None
|
| 1136 |
+
for i in range(len(result) - 1, -1, -1):
|
| 1137 |
+
if result[i].get("role") == "assistant":
|
| 1138 |
+
last_assistant_idx = i
|
| 1139 |
+
break
|
| 1140 |
+
|
| 1141 |
+
for idx, m in enumerate(result):
|
| 1142 |
+
if m.get("role") != "assistant" or not isinstance(m.get("content"), list):
|
| 1143 |
+
continue
|
| 1144 |
+
|
| 1145 |
+
if _is_third_party or idx != last_assistant_idx:
|
| 1146 |
+
# Third-party endpoint: strip ALL thinking blocks from every
|
| 1147 |
+
# assistant message — signatures are Anthropic-proprietary.
|
| 1148 |
+
# Direct Anthropic: strip from non-latest assistant messages only.
|
| 1149 |
+
stripped = [
|
| 1150 |
+
b for b in m["content"]
|
| 1151 |
+
if not (isinstance(b, dict) and b.get("type") in _THINKING_TYPES)
|
| 1152 |
+
]
|
| 1153 |
+
m["content"] = stripped or [{"type": "text", "text": "(thinking elided)"}]
|
| 1154 |
+
else:
|
| 1155 |
+
# Latest assistant on direct Anthropic: keep signed thinking
|
| 1156 |
+
# blocks for reasoning continuity; downgrade unsigned ones to
|
| 1157 |
+
# plain text.
|
| 1158 |
+
new_content = []
|
| 1159 |
+
for b in m["content"]:
|
| 1160 |
+
if not isinstance(b, dict) or b.get("type") not in _THINKING_TYPES:
|
| 1161 |
+
new_content.append(b)
|
| 1162 |
+
continue
|
| 1163 |
+
if b.get("type") == "redacted_thinking":
|
| 1164 |
+
# Redacted blocks use 'data' for the signature payload
|
| 1165 |
+
if b.get("data"):
|
| 1166 |
+
new_content.append(b)
|
| 1167 |
+
# else: drop — no data means it can't be validated
|
| 1168 |
+
elif b.get("signature"):
|
| 1169 |
+
# Signed thinking block — keep it
|
| 1170 |
+
new_content.append(b)
|
| 1171 |
+
else:
|
| 1172 |
+
# Unsigned thinking — downgrade to text so it's not lost
|
| 1173 |
+
thinking_text = b.get("thinking", "")
|
| 1174 |
+
if thinking_text:
|
| 1175 |
+
new_content.append({"type": "text", "text": thinking_text})
|
| 1176 |
+
m["content"] = new_content or [{"type": "text", "text": "(empty)"}]
|
| 1177 |
+
|
| 1178 |
+
# Strip cache_control from any remaining thinking/redacted_thinking
|
| 1179 |
+
# blocks — cache markers interfere with signature validation.
|
| 1180 |
+
for b in m["content"]:
|
| 1181 |
+
if isinstance(b, dict) and b.get("type") in _THINKING_TYPES:
|
| 1182 |
+
b.pop("cache_control", None)
|
| 1183 |
+
|
| 1184 |
+
return system, result
|
| 1185 |
+
|
| 1186 |
+
|
| 1187 |
+
def build_anthropic_kwargs(
|
| 1188 |
+
model: str,
|
| 1189 |
+
messages: List[Dict],
|
| 1190 |
+
tools: Optional[List[Dict]],
|
| 1191 |
+
max_tokens: Optional[int],
|
| 1192 |
+
reasoning_config: Optional[Dict[str, Any]],
|
| 1193 |
+
tool_choice: Optional[str] = None,
|
| 1194 |
+
is_oauth: bool = False,
|
| 1195 |
+
preserve_dots: bool = False,
|
| 1196 |
+
context_length: Optional[int] = None,
|
| 1197 |
+
base_url: str | None = None,
|
| 1198 |
+
fast_mode: bool = False,
|
| 1199 |
+
) -> Dict[str, Any]:
|
| 1200 |
+
"""Build kwargs for anthropic.messages.create().
|
| 1201 |
+
|
| 1202 |
+
Naming note — two distinct concepts, easily confused:
|
| 1203 |
+
max_tokens = OUTPUT token cap for a single response.
|
| 1204 |
+
Anthropic's API calls this "max_tokens" but it only
|
| 1205 |
+
limits the *output*. Anthropic's own native SDK
|
| 1206 |
+
renamed it "max_output_tokens" for clarity.
|
| 1207 |
+
context_length = TOTAL context window (input tokens + output tokens).
|
| 1208 |
+
The API enforces: input_tokens + max_tokens ≤ context_length.
|
| 1209 |
+
Stored on the ContextCompressor; reduced on overflow errors.
|
| 1210 |
+
|
| 1211 |
+
When *max_tokens* is None the model's native output ceiling is used
|
| 1212 |
+
(e.g. 128K for Opus 4.6, 64K for Sonnet 4.6).
|
| 1213 |
+
|
| 1214 |
+
When *context_length* is provided and the model's native output ceiling
|
| 1215 |
+
exceeds it (e.g. a local endpoint with an 8K window), the output cap is
|
| 1216 |
+
clamped to context_length − 1. This only kicks in for unusually small
|
| 1217 |
+
context windows; for full-size models the native output cap is always
|
| 1218 |
+
smaller than the context window so no clamping happens.
|
| 1219 |
+
NOTE: this clamping does not account for prompt size — if the prompt is
|
| 1220 |
+
large, Anthropic may still reject the request. The caller must detect
|
| 1221 |
+
"max_tokens too large given prompt" errors and retry with a smaller cap
|
| 1222 |
+
(see parse_available_output_tokens_from_error + _ephemeral_max_output_tokens).
|
| 1223 |
+
|
| 1224 |
+
When *is_oauth* is True, applies Claude Code compatibility transforms:
|
| 1225 |
+
system prompt prefix, tool name prefixing, and prompt sanitization.
|
| 1226 |
+
|
| 1227 |
+
When *preserve_dots* is True, model name dots are not converted to hyphens
|
| 1228 |
+
(for Alibaba/DashScope anthropic-compatible endpoints: qwen3.5-plus).
|
| 1229 |
+
|
| 1230 |
+
When *base_url* points to a third-party Anthropic-compatible endpoint,
|
| 1231 |
+
thinking block signatures are stripped (they are Anthropic-proprietary).
|
| 1232 |
+
|
| 1233 |
+
When *fast_mode* is True, adds ``speed: "fast"`` and the fast-mode beta
|
| 1234 |
+
header for ~2.5x faster output throughput on Opus 4.6. Currently only
|
| 1235 |
+
supported on native Anthropic endpoints (not third-party compatible ones).
|
| 1236 |
+
"""
|
| 1237 |
+
system, anthropic_messages = convert_messages_to_anthropic(messages, base_url=base_url)
|
| 1238 |
+
anthropic_tools = convert_tools_to_anthropic(tools) if tools else []
|
| 1239 |
+
|
| 1240 |
+
model = normalize_model_name(model, preserve_dots=preserve_dots)
|
| 1241 |
+
# effective_max_tokens = output cap for this call (≠ total context window)
|
| 1242 |
+
effective_max_tokens = max_tokens or _get_anthropic_max_output(model)
|
| 1243 |
+
|
| 1244 |
+
# Clamp output cap to fit inside the total context window.
|
| 1245 |
+
# Only matters for small custom endpoints where context_length < native
|
| 1246 |
+
# output ceiling. For standard Anthropic models context_length (e.g.
|
| 1247 |
+
# 200K) is always larger than the output ceiling (e.g. 128K), so this
|
| 1248 |
+
# branch is not taken.
|
| 1249 |
+
if context_length and effective_max_tokens > context_length:
|
| 1250 |
+
effective_max_tokens = max(context_length - 1, 1)
|
| 1251 |
+
|
| 1252 |
+
# ── OAuth: Claude Code identity ──────────────────────────────────
|
| 1253 |
+
if is_oauth:
|
| 1254 |
+
# 1. Prepend Claude Code system prompt identity
|
| 1255 |
+
cc_block = {"type": "text", "text": _CLAUDE_CODE_SYSTEM_PREFIX}
|
| 1256 |
+
if isinstance(system, list):
|
| 1257 |
+
system = [cc_block] + system
|
| 1258 |
+
elif isinstance(system, str) and system:
|
| 1259 |
+
system = [cc_block, {"type": "text", "text": system}]
|
| 1260 |
+
else:
|
| 1261 |
+
system = [cc_block]
|
| 1262 |
+
|
| 1263 |
+
# 2. Sanitize system prompt — replace product name references
|
| 1264 |
+
# to avoid Anthropic's server-side content filters.
|
| 1265 |
+
for block in system:
|
| 1266 |
+
if isinstance(block, dict) and block.get("type") == "text":
|
| 1267 |
+
text = block.get("text", "")
|
| 1268 |
+
text = text.replace("Hermes Agent", "Claude Code")
|
| 1269 |
+
text = text.replace("Hermes agent", "Claude Code")
|
| 1270 |
+
text = text.replace("hermes-agent", "claude-code")
|
| 1271 |
+
text = text.replace("Nous Research", "Anthropic")
|
| 1272 |
+
block["text"] = text
|
| 1273 |
+
|
| 1274 |
+
# 3. Prefix tool names with mcp_ (Claude Code convention)
|
| 1275 |
+
if anthropic_tools:
|
| 1276 |
+
for tool in anthropic_tools:
|
| 1277 |
+
if "name" in tool:
|
| 1278 |
+
tool["name"] = _MCP_TOOL_PREFIX + tool["name"]
|
| 1279 |
+
|
| 1280 |
+
# 4. Prefix tool names in message history (tool_use and tool_result blocks)
|
| 1281 |
+
for msg in anthropic_messages:
|
| 1282 |
+
content = msg.get("content")
|
| 1283 |
+
if isinstance(content, list):
|
| 1284 |
+
for block in content:
|
| 1285 |
+
if isinstance(block, dict):
|
| 1286 |
+
if block.get("type") == "tool_use" and "name" in block:
|
| 1287 |
+
if not block["name"].startswith(_MCP_TOOL_PREFIX):
|
| 1288 |
+
block["name"] = _MCP_TOOL_PREFIX + block["name"]
|
| 1289 |
+
elif block.get("type") == "tool_result" and "tool_use_id" in block:
|
| 1290 |
+
pass # tool_result uses ID, not name
|
| 1291 |
+
|
| 1292 |
+
kwargs: Dict[str, Any] = {
|
| 1293 |
+
"model": model,
|
| 1294 |
+
"messages": anthropic_messages,
|
| 1295 |
+
"max_tokens": effective_max_tokens,
|
| 1296 |
+
}
|
| 1297 |
+
|
| 1298 |
+
if system:
|
| 1299 |
+
kwargs["system"] = system
|
| 1300 |
+
|
| 1301 |
+
if anthropic_tools:
|
| 1302 |
+
kwargs["tools"] = anthropic_tools
|
| 1303 |
+
# Map OpenAI tool_choice to Anthropic format
|
| 1304 |
+
if tool_choice == "auto" or tool_choice is None:
|
| 1305 |
+
kwargs["tool_choice"] = {"type": "auto"}
|
| 1306 |
+
elif tool_choice == "required":
|
| 1307 |
+
kwargs["tool_choice"] = {"type": "any"}
|
| 1308 |
+
elif tool_choice == "none":
|
| 1309 |
+
# Anthropic has no tool_choice "none" — omit tools entirely to prevent use
|
| 1310 |
+
kwargs.pop("tools", None)
|
| 1311 |
+
elif isinstance(tool_choice, str):
|
| 1312 |
+
# Specific tool name
|
| 1313 |
+
kwargs["tool_choice"] = {"type": "tool", "name": tool_choice}
|
| 1314 |
+
|
| 1315 |
+
# Map reasoning_config to Anthropic's thinking parameter.
|
| 1316 |
+
# Claude 4.6 models use adaptive thinking + output_config.effort.
|
| 1317 |
+
# Older models use manual thinking with budget_tokens.
|
| 1318 |
+
# MiniMax Anthropic-compat endpoints support thinking (manual mode only,
|
| 1319 |
+
# not adaptive). Haiku does NOT support extended thinking — skip entirely.
|
| 1320 |
+
if reasoning_config and isinstance(reasoning_config, dict):
|
| 1321 |
+
if reasoning_config.get("enabled") is not False and "haiku" not in model.lower():
|
| 1322 |
+
effort = str(reasoning_config.get("effort", "medium")).lower()
|
| 1323 |
+
budget = THINKING_BUDGET.get(effort, 8000)
|
| 1324 |
+
if _supports_adaptive_thinking(model):
|
| 1325 |
+
kwargs["thinking"] = {"type": "adaptive"}
|
| 1326 |
+
kwargs["output_config"] = {
|
| 1327 |
+
"effort": ADAPTIVE_EFFORT_MAP.get(effort, "medium")
|
| 1328 |
+
}
|
| 1329 |
+
else:
|
| 1330 |
+
kwargs["thinking"] = {"type": "enabled", "budget_tokens": budget}
|
| 1331 |
+
# Anthropic requires temperature=1 when thinking is enabled on older models
|
| 1332 |
+
kwargs["temperature"] = 1
|
| 1333 |
+
kwargs["max_tokens"] = max(effective_max_tokens, budget + 4096)
|
| 1334 |
+
|
| 1335 |
+
# ── Fast mode (Opus 4.6 only) ────────────────────────────────────
|
| 1336 |
+
# Adds speed:"fast" + the fast-mode beta header for ~2.5x output speed.
|
| 1337 |
+
# Only for native Anthropic endpoints — third-party providers would
|
| 1338 |
+
# reject the unknown beta header and speed parameter.
|
| 1339 |
+
if fast_mode and not _is_third_party_anthropic_endpoint(base_url):
|
| 1340 |
+
kwargs["speed"] = "fast"
|
| 1341 |
+
# Build extra_headers with ALL applicable betas (the per-request
|
| 1342 |
+
# extra_headers override the client-level anthropic-beta header).
|
| 1343 |
+
betas = list(_common_betas_for_base_url(base_url))
|
| 1344 |
+
if is_oauth:
|
| 1345 |
+
betas.extend(_OAUTH_ONLY_BETAS)
|
| 1346 |
+
betas.append(_FAST_MODE_BETA)
|
| 1347 |
+
kwargs["extra_headers"] = {"anthropic-beta": ",".join(betas)}
|
| 1348 |
+
|
| 1349 |
+
return kwargs
|
| 1350 |
+
|
| 1351 |
+
|
| 1352 |
+
def normalize_anthropic_response(
|
| 1353 |
+
response,
|
| 1354 |
+
strip_tool_prefix: bool = False,
|
| 1355 |
+
) -> Tuple[SimpleNamespace, str]:
|
| 1356 |
+
"""Normalize Anthropic response to match the shape expected by AIAgent.
|
| 1357 |
+
|
| 1358 |
+
Returns (assistant_message, finish_reason) where assistant_message has
|
| 1359 |
+
.content, .tool_calls, and .reasoning attributes.
|
| 1360 |
+
|
| 1361 |
+
When *strip_tool_prefix* is True, removes the ``mcp_`` prefix that was
|
| 1362 |
+
added to tool names for OAuth Claude Code compatibility.
|
| 1363 |
+
"""
|
| 1364 |
+
text_parts = []
|
| 1365 |
+
reasoning_parts = []
|
| 1366 |
+
reasoning_details = []
|
| 1367 |
+
tool_calls = []
|
| 1368 |
+
|
| 1369 |
+
for block in response.content:
|
| 1370 |
+
if block.type == "text":
|
| 1371 |
+
text_parts.append(block.text)
|
| 1372 |
+
elif block.type == "thinking":
|
| 1373 |
+
reasoning_parts.append(block.thinking)
|
| 1374 |
+
block_dict = _to_plain_data(block)
|
| 1375 |
+
if isinstance(block_dict, dict):
|
| 1376 |
+
reasoning_details.append(block_dict)
|
| 1377 |
+
elif block.type == "tool_use":
|
| 1378 |
+
name = block.name
|
| 1379 |
+
if strip_tool_prefix and name.startswith(_MCP_TOOL_PREFIX):
|
| 1380 |
+
name = name[len(_MCP_TOOL_PREFIX):]
|
| 1381 |
+
tool_calls.append(
|
| 1382 |
+
SimpleNamespace(
|
| 1383 |
+
id=block.id,
|
| 1384 |
+
type="function",
|
| 1385 |
+
function=SimpleNamespace(
|
| 1386 |
+
name=name,
|
| 1387 |
+
arguments=json.dumps(block.input),
|
| 1388 |
+
),
|
| 1389 |
+
)
|
| 1390 |
+
)
|
| 1391 |
+
|
| 1392 |
+
# Map Anthropic stop_reason to OpenAI finish_reason
|
| 1393 |
+
stop_reason_map = {
|
| 1394 |
+
"end_turn": "stop",
|
| 1395 |
+
"tool_use": "tool_calls",
|
| 1396 |
+
"max_tokens": "length",
|
| 1397 |
+
"stop_sequence": "stop",
|
| 1398 |
+
}
|
| 1399 |
+
finish_reason = stop_reason_map.get(response.stop_reason, "stop")
|
| 1400 |
+
|
| 1401 |
+
return (
|
| 1402 |
+
SimpleNamespace(
|
| 1403 |
+
content="\n".join(text_parts) if text_parts else None,
|
| 1404 |
+
tool_calls=tool_calls or None,
|
| 1405 |
+
reasoning="\n\n".join(reasoning_parts) if reasoning_parts else None,
|
| 1406 |
+
reasoning_content=None,
|
| 1407 |
+
reasoning_details=reasoning_details or None,
|
| 1408 |
+
),
|
| 1409 |
+
finish_reason,
|
| 1410 |
+
)
|
agent/auxiliary_client.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
agent/context_compressor.py
ADDED
|
@@ -0,0 +1,809 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Automatic context window compression for long conversations.
|
| 2 |
+
|
| 3 |
+
Self-contained class with its own OpenAI client for summarization.
|
| 4 |
+
Uses auxiliary model (cheap/fast) to summarize middle turns while
|
| 5 |
+
protecting head and tail context.
|
| 6 |
+
|
| 7 |
+
Improvements over v2:
|
| 8 |
+
- Structured summary template with Resolved/Pending question tracking
|
| 9 |
+
- Summarizer preamble: "Do not respond to any questions" (from OpenCode)
|
| 10 |
+
- Handoff framing: "different assistant" (from Codex) to create separation
|
| 11 |
+
- "Remaining Work" replaces "Next Steps" to avoid reading as active instructions
|
| 12 |
+
- Clear separator when summary merges into tail message
|
| 13 |
+
- Iterative summary updates (preserves info across multiple compactions)
|
| 14 |
+
- Token-budget tail protection instead of fixed message count
|
| 15 |
+
- Tool output pruning before LLM summarization (cheap pre-pass)
|
| 16 |
+
- Scaled summary budget (proportional to compressed content)
|
| 17 |
+
- Richer tool call/result detail in summarizer input
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
import logging
|
| 21 |
+
import time
|
| 22 |
+
from typing import Any, Dict, List, Optional
|
| 23 |
+
|
| 24 |
+
from agent.auxiliary_client import call_llm
|
| 25 |
+
from agent.context_engine import ContextEngine
|
| 26 |
+
from agent.model_metadata import (
|
| 27 |
+
MINIMUM_CONTEXT_LENGTH,
|
| 28 |
+
get_model_context_length,
|
| 29 |
+
estimate_messages_tokens_rough,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
logger = logging.getLogger(__name__)
|
| 33 |
+
|
| 34 |
+
SUMMARY_PREFIX = (
|
| 35 |
+
"[CONTEXT COMPACTION — REFERENCE ONLY] Earlier turns were compacted "
|
| 36 |
+
"into the summary below. This is a handoff from a previous context "
|
| 37 |
+
"window — treat it as background reference, NOT as active instructions. "
|
| 38 |
+
"Do NOT answer questions or fulfill requests mentioned in this summary; "
|
| 39 |
+
"they were already addressed. Respond ONLY to the latest user message "
|
| 40 |
+
"that appears AFTER this summary. The current session state (files, "
|
| 41 |
+
"config, etc.) may reflect work described here — avoid repeating it:"
|
| 42 |
+
)
|
| 43 |
+
LEGACY_SUMMARY_PREFIX = "[CONTEXT SUMMARY]:"
|
| 44 |
+
|
| 45 |
+
# Minimum tokens for the summary output
|
| 46 |
+
_MIN_SUMMARY_TOKENS = 2000
|
| 47 |
+
# Proportion of compressed content to allocate for summary
|
| 48 |
+
_SUMMARY_RATIO = 0.20
|
| 49 |
+
# Absolute ceiling for summary tokens (even on very large context windows)
|
| 50 |
+
_SUMMARY_TOKENS_CEILING = 12_000
|
| 51 |
+
|
| 52 |
+
# Placeholder used when pruning old tool results
|
| 53 |
+
_PRUNED_TOOL_PLACEHOLDER = "[Old tool output cleared to save context space]"
|
| 54 |
+
|
| 55 |
+
# Chars per token rough estimate
|
| 56 |
+
_CHARS_PER_TOKEN = 4
|
| 57 |
+
_SUMMARY_FAILURE_COOLDOWN_SECONDS = 600
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class ContextCompressor(ContextEngine):
|
| 61 |
+
"""Default context engine — compresses conversation context via lossy summarization.
|
| 62 |
+
|
| 63 |
+
Algorithm:
|
| 64 |
+
1. Prune old tool results (cheap, no LLM call)
|
| 65 |
+
2. Protect head messages (system prompt + first exchange)
|
| 66 |
+
3. Protect tail messages by token budget (most recent ~20K tokens)
|
| 67 |
+
4. Summarize middle turns with structured LLM prompt
|
| 68 |
+
5. On subsequent compactions, iteratively update the previous summary
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
@property
|
| 72 |
+
def name(self) -> str:
|
| 73 |
+
return "compressor"
|
| 74 |
+
|
| 75 |
+
def on_session_reset(self) -> None:
|
| 76 |
+
"""Reset all per-session state for /new or /reset."""
|
| 77 |
+
super().on_session_reset()
|
| 78 |
+
self._context_probed = False
|
| 79 |
+
self._context_probe_persistable = False
|
| 80 |
+
self._previous_summary = None
|
| 81 |
+
|
| 82 |
+
def update_model(
|
| 83 |
+
self,
|
| 84 |
+
model: str,
|
| 85 |
+
context_length: int,
|
| 86 |
+
base_url: str = "",
|
| 87 |
+
api_key: str = "",
|
| 88 |
+
provider: str = "",
|
| 89 |
+
) -> None:
|
| 90 |
+
"""Update model info after a model switch or fallback activation."""
|
| 91 |
+
self.model = model
|
| 92 |
+
self.base_url = base_url
|
| 93 |
+
self.api_key = api_key
|
| 94 |
+
self.provider = provider
|
| 95 |
+
self.context_length = context_length
|
| 96 |
+
self.threshold_tokens = max(
|
| 97 |
+
int(context_length * self.threshold_percent),
|
| 98 |
+
MINIMUM_CONTEXT_LENGTH,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
def __init__(
|
| 102 |
+
self,
|
| 103 |
+
model: str,
|
| 104 |
+
threshold_percent: float = 0.50,
|
| 105 |
+
protect_first_n: int = 3,
|
| 106 |
+
protect_last_n: int = 20,
|
| 107 |
+
summary_target_ratio: float = 0.20,
|
| 108 |
+
quiet_mode: bool = False,
|
| 109 |
+
summary_model_override: str = None,
|
| 110 |
+
base_url: str = "",
|
| 111 |
+
api_key: str = "",
|
| 112 |
+
config_context_length: int | None = None,
|
| 113 |
+
provider: str = "",
|
| 114 |
+
):
|
| 115 |
+
self.model = model
|
| 116 |
+
self.base_url = base_url
|
| 117 |
+
self.api_key = api_key
|
| 118 |
+
self.provider = provider
|
| 119 |
+
self.threshold_percent = threshold_percent
|
| 120 |
+
self.protect_first_n = protect_first_n
|
| 121 |
+
self.protect_last_n = protect_last_n
|
| 122 |
+
self.summary_target_ratio = max(0.10, min(summary_target_ratio, 0.80))
|
| 123 |
+
self.quiet_mode = quiet_mode
|
| 124 |
+
|
| 125 |
+
self.context_length = get_model_context_length(
|
| 126 |
+
model, base_url=base_url, api_key=api_key,
|
| 127 |
+
config_context_length=config_context_length,
|
| 128 |
+
provider=provider,
|
| 129 |
+
)
|
| 130 |
+
# Floor: never compress below MINIMUM_CONTEXT_LENGTH tokens even if
|
| 131 |
+
# the percentage would suggest a lower value. This prevents premature
|
| 132 |
+
# compression on large-context models at 50% while keeping the % sane
|
| 133 |
+
# for models right at the minimum.
|
| 134 |
+
self.threshold_tokens = max(
|
| 135 |
+
int(self.context_length * threshold_percent),
|
| 136 |
+
MINIMUM_CONTEXT_LENGTH,
|
| 137 |
+
)
|
| 138 |
+
self.compression_count = 0
|
| 139 |
+
|
| 140 |
+
# Derive token budgets: ratio is relative to the threshold, not total context
|
| 141 |
+
target_tokens = int(self.threshold_tokens * self.summary_target_ratio)
|
| 142 |
+
self.tail_token_budget = target_tokens
|
| 143 |
+
self.max_summary_tokens = min(
|
| 144 |
+
int(self.context_length * 0.05), _SUMMARY_TOKENS_CEILING,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
if not quiet_mode:
|
| 148 |
+
logger.info(
|
| 149 |
+
"Context compressor initialized: model=%s context_length=%d "
|
| 150 |
+
"threshold=%d (%.0f%%) target_ratio=%.0f%% tail_budget=%d "
|
| 151 |
+
"provider=%s base_url=%s",
|
| 152 |
+
model, self.context_length, self.threshold_tokens,
|
| 153 |
+
threshold_percent * 100, self.summary_target_ratio * 100,
|
| 154 |
+
self.tail_token_budget,
|
| 155 |
+
provider or "none", base_url or "none",
|
| 156 |
+
)
|
| 157 |
+
self._context_probed = False # True after a step-down from context error
|
| 158 |
+
|
| 159 |
+
self.last_prompt_tokens = 0
|
| 160 |
+
self.last_completion_tokens = 0
|
| 161 |
+
|
| 162 |
+
self.summary_model = summary_model_override or ""
|
| 163 |
+
|
| 164 |
+
# Stores the previous compaction summary for iterative updates
|
| 165 |
+
self._previous_summary: Optional[str] = None
|
| 166 |
+
self._summary_failure_cooldown_until: float = 0.0
|
| 167 |
+
|
| 168 |
+
def update_from_response(self, usage: Dict[str, Any]):
|
| 169 |
+
"""Update tracked token usage from API response."""
|
| 170 |
+
self.last_prompt_tokens = usage.get("prompt_tokens", 0)
|
| 171 |
+
self.last_completion_tokens = usage.get("completion_tokens", 0)
|
| 172 |
+
|
| 173 |
+
def should_compress(self, prompt_tokens: int = None) -> bool:
|
| 174 |
+
"""Check if context exceeds the compression threshold."""
|
| 175 |
+
tokens = prompt_tokens if prompt_tokens is not None else self.last_prompt_tokens
|
| 176 |
+
return tokens >= self.threshold_tokens
|
| 177 |
+
|
| 178 |
+
# ------------------------------------------------------------------
|
| 179 |
+
# Tool output pruning (cheap pre-pass, no LLM call)
|
| 180 |
+
# ------------------------------------------------------------------
|
| 181 |
+
|
| 182 |
+
def _prune_old_tool_results(
|
| 183 |
+
self, messages: List[Dict[str, Any]], protect_tail_count: int,
|
| 184 |
+
protect_tail_tokens: int | None = None,
|
| 185 |
+
) -> tuple[List[Dict[str, Any]], int]:
|
| 186 |
+
"""Replace old tool result contents with a short placeholder.
|
| 187 |
+
|
| 188 |
+
Walks backward from the end, protecting the most recent messages that
|
| 189 |
+
fall within ``protect_tail_tokens`` (when provided) OR the last
|
| 190 |
+
``protect_tail_count`` messages (backward-compatible default).
|
| 191 |
+
When both are given, the token budget takes priority and the message
|
| 192 |
+
count acts as a hard minimum floor.
|
| 193 |
+
|
| 194 |
+
Returns (pruned_messages, pruned_count).
|
| 195 |
+
"""
|
| 196 |
+
if not messages:
|
| 197 |
+
return messages, 0
|
| 198 |
+
|
| 199 |
+
result = [m.copy() for m in messages]
|
| 200 |
+
pruned = 0
|
| 201 |
+
|
| 202 |
+
# Determine the prune boundary
|
| 203 |
+
if protect_tail_tokens is not None and protect_tail_tokens > 0:
|
| 204 |
+
# Token-budget approach: walk backward accumulating tokens
|
| 205 |
+
accumulated = 0
|
| 206 |
+
boundary = len(result)
|
| 207 |
+
min_protect = min(protect_tail_count, len(result) - 1)
|
| 208 |
+
for i in range(len(result) - 1, -1, -1):
|
| 209 |
+
msg = result[i]
|
| 210 |
+
content_len = len(msg.get("content") or "")
|
| 211 |
+
msg_tokens = content_len // _CHARS_PER_TOKEN + 10
|
| 212 |
+
for tc in msg.get("tool_calls") or []:
|
| 213 |
+
if isinstance(tc, dict):
|
| 214 |
+
args = tc.get("function", {}).get("arguments", "")
|
| 215 |
+
msg_tokens += len(args) // _CHARS_PER_TOKEN
|
| 216 |
+
if accumulated + msg_tokens > protect_tail_tokens and (len(result) - i) >= min_protect:
|
| 217 |
+
boundary = i
|
| 218 |
+
break
|
| 219 |
+
accumulated += msg_tokens
|
| 220 |
+
boundary = i
|
| 221 |
+
prune_boundary = max(boundary, len(result) - min_protect)
|
| 222 |
+
else:
|
| 223 |
+
prune_boundary = len(result) - protect_tail_count
|
| 224 |
+
|
| 225 |
+
for i in range(prune_boundary):
|
| 226 |
+
msg = result[i]
|
| 227 |
+
if msg.get("role") != "tool":
|
| 228 |
+
continue
|
| 229 |
+
content = msg.get("content", "")
|
| 230 |
+
if not content or content == _PRUNED_TOOL_PLACEHOLDER:
|
| 231 |
+
continue
|
| 232 |
+
# Only prune if the content is substantial (>200 chars)
|
| 233 |
+
if len(content) > 200:
|
| 234 |
+
result[i] = {**msg, "content": _PRUNED_TOOL_PLACEHOLDER}
|
| 235 |
+
pruned += 1
|
| 236 |
+
|
| 237 |
+
return result, pruned
|
| 238 |
+
|
| 239 |
+
# ------------------------------------------------------------------
|
| 240 |
+
# Summarization
|
| 241 |
+
# ------------------------------------------------------------------
|
| 242 |
+
|
| 243 |
+
def _compute_summary_budget(self, turns_to_summarize: List[Dict[str, Any]]) -> int:
|
| 244 |
+
"""Scale summary token budget with the amount of content being compressed.
|
| 245 |
+
|
| 246 |
+
The maximum scales with the model's context window (5% of context,
|
| 247 |
+
capped at ``_SUMMARY_TOKENS_CEILING``) so large-context models get
|
| 248 |
+
richer summaries instead of being hard-capped at 8K tokens.
|
| 249 |
+
"""
|
| 250 |
+
content_tokens = estimate_messages_tokens_rough(turns_to_summarize)
|
| 251 |
+
budget = int(content_tokens * _SUMMARY_RATIO)
|
| 252 |
+
return max(_MIN_SUMMARY_TOKENS, min(budget, self.max_summary_tokens))
|
| 253 |
+
|
| 254 |
+
# Truncation limits for the summarizer input. These bound how much of
|
| 255 |
+
# each message the summary model sees — the budget is the *summary*
|
| 256 |
+
# model's context window, not the main model's.
|
| 257 |
+
_CONTENT_MAX = 6000 # total chars per message body
|
| 258 |
+
_CONTENT_HEAD = 4000 # chars kept from the start
|
| 259 |
+
_CONTENT_TAIL = 1500 # chars kept from the end
|
| 260 |
+
_TOOL_ARGS_MAX = 1500 # tool call argument chars
|
| 261 |
+
_TOOL_ARGS_HEAD = 1200 # kept from the start of tool args
|
| 262 |
+
|
| 263 |
+
def _serialize_for_summary(self, turns: List[Dict[str, Any]]) -> str:
|
| 264 |
+
"""Serialize conversation turns into labeled text for the summarizer.
|
| 265 |
+
|
| 266 |
+
Includes tool call arguments and result content (up to
|
| 267 |
+
``_CONTENT_MAX`` chars per message) so the summarizer can preserve
|
| 268 |
+
specific details like file paths, commands, and outputs.
|
| 269 |
+
"""
|
| 270 |
+
parts = []
|
| 271 |
+
for msg in turns:
|
| 272 |
+
role = msg.get("role", "unknown")
|
| 273 |
+
content = msg.get("content") or ""
|
| 274 |
+
|
| 275 |
+
# Tool results: keep enough content for the summarizer
|
| 276 |
+
if role == "tool":
|
| 277 |
+
tool_id = msg.get("tool_call_id", "")
|
| 278 |
+
if len(content) > self._CONTENT_MAX:
|
| 279 |
+
content = content[:self._CONTENT_HEAD] + "\n...[truncated]...\n" + content[-self._CONTENT_TAIL:]
|
| 280 |
+
parts.append(f"[TOOL RESULT {tool_id}]: {content}")
|
| 281 |
+
continue
|
| 282 |
+
|
| 283 |
+
# Assistant messages: include tool call names AND arguments
|
| 284 |
+
if role == "assistant":
|
| 285 |
+
if len(content) > self._CONTENT_MAX:
|
| 286 |
+
content = content[:self._CONTENT_HEAD] + "\n...[truncated]...\n" + content[-self._CONTENT_TAIL:]
|
| 287 |
+
tool_calls = msg.get("tool_calls", [])
|
| 288 |
+
if tool_calls:
|
| 289 |
+
tc_parts = []
|
| 290 |
+
for tc in tool_calls:
|
| 291 |
+
if isinstance(tc, dict):
|
| 292 |
+
fn = tc.get("function", {})
|
| 293 |
+
name = fn.get("name", "?")
|
| 294 |
+
args = fn.get("arguments", "")
|
| 295 |
+
# Truncate long arguments but keep enough for context
|
| 296 |
+
if len(args) > self._TOOL_ARGS_MAX:
|
| 297 |
+
args = args[:self._TOOL_ARGS_HEAD] + "..."
|
| 298 |
+
tc_parts.append(f" {name}({args})")
|
| 299 |
+
else:
|
| 300 |
+
fn = getattr(tc, "function", None)
|
| 301 |
+
name = getattr(fn, "name", "?") if fn else "?"
|
| 302 |
+
tc_parts.append(f" {name}(...)")
|
| 303 |
+
content += "\n[Tool calls:\n" + "\n".join(tc_parts) + "\n]"
|
| 304 |
+
parts.append(f"[ASSISTANT]: {content}")
|
| 305 |
+
continue
|
| 306 |
+
|
| 307 |
+
# User and other roles
|
| 308 |
+
if len(content) > self._CONTENT_MAX:
|
| 309 |
+
content = content[:self._CONTENT_HEAD] + "\n...[truncated]...\n" + content[-self._CONTENT_TAIL:]
|
| 310 |
+
parts.append(f"[{role.upper()}]: {content}")
|
| 311 |
+
|
| 312 |
+
return "\n\n".join(parts)
|
| 313 |
+
|
| 314 |
+
def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topic: str = None) -> Optional[str]:
|
| 315 |
+
"""Generate a structured summary of conversation turns.
|
| 316 |
+
|
| 317 |
+
Uses a structured template (Goal, Progress, Decisions, Resolved/Pending
|
| 318 |
+
Questions, Files, Remaining Work) with explicit preamble telling the
|
| 319 |
+
summarizer not to answer questions. When a previous summary exists,
|
| 320 |
+
generates an iterative update instead of summarizing from scratch.
|
| 321 |
+
|
| 322 |
+
Args:
|
| 323 |
+
focus_topic: Optional focus string for guided compression. When
|
| 324 |
+
provided, the summariser prioritises preserving information
|
| 325 |
+
related to this topic and is more aggressive about compressing
|
| 326 |
+
everything else. Inspired by Claude Code's ``/compact``.
|
| 327 |
+
|
| 328 |
+
Returns None if all attempts fail — the caller should drop
|
| 329 |
+
the middle turns without a summary rather than inject a useless
|
| 330 |
+
placeholder.
|
| 331 |
+
"""
|
| 332 |
+
now = time.monotonic()
|
| 333 |
+
if now < self._summary_failure_cooldown_until:
|
| 334 |
+
logger.debug(
|
| 335 |
+
"Skipping context summary during cooldown (%.0fs remaining)",
|
| 336 |
+
self._summary_failure_cooldown_until - now,
|
| 337 |
+
)
|
| 338 |
+
return None
|
| 339 |
+
|
| 340 |
+
summary_budget = self._compute_summary_budget(turns_to_summarize)
|
| 341 |
+
content_to_summarize = self._serialize_for_summary(turns_to_summarize)
|
| 342 |
+
|
| 343 |
+
# Preamble shared by both first-compaction and iterative-update prompts.
|
| 344 |
+
# Inspired by OpenCode's "do not respond to any questions" instruction
|
| 345 |
+
# and Codex's "another language model" framing.
|
| 346 |
+
_summarizer_preamble = (
|
| 347 |
+
"You are a summarization agent creating a context checkpoint. "
|
| 348 |
+
"Your output will be injected as reference material for a DIFFERENT "
|
| 349 |
+
"assistant that continues the conversation. "
|
| 350 |
+
"Do NOT respond to any questions or requests in the conversation — "
|
| 351 |
+
"only output the structured summary. "
|
| 352 |
+
"Do NOT include any preamble, greeting, or prefix."
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
# Shared structured template (used by both paths).
|
| 356 |
+
# Key changes vs v1:
|
| 357 |
+
# - "Pending User Asks" section (from Claude Code) explicitly tracks
|
| 358 |
+
# unanswered questions so the model knows what's resolved vs open
|
| 359 |
+
# - "Remaining Work" replaces "Next Steps" to avoid reading as active
|
| 360 |
+
# instructions
|
| 361 |
+
# - "Resolved Questions" makes it clear which questions were already
|
| 362 |
+
# answered (prevents model from re-answering them)
|
| 363 |
+
_template_sections = f"""## Goal
|
| 364 |
+
[What the user is trying to accomplish]
|
| 365 |
+
|
| 366 |
+
## Constraints & Preferences
|
| 367 |
+
[User preferences, coding style, constraints, important decisions]
|
| 368 |
+
|
| 369 |
+
## Progress
|
| 370 |
+
### Done
|
| 371 |
+
[Completed work — include specific file paths, commands run, results obtained]
|
| 372 |
+
### In Progress
|
| 373 |
+
[Work currently underway]
|
| 374 |
+
### Blocked
|
| 375 |
+
[Any blockers or issues encountered]
|
| 376 |
+
|
| 377 |
+
## Key Decisions
|
| 378 |
+
[Important technical decisions and why they were made]
|
| 379 |
+
|
| 380 |
+
## Resolved Questions
|
| 381 |
+
[Questions the user asked that were ALREADY answered — include the answer so the next assistant does not re-answer them]
|
| 382 |
+
|
| 383 |
+
## Pending User Asks
|
| 384 |
+
[Questions or requests from the user that have NOT yet been answered or fulfilled. If none, write "None."]
|
| 385 |
+
|
| 386 |
+
## Relevant Files
|
| 387 |
+
[Files read, modified, or created — with brief note on each]
|
| 388 |
+
|
| 389 |
+
## Remaining Work
|
| 390 |
+
[What remains to be done — framed as context, not instructions]
|
| 391 |
+
|
| 392 |
+
## Critical Context
|
| 393 |
+
[Any specific values, error messages, configuration details, or data that would be lost without explicit preservation]
|
| 394 |
+
|
| 395 |
+
## Tools & Patterns
|
| 396 |
+
[Which tools were used, how they were used effectively, and any tool-specific discoveries]
|
| 397 |
+
|
| 398 |
+
Target ~{summary_budget} tokens. Be specific — include file paths, command outputs, error messages, and concrete values rather than vague descriptions.
|
| 399 |
+
|
| 400 |
+
Write only the summary body. Do not include any preamble or prefix."""
|
| 401 |
+
|
| 402 |
+
if self._previous_summary:
|
| 403 |
+
# Iterative update: preserve existing info, add new progress
|
| 404 |
+
prompt = f"""{_summarizer_preamble}
|
| 405 |
+
|
| 406 |
+
You are updating a context compaction summary. A previous compaction produced the summary below. New conversation turns have occurred since then and need to be incorporated.
|
| 407 |
+
|
| 408 |
+
PREVIOUS SUMMARY:
|
| 409 |
+
{self._previous_summary}
|
| 410 |
+
|
| 411 |
+
NEW TURNS TO INCORPORATE:
|
| 412 |
+
{content_to_summarize}
|
| 413 |
+
|
| 414 |
+
Update the summary using this exact structure. PRESERVE all existing information that is still relevant. ADD new progress. Move items from "In Progress" to "Done" when completed. Move answered questions to "Resolved Questions". Remove information only if it is clearly obsolete.
|
| 415 |
+
|
| 416 |
+
{_template_sections}"""
|
| 417 |
+
else:
|
| 418 |
+
# First compaction: summarize from scratch
|
| 419 |
+
prompt = f"""{_summarizer_preamble}
|
| 420 |
+
|
| 421 |
+
Create a structured handoff summary for a different assistant that will continue this conversation after earlier turns are compacted. The next assistant should be able to understand what happened without re-reading the original turns.
|
| 422 |
+
|
| 423 |
+
TURNS TO SUMMARIZE:
|
| 424 |
+
{content_to_summarize}
|
| 425 |
+
|
| 426 |
+
Use this exact structure:
|
| 427 |
+
|
| 428 |
+
{_template_sections}"""
|
| 429 |
+
|
| 430 |
+
# Inject focus topic guidance when the user provides one via /compress <focus>.
|
| 431 |
+
# This goes at the end of the prompt so it takes precedence.
|
| 432 |
+
if focus_topic:
|
| 433 |
+
prompt += f"""
|
| 434 |
+
|
| 435 |
+
FOCUS TOPIC: "{focus_topic}"
|
| 436 |
+
The user has requested that this compaction PRIORITISE preserving all information related to the focus topic above. For content related to "{focus_topic}", include full detail — exact values, file paths, command outputs, error messages, and decisions. For content NOT related to the focus topic, summarise more aggressively (brief one-liners or omit if truly irrelevant). The focus topic sections should receive roughly 60-70% of the summary token budget."""
|
| 437 |
+
|
| 438 |
+
try:
|
| 439 |
+
call_kwargs = {
|
| 440 |
+
"task": "compression",
|
| 441 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 442 |
+
"max_tokens": summary_budget * 2,
|
| 443 |
+
# timeout resolved from auxiliary.compression.timeout config by call_llm
|
| 444 |
+
}
|
| 445 |
+
if self.summary_model:
|
| 446 |
+
call_kwargs["model"] = self.summary_model
|
| 447 |
+
response = call_llm(**call_kwargs)
|
| 448 |
+
content = response.choices[0].message.content
|
| 449 |
+
# Handle cases where content is not a string (e.g., dict from llama.cpp)
|
| 450 |
+
if not isinstance(content, str):
|
| 451 |
+
content = str(content) if content else ""
|
| 452 |
+
summary = content.strip()
|
| 453 |
+
# Store for iterative updates on next compaction
|
| 454 |
+
self._previous_summary = summary
|
| 455 |
+
self._summary_failure_cooldown_until = 0.0
|
| 456 |
+
return self._with_summary_prefix(summary)
|
| 457 |
+
except RuntimeError:
|
| 458 |
+
self._summary_failure_cooldown_until = time.monotonic() + _SUMMARY_FAILURE_COOLDOWN_SECONDS
|
| 459 |
+
logging.warning("Context compression: no provider available for "
|
| 460 |
+
"summary. Middle turns will be dropped without summary "
|
| 461 |
+
"for %d seconds.",
|
| 462 |
+
_SUMMARY_FAILURE_COOLDOWN_SECONDS)
|
| 463 |
+
return None
|
| 464 |
+
except Exception as e:
|
| 465 |
+
self._summary_failure_cooldown_until = time.monotonic() + _SUMMARY_FAILURE_COOLDOWN_SECONDS
|
| 466 |
+
logging.warning(
|
| 467 |
+
"Failed to generate context summary: %s. "
|
| 468 |
+
"Further summary attempts paused for %d seconds.",
|
| 469 |
+
e,
|
| 470 |
+
_SUMMARY_FAILURE_COOLDOWN_SECONDS,
|
| 471 |
+
)
|
| 472 |
+
return None
|
| 473 |
+
|
| 474 |
+
@staticmethod
|
| 475 |
+
def _with_summary_prefix(summary: str) -> str:
|
| 476 |
+
"""Normalize summary text to the current compaction handoff format."""
|
| 477 |
+
text = (summary or "").strip()
|
| 478 |
+
for prefix in (LEGACY_SUMMARY_PREFIX, SUMMARY_PREFIX):
|
| 479 |
+
if text.startswith(prefix):
|
| 480 |
+
text = text[len(prefix):].lstrip()
|
| 481 |
+
break
|
| 482 |
+
return f"{SUMMARY_PREFIX}\n{text}" if text else SUMMARY_PREFIX
|
| 483 |
+
|
| 484 |
+
# ------------------------------------------------------------------
|
| 485 |
+
# Tool-call / tool-result pair integrity helpers
|
| 486 |
+
# ------------------------------------------------------------------
|
| 487 |
+
|
| 488 |
+
@staticmethod
|
| 489 |
+
def _get_tool_call_id(tc) -> str:
|
| 490 |
+
"""Extract the call ID from a tool_call entry (dict or SimpleNamespace)."""
|
| 491 |
+
if isinstance(tc, dict):
|
| 492 |
+
return tc.get("id", "")
|
| 493 |
+
return getattr(tc, "id", "") or ""
|
| 494 |
+
|
| 495 |
+
def _sanitize_tool_pairs(self, messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 496 |
+
"""Fix orphaned tool_call / tool_result pairs after compression.
|
| 497 |
+
|
| 498 |
+
Two failure modes:
|
| 499 |
+
1. A tool *result* references a call_id whose assistant tool_call was
|
| 500 |
+
removed (summarized/truncated). The API rejects this with
|
| 501 |
+
"No tool call found for function call output with call_id ...".
|
| 502 |
+
2. An assistant message has tool_calls whose results were dropped.
|
| 503 |
+
The API rejects this because every tool_call must be followed by
|
| 504 |
+
a tool result with the matching call_id.
|
| 505 |
+
|
| 506 |
+
This method removes orphaned results and inserts stub results for
|
| 507 |
+
orphaned calls so the message list is always well-formed.
|
| 508 |
+
"""
|
| 509 |
+
surviving_call_ids: set = set()
|
| 510 |
+
for msg in messages:
|
| 511 |
+
if msg.get("role") == "assistant":
|
| 512 |
+
for tc in msg.get("tool_calls") or []:
|
| 513 |
+
cid = self._get_tool_call_id(tc)
|
| 514 |
+
if cid:
|
| 515 |
+
surviving_call_ids.add(cid)
|
| 516 |
+
|
| 517 |
+
result_call_ids: set = set()
|
| 518 |
+
for msg in messages:
|
| 519 |
+
if msg.get("role") == "tool":
|
| 520 |
+
cid = msg.get("tool_call_id")
|
| 521 |
+
if cid:
|
| 522 |
+
result_call_ids.add(cid)
|
| 523 |
+
|
| 524 |
+
# 1. Remove tool results whose call_id has no matching assistant tool_call
|
| 525 |
+
orphaned_results = result_call_ids - surviving_call_ids
|
| 526 |
+
if orphaned_results:
|
| 527 |
+
messages = [
|
| 528 |
+
m for m in messages
|
| 529 |
+
if not (m.get("role") == "tool" and m.get("tool_call_id") in orphaned_results)
|
| 530 |
+
]
|
| 531 |
+
if not self.quiet_mode:
|
| 532 |
+
logger.info("Compression sanitizer: removed %d orphaned tool result(s)", len(orphaned_results))
|
| 533 |
+
|
| 534 |
+
# 2. Add stub results for assistant tool_calls whose results were dropped
|
| 535 |
+
missing_results = surviving_call_ids - result_call_ids
|
| 536 |
+
if missing_results:
|
| 537 |
+
patched: List[Dict[str, Any]] = []
|
| 538 |
+
for msg in messages:
|
| 539 |
+
patched.append(msg)
|
| 540 |
+
if msg.get("role") == "assistant":
|
| 541 |
+
for tc in msg.get("tool_calls") or []:
|
| 542 |
+
cid = self._get_tool_call_id(tc)
|
| 543 |
+
if cid in missing_results:
|
| 544 |
+
patched.append({
|
| 545 |
+
"role": "tool",
|
| 546 |
+
"content": "[Result from earlier conversation — see context summary above]",
|
| 547 |
+
"tool_call_id": cid,
|
| 548 |
+
})
|
| 549 |
+
messages = patched
|
| 550 |
+
if not self.quiet_mode:
|
| 551 |
+
logger.info("Compression sanitizer: added %d stub tool result(s)", len(missing_results))
|
| 552 |
+
|
| 553 |
+
return messages
|
| 554 |
+
|
| 555 |
+
def _align_boundary_forward(self, messages: List[Dict[str, Any]], idx: int) -> int:
|
| 556 |
+
"""Push a compress-start boundary forward past any orphan tool results.
|
| 557 |
+
|
| 558 |
+
If ``messages[idx]`` is a tool result, slide forward until we hit a
|
| 559 |
+
non-tool message so we don't start the summarised region mid-group.
|
| 560 |
+
"""
|
| 561 |
+
while idx < len(messages) and messages[idx].get("role") == "tool":
|
| 562 |
+
idx += 1
|
| 563 |
+
return idx
|
| 564 |
+
|
| 565 |
+
def _align_boundary_backward(self, messages: List[Dict[str, Any]], idx: int) -> int:
|
| 566 |
+
"""Pull a compress-end boundary backward to avoid splitting a
|
| 567 |
+
tool_call / result group.
|
| 568 |
+
|
| 569 |
+
If the boundary falls in the middle of a tool-result group (i.e.
|
| 570 |
+
there are consecutive tool messages before ``idx``), walk backward
|
| 571 |
+
past all of them to find the parent assistant message. If found,
|
| 572 |
+
move the boundary before the assistant so the entire
|
| 573 |
+
assistant + tool_results group is included in the summarised region
|
| 574 |
+
rather than being split (which causes silent data loss when
|
| 575 |
+
``_sanitize_tool_pairs`` removes the orphaned tail results).
|
| 576 |
+
"""
|
| 577 |
+
if idx <= 0 or idx >= len(messages):
|
| 578 |
+
return idx
|
| 579 |
+
# Walk backward past consecutive tool results
|
| 580 |
+
check = idx - 1
|
| 581 |
+
while check >= 0 and messages[check].get("role") == "tool":
|
| 582 |
+
check -= 1
|
| 583 |
+
# If we landed on the parent assistant with tool_calls, pull the
|
| 584 |
+
# boundary before it so the whole group gets summarised together.
|
| 585 |
+
if check >= 0 and messages[check].get("role") == "assistant" and messages[check].get("tool_calls"):
|
| 586 |
+
idx = check
|
| 587 |
+
return idx
|
| 588 |
+
|
| 589 |
+
# ------------------------------------------------------------------
|
| 590 |
+
# Tail protection by token budget
|
| 591 |
+
# ------------------------------------------------------------------
|
| 592 |
+
|
| 593 |
+
def _find_tail_cut_by_tokens(
|
| 594 |
+
self, messages: List[Dict[str, Any]], head_end: int,
|
| 595 |
+
token_budget: int | None = None,
|
| 596 |
+
) -> int:
|
| 597 |
+
"""Walk backward from the end of messages, accumulating tokens until
|
| 598 |
+
the budget is reached. Returns the index where the tail starts.
|
| 599 |
+
|
| 600 |
+
``token_budget`` defaults to ``self.tail_token_budget`` which is
|
| 601 |
+
derived from ``summary_target_ratio * context_length``, so it
|
| 602 |
+
scales automatically with the model's context window.
|
| 603 |
+
|
| 604 |
+
Token budget is the primary criterion. A hard minimum of 3 messages
|
| 605 |
+
is always protected, but the budget is allowed to exceed by up to
|
| 606 |
+
1.5x to avoid cutting inside an oversized message (tool output, file
|
| 607 |
+
read, etc.). If even the minimum 3 messages exceed 1.5x the budget
|
| 608 |
+
the cut is placed right after the head so compression still runs.
|
| 609 |
+
|
| 610 |
+
Never cuts inside a tool_call/result group.
|
| 611 |
+
"""
|
| 612 |
+
if token_budget is None:
|
| 613 |
+
token_budget = self.tail_token_budget
|
| 614 |
+
n = len(messages)
|
| 615 |
+
# Hard minimum: always keep at least 3 messages in the tail
|
| 616 |
+
min_tail = min(3, n - head_end - 1) if n - head_end > 1 else 0
|
| 617 |
+
soft_ceiling = int(token_budget * 1.5)
|
| 618 |
+
accumulated = 0
|
| 619 |
+
cut_idx = n # start from beyond the end
|
| 620 |
+
|
| 621 |
+
for i in range(n - 1, head_end - 1, -1):
|
| 622 |
+
msg = messages[i]
|
| 623 |
+
content = msg.get("content") or ""
|
| 624 |
+
msg_tokens = len(content) // _CHARS_PER_TOKEN + 10 # +10 for role/metadata
|
| 625 |
+
# Include tool call arguments in estimate
|
| 626 |
+
for tc in msg.get("tool_calls") or []:
|
| 627 |
+
if isinstance(tc, dict):
|
| 628 |
+
args = tc.get("function", {}).get("arguments", "")
|
| 629 |
+
msg_tokens += len(args) // _CHARS_PER_TOKEN
|
| 630 |
+
# Stop once we exceed the soft ceiling (unless we haven't hit min_tail yet)
|
| 631 |
+
if accumulated + msg_tokens > soft_ceiling and (n - i) >= min_tail:
|
| 632 |
+
break
|
| 633 |
+
accumulated += msg_tokens
|
| 634 |
+
cut_idx = i
|
| 635 |
+
|
| 636 |
+
# Ensure we protect at least min_tail messages
|
| 637 |
+
fallback_cut = n - min_tail
|
| 638 |
+
if cut_idx > fallback_cut:
|
| 639 |
+
cut_idx = fallback_cut
|
| 640 |
+
|
| 641 |
+
# If the token budget would protect everything (small conversations),
|
| 642 |
+
# force a cut after the head so compression can still remove middle turns.
|
| 643 |
+
if cut_idx <= head_end:
|
| 644 |
+
cut_idx = max(fallback_cut, head_end + 1)
|
| 645 |
+
|
| 646 |
+
# Align to avoid splitting tool groups
|
| 647 |
+
cut_idx = self._align_boundary_backward(messages, cut_idx)
|
| 648 |
+
|
| 649 |
+
return max(cut_idx, head_end + 1)
|
| 650 |
+
|
| 651 |
+
# ------------------------------------------------------------------
|
| 652 |
+
# Main compression entry point
|
| 653 |
+
# ------------------------------------------------------------------
|
| 654 |
+
|
| 655 |
+
def compress(self, messages: List[Dict[str, Any]], current_tokens: int = None, focus_topic: str = None) -> List[Dict[str, Any]]:
|
| 656 |
+
"""Compress conversation messages by summarizing middle turns.
|
| 657 |
+
|
| 658 |
+
Algorithm:
|
| 659 |
+
1. Prune old tool results (cheap pre-pass, no LLM call)
|
| 660 |
+
2. Protect head messages (system prompt + first exchange)
|
| 661 |
+
3. Find tail boundary by token budget (~20K tokens of recent context)
|
| 662 |
+
4. Summarize middle turns with structured LLM prompt
|
| 663 |
+
5. On re-compression, iteratively update the previous summary
|
| 664 |
+
|
| 665 |
+
After compression, orphaned tool_call / tool_result pairs are cleaned
|
| 666 |
+
up so the API never receives mismatched IDs.
|
| 667 |
+
|
| 668 |
+
Args:
|
| 669 |
+
focus_topic: Optional focus string for guided compression. When
|
| 670 |
+
provided, the summariser will prioritise preserving information
|
| 671 |
+
related to this topic and be more aggressive about compressing
|
| 672 |
+
everything else. Inspired by Claude Code's ``/compact``.
|
| 673 |
+
"""
|
| 674 |
+
n_messages = len(messages)
|
| 675 |
+
# Only need head + 3 tail messages minimum (token budget decides the real tail size)
|
| 676 |
+
_min_for_compress = self.protect_first_n + 3 + 1
|
| 677 |
+
if n_messages <= _min_for_compress:
|
| 678 |
+
if not self.quiet_mode:
|
| 679 |
+
logger.warning(
|
| 680 |
+
"Cannot compress: only %d messages (need > %d)",
|
| 681 |
+
n_messages, _min_for_compress,
|
| 682 |
+
)
|
| 683 |
+
return messages
|
| 684 |
+
|
| 685 |
+
display_tokens = current_tokens if current_tokens else self.last_prompt_tokens or estimate_messages_tokens_rough(messages)
|
| 686 |
+
|
| 687 |
+
# Phase 1: Prune old tool results (cheap, no LLM call)
|
| 688 |
+
messages, pruned_count = self._prune_old_tool_results(
|
| 689 |
+
messages, protect_tail_count=self.protect_last_n,
|
| 690 |
+
protect_tail_tokens=self.tail_token_budget,
|
| 691 |
+
)
|
| 692 |
+
if pruned_count and not self.quiet_mode:
|
| 693 |
+
logger.info("Pre-compression: pruned %d old tool result(s)", pruned_count)
|
| 694 |
+
|
| 695 |
+
# Phase 2: Determine boundaries
|
| 696 |
+
compress_start = self.protect_first_n
|
| 697 |
+
compress_start = self._align_boundary_forward(messages, compress_start)
|
| 698 |
+
|
| 699 |
+
# Use token-budget tail protection instead of fixed message count
|
| 700 |
+
compress_end = self._find_tail_cut_by_tokens(messages, compress_start)
|
| 701 |
+
|
| 702 |
+
if compress_start >= compress_end:
|
| 703 |
+
return messages
|
| 704 |
+
|
| 705 |
+
turns_to_summarize = messages[compress_start:compress_end]
|
| 706 |
+
|
| 707 |
+
if not self.quiet_mode:
|
| 708 |
+
logger.info(
|
| 709 |
+
"Context compression triggered (%d tokens >= %d threshold)",
|
| 710 |
+
display_tokens,
|
| 711 |
+
self.threshold_tokens,
|
| 712 |
+
)
|
| 713 |
+
logger.info(
|
| 714 |
+
"Model context limit: %d tokens (%.0f%% = %d)",
|
| 715 |
+
self.context_length,
|
| 716 |
+
self.threshold_percent * 100,
|
| 717 |
+
self.threshold_tokens,
|
| 718 |
+
)
|
| 719 |
+
tail_msgs = n_messages - compress_end
|
| 720 |
+
logger.info(
|
| 721 |
+
"Summarizing turns %d-%d (%d turns), protecting %d head + %d tail messages",
|
| 722 |
+
compress_start + 1,
|
| 723 |
+
compress_end,
|
| 724 |
+
len(turns_to_summarize),
|
| 725 |
+
compress_start,
|
| 726 |
+
tail_msgs,
|
| 727 |
+
)
|
| 728 |
+
|
| 729 |
+
# Phase 3: Generate structured summary
|
| 730 |
+
summary = self._generate_summary(turns_to_summarize, focus_topic=focus_topic)
|
| 731 |
+
|
| 732 |
+
# Phase 4: Assemble compressed message list
|
| 733 |
+
compressed = []
|
| 734 |
+
for i in range(compress_start):
|
| 735 |
+
msg = messages[i].copy()
|
| 736 |
+
if i == 0 and msg.get("role") == "system" and self.compression_count == 0:
|
| 737 |
+
msg["content"] = (
|
| 738 |
+
(msg.get("content") or "")
|
| 739 |
+
+ "\n\n[Note: Some earlier conversation turns have been compacted into a handoff summary to preserve context space. The current session state may still reflect earlier work, so build on that summary and state rather than re-doing work.]"
|
| 740 |
+
)
|
| 741 |
+
compressed.append(msg)
|
| 742 |
+
|
| 743 |
+
# If LLM summary failed, insert a static fallback so the model
|
| 744 |
+
# knows context was lost rather than silently dropping everything.
|
| 745 |
+
if not summary:
|
| 746 |
+
if not self.quiet_mode:
|
| 747 |
+
logger.warning("Summary generation failed — inserting static fallback context marker")
|
| 748 |
+
n_dropped = compress_end - compress_start
|
| 749 |
+
summary = (
|
| 750 |
+
f"{SUMMARY_PREFIX}\n"
|
| 751 |
+
f"Summary generation was unavailable. {n_dropped} conversation turns were "
|
| 752 |
+
f"removed to free context space but could not be summarized. The removed "
|
| 753 |
+
f"turns contained earlier work in this session. Continue based on the "
|
| 754 |
+
f"recent messages below and the current state of any files or resources."
|
| 755 |
+
)
|
| 756 |
+
|
| 757 |
+
_merge_summary_into_tail = False
|
| 758 |
+
last_head_role = messages[compress_start - 1].get("role", "user") if compress_start > 0 else "user"
|
| 759 |
+
first_tail_role = messages[compress_end].get("role", "user") if compress_end < n_messages else "user"
|
| 760 |
+
# Pick a role that avoids consecutive same-role with both neighbors.
|
| 761 |
+
# Priority: avoid colliding with head (already committed), then tail.
|
| 762 |
+
if last_head_role in ("assistant", "tool"):
|
| 763 |
+
summary_role = "user"
|
| 764 |
+
else:
|
| 765 |
+
summary_role = "assistant"
|
| 766 |
+
# If the chosen role collides with the tail AND flipping wouldn't
|
| 767 |
+
# collide with the head, flip it.
|
| 768 |
+
if summary_role == first_tail_role:
|
| 769 |
+
flipped = "assistant" if summary_role == "user" else "user"
|
| 770 |
+
if flipped != last_head_role:
|
| 771 |
+
summary_role = flipped
|
| 772 |
+
else:
|
| 773 |
+
# Both roles would create consecutive same-role messages
|
| 774 |
+
# (e.g. head=assistant, tail=user — neither role works).
|
| 775 |
+
# Merge the summary into the first tail message instead
|
| 776 |
+
# of inserting a standalone message that breaks alternation.
|
| 777 |
+
_merge_summary_into_tail = True
|
| 778 |
+
if not _merge_summary_into_tail:
|
| 779 |
+
compressed.append({"role": summary_role, "content": summary})
|
| 780 |
+
|
| 781 |
+
for i in range(compress_end, n_messages):
|
| 782 |
+
msg = messages[i].copy()
|
| 783 |
+
if _merge_summary_into_tail and i == compress_end:
|
| 784 |
+
original = msg.get("content") or ""
|
| 785 |
+
msg["content"] = (
|
| 786 |
+
summary
|
| 787 |
+
+ "\n\n--- END OF CONTEXT SUMMARY — "
|
| 788 |
+
"respond to the message below, not the summary above ---\n\n"
|
| 789 |
+
+ original
|
| 790 |
+
)
|
| 791 |
+
_merge_summary_into_tail = False
|
| 792 |
+
compressed.append(msg)
|
| 793 |
+
|
| 794 |
+
self.compression_count += 1
|
| 795 |
+
|
| 796 |
+
compressed = self._sanitize_tool_pairs(compressed)
|
| 797 |
+
|
| 798 |
+
if not self.quiet_mode:
|
| 799 |
+
new_estimate = estimate_messages_tokens_rough(compressed)
|
| 800 |
+
saved_estimate = display_tokens - new_estimate
|
| 801 |
+
logger.info(
|
| 802 |
+
"Compressed: %d -> %d messages (~%d tokens saved)",
|
| 803 |
+
n_messages,
|
| 804 |
+
len(compressed),
|
| 805 |
+
saved_estimate,
|
| 806 |
+
)
|
| 807 |
+
logger.info("Compression #%d complete", self.compression_count)
|
| 808 |
+
|
| 809 |
+
return compressed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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Abstract base class for pluggable context engines.
|
| 2 |
+
|
| 3 |
+
A context engine controls how conversation context is managed when
|
| 4 |
+
approaching the model's token limit. The built-in ContextCompressor
|
| 5 |
+
is the default implementation. Third-party engines (e.g. LCM) can
|
| 6 |
+
replace it via the plugin system or by being placed in the
|
| 7 |
+
``plugins/context_engine/<name>/`` directory.
|
| 8 |
+
|
| 9 |
+
Selection is config-driven: ``context.engine`` in config.yaml.
|
| 10 |
+
Default is ``"compressor"`` (the built-in). Only one engine is active.
|
| 11 |
+
|
| 12 |
+
The engine is responsible for:
|
| 13 |
+
- Deciding when compaction should fire
|
| 14 |
+
- Performing compaction (summarization, DAG construction, etc.)
|
| 15 |
+
- Optionally exposing tools the agent can call (e.g. lcm_grep)
|
| 16 |
+
- Tracking token usage from API responses
|
| 17 |
+
|
| 18 |
+
Lifecycle:
|
| 19 |
+
1. Engine is instantiated and registered (plugin register() or default)
|
| 20 |
+
2. on_session_start() called when a conversation begins
|
| 21 |
+
3. update_from_response() called after each API response with usage data
|
| 22 |
+
4. should_compress() checked after each turn
|
| 23 |
+
5. compress() called when should_compress() returns True
|
| 24 |
+
6. on_session_end() called at real session boundaries (CLI exit, /reset,
|
| 25 |
+
gateway session expiry) — NOT per-turn
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
from abc import ABC, abstractmethod
|
| 29 |
+
from typing import Any, Dict, List, Optional
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class ContextEngine(ABC):
|
| 33 |
+
"""Base class all context engines must implement."""
|
| 34 |
+
|
| 35 |
+
# -- Identity ----------------------------------------------------------
|
| 36 |
+
|
| 37 |
+
@property
|
| 38 |
+
@abstractmethod
|
| 39 |
+
def name(self) -> str:
|
| 40 |
+
"""Short identifier (e.g. 'compressor', 'lcm')."""
|
| 41 |
+
|
| 42 |
+
# -- Token state (read by run_agent.py for display/logging) ------------
|
| 43 |
+
#
|
| 44 |
+
# Engines MUST maintain these. run_agent.py reads them directly.
|
| 45 |
+
|
| 46 |
+
last_prompt_tokens: int = 0
|
| 47 |
+
last_completion_tokens: int = 0
|
| 48 |
+
last_total_tokens: int = 0
|
| 49 |
+
threshold_tokens: int = 0
|
| 50 |
+
context_length: int = 0
|
| 51 |
+
compression_count: int = 0
|
| 52 |
+
|
| 53 |
+
# -- Compaction parameters (read by run_agent.py for preflight) --------
|
| 54 |
+
#
|
| 55 |
+
# These control the preflight compression check. Subclasses may
|
| 56 |
+
# override via __init__ or property; defaults are sensible for most
|
| 57 |
+
# engines.
|
| 58 |
+
|
| 59 |
+
threshold_percent: float = 0.75
|
| 60 |
+
protect_first_n: int = 3
|
| 61 |
+
protect_last_n: int = 6
|
| 62 |
+
|
| 63 |
+
# -- Core interface ----------------------------------------------------
|
| 64 |
+
|
| 65 |
+
@abstractmethod
|
| 66 |
+
def update_from_response(self, usage: Dict[str, Any]) -> None:
|
| 67 |
+
"""Update tracked token usage from an API response.
|
| 68 |
+
|
| 69 |
+
Called after every LLM call with the usage dict from the response.
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
@abstractmethod
|
| 73 |
+
def should_compress(self, prompt_tokens: int = None) -> bool:
|
| 74 |
+
"""Return True if compaction should fire this turn."""
|
| 75 |
+
|
| 76 |
+
@abstractmethod
|
| 77 |
+
def compress(
|
| 78 |
+
self,
|
| 79 |
+
messages: List[Dict[str, Any]],
|
| 80 |
+
current_tokens: int = None,
|
| 81 |
+
) -> List[Dict[str, Any]]:
|
| 82 |
+
"""Compact the message list and return the new message list.
|
| 83 |
+
|
| 84 |
+
This is the main entry point. The engine receives the full message
|
| 85 |
+
list and returns a (possibly shorter) list that fits within the
|
| 86 |
+
context budget. The implementation is free to summarize, build a
|
| 87 |
+
DAG, or do anything else — as long as the returned list is a valid
|
| 88 |
+
OpenAI-format message sequence.
|
| 89 |
+
"""
|
| 90 |
+
|
| 91 |
+
# -- Optional: pre-flight check ----------------------------------------
|
| 92 |
+
|
| 93 |
+
def should_compress_preflight(self, messages: List[Dict[str, Any]]) -> bool:
|
| 94 |
+
"""Quick rough check before the API call (no real token count yet).
|
| 95 |
+
|
| 96 |
+
Default returns False (skip pre-flight). Override if your engine
|
| 97 |
+
can do a cheap estimate.
|
| 98 |
+
"""
|
| 99 |
+
return False
|
| 100 |
+
|
| 101 |
+
# -- Optional: session lifecycle ---------------------------------------
|
| 102 |
+
|
| 103 |
+
def on_session_start(self, session_id: str, **kwargs) -> None:
|
| 104 |
+
"""Called when a new conversation session begins.
|
| 105 |
+
|
| 106 |
+
Use this to load persisted state (DAG, store) for the session.
|
| 107 |
+
kwargs may include hermes_home, platform, model, etc.
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
def on_session_end(self, session_id: str, messages: List[Dict[str, Any]]) -> None:
|
| 111 |
+
"""Called at real session boundaries (CLI exit, /reset, gateway expiry).
|
| 112 |
+
|
| 113 |
+
Use this to flush state, close DB connections, etc.
|
| 114 |
+
NOT called per-turn — only when the session truly ends.
|
| 115 |
+
"""
|
| 116 |
+
|
| 117 |
+
def on_session_reset(self) -> None:
|
| 118 |
+
"""Called on /new or /reset. Reset per-session state.
|
| 119 |
+
|
| 120 |
+
Default resets compression_count and token tracking.
|
| 121 |
+
"""
|
| 122 |
+
self.last_prompt_tokens = 0
|
| 123 |
+
self.last_completion_tokens = 0
|
| 124 |
+
self.last_total_tokens = 0
|
| 125 |
+
self.compression_count = 0
|
| 126 |
+
|
| 127 |
+
# -- Optional: tools ---------------------------------------------------
|
| 128 |
+
|
| 129 |
+
def get_tool_schemas(self) -> List[Dict[str, Any]]:
|
| 130 |
+
"""Return tool schemas this engine provides to the agent.
|
| 131 |
+
|
| 132 |
+
Default returns empty list (no tools). LCM would return schemas
|
| 133 |
+
for lcm_grep, lcm_describe, lcm_expand here.
|
| 134 |
+
"""
|
| 135 |
+
return []
|
| 136 |
+
|
| 137 |
+
def handle_tool_call(self, name: str, args: Dict[str, Any], **kwargs) -> str:
|
| 138 |
+
"""Handle a tool call from the agent.
|
| 139 |
+
|
| 140 |
+
Only called for tool names returned by get_tool_schemas().
|
| 141 |
+
Must return a JSON string.
|
| 142 |
+
|
| 143 |
+
kwargs may include:
|
| 144 |
+
messages: the current in-memory message list (for live ingestion)
|
| 145 |
+
"""
|
| 146 |
+
import json
|
| 147 |
+
return json.dumps({"error": f"Unknown context engine tool: {name}"})
|
| 148 |
+
|
| 149 |
+
# -- Optional: status / display ----------------------------------------
|
| 150 |
+
|
| 151 |
+
def get_status(self) -> Dict[str, Any]:
|
| 152 |
+
"""Return status dict for display/logging.
|
| 153 |
+
|
| 154 |
+
Default returns the standard fields run_agent.py expects.
|
| 155 |
+
"""
|
| 156 |
+
return {
|
| 157 |
+
"last_prompt_tokens": self.last_prompt_tokens,
|
| 158 |
+
"threshold_tokens": self.threshold_tokens,
|
| 159 |
+
"context_length": self.context_length,
|
| 160 |
+
"usage_percent": (
|
| 161 |
+
min(100, self.last_prompt_tokens / self.context_length * 100)
|
| 162 |
+
if self.context_length else 0
|
| 163 |
+
),
|
| 164 |
+
"compression_count": self.compression_count,
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
# -- Optional: model switch support ------------------------------------
|
| 168 |
+
|
| 169 |
+
def update_model(
|
| 170 |
+
self,
|
| 171 |
+
model: str,
|
| 172 |
+
context_length: int,
|
| 173 |
+
base_url: str = "",
|
| 174 |
+
api_key: str = "",
|
| 175 |
+
provider: str = "",
|
| 176 |
+
) -> None:
|
| 177 |
+
"""Called when the user switches models or on fallback activation.
|
| 178 |
+
|
| 179 |
+
Default updates context_length and recalculates threshold_tokens
|
| 180 |
+
from threshold_percent. Override if your engine needs more
|
| 181 |
+
(e.g. recalculate DAG budgets, switch summary models).
|
| 182 |
+
"""
|
| 183 |
+
self.context_length = context_length
|
| 184 |
+
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import asyncio
|
| 4 |
+
import inspect
|
| 5 |
+
import json
|
| 6 |
+
import mimetypes
|
| 7 |
+
import os
|
| 8 |
+
import re
|
| 9 |
+
import subprocess
|
| 10 |
+
from dataclasses import dataclass, field
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
from typing import Awaitable, Callable
|
| 13 |
+
|
| 14 |
+
from agent.model_metadata import estimate_tokens_rough
|
| 15 |
+
|
| 16 |
+
_QUOTED_REFERENCE_VALUE = r'(?:`[^`\n]+`|"[^"\n]+"|\'[^\'\n]+\')'
|
| 17 |
+
REFERENCE_PATTERN = re.compile(
|
| 18 |
+
rf"(?<![\w/])@(?:(?P<simple>diff|staged)\b|(?P<kind>file|folder|git|url):(?P<value>{_QUOTED_REFERENCE_VALUE}(?::\d+(?:-\d+)?)?|\S+))"
|
| 19 |
+
)
|
| 20 |
+
TRAILING_PUNCTUATION = ",.;!?"
|
| 21 |
+
_SENSITIVE_HOME_DIRS = (".ssh", ".aws", ".gnupg", ".kube", ".docker", ".azure", ".config/gh")
|
| 22 |
+
_SENSITIVE_HERMES_DIRS = (Path("skills") / ".hub",)
|
| 23 |
+
_SENSITIVE_HOME_FILES = (
|
| 24 |
+
Path(".ssh") / "authorized_keys",
|
| 25 |
+
Path(".ssh") / "id_rsa",
|
| 26 |
+
Path(".ssh") / "id_ed25519",
|
| 27 |
+
Path(".ssh") / "config",
|
| 28 |
+
Path(".bashrc"),
|
| 29 |
+
Path(".zshrc"),
|
| 30 |
+
Path(".profile"),
|
| 31 |
+
Path(".bash_profile"),
|
| 32 |
+
Path(".zprofile"),
|
| 33 |
+
Path(".netrc"),
|
| 34 |
+
Path(".pgpass"),
|
| 35 |
+
Path(".npmrc"),
|
| 36 |
+
Path(".pypirc"),
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@dataclass(frozen=True)
|
| 41 |
+
class ContextReference:
|
| 42 |
+
raw: str
|
| 43 |
+
kind: str
|
| 44 |
+
target: str
|
| 45 |
+
start: int
|
| 46 |
+
end: int
|
| 47 |
+
line_start: int | None = None
|
| 48 |
+
line_end: int | None = None
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@dataclass
|
| 52 |
+
class ContextReferenceResult:
|
| 53 |
+
message: str
|
| 54 |
+
original_message: str
|
| 55 |
+
references: list[ContextReference] = field(default_factory=list)
|
| 56 |
+
warnings: list[str] = field(default_factory=list)
|
| 57 |
+
injected_tokens: int = 0
|
| 58 |
+
expanded: bool = False
|
| 59 |
+
blocked: bool = False
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def parse_context_references(message: str) -> list[ContextReference]:
|
| 63 |
+
refs: list[ContextReference] = []
|
| 64 |
+
if not message:
|
| 65 |
+
return refs
|
| 66 |
+
|
| 67 |
+
for match in REFERENCE_PATTERN.finditer(message):
|
| 68 |
+
simple = match.group("simple")
|
| 69 |
+
if simple:
|
| 70 |
+
refs.append(
|
| 71 |
+
ContextReference(
|
| 72 |
+
raw=match.group(0),
|
| 73 |
+
kind=simple,
|
| 74 |
+
target="",
|
| 75 |
+
start=match.start(),
|
| 76 |
+
end=match.end(),
|
| 77 |
+
)
|
| 78 |
+
)
|
| 79 |
+
continue
|
| 80 |
+
|
| 81 |
+
kind = match.group("kind")
|
| 82 |
+
value = _strip_trailing_punctuation(match.group("value") or "")
|
| 83 |
+
line_start = None
|
| 84 |
+
line_end = None
|
| 85 |
+
target = _strip_reference_wrappers(value)
|
| 86 |
+
|
| 87 |
+
if kind == "file":
|
| 88 |
+
target, line_start, line_end = _parse_file_reference_value(value)
|
| 89 |
+
|
| 90 |
+
refs.append(
|
| 91 |
+
ContextReference(
|
| 92 |
+
raw=match.group(0),
|
| 93 |
+
kind=kind,
|
| 94 |
+
target=target,
|
| 95 |
+
start=match.start(),
|
| 96 |
+
end=match.end(),
|
| 97 |
+
line_start=line_start,
|
| 98 |
+
line_end=line_end,
|
| 99 |
+
)
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return refs
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def preprocess_context_references(
|
| 106 |
+
message: str,
|
| 107 |
+
*,
|
| 108 |
+
cwd: str | Path,
|
| 109 |
+
context_length: int,
|
| 110 |
+
url_fetcher: Callable[[str], str | Awaitable[str]] | None = None,
|
| 111 |
+
allowed_root: str | Path | None = None,
|
| 112 |
+
) -> ContextReferenceResult:
|
| 113 |
+
coro = preprocess_context_references_async(
|
| 114 |
+
message,
|
| 115 |
+
cwd=cwd,
|
| 116 |
+
context_length=context_length,
|
| 117 |
+
url_fetcher=url_fetcher,
|
| 118 |
+
allowed_root=allowed_root,
|
| 119 |
+
)
|
| 120 |
+
# Safe for both CLI (no loop) and gateway (loop already running).
|
| 121 |
+
try:
|
| 122 |
+
loop = asyncio.get_running_loop()
|
| 123 |
+
except RuntimeError:
|
| 124 |
+
loop = None
|
| 125 |
+
if loop and loop.is_running():
|
| 126 |
+
import concurrent.futures
|
| 127 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
|
| 128 |
+
return pool.submit(asyncio.run, coro).result()
|
| 129 |
+
return asyncio.run(coro)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
async def preprocess_context_references_async(
|
| 133 |
+
message: str,
|
| 134 |
+
*,
|
| 135 |
+
cwd: str | Path,
|
| 136 |
+
context_length: int,
|
| 137 |
+
url_fetcher: Callable[[str], str | Awaitable[str]] | None = None,
|
| 138 |
+
allowed_root: str | Path | None = None,
|
| 139 |
+
) -> ContextReferenceResult:
|
| 140 |
+
refs = parse_context_references(message)
|
| 141 |
+
if not refs:
|
| 142 |
+
return ContextReferenceResult(message=message, original_message=message)
|
| 143 |
+
|
| 144 |
+
cwd_path = Path(cwd).expanduser().resolve()
|
| 145 |
+
# Default to the current working directory so @ references cannot escape
|
| 146 |
+
# the active workspace unless a caller explicitly widens the root.
|
| 147 |
+
allowed_root_path = (
|
| 148 |
+
Path(allowed_root).expanduser().resolve() if allowed_root is not None else cwd_path
|
| 149 |
+
)
|
| 150 |
+
warnings: list[str] = []
|
| 151 |
+
blocks: list[str] = []
|
| 152 |
+
injected_tokens = 0
|
| 153 |
+
|
| 154 |
+
for ref in refs:
|
| 155 |
+
warning, block = await _expand_reference(
|
| 156 |
+
ref,
|
| 157 |
+
cwd_path,
|
| 158 |
+
url_fetcher=url_fetcher,
|
| 159 |
+
allowed_root=allowed_root_path,
|
| 160 |
+
)
|
| 161 |
+
if warning:
|
| 162 |
+
warnings.append(warning)
|
| 163 |
+
if block:
|
| 164 |
+
blocks.append(block)
|
| 165 |
+
injected_tokens += estimate_tokens_rough(block)
|
| 166 |
+
|
| 167 |
+
hard_limit = max(1, int(context_length * 0.50))
|
| 168 |
+
soft_limit = max(1, int(context_length * 0.25))
|
| 169 |
+
if injected_tokens > hard_limit:
|
| 170 |
+
warnings.append(
|
| 171 |
+
f"@ context injection refused: {injected_tokens} tokens exceeds the 50% hard limit ({hard_limit})."
|
| 172 |
+
)
|
| 173 |
+
return ContextReferenceResult(
|
| 174 |
+
message=message,
|
| 175 |
+
original_message=message,
|
| 176 |
+
references=refs,
|
| 177 |
+
warnings=warnings,
|
| 178 |
+
injected_tokens=injected_tokens,
|
| 179 |
+
expanded=False,
|
| 180 |
+
blocked=True,
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
if injected_tokens > soft_limit:
|
| 184 |
+
warnings.append(
|
| 185 |
+
f"@ context injection warning: {injected_tokens} tokens exceeds the 25% soft limit ({soft_limit})."
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
stripped = _remove_reference_tokens(message, refs)
|
| 189 |
+
final = stripped
|
| 190 |
+
if warnings:
|
| 191 |
+
final = f"{final}\n\n--- Context Warnings ---\n" + "\n".join(f"- {warning}" for warning in warnings)
|
| 192 |
+
if blocks:
|
| 193 |
+
final = f"{final}\n\n--- Attached Context ---\n\n" + "\n\n".join(blocks)
|
| 194 |
+
|
| 195 |
+
return ContextReferenceResult(
|
| 196 |
+
message=final.strip(),
|
| 197 |
+
original_message=message,
|
| 198 |
+
references=refs,
|
| 199 |
+
warnings=warnings,
|
| 200 |
+
injected_tokens=injected_tokens,
|
| 201 |
+
expanded=bool(blocks or warnings),
|
| 202 |
+
blocked=False,
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
async def _expand_reference(
|
| 207 |
+
ref: ContextReference,
|
| 208 |
+
cwd: Path,
|
| 209 |
+
*,
|
| 210 |
+
url_fetcher: Callable[[str], str | Awaitable[str]] | None = None,
|
| 211 |
+
allowed_root: Path | None = None,
|
| 212 |
+
) -> tuple[str | None, str | None]:
|
| 213 |
+
try:
|
| 214 |
+
if ref.kind == "file":
|
| 215 |
+
return _expand_file_reference(ref, cwd, allowed_root=allowed_root)
|
| 216 |
+
if ref.kind == "folder":
|
| 217 |
+
return _expand_folder_reference(ref, cwd, allowed_root=allowed_root)
|
| 218 |
+
if ref.kind == "diff":
|
| 219 |
+
return _expand_git_reference(ref, cwd, ["diff"], "git diff")
|
| 220 |
+
if ref.kind == "staged":
|
| 221 |
+
return _expand_git_reference(ref, cwd, ["diff", "--staged"], "git diff --staged")
|
| 222 |
+
if ref.kind == "git":
|
| 223 |
+
count = max(1, min(int(ref.target or "1"), 10))
|
| 224 |
+
return _expand_git_reference(ref, cwd, ["log", f"-{count}", "-p"], f"git log -{count} -p")
|
| 225 |
+
if ref.kind == "url":
|
| 226 |
+
content = await _fetch_url_content(ref.target, url_fetcher=url_fetcher)
|
| 227 |
+
if not content:
|
| 228 |
+
return f"{ref.raw}: no content extracted", None
|
| 229 |
+
return None, f"🌐 {ref.raw} ({estimate_tokens_rough(content)} tokens)\n{content}"
|
| 230 |
+
except Exception as exc:
|
| 231 |
+
return f"{ref.raw}: {exc}", None
|
| 232 |
+
|
| 233 |
+
return f"{ref.raw}: unsupported reference type", None
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
def _expand_file_reference(
|
| 237 |
+
ref: ContextReference,
|
| 238 |
+
cwd: Path,
|
| 239 |
+
*,
|
| 240 |
+
allowed_root: Path | None = None,
|
| 241 |
+
) -> tuple[str | None, str | None]:
|
| 242 |
+
path = _resolve_path(cwd, ref.target, allowed_root=allowed_root)
|
| 243 |
+
_ensure_reference_path_allowed(path)
|
| 244 |
+
if not path.exists():
|
| 245 |
+
return f"{ref.raw}: file not found", None
|
| 246 |
+
if not path.is_file():
|
| 247 |
+
return f"{ref.raw}: path is not a file", None
|
| 248 |
+
if _is_binary_file(path):
|
| 249 |
+
return f"{ref.raw}: binary files are not supported", None
|
| 250 |
+
|
| 251 |
+
text = path.read_text(encoding="utf-8")
|
| 252 |
+
if ref.line_start is not None:
|
| 253 |
+
lines = text.splitlines()
|
| 254 |
+
start_idx = max(ref.line_start - 1, 0)
|
| 255 |
+
end_idx = min(ref.line_end or ref.line_start, len(lines))
|
| 256 |
+
text = "\n".join(lines[start_idx:end_idx])
|
| 257 |
+
|
| 258 |
+
lang = _code_fence_language(path)
|
| 259 |
+
label = ref.raw
|
| 260 |
+
return None, f"📄 {label} ({estimate_tokens_rough(text)} tokens)\n```{lang}\n{text}\n```"
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
def _expand_folder_reference(
|
| 264 |
+
ref: ContextReference,
|
| 265 |
+
cwd: Path,
|
| 266 |
+
*,
|
| 267 |
+
allowed_root: Path | None = None,
|
| 268 |
+
) -> tuple[str | None, str | None]:
|
| 269 |
+
path = _resolve_path(cwd, ref.target, allowed_root=allowed_root)
|
| 270 |
+
_ensure_reference_path_allowed(path)
|
| 271 |
+
if not path.exists():
|
| 272 |
+
return f"{ref.raw}: folder not found", None
|
| 273 |
+
if not path.is_dir():
|
| 274 |
+
return f"{ref.raw}: path is not a folder", None
|
| 275 |
+
|
| 276 |
+
listing = _build_folder_listing(path, cwd)
|
| 277 |
+
return None, f"📁 {ref.raw} ({estimate_tokens_rough(listing)} tokens)\n{listing}"
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
def _expand_git_reference(
|
| 281 |
+
ref: ContextReference,
|
| 282 |
+
cwd: Path,
|
| 283 |
+
args: list[str],
|
| 284 |
+
label: str,
|
| 285 |
+
) -> tuple[str | None, str | None]:
|
| 286 |
+
try:
|
| 287 |
+
result = subprocess.run(
|
| 288 |
+
["git", *args],
|
| 289 |
+
cwd=cwd,
|
| 290 |
+
capture_output=True,
|
| 291 |
+
text=True,
|
| 292 |
+
timeout=30,
|
| 293 |
+
)
|
| 294 |
+
except subprocess.TimeoutExpired:
|
| 295 |
+
return f"{ref.raw}: git command timed out (30s)", None
|
| 296 |
+
if result.returncode != 0:
|
| 297 |
+
stderr = (result.stderr or "").strip() or "git command failed"
|
| 298 |
+
return f"{ref.raw}: {stderr}", None
|
| 299 |
+
content = result.stdout.strip()
|
| 300 |
+
if not content:
|
| 301 |
+
content = "(no output)"
|
| 302 |
+
return None, f"🧾 {label} ({estimate_tokens_rough(content)} tokens)\n```diff\n{content}\n```"
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
async def _fetch_url_content(
|
| 306 |
+
url: str,
|
| 307 |
+
*,
|
| 308 |
+
url_fetcher: Callable[[str], str | Awaitable[str]] | None = None,
|
| 309 |
+
) -> str:
|
| 310 |
+
fetcher = url_fetcher or _default_url_fetcher
|
| 311 |
+
content = fetcher(url)
|
| 312 |
+
if inspect.isawaitable(content):
|
| 313 |
+
content = await content
|
| 314 |
+
return str(content or "").strip()
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
async def _default_url_fetcher(url: str) -> str:
|
| 318 |
+
from tools.web_tools import web_extract_tool
|
| 319 |
+
|
| 320 |
+
raw = await web_extract_tool([url], format="markdown", use_llm_processing=True)
|
| 321 |
+
payload = json.loads(raw)
|
| 322 |
+
docs = payload.get("data", {}).get("documents", [])
|
| 323 |
+
if not docs:
|
| 324 |
+
return ""
|
| 325 |
+
doc = docs[0]
|
| 326 |
+
return str(doc.get("content") or doc.get("raw_content") or "").strip()
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
def _resolve_path(cwd: Path, target: str, *, allowed_root: Path | None = None) -> Path:
|
| 330 |
+
path = Path(os.path.expanduser(target))
|
| 331 |
+
if not path.is_absolute():
|
| 332 |
+
path = cwd / path
|
| 333 |
+
resolved = path.resolve()
|
| 334 |
+
if allowed_root is not None:
|
| 335 |
+
try:
|
| 336 |
+
resolved.relative_to(allowed_root)
|
| 337 |
+
except ValueError as exc:
|
| 338 |
+
raise ValueError("path is outside the allowed workspace") from exc
|
| 339 |
+
return resolved
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
def _ensure_reference_path_allowed(path: Path) -> None:
|
| 343 |
+
from hermes_constants import get_hermes_home
|
| 344 |
+
home = Path(os.path.expanduser("~")).resolve()
|
| 345 |
+
hermes_home = get_hermes_home().resolve()
|
| 346 |
+
|
| 347 |
+
blocked_exact = {home / rel for rel in _SENSITIVE_HOME_FILES}
|
| 348 |
+
blocked_exact.add(hermes_home / ".env")
|
| 349 |
+
blocked_dirs = [home / rel for rel in _SENSITIVE_HOME_DIRS]
|
| 350 |
+
blocked_dirs.extend(hermes_home / rel for rel in _SENSITIVE_HERMES_DIRS)
|
| 351 |
+
|
| 352 |
+
if path in blocked_exact:
|
| 353 |
+
raise ValueError("path is a sensitive credential file and cannot be attached")
|
| 354 |
+
|
| 355 |
+
for blocked_dir in blocked_dirs:
|
| 356 |
+
try:
|
| 357 |
+
path.relative_to(blocked_dir)
|
| 358 |
+
except ValueError:
|
| 359 |
+
continue
|
| 360 |
+
raise ValueError("path is a sensitive credential or internal Hermes path and cannot be attached")
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
def _strip_trailing_punctuation(value: str) -> str:
|
| 364 |
+
stripped = value.rstrip(TRAILING_PUNCTUATION)
|
| 365 |
+
while stripped.endswith((")", "]", "}")):
|
| 366 |
+
closer = stripped[-1]
|
| 367 |
+
opener = {")": "(", "]": "[", "}": "{"}[closer]
|
| 368 |
+
if stripped.count(closer) > stripped.count(opener):
|
| 369 |
+
stripped = stripped[:-1]
|
| 370 |
+
continue
|
| 371 |
+
break
|
| 372 |
+
return stripped
|
| 373 |
+
|
| 374 |
+
|
| 375 |
+
def _strip_reference_wrappers(value: str) -> str:
|
| 376 |
+
if len(value) >= 2 and value[0] == value[-1] and value[0] in "`\"'":
|
| 377 |
+
return value[1:-1]
|
| 378 |
+
return value
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
def _parse_file_reference_value(value: str) -> tuple[str, int | None, int | None]:
|
| 382 |
+
quoted_match = re.match(
|
| 383 |
+
r'^(?P<quote>`|"|\')(?P<path>.+?)(?P=quote)(?::(?P<start>\d+)(?:-(?P<end>\d+))?)?$',
|
| 384 |
+
value,
|
| 385 |
+
)
|
| 386 |
+
if quoted_match:
|
| 387 |
+
line_start = quoted_match.group("start")
|
| 388 |
+
line_end = quoted_match.group("end")
|
| 389 |
+
return (
|
| 390 |
+
quoted_match.group("path"),
|
| 391 |
+
int(line_start) if line_start is not None else None,
|
| 392 |
+
int(line_end or line_start) if line_start is not None else None,
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
range_match = re.match(r"^(?P<path>.+?):(?P<start>\d+)(?:-(?P<end>\d+))?$", value)
|
| 396 |
+
if range_match:
|
| 397 |
+
line_start = int(range_match.group("start"))
|
| 398 |
+
return (
|
| 399 |
+
range_match.group("path"),
|
| 400 |
+
line_start,
|
| 401 |
+
int(range_match.group("end") or range_match.group("start")),
|
| 402 |
+
)
|
| 403 |
+
|
| 404 |
+
return _strip_reference_wrappers(value), None, None
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
def _remove_reference_tokens(message: str, refs: list[ContextReference]) -> str:
|
| 408 |
+
pieces: list[str] = []
|
| 409 |
+
cursor = 0
|
| 410 |
+
for ref in refs:
|
| 411 |
+
pieces.append(message[cursor:ref.start])
|
| 412 |
+
cursor = ref.end
|
| 413 |
+
pieces.append(message[cursor:])
|
| 414 |
+
text = "".join(pieces)
|
| 415 |
+
text = re.sub(r"\s{2,}", " ", text)
|
| 416 |
+
text = re.sub(r"\s+([,.;:!?])", r"\1", text)
|
| 417 |
+
return text.strip()
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def _is_binary_file(path: Path) -> bool:
|
| 421 |
+
mime, _ = mimetypes.guess_type(path.name)
|
| 422 |
+
if mime and not mime.startswith("text/") and not any(
|
| 423 |
+
path.name.endswith(ext) for ext in (".py", ".md", ".txt", ".json", ".yaml", ".yml", ".toml", ".js", ".ts")
|
| 424 |
+
):
|
| 425 |
+
return True
|
| 426 |
+
chunk = path.read_bytes()[:4096]
|
| 427 |
+
return b"\x00" in chunk
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
def _build_folder_listing(path: Path, cwd: Path, limit: int = 200) -> str:
|
| 431 |
+
lines = [f"{path.relative_to(cwd)}/"]
|
| 432 |
+
entries = _iter_visible_entries(path, cwd, limit=limit)
|
| 433 |
+
for entry in entries:
|
| 434 |
+
rel = entry.relative_to(cwd)
|
| 435 |
+
indent = " " * max(len(rel.parts) - len(path.relative_to(cwd).parts) - 1, 0)
|
| 436 |
+
if entry.is_dir():
|
| 437 |
+
lines.append(f"{indent}- {entry.name}/")
|
| 438 |
+
else:
|
| 439 |
+
meta = _file_metadata(entry)
|
| 440 |
+
lines.append(f"{indent}- {entry.name} ({meta})")
|
| 441 |
+
if len(entries) >= limit:
|
| 442 |
+
lines.append("- ...")
|
| 443 |
+
return "\n".join(lines)
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
def _iter_visible_entries(path: Path, cwd: Path, limit: int) -> list[Path]:
|
| 447 |
+
rg_entries = _rg_files(path, cwd, limit=limit)
|
| 448 |
+
if rg_entries is not None:
|
| 449 |
+
output: list[Path] = []
|
| 450 |
+
seen_dirs: set[Path] = set()
|
| 451 |
+
for rel in rg_entries:
|
| 452 |
+
full = cwd / rel
|
| 453 |
+
for parent in full.parents:
|
| 454 |
+
if parent == cwd or parent in seen_dirs or path not in {parent, *parent.parents}:
|
| 455 |
+
continue
|
| 456 |
+
seen_dirs.add(parent)
|
| 457 |
+
output.append(parent)
|
| 458 |
+
output.append(full)
|
| 459 |
+
return sorted({p for p in output if p.exists()}, key=lambda p: (not p.is_dir(), str(p)))
|
| 460 |
+
|
| 461 |
+
output = []
|
| 462 |
+
for root, dirs, files in os.walk(path):
|
| 463 |
+
dirs[:] = sorted(d for d in dirs if not d.startswith(".") and d != "__pycache__")
|
| 464 |
+
files = sorted(f for f in files if not f.startswith("."))
|
| 465 |
+
root_path = Path(root)
|
| 466 |
+
for d in dirs:
|
| 467 |
+
output.append(root_path / d)
|
| 468 |
+
if len(output) >= limit:
|
| 469 |
+
return output
|
| 470 |
+
for f in files:
|
| 471 |
+
output.append(root_path / f)
|
| 472 |
+
if len(output) >= limit:
|
| 473 |
+
return output
|
| 474 |
+
return output
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
def _rg_files(path: Path, cwd: Path, limit: int) -> list[Path] | None:
|
| 478 |
+
try:
|
| 479 |
+
result = subprocess.run(
|
| 480 |
+
["rg", "--files", str(path.relative_to(cwd))],
|
| 481 |
+
cwd=cwd,
|
| 482 |
+
capture_output=True,
|
| 483 |
+
text=True,
|
| 484 |
+
timeout=10,
|
| 485 |
+
)
|
| 486 |
+
except FileNotFoundError:
|
| 487 |
+
return None
|
| 488 |
+
except subprocess.TimeoutExpired:
|
| 489 |
+
return None
|
| 490 |
+
if result.returncode != 0:
|
| 491 |
+
return None
|
| 492 |
+
files = [Path(line.strip()) for line in result.stdout.splitlines() if line.strip()]
|
| 493 |
+
return files[:limit]
|
| 494 |
+
|
| 495 |
+
|
| 496 |
+
def _file_metadata(path: Path) -> str:
|
| 497 |
+
if _is_binary_file(path):
|
| 498 |
+
return f"{path.stat().st_size} bytes"
|
| 499 |
+
try:
|
| 500 |
+
line_count = path.read_text(encoding="utf-8").count("\n") + 1
|
| 501 |
+
except Exception:
|
| 502 |
+
return f"{path.stat().st_size} bytes"
|
| 503 |
+
return f"{line_count} lines"
|
| 504 |
+
|
| 505 |
+
|
| 506 |
+
def _code_fence_language(path: Path) -> str:
|
| 507 |
+
mapping = {
|
| 508 |
+
".py": "python",
|
| 509 |
+
".js": "javascript",
|
| 510 |
+
".ts": "typescript",
|
| 511 |
+
".tsx": "tsx",
|
| 512 |
+
".jsx": "jsx",
|
| 513 |
+
".json": "json",
|
| 514 |
+
".md": "markdown",
|
| 515 |
+
".sh": "bash",
|
| 516 |
+
".yml": "yaml",
|
| 517 |
+
".yaml": "yaml",
|
| 518 |
+
".toml": "toml",
|
| 519 |
+
}
|
| 520 |
+
return mapping.get(path.suffix.lower(), "")
|
agent/copilot_acp_client.py
ADDED
|
@@ -0,0 +1,570 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""OpenAI-compatible shim that forwards Hermes requests to `copilot --acp`.
|
| 2 |
+
|
| 3 |
+
This adapter lets Hermes treat the GitHub Copilot ACP server as a chat-style
|
| 4 |
+
backend. Each request starts a short-lived ACP session, sends the formatted
|
| 5 |
+
conversation as a single prompt, collects text chunks, and converts the result
|
| 6 |
+
back into the minimal shape Hermes expects from an OpenAI client.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import json
|
| 12 |
+
import os
|
| 13 |
+
import queue
|
| 14 |
+
import re
|
| 15 |
+
import shlex
|
| 16 |
+
import subprocess
|
| 17 |
+
import threading
|
| 18 |
+
import time
|
| 19 |
+
from collections import deque
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
from types import SimpleNamespace
|
| 22 |
+
from typing import Any
|
| 23 |
+
|
| 24 |
+
ACP_MARKER_BASE_URL = "acp://copilot"
|
| 25 |
+
_DEFAULT_TIMEOUT_SECONDS = 900.0
|
| 26 |
+
|
| 27 |
+
_TOOL_CALL_BLOCK_RE = re.compile(r"<tool_call>\s*(\{.*?\})\s*</tool_call>", re.DOTALL)
|
| 28 |
+
_TOOL_CALL_JSON_RE = re.compile(r"\{\s*\"id\"\s*:\s*\"[^\"]+\"\s*,\s*\"type\"\s*:\s*\"function\"\s*,\s*\"function\"\s*:\s*\{.*?\}\s*\}", re.DOTALL)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _resolve_command() -> str:
|
| 32 |
+
return (
|
| 33 |
+
os.getenv("HERMES_COPILOT_ACP_COMMAND", "").strip()
|
| 34 |
+
or os.getenv("COPILOT_CLI_PATH", "").strip()
|
| 35 |
+
or "copilot"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def _resolve_args() -> list[str]:
|
| 40 |
+
raw = os.getenv("HERMES_COPILOT_ACP_ARGS", "").strip()
|
| 41 |
+
if not raw:
|
| 42 |
+
return ["--acp", "--stdio"]
|
| 43 |
+
return shlex.split(raw)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _jsonrpc_error(message_id: Any, code: int, message: str) -> dict[str, Any]:
|
| 47 |
+
return {
|
| 48 |
+
"jsonrpc": "2.0",
|
| 49 |
+
"id": message_id,
|
| 50 |
+
"error": {
|
| 51 |
+
"code": code,
|
| 52 |
+
"message": message,
|
| 53 |
+
},
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _format_messages_as_prompt(
|
| 58 |
+
messages: list[dict[str, Any]],
|
| 59 |
+
model: str | None = None,
|
| 60 |
+
tools: list[dict[str, Any]] | None = None,
|
| 61 |
+
tool_choice: Any = None,
|
| 62 |
+
) -> str:
|
| 63 |
+
sections: list[str] = [
|
| 64 |
+
"You are being used as the active ACP agent backend for Hermes.",
|
| 65 |
+
"Use ACP capabilities to complete tasks.",
|
| 66 |
+
"IMPORTANT: If you take an action with a tool, you MUST output tool calls using <tool_call>{...}</tool_call> blocks with JSON exactly in OpenAI function-call shape.",
|
| 67 |
+
"If no tool is needed, answer normally.",
|
| 68 |
+
]
|
| 69 |
+
if model:
|
| 70 |
+
sections.append(f"Hermes requested model hint: {model}")
|
| 71 |
+
|
| 72 |
+
if isinstance(tools, list) and tools:
|
| 73 |
+
tool_specs: list[dict[str, Any]] = []
|
| 74 |
+
for t in tools:
|
| 75 |
+
if not isinstance(t, dict):
|
| 76 |
+
continue
|
| 77 |
+
fn = t.get("function") or {}
|
| 78 |
+
if not isinstance(fn, dict):
|
| 79 |
+
continue
|
| 80 |
+
name = fn.get("name")
|
| 81 |
+
if not isinstance(name, str) or not name.strip():
|
| 82 |
+
continue
|
| 83 |
+
tool_specs.append(
|
| 84 |
+
{
|
| 85 |
+
"name": name.strip(),
|
| 86 |
+
"description": fn.get("description", ""),
|
| 87 |
+
"parameters": fn.get("parameters", {}),
|
| 88 |
+
}
|
| 89 |
+
)
|
| 90 |
+
if tool_specs:
|
| 91 |
+
sections.append(
|
| 92 |
+
"Available tools (OpenAI function schema). "
|
| 93 |
+
"When using a tool, emit ONLY <tool_call>{...}</tool_call> with one JSON object "
|
| 94 |
+
"containing id/type/function{name,arguments}. arguments must be a JSON string.\n"
|
| 95 |
+
+ json.dumps(tool_specs, ensure_ascii=False)
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
if tool_choice is not None:
|
| 99 |
+
sections.append(f"Tool choice hint: {json.dumps(tool_choice, ensure_ascii=False)}")
|
| 100 |
+
|
| 101 |
+
transcript: list[str] = []
|
| 102 |
+
for message in messages:
|
| 103 |
+
if not isinstance(message, dict):
|
| 104 |
+
continue
|
| 105 |
+
role = str(message.get("role") or "unknown").strip().lower()
|
| 106 |
+
if role == "tool":
|
| 107 |
+
role = "tool"
|
| 108 |
+
elif role not in {"system", "user", "assistant"}:
|
| 109 |
+
role = "context"
|
| 110 |
+
|
| 111 |
+
content = message.get("content")
|
| 112 |
+
rendered = _render_message_content(content)
|
| 113 |
+
if not rendered:
|
| 114 |
+
continue
|
| 115 |
+
|
| 116 |
+
label = {
|
| 117 |
+
"system": "System",
|
| 118 |
+
"user": "User",
|
| 119 |
+
"assistant": "Assistant",
|
| 120 |
+
"tool": "Tool",
|
| 121 |
+
"context": "Context",
|
| 122 |
+
}.get(role, role.title())
|
| 123 |
+
transcript.append(f"{label}:\n{rendered}")
|
| 124 |
+
|
| 125 |
+
if transcript:
|
| 126 |
+
sections.append("Conversation transcript:\n\n" + "\n\n".join(transcript))
|
| 127 |
+
|
| 128 |
+
sections.append("Continue the conversation from the latest user request.")
|
| 129 |
+
return "\n\n".join(section.strip() for section in sections if section and section.strip())
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def _render_message_content(content: Any) -> str:
|
| 133 |
+
if content is None:
|
| 134 |
+
return ""
|
| 135 |
+
if isinstance(content, str):
|
| 136 |
+
return content.strip()
|
| 137 |
+
if isinstance(content, dict):
|
| 138 |
+
if "text" in content:
|
| 139 |
+
return str(content.get("text") or "").strip()
|
| 140 |
+
if "content" in content and isinstance(content.get("content"), str):
|
| 141 |
+
return str(content.get("content") or "").strip()
|
| 142 |
+
return json.dumps(content, ensure_ascii=True)
|
| 143 |
+
if isinstance(content, list):
|
| 144 |
+
parts: list[str] = []
|
| 145 |
+
for item in content:
|
| 146 |
+
if isinstance(item, str):
|
| 147 |
+
parts.append(item)
|
| 148 |
+
elif isinstance(item, dict):
|
| 149 |
+
text = item.get("text")
|
| 150 |
+
if isinstance(text, str) and text.strip():
|
| 151 |
+
parts.append(text.strip())
|
| 152 |
+
return "\n".join(parts).strip()
|
| 153 |
+
return str(content).strip()
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def _extract_tool_calls_from_text(text: str) -> tuple[list[SimpleNamespace], str]:
|
| 157 |
+
if not isinstance(text, str) or not text.strip():
|
| 158 |
+
return [], ""
|
| 159 |
+
|
| 160 |
+
extracted: list[SimpleNamespace] = []
|
| 161 |
+
consumed_spans: list[tuple[int, int]] = []
|
| 162 |
+
|
| 163 |
+
def _try_add_tool_call(raw_json: str) -> None:
|
| 164 |
+
try:
|
| 165 |
+
obj = json.loads(raw_json)
|
| 166 |
+
except Exception:
|
| 167 |
+
return
|
| 168 |
+
if not isinstance(obj, dict):
|
| 169 |
+
return
|
| 170 |
+
fn = obj.get("function")
|
| 171 |
+
if not isinstance(fn, dict):
|
| 172 |
+
return
|
| 173 |
+
fn_name = fn.get("name")
|
| 174 |
+
if not isinstance(fn_name, str) or not fn_name.strip():
|
| 175 |
+
return
|
| 176 |
+
fn_args = fn.get("arguments", "{}")
|
| 177 |
+
if not isinstance(fn_args, str):
|
| 178 |
+
fn_args = json.dumps(fn_args, ensure_ascii=False)
|
| 179 |
+
call_id = obj.get("id")
|
| 180 |
+
if not isinstance(call_id, str) or not call_id.strip():
|
| 181 |
+
call_id = f"acp_call_{len(extracted)+1}"
|
| 182 |
+
|
| 183 |
+
extracted.append(
|
| 184 |
+
SimpleNamespace(
|
| 185 |
+
id=call_id,
|
| 186 |
+
call_id=call_id,
|
| 187 |
+
response_item_id=None,
|
| 188 |
+
type="function",
|
| 189 |
+
function=SimpleNamespace(name=fn_name.strip(), arguments=fn_args),
|
| 190 |
+
)
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
for m in _TOOL_CALL_BLOCK_RE.finditer(text):
|
| 194 |
+
raw = m.group(1)
|
| 195 |
+
_try_add_tool_call(raw)
|
| 196 |
+
consumed_spans.append((m.start(), m.end()))
|
| 197 |
+
|
| 198 |
+
# Only try bare-JSON fallback when no XML blocks were found.
|
| 199 |
+
if not extracted:
|
| 200 |
+
for m in _TOOL_CALL_JSON_RE.finditer(text):
|
| 201 |
+
raw = m.group(0)
|
| 202 |
+
_try_add_tool_call(raw)
|
| 203 |
+
consumed_spans.append((m.start(), m.end()))
|
| 204 |
+
|
| 205 |
+
if not consumed_spans:
|
| 206 |
+
return extracted, text.strip()
|
| 207 |
+
|
| 208 |
+
consumed_spans.sort()
|
| 209 |
+
merged: list[tuple[int, int]] = []
|
| 210 |
+
for start, end in consumed_spans:
|
| 211 |
+
if not merged or start > merged[-1][1]:
|
| 212 |
+
merged.append((start, end))
|
| 213 |
+
else:
|
| 214 |
+
merged[-1] = (merged[-1][0], max(merged[-1][1], end))
|
| 215 |
+
|
| 216 |
+
parts: list[str] = []
|
| 217 |
+
cursor = 0
|
| 218 |
+
for start, end in merged:
|
| 219 |
+
if cursor < start:
|
| 220 |
+
parts.append(text[cursor:start])
|
| 221 |
+
cursor = max(cursor, end)
|
| 222 |
+
if cursor < len(text):
|
| 223 |
+
parts.append(text[cursor:])
|
| 224 |
+
|
| 225 |
+
cleaned = "\n".join(p.strip() for p in parts if p and p.strip()).strip()
|
| 226 |
+
return extracted, cleaned
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
def _ensure_path_within_cwd(path_text: str, cwd: str) -> Path:
|
| 231 |
+
candidate = Path(path_text)
|
| 232 |
+
if not candidate.is_absolute():
|
| 233 |
+
raise PermissionError("ACP file-system paths must be absolute.")
|
| 234 |
+
resolved = candidate.resolve()
|
| 235 |
+
root = Path(cwd).resolve()
|
| 236 |
+
try:
|
| 237 |
+
resolved.relative_to(root)
|
| 238 |
+
except ValueError as exc:
|
| 239 |
+
raise PermissionError(f"Path '{resolved}' is outside the session cwd '{root}'.") from exc
|
| 240 |
+
return resolved
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
class _ACPChatCompletions:
|
| 244 |
+
def __init__(self, client: "CopilotACPClient"):
|
| 245 |
+
self._client = client
|
| 246 |
+
|
| 247 |
+
def create(self, **kwargs: Any) -> Any:
|
| 248 |
+
return self._client._create_chat_completion(**kwargs)
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
class _ACPChatNamespace:
|
| 252 |
+
def __init__(self, client: "CopilotACPClient"):
|
| 253 |
+
self.completions = _ACPChatCompletions(client)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
class CopilotACPClient:
|
| 257 |
+
"""Minimal OpenAI-client-compatible facade for Copilot ACP."""
|
| 258 |
+
|
| 259 |
+
def __init__(
|
| 260 |
+
self,
|
| 261 |
+
*,
|
| 262 |
+
api_key: str | None = None,
|
| 263 |
+
base_url: str | None = None,
|
| 264 |
+
default_headers: dict[str, str] | None = None,
|
| 265 |
+
acp_command: str | None = None,
|
| 266 |
+
acp_args: list[str] | None = None,
|
| 267 |
+
acp_cwd: str | None = None,
|
| 268 |
+
command: str | None = None,
|
| 269 |
+
args: list[str] | None = None,
|
| 270 |
+
**_: Any,
|
| 271 |
+
):
|
| 272 |
+
self.api_key = api_key or "copilot-acp"
|
| 273 |
+
self.base_url = base_url or ACP_MARKER_BASE_URL
|
| 274 |
+
self._default_headers = dict(default_headers or {})
|
| 275 |
+
self._acp_command = acp_command or command or _resolve_command()
|
| 276 |
+
self._acp_args = list(acp_args or args or _resolve_args())
|
| 277 |
+
self._acp_cwd = str(Path(acp_cwd or os.getcwd()).resolve())
|
| 278 |
+
self.chat = _ACPChatNamespace(self)
|
| 279 |
+
self.is_closed = False
|
| 280 |
+
self._active_process: subprocess.Popen[str] | None = None
|
| 281 |
+
self._active_process_lock = threading.Lock()
|
| 282 |
+
|
| 283 |
+
def close(self) -> None:
|
| 284 |
+
proc: subprocess.Popen[str] | None
|
| 285 |
+
with self._active_process_lock:
|
| 286 |
+
proc = self._active_process
|
| 287 |
+
self._active_process = None
|
| 288 |
+
self.is_closed = True
|
| 289 |
+
if proc is None:
|
| 290 |
+
return
|
| 291 |
+
try:
|
| 292 |
+
proc.terminate()
|
| 293 |
+
proc.wait(timeout=2)
|
| 294 |
+
except Exception:
|
| 295 |
+
try:
|
| 296 |
+
proc.kill()
|
| 297 |
+
except Exception:
|
| 298 |
+
pass
|
| 299 |
+
|
| 300 |
+
def _create_chat_completion(
|
| 301 |
+
self,
|
| 302 |
+
*,
|
| 303 |
+
model: str | None = None,
|
| 304 |
+
messages: list[dict[str, Any]] | None = None,
|
| 305 |
+
timeout: float | None = None,
|
| 306 |
+
tools: list[dict[str, Any]] | None = None,
|
| 307 |
+
tool_choice: Any = None,
|
| 308 |
+
**_: Any,
|
| 309 |
+
) -> Any:
|
| 310 |
+
prompt_text = _format_messages_as_prompt(
|
| 311 |
+
messages or [],
|
| 312 |
+
model=model,
|
| 313 |
+
tools=tools,
|
| 314 |
+
tool_choice=tool_choice,
|
| 315 |
+
)
|
| 316 |
+
response_text, reasoning_text = self._run_prompt(
|
| 317 |
+
prompt_text,
|
| 318 |
+
timeout_seconds=float(timeout or _DEFAULT_TIMEOUT_SECONDS),
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
tool_calls, cleaned_text = _extract_tool_calls_from_text(response_text)
|
| 322 |
+
|
| 323 |
+
usage = SimpleNamespace(
|
| 324 |
+
prompt_tokens=0,
|
| 325 |
+
completion_tokens=0,
|
| 326 |
+
total_tokens=0,
|
| 327 |
+
prompt_tokens_details=SimpleNamespace(cached_tokens=0),
|
| 328 |
+
)
|
| 329 |
+
assistant_message = SimpleNamespace(
|
| 330 |
+
content=cleaned_text,
|
| 331 |
+
tool_calls=tool_calls,
|
| 332 |
+
reasoning=reasoning_text or None,
|
| 333 |
+
reasoning_content=reasoning_text or None,
|
| 334 |
+
reasoning_details=None,
|
| 335 |
+
)
|
| 336 |
+
finish_reason = "tool_calls" if tool_calls else "stop"
|
| 337 |
+
choice = SimpleNamespace(message=assistant_message, finish_reason=finish_reason)
|
| 338 |
+
return SimpleNamespace(
|
| 339 |
+
choices=[choice],
|
| 340 |
+
usage=usage,
|
| 341 |
+
model=model or "copilot-acp",
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
def _run_prompt(self, prompt_text: str, *, timeout_seconds: float) -> tuple[str, str]:
|
| 345 |
+
try:
|
| 346 |
+
proc = subprocess.Popen(
|
| 347 |
+
[self._acp_command] + self._acp_args,
|
| 348 |
+
stdin=subprocess.PIPE,
|
| 349 |
+
stdout=subprocess.PIPE,
|
| 350 |
+
stderr=subprocess.PIPE,
|
| 351 |
+
text=True,
|
| 352 |
+
bufsize=1,
|
| 353 |
+
cwd=self._acp_cwd,
|
| 354 |
+
)
|
| 355 |
+
except FileNotFoundError as exc:
|
| 356 |
+
raise RuntimeError(
|
| 357 |
+
f"Could not start Copilot ACP command '{self._acp_command}'. "
|
| 358 |
+
"Install GitHub Copilot CLI or set HERMES_COPILOT_ACP_COMMAND/COPILOT_CLI_PATH."
|
| 359 |
+
) from exc
|
| 360 |
+
|
| 361 |
+
if proc.stdin is None or proc.stdout is None:
|
| 362 |
+
proc.kill()
|
| 363 |
+
raise RuntimeError("Copilot ACP process did not expose stdin/stdout pipes.")
|
| 364 |
+
|
| 365 |
+
self.is_closed = False
|
| 366 |
+
with self._active_process_lock:
|
| 367 |
+
self._active_process = proc
|
| 368 |
+
|
| 369 |
+
inbox: queue.Queue[dict[str, Any]] = queue.Queue()
|
| 370 |
+
stderr_tail: deque[str] = deque(maxlen=40)
|
| 371 |
+
|
| 372 |
+
def _stdout_reader() -> None:
|
| 373 |
+
for line in proc.stdout:
|
| 374 |
+
try:
|
| 375 |
+
inbox.put(json.loads(line))
|
| 376 |
+
except Exception:
|
| 377 |
+
inbox.put({"raw": line.rstrip("\n")})
|
| 378 |
+
|
| 379 |
+
def _stderr_reader() -> None:
|
| 380 |
+
if proc.stderr is None:
|
| 381 |
+
return
|
| 382 |
+
for line in proc.stderr:
|
| 383 |
+
stderr_tail.append(line.rstrip("\n"))
|
| 384 |
+
|
| 385 |
+
out_thread = threading.Thread(target=_stdout_reader, daemon=True)
|
| 386 |
+
err_thread = threading.Thread(target=_stderr_reader, daemon=True)
|
| 387 |
+
out_thread.start()
|
| 388 |
+
err_thread.start()
|
| 389 |
+
|
| 390 |
+
next_id = 0
|
| 391 |
+
|
| 392 |
+
def _request(method: str, params: dict[str, Any], *, text_parts: list[str] | None = None, reasoning_parts: list[str] | None = None) -> Any:
|
| 393 |
+
nonlocal next_id
|
| 394 |
+
next_id += 1
|
| 395 |
+
request_id = next_id
|
| 396 |
+
payload = {
|
| 397 |
+
"jsonrpc": "2.0",
|
| 398 |
+
"id": request_id,
|
| 399 |
+
"method": method,
|
| 400 |
+
"params": params,
|
| 401 |
+
}
|
| 402 |
+
proc.stdin.write(json.dumps(payload) + "\n")
|
| 403 |
+
proc.stdin.flush()
|
| 404 |
+
|
| 405 |
+
deadline = time.time() + timeout_seconds
|
| 406 |
+
while time.time() < deadline:
|
| 407 |
+
if proc.poll() is not None:
|
| 408 |
+
break
|
| 409 |
+
try:
|
| 410 |
+
msg = inbox.get(timeout=0.1)
|
| 411 |
+
except queue.Empty:
|
| 412 |
+
continue
|
| 413 |
+
|
| 414 |
+
if self._handle_server_message(
|
| 415 |
+
msg,
|
| 416 |
+
process=proc,
|
| 417 |
+
cwd=self._acp_cwd,
|
| 418 |
+
text_parts=text_parts,
|
| 419 |
+
reasoning_parts=reasoning_parts,
|
| 420 |
+
):
|
| 421 |
+
continue
|
| 422 |
+
|
| 423 |
+
if msg.get("id") != request_id:
|
| 424 |
+
continue
|
| 425 |
+
if "error" in msg:
|
| 426 |
+
err = msg.get("error") or {}
|
| 427 |
+
raise RuntimeError(
|
| 428 |
+
f"Copilot ACP {method} failed: {err.get('message') or err}"
|
| 429 |
+
)
|
| 430 |
+
return msg.get("result")
|
| 431 |
+
|
| 432 |
+
stderr_text = "\n".join(stderr_tail).strip()
|
| 433 |
+
if proc.poll() is not None and stderr_text:
|
| 434 |
+
raise RuntimeError(f"Copilot ACP process exited early: {stderr_text}")
|
| 435 |
+
raise TimeoutError(f"Timed out waiting for Copilot ACP response to {method}.")
|
| 436 |
+
|
| 437 |
+
try:
|
| 438 |
+
_request(
|
| 439 |
+
"initialize",
|
| 440 |
+
{
|
| 441 |
+
"protocolVersion": 1,
|
| 442 |
+
"clientCapabilities": {
|
| 443 |
+
"fs": {
|
| 444 |
+
"readTextFile": True,
|
| 445 |
+
"writeTextFile": True,
|
| 446 |
+
}
|
| 447 |
+
},
|
| 448 |
+
"clientInfo": {
|
| 449 |
+
"name": "hermes-agent",
|
| 450 |
+
"title": "Hermes Agent",
|
| 451 |
+
"version": "0.0.0",
|
| 452 |
+
},
|
| 453 |
+
},
|
| 454 |
+
)
|
| 455 |
+
session = _request(
|
| 456 |
+
"session/new",
|
| 457 |
+
{
|
| 458 |
+
"cwd": self._acp_cwd,
|
| 459 |
+
"mcpServers": [],
|
| 460 |
+
},
|
| 461 |
+
) or {}
|
| 462 |
+
session_id = str(session.get("sessionId") or "").strip()
|
| 463 |
+
if not session_id:
|
| 464 |
+
raise RuntimeError("Copilot ACP did not return a sessionId.")
|
| 465 |
+
|
| 466 |
+
text_parts: list[str] = []
|
| 467 |
+
reasoning_parts: list[str] = []
|
| 468 |
+
_request(
|
| 469 |
+
"session/prompt",
|
| 470 |
+
{
|
| 471 |
+
"sessionId": session_id,
|
| 472 |
+
"prompt": [
|
| 473 |
+
{
|
| 474 |
+
"type": "text",
|
| 475 |
+
"text": prompt_text,
|
| 476 |
+
}
|
| 477 |
+
],
|
| 478 |
+
},
|
| 479 |
+
text_parts=text_parts,
|
| 480 |
+
reasoning_parts=reasoning_parts,
|
| 481 |
+
)
|
| 482 |
+
return "".join(text_parts), "".join(reasoning_parts)
|
| 483 |
+
finally:
|
| 484 |
+
self.close()
|
| 485 |
+
|
| 486 |
+
def _handle_server_message(
|
| 487 |
+
self,
|
| 488 |
+
msg: dict[str, Any],
|
| 489 |
+
*,
|
| 490 |
+
process: subprocess.Popen[str],
|
| 491 |
+
cwd: str,
|
| 492 |
+
text_parts: list[str] | None,
|
| 493 |
+
reasoning_parts: list[str] | None,
|
| 494 |
+
) -> bool:
|
| 495 |
+
method = msg.get("method")
|
| 496 |
+
if not isinstance(method, str):
|
| 497 |
+
return False
|
| 498 |
+
|
| 499 |
+
if method == "session/update":
|
| 500 |
+
params = msg.get("params") or {}
|
| 501 |
+
update = params.get("update") or {}
|
| 502 |
+
kind = str(update.get("sessionUpdate") or "").strip()
|
| 503 |
+
content = update.get("content") or {}
|
| 504 |
+
chunk_text = ""
|
| 505 |
+
if isinstance(content, dict):
|
| 506 |
+
chunk_text = str(content.get("text") or "")
|
| 507 |
+
if kind == "agent_message_chunk" and chunk_text and text_parts is not None:
|
| 508 |
+
text_parts.append(chunk_text)
|
| 509 |
+
elif kind == "agent_thought_chunk" and chunk_text and reasoning_parts is not None:
|
| 510 |
+
reasoning_parts.append(chunk_text)
|
| 511 |
+
return True
|
| 512 |
+
|
| 513 |
+
if process.stdin is None:
|
| 514 |
+
return True
|
| 515 |
+
|
| 516 |
+
message_id = msg.get("id")
|
| 517 |
+
params = msg.get("params") or {}
|
| 518 |
+
|
| 519 |
+
if method == "session/request_permission":
|
| 520 |
+
response = {
|
| 521 |
+
"jsonrpc": "2.0",
|
| 522 |
+
"id": message_id,
|
| 523 |
+
"result": {
|
| 524 |
+
"outcome": {
|
| 525 |
+
"outcome": "allow_once",
|
| 526 |
+
}
|
| 527 |
+
},
|
| 528 |
+
}
|
| 529 |
+
elif method == "fs/read_text_file":
|
| 530 |
+
try:
|
| 531 |
+
path = _ensure_path_within_cwd(str(params.get("path") or ""), cwd)
|
| 532 |
+
content = path.read_text() if path.exists() else ""
|
| 533 |
+
line = params.get("line")
|
| 534 |
+
limit = params.get("limit")
|
| 535 |
+
if isinstance(line, int) and line > 1:
|
| 536 |
+
lines = content.splitlines(keepends=True)
|
| 537 |
+
start = line - 1
|
| 538 |
+
end = start + limit if isinstance(limit, int) and limit > 0 else None
|
| 539 |
+
content = "".join(lines[start:end])
|
| 540 |
+
response = {
|
| 541 |
+
"jsonrpc": "2.0",
|
| 542 |
+
"id": message_id,
|
| 543 |
+
"result": {
|
| 544 |
+
"content": content,
|
| 545 |
+
},
|
| 546 |
+
}
|
| 547 |
+
except Exception as exc:
|
| 548 |
+
response = _jsonrpc_error(message_id, -32602, str(exc))
|
| 549 |
+
elif method == "fs/write_text_file":
|
| 550 |
+
try:
|
| 551 |
+
path = _ensure_path_within_cwd(str(params.get("path") or ""), cwd)
|
| 552 |
+
path.parent.mkdir(parents=True, exist_ok=True)
|
| 553 |
+
path.write_text(str(params.get("content") or ""))
|
| 554 |
+
response = {
|
| 555 |
+
"jsonrpc": "2.0",
|
| 556 |
+
"id": message_id,
|
| 557 |
+
"result": None,
|
| 558 |
+
}
|
| 559 |
+
except Exception as exc:
|
| 560 |
+
response = _jsonrpc_error(message_id, -32602, str(exc))
|
| 561 |
+
else:
|
| 562 |
+
response = _jsonrpc_error(
|
| 563 |
+
message_id,
|
| 564 |
+
-32601,
|
| 565 |
+
f"ACP client method '{method}' is not supported by Hermes yet.",
|
| 566 |
+
)
|
| 567 |
+
|
| 568 |
+
process.stdin.write(json.dumps(response) + "\n")
|
| 569 |
+
process.stdin.flush()
|
| 570 |
+
return True
|
agent/credential_pool.py
ADDED
|
@@ -0,0 +1,1319 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Persistent multi-credential pool for same-provider failover."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import logging
|
| 6 |
+
import random
|
| 7 |
+
import threading
|
| 8 |
+
import time
|
| 9 |
+
import uuid
|
| 10 |
+
import os
|
| 11 |
+
import re
|
| 12 |
+
from dataclasses import dataclass, fields, replace
|
| 13 |
+
from datetime import datetime
|
| 14 |
+
from typing import Any, Dict, List, Optional, Set, Tuple
|
| 15 |
+
|
| 16 |
+
from hermes_constants import OPENROUTER_BASE_URL
|
| 17 |
+
import hermes_cli.auth as auth_mod
|
| 18 |
+
from hermes_cli.auth import (
|
| 19 |
+
CODEX_ACCESS_TOKEN_REFRESH_SKEW_SECONDS,
|
| 20 |
+
DEFAULT_AGENT_KEY_MIN_TTL_SECONDS,
|
| 21 |
+
KIMI_CODE_BASE_URL,
|
| 22 |
+
PROVIDER_REGISTRY,
|
| 23 |
+
_auth_store_lock,
|
| 24 |
+
_codex_access_token_is_expiring,
|
| 25 |
+
_decode_jwt_claims,
|
| 26 |
+
_import_codex_cli_tokens,
|
| 27 |
+
_load_auth_store,
|
| 28 |
+
_load_provider_state,
|
| 29 |
+
_resolve_kimi_base_url,
|
| 30 |
+
_resolve_zai_base_url,
|
| 31 |
+
_save_auth_store,
|
| 32 |
+
_save_provider_state,
|
| 33 |
+
read_credential_pool,
|
| 34 |
+
write_credential_pool,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
logger = logging.getLogger(__name__)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _load_config_safe() -> Optional[dict]:
|
| 41 |
+
"""Load config.yaml, returning None on any error."""
|
| 42 |
+
try:
|
| 43 |
+
from hermes_cli.config import load_config
|
| 44 |
+
|
| 45 |
+
return load_config()
|
| 46 |
+
except Exception:
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# --- Status and type constants ---
|
| 51 |
+
|
| 52 |
+
STATUS_OK = "ok"
|
| 53 |
+
STATUS_EXHAUSTED = "exhausted"
|
| 54 |
+
|
| 55 |
+
AUTH_TYPE_OAUTH = "oauth"
|
| 56 |
+
AUTH_TYPE_API_KEY = "api_key"
|
| 57 |
+
|
| 58 |
+
SOURCE_MANUAL = "manual"
|
| 59 |
+
|
| 60 |
+
STRATEGY_FILL_FIRST = "fill_first"
|
| 61 |
+
STRATEGY_ROUND_ROBIN = "round_robin"
|
| 62 |
+
STRATEGY_RANDOM = "random"
|
| 63 |
+
STRATEGY_LEAST_USED = "least_used"
|
| 64 |
+
SUPPORTED_POOL_STRATEGIES = {
|
| 65 |
+
STRATEGY_FILL_FIRST,
|
| 66 |
+
STRATEGY_ROUND_ROBIN,
|
| 67 |
+
STRATEGY_RANDOM,
|
| 68 |
+
STRATEGY_LEAST_USED,
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# Cooldown before retrying an exhausted credential.
|
| 72 |
+
# 429 (rate-limited) and 402 (billing/quota) both cool down after 1 hour.
|
| 73 |
+
# Provider-supplied reset_at timestamps override these defaults.
|
| 74 |
+
EXHAUSTED_TTL_429_SECONDS = 60 * 60 # 1 hour
|
| 75 |
+
EXHAUSTED_TTL_DEFAULT_SECONDS = 60 * 60 # 1 hour
|
| 76 |
+
|
| 77 |
+
# Pool key prefix for custom OpenAI-compatible endpoints.
|
| 78 |
+
# Custom endpoints all share provider='custom' but are keyed by their
|
| 79 |
+
# custom_providers name: 'custom:<normalized_name>'.
|
| 80 |
+
CUSTOM_POOL_PREFIX = "custom:"
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Fields that are only round-tripped through JSON — never used for logic as attributes.
|
| 84 |
+
_EXTRA_KEYS = frozenset({
|
| 85 |
+
"token_type", "scope", "client_id", "portal_base_url", "obtained_at",
|
| 86 |
+
"expires_in", "agent_key_id", "agent_key_expires_in", "agent_key_reused",
|
| 87 |
+
"agent_key_obtained_at", "tls",
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
@dataclass
|
| 92 |
+
class PooledCredential:
|
| 93 |
+
provider: str
|
| 94 |
+
id: str
|
| 95 |
+
label: str
|
| 96 |
+
auth_type: str
|
| 97 |
+
priority: int
|
| 98 |
+
source: str
|
| 99 |
+
access_token: str
|
| 100 |
+
refresh_token: Optional[str] = None
|
| 101 |
+
last_status: Optional[str] = None
|
| 102 |
+
last_status_at: Optional[float] = None
|
| 103 |
+
last_error_code: Optional[int] = None
|
| 104 |
+
last_error_reason: Optional[str] = None
|
| 105 |
+
last_error_message: Optional[str] = None
|
| 106 |
+
last_error_reset_at: Optional[float] = None
|
| 107 |
+
base_url: Optional[str] = None
|
| 108 |
+
expires_at: Optional[str] = None
|
| 109 |
+
expires_at_ms: Optional[int] = None
|
| 110 |
+
last_refresh: Optional[str] = None
|
| 111 |
+
inference_base_url: Optional[str] = None
|
| 112 |
+
agent_key: Optional[str] = None
|
| 113 |
+
agent_key_expires_at: Optional[str] = None
|
| 114 |
+
request_count: int = 0
|
| 115 |
+
extra: Dict[str, Any] = None # type: ignore[assignment]
|
| 116 |
+
|
| 117 |
+
def __post_init__(self):
|
| 118 |
+
if self.extra is None:
|
| 119 |
+
self.extra = {}
|
| 120 |
+
|
| 121 |
+
def __getattr__(self, name: str):
|
| 122 |
+
if name in _EXTRA_KEYS:
|
| 123 |
+
return self.extra.get(name)
|
| 124 |
+
raise AttributeError(f"'{type(self).__name__}' object has no attribute {name!r}")
|
| 125 |
+
|
| 126 |
+
@classmethod
|
| 127 |
+
def from_dict(cls, provider: str, payload: Dict[str, Any]) -> "PooledCredential":
|
| 128 |
+
field_names = {f.name for f in fields(cls) if f.name != "provider"}
|
| 129 |
+
data = {k: payload.get(k) for k in field_names if k in payload}
|
| 130 |
+
extra = {k: payload[k] for k in _EXTRA_KEYS if k in payload and payload[k] is not None}
|
| 131 |
+
data["extra"] = extra
|
| 132 |
+
data.setdefault("id", uuid.uuid4().hex[:6])
|
| 133 |
+
data.setdefault("label", payload.get("source", provider))
|
| 134 |
+
data.setdefault("auth_type", AUTH_TYPE_API_KEY)
|
| 135 |
+
data.setdefault("priority", 0)
|
| 136 |
+
data.setdefault("source", SOURCE_MANUAL)
|
| 137 |
+
data.setdefault("access_token", "")
|
| 138 |
+
return cls(provider=provider, **data)
|
| 139 |
+
|
| 140 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 141 |
+
_ALWAYS_EMIT = {
|
| 142 |
+
"last_status",
|
| 143 |
+
"last_status_at",
|
| 144 |
+
"last_error_code",
|
| 145 |
+
"last_error_reason",
|
| 146 |
+
"last_error_message",
|
| 147 |
+
"last_error_reset_at",
|
| 148 |
+
}
|
| 149 |
+
result: Dict[str, Any] = {}
|
| 150 |
+
for field_def in fields(self):
|
| 151 |
+
if field_def.name in ("provider", "extra"):
|
| 152 |
+
continue
|
| 153 |
+
value = getattr(self, field_def.name)
|
| 154 |
+
if value is not None or field_def.name in _ALWAYS_EMIT:
|
| 155 |
+
result[field_def.name] = value
|
| 156 |
+
for k, v in self.extra.items():
|
| 157 |
+
if v is not None:
|
| 158 |
+
result[k] = v
|
| 159 |
+
return result
|
| 160 |
+
|
| 161 |
+
@property
|
| 162 |
+
def runtime_api_key(self) -> str:
|
| 163 |
+
if self.provider == "nous":
|
| 164 |
+
return str(self.agent_key or self.access_token or "")
|
| 165 |
+
return str(self.access_token or "")
|
| 166 |
+
|
| 167 |
+
@property
|
| 168 |
+
def runtime_base_url(self) -> Optional[str]:
|
| 169 |
+
if self.provider == "nous":
|
| 170 |
+
return self.inference_base_url or self.base_url
|
| 171 |
+
return self.base_url
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def label_from_token(token: str, fallback: str) -> str:
|
| 175 |
+
claims = _decode_jwt_claims(token)
|
| 176 |
+
for key in ("email", "preferred_username", "upn"):
|
| 177 |
+
value = claims.get(key)
|
| 178 |
+
if isinstance(value, str) and value.strip():
|
| 179 |
+
return value.strip()
|
| 180 |
+
return fallback
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def _next_priority(entries: List[PooledCredential]) -> int:
|
| 184 |
+
return max((entry.priority for entry in entries), default=-1) + 1
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def _is_manual_source(source: str) -> bool:
|
| 188 |
+
normalized = (source or "").strip().lower()
|
| 189 |
+
return normalized == SOURCE_MANUAL or normalized.startswith(f"{SOURCE_MANUAL}:")
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
def _exhausted_ttl(error_code: Optional[int]) -> int:
|
| 193 |
+
"""Return cooldown seconds based on the HTTP status that caused exhaustion."""
|
| 194 |
+
if error_code == 429:
|
| 195 |
+
return EXHAUSTED_TTL_429_SECONDS
|
| 196 |
+
return EXHAUSTED_TTL_DEFAULT_SECONDS
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def _parse_absolute_timestamp(value: Any) -> Optional[float]:
|
| 200 |
+
"""Best-effort parse for provider reset timestamps.
|
| 201 |
+
|
| 202 |
+
Accepts epoch seconds, epoch milliseconds, and ISO-8601 strings.
|
| 203 |
+
Returns seconds since epoch.
|
| 204 |
+
"""
|
| 205 |
+
if value is None or value == "":
|
| 206 |
+
return None
|
| 207 |
+
if isinstance(value, (int, float)):
|
| 208 |
+
numeric = float(value)
|
| 209 |
+
if numeric <= 0:
|
| 210 |
+
return None
|
| 211 |
+
return numeric / 1000.0 if numeric > 1_000_000_000_000 else numeric
|
| 212 |
+
if isinstance(value, str):
|
| 213 |
+
raw = value.strip()
|
| 214 |
+
if not raw:
|
| 215 |
+
return None
|
| 216 |
+
try:
|
| 217 |
+
numeric = float(raw)
|
| 218 |
+
except ValueError:
|
| 219 |
+
numeric = None
|
| 220 |
+
if numeric is not None:
|
| 221 |
+
return numeric / 1000.0 if numeric > 1_000_000_000_000 else numeric
|
| 222 |
+
try:
|
| 223 |
+
return datetime.fromisoformat(raw.replace("Z", "+00:00")).timestamp()
|
| 224 |
+
except ValueError:
|
| 225 |
+
return None
|
| 226 |
+
return None
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def _extract_retry_delay_seconds(message: str) -> Optional[float]:
|
| 230 |
+
if not message:
|
| 231 |
+
return None
|
| 232 |
+
delay_match = re.search(r"quotaResetDelay[:\s\"]+(\d+(?:\.\d+)?)(ms|s)", message, re.IGNORECASE)
|
| 233 |
+
if delay_match:
|
| 234 |
+
value = float(delay_match.group(1))
|
| 235 |
+
return value / 1000.0 if delay_match.group(2).lower() == "ms" else value
|
| 236 |
+
sec_match = re.search(r"retry\s+(?:after\s+)?(\d+(?:\.\d+)?)\s*(?:sec|secs|seconds|s\b)", message, re.IGNORECASE)
|
| 237 |
+
if sec_match:
|
| 238 |
+
return float(sec_match.group(1))
|
| 239 |
+
return None
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
def _normalize_error_context(error_context: Optional[Dict[str, Any]]) -> Dict[str, Any]:
|
| 243 |
+
if not isinstance(error_context, dict):
|
| 244 |
+
return {}
|
| 245 |
+
normalized: Dict[str, Any] = {}
|
| 246 |
+
reason = error_context.get("reason")
|
| 247 |
+
if isinstance(reason, str) and reason.strip():
|
| 248 |
+
normalized["reason"] = reason.strip()
|
| 249 |
+
message = error_context.get("message")
|
| 250 |
+
if isinstance(message, str) and message.strip():
|
| 251 |
+
normalized["message"] = message.strip()
|
| 252 |
+
reset_at = (
|
| 253 |
+
error_context.get("reset_at")
|
| 254 |
+
or error_context.get("resets_at")
|
| 255 |
+
or error_context.get("retry_until")
|
| 256 |
+
)
|
| 257 |
+
parsed_reset_at = _parse_absolute_timestamp(reset_at)
|
| 258 |
+
if parsed_reset_at is None and isinstance(message, str):
|
| 259 |
+
retry_delay_seconds = _extract_retry_delay_seconds(message)
|
| 260 |
+
if retry_delay_seconds is not None:
|
| 261 |
+
parsed_reset_at = time.time() + retry_delay_seconds
|
| 262 |
+
if parsed_reset_at is not None:
|
| 263 |
+
normalized["reset_at"] = parsed_reset_at
|
| 264 |
+
return normalized
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def _exhausted_until(entry: PooledCredential) -> Optional[float]:
|
| 268 |
+
if entry.last_status != STATUS_EXHAUSTED:
|
| 269 |
+
return None
|
| 270 |
+
reset_at = _parse_absolute_timestamp(getattr(entry, "last_error_reset_at", None))
|
| 271 |
+
if reset_at is not None:
|
| 272 |
+
return reset_at
|
| 273 |
+
if entry.last_status_at:
|
| 274 |
+
return entry.last_status_at + _exhausted_ttl(entry.last_error_code)
|
| 275 |
+
return None
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
def _normalize_custom_pool_name(name: str) -> str:
|
| 279 |
+
"""Normalize a custom provider name for use as a pool key suffix."""
|
| 280 |
+
return name.strip().lower().replace(" ", "-")
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
def _iter_custom_providers(config: Optional[dict] = None):
|
| 284 |
+
"""Yield (normalized_name, entry_dict) for each valid custom_providers entry."""
|
| 285 |
+
if config is None:
|
| 286 |
+
config = _load_config_safe()
|
| 287 |
+
if config is None:
|
| 288 |
+
return
|
| 289 |
+
custom_providers = config.get("custom_providers")
|
| 290 |
+
if not isinstance(custom_providers, list):
|
| 291 |
+
return
|
| 292 |
+
for entry in custom_providers:
|
| 293 |
+
if not isinstance(entry, dict):
|
| 294 |
+
continue
|
| 295 |
+
name = entry.get("name")
|
| 296 |
+
if not isinstance(name, str):
|
| 297 |
+
continue
|
| 298 |
+
yield _normalize_custom_pool_name(name), entry
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def get_custom_provider_pool_key(base_url: str) -> Optional[str]:
|
| 302 |
+
"""Look up the custom_providers list in config.yaml and return 'custom:<name>' for a matching base_url.
|
| 303 |
+
|
| 304 |
+
Returns None if no match is found.
|
| 305 |
+
"""
|
| 306 |
+
if not base_url:
|
| 307 |
+
return None
|
| 308 |
+
normalized_url = base_url.strip().rstrip("/")
|
| 309 |
+
for norm_name, entry in _iter_custom_providers():
|
| 310 |
+
entry_url = str(entry.get("base_url") or "").strip().rstrip("/")
|
| 311 |
+
if entry_url and entry_url == normalized_url:
|
| 312 |
+
return f"{CUSTOM_POOL_PREFIX}{norm_name}"
|
| 313 |
+
return None
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
def list_custom_pool_providers() -> List[str]:
|
| 317 |
+
"""Return all 'custom:*' pool keys that have entries in auth.json."""
|
| 318 |
+
pool_data = read_credential_pool(None)
|
| 319 |
+
return sorted(
|
| 320 |
+
key for key in pool_data
|
| 321 |
+
if key.startswith(CUSTOM_POOL_PREFIX)
|
| 322 |
+
and isinstance(pool_data.get(key), list)
|
| 323 |
+
and pool_data[key]
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def _get_custom_provider_config(pool_key: str) -> Optional[Dict[str, Any]]:
|
| 328 |
+
"""Return the custom_providers config entry matching a pool key like 'custom:together.ai'."""
|
| 329 |
+
if not pool_key.startswith(CUSTOM_POOL_PREFIX):
|
| 330 |
+
return None
|
| 331 |
+
suffix = pool_key[len(CUSTOM_POOL_PREFIX):]
|
| 332 |
+
for norm_name, entry in _iter_custom_providers():
|
| 333 |
+
if norm_name == suffix:
|
| 334 |
+
return entry
|
| 335 |
+
return None
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
def get_pool_strategy(provider: str) -> str:
|
| 339 |
+
"""Return the configured selection strategy for a provider."""
|
| 340 |
+
config = _load_config_safe()
|
| 341 |
+
if config is None:
|
| 342 |
+
return STRATEGY_FILL_FIRST
|
| 343 |
+
|
| 344 |
+
strategies = config.get("credential_pool_strategies")
|
| 345 |
+
if not isinstance(strategies, dict):
|
| 346 |
+
return STRATEGY_FILL_FIRST
|
| 347 |
+
|
| 348 |
+
strategy = str(strategies.get(provider, "") or "").strip().lower()
|
| 349 |
+
if strategy in SUPPORTED_POOL_STRATEGIES:
|
| 350 |
+
return strategy
|
| 351 |
+
return STRATEGY_FILL_FIRST
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
DEFAULT_MAX_CONCURRENT_PER_CREDENTIAL = 1
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
class CredentialPool:
|
| 358 |
+
def __init__(self, provider: str, entries: List[PooledCredential]):
|
| 359 |
+
self.provider = provider
|
| 360 |
+
self._entries = sorted(entries, key=lambda entry: entry.priority)
|
| 361 |
+
self._current_id: Optional[str] = None
|
| 362 |
+
self._strategy = get_pool_strategy(provider)
|
| 363 |
+
self._lock = threading.Lock()
|
| 364 |
+
self._active_leases: Dict[str, int] = {}
|
| 365 |
+
self._max_concurrent = DEFAULT_MAX_CONCURRENT_PER_CREDENTIAL
|
| 366 |
+
|
| 367 |
+
def has_credentials(self) -> bool:
|
| 368 |
+
return bool(self._entries)
|
| 369 |
+
|
| 370 |
+
def has_available(self) -> bool:
|
| 371 |
+
"""True if at least one entry is not currently in exhaustion cooldown."""
|
| 372 |
+
return bool(self._available_entries())
|
| 373 |
+
|
| 374 |
+
def entries(self) -> List[PooledCredential]:
|
| 375 |
+
return list(self._entries)
|
| 376 |
+
|
| 377 |
+
def current(self) -> Optional[PooledCredential]:
|
| 378 |
+
if not self._current_id:
|
| 379 |
+
return None
|
| 380 |
+
return next((entry for entry in self._entries if entry.id == self._current_id), None)
|
| 381 |
+
|
| 382 |
+
def _replace_entry(self, old: PooledCredential, new: PooledCredential) -> None:
|
| 383 |
+
"""Swap an entry in-place by id, preserving sort order."""
|
| 384 |
+
for idx, entry in enumerate(self._entries):
|
| 385 |
+
if entry.id == old.id:
|
| 386 |
+
self._entries[idx] = new
|
| 387 |
+
return
|
| 388 |
+
|
| 389 |
+
def _persist(self) -> None:
|
| 390 |
+
write_credential_pool(
|
| 391 |
+
self.provider,
|
| 392 |
+
[entry.to_dict() for entry in self._entries],
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
def _mark_exhausted(
|
| 396 |
+
self,
|
| 397 |
+
entry: PooledCredential,
|
| 398 |
+
status_code: Optional[int],
|
| 399 |
+
error_context: Optional[Dict[str, Any]] = None,
|
| 400 |
+
) -> PooledCredential:
|
| 401 |
+
normalized_error = _normalize_error_context(error_context)
|
| 402 |
+
updated = replace(
|
| 403 |
+
entry,
|
| 404 |
+
last_status=STATUS_EXHAUSTED,
|
| 405 |
+
last_status_at=time.time(),
|
| 406 |
+
last_error_code=status_code,
|
| 407 |
+
last_error_reason=normalized_error.get("reason"),
|
| 408 |
+
last_error_message=normalized_error.get("message"),
|
| 409 |
+
last_error_reset_at=normalized_error.get("reset_at"),
|
| 410 |
+
)
|
| 411 |
+
self._replace_entry(entry, updated)
|
| 412 |
+
self._persist()
|
| 413 |
+
return updated
|
| 414 |
+
|
| 415 |
+
def _sync_anthropic_entry_from_credentials_file(self, entry: PooledCredential) -> PooledCredential:
|
| 416 |
+
"""Sync a claude_code pool entry from ~/.claude/.credentials.json if tokens differ.
|
| 417 |
+
|
| 418 |
+
OAuth refresh tokens are single-use. When something external (e.g.
|
| 419 |
+
Claude Code CLI, or another profile's pool) refreshes the token, it
|
| 420 |
+
writes the new pair to ~/.claude/.credentials.json. The pool entry's
|
| 421 |
+
refresh token becomes stale. This method detects that and syncs.
|
| 422 |
+
"""
|
| 423 |
+
if self.provider != "anthropic" or entry.source != "claude_code":
|
| 424 |
+
return entry
|
| 425 |
+
try:
|
| 426 |
+
from agent.anthropic_adapter import read_claude_code_credentials
|
| 427 |
+
creds = read_claude_code_credentials()
|
| 428 |
+
if not creds:
|
| 429 |
+
return entry
|
| 430 |
+
file_refresh = creds.get("refreshToken", "")
|
| 431 |
+
file_access = creds.get("accessToken", "")
|
| 432 |
+
file_expires = creds.get("expiresAt", 0)
|
| 433 |
+
# If the credentials file has a different token pair, sync it
|
| 434 |
+
if file_refresh and file_refresh != entry.refresh_token:
|
| 435 |
+
logger.debug("Pool entry %s: syncing tokens from credentials file (refresh token changed)", entry.id)
|
| 436 |
+
updated = replace(
|
| 437 |
+
entry,
|
| 438 |
+
access_token=file_access,
|
| 439 |
+
refresh_token=file_refresh,
|
| 440 |
+
expires_at_ms=file_expires,
|
| 441 |
+
last_status=None,
|
| 442 |
+
last_status_at=None,
|
| 443 |
+
last_error_code=None,
|
| 444 |
+
)
|
| 445 |
+
self._replace_entry(entry, updated)
|
| 446 |
+
self._persist()
|
| 447 |
+
return updated
|
| 448 |
+
except Exception as exc:
|
| 449 |
+
logger.debug("Failed to sync from credentials file: %s", exc)
|
| 450 |
+
return entry
|
| 451 |
+
|
| 452 |
+
def _sync_codex_entry_from_cli(self, entry: PooledCredential) -> PooledCredential:
|
| 453 |
+
"""Sync an openai-codex pool entry from ~/.codex/auth.json if tokens differ.
|
| 454 |
+
|
| 455 |
+
OpenAI OAuth refresh tokens are single-use and rotate on every refresh.
|
| 456 |
+
When the Codex CLI (or another Hermes profile) refreshes its token,
|
| 457 |
+
the pool entry's refresh_token becomes stale. This method detects that
|
| 458 |
+
by comparing against ~/.codex/auth.json and syncing the fresh pair.
|
| 459 |
+
"""
|
| 460 |
+
if self.provider != "openai-codex":
|
| 461 |
+
return entry
|
| 462 |
+
try:
|
| 463 |
+
cli_tokens = _import_codex_cli_tokens()
|
| 464 |
+
if not cli_tokens:
|
| 465 |
+
return entry
|
| 466 |
+
cli_refresh = cli_tokens.get("refresh_token", "")
|
| 467 |
+
cli_access = cli_tokens.get("access_token", "")
|
| 468 |
+
if cli_refresh and cli_refresh != entry.refresh_token:
|
| 469 |
+
logger.debug("Pool entry %s: syncing tokens from ~/.codex/auth.json (refresh token changed)", entry.id)
|
| 470 |
+
updated = replace(
|
| 471 |
+
entry,
|
| 472 |
+
access_token=cli_access,
|
| 473 |
+
refresh_token=cli_refresh,
|
| 474 |
+
last_status=None,
|
| 475 |
+
last_status_at=None,
|
| 476 |
+
last_error_code=None,
|
| 477 |
+
)
|
| 478 |
+
self._replace_entry(entry, updated)
|
| 479 |
+
self._persist()
|
| 480 |
+
return updated
|
| 481 |
+
except Exception as exc:
|
| 482 |
+
logger.debug("Failed to sync from ~/.codex/auth.json: %s", exc)
|
| 483 |
+
return entry
|
| 484 |
+
|
| 485 |
+
def _sync_device_code_entry_to_auth_store(self, entry: PooledCredential) -> None:
|
| 486 |
+
"""Write refreshed pool entry tokens back to auth.json providers.
|
| 487 |
+
|
| 488 |
+
After a pool-level refresh, the pool entry has fresh tokens but
|
| 489 |
+
auth.json's ``providers.<id>`` still holds the pre-refresh state.
|
| 490 |
+
On the next ``load_pool()``, ``_seed_from_singletons()`` reads that
|
| 491 |
+
stale state and can overwrite the fresh pool entry — potentially
|
| 492 |
+
re-seeding a consumed single-use refresh token.
|
| 493 |
+
|
| 494 |
+
Applies to any OAuth provider whose singleton lives in auth.json
|
| 495 |
+
(currently Nous and OpenAI Codex).
|
| 496 |
+
"""
|
| 497 |
+
if entry.source != "device_code":
|
| 498 |
+
return
|
| 499 |
+
try:
|
| 500 |
+
with _auth_store_lock():
|
| 501 |
+
auth_store = _load_auth_store()
|
| 502 |
+
if self.provider == "nous":
|
| 503 |
+
state = _load_provider_state(auth_store, "nous")
|
| 504 |
+
if state is None:
|
| 505 |
+
return
|
| 506 |
+
state["access_token"] = entry.access_token
|
| 507 |
+
if entry.refresh_token:
|
| 508 |
+
state["refresh_token"] = entry.refresh_token
|
| 509 |
+
if entry.expires_at:
|
| 510 |
+
state["expires_at"] = entry.expires_at
|
| 511 |
+
if entry.agent_key:
|
| 512 |
+
state["agent_key"] = entry.agent_key
|
| 513 |
+
if entry.agent_key_expires_at:
|
| 514 |
+
state["agent_key_expires_at"] = entry.agent_key_expires_at
|
| 515 |
+
for extra_key in ("obtained_at", "expires_in", "agent_key_id",
|
| 516 |
+
"agent_key_expires_in", "agent_key_reused",
|
| 517 |
+
"agent_key_obtained_at"):
|
| 518 |
+
val = entry.extra.get(extra_key)
|
| 519 |
+
if val is not None:
|
| 520 |
+
state[extra_key] = val
|
| 521 |
+
if entry.inference_base_url:
|
| 522 |
+
state["inference_base_url"] = entry.inference_base_url
|
| 523 |
+
_save_provider_state(auth_store, "nous", state)
|
| 524 |
+
|
| 525 |
+
elif self.provider == "openai-codex":
|
| 526 |
+
state = _load_provider_state(auth_store, "openai-codex")
|
| 527 |
+
if not isinstance(state, dict):
|
| 528 |
+
return
|
| 529 |
+
tokens = state.get("tokens")
|
| 530 |
+
if not isinstance(tokens, dict):
|
| 531 |
+
return
|
| 532 |
+
tokens["access_token"] = entry.access_token
|
| 533 |
+
if entry.refresh_token:
|
| 534 |
+
tokens["refresh_token"] = entry.refresh_token
|
| 535 |
+
if entry.last_refresh:
|
| 536 |
+
state["last_refresh"] = entry.last_refresh
|
| 537 |
+
_save_provider_state(auth_store, "openai-codex", state)
|
| 538 |
+
|
| 539 |
+
else:
|
| 540 |
+
return
|
| 541 |
+
|
| 542 |
+
_save_auth_store(auth_store)
|
| 543 |
+
except Exception as exc:
|
| 544 |
+
logger.debug("Failed to sync %s pool entry back to auth store: %s", self.provider, exc)
|
| 545 |
+
|
| 546 |
+
def _refresh_entry(self, entry: PooledCredential, *, force: bool) -> Optional[PooledCredential]:
|
| 547 |
+
if entry.auth_type != AUTH_TYPE_OAUTH or not entry.refresh_token:
|
| 548 |
+
if force:
|
| 549 |
+
self._mark_exhausted(entry, None)
|
| 550 |
+
return None
|
| 551 |
+
|
| 552 |
+
try:
|
| 553 |
+
if self.provider == "anthropic":
|
| 554 |
+
from agent.anthropic_adapter import refresh_anthropic_oauth_pure
|
| 555 |
+
|
| 556 |
+
refreshed = refresh_anthropic_oauth_pure(
|
| 557 |
+
entry.refresh_token,
|
| 558 |
+
use_json=entry.source.endswith("hermes_pkce"),
|
| 559 |
+
)
|
| 560 |
+
updated = replace(
|
| 561 |
+
entry,
|
| 562 |
+
access_token=refreshed["access_token"],
|
| 563 |
+
refresh_token=refreshed["refresh_token"],
|
| 564 |
+
expires_at_ms=refreshed["expires_at_ms"],
|
| 565 |
+
)
|
| 566 |
+
# Keep ~/.claude/.credentials.json in sync so that the
|
| 567 |
+
# fallback path (resolve_anthropic_token) and other profiles
|
| 568 |
+
# see the latest tokens.
|
| 569 |
+
if entry.source == "claude_code":
|
| 570 |
+
try:
|
| 571 |
+
from agent.anthropic_adapter import _write_claude_code_credentials
|
| 572 |
+
_write_claude_code_credentials(
|
| 573 |
+
refreshed["access_token"],
|
| 574 |
+
refreshed["refresh_token"],
|
| 575 |
+
refreshed["expires_at_ms"],
|
| 576 |
+
)
|
| 577 |
+
except Exception as wexc:
|
| 578 |
+
logger.debug("Failed to write refreshed token to credentials file: %s", wexc)
|
| 579 |
+
elif self.provider == "openai-codex":
|
| 580 |
+
# Proactively sync from ~/.codex/auth.json before refresh.
|
| 581 |
+
# The Codex CLI (or another Hermes profile) may have already
|
| 582 |
+
# consumed our refresh_token. Syncing first avoids a
|
| 583 |
+
# "refresh_token_reused" error when the CLI has a newer pair.
|
| 584 |
+
synced = self._sync_codex_entry_from_cli(entry)
|
| 585 |
+
if synced is not entry:
|
| 586 |
+
entry = synced
|
| 587 |
+
refreshed = auth_mod.refresh_codex_oauth_pure(
|
| 588 |
+
entry.access_token,
|
| 589 |
+
entry.refresh_token,
|
| 590 |
+
)
|
| 591 |
+
updated = replace(
|
| 592 |
+
entry,
|
| 593 |
+
access_token=refreshed["access_token"],
|
| 594 |
+
refresh_token=refreshed["refresh_token"],
|
| 595 |
+
last_refresh=refreshed.get("last_refresh"),
|
| 596 |
+
)
|
| 597 |
+
elif self.provider == "nous":
|
| 598 |
+
nous_state = {
|
| 599 |
+
"access_token": entry.access_token,
|
| 600 |
+
"refresh_token": entry.refresh_token,
|
| 601 |
+
"client_id": entry.client_id,
|
| 602 |
+
"portal_base_url": entry.portal_base_url,
|
| 603 |
+
"inference_base_url": entry.inference_base_url,
|
| 604 |
+
"token_type": entry.token_type,
|
| 605 |
+
"scope": entry.scope,
|
| 606 |
+
"obtained_at": entry.obtained_at,
|
| 607 |
+
"expires_at": entry.expires_at,
|
| 608 |
+
"agent_key": entry.agent_key,
|
| 609 |
+
"agent_key_expires_at": entry.agent_key_expires_at,
|
| 610 |
+
"tls": entry.tls,
|
| 611 |
+
}
|
| 612 |
+
refreshed = auth_mod.refresh_nous_oauth_from_state(
|
| 613 |
+
nous_state,
|
| 614 |
+
min_key_ttl_seconds=DEFAULT_AGENT_KEY_MIN_TTL_SECONDS,
|
| 615 |
+
force_refresh=force,
|
| 616 |
+
force_mint=force,
|
| 617 |
+
)
|
| 618 |
+
# Apply returned fields: dataclass fields via replace, extras via dict update
|
| 619 |
+
field_updates = {}
|
| 620 |
+
extra_updates = dict(entry.extra)
|
| 621 |
+
_field_names = {f.name for f in fields(entry)}
|
| 622 |
+
for k, v in refreshed.items():
|
| 623 |
+
if k in _field_names:
|
| 624 |
+
field_updates[k] = v
|
| 625 |
+
elif k in _EXTRA_KEYS:
|
| 626 |
+
extra_updates[k] = v
|
| 627 |
+
updated = replace(entry, extra=extra_updates, **field_updates)
|
| 628 |
+
else:
|
| 629 |
+
return entry
|
| 630 |
+
except Exception as exc:
|
| 631 |
+
logger.debug("Credential refresh failed for %s/%s: %s", self.provider, entry.id, exc)
|
| 632 |
+
# For anthropic claude_code entries: the refresh token may have been
|
| 633 |
+
# consumed by another process. Check if ~/.claude/.credentials.json
|
| 634 |
+
# has a newer token pair and retry once.
|
| 635 |
+
if self.provider == "anthropic" and entry.source == "claude_code":
|
| 636 |
+
synced = self._sync_anthropic_entry_from_credentials_file(entry)
|
| 637 |
+
if synced.refresh_token != entry.refresh_token:
|
| 638 |
+
logger.debug("Retrying refresh with synced token from credentials file")
|
| 639 |
+
try:
|
| 640 |
+
from agent.anthropic_adapter import refresh_anthropic_oauth_pure
|
| 641 |
+
refreshed = refresh_anthropic_oauth_pure(
|
| 642 |
+
synced.refresh_token,
|
| 643 |
+
use_json=synced.source.endswith("hermes_pkce"),
|
| 644 |
+
)
|
| 645 |
+
updated = replace(
|
| 646 |
+
synced,
|
| 647 |
+
access_token=refreshed["access_token"],
|
| 648 |
+
refresh_token=refreshed["refresh_token"],
|
| 649 |
+
expires_at_ms=refreshed["expires_at_ms"],
|
| 650 |
+
last_status=STATUS_OK,
|
| 651 |
+
last_status_at=None,
|
| 652 |
+
last_error_code=None,
|
| 653 |
+
)
|
| 654 |
+
self._replace_entry(synced, updated)
|
| 655 |
+
self._persist()
|
| 656 |
+
try:
|
| 657 |
+
from agent.anthropic_adapter import _write_claude_code_credentials
|
| 658 |
+
_write_claude_code_credentials(
|
| 659 |
+
refreshed["access_token"],
|
| 660 |
+
refreshed["refresh_token"],
|
| 661 |
+
refreshed["expires_at_ms"],
|
| 662 |
+
)
|
| 663 |
+
except Exception as wexc:
|
| 664 |
+
logger.debug("Failed to write refreshed token to credentials file (retry path): %s", wexc)
|
| 665 |
+
return updated
|
| 666 |
+
except Exception as retry_exc:
|
| 667 |
+
logger.debug("Retry refresh also failed: %s", retry_exc)
|
| 668 |
+
elif not self._entry_needs_refresh(synced):
|
| 669 |
+
# Credentials file had a valid (non-expired) token — use it directly
|
| 670 |
+
logger.debug("Credentials file has valid token, using without refresh")
|
| 671 |
+
return synced
|
| 672 |
+
# For openai-codex: the refresh_token may have been consumed by
|
| 673 |
+
# the Codex CLI between our proactive sync and the refresh call.
|
| 674 |
+
# Re-sync and retry once.
|
| 675 |
+
if self.provider == "openai-codex":
|
| 676 |
+
synced = self._sync_codex_entry_from_cli(entry)
|
| 677 |
+
if synced.refresh_token != entry.refresh_token:
|
| 678 |
+
logger.debug("Retrying Codex refresh with synced token from ~/.codex/auth.json")
|
| 679 |
+
try:
|
| 680 |
+
refreshed = auth_mod.refresh_codex_oauth_pure(
|
| 681 |
+
synced.access_token,
|
| 682 |
+
synced.refresh_token,
|
| 683 |
+
)
|
| 684 |
+
updated = replace(
|
| 685 |
+
synced,
|
| 686 |
+
access_token=refreshed["access_token"],
|
| 687 |
+
refresh_token=refreshed["refresh_token"],
|
| 688 |
+
last_refresh=refreshed.get("last_refresh"),
|
| 689 |
+
last_status=STATUS_OK,
|
| 690 |
+
last_status_at=None,
|
| 691 |
+
last_error_code=None,
|
| 692 |
+
)
|
| 693 |
+
self._replace_entry(synced, updated)
|
| 694 |
+
self._persist()
|
| 695 |
+
self._sync_device_code_entry_to_auth_store(updated)
|
| 696 |
+
return updated
|
| 697 |
+
except Exception as retry_exc:
|
| 698 |
+
logger.debug("Codex retry refresh also failed: %s", retry_exc)
|
| 699 |
+
elif not self._entry_needs_refresh(synced):
|
| 700 |
+
logger.debug("Codex CLI has valid token, using without refresh")
|
| 701 |
+
self._sync_device_code_entry_to_auth_store(synced)
|
| 702 |
+
return synced
|
| 703 |
+
self._mark_exhausted(entry, None)
|
| 704 |
+
return None
|
| 705 |
+
|
| 706 |
+
updated = replace(
|
| 707 |
+
updated,
|
| 708 |
+
last_status=STATUS_OK,
|
| 709 |
+
last_status_at=None,
|
| 710 |
+
last_error_code=None,
|
| 711 |
+
last_error_reason=None,
|
| 712 |
+
last_error_message=None,
|
| 713 |
+
last_error_reset_at=None,
|
| 714 |
+
)
|
| 715 |
+
self._replace_entry(entry, updated)
|
| 716 |
+
self._persist()
|
| 717 |
+
# Sync refreshed tokens back to auth.json providers so that
|
| 718 |
+
# _seed_from_singletons() on the next load_pool() sees fresh state
|
| 719 |
+
# instead of re-seeding stale/consumed tokens.
|
| 720 |
+
self._sync_device_code_entry_to_auth_store(updated)
|
| 721 |
+
return updated
|
| 722 |
+
|
| 723 |
+
def _entry_needs_refresh(self, entry: PooledCredential) -> bool:
|
| 724 |
+
if entry.auth_type != AUTH_TYPE_OAUTH:
|
| 725 |
+
return False
|
| 726 |
+
if self.provider == "anthropic":
|
| 727 |
+
if entry.expires_at_ms is None:
|
| 728 |
+
return False
|
| 729 |
+
return int(entry.expires_at_ms) <= int(time.time() * 1000) + 120_000
|
| 730 |
+
if self.provider == "openai-codex":
|
| 731 |
+
return _codex_access_token_is_expiring(
|
| 732 |
+
entry.access_token,
|
| 733 |
+
CODEX_ACCESS_TOKEN_REFRESH_SKEW_SECONDS,
|
| 734 |
+
)
|
| 735 |
+
if self.provider == "nous":
|
| 736 |
+
# Nous refresh/mint can require network access and should happen when
|
| 737 |
+
# runtime credentials are actually resolved, not merely when the pool
|
| 738 |
+
# is enumerated for listing, migration, or selection.
|
| 739 |
+
return False
|
| 740 |
+
return False
|
| 741 |
+
|
| 742 |
+
def select(self) -> Optional[PooledCredential]:
|
| 743 |
+
with self._lock:
|
| 744 |
+
return self._select_unlocked()
|
| 745 |
+
|
| 746 |
+
def _available_entries(self, *, clear_expired: bool = False, refresh: bool = False) -> List[PooledCredential]:
|
| 747 |
+
"""Return entries not currently in exhaustion cooldown.
|
| 748 |
+
|
| 749 |
+
When *clear_expired* is True, entries whose cooldown has elapsed are
|
| 750 |
+
reset to STATUS_OK and persisted. When *refresh* is True, entries
|
| 751 |
+
that need a token refresh are refreshed (skipped on failure).
|
| 752 |
+
"""
|
| 753 |
+
now = time.time()
|
| 754 |
+
cleared_any = False
|
| 755 |
+
available: List[PooledCredential] = []
|
| 756 |
+
for entry in self._entries:
|
| 757 |
+
# For anthropic claude_code entries, sync from the credentials file
|
| 758 |
+
# before any status/refresh checks. This picks up tokens refreshed
|
| 759 |
+
# by other processes (Claude Code CLI, other Hermes profiles).
|
| 760 |
+
if (self.provider == "anthropic" and entry.source == "claude_code"
|
| 761 |
+
and entry.last_status == STATUS_EXHAUSTED):
|
| 762 |
+
synced = self._sync_anthropic_entry_from_credentials_file(entry)
|
| 763 |
+
if synced is not entry:
|
| 764 |
+
entry = synced
|
| 765 |
+
cleared_any = True
|
| 766 |
+
# For openai-codex entries, sync from ~/.codex/auth.json before
|
| 767 |
+
# any status/refresh checks. This picks up tokens refreshed by
|
| 768 |
+
# the Codex CLI or another Hermes profile.
|
| 769 |
+
if (self.provider == "openai-codex"
|
| 770 |
+
and entry.last_status == STATUS_EXHAUSTED
|
| 771 |
+
and entry.refresh_token):
|
| 772 |
+
synced = self._sync_codex_entry_from_cli(entry)
|
| 773 |
+
if synced is not entry:
|
| 774 |
+
entry = synced
|
| 775 |
+
cleared_any = True
|
| 776 |
+
if entry.last_status == STATUS_EXHAUSTED:
|
| 777 |
+
exhausted_until = _exhausted_until(entry)
|
| 778 |
+
if exhausted_until is not None and now < exhausted_until:
|
| 779 |
+
continue
|
| 780 |
+
if clear_expired:
|
| 781 |
+
cleared = replace(
|
| 782 |
+
entry,
|
| 783 |
+
last_status=STATUS_OK,
|
| 784 |
+
last_status_at=None,
|
| 785 |
+
last_error_code=None,
|
| 786 |
+
last_error_reason=None,
|
| 787 |
+
last_error_message=None,
|
| 788 |
+
last_error_reset_at=None,
|
| 789 |
+
)
|
| 790 |
+
self._replace_entry(entry, cleared)
|
| 791 |
+
entry = cleared
|
| 792 |
+
cleared_any = True
|
| 793 |
+
if refresh and self._entry_needs_refresh(entry):
|
| 794 |
+
refreshed = self._refresh_entry(entry, force=False)
|
| 795 |
+
if refreshed is None:
|
| 796 |
+
continue
|
| 797 |
+
entry = refreshed
|
| 798 |
+
available.append(entry)
|
| 799 |
+
if cleared_any:
|
| 800 |
+
self._persist()
|
| 801 |
+
return available
|
| 802 |
+
|
| 803 |
+
def _select_unlocked(self) -> Optional[PooledCredential]:
|
| 804 |
+
available = self._available_entries(clear_expired=True, refresh=True)
|
| 805 |
+
if not available:
|
| 806 |
+
self._current_id = None
|
| 807 |
+
logger.info("credential pool: no available entries (all exhausted or empty)")
|
| 808 |
+
return None
|
| 809 |
+
|
| 810 |
+
if self._strategy == STRATEGY_RANDOM:
|
| 811 |
+
entry = random.choice(available)
|
| 812 |
+
self._current_id = entry.id
|
| 813 |
+
return entry
|
| 814 |
+
|
| 815 |
+
if self._strategy == STRATEGY_LEAST_USED and len(available) > 1:
|
| 816 |
+
entry = min(available, key=lambda e: e.request_count)
|
| 817 |
+
self._current_id = entry.id
|
| 818 |
+
return entry
|
| 819 |
+
|
| 820 |
+
if self._strategy == STRATEGY_ROUND_ROBIN and len(available) > 1:
|
| 821 |
+
entry = available[0]
|
| 822 |
+
rotated = [candidate for candidate in self._entries if candidate.id != entry.id]
|
| 823 |
+
rotated.append(replace(entry, priority=len(self._entries) - 1))
|
| 824 |
+
self._entries = [replace(candidate, priority=idx) for idx, candidate in enumerate(rotated)]
|
| 825 |
+
self._persist()
|
| 826 |
+
self._current_id = entry.id
|
| 827 |
+
return self.current() or entry
|
| 828 |
+
|
| 829 |
+
entry = available[0]
|
| 830 |
+
self._current_id = entry.id
|
| 831 |
+
return entry
|
| 832 |
+
|
| 833 |
+
def peek(self) -> Optional[PooledCredential]:
|
| 834 |
+
current = self.current()
|
| 835 |
+
if current is not None:
|
| 836 |
+
return current
|
| 837 |
+
available = self._available_entries()
|
| 838 |
+
return available[0] if available else None
|
| 839 |
+
|
| 840 |
+
def mark_exhausted_and_rotate(
|
| 841 |
+
self,
|
| 842 |
+
*,
|
| 843 |
+
status_code: Optional[int],
|
| 844 |
+
error_context: Optional[Dict[str, Any]] = None,
|
| 845 |
+
) -> Optional[PooledCredential]:
|
| 846 |
+
with self._lock:
|
| 847 |
+
entry = self.current() or self._select_unlocked()
|
| 848 |
+
if entry is None:
|
| 849 |
+
return None
|
| 850 |
+
_label = entry.label or entry.id[:8]
|
| 851 |
+
logger.info(
|
| 852 |
+
"credential pool: marking %s exhausted (status=%s), rotating",
|
| 853 |
+
_label, status_code,
|
| 854 |
+
)
|
| 855 |
+
self._mark_exhausted(entry, status_code, error_context)
|
| 856 |
+
self._current_id = None
|
| 857 |
+
next_entry = self._select_unlocked()
|
| 858 |
+
if next_entry:
|
| 859 |
+
_next_label = next_entry.label or next_entry.id[:8]
|
| 860 |
+
logger.info("credential pool: rotated to %s", _next_label)
|
| 861 |
+
return next_entry
|
| 862 |
+
|
| 863 |
+
def acquire_lease(self, credential_id: Optional[str] = None) -> Optional[str]:
|
| 864 |
+
"""Acquire a soft lease on a credential.
|
| 865 |
+
|
| 866 |
+
If a specific credential_id is provided, lease that entry directly.
|
| 867 |
+
Otherwise prefer the least-leased available credential, using priority as
|
| 868 |
+
a stable tie-breaker. When every credential is already at the soft cap,
|
| 869 |
+
still return the least-leased one instead of blocking.
|
| 870 |
+
"""
|
| 871 |
+
with self._lock:
|
| 872 |
+
if credential_id:
|
| 873 |
+
self._active_leases[credential_id] = self._active_leases.get(credential_id, 0) + 1
|
| 874 |
+
self._current_id = credential_id
|
| 875 |
+
return credential_id
|
| 876 |
+
|
| 877 |
+
available = self._available_entries(clear_expired=True, refresh=True)
|
| 878 |
+
if not available:
|
| 879 |
+
return None
|
| 880 |
+
|
| 881 |
+
below_cap = [
|
| 882 |
+
entry for entry in available
|
| 883 |
+
if self._active_leases.get(entry.id, 0) < self._max_concurrent
|
| 884 |
+
]
|
| 885 |
+
candidates = below_cap if below_cap else available
|
| 886 |
+
chosen = min(
|
| 887 |
+
candidates,
|
| 888 |
+
key=lambda entry: (self._active_leases.get(entry.id, 0), entry.priority),
|
| 889 |
+
)
|
| 890 |
+
self._active_leases[chosen.id] = self._active_leases.get(chosen.id, 0) + 1
|
| 891 |
+
self._current_id = chosen.id
|
| 892 |
+
return chosen.id
|
| 893 |
+
|
| 894 |
+
def release_lease(self, credential_id: str) -> None:
|
| 895 |
+
"""Release a previously acquired credential lease."""
|
| 896 |
+
with self._lock:
|
| 897 |
+
count = self._active_leases.get(credential_id, 0)
|
| 898 |
+
if count <= 1:
|
| 899 |
+
self._active_leases.pop(credential_id, None)
|
| 900 |
+
else:
|
| 901 |
+
self._active_leases[credential_id] = count - 1
|
| 902 |
+
|
| 903 |
+
def try_refresh_current(self) -> Optional[PooledCredential]:
|
| 904 |
+
with self._lock:
|
| 905 |
+
return self._try_refresh_current_unlocked()
|
| 906 |
+
|
| 907 |
+
def _try_refresh_current_unlocked(self) -> Optional[PooledCredential]:
|
| 908 |
+
entry = self.current()
|
| 909 |
+
if entry is None:
|
| 910 |
+
return None
|
| 911 |
+
refreshed = self._refresh_entry(entry, force=True)
|
| 912 |
+
if refreshed is not None:
|
| 913 |
+
self._current_id = refreshed.id
|
| 914 |
+
return refreshed
|
| 915 |
+
|
| 916 |
+
def reset_statuses(self) -> int:
|
| 917 |
+
count = 0
|
| 918 |
+
new_entries = []
|
| 919 |
+
for entry in self._entries:
|
| 920 |
+
if entry.last_status or entry.last_status_at or entry.last_error_code:
|
| 921 |
+
new_entries.append(
|
| 922 |
+
replace(
|
| 923 |
+
entry,
|
| 924 |
+
last_status=None,
|
| 925 |
+
last_status_at=None,
|
| 926 |
+
last_error_code=None,
|
| 927 |
+
last_error_reason=None,
|
| 928 |
+
last_error_message=None,
|
| 929 |
+
last_error_reset_at=None,
|
| 930 |
+
)
|
| 931 |
+
)
|
| 932 |
+
count += 1
|
| 933 |
+
else:
|
| 934 |
+
new_entries.append(entry)
|
| 935 |
+
if count:
|
| 936 |
+
self._entries = new_entries
|
| 937 |
+
self._persist()
|
| 938 |
+
return count
|
| 939 |
+
|
| 940 |
+
def remove_index(self, index: int) -> Optional[PooledCredential]:
|
| 941 |
+
if index < 1 or index > len(self._entries):
|
| 942 |
+
return None
|
| 943 |
+
removed = self._entries.pop(index - 1)
|
| 944 |
+
self._entries = [
|
| 945 |
+
replace(entry, priority=new_priority)
|
| 946 |
+
for new_priority, entry in enumerate(self._entries)
|
| 947 |
+
]
|
| 948 |
+
self._persist()
|
| 949 |
+
if self._current_id == removed.id:
|
| 950 |
+
self._current_id = None
|
| 951 |
+
return removed
|
| 952 |
+
|
| 953 |
+
def resolve_target(self, target: Any) -> Tuple[Optional[int], Optional[PooledCredential], Optional[str]]:
|
| 954 |
+
raw = str(target or "").strip()
|
| 955 |
+
if not raw:
|
| 956 |
+
return None, None, "No credential target provided."
|
| 957 |
+
|
| 958 |
+
for idx, entry in enumerate(self._entries, start=1):
|
| 959 |
+
if entry.id == raw:
|
| 960 |
+
return idx, entry, None
|
| 961 |
+
|
| 962 |
+
label_matches = [
|
| 963 |
+
(idx, entry)
|
| 964 |
+
for idx, entry in enumerate(self._entries, start=1)
|
| 965 |
+
if entry.label.strip().lower() == raw.lower()
|
| 966 |
+
]
|
| 967 |
+
if len(label_matches) == 1:
|
| 968 |
+
return label_matches[0][0], label_matches[0][1], None
|
| 969 |
+
if len(label_matches) > 1:
|
| 970 |
+
return None, None, f'Ambiguous credential label "{raw}". Use the numeric index or entry id instead.'
|
| 971 |
+
if raw.isdigit():
|
| 972 |
+
index = int(raw)
|
| 973 |
+
if 1 <= index <= len(self._entries):
|
| 974 |
+
return index, self._entries[index - 1], None
|
| 975 |
+
return None, None, f"No credential #{index}."
|
| 976 |
+
return None, None, f'No credential matching "{raw}".'
|
| 977 |
+
|
| 978 |
+
def add_entry(self, entry: PooledCredential) -> PooledCredential:
|
| 979 |
+
entry = replace(entry, priority=_next_priority(self._entries))
|
| 980 |
+
self._entries.append(entry)
|
| 981 |
+
self._persist()
|
| 982 |
+
return entry
|
| 983 |
+
|
| 984 |
+
|
| 985 |
+
def _upsert_entry(entries: List[PooledCredential], provider: str, source: str, payload: Dict[str, Any]) -> bool:
|
| 986 |
+
existing_idx = None
|
| 987 |
+
for idx, entry in enumerate(entries):
|
| 988 |
+
if entry.source == source:
|
| 989 |
+
existing_idx = idx
|
| 990 |
+
break
|
| 991 |
+
|
| 992 |
+
if existing_idx is None:
|
| 993 |
+
payload.setdefault("id", uuid.uuid4().hex[:6])
|
| 994 |
+
payload.setdefault("priority", _next_priority(entries))
|
| 995 |
+
payload.setdefault("label", payload.get("label") or source)
|
| 996 |
+
entries.append(PooledCredential.from_dict(provider, payload))
|
| 997 |
+
return True
|
| 998 |
+
|
| 999 |
+
existing = entries[existing_idx]
|
| 1000 |
+
field_updates = {}
|
| 1001 |
+
extra_updates = {}
|
| 1002 |
+
_field_names = {f.name for f in fields(existing)}
|
| 1003 |
+
for key, value in payload.items():
|
| 1004 |
+
if key in {"id", "priority"} or value is None:
|
| 1005 |
+
continue
|
| 1006 |
+
if key == "label" and existing.label:
|
| 1007 |
+
continue
|
| 1008 |
+
if key in _field_names:
|
| 1009 |
+
if getattr(existing, key) != value:
|
| 1010 |
+
field_updates[key] = value
|
| 1011 |
+
elif key in _EXTRA_KEYS:
|
| 1012 |
+
if existing.extra.get(key) != value:
|
| 1013 |
+
extra_updates[key] = value
|
| 1014 |
+
if field_updates or extra_updates:
|
| 1015 |
+
if extra_updates:
|
| 1016 |
+
field_updates["extra"] = {**existing.extra, **extra_updates}
|
| 1017 |
+
entries[existing_idx] = replace(existing, **field_updates)
|
| 1018 |
+
return True
|
| 1019 |
+
return False
|
| 1020 |
+
|
| 1021 |
+
|
| 1022 |
+
def _normalize_pool_priorities(provider: str, entries: List[PooledCredential]) -> bool:
|
| 1023 |
+
if provider != "anthropic":
|
| 1024 |
+
return False
|
| 1025 |
+
|
| 1026 |
+
source_rank = {
|
| 1027 |
+
"env:ANTHROPIC_TOKEN": 0,
|
| 1028 |
+
"env:CLAUDE_CODE_OAUTH_TOKEN": 1,
|
| 1029 |
+
"hermes_pkce": 2,
|
| 1030 |
+
"claude_code": 3,
|
| 1031 |
+
"env:ANTHROPIC_API_KEY": 4,
|
| 1032 |
+
}
|
| 1033 |
+
manual_entries = sorted(
|
| 1034 |
+
(entry for entry in entries if _is_manual_source(entry.source)),
|
| 1035 |
+
key=lambda entry: entry.priority,
|
| 1036 |
+
)
|
| 1037 |
+
seeded_entries = sorted(
|
| 1038 |
+
(entry for entry in entries if not _is_manual_source(entry.source)),
|
| 1039 |
+
key=lambda entry: (
|
| 1040 |
+
source_rank.get(entry.source, len(source_rank)),
|
| 1041 |
+
entry.priority,
|
| 1042 |
+
entry.label,
|
| 1043 |
+
),
|
| 1044 |
+
)
|
| 1045 |
+
|
| 1046 |
+
ordered = [*manual_entries, *seeded_entries]
|
| 1047 |
+
id_to_idx = {entry.id: idx for idx, entry in enumerate(entries)}
|
| 1048 |
+
changed = False
|
| 1049 |
+
for new_priority, entry in enumerate(ordered):
|
| 1050 |
+
if entry.priority != new_priority:
|
| 1051 |
+
entries[id_to_idx[entry.id]] = replace(entry, priority=new_priority)
|
| 1052 |
+
changed = True
|
| 1053 |
+
return changed
|
| 1054 |
+
|
| 1055 |
+
|
| 1056 |
+
def _seed_from_singletons(provider: str, entries: List[PooledCredential]) -> Tuple[bool, Set[str]]:
|
| 1057 |
+
changed = False
|
| 1058 |
+
active_sources: Set[str] = set()
|
| 1059 |
+
auth_store = _load_auth_store()
|
| 1060 |
+
|
| 1061 |
+
if provider == "anthropic":
|
| 1062 |
+
# Only auto-discover external credentials (Claude Code, Hermes PKCE)
|
| 1063 |
+
# when the user has explicitly configured anthropic as their provider.
|
| 1064 |
+
# Without this gate, auxiliary client fallback chains silently read
|
| 1065 |
+
# ~/.claude/.credentials.json without user consent. See PR #4210.
|
| 1066 |
+
try:
|
| 1067 |
+
from hermes_cli.auth import is_provider_explicitly_configured
|
| 1068 |
+
if not is_provider_explicitly_configured("anthropic"):
|
| 1069 |
+
return changed, active_sources
|
| 1070 |
+
except ImportError:
|
| 1071 |
+
pass
|
| 1072 |
+
|
| 1073 |
+
from agent.anthropic_adapter import read_claude_code_credentials, read_hermes_oauth_credentials
|
| 1074 |
+
|
| 1075 |
+
for source_name, creds in (
|
| 1076 |
+
("hermes_pkce", read_hermes_oauth_credentials()),
|
| 1077 |
+
("claude_code", read_claude_code_credentials()),
|
| 1078 |
+
):
|
| 1079 |
+
if creds and creds.get("accessToken"):
|
| 1080 |
+
# Check if user explicitly removed this source
|
| 1081 |
+
try:
|
| 1082 |
+
from hermes_cli.auth import is_source_suppressed
|
| 1083 |
+
if is_source_suppressed(provider, source_name):
|
| 1084 |
+
continue
|
| 1085 |
+
except ImportError:
|
| 1086 |
+
pass
|
| 1087 |
+
active_sources.add(source_name)
|
| 1088 |
+
changed |= _upsert_entry(
|
| 1089 |
+
entries,
|
| 1090 |
+
provider,
|
| 1091 |
+
source_name,
|
| 1092 |
+
{
|
| 1093 |
+
"source": source_name,
|
| 1094 |
+
"auth_type": AUTH_TYPE_OAUTH,
|
| 1095 |
+
"access_token": creds.get("accessToken", ""),
|
| 1096 |
+
"refresh_token": creds.get("refreshToken"),
|
| 1097 |
+
"expires_at_ms": creds.get("expiresAt"),
|
| 1098 |
+
"label": label_from_token(creds.get("accessToken", ""), source_name),
|
| 1099 |
+
},
|
| 1100 |
+
)
|
| 1101 |
+
|
| 1102 |
+
elif provider == "nous":
|
| 1103 |
+
state = _load_provider_state(auth_store, "nous")
|
| 1104 |
+
if state:
|
| 1105 |
+
active_sources.add("device_code")
|
| 1106 |
+
changed |= _upsert_entry(
|
| 1107 |
+
entries,
|
| 1108 |
+
provider,
|
| 1109 |
+
"device_code",
|
| 1110 |
+
{
|
| 1111 |
+
"source": "device_code",
|
| 1112 |
+
"auth_type": AUTH_TYPE_OAUTH,
|
| 1113 |
+
"access_token": state.get("access_token", ""),
|
| 1114 |
+
"refresh_token": state.get("refresh_token"),
|
| 1115 |
+
"expires_at": state.get("expires_at"),
|
| 1116 |
+
"token_type": state.get("token_type"),
|
| 1117 |
+
"scope": state.get("scope"),
|
| 1118 |
+
"client_id": state.get("client_id"),
|
| 1119 |
+
"portal_base_url": state.get("portal_base_url"),
|
| 1120 |
+
"inference_base_url": state.get("inference_base_url"),
|
| 1121 |
+
"agent_key": state.get("agent_key"),
|
| 1122 |
+
"agent_key_expires_at": state.get("agent_key_expires_at"),
|
| 1123 |
+
"tls": state.get("tls") if isinstance(state.get("tls"), dict) else None,
|
| 1124 |
+
"label": label_from_token(state.get("access_token", ""), "device_code"),
|
| 1125 |
+
},
|
| 1126 |
+
)
|
| 1127 |
+
|
| 1128 |
+
elif provider == "openai-codex":
|
| 1129 |
+
state = _load_provider_state(auth_store, "openai-codex")
|
| 1130 |
+
tokens = state.get("tokens") if isinstance(state, dict) else None
|
| 1131 |
+
if isinstance(tokens, dict) and tokens.get("access_token"):
|
| 1132 |
+
active_sources.add("device_code")
|
| 1133 |
+
changed |= _upsert_entry(
|
| 1134 |
+
entries,
|
| 1135 |
+
provider,
|
| 1136 |
+
"device_code",
|
| 1137 |
+
{
|
| 1138 |
+
"source": "device_code",
|
| 1139 |
+
"auth_type": AUTH_TYPE_OAUTH,
|
| 1140 |
+
"access_token": tokens.get("access_token", ""),
|
| 1141 |
+
"refresh_token": tokens.get("refresh_token"),
|
| 1142 |
+
"base_url": "https://chatgpt.com/backend-api/codex",
|
| 1143 |
+
"last_refresh": state.get("last_refresh"),
|
| 1144 |
+
"label": label_from_token(tokens.get("access_token", ""), "device_code"),
|
| 1145 |
+
},
|
| 1146 |
+
)
|
| 1147 |
+
|
| 1148 |
+
return changed, active_sources
|
| 1149 |
+
|
| 1150 |
+
|
| 1151 |
+
def _seed_from_env(provider: str, entries: List[PooledCredential]) -> Tuple[bool, Set[str]]:
|
| 1152 |
+
changed = False
|
| 1153 |
+
active_sources: Set[str] = set()
|
| 1154 |
+
if provider == "openrouter":
|
| 1155 |
+
token = os.getenv("OPENROUTER_API_KEY", "").strip()
|
| 1156 |
+
if token:
|
| 1157 |
+
source = "env:OPENROUTER_API_KEY"
|
| 1158 |
+
active_sources.add(source)
|
| 1159 |
+
changed |= _upsert_entry(
|
| 1160 |
+
entries,
|
| 1161 |
+
provider,
|
| 1162 |
+
source,
|
| 1163 |
+
{
|
| 1164 |
+
"source": source,
|
| 1165 |
+
"auth_type": AUTH_TYPE_API_KEY,
|
| 1166 |
+
"access_token": token,
|
| 1167 |
+
"base_url": OPENROUTER_BASE_URL,
|
| 1168 |
+
"label": "OPENROUTER_API_KEY",
|
| 1169 |
+
},
|
| 1170 |
+
)
|
| 1171 |
+
return changed, active_sources
|
| 1172 |
+
|
| 1173 |
+
pconfig = PROVIDER_REGISTRY.get(provider)
|
| 1174 |
+
if not pconfig or pconfig.auth_type != AUTH_TYPE_API_KEY:
|
| 1175 |
+
return changed, active_sources
|
| 1176 |
+
|
| 1177 |
+
env_url = ""
|
| 1178 |
+
if pconfig.base_url_env_var:
|
| 1179 |
+
env_url = os.getenv(pconfig.base_url_env_var, "").strip().rstrip("/")
|
| 1180 |
+
|
| 1181 |
+
env_vars = list(pconfig.api_key_env_vars)
|
| 1182 |
+
if provider == "anthropic":
|
| 1183 |
+
env_vars = [
|
| 1184 |
+
"ANTHROPIC_TOKEN",
|
| 1185 |
+
"CLAUDE_CODE_OAUTH_TOKEN",
|
| 1186 |
+
"ANTHROPIC_API_KEY",
|
| 1187 |
+
]
|
| 1188 |
+
|
| 1189 |
+
for env_var in env_vars:
|
| 1190 |
+
token = os.getenv(env_var, "").strip()
|
| 1191 |
+
if not token:
|
| 1192 |
+
continue
|
| 1193 |
+
source = f"env:{env_var}"
|
| 1194 |
+
active_sources.add(source)
|
| 1195 |
+
auth_type = AUTH_TYPE_OAUTH if provider == "anthropic" and not token.startswith("sk-ant-api") else AUTH_TYPE_API_KEY
|
| 1196 |
+
base_url = env_url or pconfig.inference_base_url
|
| 1197 |
+
if provider == "kimi-coding":
|
| 1198 |
+
base_url = _resolve_kimi_base_url(token, pconfig.inference_base_url, env_url)
|
| 1199 |
+
elif provider == "zai":
|
| 1200 |
+
base_url = _resolve_zai_base_url(token, pconfig.inference_base_url, env_url)
|
| 1201 |
+
changed |= _upsert_entry(
|
| 1202 |
+
entries,
|
| 1203 |
+
provider,
|
| 1204 |
+
source,
|
| 1205 |
+
{
|
| 1206 |
+
"source": source,
|
| 1207 |
+
"auth_type": auth_type,
|
| 1208 |
+
"access_token": token,
|
| 1209 |
+
"base_url": base_url,
|
| 1210 |
+
"label": env_var,
|
| 1211 |
+
},
|
| 1212 |
+
)
|
| 1213 |
+
return changed, active_sources
|
| 1214 |
+
|
| 1215 |
+
|
| 1216 |
+
def _prune_stale_seeded_entries(entries: List[PooledCredential], active_sources: Set[str]) -> bool:
|
| 1217 |
+
retained = [
|
| 1218 |
+
entry
|
| 1219 |
+
for entry in entries
|
| 1220 |
+
if _is_manual_source(entry.source)
|
| 1221 |
+
or entry.source in active_sources
|
| 1222 |
+
or not (
|
| 1223 |
+
entry.source.startswith("env:")
|
| 1224 |
+
or entry.source in {"claude_code", "hermes_pkce"}
|
| 1225 |
+
)
|
| 1226 |
+
]
|
| 1227 |
+
if len(retained) == len(entries):
|
| 1228 |
+
return False
|
| 1229 |
+
entries[:] = retained
|
| 1230 |
+
return True
|
| 1231 |
+
|
| 1232 |
+
|
| 1233 |
+
def _seed_custom_pool(pool_key: str, entries: List[PooledCredential]) -> Tuple[bool, Set[str]]:
|
| 1234 |
+
"""Seed a custom endpoint pool from custom_providers config and model config."""
|
| 1235 |
+
changed = False
|
| 1236 |
+
active_sources: Set[str] = set()
|
| 1237 |
+
|
| 1238 |
+
# Seed from the custom_providers config entry's api_key field
|
| 1239 |
+
cp_config = _get_custom_provider_config(pool_key)
|
| 1240 |
+
if cp_config:
|
| 1241 |
+
api_key = str(cp_config.get("api_key") or "").strip()
|
| 1242 |
+
base_url = str(cp_config.get("base_url") or "").strip().rstrip("/")
|
| 1243 |
+
name = str(cp_config.get("name") or "").strip()
|
| 1244 |
+
if api_key:
|
| 1245 |
+
source = f"config:{name}"
|
| 1246 |
+
active_sources.add(source)
|
| 1247 |
+
changed |= _upsert_entry(
|
| 1248 |
+
entries,
|
| 1249 |
+
pool_key,
|
| 1250 |
+
source,
|
| 1251 |
+
{
|
| 1252 |
+
"source": source,
|
| 1253 |
+
"auth_type": AUTH_TYPE_API_KEY,
|
| 1254 |
+
"access_token": api_key,
|
| 1255 |
+
"base_url": base_url,
|
| 1256 |
+
"label": name or source,
|
| 1257 |
+
},
|
| 1258 |
+
)
|
| 1259 |
+
|
| 1260 |
+
# Seed from model.api_key if model.provider=='custom' and model.base_url matches
|
| 1261 |
+
try:
|
| 1262 |
+
config = _load_config_safe()
|
| 1263 |
+
model_cfg = config.get("model") if config else None
|
| 1264 |
+
if isinstance(model_cfg, dict):
|
| 1265 |
+
model_provider = str(model_cfg.get("provider") or "").strip().lower()
|
| 1266 |
+
model_base_url = str(model_cfg.get("base_url") or "").strip().rstrip("/")
|
| 1267 |
+
model_api_key = ""
|
| 1268 |
+
for k in ("api_key", "api"):
|
| 1269 |
+
v = model_cfg.get(k)
|
| 1270 |
+
if isinstance(v, str) and v.strip():
|
| 1271 |
+
model_api_key = v.strip()
|
| 1272 |
+
break
|
| 1273 |
+
if model_provider == "custom" and model_base_url and model_api_key:
|
| 1274 |
+
# Check if this model's base_url matches our custom provider
|
| 1275 |
+
matched_key = get_custom_provider_pool_key(model_base_url)
|
| 1276 |
+
if matched_key == pool_key:
|
| 1277 |
+
source = "model_config"
|
| 1278 |
+
active_sources.add(source)
|
| 1279 |
+
changed |= _upsert_entry(
|
| 1280 |
+
entries,
|
| 1281 |
+
pool_key,
|
| 1282 |
+
source,
|
| 1283 |
+
{
|
| 1284 |
+
"source": source,
|
| 1285 |
+
"auth_type": AUTH_TYPE_API_KEY,
|
| 1286 |
+
"access_token": model_api_key,
|
| 1287 |
+
"base_url": model_base_url,
|
| 1288 |
+
"label": "model_config",
|
| 1289 |
+
},
|
| 1290 |
+
)
|
| 1291 |
+
except Exception:
|
| 1292 |
+
pass
|
| 1293 |
+
|
| 1294 |
+
return changed, active_sources
|
| 1295 |
+
|
| 1296 |
+
|
| 1297 |
+
def load_pool(provider: str) -> CredentialPool:
|
| 1298 |
+
provider = (provider or "").strip().lower()
|
| 1299 |
+
raw_entries = read_credential_pool(provider)
|
| 1300 |
+
entries = [PooledCredential.from_dict(provider, payload) for payload in raw_entries]
|
| 1301 |
+
|
| 1302 |
+
if provider.startswith(CUSTOM_POOL_PREFIX):
|
| 1303 |
+
# Custom endpoint pool — seed from custom_providers config and model config
|
| 1304 |
+
custom_changed, custom_sources = _seed_custom_pool(provider, entries)
|
| 1305 |
+
changed = custom_changed
|
| 1306 |
+
changed |= _prune_stale_seeded_entries(entries, custom_sources)
|
| 1307 |
+
else:
|
| 1308 |
+
singleton_changed, singleton_sources = _seed_from_singletons(provider, entries)
|
| 1309 |
+
env_changed, env_sources = _seed_from_env(provider, entries)
|
| 1310 |
+
changed = singleton_changed or env_changed
|
| 1311 |
+
changed |= _prune_stale_seeded_entries(entries, singleton_sources | env_sources)
|
| 1312 |
+
changed |= _normalize_pool_priorities(provider, entries)
|
| 1313 |
+
|
| 1314 |
+
if changed:
|
| 1315 |
+
write_credential_pool(
|
| 1316 |
+
provider,
|
| 1317 |
+
[entry.to_dict() for entry in sorted(entries, key=lambda item: item.priority)],
|
| 1318 |
+
)
|
| 1319 |
+
return CredentialPool(provider, entries)
|
agent/display.py
ADDED
|
@@ -0,0 +1,1043 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""CLI presentation -- spinner, kawaii faces, tool preview formatting.
|
| 2 |
+
|
| 3 |
+
Pure display functions and classes with no AIAgent dependency.
|
| 4 |
+
Used by AIAgent._execute_tool_calls for CLI feedback.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import logging
|
| 8 |
+
import os
|
| 9 |
+
import sys
|
| 10 |
+
import threading
|
| 11 |
+
import time
|
| 12 |
+
from dataclasses import dataclass, field
|
| 13 |
+
from difflib import unified_diff
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
|
| 16 |
+
from utils import safe_json_loads
|
| 17 |
+
|
| 18 |
+
# ANSI escape codes for coloring tool failure indicators
|
| 19 |
+
_RED = "\033[31m"
|
| 20 |
+
_RESET = "\033[0m"
|
| 21 |
+
|
| 22 |
+
logger = logging.getLogger(__name__)
|
| 23 |
+
|
| 24 |
+
_ANSI_RESET = "\033[0m"
|
| 25 |
+
|
| 26 |
+
# Diff colors — resolved lazily from the skin engine so they adapt
|
| 27 |
+
# to light/dark themes. Falls back to sensible defaults on import
|
| 28 |
+
# failure. We cache after first resolution for performance.
|
| 29 |
+
_diff_colors_cached: dict[str, str] | None = None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _diff_ansi() -> dict[str, str]:
|
| 33 |
+
"""Return ANSI escapes for diff display, resolved from the active skin."""
|
| 34 |
+
global _diff_colors_cached
|
| 35 |
+
if _diff_colors_cached is not None:
|
| 36 |
+
return _diff_colors_cached
|
| 37 |
+
|
| 38 |
+
# Defaults that work on dark terminals
|
| 39 |
+
dim = "\033[38;2;150;150;150m"
|
| 40 |
+
file_c = "\033[38;2;180;160;255m"
|
| 41 |
+
hunk = "\033[38;2;120;120;140m"
|
| 42 |
+
minus = "\033[38;2;255;255;255;48;2;120;20;20m"
|
| 43 |
+
plus = "\033[38;2;255;255;255;48;2;20;90;20m"
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
from hermes_cli.skin_engine import get_active_skin
|
| 47 |
+
skin = get_active_skin()
|
| 48 |
+
|
| 49 |
+
def _hex_fg(key: str, fallback_rgb: tuple[int, int, int]) -> str:
|
| 50 |
+
h = skin.get_color(key, "")
|
| 51 |
+
if h and len(h) == 7 and h[0] == "#":
|
| 52 |
+
r, g, b = int(h[1:3], 16), int(h[3:5], 16), int(h[5:7], 16)
|
| 53 |
+
return f"\033[38;2;{r};{g};{b}m"
|
| 54 |
+
r, g, b = fallback_rgb
|
| 55 |
+
return f"\033[38;2;{r};{g};{b}m"
|
| 56 |
+
|
| 57 |
+
dim = _hex_fg("banner_dim", (150, 150, 150))
|
| 58 |
+
file_c = _hex_fg("session_label", (180, 160, 255))
|
| 59 |
+
hunk = _hex_fg("session_border", (120, 120, 140))
|
| 60 |
+
# minus/plus use background colors — derive from ui_error/ui_ok
|
| 61 |
+
err_h = skin.get_color("ui_error", "#ef5350")
|
| 62 |
+
ok_h = skin.get_color("ui_ok", "#4caf50")
|
| 63 |
+
if err_h and len(err_h) == 7:
|
| 64 |
+
er, eg, eb = int(err_h[1:3], 16), int(err_h[3:5], 16), int(err_h[5:7], 16)
|
| 65 |
+
# Use a dark tinted version as background
|
| 66 |
+
minus = f"\033[38;2;255;255;255;48;2;{max(er//2,20)};{max(eg//4,10)};{max(eb//4,10)}m"
|
| 67 |
+
if ok_h and len(ok_h) == 7:
|
| 68 |
+
or_, og, ob = int(ok_h[1:3], 16), int(ok_h[3:5], 16), int(ok_h[5:7], 16)
|
| 69 |
+
plus = f"\033[38;2;255;255;255;48;2;{max(or_//4,10)};{max(og//2,20)};{max(ob//4,10)}m"
|
| 70 |
+
except Exception:
|
| 71 |
+
pass
|
| 72 |
+
|
| 73 |
+
_diff_colors_cached = {
|
| 74 |
+
"dim": dim, "file": file_c, "hunk": hunk,
|
| 75 |
+
"minus": minus, "plus": plus,
|
| 76 |
+
}
|
| 77 |
+
return _diff_colors_cached
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def reset_diff_colors() -> None:
|
| 81 |
+
"""Reset cached diff colors (call after /skin switch)."""
|
| 82 |
+
global _diff_colors_cached
|
| 83 |
+
_diff_colors_cached = None
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# Module-level helpers — each call resolves from the active skin lazily.
|
| 87 |
+
def _diff_dim(): return _diff_ansi()["dim"]
|
| 88 |
+
def _diff_file(): return _diff_ansi()["file"]
|
| 89 |
+
def _diff_hunk(): return _diff_ansi()["hunk"]
|
| 90 |
+
def _diff_minus(): return _diff_ansi()["minus"]
|
| 91 |
+
def _diff_plus(): return _diff_ansi()["plus"]
|
| 92 |
+
_MAX_INLINE_DIFF_FILES = 6
|
| 93 |
+
_MAX_INLINE_DIFF_LINES = 80
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@dataclass
|
| 97 |
+
class LocalEditSnapshot:
|
| 98 |
+
"""Pre-tool filesystem snapshot used to render diffs locally after writes."""
|
| 99 |
+
paths: list[Path] = field(default_factory=list)
|
| 100 |
+
before: dict[str, str | None] = field(default_factory=dict)
|
| 101 |
+
|
| 102 |
+
# =========================================================================
|
| 103 |
+
# Configurable tool preview length (0 = no limit)
|
| 104 |
+
# Set once at startup by CLI or gateway from display.tool_preview_length config.
|
| 105 |
+
# =========================================================================
|
| 106 |
+
_tool_preview_max_len: int = 0 # 0 = unlimited
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def set_tool_preview_max_len(n: int) -> None:
|
| 110 |
+
"""Set the global max length for tool call previews. 0 = no limit."""
|
| 111 |
+
global _tool_preview_max_len
|
| 112 |
+
_tool_preview_max_len = max(int(n), 0) if n else 0
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def get_tool_preview_max_len() -> int:
|
| 116 |
+
"""Return the configured max preview length (0 = unlimited)."""
|
| 117 |
+
return _tool_preview_max_len
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# =========================================================================
|
| 121 |
+
# Skin-aware helpers (lazy import to avoid circular deps)
|
| 122 |
+
# =========================================================================
|
| 123 |
+
|
| 124 |
+
def _get_skin():
|
| 125 |
+
"""Get the active skin config, or None if not available."""
|
| 126 |
+
try:
|
| 127 |
+
from hermes_cli.skin_engine import get_active_skin
|
| 128 |
+
return get_active_skin()
|
| 129 |
+
except Exception:
|
| 130 |
+
return None
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def get_skin_tool_prefix() -> str:
|
| 134 |
+
"""Get tool output prefix character from active skin."""
|
| 135 |
+
skin = _get_skin()
|
| 136 |
+
if skin:
|
| 137 |
+
return skin.tool_prefix
|
| 138 |
+
return "┊"
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def get_tool_emoji(tool_name: str, default: str = "⚡") -> str:
|
| 142 |
+
"""Get the display emoji for a tool.
|
| 143 |
+
|
| 144 |
+
Resolution order:
|
| 145 |
+
1. Active skin's ``tool_emojis`` overrides (if a skin is loaded)
|
| 146 |
+
2. Tool registry's per-tool ``emoji`` field
|
| 147 |
+
3. *default* fallback
|
| 148 |
+
"""
|
| 149 |
+
# 1. Skin override
|
| 150 |
+
skin = _get_skin()
|
| 151 |
+
if skin and skin.tool_emojis:
|
| 152 |
+
override = skin.tool_emojis.get(tool_name)
|
| 153 |
+
if override:
|
| 154 |
+
return override
|
| 155 |
+
# 2. Registry default
|
| 156 |
+
try:
|
| 157 |
+
from tools.registry import registry
|
| 158 |
+
emoji = registry.get_emoji(tool_name, default="")
|
| 159 |
+
if emoji:
|
| 160 |
+
return emoji
|
| 161 |
+
except Exception:
|
| 162 |
+
pass
|
| 163 |
+
# 3. Hardcoded fallback
|
| 164 |
+
return default
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
# =========================================================================
|
| 168 |
+
# Tool preview (one-line summary of a tool call's primary argument)
|
| 169 |
+
# =========================================================================
|
| 170 |
+
|
| 171 |
+
def _oneline(text: str) -> str:
|
| 172 |
+
"""Collapse whitespace (including newlines) to single spaces."""
|
| 173 |
+
return " ".join(text.split())
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def build_tool_preview(tool_name: str, args: dict, max_len: int | None = None) -> str | None:
|
| 177 |
+
"""Build a short preview of a tool call's primary argument for display.
|
| 178 |
+
|
| 179 |
+
*max_len* controls truncation. ``None`` (default) defers to the global
|
| 180 |
+
``_tool_preview_max_len`` set via config; ``0`` means unlimited.
|
| 181 |
+
"""
|
| 182 |
+
if max_len is None:
|
| 183 |
+
max_len = _tool_preview_max_len
|
| 184 |
+
if not args:
|
| 185 |
+
return None
|
| 186 |
+
primary_args = {
|
| 187 |
+
"terminal": "command", "web_search": "query", "web_extract": "urls",
|
| 188 |
+
"read_file": "path", "write_file": "path", "patch": "path",
|
| 189 |
+
"search_files": "pattern", "browser_navigate": "url",
|
| 190 |
+
"browser_click": "ref", "browser_type": "text",
|
| 191 |
+
"image_generate": "prompt", "text_to_speech": "text",
|
| 192 |
+
"vision_analyze": "question", "mixture_of_agents": "user_prompt",
|
| 193 |
+
"skill_view": "name", "skills_list": "category",
|
| 194 |
+
"cronjob": "action",
|
| 195 |
+
"execute_code": "code", "delegate_task": "goal",
|
| 196 |
+
"clarify": "question", "skill_manage": "name",
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
if tool_name == "process":
|
| 200 |
+
action = args.get("action", "")
|
| 201 |
+
sid = args.get("session_id", "")
|
| 202 |
+
data = args.get("data", "")
|
| 203 |
+
timeout_val = args.get("timeout")
|
| 204 |
+
parts = [action]
|
| 205 |
+
if sid:
|
| 206 |
+
parts.append(sid[:16])
|
| 207 |
+
if data:
|
| 208 |
+
parts.append(f'"{_oneline(data[:20])}"')
|
| 209 |
+
if timeout_val and action == "wait":
|
| 210 |
+
parts.append(f"{timeout_val}s")
|
| 211 |
+
return " ".join(parts) if parts else None
|
| 212 |
+
|
| 213 |
+
if tool_name == "todo":
|
| 214 |
+
todos_arg = args.get("todos")
|
| 215 |
+
merge = args.get("merge", False)
|
| 216 |
+
if todos_arg is None:
|
| 217 |
+
return "reading task list"
|
| 218 |
+
elif merge:
|
| 219 |
+
return f"updating {len(todos_arg)} task(s)"
|
| 220 |
+
else:
|
| 221 |
+
return f"planning {len(todos_arg)} task(s)"
|
| 222 |
+
|
| 223 |
+
if tool_name == "session_search":
|
| 224 |
+
query = _oneline(args.get("query", ""))
|
| 225 |
+
return f"recall: \"{query[:25]}{'...' if len(query) > 25 else ''}\""
|
| 226 |
+
|
| 227 |
+
if tool_name == "memory":
|
| 228 |
+
action = args.get("action", "")
|
| 229 |
+
target = args.get("target", "")
|
| 230 |
+
if action == "add":
|
| 231 |
+
content = _oneline(args.get("content", ""))
|
| 232 |
+
return f"+{target}: \"{content[:25]}{'...' if len(content) > 25 else ''}\""
|
| 233 |
+
elif action == "replace":
|
| 234 |
+
return f"~{target}: \"{_oneline(args.get('old_text', '')[:20])}\""
|
| 235 |
+
elif action == "remove":
|
| 236 |
+
return f"-{target}: \"{_oneline(args.get('old_text', '')[:20])}\""
|
| 237 |
+
return action
|
| 238 |
+
|
| 239 |
+
if tool_name == "send_message":
|
| 240 |
+
target = args.get("target", "?")
|
| 241 |
+
msg = _oneline(args.get("message", ""))
|
| 242 |
+
if len(msg) > 20:
|
| 243 |
+
msg = msg[:17] + "..."
|
| 244 |
+
return f"to {target}: \"{msg}\""
|
| 245 |
+
|
| 246 |
+
if tool_name.startswith("rl_"):
|
| 247 |
+
rl_previews = {
|
| 248 |
+
"rl_list_environments": "listing envs",
|
| 249 |
+
"rl_select_environment": args.get("name", ""),
|
| 250 |
+
"rl_get_current_config": "reading config",
|
| 251 |
+
"rl_edit_config": f"{args.get('field', '')}={args.get('value', '')}",
|
| 252 |
+
"rl_start_training": "starting",
|
| 253 |
+
"rl_check_status": args.get("run_id", "")[:16],
|
| 254 |
+
"rl_stop_training": f"stopping {args.get('run_id', '')[:16]}",
|
| 255 |
+
"rl_get_results": args.get("run_id", "")[:16],
|
| 256 |
+
"rl_list_runs": "listing runs",
|
| 257 |
+
"rl_test_inference": f"{args.get('num_steps', 3)} steps",
|
| 258 |
+
}
|
| 259 |
+
return rl_previews.get(tool_name)
|
| 260 |
+
|
| 261 |
+
key = primary_args.get(tool_name)
|
| 262 |
+
if not key:
|
| 263 |
+
for fallback_key in ("query", "text", "command", "path", "name", "prompt", "code", "goal"):
|
| 264 |
+
if fallback_key in args:
|
| 265 |
+
key = fallback_key
|
| 266 |
+
break
|
| 267 |
+
|
| 268 |
+
if not key or key not in args:
|
| 269 |
+
return None
|
| 270 |
+
|
| 271 |
+
value = args[key]
|
| 272 |
+
if isinstance(value, list):
|
| 273 |
+
value = value[0] if value else ""
|
| 274 |
+
|
| 275 |
+
preview = _oneline(str(value))
|
| 276 |
+
if not preview:
|
| 277 |
+
return None
|
| 278 |
+
if max_len > 0 and len(preview) > max_len:
|
| 279 |
+
preview = preview[:max_len - 3] + "..."
|
| 280 |
+
return preview
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
# =========================================================================
|
| 284 |
+
# Inline diff previews for write actions
|
| 285 |
+
# =========================================================================
|
| 286 |
+
|
| 287 |
+
def _resolved_path(path: str) -> Path:
|
| 288 |
+
"""Resolve a possibly-relative filesystem path against the current cwd."""
|
| 289 |
+
candidate = Path(os.path.expanduser(path))
|
| 290 |
+
if candidate.is_absolute():
|
| 291 |
+
return candidate
|
| 292 |
+
return Path.cwd() / candidate
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
def _snapshot_text(path: Path) -> str | None:
|
| 296 |
+
"""Return UTF-8 file content, or None for missing/unreadable files."""
|
| 297 |
+
try:
|
| 298 |
+
return path.read_text(encoding="utf-8")
|
| 299 |
+
except (FileNotFoundError, IsADirectoryError, UnicodeDecodeError, OSError):
|
| 300 |
+
return None
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
def _display_diff_path(path: Path) -> str:
|
| 304 |
+
"""Prefer cwd-relative paths in diffs when available."""
|
| 305 |
+
try:
|
| 306 |
+
return str(path.resolve().relative_to(Path.cwd().resolve()))
|
| 307 |
+
except Exception:
|
| 308 |
+
return str(path)
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def _resolve_skill_manage_paths(args: dict) -> list[Path]:
|
| 312 |
+
"""Resolve skill_manage write targets to filesystem paths."""
|
| 313 |
+
action = args.get("action")
|
| 314 |
+
name = args.get("name")
|
| 315 |
+
if not action or not name:
|
| 316 |
+
return []
|
| 317 |
+
|
| 318 |
+
from tools.skill_manager_tool import _find_skill, _resolve_skill_dir
|
| 319 |
+
|
| 320 |
+
if action == "create":
|
| 321 |
+
skill_dir = _resolve_skill_dir(name, args.get("category"))
|
| 322 |
+
return [skill_dir / "SKILL.md"]
|
| 323 |
+
|
| 324 |
+
existing = _find_skill(name)
|
| 325 |
+
if not existing:
|
| 326 |
+
return []
|
| 327 |
+
|
| 328 |
+
skill_dir = Path(existing["path"])
|
| 329 |
+
if action in {"edit", "patch"}:
|
| 330 |
+
file_path = args.get("file_path")
|
| 331 |
+
return [skill_dir / file_path] if file_path else [skill_dir / "SKILL.md"]
|
| 332 |
+
if action in {"write_file", "remove_file"}:
|
| 333 |
+
file_path = args.get("file_path")
|
| 334 |
+
return [skill_dir / file_path] if file_path else []
|
| 335 |
+
if action == "delete":
|
| 336 |
+
files = [path for path in sorted(skill_dir.rglob("*")) if path.is_file()]
|
| 337 |
+
return files
|
| 338 |
+
return []
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
def _resolve_local_edit_paths(tool_name: str, function_args: dict | None) -> list[Path]:
|
| 342 |
+
"""Resolve local filesystem targets for write-capable tools."""
|
| 343 |
+
if not isinstance(function_args, dict):
|
| 344 |
+
return []
|
| 345 |
+
|
| 346 |
+
if tool_name == "write_file":
|
| 347 |
+
path = function_args.get("path")
|
| 348 |
+
return [_resolved_path(path)] if path else []
|
| 349 |
+
|
| 350 |
+
if tool_name == "patch":
|
| 351 |
+
path = function_args.get("path")
|
| 352 |
+
return [_resolved_path(path)] if path else []
|
| 353 |
+
|
| 354 |
+
if tool_name == "skill_manage":
|
| 355 |
+
return _resolve_skill_manage_paths(function_args)
|
| 356 |
+
|
| 357 |
+
return []
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
def capture_local_edit_snapshot(tool_name: str, function_args: dict | None) -> LocalEditSnapshot | None:
|
| 361 |
+
"""Capture before-state for local write previews."""
|
| 362 |
+
paths = _resolve_local_edit_paths(tool_name, function_args)
|
| 363 |
+
if not paths:
|
| 364 |
+
return None
|
| 365 |
+
|
| 366 |
+
snapshot = LocalEditSnapshot(paths=paths)
|
| 367 |
+
for path in paths:
|
| 368 |
+
snapshot.before[str(path)] = _snapshot_text(path)
|
| 369 |
+
return snapshot
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
def _result_succeeded(result: str | None) -> bool:
|
| 373 |
+
"""Conservatively detect whether a tool result represents success."""
|
| 374 |
+
if not result:
|
| 375 |
+
return False
|
| 376 |
+
data = safe_json_loads(result)
|
| 377 |
+
if data is None:
|
| 378 |
+
return False
|
| 379 |
+
if not isinstance(data, dict):
|
| 380 |
+
return False
|
| 381 |
+
if data.get("error"):
|
| 382 |
+
return False
|
| 383 |
+
if "success" in data:
|
| 384 |
+
return bool(data.get("success"))
|
| 385 |
+
return True
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
def _diff_from_snapshot(snapshot: LocalEditSnapshot | None) -> str | None:
|
| 389 |
+
"""Generate unified diff text from a stored before-state and current files."""
|
| 390 |
+
if not snapshot:
|
| 391 |
+
return None
|
| 392 |
+
|
| 393 |
+
chunks: list[str] = []
|
| 394 |
+
for path in snapshot.paths:
|
| 395 |
+
before = snapshot.before.get(str(path))
|
| 396 |
+
after = _snapshot_text(path)
|
| 397 |
+
if before == after:
|
| 398 |
+
continue
|
| 399 |
+
|
| 400 |
+
display_path = _display_diff_path(path)
|
| 401 |
+
diff = "".join(
|
| 402 |
+
unified_diff(
|
| 403 |
+
[] if before is None else before.splitlines(keepends=True),
|
| 404 |
+
[] if after is None else after.splitlines(keepends=True),
|
| 405 |
+
fromfile=f"a/{display_path}",
|
| 406 |
+
tofile=f"b/{display_path}",
|
| 407 |
+
)
|
| 408 |
+
)
|
| 409 |
+
if diff:
|
| 410 |
+
chunks.append(diff)
|
| 411 |
+
|
| 412 |
+
if not chunks:
|
| 413 |
+
return None
|
| 414 |
+
return "".join(chunk if chunk.endswith("\n") else chunk + "\n" for chunk in chunks)
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
def extract_edit_diff(
|
| 418 |
+
tool_name: str,
|
| 419 |
+
result: str | None,
|
| 420 |
+
*,
|
| 421 |
+
function_args: dict | None = None,
|
| 422 |
+
snapshot: LocalEditSnapshot | None = None,
|
| 423 |
+
) -> str | None:
|
| 424 |
+
"""Extract a unified diff from a file-edit tool result."""
|
| 425 |
+
if tool_name == "patch" and result:
|
| 426 |
+
data = safe_json_loads(result)
|
| 427 |
+
if isinstance(data, dict):
|
| 428 |
+
diff = data.get("diff")
|
| 429 |
+
if isinstance(diff, str) and diff.strip():
|
| 430 |
+
return diff
|
| 431 |
+
|
| 432 |
+
if tool_name not in {"write_file", "patch", "skill_manage"}:
|
| 433 |
+
return None
|
| 434 |
+
if not _result_succeeded(result):
|
| 435 |
+
return None
|
| 436 |
+
return _diff_from_snapshot(snapshot)
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
def _emit_inline_diff(diff_text: str, print_fn) -> bool:
|
| 440 |
+
"""Emit rendered diff text through the CLI's prompt_toolkit-safe printer."""
|
| 441 |
+
if print_fn is None or not diff_text:
|
| 442 |
+
return False
|
| 443 |
+
try:
|
| 444 |
+
print_fn(" ┊ review diff")
|
| 445 |
+
for line in diff_text.rstrip("\n").splitlines():
|
| 446 |
+
print_fn(line)
|
| 447 |
+
return True
|
| 448 |
+
except Exception:
|
| 449 |
+
return False
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
def _render_inline_unified_diff(diff: str) -> list[str]:
|
| 453 |
+
"""Render unified diff lines in Hermes' inline transcript style."""
|
| 454 |
+
rendered: list[str] = []
|
| 455 |
+
from_file = None
|
| 456 |
+
to_file = None
|
| 457 |
+
|
| 458 |
+
for raw_line in diff.splitlines():
|
| 459 |
+
if raw_line.startswith("--- "):
|
| 460 |
+
from_file = raw_line[4:].strip()
|
| 461 |
+
continue
|
| 462 |
+
if raw_line.startswith("+++ "):
|
| 463 |
+
to_file = raw_line[4:].strip()
|
| 464 |
+
if from_file or to_file:
|
| 465 |
+
rendered.append(f"{_diff_file()}{from_file or 'a/?'} → {to_file or 'b/?'}{_ANSI_RESET}")
|
| 466 |
+
continue
|
| 467 |
+
if raw_line.startswith("@@"):
|
| 468 |
+
rendered.append(f"{_diff_hunk()}{raw_line}{_ANSI_RESET}")
|
| 469 |
+
continue
|
| 470 |
+
if raw_line.startswith("-"):
|
| 471 |
+
rendered.append(f"{_diff_minus()}{raw_line}{_ANSI_RESET}")
|
| 472 |
+
continue
|
| 473 |
+
if raw_line.startswith("+"):
|
| 474 |
+
rendered.append(f"{_diff_plus()}{raw_line}{_ANSI_RESET}")
|
| 475 |
+
continue
|
| 476 |
+
if raw_line.startswith(" "):
|
| 477 |
+
rendered.append(f"{_diff_dim()}{raw_line}{_ANSI_RESET}")
|
| 478 |
+
continue
|
| 479 |
+
if raw_line:
|
| 480 |
+
rendered.append(raw_line)
|
| 481 |
+
|
| 482 |
+
return rendered
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
def _split_unified_diff_sections(diff: str) -> list[str]:
|
| 486 |
+
"""Split a unified diff into per-file sections."""
|
| 487 |
+
sections: list[list[str]] = []
|
| 488 |
+
current: list[str] = []
|
| 489 |
+
|
| 490 |
+
for line in diff.splitlines():
|
| 491 |
+
if line.startswith("--- ") and current:
|
| 492 |
+
sections.append(current)
|
| 493 |
+
current = [line]
|
| 494 |
+
continue
|
| 495 |
+
current.append(line)
|
| 496 |
+
|
| 497 |
+
if current:
|
| 498 |
+
sections.append(current)
|
| 499 |
+
|
| 500 |
+
return ["\n".join(section) for section in sections if section]
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
def _summarize_rendered_diff_sections(
|
| 504 |
+
diff: str,
|
| 505 |
+
*,
|
| 506 |
+
max_files: int = _MAX_INLINE_DIFF_FILES,
|
| 507 |
+
max_lines: int = _MAX_INLINE_DIFF_LINES,
|
| 508 |
+
) -> list[str]:
|
| 509 |
+
"""Render diff sections while capping file count and total line count."""
|
| 510 |
+
sections = _split_unified_diff_sections(diff)
|
| 511 |
+
rendered: list[str] = []
|
| 512 |
+
omitted_files = 0
|
| 513 |
+
omitted_lines = 0
|
| 514 |
+
|
| 515 |
+
for idx, section in enumerate(sections):
|
| 516 |
+
if idx >= max_files:
|
| 517 |
+
omitted_files += 1
|
| 518 |
+
omitted_lines += len(_render_inline_unified_diff(section))
|
| 519 |
+
continue
|
| 520 |
+
|
| 521 |
+
section_lines = _render_inline_unified_diff(section)
|
| 522 |
+
remaining_budget = max_lines - len(rendered)
|
| 523 |
+
if remaining_budget <= 0:
|
| 524 |
+
omitted_lines += len(section_lines)
|
| 525 |
+
omitted_files += 1
|
| 526 |
+
continue
|
| 527 |
+
|
| 528 |
+
if len(section_lines) <= remaining_budget:
|
| 529 |
+
rendered.extend(section_lines)
|
| 530 |
+
continue
|
| 531 |
+
|
| 532 |
+
rendered.extend(section_lines[:remaining_budget])
|
| 533 |
+
omitted_lines += len(section_lines) - remaining_budget
|
| 534 |
+
omitted_files += 1 + max(0, len(sections) - idx - 1)
|
| 535 |
+
for leftover in sections[idx + 1:]:
|
| 536 |
+
omitted_lines += len(_render_inline_unified_diff(leftover))
|
| 537 |
+
break
|
| 538 |
+
|
| 539 |
+
if omitted_files or omitted_lines:
|
| 540 |
+
summary = f"… omitted {omitted_lines} diff line(s)"
|
| 541 |
+
if omitted_files:
|
| 542 |
+
summary += f" across {omitted_files} additional file(s)/section(s)"
|
| 543 |
+
rendered.append(f"{_diff_hunk()}{summary}{_ANSI_RESET}")
|
| 544 |
+
|
| 545 |
+
return rendered
|
| 546 |
+
|
| 547 |
+
|
| 548 |
+
def render_edit_diff_with_delta(
|
| 549 |
+
tool_name: str,
|
| 550 |
+
result: str | None,
|
| 551 |
+
*,
|
| 552 |
+
function_args: dict | None = None,
|
| 553 |
+
snapshot: LocalEditSnapshot | None = None,
|
| 554 |
+
print_fn=None,
|
| 555 |
+
) -> bool:
|
| 556 |
+
"""Render an edit diff inline without taking over the terminal UI."""
|
| 557 |
+
diff = extract_edit_diff(
|
| 558 |
+
tool_name,
|
| 559 |
+
result,
|
| 560 |
+
function_args=function_args,
|
| 561 |
+
snapshot=snapshot,
|
| 562 |
+
)
|
| 563 |
+
if not diff:
|
| 564 |
+
return False
|
| 565 |
+
try:
|
| 566 |
+
rendered_lines = _summarize_rendered_diff_sections(diff)
|
| 567 |
+
except Exception as exc:
|
| 568 |
+
logger.debug("Could not render inline diff: %s", exc)
|
| 569 |
+
return False
|
| 570 |
+
return _emit_inline_diff("\n".join(rendered_lines), print_fn)
|
| 571 |
+
|
| 572 |
+
|
| 573 |
+
# =========================================================================
|
| 574 |
+
# KawaiiSpinner
|
| 575 |
+
# =========================================================================
|
| 576 |
+
|
| 577 |
+
class KawaiiSpinner:
|
| 578 |
+
"""Animated spinner with kawaii faces for CLI feedback during tool execution."""
|
| 579 |
+
|
| 580 |
+
SPINNERS = {
|
| 581 |
+
'dots': ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
|
| 582 |
+
'bounce': ['⠁', '⠂', '⠄', '⡀', '⢀', '⠠', '⠐', '⠈'],
|
| 583 |
+
'grow': ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█', '▇', '▆', '▅', '▄', '▃', '▂'],
|
| 584 |
+
'arrows': ['←', '↖', '↑', '↗', '→', '↘', '↓', '↙'],
|
| 585 |
+
'star': ['✶', '✷', '✸', '✹', '✺', '✹', '✸', '✷'],
|
| 586 |
+
'moon': ['🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘'],
|
| 587 |
+
'pulse': ['◜', '◠', '◝', '◞', '◡', '◟'],
|
| 588 |
+
'brain': ['🧠', '💭', '💡', '✨', '💫', '🌟', '💡', '💭'],
|
| 589 |
+
'sparkle': ['⁺', '˚', '*', '✧', '✦', '✧', '*', '˚'],
|
| 590 |
+
}
|
| 591 |
+
|
| 592 |
+
KAWAII_WAITING = [
|
| 593 |
+
"(。◕‿◕。)", "(◕‿◕✿)", "٩(◕‿◕。)۶", "(✿◠‿◠)", "( ˘▽˘)っ",
|
| 594 |
+
"♪(´ε` )", "(◕ᴗ◕✿)", "ヾ(^∇^)", "(≧◡≦)", "(★ω★)",
|
| 595 |
+
]
|
| 596 |
+
|
| 597 |
+
KAWAII_THINKING = [
|
| 598 |
+
"(。•́︿•̀。)", "(◔_◔)", "(¬‿¬)", "( •_•)>⌐■-■", "(⌐■_■)",
|
| 599 |
+
"(´・_・`)", "◉_◉", "(°ロ°)", "( ˘⌣˘)♡", "ヽ(>∀<☆)☆",
|
| 600 |
+
"٩(๑❛ᴗ❛๑)۶", "(⊙_⊙)", "(¬_¬)", "( ͡° ͜ʖ ͡°)", "ಠ_ಠ",
|
| 601 |
+
]
|
| 602 |
+
|
| 603 |
+
THINKING_VERBS = [
|
| 604 |
+
"pondering", "contemplating", "musing", "cogitating", "ruminating",
|
| 605 |
+
"deliberating", "mulling", "reflecting", "processing", "reasoning",
|
| 606 |
+
"analyzing", "computing", "synthesizing", "formulating", "brainstorming",
|
| 607 |
+
]
|
| 608 |
+
|
| 609 |
+
def __init__(self, message: str = "", spinner_type: str = 'dots', print_fn=None):
|
| 610 |
+
self.message = message
|
| 611 |
+
self.spinner_frames = self.SPINNERS.get(spinner_type, self.SPINNERS['dots'])
|
| 612 |
+
self.running = False
|
| 613 |
+
self.thread = None
|
| 614 |
+
self.frame_idx = 0
|
| 615 |
+
self.start_time = None
|
| 616 |
+
self.last_line_len = 0
|
| 617 |
+
# Optional callable to route all output through (e.g. a no-op for silent
|
| 618 |
+
# background agents). When set, bypasses self._out entirely so that
|
| 619 |
+
# agents with _print_fn overridden remain fully silent.
|
| 620 |
+
self._print_fn = print_fn
|
| 621 |
+
# Capture stdout NOW, before any redirect_stdout(devnull) from
|
| 622 |
+
# child agents can replace sys.stdout with a black hole.
|
| 623 |
+
self._out = sys.stdout
|
| 624 |
+
|
| 625 |
+
def _write(self, text: str, end: str = '\n', flush: bool = False):
|
| 626 |
+
"""Write to the stdout captured at spinner creation time.
|
| 627 |
+
|
| 628 |
+
If a print_fn was supplied at construction, all output is routed through
|
| 629 |
+
it instead — allowing callers to silence the spinner with a no-op lambda.
|
| 630 |
+
"""
|
| 631 |
+
if self._print_fn is not None:
|
| 632 |
+
try:
|
| 633 |
+
self._print_fn(text)
|
| 634 |
+
except Exception:
|
| 635 |
+
pass
|
| 636 |
+
return
|
| 637 |
+
try:
|
| 638 |
+
self._out.write(text + end)
|
| 639 |
+
if flush:
|
| 640 |
+
self._out.flush()
|
| 641 |
+
except (ValueError, OSError):
|
| 642 |
+
pass
|
| 643 |
+
|
| 644 |
+
@property
|
| 645 |
+
def _is_tty(self) -> bool:
|
| 646 |
+
"""Check if output is a real terminal, safe against closed streams."""
|
| 647 |
+
try:
|
| 648 |
+
return hasattr(self._out, 'isatty') and self._out.isatty()
|
| 649 |
+
except (ValueError, OSError):
|
| 650 |
+
return False
|
| 651 |
+
|
| 652 |
+
def _is_patch_stdout_proxy(self) -> bool:
|
| 653 |
+
"""Return True when stdout is prompt_toolkit's StdoutProxy.
|
| 654 |
+
|
| 655 |
+
patch_stdout wraps sys.stdout in a StdoutProxy that queues writes and
|
| 656 |
+
injects newlines around each flush(). The \\r overwrite never lands on
|
| 657 |
+
the correct line — each spinner frame ends up on its own line.
|
| 658 |
+
|
| 659 |
+
The CLI already drives a TUI widget (_spinner_text) for spinner display,
|
| 660 |
+
so KawaiiSpinner's \\r-based animation is redundant under StdoutProxy.
|
| 661 |
+
"""
|
| 662 |
+
try:
|
| 663 |
+
from prompt_toolkit.patch_stdout import StdoutProxy
|
| 664 |
+
return isinstance(self._out, StdoutProxy)
|
| 665 |
+
except ImportError:
|
| 666 |
+
return False
|
| 667 |
+
|
| 668 |
+
def _animate(self):
|
| 669 |
+
# When stdout is not a real terminal (e.g. Docker, systemd, pipe),
|
| 670 |
+
# skip the animation entirely — it creates massive log bloat.
|
| 671 |
+
# Just log the start once and let stop() log the completion.
|
| 672 |
+
if not self._is_tty:
|
| 673 |
+
self._write(f" [tool] {self.message}", flush=True)
|
| 674 |
+
while self.running:
|
| 675 |
+
time.sleep(0.5)
|
| 676 |
+
return
|
| 677 |
+
|
| 678 |
+
# When running inside prompt_toolkit's patch_stdout context the CLI
|
| 679 |
+
# renders spinner state via a dedicated TUI widget (_spinner_text).
|
| 680 |
+
# Driving a \r-based animation here too causes visual overdraw: the
|
| 681 |
+
# StdoutProxy injects newlines around each flush, so every frame lands
|
| 682 |
+
# on a new line and overwrites the status bar.
|
| 683 |
+
if self._is_patch_stdout_proxy():
|
| 684 |
+
while self.running:
|
| 685 |
+
time.sleep(0.1)
|
| 686 |
+
return
|
| 687 |
+
|
| 688 |
+
# Cache skin wings at start (avoid per-frame imports)
|
| 689 |
+
skin = _get_skin()
|
| 690 |
+
wings = skin.get_spinner_wings() if skin else []
|
| 691 |
+
|
| 692 |
+
while self.running:
|
| 693 |
+
if os.getenv("HERMES_SPINNER_PAUSE"):
|
| 694 |
+
time.sleep(0.1)
|
| 695 |
+
continue
|
| 696 |
+
frame = self.spinner_frames[self.frame_idx % len(self.spinner_frames)]
|
| 697 |
+
elapsed = time.time() - self.start_time
|
| 698 |
+
if wings:
|
| 699 |
+
left, right = wings[self.frame_idx % len(wings)]
|
| 700 |
+
line = f" {left} {frame} {self.message} {right} ({elapsed:.1f}s)"
|
| 701 |
+
else:
|
| 702 |
+
line = f" {frame} {self.message} ({elapsed:.1f}s)"
|
| 703 |
+
pad = max(self.last_line_len - len(line), 0)
|
| 704 |
+
self._write(f"\r{line}{' ' * pad}", end='', flush=True)
|
| 705 |
+
self.last_line_len = len(line)
|
| 706 |
+
self.frame_idx += 1
|
| 707 |
+
time.sleep(0.12)
|
| 708 |
+
|
| 709 |
+
def start(self):
|
| 710 |
+
if self.running:
|
| 711 |
+
return
|
| 712 |
+
self.running = True
|
| 713 |
+
self.start_time = time.time()
|
| 714 |
+
self.thread = threading.Thread(target=self._animate, daemon=True)
|
| 715 |
+
self.thread.start()
|
| 716 |
+
|
| 717 |
+
def update_text(self, new_message: str):
|
| 718 |
+
self.message = new_message
|
| 719 |
+
|
| 720 |
+
def print_above(self, text: str):
|
| 721 |
+
"""Print a line above the spinner without disrupting animation.
|
| 722 |
+
|
| 723 |
+
Clears the current spinner line, prints the text, and lets the
|
| 724 |
+
next animation tick redraw the spinner on the line below.
|
| 725 |
+
Thread-safe: uses the captured stdout reference (self._out).
|
| 726 |
+
Works inside redirect_stdout(devnull) because _write bypasses
|
| 727 |
+
sys.stdout and writes to the stdout captured at spinner creation.
|
| 728 |
+
"""
|
| 729 |
+
if not self.running:
|
| 730 |
+
self._write(f" {text}", flush=True)
|
| 731 |
+
return
|
| 732 |
+
# Clear spinner line with spaces (not \033[K) to avoid garbled escape
|
| 733 |
+
# codes when prompt_toolkit's patch_stdout is active — same approach
|
| 734 |
+
# as stop(). Then print text; spinner redraws on next tick.
|
| 735 |
+
blanks = ' ' * max(self.last_line_len + 5, 40)
|
| 736 |
+
self._write(f"\r{blanks}\r {text}", flush=True)
|
| 737 |
+
|
| 738 |
+
def stop(self, final_message: str = None):
|
| 739 |
+
self.running = False
|
| 740 |
+
if self.thread:
|
| 741 |
+
self.thread.join(timeout=0.5)
|
| 742 |
+
|
| 743 |
+
is_tty = self._is_tty
|
| 744 |
+
if is_tty:
|
| 745 |
+
# Clear the spinner line with spaces instead of \033[K to avoid
|
| 746 |
+
# garbled escape codes when prompt_toolkit's patch_stdout is active.
|
| 747 |
+
blanks = ' ' * max(self.last_line_len + 5, 40)
|
| 748 |
+
self._write(f"\r{blanks}\r", end='', flush=True)
|
| 749 |
+
if final_message:
|
| 750 |
+
elapsed = f" ({time.time() - self.start_time:.1f}s)" if self.start_time else ""
|
| 751 |
+
if is_tty:
|
| 752 |
+
self._write(f" {final_message}", flush=True)
|
| 753 |
+
else:
|
| 754 |
+
self._write(f" [done] {final_message}{elapsed}", flush=True)
|
| 755 |
+
|
| 756 |
+
def __enter__(self):
|
| 757 |
+
self.start()
|
| 758 |
+
return self
|
| 759 |
+
|
| 760 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
| 761 |
+
self.stop()
|
| 762 |
+
return False
|
| 763 |
+
|
| 764 |
+
|
| 765 |
+
# =========================================================================
|
| 766 |
+
# Cute tool message (completion line that replaces the spinner)
|
| 767 |
+
# =========================================================================
|
| 768 |
+
|
| 769 |
+
def _detect_tool_failure(tool_name: str, result: str | None) -> tuple[bool, str]:
|
| 770 |
+
"""Inspect a tool result string for signs of failure.
|
| 771 |
+
|
| 772 |
+
Returns ``(is_failure, suffix)`` where *suffix* is an informational tag
|
| 773 |
+
like ``" [exit 1]"`` for terminal failures, or ``" [error]"`` for generic
|
| 774 |
+
failures. On success, returns ``(False, "")``.
|
| 775 |
+
"""
|
| 776 |
+
if result is None:
|
| 777 |
+
return False, ""
|
| 778 |
+
|
| 779 |
+
if tool_name == "terminal":
|
| 780 |
+
data = safe_json_loads(result)
|
| 781 |
+
if isinstance(data, dict):
|
| 782 |
+
exit_code = data.get("exit_code")
|
| 783 |
+
if exit_code is not None and exit_code != 0:
|
| 784 |
+
return True, f" [exit {exit_code}]"
|
| 785 |
+
return False, ""
|
| 786 |
+
|
| 787 |
+
# Memory-specific: distinguish "full" from real errors
|
| 788 |
+
if tool_name == "memory":
|
| 789 |
+
data = safe_json_loads(result)
|
| 790 |
+
if isinstance(data, dict):
|
| 791 |
+
if data.get("success") is False and "exceed the limit" in data.get("error", ""):
|
| 792 |
+
return True, " [full]"
|
| 793 |
+
|
| 794 |
+
# Generic heuristic for non-terminal tools
|
| 795 |
+
lower = result[:500].lower()
|
| 796 |
+
if '"error"' in lower or '"failed"' in lower or result.startswith("Error"):
|
| 797 |
+
return True, " [error]"
|
| 798 |
+
|
| 799 |
+
return False, ""
|
| 800 |
+
|
| 801 |
+
|
| 802 |
+
def get_cute_tool_message(
|
| 803 |
+
tool_name: str, args: dict, duration: float, result: str | None = None,
|
| 804 |
+
) -> str:
|
| 805 |
+
"""Generate a formatted tool completion line for CLI quiet mode.
|
| 806 |
+
|
| 807 |
+
Format: ``| {emoji} {verb:9} {detail} {duration}``
|
| 808 |
+
|
| 809 |
+
When *result* is provided the line is checked for failure indicators.
|
| 810 |
+
Failed tool calls get a red prefix and an informational suffix.
|
| 811 |
+
"""
|
| 812 |
+
dur = f"{duration:.1f}s"
|
| 813 |
+
is_failure, failure_suffix = _detect_tool_failure(tool_name, result)
|
| 814 |
+
skin_prefix = get_skin_tool_prefix()
|
| 815 |
+
|
| 816 |
+
def _trunc(s, n=40):
|
| 817 |
+
s = str(s)
|
| 818 |
+
if _tool_preview_max_len == 0:
|
| 819 |
+
return s # no limit
|
| 820 |
+
return (s[:n-3] + "...") if len(s) > n else s
|
| 821 |
+
|
| 822 |
+
def _path(p, n=35):
|
| 823 |
+
p = str(p)
|
| 824 |
+
if _tool_preview_max_len == 0:
|
| 825 |
+
return p # no limit
|
| 826 |
+
return ("..." + p[-(n-3):]) if len(p) > n else p
|
| 827 |
+
|
| 828 |
+
def _wrap(line: str) -> str:
|
| 829 |
+
"""Apply skin tool prefix and failure suffix."""
|
| 830 |
+
if skin_prefix != "┊":
|
| 831 |
+
line = line.replace("┊", skin_prefix, 1)
|
| 832 |
+
if not is_failure:
|
| 833 |
+
return line
|
| 834 |
+
return f"{line}{failure_suffix}"
|
| 835 |
+
|
| 836 |
+
if tool_name == "web_search":
|
| 837 |
+
return _wrap(f"┊ 🔍 search {_trunc(args.get('query', ''), 42)} {dur}")
|
| 838 |
+
if tool_name == "web_extract":
|
| 839 |
+
urls = args.get("urls", [])
|
| 840 |
+
if urls:
|
| 841 |
+
url = urls[0] if isinstance(urls, list) else str(urls)
|
| 842 |
+
domain = url.replace("https://", "").replace("http://", "").split("/")[0]
|
| 843 |
+
extra = f" +{len(urls)-1}" if len(urls) > 1 else ""
|
| 844 |
+
return _wrap(f"┊ 📄 fetch {_trunc(domain, 35)}{extra} {dur}")
|
| 845 |
+
return _wrap(f"┊ 📄 fetch pages {dur}")
|
| 846 |
+
if tool_name == "web_crawl":
|
| 847 |
+
url = args.get("url", "")
|
| 848 |
+
domain = url.replace("https://", "").replace("http://", "").split("/")[0]
|
| 849 |
+
return _wrap(f"┊ 🕸️ crawl {_trunc(domain, 35)} {dur}")
|
| 850 |
+
if tool_name == "terminal":
|
| 851 |
+
return _wrap(f"┊ 💻 $ {_trunc(args.get('command', ''), 42)} {dur}")
|
| 852 |
+
if tool_name == "process":
|
| 853 |
+
action = args.get("action", "?")
|
| 854 |
+
sid = args.get("session_id", "")[:12]
|
| 855 |
+
labels = {"list": "ls processes", "poll": f"poll {sid}", "log": f"log {sid}",
|
| 856 |
+
"wait": f"wait {sid}", "kill": f"kill {sid}", "write": f"write {sid}", "submit": f"submit {sid}"}
|
| 857 |
+
return _wrap(f"┊ ⚙️ proc {labels.get(action, f'{action} {sid}')} {dur}")
|
| 858 |
+
if tool_name == "read_file":
|
| 859 |
+
return _wrap(f"┊ 📖 read {_path(args.get('path', ''))} {dur}")
|
| 860 |
+
if tool_name == "write_file":
|
| 861 |
+
return _wrap(f"┊ ✍️ write {_path(args.get('path', ''))} {dur}")
|
| 862 |
+
if tool_name == "patch":
|
| 863 |
+
return _wrap(f"┊ 🔧 patch {_path(args.get('path', ''))} {dur}")
|
| 864 |
+
if tool_name == "search_files":
|
| 865 |
+
pattern = _trunc(args.get("pattern", ""), 35)
|
| 866 |
+
target = args.get("target", "content")
|
| 867 |
+
verb = "find" if target == "files" else "grep"
|
| 868 |
+
return _wrap(f"┊ 🔎 {verb:9} {pattern} {dur}")
|
| 869 |
+
if tool_name == "browser_navigate":
|
| 870 |
+
url = args.get("url", "")
|
| 871 |
+
domain = url.replace("https://", "").replace("http://", "").split("/")[0]
|
| 872 |
+
return _wrap(f"┊ 🌐 navigate {_trunc(domain, 35)} {dur}")
|
| 873 |
+
if tool_name == "browser_snapshot":
|
| 874 |
+
mode = "full" if args.get("full") else "compact"
|
| 875 |
+
return _wrap(f"┊ 📸 snapshot {mode} {dur}")
|
| 876 |
+
if tool_name == "browser_click":
|
| 877 |
+
return _wrap(f"┊ 👆 click {args.get('ref', '?')} {dur}")
|
| 878 |
+
if tool_name == "browser_type":
|
| 879 |
+
return _wrap(f"┊ ⌨️ type \"{_trunc(args.get('text', ''), 30)}\" {dur}")
|
| 880 |
+
if tool_name == "browser_scroll":
|
| 881 |
+
d = args.get("direction", "down")
|
| 882 |
+
arrow = {"down": "↓", "up": "↑", "right": "→", "left": "←"}.get(d, "↓")
|
| 883 |
+
return _wrap(f"┊ {arrow} scroll {d} {dur}")
|
| 884 |
+
if tool_name == "browser_back":
|
| 885 |
+
return _wrap(f"┊ ◀️ back {dur}")
|
| 886 |
+
if tool_name == "browser_press":
|
| 887 |
+
return _wrap(f"┊ ⌨️ press {args.get('key', '?')} {dur}")
|
| 888 |
+
if tool_name == "browser_get_images":
|
| 889 |
+
return _wrap(f"┊ 🖼️ images extracting {dur}")
|
| 890 |
+
if tool_name == "browser_vision":
|
| 891 |
+
return _wrap(f"┊ 👁️ vision analyzing page {dur}")
|
| 892 |
+
if tool_name == "todo":
|
| 893 |
+
todos_arg = args.get("todos")
|
| 894 |
+
merge = args.get("merge", False)
|
| 895 |
+
if todos_arg is None:
|
| 896 |
+
return _wrap(f"┊ 📋 plan reading tasks {dur}")
|
| 897 |
+
elif merge:
|
| 898 |
+
return _wrap(f"┊ 📋 plan update {len(todos_arg)} task(s) {dur}")
|
| 899 |
+
else:
|
| 900 |
+
return _wrap(f"┊ 📋 plan {len(todos_arg)} task(s) {dur}")
|
| 901 |
+
if tool_name == "session_search":
|
| 902 |
+
return _wrap(f"┊ 🔍 recall \"{_trunc(args.get('query', ''), 35)}\" {dur}")
|
| 903 |
+
if tool_name == "memory":
|
| 904 |
+
action = args.get("action", "?")
|
| 905 |
+
target = args.get("target", "")
|
| 906 |
+
if action == "add":
|
| 907 |
+
return _wrap(f"┊ 🧠 memory +{target}: \"{_trunc(args.get('content', ''), 30)}\" {dur}")
|
| 908 |
+
elif action == "replace":
|
| 909 |
+
return _wrap(f"┊ 🧠 memory ~{target}: \"{_trunc(args.get('old_text', ''), 20)}\" {dur}")
|
| 910 |
+
elif action == "remove":
|
| 911 |
+
return _wrap(f"┊ 🧠 memory -{target}: \"{_trunc(args.get('old_text', ''), 20)}\" {dur}")
|
| 912 |
+
return _wrap(f"┊ 🧠 memory {action} {dur}")
|
| 913 |
+
if tool_name == "skills_list":
|
| 914 |
+
return _wrap(f"┊ 📚 skills list {args.get('category', 'all')} {dur}")
|
| 915 |
+
if tool_name == "skill_view":
|
| 916 |
+
return _wrap(f"┊ 📚 skill {_trunc(args.get('name', ''), 30)} {dur}")
|
| 917 |
+
if tool_name == "image_generate":
|
| 918 |
+
return _wrap(f"┊ 🎨 create {_trunc(args.get('prompt', ''), 35)} {dur}")
|
| 919 |
+
if tool_name == "text_to_speech":
|
| 920 |
+
return _wrap(f"┊ 🔊 speak {_trunc(args.get('text', ''), 30)} {dur}")
|
| 921 |
+
if tool_name == "vision_analyze":
|
| 922 |
+
return _wrap(f"┊ 👁️ vision {_trunc(args.get('question', ''), 30)} {dur}")
|
| 923 |
+
if tool_name == "mixture_of_agents":
|
| 924 |
+
return _wrap(f"┊ 🧠 reason {_trunc(args.get('user_prompt', ''), 30)} {dur}")
|
| 925 |
+
if tool_name == "send_message":
|
| 926 |
+
return _wrap(f"┊ 📨 send {args.get('target', '?')}: \"{_trunc(args.get('message', ''), 25)}\" {dur}")
|
| 927 |
+
if tool_name == "cronjob":
|
| 928 |
+
action = args.get("action", "?")
|
| 929 |
+
if action == "create":
|
| 930 |
+
skills = args.get("skills") or ([] if not args.get("skill") else [args.get("skill")])
|
| 931 |
+
label = args.get("name") or (skills[0] if skills else None) or args.get("prompt", "task")
|
| 932 |
+
return _wrap(f"┊ ⏰ cron create {_trunc(label, 24)} {dur}")
|
| 933 |
+
if action == "list":
|
| 934 |
+
return _wrap(f"┊ ⏰ cron listing {dur}")
|
| 935 |
+
return _wrap(f"┊ ⏰ cron {action} {args.get('job_id', '')} {dur}")
|
| 936 |
+
if tool_name.startswith("rl_"):
|
| 937 |
+
rl = {
|
| 938 |
+
"rl_list_environments": "list envs", "rl_select_environment": f"select {args.get('name', '')}",
|
| 939 |
+
"rl_get_current_config": "get config", "rl_edit_config": f"set {args.get('field', '?')}",
|
| 940 |
+
"rl_start_training": "start training", "rl_check_status": f"status {args.get('run_id', '?')[:12]}",
|
| 941 |
+
"rl_stop_training": f"stop {args.get('run_id', '?')[:12]}", "rl_get_results": f"results {args.get('run_id', '?')[:12]}",
|
| 942 |
+
"rl_list_runs": "list runs", "rl_test_inference": "test inference",
|
| 943 |
+
}
|
| 944 |
+
return _wrap(f"┊ 🧪 rl {rl.get(tool_name, tool_name.replace('rl_', ''))} {dur}")
|
| 945 |
+
if tool_name == "execute_code":
|
| 946 |
+
code = args.get("code", "")
|
| 947 |
+
first_line = code.strip().split("\n")[0] if code.strip() else ""
|
| 948 |
+
return _wrap(f"┊ 🐍 exec {_trunc(first_line, 35)} {dur}")
|
| 949 |
+
if tool_name == "delegate_task":
|
| 950 |
+
tasks = args.get("tasks")
|
| 951 |
+
if tasks and isinstance(tasks, list):
|
| 952 |
+
return _wrap(f"┊ 🔀 delegate {len(tasks)} parallel tasks {dur}")
|
| 953 |
+
return _wrap(f"┊ 🔀 delegate {_trunc(args.get('goal', ''), 35)} {dur}")
|
| 954 |
+
|
| 955 |
+
preview = build_tool_preview(tool_name, args) or ""
|
| 956 |
+
return _wrap(f"┊ ⚡ {tool_name[:9]:9} {_trunc(preview, 35)} {dur}")
|
| 957 |
+
|
| 958 |
+
|
| 959 |
+
# =========================================================================
|
| 960 |
+
# Honcho session line (one-liner with clickable OSC 8 hyperlink)
|
| 961 |
+
# =========================================================================
|
| 962 |
+
|
| 963 |
+
_DIM = "\033[2m"
|
| 964 |
+
_SKY_BLUE = "\033[38;5;117m"
|
| 965 |
+
_ANSI_RESET = "\033[0m"
|
| 966 |
+
|
| 967 |
+
|
| 968 |
+
# =========================================================================
|
| 969 |
+
# Context pressure display (CLI user-facing warnings)
|
| 970 |
+
# =========================================================================
|
| 971 |
+
|
| 972 |
+
# ANSI color codes for context pressure tiers
|
| 973 |
+
_CYAN = "\033[36m"
|
| 974 |
+
_YELLOW = "\033[33m"
|
| 975 |
+
_BOLD = "\033[1m"
|
| 976 |
+
_DIM_ANSI = "\033[2m"
|
| 977 |
+
|
| 978 |
+
# Bar characters
|
| 979 |
+
_BAR_FILLED = "▰"
|
| 980 |
+
_BAR_EMPTY = "▱"
|
| 981 |
+
_BAR_WIDTH = 20
|
| 982 |
+
|
| 983 |
+
|
| 984 |
+
def format_context_pressure(
|
| 985 |
+
compaction_progress: float,
|
| 986 |
+
threshold_tokens: int,
|
| 987 |
+
threshold_percent: float,
|
| 988 |
+
compression_enabled: bool = True,
|
| 989 |
+
) -> str:
|
| 990 |
+
"""Build a formatted context pressure line for CLI display.
|
| 991 |
+
|
| 992 |
+
The bar and percentage show progress toward the compaction threshold,
|
| 993 |
+
NOT the raw context window. 100% = compaction fires.
|
| 994 |
+
|
| 995 |
+
Args:
|
| 996 |
+
compaction_progress: How close to compaction (0.0–1.0, 1.0 = fires).
|
| 997 |
+
threshold_tokens: Compaction threshold in tokens.
|
| 998 |
+
threshold_percent: Compaction threshold as a fraction of context window.
|
| 999 |
+
compression_enabled: Whether auto-compression is active.
|
| 1000 |
+
"""
|
| 1001 |
+
pct_int = min(int(compaction_progress * 100), 100)
|
| 1002 |
+
filled = min(int(compaction_progress * _BAR_WIDTH), _BAR_WIDTH)
|
| 1003 |
+
bar = _BAR_FILLED * filled + _BAR_EMPTY * (_BAR_WIDTH - filled)
|
| 1004 |
+
|
| 1005 |
+
threshold_k = f"{threshold_tokens // 1000}k" if threshold_tokens >= 1000 else str(threshold_tokens)
|
| 1006 |
+
threshold_pct_int = int(threshold_percent * 100)
|
| 1007 |
+
|
| 1008 |
+
color = f"{_BOLD}{_YELLOW}"
|
| 1009 |
+
icon = "⚠"
|
| 1010 |
+
if compression_enabled:
|
| 1011 |
+
hint = "compaction approaching"
|
| 1012 |
+
else:
|
| 1013 |
+
hint = "no auto-compaction"
|
| 1014 |
+
|
| 1015 |
+
return (
|
| 1016 |
+
f" {color}{icon} context {bar} {pct_int}% to compaction{_ANSI_RESET}"
|
| 1017 |
+
f" {_DIM_ANSI}{threshold_k} threshold ({threshold_pct_int}%) · {hint}{_ANSI_RESET}"
|
| 1018 |
+
)
|
| 1019 |
+
|
| 1020 |
+
|
| 1021 |
+
def format_context_pressure_gateway(
|
| 1022 |
+
compaction_progress: float,
|
| 1023 |
+
threshold_percent: float,
|
| 1024 |
+
compression_enabled: bool = True,
|
| 1025 |
+
) -> str:
|
| 1026 |
+
"""Build a plain-text context pressure notification for messaging platforms.
|
| 1027 |
+
|
| 1028 |
+
No ANSI — just Unicode and plain text suitable for Telegram/Discord/etc.
|
| 1029 |
+
The percentage shows progress toward the compaction threshold.
|
| 1030 |
+
"""
|
| 1031 |
+
pct_int = min(int(compaction_progress * 100), 100)
|
| 1032 |
+
filled = min(int(compaction_progress * _BAR_WIDTH), _BAR_WIDTH)
|
| 1033 |
+
bar = _BAR_FILLED * filled + _BAR_EMPTY * (_BAR_WIDTH - filled)
|
| 1034 |
+
|
| 1035 |
+
threshold_pct_int = int(threshold_percent * 100)
|
| 1036 |
+
|
| 1037 |
+
icon = "⚠️"
|
| 1038 |
+
if compression_enabled:
|
| 1039 |
+
hint = f"Context compaction approaching (threshold: {threshold_pct_int}% of window)."
|
| 1040 |
+
else:
|
| 1041 |
+
hint = "Auto-compaction is disabled — context may be truncated."
|
| 1042 |
+
|
| 1043 |
+
return f"{icon} Context: {bar} {pct_int}% to compaction\n{hint}"
|
agent/error_classifier.py
ADDED
|
@@ -0,0 +1,809 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""API error classification for smart failover and recovery.
|
| 2 |
+
|
| 3 |
+
Provides a structured taxonomy of API errors and a priority-ordered
|
| 4 |
+
classification pipeline that determines the correct recovery action
|
| 5 |
+
(retry, rotate credential, fallback to another provider, compress
|
| 6 |
+
context, or abort).
|
| 7 |
+
|
| 8 |
+
Replaces scattered inline string-matching with a centralized classifier
|
| 9 |
+
that the main retry loop in run_agent.py consults for every API failure.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
import enum
|
| 15 |
+
import logging
|
| 16 |
+
import re
|
| 17 |
+
from dataclasses import dataclass, field
|
| 18 |
+
from typing import Any, Dict, Optional
|
| 19 |
+
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# ── Error taxonomy ──────────────────────────────────────────────────────
|
| 24 |
+
|
| 25 |
+
class FailoverReason(enum.Enum):
|
| 26 |
+
"""Why an API call failed — determines recovery strategy."""
|
| 27 |
+
|
| 28 |
+
# Authentication / authorization
|
| 29 |
+
auth = "auth" # Transient auth (401/403) — refresh/rotate
|
| 30 |
+
auth_permanent = "auth_permanent" # Auth failed after refresh — abort
|
| 31 |
+
|
| 32 |
+
# Billing / quota
|
| 33 |
+
billing = "billing" # 402 or confirmed credit exhaustion — rotate immediately
|
| 34 |
+
rate_limit = "rate_limit" # 429 or quota-based throttling — backoff then rotate
|
| 35 |
+
|
| 36 |
+
# Server-side
|
| 37 |
+
overloaded = "overloaded" # 503/529 — provider overloaded, backoff
|
| 38 |
+
server_error = "server_error" # 500/502 — internal server error, retry
|
| 39 |
+
|
| 40 |
+
# Transport
|
| 41 |
+
timeout = "timeout" # Connection/read timeout — rebuild client + retry
|
| 42 |
+
|
| 43 |
+
# Context / payload
|
| 44 |
+
context_overflow = "context_overflow" # Context too large — compress, not failover
|
| 45 |
+
payload_too_large = "payload_too_large" # 413 — compress payload
|
| 46 |
+
|
| 47 |
+
# Model
|
| 48 |
+
model_not_found = "model_not_found" # 404 or invalid model — fallback to different model
|
| 49 |
+
|
| 50 |
+
# Request format
|
| 51 |
+
format_error = "format_error" # 400 bad request — abort or strip + retry
|
| 52 |
+
|
| 53 |
+
# Provider-specific
|
| 54 |
+
thinking_signature = "thinking_signature" # Anthropic thinking block sig invalid
|
| 55 |
+
long_context_tier = "long_context_tier" # Anthropic "extra usage" tier gate
|
| 56 |
+
|
| 57 |
+
# Catch-all
|
| 58 |
+
unknown = "unknown" # Unclassifiable — retry with backoff
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# ── Classification result ───────────────────────────────────────────────
|
| 62 |
+
|
| 63 |
+
@dataclass
|
| 64 |
+
class ClassifiedError:
|
| 65 |
+
"""Structured classification of an API error with recovery hints."""
|
| 66 |
+
|
| 67 |
+
reason: FailoverReason
|
| 68 |
+
status_code: Optional[int] = None
|
| 69 |
+
provider: Optional[str] = None
|
| 70 |
+
model: Optional[str] = None
|
| 71 |
+
message: str = ""
|
| 72 |
+
error_context: Dict[str, Any] = field(default_factory=dict)
|
| 73 |
+
|
| 74 |
+
# Recovery action hints — the retry loop checks these instead of
|
| 75 |
+
# re-classifying the error itself.
|
| 76 |
+
retryable: bool = True
|
| 77 |
+
should_compress: bool = False
|
| 78 |
+
should_rotate_credential: bool = False
|
| 79 |
+
should_fallback: bool = False
|
| 80 |
+
|
| 81 |
+
@property
|
| 82 |
+
def is_auth(self) -> bool:
|
| 83 |
+
return self.reason in (FailoverReason.auth, FailoverReason.auth_permanent)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# ── Provider-specific patterns ──────────────────────────────────────────
|
| 88 |
+
|
| 89 |
+
# Patterns that indicate billing exhaustion (not transient rate limit)
|
| 90 |
+
_BILLING_PATTERNS = [
|
| 91 |
+
"insufficient credits",
|
| 92 |
+
"insufficient_quota",
|
| 93 |
+
"credit balance",
|
| 94 |
+
"credits have been exhausted",
|
| 95 |
+
"top up your credits",
|
| 96 |
+
"payment required",
|
| 97 |
+
"billing hard limit",
|
| 98 |
+
"exceeded your current quota",
|
| 99 |
+
"account is deactivated",
|
| 100 |
+
"plan does not include",
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
# Patterns that indicate rate limiting (transient, will resolve)
|
| 104 |
+
_RATE_LIMIT_PATTERNS = [
|
| 105 |
+
"rate limit",
|
| 106 |
+
"rate_limit",
|
| 107 |
+
"too many requests",
|
| 108 |
+
"throttled",
|
| 109 |
+
"requests per minute",
|
| 110 |
+
"tokens per minute",
|
| 111 |
+
"requests per day",
|
| 112 |
+
"try again in",
|
| 113 |
+
"please retry after",
|
| 114 |
+
"resource_exhausted",
|
| 115 |
+
"rate increased too quickly", # Alibaba/DashScope throttling
|
| 116 |
+
]
|
| 117 |
+
|
| 118 |
+
# Usage-limit patterns that need disambiguation (could be billing OR rate_limit)
|
| 119 |
+
_USAGE_LIMIT_PATTERNS = [
|
| 120 |
+
"usage limit",
|
| 121 |
+
"quota",
|
| 122 |
+
"limit exceeded",
|
| 123 |
+
"key limit exceeded",
|
| 124 |
+
]
|
| 125 |
+
|
| 126 |
+
# Patterns confirming usage limit is transient (not billing)
|
| 127 |
+
_USAGE_LIMIT_TRANSIENT_SIGNALS = [
|
| 128 |
+
"try again",
|
| 129 |
+
"retry",
|
| 130 |
+
"resets at",
|
| 131 |
+
"reset in",
|
| 132 |
+
"wait",
|
| 133 |
+
"requests remaining",
|
| 134 |
+
"periodic",
|
| 135 |
+
"window",
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
# Payload-too-large patterns detected from message text (no status_code attr).
|
| 139 |
+
# Proxies and some backends embed the HTTP status in the error message.
|
| 140 |
+
_PAYLOAD_TOO_LARGE_PATTERNS = [
|
| 141 |
+
"request entity too large",
|
| 142 |
+
"payload too large",
|
| 143 |
+
"error code: 413",
|
| 144 |
+
]
|
| 145 |
+
|
| 146 |
+
# Context overflow patterns
|
| 147 |
+
_CONTEXT_OVERFLOW_PATTERNS = [
|
| 148 |
+
"context length",
|
| 149 |
+
"context size",
|
| 150 |
+
"maximum context",
|
| 151 |
+
"token limit",
|
| 152 |
+
"too many tokens",
|
| 153 |
+
"reduce the length",
|
| 154 |
+
"exceeds the limit",
|
| 155 |
+
"context window",
|
| 156 |
+
"prompt is too long",
|
| 157 |
+
"prompt exceeds max length",
|
| 158 |
+
"max_tokens",
|
| 159 |
+
"maximum number of tokens",
|
| 160 |
+
# Chinese error messages (some providers return these)
|
| 161 |
+
"超过最大长度",
|
| 162 |
+
"上下文长度",
|
| 163 |
+
]
|
| 164 |
+
|
| 165 |
+
# Model not found patterns
|
| 166 |
+
_MODEL_NOT_FOUND_PATTERNS = [
|
| 167 |
+
"is not a valid model",
|
| 168 |
+
"invalid model",
|
| 169 |
+
"model not found",
|
| 170 |
+
"model_not_found",
|
| 171 |
+
"does not exist",
|
| 172 |
+
"no such model",
|
| 173 |
+
"unknown model",
|
| 174 |
+
"unsupported model",
|
| 175 |
+
]
|
| 176 |
+
|
| 177 |
+
# Auth patterns (non-status-code signals)
|
| 178 |
+
_AUTH_PATTERNS = [
|
| 179 |
+
"invalid api key",
|
| 180 |
+
"invalid_api_key",
|
| 181 |
+
"authentication",
|
| 182 |
+
"unauthorized",
|
| 183 |
+
"forbidden",
|
| 184 |
+
"invalid token",
|
| 185 |
+
"token expired",
|
| 186 |
+
"token revoked",
|
| 187 |
+
"access denied",
|
| 188 |
+
]
|
| 189 |
+
|
| 190 |
+
# Anthropic thinking block signature patterns
|
| 191 |
+
_THINKING_SIG_PATTERNS = [
|
| 192 |
+
"signature", # Combined with "thinking" check
|
| 193 |
+
]
|
| 194 |
+
|
| 195 |
+
# Transport error type names
|
| 196 |
+
_TRANSPORT_ERROR_TYPES = frozenset({
|
| 197 |
+
"ReadTimeout", "ConnectTimeout", "PoolTimeout",
|
| 198 |
+
"ConnectError", "RemoteProtocolError",
|
| 199 |
+
"ConnectionError", "ConnectionResetError",
|
| 200 |
+
"ConnectionAbortedError", "BrokenPipeError",
|
| 201 |
+
"TimeoutError", "ReadError",
|
| 202 |
+
"ServerDisconnectedError",
|
| 203 |
+
# OpenAI SDK errors (not subclasses of Python builtins)
|
| 204 |
+
"APIConnectionError",
|
| 205 |
+
"APITimeoutError",
|
| 206 |
+
})
|
| 207 |
+
|
| 208 |
+
# Server disconnect patterns (no status code, but transport-level)
|
| 209 |
+
_SERVER_DISCONNECT_PATTERNS = [
|
| 210 |
+
"server disconnected",
|
| 211 |
+
"peer closed connection",
|
| 212 |
+
"connection reset by peer",
|
| 213 |
+
"connection was closed",
|
| 214 |
+
"network connection lost",
|
| 215 |
+
"unexpected eof",
|
| 216 |
+
"incomplete chunked read",
|
| 217 |
+
]
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
# ── Classification pipeline ─────────────────────────────────────────────
|
| 221 |
+
|
| 222 |
+
def classify_api_error(
|
| 223 |
+
error: Exception,
|
| 224 |
+
*,
|
| 225 |
+
provider: str = "",
|
| 226 |
+
model: str = "",
|
| 227 |
+
approx_tokens: int = 0,
|
| 228 |
+
context_length: int = 200000,
|
| 229 |
+
num_messages: int = 0,
|
| 230 |
+
) -> ClassifiedError:
|
| 231 |
+
"""Classify an API error into a structured recovery recommendation.
|
| 232 |
+
|
| 233 |
+
Priority-ordered pipeline:
|
| 234 |
+
1. Special-case provider-specific patterns (thinking sigs, tier gates)
|
| 235 |
+
2. HTTP status code + message-aware refinement
|
| 236 |
+
3. Error code classification (from body)
|
| 237 |
+
4. Message pattern matching (billing vs rate_limit vs context vs auth)
|
| 238 |
+
5. Transport error heuristics
|
| 239 |
+
6. Server disconnect + large session → context overflow
|
| 240 |
+
7. Fallback: unknown (retryable with backoff)
|
| 241 |
+
|
| 242 |
+
Args:
|
| 243 |
+
error: The exception from the API call.
|
| 244 |
+
provider: Current provider name (e.g. "openrouter", "anthropic").
|
| 245 |
+
model: Current model slug.
|
| 246 |
+
approx_tokens: Approximate token count of the current context.
|
| 247 |
+
context_length: Maximum context length for the current model.
|
| 248 |
+
|
| 249 |
+
Returns:
|
| 250 |
+
ClassifiedError with reason and recovery action hints.
|
| 251 |
+
"""
|
| 252 |
+
status_code = _extract_status_code(error)
|
| 253 |
+
error_type = type(error).__name__
|
| 254 |
+
body = _extract_error_body(error)
|
| 255 |
+
error_code = _extract_error_code(body)
|
| 256 |
+
|
| 257 |
+
# Build a comprehensive error message string for pattern matching.
|
| 258 |
+
# str(error) alone may not include the body message (e.g. OpenAI SDK's
|
| 259 |
+
# APIStatusError.__str__ returns the first arg, not the body). Append
|
| 260 |
+
# the body message so patterns like "try again" in 402 disambiguation
|
| 261 |
+
# are detected even when only present in the structured body.
|
| 262 |
+
#
|
| 263 |
+
# Also extract metadata.raw — OpenRouter wraps upstream provider errors
|
| 264 |
+
# inside {"error": {"message": "Provider returned error", "metadata":
|
| 265 |
+
# {"raw": "<actual error JSON>"}}} and the real error message (e.g.
|
| 266 |
+
# "context length exceeded") is only in the inner JSON.
|
| 267 |
+
_raw_msg = str(error).lower()
|
| 268 |
+
_body_msg = ""
|
| 269 |
+
_metadata_msg = ""
|
| 270 |
+
if isinstance(body, dict):
|
| 271 |
+
_err_obj = body.get("error", {})
|
| 272 |
+
if isinstance(_err_obj, dict):
|
| 273 |
+
_body_msg = (_err_obj.get("message") or "").lower()
|
| 274 |
+
# Parse metadata.raw for wrapped provider errors
|
| 275 |
+
_metadata = _err_obj.get("metadata", {})
|
| 276 |
+
if isinstance(_metadata, dict):
|
| 277 |
+
_raw_json = _metadata.get("raw") or ""
|
| 278 |
+
if isinstance(_raw_json, str) and _raw_json.strip():
|
| 279 |
+
try:
|
| 280 |
+
import json
|
| 281 |
+
_inner = json.loads(_raw_json)
|
| 282 |
+
if isinstance(_inner, dict):
|
| 283 |
+
_inner_err = _inner.get("error", {})
|
| 284 |
+
if isinstance(_inner_err, dict):
|
| 285 |
+
_metadata_msg = (_inner_err.get("message") or "").lower()
|
| 286 |
+
except (json.JSONDecodeError, TypeError):
|
| 287 |
+
pass
|
| 288 |
+
if not _body_msg:
|
| 289 |
+
_body_msg = (body.get("message") or "").lower()
|
| 290 |
+
# Combine all message sources for pattern matching
|
| 291 |
+
parts = [_raw_msg]
|
| 292 |
+
if _body_msg and _body_msg not in _raw_msg:
|
| 293 |
+
parts.append(_body_msg)
|
| 294 |
+
if _metadata_msg and _metadata_msg not in _raw_msg and _metadata_msg not in _body_msg:
|
| 295 |
+
parts.append(_metadata_msg)
|
| 296 |
+
error_msg = " ".join(parts)
|
| 297 |
+
provider_lower = (provider or "").strip().lower()
|
| 298 |
+
model_lower = (model or "").strip().lower()
|
| 299 |
+
|
| 300 |
+
def _result(reason: FailoverReason, **overrides) -> ClassifiedError:
|
| 301 |
+
defaults = {
|
| 302 |
+
"reason": reason,
|
| 303 |
+
"status_code": status_code,
|
| 304 |
+
"provider": provider,
|
| 305 |
+
"model": model,
|
| 306 |
+
"message": _extract_message(error, body),
|
| 307 |
+
}
|
| 308 |
+
defaults.update(overrides)
|
| 309 |
+
return ClassifiedError(**defaults)
|
| 310 |
+
|
| 311 |
+
# ── 1. Provider-specific patterns (highest priority) ────────────
|
| 312 |
+
|
| 313 |
+
# Anthropic thinking block signature invalid (400).
|
| 314 |
+
# Don't gate on provider — OpenRouter proxies Anthropic errors, so the
|
| 315 |
+
# provider may be "openrouter" even though the error is Anthropic-specific.
|
| 316 |
+
# The message pattern ("signature" + "thinking") is unique enough.
|
| 317 |
+
if (
|
| 318 |
+
status_code == 400
|
| 319 |
+
and "signature" in error_msg
|
| 320 |
+
and "thinking" in error_msg
|
| 321 |
+
):
|
| 322 |
+
return _result(
|
| 323 |
+
FailoverReason.thinking_signature,
|
| 324 |
+
retryable=True,
|
| 325 |
+
should_compress=False,
|
| 326 |
+
)
|
| 327 |
+
|
| 328 |
+
# Anthropic long-context tier gate (429 "extra usage" + "long context")
|
| 329 |
+
if (
|
| 330 |
+
status_code == 429
|
| 331 |
+
and "extra usage" in error_msg
|
| 332 |
+
and "long context" in error_msg
|
| 333 |
+
):
|
| 334 |
+
return _result(
|
| 335 |
+
FailoverReason.long_context_tier,
|
| 336 |
+
retryable=True,
|
| 337 |
+
should_compress=True,
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
# ── 2. HTTP status code classification ──────────────────────────
|
| 341 |
+
|
| 342 |
+
if status_code is not None:
|
| 343 |
+
classified = _classify_by_status(
|
| 344 |
+
status_code, error_msg, error_code, body,
|
| 345 |
+
provider=provider_lower, model=model_lower,
|
| 346 |
+
approx_tokens=approx_tokens, context_length=context_length,
|
| 347 |
+
num_messages=num_messages,
|
| 348 |
+
result_fn=_result,
|
| 349 |
+
)
|
| 350 |
+
if classified is not None:
|
| 351 |
+
return classified
|
| 352 |
+
|
| 353 |
+
# ── 3. Error code classification ────────────────────────────────
|
| 354 |
+
|
| 355 |
+
if error_code:
|
| 356 |
+
classified = _classify_by_error_code(error_code, error_msg, _result)
|
| 357 |
+
if classified is not None:
|
| 358 |
+
return classified
|
| 359 |
+
|
| 360 |
+
# ── 4. Message pattern matching (no status code) ────────────────
|
| 361 |
+
|
| 362 |
+
classified = _classify_by_message(
|
| 363 |
+
error_msg, error_type,
|
| 364 |
+
approx_tokens=approx_tokens,
|
| 365 |
+
context_length=context_length,
|
| 366 |
+
result_fn=_result,
|
| 367 |
+
)
|
| 368 |
+
if classified is not None:
|
| 369 |
+
return classified
|
| 370 |
+
|
| 371 |
+
# ── 5. Server disconnect + large session → context overflow ─────
|
| 372 |
+
# Must come BEFORE generic transport error catch — a disconnect on
|
| 373 |
+
# a large session is more likely context overflow than a transient
|
| 374 |
+
# transport hiccup. Without this ordering, RemoteProtocolError
|
| 375 |
+
# always maps to timeout regardless of session size.
|
| 376 |
+
|
| 377 |
+
is_disconnect = any(p in error_msg for p in _SERVER_DISCONNECT_PATTERNS)
|
| 378 |
+
if is_disconnect and not status_code:
|
| 379 |
+
is_large = approx_tokens > context_length * 0.6 or approx_tokens > 120000 or num_messages > 200
|
| 380 |
+
if is_large:
|
| 381 |
+
return _result(
|
| 382 |
+
FailoverReason.context_overflow,
|
| 383 |
+
retryable=True,
|
| 384 |
+
should_compress=True,
|
| 385 |
+
)
|
| 386 |
+
return _result(FailoverReason.timeout, retryable=True)
|
| 387 |
+
|
| 388 |
+
# ── 6. Transport / timeout heuristics ───────────────────────────
|
| 389 |
+
|
| 390 |
+
if error_type in _TRANSPORT_ERROR_TYPES or isinstance(error, (TimeoutError, ConnectionError, OSError)):
|
| 391 |
+
return _result(FailoverReason.timeout, retryable=True)
|
| 392 |
+
|
| 393 |
+
# ── 7. Fallback: unknown ────────────────────────────────────────
|
| 394 |
+
|
| 395 |
+
return _result(FailoverReason.unknown, retryable=True)
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
# ── Status code classification ──────────────────────────────────────────
|
| 399 |
+
|
| 400 |
+
def _classify_by_status(
|
| 401 |
+
status_code: int,
|
| 402 |
+
error_msg: str,
|
| 403 |
+
error_code: str,
|
| 404 |
+
body: dict,
|
| 405 |
+
*,
|
| 406 |
+
provider: str,
|
| 407 |
+
model: str,
|
| 408 |
+
approx_tokens: int,
|
| 409 |
+
context_length: int,
|
| 410 |
+
num_messages: int = 0,
|
| 411 |
+
result_fn,
|
| 412 |
+
) -> Optional[ClassifiedError]:
|
| 413 |
+
"""Classify based on HTTP status code with message-aware refinement."""
|
| 414 |
+
|
| 415 |
+
if status_code == 401:
|
| 416 |
+
# Not retryable on its own — credential pool rotation and
|
| 417 |
+
# provider-specific refresh (Codex, Anthropic, Nous) run before
|
| 418 |
+
# the retryability check in run_agent.py. If those succeed, the
|
| 419 |
+
# loop `continue`s. If they fail, retryable=False ensures we
|
| 420 |
+
# hit the client-error abort path (which tries fallback first).
|
| 421 |
+
return result_fn(
|
| 422 |
+
FailoverReason.auth,
|
| 423 |
+
retryable=False,
|
| 424 |
+
should_rotate_credential=True,
|
| 425 |
+
should_fallback=True,
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
if status_code == 403:
|
| 429 |
+
# OpenRouter 403 "key limit exceeded" is actually billing
|
| 430 |
+
if "key limit exceeded" in error_msg or "spending limit" in error_msg:
|
| 431 |
+
return result_fn(
|
| 432 |
+
FailoverReason.billing,
|
| 433 |
+
retryable=False,
|
| 434 |
+
should_rotate_credential=True,
|
| 435 |
+
should_fallback=True,
|
| 436 |
+
)
|
| 437 |
+
return result_fn(
|
| 438 |
+
FailoverReason.auth,
|
| 439 |
+
retryable=False,
|
| 440 |
+
should_fallback=True,
|
| 441 |
+
)
|
| 442 |
+
|
| 443 |
+
if status_code == 402:
|
| 444 |
+
return _classify_402(error_msg, result_fn)
|
| 445 |
+
|
| 446 |
+
if status_code == 404:
|
| 447 |
+
if any(p in error_msg for p in _MODEL_NOT_FOUND_PATTERNS):
|
| 448 |
+
return result_fn(
|
| 449 |
+
FailoverReason.model_not_found,
|
| 450 |
+
retryable=False,
|
| 451 |
+
should_fallback=True,
|
| 452 |
+
)
|
| 453 |
+
# Generic 404 — could be model or endpoint
|
| 454 |
+
return result_fn(
|
| 455 |
+
FailoverReason.model_not_found,
|
| 456 |
+
retryable=False,
|
| 457 |
+
should_fallback=True,
|
| 458 |
+
)
|
| 459 |
+
|
| 460 |
+
if status_code == 413:
|
| 461 |
+
return result_fn(
|
| 462 |
+
FailoverReason.payload_too_large,
|
| 463 |
+
retryable=True,
|
| 464 |
+
should_compress=True,
|
| 465 |
+
)
|
| 466 |
+
|
| 467 |
+
if status_code == 429:
|
| 468 |
+
# Already checked long_context_tier above; this is a normal rate limit
|
| 469 |
+
return result_fn(
|
| 470 |
+
FailoverReason.rate_limit,
|
| 471 |
+
retryable=True,
|
| 472 |
+
should_rotate_credential=True,
|
| 473 |
+
should_fallback=True,
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
+
if status_code == 400:
|
| 477 |
+
return _classify_400(
|
| 478 |
+
error_msg, error_code, body,
|
| 479 |
+
provider=provider, model=model,
|
| 480 |
+
approx_tokens=approx_tokens,
|
| 481 |
+
context_length=context_length,
|
| 482 |
+
num_messages=num_messages,
|
| 483 |
+
result_fn=result_fn,
|
| 484 |
+
)
|
| 485 |
+
|
| 486 |
+
if status_code in (500, 502):
|
| 487 |
+
return result_fn(FailoverReason.server_error, retryable=True)
|
| 488 |
+
|
| 489 |
+
if status_code in (503, 529):
|
| 490 |
+
return result_fn(FailoverReason.overloaded, retryable=True)
|
| 491 |
+
|
| 492 |
+
# Other 4xx — non-retryable
|
| 493 |
+
if 400 <= status_code < 500:
|
| 494 |
+
return result_fn(
|
| 495 |
+
FailoverReason.format_error,
|
| 496 |
+
retryable=False,
|
| 497 |
+
should_fallback=True,
|
| 498 |
+
)
|
| 499 |
+
|
| 500 |
+
# Other 5xx — retryable
|
| 501 |
+
if 500 <= status_code < 600:
|
| 502 |
+
return result_fn(FailoverReason.server_error, retryable=True)
|
| 503 |
+
|
| 504 |
+
return None
|
| 505 |
+
|
| 506 |
+
|
| 507 |
+
def _classify_402(error_msg: str, result_fn) -> ClassifiedError:
|
| 508 |
+
"""Disambiguate 402: billing exhaustion vs transient usage limit.
|
| 509 |
+
|
| 510 |
+
The key insight from OpenClaw: some 402s are transient rate limits
|
| 511 |
+
disguised as payment errors. "Usage limit, try again in 5 minutes"
|
| 512 |
+
is NOT a billing problem — it's a periodic quota that resets.
|
| 513 |
+
"""
|
| 514 |
+
# Check for transient usage-limit signals first
|
| 515 |
+
has_usage_limit = any(p in error_msg for p in _USAGE_LIMIT_PATTERNS)
|
| 516 |
+
has_transient_signal = any(p in error_msg for p in _USAGE_LIMIT_TRANSIENT_SIGNALS)
|
| 517 |
+
|
| 518 |
+
if has_usage_limit and has_transient_signal:
|
| 519 |
+
# Transient quota — treat as rate limit, not billing
|
| 520 |
+
return result_fn(
|
| 521 |
+
FailoverReason.rate_limit,
|
| 522 |
+
retryable=True,
|
| 523 |
+
should_rotate_credential=True,
|
| 524 |
+
should_fallback=True,
|
| 525 |
+
)
|
| 526 |
+
|
| 527 |
+
# Confirmed billing exhaustion
|
| 528 |
+
return result_fn(
|
| 529 |
+
FailoverReason.billing,
|
| 530 |
+
retryable=False,
|
| 531 |
+
should_rotate_credential=True,
|
| 532 |
+
should_fallback=True,
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
def _classify_400(
|
| 537 |
+
error_msg: str,
|
| 538 |
+
error_code: str,
|
| 539 |
+
body: dict,
|
| 540 |
+
*,
|
| 541 |
+
provider: str,
|
| 542 |
+
model: str,
|
| 543 |
+
approx_tokens: int,
|
| 544 |
+
context_length: int,
|
| 545 |
+
num_messages: int = 0,
|
| 546 |
+
result_fn,
|
| 547 |
+
) -> ClassifiedError:
|
| 548 |
+
"""Classify 400 Bad Request — context overflow, format error, or generic."""
|
| 549 |
+
|
| 550 |
+
# Context overflow from 400
|
| 551 |
+
if any(p in error_msg for p in _CONTEXT_OVERFLOW_PATTERNS):
|
| 552 |
+
return result_fn(
|
| 553 |
+
FailoverReason.context_overflow,
|
| 554 |
+
retryable=True,
|
| 555 |
+
should_compress=True,
|
| 556 |
+
)
|
| 557 |
+
|
| 558 |
+
# Some providers return model-not-found as 400 instead of 404 (e.g. OpenRouter).
|
| 559 |
+
if any(p in error_msg for p in _MODEL_NOT_FOUND_PATTERNS):
|
| 560 |
+
return result_fn(
|
| 561 |
+
FailoverReason.model_not_found,
|
| 562 |
+
retryable=False,
|
| 563 |
+
should_fallback=True,
|
| 564 |
+
)
|
| 565 |
+
|
| 566 |
+
# Some providers return rate limit / billing errors as 400 instead of 429/402.
|
| 567 |
+
# Check these patterns before falling through to format_error.
|
| 568 |
+
if any(p in error_msg for p in _RATE_LIMIT_PATTERNS):
|
| 569 |
+
return result_fn(
|
| 570 |
+
FailoverReason.rate_limit,
|
| 571 |
+
retryable=True,
|
| 572 |
+
should_rotate_credential=True,
|
| 573 |
+
should_fallback=True,
|
| 574 |
+
)
|
| 575 |
+
if any(p in error_msg for p in _BILLING_PATTERNS):
|
| 576 |
+
return result_fn(
|
| 577 |
+
FailoverReason.billing,
|
| 578 |
+
retryable=False,
|
| 579 |
+
should_rotate_credential=True,
|
| 580 |
+
should_fallback=True,
|
| 581 |
+
)
|
| 582 |
+
|
| 583 |
+
# Generic 400 + large session → probable context overflow
|
| 584 |
+
# Anthropic sometimes returns a bare "Error" message when context is too large
|
| 585 |
+
err_body_msg = ""
|
| 586 |
+
if isinstance(body, dict):
|
| 587 |
+
err_obj = body.get("error", {})
|
| 588 |
+
if isinstance(err_obj, dict):
|
| 589 |
+
err_body_msg = (err_obj.get("message") or "").strip().lower()
|
| 590 |
+
# Responses API (and some providers) use flat body: {"message": "..."}
|
| 591 |
+
if not err_body_msg:
|
| 592 |
+
err_body_msg = (body.get("message") or "").strip().lower()
|
| 593 |
+
is_generic = len(err_body_msg) < 30 or err_body_msg in ("error", "")
|
| 594 |
+
is_large = approx_tokens > context_length * 0.4 or approx_tokens > 80000 or num_messages > 80
|
| 595 |
+
|
| 596 |
+
if is_generic and is_large:
|
| 597 |
+
return result_fn(
|
| 598 |
+
FailoverReason.context_overflow,
|
| 599 |
+
retryable=True,
|
| 600 |
+
should_compress=True,
|
| 601 |
+
)
|
| 602 |
+
|
| 603 |
+
# Non-retryable format error
|
| 604 |
+
return result_fn(
|
| 605 |
+
FailoverReason.format_error,
|
| 606 |
+
retryable=False,
|
| 607 |
+
should_fallback=True,
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
|
| 611 |
+
# ── Error code classification ───────────────────────────────────────────
|
| 612 |
+
|
| 613 |
+
def _classify_by_error_code(
|
| 614 |
+
error_code: str, error_msg: str, result_fn,
|
| 615 |
+
) -> Optional[ClassifiedError]:
|
| 616 |
+
"""Classify by structured error codes from the response body."""
|
| 617 |
+
code_lower = error_code.lower()
|
| 618 |
+
|
| 619 |
+
if code_lower in ("resource_exhausted", "throttled", "rate_limit_exceeded"):
|
| 620 |
+
return result_fn(
|
| 621 |
+
FailoverReason.rate_limit,
|
| 622 |
+
retryable=True,
|
| 623 |
+
should_rotate_credential=True,
|
| 624 |
+
)
|
| 625 |
+
|
| 626 |
+
if code_lower in ("insufficient_quota", "billing_not_active", "payment_required"):
|
| 627 |
+
return result_fn(
|
| 628 |
+
FailoverReason.billing,
|
| 629 |
+
retryable=False,
|
| 630 |
+
should_rotate_credential=True,
|
| 631 |
+
should_fallback=True,
|
| 632 |
+
)
|
| 633 |
+
|
| 634 |
+
if code_lower in ("model_not_found", "model_not_available", "invalid_model"):
|
| 635 |
+
return result_fn(
|
| 636 |
+
FailoverReason.model_not_found,
|
| 637 |
+
retryable=False,
|
| 638 |
+
should_fallback=True,
|
| 639 |
+
)
|
| 640 |
+
|
| 641 |
+
if code_lower in ("context_length_exceeded", "max_tokens_exceeded"):
|
| 642 |
+
return result_fn(
|
| 643 |
+
FailoverReason.context_overflow,
|
| 644 |
+
retryable=True,
|
| 645 |
+
should_compress=True,
|
| 646 |
+
)
|
| 647 |
+
|
| 648 |
+
return None
|
| 649 |
+
|
| 650 |
+
|
| 651 |
+
# ── Message pattern classification ──────────────────────────────────────
|
| 652 |
+
|
| 653 |
+
def _classify_by_message(
|
| 654 |
+
error_msg: str,
|
| 655 |
+
error_type: str,
|
| 656 |
+
*,
|
| 657 |
+
approx_tokens: int,
|
| 658 |
+
context_length: int,
|
| 659 |
+
result_fn,
|
| 660 |
+
) -> Optional[ClassifiedError]:
|
| 661 |
+
"""Classify based on error message patterns when no status code is available."""
|
| 662 |
+
|
| 663 |
+
# Payload-too-large patterns (from message text when no status_code)
|
| 664 |
+
if any(p in error_msg for p in _PAYLOAD_TOO_LARGE_PATTERNS):
|
| 665 |
+
return result_fn(
|
| 666 |
+
FailoverReason.payload_too_large,
|
| 667 |
+
retryable=True,
|
| 668 |
+
should_compress=True,
|
| 669 |
+
)
|
| 670 |
+
|
| 671 |
+
# Usage-limit patterns need the same disambiguation as 402: some providers
|
| 672 |
+
# surface "usage limit" errors without an HTTP status code. A transient
|
| 673 |
+
# signal ("try again", "resets at", …) means it's a periodic quota, not
|
| 674 |
+
# billing exhaustion.
|
| 675 |
+
has_usage_limit = any(p in error_msg for p in _USAGE_LIMIT_PATTERNS)
|
| 676 |
+
if has_usage_limit:
|
| 677 |
+
has_transient_signal = any(p in error_msg for p in _USAGE_LIMIT_TRANSIENT_SIGNALS)
|
| 678 |
+
if has_transient_signal:
|
| 679 |
+
return result_fn(
|
| 680 |
+
FailoverReason.rate_limit,
|
| 681 |
+
retryable=True,
|
| 682 |
+
should_rotate_credential=True,
|
| 683 |
+
should_fallback=True,
|
| 684 |
+
)
|
| 685 |
+
return result_fn(
|
| 686 |
+
FailoverReason.billing,
|
| 687 |
+
retryable=False,
|
| 688 |
+
should_rotate_credential=True,
|
| 689 |
+
should_fallback=True,
|
| 690 |
+
)
|
| 691 |
+
|
| 692 |
+
# Billing patterns
|
| 693 |
+
if any(p in error_msg for p in _BILLING_PATTERNS):
|
| 694 |
+
return result_fn(
|
| 695 |
+
FailoverReason.billing,
|
| 696 |
+
retryable=False,
|
| 697 |
+
should_rotate_credential=True,
|
| 698 |
+
should_fallback=True,
|
| 699 |
+
)
|
| 700 |
+
|
| 701 |
+
# Rate limit patterns
|
| 702 |
+
if any(p in error_msg for p in _RATE_LIMIT_PATTERNS):
|
| 703 |
+
return result_fn(
|
| 704 |
+
FailoverReason.rate_limit,
|
| 705 |
+
retryable=True,
|
| 706 |
+
should_rotate_credential=True,
|
| 707 |
+
should_fallback=True,
|
| 708 |
+
)
|
| 709 |
+
|
| 710 |
+
# Context overflow patterns
|
| 711 |
+
if any(p in error_msg for p in _CONTEXT_OVERFLOW_PATTERNS):
|
| 712 |
+
return result_fn(
|
| 713 |
+
FailoverReason.context_overflow,
|
| 714 |
+
retryable=True,
|
| 715 |
+
should_compress=True,
|
| 716 |
+
)
|
| 717 |
+
|
| 718 |
+
# Auth patterns
|
| 719 |
+
# Auth errors should NOT be retried directly — the credential is invalid and
|
| 720 |
+
# retrying with the same key will always fail. Set retryable=False so the
|
| 721 |
+
# caller triggers credential rotation (should_rotate_credential=True) or
|
| 722 |
+
# provider fallback rather than an immediate retry loop.
|
| 723 |
+
if any(p in error_msg for p in _AUTH_PATTERNS):
|
| 724 |
+
return result_fn(
|
| 725 |
+
FailoverReason.auth,
|
| 726 |
+
retryable=False,
|
| 727 |
+
should_rotate_credential=True,
|
| 728 |
+
should_fallback=True,
|
| 729 |
+
)
|
| 730 |
+
|
| 731 |
+
# Model not found patterns
|
| 732 |
+
if any(p in error_msg for p in _MODEL_NOT_FOUND_PATTERNS):
|
| 733 |
+
return result_fn(
|
| 734 |
+
FailoverReason.model_not_found,
|
| 735 |
+
retryable=False,
|
| 736 |
+
should_fallback=True,
|
| 737 |
+
)
|
| 738 |
+
|
| 739 |
+
return None
|
| 740 |
+
|
| 741 |
+
|
| 742 |
+
# ── Helpers ─────────────────────────────────────────────────────────────
|
| 743 |
+
|
| 744 |
+
def _extract_status_code(error: Exception) -> Optional[int]:
|
| 745 |
+
"""Walk the error and its cause chain to find an HTTP status code."""
|
| 746 |
+
current = error
|
| 747 |
+
for _ in range(5): # Max depth to prevent infinite loops
|
| 748 |
+
code = getattr(current, "status_code", None)
|
| 749 |
+
if isinstance(code, int):
|
| 750 |
+
return code
|
| 751 |
+
# Some SDKs use .status instead of .status_code
|
| 752 |
+
code = getattr(current, "status", None)
|
| 753 |
+
if isinstance(code, int) and 100 <= code < 600:
|
| 754 |
+
return code
|
| 755 |
+
# Walk cause chain
|
| 756 |
+
cause = getattr(current, "__cause__", None) or getattr(current, "__context__", None)
|
| 757 |
+
if cause is None or cause is current:
|
| 758 |
+
break
|
| 759 |
+
current = cause
|
| 760 |
+
return None
|
| 761 |
+
|
| 762 |
+
|
| 763 |
+
def _extract_error_body(error: Exception) -> dict:
|
| 764 |
+
"""Extract the structured error body from an SDK exception."""
|
| 765 |
+
body = getattr(error, "body", None)
|
| 766 |
+
if isinstance(body, dict):
|
| 767 |
+
return body
|
| 768 |
+
# Some errors have .response.json()
|
| 769 |
+
response = getattr(error, "response", None)
|
| 770 |
+
if response is not None:
|
| 771 |
+
try:
|
| 772 |
+
json_body = response.json()
|
| 773 |
+
if isinstance(json_body, dict):
|
| 774 |
+
return json_body
|
| 775 |
+
except Exception:
|
| 776 |
+
pass
|
| 777 |
+
return {}
|
| 778 |
+
|
| 779 |
+
|
| 780 |
+
def _extract_error_code(body: dict) -> str:
|
| 781 |
+
"""Extract an error code string from the response body."""
|
| 782 |
+
if not body:
|
| 783 |
+
return ""
|
| 784 |
+
error_obj = body.get("error", {})
|
| 785 |
+
if isinstance(error_obj, dict):
|
| 786 |
+
code = error_obj.get("code") or error_obj.get("type") or ""
|
| 787 |
+
if isinstance(code, str) and code.strip():
|
| 788 |
+
return code.strip()
|
| 789 |
+
# Top-level code
|
| 790 |
+
code = body.get("code") or body.get("error_code") or ""
|
| 791 |
+
if isinstance(code, (str, int)):
|
| 792 |
+
return str(code).strip()
|
| 793 |
+
return ""
|
| 794 |
+
|
| 795 |
+
|
| 796 |
+
def _extract_message(error: Exception, body: dict) -> str:
|
| 797 |
+
"""Extract the most informative error message."""
|
| 798 |
+
# Try structured body first
|
| 799 |
+
if body:
|
| 800 |
+
error_obj = body.get("error", {})
|
| 801 |
+
if isinstance(error_obj, dict):
|
| 802 |
+
msg = error_obj.get("message", "")
|
| 803 |
+
if isinstance(msg, str) and msg.strip():
|
| 804 |
+
return msg.strip()[:500]
|
| 805 |
+
msg = body.get("message", "")
|
| 806 |
+
if isinstance(msg, str) and msg.strip():
|
| 807 |
+
return msg.strip()[:500]
|
| 808 |
+
# Fallback to str(error)
|
| 809 |
+
return str(error)[:500]
|
agent/insights.py
ADDED
|
@@ -0,0 +1,790 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Session Insights Engine for Hermes Agent.
|
| 3 |
+
|
| 4 |
+
Analyzes historical session data from the SQLite state database to produce
|
| 5 |
+
comprehensive usage insights — token consumption, cost estimates, tool usage
|
| 6 |
+
patterns, activity trends, model/platform breakdowns, and session metrics.
|
| 7 |
+
|
| 8 |
+
Inspired by Claude Code's /insights command, adapted for Hermes Agent's
|
| 9 |
+
multi-platform architecture with additional cost estimation and platform
|
| 10 |
+
breakdown capabilities.
|
| 11 |
+
|
| 12 |
+
Usage:
|
| 13 |
+
from agent.insights import InsightsEngine
|
| 14 |
+
engine = InsightsEngine(db)
|
| 15 |
+
report = engine.generate(days=30)
|
| 16 |
+
print(engine.format_terminal(report))
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
import json
|
| 20 |
+
import time
|
| 21 |
+
from collections import Counter, defaultdict
|
| 22 |
+
from datetime import datetime
|
| 23 |
+
from typing import Any, Dict, List
|
| 24 |
+
|
| 25 |
+
from agent.usage_pricing import (
|
| 26 |
+
CanonicalUsage,
|
| 27 |
+
DEFAULT_PRICING,
|
| 28 |
+
estimate_usage_cost,
|
| 29 |
+
format_duration_compact,
|
| 30 |
+
get_pricing,
|
| 31 |
+
has_known_pricing,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
_DEFAULT_PRICING = DEFAULT_PRICING
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _has_known_pricing(model_name: str, provider: str = None, base_url: str = None) -> bool:
|
| 38 |
+
"""Check if a model has known pricing (vs unknown/custom endpoint)."""
|
| 39 |
+
return has_known_pricing(model_name, provider=provider, base_url=base_url)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _estimate_cost(
|
| 43 |
+
session_or_model: Dict[str, Any] | str,
|
| 44 |
+
input_tokens: int = 0,
|
| 45 |
+
output_tokens: int = 0,
|
| 46 |
+
*,
|
| 47 |
+
cache_read_tokens: int = 0,
|
| 48 |
+
cache_write_tokens: int = 0,
|
| 49 |
+
provider: str = None,
|
| 50 |
+
base_url: str = None,
|
| 51 |
+
) -> tuple[float, str]:
|
| 52 |
+
"""Estimate the USD cost for a session row or a model/token tuple."""
|
| 53 |
+
if isinstance(session_or_model, dict):
|
| 54 |
+
session = session_or_model
|
| 55 |
+
model = session.get("model") or ""
|
| 56 |
+
usage = CanonicalUsage(
|
| 57 |
+
input_tokens=session.get("input_tokens") or 0,
|
| 58 |
+
output_tokens=session.get("output_tokens") or 0,
|
| 59 |
+
cache_read_tokens=session.get("cache_read_tokens") or 0,
|
| 60 |
+
cache_write_tokens=session.get("cache_write_tokens") or 0,
|
| 61 |
+
)
|
| 62 |
+
provider = session.get("billing_provider")
|
| 63 |
+
base_url = session.get("billing_base_url")
|
| 64 |
+
else:
|
| 65 |
+
model = session_or_model or ""
|
| 66 |
+
usage = CanonicalUsage(
|
| 67 |
+
input_tokens=input_tokens,
|
| 68 |
+
output_tokens=output_tokens,
|
| 69 |
+
cache_read_tokens=cache_read_tokens,
|
| 70 |
+
cache_write_tokens=cache_write_tokens,
|
| 71 |
+
)
|
| 72 |
+
result = estimate_usage_cost(
|
| 73 |
+
model,
|
| 74 |
+
usage,
|
| 75 |
+
provider=provider,
|
| 76 |
+
base_url=base_url,
|
| 77 |
+
)
|
| 78 |
+
return float(result.amount_usd or 0.0), result.status
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def _format_duration(seconds: float) -> str:
|
| 82 |
+
"""Format seconds into a human-readable duration string."""
|
| 83 |
+
return format_duration_compact(seconds)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def _bar_chart(values: List[int], max_width: int = 20) -> List[str]:
|
| 87 |
+
"""Create simple horizontal bar chart strings from values."""
|
| 88 |
+
peak = max(values) if values else 1
|
| 89 |
+
if peak == 0:
|
| 90 |
+
return ["" for _ in values]
|
| 91 |
+
return ["█" * max(1, int(v / peak * max_width)) if v > 0 else "" for v in values]
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
class InsightsEngine:
|
| 95 |
+
"""
|
| 96 |
+
Analyzes session history and produces usage insights.
|
| 97 |
+
|
| 98 |
+
Works directly with a SessionDB instance (or raw sqlite3 connection)
|
| 99 |
+
to query session and message data.
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
def __init__(self, db):
|
| 103 |
+
"""
|
| 104 |
+
Initialize with a SessionDB instance.
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
db: A SessionDB instance (from hermes_state.py)
|
| 108 |
+
"""
|
| 109 |
+
self.db = db
|
| 110 |
+
self._conn = db._conn
|
| 111 |
+
|
| 112 |
+
def generate(self, days: int = 30, source: str = None) -> Dict[str, Any]:
|
| 113 |
+
"""
|
| 114 |
+
Generate a complete insights report.
|
| 115 |
+
|
| 116 |
+
Args:
|
| 117 |
+
days: Number of days to look back (default: 30)
|
| 118 |
+
source: Optional filter by source platform
|
| 119 |
+
|
| 120 |
+
Returns:
|
| 121 |
+
Dict with all computed insights
|
| 122 |
+
"""
|
| 123 |
+
cutoff = time.time() - (days * 86400)
|
| 124 |
+
|
| 125 |
+
# Gather raw data
|
| 126 |
+
sessions = self._get_sessions(cutoff, source)
|
| 127 |
+
tool_usage = self._get_tool_usage(cutoff, source)
|
| 128 |
+
message_stats = self._get_message_stats(cutoff, source)
|
| 129 |
+
|
| 130 |
+
if not sessions:
|
| 131 |
+
return {
|
| 132 |
+
"days": days,
|
| 133 |
+
"source_filter": source,
|
| 134 |
+
"empty": True,
|
| 135 |
+
"overview": {},
|
| 136 |
+
"models": [],
|
| 137 |
+
"platforms": [],
|
| 138 |
+
"tools": [],
|
| 139 |
+
"activity": {},
|
| 140 |
+
"top_sessions": [],
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
# Compute insights
|
| 144 |
+
overview = self._compute_overview(sessions, message_stats)
|
| 145 |
+
models = self._compute_model_breakdown(sessions)
|
| 146 |
+
platforms = self._compute_platform_breakdown(sessions)
|
| 147 |
+
tools = self._compute_tool_breakdown(tool_usage)
|
| 148 |
+
activity = self._compute_activity_patterns(sessions)
|
| 149 |
+
top_sessions = self._compute_top_sessions(sessions)
|
| 150 |
+
|
| 151 |
+
return {
|
| 152 |
+
"days": days,
|
| 153 |
+
"source_filter": source,
|
| 154 |
+
"empty": False,
|
| 155 |
+
"generated_at": time.time(),
|
| 156 |
+
"overview": overview,
|
| 157 |
+
"models": models,
|
| 158 |
+
"platforms": platforms,
|
| 159 |
+
"tools": tools,
|
| 160 |
+
"activity": activity,
|
| 161 |
+
"top_sessions": top_sessions,
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
# =========================================================================
|
| 165 |
+
# Data gathering (SQL queries)
|
| 166 |
+
# =========================================================================
|
| 167 |
+
|
| 168 |
+
# Columns we actually need (skip system_prompt, model_config blobs)
|
| 169 |
+
_SESSION_COLS = ("id, source, model, started_at, ended_at, "
|
| 170 |
+
"message_count, tool_call_count, input_tokens, output_tokens, "
|
| 171 |
+
"cache_read_tokens, cache_write_tokens, billing_provider, "
|
| 172 |
+
"billing_base_url, billing_mode, estimated_cost_usd, "
|
| 173 |
+
"actual_cost_usd, cost_status, cost_source")
|
| 174 |
+
|
| 175 |
+
# Pre-computed query strings — f-string evaluated once at class definition,
|
| 176 |
+
# not at runtime, so no user-controlled value can alter the query structure.
|
| 177 |
+
_GET_SESSIONS_WITH_SOURCE = (
|
| 178 |
+
f"SELECT {_SESSION_COLS} FROM sessions"
|
| 179 |
+
" WHERE started_at >= ? AND source = ?"
|
| 180 |
+
" ORDER BY started_at DESC"
|
| 181 |
+
)
|
| 182 |
+
_GET_SESSIONS_ALL = (
|
| 183 |
+
f"SELECT {_SESSION_COLS} FROM sessions"
|
| 184 |
+
" WHERE started_at >= ?"
|
| 185 |
+
" ORDER BY started_at DESC"
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
def _get_sessions(self, cutoff: float, source: str = None) -> List[Dict]:
|
| 189 |
+
"""Fetch sessions within the time window."""
|
| 190 |
+
if source:
|
| 191 |
+
cursor = self._conn.execute(self._GET_SESSIONS_WITH_SOURCE, (cutoff, source))
|
| 192 |
+
else:
|
| 193 |
+
cursor = self._conn.execute(self._GET_SESSIONS_ALL, (cutoff,))
|
| 194 |
+
return [dict(row) for row in cursor.fetchall()]
|
| 195 |
+
|
| 196 |
+
def _get_tool_usage(self, cutoff: float, source: str = None) -> List[Dict]:
|
| 197 |
+
"""Get tool call counts from messages.
|
| 198 |
+
|
| 199 |
+
Uses two sources:
|
| 200 |
+
1. tool_name column on 'tool' role messages (set by gateway)
|
| 201 |
+
2. tool_calls JSON on 'assistant' role messages (covers CLI where
|
| 202 |
+
tool_name is not populated on tool responses)
|
| 203 |
+
"""
|
| 204 |
+
tool_counts = Counter()
|
| 205 |
+
|
| 206 |
+
# Source 1: explicit tool_name on tool response messages
|
| 207 |
+
if source:
|
| 208 |
+
cursor = self._conn.execute(
|
| 209 |
+
"""SELECT m.tool_name, COUNT(*) as count
|
| 210 |
+
FROM messages m
|
| 211 |
+
JOIN sessions s ON s.id = m.session_id
|
| 212 |
+
WHERE s.started_at >= ? AND s.source = ?
|
| 213 |
+
AND m.role = 'tool' AND m.tool_name IS NOT NULL
|
| 214 |
+
GROUP BY m.tool_name
|
| 215 |
+
ORDER BY count DESC""",
|
| 216 |
+
(cutoff, source),
|
| 217 |
+
)
|
| 218 |
+
else:
|
| 219 |
+
cursor = self._conn.execute(
|
| 220 |
+
"""SELECT m.tool_name, COUNT(*) as count
|
| 221 |
+
FROM messages m
|
| 222 |
+
JOIN sessions s ON s.id = m.session_id
|
| 223 |
+
WHERE s.started_at >= ?
|
| 224 |
+
AND m.role = 'tool' AND m.tool_name IS NOT NULL
|
| 225 |
+
GROUP BY m.tool_name
|
| 226 |
+
ORDER BY count DESC""",
|
| 227 |
+
(cutoff,),
|
| 228 |
+
)
|
| 229 |
+
for row in cursor.fetchall():
|
| 230 |
+
tool_counts[row["tool_name"]] += row["count"]
|
| 231 |
+
|
| 232 |
+
# Source 2: extract from tool_calls JSON on assistant messages
|
| 233 |
+
# (covers CLI sessions where tool_name is NULL on tool responses)
|
| 234 |
+
if source:
|
| 235 |
+
cursor2 = self._conn.execute(
|
| 236 |
+
"""SELECT m.tool_calls
|
| 237 |
+
FROM messages m
|
| 238 |
+
JOIN sessions s ON s.id = m.session_id
|
| 239 |
+
WHERE s.started_at >= ? AND s.source = ?
|
| 240 |
+
AND m.role = 'assistant' AND m.tool_calls IS NOT NULL""",
|
| 241 |
+
(cutoff, source),
|
| 242 |
+
)
|
| 243 |
+
else:
|
| 244 |
+
cursor2 = self._conn.execute(
|
| 245 |
+
"""SELECT m.tool_calls
|
| 246 |
+
FROM messages m
|
| 247 |
+
JOIN sessions s ON s.id = m.session_id
|
| 248 |
+
WHERE s.started_at >= ?
|
| 249 |
+
AND m.role = 'assistant' AND m.tool_calls IS NOT NULL""",
|
| 250 |
+
(cutoff,),
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
tool_calls_counts = Counter()
|
| 254 |
+
for row in cursor2.fetchall():
|
| 255 |
+
try:
|
| 256 |
+
calls = row["tool_calls"]
|
| 257 |
+
if isinstance(calls, str):
|
| 258 |
+
calls = json.loads(calls)
|
| 259 |
+
if isinstance(calls, list):
|
| 260 |
+
for call in calls:
|
| 261 |
+
func = call.get("function", {}) if isinstance(call, dict) else {}
|
| 262 |
+
name = func.get("name")
|
| 263 |
+
if name:
|
| 264 |
+
tool_calls_counts[name] += 1
|
| 265 |
+
except (json.JSONDecodeError, TypeError, AttributeError):
|
| 266 |
+
continue
|
| 267 |
+
|
| 268 |
+
# Merge: prefer tool_name source, supplement with tool_calls source
|
| 269 |
+
# for tools not already counted
|
| 270 |
+
if not tool_counts and tool_calls_counts:
|
| 271 |
+
# No tool_name data at all — use tool_calls exclusively
|
| 272 |
+
tool_counts = tool_calls_counts
|
| 273 |
+
elif tool_counts and tool_calls_counts:
|
| 274 |
+
# Both sources have data — use whichever has the higher count per tool
|
| 275 |
+
# (they may overlap, so take the max to avoid double-counting)
|
| 276 |
+
all_tools = set(tool_counts) | set(tool_calls_counts)
|
| 277 |
+
merged = Counter()
|
| 278 |
+
for tool in all_tools:
|
| 279 |
+
merged[tool] = max(tool_counts.get(tool, 0), tool_calls_counts.get(tool, 0))
|
| 280 |
+
tool_counts = merged
|
| 281 |
+
|
| 282 |
+
# Convert to the expected format
|
| 283 |
+
return [
|
| 284 |
+
{"tool_name": name, "count": count}
|
| 285 |
+
for name, count in tool_counts.most_common()
|
| 286 |
+
]
|
| 287 |
+
|
| 288 |
+
def _get_message_stats(self, cutoff: float, source: str = None) -> Dict:
|
| 289 |
+
"""Get aggregate message statistics."""
|
| 290 |
+
if source:
|
| 291 |
+
cursor = self._conn.execute(
|
| 292 |
+
"""SELECT
|
| 293 |
+
COUNT(*) as total_messages,
|
| 294 |
+
SUM(CASE WHEN m.role = 'user' THEN 1 ELSE 0 END) as user_messages,
|
| 295 |
+
SUM(CASE WHEN m.role = 'assistant' THEN 1 ELSE 0 END) as assistant_messages,
|
| 296 |
+
SUM(CASE WHEN m.role = 'tool' THEN 1 ELSE 0 END) as tool_messages
|
| 297 |
+
FROM messages m
|
| 298 |
+
JOIN sessions s ON s.id = m.session_id
|
| 299 |
+
WHERE s.started_at >= ? AND s.source = ?""",
|
| 300 |
+
(cutoff, source),
|
| 301 |
+
)
|
| 302 |
+
else:
|
| 303 |
+
cursor = self._conn.execute(
|
| 304 |
+
"""SELECT
|
| 305 |
+
COUNT(*) as total_messages,
|
| 306 |
+
SUM(CASE WHEN m.role = 'user' THEN 1 ELSE 0 END) as user_messages,
|
| 307 |
+
SUM(CASE WHEN m.role = 'assistant' THEN 1 ELSE 0 END) as assistant_messages,
|
| 308 |
+
SUM(CASE WHEN m.role = 'tool' THEN 1 ELSE 0 END) as tool_messages
|
| 309 |
+
FROM messages m
|
| 310 |
+
JOIN sessions s ON s.id = m.session_id
|
| 311 |
+
WHERE s.started_at >= ?""",
|
| 312 |
+
(cutoff,),
|
| 313 |
+
)
|
| 314 |
+
row = cursor.fetchone()
|
| 315 |
+
return dict(row) if row else {
|
| 316 |
+
"total_messages": 0, "user_messages": 0,
|
| 317 |
+
"assistant_messages": 0, "tool_messages": 0,
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
# =========================================================================
|
| 321 |
+
# Computation
|
| 322 |
+
# =========================================================================
|
| 323 |
+
|
| 324 |
+
def _compute_overview(self, sessions: List[Dict], message_stats: Dict) -> Dict:
|
| 325 |
+
"""Compute high-level overview statistics."""
|
| 326 |
+
total_input = sum(s.get("input_tokens") or 0 for s in sessions)
|
| 327 |
+
total_output = sum(s.get("output_tokens") or 0 for s in sessions)
|
| 328 |
+
total_cache_read = sum(s.get("cache_read_tokens") or 0 for s in sessions)
|
| 329 |
+
total_cache_write = sum(s.get("cache_write_tokens") or 0 for s in sessions)
|
| 330 |
+
total_tokens = total_input + total_output + total_cache_read + total_cache_write
|
| 331 |
+
total_tool_calls = sum(s.get("tool_call_count") or 0 for s in sessions)
|
| 332 |
+
total_messages = sum(s.get("message_count") or 0 for s in sessions)
|
| 333 |
+
|
| 334 |
+
# Cost estimation (weighted by model)
|
| 335 |
+
total_cost = 0.0
|
| 336 |
+
actual_cost = 0.0
|
| 337 |
+
models_with_pricing = set()
|
| 338 |
+
models_without_pricing = set()
|
| 339 |
+
unknown_cost_sessions = 0
|
| 340 |
+
included_cost_sessions = 0
|
| 341 |
+
for s in sessions:
|
| 342 |
+
model = s.get("model") or ""
|
| 343 |
+
estimated, status = _estimate_cost(s)
|
| 344 |
+
total_cost += estimated
|
| 345 |
+
actual_cost += s.get("actual_cost_usd") or 0.0
|
| 346 |
+
display = model.split("/")[-1] if "/" in model else (model or "unknown")
|
| 347 |
+
if status == "included":
|
| 348 |
+
included_cost_sessions += 1
|
| 349 |
+
elif status == "unknown":
|
| 350 |
+
unknown_cost_sessions += 1
|
| 351 |
+
if _has_known_pricing(model, s.get("billing_provider"), s.get("billing_base_url")):
|
| 352 |
+
models_with_pricing.add(display)
|
| 353 |
+
else:
|
| 354 |
+
models_without_pricing.add(display)
|
| 355 |
+
|
| 356 |
+
# Session duration stats (guard against negative durations from clock drift)
|
| 357 |
+
durations = []
|
| 358 |
+
for s in sessions:
|
| 359 |
+
start = s.get("started_at")
|
| 360 |
+
end = s.get("ended_at")
|
| 361 |
+
if start and end and end > start:
|
| 362 |
+
durations.append(end - start)
|
| 363 |
+
|
| 364 |
+
total_hours = sum(durations) / 3600 if durations else 0
|
| 365 |
+
avg_duration = sum(durations) / len(durations) if durations else 0
|
| 366 |
+
|
| 367 |
+
# Earliest and latest session
|
| 368 |
+
started_timestamps = [s["started_at"] for s in sessions if s.get("started_at")]
|
| 369 |
+
date_range_start = min(started_timestamps) if started_timestamps else None
|
| 370 |
+
date_range_end = max(started_timestamps) if started_timestamps else None
|
| 371 |
+
|
| 372 |
+
return {
|
| 373 |
+
"total_sessions": len(sessions),
|
| 374 |
+
"total_messages": total_messages,
|
| 375 |
+
"total_tool_calls": total_tool_calls,
|
| 376 |
+
"total_input_tokens": total_input,
|
| 377 |
+
"total_output_tokens": total_output,
|
| 378 |
+
"total_cache_read_tokens": total_cache_read,
|
| 379 |
+
"total_cache_write_tokens": total_cache_write,
|
| 380 |
+
"total_tokens": total_tokens,
|
| 381 |
+
"estimated_cost": total_cost,
|
| 382 |
+
"actual_cost": actual_cost,
|
| 383 |
+
"total_hours": total_hours,
|
| 384 |
+
"avg_session_duration": avg_duration,
|
| 385 |
+
"avg_messages_per_session": total_messages / len(sessions) if sessions else 0,
|
| 386 |
+
"avg_tokens_per_session": total_tokens / len(sessions) if sessions else 0,
|
| 387 |
+
"user_messages": message_stats.get("user_messages") or 0,
|
| 388 |
+
"assistant_messages": message_stats.get("assistant_messages") or 0,
|
| 389 |
+
"tool_messages": message_stats.get("tool_messages") or 0,
|
| 390 |
+
"date_range_start": date_range_start,
|
| 391 |
+
"date_range_end": date_range_end,
|
| 392 |
+
"models_with_pricing": sorted(models_with_pricing),
|
| 393 |
+
"models_without_pricing": sorted(models_without_pricing),
|
| 394 |
+
"unknown_cost_sessions": unknown_cost_sessions,
|
| 395 |
+
"included_cost_sessions": included_cost_sessions,
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
def _compute_model_breakdown(self, sessions: List[Dict]) -> List[Dict]:
|
| 399 |
+
"""Break down usage by model."""
|
| 400 |
+
model_data = defaultdict(lambda: {
|
| 401 |
+
"sessions": 0, "input_tokens": 0, "output_tokens": 0,
|
| 402 |
+
"cache_read_tokens": 0, "cache_write_tokens": 0,
|
| 403 |
+
"total_tokens": 0, "tool_calls": 0, "cost": 0.0,
|
| 404 |
+
})
|
| 405 |
+
|
| 406 |
+
for s in sessions:
|
| 407 |
+
model = s.get("model") or "unknown"
|
| 408 |
+
# Normalize: strip provider prefix for display
|
| 409 |
+
display_model = model.split("/")[-1] if "/" in model else model
|
| 410 |
+
d = model_data[display_model]
|
| 411 |
+
d["sessions"] += 1
|
| 412 |
+
inp = s.get("input_tokens") or 0
|
| 413 |
+
out = s.get("output_tokens") or 0
|
| 414 |
+
cache_read = s.get("cache_read_tokens") or 0
|
| 415 |
+
cache_write = s.get("cache_write_tokens") or 0
|
| 416 |
+
d["input_tokens"] += inp
|
| 417 |
+
d["output_tokens"] += out
|
| 418 |
+
d["cache_read_tokens"] += cache_read
|
| 419 |
+
d["cache_write_tokens"] += cache_write
|
| 420 |
+
d["total_tokens"] += inp + out + cache_read + cache_write
|
| 421 |
+
d["tool_calls"] += s.get("tool_call_count") or 0
|
| 422 |
+
estimate, status = _estimate_cost(s)
|
| 423 |
+
d["cost"] += estimate
|
| 424 |
+
d["has_pricing"] = _has_known_pricing(model, s.get("billing_provider"), s.get("billing_base_url"))
|
| 425 |
+
d["cost_status"] = status
|
| 426 |
+
|
| 427 |
+
result = [
|
| 428 |
+
{"model": model, **data}
|
| 429 |
+
for model, data in model_data.items()
|
| 430 |
+
]
|
| 431 |
+
# Sort by tokens first, fall back to session count when tokens are 0
|
| 432 |
+
result.sort(key=lambda x: (x["total_tokens"], x["sessions"]), reverse=True)
|
| 433 |
+
return result
|
| 434 |
+
|
| 435 |
+
def _compute_platform_breakdown(self, sessions: List[Dict]) -> List[Dict]:
|
| 436 |
+
"""Break down usage by platform/source."""
|
| 437 |
+
platform_data = defaultdict(lambda: {
|
| 438 |
+
"sessions": 0, "messages": 0, "input_tokens": 0,
|
| 439 |
+
"output_tokens": 0, "cache_read_tokens": 0,
|
| 440 |
+
"cache_write_tokens": 0, "total_tokens": 0, "tool_calls": 0,
|
| 441 |
+
})
|
| 442 |
+
|
| 443 |
+
for s in sessions:
|
| 444 |
+
source = s.get("source") or "unknown"
|
| 445 |
+
d = platform_data[source]
|
| 446 |
+
d["sessions"] += 1
|
| 447 |
+
d["messages"] += s.get("message_count") or 0
|
| 448 |
+
inp = s.get("input_tokens") or 0
|
| 449 |
+
out = s.get("output_tokens") or 0
|
| 450 |
+
cache_read = s.get("cache_read_tokens") or 0
|
| 451 |
+
cache_write = s.get("cache_write_tokens") or 0
|
| 452 |
+
d["input_tokens"] += inp
|
| 453 |
+
d["output_tokens"] += out
|
| 454 |
+
d["cache_read_tokens"] += cache_read
|
| 455 |
+
d["cache_write_tokens"] += cache_write
|
| 456 |
+
d["total_tokens"] += inp + out + cache_read + cache_write
|
| 457 |
+
d["tool_calls"] += s.get("tool_call_count") or 0
|
| 458 |
+
|
| 459 |
+
result = [
|
| 460 |
+
{"platform": platform, **data}
|
| 461 |
+
for platform, data in platform_data.items()
|
| 462 |
+
]
|
| 463 |
+
result.sort(key=lambda x: x["sessions"], reverse=True)
|
| 464 |
+
return result
|
| 465 |
+
|
| 466 |
+
def _compute_tool_breakdown(self, tool_usage: List[Dict]) -> List[Dict]:
|
| 467 |
+
"""Process tool usage data into a ranked list with percentages."""
|
| 468 |
+
total_calls = sum(t["count"] for t in tool_usage) if tool_usage else 0
|
| 469 |
+
result = []
|
| 470 |
+
for t in tool_usage:
|
| 471 |
+
pct = (t["count"] / total_calls * 100) if total_calls else 0
|
| 472 |
+
result.append({
|
| 473 |
+
"tool": t["tool_name"],
|
| 474 |
+
"count": t["count"],
|
| 475 |
+
"percentage": pct,
|
| 476 |
+
})
|
| 477 |
+
return result
|
| 478 |
+
|
| 479 |
+
def _compute_activity_patterns(self, sessions: List[Dict]) -> Dict:
|
| 480 |
+
"""Analyze activity patterns by day of week and hour."""
|
| 481 |
+
day_counts = Counter() # 0=Monday ... 6=Sunday
|
| 482 |
+
hour_counts = Counter()
|
| 483 |
+
daily_counts = Counter() # date string -> count
|
| 484 |
+
|
| 485 |
+
for s in sessions:
|
| 486 |
+
ts = s.get("started_at")
|
| 487 |
+
if not ts:
|
| 488 |
+
continue
|
| 489 |
+
dt = datetime.fromtimestamp(ts)
|
| 490 |
+
day_counts[dt.weekday()] += 1
|
| 491 |
+
hour_counts[dt.hour] += 1
|
| 492 |
+
daily_counts[dt.strftime("%Y-%m-%d")] += 1
|
| 493 |
+
|
| 494 |
+
day_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
|
| 495 |
+
day_breakdown = [
|
| 496 |
+
{"day": day_names[i], "count": day_counts.get(i, 0)}
|
| 497 |
+
for i in range(7)
|
| 498 |
+
]
|
| 499 |
+
|
| 500 |
+
hour_breakdown = [
|
| 501 |
+
{"hour": i, "count": hour_counts.get(i, 0)}
|
| 502 |
+
for i in range(24)
|
| 503 |
+
]
|
| 504 |
+
|
| 505 |
+
# Busiest day and hour
|
| 506 |
+
busiest_day = max(day_breakdown, key=lambda x: x["count"]) if day_breakdown else None
|
| 507 |
+
busiest_hour = max(hour_breakdown, key=lambda x: x["count"]) if hour_breakdown else None
|
| 508 |
+
|
| 509 |
+
# Active days (days with at least one session)
|
| 510 |
+
active_days = len(daily_counts)
|
| 511 |
+
|
| 512 |
+
# Streak calculation
|
| 513 |
+
if daily_counts:
|
| 514 |
+
all_dates = sorted(daily_counts.keys())
|
| 515 |
+
current_streak = 1
|
| 516 |
+
max_streak = 1
|
| 517 |
+
for i in range(1, len(all_dates)):
|
| 518 |
+
d1 = datetime.strptime(all_dates[i - 1], "%Y-%m-%d")
|
| 519 |
+
d2 = datetime.strptime(all_dates[i], "%Y-%m-%d")
|
| 520 |
+
if (d2 - d1).days == 1:
|
| 521 |
+
current_streak += 1
|
| 522 |
+
max_streak = max(max_streak, current_streak)
|
| 523 |
+
else:
|
| 524 |
+
current_streak = 1
|
| 525 |
+
else:
|
| 526 |
+
max_streak = 0
|
| 527 |
+
|
| 528 |
+
return {
|
| 529 |
+
"by_day": day_breakdown,
|
| 530 |
+
"by_hour": hour_breakdown,
|
| 531 |
+
"busiest_day": busiest_day,
|
| 532 |
+
"busiest_hour": busiest_hour,
|
| 533 |
+
"active_days": active_days,
|
| 534 |
+
"max_streak": max_streak,
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
def _compute_top_sessions(self, sessions: List[Dict]) -> List[Dict]:
|
| 538 |
+
"""Find notable sessions (longest, most messages, most tokens)."""
|
| 539 |
+
top = []
|
| 540 |
+
|
| 541 |
+
# Longest by duration
|
| 542 |
+
sessions_with_duration = [
|
| 543 |
+
s for s in sessions
|
| 544 |
+
if s.get("started_at") and s.get("ended_at")
|
| 545 |
+
]
|
| 546 |
+
if sessions_with_duration:
|
| 547 |
+
longest = max(
|
| 548 |
+
sessions_with_duration,
|
| 549 |
+
key=lambda s: (s["ended_at"] - s["started_at"]),
|
| 550 |
+
)
|
| 551 |
+
dur = longest["ended_at"] - longest["started_at"]
|
| 552 |
+
top.append({
|
| 553 |
+
"label": "Longest session",
|
| 554 |
+
"session_id": longest["id"][:16],
|
| 555 |
+
"value": _format_duration(dur),
|
| 556 |
+
"date": datetime.fromtimestamp(longest["started_at"]).strftime("%b %d"),
|
| 557 |
+
})
|
| 558 |
+
|
| 559 |
+
# Most messages
|
| 560 |
+
most_msgs = max(sessions, key=lambda s: s.get("message_count") or 0)
|
| 561 |
+
if (most_msgs.get("message_count") or 0) > 0:
|
| 562 |
+
top.append({
|
| 563 |
+
"label": "Most messages",
|
| 564 |
+
"session_id": most_msgs["id"][:16],
|
| 565 |
+
"value": f"{most_msgs['message_count']} msgs",
|
| 566 |
+
"date": datetime.fromtimestamp(most_msgs["started_at"]).strftime("%b %d") if most_msgs.get("started_at") else "?",
|
| 567 |
+
})
|
| 568 |
+
|
| 569 |
+
# Most tokens
|
| 570 |
+
most_tokens = max(
|
| 571 |
+
sessions,
|
| 572 |
+
key=lambda s: (s.get("input_tokens") or 0) + (s.get("output_tokens") or 0),
|
| 573 |
+
)
|
| 574 |
+
token_total = (most_tokens.get("input_tokens") or 0) + (most_tokens.get("output_tokens") or 0)
|
| 575 |
+
if token_total > 0:
|
| 576 |
+
top.append({
|
| 577 |
+
"label": "Most tokens",
|
| 578 |
+
"session_id": most_tokens["id"][:16],
|
| 579 |
+
"value": f"{token_total:,} tokens",
|
| 580 |
+
"date": datetime.fromtimestamp(most_tokens["started_at"]).strftime("%b %d") if most_tokens.get("started_at") else "?",
|
| 581 |
+
})
|
| 582 |
+
|
| 583 |
+
# Most tool calls
|
| 584 |
+
most_tools = max(sessions, key=lambda s: s.get("tool_call_count") or 0)
|
| 585 |
+
if (most_tools.get("tool_call_count") or 0) > 0:
|
| 586 |
+
top.append({
|
| 587 |
+
"label": "Most tool calls",
|
| 588 |
+
"session_id": most_tools["id"][:16],
|
| 589 |
+
"value": f"{most_tools['tool_call_count']} calls",
|
| 590 |
+
"date": datetime.fromtimestamp(most_tools["started_at"]).strftime("%b %d") if most_tools.get("started_at") else "?",
|
| 591 |
+
})
|
| 592 |
+
|
| 593 |
+
return top
|
| 594 |
+
|
| 595 |
+
# =========================================================================
|
| 596 |
+
# Formatting
|
| 597 |
+
# =========================================================================
|
| 598 |
+
|
| 599 |
+
def format_terminal(self, report: Dict) -> str:
|
| 600 |
+
"""Format the insights report for terminal display (CLI)."""
|
| 601 |
+
if report.get("empty"):
|
| 602 |
+
days = report.get("days", 30)
|
| 603 |
+
src = f" (source: {report['source_filter']})" if report.get("source_filter") else ""
|
| 604 |
+
return f" No sessions found in the last {days} days{src}."
|
| 605 |
+
|
| 606 |
+
lines = []
|
| 607 |
+
o = report["overview"]
|
| 608 |
+
days = report["days"]
|
| 609 |
+
src_filter = report.get("source_filter")
|
| 610 |
+
|
| 611 |
+
# Header
|
| 612 |
+
lines.append("")
|
| 613 |
+
lines.append(" ╔══════════════════════════════════════════════════════════╗")
|
| 614 |
+
lines.append(" ��� 📊 Hermes Insights ║")
|
| 615 |
+
period_label = f"Last {days} days"
|
| 616 |
+
if src_filter:
|
| 617 |
+
period_label += f" ({src_filter})"
|
| 618 |
+
padding = 58 - len(period_label) - 2
|
| 619 |
+
left_pad = padding // 2
|
| 620 |
+
right_pad = padding - left_pad
|
| 621 |
+
lines.append(f" ║{' ' * left_pad} {period_label} {' ' * right_pad}║")
|
| 622 |
+
lines.append(" ╚══════════════════════════════════════════════════════════╝")
|
| 623 |
+
lines.append("")
|
| 624 |
+
|
| 625 |
+
# Date range
|
| 626 |
+
if o.get("date_range_start") and o.get("date_range_end"):
|
| 627 |
+
start_str = datetime.fromtimestamp(o["date_range_start"]).strftime("%b %d, %Y")
|
| 628 |
+
end_str = datetime.fromtimestamp(o["date_range_end"]).strftime("%b %d, %Y")
|
| 629 |
+
lines.append(f" Period: {start_str} — {end_str}")
|
| 630 |
+
lines.append("")
|
| 631 |
+
|
| 632 |
+
# Overview
|
| 633 |
+
lines.append(" 📋 Overview")
|
| 634 |
+
lines.append(" " + "─" * 56)
|
| 635 |
+
lines.append(f" Sessions: {o['total_sessions']:<12} Messages: {o['total_messages']:,}")
|
| 636 |
+
lines.append(f" Tool calls: {o['total_tool_calls']:<12,} User messages: {o['user_messages']:,}")
|
| 637 |
+
lines.append(f" Input tokens: {o['total_input_tokens']:<12,} Output tokens: {o['total_output_tokens']:,}")
|
| 638 |
+
cache_total = o.get("total_cache_read_tokens", 0) + o.get("total_cache_write_tokens", 0)
|
| 639 |
+
if cache_total > 0:
|
| 640 |
+
lines.append(f" Cache read: {o['total_cache_read_tokens']:<12,} Cache write: {o['total_cache_write_tokens']:,}")
|
| 641 |
+
cost_str = f"${o['estimated_cost']:.2f}"
|
| 642 |
+
if o.get("models_without_pricing"):
|
| 643 |
+
cost_str += " *"
|
| 644 |
+
lines.append(f" Total tokens: {o['total_tokens']:<12,} Est. cost: {cost_str}")
|
| 645 |
+
if o["total_hours"] > 0:
|
| 646 |
+
lines.append(f" Active time: ~{_format_duration(o['total_hours'] * 3600):<11} Avg session: ~{_format_duration(o['avg_session_duration'])}")
|
| 647 |
+
lines.append(f" Avg msgs/session: {o['avg_messages_per_session']:.1f}")
|
| 648 |
+
lines.append("")
|
| 649 |
+
|
| 650 |
+
# Model breakdown
|
| 651 |
+
if report["models"]:
|
| 652 |
+
lines.append(" 🤖 Models Used")
|
| 653 |
+
lines.append(" " + "─" * 56)
|
| 654 |
+
lines.append(f" {'Model':<30} {'Sessions':>8} {'Tokens':>12} {'Cost':>8}")
|
| 655 |
+
for m in report["models"]:
|
| 656 |
+
model_name = m["model"][:28]
|
| 657 |
+
if m.get("has_pricing"):
|
| 658 |
+
cost_cell = f"${m['cost']:>6.2f}"
|
| 659 |
+
else:
|
| 660 |
+
cost_cell = " N/A"
|
| 661 |
+
lines.append(f" {model_name:<30} {m['sessions']:>8} {m['total_tokens']:>12,} {cost_cell}")
|
| 662 |
+
if o.get("models_without_pricing"):
|
| 663 |
+
lines.append(" * Cost N/A for custom/self-hosted models")
|
| 664 |
+
lines.append("")
|
| 665 |
+
|
| 666 |
+
# Platform breakdown
|
| 667 |
+
if len(report["platforms"]) > 1 or (report["platforms"] and report["platforms"][0]["platform"] != "cli"):
|
| 668 |
+
lines.append(" 📱 Platforms")
|
| 669 |
+
lines.append(" " + "─" * 56)
|
| 670 |
+
lines.append(f" {'Platform':<14} {'Sessions':>8} {'Messages':>10} {'Tokens':>14}")
|
| 671 |
+
for p in report["platforms"]:
|
| 672 |
+
lines.append(f" {p['platform']:<14} {p['sessions']:>8} {p['messages']:>10,} {p['total_tokens']:>14,}")
|
| 673 |
+
lines.append("")
|
| 674 |
+
|
| 675 |
+
# Tool usage
|
| 676 |
+
if report["tools"]:
|
| 677 |
+
lines.append(" 🔧 Top Tools")
|
| 678 |
+
lines.append(" " + "─" * 56)
|
| 679 |
+
lines.append(f" {'Tool':<28} {'Calls':>8} {'%':>8}")
|
| 680 |
+
for t in report["tools"][:15]: # Top 15
|
| 681 |
+
lines.append(f" {t['tool']:<28} {t['count']:>8,} {t['percentage']:>7.1f}%")
|
| 682 |
+
if len(report["tools"]) > 15:
|
| 683 |
+
lines.append(f" ... and {len(report['tools']) - 15} more tools")
|
| 684 |
+
lines.append("")
|
| 685 |
+
|
| 686 |
+
# Activity patterns
|
| 687 |
+
act = report.get("activity", {})
|
| 688 |
+
if act.get("by_day"):
|
| 689 |
+
lines.append(" 📅 Activity Patterns")
|
| 690 |
+
lines.append(" " + "─" * 56)
|
| 691 |
+
|
| 692 |
+
# Day of week chart
|
| 693 |
+
day_values = [d["count"] for d in act["by_day"]]
|
| 694 |
+
bars = _bar_chart(day_values, max_width=15)
|
| 695 |
+
for i, d in enumerate(act["by_day"]):
|
| 696 |
+
bar = bars[i]
|
| 697 |
+
lines.append(f" {d['day']} {bar:<15} {d['count']}")
|
| 698 |
+
|
| 699 |
+
lines.append("")
|
| 700 |
+
|
| 701 |
+
# Peak hours (show top 5 busiest hours)
|
| 702 |
+
busy_hours = sorted(act["by_hour"], key=lambda x: x["count"], reverse=True)
|
| 703 |
+
busy_hours = [h for h in busy_hours if h["count"] > 0][:5]
|
| 704 |
+
if busy_hours:
|
| 705 |
+
hour_strs = []
|
| 706 |
+
for h in busy_hours:
|
| 707 |
+
hr = h["hour"]
|
| 708 |
+
ampm = "AM" if hr < 12 else "PM"
|
| 709 |
+
display_hr = hr % 12 or 12
|
| 710 |
+
hour_strs.append(f"{display_hr}{ampm} ({h['count']})")
|
| 711 |
+
lines.append(f" Peak hours: {', '.join(hour_strs)}")
|
| 712 |
+
|
| 713 |
+
if act.get("active_days"):
|
| 714 |
+
lines.append(f" Active days: {act['active_days']}")
|
| 715 |
+
if act.get("max_streak") and act["max_streak"] > 1:
|
| 716 |
+
lines.append(f" Best streak: {act['max_streak']} consecutive days")
|
| 717 |
+
lines.append("")
|
| 718 |
+
|
| 719 |
+
# Notable sessions
|
| 720 |
+
if report.get("top_sessions"):
|
| 721 |
+
lines.append(" 🏆 Notable Sessions")
|
| 722 |
+
lines.append(" " + "─" * 56)
|
| 723 |
+
for ts in report["top_sessions"]:
|
| 724 |
+
lines.append(f" {ts['label']:<20} {ts['value']:<18} ({ts['date']}, {ts['session_id']})")
|
| 725 |
+
lines.append("")
|
| 726 |
+
|
| 727 |
+
return "\n".join(lines)
|
| 728 |
+
|
| 729 |
+
def format_gateway(self, report: Dict) -> str:
|
| 730 |
+
"""Format the insights report for gateway/messaging (shorter)."""
|
| 731 |
+
if report.get("empty"):
|
| 732 |
+
days = report.get("days", 30)
|
| 733 |
+
return f"No sessions found in the last {days} days."
|
| 734 |
+
|
| 735 |
+
lines = []
|
| 736 |
+
o = report["overview"]
|
| 737 |
+
days = report["days"]
|
| 738 |
+
|
| 739 |
+
lines.append(f"📊 **Hermes Insights** — Last {days} days\n")
|
| 740 |
+
|
| 741 |
+
# Overview
|
| 742 |
+
lines.append(f"**Sessions:** {o['total_sessions']} | **Messages:** {o['total_messages']:,} | **Tool calls:** {o['total_tool_calls']:,}")
|
| 743 |
+
cache_total = o.get("total_cache_read_tokens", 0) + o.get("total_cache_write_tokens", 0)
|
| 744 |
+
if cache_total > 0:
|
| 745 |
+
lines.append(f"**Tokens:** {o['total_tokens']:,} (in: {o['total_input_tokens']:,} / out: {o['total_output_tokens']:,} / cache: {cache_total:,})")
|
| 746 |
+
else:
|
| 747 |
+
lines.append(f"**Tokens:** {o['total_tokens']:,} (in: {o['total_input_tokens']:,} / out: {o['total_output_tokens']:,})")
|
| 748 |
+
cost_note = ""
|
| 749 |
+
if o.get("models_without_pricing"):
|
| 750 |
+
cost_note = " _(excludes custom/self-hosted models)_"
|
| 751 |
+
lines.append(f"**Est. cost:** ${o['estimated_cost']:.2f}{cost_note}")
|
| 752 |
+
if o["total_hours"] > 0:
|
| 753 |
+
lines.append(f"**Active time:** ~{_format_duration(o['total_hours'] * 3600)} | **Avg session:** ~{_format_duration(o['avg_session_duration'])}")
|
| 754 |
+
lines.append("")
|
| 755 |
+
|
| 756 |
+
# Models (top 5)
|
| 757 |
+
if report["models"]:
|
| 758 |
+
lines.append("**🤖 Models:**")
|
| 759 |
+
for m in report["models"][:5]:
|
| 760 |
+
cost_str = f"${m['cost']:.2f}" if m.get("has_pricing") else "N/A"
|
| 761 |
+
lines.append(f" {m['model'][:25]} — {m['sessions']} sessions, {m['total_tokens']:,} tokens, {cost_str}")
|
| 762 |
+
lines.append("")
|
| 763 |
+
|
| 764 |
+
# Platforms (if multi-platform)
|
| 765 |
+
if len(report["platforms"]) > 1:
|
| 766 |
+
lines.append("**📱 Platforms:**")
|
| 767 |
+
for p in report["platforms"]:
|
| 768 |
+
lines.append(f" {p['platform']} — {p['sessions']} sessions, {p['messages']:,} msgs")
|
| 769 |
+
lines.append("")
|
| 770 |
+
|
| 771 |
+
# Tools (top 8)
|
| 772 |
+
if report["tools"]:
|
| 773 |
+
lines.append("**🔧 Top Tools:**")
|
| 774 |
+
for t in report["tools"][:8]:
|
| 775 |
+
lines.append(f" {t['tool']} — {t['count']:,} calls ({t['percentage']:.1f}%)")
|
| 776 |
+
lines.append("")
|
| 777 |
+
|
| 778 |
+
# Activity summary
|
| 779 |
+
act = report.get("activity", {})
|
| 780 |
+
if act.get("busiest_day") and act.get("busiest_hour"):
|
| 781 |
+
hr = act["busiest_hour"]["hour"]
|
| 782 |
+
ampm = "AM" if hr < 12 else "PM"
|
| 783 |
+
display_hr = hr % 12 or 12
|
| 784 |
+
lines.append(f"**📅 Busiest:** {act['busiest_day']['day']}s ({act['busiest_day']['count']} sessions), {display_hr}{ampm} ({act['busiest_hour']['count']} sessions)")
|
| 785 |
+
if act.get("active_days"):
|
| 786 |
+
lines.append(f"**Active days:** {act['active_days']}", )
|
| 787 |
+
if act.get("max_streak", 0) > 1:
|
| 788 |
+
lines.append(f"**Best streak:** {act['max_streak']} consecutive days")
|
| 789 |
+
|
| 790 |
+
return "\n".join(lines)
|
agent/manual_compression_feedback.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""User-facing summaries for manual compression commands."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import Any, Sequence
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def summarize_manual_compression(
|
| 9 |
+
before_messages: Sequence[dict[str, Any]],
|
| 10 |
+
after_messages: Sequence[dict[str, Any]],
|
| 11 |
+
before_tokens: int,
|
| 12 |
+
after_tokens: int,
|
| 13 |
+
) -> dict[str, Any]:
|
| 14 |
+
"""Return consistent user-facing feedback for manual compression."""
|
| 15 |
+
before_count = len(before_messages)
|
| 16 |
+
after_count = len(after_messages)
|
| 17 |
+
noop = list(after_messages) == list(before_messages)
|
| 18 |
+
|
| 19 |
+
if noop:
|
| 20 |
+
headline = f"No changes from compression: {before_count} messages"
|
| 21 |
+
if after_tokens == before_tokens:
|
| 22 |
+
token_line = (
|
| 23 |
+
f"Rough transcript estimate: ~{before_tokens:,} tokens (unchanged)"
|
| 24 |
+
)
|
| 25 |
+
else:
|
| 26 |
+
token_line = (
|
| 27 |
+
f"Rough transcript estimate: ~{before_tokens:,} → "
|
| 28 |
+
f"~{after_tokens:,} tokens"
|
| 29 |
+
)
|
| 30 |
+
else:
|
| 31 |
+
headline = f"Compressed: {before_count} → {after_count} messages"
|
| 32 |
+
token_line = (
|
| 33 |
+
f"Rough transcript estimate: ~{before_tokens:,} → "
|
| 34 |
+
f"~{after_tokens:,} tokens"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
note = None
|
| 38 |
+
if not noop and after_count < before_count and after_tokens > before_tokens:
|
| 39 |
+
note = (
|
| 40 |
+
"Note: fewer messages can still raise this rough transcript estimate "
|
| 41 |
+
"when compression rewrites the transcript into denser summaries."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
return {
|
| 45 |
+
"noop": noop,
|
| 46 |
+
"headline": headline,
|
| 47 |
+
"token_line": token_line,
|
| 48 |
+
"note": note,
|
| 49 |
+
}
|
agent/memory_manager.py
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""MemoryManager — orchestrates the built-in memory provider plus at most
|
| 2 |
+
ONE external plugin memory provider.
|
| 3 |
+
|
| 4 |
+
Single integration point in run_agent.py. Replaces scattered per-backend
|
| 5 |
+
code with one manager that delegates to registered providers.
|
| 6 |
+
|
| 7 |
+
The BuiltinMemoryProvider is always registered first and cannot be removed.
|
| 8 |
+
Only ONE external (non-builtin) provider is allowed at a time — attempting
|
| 9 |
+
to register a second external provider is rejected with a warning. This
|
| 10 |
+
prevents tool schema bloat and conflicting memory backends.
|
| 11 |
+
|
| 12 |
+
Usage in run_agent.py:
|
| 13 |
+
self._memory_manager = MemoryManager()
|
| 14 |
+
self._memory_manager.add_provider(BuiltinMemoryProvider(...))
|
| 15 |
+
# Only ONE of these:
|
| 16 |
+
self._memory_manager.add_provider(plugin_provider)
|
| 17 |
+
|
| 18 |
+
# System prompt
|
| 19 |
+
prompt_parts.append(self._memory_manager.build_system_prompt())
|
| 20 |
+
|
| 21 |
+
# Pre-turn
|
| 22 |
+
context = self._memory_manager.prefetch_all(user_message)
|
| 23 |
+
|
| 24 |
+
# Post-turn
|
| 25 |
+
self._memory_manager.sync_all(user_msg, assistant_response)
|
| 26 |
+
self._memory_manager.queue_prefetch_all(user_msg)
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
from __future__ import annotations
|
| 30 |
+
|
| 31 |
+
import json
|
| 32 |
+
import logging
|
| 33 |
+
import re
|
| 34 |
+
from typing import Any, Dict, List, Optional
|
| 35 |
+
|
| 36 |
+
from agent.memory_provider import MemoryProvider
|
| 37 |
+
from tools.registry import tool_error
|
| 38 |
+
|
| 39 |
+
logger = logging.getLogger(__name__)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# ---------------------------------------------------------------------------
|
| 43 |
+
# Context fencing helpers
|
| 44 |
+
# ---------------------------------------------------------------------------
|
| 45 |
+
|
| 46 |
+
_FENCE_TAG_RE = re.compile(r'</?\s*memory-context\s*>', re.IGNORECASE)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def sanitize_context(text: str) -> str:
|
| 50 |
+
"""Strip fence-escape sequences from provider output."""
|
| 51 |
+
return _FENCE_TAG_RE.sub('', text)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def build_memory_context_block(raw_context: str) -> str:
|
| 55 |
+
"""Wrap prefetched memory in a fenced block with system note.
|
| 56 |
+
|
| 57 |
+
The fence prevents the model from treating recalled context as user
|
| 58 |
+
discourse. Injected at API-call time only — never persisted.
|
| 59 |
+
"""
|
| 60 |
+
if not raw_context or not raw_context.strip():
|
| 61 |
+
return ""
|
| 62 |
+
clean = sanitize_context(raw_context)
|
| 63 |
+
return (
|
| 64 |
+
"<memory-context>\n"
|
| 65 |
+
"[System note: The following is recalled memory context, "
|
| 66 |
+
"NOT new user input. Treat as informational background data.]\n\n"
|
| 67 |
+
f"{clean}\n"
|
| 68 |
+
"</memory-context>"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class MemoryManager:
|
| 73 |
+
"""Orchestrates the built-in provider plus at most one external provider.
|
| 74 |
+
|
| 75 |
+
The builtin provider is always first. Only one non-builtin (external)
|
| 76 |
+
provider is allowed. Failures in one provider never block the other.
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
def __init__(self) -> None:
|
| 80 |
+
self._providers: List[MemoryProvider] = []
|
| 81 |
+
self._tool_to_provider: Dict[str, MemoryProvider] = {}
|
| 82 |
+
self._has_external: bool = False # True once a non-builtin provider is added
|
| 83 |
+
|
| 84 |
+
# -- Registration --------------------------------------------------------
|
| 85 |
+
|
| 86 |
+
def add_provider(self, provider: MemoryProvider) -> None:
|
| 87 |
+
"""Register a memory provider.
|
| 88 |
+
|
| 89 |
+
Built-in provider (name ``"builtin"``) is always accepted.
|
| 90 |
+
Only **one** external (non-builtin) provider is allowed — a second
|
| 91 |
+
attempt is rejected with a warning.
|
| 92 |
+
"""
|
| 93 |
+
is_builtin = provider.name == "builtin"
|
| 94 |
+
|
| 95 |
+
if not is_builtin:
|
| 96 |
+
if self._has_external:
|
| 97 |
+
existing = next(
|
| 98 |
+
(p.name for p in self._providers if p.name != "builtin"), "unknown"
|
| 99 |
+
)
|
| 100 |
+
logger.warning(
|
| 101 |
+
"Rejected memory provider '%s' — external provider '%s' is "
|
| 102 |
+
"already registered. Only one external memory provider is "
|
| 103 |
+
"allowed at a time. Configure which one via memory.provider "
|
| 104 |
+
"in config.yaml.",
|
| 105 |
+
provider.name, existing,
|
| 106 |
+
)
|
| 107 |
+
return
|
| 108 |
+
self._has_external = True
|
| 109 |
+
|
| 110 |
+
self._providers.append(provider)
|
| 111 |
+
|
| 112 |
+
# Index tool names → provider for routing
|
| 113 |
+
for schema in provider.get_tool_schemas():
|
| 114 |
+
tool_name = schema.get("name", "")
|
| 115 |
+
if tool_name and tool_name not in self._tool_to_provider:
|
| 116 |
+
self._tool_to_provider[tool_name] = provider
|
| 117 |
+
elif tool_name in self._tool_to_provider:
|
| 118 |
+
logger.warning(
|
| 119 |
+
"Memory tool name conflict: '%s' already registered by %s, "
|
| 120 |
+
"ignoring from %s",
|
| 121 |
+
tool_name,
|
| 122 |
+
self._tool_to_provider[tool_name].name,
|
| 123 |
+
provider.name,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
logger.info(
|
| 127 |
+
"Memory provider '%s' registered (%d tools)",
|
| 128 |
+
provider.name,
|
| 129 |
+
len(provider.get_tool_schemas()),
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
@property
|
| 133 |
+
def providers(self) -> List[MemoryProvider]:
|
| 134 |
+
"""All registered providers in order."""
|
| 135 |
+
return list(self._providers)
|
| 136 |
+
|
| 137 |
+
def get_provider(self, name: str) -> Optional[MemoryProvider]:
|
| 138 |
+
"""Get a provider by name, or None if not registered."""
|
| 139 |
+
for p in self._providers:
|
| 140 |
+
if p.name == name:
|
| 141 |
+
return p
|
| 142 |
+
return None
|
| 143 |
+
|
| 144 |
+
# -- System prompt -------------------------------------------------------
|
| 145 |
+
|
| 146 |
+
def build_system_prompt(self) -> str:
|
| 147 |
+
"""Collect system prompt blocks from all providers.
|
| 148 |
+
|
| 149 |
+
Returns combined text, or empty string if no providers contribute.
|
| 150 |
+
Each non-empty block is labeled with the provider name.
|
| 151 |
+
"""
|
| 152 |
+
blocks = []
|
| 153 |
+
for provider in self._providers:
|
| 154 |
+
try:
|
| 155 |
+
block = provider.system_prompt_block()
|
| 156 |
+
if block and block.strip():
|
| 157 |
+
blocks.append(block)
|
| 158 |
+
except Exception as e:
|
| 159 |
+
logger.warning(
|
| 160 |
+
"Memory provider '%s' system_prompt_block() failed: %s",
|
| 161 |
+
provider.name, e,
|
| 162 |
+
)
|
| 163 |
+
return "\n\n".join(blocks)
|
| 164 |
+
|
| 165 |
+
# -- Prefetch / recall ---------------------------------------------------
|
| 166 |
+
|
| 167 |
+
def prefetch_all(self, query: str, *, session_id: str = "") -> str:
|
| 168 |
+
"""Collect prefetch context from all providers.
|
| 169 |
+
|
| 170 |
+
Returns merged context text labeled by provider. Empty providers
|
| 171 |
+
are skipped. Failures in one provider don't block others.
|
| 172 |
+
"""
|
| 173 |
+
parts = []
|
| 174 |
+
for provider in self._providers:
|
| 175 |
+
try:
|
| 176 |
+
result = provider.prefetch(query, session_id=session_id)
|
| 177 |
+
if result and result.strip():
|
| 178 |
+
parts.append(result)
|
| 179 |
+
except Exception as e:
|
| 180 |
+
logger.debug(
|
| 181 |
+
"Memory provider '%s' prefetch failed (non-fatal): %s",
|
| 182 |
+
provider.name, e,
|
| 183 |
+
)
|
| 184 |
+
return "\n\n".join(parts)
|
| 185 |
+
|
| 186 |
+
def queue_prefetch_all(self, query: str, *, session_id: str = "") -> None:
|
| 187 |
+
"""Queue background prefetch on all providers for the next turn."""
|
| 188 |
+
for provider in self._providers:
|
| 189 |
+
try:
|
| 190 |
+
provider.queue_prefetch(query, session_id=session_id)
|
| 191 |
+
except Exception as e:
|
| 192 |
+
logger.debug(
|
| 193 |
+
"Memory provider '%s' queue_prefetch failed (non-fatal): %s",
|
| 194 |
+
provider.name, e,
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
# -- Sync ----------------------------------------------------------------
|
| 198 |
+
|
| 199 |
+
def sync_all(self, user_content: str, assistant_content: str, *, session_id: str = "") -> None:
|
| 200 |
+
"""Sync a completed turn to all providers."""
|
| 201 |
+
for provider in self._providers:
|
| 202 |
+
try:
|
| 203 |
+
provider.sync_turn(user_content, assistant_content, session_id=session_id)
|
| 204 |
+
except Exception as e:
|
| 205 |
+
logger.warning(
|
| 206 |
+
"Memory provider '%s' sync_turn failed: %s",
|
| 207 |
+
provider.name, e,
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
# -- Tools ---------------------------------------------------------------
|
| 211 |
+
|
| 212 |
+
def get_all_tool_schemas(self) -> List[Dict[str, Any]]:
|
| 213 |
+
"""Collect tool schemas from all providers."""
|
| 214 |
+
schemas = []
|
| 215 |
+
seen = set()
|
| 216 |
+
for provider in self._providers:
|
| 217 |
+
try:
|
| 218 |
+
for schema in provider.get_tool_schemas():
|
| 219 |
+
name = schema.get("name", "")
|
| 220 |
+
if name and name not in seen:
|
| 221 |
+
schemas.append(schema)
|
| 222 |
+
seen.add(name)
|
| 223 |
+
except Exception as e:
|
| 224 |
+
logger.warning(
|
| 225 |
+
"Memory provider '%s' get_tool_schemas() failed: %s",
|
| 226 |
+
provider.name, e,
|
| 227 |
+
)
|
| 228 |
+
return schemas
|
| 229 |
+
|
| 230 |
+
def get_all_tool_names(self) -> set:
|
| 231 |
+
"""Return set of all tool names across all providers."""
|
| 232 |
+
return set(self._tool_to_provider.keys())
|
| 233 |
+
|
| 234 |
+
def has_tool(self, tool_name: str) -> bool:
|
| 235 |
+
"""Check if any provider handles this tool."""
|
| 236 |
+
return tool_name in self._tool_to_provider
|
| 237 |
+
|
| 238 |
+
def handle_tool_call(
|
| 239 |
+
self, tool_name: str, args: Dict[str, Any], **kwargs
|
| 240 |
+
) -> str:
|
| 241 |
+
"""Route a tool call to the correct provider.
|
| 242 |
+
|
| 243 |
+
Returns JSON string result. Raises ValueError if no provider
|
| 244 |
+
handles the tool.
|
| 245 |
+
"""
|
| 246 |
+
provider = self._tool_to_provider.get(tool_name)
|
| 247 |
+
if provider is None:
|
| 248 |
+
return tool_error(f"No memory provider handles tool '{tool_name}'")
|
| 249 |
+
try:
|
| 250 |
+
return provider.handle_tool_call(tool_name, args, **kwargs)
|
| 251 |
+
except Exception as e:
|
| 252 |
+
logger.error(
|
| 253 |
+
"Memory provider '%s' handle_tool_call(%s) failed: %s",
|
| 254 |
+
provider.name, tool_name, e,
|
| 255 |
+
)
|
| 256 |
+
return tool_error(f"Memory tool '{tool_name}' failed: {e}")
|
| 257 |
+
|
| 258 |
+
# -- Lifecycle hooks -----------------------------------------------------
|
| 259 |
+
|
| 260 |
+
def on_turn_start(self, turn_number: int, message: str, **kwargs) -> None:
|
| 261 |
+
"""Notify all providers of a new turn.
|
| 262 |
+
|
| 263 |
+
kwargs may include: remaining_tokens, model, platform, tool_count.
|
| 264 |
+
"""
|
| 265 |
+
for provider in self._providers:
|
| 266 |
+
try:
|
| 267 |
+
provider.on_turn_start(turn_number, message, **kwargs)
|
| 268 |
+
except Exception as e:
|
| 269 |
+
logger.debug(
|
| 270 |
+
"Memory provider '%s' on_turn_start failed: %s",
|
| 271 |
+
provider.name, e,
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
def on_session_end(self, messages: List[Dict[str, Any]]) -> None:
|
| 275 |
+
"""Notify all providers of session end."""
|
| 276 |
+
for provider in self._providers:
|
| 277 |
+
try:
|
| 278 |
+
provider.on_session_end(messages)
|
| 279 |
+
except Exception as e:
|
| 280 |
+
logger.debug(
|
| 281 |
+
"Memory provider '%s' on_session_end failed: %s",
|
| 282 |
+
provider.name, e,
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
def on_pre_compress(self, messages: List[Dict[str, Any]]) -> str:
|
| 286 |
+
"""Notify all providers before context compression.
|
| 287 |
+
|
| 288 |
+
Returns combined text from providers to include in the compression
|
| 289 |
+
summary prompt. Empty string if no provider contributes.
|
| 290 |
+
"""
|
| 291 |
+
parts = []
|
| 292 |
+
for provider in self._providers:
|
| 293 |
+
try:
|
| 294 |
+
result = provider.on_pre_compress(messages)
|
| 295 |
+
if result and result.strip():
|
| 296 |
+
parts.append(result)
|
| 297 |
+
except Exception as e:
|
| 298 |
+
logger.debug(
|
| 299 |
+
"Memory provider '%s' on_pre_compress failed: %s",
|
| 300 |
+
provider.name, e,
|
| 301 |
+
)
|
| 302 |
+
return "\n\n".join(parts)
|
| 303 |
+
|
| 304 |
+
def on_memory_write(self, action: str, target: str, content: str) -> None:
|
| 305 |
+
"""Notify external providers when the built-in memory tool writes.
|
| 306 |
+
|
| 307 |
+
Skips the builtin provider itself (it's the source of the write).
|
| 308 |
+
"""
|
| 309 |
+
for provider in self._providers:
|
| 310 |
+
if provider.name == "builtin":
|
| 311 |
+
continue
|
| 312 |
+
try:
|
| 313 |
+
provider.on_memory_write(action, target, content)
|
| 314 |
+
except Exception as e:
|
| 315 |
+
logger.debug(
|
| 316 |
+
"Memory provider '%s' on_memory_write failed: %s",
|
| 317 |
+
provider.name, e,
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
def on_delegation(self, task: str, result: str, *,
|
| 321 |
+
child_session_id: str = "", **kwargs) -> None:
|
| 322 |
+
"""Notify all providers that a subagent completed."""
|
| 323 |
+
for provider in self._providers:
|
| 324 |
+
try:
|
| 325 |
+
provider.on_delegation(
|
| 326 |
+
task, result, child_session_id=child_session_id, **kwargs
|
| 327 |
+
)
|
| 328 |
+
except Exception as e:
|
| 329 |
+
logger.debug(
|
| 330 |
+
"Memory provider '%s' on_delegation failed: %s",
|
| 331 |
+
provider.name, e,
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
def shutdown_all(self) -> None:
|
| 335 |
+
"""Shut down all providers (reverse order for clean teardown)."""
|
| 336 |
+
for provider in reversed(self._providers):
|
| 337 |
+
try:
|
| 338 |
+
provider.shutdown()
|
| 339 |
+
except Exception as e:
|
| 340 |
+
logger.warning(
|
| 341 |
+
"Memory provider '%s' shutdown failed: %s",
|
| 342 |
+
provider.name, e,
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
def initialize_all(self, session_id: str, **kwargs) -> None:
|
| 346 |
+
"""Initialize all providers.
|
| 347 |
+
|
| 348 |
+
Automatically injects ``hermes_home`` into *kwargs* so that every
|
| 349 |
+
provider can resolve profile-scoped storage paths without importing
|
| 350 |
+
``get_hermes_home()`` themselves.
|
| 351 |
+
"""
|
| 352 |
+
if "hermes_home" not in kwargs:
|
| 353 |
+
from hermes_constants import get_hermes_home
|
| 354 |
+
kwargs["hermes_home"] = str(get_hermes_home())
|
| 355 |
+
for provider in self._providers:
|
| 356 |
+
try:
|
| 357 |
+
provider.initialize(session_id=session_id, **kwargs)
|
| 358 |
+
except Exception as e:
|
| 359 |
+
logger.warning(
|
| 360 |
+
"Memory provider '%s' initialize failed: %s",
|
| 361 |
+
provider.name, e,
|
| 362 |
+
)
|
agent/memory_provider.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Abstract base class for pluggable memory providers.
|
| 2 |
+
|
| 3 |
+
Memory providers give the agent persistent recall across sessions. One
|
| 4 |
+
external provider is active at a time alongside the always-on built-in
|
| 5 |
+
memory (MEMORY.md / USER.md). The MemoryManager enforces this limit.
|
| 6 |
+
|
| 7 |
+
Built-in memory is always active as the first provider and cannot be removed.
|
| 8 |
+
External providers (Honcho, Hindsight, Mem0, etc.) are additive — they never
|
| 9 |
+
disable the built-in store. Only one external provider runs at a time to
|
| 10 |
+
prevent tool schema bloat and conflicting memory backends.
|
| 11 |
+
|
| 12 |
+
Registration:
|
| 13 |
+
1. Built-in: BuiltinMemoryProvider — always present, not removable.
|
| 14 |
+
2. Plugins: Ship in plugins/memory/<name>/, activated by memory.provider config.
|
| 15 |
+
|
| 16 |
+
Lifecycle (called by MemoryManager, wired in run_agent.py):
|
| 17 |
+
initialize() — connect, create resources, warm up
|
| 18 |
+
system_prompt_block() — static text for the system prompt
|
| 19 |
+
prefetch(query) — background recall before each turn
|
| 20 |
+
sync_turn(user, asst) — async write after each turn
|
| 21 |
+
get_tool_schemas() — tool schemas to expose to the model
|
| 22 |
+
handle_tool_call() — dispatch a tool call
|
| 23 |
+
shutdown() — clean exit
|
| 24 |
+
|
| 25 |
+
Optional hooks (override to opt in):
|
| 26 |
+
on_turn_start(turn, message, **kwargs) — per-turn tick with runtime context
|
| 27 |
+
on_session_end(messages) — end-of-session extraction
|
| 28 |
+
on_pre_compress(messages) -> str — extract before context compression
|
| 29 |
+
on_memory_write(action, target, content) — mirror built-in memory writes
|
| 30 |
+
on_delegation(task, result, **kwargs) — parent-side observation of subagent work
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
from __future__ import annotations
|
| 34 |
+
|
| 35 |
+
import logging
|
| 36 |
+
from abc import ABC, abstractmethod
|
| 37 |
+
from typing import Any, Dict, List
|
| 38 |
+
|
| 39 |
+
logger = logging.getLogger(__name__)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class MemoryProvider(ABC):
|
| 43 |
+
"""Abstract base class for memory providers."""
|
| 44 |
+
|
| 45 |
+
@property
|
| 46 |
+
@abstractmethod
|
| 47 |
+
def name(self) -> str:
|
| 48 |
+
"""Short identifier for this provider (e.g. 'builtin', 'honcho', 'hindsight')."""
|
| 49 |
+
|
| 50 |
+
# -- Core lifecycle (implement these) ------------------------------------
|
| 51 |
+
|
| 52 |
+
@abstractmethod
|
| 53 |
+
def is_available(self) -> bool:
|
| 54 |
+
"""Return True if this provider is configured, has credentials, and is ready.
|
| 55 |
+
|
| 56 |
+
Called during agent init to decide whether to activate the provider.
|
| 57 |
+
Should not make network calls — just check config and installed deps.
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
@abstractmethod
|
| 61 |
+
def initialize(self, session_id: str, **kwargs) -> None:
|
| 62 |
+
"""Initialize for a session.
|
| 63 |
+
|
| 64 |
+
Called once at agent startup. May create resources (banks, tables),
|
| 65 |
+
establish connections, start background threads, etc.
|
| 66 |
+
|
| 67 |
+
kwargs always include:
|
| 68 |
+
- hermes_home (str): The active HERMES_HOME directory path. Use this
|
| 69 |
+
for profile-scoped storage instead of hardcoding ``~/.hermes``.
|
| 70 |
+
- platform (str): "cli", "telegram", "discord", "cron", etc.
|
| 71 |
+
|
| 72 |
+
kwargs may also include:
|
| 73 |
+
- agent_context (str): "primary", "subagent", "cron", or "flush".
|
| 74 |
+
Providers should skip writes for non-primary contexts (cron system
|
| 75 |
+
prompts would corrupt user representations).
|
| 76 |
+
- agent_identity (str): Profile name (e.g. "coder"). Use for
|
| 77 |
+
per-profile provider identity scoping.
|
| 78 |
+
- agent_workspace (str): Shared workspace name (e.g. "hermes").
|
| 79 |
+
- parent_session_id (str): For subagents, the parent's session_id.
|
| 80 |
+
- user_id (str): Platform user identifier (gateway sessions).
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
def system_prompt_block(self) -> str:
|
| 84 |
+
"""Return text to include in the system prompt.
|
| 85 |
+
|
| 86 |
+
Called during system prompt assembly. Return empty string to skip.
|
| 87 |
+
This is for STATIC provider info (instructions, status). Prefetched
|
| 88 |
+
recall context is injected separately via prefetch().
|
| 89 |
+
"""
|
| 90 |
+
return ""
|
| 91 |
+
|
| 92 |
+
def prefetch(self, query: str, *, session_id: str = "") -> str:
|
| 93 |
+
"""Recall relevant context for the upcoming turn.
|
| 94 |
+
|
| 95 |
+
Called before each API call. Return formatted text to inject as
|
| 96 |
+
context, or empty string if nothing relevant. Implementations
|
| 97 |
+
should be fast — use background threads for the actual recall
|
| 98 |
+
and return cached results here.
|
| 99 |
+
|
| 100 |
+
session_id is provided for providers serving concurrent sessions
|
| 101 |
+
(gateway group chats, cached agents). Providers that don't need
|
| 102 |
+
per-session scoping can ignore it.
|
| 103 |
+
"""
|
| 104 |
+
return ""
|
| 105 |
+
|
| 106 |
+
def queue_prefetch(self, query: str, *, session_id: str = "") -> None:
|
| 107 |
+
"""Queue a background recall for the NEXT turn.
|
| 108 |
+
|
| 109 |
+
Called after each turn completes. The result will be consumed
|
| 110 |
+
by prefetch() on the next turn. Default is no-op — providers
|
| 111 |
+
that do background prefetching should override this.
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
def sync_turn(self, user_content: str, assistant_content: str, *, session_id: str = "") -> None:
|
| 115 |
+
"""Persist a completed turn to the backend.
|
| 116 |
+
|
| 117 |
+
Called after each turn. Should be non-blocking — queue for
|
| 118 |
+
background processing if the backend has latency.
|
| 119 |
+
"""
|
| 120 |
+
|
| 121 |
+
@abstractmethod
|
| 122 |
+
def get_tool_schemas(self) -> List[Dict[str, Any]]:
|
| 123 |
+
"""Return tool schemas this provider exposes.
|
| 124 |
+
|
| 125 |
+
Each schema follows the OpenAI function calling format:
|
| 126 |
+
{"name": "...", "description": "...", "parameters": {...}}
|
| 127 |
+
|
| 128 |
+
Return empty list if this provider has no tools (context-only).
|
| 129 |
+
"""
|
| 130 |
+
|
| 131 |
+
def handle_tool_call(self, tool_name: str, args: Dict[str, Any], **kwargs) -> str:
|
| 132 |
+
"""Handle a tool call for one of this provider's tools.
|
| 133 |
+
|
| 134 |
+
Must return a JSON string (the tool result).
|
| 135 |
+
Only called for tool names returned by get_tool_schemas().
|
| 136 |
+
"""
|
| 137 |
+
raise NotImplementedError(f"Provider {self.name} does not handle tool {tool_name}")
|
| 138 |
+
|
| 139 |
+
def shutdown(self) -> None:
|
| 140 |
+
"""Clean shutdown — flush queues, close connections."""
|
| 141 |
+
|
| 142 |
+
# -- Optional hooks (override to opt in) ---------------------------------
|
| 143 |
+
|
| 144 |
+
def on_turn_start(self, turn_number: int, message: str, **kwargs) -> None:
|
| 145 |
+
"""Called at the start of each turn with the user message.
|
| 146 |
+
|
| 147 |
+
Use for turn-counting, scope management, periodic maintenance.
|
| 148 |
+
|
| 149 |
+
kwargs may include: remaining_tokens, model, platform, tool_count.
|
| 150 |
+
Providers use what they need; extras are ignored.
|
| 151 |
+
"""
|
| 152 |
+
|
| 153 |
+
def on_session_end(self, messages: List[Dict[str, Any]]) -> None:
|
| 154 |
+
"""Called when a session ends (explicit exit or timeout).
|
| 155 |
+
|
| 156 |
+
Use for end-of-session fact extraction, summarization, etc.
|
| 157 |
+
messages is the full conversation history.
|
| 158 |
+
|
| 159 |
+
NOT called after every turn — only at actual session boundaries
|
| 160 |
+
(CLI exit, /reset, gateway session expiry).
|
| 161 |
+
"""
|
| 162 |
+
|
| 163 |
+
def on_pre_compress(self, messages: List[Dict[str, Any]]) -> str:
|
| 164 |
+
"""Called before context compression discards old messages.
|
| 165 |
+
|
| 166 |
+
Use to extract insights from messages about to be compressed.
|
| 167 |
+
messages is the list that will be summarized/discarded.
|
| 168 |
+
|
| 169 |
+
Return text to include in the compression summary prompt so the
|
| 170 |
+
compressor preserves provider-extracted insights. Return empty
|
| 171 |
+
string for no contribution (backwards-compatible default).
|
| 172 |
+
"""
|
| 173 |
+
return ""
|
| 174 |
+
|
| 175 |
+
def on_delegation(self, task: str, result: str, *,
|
| 176 |
+
child_session_id: str = "", **kwargs) -> None:
|
| 177 |
+
"""Called on the PARENT agent when a subagent completes.
|
| 178 |
+
|
| 179 |
+
The parent's memory provider gets the task+result pair as an
|
| 180 |
+
observation of what was delegated and what came back. The subagent
|
| 181 |
+
itself has no provider session (skip_memory=True).
|
| 182 |
+
|
| 183 |
+
task: the delegation prompt
|
| 184 |
+
result: the subagent's final response
|
| 185 |
+
child_session_id: the subagent's session_id
|
| 186 |
+
"""
|
| 187 |
+
|
| 188 |
+
def get_config_schema(self) -> List[Dict[str, Any]]:
|
| 189 |
+
"""Return config fields this provider needs for setup.
|
| 190 |
+
|
| 191 |
+
Used by 'hermes memory setup' to walk the user through configuration.
|
| 192 |
+
Each field is a dict with:
|
| 193 |
+
key: config key name (e.g. 'api_key', 'mode')
|
| 194 |
+
description: human-readable description
|
| 195 |
+
secret: True if this should go to .env (default: False)
|
| 196 |
+
required: True if required (default: False)
|
| 197 |
+
default: default value (optional)
|
| 198 |
+
choices: list of valid values (optional)
|
| 199 |
+
url: URL where user can get this credential (optional)
|
| 200 |
+
env_var: explicit env var name for secrets (default: auto-generated)
|
| 201 |
+
|
| 202 |
+
Return empty list if no config needed (e.g. local-only providers).
|
| 203 |
+
"""
|
| 204 |
+
return []
|
| 205 |
+
|
| 206 |
+
def save_config(self, values: Dict[str, Any], hermes_home: str) -> None:
|
| 207 |
+
"""Write non-secret config to the provider's native location.
|
| 208 |
+
|
| 209 |
+
Called by 'hermes memory setup' after collecting user inputs.
|
| 210 |
+
``values`` contains only non-secret fields (secrets go to .env).
|
| 211 |
+
``hermes_home`` is the active HERMES_HOME directory path.
|
| 212 |
+
|
| 213 |
+
Providers with native config files (JSON, YAML) should override
|
| 214 |
+
this to write to their expected location. Providers that use only
|
| 215 |
+
env vars can leave the default (no-op).
|
| 216 |
+
|
| 217 |
+
All new memory provider plugins MUST implement either:
|
| 218 |
+
- save_config() for native config file formats, OR
|
| 219 |
+
- use only env vars (in which case get_config_schema() fields
|
| 220 |
+
should all have ``env_var`` set and this method stays no-op).
|
| 221 |
+
"""
|
| 222 |
+
|
| 223 |
+
def on_memory_write(self, action: str, target: str, content: str) -> None:
|
| 224 |
+
"""Called when the built-in memory tool writes an entry.
|
| 225 |
+
|
| 226 |
+
action: 'add', 'replace', or 'remove'
|
| 227 |
+
target: 'memory' or 'user'
|
| 228 |
+
content: the entry content
|
| 229 |
+
|
| 230 |
+
Use to mirror built-in memory writes to your backend.
|
| 231 |
+
"""
|
agent/model_metadata.py
ADDED
|
@@ -0,0 +1,1085 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Model metadata, context lengths, and token estimation utilities.
|
| 2 |
+
|
| 3 |
+
Pure utility functions with no AIAgent dependency. Used by ContextCompressor
|
| 4 |
+
and run_agent.py for pre-flight context checks.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import logging
|
| 8 |
+
import os
|
| 9 |
+
import re
|
| 10 |
+
import time
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
from typing import Any, Dict, List, Optional
|
| 13 |
+
from urllib.parse import urlparse
|
| 14 |
+
|
| 15 |
+
import requests
|
| 16 |
+
import yaml
|
| 17 |
+
|
| 18 |
+
from hermes_constants import OPENROUTER_MODELS_URL
|
| 19 |
+
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
# Provider names that can appear as a "provider:" prefix before a model ID.
|
| 23 |
+
# Only these are stripped — Ollama-style "model:tag" colons (e.g. "qwen3.5:27b")
|
| 24 |
+
# are preserved so the full model name reaches cache lookups and server queries.
|
| 25 |
+
_PROVIDER_PREFIXES: frozenset[str] = frozenset({
|
| 26 |
+
"openrouter", "nous", "openai-codex", "copilot", "copilot-acp",
|
| 27 |
+
"gemini", "zai", "kimi-coding", "minimax", "minimax-cn", "anthropic", "deepseek",
|
| 28 |
+
"opencode-zen", "opencode-go", "ai-gateway", "kilocode", "alibaba",
|
| 29 |
+
"qwen-oauth",
|
| 30 |
+
"xiaomi",
|
| 31 |
+
"custom", "local",
|
| 32 |
+
# Common aliases
|
| 33 |
+
"google", "google-gemini", "google-ai-studio",
|
| 34 |
+
"glm", "z-ai", "z.ai", "zhipu", "github", "github-copilot",
|
| 35 |
+
"github-models", "kimi", "moonshot", "claude", "deep-seek",
|
| 36 |
+
"opencode", "zen", "go", "vercel", "kilo", "dashscope", "aliyun", "qwen",
|
| 37 |
+
"mimo", "xiaomi-mimo",
|
| 38 |
+
"qwen-portal",
|
| 39 |
+
})
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
_OLLAMA_TAG_PATTERN = re.compile(
|
| 43 |
+
r"^(\d+\.?\d*b|latest|stable|q\d|fp?\d|instruct|chat|coder|vision|text)",
|
| 44 |
+
re.IGNORECASE,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _strip_provider_prefix(model: str) -> str:
|
| 49 |
+
"""Strip a recognised provider prefix from a model string.
|
| 50 |
+
|
| 51 |
+
``"local:my-model"`` → ``"my-model"``
|
| 52 |
+
``"qwen3.5:27b"`` → ``"qwen3.5:27b"`` (unchanged — not a provider prefix)
|
| 53 |
+
``"qwen:0.5b"`` → ``"qwen:0.5b"`` (unchanged — Ollama model:tag)
|
| 54 |
+
``"deepseek:latest"``→ ``"deepseek:latest"``(unchanged — Ollama model:tag)
|
| 55 |
+
"""
|
| 56 |
+
if ":" not in model or model.startswith("http"):
|
| 57 |
+
return model
|
| 58 |
+
prefix, suffix = model.split(":", 1)
|
| 59 |
+
prefix_lower = prefix.strip().lower()
|
| 60 |
+
if prefix_lower in _PROVIDER_PREFIXES:
|
| 61 |
+
# Don't strip if suffix looks like an Ollama tag (e.g. "7b", "latest", "q4_0")
|
| 62 |
+
if _OLLAMA_TAG_PATTERN.match(suffix.strip()):
|
| 63 |
+
return model
|
| 64 |
+
return suffix
|
| 65 |
+
return model
|
| 66 |
+
|
| 67 |
+
_model_metadata_cache: Dict[str, Dict[str, Any]] = {}
|
| 68 |
+
_model_metadata_cache_time: float = 0
|
| 69 |
+
_MODEL_CACHE_TTL = 3600
|
| 70 |
+
_endpoint_model_metadata_cache: Dict[str, Dict[str, Dict[str, Any]]] = {}
|
| 71 |
+
_endpoint_model_metadata_cache_time: Dict[str, float] = {}
|
| 72 |
+
_ENDPOINT_MODEL_CACHE_TTL = 300
|
| 73 |
+
|
| 74 |
+
# Descending tiers for context length probing when the model is unknown.
|
| 75 |
+
# We start at 128K (a safe default for most modern models) and step down
|
| 76 |
+
# on context-length errors until one works.
|
| 77 |
+
CONTEXT_PROBE_TIERS = [
|
| 78 |
+
128_000,
|
| 79 |
+
64_000,
|
| 80 |
+
32_000,
|
| 81 |
+
16_000,
|
| 82 |
+
8_000,
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
# Default context length when no detection method succeeds.
|
| 86 |
+
DEFAULT_FALLBACK_CONTEXT = CONTEXT_PROBE_TIERS[0]
|
| 87 |
+
|
| 88 |
+
# Minimum context length required to run Hermes Agent. Models with fewer
|
| 89 |
+
# tokens cannot maintain enough working memory for tool-calling workflows.
|
| 90 |
+
# Sessions, model switches, and cron jobs should reject models below this.
|
| 91 |
+
MINIMUM_CONTEXT_LENGTH = 64_000
|
| 92 |
+
|
| 93 |
+
# Thin fallback defaults — only broad model family patterns.
|
| 94 |
+
# These fire only when provider is unknown AND models.dev/OpenRouter/Anthropic
|
| 95 |
+
# all miss. Replaced the previous 80+ entry dict.
|
| 96 |
+
# For provider-specific context lengths, models.dev is the primary source.
|
| 97 |
+
DEFAULT_CONTEXT_LENGTHS = {
|
| 98 |
+
# Anthropic Claude 4.6 (1M context) — bare IDs only to avoid
|
| 99 |
+
# fuzzy-match collisions (e.g. "anthropic/claude-sonnet-4" is a
|
| 100 |
+
# substring of "anthropic/claude-sonnet-4.6").
|
| 101 |
+
# OpenRouter-prefixed models resolve via OpenRouter live API or models.dev.
|
| 102 |
+
"claude-opus-4-6": 1000000,
|
| 103 |
+
"claude-sonnet-4-6": 1000000,
|
| 104 |
+
"claude-opus-4.6": 1000000,
|
| 105 |
+
"claude-sonnet-4.6": 1000000,
|
| 106 |
+
# Catch-all for older Claude models (must sort after specific entries)
|
| 107 |
+
"claude": 200000,
|
| 108 |
+
# OpenAI
|
| 109 |
+
"gpt-4.1": 1047576,
|
| 110 |
+
"gpt-5": 128000,
|
| 111 |
+
"gpt-4": 128000,
|
| 112 |
+
# Google
|
| 113 |
+
"gemini": 1048576,
|
| 114 |
+
# Gemma (open models served via AI Studio)
|
| 115 |
+
"gemma-4-31b": 256000,
|
| 116 |
+
"gemma-4-26b": 256000,
|
| 117 |
+
"gemma-3": 131072,
|
| 118 |
+
"gemma": 8192, # fallback for older gemma models
|
| 119 |
+
# DeepSeek
|
| 120 |
+
"deepseek": 128000,
|
| 121 |
+
# Meta
|
| 122 |
+
"llama": 131072,
|
| 123 |
+
# Qwen — specific model families before the catch-all.
|
| 124 |
+
# Official docs: https://help.aliyun.com/zh/model-studio/developer-reference/
|
| 125 |
+
"qwen3-coder-plus": 1000000, # 1M context
|
| 126 |
+
"qwen3-coder": 262144, # 256K context
|
| 127 |
+
"qwen": 131072,
|
| 128 |
+
# MiniMax — official docs: 204,800 context for all models
|
| 129 |
+
# https://platform.minimax.io/docs/api-reference/text-anthropic-api
|
| 130 |
+
"minimax": 204800,
|
| 131 |
+
# GLM
|
| 132 |
+
"glm": 202752,
|
| 133 |
+
# xAI Grok — xAI /v1/models does not return context_length metadata,
|
| 134 |
+
# so these hardcoded fallbacks prevent Hermes from probing-down to
|
| 135 |
+
# the default 128k when the user points at https://api.x.ai/v1
|
| 136 |
+
# via a custom provider. Values sourced from models.dev (2026-04).
|
| 137 |
+
# Keys use substring matching (longest-first), so e.g. "grok-4.20"
|
| 138 |
+
# matches "grok-4.20-0309-reasoning" / "-non-reasoning" / "-multi-agent-0309".
|
| 139 |
+
"grok-code-fast": 256000, # grok-code-fast-1
|
| 140 |
+
"grok-4-1-fast": 2000000, # grok-4-1-fast-(non-)reasoning
|
| 141 |
+
"grok-2-vision": 8192, # grok-2-vision, -1212, -latest
|
| 142 |
+
"grok-4-fast": 2000000, # grok-4-fast-(non-)reasoning
|
| 143 |
+
"grok-4.20": 2000000, # grok-4.20-0309-(non-)reasoning, -multi-agent-0309
|
| 144 |
+
"grok-4": 256000, # grok-4, grok-4-0709
|
| 145 |
+
"grok-3": 131072, # grok-3, grok-3-mini, grok-3-fast, grok-3-mini-fast
|
| 146 |
+
"grok-2": 131072, # grok-2, grok-2-1212, grok-2-latest
|
| 147 |
+
"grok": 131072, # catch-all (grok-beta, unknown grok-*)
|
| 148 |
+
# Kimi
|
| 149 |
+
"kimi": 262144,
|
| 150 |
+
# Arcee
|
| 151 |
+
"trinity": 262144,
|
| 152 |
+
# Hugging Face Inference Providers — model IDs use org/name format
|
| 153 |
+
"Qwen/Qwen3.5-397B-A17B": 131072,
|
| 154 |
+
"Qwen/Qwen3.5-35B-A3B": 131072,
|
| 155 |
+
"deepseek-ai/DeepSeek-V3.2": 65536,
|
| 156 |
+
"moonshotai/Kimi-K2.5": 262144,
|
| 157 |
+
"moonshotai/Kimi-K2-Thinking": 262144,
|
| 158 |
+
"MiniMaxAI/MiniMax-M2.5": 204800,
|
| 159 |
+
"XiaomiMiMo/MiMo-V2-Flash": 256000,
|
| 160 |
+
"mimo-v2-pro": 1000000,
|
| 161 |
+
"mimo-v2-omni": 256000,
|
| 162 |
+
"mimo-v2-flash": 256000,
|
| 163 |
+
"zai-org/GLM-5": 202752,
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
_CONTEXT_LENGTH_KEYS = (
|
| 167 |
+
"context_length",
|
| 168 |
+
"context_window",
|
| 169 |
+
"max_context_length",
|
| 170 |
+
"max_position_embeddings",
|
| 171 |
+
"max_model_len",
|
| 172 |
+
"max_input_tokens",
|
| 173 |
+
"max_sequence_length",
|
| 174 |
+
"max_seq_len",
|
| 175 |
+
"n_ctx_train",
|
| 176 |
+
"n_ctx",
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
_MAX_COMPLETION_KEYS = (
|
| 180 |
+
"max_completion_tokens",
|
| 181 |
+
"max_output_tokens",
|
| 182 |
+
"max_tokens",
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
# Local server hostnames / address patterns
|
| 186 |
+
_LOCAL_HOSTS = ("localhost", "127.0.0.1", "::1", "0.0.0.0")
|
| 187 |
+
# Docker / Podman / Lima DNS names that resolve to the host machine
|
| 188 |
+
_CONTAINER_LOCAL_SUFFIXES = (
|
| 189 |
+
".docker.internal",
|
| 190 |
+
".containers.internal",
|
| 191 |
+
".lima.internal",
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def _normalize_base_url(base_url: str) -> str:
|
| 196 |
+
return (base_url or "").strip().rstrip("/")
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def _is_openrouter_base_url(base_url: str) -> bool:
|
| 200 |
+
return "openrouter.ai" in _normalize_base_url(base_url).lower()
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def _is_custom_endpoint(base_url: str) -> bool:
|
| 204 |
+
normalized = _normalize_base_url(base_url)
|
| 205 |
+
return bool(normalized) and not _is_openrouter_base_url(normalized)
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
_URL_TO_PROVIDER: Dict[str, str] = {
|
| 209 |
+
"api.openai.com": "openai",
|
| 210 |
+
"chatgpt.com": "openai",
|
| 211 |
+
"api.anthropic.com": "anthropic",
|
| 212 |
+
"api.z.ai": "zai",
|
| 213 |
+
"api.moonshot.ai": "kimi-coding",
|
| 214 |
+
"api.kimi.com": "kimi-coding",
|
| 215 |
+
"api.minimax": "minimax",
|
| 216 |
+
"dashscope.aliyuncs.com": "alibaba",
|
| 217 |
+
"dashscope-intl.aliyuncs.com": "alibaba",
|
| 218 |
+
"portal.qwen.ai": "qwen-oauth",
|
| 219 |
+
"openrouter.ai": "openrouter",
|
| 220 |
+
"generativelanguage.googleapis.com": "gemini",
|
| 221 |
+
"inference-api.nousresearch.com": "nous",
|
| 222 |
+
"api.deepseek.com": "deepseek",
|
| 223 |
+
"api.githubcopilot.com": "copilot",
|
| 224 |
+
"models.github.ai": "copilot",
|
| 225 |
+
"api.fireworks.ai": "fireworks",
|
| 226 |
+
"opencode.ai": "opencode-go",
|
| 227 |
+
"api.x.ai": "xai",
|
| 228 |
+
"api.xiaomimimo.com": "xiaomi",
|
| 229 |
+
"xiaomimimo.com": "xiaomi",
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def _infer_provider_from_url(base_url: str) -> Optional[str]:
|
| 234 |
+
"""Infer the models.dev provider name from a base URL.
|
| 235 |
+
|
| 236 |
+
This allows context length resolution via models.dev for custom endpoints
|
| 237 |
+
like DashScope (Alibaba), Z.AI, Kimi, etc. without requiring the user to
|
| 238 |
+
explicitly set the provider name in config.
|
| 239 |
+
"""
|
| 240 |
+
normalized = _normalize_base_url(base_url)
|
| 241 |
+
if not normalized:
|
| 242 |
+
return None
|
| 243 |
+
parsed = urlparse(normalized if "://" in normalized else f"https://{normalized}")
|
| 244 |
+
host = parsed.netloc.lower() or parsed.path.lower()
|
| 245 |
+
for url_part, provider in _URL_TO_PROVIDER.items():
|
| 246 |
+
if url_part in host:
|
| 247 |
+
return provider
|
| 248 |
+
return None
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
def _is_known_provider_base_url(base_url: str) -> bool:
|
| 252 |
+
return _infer_provider_from_url(base_url) is not None
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def is_local_endpoint(base_url: str) -> bool:
|
| 256 |
+
"""Return True if base_url points to a local machine (localhost / RFC-1918 / WSL)."""
|
| 257 |
+
normalized = _normalize_base_url(base_url)
|
| 258 |
+
if not normalized:
|
| 259 |
+
return False
|
| 260 |
+
url = normalized if "://" in normalized else f"http://{normalized}"
|
| 261 |
+
try:
|
| 262 |
+
parsed = urlparse(url)
|
| 263 |
+
host = parsed.hostname or ""
|
| 264 |
+
except Exception:
|
| 265 |
+
return False
|
| 266 |
+
if host in _LOCAL_HOSTS:
|
| 267 |
+
return True
|
| 268 |
+
# Docker / Podman / Lima internal DNS names (e.g. host.docker.internal)
|
| 269 |
+
if any(host.endswith(suffix) for suffix in _CONTAINER_LOCAL_SUFFIXES):
|
| 270 |
+
return True
|
| 271 |
+
# RFC-1918 private ranges and link-local
|
| 272 |
+
import ipaddress
|
| 273 |
+
try:
|
| 274 |
+
addr = ipaddress.ip_address(host)
|
| 275 |
+
return addr.is_private or addr.is_loopback or addr.is_link_local
|
| 276 |
+
except ValueError:
|
| 277 |
+
pass
|
| 278 |
+
# Bare IP that looks like a private range (e.g. 172.26.x.x for WSL)
|
| 279 |
+
parts = host.split(".")
|
| 280 |
+
if len(parts) == 4:
|
| 281 |
+
try:
|
| 282 |
+
first, second = int(parts[0]), int(parts[1])
|
| 283 |
+
if first == 10:
|
| 284 |
+
return True
|
| 285 |
+
if first == 172 and 16 <= second <= 31:
|
| 286 |
+
return True
|
| 287 |
+
if first == 192 and second == 168:
|
| 288 |
+
return True
|
| 289 |
+
except ValueError:
|
| 290 |
+
pass
|
| 291 |
+
return False
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
def detect_local_server_type(base_url: str) -> Optional[str]:
|
| 295 |
+
"""Detect which local server is running at base_url by probing known endpoints.
|
| 296 |
+
|
| 297 |
+
Returns one of: "ollama", "lm-studio", "vllm", "llamacpp", or None.
|
| 298 |
+
"""
|
| 299 |
+
import httpx
|
| 300 |
+
|
| 301 |
+
normalized = _normalize_base_url(base_url)
|
| 302 |
+
server_url = normalized
|
| 303 |
+
if server_url.endswith("/v1"):
|
| 304 |
+
server_url = server_url[:-3]
|
| 305 |
+
|
| 306 |
+
try:
|
| 307 |
+
with httpx.Client(timeout=2.0) as client:
|
| 308 |
+
# LM Studio exposes /api/v1/models — check first (most specific)
|
| 309 |
+
try:
|
| 310 |
+
r = client.get(f"{server_url}/api/v1/models")
|
| 311 |
+
if r.status_code == 200:
|
| 312 |
+
return "lm-studio"
|
| 313 |
+
except Exception:
|
| 314 |
+
pass
|
| 315 |
+
# Ollama exposes /api/tags and responds with {"models": [...]}
|
| 316 |
+
# LM Studio returns {"error": "Unexpected endpoint"} with status 200
|
| 317 |
+
# on this path, so we must verify the response contains "models".
|
| 318 |
+
try:
|
| 319 |
+
r = client.get(f"{server_url}/api/tags")
|
| 320 |
+
if r.status_code == 200:
|
| 321 |
+
try:
|
| 322 |
+
data = r.json()
|
| 323 |
+
if "models" in data:
|
| 324 |
+
return "ollama"
|
| 325 |
+
except Exception:
|
| 326 |
+
pass
|
| 327 |
+
except Exception:
|
| 328 |
+
pass
|
| 329 |
+
# llama.cpp exposes /v1/props (older builds used /props without the /v1 prefix)
|
| 330 |
+
try:
|
| 331 |
+
r = client.get(f"{server_url}/v1/props")
|
| 332 |
+
if r.status_code != 200:
|
| 333 |
+
r = client.get(f"{server_url}/props") # fallback for older builds
|
| 334 |
+
if r.status_code == 200 and "default_generation_settings" in r.text:
|
| 335 |
+
return "llamacpp"
|
| 336 |
+
except Exception:
|
| 337 |
+
pass
|
| 338 |
+
# vLLM: /version
|
| 339 |
+
try:
|
| 340 |
+
r = client.get(f"{server_url}/version")
|
| 341 |
+
if r.status_code == 200:
|
| 342 |
+
data = r.json()
|
| 343 |
+
if "version" in data:
|
| 344 |
+
return "vllm"
|
| 345 |
+
except Exception:
|
| 346 |
+
pass
|
| 347 |
+
except Exception:
|
| 348 |
+
pass
|
| 349 |
+
|
| 350 |
+
return None
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
def _iter_nested_dicts(value: Any):
|
| 354 |
+
if isinstance(value, dict):
|
| 355 |
+
yield value
|
| 356 |
+
for nested in value.values():
|
| 357 |
+
yield from _iter_nested_dicts(nested)
|
| 358 |
+
elif isinstance(value, list):
|
| 359 |
+
for item in value:
|
| 360 |
+
yield from _iter_nested_dicts(item)
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
def _coerce_reasonable_int(value: Any, minimum: int = 1024, maximum: int = 10_000_000) -> Optional[int]:
|
| 364 |
+
try:
|
| 365 |
+
if isinstance(value, bool):
|
| 366 |
+
return None
|
| 367 |
+
if isinstance(value, str):
|
| 368 |
+
value = value.strip().replace(",", "")
|
| 369 |
+
result = int(value)
|
| 370 |
+
except (TypeError, ValueError):
|
| 371 |
+
return None
|
| 372 |
+
if minimum <= result <= maximum:
|
| 373 |
+
return result
|
| 374 |
+
return None
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
def _extract_first_int(payload: Dict[str, Any], keys: tuple[str, ...]) -> Optional[int]:
|
| 378 |
+
keyset = {key.lower() for key in keys}
|
| 379 |
+
for mapping in _iter_nested_dicts(payload):
|
| 380 |
+
for key, value in mapping.items():
|
| 381 |
+
if str(key).lower() not in keyset:
|
| 382 |
+
continue
|
| 383 |
+
coerced = _coerce_reasonable_int(value)
|
| 384 |
+
if coerced is not None:
|
| 385 |
+
return coerced
|
| 386 |
+
return None
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
def _extract_context_length(payload: Dict[str, Any]) -> Optional[int]:
|
| 390 |
+
return _extract_first_int(payload, _CONTEXT_LENGTH_KEYS)
|
| 391 |
+
|
| 392 |
+
|
| 393 |
+
def _extract_max_completion_tokens(payload: Dict[str, Any]) -> Optional[int]:
|
| 394 |
+
return _extract_first_int(payload, _MAX_COMPLETION_KEYS)
|
| 395 |
+
|
| 396 |
+
|
| 397 |
+
def _extract_pricing(payload: Dict[str, Any]) -> Dict[str, Any]:
|
| 398 |
+
alias_map = {
|
| 399 |
+
"prompt": ("prompt", "input", "input_cost_per_token", "prompt_token_cost"),
|
| 400 |
+
"completion": ("completion", "output", "output_cost_per_token", "completion_token_cost"),
|
| 401 |
+
"request": ("request", "request_cost"),
|
| 402 |
+
"cache_read": ("cache_read", "cached_prompt", "input_cache_read", "cache_read_cost_per_token"),
|
| 403 |
+
"cache_write": ("cache_write", "cache_creation", "input_cache_write", "cache_write_cost_per_token"),
|
| 404 |
+
}
|
| 405 |
+
for mapping in _iter_nested_dicts(payload):
|
| 406 |
+
normalized = {str(key).lower(): value for key, value in mapping.items()}
|
| 407 |
+
if not any(any(alias in normalized for alias in aliases) for aliases in alias_map.values()):
|
| 408 |
+
continue
|
| 409 |
+
pricing: Dict[str, Any] = {}
|
| 410 |
+
for target, aliases in alias_map.items():
|
| 411 |
+
for alias in aliases:
|
| 412 |
+
if alias in normalized and normalized[alias] not in (None, ""):
|
| 413 |
+
pricing[target] = normalized[alias]
|
| 414 |
+
break
|
| 415 |
+
if pricing:
|
| 416 |
+
return pricing
|
| 417 |
+
return {}
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def _add_model_aliases(cache: Dict[str, Dict[str, Any]], model_id: str, entry: Dict[str, Any]) -> None:
|
| 421 |
+
cache[model_id] = entry
|
| 422 |
+
if "/" in model_id:
|
| 423 |
+
bare_model = model_id.split("/", 1)[1]
|
| 424 |
+
cache.setdefault(bare_model, entry)
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
def fetch_model_metadata(force_refresh: bool = False) -> Dict[str, Dict[str, Any]]:
|
| 428 |
+
"""Fetch model metadata from OpenRouter (cached for 1 hour)."""
|
| 429 |
+
global _model_metadata_cache, _model_metadata_cache_time
|
| 430 |
+
|
| 431 |
+
if not force_refresh and _model_metadata_cache and (time.time() - _model_metadata_cache_time) < _MODEL_CACHE_TTL:
|
| 432 |
+
return _model_metadata_cache
|
| 433 |
+
|
| 434 |
+
try:
|
| 435 |
+
response = requests.get(OPENROUTER_MODELS_URL, timeout=10)
|
| 436 |
+
response.raise_for_status()
|
| 437 |
+
data = response.json()
|
| 438 |
+
|
| 439 |
+
cache = {}
|
| 440 |
+
for model in data.get("data", []):
|
| 441 |
+
model_id = model.get("id", "")
|
| 442 |
+
entry = {
|
| 443 |
+
"context_length": model.get("context_length", 128000),
|
| 444 |
+
"max_completion_tokens": model.get("top_provider", {}).get("max_completion_tokens", 4096),
|
| 445 |
+
"name": model.get("name", model_id),
|
| 446 |
+
"pricing": model.get("pricing", {}),
|
| 447 |
+
}
|
| 448 |
+
_add_model_aliases(cache, model_id, entry)
|
| 449 |
+
canonical = model.get("canonical_slug", "")
|
| 450 |
+
if canonical and canonical != model_id:
|
| 451 |
+
_add_model_aliases(cache, canonical, entry)
|
| 452 |
+
|
| 453 |
+
_model_metadata_cache = cache
|
| 454 |
+
_model_metadata_cache_time = time.time()
|
| 455 |
+
logger.debug("Fetched metadata for %s models from OpenRouter", len(cache))
|
| 456 |
+
return cache
|
| 457 |
+
|
| 458 |
+
except Exception as e:
|
| 459 |
+
logging.warning(f"Failed to fetch model metadata from OpenRouter: {e}")
|
| 460 |
+
return _model_metadata_cache or {}
|
| 461 |
+
|
| 462 |
+
|
| 463 |
+
def fetch_endpoint_model_metadata(
|
| 464 |
+
base_url: str,
|
| 465 |
+
api_key: str = "",
|
| 466 |
+
force_refresh: bool = False,
|
| 467 |
+
) -> Dict[str, Dict[str, Any]]:
|
| 468 |
+
"""Fetch model metadata from an OpenAI-compatible ``/models`` endpoint.
|
| 469 |
+
|
| 470 |
+
This is used for explicit custom endpoints where hardcoded global model-name
|
| 471 |
+
defaults are unreliable. Results are cached in memory per base URL.
|
| 472 |
+
"""
|
| 473 |
+
normalized = _normalize_base_url(base_url)
|
| 474 |
+
if not normalized or _is_openrouter_base_url(normalized):
|
| 475 |
+
return {}
|
| 476 |
+
|
| 477 |
+
if not force_refresh:
|
| 478 |
+
cached = _endpoint_model_metadata_cache.get(normalized)
|
| 479 |
+
cached_at = _endpoint_model_metadata_cache_time.get(normalized, 0)
|
| 480 |
+
if cached is not None and (time.time() - cached_at) < _ENDPOINT_MODEL_CACHE_TTL:
|
| 481 |
+
return cached
|
| 482 |
+
|
| 483 |
+
candidates = [normalized]
|
| 484 |
+
if normalized.endswith("/v1"):
|
| 485 |
+
alternate = normalized[:-3].rstrip("/")
|
| 486 |
+
else:
|
| 487 |
+
alternate = normalized + "/v1"
|
| 488 |
+
if alternate and alternate not in candidates:
|
| 489 |
+
candidates.append(alternate)
|
| 490 |
+
|
| 491 |
+
headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
|
| 492 |
+
last_error: Optional[Exception] = None
|
| 493 |
+
|
| 494 |
+
for candidate in candidates:
|
| 495 |
+
url = candidate.rstrip("/") + "/models"
|
| 496 |
+
try:
|
| 497 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 498 |
+
response.raise_for_status()
|
| 499 |
+
payload = response.json()
|
| 500 |
+
cache: Dict[str, Dict[str, Any]] = {}
|
| 501 |
+
for model in payload.get("data", []):
|
| 502 |
+
if not isinstance(model, dict):
|
| 503 |
+
continue
|
| 504 |
+
model_id = model.get("id")
|
| 505 |
+
if not model_id:
|
| 506 |
+
continue
|
| 507 |
+
entry: Dict[str, Any] = {"name": model.get("name", model_id)}
|
| 508 |
+
context_length = _extract_context_length(model)
|
| 509 |
+
if context_length is not None:
|
| 510 |
+
entry["context_length"] = context_length
|
| 511 |
+
max_completion_tokens = _extract_max_completion_tokens(model)
|
| 512 |
+
if max_completion_tokens is not None:
|
| 513 |
+
entry["max_completion_tokens"] = max_completion_tokens
|
| 514 |
+
pricing = _extract_pricing(model)
|
| 515 |
+
if pricing:
|
| 516 |
+
entry["pricing"] = pricing
|
| 517 |
+
_add_model_aliases(cache, model_id, entry)
|
| 518 |
+
|
| 519 |
+
# If this is a llama.cpp server, query /props for actual allocated context
|
| 520 |
+
is_llamacpp = any(
|
| 521 |
+
m.get("owned_by") == "llamacpp"
|
| 522 |
+
for m in payload.get("data", []) if isinstance(m, dict)
|
| 523 |
+
)
|
| 524 |
+
if is_llamacpp:
|
| 525 |
+
try:
|
| 526 |
+
# Try /v1/props first (current llama.cpp); fall back to /props for older builds
|
| 527 |
+
base = candidate.rstrip("/").replace("/v1", "")
|
| 528 |
+
props_resp = requests.get(base + "/v1/props", headers=headers, timeout=5)
|
| 529 |
+
if not props_resp.ok:
|
| 530 |
+
props_resp = requests.get(base + "/props", headers=headers, timeout=5)
|
| 531 |
+
if props_resp.ok:
|
| 532 |
+
props = props_resp.json()
|
| 533 |
+
gen_settings = props.get("default_generation_settings", {})
|
| 534 |
+
n_ctx = gen_settings.get("n_ctx")
|
| 535 |
+
model_alias = props.get("model_alias", "")
|
| 536 |
+
if n_ctx and model_alias and model_alias in cache:
|
| 537 |
+
cache[model_alias]["context_length"] = n_ctx
|
| 538 |
+
except Exception:
|
| 539 |
+
pass
|
| 540 |
+
|
| 541 |
+
_endpoint_model_metadata_cache[normalized] = cache
|
| 542 |
+
_endpoint_model_metadata_cache_time[normalized] = time.time()
|
| 543 |
+
return cache
|
| 544 |
+
except Exception as exc:
|
| 545 |
+
last_error = exc
|
| 546 |
+
|
| 547 |
+
if last_error:
|
| 548 |
+
logger.debug("Failed to fetch model metadata from %s/models: %s", normalized, last_error)
|
| 549 |
+
_endpoint_model_metadata_cache[normalized] = {}
|
| 550 |
+
_endpoint_model_metadata_cache_time[normalized] = time.time()
|
| 551 |
+
return {}
|
| 552 |
+
|
| 553 |
+
|
| 554 |
+
def _get_context_cache_path() -> Path:
|
| 555 |
+
"""Return path to the persistent context length cache file."""
|
| 556 |
+
from hermes_constants import get_hermes_home
|
| 557 |
+
return get_hermes_home() / "context_length_cache.yaml"
|
| 558 |
+
|
| 559 |
+
|
| 560 |
+
def _load_context_cache() -> Dict[str, int]:
|
| 561 |
+
"""Load the model+provider -> context_length cache from disk."""
|
| 562 |
+
path = _get_context_cache_path()
|
| 563 |
+
if not path.exists():
|
| 564 |
+
return {}
|
| 565 |
+
try:
|
| 566 |
+
with open(path) as f:
|
| 567 |
+
data = yaml.safe_load(f) or {}
|
| 568 |
+
return data.get("context_lengths", {})
|
| 569 |
+
except Exception as e:
|
| 570 |
+
logger.debug("Failed to load context length cache: %s", e)
|
| 571 |
+
return {}
|
| 572 |
+
|
| 573 |
+
|
| 574 |
+
def save_context_length(model: str, base_url: str, length: int) -> None:
|
| 575 |
+
"""Persist a discovered context length for a model+provider combo.
|
| 576 |
+
|
| 577 |
+
Cache key is ``model@base_url`` so the same model name served from
|
| 578 |
+
different providers can have different limits.
|
| 579 |
+
"""
|
| 580 |
+
key = f"{model}@{base_url}"
|
| 581 |
+
cache = _load_context_cache()
|
| 582 |
+
if cache.get(key) == length:
|
| 583 |
+
return # already stored
|
| 584 |
+
cache[key] = length
|
| 585 |
+
path = _get_context_cache_path()
|
| 586 |
+
try:
|
| 587 |
+
path.parent.mkdir(parents=True, exist_ok=True)
|
| 588 |
+
with open(path, "w") as f:
|
| 589 |
+
yaml.dump({"context_lengths": cache}, f, default_flow_style=False)
|
| 590 |
+
logger.info("Cached context length %s -> %s tokens", key, f"{length:,}")
|
| 591 |
+
except Exception as e:
|
| 592 |
+
logger.debug("Failed to save context length cache: %s", e)
|
| 593 |
+
|
| 594 |
+
|
| 595 |
+
def get_cached_context_length(model: str, base_url: str) -> Optional[int]:
|
| 596 |
+
"""Look up a previously discovered context length for model+provider."""
|
| 597 |
+
key = f"{model}@{base_url}"
|
| 598 |
+
cache = _load_context_cache()
|
| 599 |
+
return cache.get(key)
|
| 600 |
+
|
| 601 |
+
|
| 602 |
+
def get_next_probe_tier(current_length: int) -> Optional[int]:
|
| 603 |
+
"""Return the next lower probe tier, or None if already at minimum."""
|
| 604 |
+
for tier in CONTEXT_PROBE_TIERS:
|
| 605 |
+
if tier < current_length:
|
| 606 |
+
return tier
|
| 607 |
+
return None
|
| 608 |
+
|
| 609 |
+
|
| 610 |
+
def parse_context_limit_from_error(error_msg: str) -> Optional[int]:
|
| 611 |
+
"""Try to extract the actual context limit from an API error message.
|
| 612 |
+
|
| 613 |
+
Many providers include the limit in their error text, e.g.:
|
| 614 |
+
- "maximum context length is 32768 tokens"
|
| 615 |
+
- "context_length_exceeded: 131072"
|
| 616 |
+
- "Maximum context size 32768 exceeded"
|
| 617 |
+
- "model's max context length is 65536"
|
| 618 |
+
"""
|
| 619 |
+
error_lower = error_msg.lower()
|
| 620 |
+
# Pattern: look for numbers near context-related keywords
|
| 621 |
+
patterns = [
|
| 622 |
+
r'(?:max(?:imum)?|limit)\s*(?:context\s*)?(?:length|size|window)?\s*(?:is|of|:)?\s*(\d{4,})',
|
| 623 |
+
r'context\s*(?:length|size|window)\s*(?:is|of|:)?\s*(\d{4,})',
|
| 624 |
+
r'(\d{4,})\s*(?:token)?\s*(?:context|limit)',
|
| 625 |
+
r'>\s*(\d{4,})\s*(?:max|limit|token)', # "250000 tokens > 200000 maximum"
|
| 626 |
+
r'(\d{4,})\s*(?:max(?:imum)?)\b', # "200000 maximum"
|
| 627 |
+
]
|
| 628 |
+
for pattern in patterns:
|
| 629 |
+
match = re.search(pattern, error_lower)
|
| 630 |
+
if match:
|
| 631 |
+
limit = int(match.group(1))
|
| 632 |
+
# Sanity check: must be a reasonable context length
|
| 633 |
+
if 1024 <= limit <= 10_000_000:
|
| 634 |
+
return limit
|
| 635 |
+
return None
|
| 636 |
+
|
| 637 |
+
|
| 638 |
+
def parse_available_output_tokens_from_error(error_msg: str) -> Optional[int]:
|
| 639 |
+
"""Detect an "output cap too large" error and return how many output tokens are available.
|
| 640 |
+
|
| 641 |
+
Background — two distinct context errors exist:
|
| 642 |
+
1. "Prompt too long" — the INPUT itself exceeds the context window.
|
| 643 |
+
Fix: compress history and/or halve context_length.
|
| 644 |
+
2. "max_tokens too large" — input is fine, but input + requested_output > window.
|
| 645 |
+
Fix: reduce max_tokens (the output cap) for this call.
|
| 646 |
+
Do NOT touch context_length — the window hasn't shrunk.
|
| 647 |
+
|
| 648 |
+
Anthropic's API returns errors like:
|
| 649 |
+
"max_tokens: 32768 > context_window: 200000 - input_tokens: 190000 = available_tokens: 10000"
|
| 650 |
+
|
| 651 |
+
Returns the number of output tokens that would fit (e.g. 10000 above), or None if
|
| 652 |
+
the error does not look like a max_tokens-too-large error.
|
| 653 |
+
"""
|
| 654 |
+
error_lower = error_msg.lower()
|
| 655 |
+
|
| 656 |
+
# Must look like an output-cap error, not a prompt-length error.
|
| 657 |
+
is_output_cap_error = (
|
| 658 |
+
"max_tokens" in error_lower
|
| 659 |
+
and ("available_tokens" in error_lower or "available tokens" in error_lower)
|
| 660 |
+
)
|
| 661 |
+
if not is_output_cap_error:
|
| 662 |
+
return None
|
| 663 |
+
|
| 664 |
+
# Extract the available_tokens figure.
|
| 665 |
+
# Anthropic format: "… = available_tokens: 10000"
|
| 666 |
+
patterns = [
|
| 667 |
+
r'available_tokens[:\s]+(\d+)',
|
| 668 |
+
r'available\s+tokens[:\s]+(\d+)',
|
| 669 |
+
# fallback: last number after "=" in expressions like "200000 - 190000 = 10000"
|
| 670 |
+
r'=\s*(\d+)\s*$',
|
| 671 |
+
]
|
| 672 |
+
for pattern in patterns:
|
| 673 |
+
match = re.search(pattern, error_lower)
|
| 674 |
+
if match:
|
| 675 |
+
tokens = int(match.group(1))
|
| 676 |
+
if tokens >= 1:
|
| 677 |
+
return tokens
|
| 678 |
+
return None
|
| 679 |
+
|
| 680 |
+
|
| 681 |
+
def _model_id_matches(candidate_id: str, lookup_model: str) -> bool:
|
| 682 |
+
"""Return True if *candidate_id* (from server) matches *lookup_model* (configured).
|
| 683 |
+
|
| 684 |
+
Supports two forms:
|
| 685 |
+
- Exact match: "nvidia-nemotron-super-49b-v1" == "nvidia-nemotron-super-49b-v1"
|
| 686 |
+
- Slug match: "nvidia/nvidia-nemotron-super-49b-v1" matches "nvidia-nemotron-super-49b-v1"
|
| 687 |
+
(the part after the last "/" equals lookup_model)
|
| 688 |
+
|
| 689 |
+
This covers LM Studio's native API which stores models as "publisher/slug"
|
| 690 |
+
while users typically configure only the slug after the "local:" prefix.
|
| 691 |
+
"""
|
| 692 |
+
if candidate_id == lookup_model:
|
| 693 |
+
return True
|
| 694 |
+
# Slug match: basename of candidate equals the lookup name
|
| 695 |
+
if "/" in candidate_id and candidate_id.rsplit("/", 1)[1] == lookup_model:
|
| 696 |
+
return True
|
| 697 |
+
return False
|
| 698 |
+
|
| 699 |
+
|
| 700 |
+
def query_ollama_num_ctx(model: str, base_url: str) -> Optional[int]:
|
| 701 |
+
"""Query an Ollama server for the model's context length.
|
| 702 |
+
|
| 703 |
+
Returns the model's maximum context from GGUF metadata via ``/api/show``,
|
| 704 |
+
or the explicit ``num_ctx`` from the Modelfile if set. Returns None if
|
| 705 |
+
the server is unreachable or not Ollama.
|
| 706 |
+
|
| 707 |
+
This is the value that should be passed as ``num_ctx`` in Ollama chat
|
| 708 |
+
requests to override the default 2048.
|
| 709 |
+
"""
|
| 710 |
+
import httpx
|
| 711 |
+
|
| 712 |
+
bare_model = _strip_provider_prefix(model)
|
| 713 |
+
server_url = base_url.rstrip("/")
|
| 714 |
+
if server_url.endswith("/v1"):
|
| 715 |
+
server_url = server_url[:-3]
|
| 716 |
+
|
| 717 |
+
try:
|
| 718 |
+
server_type = detect_local_server_type(base_url)
|
| 719 |
+
except Exception:
|
| 720 |
+
return None
|
| 721 |
+
if server_type != "ollama":
|
| 722 |
+
return None
|
| 723 |
+
|
| 724 |
+
try:
|
| 725 |
+
with httpx.Client(timeout=3.0) as client:
|
| 726 |
+
resp = client.post(f"{server_url}/api/show", json={"name": bare_model})
|
| 727 |
+
if resp.status_code != 200:
|
| 728 |
+
return None
|
| 729 |
+
data = resp.json()
|
| 730 |
+
|
| 731 |
+
# Prefer explicit num_ctx from Modelfile parameters (user override)
|
| 732 |
+
params = data.get("parameters", "")
|
| 733 |
+
if "num_ctx" in params:
|
| 734 |
+
for line in params.split("\n"):
|
| 735 |
+
if "num_ctx" in line:
|
| 736 |
+
parts = line.strip().split()
|
| 737 |
+
if len(parts) >= 2:
|
| 738 |
+
try:
|
| 739 |
+
return int(parts[-1])
|
| 740 |
+
except ValueError:
|
| 741 |
+
pass
|
| 742 |
+
|
| 743 |
+
# Fall back to GGUF model_info context_length (training max)
|
| 744 |
+
model_info = data.get("model_info", {})
|
| 745 |
+
for key, value in model_info.items():
|
| 746 |
+
if "context_length" in key and isinstance(value, (int, float)):
|
| 747 |
+
return int(value)
|
| 748 |
+
except Exception:
|
| 749 |
+
pass
|
| 750 |
+
return None
|
| 751 |
+
|
| 752 |
+
|
| 753 |
+
def _query_local_context_length(model: str, base_url: str) -> Optional[int]:
|
| 754 |
+
"""Query a local server for the model's context length."""
|
| 755 |
+
import httpx
|
| 756 |
+
|
| 757 |
+
# Strip recognised provider prefix (e.g., "local:model-name" → "model-name").
|
| 758 |
+
# Ollama "model:tag" colons (e.g. "qwen3.5:27b") are intentionally preserved.
|
| 759 |
+
model = _strip_provider_prefix(model)
|
| 760 |
+
|
| 761 |
+
# Strip /v1 suffix to get the server root
|
| 762 |
+
server_url = base_url.rstrip("/")
|
| 763 |
+
if server_url.endswith("/v1"):
|
| 764 |
+
server_url = server_url[:-3]
|
| 765 |
+
|
| 766 |
+
try:
|
| 767 |
+
server_type = detect_local_server_type(base_url)
|
| 768 |
+
except Exception:
|
| 769 |
+
server_type = None
|
| 770 |
+
|
| 771 |
+
try:
|
| 772 |
+
with httpx.Client(timeout=3.0) as client:
|
| 773 |
+
# Ollama: /api/show returns model details with context info
|
| 774 |
+
if server_type == "ollama":
|
| 775 |
+
resp = client.post(f"{server_url}/api/show", json={"name": model})
|
| 776 |
+
if resp.status_code == 200:
|
| 777 |
+
data = resp.json()
|
| 778 |
+
# Check model_info for context length
|
| 779 |
+
model_info = data.get("model_info", {})
|
| 780 |
+
for key, value in model_info.items():
|
| 781 |
+
if "context_length" in key and isinstance(value, (int, float)):
|
| 782 |
+
return int(value)
|
| 783 |
+
# Check parameters string for num_ctx
|
| 784 |
+
params = data.get("parameters", "")
|
| 785 |
+
if "num_ctx" in params:
|
| 786 |
+
for line in params.split("\n"):
|
| 787 |
+
if "num_ctx" in line:
|
| 788 |
+
parts = line.strip().split()
|
| 789 |
+
if len(parts) >= 2:
|
| 790 |
+
try:
|
| 791 |
+
return int(parts[-1])
|
| 792 |
+
except ValueError:
|
| 793 |
+
pass
|
| 794 |
+
|
| 795 |
+
# LM Studio native API: /api/v1/models returns max_context_length.
|
| 796 |
+
# This is more reliable than the OpenAI-compat /v1/models which
|
| 797 |
+
# doesn't include context window information for LM Studio servers.
|
| 798 |
+
# Use _model_id_matches for fuzzy matching: LM Studio stores models as
|
| 799 |
+
# "publisher/slug" but users configure only "slug" after "local:" prefix.
|
| 800 |
+
if server_type == "lm-studio":
|
| 801 |
+
resp = client.get(f"{server_url}/api/v1/models")
|
| 802 |
+
if resp.status_code == 200:
|
| 803 |
+
data = resp.json()
|
| 804 |
+
for m in data.get("models", []):
|
| 805 |
+
if _model_id_matches(m.get("key", ""), model) or _model_id_matches(m.get("id", ""), model):
|
| 806 |
+
# Prefer loaded instance context (actual runtime value)
|
| 807 |
+
for inst in m.get("loaded_instances", []):
|
| 808 |
+
cfg = inst.get("config", {})
|
| 809 |
+
ctx = cfg.get("context_length")
|
| 810 |
+
if ctx and isinstance(ctx, (int, float)):
|
| 811 |
+
return int(ctx)
|
| 812 |
+
# Fall back to max_context_length (theoretical model max)
|
| 813 |
+
ctx = m.get("max_context_length") or m.get("context_length")
|
| 814 |
+
if ctx and isinstance(ctx, (int, float)):
|
| 815 |
+
return int(ctx)
|
| 816 |
+
|
| 817 |
+
# LM Studio / vLLM / llama.cpp: try /v1/models/{model}
|
| 818 |
+
resp = client.get(f"{server_url}/v1/models/{model}")
|
| 819 |
+
if resp.status_code == 200:
|
| 820 |
+
data = resp.json()
|
| 821 |
+
# vLLM returns max_model_len
|
| 822 |
+
ctx = data.get("max_model_len") or data.get("context_length") or data.get("max_tokens")
|
| 823 |
+
if ctx and isinstance(ctx, (int, float)):
|
| 824 |
+
return int(ctx)
|
| 825 |
+
|
| 826 |
+
# Try /v1/models and find the model in the list.
|
| 827 |
+
# Use _model_id_matches to handle "publisher/slug" vs bare "slug".
|
| 828 |
+
resp = client.get(f"{server_url}/v1/models")
|
| 829 |
+
if resp.status_code == 200:
|
| 830 |
+
data = resp.json()
|
| 831 |
+
models_list = data.get("data", [])
|
| 832 |
+
for m in models_list:
|
| 833 |
+
if _model_id_matches(m.get("id", ""), model):
|
| 834 |
+
ctx = m.get("max_model_len") or m.get("context_length") or m.get("max_tokens")
|
| 835 |
+
if ctx and isinstance(ctx, (int, float)):
|
| 836 |
+
return int(ctx)
|
| 837 |
+
except Exception:
|
| 838 |
+
pass
|
| 839 |
+
|
| 840 |
+
return None
|
| 841 |
+
|
| 842 |
+
|
| 843 |
+
def _normalize_model_version(model: str) -> str:
|
| 844 |
+
"""Normalize version separators for matching.
|
| 845 |
+
|
| 846 |
+
Nous uses dashes: claude-opus-4-6, claude-sonnet-4-5
|
| 847 |
+
OpenRouter uses dots: claude-opus-4.6, claude-sonnet-4.5
|
| 848 |
+
Normalize both to dashes for comparison.
|
| 849 |
+
"""
|
| 850 |
+
return model.replace(".", "-")
|
| 851 |
+
|
| 852 |
+
|
| 853 |
+
def _query_anthropic_context_length(model: str, base_url: str, api_key: str) -> Optional[int]:
|
| 854 |
+
"""Query Anthropic's /v1/models endpoint for context length.
|
| 855 |
+
|
| 856 |
+
Only works with regular ANTHROPIC_API_KEY (sk-ant-api*).
|
| 857 |
+
OAuth tokens (sk-ant-oat*) from Claude Code return 401.
|
| 858 |
+
"""
|
| 859 |
+
if not api_key or api_key.startswith("sk-ant-oat"):
|
| 860 |
+
return None # OAuth tokens can't access /v1/models
|
| 861 |
+
try:
|
| 862 |
+
base = base_url.rstrip("/")
|
| 863 |
+
if base.endswith("/v1"):
|
| 864 |
+
base = base[:-3]
|
| 865 |
+
url = f"{base}/v1/models?limit=1000"
|
| 866 |
+
headers = {
|
| 867 |
+
"x-api-key": api_key,
|
| 868 |
+
"anthropic-version": "2023-06-01",
|
| 869 |
+
}
|
| 870 |
+
resp = requests.get(url, headers=headers, timeout=10)
|
| 871 |
+
if resp.status_code != 200:
|
| 872 |
+
return None
|
| 873 |
+
data = resp.json()
|
| 874 |
+
for m in data.get("data", []):
|
| 875 |
+
if m.get("id") == model:
|
| 876 |
+
ctx = m.get("max_input_tokens")
|
| 877 |
+
if isinstance(ctx, int) and ctx > 0:
|
| 878 |
+
return ctx
|
| 879 |
+
except Exception as e:
|
| 880 |
+
logger.debug("Anthropic /v1/models query failed: %s", e)
|
| 881 |
+
return None
|
| 882 |
+
|
| 883 |
+
|
| 884 |
+
def _resolve_nous_context_length(model: str) -> Optional[int]:
|
| 885 |
+
"""Resolve Nous Portal model context length via OpenRouter metadata.
|
| 886 |
+
|
| 887 |
+
Nous model IDs are bare (e.g. 'claude-opus-4-6') while OpenRouter uses
|
| 888 |
+
prefixed IDs (e.g. 'anthropic/claude-opus-4.6'). Try suffix matching
|
| 889 |
+
with version normalization (dot↔dash).
|
| 890 |
+
"""
|
| 891 |
+
metadata = fetch_model_metadata() # OpenRouter cache
|
| 892 |
+
# Exact match first
|
| 893 |
+
if model in metadata:
|
| 894 |
+
return metadata[model].get("context_length")
|
| 895 |
+
|
| 896 |
+
normalized = _normalize_model_version(model).lower()
|
| 897 |
+
|
| 898 |
+
for or_id, entry in metadata.items():
|
| 899 |
+
bare = or_id.split("/", 1)[1] if "/" in or_id else or_id
|
| 900 |
+
if bare.lower() == model.lower() or _normalize_model_version(bare).lower() == normalized:
|
| 901 |
+
return entry.get("context_length")
|
| 902 |
+
|
| 903 |
+
# Partial prefix match for cases like gemini-3-flash → gemini-3-flash-preview
|
| 904 |
+
# Require match to be at a word boundary (followed by -, :, or end of string)
|
| 905 |
+
model_lower = model.lower()
|
| 906 |
+
for or_id, entry in metadata.items():
|
| 907 |
+
bare = or_id.split("/", 1)[1] if "/" in or_id else or_id
|
| 908 |
+
for candidate, query in [(bare.lower(), model_lower), (_normalize_model_version(bare).lower(), normalized)]:
|
| 909 |
+
if candidate.startswith(query) and (
|
| 910 |
+
len(candidate) == len(query) or candidate[len(query)] in "-:."
|
| 911 |
+
):
|
| 912 |
+
return entry.get("context_length")
|
| 913 |
+
|
| 914 |
+
return None
|
| 915 |
+
|
| 916 |
+
|
| 917 |
+
def get_model_context_length(
|
| 918 |
+
model: str,
|
| 919 |
+
base_url: str = "",
|
| 920 |
+
api_key: str = "",
|
| 921 |
+
config_context_length: int | None = None,
|
| 922 |
+
provider: str = "",
|
| 923 |
+
) -> int:
|
| 924 |
+
"""Get the context length for a model.
|
| 925 |
+
|
| 926 |
+
Resolution order:
|
| 927 |
+
0. Explicit config override (model.context_length or custom_providers per-model)
|
| 928 |
+
1. Persistent cache (previously discovered via probing)
|
| 929 |
+
2. Active endpoint metadata (/models for explicit custom endpoints)
|
| 930 |
+
3. Local server query (for local endpoints)
|
| 931 |
+
4. Anthropic /v1/models API (API-key users only, not OAuth)
|
| 932 |
+
5. OpenRouter live API metadata
|
| 933 |
+
6. Nous suffix-match via OpenRouter cache
|
| 934 |
+
7. models.dev registry lookup (provider-aware)
|
| 935 |
+
8. Thin hardcoded defaults (broad family patterns)
|
| 936 |
+
9. Default fallback (128K)
|
| 937 |
+
"""
|
| 938 |
+
# 0. Explicit config override — user knows best
|
| 939 |
+
if config_context_length is not None and isinstance(config_context_length, int) and config_context_length > 0:
|
| 940 |
+
return config_context_length
|
| 941 |
+
|
| 942 |
+
# Normalise provider-prefixed model names (e.g. "local:model-name" →
|
| 943 |
+
# "model-name") so cache lookups and server queries use the bare ID that
|
| 944 |
+
# local servers actually know about. Ollama "model:tag" colons are preserved.
|
| 945 |
+
model = _strip_provider_prefix(model)
|
| 946 |
+
|
| 947 |
+
# 1. Check persistent cache (model+provider)
|
| 948 |
+
if base_url:
|
| 949 |
+
cached = get_cached_context_length(model, base_url)
|
| 950 |
+
if cached is not None:
|
| 951 |
+
return cached
|
| 952 |
+
|
| 953 |
+
# 2. Active endpoint metadata for truly custom/unknown endpoints.
|
| 954 |
+
# Known providers (Copilot, OpenAI, Anthropic, etc.) skip this — their
|
| 955 |
+
# /models endpoint may report a provider-imposed limit (e.g. Copilot
|
| 956 |
+
# returns 128k) instead of the model's full context (400k). models.dev
|
| 957 |
+
# has the correct per-provider values and is checked at step 5+.
|
| 958 |
+
if _is_custom_endpoint(base_url) and not _is_known_provider_base_url(base_url):
|
| 959 |
+
endpoint_metadata = fetch_endpoint_model_metadata(base_url, api_key=api_key)
|
| 960 |
+
matched = endpoint_metadata.get(model)
|
| 961 |
+
if not matched:
|
| 962 |
+
# Single-model servers: if only one model is loaded, use it
|
| 963 |
+
if len(endpoint_metadata) == 1:
|
| 964 |
+
matched = next(iter(endpoint_metadata.values()))
|
| 965 |
+
else:
|
| 966 |
+
# Fuzzy match: substring in either direction
|
| 967 |
+
for key, entry in endpoint_metadata.items():
|
| 968 |
+
if model in key or key in model:
|
| 969 |
+
matched = entry
|
| 970 |
+
break
|
| 971 |
+
if matched:
|
| 972 |
+
context_length = matched.get("context_length")
|
| 973 |
+
if isinstance(context_length, int):
|
| 974 |
+
return context_length
|
| 975 |
+
if not _is_known_provider_base_url(base_url):
|
| 976 |
+
# 3. Try querying local server directly
|
| 977 |
+
if is_local_endpoint(base_url):
|
| 978 |
+
local_ctx = _query_local_context_length(model, base_url)
|
| 979 |
+
if local_ctx and local_ctx > 0:
|
| 980 |
+
save_context_length(model, base_url, local_ctx)
|
| 981 |
+
return local_ctx
|
| 982 |
+
logger.info(
|
| 983 |
+
"Could not detect context length for model %r at %s — "
|
| 984 |
+
"defaulting to %s tokens (probe-down). Set model.context_length "
|
| 985 |
+
"in config.yaml to override.",
|
| 986 |
+
model, base_url, f"{DEFAULT_FALLBACK_CONTEXT:,}",
|
| 987 |
+
)
|
| 988 |
+
return DEFAULT_FALLBACK_CONTEXT
|
| 989 |
+
|
| 990 |
+
# 4. Anthropic /v1/models API (only for regular API keys, not OAuth)
|
| 991 |
+
if provider == "anthropic" or (
|
| 992 |
+
base_url and "api.anthropic.com" in base_url
|
| 993 |
+
):
|
| 994 |
+
ctx = _query_anthropic_context_length(model, base_url or "https://api.anthropic.com", api_key)
|
| 995 |
+
if ctx:
|
| 996 |
+
return ctx
|
| 997 |
+
|
| 998 |
+
# 5. Provider-aware lookups (before generic OpenRouter cache)
|
| 999 |
+
# These are provider-specific and take priority over the generic OR cache,
|
| 1000 |
+
# since the same model can have different context limits per provider
|
| 1001 |
+
# (e.g. claude-opus-4.6 is 1M on Anthropic but 128K on GitHub Copilot).
|
| 1002 |
+
# If provider is generic (openrouter/custom/empty), try to infer from URL.
|
| 1003 |
+
effective_provider = provider
|
| 1004 |
+
if not effective_provider or effective_provider in ("openrouter", "custom"):
|
| 1005 |
+
if base_url:
|
| 1006 |
+
inferred = _infer_provider_from_url(base_url)
|
| 1007 |
+
if inferred:
|
| 1008 |
+
effective_provider = inferred
|
| 1009 |
+
|
| 1010 |
+
if effective_provider == "nous":
|
| 1011 |
+
ctx = _resolve_nous_context_length(model)
|
| 1012 |
+
if ctx:
|
| 1013 |
+
return ctx
|
| 1014 |
+
if effective_provider:
|
| 1015 |
+
from agent.models_dev import lookup_models_dev_context
|
| 1016 |
+
ctx = lookup_models_dev_context(effective_provider, model)
|
| 1017 |
+
if ctx:
|
| 1018 |
+
return ctx
|
| 1019 |
+
|
| 1020 |
+
# 6. OpenRouter live API metadata (provider-unaware fallback)
|
| 1021 |
+
metadata = fetch_model_metadata()
|
| 1022 |
+
if model in metadata:
|
| 1023 |
+
return metadata[model].get("context_length", 128000)
|
| 1024 |
+
|
| 1025 |
+
# 8. Hardcoded defaults (fuzzy match — longest key first for specificity)
|
| 1026 |
+
# Only check `default_model in model` (is the key a substring of the input).
|
| 1027 |
+
# The reverse (`model in default_model`) causes shorter names like
|
| 1028 |
+
# "claude-sonnet-4" to incorrectly match "claude-sonnet-4-6" and return 1M.
|
| 1029 |
+
model_lower = model.lower()
|
| 1030 |
+
for default_model, length in sorted(
|
| 1031 |
+
DEFAULT_CONTEXT_LENGTHS.items(), key=lambda x: len(x[0]), reverse=True
|
| 1032 |
+
):
|
| 1033 |
+
if default_model in model_lower:
|
| 1034 |
+
return length
|
| 1035 |
+
|
| 1036 |
+
# 9. Query local server as last resort
|
| 1037 |
+
if base_url and is_local_endpoint(base_url):
|
| 1038 |
+
local_ctx = _query_local_context_length(model, base_url)
|
| 1039 |
+
if local_ctx and local_ctx > 0:
|
| 1040 |
+
save_context_length(model, base_url, local_ctx)
|
| 1041 |
+
return local_ctx
|
| 1042 |
+
|
| 1043 |
+
# 10. Default fallback — 128K
|
| 1044 |
+
return DEFAULT_FALLBACK_CONTEXT
|
| 1045 |
+
|
| 1046 |
+
|
| 1047 |
+
def estimate_tokens_rough(text: str) -> int:
|
| 1048 |
+
"""Rough token estimate (~4 chars/token) for pre-flight checks.
|
| 1049 |
+
|
| 1050 |
+
Uses ceiling division so short texts (1-3 chars) never estimate as
|
| 1051 |
+
0 tokens, which would cause the compressor and pre-flight checks to
|
| 1052 |
+
systematically undercount when many short tool results are present.
|
| 1053 |
+
"""
|
| 1054 |
+
if not text:
|
| 1055 |
+
return 0
|
| 1056 |
+
return (len(text) + 3) // 4
|
| 1057 |
+
|
| 1058 |
+
|
| 1059 |
+
def estimate_messages_tokens_rough(messages: List[Dict[str, Any]]) -> int:
|
| 1060 |
+
"""Rough token estimate for a message list (pre-flight only)."""
|
| 1061 |
+
total_chars = sum(len(str(msg)) for msg in messages)
|
| 1062 |
+
return (total_chars + 3) // 4
|
| 1063 |
+
|
| 1064 |
+
|
| 1065 |
+
def estimate_request_tokens_rough(
|
| 1066 |
+
messages: List[Dict[str, Any]],
|
| 1067 |
+
*,
|
| 1068 |
+
system_prompt: str = "",
|
| 1069 |
+
tools: Optional[List[Dict[str, Any]]] = None,
|
| 1070 |
+
) -> int:
|
| 1071 |
+
"""Rough token estimate for a full chat-completions request.
|
| 1072 |
+
|
| 1073 |
+
Includes the major payload buckets Hermes sends to providers:
|
| 1074 |
+
system prompt, conversation messages, and tool schemas. With 50+
|
| 1075 |
+
tools enabled, schemas alone can add 20-30K tokens — a significant
|
| 1076 |
+
blind spot when only counting messages.
|
| 1077 |
+
"""
|
| 1078 |
+
total_chars = 0
|
| 1079 |
+
if system_prompt:
|
| 1080 |
+
total_chars += len(system_prompt)
|
| 1081 |
+
if messages:
|
| 1082 |
+
total_chars += sum(len(str(msg)) for msg in messages)
|
| 1083 |
+
if tools:
|
| 1084 |
+
total_chars += len(str(tools))
|
| 1085 |
+
return (total_chars + 3) // 4
|
agent/models_dev.py
ADDED
|
@@ -0,0 +1,678 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Models.dev registry integration — primary database for providers and models.
|
| 2 |
+
|
| 3 |
+
Fetches from https://models.dev/api.json — a community-maintained database
|
| 4 |
+
of 4000+ models across 109+ providers. Provides:
|
| 5 |
+
|
| 6 |
+
- **Provider metadata**: name, base URL, env vars, documentation link
|
| 7 |
+
- **Model metadata**: context window, max output, cost/M tokens, capabilities
|
| 8 |
+
(reasoning, tools, vision, PDF, audio), modalities, knowledge cutoff,
|
| 9 |
+
open-weights flag, family grouping, deprecation status
|
| 10 |
+
|
| 11 |
+
Data resolution order (like TypeScript OpenCode):
|
| 12 |
+
1. Bundled snapshot (ships with the package — offline-first)
|
| 13 |
+
2. Disk cache (~/.hermes/models_dev_cache.json)
|
| 14 |
+
3. Network fetch (https://models.dev/api.json)
|
| 15 |
+
4. Background refresh every 60 minutes
|
| 16 |
+
|
| 17 |
+
Other modules should import the dataclasses and query functions from here
|
| 18 |
+
rather than parsing the raw JSON themselves.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
import difflib
|
| 22 |
+
import json
|
| 23 |
+
import logging
|
| 24 |
+
import os
|
| 25 |
+
import time
|
| 26 |
+
from dataclasses import dataclass
|
| 27 |
+
from pathlib import Path
|
| 28 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 29 |
+
|
| 30 |
+
from utils import atomic_json_write
|
| 31 |
+
|
| 32 |
+
import requests
|
| 33 |
+
|
| 34 |
+
logger = logging.getLogger(__name__)
|
| 35 |
+
|
| 36 |
+
MODELS_DEV_URL = "https://models.dev/api.json"
|
| 37 |
+
_MODELS_DEV_CACHE_TTL = 3600 # 1 hour in-memory
|
| 38 |
+
|
| 39 |
+
# In-memory cache
|
| 40 |
+
_models_dev_cache: Dict[str, Any] = {}
|
| 41 |
+
_models_dev_cache_time: float = 0
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# ---------------------------------------------------------------------------
|
| 45 |
+
# Dataclasses — rich metadata for providers and models
|
| 46 |
+
# ---------------------------------------------------------------------------
|
| 47 |
+
|
| 48 |
+
@dataclass
|
| 49 |
+
class ModelInfo:
|
| 50 |
+
"""Full metadata for a single model from models.dev."""
|
| 51 |
+
|
| 52 |
+
id: str
|
| 53 |
+
name: str
|
| 54 |
+
family: str
|
| 55 |
+
provider_id: str # models.dev provider ID (e.g. "anthropic")
|
| 56 |
+
|
| 57 |
+
# Capabilities
|
| 58 |
+
reasoning: bool = False
|
| 59 |
+
tool_call: bool = False
|
| 60 |
+
attachment: bool = False # supports image/file attachments (vision)
|
| 61 |
+
temperature: bool = False
|
| 62 |
+
structured_output: bool = False
|
| 63 |
+
open_weights: bool = False
|
| 64 |
+
|
| 65 |
+
# Modalities
|
| 66 |
+
input_modalities: Tuple[str, ...] = () # ("text", "image", "pdf", ...)
|
| 67 |
+
output_modalities: Tuple[str, ...] = ()
|
| 68 |
+
|
| 69 |
+
# Limits
|
| 70 |
+
context_window: int = 0
|
| 71 |
+
max_output: int = 0
|
| 72 |
+
max_input: Optional[int] = None
|
| 73 |
+
|
| 74 |
+
# Cost (per million tokens, USD)
|
| 75 |
+
cost_input: float = 0.0
|
| 76 |
+
cost_output: float = 0.0
|
| 77 |
+
cost_cache_read: Optional[float] = None
|
| 78 |
+
cost_cache_write: Optional[float] = None
|
| 79 |
+
|
| 80 |
+
# Metadata
|
| 81 |
+
knowledge_cutoff: str = ""
|
| 82 |
+
release_date: str = ""
|
| 83 |
+
status: str = "" # "alpha", "beta", "deprecated", or ""
|
| 84 |
+
interleaved: Any = False # True or {"field": "reasoning_content"}
|
| 85 |
+
|
| 86 |
+
def has_cost_data(self) -> bool:
|
| 87 |
+
return self.cost_input > 0 or self.cost_output > 0
|
| 88 |
+
|
| 89 |
+
def supports_vision(self) -> bool:
|
| 90 |
+
return self.attachment or "image" in self.input_modalities
|
| 91 |
+
|
| 92 |
+
def supports_pdf(self) -> bool:
|
| 93 |
+
return "pdf" in self.input_modalities
|
| 94 |
+
|
| 95 |
+
def supports_audio_input(self) -> bool:
|
| 96 |
+
return "audio" in self.input_modalities
|
| 97 |
+
|
| 98 |
+
def format_cost(self) -> str:
|
| 99 |
+
"""Human-readable cost string, e.g. '$3.00/M in, $15.00/M out'."""
|
| 100 |
+
if not self.has_cost_data():
|
| 101 |
+
return "unknown"
|
| 102 |
+
parts = [f"${self.cost_input:.2f}/M in", f"${self.cost_output:.2f}/M out"]
|
| 103 |
+
if self.cost_cache_read is not None:
|
| 104 |
+
parts.append(f"cache read ${self.cost_cache_read:.2f}/M")
|
| 105 |
+
return ", ".join(parts)
|
| 106 |
+
|
| 107 |
+
def format_capabilities(self) -> str:
|
| 108 |
+
"""Human-readable capabilities, e.g. 'reasoning, tools, vision, PDF'."""
|
| 109 |
+
caps = []
|
| 110 |
+
if self.reasoning:
|
| 111 |
+
caps.append("reasoning")
|
| 112 |
+
if self.tool_call:
|
| 113 |
+
caps.append("tools")
|
| 114 |
+
if self.supports_vision():
|
| 115 |
+
caps.append("vision")
|
| 116 |
+
if self.supports_pdf():
|
| 117 |
+
caps.append("PDF")
|
| 118 |
+
if self.supports_audio_input():
|
| 119 |
+
caps.append("audio")
|
| 120 |
+
if self.structured_output:
|
| 121 |
+
caps.append("structured output")
|
| 122 |
+
if self.open_weights:
|
| 123 |
+
caps.append("open weights")
|
| 124 |
+
return ", ".join(caps) if caps else "basic"
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
@dataclass
|
| 128 |
+
class ProviderInfo:
|
| 129 |
+
"""Full metadata for a provider from models.dev."""
|
| 130 |
+
|
| 131 |
+
id: str # models.dev provider ID
|
| 132 |
+
name: str # display name
|
| 133 |
+
env: Tuple[str, ...] # env var names for API key
|
| 134 |
+
api: str # base URL
|
| 135 |
+
doc: str = "" # documentation URL
|
| 136 |
+
model_count: int = 0
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# ---------------------------------------------------------------------------
|
| 140 |
+
# Provider ID mapping: Hermes ↔ models.dev
|
| 141 |
+
# ---------------------------------------------------------------------------
|
| 142 |
+
|
| 143 |
+
# Hermes provider names → models.dev provider IDs
|
| 144 |
+
PROVIDER_TO_MODELS_DEV: Dict[str, str] = {
|
| 145 |
+
"openrouter": "openrouter",
|
| 146 |
+
"anthropic": "anthropic",
|
| 147 |
+
"zai": "zai",
|
| 148 |
+
"kimi-coding": "kimi-for-coding",
|
| 149 |
+
"minimax": "minimax",
|
| 150 |
+
"minimax-cn": "minimax-cn",
|
| 151 |
+
"deepseek": "deepseek",
|
| 152 |
+
"alibaba": "alibaba",
|
| 153 |
+
"qwen-oauth": "alibaba",
|
| 154 |
+
"copilot": "github-copilot",
|
| 155 |
+
"ai-gateway": "vercel",
|
| 156 |
+
"opencode-zen": "opencode",
|
| 157 |
+
"opencode-go": "opencode-go",
|
| 158 |
+
"kilocode": "kilo",
|
| 159 |
+
"fireworks": "fireworks-ai",
|
| 160 |
+
"huggingface": "huggingface",
|
| 161 |
+
"gemini": "google",
|
| 162 |
+
"google": "google",
|
| 163 |
+
"xai": "xai",
|
| 164 |
+
"xiaomi": "xiaomi",
|
| 165 |
+
"nvidia": "nvidia",
|
| 166 |
+
"groq": "groq",
|
| 167 |
+
"mistral": "mistral",
|
| 168 |
+
"togetherai": "togetherai",
|
| 169 |
+
"perplexity": "perplexity",
|
| 170 |
+
"cohere": "cohere",
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
# Reverse mapping: models.dev → Hermes (built lazily)
|
| 174 |
+
_MODELS_DEV_TO_PROVIDER: Optional[Dict[str, str]] = None
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def _get_reverse_mapping() -> Dict[str, str]:
|
| 178 |
+
"""Return models.dev ID → Hermes provider ID mapping."""
|
| 179 |
+
global _MODELS_DEV_TO_PROVIDER
|
| 180 |
+
if _MODELS_DEV_TO_PROVIDER is None:
|
| 181 |
+
_MODELS_DEV_TO_PROVIDER = {v: k for k, v in PROVIDER_TO_MODELS_DEV.items()}
|
| 182 |
+
return _MODELS_DEV_TO_PROVIDER
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def _get_cache_path() -> Path:
|
| 186 |
+
"""Return path to disk cache file."""
|
| 187 |
+
from hermes_constants import get_hermes_home
|
| 188 |
+
return get_hermes_home() / "models_dev_cache.json"
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
def _load_disk_cache() -> Dict[str, Any]:
|
| 192 |
+
"""Load models.dev data from disk cache."""
|
| 193 |
+
try:
|
| 194 |
+
cache_path = _get_cache_path()
|
| 195 |
+
if cache_path.exists():
|
| 196 |
+
with open(cache_path, encoding="utf-8") as f:
|
| 197 |
+
return json.load(f)
|
| 198 |
+
except Exception as e:
|
| 199 |
+
logger.debug("Failed to load models.dev disk cache: %s", e)
|
| 200 |
+
return {}
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def _save_disk_cache(data: Dict[str, Any]) -> None:
|
| 204 |
+
"""Save models.dev data to disk cache atomically."""
|
| 205 |
+
try:
|
| 206 |
+
cache_path = _get_cache_path()
|
| 207 |
+
atomic_json_write(cache_path, data, indent=None, separators=(",", ":"))
|
| 208 |
+
except Exception as e:
|
| 209 |
+
logger.debug("Failed to save models.dev disk cache: %s", e)
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def fetch_models_dev(force_refresh: bool = False) -> Dict[str, Any]:
|
| 213 |
+
"""Fetch models.dev registry. In-memory cache (1hr) + disk fallback.
|
| 214 |
+
|
| 215 |
+
Returns the full registry dict keyed by provider ID, or empty dict on failure.
|
| 216 |
+
"""
|
| 217 |
+
global _models_dev_cache, _models_dev_cache_time
|
| 218 |
+
|
| 219 |
+
# Check in-memory cache
|
| 220 |
+
if (
|
| 221 |
+
not force_refresh
|
| 222 |
+
and _models_dev_cache
|
| 223 |
+
and (time.time() - _models_dev_cache_time) < _MODELS_DEV_CACHE_TTL
|
| 224 |
+
):
|
| 225 |
+
return _models_dev_cache
|
| 226 |
+
|
| 227 |
+
# Try network fetch
|
| 228 |
+
try:
|
| 229 |
+
response = requests.get(MODELS_DEV_URL, timeout=15)
|
| 230 |
+
response.raise_for_status()
|
| 231 |
+
data = response.json()
|
| 232 |
+
if isinstance(data, dict) and data:
|
| 233 |
+
_models_dev_cache = data
|
| 234 |
+
_models_dev_cache_time = time.time()
|
| 235 |
+
_save_disk_cache(data)
|
| 236 |
+
logger.debug(
|
| 237 |
+
"Fetched models.dev registry: %d providers, %d total models",
|
| 238 |
+
len(data),
|
| 239 |
+
sum(len(p.get("models", {})) for p in data.values() if isinstance(p, dict)),
|
| 240 |
+
)
|
| 241 |
+
return data
|
| 242 |
+
except Exception as e:
|
| 243 |
+
logger.debug("Failed to fetch models.dev: %s", e)
|
| 244 |
+
|
| 245 |
+
# Fall back to disk cache — use a short TTL (5 min) so we retry
|
| 246 |
+
# the network fetch soon instead of serving stale data for a full hour.
|
| 247 |
+
if not _models_dev_cache:
|
| 248 |
+
_models_dev_cache = _load_disk_cache()
|
| 249 |
+
if _models_dev_cache:
|
| 250 |
+
_models_dev_cache_time = time.time() - _MODELS_DEV_CACHE_TTL + 300
|
| 251 |
+
logger.debug("Loaded models.dev from disk cache (%d providers)", len(_models_dev_cache))
|
| 252 |
+
|
| 253 |
+
return _models_dev_cache
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
def lookup_models_dev_context(provider: str, model: str) -> Optional[int]:
|
| 257 |
+
"""Look up context_length for a provider+model combo in models.dev.
|
| 258 |
+
|
| 259 |
+
Returns the context window in tokens, or None if not found.
|
| 260 |
+
Handles case-insensitive matching and filters out context=0 entries.
|
| 261 |
+
"""
|
| 262 |
+
mdev_provider_id = PROVIDER_TO_MODELS_DEV.get(provider)
|
| 263 |
+
if not mdev_provider_id:
|
| 264 |
+
return None
|
| 265 |
+
|
| 266 |
+
data = fetch_models_dev()
|
| 267 |
+
provider_data = data.get(mdev_provider_id)
|
| 268 |
+
if not isinstance(provider_data, dict):
|
| 269 |
+
return None
|
| 270 |
+
|
| 271 |
+
models = provider_data.get("models", {})
|
| 272 |
+
if not isinstance(models, dict):
|
| 273 |
+
return None
|
| 274 |
+
|
| 275 |
+
# Exact match
|
| 276 |
+
entry = models.get(model)
|
| 277 |
+
if entry:
|
| 278 |
+
ctx = _extract_context(entry)
|
| 279 |
+
if ctx:
|
| 280 |
+
return ctx
|
| 281 |
+
|
| 282 |
+
# Case-insensitive match
|
| 283 |
+
model_lower = model.lower()
|
| 284 |
+
for mid, mdata in models.items():
|
| 285 |
+
if mid.lower() == model_lower:
|
| 286 |
+
ctx = _extract_context(mdata)
|
| 287 |
+
if ctx:
|
| 288 |
+
return ctx
|
| 289 |
+
|
| 290 |
+
return None
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def _extract_context(entry: Dict[str, Any]) -> Optional[int]:
|
| 294 |
+
"""Extract context_length from a models.dev model entry.
|
| 295 |
+
|
| 296 |
+
Returns None for invalid/zero values (some audio/image models have context=0).
|
| 297 |
+
"""
|
| 298 |
+
if not isinstance(entry, dict):
|
| 299 |
+
return None
|
| 300 |
+
limit = entry.get("limit")
|
| 301 |
+
if not isinstance(limit, dict):
|
| 302 |
+
return None
|
| 303 |
+
ctx = limit.get("context")
|
| 304 |
+
if isinstance(ctx, (int, float)) and ctx > 0:
|
| 305 |
+
return int(ctx)
|
| 306 |
+
return None
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
# ---------------------------------------------------------------------------
|
| 310 |
+
# Model capability metadata
|
| 311 |
+
# ---------------------------------------------------------------------------
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
@dataclass
|
| 315 |
+
class ModelCapabilities:
|
| 316 |
+
"""Structured capability metadata for a model from models.dev."""
|
| 317 |
+
|
| 318 |
+
supports_tools: bool = True
|
| 319 |
+
supports_vision: bool = False
|
| 320 |
+
supports_reasoning: bool = False
|
| 321 |
+
context_window: int = 200000
|
| 322 |
+
max_output_tokens: int = 8192
|
| 323 |
+
model_family: str = ""
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
def _get_provider_models(provider: str) -> Optional[Dict[str, Any]]:
|
| 327 |
+
"""Resolve a Hermes provider ID to its models dict from models.dev.
|
| 328 |
+
|
| 329 |
+
Returns the models dict or None if the provider is unknown or has no data.
|
| 330 |
+
"""
|
| 331 |
+
mdev_provider_id = PROVIDER_TO_MODELS_DEV.get(provider)
|
| 332 |
+
if not mdev_provider_id:
|
| 333 |
+
return None
|
| 334 |
+
|
| 335 |
+
data = fetch_models_dev()
|
| 336 |
+
provider_data = data.get(mdev_provider_id)
|
| 337 |
+
if not isinstance(provider_data, dict):
|
| 338 |
+
return None
|
| 339 |
+
|
| 340 |
+
models = provider_data.get("models", {})
|
| 341 |
+
if not isinstance(models, dict):
|
| 342 |
+
return None
|
| 343 |
+
|
| 344 |
+
return models
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
def _find_model_entry(models: Dict[str, Any], model: str) -> Optional[Dict[str, Any]]:
|
| 348 |
+
"""Find a model entry by exact match, then case-insensitive fallback."""
|
| 349 |
+
# Exact match
|
| 350 |
+
entry = models.get(model)
|
| 351 |
+
if isinstance(entry, dict):
|
| 352 |
+
return entry
|
| 353 |
+
|
| 354 |
+
# Case-insensitive match
|
| 355 |
+
model_lower = model.lower()
|
| 356 |
+
for mid, mdata in models.items():
|
| 357 |
+
if mid.lower() == model_lower and isinstance(mdata, dict):
|
| 358 |
+
return mdata
|
| 359 |
+
|
| 360 |
+
return None
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
def get_model_capabilities(provider: str, model: str) -> Optional[ModelCapabilities]:
|
| 364 |
+
"""Look up full capability metadata from models.dev cache.
|
| 365 |
+
|
| 366 |
+
Uses the existing fetch_models_dev() and PROVIDER_TO_MODELS_DEV mapping.
|
| 367 |
+
Returns None if model not found.
|
| 368 |
+
|
| 369 |
+
Extracts from model entry fields:
|
| 370 |
+
- reasoning (bool) → supports_reasoning
|
| 371 |
+
- tool_call (bool) → supports_tools
|
| 372 |
+
- attachment (bool) → supports_vision
|
| 373 |
+
- limit.context (int) → context_window
|
| 374 |
+
- limit.output (int) → max_output_tokens
|
| 375 |
+
- family (str) → model_family
|
| 376 |
+
"""
|
| 377 |
+
models = _get_provider_models(provider)
|
| 378 |
+
if models is None:
|
| 379 |
+
return None
|
| 380 |
+
|
| 381 |
+
entry = _find_model_entry(models, model)
|
| 382 |
+
if entry is None:
|
| 383 |
+
return None
|
| 384 |
+
|
| 385 |
+
# Extract capability flags (default to False if missing)
|
| 386 |
+
supports_tools = bool(entry.get("tool_call", False))
|
| 387 |
+
# Vision: check both the `attachment` flag and `modalities.input` for "image".
|
| 388 |
+
# Some models (e.g. gemma-4) list image in input modalities but not attachment.
|
| 389 |
+
input_mods = entry.get("modalities", {})
|
| 390 |
+
if isinstance(input_mods, dict):
|
| 391 |
+
input_mods = input_mods.get("input", [])
|
| 392 |
+
else:
|
| 393 |
+
input_mods = []
|
| 394 |
+
supports_vision = bool(entry.get("attachment", False)) or "image" in input_mods
|
| 395 |
+
supports_reasoning = bool(entry.get("reasoning", False))
|
| 396 |
+
|
| 397 |
+
# Extract limits
|
| 398 |
+
limit = entry.get("limit", {})
|
| 399 |
+
if not isinstance(limit, dict):
|
| 400 |
+
limit = {}
|
| 401 |
+
|
| 402 |
+
ctx = limit.get("context")
|
| 403 |
+
context_window = int(ctx) if isinstance(ctx, (int, float)) and ctx > 0 else 200000
|
| 404 |
+
|
| 405 |
+
out = limit.get("output")
|
| 406 |
+
max_output_tokens = int(out) if isinstance(out, (int, float)) and out > 0 else 8192
|
| 407 |
+
|
| 408 |
+
model_family = entry.get("family", "") or ""
|
| 409 |
+
|
| 410 |
+
return ModelCapabilities(
|
| 411 |
+
supports_tools=supports_tools,
|
| 412 |
+
supports_vision=supports_vision,
|
| 413 |
+
supports_reasoning=supports_reasoning,
|
| 414 |
+
context_window=context_window,
|
| 415 |
+
max_output_tokens=max_output_tokens,
|
| 416 |
+
model_family=model_family,
|
| 417 |
+
)
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def list_provider_models(provider: str) -> List[str]:
|
| 421 |
+
"""Return all model IDs for a provider from models.dev.
|
| 422 |
+
|
| 423 |
+
Returns an empty list if the provider is unknown or has no data.
|
| 424 |
+
"""
|
| 425 |
+
models = _get_provider_models(provider)
|
| 426 |
+
if models is None:
|
| 427 |
+
return []
|
| 428 |
+
return list(models.keys())
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
# Patterns that indicate non-agentic or noise models (TTS, embedding,
|
| 432 |
+
# dated preview snapshots, live/streaming-only, image-only).
|
| 433 |
+
import re
|
| 434 |
+
_NOISE_PATTERNS: re.Pattern = re.compile(
|
| 435 |
+
r"-tts\b|embedding|live-|-(preview|exp)-\d{2,4}[-_]|"
|
| 436 |
+
r"-image\b|-image-preview\b|-customtools\b",
|
| 437 |
+
re.IGNORECASE,
|
| 438 |
+
)
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
def list_agentic_models(provider: str) -> List[str]:
|
| 442 |
+
"""Return model IDs suitable for agentic use from models.dev.
|
| 443 |
+
|
| 444 |
+
Filters for tool_call=True and excludes noise (TTS, embedding,
|
| 445 |
+
dated preview snapshots, live/streaming, image-only models).
|
| 446 |
+
Returns an empty list on any failure.
|
| 447 |
+
"""
|
| 448 |
+
models = _get_provider_models(provider)
|
| 449 |
+
if models is None:
|
| 450 |
+
return []
|
| 451 |
+
|
| 452 |
+
result = []
|
| 453 |
+
for mid, entry in models.items():
|
| 454 |
+
if not isinstance(entry, dict):
|
| 455 |
+
continue
|
| 456 |
+
if not entry.get("tool_call", False):
|
| 457 |
+
continue
|
| 458 |
+
if _NOISE_PATTERNS.search(mid):
|
| 459 |
+
continue
|
| 460 |
+
result.append(mid)
|
| 461 |
+
return result
|
| 462 |
+
|
| 463 |
+
|
| 464 |
+
def search_models_dev(
|
| 465 |
+
query: str, provider: str = None, limit: int = 5
|
| 466 |
+
) -> List[Dict[str, Any]]:
|
| 467 |
+
"""Fuzzy search across models.dev catalog. Returns matching model entries.
|
| 468 |
+
|
| 469 |
+
Args:
|
| 470 |
+
query: Search string to match against model IDs.
|
| 471 |
+
provider: Optional Hermes provider ID to restrict search scope.
|
| 472 |
+
If None, searches across all providers in PROVIDER_TO_MODELS_DEV.
|
| 473 |
+
limit: Maximum number of results to return.
|
| 474 |
+
|
| 475 |
+
Returns:
|
| 476 |
+
List of dicts, each containing 'provider', 'model_id', and the full
|
| 477 |
+
model 'entry' from models.dev.
|
| 478 |
+
"""
|
| 479 |
+
data = fetch_models_dev()
|
| 480 |
+
if not data:
|
| 481 |
+
return []
|
| 482 |
+
|
| 483 |
+
# Build list of (provider_id, model_id, entry) candidates
|
| 484 |
+
candidates: List[tuple] = []
|
| 485 |
+
|
| 486 |
+
if provider is not None:
|
| 487 |
+
# Search only the specified provider
|
| 488 |
+
mdev_provider_id = PROVIDER_TO_MODELS_DEV.get(provider)
|
| 489 |
+
if not mdev_provider_id:
|
| 490 |
+
return []
|
| 491 |
+
provider_data = data.get(mdev_provider_id, {})
|
| 492 |
+
if isinstance(provider_data, dict):
|
| 493 |
+
models = provider_data.get("models", {})
|
| 494 |
+
if isinstance(models, dict):
|
| 495 |
+
for mid, mdata in models.items():
|
| 496 |
+
candidates.append((provider, mid, mdata))
|
| 497 |
+
else:
|
| 498 |
+
# Search across all mapped providers
|
| 499 |
+
for hermes_prov, mdev_prov in PROVIDER_TO_MODELS_DEV.items():
|
| 500 |
+
provider_data = data.get(mdev_prov, {})
|
| 501 |
+
if isinstance(provider_data, dict):
|
| 502 |
+
models = provider_data.get("models", {})
|
| 503 |
+
if isinstance(models, dict):
|
| 504 |
+
for mid, mdata in models.items():
|
| 505 |
+
candidates.append((hermes_prov, mid, mdata))
|
| 506 |
+
|
| 507 |
+
if not candidates:
|
| 508 |
+
return []
|
| 509 |
+
|
| 510 |
+
# Use difflib for fuzzy matching — case-insensitive comparison
|
| 511 |
+
model_ids_lower = [c[1].lower() for c in candidates]
|
| 512 |
+
query_lower = query.lower()
|
| 513 |
+
|
| 514 |
+
# First try exact substring matches (more intuitive than pure edit-distance)
|
| 515 |
+
substring_matches = []
|
| 516 |
+
for prov, mid, mdata in candidates:
|
| 517 |
+
if query_lower in mid.lower():
|
| 518 |
+
substring_matches.append({"provider": prov, "model_id": mid, "entry": mdata})
|
| 519 |
+
|
| 520 |
+
# Then add difflib fuzzy matches for any remaining slots
|
| 521 |
+
fuzzy_ids = difflib.get_close_matches(
|
| 522 |
+
query_lower, model_ids_lower, n=limit * 2, cutoff=0.4
|
| 523 |
+
)
|
| 524 |
+
|
| 525 |
+
seen_ids: set = set()
|
| 526 |
+
results: List[Dict[str, Any]] = []
|
| 527 |
+
|
| 528 |
+
# Prioritize substring matches
|
| 529 |
+
for match in substring_matches:
|
| 530 |
+
key = (match["provider"], match["model_id"])
|
| 531 |
+
if key not in seen_ids:
|
| 532 |
+
seen_ids.add(key)
|
| 533 |
+
results.append(match)
|
| 534 |
+
if len(results) >= limit:
|
| 535 |
+
return results
|
| 536 |
+
|
| 537 |
+
# Add fuzzy matches
|
| 538 |
+
for fid in fuzzy_ids:
|
| 539 |
+
# Find original-case candidates matching this lowered ID
|
| 540 |
+
for prov, mid, mdata in candidates:
|
| 541 |
+
if mid.lower() == fid:
|
| 542 |
+
key = (prov, mid)
|
| 543 |
+
if key not in seen_ids:
|
| 544 |
+
seen_ids.add(key)
|
| 545 |
+
results.append({"provider": prov, "model_id": mid, "entry": mdata})
|
| 546 |
+
if len(results) >= limit:
|
| 547 |
+
return results
|
| 548 |
+
|
| 549 |
+
return results
|
| 550 |
+
|
| 551 |
+
|
| 552 |
+
# ---------------------------------------------------------------------------
|
| 553 |
+
# Rich dataclass constructors — parse raw models.dev JSON into dataclasses
|
| 554 |
+
# ---------------------------------------------------------------------------
|
| 555 |
+
|
| 556 |
+
def _parse_model_info(model_id: str, raw: Dict[str, Any], provider_id: str) -> ModelInfo:
|
| 557 |
+
"""Convert a raw models.dev model entry dict into a ModelInfo dataclass."""
|
| 558 |
+
limit = raw.get("limit") or {}
|
| 559 |
+
if not isinstance(limit, dict):
|
| 560 |
+
limit = {}
|
| 561 |
+
|
| 562 |
+
cost = raw.get("cost") or {}
|
| 563 |
+
if not isinstance(cost, dict):
|
| 564 |
+
cost = {}
|
| 565 |
+
|
| 566 |
+
modalities = raw.get("modalities") or {}
|
| 567 |
+
if not isinstance(modalities, dict):
|
| 568 |
+
modalities = {}
|
| 569 |
+
|
| 570 |
+
input_mods = modalities.get("input") or []
|
| 571 |
+
output_mods = modalities.get("output") or []
|
| 572 |
+
|
| 573 |
+
ctx = limit.get("context")
|
| 574 |
+
ctx_int = int(ctx) if isinstance(ctx, (int, float)) and ctx > 0 else 0
|
| 575 |
+
out = limit.get("output")
|
| 576 |
+
out_int = int(out) if isinstance(out, (int, float)) and out > 0 else 0
|
| 577 |
+
inp = limit.get("input")
|
| 578 |
+
inp_int = int(inp) if isinstance(inp, (int, float)) and inp > 0 else None
|
| 579 |
+
|
| 580 |
+
return ModelInfo(
|
| 581 |
+
id=model_id,
|
| 582 |
+
name=raw.get("name", "") or model_id,
|
| 583 |
+
family=raw.get("family", "") or "",
|
| 584 |
+
provider_id=provider_id,
|
| 585 |
+
reasoning=bool(raw.get("reasoning", False)),
|
| 586 |
+
tool_call=bool(raw.get("tool_call", False)),
|
| 587 |
+
attachment=bool(raw.get("attachment", False)),
|
| 588 |
+
temperature=bool(raw.get("temperature", False)),
|
| 589 |
+
structured_output=bool(raw.get("structured_output", False)),
|
| 590 |
+
open_weights=bool(raw.get("open_weights", False)),
|
| 591 |
+
input_modalities=tuple(input_mods) if isinstance(input_mods, list) else (),
|
| 592 |
+
output_modalities=tuple(output_mods) if isinstance(output_mods, list) else (),
|
| 593 |
+
context_window=ctx_int,
|
| 594 |
+
max_output=out_int,
|
| 595 |
+
max_input=inp_int,
|
| 596 |
+
cost_input=float(cost.get("input", 0) or 0),
|
| 597 |
+
cost_output=float(cost.get("output", 0) or 0),
|
| 598 |
+
cost_cache_read=float(cost["cache_read"]) if "cache_read" in cost and cost["cache_read"] is not None else None,
|
| 599 |
+
cost_cache_write=float(cost["cache_write"]) if "cache_write" in cost and cost["cache_write"] is not None else None,
|
| 600 |
+
knowledge_cutoff=raw.get("knowledge", "") or "",
|
| 601 |
+
release_date=raw.get("release_date", "") or "",
|
| 602 |
+
status=raw.get("status", "") or "",
|
| 603 |
+
interleaved=raw.get("interleaved", False),
|
| 604 |
+
)
|
| 605 |
+
|
| 606 |
+
|
| 607 |
+
def _parse_provider_info(provider_id: str, raw: Dict[str, Any]) -> ProviderInfo:
|
| 608 |
+
"""Convert a raw models.dev provider entry dict into a ProviderInfo."""
|
| 609 |
+
env = raw.get("env") or []
|
| 610 |
+
models = raw.get("models") or {}
|
| 611 |
+
return ProviderInfo(
|
| 612 |
+
id=provider_id,
|
| 613 |
+
name=raw.get("name", "") or provider_id,
|
| 614 |
+
env=tuple(env) if isinstance(env, list) else (),
|
| 615 |
+
api=raw.get("api", "") or "",
|
| 616 |
+
doc=raw.get("doc", "") or "",
|
| 617 |
+
model_count=len(models) if isinstance(models, dict) else 0,
|
| 618 |
+
)
|
| 619 |
+
|
| 620 |
+
|
| 621 |
+
# ---------------------------------------------------------------------------
|
| 622 |
+
# Provider-level queries
|
| 623 |
+
# ---------------------------------------------------------------------------
|
| 624 |
+
|
| 625 |
+
def get_provider_info(provider_id: str) -> Optional[ProviderInfo]:
|
| 626 |
+
"""Get full provider metadata from models.dev.
|
| 627 |
+
|
| 628 |
+
Accepts either a Hermes provider ID (e.g. "kilocode") or a models.dev
|
| 629 |
+
ID (e.g. "kilo"). Returns None if the provider is not in the catalog.
|
| 630 |
+
"""
|
| 631 |
+
# Resolve Hermes ID → models.dev ID
|
| 632 |
+
mdev_id = PROVIDER_TO_MODELS_DEV.get(provider_id, provider_id)
|
| 633 |
+
|
| 634 |
+
data = fetch_models_dev()
|
| 635 |
+
raw = data.get(mdev_id)
|
| 636 |
+
if not isinstance(raw, dict):
|
| 637 |
+
return None
|
| 638 |
+
|
| 639 |
+
return _parse_provider_info(mdev_id, raw)
|
| 640 |
+
|
| 641 |
+
|
| 642 |
+
# ---------------------------------------------------------------------------
|
| 643 |
+
# Model-level queries (rich ModelInfo)
|
| 644 |
+
# ---------------------------------------------------------------------------
|
| 645 |
+
|
| 646 |
+
def get_model_info(
|
| 647 |
+
provider_id: str, model_id: str
|
| 648 |
+
) -> Optional[ModelInfo]:
|
| 649 |
+
"""Get full model metadata from models.dev.
|
| 650 |
+
|
| 651 |
+
Accepts Hermes or models.dev provider ID. Tries exact match then
|
| 652 |
+
case-insensitive fallback. Returns None if not found.
|
| 653 |
+
"""
|
| 654 |
+
mdev_id = PROVIDER_TO_MODELS_DEV.get(provider_id, provider_id)
|
| 655 |
+
|
| 656 |
+
data = fetch_models_dev()
|
| 657 |
+
pdata = data.get(mdev_id)
|
| 658 |
+
if not isinstance(pdata, dict):
|
| 659 |
+
return None
|
| 660 |
+
|
| 661 |
+
models = pdata.get("models", {})
|
| 662 |
+
if not isinstance(models, dict):
|
| 663 |
+
return None
|
| 664 |
+
|
| 665 |
+
# Exact match
|
| 666 |
+
raw = models.get(model_id)
|
| 667 |
+
if isinstance(raw, dict):
|
| 668 |
+
return _parse_model_info(model_id, raw, mdev_id)
|
| 669 |
+
|
| 670 |
+
# Case-insensitive fallback
|
| 671 |
+
model_lower = model_id.lower()
|
| 672 |
+
for mid, mdata in models.items():
|
| 673 |
+
if mid.lower() == model_lower and isinstance(mdata, dict):
|
| 674 |
+
return _parse_model_info(mid, mdata, mdev_id)
|
| 675 |
+
|
| 676 |
+
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""System prompt assembly -- identity, platform hints, skills index, context files.
|
| 2 |
+
|
| 3 |
+
All functions are stateless. AIAgent._build_system_prompt() calls these to
|
| 4 |
+
assemble pieces, then combines them with memory and ephemeral prompts.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import json
|
| 8 |
+
import logging
|
| 9 |
+
import os
|
| 10 |
+
import re
|
| 11 |
+
import threading
|
| 12 |
+
from collections import OrderedDict
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
from hermes_constants import get_hermes_home, get_skills_dir
|
| 16 |
+
from typing import Optional
|
| 17 |
+
|
| 18 |
+
from agent.skill_utils import (
|
| 19 |
+
extract_skill_conditions,
|
| 20 |
+
extract_skill_description,
|
| 21 |
+
get_all_skills_dirs,
|
| 22 |
+
get_disabled_skill_names,
|
| 23 |
+
iter_skill_index_files,
|
| 24 |
+
parse_frontmatter,
|
| 25 |
+
skill_matches_platform,
|
| 26 |
+
)
|
| 27 |
+
from utils import atomic_json_write
|
| 28 |
+
|
| 29 |
+
logger = logging.getLogger(__name__)
|
| 30 |
+
|
| 31 |
+
# ---------------------------------------------------------------------------
|
| 32 |
+
# Context file scanning — detect prompt injection in AGENTS.md, .cursorrules,
|
| 33 |
+
# SOUL.md before they get injected into the system prompt.
|
| 34 |
+
# ---------------------------------------------------------------------------
|
| 35 |
+
|
| 36 |
+
_CONTEXT_THREAT_PATTERNS = [
|
| 37 |
+
(r'ignore\s+(previous|all|above|prior)\s+instructions', "prompt_injection"),
|
| 38 |
+
(r'do\s+not\s+tell\s+the\s+user', "deception_hide"),
|
| 39 |
+
(r'system\s+prompt\s+override', "sys_prompt_override"),
|
| 40 |
+
(r'disregard\s+(your|all|any)\s+(instructions|rules|guidelines)', "disregard_rules"),
|
| 41 |
+
(r'act\s+as\s+(if|though)\s+you\s+(have\s+no|don\'t\s+have)\s+(restrictions|limits|rules)', "bypass_restrictions"),
|
| 42 |
+
(r'<!--[^>]*(?:ignore|override|system|secret|hidden)[^>]*-->', "html_comment_injection"),
|
| 43 |
+
(r'<\s*div\s+style\s*=\s*["\'][\s\S]*?display\s*:\s*none', "hidden_div"),
|
| 44 |
+
(r'translate\s+.*\s+into\s+.*\s+and\s+(execute|run|eval)', "translate_execute"),
|
| 45 |
+
(r'curl\s+[^\n]*\$\{?\w*(KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL|API)', "exfil_curl"),
|
| 46 |
+
(r'cat\s+[^\n]*(\.env|credentials|\.netrc|\.pgpass)', "read_secrets"),
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
_CONTEXT_INVISIBLE_CHARS = {
|
| 50 |
+
'\u200b', '\u200c', '\u200d', '\u2060', '\ufeff',
|
| 51 |
+
'\u202a', '\u202b', '\u202c', '\u202d', '\u202e',
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def _scan_context_content(content: str, filename: str) -> str:
|
| 56 |
+
"""Scan context file content for injection. Returns sanitized content."""
|
| 57 |
+
findings = []
|
| 58 |
+
|
| 59 |
+
# Check invisible unicode
|
| 60 |
+
for char in _CONTEXT_INVISIBLE_CHARS:
|
| 61 |
+
if char in content:
|
| 62 |
+
findings.append(f"invisible unicode U+{ord(char):04X}")
|
| 63 |
+
|
| 64 |
+
# Check threat patterns
|
| 65 |
+
for pattern, pid in _CONTEXT_THREAT_PATTERNS:
|
| 66 |
+
if re.search(pattern, content, re.IGNORECASE):
|
| 67 |
+
findings.append(pid)
|
| 68 |
+
|
| 69 |
+
if findings:
|
| 70 |
+
logger.warning("Context file %s blocked: %s", filename, ", ".join(findings))
|
| 71 |
+
return f"[BLOCKED: {filename} contained potential prompt injection ({', '.join(findings)}). Content not loaded.]"
|
| 72 |
+
|
| 73 |
+
return content
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def _find_git_root(start: Path) -> Optional[Path]:
|
| 77 |
+
"""Walk *start* and its parents looking for a ``.git`` directory.
|
| 78 |
+
|
| 79 |
+
Returns the directory containing ``.git``, or ``None`` if we hit the
|
| 80 |
+
filesystem root without finding one.
|
| 81 |
+
"""
|
| 82 |
+
current = start.resolve()
|
| 83 |
+
for parent in [current, *current.parents]:
|
| 84 |
+
if (parent / ".git").exists():
|
| 85 |
+
return parent
|
| 86 |
+
return None
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
_HERMES_MD_NAMES = (".hermes.md", "HERMES.md")
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def _find_hermes_md(cwd: Path) -> Optional[Path]:
|
| 93 |
+
"""Discover the nearest ``.hermes.md`` or ``HERMES.md``.
|
| 94 |
+
|
| 95 |
+
Search order: *cwd* first, then each parent directory up to (and
|
| 96 |
+
including) the git repository root. Returns the first match, or
|
| 97 |
+
``None`` if nothing is found.
|
| 98 |
+
"""
|
| 99 |
+
stop_at = _find_git_root(cwd)
|
| 100 |
+
current = cwd.resolve()
|
| 101 |
+
|
| 102 |
+
for directory in [current, *current.parents]:
|
| 103 |
+
for name in _HERMES_MD_NAMES:
|
| 104 |
+
candidate = directory / name
|
| 105 |
+
if candidate.is_file():
|
| 106 |
+
return candidate
|
| 107 |
+
# Stop walking at the git root (or filesystem root).
|
| 108 |
+
if stop_at and directory == stop_at:
|
| 109 |
+
break
|
| 110 |
+
return None
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def _strip_yaml_frontmatter(content: str) -> str:
|
| 114 |
+
"""Remove optional YAML frontmatter (``---`` delimited) from *content*.
|
| 115 |
+
|
| 116 |
+
The frontmatter may contain structured config (model overrides, tool
|
| 117 |
+
settings) that will be handled separately in a future PR. For now we
|
| 118 |
+
strip it so only the human-readable markdown body is injected into the
|
| 119 |
+
system prompt.
|
| 120 |
+
"""
|
| 121 |
+
if content.startswith("---"):
|
| 122 |
+
end = content.find("\n---", 3)
|
| 123 |
+
if end != -1:
|
| 124 |
+
# Skip past the closing --- and any trailing newline
|
| 125 |
+
body = content[end + 4:].lstrip("\n")
|
| 126 |
+
return body if body else content
|
| 127 |
+
return content
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# =========================================================================
|
| 131 |
+
# Constants
|
| 132 |
+
# =========================================================================
|
| 133 |
+
|
| 134 |
+
DEFAULT_AGENT_IDENTITY = (
|
| 135 |
+
"You are Hermes Agent, an intelligent AI assistant created by Nous Research. "
|
| 136 |
+
"You are helpful, knowledgeable, and direct. You assist users with a wide "
|
| 137 |
+
"range of tasks including answering questions, writing and editing code, "
|
| 138 |
+
"analyzing information, creative work, and executing actions via your tools. "
|
| 139 |
+
"You communicate clearly, admit uncertainty when appropriate, and prioritize "
|
| 140 |
+
"being genuinely useful over being verbose unless otherwise directed below. "
|
| 141 |
+
"Be targeted and efficient in your exploration and investigations."
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
MEMORY_GUIDANCE = (
|
| 145 |
+
"You have persistent memory across sessions. Save durable facts using the memory "
|
| 146 |
+
"tool: user preferences, environment details, tool quirks, and stable conventions. "
|
| 147 |
+
"Memory is injected into every turn, so keep it compact and focused on facts that "
|
| 148 |
+
"will still matter later.\n"
|
| 149 |
+
"Prioritize what reduces future user steering — the most valuable memory is one "
|
| 150 |
+
"that prevents the user from having to correct or remind you again. "
|
| 151 |
+
"User preferences and recurring corrections matter more than procedural task details.\n"
|
| 152 |
+
"Do NOT save task progress, session outcomes, completed-work logs, or temporary TODO "
|
| 153 |
+
"state to memory; use session_search to recall those from past transcripts. "
|
| 154 |
+
"If you've discovered a new way to do something, solved a problem that could be "
|
| 155 |
+
"necessary later, save it as a skill with the skill tool."
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
SESSION_SEARCH_GUIDANCE = (
|
| 159 |
+
"When the user references something from a past conversation or you suspect "
|
| 160 |
+
"relevant cross-session context exists, use session_search to recall it before "
|
| 161 |
+
"asking them to repeat themselves."
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
SKILLS_GUIDANCE = (
|
| 165 |
+
"After completing a complex task (5+ tool calls), fixing a tricky error, "
|
| 166 |
+
"or discovering a non-trivial workflow, save the approach as a "
|
| 167 |
+
"skill with skill_manage so you can reuse it next time.\n"
|
| 168 |
+
"When using a skill and finding it outdated, incomplete, or wrong, "
|
| 169 |
+
"patch it immediately with skill_manage(action='patch') — don't wait to be asked. "
|
| 170 |
+
"Skills that aren't maintained become liabilities."
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
TOOL_USE_ENFORCEMENT_GUIDANCE = (
|
| 174 |
+
"# Tool-use enforcement\n"
|
| 175 |
+
"You MUST use your tools to take action — do not describe what you would do "
|
| 176 |
+
"or plan to do without actually doing it. When you say you will perform an "
|
| 177 |
+
"action (e.g. 'I will run the tests', 'Let me check the file', 'I will create "
|
| 178 |
+
"the project'), you MUST immediately make the corresponding tool call in the same "
|
| 179 |
+
"response. Never end your turn with a promise of future action — execute it now.\n"
|
| 180 |
+
"Keep working until the task is actually complete. Do not stop with a summary of "
|
| 181 |
+
"what you plan to do next time. If you have tools available that can accomplish "
|
| 182 |
+
"the task, use them instead of telling the user what you would do.\n"
|
| 183 |
+
"Every response should either (a) contain tool calls that make progress, or "
|
| 184 |
+
"(b) deliver a final result to the user. Responses that only describe intentions "
|
| 185 |
+
"without acting are not acceptable."
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# Model name substrings that trigger tool-use enforcement guidance.
|
| 189 |
+
# Add new patterns here when a model family needs explicit steering.
|
| 190 |
+
TOOL_USE_ENFORCEMENT_MODELS = ("gpt", "codex", "gemini", "gemma", "grok")
|
| 191 |
+
|
| 192 |
+
# OpenAI GPT/Codex-specific execution guidance. Addresses known failure modes
|
| 193 |
+
# where GPT models abandon work on partial results, skip prerequisite lookups,
|
| 194 |
+
# hallucinate instead of using tools, and declare "done" without verification.
|
| 195 |
+
# Inspired by patterns from OpenAI's GPT-5.4 prompting guide & OpenClaw PR #38953.
|
| 196 |
+
OPENAI_MODEL_EXECUTION_GUIDANCE = (
|
| 197 |
+
"# Execution discipline\n"
|
| 198 |
+
"<tool_persistence>\n"
|
| 199 |
+
"- Use tools whenever they improve correctness, completeness, or grounding.\n"
|
| 200 |
+
"- Do not stop early when another tool call would materially improve the result.\n"
|
| 201 |
+
"- If a tool returns empty or partial results, retry with a different query or "
|
| 202 |
+
"strategy before giving up.\n"
|
| 203 |
+
"- Keep calling tools until: (1) the task is complete, AND (2) you have verified "
|
| 204 |
+
"the result.\n"
|
| 205 |
+
"</tool_persistence>\n"
|
| 206 |
+
"\n"
|
| 207 |
+
"<mandatory_tool_use>\n"
|
| 208 |
+
"NEVER answer these from memory or mental computation — ALWAYS use a tool:\n"
|
| 209 |
+
"- Arithmetic, math, calculations → use terminal or execute_code\n"
|
| 210 |
+
"- Hashes, encodings, checksums → use terminal (e.g. sha256sum, base64)\n"
|
| 211 |
+
"- Current time, date, timezone → use terminal (e.g. date)\n"
|
| 212 |
+
"- System state: OS, CPU, memory, disk, ports, processes → use terminal\n"
|
| 213 |
+
"- File contents, sizes, line counts → use read_file, search_files, or terminal\n"
|
| 214 |
+
"- Git history, branches, diffs → use terminal\n"
|
| 215 |
+
"- Current facts (weather, news, versions) → use web_search\n"
|
| 216 |
+
"Your memory and user profile describe the USER, not the system you are "
|
| 217 |
+
"running on. The execution environment may differ from what the user profile "
|
| 218 |
+
"says about their personal setup.\n"
|
| 219 |
+
"</mandatory_tool_use>\n"
|
| 220 |
+
"\n"
|
| 221 |
+
"<act_dont_ask>\n"
|
| 222 |
+
"When a question has an obvious default interpretation, act on it immediately "
|
| 223 |
+
"instead of asking for clarification. Examples:\n"
|
| 224 |
+
"- 'Is port 443 open?' → check THIS machine (don't ask 'open where?')\n"
|
| 225 |
+
"- 'What OS am I running?' → check the live system (don't use user profile)\n"
|
| 226 |
+
"- 'What time is it?' → run `date` (don't guess)\n"
|
| 227 |
+
"Only ask for clarification when the ambiguity genuinely changes what tool "
|
| 228 |
+
"you would call.\n"
|
| 229 |
+
"</act_dont_ask>\n"
|
| 230 |
+
"\n"
|
| 231 |
+
"<prerequisite_checks>\n"
|
| 232 |
+
"- Before taking an action, check whether prerequisite discovery, lookup, or "
|
| 233 |
+
"context-gathering steps are needed.\n"
|
| 234 |
+
"- Do not skip prerequisite steps just because the final action seems obvious.\n"
|
| 235 |
+
"- If a task depends on output from a prior step, resolve that dependency first.\n"
|
| 236 |
+
"</prerequisite_checks>\n"
|
| 237 |
+
"\n"
|
| 238 |
+
"<verification>\n"
|
| 239 |
+
"Before finalizing your response:\n"
|
| 240 |
+
"- Correctness: does the output satisfy every stated requirement?\n"
|
| 241 |
+
"- Grounding: are factual claims backed by tool outputs or provided context?\n"
|
| 242 |
+
"- Formatting: does the output match the requested format or schema?\n"
|
| 243 |
+
"- Safety: if the next step has side effects (file writes, commands, API calls), "
|
| 244 |
+
"confirm scope before executing.\n"
|
| 245 |
+
"</verification>\n"
|
| 246 |
+
"\n"
|
| 247 |
+
"<missing_context>\n"
|
| 248 |
+
"- If required context is missing, do NOT guess or hallucinate an answer.\n"
|
| 249 |
+
"- Use the appropriate lookup tool when missing information is retrievable "
|
| 250 |
+
"(search_files, web_search, read_file, etc.).\n"
|
| 251 |
+
"- Ask a clarifying question only when the information cannot be retrieved by tools.\n"
|
| 252 |
+
"- If you must proceed with incomplete information, label assumptions explicitly.\n"
|
| 253 |
+
"</missing_context>"
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
# Gemini/Gemma-specific operational guidance, adapted from OpenCode's gemini.txt.
|
| 257 |
+
# Injected alongside TOOL_USE_ENFORCEMENT_GUIDANCE when the model is Gemini or Gemma.
|
| 258 |
+
GOOGLE_MODEL_OPERATIONAL_GUIDANCE = (
|
| 259 |
+
"# Google model operational directives\n"
|
| 260 |
+
"Follow these operational rules strictly:\n"
|
| 261 |
+
"- **Absolute paths:** Always construct and use absolute file paths for all "
|
| 262 |
+
"file system operations. Combine the project root with relative paths.\n"
|
| 263 |
+
"- **Verify first:** Use read_file/search_files to check file contents and "
|
| 264 |
+
"project structure before making changes. Never guess at file contents.\n"
|
| 265 |
+
"- **Dependency checks:** Never assume a library is available. Check "
|
| 266 |
+
"package.json, requirements.txt, Cargo.toml, etc. before importing.\n"
|
| 267 |
+
"- **Conciseness:** Keep explanatory text brief — a few sentences, not "
|
| 268 |
+
"paragraphs. Focus on actions and results over narration.\n"
|
| 269 |
+
"- **Parallel tool calls:** When you need to perform multiple independent "
|
| 270 |
+
"operations (e.g. reading several files), make all the tool calls in a "
|
| 271 |
+
"single response rather than sequentially.\n"
|
| 272 |
+
"- **Non-interactive commands:** Use flags like -y, --yes, --non-interactive "
|
| 273 |
+
"to prevent CLI tools from hanging on prompts.\n"
|
| 274 |
+
"- **Keep going:** Work autonomously until the task is fully resolved. "
|
| 275 |
+
"Don't stop with a plan — execute it.\n"
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
# Model name substrings that should use the 'developer' role instead of
|
| 279 |
+
# 'system' for the system prompt. OpenAI's newer models (GPT-5, Codex)
|
| 280 |
+
# give stronger instruction-following weight to the 'developer' role.
|
| 281 |
+
# The swap happens at the API boundary in _build_api_kwargs() so internal
|
| 282 |
+
# message representation stays consistent ("system" everywhere).
|
| 283 |
+
DEVELOPER_ROLE_MODELS = ("gpt-5", "codex")
|
| 284 |
+
|
| 285 |
+
PLATFORM_HINTS = {
|
| 286 |
+
"whatsapp": (
|
| 287 |
+
"You are on a text messaging communication platform, WhatsApp. "
|
| 288 |
+
"Please do not use markdown as it does not render. "
|
| 289 |
+
"You can send media files natively: to deliver a file to the user, "
|
| 290 |
+
"include MEDIA:/absolute/path/to/file in your response. The file "
|
| 291 |
+
"will be sent as a native WhatsApp attachment — images (.jpg, .png, "
|
| 292 |
+
".webp) appear as photos, videos (.mp4, .mov) play inline, and other "
|
| 293 |
+
"files arrive as downloadable documents. You can also include image "
|
| 294 |
+
"URLs in markdown format  and they will be sent as photos."
|
| 295 |
+
),
|
| 296 |
+
"telegram": (
|
| 297 |
+
"You are on a text messaging communication platform, Telegram. "
|
| 298 |
+
"Please do not use markdown as it does not render. "
|
| 299 |
+
"You can send media files natively: to deliver a file to the user, "
|
| 300 |
+
"include MEDIA:/absolute/path/to/file in your response. Images "
|
| 301 |
+
"(.png, .jpg, .webp) appear as photos, audio (.ogg) sends as voice "
|
| 302 |
+
"bubbles, and videos (.mp4) play inline. You can also include image "
|
| 303 |
+
"URLs in markdown format  and they will be sent as native photos."
|
| 304 |
+
),
|
| 305 |
+
"discord": (
|
| 306 |
+
"You are in a Discord server or group chat communicating with your user. "
|
| 307 |
+
"You can send media files natively: include MEDIA:/absolute/path/to/file "
|
| 308 |
+
"in your response. Images (.png, .jpg, .webp) are sent as photo "
|
| 309 |
+
"attachments, audio as file attachments. You can also include image URLs "
|
| 310 |
+
"in markdown format  and they will be sent as attachments."
|
| 311 |
+
),
|
| 312 |
+
"slack": (
|
| 313 |
+
"You are in a Slack workspace communicating with your user. "
|
| 314 |
+
"You can send media files natively: include MEDIA:/absolute/path/to/file "
|
| 315 |
+
"in your response. Images (.png, .jpg, .webp) are uploaded as photo "
|
| 316 |
+
"attachments, audio as file attachments. You can also include image URLs "
|
| 317 |
+
"in markdown format  and they will be uploaded as attachments."
|
| 318 |
+
),
|
| 319 |
+
"signal": (
|
| 320 |
+
"You are on a text messaging communication platform, Signal. "
|
| 321 |
+
"Please do not use markdown as it does not render. "
|
| 322 |
+
"You can send media files natively: to deliver a file to the user, "
|
| 323 |
+
"include MEDIA:/absolute/path/to/file in your response. Images "
|
| 324 |
+
"(.png, .jpg, .webp) appear as photos, audio as attachments, and other "
|
| 325 |
+
"files arrive as downloadable documents. You can also include image "
|
| 326 |
+
"URLs in markdown format  and they will be sent as photos."
|
| 327 |
+
),
|
| 328 |
+
"email": (
|
| 329 |
+
"You are communicating via email. Write clear, well-structured responses "
|
| 330 |
+
"suitable for email. Use plain text formatting (no markdown). "
|
| 331 |
+
"Keep responses concise but complete. You can send file attachments — "
|
| 332 |
+
"include MEDIA:/absolute/path/to/file in your response. The subject line "
|
| 333 |
+
"is preserved for threading. Do not include greetings or sign-offs unless "
|
| 334 |
+
"contextually appropriate."
|
| 335 |
+
),
|
| 336 |
+
"cron": (
|
| 337 |
+
"You are running as a scheduled cron job. There is no user present — you "
|
| 338 |
+
"cannot ask questions, request clarification, or wait for follow-up. Execute "
|
| 339 |
+
"the task fully and autonomously, making reasonable decisions where needed. "
|
| 340 |
+
"Your final response is automatically delivered to the job's configured "
|
| 341 |
+
"destination — put the primary content directly in your response."
|
| 342 |
+
),
|
| 343 |
+
"cli": (
|
| 344 |
+
"You are a CLI AI Agent. Try not to use markdown but simple text "
|
| 345 |
+
"renderable inside a terminal."
|
| 346 |
+
),
|
| 347 |
+
"sms": (
|
| 348 |
+
"You are communicating via SMS. Keep responses concise and use plain text "
|
| 349 |
+
"only — no markdown, no formatting. SMS messages are limited to ~1600 "
|
| 350 |
+
"characters, so be brief and direct."
|
| 351 |
+
),
|
| 352 |
+
"bluebubbles": (
|
| 353 |
+
"You are chatting via iMessage (BlueBubbles). iMessage does not render "
|
| 354 |
+
"markdown formatting — use plain text. Keep responses concise as they "
|
| 355 |
+
"appear as text messages. You can send media files natively: include "
|
| 356 |
+
"MEDIA:/absolute/path/to/file in your response. Images (.jpg, .png, "
|
| 357 |
+
".heic) appear as photos and other files arrive as attachments."
|
| 358 |
+
),
|
| 359 |
+
"weixin": (
|
| 360 |
+
"You are on Weixin/WeChat. Markdown formatting is supported, so you may use it when "
|
| 361 |
+
"it improves readability, but keep the message compact and chat-friendly. You can send media files natively: "
|
| 362 |
+
"include MEDIA:/absolute/path/to/file in your response. Images are sent as native "
|
| 363 |
+
"photos, videos play inline when supported, and other files arrive as downloadable "
|
| 364 |
+
"documents. You can also include image URLs in markdown format  and they "
|
| 365 |
+
"will be downloaded and sent as native media when possible."
|
| 366 |
+
),
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
CONTEXT_FILE_MAX_CHARS = 20_000
|
| 370 |
+
CONTEXT_TRUNCATE_HEAD_RATIO = 0.7
|
| 371 |
+
CONTEXT_TRUNCATE_TAIL_RATIO = 0.2
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
# =========================================================================
|
| 375 |
+
# Skills prompt cache
|
| 376 |
+
# =========================================================================
|
| 377 |
+
|
| 378 |
+
_SKILLS_PROMPT_CACHE_MAX = 8
|
| 379 |
+
_SKILLS_PROMPT_CACHE: OrderedDict[tuple, str] = OrderedDict()
|
| 380 |
+
_SKILLS_PROMPT_CACHE_LOCK = threading.Lock()
|
| 381 |
+
_SKILLS_SNAPSHOT_VERSION = 1
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
def _skills_prompt_snapshot_path() -> Path:
|
| 385 |
+
return get_hermes_home() / ".skills_prompt_snapshot.json"
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
def clear_skills_system_prompt_cache(*, clear_snapshot: bool = False) -> None:
|
| 389 |
+
"""Drop the in-process skills prompt cache (and optionally the disk snapshot)."""
|
| 390 |
+
with _SKILLS_PROMPT_CACHE_LOCK:
|
| 391 |
+
_SKILLS_PROMPT_CACHE.clear()
|
| 392 |
+
if clear_snapshot:
|
| 393 |
+
try:
|
| 394 |
+
_skills_prompt_snapshot_path().unlink(missing_ok=True)
|
| 395 |
+
except OSError as e:
|
| 396 |
+
logger.debug("Could not remove skills prompt snapshot: %s", e)
|
| 397 |
+
|
| 398 |
+
|
| 399 |
+
def _build_skills_manifest(skills_dir: Path) -> dict[str, list[int]]:
|
| 400 |
+
"""Build an mtime/size manifest of all SKILL.md and DESCRIPTION.md files."""
|
| 401 |
+
manifest: dict[str, list[int]] = {}
|
| 402 |
+
for filename in ("SKILL.md", "DESCRIPTION.md"):
|
| 403 |
+
for path in iter_skill_index_files(skills_dir, filename):
|
| 404 |
+
try:
|
| 405 |
+
st = path.stat()
|
| 406 |
+
except OSError:
|
| 407 |
+
continue
|
| 408 |
+
manifest[str(path.relative_to(skills_dir))] = [st.st_mtime_ns, st.st_size]
|
| 409 |
+
return manifest
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
def _load_skills_snapshot(skills_dir: Path) -> Optional[dict]:
|
| 413 |
+
"""Load the disk snapshot if it exists and its manifest still matches."""
|
| 414 |
+
snapshot_path = _skills_prompt_snapshot_path()
|
| 415 |
+
if not snapshot_path.exists():
|
| 416 |
+
return None
|
| 417 |
+
try:
|
| 418 |
+
snapshot = json.loads(snapshot_path.read_text(encoding="utf-8"))
|
| 419 |
+
except Exception:
|
| 420 |
+
return None
|
| 421 |
+
if not isinstance(snapshot, dict):
|
| 422 |
+
return None
|
| 423 |
+
if snapshot.get("version") != _SKILLS_SNAPSHOT_VERSION:
|
| 424 |
+
return None
|
| 425 |
+
if snapshot.get("manifest") != _build_skills_manifest(skills_dir):
|
| 426 |
+
return None
|
| 427 |
+
return snapshot
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
def _write_skills_snapshot(
|
| 431 |
+
skills_dir: Path,
|
| 432 |
+
manifest: dict[str, list[int]],
|
| 433 |
+
skill_entries: list[dict],
|
| 434 |
+
category_descriptions: dict[str, str],
|
| 435 |
+
) -> None:
|
| 436 |
+
"""Persist skill metadata to disk for fast cold-start reuse."""
|
| 437 |
+
payload = {
|
| 438 |
+
"version": _SKILLS_SNAPSHOT_VERSION,
|
| 439 |
+
"manifest": manifest,
|
| 440 |
+
"skills": skill_entries,
|
| 441 |
+
"category_descriptions": category_descriptions,
|
| 442 |
+
}
|
| 443 |
+
try:
|
| 444 |
+
atomic_json_write(_skills_prompt_snapshot_path(), payload)
|
| 445 |
+
except Exception as e:
|
| 446 |
+
logger.debug("Could not write skills prompt snapshot: %s", e)
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
def _build_snapshot_entry(
|
| 450 |
+
skill_file: Path,
|
| 451 |
+
skills_dir: Path,
|
| 452 |
+
frontmatter: dict,
|
| 453 |
+
description: str,
|
| 454 |
+
) -> dict:
|
| 455 |
+
"""Build a serialisable metadata dict for one skill."""
|
| 456 |
+
rel_path = skill_file.relative_to(skills_dir)
|
| 457 |
+
parts = rel_path.parts
|
| 458 |
+
if len(parts) >= 2:
|
| 459 |
+
skill_name = parts[-2]
|
| 460 |
+
category = "/".join(parts[:-2]) if len(parts) > 2 else parts[0]
|
| 461 |
+
else:
|
| 462 |
+
category = "general"
|
| 463 |
+
skill_name = skill_file.parent.name
|
| 464 |
+
|
| 465 |
+
platforms = frontmatter.get("platforms") or []
|
| 466 |
+
if isinstance(platforms, str):
|
| 467 |
+
platforms = [platforms]
|
| 468 |
+
|
| 469 |
+
return {
|
| 470 |
+
"skill_name": skill_name,
|
| 471 |
+
"category": category,
|
| 472 |
+
"frontmatter_name": str(frontmatter.get("name", skill_name)),
|
| 473 |
+
"description": description,
|
| 474 |
+
"platforms": [str(p).strip() for p in platforms if str(p).strip()],
|
| 475 |
+
"conditions": extract_skill_conditions(frontmatter),
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
# =========================================================================
|
| 480 |
+
# Skills index
|
| 481 |
+
# =========================================================================
|
| 482 |
+
|
| 483 |
+
def _parse_skill_file(skill_file: Path) -> tuple[bool, dict, str]:
|
| 484 |
+
"""Read a SKILL.md once and return platform compatibility, frontmatter, and description.
|
| 485 |
+
|
| 486 |
+
Returns (is_compatible, frontmatter, description). On any error, returns
|
| 487 |
+
(True, {}, "") to err on the side of showing the skill.
|
| 488 |
+
"""
|
| 489 |
+
try:
|
| 490 |
+
raw = skill_file.read_text(encoding="utf-8")
|
| 491 |
+
frontmatter, _ = parse_frontmatter(raw)
|
| 492 |
+
|
| 493 |
+
if not skill_matches_platform(frontmatter):
|
| 494 |
+
return False, frontmatter, ""
|
| 495 |
+
|
| 496 |
+
return True, frontmatter, extract_skill_description(frontmatter)
|
| 497 |
+
except Exception as e:
|
| 498 |
+
logger.warning("Failed to parse skill file %s: %s", skill_file, e)
|
| 499 |
+
return True, {}, ""
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
def _skill_should_show(
|
| 503 |
+
conditions: dict,
|
| 504 |
+
available_tools: "set[str] | None",
|
| 505 |
+
available_toolsets: "set[str] | None",
|
| 506 |
+
) -> bool:
|
| 507 |
+
"""Return False if the skill's conditional activation rules exclude it."""
|
| 508 |
+
if available_tools is None and available_toolsets is None:
|
| 509 |
+
return True # No filtering info — show everything (backward compat)
|
| 510 |
+
|
| 511 |
+
at = available_tools or set()
|
| 512 |
+
ats = available_toolsets or set()
|
| 513 |
+
|
| 514 |
+
# fallback_for: hide when the primary tool/toolset IS available
|
| 515 |
+
for ts in conditions.get("fallback_for_toolsets", []):
|
| 516 |
+
if ts in ats:
|
| 517 |
+
return False
|
| 518 |
+
for t in conditions.get("fallback_for_tools", []):
|
| 519 |
+
if t in at:
|
| 520 |
+
return False
|
| 521 |
+
|
| 522 |
+
# requires: hide when a required tool/toolset is NOT available
|
| 523 |
+
for ts in conditions.get("requires_toolsets", []):
|
| 524 |
+
if ts not in ats:
|
| 525 |
+
return False
|
| 526 |
+
for t in conditions.get("requires_tools", []):
|
| 527 |
+
if t not in at:
|
| 528 |
+
return False
|
| 529 |
+
|
| 530 |
+
return True
|
| 531 |
+
|
| 532 |
+
|
| 533 |
+
def build_skills_system_prompt(
|
| 534 |
+
available_tools: "set[str] | None" = None,
|
| 535 |
+
available_toolsets: "set[str] | None" = None,
|
| 536 |
+
) -> str:
|
| 537 |
+
"""Build a compact skill index for the system prompt.
|
| 538 |
+
|
| 539 |
+
Two-layer cache:
|
| 540 |
+
1. In-process LRU dict keyed by (skills_dir, tools, toolsets)
|
| 541 |
+
2. Disk snapshot (``.skills_prompt_snapshot.json``) validated by
|
| 542 |
+
mtime/size manifest — survives process restarts
|
| 543 |
+
|
| 544 |
+
Falls back to a full filesystem scan when both layers miss.
|
| 545 |
+
|
| 546 |
+
External skill directories (``skills.external_dirs`` in config.yaml) are
|
| 547 |
+
scanned alongside the local ``~/.hermes/skills/`` directory. External dirs
|
| 548 |
+
are read-only — they appear in the index but new skills are always created
|
| 549 |
+
in the local dir. Local skills take precedence when names collide.
|
| 550 |
+
"""
|
| 551 |
+
skills_dir = get_skills_dir()
|
| 552 |
+
external_dirs = get_all_skills_dirs()[1:] # skip local (index 0)
|
| 553 |
+
|
| 554 |
+
if not skills_dir.exists() and not external_dirs:
|
| 555 |
+
return ""
|
| 556 |
+
|
| 557 |
+
# ── Layer 1: in-process LRU cache ─────────────────────────────────
|
| 558 |
+
# Include the resolved platform so per-platform disabled-skill lists
|
| 559 |
+
# produce distinct cache entries (gateway serves multiple platforms).
|
| 560 |
+
from gateway.session_context import get_session_env
|
| 561 |
+
_platform_hint = (
|
| 562 |
+
os.environ.get("HERMES_PLATFORM")
|
| 563 |
+
or get_session_env("HERMES_SESSION_PLATFORM")
|
| 564 |
+
or ""
|
| 565 |
+
)
|
| 566 |
+
cache_key = (
|
| 567 |
+
str(skills_dir.resolve()),
|
| 568 |
+
tuple(str(d) for d in external_dirs),
|
| 569 |
+
tuple(sorted(str(t) for t in (available_tools or set()))),
|
| 570 |
+
tuple(sorted(str(ts) for ts in (available_toolsets or set()))),
|
| 571 |
+
_platform_hint,
|
| 572 |
+
)
|
| 573 |
+
with _SKILLS_PROMPT_CACHE_LOCK:
|
| 574 |
+
cached = _SKILLS_PROMPT_CACHE.get(cache_key)
|
| 575 |
+
if cached is not None:
|
| 576 |
+
_SKILLS_PROMPT_CACHE.move_to_end(cache_key)
|
| 577 |
+
return cached
|
| 578 |
+
|
| 579 |
+
disabled = get_disabled_skill_names()
|
| 580 |
+
|
| 581 |
+
# ── Layer 2: disk snapshot ────────────────────────────────────────
|
| 582 |
+
snapshot = _load_skills_snapshot(skills_dir)
|
| 583 |
+
|
| 584 |
+
skills_by_category: dict[str, list[tuple[str, str]]] = {}
|
| 585 |
+
category_descriptions: dict[str, str] = {}
|
| 586 |
+
|
| 587 |
+
if snapshot is not None:
|
| 588 |
+
# Fast path: use pre-parsed metadata from disk
|
| 589 |
+
for entry in snapshot.get("skills", []):
|
| 590 |
+
if not isinstance(entry, dict):
|
| 591 |
+
continue
|
| 592 |
+
skill_name = entry.get("skill_name") or ""
|
| 593 |
+
category = entry.get("category") or "general"
|
| 594 |
+
frontmatter_name = entry.get("frontmatter_name") or skill_name
|
| 595 |
+
platforms = entry.get("platforms") or []
|
| 596 |
+
if not skill_matches_platform({"platforms": platforms}):
|
| 597 |
+
continue
|
| 598 |
+
if frontmatter_name in disabled or skill_name in disabled:
|
| 599 |
+
continue
|
| 600 |
+
if not _skill_should_show(
|
| 601 |
+
entry.get("conditions") or {},
|
| 602 |
+
available_tools,
|
| 603 |
+
available_toolsets,
|
| 604 |
+
):
|
| 605 |
+
continue
|
| 606 |
+
skills_by_category.setdefault(category, []).append(
|
| 607 |
+
(skill_name, entry.get("description", ""))
|
| 608 |
+
)
|
| 609 |
+
category_descriptions = {
|
| 610 |
+
str(k): str(v)
|
| 611 |
+
for k, v in (snapshot.get("category_descriptions") or {}).items()
|
| 612 |
+
}
|
| 613 |
+
else:
|
| 614 |
+
# Cold path: full filesystem scan + write snapshot for next time
|
| 615 |
+
skill_entries: list[dict] = []
|
| 616 |
+
for skill_file in iter_skill_index_files(skills_dir, "SKILL.md"):
|
| 617 |
+
is_compatible, frontmatter, desc = _parse_skill_file(skill_file)
|
| 618 |
+
entry = _build_snapshot_entry(skill_file, skills_dir, frontmatter, desc)
|
| 619 |
+
skill_entries.append(entry)
|
| 620 |
+
if not is_compatible:
|
| 621 |
+
continue
|
| 622 |
+
skill_name = entry["skill_name"]
|
| 623 |
+
if entry["frontmatter_name"] in disabled or skill_name in disabled:
|
| 624 |
+
continue
|
| 625 |
+
if not _skill_should_show(
|
| 626 |
+
extract_skill_conditions(frontmatter),
|
| 627 |
+
available_tools,
|
| 628 |
+
available_toolsets,
|
| 629 |
+
):
|
| 630 |
+
continue
|
| 631 |
+
skills_by_category.setdefault(entry["category"], []).append(
|
| 632 |
+
(skill_name, entry["description"])
|
| 633 |
+
)
|
| 634 |
+
|
| 635 |
+
# Read category-level DESCRIPTION.md files
|
| 636 |
+
for desc_file in iter_skill_index_files(skills_dir, "DESCRIPTION.md"):
|
| 637 |
+
try:
|
| 638 |
+
content = desc_file.read_text(encoding="utf-8")
|
| 639 |
+
fm, _ = parse_frontmatter(content)
|
| 640 |
+
cat_desc = fm.get("description")
|
| 641 |
+
if not cat_desc:
|
| 642 |
+
continue
|
| 643 |
+
rel = desc_file.relative_to(skills_dir)
|
| 644 |
+
cat = "/".join(rel.parts[:-1]) if len(rel.parts) > 1 else "general"
|
| 645 |
+
category_descriptions[cat] = str(cat_desc).strip().strip("'\"")
|
| 646 |
+
except Exception as e:
|
| 647 |
+
logger.debug("Could not read skill description %s: %s", desc_file, e)
|
| 648 |
+
|
| 649 |
+
_write_skills_snapshot(
|
| 650 |
+
skills_dir,
|
| 651 |
+
_build_skills_manifest(skills_dir),
|
| 652 |
+
skill_entries,
|
| 653 |
+
category_descriptions,
|
| 654 |
+
)
|
| 655 |
+
|
| 656 |
+
# ── External skill directories ─────────────────────────────────────
|
| 657 |
+
# Scan external dirs directly (no snapshot caching — they're read-only
|
| 658 |
+
# and typically small). Local skills already in skills_by_category take
|
| 659 |
+
# precedence: we track seen names and skip duplicates from external dirs.
|
| 660 |
+
seen_skill_names: set[str] = set()
|
| 661 |
+
for cat_skills in skills_by_category.values():
|
| 662 |
+
for name, _desc in cat_skills:
|
| 663 |
+
seen_skill_names.add(name)
|
| 664 |
+
|
| 665 |
+
for ext_dir in external_dirs:
|
| 666 |
+
if not ext_dir.exists():
|
| 667 |
+
continue
|
| 668 |
+
for skill_file in iter_skill_index_files(ext_dir, "SKILL.md"):
|
| 669 |
+
try:
|
| 670 |
+
is_compatible, frontmatter, desc = _parse_skill_file(skill_file)
|
| 671 |
+
if not is_compatible:
|
| 672 |
+
continue
|
| 673 |
+
entry = _build_snapshot_entry(skill_file, ext_dir, frontmatter, desc)
|
| 674 |
+
skill_name = entry["skill_name"]
|
| 675 |
+
if skill_name in seen_skill_names:
|
| 676 |
+
continue
|
| 677 |
+
if entry["frontmatter_name"] in disabled or skill_name in disabled:
|
| 678 |
+
continue
|
| 679 |
+
if not _skill_should_show(
|
| 680 |
+
extract_skill_conditions(frontmatter),
|
| 681 |
+
available_tools,
|
| 682 |
+
available_toolsets,
|
| 683 |
+
):
|
| 684 |
+
continue
|
| 685 |
+
seen_skill_names.add(skill_name)
|
| 686 |
+
skills_by_category.setdefault(entry["category"], []).append(
|
| 687 |
+
(skill_name, entry["description"])
|
| 688 |
+
)
|
| 689 |
+
except Exception as e:
|
| 690 |
+
logger.debug("Error reading external skill %s: %s", skill_file, e)
|
| 691 |
+
|
| 692 |
+
# External category descriptions
|
| 693 |
+
for desc_file in iter_skill_index_files(ext_dir, "DESCRIPTION.md"):
|
| 694 |
+
try:
|
| 695 |
+
content = desc_file.read_text(encoding="utf-8")
|
| 696 |
+
fm, _ = parse_frontmatter(content)
|
| 697 |
+
cat_desc = fm.get("description")
|
| 698 |
+
if not cat_desc:
|
| 699 |
+
continue
|
| 700 |
+
rel = desc_file.relative_to(ext_dir)
|
| 701 |
+
cat = "/".join(rel.parts[:-1]) if len(rel.parts) > 1 else "general"
|
| 702 |
+
category_descriptions.setdefault(cat, str(cat_desc).strip().strip("'\""))
|
| 703 |
+
except Exception as e:
|
| 704 |
+
logger.debug("Could not read external skill description %s: %s", desc_file, e)
|
| 705 |
+
|
| 706 |
+
if not skills_by_category:
|
| 707 |
+
result = ""
|
| 708 |
+
else:
|
| 709 |
+
index_lines = []
|
| 710 |
+
for category in sorted(skills_by_category.keys()):
|
| 711 |
+
cat_desc = category_descriptions.get(category, "")
|
| 712 |
+
if cat_desc:
|
| 713 |
+
index_lines.append(f" {category}: {cat_desc}")
|
| 714 |
+
else:
|
| 715 |
+
index_lines.append(f" {category}:")
|
| 716 |
+
# Deduplicate and sort skills within each category
|
| 717 |
+
seen = set()
|
| 718 |
+
for name, desc in sorted(skills_by_category[category], key=lambda x: x[0]):
|
| 719 |
+
if name in seen:
|
| 720 |
+
continue
|
| 721 |
+
seen.add(name)
|
| 722 |
+
if desc:
|
| 723 |
+
index_lines.append(f" - {name}: {desc}")
|
| 724 |
+
else:
|
| 725 |
+
index_lines.append(f" - {name}")
|
| 726 |
+
|
| 727 |
+
result = (
|
| 728 |
+
"## Skills (mandatory)\n"
|
| 729 |
+
"Before replying, scan the skills below. If one clearly matches your task, "
|
| 730 |
+
"load it with skill_view(name) and follow its instructions. "
|
| 731 |
+
"If a skill has issues, fix it with skill_manage(action='patch').\n"
|
| 732 |
+
"After difficult/iterative tasks, offer to save as a skill. "
|
| 733 |
+
"If a skill you loaded was missing steps, had wrong commands, or needed "
|
| 734 |
+
"pitfalls you discovered, update it before finishing.\n"
|
| 735 |
+
"\n"
|
| 736 |
+
"<available_skills>\n"
|
| 737 |
+
+ "\n".join(index_lines) + "\n"
|
| 738 |
+
"</available_skills>\n"
|
| 739 |
+
"\n"
|
| 740 |
+
"If none match, proceed normally without loading a skill."
|
| 741 |
+
)
|
| 742 |
+
|
| 743 |
+
# ── Store in LRU cache ────────────────────────────────────────────
|
| 744 |
+
with _SKILLS_PROMPT_CACHE_LOCK:
|
| 745 |
+
_SKILLS_PROMPT_CACHE[cache_key] = result
|
| 746 |
+
_SKILLS_PROMPT_CACHE.move_to_end(cache_key)
|
| 747 |
+
while len(_SKILLS_PROMPT_CACHE) > _SKILLS_PROMPT_CACHE_MAX:
|
| 748 |
+
_SKILLS_PROMPT_CACHE.popitem(last=False)
|
| 749 |
+
|
| 750 |
+
return result
|
| 751 |
+
|
| 752 |
+
|
| 753 |
+
def build_nous_subscription_prompt(valid_tool_names: "set[str] | None" = None) -> str:
|
| 754 |
+
"""Build a compact Nous subscription capability block for the system prompt."""
|
| 755 |
+
try:
|
| 756 |
+
from hermes_cli.nous_subscription import get_nous_subscription_features
|
| 757 |
+
from tools.tool_backend_helpers import managed_nous_tools_enabled
|
| 758 |
+
except Exception as exc:
|
| 759 |
+
logger.debug("Failed to import Nous subscription helper: %s", exc)
|
| 760 |
+
return ""
|
| 761 |
+
|
| 762 |
+
if not managed_nous_tools_enabled():
|
| 763 |
+
return ""
|
| 764 |
+
|
| 765 |
+
valid_names = set(valid_tool_names or set())
|
| 766 |
+
relevant_tool_names = {
|
| 767 |
+
"web_search",
|
| 768 |
+
"web_extract",
|
| 769 |
+
"browser_navigate",
|
| 770 |
+
"browser_snapshot",
|
| 771 |
+
"browser_click",
|
| 772 |
+
"browser_type",
|
| 773 |
+
"browser_scroll",
|
| 774 |
+
"browser_console",
|
| 775 |
+
"browser_press",
|
| 776 |
+
"browser_get_images",
|
| 777 |
+
"browser_vision",
|
| 778 |
+
"image_generate",
|
| 779 |
+
"text_to_speech",
|
| 780 |
+
"terminal",
|
| 781 |
+
"process",
|
| 782 |
+
"execute_code",
|
| 783 |
+
}
|
| 784 |
+
|
| 785 |
+
if valid_names and not (valid_names & relevant_tool_names):
|
| 786 |
+
return ""
|
| 787 |
+
|
| 788 |
+
features = get_nous_subscription_features()
|
| 789 |
+
|
| 790 |
+
def _status_line(feature) -> str:
|
| 791 |
+
if feature.managed_by_nous:
|
| 792 |
+
return f"- {feature.label}: active via Nous subscription"
|
| 793 |
+
if feature.active:
|
| 794 |
+
current = feature.current_provider or "configured provider"
|
| 795 |
+
return f"- {feature.label}: currently using {current}"
|
| 796 |
+
if feature.included_by_default and features.nous_auth_present:
|
| 797 |
+
return f"- {feature.label}: included with Nous subscription, not currently selected"
|
| 798 |
+
if feature.key == "modal" and features.nous_auth_present:
|
| 799 |
+
return f"- {feature.label}: optional via Nous subscription"
|
| 800 |
+
return f"- {feature.label}: not currently available"
|
| 801 |
+
|
| 802 |
+
lines = [
|
| 803 |
+
"# Nous Subscription",
|
| 804 |
+
"Nous subscription includes managed web tools (Firecrawl), image generation (FAL), OpenAI TTS, and browser automation (Browser Use) by default. Modal execution is optional.",
|
| 805 |
+
"Current capability status:",
|
| 806 |
+
]
|
| 807 |
+
lines.extend(_status_line(feature) for feature in features.items())
|
| 808 |
+
lines.extend(
|
| 809 |
+
[
|
| 810 |
+
"When a Nous-managed feature is active, do not ask the user for Firecrawl, FAL, OpenAI TTS, or Browser-Use API keys.",
|
| 811 |
+
"If the user is not subscribed and asks for a capability that Nous subscription would unlock or simplify, suggest Nous subscription as one option alongside direct setup or local alternatives.",
|
| 812 |
+
"Do not mention subscription unless the user asks about it or it directly solves the current missing capability.",
|
| 813 |
+
"Useful commands: hermes setup, hermes setup tools, hermes setup terminal, hermes status.",
|
| 814 |
+
]
|
| 815 |
+
)
|
| 816 |
+
return "\n".join(lines)
|
| 817 |
+
|
| 818 |
+
|
| 819 |
+
# =========================================================================
|
| 820 |
+
# Context files (SOUL.md, AGENTS.md, .cursorrules)
|
| 821 |
+
# =========================================================================
|
| 822 |
+
|
| 823 |
+
def _truncate_content(content: str, filename: str, max_chars: int = CONTEXT_FILE_MAX_CHARS) -> str:
|
| 824 |
+
"""Head/tail truncation with a marker in the middle."""
|
| 825 |
+
if len(content) <= max_chars:
|
| 826 |
+
return content
|
| 827 |
+
head_chars = int(max_chars * CONTEXT_TRUNCATE_HEAD_RATIO)
|
| 828 |
+
tail_chars = int(max_chars * CONTEXT_TRUNCATE_TAIL_RATIO)
|
| 829 |
+
head = content[:head_chars]
|
| 830 |
+
tail = content[-tail_chars:]
|
| 831 |
+
marker = f"\n\n[...truncated {filename}: kept {head_chars}+{tail_chars} of {len(content)} chars. Use file tools to read the full file.]\n\n"
|
| 832 |
+
return head + marker + tail
|
| 833 |
+
|
| 834 |
+
|
| 835 |
+
def load_soul_md() -> Optional[str]:
|
| 836 |
+
"""Load SOUL.md from HERMES_HOME and return its content, or None.
|
| 837 |
+
|
| 838 |
+
Used as the agent identity (slot #1 in the system prompt). When this
|
| 839 |
+
returns content, ``build_context_files_prompt`` should be called with
|
| 840 |
+
``skip_soul=True`` so SOUL.md isn't injected twice.
|
| 841 |
+
"""
|
| 842 |
+
try:
|
| 843 |
+
from hermes_cli.config import ensure_hermes_home
|
| 844 |
+
ensure_hermes_home()
|
| 845 |
+
except Exception as e:
|
| 846 |
+
logger.debug("Could not ensure HERMES_HOME before loading SOUL.md: %s", e)
|
| 847 |
+
|
| 848 |
+
soul_path = get_hermes_home() / "SOUL.md"
|
| 849 |
+
if not soul_path.exists():
|
| 850 |
+
return None
|
| 851 |
+
try:
|
| 852 |
+
content = soul_path.read_text(encoding="utf-8").strip()
|
| 853 |
+
if not content:
|
| 854 |
+
return None
|
| 855 |
+
content = _scan_context_content(content, "SOUL.md")
|
| 856 |
+
content = _truncate_content(content, "SOUL.md")
|
| 857 |
+
return content
|
| 858 |
+
except Exception as e:
|
| 859 |
+
logger.debug("Could not read SOUL.md from %s: %s", soul_path, e)
|
| 860 |
+
return None
|
| 861 |
+
|
| 862 |
+
|
| 863 |
+
def _load_hermes_md(cwd_path: Path) -> str:
|
| 864 |
+
""".hermes.md / HERMES.md — walk to git root."""
|
| 865 |
+
hermes_md_path = _find_hermes_md(cwd_path)
|
| 866 |
+
if not hermes_md_path:
|
| 867 |
+
return ""
|
| 868 |
+
try:
|
| 869 |
+
content = hermes_md_path.read_text(encoding="utf-8").strip()
|
| 870 |
+
if not content:
|
| 871 |
+
return ""
|
| 872 |
+
content = _strip_yaml_frontmatter(content)
|
| 873 |
+
rel = hermes_md_path.name
|
| 874 |
+
try:
|
| 875 |
+
rel = str(hermes_md_path.relative_to(cwd_path))
|
| 876 |
+
except ValueError:
|
| 877 |
+
pass
|
| 878 |
+
content = _scan_context_content(content, rel)
|
| 879 |
+
result = f"## {rel}\n\n{content}"
|
| 880 |
+
return _truncate_content(result, ".hermes.md")
|
| 881 |
+
except Exception as e:
|
| 882 |
+
logger.debug("Could not read %s: %s", hermes_md_path, e)
|
| 883 |
+
return ""
|
| 884 |
+
|
| 885 |
+
|
| 886 |
+
def _load_agents_md(cwd_path: Path) -> str:
|
| 887 |
+
"""AGENTS.md — top-level only (no recursive walk)."""
|
| 888 |
+
for name in ["AGENTS.md", "agents.md"]:
|
| 889 |
+
candidate = cwd_path / name
|
| 890 |
+
if candidate.exists():
|
| 891 |
+
try:
|
| 892 |
+
content = candidate.read_text(encoding="utf-8").strip()
|
| 893 |
+
if content:
|
| 894 |
+
content = _scan_context_content(content, name)
|
| 895 |
+
result = f"## {name}\n\n{content}"
|
| 896 |
+
return _truncate_content(result, "AGENTS.md")
|
| 897 |
+
except Exception as e:
|
| 898 |
+
logger.debug("Could not read %s: %s", candidate, e)
|
| 899 |
+
return ""
|
| 900 |
+
|
| 901 |
+
|
| 902 |
+
def _load_claude_md(cwd_path: Path) -> str:
|
| 903 |
+
"""CLAUDE.md / claude.md — cwd only."""
|
| 904 |
+
for name in ["CLAUDE.md", "claude.md"]:
|
| 905 |
+
candidate = cwd_path / name
|
| 906 |
+
if candidate.exists():
|
| 907 |
+
try:
|
| 908 |
+
content = candidate.read_text(encoding="utf-8").strip()
|
| 909 |
+
if content:
|
| 910 |
+
content = _scan_context_content(content, name)
|
| 911 |
+
result = f"## {name}\n\n{content}"
|
| 912 |
+
return _truncate_content(result, "CLAUDE.md")
|
| 913 |
+
except Exception as e:
|
| 914 |
+
logger.debug("Could not read %s: %s", candidate, e)
|
| 915 |
+
return ""
|
| 916 |
+
|
| 917 |
+
|
| 918 |
+
def _load_cursorrules(cwd_path: Path) -> str:
|
| 919 |
+
""".cursorrules + .cursor/rules/*.mdc — cwd only."""
|
| 920 |
+
cursorrules_content = ""
|
| 921 |
+
cursorrules_file = cwd_path / ".cursorrules"
|
| 922 |
+
if cursorrules_file.exists():
|
| 923 |
+
try:
|
| 924 |
+
content = cursorrules_file.read_text(encoding="utf-8").strip()
|
| 925 |
+
if content:
|
| 926 |
+
content = _scan_context_content(content, ".cursorrules")
|
| 927 |
+
cursorrules_content += f"## .cursorrules\n\n{content}\n\n"
|
| 928 |
+
except Exception as e:
|
| 929 |
+
logger.debug("Could not read .cursorrules: %s", e)
|
| 930 |
+
|
| 931 |
+
cursor_rules_dir = cwd_path / ".cursor" / "rules"
|
| 932 |
+
if cursor_rules_dir.exists() and cursor_rules_dir.is_dir():
|
| 933 |
+
mdc_files = sorted(cursor_rules_dir.glob("*.mdc"))
|
| 934 |
+
for mdc_file in mdc_files:
|
| 935 |
+
try:
|
| 936 |
+
content = mdc_file.read_text(encoding="utf-8").strip()
|
| 937 |
+
if content:
|
| 938 |
+
content = _scan_context_content(content, f".cursor/rules/{mdc_file.name}")
|
| 939 |
+
cursorrules_content += f"## .cursor/rules/{mdc_file.name}\n\n{content}\n\n"
|
| 940 |
+
except Exception as e:
|
| 941 |
+
logger.debug("Could not read %s: %s", mdc_file, e)
|
| 942 |
+
|
| 943 |
+
if not cursorrules_content:
|
| 944 |
+
return ""
|
| 945 |
+
return _truncate_content(cursorrules_content, ".cursorrules")
|
| 946 |
+
|
| 947 |
+
|
| 948 |
+
def build_context_files_prompt(cwd: Optional[str] = None, skip_soul: bool = False) -> str:
|
| 949 |
+
"""Discover and load context files for the system prompt.
|
| 950 |
+
|
| 951 |
+
Priority (first found wins — only ONE project context type is loaded):
|
| 952 |
+
1. .hermes.md / HERMES.md (walk to git root)
|
| 953 |
+
2. AGENTS.md / agents.md (cwd only)
|
| 954 |
+
3. CLAUDE.md / claude.md (cwd only)
|
| 955 |
+
4. .cursorrules / .cursor/rules/*.mdc (cwd only)
|
| 956 |
+
|
| 957 |
+
SOUL.md from HERMES_HOME is independent and always included when present.
|
| 958 |
+
Each context source is capped at 20,000 chars.
|
| 959 |
+
|
| 960 |
+
When *skip_soul* is True, SOUL.md is not included here (it was already
|
| 961 |
+
loaded via ``load_soul_md()`` for the identity slot).
|
| 962 |
+
"""
|
| 963 |
+
if cwd is None:
|
| 964 |
+
cwd = os.getcwd()
|
| 965 |
+
|
| 966 |
+
cwd_path = Path(cwd).resolve()
|
| 967 |
+
sections = []
|
| 968 |
+
|
| 969 |
+
# Priority-based project context: first match wins
|
| 970 |
+
project_context = (
|
| 971 |
+
_load_hermes_md(cwd_path)
|
| 972 |
+
or _load_agents_md(cwd_path)
|
| 973 |
+
or _load_claude_md(cwd_path)
|
| 974 |
+
or _load_cursorrules(cwd_path)
|
| 975 |
+
)
|
| 976 |
+
if project_context:
|
| 977 |
+
sections.append(project_context)
|
| 978 |
+
|
| 979 |
+
# SOUL.md from HERMES_HOME only — skip when already loaded as identity
|
| 980 |
+
if not skip_soul:
|
| 981 |
+
soul_content = load_soul_md()
|
| 982 |
+
if soul_content:
|
| 983 |
+
sections.append(soul_content)
|
| 984 |
+
|
| 985 |
+
if not sections:
|
| 986 |
+
return ""
|
| 987 |
+
return "# Project Context\n\nThe following project context files have been loaded and should be followed:\n\n" + "\n".join(sections)
|
agent/prompt_caching.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Anthropic prompt caching (system_and_3 strategy).
|
| 2 |
+
|
| 3 |
+
Reduces input token costs by ~75% on multi-turn conversations by caching
|
| 4 |
+
the conversation prefix. Uses 4 cache_control breakpoints (Anthropic max):
|
| 5 |
+
1. System prompt (stable across all turns)
|
| 6 |
+
2-4. Last 3 non-system messages (rolling window)
|
| 7 |
+
|
| 8 |
+
Pure functions -- no class state, no AIAgent dependency.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import copy
|
| 12 |
+
from typing import Any, Dict, List
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _apply_cache_marker(msg: dict, cache_marker: dict, native_anthropic: bool = False) -> None:
|
| 16 |
+
"""Add cache_control to a single message, handling all format variations."""
|
| 17 |
+
role = msg.get("role", "")
|
| 18 |
+
content = msg.get("content")
|
| 19 |
+
|
| 20 |
+
if role == "tool":
|
| 21 |
+
if native_anthropic:
|
| 22 |
+
msg["cache_control"] = cache_marker
|
| 23 |
+
return
|
| 24 |
+
|
| 25 |
+
if content is None or content == "":
|
| 26 |
+
msg["cache_control"] = cache_marker
|
| 27 |
+
return
|
| 28 |
+
|
| 29 |
+
if isinstance(content, str):
|
| 30 |
+
msg["content"] = [
|
| 31 |
+
{"type": "text", "text": content, "cache_control": cache_marker}
|
| 32 |
+
]
|
| 33 |
+
return
|
| 34 |
+
|
| 35 |
+
if isinstance(content, list) and content:
|
| 36 |
+
last = content[-1]
|
| 37 |
+
if isinstance(last, dict):
|
| 38 |
+
last["cache_control"] = cache_marker
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def apply_anthropic_cache_control(
|
| 42 |
+
api_messages: List[Dict[str, Any]],
|
| 43 |
+
cache_ttl: str = "5m",
|
| 44 |
+
native_anthropic: bool = False,
|
| 45 |
+
) -> List[Dict[str, Any]]:
|
| 46 |
+
"""Apply system_and_3 caching strategy to messages for Anthropic models.
|
| 47 |
+
|
| 48 |
+
Places up to 4 cache_control breakpoints: system prompt + last 3 non-system messages.
|
| 49 |
+
|
| 50 |
+
Returns:
|
| 51 |
+
Deep copy of messages with cache_control breakpoints injected.
|
| 52 |
+
"""
|
| 53 |
+
messages = copy.deepcopy(api_messages)
|
| 54 |
+
if not messages:
|
| 55 |
+
return messages
|
| 56 |
+
|
| 57 |
+
marker = {"type": "ephemeral"}
|
| 58 |
+
if cache_ttl == "1h":
|
| 59 |
+
marker["ttl"] = "1h"
|
| 60 |
+
|
| 61 |
+
breakpoints_used = 0
|
| 62 |
+
|
| 63 |
+
if messages[0].get("role") == "system":
|
| 64 |
+
_apply_cache_marker(messages[0], marker, native_anthropic=native_anthropic)
|
| 65 |
+
breakpoints_used += 1
|
| 66 |
+
|
| 67 |
+
remaining = 4 - breakpoints_used
|
| 68 |
+
non_sys = [i for i in range(len(messages)) if messages[i].get("role") != "system"]
|
| 69 |
+
for idx in non_sys[-remaining:]:
|
| 70 |
+
_apply_cache_marker(messages[idx], marker, native_anthropic=native_anthropic)
|
| 71 |
+
|
| 72 |
+
return messages
|