refactor(tools): fold small conditionals in send_message helpers
This commit is contained in:
@@ -124,14 +124,9 @@ REACT_TO_MESSAGE_SCHEMA = {
|
||||
|
||||
|
||||
registry.register(
|
||||
name="react_to_message",
|
||||
toolset="desktop_ui",
|
||||
schema=REACT_TO_MESSAGE_SCHEMA,
|
||||
name="react_to_message", toolset="desktop_ui", schema=REACT_TO_MESSAGE_SCHEMA,
|
||||
handler=lambda args, **kw: react_to_message_tool(
|
||||
emoji=args.get("emoji", ""),
|
||||
message_row_id=args.get("message_row_id"),
|
||||
messages_back=args.get("messages_back"),
|
||||
),
|
||||
check_fn=check_react_requirements,
|
||||
emoji="💛",
|
||||
emoji=args.get("emoji", ""), message_row_id=args.get("message_row_id"),
|
||||
messages_back=args.get("messages_back")),
|
||||
check_fn=check_react_requirements, emoji="💛",
|
||||
)
|
||||
|
||||
@@ -286,9 +286,7 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No
|
||||
media_files = media_files or []
|
||||
thread_kwargs = _telegram_thread_kwargs(thread_id)
|
||||
# disable_web_page_preview is only valid for send_message, not media sends.
|
||||
text_kwargs = dict(thread_kwargs)
|
||||
if disable_link_previews:
|
||||
text_kwargs["disable_web_page_preview"] = True
|
||||
text_kwargs = {**thread_kwargs, **({"disable_web_page_preview": True} if disable_link_previews else {})}
|
||||
last_msg, warnings = None, []
|
||||
|
||||
# MEDIA caption: a single captionable file + short text rides on the bubble as
|
||||
@@ -694,8 +692,7 @@ async def _send_qqbot(pconfig, chat_id, message):
|
||||
from gateway.config import _getenv
|
||||
extra = pconfig.extra or {}
|
||||
appid = extra.get("app_id") or _getenv("QQ_APP_ID", "")
|
||||
secret = (pconfig.token or extra.get("client_secret")
|
||||
or _getenv("QQ_CLIENT_SECRET", ""))
|
||||
secret = pconfig.token or extra.get("client_secret") or _getenv("QQ_CLIENT_SECRET", "")
|
||||
if not appid or not secret:
|
||||
return _error("QQBot: QQ_APP_ID / QQ_CLIENT_SECRET not configured.")
|
||||
|
||||
@@ -711,8 +708,7 @@ async def _send_qqbot(pconfig, chat_id, message):
|
||||
return _error("QQBot: no access_token in response")
|
||||
|
||||
# Separate endpoints for guild channels, C2C (private) and groups; first 2xx wins.
|
||||
headers = {
|
||||
"Authorization": f"QQBot {access_token}", "Content-Type": "application/json"}
|
||||
headers = {"Authorization": f"QQBot {access_token}", "Content-Type": "application/json"}
|
||||
payload = {"content": message[:4000], "msg_type": 0}
|
||||
endpoints = (
|
||||
("channel", f"https://api.sgroup.qq.com/channels/{chat_id}/messages"),
|
||||
|
||||
@@ -151,11 +151,8 @@ def _parse_target_ref(platform_name: str, target_ref: str):
|
||||
return parsed[0], parsed[1], True
|
||||
if platform_name in _PHONE_PLATFORMS and _E164_TARGET_RE.fullmatch(target_ref):
|
||||
return target_ref.strip(), None, True
|
||||
if target_ref.lstrip("-").isdigit():
|
||||
return target_ref, None, True
|
||||
if platform_name == "matrix" and target_ref.startswith(("!", "@")):
|
||||
return target_ref, None, True
|
||||
if platform_name == "xmpp" and "@" in target_ref:
|
||||
if (target_ref.lstrip("-").isdigit() or (platform_name == "matrix" and target_ref.startswith(("!", "@")))
|
||||
or (platform_name == "xmpp" and "@" in target_ref)):
|
||||
return target_ref, None, True
|
||||
return None, None, False
|
||||
|
||||
@@ -187,9 +184,8 @@ def resolve_send_target(
|
||||
return f"Target validator failed for platform '{platform_name}'"
|
||||
if verdict is True:
|
||||
return None
|
||||
if isinstance(verdict, str) and verdict:
|
||||
return f"Invalid target '{target_ref}' on {platform_name}: {verdict}"
|
||||
return f"Invalid target '{target_ref}' on {platform_name}"
|
||||
detail = f": {verdict}" if isinstance(verdict, str) and verdict else ""
|
||||
return f"Invalid target '{target_ref}' on {platform_name}{detail}"
|
||||
|
||||
def _validated(chat_id, thread_id):
|
||||
error = _validate(chat_id)
|
||||
|
||||
@@ -5,6 +5,7 @@ import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from functools import partial
|
||||
|
||||
from agent.secret_scope import get_secret
|
||||
|
||||
@@ -106,9 +107,7 @@ def _handle_react(args, remove=False):
|
||||
if not callable(react_fn):
|
||||
return tool_error(f"Platform '{platform_name}' does not support message reactions.")
|
||||
|
||||
kwargs = {"chat_id": chat_id, "message_id": message_id}
|
||||
if not remove:
|
||||
kwargs["emoji"] = emoji
|
||||
kwargs = {"chat_id": chat_id, "message_id": message_id, **({} if remove else {"emoji": emoji})}
|
||||
try:
|
||||
from model_tools import _run_async
|
||||
result = _run_async(react_fn(**kwargs))
|
||||
@@ -537,14 +536,11 @@ _CHUNKED_ROUTES = {
|
||||
"wecom": (True, None, _via_adapter_route)}
|
||||
|
||||
|
||||
def _registry_text_sender(name):
|
||||
return lambda pc, cid, chunk, tid: _registry_standalone_send(name, pc, cid, chunk, tid)
|
||||
|
||||
|
||||
# Text-only senders for built-in platforms (generic path; media is dropped with a
|
||||
# warning). Signature: (pconfig, chat_id, chunk, thread_id) -> result.
|
||||
_TEXT_SENDERS = {
|
||||
**{name: _registry_text_sender(name) for name in ("whatsapp", "email", "sms", "dingtalk", "feishu", "wecom")},
|
||||
**{name: partial(_registry_standalone_send, name)
|
||||
for name in ("whatsapp", "email", "sms", "dingtalk", "feishu", "wecom")},
|
||||
"signal": lambda pc, cid, chunk, tid: _send_signal(pc.extra, cid, chunk),
|
||||
"bluebubbles": lambda pc, cid, chunk, tid: _send_bluebubbles(pc.extra, cid, chunk),
|
||||
"qqbot": lambda pc, cid, chunk, tid: _send_qqbot(pc, cid, chunk),
|
||||
@@ -611,9 +607,7 @@ async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None,
|
||||
try:
|
||||
import inspect
|
||||
result = handler(args or {}, chat_id, platform_name, pconfig)
|
||||
if inspect.isawaitable(result):
|
||||
result = await result
|
||||
return result
|
||||
return await result if inspect.isawaitable(result) else result
|
||||
except Exception as e:
|
||||
return {"error": f"Plugin send_message handler failed: {e}"}
|
||||
# Plugin platform: live gateway adapter if available, else standalone_sender_fn.
|
||||
|
||||
Reference in New Issue
Block a user