|
22 | 22 |
|
23 | 23 | from slack_bolt.context.assistant.thread_context_store.store import AssistantThreadContextStore |
24 | 24 |
|
25 | | -from slack_bolt.context.assistant.assistant_utilities import AssistantUtilities |
26 | 25 | from slack_bolt.error import BoltError, BoltUnhandledRequestError |
27 | 26 | from slack_bolt.lazy_listener.thread_runner import ThreadLazyListenerRunner |
28 | 27 | from slack_bolt.listener.builtins import TokenRevocationListeners |
|
70 | 69 | IgnoringSelfEvents, |
71 | 70 | CustomMiddleware, |
72 | 71 | AttachingFunctionToken, |
| 72 | + AttachingAgentKwargs, |
73 | 73 | ) |
74 | 74 | from slack_bolt.middleware.assistant import Assistant |
75 | 75 | from slack_bolt.middleware.message_listener_matches import MessageListenerMatches |
|
83 | 83 | from slack_bolt.oauth.internals import select_consistent_installation_store |
84 | 84 | from slack_bolt.oauth.oauth_settings import OAuthSettings |
85 | 85 | from slack_bolt.request import BoltRequest |
86 | | -from slack_bolt.request.payload_utils import ( |
87 | | - is_assistant_event, |
88 | | - to_event, |
89 | | -) |
90 | 86 | from slack_bolt.response import BoltResponse |
91 | 87 | from slack_bolt.util.utils import ( |
92 | 88 | create_web_client, |
@@ -137,6 +133,7 @@ def __init__( |
137 | 133 | listener_executor: Optional[Executor] = None, |
138 | 134 | # for AI Agents & Assistants |
139 | 135 | assistant_thread_context_store: Optional[AssistantThreadContextStore] = None, |
| 136 | + attaching_agent_kwargs_enabled: bool = True, |
140 | 137 | ): |
141 | 138 | """Bolt App that provides functionalities to register middleware/listeners. |
142 | 139 |
|
@@ -357,6 +354,7 @@ def message_hello(message, say): |
357 | 354 | listener_executor = ThreadPoolExecutor(max_workers=5) |
358 | 355 |
|
359 | 356 | self._assistant_thread_context_store = assistant_thread_context_store |
| 357 | + self._attaching_agent_kwargs_enabled = attaching_agent_kwargs_enabled |
360 | 358 |
|
361 | 359 | self._process_before_response = process_before_response |
362 | 360 | self._listener_runner = ThreadListenerRunner( |
@@ -841,10 +839,13 @@ def ask_for_introduction(event, say): |
841 | 839 | middleware: A list of lister middleware functions. |
842 | 840 | Only when all the middleware call `next()` method, the listener function can be invoked. |
843 | 841 | """ |
| 842 | + middleware = list(middleware) if middleware else [] |
844 | 843 |
|
845 | 844 | def __call__(*args, **kwargs): |
846 | 845 | functions = self._to_listener_functions(kwargs) if kwargs else list(args) |
847 | 846 | 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)) |
848 | 849 | return self._register_listener(list(functions), primary_matcher, matchers, middleware, True) |
849 | 850 |
|
850 | 851 | return __call__ |
@@ -902,6 +903,8 @@ def __call__(*args, **kwargs): |
902 | 903 | primary_matcher = builtin_matchers.message_event( |
903 | 904 | keyword=keyword, constraints=constraints, base_logger=self._base_logger |
904 | 905 | ) |
| 906 | + if self._attaching_agent_kwargs_enabled: |
| 907 | + middleware.insert(0, AttachingAgentKwargs(self._assistant_thread_context_store)) |
905 | 908 | middleware.insert(0, MessageListenerMatches(keyword)) |
906 | 909 | return self._register_listener(list(functions), primary_matcher, matchers, middleware, True) |
907 | 910 |
|
@@ -1398,20 +1401,6 @@ def _init_context(self, req: BoltRequest): |
1398 | 1401 | # It is intended for apps that start lazy listeners from their custom global middleware. |
1399 | 1402 | req.context["listener_runner"] = self.listener_runner |
1400 | 1403 |
|
1401 | | - # For AI Agents & Assistants |
1402 | | - if is_assistant_event(req.body): |
1403 | | - assistant = AssistantUtilities( |
1404 | | - payload=to_event(req.body), # type: ignore[arg-type] |
1405 | | - context=req.context, |
1406 | | - thread_context_store=self._assistant_thread_context_store, |
1407 | | - ) |
1408 | | - req.context["say"] = assistant.say |
1409 | | - req.context["set_status"] = assistant.set_status |
1410 | | - req.context["set_title"] = assistant.set_title |
1411 | | - req.context["set_suggested_prompts"] = assistant.set_suggested_prompts |
1412 | | - req.context["get_thread_context"] = assistant.get_thread_context |
1413 | | - req.context["save_thread_context"] = assistant.save_thread_context |
1414 | | - |
1415 | 1404 | @staticmethod |
1416 | 1405 | def _to_listener_functions( |
1417 | 1406 | kwargs: dict, |
|
0 commit comments