ESM
import { createHumanInTheLoopMiddleware, createHumanInTheLoopToolMiddleware, getHumanInTheLoopPayload } from "@arcgis/ai-components/agent-utils/middlewares/humanInTheLoop.js";

Type definitions

HumanInTheLoopInterruptRequest

Type definition
Type parameters
<TCustomState extends AgentData = AgentData>
Since
ArcGIS Maps SDK for JavaScript 5.1
Supertypes
AgentRunRequest<TCustomState>

agentId

Property
Type
string
Since
ArcGIS Maps SDK for JavaScript 5.1

The ID of the agent requesting human intervention. This can be used to correlate the interrupt with a specific agent, especially in cases where multiple agents may be running concurrently and using the same middleware.

HumanInTheLoopInterrupt

Type definition
Since
ArcGIS Maps SDK for JavaScript 5.1
Supertypes
Partial<Omit<UiInterrupt, "agentId" | "id">>

id

Property
Type
never | undefined

HumanInTheLoopMiddlewareOptions

Type definition
Type parameters
<TCustomState extends AgentData = AgentData>
Since
ArcGIS Maps SDK for JavaScript 5.1

Options for configuring the HumanInTheLoopMiddleware.

id

readonly Property
Type
string | undefined
Since
ArcGIS Maps SDK for JavaScript 5.2

Stable approval ID. Optional for a single approval. Provide unique, non-empty IDs when the agent has multiple approval middlewares.

Default value
"human-in-the-loop"

interrupt

Method
Signature
interrupt (request: HumanInTheLoopInterruptRequest<TCustomState>): HumanInTheLoopInterrupt | Promise<HumanInTheLoopInterrupt>
Since
ArcGIS Maps SDK for JavaScript 5.1

Creates interrupt details only when this step has no matching response.

Parameters

ParameterTypeDescriptionRequired
request

The agent run request that triggered the interrupt, containing details about the agent, node, and execution context.

Returns
HumanInTheLoopInterrupt | Promise<HumanInTheLoopInterrupt>

An object containing the interrupt details, which will be thrown as a NodeInterrupt to pause the agent's execution.

outputKey

Property
Type
keyof AgentGraphState | undefined
Since
ArcGIS Maps SDK for JavaScript 5.1

An optional key in the agent state where the human response should be stored.

HumanInTheLoopToolMessageRequest

Type definition
Type parameters
<TInput extends AgentData = AgentData>
Since
ArcGIS Maps SDK for JavaScript 5.1
Supertypes
FunctionToolMiddlewareRequest<TInput>

args

Property
Type
object
Since
ArcGIS Maps SDK for JavaScript 5.1

The arguments passed to the tool that is requesting human intervention.

toolCallId

Property
Type
string | undefined
Since
ArcGIS Maps SDK for JavaScript 5.1

The id of the tool call that is requesting human intervention.

toolName

Property
Type
string
Since
ArcGIS Maps SDK for JavaScript 5.1

The name of the tool that is requesting human intervention.

HumanInTheLoopToolMiddlewareOptions

Type definition
Type parameters
<TInput extends AgentData = AgentData>
Since
ArcGIS Maps SDK for JavaScript 5.1

This middleware is designed to allow for human intervention during agent execution by throwing an interrupt that can be handled by the orchestrator to pause the agent and prompt the user for input or approval.

id

readonly Property
Type
string | undefined
Since
ArcGIS Maps SDK for JavaScript 5.2

Stable approval ID. Optional for a single approval. Provide unique, non-empty IDs when the tool has multiple approval middlewares.

Default value
"human-in-the-loop-tool"

interrupt

Method
Signature
interrupt (request: HumanInTheLoopToolMessageRequest<TInput>): HumanInTheLoopInterrupt | Promise<HumanInTheLoopInterrupt>
Since
ArcGIS Maps SDK for JavaScript 5.1

Creates interrupt details only when this step has no matching response.

Parameters

ParameterTypeDescriptionRequired
request

