Skip to content

Commit 807e53b

Browse files
prevent any possible breaking changes
1 parent 38c40a1 commit 807e53b

File tree

12 files changed

+232
-50
lines changed

12 files changed

+232
-50
lines changed

slack_bolt/app/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
IgnoringSelfEvents,
7070
CustomMiddleware,
7171
AttachingFunctionToken,
72+
AttachingAgentKwargs,
7273
)
7374
from slack_bolt.middleware.assistant import Assistant
7475
from slack_bolt.middleware.message_listener_matches import MessageListenerMatches
@@ -132,6 +133,7 @@ def __init__(
132133
listener_executor: Optional[Executor] = None,
133134
# for AI Agents & Assistants
134135
assistant_thread_context_store: Optional[AssistantThreadContextStore] = None,
136+
attaching_agent_kwargs_enabled: bool = True,
135137
):
136138
"""Bolt App that provides functionalities to register middleware/listeners.
137139
@@ -352,6 +354,7 @@ def message_hello(message, say):
352354
listener_executor = ThreadPoolExecutor(max_workers=5)
353355

354356
self._assistant_thread_context_store = assistant_thread_context_store
357+
self._attaching_agent_kwargs_enabled = attaching_agent_kwargs_enabled
355358

356359
self._process_before_response = process_before_response
357360
self._listener_runner = ThreadListenerRunner(
@@ -836,10 +839,13 @@ def ask_for_introduction(event, say):
836839
middleware: A list of lister middleware functions.
837840
Only when all the middleware call `next()` method, the listener function can be invoked.
838841
"""
842+
middleware = list(middleware) if middleware else []
839843

840844
def __call__(*args, **kwargs):
841845
functions = self._to_listener_functions(kwargs) if kwargs else list(args)
842846
primary_matcher = builtin_matchers.event(event, base_logger=self._base_logger)
847+
if self._attaching_agent_kwargs_enabled:
848+
middleware.insert(0, AttachingAgentKwargs(self._assistant_thread_context_store))
843849
return self._register_listener(list(functions), primary_matcher, matchers, middleware, True)
844850

845851
return __call__
@@ -897,6 +903,8 @@ def __call__(*args, **kwargs):
897903
primary_matcher = builtin_matchers.message_event(
898904
keyword=keyword, constraints=constraints, base_logger=self._base_logger
899905
)
906+
if self._attaching_agent_kwargs_enabled:
907+
middleware.insert(0, AttachingAgentKwargs(self._assistant_thread_context_store))
900908
middleware.insert(0, MessageListenerMatches(keyword))
901909
return self._register_listener(list(functions), primary_matcher, matchers, middleware, True)
902910

slack_bolt/app/async_app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
AsyncIgnoringSelfEvents,
8787
AsyncUrlVerification,
8888
AsyncAttachingFunctionToken,
89+
AsyncAttachingAgentKwargs,
8990
)
9091
from slack_bolt.middleware.async_custom_middleware import (
9192
AsyncMiddleware,
@@ -141,6 +142,7 @@ def __init__(
141142
verification_token: Optional[str] = None,
142143
# for AI Agents & Assistants
143144
assistant_thread_context_store: Optional[AsyncAssistantThreadContextStore] = None,
145+
attaching_agent_kwargs_enabled: bool = True,
144146
):
145147
"""Bolt App that provides functionalities to register middleware/listeners.
146148
@@ -361,6 +363,7 @@ async def message_hello(message, say): # async function
361363
self._async_listeners: List[AsyncListener] = []
362364

363365
self._assistant_thread_context_store = assistant_thread_context_store
366+
self._attaching_agent_kwargs_enabled = attaching_agent_kwargs_enabled
364367

365368
self._process_before_response = process_before_response
366369
self._async_listener_runner = AsyncioListenerRunner(
@@ -864,10 +867,13 @@ async def ask_for_introduction(event, say):
864867
middleware: A list of lister middleware functions.
865868
Only when all the middleware call `next()` method, the listener function can be invoked.
866869
"""
870+
middleware = list(middleware) if middleware else []
867871

868872
def __call__(*args, **kwargs):
869873
functions = self._to_listener_functions(kwargs) if kwargs else list(args)
870874
primary_matcher = builtin_matchers.event(event, True, base_logger=self._base_logger)
875+
if self._attaching_agent_kwargs_enabled:
876+
middleware.insert(0, AsyncAttachingAgentKwargs(self._assistant_thread_context_store))
871877
return self._register_listener(list(functions), primary_matcher, matchers, middleware, True)
872878

873879
return __call__
@@ -928,6 +934,8 @@ def __call__(*args, **kwargs):
928934
asyncio=True,
929935
base_logger=self._base_logger,
930936
)
937+
if self._attaching_agent_kwargs_enabled:
938+
middleware.insert(0, AsyncAttachingAgentKwargs(self._assistant_thread_context_store))
931939
middleware.insert(0, AsyncMessageListenerMatches(keyword))
932940
return self._register_listener(list(functions), primary_matcher, matchers, middleware, True)
933941

