|
| 1 | +"""Natural-language Telegram agent backed by an OpenAI-compatible model and MCP. |
| 2 | +
|
| 3 | +The Telegram conversation pattern is inspired by Luis Rivera's ocabra_telegram: |
| 4 | +https://github.com/luisriverag/ocabra_telegram |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import json |
| 10 | +import os |
| 11 | +import threading |
| 12 | +import time |
| 13 | +from collections import defaultdict, deque |
| 14 | +from dataclasses import dataclass, field |
| 15 | +from typing import Any |
| 16 | + |
| 17 | +import requests |
| 18 | + |
| 19 | +from app import mcp_server |
| 20 | + |
| 21 | + |
| 22 | +READ_ONLY_TOOLS = { |
| 23 | + "list_proposals", |
| 24 | + "current_budget", |
| 25 | + "get_voting_settings", |
| 26 | +} |
| 27 | +MUTATING_TOOLS = {"create_member", "create_proposal", "create_poll", "update_voting_settings"} |
| 28 | +MAX_TOOL_ROUNDS = 4 |
| 29 | +ConversationKey = tuple[int, int] |
| 30 | +_history: dict[ConversationKey, deque[dict[str, Any]]] = defaultdict(lambda: deque(maxlen=12)) |
| 31 | +_pending_actions: dict[ConversationKey, "PendingAction"] = {} |
| 32 | +_state_lock = threading.RLock() |
| 33 | + |
| 34 | + |
| 35 | +@dataclass(frozen=True) |
| 36 | +class PendingAction: |
| 37 | + tool_name: str |
| 38 | + arguments: dict[str, Any] |
| 39 | + created_at: float = field(default_factory=time.monotonic) |
| 40 | + |
| 41 | + |
| 42 | +def is_configured() -> bool: |
| 43 | + return bool(os.getenv("OCABRA_CHAT_URL", "").strip() and mcp_server.MCP_API_KEY) |
| 44 | + |
| 45 | + |
| 46 | +def _headers() -> dict[str, str]: |
| 47 | + headers = {"Content-Type": "application/json"} |
| 48 | + api_key = os.getenv("OCABRA_API_KEY", "").strip() |
| 49 | + if api_key: |
| 50 | + headers["Authorization"] = f"Bearer {api_key}" |
| 51 | + return headers |
| 52 | + |
| 53 | + |
| 54 | +def _openai_tools(*, is_admin: bool) -> list[dict[str, Any]]: |
| 55 | + definitions = mcp_server._tool_definitions() |
| 56 | + if not is_admin: |
| 57 | + definitions = [item for item in definitions if item["name"] in READ_ONLY_TOOLS] |
| 58 | + return [ |
| 59 | + { |
| 60 | + "type": "function", |
| 61 | + "function": { |
| 62 | + "name": item["name"], |
| 63 | + "description": item["description"], |
| 64 | + "parameters": item["inputSchema"], |
| 65 | + }, |
| 66 | + } |
| 67 | + for item in definitions |
| 68 | + ] |
| 69 | + |
| 70 | + |
| 71 | +def _call_mcp(tool_name: str, arguments: dict[str, Any], *, is_admin: bool) -> str: |
| 72 | + allowed = {item["function"]["name"] for item in _openai_tools(is_admin=is_admin)} |
| 73 | + if tool_name not in allowed: |
| 74 | + return json.dumps({"error": "This Telegram account may not use that tool."}) |
| 75 | + response = mcp_server.handle_request( |
| 76 | + { |
| 77 | + "jsonrpc": "2.0", |
| 78 | + "id": "telegram-agent", |
| 79 | + "method": "tools/call", |
| 80 | + "params": { |
| 81 | + "name": tool_name, |
| 82 | + "arguments": arguments, |
| 83 | + "api_key": mcp_server.MCP_API_KEY, |
| 84 | + }, |
| 85 | + } |
| 86 | + ) |
| 87 | + if not response: |
| 88 | + return json.dumps({"error": "MCP returned no response."}) |
| 89 | + if "error" in response: |
| 90 | + return json.dumps({"error": response["error"]["message"]}) |
| 91 | + content = response.get("result", {}).get("content", []) |
| 92 | + return content[0].get("text", "{}") if content else "{}" |
| 93 | + |
| 94 | + |
| 95 | +def _conversation_key(chat_id: int, telegram_user_id: int | None) -> ConversationKey: |
| 96 | + # Private chats normally use the same value for both IDs. Including the user |
| 97 | + # ID prevents one member in a group chat from seeing another member's context. |
| 98 | + return int(chat_id), int(telegram_user_id if telegram_user_id is not None else chat_id) |
| 99 | + |
| 100 | + |
| 101 | +def _confirmation_text(action: PendingAction) -> str: |
| 102 | + return ( |
| 103 | + f"⚠️ Please confirm MCP action {action.tool_name} with /confirm, " |
| 104 | + "or discard it with /cancel.\n" |
| 105 | + f"Arguments: {json.dumps(action.arguments, ensure_ascii=False, sort_keys=True)}" |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +def _action_result_text(action: PendingAction, result: str) -> str: |
| 110 | + """Turn MCP JSON into a compact, Telegram-friendly completion message.""" |
| 111 | + try: |
| 112 | + payload = json.loads(result) |
| 113 | + except (TypeError, json.JSONDecodeError): |
| 114 | + payload = result |
| 115 | + if isinstance(payload, dict) and payload.get("error"): |
| 116 | + return f"❌ {action.tool_name} failed: {payload['error']}" |
| 117 | + if isinstance(payload, dict): |
| 118 | + details = "\n".join( |
| 119 | + f"{str(key).replace('_', ' ').capitalize()}: " |
| 120 | + f"{json.dumps(value, ensure_ascii=False) if isinstance(value, (dict, list)) else value}" |
| 121 | + for key, value in payload.items() |
| 122 | + ) |
| 123 | + else: |
| 124 | + details = str(payload) |
| 125 | + suffix = f"\n{details}" if details and details != "{}" else "" |
| 126 | + return f"✅ {action.tool_name} completed.{suffix}" |
| 127 | + |
| 128 | + |
| 129 | +def answer( |
| 130 | + chat_id: int, |
| 131 | + text: str, |
| 132 | + *, |
| 133 | + telegram_user_id: int | None = None, |
| 134 | + is_admin: bool = False, |
| 135 | +) -> str: |
| 136 | + """Answer natural language and let the model operate ManaVote through MCP tools.""" |
| 137 | + url = os.getenv("OCABRA_CHAT_URL", "").strip() |
| 138 | + if not url: |
| 139 | + raise RuntimeError("Natural-language Telegram support is not configured.") |
| 140 | + |
| 141 | + key = _conversation_key(chat_id, telegram_user_id) |
| 142 | + normalized_text = text.strip().lower() |
| 143 | + with _state_lock: |
| 144 | + pending = _pending_actions.get(key) |
| 145 | + if normalized_text in {"/cancel", "cancel"}: |
| 146 | + with _state_lock: |
| 147 | + removed = _pending_actions.pop(key, None) |
| 148 | + return "✅ Pending action cancelled." if removed else "There is no pending action to cancel." |
| 149 | + if normalized_text in {"/confirm", "confirm"}: |
| 150 | + if not pending: |
| 151 | + return "There is no pending action to confirm." |
| 152 | + confirmation_ttl = float(os.getenv("TELEGRAM_CONFIRM_TTL_SECONDS", "300")) |
| 153 | + if time.monotonic() - pending.created_at > confirmation_ttl: |
| 154 | + with _state_lock: |
| 155 | + _pending_actions.pop(key, None) |
| 156 | + return "⌛ That pending action expired. Please request it again." |
| 157 | + if not is_admin: |
| 158 | + with _state_lock: |
| 159 | + _pending_actions.pop(key, None) |
| 160 | + return "❌ Only a linked administrator can confirm that action." |
| 161 | + result = _call_mcp(pending.tool_name, pending.arguments, is_admin=True) |
| 162 | + with _state_lock: |
| 163 | + _pending_actions.pop(key, None) |
| 164 | + return _action_result_text(pending, result) |
| 165 | + |
| 166 | + with _state_lock: |
| 167 | + history_snapshot = list(_history[key]) |
| 168 | + messages: list[dict[str, Any]] = [ |
| 169 | + { |
| 170 | + "role": "system", |
| 171 | + "content": os.getenv( |
| 172 | + "TELEGRAM_AGENT_SYSTEM_PROMPT", |
| 173 | + "You are the ManaVote Telegram assistant. Use the supplied tools for ManaVote facts and actions. " |
| 174 | + "Never invent tool results. If a tool says confirmation_required, repeat its confirmation message " |
| 175 | + "and do not claim the action succeeded. Reply concisely in the user's language using plain text " |
| 176 | + "that reads well in Telegram.", |
| 177 | + ), |
| 178 | + }, |
| 179 | + *history_snapshot, |
| 180 | + {"role": "user", "content": text}, |
| 181 | + ] |
| 182 | + tools = _openai_tools(is_admin=is_admin) |
| 183 | + |
| 184 | + for _ in range(MAX_TOOL_ROUNDS): |
| 185 | + response = requests.post( |
| 186 | + url, |
| 187 | + headers=_headers(), |
| 188 | + json={ |
| 189 | + "model": os.getenv("OCABRA_MODEL", "ocabra"), |
| 190 | + "messages": messages, |
| 191 | + "tools": tools, |
| 192 | + "tool_choice": "auto", |
| 193 | + "temperature": 0.2, |
| 194 | + "stream": False, |
| 195 | + }, |
| 196 | + timeout=float(os.getenv("OCABRA_TIMEOUT_SECONDS", "60")), |
| 197 | + ) |
| 198 | + response.raise_for_status() |
| 199 | + assistant = response.json()["choices"][0]["message"] |
| 200 | + tool_calls = assistant.get("tool_calls") or [] |
| 201 | + if not tool_calls: |
| 202 | + reply = (assistant.get("content") or "I couldn't produce a response.").strip() |
| 203 | + with _state_lock: |
| 204 | + _history[key].extend( |
| 205 | + ({"role": "user", "content": text}, {"role": "assistant", "content": reply}) |
| 206 | + ) |
| 207 | + return reply |
| 208 | + |
| 209 | + messages.append(assistant) |
| 210 | + for tool_call in tool_calls: |
| 211 | + function = tool_call.get("function") or {} |
| 212 | + try: |
| 213 | + arguments = json.loads(function.get("arguments") or "{}") |
| 214 | + except (TypeError, json.JSONDecodeError): |
| 215 | + arguments = {} |
| 216 | + if not isinstance(arguments, dict): |
| 217 | + result = json.dumps({"error": "Tool arguments must be a JSON object."}) |
| 218 | + elif function.get("name") in MUTATING_TOOLS: |
| 219 | + action = PendingAction(function["name"], arguments) |
| 220 | + with _state_lock: |
| 221 | + _pending_actions[key] = action |
| 222 | + # Do not rely on the model to reproduce a safety prompt. Return |
| 223 | + # the exact confirmation instructions immediately and avoid an |
| 224 | + # unnecessary second model round. |
| 225 | + return _confirmation_text(action) |
| 226 | + else: |
| 227 | + result = _call_mcp(function.get("name", ""), arguments, is_admin=is_admin) |
| 228 | + messages.append( |
| 229 | + {"role": "tool", "tool_call_id": tool_call.get("id", ""), "content": result} |
| 230 | + ) |
| 231 | + |
| 232 | + raise RuntimeError("The assistant exceeded the MCP tool-call limit.") |
| 233 | + |
| 234 | + |
| 235 | +def reset(chat_id: int, telegram_user_id: int | None = None) -> None: |
| 236 | + key = _conversation_key(chat_id, telegram_user_id) |
| 237 | + with _state_lock: |
| 238 | + _history.pop(key, None) |
| 239 | + _pending_actions.pop(key, None) |
0 commit comments