Spaces:
Running
Running
File size: 12,348 Bytes
db944ac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | from __future__ import annotations
import re
import time
from agent import AgentContext
from helpers.print_style import PrintStyle
from plugins._telegram_integration.helpers import telegram_client as tc
from plugins._telegram_integration.helpers.bot_manager import get_bot
from plugins._telegram_integration.helpers.constants import (
CTX_TG_BOT,
CTX_TG_CHAT_ID,
CTX_TG_PROGRESS_LINES,
CTX_TG_PROGRESS_MESSAGE_ID,
CTX_TG_REPLY_TO,
CTX_TG_RESPONSE_LAST_UPDATE,
CTX_TG_RESPONSE_MESSAGE_ID,
CTX_TG_RESPONSE_TEXT,
CTX_TG_STREAM_ENABLED,
CTX_TG_TOOLS_ENABLED,
CTX_TG_ERROR_SENT,
)
MAX_STREAM_CHARS: int = 3900
MIN_RESPONSE_UPDATE_SECONDS: float = 1.0
MAX_PROGRESS_LINES: int = 12
TOOL_EMOJIS: dict[str, str] = {
"browser": "π",
"code": "β¨οΈ",
"code_execution_tool": "β¨οΈ",
"duckduckgo_search": "π",
"read_file": "π",
"file": "π",
"knowledge_tool": "π",
"memory": "π§ ",
"search": "π",
"search_engine": "π",
"search_files": "π",
"skill": "π",
"skill_view": "π",
}
async def start(context: AgentContext) -> None:
context.data.pop(CTX_TG_ERROR_SENT, None)
# Do not pre-create a placeholder Telegram message. Wait until we have
# real assistant text so the stream starts with meaningful content.
if not _stream_enabled(context):
return
async def add_tool_start(
context: AgentContext,
tool_name: str,
args: dict | None = None,
) -> None:
if not _tools_enabled(context):
return
label = _tool_progress_label(tool_name, args or {})
_append_progress_line(context, f"{_tool_emoji(tool_name)} {label}")
await _send_progress(context)
async def add_tool_done(context: AgentContext, tool_name: str, ok: bool = True) -> None:
if not _tools_enabled(context):
return
if ok:
return
_mark_tool_failed(context, tool_name)
await _send_progress(context)
async def update_response(context: AgentContext, response_text: str) -> None:
if not _stream_enabled(context):
return
cleaned = _visible_response_text(response_text)
if not cleaned:
return
context.data[CTX_TG_RESPONSE_TEXT] = response_text or ""
now = time.time()
last = float(context.data.get(CTX_TG_RESPONSE_LAST_UPDATE) or 0.0)
if now - last < MIN_RESPONSE_UPDATE_SECONDS:
return
await _update_response_message(context, response_text or "")
context.data[CTX_TG_RESPONSE_LAST_UPDATE] = now
async def send_intermediate_response(
context: AgentContext,
response_text: str,
keyboard: list[list[dict]] | None = None,
) -> bool:
if context.data.get(CTX_TG_RESPONSE_MESSAGE_ID):
sent = await finalize_response(context, response_text, keyboard)
if sent:
_reset_progress_group(context)
return sent
html = _format_response(response_text)
if not html:
return False
bot = _bot_instance(context)
chat_id = context.data.get(CTX_TG_CHAT_ID)
if not bot or not chat_id:
return False
try:
sent_id = await tc.raw_send_text(
bot.bot.token,
int(chat_id),
html,
parse_mode="HTML",
reply_markup=_keyboard_markup(keyboard),
)
sent = bool(sent_id)
if sent:
_reset_progress_group(context)
return sent
except Exception as e:
PrintStyle.debug(f"Telegram intermediate response failed: {e}")
return False
async def finalize_response(
context: AgentContext,
response_text: str,
keyboard: list[list[dict]] | None = None,
) -> bool:
message_id = context.data.get(CTX_TG_RESPONSE_MESSAGE_ID)
if not message_id:
return False
ok = await _update_response_message(
context,
response_text or context.data.get(CTX_TG_RESPONSE_TEXT) or "",
keyboard=keyboard,
force=True,
)
if ok:
context.data.pop(CTX_TG_RESPONSE_MESSAGE_ID, None)
context.data.pop(CTX_TG_RESPONSE_TEXT, None)
context.data.pop(CTX_TG_RESPONSE_LAST_UPDATE, None)
return ok
def clear(context: AgentContext) -> None:
for key in (
CTX_TG_PROGRESS_LINES,
CTX_TG_PROGRESS_MESSAGE_ID,
CTX_TG_RESPONSE_MESSAGE_ID,
CTX_TG_RESPONSE_TEXT,
CTX_TG_RESPONSE_LAST_UPDATE,
):
context.data.pop(key, None)
def _reset_progress_group(context: AgentContext) -> None:
context.data.pop(CTX_TG_PROGRESS_LINES, None)
context.data.pop(CTX_TG_PROGRESS_MESSAGE_ID, None)
def _stream_enabled(context: AgentContext) -> bool:
value = context.get_data(CTX_TG_STREAM_ENABLED)
return True if value is None else bool(value)
def _tools_enabled(context: AgentContext) -> bool:
value = context.get_data(CTX_TG_TOOLS_ENABLED)
return True if value is None else bool(value)
async def _send_progress(context: AgentContext) -> None:
bot = _bot_instance(context)
chat_id = context.data.get(CTX_TG_CHAT_ID)
if not bot or not chat_id:
return
text = "\n".join(context.data.get(CTX_TG_PROGRESS_LINES) or [])
if not text:
return
message_id = context.data.get(CTX_TG_PROGRESS_MESSAGE_ID)
try:
if message_id:
await tc.raw_edit_text(bot.bot.token, int(chat_id), int(message_id), text, parse_mode=None)
return
sent_id = await tc.raw_send_text(
bot.bot.token,
int(chat_id),
text,
parse_mode=None,
)
if sent_id:
context.data[CTX_TG_PROGRESS_MESSAGE_ID] = sent_id
except Exception as e:
PrintStyle.debug(f"Telegram progress update failed: {e}")
async def _ensure_response_message(context: AgentContext, text: str) -> int | None:
message_id = context.data.get(CTX_TG_RESPONSE_MESSAGE_ID)
if message_id:
return int(message_id)
html = _format_response(text)
if not html:
return None
bot = _bot_instance(context)
chat_id = context.data.get(CTX_TG_CHAT_ID)
if not bot or not chat_id:
return None
sent_id = await tc.raw_send_text(
bot.bot.token,
int(chat_id),
html,
reply_to_message_id=_reply_to(context),
parse_mode="HTML",
)
if sent_id:
context.data[CTX_TG_RESPONSE_MESSAGE_ID] = sent_id
context.data[CTX_TG_RESPONSE_LAST_UPDATE] = time.time()
return sent_id
async def _update_response_message(
context: AgentContext,
text: str,
*,
keyboard: list[list[dict]] | None = None,
force: bool = False,
) -> bool:
had_message = bool(context.data.get(CTX_TG_RESPONSE_MESSAGE_ID))
message_id = await _ensure_response_message(context, text)
bot = _bot_instance(context)
chat_id = context.data.get(CTX_TG_CHAT_ID)
if not message_id or not bot or not chat_id:
return False
markup = _keyboard_markup(keyboard)
html = _format_response(text)
if not had_message and not markup and not force:
return True
try:
ok = await tc.raw_edit_text(
bot.bot.token,
int(chat_id),
int(message_id),
html,
parse_mode="HTML",
reply_markup=markup,
)
if ok or force:
return ok
return False
except Exception as e:
PrintStyle.debug(f"Telegram response update failed: {e}")
return False
def _append_progress_line(context: AgentContext, line: str) -> None:
lines = list(context.data.get(CTX_TG_PROGRESS_LINES) or [])
if lines and lines[-1] == line:
return
lines.append(line)
context.data[CTX_TG_PROGRESS_LINES] = lines[-MAX_PROGRESS_LINES:]
def _mark_tool_failed(context: AgentContext, tool_name: str) -> None:
lines = list(context.data.get(CTX_TG_PROGRESS_LINES) or [])
if not lines:
return
label = _tool_label(tool_name)
for index in range(len(lines) - 1, -1, -1):
line = lines[index]
if _line_matches_tool(line, label):
_, _, suffix = line.partition(" ")
lines[index] = f"β {suffix or label}"
context.data[CTX_TG_PROGRESS_LINES] = lines[-MAX_PROGRESS_LINES:]
return
lines.append(f"β {label}")
context.data[CTX_TG_PROGRESS_LINES] = lines[-MAX_PROGRESS_LINES:]
def _bot_instance(context: AgentContext):
bot_name = context.data.get(CTX_TG_BOT)
return get_bot(bot_name) if bot_name else None
def _reply_to(context: AgentContext) -> int | None:
value = context.data.get(CTX_TG_REPLY_TO)
try:
return int(value) if value else None
except (TypeError, ValueError):
return None
def _tool_label(tool_name: str) -> str:
value = (tool_name or "tool").replace("_", " ").replace("-", " ").strip()
return value or "tool"
def _line_matches_tool(line: str, label: str) -> bool:
_, _, suffix = line.partition(" ")
normalized_suffix = suffix.strip().lower()
normalized_label = label.strip().lower()
return normalized_suffix == normalized_label or normalized_suffix.startswith(f"{normalized_label}:")
def _tool_progress_label(tool_name: str, args: dict) -> str:
label = _tool_label(tool_name)
detail = _tool_detail(tool_name, args)
return f"{label}: {detail}" if detail else label
def _tool_detail(tool_name: str, args: dict) -> str:
if not isinstance(args, dict) or not args:
return ""
normalized = (tool_name or "").strip().lower()
if "search" in normalized:
return _first_arg(args, ("query", "q", "search", "term", "keywords", "pattern"))
if "browser" in normalized:
return _first_arg(args, ("url", "link", "query", "action"))
if "file" in normalized:
return _first_arg(args, ("path", "file", "filename", "query", "pattern"))
if "skill" in normalized:
return _first_arg(args, ("skill", "name", "query", "path"))
return _first_arg(args, ("action", "method", "operation"))
def _first_arg(args: dict, keys: tuple[str, ...]) -> str:
for key in keys:
value = args.get(key)
if value is None:
continue
text = _compact_detail(value)
if text:
return text
return ""
def _compact_detail(value: object) -> str:
if isinstance(value, (list, tuple)):
parts = [_compact_detail(item) for item in value[:2]]
text = ", ".join(part for part in parts if part)
if len(value) > 2:
text = f"{text}, ..."
elif isinstance(value, dict):
return ""
else:
text = str(value).strip()
text = re.sub(r"\s+", " ", text).strip().strip("\"'")
if len(text) > 80:
text = f"{text[:77].rstrip()}..."
return text
def _tool_emoji(tool_name: str) -> str:
normalized = (tool_name or "").strip().lower()
for key, emoji in TOOL_EMOJIS.items():
if key in normalized:
return emoji
return "π οΈ"
def _format_response(text: str) -> str:
value = _visible_response_text(text)
if not value:
return ""
return tc.md_to_telegram_html(value)
def _strip_incomplete_tool_markup(text: str) -> str:
value = text.lstrip()
value = re.sub(r"^<[^>\n]{0,80}$", "", value)
value = re.sub(r"^```(?:json|xml)?\s*$", "", value, flags=re.IGNORECASE)
return value
def _visible_response_text(text: str) -> str:
value = _trim(text or "")
return _strip_incomplete_tool_markup(value)
def _trim(text: str) -> str:
if len(text) <= MAX_STREAM_CHARS:
return text
return text[-MAX_STREAM_CHARS:]
def _keyboard_markup(keyboard: list[list[dict]] | None) -> dict | None:
if not keyboard:
return None
rows: list[list[dict[str, str]]] = []
for row in keyboard:
out_row: list[dict[str, str]] = []
for button in row:
text = str(button.get("text") or "")[:64]
if not text:
continue
if button.get("url"):
out_row.append({"text": text, "url": str(button["url"])})
else:
data = str(button.get("callback_data", text))
out_row.append({"text": text, "callback_data": data[:64]})
if out_row:
rows.append(out_row)
return {"inline_keyboard": rows} if rows else None
|