slack_bolt/middleware/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .ssl_check import SslCheck
1818
from .url_verification import UrlVerification
1919
from .attaching_function_token import AttachingFunctionToken
20+
from .attaching_agent_kwargs import AttachingAgentKwargs
2021

2122
builtin_middleware_classes = [
2223
SslCheck,
@@ -41,5 +42,6 @@
4142
"SslCheck",
4243
"UrlVerification",
4344
"AttachingFunctionToken",
45+
"AttachingAgentKwargs",
4446
"builtin_middleware_classes",
4547
]

slack_bolt/middleware/assistant/assistant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from slack_bolt.context.assistant.thread_context_store.store import AssistantThreadContextStore
88
from slack_bolt.listener_matcher.builtins import build_listener_matcher
99

10-
from slack_bolt.middleware.assistant.attaching_assistant_kwargs import AttachingAssistantKwargs
10+
from slack_bolt.middleware.attaching_agent_kwargs import AttachingAgentKwargs
1111
from slack_bolt.request.request import BoltRequest
1212
from slack_bolt.response.response import BoltResponse
1313
from slack_bolt.listener_matcher import CustomListenerMatcher
@@ -272,7 +272,7 @@ def build_listener(
272272
return listener_or_functions
273273
elif isinstance(listener_or_functions, list):
274274
middleware = middleware if middleware else []
275-
middleware.insert(0, AttachingAssistantKwargs(self.thread_context_store))
275+
middleware.insert(0, AttachingAgentKwargs(self.thread_context_store))
276276
functions = listener_or_functions
277277
ack_function = functions.pop(0)
278278

slack_bolt/middleware/assistant/async_assistant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from slack_bolt.listener.asyncio_runner import AsyncioListenerRunner
1010
from slack_bolt.listener_matcher.builtins import build_listener_matcher
11-
from slack_bolt.middleware.assistant.async_attaching_assistant_kwargs import AsyncAttachingAssistantKwargs
11+
from slack_bolt.middleware.attaching_agent_kwargs.async_attaching_agent_kwargs import AsyncAttachingAgentKwargs
1212
from slack_bolt.request.async_request import AsyncBoltRequest
1313
from slack_bolt.response import BoltResponse
1414
from slack_bolt.error import BoltError
@@ -301,7 +301,7 @@ def build_listener(
301301
return listener_or_functions
302302
elif isinstance(listener_or_functions, list):
303303
middleware = middleware if middleware else []
304-
middleware.insert(0, AsyncAttachingAssistantKwargs(self.thread_context_store))
304+
middleware.insert(0, AsyncAttachingAgentKwargs(self.thread_context_store))
305305
functions = listener_or_functions
306306
ack_function = functions.pop(0)
307307

slack_bolt/middleware/assistant/attaching_assistant_kwargs.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

slack_bolt/middleware/async_builtins.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
AsyncMessageListenerMatches,
1111
)
1212
from .attaching_function_token.async_attaching_function_token import AsyncAttachingFunctionToken
13+
from .attaching_agent_kwargs.async_attaching_agent_kwargs import AsyncAttachingAgentKwargs
1314

1415
__all__ = [
1516
"AsyncIgnoringSelfEvents",
@@ -18,4 +19,5 @@
1819
"AsyncUrlVerification",
1920
"AsyncMessageListenerMatches",
2021
"AsyncAttachingFunctionToken",
22+
"AsyncAttachingAgentKwargs",
2123
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .attaching_agent_kwargs import AttachingAgentKwargs
2+
3+
__all__ = [
4+
"AttachingAgentKwargs",
5+
]

slack_bolt/middleware/assistant/async_attaching_assistant_kwargs.py renamed to slack_bolt/middleware/attaching_agent_kwargs/async_attaching_agent_kwargs.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
from slack_bolt.context.assistant.thread_context_store.async_store import AsyncAssistantThreadContextStore
55
from slack_bolt.middleware.async_middleware import AsyncMiddleware
66
from slack_bolt.request.async_request import AsyncBoltRequest
7-
from slack_bolt.request.payload_utils import to_event
7+
from slack_bolt.request.payload_utils import is_assistant_event, to_event
88
from slack_bolt.response import BoltResponse
99

1010

11-
class AsyncAttachingAssistantKwargs(AsyncMiddleware):
11+
class AsyncAttachingAgentKwargs(AsyncMiddleware):
1212

1313
thread_context_store: Optional[AsyncAssistantThreadContextStore]
1414

15-
def __init__(self, thread_context_store: Optional[AsyncAssistantThreadContextStore]):
15+
def __init__(self, thread_context_store: Optional[AsyncAssistantThreadContextStore] = None):
1616
self.thread_context_store = thread_context_store
1717

1818
async def async_process(
@@ -24,15 +24,16 @@ async def async_process(
2424
) -> Optional[BoltResponse]:
2525
event = to_event(req.body)
2626
if event is not None:
27-
assistant = AsyncAssistantUtilities(
28-
payload=event,
29-
context=req.context,
30-
thread_context_store=self.thread_context_store,
31-
)
32-
req.context["say"] = assistant.say
33-
req.context["set_status"] = assistant.set_status
34-
req.context["set_title"] = assistant.set_title
35-
req.context["set_suggested_prompts"] = assistant.set_suggested_prompts
36-
req.context["get_thread_context"] = assistant.get_thread_context
37-
req.context["save_thread_context"] = assistant.save_thread_context
27+
if is_assistant_event(req.body):
28+
assistant = AsyncAssistantUtilities(
29+
payload=event,
30+
context=req.context,
31+
thread_context_store=self.thread_context_store,
32+
)
33+
req.context["say"] = assistant.say
34+
req.context["set_status"] = assistant.set_status
35+
req.context["set_title"] = assistant.set_title
36+
req.context["set_suggested_prompts"] = assistant.set_suggested_prompts
37+
req.context["get_thread_context"] = assistant.get_thread_context
38+
req.context["save_thread_context"] = assistant.save_thread_context
3839
return await next()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import Optional, Callable
2+
3+
from slack_bolt.context.assistant.assistant_utilities import AssistantUtilities
4+
from slack_bolt.context.assistant.thread_context_store.store import AssistantThreadContextStore
5+
from slack_bolt.middleware import Middleware
6+
from slack_bolt.request.payload_utils import is_assistant_event, to_event
7+
from slack_bolt.request.request import BoltRequest
8+
from slack_bolt.response.response import BoltResponse
9+
10+
11+
class AttachingAgentKwargs(Middleware):
12+
13+
thread_context_store: Optional[AssistantThreadContextStore]
14+
15+
def __init__(self, thread_context_store: Optional[AssistantThreadContextStore] = None):
16+
self.thread_context_store = thread_context_store
17+
18+
def process(self, *, req: BoltRequest, resp: BoltResponse, next: Callable[[], BoltResponse]) -> Optional[BoltResponse]:
19+
event = to_event(req.body)
20+
if event is not None:
21+
if is_assistant_event(req.body):
22+
assistant = AssistantUtilities(
23+
payload=event,
24+
context=req.context,
25+
thread_context_store=self.thread_context_store,
26+
)
27+
req.context["say"] = assistant.say
28+
req.context["set_status"] = assistant.set_status
29+
req.context["set_title"] = assistant.set_title
30+
req.context["set_suggested_prompts"] = assistant.set_suggested_prompts
31+
req.context["get_thread_context"] = assistant.get_thread_context
32+
req.context["save_thread_context"] = assistant.save_thread_context
33+
return next()

0 commit comments

Comments
 (0)