HumanInTheLoopMiddleware
Approve, reject, edit, defer, and suspend tool calls with policy- and gateway-driven human approval
Class: bxModules.bxai.models.middleware.core.HumanInTheLoopMiddleware
Adds human approval to tool calls using three collaborators:
IApprovalPolicydecides if a tool call needs approvalIGatewaypresents and resolves the decisionHumanInteractionCoordinatortracks suspensions and decisions
Features
Tool-name approval policy by default
Optional callback-based or custom policy-based approval
CLI mode with default
CliGatewayWeb mode with deferred suspension
Gateway mode for platform-backed approvals
Batch-aware defer/suspend via
afterToolBatchResume-aware behavior (
agent.resume) with approve/reject/edit mappingDurable grant support via
IDecisionStore
Constructor
Configuration
toolsRequiringApproval
array
[]
Tool names requiring approval when no custom policy is provided
mode
string
"cli"
Approval mode without explicit gateway: "cli" or "web"
showArguments
boolean
true
Include tool arguments in approval prompt/message
approvalCallback
function
none
Function policy fallback: returns true when approval is required
policy
IApprovalPolicy
ToolNameApprovalPolicy
Explicit policy override
gateway
IGateway
CliGateway unless mode="web"
Gateway used for human interaction
decisionStore
IDecisionStore
aiDecisionStore()
Durable decision/grant store
Hooks Used
onAttachbeforeToolCallafterToolBatch
Mode Behavior
CLI mode
Auto-attaches
CliGatewaywhen no explicit gateway is providedBlocks for immediate terminal decision
Web mode
No default gateway
Uses
defer()on individual tool calls andsuspend()once per turn inafterToolBatchRequires an agent checkpointer to resume safely
Explicit gateway
Uses provided gateway regardless of mode
Asynchronous gateways return suspensions with suspension IDs
Resume Semantics
When resuming, middleware maps human decisions to middleware outcomes:
approve ->
AiMiddlewareResult.approve()reject ->
AiMiddlewareResult.reject(reason)edit -> mutates provider-specific tool-call arguments, then
continue()cancel/other ->
AiMiddlewareResult.cancel(reason)
Example
Notes
If suspension is possible and the agent has no checkpointer, middleware throws on attach.
Unknown
modevalues fall back to CLI gateway with a deprecation warning.afterToolBatchenables batched suspension so multiple pending decisions can pause as one checkpoint.
Last updated