mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-06-10 09:25:57 +00:00
fix(skills): harden slash skill activation across chat channels (#3466)
* support slash skill activation * format slash skill activation * Preserve slash skill activation with uploads * Address slash skill review feedback * Address slash skill follow-up review * Fix lazy slash skill storage resolution * Keep slash skill activation out of system prompt * Address slash skill review issues * fix: harden slash skill command handling * feat(frontend): add slash skill autocomplete * fix: address slash skill review feedback * fix: preserve slash skill text for IM uploads
This commit is contained in:
@@ -21,6 +21,42 @@ from app.channels.message_bus import (
|
||||
ResolvedAttachment,
|
||||
)
|
||||
from app.channels.store import ChannelStore
|
||||
from deerflow.skills.types import Skill, SkillCategory
|
||||
from deerflow.utils.messages import ORIGINAL_USER_CONTENT_KEY
|
||||
|
||||
|
||||
def test_known_channel_command_detection_only_matches_control_commands():
|
||||
from app.channels.commands import is_known_channel_command
|
||||
|
||||
assert is_known_channel_command("/new")
|
||||
assert is_known_channel_command("/HELP now")
|
||||
assert not is_known_channel_command("/mnt/user-data/uploads/report.pdf")
|
||||
assert not is_known_channel_command("/data-analysis analyze uploads/foo.csv")
|
||||
assert not is_known_channel_command(" /new")
|
||||
|
||||
|
||||
def _make_channel_skill(tmp_path: Path, name: str, *, enabled: bool = True) -> Skill:
|
||||
skill_dir = tmp_path / name
|
||||
skill_dir.mkdir(parents=True, exist_ok=True)
|
||||
skill_file = skill_dir / "SKILL.md"
|
||||
skill_file.write_text(f"# {name}\n", encoding="utf-8")
|
||||
return Skill(
|
||||
name=name,
|
||||
description=f"Description for {name}",
|
||||
license="MIT",
|
||||
skill_dir=skill_dir,
|
||||
skill_file=skill_file,
|
||||
relative_path=Path(name),
|
||||
category=SkillCategory.CUSTOM,
|
||||
enabled=enabled,
|
||||
)
|
||||
|
||||
|
||||
def _make_channel_skill_storage(skills: list[Skill]):
|
||||
return SimpleNamespace(
|
||||
load_skills=lambda *, enabled_only: [skill for skill in skills if skill.enabled] if enabled_only else skills,
|
||||
get_container_root=lambda: "/mnt/skills",
|
||||
)
|
||||
|
||||
|
||||
def _run(coro):
|
||||
@@ -1334,6 +1370,496 @@ class TestChannelManager:
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_blank_text_is_reported_without_running_agent(self):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text=" ",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.wait.assert_not_called()
|
||||
assert outbound_received[0].text.startswith("Unknown command.")
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_rejects_multi_slash_control_command(self):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text="//help",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.wait.assert_not_called()
|
||||
assert outbound_received[0].text.startswith("Unknown command: //help.")
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_requires_control_command_at_start(self):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
|
||||
mock_client = _make_mock_langgraph_client(thread_id="new-thread-456")
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text=" /new",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.threads.create.assert_not_called()
|
||||
assert store.get_thread_id("test", "chat1") is None
|
||||
assert outbound_received[0].text.startswith("Unknown command: /new.")
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_outbound_thread_id_uses_topic_thread(self):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
store.set_thread_id("test", "chat1", "base-thread")
|
||||
store.set_thread_id("test", "chat1", "topic-thread", topic_id="topic-1")
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text="/status",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
topic_id="topic-1",
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
assert outbound_received[0].text == "Active thread: topic-thread"
|
||||
assert outbound_received[0].thread_id == "topic-thread"
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_slash_skill_routes_to_chat(self, tmp_path):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
manager._skill_storage = _make_channel_skill_storage([_make_channel_skill(tmp_path, "data-analysis")])
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text="/data-analysis analyze uploads/foo.csv",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.wait.assert_called_once()
|
||||
call_args = mock_client.runs.wait.call_args
|
||||
assert call_args[1]["input"]["messages"][0]["content"] == "/data-analysis analyze uploads/foo.csv"
|
||||
assert outbound_received[0].text == "Hello from agent!"
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_slash_skill_with_attachment_preserves_original_content(self, monkeypatch, tmp_path):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def fake_ingest(thread_id, msg):
|
||||
return [
|
||||
{
|
||||
"filename": "report.pdf",
|
||||
"size": 12,
|
||||
"path": "/mnt/user-data/uploads/report.pdf",
|
||||
"is_image": False,
|
||||
}
|
||||
]
|
||||
|
||||
monkeypatch.setattr("app.channels.manager._ingest_inbound_files", fake_ingest)
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
manager._skill_storage = _make_channel_skill_storage([_make_channel_skill(tmp_path, "data-analysis")])
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
original_text = "/data-analysis analyze report.pdf"
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text=original_text,
|
||||
files=[{"filename": "report.pdf"}],
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.wait.assert_called_once()
|
||||
human_message = mock_client.runs.wait.call_args[1]["input"]["messages"][0]
|
||||
assert human_message["content"].startswith("<uploaded_files>")
|
||||
assert original_text in human_message["content"]
|
||||
assert human_message["additional_kwargs"][ORIGINAL_USER_CONTENT_KEY] == original_text
|
||||
assert outbound_received[0].text == "Hello from agent!"
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_streaming_slash_skill_with_attachment_preserves_original_content(self, monkeypatch, tmp_path):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def fake_ingest(thread_id, msg):
|
||||
return [
|
||||
{
|
||||
"filename": "report.pdf",
|
||||
"size": 12,
|
||||
"path": "/mnt/user-data/uploads/report.pdf",
|
||||
"is_image": False,
|
||||
}
|
||||
]
|
||||
|
||||
monkeypatch.setattr("app.channels.manager._ingest_inbound_files", fake_ingest)
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
manager._skill_storage = _make_channel_skill_storage([_make_channel_skill(tmp_path, "data-analysis")])
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
mock_client.runs.stream = MagicMock(
|
||||
return_value=_make_async_iterator(
|
||||
[
|
||||
_make_stream_part(
|
||||
"values",
|
||||
{"messages": [{"type": "ai", "content": "streamed response"}]},
|
||||
)
|
||||
]
|
||||
)
|
||||
)
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
original_text = "/data-analysis analyze report.pdf"
|
||||
inbound = InboundMessage(
|
||||
channel_name="feishu",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text=original_text,
|
||||
files=[{"filename": "report.pdf"}],
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: any(message.is_final for message in outbound_received))
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.stream.assert_called_once()
|
||||
human_message = mock_client.runs.stream.call_args[1]["input"]["messages"][0]
|
||||
assert human_message["content"].startswith("<uploaded_files>")
|
||||
assert original_text in human_message["content"]
|
||||
assert human_message["additional_kwargs"][ORIGINAL_USER_CONTENT_KEY] == original_text
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_slash_skill_requires_command_at_start(self, tmp_path):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
manager._skill_storage = _make_channel_skill_storage([_make_channel_skill(tmp_path, "data-analysis")])
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text=" /data-analysis analyze uploads/foo.csv",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.wait.assert_not_called()
|
||||
assert outbound_received[0].text.startswith("Unknown command: /data-analysis.")
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_slash_skill_respects_custom_agent_skill_whitelist(self, monkeypatch, tmp_path):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
monkeypatch.setattr("app.channels.manager.load_agent_config", lambda name: SimpleNamespace(skills=["frontend-design"]))
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(
|
||||
bus=bus,
|
||||
store=store,
|
||||
default_session={"assistant_id": "analyst-agent"},
|
||||
)
|
||||
manager._skill_storage = _make_channel_skill_storage([_make_channel_skill(tmp_path, "data-analysis")])
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text="/data-analysis analyze uploads/foo.csv",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.wait.assert_not_called()
|
||||
assert outbound_received[0].text == "Skill `/data-analysis` is not available for this agent."
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_slash_skill_reports_disabled_skill(self, tmp_path):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
manager._skill_storage = _make_channel_skill_storage([_make_channel_skill(tmp_path, "data-analysis", enabled=False)])
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text="/data-analysis analyze uploads/foo.csv",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.wait.assert_not_called()
|
||||
assert outbound_received[0].text == "Skill `/data-analysis` is installed but disabled. Enable it before using slash activation."
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_uninstalled_slash_skill_stays_unknown_command(self, tmp_path):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
manager._skill_storage = _make_channel_skill_storage([_make_channel_skill(tmp_path, "frontend-design")])
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text="/data-analysis analyze uploads/foo.csv",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.wait.assert_not_called()
|
||||
assert outbound_received[0].text.startswith("Unknown command: /data-analysis.")
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_slash_skill_resolution_error_is_reported(self, monkeypatch):
|
||||
from app.channels.manager import ChannelManager, SlashSkillCommandResolutionError
|
||||
|
||||
def fail_resolution(text, available_skills=None, storage=None):
|
||||
raise SlashSkillCommandResolutionError("Failed to resolve slash skill command. Please check the skill configuration.")
|
||||
|
||||
monkeypatch.setattr("app.channels.manager._resolve_slash_skill_command", fail_resolution)
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
store = ChannelStore(path=Path(tempfile.mkdtemp()) / "store.json")
|
||||
manager = ChannelManager(bus=bus, store=store)
|
||||
store.set_thread_id("test", "chat1", "base-thread")
|
||||
store.set_thread_id("test", "chat1", "topic-thread", topic_id="topic-1")
|
||||
|
||||
mock_client = _make_mock_langgraph_client()
|
||||
manager._client = mock_client
|
||||
|
||||
outbound_received = []
|
||||
|
||||
async def capture_outbound(msg):
|
||||
outbound_received.append(msg)
|
||||
|
||||
bus.subscribe_outbound(capture_outbound)
|
||||
await manager.start()
|
||||
|
||||
inbound = InboundMessage(
|
||||
channel_name="test",
|
||||
chat_id="chat1",
|
||||
user_id="user1",
|
||||
text="/data-analysis analyze uploads/foo.csv",
|
||||
msg_type=InboundMessageType.COMMAND,
|
||||
topic_id="topic-1",
|
||||
)
|
||||
await bus.publish_inbound(inbound)
|
||||
await _wait_for(lambda: len(outbound_received) >= 1)
|
||||
await manager.stop()
|
||||
|
||||
mock_client.runs.wait.assert_not_called()
|
||||
assert outbound_received[0].text == "Failed to resolve slash skill command. Please check the skill configuration."
|
||||
assert outbound_received[0].thread_id == "topic-thread"
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_handle_command_new(self):
|
||||
from app.channels.manager import ChannelManager
|
||||
|
||||
@@ -2440,6 +2966,36 @@ class TestWeComChannel:
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_publish_ws_inbound_treats_slash_prefixed_paths_as_chat(self, monkeypatch):
|
||||
from app.channels.wecom import WeComChannel
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
bus.publish_inbound = AsyncMock()
|
||||
channel = WeComChannel(bus, config={})
|
||||
channel._ws_client = SimpleNamespace(reply_stream=AsyncMock())
|
||||
|
||||
monkeypatch.setitem(
|
||||
__import__("sys").modules,
|
||||
"aibot",
|
||||
SimpleNamespace(generate_req_id=lambda prefix: "stream-1"),
|
||||
)
|
||||
|
||||
frame = {
|
||||
"body": {
|
||||
"msgid": "msg-1",
|
||||
"from": {"userid": "user-1"},
|
||||
}
|
||||
}
|
||||
|
||||
await channel._publish_ws_inbound(frame, "/mnt/user-data/uploads/report.pdf")
|
||||
|
||||
inbound = bus.publish_inbound.await_args.args[0]
|
||||
assert inbound.text == "/mnt/user-data/uploads/report.pdf"
|
||||
assert inbound.msg_type == InboundMessageType.CHAT
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_on_outbound_sends_attachment_before_clearing_context(self, tmp_path):
|
||||
from app.channels.wecom import WeComChannel
|
||||
|
||||
@@ -2788,6 +3344,219 @@ class TestSlackAllowedUsers:
|
||||
assert inbound.chat_id == "C123"
|
||||
assert inbound.text == "hello from slack"
|
||||
|
||||
def test_app_mention_strips_leading_bot_mention_before_command_detection(self):
|
||||
from app.channels.slack import SlackChannel
|
||||
|
||||
bus = MessageBus()
|
||||
bus.publish_inbound = AsyncMock()
|
||||
channel = SlackChannel(bus=bus, config={"bot_user_id": "UBOT"})
|
||||
channel._loop = MagicMock()
|
||||
channel._loop.is_running.return_value = True
|
||||
channel._add_reaction = MagicMock()
|
||||
channel._send_running_reply = MagicMock()
|
||||
|
||||
event = {
|
||||
"type": "app_mention",
|
||||
"user": "U123456",
|
||||
"text": "<@UBOT> /help",
|
||||
"channel": "C123",
|
||||
"ts": "1710000000.000100",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"app.channels.slack.asyncio.run_coroutine_threadsafe",
|
||||
side_effect=self._submit_coro,
|
||||
):
|
||||
channel._handle_message_event(event)
|
||||
|
||||
inbound = bus.publish_inbound.call_args.args[0]
|
||||
assert inbound.text == "/help"
|
||||
assert inbound.msg_type == InboundMessageType.COMMAND
|
||||
|
||||
def test_app_mention_strips_labelled_leading_bot_mention(self):
|
||||
from app.channels.slack import SlackChannel
|
||||
|
||||
bus = MessageBus()
|
||||
bus.publish_inbound = AsyncMock()
|
||||
channel = SlackChannel(bus=bus, config={"bot_user_id": "UBOT"})
|
||||
channel._loop = MagicMock()
|
||||
channel._loop.is_running.return_value = True
|
||||
channel._add_reaction = MagicMock()
|
||||
channel._send_running_reply = MagicMock()
|
||||
|
||||
event = {
|
||||
"type": "app_mention",
|
||||
"user": "U123456",
|
||||
"text": "<@UBOT|deerflow> /help",
|
||||
"channel": "C123",
|
||||
"ts": "1710000000.000100",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"app.channels.slack.asyncio.run_coroutine_threadsafe",
|
||||
side_effect=self._submit_coro,
|
||||
):
|
||||
channel._handle_message_event(event)
|
||||
|
||||
inbound = bus.publish_inbound.call_args.args[0]
|
||||
assert inbound.text == "/help"
|
||||
assert inbound.msg_type == InboundMessageType.COMMAND
|
||||
|
||||
def test_app_mention_strips_leading_bot_mention_before_slash_skill(self):
|
||||
from app.channels.slack import SlackChannel
|
||||
|
||||
bus = MessageBus()
|
||||
bus.publish_inbound = AsyncMock()
|
||||
channel = SlackChannel(bus=bus, config={"bot_user_id": "UBOT"})
|
||||
channel._loop = MagicMock()
|
||||
channel._loop.is_running.return_value = True
|
||||
channel._add_reaction = MagicMock()
|
||||
channel._send_running_reply = MagicMock()
|
||||
|
||||
event = {
|
||||
"type": "app_mention",
|
||||
"user": "U123456",
|
||||
"text": "<@UBOT> /data-analysis analyze uploads/foo.csv",
|
||||
"channel": "C123",
|
||||
"ts": "1710000000.000100",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"app.channels.slack.asyncio.run_coroutine_threadsafe",
|
||||
side_effect=self._submit_coro,
|
||||
):
|
||||
channel._handle_message_event(event)
|
||||
|
||||
inbound = bus.publish_inbound.call_args.args[0]
|
||||
assert inbound.text == "/data-analysis analyze uploads/foo.csv"
|
||||
assert inbound.msg_type == InboundMessageType.CHAT
|
||||
|
||||
def test_app_mention_preserves_following_user_mention(self):
|
||||
from app.channels.slack import SlackChannel
|
||||
|
||||
bus = MessageBus()
|
||||
bus.publish_inbound = AsyncMock()
|
||||
channel = SlackChannel(bus=bus, config={"bot_user_id": "UBOT"})
|
||||
channel._loop = MagicMock()
|
||||
channel._loop.is_running.return_value = True
|
||||
channel._add_reaction = MagicMock()
|
||||
channel._send_running_reply = MagicMock()
|
||||
|
||||
event = {
|
||||
"type": "app_mention",
|
||||
"user": "U123456",
|
||||
"text": "<@UBOT> <@UASSIGNEE> please review this",
|
||||
"channel": "C123",
|
||||
"ts": "1710000000.000100",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"app.channels.slack.asyncio.run_coroutine_threadsafe",
|
||||
side_effect=self._submit_coro,
|
||||
):
|
||||
channel._handle_message_event(event)
|
||||
|
||||
inbound = bus.publish_inbound.call_args.args[0]
|
||||
assert inbound.text == "<@UASSIGNEE> please review this"
|
||||
assert inbound.msg_type == InboundMessageType.CHAT
|
||||
|
||||
def test_app_mention_preserves_leading_non_bot_mention_when_bot_id_known(self):
|
||||
from app.channels.slack import SlackChannel
|
||||
|
||||
bus = MessageBus()
|
||||
bus.publish_inbound = AsyncMock()
|
||||
channel = SlackChannel(bus=bus, config={"bot_user_id": "UBOT"})
|
||||
channel._loop = MagicMock()
|
||||
channel._loop.is_running.return_value = True
|
||||
channel._add_reaction = MagicMock()
|
||||
channel._send_running_reply = MagicMock()
|
||||
|
||||
event = {
|
||||
"type": "app_mention",
|
||||
"user": "U123456",
|
||||
"text": "<@UASSIGNEE> <@UBOT> please review this",
|
||||
"channel": "C123",
|
||||
"ts": "1710000000.000100",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"app.channels.slack.asyncio.run_coroutine_threadsafe",
|
||||
side_effect=self._submit_coro,
|
||||
):
|
||||
channel._handle_message_event(event)
|
||||
|
||||
inbound = bus.publish_inbound.call_args.args[0]
|
||||
assert inbound.text == "<@UASSIGNEE> <@UBOT> please review this"
|
||||
assert inbound.msg_type == InboundMessageType.CHAT
|
||||
|
||||
def test_app_mention_preserves_leading_non_bot_mention_when_bot_id_unknown(self):
|
||||
from app.channels.slack import SlackChannel
|
||||
|
||||
bus = MessageBus()
|
||||
bus.publish_inbound = AsyncMock()
|
||||
channel = SlackChannel(bus=bus, config={})
|
||||
channel._loop = MagicMock()
|
||||
channel._loop.is_running.return_value = True
|
||||
channel._add_reaction = MagicMock()
|
||||
channel._send_running_reply = MagicMock()
|
||||
|
||||
event = {
|
||||
"type": "app_mention",
|
||||
"user": "U123456",
|
||||
"text": "<@UASSIGNEE> /help <@UBOT>",
|
||||
"channel": "C123",
|
||||
"ts": "1710000000.000100",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"app.channels.slack.asyncio.run_coroutine_threadsafe",
|
||||
side_effect=self._submit_coro,
|
||||
):
|
||||
channel._handle_message_event(event)
|
||||
|
||||
inbound = bus.publish_inbound.call_args.args[0]
|
||||
assert inbound.text == "<@UASSIGNEE> /help <@UBOT>"
|
||||
assert inbound.msg_type == InboundMessageType.CHAT
|
||||
|
||||
def test_socket_event_resolves_bot_user_id_before_app_mention_command_detection(self):
|
||||
from app.channels.slack import SlackChannel
|
||||
|
||||
bus = MessageBus()
|
||||
bus.publish_inbound = AsyncMock()
|
||||
channel = SlackChannel(bus=bus, config={})
|
||||
channel._SocketModeResponse = lambda envelope_id: SimpleNamespace(envelope_id=envelope_id)
|
||||
channel._loop = MagicMock()
|
||||
channel._loop.is_running.return_value = True
|
||||
channel._add_reaction = MagicMock()
|
||||
channel._send_running_reply = MagicMock()
|
||||
|
||||
client = SimpleNamespace(send_socket_mode_response=MagicMock())
|
||||
req = SimpleNamespace(
|
||||
envelope_id="env-1",
|
||||
type="events_api",
|
||||
payload={
|
||||
"authorizations": [{"user_id": "UBOT"}],
|
||||
"event": {
|
||||
"type": "app_mention",
|
||||
"user": "U123456",
|
||||
"text": "<@UBOT> /help",
|
||||
"channel": "C123",
|
||||
"ts": "1710000000.000100",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"app.channels.slack.asyncio.run_coroutine_threadsafe",
|
||||
side_effect=self._submit_coro,
|
||||
):
|
||||
channel._on_socket_event(client, req)
|
||||
|
||||
inbound = bus.publish_inbound.call_args.args[0]
|
||||
assert channel._bot_user_id == "UBOT"
|
||||
assert inbound.text == "/help"
|
||||
assert inbound.msg_type == InboundMessageType.COMMAND
|
||||
|
||||
def test_scalar_allowed_users_warns_and_matches_stringified_event_user_id(self, caplog):
|
||||
from app.channels.slack import SlackChannel
|
||||
|
||||
@@ -2861,6 +3630,86 @@ class TestSlackAllowedUsers:
|
||||
|
||||
|
||||
class TestTelegramSendRetry:
|
||||
def test_start_registers_known_channel_commands(self, monkeypatch):
|
||||
import sys
|
||||
from types import ModuleType
|
||||
|
||||
from app.channels.commands import KNOWN_CHANNEL_COMMANDS
|
||||
from app.channels.telegram import TelegramChannel
|
||||
|
||||
class FakeFilter:
|
||||
def __init__(self, expr: str):
|
||||
self.expr = expr
|
||||
|
||||
def __and__(self, other):
|
||||
return FakeFilter(f"{self.expr}&{other.expr}")
|
||||
|
||||
def __invert__(self):
|
||||
return FakeFilter(f"~{self.expr}")
|
||||
|
||||
class FakeApplication:
|
||||
def __init__(self):
|
||||
self.handlers = []
|
||||
|
||||
def add_handler(self, handler):
|
||||
self.handlers.append(handler)
|
||||
|
||||
fake_app = FakeApplication()
|
||||
|
||||
class FakeApplicationBuilder:
|
||||
def token(self, token):
|
||||
assert token == "test-token"
|
||||
return self
|
||||
|
||||
def build(self):
|
||||
return fake_app
|
||||
|
||||
def fake_command_handler(command, callback):
|
||||
return SimpleNamespace(kind="command", command=command, callback=callback)
|
||||
|
||||
def fake_message_handler(filter_expr, callback):
|
||||
return SimpleNamespace(kind="message", filter_expr=filter_expr, callback=callback)
|
||||
|
||||
telegram_mod = ModuleType("telegram")
|
||||
telegram_ext_mod = ModuleType("telegram.ext")
|
||||
telegram_ext_mod.ApplicationBuilder = FakeApplicationBuilder
|
||||
telegram_ext_mod.CommandHandler = fake_command_handler
|
||||
telegram_ext_mod.MessageHandler = fake_message_handler
|
||||
telegram_ext_mod.filters = SimpleNamespace(TEXT=FakeFilter("TEXT"), COMMAND=FakeFilter("COMMAND"))
|
||||
telegram_mod.ext = telegram_ext_mod
|
||||
monkeypatch.setitem(sys.modules, "telegram", telegram_mod)
|
||||
monkeypatch.setitem(sys.modules, "telegram.ext", telegram_ext_mod)
|
||||
|
||||
class FakeThread:
|
||||
def __init__(self, *, target, daemon):
|
||||
self.target = target
|
||||
self.daemon = daemon
|
||||
|
||||
def start(self):
|
||||
return None
|
||||
|
||||
def join(self, timeout=None):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr("app.channels.telegram.threading.Thread", FakeThread)
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
ch = TelegramChannel(bus=bus, config={"bot_token": "test-token"})
|
||||
|
||||
await ch.start()
|
||||
try:
|
||||
registered_commands = {handler.command for handler in fake_app.handlers if handler.kind == "command"}
|
||||
expected_commands = {command.removeprefix("/") for command in KNOWN_CHANNEL_COMMANDS}
|
||||
assert expected_commands <= registered_commands
|
||||
assert "start" in registered_commands
|
||||
message_filters = {handler.filter_expr.expr for handler in fake_app.handlers if handler.kind == "message"}
|
||||
assert {"TEXT&COMMAND", "TEXT&~COMMAND"} <= message_filters
|
||||
finally:
|
||||
await ch.stop()
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_retries_on_failure_then_succeeds(self):
|
||||
from app.channels.telegram import TelegramChannel
|
||||
|
||||
@@ -2984,6 +3833,47 @@ class TestTelegramPrivateChatThread:
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_private_chat_slash_skill_text_routes_as_chat(self):
|
||||
from app.channels.telegram import TelegramChannel
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
ch = TelegramChannel(bus=bus, config={"bot_token": "test-token"})
|
||||
ch._main_loop = asyncio.get_event_loop()
|
||||
|
||||
update = _make_telegram_update("private", message_id=12, text="/data-analysis analyze uploads/foo.csv")
|
||||
await ch._on_text(update, None)
|
||||
|
||||
msg = await asyncio.wait_for(bus.get_inbound(), timeout=2)
|
||||
assert msg.text == "/data-analysis analyze uploads/foo.csv"
|
||||
assert msg.msg_type == InboundMessageType.CHAT
|
||||
assert msg.topic_id is None
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_slash_skill_addressed_to_telegram_bot_strips_username(self):
|
||||
from app.channels.telegram import TelegramChannel
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
ch = TelegramChannel(bus=bus, config={"bot_token": "test-token"})
|
||||
ch._main_loop = asyncio.get_event_loop()
|
||||
|
||||
update = _make_telegram_update(
|
||||
"group",
|
||||
message_id=13,
|
||||
text="/data-analysis@DeerFlowBot analyze uploads/foo.csv",
|
||||
)
|
||||
context = SimpleNamespace(bot=SimpleNamespace(username="DeerFlowBot"))
|
||||
await ch._on_text(update, context)
|
||||
|
||||
msg = await asyncio.wait_for(bus.get_inbound(), timeout=2)
|
||||
assert msg.text == "/data-analysis analyze uploads/foo.csv"
|
||||
assert msg.msg_type == InboundMessageType.CHAT
|
||||
assert msg.topic_id == "13"
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_private_chat_with_reply_still_uses_none_topic(self):
|
||||
from app.channels.telegram import TelegramChannel
|
||||
|
||||
@@ -3099,6 +3989,25 @@ class TestTelegramPrivateChatThread:
|
||||
|
||||
_run(go())
|
||||
|
||||
def test_cmd_generic_strips_addressed_telegram_bot_username(self):
|
||||
from app.channels.telegram import TelegramChannel
|
||||
|
||||
async def go():
|
||||
bus = MessageBus()
|
||||
ch = TelegramChannel(bus=bus, config={"bot_token": "test-token"})
|
||||
ch._main_loop = asyncio.get_event_loop()
|
||||
|
||||
update = _make_telegram_update("group", message_id=33, text="/status@DeerFlowBot")
|
||||
context = SimpleNamespace(bot=SimpleNamespace(username="DeerFlowBot"))
|
||||
await ch._cmd_generic(update, context)
|
||||
|
||||
msg = await asyncio.wait_for(bus.get_inbound(), timeout=2)
|
||||
assert msg.text == "/status"
|
||||
assert msg.topic_id == "33"
|
||||
assert msg.msg_type == InboundMessageType.COMMAND
|
||||
|
||||
_run(go())
|
||||
|
||||
|
||||
class TestTelegramProcessingOrder:
|
||||
"""Ensure 'working on it...' is sent before inbound is published."""
|
||||
|
||||
Reference in New Issue
Block a user