Files
trello-plugin/src/trello_plugin/tools.py
T
Marko (Hermes Implementer) 43acd5bfbf fix: address PR #5 review comments
- Add class-level type annotations on TrelloClient (api_key, token, _session)
- Add TypedDict response contracts with TypeGuard narrowing for _request
- Replace os.environ manipulation with monkeypatch in tests
- Align disconnect message with auto-reconnect behavior
- Add docstring for TRELLO_API_BASE constant
- Add _post and _put request helpers for future endpoints
2026-05-26 22:22:25 +00:00

96 lines
3.2 KiB
Python

"""
Trello Plugin — Hermes Agent tool definitions.
Each function decorated with ``@tool`` registers itself as a Hermes tool
that the agent can invoke during a conversation.
"""
from __future__ import annotations
import json
import os
from typing import Any
from trello_plugin.client import TrelloClient
# ---------------------------------------------------------------------------
# Module-level client (lazily initialised so env vars can be overridden)
# ---------------------------------------------------------------------------
_client: TrelloClient | None = None
def _get_client() -> TrelloClient:
global _client # noqa: PLW0603
api_key = os.environ.get("TRELLO_API_KEY", "")
token = os.environ.get("TRELLO_TOKEN", "")
if _client is None or _client.api_key != api_key or _client.token != token:
_client = TrelloClient(api_key=api_key, token=token)
return _client
# ---------------------------------------------------------------------------
# Tool helpers
# ---------------------------------------------------------------------------
def _respond(data: dict[str, Any]) -> str:
"""Wrap a result dict in a JSON string, the Hermes tool contract."""
return json.dumps(data, indent=2)
# ---------------------------------------------------------------------------
# Tools
# ---------------------------------------------------------------------------
def trello_verify_credentials() -> str:
"""Verify that the Trello API key and token are valid.
Checks the ``TRELLO_API_KEY`` and ``TRELLO_TOKEN`` environment variables
and attempts a test call to the Trello API. Returns the authenticated
user's Trello username on success.
"""
client = _get_client()
result = client.verify_credentials()
return _respond(result)
def trello_list_boards() -> str:
"""List all Trello boards accessible to the authenticated user.
Requires ``TRELLO_API_KEY`` and ``TRELLO_TOKEN`` to be set.
Returns board id, name, URL, and whether the board is closed/starred.
"""
client = _get_client()
result = client.list_boards()
return _respond(result)
def trello_disconnect() -> str:
"""Disconnect from Trello by clearing credentials from memory.
This does NOT revoke the Trello token — invalidate it via
Trello's account settings if full revocation is desired.
"""
global _client # noqa: PLW0603
_client = None
return _respond({"success": True, "message": "Trello credentials cleared from memory. Next tool call will re-read TRELLO_API_KEY and TRELLO_TOKEN from environment and reconnect automatically."})
# ---------------------------------------------------------------------------
# Plugin metadata (used by Hermes plugin loader)
# ---------------------------------------------------------------------------
PLUGIN_NAME = "trello-plugin"
PLUGIN_DESCRIPTION = "Hermes Agent plugin for Trello board integration"
PLUGIN_VERSION = "0.1.0"
PLUGIN_TOOLS = [
trello_verify_credentials,
trello_list_boards,
trello_disconnect,
]
PLUGIN_REQUIRES_ENV = ["TRELLO_API_KEY", "TRELLO_TOKEN"]
def check_requirements() -> bool:
"""Return True when both Trello env vars are present."""
return TrelloClient.check_requirements()