# Backwards-compatible refactoring of the Slack toolkit Several performance improvements, streamlined tool set, and easier to understand tool interfaces. All "old" tools were kept for backwards compatibility, with the same interfaces and response structure (but using the new and more performant tools under the hood). Full revision of unit-tests and evals. ## Streamlined tool set Multiple groups of tools were streamlined into a single one: * "get conversation metadata" from 5 tools to one; * "send message" from 2 tools to one; * "get users in conversation" from 3 tools to one; * "get messages" from 4 tools to one ## New capabilities * Messages retrieved are now populated with the users' names, apart from ID: makes it easier for LLMs to reference who sent a message, were mentioned, or reacted to a message * Retrieve users by username, email, and/or ID (before we only supported ID) * Retrieve multiple users in a single tool call ## Concurrency controls All operations issuing multiple requests concurrently now have a `Semaphore` to limit the concurrency level. The limit can be controlled through the `SLACK_MAX_CONCURRENT_REQUESTS` env var (defaults to 3). ## Networking performance improvement Various operations that used to make multiple API calls are now executed more efficiently: ### Find users by username * Before: a full scan of `users_list` was required (potentially multiple pages for large workspaces); * Now it stops as soon as we have all users needed (yes, it was dumb before) ### Get multiple users by their IDs * Before: for each user ID, we made one API call to the `users_info` endpoint * Now: we call `list_users` and scan the results to match the user IDs (an estimate of 99.5% of Slack workspaces have < 200 users; for large workspaces, we may need to paginate `list_users`) ### Get a conversation by its users * Before: * Call to `list_conversations` (potentially paginating) * For each conversation, one call to `conversations_members` (potentially paginating) * Then loop and find which conversation matches the users' IDs * Now: * A single call to `conversations_open`
163 lines
4.6 KiB
Python
163 lines
4.6 KiB
Python
import random
|
|
import string
|
|
from collections.abc import Callable
|
|
|
|
import pytest
|
|
from arcade_tdk import ToolAuthorizationContext, ToolContext
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_context():
|
|
mock_auth = ToolAuthorizationContext(token="fake-token") # noqa: S106
|
|
return ToolContext(authorization=mock_auth)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_chat_slack_client(mocker):
|
|
mock_client = mocker.patch("arcade_slack.tools.chat.AsyncWebClient", autospec=True)
|
|
return mock_client.return_value
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_users_slack_client(mocker):
|
|
mock_client = mocker.patch("arcade_slack.tools.users.AsyncWebClient", autospec=True)
|
|
return mock_client.return_value
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_user_retrieval_slack_client(mocker):
|
|
mock_client = mocker.patch("arcade_slack.user_retrieval.AsyncWebClient", autospec=True)
|
|
return mock_client.return_value
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_conversation_retrieval_slack_client(mocker):
|
|
mock_client = mocker.patch("arcade_slack.conversation_retrieval.AsyncWebClient", autospec=True)
|
|
return mock_client.return_value
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_message_retrieval_slack_client(mocker):
|
|
mock_client = mocker.patch("arcade_slack.message_retrieval.AsyncWebClient", autospec=True)
|
|
return mock_client.return_value
|
|
|
|
|
|
@pytest.fixture
|
|
def random_str_factory():
|
|
def random_str_factory(length: int = 10):
|
|
return "".join(random.choices(string.ascii_letters + string.digits, k=length)) # noqa: S311
|
|
|
|
return random_str_factory
|
|
|
|
|
|
@pytest.fixture
|
|
def random_ts_factory():
|
|
def random_ts_factory():
|
|
return f"{random.uniform(1735689600.000000, 1751327999.999999)}" # noqa: S311
|
|
|
|
return random_ts_factory
|
|
|
|
|
|
@pytest.fixture
|
|
def dummy_channel_factory(random_str_factory: Callable[[int], str]):
|
|
def dummy_channel_factory(
|
|
id_: str | None = None,
|
|
name: str | None = None,
|
|
is_member: bool = True,
|
|
is_private: bool = False,
|
|
is_archived: bool = False,
|
|
is_channel: bool = False,
|
|
is_im: bool = False,
|
|
is_mpim: bool = False,
|
|
num_members: int | None = None,
|
|
user: str | None = None,
|
|
is_user_deleted: bool = False,
|
|
):
|
|
channel = {
|
|
"id": id_ or f"channel_id_{random_str_factory()}",
|
|
"is_member": is_member,
|
|
"is_private": is_private,
|
|
"is_archived": is_archived,
|
|
}
|
|
|
|
if name or is_channel or is_mpim:
|
|
channel["name"] = name or f"channel_name_{random_str_factory()}"
|
|
|
|
if is_channel:
|
|
channel["is_channel"] = True
|
|
if is_im:
|
|
channel["is_im"] = True
|
|
if is_mpim:
|
|
channel["is_group"] = True
|
|
if num_members:
|
|
channel["num_members"] = num_members
|
|
if user or is_im:
|
|
channel["user"] = user or f"user_id_{random_str_factory()}"
|
|
if is_user_deleted:
|
|
channel["is_user_deleted"] = is_user_deleted
|
|
|
|
return channel
|
|
|
|
return dummy_channel_factory
|
|
|
|
|
|
@pytest.fixture
|
|
def dummy_user_factory(random_str_factory: Callable[[int], str]):
|
|
def dummy_user_factory(
|
|
id_: str | None = None,
|
|
name: str | None = None,
|
|
email: str | None = None,
|
|
is_bot: bool = False,
|
|
):
|
|
return {
|
|
"id": id_ or random_str_factory(),
|
|
"name": name or random_str_factory(),
|
|
"profile": {
|
|
"email": email or f"{random_str_factory()}@{random_str_factory()}.com",
|
|
},
|
|
"is_bot": is_bot,
|
|
}
|
|
|
|
return dummy_user_factory
|
|
|
|
|
|
@pytest.fixture
|
|
def dummy_reaction_factory(random_str_factory):
|
|
def reaction_factory(
|
|
name: str | None = None,
|
|
user_ids: list[str] | None = None,
|
|
count: int | None = None,
|
|
):
|
|
count = count or random.randint(1, 10) # noqa: S311
|
|
if user_ids:
|
|
count = len(user_ids)
|
|
return {
|
|
"count": count,
|
|
"name": name or random_str_factory(),
|
|
"users": user_ids or [random_str_factory() for _ in range(count)],
|
|
}
|
|
|
|
return reaction_factory
|
|
|
|
|
|
@pytest.fixture
|
|
def dummy_message_factory(random_str_factory, random_ts_factory):
|
|
def message_factory(
|
|
user_id: str | None = None,
|
|
text: str | None = None,
|
|
reactions: list[dict] | None = None,
|
|
type_: str = "message",
|
|
ts: float | None = None,
|
|
):
|
|
message = {
|
|
"user": user_id or random_str_factory(),
|
|
"text": text or random_str_factory(),
|
|
"type": type_,
|
|
"ts": ts or random_ts_factory(),
|
|
}
|
|
|
|
if reactions:
|
|
message["reactions"] = reactions
|
|
return message
|
|
|
|
return message_factory
|