Middleware¶
Sub-agent dispatch middleware and request types.
TemporalSubAgentMiddleware¶
TemporalSubAgentMiddleware
¶
TemporalSubAgentMiddleware(
subagent_specs: dict[str, str] | None = None,
default_graph_ref: str | None = None,
)
Temporal-aware replacement for SubAgentMiddleware.
Instead of invoking sub-agents in-process, the task tool stores a
SubAgentRequest in the _child_workflow_requests_var context variable
and returns a placeholder string. The Activity collects pending requests
after execution and includes them in the output.
This class is standalone (does not subclass deepagents middleware) to avoid a hard dependency on the deepagents package during unit testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subagent_specs
|
dict[str, str] | None
|
Mapping of sub-agent type names to their graph definition references in GraphRegistry. |
None
|
default_graph_ref
|
str | None
|
Default graph reference for sub-agents not in
|
None
|
Source code in deepagent_temporal/middleware.py
build_task_tool
¶
Build a task tool function that stores SubAgentRequests.
Returns a callable that, when invoked with an instruction and
sub-agent type, appends a SubAgentRequest to the context
variable and returns a placeholder string.
Source code in deepagent_temporal/middleware.py
SubAgentRequest¶
SubAgentRequest
dataclass
¶
SubAgentRequest(
subagent_type: str,
instruction: str,
tool_call_id: str,
initial_state: dict[str, Any],
graph_definition_ref: str,
)
Request for a sub-agent Child Workflow dispatch.
Stored in context variable during tool execution; collected by the
Activity and returned in NodeActivityOutput.child_workflow_requests.
All fields must be JSON-serializable for Temporal payload conversion.
Attributes:
| Name | Type | Description |
|---|---|---|
subagent_type |
str
|
Type/name of the sub-agent to dispatch. |
instruction |
str
|
The instruction/prompt for the sub-agent. |
tool_call_id |
str
|
The tool call ID to map the result back. |
initial_state |
dict[str, Any]
|
Initial state for the sub-agent (excluded keys removed). |
graph_definition_ref |
str
|
Reference to the pre-registered sub-agent graph. |
to_dict
¶
Serialize to JSON-safe dict for Temporal payload.
Source code in deepagent_temporal/middleware.py
from_dict
classmethod
¶
from_dict(d: dict[str, Any]) -> SubAgentRequest
Deserialize from dict.
Source code in deepagent_temporal/middleware.py
collect_pending_requests¶
collect_pending_requests
¶
Collect pending SubAgentRequests from the context variable.
Returns the list of serialized requests and resets the context variable.