Skip to content

Commit e6e456a

Browse files
committed
perf: defer BoltAgent construction to when listener requests it
1 parent 10c56e9 commit e6e456a

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

slack_bolt/kwargs_injection/async_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def build_async_required_kwargs(
5858
"set_suggested_prompts": request.context.set_suggested_prompts,
5959
"get_thread_context": request.context.get_thread_context,
6060
"save_thread_context": request.context.save_thread_context,
61-
"agent": request.context.agent,
6261
# middleware
6362
"next": next_func,
6463
"next_": next_func, # for the middleware using Python's built-in `next()` function
@@ -84,6 +83,10 @@ def build_async_required_kwargs(
8483
if k not in all_available_args:
8584
all_available_args[k] = v
8685

86+
# Defer agent creation to avoid constructing AsyncBoltAgent on every request
87+
if "agent" in required_arg_names or "args" in required_arg_names:
88+
all_available_args["agent"] = request.context.agent
89+
8790
if len(required_arg_names) > 0:
8891
# To support instance/class methods in a class for listeners/middleware,
8992
# check if the first argument is either self or cls

slack_bolt/kwargs_injection/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def build_required_kwargs(
5757
"set_title": request.context.set_title,
5858
"set_suggested_prompts": request.context.set_suggested_prompts,
5959
"save_thread_context": request.context.save_thread_context,
60-
"agent": request.context.agent,
6160
# middleware
6261
"next": next_func,
6362
"next_": next_func, # for the middleware using Python's built-in `next()` function
@@ -83,6 +82,10 @@ def build_required_kwargs(
8382
if k not in all_available_args:
8483
all_available_args[k] = v
8584

85+
# Defer agent creation to avoid constructing BoltAgent on every request
86+
if "agent" in required_arg_names or "args" in required_arg_names:
87+
all_available_args["agent"] = request.context.agent
88+
8689
if len(required_arg_names) > 0:
8790
# To support instance/class methods in a class for listeners/middleware,
8891
# check if the first argument is either self or cls

0 commit comments

Comments
 (0)