mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-09-30 11:33:14 +00:00
* feat: Add slack trigger webhook * cr * better username tagging, react with eyes * cr * cr * cr
114 lines
3.9 KiB
Python
114 lines
3.9 KiB
Python
from agent.utils.slack import (
|
|
format_slack_messages_for_prompt,
|
|
replace_bot_mention_with_username,
|
|
select_slack_context_messages,
|
|
strip_bot_mention,
|
|
)
|
|
from agent.webapp import generate_thread_id_from_slack_thread
|
|
|
|
|
|
def test_generate_thread_id_from_slack_thread_is_deterministic() -> None:
|
|
channel_id = "C12345"
|
|
thread_ts = "1730900000.123456"
|
|
first = generate_thread_id_from_slack_thread(channel_id, thread_ts)
|
|
second = generate_thread_id_from_slack_thread(channel_id, thread_ts)
|
|
assert first == second
|
|
assert len(first) == 36
|
|
|
|
|
|
def test_select_slack_context_messages_uses_thread_start_when_no_prior_mention() -> None:
|
|
bot_user_id = "UBOT"
|
|
messages = [
|
|
{"ts": "1.0", "text": "hello", "user": "U1"},
|
|
{"ts": "2.0", "text": "context", "user": "U2"},
|
|
{"ts": "3.0", "text": "<@UBOT> please help", "user": "U1"},
|
|
]
|
|
|
|
selected, mode = select_slack_context_messages(messages, "3.0", bot_user_id)
|
|
|
|
assert mode == "thread_start"
|
|
assert [item["ts"] for item in selected] == ["1.0", "2.0", "3.0"]
|
|
|
|
|
|
def test_select_slack_context_messages_uses_previous_mention_boundary() -> None:
|
|
bot_user_id = "UBOT"
|
|
messages = [
|
|
{"ts": "1.0", "text": "hello", "user": "U1"},
|
|
{"ts": "2.0", "text": "<@UBOT> first request", "user": "U1"},
|
|
{"ts": "3.0", "text": "extra context", "user": "U2"},
|
|
{"ts": "4.0", "text": "<@UBOT> second request", "user": "U3"},
|
|
]
|
|
|
|
selected, mode = select_slack_context_messages(messages, "4.0", bot_user_id)
|
|
|
|
assert mode == "last_mention"
|
|
assert [item["ts"] for item in selected] == ["2.0", "3.0", "4.0"]
|
|
|
|
|
|
def test_select_slack_context_messages_ignores_messages_after_current_event() -> None:
|
|
bot_user_id = "UBOT"
|
|
messages = [
|
|
{"ts": "1.0", "text": "<@UBOT> first request", "user": "U1"},
|
|
{"ts": "2.0", "text": "follow-up", "user": "U2"},
|
|
{"ts": "3.0", "text": "<@UBOT> second request", "user": "U3"},
|
|
{"ts": "4.0", "text": "after event", "user": "U4"},
|
|
]
|
|
|
|
selected, mode = select_slack_context_messages(messages, "3.0", bot_user_id)
|
|
|
|
assert mode == "last_mention"
|
|
assert [item["ts"] for item in selected] == ["1.0", "2.0", "3.0"]
|
|
|
|
|
|
def test_strip_bot_mention_removes_bot_tag() -> None:
|
|
assert strip_bot_mention("<@UBOT> please check", "UBOT") == "please check"
|
|
|
|
|
|
def test_strip_bot_mention_removes_bot_username_tag() -> None:
|
|
assert (
|
|
strip_bot_mention("@open-swe please check", "UBOT", bot_username="open-swe")
|
|
== "please check"
|
|
)
|
|
|
|
|
|
def test_replace_bot_mention_with_username() -> None:
|
|
assert (
|
|
replace_bot_mention_with_username("<@UBOT> can you help?", "UBOT", "open-swe")
|
|
== "@open-swe can you help?"
|
|
)
|
|
|
|
|
|
def test_format_slack_messages_for_prompt_uses_name_and_id() -> None:
|
|
formatted = format_slack_messages_for_prompt(
|
|
[{"ts": "1.0", "text": "hello", "user": "U123"}],
|
|
{"U123": "alice"},
|
|
)
|
|
|
|
assert formatted == "@alice(U123): hello"
|
|
|
|
|
|
def test_format_slack_messages_for_prompt_replaces_bot_id_mention_in_text() -> None:
|
|
formatted = format_slack_messages_for_prompt(
|
|
[{"ts": "1.0", "text": "<@UBOT> status update?", "user": "U123"}],
|
|
{"U123": "alice"},
|
|
bot_user_id="UBOT",
|
|
bot_username="open-swe",
|
|
)
|
|
|
|
assert formatted == "@alice(U123): @open-swe status update?"
|
|
|
|
|
|
def test_select_slack_context_messages_detects_username_mention() -> None:
|
|
selected, mode = select_slack_context_messages(
|
|
[
|
|
{"ts": "1.0", "text": "@open-swe first request", "user": "U1"},
|
|
{"ts": "2.0", "text": "follow up", "user": "U2"},
|
|
{"ts": "3.0", "text": "@open-swe second request", "user": "U3"},
|
|
],
|
|
"3.0",
|
|
bot_user_id="UBOT",
|
|
bot_username="open-swe",
|
|
)
|
|
|
|
assert mode == "last_mention"
|
|
assert [item["ts"] for item in selected] == ["1.0", "2.0", "3.0"]
|