add tool calling
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
"""LangChain tools for the Discord bot."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from langchain_core.tools import tool
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _format_member(member: Any) -> str:
|
||||
"""Format a single member for display."""
|
||||
raw_display = getattr(member, "display_name", None)
|
||||
raw_name = getattr(member, "name", "Unknown")
|
||||
display_name = str(raw_display) if raw_display else str(raw_name)
|
||||
parts: list[str] = [display_name]
|
||||
|
||||
nick = getattr(member, "nick", None)
|
||||
if nick and nick != display_name:
|
||||
parts.append(f"(nickname: {nick})")
|
||||
|
||||
global_name = getattr(member, "global_name", None)
|
||||
if global_name and global_name != getattr(member, "name", ""):
|
||||
parts.append(f"(global name: {global_name})")
|
||||
|
||||
status = getattr(member, "status", None)
|
||||
if status:
|
||||
parts.append(f"[{status.value}]")
|
||||
|
||||
return " ".join(parts)
|
||||
|
||||
|
||||
def get_channel_members_impl(channel: Any) -> str:
|
||||
"""Get a list of all members in the Discord channel the bot is part of.
|
||||
|
||||
Use this tool when asked about who is in the channel, who the members are,
|
||||
or to get a roster of people present in the current channel.
|
||||
|
||||
Returns:
|
||||
A formatted string listing all members in the channel with their usernames,
|
||||
display names, and nicknames.
|
||||
|
||||
"""
|
||||
try:
|
||||
members = channel.members
|
||||
if not members:
|
||||
return "No members found in this channel."
|
||||
|
||||
lines: list[str] = [f"Members in this channel ({len(members)} total):"]
|
||||
for member in sorted(
|
||||
members,
|
||||
key=lambda m: (
|
||||
getattr(m, "display_name", "") or getattr(m, "name", "")
|
||||
).lower(),
|
||||
):
|
||||
lines.append(f" - {_format_member(member)}")
|
||||
|
||||
return "\n".join(lines)
|
||||
except Exception:
|
||||
logger.exception("Error fetching channel members")
|
||||
return "Failed to retrieve channel members."
|
||||
|
||||
|
||||
@tool
|
||||
def get_channel_members() -> str:
|
||||
"""Get a list of all members in the Discord channel the bot is part of.
|
||||
|
||||
Use this tool when asked about who is in the channel, who the members are,
|
||||
or to get a roster of people present in the current channel.
|
||||
|
||||
Returns:
|
||||
A formatted string listing all members in the channel with their usernames,
|
||||
display names, and nicknames.
|
||||
|
||||
"""
|
||||
return "No channel provided."
|
||||
Reference in New Issue
Block a user