Add Discord attachment reading and web search capabilities
- Discord channel now downloads and extracts text from attachments (text files, PDFs) - Added WebSearchTool using DuckDuckGo for researcher and coder agents - Improved WebFetchTool with User-Agent header and HTML-to-text stripping - Added pypdf and duckduckgo-search dependencies Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,14 +3,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import io
|
||||
import os
|
||||
|
||||
import discord
|
||||
import httpx
|
||||
from loguru import logger
|
||||
|
||||
from xtrm_agent.bus import InboundMessage, MessageBus, OutboundMessage
|
||||
from xtrm_agent.bus import Attachment, InboundMessage, MessageBus, OutboundMessage
|
||||
from xtrm_agent.channels.base import BaseChannel
|
||||
|
||||
# Extensions treated as plain text (decoded as UTF-8)
|
||||
_TEXT_EXTENSIONS = frozenset({
|
||||
".txt", ".py", ".md", ".json", ".yaml", ".yml", ".csv", ".log",
|
||||
".js", ".ts", ".html", ".css", ".xml", ".toml", ".ini", ".sh",
|
||||
".sql", ".rs", ".go", ".java", ".c", ".cpp", ".h", ".rb", ".php",
|
||||
".swift", ".kt", ".r", ".cfg", ".env", ".conf", ".dockerfile",
|
||||
".makefile", ".bat", ".ps1", ".lua", ".zig", ".hs",
|
||||
})
|
||||
|
||||
_IMAGE_EXTENSIONS = frozenset({".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".svg"})
|
||||
|
||||
_MAX_ATTACHMENT_SIZE = 1_024_000 # 1 MB
|
||||
|
||||
|
||||
class DiscordChannel(BaseChannel):
|
||||
"""Discord bot channel."""
|
||||
@@ -54,12 +69,15 @@ class DiscordChannel(BaseChannel):
|
||||
if self.client.user:
|
||||
content = content.replace(f"<@{self.client.user.id}>", "").strip()
|
||||
|
||||
attachments = await self._extract_attachments(message.attachments)
|
||||
|
||||
msg = InboundMessage(
|
||||
channel="discord",
|
||||
sender_id=str(message.author.id),
|
||||
chat_id=str(message.channel.id),
|
||||
content=content,
|
||||
metadata={"guild_id": str(message.guild.id) if message.guild else ""},
|
||||
attachments=attachments,
|
||||
)
|
||||
await self.bus.publish_inbound(msg)
|
||||
|
||||
@@ -71,6 +89,80 @@ class DiscordChannel(BaseChannel):
|
||||
except asyncio.TimeoutError:
|
||||
await message.channel.send("Sorry, I timed out processing your request.")
|
||||
|
||||
async def _extract_attachments(
|
||||
self, discord_attachments: list[discord.Attachment]
|
||||
) -> list[Attachment]:
|
||||
"""Download Discord attachments and extract text content."""
|
||||
results: list[Attachment] = []
|
||||
for att in discord_attachments:
|
||||
name = att.filename.lower()
|
||||
ext = "." + name.rsplit(".", 1)[-1] if "." in name else ""
|
||||
|
||||
if att.size > _MAX_ATTACHMENT_SIZE:
|
||||
results.append(Attachment(
|
||||
filename=att.filename,
|
||||
content=f"(file skipped — {att.size / 1_048_576:.1f} MB exceeds 1 MB limit)",
|
||||
))
|
||||
continue
|
||||
|
||||
if ext in _IMAGE_EXTENSIONS:
|
||||
results.append(Attachment(
|
||||
filename=att.filename,
|
||||
content="(image attached — cannot read image content)",
|
||||
))
|
||||
continue
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=30) as client:
|
||||
resp = await client.get(att.url)
|
||||
resp.raise_for_status()
|
||||
raw = resp.content
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to download attachment {att.filename}: {e}")
|
||||
results.append(Attachment(
|
||||
filename=att.filename,
|
||||
content=f"(failed to download: {e})",
|
||||
))
|
||||
continue
|
||||
|
||||
if ext == ".pdf":
|
||||
try:
|
||||
from pypdf import PdfReader
|
||||
|
||||
reader = PdfReader(io.BytesIO(raw))
|
||||
text = "\n".join(
|
||||
page.extract_text() or "" for page in reader.pages
|
||||
).strip()
|
||||
if text:
|
||||
results.append(Attachment(filename=att.filename, content=text))
|
||||
else:
|
||||
results.append(Attachment(
|
||||
filename=att.filename,
|
||||
content="(PDF has no extractable text)",
|
||||
))
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to extract PDF text from {att.filename}: {e}")
|
||||
results.append(Attachment(
|
||||
filename=att.filename,
|
||||
content=f"(failed to read PDF: {e})",
|
||||
))
|
||||
elif ext in _TEXT_EXTENSIONS or (att.content_type and att.content_type.startswith("text/")):
|
||||
try:
|
||||
text = raw.decode("utf-8", errors="replace")
|
||||
results.append(Attachment(filename=att.filename, content=text))
|
||||
except Exception as e:
|
||||
results.append(Attachment(
|
||||
filename=att.filename,
|
||||
content=f"(failed to decode text: {e})",
|
||||
))
|
||||
else:
|
||||
results.append(Attachment(
|
||||
filename=att.filename,
|
||||
content=f"(unsupported file type: {ext or 'unknown'})",
|
||||
))
|
||||
|
||||
return results
|
||||
|
||||
async def _send_chunked(
|
||||
self, channel: discord.abc.Messageable, content: str
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user