feat: make Telegram relay delivery opt-in
This commit is contained in:
@@ -19,11 +19,16 @@ if _HERMES_SITE not in sys.path:
|
||||
|
||||
from quart import Quart, jsonify, request
|
||||
|
||||
VERSION = "3.2.0"
|
||||
VERSION = "3.3.0"
|
||||
TIMEOUT = int(os.environ.get("HERMES_RELAY_TIMEOUT", "120"))
|
||||
TG_TARGET = os.environ.get("HERMES_RELAY_TELEGRAM_TARGET", "telegram")
|
||||
DELIVERY_WORKERS = int(os.environ.get("HERMES_RELAY_DELIVERY_WORKERS", "2"))
|
||||
MAX_PAYLOAD_CHARS = int(os.environ.get("HERMES_RELAY_MAX_PAYLOAD_CHARS", "12000"))
|
||||
# HTTP replies are the default. Set this explicitly to true only when relay
|
||||
# answers should additionally be delivered through Telegram.
|
||||
TELEGRAM_DELIVERY_ENABLED = os.environ.get(
|
||||
"HERMES_RELAY_TELEGRAM_DELIVERY_ENABLED", "false"
|
||||
).strip().lower() in {"1", "true", "yes", "on"}
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -114,7 +119,7 @@ def _queue_telegram(text: str) -> None:
|
||||
|
||||
@app.route("/ask", methods=["POST"])
|
||||
async def ask():
|
||||
"""Answer a request; Telegram delivery is asynchronous and non-blocking."""
|
||||
"""Answer a request; optional Telegram delivery stays outside the HTTP path."""
|
||||
request_started = time.perf_counter()
|
||||
|
||||
if request.is_json:
|
||||
@@ -161,7 +166,8 @@ async def ask():
|
||||
request_elapsed = round(time.perf_counter() - request_started, 3)
|
||||
logger.info("Answer (llm=%.3fs, request=%.3fs): %s...", llm_elapsed, request_elapsed, answer[:200])
|
||||
|
||||
_queue_telegram(answer)
|
||||
if TELEGRAM_DELIVERY_ENABLED:
|
||||
_queue_telegram(answer)
|
||||
return jsonify({
|
||||
"status": "ok",
|
||||
"answer": answer,
|
||||
@@ -169,7 +175,7 @@ async def ask():
|
||||
"elapsed_seconds": llm_elapsed,
|
||||
"llm_elapsed_seconds": llm_elapsed,
|
||||
"request_elapsed_seconds": request_elapsed,
|
||||
"telegram_queued": True,
|
||||
"telegram_queued": TELEGRAM_DELIVERY_ENABLED,
|
||||
})
|
||||
|
||||
|
||||
@@ -181,6 +187,7 @@ async def health():
|
||||
"version": VERSION,
|
||||
"mode": "async-quart",
|
||||
"profile": "voice-assistant",
|
||||
"telegram_delivery_enabled": TELEGRAM_DELIVERY_ENABLED,
|
||||
"pending_telegram_deliveries": len(_delivery_tasks),
|
||||
}
|
||||
if request.args.get("detail", "false").lower() == "true":
|
||||
@@ -199,8 +206,8 @@ async def startup() -> None:
|
||||
global _hermes_lock
|
||||
_hermes_lock = asyncio.Lock()
|
||||
logger.info(
|
||||
"Hermes Relay v%s startup complete (one serialized agent, delivery_workers=%d, timeout=%ds)",
|
||||
VERSION, DELIVERY_WORKERS, TIMEOUT,
|
||||
"Hermes Relay v%s startup complete (one serialized agent, telegram_delivery=%s, delivery_workers=%d, timeout=%ds)",
|
||||
VERSION, TELEGRAM_DELIVERY_ENABLED, DELIVERY_WORKERS, TIMEOUT,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user