The tool middleware request that triggered the interrupt, containing details about the tool, input arguments, and execution context.

Returns
HumanInTheLoopInterrupt | Promise<HumanInTheLoopInterrupt>

outputKey

Property
Type
keyof TInput | undefined
Since
ArcGIS Maps SDK for JavaScript 5.1

An optional key in the tool input where the human response should be stored.

Functions

createHumanInTheLoopMiddleware

Function
Type parameters
<TCustomState extends AgentData = AgentData>
Since
ArcGIS Maps SDK for JavaScript 5.1

Creates agent-level middleware that pauses the agent before execution when a response from the end user is needed. The application can use the interrupt to request approval, a selection, or additional information.

Use it when the response applies to the agent's overall task. For example, require approval before a task that may modify data or make a consequential decision: "Do you approve saving the results? (This will modify the saved feature layer.)"

Add it to the middlewares array of a FunctionAgent, LLMAgent, or WorkflowAgent.

Signature
createHumanInTheLoopMiddleware <TCustomState extends AgentData = AgentData>(options?: HumanInTheLoopMiddlewareOptions<TCustomState>): AgentLifecycleMiddleware<TCustomState>

Parameters

ParameterTypeDescriptionRequired
options

Configuration options for the middleware, including the interrupt handler and optional output key for storing the human response in the agent state.

Returns
AgentLifecycleMiddleware

Agent lifecycle middleware to add to an agent's middlewares array.

Example
const agent = new LLMAgent({
name: "Approved task",
prompt: "Complete the user's task after approval.",
middlewares: [
createHumanInTheLoopMiddleware({
interrupt: async ({ agentId }) => ({
message: `Agent ${agentId} is requesting approval to continue. Do you approve?`,
}),
}),
],
});

createHumanInTheLoopToolMiddleware

Function
Type parameters
<TInput extends AgentData = AgentData, TOutput = unknown>
Since
ArcGIS Maps SDK for JavaScript 5.1

Creates tool middleware that pauses a tool call before the tool executes when a response from the end user is needed. The application can use the interrupt to request approval, a selection, or additional information.

Use it when the response applies to a specific tool operation. For example, ask what name to use for the saved results, or require approval before a tool changes data: "Do you approve updating the Parcels layer with these values? (This will modify its saved data.)"

Add it to a FunctionTool's middlewares array. The tool can be supplied through an LLMAgent's tools array or a named toolLoaders entry on an SkillAgent referenced by a skill's allowed-tools metadata.

Signature
createHumanInTheLoopToolMiddleware <TInput extends AgentData = AgentData, TOutput = unknown>(options?: HumanInTheLoopToolMiddlewareOptions<TInput>): FunctionToolMiddleware<TInput, TOutput>

Parameters

ParameterTypeDescriptionRequired
options

Configuration options for the middleware, including the interrupt handler and optional output key for storing the human response in the tool input.

Returns
FunctionToolMiddleware<TInput, TOutput>

FunctionTool middleware to add to a tool's middlewares array.

Example
const myTool = new FunctionTool({
name: "sensitiveOperation",
description: "Executes a sensitive operation after approval.",
execute: async (input) => {
// perform sensitive operation
},
middlewares: [
createHumanInTheLoopToolMiddleware({
interrupt: async ({ toolName, args }) => ({
message: `Tool ${toolName} is requesting approval to execute with arguments: ${JSON.stringify(args)}. Do you approve?`,
}),
}),
],
});

getHumanInTheLoopPayload

Function
Type parameters
<TPayload = unknown>
Since
ArcGIS Maps SDK for JavaScript 5.1

This function retrieves the human-in-the-loop (HITL) response payload from the agent execution configuration.

Signature
getHumanInTheLoopPayload <TPayload = unknown>(config: AgentExecutionConfig | undefined): TPayload | undefined

Parameters

ParameterTypeDescriptionRequired
config

The agent execution configuration that may contain a HITL response.

Returns
TPayload | undefined

The payload from the HITL response if available; otherwise, undefined.