Skip to content

Commit 724ea5f

Browse files
committed
fix: disallow partial overrides of context args in agent chat_stream()
1 parent cf5ef98 commit 724ea5f

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

slack_bolt/agent/agent.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def chat_stream(
5858
Returns:
5959
A new ``ChatStream`` instance.
6060
"""
61+
provided = [arg for arg in (channel, thread_ts, recipient_team_id, recipient_user_id) if arg is not None]
62+
if provided and len(provided) < 4:
63+
raise ValueError(
64+
"Either provide all of channel, thread_ts, recipient_team_id, and recipient_user_id, or none of them"
65+
)
6166
resolved_channel = channel or self._channel_id
6267
resolved_thread_ts = thread_ts or self._thread_ts
6368
if resolved_channel is None:

slack_bolt/agent/async_agent.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ async def chat_stream(
5555
Returns:
5656
A new ``AsyncChatStream`` instance.
5757
"""
58+
provided = [arg for arg in (channel, thread_ts, recipient_team_id, recipient_user_id) if arg is not None]
59+
if provided and len(provided) < 4:
60+
raise ValueError(
61+
"Either provide all of channel, thread_ts, recipient_team_id, and recipient_user_id, or none of them"
62+
)
5863
resolved_channel = channel or self._channel_id
5964
resolved_thread_ts = thread_ts or self._thread_ts
6065
if resolved_channel is None:

tests/scenario_tests/test_events_agent.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ def test_agent_chat_stream_overrides_context_defaults(self):
106106
)
107107
assert stream is not None
108108

109+
def test_agent_chat_stream_rejects_partial_overrides(self):
110+
"""Passing only some of the four context args raises ValueError."""
111+
client = MagicMock(spec=WebClient)
112+
agent = BoltAgentDirect(
113+
client=client,
114+
channel_id="C111",
115+
thread_ts="1234567890.123456",
116+
team_id="T111",
117+
user_id="W222",
118+
)
119+
with pytest.raises(ValueError, match="Either provide all of"):
120+
agent.chat_stream(channel="C999")
121+
109122
def test_agent_chat_stream_passes_extra_kwargs(self):
110123
"""Extra kwargs are forwarded to WebClient.chat_stream()."""
111124
client = MagicMock(spec=WebClient)

tests/scenario_tests_async/test_events_agent.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ async def test_agent_chat_stream_overrides_context_defaults(self):
124124
)
125125
assert stream is not None
126126

127+
@pytest.mark.asyncio
128+
async def test_agent_chat_stream_rejects_partial_overrides(self):
129+
"""Passing only some of the four context args raises ValueError."""
130+
client = MagicMock(spec=AsyncWebClient)
131+
agent = AsyncBoltAgent(
132+
client=client,
133+
channel_id="C111",
134+
thread_ts="1234567890.123456",
135+
team_id="T111",
136+
user_id="W222",
137+
)
138+
with pytest.raises(ValueError, match="Either provide all of"):
139+
await agent.chat_stream(channel="C999")
140+
127141
@pytest.mark.asyncio
128142
async def test_agent_chat_stream_passes_extra_kwargs(self):
129143
"""Extra kwargs are forwarded to AsyncWebClient.chat_stream()."""

0 commit comments

Comments
 (0)