# Hermes Agent Bridge Tool

Install via Admin UI → Tools → "+" → paste code → save. Requires env vars in compose:

```yaml
environment:
  - HERMES_API_KEY=<from api_server.key in ~/.hermes/config.yaml>
  - HERMES_API_URL=http://host.docker.internal:18789
```

## Full Source

```python
"""
Hermes Agent Bridge — delegate complex tasks to Hermes Agent's full AI agent
capabilities (terminal, web search, file ops, code execution, and more).

Configure via Valves or env vars:
  - HERMES_API_KEY: required
  - HERMES_API_URL: default http://host.docker.internal:18789
"""

import json
import os
import urllib.error
import urllib.request
from typing import Optional


async def hermes_agent_task(
    task: str,
    model: Optional[str] = None,
    __user__: Optional[dict] = None,
) -> str:
    """
    Delegate a complex multi-step task to Hermes Agent.

    Hermes is an autonomous AI agent with access to: terminal/shell commands,
    web search, file reading/writing, code execution, web browsing, GitHub,
    and many more tools. Use this for tasks that require multiple steps,
    research, or tool usage beyond what the current model can do directly.

    Args:
        task: Full description of the task for Hermes Agent. Be specific.
        model: Model override (default: gemma-12b). Options: gemma-12b,
               qwen36-35b-mtp, qwen35-9b-mtp, qwopus-9b, gemma-26b

    Returns:
        Hermes Agent's complete response
    """
    api_key = os.environ.get("HERMES_API_KEY", "")
    if not api_key:
        return (
            "Error: HERMES_API_KEY not set. "
            "Configure it in Admin Settings or docker-compose environment."
        )

    base_url = os.environ.get(
        "HERMES_API_URL", "http://host.docker.internal:18789"
    )
    url = f"{base_url}/v1/chat/completions"

    payload = json.dumps({
        "model": model or "gemma-12b",
        "messages": [{"role": "user", "content": task}],
        "max_tokens": 8192,
        "temperature": 0.2,
    }).encode("utf-8")

    req = urllib.request.Request(url, data=payload)
    req.add_header("Authorization", f"Bearer {api_key}")
    req.add_header("Content-Type", "application/json")

    try:
        resp = urllib.request.urlopen(req, timeout=300)
        result = json.loads(resp.read())
        content = result["choices"][0]["message"]["content"]
        model_used = result.get("model", model or "gemma-12b")
        usage = result.get("usage", {})
        tok_info = ""
        if usage:
            tok_info = (
                f" (prompt={usage.get('prompt_tokens','?')}, "
                f"completion={usage.get('completion_tokens','?')})"
            )
        return (
            f"Hermes Agent [{model_used}] response:{tok_info}\n\n{content}"
        )
    except urllib.error.HTTPError as e:
        body = e.read().decode()
        return f"HTTP {e.code} from Hermes API: {body}"
    except Exception as e:
        return f"Error calling Hermes Agent: {e}"
```
